Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.44
      1 /*	$NetBSD: kern_sig.c,v 1.44 1995/07/24 03:18:42 mycroft 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.7 (Berkeley) 4/18/94
     41  */
     42 
     43 #define	SIGPROP		/* include signal properties table */
     44 #include <sys/param.h>
     45 #include <sys/signalvar.h>
     46 #include <sys/resourcevar.h>
     47 #include <sys/namei.h>
     48 #include <sys/vnode.h>
     49 #include <sys/proc.h>
     50 #include <sys/systm.h>
     51 #include <sys/timeb.h>
     52 #include <sys/times.h>
     53 #include <sys/buf.h>
     54 #include <sys/acct.h>
     55 #include <sys/file.h>
     56 #include <sys/kernel.h>
     57 #include <sys/wait.h>
     58 #include <sys/ktrace.h>
     59 #include <sys/syslog.h>
     60 #include <sys/stat.h>
     61 #include <sys/core.h>
     62 
     63 #include <sys/mount.h>
     64 #include <sys/syscallargs.h>
     65 
     66 #include <machine/cpu.h>
     67 
     68 #include <vm/vm.h>
     69 #include <sys/user.h>		/* for coredump */
     70 
     71 void stop __P((struct proc *p));
     72 
     73 /*
     74  * Can process p, with pcred pc, send the signal signum to process q?
     75  */
     76 #define CANSIGNAL(p, pc, q, signum) \
     77 	((pc)->pc_ucred->cr_uid == 0 || \
     78 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
     79 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
     80 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
     81 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
     82 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
     83 
     84 /* ARGSUSED */
     85 sigaction(p, uap, retval)
     86 	struct proc *p;
     87 	register struct sigaction_args /* {
     88 		syscallarg(int) signum;
     89 		syscallarg(struct sigaction *) nsa;
     90 		syscallarg(struct sigaction *) osa;
     91 	} */ *uap;
     92 	register_t *retval;
     93 {
     94 	struct sigaction vec;
     95 	register struct sigaction *sa;
     96 	register struct sigacts *ps = p->p_sigacts;
     97 	register int signum;
     98 	int bit, error;
     99 
    100 	signum = SCARG(uap, signum);
    101 	if (signum <= 0 || signum >= NSIG ||
    102 	    signum == SIGKILL || signum == SIGSTOP)
    103 		return (EINVAL);
    104 	sa = &vec;
    105 	if (SCARG(uap, osa)) {
    106 		sa->sa_handler = ps->ps_sigact[signum];
    107 		sa->sa_mask = ps->ps_catchmask[signum];
    108 		bit = sigmask(signum);
    109 		sa->sa_flags = 0;
    110 		if ((ps->ps_sigonstack & bit) != 0)
    111 			sa->sa_flags |= SA_ONSTACK;
    112 		if ((ps->ps_sigintr & bit) == 0)
    113 			sa->sa_flags |= SA_RESTART;
    114 		if (p->p_flag & P_NOCLDSTOP)
    115 			sa->sa_flags |= SA_NOCLDSTOP;
    116 		if (error = copyout((caddr_t)sa, (caddr_t)SCARG(uap, osa),
    117 		    sizeof (vec)))
    118 			return (error);
    119 	}
    120 	if (SCARG(uap, nsa)) {
    121 		if (error = copyin((caddr_t)SCARG(uap, nsa), (caddr_t)sa,
    122 		    sizeof (vec)))
    123 			return (error);
    124 		setsigvec(p, signum, sa);
    125 	}
    126 	return (0);
    127 }
    128 
    129 setsigvec(p, signum, sa)
    130 	register struct proc *p;
    131 	int signum;
    132 	register struct sigaction *sa;
    133 {
    134 	register struct sigacts *ps = p->p_sigacts;
    135 	register int bit;
    136 
    137 	bit = sigmask(signum);
    138 	/*
    139 	 * Change setting atomically.
    140 	 */
    141 	(void) splhigh();
    142 	ps->ps_sigact[signum] = sa->sa_handler;
    143 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
    144 	if ((sa->sa_flags & SA_RESTART) == 0)
    145 		ps->ps_sigintr |= bit;
    146 	else
    147 		ps->ps_sigintr &= ~bit;
    148 	if (sa->sa_flags & SA_ONSTACK)
    149 		ps->ps_sigonstack |= bit;
    150 	else
    151 		ps->ps_sigonstack &= ~bit;
    152 #ifdef COMPAT_SUNOS
    153 	{
    154 		extern struct emul emul_sunos;
    155 		if (p->p_emul == &emul_sunos && sa->sa_flags & SA_USERTRAMP)
    156 			ps->ps_usertramp |= bit;
    157 		else
    158 			ps->ps_usertramp &= ~bit;
    159 	}
    160 #endif
    161 	if (signum == SIGCHLD) {
    162 		if (sa->sa_flags & SA_NOCLDSTOP)
    163 			p->p_flag |= P_NOCLDSTOP;
    164 		else
    165 			p->p_flag &= ~P_NOCLDSTOP;
    166 	}
    167 	/*
    168 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
    169 	 * and for signals set to SIG_DFL where the default is to ignore.
    170 	 * However, don't put SIGCONT in p_sigignore,
    171 	 * as we have to restart the process.
    172 	 */
    173 	if (sa->sa_handler == SIG_IGN ||
    174 	    (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
    175 		p->p_siglist &= ~bit;		/* never to be seen again */
    176 		if (signum != SIGCONT)
    177 			p->p_sigignore |= bit;	/* easier in psignal */
    178 		p->p_sigcatch &= ~bit;
    179 	} else {
    180 		p->p_sigignore &= ~bit;
    181 		if (sa->sa_handler == SIG_DFL)
    182 			p->p_sigcatch &= ~bit;
    183 		else
    184 			p->p_sigcatch |= bit;
    185 	}
    186 	(void) spl0();
    187 }
    188 
    189 /*
    190  * Initialize signal state for process 0;
    191  * set to ignore signals that are ignored by default.
    192  */
    193 void
    194 siginit(p)
    195 	struct proc *p;
    196 {
    197 	register int i;
    198 
    199 	for (i = 0; i < NSIG; i++)
    200 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
    201 			p->p_sigignore |= sigmask(i);
    202 }
    203 
    204 /*
    205  * Reset signals for an exec of the specified process.
    206  */
    207 void
    208 execsigs(p)
    209 	register struct proc *p;
    210 {
    211 	register struct sigacts *ps = p->p_sigacts;
    212 	register int nc, mask;
    213 
    214 	/*
    215 	 * Reset caught signals.  Held signals remain held
    216 	 * through p_sigmask (unless they were caught,
    217 	 * and are now ignored by default).
    218 	 */
    219 	while (p->p_sigcatch) {
    220 		nc = ffs((long)p->p_sigcatch);
    221 		mask = sigmask(nc);
    222 		p->p_sigcatch &= ~mask;
    223 		if (sigprop[nc] & SA_IGNORE) {
    224 			if (nc != SIGCONT)
    225 				p->p_sigignore |= mask;
    226 			p->p_siglist &= ~mask;
    227 		}
    228 		ps->ps_sigact[nc] = SIG_DFL;
    229 	}
    230 	/*
    231 	 * Reset stack state to the user stack.
    232 	 * Clear set of signals caught on the signal stack.
    233 	 */
    234 	ps->ps_sigstk.ss_flags = SA_DISABLE;
    235 	ps->ps_sigstk.ss_size = 0;
    236 	ps->ps_sigstk.ss_base = 0;
    237 	ps->ps_flags = 0;
    238 }
    239 
    240 /*
    241  * Manipulate signal mask.
    242  * Note that we receive new mask, not pointer,
    243  * and return old mask as return value;
    244  * the library stub does the rest.
    245  */
    246 sigprocmask(p, uap, retval)
    247 	register struct proc *p;
    248 	struct sigprocmask_args /* {
    249 		syscallarg(int) how;
    250 		syscallarg(sigset_t) mask;
    251 	} */ *uap;
    252 	register_t *retval;
    253 {
    254 	int error = 0;
    255 
    256 	*retval = p->p_sigmask;
    257 	(void) splhigh();
    258 
    259 	switch (SCARG(uap, how)) {
    260 	case SIG_BLOCK:
    261 		p->p_sigmask |= SCARG(uap, mask) &~ sigcantmask;
    262 		break;
    263 
    264 	case SIG_UNBLOCK:
    265 		p->p_sigmask &= ~SCARG(uap, mask);
    266 		break;
    267 
    268 	case SIG_SETMASK:
    269 		p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
    270 		break;
    271 
    272 	default:
    273 		error = EINVAL;
    274 		break;
    275 	}
    276 	(void) spl0();
    277 	return (error);
    278 }
    279 
    280 /* ARGSUSED */
    281 sigpending(p, uap, retval)
    282 	struct proc *p;
    283 	void *uap;
    284 	register_t *retval;
    285 {
    286 
    287 	*retval = p->p_siglist;
    288 	return (0);
    289 }
    290 
    291 /*
    292  * Suspend process until signal, providing mask to be set
    293  * in the meantime.  Note nonstandard calling convention:
    294  * libc stub passes mask, not pointer, to save a copyin.
    295  */
    296 /* ARGSUSED */
    297 int
    298 sigsuspend(p, uap, retval)
    299 	register struct proc *p;
    300 	struct sigsuspend_args /* {
    301 		syscallarg(int) mask;
    302 	} */ *uap;
    303 	register_t *retval;
    304 {
    305 	register struct sigacts *ps = p->p_sigacts;
    306 
    307 	/*
    308 	 * When returning from sigpause, we want
    309 	 * the old mask to be restored after the
    310 	 * signal handler has finished.  Thus, we
    311 	 * save it here and mark the sigacts structure
    312 	 * to indicate this.
    313 	 */
    314 	ps->ps_oldmask = p->p_sigmask;
    315 	ps->ps_flags |= SAS_OLDMASK;
    316 	p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
    317 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    318 		/* void */;
    319 	/* always return EINTR rather than ERESTART... */
    320 	return (EINTR);
    321 }
    322 
    323 /* ARGSUSED */
    324 sigaltstack(p, uap, retval)
    325 	struct proc *p;
    326 	register struct sigaltstack_args /* {
    327 		syscallarg(struct sigaltstack *) nss;
    328 		syscallarg(struct sigaltstack *) oss;
    329 	} */ *uap;
    330 	register_t *retval;
    331 {
    332 	struct sigacts *psp;
    333 	struct sigaltstack ss;
    334 	int error;
    335 
    336 	psp = p->p_sigacts;
    337 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
    338 		psp->ps_sigstk.ss_flags |= SA_DISABLE;
    339 	if (SCARG(uap, oss) && (error = copyout((caddr_t)&psp->ps_sigstk,
    340 	    (caddr_t)SCARG(uap, oss), sizeof (struct sigaltstack))))
    341 		return (error);
    342 	if (SCARG(uap, nss) == 0)
    343 		return (0);
    344 	if (error = copyin((caddr_t)SCARG(uap, nss), (caddr_t)&ss,
    345 	    sizeof (ss)))
    346 		return (error);
    347 	if (ss.ss_flags & SA_DISABLE) {
    348 		if (psp->ps_sigstk.ss_flags & SA_ONSTACK)
    349 			return (EINVAL);
    350 		psp->ps_flags &= ~SAS_ALTSTACK;
    351 		psp->ps_sigstk.ss_flags = ss.ss_flags;
    352 		return (0);
    353 	}
    354 	if (ss.ss_size < MINSIGSTKSZ)
    355 		return (ENOMEM);
    356 	psp->ps_flags |= SAS_ALTSTACK;
    357 	psp->ps_sigstk= ss;
    358 	return (0);
    359 }
    360 
    361 /* ARGSUSED */
    362 int
    363 kill(cp, uap, retval)
    364 	register struct proc *cp;
    365 	register struct kill_args /* {
    366 		syscallarg(int) pid;
    367 		syscallarg(int) signum;
    368 	} */ *uap;
    369 	register_t *retval;
    370 {
    371 	register struct proc *p;
    372 	register struct pcred *pc = cp->p_cred;
    373 
    374 #ifdef COMPAT_09
    375 	SCARG(uap, pid) = (short) SCARG(uap, pid);
    376 #endif
    377 
    378 	if ((u_int)SCARG(uap, signum) >= NSIG)
    379 		return (EINVAL);
    380 	if (SCARG(uap, pid) > 0) {
    381 		/* kill single process */
    382 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    383 			return (ESRCH);
    384 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    385 			return (EPERM);
    386 		if (SCARG(uap, signum))
    387 			psignal(p, SCARG(uap, signum));
    388 		return (0);
    389 	}
    390 	switch (SCARG(uap, pid)) {
    391 	case -1:		/* broadcast signal */
    392 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
    393 	case 0:			/* signal own process group */
    394 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
    395 	default:		/* negative explicit process group */
    396 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
    397 	}
    398 	/* NOTREACHED */
    399 }
    400 
    401 /*
    402  * Common code for kill process group/broadcast kill.
    403  * cp is calling process.
    404  */
    405 killpg1(cp, signum, pgid, all)
    406 	register struct proc *cp;
    407 	int signum, pgid, all;
    408 {
    409 	register struct proc *p;
    410 	register struct pcred *pc = cp->p_cred;
    411 	struct pgrp *pgrp;
    412 	int nfound = 0;
    413 
    414 	if (all)
    415 		/*
    416 		 * broadcast
    417 		 */
    418 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    419 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    420 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    421 				continue;
    422 			nfound++;
    423 			if (signum)
    424 				psignal(p, signum);
    425 		}
    426 	else {
    427 		if (pgid == 0)
    428 			/*
    429 			 * zero pgid means send to my process group.
    430 			 */
    431 			pgrp = cp->p_pgrp;
    432 		else {
    433 			pgrp = pgfind(pgid);
    434 			if (pgrp == NULL)
    435 				return (ESRCH);
    436 		}
    437 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    438 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    439 			    p->p_stat == SZOMB ||
    440 			    !CANSIGNAL(cp, pc, p, signum))
    441 				continue;
    442 			nfound++;
    443 			if (signum)
    444 				psignal(p, signum);
    445 		}
    446 	}
    447 	return (nfound ? 0 : ESRCH);
    448 }
    449 
    450 /*
    451  * Send a signal to a process group.
    452  */
    453 void
    454 gsignal(pgid, signum)
    455 	int pgid, signum;
    456 {
    457 	struct pgrp *pgrp;
    458 
    459 	if (pgid && (pgrp = pgfind(pgid)))
    460 		pgsignal(pgrp, signum, 0);
    461 }
    462 
    463 /*
    464  * Send a signal to a process group.  If checktty is 1,
    465  * limit to members which have a controlling terminal.
    466  */
    467 void
    468 pgsignal(pgrp, signum, checkctty)
    469 	struct pgrp *pgrp;
    470 	int signum, checkctty;
    471 {
    472 	register struct proc *p;
    473 
    474 	if (pgrp)
    475 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
    476 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    477 				psignal(p, signum);
    478 }
    479 
    480 /*
    481  * Send a signal caused by a trap to the current process.
    482  * If it will be caught immediately, deliver it with correct code.
    483  * Otherwise, post it normally.
    484  */
    485 void
    486 trapsignal(p, signum, code)
    487 	struct proc *p;
    488 	register int signum;
    489 	u_long code;
    490 {
    491 	register struct sigacts *ps = p->p_sigacts;
    492 	int mask;
    493 
    494 	mask = sigmask(signum);
    495 	if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
    496 	    (p->p_sigmask & mask) == 0) {
    497 		p->p_stats->p_ru.ru_nsignals++;
    498 #ifdef KTRACE
    499 		if (KTRPOINT(p, KTR_PSIG))
    500 			ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
    501 				p->p_sigmask, code);
    502 #endif
    503 		(*p->p_emul->e_sendsig)(ps->ps_sigact[signum], signum,
    504 					 p->p_sigmask, code);
    505 		p->p_sigmask |= ps->ps_catchmask[signum] | mask;
    506 	} else {
    507 		ps->ps_code = code;	/* XXX for core dump/debugger */
    508 		psignal(p, signum);
    509 	}
    510 }
    511 
    512 /*
    513  * Send the signal to the process.  If the signal has an action, the action
    514  * is usually performed by the target process rather than the caller; we add
    515  * the signal to the set of pending signals for the process.
    516  *
    517  * Exceptions:
    518  *   o When a stop signal is sent to a sleeping process that takes the
    519  *     default action, the process is stopped without awakening it.
    520  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    521  *     regardless of the signal action (eg, blocked or ignored).
    522  *
    523  * Other ignored signals are discarded immediately.
    524  */
    525 void
    526 psignal(p, signum)
    527 	register struct proc *p;
    528 	register int signum;
    529 {
    530 	register int s, prop;
    531 	register sig_t action;
    532 	int mask;
    533 
    534 	if ((u_int)signum >= NSIG || signum == 0)
    535 		panic("psignal signal number");
    536 	mask = sigmask(signum);
    537 	prop = sigprop[signum];
    538 
    539 	/*
    540 	 * If proc is traced, always give parent a chance.
    541 	 */
    542 	if (p->p_flag & P_TRACED)
    543 		action = SIG_DFL;
    544 	else {
    545 		/*
    546 		 * If the signal is being ignored,
    547 		 * then we forget about it immediately.
    548 		 * (Note: we don't set SIGCONT in p_sigignore,
    549 		 * and if it is set to SIG_IGN,
    550 		 * action will be SIG_DFL here.)
    551 		 */
    552 		if (p->p_sigignore & mask)
    553 			return;
    554 		if (p->p_sigmask & mask)
    555 			action = SIG_HOLD;
    556 		else if (p->p_sigcatch & mask)
    557 			action = SIG_CATCH;
    558 		else {
    559 			action = SIG_DFL;
    560 
    561 			if (prop & SA_KILL && p->p_nice > NZERO)
    562 				p->p_nice = NZERO;
    563 
    564 			/*
    565 			 * If sending a tty stop signal to a member of an
    566 			 * orphaned process group, discard the signal here if
    567 			 * the action is default; don't stop the process below
    568 			 * if sleeping, and don't clear any pending SIGCONT.
    569 			 */
    570 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
    571 				return;
    572 		}
    573 	}
    574 
    575 	if (prop & SA_CONT)
    576 		p->p_siglist &= ~stopsigmask;
    577 
    578 	if (prop & SA_STOP)
    579 		p->p_siglist &= ~contsigmask;
    580 
    581 	p->p_siglist |= mask;
    582 
    583 	/*
    584 	 * Defer further processing for signals which are held,
    585 	 * except that stopped processes must be continued by SIGCONT.
    586 	 */
    587 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
    588 		return;
    589 	s = splhigh();
    590 	switch (p->p_stat) {
    591 
    592 	case SSLEEP:
    593 		/*
    594 		 * If process is sleeping uninterruptibly
    595 		 * we can't interrupt the sleep... the signal will
    596 		 * be noticed when the process returns through
    597 		 * trap() or syscall().
    598 		 */
    599 		if ((p->p_flag & P_SINTR) == 0)
    600 			goto out;
    601 		/*
    602 		 * Process is sleeping and traced... make it runnable
    603 		 * so it can discover the signal in issignal() and stop
    604 		 * for the parent.
    605 		 */
    606 		if (p->p_flag & P_TRACED)
    607 			goto run;
    608 		/*
    609 		 * If SIGCONT is default (or ignored) and process is
    610 		 * asleep, we are finished; the process should not
    611 		 * be awakened.
    612 		 */
    613 		if ((prop & SA_CONT) && action == SIG_DFL) {
    614 			p->p_siglist &= ~mask;
    615 			goto out;
    616 		}
    617 		/*
    618 		 * When a sleeping process receives a stop
    619 		 * signal, process immediately if possible.
    620 		 */
    621 		if ((prop & SA_STOP) && action == SIG_DFL) {
    622 			/*
    623 			 * If a child holding parent blocked,
    624 			 * stopping could cause deadlock.
    625 			 */
    626 			if (p->p_flag & P_PPWAIT)
    627 				goto out;
    628 			p->p_siglist &= ~mask;
    629 			p->p_xstat = signum;
    630 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
    631 				psignal(p->p_pptr, SIGCHLD);
    632 			stop(p);
    633 			goto out;
    634 		}
    635 		/*
    636 		 * All other (caught or default) signals
    637 		 * cause the process to run.
    638 		 */
    639 		goto runfast;
    640 		/*NOTREACHED*/
    641 
    642 	case SSTOP:
    643 		/*
    644 		 * If traced process is already stopped,
    645 		 * then no further action is necessary.
    646 		 */
    647 		if (p->p_flag & P_TRACED)
    648 			goto out;
    649 
    650 		/*
    651 		 * Kill signal always sets processes running.
    652 		 */
    653 		if (signum == SIGKILL)
    654 			goto runfast;
    655 
    656 		if (prop & SA_CONT) {
    657 			/*
    658 			 * If SIGCONT is default (or ignored), we continue the
    659 			 * process but don't leave the signal in p_siglist, as
    660 			 * it has no further action.  If SIGCONT is held, we
    661 			 * continue the process and leave the signal in
    662 			 * p_siglist.  If the process catches SIGCONT, let it
    663 			 * handle the signal itself.  If it isn't waiting on
    664 			 * an event, then it goes back to run state.
    665 			 * Otherwise, process goes back to sleep state.
    666 			 */
    667 			if (action == SIG_DFL)
    668 				p->p_siglist &= ~mask;
    669 			if (action == SIG_CATCH)
    670 				goto runfast;
    671 			if (p->p_wchan == 0)
    672 				goto run;
    673 			p->p_stat = SSLEEP;
    674 			goto out;
    675 		}
    676 
    677 		if (prop & SA_STOP) {
    678 			/*
    679 			 * Already stopped, don't need to stop again.
    680 			 * (If we did the shell could get confused.)
    681 			 */
    682 			p->p_siglist &= ~mask;		/* take it away */
    683 			goto out;
    684 		}
    685 
    686 		/*
    687 		 * If process is sleeping interruptibly, then simulate a
    688 		 * wakeup so that when it is continued, it will be made
    689 		 * runnable and can look at the signal.  But don't make
    690 		 * the process runnable, leave it stopped.
    691 		 */
    692 		if (p->p_wchan && p->p_flag & P_SINTR)
    693 			unsleep(p);
    694 		goto out;
    695 
    696 	default:
    697 		/*
    698 		 * SRUN, SIDL, SZOMB do nothing with the signal,
    699 		 * other than kicking ourselves if we are running.
    700 		 * It will either never be noticed, or noticed very soon.
    701 		 */
    702 		if (p == curproc)
    703 			signotify(p);
    704 		goto out;
    705 	}
    706 	/*NOTREACHED*/
    707 
    708 runfast:
    709 	/*
    710 	 * Raise priority to at least PUSER.
    711 	 */
    712 	if (p->p_priority > PUSER)
    713 		p->p_priority = PUSER;
    714 run:
    715 	setrunnable(p);
    716 out:
    717 	splx(s);
    718 }
    719 
    720 /*
    721  * If the current process has received a signal (should be caught or cause
    722  * termination, should interrupt current syscall), return the signal number.
    723  * Stop signals with default action are processed immediately, then cleared;
    724  * they aren't returned.  This is checked after each entry to the system for
    725  * a syscall or trap (though this can usually be done without calling issignal
    726  * by checking the pending signal masks in the CURSIG macro.) The normal call
    727  * sequence is
    728  *
    729  *	while (signum = CURSIG(curproc))
    730  *		postsig(signum);
    731  */
    732 int
    733 issignal(p)
    734 	register struct proc *p;
    735 {
    736 	register int signum, mask, prop;
    737 
    738 	for (;;) {
    739 		mask = p->p_siglist & ~p->p_sigmask;
    740 		if (p->p_flag & P_PPWAIT)
    741 			mask &= ~stopsigmask;
    742 		if (mask == 0)	 	/* no signal to send */
    743 			return (0);
    744 		signum = ffs((long)mask);
    745 		mask = sigmask(signum);
    746 		p->p_siglist &= ~mask;		/* take the signal! */
    747 
    748 		/*
    749 		 * We should see pending but ignored signals
    750 		 * only if P_TRACED was on when they were posted.
    751 		 */
    752 		if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0)
    753 			continue;
    754 
    755 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
    756 			/*
    757 			 * If traced, always stop, and stay
    758 			 * stopped until released by the debugger.
    759 			 */
    760 			p->p_xstat = signum;
    761 
    762 			if (p->p_flag & P_FSTRACE) {
    763 #ifdef	PROCFS
    764 				/* procfs debugging */
    765 				p->p_stat = SSTOP;
    766 				wakeup((caddr_t)p);
    767 				mi_switch();
    768 #else
    769 				panic("procfs debugging");
    770 #endif
    771 			} else {
    772 				/* ptrace debugging */
    773 				psignal(p->p_pptr, SIGCHLD);
    774 				do {
    775 					stop(p);
    776 					mi_switch();
    777 				} while (!trace_req(p) && p->p_flag & P_TRACED);
    778 			}
    779 
    780 			/*
    781 			 * If we are no longer being traced, or the parent
    782 			 * didn't give us a signal, look for more signals.
    783 			 */
    784 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
    785 				continue;
    786 
    787 			/*
    788 			 * If the new signal is being masked, look for other
    789 			 * signals.
    790 			 */
    791 			signum = p->p_xstat;
    792 			mask = sigmask(signum);
    793 			if ((p->p_sigmask & mask) != 0)
    794 				continue;
    795 			p->p_siglist &= ~mask;		/* take the signal! */
    796 		}
    797 
    798 		prop = sigprop[signum];
    799 
    800 		/*
    801 		 * Decide whether the signal should be returned.
    802 		 * Return the signal's number, or fall through
    803 		 * to clear it from the pending mask.
    804 		 */
    805 		switch ((long)p->p_sigacts->ps_sigact[signum]) {
    806 
    807 		case (long)SIG_DFL:
    808 			/*
    809 			 * Don't take default actions on system processes.
    810 			 */
    811 			if (p->p_pid <= 1) {
    812 #ifdef DIAGNOSTIC
    813 				/*
    814 				 * Are you sure you want to ignore SIGSEGV
    815 				 * in init? XXX
    816 				 */
    817 				printf("Process (pid %d) got signal %d\n",
    818 				    p->p_pid, signum);
    819 #endif
    820 				break;		/* == ignore */
    821 			}
    822 			/*
    823 			 * If there is a pending stop signal to process
    824 			 * with default action, stop here,
    825 			 * then clear the signal.  However,
    826 			 * if process is member of an orphaned
    827 			 * process group, ignore tty stop signals.
    828 			 */
    829 			if (prop & SA_STOP) {
    830 				if (p->p_flag & P_TRACED ||
    831 		    		    (p->p_pgrp->pg_jobc == 0 &&
    832 				    prop & SA_TTYSTOP))
    833 					break;	/* == ignore */
    834 				p->p_xstat = signum;
    835 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
    836 					psignal(p->p_pptr, SIGCHLD);
    837 				stop(p);
    838 				mi_switch();
    839 				break;
    840 			} else if (prop & SA_IGNORE) {
    841 				/*
    842 				 * Except for SIGCONT, shouldn't get here.
    843 				 * Default action is to ignore; drop it.
    844 				 */
    845 				break;		/* == ignore */
    846 			} else
    847 				goto keep;
    848 			/*NOTREACHED*/
    849 
    850 		case (long)SIG_IGN:
    851 			/*
    852 			 * Masking above should prevent us ever trying
    853 			 * to take action on an ignored signal other
    854 			 * than SIGCONT, unless process is traced.
    855 			 */
    856 			if ((prop & SA_CONT) == 0 &&
    857 			    (p->p_flag & P_TRACED) == 0)
    858 				printf("issignal\n");
    859 			break;		/* == ignore */
    860 
    861 		default:
    862 			/*
    863 			 * This signal has an action, let
    864 			 * postsig() process it.
    865 			 */
    866 			goto keep;
    867 		}
    868 	}
    869 	/* NOTREACHED */
    870 
    871 keep:
    872 	p->p_siglist |= mask;		/* leave the signal for later */
    873 	return (signum);
    874 }
    875 
    876 /*
    877  * Put the argument process into the stopped state and notify the parent
    878  * via wakeup.  Signals are handled elsewhere.  The process must not be
    879  * on the run queue.
    880  */
    881 void
    882 stop(p)
    883 	register struct proc *p;
    884 {
    885 
    886 	p->p_stat = SSTOP;
    887 	p->p_flag &= ~P_WAITED;
    888 	wakeup((caddr_t)p->p_pptr);
    889 }
    890 
    891 /*
    892  * Take the action for the specified signal
    893  * from the current set of pending signals.
    894  */
    895 void
    896 postsig(signum)
    897 	register int signum;
    898 {
    899 	register struct proc *p = curproc;
    900 	register struct sigacts *ps = p->p_sigacts;
    901 	register sig_t action;
    902 	u_long code;
    903 	int mask, returnmask;
    904 
    905 #ifdef DIAGNOSTIC
    906 	if (signum == 0)
    907 		panic("postsig");
    908 #endif
    909 	mask = sigmask(signum);
    910 	p->p_siglist &= ~mask;
    911 	action = ps->ps_sigact[signum];
    912 #ifdef KTRACE
    913 	if (KTRPOINT(p, KTR_PSIG))
    914 		ktrpsig(p->p_tracep,
    915 		    signum, action, ps->ps_flags & SAS_OLDMASK ?
    916 		    ps->ps_oldmask : p->p_sigmask, 0);
    917 #endif
    918 	if (action == SIG_DFL) {
    919 		/*
    920 		 * Default action, where the default is to kill
    921 		 * the process.  (Other cases were ignored above.)
    922 		 */
    923 		sigexit(p, signum);
    924 		/* NOTREACHED */
    925 	} else {
    926 		/*
    927 		 * If we get here, the signal must be caught.
    928 		 */
    929 #ifdef DIAGNOSTIC
    930 		if (action == SIG_IGN || (p->p_sigmask & mask))
    931 			panic("postsig action");
    932 #endif
    933 		/*
    934 		 * Set the new mask value and also defer further
    935 		 * occurences of this signal.
    936 		 *
    937 		 * Special case: user has done a sigpause.  Here the
    938 		 * current mask is not of interest, but rather the
    939 		 * mask from before the sigpause is what we want
    940 		 * restored after the signal processing is completed.
    941 		 */
    942 		(void) splhigh();
    943 		if (ps->ps_flags & SAS_OLDMASK) {
    944 			returnmask = ps->ps_oldmask;
    945 			ps->ps_flags &= ~SAS_OLDMASK;
    946 		} else
    947 			returnmask = p->p_sigmask;
    948 		p->p_sigmask |= ps->ps_catchmask[signum] | mask;
    949 		(void) spl0();
    950 		p->p_stats->p_ru.ru_nsignals++;
    951 		if (ps->ps_sig != signum) {
    952 			code = 0;
    953 		} else {
    954 			code = ps->ps_code;
    955 			ps->ps_code = 0;
    956 		}
    957 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code);
    958 	}
    959 }
    960 
    961 /*
    962  * Kill the current process for stated reason.
    963  */
    964 killproc(p, why)
    965 	struct proc *p;
    966 	char *why;
    967 {
    968 
    969 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
    970 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
    971 	psignal(p, SIGKILL);
    972 }
    973 
    974 /*
    975  * Force the current process to exit with the specified signal, dumping core
    976  * if appropriate.  We bypass the normal tests for masked and caught signals,
    977  * allowing unrecoverable failures to terminate the process without changing
    978  * signal state.  Mark the accounting record with the signal termination.
    979  * If dumping core, save the signal number for the debugger.  Calls exit and
    980  * does not return.
    981  */
    982 int
    983 sigexit(p, signum)
    984 	register struct proc *p;
    985 	int signum;
    986 {
    987 
    988 	p->p_acflag |= AXSIG;
    989 	if (sigprop[signum] & SA_CORE) {
    990 		p->p_sigacts->ps_sig = signum;
    991 		if (coredump(p) == 0)
    992 			signum |= WCOREFLAG;
    993 	}
    994 	exit1(p, W_EXITCODE(0, signum));
    995 	/* NOTREACHED */
    996 }
    997 
    998 /*
    999  * Dump core, into a file named "progname.core", unless the process was
   1000  * setuid/setgid.
   1001  */
   1002 int
   1003 coredump(p)
   1004 	register struct proc *p;
   1005 {
   1006 	register struct vnode *vp;
   1007 	register struct pcred *pcred = p->p_cred;
   1008 	register struct ucred *cred = pcred->pc_ucred;
   1009 	register struct vmspace *vm = p->p_vmspace;
   1010 	struct nameidata nd;
   1011 	struct vattr vattr;
   1012 	int error, error1;
   1013 	char name[MAXCOMLEN+6];		/* progname.core */
   1014 	struct core core;
   1015 
   1016 	if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid)
   1017 		return (EFAULT);
   1018 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   1019 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   1020 		return (EFAULT);
   1021 	sprintf(name, "%s.core", p->p_comm);
   1022 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
   1023 	if (error = vn_open(&nd,
   1024 	    O_CREAT | FWRITE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
   1025 		return (error);
   1026 	vp = nd.ni_vp;
   1027 
   1028 	/* Don't dump to non-regular files or files with links. */
   1029 	if (vp->v_type != VREG ||
   1030 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
   1031 		error = EFAULT;
   1032 		goto out;
   1033 	}
   1034 	VATTR_NULL(&vattr);
   1035 	vattr.va_size = 0;
   1036 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1037 	VOP_SETATTR(vp, &vattr, cred, p);
   1038 	p->p_acflag |= ACORE;
   1039 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
   1040 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
   1041 
   1042 	core.c_midmag = 0;
   1043 	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
   1044 	core.c_nseg = 0;
   1045 	core.c_signo = p->p_sigacts->ps_sig;
   1046 	core.c_ucode = p->p_sigacts->ps_code;
   1047 	core.c_cpusize = 0;
   1048 	core.c_tsize = (u_long)ctob(vm->vm_tsize);
   1049 	core.c_dsize = (u_long)ctob(vm->vm_dsize);
   1050 	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
   1051 	error = cpu_coredump(p, vp, cred, &core);
   1052 	if (error)
   1053 		goto out;
   1054 	if (core.c_midmag == 0) {
   1055 		/* XXX
   1056 		 * cpu_coredump() didn't bother to set the magic; assume
   1057 		 * this is a request to do a traditional dump. cpu_coredump()
   1058 		 * is still responsible for setting sensible values in
   1059 		 * the core header.
   1060 		 */
   1061 		if (core.c_cpusize == 0)
   1062 			core.c_cpusize = USPACE; /* Just in case */
   1063 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
   1064 		    (int)core.c_dsize,
   1065 		    (off_t)core.c_cpusize, UIO_USERSPACE,
   1066 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
   1067 		if (error)
   1068 			goto out;
   1069 		error = vn_rdwr(UIO_WRITE, vp,
   1070 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
   1071 		    core.c_ssize,
   1072 		    (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
   1073 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
   1074 	} else {
   1075 		/*
   1076 		 * vm_coredump() spits out all appropriate segments.
   1077 		 * All that's left to do is to write the core header.
   1078 		 */
   1079 		error = vm_coredump(p, vp, cred, &core);
   1080 		if (error)
   1081 			goto out;
   1082 		error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
   1083 		    (int)core.c_hdrsize, (off_t)0,
   1084 		    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
   1085 	}
   1086 out:
   1087 	VOP_UNLOCK(vp);
   1088 	error1 = vn_close(vp, FWRITE, cred, p);
   1089 	if (error == 0)
   1090 		error = error1;
   1091 	return (error);
   1092 }
   1093 
   1094 /*
   1095  * Nonexistent system call-- signal process (may want to handle it).
   1096  * Flag error in case process won't see signal immediately (blocked or ignored).
   1097  */
   1098 /* ARGSUSED */
   1099 int
   1100 nosys(p, args, retval)
   1101 	struct proc *p;
   1102 	void *args;
   1103 	register_t *retval;
   1104 {
   1105 
   1106 	psignal(p, SIGSYS);
   1107 	return (ENOSYS);
   1108 }
   1109