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