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