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