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