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