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