1 #include <signal.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <sys/wait.h>
7 #include <sys/types.h>
8 #include <spawn.h>
9
10 #include <darwintest.h>
11
12 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true), T_META_IGNORECRASHES("sigchld.*"));
13
14 static int exitcode = 0x6789BEEF;
15
16 static void
handler(int sig,siginfo_t * sip,__unused void * uconp)17 handler(int sig, siginfo_t *sip, __unused void *uconp)
18 {
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
25 T_END;
26 }
27
28
29 T_DECL(sigchldreturn, "checks that a child process exited with an exitcode returns correctly to parent", T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
30 {
31 struct sigaction act;
32 int pid;
33
34 act.sa_sigaction = handler;
35 act.sa_flags = SA_SIGINFO;
36
37 /* Set action for signal */
38 T_QUIET; T_ASSERT_POSIX_SUCCESS(sigaction(SIGCHLD, &act, NULL), "Calling sigaction() failed for SIGCHLD");
39
40 /* Now fork a child that just exits */
41 pid = fork();
42 T_QUIET; T_ASSERT_NE_INT(pid, -1, "fork() failed!");
43
44 if (pid == 0) {
45 /* Child process! */
46 exit(exitcode);
47 }
48
49 /* Main program that did the fork */
50 /* We should process the signal, then exit */
51 while (1) {
52 sleep(1);
53 }
54 }
55
56 T_DECL(sigabrt_test, "check that child process' exitcode contains signum = SIGABRT", T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
57 {
58 int ret;
59 siginfo_t siginfo;
60 pid_t pid = fork();
61 int expected_signal = SIGABRT;
62 if (pid == 0) {
63 /* child exits with SIGABRT */
64 T_LOG("In child process. Now signalling SIGABRT");
65 (void)signal(SIGABRT, SIG_DFL);
66 raise(SIGABRT);
67 T_LOG("Child should not print");
68 } else {
69 ret = waitid(P_PID, (id_t) pid, &siginfo, WEXITED);
70 T_ASSERT_POSIX_SUCCESS(0, "waitid");
71 if (siginfo.si_signo != SIGCHLD) {
72 T_FAIL("Signal was not SIGCHLD.");
73 }
74 T_LOG("si_status = 0x%x , expected = 0x%x \n", siginfo.si_status, expected_signal);
75 if (siginfo.si_status != expected_signal) {
76 T_FAIL("Unexpected exitcode");
77 }
78 }
79 }
80
81 T_DECL(sigkill_test, "check that child process' exitcode contains signum = SIGKILL", T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
82 {
83 int ret;
84 siginfo_t siginfo;
85 pid_t pid = fork();
86 int expected_signal = SIGKILL;
87 if (pid == 0) {
88 /* child exits with SIGKILL */
89 T_LOG("In child process. Now signalling SIGKILL");
90 raise(SIGKILL);
91 T_LOG("Child should not print");
92 } else {
93 ret = waitid(P_PID, (id_t) pid, &siginfo, WEXITED);
94 T_ASSERT_POSIX_SUCCESS(0, "waitid");
95 if (siginfo.si_signo != SIGCHLD) {
96 T_FAIL("Signal was not SIGCHLD.");
97 }
98 T_LOG("si_status = 0x%x , expected = 0x%x \n", siginfo.si_status, expected_signal);
99 if (siginfo.si_status != expected_signal) {
100 T_FAIL("Unexpected exitcode");
101 }
102 }
103 }
104
105 T_DECL(sigchild_posix_spawn_fail, "check SIGCHLD is correctly delivered when posix_spawn fails", T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
106 {
107 struct sigaction act;
108 int pid;
109 char *args[4];
110
111 act.sa_sigaction = handler;
112 act.sa_flags = SA_SIGINFO;
113
114 exitcode = 0;
115
116 /* Set action for signal */
117 T_QUIET; T_ASSERT_POSIX_SUCCESS(sigaction(SIGCHLD, &act, NULL), "Calling sigaction() failed for SIGCHLD");
118
119 args[0] = "sh";
120 args[1] = "-c";
121 args[2] = "exit 0";
122 args[3] = NULL;
123
124 T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_spawn(&pid, "/bin/sh", NULL, NULL, args, NULL), "posix_spawn failed");
125
126 for (int i = 0; i < 500; i++) {
127 int ret = posix_spawn(&pid, "does not exist", NULL, NULL, args, NULL);
128 T_QUIET; T_ASSERT_EQ(ret, ENOENT, "posix_spawn should fail with ENOENT");
129 }
130
131 /* Exit successfully in signal handler when SIGCHLD is delivered */
132 sleep(10);
133
134 T_FAIL("SIGCHLD is not delivered");
135 }
136