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