linux_signal.c revision 1.43 1 /* $NetBSD: linux_signal.c,v 1.43 2005/05/03 16:26:29 manu 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.43 2005/05/03 16:26:29 manu 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_util.h>
80
81 #include <compat/linux/linux_syscallargs.h>
82
83 /* Locally used defines (in bsd<->linux conversion functions): */
84 #define linux_sigemptyset(s) memset((s), 0, sizeof(*(s)))
85 #define linux_sigismember(s, n) ((s)->sig[((n) - 1) / LINUX__NSIG_BPW] \
86 & (1 << ((n) - 1) % LINUX__NSIG_BPW))
87 #define linux_sigaddset(s, n) ((s)->sig[((n) - 1) / LINUX__NSIG_BPW] \
88 |= (1 << ((n) - 1) % LINUX__NSIG_BPW))
89
90 #ifdef DEBUG_LINUX
91 #define DPRINTF(a) uprintf a
92 #else
93 #define DPRINTF(a)
94 #endif
95
96 extern const int native_to_linux_signo[];
97 extern const int linux_to_native_signo[];
98
99 /*
100 * Convert between Linux and BSD signal sets.
101 */
102 #if LINUX__NSIG_WORDS > 1
103 void
104 linux_old_extra_to_native_sigset(bss, lss, extra)
105 sigset_t *bss;
106 const linux_old_sigset_t *lss;
107 const unsigned long *extra;
108 {
109 linux_sigset_t lsnew;
110
111 /* convert old sigset to new sigset */
112 linux_sigemptyset(&lsnew);
113 lsnew.sig[0] = *lss;
114 if (extra)
115 memcpy(&lsnew.sig[1], extra,
116 sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
117
118 linux_to_native_sigset(bss, &lsnew);
119 }
120
121 void
122 native_to_linux_old_extra_sigset(lss, extra, bss)
123 linux_old_sigset_t *lss;
124 unsigned long *extra;
125 const sigset_t *bss;
126 {
127 linux_sigset_t lsnew;
128
129 native_to_linux_sigset(&lsnew, bss);
130
131 /* convert new sigset to old sigset */
132 *lss = lsnew.sig[0];
133 if (extra)
134 memcpy(extra, &lsnew.sig[1],
135 sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
136 }
137 #endif /* LINUX__NSIG_WORDS > 1 */
138
139 void
140 linux_to_native_sigset(bss, lss)
141 sigset_t *bss;
142 const linux_sigset_t *lss;
143 {
144 int i, newsig;
145
146 sigemptyset(bss);
147 for (i = 1; i < LINUX__NSIG; i++) {
148 if (linux_sigismember(lss, i)) {
149 newsig = linux_to_native_signo[i];
150 if (newsig)
151 sigaddset(bss, newsig);
152 }
153 }
154 }
155
156 void
157 native_to_linux_sigset(lss, bss)
158 linux_sigset_t *lss;
159 const sigset_t *bss;
160 {
161 int i, newsig;
162
163 linux_sigemptyset(lss);
164 for (i = 1; i < NSIG; i++) {
165 if (sigismember(bss, i)) {
166 newsig = native_to_linux_signo[i];
167 if (newsig)
168 linux_sigaddset(lss, newsig);
169 }
170 }
171 }
172
173 unsigned int
174 native_to_linux_sigflags(bsf)
175 const int bsf;
176 {
177 unsigned int lsf = 0;
178 if ((bsf & SA_NOCLDSTOP) != 0)
179 lsf |= LINUX_SA_NOCLDSTOP;
180 if ((bsf & SA_NOCLDWAIT) != 0)
181 lsf |= LINUX_SA_NOCLDWAIT;
182 if ((bsf & SA_ONSTACK) != 0)
183 lsf |= LINUX_SA_ONSTACK;
184 if ((bsf & SA_RESTART) != 0)
185 lsf |= LINUX_SA_RESTART;
186 if ((bsf & SA_NODEFER) != 0)
187 lsf |= LINUX_SA_NOMASK;
188 if ((bsf & SA_RESETHAND) != 0)
189 lsf |= LINUX_SA_ONESHOT;
190 if ((bsf & SA_SIGINFO) != 0)
191 lsf |= LINUX_SA_SIGINFO;
192 return lsf;
193 }
194
195 int
196 linux_to_native_sigflags(lsf)
197 const unsigned long lsf;
198 {
199 int bsf = 0;
200 if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
201 bsf |= SA_NOCLDSTOP;
202 if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
203 bsf |= SA_NOCLDWAIT;
204 if ((lsf & LINUX_SA_ONSTACK) != 0)
205 bsf |= SA_ONSTACK;
206 if ((lsf & LINUX_SA_RESTART) != 0)
207 bsf |= SA_RESTART;
208 if ((lsf & LINUX_SA_ONESHOT) != 0)
209 bsf |= SA_RESETHAND;
210 if ((lsf & LINUX_SA_NOMASK) != 0)
211 bsf |= SA_NODEFER;
212 if ((lsf & LINUX_SA_SIGINFO) != 0)
213 bsf |= SA_SIGINFO;
214 if ((lsf & ~LINUX_SA_ALLBITS) != 0)
215 DPRINTF(("linux_old_to_native_sigflags: "
216 "%lx extra bits ignored\n", lsf));
217 return bsf;
218 }
219
220 /*
221 * Convert between Linux and BSD sigaction structures. Linux sometimes
222 * has one extra field (sa_restorer) which we don't support.
223 */
224 void
225 linux_old_to_native_sigaction(bsa, lsa)
226 struct sigaction *bsa;
227 const struct linux_old_sigaction *lsa;
228 {
229 bsa->sa_handler = lsa->linux_sa_handler;
230 linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
231 bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
232 #ifndef __alpha__
233 /*
234 * XXX: On the alpha sa_restorer is elsewhere.
235 */
236 if (lsa->linux_sa_restorer != NULL)
237 DPRINTF(("linux_old_to_native_sigaction: "
238 "sa_restorer ignored\n"));
239 #endif /* !__alpha__ */
240 }
241
242 void
243 native_to_linux_old_sigaction(lsa, bsa)
244 struct linux_old_sigaction *lsa;
245 const struct sigaction *bsa;
246 {
247 lsa->linux_sa_handler = bsa->sa_handler;
248 native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
249 lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
250 #ifndef __alpha__
251 lsa->linux_sa_restorer = NULL;
252 #endif
253 }
254
255 /* ...and the new sigaction conversion funcs. */
256 void
257 linux_to_native_sigaction(bsa, lsa)
258 struct sigaction *bsa;
259 const struct linux_sigaction *lsa;
260 {
261 bsa->sa_handler = lsa->linux_sa_handler;
262 linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
263 bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
264 #ifndef __alpha__
265 if (lsa->linux_sa_restorer != 0)
266 DPRINTF(("linux_to_native_sigaction: sa_restorer ignored\n"));
267 #endif
268 }
269
270 void
271 native_to_linux_sigaction(lsa, bsa)
272 struct linux_sigaction *lsa;
273 const struct sigaction *bsa;
274 {
275 lsa->linux_sa_handler = bsa->sa_handler;
276 native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
277 lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
278 #ifndef __alpha__
279 lsa->linux_sa_restorer = NULL;
280 #endif
281 }
282
283 /* ----------------------------------------------------------------------- */
284
285 /*
286 * The Linux sigaction() system call. Do the usual conversions,
287 * and just call sigaction(). Some flags and values are silently
288 * ignored (see above).
289 */
290 int
291 linux_sys_rt_sigaction(l, v, retval)
292 struct lwp *l;
293 void *v;
294 register_t *retval;
295 {
296 struct linux_sys_rt_sigaction_args /* {
297 syscallarg(int) signum;
298 syscallarg(const struct linux_sigaction *) nsa;
299 syscallarg(struct linux_sigaction *) osa;
300 syscallarg(size_t) sigsetsize;
301 } */ *uap = v;
302 struct proc *p = l->l_proc;
303 struct linux_sigaction nlsa, olsa;
304 struct sigaction nbsa, obsa;
305 int error, sig;
306
307 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
308 return (EINVAL);
309
310 if (SCARG(uap, nsa)) {
311 error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
312 if (error)
313 return (error);
314 linux_to_native_sigaction(&nbsa, &nlsa);
315 }
316 sig = SCARG(uap, signum);
317 if (sig < 0 || sig >= LINUX__NSIG)
318 return (EINVAL);
319 if (sig > 0 && !linux_to_native_signo[sig]) {
320 /* Pretend that we did something useful for unknown signals. */
321 obsa.sa_handler = SIG_IGN;
322 sigemptyset(&obsa.sa_mask);
323 obsa.sa_flags = 0;
324 } else {
325 error = sigaction1(p, linux_to_native_signo[sig],
326 SCARG(uap, nsa) ? &nbsa : NULL,
327 SCARG(uap, osa) ? &obsa : NULL,
328 NULL, 0);
329 if (error)
330 return (error);
331 }
332 if (SCARG(uap, osa)) {
333 native_to_linux_sigaction(&olsa, &obsa);
334 error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
335 if (error)
336 return (error);
337 }
338 return (0);
339 }
340
341 int
342 linux_sigprocmask1(p, how, set, oset)
343 struct proc *p;
344 int how;
345 const linux_old_sigset_t *set;
346 linux_old_sigset_t *oset;
347 {
348 linux_old_sigset_t nlss, olss;
349 sigset_t nbss, obss;
350 int error;
351
352 switch (how) {
353 case LINUX_SIG_BLOCK:
354 how = SIG_BLOCK;
355 break;
356 case LINUX_SIG_UNBLOCK:
357 how = SIG_UNBLOCK;
358 break;
359 case LINUX_SIG_SETMASK:
360 how = SIG_SETMASK;
361 break;
362 default:
363 return (EINVAL);
364 }
365
366 if (set) {
367 error = copyin(set, &nlss, sizeof(nlss));
368 if (error)
369 return (error);
370 linux_old_to_native_sigset(&nbss, &nlss);
371 }
372 error = sigprocmask1(p, how,
373 set ? &nbss : NULL, oset ? &obss : NULL);
374 if (error)
375 return (error);
376 if (oset) {
377 native_to_linux_old_sigset(&olss, &obss);
378 error = copyout(&olss, oset, sizeof(olss));
379 if (error)
380 return (error);
381 }
382 return (error);
383 }
384
385 int
386 linux_sys_rt_sigprocmask(l, v, retval)
387 struct lwp *l;
388 void *v;
389 register_t *retval;
390 {
391 struct linux_sys_rt_sigprocmask_args /* {
392 syscallarg(int) how;
393 syscallarg(const linux_sigset_t *) set;
394 syscallarg(linux_sigset_t *) oset;
395 syscallarg(size_t) sigsetsize;
396 } */ *uap = v;
397 struct proc *p = l->l_proc;
398 linux_sigset_t nlss, olss, *oset;
399 const linux_sigset_t *set;
400 sigset_t nbss, obss;
401 int error, how;
402
403 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
404 return (EINVAL);
405
406 switch (SCARG(uap, how)) {
407 case LINUX_SIG_BLOCK:
408 how = SIG_BLOCK;
409 break;
410 case LINUX_SIG_UNBLOCK:
411 how = SIG_UNBLOCK;
412 break;
413 case LINUX_SIG_SETMASK:
414 how = SIG_SETMASK;
415 break;
416 default:
417 return (EINVAL);
418 }
419
420 set = SCARG(uap, set);
421 oset = SCARG(uap, oset);
422
423 if (set) {
424 error = copyin(set, &nlss, sizeof(nlss));
425 if (error)
426 return (error);
427 linux_to_native_sigset(&nbss, &nlss);
428 }
429 error = sigprocmask1(p, how,
430 set ? &nbss : NULL, oset ? &obss : NULL);
431 if (!error && oset) {
432 native_to_linux_sigset(&olss, &obss);
433 error = copyout(&olss, oset, sizeof(olss));
434 }
435 return (error);
436 }
437
438 int
439 linux_sys_rt_sigpending(l, v, retval)
440 struct lwp *l;
441 void *v;
442 register_t *retval;
443 {
444 struct linux_sys_rt_sigpending_args /* {
445 syscallarg(linux_sigset_t *) set;
446 syscallarg(size_t) sigsetsize;
447 } */ *uap = v;
448 struct proc *p = l->l_proc;
449 sigset_t bss;
450 linux_sigset_t lss;
451
452 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
453 return (EINVAL);
454
455 sigpending1(p, &bss);
456 native_to_linux_sigset(&lss, &bss);
457 return copyout(&lss, SCARG(uap, set), sizeof(lss));
458 }
459
460 #ifndef __amd64__
461 int
462 linux_sys_sigpending(l, v, retval)
463 struct lwp *l;
464 void *v;
465 register_t *retval;
466 {
467 struct linux_sys_sigpending_args /* {
468 syscallarg(linux_old_sigset_t *) mask;
469 } */ *uap = v;
470 struct proc *p = l->l_proc;
471 sigset_t bss;
472 linux_old_sigset_t lss;
473
474 sigpending1(p, &bss);
475 native_to_linux_old_sigset(&lss, &bss);
476 return copyout(&lss, SCARG(uap, set), sizeof(lss));
477 }
478
479 int
480 linux_sys_sigsuspend(l, v, retval)
481 struct lwp *l;
482 void *v;
483 register_t *retval;
484 {
485 struct linux_sys_sigsuspend_args /* {
486 syscallarg(caddr_t) restart;
487 syscallarg(int) oldmask;
488 syscallarg(int) mask;
489 } */ *uap = v;
490 struct proc *p = l->l_proc;
491 linux_old_sigset_t lss;
492 sigset_t bss;
493
494 lss = SCARG(uap, mask);
495 linux_old_to_native_sigset(&bss, &lss);
496 return (sigsuspend1(p, &bss));
497 }
498 #endif /* __amd64__ */
499
500 int
501 linux_sys_rt_sigsuspend(l, v, retval)
502 struct lwp *l;
503 void *v;
504 register_t *retval;
505 {
506 struct linux_sys_rt_sigsuspend_args /* {
507 syscallarg(linux_sigset_t *) unewset;
508 syscallarg(size_t) sigsetsize;
509 } */ *uap = v;
510 struct proc *p = l->l_proc;
511 linux_sigset_t lss;
512 sigset_t bss;
513 int error;
514
515 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
516 return (EINVAL);
517
518 error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
519 if (error)
520 return (error);
521
522 linux_to_native_sigset(&bss, &lss);
523
524 return (sigsuspend1(p, &bss));
525 }
526
527 /*
528 * Once more: only a signal conversion is needed.
529 * Note: also used as sys_rt_queueinfo. The info field is ignored.
530 */
531 int
532 linux_sys_rt_queueinfo(l, v, retval)
533 struct lwp *l;
534 void *v;
535 register_t *retval;
536 {
537 /* XXX XAX This isn't this really int, int, siginfo_t *, is it? */
538 #if 0
539 struct linux_sys_rt_queueinfo_args /* {
540 syscallarg(int) pid;
541 syscallarg(int) signum;
542 syscallarg(siginfo_t *) uinfo;
543 } */ *uap = v;
544 #endif
545
546 /* XXX To really implement this we need to */
547 /* XXX keep a list of queued signals somewhere. */
548 return (linux_sys_kill(l, v, retval));
549 }
550
551 int
552 linux_sys_kill(l, v, retval)
553 struct lwp *l;
554 void *v;
555 register_t *retval;
556 {
557 struct linux_sys_kill_args /* {
558 syscallarg(int) pid;
559 syscallarg(int) signum;
560 } */ *uap = v;
561
562 struct sys_kill_args ka;
563 int sig;
564
565 SCARG(&ka, pid) = SCARG(uap, pid);
566 sig = SCARG(uap, signum);
567 if (sig < 0 || sig >= LINUX__NSIG)
568 return (EINVAL);
569 SCARG(&ka, signum) = linux_to_native_signo[sig];
570 return sys_kill(l, &ka, retval);
571 }
572
573 #ifdef LINUX_SS_ONSTACK
574 static void linux_to_native_sigaltstack __P((struct sigaltstack *,
575 const struct linux_sigaltstack *));
576
577 static void
578 linux_to_native_sigaltstack(bss, lss)
579 struct sigaltstack *bss;
580 const struct linux_sigaltstack *lss;
581 {
582 bss->ss_sp = lss->ss_sp;
583 bss->ss_size = lss->ss_size;
584 if (lss->ss_flags & LINUX_SS_ONSTACK)
585 bss->ss_flags = SS_ONSTACK;
586 else if (lss->ss_flags & LINUX_SS_DISABLE)
587 bss->ss_flags = SS_DISABLE;
588 else
589 bss->ss_flags = 0;
590 }
591
592 void
593 native_to_linux_sigaltstack(lss, bss)
594 struct linux_sigaltstack *lss;
595 const struct sigaltstack *bss;
596 {
597 lss->ss_sp = bss->ss_sp;
598 lss->ss_size = bss->ss_size;
599 if (bss->ss_flags & SS_ONSTACK)
600 lss->ss_flags = LINUX_SS_ONSTACK;
601 else if (bss->ss_flags & SS_DISABLE)
602 lss->ss_flags = LINUX_SS_DISABLE;
603 else
604 lss->ss_flags = 0;
605 }
606 #endif /* LINUX_SS_ONSTACK */
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