block all signals during rsyscall
[musl] / src / thread / pthread_create.c
1 #include "pthread_impl.h"
2
3 static void dummy_1(pthread_t self)
4 {
5 }
6 weak_alias(dummy_1, __pthread_tsd_run_dtors);
7 weak_alias(dummy_1, __sigtimer_handler);
8
9 #ifdef __pthread_unwind_next
10 #undef __pthread_unwind_next
11 #define __pthread_unwind_next __pthread_unwind_next_3
12 #endif
13
14 void __pthread_unwind_next(struct __ptcb *cb)
15 {
16         pthread_t self;
17
18         if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
19
20         self = pthread_self();
21
22         LOCK(&self->exitlock);
23
24         __pthread_tsd_run_dtors(self);
25
26         /* Mark this thread dead before decrementing count */
27         self->dead = 1;
28
29         if (!a_fetch_add(&libc.threads_minus_1, -1))
30                 exit(0);
31
32         if (self->detached && self->map_base) {
33                 syscall(__NR_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
34                 __unmapself(self->map_base, self->map_size);
35         }
36
37         syscall(SYS_exit, 0);
38 }
39
40 static void docancel(struct pthread *self)
41 {
42         struct __ptcb cb = { .__next = self->cancelbuf };
43         self->canceldisable = 1;
44         self->cancelasync = 0;
45         __pthread_unwind_next(&cb);
46 }
47
48 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
49 {
50         struct pthread *self = __pthread_self();
51         if (si->si_code == SI_TIMER) __sigtimer_handler(self);
52         if (self->cancel && !self->canceldisable &&
53             (self->cancelasync || (self->cancelpoint==1 && PC_AT_SYS(ctx))))
54                 docancel(self);
55 }
56
57 static void cancelpt(int x)
58 {
59         struct pthread *self = __pthread_self();
60         if (self->canceldisable) return;
61         if ((self->cancelpoint+=x)==1 && x>=0 && self->cancel)
62                 docancel(self);
63 }
64
65 /* "rsyscall" is a mechanism by which a thread can synchronously force all
66  * other threads to perform an arbitrary syscall. It is necessary to work
67  * around the non-conformant implementation of setuid() et al on Linux,
68  * which affect only the calling thread and not the whole process. This
69  * implementation performs some tricks with signal delivery to work around
70  * the fact that it does not keep any list of threads in userspace. */
71
72 static struct {
73         volatile int lock, hold, blocks, cnt;
74         unsigned long arg[6];
75         int nr;
76         int err;
77 } rs;
78
79 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
80 {
81         struct pthread *self = __pthread_self();
82
83         if (!rs.hold || rs.cnt == libc.threads_minus_1) return;
84
85         /* Threads which have already decremented themselves from the
86          * thread count must not increment rs.cnt or otherwise act. */
87         if (self->dead) {
88                 sigfillset(&((ucontext_t *)ctx)->uc_sigmask);
89                 return;
90         }
91
92         if (syscall(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
93                 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
94
95         a_inc(&rs.cnt);
96         __wake(&rs.cnt, 1, 1);
97         while(rs.hold)
98                 __wait(&rs.hold, 0, 1, 1);
99         a_dec(&rs.cnt);
100         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
101 }
102
103 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
104 {
105         int i, ret;
106         sigset_t set = { 0 };
107         struct pthread *self = __pthread_self();
108         sigaddset(&set, SIGSYSCALL);
109
110         LOCK(&rs.lock);
111         while ((i=rs.blocks))
112                 __wait(&rs.blocks, 0, i, 1);
113
114         __libc_sigprocmask(SIG_BLOCK, &set, 0);
115
116         rs.nr = nr;
117         rs.arg[0] = a; rs.arg[1] = b;
118         rs.arg[2] = c; rs.arg[3] = d;
119         rs.arg[4] = d; rs.arg[5] = f;
120         rs.err = 0;
121         rs.cnt = 0;
122         rs.hold = 1;
123
124         /* Dispatch signals until all threads respond */
125         for (i=libc.threads_minus_1; i; i--)
126                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
127         while ((i=rs.cnt) < libc.threads_minus_1) {
128                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
129                 __wait(&rs.cnt, 0, i, 1);
130         }
131
132         /* Handle any lingering signals with no-op */
133         __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
134
135         /* Resume other threads' signal handlers and wait for them */
136         rs.hold = 0;
137         __wake(&rs.hold, -1, 0);
138         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
139
140         if (rs.err) errno = rs.err, ret = -1;
141         else ret = syscall(nr, a, b, c, d, e, f);
142
143         UNLOCK(&rs.lock);
144         return ret;
145 }
146
147 static void init_threads()
148 {
149         struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
150         libc.lock = __lock;
151         libc.lockfile = __lockfile;
152         libc.cancelpt = cancelpt;
153         libc.rsyscall = rsyscall;
154
155         sigfillset(&sa.sa_mask);
156         sa.sa_sigaction = rsyscall_handler;
157         __libc_sigaction(SIGSYSCALL, &sa, 0);
158
159         sigemptyset(&sa.sa_mask);
160         sa.sa_sigaction = cancel_handler;
161         __libc_sigaction(SIGCANCEL, &sa, 0);
162
163         sigaddset(&sa.sa_mask, SIGSYSCALL);
164         sigaddset(&sa.sa_mask, SIGCANCEL);
165         __libc_sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
166 }
167
168 static int start(void *p)
169 {
170         struct pthread *self = p;
171         if (self->unblock_cancel) {
172                 sigset_t set;
173                 sigemptyset(&set);
174                 sigaddset(&set, SIGCANCEL);
175                 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
176         }
177         pthread_exit(self->start(self->start_arg));
178         return 0;
179 }
180
181 int __uniclone(void *, int (*)(), void *);
182
183 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
184
185 /* pthread_key_create.c overrides this */
186 static const size_t dummy = 0;
187 weak_alias(dummy, __pthread_tsd_size);
188
189 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
190 {
191         static int init;
192         int ret;
193         size_t size, guard;
194         struct pthread *self = pthread_self(), *new;
195         unsigned char *map, *stack, *tsd;
196         static const pthread_attr_t default_attr;
197
198         if (!self) return errno = ENOSYS;
199         if (!init && ++init) init_threads();
200
201         if (!attr) attr = &default_attr;
202         guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
203         size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
204         size += __pthread_tsd_size;
205         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
206         if (!map) return EAGAIN;
207         if (guard) mprotect(map, guard, PROT_NONE);
208
209         tsd = map + size - __pthread_tsd_size;
210         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
211         new->map_base = map;
212         new->map_size = size;
213         new->pid = self->pid;
214         new->errno_ptr = &new->errno_val;
215         new->start = entry;
216         new->start_arg = arg;
217         new->self = new;
218         new->tsd = (void *)tsd;
219         new->detached = attr->_a_detach;
220         new->attr = *attr;
221         new->unblock_cancel = self->cancel;
222         new->result = PTHREAD_CANCELED;
223         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
224         new->tlsdesc[1] = (uintptr_t)new;
225         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
226
227         /* We must synchronize new thread creation with rsyscall
228          * delivery. This looks to be the least expensive way: */
229         a_inc(&rs.blocks);
230         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
231
232         a_inc(&libc.threads_minus_1);
233         ret = __uniclone(stack, start, new);
234
235         a_dec(&rs.blocks);
236         if (rs.lock) __wake(&rs.blocks, 1, 1);
237
238         if (ret < 0) {
239                 a_dec(&libc.threads_minus_1);
240                 munmap(map, size);
241                 return EAGAIN;
242         }
243         *res = new;
244         return 0;
245 }
246
247 void pthread_exit(void *result)
248 {
249         struct pthread *self = pthread_self();
250         self->result = result;
251         docancel(self);
252 }