1*d8b80295SApple OSS Distributions #include <darwintest.h>
2*d8b80295SApple OSS Distributions
3*d8b80295SApple OSS Distributions #include <stdio.h>
4*d8b80295SApple OSS Distributions #include <unistd.h>
5*d8b80295SApple OSS Distributions #include <stdlib.h>
6*d8b80295SApple OSS Distributions #include <errno.h>
7*d8b80295SApple OSS Distributions #include <string.h>
8*d8b80295SApple OSS Distributions #include <assert.h>
9*d8b80295SApple OSS Distributions #include <signal.h>
10*d8b80295SApple OSS Distributions #include <spawn.h>
11*d8b80295SApple OSS Distributions #include <stdint.h>
12*d8b80295SApple OSS Distributions #include <sys/sysctl.h>
13*d8b80295SApple OSS Distributions #include <stdbool.h>
14*d8b80295SApple OSS Distributions #include <sysexits.h>
15*d8b80295SApple OSS Distributions #include <err.h>
16*d8b80295SApple OSS Distributions
17*d8b80295SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
18*d8b80295SApple OSS Distributions
19*d8b80295SApple OSS Distributions /*
20*d8b80295SApple OSS Distributions * Test to validate that suspended-spawn DTRTs when a SIGKILL is recieved
21*d8b80295SApple OSS Distributions * while the process is waiting for SIGCONT.
22*d8b80295SApple OSS Distributions *
23*d8b80295SApple OSS Distributions * Also test that suspended-spawn correctly looks like a SIGSTOP while it's suspended.
24*d8b80295SApple OSS Distributions *
25*d8b80295SApple OSS Distributions * <rdar://problem/26184412> posix_spawn non-exec with POSIX_SPAWN_START_SUSPENDED, then killing instead of SIGCONT-ing causes unkillable hung processes
26*d8b80295SApple OSS Distributions */
27*d8b80295SApple OSS Distributions
28*d8b80295SApple OSS Distributions static void
spawn_and_signal(int signal)29*d8b80295SApple OSS Distributions spawn_and_signal(int signal)
30*d8b80295SApple OSS Distributions {
31*d8b80295SApple OSS Distributions /* do not buffer output to stdout */
32*d8b80295SApple OSS Distributions setvbuf(stdout, NULL, _IONBF, 0);
33*d8b80295SApple OSS Distributions
34*d8b80295SApple OSS Distributions int ret;
35*d8b80295SApple OSS Distributions posix_spawnattr_t attr;
36*d8b80295SApple OSS Distributions
37*d8b80295SApple OSS Distributions ret = posix_spawnattr_init(&attr);
38*d8b80295SApple OSS Distributions T_QUIET;
39*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init");
40*d8b80295SApple OSS Distributions
41*d8b80295SApple OSS Distributions ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
42*d8b80295SApple OSS Distributions T_QUIET;
43*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setflags");
44*d8b80295SApple OSS Distributions
45*d8b80295SApple OSS Distributions char * const prog = "/usr/bin/true";
46*d8b80295SApple OSS Distributions char * const argv_child[] = { prog, NULL };
47*d8b80295SApple OSS Distributions pid_t child_pid;
48*d8b80295SApple OSS Distributions extern char **environ;
49*d8b80295SApple OSS Distributions
50*d8b80295SApple OSS Distributions ret = posix_spawn(&child_pid, prog, NULL, &attr, argv_child, environ);
51*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn");
52*d8b80295SApple OSS Distributions
53*d8b80295SApple OSS Distributions printf("parent: spawned child with pid %d\n", child_pid);
54*d8b80295SApple OSS Distributions
55*d8b80295SApple OSS Distributions ret = posix_spawnattr_destroy(&attr);
56*d8b80295SApple OSS Distributions T_QUIET;
57*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_destroy");
58*d8b80295SApple OSS Distributions
59*d8b80295SApple OSS Distributions int status = 0;
60*d8b80295SApple OSS Distributions int waitpid_result = waitpid(child_pid, &status, WUNTRACED | WNOHANG);
61*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid");
62*d8b80295SApple OSS Distributions
63*d8b80295SApple OSS Distributions T_ASSERT_EQ(waitpid_result, child_pid, "waitpid should return child we spawned");
64*d8b80295SApple OSS Distributions
65*d8b80295SApple OSS Distributions T_ASSERT_EQ(WIFEXITED(status), 0, "before SIGCONT: must not have exited");
66*d8b80295SApple OSS Distributions T_ASSERT_EQ(WIFSTOPPED(status), 1, "before SIGCONT: must be stopped");
67*d8b80295SApple OSS Distributions
68*d8b80295SApple OSS Distributions printf("parent: continuing child process\n");
69*d8b80295SApple OSS Distributions
70*d8b80295SApple OSS Distributions ret = kill(child_pid, signal);
71*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "kill(signal)");
72*d8b80295SApple OSS Distributions
73*d8b80295SApple OSS Distributions printf("parent: waiting for child process\n");
74*d8b80295SApple OSS Distributions
75*d8b80295SApple OSS Distributions status = 0;
76*d8b80295SApple OSS Distributions waitpid_result = waitpid(child_pid, &status, 0);
77*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid");
78*d8b80295SApple OSS Distributions
79*d8b80295SApple OSS Distributions T_ASSERT_EQ(waitpid_result, child_pid, "waitpid should return child we spawned");
80*d8b80295SApple OSS Distributions
81*d8b80295SApple OSS Distributions if (signal == SIGKILL) {
82*d8b80295SApple OSS Distributions T_ASSERT_EQ(WIFSIGNALED(status), 1, "child should have exited due to signal");
83*d8b80295SApple OSS Distributions T_ASSERT_EQ(WTERMSIG(status), SIGKILL, "child should have exited due to SIGKILL");
84*d8b80295SApple OSS Distributions } else {
85*d8b80295SApple OSS Distributions T_ASSERT_EQ(WIFEXITED(status), 1, "child should have exited normally");
86*d8b80295SApple OSS Distributions T_ASSERT_EQ(WEXITSTATUS(status), EX_OK, "child should have exited with success");
87*d8b80295SApple OSS Distributions }
88*d8b80295SApple OSS Distributions
89*d8b80295SApple OSS Distributions printf("wait returned with pid %d, status %d\n", ret, status);
90*d8b80295SApple OSS Distributions }
91*d8b80295SApple OSS Distributions
92*d8b80295SApple OSS Distributions T_DECL(suspended_spawn_continue, "Tests spawning a suspended process and continuing it", T_META_TIMEOUT(2))
93*d8b80295SApple OSS Distributions {
94*d8b80295SApple OSS Distributions spawn_and_signal(SIGCONT);
95*d8b80295SApple OSS Distributions }
96*d8b80295SApple OSS Distributions
97*d8b80295SApple OSS Distributions T_DECL(suspended_spawn_kill, "Tests spawning a suspended process and killing it", T_META_TIMEOUT(2))
98*d8b80295SApple OSS Distributions {
99*d8b80295SApple OSS Distributions spawn_and_signal(SIGKILL);
100*d8b80295SApple OSS Distributions }
101