linux_signal.c revision 1.31.4.4 1 /* $NetBSD: linux_signal.c,v 1.31.4.4 2002/09/06 08:43:21 jdolecek 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.4.4 2002/09/06 08:43:21 jdolecek 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 #ifdef DEBUG_LINUX
90 #define DPRINTF(a) uprintf a
91 #else
92 #define DPRINTF(a)
93 #endif
94
95 extern const int native_to_linux_signo[];
96 extern const int linux_to_native_signo[];
97
98 /*
99 * Convert between Linux and BSD signal sets.
100 */
101 #if LINUX__NSIG_WORDS > 1
102 void
103 linux_old_extra_to_native_sigset(bss, lss, extra)
104 sigset_t *bss;
105 const linux_old_sigset_t *lss;
106 const unsigned long *extra;
107 {
108 linux_sigset_t lsnew;
109
110 /* convert old sigset to new sigset */
111 linux_sigemptyset(&lsnew);
112 lsnew.sig[0] = *lss;
113 if (extra)
114 memcpy(&lsnew.sig[1], extra,
115 sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
116
117 linux_to_native_sigset(bss, &lsnew);
118 }
119
120 void
121 native_to_linux_old_extra_sigset(lss, extra, bss)
122 linux_old_sigset_t *lss;
123 unsigned long *extra;
124 const sigset_t *bss;
125 {
126 linux_sigset_t lsnew;
127
128 native_to_linux_sigset(&lsnew, bss);
129
130 /* convert new sigset to old sigset */
131 *lss = lsnew.sig[0];
132 if (extra)
133 memcpy(extra, &lsnew.sig[1],
134 sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
135 }
136 #endif
137
138 void
139 linux_to_native_sigset(bss, lss)
140 sigset_t *bss;
141 const linux_sigset_t *lss;
142 {
143 int i, newsig;
144
145 sigemptyset(bss);
146 for (i = 1; i < LINUX__NSIG; i++) {
147 if (linux_sigismember(lss, i)) {
148 newsig = linux_to_native_signo[i];
149 if (newsig)
150 sigaddset(bss, newsig);
151 }
152 }
153 }
154
155 void
156 native_to_linux_sigset(lss, bss)
157 linux_sigset_t *lss;
158 const sigset_t *bss;
159 {
160 int i, newsig;
161
162 linux_sigemptyset(lss);
163 for (i = 1; i < NSIG; i++) {
164 if (sigismember(bss, i)) {
165 newsig = native_to_linux_signo[i];
166 if (newsig)
167 linux_sigaddset(lss, newsig);
168 }
169 }
170 }
171
172 unsigned int
173 native_to_linux_sigflags(bsf)
174 const int bsf;
175 {
176 unsigned int lsf = 0;
177 if ((bsf & SA_NOCLDSTOP) != 0)
178 lsf |= LINUX_SA_NOCLDSTOP;
179 if ((bsf & SA_NOCLDWAIT) != 0)
180 lsf |= LINUX_SA_NOCLDWAIT;
181 if ((bsf & SA_ONSTACK) != 0)
182 lsf |= LINUX_SA_ONSTACK;
183 if ((bsf & SA_RESTART) != 0)
184 lsf |= LINUX_SA_RESTART;
185 if ((bsf & SA_NODEFER) != 0)
186 lsf |= LINUX_SA_NOMASK;
187 if ((bsf & SA_RESETHAND) != 0)
188 lsf |= LINUX_SA_ONESHOT;
189 if ((bsf & SA_SIGINFO) != 0)
190 lsf |= LINUX_SA_SIGINFO;
191 return lsf;
192 }
193
194 int
195 linux_to_native_sigflags(lsf)
196 const unsigned long lsf;
197 {
198 int bsf = 0;
199 if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
200 bsf |= SA_NOCLDSTOP;
201 if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
202 bsf |= SA_NOCLDWAIT;
203 if ((lsf & LINUX_SA_ONSTACK) != 0)
204 bsf |= SA_ONSTACK;
205 if ((lsf & LINUX_SA_RESTART) != 0)
206 bsf |= SA_RESTART;
207 if ((lsf & LINUX_SA_ONESHOT) != 0)
208 bsf |= SA_RESETHAND;
209 if ((lsf & LINUX_SA_NOMASK) != 0)
210 bsf |= SA_NODEFER;
211 if ((lsf & LINUX_SA_SIGINFO) != 0)
212 bsf |= SA_SIGINFO;
213 if ((lsf & ~LINUX_SA_ALLBITS) != 0)
214 DPRINTF(("linux_old_to_native_sigflags: "
215 "%lx extra bits ignored\n", lsf));
216 return bsf;
217 }
218
219 /*
220 * Convert between Linux and BSD sigaction structures. Linux sometimes
221 * has one extra field (sa_restorer) which we don't support.
222 */
223 void
224 linux_old_to_native_sigaction(bsa, lsa)
225 struct sigaction *bsa;
226 const struct linux_old_sigaction *lsa;
227 {
228 bsa->sa_handler = lsa->sa_handler;
229 linux_old_to_native_sigset(&bsa->sa_mask, &lsa->sa_mask);
230 bsa->sa_flags = linux_to_native_sigflags(lsa->sa_flags);
231 #ifndef __alpha__
232 /*
233 * XXX: On the alpha sa_restorer is elsewhere.
234 */
235 if (lsa->sa_restorer != NULL)
236 DPRINTF(("linux_old_to_native_sigaction: "
237 "sa_restorer ignored\n"));
238 #endif
239 }
240
241 void
242 native_to_linux_old_sigaction(lsa, bsa)
243 struct linux_old_sigaction *lsa;
244 const struct sigaction *bsa;
245 {
246 lsa->sa_handler = bsa->sa_handler;
247 native_to_linux_old_sigset(&lsa->sa_mask, &bsa->sa_mask);
248 lsa->sa_flags = native_to_linux_sigflags(bsa->sa_flags);
249 #ifndef __alpha__
250 lsa->sa_restorer = NULL;
251 #endif
252 }
253
254 /* ...and the new sigaction conversion funcs. */
255 void
256 linux_to_native_sigaction(bsa, lsa)
257 struct sigaction *bsa;
258 const struct linux_sigaction *lsa;
259 {
260 bsa->sa_handler = lsa->sa_handler;
261 linux_to_native_sigset(&bsa->sa_mask, &lsa->sa_mask);
262 bsa->sa_flags = linux_to_native_sigflags(lsa->sa_flags);
263 #ifndef __alpha__
264 if (lsa->sa_restorer != 0)
265 DPRINTF(("linux_to_native_sigaction: sa_restorer ignored\n"));
266 #endif
267 }
268
269 void
270 native_to_linux_sigaction(lsa, bsa)
271 struct linux_sigaction *lsa;
272 const struct sigaction *bsa;
273 {
274 lsa->sa_handler = bsa->sa_handler;
275 native_to_linux_sigset(&lsa->sa_mask, &bsa->sa_mask);
276 lsa->sa_flags = native_to_linux_sigflags(bsa->sa_flags);
277 #ifndef __alpha__
278 lsa->sa_restorer = NULL;
279 #endif
280 }
281
282 /* ----------------------------------------------------------------------- */
283
284 /*
285 * The Linux sigaction() system call. Do the usual conversions,
286 * and just call sigaction(). Some flags and values are silently
287 * ignored (see above).
288 */
289 int
290 linux_sys_rt_sigaction(p, v, retval)
291 struct proc *p;
292 void *v;
293 register_t *retval;
294 {
295 struct linux_sys_rt_sigaction_args /* {
296 syscallarg(int) signum;
297 syscallarg(const struct linux_sigaction *) nsa;
298 syscallarg(struct linux_sigaction *) osa;
299 syscallarg(size_t) sigsetsize;
300 } */ *uap = v;
301 struct linux_sigaction nlsa, olsa;
302 struct sigaction nbsa, obsa;
303 int error, sig;
304
305 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
306 return (EINVAL);
307
308 if (SCARG(uap, nsa)) {
309 error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
310 if (error)
311 return (error);
312 linux_to_native_sigaction(&nbsa, &nlsa);
313 }
314 sig = SCARG(uap, signum);
315 if (sig < 0 || sig >= LINUX__NSIG)
316 return (EINVAL);
317 if (sig > 0 && !linux_to_native_signo[sig]) {
318 /* Pretend that we did something useful for unknown signals. */
319 obsa.sa_handler = SIG_IGN;
320 sigemptyset(&obsa.sa_mask);
321 obsa.sa_flags = 0;
322 } else {
323 error = sigaction1(p, linux_to_native_signo[sig],
324 SCARG(uap, nsa) ? &nbsa : NULL,
325 SCARG(uap, osa) ? &obsa : NULL,
326 NULL, 0);
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(p, v, retval)
385 struct proc *p;
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
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(p, v, retval)
438 struct proc *p;
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 sigset_t bss;
447 linux_sigset_t lss;
448
449 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
450 return (EINVAL);
451
452 sigpending1(p, &bss);
453 native_to_linux_sigset(&lss, &bss);
454 return copyout(&lss, SCARG(uap, set), sizeof(lss));
455 }
456
457 int
458 linux_sys_sigpending(p, v, retval)
459 struct proc *p;
460 void *v;
461 register_t *retval;
462 {
463 struct linux_sys_sigpending_args /* {
464 syscallarg(linux_old_sigset_t *) mask;
465 } */ *uap = v;
466 sigset_t bss;
467 linux_old_sigset_t lss;
468
469 sigpending1(p, &bss);
470 native_to_linux_old_sigset(&lss, &bss);
471 return copyout(&lss, SCARG(uap, set), sizeof(lss));
472 }
473
474 int
475 linux_sys_sigsuspend(p, v, retval)
476 struct proc *p;
477 void *v;
478 register_t *retval;
479 {
480 struct linux_sys_sigsuspend_args /* {
481 syscallarg(caddr_t) restart;
482 syscallarg(int) oldmask;
483 syscallarg(int) mask;
484 } */ *uap = v;
485 linux_old_sigset_t lss;
486 sigset_t bss;
487
488 lss = SCARG(uap, mask);
489 linux_old_to_native_sigset(&bss, &lss);
490 return (sigsuspend1(p, &bss));
491 }
492 int
493 linux_sys_rt_sigsuspend(p, v, retval)
494 struct proc *p;
495 void *v;
496 register_t *retval;
497 {
498 struct linux_sys_rt_sigsuspend_args /* {
499 syscallarg(linux_sigset_t *) unewset;
500 syscallarg(size_t) sigsetsize;
501 } */ *uap = v;
502 linux_sigset_t lss;
503 sigset_t bss;
504 int error;
505
506 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
507 return (EINVAL);
508
509 error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
510 if (error)
511 return (error);
512
513 linux_to_native_sigset(&bss, &lss);
514
515 return (sigsuspend1(p, &bss));
516 }
517
518 /*
519 * Once more: only a signal conversion is needed.
520 * Note: also used as sys_rt_queueinfo. The info field is ignored.
521 */
522 int
523 linux_sys_rt_queueinfo(p, v, retval)
524 struct proc *p;
525 void *v;
526 register_t *retval;
527 {
528 /* XXX XAX This isn't this really int, int, siginfo_t *, is it? */
529 #if 0
530 struct linux_sys_rt_queueinfo_args /* {
531 syscallarg(int) pid;
532 syscallarg(int) signum;
533 syscallarg(siginfo_t *) uinfo;
534 } */ *uap = v;
535 #endif
536
537 /* XXX To really implement this we need to */
538 /* XXX keep a list of queued signals somewhere. */
539 return (linux_sys_kill(p, v, retval));
540 }
541
542 int
543 linux_sys_kill(p, v, retval)
544 struct proc *p;
545 void *v;
546 register_t *retval;
547 {
548 struct linux_sys_kill_args /* {
549 syscallarg(int) pid;
550 syscallarg(int) signum;
551 } */ *uap = v;
552 struct sys_kill_args ka;
553 int sig;
554
555 SCARG(&ka, pid) = SCARG(uap, pid);
556 sig = SCARG(uap, signum);
557 if (sig < 0 || sig >= LINUX__NSIG)
558 return (EINVAL);
559 SCARG(&ka, signum) = linux_to_native_signo[sig];
560 return sys_kill(p, &ka, retval);
561 }
562
563 #ifdef LINUX_SS_ONSTACK
564 static void linux_to_native_sigaltstack __P((struct sigaltstack *,
565 const struct linux_sigaltstack *));
566 static void native_to_linux_sigaltstack __P((struct linux_sigaltstack *,
567 const struct sigaltstack *));
568
569 static void
570 linux_to_native_sigaltstack(bss, lss)
571 struct sigaltstack *bss;
572 const struct linux_sigaltstack *lss;
573 {
574 bss->ss_sp = lss->ss_sp;
575 bss->ss_size = lss->ss_size;
576 if (lss->ss_flags & LINUX_SS_ONSTACK)
577 bss->ss_flags = SS_ONSTACK;
578 else if (lss->ss_flags & LINUX_SS_DISABLE)
579 bss->ss_flags = SS_DISABLE;
580 else
581 bss->ss_flags = 0;
582 }
583
584 static void
585 native_to_linux_sigaltstack(lss, bss)
586 struct linux_sigaltstack *lss;
587 const struct sigaltstack *bss;
588 {
589 lss->ss_sp = bss->ss_sp;
590 lss->ss_size = bss->ss_size;
591 if (bss->ss_flags & SS_ONSTACK)
592 lss->ss_flags = LINUX_SS_ONSTACK;
593 else if (bss->ss_flags & SS_DISABLE)
594 lss->ss_flags = LINUX_SS_DISABLE;
595 else
596 lss->ss_flags = 0;
597 }
598
599 int
600 linux_sys_sigaltstack(p, v, retval)
601 struct proc *p;
602 void *v;
603 register_t *retval;
604 {
605 struct linux_sys_sigaltstack_args /* {
606 syscallarg(const struct linux_sigaltstack *) ss;
607 syscallarg(struct linux_sigaltstack *) oss;
608 } */ *uap = v;
609 struct linux_sigaltstack ss;
610 struct sigaltstack nss, oss;
611 int error;
612
613 if (SCARG(uap, ss) != NULL) {
614 if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
615 return error;
616 linux_to_native_sigaltstack(&nss, &ss);
617 }
618
619 error = sigaltstack1(p,
620 SCARG(uap, ss) ? &nss : NULL, SCARG(uap, oss) ? &oss : NULL);
621 if (error)
622 return error;
623
624 if (SCARG(uap, oss) != NULL) {
625 native_to_linux_sigaltstack(&ss, &oss);
626 if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
627 return error;
628 }
629 return 0;
630 }
631 #endif
632