Home | History | Annotate | Line # | Download | only in kern
sys_sig.c revision 1.25
      1 /*	$NetBSD: sys_sig.c,v 1.25 2009/12/20 04:49:09 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  * (c) UNIX System Laboratories, Inc.
     36  * All or some portions of this file are derived from material licensed
     37  * to the University of California by American Telephone and Telegraph
     38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39  * the permission of UNIX System Laboratories, Inc.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.25 2009/12/20 04:49:09 rmind Exp $");
     70 
     71 #include <sys/param.h>
     72 #include <sys/kernel.h>
     73 #include <sys/signalvar.h>
     74 #include <sys/proc.h>
     75 #include <sys/pool.h>
     76 #include <sys/sa.h>
     77 #include <sys/savar.h>
     78 #include <sys/syscallargs.h>
     79 #include <sys/kauth.h>
     80 #include <sys/wait.h>
     81 #include <sys/kmem.h>
     82 #include <sys/module.h>
     83 
     84 int
     85 sys___sigaction_sigtramp(struct lwp *l,
     86     const struct sys___sigaction_sigtramp_args *uap, register_t *retval)
     87 {
     88 	/* {
     89 		syscallarg(int)				signum;
     90 		syscallarg(const struct sigaction *)	nsa;
     91 		syscallarg(struct sigaction *)		osa;
     92 		syscallarg(void *)			tramp;
     93 		syscallarg(int)				vers;
     94 	} */
     95 	struct sigaction nsa, osa;
     96 	int error;
     97 
     98 	if (SCARG(uap, nsa)) {
     99 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    100 		if (error)
    101 			return (error);
    102 	}
    103 	error = sigaction1(l, SCARG(uap, signum),
    104 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    105 	    SCARG(uap, tramp), SCARG(uap, vers));
    106 	if (error)
    107 		return (error);
    108 	if (SCARG(uap, osa)) {
    109 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    110 		if (error)
    111 			return (error);
    112 	}
    113 	return 0;
    114 }
    115 
    116 /*
    117  * Manipulate signal mask.  Note that we receive new mask, not pointer, and
    118  * return old mask as return value; the library stub does the rest.
    119  */
    120 int
    121 sys___sigprocmask14(struct lwp *l, const struct sys___sigprocmask14_args *uap,
    122     register_t *retval)
    123 {
    124 	/* {
    125 		syscallarg(int)			how;
    126 		syscallarg(const sigset_t *)	set;
    127 		syscallarg(sigset_t *)		oset;
    128 	} */
    129 	struct proc	*p = l->l_proc;
    130 	sigset_t	nss, oss;
    131 	int		error;
    132 
    133 	if (SCARG(uap, set)) {
    134 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    135 		if (error)
    136 			return error;
    137 	}
    138 	mutex_enter(p->p_lock);
    139 	error = sigprocmask1(l, SCARG(uap, how),
    140 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    141 	mutex_exit(p->p_lock);
    142 	if (error)
    143 		return error;
    144 	if (SCARG(uap, oset)) {
    145 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    146 		if (error)
    147 			return error;
    148 	}
    149 	return 0;
    150 }
    151 
    152 int
    153 sys___sigpending14(struct lwp *l, const struct sys___sigpending14_args *uap,
    154     register_t *retval)
    155 {
    156 	/* {
    157 		syscallarg(sigset_t *)	set;
    158 	} */
    159 	sigset_t ss;
    160 
    161 	sigpending1(l, &ss);
    162 	return copyout(&ss, SCARG(uap, set), sizeof(ss));
    163 }
    164 
    165 /*
    166  * Suspend process until signal, providing mask to be set in the meantime.
    167  * Note nonstandard calling convention: libc stub passes mask, not pointer,
    168  * to save a copyin.
    169  */
    170 int
    171 sys___sigsuspend14(struct lwp *l, const struct sys___sigsuspend14_args *uap,
    172     register_t *retval)
    173 {
    174 	/* {
    175 		syscallarg(const sigset_t *)	set;
    176 	} */
    177 	sigset_t	ss;
    178 	int		error;
    179 
    180 	if (SCARG(uap, set)) {
    181 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    182 		if (error)
    183 			return error;
    184 	}
    185 	return sigsuspend1(l, SCARG(uap, set) ? &ss : 0);
    186 }
    187 
    188 int
    189 sys___sigaltstack14(struct lwp *l, const struct sys___sigaltstack14_args *uap,
    190     register_t *retval)
    191 {
    192 	/* {
    193 		syscallarg(const struct sigaltstack *)	nss;
    194 		syscallarg(struct sigaltstack *)	oss;
    195 	} */
    196 	struct sigaltstack	nss, oss;
    197 	int			error;
    198 
    199 	if (SCARG(uap, nss)) {
    200 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    201 		if (error)
    202 			return error;
    203 	}
    204 	error = sigaltstack1(l,
    205 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    206 	if (error)
    207 		return error;
    208 	if (SCARG(uap, oss)) {
    209 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    210 		if (error)
    211 			return error;
    212 	}
    213 	return 0;
    214 }
    215 
    216 int
    217 sys_kill(struct lwp *l, const struct sys_kill_args *uap, register_t *retval)
    218 {
    219 	/* {
    220 		syscallarg(int)	pid;
    221 		syscallarg(int)	signum;
    222 	} */
    223 	struct proc	*p;
    224 	ksiginfo_t	ksi;
    225 	int signum = SCARG(uap, signum);
    226 	int error;
    227 
    228 	if ((u_int)signum >= NSIG)
    229 		return EINVAL;
    230 	KSI_INIT(&ksi);
    231 	ksi.ksi_signo = signum;
    232 	ksi.ksi_code = SI_USER;
    233 	ksi.ksi_pid = l->l_proc->p_pid;
    234 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    235 	if (SCARG(uap, pid) > 0) {
    236 		/* kill single process */
    237 		mutex_enter(proc_lock);
    238 		if ((p = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) {
    239 			mutex_exit(proc_lock);
    240 			return ESRCH;
    241 		}
    242 		mutex_enter(p->p_lock);
    243 		error = kauth_authorize_process(l->l_cred,
    244 		    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum),
    245 		    NULL, NULL);
    246 		if (!error && signum) {
    247 			kpsignal2(p, &ksi);
    248 		}
    249 		mutex_exit(p->p_lock);
    250 		mutex_exit(proc_lock);
    251 		return error;
    252 	}
    253 	switch (SCARG(uap, pid)) {
    254 	case -1:		/* broadcast signal */
    255 		return killpg1(l, &ksi, 0, 1);
    256 	case 0:			/* signal own process group */
    257 		return killpg1(l, &ksi, 0, 0);
    258 	default:		/* negative explicit process group */
    259 		return killpg1(l, &ksi, -SCARG(uap, pid), 0);
    260 	}
    261 	/* NOTREACHED */
    262 }
    263 
    264 int
    265 sys_getcontext(struct lwp *l, const struct sys_getcontext_args *uap,
    266     register_t *retval)
    267 {
    268 	/* {
    269 		syscallarg(struct __ucontext *) ucp;
    270 	} */
    271 	struct proc *p = l->l_proc;
    272 	ucontext_t uc;
    273 
    274 	mutex_enter(p->p_lock);
    275 	getucontext(l, &uc);
    276 	mutex_exit(p->p_lock);
    277 
    278 	return copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)));
    279 }
    280 
    281 int
    282 sys_setcontext(struct lwp *l, const struct sys_setcontext_args *uap,
    283     register_t *retval)
    284 {
    285 	/* {
    286 		syscallarg(const ucontext_t *) ucp;
    287 	} */
    288 	struct proc *p = l->l_proc;
    289 	ucontext_t uc;
    290 	int error;
    291 
    292 	error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
    293 	if (error)
    294 		return error;
    295 	if ((uc.uc_flags & _UC_CPU) == 0)
    296 		return EINVAL;
    297 	mutex_enter(p->p_lock);
    298 	error = setucontext(l, &uc);
    299 	mutex_exit(p->p_lock);
    300 	if (error)
    301  		return error;
    302 
    303 	return EJUSTRETURN;
    304 }
    305 
    306 /*
    307  * sigtimedwait(2) system call, used also for implementation
    308  * of sigwaitinfo() and sigwait().
    309  *
    310  * This only handles single LWP in signal wait. libpthread provides
    311  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
    312  */
    313 int
    314 sys_____sigtimedwait50(struct lwp *l,
    315     const struct sys_____sigtimedwait50_args *uap, register_t *retval)
    316 {
    317 
    318 	return __sigtimedwait1(l, uap, retval, copyout, copyin, copyout);
    319 }
    320 
    321 int
    322 sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
    323 	struct sigaction *osa, const void *tramp, int vers)
    324 {
    325 	struct proc *p;
    326 	struct sigacts *ps;
    327 	sigset_t tset;
    328 	int prop, error;
    329 	ksiginfoq_t kq;
    330 	static bool v0v1valid;
    331 
    332 	if (signum <= 0 || signum >= NSIG)
    333 		return EINVAL;
    334 
    335 	p = l->l_proc;
    336 	error = 0;
    337 	ksiginfo_queue_init(&kq);
    338 
    339 	/*
    340 	 * Trampoline ABI version 0 is reserved for the legacy kernel
    341 	 * provided on-stack trampoline.  Conversely, if we are using a
    342 	 * non-0 ABI version, we must have a trampoline.  Only validate the
    343 	 * vers if a new sigaction was supplied. Emulations use legacy
    344 	 * kernel trampolines with version 0, alternatively check for that
    345 	 * too.
    346 	 *
    347 	 * If version < 2, we try to autoload the compat module.  Note
    348 	 * that we interlock with the unload check in compat_modcmd()
    349 	 * using module_lock.  If the autoload fails, we don't try it
    350 	 * again for this process.
    351 	 */
    352 	if (nsa != NULL) {
    353 		if (__predict_false(vers < 2) &&
    354 		    (p->p_lflag & PL_SIGCOMPAT) == 0) {
    355 			mutex_enter(&module_lock);
    356 			if (sendsig_sigcontext_vec == NULL) {
    357 				(void)module_autoload("compat",
    358 				    MODULE_CLASS_ANY);
    359 			}
    360 			if (sendsig_sigcontext_vec != NULL) {
    361 				/*
    362 				 * We need to remember if the
    363 				 * sigcontext method may be useable,
    364 				 * because libc may use it even
    365 				 * if siginfo is available.
    366 				 */
    367 				v0v1valid = true;
    368 			}
    369 			mutex_enter(proc_lock);
    370 			/*
    371 			 * Prevent unload of compat module while
    372 			 * this process remains.
    373 			 */
    374 			p->p_lflag |= PL_SIGCOMPAT;
    375 			mutex_exit(proc_lock);
    376 			mutex_exit(&module_lock);
    377 		}
    378 
    379 		switch (vers) {
    380 		case 0:
    381 			/* sigcontext, kernel supplied trampoline. */
    382 			if (tramp != NULL || !v0v1valid) {
    383 				return EINVAL;
    384 			}
    385 			break;
    386 		case 1:
    387 			/* sigcontext, user supplied trampoline. */
    388 			if (tramp == NULL || !v0v1valid) {
    389 				return EINVAL;
    390 			}
    391 			break;
    392 		case 2:
    393 		case 3:
    394 			/* siginfo, user supplied trampoline. */
    395 			if (tramp == NULL) {
    396 				return EINVAL;
    397 			}
    398 			break;
    399 		default:
    400 			return EINVAL;
    401 		}
    402 	}
    403 
    404 	mutex_enter(p->p_lock);
    405 
    406 	ps = p->p_sigacts;
    407 	if (osa)
    408 		*osa = SIGACTION_PS(ps, signum);
    409 	if (!nsa)
    410 		goto out;
    411 
    412 	prop = sigprop[signum];
    413 	if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
    414 		error = EINVAL;
    415 		goto out;
    416 	}
    417 
    418 	SIGACTION_PS(ps, signum) = *nsa;
    419 	ps->sa_sigdesc[signum].sd_tramp = tramp;
    420 	ps->sa_sigdesc[signum].sd_vers = vers;
    421 	sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    422 
    423 	if ((prop & SA_NORESET) != 0)
    424 		SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    425 
    426 	if (signum == SIGCHLD) {
    427 		if (nsa->sa_flags & SA_NOCLDSTOP)
    428 			p->p_sflag |= PS_NOCLDSTOP;
    429 		else
    430 			p->p_sflag &= ~PS_NOCLDSTOP;
    431 		if (nsa->sa_flags & SA_NOCLDWAIT) {
    432 			/*
    433 			 * Paranoia: since SA_NOCLDWAIT is implemented by
    434 			 * reparenting the dying child to PID 1 (and trust
    435 			 * it to reap the zombie), PID 1 itself is forbidden
    436 			 * to set SA_NOCLDWAIT.
    437 			 */
    438 			if (p->p_pid == 1)
    439 				p->p_flag &= ~PK_NOCLDWAIT;
    440 			else
    441 				p->p_flag |= PK_NOCLDWAIT;
    442 		} else
    443 			p->p_flag &= ~PK_NOCLDWAIT;
    444 
    445 		if (nsa->sa_handler == SIG_IGN) {
    446 			/*
    447 			 * Paranoia: same as above.
    448 			 */
    449 			if (p->p_pid == 1)
    450 				p->p_flag &= ~PK_CLDSIGIGN;
    451 			else
    452 				p->p_flag |= PK_CLDSIGIGN;
    453 		} else
    454 			p->p_flag &= ~PK_CLDSIGIGN;
    455 	}
    456 
    457 	if ((nsa->sa_flags & SA_NODEFER) == 0)
    458 		sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    459 	else
    460 		sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    461 
    462 	/*
    463 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    464 	 * SIG_IGN, and for signals set to SIG_DFL where the default is to
    465 	 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
    466 	 * we have to restart the process.
    467 	 */
    468 	if (nsa->sa_handler == SIG_IGN ||
    469 	    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    470 		/* Never to be seen again. */
    471 		sigemptyset(&tset);
    472 		sigaddset(&tset, signum);
    473 		sigclearall(p, &tset, &kq);
    474 		if (signum != SIGCONT) {
    475 			/* Easier in psignal */
    476 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    477 		}
    478 		sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    479 	} else {
    480 		sigdelset(&p->p_sigctx.ps_sigignore, signum);
    481 		if (nsa->sa_handler == SIG_DFL)
    482 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    483 		else
    484 			sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    485 	}
    486 
    487 	/*
    488 	 * Previously held signals may now have become visible.  Ensure that
    489 	 * we check for them before returning to userspace.
    490 	 */
    491 	if (sigispending(l, 0)) {
    492 		lwp_lock(l);
    493 		l->l_flag |= LW_PENDSIG;
    494 		lwp_unlock(l);
    495 	}
    496 out:
    497 	mutex_exit(p->p_lock);
    498 	ksiginfo_queue_drain(&kq);
    499 
    500 	return error;
    501 }
    502 
    503 int
    504 sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
    505 {
    506 	int more;
    507 	struct proc *p = l->l_proc;
    508 	sigset_t *mask;
    509 	mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask;
    510 
    511 	KASSERT(mutex_owned(p->p_lock));
    512 
    513 	if (oss)
    514 		*oss = *mask;
    515 	if (nss) {
    516 		switch (how) {
    517 		case SIG_BLOCK:
    518 			sigplusset(nss, mask);
    519 			more = 0;
    520 			break;
    521 		case SIG_UNBLOCK:
    522 			sigminusset(nss, mask);
    523 			more = 1;
    524 			break;
    525 		case SIG_SETMASK:
    526 			*mask = *nss;
    527 			more = 1;
    528 			break;
    529 		default:
    530 			return (EINVAL);
    531 		}
    532 		sigminusset(&sigcantmask, mask);
    533 		if (more && sigispending(l, 0)) {
    534 			/*
    535 			 * Check for pending signals on return to user.
    536 			 */
    537 			lwp_lock(l);
    538 			l->l_flag |= LW_PENDSIG;
    539 			lwp_unlock(l);
    540 		}
    541 	}
    542 
    543 	return 0;
    544 }
    545 
    546 void
    547 sigpending1(struct lwp *l, sigset_t *ss)
    548 {
    549 	struct proc *p = l->l_proc;
    550 
    551 	mutex_enter(p->p_lock);
    552 	*ss = l->l_sigpend.sp_set;
    553 	sigplusset(&p->p_sigpend.sp_set, ss);
    554 	mutex_exit(p->p_lock);
    555 }
    556 
    557 int
    558 sigsuspend1(struct lwp *l, const sigset_t *ss)
    559 {
    560 	struct proc *p = l->l_proc;
    561 
    562 	if (ss) {
    563 		/*
    564 		 * When returning from sigsuspend, we want
    565 		 * the old mask to be restored after the
    566 		 * signal handler has finished.  Thus, we
    567 		 * save it here and mark the sigctx structure
    568 		 * to indicate this.
    569 		 */
    570 		mutex_enter(p->p_lock);
    571 		l->l_sigrestore = 1;
    572 		l->l_sigoldmask = l->l_sigmask;
    573 		l->l_sigmask = *ss;
    574 		sigminusset(&sigcantmask, &l->l_sigmask);
    575 
    576 		/* Check for pending signals when sleeping. */
    577 		if (sigispending(l, 0)) {
    578 			lwp_lock(l);
    579 			l->l_flag |= LW_PENDSIG;
    580 			lwp_unlock(l);
    581 		}
    582 		mutex_exit(p->p_lock);
    583 	}
    584 
    585 	while (kpause("pause", true, 0, NULL) == 0)
    586 		;
    587 
    588 	/* always return EINTR rather than ERESTART... */
    589 	return EINTR;
    590 }
    591 
    592 int
    593 sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
    594     struct sigaltstack *oss)
    595 {
    596 	struct proc *p = l->l_proc;
    597 	int error = 0;
    598 
    599 	mutex_enter(p->p_lock);
    600 
    601 	if (oss)
    602 		*oss = l->l_sigstk;
    603 
    604 	if (nss) {
    605 		if (nss->ss_flags & ~SS_ALLBITS)
    606 			error = EINVAL;
    607 		else if (nss->ss_flags & SS_DISABLE) {
    608 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    609 				error = EINVAL;
    610 		} else if (nss->ss_size < MINSIGSTKSZ)
    611 			error = ENOMEM;
    612 
    613 		if (!error)
    614 			l->l_sigstk = *nss;
    615 	}
    616 
    617 	mutex_exit(p->p_lock);
    618 
    619 	return error;
    620 }
    621 
    622 int
    623 __sigtimedwait1(struct lwp *l, const struct sys_____sigtimedwait50_args *uap,
    624     register_t *retval, copyout_t storeinf, copyin_t fetchts, copyout_t storets)
    625 {
    626 	/* {
    627 		syscallarg(const sigset_t *) set;
    628 		syscallarg(siginfo_t *) info;
    629 		syscallarg(struct timespec *) timeout;
    630 	} */
    631 	struct proc *p = l->l_proc;
    632 	int error, signum, timo;
    633 	struct timespec ts, tsstart, tsnow;
    634 	ksiginfo_t ksi;
    635 
    636 	/*
    637 	 * Calculate timeout, if it was specified.
    638 	 */
    639 	if (SCARG(uap, timeout)) {
    640 		error = (*fetchts)(SCARG(uap, timeout), &ts, sizeof(ts));
    641 		if (error)
    642 			return error;
    643 
    644 		if ((error = itimespecfix(&ts)) != 0)
    645 			return error;
    646 
    647 		timo = tstohz(&ts);
    648 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec != 0)
    649 			timo++;
    650 
    651 		/*
    652 		 * Remember current uptime, it would be used in
    653 		 * ECANCELED/ERESTART case.
    654 		 */
    655 		getnanouptime(&tsstart);
    656 	} else {
    657 		memset(&tsstart, 0, sizeof(tsstart)); /* XXXgcc */
    658 		timo = 0;
    659 	}
    660 
    661 	error = copyin(SCARG(uap, set), &l->l_sigwaitset,
    662 	    sizeof(l->l_sigwaitset));
    663 	if (error)
    664 		return error;
    665 
    666 	/*
    667 	 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
    668 	 * SA_CANTMASK signals in waitset, we do this only for the below
    669 	 * siglist check.
    670 	 */
    671 	sigminusset(&sigcantmask, &l->l_sigwaitset);
    672 
    673 	mutex_enter(p->p_lock);
    674 
    675 	/* SA processes can have no more than 1 sigwaiter. */
    676 	if ((p->p_sflag & PS_SA) != 0 && !LIST_EMPTY(&p->p_sigwaiters)) {
    677 		mutex_exit(p->p_lock);
    678 		error = EINVAL;
    679 		goto out;
    680 	}
    681 
    682 	/* Check for pending signals in the process, if no - then in LWP. */
    683 	if ((signum = sigget(&p->p_sigpend, &ksi, 0, &l->l_sigwaitset)) == 0)
    684 		signum = sigget(&l->l_sigpend, &ksi, 0, &l->l_sigwaitset);
    685 
    686 	if (signum != 0) {
    687 		/* If found a pending signal, just copy it out to the user. */
    688 		mutex_exit(p->p_lock);
    689 		goto out;
    690 	}
    691 
    692 	/*
    693 	 * Set up the sigwait list and wait for signal to arrive.
    694 	 * We can either be woken up or time out.
    695 	 */
    696 	l->l_sigwaited = &ksi;
    697 	LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
    698 	error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
    699 
    700 	/*
    701 	 * Need to find out if we woke as a result of _lwp_wakeup() or a
    702 	 * signal outside our wait set.
    703 	 */
    704 	if (l->l_sigwaited != NULL) {
    705 		if (error == EINTR) {
    706 			/* Wakeup via _lwp_wakeup(). */
    707 			error = ECANCELED;
    708 		} else if (!error) {
    709 			/* Spurious wakeup - arrange for syscall restart. */
    710 			error = ERESTART;
    711 		}
    712 		l->l_sigwaited = NULL;
    713 		LIST_REMOVE(l, l_sigwaiter);
    714 	}
    715 	mutex_exit(p->p_lock);
    716 
    717 	/*
    718 	 * If the sleep was interrupted (either by signal or wakeup), update
    719 	 * the timeout and copyout new value back.  It would be used when
    720 	 * the syscall would be restarted or called again.
    721 	 */
    722 	if (timo && (error == ERESTART || error == ECANCELED)) {
    723 		getnanouptime(&tsnow);
    724 
    725 		/* Compute how much time has passed since start. */
    726 		timespecsub(&tsnow, &tsstart, &tsnow);
    727 
    728 		/* Substract passed time from timeout. */
    729 		timespecsub(&ts, &tsnow, &ts);
    730 
    731 		if (ts.tv_sec < 0)
    732 			error = EAGAIN;
    733 		else {
    734 			/* Copy updated timeout to userland. */
    735 			error = (*storets)(&ts, SCARG(uap, timeout),
    736 			    sizeof(ts));
    737 		}
    738 	}
    739 out:
    740 	/*
    741 	 * If a signal from the wait set arrived, copy it to userland.
    742 	 * Copy only the used part of siginfo, the padding part is
    743 	 * left unchanged (userland is not supposed to touch it anyway).
    744 	 */
    745 	if (error == 0) {
    746 		error = (*storeinf)(&ksi.ksi_info, SCARG(uap, info),
    747 		    sizeof(ksi.ksi_info));
    748 	}
    749 	return error;
    750 }
    751