xref: /csrg-svn/sys/kern/kern_sig.c (revision 14782)
1*14782Ssam /*	kern_sig.c	5.24	83/08/20	*/
27421Sroot 
39755Ssam #include "../machine/reg.h"
49755Ssam #include "../machine/pte.h"
59755Ssam #include "../machine/psl.h"
69755Ssam 
77421Sroot #include "../h/param.h"
87421Sroot #include "../h/systm.h"
97421Sroot #include "../h/dir.h"
107421Sroot #include "../h/user.h"
117421Sroot #include "../h/inode.h"
127421Sroot #include "../h/proc.h"
137421Sroot #include "../h/timeb.h"
147421Sroot #include "../h/times.h"
157421Sroot #include "../h/conf.h"
167421Sroot #include "../h/buf.h"
177421Sroot #include "../h/mount.h"
187421Sroot #include "../h/text.h"
197421Sroot #include "../h/seg.h"
207421Sroot #include "../h/vm.h"
217421Sroot #include "../h/acct.h"
227818Sroot #include "../h/uio.h"
238117Sroot #include "../h/kernel.h"
249160Ssam #include "../h/nami.h"
257421Sroot 
2612951Ssam #define	mask(s)	(1 << ((s)-1))
2712951Ssam #define	cantmask	(mask(SIGKILL)|mask(SIGCONT)|mask(SIGSTOP))
2812951Ssam 
297499Sroot sigvec()
307421Sroot {
3112951Ssam 	register struct a {
3212882Ssam 		int	signo;
3312951Ssam 		struct	sigvec *nsv;
3412951Ssam 		struct	sigvec *osv;
3512882Ssam 	} *uap = (struct a  *)u.u_ap;
3612951Ssam 	struct sigvec vec;
3712951Ssam 	register struct sigvec *sv;
3812882Ssam 	register int sig;
397421Sroot 
4012882Ssam 	sig = uap->signo;
4112951Ssam 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) {
4212882Ssam 		u.u_error = EINVAL;
4312882Ssam 		return;
4412882Ssam 	}
4512951Ssam 	sv = &vec;
4612951Ssam 	if (uap->osv) {
4712951Ssam 		sv->sv_handler = u.u_signal[sig];
4812951Ssam 		sv->sv_mask = u.u_sigmask[sig];
4912951Ssam 		sv->sv_onstack = (u.u_sigonstack & mask(sig)) != 0;
5012951Ssam 		u.u_error =
5112951Ssam 		    copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec));
5212951Ssam 		if (u.u_error)
5312951Ssam 			return;
5412951Ssam 	}
5512951Ssam 	if (uap->nsv) {
5612951Ssam 		u.u_error =
5712951Ssam 		    copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec));
5812951Ssam 		if (u.u_error)
5912951Ssam 			return;
6012951Ssam 		if (sig == SIGCONT && sv->sv_handler == SIG_IGN) {
6112951Ssam 			u.u_error = EINVAL;
6212951Ssam 			return;
6312951Ssam 		}
6412951Ssam 		setsigvec(sig, sv);
6512951Ssam 	}
667421Sroot }
677421Sroot 
6812951Ssam setsigvec(sig, sv)
6912951Ssam 	int sig;
7012951Ssam 	register struct sigvec *sv;
7112882Ssam {
7212882Ssam 	register struct proc *p;
7312951Ssam 	register int bit;
7412882Ssam 
7512951Ssam 	bit = mask(sig);
7612882Ssam 	p = u.u_procp;
7712882Ssam 	/*
7812882Ssam 	 * Change setting atomically.
7912882Ssam 	 */
8012882Ssam 	(void) spl6();
8112951Ssam 	u.u_signal[sig] = sv->sv_handler;
8212951Ssam 	u.u_sigmask[sig] = sv->sv_mask &~ cantmask;
8312951Ssam 	if (sv->sv_onstack)
8412951Ssam 		u.u_sigonstack |= bit;
8512951Ssam 	else
8612951Ssam 		u.u_sigonstack &= ~bit;
8712951Ssam 	if (sv->sv_handler == SIG_IGN) {
8812951Ssam 		p->p_sig &= ~bit;		/* never to be seen again */
8912951Ssam 		p->p_sigignore |= bit;
9012951Ssam 		p->p_sigcatch &= ~bit;
9112882Ssam 	} else {
9212951Ssam 		p->p_sigignore &= ~bit;
9312951Ssam 		if (sv->sv_handler == SIG_DFL)
9412951Ssam 			p->p_sigcatch &= ~bit;
9512882Ssam 		else
9612951Ssam 			p->p_sigcatch |= bit;
9712882Ssam 	}
9812882Ssam 	(void) spl0();
9912882Ssam }
10012882Ssam 
1017499Sroot sigblock()
1027421Sroot {
10312882Ssam 	struct a {
10412951Ssam 		int	sigmask;
10512882Ssam 	} *uap = (struct a *)u.u_ap;
10612951Ssam 	register struct proc *p = u.u_procp;
1077499Sroot 
10812882Ssam 	(void) spl6();
10912882Ssam 	u.u_r.r_val1 = p->p_sigmask;
11012951Ssam 	p->p_sigmask |= uap->sigmask &~ cantmask;
11112882Ssam 	(void) spl0();
1127499Sroot }
1137499Sroot 
1147499Sroot sigsetmask()
1157499Sroot {
11612882Ssam 	struct a {
11712951Ssam 		int	sigmask;
11812882Ssam 	} *uap = (struct a *)u.u_ap;
11912882Ssam 	register struct proc *p = u.u_procp;
1207499Sroot 
12112882Ssam 	(void) spl6();
12212882Ssam 	u.u_r.r_val1 = p->p_sigmask;
12312951Ssam 	p->p_sigmask = uap->sigmask &~ cantmask;
12412882Ssam 	(void) spl0();
1257499Sroot }
1267499Sroot 
1277499Sroot sigpause()
1287499Sroot {
12912882Ssam 	struct a {
13012951Ssam 		int	sigmask;
13112882Ssam 	} *uap = (struct a *)u.u_ap;
13212882Ssam 	register struct proc *p = u.u_procp;
1337499Sroot 
13412882Ssam 	/*
13512882Ssam 	 * When returning from sigpause, we want
13612882Ssam 	 * the old mask to be restored after the
13712882Ssam 	 * signal handler has finished.  Thus, we
13812882Ssam 	 * save it here and mark the proc structure
13912882Ssam 	 * to indicate this (should be in u.).
14012882Ssam 	 */
14112882Ssam 	u.u_oldmask = p->p_sigmask;
14212882Ssam 	p->p_flag |= SOMASK;
14312951Ssam 	p->p_sigmask = uap->sigmask &~ cantmask;
14412882Ssam 	for (;;)
14512882Ssam 		sleep((caddr_t)&u, PSLEP);
14612882Ssam 	/*NOTREACHED*/
1477499Sroot }
14812951Ssam #undef cantmask
14912951Ssam #undef mask
1507499Sroot 
1517499Sroot sigstack()
1527499Sroot {
15312951Ssam 	register struct a {
15412951Ssam 		struct	sigstack *nss;
15512951Ssam 		struct	sigstack *oss;
15612882Ssam 	} *uap = (struct a *)u.u_ap;
15712951Ssam 	struct sigstack ss;
1587499Sroot 
15912951Ssam 	if (uap->oss) {
16012951Ssam 		u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss,
16112951Ssam 		    sizeof (struct sigstack));
16212951Ssam 		if (u.u_error)
16312951Ssam 			return;
16412951Ssam 	}
16512951Ssam 	if (uap->nss) {
16612951Ssam 		u.u_error =
16712951Ssam 		    copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss));
16812951Ssam 		if (u.u_error == 0)
16912951Ssam 			u.u_sigstack = ss;
17012951Ssam 	}
1717499Sroot }
1727499Sroot 
17313227Ssam /* KILL SHOULD BE UPDATED */
17413227Ssam 
1758032Sroot kill()
1768032Sroot {
17712882Ssam 	register struct a {
17812882Ssam 		int	pid;
17912882Ssam 		int	signo;
18012882Ssam 	} *uap = (struct a *)u.u_ap;
1818032Sroot 
18213227Ssam 	u.u_error = kill1(uap->signo < 0,
18313227Ssam 		uap->signo < 0 ? -uap->signo : uap->signo, uap->pid);
1848032Sroot }
1858032Sroot 
1868032Sroot killpg()
1878032Sroot {
1889989Ssam 	register struct a {
1899989Ssam 		int	pgrp;
1909989Ssam 		int	signo;
1919989Ssam 	} *uap = (struct a *)u.u_ap;
1928032Sroot 
19312750Ssam 	u.u_error = kill1(1, uap->signo, uap->pgrp);
1948032Sroot }
1958032Sroot 
19612882Ssam /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
19712882Ssam 
19812750Ssam kill1(ispgrp, signo, who)
1999989Ssam 	int ispgrp, signo, who;
2009989Ssam {
2019989Ssam 	register struct proc *p;
2029989Ssam 	int f, priv = 0;
2039989Ssam 
20412835Ssam 	if (signo < 0 || signo > NSIG)
2059989Ssam 		return (EINVAL);
2069989Ssam 	if (who > 0 && !ispgrp) {
2079989Ssam 		p = pfind(who);
2089989Ssam 		if (p == 0 || u.u_uid && u.u_uid != p->p_uid)
2099989Ssam 			return (ESRCH);
21012835Ssam 		if (signo)
21112835Ssam 			psignal(p, signo);
2129989Ssam 		return (0);
2137421Sroot 	}
2149989Ssam 	if (who == -1 && u.u_uid == 0)
2159989Ssam 		priv++, who = 0, ispgrp = 1;	/* like sending to pgrp */
2169989Ssam 	else if (who == 0) {
2177421Sroot 		/*
2187421Sroot 		 * Zero process id means send to my process group.
2197421Sroot 		 */
2209989Ssam 		ispgrp = 1;
2219989Ssam 		who = u.u_procp->p_pgrp;
2229989Ssam 		if (who == 0)
2239989Ssam 			return (EINVAL);
2247421Sroot 	}
2259989Ssam 	for (f = 0, p = proc; p < procNPROC; p++) {
2267421Sroot 		if (p->p_stat == NULL)
2277421Sroot 			continue;
2289989Ssam 		if (!ispgrp) {
2299989Ssam 			if (p->p_pid != who)
2307421Sroot 				continue;
2319989Ssam 		} else if (p->p_pgrp != who && priv == 0 || p->p_ppid == 0 ||
2329989Ssam 		    (p->p_flag&SSYS) || (priv && p == u.u_procp))
2337421Sroot 			continue;
2347421Sroot 		if (u.u_uid != 0 && u.u_uid != p->p_uid &&
2359989Ssam 		    (signo != SIGCONT || !inferior(p)))
2367421Sroot 			continue;
2377421Sroot 		f++;
23812835Ssam 		if (signo)
23912835Ssam 			psignal(p, signo);
2407421Sroot 	}
24112750Ssam 	return (f == 0 ? ESRCH : 0);
2427421Sroot }
2437421Sroot 
2447421Sroot /*
2457421Sroot  * Send the specified signal to
2467421Sroot  * all processes with 'pgrp' as
2477421Sroot  * process group.
2487421Sroot  */
2497421Sroot gsignal(pgrp, sig)
2507421Sroot 	register int pgrp;
2517421Sroot {
2527421Sroot 	register struct proc *p;
2537421Sroot 
2547421Sroot 	if (pgrp == 0)
2557421Sroot 		return;
2567421Sroot 	for(p = proc; p < procNPROC; p++)
2577421Sroot 		if (p->p_pgrp == pgrp)
2587421Sroot 			psignal(p, sig);
2597421Sroot }
2607421Sroot 
2617421Sroot /*
2627421Sroot  * Send the specified signal to
2637421Sroot  * the specified process.
2647421Sroot  */
2657421Sroot psignal(p, sig)
2667421Sroot 	register struct proc *p;
2677421Sroot 	register int sig;
2687421Sroot {
2697421Sroot 	register int s;
2707421Sroot 	register int (*action)();
27112882Ssam 	int sigmask;
2727421Sroot 
2737421Sroot 	if ((unsigned)sig >= NSIG)
2747421Sroot 		return;
27512882Ssam 	sigmask = 1 << (sig-1);
2767421Sroot 
2777421Sroot 	/*
2787421Sroot 	 * If proc is traced, always give parent a chance.
2797421Sroot 	 */
2807421Sroot 	if (p->p_flag & STRC)
2817421Sroot 		action = SIG_DFL;
2827421Sroot 	else {
2837421Sroot 		/*
28412882Ssam 		 * If the signal is being ignored,
28512882Ssam 		 * then we forget about it immediately.
2867421Sroot 		 */
28712882Ssam 		if (p->p_sigignore & sigmask)
2887421Sroot 			return;
28912882Ssam 		if (p->p_sigmask & sigmask)
29012882Ssam 			action = SIG_HOLD;
29112882Ssam 		else if (p->p_sigcatch & sigmask)
29212882Ssam 			action = SIG_CATCH;
29312882Ssam 		else
29412882Ssam 			action = SIG_DFL;
2957421Sroot 	}
2967421Sroot #define mask(sig)	(1<<(sig-1))
2977421Sroot #define	stops	(mask(SIGSTOP)|mask(SIGTSTP)|mask(SIGTTIN)|mask(SIGTTOU))
2987421Sroot 	if (sig) {
2997421Sroot 		p->p_sig |= sigmask;
3007421Sroot 		switch (sig) {
3017421Sroot 
3027421Sroot 		case SIGTERM:
30312882Ssam 			if ((p->p_flag&STRC) || action != SIG_DFL)
3047421Sroot 				break;
3057421Sroot 			/* fall into ... */
3067421Sroot 
3077421Sroot 		case SIGKILL:
3087421Sroot 			if (p->p_nice > NZERO)
3097421Sroot 				p->p_nice = NZERO;
3107421Sroot 			break;
3117421Sroot 
3127421Sroot 		case SIGCONT:
3137421Sroot 			p->p_sig &= ~stops;
3147421Sroot 			break;
3157421Sroot 
3167421Sroot 		case SIGSTOP:
3177421Sroot 		case SIGTSTP:
3187421Sroot 		case SIGTTIN:
3197421Sroot 		case SIGTTOU:
3207421Sroot 			p->p_sig &= ~mask(SIGCONT);
3217421Sroot 			break;
3227421Sroot 		}
3237421Sroot 	}
3247421Sroot #undef mask
3257421Sroot #undef stops
3267421Sroot 	/*
3277421Sroot 	 * Defer further processing for signals which are held.
3287421Sroot 	 */
3297421Sroot 	if (action == SIG_HOLD)
3307421Sroot 		return;
3317421Sroot 	s = spl6();
3327421Sroot 	switch (p->p_stat) {
3337421Sroot 
3347421Sroot 	case SSLEEP:
3357421Sroot 		/*
3367421Sroot 		 * If process is sleeping at negative priority
3377421Sroot 		 * we can't interrupt the sleep... the signal will
3387421Sroot 		 * be noticed when the process returns through
3397421Sroot 		 * trap() or syscall().
3407421Sroot 		 */
3417421Sroot 		if (p->p_pri <= PZERO)
3427421Sroot 			goto out;
3437421Sroot 		/*
3447421Sroot 		 * Process is sleeping and traced... make it runnable
3457421Sroot 		 * so it can discover the signal in issig() and stop
3467421Sroot 		 * for the parent.
3477421Sroot 		 */
3487421Sroot 		if (p->p_flag&STRC)
3497421Sroot 			goto run;
3507421Sroot 		switch (sig) {
3517421Sroot 
3527421Sroot 		case SIGSTOP:
3537421Sroot 		case SIGTSTP:
3547421Sroot 		case SIGTTIN:
3557421Sroot 		case SIGTTOU:
3567421Sroot 			/*
3577421Sroot 			 * These are the signals which by default
3587421Sroot 			 * stop a process.
3597421Sroot 			 */
3607421Sroot 			if (action != SIG_DFL)
3617421Sroot 				goto run;
3627421Sroot 			/*
3637421Sroot 			 * Don't clog system with children of init
3647421Sroot 			 * stopped from the keyboard.
3657421Sroot 			 */
3667421Sroot 			if (sig != SIGSTOP && p->p_pptr == &proc[1]) {
3677421Sroot 				psignal(p, SIGKILL);
3687421Sroot 				p->p_sig &= ~sigmask;
3697421Sroot 				splx(s);
3707421Sroot 				return;
3717421Sroot 			}
3727421Sroot 			/*
3737421Sroot 			 * If a child in vfork(), stopping could
3747421Sroot 			 * cause deadlock.
3757421Sroot 			 */
3767421Sroot 			if (p->p_flag&SVFORK)
3777421Sroot 				goto out;
3787421Sroot 			p->p_sig &= ~sigmask;
3797421Sroot 			p->p_cursig = sig;
3807421Sroot 			stop(p);
3817421Sroot 			goto out;
3827421Sroot 
3837421Sroot 		case SIGIO:
3847421Sroot 		case SIGURG:
3857421Sroot 		case SIGCHLD:
3867421Sroot 			/*
3877421Sroot 			 * These signals are special in that they
3887421Sroot 			 * don't get propogated... if the process
3897421Sroot 			 * isn't interested, forget it.
3907421Sroot 			 */
3917421Sroot 			if (action != SIG_DFL)
3927421Sroot 				goto run;
3937421Sroot 			p->p_sig &= ~sigmask;		/* take it away */
3947421Sroot 			goto out;
3957421Sroot 
3967421Sroot 		default:
3977421Sroot 			/*
3987421Sroot 			 * All other signals cause the process to run
3997421Sroot 			 */
4007421Sroot 			goto run;
4017421Sroot 		}
4027421Sroot 		/*NOTREACHED*/
4037421Sroot 
4047421Sroot 	case SSTOP:
4057421Sroot 		/*
4067421Sroot 		 * If traced process is already stopped,
4077421Sroot 		 * then no further action is necessary.
4087421Sroot 		 */
4097421Sroot 		if (p->p_flag&STRC)
4107421Sroot 			goto out;
4117421Sroot 		switch (sig) {
4127421Sroot 
4137421Sroot 		case SIGKILL:
4147421Sroot 			/*
4157421Sroot 			 * Kill signal always sets processes running.
4167421Sroot 			 */
4177421Sroot 			goto run;
4187421Sroot 
4197421Sroot 		case SIGCONT:
4207421Sroot 			/*
4217421Sroot 			 * If the process catches SIGCONT, let it handle
4227421Sroot 			 * the signal itself.  If it isn't waiting on
4237421Sroot 			 * an event, then it goes back to run state.
4247421Sroot 			 * Otherwise, process goes back to sleep state.
4257421Sroot 			 */
4267421Sroot 			if (action != SIG_DFL || p->p_wchan == 0)
4277421Sroot 				goto run;
4287421Sroot 			p->p_stat = SSLEEP;
4297421Sroot 			goto out;
4307421Sroot 
4317421Sroot 		case SIGSTOP:
4327421Sroot 		case SIGTSTP:
4337421Sroot 		case SIGTTIN:
4347421Sroot 		case SIGTTOU:
4357421Sroot 			/*
4367421Sroot 			 * Already stopped, don't need to stop again.
4377421Sroot 			 * (If we did the shell could get confused.)
4387421Sroot 			 */
4397421Sroot 			p->p_sig &= ~sigmask;		/* take it away */
4407421Sroot 			goto out;
4417421Sroot 
4427421Sroot 		default:
4437421Sroot 			/*
4447421Sroot 			 * If process is sleeping interruptibly, then
4457421Sroot 			 * unstick it so that when it is continued
4467421Sroot 			 * it can look at the signal.
4477421Sroot 			 * But don't setrun the process as its not to
4487421Sroot 			 * be unstopped by the signal alone.
4497421Sroot 			 */
4507421Sroot 			if (p->p_wchan && p->p_pri > PZERO)
4517421Sroot 				unsleep(p);
4527421Sroot 			goto out;
4537421Sroot 		}
4547421Sroot 		/*NOTREACHED*/
4557421Sroot 
4567421Sroot 	default:
4577421Sroot 		/*
4587421Sroot 		 * SRUN, SIDL, SZOMB do nothing with the signal,
4597421Sroot 		 * other than kicking ourselves if we are running.
4607421Sroot 		 * It will either never be noticed, or noticed very soon.
4617421Sroot 		 */
4627421Sroot 		if (p == u.u_procp && !noproc)
4638444Sroot #include "../vax/mtpr.h"
4647421Sroot 			aston();
4657421Sroot 		goto out;
4667421Sroot 	}
4677421Sroot 	/*NOTREACHED*/
4687421Sroot run:
4697421Sroot 	/*
4707421Sroot 	 * Raise priority to at least PUSER.
4717421Sroot 	 */
4727421Sroot 	if (p->p_pri > PUSER)
4737421Sroot 		if ((p != u.u_procp || noproc) && p->p_stat == SRUN &&
4747421Sroot 		    (p->p_flag & SLOAD)) {
4757421Sroot 			remrq(p);
4767421Sroot 			p->p_pri = PUSER;
4777421Sroot 			setrq(p);
4787421Sroot 		} else
4797421Sroot 			p->p_pri = PUSER;
4807421Sroot 	setrun(p);
4817421Sroot out:
4827421Sroot 	splx(s);
4837421Sroot }
4847421Sroot 
4857421Sroot /*
4867421Sroot  * Returns true if the current
4877421Sroot  * process has a signal to process.
4887421Sroot  * The signal to process is put in p_cursig.
4897421Sroot  * This is asked at least once each time a process enters the
4907421Sroot  * system (though this can usually be done without actually
4917421Sroot  * calling issig by checking the pending signal masks.)
4927421Sroot  * A signal does not do anything
4937421Sroot  * directly to a process; it sets
4947421Sroot  * a flag that asks the process to
4957421Sroot  * do something to itself.
4967421Sroot  */
4977421Sroot issig()
4987421Sroot {
4997421Sroot 	register struct proc *p;
5007421Sroot 	register int sig;
50112882Ssam 	int sigbits, sigmask;
5027421Sroot 
5037421Sroot 	p = u.u_procp;
5047421Sroot 	for (;;) {
505*14782Ssam 		sigbits = p->p_sig &~ p->p_sigmask;
5067421Sroot 		if ((p->p_flag&STRC) == 0)
507*14782Ssam 			sigbits &= ~p->p_sigignore;
5087421Sroot 		if (p->p_flag&SVFORK)
5097421Sroot #define bit(a) (1<<(a-1))
5107421Sroot 			sigbits &= ~(bit(SIGSTOP)|bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU));
5117421Sroot 		if (sigbits == 0)
5127421Sroot 			break;
51312882Ssam 		sig = ffs(sigbits);
51412882Ssam 		sigmask = 1 << (sig-1);
5157421Sroot 		p->p_sig &= ~sigmask;		/* take the signal! */
5167421Sroot 		p->p_cursig = sig;
51712882Ssam 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
5187421Sroot 			/*
5197421Sroot 			 * If traced, always stop, and stay
5207421Sroot 			 * stopped until released by the parent.
5217421Sroot 			 */
5227421Sroot 			do {
5237421Sroot 				stop(p);
5247421Sroot 				swtch();
5257421Sroot 			} while (!procxmt() && p->p_flag&STRC);
5267421Sroot 
5277421Sroot 			/*
528*14782Ssam 			 * If the traced bit got turned off,
529*14782Ssam 			 * then put the signal taken above back into p_sig
530*14782Ssam 			 * and go back up to the top to rescan signals.
531*14782Ssam 			 * This ensures that p_sig* and u_signal are consistent.
5327421Sroot 			 */
533*14782Ssam 			if ((p->p_flag&STRC) == 0) {
5347421Sroot 				p->p_sig |= sigmask;
5357421Sroot 				continue;
5367421Sroot 			}
5377421Sroot 
5387421Sroot 			/*
5397421Sroot 			 * If parent wants us to take the signal,
5407421Sroot 			 * then it will leave it in p->p_cursig;
5417421Sroot 			 * otherwise we just look for signals again.
5427421Sroot 			 */
5437421Sroot 			sig = p->p_cursig;
5447421Sroot 			if (sig == 0)
5457421Sroot 				continue;
546*14782Ssam 
547*14782Ssam 			/*
548*14782Ssam 			 * If signal is being masked put it back
549*14782Ssam 			 * into p_sig and look for other signals.
550*14782Ssam 			 */
551*14782Ssam 			sigmask = 1 << (sig-1);
552*14782Ssam 			if (p->p_sigmask & sigmask) {
553*14782Ssam 				p->p_sig |= sigmask;
554*14782Ssam 				continue;
555*14782Ssam 			}
5567421Sroot 		}
5577421Sroot 		switch (u.u_signal[sig]) {
5587421Sroot 
5597421Sroot 		case SIG_DFL:
5607421Sroot 			/*
5617421Sroot 			 * Don't take default actions on system processes.
5627421Sroot 			 */
5637421Sroot 			if (p->p_ppid == 0)
5647421Sroot 				break;
5657421Sroot 			switch (sig) {
5667421Sroot 
5677421Sroot 			case SIGTSTP:
5687421Sroot 			case SIGTTIN:
5697421Sroot 			case SIGTTOU:
5707421Sroot 				/*
5717421Sroot 				 * Children of init aren't allowed to stop
5727421Sroot 				 * on signals from the keyboard.
5737421Sroot 				 */
5747421Sroot 				if (p->p_pptr == &proc[1]) {
5757421Sroot 					psignal(p, SIGKILL);
5767421Sroot 					continue;
5777421Sroot 				}
5787421Sroot 				/* fall into ... */
5797421Sroot 
5807421Sroot 			case SIGSTOP:
5817421Sroot 				if (p->p_flag&STRC)
5827421Sroot 					continue;
5837421Sroot 				stop(p);
5847421Sroot 				swtch();
5857421Sroot 				continue;
5867421Sroot 
5877421Sroot 			case SIGCONT:
5887421Sroot 			case SIGCHLD:
58912882Ssam 			case SIGURG:
59012951Ssam 			case SIGIO:
5917421Sroot 				/*
5927421Sroot 				 * These signals are normally not
5937421Sroot 				 * sent if the action is the default.
5947421Sroot 				 */
5957421Sroot 				continue;		/* == ignore */
5967421Sroot 
5977421Sroot 			default:
5987421Sroot 				goto send;
5997421Sroot 			}
6007421Sroot 			/*NOTREACHED*/
6017421Sroot 
6027421Sroot 		case SIG_HOLD:
6037421Sroot 		case SIG_IGN:
6047421Sroot 			/*
6057421Sroot 			 * Masking above should prevent us
6067421Sroot 			 * ever trying to take action on a held
6077421Sroot 			 * or ignored signal, unless process is traced.
6087421Sroot 			 */
6097421Sroot 			if ((p->p_flag&STRC) == 0)
6107421Sroot 				printf("issig\n");
6117421Sroot 			continue;
6127421Sroot 
6137421Sroot 		default:
6147421Sroot 			/*
6157421Sroot 			 * This signal has an action, let
6167421Sroot 			 * psig process it.
6177421Sroot 			 */
6187421Sroot 			goto send;
6197421Sroot 		}
6207421Sroot 		/*NOTREACHED*/
6217421Sroot 	}
6227421Sroot 	/*
6237421Sroot 	 * Didn't find a signal to send.
6247421Sroot 	 */
6257421Sroot 	p->p_cursig = 0;
6267421Sroot 	return (0);
6277421Sroot 
6287421Sroot send:
6297421Sroot 	/*
6307421Sroot 	 * Let psig process the signal.
6317421Sroot 	 */
6327421Sroot 	return (sig);
6337421Sroot }
6347421Sroot 
6357421Sroot /*
6367421Sroot  * Put the argument process into the stopped
6377421Sroot  * state and notify the parent via wakeup and/or signal.
6387421Sroot  */
6397421Sroot stop(p)
6407421Sroot 	register struct proc *p;
6417421Sroot {
6427421Sroot 
6437421Sroot 	p->p_stat = SSTOP;
6447421Sroot 	p->p_flag &= ~SWTED;
6457421Sroot 	wakeup((caddr_t)p->p_pptr);
6467421Sroot 	/*
6477421Sroot 	 * Avoid sending signal to parent if process is traced
6487421Sroot 	 */
6497421Sroot 	if (p->p_flag&STRC)
6507421Sroot 		return;
6517421Sroot 	psignal(p->p_pptr, SIGCHLD);
6527421Sroot }
6537421Sroot 
6547421Sroot /*
6557421Sroot  * Perform the action specified by
6567421Sroot  * the current signal.
6577421Sroot  * The usual sequence is:
6587421Sroot  *	if (issig())
6597421Sroot  *		psig();
6607421Sroot  * The signal bit has already been cleared by issig,
6617421Sroot  * and the current signal number stored in p->p_cursig.
6627421Sroot  */
6637421Sroot psig()
6647421Sroot {
66512882Ssam 	register struct proc *p = u.u_procp;
66612882Ssam 	register int sig = p->p_cursig;
66712882Ssam 	int sigmask = 1 << (sig - 1), returnmask;
6687421Sroot 	register int (*action)();
6697421Sroot 
67012882Ssam 	if (sig == 0)
6717421Sroot 		panic("psig");
67212882Ssam 	action = u.u_signal[sig];
6737421Sroot 	if (action != SIG_DFL) {
67412882Ssam 		if (action == SIG_IGN || (p->p_sigmask & sigmask))
6757421Sroot 			panic("psig action");
6767421Sroot 		u.u_error = 0;
6777421Sroot 		/*
67812882Ssam 		 * Set the new mask value and also defer further
67912882Ssam 		 * occurences of this signal (unless we're simulating
68012882Ssam 		 * the old signal facilities).
68112882Ssam 		 *
68212882Ssam 		 * Special case: user has done a sigpause.  Here the
68312882Ssam 		 * current mask is not of interest, but rather the
68412882Ssam 		 * mask from before the sigpause is what we want restored
68512882Ssam 		 * after the signal processing is completed.
6867421Sroot 		 */
68712882Ssam 		(void) spl6();
68812882Ssam 		if (p->p_flag & SOUSIG) {
68912882Ssam 			if (sig != SIGILL && sig != SIGTRAP) {
69012882Ssam 				u.u_signal[sig] = SIG_DFL;
69112882Ssam 				p->p_sigcatch &= ~sigmask;
69212882Ssam 			}
69312882Ssam 			sigmask = 0;
6947421Sroot 		}
69512882Ssam 		if (p->p_flag & SOMASK) {
69612882Ssam 			returnmask = u.u_oldmask;
69712882Ssam 			p->p_flag &= ~SOMASK;
69812882Ssam 		} else
69912882Ssam 			returnmask = p->p_sigmask;
70012951Ssam 		p->p_sigmask |= u.u_sigmask[sig] | sigmask;
70112882Ssam 		(void) spl0();
7028032Sroot 		u.u_ru.ru_nsignals++;
70312882Ssam 		sendsig(action, sig, returnmask);
70412882Ssam 		p->p_cursig = 0;
7057421Sroot 		return;
7067421Sroot 	}
7077421Sroot 	u.u_acflag |= AXSIG;
70812882Ssam 	switch (sig) {
7097421Sroot 
7107421Sroot 	case SIGILL:
7117421Sroot 	case SIGIOT:
7127421Sroot 	case SIGBUS:
7137421Sroot 	case SIGQUIT:
7147421Sroot 	case SIGTRAP:
7157421Sroot 	case SIGEMT:
7167421Sroot 	case SIGFPE:
7177421Sroot 	case SIGSEGV:
7187421Sroot 	case SIGSYS:
71912882Ssam 		u.u_arg[0] = sig;
7207421Sroot 		if (core())
72112882Ssam 			sig += 0200;
7227421Sroot 	}
72312882Ssam 	exit(sig);
7247421Sroot }
7257421Sroot 
7267421Sroot /*
7277421Sroot  * Create a core image on the file "core"
7287421Sroot  * If you are looking for protection glitches,
7297421Sroot  * there are probably a wealth of them here
7307421Sroot  * when this occurs to a suid command.
7317421Sroot  *
7327421Sroot  * It writes UPAGES block of the
7337421Sroot  * user.h area followed by the entire
7347421Sroot  * data+stack segments.
7357421Sroot  */
7367421Sroot core()
7377421Sroot {
7387421Sroot 	register struct inode *ip;
7397421Sroot 	extern schar();
7407421Sroot 
74112639Ssam 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
7427818Sroot 		return (0);
7438032Sroot 	if (ctob(UPAGES+u.u_dsize+u.u_ssize) >=
7448032Sroot 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
7457421Sroot 		return (0);
7467421Sroot 	u.u_error = 0;
7477421Sroot 	u.u_dirp = "core";
7489160Ssam 	ip = namei(schar, CREATE, 1);
7497421Sroot 	if (ip == NULL) {
7507421Sroot 		if (u.u_error)
7517421Sroot 			return (0);
75212639Ssam 		ip = maknode(0644);
7537421Sroot 		if (ip==NULL)
7547421Sroot 			return (0);
7557421Sroot 	}
7567818Sroot 	if (access(ip, IWRITE) ||
7577818Sroot 	   (ip->i_mode&IFMT) != IFREG ||
7587818Sroot 	   ip->i_nlink != 1) {
7597421Sroot 		u.u_error = EFAULT;
7607818Sroot 		goto out;
7617818Sroot 	}
7629160Ssam 	itrunc(ip, (u_long)0);
7637818Sroot 	u.u_acflag |= ACORE;
76412882Ssam 	u.u_error = rdwri(UIO_WRITE, ip,
76512882Ssam 	    (caddr_t)&u,
76612882Ssam 	    ctob(UPAGES),
76712882Ssam 	    0, 1, (int *)0);
7688101Sroot 	if (u.u_error == 0)
7698644Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
7708967Sroot 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
7718967Sroot 		    ctob(u.u_dsize),
7728644Sroot 		    ctob(UPAGES), 0, (int *)0);
7738101Sroot 	if (u.u_error == 0)
7748644Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
7758967Sroot 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
7768967Sroot 		    ctob(u.u_ssize),
7778644Sroot 		    ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0);
7787818Sroot out:
7797421Sroot 	iput(ip);
7807818Sroot 	return (u.u_error == 0);
7817421Sroot }
782