Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 23 | #include "ScopedSignalHandler.h" |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 24 | #include "utils.h" |
| 25 | |
| 26 | #include <android-base/file.h> |
| 27 | #include <android-base/strings.h> |
| 28 | |
| 29 | // Old versions of glibc didn't have POSIX_SPAWN_SETSID. |
| 30 | #if __GLIBC__ |
| 31 | # if !defined(POSIX_SPAWN_SETSID) |
| 32 | # define POSIX_SPAWN_SETSID 0 |
| 33 | # endif |
| 34 | #endif |
| 35 | |
| 36 | TEST(spawn, posix_spawnattr_init_posix_spawnattr_destroy) { |
| 37 | posix_spawnattr_t sa; |
| 38 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 39 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 40 | } |
| 41 | |
| 42 | TEST(spawn, posix_spawnattr_setflags_EINVAL) { |
| 43 | posix_spawnattr_t sa; |
| 44 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 45 | ASSERT_EQ(EINVAL, posix_spawnattr_setflags(&sa, ~0)); |
| 46 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 47 | } |
| 48 | |
| 49 | TEST(spawn, posix_spawnattr_setflags_posix_spawnattr_getflags) { |
| 50 | posix_spawnattr_t sa; |
| 51 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 52 | |
| 53 | ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_RESETIDS)); |
| 54 | short flags; |
| 55 | ASSERT_EQ(0, posix_spawnattr_getflags(&sa, &flags)); |
| 56 | ASSERT_EQ(POSIX_SPAWN_RESETIDS, flags); |
| 57 | |
| 58 | constexpr short all_flags = POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | |
| 59 | POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | |
| 60 | POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID; |
| 61 | ASSERT_EQ(0, posix_spawnattr_setflags(&sa, all_flags)); |
| 62 | ASSERT_EQ(0, posix_spawnattr_getflags(&sa, &flags)); |
| 63 | ASSERT_EQ(all_flags, flags); |
| 64 | |
| 65 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 66 | } |
| 67 | |
| 68 | TEST(spawn, posix_spawnattr_setpgroup_posix_spawnattr_getpgroup) { |
| 69 | posix_spawnattr_t sa; |
| 70 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 71 | |
| 72 | ASSERT_EQ(0, posix_spawnattr_setpgroup(&sa, 123)); |
| 73 | pid_t g; |
| 74 | ASSERT_EQ(0, posix_spawnattr_getpgroup(&sa, &g)); |
| 75 | ASSERT_EQ(123, g); |
| 76 | |
| 77 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 78 | } |
| 79 | |
| 80 | TEST(spawn, posix_spawnattr_setsigmask_posix_spawnattr_getsigmask) { |
| 81 | posix_spawnattr_t sa; |
| 82 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 83 | |
| 84 | sigset_t sigs; |
| 85 | ASSERT_EQ(0, posix_spawnattr_getsigmask(&sa, &sigs)); |
| 86 | ASSERT_FALSE(sigismember(&sigs, SIGALRM)); |
| 87 | |
| 88 | sigset_t just_SIGALRM; |
| 89 | sigemptyset(&just_SIGALRM); |
| 90 | sigaddset(&just_SIGALRM, SIGALRM); |
| 91 | ASSERT_EQ(0, posix_spawnattr_setsigmask(&sa, &just_SIGALRM)); |
| 92 | |
| 93 | ASSERT_EQ(0, posix_spawnattr_getsigmask(&sa, &sigs)); |
| 94 | ASSERT_TRUE(sigismember(&sigs, SIGALRM)); |
| 95 | |
| 96 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 97 | } |
| 98 | |
| 99 | TEST(spawn, posix_spawnattr_setsigdefault_posix_spawnattr_getsigdefault) { |
| 100 | posix_spawnattr_t sa; |
| 101 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 102 | |
| 103 | sigset_t sigs; |
| 104 | ASSERT_EQ(0, posix_spawnattr_getsigdefault(&sa, &sigs)); |
| 105 | ASSERT_FALSE(sigismember(&sigs, SIGALRM)); |
| 106 | |
| 107 | sigset_t just_SIGALRM; |
| 108 | sigemptyset(&just_SIGALRM); |
| 109 | sigaddset(&just_SIGALRM, SIGALRM); |
| 110 | ASSERT_EQ(0, posix_spawnattr_setsigdefault(&sa, &just_SIGALRM)); |
| 111 | |
| 112 | ASSERT_EQ(0, posix_spawnattr_getsigdefault(&sa, &sigs)); |
| 113 | ASSERT_TRUE(sigismember(&sigs, SIGALRM)); |
| 114 | |
| 115 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 116 | } |
| 117 | |
| 118 | TEST(spawn, posix_spawnattr_setsschedparam_posix_spawnattr_getsschedparam) { |
| 119 | posix_spawnattr_t sa; |
| 120 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 121 | |
| 122 | sched_param sp; |
| 123 | ASSERT_EQ(0, posix_spawnattr_getschedparam(&sa, &sp)); |
| 124 | ASSERT_EQ(0, sp.sched_priority); |
| 125 | |
| 126 | sched_param sp123 = { .sched_priority = 123 }; |
| 127 | ASSERT_EQ(0, posix_spawnattr_setschedparam(&sa, &sp123)); |
| 128 | |
| 129 | ASSERT_EQ(0, posix_spawnattr_getschedparam(&sa, &sp)); |
| 130 | ASSERT_EQ(123, sp.sched_priority); |
| 131 | |
| 132 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 133 | } |
| 134 | |
| 135 | TEST(spawn, posix_spawnattr_setschedpolicy_posix_spawnattr_getschedpolicy) { |
| 136 | posix_spawnattr_t sa; |
| 137 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 138 | |
| 139 | int p; |
| 140 | ASSERT_EQ(0, posix_spawnattr_getschedpolicy(&sa, &p)); |
| 141 | ASSERT_EQ(0, p); |
| 142 | |
| 143 | ASSERT_EQ(0, posix_spawnattr_setschedpolicy(&sa, SCHED_FIFO)); |
| 144 | |
| 145 | ASSERT_EQ(0, posix_spawnattr_getschedpolicy(&sa, &p)); |
| 146 | ASSERT_EQ(SCHED_FIFO, p); |
| 147 | |
| 148 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 149 | } |
| 150 | |
| 151 | TEST(spawn, posix_spawn) { |
| 152 | ExecTestHelper eth; |
| 153 | eth.SetArgs({BIN_DIR "true", nullptr}); |
| 154 | pid_t pid; |
| 155 | ASSERT_EQ(0, posix_spawn(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr)); |
| 156 | AssertChildExited(pid, 0); |
| 157 | } |
| 158 | |
| 159 | TEST(spawn, posix_spawn_not_found) { |
| 160 | ExecTestHelper eth; |
| 161 | eth.SetArgs({"true", nullptr}); |
| 162 | pid_t pid; |
| 163 | ASSERT_EQ(0, posix_spawn(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr)); |
| 164 | AssertChildExited(pid, 127); |
| 165 | } |
| 166 | |
| 167 | TEST(spawn, posix_spawnp) { |
| 168 | ExecTestHelper eth; |
| 169 | eth.SetArgs({"true", nullptr}); |
| 170 | pid_t pid; |
| 171 | ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr)); |
| 172 | AssertChildExited(pid, 0); |
| 173 | } |
| 174 | |
| 175 | TEST(spawn, posix_spawnp_not_found) { |
| 176 | ExecTestHelper eth; |
| 177 | eth.SetArgs({"does-not-exist", nullptr}); |
| 178 | pid_t pid; |
| 179 | ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr)); |
| 180 | AssertChildExited(pid, 127); |
| 181 | } |
| 182 | |
| 183 | TEST(spawn, posix_spawn_environment) { |
| 184 | ExecTestHelper eth; |
| 185 | eth.SetArgs({"sh", "-c", "exit $posix_spawn_environment_test", nullptr}); |
| 186 | eth.SetEnv({"posix_spawn_environment_test=66", nullptr}); |
| 187 | pid_t pid; |
| 188 | ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), eth.GetEnv())); |
| 189 | AssertChildExited(pid, 66); |
| 190 | } |
| 191 | |
| 192 | TEST(spawn, posix_spawn_file_actions) { |
| 193 | int fds[2]; |
| 194 | ASSERT_NE(-1, pipe(fds)); |
| 195 | |
| 196 | posix_spawn_file_actions_t fa; |
| 197 | ASSERT_EQ(0, posix_spawn_file_actions_init(&fa)); |
| 198 | |
| 199 | ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[0])); |
| 200 | ASSERT_EQ(0, posix_spawn_file_actions_adddup2(&fa, fds[1], 1)); |
| 201 | ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[1])); |
| 202 | // Check that close(2) failures are ignored by closing the same fd again. |
| 203 | ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[1])); |
| 204 | ASSERT_EQ(0, posix_spawn_file_actions_addopen(&fa, 56, "/proc/version", O_RDONLY, 0)); |
| 205 | |
| 206 | ExecTestHelper eth; |
| 207 | eth.SetArgs({"ls", "-l", "/proc/self/fd", nullptr}); |
| 208 | pid_t pid; |
| 209 | ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), &fa, nullptr, eth.GetArgs(), eth.GetEnv())); |
| 210 | ASSERT_EQ(0, posix_spawn_file_actions_destroy(&fa)); |
| 211 | |
| 212 | ASSERT_EQ(0, close(fds[1])); |
| 213 | std::string content; |
| 214 | ASSERT_TRUE(android::base::ReadFdToString(fds[0], &content)); |
| 215 | ASSERT_EQ(0, close(fds[0])); |
| 216 | |
| 217 | AssertChildExited(pid, 0); |
| 218 | |
| 219 | // We'll know the dup2 worked if we see any ls(1) output in our pipe. |
| 220 | // The open we can check manually... |
| 221 | bool open_to_fd_56_worked = false; |
| 222 | for (const auto& line : android::base::Split(content, "\n")) { |
| 223 | if (line.find(" 56 -> /proc/version") != std::string::npos) open_to_fd_56_worked = true; |
| 224 | } |
| 225 | ASSERT_TRUE(open_to_fd_56_worked); |
| 226 | } |
| 227 | |
| 228 | static void CatFileToString(posix_spawnattr_t* sa, const char* path, std::string* content) { |
| 229 | int fds[2]; |
| 230 | ASSERT_NE(-1, pipe(fds)); |
| 231 | |
| 232 | posix_spawn_file_actions_t fa; |
| 233 | ASSERT_EQ(0, posix_spawn_file_actions_init(&fa)); |
| 234 | ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[0])); |
| 235 | ASSERT_EQ(0, posix_spawn_file_actions_adddup2(&fa, fds[1], 1)); |
| 236 | ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[1])); |
| 237 | |
| 238 | ExecTestHelper eth; |
| 239 | eth.SetArgs({"cat", path, nullptr}); |
| 240 | pid_t pid; |
| 241 | ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), &fa, sa, eth.GetArgs(), nullptr)); |
| 242 | ASSERT_EQ(0, posix_spawn_file_actions_destroy(&fa)); |
| 243 | |
| 244 | ASSERT_EQ(0, close(fds[1])); |
| 245 | ASSERT_TRUE(android::base::ReadFdToString(fds[0], content)); |
| 246 | ASSERT_EQ(0, close(fds[0])); |
| 247 | AssertChildExited(pid, 0); |
| 248 | } |
| 249 | |
| 250 | struct ProcStat { |
| 251 | pid_t pid; |
| 252 | pid_t ppid; |
| 253 | pid_t pgrp; |
| 254 | pid_t sid; |
| 255 | }; |
| 256 | |
| 257 | static void GetChildStat(posix_spawnattr_t* sa, ProcStat* ps) { |
| 258 | std::string content; |
| 259 | CatFileToString(sa, "/proc/self/stat", &content); |
| 260 | |
| 261 | ASSERT_EQ(4, sscanf(content.c_str(), "%d (cat) %*c %d %d %d", &ps->pid, &ps->ppid, &ps->pgrp, |
| 262 | &ps->sid)); |
| 263 | |
| 264 | ASSERT_EQ(getpid(), ps->ppid); |
| 265 | } |
| 266 | |
| 267 | struct ProcStatus { |
| 268 | uint64_t sigblk; |
| 269 | uint64_t sigign; |
| 270 | }; |
| 271 | |
| 272 | static void GetChildStatus(posix_spawnattr_t* sa, ProcStatus* ps) { |
| 273 | std::string content; |
| 274 | CatFileToString(sa, "/proc/self/status", &content); |
| 275 | |
| 276 | bool saw_blk = false; |
| 277 | bool saw_ign = false; |
| 278 | for (const auto& line : android::base::Split(content, "\n")) { |
| 279 | if (sscanf(line.c_str(), "SigBlk: %" SCNx64, &ps->sigblk) == 1) saw_blk = true; |
| 280 | if (sscanf(line.c_str(), "SigIgn: %" SCNx64, &ps->sigign) == 1) saw_ign = true; |
| 281 | } |
| 282 | ASSERT_TRUE(saw_blk); |
| 283 | ASSERT_TRUE(saw_ign); |
| 284 | } |
| 285 | |
| 286 | TEST(spawn, posix_spawn_POSIX_SPAWN_SETSID_clear) { |
| 287 | pid_t parent_sid = getsid(0); |
| 288 | |
| 289 | posix_spawnattr_t sa; |
| 290 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 291 | ASSERT_EQ(0, posix_spawnattr_setflags(&sa, 0)); |
| 292 | |
| 293 | ProcStat ps = {}; |
| 294 | GetChildStat(&sa, &ps); |
| 295 | ASSERT_EQ(parent_sid, ps.sid); |
| 296 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 297 | } |
| 298 | |
| 299 | TEST(spawn, posix_spawn_POSIX_SPAWN_SETSID_set) { |
| 300 | pid_t parent_sid = getsid(0); |
| 301 | |
| 302 | posix_spawnattr_t sa; |
| 303 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 304 | ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSID)); |
| 305 | |
| 306 | ProcStat ps = {}; |
| 307 | GetChildStat(&sa, &ps); |
| 308 | ASSERT_NE(parent_sid, ps.sid); |
| 309 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 310 | } |
| 311 | |
| 312 | TEST(spawn, posix_spawn_POSIX_SPAWN_SETPGROUP_clear) { |
| 313 | pid_t parent_pgrp = getpgrp(); |
| 314 | |
| 315 | posix_spawnattr_t sa; |
| 316 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 317 | ASSERT_EQ(0, posix_spawnattr_setflags(&sa, 0)); |
| 318 | |
| 319 | ProcStat ps = {}; |
| 320 | GetChildStat(&sa, &ps); |
| 321 | ASSERT_EQ(parent_pgrp, ps.pgrp); |
| 322 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 323 | } |
| 324 | |
| 325 | TEST(spawn, posix_spawn_POSIX_SPAWN_SETPGROUP_set) { |
| 326 | pid_t parent_pgrp = getpgrp(); |
| 327 | |
| 328 | posix_spawnattr_t sa; |
| 329 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 330 | ASSERT_EQ(0, posix_spawnattr_setpgroup(&sa, 0)); |
| 331 | ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETPGROUP)); |
| 332 | |
| 333 | ProcStat ps = {}; |
| 334 | GetChildStat(&sa, &ps); |
| 335 | ASSERT_NE(parent_pgrp, ps.pgrp); |
| 336 | // Setting pgid 0 means "the same as the caller's pid". |
| 337 | ASSERT_EQ(ps.pid, ps.pgrp); |
| 338 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 339 | } |
| 340 | |
| 341 | TEST(spawn, posix_spawn_POSIX_SPAWN_SETSIGMASK) { |
| 342 | // Block SIGBUS in the parent... |
| 343 | sigset_t just_SIGBUS; |
| 344 | sigemptyset(&just_SIGBUS); |
| 345 | sigaddset(&just_SIGBUS, SIGBUS); |
| 346 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGBUS, nullptr)); |
| 347 | |
| 348 | posix_spawnattr_t sa; |
| 349 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 350 | |
| 351 | // Ask for only SIGALRM to be blocked in the child... |
| 352 | sigset_t just_SIGALRM; |
| 353 | sigemptyset(&just_SIGALRM); |
| 354 | sigaddset(&just_SIGALRM, SIGALRM); |
| 355 | ASSERT_EQ(0, posix_spawnattr_setsigmask(&sa, &just_SIGALRM)); |
| 356 | ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSIGMASK)); |
| 357 | |
| 358 | // Check that's what happens... |
| 359 | ProcStatus ps = {}; |
| 360 | GetChildStatus(&sa, &ps); |
| 361 | EXPECT_EQ(static_cast<uint64_t>(1 << (SIGALRM - 1)), ps.sigblk); |
| 362 | EXPECT_EQ(static_cast<uint64_t>(0), ps.sigign); |
| 363 | |
| 364 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 365 | } |
| 366 | |
| 367 | TEST(spawn, posix_spawn_POSIX_SPAWN_SETSIGDEF) { |
| 368 | // Ignore SIGALRM and SIGCONT in the parent... |
| 369 | ASSERT_NE(SIG_ERR, signal(SIGALRM, SIG_IGN)); |
| 370 | ASSERT_NE(SIG_ERR, signal(SIGCONT, SIG_IGN)); |
| 371 | |
| 372 | posix_spawnattr_t sa; |
| 373 | ASSERT_EQ(0, posix_spawnattr_init(&sa)); |
| 374 | |
| 375 | // Ask for SIGALRM to be defaulted in the child... |
| 376 | sigset_t just_SIGALRM; |
| 377 | sigemptyset(&just_SIGALRM); |
| 378 | sigaddset(&just_SIGALRM, SIGALRM); |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame^] | 379 | |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 380 | ASSERT_EQ(0, posix_spawnattr_setsigdefault(&sa, &just_SIGALRM)); |
| 381 | ASSERT_EQ(0, posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSIGDEF)); |
| 382 | |
| 383 | // Check that's what happens... |
| 384 | ProcStatus ps = {}; |
| 385 | GetChildStatus(&sa, &ps); |
| 386 | EXPECT_EQ(static_cast<uint64_t>(0), ps.sigblk); |
| 387 | EXPECT_EQ(static_cast<uint64_t>(1 << (SIGCONT - 1)), ps.sigign); |
| 388 | |
| 389 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
| 390 | } |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 391 | |
| 392 | TEST(spawn, signal_stress) { |
| 393 | // Ensure that posix_spawn doesn't restore the caller's signal mask in the |
| 394 | // child without first defaulting any caught signals (http://b/68707996). |
| 395 | static pid_t parent = getpid(); |
| 396 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame^] | 397 | setpgid(0, 0); |
| 398 | |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 399 | pid_t pid = fork(); |
| 400 | ASSERT_NE(-1, pid); |
| 401 | |
| 402 | if (pid == 0) { |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame^] | 403 | signal(SIGRTMIN, SIG_IGN); |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 404 | for (size_t i = 0; i < 1024; ++i) { |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame^] | 405 | kill(0, SIGRTMIN); |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 406 | usleep(10); |
| 407 | } |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame^] | 408 | _exit(99); |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // We test both with and without attributes, because they used to be |
| 412 | // different codepaths. We also test with an empty `sigdefault` set. |
| 413 | posix_spawnattr_t attr1; |
| 414 | posix_spawnattr_init(&attr1); |
| 415 | |
| 416 | sigset_t empty_mask = {}; |
| 417 | posix_spawnattr_t attr2; |
| 418 | posix_spawnattr_init(&attr2); |
| 419 | posix_spawnattr_setflags(&attr2, POSIX_SPAWN_SETSIGDEF); |
| 420 | posix_spawnattr_setsigdefault(&attr2, &empty_mask); |
| 421 | |
| 422 | posix_spawnattr_t* attrs[] = { nullptr, &attr1, &attr2 }; |
| 423 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame^] | 424 | // We use a real-time signal because that's a tricky case for LP32 |
| 425 | // because our sigset_t was too small. |
| 426 | ScopedSignalHandler ssh(SIGRTMIN, [](int) { ASSERT_EQ(getpid(), parent); }); |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 427 | |
| 428 | ExecTestHelper eth; |
| 429 | eth.SetArgs({"true", nullptr}); |
| 430 | for (size_t i = 0; i < 128; ++i) { |
| 431 | posix_spawn(nullptr, "true", nullptr, attrs[i % 3], eth.GetArgs(), nullptr); |
| 432 | } |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame^] | 433 | |
| 434 | AssertChildExited(pid, 99); |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 435 | } |