Home | History | Annotate | Line # | Download | only in kern
sys_sig.c revision 1.17
      1 /*	$NetBSD: sys_sig.c,v 1.17 2008/10/15 06:51:20 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.17 2008/10/15 06:51:20 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 	mutex_exit(p->p_lock);
    543 }
    544 
    545 int
    546 sigsuspend1(struct lwp *l, const sigset_t *ss)
    547 {
    548 	struct proc *p;
    549 
    550 	p = l->l_proc;
    551 
    552 	if (ss) {
    553 		/*
    554 		 * When returning from sigsuspend, we want
    555 		 * the old mask to be restored after the
    556 		 * signal handler has finished.  Thus, we
    557 		 * save it here and mark the sigctx structure
    558 		 * to indicate this.
    559 		 */
    560 		mutex_enter(p->p_lock);
    561 		l->l_sigrestore = 1;
    562 		l->l_sigoldmask = l->l_sigmask;
    563 		l->l_sigmask = *ss;
    564 		sigminusset(&sigcantmask, &l->l_sigmask);
    565 
    566 		/* Check for pending signals when sleeping. */
    567 		if (sigispending(l, 0)) {
    568 			lwp_lock(l);
    569 			l->l_flag |= LW_PENDSIG;
    570 			lwp_unlock(l);
    571 		}
    572 		mutex_exit(p->p_lock);
    573 	}
    574 
    575 	while (kpause("pause", true, 0, NULL) == 0)
    576 		;
    577 
    578 	/* always return EINTR rather than ERESTART... */
    579 	return (EINTR);
    580 }
    581 
    582 int
    583 sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
    584 	     struct sigaltstack *oss)
    585 {
    586 	struct proc *p = l->l_proc;
    587 	int error = 0;
    588 
    589 	mutex_enter(p->p_lock);
    590 
    591 	if (oss)
    592 		*oss = l->l_sigstk;
    593 
    594 	if (nss) {
    595 		if (nss->ss_flags & ~SS_ALLBITS)
    596 			error = EINVAL;
    597 		else if (nss->ss_flags & SS_DISABLE) {
    598 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    599 				error = EINVAL;
    600 		} else if (nss->ss_size < MINSIGSTKSZ)
    601 			error = ENOMEM;
    602 
    603 		if (!error)
    604 			l->l_sigstk = *nss;
    605 	}
    606 
    607 	mutex_exit(p->p_lock);
    608 
    609 	return (error);
    610 }
    611 
    612 int
    613 __sigtimedwait1(struct lwp *l, const struct sys___sigtimedwait_args *uap, register_t *retval,
    614     copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
    615 {
    616 	/* {
    617 		syscallarg(const sigset_t *) set;
    618 		syscallarg(siginfo_t *) info;
    619 		syscallarg(struct timespec *) timeout;
    620 	} */
    621 	struct proc *p = l->l_proc;
    622 	int error, signum;
    623 	int timo = 0;
    624 	struct timespec ts, tsstart, tsnow;
    625 	ksiginfo_t *ksi;
    626 
    627 	memset(&tsstart, 0, sizeof tsstart);	 /* XXX gcc */
    628 
    629 	/*
    630 	 * Calculate timeout, if it was specified.
    631 	 */
    632 	if (SCARG(uap, timeout)) {
    633 		uint64_t ms;
    634 
    635 		if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts))))
    636 			return (error);
    637 
    638 		ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
    639 		timo = mstohz(ms);
    640 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
    641 			timo = 1;
    642 		if (timo <= 0)
    643 			return (EAGAIN);
    644 
    645 		/*
    646 		 * Remember current uptime, it would be used in
    647 		 * ECANCELED/ERESTART case.
    648 		 */
    649 		getnanouptime(&tsstart);
    650 	}
    651 
    652 	error = copyin(SCARG(uap, set), &l->l_sigwaitset,
    653 	    sizeof(l->l_sigwaitset));
    654 	if (error != 0)
    655 		return (error);
    656 
    657 	/*
    658 	 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
    659 	 * SA_CANTMASK signals in waitset, we do this only for the below
    660 	 * siglist check.
    661 	 */
    662 	sigminusset(&sigcantmask, &l->l_sigwaitset);
    663 
    664 	/*
    665 	 * Allocate a ksi up front.  We can't sleep with the mutex held.
    666 	 */
    667 	ksi = ksiginfo_alloc(p, NULL, PR_WAITOK);
    668 	if (ksi == NULL)
    669 		return (ENOMEM);
    670 
    671 	mutex_enter(p->p_lock);
    672 
    673 	/*
    674 	 * SA processes can have no more than 1 sigwaiter.
    675 	 */
    676 	if ((p->p_sflag & PS_SA) != 0 && !LIST_EMPTY(&p->p_sigwaiters)) {
    677 		mutex_exit(p->p_lock);
    678 		error = EINVAL;
    679 		goto out;
    680 	}
    681 
    682 	if ((signum = sigget(&p->p_sigpend, ksi, 0, &l->l_sigwaitset)) == 0)
    683 		signum = sigget(&l->l_sigpend, ksi, 0, &l->l_sigwaitset);
    684 
    685 	if (signum != 0) {
    686 		/*
    687 		 * We found a pending signal - copy it out to the user.
    688 		 */
    689 		mutex_exit(p->p_lock);
    690 		goto out;
    691 	}
    692 
    693 	/*
    694 	 * Set up the sigwait list.
    695 	 */
    696 	l->l_sigwaited = ksi;
    697 	LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
    698 
    699 	/*
    700 	 * Wait for signal to arrive. We can either be woken up or time out.
    701 	 */
    702 	error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
    703 
    704 	/*
    705 	 * Need to find out if we woke as a result of lwp_wakeup() or a
    706 	 * signal outside our wait set.
    707 	 */
    708 	if (l->l_sigwaited != NULL) {
    709 		if (error == EINTR) {
    710 			/* wakeup via _lwp_wakeup() */
    711 			error = ECANCELED;
    712 		} else if (!error) {
    713 			/* spurious wakeup - arrange for syscall restart */
    714 			error = ERESTART;
    715 		}
    716 		l->l_sigwaited = NULL;
    717 		LIST_REMOVE(l, l_sigwaiter);
    718 	}
    719 
    720 	mutex_exit(p->p_lock);
    721 
    722 	/*
    723 	 * If the sleep was interrupted (either by signal or wakeup), update
    724 	 * the timeout and copyout new value back.  It would be used when
    725 	 * the syscall would be restarted or called again.
    726 	 */
    727 	if (timo && (error == ERESTART || error == ECANCELED)) {
    728 		getnanouptime(&tsnow);
    729 
    730 		/* compute how much time has passed since start */
    731 		timespecsub(&tsnow, &tsstart, &tsnow);
    732 		/* substract passed time from timeout */
    733 		timespecsub(&ts, &tsnow, &ts);
    734 
    735 		if (ts.tv_sec < 0)
    736 			error = EAGAIN;
    737 		else {
    738 			/* copy updated timeout to userland */
    739 			error = (*put_timeout)(&ts, SCARG(uap, timeout),
    740 			    sizeof(ts));
    741 		}
    742 	}
    743 
    744 	/*
    745 	 * If a signal from the wait set arrived, copy it to userland.
    746 	 * Copy only the used part of siginfo, the padding part is
    747 	 * left unchanged (userland is not supposed to touch it anyway).
    748 	 */
    749  out:
    750 	if (error == 0)
    751 		error = (*put_info)(&ksi->ksi_info, SCARG(uap, info),
    752 		    sizeof(ksi->ksi_info));
    753 
    754 	ksiginfo_free(ksi);
    755 
    756 	return error;
    757 }
    758