1 //===-- Unittests for sigaction -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/signal/raise.h"
10 #include "src/signal/sigaction.h"
12 #include "test/ErrnoSetterMatcher.h"
13 #include "test/UnitTest/Test.h"
18 using __llvm_libc::testing::ErrnoSetterMatcher::Fails
;
19 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds
;
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
));
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
));
33 static int sigusr1Count
;
34 static bool correctSignal
;
36 TEST(LlvmLibcSigaction
, CustomAction
) {
37 // Zero this incase tests get run multiple times in the future.
40 struct sigaction action
;
41 EXPECT_THAT(__llvm_libc::sigaction(SIGUSR1
, nullptr, &action
), Succeeds());
43 action
.sa_handler
= +[](int signal
) {
44 correctSignal
= signal
== SIGUSR1
;
47 EXPECT_THAT(__llvm_libc::sigaction(SIGUSR1
, &action
, nullptr), Succeeds());
49 __llvm_libc::raise(SIGUSR1
);
50 EXPECT_EQ(sigusr1Count
, 1);
51 EXPECT_TRUE(correctSignal
);
53 action
.sa_handler
= SIG_DFL
;
54 EXPECT_THAT(__llvm_libc::sigaction(SIGUSR1
, &action
, nullptr), Succeeds());
56 EXPECT_DEATH([] { __llvm_libc::raise(SIGUSR1
); }, WITH_SIGNAL(SIGUSR1
));
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());
65 EXPECT_EXITS([] { __llvm_libc::raise(SIGUSR1
); }, 0);