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