xref: /csrg-svn/sys/kern/kern_sig.c (revision 42004)
123374Smckusick /*
237580Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337580Smckusick  * All rights reserved.
423374Smckusick  *
537580Smckusick  * Redistribution and use in source and binary forms are permitted
637580Smckusick  * provided that the above copyright notice and this paragraph are
737580Smckusick  * duplicated in all such forms and that any documentation,
837580Smckusick  * advertising materials, and other materials related to such
937580Smckusick  * distribution and use acknowledge that the software was developed
1037580Smckusick  * by the University of California, Berkeley.  The name of the
1137580Smckusick  * University may not be used to endorse or promote products derived
1237580Smckusick  * from this software without specific prior written permission.
1337580Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437580Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537580Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637580Smckusick  *
17*42004Smckusick  *	@(#)kern_sig.c	7.15 (Berkeley) 05/15/90
1823374Smckusick  */
197421Sroot 
2017092Sbloom #include "param.h"
2117092Sbloom #include "systm.h"
2239513Skarels #include "syscontext.h"	/* XXX */
2337580Smckusick #include "vnode.h"
2417092Sbloom #include "proc.h"
2517092Sbloom #include "timeb.h"
2617092Sbloom #include "times.h"
2717092Sbloom #include "buf.h"
2817092Sbloom #include "text.h"
2917092Sbloom #include "seg.h"
3017092Sbloom #include "vm.h"
3117092Sbloom #include "acct.h"
3217092Sbloom #include "uio.h"
3337580Smckusick #include "file.h"
3417092Sbloom #include "kernel.h"
3539513Skarels #include "wait.h"
3640807Smarc #include "ktrace.h"
377421Sroot 
3837581Smckusick #include "machine/reg.h"
3937581Smckusick #include "machine/pte.h"
4037581Smckusick #include "machine/psl.h"
4137581Smckusick #include "machine/mtpr.h"
4237581Smckusick 
4326276Skarels #define	stopsigmask	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
4426276Skarels 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
4539513Skarels #define defaultignmask	(sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \
4639513Skarels 			sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO))
4712951Ssam 
4817013Smckusick /*
4939513Skarels  * Can the current process (u.u_procp) send the specified signal
5039513Skarels  * to the specified process?
5117013Smckusick  */
5239513Skarels #define CANSIGNAL(p, signo) \
5339513Skarels 	(u.u_uid == 0 || \
5439513Skarels 	    u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \
5539513Skarels 	    u.u_procp->p_ruid == (p)->p_uid || \
5639513Skarels 	    u.u_procp->p_ruid == (p)->p_ruid || \
5739513Skarels 	    ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session))
5839513Skarels 
5939513Skarels sigaction()
607421Sroot {
6112951Ssam 	register struct a {
6212882Ssam 		int	signo;
6339513Skarels 		struct	sigaction *nsa;
6439513Skarels 		struct	sigaction *osa;
6512882Ssam 	} *uap = (struct a  *)u.u_ap;
6639513Skarels 	struct sigaction vec;
6739513Skarels 	register struct sigaction *sa;
6812882Ssam 	register int sig;
6939513Skarels 	int bit, error;
707421Sroot 
7112882Ssam 	sig = uap->signo;
7239513Skarels 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
7339513Skarels 		RETURN (EINVAL);
7439513Skarels 	sa = &vec;
7539513Skarels 	if (uap->osa) {
7639513Skarels 		sa->sa_handler = u.u_signal[sig];
7739513Skarels 		sa->sa_mask = u.u_sigmask[sig];
7818308Smckusick 		bit = sigmask(sig);
7939513Skarels 		sa->sa_flags = 0;
8018308Smckusick 		if ((u.u_sigonstack & bit) != 0)
8139513Skarels 			sa->sa_flags |= SA_ONSTACK;
8239513Skarels 		if ((u.u_sigintr & bit) == 0)
8339513Skarels 			sa->sa_flags |= SA_RESTART;
8439513Skarels 		if (u.u_procp->p_flag & SNOCLDSTOP)
8539513Skarels 			sa->sa_flags |= SA_NOCLDSTOP;
8639513Skarels 		if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
8739513Skarels 		    sizeof (vec)))
8839513Skarels 			RETURN (error);
8912951Ssam 	}
9039513Skarels 	if (uap->nsa) {
9139513Skarels 		if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
9239513Skarels 		    sizeof (vec)))
9339513Skarels 			RETURN (error);
9439513Skarels 		setsigvec(sig, sa);
9512951Ssam 	}
9639513Skarels 	RETURN (0);
977421Sroot }
987421Sroot 
9939513Skarels setsigvec(sig, sa)
10012951Ssam 	int sig;
10139513Skarels 	register struct sigaction *sa;
10212882Ssam {
10312882Ssam 	register struct proc *p;
10412951Ssam 	register int bit;
10512882Ssam 
10617153Sbloom 	bit = sigmask(sig);
10712882Ssam 	p = u.u_procp;
10812882Ssam 	/*
10912882Ssam 	 * Change setting atomically.
11012882Ssam 	 */
11117153Sbloom 	(void) splhigh();
11239513Skarels 	u.u_signal[sig] = sa->sa_handler;
11339513Skarels 	u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask;
11439513Skarels 	if ((sa->sa_flags & SA_RESTART) == 0)
11518308Smckusick 		u.u_sigintr |= bit;
11618308Smckusick 	else
11718308Smckusick 		u.u_sigintr &= ~bit;
11839513Skarels 	if (sa->sa_flags & SA_ONSTACK)
11912951Ssam 		u.u_sigonstack |= bit;
12012951Ssam 	else
12112951Ssam 		u.u_sigonstack &= ~bit;
12239513Skarels 	if (sig == SIGCHLD) {
12339513Skarels 		if (sa->sa_flags & SA_NOCLDSTOP)
12439513Skarels 			p->p_flag |= SNOCLDSTOP;
12539513Skarels 		else
12639513Skarels 			p->p_flag &= ~SNOCLDSTOP;
12739513Skarels 	}
12839513Skarels 	/*
12939513Skarels 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
13039513Skarels 	 * and for signals set to SIG_DFL where the default is to ignore.
13139513Skarels 	 * However, don't put SIGCONT in p_sigignore,
13239513Skarels 	 * as we have to restart the process.
13339513Skarels 	 */
13439513Skarels 	if (sa->sa_handler == SIG_IGN ||
13539513Skarels 	   (bit & defaultignmask && sa->sa_handler == SIG_DFL)) {
13612951Ssam 		p->p_sig &= ~bit;		/* never to be seen again */
13739513Skarels 		if (sig != SIGCONT)
13839513Skarels 			p->p_sigignore |= bit;	/* easier in psignal */
13912951Ssam 		p->p_sigcatch &= ~bit;
14012882Ssam 	} else {
14112951Ssam 		p->p_sigignore &= ~bit;
14239513Skarels 		if (sa->sa_handler == SIG_DFL)
14312951Ssam 			p->p_sigcatch &= ~bit;
14412882Ssam 		else
14512951Ssam 			p->p_sigcatch |= bit;
14612882Ssam 	}
14712882Ssam 	(void) spl0();
14812882Ssam }
14912882Ssam 
15039513Skarels /*
15139513Skarels  * Initialize signal state for process 0;
15239513Skarels  * set to ignore signals that are ignored by default.
15339513Skarels  */
15439513Skarels siginit(p)
15539513Skarels 	struct proc *p;
1567421Sroot {
15739513Skarels 
15839513Skarels 	p->p_sigignore = defaultignmask &~ sigmask(SIGCONT);
15939513Skarels }
16039513Skarels 
16139513Skarels /*
16239513Skarels  * Reset signals for an exec of the specified process.
16339513Skarels  */
16439513Skarels execsigs(p)
16539513Skarels 	register struct proc *p;
16639513Skarels {
16739513Skarels 	register int nc, mask;
16839513Skarels 
16939513Skarels 	/*
17039513Skarels 	 * Reset caught signals.  Held signals remain held
17139513Skarels 	 * through p_sigmask (unless they were caught,
17239513Skarels 	 * and are now ignored by default).
17339513Skarels 	 */
17439513Skarels 	while (p->p_sigcatch) {
17539513Skarels 		nc = ffs((long)p->p_sigcatch);
17639513Skarels 		mask = sigmask(nc);
17739513Skarels 		p->p_sigcatch &= ~mask;
17839513Skarels 		if (mask & defaultignmask) {
17939513Skarels 			if (nc != SIGCONT)
18039513Skarels 				p->p_sigignore |= mask;
18139513Skarels 			p->p_sig &= ~mask;
18239513Skarels 		}
18339513Skarels 		u.u_signal[nc] = SIG_DFL;
18439513Skarels 	}
18539513Skarels 	/*
18639513Skarels 	 * Reset stack state to the user stack.
18739513Skarels 	 * Clear set of signals caught on the signal stack.
18839513Skarels 	 */
18939513Skarels 	u.u_onstack = 0;
19039513Skarels 	u.u_sigsp = 0;
19139513Skarels 	u.u_sigonstack = 0;
19239513Skarels }
19339513Skarels 
19439513Skarels /*
19539513Skarels  * Manipulate signal mask.
19639513Skarels  * Note that we receive new mask, not pointer,
19739513Skarels  * and return old mask as return value;
19839513Skarels  * the library stub does the rest.
19939513Skarels  */
20039513Skarels sigprocmask()
20139513Skarels {
20212882Ssam 	struct a {
20339513Skarels 		int	how;
20439513Skarels 		sigset_t mask;
20539513Skarels 	} *uap = (struct a *)u.u_ap;
20639513Skarels 	register struct proc *p = u.u_procp;
20739513Skarels 	int error = 0;
20839513Skarels 
20939513Skarels 	u.u_r.r_val1 = p->p_sigmask;
21039513Skarels 	(void) splhigh();
21139513Skarels 
21239513Skarels 	switch (uap->how) {
21339513Skarels 	case SIG_BLOCK:
21439513Skarels 		p->p_sigmask |= uap->mask &~ sigcantmask;
21539513Skarels 		break;
21639513Skarels 
21739513Skarels 	case SIG_UNBLOCK:
21839513Skarels 		p->p_sigmask &= ~uap->mask;
21939513Skarels 		break;
22039513Skarels 
22139513Skarels 	case SIG_SETMASK:
22239513Skarels 		p->p_sigmask = uap->mask &~ sigcantmask;
22339513Skarels 		break;
22439513Skarels 
22539513Skarels 	default:
22639513Skarels 		error = EINVAL;
22739513Skarels 		break;
22839513Skarels 	}
22939513Skarels 	(void) spl0();
23039513Skarels 	RETURN (error);
23139513Skarels }
23239513Skarels 
23339513Skarels sigpending()
23439513Skarels {
23539513Skarels 
23639513Skarels 	u.u_r.r_val1 = u.u_procp->p_sig;
23739513Skarels 	RETURN (0);
23839513Skarels }
23939513Skarels 
24039513Skarels #ifdef COMPAT_43
24139513Skarels /*
24239513Skarels  * Generalized interface signal handler, 4.3-compatible.
24339513Skarels  */
24439513Skarels osigvec()
24539513Skarels {
24639513Skarels 	register struct a {
24739513Skarels 		int	signo;
24839513Skarels 		struct	sigvec *nsv;
24939513Skarels 		struct	sigvec *osv;
25039513Skarels 	} *uap = (struct a  *)u.u_ap;
25139513Skarels 	struct sigvec vec;
25239513Skarels 	register struct sigvec *sv;
25339513Skarels 	register int sig;
25439513Skarels 	int bit, error;
25539513Skarels 
25639513Skarels 	sig = uap->signo;
25739513Skarels 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
25839513Skarels 		RETURN (EINVAL);
25939513Skarels 	sv = &vec;
26039513Skarels 	if (uap->osv) {
26139513Skarels 		*(sig_t *)&sv->sv_handler = u.u_signal[sig];
26239513Skarels 		sv->sv_mask = u.u_sigmask[sig];
26339513Skarels 		bit = sigmask(sig);
26439513Skarels 		sv->sv_flags = 0;
26539513Skarels 		if ((u.u_sigonstack & bit) != 0)
26639513Skarels 			sv->sv_flags |= SV_ONSTACK;
26739513Skarels 		if ((u.u_sigintr & bit) != 0)
26839513Skarels 			sv->sv_flags |= SV_INTERRUPT;
26939513Skarels 		if (u.u_procp->p_flag & SNOCLDSTOP)
27039513Skarels 			sv->sv_flags |= SA_NOCLDSTOP;
27139513Skarels 		if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
27239513Skarels 		    sizeof (vec)))
27339513Skarels 			RETURN (error);
27439513Skarels 	}
27539513Skarels 	if (uap->nsv) {
27639513Skarels 		if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
27739513Skarels 		    sizeof (vec)))
27839513Skarels 			RETURN (error);
27939513Skarels 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
280*42004Smckusick 		setsigvec(sig, (struct sigaction *)sv);
28139513Skarels 	}
28239513Skarels 	RETURN (0);
28339513Skarels }
28439513Skarels 
28539513Skarels osigblock()
28639513Skarels {
28739513Skarels 	struct a {
28817153Sbloom 		int	mask;
28912882Ssam 	} *uap = (struct a *)u.u_ap;
29012951Ssam 	register struct proc *p = u.u_procp;
2917499Sroot 
29217153Sbloom 	(void) splhigh();
29312882Ssam 	u.u_r.r_val1 = p->p_sigmask;
29439513Skarels 	p->p_sigmask |= uap->mask &~ sigcantmask;
29512882Ssam 	(void) spl0();
29639513Skarels 	RETURN (0);
2977499Sroot }
2987499Sroot 
29939513Skarels osigsetmask()
3007499Sroot {
30112882Ssam 	struct a {
30217153Sbloom 		int	mask;
30312882Ssam 	} *uap = (struct a *)u.u_ap;
30412882Ssam 	register struct proc *p = u.u_procp;
3057499Sroot 
30617153Sbloom 	(void) splhigh();
30712882Ssam 	u.u_r.r_val1 = p->p_sigmask;
30839513Skarels 	p->p_sigmask = uap->mask &~ sigcantmask;
30912882Ssam 	(void) spl0();
31039513Skarels 	RETURN (0);
3117499Sroot }
31239513Skarels #endif
3137499Sroot 
31439513Skarels /*
31539513Skarels  * Suspend process until signal, providing mask to be set
31639513Skarels  * in the meantime.  Note nonstandard calling convention:
31739513Skarels  * libc stub passes mask, not pointer, to save a copyin.
31839513Skarels  */
31939513Skarels sigsuspend()
3207499Sroot {
32112882Ssam 	struct a {
32239513Skarels 		sigset_t mask;
32312882Ssam 	} *uap = (struct a *)u.u_ap;
32412882Ssam 	register struct proc *p = u.u_procp;
3257499Sroot 
32612882Ssam 	/*
32712882Ssam 	 * When returning from sigpause, we want
32812882Ssam 	 * the old mask to be restored after the
32912882Ssam 	 * signal handler has finished.  Thus, we
33012882Ssam 	 * save it here and mark the proc structure
33112882Ssam 	 * to indicate this (should be in u.).
33212882Ssam 	 */
33312882Ssam 	u.u_oldmask = p->p_sigmask;
33412882Ssam 	p->p_flag |= SOMASK;
33539513Skarels 	p->p_sigmask = uap->mask &~ sigcantmask;
33640807Smarc 	(void) tsleep((caddr_t)&u, PPAUSE | PCATCH, "pause", 0);
33740807Smarc 	/* always return EINTR rather than ERESTART... */
33840807Smarc 	RETURN (EINTR);
3397499Sroot }
3407499Sroot 
3417499Sroot sigstack()
3427499Sroot {
34312951Ssam 	register struct a {
34412951Ssam 		struct	sigstack *nss;
34512951Ssam 		struct	sigstack *oss;
34612882Ssam 	} *uap = (struct a *)u.u_ap;
34712951Ssam 	struct sigstack ss;
34839513Skarels 	int error = 0;
3497499Sroot 
35039513Skarels 	if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack,
35139513Skarels 	    (caddr_t)uap->oss, sizeof (struct sigstack))))
35239513Skarels 		RETURN (error);
35339513Skarels 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
35439513Skarels 	    sizeof (ss))) == 0)
35539513Skarels 		u.u_sigstack = ss;
35639513Skarels 	RETURN (error);
3577499Sroot }
3587499Sroot 
3598032Sroot kill()
3608032Sroot {
36112882Ssam 	register struct a {
36212882Ssam 		int	pid;
36312882Ssam 		int	signo;
36412882Ssam 	} *uap = (struct a *)u.u_ap;
36518336Smckusick 	register struct proc *p;
3668032Sroot 
36739513Skarels 	if ((unsigned) uap->signo >= NSIG)
36839513Skarels 		RETURN (EINVAL);
36918336Smckusick 	if (uap->pid > 0) {
37018336Smckusick 		/* kill single process */
37118336Smckusick 		p = pfind(uap->pid);
37239513Skarels 		if (p == 0)
37339513Skarels 			RETURN (ESRCH);
37439513Skarels 		if (!CANSIGNAL(p, uap->signo))
37539513Skarels 			RETURN (EPERM);
37639513Skarels 		if (uap->signo)
37718336Smckusick 			psignal(p, uap->signo);
37839513Skarels 		RETURN (0);
37918336Smckusick 	}
38018336Smckusick 	switch (uap->pid) {
38118336Smckusick 	case -1:		/* broadcast signal */
38239513Skarels 		RETURN (killpg1(uap->signo, 0, 1));
38318336Smckusick 	case 0:			/* signal own process group */
38439513Skarels 		RETURN (killpg1(uap->signo, 0, 0));
38518336Smckusick 	default:		/* negative explicit process group */
38639513Skarels 		RETURN (killpg1(uap->signo, -uap->pid, 0));
38718336Smckusick 	}
38839513Skarels 	/* NOTREACHED */
3898032Sroot }
3908032Sroot 
39139513Skarels #ifdef COMPAT_43
39239513Skarels okillpg()
3938032Sroot {
3949989Ssam 	register struct a {
39537581Smckusick 		int	pgid;
3969989Ssam 		int	signo;
3979989Ssam 	} *uap = (struct a *)u.u_ap;
3988032Sroot 
39939513Skarels 	if ((unsigned) uap->signo >= NSIG)
40039513Skarels 		RETURN (EINVAL);
40139513Skarels 	RETURN (killpg1(uap->signo, uap->pgid, 0));
4028032Sroot }
40339513Skarels #endif
4048032Sroot 
40537581Smckusick killpg1(signo, pgid, all)
40637581Smckusick 	int signo, pgid, all;
4079989Ssam {
4089989Ssam 	register struct proc *p;
40937581Smckusick 	struct pgrp *pgrp;
41039513Skarels 	int f = 0, error = ESRCH;
41137581Smckusick 
41237581Smckusick 	if (all)
41337581Smckusick 		/*
41437581Smckusick 		 * broadcast
4157421Sroot 		 */
41637581Smckusick 		for (p = allproc; p != NULL; p = p->p_nxt) {
41737581Smckusick 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
41839513Skarels 			    p == u.u_procp || !CANSIGNAL(p, signo))
41937581Smckusick 				continue;
42037581Smckusick 			f++;
42137581Smckusick 			if (signo)
42237581Smckusick 				psignal(p, signo);
42337581Smckusick 		}
42437581Smckusick 	else {
42537581Smckusick 		if (pgid == 0)
42637581Smckusick 			/*
42737581Smckusick 			 * zero pgid means send to my process group.
42837581Smckusick 			 */
42937581Smckusick 			pgrp = u.u_procp->p_pgrp;
43037581Smckusick 		else {
43137581Smckusick 			pgrp = pgfind(pgid);
43237581Smckusick 			if (pgrp == NULL)
43339513Skarels 				return (ESRCH);
43437581Smckusick 		}
43537581Smckusick 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
43639513Skarels 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
43739513Skarels 			    !CANSIGNAL(p, signo))
43837581Smckusick 				continue;
43937581Smckusick 			f++;
44037581Smckusick 			if (signo)
44137581Smckusick 				psignal(p, signo);
44218336Smckusick 		}
4437421Sroot 	}
44439513Skarels 	return (f ? 0 : error);
4457421Sroot }
4467421Sroot 
44740807Smarc /* XXX - to be removed, as soon as sockets are changed to operate on pgrps
4487421Sroot  * Send the specified signal to
44937581Smckusick  * all processes with 'pgid' as
4507421Sroot  * process group.
4517421Sroot  */
45237581Smckusick gsignal(pgid, sig)
4537421Sroot {
45439513Skarels 	struct pgrp *pgrp;
4557421Sroot 
45639513Skarels 	if (pgid && (pgrp = pgfind(pgid)))
45739513Skarels 		pgsignal(pgrp, sig);
4587421Sroot }
45940807Smarc /*
46040807Smarc  * Send sig to all all members of the process group
46140807Smarc  */
46237581Smckusick pgsignal(pgrp, sig)
46339513Skarels 	struct pgrp *pgrp;
46437581Smckusick {
46537581Smckusick 	register struct proc *p;
46637581Smckusick 
46740807Smarc 	if (pgrp)
46840807Smarc 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
46940807Smarc 			psignal(p, sig);
47037581Smckusick }
47137581Smckusick 
4727421Sroot /*
47339513Skarels  * Send a signal caused by a trap to the current process.
47439513Skarels  * If it will be caught immediately, deliver it with correct code.
47539513Skarels  * Otherwise, post it normally.
47639513Skarels  */
47739513Skarels trapsignal(sig, code)
47839513Skarels 	register int sig;
47939513Skarels 	unsigned code;
48039513Skarels {
48139513Skarels 	register struct proc *p = u.u_procp;
48239513Skarels 	int mask;
48339513Skarels 
48439513Skarels 	mask = sigmask(sig);
48539513Skarels 	if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
48639513Skarels 	    (p->p_sigmask & mask) == 0) {
48739513Skarels 		u.u_ru.ru_nsignals++;
48840807Smarc #ifdef KTRACE
48940807Smarc 		if (KTRPOINT(p, KTR_PSIG))
49040807Smarc 			ktrpsig(p->p_tracep, sig, u.u_signal[sig],
49140807Smarc 				p->p_sigmask, code);
49240807Smarc #endif
49339513Skarels 		sendsig(u.u_signal[sig], sig, p->p_sigmask, code);
49439513Skarels 		p->p_sigmask |= u.u_sigmask[sig] | mask;
49539513Skarels 	} else {
49639513Skarels 		u.u_arg[1] = code;	/* XXX for core dump/debugger */
49739513Skarels 		psignal(p, sig);
49839513Skarels 	}
49939513Skarels }
50039513Skarels 
50139513Skarels /*
50240807Smarc  * Send the specified signal to the specified process.
50340807Smarc  * Most signals do not do anything directly to a process;
50440807Smarc  * they set a flag that asks the process to do something to itself.
50540807Smarc  * Exceptions:
50640807Smarc  *   o When a stop signal is sent to a sleeping process that takes the default
50740807Smarc  *     action, the process is stopped without awakening it.
50840807Smarc  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
50940807Smarc  *     regardless of the signal action (eg, blocked or ignored).
51040807Smarc  * Other ignored signals are discarded immediately.
5117421Sroot  */
5127421Sroot psignal(p, sig)
5137421Sroot 	register struct proc *p;
5147421Sroot 	register int sig;
5157421Sroot {
5167421Sroot 	register int s;
51739513Skarels 	register sig_t action;
51817153Sbloom 	int mask;
5197421Sroot 
52039513Skarels 	if ((unsigned)sig >= NSIG || sig == 0)
52139513Skarels 		panic("psignal sig");
52217153Sbloom 	mask = sigmask(sig);
5237421Sroot 
5247421Sroot 	/*
5257421Sroot 	 * If proc is traced, always give parent a chance.
5267421Sroot 	 */
5277421Sroot 	if (p->p_flag & STRC)
5287421Sroot 		action = SIG_DFL;
5297421Sroot 	else {
5307421Sroot 		/*
53112882Ssam 		 * If the signal is being ignored,
53212882Ssam 		 * then we forget about it immediately.
53339513Skarels 		 * (Note: we don't set SIGCONT in p_sigignore,
53439513Skarels 		 * and if it is set to SIG_IGN,
53539513Skarels 		 * action will be SIG_DFL here.)
5367421Sroot 		 */
53717153Sbloom 		if (p->p_sigignore & mask)
5387421Sroot 			return;
53917153Sbloom 		if (p->p_sigmask & mask)
54012882Ssam 			action = SIG_HOLD;
54117153Sbloom 		else if (p->p_sigcatch & mask)
54212882Ssam 			action = SIG_CATCH;
54340807Smarc 		else {
54440807Smarc 			if (p->p_pgrp->pg_jobc == 0 && (sig == SIGTTIN ||
54540807Smarc 			    sig == SIGTTOU || sig == SIGTSTP))
54640807Smarc 				return;
54712882Ssam 			action = SIG_DFL;
54840807Smarc 		}
5497421Sroot 	}
55039513Skarels 	switch (sig) {
5517421Sroot 
55239513Skarels 	case SIGTERM:
55339513Skarels 		if ((p->p_flag&STRC) || action != SIG_DFL)
5547421Sroot 			break;
55539513Skarels 		/* FALLTHROUGH */
5567421Sroot 
55739513Skarels 	case SIGKILL:
55839513Skarels 		if (p->p_nice > NZERO)
55939513Skarels 			p->p_nice = NZERO;
56039513Skarels 		break;
5617421Sroot 
56239513Skarels 	case SIGCONT:
56339513Skarels 		p->p_sig &= ~stopsigmask;
56439513Skarels 		break;
56539513Skarels 
56639513Skarels 	case SIGTSTP:
56739513Skarels 	case SIGTTIN:
56839513Skarels 	case SIGTTOU:
56939513Skarels 	case SIGSTOP:
57039513Skarels 		p->p_sig &= ~sigmask(SIGCONT);
57139513Skarels 		break;
5727421Sroot 	}
57339513Skarels 	p->p_sig |= mask;
57439513Skarels 
5757421Sroot 	/*
57639513Skarels 	 * Defer further processing for signals which are held,
57739513Skarels 	 * except that stopped processes must be continued by SIGCONT.
5787421Sroot 	 */
57939513Skarels 	if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP))
5807421Sroot 		return;
58117153Sbloom 	s = splhigh();
5827421Sroot 	switch (p->p_stat) {
5837421Sroot 
5847421Sroot 	case SSLEEP:
5857421Sroot 		/*
58640807Smarc 		 * If process is sleeping uninterruptibly
5877421Sroot 		 * we can't interrupt the sleep... the signal will
5887421Sroot 		 * be noticed when the process returns through
5897421Sroot 		 * trap() or syscall().
5907421Sroot 		 */
59140807Smarc 		if ((p->p_flag & SSINTR) == 0)
5927421Sroot 			goto out;
5937421Sroot 		/*
5947421Sroot 		 * Process is sleeping and traced... make it runnable
5957421Sroot 		 * so it can discover the signal in issig() and stop
5967421Sroot 		 * for the parent.
5977421Sroot 		 */
5987421Sroot 		if (p->p_flag&STRC)
5997421Sroot 			goto run;
60039513Skarels 		/*
60139513Skarels 		 * When a sleeping process receives a stop
60239513Skarels 		 * signal, process immediately if possible.
60339513Skarels 		 * All other (caught or default) signals
60439513Skarels 		 * cause the process to run.
60539513Skarels 		 */
60639513Skarels 		if (mask & stopsigmask) {
6077421Sroot 			if (action != SIG_DFL)
60839513Skarels 				goto runfast;
6097421Sroot 			/*
6107421Sroot 			 * If a child in vfork(), stopping could
6117421Sroot 			 * cause deadlock.
6127421Sroot 			 */
6137421Sroot 			if (p->p_flag&SVFORK)
6147421Sroot 				goto out;
61517153Sbloom 			p->p_sig &= ~mask;
6167421Sroot 			p->p_cursig = sig;
61739513Skarels 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
61839513Skarels 				psignal(p->p_pptr, SIGCHLD);
6197421Sroot 			stop(p);
6207421Sroot 			goto out;
62139513Skarels 		} else
62239513Skarels 			goto runfast;
6237421Sroot 		/*NOTREACHED*/
6247421Sroot 
6257421Sroot 	case SSTOP:
6267421Sroot 		/*
6277421Sroot 		 * If traced process is already stopped,
6287421Sroot 		 * then no further action is necessary.
6297421Sroot 		 */
6307421Sroot 		if (p->p_flag&STRC)
6317421Sroot 			goto out;
6327421Sroot 		switch (sig) {
6337421Sroot 
6347421Sroot 		case SIGKILL:
6357421Sroot 			/*
6367421Sroot 			 * Kill signal always sets processes running.
6377421Sroot 			 */
63839513Skarels 			goto runfast;
6397421Sroot 
6407421Sroot 		case SIGCONT:
6417421Sroot 			/*
64239513Skarels 			 * If SIGCONT is default (or ignored), we continue
64339513Skarels 			 * the process but don't leave the signal in p_sig,
64439513Skarels 			 * as it has no further action.  If SIGCONT is held,
64539513Skarels 			 * continue the process and leave the signal in p_sig.
6467421Sroot 			 * If the process catches SIGCONT, let it handle
6477421Sroot 			 * the signal itself.  If it isn't waiting on
6487421Sroot 			 * an event, then it goes back to run state.
6497421Sroot 			 * Otherwise, process goes back to sleep state.
6507421Sroot 			 */
65139513Skarels 			p->p_cursig = 0;	/* ??? XXX */
65239513Skarels 			if (action == SIG_DFL)
65339513Skarels 				p->p_sig &= ~mask;
65439513Skarels 			if (action == SIG_CATCH)
65539513Skarels 				goto runfast;
65639513Skarels 			if (p->p_wchan == 0)
6577421Sroot 				goto run;
6587421Sroot 			p->p_stat = SSLEEP;
6597421Sroot 			goto out;
6607421Sroot 
6617421Sroot 		case SIGSTOP:
6627421Sroot 		case SIGTSTP:
6637421Sroot 		case SIGTTIN:
6647421Sroot 		case SIGTTOU:
6657421Sroot 			/*
6667421Sroot 			 * Already stopped, don't need to stop again.
6677421Sroot 			 * (If we did the shell could get confused.)
6687421Sroot 			 */
66917153Sbloom 			p->p_sig &= ~mask;		/* take it away */
6707421Sroot 			goto out;
6717421Sroot 
6727421Sroot 		default:
6737421Sroot 			/*
6747421Sroot 			 * If process is sleeping interruptibly, then
67540807Smarc 			 * simulate a wakeup so that when it is continued,
67640807Smarc 			 * it will be made runnable and can look at the signal.
67740807Smarc 			 * But don't setrun the process, leave it stopped.
6787421Sroot 			 */
67940807Smarc 			if (p->p_wchan && p->p_flag & SSINTR)
6807421Sroot 				unsleep(p);
6817421Sroot 			goto out;
6827421Sroot 		}
6837421Sroot 		/*NOTREACHED*/
6847421Sroot 
6857421Sroot 	default:
6867421Sroot 		/*
6877421Sroot 		 * SRUN, SIDL, SZOMB do nothing with the signal,
6887421Sroot 		 * other than kicking ourselves if we are running.
6897421Sroot 		 * It will either never be noticed, or noticed very soon.
6907421Sroot 		 */
6917421Sroot 		if (p == u.u_procp && !noproc)
6927421Sroot 			aston();
6937421Sroot 		goto out;
6947421Sroot 	}
6957421Sroot 	/*NOTREACHED*/
69639513Skarels 
69739513Skarels runfast:
6987421Sroot 	/*
6997421Sroot 	 * Raise priority to at least PUSER.
7007421Sroot 	 */
7017421Sroot 	if (p->p_pri > PUSER)
70217399Skarels 		p->p_pri = PUSER;
70339513Skarels run:
7047421Sroot 	setrun(p);
7057421Sroot out:
7067421Sroot 	splx(s);
7077421Sroot }
7087421Sroot 
7097421Sroot /*
71040807Smarc  * If the current process has a signal to process (should be caught
71140807Smarc  * or cause termination, should interrupt current syscall),
71240807Smarc  * return the signal number.  Stop signals with default action
71340807Smarc  * are processed immediately, then cleared; they aren't returned.
7147421Sroot  * This is asked at least once each time a process enters the
7157421Sroot  * system (though this can usually be done without actually
7167421Sroot  * calling issig by checking the pending signal masks.)
7177421Sroot  */
7187421Sroot issig()
7197421Sroot {
7207421Sroot 	register struct proc *p;
72139513Skarels 	register int sig, mask;
7227421Sroot 
7237421Sroot 	p = u.u_procp;
7247421Sroot 	for (;;) {
72539513Skarels 		mask = p->p_sig &~ p->p_sigmask;
7267421Sroot 		if (p->p_flag&SVFORK)
72739513Skarels 			mask &= ~stopsigmask;
72840807Smarc 		if (mask == 0)	 	/* no signal to send */
72940807Smarc 			return (0);
73039513Skarels 		sig = ffs((long)mask);
73117153Sbloom 		mask = sigmask(sig);
73240807Smarc 		/*
73340807Smarc 		 * We should see pending but ignored signals
73440807Smarc 		 * only if STRC was on when they were posted.
73540807Smarc 		 */
73640807Smarc 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
73740807Smarc 			p->p_sig &= ~mask;
73840807Smarc 			continue;
73940807Smarc 		}
74012882Ssam 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
7417421Sroot 			/*
7427421Sroot 			 * If traced, always stop, and stay
7437421Sroot 			 * stopped until released by the parent.
7447421Sroot 			 */
74540807Smarc 			p->p_cursig = sig;
74618331Skarels 			psignal(p->p_pptr, SIGCHLD);
7477421Sroot 			do {
7487421Sroot 				stop(p);
7497421Sroot 				swtch();
7507421Sroot 			} while (!procxmt() && p->p_flag&STRC);
7517421Sroot 
7527421Sroot 			/*
75314782Ssam 			 * If the traced bit got turned off,
75440807Smarc 			 * go back up to the top to rescan signals.
75514782Ssam 			 * This ensures that p_sig* and u_signal are consistent.
7567421Sroot 			 */
75740807Smarc 			if ((p->p_flag&STRC) == 0)
7587421Sroot 				continue;
7597421Sroot 
7607421Sroot 			/*
7617421Sroot 			 * If parent wants us to take the signal,
7627421Sroot 			 * then it will leave it in p->p_cursig;
7637421Sroot 			 * otherwise we just look for signals again.
7647421Sroot 			 */
76540807Smarc 			p->p_sig &= ~mask;	/* clear the old signal */
7667421Sroot 			sig = p->p_cursig;
7677421Sroot 			if (sig == 0)
7687421Sroot 				continue;
76914782Ssam 
77014782Ssam 			/*
77140807Smarc 			 * Put the new signal into p_sig.
77240807Smarc 			 * If signal is being masked,
77340807Smarc 			 * look for other signals.
77414782Ssam 			 */
77517153Sbloom 			mask = sigmask(sig);
77640807Smarc 			p->p_sig |= mask;
77740807Smarc 			if (p->p_sigmask & mask)
77814782Ssam 				continue;
7797421Sroot 		}
78040807Smarc 
78140807Smarc 		/*
78240807Smarc 		 * Decide whether the signal should be returned.
78340807Smarc 		 * Return the signal's number, or fall through
78440807Smarc 		 * to clear it from the pending mask.
78540807Smarc 		 */
78624901Skarels 		switch ((int)u.u_signal[sig]) {
7877421Sroot 
7887421Sroot 		case SIG_DFL:
7897421Sroot 			/*
7907421Sroot 			 * Don't take default actions on system processes.
7917421Sroot 			 */
7927421Sroot 			if (p->p_ppid == 0)
79340807Smarc 				break;		/* == ignore */
79440807Smarc 			/*
79540807Smarc 			 * If there is a pending stop signal to process
79640807Smarc 			 * with default action, stop here,
79740807Smarc 			 * then clear the signal.
79840807Smarc 			 */
79939513Skarels 			if (mask & stopsigmask) {
8007421Sroot 				if (p->p_flag&STRC)
80140807Smarc 					break;	/* == ignore */
80240807Smarc 				p->p_cursig = sig;
8037421Sroot 				stop(p);
80439513Skarels 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
80539513Skarels 					psignal(p->p_pptr, SIGCHLD);
8067421Sroot 				swtch();
80740807Smarc 				break;
80839513Skarels 			} else if (mask & defaultignmask) {
8097421Sroot 				/*
81039513Skarels 				 * Except for SIGCONT, shouldn't get here.
81139513Skarels 				 * Default action is to ignore; drop it.
8127421Sroot 				 */
81340807Smarc 				break;		/* == ignore */
81439513Skarels 			} else
81540807Smarc 				return (sig);
8167421Sroot 			/*NOTREACHED*/
8177421Sroot 
8187421Sroot 		case SIG_IGN:
8197421Sroot 			/*
82039513Skarels 			 * Masking above should prevent us ever trying
82139513Skarels 			 * to take action on an ignored signal other
82239513Skarels 			 * than SIGCONT, unless process is traced.
8237421Sroot 			 */
82439513Skarels 			if (sig != SIGCONT && (p->p_flag&STRC) == 0)
8257421Sroot 				printf("issig\n");
82640807Smarc 			break;		/* == ignore */
8277421Sroot 
8287421Sroot 		default:
8297421Sroot 			/*
8307421Sroot 			 * This signal has an action, let
8317421Sroot 			 * psig process it.
8327421Sroot 			 */
83340807Smarc 			return (sig);
8347421Sroot 		}
83540807Smarc 		p->p_sig &= ~mask;		/* take the signal! */
8367421Sroot 	}
83740807Smarc 	/* NOTREACHED */
8387421Sroot }
8397421Sroot 
8407421Sroot /*
8417421Sroot  * Put the argument process into the stopped
84218331Skarels  * state and notify the parent via wakeup.
84318331Skarels  * Signals are handled elsewhere.
84440807Smarc  * The process must not be on the run queue.
8457421Sroot  */
8467421Sroot stop(p)
8477421Sroot 	register struct proc *p;
8487421Sroot {
8497421Sroot 
8507421Sroot 	p->p_stat = SSTOP;
8517421Sroot 	p->p_flag &= ~SWTED;
8527421Sroot 	wakeup((caddr_t)p->p_pptr);
8537421Sroot }
8547421Sroot 
8557421Sroot /*
85640807Smarc  * Perform the action specified by the current signal.
8577421Sroot  * The usual sequence is:
85840807Smarc  *	if (sig = CURSIG(p))
85940807Smarc  *		psig(sig);
8607421Sroot  */
86140807Smarc psig(sig)
86240807Smarc 	register int sig;
8637421Sroot {
86412882Ssam 	register struct proc *p = u.u_procp;
86539513Skarels 	int mask, returnmask;
86639513Skarels 	register sig_t action;
8677421Sroot 
86839513Skarels 	do {
86940807Smarc #ifdef DIAGNOSTIC
87039513Skarels 		if (sig == 0)
87139513Skarels 			panic("psig");
87240807Smarc #endif
87340807Smarc 		mask = sigmask(sig);
87440807Smarc 		p->p_sig &= ~mask;
87539513Skarels 		action = u.u_signal[sig];
87640807Smarc #ifdef KTRACE
87740807Smarc 		if (KTRPOINT(p, KTR_PSIG))
87840807Smarc 			ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ?
87940807Smarc 				u.u_oldmask : p->p_sigmask, 0);
88040807Smarc #endif
88139513Skarels 		if (action != SIG_DFL) {
88239513Skarels #ifdef DIAGNOSTIC
88339513Skarels 			if (action == SIG_IGN || (p->p_sigmask & mask))
88439513Skarels 				panic("psig action");
88539513Skarels #endif
88639513Skarels 			u.u_error = 0;
88739513Skarels 			/*
88839513Skarels 			 * Set the new mask value and also defer further
88939513Skarels 			 * occurences of this signal.
89039513Skarels 			 *
89139513Skarels 			 * Special case: user has done a sigpause.  Here the
89239513Skarels 			 * current mask is not of interest, but rather the
89339513Skarels 			 * mask from before the sigpause is what we want
89439513Skarels 			 * restored after the signal processing is completed.
89539513Skarels 			 */
89639513Skarels 			(void) splhigh();
89739513Skarels 			if (p->p_flag & SOMASK) {
89839513Skarels 				returnmask = u.u_oldmask;
89939513Skarels 				p->p_flag &= ~SOMASK;
90039513Skarels 			} else
90139513Skarels 				returnmask = p->p_sigmask;
90239513Skarels 			p->p_sigmask |= u.u_sigmask[sig] | mask;
90339513Skarels 			(void) spl0();
90439513Skarels 			u.u_ru.ru_nsignals++;
90539513Skarels 			sendsig(action, sig, returnmask, 0);
90639513Skarels 			continue;
9077421Sroot 		}
90839513Skarels 		u.u_acflag |= AXSIG;
90939513Skarels 		switch (sig) {
9107421Sroot 
91139513Skarels 		case SIGILL:
91239513Skarels 		case SIGIOT:
91339513Skarels 		case SIGBUS:
91439513Skarels 		case SIGQUIT:
91539513Skarels 		case SIGTRAP:
91639513Skarels 		case SIGEMT:
91739513Skarels 		case SIGFPE:
91839513Skarels 		case SIGSEGV:
91939513Skarels 		case SIGSYS:
92039513Skarels 			u.u_arg[0] = sig;
92139513Skarels 			if (core() == 0)
92239513Skarels 				sig |= WCOREFLAG;
92339513Skarels 		}
92439513Skarels 		exit(W_EXITCODE(0, sig));
92539513Skarels 		/* NOTREACHED */
92640807Smarc 	} while (sig = CURSIG(p));
9277421Sroot }
9287421Sroot 
9297421Sroot /*
93039513Skarels  * Create a core image on the file "core".
9317421Sroot  * It writes UPAGES block of the
9327421Sroot  * user.h area followed by the entire
9337421Sroot  * data+stack segments.
9347421Sroot  */
9357421Sroot core()
9367421Sroot {
93737728Smckusick 	register struct vnode *vp;
93839513Skarels 	register struct proc *p = u.u_procp;
93916692Smckusick 	register struct nameidata *ndp = &u.u_nd;
94037580Smckusick 	struct vattr vattr;
94137580Smckusick 	int error;
9427421Sroot 
94339513Skarels 	if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
94437580Smckusick 		return (EFAULT);
94537580Smckusick 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
9468032Sroot 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
94737580Smckusick 		return (EFAULT);
94839513Skarels 	if (p->p_textp) {
94939513Skarels 		VOP_LOCK(p->p_textp->x_vptr);
95039513Skarels 		error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
95139513Skarels 		VOP_UNLOCK(p->p_textp->x_vptr);
95237580Smckusick 		if (error)
95337580Smckusick 			return (EFAULT);
95437580Smckusick 	}
95516692Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
95616692Smckusick 	ndp->ni_dirp = "core";
95737580Smckusick 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
95837580Smckusick 		return (error);
95937580Smckusick 	vp = ndp->ni_vp;
96038394Smckusick 	VOP_LOCK(vp);
96137580Smckusick 	if (vp->v_type != VREG ||
96237728Smckusick 	    VOP_GETATTR(vp, &vattr, u.u_cred) ||
96337580Smckusick 	    vattr.va_nlink != 1) {
96438394Smckusick 		vput(vp);
96538394Smckusick 		return (EFAULT);
9667818Sroot 	}
967*42004Smckusick #ifdef MAPMEM
968*42004Smckusick 	mmcore();
96930290Ssam #endif
97041362Smckusick 	VATTR_NULL(&vattr);
97137580Smckusick 	vattr.va_size = 0;
97237728Smckusick 	VOP_SETATTR(vp, &vattr, u.u_cred);
9737818Sroot 	u.u_acflag |= ACORE;
974*42004Smckusick #ifdef HPUXCOMPAT
975*42004Smckusick 	/*
976*42004Smckusick 	 * BLETCH!  If we loaded from an HPUX format binary file
977*42004Smckusick 	 * we have to dump an HPUX style user struct so that the
978*42004Smckusick 	 * HPUX debuggers can grok it.
979*42004Smckusick 	 */
980*42004Smckusick 	if (u.u_pcb.pcb_flags & PCB_HPUXBIN)
981*42004Smckusick 		error = hpuxdumpu(vp, ndp->ni_cred);
982*42004Smckusick 	else
983*42004Smckusick #endif
98437580Smckusick 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
98538394Smckusick 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
98637580Smckusick 	if (error == 0)
98737580Smckusick 		error = vn_rdwr(UIO_WRITE, vp,
98839513Skarels 		    (caddr_t)ctob(dptov(p, 0)),
98938394Smckusick 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
99038394Smckusick 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
99137580Smckusick 	if (error == 0)
99237580Smckusick 		error = vn_rdwr(UIO_WRITE, vp,
99339513Skarels 		    (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
99426354Skarels 		    (int)ctob(u.u_ssize),
99538394Smckusick 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
99638394Smckusick 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
99738394Smckusick 	vput(vp);
99837580Smckusick 	return (error);
9997421Sroot }
100039513Skarels 
100139513Skarels /*
100239513Skarels  * Nonexistent system call-- signal process (may want to handle it).
100339513Skarels  * Flag error in case process won't see signal immediately (blocked or ignored).
100439513Skarels  */
100539513Skarels nosys()
100639513Skarels {
100739513Skarels 
100839513Skarels 	psignal(u.u_procp, SIGSYS);
100939513Skarels 	u.u_error = EINVAL;
101039513Skarels }
1011