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 "hdr/errno_macros.h"
10 #include "hdr/signal_macros.h"
11 #include "src/signal/raise.h"
12 #include "src/signal/sigaction.h"
13 #include "test/UnitTest/ErrnoSetterMatcher.h"
14 #include "test/UnitTest/Test.h"
16 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
17 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
19 TEST(LlvmLibcSigaction
, Invalid
) {
20 // -1 is a much larger signal that NSIG, so this should fail.
21 EXPECT_THAT(LIBC_NAMESPACE::sigaction(-1, nullptr, nullptr), Fails(EINVAL
));
24 // SIGKILL cannot have its action changed, but it can be examined.
25 TEST(LlvmLibcSigaction
, Sigkill
) {
26 struct sigaction action
;
27 EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGKILL
, nullptr, &action
), Succeeds());
28 EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGKILL
, &action
, nullptr),
32 static int sigusr1Count
;
33 static bool correctSignal
;
35 TEST(LlvmLibcSigaction
, CustomAction
) {
36 // Zero this incase tests get run multiple times in the future.
39 struct sigaction action
;
40 EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1
, nullptr, &action
), Succeeds());
42 action
.sa_handler
= +[](int signal
) {
43 correctSignal
= signal
== SIGUSR1
;
46 EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1
, &action
, nullptr), Succeeds());
48 LIBC_NAMESPACE::raise(SIGUSR1
);
49 EXPECT_EQ(sigusr1Count
, 1);
50 EXPECT_TRUE(correctSignal
);
52 action
.sa_handler
= SIG_DFL
;
53 EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1
, &action
, nullptr), Succeeds());
55 EXPECT_DEATH([] { LIBC_NAMESPACE::raise(SIGUSR1
); }, WITH_SIGNAL(SIGUSR1
));
58 TEST(LlvmLibcSigaction
, Ignore
) {
59 struct sigaction action
;
60 EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1
, nullptr, &action
), Succeeds());
61 action
.sa_handler
= SIG_IGN
;
62 EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1
, &action
, nullptr), Succeeds());
64 EXPECT_EXITS([] { LIBC_NAMESPACE::raise(SIGUSR1
); }, 0);