1 /* 2 * Copyright (C) 2012 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 <errno.h> 18 #include <signal.h> 19 #include <sys/syscall.h> 20 #include <sys/types.h> 21 #include <unistd.h> 22 23 #include <thread> 24 25 #include <android-base/macros.h> 26 #include <gtest/gtest.h> 27 28 #include "SignalUtils.h" 29 30 static int SIGNAL_MIN() { 31 return 1; // Signals start at 1 (SIGHUP), not 0. 32 } 33 34 template <typename SigSetT> 35 static int SIGNAL_MAX(SigSetT* set) { 36 return sizeof(*set) * 8; 37 } 38 39 template <typename SigSetT> 40 static void TestSigSet1(int (fn)(SigSetT*)) { 41 // nullptr sigset_t*/sigset64_t*. 42 SigSetT* set_ptr = nullptr; 43 errno = 0; 44 ASSERT_EQ(-1, fn(set_ptr)); 45 ASSERT_EQ(EINVAL, errno); 46 47 // Non-nullptr. 48 SigSetT set = {}; 49 errno = 0; 50 ASSERT_EQ(0, fn(&set)); 51 ASSERT_EQ(0, errno); 52 } 53 54 template <typename SigSetT> 55 static void TestSigSet2(int (fn)(SigSetT*, int)) { 56 // nullptr sigset_t*/sigset64_t*. 57 SigSetT* set_ptr = nullptr; 58 errno = 0; 59 ASSERT_EQ(-1, fn(set_ptr, SIGSEGV)); 60 ASSERT_EQ(EINVAL, errno); 61 62 SigSetT set = {}; 63 64 // Bad signal number: too small. 65 errno = 0; 66 ASSERT_EQ(-1, fn(&set, 0)); 67 ASSERT_EQ(EINVAL, errno); 68 69 // Bad signal number: too high. 70 errno = 0; 71 ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1)); 72 ASSERT_EQ(EINVAL, errno); 73 74 // Good signal numbers, low and high ends of range. 75 errno = 0; 76 ASSERT_EQ(0, fn(&set, SIGNAL_MIN())); 77 ASSERT_EQ(0, errno); 78 ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set))); 79 ASSERT_EQ(0, errno); 80 } 81 82 TEST(signal, sigaddset_invalid) { 83 TestSigSet2(sigaddset); 84 } 85 86 TEST(signal, sigaddset64_invalid) { 87 #if defined(__BIONIC__) 88 TestSigSet2(sigaddset64); 89 #endif 90 } 91 92 TEST(signal, sigdelset_invalid) { 93 TestSigSet2(sigdelset); 94 } 95 96 TEST(signal, sigdelset64_invalid) { 97 #if defined(__BIONIC__) 98 TestSigSet2(sigdelset64); 99 #endif 100 } 101 102 TEST(signal, sigemptyset_invalid) { 103 TestSigSet1(sigemptyset); 104 } 105 106 TEST(signal, sigemptyset64_invalid) { 107 #if defined(__BIONIC__) 108 TestSigSet1(sigemptyset64); 109 #endif 110 } 111 112 TEST(signal, sigfillset_invalid) { 113 TestSigSet1(sigfillset); 114 } 115 116 TEST(signal, sigfillset64_invalid) { 117 #if defined(__BIONIC__) 118 TestSigSet1(sigfillset64); 119 #endif 120 } 121 122 TEST(signal, sigismember_invalid) { 123 TestSigSet2(sigismember); 124 } 125 126 TEST(signal, sigismember64_invalid) { 127 #if defined(__BIONIC__) 128 TestSigSet2(sigismember64); 129 #endif 130 } 131 132 TEST(signal, raise_invalid) { 133 errno = 0; 134 ASSERT_EQ(-1, raise(-1)); 135 ASSERT_EQ(EINVAL, errno); 136 } 137 138 static void raise_in_signal_handler_helper(int signal_number) { 139 ASSERT_EQ(SIGALRM, signal_number); 140 static int count = 0; 141 if (++count == 1) { 142 raise(SIGALRM); 143 } 144 } 145 146 TEST(signal, raise_in_signal_handler) { 147 ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper); 148 raise(SIGALRM); 149 } 150 151 TEST(signal, sigwait_SIGALRM) { 152 ScopedSignalHandler ssh(SIGALRM, [](int sig) { ASSERT_EQ(SIGALRM, sig); }); 153 154 sigset_t wait_set; 155 sigemptyset(&wait_set); 156 sigaddset(&wait_set, SIGALRM); 157 158 alarm(1); 159 160 int received_signal; 161 errno = 0; 162 ASSERT_EQ(0, sigwait(&wait_set, &received_signal)); 163 ASSERT_EQ(0, errno); 164 ASSERT_EQ(SIGALRM, received_signal); 165 } 166 167 TEST(signal, sigwait64_SIGRTMIN) { 168 ScopedSignalHandler ssh(SIGRTMIN, [](int sig) { ASSERT_EQ(SIGRTMIN, sig); }); 169 170 sigset64_t wait_set; 171 sigemptyset64(&wait_set); 172 sigaddset64(&wait_set, SIGRTMIN); 173 174 pid_t pid = getpid(); 175 std::thread thread([&pid]() { 176 usleep(5000); 177 kill(pid, SIGRTMIN); 178 }); 179 180 int received_signal; 181 errno = 0; 182 ASSERT_EQ(0, sigwait64(&wait_set, &received_signal)); 183 ASSERT_EQ(0, errno); 184 ASSERT_EQ(SIGRTMIN, received_signal); 185 186 thread.join(); 187 } 188 189 static int g_sigsuspend_signal_handler_call_count = 0; 190 191 TEST(signal, sigsuspend_sigpending) { 192 SignalMaskRestorer smr; 193 194 // Block SIGALRM. 195 sigset_t just_SIGALRM; 196 sigemptyset(&just_SIGALRM); 197 sigaddset(&just_SIGALRM, SIGALRM); 198 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); 199 200 ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; }); 201 202 // There should be no pending signals. 203 sigset_t pending; 204 sigemptyset(&pending); 205 ASSERT_EQ(0, sigpending(&pending)); 206 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { 207 EXPECT_FALSE(sigismember(&pending, i)) << i; 208 } 209 210 // Raise SIGALRM and check our signal handler wasn't called. 211 raise(SIGALRM); 212 ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count); 213 214 // We should now have a pending SIGALRM but nothing else. 215 sigemptyset(&pending); 216 ASSERT_EQ(0, sigpending(&pending)); 217 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { 218 EXPECT_EQ((i == SIGALRM), sigismember(&pending, i)); 219 } 220 221 // Use sigsuspend to block everything except SIGALRM... 222 sigset_t not_SIGALRM; 223 sigfillset(¬_SIGALRM); 224 sigdelset(¬_SIGALRM, SIGALRM); 225 ASSERT_EQ(-1, sigsuspend(¬_SIGALRM)); 226 ASSERT_EQ(EINTR, errno); 227 // ...and check that we now receive our pending SIGALRM. 228 ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count); 229 } 230 231 static int g_sigsuspend64_signal_handler_call_count = 0; 232 233 TEST(signal, sigsuspend64_sigpending64) { 234 SignalMaskRestorer smr; 235 236 // Block SIGRTMIN. 237 sigset64_t just_SIGRTMIN; 238 sigemptyset64(&just_SIGRTMIN); 239 sigaddset64(&just_SIGRTMIN, SIGRTMIN); 240 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); 241 242 ScopedSignalHandler ssh(SIGRTMIN, [](int) { ++g_sigsuspend64_signal_handler_call_count; }); 243 244 // There should be no pending signals. 245 sigset64_t pending; 246 sigemptyset64(&pending); 247 ASSERT_EQ(0, sigpending64(&pending)); 248 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { 249 EXPECT_FALSE(sigismember64(&pending, i)) << i; 250 } 251 252 // Raise SIGRTMIN and check our signal handler wasn't called. 253 raise(SIGRTMIN); 254 ASSERT_EQ(0, g_sigsuspend64_signal_handler_call_count); 255 256 // We should now have a pending SIGRTMIN but nothing else. 257 sigemptyset64(&pending); 258 ASSERT_EQ(0, sigpending64(&pending)); 259 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { 260 EXPECT_EQ((i == SIGRTMIN), sigismember64(&pending, i)); 261 } 262 263 // Use sigsuspend64 to block everything except SIGRTMIN... 264 sigset64_t not_SIGRTMIN; 265 sigfillset64(¬_SIGRTMIN); 266 sigdelset64(¬_SIGRTMIN, SIGRTMIN); 267 ASSERT_EQ(-1, sigsuspend64(¬_SIGRTMIN)); 268 ASSERT_EQ(EINTR, errno); 269 // ...and check that we now receive our pending SIGRTMIN. 270 ASSERT_EQ(1, g_sigsuspend64_signal_handler_call_count); 271 } 272 273 template <typename SigActionT, typename SigSetT> 274 static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*), 275 int (sigaddset_fn)(SigSetT*, int), 276 int sig) { 277 // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm, 278 // arm64, x86, and x86-64. The version of glibc we're using also doesn't 279 // define SA_RESTORER, but luckily it's the same value everywhere, and mips 280 // doesn't use the bit for anything. 281 static const unsigned sa_restorer = 0x4000000; 282 283 // See what's currently set for this signal. 284 SigActionT original_sa = {}; 285 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &original_sa)); 286 ASSERT_TRUE(original_sa.sa_handler == nullptr); 287 ASSERT_TRUE(original_sa.sa_sigaction == nullptr); 288 ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer); 289 #ifdef SA_RESTORER 290 ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer)); 291 #endif 292 293 // Set a traditional sa_handler signal handler. 294 auto no_op_signal_handler = [](int) {}; 295 SigActionT sa = {}; 296 sigaddset_fn(&sa.sa_mask, sig); 297 sa.sa_flags = SA_ONSTACK; 298 sa.sa_handler = no_op_signal_handler; 299 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr)); 300 301 // Check that we can read it back. 302 sa = {}; 303 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa)); 304 ASSERT_TRUE(sa.sa_handler == no_op_signal_handler); 305 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler); 306 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer); 307 #ifdef SA_RESTORER 308 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer)); 309 #endif 310 311 // Set a new-style sa_sigaction signal handler. 312 auto no_op_sigaction = [](int, siginfo_t*, void*) {}; 313 sa = {}; 314 sigaddset_fn(&sa.sa_mask, sig); 315 sa.sa_flags = SA_ONSTACK | SA_SIGINFO; 316 sa.sa_sigaction = no_op_sigaction; 317 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr)); 318 319 // Check that we can read it back. 320 sa = {}; 321 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa)); 322 ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction); 323 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler); 324 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer); 325 #ifdef SA_RESTORER 326 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer)); 327 #endif 328 329 // Put everything back how it was. 330 ASSERT_EQ(0, sigaction_fn(sig, &original_sa, nullptr)); 331 } 332 333 TEST(signal, sigaction) { 334 TestSigAction(sigaction, sigaddset, SIGALRM); 335 } 336 337 TEST(signal, sigaction64_SIGRTMIN) { 338 TestSigAction(sigaction64, sigaddset64, SIGRTMIN); 339 } 340 341 static void ClearSignalMask() { 342 uint64_t sigset = 0; 343 SignalSetAdd(&sigset, __SIGRTMIN); 344 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) { 345 abort(); 346 } 347 } 348 349 static void FillSignalMask() { 350 uint64_t sigset = ~0ULL; 351 for (int signo = __SIGRTMIN + 1; signo < SIGRTMIN; ++signo) { 352 SignalSetDel(&sigset, signo); 353 } 354 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) { 355 abort(); 356 } 357 } 358 359 static uint64_t GetSignalMask() { 360 uint64_t sigset; 361 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, nullptr, &sigset, sizeof(sigset)) != 0) { 362 abort(); 363 } 364 return sigset; 365 } 366 367 static void TestSignalMaskFiltered(uint64_t sigset) { 368 #if defined(__BIONIC__) 369 for (int signo = __SIGRTMIN; signo < SIGRTMIN; ++signo) { 370 bool signal_blocked = sigset & (1ULL << (signo - 1)); 371 if (signo == __SIGRTMIN) { 372 // TIMER_SIGNAL must be blocked. 373 EXPECT_EQ(true, signal_blocked) << "signal " << signo; 374 } else { 375 // The other reserved signals must not be blocked. 376 EXPECT_EQ(false, signal_blocked) << "signal " << signo; 377 } 378 } 379 #else 380 UNUSED(sigset); 381 #endif 382 } 383 384 static void TestSignalMaskFunction(std::function<void()> fn) { 385 ClearSignalMask(); 386 fn(); 387 TestSignalMaskFiltered(GetSignalMask()); 388 } 389 390 TEST(signal, sigaction_filter) { 391 ClearSignalMask(); 392 static uint64_t sigset; 393 struct sigaction sa = {}; 394 sa.sa_handler = [](int) { sigset = GetSignalMask(); }; 395 sa.sa_flags = SA_ONSTACK | SA_NODEFER; 396 sigfillset(&sa.sa_mask); 397 sigaction(SIGUSR1, &sa, nullptr); 398 raise(SIGUSR1); 399 400 // On LP32, struct sigaction::sa_mask is only 32-bits wide. 401 unsigned long expected_sigset = ~0UL; 402 403 // SIGKILL and SIGSTOP are always blocked. 404 expected_sigset &= ~(1UL << (SIGKILL - 1)); 405 expected_sigset &= ~(1UL << (SIGSTOP - 1)); 406 407 ASSERT_EQ(static_cast<uint64_t>(expected_sigset), sigset); 408 } 409 410 TEST(signal, sigaction64_filter) { 411 ClearSignalMask(); 412 static uint64_t sigset; 413 struct sigaction64 sa = {}; 414 sa.sa_handler = [](int) { sigset = GetSignalMask(); }; 415 sa.sa_flags = SA_ONSTACK | SA_NODEFER; 416 sigfillset64(&sa.sa_mask); 417 sigaction64(SIGUSR1, &sa, nullptr); 418 raise(SIGUSR1); 419 420 uint64_t expected_sigset = ~0ULL; 421 422 // SIGKILL and SIGSTOP are always blocked. 423 expected_sigset &= ~(1ULL << (SIGKILL - 1)); 424 expected_sigset &= ~(1ULL << (SIGSTOP - 1)); 425 426 ASSERT_EQ(expected_sigset, sigset); 427 } 428 429 TEST(signal, sigprocmask_setmask_filter) { 430 TestSignalMaskFunction([]() { 431 ClearSignalMask(); 432 sigset_t sigset_libc; 433 sigfillset(&sigset_libc); 434 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &sigset_libc, nullptr)); 435 }); 436 } 437 438 TEST(signal, sigprocmask64_setmask_filter) { 439 TestSignalMaskFunction([]() { 440 ClearSignalMask(); 441 sigset64_t sigset_libc; 442 sigfillset64(&sigset_libc); 443 ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &sigset_libc, nullptr)); 444 }); 445 } 446 447 TEST(signal, pthread_sigmask_setmask_filter) { 448 TestSignalMaskFunction([]() { 449 ClearSignalMask(); 450 sigset_t sigset_libc; 451 sigfillset(&sigset_libc); 452 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &sigset_libc, nullptr)); 453 }); 454 } 455 456 TEST(signal, pthread_sigmask64_setmask_filter) { 457 TestSignalMaskFunction([]() { 458 ClearSignalMask(); 459 sigset64_t sigset_libc; 460 sigfillset64(&sigset_libc); 461 ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &sigset_libc, nullptr)); 462 }); 463 } 464 465 TEST(signal, sigprocmask_block_filter) { 466 TestSignalMaskFunction([]() { 467 ClearSignalMask(); 468 sigset_t sigset_libc; 469 sigfillset(&sigset_libc); 470 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset_libc, nullptr)); 471 }); 472 } 473 474 TEST(signal, sigprocmask64_block_filter) { 475 TestSignalMaskFunction([]() { 476 ClearSignalMask(); 477 sigset64_t sigset_libc; 478 sigfillset64(&sigset_libc); 479 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &sigset_libc, nullptr)); 480 }); 481 } 482 483 TEST(signal, pthread_sigmask_block_filter) { 484 TestSignalMaskFunction([]() { 485 ClearSignalMask(); 486 sigset_t sigset_libc; 487 sigfillset(&sigset_libc); 488 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &sigset_libc, nullptr)); 489 }); 490 } 491 492 TEST(signal, pthread_sigmask64_block_filter) { 493 TestSignalMaskFunction([]() { 494 ClearSignalMask(); 495 sigset64_t sigset_libc; 496 sigfillset64(&sigset_libc); 497 ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &sigset_libc, nullptr)); 498 }); 499 } 500 501 TEST(signal, sigprocmask_unblock_filter) { 502 TestSignalMaskFunction([]() { 503 FillSignalMask(); 504 sigset_t sigset_libc; 505 sigfillset(&sigset_libc); 506 ASSERT_EQ(0, sigprocmask(SIG_UNBLOCK, &sigset_libc, nullptr)); 507 }); 508 } 509 510 TEST(signal, sigprocmask64_unblock_filter) { 511 TestSignalMaskFunction([]() { 512 FillSignalMask(); 513 sigset64_t sigset_libc; 514 sigfillset64(&sigset_libc); 515 ASSERT_EQ(0, sigprocmask64(SIG_UNBLOCK, &sigset_libc, nullptr)); 516 }); 517 } 518 519 TEST(signal, pthread_sigmask_unblock_filter) { 520 TestSignalMaskFunction([]() { 521 FillSignalMask(); 522 sigset_t sigset_libc; 523 sigfillset(&sigset_libc); 524 ASSERT_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigset_libc, nullptr)); 525 }); 526 } 527 528 TEST(signal, pthread_sigmask64_unblock_filter) { 529 TestSignalMaskFunction([]() { 530 FillSignalMask(); 531 sigset64_t sigset_libc; 532 sigfillset64(&sigset_libc); 533 ASSERT_EQ(0, pthread_sigmask64(SIG_UNBLOCK, &sigset_libc, nullptr)); 534 }); 535 } 536 537 // glibc filters out signals via sigfillset, not the actual underlying functions. 538 TEST(signal, sigset_filter) { 539 #if defined(__BIONIC__) 540 TestSignalMaskFunction([]() { 541 for (int i = 1; i <= 64; ++i) { 542 sigset(i, SIG_HOLD); 543 } 544 }); 545 #endif 546 } 547 548 TEST(signal, sighold_filter) { 549 #if defined(__BIONIC__) 550 TestSignalMaskFunction([]() { 551 for (int i = 1; i <= 64; ++i) { 552 sighold(i); 553 } 554 }); 555 #endif 556 } 557 558 #if defined(__BIONIC__) 559 // Not exposed via headers, but the symbols are available if you declare them yourself. 560 extern "C" int sigblock(int); 561 extern "C" int sigsetmask(int); 562 #endif 563 564 TEST(signal, sigblock_filter) { 565 #if defined(__BIONIC__) 566 TestSignalMaskFunction([]() { 567 sigblock(~0U); 568 }); 569 #endif 570 } 571 572 TEST(signal, sigsetmask_filter) { 573 #if defined(__BIONIC__) 574 TestSignalMaskFunction([]() { 575 sigsetmask(~0U); 576 }); 577 #endif 578 } 579 580 TEST(signal, sys_signame) { 581 #if defined(__BIONIC__) 582 ASSERT_TRUE(sys_signame[0] == nullptr); 583 ASSERT_STREQ("HUP", sys_signame[SIGHUP]); 584 #else 585 GTEST_SKIP() << "glibc doesn't have sys_signame"; 586 #endif 587 } 588 589 TEST(signal, sys_siglist) { 590 ASSERT_TRUE(sys_siglist[0] == nullptr); 591 ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]); 592 } 593 594 TEST(signal, limits) { 595 // This comes from the kernel. 596 ASSERT_EQ(32, __SIGRTMIN); 597 598 // We reserve a non-zero number at the bottom for ourselves. 599 ASSERT_GT(SIGRTMIN, __SIGRTMIN); 600 601 // MIPS has more signals than everyone else. 602 #if defined(__mips__) 603 ASSERT_EQ(128, __SIGRTMAX); 604 #else 605 ASSERT_EQ(64, __SIGRTMAX); 606 #endif 607 608 // We don't currently reserve any at the top. 609 ASSERT_EQ(SIGRTMAX, __SIGRTMAX); 610 } 611 612 static int g_sigqueue_signal_handler_call_count = 0; 613 614 static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) { 615 ASSERT_EQ(SIGALRM, signum); 616 ASSERT_EQ(SIGALRM, info->si_signo); 617 ASSERT_EQ(SI_QUEUE, info->si_code); 618 ASSERT_EQ(1, info->si_value.sival_int); 619 ++g_sigqueue_signal_handler_call_count; 620 } 621 622 TEST(signal, sigqueue) { 623 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); 624 sigval_t sigval; 625 sigval.sival_int = 1; 626 errno = 0; 627 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); 628 ASSERT_EQ(0, errno); 629 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); 630 } 631 632 TEST(signal, pthread_sigqueue_self) { 633 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); 634 sigval_t sigval; 635 sigval.sival_int = 1; 636 errno = 0; 637 ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval)); 638 ASSERT_EQ(0, errno); 639 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); 640 } 641 642 TEST(signal, pthread_sigqueue_other) { 643 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); 644 sigval_t sigval; 645 sigval.sival_int = 1; 646 647 sigset_t mask; 648 sigfillset(&mask); 649 pthread_sigmask(SIG_SETMASK, &mask, nullptr); 650 pthread_t thread; 651 int rc = pthread_create(&thread, nullptr, 652 [](void*) -> void* { 653 sigset_t mask; 654 sigemptyset(&mask); 655 sigsuspend(&mask); 656 return nullptr; 657 }, 658 nullptr); 659 ASSERT_EQ(0, rc); 660 661 errno = 0; 662 ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval)); 663 ASSERT_EQ(0, errno); 664 pthread_join(thread, nullptr); 665 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); 666 } 667 668 TEST(signal, sigwaitinfo) { 669 SignalMaskRestorer smr; 670 671 // Block SIGALRM. 672 sigset_t just_SIGALRM; 673 sigemptyset(&just_SIGALRM); 674 sigaddset(&just_SIGALRM, SIGALRM); 675 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); 676 677 // Raise SIGALRM. 678 sigval_t sigval; 679 sigval.sival_int = 1; 680 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); 681 682 // Get pending SIGALRM. 683 siginfo_t info; 684 errno = 0; 685 ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info)); 686 ASSERT_EQ(0, errno); 687 ASSERT_EQ(SIGALRM, info.si_signo); 688 ASSERT_EQ(1, info.si_value.sival_int); 689 } 690 691 TEST(signal, sigwaitinfo64_SIGRTMIN) { 692 SignalMaskRestorer smr; 693 694 // Block SIGRTMIN. 695 sigset64_t just_SIGRTMIN; 696 sigemptyset64(&just_SIGRTMIN); 697 sigaddset64(&just_SIGRTMIN, SIGRTMIN); 698 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); 699 700 // Raise SIGRTMIN. 701 sigval_t sigval; 702 sigval.sival_int = 1; 703 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); 704 705 // Get pending SIGRTMIN. 706 siginfo_t info; 707 errno = 0; 708 ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info)); 709 ASSERT_EQ(0, errno); 710 ASSERT_EQ(SIGRTMIN, info.si_signo); 711 ASSERT_EQ(1, info.si_value.sival_int); 712 } 713 714 TEST(signal, sigtimedwait) { 715 SignalMaskRestorer smr; 716 717 // Block SIGALRM. 718 sigset_t just_SIGALRM; 719 sigemptyset(&just_SIGALRM); 720 sigaddset(&just_SIGALRM, SIGALRM); 721 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); 722 723 // Raise SIGALRM. 724 sigval_t sigval = { .sival_int = 1 }; 725 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); 726 727 // Get pending SIGALRM. 728 siginfo_t info; 729 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 }; 730 errno = 0; 731 ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout)); 732 ASSERT_EQ(0, errno); 733 } 734 735 TEST(signal, sigtimedwait64_SIGRTMIN) { 736 SignalMaskRestorer smr; 737 738 // Block SIGRTMIN. 739 sigset64_t just_SIGRTMIN; 740 sigemptyset64(&just_SIGRTMIN); 741 sigaddset64(&just_SIGRTMIN, SIGRTMIN); 742 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); 743 744 // Raise SIGALRM. 745 sigval_t sigval = { .sival_int = 1 }; 746 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); 747 748 // Get pending SIGALRM. 749 siginfo_t info; 750 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 }; 751 errno = 0; 752 ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout)); 753 ASSERT_EQ(0, errno); 754 } 755 756 static int64_t NanoTime() { 757 timespec t; 758 clock_gettime(CLOCK_MONOTONIC, &t); 759 return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec; 760 } 761 762 TEST(signal, sigtimedwait_timeout) { 763 // Block SIGALRM. 764 sigset_t just_SIGALRM; 765 sigemptyset(&just_SIGALRM); 766 sigaddset(&just_SIGALRM, SIGALRM); 767 sigset_t original_set; 768 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set)); 769 770 // Wait timeout. 771 int64_t start_time = NanoTime(); 772 siginfo_t info; 773 timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 }; 774 errno = 0; 775 ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout)); 776 ASSERT_EQ(EAGAIN, errno); 777 ASSERT_GE(NanoTime() - start_time, 1000000); 778 779 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr)); 780 } 781 782 #if defined(__BIONIC__) 783 TEST(signal, rt_tgsigqueueinfo) { 784 // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self. 785 // If this fails, your kernel needs commit 66dd34a to be backported. 786 static constexpr char error_msg[] = 787 "\nPlease ensure that the following kernel patch has been applied:\n" 788 "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n"; 789 static siginfo received; 790 791 struct sigaction handler = {}; 792 handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; }; 793 handler.sa_flags = SA_SIGINFO; 794 795 ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr)); 796 797 siginfo sent; 798 memset(&sent, 0, sizeof(sent)); 799 800 sent.si_code = SI_TKILL; 801 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent)) 802 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg; 803 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected " 804 << sent.si_code << ", received " << received.si_code 805 << error_msg; 806 807 sent.si_code = SI_USER; 808 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent)) 809 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg; 810 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected " 811 << sent.si_code << ", received " << received.si_code 812 << error_msg; 813 } 814 #endif 815 816 TEST(signal, sigset_size) { 817 // The setjmp implementations for ARM, AArch64, x86, and x86_64 assume that sigset_t can fit in a 818 // long. This is true because ARM and x86 have broken rt signal support, and AArch64 and x86_64 819 // both have a SIGRTMAX defined as 64. 820 #if defined(__arm__) || defined(__aarch64__) || defined(__i386__) || defined(__x86_64__) 821 #if defined(__BIONIC__) 822 static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long"); 823 #endif 824 static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals"); 825 #endif 826 } 827 828 TEST(signal, sigignore_EINVAL) { 829 errno = 0; 830 ASSERT_EQ(-1, sigignore(99999)); 831 ASSERT_EQ(EINVAL, errno); 832 } 833 834 TEST(signal, sigignore) { 835 errno = 0; 836 EXPECT_EQ(-1, sigignore(SIGKILL)); 837 EXPECT_EQ(errno, EINVAL); 838 839 errno = 0; 840 EXPECT_EQ(-1, sigignore(SIGSTOP)); 841 EXPECT_EQ(errno, EINVAL); 842 843 ScopedSignalHandler sigalrm{SIGALRM}; 844 ASSERT_EQ(0, sigignore(SIGALRM)); 845 846 struct sigaction sa; 847 ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa)); 848 EXPECT_EQ(SIG_IGN, sa.sa_handler); 849 } 850 851 TEST(signal, sighold_EINVAL) { 852 errno = 0; 853 ASSERT_EQ(-1, sighold(99999)); 854 ASSERT_EQ(EINVAL, errno); 855 } 856 857 TEST(signal, sigpause_EINVAL) { 858 errno = 0; 859 ASSERT_EQ(-1, sigpause(99999)); 860 ASSERT_EQ(EINVAL, errno); 861 } 862 863 TEST(signal, sigrelse_EINVAL) { 864 errno = 0; 865 ASSERT_EQ(-1, sigpause(99999)); 866 ASSERT_EQ(EINVAL, errno); 867 } 868 869 static void TestSigholdSigpauseSigrelse(int sig) { 870 static int signal_handler_call_count = 0; 871 ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }}; 872 SignalMaskRestorer mask_restorer; 873 sigset_t set; 874 875 // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ... 876 ASSERT_EQ(0, sighold(sig)); 877 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); 878 EXPECT_TRUE(sigismember(&set, sig)); 879 880 // ... preventing our SIGALRM/SIGRTMIN handler from running ... 881 raise(sig); 882 ASSERT_EQ(0, signal_handler_call_count); 883 // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it. 884 ASSERT_EQ(-1, sigpause(sig)); 885 ASSERT_EQ(EINTR, errno); 886 ASSERT_EQ(1, signal_handler_call_count); 887 888 if (sig >= SIGRTMIN && sizeof(void*) == 8) { 889 // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN. 890 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); 891 EXPECT_TRUE(sigismember(&set, sig)); 892 893 // Whereas sigrelse(SIGALRM/SIGRTMIN) should. 894 ASSERT_EQ(0, sigrelse(sig)); 895 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); 896 EXPECT_FALSE(sigismember(&set, sig)); 897 } else { 898 // sigismember won't work for SIGRTMIN on LP32. 899 } 900 } 901 902 TEST(signal, sighold_sigpause_sigrelse) { 903 TestSigholdSigpauseSigrelse(SIGALRM); 904 } 905 906 TEST(signal, sighold_sigpause_sigrelse_RT) { 907 TestSigholdSigpauseSigrelse(SIGRTMIN); 908 } 909 910 TEST(signal, sigset_EINVAL) { 911 errno = 0; 912 ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL)); 913 ASSERT_EQ(EINVAL, errno); 914 } 915 916 TEST(signal, sigset_RT) { 917 static int signal_handler_call_count = 0; 918 auto signal_handler = [](int) { signal_handler_call_count++; }; 919 ScopedSignalHandler ssh{SIGRTMIN, signal_handler}; 920 SignalMaskRestorer mask_restorer; 921 922 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD)); 923 #if defined(__LP64__) 924 sigset_t set; 925 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); 926 ASSERT_TRUE(sigismember(&set, SIGRTMIN)); 927 #endif 928 929 ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler)); 930 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler)); 931 ASSERT_EQ(0, signal_handler_call_count); 932 raise(SIGRTMIN); 933 ASSERT_EQ(1, signal_handler_call_count); 934 } 935 936 TEST(signal, sigset) { 937 static int signal_handler_call_count = 0; 938 auto signal_handler = [](int) { signal_handler_call_count++; }; 939 ScopedSignalHandler ssh{SIGALRM, signal_handler}; 940 SignalMaskRestorer mask_restorer; 941 942 ASSERT_EQ(0, signal_handler_call_count); 943 raise(SIGALRM); 944 ASSERT_EQ(1, signal_handler_call_count); 945 946 // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD. 947 sigset_t set; 948 sigemptyset(&set); 949 sigaddset(&set, SIGALRM); 950 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr)); 951 952 sigemptyset(&set); 953 ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler)); 954 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); 955 EXPECT_FALSE(sigismember(&set, SIGALRM)); 956 957 ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN)); 958 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); 959 EXPECT_FALSE(sigismember(&set, SIGALRM)); 960 961 ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL)); 962 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); 963 EXPECT_FALSE(sigismember(&set, SIGALRM)); 964 965 ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD)); 966 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); 967 EXPECT_TRUE(sigismember(&set, SIGALRM)); 968 } 969 970 TEST(signal, killpg_EINVAL) { 971 // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0 972 // and passes 0 through to kill(2). 973 errno = 0; 974 ASSERT_EQ(-1, killpg(-1, SIGKILL)); 975 ASSERT_EQ(EINVAL, errno); 976 } 977