Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.236
      1 /*	$NetBSD: kern_sig.c,v 1.236 2006/11/03 11:41:07 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.236 2006/11/03 11:41:07 yamt Exp $");
     41 
     42 #include "opt_coredump.h"
     43 #include "opt_ktrace.h"
     44 #include "opt_ptrace.h"
     45 #include "opt_multiprocessor.h"
     46 #include "opt_compat_sunos.h"
     47 #include "opt_compat_netbsd.h"
     48 #include "opt_compat_netbsd32.h"
     49 
     50 #define	SIGPROP		/* include signal properties table */
     51 #include <sys/param.h>
     52 #include <sys/signalvar.h>
     53 #include <sys/resourcevar.h>
     54 #include <sys/namei.h>
     55 #include <sys/vnode.h>
     56 #include <sys/proc.h>
     57 #include <sys/systm.h>
     58 #include <sys/timeb.h>
     59 #include <sys/times.h>
     60 #include <sys/buf.h>
     61 #include <sys/acct.h>
     62 #include <sys/file.h>
     63 #include <sys/kernel.h>
     64 #include <sys/wait.h>
     65 #include <sys/ktrace.h>
     66 #include <sys/syslog.h>
     67 #include <sys/stat.h>
     68 #include <sys/core.h>
     69 #include <sys/filedesc.h>
     70 #include <sys/malloc.h>
     71 #include <sys/pool.h>
     72 #include <sys/ucontext.h>
     73 #include <sys/sa.h>
     74 #include <sys/savar.h>
     75 #include <sys/exec.h>
     76 #include <sys/sysctl.h>
     77 #include <sys/kauth.h>
     78 
     79 #include <sys/mount.h>
     80 #include <sys/syscallargs.h>
     81 
     82 #include <machine/cpu.h>
     83 
     84 #include <sys/user.h>		/* for coredump */
     85 
     86 #include <uvm/uvm.h>
     87 #include <uvm/uvm_extern.h>
     88 
     89 #ifdef COREDUMP
     90 static int	build_corename(struct proc *, char *, const char *, size_t);
     91 #endif
     92 static void	ksiginfo_exithook(struct proc *, void *);
     93 static void	ksiginfo_queue(struct proc *, const ksiginfo_t *, ksiginfo_t **);
     94 static ksiginfo_t *ksiginfo_dequeue(struct proc *, int);
     95 static void	kpsignal2(struct proc *, const ksiginfo_t *);
     96 
     97 sigset_t	contsigmask, stopsigmask, sigcantmask;
     98 
     99 struct pool	sigacts_pool;	/* memory pool for sigacts structures */
    100 
    101 /*
    102  * struct sigacts memory pool allocator.
    103  */
    104 
    105 static void *
    106 sigacts_poolpage_alloc(struct pool *pp, int flags)
    107 {
    108 
    109 	return (void *)uvm_km_alloc(kernel_map,
    110 	    (PAGE_SIZE)*2, (PAGE_SIZE)*2,
    111 	    ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
    112 	    | UVM_KMF_WIRED);
    113 }
    114 
    115 static void
    116 sigacts_poolpage_free(struct pool *pp, void *v)
    117 {
    118         uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2, UVM_KMF_WIRED);
    119 }
    120 
    121 static struct pool_allocator sigactspool_allocator = {
    122         .pa_alloc = sigacts_poolpage_alloc,
    123 	.pa_free = sigacts_poolpage_free,
    124 };
    125 
    126 static POOL_INIT(siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
    127     &pool_allocator_nointr);
    128 static POOL_INIT(ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo", NULL);
    129 
    130 /*
    131  * Remove and return the first ksiginfo element that matches our requested
    132  * signal, or return NULL if one not found.
    133  */
    134 static ksiginfo_t *
    135 ksiginfo_dequeue(struct proc *p, int signo)
    136 {
    137 	ksiginfo_t *ksi;
    138 	int s;
    139 
    140 	s = splsoftclock();
    141 	simple_lock(&p->p_sigctx.ps_silock);
    142 	CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) {
    143 		if (ksi->ksi_signo == signo) {
    144 			CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
    145 			goto out;
    146 		}
    147 	}
    148 	ksi = NULL;
    149 out:
    150 	simple_unlock(&p->p_sigctx.ps_silock);
    151 	splx(s);
    152 	return ksi;
    153 }
    154 
    155 /*
    156  * Append a new ksiginfo element to the list of pending ksiginfo's, if
    157  * we need to (SA_SIGINFO was requested). We replace non RT signals if
    158  * they already existed in the queue and we add new entries for RT signals,
    159  * or for non RT signals with non-existing entries.
    160  */
    161 static void
    162 ksiginfo_queue(struct proc *p, const ksiginfo_t *ksi, ksiginfo_t **newkp)
    163 {
    164 	ksiginfo_t *kp;
    165 	struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo);
    166 	int s;
    167 
    168 	if ((sa->sa_flags & SA_SIGINFO) == 0)
    169 		return;
    170 
    171 	/*
    172 	 * If there's no info, don't save it.
    173 	 */
    174 	if (KSI_EMPTY_P(ksi))
    175 		return;
    176 
    177 	s = splsoftclock();
    178 	simple_lock(&p->p_sigctx.ps_silock);
    179 #ifdef notyet	/* XXX: QUEUING */
    180 	if (ksi->ksi_signo < SIGRTMIN)
    181 #endif
    182 	{
    183 		CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) {
    184 			if (kp->ksi_signo == ksi->ksi_signo) {
    185 				KSI_COPY(ksi, kp);
    186 				goto out;
    187 			}
    188 		}
    189 	}
    190 	if (newkp && *newkp) {
    191 		kp = *newkp;
    192 		*newkp = NULL;
    193 	} else {
    194 		SCHED_ASSERT_UNLOCKED();
    195 		kp = pool_get(&ksiginfo_pool, PR_NOWAIT);
    196 		if (kp == NULL) {
    197 #ifdef DIAGNOSTIC
    198 			printf("Out of memory allocating siginfo for pid %d\n",
    199 			    p->p_pid);
    200 #endif
    201 			goto out;
    202 		}
    203 	}
    204 	*kp = *ksi;
    205 	CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list);
    206 out:
    207 	simple_unlock(&p->p_sigctx.ps_silock);
    208 	splx(s);
    209 }
    210 
    211 /*
    212  * free all pending ksiginfo on exit
    213  */
    214 static void
    215 ksiginfo_exithook(struct proc *p, void *v)
    216 {
    217 	int s;
    218 
    219 	s = splsoftclock();
    220 	simple_lock(&p->p_sigctx.ps_silock);
    221 	while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) {
    222 		ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo);
    223 		CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
    224 		pool_put(&ksiginfo_pool, ksi);
    225 	}
    226 	simple_unlock(&p->p_sigctx.ps_silock);
    227 	splx(s);
    228 }
    229 
    230 /*
    231  * Initialize signal-related data structures.
    232  */
    233 void
    234 signal_init(void)
    235 {
    236 
    237 	sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
    238 
    239 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    240 	    sizeof(struct sigacts) > PAGE_SIZE ?
    241 	    &sigactspool_allocator : &pool_allocator_nointr);
    242 
    243 	exithook_establish(ksiginfo_exithook, NULL);
    244 	exechook_establish(ksiginfo_exithook, NULL);
    245 }
    246 
    247 /*
    248  * Create an initial sigctx structure, using the same signal state
    249  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
    250  * copy it from parent.
    251  */
    252 void
    253 sigactsinit(struct proc *np, struct proc *pp, int share)
    254 {
    255 	struct sigacts *ps;
    256 
    257 	if (share) {
    258 		np->p_sigacts = pp->p_sigacts;
    259 		pp->p_sigacts->sa_refcnt++;
    260 	} else {
    261 		ps = pool_get(&sigacts_pool, PR_WAITOK);
    262 		if (pp)
    263 			memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
    264 		else
    265 			memset(ps, '\0', sizeof(struct sigacts));
    266 		ps->sa_refcnt = 1;
    267 		np->p_sigacts = ps;
    268 	}
    269 }
    270 
    271 /*
    272  * Make this process not share its sigctx, maintaining all
    273  * signal state.
    274  */
    275 void
    276 sigactsunshare(struct proc *p)
    277 {
    278 	struct sigacts *oldps;
    279 
    280 	if (p->p_sigacts->sa_refcnt == 1)
    281 		return;
    282 
    283 	oldps = p->p_sigacts;
    284 	sigactsinit(p, NULL, 0);
    285 
    286 	if (--oldps->sa_refcnt == 0)
    287 		pool_put(&sigacts_pool, oldps);
    288 }
    289 
    290 /*
    291  * Release a sigctx structure.
    292  */
    293 void
    294 sigactsfree(struct sigacts *ps)
    295 {
    296 
    297 	if (--ps->sa_refcnt > 0)
    298 		return;
    299 
    300 	pool_put(&sigacts_pool, ps);
    301 }
    302 
    303 int
    304 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
    305 	struct sigaction *osa, const void *tramp, int vers)
    306 {
    307 	struct sigacts	*ps;
    308 	int		prop;
    309 
    310 	ps = p->p_sigacts;
    311 	if (signum <= 0 || signum >= NSIG)
    312 		return (EINVAL);
    313 
    314 	/*
    315 	 * Trampoline ABI version 0 is reserved for the legacy
    316 	 * kernel-provided on-stack trampoline.  Conversely, if we are
    317 	 * using a non-0 ABI version, we must have a trampoline.  Only
    318 	 * validate the vers if a new sigaction was supplied. Emulations
    319 	 * use legacy kernel trampolines with version 0, alternatively
    320 	 * check for that too.
    321 	 */
    322 	if ((vers != 0 && tramp == NULL) ||
    323 #ifdef SIGTRAMP_VALID
    324 	    (nsa != NULL &&
    325 	    ((vers == 0) ?
    326 		(p->p_emul->e_sigcode == NULL) :
    327 		!SIGTRAMP_VALID(vers))) ||
    328 #endif
    329 	    (vers == 0 && tramp != NULL))
    330 		return (EINVAL);
    331 
    332 	if (osa)
    333 		*osa = SIGACTION_PS(ps, signum);
    334 
    335 	if (nsa) {
    336 		if (nsa->sa_flags & ~SA_ALLBITS)
    337 			return (EINVAL);
    338 
    339 		prop = sigprop[signum];
    340 		if (prop & SA_CANTMASK)
    341 			return (EINVAL);
    342 
    343 		(void) splsched();	/* XXXSMP */
    344 		SIGACTION_PS(ps, signum) = *nsa;
    345 		ps->sa_sigdesc[signum].sd_tramp = tramp;
    346 		ps->sa_sigdesc[signum].sd_vers = vers;
    347 		sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    348 		if ((prop & SA_NORESET) != 0)
    349 			SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    350 		if (signum == SIGCHLD) {
    351 			if (nsa->sa_flags & SA_NOCLDSTOP)
    352 				p->p_flag |= P_NOCLDSTOP;
    353 			else
    354 				p->p_flag &= ~P_NOCLDSTOP;
    355 			if (nsa->sa_flags & SA_NOCLDWAIT) {
    356 				/*
    357 				 * Paranoia: since SA_NOCLDWAIT is implemented
    358 				 * by reparenting the dying child to PID 1 (and
    359 				 * trust it to reap the zombie), PID 1 itself
    360 				 * is forbidden to set SA_NOCLDWAIT.
    361 				 */
    362 				if (p->p_pid == 1)
    363 					p->p_flag &= ~P_NOCLDWAIT;
    364 				else
    365 					p->p_flag |= P_NOCLDWAIT;
    366 			} else
    367 				p->p_flag &= ~P_NOCLDWAIT;
    368 
    369 			if (nsa->sa_handler == SIG_IGN) {
    370 				/*
    371 				 * Paranoia: same as above.
    372 				 */
    373 				if (p->p_pid == 1)
    374 					p->p_flag &= ~P_CLDSIGIGN;
    375 				else
    376 					p->p_flag |= P_CLDSIGIGN;
    377 			} else
    378 				p->p_flag &= ~P_CLDSIGIGN;
    379 
    380 		}
    381 		if ((nsa->sa_flags & SA_NODEFER) == 0)
    382 			sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    383 		else
    384 			sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    385 		/*
    386 	 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    387 		 * SIG_IGN, and for signals set to SIG_DFL where the default is
    388 		 * to ignore. However, don't put SIGCONT in
    389 		 * p_sigctx.ps_sigignore, as we have to restart the process.
    390 	 	 */
    391 		if (nsa->sa_handler == SIG_IGN ||
    392 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    393 						/* never to be seen again */
    394 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    395 			if (signum != SIGCONT) {
    396 						/* easier in psignal */
    397 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    398 			}
    399 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    400 		} else {
    401 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
    402 			if (nsa->sa_handler == SIG_DFL)
    403 				sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    404 			else
    405 				sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    406 		}
    407 		(void) spl0();
    408 	}
    409 
    410 	return (0);
    411 }
    412 
    413 #ifdef COMPAT_16
    414 /* ARGSUSED */
    415 int
    416 compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
    417 {
    418 	struct compat_16_sys___sigaction14_args /* {
    419 		syscallarg(int)				signum;
    420 		syscallarg(const struct sigaction *)	nsa;
    421 		syscallarg(struct sigaction *)		osa;
    422 	} */ *uap = v;
    423 	struct proc		*p;
    424 	struct sigaction	nsa, osa;
    425 	int			error;
    426 
    427 	if (SCARG(uap, nsa)) {
    428 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    429 		if (error)
    430 			return (error);
    431 	}
    432 	p = l->l_proc;
    433 	error = sigaction1(p, SCARG(uap, signum),
    434 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    435 	    NULL, 0);
    436 	if (error)
    437 		return (error);
    438 	if (SCARG(uap, osa)) {
    439 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    440 		if (error)
    441 			return (error);
    442 	}
    443 	return (0);
    444 }
    445 #endif
    446 
    447 /* ARGSUSED */
    448 int
    449 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
    450 {
    451 	struct sys___sigaction_sigtramp_args /* {
    452 		syscallarg(int)				signum;
    453 		syscallarg(const struct sigaction *)	nsa;
    454 		syscallarg(struct sigaction *)		osa;
    455 		syscallarg(void *)			tramp;
    456 		syscallarg(int)				vers;
    457 	} */ *uap = v;
    458 	struct proc *p = l->l_proc;
    459 	struct sigaction nsa, osa;
    460 	int error;
    461 
    462 	if (SCARG(uap, nsa)) {
    463 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    464 		if (error)
    465 			return (error);
    466 	}
    467 	error = sigaction1(p, SCARG(uap, signum),
    468 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    469 	    SCARG(uap, tramp), SCARG(uap, vers));
    470 	if (error)
    471 		return (error);
    472 	if (SCARG(uap, osa)) {
    473 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    474 		if (error)
    475 			return (error);
    476 	}
    477 	return (0);
    478 }
    479 
    480 /*
    481  * Initialize signal state for process 0;
    482  * set to ignore signals that are ignored by default and disable the signal
    483  * stack.
    484  */
    485 void
    486 siginit(struct proc *p)
    487 {
    488 	struct sigacts	*ps;
    489 	int		signum, prop;
    490 
    491 	ps = p->p_sigacts;
    492 	sigemptyset(&contsigmask);
    493 	sigemptyset(&stopsigmask);
    494 	sigemptyset(&sigcantmask);
    495 	for (signum = 1; signum < NSIG; signum++) {
    496 		prop = sigprop[signum];
    497 		if (prop & SA_CONT)
    498 			sigaddset(&contsigmask, signum);
    499 		if (prop & SA_STOP)
    500 			sigaddset(&stopsigmask, signum);
    501 		if (prop & SA_CANTMASK)
    502 			sigaddset(&sigcantmask, signum);
    503 		if (prop & SA_IGNORE && signum != SIGCONT)
    504 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    505 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    506 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    507 	}
    508 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    509 	p->p_sigctx.ps_sigwaited = NULL;
    510 	p->p_flag &= ~P_NOCLDSTOP;
    511 
    512 	/*
    513 	 * Reset stack state to the user stack.
    514 	 */
    515 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    516 	p->p_sigctx.ps_sigstk.ss_size = 0;
    517 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    518 
    519 	/* One reference. */
    520 	ps->sa_refcnt = 1;
    521 }
    522 
    523 /*
    524  * Reset signals for an exec of the specified process.
    525  */
    526 void
    527 execsigs(struct proc *p)
    528 {
    529 	struct sigacts	*ps;
    530 	int		signum, prop;
    531 
    532 	sigactsunshare(p);
    533 
    534 	ps = p->p_sigacts;
    535 
    536 	/*
    537 	 * Reset caught signals.  Held signals remain held
    538 	 * through p_sigctx.ps_sigmask (unless they were caught,
    539 	 * and are now ignored by default).
    540 	 */
    541 	for (signum = 1; signum < NSIG; signum++) {
    542 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
    543 			prop = sigprop[signum];
    544 			if (prop & SA_IGNORE) {
    545 				if ((prop & SA_CONT) == 0)
    546 					sigaddset(&p->p_sigctx.ps_sigignore,
    547 					    signum);
    548 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    549 			}
    550 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    551 		}
    552 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    553 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    554 	}
    555 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    556 	p->p_sigctx.ps_sigwaited = NULL;
    557 
    558 	/*
    559 	 * Reset no zombies if child dies flag as Solaris does.
    560 	 */
    561 	p->p_flag &= ~(P_NOCLDWAIT | P_CLDSIGIGN);
    562 	if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN)
    563 		SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL;
    564 
    565 	/*
    566 	 * Reset stack state to the user stack.
    567 	 */
    568 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    569 	p->p_sigctx.ps_sigstk.ss_size = 0;
    570 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    571 }
    572 
    573 int
    574 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
    575 {
    576 
    577 	if (oss)
    578 		*oss = p->p_sigctx.ps_sigmask;
    579 
    580 	if (nss) {
    581 		(void)splsched();	/* XXXSMP */
    582 		switch (how) {
    583 		case SIG_BLOCK:
    584 			sigplusset(nss, &p->p_sigctx.ps_sigmask);
    585 			break;
    586 		case SIG_UNBLOCK:
    587 			sigminusset(nss, &p->p_sigctx.ps_sigmask);
    588 			CHECKSIGS(p);
    589 			break;
    590 		case SIG_SETMASK:
    591 			p->p_sigctx.ps_sigmask = *nss;
    592 			CHECKSIGS(p);
    593 			break;
    594 		default:
    595 			(void)spl0();	/* XXXSMP */
    596 			return (EINVAL);
    597 		}
    598 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    599 		(void)spl0();		/* XXXSMP */
    600 	}
    601 
    602 	return (0);
    603 }
    604 
    605 /*
    606  * Manipulate signal mask.
    607  * Note that we receive new mask, not pointer,
    608  * and return old mask as return value;
    609  * the library stub does the rest.
    610  */
    611 int
    612 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
    613 {
    614 	struct sys___sigprocmask14_args /* {
    615 		syscallarg(int)			how;
    616 		syscallarg(const sigset_t *)	set;
    617 		syscallarg(sigset_t *)		oset;
    618 	} */ *uap = v;
    619 	struct proc	*p;
    620 	sigset_t	nss, oss;
    621 	int		error;
    622 
    623 	if (SCARG(uap, set)) {
    624 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    625 		if (error)
    626 			return (error);
    627 	}
    628 	p = l->l_proc;
    629 	error = sigprocmask1(p, SCARG(uap, how),
    630 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    631 	if (error)
    632 		return (error);
    633 	if (SCARG(uap, oset)) {
    634 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    635 		if (error)
    636 			return (error);
    637 	}
    638 	return (0);
    639 }
    640 
    641 void
    642 sigpending1(struct proc *p, sigset_t *ss)
    643 {
    644 
    645 	*ss = p->p_sigctx.ps_siglist;
    646 	sigminusset(&p->p_sigctx.ps_sigmask, ss);
    647 }
    648 
    649 /* ARGSUSED */
    650 int
    651 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
    652 {
    653 	struct sys___sigpending14_args /* {
    654 		syscallarg(sigset_t *)	set;
    655 	} */ *uap = v;
    656 	struct proc	*p;
    657 	sigset_t	ss;
    658 
    659 	p = l->l_proc;
    660 	sigpending1(p, &ss);
    661 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    662 }
    663 
    664 int
    665 sigsuspend1(struct proc *p, const sigset_t *ss)
    666 {
    667 	struct sigacts *ps;
    668 
    669 	ps = p->p_sigacts;
    670 	if (ss) {
    671 		/*
    672 		 * When returning from sigpause, we want
    673 		 * the old mask to be restored after the
    674 		 * signal handler has finished.  Thus, we
    675 		 * save it here and mark the sigctx structure
    676 		 * to indicate this.
    677 		 */
    678 		p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
    679 		p->p_sigctx.ps_flags |= SAS_OLDMASK;
    680 		(void) splsched();	/* XXXSMP */
    681 		p->p_sigctx.ps_sigmask = *ss;
    682 		CHECKSIGS(p);
    683 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    684 		(void) spl0();		/* XXXSMP */
    685 	}
    686 
    687 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    688 		/* void */;
    689 
    690 	/* always return EINTR rather than ERESTART... */
    691 	return (EINTR);
    692 }
    693 
    694 /*
    695  * Suspend process until signal, providing mask to be set
    696  * in the meantime.  Note nonstandard calling convention:
    697  * libc stub passes mask, not pointer, to save a copyin.
    698  */
    699 /* ARGSUSED */
    700 int
    701 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
    702 {
    703 	struct sys___sigsuspend14_args /* {
    704 		syscallarg(const sigset_t *)	set;
    705 	} */ *uap = v;
    706 	struct proc	*p;
    707 	sigset_t	ss;
    708 	int		error;
    709 
    710 	if (SCARG(uap, set)) {
    711 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    712 		if (error)
    713 			return (error);
    714 	}
    715 
    716 	p = l->l_proc;
    717 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
    718 }
    719 
    720 int
    721 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
    722 	struct sigaltstack *oss)
    723 {
    724 
    725 	if (oss)
    726 		*oss = p->p_sigctx.ps_sigstk;
    727 
    728 	if (nss) {
    729 		if (nss->ss_flags & ~SS_ALLBITS)
    730 			return (EINVAL);
    731 
    732 		if (nss->ss_flags & SS_DISABLE) {
    733 			if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    734 				return (EINVAL);
    735 		} else {
    736 			if (nss->ss_size < MINSIGSTKSZ)
    737 				return (ENOMEM);
    738 		}
    739 		p->p_sigctx.ps_sigstk = *nss;
    740 	}
    741 
    742 	return (0);
    743 }
    744 
    745 /* ARGSUSED */
    746 int
    747 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
    748 {
    749 	struct sys___sigaltstack14_args /* {
    750 		syscallarg(const struct sigaltstack *)	nss;
    751 		syscallarg(struct sigaltstack *)	oss;
    752 	} */ *uap = v;
    753 	struct proc		*p;
    754 	struct sigaltstack	nss, oss;
    755 	int			error;
    756 
    757 	if (SCARG(uap, nss)) {
    758 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    759 		if (error)
    760 			return (error);
    761 	}
    762 	p = l->l_proc;
    763 	error = sigaltstack1(p,
    764 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    765 	if (error)
    766 		return (error);
    767 	if (SCARG(uap, oss)) {
    768 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    769 		if (error)
    770 			return (error);
    771 	}
    772 	return (0);
    773 }
    774 
    775 /* ARGSUSED */
    776 int
    777 sys_kill(struct lwp *l, void *v, register_t *retval)
    778 {
    779 	struct sys_kill_args /* {
    780 		syscallarg(int)	pid;
    781 		syscallarg(int)	signum;
    782 	} */ *uap = v;
    783 	struct proc	*p;
    784 	ksiginfo_t	ksi;
    785 	int signum = SCARG(uap, signum);
    786 	int error;
    787 
    788 	if ((u_int)signum >= NSIG)
    789 		return (EINVAL);
    790 	KSI_INIT(&ksi);
    791 	ksi.ksi_signo = signum;
    792 	ksi.ksi_code = SI_USER;
    793 	ksi.ksi_pid = l->l_proc->p_pid;
    794 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    795 	if (SCARG(uap, pid) > 0) {
    796 		/* kill single process */
    797 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    798 			return (ESRCH);
    799 		error = kauth_authorize_process(l->l_cred,
    800 		    KAUTH_PROCESS_CANSIGNAL, p, (void *)(uintptr_t)signum,
    801 		    NULL, NULL);
    802 		if (error)
    803 			return error;
    804 		if (signum)
    805 			kpsignal2(p, &ksi);
    806 		return (0);
    807 	}
    808 	switch (SCARG(uap, pid)) {
    809 	case -1:		/* broadcast signal */
    810 		return (killpg1(l, &ksi, 0, 1));
    811 	case 0:			/* signal own process group */
    812 		return (killpg1(l, &ksi, 0, 0));
    813 	default:		/* negative explicit process group */
    814 		return (killpg1(l, &ksi, -SCARG(uap, pid), 0));
    815 	}
    816 	/* NOTREACHED */
    817 }
    818 
    819 /*
    820  * Common code for kill process group/broadcast kill.
    821  * cp is calling process.
    822  */
    823 int
    824 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
    825 {
    826 	struct proc	*p, *cp;
    827 	kauth_cred_t	pc;
    828 	struct pgrp	*pgrp;
    829 	int		nfound;
    830 	int		signum = ksi->ksi_signo;
    831 
    832 	cp = l->l_proc;
    833 	pc = l->l_cred;
    834 	nfound = 0;
    835 	if (all) {
    836 		/*
    837 		 * broadcast
    838 		 */
    839 		proclist_lock_read();
    840 		PROCLIST_FOREACH(p, &allproc) {
    841 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp ||
    842 			    kauth_authorize_process(pc, KAUTH_PROCESS_CANSIGNAL,
    843 			    p, (void *)(uintptr_t)signum, NULL, NULL) != 0)
    844 				continue;
    845 			nfound++;
    846 			if (signum)
    847 				kpsignal2(p, ksi);
    848 		}
    849 		proclist_unlock_read();
    850 	} else {
    851 		if (pgid == 0)
    852 			/*
    853 			 * zero pgid means send to my process group.
    854 			 */
    855 			pgrp = cp->p_pgrp;
    856 		else {
    857 			pgrp = pgfind(pgid);
    858 			if (pgrp == NULL)
    859 				return (ESRCH);
    860 		}
    861 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
    862 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    863 			    kauth_authorize_process(pc, KAUTH_PROCESS_CANSIGNAL,
    864 			    p, (void *)(uintptr_t)signum, NULL, NULL) != 0)
    865 				continue;
    866 			nfound++;
    867 			if (signum && P_ZOMBIE(p) == 0)
    868 				kpsignal2(p, ksi);
    869 		}
    870 	}
    871 	return (nfound ? 0 : ESRCH);
    872 }
    873 
    874 /*
    875  * Send a signal to a process group.
    876  */
    877 void
    878 gsignal(int pgid, int signum)
    879 {
    880 	ksiginfo_t ksi;
    881 	KSI_INIT_EMPTY(&ksi);
    882 	ksi.ksi_signo = signum;
    883 	kgsignal(pgid, &ksi, NULL);
    884 }
    885 
    886 void
    887 kgsignal(int pgid, ksiginfo_t *ksi, void *data)
    888 {
    889 	struct pgrp *pgrp;
    890 
    891 	if (pgid && (pgrp = pgfind(pgid)))
    892 		kpgsignal(pgrp, ksi, data, 0);
    893 }
    894 
    895 /*
    896  * Send a signal to a process group. If checktty is 1,
    897  * limit to members which have a controlling terminal.
    898  */
    899 void
    900 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
    901 {
    902 	ksiginfo_t ksi;
    903 	KSI_INIT_EMPTY(&ksi);
    904 	ksi.ksi_signo = sig;
    905 	kpgsignal(pgrp, &ksi, NULL, checkctty);
    906 }
    907 
    908 void
    909 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    910 {
    911 	struct proc *p;
    912 
    913 	if (pgrp)
    914 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
    915 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    916 				kpsignal(p, ksi, data);
    917 }
    918 
    919 /*
    920  * Send a signal caused by a trap to the current process.
    921  * If it will be caught immediately, deliver it with correct code.
    922  * Otherwise, post it normally.
    923  */
    924 void
    925 trapsignal(struct lwp *l, const ksiginfo_t *ksi)
    926 {
    927 	struct proc	*p;
    928 	struct sigacts	*ps;
    929 	int signum = ksi->ksi_signo;
    930 
    931 	KASSERT(KSI_TRAP_P(ksi));
    932 
    933 	p = l->l_proc;
    934 	ps = p->p_sigacts;
    935 	if ((p->p_flag & P_TRACED) == 0 &&
    936 	    sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
    937 	    !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
    938 		p->p_stats->p_ru.ru_nsignals++;
    939 #ifdef KTRACE
    940 		if (KTRPOINT(p, KTR_PSIG))
    941 			ktrpsig(l, signum, SIGACTION_PS(ps, signum).sa_handler,
    942 			    &p->p_sigctx.ps_sigmask, ksi);
    943 #endif
    944 		kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask);
    945 		(void) splsched();	/* XXXSMP */
    946 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
    947 		    &p->p_sigctx.ps_sigmask);
    948 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
    949 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    950 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    951 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    952 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    953 		}
    954 		(void) spl0();		/* XXXSMP */
    955 	} else {
    956 		p->p_sigctx.ps_lwp = l->l_lid;
    957 		/* XXX for core dump/debugger */
    958 		p->p_sigctx.ps_signo = ksi->ksi_signo;
    959 		p->p_sigctx.ps_code = ksi->ksi_trap;
    960 		kpsignal2(p, ksi);
    961 	}
    962 }
    963 
    964 /*
    965  * Fill in signal information and signal the parent for a child status change.
    966  */
    967 void
    968 child_psignal(struct proc *p)
    969 {
    970 	ksiginfo_t ksi;
    971 
    972 	KSI_INIT(&ksi);
    973 	ksi.ksi_signo = SIGCHLD;
    974 	ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED;
    975 	ksi.ksi_pid = p->p_pid;
    976 	ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
    977 	ksi.ksi_status = p->p_xstat;
    978 	ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
    979 	ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
    980 	kpsignal2(p->p_pptr, &ksi);
    981 }
    982 
    983 /*
    984  * Send the signal to the process.  If the signal has an action, the action
    985  * is usually performed by the target process rather than the caller; we add
    986  * the signal to the set of pending signals for the process.
    987  *
    988  * Exceptions:
    989  *   o When a stop signal is sent to a sleeping process that takes the
    990  *     default action, the process is stopped without awakening it.
    991  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    992  *     regardless of the signal action (eg, blocked or ignored).
    993  *
    994  * Other ignored signals are discarded immediately.
    995  */
    996 void
    997 psignal(struct proc *p, int signum)
    998 {
    999 	ksiginfo_t ksi;
   1000 
   1001 	KSI_INIT_EMPTY(&ksi);
   1002 	ksi.ksi_signo = signum;
   1003 	kpsignal2(p, &ksi);
   1004 }
   1005 
   1006 void
   1007 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
   1008 {
   1009 
   1010 	if ((p->p_flag & P_WEXIT) == 0 && data) {
   1011 		size_t fd;
   1012 		struct filedesc *fdp = p->p_fd;
   1013 
   1014 		ksi->ksi_fd = -1;
   1015 		for (fd = 0; fd < fdp->fd_nfiles; fd++) {
   1016 			struct file *fp = fdp->fd_ofiles[fd];
   1017 			/* XXX: lock? */
   1018 			if (fp && fp->f_data == data) {
   1019 				ksi->ksi_fd = fd;
   1020 				break;
   1021 			}
   1022 		}
   1023 	}
   1024 	kpsignal2(p, ksi);
   1025 }
   1026 
   1027 static void
   1028 kpsignal2(struct proc *p, const ksiginfo_t *ksi)
   1029 {
   1030 	struct lwp *l, *suspended = NULL;
   1031 	struct sadata_vp *vp;
   1032 	ksiginfo_t *newkp;
   1033 	int	s = 0, prop, allsusp;
   1034 	sig_t	action;
   1035 	int	signum = ksi->ksi_signo;
   1036 
   1037 #ifdef DIAGNOSTIC
   1038 	if (signum <= 0 || signum >= NSIG)
   1039 		panic("psignal signal number %d", signum);
   1040 
   1041 	SCHED_ASSERT_UNLOCKED();
   1042 #endif
   1043 
   1044 	/*
   1045 	 * Notify any interested parties in the signal.
   1046 	 */
   1047 	KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
   1048 
   1049 	prop = sigprop[signum];
   1050 
   1051 	/*
   1052 	 * If proc is traced, always give parent a chance.
   1053 	 */
   1054 	if (p->p_flag & P_TRACED) {
   1055 		action = SIG_DFL;
   1056 
   1057 		/*
   1058 		 * If the process is being traced and the signal is being
   1059 		 * caught, make sure to save any ksiginfo.
   1060 		 */
   1061 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
   1062 			SCHED_ASSERT_UNLOCKED();
   1063 			ksiginfo_queue(p, ksi, NULL);
   1064 		}
   1065 	} else {
   1066 		/*
   1067 		 * If the signal was the result of a trap, reset it
   1068 		 * to default action if it's currently masked, so that it would
   1069 		 * coredump immediatelly instead of spinning repeatedly
   1070 		 * taking the signal.
   1071 		 */
   1072 		if (KSI_TRAP_P(ksi)
   1073 		    && sigismember(&p->p_sigctx.ps_sigmask, signum)
   1074 		    && !sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
   1075 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
   1076 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1077 			sigdelset(&p->p_sigctx.ps_sigmask, signum);
   1078 			SIGACTION(p, signum).sa_handler = SIG_DFL;
   1079 		}
   1080 
   1081 		/*
   1082 		 * If the signal is being ignored,
   1083 		 * then we forget about it immediately.
   1084 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
   1085 		 * and if it is set to SIG_IGN,
   1086 		 * action will be SIG_DFL here.)
   1087 		 */
   1088 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
   1089 			return;
   1090 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1091 			action = SIG_HOLD;
   1092 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
   1093 			action = SIG_CATCH;
   1094 		else {
   1095 			action = SIG_DFL;
   1096 
   1097 			if (prop & SA_KILL && p->p_nice > NZERO)
   1098 				p->p_nice = NZERO;
   1099 
   1100 			/*
   1101 			 * If sending a tty stop signal to a member of an
   1102 			 * orphaned process group, discard the signal here if
   1103 			 * the action is default; don't stop the process below
   1104 			 * if sleeping, and don't clear any pending SIGCONT.
   1105 			 */
   1106 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
   1107 				return;
   1108 		}
   1109 	}
   1110 
   1111 	if (prop & SA_CONT)
   1112 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
   1113 
   1114 	if (prop & SA_STOP)
   1115 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
   1116 
   1117 	/*
   1118 	 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
   1119 	 * please!), check if anything waits on it. If yes, save the
   1120 	 * info into provided ps_sigwaited, and wake-up the waiter.
   1121 	 * The signal won't be processed further here.
   1122 	 */
   1123 	if ((prop & SA_CANTMASK) == 0
   1124 	    && p->p_sigctx.ps_sigwaited
   1125 	    && sigismember(p->p_sigctx.ps_sigwait, signum)
   1126 	    && p->p_stat != SSTOP) {
   1127 		p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info;
   1128 		p->p_sigctx.ps_sigwaited = NULL;
   1129 		wakeup_one(&p->p_sigctx.ps_sigwait);
   1130 		return;
   1131 	}
   1132 
   1133 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1134 
   1135 	/* CHECKSIGS() is "inlined" here. */
   1136 	p->p_sigctx.ps_sigcheck = 1;
   1137 
   1138 	/*
   1139 	 * Defer further processing for signals which are held,
   1140 	 * except that stopped processes must be continued by SIGCONT.
   1141 	 */
   1142 	if (action == SIG_HOLD &&
   1143 	    ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) {
   1144 		SCHED_ASSERT_UNLOCKED();
   1145 		ksiginfo_queue(p, ksi, NULL);
   1146 		return;
   1147 	}
   1148 
   1149 	/*
   1150 	 * Allocate a ksiginfo_t incase we need to insert it with the
   1151 	 * scheduler lock held, but only if this ksiginfo_t isn't empty.
   1152 	 */
   1153 	if (!KSI_EMPTY_P(ksi)) {
   1154 		newkp = pool_get(&ksiginfo_pool, PR_NOWAIT);
   1155 		if (newkp == NULL) {
   1156 #ifdef DIAGNOSTIC
   1157 			printf("kpsignal2: couldn't allocated from ksiginfo_pool\n");
   1158 #endif
   1159 			return;
   1160 		}
   1161 	} else
   1162 		newkp = NULL;
   1163 
   1164 	SCHED_LOCK(s);
   1165 
   1166 	if (p->p_flag & P_SA) {
   1167 		allsusp = 0;
   1168 		l = NULL;
   1169 		if (p->p_stat == SACTIVE) {
   1170 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1171 				l = vp->savp_lwp;
   1172 				KDASSERT(l != NULL);
   1173 				if (l->l_flag & L_SA_IDLE) {
   1174 					/* wakeup idle LWP */
   1175 					goto found;
   1176 					/*NOTREACHED*/
   1177 				} else if (l->l_flag & L_SA_YIELD) {
   1178 					/* idle LWP is already waking up */
   1179 					goto out;
   1180 					/*NOTREACHED*/
   1181 				}
   1182 			}
   1183 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1184 				l = vp->savp_lwp;
   1185 				if (l->l_stat == LSRUN ||
   1186 				    l->l_stat == LSONPROC) {
   1187 					signotify(p);
   1188 					goto out;
   1189 					/*NOTREACHED*/
   1190 				}
   1191 				if (l->l_stat == LSSLEEP &&
   1192 				    l->l_flag & L_SINTR) {
   1193 					/* ok to signal vp lwp */
   1194 					break;
   1195 				} else
   1196 					l = NULL;
   1197 			}
   1198 		} else if (p->p_stat == SSTOP) {
   1199 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1200 				l = vp->savp_lwp;
   1201 				if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0)
   1202 					break;
   1203 				l = NULL;
   1204 			}
   1205 		}
   1206 	} else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) {
   1207 		/*
   1208 		 * At least one LWP is running or on a run queue.
   1209 		 * The signal will be noticed when one of them returns
   1210 		 * to userspace.
   1211 		 */
   1212 		signotify(p);
   1213 		/*
   1214 		 * The signal will be noticed very soon.
   1215 		 */
   1216 		goto out;
   1217 		/*NOTREACHED*/
   1218 	} else {
   1219 		/*
   1220 		 * Find out if any of the sleeps are interruptable,
   1221 		 * and if all the live LWPs remaining are suspended.
   1222 		 */
   1223 		allsusp = 1;
   1224 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1225 			if (l->l_stat == LSSLEEP &&
   1226 			    l->l_flag & L_SINTR)
   1227 				break;
   1228 			if (l->l_stat == LSSUSPENDED)
   1229 				suspended = l;
   1230 			else if ((l->l_stat != LSZOMB) &&
   1231 			    (l->l_stat != LSDEAD))
   1232 				allsusp = 0;
   1233 		}
   1234 	}
   1235 
   1236  found:
   1237 	switch (p->p_stat) {
   1238 	case SACTIVE:
   1239 
   1240 		if (l != NULL && (p->p_flag & P_TRACED))
   1241 			goto run;
   1242 
   1243 		/*
   1244 		 * If SIGCONT is default (or ignored) and process is
   1245 		 * asleep, we are finished; the process should not
   1246 		 * be awakened.
   1247 		 */
   1248 		if ((prop & SA_CONT) && action == SIG_DFL) {
   1249 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1250 			goto done;
   1251 		}
   1252 
   1253 		/*
   1254 		 * When a sleeping process receives a stop
   1255 		 * signal, process immediately if possible.
   1256 		 */
   1257 		if ((prop & SA_STOP) && action == SIG_DFL) {
   1258 			/*
   1259 			 * If a child holding parent blocked,
   1260 			 * stopping could cause deadlock.
   1261 			 */
   1262 			if (p->p_flag & P_PPWAIT) {
   1263 				goto out;
   1264 			}
   1265 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1266 			p->p_xstat = signum;
   1267 			proc_stop(p, 1);	/* XXXSMP: recurse? */
   1268 			SCHED_UNLOCK(s);
   1269 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
   1270 				child_psignal(p);
   1271 			}
   1272 			goto done_unlocked;
   1273 		}
   1274 
   1275 		if (l == NULL) {
   1276 			/*
   1277 			 * Special case: SIGKILL of a process
   1278 			 * which is entirely composed of
   1279 			 * suspended LWPs should succeed. We
   1280 			 * make this happen by unsuspending one of
   1281 			 * them.
   1282 			 */
   1283 			if (allsusp && (signum == SIGKILL)) {
   1284 				lwp_continue(suspended);
   1285 			}
   1286 			goto done;
   1287 		}
   1288 		/*
   1289 		 * All other (caught or default) signals
   1290 		 * cause the process to run.
   1291 		 */
   1292 		goto runfast;
   1293 		/*NOTREACHED*/
   1294 	case SSTOP:
   1295 		/* Process is stopped */
   1296 		/*
   1297 		 * If traced process is already stopped,
   1298 		 * then no further action is necessary.
   1299 		 */
   1300 		if (p->p_flag & P_TRACED)
   1301 			goto done;
   1302 
   1303 		/*
   1304 		 * Kill signal always sets processes running,
   1305 		 * if possible.
   1306 		 */
   1307 		if (signum == SIGKILL) {
   1308 			l = proc_unstop(p);
   1309 			if (l)
   1310 				goto runfast;
   1311 			goto done;
   1312 		}
   1313 
   1314 		if (prop & SA_CONT) {
   1315 			/*
   1316 			 * If SIGCONT is default (or ignored),
   1317 			 * we continue the process but don't
   1318 			 * leave the signal in ps_siglist, as
   1319 			 * it has no further action.  If
   1320 			 * SIGCONT is held, we continue the
   1321 			 * process and leave the signal in
   1322 			 * ps_siglist.  If the process catches
   1323 			 * SIGCONT, let it handle the signal
   1324 			 * itself.  If it isn't waiting on an
   1325 			 * event, then it goes back to run
   1326 			 * state.  Otherwise, process goes
   1327 			 * back to sleep state.
   1328 			 */
   1329 			if (action == SIG_DFL)
   1330 				sigdelset(&p->p_sigctx.ps_siglist,
   1331 				    signum);
   1332 			l = proc_unstop(p);
   1333 			if (l && (action == SIG_CATCH))
   1334 				goto runfast;
   1335 			goto out;
   1336 		}
   1337 
   1338 		if (prop & SA_STOP) {
   1339 			/*
   1340 			 * Already stopped, don't need to stop again.
   1341 			 * (If we did the shell could get confused.)
   1342 			 */
   1343 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1344 			goto done;
   1345 		}
   1346 
   1347 		/*
   1348 		 * If a lwp is sleeping interruptibly, then
   1349 		 * wake it up; it will run until the kernel
   1350 		 * boundary, where it will stop in issignal(),
   1351 		 * since p->p_stat is still SSTOP. When the
   1352 		 * process is continued, it will be made
   1353 		 * runnable and can look at the signal.
   1354 		 */
   1355 		if (l)
   1356 			goto run;
   1357 		goto out;
   1358 	case SIDL:
   1359 		/* Process is being created by fork */
   1360 		/* XXX: We are not ready to receive signals yet */
   1361 		goto done;
   1362 	default:
   1363 		/* Else what? */
   1364 		panic("psignal: Invalid process state %d.", p->p_stat);
   1365 	}
   1366 	/*NOTREACHED*/
   1367 
   1368  runfast:
   1369 	if (action == SIG_CATCH) {
   1370 		ksiginfo_queue(p, ksi, &newkp);
   1371 		action = SIG_HOLD;
   1372 	}
   1373 	/*
   1374 	 * Raise priority to at least PUSER.
   1375 	 */
   1376 	if (l->l_priority > PUSER)
   1377 		l->l_priority = PUSER;
   1378  run:
   1379 	if (action == SIG_CATCH) {
   1380 		ksiginfo_queue(p, ksi, &newkp);
   1381 		action = SIG_HOLD;
   1382 	}
   1383 
   1384 	setrunnable(l);		/* XXXSMP: recurse? */
   1385  out:
   1386 	if (action == SIG_CATCH)
   1387 		ksiginfo_queue(p, ksi, &newkp);
   1388  done:
   1389 	SCHED_UNLOCK(s);
   1390 
   1391  done_unlocked:
   1392 	if (newkp)
   1393 		pool_put(&ksiginfo_pool, newkp);
   1394 }
   1395 
   1396 siginfo_t *
   1397 siginfo_alloc(int flags)
   1398 {
   1399 
   1400 	return pool_get(&siginfo_pool, flags);
   1401 }
   1402 
   1403 void
   1404 siginfo_free(void *arg)
   1405 {
   1406 
   1407 	pool_put(&siginfo_pool, arg);
   1408 }
   1409 
   1410 void
   1411 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
   1412 {
   1413 	struct proc *p = l->l_proc;
   1414 	struct lwp *le, *li;
   1415 	siginfo_t *si;
   1416 	int f;
   1417 
   1418 	if (p->p_flag & P_SA) {
   1419 
   1420 		/* XXXUPSXXX What if not on sa_vp ? */
   1421 
   1422 		f = l->l_flag & L_SA;
   1423 		l->l_flag &= ~L_SA;
   1424 		si = siginfo_alloc(PR_WAITOK);
   1425 		si->_info = ksi->ksi_info;
   1426 		le = li = NULL;
   1427 		if (KSI_TRAP_P(ksi))
   1428 			le = l;
   1429 		else
   1430 			li = l;
   1431 		if (sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
   1432 		    sizeof(*si), si, siginfo_free) != 0) {
   1433 			siginfo_free(si);
   1434 #if 0
   1435 			if (KSI_TRAP_P(ksi))
   1436 				/* XXX What do we do here?? */;
   1437 #endif
   1438 		}
   1439 		l->l_flag |= f;
   1440 		return;
   1441 	}
   1442 
   1443 	(*p->p_emul->e_sendsig)(ksi, mask);
   1444 }
   1445 
   1446 static inline int firstsig(const sigset_t *);
   1447 
   1448 static inline int
   1449 firstsig(const sigset_t *ss)
   1450 {
   1451 	int sig;
   1452 
   1453 	sig = ffs(ss->__bits[0]);
   1454 	if (sig != 0)
   1455 		return (sig);
   1456 #if NSIG > 33
   1457 	sig = ffs(ss->__bits[1]);
   1458 	if (sig != 0)
   1459 		return (sig + 32);
   1460 #endif
   1461 #if NSIG > 65
   1462 	sig = ffs(ss->__bits[2]);
   1463 	if (sig != 0)
   1464 		return (sig + 64);
   1465 #endif
   1466 #if NSIG > 97
   1467 	sig = ffs(ss->__bits[3]);
   1468 	if (sig != 0)
   1469 		return (sig + 96);
   1470 #endif
   1471 	return (0);
   1472 }
   1473 
   1474 /*
   1475  * If the current process has received a signal (should be caught or cause
   1476  * termination, should interrupt current syscall), return the signal number.
   1477  * Stop signals with default action are processed immediately, then cleared;
   1478  * they aren't returned.  This is checked after each entry to the system for
   1479  * a syscall or trap (though this can usually be done without calling issignal
   1480  * by checking the pending signal masks in the CURSIG macro.) The normal call
   1481  * sequence is
   1482  *
   1483  *	while (signum = CURSIG(curlwp))
   1484  *		postsig(signum);
   1485  */
   1486 int
   1487 issignal(struct lwp *l)
   1488 {
   1489 	struct proc	*p = l->l_proc;
   1490 	int		s, signum, prop;
   1491 	sigset_t	ss;
   1492 
   1493 	/* Bail out if we do not own the virtual processor */
   1494 	if (l->l_flag & L_SA && l->l_savp->savp_lwp != l)
   1495 		return 0;
   1496 
   1497 	if (p->p_stat == SSTOP) {
   1498 		/*
   1499 		 * The process is stopped/stopping. Stop ourselves now that
   1500 		 * we're on the kernel/userspace boundary.
   1501 		 */
   1502 		SCHED_LOCK(s);
   1503 		l->l_stat = LSSTOP;
   1504 		p->p_nrlwps--;
   1505 		if (p->p_flag & P_TRACED)
   1506 			goto sigtraceswitch;
   1507 		else
   1508 			goto sigswitch;
   1509 	}
   1510 	for (;;) {
   1511 		sigpending1(p, &ss);
   1512 		if (p->p_flag & P_PPWAIT)
   1513 			sigminusset(&stopsigmask, &ss);
   1514 		signum = firstsig(&ss);
   1515 		if (signum == 0) {		 	/* no signal to send */
   1516 			p->p_sigctx.ps_sigcheck = 0;
   1517 			return (0);
   1518 		}
   1519 							/* take the signal! */
   1520 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   1521 
   1522 		/*
   1523 		 * We should see pending but ignored signals
   1524 		 * only if P_TRACED was on when they were posted.
   1525 		 */
   1526 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
   1527 		    (p->p_flag & P_TRACED) == 0)
   1528 			continue;
   1529 
   1530 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1531 			/*
   1532 			 * If traced, always stop, and stay
   1533 			 * stopped until released by the debugger.
   1534 			 */
   1535 			p->p_xstat = signum;
   1536 
   1537 			/* Emulation-specific handling of signal trace */
   1538 			if ((p->p_emul->e_tracesig != NULL) &&
   1539 			    ((*p->p_emul->e_tracesig)(p, signum) != 0))
   1540 				goto childresumed;
   1541 
   1542 			if ((p->p_flag & P_FSTRACE) == 0)
   1543 				child_psignal(p);
   1544 			SCHED_LOCK(s);
   1545 			proc_stop(p, 1);
   1546 		sigtraceswitch:
   1547 			mi_switch(l, NULL);
   1548 			SCHED_ASSERT_UNLOCKED();
   1549 			splx(s);
   1550 
   1551 		childresumed:
   1552 			/*
   1553 			 * If we are no longer being traced, or the parent
   1554 			 * didn't give us a signal, look for more signals.
   1555 			 */
   1556 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1557 				continue;
   1558 
   1559 			/*
   1560 			 * If the new signal is being masked, look for other
   1561 			 * signals.
   1562 			 */
   1563 			signum = p->p_xstat;
   1564 			p->p_xstat = 0;
   1565 			/*
   1566 			 * `p->p_sigctx.ps_siglist |= mask' is done
   1567 			 * in setrunnable().
   1568 			 */
   1569 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1570 				continue;
   1571 							/* take the signal! */
   1572 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1573 		}
   1574 
   1575 		prop = sigprop[signum];
   1576 
   1577 		/*
   1578 		 * Decide whether the signal should be returned.
   1579 		 * Return the signal's number, or fall through
   1580 		 * to clear it from the pending mask.
   1581 		 */
   1582 		switch ((long)SIGACTION(p, signum).sa_handler) {
   1583 
   1584 		case (long)SIG_DFL:
   1585 			/*
   1586 			 * Don't take default actions on system processes.
   1587 			 */
   1588 			if (p->p_pid <= 1) {
   1589 #ifdef DIAGNOSTIC
   1590 				/*
   1591 				 * Are you sure you want to ignore SIGSEGV
   1592 				 * in init? XXX
   1593 				 */
   1594 				printf("Process (pid %d) got signal %d\n",
   1595 				    p->p_pid, signum);
   1596 #endif
   1597 				break;		/* == ignore */
   1598 			}
   1599 			/*
   1600 			 * If there is a pending stop signal to process
   1601 			 * with default action, stop here,
   1602 			 * then clear the signal.  However,
   1603 			 * if process is member of an orphaned
   1604 			 * process group, ignore tty stop signals.
   1605 			 */
   1606 			if (prop & SA_STOP) {
   1607 				if (p->p_flag & P_TRACED ||
   1608 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1609 				    prop & SA_TTYSTOP))
   1610 					break;	/* == ignore */
   1611 				p->p_xstat = signum;
   1612 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1613 					child_psignal(p);
   1614 				SCHED_LOCK(s);
   1615 				proc_stop(p, 1);
   1616 			sigswitch:
   1617 				mi_switch(l, NULL);
   1618 				SCHED_ASSERT_UNLOCKED();
   1619 				splx(s);
   1620 				break;
   1621 			} else if (prop & SA_IGNORE) {
   1622 				/*
   1623 				 * Except for SIGCONT, shouldn't get here.
   1624 				 * Default action is to ignore; drop it.
   1625 				 */
   1626 				break;		/* == ignore */
   1627 			} else
   1628 				goto keep;
   1629 			/*NOTREACHED*/
   1630 
   1631 		case (long)SIG_IGN:
   1632 			/*
   1633 			 * Masking above should prevent us ever trying
   1634 			 * to take action on an ignored signal other
   1635 			 * than SIGCONT, unless process is traced.
   1636 			 */
   1637 #ifdef DEBUG_ISSIGNAL
   1638 			if ((prop & SA_CONT) == 0 &&
   1639 			    (p->p_flag & P_TRACED) == 0)
   1640 				printf("issignal\n");
   1641 #endif
   1642 			break;		/* == ignore */
   1643 
   1644 		default:
   1645 			/*
   1646 			 * This signal has an action, let
   1647 			 * postsig() process it.
   1648 			 */
   1649 			goto keep;
   1650 		}
   1651 	}
   1652 	/* NOTREACHED */
   1653 
   1654  keep:
   1655 						/* leave the signal for later */
   1656 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1657 	CHECKSIGS(p);
   1658 	return (signum);
   1659 }
   1660 
   1661 /*
   1662  * Put the argument process into the stopped state and notify the parent
   1663  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1664  * on the run queue.
   1665  */
   1666 void
   1667 proc_stop(struct proc *p, int dowakeup)
   1668 {
   1669 	struct lwp *l;
   1670 	struct proc *parent;
   1671 	struct sadata_vp *vp;
   1672 
   1673 	SCHED_ASSERT_LOCKED();
   1674 
   1675 	/* XXX lock process LWP state */
   1676 	p->p_flag &= ~P_WAITED;
   1677 	p->p_stat = SSTOP;
   1678 	parent = p->p_pptr;
   1679 	parent->p_nstopchild++;
   1680 
   1681 	if (p->p_flag & P_SA) {
   1682 		/*
   1683 		 * Only (try to) put the LWP on the VP in stopped
   1684 		 * state.
   1685 		 * All other LWPs will suspend in sa_setwoken()
   1686 		 * because the VP-LWP in stopped state cannot be
   1687 		 * repossessed.
   1688 		 */
   1689 		SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1690 			l = vp->savp_lwp;
   1691 			if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) {
   1692 				l->l_stat = LSSTOP;
   1693 				p->p_nrlwps--;
   1694 			} else if (l->l_stat == LSRUN) {
   1695 				/* Remove LWP from the run queue */
   1696 				remrunqueue(l);
   1697 				l->l_stat = LSSTOP;
   1698 				p->p_nrlwps--;
   1699 			} else if (l->l_stat == LSSLEEP &&
   1700 			    l->l_flag & L_SA_IDLE) {
   1701 				l->l_flag &= ~L_SA_IDLE;
   1702 				l->l_stat = LSSTOP;
   1703 			}
   1704 		}
   1705 		goto out;
   1706 	}
   1707 
   1708 	/*
   1709 	 * Put as many LWP's as possible in stopped state.
   1710 	 * Sleeping ones will notice the stopped state as they try to
   1711 	 * return to userspace.
   1712 	 */
   1713 
   1714 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1715 		if (l->l_stat == LSONPROC) {
   1716 			/* XXX SMP this assumes that a LWP that is LSONPROC
   1717 			 * is curlwp and hence is about to be mi_switched
   1718 			 * away; the only callers of proc_stop() are:
   1719 			 * - psignal
   1720 			 * - issignal()
   1721 			 * For the former, proc_stop() is only called when
   1722 			 * no processes are running, so we don't worry.
   1723 			 * For the latter, proc_stop() is called right
   1724 			 * before mi_switch().
   1725 			 */
   1726 			l->l_stat = LSSTOP;
   1727 			p->p_nrlwps--;
   1728 		} else if (l->l_stat == LSRUN) {
   1729 			/* Remove LWP from the run queue */
   1730 			remrunqueue(l);
   1731 			l->l_stat = LSSTOP;
   1732 			p->p_nrlwps--;
   1733 		} else if ((l->l_stat == LSSLEEP) ||
   1734 		    (l->l_stat == LSSUSPENDED) ||
   1735 		    (l->l_stat == LSZOMB) ||
   1736 		    (l->l_stat == LSDEAD)) {
   1737 			/*
   1738 			 * Don't do anything; let sleeping LWPs
   1739 			 * discover the stopped state of the process
   1740 			 * on their way out of the kernel; otherwise,
   1741 			 * things like NFS threads that sleep with
   1742 			 * locks will block the rest of the system
   1743 			 * from getting any work done.
   1744 			 *
   1745 			 * Suspended/dead/zombie LWPs aren't going
   1746 			 * anywhere, so we don't need to touch them.
   1747 			 */
   1748 		}
   1749 #ifdef DIAGNOSTIC
   1750 		else {
   1751 			panic("proc_stop: process %d lwp %d "
   1752 			      "in unstoppable state %d.\n",
   1753 			    p->p_pid, l->l_lid, l->l_stat);
   1754 		}
   1755 #endif
   1756 	}
   1757 
   1758  out:
   1759 	/* XXX unlock process LWP state */
   1760 
   1761 	if (dowakeup)
   1762 		sched_wakeup((caddr_t)p->p_pptr);
   1763 }
   1764 
   1765 /*
   1766  * Given a process in state SSTOP, set the state back to SACTIVE and
   1767  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
   1768  *
   1769  * If no LWPs ended up runnable (and therefore able to take a signal),
   1770  * return a LWP that is sleeping interruptably. The caller can wake
   1771  * that LWP up to take a signal.
   1772  */
   1773 struct lwp *
   1774 proc_unstop(struct proc *p)
   1775 {
   1776 	struct lwp *l, *lr = NULL;
   1777 	struct sadata_vp *vp;
   1778 	int cantake = 0;
   1779 
   1780 	SCHED_ASSERT_LOCKED();
   1781 
   1782 	/*
   1783 	 * Our caller wants to be informed if there are only sleeping
   1784 	 * and interruptable LWPs left after we have run so that it
   1785 	 * can invoke setrunnable() if required - return one of the
   1786 	 * interruptable LWPs if this is the case.
   1787 	 */
   1788 
   1789 	if (!(p->p_flag & P_WAITED))
   1790 		p->p_pptr->p_nstopchild--;
   1791 	p->p_stat = SACTIVE;
   1792 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1793 		if (l->l_stat == LSRUN) {
   1794 			lr = NULL;
   1795 			cantake = 1;
   1796 		}
   1797 		if (l->l_stat != LSSTOP)
   1798 			continue;
   1799 
   1800 		if (l->l_wchan != NULL) {
   1801 			l->l_stat = LSSLEEP;
   1802 			if ((cantake == 0) && (l->l_flag & L_SINTR)) {
   1803 				lr = l;
   1804 				cantake = 1;
   1805 			}
   1806 		} else {
   1807 			setrunnable(l);
   1808 			lr = NULL;
   1809 			cantake = 1;
   1810 		}
   1811 	}
   1812 	if (p->p_flag & P_SA) {
   1813 		/* Only consider returning the LWP on the VP. */
   1814 		SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1815 			lr = vp->savp_lwp;
   1816 			if (lr->l_stat == LSSLEEP) {
   1817 				if (lr->l_flag & L_SA_YIELD) {
   1818 					setrunnable(lr);
   1819 					break;
   1820 				} else if (lr->l_flag & L_SINTR)
   1821 					return lr;
   1822 			}
   1823 		}
   1824 		return NULL;
   1825 	}
   1826 	return lr;
   1827 }
   1828 
   1829 /*
   1830  * Take the action for the specified signal
   1831  * from the current set of pending signals.
   1832  */
   1833 void
   1834 postsig(int signum)
   1835 {
   1836 	struct lwp *l;
   1837 	struct proc	*p;
   1838 	struct sigacts	*ps;
   1839 	sig_t		action;
   1840 	sigset_t	*returnmask;
   1841 
   1842 	l = curlwp;
   1843 	p = l->l_proc;
   1844 	ps = p->p_sigacts;
   1845 #ifdef DIAGNOSTIC
   1846 	if (signum == 0)
   1847 		panic("postsig");
   1848 #endif
   1849 
   1850 	KERNEL_PROC_LOCK(l);
   1851 
   1852 #ifdef MULTIPROCESSOR
   1853 	/*
   1854 	 * On MP, issignal() can return the same signal to multiple
   1855 	 * LWPs.  The LWPs will block above waiting for the kernel
   1856 	 * lock and the first LWP which gets through will then remove
   1857 	 * the signal from ps_siglist.  All other LWPs exit here.
   1858 	 */
   1859 	if (!sigismember(&p->p_sigctx.ps_siglist, signum)) {
   1860 		KERNEL_PROC_UNLOCK(l);
   1861 		return;
   1862 	}
   1863 #endif
   1864 	sigdelset(&p->p_sigctx.ps_siglist, signum);
   1865 	action = SIGACTION_PS(ps, signum).sa_handler;
   1866 	if (action == SIG_DFL) {
   1867 #ifdef KTRACE
   1868 		if (KTRPOINT(p, KTR_PSIG))
   1869 			ktrpsig(l, signum, action,
   1870 			    p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1871 			    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
   1872 			    NULL);
   1873 #endif
   1874 		/*
   1875 		 * Default action, where the default is to kill
   1876 		 * the process.  (Other cases were ignored above.)
   1877 		 */
   1878 		sigexit(l, signum);
   1879 		/* NOTREACHED */
   1880 	} else {
   1881 		ksiginfo_t *ksi;
   1882 		/*
   1883 		 * If we get here, the signal must be caught.
   1884 		 */
   1885 #ifdef DIAGNOSTIC
   1886 		if (action == SIG_IGN ||
   1887 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
   1888 			panic("postsig action");
   1889 #endif
   1890 		/*
   1891 		 * Set the new mask value and also defer further
   1892 		 * occurrences of this signal.
   1893 		 *
   1894 		 * Special case: user has done a sigpause.  Here the
   1895 		 * current mask is not of interest, but rather the
   1896 		 * mask from before the sigpause is what we want
   1897 		 * restored after the signal processing is completed.
   1898 		 */
   1899 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
   1900 			returnmask = &p->p_sigctx.ps_oldmask;
   1901 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
   1902 		} else
   1903 			returnmask = &p->p_sigctx.ps_sigmask;
   1904 		p->p_stats->p_ru.ru_nsignals++;
   1905 		ksi = ksiginfo_dequeue(p, signum);
   1906 #ifdef KTRACE
   1907 		if (KTRPOINT(p, KTR_PSIG))
   1908 			ktrpsig(l, signum, action,
   1909 			    p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1910 			    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
   1911 			    ksi);
   1912 #endif
   1913 		if (ksi == NULL) {
   1914 			ksiginfo_t ksi1;
   1915 			/*
   1916 			 * we did not save any siginfo for this, either
   1917 			 * because the signal was not caught, or because the
   1918 			 * user did not request SA_SIGINFO
   1919 			 */
   1920 			KSI_INIT_EMPTY(&ksi1);
   1921 			ksi1.ksi_signo = signum;
   1922 			kpsendsig(l, &ksi1, returnmask);
   1923 		} else {
   1924 			kpsendsig(l, ksi, returnmask);
   1925 			pool_put(&ksiginfo_pool, ksi);
   1926 		}
   1927 		p->p_sigctx.ps_lwp = 0;
   1928 		p->p_sigctx.ps_code = 0;
   1929 		p->p_sigctx.ps_signo = 0;
   1930 		(void) splsched();	/* XXXSMP */
   1931 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
   1932 		    &p->p_sigctx.ps_sigmask);
   1933 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
   1934 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1935 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1936 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
   1937 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
   1938 		}
   1939 		(void) spl0();		/* XXXSMP */
   1940 	}
   1941 
   1942 	KERNEL_PROC_UNLOCK(l);
   1943 }
   1944 
   1945 /*
   1946  * Kill the current process for stated reason.
   1947  */
   1948 void
   1949 killproc(struct proc *p, const char *why)
   1950 {
   1951 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1952 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1953 	psignal(p, SIGKILL);
   1954 }
   1955 
   1956 /*
   1957  * Force the current process to exit with the specified signal, dumping core
   1958  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1959  * allowing unrecoverable failures to terminate the process without changing
   1960  * signal state.  Mark the accounting record with the signal termination.
   1961  * If dumping core, save the signal number for the debugger.  Calls exit and
   1962  * does not return.
   1963  */
   1964 
   1965 #if defined(DEBUG)
   1966 int	kern_logsigexit = 1;	/* not static to make public for sysctl */
   1967 #else
   1968 int	kern_logsigexit = 0;	/* not static to make public for sysctl */
   1969 #endif
   1970 
   1971 static	const char logcoredump[] =
   1972 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
   1973 static	const char lognocoredump[] =
   1974 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
   1975 
   1976 /* Wrapper function for use in p_userret */
   1977 static void
   1978 lwp_coredump_hook(struct lwp *l, void *arg)
   1979 {
   1980 	int s;
   1981 
   1982 	/*
   1983 	 * Suspend ourselves, so that the kernel stack and therefore
   1984 	 * the userland registers saved in the trapframe are around
   1985 	 * for coredump() to write them out.
   1986 	 */
   1987 	KERNEL_PROC_LOCK(l);
   1988 	l->l_flag &= ~L_DETACHED;
   1989 	SCHED_LOCK(s);
   1990 	l->l_stat = LSSUSPENDED;
   1991 	l->l_proc->p_nrlwps--;
   1992 	/* XXX NJWLWP check if this makes sense here: */
   1993 	l->l_proc->p_stats->p_ru.ru_nvcsw++;
   1994 	mi_switch(l, NULL);
   1995 	SCHED_ASSERT_UNLOCKED();
   1996 	splx(s);
   1997 
   1998 	lwp_exit(l);
   1999 }
   2000 
   2001 void
   2002 sigexit(struct lwp *l, int signum)
   2003 {
   2004 	struct proc	*p;
   2005 #if 0
   2006 	struct lwp	*l2;
   2007 #endif
   2008 	int		exitsig;
   2009 #ifdef COREDUMP
   2010 	int		error;
   2011 #endif
   2012 
   2013 	p = l->l_proc;
   2014 
   2015 	/*
   2016 	 * Don't permit coredump() or exit1() multiple times
   2017 	 * in the same process.
   2018 	 */
   2019 	if (p->p_flag & P_WEXIT) {
   2020 		KERNEL_PROC_UNLOCK(l);
   2021 		(*p->p_userret)(l, p->p_userret_arg);
   2022 	}
   2023 	p->p_flag |= P_WEXIT;
   2024 	/* We don't want to switch away from exiting. */
   2025 	/* XXX multiprocessor: stop LWPs on other processors. */
   2026 #if 0
   2027 	if (p->p_flag & P_SA) {
   2028 		LIST_FOREACH(l2, &p->p_lwps, l_sibling)
   2029 		    l2->l_flag &= ~L_SA;
   2030 		p->p_flag &= ~P_SA;
   2031 	}
   2032 #endif
   2033 
   2034 	/* Make other LWPs stick around long enough to be dumped */
   2035 	p->p_userret = lwp_coredump_hook;
   2036 	p->p_userret_arg = NULL;
   2037 
   2038 	exitsig = signum;
   2039 	p->p_acflag |= AXSIG;
   2040 	if (sigprop[signum] & SA_CORE) {
   2041 		p->p_sigctx.ps_signo = signum;
   2042 #ifdef COREDUMP
   2043 		if ((error = coredump(l, NULL)) == 0)
   2044 			exitsig |= WCOREFLAG;
   2045 #endif
   2046 
   2047 		if (kern_logsigexit) {
   2048 			/* XXX What if we ever have really large UIDs? */
   2049 			int uid = l->l_cred ?
   2050 			    (int)kauth_cred_geteuid(l->l_cred) : -1;
   2051 
   2052 #ifdef COREDUMP
   2053 			if (error)
   2054 				log(LOG_INFO, lognocoredump, p->p_pid,
   2055 				    p->p_comm, uid, signum, error);
   2056 			else
   2057 #endif
   2058 				log(LOG_INFO, logcoredump, p->p_pid,
   2059 				    p->p_comm, uid, signum);
   2060 		}
   2061 
   2062 	}
   2063 
   2064 	exit1(l, W_EXITCODE(0, exitsig));
   2065 	/* NOTREACHED */
   2066 }
   2067 
   2068 #ifdef COREDUMP
   2069 struct coredump_iostate {
   2070 	struct lwp *io_lwp;
   2071 	struct vnode *io_vp;
   2072 	kauth_cred_t io_cred;
   2073 	off_t io_offset;
   2074 };
   2075 
   2076 int
   2077 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
   2078 {
   2079 	struct coredump_iostate *io = cookie;
   2080 	int error;
   2081 
   2082 	error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
   2083 	    io->io_offset, segflg,
   2084 	    IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
   2085 	    segflg == UIO_USERSPACE ? io->io_lwp : NULL);
   2086 	if (error) {
   2087 		printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
   2088 		    io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
   2089 		    segflg == UIO_USERSPACE ? "user" : "system",
   2090 		    len, data, (long long) io->io_offset, error);
   2091 		return (error);
   2092 	}
   2093 
   2094 	io->io_offset += len;
   2095 	return (0);
   2096 }
   2097 
   2098 /*
   2099  * Dump core, into a file named "progname.core" or "core" (depending on the
   2100  * value of shortcorename), unless the process was setuid/setgid.
   2101  */
   2102 int
   2103 coredump(struct lwp *l, const char *pattern)
   2104 {
   2105 	struct vnode		*vp;
   2106 	struct proc		*p;
   2107 	struct vmspace		*vm;
   2108 	kauth_cred_t		cred;
   2109 	struct nameidata	nd;
   2110 	struct vattr		vattr;
   2111 	struct mount		*mp;
   2112 	struct coredump_iostate	io;
   2113 	int			error, error1;
   2114 	char			*name = NULL;
   2115 
   2116 	p = l->l_proc;
   2117 	vm = p->p_vmspace;
   2118 	cred = l->l_cred;
   2119 
   2120 	/*
   2121 	 * Make sure the process has not set-id, to prevent data leaks,
   2122 	 * unless it was specifically requested to allow set-id coredumps.
   2123 	 */
   2124 	if ((p->p_flag & P_SUGID) && !security_setidcore_dump)
   2125 		return EPERM;
   2126 
   2127 	/*
   2128 	 * Refuse to core if the data + stack + user size is larger than
   2129 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
   2130 	 * data.
   2131 	 */
   2132 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   2133 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   2134 		return EFBIG;		/* better error code? */
   2135 
   2136 restart:
   2137 	/*
   2138 	 * The core dump will go in the current working directory.  Make
   2139 	 * sure that the directory is still there and that the mount flags
   2140 	 * allow us to write core dumps there.
   2141 	 */
   2142 	vp = p->p_cwdi->cwdi_cdir;
   2143 	if (vp->v_mount == NULL ||
   2144 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) {
   2145 		error = EPERM;
   2146 		goto done;
   2147 	}
   2148 
   2149 	if ((p->p_flag & P_SUGID) && security_setidcore_dump)
   2150 		pattern = security_setidcore_path;
   2151 
   2152 	if (pattern == NULL)
   2153 		pattern = p->p_limit->pl_corename;
   2154 	if (name == NULL) {
   2155 		name = PNBUF_GET();
   2156 	}
   2157 	if ((error = build_corename(p, name, pattern, MAXPATHLEN)) != 0)
   2158 		goto done;
   2159 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, l);
   2160 	if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
   2161 	    S_IRUSR | S_IWUSR)) != 0)
   2162 		goto done;
   2163 	vp = nd.ni_vp;
   2164 
   2165 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
   2166 		VOP_UNLOCK(vp, 0);
   2167 		if ((error = vn_close(vp, FWRITE, cred, l)) != 0)
   2168 			goto done;
   2169 		if ((error = vn_start_write(NULL, &mp,
   2170 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   2171 			goto done;
   2172 		goto restart;
   2173 	}
   2174 
   2175 	/* Don't dump to non-regular files or files with links. */
   2176 	if (vp->v_type != VREG ||
   2177 	    VOP_GETATTR(vp, &vattr, cred, l) || vattr.va_nlink != 1) {
   2178 		error = EINVAL;
   2179 		goto out;
   2180 	}
   2181 	VATTR_NULL(&vattr);
   2182 	vattr.va_size = 0;
   2183 
   2184 	if ((p->p_flag & P_SUGID) && security_setidcore_dump) {
   2185 		vattr.va_uid = security_setidcore_owner;
   2186 		vattr.va_gid = security_setidcore_group;
   2187 		vattr.va_mode = security_setidcore_mode;
   2188 	}
   2189 
   2190 	VOP_LEASE(vp, l, cred, LEASE_WRITE);
   2191 	VOP_SETATTR(vp, &vattr, cred, l);
   2192 	p->p_acflag |= ACORE;
   2193 
   2194 	io.io_lwp = l;
   2195 	io.io_vp = vp;
   2196 	io.io_cred = cred;
   2197 	io.io_offset = 0;
   2198 
   2199 	/* Now dump the actual core file. */
   2200 	error = (*p->p_execsw->es_coredump)(l, &io);
   2201  out:
   2202 	VOP_UNLOCK(vp, 0);
   2203 	vn_finished_write(mp, 0);
   2204 	error1 = vn_close(vp, FWRITE, cred, l);
   2205 	if (error == 0)
   2206 		error = error1;
   2207 done:
   2208 	if (name != NULL)
   2209 		PNBUF_PUT(name);
   2210 	return error;
   2211 }
   2212 #endif /* COREDUMP */
   2213 
   2214 /*
   2215  * Nonexistent system call-- signal process (may want to handle it).
   2216  * Flag error in case process won't see signal immediately (blocked or ignored).
   2217  */
   2218 #ifndef PTRACE
   2219 __weak_alias(sys_ptrace, sys_nosys);
   2220 #endif
   2221 
   2222 /* ARGSUSED */
   2223 int
   2224 sys_nosys(struct lwp *l, void *v, register_t *retval)
   2225 {
   2226 	struct proc 	*p;
   2227 
   2228 	p = l->l_proc;
   2229 	psignal(p, SIGSYS);
   2230 	return (ENOSYS);
   2231 }
   2232 
   2233 #ifdef COREDUMP
   2234 static int
   2235 build_corename(struct proc *p, char *dst, const char *src, size_t len)
   2236 {
   2237 	const char	*s;
   2238 	char		*d, *end;
   2239 	int		i;
   2240 
   2241 	for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
   2242 		if (*s == '%') {
   2243 			switch (*(s + 1)) {
   2244 			case 'n':
   2245 				i = snprintf(d, end - d, "%s", p->p_comm);
   2246 				break;
   2247 			case 'p':
   2248 				i = snprintf(d, end - d, "%d", p->p_pid);
   2249 				break;
   2250 			case 'u':
   2251 				i = snprintf(d, end - d, "%.*s",
   2252 				    (int)sizeof p->p_pgrp->pg_session->s_login,
   2253 				    p->p_pgrp->pg_session->s_login);
   2254 				break;
   2255 			case 't':
   2256 				i = snprintf(d, end - d, "%ld",
   2257 				    p->p_stats->p_start.tv_sec);
   2258 				break;
   2259 			default:
   2260 				goto copy;
   2261 			}
   2262 			d += i;
   2263 			s++;
   2264 		} else {
   2265  copy:			*d = *s;
   2266 			d++;
   2267 		}
   2268 		if (d >= end)
   2269 			return (ENAMETOOLONG);
   2270 	}
   2271 	*d = '\0';
   2272 	return 0;
   2273 }
   2274 #endif /* COREDUMP */
   2275 
   2276 void
   2277 getucontext(struct lwp *l, ucontext_t *ucp)
   2278 {
   2279 	struct proc	*p;
   2280 
   2281 	p = l->l_proc;
   2282 
   2283 	ucp->uc_flags = 0;
   2284 	ucp->uc_link = l->l_ctxlink;
   2285 
   2286 	(void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
   2287 	ucp->uc_flags |= _UC_SIGMASK;
   2288 
   2289 	/*
   2290 	 * The (unsupplied) definition of the `current execution stack'
   2291 	 * in the System V Interface Definition appears to allow returning
   2292 	 * the main context stack.
   2293 	 */
   2294 	if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
   2295 		ucp->uc_stack.ss_sp = (void *)USRSTACK;
   2296 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
   2297 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
   2298 	} else {
   2299 		/* Simply copy alternate signal execution stack. */
   2300 		ucp->uc_stack = p->p_sigctx.ps_sigstk;
   2301 	}
   2302 	ucp->uc_flags |= _UC_STACK;
   2303 
   2304 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
   2305 }
   2306 
   2307 /* ARGSUSED */
   2308 int
   2309 sys_getcontext(struct lwp *l, void *v, register_t *retval)
   2310 {
   2311 	struct sys_getcontext_args /* {
   2312 		syscallarg(struct __ucontext *) ucp;
   2313 	} */ *uap = v;
   2314 	ucontext_t uc;
   2315 
   2316 	getucontext(l, &uc);
   2317 
   2318 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
   2319 }
   2320 
   2321 int
   2322 setucontext(struct lwp *l, const ucontext_t *ucp)
   2323 {
   2324 	struct proc	*p;
   2325 	int		error;
   2326 
   2327 	p = l->l_proc;
   2328 	if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
   2329 		return (error);
   2330 	l->l_ctxlink = ucp->uc_link;
   2331 
   2332 	if ((ucp->uc_flags & _UC_SIGMASK) != 0)
   2333 		sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
   2334 
   2335 	/*
   2336 	 * If there was stack information, update whether or not we are
   2337 	 * still running on an alternate signal stack.
   2338 	 */
   2339 	if ((ucp->uc_flags & _UC_STACK) != 0) {
   2340 		if (ucp->uc_stack.ss_flags & SS_ONSTACK)
   2341 			p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
   2342 		else
   2343 			p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
   2344 	}
   2345 
   2346 	return 0;
   2347 }
   2348 
   2349 /* ARGSUSED */
   2350 int
   2351 sys_setcontext(struct lwp *l, void *v, register_t *retval)
   2352 {
   2353 	struct sys_setcontext_args /* {
   2354 		syscallarg(const ucontext_t *) ucp;
   2355 	} */ *uap = v;
   2356 	ucontext_t uc;
   2357 	int error;
   2358 
   2359 	if (SCARG(uap, ucp) == NULL)	/* i.e. end of uc_link chain */
   2360 		exit1(l, W_EXITCODE(0, 0));
   2361 	else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
   2362 	    (error = setucontext(l, &uc)) != 0)
   2363 		return (error);
   2364 
   2365 	return (EJUSTRETURN);
   2366 }
   2367 
   2368 /*
   2369  * sigtimedwait(2) system call, used also for implementation
   2370  * of sigwaitinfo() and sigwait().
   2371  *
   2372  * This only handles single LWP in signal wait. libpthread provides
   2373  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
   2374  */
   2375 int
   2376 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
   2377 {
   2378 	return __sigtimedwait1(l, v, retval, copyout, copyin, copyout);
   2379 }
   2380 
   2381 int
   2382 __sigtimedwait1(struct lwp *l, void *v, register_t *retval,
   2383     copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
   2384 {
   2385 	struct sys___sigtimedwait_args /* {
   2386 		syscallarg(const sigset_t *) set;
   2387 		syscallarg(siginfo_t *) info;
   2388 		syscallarg(struct timespec *) timeout;
   2389 	} */ *uap = v;
   2390 	sigset_t *waitset, twaitset;
   2391 	struct proc *p = l->l_proc;
   2392 	int error, signum;
   2393 	int timo = 0;
   2394 	struct timespec ts, tsstart;
   2395 	ksiginfo_t *ksi;
   2396 
   2397 	memset(&tsstart, 0, sizeof tsstart);	 /* XXX gcc */
   2398 
   2399 	MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
   2400 
   2401 	if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
   2402 		FREE(waitset, M_TEMP);
   2403 		return (error);
   2404 	}
   2405 
   2406 	/*
   2407 	 * Silently ignore SA_CANTMASK signals. psignal() would
   2408 	 * ignore SA_CANTMASK signals in waitset, we do this
   2409 	 * only for the below siglist check.
   2410 	 */
   2411 	sigminusset(&sigcantmask, waitset);
   2412 
   2413 	/*
   2414 	 * First scan siglist and check if there is signal from
   2415 	 * our waitset already pending.
   2416 	 */
   2417 	twaitset = *waitset;
   2418 	__sigandset(&p->p_sigctx.ps_siglist, &twaitset);
   2419 	if ((signum = firstsig(&twaitset))) {
   2420 		/* found pending signal */
   2421 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   2422 		ksi = ksiginfo_dequeue(p, signum);
   2423 		if (!ksi) {
   2424 			/* No queued siginfo, manufacture one */
   2425 			ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
   2426 			KSI_INIT(ksi);
   2427 			ksi->ksi_info._signo = signum;
   2428 			ksi->ksi_info._code = SI_USER;
   2429 		}
   2430 
   2431 		goto sig;
   2432 	}
   2433 
   2434 	/*
   2435 	 * Calculate timeout, if it was specified.
   2436 	 */
   2437 	if (SCARG(uap, timeout)) {
   2438 		uint64_t ms;
   2439 
   2440 		if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts))))
   2441 			return (error);
   2442 
   2443 		ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
   2444 		timo = mstohz(ms);
   2445 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
   2446 			timo = 1;
   2447 		if (timo <= 0)
   2448 			return (EAGAIN);
   2449 
   2450 		/*
   2451 		 * Remember current uptime, it would be used in
   2452 		 * ECANCELED/ERESTART case.
   2453 		 */
   2454 		getnanouptime(&tsstart);
   2455 	}
   2456 
   2457 	/*
   2458 	 * Setup ps_sigwait list. Pass pointer to malloced memory
   2459 	 * here; it's not possible to pass pointer to a structure
   2460 	 * on current process's stack, the current process might
   2461 	 * be swapped out at the time the signal would get delivered.
   2462 	 */
   2463 	ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
   2464 	p->p_sigctx.ps_sigwaited = ksi;
   2465 	p->p_sigctx.ps_sigwait = waitset;
   2466 
   2467 	/*
   2468 	 * Wait for signal to arrive. We can either be woken up or
   2469 	 * time out.
   2470 	 */
   2471 	error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
   2472 
   2473 	/*
   2474 	 * Need to find out if we woke as a result of lwp_wakeup()
   2475 	 * or a signal outside our wait set.
   2476 	 */
   2477 	if (error == EINTR && p->p_sigctx.ps_sigwaited
   2478 	    && !firstsig(&p->p_sigctx.ps_siglist)) {
   2479 		/* wakeup via _lwp_wakeup() */
   2480 		error = ECANCELED;
   2481 	} else if (!error && p->p_sigctx.ps_sigwaited) {
   2482 		/* spurious wakeup - arrange for syscall restart */
   2483 		error = ERESTART;
   2484 		goto fail;
   2485 	}
   2486 
   2487 	/*
   2488 	 * On error, clear sigwait indication. psignal() clears it
   2489 	 * in !error case.
   2490 	 */
   2491 	if (error) {
   2492 		p->p_sigctx.ps_sigwaited = NULL;
   2493 
   2494 		/*
   2495 		 * If the sleep was interrupted (either by signal or wakeup),
   2496 		 * update the timeout and copyout new value back.
   2497 		 * It would be used when the syscall would be restarted
   2498 		 * or called again.
   2499 		 */
   2500 		if (timo && (error == ERESTART || error == ECANCELED)) {
   2501 			struct timespec tsnow;
   2502 			int err;
   2503 
   2504 /* XXX double check the following change */
   2505 			getnanouptime(&tsnow);
   2506 
   2507 			/* compute how much time has passed since start */
   2508 			timespecsub(&tsnow, &tsstart, &tsnow);
   2509 			/* substract passed time from timeout */
   2510 			timespecsub(&ts, &tsnow, &ts);
   2511 
   2512 			if (ts.tv_sec < 0) {
   2513 				error = EAGAIN;
   2514 				goto fail;
   2515 			}
   2516 /* XXX double check the previous change */
   2517 
   2518 			/* copy updated timeout to userland */
   2519 			if ((err = (*put_timeout)(&ts, SCARG(uap, timeout),
   2520 			    sizeof(ts)))) {
   2521 				error = err;
   2522 				goto fail;
   2523 			}
   2524 		}
   2525 
   2526 		goto fail;
   2527 	}
   2528 
   2529 	/*
   2530 	 * If a signal from the wait set arrived, copy it to userland.
   2531 	 * Copy only the used part of siginfo, the padding part is
   2532 	 * left unchanged (userland is not supposed to touch it anyway).
   2533 	 */
   2534  sig:
   2535 	return (*put_info)(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info));
   2536 
   2537  fail:
   2538 	FREE(waitset, M_TEMP);
   2539 	pool_put(&ksiginfo_pool, ksi);
   2540 	p->p_sigctx.ps_sigwait = NULL;
   2541 
   2542 	return (error);
   2543 }
   2544 
   2545 /*
   2546  * Returns true if signal is ignored or masked for passed process.
   2547  */
   2548 int
   2549 sigismasked(struct proc *p, int sig)
   2550 {
   2551 
   2552 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
   2553 	    sigismember(&p->p_sigctx.ps_sigmask, sig));
   2554 }
   2555 
   2556 static int
   2557 filt_sigattach(struct knote *kn)
   2558 {
   2559 	struct proc *p = curproc;
   2560 
   2561 	kn->kn_ptr.p_proc = p;
   2562 	kn->kn_flags |= EV_CLEAR;               /* automatically set */
   2563 
   2564 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
   2565 
   2566 	return (0);
   2567 }
   2568 
   2569 static void
   2570 filt_sigdetach(struct knote *kn)
   2571 {
   2572 	struct proc *p = kn->kn_ptr.p_proc;
   2573 
   2574 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
   2575 }
   2576 
   2577 /*
   2578  * signal knotes are shared with proc knotes, so we apply a mask to
   2579  * the hint in order to differentiate them from process hints.  This
   2580  * could be avoided by using a signal-specific knote list, but probably
   2581  * isn't worth the trouble.
   2582  */
   2583 static int
   2584 filt_signal(struct knote *kn, long hint)
   2585 {
   2586 
   2587 	if (hint & NOTE_SIGNAL) {
   2588 		hint &= ~NOTE_SIGNAL;
   2589 
   2590 		if (kn->kn_id == hint)
   2591 			kn->kn_data++;
   2592 	}
   2593 	return (kn->kn_data != 0);
   2594 }
   2595 
   2596 const struct filterops sig_filtops = {
   2597 	0, filt_sigattach, filt_sigdetach, filt_signal
   2598 };
   2599