block all signals (even internal ones) in cancellation signal handler
[musl] / src / thread / pthread_cancel.c
1 #include <string.h>
2 #include "pthread_impl.h"
3 #include "syscall.h"
4 #include "libc.h"
5
6 long __cancel()
7 {
8         pthread_t self = __pthread_self();
9         if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
10                 pthread_exit(PTHREAD_CANCELED);
11         self->canceldisable = PTHREAD_CANCEL_DISABLE;
12         return -ECANCELED;
13 }
14
15 /* If __syscall_cp_asm has adjusted the stack pointer, it must provide a
16  * definition of __cp_cancel to undo those adjustments and call __cancel.
17  * Otherwise, __cancel provides a definition for __cp_cancel. */
18
19 weak_alias(__cancel, __cp_cancel);
20
21 long __syscall_cp_asm(volatile void *, syscall_arg_t,
22                       syscall_arg_t, syscall_arg_t, syscall_arg_t,
23                       syscall_arg_t, syscall_arg_t, syscall_arg_t);
24
25 long __syscall_cp_c(syscall_arg_t nr,
26                     syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
27                     syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
28 {
29         pthread_t self;
30         long r;
31         int st;
32
33         if (!libc.has_thread_pointer || (st=(self=__pthread_self())->canceldisable)
34             && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
35                 return __syscall(nr, u, v, w, x, y, z);
36
37         r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
38         if (r==-EINTR && nr!=SYS_close && self->cancel &&
39             self->canceldisable != PTHREAD_CANCEL_DISABLE)
40                 r = __cancel();
41         return r;
42 }
43
44 static void _sigaddset(sigset_t *set, int sig)
45 {
46         unsigned s = sig-1;
47         set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
48 }
49
50 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
51 {
52         pthread_t self = __pthread_self();
53         ucontext_t *uc = ctx;
54         const char *ip = ((char **)&uc->uc_mcontext)[CANCEL_REG_IP];
55         extern const char __cp_begin[1], __cp_end[1];
56
57         a_barrier();
58         if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
59
60         _sigaddset(&uc->uc_sigmask, SIGCANCEL);
61
62         if (self->cancelasync || ip >= __cp_begin && ip < __cp_end) {
63                 ((char **)&uc->uc_mcontext)[CANCEL_REG_IP] = (char *)__cp_cancel;
64                 return;
65         }
66
67         __syscall(SYS_tkill, self->tid, SIGCANCEL);
68 }
69
70 void __testcancel()
71 {
72         if (!libc.has_thread_pointer) return;
73         pthread_t self = __pthread_self();
74         if (self->cancel && !self->canceldisable)
75                 __cancel();
76 }
77
78 static void init_cancellation()
79 {
80         struct sigaction sa = {
81                 .sa_flags = SA_SIGINFO | SA_RESTART,
82                 .sa_sigaction = cancel_handler
83         };
84         memset(&sa.sa_mask, -1, _NSIG/8);
85         __libc_sigaction(SIGCANCEL, &sa, 0);
86 }
87
88 int pthread_cancel(pthread_t t)
89 {
90         static int init;
91         if (!init) {
92                 init_cancellation();
93                 init = 1;
94         }
95         a_store(&t->cancel, 1);
96         return pthread_kill(t, SIGCANCEL);
97 }