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 "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" |
15 | |
16 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
17 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
18 | |
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)); |
22 | } |
23 | |
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), |
29 | Fails(EINVAL)); |
30 | } |
31 | |
32 | static int sigusr1Count; |
33 | static bool correctSignal; |
34 | |
35 | TEST(LlvmLibcSigaction, CustomAction) { |
36 | // Zero this incase tests get run multiple times in the future. |
37 | sigusr1Count = 0; |
38 | |
39 | struct sigaction action; |
40 | EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1, nullptr, &action), Succeeds()); |
41 | |
42 | action.sa_handler = +[](int signal) { |
43 | correctSignal = signal == SIGUSR1; |
44 | sigusr1Count++; |
45 | }; |
46 | EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1, &action, nullptr), Succeeds()); |
47 | |
48 | LIBC_NAMESPACE::raise(SIGUSR1); |
49 | EXPECT_EQ(sigusr1Count, 1); |
50 | EXPECT_TRUE(correctSignal); |
51 | |
52 | action.sa_handler = SIG_DFL; |
53 | EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1, &action, nullptr), Succeeds()); |
54 | |
55 | EXPECT_DEATH([] { LIBC_NAMESPACE::raise(SIGUSR1); }, WITH_SIGNAL(SIGUSR1)); |
56 | } |
57 | |
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()); |
63 | |
64 | EXPECT_EXITS([] { LIBC_NAMESPACE::raise(SIGUSR1); }, 0); |
65 | } |
66 | |