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