Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.92
      1 /*	$NetBSD: kern_sig.c,v 1.92 1999/07/25 06:30:34 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     41  */
     42 
     43 #include "opt_ktrace.h"
     44 #include "opt_compat_sunos.h"
     45 
     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 
     83 sigset_t contsigmask, stopsigmask, sigcantmask;
     84 
     85 struct pool sigacts_pool;	/* memory pool for sigacts structures */
     86 
     87 /*
     88  * Can process p, with pcred pc, send the signal signum to process q?
     89  */
     90 #define CANSIGNAL(p, pc, q, signum) \
     91 	((pc)->pc_ucred->cr_uid == 0 || \
     92 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
     93 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
     94 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
     95 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
     96 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
     97 
     98 /*
     99  * Initialize signal-related data structures.
    100  */
    101 void
    102 signal_init()
    103 {
    104 
    105 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    106 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    107 }
    108 
    109 /*
    110  * Create an initial sigacts structure, using the same signal state
    111  * as p.
    112  */
    113 struct sigacts *
    114 sigactsinit(p)
    115 	struct proc *p;
    116 {
    117 	struct sigacts *ps;
    118 
    119 	ps = pool_get(&sigacts_pool, PR_WAITOK);
    120 	memcpy(ps, p->p_sigacts, sizeof(struct sigacts));
    121 	ps->ps_refcnt = 1;
    122 	return (ps);
    123 }
    124 
    125 /*
    126  * Make p2 share p1's sigacts.
    127  */
    128 void
    129 sigactsshare(p1, p2)
    130 	struct proc *p1, *p2;
    131 {
    132 
    133 	p2->p_sigacts = p1->p_sigacts;
    134 	p1->p_sigacts->ps_refcnt++;
    135 }
    136 
    137 /*
    138  * Make this process not share its sigacts, maintaining all
    139  * signal state.
    140  */
    141 void
    142 sigactsunshare(p)
    143 	struct proc *p;
    144 {
    145 	struct sigacts *newps;
    146 
    147 	if (p->p_sigacts->ps_refcnt == 1)
    148 		return;
    149 
    150 	newps = sigactsinit(p);
    151 	sigactsfree(p);
    152 	p->p_sigacts = newps;
    153 }
    154 
    155 /*
    156  * Release a sigacts structure.
    157  */
    158 void
    159 sigactsfree(p)
    160 	struct proc *p;
    161 {
    162 	struct sigacts *ps = p->p_sigacts;
    163 
    164 	if (--ps->ps_refcnt > 0)
    165 		return;
    166 
    167 	p->p_sigacts = NULL;
    168 
    169 	pool_put(&sigacts_pool, ps);
    170 }
    171 
    172 int
    173 sigaction1(p, signum, nsa, osa)
    174 	struct proc *p;
    175 	int signum;
    176 	const struct sigaction *nsa;
    177 	struct sigaction *osa;
    178 {
    179 	register struct sigacts *ps = p->p_sigacts;
    180 	int prop;
    181 
    182 	if (signum <= 0 || signum >= NSIG)
    183 		return (EINVAL);
    184 
    185 	if (osa)
    186 		*osa = ps->ps_sigact[signum];
    187 
    188 	if (nsa) {
    189 		if (nsa->sa_flags & ~SA_ALLBITS)
    190 			return (EINVAL);
    191 
    192 		prop = sigprop[signum];
    193 		if (prop & SA_CANTMASK)
    194 			return (EINVAL);
    195 
    196 		(void) splhigh();
    197 		ps->ps_sigact[signum] = *nsa;
    198 		sigminusset(&sigcantmask, &ps->ps_sigact[signum].sa_mask);
    199 		if ((prop & SA_NORESET) != 0)
    200 			ps->ps_sigact[signum].sa_flags &= ~SA_RESETHAND;
    201 		if (signum == SIGCHLD) {
    202 			if (nsa->sa_flags & SA_NOCLDSTOP)
    203 				p->p_flag |= P_NOCLDSTOP;
    204 			else
    205 				p->p_flag &= ~P_NOCLDSTOP;
    206 			if (nsa->sa_flags & SA_NOCLDWAIT) {
    207 				/*
    208 				 * Paranoia: since SA_NOCLDWAIT is implemented
    209 				 * by reparenting the dying child to PID 1 (and
    210 				 * trust it to reap the zombie), PID 1 itself is
    211 				 * forbidden to set SA_NOCLDWAIT.
    212 				 */
    213 				if (p->p_pid == 1)
    214 					p->p_flag &= ~P_NOCLDWAIT;
    215 				else
    216 					p->p_flag |= P_NOCLDWAIT;
    217 			} else
    218 				p->p_flag &= ~P_NOCLDWAIT;
    219 		}
    220 		if ((nsa->sa_flags & SA_NODEFER) == 0)
    221 			sigaddset(&ps->ps_sigact[signum].sa_mask, signum);
    222 		else
    223 			sigdelset(&ps->ps_sigact[signum].sa_mask, signum);
    224 		/*
    225 	 	* Set bit in p_sigignore for signals that are set to SIG_IGN,
    226 	 	* and for signals set to SIG_DFL where the default is to ignore.
    227 	 	* However, don't put SIGCONT in p_sigignore,
    228 	 	* as we have to restart the process.
    229 	 	*/
    230 		if (nsa->sa_handler == SIG_IGN ||
    231 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    232 			sigdelset(&p->p_siglist, signum);	/* never to be seen again */
    233 			if (signum != SIGCONT)
    234 				sigaddset(&p->p_sigignore, signum);	/* easier in psignal */
    235 			sigdelset(&p->p_sigcatch, signum);
    236 		} else {
    237 			sigdelset(&p->p_sigignore, signum);
    238 			if (nsa->sa_handler == SIG_DFL)
    239 				sigdelset(&p->p_sigcatch, signum);
    240 			else
    241 				sigaddset(&p->p_sigcatch, signum);
    242 		}
    243 		(void) spl0();
    244 	}
    245 
    246 	return (0);
    247 }
    248 
    249 /* ARGSUSED */
    250 int
    251 sys___sigaction14(p, v, retval)
    252 	struct proc *p;
    253 	void *v;
    254 	register_t *retval;
    255 {
    256 	register struct sys___sigaction14_args /* {
    257 		syscallarg(int) signum;
    258 		syscallarg(const struct sigaction *) nsa;
    259 		syscallarg(struct sigaction *) osa;
    260 	} */ *uap = v;
    261 	struct sigaction nsa, osa;
    262 	int error;
    263 
    264 	if (SCARG(uap, nsa)) {
    265 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    266 		if (error)
    267 			return (error);
    268 	}
    269 	error = sigaction1(p, SCARG(uap, signum),
    270 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0);
    271 	if (error)
    272 		return (error);
    273 	if (SCARG(uap, osa)) {
    274 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    275 		if (error)
    276 			return (error);
    277 	}
    278 	return (0);
    279 }
    280 
    281 /*
    282  * Initialize signal state for process 0;
    283  * set to ignore signals that are ignored by default and disable the signal
    284  * stack.
    285  */
    286 void
    287 siginit(p)
    288 	struct proc *p;
    289 {
    290 	register struct sigacts *ps = p->p_sigacts;
    291 	register int signum;
    292 	int prop;
    293 
    294 	sigemptyset(&contsigmask);
    295 	sigemptyset(&stopsigmask);
    296 	sigemptyset(&sigcantmask);
    297 	for (signum = 1; signum < NSIG; signum++) {
    298 		prop = sigprop[signum];
    299 		if (prop & SA_CONT)
    300 			sigaddset(&contsigmask, signum);
    301 		if (prop & SA_STOP)
    302 			sigaddset(&stopsigmask, signum);
    303 		if (prop & SA_CANTMASK)
    304 			sigaddset(&sigcantmask, signum);
    305 		if (prop & SA_IGNORE && signum != SIGCONT)
    306 			sigaddset(&p->p_sigignore, signum);
    307 		sigemptyset(&ps->ps_sigact[signum].sa_mask);
    308 		ps->ps_sigact[signum].sa_flags = SA_RESTART;
    309 	}
    310 	sigemptyset(&p->p_sigcatch);
    311 	p->p_flag &= ~P_NOCLDSTOP;
    312 
    313 	/*
    314 	 * Reset stack state to the user stack.
    315 	 */
    316 	ps->ps_sigstk.ss_flags = SS_DISABLE;
    317 	ps->ps_sigstk.ss_size = 0;
    318 	ps->ps_sigstk.ss_sp = 0;
    319 
    320 	/* One reference. */
    321 	ps->ps_refcnt = 1;
    322 }
    323 
    324 /*
    325  * Reset signals for an exec of the specified process.
    326  */
    327 void
    328 execsigs(p)
    329 	register struct proc *p;
    330 {
    331 	register struct sigacts *ps = p->p_sigacts;
    332 	register int signum;
    333 	int prop;
    334 
    335 	/*
    336 	 * Reset caught signals.  Held signals remain held
    337 	 * through p_sigmask (unless they were caught,
    338 	 * and are now ignored by default).
    339 	 */
    340 	for (signum = 1; signum < NSIG; signum++) {
    341 		if (sigismember(&p->p_sigcatch, signum)) {
    342 			prop = sigprop[signum];
    343 			if (prop & SA_IGNORE) {
    344 				if ((prop & SA_CONT) == 0)
    345 					sigaddset(&p->p_sigignore, signum);
    346 				sigdelset(&p->p_siglist, signum);
    347 			}
    348 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
    349 		}
    350 		sigemptyset(&ps->ps_sigact[signum].sa_mask);
    351 		ps->ps_sigact[signum].sa_flags = SA_RESTART;
    352 	}
    353 	sigemptyset(&p->p_sigcatch);
    354 	p->p_flag &= ~P_NOCLDSTOP;
    355 
    356 	/*
    357 	 * Reset stack state to the user stack.
    358 	 */
    359 	ps->ps_sigstk.ss_flags = SS_DISABLE;
    360 	ps->ps_sigstk.ss_size = 0;
    361 	ps->ps_sigstk.ss_sp = 0;
    362 }
    363 
    364 int
    365 sigprocmask1(p, how, nss, oss)
    366 	struct proc *p;
    367 	int how;
    368 	const sigset_t *nss;
    369 	sigset_t *oss;
    370 {
    371 
    372 	if (oss)
    373 		*oss = p->p_sigmask;
    374 
    375 	if (nss) {
    376 		(void)splhigh();
    377 		switch (how) {
    378 		case SIG_BLOCK:
    379 			sigplusset(nss, &p->p_sigmask);
    380 			break;
    381 		case SIG_UNBLOCK:
    382 			sigminusset(nss, &p->p_sigmask);
    383 			p->p_sigcheck = 1;
    384 			break;
    385 		case SIG_SETMASK:
    386 			p->p_sigmask = *nss;
    387 			p->p_sigcheck = 1;
    388 			break;
    389 		default:
    390 			(void)spl0();
    391 			return (EINVAL);
    392 		}
    393 		sigminusset(&sigcantmask, &p->p_sigmask);
    394 		(void)spl0();
    395 	}
    396 
    397 	return (0);
    398 }
    399 
    400 /*
    401  * Manipulate signal mask.
    402  * Note that we receive new mask, not pointer,
    403  * and return old mask as return value;
    404  * the library stub does the rest.
    405  */
    406 int
    407 sys___sigprocmask14(p, v, retval)
    408 	register struct proc *p;
    409 	void *v;
    410 	register_t *retval;
    411 {
    412 	struct sys___sigprocmask14_args /* {
    413 		syscallarg(int) how;
    414 		syscallarg(const sigset_t *) set;
    415 		syscallarg(sigset_t *) oset;
    416 	} */ *uap = v;
    417 	sigset_t nss, oss;
    418 	int error;
    419 
    420 	if (SCARG(uap, set)) {
    421 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    422 		if (error)
    423 			return (error);
    424 	}
    425 	error = sigprocmask1(p, SCARG(uap, how),
    426 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    427 	if (error)
    428 		return (error);
    429 	if (SCARG(uap, oset)) {
    430 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    431 		if (error)
    432 			return (error);
    433 	}
    434 	return (0);
    435 }
    436 
    437 void
    438 sigpending1(p, ss)
    439 	struct proc *p;
    440 	sigset_t *ss;
    441 {
    442 
    443 	*ss = p->p_siglist;
    444 	sigminusset(&p->p_sigmask, ss);
    445 }
    446 
    447 /* ARGSUSED */
    448 int
    449 sys___sigpending14(p, v, retval)
    450 	struct proc *p;
    451 	void *v;
    452 	register_t *retval;
    453 {
    454 	register struct sys___sigpending14_args /* {
    455 		syscallarg(sigset_t *) set;
    456 	} */ *uap = v;
    457 	sigset_t ss;
    458 
    459 	sigpending1(p, &ss);
    460 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    461 }
    462 
    463 int
    464 sigsuspend1(p, ss)
    465 	struct proc *p;
    466 	const sigset_t *ss;
    467 {
    468 	register struct sigacts *ps = p->p_sigacts;
    469 
    470 	if (ss) {
    471 		/*
    472 		 * When returning from sigpause, we want
    473 		 * the old mask to be restored after the
    474 		 * signal handler has finished.  Thus, we
    475 		 * save it here and mark the sigacts structure
    476 		 * to indicate this.
    477 		 */
    478 		ps->ps_oldmask = p->p_sigmask;
    479 		ps->ps_flags |= SAS_OLDMASK;
    480 		(void) splhigh();
    481 		p->p_sigmask = *ss;
    482 		p->p_sigcheck = 1;
    483 		sigminusset(&sigcantmask, &p->p_sigmask);
    484 		(void) spl0();
    485 	}
    486 
    487 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    488 		/* void */;
    489 	/* always return EINTR rather than ERESTART... */
    490 	return (EINTR);
    491 }
    492 
    493 /*
    494  * Suspend process until signal, providing mask to be set
    495  * in the meantime.  Note nonstandard calling convention:
    496  * libc stub passes mask, not pointer, to save a copyin.
    497  */
    498 /* ARGSUSED */
    499 int
    500 sys___sigsuspend14(p, v, retval)
    501 	register struct proc *p;
    502 	void *v;
    503 	register_t *retval;
    504 {
    505 	struct sys___sigsuspend14_args /* {
    506 		syscallarg(const sigset_t *) set;
    507 	} */ *uap = v;
    508 	sigset_t ss;
    509 	int error;
    510 
    511 	if (SCARG(uap, set)) {
    512 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    513 		if (error)
    514 			return (error);
    515 	}
    516 
    517 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
    518 }
    519 
    520 int
    521 sigaltstack1(p, nss, oss)
    522 	struct proc *p;
    523 	const struct sigaltstack *nss;
    524 	struct sigaltstack *oss;
    525 {
    526 	register struct sigacts *ps = p->p_sigacts;
    527 
    528 	if (oss)
    529 		*oss = ps->ps_sigstk;
    530 
    531 	if (nss) {
    532 		if (nss->ss_flags & ~SS_ALLBITS)
    533 			return (EINVAL);
    534 
    535 		if (nss->ss_flags & SS_DISABLE) {
    536 			if (ps->ps_sigstk.ss_flags & SS_ONSTACK)
    537 				return (EINVAL);
    538 		} else {
    539 			if (nss->ss_size < MINSIGSTKSZ)
    540 				return (ENOMEM);
    541 		}
    542 		ps->ps_sigstk = *nss;
    543 	}
    544 
    545 	return (0);
    546 }
    547 
    548 /* ARGSUSED */
    549 int
    550 sys___sigaltstack14(p, v, retval)
    551 	struct proc *p;
    552 	void *v;
    553 	register_t *retval;
    554 {
    555 	register struct sys___sigaltstack14_args /* {
    556 		syscallarg(const struct sigaltstack *) nss;
    557 		syscallarg(struct sigaltstack *) oss;
    558 	} */ *uap = v;
    559 	struct sigaltstack nss, oss;
    560 	int error;
    561 
    562 	if (SCARG(uap, nss)) {
    563 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    564 		if (error)
    565 			return (error);
    566 	}
    567 	error = sigaltstack1(p,
    568 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    569 	if (error)
    570 		return (error);
    571 	if (SCARG(uap, oss)) {
    572 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    573 		if (error)
    574 			return (error);
    575 	}
    576 	return (0);
    577 }
    578 
    579 /* ARGSUSED */
    580 int
    581 sys_kill(cp, v, retval)
    582 	register struct proc *cp;
    583 	void *v;
    584 	register_t *retval;
    585 {
    586 	register struct sys_kill_args /* {
    587 		syscallarg(int) pid;
    588 		syscallarg(int) signum;
    589 	} */ *uap = v;
    590 	register struct proc *p;
    591 	register struct pcred *pc = cp->p_cred;
    592 
    593 	if ((u_int)SCARG(uap, signum) >= NSIG)
    594 		return (EINVAL);
    595 	if (SCARG(uap, pid) > 0) {
    596 		/* kill single process */
    597 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    598 			return (ESRCH);
    599 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    600 			return (EPERM);
    601 		if (SCARG(uap, signum))
    602 			psignal(p, SCARG(uap, signum));
    603 		return (0);
    604 	}
    605 	switch (SCARG(uap, pid)) {
    606 	case -1:		/* broadcast signal */
    607 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
    608 	case 0:			/* signal own process group */
    609 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
    610 	default:		/* negative explicit process group */
    611 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
    612 	}
    613 	/* NOTREACHED */
    614 }
    615 
    616 /*
    617  * Common code for kill process group/broadcast kill.
    618  * cp is calling process.
    619  */
    620 int
    621 killpg1(cp, signum, pgid, all)
    622 	register struct proc *cp;
    623 	int signum, pgid, all;
    624 {
    625 	register struct proc *p;
    626 	register struct pcred *pc = cp->p_cred;
    627 	struct pgrp *pgrp;
    628 	int nfound = 0;
    629 
    630 	if (all) {
    631 		/*
    632 		 * broadcast
    633 		 */
    634 		proclist_lock_read();
    635 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    636 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    637 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    638 				continue;
    639 			nfound++;
    640 			if (signum)
    641 				psignal(p, signum);
    642 		}
    643 		proclist_unlock_read();
    644 	} else {
    645 		if (pgid == 0)
    646 			/*
    647 			 * zero pgid means send to my process group.
    648 			 */
    649 			pgrp = cp->p_pgrp;
    650 		else {
    651 			pgrp = pgfind(pgid);
    652 			if (pgrp == NULL)
    653 				return (ESRCH);
    654 		}
    655 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    656 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    657 			    !CANSIGNAL(cp, pc, p, signum))
    658 				continue;
    659 			nfound++;
    660 			if (signum && P_ZOMBIE(p) == 0)
    661 				psignal(p, signum);
    662 		}
    663 	}
    664 	return (nfound ? 0 : ESRCH);
    665 }
    666 
    667 /*
    668  * Send a signal to a process group.
    669  */
    670 void
    671 gsignal(pgid, signum)
    672 	int pgid, signum;
    673 {
    674 	struct pgrp *pgrp;
    675 
    676 	if (pgid && (pgrp = pgfind(pgid)))
    677 		pgsignal(pgrp, signum, 0);
    678 }
    679 
    680 /*
    681  * Send a signal to a process group. If checktty is 1,
    682  * limit to members which have a controlling terminal.
    683  */
    684 void
    685 pgsignal(pgrp, signum, checkctty)
    686 	struct pgrp *pgrp;
    687 	int signum, checkctty;
    688 {
    689 	register struct proc *p;
    690 
    691 	if (pgrp)
    692 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
    693 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    694 				psignal(p, signum);
    695 }
    696 
    697 /*
    698  * Send a signal caused by a trap to the current process.
    699  * If it will be caught immediately, deliver it with correct code.
    700  * Otherwise, post it normally.
    701  */
    702 void
    703 trapsignal(p, signum, code)
    704 	struct proc *p;
    705 	register int signum;
    706 	u_long code;
    707 {
    708 	register struct sigacts *ps = p->p_sigacts;
    709 
    710 	if ((p->p_flag & P_TRACED) == 0 &&
    711 	    sigismember(&p->p_sigcatch, signum) &&
    712 	    !sigismember(&p->p_sigmask, signum)) {
    713 		p->p_stats->p_ru.ru_nsignals++;
    714 #ifdef KTRACE
    715 		if (KTRPOINT(p, KTR_PSIG))
    716 			ktrpsig(p->p_tracep, signum,
    717 			    ps->ps_sigact[signum].sa_handler, &p->p_sigmask,
    718 			    code);
    719 #endif
    720 		(*p->p_emul->e_sendsig)(ps->ps_sigact[signum].sa_handler,
    721 		    signum, &p->p_sigmask, code);
    722 		(void) splhigh();
    723 		sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
    724 		if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
    725 			sigdelset(&p->p_sigcatch, signum);
    726 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    727 				sigaddset(&p->p_sigignore, signum);
    728 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
    729 		}
    730 		(void) spl0();
    731 	} else {
    732 		ps->ps_code = code;	/* XXX for core dump/debugger */
    733 		ps->ps_sig = signum;	/* XXX to verify code */
    734 		psignal(p, signum);
    735 	}
    736 }
    737 
    738 /*
    739  * Send the signal to the process.  If the signal has an action, the action
    740  * is usually performed by the target process rather than the caller; we add
    741  * the signal to the set of pending signals for the process.
    742  *
    743  * Exceptions:
    744  *   o When a stop signal is sent to a sleeping process that takes the
    745  *     default action, the process is stopped without awakening it.
    746  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    747  *     regardless of the signal action (eg, blocked or ignored).
    748  *
    749  * Other ignored signals are discarded immediately.
    750  */
    751 void
    752 psignal(p, signum)
    753 	register struct proc *p;
    754 	register int signum;
    755 {
    756 	register int s, prop;
    757 	register sig_t action;
    758 
    759 #ifdef DIAGNOSTIC
    760 	if (signum <= 0 || signum >= NSIG)
    761 		panic("psignal signal number");
    762 #endif
    763 	prop = sigprop[signum];
    764 
    765 	/*
    766 	 * If proc is traced, always give parent a chance.
    767 	 */
    768 	if (p->p_flag & P_TRACED)
    769 		action = SIG_DFL;
    770 	else {
    771 		/*
    772 		 * If the signal is being ignored,
    773 		 * then we forget about it immediately.
    774 		 * (Note: we don't set SIGCONT in p_sigignore,
    775 		 * and if it is set to SIG_IGN,
    776 		 * action will be SIG_DFL here.)
    777 		 */
    778 		if (sigismember(&p->p_sigignore, signum))
    779 			return;
    780 		if (sigismember(&p->p_sigmask, signum))
    781 			action = SIG_HOLD;
    782 		else if (sigismember(&p->p_sigcatch, signum))
    783 			action = SIG_CATCH;
    784 		else {
    785 			action = SIG_DFL;
    786 
    787 			if (prop & SA_KILL && p->p_nice > NZERO)
    788 				p->p_nice = NZERO;
    789 
    790 			/*
    791 			 * If sending a tty stop signal to a member of an
    792 			 * orphaned process group, discard the signal here if
    793 			 * the action is default; don't stop the process below
    794 			 * if sleeping, and don't clear any pending SIGCONT.
    795 			 */
    796 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
    797 				return;
    798 		}
    799 	}
    800 
    801 	if (prop & SA_CONT)
    802 		sigminusset(&stopsigmask, &p->p_siglist);
    803 
    804 	if (prop & SA_STOP)
    805 		sigminusset(&contsigmask, &p->p_siglist);
    806 
    807 	sigaddset(&p->p_siglist, signum);
    808 	p->p_sigcheck = 1;
    809 
    810 	/*
    811 	 * Defer further processing for signals which are held,
    812 	 * except that stopped processes must be continued by SIGCONT.
    813 	 */
    814 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
    815 		return;
    816 	s = splhigh();
    817 	switch (p->p_stat) {
    818 
    819 	case SSLEEP:
    820 		/*
    821 		 * If process is sleeping uninterruptibly
    822 		 * we can't interrupt the sleep... the signal will
    823 		 * be noticed when the process returns through
    824 		 * trap() or syscall().
    825 		 */
    826 		if ((p->p_flag & P_SINTR) == 0)
    827 			goto out;
    828 		/*
    829 		 * Process is sleeping and traced... make it runnable
    830 		 * so it can discover the signal in issignal() and stop
    831 		 * for the parent.
    832 		 */
    833 		if (p->p_flag & P_TRACED)
    834 			goto run;
    835 		/*
    836 		 * If SIGCONT is default (or ignored) and process is
    837 		 * asleep, we are finished; the process should not
    838 		 * be awakened.
    839 		 */
    840 		if ((prop & SA_CONT) && action == SIG_DFL) {
    841 			sigdelset(&p->p_siglist, signum);
    842 			goto out;
    843 		}
    844 		/*
    845 		 * When a sleeping process receives a stop
    846 		 * signal, process immediately if possible.
    847 		 */
    848 		if ((prop & SA_STOP) && action == SIG_DFL) {
    849 			/*
    850 			 * If a child holding parent blocked,
    851 			 * stopping could cause deadlock.
    852 			 */
    853 			if (p->p_flag & P_PPWAIT)
    854 				goto out;
    855 			sigdelset(&p->p_siglist, signum);
    856 			p->p_xstat = signum;
    857 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
    858 				psignal(p->p_pptr, SIGCHLD);
    859 			stop(p);
    860 			goto out;
    861 		}
    862 		/*
    863 		 * All other (caught or default) signals
    864 		 * cause the process to run.
    865 		 */
    866 		goto runfast;
    867 		/*NOTREACHED*/
    868 
    869 	case SSTOP:
    870 		/*
    871 		 * If traced process is already stopped,
    872 		 * then no further action is necessary.
    873 		 */
    874 		if (p->p_flag & P_TRACED)
    875 			goto out;
    876 
    877 		/*
    878 		 * Kill signal always sets processes running.
    879 		 */
    880 		if (signum == SIGKILL)
    881 			goto runfast;
    882 
    883 		if (prop & SA_CONT) {
    884 			/*
    885 			 * If SIGCONT is default (or ignored), we continue the
    886 			 * process but don't leave the signal in p_siglist, as
    887 			 * it has no further action.  If SIGCONT is held, we
    888 			 * continue the process and leave the signal in
    889 			 * p_siglist.  If the process catches SIGCONT, let it
    890 			 * handle the signal itself.  If it isn't waiting on
    891 			 * an event, then it goes back to run state.
    892 			 * Otherwise, process goes back to sleep state.
    893 			 */
    894 			if (action == SIG_DFL)
    895 				sigdelset(&p->p_siglist, signum);
    896 			if (action == SIG_CATCH)
    897 				goto runfast;
    898 			if (p->p_wchan == 0)
    899 				goto run;
    900 			p->p_stat = SSLEEP;
    901 			goto out;
    902 		}
    903 
    904 		if (prop & SA_STOP) {
    905 			/*
    906 			 * Already stopped, don't need to stop again.
    907 			 * (If we did the shell could get confused.)
    908 			 */
    909 			sigdelset(&p->p_siglist, signum);
    910 			goto out;
    911 		}
    912 
    913 		/*
    914 		 * If process is sleeping interruptibly, then simulate a
    915 		 * wakeup so that when it is continued, it will be made
    916 		 * runnable and can look at the signal.  But don't make
    917 		 * the process runnable, leave it stopped.
    918 		 */
    919 		if (p->p_wchan && p->p_flag & P_SINTR)
    920 			unsleep(p);
    921 		goto out;
    922 
    923 	default:
    924 		/*
    925 		 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal,
    926 		 * other than kicking ourselves if we are running.
    927 		 * It will either never be noticed, or noticed very soon.
    928 		 */
    929 		if (p == curproc)
    930 			signotify(p);
    931 		goto out;
    932 	}
    933 	/*NOTREACHED*/
    934 
    935 runfast:
    936 	/*
    937 	 * Raise priority to at least PUSER.
    938 	 */
    939 	if (p->p_priority > PUSER)
    940 		p->p_priority = PUSER;
    941 run:
    942 	setrunnable(p);
    943 out:
    944 	splx(s);
    945 }
    946 
    947 static __inline int firstsig __P((const sigset_t *));
    948 
    949 static __inline int
    950 firstsig(ss)
    951 	const sigset_t *ss;
    952 {
    953 	int sig;
    954 
    955 	sig = ffs(ss->__bits[0]);
    956 	if (sig != 0)
    957 		return (sig);
    958 #if NSIG > 33
    959 	sig = ffs(ss->__bits[1]);
    960 	if (sig != 0)
    961 		return (sig + 32);
    962 #endif
    963 #if NSIG > 65
    964 	sig = ffs(ss->__bits[2]);
    965 	if (sig != 0)
    966 		return (sig + 64);
    967 #endif
    968 #if NSIG > 97
    969 	sig = ffs(ss->__bits[3]);
    970 	if (sig != 0)
    971 		return (sig + 96);
    972 #endif
    973 	return (0);
    974 }
    975 
    976 /*
    977  * If the current process has received a signal (should be caught or cause
    978  * termination, should interrupt current syscall), return the signal number.
    979  * Stop signals with default action are processed immediately, then cleared;
    980  * they aren't returned.  This is checked after each entry to the system for
    981  * a syscall or trap (though this can usually be done without calling issignal
    982  * by checking the pending signal masks in the CURSIG macro.) The normal call
    983  * sequence is
    984  *
    985  *	while (signum = CURSIG(curproc))
    986  *		postsig(signum);
    987  */
    988 int
    989 issignal(p)
    990 	register struct proc *p;
    991 {
    992 	register int signum, prop;
    993 	sigset_t ss;
    994 
    995 	for (;;) {
    996 		sigpending1(p, &ss);
    997 		if (p->p_flag & P_PPWAIT)
    998 			sigminusset(&stopsigmask, &ss);
    999 		signum = firstsig(&ss);
   1000 		if (signum == 0) {		 	/* no signal to send */
   1001 			p->p_sigcheck = 0;
   1002 			return (0);
   1003 		}
   1004 		sigdelset(&p->p_siglist, signum);	/* take the signal! */
   1005 
   1006 		/*
   1007 		 * We should see pending but ignored signals
   1008 		 * only if P_TRACED was on when they were posted.
   1009 		 */
   1010 		if (sigismember(&p->p_sigignore, signum) &&
   1011 		    (p->p_flag & P_TRACED) == 0)
   1012 			continue;
   1013 
   1014 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1015 			/*
   1016 			 * If traced, always stop, and stay
   1017 			 * stopped until released by the debugger.
   1018 			 */
   1019 			p->p_xstat = signum;
   1020 			if ((p->p_flag & P_FSTRACE) == 0)
   1021 				psignal(p->p_pptr, SIGCHLD);
   1022 			do {
   1023 				stop(p);
   1024 				mi_switch();
   1025 			} while (!trace_req(p) && p->p_flag & P_TRACED);
   1026 
   1027 			/*
   1028 			 * If we are no longer being traced, or the parent
   1029 			 * didn't give us a signal, look for more signals.
   1030 			 */
   1031 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1032 				continue;
   1033 
   1034 			/*
   1035 			 * If the new signal is being masked, look for other
   1036 			 * signals.
   1037 			 */
   1038 			signum = p->p_xstat;
   1039 			/* `p->p_siglist |= mask' is done in setrunnable(). */
   1040 			if (sigismember(&p->p_sigmask, signum))
   1041 				continue;
   1042 			sigdelset(&p->p_siglist, signum);	/* take the signal! */
   1043 		}
   1044 
   1045 		prop = sigprop[signum];
   1046 
   1047 		/*
   1048 		 * Decide whether the signal should be returned.
   1049 		 * Return the signal's number, or fall through
   1050 		 * to clear it from the pending mask.
   1051 		 */
   1052 		switch ((long)p->p_sigacts->ps_sigact[signum].sa_handler) {
   1053 
   1054 		case (long)SIG_DFL:
   1055 			/*
   1056 			 * Don't take default actions on system processes.
   1057 			 */
   1058 			if (p->p_pid <= 1) {
   1059 #ifdef DIAGNOSTIC
   1060 				/*
   1061 				 * Are you sure you want to ignore SIGSEGV
   1062 				 * in init? XXX
   1063 				 */
   1064 				printf("Process (pid %d) got signal %d\n",
   1065 				    p->p_pid, signum);
   1066 #endif
   1067 				break;		/* == ignore */
   1068 			}
   1069 			/*
   1070 			 * If there is a pending stop signal to process
   1071 			 * with default action, stop here,
   1072 			 * then clear the signal.  However,
   1073 			 * if process is member of an orphaned
   1074 			 * process group, ignore tty stop signals.
   1075 			 */
   1076 			if (prop & SA_STOP) {
   1077 				if (p->p_flag & P_TRACED ||
   1078 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1079 				    prop & SA_TTYSTOP))
   1080 					break;	/* == ignore */
   1081 				p->p_xstat = signum;
   1082 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1083 					psignal(p->p_pptr, SIGCHLD);
   1084 				stop(p);
   1085 				mi_switch();
   1086 				break;
   1087 			} else if (prop & SA_IGNORE) {
   1088 				/*
   1089 				 * Except for SIGCONT, shouldn't get here.
   1090 				 * Default action is to ignore; drop it.
   1091 				 */
   1092 				break;		/* == ignore */
   1093 			} else
   1094 				goto keep;
   1095 			/*NOTREACHED*/
   1096 
   1097 		case (long)SIG_IGN:
   1098 			/*
   1099 			 * Masking above should prevent us ever trying
   1100 			 * to take action on an ignored signal other
   1101 			 * than SIGCONT, unless process is traced.
   1102 			 */
   1103 			if ((prop & SA_CONT) == 0 &&
   1104 			    (p->p_flag & P_TRACED) == 0)
   1105 				printf("issignal\n");
   1106 			break;		/* == ignore */
   1107 
   1108 		default:
   1109 			/*
   1110 			 * This signal has an action, let
   1111 			 * postsig() process it.
   1112 			 */
   1113 			goto keep;
   1114 		}
   1115 	}
   1116 	/* NOTREACHED */
   1117 
   1118 keep:
   1119 	sigaddset(&p->p_siglist, signum);	/* leave the signal for later */
   1120 	p->p_sigcheck = 1;
   1121 	return (signum);
   1122 }
   1123 
   1124 /*
   1125  * Put the argument process into the stopped state and notify the parent
   1126  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1127  * on the run queue.
   1128  */
   1129 void
   1130 stop(p)
   1131 	register struct proc *p;
   1132 {
   1133 
   1134 	p->p_stat = SSTOP;
   1135 	p->p_flag &= ~P_WAITED;
   1136 	wakeup((caddr_t)p->p_pptr);
   1137 }
   1138 
   1139 /*
   1140  * Take the action for the specified signal
   1141  * from the current set of pending signals.
   1142  */
   1143 void
   1144 postsig(signum)
   1145 	register int signum;
   1146 {
   1147 	register struct proc *p = curproc;
   1148 	register struct sigacts *ps = p->p_sigacts;
   1149 	register sig_t action;
   1150 	u_long code;
   1151 	sigset_t *returnmask;
   1152 
   1153 #ifdef DIAGNOSTIC
   1154 	if (signum == 0)
   1155 		panic("postsig");
   1156 #endif
   1157 	sigdelset(&p->p_siglist, signum);
   1158 	action = ps->ps_sigact[signum].sa_handler;
   1159 #ifdef KTRACE
   1160 	if (KTRPOINT(p, KTR_PSIG))
   1161 		ktrpsig(p->p_tracep,
   1162 		    signum, action, ps->ps_flags & SAS_OLDMASK ?
   1163 		    &ps->ps_oldmask : &p->p_sigmask, 0);
   1164 #endif
   1165 	if (action == SIG_DFL) {
   1166 		/*
   1167 		 * Default action, where the default is to kill
   1168 		 * the process.  (Other cases were ignored above.)
   1169 		 */
   1170 		sigexit(p, signum);
   1171 		/* NOTREACHED */
   1172 	} else {
   1173 		/*
   1174 		 * If we get here, the signal must be caught.
   1175 		 */
   1176 #ifdef DIAGNOSTIC
   1177 		if (action == SIG_IGN || sigismember(&p->p_sigmask, signum))
   1178 			panic("postsig action");
   1179 #endif
   1180 		/*
   1181 		 * Set the new mask value and also defer further
   1182 		 * occurences of this signal.
   1183 		 *
   1184 		 * Special case: user has done a sigpause.  Here the
   1185 		 * current mask is not of interest, but rather the
   1186 		 * mask from before the sigpause is what we want
   1187 		 * restored after the signal processing is completed.
   1188 		 */
   1189 		if (ps->ps_flags & SAS_OLDMASK) {
   1190 			returnmask = &ps->ps_oldmask;
   1191 			ps->ps_flags &= ~SAS_OLDMASK;
   1192 		} else
   1193 			returnmask = &p->p_sigmask;
   1194 		p->p_stats->p_ru.ru_nsignals++;
   1195 		if (ps->ps_sig != signum) {
   1196 			code = 0;
   1197 		} else {
   1198 			code = ps->ps_code;
   1199 			ps->ps_code = 0;
   1200 			ps->ps_sig = 0;
   1201 		}
   1202 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code);
   1203 		(void) splhigh();
   1204 		sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
   1205 		if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
   1206 			sigdelset(&p->p_sigcatch, signum);
   1207 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1208 				sigaddset(&p->p_sigignore, signum);
   1209 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
   1210 		}
   1211 		(void) spl0();
   1212 	}
   1213 }
   1214 
   1215 /*
   1216  * Kill the current process for stated reason.
   1217  */
   1218 void
   1219 killproc(p, why)
   1220 	struct proc *p;
   1221 	char *why;
   1222 {
   1223 
   1224 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1225 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1226 	psignal(p, SIGKILL);
   1227 }
   1228 
   1229 /*
   1230  * Force the current process to exit with the specified signal, dumping core
   1231  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1232  * allowing unrecoverable failures to terminate the process without changing
   1233  * signal state.  Mark the accounting record with the signal termination.
   1234  * If dumping core, save the signal number for the debugger.  Calls exit and
   1235  * does not return.
   1236  */
   1237 void
   1238 sigexit(p, signum)
   1239 	register struct proc *p;
   1240 	int signum;
   1241 {
   1242 
   1243 	p->p_acflag |= AXSIG;
   1244 	if (sigprop[signum] & SA_CORE) {
   1245 		p->p_sigacts->ps_sig = signum;
   1246 		if (coredump(p) == 0)
   1247 			signum |= WCOREFLAG;
   1248 	}
   1249 	exit1(p, W_EXITCODE(0, signum));
   1250 	/* NOTREACHED */
   1251 }
   1252 
   1253 /*
   1254  * Dump core, into a file named "progname.core" or "core" (depending on the
   1255  * value of shortcorename), unless the process was setuid/setgid.
   1256  */
   1257 int
   1258 coredump(p)
   1259 	register struct proc *p;
   1260 {
   1261 	register struct vnode *vp;
   1262 	register struct vmspace *vm = p->p_vmspace;
   1263 	register struct ucred *cred = p->p_cred->pc_ucred;
   1264 	struct nameidata nd;
   1265 	struct vattr vattr;
   1266 	int error, error1;
   1267 	char name[MAXCOMLEN+6];		/* progname.core */
   1268 	struct core core;
   1269 	extern int shortcorename;
   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 	if (shortcorename)
   1297 		sprintf(name, "core");
   1298 	else
   1299 		sprintf(name, "%s.core", p->p_comm);
   1300 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
   1301 	error = vn_open(&nd, O_CREAT | FWRITE, 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