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