Home | History | Annotate | Line # | Download | only in common
linux_signal.c revision 1.14
      1 /*	$NetBSD: linux_signal.c,v 1.14 1998/10/01 02:27:33 erh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 Eric Haszlakiewicz
      5  * Copyright (c) 1995 Frank van der Linden
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed for the NetBSD Project
     19  *      by Frank van der Linden
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * heavily from: svr4_signal.c,v 1.7 1995/01/09 01:04:21 christos Exp
     35  */
     36 
     37 /*
     38  *   Functions in multiarch:
     39  *	linux_sys_signal	: linux_sig_notalpha.c
     40  *	linux_sys_siggetmask	: linux_sig_notalpha.c
     41  *	linux_sys_sigsetmask	: linux_sig_notalpha.c
     42  *	linux_sys_pause		: linux_sig_notalpha.c
     43  *	linux_sys_sigaction	: linux_sigaction.c
     44  *
     45  */
     46 
     47 /*
     48  *   Unimplemented:
     49  *	linux_sys_rt_sigtimedwait	: sigsuspend w/timeout.
     50  */
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/namei.h>
     55 #include <sys/proc.h>
     56 #include <sys/filedesc.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/mount.h>
     59 #include <sys/kernel.h>
     60 #include <sys/signal.h>
     61 #include <sys/signalvar.h>
     62 #include <sys/malloc.h>
     63 
     64 #include <sys/syscallargs.h>
     65 
     66 #include <compat/linux/linux_types.h>
     67 #include <compat/linux/linux_signal.h>
     68 #include <compat/linux/linux_syscallargs.h>
     69 #include <compat/linux/linux_util.h>
     70 
     71 /* Locally used defines (in bsd<->linux conversion functions): */
     72 /* XXX XAX rename to linux_old.  Add stuff for new type linux_sigset_t
     73 	handle _NSIG_WORDS > 1 */
     74 #define	linux_sigmask(n)	(1 << ((n) - 1))
     75 #define	linux_sigemptyset(s)	memset((s), 0, sizeof(*(s)))
     76 #define	linux_sigismember(s, n)	(*(s) & linux_sigmask(n))
     77 #define	linux_sigaddset(s, n)	(*(s) |= linux_sigmask(n))
     78 
     79 /* Note: linux_to_native_sig[] is in <arch>/linux_sigarray.c */
     80 int native_to_linux_sig[NSIG] = {
     81 	0,
     82 	LINUX_SIGHUP,
     83 	LINUX_SIGINT,
     84 	LINUX_SIGQUIT,
     85 	LINUX_SIGILL,
     86 	LINUX_SIGTRAP,
     87 	LINUX_SIGABRT,
     88 	0,			/* SIGEMT */
     89 	LINUX_SIGFPE,
     90 	LINUX_SIGKILL,
     91 	LINUX_SIGBUS,
     92 	LINUX_SIGSEGV,
     93 	0,			/* SIGSEGV */
     94 	LINUX_SIGPIPE,
     95 	LINUX_SIGALRM,
     96 	LINUX_SIGTERM,
     97 	LINUX_SIGURG,
     98 	LINUX_SIGSTOP,
     99 	LINUX_SIGTSTP,
    100 	LINUX_SIGCONT,
    101 	LINUX_SIGCHLD,
    102 	LINUX_SIGTTIN,
    103 	LINUX_SIGTTOU,
    104 	LINUX_SIGIO,
    105 	LINUX_SIGXCPU,
    106 	LINUX_SIGXFSZ,
    107 	LINUX_SIGVTALRM,
    108 	LINUX_SIGPROF,
    109 	LINUX_SIGWINCH,
    110 	0,			/* SIGINFO */
    111 	LINUX_SIGUSR1,
    112 	LINUX_SIGUSR2,
    113 	LINUX_SIGPWR,
    114 };
    115 
    116 /*
    117  * Ok, we know that Linux and BSD signals both are just an unsigned int.
    118  * Don't bother to use the sigismember() stuff for now.
    119  */
    120 void
    121 linux_old_to_native_sigset(lss, bss)
    122 	const linux_old_sigset_t *lss;
    123 	sigset_t *bss;
    124 {
    125 	int i, newsig;
    126 
    127 	sigemptyset(bss);
    128 	for (i = 1; i < LINUX_NSIG; i++) {
    129 		if (linux_sigismember(lss, i)) {
    130 			newsig = linux_to_native_sig[i];
    131 			if (newsig)
    132 				sigaddset(bss, newsig);
    133 		}
    134 	}
    135 }
    136 
    137 void
    138 native_to_linux_old_sigset(bss, lss)
    139 	const sigset_t *bss;
    140 	linux_old_sigset_t *lss;
    141 {
    142 	int i, newsig;
    143 
    144 	linux_sigemptyset(lss);
    145 	for (i = 1; i < NSIG; i++) {
    146 		if (sigismember(bss, i)) {
    147 			newsig = native_to_linux_sig[i];
    148 			if (newsig)
    149 				linux_sigaddset(lss, newsig);
    150 		}
    151 	}
    152 }
    153 
    154 /*
    155  * Convert between Linux and BSD sigaction structures. Linux sometimes
    156  * has one extra field (sa_restorer) which we don't support.
    157  */
    158 void
    159 linux_old_to_native_sigaction(lsa, bsa)
    160 	struct linux_old_sigaction *lsa;
    161 	struct sigaction *bsa;
    162 {
    163 
    164 	bsa->sa_handler = lsa->sa_handler;
    165 	linux_old_to_native_sigset(&lsa->sa_mask, &bsa->sa_mask);
    166 	bsa->sa_flags = 0;
    167 	if ((lsa->sa_flags & LINUX_SA_NOCLDSTOP) != 0)
    168 		bsa->sa_flags |= SA_NOCLDSTOP;
    169 	if ((lsa->sa_flags & LINUX_SA_ONSTACK) != 0)
    170 		bsa->sa_flags |= SA_ONSTACK;
    171 	if ((lsa->sa_flags & LINUX_SA_RESTART) != 0)
    172 		bsa->sa_flags |= SA_RESTART;
    173 	if ((lsa->sa_flags & LINUX_SA_ONESHOT) != 0)
    174 		bsa->sa_flags |= SA_RESETHAND;
    175 	if ((lsa->sa_flags & LINUX_SA_NOMASK) != 0)
    176 		bsa->sa_flags |= SA_NODEFER;
    177 	if ((lsa->sa_flags & LINUX_SA_SIGINFO) != 0)
    178 		bsa->sa_flags |= SA_SIGINFO;
    179 #ifdef DEBUG
    180 	if ((lsa->sa_flags & ~LINUX_SA_ALLBITS) != 0)
    181 /*XXX*/		printf("linux_old_to_native_sigaction: extra bits ignored\n");
    182 	if (lsa->sa_restorer != 0)
    183 /*XXX*/		printf("linux_old_to_native_sigaction: sa_restorer ignored\n");
    184 #endif
    185 }
    186 
    187 void
    188 native_to_linux_old_sigaction(bsa, lsa)
    189 	struct sigaction *bsa;
    190 	struct linux_old_sigaction *lsa;
    191 {
    192 
    193 	/* Clear sa_flags and sa_restorer (if it exists) */
    194 	bzero(lsa, sizeof(struct linux_old_sigaction));
    195 
    196 	/* ...and fill in the mask and flags */
    197 	native_to_linux_old_sigset(&bsa->sa_mask, &lsa->sa_mask);
    198 	if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
    199 		lsa->sa_flags |= LINUX_SA_NOCLDSTOP;
    200 	if ((bsa->sa_flags & SA_ONSTACK) != 0)
    201 		lsa->sa_flags |= LINUX_SA_ONSTACK;
    202 	if ((bsa->sa_flags & SA_RESTART) != 0)
    203 		lsa->sa_flags |= LINUX_SA_RESTART;
    204 	if ((bsa->sa_flags & SA_NODEFER) != 0)
    205 		lsa->sa_flags |= LINUX_SA_NOMASK;
    206 	if ((bsa->sa_flags & SA_RESETHAND) != 0)
    207 		lsa->sa_flags |= LINUX_SA_ONESHOT;
    208 	if ((bsa->sa_flags & SA_SIGINFO) != 0)
    209 		lsa->sa_flags |= LINUX_SA_SIGINFO;
    210 	lsa->sa_handler = bsa->sa_handler;
    211 }
    212 
    213 /* ...and the new sigaction conversion funcs. */
    214 void
    215 linux_to_native_sigaction(lsa, bsa)
    216 	struct linux_sigaction *lsa;
    217 	struct sigaction *bsa;
    218 {
    219 
    220 	bsa->sa_handler = lsa->sa_handler;
    221 	linux_to_native_sigset(&lsa->sa_mask, &bsa->sa_mask);
    222 	bsa->sa_flags = 0;
    223 	if ((lsa->sa_flags & LINUX_SA_NOCLDSTOP) != 0)
    224 		bsa->sa_flags |= SA_NOCLDSTOP;
    225 	if ((lsa->sa_flags & LINUX_SA_ONSTACK) != 0)
    226 		bsa->sa_flags |= SA_ONSTACK;
    227 	if ((lsa->sa_flags & LINUX_SA_RESTART) != 0)
    228 		bsa->sa_flags |= SA_RESTART;
    229 	if ((lsa->sa_flags & LINUX_SA_ONESHOT) != 0)
    230 		bsa->sa_flags |= SA_RESETHAND;
    231 	if ((lsa->sa_flags & LINUX_SA_NOMASK) != 0)
    232 		bsa->sa_flags |= SA_NODEFER;
    233 	if ((lsa->sa_flags & LINUX_SA_SIGINFO) != 0)
    234 		bsa->sa_flags |= SA_SIGINFO;
    235 #ifdef DEBUG
    236 	if ((lsa->sa_flags & ~LINUX_SA_ALLBITS) != 0)
    237 /*XXX*/		printf("linux_to_native_sigaction: extra bits ignored\n");
    238 	if (lsa->sa_restorer != 0)
    239 /*XXX*/		printf("linux_to_native_sigaction: sa_restorer ignored\n");
    240 #endif
    241 }
    242 
    243 void
    244 native_to_linux_sigaction(bsa, lsa)
    245 	struct sigaction *bsa;
    246 	struct linux_sigaction *lsa;
    247 {
    248 
    249 	/* Clear sa_flags and sa_restorer (if it exists) */
    250 	bzero(lsa, sizeof(struct linux_sigaction));
    251 
    252 	/* ...and fill in the mask and flags */
    253 	native_to_linux_sigset(&bsa->sa_mask, &lsa->sa_mask);
    254 	if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
    255 		lsa->sa_flags |= LINUX_SA_NOCLDSTOP;
    256 	if ((bsa->sa_flags & SA_ONSTACK) != 0)
    257 		lsa->sa_flags |= LINUX_SA_ONSTACK;
    258 	if ((bsa->sa_flags & SA_RESTART) != 0)
    259 		lsa->sa_flags |= LINUX_SA_RESTART;
    260 	if ((bsa->sa_flags & SA_NODEFER) != 0)
    261 		lsa->sa_flags |= LINUX_SA_NOMASK;
    262 	if ((bsa->sa_flags & SA_RESETHAND) != 0)
    263 		lsa->sa_flags |= LINUX_SA_ONESHOT;
    264 	if ((bsa->sa_flags & SA_SIGINFO) != 0)
    265 		lsa->sa_flags |= LINUX_SA_SIGINFO;
    266 	lsa->sa_handler = bsa->sa_handler;
    267 }
    268 
    269 /* ----------------------------------------------------------------------- */
    270 
    271 /*
    272  * The Linux sigaction() system call. Do the usual conversions,
    273  * and just call sigaction(). Some flags and values are silently
    274  * ignored (see above).
    275  */
    276 int
    277 linux_sys_rt_sigaction(p, v, retval)
    278 	register struct proc *p;
    279 	void *v;
    280 	register_t *retval;
    281 {
    282 	struct linux_sys_rt_sigaction_args /* {
    283 		syscallarg(int) signum;
    284 		syscallarg(const struct linux_sigaction *) nsa;
    285 		syscallarg(struct linux_sigaction *) osa;
    286 		syscallarg(size_t) sigsetsize;
    287 	} */ *uap = v;
    288 	struct linux_sigaction nlsa, olsa;
    289 	struct sigaction nbsa, obsa;
    290 	int error;
    291 
    292 	/* XXX XAX linux_sigset_t or struct linux_sigaction here? */
    293 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    294 		return (EINVAL);
    295 
    296 	if (SCARG(uap, nsa)) {
    297 		error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
    298 		if (error)
    299 			return (error);
    300 		linux_to_native_sigaction(&nlsa, &nbsa);
    301 	}
    302 	error = sigaction1(p, linux_to_native_sig[SCARG(uap, signum)],
    303 	    SCARG(uap, nsa) ? &nbsa : 0, SCARG(uap, osa) ? &obsa : 0);
    304 	if (error)
    305 		return (error);
    306 	if (SCARG(uap, osa)) {
    307 		native_to_linux_sigaction(&obsa, &olsa);
    308 		error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
    309 		if (error)
    310 			return (error);
    311 	}
    312 	return (0);
    313 }
    314 
    315 int
    316 linux_sys_rt_sigprocmask(p, v, retval)
    317 	register struct proc *p;
    318 	void *v;
    319 	register_t *retval;
    320 {
    321 	/* Use non-rt function: sigsetsize is ignored. */
    322 	/* Assume sizeof(linux_sigset_t) == sizeof(linux_old_sigset_t) */
    323 	return(linux_sys_sigprocmask(p, v, retval));
    324 }
    325 
    326 
    327 int
    328 linux_sys_sigprocmask(p, v, retval)
    329 	register struct proc *p;
    330 	void *v;
    331 	register_t *retval;
    332 {
    333 	struct linux_sys_sigprocmask_args /* {
    334 		syscallarg(int) how;
    335 		syscallarg(const linux_old_sigset_t *) set;
    336 		syscallarg(linux_old_sigset_t *) oset;
    337 	} */ *uap = v;
    338 	linux_old_sigset_t nlss, olss;
    339 	sigset_t nbss, obss;
    340 	int how;
    341 	int error;
    342 
    343 	switch (SCARG(uap, how)) {
    344 	case LINUX_SIG_BLOCK:
    345 		how = SIG_BLOCK;
    346 		break;
    347 	case LINUX_SIG_UNBLOCK:
    348 		how = SIG_UNBLOCK;
    349 		break;
    350 	case LINUX_SIG_SETMASK:
    351 		how = SIG_SETMASK;
    352 		break;
    353 	default:
    354 		return (EINVAL);
    355 	}
    356 
    357 	if (SCARG(uap, set)) {
    358 		error = copyin(SCARG(uap, set), &nlss, sizeof(nlss));
    359 		if (error)
    360 			return (error);
    361 		linux_old_to_native_sigset(&nlss, &nbss);
    362 	}
    363 	error = sigprocmask1(p, how,
    364 	    SCARG(uap, set) ? &nbss : 0, SCARG(uap, oset) ? &obss : 0);
    365 	if (error)
    366 		return (error);
    367 	if (SCARG(uap, oset)) {
    368 		native_to_linux_old_sigset(&obss, &olss);
    369 		error = copyout(&olss, SCARG(uap, oset), sizeof(olss));
    370 		if (error)
    371 			return (error);
    372 	}
    373 	return (error);
    374 }
    375 
    376 int
    377 linux_sys_rt_sigpending(p, v, retval)
    378 	register struct proc *p;
    379 	void *v;
    380 	register_t *retval;
    381 {
    382 	struct linux_sys_rt_sigpending_args /* {
    383 		syscallarg(linux_sigset_t *) set;
    384 		syscallarg(size_t) sigsetsize;
    385 	} */ *uap = v;
    386 	sigset_t bss;
    387 	linux_sigset_t lss;
    388 
    389 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    390 		return (EINVAL);
    391 
    392 	sigpending1(p, &bss);
    393 	native_to_linux_sigset(&bss, &lss);
    394 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    395 }
    396 int
    397 linux_sys_sigpending(p, v, retval)
    398 	register struct proc *p;
    399 	void *v;
    400 	register_t *retval;
    401 {
    402 	struct linux_sys_sigpending_args /* {
    403 		syscallarg(linux_old_sigset_t *) mask;
    404 	} */ *uap = v;
    405 	sigset_t bss;
    406 	linux_old_sigset_t lss;
    407 
    408 	sigpending1(p, &bss);
    409 	native_to_linux_old_sigset(&bss, &lss);
    410 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    411 }
    412 
    413 int
    414 linux_sys_sigsuspend(p, v, retval)
    415 	register struct proc *p;
    416 	void *v;
    417 	register_t *retval;
    418 {
    419 	struct linux_sys_sigsuspend_args /* {
    420 		syscallarg(caddr_t) restart;
    421 		syscallarg(int) oldmask;
    422 		syscallarg(int) mask;
    423 	} */ *uap = v;
    424 	linux_old_sigset_t lss;
    425 	sigset_t bss;
    426 
    427 	lss = SCARG(uap, mask);
    428 	linux_old_to_native_sigset(&lss, &bss);
    429 	return (sigsuspend1(p, &bss));
    430 }
    431 int
    432 linux_sys_rt_sigsuspend(p, v, retval)
    433 	register struct proc *p;
    434 	void *v;
    435 	register_t *retval;
    436 {
    437 	struct linux_sys_rt_sigsuspend_args /* {
    438 		syscallarg(linux_sigset_t *) unewset;
    439 		syscallarg(size_t) sigsetsize;
    440 	} */ *uap = v;
    441 	linux_sigset_t lss;
    442 	sigset_t bss;
    443 	int error;
    444 
    445 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    446 		return (EINVAL);
    447 
    448 	error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
    449 	if (error)
    450 		return (error);
    451 
    452 	linux_to_native_sigset(&lss, &bss);
    453 
    454 	return (sigsuspend1(p, &bss));
    455 }
    456 
    457 /*
    458  * Once more: only a signal conversion is needed.
    459  * Note: also used as sys_rt_queueinfo.  The info field is ignored.
    460  */
    461 int
    462 linux_sys_rt_queueinfo(p, v, retval)
    463 	register struct proc *p;
    464 	void *v;
    465 	register_t *retval;
    466 {
    467 	/* XXX XAX This isn't this really int, int, siginfo_t *, is it? */
    468 #if 0
    469 	struct linux_sys_rt_queueinfo_args /* {
    470 		syscallarg(int) pid;
    471 		syscallarg(int) signum;
    472 		syscallarg(siginfo_t *) uinfo;
    473 	} */ *uap = v;
    474 #endif
    475 
    476 	/* XXX To really implement this we need to	*/
    477 	/* XXX keep a list of queued signals somewhere.	*/
    478 	return (linux_sys_kill(p, v, retval));
    479 }
    480 
    481 int
    482 linux_sys_kill(p, v, retval)
    483 	register struct proc *p;
    484 	void *v;
    485 	register_t *retval;
    486 {
    487 	struct linux_sys_kill_args /* {
    488 		syscallarg(int) pid;
    489 		syscallarg(int) signum;
    490 	} */ *uap = v;
    491 	struct sys_kill_args ka;
    492 
    493 	SCARG(&ka, pid) = SCARG(uap, pid);
    494 	SCARG(&ka, signum) = linux_to_native_sig[SCARG(uap, signum)];
    495 	return sys_kill(p, &ka, retval);
    496 }
    497