Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.115
      1 /*	$NetBSD: kern_sig.c,v 1.115 2001/07/18 05:34:58 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. 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 "opt_ktrace.h"
     44 #include "opt_compat_sunos.h"
     45 #include "opt_compat_netbsd32.h"
     46 
     47 #define	SIGPROP		/* include signal properties table */
     48 #include <sys/param.h>
     49 #include <sys/signalvar.h>
     50 #include <sys/resourcevar.h>
     51 #include <sys/namei.h>
     52 #include <sys/vnode.h>
     53 #include <sys/proc.h>
     54 #include <sys/systm.h>
     55 #include <sys/timeb.h>
     56 #include <sys/times.h>
     57 #include <sys/buf.h>
     58 #include <sys/acct.h>
     59 #include <sys/file.h>
     60 #include <sys/kernel.h>
     61 #include <sys/wait.h>
     62 #include <sys/ktrace.h>
     63 #include <sys/syslog.h>
     64 #include <sys/stat.h>
     65 #include <sys/core.h>
     66 #include <sys/filedesc.h>
     67 #include <sys/malloc.h>
     68 #include <sys/pool.h>
     69 
     70 #include <sys/mount.h>
     71 #include <sys/syscallargs.h>
     72 
     73 #include <machine/cpu.h>
     74 
     75 #include <sys/user.h>		/* for coredump */
     76 
     77 #include <uvm/uvm_extern.h>
     78 
     79 static void	proc_stop(struct proc *p);
     80 void		killproc(struct proc *, char *);
     81 static int	build_corename(struct proc *, char [MAXPATHLEN]);
     82 sigset_t	contsigmask, stopsigmask, sigcantmask;
     83 
     84 struct pool	sigacts_pool;	/* memory pool for sigacts structures */
     85 
     86 int	(*coredump32_hook)(struct proc *p, struct vnode *vp);
     87 
     88 /*
     89  * Can process p, with pcred pc, send the signal signum to process q?
     90  */
     91 #define	CANSIGNAL(p, pc, q, signum) \
     92 	((pc)->pc_ucred->cr_uid == 0 || \
     93 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
     94 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
     95 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
     96 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
     97 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
     98 
     99 /*
    100  * Initialize signal-related data structures.
    101  */
    102 void
    103 signal_init(void)
    104 {
    105 
    106 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    107 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    108 }
    109 
    110 /*
    111  * Create an initial sigctx structure, using the same signal state
    112  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
    113  * copy it from parent.
    114  */
    115 void
    116 sigactsinit(struct proc *np, struct proc *pp, int share)
    117 {
    118 	struct sigacts *ps;
    119 
    120 	if (share) {
    121 		np->p_sigacts = pp->p_sigacts;
    122 		pp->p_sigacts->sa_refcnt++;
    123 	} else {
    124 		ps = pool_get(&sigacts_pool, PR_WAITOK);
    125 		if (pp)
    126 			memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
    127 		else
    128 			memset(ps, '\0', sizeof(struct sigacts));
    129 		ps->sa_refcnt = 1;
    130 		np->p_sigacts = ps;
    131 	}
    132 }
    133 
    134 /*
    135  * Make this process not share its sigctx, maintaining all
    136  * signal state.
    137  */
    138 void
    139 sigactsunshare(struct proc *p)
    140 {
    141 	struct sigacts *oldps;
    142 
    143 	if (p->p_sigacts->sa_refcnt == 1)
    144 		return;
    145 
    146 	oldps = p->p_sigacts;
    147 	sigactsinit(p, NULL, 0);
    148 
    149 	if (--oldps->sa_refcnt == 0)
    150 		pool_put(&sigacts_pool, oldps);
    151 }
    152 
    153 /*
    154  * Release a sigctx structure.
    155  */
    156 void
    157 sigactsfree(struct proc *p)
    158 {
    159 	struct sigacts *ps;
    160 
    161 	ps = p->p_sigacts;
    162 	if (--ps->sa_refcnt > 0)
    163 		return;
    164 
    165 	pool_put(&sigacts_pool, ps);
    166 }
    167 
    168 int
    169 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
    170 	struct sigaction *osa)
    171 {
    172 	struct sigacts	*ps;
    173 	int		prop;
    174 
    175 	ps = p->p_sigacts;
    176 	if (signum <= 0 || signum >= NSIG)
    177 		return (EINVAL);
    178 
    179 	if (osa)
    180 		*osa = SIGACTION_PS(ps, signum);
    181 
    182 	if (nsa) {
    183 		if (nsa->sa_flags & ~SA_ALLBITS)
    184 			return (EINVAL);
    185 
    186 		prop = sigprop[signum];
    187 		if (prop & SA_CANTMASK)
    188 			return (EINVAL);
    189 
    190 		(void) splsched();	/* XXXSMP */
    191 		SIGACTION_PS(ps, signum) = *nsa;
    192 		sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    193 		if ((prop & SA_NORESET) != 0)
    194 			SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    195 		if (signum == SIGCHLD) {
    196 			if (nsa->sa_flags & SA_NOCLDSTOP)
    197 				p->p_flag |= P_NOCLDSTOP;
    198 			else
    199 				p->p_flag &= ~P_NOCLDSTOP;
    200 			if (nsa->sa_flags & SA_NOCLDWAIT) {
    201 				/*
    202 				 * Paranoia: since SA_NOCLDWAIT is implemented
    203 				 * by reparenting the dying child to PID 1 (and
    204 				 * trust it to reap the zombie), PID 1 itself
    205 				 * is forbidden to set SA_NOCLDWAIT.
    206 				 */
    207 				if (p->p_pid == 1)
    208 					p->p_flag &= ~P_NOCLDWAIT;
    209 				else
    210 					p->p_flag |= P_NOCLDWAIT;
    211 			} else
    212 				p->p_flag &= ~P_NOCLDWAIT;
    213 		}
    214 		if ((nsa->sa_flags & SA_NODEFER) == 0)
    215 			sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    216 		else
    217 			sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    218 		/*
    219 	 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    220 		 * SIG_IGN, and for signals set to SIG_DFL where the default is
    221 		 * to ignore. However, don't put SIGCONT in
    222 		 * p_sigctx.ps_sigignore, as we have to restart the process.
    223 	 	 */
    224 		if (nsa->sa_handler == SIG_IGN ||
    225 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    226 						/* never to be seen again */
    227 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    228 			if (signum != SIGCONT) {
    229 						/* easier in psignal */
    230 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    231 			}
    232 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    233 		} else {
    234 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
    235 			if (nsa->sa_handler == SIG_DFL)
    236 				sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    237 			else
    238 				sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    239 		}
    240 		(void) spl0();
    241 	}
    242 
    243 	return (0);
    244 }
    245 
    246 /* ARGSUSED */
    247 int
    248 sys___sigaction14(struct proc *p, void *v, register_t *retval)
    249 {
    250 	struct sys___sigaction14_args /* {
    251 		syscallarg(int)				signum;
    252 		syscallarg(const struct sigaction *)	nsa;
    253 		syscallarg(struct sigaction *)		osa;
    254 	} */ *uap = v;
    255 	struct sigaction	nsa, osa;
    256 	int			error;
    257 
    258 	if (SCARG(uap, nsa)) {
    259 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    260 		if (error)
    261 			return (error);
    262 	}
    263 	error = sigaction1(p, SCARG(uap, signum),
    264 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0);
    265 	if (error)
    266 		return (error);
    267 	if (SCARG(uap, osa)) {
    268 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    269 		if (error)
    270 			return (error);
    271 	}
    272 	return (0);
    273 }
    274 
    275 /*
    276  * Initialize signal state for process 0;
    277  * set to ignore signals that are ignored by default and disable the signal
    278  * stack.
    279  */
    280 void
    281 siginit(struct proc *p)
    282 {
    283 	struct sigacts	*ps;
    284 	int		signum, prop;
    285 
    286 	ps = p->p_sigacts;
    287 	sigemptyset(&contsigmask);
    288 	sigemptyset(&stopsigmask);
    289 	sigemptyset(&sigcantmask);
    290 	for (signum = 1; signum < NSIG; signum++) {
    291 		prop = sigprop[signum];
    292 		if (prop & SA_CONT)
    293 			sigaddset(&contsigmask, signum);
    294 		if (prop & SA_STOP)
    295 			sigaddset(&stopsigmask, signum);
    296 		if (prop & SA_CANTMASK)
    297 			sigaddset(&sigcantmask, signum);
    298 		if (prop & SA_IGNORE && signum != SIGCONT)
    299 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    300 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    301 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    302 	}
    303 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    304 	p->p_flag &= ~P_NOCLDSTOP;
    305 
    306 	/*
    307 	 * Reset stack state to the user stack.
    308 	 */
    309 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    310 	p->p_sigctx.ps_sigstk.ss_size = 0;
    311 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    312 
    313 	/* One reference. */
    314 	ps->sa_refcnt = 1;
    315 }
    316 
    317 /*
    318  * Reset signals for an exec of the specified process.
    319  */
    320 void
    321 execsigs(struct proc *p)
    322 {
    323 	struct sigacts	*ps;
    324 	int		signum, prop;
    325 
    326 	sigactsunshare(p);
    327 
    328 	ps = p->p_sigacts;
    329 
    330 	/*
    331 	 * Reset caught signals.  Held signals remain held
    332 	 * through p_sigctx.ps_sigmask (unless they were caught,
    333 	 * and are now ignored by default).
    334 	 */
    335 	for (signum = 1; signum < NSIG; signum++) {
    336 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
    337 			prop = sigprop[signum];
    338 			if (prop & SA_IGNORE) {
    339 				if ((prop & SA_CONT) == 0)
    340 					sigaddset(&p->p_sigctx.ps_sigignore,
    341 					    signum);
    342 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    343 			}
    344 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    345 		}
    346 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    347 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    348 	}
    349 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    350 	p->p_flag &= ~P_NOCLDSTOP;
    351 
    352 	/*
    353 	 * Reset stack state to the user stack.
    354 	 */
    355 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    356 	p->p_sigctx.ps_sigstk.ss_size = 0;
    357 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    358 }
    359 
    360 int
    361 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
    362 {
    363 
    364 	if (oss)
    365 		*oss = p->p_sigctx.ps_sigmask;
    366 
    367 	if (nss) {
    368 		(void)splsched();	/* XXXSMP */
    369 		switch (how) {
    370 		case SIG_BLOCK:
    371 			sigplusset(nss, &p->p_sigctx.ps_sigmask);
    372 			break;
    373 		case SIG_UNBLOCK:
    374 			sigminusset(nss, &p->p_sigctx.ps_sigmask);
    375 			CHECKSIGS(p);
    376 			break;
    377 		case SIG_SETMASK:
    378 			p->p_sigctx.ps_sigmask = *nss;
    379 			CHECKSIGS(p);
    380 			break;
    381 		default:
    382 			(void)spl0();	/* XXXSMP */
    383 			return (EINVAL);
    384 		}
    385 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    386 		(void)spl0();		/* XXXSMP */
    387 	}
    388 
    389 	return (0);
    390 }
    391 
    392 /*
    393  * Manipulate signal mask.
    394  * Note that we receive new mask, not pointer,
    395  * and return old mask as return value;
    396  * the library stub does the rest.
    397  */
    398 int
    399 sys___sigprocmask14(struct proc *p, void *v, register_t *retval)
    400 {
    401 	struct sys___sigprocmask14_args /* {
    402 		syscallarg(int)			how;
    403 		syscallarg(const sigset_t *)	set;
    404 		syscallarg(sigset_t *)		oset;
    405 	} */ *uap = v;
    406 	sigset_t	nss, oss;
    407 	int		error;
    408 
    409 	if (SCARG(uap, set)) {
    410 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    411 		if (error)
    412 			return (error);
    413 	}
    414 	error = sigprocmask1(p, SCARG(uap, how),
    415 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    416 	if (error)
    417 		return (error);
    418 	if (SCARG(uap, oset)) {
    419 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    420 		if (error)
    421 			return (error);
    422 	}
    423 	return (0);
    424 }
    425 
    426 void
    427 sigpending1(struct proc *p, sigset_t *ss)
    428 {
    429 
    430 	*ss = p->p_sigctx.ps_siglist;
    431 	sigminusset(&p->p_sigctx.ps_sigmask, ss);
    432 }
    433 
    434 /* ARGSUSED */
    435 int
    436 sys___sigpending14(struct proc *p, void *v, register_t *retval)
    437 {
    438 	struct sys___sigpending14_args /* {
    439 		syscallarg(sigset_t *)	set;
    440 	} */ *uap = v;
    441 	sigset_t ss;
    442 
    443 	sigpending1(p, &ss);
    444 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    445 }
    446 
    447 int
    448 sigsuspend1(struct proc *p, const sigset_t *ss)
    449 {
    450 	struct sigacts *ps;
    451 
    452 	ps = p->p_sigacts;
    453 	if (ss) {
    454 		/*
    455 		 * When returning from sigpause, we want
    456 		 * the old mask to be restored after the
    457 		 * signal handler has finished.  Thus, we
    458 		 * save it here and mark the sigctx structure
    459 		 * to indicate this.
    460 		 */
    461 		p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
    462 		p->p_sigctx.ps_flags |= SAS_OLDMASK;
    463 		(void) splsched();	/* XXXSMP */
    464 		p->p_sigctx.ps_sigmask = *ss;
    465 		CHECKSIGS(p);
    466 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    467 		(void) spl0();		/* XXXSMP */
    468 	}
    469 
    470 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    471 		/* void */;
    472 	/* always return EINTR rather than ERESTART... */
    473 	return (EINTR);
    474 }
    475 
    476 /*
    477  * Suspend process until signal, providing mask to be set
    478  * in the meantime.  Note nonstandard calling convention:
    479  * libc stub passes mask, not pointer, to save a copyin.
    480  */
    481 /* ARGSUSED */
    482 int
    483 sys___sigsuspend14(struct proc *p, void *v, register_t *retval)
    484 {
    485 	struct sys___sigsuspend14_args /* {
    486 		syscallarg(const sigset_t *)	set;
    487 	} */ *uap = v;
    488 	sigset_t	ss;
    489 	int		error;
    490 
    491 	if (SCARG(uap, set)) {
    492 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    493 		if (error)
    494 			return (error);
    495 	}
    496 
    497 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
    498 }
    499 
    500 int
    501 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
    502 	struct sigaltstack *oss)
    503 {
    504 
    505 	if (oss)
    506 		*oss = p->p_sigctx.ps_sigstk;
    507 
    508 	if (nss) {
    509 		if (nss->ss_flags & ~SS_ALLBITS)
    510 			return (EINVAL);
    511 
    512 		if (nss->ss_flags & SS_DISABLE) {
    513 			if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    514 				return (EINVAL);
    515 		} else {
    516 			if (nss->ss_size < MINSIGSTKSZ)
    517 				return (ENOMEM);
    518 		}
    519 		p->p_sigctx.ps_sigstk = *nss;
    520 	}
    521 
    522 	return (0);
    523 }
    524 
    525 /* ARGSUSED */
    526 int
    527 sys___sigaltstack14(struct proc *p, void *v, register_t *retval)
    528 {
    529 	struct sys___sigaltstack14_args /* {
    530 		syscallarg(const struct sigaltstack *)	nss;
    531 		syscallarg(struct sigaltstack *)	oss;
    532 	} */ *uap = v;
    533 	struct sigaltstack	nss, oss;
    534 	int			error;
    535 
    536 	if (SCARG(uap, nss)) {
    537 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    538 		if (error)
    539 			return (error);
    540 	}
    541 	error = sigaltstack1(p,
    542 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    543 	if (error)
    544 		return (error);
    545 	if (SCARG(uap, oss)) {
    546 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    547 		if (error)
    548 			return (error);
    549 	}
    550 	return (0);
    551 }
    552 
    553 /* ARGSUSED */
    554 int
    555 sys_kill(struct proc *cp, void *v, register_t *retval)
    556 {
    557 	struct sys_kill_args /* {
    558 		syscallarg(int)	pid;
    559 		syscallarg(int)	signum;
    560 	} */ *uap = v;
    561 	struct proc	*p;
    562 	struct pcred	*pc;
    563 
    564 	pc = cp->p_cred;
    565 	if ((u_int)SCARG(uap, signum) >= NSIG)
    566 		return (EINVAL);
    567 	if (SCARG(uap, pid) > 0) {
    568 		/* kill single process */
    569 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    570 			return (ESRCH);
    571 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    572 			return (EPERM);
    573 		if (SCARG(uap, signum))
    574 			psignal(p, SCARG(uap, signum));
    575 		return (0);
    576 	}
    577 	switch (SCARG(uap, pid)) {
    578 	case -1:		/* broadcast signal */
    579 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
    580 	case 0:			/* signal own process group */
    581 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
    582 	default:		/* negative explicit process group */
    583 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
    584 	}
    585 	/* NOTREACHED */
    586 }
    587 
    588 /*
    589  * Common code for kill process group/broadcast kill.
    590  * cp is calling process.
    591  */
    592 int
    593 killpg1(struct proc *cp, int signum, int pgid, int all)
    594 {
    595 	struct proc	*p;
    596 	struct pcred	*pc;
    597 	struct pgrp	*pgrp;
    598 	int		nfound;
    599 
    600 	pc = cp->p_cred;
    601 	nfound = 0;
    602 	if (all) {
    603 		/*
    604 		 * broadcast
    605 		 */
    606 		proclist_lock_read();
    607 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    608 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    609 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    610 				continue;
    611 			nfound++;
    612 			if (signum)
    613 				psignal(p, signum);
    614 		}
    615 		proclist_unlock_read();
    616 	} else {
    617 		if (pgid == 0)
    618 			/*
    619 			 * zero pgid means send to my process group.
    620 			 */
    621 			pgrp = cp->p_pgrp;
    622 		else {
    623 			pgrp = pgfind(pgid);
    624 			if (pgrp == NULL)
    625 				return (ESRCH);
    626 		}
    627 		for (p = pgrp->pg_members.lh_first;
    628 		    p != 0;
    629 		    p = p->p_pglist.le_next) {
    630 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    631 			    !CANSIGNAL(cp, pc, p, signum))
    632 				continue;
    633 			nfound++;
    634 			if (signum && P_ZOMBIE(p) == 0)
    635 				psignal(p, signum);
    636 		}
    637 	}
    638 	return (nfound ? 0 : ESRCH);
    639 }
    640 
    641 /*
    642  * Send a signal to a process group.
    643  */
    644 void
    645 gsignal(int pgid, int signum)
    646 {
    647 	struct pgrp *pgrp;
    648 
    649 	if (pgid && (pgrp = pgfind(pgid)))
    650 		pgsignal(pgrp, signum, 0);
    651 }
    652 
    653 /*
    654  * Send a signal to a process group. If checktty is 1,
    655  * limit to members which have a controlling terminal.
    656  */
    657 void
    658 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
    659 {
    660 	struct proc *p;
    661 
    662 	if (pgrp)
    663 		for (p = pgrp->pg_members.lh_first; p != 0;
    664 		    p = p->p_pglist.le_next)
    665 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    666 				psignal(p, signum);
    667 }
    668 
    669 /*
    670  * Send a signal caused by a trap to the current process.
    671  * If it will be caught immediately, deliver it with correct code.
    672  * Otherwise, post it normally.
    673  */
    674 void
    675 trapsignal(struct proc *p, int signum, u_long code)
    676 {
    677 	struct sigacts *ps;
    678 
    679 	ps = p->p_sigacts;
    680 	if ((p->p_flag & P_TRACED) == 0 &&
    681 	    sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
    682 	    !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
    683 		p->p_stats->p_ru.ru_nsignals++;
    684 #ifdef KTRACE
    685 		if (KTRPOINT(p, KTR_PSIG))
    686 			ktrpsig(p, signum,
    687 			    SIGACTION_PS(ps, signum).sa_handler,
    688 			    &p->p_sigctx.ps_sigmask, code);
    689 #endif
    690 		(*p->p_emul->e_sendsig)(SIGACTION_PS(ps, signum).sa_handler,
    691 		    signum, &p->p_sigctx.ps_sigmask, code);
    692 		(void) splsched();	/* XXXSMP */
    693 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
    694 		    &p->p_sigctx.ps_sigmask);
    695 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
    696 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    697 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    698 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    699 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    700 		}
    701 		(void) spl0();		/* XXXSMP */
    702 	} else {
    703 		p->p_sigctx.ps_code = code;	/* XXX for core dump/debugger */
    704 		p->p_sigctx.ps_sig = signum;	/* XXX to verify code */
    705 		psignal(p, signum);
    706 	}
    707 }
    708 
    709 /*
    710  * Send the signal to the process.  If the signal has an action, the action
    711  * is usually performed by the target process rather than the caller; we add
    712  * the signal to the set of pending signals for the process.
    713  *
    714  * Exceptions:
    715  *   o When a stop signal is sent to a sleeping process that takes the
    716  *     default action, the process is stopped without awakening it.
    717  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    718  *     regardless of the signal action (eg, blocked or ignored).
    719  *
    720  * Other ignored signals are discarded immediately.
    721  *
    722  * XXXSMP: Invoked as psignal() or sched_psignal().
    723  */
    724 void
    725 psignal1(struct proc *p, int signum,
    726 	int dolock)		/* XXXSMP: works, but icky */
    727 {
    728 	int	s, prop;
    729 	sig_t	action;
    730 
    731 #ifdef DIAGNOSTIC
    732 	if (signum <= 0 || signum >= NSIG)
    733 		panic("psignal signal number");
    734 
    735 	/* XXXSMP: works, but icky */
    736 	if (dolock)
    737 		SCHED_ASSERT_UNLOCKED();
    738 	else
    739 		SCHED_ASSERT_LOCKED();
    740 #endif
    741 	prop = sigprop[signum];
    742 
    743 	/*
    744 	 * If proc is traced, always give parent a chance.
    745 	 */
    746 	if (p->p_flag & P_TRACED)
    747 		action = SIG_DFL;
    748 	else {
    749 		/*
    750 		 * If the signal is being ignored,
    751 		 * then we forget about it immediately.
    752 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
    753 		 * and if it is set to SIG_IGN,
    754 		 * action will be SIG_DFL here.)
    755 		 */
    756 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
    757 			return;
    758 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
    759 			action = SIG_HOLD;
    760 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
    761 			action = SIG_CATCH;
    762 		else {
    763 			action = SIG_DFL;
    764 
    765 			if (prop & SA_KILL && p->p_nice > NZERO)
    766 				p->p_nice = NZERO;
    767 
    768 			/*
    769 			 * If sending a tty stop signal to a member of an
    770 			 * orphaned process group, discard the signal here if
    771 			 * the action is default; don't stop the process below
    772 			 * if sleeping, and don't clear any pending SIGCONT.
    773 			 */
    774 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
    775 				return;
    776 		}
    777 	}
    778 
    779 	if (prop & SA_CONT)
    780 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
    781 
    782 	if (prop & SA_STOP)
    783 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
    784 
    785 	sigaddset(&p->p_sigctx.ps_siglist, signum);
    786 
    787 	/* CHECKSIGS() is "inlined" here. */
    788 	p->p_sigctx.ps_sigcheck = 1;
    789 
    790 	/*
    791 	 * Defer further processing for signals which are held,
    792 	 * except that stopped processes must be continued by SIGCONT.
    793 	 */
    794 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
    795 		return;
    796 
    797 	/* XXXSMP: works, but icky */
    798 	if (dolock)
    799 		SCHED_LOCK(s);
    800 
    801 	switch (p->p_stat) {
    802 	case SSLEEP:
    803 		/*
    804 		 * If process is sleeping uninterruptibly
    805 		 * we can't interrupt the sleep... the signal will
    806 		 * be noticed when the process returns through
    807 		 * trap() or syscall().
    808 		 */
    809 		if ((p->p_flag & P_SINTR) == 0)
    810 			goto out;
    811 		/*
    812 		 * Process is sleeping and traced... make it runnable
    813 		 * so it can discover the signal in issignal() and stop
    814 		 * for the parent.
    815 		 */
    816 		if (p->p_flag & P_TRACED)
    817 			goto run;
    818 		/*
    819 		 * If SIGCONT is default (or ignored) and process is
    820 		 * asleep, we are finished; the process should not
    821 		 * be awakened.
    822 		 */
    823 		if ((prop & SA_CONT) && action == SIG_DFL) {
    824 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    825 			goto out;
    826 		}
    827 		/*
    828 		 * When a sleeping process receives a stop
    829 		 * signal, process immediately if possible.
    830 		 */
    831 		if ((prop & SA_STOP) && action == SIG_DFL) {
    832 			/*
    833 			 * If a child holding parent blocked,
    834 			 * stopping could cause deadlock.
    835 			 */
    836 			if (p->p_flag & P_PPWAIT)
    837 				goto out;
    838 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    839 			p->p_xstat = signum;
    840 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
    841 				/*
    842 				 * XXXSMP: recursive call; don't lock
    843 				 * the second time around.
    844 				 */
    845 				sched_psignal(p->p_pptr, SIGCHLD);
    846 			}
    847 			proc_stop(p);	/* XXXSMP: recurse? */
    848 			goto out;
    849 		}
    850 		/*
    851 		 * All other (caught or default) signals
    852 		 * cause the process to run.
    853 		 */
    854 		goto runfast;
    855 		/*NOTREACHED*/
    856 
    857 	case SSTOP:
    858 		/*
    859 		 * If traced process is already stopped,
    860 		 * then no further action is necessary.
    861 		 */
    862 		if (p->p_flag & P_TRACED)
    863 			goto out;
    864 
    865 		/*
    866 		 * Kill signal always sets processes running.
    867 		 */
    868 		if (signum == SIGKILL)
    869 			goto runfast;
    870 
    871 		if (prop & SA_CONT) {
    872 			/*
    873 			 * If SIGCONT is default (or ignored), we continue the
    874 			 * process but don't leave the signal in p_sigctx.ps_siglist, as
    875 			 * it has no further action.  If SIGCONT is held, we
    876 			 * continue the process and leave the signal in
    877 			 * p_sigctx.ps_siglist.  If the process catches SIGCONT, let it
    878 			 * handle the signal itself.  If it isn't waiting on
    879 			 * an event, then it goes back to run state.
    880 			 * Otherwise, process goes back to sleep state.
    881 			 */
    882 			if (action == SIG_DFL)
    883 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    884 			if (action == SIG_CATCH)
    885 				goto runfast;
    886 			if (p->p_wchan == 0)
    887 				goto run;
    888 			p->p_stat = SSLEEP;
    889 			goto out;
    890 		}
    891 
    892 		if (prop & SA_STOP) {
    893 			/*
    894 			 * Already stopped, don't need to stop again.
    895 			 * (If we did the shell could get confused.)
    896 			 */
    897 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    898 			goto out;
    899 		}
    900 
    901 		/*
    902 		 * If process is sleeping interruptibly, then simulate a
    903 		 * wakeup so that when it is continued, it will be made
    904 		 * runnable and can look at the signal.  But don't make
    905 		 * the process runnable, leave it stopped.
    906 		 */
    907 		if (p->p_wchan && p->p_flag & P_SINTR)
    908 			unsleep(p);
    909 		goto out;
    910 #ifdef __HAVE_AST_PERPROC
    911 	case SONPROC:
    912 	case SRUN:
    913 	case SIDL:
    914 		/*
    915 		 * SONPROC: We're running, notice the signal when
    916 		 * we return back to userspace.
    917 		 *
    918 		 * SRUN, SIDL: Notice the signal when we run again
    919 		 * and return to back to userspace.
    920 		 */
    921 		signotify(p);
    922 		goto out;
    923 
    924 	default:
    925 		/*
    926 		 * SDEAD, SZOMB: The signal will never be noticed.
    927 		 */
    928 		goto out;
    929 #else /* ! __HAVE_AST_PERPROC */
    930 	case SONPROC:
    931 		/*
    932 		 * We're running; notice the signal.
    933 		 */
    934 		signotify(p);
    935 		goto out;
    936 
    937 	default:
    938 		/*
    939 		 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal.
    940 		 * It will either never be noticed, or noticed very soon.
    941 		 */
    942 		goto out;
    943 #endif /* __HAVE_AST_PERPROC */
    944 	}
    945 	/*NOTREACHED*/
    946 
    947  runfast:
    948 	/*
    949 	 * Raise priority to at least PUSER.
    950 	 */
    951 	if (p->p_priority > PUSER)
    952 		p->p_priority = PUSER;
    953  run:
    954 	setrunnable(p);		/* XXXSMP: recurse? */
    955  out:
    956 	/* XXXSMP: works, but icky */
    957 	if (dolock)
    958 		SCHED_UNLOCK(s);
    959 }
    960 
    961 static __inline int firstsig(const sigset_t *);
    962 
    963 static __inline int
    964 firstsig(const sigset_t *ss)
    965 {
    966 	int sig;
    967 
    968 	sig = ffs(ss->__bits[0]);
    969 	if (sig != 0)
    970 		return (sig);
    971 #if NSIG > 33
    972 	sig = ffs(ss->__bits[1]);
    973 	if (sig != 0)
    974 		return (sig + 32);
    975 #endif
    976 #if NSIG > 65
    977 	sig = ffs(ss->__bits[2]);
    978 	if (sig != 0)
    979 		return (sig + 64);
    980 #endif
    981 #if NSIG > 97
    982 	sig = ffs(ss->__bits[3]);
    983 	if (sig != 0)
    984 		return (sig + 96);
    985 #endif
    986 	return (0);
    987 }
    988 
    989 /*
    990  * If the current process has received a signal (should be caught or cause
    991  * termination, should interrupt current syscall), return the signal number.
    992  * Stop signals with default action are processed immediately, then cleared;
    993  * they aren't returned.  This is checked after each entry to the system for
    994  * a syscall or trap (though this can usually be done without calling issignal
    995  * by checking the pending signal masks in the CURSIG macro.) The normal call
    996  * sequence is
    997  *
    998  *	while (signum = CURSIG(curproc))
    999  *		postsig(signum);
   1000  */
   1001 int
   1002 issignal(struct proc *p)
   1003 {
   1004 	int		s, signum, prop;
   1005 	sigset_t	ss;
   1006 
   1007 	for (;;) {
   1008 		sigpending1(p, &ss);
   1009 		if (p->p_flag & P_PPWAIT)
   1010 			sigminusset(&stopsigmask, &ss);
   1011 		signum = firstsig(&ss);
   1012 		if (signum == 0) {		 	/* no signal to send */
   1013 			p->p_sigctx.ps_sigcheck = 0;
   1014 			return (0);
   1015 		}
   1016 							/* take the signal! */
   1017 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   1018 
   1019 		/*
   1020 		 * We should see pending but ignored signals
   1021 		 * only if P_TRACED was on when they were posted.
   1022 		 */
   1023 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
   1024 		    (p->p_flag & P_TRACED) == 0)
   1025 			continue;
   1026 
   1027 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1028 			/*
   1029 			 * If traced, always stop, and stay
   1030 			 * stopped until released by the debugger.
   1031 			 */
   1032 			p->p_xstat = signum;
   1033 			if ((p->p_flag & P_FSTRACE) == 0)
   1034 				psignal(p->p_pptr, SIGCHLD);
   1035 			SCHED_LOCK(s);
   1036 			proc_stop(p);
   1037 			mi_switch(p);
   1038 			SCHED_ASSERT_UNLOCKED();
   1039 			splx(s);
   1040 
   1041 			/*
   1042 			 * If we are no longer being traced, or the parent
   1043 			 * didn't give us a signal, look for more signals.
   1044 			 */
   1045 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1046 				continue;
   1047 
   1048 			/*
   1049 			 * If the new signal is being masked, look for other
   1050 			 * signals.
   1051 			 */
   1052 			signum = p->p_xstat;
   1053 			/*
   1054 			 * `p->p_sigctx.ps_siglist |= mask' is done
   1055 			 * in setrunnable().
   1056 			 */
   1057 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1058 				continue;
   1059 							/* take the signal! */
   1060 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1061 		}
   1062 
   1063 		prop = sigprop[signum];
   1064 
   1065 		/*
   1066 		 * Decide whether the signal should be returned.
   1067 		 * Return the signal's number, or fall through
   1068 		 * to clear it from the pending mask.
   1069 		 */
   1070 		switch ((long)SIGACTION(p, signum).sa_handler) {
   1071 
   1072 		case (long)SIG_DFL:
   1073 			/*
   1074 			 * Don't take default actions on system processes.
   1075 			 */
   1076 			if (p->p_pid <= 1) {
   1077 #ifdef DIAGNOSTIC
   1078 				/*
   1079 				 * Are you sure you want to ignore SIGSEGV
   1080 				 * in init? XXX
   1081 				 */
   1082 				printf("Process (pid %d) got signal %d\n",
   1083 				    p->p_pid, signum);
   1084 #endif
   1085 				break;		/* == ignore */
   1086 			}
   1087 			/*
   1088 			 * If there is a pending stop signal to process
   1089 			 * with default action, stop here,
   1090 			 * then clear the signal.  However,
   1091 			 * if process is member of an orphaned
   1092 			 * process group, ignore tty stop signals.
   1093 			 */
   1094 			if (prop & SA_STOP) {
   1095 				if (p->p_flag & P_TRACED ||
   1096 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1097 				    prop & SA_TTYSTOP))
   1098 					break;	/* == ignore */
   1099 				p->p_xstat = signum;
   1100 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1101 					psignal(p->p_pptr, SIGCHLD);
   1102 				SCHED_LOCK(s);
   1103 				proc_stop(p);
   1104 				mi_switch(p);
   1105 				SCHED_ASSERT_UNLOCKED();
   1106 				splx(s);
   1107 				break;
   1108 			} else if (prop & SA_IGNORE) {
   1109 				/*
   1110 				 * Except for SIGCONT, shouldn't get here.
   1111 				 * Default action is to ignore; drop it.
   1112 				 */
   1113 				break;		/* == ignore */
   1114 			} else
   1115 				goto keep;
   1116 			/*NOTREACHED*/
   1117 
   1118 		case (long)SIG_IGN:
   1119 			/*
   1120 			 * Masking above should prevent us ever trying
   1121 			 * to take action on an ignored signal other
   1122 			 * than SIGCONT, unless process is traced.
   1123 			 */
   1124 			if ((prop & SA_CONT) == 0 &&
   1125 			    (p->p_flag & P_TRACED) == 0)
   1126 				printf("issignal\n");
   1127 			break;		/* == ignore */
   1128 
   1129 		default:
   1130 			/*
   1131 			 * This signal has an action, let
   1132 			 * postsig() process it.
   1133 			 */
   1134 			goto keep;
   1135 		}
   1136 	}
   1137 	/* NOTREACHED */
   1138 
   1139  keep:
   1140 						/* leave the signal for later */
   1141 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1142 	CHECKSIGS(p);
   1143 	return (signum);
   1144 }
   1145 
   1146 /*
   1147  * Put the argument process into the stopped state and notify the parent
   1148  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1149  * on the run queue.
   1150  */
   1151 static void
   1152 proc_stop(struct proc *p)
   1153 {
   1154 
   1155 	SCHED_ASSERT_LOCKED();
   1156 
   1157 	p->p_stat = SSTOP;
   1158 	p->p_flag &= ~P_WAITED;
   1159 	sched_wakeup((caddr_t)p->p_pptr);
   1160 }
   1161 
   1162 /*
   1163  * Take the action for the specified signal
   1164  * from the current set of pending signals.
   1165  */
   1166 void
   1167 postsig(int signum)
   1168 {
   1169 	struct proc	*p;
   1170 	struct sigacts	*ps;
   1171 	sig_t		action;
   1172 	u_long		code;
   1173 	sigset_t	*returnmask;
   1174 
   1175 	p = curproc;
   1176 	ps = p->p_sigacts;
   1177 #ifdef DIAGNOSTIC
   1178 	if (signum == 0)
   1179 		panic("postsig");
   1180 #endif
   1181 
   1182 	KERNEL_PROC_LOCK(p);
   1183 
   1184 	sigdelset(&p->p_sigctx.ps_siglist, signum);
   1185 	action = SIGACTION_PS(ps, signum).sa_handler;
   1186 #ifdef KTRACE
   1187 	if (KTRPOINT(p, KTR_PSIG))
   1188 		ktrpsig(p,
   1189 		    signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1190 		    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
   1191 #endif
   1192 	if (action == SIG_DFL) {
   1193 		/*
   1194 		 * Default action, where the default is to kill
   1195 		 * the process.  (Other cases were ignored above.)
   1196 		 */
   1197 		sigexit(p, signum);
   1198 		/* NOTREACHED */
   1199 	} else {
   1200 		/*
   1201 		 * If we get here, the signal must be caught.
   1202 		 */
   1203 #ifdef DIAGNOSTIC
   1204 		if (action == SIG_IGN ||
   1205 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
   1206 			panic("postsig action");
   1207 #endif
   1208 		/*
   1209 		 * Set the new mask value and also defer further
   1210 		 * occurences of this signal.
   1211 		 *
   1212 		 * Special case: user has done a sigpause.  Here the
   1213 		 * current mask is not of interest, but rather the
   1214 		 * mask from before the sigpause is what we want
   1215 		 * restored after the signal processing is completed.
   1216 		 */
   1217 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
   1218 			returnmask = &p->p_sigctx.ps_oldmask;
   1219 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
   1220 		} else
   1221 			returnmask = &p->p_sigctx.ps_sigmask;
   1222 		p->p_stats->p_ru.ru_nsignals++;
   1223 		if (p->p_sigctx.ps_sig != signum) {
   1224 			code = 0;
   1225 		} else {
   1226 			code = p->p_sigctx.ps_code;
   1227 			p->p_sigctx.ps_code = 0;
   1228 			p->p_sigctx.ps_sig = 0;
   1229 		}
   1230 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code);
   1231 		(void) splsched();	/* XXXSMP */
   1232 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
   1233 		    &p->p_sigctx.ps_sigmask);
   1234 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
   1235 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1236 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1237 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
   1238 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
   1239 		}
   1240 		(void) spl0();		/* XXXSMP */
   1241 	}
   1242 
   1243 	KERNEL_PROC_UNLOCK(p);
   1244 }
   1245 
   1246 /*
   1247  * Kill the current process for stated reason.
   1248  */
   1249 void
   1250 killproc(struct proc *p, char *why)
   1251 {
   1252 
   1253 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1254 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1255 	psignal(p, SIGKILL);
   1256 }
   1257 
   1258 /*
   1259  * Force the current process to exit with the specified signal, dumping core
   1260  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1261  * allowing unrecoverable failures to terminate the process without changing
   1262  * signal state.  Mark the accounting record with the signal termination.
   1263  * If dumping core, save the signal number for the debugger.  Calls exit and
   1264  * does not return.
   1265  */
   1266 
   1267 #if defined(DEBUG)
   1268 int	kern_logsigexit = 1;	/* not static to make public for sysctl */
   1269 #else
   1270 int	kern_logsigexit = 0;	/* not static to make public for sysctl */
   1271 #endif
   1272 
   1273 static	const char logcoredump[] =
   1274 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
   1275 static	const char lognocoredump[] =
   1276 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
   1277 
   1278 void
   1279 sigexit(struct proc *p, int signum)
   1280 {
   1281 	int	error, exitsig;
   1282 
   1283 	exitsig = signum;
   1284 	p->p_acflag |= AXSIG;
   1285 	if (sigprop[signum] & SA_CORE) {
   1286 		p->p_sigctx.ps_sig = signum;
   1287 		if ((error = coredump(p)) == 0)
   1288 			exitsig |= WCOREFLAG;
   1289 
   1290 		if (kern_logsigexit) {
   1291 			int uid = p->p_cred && p->p_ucred ?
   1292 				p->p_ucred->cr_uid : -1;
   1293 
   1294 			if (error)
   1295 				log(LOG_INFO, lognocoredump, p->p_pid,
   1296 				    p->p_comm, uid, signum, error);
   1297 			else
   1298 				log(LOG_INFO, logcoredump, p->p_pid,
   1299 				    p->p_comm, uid, signum);
   1300 		}
   1301 
   1302 	}
   1303 
   1304 	exit1(p, W_EXITCODE(0, exitsig));
   1305 	/* NOTREACHED */
   1306 }
   1307 
   1308 /*
   1309  * Dump core, into a file named "progname.core" or "core" (depending on the
   1310  * value of shortcorename), unless the process was setuid/setgid.
   1311  */
   1312 int
   1313 coredump(struct proc *p)
   1314 {
   1315 	struct vnode		*vp;
   1316 	struct vmspace		*vm;
   1317 	struct ucred		*cred;
   1318 	struct nameidata	nd;
   1319 	struct vattr		vattr;
   1320 	int			error, error1;
   1321 	char			name[MAXPATHLEN];
   1322 	struct core		core;
   1323 
   1324 	vm = p->p_vmspace;
   1325 	cred = p->p_cred->pc_ucred;
   1326 
   1327 	/*
   1328 	 * Make sure the process has not set-id, to prevent data leaks.
   1329 	 */
   1330 	if (p->p_flag & P_SUGID)
   1331 		return (EPERM);
   1332 
   1333 	/*
   1334 	 * Refuse to core if the data + stack + user size is larger than
   1335 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
   1336 	 * data.
   1337 	 */
   1338 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   1339 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   1340 		return (EFBIG);		/* better error code? */
   1341 
   1342 	/*
   1343 	 * The core dump will go in the current working directory.  Make
   1344 	 * sure that the directory is still there and that the mount flags
   1345 	 * allow us to write core dumps there.
   1346 	 */
   1347 	vp = p->p_cwdi->cwdi_cdir;
   1348 	if (vp->v_mount == NULL ||
   1349 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
   1350 		return (EPERM);
   1351 
   1352 	error = build_corename(p, name);
   1353 	if (error)
   1354 		return error;
   1355 
   1356 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
   1357 	error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR);
   1358 	if (error)
   1359 		return (error);
   1360 	vp = nd.ni_vp;
   1361 
   1362 	/* Don't dump to non-regular files or files with links. */
   1363 	if (vp->v_type != VREG ||
   1364 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
   1365 		error = EINVAL;
   1366 		goto out;
   1367 	}
   1368 	VATTR_NULL(&vattr);
   1369 	vattr.va_size = 0;
   1370 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1371 	VOP_SETATTR(vp, &vattr, cred, p);
   1372 	p->p_acflag |= ACORE;
   1373 
   1374 	if ((p->p_flag & P_32) && coredump32_hook != NULL)
   1375 		return (*coredump32_hook)(p, vp);
   1376 #if 0
   1377 	/*
   1378 	 * XXX
   1379 	 * It would be nice if we at least dumped the signal state (and made it
   1380 	 * available at run time to the debugger, as well), but this code
   1381 	 * hasn't actually had any effect for a long time, since we don't dump
   1382 	 * the user area.  For now, it's dead.
   1383 	 */
   1384 	memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
   1385 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
   1386 #endif
   1387 
   1388 	core.c_midmag = 0;
   1389 	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
   1390 	core.c_nseg = 0;
   1391 	core.c_signo = p->p_sigctx.ps_sig;
   1392 	core.c_ucode = p->p_sigctx.ps_code;
   1393 	core.c_cpusize = 0;
   1394 	core.c_tsize = (u_long)ctob(vm->vm_tsize);
   1395 	core.c_dsize = (u_long)ctob(vm->vm_dsize);
   1396 	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
   1397 	error = cpu_coredump(p, vp, cred, &core);
   1398 	if (error)
   1399 		goto out;
   1400 	/*
   1401 	 * uvm_coredump() spits out all appropriate segments.
   1402 	 * All that's left to do is to write the core header.
   1403 	 */
   1404 	error = uvm_coredump(p, vp, cred, &core);
   1405 	if (error)
   1406 		goto out;
   1407 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
   1408 	    (int)core.c_hdrsize, (off_t)0,
   1409 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
   1410  out:
   1411 	VOP_UNLOCK(vp, 0);
   1412 	error1 = vn_close(vp, FWRITE, cred, p);
   1413 	if (error == 0)
   1414 		error = error1;
   1415 	return (error);
   1416 }
   1417 
   1418 /*
   1419  * Nonexistent system call-- signal process (may want to handle it).
   1420  * Flag error in case process won't see signal immediately (blocked or ignored).
   1421  */
   1422 /* ARGSUSED */
   1423 int
   1424 sys_nosys(struct proc *p, void *v, register_t *retval)
   1425 {
   1426 
   1427 	psignal(p, SIGSYS);
   1428 	return (ENOSYS);
   1429 }
   1430 
   1431 static int
   1432 build_corename(struct proc *p, char dst[MAXPATHLEN])
   1433 {
   1434 	const char	*s;
   1435 	char		*d, *end;
   1436 	int		i;
   1437 
   1438 	for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
   1439 	    *s != '\0'; s++) {
   1440 		if (*s == '%') {
   1441 			switch (*(s + 1)) {
   1442 			case 'n':
   1443 				i = snprintf(d, end - d, "%s", p->p_comm);
   1444 				break;
   1445 			case 'p':
   1446 				i = snprintf(d, end - d, "%d", p->p_pid);
   1447 				break;
   1448 			case 'u':
   1449 				i = snprintf(d, end - d, "%s",
   1450 				    p->p_pgrp->pg_session->s_login);
   1451 				break;
   1452 			case 't':
   1453 				i = snprintf(d, end - d, "%ld",
   1454 				    p->p_stats->p_start.tv_sec);
   1455 				break;
   1456 			default:
   1457 				goto copy;
   1458 			}
   1459 			d += i;
   1460 			s++;
   1461 		} else {
   1462  copy:			*d = *s;
   1463 			d++;
   1464 		}
   1465 		if (d >= end)
   1466 			return (ENAMETOOLONG);
   1467 	}
   1468 	*d = '\0';
   1469 	return (0);
   1470 }
   1471 
   1472 /*
   1473  * Returns true if signal is ignored or masked for passed process.
   1474  */
   1475 int
   1476 sigismasked(struct proc *p, int sig)
   1477 {
   1478 
   1479 	return sigismember(&p->p_sigctx.ps_sigignore, SIGTTOU)
   1480 		|| sigismember(&p->p_sigctx.ps_sigmask, SIGTTOU);
   1481 }
   1482