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