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