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