1//===-- Linux implementation of sigaction ---------------------------------===//
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/signal/sigaction.h"
10
11#include "hdr/types/sigset_t.h"
12#include "src/__support/common.h"
13#include "src/errno/libc_errno.h"
14#include "src/signal/linux/signal_utils.h"
15
16namespace LIBC_NAMESPACE {
17
18// TOOD: Some architectures will have their signal trampoline functions in the
19// vdso, use those when available.
20
21extern "C" void __restore_rt();
22
23LLVM_LIBC_FUNCTION(int, sigaction,
24 (int signal, const struct sigaction *__restrict libc_new,
25 struct sigaction *__restrict libc_old)) {
26 KernelSigaction kernel_new;
27 if (libc_new) {
28 kernel_new = *libc_new;
29 if (!(kernel_new.sa_flags & SA_RESTORER)) {
30 kernel_new.sa_flags |= SA_RESTORER;
31 kernel_new.sa_restorer = __restore_rt;
32 }
33 }
34
35 KernelSigaction kernel_old;
36 int ret = LIBC_NAMESPACE::syscall_impl<int>(
37 SYS_rt_sigaction, ts: signal, ts: libc_new ? &kernel_new : nullptr,
38 ts: libc_old ? &kernel_old : nullptr, ts: sizeof(sigset_t));
39 if (ret) {
40 libc_errno = -ret;
41 return -1;
42 }
43
44 if (libc_old)
45 *libc_old = kernel_old;
46 return 0;
47}
48
49} // namespace LIBC_NAMESPACE
50

source code of libc/src/signal/linux/sigaction.cpp