1*27b03b36SApple OSS Distributions #include <assert.h>
2*27b03b36SApple OSS Distributions #include <stdio.h>
3*27b03b36SApple OSS Distributions #include <pthread.h>
4*27b03b36SApple OSS Distributions #include <signal.h>
5*27b03b36SApple OSS Distributions #include <unistd.h>
6*27b03b36SApple OSS Distributions #include <errno.h>
7*27b03b36SApple OSS Distributions #include <string.h>
8*27b03b36SApple OSS Distributions #include <sys/wait.h>
9*27b03b36SApple OSS Distributions
10*27b03b36SApple OSS Distributions #include <darwintest.h>
11*27b03b36SApple OSS Distributions
12*27b03b36SApple OSS Distributions // rdar://58566604
13*27b03b36SApple OSS Distributions // Exercise races of signal delivery vs exec in multi-threaded processes
14*27b03b36SApple OSS Distributions
15*27b03b36SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.exec"),
16*27b03b36SApple OSS Distributions T_META_CHECK_LEAKS(false),
17*27b03b36SApple OSS Distributions T_META_ALL_VALID_ARCHS(true));
18*27b03b36SApple OSS Distributions
19*27b03b36SApple OSS Distributions enum { KILL_ONCE, KILL_MANY, KILL_LAST } kill_mode;
20*27b03b36SApple OSS Distributions enum { EXEC_FIRST, EXEC_SECOND, EXEC_LAST } exec_mode;
21*27b03b36SApple OSS Distributions
22*27b03b36SApple OSS Distributions static int fd[2];
23*27b03b36SApple OSS Distributions
24*27b03b36SApple OSS Distributions static void
do_exec(void)25*27b03b36SApple OSS Distributions do_exec(void)
26*27b03b36SApple OSS Distributions {
27*27b03b36SApple OSS Distributions char echo_arg[50] = "";
28*27b03b36SApple OSS Distributions
29*27b03b36SApple OSS Distributions snprintf(echo_arg, sizeof(echo_arg), " Child[%d] says hello after exec", getpid());
30*27b03b36SApple OSS Distributions
31*27b03b36SApple OSS Distributions char * new_argv[] = {
32*27b03b36SApple OSS Distributions "/bin/echo",
33*27b03b36SApple OSS Distributions echo_arg,
34*27b03b36SApple OSS Distributions NULL
35*27b03b36SApple OSS Distributions };
36*27b03b36SApple OSS Distributions
37*27b03b36SApple OSS Distributions int ret = execv(new_argv[0], new_argv);
38*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "execv()");
39*27b03b36SApple OSS Distributions }
40*27b03b36SApple OSS Distributions
41*27b03b36SApple OSS Distributions static void*
thread_main(void * arg)42*27b03b36SApple OSS Distributions thread_main(void* arg)
43*27b03b36SApple OSS Distributions {
44*27b03b36SApple OSS Distributions T_LOG("mode: %d, %d: Child[%d] created second thread\n",
45*27b03b36SApple OSS Distributions kill_mode, exec_mode, getpid());
46*27b03b36SApple OSS Distributions
47*27b03b36SApple OSS Distributions if (exec_mode == EXEC_SECOND) {
48*27b03b36SApple OSS Distributions int ret = dprintf(fd[1], "Hi!");
49*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "dprintf()");
50*27b03b36SApple OSS Distributions do_exec();
51*27b03b36SApple OSS Distributions }
52*27b03b36SApple OSS Distributions
53*27b03b36SApple OSS Distributions while (1) {
54*27b03b36SApple OSS Distributions }
55*27b03b36SApple OSS Distributions return NULL;
56*27b03b36SApple OSS Distributions }
57*27b03b36SApple OSS Distributions
58*27b03b36SApple OSS Distributions void
run_test(void)59*27b03b36SApple OSS Distributions run_test(void)
60*27b03b36SApple OSS Distributions {
61*27b03b36SApple OSS Distributions T_LOG("mode: %d, %d: Parent[%d]: forking\n",
62*27b03b36SApple OSS Distributions kill_mode, exec_mode, getpid());
63*27b03b36SApple OSS Distributions
64*27b03b36SApple OSS Distributions pid_t child_pid = fork();
65*27b03b36SApple OSS Distributions
66*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(child_pid, "fork()");
67*27b03b36SApple OSS Distributions
68*27b03b36SApple OSS Distributions int ret = 0;
69*27b03b36SApple OSS Distributions
70*27b03b36SApple OSS Distributions if (child_pid == 0) {
71*27b03b36SApple OSS Distributions pthread_t thread;
72*27b03b36SApple OSS Distributions ret = pthread_create(&thread, NULL, thread_main, NULL);
73*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_create()");
74*27b03b36SApple OSS Distributions
75*27b03b36SApple OSS Distributions if (exec_mode == EXEC_FIRST) {
76*27b03b36SApple OSS Distributions ret = dprintf(fd[1], "Hi!");
77*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "dprintf()");
78*27b03b36SApple OSS Distributions
79*27b03b36SApple OSS Distributions do_exec();
80*27b03b36SApple OSS Distributions }
81*27b03b36SApple OSS Distributions
82*27b03b36SApple OSS Distributions while (1) {
83*27b03b36SApple OSS Distributions }
84*27b03b36SApple OSS Distributions } else {
85*27b03b36SApple OSS Distributions char buffer[4] = "";
86*27b03b36SApple OSS Distributions ret = read(fd[0], buffer, sizeof(buffer));
87*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "read()");
88*27b03b36SApple OSS Distributions
89*27b03b36SApple OSS Distributions T_LOG("mode: %d, %d: Parent[%d]: got: '%s' from execing child, trying to kill and wait\n",
90*27b03b36SApple OSS Distributions kill_mode, exec_mode, getpid(), buffer);
91*27b03b36SApple OSS Distributions
92*27b03b36SApple OSS Distributions int killcount = 0, status = 0, waitedpid = 0;
93*27b03b36SApple OSS Distributions
94*27b03b36SApple OSS Distributions switch (kill_mode) {
95*27b03b36SApple OSS Distributions case KILL_ONCE:
96*27b03b36SApple OSS Distributions ret = kill(child_pid, SIGKILL);
97*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kill()");
98*27b03b36SApple OSS Distributions
99*27b03b36SApple OSS Distributions waitedpid = waitpid(child_pid, &status, 0);
100*27b03b36SApple OSS Distributions
101*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitedpid, "waitpid()");
102*27b03b36SApple OSS Distributions
103*27b03b36SApple OSS Distributions killcount++;
104*27b03b36SApple OSS Distributions break;
105*27b03b36SApple OSS Distributions case KILL_MANY:
106*27b03b36SApple OSS Distributions while (waitedpid == 0) {
107*27b03b36SApple OSS Distributions ret = kill(child_pid, SIGKILL);
108*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kill()");
109*27b03b36SApple OSS Distributions
110*27b03b36SApple OSS Distributions waitedpid = waitpid(child_pid, &status, WNOHANG);
111*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitedpid, "waitpid()");
112*27b03b36SApple OSS Distributions
113*27b03b36SApple OSS Distributions killcount++;
114*27b03b36SApple OSS Distributions }
115*27b03b36SApple OSS Distributions break;
116*27b03b36SApple OSS Distributions default:
117*27b03b36SApple OSS Distributions break;
118*27b03b36SApple OSS Distributions }
119*27b03b36SApple OSS Distributions
120*27b03b36SApple OSS Distributions T_LOG("mode: %d, %d: Parent[%d]: waitpid returned: %d, errno %d (%s), exit signal %d, after %d loops\n",
121*27b03b36SApple OSS Distributions kill_mode, exec_mode, getpid(), waitedpid, errno, strerror(errno), WTERMSIG(status), killcount);
122*27b03b36SApple OSS Distributions }
123*27b03b36SApple OSS Distributions }
124*27b03b36SApple OSS Distributions
125*27b03b36SApple OSS Distributions T_DECL(exec_exit_race_once_first, "Exec-exit race, one kill, exec on first thread") {
126*27b03b36SApple OSS Distributions int rv = pipe(fd);
127*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pipe()");
128*27b03b36SApple OSS Distributions
129*27b03b36SApple OSS Distributions kill_mode = KILL_ONCE;
130*27b03b36SApple OSS Distributions exec_mode = EXEC_FIRST;
131*27b03b36SApple OSS Distributions
132*27b03b36SApple OSS Distributions for (int i = 0; i < 1000; i++) {
133*27b03b36SApple OSS Distributions run_test();
134*27b03b36SApple OSS Distributions }
135*27b03b36SApple OSS Distributions }
136*27b03b36SApple OSS Distributions
137*27b03b36SApple OSS Distributions T_DECL(exec_exit_race_many_first, "Exec-exit race, many kill, exec on first thread") {
138*27b03b36SApple OSS Distributions int rv = pipe(fd);
139*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pipe()");
140*27b03b36SApple OSS Distributions
141*27b03b36SApple OSS Distributions kill_mode = KILL_MANY;
142*27b03b36SApple OSS Distributions exec_mode = EXEC_FIRST;
143*27b03b36SApple OSS Distributions
144*27b03b36SApple OSS Distributions for (int i = 0; i < 1000; i++) {
145*27b03b36SApple OSS Distributions run_test();
146*27b03b36SApple OSS Distributions }
147*27b03b36SApple OSS Distributions }
148*27b03b36SApple OSS Distributions
149*27b03b36SApple OSS Distributions T_DECL(exec_exit_race_once_second, "Exec-exit race, one kill, exec on second thread") {
150*27b03b36SApple OSS Distributions int rv = pipe(fd);
151*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pipe()");
152*27b03b36SApple OSS Distributions
153*27b03b36SApple OSS Distributions kill_mode = KILL_ONCE;
154*27b03b36SApple OSS Distributions exec_mode = EXEC_SECOND;
155*27b03b36SApple OSS Distributions
156*27b03b36SApple OSS Distributions for (int i = 0; i < 1000; i++) {
157*27b03b36SApple OSS Distributions run_test();
158*27b03b36SApple OSS Distributions }
159*27b03b36SApple OSS Distributions }
160*27b03b36SApple OSS Distributions
161*27b03b36SApple OSS Distributions T_DECL(exec_exit_race_many_second, "Exec-exit race, many kill, exec on second thread") {
162*27b03b36SApple OSS Distributions int rv = pipe(fd);
163*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pipe()");
164*27b03b36SApple OSS Distributions
165*27b03b36SApple OSS Distributions kill_mode = KILL_MANY;
166*27b03b36SApple OSS Distributions exec_mode = EXEC_SECOND;
167*27b03b36SApple OSS Distributions
168*27b03b36SApple OSS Distributions for (int i = 0; i < 1000; i++) {
169*27b03b36SApple OSS Distributions run_test();
170*27b03b36SApple OSS Distributions }
171*27b03b36SApple OSS Distributions }
172