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> |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 21 | #include <sys/cdefs.h> |
| 22 | |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 25 | #include "SignalUtils.h" |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 26 | #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 Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 36 | #elif defined(__BIONIC__) |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 37 | #include <platform/bionic/reserved_signals.h> |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | TEST(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 | |
| 46 | TEST(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 | |
| 53 | TEST(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 | |
| 72 | TEST(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 | |
| 84 | TEST(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 Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 103 | TEST(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 Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 122 | TEST(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 Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 141 | TEST(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 Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 160 | TEST(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 | |
| 177 | TEST(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 | |
| 193 | TEST(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 | |
| 201 | TEST(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 | |
| 209 | TEST(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 | |
| 217 | TEST(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 | |
| 225 | TEST(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 | |
| 234 | TEST(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 | |
| 270 | static 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 | |
| 292 | struct ProcStat { |
| 293 | pid_t pid; |
| 294 | pid_t ppid; |
| 295 | pid_t pgrp; |
| 296 | pid_t sid; |
| 297 | }; |
| 298 | |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 299 | static __attribute__((unused)) void GetChildStat(posix_spawnattr_t* sa, ProcStat* ps) { |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 300 | 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 | |
| 309 | struct ProcStatus { |
| 310 | uint64_t sigblk; |
| 311 | uint64_t sigign; |
| 312 | }; |
| 313 | |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 314 | static void __attribute__((unused)) GetChildStatus(posix_spawnattr_t* sa, ProcStatus* ps) { |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 315 | 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 | |
| 328 | TEST(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 | |
| 341 | TEST(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 | |
| 354 | TEST(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 | |
| 367 | TEST(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 | |
| 383 | TEST(spawn, posix_spawn_POSIX_SPAWN_SETSIGMASK) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 384 | #if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL) |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 385 | GTEST_SKIP() << "glibc doesn't ignore the same signals."; |
| 386 | #else |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 387 | // 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 Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 406 | |
| 407 | // TIMER_SIGNAL should also be blocked. |
| 408 | uint64_t expected_blocked = 0; |
| 409 | SignalSetAdd(&expected_blocked, SIGALRM); |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 410 | SignalSetAdd(&expected_blocked, BIONIC_SIGNAL_POSIX_TIMERS); |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 411 | EXPECT_EQ(expected_blocked, ps.sigblk); |
| 412 | |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 413 | uint64_t expected_ignored = 0; |
| 414 | SignalSetAdd(&expected_ignored, BIONIC_SIGNAL_ART_PROFILER); |
| 415 | EXPECT_EQ(expected_ignored, ps.sigign); |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 416 | |
| 417 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 418 | #endif |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | TEST(spawn, posix_spawn_POSIX_SPAWN_SETSIGDEF) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 422 | #if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL) |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 423 | GTEST_SKIP() << "glibc doesn't ignore the same signals."; |
| 424 | #else |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 425 | // 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 Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 436 | |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 437 | 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 Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 443 | |
| 444 | // TIMER_SIGNAL should be blocked. |
| 445 | uint64_t expected_blocked = 0; |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 446 | SignalSetAdd(&expected_blocked, BIONIC_SIGNAL_POSIX_TIMERS); |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 447 | EXPECT_EQ(expected_blocked, ps.sigblk); |
| 448 | |
| 449 | uint64_t expected_ignored = 0; |
| 450 | SignalSetAdd(&expected_ignored, SIGCONT); |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 451 | SignalSetAdd(&expected_ignored, BIONIC_SIGNAL_ART_PROFILER); |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 452 | EXPECT_EQ(expected_ignored, ps.sigign); |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 453 | |
| 454 | ASSERT_EQ(0, posix_spawnattr_destroy(&sa)); |
Florian Mayer | 96272df | 2020-03-24 15:59:27 +0100 | [diff] [blame] | 455 | #endif |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 456 | } |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 457 | |
| 458 | TEST(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 Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 463 | setpgid(0, 0); |
Ryan Prichard | a1bc826 | 2018-04-03 20:46:11 -0700 | [diff] [blame] | 464 | signal(SIGRTMIN, SIG_IGN); |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 465 | |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 466 | pid_t pid = fork(); |
| 467 | ASSERT_NE(-1, pid); |
| 468 | |
| 469 | if (pid == 0) { |
| 470 | for (size_t i = 0; i < 1024; ++i) { |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 471 | kill(0, SIGRTMIN); |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 472 | usleep(10); |
| 473 | } |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 474 | _exit(99); |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 475 | } |
| 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 Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 490 | // 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 Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 493 | |
Ryan Prichard | 344969c | 2018-04-09 16:26:20 -0700 | [diff] [blame] | 494 | const size_t pid_count = 128; |
| 495 | pid_t spawned_pids[pid_count]; |
| 496 | |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 497 | ExecTestHelper eth; |
| 498 | eth.SetArgs({"true", nullptr}); |
Ryan Prichard | 344969c | 2018-04-09 16:26:20 -0700 | [diff] [blame] | 499 | 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 Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 507 | } |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 508 | |
| 509 | AssertChildExited(pid, 99); |
Elliott Hughes | 7bfacaa | 2017-11-28 19:58:00 -0800 | [diff] [blame] | 510 | } |