]>
Commit | Line | Data |
---|---|---|
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 | ||
9 | #include <darwintest.h> | |
10 | ||
11 | T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true)); | |
12 | ||
13 | static int exitcode = 0x6789BEEF; | |
14 | static int should_exit = 0; | |
15 | ||
16 | static void | |
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 | should_exit = 1; | |
25 | } | |
26 | ||
27 | ||
28 | T_DECL(sigchldreturn, "checks that a child process exited with an exitcode returns correctly to parent", T_META_CHECK_LEAKS(false)) | |
29 | { | |
30 | struct sigaction act; | |
31 | int pid; | |
32 | ||
33 | act.sa_sigaction = handler; | |
34 | act.sa_flags = SA_SIGINFO; | |
35 | ||
36 | /* Set action for signal */ | |
37 | T_QUIET; T_ASSERT_POSIX_SUCCESS(sigaction(SIGCHLD, &act, NULL), "Calling sigaction() failed for SIGCHLD"); | |
38 | ||
39 | /* Now fork a child that just exits */ | |
40 | pid = fork(); | |
41 | T_QUIET; T_ASSERT_NE_INT(pid, -1, "fork() failed!"); | |
42 | ||
43 | if (pid == 0) { | |
44 | /* Child process! */ | |
45 | exit(exitcode); | |
46 | } | |
47 | ||
48 | /* Main program that did the fork */ | |
49 | /* We should process the signal, then exit */ | |
50 | while (!should_exit) { | |
51 | sleep(1); | |
52 | } | |
53 | } | |
54 | ||
55 | T_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 | ||
80 | T_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 | } |