linux_signal.c revision 1.45 1 /* $NetBSD: linux_signal.c,v 1.45 2005/05/16 16:00:31 fvdl Exp $ */
2 /*-
3 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Frank van der Linden and Eric Haszlakiewicz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37 /*
38 * heavily from: svr4_signal.c,v 1.7 1995/01/09 01:04:21 christos Exp
39 */
40
41 /*
42 * Functions in multiarch:
43 * linux_sys_signal : linux_sig_notalpha.c
44 * linux_sys_siggetmask : linux_sig_notalpha.c
45 * linux_sys_sigsetmask : linux_sig_notalpha.c
46 * linux_sys_pause : linux_sig_notalpha.c
47 * linux_sys_sigaction : linux_sigaction.c
48 *
49 */
50
51 /*
52 * Unimplemented:
53 * linux_sys_rt_sigtimedwait : sigsuspend w/timeout.
54 */
55
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: linux_signal.c,v 1.45 2005/05/16 16:00:31 fvdl Exp $");
58
59 #define COMPAT_LINUX 1
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/namei.h>
64 #include <sys/proc.h>
65 #include <sys/filedesc.h>
66 #include <sys/ioctl.h>
67 #include <sys/mount.h>
68 #include <sys/kernel.h>
69 #include <sys/signal.h>
70 #include <sys/signalvar.h>
71 #include <sys/malloc.h>
72
73 #include <sys/sa.h>
74 #include <sys/syscallargs.h>
75
76 #include <compat/linux/common/linux_types.h>
77 #include <compat/linux/common/linux_signal.h>
78 #include <compat/linux/common/linux_siginfo.h>
79 #include <compat/linux/common/linux_sigevent.h>
80 #include <compat/linux/common/linux_util.h>
81
82 #include <compat/linux/linux_syscallargs.h>
83
84 /* Locally used defines (in bsd<->linux conversion functions): */
85 #define linux_sigemptyset(s) memset((s), 0, sizeof(*(s)))
86 #define linux_sigismember(s, n) ((s)->sig[((n) - 1) / LINUX__NSIG_BPW] \
87 & (1 << ((n) - 1) % LINUX__NSIG_BPW))
88 #define linux_sigaddset(s, n) ((s)->sig[((n) - 1) / LINUX__NSIG_BPW] \
89 |= (1 << ((n) - 1) % LINUX__NSIG_BPW))
90
91 #ifdef DEBUG_LINUX
92 #define DPRINTF(a) uprintf a
93 #else
94 #define DPRINTF(a)
95 #endif
96
97 extern const int native_to_linux_signo[];
98 extern const int linux_to_native_signo[];
99
100 /*
101 * Convert between Linux and BSD signal sets.
102 */
103 #if LINUX__NSIG_WORDS > 1
104 void
105 linux_old_extra_to_native_sigset(bss, lss, extra)
106 sigset_t *bss;
107 const linux_old_sigset_t *lss;
108 const unsigned long *extra;
109 {
110 linux_sigset_t lsnew;
111
112 /* convert old sigset to new sigset */
113 linux_sigemptyset(&lsnew);
114 lsnew.sig[0] = *lss;
115 if (extra)
116 memcpy(&lsnew.sig[1], extra,
117 sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
118
119 linux_to_native_sigset(bss, &lsnew);
120 }
121
122 void
123 native_to_linux_old_extra_sigset(lss, extra, bss)
124 linux_old_sigset_t *lss;
125 unsigned long *extra;
126 const sigset_t *bss;
127 {
128 linux_sigset_t lsnew;
129
130 native_to_linux_sigset(&lsnew, bss);
131
132 /* convert new sigset to old sigset */
133 *lss = lsnew.sig[0];
134 if (extra)
135 memcpy(extra, &lsnew.sig[1],
136 sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
137 }
138 #endif /* LINUX__NSIG_WORDS > 1 */
139
140 void
141 linux_to_native_sigset(bss, lss)
142 sigset_t *bss;
143 const linux_sigset_t *lss;
144 {
145 int i, newsig;
146
147 sigemptyset(bss);
148 for (i = 1; i < LINUX__NSIG; i++) {
149 if (linux_sigismember(lss, i)) {
150 newsig = linux_to_native_signo[i];
151 if (newsig)
152 sigaddset(bss, newsig);
153 }
154 }
155 }
156
157 void
158 native_to_linux_sigset(lss, bss)
159 linux_sigset_t *lss;
160 const sigset_t *bss;
161 {
162 int i, newsig;
163
164 linux_sigemptyset(lss);
165 for (i = 1; i < NSIG; i++) {
166 if (sigismember(bss, i)) {
167 newsig = native_to_linux_signo[i];
168 if (newsig)
169 linux_sigaddset(lss, newsig);
170 }
171 }
172 }
173
174 unsigned int
175 native_to_linux_sigflags(bsf)
176 const int bsf;
177 {
178 unsigned int lsf = 0;
179 if ((bsf & SA_NOCLDSTOP) != 0)
180 lsf |= LINUX_SA_NOCLDSTOP;
181 if ((bsf & SA_NOCLDWAIT) != 0)
182 lsf |= LINUX_SA_NOCLDWAIT;
183 if ((bsf & SA_ONSTACK) != 0)
184 lsf |= LINUX_SA_ONSTACK;
185 if ((bsf & SA_RESTART) != 0)
186 lsf |= LINUX_SA_RESTART;
187 if ((bsf & SA_NODEFER) != 0)
188 lsf |= LINUX_SA_NOMASK;
189 if ((bsf & SA_RESETHAND) != 0)
190 lsf |= LINUX_SA_ONESHOT;
191 if ((bsf & SA_SIGINFO) != 0)
192 lsf |= LINUX_SA_SIGINFO;
193 return lsf;
194 }
195
196 int
197 linux_to_native_sigflags(lsf)
198 const unsigned long lsf;
199 {
200 int bsf = 0;
201 if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
202 bsf |= SA_NOCLDSTOP;
203 if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
204 bsf |= SA_NOCLDWAIT;
205 if ((lsf & LINUX_SA_ONSTACK) != 0)
206 bsf |= SA_ONSTACK;
207 if ((lsf & LINUX_SA_RESTART) != 0)
208 bsf |= SA_RESTART;
209 if ((lsf & LINUX_SA_ONESHOT) != 0)
210 bsf |= SA_RESETHAND;
211 if ((lsf & LINUX_SA_NOMASK) != 0)
212 bsf |= SA_NODEFER;
213 if ((lsf & LINUX_SA_SIGINFO) != 0)
214 bsf |= SA_SIGINFO;
215 if ((lsf & ~LINUX_SA_ALLBITS) != 0)
216 DPRINTF(("linux_old_to_native_sigflags: "
217 "%lx extra bits ignored\n", lsf));
218 return bsf;
219 }
220
221 /*
222 * Convert between Linux and BSD sigaction structures. Linux sometimes
223 * has one extra field (sa_restorer) which we don't support.
224 */
225 void
226 linux_old_to_native_sigaction(bsa, lsa)
227 struct sigaction *bsa;
228 const struct linux_old_sigaction *lsa;
229 {
230 bsa->sa_handler = lsa->linux_sa_handler;
231 linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
232 bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
233 #ifndef __alpha__
234 /*
235 * XXX: On the alpha sa_restorer is elsewhere.
236 */
237 if (lsa->linux_sa_restorer != NULL)
238 DPRINTF(("linux_old_to_native_sigaction: "
239 "sa_restorer ignored\n"));
240 #endif /* !__alpha__ */
241 }
242
243 void
244 native_to_linux_old_sigaction(lsa, bsa)
245 struct linux_old_sigaction *lsa;
246 const struct sigaction *bsa;
247 {
248 lsa->linux_sa_handler = bsa->sa_handler;
249 native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
250 lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
251 #ifndef __alpha__
252 lsa->linux_sa_restorer = NULL;
253 #endif
254 }
255
256 /* ...and the new sigaction conversion funcs. */
257 void
258 linux_to_native_sigaction(bsa, lsa)
259 struct sigaction *bsa;
260 const struct linux_sigaction *lsa;
261 {
262 bsa->sa_handler = lsa->linux_sa_handler;
263 linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
264 bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
265 #ifndef __alpha__
266 if (lsa->linux_sa_restorer != 0)
267 DPRINTF(("linux_to_native_sigaction: sa_restorer ignored\n"));
268 #endif
269 }
270
271 void
272 native_to_linux_sigaction(lsa, bsa)
273 struct linux_sigaction *lsa;
274 const struct sigaction *bsa;
275 {
276 lsa->linux_sa_handler = bsa->sa_handler;
277 native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
278 lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
279 #ifndef __alpha__
280 lsa->linux_sa_restorer = NULL;
281 #endif
282 }
283
284 /* ----------------------------------------------------------------------- */
285
286 /*
287 * The Linux sigaction() system call. Do the usual conversions,
288 * and just call sigaction(). Some flags and values are silently
289 * ignored (see above).
290 */
291 int
292 linux_sys_rt_sigaction(l, v, retval)
293 struct lwp *l;
294 void *v;
295 register_t *retval;
296 {
297 struct linux_sys_rt_sigaction_args /* {
298 syscallarg(int) signum;
299 syscallarg(const struct linux_sigaction *) nsa;
300 syscallarg(struct linux_sigaction *) osa;
301 syscallarg(size_t) sigsetsize;
302 } */ *uap = v;
303 struct proc *p = l->l_proc;
304 struct linux_sigaction nlsa, olsa;
305 struct sigaction nbsa, obsa;
306 int error, sig;
307
308 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
309 return (EINVAL);
310
311 if (SCARG(uap, nsa)) {
312 error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
313 if (error)
314 return (error);
315 linux_to_native_sigaction(&nbsa, &nlsa);
316 }
317 sig = SCARG(uap, signum);
318 if (sig < 0 || sig >= LINUX__NSIG)
319 return (EINVAL);
320 if (sig > 0 && !linux_to_native_signo[sig]) {
321 /* Pretend that we did something useful for unknown signals. */
322 obsa.sa_handler = SIG_IGN;
323 sigemptyset(&obsa.sa_mask);
324 obsa.sa_flags = 0;
325 } else {
326 error = sigaction1(p, linux_to_native_signo[sig],
327 SCARG(uap, nsa) ? &nbsa : NULL,
328 SCARG(uap, osa) ? &obsa : NULL,
329 NULL, 0);
330 if (error)
331 return (error);
332 }
333 if (SCARG(uap, osa)) {
334 native_to_linux_sigaction(&olsa, &obsa);
335 error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
336 if (error)
337 return (error);
338 }
339 return (0);
340 }
341
342 int
343 linux_sigprocmask1(p, how, set, oset)
344 struct proc *p;
345 int how;
346 const linux_old_sigset_t *set;
347 linux_old_sigset_t *oset;
348 {
349 linux_old_sigset_t nlss, olss;
350 sigset_t nbss, obss;
351 int error;
352
353 switch (how) {
354 case LINUX_SIG_BLOCK:
355 how = SIG_BLOCK;
356 break;
357 case LINUX_SIG_UNBLOCK:
358 how = SIG_UNBLOCK;
359 break;
360 case LINUX_SIG_SETMASK:
361 how = SIG_SETMASK;
362 break;
363 default:
364 return (EINVAL);
365 }
366
367 if (set) {
368 error = copyin(set, &nlss, sizeof(nlss));
369 if (error)
370 return (error);
371 linux_old_to_native_sigset(&nbss, &nlss);
372 }
373 error = sigprocmask1(p, how,
374 set ? &nbss : NULL, oset ? &obss : NULL);
375 if (error)
376 return (error);
377 if (oset) {
378 native_to_linux_old_sigset(&olss, &obss);
379 error = copyout(&olss, oset, sizeof(olss));
380 if (error)
381 return (error);
382 }
383 return (error);
384 }
385
386 int
387 linux_sys_rt_sigprocmask(l, v, retval)
388 struct lwp *l;
389 void *v;
390 register_t *retval;
391 {
392 struct linux_sys_rt_sigprocmask_args /* {
393 syscallarg(int) how;
394 syscallarg(const linux_sigset_t *) set;
395 syscallarg(linux_sigset_t *) oset;
396 syscallarg(size_t) sigsetsize;
397 } */ *uap = v;
398 struct proc *p = l->l_proc;
399 linux_sigset_t nlss, olss, *oset;
400 const linux_sigset_t *set;
401 sigset_t nbss, obss;
402 int error, how;
403
404 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
405 return (EINVAL);
406
407 switch (SCARG(uap, how)) {
408 case LINUX_SIG_BLOCK:
409 how = SIG_BLOCK;
410 break;
411 case LINUX_SIG_UNBLOCK:
412 how = SIG_UNBLOCK;
413 break;
414 case LINUX_SIG_SETMASK:
415 how = SIG_SETMASK;
416 break;
417 default:
418 return (EINVAL);
419 }
420
421 set = SCARG(uap, set);
422 oset = SCARG(uap, oset);
423
424 if (set) {
425 error = copyin(set, &nlss, sizeof(nlss));
426 if (error)
427 return (error);
428 linux_to_native_sigset(&nbss, &nlss);
429 }
430 error = sigprocmask1(p, how,
431 set ? &nbss : NULL, oset ? &obss : NULL);
432 if (!error && oset) {
433 native_to_linux_sigset(&olss, &obss);
434 error = copyout(&olss, oset, sizeof(olss));
435 }
436 return (error);
437 }
438
439 int
440 linux_sys_rt_sigpending(l, v, retval)
441 struct lwp *l;
442 void *v;
443 register_t *retval;
444 {
445 struct linux_sys_rt_sigpending_args /* {
446 syscallarg(linux_sigset_t *) set;
447 syscallarg(size_t) sigsetsize;
448 } */ *uap = v;
449 struct proc *p = l->l_proc;
450 sigset_t bss;
451 linux_sigset_t lss;
452
453 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
454 return (EINVAL);
455
456 sigpending1(p, &bss);
457 native_to_linux_sigset(&lss, &bss);
458 return copyout(&lss, SCARG(uap, set), sizeof(lss));
459 }
460
461 #ifndef __amd64__
462 int
463 linux_sys_sigpending(l, v, retval)
464 struct lwp *l;
465 void *v;
466 register_t *retval;
467 {
468 struct linux_sys_sigpending_args /* {
469 syscallarg(linux_old_sigset_t *) mask;
470 } */ *uap = v;
471 struct proc *p = l->l_proc;
472 sigset_t bss;
473 linux_old_sigset_t lss;
474
475 sigpending1(p, &bss);
476 native_to_linux_old_sigset(&lss, &bss);
477 return copyout(&lss, SCARG(uap, set), sizeof(lss));
478 }
479
480 int
481 linux_sys_sigsuspend(l, v, retval)
482 struct lwp *l;
483 void *v;
484 register_t *retval;
485 {
486 struct linux_sys_sigsuspend_args /* {
487 syscallarg(caddr_t) restart;
488 syscallarg(int) oldmask;
489 syscallarg(int) mask;
490 } */ *uap = v;
491 struct proc *p = l->l_proc;
492 linux_old_sigset_t lss;
493 sigset_t bss;
494
495 lss = SCARG(uap, mask);
496 linux_old_to_native_sigset(&bss, &lss);
497 return (sigsuspend1(p, &bss));
498 }
499 #endif /* __amd64__ */
500
501 int
502 linux_sys_rt_sigsuspend(l, v, retval)
503 struct lwp *l;
504 void *v;
505 register_t *retval;
506 {
507 struct linux_sys_rt_sigsuspend_args /* {
508 syscallarg(linux_sigset_t *) unewset;
509 syscallarg(size_t) sigsetsize;
510 } */ *uap = v;
511 struct proc *p = l->l_proc;
512 linux_sigset_t lss;
513 sigset_t bss;
514 int error;
515
516 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
517 return (EINVAL);
518
519 error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
520 if (error)
521 return (error);
522
523 linux_to_native_sigset(&bss, &lss);
524
525 return (sigsuspend1(p, &bss));
526 }
527
528 /*
529 * Once more: only a signal conversion is needed.
530 * Note: also used as sys_rt_queueinfo. The info field is ignored.
531 */
532 int
533 linux_sys_rt_queueinfo(l, v, retval)
534 struct lwp *l;
535 void *v;
536 register_t *retval;
537 {
538 /* XXX XAX This isn't this really int, int, siginfo_t *, is it? */
539 #if 0
540 struct linux_sys_rt_queueinfo_args /* {
541 syscallarg(int) pid;
542 syscallarg(int) signum;
543 syscallarg(siginfo_t *) uinfo;
544 } */ *uap = v;
545 #endif
546
547 /* XXX To really implement this we need to */
548 /* XXX keep a list of queued signals somewhere. */
549 return (linux_sys_kill(l, v, retval));
550 }
551
552 int
553 linux_sys_kill(l, v, retval)
554 struct lwp *l;
555 void *v;
556 register_t *retval;
557 {
558 struct linux_sys_kill_args /* {
559 syscallarg(int) pid;
560 syscallarg(int) signum;
561 } */ *uap = v;
562
563 struct sys_kill_args ka;
564 int sig;
565
566 SCARG(&ka, pid) = SCARG(uap, pid);
567 sig = SCARG(uap, signum);
568 if (sig < 0 || sig >= LINUX__NSIG)
569 return (EINVAL);
570 SCARG(&ka, signum) = linux_to_native_signo[sig];
571 return sys_kill(l, &ka, retval);
572 }
573
574 #ifdef LINUX_SS_ONSTACK
575 static void linux_to_native_sigaltstack __P((struct sigaltstack *,
576 const struct linux_sigaltstack *));
577
578 static void
579 linux_to_native_sigaltstack(bss, lss)
580 struct sigaltstack *bss;
581 const struct linux_sigaltstack *lss;
582 {
583 bss->ss_sp = lss->ss_sp;
584 bss->ss_size = lss->ss_size;
585 if (lss->ss_flags & LINUX_SS_ONSTACK)
586 bss->ss_flags = SS_ONSTACK;
587 else if (lss->ss_flags & LINUX_SS_DISABLE)
588 bss->ss_flags = SS_DISABLE;
589 else
590 bss->ss_flags = 0;
591 }
592
593 void
594 native_to_linux_sigaltstack(lss, bss)
595 struct linux_sigaltstack *lss;
596 const struct sigaltstack *bss;
597 {
598 lss->ss_sp = bss->ss_sp;
599 lss->ss_size = bss->ss_size;
600 if (bss->ss_flags & SS_ONSTACK)
601 lss->ss_flags = LINUX_SS_ONSTACK;
602 else if (bss->ss_flags & SS_DISABLE)
603 lss->ss_flags = LINUX_SS_DISABLE;
604 else
605 lss->ss_flags = 0;
606 }
607
608 int
609 linux_sys_sigaltstack(l, v, retval)
610 struct lwp *l;
611 void *v;
612 register_t *retval;
613 {
614 struct linux_sys_sigaltstack_args /* {
615 syscallarg(const struct linux_sigaltstack *) ss;
616 syscallarg(struct linux_sigaltstack *) oss;
617 } */ *uap = v;
618 struct proc *p = l->l_proc;
619 struct linux_sigaltstack ss;
620 struct sigaltstack nss, oss;
621 int error;
622
623 if (SCARG(uap, ss) != NULL) {
624 if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
625 return error;
626 linux_to_native_sigaltstack(&nss, &ss);
627 }
628
629 error = sigaltstack1(p,
630 SCARG(uap, ss) ? &nss : NULL, SCARG(uap, oss) ? &oss : NULL);
631 if (error)
632 return error;
633
634 if (SCARG(uap, oss) != NULL) {
635 native_to_linux_sigaltstack(&ss, &oss);
636 if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
637 return error;
638 }
639 return 0;
640 }
641 #endif /* LINUX_SS_ONSTACK */
642