5499bd181c37c06978f4c0a55d9571078b9faffd
[musl] / src / signal / sigaction.c
1 #include <stdlib.h>
2 #include <signal.h>
3 #include <errno.h>
4 #include <string.h>
5 #include "syscall.h"
6 #include "pthread_impl.h"
7 #include "libc.h"
8 #include "ksigaction.h"
9
10 void __restore(), __restore_rt();
11
12 static pthread_t dummy(void) { return 0; }
13 weak_alias(dummy, __pthread_self_def);
14
15 static unsigned long handler_set[_NSIG/(8*sizeof(long))];
16
17 void __get_handler_set(sigset_t *set)
18 {
19         memcpy(set, handler_set, sizeof handler_set);
20 }
21
22 int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
23 {
24         struct k_sigaction ksa, ksa_old;
25         if (sig >= (unsigned)_NSIG) {
26                 errno = EINVAL;
27                 return -1;
28         }
29         if (sa) {
30                 if ((uintptr_t)sa->sa_handler > 1UL) {
31                         a_or_l(handler_set+(sig-1)/(8*sizeof(long)),
32                                 1UL<<(sig-1)%(8*sizeof(long)));
33                         __pthread_self_def();
34                 }
35                 ksa.handler = sa->sa_handler;
36                 ksa.flags = sa->sa_flags | SA_RESTORER;
37                 ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore;
38                 memcpy(&ksa.mask, &sa->sa_mask, sizeof ksa.mask);
39         }
40         if (syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, sizeof ksa.mask))
41                 return -1;
42         if (old) {
43                 old->sa_handler = ksa_old.handler;
44                 old->sa_flags = ksa_old.flags;
45                 memcpy(&old->sa_mask, &ksa_old.mask, sizeof ksa_old.mask);
46         }
47         return 0;
48 }
49
50 int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
51 {
52         if (sig-32U < 3) {
53                 errno = EINVAL;
54                 return -1;
55         }
56         return __libc_sigaction(sig, sa, old);
57 }
58
59 weak_alias(__sigaction, sigaction);