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