113 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_UTILS_H10#define LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_UTILS_H11 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#include "src/__support/macros/config.h"16 17#include <signal.h> // sigaction18#include <stddef.h>19#include <sys/syscall.h> // For syscall numbers.20 21namespace LIBC_NAMESPACE_DECL {22 23// The POSIX definition of struct sigaction and the sigaction data structure24// expected by the rt_sigaction syscall differ in their definition. So, we25// define the equivalent of the what the kernel expects to help with making26// the rt_sigaction syscall.27//28// NOTE: Though the kernel definition does not have a union to include the29// handler taking siginfo_t * argument, one can set sa_handler to sa_sigaction30// if SA_SIGINFO is set in sa_flags.31struct KernelSigaction {32 LIBC_INLINE KernelSigaction &operator=(const struct sigaction &sa) {33 sa_flags = sa.sa_flags;34 sa_restorer = sa.sa_restorer;35 sa_mask = sa.sa_mask;36 if (sa_flags & SA_SIGINFO) {37 sa_sigaction = sa.sa_sigaction;38 } else {39 sa_handler = sa.sa_handler;40 }41 return *this;42 }43 44 LIBC_INLINE operator struct sigaction() const {45 struct sigaction sa;46 sa.sa_flags = static_cast<int>(sa_flags);47 sa.sa_mask = sa_mask;48 sa.sa_restorer = sa_restorer;49 if (sa_flags & SA_SIGINFO)50 sa.sa_sigaction = sa_sigaction;51 else52 sa.sa_handler = sa_handler;53 return sa;54 }55 56 union {57 void (*sa_handler)(int);58 void (*sa_sigaction)(int, siginfo_t *, void *);59 };60 unsigned long sa_flags;61 void (*sa_restorer)(void);62 // Our public definition of sigset_t matches that of the kernel's definition.63 // So, we can use the public sigset_t type here.64 sigset_t sa_mask;65};66 67static constexpr size_t BITS_PER_SIGWORD = sizeof(unsigned long) * 8;68 69LIBC_INLINE constexpr sigset_t full_set() { return sigset_t{{-1UL}}; }70 71LIBC_INLINE constexpr sigset_t empty_set() { return sigset_t{{0}}; }72 73// Set the bit corresponding to |signal| in |set|. Return true on success74// and false on failure. The function will fail if |signal| is greater than75// NSIG or negative.76LIBC_INLINE constexpr bool add_signal(sigset_t &set, int signal) {77 if (signal > NSIG || signal <= 0)78 return false;79 size_t n = size_t(signal) - 1;80 size_t word = n / BITS_PER_SIGWORD;81 size_t bit = n % BITS_PER_SIGWORD;82 set.__signals[word] |= (1UL << bit);83 return true;84}85 86// Reset the bit corresponding to |signal| in |set|. Return true on success87// and false on failure. The function will fail if |signal| is greater than88// NSIG or negative.89LIBC_INLINE constexpr bool delete_signal(sigset_t &set, int signal) {90 if (signal > NSIG || signal <= 0)91 return false;92 size_t n = size_t(signal) - 1;93 size_t word = n / BITS_PER_SIGWORD;94 size_t bit = n % BITS_PER_SIGWORD;95 set.__signals[word] &= ~(1UL << bit);96 return true;97}98 99LIBC_INLINE int block_all_signals(sigset_t &set) {100 sigset_t full = full_set();101 return LIBC_NAMESPACE::syscall_impl<int>(SYS_rt_sigprocmask, SIG_BLOCK, &full,102 &set, sizeof(sigset_t));103}104 105LIBC_INLINE int restore_signals(const sigset_t &set) {106 return LIBC_NAMESPACE::syscall_impl<int>(SYS_rt_sigprocmask, SIG_SETMASK,107 &set, nullptr, sizeof(sigset_t));108}109 110} // namespace LIBC_NAMESPACE_DECL111 112#endif // LLVM_LIBC_SRC_SIGNAL_LINUX_SIGNAL_UTILS_H113