]> git.saurik.com Git - apple/xnu.git/blame - tests/sigchld_return.c
xnu-7195.101.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>
f427ee49
A
6#include <sys/wait.h>
7#include <sys/types.h>
5ba3f43e
A
8
9#include <darwintest.h>
10
cb323159 11T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
5ba3f43e
A
12
13static int exitcode = 0x6789BEEF;
f427ee49 14static int should_exit = 0;
5ba3f43e 15
f427ee49 16static void
0a7de745 17handler(int sig, siginfo_t *sip, __unused void *uconp)
5ba3f43e 18{
0a7de745
A
19 /* Should handle the SIGCHLD signal */
20 T_ASSERT_EQ_INT(sig, SIGCHLD, "Captured signal returns 0x%x, expected SIGCHLD (0x%x).", sig, SIGCHLD);
21 T_QUIET; T_ASSERT_NOTNULL(sip, "siginfo_t returned NULL but should have returned data.");
22 T_ASSERT_EQ_INT(sip->si_code, CLD_EXITED, "si_code returns 0x%x, expected CLD_EXITED (0x%x).", sip->si_code, CLD_EXITED);
23 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);
24 should_exit = 1;
5ba3f43e
A
25}
26
27
28T_DECL(sigchldreturn, "checks that a child process exited with an exitcode returns correctly to parent", T_META_CHECK_LEAKS(false))
29{
0a7de745
A
30 struct sigaction act;
31 int pid;
5ba3f43e 32
0a7de745
A
33 act.sa_sigaction = handler;
34 act.sa_flags = SA_SIGINFO;
5ba3f43e 35
0a7de745
A
36 /* Set action for signal */
37 T_QUIET; T_ASSERT_POSIX_SUCCESS(sigaction(SIGCHLD, &act, NULL), "Calling sigaction() failed for SIGCHLD");
5ba3f43e 38
0a7de745
A
39 /* Now fork a child that just exits */
40 pid = fork();
41 T_QUIET; T_ASSERT_NE_INT(pid, -1, "fork() failed!");
5ba3f43e 42
0a7de745
A
43 if (pid == 0) {
44 /* Child process! */
45 exit(exitcode);
46 }
5ba3f43e 47
0a7de745
A
48 /* Main program that did the fork */
49 /* We should process the signal, then exit */
50 while (!should_exit) {
51 sleep(1);
52 }
5ba3f43e 53}
f427ee49
A
54
55T_DECL(sigabrt_test, "check that child process' exitcode contains signum = SIGABRT", T_META_CHECK_LEAKS(false))
56{
57 int ret;
58 siginfo_t siginfo;
59 pid_t pid = fork();
60 int expected_signal = SIGABRT;
61 if (pid == 0) {
62 /* child exits with SIGABRT */
63 T_LOG("In child process. Now signalling SIGABRT");
64 (void)signal(SIGABRT, SIG_DFL);
65 raise(SIGABRT);
66 T_LOG("Child should not print");
67 } else {
68 ret = waitid(P_PID, (id_t) pid, &siginfo, WEXITED);
69 T_ASSERT_POSIX_SUCCESS(0, "waitid");
70 if (siginfo.si_signo != SIGCHLD) {
71 T_FAIL("Signal was not SIGCHLD.");
72 }
73 T_LOG("si_status = 0x%x , expected = 0x%x \n", siginfo.si_status, expected_signal);
74 if (siginfo.si_status != expected_signal) {
75 T_FAIL("Unexpected exitcode");
76 }
77 }
78}
79
80T_DECL(sigkill_test, "check that child process' exitcode contains signum = SIGKILL", T_META_CHECK_LEAKS(false))
81{
82 int ret;
83 siginfo_t siginfo;
84 pid_t pid = fork();
85 int expected_signal = SIGKILL;
86 if (pid == 0) {
87 /* child exits with SIGKILL */
88 T_LOG("In child process. Now signalling SIGKILL");
89 raise(SIGKILL);
90 T_LOG("Child should not print");
91 } else {
92 ret = waitid(P_PID, (id_t) pid, &siginfo, WEXITED);
93 T_ASSERT_POSIX_SUCCESS(0, "waitid");
94 if (siginfo.si_signo != SIGCHLD) {
95 T_FAIL("Signal was not SIGCHLD.");
96 }
97 T_LOG("si_status = 0x%x , expected = 0x%x \n", siginfo.si_status, expected_signal);
98 if (siginfo.si_status != expected_signal) {
99 T_FAIL("Unexpected exitcode");
100 }
101 }
102}