kernel - MPSAFE work - fix bugs in recent MPSAFE work.
[dragonfly.git] / sys / emulation / linux / linux_signal.c
1 /*-
2  * Copyright (c) 1994-1995 Søren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer 
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/compat/linux/linux_signal.c,v 1.23.2.3 2001/11/05 19:08:23 marcel Exp $
29  * $DragonFly: src/sys/emulation/linux/linux_signal.c,v 1.14 2007/03/12 21:07:42 corecode Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/lock.h>
35 #include <sys/proc.h>
36 #include <sys/signalvar.h>
37 #include <sys/sysproto.h>
38 #include <sys/kern_syscall.h>
39 #include <sys/thread.h>
40
41 #include <sys/thread2.h>
42 #include <sys/mplock2.h>
43
44 #include <arch_linux/linux.h>
45 #include <arch_linux/linux_proto.h>
46 #include "linux_emuldata.h"
47 #include "linux_signal.h"
48 #include "linux_util.h"
49
50 void
51 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
52 {
53         int b, l;
54
55         SIGEMPTYSET(*bss);
56         bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
57         bss->__bits[1] = lss->__bits[1];
58         for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
59                 if (LINUX_SIGISMEMBER(*lss, l)) {
60                         b = linux_to_bsd_signal[_SIG_IDX(l)];
61                         if (b)
62                                 SIGADDSET(*bss, b);
63                 }
64         }
65 }
66
67 void
68 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
69 {
70         int b, l;
71
72         LINUX_SIGEMPTYSET(*lss);
73         lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
74         lss->__bits[1] = bss->__bits[1];
75         for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
76                 if (SIGISMEMBER(*bss, b)) {
77                         l = bsd_to_linux_signal[_SIG_IDX(b)];
78                         if (l)
79                                 LINUX_SIGADDSET(*lss, l);
80                 }
81         }
82 }
83
84 void
85 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
86 {
87
88         linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
89         bsa->sa_handler = lsa->lsa_handler;
90         bsa->sa_flags = 0;
91         if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
92                 bsa->sa_flags |= SA_NOCLDSTOP;
93         if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
94                 bsa->sa_flags |= SA_NOCLDWAIT;
95         if (lsa->lsa_flags & LINUX_SA_SIGINFO)
96                 bsa->sa_flags |= SA_SIGINFO;
97         if (lsa->lsa_flags & LINUX_SA_ONSTACK)
98                 bsa->sa_flags |= SA_ONSTACK;
99         if (lsa->lsa_flags & LINUX_SA_RESTART)
100                 bsa->sa_flags |= SA_RESTART;
101         if (lsa->lsa_flags & LINUX_SA_ONESHOT)
102                 bsa->sa_flags |= SA_RESETHAND;
103         if (lsa->lsa_flags & LINUX_SA_NOMASK)
104                 bsa->sa_flags |= SA_NODEFER;
105 }
106
107 void
108 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
109 {
110
111         bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
112         lsa->lsa_handler = bsa->sa_handler;
113         lsa->lsa_restorer = NULL;       /* unsupported */
114         lsa->lsa_flags = 0;
115         if (bsa->sa_flags & SA_NOCLDSTOP)
116                 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
117         if (bsa->sa_flags & SA_NOCLDWAIT)
118                 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
119         if (bsa->sa_flags & SA_SIGINFO)
120                 lsa->lsa_flags |= LINUX_SA_SIGINFO;
121         if (bsa->sa_flags & SA_ONSTACK)
122                 lsa->lsa_flags |= LINUX_SA_ONSTACK;
123         if (bsa->sa_flags & SA_RESTART)
124                 lsa->lsa_flags |= LINUX_SA_RESTART;
125         if (bsa->sa_flags & SA_RESETHAND)
126                 lsa->lsa_flags |= LINUX_SA_ONESHOT;
127         if (bsa->sa_flags & SA_NODEFER)
128                 lsa->lsa_flags |= LINUX_SA_NOMASK;
129 }
130
131 /*
132  * MPALMOSTSAFE
133  */
134 int
135 sys_linux_signal(struct linux_signal_args *args)
136 {
137         l_sigaction_t linux_nsa, linux_osa;
138         struct sigaction nsa, osa;
139         int error, sig;
140
141 #ifdef DEBUG
142         if (ldebug(signal))
143                 kprintf(ARGS(signal, "%d, %p"),
144                     args->sig, (void *)args->handler);
145 #endif
146         linux_nsa.lsa_handler = args->handler;
147         linux_nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
148         LINUX_SIGEMPTYSET(linux_nsa.lsa_mask);
149         linux_to_bsd_sigaction(&linux_nsa, &nsa);
150         if (args->sig <= LINUX_SIGTBLSZ) {
151                 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
152         } else {
153                 sig = args->sig;
154         }
155
156         get_mplock();
157         error = kern_sigaction(sig, &nsa, &osa);
158         rel_mplock();
159
160         bsd_to_linux_sigaction(&osa, &linux_osa);
161         args->sysmsg_result = (int) linux_osa.lsa_handler;
162         return (error);
163 }
164
165 /*
166  * MPALMOSTSAFE
167  */
168 int
169 sys_linux_rt_sigaction(struct linux_rt_sigaction_args *args)
170 {
171         l_sigaction_t linux_nsa, linux_osa;
172         struct sigaction nsa, osa;
173         int error, sig;
174
175 #ifdef DEBUG
176         if (ldebug(rt_sigaction))
177                 kprintf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
178                     (long)args->sig, (void *)args->act,
179                     (void *)args->oact, (long)args->sigsetsize);
180 #endif
181         if (args->sigsetsize != sizeof(l_sigset_t))
182                 return (EINVAL);
183
184         if (args->act) {
185                 error = copyin(args->act, &linux_nsa, sizeof(linux_nsa));
186                 if (error)
187                         return (error);
188                 linux_to_bsd_sigaction(&linux_nsa, &nsa);
189         }
190         if (args->sig <= LINUX_SIGTBLSZ) {
191                 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
192         } else {
193                 sig = args->sig;
194         }
195
196         get_mplock();
197         error = kern_sigaction(sig, args->act ? &nsa : NULL,
198                                args->oact ? &osa : NULL);
199         rel_mplock();
200
201         if (error == 0 && args->oact) {
202                 bsd_to_linux_sigaction(&osa, &linux_osa);
203                 error = copyout(&linux_osa, args->oact, sizeof(linux_osa));
204         }
205
206         return (error);
207 }
208
209 static int
210 linux_to_bsd_sigprocmask(int how)
211 {
212         switch (how) {
213         case LINUX_SIG_BLOCK:
214                 return SIG_BLOCK;
215         case LINUX_SIG_UNBLOCK:
216                 return SIG_UNBLOCK;
217         case LINUX_SIG_SETMASK:
218                 return SIG_SETMASK;
219         default:
220                 return (-1);
221         }
222 }
223
224 /*
225  * MPALMOSTSAFE
226  */
227 int
228 sys_linux_sigprocmask(struct linux_sigprocmask_args *args)
229 {
230         l_osigset_t mask;
231         l_sigset_t linux_set, linux_oset;
232         sigset_t set, oset;
233         int error, how;
234
235 #ifdef DEBUG
236         if (ldebug(sigprocmask))
237                 kprintf(ARGS(sigprocmask, "%d, *, *"), args->how);
238 #endif
239
240         if (args->mask) {
241                 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
242                 if (error)
243                         return (error);
244                 LINUX_SIGEMPTYSET(linux_set);
245                 linux_set.__bits[0] = mask;
246                 linux_to_bsd_sigset(&linux_set, &set);
247         }
248         how = linux_to_bsd_sigprocmask(args->how);
249
250         get_mplock();
251         error = kern_sigprocmask(how, args->mask ? &set : NULL,
252                                  args->omask ? &oset : NULL);
253         rel_mplock();
254
255         if (error == 0 && args->omask) {
256                 bsd_to_linux_sigset(&oset, &linux_oset);
257                 mask = linux_oset.__bits[0];
258                 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
259         }
260         return (error);
261 }
262
263 /*
264  * MPALMOSTSAFE
265  */
266 int
267 sys_linux_rt_sigprocmask(struct linux_rt_sigprocmask_args *args)
268 {
269         l_sigset_t linux_set, linux_oset;
270         sigset_t set, oset;
271         int error, how;
272
273 #ifdef DEBUG
274         if (ldebug(rt_sigprocmask))
275                 kprintf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
276                     args->how, (void *)args->mask,
277                     (void *)args->omask, (long)args->sigsetsize);
278 #endif
279
280         if (args->sigsetsize != sizeof(l_sigset_t))
281                 return EINVAL;
282
283         if (args->mask) {
284                 error = copyin(args->mask, &linux_set, sizeof(l_sigset_t));
285                 if (error)
286                         return (error);
287                 linux_to_bsd_sigset(&linux_set, &set);
288         }
289         how = linux_to_bsd_sigprocmask(args->how);
290
291         get_mplock();
292         error = kern_sigprocmask(how, args->mask ? &set : NULL,
293                                  args->omask ? &oset : NULL);
294         rel_mplock();
295
296         if (error == 0 && args->omask) {
297                 bsd_to_linux_sigset(&oset, &linux_oset);
298                 error = copyout(&linux_oset, args->omask, sizeof(l_sigset_t));
299         }
300
301         return (error);
302 }
303
304 /*
305  * MPSAFE
306  */
307 int
308 sys_linux_sgetmask(struct linux_sgetmask_args *args)
309 {
310         struct lwp *lp = curthread->td_lwp;
311         l_sigset_t mask;
312
313 #ifdef DEBUG
314         if (ldebug(sgetmask))
315                 kprintf(ARGS(sgetmask, ""));
316 #endif
317
318         bsd_to_linux_sigset(&lp->lwp_sigmask, &mask);
319         args->sysmsg_result = mask.__bits[0];
320         return (0);
321 }
322
323 /*
324  * MPSAFE
325  */
326 int
327 sys_linux_ssetmask(struct linux_ssetmask_args *args)
328 {
329         struct lwp *lp = curthread->td_lwp;
330         l_sigset_t lset;
331         sigset_t bset;
332
333 #ifdef DEBUG
334         if (ldebug(ssetmask))
335                 kprintf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
336 #endif
337
338         bsd_to_linux_sigset(&lp->lwp_sigmask, &lset);
339         args->sysmsg_result = lset.__bits[0];
340         LINUX_SIGEMPTYSET(lset);
341         lset.__bits[0] = args->mask;
342         linux_to_bsd_sigset(&lset, &bset);
343         crit_enter();
344         lp->lwp_sigmask = bset;
345         SIG_CANTMASK(lp->lwp_sigmask);
346         crit_exit();
347         return (0);
348 }
349
350 /*
351  * MPSAFE
352  */
353 int
354 sys_linux_sigpending(struct linux_sigpending_args *args)
355 {
356         struct thread *td = curthread;
357         struct lwp *lp = td->td_lwp;
358         sigset_t set;
359         l_sigset_t linux_set;
360         l_osigset_t mask;
361         int error;
362
363 #ifdef DEBUG
364         if (ldebug(sigpending))
365                 kprintf(ARGS(sigpending, "*"));
366 #endif
367
368         error = kern_sigpending(&set);
369
370         if (error == 0) {
371                 SIGSETAND(set, lp->lwp_sigmask);
372                 bsd_to_linux_sigset(&set, &linux_set);
373                 mask = linux_set.__bits[0];
374                 error = copyout(&mask, args->mask, sizeof(mask));
375         }
376         return (error);
377 }
378
379 /*
380  * MPALMOSTSAFE
381  */
382 int
383 sys_linux_kill(struct linux_kill_args *args)
384 {
385         int error, sig;
386
387 #ifdef DEBUG
388         if (ldebug(kill))
389                 kprintf(ARGS(kill, "%d, %d"), args->pid, args->signum);
390 #endif
391         /*
392          * Allow signal 0 as a means to check for privileges
393          */
394         if (args->signum < 0 || args->signum > LINUX_NSIG)
395                 return EINVAL;
396
397         if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
398                 sig = linux_to_bsd_signal[_SIG_IDX(args->signum)];
399         else
400                 sig = args->signum;
401
402         get_mplock();
403         error = kern_kill(sig, args->pid, -1);
404         rel_mplock();
405
406         return(error);
407 }
408
409
410 static int
411 linux_do_tkill(l_int tgid, l_int pid, l_int sig)
412 {
413         struct linux_emuldata *em;
414         struct proc *p;
415         int error = 0;
416
417         /*
418          * Allow signal 0 as a means to check for privileges
419          */
420         if (sig < 0 || sig > LINUX_NSIG)
421                 return (EINVAL);
422
423         if (sig > 0 && sig <= LINUX_SIGTBLSZ)
424                 sig = linux_to_bsd_signal[_SIG_IDX(sig)];
425
426         get_mplock();
427         lwkt_gettoken(&proc_token);
428         if ((p = pfind(pid)) == NULL) {
429                 if ((p = zpfind(pid)) == NULL) {
430                         error = ESRCH;
431                         goto done2;
432                 }
433         }
434         PHOLD(p);
435
436         EMUL_LOCK();
437         em = emuldata_get(p);
438
439         if (em == NULL) {
440                 EMUL_UNLOCK();
441                 error = ESRCH;
442                 goto done1;
443         }
444
445         if (tgid > 0 && em->s->group_pid != tgid) {
446                 EMUL_UNLOCK();
447                 error = ESRCH;
448                 goto done1;
449         }
450         EMUL_UNLOCK();
451         
452         error = kern_kill(sig, pid, -1);
453 done1:
454         PRELE(p);
455 done2:
456         lwkt_reltoken(&proc_token);
457         rel_mplock();
458
459         return (error);
460 }
461
462 int
463 sys_linux_tgkill(struct linux_tgkill_args *args)
464 {
465
466 #ifdef DEBUG
467         if (ldebug(tgkill))
468                 kprintf(ARGS(tgkill, "%d, %d, %d"), args->tgid, args->pid, args->sig);
469 #endif
470         if (args->pid <= 0 || args->tgid <= 0)
471                 return (EINVAL);
472
473         return (linux_do_tkill(args->tgid, args->pid, args->sig));
474 }
475
476 int
477 sys_linux_tkill(struct linux_tkill_args *args)
478 {
479 #ifdef DEBUG
480         if (ldebug(tkill))
481                 kprintf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
482 #endif
483         if (args->tid <= 0)
484                 return (EINVAL);
485
486         return (linux_do_tkill(0, args->tid, args->sig));
487 }
488