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