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