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