Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.233
      1 /*	$NetBSD: kern_sig.c,v 1.233 2006/11/01 09:33:45 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.233 2006/11/01 09:33:45 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 *, int);
     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 __unused, 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 __unused, 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 POOL_INIT(siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
    127     &pool_allocator_nointr);
    128 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 __unused)
    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 __unused)
    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 __unused)
    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 __unused)
    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 __unused)
    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 __unused)
    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 __unused)
    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 __unused)
    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, 1);
    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, 1);
    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, 1);
    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, 1);
    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, int dolock)
    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, dolock);
    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, 1);
   1004 }
   1005 
   1006 void
   1007 kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data, int dolock)
   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, dolock);
   1025 }
   1026 
   1027 static void
   1028 kpsignal2(struct proc *p, const ksiginfo_t *ksi, int dolock)
   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 	/* XXXSMP: works, but icky */
   1042 	if (dolock) {
   1043 		SCHED_ASSERT_UNLOCKED();
   1044 	} else {
   1045 		SCHED_ASSERT_LOCKED();
   1046 	}
   1047 #endif
   1048 
   1049 	/*
   1050 	 * Notify any interested parties in the signal.
   1051 	 */
   1052 	KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
   1053 
   1054 	prop = sigprop[signum];
   1055 
   1056 	/*
   1057 	 * If proc is traced, always give parent a chance.
   1058 	 */
   1059 	if (p->p_flag & P_TRACED) {
   1060 		action = SIG_DFL;
   1061 
   1062 		/*
   1063 		 * If the process is being traced and the signal is being
   1064 		 * caught, make sure to save any ksiginfo.
   1065 		 */
   1066 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
   1067 			SCHED_ASSERT_UNLOCKED();
   1068 			ksiginfo_queue(p, ksi, NULL);
   1069 		}
   1070 	} else {
   1071 		/*
   1072 		 * If the signal was the result of a trap, reset it
   1073 		 * to default action if it's currently masked, so that it would
   1074 		 * coredump immediatelly instead of spinning repeatedly
   1075 		 * taking the signal.
   1076 		 */
   1077 		if (KSI_TRAP_P(ksi)
   1078 		    && sigismember(&p->p_sigctx.ps_sigmask, signum)
   1079 		    && !sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
   1080 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
   1081 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1082 			sigdelset(&p->p_sigctx.ps_sigmask, signum);
   1083 			SIGACTION(p, signum).sa_handler = SIG_DFL;
   1084 		}
   1085 
   1086 		/*
   1087 		 * If the signal is being ignored,
   1088 		 * then we forget about it immediately.
   1089 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
   1090 		 * and if it is set to SIG_IGN,
   1091 		 * action will be SIG_DFL here.)
   1092 		 */
   1093 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
   1094 			return;
   1095 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1096 			action = SIG_HOLD;
   1097 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
   1098 			action = SIG_CATCH;
   1099 		else {
   1100 			action = SIG_DFL;
   1101 
   1102 			if (prop & SA_KILL && p->p_nice > NZERO)
   1103 				p->p_nice = NZERO;
   1104 
   1105 			/*
   1106 			 * If sending a tty stop signal to a member of an
   1107 			 * orphaned process group, discard the signal here if
   1108 			 * the action is default; don't stop the process below
   1109 			 * if sleeping, and don't clear any pending SIGCONT.
   1110 			 */
   1111 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
   1112 				return;
   1113 		}
   1114 	}
   1115 
   1116 	if (prop & SA_CONT)
   1117 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
   1118 
   1119 	if (prop & SA_STOP)
   1120 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
   1121 
   1122 	/*
   1123 	 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
   1124 	 * please!), check if anything waits on it. If yes, save the
   1125 	 * info into provided ps_sigwaited, and wake-up the waiter.
   1126 	 * The signal won't be processed further here.
   1127 	 */
   1128 	if ((prop & SA_CANTMASK) == 0
   1129 	    && p->p_sigctx.ps_sigwaited
   1130 	    && sigismember(p->p_sigctx.ps_sigwait, signum)
   1131 	    && p->p_stat != SSTOP) {
   1132 		p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info;
   1133 		p->p_sigctx.ps_sigwaited = NULL;
   1134 		if (dolock)
   1135 			wakeup_one(&p->p_sigctx.ps_sigwait);
   1136 		else
   1137 			sched_wakeup(&p->p_sigctx.ps_sigwait);
   1138 		return;
   1139 	}
   1140 
   1141 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1142 
   1143 	/* CHECKSIGS() is "inlined" here. */
   1144 	p->p_sigctx.ps_sigcheck = 1;
   1145 
   1146 	/*
   1147 	 * Defer further processing for signals which are held,
   1148 	 * except that stopped processes must be continued by SIGCONT.
   1149 	 */
   1150 	if (action == SIG_HOLD &&
   1151 	    ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) {
   1152 		SCHED_ASSERT_UNLOCKED();
   1153 		ksiginfo_queue(p, ksi, NULL);
   1154 		return;
   1155 	}
   1156 
   1157 	/*
   1158 	 * Allocate a ksiginfo_t incase we need to insert it with the
   1159 	 * scheduler lock held, but only if this ksiginfo_t isn't empty.
   1160 	 */
   1161 	if (dolock && !KSI_EMPTY_P(ksi)) {
   1162 		newkp = pool_get(&ksiginfo_pool, PR_NOWAIT);
   1163 		if (newkp == NULL) {
   1164 #ifdef DIAGNOSTIC
   1165 			printf("kpsignal2: couldn't allocated from ksiginfo_pool\n");
   1166 #endif
   1167 			return;
   1168 		}
   1169 	} else
   1170 		newkp = NULL;
   1171 
   1172 	/* XXXSMP: works, but icky */
   1173 	if (dolock)
   1174 		SCHED_LOCK(s);
   1175 
   1176 	if (p->p_flag & P_SA) {
   1177 		allsusp = 0;
   1178 		l = NULL;
   1179 		if (p->p_stat == SACTIVE) {
   1180 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1181 				l = vp->savp_lwp;
   1182 				KDASSERT(l != NULL);
   1183 				if (l->l_flag & L_SA_IDLE) {
   1184 					/* wakeup idle LWP */
   1185 					goto found;
   1186 					/*NOTREACHED*/
   1187 				} else if (l->l_flag & L_SA_YIELD) {
   1188 					/* idle LWP is already waking up */
   1189 					goto out;
   1190 					/*NOTREACHED*/
   1191 				}
   1192 			}
   1193 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1194 				l = vp->savp_lwp;
   1195 				if (l->l_stat == LSRUN ||
   1196 				    l->l_stat == LSONPROC) {
   1197 					signotify(p);
   1198 					goto out;
   1199 					/*NOTREACHED*/
   1200 				}
   1201 				if (l->l_stat == LSSLEEP &&
   1202 				    l->l_flag & L_SINTR) {
   1203 					/* ok to signal vp lwp */
   1204 					break;
   1205 				} else
   1206 					l = NULL;
   1207 			}
   1208 		} else if (p->p_stat == SSTOP) {
   1209 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1210 				l = vp->savp_lwp;
   1211 				if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0)
   1212 					break;
   1213 				l = NULL;
   1214 			}
   1215 		}
   1216 	} else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) {
   1217 		/*
   1218 		 * At least one LWP is running or on a run queue.
   1219 		 * The signal will be noticed when one of them returns
   1220 		 * to userspace.
   1221 		 */
   1222 		signotify(p);
   1223 		/*
   1224 		 * The signal will be noticed very soon.
   1225 		 */
   1226 		goto out;
   1227 		/*NOTREACHED*/
   1228 	} else {
   1229 		/*
   1230 		 * Find out if any of the sleeps are interruptable,
   1231 		 * and if all the live LWPs remaining are suspended.
   1232 		 */
   1233 		allsusp = 1;
   1234 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1235 			if (l->l_stat == LSSLEEP &&
   1236 			    l->l_flag & L_SINTR)
   1237 				break;
   1238 			if (l->l_stat == LSSUSPENDED)
   1239 				suspended = l;
   1240 			else if ((l->l_stat != LSZOMB) &&
   1241 			    (l->l_stat != LSDEAD))
   1242 				allsusp = 0;
   1243 		}
   1244 	}
   1245 
   1246  found:
   1247 	switch (p->p_stat) {
   1248 	case SACTIVE:
   1249 
   1250 		if (l != NULL && (p->p_flag & P_TRACED))
   1251 			goto run;
   1252 
   1253 		/*
   1254 		 * If SIGCONT is default (or ignored) and process is
   1255 		 * asleep, we are finished; the process should not
   1256 		 * be awakened.
   1257 		 */
   1258 		if ((prop & SA_CONT) && action == SIG_DFL) {
   1259 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1260 			goto done;
   1261 		}
   1262 
   1263 		/*
   1264 		 * When a sleeping process receives a stop
   1265 		 * signal, process immediately if possible.
   1266 		 */
   1267 		if ((prop & SA_STOP) && action == SIG_DFL) {
   1268 			/*
   1269 			 * If a child holding parent blocked,
   1270 			 * stopping could cause deadlock.
   1271 			 */
   1272 			if (p->p_flag & P_PPWAIT) {
   1273 				goto out;
   1274 			}
   1275 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1276 			p->p_xstat = signum;
   1277 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
   1278 				/*
   1279 				 * XXXSMP: recursive call; don't lock
   1280 				 * the second time around.
   1281 				 */
   1282 				child_psignal(p, 0);
   1283 			}
   1284 			proc_stop(p, 1);	/* XXXSMP: recurse? */
   1285 			goto done;
   1286 		}
   1287 
   1288 		if (l == NULL) {
   1289 			/*
   1290 			 * Special case: SIGKILL of a process
   1291 			 * which is entirely composed of
   1292 			 * suspended LWPs should succeed. We
   1293 			 * make this happen by unsuspending one of
   1294 			 * them.
   1295 			 */
   1296 			if (allsusp && (signum == SIGKILL)) {
   1297 				lwp_continue(suspended);
   1298 			}
   1299 			goto done;
   1300 		}
   1301 		/*
   1302 		 * All other (caught or default) signals
   1303 		 * cause the process to run.
   1304 		 */
   1305 		goto runfast;
   1306 		/*NOTREACHED*/
   1307 	case SSTOP:
   1308 		/* Process is stopped */
   1309 		/*
   1310 		 * If traced process is already stopped,
   1311 		 * then no further action is necessary.
   1312 		 */
   1313 		if (p->p_flag & P_TRACED)
   1314 			goto done;
   1315 
   1316 		/*
   1317 		 * Kill signal always sets processes running,
   1318 		 * if possible.
   1319 		 */
   1320 		if (signum == SIGKILL) {
   1321 			l = proc_unstop(p);
   1322 			if (l)
   1323 				goto runfast;
   1324 			goto done;
   1325 		}
   1326 
   1327 		if (prop & SA_CONT) {
   1328 			/*
   1329 			 * If SIGCONT is default (or ignored),
   1330 			 * we continue the process but don't
   1331 			 * leave the signal in ps_siglist, as
   1332 			 * it has no further action.  If
   1333 			 * SIGCONT is held, we continue the
   1334 			 * process and leave the signal in
   1335 			 * ps_siglist.  If the process catches
   1336 			 * SIGCONT, let it handle the signal
   1337 			 * itself.  If it isn't waiting on an
   1338 			 * event, then it goes back to run
   1339 			 * state.  Otherwise, process goes
   1340 			 * back to sleep state.
   1341 			 */
   1342 			if (action == SIG_DFL)
   1343 				sigdelset(&p->p_sigctx.ps_siglist,
   1344 				    signum);
   1345 			l = proc_unstop(p);
   1346 			if (l && (action == SIG_CATCH))
   1347 				goto runfast;
   1348 			goto out;
   1349 		}
   1350 
   1351 		if (prop & SA_STOP) {
   1352 			/*
   1353 			 * Already stopped, don't need to stop again.
   1354 			 * (If we did the shell could get confused.)
   1355 			 */
   1356 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1357 			goto done;
   1358 		}
   1359 
   1360 		/*
   1361 		 * If a lwp is sleeping interruptibly, then
   1362 		 * wake it up; it will run until the kernel
   1363 		 * boundary, where it will stop in issignal(),
   1364 		 * since p->p_stat is still SSTOP. When the
   1365 		 * process is continued, it will be made
   1366 		 * runnable and can look at the signal.
   1367 		 */
   1368 		if (l)
   1369 			goto run;
   1370 		goto out;
   1371 	case SIDL:
   1372 		/* Process is being created by fork */
   1373 		/* XXX: We are not ready to receive signals yet */
   1374 		goto done;
   1375 	default:
   1376 		/* Else what? */
   1377 		panic("psignal: Invalid process state %d.", p->p_stat);
   1378 	}
   1379 	/*NOTREACHED*/
   1380 
   1381  runfast:
   1382 	if (action == SIG_CATCH) {
   1383 		ksiginfo_queue(p, ksi, &newkp);
   1384 		action = SIG_HOLD;
   1385 	}
   1386 	/*
   1387 	 * Raise priority to at least PUSER.
   1388 	 */
   1389 	if (l->l_priority > PUSER)
   1390 		l->l_priority = PUSER;
   1391  run:
   1392 	if (action == SIG_CATCH) {
   1393 		ksiginfo_queue(p, ksi, &newkp);
   1394 		action = SIG_HOLD;
   1395 	}
   1396 
   1397 	setrunnable(l);		/* XXXSMP: recurse? */
   1398  out:
   1399 	if (action == SIG_CATCH)
   1400 		ksiginfo_queue(p, ksi, &newkp);
   1401  done:
   1402 	/* XXXSMP: works, but icky */
   1403 	if (dolock)
   1404 		SCHED_UNLOCK(s);
   1405 
   1406 	if (newkp)
   1407 		pool_put(&ksiginfo_pool, newkp);
   1408 }
   1409 
   1410 siginfo_t *
   1411 siginfo_alloc(int flags)
   1412 {
   1413 
   1414 	return pool_get(&siginfo_pool, flags);
   1415 }
   1416 
   1417 void
   1418 siginfo_free(void *arg)
   1419 {
   1420 
   1421 	pool_put(&siginfo_pool, arg);
   1422 }
   1423 
   1424 void
   1425 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
   1426 {
   1427 	struct proc *p = l->l_proc;
   1428 	struct lwp *le, *li;
   1429 	siginfo_t *si;
   1430 	int f;
   1431 
   1432 	if (p->p_flag & P_SA) {
   1433 
   1434 		/* XXXUPSXXX What if not on sa_vp ? */
   1435 
   1436 		f = l->l_flag & L_SA;
   1437 		l->l_flag &= ~L_SA;
   1438 		si = siginfo_alloc(PR_WAITOK);
   1439 		si->_info = ksi->ksi_info;
   1440 		le = li = NULL;
   1441 		if (KSI_TRAP_P(ksi))
   1442 			le = l;
   1443 		else
   1444 			li = l;
   1445 		if (sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
   1446 		    sizeof(*si), si, siginfo_free) != 0) {
   1447 			siginfo_free(si);
   1448 #if 0
   1449 			if (KSI_TRAP_P(ksi))
   1450 				/* XXX What do we do here?? */;
   1451 #endif
   1452 		}
   1453 		l->l_flag |= f;
   1454 		return;
   1455 	}
   1456 
   1457 	(*p->p_emul->e_sendsig)(ksi, mask);
   1458 }
   1459 
   1460 static inline int firstsig(const sigset_t *);
   1461 
   1462 static inline int
   1463 firstsig(const sigset_t *ss)
   1464 {
   1465 	int sig;
   1466 
   1467 	sig = ffs(ss->__bits[0]);
   1468 	if (sig != 0)
   1469 		return (sig);
   1470 #if NSIG > 33
   1471 	sig = ffs(ss->__bits[1]);
   1472 	if (sig != 0)
   1473 		return (sig + 32);
   1474 #endif
   1475 #if NSIG > 65
   1476 	sig = ffs(ss->__bits[2]);
   1477 	if (sig != 0)
   1478 		return (sig + 64);
   1479 #endif
   1480 #if NSIG > 97
   1481 	sig = ffs(ss->__bits[3]);
   1482 	if (sig != 0)
   1483 		return (sig + 96);
   1484 #endif
   1485 	return (0);
   1486 }
   1487 
   1488 /*
   1489  * If the current process has received a signal (should be caught or cause
   1490  * termination, should interrupt current syscall), return the signal number.
   1491  * Stop signals with default action are processed immediately, then cleared;
   1492  * they aren't returned.  This is checked after each entry to the system for
   1493  * a syscall or trap (though this can usually be done without calling issignal
   1494  * by checking the pending signal masks in the CURSIG macro.) The normal call
   1495  * sequence is
   1496  *
   1497  *	while (signum = CURSIG(curlwp))
   1498  *		postsig(signum);
   1499  */
   1500 int
   1501 issignal(struct lwp *l)
   1502 {
   1503 	struct proc	*p = l->l_proc;
   1504 	int		s = 0, signum, prop;
   1505 	int		dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
   1506 	sigset_t	ss;
   1507 
   1508 	/* Bail out if we do not own the virtual processor */
   1509 	if (l->l_flag & L_SA && l->l_savp->savp_lwp != l)
   1510 		return 0;
   1511 
   1512 	if (p->p_stat == SSTOP) {
   1513 		/*
   1514 		 * The process is stopped/stopping. Stop ourselves now that
   1515 		 * we're on the kernel/userspace boundary.
   1516 		 */
   1517 		if (dolock)
   1518 			SCHED_LOCK(s);
   1519 		l->l_stat = LSSTOP;
   1520 		p->p_nrlwps--;
   1521 		if (p->p_flag & P_TRACED)
   1522 			goto sigtraceswitch;
   1523 		else
   1524 			goto sigswitch;
   1525 	}
   1526 	for (;;) {
   1527 		sigpending1(p, &ss);
   1528 		if (p->p_flag & P_PPWAIT)
   1529 			sigminusset(&stopsigmask, &ss);
   1530 		signum = firstsig(&ss);
   1531 		if (signum == 0) {		 	/* no signal to send */
   1532 			p->p_sigctx.ps_sigcheck = 0;
   1533 			if (locked && dolock)
   1534 				SCHED_LOCK(s);
   1535 			return (0);
   1536 		}
   1537 							/* take the signal! */
   1538 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   1539 
   1540 		/*
   1541 		 * We should see pending but ignored signals
   1542 		 * only if P_TRACED was on when they were posted.
   1543 		 */
   1544 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
   1545 		    (p->p_flag & P_TRACED) == 0)
   1546 			continue;
   1547 
   1548 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1549 			/*
   1550 			 * If traced, always stop, and stay
   1551 			 * stopped until released by the debugger.
   1552 			 */
   1553 			p->p_xstat = signum;
   1554 
   1555 			/* Emulation-specific handling of signal trace */
   1556 			if ((p->p_emul->e_tracesig != NULL) &&
   1557 			    ((*p->p_emul->e_tracesig)(p, signum) != 0))
   1558 				goto childresumed;
   1559 
   1560 			if ((p->p_flag & P_FSTRACE) == 0)
   1561 				child_psignal(p, dolock);
   1562 			if (dolock)
   1563 				SCHED_LOCK(s);
   1564 			proc_stop(p, 1);
   1565 		sigtraceswitch:
   1566 			mi_switch(l, NULL);
   1567 			SCHED_ASSERT_UNLOCKED();
   1568 			if (dolock)
   1569 				splx(s);
   1570 			else
   1571 				dolock = 1;
   1572 
   1573 		childresumed:
   1574 			/*
   1575 			 * If we are no longer being traced, or the parent
   1576 			 * didn't give us a signal, look for more signals.
   1577 			 */
   1578 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1579 				continue;
   1580 
   1581 			/*
   1582 			 * If the new signal is being masked, look for other
   1583 			 * signals.
   1584 			 */
   1585 			signum = p->p_xstat;
   1586 			p->p_xstat = 0;
   1587 			/*
   1588 			 * `p->p_sigctx.ps_siglist |= mask' is done
   1589 			 * in setrunnable().
   1590 			 */
   1591 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1592 				continue;
   1593 							/* take the signal! */
   1594 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1595 		}
   1596 
   1597 		prop = sigprop[signum];
   1598 
   1599 		/*
   1600 		 * Decide whether the signal should be returned.
   1601 		 * Return the signal's number, or fall through
   1602 		 * to clear it from the pending mask.
   1603 		 */
   1604 		switch ((long)SIGACTION(p, signum).sa_handler) {
   1605 
   1606 		case (long)SIG_DFL:
   1607 			/*
   1608 			 * Don't take default actions on system processes.
   1609 			 */
   1610 			if (p->p_pid <= 1) {
   1611 #ifdef DIAGNOSTIC
   1612 				/*
   1613 				 * Are you sure you want to ignore SIGSEGV
   1614 				 * in init? XXX
   1615 				 */
   1616 				printf("Process (pid %d) got signal %d\n",
   1617 				    p->p_pid, signum);
   1618 #endif
   1619 				break;		/* == ignore */
   1620 			}
   1621 			/*
   1622 			 * If there is a pending stop signal to process
   1623 			 * with default action, stop here,
   1624 			 * then clear the signal.  However,
   1625 			 * if process is member of an orphaned
   1626 			 * process group, ignore tty stop signals.
   1627 			 */
   1628 			if (prop & SA_STOP) {
   1629 				if (p->p_flag & P_TRACED ||
   1630 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1631 				    prop & SA_TTYSTOP))
   1632 					break;	/* == ignore */
   1633 				p->p_xstat = signum;
   1634 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1635 					child_psignal(p, dolock);
   1636 				if (dolock)
   1637 					SCHED_LOCK(s);
   1638 				proc_stop(p, 1);
   1639 			sigswitch:
   1640 				mi_switch(l, NULL);
   1641 				SCHED_ASSERT_UNLOCKED();
   1642 				if (dolock)
   1643 					splx(s);
   1644 				else
   1645 					dolock = 1;
   1646 				break;
   1647 			} else if (prop & SA_IGNORE) {
   1648 				/*
   1649 				 * Except for SIGCONT, shouldn't get here.
   1650 				 * Default action is to ignore; drop it.
   1651 				 */
   1652 				break;		/* == ignore */
   1653 			} else
   1654 				goto keep;
   1655 			/*NOTREACHED*/
   1656 
   1657 		case (long)SIG_IGN:
   1658 			/*
   1659 			 * Masking above should prevent us ever trying
   1660 			 * to take action on an ignored signal other
   1661 			 * than SIGCONT, unless process is traced.
   1662 			 */
   1663 #ifdef DEBUG_ISSIGNAL
   1664 			if ((prop & SA_CONT) == 0 &&
   1665 			    (p->p_flag & P_TRACED) == 0)
   1666 				printf("issignal\n");
   1667 #endif
   1668 			break;		/* == ignore */
   1669 
   1670 		default:
   1671 			/*
   1672 			 * This signal has an action, let
   1673 			 * postsig() process it.
   1674 			 */
   1675 			goto keep;
   1676 		}
   1677 	}
   1678 	/* NOTREACHED */
   1679 
   1680  keep:
   1681 						/* leave the signal for later */
   1682 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1683 	CHECKSIGS(p);
   1684 	if (locked && dolock)
   1685 		SCHED_LOCK(s);
   1686 	return (signum);
   1687 }
   1688 
   1689 /*
   1690  * Put the argument process into the stopped state and notify the parent
   1691  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1692  * on the run queue.
   1693  */
   1694 void
   1695 proc_stop(struct proc *p, int dowakeup)
   1696 {
   1697 	struct lwp *l;
   1698 	struct proc *parent;
   1699 	struct sadata_vp *vp;
   1700 
   1701 	SCHED_ASSERT_LOCKED();
   1702 
   1703 	/* XXX lock process LWP state */
   1704 	p->p_flag &= ~P_WAITED;
   1705 	p->p_stat = SSTOP;
   1706 	parent = p->p_pptr;
   1707 	parent->p_nstopchild++;
   1708 
   1709 	if (p->p_flag & P_SA) {
   1710 		/*
   1711 		 * Only (try to) put the LWP on the VP in stopped
   1712 		 * state.
   1713 		 * All other LWPs will suspend in sa_setwoken()
   1714 		 * because the VP-LWP in stopped state cannot be
   1715 		 * repossessed.
   1716 		 */
   1717 		SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1718 			l = vp->savp_lwp;
   1719 			if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) {
   1720 				l->l_stat = LSSTOP;
   1721 				p->p_nrlwps--;
   1722 			} else if (l->l_stat == LSRUN) {
   1723 				/* Remove LWP from the run queue */
   1724 				remrunqueue(l);
   1725 				l->l_stat = LSSTOP;
   1726 				p->p_nrlwps--;
   1727 			} else if (l->l_stat == LSSLEEP &&
   1728 			    l->l_flag & L_SA_IDLE) {
   1729 				l->l_flag &= ~L_SA_IDLE;
   1730 				l->l_stat = LSSTOP;
   1731 			}
   1732 		}
   1733 		goto out;
   1734 	}
   1735 
   1736 	/*
   1737 	 * Put as many LWP's as possible in stopped state.
   1738 	 * Sleeping ones will notice the stopped state as they try to
   1739 	 * return to userspace.
   1740 	 */
   1741 
   1742 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1743 		if (l->l_stat == LSONPROC) {
   1744 			/* XXX SMP this assumes that a LWP that is LSONPROC
   1745 			 * is curlwp and hence is about to be mi_switched
   1746 			 * away; the only callers of proc_stop() are:
   1747 			 * - psignal
   1748 			 * - issignal()
   1749 			 * For the former, proc_stop() is only called when
   1750 			 * no processes are running, so we don't worry.
   1751 			 * For the latter, proc_stop() is called right
   1752 			 * before mi_switch().
   1753 			 */
   1754 			l->l_stat = LSSTOP;
   1755 			p->p_nrlwps--;
   1756 		} else if (l->l_stat == LSRUN) {
   1757 			/* Remove LWP from the run queue */
   1758 			remrunqueue(l);
   1759 			l->l_stat = LSSTOP;
   1760 			p->p_nrlwps--;
   1761 		} else if ((l->l_stat == LSSLEEP) ||
   1762 		    (l->l_stat == LSSUSPENDED) ||
   1763 		    (l->l_stat == LSZOMB) ||
   1764 		    (l->l_stat == LSDEAD)) {
   1765 			/*
   1766 			 * Don't do anything; let sleeping LWPs
   1767 			 * discover the stopped state of the process
   1768 			 * on their way out of the kernel; otherwise,
   1769 			 * things like NFS threads that sleep with
   1770 			 * locks will block the rest of the system
   1771 			 * from getting any work done.
   1772 			 *
   1773 			 * Suspended/dead/zombie LWPs aren't going
   1774 			 * anywhere, so we don't need to touch them.
   1775 			 */
   1776 		}
   1777 #ifdef DIAGNOSTIC
   1778 		else {
   1779 			panic("proc_stop: process %d lwp %d "
   1780 			      "in unstoppable state %d.\n",
   1781 			    p->p_pid, l->l_lid, l->l_stat);
   1782 		}
   1783 #endif
   1784 	}
   1785 
   1786  out:
   1787 	/* XXX unlock process LWP state */
   1788 
   1789 	if (dowakeup)
   1790 		sched_wakeup((caddr_t)p->p_pptr);
   1791 }
   1792 
   1793 /*
   1794  * Given a process in state SSTOP, set the state back to SACTIVE and
   1795  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
   1796  *
   1797  * If no LWPs ended up runnable (and therefore able to take a signal),
   1798  * return a LWP that is sleeping interruptably. The caller can wake
   1799  * that LWP up to take a signal.
   1800  */
   1801 struct lwp *
   1802 proc_unstop(struct proc *p)
   1803 {
   1804 	struct lwp *l, *lr = NULL;
   1805 	struct sadata_vp *vp;
   1806 	int cantake = 0;
   1807 
   1808 	SCHED_ASSERT_LOCKED();
   1809 
   1810 	/*
   1811 	 * Our caller wants to be informed if there are only sleeping
   1812 	 * and interruptable LWPs left after we have run so that it
   1813 	 * can invoke setrunnable() if required - return one of the
   1814 	 * interruptable LWPs if this is the case.
   1815 	 */
   1816 
   1817 	if (!(p->p_flag & P_WAITED))
   1818 		p->p_pptr->p_nstopchild--;
   1819 	p->p_stat = SACTIVE;
   1820 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1821 		if (l->l_stat == LSRUN) {
   1822 			lr = NULL;
   1823 			cantake = 1;
   1824 		}
   1825 		if (l->l_stat != LSSTOP)
   1826 			continue;
   1827 
   1828 		if (l->l_wchan != NULL) {
   1829 			l->l_stat = LSSLEEP;
   1830 			if ((cantake == 0) && (l->l_flag & L_SINTR)) {
   1831 				lr = l;
   1832 				cantake = 1;
   1833 			}
   1834 		} else {
   1835 			setrunnable(l);
   1836 			lr = NULL;
   1837 			cantake = 1;
   1838 		}
   1839 	}
   1840 	if (p->p_flag & P_SA) {
   1841 		/* Only consider returning the LWP on the VP. */
   1842 		SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1843 			lr = vp->savp_lwp;
   1844 			if (lr->l_stat == LSSLEEP) {
   1845 				if (lr->l_flag & L_SA_YIELD) {
   1846 					setrunnable(lr);
   1847 					break;
   1848 				} else if (lr->l_flag & L_SINTR)
   1849 					return lr;
   1850 			}
   1851 		}
   1852 		return NULL;
   1853 	}
   1854 	return lr;
   1855 }
   1856 
   1857 /*
   1858  * Take the action for the specified signal
   1859  * from the current set of pending signals.
   1860  */
   1861 void
   1862 postsig(int signum)
   1863 {
   1864 	struct lwp *l;
   1865 	struct proc	*p;
   1866 	struct sigacts	*ps;
   1867 	sig_t		action;
   1868 	sigset_t	*returnmask;
   1869 
   1870 	l = curlwp;
   1871 	p = l->l_proc;
   1872 	ps = p->p_sigacts;
   1873 #ifdef DIAGNOSTIC
   1874 	if (signum == 0)
   1875 		panic("postsig");
   1876 #endif
   1877 
   1878 	KERNEL_PROC_LOCK(l);
   1879 
   1880 #ifdef MULTIPROCESSOR
   1881 	/*
   1882 	 * On MP, issignal() can return the same signal to multiple
   1883 	 * LWPs.  The LWPs will block above waiting for the kernel
   1884 	 * lock and the first LWP which gets through will then remove
   1885 	 * the signal from ps_siglist.  All other LWPs exit here.
   1886 	 */
   1887 	if (!sigismember(&p->p_sigctx.ps_siglist, signum)) {
   1888 		KERNEL_PROC_UNLOCK(l);
   1889 		return;
   1890 	}
   1891 #endif
   1892 	sigdelset(&p->p_sigctx.ps_siglist, signum);
   1893 	action = SIGACTION_PS(ps, signum).sa_handler;
   1894 	if (action == SIG_DFL) {
   1895 #ifdef KTRACE
   1896 		if (KTRPOINT(p, KTR_PSIG))
   1897 			ktrpsig(l, signum, action,
   1898 			    p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1899 			    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
   1900 			    NULL);
   1901 #endif
   1902 		/*
   1903 		 * Default action, where the default is to kill
   1904 		 * the process.  (Other cases were ignored above.)
   1905 		 */
   1906 		sigexit(l, signum);
   1907 		/* NOTREACHED */
   1908 	} else {
   1909 		ksiginfo_t *ksi;
   1910 		/*
   1911 		 * If we get here, the signal must be caught.
   1912 		 */
   1913 #ifdef DIAGNOSTIC
   1914 		if (action == SIG_IGN ||
   1915 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
   1916 			panic("postsig action");
   1917 #endif
   1918 		/*
   1919 		 * Set the new mask value and also defer further
   1920 		 * occurrences of this signal.
   1921 		 *
   1922 		 * Special case: user has done a sigpause.  Here the
   1923 		 * current mask is not of interest, but rather the
   1924 		 * mask from before the sigpause is what we want
   1925 		 * restored after the signal processing is completed.
   1926 		 */
   1927 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
   1928 			returnmask = &p->p_sigctx.ps_oldmask;
   1929 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
   1930 		} else
   1931 			returnmask = &p->p_sigctx.ps_sigmask;
   1932 		p->p_stats->p_ru.ru_nsignals++;
   1933 		ksi = ksiginfo_dequeue(p, signum);
   1934 #ifdef KTRACE
   1935 		if (KTRPOINT(p, KTR_PSIG))
   1936 			ktrpsig(l, signum, action,
   1937 			    p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1938 			    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
   1939 			    ksi);
   1940 #endif
   1941 		if (ksi == NULL) {
   1942 			ksiginfo_t ksi1;
   1943 			/*
   1944 			 * we did not save any siginfo for this, either
   1945 			 * because the signal was not caught, or because the
   1946 			 * user did not request SA_SIGINFO
   1947 			 */
   1948 			KSI_INIT_EMPTY(&ksi1);
   1949 			ksi1.ksi_signo = signum;
   1950 			kpsendsig(l, &ksi1, returnmask);
   1951 		} else {
   1952 			kpsendsig(l, ksi, returnmask);
   1953 			pool_put(&ksiginfo_pool, ksi);
   1954 		}
   1955 		p->p_sigctx.ps_lwp = 0;
   1956 		p->p_sigctx.ps_code = 0;
   1957 		p->p_sigctx.ps_signo = 0;
   1958 		(void) splsched();	/* XXXSMP */
   1959 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
   1960 		    &p->p_sigctx.ps_sigmask);
   1961 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
   1962 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1963 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1964 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
   1965 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
   1966 		}
   1967 		(void) spl0();		/* XXXSMP */
   1968 	}
   1969 
   1970 	KERNEL_PROC_UNLOCK(l);
   1971 }
   1972 
   1973 /*
   1974  * Kill the current process for stated reason.
   1975  */
   1976 void
   1977 killproc(struct proc *p, const char *why)
   1978 {
   1979 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1980 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1981 	psignal(p, SIGKILL);
   1982 }
   1983 
   1984 /*
   1985  * Force the current process to exit with the specified signal, dumping core
   1986  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1987  * allowing unrecoverable failures to terminate the process without changing
   1988  * signal state.  Mark the accounting record with the signal termination.
   1989  * If dumping core, save the signal number for the debugger.  Calls exit and
   1990  * does not return.
   1991  */
   1992 
   1993 #if defined(DEBUG)
   1994 int	kern_logsigexit = 1;	/* not static to make public for sysctl */
   1995 #else
   1996 int	kern_logsigexit = 0;	/* not static to make public for sysctl */
   1997 #endif
   1998 
   1999 static	const char logcoredump[] =
   2000 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
   2001 static	const char lognocoredump[] =
   2002 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
   2003 
   2004 /* Wrapper function for use in p_userret */
   2005 static void
   2006 lwp_coredump_hook(struct lwp *l, void *arg __unused)
   2007 {
   2008 	int s;
   2009 
   2010 	/*
   2011 	 * Suspend ourselves, so that the kernel stack and therefore
   2012 	 * the userland registers saved in the trapframe are around
   2013 	 * for coredump() to write them out.
   2014 	 */
   2015 	KERNEL_PROC_LOCK(l);
   2016 	l->l_flag &= ~L_DETACHED;
   2017 	SCHED_LOCK(s);
   2018 	l->l_stat = LSSUSPENDED;
   2019 	l->l_proc->p_nrlwps--;
   2020 	/* XXX NJWLWP check if this makes sense here: */
   2021 	l->l_proc->p_stats->p_ru.ru_nvcsw++;
   2022 	mi_switch(l, NULL);
   2023 	SCHED_ASSERT_UNLOCKED();
   2024 	splx(s);
   2025 
   2026 	lwp_exit(l);
   2027 }
   2028 
   2029 void
   2030 sigexit(struct lwp *l, int signum)
   2031 {
   2032 	struct proc	*p;
   2033 #if 0
   2034 	struct lwp	*l2;
   2035 #endif
   2036 	int		exitsig;
   2037 #ifdef COREDUMP
   2038 	int		error;
   2039 #endif
   2040 
   2041 	p = l->l_proc;
   2042 
   2043 	/*
   2044 	 * Don't permit coredump() or exit1() multiple times
   2045 	 * in the same process.
   2046 	 */
   2047 	if (p->p_flag & P_WEXIT) {
   2048 		KERNEL_PROC_UNLOCK(l);
   2049 		(*p->p_userret)(l, p->p_userret_arg);
   2050 	}
   2051 	p->p_flag |= P_WEXIT;
   2052 	/* We don't want to switch away from exiting. */
   2053 	/* XXX multiprocessor: stop LWPs on other processors. */
   2054 #if 0
   2055 	if (p->p_flag & P_SA) {
   2056 		LIST_FOREACH(l2, &p->p_lwps, l_sibling)
   2057 		    l2->l_flag &= ~L_SA;
   2058 		p->p_flag &= ~P_SA;
   2059 	}
   2060 #endif
   2061 
   2062 	/* Make other LWPs stick around long enough to be dumped */
   2063 	p->p_userret = lwp_coredump_hook;
   2064 	p->p_userret_arg = NULL;
   2065 
   2066 	exitsig = signum;
   2067 	p->p_acflag |= AXSIG;
   2068 	if (sigprop[signum] & SA_CORE) {
   2069 		p->p_sigctx.ps_signo = signum;
   2070 #ifdef COREDUMP
   2071 		if ((error = coredump(l, NULL)) == 0)
   2072 			exitsig |= WCOREFLAG;
   2073 #endif
   2074 
   2075 		if (kern_logsigexit) {
   2076 			/* XXX What if we ever have really large UIDs? */
   2077 			int uid = l->l_cred ?
   2078 			    (int)kauth_cred_geteuid(l->l_cred) : -1;
   2079 
   2080 #ifdef COREDUMP
   2081 			if (error)
   2082 				log(LOG_INFO, lognocoredump, p->p_pid,
   2083 				    p->p_comm, uid, signum, error);
   2084 			else
   2085 #endif
   2086 				log(LOG_INFO, logcoredump, p->p_pid,
   2087 				    p->p_comm, uid, signum);
   2088 		}
   2089 
   2090 	}
   2091 
   2092 	exit1(l, W_EXITCODE(0, exitsig));
   2093 	/* NOTREACHED */
   2094 }
   2095 
   2096 #ifdef COREDUMP
   2097 struct coredump_iostate {
   2098 	struct lwp *io_lwp;
   2099 	struct vnode *io_vp;
   2100 	kauth_cred_t io_cred;
   2101 	off_t io_offset;
   2102 };
   2103 
   2104 int
   2105 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
   2106 {
   2107 	struct coredump_iostate *io = cookie;
   2108 	int error;
   2109 
   2110 	error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
   2111 	    io->io_offset, segflg,
   2112 	    IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
   2113 	    segflg == UIO_USERSPACE ? io->io_lwp : NULL);
   2114 	if (error) {
   2115 		printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
   2116 		    io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
   2117 		    segflg == UIO_USERSPACE ? "user" : "system",
   2118 		    len, data, (long long) io->io_offset, error);
   2119 		return (error);
   2120 	}
   2121 
   2122 	io->io_offset += len;
   2123 	return (0);
   2124 }
   2125 
   2126 /*
   2127  * Dump core, into a file named "progname.core" or "core" (depending on the
   2128  * value of shortcorename), unless the process was setuid/setgid.
   2129  */
   2130 int
   2131 coredump(struct lwp *l, const char *pattern)
   2132 {
   2133 	struct vnode		*vp;
   2134 	struct proc		*p;
   2135 	struct vmspace		*vm;
   2136 	kauth_cred_t		cred;
   2137 	struct nameidata	nd;
   2138 	struct vattr		vattr;
   2139 	struct mount		*mp;
   2140 	struct coredump_iostate	io;
   2141 	int			error, error1;
   2142 	char			*name = NULL;
   2143 
   2144 	p = l->l_proc;
   2145 	vm = p->p_vmspace;
   2146 	cred = l->l_cred;
   2147 
   2148 	/*
   2149 	 * Make sure the process has not set-id, to prevent data leaks,
   2150 	 * unless it was specifically requested to allow set-id coredumps.
   2151 	 */
   2152 	if ((p->p_flag & P_SUGID) && !security_setidcore_dump)
   2153 		return EPERM;
   2154 
   2155 	/*
   2156 	 * Refuse to core if the data + stack + user size is larger than
   2157 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
   2158 	 * data.
   2159 	 */
   2160 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   2161 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   2162 		return EFBIG;		/* better error code? */
   2163 
   2164 restart:
   2165 	/*
   2166 	 * The core dump will go in the current working directory.  Make
   2167 	 * sure that the directory is still there and that the mount flags
   2168 	 * allow us to write core dumps there.
   2169 	 */
   2170 	vp = p->p_cwdi->cwdi_cdir;
   2171 	if (vp->v_mount == NULL ||
   2172 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) {
   2173 		error = EPERM;
   2174 		goto done;
   2175 	}
   2176 
   2177 	if ((p->p_flag & P_SUGID) && security_setidcore_dump)
   2178 		pattern = security_setidcore_path;
   2179 
   2180 	if (pattern == NULL)
   2181 		pattern = p->p_limit->pl_corename;
   2182 	if (name == NULL) {
   2183 		name = PNBUF_GET();
   2184 	}
   2185 	if ((error = build_corename(p, name, pattern, MAXPATHLEN)) != 0)
   2186 		goto done;
   2187 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, l);
   2188 	if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
   2189 	    S_IRUSR | S_IWUSR)) != 0)
   2190 		goto done;
   2191 	vp = nd.ni_vp;
   2192 
   2193 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
   2194 		VOP_UNLOCK(vp, 0);
   2195 		if ((error = vn_close(vp, FWRITE, cred, l)) != 0)
   2196 			goto done;
   2197 		if ((error = vn_start_write(NULL, &mp,
   2198 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   2199 			goto done;
   2200 		goto restart;
   2201 	}
   2202 
   2203 	/* Don't dump to non-regular files or files with links. */
   2204 	if (vp->v_type != VREG ||
   2205 	    VOP_GETATTR(vp, &vattr, cred, l) || vattr.va_nlink != 1) {
   2206 		error = EINVAL;
   2207 		goto out;
   2208 	}
   2209 	VATTR_NULL(&vattr);
   2210 	vattr.va_size = 0;
   2211 
   2212 	if ((p->p_flag & P_SUGID) && security_setidcore_dump) {
   2213 		vattr.va_uid = security_setidcore_owner;
   2214 		vattr.va_gid = security_setidcore_group;
   2215 		vattr.va_mode = security_setidcore_mode;
   2216 	}
   2217 
   2218 	VOP_LEASE(vp, l, cred, LEASE_WRITE);
   2219 	VOP_SETATTR(vp, &vattr, cred, l);
   2220 	p->p_acflag |= ACORE;
   2221 
   2222 	io.io_lwp = l;
   2223 	io.io_vp = vp;
   2224 	io.io_cred = cred;
   2225 	io.io_offset = 0;
   2226 
   2227 	/* Now dump the actual core file. */
   2228 	error = (*p->p_execsw->es_coredump)(l, &io);
   2229  out:
   2230 	VOP_UNLOCK(vp, 0);
   2231 	vn_finished_write(mp, 0);
   2232 	error1 = vn_close(vp, FWRITE, cred, l);
   2233 	if (error == 0)
   2234 		error = error1;
   2235 done:
   2236 	if (name != NULL)
   2237 		PNBUF_PUT(name);
   2238 	return error;
   2239 }
   2240 #endif /* COREDUMP */
   2241 
   2242 /*
   2243  * Nonexistent system call-- signal process (may want to handle it).
   2244  * Flag error in case process won't see signal immediately (blocked or ignored).
   2245  */
   2246 #ifndef PTRACE
   2247 __weak_alias(sys_ptrace, sys_nosys);
   2248 #endif
   2249 
   2250 /* ARGSUSED */
   2251 int
   2252 sys_nosys(struct lwp *l, void *v __unused, register_t *retval __unused)
   2253 {
   2254 	struct proc 	*p;
   2255 
   2256 	p = l->l_proc;
   2257 	psignal(p, SIGSYS);
   2258 	return (ENOSYS);
   2259 }
   2260 
   2261 #ifdef COREDUMP
   2262 static int
   2263 build_corename(struct proc *p, char *dst, const char *src, size_t len)
   2264 {
   2265 	const char	*s;
   2266 	char		*d, *end;
   2267 	int		i;
   2268 
   2269 	for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
   2270 		if (*s == '%') {
   2271 			switch (*(s + 1)) {
   2272 			case 'n':
   2273 				i = snprintf(d, end - d, "%s", p->p_comm);
   2274 				break;
   2275 			case 'p':
   2276 				i = snprintf(d, end - d, "%d", p->p_pid);
   2277 				break;
   2278 			case 'u':
   2279 				i = snprintf(d, end - d, "%.*s",
   2280 				    (int)sizeof p->p_pgrp->pg_session->s_login,
   2281 				    p->p_pgrp->pg_session->s_login);
   2282 				break;
   2283 			case 't':
   2284 				i = snprintf(d, end - d, "%ld",
   2285 				    p->p_stats->p_start.tv_sec);
   2286 				break;
   2287 			default:
   2288 				goto copy;
   2289 			}
   2290 			d += i;
   2291 			s++;
   2292 		} else {
   2293  copy:			*d = *s;
   2294 			d++;
   2295 		}
   2296 		if (d >= end)
   2297 			return (ENAMETOOLONG);
   2298 	}
   2299 	*d = '\0';
   2300 	return 0;
   2301 }
   2302 #endif /* COREDUMP */
   2303 
   2304 void
   2305 getucontext(struct lwp *l, ucontext_t *ucp)
   2306 {
   2307 	struct proc	*p;
   2308 
   2309 	p = l->l_proc;
   2310 
   2311 	ucp->uc_flags = 0;
   2312 	ucp->uc_link = l->l_ctxlink;
   2313 
   2314 	(void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
   2315 	ucp->uc_flags |= _UC_SIGMASK;
   2316 
   2317 	/*
   2318 	 * The (unsupplied) definition of the `current execution stack'
   2319 	 * in the System V Interface Definition appears to allow returning
   2320 	 * the main context stack.
   2321 	 */
   2322 	if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
   2323 		ucp->uc_stack.ss_sp = (void *)USRSTACK;
   2324 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
   2325 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
   2326 	} else {
   2327 		/* Simply copy alternate signal execution stack. */
   2328 		ucp->uc_stack = p->p_sigctx.ps_sigstk;
   2329 	}
   2330 	ucp->uc_flags |= _UC_STACK;
   2331 
   2332 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
   2333 }
   2334 
   2335 /* ARGSUSED */
   2336 int
   2337 sys_getcontext(struct lwp *l, void *v, register_t *retval __unused)
   2338 {
   2339 	struct sys_getcontext_args /* {
   2340 		syscallarg(struct __ucontext *) ucp;
   2341 	} */ *uap = v;
   2342 	ucontext_t uc;
   2343 
   2344 	getucontext(l, &uc);
   2345 
   2346 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
   2347 }
   2348 
   2349 int
   2350 setucontext(struct lwp *l, const ucontext_t *ucp)
   2351 {
   2352 	struct proc	*p;
   2353 	int		error;
   2354 
   2355 	p = l->l_proc;
   2356 	if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
   2357 		return (error);
   2358 	l->l_ctxlink = ucp->uc_link;
   2359 
   2360 	if ((ucp->uc_flags & _UC_SIGMASK) != 0)
   2361 		sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
   2362 
   2363 	/*
   2364 	 * If there was stack information, update whether or not we are
   2365 	 * still running on an alternate signal stack.
   2366 	 */
   2367 	if ((ucp->uc_flags & _UC_STACK) != 0) {
   2368 		if (ucp->uc_stack.ss_flags & SS_ONSTACK)
   2369 			p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
   2370 		else
   2371 			p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
   2372 	}
   2373 
   2374 	return 0;
   2375 }
   2376 
   2377 /* ARGSUSED */
   2378 int
   2379 sys_setcontext(struct lwp *l, void *v, register_t *retval __unused)
   2380 {
   2381 	struct sys_setcontext_args /* {
   2382 		syscallarg(const ucontext_t *) ucp;
   2383 	} */ *uap = v;
   2384 	ucontext_t uc;
   2385 	int error;
   2386 
   2387 	if (SCARG(uap, ucp) == NULL)	/* i.e. end of uc_link chain */
   2388 		exit1(l, W_EXITCODE(0, 0));
   2389 	else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
   2390 	    (error = setucontext(l, &uc)) != 0)
   2391 		return (error);
   2392 
   2393 	return (EJUSTRETURN);
   2394 }
   2395 
   2396 /*
   2397  * sigtimedwait(2) system call, used also for implementation
   2398  * of sigwaitinfo() and sigwait().
   2399  *
   2400  * This only handles single LWP in signal wait. libpthread provides
   2401  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
   2402  */
   2403 int
   2404 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
   2405 {
   2406 	return __sigtimedwait1(l, v, retval, copyout, copyin, copyout);
   2407 }
   2408 
   2409 int
   2410 __sigtimedwait1(struct lwp *l, void *v, register_t *retval __unused,
   2411     copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
   2412 {
   2413 	struct sys___sigtimedwait_args /* {
   2414 		syscallarg(const sigset_t *) set;
   2415 		syscallarg(siginfo_t *) info;
   2416 		syscallarg(struct timespec *) timeout;
   2417 	} */ *uap = v;
   2418 	sigset_t *waitset, twaitset;
   2419 	struct proc *p = l->l_proc;
   2420 	int error, signum;
   2421 	int timo = 0;
   2422 	struct timespec ts, tsstart;
   2423 	ksiginfo_t *ksi;
   2424 
   2425 	memset(&tsstart, 0, sizeof tsstart);	 /* XXX gcc */
   2426 
   2427 	MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
   2428 
   2429 	if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
   2430 		FREE(waitset, M_TEMP);
   2431 		return (error);
   2432 	}
   2433 
   2434 	/*
   2435 	 * Silently ignore SA_CANTMASK signals. psignal1() would
   2436 	 * ignore SA_CANTMASK signals in waitset, we do this
   2437 	 * only for the below siglist check.
   2438 	 */
   2439 	sigminusset(&sigcantmask, waitset);
   2440 
   2441 	/*
   2442 	 * First scan siglist and check if there is signal from
   2443 	 * our waitset already pending.
   2444 	 */
   2445 	twaitset = *waitset;
   2446 	__sigandset(&p->p_sigctx.ps_siglist, &twaitset);
   2447 	if ((signum = firstsig(&twaitset))) {
   2448 		/* found pending signal */
   2449 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   2450 		ksi = ksiginfo_dequeue(p, signum);
   2451 		if (!ksi) {
   2452 			/* No queued siginfo, manufacture one */
   2453 			ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
   2454 			KSI_INIT(ksi);
   2455 			ksi->ksi_info._signo = signum;
   2456 			ksi->ksi_info._code = SI_USER;
   2457 		}
   2458 
   2459 		goto sig;
   2460 	}
   2461 
   2462 	/*
   2463 	 * Calculate timeout, if it was specified.
   2464 	 */
   2465 	if (SCARG(uap, timeout)) {
   2466 		uint64_t ms;
   2467 
   2468 		if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts))))
   2469 			return (error);
   2470 
   2471 		ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
   2472 		timo = mstohz(ms);
   2473 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
   2474 			timo = 1;
   2475 		if (timo <= 0)
   2476 			return (EAGAIN);
   2477 
   2478 		/*
   2479 		 * Remember current uptime, it would be used in
   2480 		 * ECANCELED/ERESTART case.
   2481 		 */
   2482 		getnanouptime(&tsstart);
   2483 	}
   2484 
   2485 	/*
   2486 	 * Setup ps_sigwait list. Pass pointer to malloced memory
   2487 	 * here; it's not possible to pass pointer to a structure
   2488 	 * on current process's stack, the current process might
   2489 	 * be swapped out at the time the signal would get delivered.
   2490 	 */
   2491 	ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
   2492 	p->p_sigctx.ps_sigwaited = ksi;
   2493 	p->p_sigctx.ps_sigwait = waitset;
   2494 
   2495 	/*
   2496 	 * Wait for signal to arrive. We can either be woken up or
   2497 	 * time out.
   2498 	 */
   2499 	error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
   2500 
   2501 	/*
   2502 	 * Need to find out if we woke as a result of lwp_wakeup()
   2503 	 * or a signal outside our wait set.
   2504 	 */
   2505 	if (error == EINTR && p->p_sigctx.ps_sigwaited
   2506 	    && !firstsig(&p->p_sigctx.ps_siglist)) {
   2507 		/* wakeup via _lwp_wakeup() */
   2508 		error = ECANCELED;
   2509 	} else if (!error && p->p_sigctx.ps_sigwaited) {
   2510 		/* spurious wakeup - arrange for syscall restart */
   2511 		error = ERESTART;
   2512 		goto fail;
   2513 	}
   2514 
   2515 	/*
   2516 	 * On error, clear sigwait indication. psignal1() clears it
   2517 	 * in !error case.
   2518 	 */
   2519 	if (error) {
   2520 		p->p_sigctx.ps_sigwaited = NULL;
   2521 
   2522 		/*
   2523 		 * If the sleep was interrupted (either by signal or wakeup),
   2524 		 * update the timeout and copyout new value back.
   2525 		 * It would be used when the syscall would be restarted
   2526 		 * or called again.
   2527 		 */
   2528 		if (timo && (error == ERESTART || error == ECANCELED)) {
   2529 			struct timespec tsnow;
   2530 			int err;
   2531 
   2532 /* XXX double check the following change */
   2533 			getnanouptime(&tsnow);
   2534 
   2535 			/* compute how much time has passed since start */
   2536 			timespecsub(&tsnow, &tsstart, &tsnow);
   2537 			/* substract passed time from timeout */
   2538 			timespecsub(&ts, &tsnow, &ts);
   2539 
   2540 			if (ts.tv_sec < 0) {
   2541 				error = EAGAIN;
   2542 				goto fail;
   2543 			}
   2544 /* XXX double check the previous change */
   2545 
   2546 			/* copy updated timeout to userland */
   2547 			if ((err = (*put_timeout)(&ts, SCARG(uap, timeout),
   2548 			    sizeof(ts)))) {
   2549 				error = err;
   2550 				goto fail;
   2551 			}
   2552 		}
   2553 
   2554 		goto fail;
   2555 	}
   2556 
   2557 	/*
   2558 	 * If a signal from the wait set arrived, copy it to userland.
   2559 	 * Copy only the used part of siginfo, the padding part is
   2560 	 * left unchanged (userland is not supposed to touch it anyway).
   2561 	 */
   2562  sig:
   2563 	return (*put_info)(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info));
   2564 
   2565  fail:
   2566 	FREE(waitset, M_TEMP);
   2567 	pool_put(&ksiginfo_pool, ksi);
   2568 	p->p_sigctx.ps_sigwait = NULL;
   2569 
   2570 	return (error);
   2571 }
   2572 
   2573 /*
   2574  * Returns true if signal is ignored or masked for passed process.
   2575  */
   2576 int
   2577 sigismasked(struct proc *p, int sig)
   2578 {
   2579 
   2580 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
   2581 	    sigismember(&p->p_sigctx.ps_sigmask, sig));
   2582 }
   2583 
   2584 static int
   2585 filt_sigattach(struct knote *kn)
   2586 {
   2587 	struct proc *p = curproc;
   2588 
   2589 	kn->kn_ptr.p_proc = p;
   2590 	kn->kn_flags |= EV_CLEAR;               /* automatically set */
   2591 
   2592 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
   2593 
   2594 	return (0);
   2595 }
   2596 
   2597 static void
   2598 filt_sigdetach(struct knote *kn)
   2599 {
   2600 	struct proc *p = kn->kn_ptr.p_proc;
   2601 
   2602 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
   2603 }
   2604 
   2605 /*
   2606  * signal knotes are shared with proc knotes, so we apply a mask to
   2607  * the hint in order to differentiate them from process hints.  This
   2608  * could be avoided by using a signal-specific knote list, but probably
   2609  * isn't worth the trouble.
   2610  */
   2611 static int
   2612 filt_signal(struct knote *kn, long hint)
   2613 {
   2614 
   2615 	if (hint & NOTE_SIGNAL) {
   2616 		hint &= ~NOTE_SIGNAL;
   2617 
   2618 		if (kn->kn_id == hint)
   2619 			kn->kn_data++;
   2620 	}
   2621 	return (kn->kn_data != 0);
   2622 }
   2623 
   2624 const struct filterops sig_filtops = {
   2625 	0, filt_sigattach, filt_sigdetach, filt_signal
   2626 };
   2627