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