move __abort_lock to its own file and drop pointless weak_alias trick
[musl] / src / signal / sigaction.c
1 #include <signal.h>
2 #include <errno.h>
3 #include <string.h>
4 #include "syscall.h"
5 #include "pthread_impl.h"
6 #include "libc.h"
7 #include "lock.h"
8 #include "ksigaction.h"
9
10 static int unmask_done;
11 static unsigned long handler_set[_NSIG/(8*sizeof(long))];
12
13 void __get_handler_set(sigset_t *set)
14 {
15         memcpy(set, handler_set, sizeof handler_set);
16 }
17
18 volatile int __eintr_valid_flag;
19
20 int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
21 {
22         struct k_sigaction ksa, ksa_old;
23         unsigned long set[_NSIG/(8*sizeof(long))];
24         if (sa) {
25                 if ((uintptr_t)sa->sa_handler > 1UL) {
26                         a_or_l(handler_set+(sig-1)/(8*sizeof(long)),
27                                 1UL<<(sig-1)%(8*sizeof(long)));
28
29                         /* If pthread_create has not yet been called,
30                          * implementation-internal signals might not
31                          * yet have been unblocked. They must be
32                          * unblocked before any signal handler is
33                          * installed, so that an application cannot
34                          * receive an illegal sigset_t (with them
35                          * blocked) as part of the ucontext_t passed
36                          * to the signal handler. */
37                         if (!libc.threaded && !unmask_done) {
38                                 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
39                                         SIGPT_SET, 0, _NSIG/8);
40                                 unmask_done = 1;
41                         }
42
43                         if (!(sa->sa_flags & SA_RESTART)) {
44                                 a_store(&__eintr_valid_flag, 1);
45                         }
46                 }
47                 /* Changing the disposition of SIGABRT to anything but
48                  * SIG_DFL requires a lock, so that it cannot be changed
49                  * while abort is terminating the process after simply
50                  * calling raise(SIGABRT) failed to do so. */
51                 if (sa->sa_handler != SIG_DFL && sig == SIGABRT) {
52                         __block_all_sigs(&set);
53                         LOCK(__abort_lock);
54                 }
55                 ksa.handler = sa->sa_handler;
56                 ksa.flags = sa->sa_flags | SA_RESTORER;
57                 ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore;
58                 memcpy(&ksa.mask, &sa->sa_mask, _NSIG/8);
59         }
60         int r = __syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, _NSIG/8);
61         if (sig == SIGABRT && sa && sa->sa_handler != SIG_DFL) {
62                 UNLOCK(__abort_lock);
63                 __restore_sigs(&set);
64         }
65         if (old && !r) {
66                 old->sa_handler = ksa_old.handler;
67                 old->sa_flags = ksa_old.flags;
68                 memcpy(&old->sa_mask, &ksa_old.mask, _NSIG/8);
69         }
70         return __syscall_ret(r);
71 }
72
73 int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
74 {
75         if (sig-32U < 3 || sig-1U >= _NSIG-1) {
76                 errno = EINVAL;
77                 return -1;
78         }
79         return __libc_sigaction(sig, sa, old);
80 }
81
82 weak_alias(__sigaction, sigaction);