6eaf72c45c90e82b9e4bbe5bc2314111d39bc977
[musl] / src / thread / pthread_cancel.c
1 #define _GNU_SOURCE
2 #include <string.h>
3 #include "pthread_impl.h"
4 #include "syscall.h"
5 #include "libc.h"
6
7 #ifdef SHARED
8 __attribute__((__visibility__("hidden")))
9 #endif
10 long __cancel(), __cp_cancel(), __syscall_cp_asm(), __syscall_cp_c();
11
12 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 weak_alias(__cancel, __cp_cancel);
26
27 long __syscall_cp_asm(volatile void *, syscall_arg_t,
28                       syscall_arg_t, syscall_arg_t, syscall_arg_t,
29                       syscall_arg_t, syscall_arg_t, syscall_arg_t);
30
31 long __syscall_cp_c(syscall_arg_t nr,
32                     syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
33                     syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
34 {
35         pthread_t self;
36         long r;
37         int st;
38
39         if ((st=(self=__pthread_self())->canceldisable)
40             && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
41                 return __syscall(nr, u, v, w, x, y, z);
42
43         r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
44         if (r==-EINTR && nr!=SYS_close && self->cancel &&
45             self->canceldisable != PTHREAD_CANCEL_DISABLE)
46                 r = __cancel();
47         return r;
48 }
49
50 static void _sigaddset(sigset_t *set, int sig)
51 {
52         unsigned s = sig-1;
53         set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
54 }
55
56 #ifdef SHARED
57 __attribute__((__visibility__("hidden")))
58 #endif
59 extern const char __cp_begin[1], __cp_end[1];
60
61 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
62 {
63         pthread_t self = __pthread_self();
64         ucontext_t *uc = ctx;
65         uintptr_t pc = uc->uc_mcontext.MC_PC;
66
67         a_barrier();
68         if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
69
70         _sigaddset(&uc->uc_sigmask, SIGCANCEL);
71
72         if (self->cancelasync || pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end) {
73                 uc->uc_mcontext.MC_PC = (uintptr_t)__cp_cancel;
74                 return;
75         }
76
77         __syscall(SYS_tkill, self->tid, SIGCANCEL);
78 }
79
80 void __testcancel()
81 {
82         pthread_t self = __pthread_self();
83         if (self->cancel && !self->canceldisable)
84                 __cancel();
85 }
86
87 static void init_cancellation()
88 {
89         struct sigaction sa = {
90                 .sa_flags = SA_SIGINFO | SA_RESTART,
91                 .sa_sigaction = cancel_handler
92         };
93         memset(&sa.sa_mask, -1, _NSIG/8);
94         __libc_sigaction(SIGCANCEL, &sa, 0);
95 }
96
97 int pthread_cancel(pthread_t t)
98 {
99         static int init;
100         if (!init) {
101                 init_cancellation();
102                 init = 1;
103         }
104         a_store(&t->cancel, 1);
105         return pthread_kill(t, SIGCANCEL);
106 }