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