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