Skip to content

Commit afa685b

Browse files
utest: perform the signal_handler test in a child process so we restore the orignal signal handlers at the end :^)
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent bd09767 commit afa685b

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

userland/utest/src/main.rs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ pub struct Test<'a> {
3232
}
3333

3434
static TEST_FUNCTIONS: &[&'static Test<'static>] = &[
35-
// TODO: When did these tests start failing?
35+
// TODO: Why does clone process fail?
3636
// &clone_process,
37-
// &forked_pipe,
37+
&forked_pipe,
3838
&signal_handler,
3939
&dup_fds,
4040
&dup2_redirect_stdout,
@@ -126,49 +126,58 @@ fn dup_fds() -> Result<(), AeroSyscallError> {
126126

127127
#[utest_proc::test]
128128
fn signal_handler() -> Result<(), AeroSyscallError> {
129-
let mut pipe = [0usize; 2];
130-
sys_pipe(&mut pipe, OpenFlags::empty())?;
129+
let child = sys_fork()?;
131130

132-
static PIPE_WRITE: AtomicUsize = AtomicUsize::new(0);
133-
PIPE_WRITE.store(pipe[1], Ordering::SeqCst);
131+
// We perform the test in a forked process since then we dont
132+
// have to worry about restoring the signal handlers.
133+
if child == 0 {
134+
let mut pipe = [0usize; 2];
135+
sys_pipe(&mut pipe, OpenFlags::empty())?;
134136

135-
fn handle_segmentation_fault(fault: usize) {
136-
core::assert_eq!(fault, 11);
137+
static PIPE_WRITE: AtomicUsize = AtomicUsize::new(0);
138+
PIPE_WRITE.store(pipe[1], Ordering::SeqCst);
137139

138-
let pfd = PIPE_WRITE.load(Ordering::SeqCst);
140+
fn handle_segmentation_fault(fault: usize) {
141+
core::assert_eq!(fault, 11);
139142

140-
// Dont worry about closing the file descriptors since they will
141-
// be auto closed by the parent.
142-
sys_write(pfd, b"yes").expect("failed to write to the pipe");
143-
sys_exit(0);
144-
}
143+
let pfd = PIPE_WRITE.load(Ordering::SeqCst);
145144

146-
// Install the signal handler.
147-
let handler = SignalHandler::Handle(handle_segmentation_fault);
148-
let sigaction = SigAction::new(handler, 0, SignalFlags::empty());
145+
// Dont worry about closing the file descriptors since they will
146+
// be auto closed by the parent.
147+
sys_write(pfd, b"yes").expect("failed to write to the pipe");
148+
sys_exit(0);
149+
}
149150

150-
sys_sigaction(SIGSEGV, Some(&sigaction), None)
151-
.expect("failed to install the segmentation fault handler");
151+
// Install the signal handler.
152+
let handler = SignalHandler::Handle(handle_segmentation_fault);
153+
let sigaction = SigAction::new(handler, 0, SignalFlags::empty());
152154

153-
// On fork the signal handler will be copied over to the child process.
154-
let child = sys_fork()?;
155+
sys_sigaction(SIGSEGV, Some(&sigaction), None)
156+
.expect("failed to install the segmentation fault handler");
155157

156-
if child == 0 {
157-
// Create a traditional page fault :^)
158-
unsafe {
159-
*(0xcafebabe as *mut usize) = 69;
158+
// On fork the signal handler will be copied over to the child process.
159+
let child = sys_fork()?;
160+
161+
if child == 0 {
162+
// Create a traditional page fault :^)
163+
unsafe {
164+
*(0xcafebabe as *mut usize) = 69;
165+
}
166+
} else {
167+
let mut buffer = [0; 3];
168+
sys_read(pipe[0], &mut buffer)?;
169+
170+
core::assert_eq!(&buffer, b"yes");
160171
}
161-
} else {
162-
let mut buffer = [0; 3];
163-
sys_read(pipe[0], &mut buffer)?;
164172

165-
core::assert_eq!(&buffer, b"yes");
173+
// Close the pipe
174+
sys_close(pipe[0])?;
175+
sys_close(pipe[1])?;
176+
} else {
177+
let mut status = 0;
178+
sys_waitpid(child, &mut status, 0)?;
166179
}
167180

168-
// Close the pipe/
169-
sys_close(pipe[0])?;
170-
sys_close(pipe[1])?;
171-
172181
Ok(())
173182
}
174183

0 commit comments

Comments
 (0)