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