Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.131
      1 /*	$NetBSD: kern_sig.c,v 1.131 2003/02/03 22:56:23 jdolecek 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.131 2003/02/03 22:56:23 jdolecek Exp $");
     45 
     46 #include "opt_ktrace.h"
     47 #include "opt_compat_sunos.h"
     48 #include "opt_compat_netbsd32.h"
     49 
     50 #define	SIGPROP		/* include signal properties table */
     51 #include <sys/param.h>
     52 #include <sys/signalvar.h>
     53 #include <sys/resourcevar.h>
     54 #include <sys/namei.h>
     55 #include <sys/vnode.h>
     56 #include <sys/proc.h>
     57 #include <sys/systm.h>
     58 #include <sys/timeb.h>
     59 #include <sys/times.h>
     60 #include <sys/buf.h>
     61 #include <sys/acct.h>
     62 #include <sys/file.h>
     63 #include <sys/kernel.h>
     64 #include <sys/wait.h>
     65 #include <sys/ktrace.h>
     66 #include <sys/syslog.h>
     67 #include <sys/stat.h>
     68 #include <sys/core.h>
     69 #include <sys/filedesc.h>
     70 #include <sys/malloc.h>
     71 #include <sys/pool.h>
     72 #include <sys/ucontext.h>
     73 #include <sys/sa.h>
     74 #include <sys/savar.h>
     75 #include <sys/exec.h>
     76 
     77 #include <sys/mount.h>
     78 #include <sys/syscallargs.h>
     79 
     80 #include <machine/cpu.h>
     81 
     82 #include <sys/user.h>		/* for coredump */
     83 
     84 #include <uvm/uvm_extern.h>
     85 
     86 static void	proc_stop(struct proc *p);
     87 static int	build_corename(struct proc *, char [MAXPATHLEN]);
     88 sigset_t	contsigmask, stopsigmask, sigcantmask;
     89 
     90 struct pool	sigacts_pool;	/* memory pool for sigacts structures */
     91 struct pool	siginfo_pool;	/* memory pool for siginfo structures */
     92 
     93 /*
     94  * Can process p, with pcred pc, send the signal signum to process q?
     95  */
     96 #define	CANSIGNAL(p, pc, q, signum) \
     97 	((pc)->pc_ucred->cr_uid == 0 || \
     98 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
     99 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
    100 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
    101 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
    102 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
    103 
    104 /*
    105  * Initialize signal-related data structures.
    106  */
    107 void
    108 signal_init(void)
    109 {
    110 
    111 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    112 	    &pool_allocator_nointr);
    113 	pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
    114 	    &pool_allocator_nointr);
    115 }
    116 
    117 /*
    118  * Create an initial sigctx structure, using the same signal state
    119  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
    120  * copy it from parent.
    121  */
    122 void
    123 sigactsinit(struct proc *np, struct proc *pp, int share)
    124 {
    125 	struct sigacts *ps;
    126 
    127 	if (share) {
    128 		np->p_sigacts = pp->p_sigacts;
    129 		pp->p_sigacts->sa_refcnt++;
    130 	} else {
    131 		ps = pool_get(&sigacts_pool, PR_WAITOK);
    132 		if (pp)
    133 			memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
    134 		else
    135 			memset(ps, '\0', sizeof(struct sigacts));
    136 		ps->sa_refcnt = 1;
    137 		np->p_sigacts = ps;
    138 	}
    139 }
    140 
    141 /*
    142  * Make this process not share its sigctx, maintaining all
    143  * signal state.
    144  */
    145 void
    146 sigactsunshare(struct proc *p)
    147 {
    148 	struct sigacts *oldps;
    149 
    150 	if (p->p_sigacts->sa_refcnt == 1)
    151 		return;
    152 
    153 	oldps = p->p_sigacts;
    154 	sigactsinit(p, NULL, 0);
    155 
    156 	if (--oldps->sa_refcnt == 0)
    157 		pool_put(&sigacts_pool, oldps);
    158 }
    159 
    160 /*
    161  * Release a sigctx structure.
    162  */
    163 void
    164 sigactsfree(struct proc *p)
    165 {
    166 	struct sigacts *ps;
    167 
    168 	ps = p->p_sigacts;
    169 	if (--ps->sa_refcnt > 0)
    170 		return;
    171 
    172 	pool_put(&sigacts_pool, ps);
    173 }
    174 
    175 int
    176 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
    177 	struct sigaction *osa, void *tramp, int vers)
    178 {
    179 	struct sigacts	*ps;
    180 	int		prop;
    181 
    182 	ps = p->p_sigacts;
    183 	if (signum <= 0 || signum >= NSIG)
    184 		return (EINVAL);
    185 
    186 	/*
    187 	 * Trampoline ABI version 0 is reserved for the legacy
    188 	 * kernel-provided on-stack trampoline.  Conversely, if
    189 	 * we are using a non-0 ABI version, we must have a
    190 	 * trampoline.
    191 	 */
    192 	if ((vers != 0 && tramp == NULL) ||
    193 	    (vers == 0 && tramp != NULL))
    194 		return (EINVAL);
    195 
    196 	if (osa)
    197 		*osa = SIGACTION_PS(ps, signum);
    198 
    199 	if (nsa) {
    200 		if (nsa->sa_flags & ~SA_ALLBITS)
    201 			return (EINVAL);
    202 
    203 		prop = sigprop[signum];
    204 		if (prop & SA_CANTMASK)
    205 			return (EINVAL);
    206 
    207 		(void) splsched();	/* XXXSMP */
    208 		SIGACTION_PS(ps, signum) = *nsa;
    209 		ps->sa_sigdesc[signum].sd_tramp = tramp;
    210 		ps->sa_sigdesc[signum].sd_vers = vers;
    211 		sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    212 		if ((prop & SA_NORESET) != 0)
    213 			SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    214 		if (signum == SIGCHLD) {
    215 			if (nsa->sa_flags & SA_NOCLDSTOP)
    216 				p->p_flag |= P_NOCLDSTOP;
    217 			else
    218 				p->p_flag &= ~P_NOCLDSTOP;
    219 			if (nsa->sa_flags & SA_NOCLDWAIT) {
    220 				/*
    221 				 * Paranoia: since SA_NOCLDWAIT is implemented
    222 				 * by reparenting the dying child to PID 1 (and
    223 				 * trust it to reap the zombie), PID 1 itself
    224 				 * is forbidden to set SA_NOCLDWAIT.
    225 				 */
    226 				if (p->p_pid == 1)
    227 					p->p_flag &= ~P_NOCLDWAIT;
    228 				else
    229 					p->p_flag |= P_NOCLDWAIT;
    230 			} else
    231 				p->p_flag &= ~P_NOCLDWAIT;
    232 		}
    233 		if ((nsa->sa_flags & SA_NODEFER) == 0)
    234 			sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    235 		else
    236 			sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    237 		/*
    238 	 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    239 		 * SIG_IGN, and for signals set to SIG_DFL where the default is
    240 		 * to ignore. However, don't put SIGCONT in
    241 		 * p_sigctx.ps_sigignore, as we have to restart the process.
    242 	 	 */
    243 		if (nsa->sa_handler == SIG_IGN ||
    244 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    245 						/* never to be seen again */
    246 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    247 			if (signum != SIGCONT) {
    248 						/* easier in psignal */
    249 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    250 			}
    251 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    252 		} else {
    253 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
    254 			if (nsa->sa_handler == SIG_DFL)
    255 				sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    256 			else
    257 				sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    258 		}
    259 		(void) spl0();
    260 	}
    261 
    262 	return (0);
    263 }
    264 
    265 /* ARGSUSED */
    266 int
    267 sys___sigaction14(struct lwp *l, void *v, register_t *retval)
    268 {
    269 	struct sys___sigaction14_args /* {
    270 		syscallarg(int)				signum;
    271 		syscallarg(const struct sigaction *)	nsa;
    272 		syscallarg(struct sigaction *)		osa;
    273 	} */ *uap = v;
    274 	struct proc		*p;
    275 	struct sigaction	nsa, osa;
    276 	int			error;
    277 
    278 	if (SCARG(uap, nsa)) {
    279 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    280 		if (error)
    281 			return (error);
    282 	}
    283 	p = l->l_proc;
    284 	error = sigaction1(p, SCARG(uap, signum),
    285 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    286 	    NULL, 0);
    287 	if (error)
    288 		return (error);
    289 	if (SCARG(uap, osa)) {
    290 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    291 		if (error)
    292 			return (error);
    293 	}
    294 	return (0);
    295 }
    296 
    297 /* ARGSUSED */
    298 int
    299 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
    300 {
    301 	struct sys___sigaction_sigtramp_args /* {
    302 		syscallarg(int)				signum;
    303 		syscallarg(const struct sigaction *)	nsa;
    304 		syscallarg(struct sigaction *)		osa;
    305 		syscallarg(void *)			tramp;
    306 		syscallarg(int)				vers;
    307 	} */ *uap = v;
    308 	struct proc *p = l->l_proc;
    309 	struct sigaction nsa, osa;
    310 	int error;
    311 
    312 	if (SCARG(uap, nsa)) {
    313 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    314 		if (error)
    315 			return (error);
    316 	}
    317 	error = sigaction1(p, SCARG(uap, signum),
    318 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    319 	    SCARG(uap, tramp), SCARG(uap, vers));
    320 	if (error)
    321 		return (error);
    322 	if (SCARG(uap, osa)) {
    323 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    324 		if (error)
    325 			return (error);
    326 	}
    327 	return (0);
    328 }
    329 
    330 /*
    331  * Initialize signal state for process 0;
    332  * set to ignore signals that are ignored by default and disable the signal
    333  * stack.
    334  */
    335 void
    336 siginit(struct proc *p)
    337 {
    338 	struct sigacts	*ps;
    339 	int		signum, prop;
    340 
    341 	ps = p->p_sigacts;
    342 	sigemptyset(&contsigmask);
    343 	sigemptyset(&stopsigmask);
    344 	sigemptyset(&sigcantmask);
    345 	for (signum = 1; signum < NSIG; signum++) {
    346 		prop = sigprop[signum];
    347 		if (prop & SA_CONT)
    348 			sigaddset(&contsigmask, signum);
    349 		if (prop & SA_STOP)
    350 			sigaddset(&stopsigmask, signum);
    351 		if (prop & SA_CANTMASK)
    352 			sigaddset(&sigcantmask, signum);
    353 		if (prop & SA_IGNORE && signum != SIGCONT)
    354 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    355 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    356 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    357 	}
    358 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    359 	p->p_flag &= ~P_NOCLDSTOP;
    360 
    361 	/*
    362 	 * Reset stack state to the user stack.
    363 	 */
    364 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    365 	p->p_sigctx.ps_sigstk.ss_size = 0;
    366 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    367 
    368 	/* One reference. */
    369 	ps->sa_refcnt = 1;
    370 }
    371 
    372 /*
    373  * Reset signals for an exec of the specified process.
    374  */
    375 void
    376 execsigs(struct proc *p)
    377 {
    378 	struct sigacts	*ps;
    379 	int		signum, prop;
    380 
    381 	sigactsunshare(p);
    382 
    383 	ps = p->p_sigacts;
    384 
    385 	/*
    386 	 * Reset caught signals.  Held signals remain held
    387 	 * through p_sigctx.ps_sigmask (unless they were caught,
    388 	 * and are now ignored by default).
    389 	 */
    390 	for (signum = 1; signum < NSIG; signum++) {
    391 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
    392 			prop = sigprop[signum];
    393 			if (prop & SA_IGNORE) {
    394 				if ((prop & SA_CONT) == 0)
    395 					sigaddset(&p->p_sigctx.ps_sigignore,
    396 					    signum);
    397 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    398 			}
    399 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    400 		}
    401 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    402 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    403 	}
    404 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    405 	p->p_flag &= ~P_NOCLDSTOP;
    406 
    407 	/*
    408 	 * Reset stack state to the user stack.
    409 	 */
    410 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    411 	p->p_sigctx.ps_sigstk.ss_size = 0;
    412 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    413 }
    414 
    415 int
    416 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
    417 {
    418 
    419 	if (oss)
    420 		*oss = p->p_sigctx.ps_sigmask;
    421 
    422 	if (nss) {
    423 		(void)splsched();	/* XXXSMP */
    424 		switch (how) {
    425 		case SIG_BLOCK:
    426 			sigplusset(nss, &p->p_sigctx.ps_sigmask);
    427 			break;
    428 		case SIG_UNBLOCK:
    429 			sigminusset(nss, &p->p_sigctx.ps_sigmask);
    430 			CHECKSIGS(p);
    431 			break;
    432 		case SIG_SETMASK:
    433 			p->p_sigctx.ps_sigmask = *nss;
    434 			CHECKSIGS(p);
    435 			break;
    436 		default:
    437 			(void)spl0();	/* XXXSMP */
    438 			return (EINVAL);
    439 		}
    440 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    441 		(void)spl0();		/* XXXSMP */
    442 	}
    443 
    444 	return (0);
    445 }
    446 
    447 /*
    448  * Manipulate signal mask.
    449  * Note that we receive new mask, not pointer,
    450  * and return old mask as return value;
    451  * the library stub does the rest.
    452  */
    453 int
    454 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
    455 {
    456 	struct sys___sigprocmask14_args /* {
    457 		syscallarg(int)			how;
    458 		syscallarg(const sigset_t *)	set;
    459 		syscallarg(sigset_t *)		oset;
    460 	} */ *uap = v;
    461 	struct proc	*p;
    462 	sigset_t	nss, oss;
    463 	int		error;
    464 
    465 	if (SCARG(uap, set)) {
    466 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    467 		if (error)
    468 			return (error);
    469 	}
    470 	p = l->l_proc;
    471 	error = sigprocmask1(p, SCARG(uap, how),
    472 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    473 	if (error)
    474 		return (error);
    475 	if (SCARG(uap, oset)) {
    476 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    477 		if (error)
    478 			return (error);
    479 	}
    480 	return (0);
    481 }
    482 
    483 void
    484 sigpending1(struct proc *p, sigset_t *ss)
    485 {
    486 
    487 	*ss = p->p_sigctx.ps_siglist;
    488 	sigminusset(&p->p_sigctx.ps_sigmask, ss);
    489 }
    490 
    491 /* ARGSUSED */
    492 int
    493 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
    494 {
    495 	struct sys___sigpending14_args /* {
    496 		syscallarg(sigset_t *)	set;
    497 	} */ *uap = v;
    498 	struct proc	*p;
    499 	sigset_t	ss;
    500 
    501 	p = l->l_proc;
    502 	sigpending1(p, &ss);
    503 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    504 }
    505 
    506 int
    507 sigsuspend1(struct proc *p, const sigset_t *ss)
    508 {
    509 	struct sigacts *ps;
    510 
    511 	ps = p->p_sigacts;
    512 	if (ss) {
    513 		/*
    514 		 * When returning from sigpause, we want
    515 		 * the old mask to be restored after the
    516 		 * signal handler has finished.  Thus, we
    517 		 * save it here and mark the sigctx structure
    518 		 * to indicate this.
    519 		 */
    520 		p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
    521 		p->p_sigctx.ps_flags |= SAS_OLDMASK;
    522 		(void) splsched();	/* XXXSMP */
    523 		p->p_sigctx.ps_sigmask = *ss;
    524 		CHECKSIGS(p);
    525 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    526 		(void) spl0();		/* XXXSMP */
    527 	}
    528 
    529 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    530 		/* void */;
    531 	/* always return EINTR rather than ERESTART... */
    532 	return (EINTR);
    533 }
    534 
    535 /*
    536  * Suspend process until signal, providing mask to be set
    537  * in the meantime.  Note nonstandard calling convention:
    538  * libc stub passes mask, not pointer, to save a copyin.
    539  */
    540 /* ARGSUSED */
    541 int
    542 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
    543 {
    544 	struct sys___sigsuspend14_args /* {
    545 		syscallarg(const sigset_t *)	set;
    546 	} */ *uap = v;
    547 	struct proc	*p;
    548 	sigset_t	ss;
    549 	int		error;
    550 
    551 	if (SCARG(uap, set)) {
    552 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    553 		if (error)
    554 			return (error);
    555 	}
    556 
    557 	p = l->l_proc;
    558 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
    559 }
    560 
    561 int
    562 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
    563 	struct sigaltstack *oss)
    564 {
    565 
    566 	if (oss)
    567 		*oss = p->p_sigctx.ps_sigstk;
    568 
    569 	if (nss) {
    570 		if (nss->ss_flags & ~SS_ALLBITS)
    571 			return (EINVAL);
    572 
    573 		if (nss->ss_flags & SS_DISABLE) {
    574 			if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    575 				return (EINVAL);
    576 		} else {
    577 			if (nss->ss_size < MINSIGSTKSZ)
    578 				return (ENOMEM);
    579 		}
    580 		p->p_sigctx.ps_sigstk = *nss;
    581 	}
    582 
    583 	return (0);
    584 }
    585 
    586 /* ARGSUSED */
    587 int
    588 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
    589 {
    590 	struct sys___sigaltstack14_args /* {
    591 		syscallarg(const struct sigaltstack *)	nss;
    592 		syscallarg(struct sigaltstack *)	oss;
    593 	} */ *uap = v;
    594 	struct proc		*p;
    595 	struct sigaltstack	nss, oss;
    596 	int			error;
    597 
    598 	if (SCARG(uap, nss)) {
    599 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    600 		if (error)
    601 			return (error);
    602 	}
    603 	p = l->l_proc;
    604 	error = sigaltstack1(p,
    605 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    606 	if (error)
    607 		return (error);
    608 	if (SCARG(uap, oss)) {
    609 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    610 		if (error)
    611 			return (error);
    612 	}
    613 	return (0);
    614 }
    615 
    616 /* ARGSUSED */
    617 int
    618 sys_kill(struct lwp *l, void *v, register_t *retval)
    619 {
    620 	struct sys_kill_args /* {
    621 		syscallarg(int)	pid;
    622 		syscallarg(int)	signum;
    623 	} */ *uap = v;
    624 	struct proc	*cp, *p;
    625 	struct pcred	*pc;
    626 
    627 	cp = l->l_proc;
    628 	pc = cp->p_cred;
    629 	if ((u_int)SCARG(uap, signum) >= NSIG)
    630 		return (EINVAL);
    631 	if (SCARG(uap, pid) > 0) {
    632 		/* kill single process */
    633 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    634 			return (ESRCH);
    635 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    636 			return (EPERM);
    637 		if (SCARG(uap, signum))
    638 			psignal(p, SCARG(uap, signum));
    639 		return (0);
    640 	}
    641 	switch (SCARG(uap, pid)) {
    642 	case -1:		/* broadcast signal */
    643 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
    644 	case 0:			/* signal own process group */
    645 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
    646 	default:		/* negative explicit process group */
    647 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
    648 	}
    649 	/* NOTREACHED */
    650 }
    651 
    652 /*
    653  * Common code for kill process group/broadcast kill.
    654  * cp is calling process.
    655  */
    656 int
    657 killpg1(struct proc *cp, int signum, int pgid, int all)
    658 {
    659 	struct proc	*p;
    660 	struct pcred	*pc;
    661 	struct pgrp	*pgrp;
    662 	int		nfound;
    663 
    664 	pc = cp->p_cred;
    665 	nfound = 0;
    666 	if (all) {
    667 		/*
    668 		 * broadcast
    669 		 */
    670 		proclist_lock_read();
    671 		LIST_FOREACH(p, &allproc, p_list) {
    672 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    673 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    674 				continue;
    675 			nfound++;
    676 			if (signum)
    677 				psignal(p, signum);
    678 		}
    679 		proclist_unlock_read();
    680 	} else {
    681 		if (pgid == 0)
    682 			/*
    683 			 * zero pgid means send to my process group.
    684 			 */
    685 			pgrp = cp->p_pgrp;
    686 		else {
    687 			pgrp = pgfind(pgid);
    688 			if (pgrp == NULL)
    689 				return (ESRCH);
    690 		}
    691 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
    692 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    693 			    !CANSIGNAL(cp, pc, p, signum))
    694 				continue;
    695 			nfound++;
    696 			if (signum && P_ZOMBIE(p) == 0)
    697 				psignal(p, signum);
    698 		}
    699 	}
    700 	return (nfound ? 0 : ESRCH);
    701 }
    702 
    703 /*
    704  * Send a signal to a process group.
    705  */
    706 void
    707 gsignal(int pgid, int signum)
    708 {
    709 	struct pgrp *pgrp;
    710 
    711 	if (pgid && (pgrp = pgfind(pgid)))
    712 		pgsignal(pgrp, signum, 0);
    713 }
    714 
    715 /*
    716  * Send a signal to a process group. If checktty is 1,
    717  * limit to members which have a controlling terminal.
    718  */
    719 void
    720 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
    721 {
    722 	struct proc *p;
    723 
    724 	if (pgrp)
    725 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
    726 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    727 				psignal(p, signum);
    728 }
    729 
    730 /*
    731  * Send a signal caused by a trap to the current process.
    732  * If it will be caught immediately, deliver it with correct code.
    733  * Otherwise, post it normally.
    734  */
    735 void
    736 trapsignal(struct lwp *l, int signum, u_long code)
    737 {
    738 	struct proc	*p;
    739 	struct sigacts	*ps;
    740 
    741 	p = l->l_proc;
    742 	ps = p->p_sigacts;
    743 	if ((p->p_flag & P_TRACED) == 0 &&
    744 	    sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
    745 	    !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
    746 		p->p_stats->p_ru.ru_nsignals++;
    747 #ifdef KTRACE
    748 		if (KTRPOINT(p, KTR_PSIG))
    749 			ktrpsig(p, signum,
    750 			    SIGACTION_PS(ps, signum).sa_handler,
    751 			    &p->p_sigctx.ps_sigmask, code);
    752 #endif
    753 		psendsig(l, signum, &p->p_sigctx.ps_sigmask, code);
    754 		(void) splsched();	/* XXXSMP */
    755 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
    756 		    &p->p_sigctx.ps_sigmask);
    757 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
    758 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    759 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    760 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    761 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    762 		}
    763 		(void) spl0();		/* XXXSMP */
    764 	} else {
    765 		p->p_sigctx.ps_code = code;	/* XXX for core dump/debugger */
    766 		p->p_sigctx.ps_sig = signum;	/* XXX to verify code */
    767 		psignal(p, signum);
    768 	}
    769 }
    770 
    771 /*
    772  * Send the signal to the process.  If the signal has an action, the action
    773  * is usually performed by the target process rather than the caller; we add
    774  * the signal to the set of pending signals for the process.
    775  *
    776  * Exceptions:
    777  *   o When a stop signal is sent to a sleeping process that takes the
    778  *     default action, the process is stopped without awakening it.
    779  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    780  *     regardless of the signal action (eg, blocked or ignored).
    781  *
    782  * Other ignored signals are discarded immediately.
    783  *
    784  * XXXSMP: Invoked as psignal() or sched_psignal().
    785  */
    786 void
    787 psignal1(struct proc *p, int signum,
    788 	int dolock)		/* XXXSMP: works, but icky */
    789 {
    790 	struct lwp *l, *suspended;
    791 	int	s = 0, prop, allsusp;
    792 	sig_t	action;
    793 
    794 #ifdef DIAGNOSTIC
    795 	if (signum <= 0 || signum >= NSIG)
    796 		panic("psignal signal number");
    797 
    798 	/* XXXSMP: works, but icky */
    799 	if (dolock)
    800 		SCHED_ASSERT_UNLOCKED();
    801 	else
    802 		SCHED_ASSERT_LOCKED();
    803 #endif
    804 	/*
    805 	 * Notify any interested parties in the signal.
    806 	 */
    807 	KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
    808 
    809 	prop = sigprop[signum];
    810 
    811 	/*
    812 	 * If proc is traced, always give parent a chance.
    813 	 */
    814 	if (p->p_flag & P_TRACED)
    815 		action = SIG_DFL;
    816 	else {
    817 		/*
    818 		 * If the signal is being ignored,
    819 		 * then we forget about it immediately.
    820 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
    821 		 * and if it is set to SIG_IGN,
    822 		 * action will be SIG_DFL here.)
    823 		 */
    824 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
    825 			return;
    826 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
    827 			action = SIG_HOLD;
    828 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
    829 			action = SIG_CATCH;
    830 		else {
    831 			action = SIG_DFL;
    832 
    833 			if (prop & SA_KILL && p->p_nice > NZERO)
    834 				p->p_nice = NZERO;
    835 
    836 			/*
    837 			 * If sending a tty stop signal to a member of an
    838 			 * orphaned process group, discard the signal here if
    839 			 * the action is default; don't stop the process below
    840 			 * if sleeping, and don't clear any pending SIGCONT.
    841 			 */
    842 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
    843 				return;
    844 		}
    845 	}
    846 
    847 	if (prop & SA_CONT)
    848 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
    849 
    850 	if (prop & SA_STOP)
    851 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
    852 
    853 	sigaddset(&p->p_sigctx.ps_siglist, signum);
    854 
    855 	/* CHECKSIGS() is "inlined" here. */
    856 	p->p_sigctx.ps_sigcheck = 1;
    857 
    858 	/*
    859 	 * Defer further processing for signals which are held,
    860 	 * except that stopped processes must be continued by SIGCONT.
    861 	 */
    862 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
    863 		return;
    864 	/* XXXSMP: works, but icky */
    865 	if (dolock)
    866 		SCHED_LOCK(s);
    867 
    868 	if (p->p_nrlwps > 0) {
    869 		/*
    870 		 * At least one LWP is running or on a run queue.
    871 		 * The signal will be noticed when one of them returns
    872 		 * to userspace.
    873 		 */
    874 		signotify(p);
    875 		/*
    876 		 * The signal will be noticed very soon.
    877 		 */
    878 		goto out;
    879 	} else {
    880 		/* Process is sleeping or stopped */
    881 		if (p->p_flag & P_SA) {
    882 			l = p->p_sa->sa_idle;
    883 		} else {
    884 			/*
    885 			 * Find out if any of the sleeps are interruptable,
    886 			 * and if all the live LWPs remaining are suspended.
    887 			 */
    888 			allsusp = 1;
    889 			LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    890 				if (l->l_stat == LSSLEEP &&
    891 				    l->l_flag & L_SINTR)
    892 					break;
    893 				if (l->l_stat == LSSUSPENDED)
    894 					suspended = l;
    895 				else if ((l->l_stat != LSZOMB) &&
    896 				         (l->l_stat != LSDEAD))
    897 					allsusp = 0;
    898 			}
    899 		}
    900 		if (p->p_stat == SACTIVE) {
    901 			/* All LWPs must be sleeping */
    902 			KDASSERT(((p->p_flag & P_SA) == 0) || (l != NULL));
    903 
    904 			if (l != NULL && (p->p_flag & P_TRACED))
    905 				goto run;
    906 
    907 			/*
    908 			 * If SIGCONT is default (or ignored) and process is
    909 			 * asleep, we are finished; the process should not
    910 			 * be awakened.
    911 			 */
    912 			if ((prop & SA_CONT) && action == SIG_DFL) {
    913 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    914 				goto out;
    915 			}
    916 
    917 			/*
    918 			 * When a sleeping process receives a stop
    919 			 * signal, process immediately if possible.
    920 			 */
    921 			if ((prop & SA_STOP) && action == SIG_DFL) {
    922 				/*
    923 				 * If a child holding parent blocked,
    924 				 * stopping could cause deadlock.
    925 				 */
    926 				if (p->p_flag & P_PPWAIT)
    927 					goto out;
    928 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    929 				p->p_xstat = signum;
    930 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
    931 					/*
    932 					 * XXXSMP: recursive call; don't lock
    933 					 * the second time around.
    934 					 */
    935 					sched_psignal(p->p_pptr, SIGCHLD);
    936 				}
    937 				proc_stop(p);	/* XXXSMP: recurse? */
    938 				goto out;
    939 			}
    940 
    941 			if (l == NULL) {
    942 				/*
    943 				 * Special case: SIGKILL of a process
    944 				 * which is entirely composed of
    945 				 * suspended LWPs should succeed. We
    946 				 * make this happen by unsuspending one of
    947 				 * them.
    948 				 */
    949 				if (allsusp && (signum == SIGKILL))
    950 					lwp_continue(suspended);
    951 				goto out;
    952 			}
    953 			/*
    954 			 * All other (caught or default) signals
    955 			 * cause the process to run.
    956 			 */
    957 			goto runfast;
    958 			/*NOTREACHED*/
    959 		} else if (p->p_stat == SSTOP) {
    960 			/* Process is stopped */
    961 			/*
    962 			 * If traced process is already stopped,
    963 			 * then no further action is necessary.
    964 			 */
    965 			if (p->p_flag & P_TRACED)
    966 				goto out;
    967 
    968 			/*
    969 			 * Kill signal always sets processes running,
    970 			 * if possible.
    971 			 */
    972 			if (signum == SIGKILL) {
    973 				l = proc_unstop(p);
    974 				if (l)
    975 					goto runfast;
    976 				/* XXX should this be possible? */
    977 				goto out;
    978 			}
    979 
    980 			if (prop & SA_CONT) {
    981 				/*
    982 				 * If SIGCONT is default (or ignored),
    983 				 * we continue the process but don't
    984 				 * leave the signal in ps_siglist, as
    985 				 * it has no further action.  If
    986 				 * SIGCONT is held, we continue the
    987 				 * process and leave the signal in
    988 				 * ps_siglist.  If the process catches
    989 				 * SIGCONT, let it handle the signal
    990 				 * itself.  If it isn't waiting on an
    991 				 * event, then it goes back to run
    992 				 * state.  Otherwise, process goes
    993 				 * back to sleep state.
    994 				 */
    995 				if (action == SIG_DFL)
    996 					sigdelset(&p->p_sigctx.ps_siglist,
    997 					signum);
    998 				l = proc_unstop(p);
    999 				/*
   1000 				 * XXX see note in proc_unstop(). SIGKILL
   1001 				 * XXX and SIGCONT have conflicting needs.
   1002 				 */
   1003 				if (l && (l->l_stat == LSSLEEP))
   1004 					l = NULL;
   1005 				if (l && (action == SIG_CATCH))
   1006 					goto runfast;
   1007 				if (l)
   1008 					goto run;
   1009 				goto out;
   1010 			}
   1011 
   1012 			if (prop & SA_STOP) {
   1013 				/*
   1014 				 * Already stopped, don't need to stop again.
   1015 				 * (If we did the shell could get confused.)
   1016 				 */
   1017 				sigdelset(&p->p_sigctx.ps_siglist, signum);
   1018 				goto out;
   1019 			}
   1020 
   1021 			/*
   1022 			 * If process is sleeping interruptibly, then
   1023 			 * simulate a wakeup so that when it is
   1024 			 * continued, it will be made runnable and can
   1025 			 * look at the signal.  But don't make the
   1026 			 * process runnable, leave it stopped.
   1027 			 */
   1028 			if (l)
   1029 				unsleep(l);
   1030 			goto out;
   1031 		} else {
   1032 			/* Else what? */
   1033 			panic("psignal: Invalid process state %d.",
   1034 				p->p_stat);
   1035 		}
   1036 	}
   1037 	/*NOTREACHED*/
   1038 
   1039  runfast:
   1040 	/*
   1041 	 * Raise priority to at least PUSER.
   1042 	 */
   1043 	if (l->l_priority > PUSER)
   1044 		l->l_priority = PUSER;
   1045  run:
   1046 	setrunnable(l);		/* XXXSMP: recurse? */
   1047  out:
   1048 	/* XXXSMP: works, but icky */
   1049 	if (dolock)
   1050 		SCHED_UNLOCK(s);
   1051 }
   1052 
   1053 void
   1054 psendsig(struct lwp *l, int sig, sigset_t *mask, u_long code)
   1055 {
   1056 	struct proc *p = l->l_proc;
   1057 	struct lwp *le, *li;
   1058 	siginfo_t *si;
   1059 
   1060 	if (p->p_flag & P_SA) {
   1061 		si = pool_get(&siginfo_pool, PR_WAITOK);
   1062 		si->si_signo = sig;
   1063 		si->si_errno = 0;
   1064 		si->si_code = code;
   1065 		le = li = NULL;
   1066 		if (code)
   1067 			le = l;
   1068 		else
   1069 			li = l;
   1070 
   1071 		sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
   1072 			    sizeof(siginfo_t), si);
   1073 		return;
   1074 	}
   1075 
   1076 	(*p->p_emul->e_sendsig)(sig, mask, code);
   1077 }
   1078 
   1079 static __inline int firstsig(const sigset_t *);
   1080 
   1081 static __inline int
   1082 firstsig(const sigset_t *ss)
   1083 {
   1084 	int sig;
   1085 
   1086 	sig = ffs(ss->__bits[0]);
   1087 	if (sig != 0)
   1088 		return (sig);
   1089 #if NSIG > 33
   1090 	sig = ffs(ss->__bits[1]);
   1091 	if (sig != 0)
   1092 		return (sig + 32);
   1093 #endif
   1094 #if NSIG > 65
   1095 	sig = ffs(ss->__bits[2]);
   1096 	if (sig != 0)
   1097 		return (sig + 64);
   1098 #endif
   1099 #if NSIG > 97
   1100 	sig = ffs(ss->__bits[3]);
   1101 	if (sig != 0)
   1102 		return (sig + 96);
   1103 #endif
   1104 	return (0);
   1105 }
   1106 
   1107 /*
   1108  * If the current process has received a signal (should be caught or cause
   1109  * termination, should interrupt current syscall), return the signal number.
   1110  * Stop signals with default action are processed immediately, then cleared;
   1111  * they aren't returned.  This is checked after each entry to the system for
   1112  * a syscall or trap (though this can usually be done without calling issignal
   1113  * by checking the pending signal masks in the CURSIG macro.) The normal call
   1114  * sequence is
   1115  *
   1116  *	while (signum = CURSIG(curlwp))
   1117  *		postsig(signum);
   1118  */
   1119 int
   1120 issignal(struct lwp *l)
   1121 {
   1122 	struct proc	*p = l->l_proc;
   1123 	int		s = 0, signum, prop;
   1124 	int		dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
   1125 	sigset_t	ss;
   1126 
   1127 	if (p->p_stat == SSTOP) {
   1128 		/*
   1129 		 * The process is stopped/stopping. Stop ourselves now that
   1130 		 * we're on the kernel/userspace boundary.
   1131 		 */
   1132 		if (dolock)
   1133 			SCHED_LOCK(s);
   1134 		l->l_stat = LSSTOP;
   1135 		p->p_nrlwps--;
   1136 		if (p->p_flag & P_TRACED)
   1137 			goto sigtraceswitch;
   1138 		else
   1139 			goto sigswitch;
   1140 	}
   1141 	for (;;) {
   1142 		sigpending1(p, &ss);
   1143 		if (p->p_flag & P_PPWAIT)
   1144 			sigminusset(&stopsigmask, &ss);
   1145 		signum = firstsig(&ss);
   1146 		if (signum == 0) {		 	/* no signal to send */
   1147 			p->p_sigctx.ps_sigcheck = 0;
   1148 			if (locked && dolock)
   1149 				SCHED_LOCK(s);
   1150 			return (0);
   1151 		}
   1152 							/* take the signal! */
   1153 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   1154 
   1155 		/*
   1156 		 * We should see pending but ignored signals
   1157 		 * only if P_TRACED was on when they were posted.
   1158 		 */
   1159 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
   1160 		    (p->p_flag & P_TRACED) == 0)
   1161 			continue;
   1162 
   1163 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1164 			/*
   1165 			 * If traced, always stop, and stay
   1166 			 * stopped until released by the debugger.
   1167 			 */
   1168 			p->p_xstat = signum;
   1169 			if ((p->p_flag & P_FSTRACE) == 0)
   1170 				psignal1(p->p_pptr, SIGCHLD, dolock);
   1171 			if (dolock)
   1172 				SCHED_LOCK(s);
   1173 			proc_stop(p);
   1174 		sigtraceswitch:
   1175 			mi_switch(l, NULL);
   1176 			SCHED_ASSERT_UNLOCKED();
   1177 			if (dolock)
   1178 				splx(s);
   1179 			else
   1180 				dolock = 1;
   1181 
   1182 			/*
   1183 			 * If we are no longer being traced, or the parent
   1184 			 * didn't give us a signal, look for more signals.
   1185 			 */
   1186 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1187 				continue;
   1188 
   1189 			/*
   1190 			 * If the new signal is being masked, look for other
   1191 			 * signals.
   1192 			 */
   1193 			signum = p->p_xstat;
   1194 			p->p_xstat = 0;
   1195 			/*
   1196 			 * `p->p_sigctx.ps_siglist |= mask' is done
   1197 			 * in setrunnable().
   1198 			 */
   1199 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1200 				continue;
   1201 							/* take the signal! */
   1202 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1203 		}
   1204 
   1205 		prop = sigprop[signum];
   1206 
   1207 		/*
   1208 		 * Decide whether the signal should be returned.
   1209 		 * Return the signal's number, or fall through
   1210 		 * to clear it from the pending mask.
   1211 		 */
   1212 		switch ((long)SIGACTION(p, signum).sa_handler) {
   1213 
   1214 		case (long)SIG_DFL:
   1215 			/*
   1216 			 * Don't take default actions on system processes.
   1217 			 */
   1218 			if (p->p_pid <= 1) {
   1219 #ifdef DIAGNOSTIC
   1220 				/*
   1221 				 * Are you sure you want to ignore SIGSEGV
   1222 				 * in init? XXX
   1223 				 */
   1224 				printf("Process (pid %d) got signal %d\n",
   1225 				    p->p_pid, signum);
   1226 #endif
   1227 				break;		/* == ignore */
   1228 			}
   1229 			/*
   1230 			 * If there is a pending stop signal to process
   1231 			 * with default action, stop here,
   1232 			 * then clear the signal.  However,
   1233 			 * if process is member of an orphaned
   1234 			 * process group, ignore tty stop signals.
   1235 			 */
   1236 			if (prop & SA_STOP) {
   1237 				if (p->p_flag & P_TRACED ||
   1238 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1239 				    prop & SA_TTYSTOP))
   1240 					break;	/* == ignore */
   1241 				p->p_xstat = signum;
   1242 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1243 					psignal1(p->p_pptr, SIGCHLD, dolock);
   1244 				if (dolock)
   1245 					SCHED_LOCK(s);
   1246 				proc_stop(p);
   1247 			sigswitch:
   1248 				mi_switch(l, NULL);
   1249 				SCHED_ASSERT_UNLOCKED();
   1250 				if (dolock)
   1251 					splx(s);
   1252 				else
   1253 					dolock = 1;
   1254 				break;
   1255 			} else if (prop & SA_IGNORE) {
   1256 				/*
   1257 				 * Except for SIGCONT, shouldn't get here.
   1258 				 * Default action is to ignore; drop it.
   1259 				 */
   1260 				break;		/* == ignore */
   1261 			} else
   1262 				goto keep;
   1263 			/*NOTREACHED*/
   1264 
   1265 		case (long)SIG_IGN:
   1266 			/*
   1267 			 * Masking above should prevent us ever trying
   1268 			 * to take action on an ignored signal other
   1269 			 * than SIGCONT, unless process is traced.
   1270 			 */
   1271 #ifdef DEBUG_ISSIGNAL
   1272 			if ((prop & SA_CONT) == 0 &&
   1273 			    (p->p_flag & P_TRACED) == 0)
   1274 				printf("issignal\n");
   1275 #endif
   1276 			break;		/* == ignore */
   1277 
   1278 		default:
   1279 			/*
   1280 			 * This signal has an action, let
   1281 			 * postsig() process it.
   1282 			 */
   1283 			goto keep;
   1284 		}
   1285 	}
   1286 	/* NOTREACHED */
   1287 
   1288  keep:
   1289 						/* leave the signal for later */
   1290 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1291 	CHECKSIGS(p);
   1292 	if (locked && dolock)
   1293 		SCHED_LOCK(s);
   1294 	return (signum);
   1295 }
   1296 
   1297 /*
   1298  * Put the argument process into the stopped state and notify the parent
   1299  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1300  * on the run queue.
   1301  */
   1302 static void
   1303 proc_stop(struct proc *p)
   1304 {
   1305 	struct lwp *l;
   1306 
   1307 	SCHED_ASSERT_LOCKED();
   1308 
   1309 	/* XXX lock process LWP state */
   1310 	p->p_stat = SSTOP;
   1311 	p->p_flag &= ~P_WAITED;
   1312 
   1313 	/*
   1314 	 * Put as many LWP's as possible in stopped state.
   1315 	 * Sleeping ones will notice the stopped state as they try to
   1316 	 * return to userspace.
   1317 	 */
   1318 
   1319 	for (l = LIST_FIRST(&p->p_lwps); l != NULL;
   1320 	     l = LIST_NEXT(l, l_sibling)) {
   1321 		if (l->l_stat == LSONPROC) {
   1322 			/* XXX SMP this assumes that a LWP that is LSONPROC
   1323 			 * is curlwp and hence is about to be mi_switched
   1324 			 * away; the only callers of proc_stop() are:
   1325 			 * - psignal
   1326 			 * - issignal()
   1327 			 * For the former, proc_stop() is only called when
   1328 			 * no processes are running, so we don't worry.
   1329 			 * For the latter, proc_stop() is called right
   1330 			 * before mi_switch().
   1331 			 */
   1332 			l->l_stat = LSSTOP;
   1333 			p->p_nrlwps--;
   1334 		} else if (l->l_stat == LSRUN) {
   1335 			/* Remove LWP from the run queue */
   1336 			remrunqueue(l);
   1337 			l->l_stat = LSSTOP;
   1338 			p->p_nrlwps--;
   1339 		} else if ((l->l_stat == LSSLEEP) ||
   1340 		    (l->l_stat == LSSUSPENDED) ||
   1341 		    (l->l_stat == LSZOMB) ||
   1342 		    (l->l_stat == LSDEAD)) {
   1343 			/*
   1344 			 * Don't do anything; let sleeping LWPs
   1345 			 * discover the stopped state of the process
   1346 			 * on their way out of the kernel; otherwise,
   1347 			 * things like NFS threads that sleep with
   1348 			 * locks will block the rest of the system
   1349 			 * from getting any work done.
   1350 			 *
   1351 			 * Suspended/dead/zombie LWPs aren't going
   1352 			 * anywhere, so we don't need to touch them.
   1353 			 */
   1354 		}
   1355 #ifdef DIAGNOSTIC
   1356 		else {
   1357 			panic("proc_stop: process %d lwp %d "
   1358 			      "in unstoppable state %d.\n",
   1359 			    p->p_pid, l->l_lid, l->l_stat);
   1360 		}
   1361 #endif
   1362 	}
   1363 	/* XXX unlock process LWP state */
   1364 
   1365 	sched_wakeup((caddr_t)p->p_pptr);
   1366 }
   1367 
   1368 struct lwp *
   1369 proc_unstop(p)
   1370 	struct proc *p;
   1371 {
   1372 	struct lwp *l, *lr = NULL;
   1373 
   1374 	SCHED_ASSERT_LOCKED();
   1375 
   1376 	/*
   1377 	 * Our caller will want to invoke setrunnable() on whatever we return,
   1378 	 * provided that it isn't NULL.
   1379 	 */
   1380 
   1381 	p->p_stat = SACTIVE;
   1382 	/*
   1383 	 * For the benefit of SIGKILL, return the idle LWP if there's
   1384 	 * nothing better (and if there is an idle LWP, there
   1385 	 * shouldn't be anything better.
   1386 	 * XXX This is bad for SIGCONT; SIGSTOP/SIGCONT shouldn't
   1387 	 * XXX noticably affect the state of a process, idling or
   1388 	 * XXX not. We work around this in the SIGCONT handling in
   1389 	 * XXX psignal().
   1390 	 */
   1391 	if (p->p_flag & P_SA)
   1392 		lr = p->p_sa->sa_idle; /* OK if this is NULL. */
   1393 	for (l = LIST_FIRST(&p->p_lwps); l != NULL;
   1394 	     l = LIST_NEXT(l, l_sibling))
   1395 		if (l->l_stat == LSSTOP) {
   1396 			if (l->l_wchan == 0) {
   1397 				if (lr == NULL || l == lr)
   1398 					lr = l;
   1399 				else
   1400 					setrunnable(l);
   1401 			} else
   1402 				l->l_stat = LSSLEEP;
   1403 		}
   1404 
   1405 	return lr;
   1406 }
   1407 
   1408 /*
   1409  * Take the action for the specified signal
   1410  * from the current set of pending signals.
   1411  */
   1412 void
   1413 postsig(int signum)
   1414 {
   1415 	struct lwp *l;
   1416 	struct proc	*p;
   1417 	struct sigacts	*ps;
   1418 	sig_t		action;
   1419 	u_long		code;
   1420 	sigset_t	*returnmask;
   1421 
   1422 	l = curlwp;
   1423 	p = l->l_proc;
   1424 	ps = p->p_sigacts;
   1425 #ifdef DIAGNOSTIC
   1426 	if (signum == 0)
   1427 		panic("postsig");
   1428 #endif
   1429 
   1430 	KERNEL_PROC_LOCK(l);
   1431 
   1432 	sigdelset(&p->p_sigctx.ps_siglist, signum);
   1433 	action = SIGACTION_PS(ps, signum).sa_handler;
   1434 #ifdef KTRACE
   1435 	if (KTRPOINT(p, KTR_PSIG))
   1436 		ktrpsig(p,
   1437 		    signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1438 		    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
   1439 #endif
   1440 	if (action == SIG_DFL) {
   1441 		/*
   1442 		 * Default action, where the default is to kill
   1443 		 * the process.  (Other cases were ignored above.)
   1444 		 */
   1445 		sigexit(l, signum);
   1446 		/* NOTREACHED */
   1447 	} else {
   1448 		/*
   1449 		 * If we get here, the signal must be caught.
   1450 		 */
   1451 #ifdef DIAGNOSTIC
   1452 		if (action == SIG_IGN ||
   1453 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
   1454 			panic("postsig action");
   1455 #endif
   1456 		/*
   1457 		 * Set the new mask value and also defer further
   1458 		 * occurences of this signal.
   1459 		 *
   1460 		 * Special case: user has done a sigpause.  Here the
   1461 		 * current mask is not of interest, but rather the
   1462 		 * mask from before the sigpause is what we want
   1463 		 * restored after the signal processing is completed.
   1464 		 */
   1465 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
   1466 			returnmask = &p->p_sigctx.ps_oldmask;
   1467 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
   1468 		} else
   1469 			returnmask = &p->p_sigctx.ps_sigmask;
   1470 		p->p_stats->p_ru.ru_nsignals++;
   1471 		if (p->p_sigctx.ps_sig != signum) {
   1472 			code = 0;
   1473 		} else {
   1474 			code = p->p_sigctx.ps_code;
   1475 			p->p_sigctx.ps_code = 0;
   1476 			p->p_sigctx.ps_sig = 0;
   1477 		}
   1478 		psendsig(l, signum, returnmask, code);
   1479 		(void) splsched();	/* XXXSMP */
   1480 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
   1481 		    &p->p_sigctx.ps_sigmask);
   1482 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
   1483 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1484 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1485 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
   1486 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
   1487 		}
   1488 		(void) spl0();		/* XXXSMP */
   1489 	}
   1490 
   1491 	KERNEL_PROC_UNLOCK(l);
   1492 }
   1493 
   1494 /*
   1495  * Kill the current process for stated reason.
   1496  */
   1497 void
   1498 killproc(struct proc *p, const char *why)
   1499 {
   1500 
   1501 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1502 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1503 	psignal(p, SIGKILL);
   1504 }
   1505 
   1506 /*
   1507  * Force the current process to exit with the specified signal, dumping core
   1508  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1509  * allowing unrecoverable failures to terminate the process without changing
   1510  * signal state.  Mark the accounting record with the signal termination.
   1511  * If dumping core, save the signal number for the debugger.  Calls exit and
   1512  * does not return.
   1513  */
   1514 
   1515 #if defined(DEBUG)
   1516 int	kern_logsigexit = 1;	/* not static to make public for sysctl */
   1517 #else
   1518 int	kern_logsigexit = 0;	/* not static to make public for sysctl */
   1519 #endif
   1520 
   1521 static	const char logcoredump[] =
   1522 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
   1523 static	const char lognocoredump[] =
   1524 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
   1525 
   1526 /* Wrapper function for use in p_userret */
   1527 static void
   1528 lwp_coredump_hook(struct lwp *l, void *arg)
   1529 {
   1530 	int s;
   1531 
   1532 	/*
   1533 	 * Suspend ourselves, so that the kernel stack and therefore
   1534 	 * the userland registers saved in the trapframe are around
   1535 	 * for coredump() to write them out.
   1536 	 */
   1537 	KERNEL_PROC_LOCK(l);
   1538 	l->l_flag &= ~L_DETACHED;
   1539 	SCHED_LOCK(s);
   1540 	l->l_stat = LSSUSPENDED;
   1541 	l->l_proc->p_nrlwps--;
   1542 	/* XXX NJWLWP check if this makes sense here: */
   1543 	l->l_proc->p_stats->p_ru.ru_nvcsw++;
   1544 	mi_switch(l, NULL);
   1545 	SCHED_ASSERT_UNLOCKED();
   1546 	splx(s);
   1547 
   1548 	lwp_exit(l);
   1549 }
   1550 
   1551 void
   1552 sigexit(struct lwp *l, int signum)
   1553 {
   1554 	struct proc	*p;
   1555 	int		error, exitsig;
   1556 
   1557 	p = l->l_proc;
   1558 
   1559 	/*
   1560 	 * Don't permit coredump() or exit1() multiple times
   1561 	 * in the same process.
   1562 	 */
   1563 	if (p->p_flag & P_WEXIT)
   1564 		(*p->p_userret)(l, p->p_userret_arg);
   1565 	p->p_flag |= P_WEXIT;
   1566 	/* We don't want to switch away from exiting. */
   1567 	/* XXX multiprocessor: stop LWPs on other processors. */
   1568 	if (l->l_flag & L_SA) {
   1569 		l->l_flag &= ~L_SA;
   1570 		p->p_flag &= ~P_SA;
   1571 	}
   1572 
   1573 	/* Make other LWPs stick around long enough to be dumped */
   1574 	p->p_userret = lwp_coredump_hook;
   1575 	p->p_userret_arg = NULL;
   1576 
   1577 	exitsig = signum;
   1578 	p->p_acflag |= AXSIG;
   1579 	if (sigprop[signum] & SA_CORE) {
   1580 		p->p_sigctx.ps_sig = signum;
   1581 		if ((error = coredump(l)) == 0)
   1582 			exitsig |= WCOREFLAG;
   1583 
   1584 		if (kern_logsigexit) {
   1585 			/* XXX What if we ever have really large UIDs? */
   1586 			int uid = p->p_cred && p->p_ucred ?
   1587 				(int) p->p_ucred->cr_uid : -1;
   1588 
   1589 			if (error)
   1590 				log(LOG_INFO, lognocoredump, p->p_pid,
   1591 				    p->p_comm, uid, signum, error);
   1592 			else
   1593 				log(LOG_INFO, logcoredump, p->p_pid,
   1594 				    p->p_comm, uid, signum);
   1595 		}
   1596 
   1597 	}
   1598 
   1599 	exit1(l, W_EXITCODE(0, exitsig));
   1600 	/* NOTREACHED */
   1601 }
   1602 
   1603 /*
   1604  * Dump core, into a file named "progname.core" or "core" (depending on the
   1605  * value of shortcorename), unless the process was setuid/setgid.
   1606  */
   1607 int
   1608 coredump(struct lwp *l)
   1609 {
   1610 	struct vnode		*vp;
   1611 	struct proc		*p;
   1612 	struct vmspace		*vm;
   1613 	struct ucred		*cred;
   1614 	struct nameidata	nd;
   1615 	struct vattr		vattr;
   1616 	int			error, error1;
   1617 	char			name[MAXPATHLEN];
   1618 
   1619 	p = l->l_proc;
   1620 	vm = p->p_vmspace;
   1621 	cred = p->p_cred->pc_ucred;
   1622 
   1623 	/*
   1624 	 * Make sure the process has not set-id, to prevent data leaks.
   1625 	 */
   1626 	if (p->p_flag & P_SUGID)
   1627 		return (EPERM);
   1628 
   1629 	/*
   1630 	 * Refuse to core if the data + stack + user size is larger than
   1631 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
   1632 	 * data.
   1633 	 */
   1634 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   1635 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   1636 		return (EFBIG);		/* better error code? */
   1637 
   1638 	/*
   1639 	 * The core dump will go in the current working directory.  Make
   1640 	 * sure that the directory is still there and that the mount flags
   1641 	 * allow us to write core dumps there.
   1642 	 */
   1643 	vp = p->p_cwdi->cwdi_cdir;
   1644 	if (vp->v_mount == NULL ||
   1645 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
   1646 		return (EPERM);
   1647 
   1648 	error = build_corename(p, name);
   1649 	if (error)
   1650 		return error;
   1651 
   1652 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
   1653 	error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
   1654 	if (error)
   1655 		return (error);
   1656 	vp = nd.ni_vp;
   1657 
   1658 	/* Don't dump to non-regular files or files with links. */
   1659 	if (vp->v_type != VREG ||
   1660 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
   1661 		error = EINVAL;
   1662 		goto out;
   1663 	}
   1664 	VATTR_NULL(&vattr);
   1665 	vattr.va_size = 0;
   1666 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1667 	VOP_SETATTR(vp, &vattr, cred, p);
   1668 	p->p_acflag |= ACORE;
   1669 
   1670 	/* Now dump the actual core file. */
   1671 	error = (*p->p_execsw->es_coredump)(l, vp, cred);
   1672  out:
   1673 	VOP_UNLOCK(vp, 0);
   1674 	error1 = vn_close(vp, FWRITE, cred, p);
   1675 	if (error == 0)
   1676 		error = error1;
   1677 	return (error);
   1678 }
   1679 
   1680 /*
   1681  * Nonexistent system call-- signal process (may want to handle it).
   1682  * Flag error in case process won't see signal immediately (blocked or ignored).
   1683  */
   1684 /* ARGSUSED */
   1685 int
   1686 sys_nosys(struct lwp *l, void *v, register_t *retval)
   1687 {
   1688 	struct proc 	*p;
   1689 
   1690 	p = l->l_proc;
   1691 	psignal(p, SIGSYS);
   1692 	return (ENOSYS);
   1693 }
   1694 
   1695 static int
   1696 build_corename(struct proc *p, char dst[MAXPATHLEN])
   1697 {
   1698 	const char	*s;
   1699 	char		*d, *end;
   1700 	int		i;
   1701 
   1702 	for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
   1703 	    *s != '\0'; s++) {
   1704 		if (*s == '%') {
   1705 			switch (*(s + 1)) {
   1706 			case 'n':
   1707 				i = snprintf(d, end - d, "%s", p->p_comm);
   1708 				break;
   1709 			case 'p':
   1710 				i = snprintf(d, end - d, "%d", p->p_pid);
   1711 				break;
   1712 			case 'u':
   1713 				i = snprintf(d, end - d, "%s",
   1714 				    p->p_pgrp->pg_session->s_login);
   1715 				break;
   1716 			case 't':
   1717 				i = snprintf(d, end - d, "%ld",
   1718 				    p->p_stats->p_start.tv_sec);
   1719 				break;
   1720 			default:
   1721 				goto copy;
   1722 			}
   1723 			d += i;
   1724 			s++;
   1725 		} else {
   1726  copy:			*d = *s;
   1727 			d++;
   1728 		}
   1729 		if (d >= end)
   1730 			return (ENAMETOOLONG);
   1731 	}
   1732 	*d = '\0';
   1733 	return 0;
   1734 }
   1735 
   1736 void
   1737 getucontext(struct lwp *l, ucontext_t *ucp)
   1738 {
   1739 	struct proc	*p;
   1740 
   1741 	p = l->l_proc;
   1742 
   1743 	ucp->uc_flags = 0;
   1744 	ucp->uc_link = l->l_ctxlink;
   1745 
   1746 	(void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
   1747 	ucp->uc_flags |= _UC_SIGMASK;
   1748 
   1749 	/*
   1750 	 * The (unsupplied) definition of the `current execution stack'
   1751 	 * in the System V Interface Definition appears to allow returning
   1752 	 * the main context stack.
   1753 	 */
   1754 	if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
   1755 		ucp->uc_stack.ss_sp = (void *)USRSTACK;
   1756 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
   1757 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
   1758 	} else {
   1759 		/* Simply copy alternate signal execution stack. */
   1760 		ucp->uc_stack = p->p_sigctx.ps_sigstk;
   1761 	}
   1762 	ucp->uc_flags |= _UC_STACK;
   1763 
   1764 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
   1765 }
   1766 
   1767 /* ARGSUSED */
   1768 int
   1769 sys_getcontext(struct lwp *l, void *v, register_t *retval)
   1770 {
   1771 	struct sys_getcontext_args /* {
   1772 		syscallarg(struct __ucontext *) ucp;
   1773 	} */ *uap = v;
   1774 	ucontext_t uc;
   1775 
   1776 	getucontext(l, &uc);
   1777 
   1778 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
   1779 }
   1780 
   1781 int
   1782 setucontext(struct lwp *l, const ucontext_t *ucp)
   1783 {
   1784 	struct proc	*p;
   1785 	int		error;
   1786 
   1787 	p = l->l_proc;
   1788 	if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
   1789 		return (error);
   1790 	l->l_ctxlink = ucp->uc_link;
   1791 	/*
   1792 	 * We might want to take care of the stack portion here but currently
   1793 	 * don't; see the comment in getucontext().
   1794 	 */
   1795 	if ((ucp->uc_flags & _UC_SIGMASK) != 0)
   1796 		sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
   1797 
   1798 	return 0;
   1799 }
   1800 
   1801 /* ARGSUSED */
   1802 int
   1803 sys_setcontext(struct lwp *l, void *v, register_t *retval)
   1804 {
   1805 	struct sys_setcontext_args /* {
   1806 		syscallarg(const ucontext_t *) ucp;
   1807 	} */ *uap = v;
   1808 	ucontext_t uc;
   1809 	int error;
   1810 
   1811 	if (SCARG(uap, ucp) == NULL)	/* i.e. end of uc_link chain */
   1812 		exit1(l, W_EXITCODE(0, 0));
   1813 	else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
   1814 	    (error = setucontext(l, &uc)) != 0)
   1815 		return (error);
   1816 
   1817 	return (EJUSTRETURN);
   1818 }
   1819 
   1820 
   1821 /*
   1822  * Returns true if signal is ignored or masked for passed process.
   1823  */
   1824 int
   1825 sigismasked(struct proc *p, int sig)
   1826 {
   1827 
   1828 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
   1829 	    sigismember(&p->p_sigctx.ps_sigmask, sig));
   1830 }
   1831 
   1832 static int
   1833 filt_sigattach(struct knote *kn)
   1834 {
   1835 	struct proc *p = curproc;
   1836 
   1837 	kn->kn_ptr.p_proc = p;
   1838 	kn->kn_flags |= EV_CLEAR;               /* automatically set */
   1839 
   1840 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
   1841 
   1842 	return (0);
   1843 }
   1844 
   1845 static void
   1846 filt_sigdetach(struct knote *kn)
   1847 {
   1848 	struct proc *p = kn->kn_ptr.p_proc;
   1849 
   1850 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
   1851 }
   1852 
   1853 /*
   1854  * signal knotes are shared with proc knotes, so we apply a mask to
   1855  * the hint in order to differentiate them from process hints.  This
   1856  * could be avoided by using a signal-specific knote list, but probably
   1857  * isn't worth the trouble.
   1858  */
   1859 static int
   1860 filt_signal(struct knote *kn, long hint)
   1861 {
   1862 
   1863 	if (hint & NOTE_SIGNAL) {
   1864 		hint &= ~NOTE_SIGNAL;
   1865 
   1866 		if (kn->kn_id == hint)
   1867 			kn->kn_data++;
   1868 	}
   1869 	return (kn->kn_data != 0);
   1870 }
   1871 
   1872 const struct filterops sig_filtops = {
   1873 	0, filt_sigattach, filt_sigdetach, filt_signal
   1874 };
   1875