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_H
10 #define LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_H
11
12 #include "config/linux/syscall.h" // For internal syscall function.
13 #include "include/sys/syscall.h" // For syscall numbers.
14
15 #include "include/signal.h"
16
17 static_assert(sizeof(sigset_t) * 8 >= NSIG, "sigset_t cannot hold all signals");
18
19 namespace __llvm_libc {
20
21 // Using this internally defined type will make it easier in the future to port
22 // to different architectures.
23 struct Sigset {
24 sigset_t nativeSigset;
25
fullsetSigset26 constexpr static Sigset fullset() { return {-1UL}; }
emptySetSigset27 constexpr static Sigset emptySet() { return {0}; }
28
addsetSigset29 constexpr void addset(int signal) { nativeSigset |= (1L << (signal - 1)); }
30
delsetSigset31 constexpr void delset(int signal) { nativeSigset &= ~(1L << (signal - 1)); }
32
sigset_tSigset33 operator sigset_t() const { return nativeSigset; }
34 };
35
36 constexpr static Sigset all = Sigset::fullset();
37
block_all_signals(Sigset & set)38 static inline int block_all_signals(Sigset &set) {
39 sigset_t nativeSigset = all;
40 sigset_t oldSet = set;
41 int ret = __llvm_libc::syscall(SYS_rt_sigprocmask, SIG_BLOCK, &nativeSigset,
42 &oldSet, sizeof(sigset_t));
43 set = {oldSet};
44 return ret;
45 }
46
restore_signals(const Sigset & set)47 static inline int restore_signals(const Sigset &set) {
48 sigset_t nativeSigset = set;
49 return __llvm_libc::syscall(SYS_rt_sigprocmask, SIG_SETMASK, &nativeSigset,
50 nullptr, sizeof(sigset_t));
51 }
52
53 } // namespace __llvm_libc
54
55 #endif // LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_H
56