sigaction.cpp
1.65 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
56
//===-- Linux implementation of sigaction ---------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#define __LLVM_LIBC_INTERNAL_SIGACTION
#include "src/signal/sigaction.h"
#include "src/errno/llvmlibc_errno.h"
#include "src/signal/linux/signal.h"
#include "src/__support/common.h"
namespace __llvm_libc {
// TOOD: Some architectures will have their signal trampoline functions in the
// vdso, use those when available.
extern "C" void __restore_rt();
template <typename T, typename V>
static void copySigaction(T &dest, const V &source) {
dest.sa_handler = source.sa_handler;
dest.sa_mask = source.sa_mask;
dest.sa_flags = source.sa_flags;
dest.sa_restorer = source.sa_restorer;
}
int LLVM_LIBC_ENTRYPOINT(sigaction)(
int signal, const struct __sigaction *__restrict libc_new,
struct __sigaction *__restrict libc_old) {
struct sigaction kernel_new;
if (libc_new) {
copySigaction(kernel_new, *libc_new);
if (!(kernel_new.sa_flags & SA_RESTORER)) {
kernel_new.sa_flags |= SA_RESTORER;
kernel_new.sa_restorer = __restore_rt;
}
}
struct sigaction kernel_old;
int ret = syscall(SYS_rt_sigaction, signal, libc_new ? &kernel_new : nullptr,
libc_old ? &kernel_old : nullptr, sizeof(sigset_t));
if (ret) {
llvmlibc_errno = -ret;
return -1;
}
if (libc_old)
copySigaction(*libc_old, kernel_old);
return 0;
}
} // namespace __llvm_libc