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