]> git.saurik.com Git - apple/xnu.git/blob - tests/sigchld_return.c
xnu-6153.41.3.tar.gz
[apple/xnu.git] / tests / sigchld_return.c
1 #include <signal.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
6
7 #include <darwintest.h>
8
9 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
10
11 static int exitcode = 0x6789BEEF;
12 int should_exit = 0;
13
14 void
15 handler(int sig, siginfo_t *sip, __unused void *uconp)
16 {
17 /* Should handle the SIGCHLD signal */
18 T_ASSERT_EQ_INT(sig, SIGCHLD, "Captured signal returns 0x%x, expected SIGCHLD (0x%x).", sig, SIGCHLD);
19 T_QUIET; T_ASSERT_NOTNULL(sip, "siginfo_t returned NULL but should have returned data.");
20 T_ASSERT_EQ_INT(sip->si_code, CLD_EXITED, "si_code returns 0x%x, expected CLD_EXITED (0x%x).", sip->si_code, CLD_EXITED);
21 T_ASSERT_EQ_INT(sip->si_status, exitcode, "si_status returns 0x%08X, expected the child's exit code (0x%08X).", sip->si_status, exitcode);
22 should_exit = 1;
23 }
24
25
26 T_DECL(sigchldreturn, "checks that a child process exited with an exitcode returns correctly to parent", T_META_CHECK_LEAKS(false))
27 {
28 struct sigaction act;
29 int pid;
30
31 act.sa_sigaction = handler;
32 act.sa_flags = SA_SIGINFO;
33
34 /* Set action for signal */
35 T_QUIET; T_ASSERT_POSIX_SUCCESS(sigaction(SIGCHLD, &act, NULL), "Calling sigaction() failed for SIGCHLD");
36
37 /* Now fork a child that just exits */
38 pid = fork();
39 T_QUIET; T_ASSERT_NE_INT(pid, -1, "fork() failed!");
40
41 if (pid == 0) {
42 /* Child process! */
43 exit(exitcode);
44 }
45
46 /* Main program that did the fork */
47 /* We should process the signal, then exit */
48 while (!should_exit) {
49 sleep(1);
50 }
51 }