linux_signal.c revision 1.31.2.5 1 /* $NetBSD: linux_signal.c,v 1.31.2.5 2002/04/17 00:05:12 nathanw 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.31.2.5 2002/04/17 00:05:12 nathanw 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/lwp.h>
65 #include <sys/proc.h>
66 #include <sys/filedesc.h>
67 #include <sys/ioctl.h>
68 #include <sys/mount.h>
69 #include <sys/kernel.h>
70 #include <sys/signal.h>
71 #include <sys/signalvar.h>
72 #include <sys/malloc.h>
73
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
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->sa_handler;
230 linux_old_to_native_sigset(&bsa->sa_mask, &lsa->sa_mask);
231 bsa->sa_flags = linux_to_native_sigflags(lsa->sa_flags);
232 #ifndef __alpha__
233 /*
234 * XXX: On the alpha sa_restorer is elsewhere.
235 */
236 if (lsa->sa_restorer != NULL)
237 DPRINTF(("linux_old_to_native_sigaction: "
238 "sa_restorer ignored\n"));
239 #endif
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->sa_handler = bsa->sa_handler;
248 native_to_linux_old_sigset(&lsa->sa_mask, &bsa->sa_mask);
249 lsa->sa_flags = native_to_linux_sigflags(bsa->sa_flags);
250 #ifndef __alpha__
251 lsa->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->sa_handler;
262 linux_to_native_sigset(&bsa->sa_mask, &lsa->sa_mask);
263 bsa->sa_flags = linux_to_native_sigflags(lsa->sa_flags);
264 #ifndef __alpha__
265 if (lsa->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->sa_handler = bsa->sa_handler;
276 native_to_linux_sigset(&lsa->sa_mask, &bsa->sa_mask);
277 lsa->sa_flags = native_to_linux_sigflags(bsa->sa_flags);
278 #ifndef __alpha__
279 lsa->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, SCARG(uap, osa) ? &obsa : NULL);
327 if (error)
328 return (error);
329 }
330 if (SCARG(uap, osa)) {
331 native_to_linux_sigaction(&olsa, &obsa);
332 error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
333 if (error)
334 return (error);
335 }
336 return (0);
337 }
338
339 int
340 linux_sigprocmask1(p, how, set, oset)
341 struct proc *p;
342 int how;
343 const linux_old_sigset_t *set;
344 linux_old_sigset_t *oset;
345 {
346 linux_old_sigset_t nlss, olss;
347 sigset_t nbss, obss;
348 int error;
349
350 switch (how) {
351 case LINUX_SIG_BLOCK:
352 how = SIG_BLOCK;
353 break;
354 case LINUX_SIG_UNBLOCK:
355 how = SIG_UNBLOCK;
356 break;
357 case LINUX_SIG_SETMASK:
358 how = SIG_SETMASK;
359 break;
360 default:
361 return (EINVAL);
362 }
363
364 if (set) {
365 error = copyin(set, &nlss, sizeof(nlss));
366 if (error)
367 return (error);
368 linux_old_to_native_sigset(&nbss, &nlss);
369 }
370 error = sigprocmask1(p, how,
371 set ? &nbss : NULL, oset ? &obss : NULL);
372 if (error)
373 return (error);
374 if (oset) {
375 native_to_linux_old_sigset(&olss, &obss);
376 error = copyout(&olss, oset, sizeof(olss));
377 if (error)
378 return (error);
379 }
380 return (error);
381 }
382
383 int
384 linux_sys_rt_sigprocmask(l, v, retval)
385 struct lwp *l;
386 void *v;
387 register_t *retval;
388 {
389 struct linux_sys_rt_sigprocmask_args /* {
390 syscallarg(int) how;
391 syscallarg(const linux_sigset_t *) set;
392 syscallarg(linux_sigset_t *) oset;
393 syscallarg(size_t) sigsetsize;
394 } */ *uap = v;
395 struct proc *p = l->l_proc;
396 linux_sigset_t nlss, olss, *oset;
397 const linux_sigset_t *set;
398 sigset_t nbss, obss;
399 int error, how;
400
401 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
402 return (EINVAL);
403
404 switch (SCARG(uap, how)) {
405 case LINUX_SIG_BLOCK:
406 how = SIG_BLOCK;
407 break;
408 case LINUX_SIG_UNBLOCK:
409 how = SIG_UNBLOCK;
410 break;
411 case LINUX_SIG_SETMASK:
412 how = SIG_SETMASK;
413 break;
414 default:
415 return (EINVAL);
416 }
417
418 set = SCARG(uap, set);
419 oset = SCARG(uap, oset);
420
421 if (set) {
422 error = copyin(set, &nlss, sizeof(nlss));
423 if (error)
424 return (error);
425 linux_to_native_sigset(&nbss, &nlss);
426 }
427 error = sigprocmask1(p, how,
428 set ? &nbss : NULL, oset ? &obss : NULL);
429 if (!error && oset) {
430 native_to_linux_sigset(&olss, &obss);
431 error = copyout(&olss, oset, sizeof(olss));
432 }
433 return (error);
434 }
435
436 int
437 linux_sys_rt_sigpending(l, v, retval)
438 struct lwp *l;
439 void *v;
440 register_t *retval;
441 {
442 struct linux_sys_rt_sigpending_args /* {
443 syscallarg(linux_sigset_t *) set;
444 syscallarg(size_t) sigsetsize;
445 } */ *uap = v;
446 struct proc *p = l->l_proc;
447 sigset_t bss;
448 linux_sigset_t lss;
449
450 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
451 return (EINVAL);
452
453 sigpending1(p, &bss);
454 native_to_linux_sigset(&lss, &bss);
455 return copyout(&lss, SCARG(uap, set), sizeof(lss));
456 }
457
458 int
459 linux_sys_sigpending(l, v, retval)
460 struct lwp *l;
461 void *v;
462 register_t *retval;
463 {
464 struct linux_sys_sigpending_args /* {
465 syscallarg(linux_old_sigset_t *) mask;
466 } */ *uap = v;
467 struct proc *p = l->l_proc;
468 sigset_t bss;
469 linux_old_sigset_t lss;
470
471 sigpending1(p, &bss);
472 native_to_linux_old_sigset(&lss, &bss);
473 return copyout(&lss, SCARG(uap, set), sizeof(lss));
474 }
475
476 int
477 linux_sys_sigsuspend(l, v, retval)
478 struct lwp *l;
479 void *v;
480 register_t *retval;
481 {
482 struct linux_sys_sigsuspend_args /* {
483 syscallarg(caddr_t) restart;
484 syscallarg(int) oldmask;
485 syscallarg(int) mask;
486 } */ *uap = v;
487 struct proc *p = l->l_proc;
488 linux_old_sigset_t lss;
489 sigset_t bss;
490
491 lss = SCARG(uap, mask);
492 linux_old_to_native_sigset(&bss, &lss);
493 return (sigsuspend1(p, &bss));
494 }
495 int
496 linux_sys_rt_sigsuspend(l, v, retval)
497 struct lwp *l;
498 void *v;
499 register_t *retval;
500 {
501 struct linux_sys_rt_sigsuspend_args /* {
502 syscallarg(linux_sigset_t *) unewset;
503 syscallarg(size_t) sigsetsize;
504 } */ *uap = v;
505 struct proc *p = l->l_proc;
506 linux_sigset_t lss;
507 sigset_t bss;
508 int error;
509
510 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
511 return (EINVAL);
512
513 error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
514 if (error)
515 return (error);
516
517 linux_to_native_sigset(&bss, &lss);
518
519 return (sigsuspend1(p, &bss));
520 }
521
522 /*
523 * Once more: only a signal conversion is needed.
524 * Note: also used as sys_rt_queueinfo. The info field is ignored.
525 */
526 int
527 linux_sys_rt_queueinfo(l, v, retval)
528 struct lwp *l;
529 void *v;
530 register_t *retval;
531 {
532 /* XXX XAX This isn't this really int, int, siginfo_t *, is it? */
533 #if 0
534 struct linux_sys_rt_queueinfo_args /* {
535 syscallarg(int) pid;
536 syscallarg(int) signum;
537 syscallarg(siginfo_t *) uinfo;
538 } */ *uap = v;
539 #endif
540
541 /* XXX To really implement this we need to */
542 /* XXX keep a list of queued signals somewhere. */
543 return (linux_sys_kill(l, v, retval));
544 }
545
546 int
547 linux_sys_kill(l, v, retval)
548 struct lwp *l;
549 void *v;
550 register_t *retval;
551 {
552 struct linux_sys_kill_args /* {
553 syscallarg(int) pid;
554 syscallarg(int) signum;
555 } */ *uap = v;
556
557 struct sys_kill_args ka;
558 int sig;
559
560 SCARG(&ka, pid) = SCARG(uap, pid);
561 sig = SCARG(uap, signum);
562 if (sig < 0 || sig >= LINUX__NSIG)
563 return (EINVAL);
564 SCARG(&ka, signum) = linux_to_native_signo[sig];
565 return sys_kill(l, &ka, retval);
566 }
567
568 #ifdef LINUX_SS_ONSTACK
569 static void linux_to_native_sigaltstack __P((struct sigaltstack *,
570 const struct linux_sigaltstack *));
571 static void native_to_linux_sigaltstack __P((struct linux_sigaltstack *,
572 const struct sigaltstack *));
573
574 static void
575 linux_to_native_sigaltstack(bss, lss)
576 struct sigaltstack *bss;
577 const struct linux_sigaltstack *lss;
578 {
579 bss->ss_sp = lss->ss_sp;
580 bss->ss_size = lss->ss_size;
581 if (lss->ss_flags & LINUX_SS_ONSTACK)
582 bss->ss_flags = SS_ONSTACK;
583 else if (lss->ss_flags & LINUX_SS_DISABLE)
584 bss->ss_flags = SS_DISABLE;
585 else
586 bss->ss_flags = 0;
587 }
588
589 static void
590 native_to_linux_sigaltstack(lss, bss)
591 struct linux_sigaltstack *lss;
592 const struct sigaltstack *bss;
593 {
594 lss->ss_sp = bss->ss_sp;
595 lss->ss_size = bss->ss_size;
596 if (bss->ss_flags & SS_ONSTACK)
597 lss->ss_flags = LINUX_SS_ONSTACK;
598 else if (bss->ss_flags & SS_DISABLE)
599 lss->ss_flags = LINUX_SS_DISABLE;
600 else
601 lss->ss_flags = 0;
602 }
603
604 int
605 linux_sys_sigaltstack(l, v, retval)
606 struct lwp *l;
607 void *v;
608 register_t *retval;
609 {
610 struct linux_sys_sigaltstack_args /* {
611 syscallarg(const struct linux_sigaltstack *) ss;
612 syscallarg(struct linux_sigaltstack *) oss;
613 } */ *uap = v;
614 struct proc *p = l->l_proc;
615 struct linux_sigaltstack ss;
616 struct sigaltstack nss, oss;
617 int error;
618
619 if (SCARG(uap, ss) != NULL) {
620 if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
621 return error;
622 linux_to_native_sigaltstack(&nss, &ss);
623 }
624
625 error = sigaltstack1(p,
626 SCARG(uap, ss) ? &nss : NULL, SCARG(uap, oss) ? &oss : NULL);
627 if (error)
628 return error;
629
630 if (SCARG(uap, oss) != NULL) {
631 native_to_linux_sigaltstack(&ss, &oss);
632 if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
633 return error;
634 }
635 return 0;
636 }
637 #endif
638