1//===-- Internal header for Linux signals -----------------------*- C++ -*-===//
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#ifndef LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_UTILS_H
10#define LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_UTILS_H
11
12#include "hdr/types/sigset_t.h"
13#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
14#include "src/__support/common.h"
15
16#include <signal.h> // sigaction
17#include <stddef.h>
18#include <sys/syscall.h> // For syscall numbers.
19
20namespace LIBC_NAMESPACE {
21
22// The POSIX definition of struct sigaction and the sigaction data structure
23// expected by the rt_sigaction syscall differ in their definition. So, we
24// define the equivalent of the what the kernel expects to help with making
25// the rt_sigaction syscall.
26//
27// NOTE: Though the kernel definition does not have a union to include the
28// handler taking siginfo_t * argument, one can set sa_handler to sa_sigaction
29// if SA_SIGINFO is set in sa_flags.
30struct KernelSigaction {
31 LIBC_INLINE KernelSigaction &operator=(const struct sigaction &sa) {
32 sa_flags = sa.sa_flags;
33 sa_restorer = sa.sa_restorer;
34 sa_mask = sa.sa_mask;
35 if (sa_flags & SA_SIGINFO) {
36 sa_sigaction = sa.sa_sigaction;
37 } else {
38 sa_handler = sa.sa_handler;
39 }
40 return *this;
41 }
42
43 LIBC_INLINE operator struct sigaction() const {
44 struct sigaction sa;
45 sa.sa_flags = static_cast<int>(sa_flags);
46 sa.sa_mask = sa_mask;
47 sa.sa_restorer = sa_restorer;
48 if (sa_flags & SA_SIGINFO)
49 sa.sa_sigaction = sa_sigaction;
50 else
51 sa.sa_handler = sa_handler;
52 return sa;
53 }
54
55 union {
56 void (*sa_handler)(int);
57 void (*sa_sigaction)(int, siginfo_t *, void *);
58 };
59 unsigned long sa_flags;
60 void (*sa_restorer)(void);
61 // Our public definition of sigset_t matches that of the kernel's definition.
62 // So, we can use the public sigset_t type here.
63 sigset_t sa_mask;
64};
65
66static constexpr size_t BITS_PER_SIGWORD = sizeof(unsigned long) * 8;
67
68LIBC_INLINE constexpr sigset_t full_set() { return sigset_t{{-1UL}}; }
69
70LIBC_INLINE constexpr sigset_t empty_set() { return sigset_t{{0}}; }
71
72// Set the bit corresponding to |signal| in |set|. Return true on success
73// and false on failure. The function will fail if |signal| is greater than
74// NSIG or negative.
75LIBC_INLINE constexpr bool add_signal(sigset_t &set, int signal) {
76 if (signal > NSIG || signal <= 0)
77 return false;
78 size_t n = size_t(signal) - 1;
79 size_t word = n / BITS_PER_SIGWORD;
80 size_t bit = n % BITS_PER_SIGWORD;
81 set.__signals[word] |= (1UL << bit);
82 return true;
83}
84
85// Reset the bit corresponding to |signal| in |set|. Return true on success
86// and false on failure. The function will fail if |signal| is greater than
87// NSIG or negative.
88LIBC_INLINE constexpr bool delete_signal(sigset_t &set, int signal) {
89 if (signal > NSIG || signal <= 0)
90 return false;
91 size_t n = size_t(signal) - 1;
92 size_t word = n / BITS_PER_SIGWORD;
93 size_t bit = n % BITS_PER_SIGWORD;
94 set.__signals[word] &= ~(1UL << bit);
95 return true;
96}
97
98LIBC_INLINE int block_all_signals(sigset_t &set) {
99 sigset_t full = full_set();
100 return LIBC_NAMESPACE::syscall_impl<int>(SYS_rt_sigprocmask, SIG_BLOCK, ts: &full,
101 ts: &set, ts: sizeof(sigset_t));
102}
103
104LIBC_INLINE int restore_signals(const sigset_t &set) {
105 return LIBC_NAMESPACE::syscall_impl<int>(SYS_rt_sigprocmask, SIG_SETMASK,
106 ts: &set, ts: nullptr, ts: sizeof(sigset_t));
107}
108
109} // namespace LIBC_NAMESPACE
110
111#endif // LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_UTILS_H
112

source code of libc/src/signal/linux/signal_utils.h