xref: /csrg-svn/sys/kern/kern_sig.c (revision 17597)
1*17597Sbloom /*	kern_sig.c	6.9	84/12/31	*/
27421Sroot 
39755Ssam #include "../machine/reg.h"
49755Ssam #include "../machine/pte.h"
59755Ssam #include "../machine/psl.h"
69755Ssam 
717092Sbloom #include "param.h"
817092Sbloom #include "systm.h"
917092Sbloom #include "dir.h"
1017092Sbloom #include "user.h"
1117092Sbloom #include "inode.h"
1217092Sbloom #include "proc.h"
1317092Sbloom #include "timeb.h"
1417092Sbloom #include "times.h"
1517092Sbloom #include "conf.h"
1617092Sbloom #include "buf.h"
1717092Sbloom #include "mount.h"
1817092Sbloom #include "text.h"
1917092Sbloom #include "seg.h"
2017092Sbloom #include "vm.h"
2117092Sbloom #include "acct.h"
2217092Sbloom #include "uio.h"
2317092Sbloom #include "kernel.h"
247421Sroot 
2517153Sbloom #define	cantmask	(sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP))
2612951Ssam 
2717013Smckusick /*
2817013Smckusick  * Quick interface to signal handler.
2917013Smckusick  */
3017013Smckusick signal()
3117013Smckusick {
3217013Smckusick 	register struct a {
3317013Smckusick 		int	signo;
3417013Smckusick 		int	(*handler)();	/* signal handler */
3517013Smckusick 	} *uap = (struct a  *)u.u_ap;
3617013Smckusick 	struct sigvec vec;
3717013Smckusick 	register struct sigvec *sv = &vec;
3817013Smckusick 	register int sig;
3917013Smckusick 
4017013Smckusick 	sig = uap->signo;
4117013Smckusick 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP ||
4217013Smckusick 	    (sig == SIGCONT && uap->handler == SIG_IGN)) {
4317013Smckusick 		u.u_error = EINVAL;
4417013Smckusick 		return;
4517013Smckusick 	}
4617013Smckusick 	sv->sv_handler = uap->handler;
4717013Smckusick 	sv->sv_mask = 0;
4817013Smckusick 	sv->sv_onstack = 0;
4917013Smckusick 	u.u_r.r_val1 = (int)u.u_signal[sig];
5017013Smckusick 	setsigvec(sig, sv);
5117013Smckusick }
5217013Smckusick 
5317013Smckusick /*
5417013Smckusick  * Generalized interface signal handler.
5517013Smckusick  */
567499Sroot sigvec()
577421Sroot {
5812951Ssam 	register struct a {
5912882Ssam 		int	signo;
6012951Ssam 		struct	sigvec *nsv;
6112951Ssam 		struct	sigvec *osv;
6212882Ssam 	} *uap = (struct a  *)u.u_ap;
6312951Ssam 	struct sigvec vec;
6412951Ssam 	register struct sigvec *sv;
6512882Ssam 	register int sig;
667421Sroot 
6712882Ssam 	sig = uap->signo;
6812951Ssam 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) {
6912882Ssam 		u.u_error = EINVAL;
7012882Ssam 		return;
7112882Ssam 	}
7212951Ssam 	sv = &vec;
7312951Ssam 	if (uap->osv) {
7412951Ssam 		sv->sv_handler = u.u_signal[sig];
7512951Ssam 		sv->sv_mask = u.u_sigmask[sig];
7617153Sbloom 		sv->sv_onstack = (u.u_sigonstack & sigmask(sig)) != 0;
7712951Ssam 		u.u_error =
7812951Ssam 		    copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec));
7912951Ssam 		if (u.u_error)
8012951Ssam 			return;
8112951Ssam 	}
8212951Ssam 	if (uap->nsv) {
8312951Ssam 		u.u_error =
8412951Ssam 		    copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec));
8512951Ssam 		if (u.u_error)
8612951Ssam 			return;
8712951Ssam 		if (sig == SIGCONT && sv->sv_handler == SIG_IGN) {
8812951Ssam 			u.u_error = EINVAL;
8912951Ssam 			return;
9012951Ssam 		}
9112951Ssam 		setsigvec(sig, sv);
9212951Ssam 	}
937421Sroot }
947421Sroot 
9512951Ssam setsigvec(sig, sv)
9612951Ssam 	int sig;
9712951Ssam 	register struct sigvec *sv;
9812882Ssam {
9912882Ssam 	register struct proc *p;
10012951Ssam 	register int bit;
10112882Ssam 
10217153Sbloom 	bit = sigmask(sig);
10312882Ssam 	p = u.u_procp;
10412882Ssam 	/*
10512882Ssam 	 * Change setting atomically.
10612882Ssam 	 */
10717153Sbloom 	(void) splhigh();
10812951Ssam 	u.u_signal[sig] = sv->sv_handler;
10912951Ssam 	u.u_sigmask[sig] = sv->sv_mask &~ cantmask;
11012951Ssam 	if (sv->sv_onstack)
11112951Ssam 		u.u_sigonstack |= bit;
11212951Ssam 	else
11312951Ssam 		u.u_sigonstack &= ~bit;
11412951Ssam 	if (sv->sv_handler == SIG_IGN) {
11512951Ssam 		p->p_sig &= ~bit;		/* never to be seen again */
11612951Ssam 		p->p_sigignore |= bit;
11712951Ssam 		p->p_sigcatch &= ~bit;
11812882Ssam 	} else {
11912951Ssam 		p->p_sigignore &= ~bit;
12012951Ssam 		if (sv->sv_handler == SIG_DFL)
12112951Ssam 			p->p_sigcatch &= ~bit;
12212882Ssam 		else
12312951Ssam 			p->p_sigcatch |= bit;
12412882Ssam 	}
12512882Ssam 	(void) spl0();
12612882Ssam }
12712882Ssam 
1287499Sroot sigblock()
1297421Sroot {
13012882Ssam 	struct a {
13117153Sbloom 		int	mask;
13212882Ssam 	} *uap = (struct a *)u.u_ap;
13312951Ssam 	register struct proc *p = u.u_procp;
1347499Sroot 
13517153Sbloom 	(void) splhigh();
13612882Ssam 	u.u_r.r_val1 = p->p_sigmask;
13717153Sbloom 	p->p_sigmask |= uap->mask &~ cantmask;
13812882Ssam 	(void) spl0();
1397499Sroot }
1407499Sroot 
1417499Sroot sigsetmask()
1427499Sroot {
14312882Ssam 	struct a {
14417153Sbloom 		int	mask;
14512882Ssam 	} *uap = (struct a *)u.u_ap;
14612882Ssam 	register struct proc *p = u.u_procp;
1477499Sroot 
14817153Sbloom 	(void) splhigh();
14912882Ssam 	u.u_r.r_val1 = p->p_sigmask;
15017153Sbloom 	p->p_sigmask = uap->mask &~ cantmask;
15112882Ssam 	(void) spl0();
1527499Sroot }
1537499Sroot 
1547499Sroot sigpause()
1557499Sroot {
15612882Ssam 	struct a {
15717153Sbloom 		int	mask;
15812882Ssam 	} *uap = (struct a *)u.u_ap;
15912882Ssam 	register struct proc *p = u.u_procp;
1607499Sroot 
16112882Ssam 	/*
16212882Ssam 	 * When returning from sigpause, we want
16312882Ssam 	 * the old mask to be restored after the
16412882Ssam 	 * signal handler has finished.  Thus, we
16512882Ssam 	 * save it here and mark the proc structure
16612882Ssam 	 * to indicate this (should be in u.).
16712882Ssam 	 */
16812882Ssam 	u.u_oldmask = p->p_sigmask;
16912882Ssam 	p->p_flag |= SOMASK;
17017153Sbloom 	p->p_sigmask = uap->mask &~ cantmask;
17112882Ssam 	for (;;)
17212882Ssam 		sleep((caddr_t)&u, PSLEP);
17312882Ssam 	/*NOTREACHED*/
1747499Sroot }
17512951Ssam #undef cantmask
1767499Sroot 
1777499Sroot sigstack()
1787499Sroot {
17912951Ssam 	register struct a {
18012951Ssam 		struct	sigstack *nss;
18112951Ssam 		struct	sigstack *oss;
18212882Ssam 	} *uap = (struct a *)u.u_ap;
18312951Ssam 	struct sigstack ss;
1847499Sroot 
18512951Ssam 	if (uap->oss) {
18612951Ssam 		u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss,
18712951Ssam 		    sizeof (struct sigstack));
18812951Ssam 		if (u.u_error)
18912951Ssam 			return;
19012951Ssam 	}
19112951Ssam 	if (uap->nss) {
19212951Ssam 		u.u_error =
19312951Ssam 		    copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss));
19412951Ssam 		if (u.u_error == 0)
19512951Ssam 			u.u_sigstack = ss;
19612951Ssam 	}
1977499Sroot }
1987499Sroot 
19913227Ssam /* KILL SHOULD BE UPDATED */
20013227Ssam 
2018032Sroot kill()
2028032Sroot {
20312882Ssam 	register struct a {
20412882Ssam 		int	pid;
20512882Ssam 		int	signo;
20612882Ssam 	} *uap = (struct a *)u.u_ap;
2078032Sroot 
20813227Ssam 	u.u_error = kill1(uap->signo < 0,
20913227Ssam 		uap->signo < 0 ? -uap->signo : uap->signo, uap->pid);
2108032Sroot }
2118032Sroot 
2128032Sroot killpg()
2138032Sroot {
2149989Ssam 	register struct a {
2159989Ssam 		int	pgrp;
2169989Ssam 		int	signo;
2179989Ssam 	} *uap = (struct a *)u.u_ap;
2188032Sroot 
21912750Ssam 	u.u_error = kill1(1, uap->signo, uap->pgrp);
2208032Sroot }
2218032Sroot 
22212882Ssam /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
22312882Ssam 
22412750Ssam kill1(ispgrp, signo, who)
2259989Ssam 	int ispgrp, signo, who;
2269989Ssam {
2279989Ssam 	register struct proc *p;
2289989Ssam 	int f, priv = 0;
2299989Ssam 
23012835Ssam 	if (signo < 0 || signo > NSIG)
2319989Ssam 		return (EINVAL);
2329989Ssam 	if (who > 0 && !ispgrp) {
2339989Ssam 		p = pfind(who);
23414926Smckusick 		if (p == 0)
2359989Ssam 			return (ESRCH);
23614926Smckusick 		if (u.u_uid && u.u_uid != p->p_uid)
23714926Smckusick 			return (EPERM);
23812835Ssam 		if (signo)
23912835Ssam 			psignal(p, signo);
2409989Ssam 		return (0);
2417421Sroot 	}
2429989Ssam 	if (who == -1 && u.u_uid == 0)
2439989Ssam 		priv++, who = 0, ispgrp = 1;	/* like sending to pgrp */
2449989Ssam 	else if (who == 0) {
2457421Sroot 		/*
2467421Sroot 		 * Zero process id means send to my process group.
2477421Sroot 		 */
2489989Ssam 		ispgrp = 1;
2499989Ssam 		who = u.u_procp->p_pgrp;
2509989Ssam 		if (who == 0)
2519989Ssam 			return (EINVAL);
2527421Sroot 	}
25316531Skarels 	for (f = 0, p = allproc; p != NULL; p = p->p_nxt) {
2549989Ssam 		if (!ispgrp) {
2559989Ssam 			if (p->p_pid != who)
2567421Sroot 				continue;
2579989Ssam 		} else if (p->p_pgrp != who && priv == 0 || p->p_ppid == 0 ||
2589989Ssam 		    (p->p_flag&SSYS) || (priv && p == u.u_procp))
2597421Sroot 			continue;
2607421Sroot 		if (u.u_uid != 0 && u.u_uid != p->p_uid &&
2619989Ssam 		    (signo != SIGCONT || !inferior(p)))
2627421Sroot 			continue;
2637421Sroot 		f++;
26412835Ssam 		if (signo)
26512835Ssam 			psignal(p, signo);
2667421Sroot 	}
26712750Ssam 	return (f == 0 ? ESRCH : 0);
2687421Sroot }
2697421Sroot 
2707421Sroot /*
2717421Sroot  * Send the specified signal to
2727421Sroot  * all processes with 'pgrp' as
2737421Sroot  * process group.
2747421Sroot  */
2757421Sroot gsignal(pgrp, sig)
2767421Sroot 	register int pgrp;
2777421Sroot {
2787421Sroot 	register struct proc *p;
2797421Sroot 
2807421Sroot 	if (pgrp == 0)
2817421Sroot 		return;
28216531Skarels 	for (p = allproc; p != NULL; p = p->p_nxt)
2837421Sroot 		if (p->p_pgrp == pgrp)
2847421Sroot 			psignal(p, sig);
2857421Sroot }
2867421Sroot 
2877421Sroot /*
2887421Sroot  * Send the specified signal to
2897421Sroot  * the specified process.
2907421Sroot  */
2917421Sroot psignal(p, sig)
2927421Sroot 	register struct proc *p;
2937421Sroot 	register int sig;
2947421Sroot {
2957421Sroot 	register int s;
2967421Sroot 	register int (*action)();
29717153Sbloom 	int mask;
2987421Sroot 
2997421Sroot 	if ((unsigned)sig >= NSIG)
3007421Sroot 		return;
30117153Sbloom 	mask = sigmask(sig);
3027421Sroot 
3037421Sroot 	/*
3047421Sroot 	 * If proc is traced, always give parent a chance.
3057421Sroot 	 */
3067421Sroot 	if (p->p_flag & STRC)
3077421Sroot 		action = SIG_DFL;
3087421Sroot 	else {
3097421Sroot 		/*
31012882Ssam 		 * If the signal is being ignored,
31112882Ssam 		 * then we forget about it immediately.
3127421Sroot 		 */
31317153Sbloom 		if (p->p_sigignore & mask)
3147421Sroot 			return;
31517153Sbloom 		if (p->p_sigmask & mask)
31612882Ssam 			action = SIG_HOLD;
31717153Sbloom 		else if (p->p_sigcatch & mask)
31812882Ssam 			action = SIG_CATCH;
31912882Ssam 		else
32012882Ssam 			action = SIG_DFL;
3217421Sroot 	}
32217153Sbloom #define	stops	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
32317153Sbloom 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
3247421Sroot 	if (sig) {
32517153Sbloom 		p->p_sig |= mask;
3267421Sroot 		switch (sig) {
3277421Sroot 
3287421Sroot 		case SIGTERM:
32912882Ssam 			if ((p->p_flag&STRC) || action != SIG_DFL)
3307421Sroot 				break;
3317421Sroot 			/* fall into ... */
3327421Sroot 
3337421Sroot 		case SIGKILL:
3347421Sroot 			if (p->p_nice > NZERO)
3357421Sroot 				p->p_nice = NZERO;
3367421Sroot 			break;
3377421Sroot 
3387421Sroot 		case SIGCONT:
3397421Sroot 			p->p_sig &= ~stops;
3407421Sroot 			break;
3417421Sroot 
3427421Sroot 		case SIGSTOP:
3437421Sroot 		case SIGTSTP:
3447421Sroot 		case SIGTTIN:
3457421Sroot 		case SIGTTOU:
34617153Sbloom 			p->p_sig &= ~sigmask(SIGCONT);
3477421Sroot 			break;
3487421Sroot 		}
3497421Sroot 	}
3507421Sroot #undef stops
3517421Sroot 	/*
3527421Sroot 	 * Defer further processing for signals which are held.
3537421Sroot 	 */
3547421Sroot 	if (action == SIG_HOLD)
3557421Sroot 		return;
35617153Sbloom 	s = splhigh();
3577421Sroot 	switch (p->p_stat) {
3587421Sroot 
3597421Sroot 	case SSLEEP:
3607421Sroot 		/*
3617421Sroot 		 * If process is sleeping at negative priority
3627421Sroot 		 * we can't interrupt the sleep... the signal will
3637421Sroot 		 * be noticed when the process returns through
3647421Sroot 		 * trap() or syscall().
3657421Sroot 		 */
3667421Sroot 		if (p->p_pri <= PZERO)
3677421Sroot 			goto out;
3687421Sroot 		/*
3697421Sroot 		 * Process is sleeping and traced... make it runnable
3707421Sroot 		 * so it can discover the signal in issig() and stop
3717421Sroot 		 * for the parent.
3727421Sroot 		 */
3737421Sroot 		if (p->p_flag&STRC)
3747421Sroot 			goto run;
3757421Sroot 		switch (sig) {
3767421Sroot 
3777421Sroot 		case SIGSTOP:
3787421Sroot 		case SIGTSTP:
3797421Sroot 		case SIGTTIN:
3807421Sroot 		case SIGTTOU:
3817421Sroot 			/*
3827421Sroot 			 * These are the signals which by default
3837421Sroot 			 * stop a process.
3847421Sroot 			 */
3857421Sroot 			if (action != SIG_DFL)
3867421Sroot 				goto run;
3877421Sroot 			/*
3887421Sroot 			 * Don't clog system with children of init
3897421Sroot 			 * stopped from the keyboard.
3907421Sroot 			 */
3917421Sroot 			if (sig != SIGSTOP && p->p_pptr == &proc[1]) {
3927421Sroot 				psignal(p, SIGKILL);
39317153Sbloom 				p->p_sig &= ~mask;
3947421Sroot 				splx(s);
3957421Sroot 				return;
3967421Sroot 			}
3977421Sroot 			/*
3987421Sroot 			 * If a child in vfork(), stopping could
3997421Sroot 			 * cause deadlock.
4007421Sroot 			 */
4017421Sroot 			if (p->p_flag&SVFORK)
4027421Sroot 				goto out;
40317153Sbloom 			p->p_sig &= ~mask;
4047421Sroot 			p->p_cursig = sig;
4057421Sroot 			stop(p);
4067421Sroot 			goto out;
4077421Sroot 
4087421Sroot 		case SIGIO:
4097421Sroot 		case SIGURG:
4107421Sroot 		case SIGCHLD:
411*17597Sbloom 		case SIGWINCH:
4127421Sroot 			/*
4137421Sroot 			 * These signals are special in that they
4147421Sroot 			 * don't get propogated... if the process
4157421Sroot 			 * isn't interested, forget it.
4167421Sroot 			 */
4177421Sroot 			if (action != SIG_DFL)
4187421Sroot 				goto run;
41917153Sbloom 			p->p_sig &= ~mask;		/* take it away */
4207421Sroot 			goto out;
4217421Sroot 
4227421Sroot 		default:
4237421Sroot 			/*
4247421Sroot 			 * All other signals cause the process to run
4257421Sroot 			 */
4267421Sroot 			goto run;
4277421Sroot 		}
4287421Sroot 		/*NOTREACHED*/
4297421Sroot 
4307421Sroot 	case SSTOP:
4317421Sroot 		/*
4327421Sroot 		 * If traced process is already stopped,
4337421Sroot 		 * then no further action is necessary.
4347421Sroot 		 */
4357421Sroot 		if (p->p_flag&STRC)
4367421Sroot 			goto out;
4377421Sroot 		switch (sig) {
4387421Sroot 
4397421Sroot 		case SIGKILL:
4407421Sroot 			/*
4417421Sroot 			 * Kill signal always sets processes running.
4427421Sroot 			 */
4437421Sroot 			goto run;
4447421Sroot 
4457421Sroot 		case SIGCONT:
4467421Sroot 			/*
4477421Sroot 			 * If the process catches SIGCONT, let it handle
4487421Sroot 			 * the signal itself.  If it isn't waiting on
4497421Sroot 			 * an event, then it goes back to run state.
4507421Sroot 			 * Otherwise, process goes back to sleep state.
4517421Sroot 			 */
4527421Sroot 			if (action != SIG_DFL || p->p_wchan == 0)
4537421Sroot 				goto run;
4547421Sroot 			p->p_stat = SSLEEP;
4557421Sroot 			goto out;
4567421Sroot 
4577421Sroot 		case SIGSTOP:
4587421Sroot 		case SIGTSTP:
4597421Sroot 		case SIGTTIN:
4607421Sroot 		case SIGTTOU:
4617421Sroot 			/*
4627421Sroot 			 * Already stopped, don't need to stop again.
4637421Sroot 			 * (If we did the shell could get confused.)
4647421Sroot 			 */
46517153Sbloom 			p->p_sig &= ~mask;		/* take it away */
4667421Sroot 			goto out;
4677421Sroot 
4687421Sroot 		default:
4697421Sroot 			/*
4707421Sroot 			 * If process is sleeping interruptibly, then
4717421Sroot 			 * unstick it so that when it is continued
4727421Sroot 			 * it can look at the signal.
4737421Sroot 			 * But don't setrun the process as its not to
4747421Sroot 			 * be unstopped by the signal alone.
4757421Sroot 			 */
4767421Sroot 			if (p->p_wchan && p->p_pri > PZERO)
4777421Sroot 				unsleep(p);
4787421Sroot 			goto out;
4797421Sroot 		}
4807421Sroot 		/*NOTREACHED*/
4817421Sroot 
4827421Sroot 	default:
4837421Sroot 		/*
4847421Sroot 		 * SRUN, SIDL, SZOMB do nothing with the signal,
4857421Sroot 		 * other than kicking ourselves if we are running.
4867421Sroot 		 * It will either never be noticed, or noticed very soon.
4877421Sroot 		 */
4887421Sroot 		if (p == u.u_procp && !noproc)
4898444Sroot #include "../vax/mtpr.h"
4907421Sroot 			aston();
4917421Sroot 		goto out;
4927421Sroot 	}
4937421Sroot 	/*NOTREACHED*/
4947421Sroot run:
4957421Sroot 	/*
4967421Sroot 	 * Raise priority to at least PUSER.
4977421Sroot 	 */
4987421Sroot 	if (p->p_pri > PUSER)
49917399Skarels 		p->p_pri = PUSER;
5007421Sroot 	setrun(p);
5017421Sroot out:
5027421Sroot 	splx(s);
5037421Sroot }
5047421Sroot 
5057421Sroot /*
5067421Sroot  * Returns true if the current
5077421Sroot  * process has a signal to process.
5087421Sroot  * The signal to process is put in p_cursig.
5097421Sroot  * This is asked at least once each time a process enters the
5107421Sroot  * system (though this can usually be done without actually
5117421Sroot  * calling issig by checking the pending signal masks.)
5127421Sroot  * A signal does not do anything
5137421Sroot  * directly to a process; it sets
5147421Sroot  * a flag that asks the process to
5157421Sroot  * do something to itself.
5167421Sroot  */
5177421Sroot issig()
5187421Sroot {
5197421Sroot 	register struct proc *p;
5207421Sroot 	register int sig;
52117153Sbloom 	int sigbits, mask;
5227421Sroot 
5237421Sroot 	p = u.u_procp;
5247421Sroot 	for (;;) {
52514782Ssam 		sigbits = p->p_sig &~ p->p_sigmask;
5267421Sroot 		if ((p->p_flag&STRC) == 0)
52714782Ssam 			sigbits &= ~p->p_sigignore;
5287421Sroot 		if (p->p_flag&SVFORK)
5297421Sroot #define bit(a) (1<<(a-1))
5307421Sroot 			sigbits &= ~(bit(SIGSTOP)|bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU));
5317421Sroot 		if (sigbits == 0)
5327421Sroot 			break;
53312882Ssam 		sig = ffs(sigbits);
53417153Sbloom 		mask = sigmask(sig);
53517153Sbloom 		p->p_sig &= ~mask;		/* take the signal! */
5367421Sroot 		p->p_cursig = sig;
53712882Ssam 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
5387421Sroot 			/*
5397421Sroot 			 * If traced, always stop, and stay
5407421Sroot 			 * stopped until released by the parent.
5417421Sroot 			 */
5427421Sroot 			do {
5437421Sroot 				stop(p);
5447421Sroot 				swtch();
5457421Sroot 			} while (!procxmt() && p->p_flag&STRC);
5467421Sroot 
5477421Sroot 			/*
54814782Ssam 			 * If the traced bit got turned off,
54914782Ssam 			 * then put the signal taken above back into p_sig
55014782Ssam 			 * and go back up to the top to rescan signals.
55114782Ssam 			 * This ensures that p_sig* and u_signal are consistent.
5527421Sroot 			 */
55314782Ssam 			if ((p->p_flag&STRC) == 0) {
55417153Sbloom 				p->p_sig |= mask;
5557421Sroot 				continue;
5567421Sroot 			}
5577421Sroot 
5587421Sroot 			/*
5597421Sroot 			 * If parent wants us to take the signal,
5607421Sroot 			 * then it will leave it in p->p_cursig;
5617421Sroot 			 * otherwise we just look for signals again.
5627421Sroot 			 */
5637421Sroot 			sig = p->p_cursig;
5647421Sroot 			if (sig == 0)
5657421Sroot 				continue;
56614782Ssam 
56714782Ssam 			/*
56814782Ssam 			 * If signal is being masked put it back
56914782Ssam 			 * into p_sig and look for other signals.
57014782Ssam 			 */
57117153Sbloom 			mask = sigmask(sig);
57217153Sbloom 			if (p->p_sigmask & mask) {
57317153Sbloom 				p->p_sig |= mask;
57414782Ssam 				continue;
57514782Ssam 			}
5767421Sroot 		}
5777421Sroot 		switch (u.u_signal[sig]) {
5787421Sroot 
5797421Sroot 		case SIG_DFL:
5807421Sroot 			/*
5817421Sroot 			 * Don't take default actions on system processes.
5827421Sroot 			 */
5837421Sroot 			if (p->p_ppid == 0)
5847421Sroot 				break;
5857421Sroot 			switch (sig) {
5867421Sroot 
5877421Sroot 			case SIGTSTP:
5887421Sroot 			case SIGTTIN:
5897421Sroot 			case SIGTTOU:
5907421Sroot 				/*
5917421Sroot 				 * Children of init aren't allowed to stop
5927421Sroot 				 * on signals from the keyboard.
5937421Sroot 				 */
5947421Sroot 				if (p->p_pptr == &proc[1]) {
5957421Sroot 					psignal(p, SIGKILL);
5967421Sroot 					continue;
5977421Sroot 				}
5987421Sroot 				/* fall into ... */
5997421Sroot 
6007421Sroot 			case SIGSTOP:
6017421Sroot 				if (p->p_flag&STRC)
6027421Sroot 					continue;
6037421Sroot 				stop(p);
6047421Sroot 				swtch();
6057421Sroot 				continue;
6067421Sroot 
6077421Sroot 			case SIGCONT:
6087421Sroot 			case SIGCHLD:
60912882Ssam 			case SIGURG:
61012951Ssam 			case SIGIO:
611*17597Sbloom 			case SIGWINCH:
6127421Sroot 				/*
6137421Sroot 				 * These signals are normally not
6147421Sroot 				 * sent if the action is the default.
6157421Sroot 				 */
6167421Sroot 				continue;		/* == ignore */
6177421Sroot 
6187421Sroot 			default:
6197421Sroot 				goto send;
6207421Sroot 			}
6217421Sroot 			/*NOTREACHED*/
6227421Sroot 
6237421Sroot 		case SIG_HOLD:
6247421Sroot 		case SIG_IGN:
6257421Sroot 			/*
6267421Sroot 			 * Masking above should prevent us
6277421Sroot 			 * ever trying to take action on a held
6287421Sroot 			 * or ignored signal, unless process is traced.
6297421Sroot 			 */
6307421Sroot 			if ((p->p_flag&STRC) == 0)
6317421Sroot 				printf("issig\n");
6327421Sroot 			continue;
6337421Sroot 
6347421Sroot 		default:
6357421Sroot 			/*
6367421Sroot 			 * This signal has an action, let
6377421Sroot 			 * psig process it.
6387421Sroot 			 */
6397421Sroot 			goto send;
6407421Sroot 		}
6417421Sroot 		/*NOTREACHED*/
6427421Sroot 	}
6437421Sroot 	/*
6447421Sroot 	 * Didn't find a signal to send.
6457421Sroot 	 */
6467421Sroot 	p->p_cursig = 0;
6477421Sroot 	return (0);
6487421Sroot 
6497421Sroot send:
6507421Sroot 	/*
6517421Sroot 	 * Let psig process the signal.
6527421Sroot 	 */
6537421Sroot 	return (sig);
6547421Sroot }
6557421Sroot 
6567421Sroot /*
6577421Sroot  * Put the argument process into the stopped
6587421Sroot  * state and notify the parent via wakeup and/or signal.
6597421Sroot  */
6607421Sroot stop(p)
6617421Sroot 	register struct proc *p;
6627421Sroot {
6637421Sroot 
6647421Sroot 	p->p_stat = SSTOP;
6657421Sroot 	p->p_flag &= ~SWTED;
6667421Sroot 	wakeup((caddr_t)p->p_pptr);
6677421Sroot 	/*
6687421Sroot 	 * Avoid sending signal to parent if process is traced
6697421Sroot 	 */
6707421Sroot 	if (p->p_flag&STRC)
6717421Sroot 		return;
6727421Sroot 	psignal(p->p_pptr, SIGCHLD);
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);
7677421Sroot 	u.u_error = 0;
76816692Smckusick 	ndp->ni_nameiop = CREATE | FOLLOW;
76916692Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
77016692Smckusick 	ndp->ni_dirp = "core";
77116692Smckusick 	ip = namei(ndp);
7727421Sroot 	if (ip == NULL) {
7737421Sroot 		if (u.u_error)
7747421Sroot 			return (0);
77516692Smckusick 		ip = maknode(0644, ndp);
7767421Sroot 		if (ip==NULL)
7777421Sroot 			return (0);
7787421Sroot 	}
7797818Sroot 	if (access(ip, IWRITE) ||
7807818Sroot 	   (ip->i_mode&IFMT) != IFREG ||
7817818Sroot 	   ip->i_nlink != 1) {
7827421Sroot 		u.u_error = EFAULT;
7837818Sroot 		goto out;
7847818Sroot 	}
7859160Ssam 	itrunc(ip, (u_long)0);
7867818Sroot 	u.u_acflag |= ACORE;
78712882Ssam 	u.u_error = rdwri(UIO_WRITE, ip,
78812882Ssam 	    (caddr_t)&u,
78912882Ssam 	    ctob(UPAGES),
79012882Ssam 	    0, 1, (int *)0);
7918101Sroot 	if (u.u_error == 0)
7928644Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
7938967Sroot 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
7948967Sroot 		    ctob(u.u_dsize),
7958644Sroot 		    ctob(UPAGES), 0, (int *)0);
7968101Sroot 	if (u.u_error == 0)
7978644Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
7988967Sroot 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
7998967Sroot 		    ctob(u.u_ssize),
8008644Sroot 		    ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0);
8017818Sroot out:
8027421Sroot 	iput(ip);
8037818Sroot 	return (u.u_error == 0);
8047421Sroot }
805