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