Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.94
      1 /*	$NetBSD: kern_sig.c,v 1.94 1999/09/28 14:47:03 bouyer 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 
     46 #define	SIGPROP		/* include signal properties table */
     47 #include <sys/param.h>
     48 #include <sys/signalvar.h>
     49 #include <sys/resourcevar.h>
     50 #include <sys/namei.h>
     51 #include <sys/vnode.h>
     52 #include <sys/proc.h>
     53 #include <sys/systm.h>
     54 #include <sys/timeb.h>
     55 #include <sys/times.h>
     56 #include <sys/buf.h>
     57 #include <sys/acct.h>
     58 #include <sys/file.h>
     59 #include <sys/kernel.h>
     60 #include <sys/wait.h>
     61 #include <sys/ktrace.h>
     62 #include <sys/syslog.h>
     63 #include <sys/stat.h>
     64 #include <sys/core.h>
     65 #include <sys/ptrace.h>
     66 #include <sys/filedesc.h>
     67 #include <sys/malloc.h>
     68 #include <sys/pool.h>
     69 
     70 #include <sys/mount.h>
     71 #include <sys/syscallargs.h>
     72 
     73 #include <machine/cpu.h>
     74 
     75 #include <vm/vm.h>
     76 #include <sys/user.h>		/* for coredump */
     77 
     78 #include <uvm/uvm_extern.h>
     79 
     80 void stop __P((struct proc *p));
     81 void killproc __P((struct proc *, char *));
     82 static int build_corename __P((char *));
     83 
     84 sigset_t contsigmask, stopsigmask, sigcantmask;
     85 
     86 struct pool sigacts_pool;	/* memory pool for sigacts structures */
     87 
     88 /*
     89  * Can process p, with pcred pc, send the signal signum to process q?
     90  */
     91 #define CANSIGNAL(p, pc, q, signum) \
     92 	((pc)->pc_ucred->cr_uid == 0 || \
     93 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
     94 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
     95 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
     96 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
     97 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
     98 
     99 /*
    100  * Initialize signal-related data structures.
    101  */
    102 void
    103 signal_init()
    104 {
    105 
    106 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    107 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    108 }
    109 
    110 /*
    111  * Create an initial sigacts structure, using the same signal state
    112  * as p.
    113  */
    114 struct sigacts *
    115 sigactsinit(p)
    116 	struct proc *p;
    117 {
    118 	struct sigacts *ps;
    119 
    120 	ps = pool_get(&sigacts_pool, PR_WAITOK);
    121 	memcpy(ps, p->p_sigacts, sizeof(struct sigacts));
    122 	ps->ps_refcnt = 1;
    123 	return (ps);
    124 }
    125 
    126 /*
    127  * Make p2 share p1's sigacts.
    128  */
    129 void
    130 sigactsshare(p1, p2)
    131 	struct proc *p1, *p2;
    132 {
    133 
    134 	p2->p_sigacts = p1->p_sigacts;
    135 	p1->p_sigacts->ps_refcnt++;
    136 }
    137 
    138 /*
    139  * Make this process not share its sigacts, maintaining all
    140  * signal state.
    141  */
    142 void
    143 sigactsunshare(p)
    144 	struct proc *p;
    145 {
    146 	struct sigacts *newps;
    147 
    148 	if (p->p_sigacts->ps_refcnt == 1)
    149 		return;
    150 
    151 	newps = sigactsinit(p);
    152 	sigactsfree(p);
    153 	p->p_sigacts = newps;
    154 }
    155 
    156 /*
    157  * Release a sigacts structure.
    158  */
    159 void
    160 sigactsfree(p)
    161 	struct proc *p;
    162 {
    163 	struct sigacts *ps = p->p_sigacts;
    164 
    165 	if (--ps->ps_refcnt > 0)
    166 		return;
    167 
    168 	p->p_sigacts = NULL;
    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 	register 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 = ps->ps_sigact[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) splhigh();
    198 		ps->ps_sigact[signum] = *nsa;
    199 		sigminusset(&sigcantmask, &ps->ps_sigact[signum].sa_mask);
    200 		if ((prop & SA_NORESET) != 0)
    201 			ps->ps_sigact[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(&ps->ps_sigact[signum].sa_mask, signum);
    223 		else
    224 			sigdelset(&ps->ps_sigact[signum].sa_mask, signum);
    225 		/*
    226 	 	* Set bit in p_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_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_siglist, signum);	/* never to be seen again */
    234 			if (signum != SIGCONT)
    235 				sigaddset(&p->p_sigignore, signum);	/* easier in psignal */
    236 			sigdelset(&p->p_sigcatch, signum);
    237 		} else {
    238 			sigdelset(&p->p_sigignore, signum);
    239 			if (nsa->sa_handler == SIG_DFL)
    240 				sigdelset(&p->p_sigcatch, signum);
    241 			else
    242 				sigaddset(&p->p_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 	register 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 	register struct sigacts *ps = p->p_sigacts;
    292 	register 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_sigignore, signum);
    308 		sigemptyset(&ps->ps_sigact[signum].sa_mask);
    309 		ps->ps_sigact[signum].sa_flags = SA_RESTART;
    310 	}
    311 	sigemptyset(&p->p_sigcatch);
    312 	p->p_flag &= ~P_NOCLDSTOP;
    313 
    314 	/*
    315 	 * Reset stack state to the user stack.
    316 	 */
    317 	ps->ps_sigstk.ss_flags = SS_DISABLE;
    318 	ps->ps_sigstk.ss_size = 0;
    319 	ps->ps_sigstk.ss_sp = 0;
    320 
    321 	/* One reference. */
    322 	ps->ps_refcnt = 1;
    323 }
    324 
    325 /*
    326  * Reset signals for an exec of the specified process.
    327  */
    328 void
    329 execsigs(p)
    330 	register struct proc *p;
    331 {
    332 	register struct sigacts *ps = p->p_sigacts;
    333 	register int signum;
    334 	int prop;
    335 
    336 	/*
    337 	 * Reset caught signals.  Held signals remain held
    338 	 * through p_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_sigcatch, signum)) {
    343 			prop = sigprop[signum];
    344 			if (prop & SA_IGNORE) {
    345 				if ((prop & SA_CONT) == 0)
    346 					sigaddset(&p->p_sigignore, signum);
    347 				sigdelset(&p->p_siglist, signum);
    348 			}
    349 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
    350 		}
    351 		sigemptyset(&ps->ps_sigact[signum].sa_mask);
    352 		ps->ps_sigact[signum].sa_flags = SA_RESTART;
    353 	}
    354 	sigemptyset(&p->p_sigcatch);
    355 	p->p_flag &= ~P_NOCLDSTOP;
    356 
    357 	/*
    358 	 * Reset stack state to the user stack.
    359 	 */
    360 	ps->ps_sigstk.ss_flags = SS_DISABLE;
    361 	ps->ps_sigstk.ss_size = 0;
    362 	ps->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_sigmask;
    375 
    376 	if (nss) {
    377 		(void)splhigh();
    378 		switch (how) {
    379 		case SIG_BLOCK:
    380 			sigplusset(nss, &p->p_sigmask);
    381 			break;
    382 		case SIG_UNBLOCK:
    383 			sigminusset(nss, &p->p_sigmask);
    384 			p->p_sigcheck = 1;
    385 			break;
    386 		case SIG_SETMASK:
    387 			p->p_sigmask = *nss;
    388 			p->p_sigcheck = 1;
    389 			break;
    390 		default:
    391 			(void)spl0();
    392 			return (EINVAL);
    393 		}
    394 		sigminusset(&sigcantmask, &p->p_sigmask);
    395 		(void)spl0();
    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 	register 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_siglist;
    445 	sigminusset(&p->p_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 	register 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 	register 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 sigacts structure
    477 		 * to indicate this.
    478 		 */
    479 		ps->ps_oldmask = p->p_sigmask;
    480 		ps->ps_flags |= SAS_OLDMASK;
    481 		(void) splhigh();
    482 		p->p_sigmask = *ss;
    483 		p->p_sigcheck = 1;
    484 		sigminusset(&sigcantmask, &p->p_sigmask);
    485 		(void) spl0();
    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 	register 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 	register struct sigacts *ps = p->p_sigacts;
    528 
    529 	if (oss)
    530 		*oss = ps->ps_sigstk;
    531 
    532 	if (nss) {
    533 		if (nss->ss_flags & ~SS_ALLBITS)
    534 			return (EINVAL);
    535 
    536 		if (nss->ss_flags & SS_DISABLE) {
    537 			if (ps->ps_sigstk.ss_flags & SS_ONSTACK)
    538 				return (EINVAL);
    539 		} else {
    540 			if (nss->ss_size < MINSIGSTKSZ)
    541 				return (ENOMEM);
    542 		}
    543 		ps->ps_sigstk = *nss;
    544 	}
    545 
    546 	return (0);
    547 }
    548 
    549 /* ARGSUSED */
    550 int
    551 sys___sigaltstack14(p, v, retval)
    552 	struct proc *p;
    553 	void *v;
    554 	register_t *retval;
    555 {
    556 	register struct sys___sigaltstack14_args /* {
    557 		syscallarg(const struct sigaltstack *) nss;
    558 		syscallarg(struct sigaltstack *) oss;
    559 	} */ *uap = v;
    560 	struct sigaltstack nss, oss;
    561 	int error;
    562 
    563 	if (SCARG(uap, nss)) {
    564 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    565 		if (error)
    566 			return (error);
    567 	}
    568 	error = sigaltstack1(p,
    569 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    570 	if (error)
    571 		return (error);
    572 	if (SCARG(uap, oss)) {
    573 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    574 		if (error)
    575 			return (error);
    576 	}
    577 	return (0);
    578 }
    579 
    580 /* ARGSUSED */
    581 int
    582 sys_kill(cp, v, retval)
    583 	register struct proc *cp;
    584 	void *v;
    585 	register_t *retval;
    586 {
    587 	register struct sys_kill_args /* {
    588 		syscallarg(int) pid;
    589 		syscallarg(int) signum;
    590 	} */ *uap = v;
    591 	register struct proc *p;
    592 	register struct pcred *pc = cp->p_cred;
    593 
    594 	if ((u_int)SCARG(uap, signum) >= NSIG)
    595 		return (EINVAL);
    596 	if (SCARG(uap, pid) > 0) {
    597 		/* kill single process */
    598 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    599 			return (ESRCH);
    600 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    601 			return (EPERM);
    602 		if (SCARG(uap, signum))
    603 			psignal(p, SCARG(uap, signum));
    604 		return (0);
    605 	}
    606 	switch (SCARG(uap, pid)) {
    607 	case -1:		/* broadcast signal */
    608 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
    609 	case 0:			/* signal own process group */
    610 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
    611 	default:		/* negative explicit process group */
    612 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
    613 	}
    614 	/* NOTREACHED */
    615 }
    616 
    617 /*
    618  * Common code for kill process group/broadcast kill.
    619  * cp is calling process.
    620  */
    621 int
    622 killpg1(cp, signum, pgid, all)
    623 	register struct proc *cp;
    624 	int signum, pgid, all;
    625 {
    626 	register struct proc *p;
    627 	register struct pcred *pc = cp->p_cred;
    628 	struct pgrp *pgrp;
    629 	int nfound = 0;
    630 
    631 	if (all) {
    632 		/*
    633 		 * broadcast
    634 		 */
    635 		proclist_lock_read();
    636 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    637 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    638 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    639 				continue;
    640 			nfound++;
    641 			if (signum)
    642 				psignal(p, signum);
    643 		}
    644 		proclist_unlock_read();
    645 	} else {
    646 		if (pgid == 0)
    647 			/*
    648 			 * zero pgid means send to my process group.
    649 			 */
    650 			pgrp = cp->p_pgrp;
    651 		else {
    652 			pgrp = pgfind(pgid);
    653 			if (pgrp == NULL)
    654 				return (ESRCH);
    655 		}
    656 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    657 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    658 			    !CANSIGNAL(cp, pc, p, signum))
    659 				continue;
    660 			nfound++;
    661 			if (signum && P_ZOMBIE(p) == 0)
    662 				psignal(p, signum);
    663 		}
    664 	}
    665 	return (nfound ? 0 : ESRCH);
    666 }
    667 
    668 /*
    669  * Send a signal to a process group.
    670  */
    671 void
    672 gsignal(pgid, signum)
    673 	int pgid, signum;
    674 {
    675 	struct pgrp *pgrp;
    676 
    677 	if (pgid && (pgrp = pgfind(pgid)))
    678 		pgsignal(pgrp, signum, 0);
    679 }
    680 
    681 /*
    682  * Send a signal to a process group. If checktty is 1,
    683  * limit to members which have a controlling terminal.
    684  */
    685 void
    686 pgsignal(pgrp, signum, checkctty)
    687 	struct pgrp *pgrp;
    688 	int signum, checkctty;
    689 {
    690 	register struct proc *p;
    691 
    692 	if (pgrp)
    693 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
    694 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    695 				psignal(p, signum);
    696 }
    697 
    698 /*
    699  * Send a signal caused by a trap to the current process.
    700  * If it will be caught immediately, deliver it with correct code.
    701  * Otherwise, post it normally.
    702  */
    703 void
    704 trapsignal(p, signum, code)
    705 	struct proc *p;
    706 	register int signum;
    707 	u_long code;
    708 {
    709 	register struct sigacts *ps = p->p_sigacts;
    710 
    711 	if ((p->p_flag & P_TRACED) == 0 &&
    712 	    sigismember(&p->p_sigcatch, signum) &&
    713 	    !sigismember(&p->p_sigmask, signum)) {
    714 		p->p_stats->p_ru.ru_nsignals++;
    715 #ifdef KTRACE
    716 		if (KTRPOINT(p, KTR_PSIG))
    717 			ktrpsig(p->p_tracep, signum,
    718 			    ps->ps_sigact[signum].sa_handler, &p->p_sigmask,
    719 			    code);
    720 #endif
    721 		(*p->p_emul->e_sendsig)(ps->ps_sigact[signum].sa_handler,
    722 		    signum, &p->p_sigmask, code);
    723 		(void) splhigh();
    724 		sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
    725 		if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
    726 			sigdelset(&p->p_sigcatch, signum);
    727 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    728 				sigaddset(&p->p_sigignore, signum);
    729 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
    730 		}
    731 		(void) spl0();
    732 	} else {
    733 		ps->ps_code = code;	/* XXX for core dump/debugger */
    734 		ps->ps_sig = signum;	/* XXX to verify code */
    735 		psignal(p, signum);
    736 	}
    737 }
    738 
    739 /*
    740  * Send the signal to the process.  If the signal has an action, the action
    741  * is usually performed by the target process rather than the caller; we add
    742  * the signal to the set of pending signals for the process.
    743  *
    744  * Exceptions:
    745  *   o When a stop signal is sent to a sleeping process that takes the
    746  *     default action, the process is stopped without awakening it.
    747  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    748  *     regardless of the signal action (eg, blocked or ignored).
    749  *
    750  * Other ignored signals are discarded immediately.
    751  */
    752 void
    753 psignal(p, signum)
    754 	register struct proc *p;
    755 	register int signum;
    756 {
    757 	register int s, prop;
    758 	register sig_t action;
    759 
    760 #ifdef DIAGNOSTIC
    761 	if (signum <= 0 || signum >= NSIG)
    762 		panic("psignal signal number");
    763 #endif
    764 	prop = sigprop[signum];
    765 
    766 	/*
    767 	 * If proc is traced, always give parent a chance.
    768 	 */
    769 	if (p->p_flag & P_TRACED)
    770 		action = SIG_DFL;
    771 	else {
    772 		/*
    773 		 * If the signal is being ignored,
    774 		 * then we forget about it immediately.
    775 		 * (Note: we don't set SIGCONT in p_sigignore,
    776 		 * and if it is set to SIG_IGN,
    777 		 * action will be SIG_DFL here.)
    778 		 */
    779 		if (sigismember(&p->p_sigignore, signum))
    780 			return;
    781 		if (sigismember(&p->p_sigmask, signum))
    782 			action = SIG_HOLD;
    783 		else if (sigismember(&p->p_sigcatch, signum))
    784 			action = SIG_CATCH;
    785 		else {
    786 			action = SIG_DFL;
    787 
    788 			if (prop & SA_KILL && p->p_nice > NZERO)
    789 				p->p_nice = NZERO;
    790 
    791 			/*
    792 			 * If sending a tty stop signal to a member of an
    793 			 * orphaned process group, discard the signal here if
    794 			 * the action is default; don't stop the process below
    795 			 * if sleeping, and don't clear any pending SIGCONT.
    796 			 */
    797 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
    798 				return;
    799 		}
    800 	}
    801 
    802 	if (prop & SA_CONT)
    803 		sigminusset(&stopsigmask, &p->p_siglist);
    804 
    805 	if (prop & SA_STOP)
    806 		sigminusset(&contsigmask, &p->p_siglist);
    807 
    808 	sigaddset(&p->p_siglist, signum);
    809 	p->p_sigcheck = 1;
    810 
    811 	/*
    812 	 * Defer further processing for signals which are held,
    813 	 * except that stopped processes must be continued by SIGCONT.
    814 	 */
    815 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
    816 		return;
    817 	s = splhigh();
    818 	switch (p->p_stat) {
    819 
    820 	case SSLEEP:
    821 		/*
    822 		 * If process is sleeping uninterruptibly
    823 		 * we can't interrupt the sleep... the signal will
    824 		 * be noticed when the process returns through
    825 		 * trap() or syscall().
    826 		 */
    827 		if ((p->p_flag & P_SINTR) == 0)
    828 			goto out;
    829 		/*
    830 		 * Process is sleeping and traced... make it runnable
    831 		 * so it can discover the signal in issignal() and stop
    832 		 * for the parent.
    833 		 */
    834 		if (p->p_flag & P_TRACED)
    835 			goto run;
    836 		/*
    837 		 * If SIGCONT is default (or ignored) and process is
    838 		 * asleep, we are finished; the process should not
    839 		 * be awakened.
    840 		 */
    841 		if ((prop & SA_CONT) && action == SIG_DFL) {
    842 			sigdelset(&p->p_siglist, signum);
    843 			goto out;
    844 		}
    845 		/*
    846 		 * When a sleeping process receives a stop
    847 		 * signal, process immediately if possible.
    848 		 */
    849 		if ((prop & SA_STOP) && action == SIG_DFL) {
    850 			/*
    851 			 * If a child holding parent blocked,
    852 			 * stopping could cause deadlock.
    853 			 */
    854 			if (p->p_flag & P_PPWAIT)
    855 				goto out;
    856 			sigdelset(&p->p_siglist, signum);
    857 			p->p_xstat = signum;
    858 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
    859 				psignal(p->p_pptr, SIGCHLD);
    860 			stop(p);
    861 			goto out;
    862 		}
    863 		/*
    864 		 * All other (caught or default) signals
    865 		 * cause the process to run.
    866 		 */
    867 		goto runfast;
    868 		/*NOTREACHED*/
    869 
    870 	case SSTOP:
    871 		/*
    872 		 * If traced process is already stopped,
    873 		 * then no further action is necessary.
    874 		 */
    875 		if (p->p_flag & P_TRACED)
    876 			goto out;
    877 
    878 		/*
    879 		 * Kill signal always sets processes running.
    880 		 */
    881 		if (signum == SIGKILL)
    882 			goto runfast;
    883 
    884 		if (prop & SA_CONT) {
    885 			/*
    886 			 * If SIGCONT is default (or ignored), we continue the
    887 			 * process but don't leave the signal in p_siglist, as
    888 			 * it has no further action.  If SIGCONT is held, we
    889 			 * continue the process and leave the signal in
    890 			 * p_siglist.  If the process catches SIGCONT, let it
    891 			 * handle the signal itself.  If it isn't waiting on
    892 			 * an event, then it goes back to run state.
    893 			 * Otherwise, process goes back to sleep state.
    894 			 */
    895 			if (action == SIG_DFL)
    896 				sigdelset(&p->p_siglist, signum);
    897 			if (action == SIG_CATCH)
    898 				goto runfast;
    899 			if (p->p_wchan == 0)
    900 				goto run;
    901 			p->p_stat = SSLEEP;
    902 			goto out;
    903 		}
    904 
    905 		if (prop & SA_STOP) {
    906 			/*
    907 			 * Already stopped, don't need to stop again.
    908 			 * (If we did the shell could get confused.)
    909 			 */
    910 			sigdelset(&p->p_siglist, signum);
    911 			goto out;
    912 		}
    913 
    914 		/*
    915 		 * If process is sleeping interruptibly, then simulate a
    916 		 * wakeup so that when it is continued, it will be made
    917 		 * runnable and can look at the signal.  But don't make
    918 		 * the process runnable, leave it stopped.
    919 		 */
    920 		if (p->p_wchan && p->p_flag & P_SINTR)
    921 			unsleep(p);
    922 		goto out;
    923 
    924 	default:
    925 		/*
    926 		 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal,
    927 		 * other than kicking ourselves if we are running.
    928 		 * It will either never be noticed, or noticed very soon.
    929 		 */
    930 		if (p == curproc)
    931 			signotify(p);
    932 		goto out;
    933 	}
    934 	/*NOTREACHED*/
    935 
    936 runfast:
    937 	/*
    938 	 * Raise priority to at least PUSER.
    939 	 */
    940 	if (p->p_priority > PUSER)
    941 		p->p_priority = PUSER;
    942 run:
    943 	setrunnable(p);
    944 out:
    945 	splx(s);
    946 }
    947 
    948 static __inline int firstsig __P((const sigset_t *));
    949 
    950 static __inline int
    951 firstsig(ss)
    952 	const sigset_t *ss;
    953 {
    954 	int sig;
    955 
    956 	sig = ffs(ss->__bits[0]);
    957 	if (sig != 0)
    958 		return (sig);
    959 #if NSIG > 33
    960 	sig = ffs(ss->__bits[1]);
    961 	if (sig != 0)
    962 		return (sig + 32);
    963 #endif
    964 #if NSIG > 65
    965 	sig = ffs(ss->__bits[2]);
    966 	if (sig != 0)
    967 		return (sig + 64);
    968 #endif
    969 #if NSIG > 97
    970 	sig = ffs(ss->__bits[3]);
    971 	if (sig != 0)
    972 		return (sig + 96);
    973 #endif
    974 	return (0);
    975 }
    976 
    977 /*
    978  * If the current process has received a signal (should be caught or cause
    979  * termination, should interrupt current syscall), return the signal number.
    980  * Stop signals with default action are processed immediately, then cleared;
    981  * they aren't returned.  This is checked after each entry to the system for
    982  * a syscall or trap (though this can usually be done without calling issignal
    983  * by checking the pending signal masks in the CURSIG macro.) The normal call
    984  * sequence is
    985  *
    986  *	while (signum = CURSIG(curproc))
    987  *		postsig(signum);
    988  */
    989 int
    990 issignal(p)
    991 	register struct proc *p;
    992 {
    993 	register int signum, prop;
    994 	sigset_t ss;
    995 
    996 	for (;;) {
    997 		sigpending1(p, &ss);
    998 		if (p->p_flag & P_PPWAIT)
    999 			sigminusset(&stopsigmask, &ss);
   1000 		signum = firstsig(&ss);
   1001 		if (signum == 0) {		 	/* no signal to send */
   1002 			p->p_sigcheck = 0;
   1003 			return (0);
   1004 		}
   1005 		sigdelset(&p->p_siglist, signum);	/* take the signal! */
   1006 
   1007 		/*
   1008 		 * We should see pending but ignored signals
   1009 		 * only if P_TRACED was on when they were posted.
   1010 		 */
   1011 		if (sigismember(&p->p_sigignore, signum) &&
   1012 		    (p->p_flag & P_TRACED) == 0)
   1013 			continue;
   1014 
   1015 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1016 			/*
   1017 			 * If traced, always stop, and stay
   1018 			 * stopped until released by the debugger.
   1019 			 */
   1020 			p->p_xstat = signum;
   1021 			if ((p->p_flag & P_FSTRACE) == 0)
   1022 				psignal(p->p_pptr, SIGCHLD);
   1023 			do {
   1024 				stop(p);
   1025 				mi_switch();
   1026 			} while (!trace_req(p) && p->p_flag & P_TRACED);
   1027 
   1028 			/*
   1029 			 * If we are no longer being traced, or the parent
   1030 			 * didn't give us a signal, look for more signals.
   1031 			 */
   1032 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1033 				continue;
   1034 
   1035 			/*
   1036 			 * If the new signal is being masked, look for other
   1037 			 * signals.
   1038 			 */
   1039 			signum = p->p_xstat;
   1040 			/* `p->p_siglist |= mask' is done in setrunnable(). */
   1041 			if (sigismember(&p->p_sigmask, signum))
   1042 				continue;
   1043 			sigdelset(&p->p_siglist, signum);	/* take the signal! */
   1044 		}
   1045 
   1046 		prop = sigprop[signum];
   1047 
   1048 		/*
   1049 		 * Decide whether the signal should be returned.
   1050 		 * Return the signal's number, or fall through
   1051 		 * to clear it from the pending mask.
   1052 		 */
   1053 		switch ((long)p->p_sigacts->ps_sigact[signum].sa_handler) {
   1054 
   1055 		case (long)SIG_DFL:
   1056 			/*
   1057 			 * Don't take default actions on system processes.
   1058 			 */
   1059 			if (p->p_pid <= 1) {
   1060 #ifdef DIAGNOSTIC
   1061 				/*
   1062 				 * Are you sure you want to ignore SIGSEGV
   1063 				 * in init? XXX
   1064 				 */
   1065 				printf("Process (pid %d) got signal %d\n",
   1066 				    p->p_pid, signum);
   1067 #endif
   1068 				break;		/* == ignore */
   1069 			}
   1070 			/*
   1071 			 * If there is a pending stop signal to process
   1072 			 * with default action, stop here,
   1073 			 * then clear the signal.  However,
   1074 			 * if process is member of an orphaned
   1075 			 * process group, ignore tty stop signals.
   1076 			 */
   1077 			if (prop & SA_STOP) {
   1078 				if (p->p_flag & P_TRACED ||
   1079 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1080 				    prop & SA_TTYSTOP))
   1081 					break;	/* == ignore */
   1082 				p->p_xstat = signum;
   1083 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1084 					psignal(p->p_pptr, SIGCHLD);
   1085 				stop(p);
   1086 				mi_switch();
   1087 				break;
   1088 			} else if (prop & SA_IGNORE) {
   1089 				/*
   1090 				 * Except for SIGCONT, shouldn't get here.
   1091 				 * Default action is to ignore; drop it.
   1092 				 */
   1093 				break;		/* == ignore */
   1094 			} else
   1095 				goto keep;
   1096 			/*NOTREACHED*/
   1097 
   1098 		case (long)SIG_IGN:
   1099 			/*
   1100 			 * Masking above should prevent us ever trying
   1101 			 * to take action on an ignored signal other
   1102 			 * than SIGCONT, unless process is traced.
   1103 			 */
   1104 			if ((prop & SA_CONT) == 0 &&
   1105 			    (p->p_flag & P_TRACED) == 0)
   1106 				printf("issignal\n");
   1107 			break;		/* == ignore */
   1108 
   1109 		default:
   1110 			/*
   1111 			 * This signal has an action, let
   1112 			 * postsig() process it.
   1113 			 */
   1114 			goto keep;
   1115 		}
   1116 	}
   1117 	/* NOTREACHED */
   1118 
   1119 keep:
   1120 	sigaddset(&p->p_siglist, signum);	/* leave the signal for later */
   1121 	p->p_sigcheck = 1;
   1122 	return (signum);
   1123 }
   1124 
   1125 /*
   1126  * Put the argument process into the stopped state and notify the parent
   1127  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1128  * on the run queue.
   1129  */
   1130 void
   1131 stop(p)
   1132 	register struct proc *p;
   1133 {
   1134 
   1135 	p->p_stat = SSTOP;
   1136 	p->p_flag &= ~P_WAITED;
   1137 	wakeup((caddr_t)p->p_pptr);
   1138 }
   1139 
   1140 /*
   1141  * Take the action for the specified signal
   1142  * from the current set of pending signals.
   1143  */
   1144 void
   1145 postsig(signum)
   1146 	register int signum;
   1147 {
   1148 	register struct proc *p = curproc;
   1149 	register struct sigacts *ps = p->p_sigacts;
   1150 	register sig_t action;
   1151 	u_long code;
   1152 	sigset_t *returnmask;
   1153 
   1154 #ifdef DIAGNOSTIC
   1155 	if (signum == 0)
   1156 		panic("postsig");
   1157 #endif
   1158 	sigdelset(&p->p_siglist, signum);
   1159 	action = ps->ps_sigact[signum].sa_handler;
   1160 #ifdef KTRACE
   1161 	if (KTRPOINT(p, KTR_PSIG))
   1162 		ktrpsig(p->p_tracep,
   1163 		    signum, action, ps->ps_flags & SAS_OLDMASK ?
   1164 		    &ps->ps_oldmask : &p->p_sigmask, 0);
   1165 #endif
   1166 	if (action == SIG_DFL) {
   1167 		/*
   1168 		 * Default action, where the default is to kill
   1169 		 * the process.  (Other cases were ignored above.)
   1170 		 */
   1171 		sigexit(p, signum);
   1172 		/* NOTREACHED */
   1173 	} else {
   1174 		/*
   1175 		 * If we get here, the signal must be caught.
   1176 		 */
   1177 #ifdef DIAGNOSTIC
   1178 		if (action == SIG_IGN || sigismember(&p->p_sigmask, signum))
   1179 			panic("postsig action");
   1180 #endif
   1181 		/*
   1182 		 * Set the new mask value and also defer further
   1183 		 * occurences of this signal.
   1184 		 *
   1185 		 * Special case: user has done a sigpause.  Here the
   1186 		 * current mask is not of interest, but rather the
   1187 		 * mask from before the sigpause is what we want
   1188 		 * restored after the signal processing is completed.
   1189 		 */
   1190 		if (ps->ps_flags & SAS_OLDMASK) {
   1191 			returnmask = &ps->ps_oldmask;
   1192 			ps->ps_flags &= ~SAS_OLDMASK;
   1193 		} else
   1194 			returnmask = &p->p_sigmask;
   1195 		p->p_stats->p_ru.ru_nsignals++;
   1196 		if (ps->ps_sig != signum) {
   1197 			code = 0;
   1198 		} else {
   1199 			code = ps->ps_code;
   1200 			ps->ps_code = 0;
   1201 			ps->ps_sig = 0;
   1202 		}
   1203 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code);
   1204 		(void) splhigh();
   1205 		sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
   1206 		if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
   1207 			sigdelset(&p->p_sigcatch, signum);
   1208 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1209 				sigaddset(&p->p_sigignore, signum);
   1210 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
   1211 		}
   1212 		(void) spl0();
   1213 	}
   1214 }
   1215 
   1216 /*
   1217  * Kill the current process for stated reason.
   1218  */
   1219 void
   1220 killproc(p, why)
   1221 	struct proc *p;
   1222 	char *why;
   1223 {
   1224 
   1225 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1226 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1227 	psignal(p, SIGKILL);
   1228 }
   1229 
   1230 /*
   1231  * Force the current process to exit with the specified signal, dumping core
   1232  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1233  * allowing unrecoverable failures to terminate the process without changing
   1234  * signal state.  Mark the accounting record with the signal termination.
   1235  * If dumping core, save the signal number for the debugger.  Calls exit and
   1236  * does not return.
   1237  */
   1238 void
   1239 sigexit(p, signum)
   1240 	register struct proc *p;
   1241 	int signum;
   1242 {
   1243 
   1244 	p->p_acflag |= AXSIG;
   1245 	if (sigprop[signum] & SA_CORE) {
   1246 		p->p_sigacts->ps_sig = signum;
   1247 		if (coredump(p) == 0)
   1248 			signum |= WCOREFLAG;
   1249 	}
   1250 	exit1(p, W_EXITCODE(0, signum));
   1251 	/* NOTREACHED */
   1252 }
   1253 
   1254 /*
   1255  * Dump core, into a file named "progname.core" or "core" (depending on the
   1256  * value of shortcorename), unless the process was setuid/setgid.
   1257  */
   1258 int
   1259 coredump(p)
   1260 	register struct proc *p;
   1261 {
   1262 	register struct vnode *vp;
   1263 	register struct vmspace *vm = p->p_vmspace;
   1264 	register struct ucred *cred = p->p_cred->pc_ucred;
   1265 	struct nameidata nd;
   1266 	struct vattr vattr;
   1267 	int error, error1;
   1268 	char name[MAXPATHLEN];
   1269 	struct core core;
   1270 
   1271 	/*
   1272 	 * Make sure the process has not set-id, to prevent data leaks.
   1273 	 */
   1274 	if (p->p_flag & P_SUGID)
   1275 		return (EPERM);
   1276 
   1277 	/*
   1278 	 * Refuse to core if the data + stack + user size is larger than
   1279 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
   1280 	 * data.
   1281 	 */
   1282 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   1283 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   1284 		return (EFBIG);		/* better error code? */
   1285 
   1286 	/*
   1287 	 * The core dump will go in the current working directory.  Make
   1288 	 * sure that the directory is still there and that the mount flags
   1289 	 * allow us to write core dumps there.
   1290 	 */
   1291 	vp = p->p_cwdi->cwdi_cdir;
   1292 	if (vp->v_mount == NULL ||
   1293 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
   1294 		return (EPERM);
   1295 
   1296 	error = build_corename(name);
   1297 	if (error)
   1298 		return error;
   1299 
   1300 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
   1301 	error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR);
   1302 	if (error)
   1303 		return (error);
   1304 	vp = nd.ni_vp;
   1305 
   1306 	/* Don't dump to non-regular files or files with links. */
   1307 	if (vp->v_type != VREG ||
   1308 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
   1309 		error = EINVAL;
   1310 		goto out;
   1311 	}
   1312 	VATTR_NULL(&vattr);
   1313 	vattr.va_size = 0;
   1314 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1315 	VOP_SETATTR(vp, &vattr, cred, p);
   1316 	p->p_acflag |= ACORE;
   1317 #if 0
   1318 	/*
   1319 	 * XXX
   1320 	 * It would be nice if we at least dumped the signal state (and made it
   1321 	 * available at run time to the debugger, as well), but this code
   1322 	 * hasn't actually had any effect for a long time, since we don't dump
   1323 	 * the user area.  For now, it's dead.
   1324 	 */
   1325 	memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
   1326 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
   1327 #endif
   1328 
   1329 	core.c_midmag = 0;
   1330 	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
   1331 	core.c_nseg = 0;
   1332 	core.c_signo = p->p_sigacts->ps_sig;
   1333 	core.c_ucode = p->p_sigacts->ps_code;
   1334 	core.c_cpusize = 0;
   1335 	core.c_tsize = (u_long)ctob(vm->vm_tsize);
   1336 	core.c_dsize = (u_long)ctob(vm->vm_dsize);
   1337 	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
   1338 	error = cpu_coredump(p, vp, cred, &core);
   1339 	if (error)
   1340 		goto out;
   1341 	if (core.c_midmag == 0) {
   1342 		/* XXX
   1343 		 * cpu_coredump() didn't bother to set the magic; assume
   1344 		 * this is a request to do a traditional dump. cpu_coredump()
   1345 		 * is still responsible for setting sensible values in
   1346 		 * the core header.
   1347 		 */
   1348 		if (core.c_cpusize == 0)
   1349 			core.c_cpusize = USPACE; /* Just in case */
   1350 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
   1351 		    (int)core.c_dsize,
   1352 		    (off_t)core.c_cpusize, UIO_USERSPACE,
   1353 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
   1354 		if (error)
   1355 			goto out;
   1356 		error = vn_rdwr(UIO_WRITE, vp,
   1357 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
   1358 		    core.c_ssize,
   1359 		    (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
   1360 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
   1361 	} else {
   1362 		/*
   1363 		 * uvm_coredump() spits out all appropriate segments.
   1364 		 * All that's left to do is to write the core header.
   1365 		 */
   1366 		error = uvm_coredump(p, vp, cred, &core);
   1367 		if (error)
   1368 			goto out;
   1369 		error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
   1370 		    (int)core.c_hdrsize, (off_t)0,
   1371 		    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
   1372 	}
   1373 out:
   1374 	VOP_UNLOCK(vp, 0);
   1375 	error1 = vn_close(vp, FWRITE, cred, p);
   1376 	if (error == 0)
   1377 		error = error1;
   1378 	return (error);
   1379 }
   1380 
   1381 /*
   1382  * Nonexistent system call-- signal process (may want to handle it).
   1383  * Flag error in case process won't see signal immediately (blocked or ignored).
   1384  */
   1385 /* ARGSUSED */
   1386 int
   1387 sys_nosys(p, v, retval)
   1388 	struct proc *p;
   1389 	void *v;
   1390 	register_t *retval;
   1391 {
   1392 
   1393 	psignal(p, SIGSYS);
   1394 	return (ENOSYS);
   1395 }
   1396 
   1397 static int
   1398 build_corename(dst)
   1399 	char *dst;
   1400 {
   1401 	const char *s;
   1402 	char *d;
   1403 	int len, i;
   1404 
   1405 	for (s = curproc->p_limit->pl_corename, len = 0, d = dst;
   1406 	    *s != '\0'; s++) {
   1407 		if (*s == '%') {
   1408 			switch (*(s+1)) {
   1409 			case 'n':
   1410 				i = snprintf(d,MAXPATHLEN - 1 - len, "%s",
   1411 				    curproc->p_comm);
   1412 				break;
   1413 			case 'p':
   1414 				i = snprintf(d, MAXPATHLEN - 1 - len, "%d",
   1415 				    curproc->p_pid);
   1416 				break;
   1417 			case 'u':
   1418 				i = snprintf(d, MAXPATHLEN - 1 - len, "%s",
   1419 				    curproc->p_pgrp->pg_session->s_login);
   1420 				break;
   1421 			case 't':
   1422 				i = snprintf(d, MAXPATHLEN - 1 - len, "%ld",
   1423 				    curproc->p_stats->p_start.tv_sec);
   1424 				break;
   1425 			default:
   1426 				goto copy;
   1427 			}
   1428 			if (i >= MAXPATHLEN - 1 - len)
   1429 				return ENAMETOOLONG;
   1430 			len += i;
   1431 			d += i;
   1432 			s++;
   1433 		} else {
   1434 copy:			*d = *s;
   1435 			d++;
   1436 			len++;
   1437 			if (len >= MAXPATHLEN - 1)
   1438 				return ENAMETOOLONG;
   1439 		}
   1440 	}
   1441 	*d = '\0';
   1442 	return 0;
   1443 }
   1444