xref: /aosp_15_r20/external/llvm-libc/src/signal/linux/signal.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of signal ------------------------------------===//
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/signal.h"
10 #include "hdr/signal_macros.h"
11 #include "hdr/types/sighandler_t.h"
12 #include "src/__support/common.h"
13 #include "src/__support/macros/config.h"
14 #include "src/signal/sigaction.h"
15 
16 namespace LIBC_NAMESPACE_DECL {
17 
18 LLVM_LIBC_FUNCTION(sighandler_t, signal, (int signum, sighandler_t handler)) {
19   struct sigaction action, old;
20   action.sa_handler = handler;
21   action.sa_flags = SA_RESTART;
22   // Errno will already be set so no need to worry about changing errno here.
23   return LIBC_NAMESPACE::sigaction(signum, &action, &old) == -1
24              ? SIG_ERR
25              : old.sa_handler;
26 }
27 
28 } // namespace LIBC_NAMESPACE_DECL
29