xref: /csrg-svn/sys/kern/kern_sig.c (revision 37580)
123374Smckusick /*
2*37580Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3*37580Smckusick  * All rights reserved.
423374Smckusick  *
5*37580Smckusick  * Redistribution and use in source and binary forms are permitted
6*37580Smckusick  * provided that the above copyright notice and this paragraph are
7*37580Smckusick  * duplicated in all such forms and that any documentation,
8*37580Smckusick  * advertising materials, and other materials related to such
9*37580Smckusick  * distribution and use acknowledge that the software was developed
10*37580Smckusick  * by the University of California, Berkeley.  The name of the
11*37580Smckusick  * University may not be used to endorse or promote products derived
12*37580Smckusick  * from this software without specific prior written permission.
13*37580Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*37580Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*37580Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*37580Smckusick  *
17*37580Smckusick  *	@(#)kern_sig.c	7.3.1.1 (Berkeley) 05/01/89
1823374Smckusick  */
197421Sroot 
20*37580Smckusick #include "machine/reg.h"
21*37580Smckusick #include "machine/pte.h"
22*37580Smckusick #include "machine/psl.h"
23*37580Smckusick #include "machine/mtpr.h"
24*37580Smckusick 
2517092Sbloom #include "param.h"
2617092Sbloom #include "systm.h"
2717092Sbloom #include "dir.h"
28*37580Smckusick #include "ucred.h"
2917092Sbloom #include "user.h"
30*37580Smckusick #include "vnode.h"
3117092Sbloom #include "proc.h"
3217092Sbloom #include "timeb.h"
3317092Sbloom #include "times.h"
3417092Sbloom #include "buf.h"
3517092Sbloom #include "mount.h"
3617092Sbloom #include "text.h"
3717092Sbloom #include "seg.h"
3817092Sbloom #include "vm.h"
3917092Sbloom #include "acct.h"
4017092Sbloom #include "uio.h"
41*37580Smckusick #include "file.h"
4217092Sbloom #include "kernel.h"
437421Sroot 
4417153Sbloom #define	cantmask	(sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP))
4526276Skarels #define	stopsigmask	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
4626276Skarels 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
4712951Ssam 
4817013Smckusick /*
4917013Smckusick  * Generalized interface signal handler.
5017013Smckusick  */
517499Sroot sigvec()
527421Sroot {
5312951Ssam 	register struct a {
5412882Ssam 		int	signo;
5512951Ssam 		struct	sigvec *nsv;
5612951Ssam 		struct	sigvec *osv;
5712882Ssam 	} *uap = (struct a  *)u.u_ap;
5812951Ssam 	struct sigvec vec;
5912951Ssam 	register struct sigvec *sv;
6012882Ssam 	register int sig;
6118308Smckusick 	int bit;
627421Sroot 
6312882Ssam 	sig = uap->signo;
6412951Ssam 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) {
6512882Ssam 		u.u_error = EINVAL;
6612882Ssam 		return;
6712882Ssam 	}
6812951Ssam 	sv = &vec;
6912951Ssam 	if (uap->osv) {
7012951Ssam 		sv->sv_handler = u.u_signal[sig];
7112951Ssam 		sv->sv_mask = u.u_sigmask[sig];
7218308Smckusick 		bit = sigmask(sig);
7318308Smckusick 		sv->sv_flags = 0;
7418308Smckusick 		if ((u.u_sigonstack & bit) != 0)
7518308Smckusick 			sv->sv_flags |= SV_ONSTACK;
7618308Smckusick 		if ((u.u_sigintr & bit) != 0)
7718308Smckusick 			sv->sv_flags |= SV_INTERRUPT;
7812951Ssam 		u.u_error =
7912951Ssam 		    copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec));
8012951Ssam 		if (u.u_error)
8112951Ssam 			return;
8212951Ssam 	}
8312951Ssam 	if (uap->nsv) {
8412951Ssam 		u.u_error =
8512951Ssam 		    copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec));
8612951Ssam 		if (u.u_error)
8712951Ssam 			return;
8812951Ssam 		if (sig == SIGCONT && sv->sv_handler == SIG_IGN) {
8912951Ssam 			u.u_error = EINVAL;
9012951Ssam 			return;
9112951Ssam 		}
9212951Ssam 		setsigvec(sig, sv);
9312951Ssam 	}
947421Sroot }
957421Sroot 
9612951Ssam setsigvec(sig, sv)
9712951Ssam 	int sig;
9812951Ssam 	register struct sigvec *sv;
9912882Ssam {
10012882Ssam 	register struct proc *p;
10112951Ssam 	register int bit;
10212882Ssam 
10317153Sbloom 	bit = sigmask(sig);
10412882Ssam 	p = u.u_procp;
10512882Ssam 	/*
10612882Ssam 	 * Change setting atomically.
10712882Ssam 	 */
10817153Sbloom 	(void) splhigh();
10912951Ssam 	u.u_signal[sig] = sv->sv_handler;
11012951Ssam 	u.u_sigmask[sig] = sv->sv_mask &~ cantmask;
11118308Smckusick 	if (sv->sv_flags & SV_INTERRUPT)
11218308Smckusick 		u.u_sigintr |= bit;
11318308Smckusick 	else
11418308Smckusick 		u.u_sigintr &= ~bit;
11518308Smckusick 	if (sv->sv_flags & SV_ONSTACK)
11612951Ssam 		u.u_sigonstack |= bit;
11712951Ssam 	else
11812951Ssam 		u.u_sigonstack &= ~bit;
11912951Ssam 	if (sv->sv_handler == SIG_IGN) {
12012951Ssam 		p->p_sig &= ~bit;		/* never to be seen again */
12112951Ssam 		p->p_sigignore |= bit;
12212951Ssam 		p->p_sigcatch &= ~bit;
12312882Ssam 	} else {
12412951Ssam 		p->p_sigignore &= ~bit;
12512951Ssam 		if (sv->sv_handler == SIG_DFL)
12612951Ssam 			p->p_sigcatch &= ~bit;
12712882Ssam 		else
12812951Ssam 			p->p_sigcatch |= bit;
12912882Ssam 	}
13012882Ssam 	(void) spl0();
13112882Ssam }
13212882Ssam 
1337499Sroot sigblock()
1347421Sroot {
13512882Ssam 	struct a {
13617153Sbloom 		int	mask;
13712882Ssam 	} *uap = (struct a *)u.u_ap;
13812951Ssam 	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 sigsetmask()
1477499Sroot {
14812882Ssam 	struct a {
14917153Sbloom 		int	mask;
15012882Ssam 	} *uap = (struct a *)u.u_ap;
15112882Ssam 	register struct proc *p = u.u_procp;
1527499Sroot 
15317153Sbloom 	(void) splhigh();
15412882Ssam 	u.u_r.r_val1 = p->p_sigmask;
15517153Sbloom 	p->p_sigmask = uap->mask &~ cantmask;
15612882Ssam 	(void) spl0();
1577499Sroot }
1587499Sroot 
1597499Sroot sigpause()
1607499Sroot {
16112882Ssam 	struct a {
16217153Sbloom 		int	mask;
16312882Ssam 	} *uap = (struct a *)u.u_ap;
16412882Ssam 	register struct proc *p = u.u_procp;
1657499Sroot 
16612882Ssam 	/*
16712882Ssam 	 * When returning from sigpause, we want
16812882Ssam 	 * the old mask to be restored after the
16912882Ssam 	 * signal handler has finished.  Thus, we
17012882Ssam 	 * save it here and mark the proc structure
17112882Ssam 	 * to indicate this (should be in u.).
17212882Ssam 	 */
17312882Ssam 	u.u_oldmask = p->p_sigmask;
17412882Ssam 	p->p_flag |= SOMASK;
17517153Sbloom 	p->p_sigmask = uap->mask &~ cantmask;
17612882Ssam 	for (;;)
17712882Ssam 		sleep((caddr_t)&u, PSLEP);
17812882Ssam 	/*NOTREACHED*/
1797499Sroot }
18012951Ssam #undef cantmask
1817499Sroot 
1827499Sroot sigstack()
1837499Sroot {
18412951Ssam 	register struct a {
18512951Ssam 		struct	sigstack *nss;
18612951Ssam 		struct	sigstack *oss;
18712882Ssam 	} *uap = (struct a *)u.u_ap;
18812951Ssam 	struct sigstack ss;
1897499Sroot 
19012951Ssam 	if (uap->oss) {
19112951Ssam 		u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss,
19212951Ssam 		    sizeof (struct sigstack));
19312951Ssam 		if (u.u_error)
19412951Ssam 			return;
19512951Ssam 	}
19612951Ssam 	if (uap->nss) {
19712951Ssam 		u.u_error =
19812951Ssam 		    copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss));
19912951Ssam 		if (u.u_error == 0)
20012951Ssam 			u.u_sigstack = ss;
20112951Ssam 	}
2027499Sroot }
2037499Sroot 
2048032Sroot kill()
2058032Sroot {
20612882Ssam 	register struct a {
20712882Ssam 		int	pid;
20812882Ssam 		int	signo;
20912882Ssam 	} *uap = (struct a *)u.u_ap;
21018336Smckusick 	register struct proc *p;
2118032Sroot 
21218336Smckusick 	if (uap->signo < 0 || uap->signo > NSIG) {
21318336Smckusick 		u.u_error = EINVAL;
21418336Smckusick 		return;
21518336Smckusick 	}
21618336Smckusick 	if (uap->pid > 0) {
21718336Smckusick 		/* kill single process */
21818336Smckusick 		p = pfind(uap->pid);
21918336Smckusick 		if (p == 0) {
22018336Smckusick 			u.u_error = ESRCH;
22118336Smckusick 			return;
22218336Smckusick 		}
22318336Smckusick 		if (u.u_uid && u.u_uid != p->p_uid)
22418336Smckusick 			u.u_error = EPERM;
22518336Smckusick 		else if (uap->signo)
22618336Smckusick 			psignal(p, uap->signo);
22718336Smckusick 		return;
22818336Smckusick 	}
22918336Smckusick 	switch (uap->pid) {
23018336Smckusick 	case -1:		/* broadcast signal */
23120998Smckusick 		u.u_error = killpg1(uap->signo, 0, 1);
23218336Smckusick 		break;
23318336Smckusick 	case 0:			/* signal own process group */
23418336Smckusick 		u.u_error = killpg1(uap->signo, 0, 0);
23518336Smckusick 		break;
23618336Smckusick 	default:		/* negative explicit process group */
23718336Smckusick 		u.u_error = killpg1(uap->signo, -uap->pid, 0);
23818336Smckusick 		break;
23918336Smckusick 	}
24018336Smckusick 	return;
2418032Sroot }
2428032Sroot 
2438032Sroot killpg()
2448032Sroot {
2459989Ssam 	register struct a {
246*37580Smckusick 		int	pgrp;
2479989Ssam 		int	signo;
2489989Ssam 	} *uap = (struct a *)u.u_ap;
2498032Sroot 
25018336Smckusick 	if (uap->signo < 0 || uap->signo > NSIG) {
25118336Smckusick 		u.u_error = EINVAL;
25218336Smckusick 		return;
25318336Smckusick 	}
254*37580Smckusick 	u.u_error = killpg1(uap->signo, uap->pgrp, 0);
2558032Sroot }
2568032Sroot 
25712882Ssam /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
25812882Ssam 
259*37580Smckusick killpg1(signo, pgrp, all)
260*37580Smckusick 	int signo, pgrp, all;
2619989Ssam {
2629989Ssam 	register struct proc *p;
263*37580Smckusick 	int f, error = 0;
2649989Ssam 
265*37580Smckusick 	if (!all && pgrp == 0) {
266*37580Smckusick 		/*
267*37580Smckusick 		 * Zero process id means send to my process group.
2687421Sroot 		 */
269*37580Smckusick 		pgrp = u.u_procp->p_pgrp;
270*37580Smckusick 		if (pgrp == 0)
271*37580Smckusick 			return (ESRCH);
272*37580Smckusick 	}
273*37580Smckusick 	for (f = 0, p = allproc; p != NULL; p = p->p_nxt) {
274*37580Smckusick 		if ((p->p_pgrp != pgrp && !all) || p->p_ppid == 0 ||
275*37580Smckusick 		    (p->p_flag&SSYS) || (all && p == u.u_procp))
276*37580Smckusick 			continue;
277*37580Smckusick 		if (u.u_uid != 0 && u.u_uid != p->p_uid &&
278*37580Smckusick 		    (signo != SIGCONT || !inferior(p))) {
279*37580Smckusick 			if (!all)
28020998Smckusick 				error = EPERM;
281*37580Smckusick 			continue;
28218336Smckusick 		}
283*37580Smckusick 		f++;
284*37580Smckusick 		if (signo)
285*37580Smckusick 			psignal(p, signo);
2867421Sroot 	}
28724420Skarels 	return (error ? error : (f == 0 ? ESRCH : 0));
2887421Sroot }
2897421Sroot 
2907421Sroot /*
2917421Sroot  * Send the specified signal to
292*37580Smckusick  * all processes with 'pgrp' as
2937421Sroot  * process group.
2947421Sroot  */
295*37580Smckusick gsignal(pgrp, sig)
296*37580Smckusick 	register int pgrp;
2977421Sroot {
2987421Sroot 	register struct proc *p;
2997421Sroot 
300*37580Smckusick 	if (pgrp == 0)
3017421Sroot 		return;
302*37580Smckusick 	for (p = allproc; p != NULL; p = p->p_nxt)
303*37580Smckusick 		if (p->p_pgrp == pgrp)
304*37580Smckusick 			psignal(p, sig);
3057421Sroot }
3067421Sroot 
3077421Sroot /*
3087421Sroot  * Send the specified signal to
3097421Sroot  * the specified process.
3107421Sroot  */
3117421Sroot psignal(p, sig)
3127421Sroot 	register struct proc *p;
3137421Sroot 	register int sig;
3147421Sroot {
3157421Sroot 	register int s;
3167421Sroot 	register int (*action)();
31717153Sbloom 	int mask;
3187421Sroot 
3197421Sroot 	if ((unsigned)sig >= NSIG)
3207421Sroot 		return;
32117153Sbloom 	mask = sigmask(sig);
3227421Sroot 
3237421Sroot 	/*
3247421Sroot 	 * If proc is traced, always give parent a chance.
3257421Sroot 	 */
3267421Sroot 	if (p->p_flag & STRC)
3277421Sroot 		action = SIG_DFL;
3287421Sroot 	else {
3297421Sroot 		/*
33012882Ssam 		 * If the signal is being ignored,
33112882Ssam 		 * then we forget about it immediately.
3327421Sroot 		 */
33317153Sbloom 		if (p->p_sigignore & mask)
3347421Sroot 			return;
33517153Sbloom 		if (p->p_sigmask & mask)
33612882Ssam 			action = SIG_HOLD;
33717153Sbloom 		else if (p->p_sigcatch & mask)
33812882Ssam 			action = SIG_CATCH;
33912882Ssam 		else
34012882Ssam 			action = SIG_DFL;
3417421Sroot 	}
3427421Sroot 	if (sig) {
343*37580Smckusick 		p->p_sig |= mask;
3447421Sroot 		switch (sig) {
3457421Sroot 
3467421Sroot 		case SIGTERM:
34712882Ssam 			if ((p->p_flag&STRC) || action != SIG_DFL)
3487421Sroot 				break;
3497421Sroot 			/* fall into ... */
3507421Sroot 
3517421Sroot 		case SIGKILL:
3527421Sroot 			if (p->p_nice > NZERO)
3537421Sroot 				p->p_nice = NZERO;
3547421Sroot 			break;
3557421Sroot 
3567421Sroot 		case SIGCONT:
35726276Skarels 			p->p_sig &= ~stopsigmask;
3587421Sroot 			break;
3597421Sroot 
360*37580Smckusick 		case SIGSTOP:
3617421Sroot 		case SIGTSTP:
3627421Sroot 		case SIGTTIN:
3637421Sroot 		case SIGTTOU:
36417153Sbloom 			p->p_sig &= ~sigmask(SIGCONT);
3657421Sroot 			break;
3667421Sroot 		}
3677421Sroot 	}
3687421Sroot 	/*
3697421Sroot 	 * Defer further processing for signals which are held.
3707421Sroot 	 */
3717421Sroot 	if (action == SIG_HOLD)
3727421Sroot 		return;
37317153Sbloom 	s = splhigh();
3747421Sroot 	switch (p->p_stat) {
3757421Sroot 
3767421Sroot 	case SSLEEP:
3777421Sroot 		/*
3787421Sroot 		 * If process is sleeping at negative priority
3797421Sroot 		 * we can't interrupt the sleep... the signal will
3807421Sroot 		 * be noticed when the process returns through
3817421Sroot 		 * trap() or syscall().
3827421Sroot 		 */
3837421Sroot 		if (p->p_pri <= PZERO)
3847421Sroot 			goto out;
3857421Sroot 		/*
3867421Sroot 		 * Process is sleeping and traced... make it runnable
3877421Sroot 		 * so it can discover the signal in issig() and stop
3887421Sroot 		 * for the parent.
3897421Sroot 		 */
3907421Sroot 		if (p->p_flag&STRC)
3917421Sroot 			goto run;
3927421Sroot 		switch (sig) {
3937421Sroot 
3947421Sroot 		case SIGSTOP:
3957421Sroot 		case SIGTSTP:
3967421Sroot 		case SIGTTIN:
3977421Sroot 		case SIGTTOU:
3987421Sroot 			/*
3997421Sroot 			 * These are the signals which by default
4007421Sroot 			 * stop a process.
4017421Sroot 			 */
4027421Sroot 			if (action != SIG_DFL)
4037421Sroot 				goto run;
4047421Sroot 			/*
405*37580Smckusick 			 * Don't clog system with children of init
406*37580Smckusick 			 * stopped from the keyboard.
407*37580Smckusick 			 */
408*37580Smckusick 			if (sig != SIGSTOP && p->p_pptr == &proc[1]) {
409*37580Smckusick 				psignal(p, SIGKILL);
410*37580Smckusick 				p->p_sig &= ~mask;
411*37580Smckusick 				splx(s);
412*37580Smckusick 				return;
413*37580Smckusick 			}
414*37580Smckusick 			/*
4157421Sroot 			 * If a child in vfork(), stopping could
4167421Sroot 			 * cause deadlock.
4177421Sroot 			 */
4187421Sroot 			if (p->p_flag&SVFORK)
4197421Sroot 				goto out;
42017153Sbloom 			p->p_sig &= ~mask;
4217421Sroot 			p->p_cursig = sig;
42218331Skarels 			psignal(p->p_pptr, SIGCHLD);
4237421Sroot 			stop(p);
4247421Sroot 			goto out;
4257421Sroot 
4267421Sroot 		case SIGIO:
4277421Sroot 		case SIGURG:
4287421Sroot 		case SIGCHLD:
42917597Sbloom 		case SIGWINCH:
4307421Sroot 			/*
4317421Sroot 			 * These signals are special in that they
4327421Sroot 			 * don't get propogated... if the process
4337421Sroot 			 * isn't interested, forget it.
4347421Sroot 			 */
4357421Sroot 			if (action != SIG_DFL)
4367421Sroot 				goto run;
43717153Sbloom 			p->p_sig &= ~mask;		/* take it away */
4387421Sroot 			goto out;
4397421Sroot 
4407421Sroot 		default:
4417421Sroot 			/*
4427421Sroot 			 * All other signals cause the process to run
4437421Sroot 			 */
4447421Sroot 			goto run;
4457421Sroot 		}
4467421Sroot 		/*NOTREACHED*/
4477421Sroot 
4487421Sroot 	case SSTOP:
4497421Sroot 		/*
4507421Sroot 		 * If traced process is already stopped,
4517421Sroot 		 * then no further action is necessary.
4527421Sroot 		 */
4537421Sroot 		if (p->p_flag&STRC)
4547421Sroot 			goto out;
4557421Sroot 		switch (sig) {
4567421Sroot 
4577421Sroot 		case SIGKILL:
4587421Sroot 			/*
4597421Sroot 			 * Kill signal always sets processes running.
4607421Sroot 			 */
4617421Sroot 			goto run;
4627421Sroot 
4637421Sroot 		case SIGCONT:
4647421Sroot 			/*
4657421Sroot 			 * If the process catches SIGCONT, let it handle
4667421Sroot 			 * the signal itself.  If it isn't waiting on
4677421Sroot 			 * an event, then it goes back to run state.
4687421Sroot 			 * Otherwise, process goes back to sleep state.
4697421Sroot 			 */
4707421Sroot 			if (action != SIG_DFL || p->p_wchan == 0)
4717421Sroot 				goto run;
4727421Sroot 			p->p_stat = SSLEEP;
4737421Sroot 			goto out;
4747421Sroot 
4757421Sroot 		case SIGSTOP:
4767421Sroot 		case SIGTSTP:
4777421Sroot 		case SIGTTIN:
4787421Sroot 		case SIGTTOU:
4797421Sroot 			/*
4807421Sroot 			 * Already stopped, don't need to stop again.
4817421Sroot 			 * (If we did the shell could get confused.)
4827421Sroot 			 */
48317153Sbloom 			p->p_sig &= ~mask;		/* take it away */
4847421Sroot 			goto out;
4857421Sroot 
4867421Sroot 		default:
4877421Sroot 			/*
4887421Sroot 			 * If process is sleeping interruptibly, then
4897421Sroot 			 * unstick it so that when it is continued
4907421Sroot 			 * it can look at the signal.
4917421Sroot 			 * But don't setrun the process as its not to
4927421Sroot 			 * be unstopped by the signal alone.
4937421Sroot 			 */
4947421Sroot 			if (p->p_wchan && p->p_pri > PZERO)
4957421Sroot 				unsleep(p);
4967421Sroot 			goto out;
4977421Sroot 		}
4987421Sroot 		/*NOTREACHED*/
4997421Sroot 
5007421Sroot 	default:
5017421Sroot 		/*
5027421Sroot 		 * SRUN, SIDL, SZOMB do nothing with the signal,
5037421Sroot 		 * other than kicking ourselves if we are running.
5047421Sroot 		 * It will either never be noticed, or noticed very soon.
5057421Sroot 		 */
5067421Sroot 		if (p == u.u_procp && !noproc)
5077421Sroot 			aston();
5087421Sroot 		goto out;
5097421Sroot 	}
5107421Sroot 	/*NOTREACHED*/
5117421Sroot run:
5127421Sroot 	/*
5137421Sroot 	 * Raise priority to at least PUSER.
5147421Sroot 	 */
5157421Sroot 	if (p->p_pri > PUSER)
51617399Skarels 		p->p_pri = PUSER;
5177421Sroot 	setrun(p);
5187421Sroot out:
5197421Sroot 	splx(s);
5207421Sroot }
5217421Sroot 
5227421Sroot /*
5237421Sroot  * Returns true if the current
5247421Sroot  * process has a signal to process.
5257421Sroot  * The signal to process is put in p_cursig.
5267421Sroot  * This is asked at least once each time a process enters the
5277421Sroot  * system (though this can usually be done without actually
5287421Sroot  * calling issig by checking the pending signal masks.)
5297421Sroot  * A signal does not do anything
5307421Sroot  * directly to a process; it sets
5317421Sroot  * a flag that asks the process to
5327421Sroot  * do something to itself.
5337421Sroot  */
5347421Sroot issig()
5357421Sroot {
5367421Sroot 	register struct proc *p;
5377421Sroot 	register int sig;
53817153Sbloom 	int sigbits, mask;
5397421Sroot 
5407421Sroot 	p = u.u_procp;
5417421Sroot 	for (;;) {
54214782Ssam 		sigbits = p->p_sig &~ p->p_sigmask;
5437421Sroot 		if ((p->p_flag&STRC) == 0)
54414782Ssam 			sigbits &= ~p->p_sigignore;
5457421Sroot 		if (p->p_flag&SVFORK)
54626276Skarels 			sigbits &= ~stopsigmask;
5477421Sroot 		if (sigbits == 0)
5487421Sroot 			break;
54926354Skarels 		sig = ffs((long)sigbits);
55017153Sbloom 		mask = sigmask(sig);
55117153Sbloom 		p->p_sig &= ~mask;		/* take the signal! */
5527421Sroot 		p->p_cursig = sig;
55312882Ssam 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
5547421Sroot 			/*
5557421Sroot 			 * If traced, always stop, and stay
5567421Sroot 			 * stopped until released by the parent.
5577421Sroot 			 */
55818331Skarels 			psignal(p->p_pptr, SIGCHLD);
5597421Sroot 			do {
5607421Sroot 				stop(p);
5617421Sroot 				swtch();
5627421Sroot 			} while (!procxmt() && p->p_flag&STRC);
5637421Sroot 
5647421Sroot 			/*
56514782Ssam 			 * If the traced bit got turned off,
56614782Ssam 			 * then put the signal taken above back into p_sig
56714782Ssam 			 * and go back up to the top to rescan signals.
56814782Ssam 			 * This ensures that p_sig* and u_signal are consistent.
5697421Sroot 			 */
57014782Ssam 			if ((p->p_flag&STRC) == 0) {
57117153Sbloom 				p->p_sig |= mask;
5727421Sroot 				continue;
5737421Sroot 			}
5747421Sroot 
5757421Sroot 			/*
5767421Sroot 			 * If parent wants us to take the signal,
5777421Sroot 			 * then it will leave it in p->p_cursig;
5787421Sroot 			 * otherwise we just look for signals again.
5797421Sroot 			 */
5807421Sroot 			sig = p->p_cursig;
5817421Sroot 			if (sig == 0)
5827421Sroot 				continue;
58314782Ssam 
58414782Ssam 			/*
58514782Ssam 			 * If signal is being masked put it back
58614782Ssam 			 * into p_sig and look for other signals.
58714782Ssam 			 */
58817153Sbloom 			mask = sigmask(sig);
58917153Sbloom 			if (p->p_sigmask & mask) {
59017153Sbloom 				p->p_sig |= mask;
59114782Ssam 				continue;
59214782Ssam 			}
5937421Sroot 		}
59424901Skarels 		switch ((int)u.u_signal[sig]) {
5957421Sroot 
5967421Sroot 		case SIG_DFL:
5977421Sroot 			/*
5987421Sroot 			 * Don't take default actions on system processes.
5997421Sroot 			 */
6007421Sroot 			if (p->p_ppid == 0)
6017421Sroot 				break;
6027421Sroot 			switch (sig) {
6037421Sroot 
6047421Sroot 			case SIGTSTP:
6057421Sroot 			case SIGTTIN:
6067421Sroot 			case SIGTTOU:
607*37580Smckusick 				/*
608*37580Smckusick 				 * Children of init aren't allowed to stop
609*37580Smckusick 				 * on signals from the keyboard.
610*37580Smckusick 				 */
611*37580Smckusick 				if (p->p_pptr == &proc[1]) {
612*37580Smckusick 					psignal(p, SIGKILL);
613*37580Smckusick 					continue;
614*37580Smckusick 				}
615*37580Smckusick 				/* fall into ... */
616*37580Smckusick 
6177421Sroot 			case SIGSTOP:
6187421Sroot 				if (p->p_flag&STRC)
6197421Sroot 					continue;
62018331Skarels 				psignal(p->p_pptr, SIGCHLD);
6217421Sroot 				stop(p);
6227421Sroot 				swtch();
6237421Sroot 				continue;
6247421Sroot 
6257421Sroot 			case SIGCONT:
6267421Sroot 			case SIGCHLD:
62712882Ssam 			case SIGURG:
62812951Ssam 			case SIGIO:
62917597Sbloom 			case SIGWINCH:
6307421Sroot 				/*
6317421Sroot 				 * These signals are normally not
6327421Sroot 				 * sent if the action is the default.
6337421Sroot 				 */
6347421Sroot 				continue;		/* == ignore */
6357421Sroot 
6367421Sroot 			default:
6377421Sroot 				goto send;
6387421Sroot 			}
6397421Sroot 			/*NOTREACHED*/
6407421Sroot 
6417421Sroot 		case SIG_HOLD:
6427421Sroot 		case SIG_IGN:
6437421Sroot 			/*
6447421Sroot 			 * Masking above should prevent us
6457421Sroot 			 * ever trying to take action on a held
6467421Sroot 			 * or ignored signal, unless process is traced.
6477421Sroot 			 */
6487421Sroot 			if ((p->p_flag&STRC) == 0)
6497421Sroot 				printf("issig\n");
6507421Sroot 			continue;
6517421Sroot 
6527421Sroot 		default:
6537421Sroot 			/*
6547421Sroot 			 * This signal has an action, let
6557421Sroot 			 * psig process it.
6567421Sroot 			 */
6577421Sroot 			goto send;
6587421Sroot 		}
6597421Sroot 		/*NOTREACHED*/
6607421Sroot 	}
6617421Sroot 	/*
6627421Sroot 	 * Didn't find a signal to send.
6637421Sroot 	 */
6647421Sroot 	p->p_cursig = 0;
6657421Sroot 	return (0);
6667421Sroot 
6677421Sroot send:
6687421Sroot 	/*
6697421Sroot 	 * Let psig process the signal.
6707421Sroot 	 */
6717421Sroot 	return (sig);
6727421Sroot }
6737421Sroot 
6747421Sroot /*
6757421Sroot  * Put the argument process into the stopped
67618331Skarels  * state and notify the parent via wakeup.
67718331Skarels  * Signals are handled elsewhere.
6787421Sroot  */
6797421Sroot stop(p)
6807421Sroot 	register struct proc *p;
6817421Sroot {
6827421Sroot 
6837421Sroot 	p->p_stat = SSTOP;
6847421Sroot 	p->p_flag &= ~SWTED;
6857421Sroot 	wakeup((caddr_t)p->p_pptr);
6867421Sroot }
6877421Sroot 
6887421Sroot /*
6897421Sroot  * Perform the action specified by
6907421Sroot  * the current signal.
6917421Sroot  * The usual sequence is:
6927421Sroot  *	if (issig())
6937421Sroot  *		psig();
6947421Sroot  * The signal bit has already been cleared by issig,
6957421Sroot  * and the current signal number stored in p->p_cursig.
6967421Sroot  */
6977421Sroot psig()
6987421Sroot {
69912882Ssam 	register struct proc *p = u.u_procp;
70012882Ssam 	register int sig = p->p_cursig;
70117153Sbloom 	int mask = sigmask(sig), returnmask;
7027421Sroot 	register int (*action)();
7037421Sroot 
70412882Ssam 	if (sig == 0)
7057421Sroot 		panic("psig");
70612882Ssam 	action = u.u_signal[sig];
7077421Sroot 	if (action != SIG_DFL) {
70817153Sbloom 		if (action == SIG_IGN || (p->p_sigmask & mask))
7097421Sroot 			panic("psig action");
7107421Sroot 		u.u_error = 0;
7117421Sroot 		/*
71212882Ssam 		 * Set the new mask value and also defer further
71312882Ssam 		 * occurences of this signal (unless we're simulating
71412882Ssam 		 * the old signal facilities).
71512882Ssam 		 *
71612882Ssam 		 * Special case: user has done a sigpause.  Here the
71712882Ssam 		 * current mask is not of interest, but rather the
71812882Ssam 		 * mask from before the sigpause is what we want restored
71912882Ssam 		 * after the signal processing is completed.
7207421Sroot 		 */
72117153Sbloom 		(void) splhigh();
72212882Ssam 		if (p->p_flag & SOUSIG) {
72312882Ssam 			if (sig != SIGILL && sig != SIGTRAP) {
72412882Ssam 				u.u_signal[sig] = SIG_DFL;
72517153Sbloom 				p->p_sigcatch &= ~mask;
72612882Ssam 			}
72717153Sbloom 			mask = 0;
7287421Sroot 		}
72912882Ssam 		if (p->p_flag & SOMASK) {
73012882Ssam 			returnmask = u.u_oldmask;
73112882Ssam 			p->p_flag &= ~SOMASK;
73212882Ssam 		} else
73312882Ssam 			returnmask = p->p_sigmask;
73417153Sbloom 		p->p_sigmask |= u.u_sigmask[sig] | mask;
73512882Ssam 		(void) spl0();
7368032Sroot 		u.u_ru.ru_nsignals++;
73712882Ssam 		sendsig(action, sig, returnmask);
73812882Ssam 		p->p_cursig = 0;
7397421Sroot 		return;
7407421Sroot 	}
7417421Sroot 	u.u_acflag |= AXSIG;
74212882Ssam 	switch (sig) {
7437421Sroot 
7447421Sroot 	case SIGILL:
7457421Sroot 	case SIGIOT:
7467421Sroot 	case SIGBUS:
7477421Sroot 	case SIGQUIT:
7487421Sroot 	case SIGTRAP:
7497421Sroot 	case SIGEMT:
7507421Sroot 	case SIGFPE:
7517421Sroot 	case SIGSEGV:
7527421Sroot 	case SIGSYS:
75312882Ssam 		u.u_arg[0] = sig;
754*37580Smckusick 		if (core() == 0)
75512882Ssam 			sig += 0200;
7567421Sroot 	}
75712882Ssam 	exit(sig);
7587421Sroot }
7597421Sroot 
7607421Sroot /*
7617421Sroot  * Create a core image on the file "core"
7627421Sroot  * If you are looking for protection glitches,
7637421Sroot  * there are probably a wealth of them here
7647421Sroot  * when this occurs to a suid command.
7657421Sroot  *
7667421Sroot  * It writes UPAGES block of the
7677421Sroot  * user.h area followed by the entire
7687421Sroot  * data+stack segments.
7697421Sroot  */
7707421Sroot core()
7717421Sroot {
772*37580Smckusick 	register struct vnode *vp, *dvp;
77316692Smckusick 	register struct nameidata *ndp = &u.u_nd;
774*37580Smckusick 	struct vattr vattr;
775*37580Smckusick 	int error;
7767421Sroot 
77712639Ssam 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
778*37580Smckusick 		return (EFAULT);
779*37580Smckusick 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
7808032Sroot 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
781*37580Smckusick 		return (EFAULT);
782*37580Smckusick 	if (u.u_procp->p_textp) {
783*37580Smckusick 		vop_lock(u.u_procp->p_textp->x_vptr);
784*37580Smckusick 		error = vn_access(u.u_procp->p_textp->x_vptr, VREAD, u.u_cred);
785*37580Smckusick 		vop_unlock(u.u_procp->p_textp->x_vptr);
786*37580Smckusick 		if (error)
787*37580Smckusick 			return (EFAULT);
788*37580Smckusick 	}
78916692Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
79016692Smckusick 	ndp->ni_dirp = "core";
791*37580Smckusick 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
792*37580Smckusick 		return (error);
793*37580Smckusick 	vp = ndp->ni_vp;
794*37580Smckusick 	if (vp->v_type != VREG ||
795*37580Smckusick 	    vop_getattr(vp, &vattr, u.u_cred) ||
796*37580Smckusick 	    vattr.va_nlink != 1) {
797*37580Smckusick 		error = EFAULT;
7987818Sroot 		goto out;
7997818Sroot 	}
80030290Ssam #ifdef MMAP
80130290Ssam 	{ register int fd;
80230290Ssam 	/* unmasp funky devices in the user's address space */
80330290Ssam 	for (fd = 0; fd < u.u_lastfile; fd++)
80430290Ssam 		if (u.u_ofile[fd] && (u.u_pofile[fd] & UF_MAPPED))
80530290Ssam 			munmapfd(fd);
80630290Ssam 	}
80730290Ssam #endif
808*37580Smckusick 	vattr_null(&vattr);
809*37580Smckusick 	vattr.va_size = 0;
810*37580Smckusick 	vop_setattr(vp, &vattr, u.u_cred);
8117818Sroot 	u.u_acflag |= ACORE;
812*37580Smckusick 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
813*37580Smckusick 	    UIO_SYSSPACE, IO_UNIT, (int *)0);
814*37580Smckusick 	if (error == 0)
815*37580Smckusick 		error = vn_rdwr(UIO_WRITE, vp,
8168967Sroot 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
817*37580Smckusick 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES),
818*37580Smckusick 		    UIO_USERSPACE, IO_UNIT, (int *)0);
819*37580Smckusick 	if (error == 0)
820*37580Smckusick 		error = vn_rdwr(UIO_WRITE, vp,
8218967Sroot 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
82226354Skarels 		    (int)ctob(u.u_ssize),
823*37580Smckusick 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize),
824*37580Smckusick 		    UIO_USERSPACE, IO_UNIT, (int *)0);
8257818Sroot out:
826*37580Smckusick 	if (vp)
827*37580Smckusick 		vrele(vp);
828*37580Smckusick 	return (error);
8297421Sroot }
830