1//===-- Unittests for sigprocmask -----------------------------------------===//
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 "src/errno/libc_errno.h"
10#include "src/signal/raise.h"
11#include "src/signal/sigaddset.h"
12#include "src/signal/sigemptyset.h"
13#include "src/signal/sigprocmask.h"
14
15#include "test/UnitTest/ErrnoSetterMatcher.h"
16#include "test/UnitTest/Test.h"
17
18#include <signal.h>
19
20class LlvmLibcSignalTest : public LIBC_NAMESPACE::testing::Test {
21 sigset_t oldSet;
22
23public:
24 void SetUp() override { LIBC_NAMESPACE::sigprocmask(how: 0, set: nullptr, oldset: &oldSet); }
25
26 void TearDown() override {
27 LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, set: &oldSet, oldset: nullptr);
28 }
29};
30
31using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
32using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
33
34// This tests for invalid input.
35TEST_F(LlvmLibcSignalTest, SigprocmaskInvalid) {
36 LIBC_NAMESPACE::libc_errno = 0;
37
38 sigset_t valid;
39 // 17 and -4 are out of the range for sigprocmask's how paramater.
40 EXPECT_THAT(LIBC_NAMESPACE::sigprocmask(17, &valid, nullptr), Fails(EINVAL));
41 EXPECT_THAT(LIBC_NAMESPACE::sigprocmask(-4, &valid, nullptr), Fails(EINVAL));
42
43 // This pointer is out of this processes address range.
44 sigset_t *invalid = reinterpret_cast<sigset_t *>(-1);
45 EXPECT_THAT(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, invalid, nullptr),
46 Fails(EFAULT));
47 EXPECT_THAT(LIBC_NAMESPACE::sigprocmask(-4, nullptr, invalid), Fails(EFAULT));
48}
49
50// This tests that when nothing is blocked, a process gets killed and alse tests
51// that when signals are blocked they are not delivered to the process.
52TEST_F(LlvmLibcSignalTest, BlockUnblock) {
53 sigset_t sigset;
54 EXPECT_EQ(LIBC_NAMESPACE::sigemptyset(&sigset), 0);
55 EXPECT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, &sigset, nullptr), 0);
56 EXPECT_DEATH([] { LIBC_NAMESPACE::raise(SIGUSR1); }, WITH_SIGNAL(SIGUSR1));
57 EXPECT_EQ(LIBC_NAMESPACE::sigaddset(&sigset, SIGUSR1), 0);
58 EXPECT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, &sigset, nullptr), 0);
59 EXPECT_EXITS([] { LIBC_NAMESPACE::raise(SIGUSR1); }, 0);
60}
61

source code of libc/test/src/signal/sigprocmask_test.cpp