blob: f3c5b9aff060e05a6c744de50fd3eb85101d77df [file] [log] [blame]
Elliott Hughes14e3ff92017-10-06 16:58:36 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <spawn.h>
18
19#include <errno.h>
20#include <fcntl.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070021#include <sys/cdefs.h>
22
Elliott Hughes14e3ff92017-10-06 16:58:36 -070023#include <gtest/gtest.h>
24
Elliott Hughes71ba5892018-02-07 12:44:45 -080025#include "SignalUtils.h"
Elliott Hughes14e3ff92017-10-06 16:58:36 -070026#include "utils.h"
27
28#include <android-base/file.h>
29#include <android-base/strings.h>
30
31// Old versions of glibc didn't have POSIX_SPAWN_SETSID.
32#if __GLIBC__
33# if !defined(POSIX_SPAWN_SETSID)
34# define POSIX_SPAWN_SETSID 0
35# endif
Colin Cross7da20342021-07-28 11:18:11 -070036#elif defined(__BIONIC__)
Florian Mayer96272df2020-03-24 15:59:27 +010037#include <platform/bionic/reserved_signals.h>
Elliott Hughes14e3ff92017-10-06 16:58:36 -070038#endif
39
40TEST(spawn, posix_spawnattr_init_posix_spawnattr_destroy) {
41 posix_spawnattr_t sa;
42 ASSERT_EQ(0, posix_spawnattr_init(&sa));
43 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
44}
45
46TEST(spawn, posix_spawnattr_setflags_EINVAL) {
47 posix_spawnattr_t sa;
48 ASSERT_EQ(0, posix_spawnattr_init(&sa));
49 ASSERT_EQ(EINVAL, posix_spawnattr_setflags(&sa, ~0));
50 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
51}
52
53TEST(spawn, posix_spawnattr_setflags_posix_spawnattr_getflags) {
54 posix_spawnattr_t sa;
55 ASSERT_EQ(0, posix_spawnattr_init(&sa));
56
57 ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_RESETIDS));
58 short flags;
59 ASSERT_EQ(0, posix_spawnattr_getflags(&sa, &flags));
60 ASSERT_EQ(POSIX_SPAWN_RESETIDS, flags);
61
62 constexpr short all_flags = POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
63 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM |
64 POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID;
65 ASSERT_EQ(0, posix_spawnattr_setflags(&sa, all_flags));
66 ASSERT_EQ(0, posix_spawnattr_getflags(&sa, &flags));
67 ASSERT_EQ(all_flags, flags);
68
69 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
70}
71
72TEST(spawn, posix_spawnattr_setpgroup_posix_spawnattr_getpgroup) {
73 posix_spawnattr_t sa;
74 ASSERT_EQ(0, posix_spawnattr_init(&sa));
75
76 ASSERT_EQ(0, posix_spawnattr_setpgroup(&sa, 123));
77 pid_t g;
78 ASSERT_EQ(0, posix_spawnattr_getpgroup(&sa, &g));
79 ASSERT_EQ(123, g);
80
81 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
82}
83
84TEST(spawn, posix_spawnattr_setsigmask_posix_spawnattr_getsigmask) {
85 posix_spawnattr_t sa;
86 ASSERT_EQ(0, posix_spawnattr_init(&sa));
87
88 sigset_t sigs;
89 ASSERT_EQ(0, posix_spawnattr_getsigmask(&sa, &sigs));
90 ASSERT_FALSE(sigismember(&sigs, SIGALRM));
91
92 sigset_t just_SIGALRM;
93 sigemptyset(&just_SIGALRM);
94 sigaddset(&just_SIGALRM, SIGALRM);
95 ASSERT_EQ(0, posix_spawnattr_setsigmask(&sa, &just_SIGALRM));
96
97 ASSERT_EQ(0, posix_spawnattr_getsigmask(&sa, &sigs));
98 ASSERT_TRUE(sigismember(&sigs, SIGALRM));
99
100 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
101}
102
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800103TEST(spawn, posix_spawnattr_setsigmask64_posix_spawnattr_getsigmask64) {
104 posix_spawnattr_t sa;
105 ASSERT_EQ(0, posix_spawnattr_init(&sa));
106
107 sigset64_t sigs;
108 ASSERT_EQ(0, posix_spawnattr_getsigmask64(&sa, &sigs));
109 ASSERT_FALSE(sigismember64(&sigs, SIGRTMIN));
110
111 sigset64_t just_SIGRTMIN;
112 sigemptyset64(&just_SIGRTMIN);
113 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
114 ASSERT_EQ(0, posix_spawnattr_setsigmask64(&sa, &just_SIGRTMIN));
115
116 ASSERT_EQ(0, posix_spawnattr_getsigmask64(&sa, &sigs));
117 ASSERT_TRUE(sigismember64(&sigs, SIGRTMIN));
118
119 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
120}
121
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700122TEST(spawn, posix_spawnattr_setsigdefault_posix_spawnattr_getsigdefault) {
123 posix_spawnattr_t sa;
124 ASSERT_EQ(0, posix_spawnattr_init(&sa));
125
126 sigset_t sigs;
127 ASSERT_EQ(0, posix_spawnattr_getsigdefault(&sa, &sigs));
128 ASSERT_FALSE(sigismember(&sigs, SIGALRM));
129
130 sigset_t just_SIGALRM;
131 sigemptyset(&just_SIGALRM);
132 sigaddset(&just_SIGALRM, SIGALRM);
133 ASSERT_EQ(0, posix_spawnattr_setsigdefault(&sa, &just_SIGALRM));
134
135 ASSERT_EQ(0, posix_spawnattr_getsigdefault(&sa, &sigs));
136 ASSERT_TRUE(sigismember(&sigs, SIGALRM));
137
138 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
139}
140
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800141TEST(spawn, posix_spawnattr_setsigdefault64_posix_spawnattr_getsigdefault64) {
142 posix_spawnattr_t sa;
143 ASSERT_EQ(0, posix_spawnattr_init(&sa));
144
145 sigset64_t sigs;
146 ASSERT_EQ(0, posix_spawnattr_getsigdefault64(&sa, &sigs));
147 ASSERT_FALSE(sigismember64(&sigs, SIGRTMIN));
148
149 sigset64_t just_SIGRTMIN;
150 sigemptyset64(&just_SIGRTMIN);
151 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
152 ASSERT_EQ(0, posix_spawnattr_setsigdefault64(&sa, &just_SIGRTMIN));
153
154 ASSERT_EQ(0, posix_spawnattr_getsigdefault64(&sa, &sigs));
155 ASSERT_TRUE(sigismember64(&sigs, SIGRTMIN));
156
157 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
158}
159
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700160TEST(spawn, posix_spawnattr_setsschedparam_posix_spawnattr_getsschedparam) {
161 posix_spawnattr_t sa;
162 ASSERT_EQ(0, posix_spawnattr_init(&sa));
163
164 sched_param sp;
165 ASSERT_EQ(0, posix_spawnattr_getschedparam(&sa, &sp));
166 ASSERT_EQ(0, sp.sched_priority);
167
168 sched_param sp123 = { .sched_priority = 123 };
169 ASSERT_EQ(0, posix_spawnattr_setschedparam(&sa, &sp123));
170
171 ASSERT_EQ(0, posix_spawnattr_getschedparam(&sa, &sp));
172 ASSERT_EQ(123, sp.sched_priority);
173
174 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
175}
176
177TEST(spawn, posix_spawnattr_setschedpolicy_posix_spawnattr_getschedpolicy) {
178 posix_spawnattr_t sa;
179 ASSERT_EQ(0, posix_spawnattr_init(&sa));
180
181 int p;
182 ASSERT_EQ(0, posix_spawnattr_getschedpolicy(&sa, &p));
183 ASSERT_EQ(0, p);
184
185 ASSERT_EQ(0, posix_spawnattr_setschedpolicy(&sa, SCHED_FIFO));
186
187 ASSERT_EQ(0, posix_spawnattr_getschedpolicy(&sa, &p));
188 ASSERT_EQ(SCHED_FIFO, p);
189
190 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
191}
192
193TEST(spawn, posix_spawn) {
194 ExecTestHelper eth;
195 eth.SetArgs({BIN_DIR "true", nullptr});
196 pid_t pid;
197 ASSERT_EQ(0, posix_spawn(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr));
198 AssertChildExited(pid, 0);
199}
200
201TEST(spawn, posix_spawn_not_found) {
202 ExecTestHelper eth;
203 eth.SetArgs({"true", nullptr});
204 pid_t pid;
205 ASSERT_EQ(0, posix_spawn(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr));
206 AssertChildExited(pid, 127);
207}
208
209TEST(spawn, posix_spawnp) {
210 ExecTestHelper eth;
211 eth.SetArgs({"true", nullptr});
212 pid_t pid;
213 ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr));
214 AssertChildExited(pid, 0);
215}
216
217TEST(spawn, posix_spawnp_not_found) {
218 ExecTestHelper eth;
219 eth.SetArgs({"does-not-exist", nullptr});
220 pid_t pid;
221 ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr));
222 AssertChildExited(pid, 127);
223}
224
225TEST(spawn, posix_spawn_environment) {
226 ExecTestHelper eth;
227 eth.SetArgs({"sh", "-c", "exit $posix_spawn_environment_test", nullptr});
228 eth.SetEnv({"posix_spawn_environment_test=66", nullptr});
229 pid_t pid;
230 ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), eth.GetEnv()));
231 AssertChildExited(pid, 66);
232}
233
234TEST(spawn, posix_spawn_file_actions) {
235 int fds[2];
236 ASSERT_NE(-1, pipe(fds));
237
238 posix_spawn_file_actions_t fa;
239 ASSERT_EQ(0, posix_spawn_file_actions_init(&fa));
240
241 ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[0]));
242 ASSERT_EQ(0, posix_spawn_file_actions_adddup2(&fa, fds[1], 1));
243 ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[1]));
244 // Check that close(2) failures are ignored by closing the same fd again.
245 ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[1]));
246 ASSERT_EQ(0, posix_spawn_file_actions_addopen(&fa, 56, "/proc/version", O_RDONLY, 0));
247
248 ExecTestHelper eth;
249 eth.SetArgs({"ls", "-l", "/proc/self/fd", nullptr});
250 pid_t pid;
251 ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), &fa, nullptr, eth.GetArgs(), eth.GetEnv()));
252 ASSERT_EQ(0, posix_spawn_file_actions_destroy(&fa));
253
254 ASSERT_EQ(0, close(fds[1]));
255 std::string content;
256 ASSERT_TRUE(android::base::ReadFdToString(fds[0], &content));
257 ASSERT_EQ(0, close(fds[0]));
258
259 AssertChildExited(pid, 0);
260
261 // We'll know the dup2 worked if we see any ls(1) output in our pipe.
262 // The open we can check manually...
263 bool open_to_fd_56_worked = false;
264 for (const auto& line : android::base::Split(content, "\n")) {
265 if (line.find(" 56 -> /proc/version") != std::string::npos) open_to_fd_56_worked = true;
266 }
267 ASSERT_TRUE(open_to_fd_56_worked);
268}
269
270static void CatFileToString(posix_spawnattr_t* sa, const char* path, std::string* content) {
271 int fds[2];
272 ASSERT_NE(-1, pipe(fds));
273
274 posix_spawn_file_actions_t fa;
275 ASSERT_EQ(0, posix_spawn_file_actions_init(&fa));
276 ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[0]));
277 ASSERT_EQ(0, posix_spawn_file_actions_adddup2(&fa, fds[1], 1));
278 ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[1]));
279
280 ExecTestHelper eth;
281 eth.SetArgs({"cat", path, nullptr});
282 pid_t pid;
283 ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), &fa, sa, eth.GetArgs(), nullptr));
284 ASSERT_EQ(0, posix_spawn_file_actions_destroy(&fa));
285
286 ASSERT_EQ(0, close(fds[1]));
287 ASSERT_TRUE(android::base::ReadFdToString(fds[0], content));
288 ASSERT_EQ(0, close(fds[0]));
289 AssertChildExited(pid, 0);
290}
291
292struct ProcStat {
293 pid_t pid;
294 pid_t ppid;
295 pid_t pgrp;
296 pid_t sid;
297};
298
Florian Mayer96272df2020-03-24 15:59:27 +0100299static __attribute__((unused)) void GetChildStat(posix_spawnattr_t* sa, ProcStat* ps) {
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700300 std::string content;
301 CatFileToString(sa, "/proc/self/stat", &content);
302
303 ASSERT_EQ(4, sscanf(content.c_str(), "%d (cat) %*c %d %d %d", &ps->pid, &ps->ppid, &ps->pgrp,
304 &ps->sid));
305
306 ASSERT_EQ(getpid(), ps->ppid);
307}
308
309struct ProcStatus {
310 uint64_t sigblk;
311 uint64_t sigign;
312};
313
Florian Mayer96272df2020-03-24 15:59:27 +0100314static void __attribute__((unused)) GetChildStatus(posix_spawnattr_t* sa, ProcStatus* ps) {
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700315 std::string content;
316 CatFileToString(sa, "/proc/self/status", &content);
317
318 bool saw_blk = false;
319 bool saw_ign = false;
320 for (const auto& line : android::base::Split(content, "\n")) {
321 if (sscanf(line.c_str(), "SigBlk: %" SCNx64, &ps->sigblk) == 1) saw_blk = true;
322 if (sscanf(line.c_str(), "SigIgn: %" SCNx64, &ps->sigign) == 1) saw_ign = true;
323 }
324 ASSERT_TRUE(saw_blk);
325 ASSERT_TRUE(saw_ign);
326}
327
328TEST(spawn, posix_spawn_POSIX_SPAWN_SETSID_clear) {
329 pid_t parent_sid = getsid(0);
330
331 posix_spawnattr_t sa;
332 ASSERT_EQ(0, posix_spawnattr_init(&sa));
333 ASSERT_EQ(0, posix_spawnattr_setflags(&sa, 0));
334
335 ProcStat ps = {};
336 GetChildStat(&sa, &ps);
337 ASSERT_EQ(parent_sid, ps.sid);
338 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
339}
340
341TEST(spawn, posix_spawn_POSIX_SPAWN_SETSID_set) {
342 pid_t parent_sid = getsid(0);
343
344 posix_spawnattr_t sa;
345 ASSERT_EQ(0, posix_spawnattr_init(&sa));
346 ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSID));
347
348 ProcStat ps = {};
349 GetChildStat(&sa, &ps);
350 ASSERT_NE(parent_sid, ps.sid);
351 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
352}
353
354TEST(spawn, posix_spawn_POSIX_SPAWN_SETPGROUP_clear) {
355 pid_t parent_pgrp = getpgrp();
356
357 posix_spawnattr_t sa;
358 ASSERT_EQ(0, posix_spawnattr_init(&sa));
359 ASSERT_EQ(0, posix_spawnattr_setflags(&sa, 0));
360
361 ProcStat ps = {};
362 GetChildStat(&sa, &ps);
363 ASSERT_EQ(parent_pgrp, ps.pgrp);
364 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
365}
366
367TEST(spawn, posix_spawn_POSIX_SPAWN_SETPGROUP_set) {
368 pid_t parent_pgrp = getpgrp();
369
370 posix_spawnattr_t sa;
371 ASSERT_EQ(0, posix_spawnattr_init(&sa));
372 ASSERT_EQ(0, posix_spawnattr_setpgroup(&sa, 0));
373 ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETPGROUP));
374
375 ProcStat ps = {};
376 GetChildStat(&sa, &ps);
377 ASSERT_NE(parent_pgrp, ps.pgrp);
378 // Setting pgid 0 means "the same as the caller's pid".
379 ASSERT_EQ(ps.pid, ps.pgrp);
380 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
381}
382
383TEST(spawn, posix_spawn_POSIX_SPAWN_SETSIGMASK) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700384#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
Florian Mayer96272df2020-03-24 15:59:27 +0100385 GTEST_SKIP() << "glibc doesn't ignore the same signals.";
386#else
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700387 // Block SIGBUS in the parent...
388 sigset_t just_SIGBUS;
389 sigemptyset(&just_SIGBUS);
390 sigaddset(&just_SIGBUS, SIGBUS);
391 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGBUS, nullptr));
392
393 posix_spawnattr_t sa;
394 ASSERT_EQ(0, posix_spawnattr_init(&sa));
395
396 // Ask for only SIGALRM to be blocked in the child...
397 sigset_t just_SIGALRM;
398 sigemptyset(&just_SIGALRM);
399 sigaddset(&just_SIGALRM, SIGALRM);
400 ASSERT_EQ(0, posix_spawnattr_setsigmask(&sa, &just_SIGALRM));
401 ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSIGMASK));
402
403 // Check that's what happens...
404 ProcStatus ps = {};
405 GetChildStatus(&sa, &ps);
Josh Gaobaf20fc2018-10-08 17:28:07 -0700406
407 // TIMER_SIGNAL should also be blocked.
408 uint64_t expected_blocked = 0;
409 SignalSetAdd(&expected_blocked, SIGALRM);
Florian Mayer96272df2020-03-24 15:59:27 +0100410 SignalSetAdd(&expected_blocked, BIONIC_SIGNAL_POSIX_TIMERS);
Josh Gaobaf20fc2018-10-08 17:28:07 -0700411 EXPECT_EQ(expected_blocked, ps.sigblk);
412
Florian Mayer96272df2020-03-24 15:59:27 +0100413 uint64_t expected_ignored = 0;
414 SignalSetAdd(&expected_ignored, BIONIC_SIGNAL_ART_PROFILER);
415 EXPECT_EQ(expected_ignored, ps.sigign);
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700416
417 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
Florian Mayer96272df2020-03-24 15:59:27 +0100418#endif
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700419}
420
421TEST(spawn, posix_spawn_POSIX_SPAWN_SETSIGDEF) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700422#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
Florian Mayer96272df2020-03-24 15:59:27 +0100423 GTEST_SKIP() << "glibc doesn't ignore the same signals.";
424#else
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700425 // Ignore SIGALRM and SIGCONT in the parent...
426 ASSERT_NE(SIG_ERR, signal(SIGALRM, SIG_IGN));
427 ASSERT_NE(SIG_ERR, signal(SIGCONT, SIG_IGN));
428
429 posix_spawnattr_t sa;
430 ASSERT_EQ(0, posix_spawnattr_init(&sa));
431
432 // Ask for SIGALRM to be defaulted in the child...
433 sigset_t just_SIGALRM;
434 sigemptyset(&just_SIGALRM);
435 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800436
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700437 ASSERT_EQ(0, posix_spawnattr_setsigdefault(&sa, &just_SIGALRM));
438 ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSIGDEF));
439
440 // Check that's what happens...
441 ProcStatus ps = {};
442 GetChildStatus(&sa, &ps);
Josh Gaobaf20fc2018-10-08 17:28:07 -0700443
444 // TIMER_SIGNAL should be blocked.
445 uint64_t expected_blocked = 0;
Florian Mayer96272df2020-03-24 15:59:27 +0100446 SignalSetAdd(&expected_blocked, BIONIC_SIGNAL_POSIX_TIMERS);
Josh Gaobaf20fc2018-10-08 17:28:07 -0700447 EXPECT_EQ(expected_blocked, ps.sigblk);
448
449 uint64_t expected_ignored = 0;
450 SignalSetAdd(&expected_ignored, SIGCONT);
Florian Mayer96272df2020-03-24 15:59:27 +0100451 SignalSetAdd(&expected_ignored, BIONIC_SIGNAL_ART_PROFILER);
Josh Gaobaf20fc2018-10-08 17:28:07 -0700452 EXPECT_EQ(expected_ignored, ps.sigign);
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700453
454 ASSERT_EQ(0, posix_spawnattr_destroy(&sa));
Florian Mayer96272df2020-03-24 15:59:27 +0100455#endif
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700456}
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800457
458TEST(spawn, signal_stress) {
459 // Ensure that posix_spawn doesn't restore the caller's signal mask in the
460 // child without first defaulting any caught signals (http://b/68707996).
461 static pid_t parent = getpid();
462
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800463 setpgid(0, 0);
Ryan Pricharda1bc8262018-04-03 20:46:11 -0700464 signal(SIGRTMIN, SIG_IGN);
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800465
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800466 pid_t pid = fork();
467 ASSERT_NE(-1, pid);
468
469 if (pid == 0) {
470 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800471 kill(0, SIGRTMIN);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800472 usleep(10);
473 }
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800474 _exit(99);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800475 }
476
477 // We test both with and without attributes, because they used to be
478 // different codepaths. We also test with an empty `sigdefault` set.
479 posix_spawnattr_t attr1;
480 posix_spawnattr_init(&attr1);
481
482 sigset_t empty_mask = {};
483 posix_spawnattr_t attr2;
484 posix_spawnattr_init(&attr2);
485 posix_spawnattr_setflags(&attr2, POSIX_SPAWN_SETSIGDEF);
486 posix_spawnattr_setsigdefault(&attr2, &empty_mask);
487
488 posix_spawnattr_t* attrs[] = { nullptr, &attr1, &attr2 };
489
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800490 // We use a real-time signal because that's a tricky case for LP32
491 // because our sigset_t was too small.
492 ScopedSignalHandler ssh(SIGRTMIN, [](int) { ASSERT_EQ(getpid(), parent); });
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800493
Ryan Prichard344969c2018-04-09 16:26:20 -0700494 const size_t pid_count = 128;
495 pid_t spawned_pids[pid_count];
496
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800497 ExecTestHelper eth;
498 eth.SetArgs({"true", nullptr});
Ryan Prichard344969c2018-04-09 16:26:20 -0700499 for (size_t i = 0; i < pid_count; ++i) {
500 pid_t spawned_pid;
501 ASSERT_EQ(0, posix_spawn(&spawned_pid, "true", nullptr, attrs[i % 3], eth.GetArgs(), nullptr));
502 spawned_pids[i] = spawned_pid;
503 }
504
505 for (pid_t spawned_pid : spawned_pids) {
506 ASSERT_EQ(spawned_pid, TEMP_FAILURE_RETRY(waitpid(spawned_pid, nullptr, 0)));
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800507 }
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800508
509 AssertChildExited(pid, 99);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800510}