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