Home | History | Annotate | Line # | Download | only in kern
sys_sig.c revision 1.15.2.4
      1 /*	$NetBSD: sys_sig.c,v 1.15.2.4 2008/05/28 06:33:24 wrstuden 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.15.2.4 2008/05/28 06:33:24 wrstuden Exp $");
     70 
     71 #include "opt_ptrace.h"
     72 #include "opt_compat_netbsd.h"
     73 #include "opt_compat_netbsd32.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/kernel.h>
     77 #include <sys/signalvar.h>
     78 #include <sys/proc.h>
     79 #include <sys/pool.h>
     80 #include <sys/sa.h>
     81 #include <sys/savar.h>
     82 #include <sys/syscallargs.h>
     83 #include <sys/kauth.h>
     84 #include <sys/wait.h>
     85 #include <sys/kmem.h>
     86 
     87 #ifdef COMPAT_16
     88 /* ARGSUSED */
     89 int
     90 compat_16_sys___sigaction14(struct lwp *l, const struct compat_16_sys___sigaction14_args *uap, register_t *retval)
     91 {
     92 	/* {
     93 		syscallarg(int)				signum;
     94 		syscallarg(const struct sigaction *)	nsa;
     95 		syscallarg(struct sigaction *)		osa;
     96 	} */
     97 	struct sigaction	nsa, osa;
     98 	int			error;
     99 
    100 	if (SCARG(uap, nsa)) {
    101 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    102 		if (error)
    103 			return (error);
    104 	}
    105 	error = sigaction1(l, SCARG(uap, signum),
    106 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    107 	    NULL, 0);
    108 	if (error)
    109 		return (error);
    110 	if (SCARG(uap, osa)) {
    111 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    112 		if (error)
    113 			return (error);
    114 	}
    115 	return (0);
    116 }
    117 #endif
    118 
    119 /* ARGSUSED */
    120 int
    121 sys___sigaction_sigtramp(struct lwp *l, const struct sys___sigaction_sigtramp_args *uap, register_t *retval)
    122 {
    123 	/* {
    124 		syscallarg(int)				signum;
    125 		syscallarg(const struct sigaction *)	nsa;
    126 		syscallarg(struct sigaction *)		osa;
    127 		syscallarg(void *)			tramp;
    128 		syscallarg(int)				vers;
    129 	} */
    130 	struct sigaction nsa, osa;
    131 	int error;
    132 
    133 	if (SCARG(uap, nsa)) {
    134 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    135 		if (error)
    136 			return (error);
    137 	}
    138 	error = sigaction1(l, SCARG(uap, signum),
    139 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    140 	    SCARG(uap, tramp), SCARG(uap, vers));
    141 	if (error)
    142 		return (error);
    143 	if (SCARG(uap, osa)) {
    144 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    145 		if (error)
    146 			return (error);
    147 	}
    148 	return (0);
    149 }
    150 
    151 /*
    152  * Manipulate signal mask.  Note that we receive new mask, not pointer, and
    153  * return old mask as return value; the library stub does the rest.
    154  */
    155 int
    156 sys___sigprocmask14(struct lwp *l, const struct sys___sigprocmask14_args *uap, register_t *retval)
    157 {
    158 	/* {
    159 		syscallarg(int)			how;
    160 		syscallarg(const sigset_t *)	set;
    161 		syscallarg(sigset_t *)		oset;
    162 	} */
    163 	struct proc	*p = l->l_proc;
    164 	sigset_t	nss, oss;
    165 	int		error;
    166 
    167 	if (SCARG(uap, set)) {
    168 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    169 		if (error)
    170 			return (error);
    171 	}
    172 	mutex_enter(p->p_lock);
    173 	error = sigprocmask1(l, SCARG(uap, how),
    174 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    175 	mutex_exit(p->p_lock);
    176 	if (error)
    177 		return (error);
    178 	if (SCARG(uap, oset)) {
    179 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    180 		if (error)
    181 			return (error);
    182 	}
    183 	return (0);
    184 }
    185 
    186 /* ARGSUSED */
    187 int
    188 sys___sigpending14(struct lwp *l, const struct sys___sigpending14_args *uap, register_t *retval)
    189 {
    190 	/* {
    191 		syscallarg(sigset_t *)	set;
    192 	} */
    193 	sigset_t ss;
    194 
    195 	sigpending1(l, &ss);
    196 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    197 }
    198 
    199 /*
    200  * Suspend process until signal, providing mask to be set in the meantime.
    201  * Note nonstandard calling convention: libc stub passes mask, not pointer,
    202  * to save a copyin.
    203  */
    204 /* ARGSUSED */
    205 int
    206 sys___sigsuspend14(struct lwp *l, const struct sys___sigsuspend14_args *uap, register_t *retval)
    207 {
    208 	/* {
    209 		syscallarg(const sigset_t *)	set;
    210 	} */
    211 	sigset_t	ss;
    212 	int		error;
    213 
    214 	if (SCARG(uap, set)) {
    215 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    216 		if (error)
    217 			return (error);
    218 	}
    219 
    220 	return (sigsuspend1(l, SCARG(uap, set) ? &ss : 0));
    221 }
    222 
    223 /* ARGSUSED */
    224 int
    225 sys___sigaltstack14(struct lwp *l, const struct sys___sigaltstack14_args *uap, register_t *retval)
    226 {
    227 	/* {
    228 		syscallarg(const struct sigaltstack *)	nss;
    229 		syscallarg(struct sigaltstack *)	oss;
    230 	} */
    231 	struct sigaltstack	nss, oss;
    232 	int			error;
    233 
    234 	if (SCARG(uap, nss)) {
    235 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    236 		if (error)
    237 			return (error);
    238 	}
    239 	error = sigaltstack1(l,
    240 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    241 	if (error)
    242 		return (error);
    243 	if (SCARG(uap, oss)) {
    244 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    245 		if (error)
    246 			return (error);
    247 	}
    248 	return (0);
    249 }
    250 
    251 /* ARGSUSED */
    252 int
    253 sys_kill(struct lwp *l, const struct sys_kill_args *uap, register_t *retval)
    254 {
    255 	/* {
    256 		syscallarg(int)	pid;
    257 		syscallarg(int)	signum;
    258 	} */
    259 	struct proc	*p;
    260 	ksiginfo_t	ksi;
    261 	int signum = SCARG(uap, signum);
    262 	int error;
    263 
    264 	if ((u_int)signum >= NSIG)
    265 		return (EINVAL);
    266 	KSI_INIT(&ksi);
    267 	ksi.ksi_signo = signum;
    268 	ksi.ksi_code = SI_USER;
    269 	ksi.ksi_pid = l->l_proc->p_pid;
    270 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    271 	if (SCARG(uap, pid) > 0) {
    272 		/* kill single process */
    273 		mutex_enter(proc_lock);
    274 		if ((p = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) {
    275 			mutex_exit(proc_lock);
    276 			return (ESRCH);
    277 		}
    278 		mutex_enter(p->p_lock);
    279 		error = kauth_authorize_process(l->l_cred,
    280 		    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum),
    281 		    NULL, NULL);
    282 		if (!error && signum) {
    283 			kpsignal2(p, &ksi);
    284 		}
    285 		mutex_exit(p->p_lock);
    286 		mutex_exit(proc_lock);
    287 		return (error);
    288 	}
    289 	switch (SCARG(uap, pid)) {
    290 	case -1:		/* broadcast signal */
    291 		return (killpg1(l, &ksi, 0, 1));
    292 	case 0:			/* signal own process group */
    293 		return (killpg1(l, &ksi, 0, 0));
    294 	default:		/* negative explicit process group */
    295 		return (killpg1(l, &ksi, -SCARG(uap, pid), 0));
    296 	}
    297 	/* NOTREACHED */
    298 }
    299 
    300 /* ARGSUSED */
    301 int
    302 sys_getcontext(struct lwp *l, const struct sys_getcontext_args *uap, register_t *retval)
    303 {
    304 	/* {
    305 		syscallarg(struct __ucontext *) ucp;
    306 	} */
    307 	struct proc *p = l->l_proc;
    308 	ucontext_t uc;
    309 
    310 	mutex_enter(p->p_lock);
    311 	getucontext(l, &uc);
    312 	mutex_exit(p->p_lock);
    313 
    314 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
    315 }
    316 
    317 /* ARGSUSED */
    318 int
    319 sys_setcontext(struct lwp *l, const struct sys_setcontext_args *uap, register_t *retval)
    320 {
    321 	/* {
    322 		syscallarg(const ucontext_t *) ucp;
    323 	} */
    324 	struct proc *p = l->l_proc;
    325 	ucontext_t uc;
    326 	int error;
    327 
    328 	error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
    329 	if (error)
    330 		return (error);
    331 	if (!(uc.uc_flags & _UC_CPU))
    332 		return (EINVAL);
    333 	mutex_enter(p->p_lock);
    334 	error = setucontext(l, &uc);
    335 	mutex_exit(p->p_lock);
    336 	if (error)
    337  		return (error);
    338 
    339 	return (EJUSTRETURN);
    340 }
    341 
    342 /*
    343  * sigtimedwait(2) system call, used also for implementation
    344  * of sigwaitinfo() and sigwait().
    345  *
    346  * This only handles single LWP in signal wait. libpthread provides
    347  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
    348  */
    349 int
    350 sys___sigtimedwait(struct lwp *l, const struct sys___sigtimedwait_args *uap, register_t *retval)
    351 {
    352 
    353 	return __sigtimedwait1(l, uap, retval, copyout, copyin, copyout);
    354 }
    355 
    356 int
    357 sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
    358 	struct sigaction *osa, const void *tramp, int vers)
    359 {
    360 	struct proc *p;
    361 	struct sigacts *ps;
    362 	sigset_t tset;
    363 	int prop, error;
    364 	ksiginfoq_t kq;
    365 
    366 	if (signum <= 0 || signum >= NSIG)
    367 		return (EINVAL);
    368 
    369 	p = l->l_proc;
    370 	error = 0;
    371 	ksiginfo_queue_init(&kq);
    372 
    373 	/*
    374 	 * Trampoline ABI version 0 is reserved for the legacy kernel
    375 	 * provided on-stack trampoline.  Conversely, if we are using a
    376 	 * non-0 ABI version, we must have a trampoline.  Only validate the
    377 	 * vers if a new sigaction was supplied. Emulations use legacy
    378 	 * kernel trampolines with version 0, alternatively check for that
    379 	 * too.
    380 	 */
    381 	if ((vers != 0 && tramp == NULL) ||
    382 #ifdef SIGTRAMP_VALID
    383 	    (nsa != NULL &&
    384 	    ((vers == 0) ?
    385 		(p->p_emul->e_sigcode == NULL) :
    386 		!SIGTRAMP_VALID(vers))) ||
    387 #endif
    388 	    (vers == 0 && tramp != NULL)) {
    389 		return (EINVAL);
    390 	}
    391 
    392 	mutex_enter(p->p_lock);
    393 
    394 	ps = p->p_sigacts;
    395 	if (osa)
    396 		*osa = SIGACTION_PS(ps, signum);
    397 	if (!nsa)
    398 		goto out;
    399 
    400 	prop = sigprop[signum];
    401 	if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
    402 		error = EINVAL;
    403 		goto out;
    404 	}
    405 
    406 	SIGACTION_PS(ps, signum) = *nsa;
    407 	ps->sa_sigdesc[signum].sd_tramp = tramp;
    408 	ps->sa_sigdesc[signum].sd_vers = vers;
    409 	sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    410 
    411 	if ((prop & SA_NORESET) != 0)
    412 		SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    413 
    414 	if (signum == SIGCHLD) {
    415 		if (nsa->sa_flags & SA_NOCLDSTOP)
    416 			p->p_sflag |= PS_NOCLDSTOP;
    417 		else
    418 			p->p_sflag &= ~PS_NOCLDSTOP;
    419 		if (nsa->sa_flags & SA_NOCLDWAIT) {
    420 			/*
    421 			 * Paranoia: since SA_NOCLDWAIT is implemented by
    422 			 * reparenting the dying child to PID 1 (and trust
    423 			 * it to reap the zombie), PID 1 itself is forbidden
    424 			 * to set SA_NOCLDWAIT.
    425 			 */
    426 			if (p->p_pid == 1)
    427 				p->p_flag &= ~PK_NOCLDWAIT;
    428 			else
    429 				p->p_flag |= PK_NOCLDWAIT;
    430 		} else
    431 			p->p_flag &= ~PK_NOCLDWAIT;
    432 
    433 		if (nsa->sa_handler == SIG_IGN) {
    434 			/*
    435 			 * Paranoia: same as above.
    436 			 */
    437 			if (p->p_pid == 1)
    438 				p->p_flag &= ~PK_CLDSIGIGN;
    439 			else
    440 				p->p_flag |= PK_CLDSIGIGN;
    441 		} else
    442 			p->p_flag &= ~PK_CLDSIGIGN;
    443 	}
    444 
    445 	if ((nsa->sa_flags & SA_NODEFER) == 0)
    446 		sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    447 	else
    448 		sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    449 
    450 	/*
    451 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    452 	 * SIG_IGN, and for signals set to SIG_DFL where the default is to
    453 	 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
    454 	 * we have to restart the process.
    455 	 */
    456 	if (nsa->sa_handler == SIG_IGN ||
    457 	    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    458 		/* Never to be seen again. */
    459 		sigemptyset(&tset);
    460 		sigaddset(&tset, signum);
    461 		sigclearall(p, &tset, &kq);
    462 		if (signum != SIGCONT) {
    463 			/* Easier in psignal */
    464 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    465 		}
    466 		sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    467 	} else {
    468 		sigdelset(&p->p_sigctx.ps_sigignore, signum);
    469 		if (nsa->sa_handler == SIG_DFL)
    470 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    471 		else
    472 			sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    473 	}
    474 
    475 	/*
    476 	 * Previously held signals may now have become visible.  Ensure that
    477 	 * we check for them before returning to userspace.
    478 	 */
    479 	if (sigispending(l, 0)) {
    480 		lwp_lock(l);
    481 		l->l_flag |= LW_PENDSIG;
    482 		lwp_unlock(l);
    483 	}
    484  out:
    485 	mutex_exit(p->p_lock);
    486 	ksiginfo_queue_drain(&kq);
    487 
    488 	return (error);
    489 }
    490 
    491 int
    492 sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
    493 {
    494 	int more;
    495 	struct proc *p = l->l_proc;
    496 	sigset_t *mask;
    497 	mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask;
    498 
    499 	KASSERT(mutex_owned(p->p_lock));
    500 
    501 	if (oss)
    502 		*oss = *mask;
    503 	if (nss) {
    504 		switch (how) {
    505 		case SIG_BLOCK:
    506 			sigplusset(nss, mask);
    507 			more = 0;
    508 			break;
    509 		case SIG_UNBLOCK:
    510 			sigminusset(nss, mask);
    511 			more = 1;
    512 			break;
    513 		case SIG_SETMASK:
    514 			*mask = *nss;
    515 			more = 1;
    516 			break;
    517 		default:
    518 			return (EINVAL);
    519 		}
    520 		sigminusset(&sigcantmask, mask);
    521 		if (more && sigispending(l, 0)) {
    522 			/*
    523 			 * Check for pending signals on return to user.
    524 			 */
    525 			lwp_lock(l);
    526 			l->l_flag |= LW_PENDSIG;
    527 			lwp_unlock(l);
    528 		}
    529 	}
    530 
    531 	return (0);
    532 }
    533 
    534 void
    535 sigpending1(struct lwp *l, sigset_t *ss)
    536 {
    537 	struct proc *p = l->l_proc;
    538 
    539 	mutex_enter(p->p_lock);
    540 	*ss = l->l_sigpend.sp_set;
    541 	sigplusset(&p->p_sigpend.sp_set, ss);
    542 	if (p->p_sa)
    543 		sigminusset(&p->p_sa->sa_sigmask, ss);
    544 	else
    545 		sigminusset(&l->l_sigmask, ss);
    546 	mutex_exit(p->p_lock);
    547 }
    548 
    549 int
    550 sigsuspend1(struct lwp *l, const sigset_t *ss)
    551 {
    552 	struct proc *p;
    553 
    554 	p = l->l_proc;
    555 
    556 	if (ss) {
    557 		/*
    558 		 * When returning from sigsuspend, we want
    559 		 * the old mask to be restored after the
    560 		 * signal handler has finished.  Thus, we
    561 		 * save it here and mark the sigctx structure
    562 		 * to indicate this.
    563 		 */
    564 		mutex_enter(p->p_lock);
    565 		l->l_sigrestore = 1;
    566 		l->l_sigoldmask = l->l_sigmask;
    567 		l->l_sigmask = *ss;
    568 		sigminusset(&sigcantmask, &l->l_sigmask);
    569 
    570 		/* Check for pending signals when sleeping. */
    571 		if (sigispending(l, 0)) {
    572 			lwp_lock(l);
    573 			l->l_flag |= LW_PENDSIG;
    574 			lwp_unlock(l);
    575 		}
    576 		mutex_exit(p->p_lock);
    577 	}
    578 
    579 	while (kpause("pause", true, 0, NULL) == 0)
    580 		;
    581 
    582 	/* always return EINTR rather than ERESTART... */
    583 	return (EINTR);
    584 }
    585 
    586 int
    587 sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
    588 	     struct sigaltstack *oss)
    589 {
    590 	struct proc *p = l->l_proc;
    591 	int error = 0;
    592 
    593 	mutex_enter(p->p_lock);
    594 
    595 	if (oss)
    596 		*oss = l->l_sigstk;
    597 
    598 	if (nss) {
    599 		if (nss->ss_flags & ~SS_ALLBITS)
    600 			error = EINVAL;
    601 		else if (nss->ss_flags & SS_DISABLE) {
    602 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    603 				error = EINVAL;
    604 		} else if (nss->ss_size < MINSIGSTKSZ)
    605 			error = ENOMEM;
    606 
    607 		if (!error)
    608 			l->l_sigstk = *nss;
    609 	}
    610 
    611 	mutex_exit(p->p_lock);
    612 
    613 	return (error);
    614 }
    615 
    616 int
    617 __sigtimedwait1(struct lwp *l, const struct sys___sigtimedwait_args *uap, register_t *retval,
    618     copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
    619 {
    620 	/* {
    621 		syscallarg(const sigset_t *) set;
    622 		syscallarg(siginfo_t *) info;
    623 		syscallarg(struct timespec *) timeout;
    624 	} */
    625 	struct proc *p = l->l_proc;
    626 	int error, signum;
    627 	int timo = 0;
    628 	struct timespec ts, tsstart, tsnow;
    629 	ksiginfo_t *ksi;
    630 
    631 	memset(&tsstart, 0, sizeof tsstart);	 /* XXX gcc */
    632 
    633 	/*
    634 	 * Calculate timeout, if it was specified.
    635 	 */
    636 	if (SCARG(uap, timeout)) {
    637 		uint64_t ms;
    638 
    639 		if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts))))
    640 			return (error);
    641 
    642 		ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
    643 		timo = mstohz(ms);
    644 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
    645 			timo = 1;
    646 		if (timo <= 0)
    647 			return (EAGAIN);
    648 
    649 		/*
    650 		 * Remember current uptime, it would be used in
    651 		 * ECANCELED/ERESTART case.
    652 		 */
    653 		getnanouptime(&tsstart);
    654 	}
    655 
    656 	error = copyin(SCARG(uap, set), &l->l_sigwaitset,
    657 	    sizeof(l->l_sigwaitset));
    658 	if (error != 0)
    659 		return (error);
    660 
    661 	/*
    662 	 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
    663 	 * SA_CANTMASK signals in waitset, we do this only for the below
    664 	 * siglist check.
    665 	 */
    666 	sigminusset(&sigcantmask, &l->l_sigwaitset);
    667 
    668 	/*
    669 	 * Allocate a ksi up front.  We can't sleep with the mutex held.
    670 	 */
    671 	ksi = ksiginfo_alloc(p, NULL, PR_WAITOK);
    672 	if (ksi == NULL)
    673 		return (ENOMEM);
    674 
    675 	mutex_enter(p->p_lock);
    676 
    677 	/*
    678 	 * SA processes can have no more than 1 sigwaiter.
    679 	 */
    680 	if ((p->p_sflag & PS_SA) != 0 && !LIST_EMPTY(&p->p_sigwaiters)) {
    681 		mutex_exit(p->p_lock);
    682 		error = EINVAL;
    683 		goto out;
    684 	}
    685 
    686 	if ((signum = sigget(&p->p_sigpend, ksi, 0, &l->l_sigwaitset)) == 0)
    687 		signum = sigget(&l->l_sigpend, ksi, 0, &l->l_sigwaitset);
    688 
    689 	if (signum != 0) {
    690 		/*
    691 		 * We found a pending signal - copy it out to the user.
    692 		 */
    693 		mutex_exit(p->p_lock);
    694 		goto out;
    695 	}
    696 
    697 	/*
    698 	 * Set up the sigwait list.
    699 	 */
    700 	l->l_sigwaited = ksi;
    701 	LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
    702 
    703 	/*
    704 	 * Wait for signal to arrive. We can either be woken up or time out.
    705 	 */
    706 	error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
    707 
    708 	/*
    709 	 * Need to find out if we woke as a result of lwp_wakeup() or a
    710 	 * signal outside our wait set.
    711 	 */
    712 	if (l->l_sigwaited != NULL) {
    713 		if (error == EINTR) {
    714 			/* wakeup via _lwp_wakeup() */
    715 			error = ECANCELED;
    716 		} else if (!error) {
    717 			/* spurious wakeup - arrange for syscall restart */
    718 			error = ERESTART;
    719 		}
    720 		l->l_sigwaited = NULL;
    721 		LIST_REMOVE(l, l_sigwaiter);
    722 	}
    723 
    724 	mutex_exit(p->p_lock);
    725 
    726 	/*
    727 	 * If the sleep was interrupted (either by signal or wakeup), update
    728 	 * the timeout and copyout new value back.  It would be used when
    729 	 * the syscall would be restarted or called again.
    730 	 */
    731 	if (timo && (error == ERESTART || error == ECANCELED)) {
    732 		getnanouptime(&tsnow);
    733 
    734 		/* compute how much time has passed since start */
    735 		timespecsub(&tsnow, &tsstart, &tsnow);
    736 		/* substract passed time from timeout */
    737 		timespecsub(&ts, &tsnow, &ts);
    738 
    739 		if (ts.tv_sec < 0)
    740 			error = EAGAIN;
    741 		else {
    742 			/* copy updated timeout to userland */
    743 			error = (*put_timeout)(&ts, SCARG(uap, timeout),
    744 			    sizeof(ts));
    745 		}
    746 	}
    747 
    748 	/*
    749 	 * If a signal from the wait set arrived, copy it to userland.
    750 	 * Copy only the used part of siginfo, the padding part is
    751 	 * left unchanged (userland is not supposed to touch it anyway).
    752 	 */
    753  out:
    754 	if (error == 0)
    755 		error = (*put_info)(&ksi->ksi_info, SCARG(uap, info),
    756 		    sizeof(ksi->ksi_info));
    757 
    758 	ksiginfo_free(ksi);
    759 
    760 	return error;
    761 }
    762