Home | History | Annotate | Line # | Download | only in common
linux_signal.c revision 1.54.14.1
      1 /*	$NetBSD: linux_signal.c,v 1.54.14.1 2007/10/26 15:43:59 joerg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Eric Haszlakiewicz.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * heavily from: svr4_signal.c,v 1.7 1995/01/09 01:04:21 christos Exp
     40  */
     41 
     42 /*
     43  *   Functions in multiarch:
     44  *	linux_sys_signal	: linux_sig_notalpha.c
     45  *	linux_sys_siggetmask	: linux_sig_notalpha.c
     46  *	linux_sys_sigsetmask	: linux_sig_notalpha.c
     47  *	linux_sys_pause		: linux_sig_notalpha.c
     48  *	linux_sys_sigaction	: linux_sigaction.c
     49  *
     50  */
     51 
     52 /*
     53  *   Unimplemented:
     54  *	linux_sys_rt_sigtimedwait	: sigsuspend w/timeout.
     55  */
     56 
     57 #include <sys/cdefs.h>
     58 __KERNEL_RCSID(0, "$NetBSD: linux_signal.c,v 1.54.14.1 2007/10/26 15:43:59 joerg Exp $");
     59 
     60 #define COMPAT_LINUX 1
     61 
     62 #include <sys/param.h>
     63 #include <sys/systm.h>
     64 #include <sys/namei.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/syscallargs.h>
     75 
     76 #include <compat/linux/common/linux_types.h>
     77 #include <compat/linux/common/linux_signal.h>
     78 #include <compat/linux/common/linux_exec.h> /* For emul_linux */
     79 #include <compat/linux/common/linux_machdep.h> /* For LINUX_NPTL */
     80 #include <compat/linux/common/linux_emuldata.h> /* for linux_emuldata */
     81 #include <compat/linux/common/linux_siginfo.h>
     82 #include <compat/linux/common/linux_sigevent.h>
     83 #include <compat/linux/common/linux_util.h>
     84 #include <compat/linux/common/linux_ipc.h>
     85 #include <compat/linux/common/linux_sem.h>
     86 
     87 #include <compat/linux/linux_syscallargs.h>
     88 
     89 /* Locally used defines (in bsd<->linux conversion functions): */
     90 #define	linux_sigemptyset(s)	memset((s), 0, sizeof(*(s)))
     91 #define	linux_sigismember(s, n)	((s)->sig[((n) - 1) / LINUX__NSIG_BPW]	\
     92 					& (1 << ((n) - 1) % LINUX__NSIG_BPW))
     93 #define	linux_sigaddset(s, n)	((s)->sig[((n) - 1) / LINUX__NSIG_BPW]	\
     94 					|= (1 << ((n) - 1) % LINUX__NSIG_BPW))
     95 
     96 #ifdef DEBUG_LINUX
     97 #define DPRINTF(a)	uprintf a
     98 #else
     99 #define DPRINTF(a)
    100 #endif
    101 
    102 extern const int native_to_linux_signo[];
    103 extern const int linux_to_native_signo[];
    104 
    105 /*
    106  * Convert between Linux and BSD signal sets.
    107  */
    108 #if LINUX__NSIG_WORDS > 1
    109 void
    110 linux_old_extra_to_native_sigset(bss, lss, extra)
    111 	sigset_t *bss;
    112 	const linux_old_sigset_t *lss;
    113 	const unsigned long *extra;
    114 {
    115 	linux_sigset_t lsnew;
    116 
    117 	/* convert old sigset to new sigset */
    118 	linux_sigemptyset(&lsnew);
    119 	lsnew.sig[0] = *lss;
    120 	if (extra)
    121 		memcpy(&lsnew.sig[1], extra,
    122 		    sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
    123 
    124 	linux_to_native_sigset(bss, &lsnew);
    125 }
    126 
    127 void
    128 native_to_linux_old_extra_sigset(lss, extra, bss)
    129 	linux_old_sigset_t *lss;
    130 	unsigned long *extra;
    131 	const sigset_t *bss;
    132 {
    133 	linux_sigset_t lsnew;
    134 
    135 	native_to_linux_sigset(&lsnew, bss);
    136 
    137 	/* convert new sigset to old sigset */
    138 	*lss = lsnew.sig[0];
    139 	if (extra)
    140 		memcpy(extra, &lsnew.sig[1],
    141 		    sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
    142 }
    143 #endif /* LINUX__NSIG_WORDS > 1 */
    144 
    145 void
    146 linux_to_native_sigset(bss, lss)
    147 	sigset_t *bss;
    148 	const linux_sigset_t *lss;
    149 {
    150 	int i, newsig;
    151 
    152 	sigemptyset(bss);
    153 	for (i = 1; i < LINUX__NSIG; i++) {
    154 		if (linux_sigismember(lss, i)) {
    155 			newsig = linux_to_native_signo[i];
    156 			if (newsig)
    157 				sigaddset(bss, newsig);
    158 		}
    159 	}
    160 }
    161 
    162 void
    163 native_to_linux_sigset(lss, bss)
    164 	linux_sigset_t *lss;
    165 	const sigset_t *bss;
    166 {
    167 	int i, newsig;
    168 
    169 	linux_sigemptyset(lss);
    170 	for (i = 1; i < NSIG; i++) {
    171 		if (sigismember(bss, i)) {
    172 			newsig = native_to_linux_signo[i];
    173 			if (newsig)
    174 				linux_sigaddset(lss, newsig);
    175 		}
    176 	}
    177 }
    178 
    179 unsigned int
    180 native_to_linux_sigflags(bsf)
    181 	const int bsf;
    182 {
    183 	unsigned int lsf = 0;
    184 	if ((bsf & SA_NOCLDSTOP) != 0)
    185 		lsf |= LINUX_SA_NOCLDSTOP;
    186 	if ((bsf & SA_NOCLDWAIT) != 0)
    187 		lsf |= LINUX_SA_NOCLDWAIT;
    188 	if ((bsf & SA_ONSTACK) != 0)
    189 		lsf |= LINUX_SA_ONSTACK;
    190 	if ((bsf & SA_RESTART) != 0)
    191 		lsf |= LINUX_SA_RESTART;
    192 	if ((bsf & SA_NODEFER) != 0)
    193 		lsf |= LINUX_SA_NOMASK;
    194 	if ((bsf & SA_RESETHAND) != 0)
    195 		lsf |= LINUX_SA_ONESHOT;
    196 	if ((bsf & SA_SIGINFO) != 0)
    197 		lsf |= LINUX_SA_SIGINFO;
    198 	return lsf;
    199 }
    200 
    201 int
    202 linux_to_native_sigflags(lsf)
    203 	const unsigned long lsf;
    204 {
    205 	int bsf = 0;
    206 	if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
    207 		bsf |= SA_NOCLDSTOP;
    208 	if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
    209 		bsf |= SA_NOCLDWAIT;
    210 	if ((lsf & LINUX_SA_ONSTACK) != 0)
    211 		bsf |= SA_ONSTACK;
    212 	if ((lsf & LINUX_SA_RESTART) != 0)
    213 		bsf |= SA_RESTART;
    214 	if ((lsf & LINUX_SA_ONESHOT) != 0)
    215 		bsf |= SA_RESETHAND;
    216 	if ((lsf & LINUX_SA_NOMASK) != 0)
    217 		bsf |= SA_NODEFER;
    218 	if ((lsf & LINUX_SA_SIGINFO) != 0)
    219 		bsf |= SA_SIGINFO;
    220 	if ((lsf & ~LINUX_SA_ALLBITS) != 0) {
    221 		DPRINTF(("linux_old_to_native_sigflags: "
    222 		    "%lx extra bits ignored\n", lsf));
    223 	}
    224 	return bsf;
    225 }
    226 
    227 /*
    228  * Convert between Linux and BSD sigaction structures.
    229  */
    230 void
    231 linux_old_to_native_sigaction(bsa, lsa)
    232 	struct sigaction *bsa;
    233 	const struct linux_old_sigaction *lsa;
    234 {
    235 	bsa->sa_handler = lsa->linux_sa_handler;
    236 	linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    237 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
    238 }
    239 
    240 void
    241 native_to_linux_old_sigaction(lsa, bsa)
    242 	struct linux_old_sigaction *lsa;
    243 	const struct sigaction *bsa;
    244 {
    245 	lsa->linux_sa_handler = bsa->sa_handler;
    246 	native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    247 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
    248 #ifndef __alpha__
    249 	lsa->linux_sa_restorer = NULL;
    250 #endif
    251 }
    252 
    253 /* ...and the new sigaction conversion funcs. */
    254 void
    255 linux_to_native_sigaction(bsa, lsa)
    256 	struct sigaction *bsa;
    257 	const struct linux_sigaction *lsa;
    258 {
    259 	bsa->sa_handler = lsa->linux_sa_handler;
    260 	linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    261 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
    262 }
    263 
    264 void
    265 native_to_linux_sigaction(lsa, bsa)
    266 	struct linux_sigaction *lsa;
    267 	const struct sigaction *bsa;
    268 {
    269 	lsa->linux_sa_handler = bsa->sa_handler;
    270 	native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    271 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
    272 #ifndef __alpha__
    273 	lsa->linux_sa_restorer = NULL;
    274 #endif
    275 }
    276 
    277 /* ----------------------------------------------------------------------- */
    278 
    279 /*
    280  * The Linux sigaction() system call. Do the usual conversions,
    281  * and just call sigaction(). Some flags and values are silently
    282  * ignored (see above).
    283  */
    284 int
    285 linux_sys_rt_sigaction(struct lwp *l, void *v, register_t *retval)
    286 {
    287 	struct linux_sys_rt_sigaction_args /* {
    288 		syscallarg(int) signum;
    289 		syscallarg(const struct linux_sigaction *) nsa;
    290 		syscallarg(struct linux_sigaction *) osa;
    291 		syscallarg(size_t) sigsetsize;
    292 	} */ *uap = v;
    293 	struct linux_sigaction nlsa, olsa;
    294 	struct sigaction nbsa, obsa;
    295 	int error, sig;
    296 	void *tramp = NULL;
    297 	int vers = 0;
    298 #if defined __amd64__
    299 	struct sigacts *ps = l->l_proc->p_sigacts;
    300 #endif
    301 
    302 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    303 		return (EINVAL);
    304 
    305 	if (SCARG(uap, nsa)) {
    306 		error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
    307 		if (error)
    308 			return (error);
    309 		linux_to_native_sigaction(&nbsa, &nlsa);
    310 	}
    311 
    312 	sig = SCARG(uap, signum);
    313 	if (sig < 0 || sig >= LINUX__NSIG)
    314 		return (EINVAL);
    315 	if (sig > 0 && !linux_to_native_signo[sig]) {
    316 		/* Pretend that we did something useful for unknown signals. */
    317 		obsa.sa_handler = SIG_IGN;
    318 		sigemptyset(&obsa.sa_mask);
    319 		obsa.sa_flags = 0;
    320 	} else {
    321 #if defined __amd64__
    322 		if (nlsa.linux_sa_flags & LINUX_SA_RESTORER) {
    323 			if ((tramp = nlsa.linux_sa_restorer) != NULL)
    324 				vers = 2; /* XXX arch dependant */
    325 		}
    326 #endif
    327 
    328 		error = sigaction1(l, linux_to_native_signo[sig],
    329 		    SCARG(uap, nsa) ? &nbsa : NULL,
    330 		    SCARG(uap, osa) ? &obsa : NULL,
    331 		    tramp, vers);
    332 		if (error)
    333 			return (error);
    334 	}
    335 	if (SCARG(uap, osa)) {
    336 		native_to_linux_sigaction(&olsa, &obsa);
    337 
    338 #if defined __amd64__
    339 		if (ps->sa_sigdesc[sig].sd_vers != 0) {
    340 			olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp;
    341 			olsa.linux_sa_flags |= LINUX_SA_RESTORER;
    342 		}
    343 #endif
    344 
    345 		error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
    346 		if (error)
    347 			return (error);
    348 	}
    349 	return (0);
    350 }
    351 
    352 int
    353 linux_sigprocmask1(l, how, set, oset)
    354 	struct lwp *l;
    355 	int how;
    356 	const linux_old_sigset_t *set;
    357 	linux_old_sigset_t *oset;
    358 {
    359 	struct proc *p = l->l_proc;
    360 	linux_old_sigset_t nlss, olss;
    361 	sigset_t nbss, obss;
    362 	int error;
    363 
    364 	switch (how) {
    365 	case LINUX_SIG_BLOCK:
    366 		how = SIG_BLOCK;
    367 		break;
    368 	case LINUX_SIG_UNBLOCK:
    369 		how = SIG_UNBLOCK;
    370 		break;
    371 	case LINUX_SIG_SETMASK:
    372 		how = SIG_SETMASK;
    373 		break;
    374 	default:
    375 		return (EINVAL);
    376 	}
    377 
    378 	if (set) {
    379 		error = copyin(set, &nlss, sizeof(nlss));
    380 		if (error)
    381 			return (error);
    382 		linux_old_to_native_sigset(&nbss, &nlss);
    383 	}
    384 	mutex_enter(&p->p_smutex);
    385 	error = sigprocmask1(l, how,
    386 	    set ? &nbss : NULL, oset ? &obss : NULL);
    387 	mutex_exit(&p->p_smutex);
    388 	if (error)
    389 		return (error);
    390 	if (oset) {
    391 		native_to_linux_old_sigset(&olss, &obss);
    392 		error = copyout(&olss, oset, sizeof(olss));
    393 		if (error)
    394 			return (error);
    395 	}
    396 	return (error);
    397 }
    398 
    399 int
    400 linux_sys_rt_sigprocmask(struct lwp *l, void *v, register_t *retval)
    401 {
    402 	struct linux_sys_rt_sigprocmask_args /* {
    403 		syscallarg(int) how;
    404 		syscallarg(const linux_sigset_t *) set;
    405 		syscallarg(linux_sigset_t *) oset;
    406 		syscallarg(size_t) sigsetsize;
    407 	} */ *uap = v;
    408 	linux_sigset_t nlss, olss, *oset;
    409 	const linux_sigset_t *set;
    410 	struct proc *p = l->l_proc;
    411 	sigset_t nbss, obss;
    412 	int error, how;
    413 
    414 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    415 		return (EINVAL);
    416 
    417 	switch (SCARG(uap, how)) {
    418 	case LINUX_SIG_BLOCK:
    419 		how = SIG_BLOCK;
    420 		break;
    421 	case LINUX_SIG_UNBLOCK:
    422 		how = SIG_UNBLOCK;
    423 		break;
    424 	case LINUX_SIG_SETMASK:
    425 		how = SIG_SETMASK;
    426 		break;
    427 	default:
    428 		return (EINVAL);
    429 	}
    430 
    431 	set = SCARG(uap, set);
    432 	oset = SCARG(uap, oset);
    433 
    434 	if (set) {
    435 		error = copyin(set, &nlss, sizeof(nlss));
    436 		if (error)
    437 			return (error);
    438 		linux_to_native_sigset(&nbss, &nlss);
    439 	}
    440 	mutex_enter(&p->p_smutex);
    441 	error = sigprocmask1(l, how,
    442 	    set ? &nbss : NULL, oset ? &obss : NULL);
    443 	mutex_exit(&p->p_smutex);
    444 	if (!error && oset) {
    445 		native_to_linux_sigset(&olss, &obss);
    446 		error = copyout(&olss, oset, sizeof(olss));
    447 	}
    448 	return (error);
    449 }
    450 
    451 int
    452 linux_sys_rt_sigpending(struct lwp *l, void *v, register_t *retval)
    453 {
    454 	struct linux_sys_rt_sigpending_args /* {
    455 		syscallarg(linux_sigset_t *) set;
    456 		syscallarg(size_t) sigsetsize;
    457 	} */ *uap = v;
    458 	sigset_t bss;
    459 	linux_sigset_t lss;
    460 
    461 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    462 		return (EINVAL);
    463 
    464 	sigpending1(l, &bss);
    465 	native_to_linux_sigset(&lss, &bss);
    466 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    467 }
    468 
    469 #ifndef __amd64__
    470 int
    471 linux_sys_sigpending(struct lwp *l, void *v, register_t *retval)
    472 {
    473 	struct linux_sys_sigpending_args /* {
    474 		syscallarg(linux_old_sigset_t *) mask;
    475 	} */ *uap = v;
    476 	sigset_t bss;
    477 	linux_old_sigset_t lss;
    478 
    479 	sigpending1(l, &bss);
    480 	native_to_linux_old_sigset(&lss, &bss);
    481 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    482 }
    483 
    484 int
    485 linux_sys_sigsuspend(struct lwp *l, void *v, register_t *retval)
    486 {
    487 	struct linux_sys_sigsuspend_args /* {
    488 		syscallarg(void *) restart;
    489 		syscallarg(int) oldmask;
    490 		syscallarg(int) mask;
    491 	} */ *uap = v;
    492 	linux_old_sigset_t lss;
    493 	sigset_t bss;
    494 
    495 	lss = SCARG(uap, mask);
    496 	linux_old_to_native_sigset(&bss, &lss);
    497 	return (sigsuspend1(l, &bss));
    498 }
    499 #endif /* __amd64__ */
    500 
    501 int
    502 linux_sys_rt_sigsuspend(struct lwp *l, void *v, register_t *retval)
    503 {
    504 	struct linux_sys_rt_sigsuspend_args /* {
    505 		syscallarg(linux_sigset_t *) unewset;
    506 		syscallarg(size_t) sigsetsize;
    507 	} */ *uap = v;
    508 	linux_sigset_t lss;
    509 	sigset_t bss;
    510 	int error;
    511 
    512 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    513 		return (EINVAL);
    514 
    515 	error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
    516 	if (error)
    517 		return (error);
    518 
    519 	linux_to_native_sigset(&bss, &lss);
    520 
    521 	return (sigsuspend1(l, &bss));
    522 }
    523 
    524 /*
    525  * Once more: only a signal conversion is needed.
    526  * Note: also used as sys_rt_queueinfo.  The info field is ignored.
    527  */
    528 int
    529 linux_sys_rt_queueinfo(l, v, retval)
    530 	struct lwp *l;
    531 	void *v;
    532 	register_t *retval;
    533 {
    534 	/* XXX XAX This isn't this really int, int, siginfo_t *, is it? */
    535 #if 0
    536 	struct linux_sys_rt_queueinfo_args /* {
    537 		syscallarg(int) pid;
    538 		syscallarg(int) signum;
    539 		syscallarg(siginfo_t *) uinfo;
    540 	} */ *uap = v;
    541 #endif
    542 
    543 	/* XXX To really implement this we need to	*/
    544 	/* XXX keep a list of queued signals somewhere.	*/
    545 	return (linux_sys_kill(l, v, retval));
    546 }
    547 
    548 int
    549 linux_sys_kill(l, v, retval)
    550 	struct lwp *l;
    551 	void *v;
    552 	register_t *retval;
    553 {
    554 	struct linux_sys_kill_args /* {
    555 		syscallarg(int) pid;
    556 		syscallarg(int) signum;
    557 	} */ *uap = v;
    558 
    559 	struct sys_kill_args ka;
    560 	int sig;
    561 
    562 	SCARG(&ka, pid) = SCARG(uap, pid);
    563 	sig = SCARG(uap, signum);
    564 	if (sig < 0 || sig >= LINUX__NSIG)
    565 		return (EINVAL);
    566 	SCARG(&ka, signum) = linux_to_native_signo[sig];
    567 	return sys_kill(l, &ka, retval);
    568 }
    569 
    570 #ifdef LINUX_SS_ONSTACK
    571 static void linux_to_native_sigaltstack __P((struct sigaltstack *,
    572     const struct linux_sigaltstack *));
    573 
    574 static void
    575 linux_to_native_sigaltstack(bss, lss)
    576 	struct sigaltstack *bss;
    577 	const struct linux_sigaltstack *lss;
    578 {
    579 	bss->ss_sp = lss->ss_sp;
    580 	bss->ss_size = lss->ss_size;
    581 	if (lss->ss_flags & LINUX_SS_ONSTACK)
    582 	    bss->ss_flags = SS_ONSTACK;
    583 	else if (lss->ss_flags & LINUX_SS_DISABLE)
    584 	    bss->ss_flags = SS_DISABLE;
    585 	else
    586 	    bss->ss_flags = 0;
    587 }
    588 
    589 void
    590 native_to_linux_sigaltstack(lss, bss)
    591 	struct linux_sigaltstack *lss;
    592 	const struct sigaltstack *bss;
    593 {
    594 	lss->ss_sp = bss->ss_sp;
    595 	lss->ss_size = bss->ss_size;
    596 	if (bss->ss_flags & SS_ONSTACK)
    597 	    lss->ss_flags = LINUX_SS_ONSTACK;
    598 	else if (bss->ss_flags & SS_DISABLE)
    599 	    lss->ss_flags = LINUX_SS_DISABLE;
    600 	else
    601 	    lss->ss_flags = 0;
    602 }
    603 
    604 int
    605 linux_sys_sigaltstack(struct lwp *l, void *v, register_t *retval)
    606 {
    607 	struct linux_sys_sigaltstack_args /* {
    608 		syscallarg(const struct linux_sigaltstack *) ss;
    609 		syscallarg(struct linux_sigaltstack *) oss;
    610 	} */ *uap = v;
    611 	struct linux_sigaltstack ss;
    612 	struct sigaltstack nss;
    613 	struct proc *p = l->l_proc;
    614 	int error = 0;
    615 
    616 	if (SCARG(uap, oss)) {
    617 		native_to_linux_sigaltstack(&ss, &l->l_sigstk);
    618 		if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
    619 			return error;
    620 	}
    621 
    622 	if (SCARG(uap, ss) != NULL) {
    623 		if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
    624 			return error;
    625 		linux_to_native_sigaltstack(&nss, &ss);
    626 
    627 		mutex_enter(&p->p_smutex);
    628 
    629 		if (nss.ss_flags & ~SS_ALLBITS)
    630 			error = EINVAL;
    631 		else if (nss.ss_flags & SS_DISABLE) {
    632 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    633 				error = EINVAL;
    634 		} else if (nss.ss_size < LINUX_MINSIGSTKSZ)
    635 			error = ENOMEM;
    636 
    637 		if (error == 0)
    638 			l->l_sigstk = nss;
    639 
    640 		mutex_exit(&p->p_smutex);
    641 	}
    642 
    643 	return error;
    644 }
    645 #endif /* LINUX_SS_ONSTACK */
    646 
    647 #ifdef LINUX_NPTL
    648 int
    649 linux_sys_tkill(l, v, retval)
    650 	struct lwp *l;
    651 	void *v;
    652 	register_t *retval;
    653 {
    654 	struct linux_sys_tkill_args /* {
    655 		syscallarg(int) tid;
    656 		syscallarg(int) sig;
    657 	} */ *uap = v;
    658 	struct linux_sys_kill_args cup;
    659 
    660 	/* We use the PID as the TID ... */
    661 	SCARG(&cup, pid) = SCARG(uap, tid);
    662 	SCARG(&cup, signum) = SCARG(uap, sig);
    663 
    664 	return linux_sys_kill(l, &cup, retval);
    665 }
    666 
    667 int
    668 linux_sys_tgkill(l, v, retval)
    669 	struct lwp *l;
    670 	void *v;
    671 	register_t *retval;
    672 {
    673 	struct linux_sys_tgkill_args /* {
    674 		syscallarg(int) tgid;
    675 		syscallarg(int) tid;
    676 		syscallarg(int) sig;
    677 	} */ *uap = v;
    678 	struct linux_sys_kill_args cup;
    679 	struct linux_emuldata *led;
    680 	struct proc *p;
    681 
    682 	SCARG(&cup, pid) = SCARG(uap, tid);
    683 	SCARG(&cup, signum) = SCARG(uap, sig);
    684 
    685 	if (SCARG(uap, tgid) == -1)
    686 		return linux_sys_kill(l, &cup, retval);
    687 
    688 	/* We use the PID as the TID, but make sure the group ID is right */
    689 	if ((p = pfind(SCARG(uap, tid))) == NULL)
    690 		return ESRCH;
    691 
    692 	if (p->p_emul != &emul_linux)
    693 		return ESRCH;
    694 
    695 	led = p->p_emuldata;
    696 
    697 	if (led->s->group_pid != SCARG(uap, tgid))
    698 		return ESRCH;
    699 
    700 	return linux_sys_kill(l, &cup, retval);
    701 }
    702 #endif /* LINUX_NPTL */
    703