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