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