xref: /csrg-svn/sys/kern/kern_sig.c (revision 29946)
123374Smckusick /*
229094Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323374Smckusick  * All rights reserved.  The Berkeley software License Agreement
423374Smckusick  * specifies the terms and conditions for redistribution.
523374Smckusick  *
6*29946Skarels  *	@(#)kern_sig.c	7.2 (Berkeley) 11/03/86
723374Smckusick  */
87421Sroot 
99755Ssam #include "../machine/reg.h"
109755Ssam #include "../machine/pte.h"
119755Ssam #include "../machine/psl.h"
12*29946Skarels #include "../machine/mtpr.h"
139755Ssam 
1417092Sbloom #include "param.h"
1517092Sbloom #include "systm.h"
1617092Sbloom #include "dir.h"
1717092Sbloom #include "user.h"
1817092Sbloom #include "inode.h"
1917092Sbloom #include "proc.h"
2017092Sbloom #include "timeb.h"
2117092Sbloom #include "times.h"
2217092Sbloom #include "buf.h"
2317092Sbloom #include "mount.h"
2417092Sbloom #include "text.h"
2517092Sbloom #include "seg.h"
2617092Sbloom #include "vm.h"
2717092Sbloom #include "acct.h"
2817092Sbloom #include "uio.h"
2917092Sbloom #include "kernel.h"
307421Sroot 
3117153Sbloom #define	cantmask	(sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP))
3226276Skarels #define	stopsigmask	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
3326276Skarels 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
3412951Ssam 
3517013Smckusick /*
3617013Smckusick  * Generalized interface signal handler.
3717013Smckusick  */
387499Sroot sigvec()
397421Sroot {
4012951Ssam 	register struct a {
4112882Ssam 		int	signo;
4212951Ssam 		struct	sigvec *nsv;
4312951Ssam 		struct	sigvec *osv;
4412882Ssam 	} *uap = (struct a  *)u.u_ap;
4512951Ssam 	struct sigvec vec;
4612951Ssam 	register struct sigvec *sv;
4712882Ssam 	register int sig;
4818308Smckusick 	int bit;
497421Sroot 
5012882Ssam 	sig = uap->signo;
5112951Ssam 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) {
5212882Ssam 		u.u_error = EINVAL;
5312882Ssam 		return;
5412882Ssam 	}
5512951Ssam 	sv = &vec;
5612951Ssam 	if (uap->osv) {
5712951Ssam 		sv->sv_handler = u.u_signal[sig];
5812951Ssam 		sv->sv_mask = u.u_sigmask[sig];
5918308Smckusick 		bit = sigmask(sig);
6018308Smckusick 		sv->sv_flags = 0;
6118308Smckusick 		if ((u.u_sigonstack & bit) != 0)
6218308Smckusick 			sv->sv_flags |= SV_ONSTACK;
6318308Smckusick 		if ((u.u_sigintr & bit) != 0)
6418308Smckusick 			sv->sv_flags |= SV_INTERRUPT;
6512951Ssam 		u.u_error =
6612951Ssam 		    copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec));
6712951Ssam 		if (u.u_error)
6812951Ssam 			return;
6912951Ssam 	}
7012951Ssam 	if (uap->nsv) {
7112951Ssam 		u.u_error =
7212951Ssam 		    copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec));
7312951Ssam 		if (u.u_error)
7412951Ssam 			return;
7512951Ssam 		if (sig == SIGCONT && sv->sv_handler == SIG_IGN) {
7612951Ssam 			u.u_error = EINVAL;
7712951Ssam 			return;
7812951Ssam 		}
7912951Ssam 		setsigvec(sig, sv);
8012951Ssam 	}
817421Sroot }
827421Sroot 
8312951Ssam setsigvec(sig, sv)
8412951Ssam 	int sig;
8512951Ssam 	register struct sigvec *sv;
8612882Ssam {
8712882Ssam 	register struct proc *p;
8812951Ssam 	register int bit;
8912882Ssam 
9017153Sbloom 	bit = sigmask(sig);
9112882Ssam 	p = u.u_procp;
9212882Ssam 	/*
9312882Ssam 	 * Change setting atomically.
9412882Ssam 	 */
9517153Sbloom 	(void) splhigh();
9612951Ssam 	u.u_signal[sig] = sv->sv_handler;
9712951Ssam 	u.u_sigmask[sig] = sv->sv_mask &~ cantmask;
9818308Smckusick 	if (sv->sv_flags & SV_INTERRUPT)
9918308Smckusick 		u.u_sigintr |= bit;
10018308Smckusick 	else
10118308Smckusick 		u.u_sigintr &= ~bit;
10218308Smckusick 	if (sv->sv_flags & SV_ONSTACK)
10312951Ssam 		u.u_sigonstack |= bit;
10412951Ssam 	else
10512951Ssam 		u.u_sigonstack &= ~bit;
10612951Ssam 	if (sv->sv_handler == SIG_IGN) {
10712951Ssam 		p->p_sig &= ~bit;		/* never to be seen again */
10812951Ssam 		p->p_sigignore |= bit;
10912951Ssam 		p->p_sigcatch &= ~bit;
11012882Ssam 	} else {
11112951Ssam 		p->p_sigignore &= ~bit;
11212951Ssam 		if (sv->sv_handler == SIG_DFL)
11312951Ssam 			p->p_sigcatch &= ~bit;
11412882Ssam 		else
11512951Ssam 			p->p_sigcatch |= bit;
11612882Ssam 	}
11712882Ssam 	(void) spl0();
11812882Ssam }
11912882Ssam 
1207499Sroot sigblock()
1217421Sroot {
12212882Ssam 	struct a {
12317153Sbloom 		int	mask;
12412882Ssam 	} *uap = (struct a *)u.u_ap;
12512951Ssam 	register struct proc *p = u.u_procp;
1267499Sroot 
12717153Sbloom 	(void) splhigh();
12812882Ssam 	u.u_r.r_val1 = p->p_sigmask;
12917153Sbloom 	p->p_sigmask |= uap->mask &~ cantmask;
13012882Ssam 	(void) spl0();
1317499Sroot }
1327499Sroot 
1337499Sroot sigsetmask()
1347499Sroot {
13512882Ssam 	struct a {
13617153Sbloom 		int	mask;
13712882Ssam 	} *uap = (struct a *)u.u_ap;
13812882Ssam 	register struct proc *p = u.u_procp;
1397499Sroot 
14017153Sbloom 	(void) splhigh();
14112882Ssam 	u.u_r.r_val1 = p->p_sigmask;
14217153Sbloom 	p->p_sigmask = uap->mask &~ cantmask;
14312882Ssam 	(void) spl0();
1447499Sroot }
1457499Sroot 
1467499Sroot sigpause()
1477499Sroot {
14812882Ssam 	struct a {
14917153Sbloom 		int	mask;
15012882Ssam 	} *uap = (struct a *)u.u_ap;
15112882Ssam 	register struct proc *p = u.u_procp;
1527499Sroot 
15312882Ssam 	/*
15412882Ssam 	 * When returning from sigpause, we want
15512882Ssam 	 * the old mask to be restored after the
15612882Ssam 	 * signal handler has finished.  Thus, we
15712882Ssam 	 * save it here and mark the proc structure
15812882Ssam 	 * to indicate this (should be in u.).
15912882Ssam 	 */
16012882Ssam 	u.u_oldmask = p->p_sigmask;
16112882Ssam 	p->p_flag |= SOMASK;
16217153Sbloom 	p->p_sigmask = uap->mask &~ cantmask;
16312882Ssam 	for (;;)
16412882Ssam 		sleep((caddr_t)&u, PSLEP);
16512882Ssam 	/*NOTREACHED*/
1667499Sroot }
16712951Ssam #undef cantmask
1687499Sroot 
1697499Sroot sigstack()
1707499Sroot {
17112951Ssam 	register struct a {
17212951Ssam 		struct	sigstack *nss;
17312951Ssam 		struct	sigstack *oss;
17412882Ssam 	} *uap = (struct a *)u.u_ap;
17512951Ssam 	struct sigstack ss;
1767499Sroot 
17712951Ssam 	if (uap->oss) {
17812951Ssam 		u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss,
17912951Ssam 		    sizeof (struct sigstack));
18012951Ssam 		if (u.u_error)
18112951Ssam 			return;
18212951Ssam 	}
18312951Ssam 	if (uap->nss) {
18412951Ssam 		u.u_error =
18512951Ssam 		    copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss));
18612951Ssam 		if (u.u_error == 0)
18712951Ssam 			u.u_sigstack = ss;
18812951Ssam 	}
1897499Sroot }
1907499Sroot 
1918032Sroot kill()
1928032Sroot {
19312882Ssam 	register struct a {
19412882Ssam 		int	pid;
19512882Ssam 		int	signo;
19612882Ssam 	} *uap = (struct a *)u.u_ap;
19718336Smckusick 	register struct proc *p;
1988032Sroot 
19918336Smckusick 	if (uap->signo < 0 || uap->signo > NSIG) {
20018336Smckusick 		u.u_error = EINVAL;
20118336Smckusick 		return;
20218336Smckusick 	}
20318336Smckusick 	if (uap->pid > 0) {
20418336Smckusick 		/* kill single process */
20518336Smckusick 		p = pfind(uap->pid);
20618336Smckusick 		if (p == 0) {
20718336Smckusick 			u.u_error = ESRCH;
20818336Smckusick 			return;
20918336Smckusick 		}
21018336Smckusick 		if (u.u_uid && u.u_uid != p->p_uid)
21118336Smckusick 			u.u_error = EPERM;
21218336Smckusick 		else if (uap->signo)
21318336Smckusick 			psignal(p, uap->signo);
21418336Smckusick 		return;
21518336Smckusick 	}
21618336Smckusick 	switch (uap->pid) {
21718336Smckusick 	case -1:		/* broadcast signal */
21820998Smckusick 		u.u_error = killpg1(uap->signo, 0, 1);
21918336Smckusick 		break;
22018336Smckusick 	case 0:			/* signal own process group */
22118336Smckusick 		u.u_error = killpg1(uap->signo, 0, 0);
22218336Smckusick 		break;
22318336Smckusick 	default:		/* negative explicit process group */
22418336Smckusick 		u.u_error = killpg1(uap->signo, -uap->pid, 0);
22518336Smckusick 		break;
22618336Smckusick 	}
22718336Smckusick 	return;
2288032Sroot }
2298032Sroot 
2308032Sroot killpg()
2318032Sroot {
2329989Ssam 	register struct a {
2339989Ssam 		int	pgrp;
2349989Ssam 		int	signo;
2359989Ssam 	} *uap = (struct a *)u.u_ap;
2368032Sroot 
23718336Smckusick 	if (uap->signo < 0 || uap->signo > NSIG) {
23818336Smckusick 		u.u_error = EINVAL;
23918336Smckusick 		return;
24018336Smckusick 	}
24118336Smckusick 	u.u_error = killpg1(uap->signo, uap->pgrp, 0);
2428032Sroot }
2438032Sroot 
24412882Ssam /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
24512882Ssam 
24618336Smckusick killpg1(signo, pgrp, all)
24718336Smckusick 	int signo, pgrp, all;
2489989Ssam {
2499989Ssam 	register struct proc *p;
25018336Smckusick 	int f, error = 0;
2519989Ssam 
25218336Smckusick 	if (!all && pgrp == 0) {
2537421Sroot 		/*
2547421Sroot 		 * Zero process id means send to my process group.
2557421Sroot 		 */
25618336Smckusick 		pgrp = u.u_procp->p_pgrp;
25718336Smckusick 		if (pgrp == 0)
25820998Smckusick 			return (ESRCH);
2597421Sroot 	}
26016531Skarels 	for (f = 0, p = allproc; p != NULL; p = p->p_nxt) {
26118336Smckusick 		if ((p->p_pgrp != pgrp && !all) || p->p_ppid == 0 ||
26218336Smckusick 		    (p->p_flag&SSYS) || (all && p == u.u_procp))
2637421Sroot 			continue;
2647421Sroot 		if (u.u_uid != 0 && u.u_uid != p->p_uid &&
26518336Smckusick 		    (signo != SIGCONT || !inferior(p))) {
26620998Smckusick 			if (!all)
26720998Smckusick 				error = EPERM;
2687421Sroot 			continue;
26918336Smckusick 		}
27020998Smckusick 		f++;
27112835Ssam 		if (signo)
27212835Ssam 			psignal(p, signo);
2737421Sroot 	}
27424420Skarels 	return (error ? error : (f == 0 ? ESRCH : 0));
2757421Sroot }
2767421Sroot 
2777421Sroot /*
2787421Sroot  * Send the specified signal to
2797421Sroot  * all processes with 'pgrp' as
2807421Sroot  * process group.
2817421Sroot  */
2827421Sroot gsignal(pgrp, sig)
2837421Sroot 	register int pgrp;
2847421Sroot {
2857421Sroot 	register struct proc *p;
2867421Sroot 
2877421Sroot 	if (pgrp == 0)
2887421Sroot 		return;
28916531Skarels 	for (p = allproc; p != NULL; p = p->p_nxt)
2907421Sroot 		if (p->p_pgrp == pgrp)
2917421Sroot 			psignal(p, sig);
2927421Sroot }
2937421Sroot 
2947421Sroot /*
2957421Sroot  * Send the specified signal to
2967421Sroot  * the specified process.
2977421Sroot  */
2987421Sroot psignal(p, sig)
2997421Sroot 	register struct proc *p;
3007421Sroot 	register int sig;
3017421Sroot {
3027421Sroot 	register int s;
3037421Sroot 	register int (*action)();
30417153Sbloom 	int mask;
3057421Sroot 
3067421Sroot 	if ((unsigned)sig >= NSIG)
3077421Sroot 		return;
30817153Sbloom 	mask = sigmask(sig);
3097421Sroot 
3107421Sroot 	/*
3117421Sroot 	 * If proc is traced, always give parent a chance.
3127421Sroot 	 */
3137421Sroot 	if (p->p_flag & STRC)
3147421Sroot 		action = SIG_DFL;
3157421Sroot 	else {
3167421Sroot 		/*
31712882Ssam 		 * If the signal is being ignored,
31812882Ssam 		 * then we forget about it immediately.
3197421Sroot 		 */
32017153Sbloom 		if (p->p_sigignore & mask)
3217421Sroot 			return;
32217153Sbloom 		if (p->p_sigmask & mask)
32312882Ssam 			action = SIG_HOLD;
32417153Sbloom 		else if (p->p_sigcatch & mask)
32512882Ssam 			action = SIG_CATCH;
32612882Ssam 		else
32712882Ssam 			action = SIG_DFL;
3287421Sroot 	}
3297421Sroot 	if (sig) {
33017153Sbloom 		p->p_sig |= mask;
3317421Sroot 		switch (sig) {
3327421Sroot 
3337421Sroot 		case SIGTERM:
33412882Ssam 			if ((p->p_flag&STRC) || action != SIG_DFL)
3357421Sroot 				break;
3367421Sroot 			/* fall into ... */
3377421Sroot 
3387421Sroot 		case SIGKILL:
3397421Sroot 			if (p->p_nice > NZERO)
3407421Sroot 				p->p_nice = NZERO;
3417421Sroot 			break;
3427421Sroot 
3437421Sroot 		case SIGCONT:
34426276Skarels 			p->p_sig &= ~stopsigmask;
3457421Sroot 			break;
3467421Sroot 
3477421Sroot 		case SIGSTOP:
3487421Sroot 		case SIGTSTP:
3497421Sroot 		case SIGTTIN:
3507421Sroot 		case SIGTTOU:
35117153Sbloom 			p->p_sig &= ~sigmask(SIGCONT);
3527421Sroot 			break;
3537421Sroot 		}
3547421Sroot 	}
3557421Sroot 	/*
3567421Sroot 	 * Defer further processing for signals which are held.
3577421Sroot 	 */
3587421Sroot 	if (action == SIG_HOLD)
3597421Sroot 		return;
36017153Sbloom 	s = splhigh();
3617421Sroot 	switch (p->p_stat) {
3627421Sroot 
3637421Sroot 	case SSLEEP:
3647421Sroot 		/*
3657421Sroot 		 * If process is sleeping at negative priority
3667421Sroot 		 * we can't interrupt the sleep... the signal will
3677421Sroot 		 * be noticed when the process returns through
3687421Sroot 		 * trap() or syscall().
3697421Sroot 		 */
3707421Sroot 		if (p->p_pri <= PZERO)
3717421Sroot 			goto out;
3727421Sroot 		/*
3737421Sroot 		 * Process is sleeping and traced... make it runnable
3747421Sroot 		 * so it can discover the signal in issig() and stop
3757421Sroot 		 * for the parent.
3767421Sroot 		 */
3777421Sroot 		if (p->p_flag&STRC)
3787421Sroot 			goto run;
3797421Sroot 		switch (sig) {
3807421Sroot 
3817421Sroot 		case SIGSTOP:
3827421Sroot 		case SIGTSTP:
3837421Sroot 		case SIGTTIN:
3847421Sroot 		case SIGTTOU:
3857421Sroot 			/*
3867421Sroot 			 * These are the signals which by default
3877421Sroot 			 * stop a process.
3887421Sroot 			 */
3897421Sroot 			if (action != SIG_DFL)
3907421Sroot 				goto run;
3917421Sroot 			/*
3927421Sroot 			 * Don't clog system with children of init
3937421Sroot 			 * stopped from the keyboard.
3947421Sroot 			 */
3957421Sroot 			if (sig != SIGSTOP && p->p_pptr == &proc[1]) {
3967421Sroot 				psignal(p, SIGKILL);
39717153Sbloom 				p->p_sig &= ~mask;
3987421Sroot 				splx(s);
3997421Sroot 				return;
4007421Sroot 			}
4017421Sroot 			/*
4027421Sroot 			 * If a child in vfork(), stopping could
4037421Sroot 			 * cause deadlock.
4047421Sroot 			 */
4057421Sroot 			if (p->p_flag&SVFORK)
4067421Sroot 				goto out;
40717153Sbloom 			p->p_sig &= ~mask;
4087421Sroot 			p->p_cursig = sig;
40918331Skarels 			psignal(p->p_pptr, SIGCHLD);
4107421Sroot 			stop(p);
4117421Sroot 			goto out;
4127421Sroot 
4137421Sroot 		case SIGIO:
4147421Sroot 		case SIGURG:
4157421Sroot 		case SIGCHLD:
41617597Sbloom 		case SIGWINCH:
4177421Sroot 			/*
4187421Sroot 			 * These signals are special in that they
4197421Sroot 			 * don't get propogated... if the process
4207421Sroot 			 * isn't interested, forget it.
4217421Sroot 			 */
4227421Sroot 			if (action != SIG_DFL)
4237421Sroot 				goto run;
42417153Sbloom 			p->p_sig &= ~mask;		/* take it away */
4257421Sroot 			goto out;
4267421Sroot 
4277421Sroot 		default:
4287421Sroot 			/*
4297421Sroot 			 * All other signals cause the process to run
4307421Sroot 			 */
4317421Sroot 			goto run;
4327421Sroot 		}
4337421Sroot 		/*NOTREACHED*/
4347421Sroot 
4357421Sroot 	case SSTOP:
4367421Sroot 		/*
4377421Sroot 		 * If traced process is already stopped,
4387421Sroot 		 * then no further action is necessary.
4397421Sroot 		 */
4407421Sroot 		if (p->p_flag&STRC)
4417421Sroot 			goto out;
4427421Sroot 		switch (sig) {
4437421Sroot 
4447421Sroot 		case SIGKILL:
4457421Sroot 			/*
4467421Sroot 			 * Kill signal always sets processes running.
4477421Sroot 			 */
4487421Sroot 			goto run;
4497421Sroot 
4507421Sroot 		case SIGCONT:
4517421Sroot 			/*
4527421Sroot 			 * If the process catches SIGCONT, let it handle
4537421Sroot 			 * the signal itself.  If it isn't waiting on
4547421Sroot 			 * an event, then it goes back to run state.
4557421Sroot 			 * Otherwise, process goes back to sleep state.
4567421Sroot 			 */
4577421Sroot 			if (action != SIG_DFL || p->p_wchan == 0)
4587421Sroot 				goto run;
4597421Sroot 			p->p_stat = SSLEEP;
4607421Sroot 			goto out;
4617421Sroot 
4627421Sroot 		case SIGSTOP:
4637421Sroot 		case SIGTSTP:
4647421Sroot 		case SIGTTIN:
4657421Sroot 		case SIGTTOU:
4667421Sroot 			/*
4677421Sroot 			 * Already stopped, don't need to stop again.
4687421Sroot 			 * (If we did the shell could get confused.)
4697421Sroot 			 */
47017153Sbloom 			p->p_sig &= ~mask;		/* take it away */
4717421Sroot 			goto out;
4727421Sroot 
4737421Sroot 		default:
4747421Sroot 			/*
4757421Sroot 			 * If process is sleeping interruptibly, then
4767421Sroot 			 * unstick it so that when it is continued
4777421Sroot 			 * it can look at the signal.
4787421Sroot 			 * But don't setrun the process as its not to
4797421Sroot 			 * be unstopped by the signal alone.
4807421Sroot 			 */
4817421Sroot 			if (p->p_wchan && p->p_pri > PZERO)
4827421Sroot 				unsleep(p);
4837421Sroot 			goto out;
4847421Sroot 		}
4857421Sroot 		/*NOTREACHED*/
4867421Sroot 
4877421Sroot 	default:
4887421Sroot 		/*
4897421Sroot 		 * SRUN, SIDL, SZOMB do nothing with the signal,
4907421Sroot 		 * other than kicking ourselves if we are running.
4917421Sroot 		 * It will either never be noticed, or noticed very soon.
4927421Sroot 		 */
4937421Sroot 		if (p == u.u_procp && !noproc)
4947421Sroot 			aston();
4957421Sroot 		goto out;
4967421Sroot 	}
4977421Sroot 	/*NOTREACHED*/
4987421Sroot run:
4997421Sroot 	/*
5007421Sroot 	 * Raise priority to at least PUSER.
5017421Sroot 	 */
5027421Sroot 	if (p->p_pri > PUSER)
50317399Skarels 		p->p_pri = PUSER;
5047421Sroot 	setrun(p);
5057421Sroot out:
5067421Sroot 	splx(s);
5077421Sroot }
5087421Sroot 
5097421Sroot /*
5107421Sroot  * Returns true if the current
5117421Sroot  * process has a signal to process.
5127421Sroot  * The signal to process is put in p_cursig.
5137421Sroot  * This is asked at least once each time a process enters the
5147421Sroot  * system (though this can usually be done without actually
5157421Sroot  * calling issig by checking the pending signal masks.)
5167421Sroot  * A signal does not do anything
5177421Sroot  * directly to a process; it sets
5187421Sroot  * a flag that asks the process to
5197421Sroot  * do something to itself.
5207421Sroot  */
5217421Sroot issig()
5227421Sroot {
5237421Sroot 	register struct proc *p;
5247421Sroot 	register int sig;
52517153Sbloom 	int sigbits, mask;
5267421Sroot 
5277421Sroot 	p = u.u_procp;
5287421Sroot 	for (;;) {
52914782Ssam 		sigbits = p->p_sig &~ p->p_sigmask;
5307421Sroot 		if ((p->p_flag&STRC) == 0)
53114782Ssam 			sigbits &= ~p->p_sigignore;
5327421Sroot 		if (p->p_flag&SVFORK)
53326276Skarels 			sigbits &= ~stopsigmask;
5347421Sroot 		if (sigbits == 0)
5357421Sroot 			break;
53626354Skarels 		sig = ffs((long)sigbits);
53717153Sbloom 		mask = sigmask(sig);
53817153Sbloom 		p->p_sig &= ~mask;		/* take the signal! */
5397421Sroot 		p->p_cursig = sig;
54012882Ssam 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
5417421Sroot 			/*
5427421Sroot 			 * If traced, always stop, and stay
5437421Sroot 			 * stopped until released by the parent.
5447421Sroot 			 */
54518331Skarels 			psignal(p->p_pptr, SIGCHLD);
5467421Sroot 			do {
5477421Sroot 				stop(p);
5487421Sroot 				swtch();
5497421Sroot 			} while (!procxmt() && p->p_flag&STRC);
5507421Sroot 
5517421Sroot 			/*
55214782Ssam 			 * If the traced bit got turned off,
55314782Ssam 			 * then put the signal taken above back into p_sig
55414782Ssam 			 * and go back up to the top to rescan signals.
55514782Ssam 			 * This ensures that p_sig* and u_signal are consistent.
5567421Sroot 			 */
55714782Ssam 			if ((p->p_flag&STRC) == 0) {
55817153Sbloom 				p->p_sig |= mask;
5597421Sroot 				continue;
5607421Sroot 			}
5617421Sroot 
5627421Sroot 			/*
5637421Sroot 			 * If parent wants us to take the signal,
5647421Sroot 			 * then it will leave it in p->p_cursig;
5657421Sroot 			 * otherwise we just look for signals again.
5667421Sroot 			 */
5677421Sroot 			sig = p->p_cursig;
5687421Sroot 			if (sig == 0)
5697421Sroot 				continue;
57014782Ssam 
57114782Ssam 			/*
57214782Ssam 			 * If signal is being masked put it back
57314782Ssam 			 * into p_sig and look for other signals.
57414782Ssam 			 */
57517153Sbloom 			mask = sigmask(sig);
57617153Sbloom 			if (p->p_sigmask & mask) {
57717153Sbloom 				p->p_sig |= mask;
57814782Ssam 				continue;
57914782Ssam 			}
5807421Sroot 		}
58124901Skarels 		switch ((int)u.u_signal[sig]) {
5827421Sroot 
5837421Sroot 		case SIG_DFL:
5847421Sroot 			/*
5857421Sroot 			 * Don't take default actions on system processes.
5867421Sroot 			 */
5877421Sroot 			if (p->p_ppid == 0)
5887421Sroot 				break;
5897421Sroot 			switch (sig) {
5907421Sroot 
5917421Sroot 			case SIGTSTP:
5927421Sroot 			case SIGTTIN:
5937421Sroot 			case SIGTTOU:
5947421Sroot 				/*
5957421Sroot 				 * Children of init aren't allowed to stop
5967421Sroot 				 * on signals from the keyboard.
5977421Sroot 				 */
5987421Sroot 				if (p->p_pptr == &proc[1]) {
5997421Sroot 					psignal(p, SIGKILL);
6007421Sroot 					continue;
6017421Sroot 				}
6027421Sroot 				/* fall into ... */
6037421Sroot 
6047421Sroot 			case SIGSTOP:
6057421Sroot 				if (p->p_flag&STRC)
6067421Sroot 					continue;
60718331Skarels 				psignal(p->p_pptr, SIGCHLD);
6087421Sroot 				stop(p);
6097421Sroot 				swtch();
6107421Sroot 				continue;
6117421Sroot 
6127421Sroot 			case SIGCONT:
6137421Sroot 			case SIGCHLD:
61412882Ssam 			case SIGURG:
61512951Ssam 			case SIGIO:
61617597Sbloom 			case SIGWINCH:
6177421Sroot 				/*
6187421Sroot 				 * These signals are normally not
6197421Sroot 				 * sent if the action is the default.
6207421Sroot 				 */
6217421Sroot 				continue;		/* == ignore */
6227421Sroot 
6237421Sroot 			default:
6247421Sroot 				goto send;
6257421Sroot 			}
6267421Sroot 			/*NOTREACHED*/
6277421Sroot 
6287421Sroot 		case SIG_HOLD:
6297421Sroot 		case SIG_IGN:
6307421Sroot 			/*
6317421Sroot 			 * Masking above should prevent us
6327421Sroot 			 * ever trying to take action on a held
6337421Sroot 			 * or ignored signal, unless process is traced.
6347421Sroot 			 */
6357421Sroot 			if ((p->p_flag&STRC) == 0)
6367421Sroot 				printf("issig\n");
6377421Sroot 			continue;
6387421Sroot 
6397421Sroot 		default:
6407421Sroot 			/*
6417421Sroot 			 * This signal has an action, let
6427421Sroot 			 * psig process it.
6437421Sroot 			 */
6447421Sroot 			goto send;
6457421Sroot 		}
6467421Sroot 		/*NOTREACHED*/
6477421Sroot 	}
6487421Sroot 	/*
6497421Sroot 	 * Didn't find a signal to send.
6507421Sroot 	 */
6517421Sroot 	p->p_cursig = 0;
6527421Sroot 	return (0);
6537421Sroot 
6547421Sroot send:
6557421Sroot 	/*
6567421Sroot 	 * Let psig process the signal.
6577421Sroot 	 */
6587421Sroot 	return (sig);
6597421Sroot }
6607421Sroot 
6617421Sroot /*
6627421Sroot  * Put the argument process into the stopped
66318331Skarels  * state and notify the parent via wakeup.
66418331Skarels  * Signals are handled elsewhere.
6657421Sroot  */
6667421Sroot stop(p)
6677421Sroot 	register struct proc *p;
6687421Sroot {
6697421Sroot 
6707421Sroot 	p->p_stat = SSTOP;
6717421Sroot 	p->p_flag &= ~SWTED;
6727421Sroot 	wakeup((caddr_t)p->p_pptr);
6737421Sroot }
6747421Sroot 
6757421Sroot /*
6767421Sroot  * Perform the action specified by
6777421Sroot  * the current signal.
6787421Sroot  * The usual sequence is:
6797421Sroot  *	if (issig())
6807421Sroot  *		psig();
6817421Sroot  * The signal bit has already been cleared by issig,
6827421Sroot  * and the current signal number stored in p->p_cursig.
6837421Sroot  */
6847421Sroot psig()
6857421Sroot {
68612882Ssam 	register struct proc *p = u.u_procp;
68712882Ssam 	register int sig = p->p_cursig;
68817153Sbloom 	int mask = sigmask(sig), returnmask;
6897421Sroot 	register int (*action)();
6907421Sroot 
69112882Ssam 	if (sig == 0)
6927421Sroot 		panic("psig");
69312882Ssam 	action = u.u_signal[sig];
6947421Sroot 	if (action != SIG_DFL) {
69517153Sbloom 		if (action == SIG_IGN || (p->p_sigmask & mask))
6967421Sroot 			panic("psig action");
6977421Sroot 		u.u_error = 0;
6987421Sroot 		/*
69912882Ssam 		 * Set the new mask value and also defer further
70012882Ssam 		 * occurences of this signal (unless we're simulating
70112882Ssam 		 * the old signal facilities).
70212882Ssam 		 *
70312882Ssam 		 * Special case: user has done a sigpause.  Here the
70412882Ssam 		 * current mask is not of interest, but rather the
70512882Ssam 		 * mask from before the sigpause is what we want restored
70612882Ssam 		 * after the signal processing is completed.
7077421Sroot 		 */
70817153Sbloom 		(void) splhigh();
70912882Ssam 		if (p->p_flag & SOUSIG) {
71012882Ssam 			if (sig != SIGILL && sig != SIGTRAP) {
71112882Ssam 				u.u_signal[sig] = SIG_DFL;
71217153Sbloom 				p->p_sigcatch &= ~mask;
71312882Ssam 			}
71417153Sbloom 			mask = 0;
7157421Sroot 		}
71612882Ssam 		if (p->p_flag & SOMASK) {
71712882Ssam 			returnmask = u.u_oldmask;
71812882Ssam 			p->p_flag &= ~SOMASK;
71912882Ssam 		} else
72012882Ssam 			returnmask = p->p_sigmask;
72117153Sbloom 		p->p_sigmask |= u.u_sigmask[sig] | mask;
72212882Ssam 		(void) spl0();
7238032Sroot 		u.u_ru.ru_nsignals++;
72412882Ssam 		sendsig(action, sig, returnmask);
72512882Ssam 		p->p_cursig = 0;
7267421Sroot 		return;
7277421Sroot 	}
7287421Sroot 	u.u_acflag |= AXSIG;
72912882Ssam 	switch (sig) {
7307421Sroot 
7317421Sroot 	case SIGILL:
7327421Sroot 	case SIGIOT:
7337421Sroot 	case SIGBUS:
7347421Sroot 	case SIGQUIT:
7357421Sroot 	case SIGTRAP:
7367421Sroot 	case SIGEMT:
7377421Sroot 	case SIGFPE:
7387421Sroot 	case SIGSEGV:
7397421Sroot 	case SIGSYS:
74012882Ssam 		u.u_arg[0] = sig;
7417421Sroot 		if (core())
74212882Ssam 			sig += 0200;
7437421Sroot 	}
74412882Ssam 	exit(sig);
7457421Sroot }
7467421Sroot 
7477421Sroot /*
7487421Sroot  * Create a core image on the file "core"
7497421Sroot  * If you are looking for protection glitches,
7507421Sroot  * there are probably a wealth of them here
7517421Sroot  * when this occurs to a suid command.
7527421Sroot  *
7537421Sroot  * It writes UPAGES block of the
7547421Sroot  * user.h area followed by the entire
7557421Sroot  * data+stack segments.
7567421Sroot  */
7577421Sroot core()
7587421Sroot {
7597421Sroot 	register struct inode *ip;
76016692Smckusick 	register struct nameidata *ndp = &u.u_nd;
7617421Sroot 
76212639Ssam 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
7637818Sroot 		return (0);
7648032Sroot 	if (ctob(UPAGES+u.u_dsize+u.u_ssize) >=
7658032Sroot 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
7667421Sroot 		return (0);
76721016Smckusick 	if (u.u_procp->p_textp && access(u.u_procp->p_textp->x_iptr, IREAD))
76821016Smckusick 		return (0);
7697421Sroot 	u.u_error = 0;
77016692Smckusick 	ndp->ni_nameiop = CREATE | FOLLOW;
77116692Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
77216692Smckusick 	ndp->ni_dirp = "core";
77316692Smckusick 	ip = namei(ndp);
7747421Sroot 	if (ip == NULL) {
7757421Sroot 		if (u.u_error)
7767421Sroot 			return (0);
77716692Smckusick 		ip = maknode(0644, ndp);
7787421Sroot 		if (ip==NULL)
7797421Sroot 			return (0);
7807421Sroot 	}
7817818Sroot 	if (access(ip, IWRITE) ||
7827818Sroot 	   (ip->i_mode&IFMT) != IFREG ||
7837818Sroot 	   ip->i_nlink != 1) {
7847421Sroot 		u.u_error = EFAULT;
7857818Sroot 		goto out;
7867818Sroot 	}
7879160Ssam 	itrunc(ip, (u_long)0);
7887818Sroot 	u.u_acflag |= ACORE;
78912882Ssam 	u.u_error = rdwri(UIO_WRITE, ip,
79012882Ssam 	    (caddr_t)&u,
79112882Ssam 	    ctob(UPAGES),
79226354Skarels 	    (off_t)0, 1, (int *)0);
7938101Sroot 	if (u.u_error == 0)
7948644Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
7958967Sroot 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
79626354Skarels 		    (int)ctob(u.u_dsize),
79726354Skarels 		    (off_t)ctob(UPAGES), 0, (int *)0);
7988101Sroot 	if (u.u_error == 0)
7998644Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
8008967Sroot 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
80126354Skarels 		    (int)ctob(u.u_ssize),
80226354Skarels 		    (off_t)ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0);
8037818Sroot out:
8047421Sroot 	iput(ip);
8057818Sroot 	return (u.u_error == 0);
8067421Sroot }
807