move rsyscall out of pthread_create module
[musl] / src / thread / __rsyscall.c
1 #include "pthread_impl.h"
2
3 /* "rsyscall" is a mechanism by which a thread can synchronously force all
4  * other threads to perform an arbitrary syscall. It is necessary to work
5  * around the non-conformant implementation of setuid() et al on Linux,
6  * which affect only the calling thread and not the whole process. This
7  * implementation performs some tricks with signal delivery to work around
8  * the fact that it does not keep any list of threads in userspace. */
9
10 static struct {
11         volatile int lock, hold, blocks, cnt;
12         unsigned long arg[6];
13         int nr;
14         int err;
15         int init;
16 } rs;
17
18 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
19 {
20         struct pthread *self = __pthread_self();
21         long r;
22
23         if (!rs.hold || rs.cnt == libc.threads_minus_1) return;
24
25         /* Threads which have already decremented themselves from the
26          * thread count must not increment rs.cnt or otherwise act. */
27         if (self->dead) {
28                 sigfillset(&((ucontext_t *)ctx)->uc_sigmask);
29                 return;
30         }
31
32         r = __syscall(rs.nr, rs.arg[0], rs.arg[1],
33                 rs.arg[2], rs.arg[3], rs.arg[4], rs.arg[5]);
34         if (r < 0) rs.err=-r;
35
36         a_inc(&rs.cnt);
37         __wake(&rs.cnt, 1, 1);
38         while(rs.hold)
39                 __wait(&rs.hold, 0, 1, 1);
40         a_dec(&rs.cnt);
41         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
42 }
43
44 int __rsyscall(int nr, long a, long b, long c, long d, long e, long f)
45 {
46         int i, ret;
47         sigset_t set = { 0 };
48         struct pthread *self;
49
50         if (!libc.threads_minus_1)
51                 return syscall(nr, a, b, c, d, e, f);
52
53         self = __pthread_self();
54
55         LOCK(&rs.lock);
56         while ((i=rs.blocks))
57                 __wait(&rs.blocks, 0, i, 1);
58
59         sigfillset(&set);
60         __libc_sigprocmask(SIG_BLOCK, &set, &set);
61
62         if (!rs.init) {
63                 struct sigaction sa = {
64                         .sa_sigaction = rsyscall_handler,
65                         .sa_mask = set
66                 };
67                 sigfillset(&sa.sa_mask);
68                 sa.sa_sigaction = rsyscall_handler;
69                 __libc_sigaction(SIGSYSCALL, &sa, 0);
70         }
71
72         rs.nr = nr;
73         rs.arg[0] = a; rs.arg[1] = b;
74         rs.arg[2] = c; rs.arg[3] = d;
75         rs.arg[4] = d; rs.arg[5] = f;
76         rs.err = 0;
77         rs.cnt = 0;
78         rs.hold = 1;
79
80         /* Dispatch signals until all threads respond */
81         for (i=libc.threads_minus_1; i; i--)
82                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
83         while ((i=rs.cnt) < libc.threads_minus_1) {
84                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
85                 __wait(&rs.cnt, 0, i, 1);
86         }
87
88         /* Handle any lingering signals with no-op */
89         __libc_sigprocmask(SIG_UNBLOCK, &set, &set);
90
91         /* Resume other threads' signal handlers and wait for them */
92         rs.hold = 0;
93         __wake(&rs.hold, -1, 0);
94         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
95
96         if (rs.err) errno = rs.err, ret = -1;
97         else ret = syscall(nr, a, b, c, d, e, f);
98
99         UNLOCK(&rs.lock);
100         return ret;
101 }
102
103 void __rsyscall_lock()
104 {
105         a_inc(&rs.blocks);
106         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
107 }
108
109 void __rsyscall_unlock()
110 {
111         a_dec(&rs.blocks);
112         if (rs.lock) __wake(&rs.blocks, 1, 1);
113 }