Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.113
      1 /*	$NetBSD: kern_sig.c,v 1.113 2001/06/06 21:37:19 mrg 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/ptrace.h>
     67 #include <sys/filedesc.h>
     68 #include <sys/malloc.h>
     69 #include <sys/pool.h>
     70 
     71 #include <sys/mount.h>
     72 #include <sys/syscallargs.h>
     73 
     74 #include <machine/cpu.h>
     75 
     76 #include <sys/user.h>		/* for coredump */
     77 
     78 #include <uvm/uvm_extern.h>
     79 
     80 static void	proc_stop(struct proc *p);
     81 void		killproc(struct proc *, char *);
     82 static int	build_corename(struct proc *, char [MAXPATHLEN]);
     83 sigset_t	contsigmask, stopsigmask, sigcantmask;
     84 
     85 struct pool	sigacts_pool;	/* memory pool for sigacts structures */
     86 
     87 int	(*coredump32_hook)(struct proc *p, struct vnode *vp);
     88 
     89 /*
     90  * Can process p, with pcred pc, send the signal signum to process q?
     91  */
     92 #define	CANSIGNAL(p, pc, q, signum) \
     93 	((pc)->pc_ucred->cr_uid == 0 || \
     94 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
     95 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
     96 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
     97 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
     98 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
     99 
    100 /*
    101  * Initialize signal-related data structures.
    102  */
    103 void
    104 signal_init(void)
    105 {
    106 
    107 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    108 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    109 }
    110 
    111 /*
    112  * Create an initial sigctx structure, using the same signal state
    113  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
    114  * copy it from parent.
    115  */
    116 void
    117 sigactsinit(struct proc *np, struct proc *pp, int share)
    118 {
    119 	struct sigacts *ps;
    120 
    121 	if (share) {
    122 		np->p_sigacts = pp->p_sigacts;
    123 		pp->p_sigacts->sa_refcnt++;
    124 	} else {
    125 		ps = pool_get(&sigacts_pool, PR_WAITOK);
    126 		if (pp)
    127 			memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
    128 		else
    129 			memset(ps, '\0', sizeof(struct sigacts));
    130 		ps->sa_refcnt = 1;
    131 		np->p_sigacts = ps;
    132 	}
    133 }
    134 
    135 /*
    136  * Make this process not share its sigctx, maintaining all
    137  * signal state.
    138  */
    139 void
    140 sigactsunshare(struct proc *p)
    141 {
    142 	struct sigacts *oldps;
    143 
    144 	if (p->p_sigacts->sa_refcnt == 1)
    145 		return;
    146 
    147 	oldps = p->p_sigacts;
    148 	sigactsinit(p, NULL, 0);
    149 
    150 	if (--oldps->sa_refcnt == 0)
    151 		pool_put(&sigacts_pool, oldps);
    152 }
    153 
    154 /*
    155  * Release a sigctx structure.
    156  */
    157 void
    158 sigactsfree(struct proc *p)
    159 {
    160 	struct sigacts *ps;
    161 
    162 	ps = p->p_sigacts;
    163 	if (--ps->sa_refcnt > 0)
    164 		return;
    165 
    166 	pool_put(&sigacts_pool, ps);
    167 }
    168 
    169 int
    170 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
    171 	struct sigaction *osa)
    172 {
    173 	struct sigacts	*ps;
    174 	int		prop;
    175 
    176 	ps = p->p_sigacts;
    177 	if (signum <= 0 || signum >= NSIG)
    178 		return (EINVAL);
    179 
    180 	if (osa)
    181 		*osa = SIGACTION_PS(ps, signum);
    182 
    183 	if (nsa) {
    184 		if (nsa->sa_flags & ~SA_ALLBITS)
    185 			return (EINVAL);
    186 
    187 		prop = sigprop[signum];
    188 		if (prop & SA_CANTMASK)
    189 			return (EINVAL);
    190 
    191 		(void) splsched();	/* XXXSMP */
    192 		SIGACTION_PS(ps, signum) = *nsa;
    193 		sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    194 		if ((prop & SA_NORESET) != 0)
    195 			SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    196 		if (signum == SIGCHLD) {
    197 			if (nsa->sa_flags & SA_NOCLDSTOP)
    198 				p->p_flag |= P_NOCLDSTOP;
    199 			else
    200 				p->p_flag &= ~P_NOCLDSTOP;
    201 			if (nsa->sa_flags & SA_NOCLDWAIT) {
    202 				/*
    203 				 * Paranoia: since SA_NOCLDWAIT is implemented
    204 				 * by reparenting the dying child to PID 1 (and
    205 				 * trust it to reap the zombie), PID 1 itself
    206 				 * is forbidden to set SA_NOCLDWAIT.
    207 				 */
    208 				if (p->p_pid == 1)
    209 					p->p_flag &= ~P_NOCLDWAIT;
    210 				else
    211 					p->p_flag |= P_NOCLDWAIT;
    212 			} else
    213 				p->p_flag &= ~P_NOCLDWAIT;
    214 		}
    215 		if ((nsa->sa_flags & SA_NODEFER) == 0)
    216 			sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    217 		else
    218 			sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    219 		/*
    220 	 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    221 		 * SIG_IGN, and for signals set to SIG_DFL where the default is
    222 		 * to ignore. However, don't put SIGCONT in
    223 		 * p_sigctx.ps_sigignore, as we have to restart the process.
    224 	 	 */
    225 		if (nsa->sa_handler == SIG_IGN ||
    226 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    227 						/* never to be seen again */
    228 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    229 			if (signum != SIGCONT) {
    230 						/* easier in psignal */
    231 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    232 			}
    233 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    234 		} else {
    235 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
    236 			if (nsa->sa_handler == SIG_DFL)
    237 				sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    238 			else
    239 				sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    240 		}
    241 		(void) spl0();
    242 	}
    243 
    244 	return (0);
    245 }
    246 
    247 /* ARGSUSED */
    248 int
    249 sys___sigaction14(struct proc *p, void *v, register_t *retval)
    250 {
    251 	struct sys___sigaction14_args /* {
    252 		syscallarg(int)				signum;
    253 		syscallarg(const struct sigaction *)	nsa;
    254 		syscallarg(struct sigaction *)		osa;
    255 	} */ *uap = v;
    256 	struct sigaction	nsa, osa;
    257 	int			error;
    258 
    259 	if (SCARG(uap, nsa)) {
    260 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    261 		if (error)
    262 			return (error);
    263 	}
    264 	error = sigaction1(p, SCARG(uap, signum),
    265 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0);
    266 	if (error)
    267 		return (error);
    268 	if (SCARG(uap, osa)) {
    269 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    270 		if (error)
    271 			return (error);
    272 	}
    273 	return (0);
    274 }
    275 
    276 /*
    277  * Initialize signal state for process 0;
    278  * set to ignore signals that are ignored by default and disable the signal
    279  * stack.
    280  */
    281 void
    282 siginit(struct proc *p)
    283 {
    284 	struct sigacts	*ps;
    285 	int		signum, prop;
    286 
    287 	ps = p->p_sigacts;
    288 	sigemptyset(&contsigmask);
    289 	sigemptyset(&stopsigmask);
    290 	sigemptyset(&sigcantmask);
    291 	for (signum = 1; signum < NSIG; signum++) {
    292 		prop = sigprop[signum];
    293 		if (prop & SA_CONT)
    294 			sigaddset(&contsigmask, signum);
    295 		if (prop & SA_STOP)
    296 			sigaddset(&stopsigmask, signum);
    297 		if (prop & SA_CANTMASK)
    298 			sigaddset(&sigcantmask, signum);
    299 		if (prop & SA_IGNORE && signum != SIGCONT)
    300 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    301 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    302 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    303 	}
    304 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    305 	p->p_flag &= ~P_NOCLDSTOP;
    306 
    307 	/*
    308 	 * Reset stack state to the user stack.
    309 	 */
    310 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    311 	p->p_sigctx.ps_sigstk.ss_size = 0;
    312 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    313 
    314 	/* One reference. */
    315 	ps->sa_refcnt = 1;
    316 }
    317 
    318 /*
    319  * Reset signals for an exec of the specified process.
    320  */
    321 void
    322 execsigs(struct proc *p)
    323 {
    324 	struct sigacts	*ps;
    325 	int		signum, prop;
    326 
    327 	ps = p->p_sigacts;
    328 	/*
    329 	 * Reset caught signals.  Held signals remain held
    330 	 * through p_sigctx.ps_sigmask (unless they were caught,
    331 	 * and are now ignored by default).
    332 	 */
    333 	for (signum = 1; signum < NSIG; signum++) {
    334 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
    335 			prop = sigprop[signum];
    336 			if (prop & SA_IGNORE) {
    337 				if ((prop & SA_CONT) == 0)
    338 					sigaddset(&p->p_sigctx.ps_sigignore,
    339 					    signum);
    340 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    341 			}
    342 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    343 		}
    344 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    345 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    346 	}
    347 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    348 	p->p_flag &= ~P_NOCLDSTOP;
    349 
    350 	/*
    351 	 * Reset stack state to the user stack.
    352 	 */
    353 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    354 	p->p_sigctx.ps_sigstk.ss_size = 0;
    355 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    356 }
    357 
    358 int
    359 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
    360 {
    361 
    362 	if (oss)
    363 		*oss = p->p_sigctx.ps_sigmask;
    364 
    365 	if (nss) {
    366 		(void)splsched();	/* XXXSMP */
    367 		switch (how) {
    368 		case SIG_BLOCK:
    369 			sigplusset(nss, &p->p_sigctx.ps_sigmask);
    370 			break;
    371 		case SIG_UNBLOCK:
    372 			sigminusset(nss, &p->p_sigctx.ps_sigmask);
    373 			CHECKSIGS(p);
    374 			break;
    375 		case SIG_SETMASK:
    376 			p->p_sigctx.ps_sigmask = *nss;
    377 			CHECKSIGS(p);
    378 			break;
    379 		default:
    380 			(void)spl0();	/* XXXSMP */
    381 			return (EINVAL);
    382 		}
    383 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    384 		(void)spl0();		/* XXXSMP */
    385 	}
    386 
    387 	return (0);
    388 }
    389 
    390 /*
    391  * Manipulate signal mask.
    392  * Note that we receive new mask, not pointer,
    393  * and return old mask as return value;
    394  * the library stub does the rest.
    395  */
    396 int
    397 sys___sigprocmask14(struct proc *p, void *v, register_t *retval)
    398 {
    399 	struct sys___sigprocmask14_args /* {
    400 		syscallarg(int)			how;
    401 		syscallarg(const sigset_t *)	set;
    402 		syscallarg(sigset_t *)		oset;
    403 	} */ *uap = v;
    404 	sigset_t	nss, oss;
    405 	int		error;
    406 
    407 	if (SCARG(uap, set)) {
    408 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    409 		if (error)
    410 			return (error);
    411 	}
    412 	error = sigprocmask1(p, SCARG(uap, how),
    413 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    414 	if (error)
    415 		return (error);
    416 	if (SCARG(uap, oset)) {
    417 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    418 		if (error)
    419 			return (error);
    420 	}
    421 	return (0);
    422 }
    423 
    424 void
    425 sigpending1(struct proc *p, sigset_t *ss)
    426 {
    427 
    428 	*ss = p->p_sigctx.ps_siglist;
    429 	sigminusset(&p->p_sigctx.ps_sigmask, ss);
    430 }
    431 
    432 /* ARGSUSED */
    433 int
    434 sys___sigpending14(struct proc *p, void *v, register_t *retval)
    435 {
    436 	struct sys___sigpending14_args /* {
    437 		syscallarg(sigset_t *)	set;
    438 	} */ *uap = v;
    439 	sigset_t ss;
    440 
    441 	sigpending1(p, &ss);
    442 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    443 }
    444 
    445 int
    446 sigsuspend1(struct proc *p, const sigset_t *ss)
    447 {
    448 	struct sigacts *ps;
    449 
    450 	ps = p->p_sigacts;
    451 	if (ss) {
    452 		/*
    453 		 * When returning from sigpause, we want
    454 		 * the old mask to be restored after the
    455 		 * signal handler has finished.  Thus, we
    456 		 * save it here and mark the sigctx structure
    457 		 * to indicate this.
    458 		 */
    459 		p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
    460 		p->p_sigctx.ps_flags |= SAS_OLDMASK;
    461 		(void) splsched();	/* XXXSMP */
    462 		p->p_sigctx.ps_sigmask = *ss;
    463 		CHECKSIGS(p);
    464 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    465 		(void) spl0();		/* XXXSMP */
    466 	}
    467 
    468 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    469 		/* void */;
    470 	/* always return EINTR rather than ERESTART... */
    471 	return (EINTR);
    472 }
    473 
    474 /*
    475  * Suspend process until signal, providing mask to be set
    476  * in the meantime.  Note nonstandard calling convention:
    477  * libc stub passes mask, not pointer, to save a copyin.
    478  */
    479 /* ARGSUSED */
    480 int
    481 sys___sigsuspend14(struct proc *p, void *v, register_t *retval)
    482 {
    483 	struct sys___sigsuspend14_args /* {
    484 		syscallarg(const sigset_t *)	set;
    485 	} */ *uap = v;
    486 	sigset_t	ss;
    487 	int		error;
    488 
    489 	if (SCARG(uap, set)) {
    490 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    491 		if (error)
    492 			return (error);
    493 	}
    494 
    495 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
    496 }
    497 
    498 int
    499 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
    500 	struct sigaltstack *oss)
    501 {
    502 
    503 	if (oss)
    504 		*oss = p->p_sigctx.ps_sigstk;
    505 
    506 	if (nss) {
    507 		if (nss->ss_flags & ~SS_ALLBITS)
    508 			return (EINVAL);
    509 
    510 		if (nss->ss_flags & SS_DISABLE) {
    511 			if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    512 				return (EINVAL);
    513 		} else {
    514 			if (nss->ss_size < MINSIGSTKSZ)
    515 				return (ENOMEM);
    516 		}
    517 		p->p_sigctx.ps_sigstk = *nss;
    518 	}
    519 
    520 	return (0);
    521 }
    522 
    523 /* ARGSUSED */
    524 int
    525 sys___sigaltstack14(struct proc *p, void *v, register_t *retval)
    526 {
    527 	struct sys___sigaltstack14_args /* {
    528 		syscallarg(const struct sigaltstack *)	nss;
    529 		syscallarg(struct sigaltstack *)	oss;
    530 	} */ *uap = v;
    531 	struct sigaltstack	nss, oss;
    532 	int			error;
    533 
    534 	if (SCARG(uap, nss)) {
    535 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    536 		if (error)
    537 			return (error);
    538 	}
    539 	error = sigaltstack1(p,
    540 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    541 	if (error)
    542 		return (error);
    543 	if (SCARG(uap, oss)) {
    544 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    545 		if (error)
    546 			return (error);
    547 	}
    548 	return (0);
    549 }
    550 
    551 /* ARGSUSED */
    552 int
    553 sys_kill(struct proc *cp, void *v, register_t *retval)
    554 {
    555 	struct sys_kill_args /* {
    556 		syscallarg(int)	pid;
    557 		syscallarg(int)	signum;
    558 	} */ *uap = v;
    559 	struct proc	*p;
    560 	struct pcred	*pc;
    561 
    562 	pc = cp->p_cred;
    563 	if ((u_int)SCARG(uap, signum) >= NSIG)
    564 		return (EINVAL);
    565 	if (SCARG(uap, pid) > 0) {
    566 		/* kill single process */
    567 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    568 			return (ESRCH);
    569 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    570 			return (EPERM);
    571 		if (SCARG(uap, signum))
    572 			psignal(p, SCARG(uap, signum));
    573 		return (0);
    574 	}
    575 	switch (SCARG(uap, pid)) {
    576 	case -1:		/* broadcast signal */
    577 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
    578 	case 0:			/* signal own process group */
    579 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
    580 	default:		/* negative explicit process group */
    581 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
    582 	}
    583 	/* NOTREACHED */
    584 }
    585 
    586 /*
    587  * Common code for kill process group/broadcast kill.
    588  * cp is calling process.
    589  */
    590 int
    591 killpg1(struct proc *cp, int signum, int pgid, int all)
    592 {
    593 	struct proc	*p;
    594 	struct pcred	*pc;
    595 	struct pgrp	*pgrp;
    596 	int		nfound;
    597 
    598 	pc = cp->p_cred;
    599 	nfound = 0;
    600 	if (all) {
    601 		/*
    602 		 * broadcast
    603 		 */
    604 		proclist_lock_read();
    605 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    606 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    607 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    608 				continue;
    609 			nfound++;
    610 			if (signum)
    611 				psignal(p, signum);
    612 		}
    613 		proclist_unlock_read();
    614 	} else {
    615 		if (pgid == 0)
    616 			/*
    617 			 * zero pgid means send to my process group.
    618 			 */
    619 			pgrp = cp->p_pgrp;
    620 		else {
    621 			pgrp = pgfind(pgid);
    622 			if (pgrp == NULL)
    623 				return (ESRCH);
    624 		}
    625 		for (p = pgrp->pg_members.lh_first;
    626 		    p != 0;
    627 		    p = p->p_pglist.le_next) {
    628 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    629 			    !CANSIGNAL(cp, pc, p, signum))
    630 				continue;
    631 			nfound++;
    632 			if (signum && P_ZOMBIE(p) == 0)
    633 				psignal(p, signum);
    634 		}
    635 	}
    636 	return (nfound ? 0 : ESRCH);
    637 }
    638 
    639 /*
    640  * Send a signal to a process group.
    641  */
    642 void
    643 gsignal(int pgid, int signum)
    644 {
    645 	struct pgrp *pgrp;
    646 
    647 	if (pgid && (pgrp = pgfind(pgid)))
    648 		pgsignal(pgrp, signum, 0);
    649 }
    650 
    651 /*
    652  * Send a signal to a process group. If checktty is 1,
    653  * limit to members which have a controlling terminal.
    654  */
    655 void
    656 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
    657 {
    658 	struct proc *p;
    659 
    660 	if (pgrp)
    661 		for (p = pgrp->pg_members.lh_first; p != 0;
    662 		    p = p->p_pglist.le_next)
    663 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    664 				psignal(p, signum);
    665 }
    666 
    667 /*
    668  * Send a signal caused by a trap to the current process.
    669  * If it will be caught immediately, deliver it with correct code.
    670  * Otherwise, post it normally.
    671  */
    672 void
    673 trapsignal(struct proc *p, int signum, u_long code)
    674 {
    675 	struct sigacts *ps;
    676 
    677 	ps = p->p_sigacts;
    678 	if ((p->p_flag & P_TRACED) == 0 &&
    679 	    sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
    680 	    !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
    681 		p->p_stats->p_ru.ru_nsignals++;
    682 #ifdef KTRACE
    683 		if (KTRPOINT(p, KTR_PSIG))
    684 			ktrpsig(p, signum,
    685 			    SIGACTION_PS(ps, signum).sa_handler,
    686 			    &p->p_sigctx.ps_sigmask, code);
    687 #endif
    688 		(*p->p_emul->e_sendsig)(SIGACTION_PS(ps, signum).sa_handler,
    689 		    signum, &p->p_sigctx.ps_sigmask, code);
    690 		(void) splsched();	/* XXXSMP */
    691 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
    692 		    &p->p_sigctx.ps_sigmask);
    693 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
    694 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    695 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    696 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    697 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    698 		}
    699 		(void) spl0();		/* XXXSMP */
    700 	} else {
    701 		p->p_sigctx.ps_code = code;	/* XXX for core dump/debugger */
    702 		p->p_sigctx.ps_sig = signum;	/* XXX to verify code */
    703 		psignal(p, signum);
    704 	}
    705 }
    706 
    707 /*
    708  * Send the signal to the process.  If the signal has an action, the action
    709  * is usually performed by the target process rather than the caller; we add
    710  * the signal to the set of pending signals for the process.
    711  *
    712  * Exceptions:
    713  *   o When a stop signal is sent to a sleeping process that takes the
    714  *     default action, the process is stopped without awakening it.
    715  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    716  *     regardless of the signal action (eg, blocked or ignored).
    717  *
    718  * Other ignored signals are discarded immediately.
    719  *
    720  * XXXSMP: Invoked as psignal() or sched_psignal().
    721  */
    722 void
    723 psignal1(struct proc *p, int signum,
    724 	int dolock)		/* XXXSMP: works, but icky */
    725 {
    726 	int	s, prop;
    727 	sig_t	action;
    728 
    729 #ifdef DIAGNOSTIC
    730 	if (signum <= 0 || signum >= NSIG)
    731 		panic("psignal signal number");
    732 
    733 	/* XXXSMP: works, but icky */
    734 	if (dolock)
    735 		SCHED_ASSERT_UNLOCKED();
    736 	else
    737 		SCHED_ASSERT_LOCKED();
    738 #endif
    739 	prop = sigprop[signum];
    740 
    741 	/*
    742 	 * If proc is traced, always give parent a chance.
    743 	 */
    744 	if (p->p_flag & P_TRACED)
    745 		action = SIG_DFL;
    746 	else {
    747 		/*
    748 		 * If the signal is being ignored,
    749 		 * then we forget about it immediately.
    750 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
    751 		 * and if it is set to SIG_IGN,
    752 		 * action will be SIG_DFL here.)
    753 		 */
    754 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
    755 			return;
    756 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
    757 			action = SIG_HOLD;
    758 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
    759 			action = SIG_CATCH;
    760 		else {
    761 			action = SIG_DFL;
    762 
    763 			if (prop & SA_KILL && p->p_nice > NZERO)
    764 				p->p_nice = NZERO;
    765 
    766 			/*
    767 			 * If sending a tty stop signal to a member of an
    768 			 * orphaned process group, discard the signal here if
    769 			 * the action is default; don't stop the process below
    770 			 * if sleeping, and don't clear any pending SIGCONT.
    771 			 */
    772 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
    773 				return;
    774 		}
    775 	}
    776 
    777 	if (prop & SA_CONT)
    778 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
    779 
    780 	if (prop & SA_STOP)
    781 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
    782 
    783 	sigaddset(&p->p_sigctx.ps_siglist, signum);
    784 
    785 	/* CHECKSIGS() is "inlined" here. */
    786 	p->p_sigctx.ps_sigcheck = 1;
    787 
    788 	/*
    789 	 * Defer further processing for signals which are held,
    790 	 * except that stopped processes must be continued by SIGCONT.
    791 	 */
    792 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
    793 		return;
    794 
    795 	/* XXXSMP: works, but icky */
    796 	if (dolock)
    797 		SCHED_LOCK(s);
    798 
    799 	switch (p->p_stat) {
    800 	case SSLEEP:
    801 		/*
    802 		 * If process is sleeping uninterruptibly
    803 		 * we can't interrupt the sleep... the signal will
    804 		 * be noticed when the process returns through
    805 		 * trap() or syscall().
    806 		 */
    807 		if ((p->p_flag & P_SINTR) == 0)
    808 			goto out;
    809 		/*
    810 		 * Process is sleeping and traced... make it runnable
    811 		 * so it can discover the signal in issignal() and stop
    812 		 * for the parent.
    813 		 */
    814 		if (p->p_flag & P_TRACED)
    815 			goto run;
    816 		/*
    817 		 * If SIGCONT is default (or ignored) and process is
    818 		 * asleep, we are finished; the process should not
    819 		 * be awakened.
    820 		 */
    821 		if ((prop & SA_CONT) && action == SIG_DFL) {
    822 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    823 			goto out;
    824 		}
    825 		/*
    826 		 * When a sleeping process receives a stop
    827 		 * signal, process immediately if possible.
    828 		 */
    829 		if ((prop & SA_STOP) && action == SIG_DFL) {
    830 			/*
    831 			 * If a child holding parent blocked,
    832 			 * stopping could cause deadlock.
    833 			 */
    834 			if (p->p_flag & P_PPWAIT)
    835 				goto out;
    836 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    837 			p->p_xstat = signum;
    838 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
    839 				/*
    840 				 * XXXSMP: recursive call; don't lock
    841 				 * the second time around.
    842 				 */
    843 				sched_psignal(p->p_pptr, SIGCHLD);
    844 			}
    845 			proc_stop(p);	/* XXXSMP: recurse? */
    846 			goto out;
    847 		}
    848 		/*
    849 		 * All other (caught or default) signals
    850 		 * cause the process to run.
    851 		 */
    852 		goto runfast;
    853 		/*NOTREACHED*/
    854 
    855 	case SSTOP:
    856 		/*
    857 		 * If traced process is already stopped,
    858 		 * then no further action is necessary.
    859 		 */
    860 		if (p->p_flag & P_TRACED)
    861 			goto out;
    862 
    863 		/*
    864 		 * Kill signal always sets processes running.
    865 		 */
    866 		if (signum == SIGKILL)
    867 			goto runfast;
    868 
    869 		if (prop & SA_CONT) {
    870 			/*
    871 			 * If SIGCONT is default (or ignored), we continue the
    872 			 * process but don't leave the signal in p_sigctx.ps_siglist, as
    873 			 * it has no further action.  If SIGCONT is held, we
    874 			 * continue the process and leave the signal in
    875 			 * p_sigctx.ps_siglist.  If the process catches SIGCONT, let it
    876 			 * handle the signal itself.  If it isn't waiting on
    877 			 * an event, then it goes back to run state.
    878 			 * Otherwise, process goes back to sleep state.
    879 			 */
    880 			if (action == SIG_DFL)
    881 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    882 			if (action == SIG_CATCH)
    883 				goto runfast;
    884 			if (p->p_wchan == 0)
    885 				goto run;
    886 			p->p_stat = SSLEEP;
    887 			goto out;
    888 		}
    889 
    890 		if (prop & SA_STOP) {
    891 			/*
    892 			 * Already stopped, don't need to stop again.
    893 			 * (If we did the shell could get confused.)
    894 			 */
    895 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    896 			goto out;
    897 		}
    898 
    899 		/*
    900 		 * If process is sleeping interruptibly, then simulate a
    901 		 * wakeup so that when it is continued, it will be made
    902 		 * runnable and can look at the signal.  But don't make
    903 		 * the process runnable, leave it stopped.
    904 		 */
    905 		if (p->p_wchan && p->p_flag & P_SINTR)
    906 			unsleep(p);
    907 		goto out;
    908 #ifdef __HAVE_AST_PERPROC
    909 	case SONPROC:
    910 	case SRUN:
    911 	case SIDL:
    912 		/*
    913 		 * SONPROC: We're running, notice the signal when
    914 		 * we return back to userspace.
    915 		 *
    916 		 * SRUN, SIDL: Notice the signal when we run again
    917 		 * and return to back to userspace.
    918 		 */
    919 		signotify(p);
    920 		goto out;
    921 
    922 	default:
    923 		/*
    924 		 * SDEAD, SZOMB: The signal will never be noticed.
    925 		 */
    926 		goto out;
    927 #else /* ! __HAVE_AST_PERPROC */
    928 	case SONPROC:
    929 		/*
    930 		 * We're running; notice the signal.
    931 		 */
    932 		signotify(p);
    933 		goto out;
    934 
    935 	default:
    936 		/*
    937 		 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal.
    938 		 * It will either never be noticed, or noticed very soon.
    939 		 */
    940 		goto out;
    941 #endif /* __HAVE_AST_PERPROC */
    942 	}
    943 	/*NOTREACHED*/
    944 
    945  runfast:
    946 	/*
    947 	 * Raise priority to at least PUSER.
    948 	 */
    949 	if (p->p_priority > PUSER)
    950 		p->p_priority = PUSER;
    951  run:
    952 	setrunnable(p);		/* XXXSMP: recurse? */
    953  out:
    954 	/* XXXSMP: works, but icky */
    955 	if (dolock)
    956 		SCHED_UNLOCK(s);
    957 }
    958 
    959 static __inline int firstsig(const sigset_t *);
    960 
    961 static __inline int
    962 firstsig(const sigset_t *ss)
    963 {
    964 	int sig;
    965 
    966 	sig = ffs(ss->__bits[0]);
    967 	if (sig != 0)
    968 		return (sig);
    969 #if NSIG > 33
    970 	sig = ffs(ss->__bits[1]);
    971 	if (sig != 0)
    972 		return (sig + 32);
    973 #endif
    974 #if NSIG > 65
    975 	sig = ffs(ss->__bits[2]);
    976 	if (sig != 0)
    977 		return (sig + 64);
    978 #endif
    979 #if NSIG > 97
    980 	sig = ffs(ss->__bits[3]);
    981 	if (sig != 0)
    982 		return (sig + 96);
    983 #endif
    984 	return (0);
    985 }
    986 
    987 /*
    988  * If the current process has received a signal (should be caught or cause
    989  * termination, should interrupt current syscall), return the signal number.
    990  * Stop signals with default action are processed immediately, then cleared;
    991  * they aren't returned.  This is checked after each entry to the system for
    992  * a syscall or trap (though this can usually be done without calling issignal
    993  * by checking the pending signal masks in the CURSIG macro.) The normal call
    994  * sequence is
    995  *
    996  *	while (signum = CURSIG(curproc))
    997  *		postsig(signum);
    998  */
    999 int
   1000 issignal(struct proc *p)
   1001 {
   1002 	int		s, signum, prop;
   1003 	sigset_t	ss;
   1004 
   1005 	for (;;) {
   1006 		sigpending1(p, &ss);
   1007 		if (p->p_flag & P_PPWAIT)
   1008 			sigminusset(&stopsigmask, &ss);
   1009 		signum = firstsig(&ss);
   1010 		if (signum == 0) {		 	/* no signal to send */
   1011 			p->p_sigctx.ps_sigcheck = 0;
   1012 			return (0);
   1013 		}
   1014 							/* take the signal! */
   1015 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   1016 
   1017 		/*
   1018 		 * We should see pending but ignored signals
   1019 		 * only if P_TRACED was on when they were posted.
   1020 		 */
   1021 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
   1022 		    (p->p_flag & P_TRACED) == 0)
   1023 			continue;
   1024 
   1025 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1026 			/*
   1027 			 * If traced, always stop, and stay
   1028 			 * stopped until released by the debugger.
   1029 			 */
   1030 			p->p_xstat = signum;
   1031 			if ((p->p_flag & P_FSTRACE) == 0)
   1032 				psignal(p->p_pptr, SIGCHLD);
   1033 			do {
   1034 				SCHED_LOCK(s);
   1035 				proc_stop(p);
   1036 				mi_switch(p);
   1037 				SCHED_ASSERT_UNLOCKED();
   1038 				splx(s);
   1039 			} while (!trace_req(p) && p->p_flag & P_TRACED);
   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