work around "signal loses thread pointer" issue with "approach 2"
[musl] / src / signal / sigaction.c
1 #include <stdlib.h>
2 #include <signal.h>
3 #include <errno.h>
4 #include "syscall.h"
5 #include "pthread_impl.h"
6 #include "libc.h"
7
8 void __restore(), __restore_rt();
9
10 static pthread_t dummy(void) { return 0; }
11 weak_alias(dummy, __pthread_self_def);
12
13 int __libc_sigaction(int sig, const struct sigaction *sa, struct sigaction *old)
14 {
15         struct {
16                 void *handler;
17                 unsigned long flags;
18                 void (*restorer)(void);
19                 sigset_t mask;
20         } ksa, kold;
21         long pksa=0, pkold=0;
22         if (sa) {
23                 ksa.handler = sa->sa_handler;
24                 ksa.flags = sa->sa_flags | SA_RESTORER;
25                 ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore;
26                 ksa.mask = sa->sa_mask;
27                 pksa = (long)&ksa;
28         }
29         if (old) pkold = (long)&kold;
30         __pthread_self_def();
31         if (syscall(SYS_rt_sigaction, sig, pksa, pkold, 8))
32                 return -1;
33         if (old) {
34                 old->sa_handler = kold.handler;
35                 old->sa_flags = kold.flags;
36                 old->sa_mask = kold.mask;
37         }
38         return 0;
39 }
40
41 int __sigaction(int sig, const struct sigaction *sa, struct sigaction *old)
42 {
43         if (sig-32U < 3) {
44                 errno = EINVAL;
45                 return -1;
46         }
47         return __libc_sigaction(sig, sa, old);
48 }
49
50 weak_alias(__sigaction, sigaction);