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