61d41df76b4b3466a549be50b27789ee829bbbed
[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_flags = SA_SIGINFO | SA_RESTART,
65                         .sa_sigaction = rsyscall_handler,
66                         .sa_mask = set
67                 };
68                 sigfillset(&sa.sa_mask);
69                 sa.sa_sigaction = rsyscall_handler;
70                 __libc_sigaction(SIGSYSCALL, &sa, 0);
71                 rs.init = 1;
72         }
73
74         rs.nr = nr;
75         rs.arg[0] = a; rs.arg[1] = b;
76         rs.arg[2] = c; rs.arg[3] = d;
77         rs.arg[4] = d; rs.arg[5] = f;
78         rs.err = 0;
79         rs.cnt = 0;
80         rs.hold = 1;
81
82         /* Dispatch signals until all threads respond */
83         for (i=libc.threads_minus_1; i; i--)
84                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
85         while ((i=rs.cnt) < libc.threads_minus_1) {
86                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
87                 __wait(&rs.cnt, 0, i, 1);
88         }
89
90         /* Handle any lingering signals with no-op */
91         __libc_sigprocmask(SIG_UNBLOCK, &set, &set);
92
93         /* Resume other threads' signal handlers and wait for them */
94         rs.hold = 0;
95         __wake(&rs.hold, -1, 0);
96         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
97
98         if (rs.err) errno = rs.err, ret = -1;
99         else ret = syscall(nr, a, b, c, d, e, f);
100
101         UNLOCK(&rs.lock);
102         return ret;
103 }
104
105 void __rsyscall_lock()
106 {
107         a_inc(&rs.blocks);
108         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
109 }
110
111 void __rsyscall_unlock()
112 {
113         a_dec(&rs.blocks);
114         if (rs.lock) __wake(&rs.blocks, 1, 1);
115 }