signal.h
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//===-- Internal header for Linux signals -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_H
#define LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_H
#include "config/linux/syscall.h" // For internal syscall function.
#include "include/sys/syscall.h" // For syscall numbers.
#include "include/signal.h"
static_assert(sizeof(sigset_t) * 8 >= NSIG, "sigset_t cannot hold all signals");
namespace __llvm_libc {
// Using this internally defined type will make it easier in the future to port
// to different architectures.
struct Sigset {
sigset_t nativeSigset;
constexpr static Sigset fullset() { return {-1UL}; }
constexpr static Sigset emptySet() { return {0}; }
constexpr void addset(int signal) { nativeSigset |= (1L << (signal - 1)); }
constexpr void delset(int signal) { nativeSigset &= ~(1L << (signal - 1)); }
operator sigset_t() const { return nativeSigset; }
};
constexpr static Sigset all = Sigset::fullset();
static inline int block_all_signals(Sigset &set) {
sigset_t nativeSigset = all;
sigset_t oldSet = set;
int ret = __llvm_libc::syscall(SYS_rt_sigprocmask, SIG_BLOCK, &nativeSigset,
&oldSet, sizeof(sigset_t));
set = {oldSet};
return ret;
}
static inline int restore_signals(const Sigset &set) {
sigset_t nativeSigset = set;
return __llvm_libc::syscall(SYS_rt_sigprocmask, SIG_SETMASK, &nativeSigset,
nullptr, sizeof(sigset_t));
}
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_H