1 //===-- Unittests for sigaction -------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "include/errno.h" 10 #define __LLVM_LIBC_INTERNAL_SIGACTION 11 #include "include/signal.h" 12 #include "src/signal/raise.h" 13 #include "src/signal/sigaction.h" 14 15 #include "test/ErrnoSetterMatcher.h" 16 #include "utils/UnitTest/Test.h" 17 18 using __llvm_libc::testing::ErrnoSetterMatcher::Fails; 19 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; 20 21 TEST(LlvmLibcSigaction, Invalid) { 22 // -1 is a much larger signal that NSIG, so this should fail. 23 EXPECT_THAT(__llvm_libc::sigaction(-1, nullptr, nullptr), Fails(EINVAL)); 24 } 25 26 // SIGKILL cannot have its action changed, but it can be examined. 27 TEST(LlvmLibcSigaction, Sigkill) { 28 struct __sigaction action; 29 EXPECT_THAT(__llvm_libc::sigaction(SIGKILL, nullptr, &action), Succeeds()); 30 EXPECT_THAT(__llvm_libc::sigaction(SIGKILL, &action, nullptr), Fails(EINVAL)); 31 } 32 33 static int sigusr1Count; 34 static bool correctSignal; 35 36 TEST(LlvmLibcSigaction, CustomAction) { 37 // Zero this incase tests get run multiple times in the future. 38 sigusr1Count = 0; 39 40 struct __sigaction action; 41 EXPECT_THAT(__llvm_libc::sigaction(SIGUSR1, nullptr, &action), Succeeds()); 42 43 action.sa_handler = +[](int signal) { 44 correctSignal = signal == SIGUSR1; 45 sigusr1Count++; 46 }; 47 EXPECT_THAT(__llvm_libc::sigaction(SIGUSR1, &action, nullptr), Succeeds()); 48 49 __llvm_libc::raise(SIGUSR1); 50 EXPECT_EQ(sigusr1Count, 1); 51 EXPECT_TRUE(correctSignal); 52 53 action.sa_handler = SIG_DFL; 54 EXPECT_THAT(__llvm_libc::sigaction(SIGUSR1, &action, nullptr), Succeeds()); 55 56 EXPECT_DEATH([] { __llvm_libc::raise(SIGUSR1); }, WITH_SIGNAL(SIGUSR1)); 57 } 58 59 TEST(LlvmLibcSigaction, Ignore) { 60 struct __sigaction action; 61 EXPECT_THAT(__llvm_libc::sigaction(SIGUSR1, nullptr, &action), Succeeds()); 62 action.sa_handler = SIG_IGN; 63 EXPECT_THAT(__llvm_libc::sigaction(SIGUSR1, &action, nullptr), Succeeds()); 64 65 EXPECT_EXITS([] { __llvm_libc::raise(SIGUSR1); }, 0); 66 } 67