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