Home | History | Annotate | Line # | Download | only in kern
sys_sig.c revision 1.1.2.1
      1 /*	$NetBSD: sys_sig.c,v 1.1.2.1 2006/10/21 14:26:41 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the NetBSD
     18  *	Foundation, Inc. and its contributors.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     38  *	The Regents of the University of California.  All rights reserved.
     39  * (c) UNIX System Laboratories, Inc.
     40  * All or some portions of this file are derived from material licensed
     41  * to the University of California by American Telephone and Telegraph
     42  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     43  * the permission of UNIX System Laboratories, Inc.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. Neither the name of the University nor the names of its contributors
     54  *    may be used to endorse or promote products derived from this software
     55  *    without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  *
     69  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.1.2.1 2006/10/21 14:26:41 ad Exp $");
     74 
     75 #include "opt_ptrace.h"
     76 #include "opt_compat_netbsd.h"
     77 #include "opt_compat_netbsd32.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/kernel.h>
     81 #include <sys/malloc.h>
     82 #include <sys/signalvar.h>
     83 #include <sys/proc.h>
     84 #include <sys/pool.h>
     85 #include <sys/sa.h>
     86 #include <sys/savar.h>
     87 #include <sys/syscallargs.h>
     88 #include <sys/kauth.h>
     89 #include <sys/wait.h>
     90 
     91 #ifdef COMPAT_16
     92 /* ARGSUSED */
     93 int
     94 compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
     95 {
     96 	struct compat_16_sys___sigaction14_args /* {
     97 		syscallarg(int)				signum;
     98 		syscallarg(const struct sigaction *)	nsa;
     99 		syscallarg(struct sigaction *)		osa;
    100 	} */ *uap = v;
    101 	struct sigaction	nsa, osa;
    102 	int			error;
    103 
    104 	if (SCARG(uap, nsa)) {
    105 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    106 		if (error)
    107 			return (error);
    108 	}
    109 	error = sigaction1(l, SCARG(uap, signum),
    110 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    111 	    NULL, 0);
    112 	if (error)
    113 		return (error);
    114 	if (SCARG(uap, osa)) {
    115 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    116 		if (error)
    117 			return (error);
    118 	}
    119 	return (0);
    120 }
    121 #endif
    122 
    123 /* ARGSUSED */
    124 int
    125 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
    126 {
    127 	struct sys___sigaction_sigtramp_args /* {
    128 		syscallarg(int)				signum;
    129 		syscallarg(const struct sigaction *)	nsa;
    130 		syscallarg(struct sigaction *)		osa;
    131 		syscallarg(void *)			tramp;
    132 		syscallarg(int)				vers;
    133 	} */ *uap = v;
    134 	struct sigaction nsa, osa;
    135 	int error;
    136 
    137 	if (SCARG(uap, nsa)) {
    138 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    139 		if (error)
    140 			return (error);
    141 	}
    142 	error = sigaction1(l, SCARG(uap, signum),
    143 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    144 	    SCARG(uap, tramp), SCARG(uap, vers));
    145 	if (error)
    146 		return (error);
    147 	if (SCARG(uap, osa)) {
    148 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    149 		if (error)
    150 			return (error);
    151 	}
    152 	return (0);
    153 }
    154 
    155 /*
    156  * Manipulate signal mask.  Note that we receive new mask, not pointer, and
    157  * return old mask as return value; the library stub does the rest.
    158  */
    159 int
    160 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
    161 {
    162 	struct sys___sigprocmask14_args /* {
    163 		syscallarg(int)			how;
    164 		syscallarg(const sigset_t *)	set;
    165 		syscallarg(sigset_t *)		oset;
    166 	} */ *uap = v;
    167 	sigset_t	nss, oss;
    168 	int		error;
    169 
    170 	if (SCARG(uap, set)) {
    171 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    172 		if (error)
    173 			return (error);
    174 	}
    175 	error = sigprocmask1(l, SCARG(uap, how),
    176 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    177 	if (error)
    178 		return (error);
    179 	if (SCARG(uap, oset)) {
    180 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    181 		if (error)
    182 			return (error);
    183 	}
    184 	return (0);
    185 }
    186 
    187 /* ARGSUSED */
    188 int
    189 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
    190 {
    191 	struct sys___sigpending14_args /* {
    192 		syscallarg(sigset_t *)	set;
    193 	} */ *uap = v;
    194 	sigset_t ss;
    195 
    196 	sigpending1(l, &ss);
    197 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    198 }
    199 
    200 /*
    201  * Suspend process until signal, providing mask to be set in the meantime.
    202  * Note nonstandard calling convention: libc stub passes mask, not pointer,
    203  * to save a copyin.
    204  */
    205 /* ARGSUSED */
    206 int
    207 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
    208 {
    209 	struct sys___sigsuspend14_args /* {
    210 		syscallarg(const sigset_t *)	set;
    211 	} */ *uap = v;
    212 	sigset_t	ss;
    213 	int		error;
    214 
    215 	if (SCARG(uap, set)) {
    216 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    217 		if (error)
    218 			return (error);
    219 	}
    220 
    221 	return (sigsuspend1(l, SCARG(uap, set) ? &ss : 0));
    222 }
    223 
    224 /* ARGSUSED */
    225 int
    226 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
    227 {
    228 	struct sys___sigaltstack14_args /* {
    229 		syscallarg(const struct sigaltstack *)	nss;
    230 		syscallarg(struct sigaltstack *)	oss;
    231 	} */ *uap = v;
    232 	struct sigaltstack	nss, oss;
    233 	int			error;
    234 
    235 	if (SCARG(uap, nss)) {
    236 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    237 		if (error)
    238 			return (error);
    239 	}
    240 	error = sigaltstack1(l,
    241 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    242 	if (error)
    243 		return (error);
    244 	if (SCARG(uap, oss)) {
    245 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    246 		if (error)
    247 			return (error);
    248 	}
    249 	return (0);
    250 }
    251 
    252 /* ARGSUSED */
    253 int
    254 sys_kill(struct lwp *l, void *v, register_t *retval)
    255 {
    256 	struct sys_kill_args /* {
    257 		syscallarg(int)	pid;
    258 		syscallarg(int)	signum;
    259 	} */ *uap = v;
    260 	struct proc	*p;
    261 	ksiginfo_t	ksi;
    262 	int signum = SCARG(uap, signum);
    263 	int error;
    264 
    265 	if ((u_int)signum >= NSIG)
    266 		return (EINVAL);
    267 	KSI_INIT(&ksi);
    268 	ksi.ksi_signo = signum;
    269 	ksi.ksi_code = SI_USER;
    270 	ksi.ksi_pid = l->l_proc->p_pid;
    271 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    272 	if (SCARG(uap, pid) > 0) {
    273 		/* kill single process */
    274 		if ((p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL)) == NULL)
    275 			return (ESRCH);
    276 		mutex_enter(&p->p_crmutex);
    277 		error = kauth_authorize_process(l->l_cred,
    278 		    KAUTH_PROCESS_CANSIGNAL, p, (void *)(uintptr_t)signum,
    279 		    NULL, NULL);
    280 		if (!error && signum) {
    281 			mutex_enter(&p->p_smutex);
    282 			kpsignal2(p, &ksi);
    283 			mutex_exit(&p->p_smutex);
    284 		}
    285 		mutex_exit(&p->p_crmutex);
    286 		rw_exit(&proclist_lock);
    287 		return (0);
    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 /*
    301  * Nonexistent system call-- signal process (may want to handle it).  Flag
    302  * error in case process won't see signal immediately (blocked or ignored).
    303  *
    304  * XXX This should not be here.
    305  */
    306 #ifndef PTRACE
    307 __weak_alias(sys_ptrace, sys_nosys);
    308 #endif
    309 
    310 /* ARGSUSED */
    311 int
    312 sys_nosys(struct lwp *l, void *v, register_t *retval)
    313 {
    314 	struct proc 	*p;
    315 
    316 	p = l->l_proc;
    317 	psignal(p, SIGSYS);
    318 	return (ENOSYS);
    319 }
    320 
    321 /* ARGSUSED */
    322 int
    323 sys_getcontext(struct lwp *l, void *v, register_t *retval)
    324 {
    325 	struct sys_getcontext_args /* {
    326 		syscallarg(struct __ucontext *) ucp;
    327 	} */ *uap = v;
    328 	ucontext_t uc;
    329 
    330 	getucontext(l, &uc);
    331 
    332 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
    333 }
    334 
    335 /* ARGSUSED */
    336 int
    337 sys_setcontext(struct lwp *l, void *v, register_t *retval)
    338 {
    339 	struct sys_setcontext_args /* {
    340 		syscallarg(const ucontext_t *) ucp;
    341 	} */ *uap = v;
    342 	ucontext_t uc;
    343 	int error;
    344 
    345 	if (SCARG(uap, ucp) == NULL) {	/* i.e. end of uc_link chain */
    346 		/* Acquire the sched state mutex.  exit1() will release it. */
    347 		mutex_enter(&l->l_proc->p_smutex);
    348 		exit1(l, W_EXITCODE(0, 0));
    349 	} else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
    350 	    (error = setucontext(l, &uc)) != 0)
    351 		return (error);
    352 
    353 	return (EJUSTRETURN);
    354 }
    355 
    356 /*
    357  * sigtimedwait(2) system call, used also for implementation
    358  * of sigwaitinfo() and sigwait().
    359  *
    360  * This only handles single LWP in signal wait. libpthread provides
    361  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
    362  */
    363 int
    364 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
    365 {
    366 
    367 	return __sigtimedwait1(l, v, retval, copyout, copyin, copyout);
    368 }
    369 
    370 int
    371 sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
    372 	struct sigaction *osa, const void *tramp, int vers)
    373 {
    374 	struct proc *p;
    375 	struct sigacts *ps;
    376 	sigset_t tset;
    377 	int prop;
    378 
    379 	if (signum <= 0 || signum >= NSIG)
    380 		return (EINVAL);
    381 
    382 	p = l->l_proc;
    383 
    384 	/*
    385 	 * Trampoline ABI version 0 is reserved for the legacy kernel
    386 	 * provided on-stack trampoline.  Conversely, if we are using a
    387 	 * non-0 ABI version, we must have a trampoline.  Only validate the
    388 	 * vers if a new sigaction was supplied. Emulations use legacy
    389 	 * kernel trampolines with version 0, alternatively check for that
    390 	 * too.
    391 	 */
    392 	if ((vers != 0 && tramp == NULL) ||
    393 #ifdef SIGTRAMP_VALID
    394 	    (nsa != NULL &&
    395 	    ((vers == 0) ?
    396 		(p->p_emul->e_sigcode == NULL) :
    397 		!SIGTRAMP_VALID(vers))) ||
    398 #endif
    399 	    (vers == 0 && tramp != NULL)) {
    400 		return (EINVAL);
    401 	}
    402 
    403 	mutex_enter(&p->p_smutex);
    404 	ps = p->p_sigacts;
    405 	if (osa)
    406 		*osa = SIGACTION_PS(ps, signum);
    407 	if (!nsa) {
    408 		mutex_exit(&p->p_smutex);
    409 		return (0);
    410 	}
    411 
    412 	prop = sigprop[signum];
    413 	if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
    414 		mutex_exit(&p->p_smutex);
    415 		return (EINVAL);
    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_flag |= P_NOCLDSTOP;
    429 		else
    430 			p->p_flag &= ~P_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 &= ~P_NOCLDWAIT;
    440 			else
    441 				p->p_flag |= P_NOCLDWAIT;
    442 		} else
    443 			p->p_flag &= ~P_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 &= ~P_CLDSIGIGN;
    451 			else
    452 				p->p_flag |= P_CLDSIGIGN;
    453 		} else
    454 			p->p_flag &= ~P_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);
    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 	lwp_lock(l);
    492 	signotify(l);
    493 	lwp_unlock(l);
    494 
    495 	mutex_exit(&p->p_smutex);
    496 	return (0);
    497 }
    498 
    499 int
    500 sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
    501 {
    502 	struct proc *p = l->l_proc;
    503 	int more;
    504 
    505 	mutex_enter(&p->p_smutex);
    506 
    507 	if (oss)
    508 		*oss = l->l_sigmask;
    509 	if (nss) {
    510 		switch (how) {
    511 		case SIG_BLOCK:
    512 			sigplusset(nss, &l->l_sigmask);
    513 			more = 0;
    514 			break;
    515 		case SIG_UNBLOCK:
    516 			sigminusset(nss, &l->l_sigmask);
    517 			more = 1;
    518 			break;
    519 		case SIG_SETMASK:
    520 			l->l_sigmask = *nss;
    521 			more = 1;
    522 			break;
    523 		default:
    524 			mutex_exit(&p->p_smutex);
    525 			return (EINVAL);
    526 		}
    527 		sigminusset(&sigcantmask, &l->l_sigmask);
    528 		if (more) {
    529 			/*
    530 			 * Pinch any signals from the per-process pending
    531 			 * list that are now of interest to us.
    532 			 */
    533 			sigpinch(&p->p_sigpend, &l->l_sigpend, &l->l_sigmask);
    534 
    535 			/*
    536 			 * Check for pending signals on return to user.
    537 			 */
    538 			lwp_lock(l);
    539 			signotify(l);
    540 			lwp_unlock(l);
    541 		}
    542 	}
    543 
    544 	mutex_exit(&p->p_smutex);
    545 
    546 	return (0);
    547 }
    548 
    549 void
    550 sigpending1(struct lwp *l, sigset_t *ss)
    551 {
    552 	struct proc *p = l->l_proc;
    553 
    554 	mutex_enter(&p->p_smutex);
    555 	*ss = l->l_sigpend.sp_set;
    556 	sigplusset(&p->p_sigpend.sp_set, ss);
    557 	sigminusset(&l->l_sigmask, ss);
    558 	mutex_exit(&p->p_smutex);
    559 }
    560 
    561 int
    562 sigsuspend1(struct lwp *l, const sigset_t *ss)
    563 {
    564 	struct proc *p;
    565 	struct sigacts *ps;
    566 
    567 	p = l->l_proc;
    568 	ps = p->p_sigacts;
    569 
    570 	mutex_enter(&p->p_smutex);
    571 
    572 	if (ss) {
    573 		/*
    574 		 * When returning from sigpause, we want
    575 		 * the old mask to be restored after the
    576 		 * signal handler has finished.  Thus, we
    577 		 * save it here and mark the sigctx structure
    578 		 * to indicate this.
    579 		 */
    580 		l->l_sigoldmask = l->l_sigmask;
    581 		l->l_sigrestore = 1;
    582 		l->l_sigmask = *ss;
    583 		sigminusset(&sigcantmask, &l->l_sigmask);
    584 		lwp_lock(l);
    585 		signotify(l);
    586 		lwp_unlock(l);
    587 	}
    588 
    589 	while (mtsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0,
    590 	    &p->p_smutex) == 0)
    591 		/* void */;
    592 
    593 	mutex_exit(&p->p_smutex);
    594 
    595 	/* always return EINTR rather than ERESTART... */
    596 	return (EINTR);
    597 }
    598 
    599 int
    600 sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
    601 	     struct sigaltstack *oss)
    602 {
    603 
    604 	if (oss)
    605 		*oss = l->l_sigstk;
    606 
    607 	if (nss) {
    608 		if (nss->ss_flags & ~SS_ALLBITS)
    609 			return (EINVAL);
    610 
    611 		if (nss->ss_flags & SS_DISABLE) {
    612 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    613 				return (EINVAL);
    614 		} else {
    615 			if (nss->ss_size < MINSIGSTKSZ)
    616 				return (ENOMEM);
    617 		}
    618 		l->l_sigstk = *nss;
    619 	}
    620 
    621 	return (0);
    622 }
    623 
    624 int
    625 __sigtimedwait1(struct lwp *l, void *v, register_t *retval,
    626     copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
    627 {
    628 	struct sys___sigtimedwait_args /* {
    629 		syscallarg(const sigset_t *) set;
    630 		syscallarg(siginfo_t *) info;
    631 		syscallarg(struct timespec *) timeout;
    632 	} */ *uap = v;
    633 	sigset_t *waitset;
    634 	struct proc *p = l->l_proc;
    635 	int error, signum;
    636 	int timo = 0;
    637 	struct timespec ts, tsstart, tsnow;
    638 	ksiginfo_t *ksi;
    639 
    640 	memset(&tsstart, 0, sizeof tsstart);	 /* XXX gcc */
    641 
    642 	/*
    643 	 * Calculate timeout, if it was specified.
    644 	 */
    645 	if (SCARG(uap, timeout)) {
    646 		uint64_t ms;
    647 
    648 		if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts))))
    649 			return (error);
    650 
    651 		ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
    652 		timo = mstohz(ms);
    653 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
    654 			timo = 1;
    655 		if (timo <= 0)
    656 			return (EAGAIN);
    657 
    658 		/*
    659 		 * Remember current uptime, it would be used in
    660 		 * ECANCELED/ERESTART case.
    661 		 */
    662 		getnanouptime(&tsstart);
    663 	}
    664 
    665 	MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
    666 	if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
    667 		FREE(waitset, M_TEMP);
    668 		return (error);
    669 	}
    670 
    671 	/*
    672 	 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
    673 	 * SA_CANTMASK signals in waitset, we do this only for the below
    674 	 * siglist check.
    675 	 */
    676 	sigminusset(&sigcantmask, waitset);
    677 
    678 	/*
    679 	 * Allocate a ksi up front.  We can't sleep with the mutex held.
    680 	 */
    681 	ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
    682 
    683 	/*
    684 	 * First scan the per-proc and per-LWP lists and check if there is
    685 	 * signal from our waitset already pending.
    686 	 */
    687 	mutex_enter(&p->p_smutex);
    688 
    689 	if ((signum = sigget(&p->p_sigpend, ksi, 0, waitset)) == 0)
    690 		signum = sigget(&l->l_sigpend, ksi, 0, waitset);
    691 
    692 	if (signum != 0) {
    693 		/*
    694 		 * We found a pending signal - copy it out to the user.
    695 		 */
    696 		mutex_exit(&p->p_smutex);
    697 		error = (*put_info)(&ksi->ksi_info, SCARG(uap, info),
    698 		    sizeof(ksi->ksi_info));
    699 		pool_put(&ksiginfo_pool, ksi);
    700 		return error;
    701 	}
    702 
    703 	/*
    704 	 * Set up the sigwait list. Pass pointer to malloced memory here;
    705 	 * it's not possible to pass pointer to a structure on current
    706 	 * process's stack, the current LWP might be swapped out when the
    707 	 * when the signal is delivered.
    708 	 */
    709 	l->l_sigwaited = ksi;
    710 	l->l_sigwait = waitset;
    711 
    712 	/*
    713 	 * Wait for signal to arrive. We can either be woken up or time out.
    714 	 */
    715 	error = mtsleep(&l->l_sigwait, PPAUSE|PCATCH, "sigwait", timo,
    716 	    &p->p_smutex);
    717 
    718 	/*
    719 	 * Need to find out if we woke as a result of lwp_wakeup() or a
    720 	 * signal outside our wait set.
    721 	 */
    722 	if (l->l_sigwaited != NULL) {
    723 		if (error == EINTR) {
    724 			/* wakeup via _lwp_wakeup() */
    725 			error = ECANCELED;
    726 		} else if (!error) {
    727 			/* spurious wakeup - arrange for syscall restart */
    728 			error = ERESTART;
    729 		}
    730 	}
    731 
    732 	/*
    733 	 * Clear the sigwait indication and unlock.
    734 	 */
    735 	l->l_sigwait = NULL;
    736 	l->l_sigwaited = NULL;
    737 	mutex_exit(&p->p_smutex);
    738 
    739 	/*
    740 	 * If the sleep was interrupted (either by signal or wakeup), update
    741 	 * the timeout and copyout new value back.  It would be used when
    742 	 * the syscall would be restarted or called again.
    743 	 */
    744 	if (timo && (error == ERESTART || error == ECANCELED)) {
    745 		getnanouptime(&tsnow);
    746 
    747 		/* compute how much time has passed since start */
    748 		timespecsub(&tsnow, &tsstart, &tsnow);
    749 		/* substract passed time from timeout */
    750 		timespecsub(&ts, &tsnow, &ts);
    751 
    752 		if (ts.tv_sec < 0)
    753 			error = EAGAIN;
    754 		else {
    755 			/* copy updated timeout to userland */
    756 			error = (*put_timeout)(&ts, SCARG(uap, timeout),
    757 			    sizeof(ts));
    758 		}
    759 	}
    760 
    761 	/*
    762 	 * If a signal from the wait set arrived, copy it to userland.
    763 	 * Copy only the used part of siginfo, the padding part is
    764 	 * left unchanged (userland is not supposed to touch it anyway).
    765 	 */
    766 	FREE(waitset, M_TEMP);
    767 	pool_put(&ksiginfo_pool, ksi);
    768 
    769 	if (error == 0)
    770 		error = (*put_info)(&ksi->ksi_info, SCARG(uap, info),
    771 		    sizeof(ksi->ksi_info));
    772 
    773 	return error;
    774 }
    775