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