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