xref: /csrg-svn/sys/kern/kern_sig.c (revision 41362)
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*41362Smckusick  *	@(#)kern_sig.c	7.13 (Berkeley) 05/03/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 "mount.h"
2917092Sbloom #include "text.h"
3017092Sbloom #include "seg.h"
3117092Sbloom #include "vm.h"
3217092Sbloom #include "acct.h"
3317092Sbloom #include "uio.h"
3437580Smckusick #include "file.h"
3517092Sbloom #include "kernel.h"
3639513Skarels #include "wait.h"
3740807Smarc #include "ktrace.h"
387421Sroot 
3937581Smckusick #include "machine/reg.h"
4037581Smckusick #include "machine/pte.h"
4137581Smckusick #include "machine/psl.h"
4237581Smckusick #include "machine/mtpr.h"
4337581Smckusick 
4426276Skarels #define	stopsigmask	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
4526276Skarels 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
4639513Skarels #define defaultignmask	(sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \
4739513Skarels 			sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO))
4812951Ssam 
4917013Smckusick /*
5039513Skarels  * Can the current process (u.u_procp) send the specified signal
5139513Skarels  * to the specified process?
5217013Smckusick  */
5339513Skarels #define CANSIGNAL(p, signo) \
5439513Skarels 	(u.u_uid == 0 || \
5539513Skarels 	    u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \
5639513Skarels 	    u.u_procp->p_ruid == (p)->p_uid || \
5739513Skarels 	    u.u_procp->p_ruid == (p)->p_ruid || \
5839513Skarels 	    ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session))
5939513Skarels 
6039513Skarels sigaction()
617421Sroot {
6212951Ssam 	register struct a {
6312882Ssam 		int	signo;
6439513Skarels 		struct	sigaction *nsa;
6539513Skarels 		struct	sigaction *osa;
6612882Ssam 	} *uap = (struct a  *)u.u_ap;
6739513Skarels 	struct sigaction vec;
6839513Skarels 	register struct sigaction *sa;
6912882Ssam 	register int sig;
7039513Skarels 	int bit, error;
717421Sroot 
7212882Ssam 	sig = uap->signo;
7339513Skarels 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
7439513Skarels 		RETURN (EINVAL);
7539513Skarels 	sa = &vec;
7639513Skarels 	if (uap->osa) {
7739513Skarels 		sa->sa_handler = u.u_signal[sig];
7839513Skarels 		sa->sa_mask = u.u_sigmask[sig];
7918308Smckusick 		bit = sigmask(sig);
8039513Skarels 		sa->sa_flags = 0;
8118308Smckusick 		if ((u.u_sigonstack & bit) != 0)
8239513Skarels 			sa->sa_flags |= SA_ONSTACK;
8339513Skarels 		if ((u.u_sigintr & bit) == 0)
8439513Skarels 			sa->sa_flags |= SA_RESTART;
8539513Skarels 		if (u.u_procp->p_flag & SNOCLDSTOP)
8639513Skarels 			sa->sa_flags |= SA_NOCLDSTOP;
8739513Skarels 		if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
8839513Skarels 		    sizeof (vec)))
8939513Skarels 			RETURN (error);
9012951Ssam 	}
9139513Skarels 	if (uap->nsa) {
9239513Skarels 		if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
9339513Skarels 		    sizeof (vec)))
9439513Skarels 			RETURN (error);
9539513Skarels 		setsigvec(sig, sa);
9612951Ssam 	}
9739513Skarels 	RETURN (0);
987421Sroot }
997421Sroot 
10039513Skarels setsigvec(sig, sa)
10112951Ssam 	int sig;
10239513Skarels 	register struct sigaction *sa;
10312882Ssam {
10412882Ssam 	register struct proc *p;
10512951Ssam 	register int bit;
10612882Ssam 
10717153Sbloom 	bit = sigmask(sig);
10812882Ssam 	p = u.u_procp;
10912882Ssam 	/*
11012882Ssam 	 * Change setting atomically.
11112882Ssam 	 */
11217153Sbloom 	(void) splhigh();
11339513Skarels 	u.u_signal[sig] = sa->sa_handler;
11439513Skarels 	u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask;
11539513Skarels 	if ((sa->sa_flags & SA_RESTART) == 0)
11618308Smckusick 		u.u_sigintr |= bit;
11718308Smckusick 	else
11818308Smckusick 		u.u_sigintr &= ~bit;
11939513Skarels 	if (sa->sa_flags & SA_ONSTACK)
12012951Ssam 		u.u_sigonstack |= bit;
12112951Ssam 	else
12212951Ssam 		u.u_sigonstack &= ~bit;
12339513Skarels 	if (sig == SIGCHLD) {
12439513Skarels 		if (sa->sa_flags & SA_NOCLDSTOP)
12539513Skarels 			p->p_flag |= SNOCLDSTOP;
12639513Skarels 		else
12739513Skarels 			p->p_flag &= ~SNOCLDSTOP;
12839513Skarels 	}
12939513Skarels 	/*
13039513Skarels 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
13139513Skarels 	 * and for signals set to SIG_DFL where the default is to ignore.
13239513Skarels 	 * However, don't put SIGCONT in p_sigignore,
13339513Skarels 	 * as we have to restart the process.
13439513Skarels 	 */
13539513Skarels 	if (sa->sa_handler == SIG_IGN ||
13639513Skarels 	   (bit & defaultignmask && sa->sa_handler == SIG_DFL)) {
13712951Ssam 		p->p_sig &= ~bit;		/* never to be seen again */
13839513Skarels 		if (sig != SIGCONT)
13939513Skarels 			p->p_sigignore |= bit;	/* easier in psignal */
14012951Ssam 		p->p_sigcatch &= ~bit;
14112882Ssam 	} else {
14212951Ssam 		p->p_sigignore &= ~bit;
14339513Skarels 		if (sa->sa_handler == SIG_DFL)
14412951Ssam 			p->p_sigcatch &= ~bit;
14512882Ssam 		else
14612951Ssam 			p->p_sigcatch |= bit;
14712882Ssam 	}
14812882Ssam 	(void) spl0();
14912882Ssam }
15012882Ssam 
15139513Skarels /*
15239513Skarels  * Initialize signal state for process 0;
15339513Skarels  * set to ignore signals that are ignored by default.
15439513Skarels  */
15539513Skarels siginit(p)
15639513Skarels 	struct proc *p;
1577421Sroot {
15839513Skarels 
15939513Skarels 	p->p_sigignore = defaultignmask &~ sigmask(SIGCONT);
16039513Skarels }
16139513Skarels 
16239513Skarels /*
16339513Skarels  * Reset signals for an exec of the specified process.
16439513Skarels  */
16539513Skarels execsigs(p)
16639513Skarels 	register struct proc *p;
16739513Skarels {
16839513Skarels 	register int nc, mask;
16939513Skarels 
17039513Skarels 	/*
17139513Skarels 	 * Reset caught signals.  Held signals remain held
17239513Skarels 	 * through p_sigmask (unless they were caught,
17339513Skarels 	 * and are now ignored by default).
17439513Skarels 	 */
17539513Skarels 	while (p->p_sigcatch) {
17639513Skarels 		nc = ffs((long)p->p_sigcatch);
17739513Skarels 		mask = sigmask(nc);
17839513Skarels 		p->p_sigcatch &= ~mask;
17939513Skarels 		if (mask & defaultignmask) {
18039513Skarels 			if (nc != SIGCONT)
18139513Skarels 				p->p_sigignore |= mask;
18239513Skarels 			p->p_sig &= ~mask;
18339513Skarels 		}
18439513Skarels 		u.u_signal[nc] = SIG_DFL;
18539513Skarels 	}
18639513Skarels 	/*
18739513Skarels 	 * Reset stack state to the user stack.
18839513Skarels 	 * Clear set of signals caught on the signal stack.
18939513Skarels 	 */
19039513Skarels 	u.u_onstack = 0;
19139513Skarels 	u.u_sigsp = 0;
19239513Skarels 	u.u_sigonstack = 0;
19339513Skarels }
19439513Skarels 
19539513Skarels /*
19639513Skarels  * Manipulate signal mask.
19739513Skarels  * Note that we receive new mask, not pointer,
19839513Skarels  * and return old mask as return value;
19939513Skarels  * the library stub does the rest.
20039513Skarels  */
20139513Skarels sigprocmask()
20239513Skarels {
20312882Ssam 	struct a {
20439513Skarels 		int	how;
20539513Skarels 		sigset_t mask;
20639513Skarels 	} *uap = (struct a *)u.u_ap;
20739513Skarels 	register struct proc *p = u.u_procp;
20839513Skarels 	int error = 0;
20939513Skarels 
21039513Skarels 	u.u_r.r_val1 = p->p_sigmask;
21139513Skarels 	(void) splhigh();
21239513Skarels 
21339513Skarels 	switch (uap->how) {
21439513Skarels 	case SIG_BLOCK:
21539513Skarels 		p->p_sigmask |= uap->mask &~ sigcantmask;
21639513Skarels 		break;
21739513Skarels 
21839513Skarels 	case SIG_UNBLOCK:
21939513Skarels 		p->p_sigmask &= ~uap->mask;
22039513Skarels 		break;
22139513Skarels 
22239513Skarels 	case SIG_SETMASK:
22339513Skarels 		p->p_sigmask = uap->mask &~ sigcantmask;
22439513Skarels 		break;
22539513Skarels 
22639513Skarels 	default:
22739513Skarels 		error = EINVAL;
22839513Skarels 		break;
22939513Skarels 	}
23039513Skarels 	(void) spl0();
23139513Skarels 	RETURN (error);
23239513Skarels }
23339513Skarels 
23439513Skarels sigpending()
23539513Skarels {
23639513Skarels 
23739513Skarels 	u.u_r.r_val1 = u.u_procp->p_sig;
23839513Skarels 	RETURN (0);
23939513Skarels }
24039513Skarels 
24139513Skarels #ifdef COMPAT_43
24239513Skarels /*
24339513Skarels  * Generalized interface signal handler, 4.3-compatible.
24439513Skarels  */
24539513Skarels osigvec()
24639513Skarels {
24739513Skarels 	register struct a {
24839513Skarels 		int	signo;
24939513Skarels 		struct	sigvec *nsv;
25039513Skarels 		struct	sigvec *osv;
25139513Skarels 	} *uap = (struct a  *)u.u_ap;
25239513Skarels 	struct sigvec vec;
25339513Skarels 	register struct sigvec *sv;
25439513Skarels 	register int sig;
25539513Skarels 	int bit, error;
25639513Skarels 
25739513Skarels 	sig = uap->signo;
25839513Skarels 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
25939513Skarels 		RETURN (EINVAL);
26039513Skarels 	sv = &vec;
26139513Skarels 	if (uap->osv) {
26239513Skarels 		*(sig_t *)&sv->sv_handler = u.u_signal[sig];
26339513Skarels 		sv->sv_mask = u.u_sigmask[sig];
26439513Skarels 		bit = sigmask(sig);
26539513Skarels 		sv->sv_flags = 0;
26639513Skarels 		if ((u.u_sigonstack & bit) != 0)
26739513Skarels 			sv->sv_flags |= SV_ONSTACK;
26839513Skarels 		if ((u.u_sigintr & bit) != 0)
26939513Skarels 			sv->sv_flags |= SV_INTERRUPT;
27039513Skarels 		if (u.u_procp->p_flag & SNOCLDSTOP)
27139513Skarels 			sv->sv_flags |= SA_NOCLDSTOP;
27239513Skarels 		if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
27339513Skarels 		    sizeof (vec)))
27439513Skarels 			RETURN (error);
27539513Skarels 	}
27639513Skarels 	if (uap->nsv) {
27739513Skarels 		if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
27839513Skarels 		    sizeof (vec)))
27939513Skarels 			RETURN (error);
28039513Skarels 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
28139513Skarels 		setsigvec(sig, sv);
28239513Skarels 	}
28339513Skarels 	RETURN (0);
28439513Skarels }
28539513Skarels 
28639513Skarels osigblock()
28739513Skarels {
28839513Skarels 	struct a {
28917153Sbloom 		int	mask;
29012882Ssam 	} *uap = (struct a *)u.u_ap;
29112951Ssam 	register struct proc *p = u.u_procp;
2927499Sroot 
29317153Sbloom 	(void) splhigh();
29412882Ssam 	u.u_r.r_val1 = p->p_sigmask;
29539513Skarels 	p->p_sigmask |= uap->mask &~ sigcantmask;
29612882Ssam 	(void) spl0();
29739513Skarels 	RETURN (0);
2987499Sroot }
2997499Sroot 
30039513Skarels osigsetmask()
3017499Sroot {
30212882Ssam 	struct a {
30317153Sbloom 		int	mask;
30412882Ssam 	} *uap = (struct a *)u.u_ap;
30512882Ssam 	register struct proc *p = u.u_procp;
3067499Sroot 
30717153Sbloom 	(void) splhigh();
30812882Ssam 	u.u_r.r_val1 = p->p_sigmask;
30939513Skarels 	p->p_sigmask = uap->mask &~ sigcantmask;
31012882Ssam 	(void) spl0();
31139513Skarels 	RETURN (0);
3127499Sroot }
31339513Skarels #endif
3147499Sroot 
31539513Skarels /*
31639513Skarels  * Suspend process until signal, providing mask to be set
31739513Skarels  * in the meantime.  Note nonstandard calling convention:
31839513Skarels  * libc stub passes mask, not pointer, to save a copyin.
31939513Skarels  */
32039513Skarels sigsuspend()
3217499Sroot {
32212882Ssam 	struct a {
32339513Skarels 		sigset_t mask;
32412882Ssam 	} *uap = (struct a *)u.u_ap;
32512882Ssam 	register struct proc *p = u.u_procp;
3267499Sroot 
32712882Ssam 	/*
32812882Ssam 	 * When returning from sigpause, we want
32912882Ssam 	 * the old mask to be restored after the
33012882Ssam 	 * signal handler has finished.  Thus, we
33112882Ssam 	 * save it here and mark the proc structure
33212882Ssam 	 * to indicate this (should be in u.).
33312882Ssam 	 */
33412882Ssam 	u.u_oldmask = p->p_sigmask;
33512882Ssam 	p->p_flag |= SOMASK;
33639513Skarels 	p->p_sigmask = uap->mask &~ sigcantmask;
33740807Smarc 	(void) tsleep((caddr_t)&u, PPAUSE | PCATCH, "pause", 0);
33840807Smarc 	/* always return EINTR rather than ERESTART... */
33940807Smarc 	RETURN (EINTR);
3407499Sroot }
3417499Sroot 
3427499Sroot sigstack()
3437499Sroot {
34412951Ssam 	register struct a {
34512951Ssam 		struct	sigstack *nss;
34612951Ssam 		struct	sigstack *oss;
34712882Ssam 	} *uap = (struct a *)u.u_ap;
34812951Ssam 	struct sigstack ss;
34939513Skarels 	int error = 0;
3507499Sroot 
35139513Skarels 	if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack,
35239513Skarels 	    (caddr_t)uap->oss, sizeof (struct sigstack))))
35339513Skarels 		RETURN (error);
35439513Skarels 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
35539513Skarels 	    sizeof (ss))) == 0)
35639513Skarels 		u.u_sigstack = ss;
35739513Skarels 	RETURN (error);
3587499Sroot }
3597499Sroot 
3608032Sroot kill()
3618032Sroot {
36212882Ssam 	register struct a {
36312882Ssam 		int	pid;
36412882Ssam 		int	signo;
36512882Ssam 	} *uap = (struct a *)u.u_ap;
36618336Smckusick 	register struct proc *p;
3678032Sroot 
36839513Skarels 	if ((unsigned) uap->signo >= NSIG)
36939513Skarels 		RETURN (EINVAL);
37018336Smckusick 	if (uap->pid > 0) {
37118336Smckusick 		/* kill single process */
37218336Smckusick 		p = pfind(uap->pid);
37339513Skarels 		if (p == 0)
37439513Skarels 			RETURN (ESRCH);
37539513Skarels 		if (!CANSIGNAL(p, uap->signo))
37639513Skarels 			RETURN (EPERM);
37739513Skarels 		if (uap->signo)
37818336Smckusick 			psignal(p, uap->signo);
37939513Skarels 		RETURN (0);
38018336Smckusick 	}
38118336Smckusick 	switch (uap->pid) {
38218336Smckusick 	case -1:		/* broadcast signal */
38339513Skarels 		RETURN (killpg1(uap->signo, 0, 1));
38418336Smckusick 	case 0:			/* signal own process group */
38539513Skarels 		RETURN (killpg1(uap->signo, 0, 0));
38618336Smckusick 	default:		/* negative explicit process group */
38739513Skarels 		RETURN (killpg1(uap->signo, -uap->pid, 0));
38818336Smckusick 	}
38939513Skarels 	/* NOTREACHED */
3908032Sroot }
3918032Sroot 
39239513Skarels #ifdef COMPAT_43
39339513Skarels okillpg()
3948032Sroot {
3959989Ssam 	register struct a {
39637581Smckusick 		int	pgid;
3979989Ssam 		int	signo;
3989989Ssam 	} *uap = (struct a *)u.u_ap;
3998032Sroot 
40039513Skarels 	if ((unsigned) uap->signo >= NSIG)
40139513Skarels 		RETURN (EINVAL);
40239513Skarels 	RETURN (killpg1(uap->signo, uap->pgid, 0));
4038032Sroot }
40439513Skarels #endif
4058032Sroot 
40637581Smckusick killpg1(signo, pgid, all)
40737581Smckusick 	int signo, pgid, all;
4089989Ssam {
4099989Ssam 	register struct proc *p;
41037581Smckusick 	struct pgrp *pgrp;
41139513Skarels 	int f = 0, error = ESRCH;
41237581Smckusick 
41337581Smckusick 	if (all)
41437581Smckusick 		/*
41537581Smckusick 		 * broadcast
4167421Sroot 		 */
41737581Smckusick 		for (p = allproc; p != NULL; p = p->p_nxt) {
41837581Smckusick 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
41939513Skarels 			    p == u.u_procp || !CANSIGNAL(p, signo))
42037581Smckusick 				continue;
42137581Smckusick 			f++;
42237581Smckusick 			if (signo)
42337581Smckusick 				psignal(p, signo);
42437581Smckusick 		}
42537581Smckusick 	else {
42637581Smckusick 		if (pgid == 0)
42737581Smckusick 			/*
42837581Smckusick 			 * zero pgid means send to my process group.
42937581Smckusick 			 */
43037581Smckusick 			pgrp = u.u_procp->p_pgrp;
43137581Smckusick 		else {
43237581Smckusick 			pgrp = pgfind(pgid);
43337581Smckusick 			if (pgrp == NULL)
43439513Skarels 				return (ESRCH);
43537581Smckusick 		}
43637581Smckusick 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
43739513Skarels 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
43839513Skarels 			    !CANSIGNAL(p, signo))
43937581Smckusick 				continue;
44037581Smckusick 			f++;
44137581Smckusick 			if (signo)
44237581Smckusick 				psignal(p, signo);
44318336Smckusick 		}
4447421Sroot 	}
44539513Skarels 	return (f ? 0 : error);
4467421Sroot }
4477421Sroot 
44840807Smarc /* XXX - to be removed, as soon as sockets are changed to operate on pgrps
4497421Sroot  * Send the specified signal to
45037581Smckusick  * all processes with 'pgid' as
4517421Sroot  * process group.
4527421Sroot  */
45337581Smckusick gsignal(pgid, sig)
4547421Sroot {
45539513Skarels 	struct pgrp *pgrp;
4567421Sroot 
45739513Skarels 	if (pgid && (pgrp = pgfind(pgid)))
45839513Skarels 		pgsignal(pgrp, sig);
4597421Sroot }
46040807Smarc /*
46140807Smarc  * Send sig to all all members of the process group
46240807Smarc  */
46337581Smckusick pgsignal(pgrp, sig)
46439513Skarels 	struct pgrp *pgrp;
46537581Smckusick {
46637581Smckusick 	register struct proc *p;
46737581Smckusick 
46840807Smarc 	if (pgrp)
46940807Smarc 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
47040807Smarc 			psignal(p, sig);
47137581Smckusick }
47237581Smckusick 
4737421Sroot /*
47439513Skarels  * Send a signal caused by a trap to the current process.
47539513Skarels  * If it will be caught immediately, deliver it with correct code.
47639513Skarels  * Otherwise, post it normally.
47739513Skarels  */
47839513Skarels trapsignal(sig, code)
47939513Skarels 	register int sig;
48039513Skarels 	unsigned code;
48139513Skarels {
48239513Skarels 	register struct proc *p = u.u_procp;
48339513Skarels 	int mask;
48439513Skarels 
48539513Skarels 	mask = sigmask(sig);
48639513Skarels 	if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
48739513Skarels 	    (p->p_sigmask & mask) == 0) {
48839513Skarels 		u.u_ru.ru_nsignals++;
48940807Smarc #ifdef KTRACE
49040807Smarc 		if (KTRPOINT(p, KTR_PSIG))
49140807Smarc 			ktrpsig(p->p_tracep, sig, u.u_signal[sig],
49240807Smarc 				p->p_sigmask, code);
49340807Smarc #endif
49439513Skarels 		sendsig(u.u_signal[sig], sig, p->p_sigmask, code);
49539513Skarels 		p->p_sigmask |= u.u_sigmask[sig] | mask;
49639513Skarels 	} else {
49739513Skarels 		u.u_arg[1] = code;	/* XXX for core dump/debugger */
49839513Skarels 		psignal(p, sig);
49939513Skarels 	}
50039513Skarels }
50139513Skarels 
50239513Skarels /*
50340807Smarc  * Send the specified signal to the specified process.
50440807Smarc  * Most signals do not do anything directly to a process;
50540807Smarc  * they set a flag that asks the process to do something to itself.
50640807Smarc  * Exceptions:
50740807Smarc  *   o When a stop signal is sent to a sleeping process that takes the default
50840807Smarc  *     action, the process is stopped without awakening it.
50940807Smarc  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
51040807Smarc  *     regardless of the signal action (eg, blocked or ignored).
51140807Smarc  * Other ignored signals are discarded immediately.
5127421Sroot  */
5137421Sroot psignal(p, sig)
5147421Sroot 	register struct proc *p;
5157421Sroot 	register int sig;
5167421Sroot {
5177421Sroot 	register int s;
51839513Skarels 	register sig_t action;
51917153Sbloom 	int mask;
5207421Sroot 
52139513Skarels 	if ((unsigned)sig >= NSIG || sig == 0)
52239513Skarels 		panic("psignal sig");
52317153Sbloom 	mask = sigmask(sig);
5247421Sroot 
5257421Sroot 	/*
5267421Sroot 	 * If proc is traced, always give parent a chance.
5277421Sroot 	 */
5287421Sroot 	if (p->p_flag & STRC)
5297421Sroot 		action = SIG_DFL;
5307421Sroot 	else {
5317421Sroot 		/*
53212882Ssam 		 * If the signal is being ignored,
53312882Ssam 		 * then we forget about it immediately.
53439513Skarels 		 * (Note: we don't set SIGCONT in p_sigignore,
53539513Skarels 		 * and if it is set to SIG_IGN,
53639513Skarels 		 * action will be SIG_DFL here.)
5377421Sroot 		 */
53817153Sbloom 		if (p->p_sigignore & mask)
5397421Sroot 			return;
54017153Sbloom 		if (p->p_sigmask & mask)
54112882Ssam 			action = SIG_HOLD;
54217153Sbloom 		else if (p->p_sigcatch & mask)
54312882Ssam 			action = SIG_CATCH;
54440807Smarc 		else {
54540807Smarc 			if (p->p_pgrp->pg_jobc == 0 && (sig == SIGTTIN ||
54640807Smarc 			    sig == SIGTTOU || sig == SIGTSTP))
54740807Smarc 				return;
54812882Ssam 			action = SIG_DFL;
54940807Smarc 		}
5507421Sroot 	}
55139513Skarels 	switch (sig) {
5527421Sroot 
55339513Skarels 	case SIGTERM:
55439513Skarels 		if ((p->p_flag&STRC) || action != SIG_DFL)
5557421Sroot 			break;
55639513Skarels 		/* FALLTHROUGH */
5577421Sroot 
55839513Skarels 	case SIGKILL:
55939513Skarels 		if (p->p_nice > NZERO)
56039513Skarels 			p->p_nice = NZERO;
56139513Skarels 		break;
5627421Sroot 
56339513Skarels 	case SIGCONT:
56439513Skarels 		p->p_sig &= ~stopsigmask;
56539513Skarels 		break;
56639513Skarels 
56739513Skarels 	case SIGTSTP:
56839513Skarels 	case SIGTTIN:
56939513Skarels 	case SIGTTOU:
57039513Skarels 	case SIGSTOP:
57139513Skarels 		p->p_sig &= ~sigmask(SIGCONT);
57239513Skarels 		break;
5737421Sroot 	}
57439513Skarels 	p->p_sig |= mask;
57539513Skarels 
5767421Sroot 	/*
57739513Skarels 	 * Defer further processing for signals which are held,
57839513Skarels 	 * except that stopped processes must be continued by SIGCONT.
5797421Sroot 	 */
58039513Skarels 	if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP))
5817421Sroot 		return;
58217153Sbloom 	s = splhigh();
5837421Sroot 	switch (p->p_stat) {
5847421Sroot 
5857421Sroot 	case SSLEEP:
5867421Sroot 		/*
58740807Smarc 		 * If process is sleeping uninterruptibly
5887421Sroot 		 * we can't interrupt the sleep... the signal will
5897421Sroot 		 * be noticed when the process returns through
5907421Sroot 		 * trap() or syscall().
5917421Sroot 		 */
59240807Smarc 		if ((p->p_flag & SSINTR) == 0)
5937421Sroot 			goto out;
5947421Sroot 		/*
5957421Sroot 		 * Process is sleeping and traced... make it runnable
5967421Sroot 		 * so it can discover the signal in issig() and stop
5977421Sroot 		 * for the parent.
5987421Sroot 		 */
5997421Sroot 		if (p->p_flag&STRC)
6007421Sroot 			goto run;
60139513Skarels 		/*
60239513Skarels 		 * When a sleeping process receives a stop
60339513Skarels 		 * signal, process immediately if possible.
60439513Skarels 		 * All other (caught or default) signals
60539513Skarels 		 * cause the process to run.
60639513Skarels 		 */
60739513Skarels 		if (mask & stopsigmask) {
6087421Sroot 			if (action != SIG_DFL)
60939513Skarels 				goto runfast;
6107421Sroot 			/*
6117421Sroot 			 * If a child in vfork(), stopping could
6127421Sroot 			 * cause deadlock.
6137421Sroot 			 */
6147421Sroot 			if (p->p_flag&SVFORK)
6157421Sroot 				goto out;
61617153Sbloom 			p->p_sig &= ~mask;
6177421Sroot 			p->p_cursig = sig;
61839513Skarels 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
61939513Skarels 				psignal(p->p_pptr, SIGCHLD);
6207421Sroot 			stop(p);
6217421Sroot 			goto out;
62239513Skarels 		} else
62339513Skarels 			goto runfast;
6247421Sroot 		/*NOTREACHED*/
6257421Sroot 
6267421Sroot 	case SSTOP:
6277421Sroot 		/*
6287421Sroot 		 * If traced process is already stopped,
6297421Sroot 		 * then no further action is necessary.
6307421Sroot 		 */
6317421Sroot 		if (p->p_flag&STRC)
6327421Sroot 			goto out;
6337421Sroot 		switch (sig) {
6347421Sroot 
6357421Sroot 		case SIGKILL:
6367421Sroot 			/*
6377421Sroot 			 * Kill signal always sets processes running.
6387421Sroot 			 */
63939513Skarels 			goto runfast;
6407421Sroot 
6417421Sroot 		case SIGCONT:
6427421Sroot 			/*
64339513Skarels 			 * If SIGCONT is default (or ignored), we continue
64439513Skarels 			 * the process but don't leave the signal in p_sig,
64539513Skarels 			 * as it has no further action.  If SIGCONT is held,
64639513Skarels 			 * continue the process and leave the signal in p_sig.
6477421Sroot 			 * If the process catches SIGCONT, let it handle
6487421Sroot 			 * the signal itself.  If it isn't waiting on
6497421Sroot 			 * an event, then it goes back to run state.
6507421Sroot 			 * Otherwise, process goes back to sleep state.
6517421Sroot 			 */
65239513Skarels 			p->p_cursig = 0;	/* ??? XXX */
65339513Skarels 			if (action == SIG_DFL)
65439513Skarels 				p->p_sig &= ~mask;
65539513Skarels 			if (action == SIG_CATCH)
65639513Skarels 				goto runfast;
65739513Skarels 			if (p->p_wchan == 0)
6587421Sroot 				goto run;
6597421Sroot 			p->p_stat = SSLEEP;
6607421Sroot 			goto out;
6617421Sroot 
6627421Sroot 		case SIGSTOP:
6637421Sroot 		case SIGTSTP:
6647421Sroot 		case SIGTTIN:
6657421Sroot 		case SIGTTOU:
6667421Sroot 			/*
6677421Sroot 			 * Already stopped, don't need to stop again.
6687421Sroot 			 * (If we did the shell could get confused.)
6697421Sroot 			 */
67017153Sbloom 			p->p_sig &= ~mask;		/* take it away */
6717421Sroot 			goto out;
6727421Sroot 
6737421Sroot 		default:
6747421Sroot 			/*
6757421Sroot 			 * If process is sleeping interruptibly, then
67640807Smarc 			 * simulate a wakeup so that when it is continued,
67740807Smarc 			 * it will be made runnable and can look at the signal.
67840807Smarc 			 * But don't setrun the process, leave it stopped.
6797421Sroot 			 */
68040807Smarc 			if (p->p_wchan && p->p_flag & SSINTR)
6817421Sroot 				unsleep(p);
6827421Sroot 			goto out;
6837421Sroot 		}
6847421Sroot 		/*NOTREACHED*/
6857421Sroot 
6867421Sroot 	default:
6877421Sroot 		/*
6887421Sroot 		 * SRUN, SIDL, SZOMB do nothing with the signal,
6897421Sroot 		 * other than kicking ourselves if we are running.
6907421Sroot 		 * It will either never be noticed, or noticed very soon.
6917421Sroot 		 */
6927421Sroot 		if (p == u.u_procp && !noproc)
6937421Sroot 			aston();
6947421Sroot 		goto out;
6957421Sroot 	}
6967421Sroot 	/*NOTREACHED*/
69739513Skarels 
69839513Skarels runfast:
6997421Sroot 	/*
7007421Sroot 	 * Raise priority to at least PUSER.
7017421Sroot 	 */
7027421Sroot 	if (p->p_pri > PUSER)
70317399Skarels 		p->p_pri = PUSER;
70439513Skarels run:
7057421Sroot 	setrun(p);
7067421Sroot out:
7077421Sroot 	splx(s);
7087421Sroot }
7097421Sroot 
7107421Sroot /*
71140807Smarc  * If the current process has a signal to process (should be caught
71240807Smarc  * or cause termination, should interrupt current syscall),
71340807Smarc  * return the signal number.  Stop signals with default action
71440807Smarc  * are processed immediately, then cleared; they aren't returned.
7157421Sroot  * This is asked at least once each time a process enters the
7167421Sroot  * system (though this can usually be done without actually
7177421Sroot  * calling issig by checking the pending signal masks.)
7187421Sroot  */
7197421Sroot issig()
7207421Sroot {
7217421Sroot 	register struct proc *p;
72239513Skarels 	register int sig, mask;
7237421Sroot 
7247421Sroot 	p = u.u_procp;
7257421Sroot 	for (;;) {
72639513Skarels 		mask = p->p_sig &~ p->p_sigmask;
7277421Sroot 		if (p->p_flag&SVFORK)
72839513Skarels 			mask &= ~stopsigmask;
72940807Smarc 		if (mask == 0)	 	/* no signal to send */
73040807Smarc 			return (0);
73139513Skarels 		sig = ffs((long)mask);
73217153Sbloom 		mask = sigmask(sig);
73340807Smarc 		/*
73440807Smarc 		 * We should see pending but ignored signals
73540807Smarc 		 * only if STRC was on when they were posted.
73640807Smarc 		 */
73740807Smarc 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
73840807Smarc 			p->p_sig &= ~mask;
73940807Smarc 			continue;
74040807Smarc 		}
74112882Ssam 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
7427421Sroot 			/*
7437421Sroot 			 * If traced, always stop, and stay
7447421Sroot 			 * stopped until released by the parent.
7457421Sroot 			 */
74640807Smarc 			p->p_cursig = sig;
74718331Skarels 			psignal(p->p_pptr, SIGCHLD);
7487421Sroot 			do {
7497421Sroot 				stop(p);
7507421Sroot 				swtch();
7517421Sroot 			} while (!procxmt() && p->p_flag&STRC);
7527421Sroot 
7537421Sroot 			/*
75414782Ssam 			 * If the traced bit got turned off,
75540807Smarc 			 * go back up to the top to rescan signals.
75614782Ssam 			 * This ensures that p_sig* and u_signal are consistent.
7577421Sroot 			 */
75840807Smarc 			if ((p->p_flag&STRC) == 0)
7597421Sroot 				continue;
7607421Sroot 
7617421Sroot 			/*
7627421Sroot 			 * If parent wants us to take the signal,
7637421Sroot 			 * then it will leave it in p->p_cursig;
7647421Sroot 			 * otherwise we just look for signals again.
7657421Sroot 			 */
76640807Smarc 			p->p_sig &= ~mask;	/* clear the old signal */
7677421Sroot 			sig = p->p_cursig;
7687421Sroot 			if (sig == 0)
7697421Sroot 				continue;
77014782Ssam 
77114782Ssam 			/*
77240807Smarc 			 * Put the new signal into p_sig.
77340807Smarc 			 * If signal is being masked,
77440807Smarc 			 * look for other signals.
77514782Ssam 			 */
77617153Sbloom 			mask = sigmask(sig);
77740807Smarc 			p->p_sig |= mask;
77840807Smarc 			if (p->p_sigmask & mask)
77914782Ssam 				continue;
7807421Sroot 		}
78140807Smarc 
78240807Smarc 		/*
78340807Smarc 		 * Decide whether the signal should be returned.
78440807Smarc 		 * Return the signal's number, or fall through
78540807Smarc 		 * to clear it from the pending mask.
78640807Smarc 		 */
78724901Skarels 		switch ((int)u.u_signal[sig]) {
7887421Sroot 
7897421Sroot 		case SIG_DFL:
7907421Sroot 			/*
7917421Sroot 			 * Don't take default actions on system processes.
7927421Sroot 			 */
7937421Sroot 			if (p->p_ppid == 0)
79440807Smarc 				break;		/* == ignore */
79540807Smarc 			/*
79640807Smarc 			 * If there is a pending stop signal to process
79740807Smarc 			 * with default action, stop here,
79840807Smarc 			 * then clear the signal.
79940807Smarc 			 */
80039513Skarels 			if (mask & stopsigmask) {
8017421Sroot 				if (p->p_flag&STRC)
80240807Smarc 					break;	/* == ignore */
80340807Smarc 				p->p_cursig = sig;
8047421Sroot 				stop(p);
80539513Skarels 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
80639513Skarels 					psignal(p->p_pptr, SIGCHLD);
8077421Sroot 				swtch();
80840807Smarc 				break;
80939513Skarels 			} else if (mask & defaultignmask) {
8107421Sroot 				/*
81139513Skarels 				 * Except for SIGCONT, shouldn't get here.
81239513Skarels 				 * Default action is to ignore; drop it.
8137421Sroot 				 */
81440807Smarc 				break;		/* == ignore */
81539513Skarels 			} else
81640807Smarc 				return (sig);
8177421Sroot 			/*NOTREACHED*/
8187421Sroot 
8197421Sroot 		case SIG_IGN:
8207421Sroot 			/*
82139513Skarels 			 * Masking above should prevent us ever trying
82239513Skarels 			 * to take action on an ignored signal other
82339513Skarels 			 * than SIGCONT, unless process is traced.
8247421Sroot 			 */
82539513Skarels 			if (sig != SIGCONT && (p->p_flag&STRC) == 0)
8267421Sroot 				printf("issig\n");
82740807Smarc 			break;		/* == ignore */
8287421Sroot 
8297421Sroot 		default:
8307421Sroot 			/*
8317421Sroot 			 * This signal has an action, let
8327421Sroot 			 * psig process it.
8337421Sroot 			 */
83440807Smarc 			return (sig);
8357421Sroot 		}
83640807Smarc 		p->p_sig &= ~mask;		/* take the signal! */
8377421Sroot 	}
83840807Smarc 	/* NOTREACHED */
8397421Sroot }
8407421Sroot 
8417421Sroot /*
8427421Sroot  * Put the argument process into the stopped
84318331Skarels  * state and notify the parent via wakeup.
84418331Skarels  * Signals are handled elsewhere.
84540807Smarc  * The process must not be on the run queue.
8467421Sroot  */
8477421Sroot stop(p)
8487421Sroot 	register struct proc *p;
8497421Sroot {
8507421Sroot 
8517421Sroot 	p->p_stat = SSTOP;
8527421Sroot 	p->p_flag &= ~SWTED;
8537421Sroot 	wakeup((caddr_t)p->p_pptr);
8547421Sroot }
8557421Sroot 
8567421Sroot /*
85740807Smarc  * Perform the action specified by the current signal.
8587421Sroot  * The usual sequence is:
85940807Smarc  *	if (sig = CURSIG(p))
86040807Smarc  *		psig(sig);
8617421Sroot  */
86240807Smarc psig(sig)
86340807Smarc 	register int sig;
8647421Sroot {
86512882Ssam 	register struct proc *p = u.u_procp;
86639513Skarels 	int mask, returnmask;
86739513Skarels 	register sig_t action;
8687421Sroot 
86939513Skarels 	do {
87040807Smarc #ifdef DIAGNOSTIC
87139513Skarels 		if (sig == 0)
87239513Skarels 			panic("psig");
87340807Smarc #endif
87440807Smarc 		mask = sigmask(sig);
87540807Smarc 		p->p_sig &= ~mask;
87639513Skarels 		action = u.u_signal[sig];
87740807Smarc #ifdef KTRACE
87840807Smarc 		if (KTRPOINT(p, KTR_PSIG))
87940807Smarc 			ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ?
88040807Smarc 				u.u_oldmask : p->p_sigmask, 0);
88140807Smarc #endif
88239513Skarels 		if (action != SIG_DFL) {
88339513Skarels #ifdef DIAGNOSTIC
88439513Skarels 			if (action == SIG_IGN || (p->p_sigmask & mask))
88539513Skarels 				panic("psig action");
88639513Skarels #endif
88739513Skarels 			u.u_error = 0;
88839513Skarels 			/*
88939513Skarels 			 * Set the new mask value and also defer further
89039513Skarels 			 * occurences of this signal.
89139513Skarels 			 *
89239513Skarels 			 * Special case: user has done a sigpause.  Here the
89339513Skarels 			 * current mask is not of interest, but rather the
89439513Skarels 			 * mask from before the sigpause is what we want
89539513Skarels 			 * restored after the signal processing is completed.
89639513Skarels 			 */
89739513Skarels 			(void) splhigh();
89839513Skarels 			if (p->p_flag & SOMASK) {
89939513Skarels 				returnmask = u.u_oldmask;
90039513Skarels 				p->p_flag &= ~SOMASK;
90139513Skarels 			} else
90239513Skarels 				returnmask = p->p_sigmask;
90339513Skarels 			p->p_sigmask |= u.u_sigmask[sig] | mask;
90439513Skarels 			(void) spl0();
90539513Skarels 			u.u_ru.ru_nsignals++;
90639513Skarels 			sendsig(action, sig, returnmask, 0);
90739513Skarels 			continue;
9087421Sroot 		}
90939513Skarels 		u.u_acflag |= AXSIG;
91039513Skarels 		switch (sig) {
9117421Sroot 
91239513Skarels 		case SIGILL:
91339513Skarels 		case SIGIOT:
91439513Skarels 		case SIGBUS:
91539513Skarels 		case SIGQUIT:
91639513Skarels 		case SIGTRAP:
91739513Skarels 		case SIGEMT:
91839513Skarels 		case SIGFPE:
91939513Skarels 		case SIGSEGV:
92039513Skarels 		case SIGSYS:
92139513Skarels 			u.u_arg[0] = sig;
92239513Skarels 			if (core() == 0)
92339513Skarels 				sig |= WCOREFLAG;
92439513Skarels 		}
92539513Skarels 		exit(W_EXITCODE(0, sig));
92639513Skarels 		/* NOTREACHED */
92740807Smarc 	} while (sig = CURSIG(p));
9287421Sroot }
9297421Sroot 
9307421Sroot /*
93139513Skarels  * Create a core image on the file "core".
9327421Sroot  * It writes UPAGES block of the
9337421Sroot  * user.h area followed by the entire
9347421Sroot  * data+stack segments.
9357421Sroot  */
9367421Sroot core()
9377421Sroot {
93837728Smckusick 	register struct vnode *vp;
93939513Skarels 	register struct proc *p = u.u_procp;
94016692Smckusick 	register struct nameidata *ndp = &u.u_nd;
94137580Smckusick 	struct vattr vattr;
94237580Smckusick 	int error;
9437421Sroot 
94439513Skarels 	if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
94537580Smckusick 		return (EFAULT);
94637580Smckusick 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
9478032Sroot 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
94837580Smckusick 		return (EFAULT);
94939513Skarels 	if (p->p_textp) {
95039513Skarels 		VOP_LOCK(p->p_textp->x_vptr);
95139513Skarels 		error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
95239513Skarels 		VOP_UNLOCK(p->p_textp->x_vptr);
95337580Smckusick 		if (error)
95437580Smckusick 			return (EFAULT);
95537580Smckusick 	}
95616692Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
95716692Smckusick 	ndp->ni_dirp = "core";
95837580Smckusick 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
95937580Smckusick 		return (error);
96037580Smckusick 	vp = ndp->ni_vp;
96138394Smckusick 	VOP_LOCK(vp);
96237580Smckusick 	if (vp->v_type != VREG ||
96337728Smckusick 	    VOP_GETATTR(vp, &vattr, u.u_cred) ||
96437580Smckusick 	    vattr.va_nlink != 1) {
96538394Smckusick 		vput(vp);
96638394Smckusick 		return (EFAULT);
9677818Sroot 	}
96830290Ssam #ifdef MMAP
96930290Ssam 	{ register int fd;
97038394Smckusick 	/* unmap funky devices in the user's address space */
97130290Ssam 	for (fd = 0; fd < u.u_lastfile; fd++)
97230290Ssam 		if (u.u_ofile[fd] && (u.u_pofile[fd] & UF_MAPPED))
97330290Ssam 			munmapfd(fd);
97430290Ssam 	}
97530290Ssam #endif
976*41362Smckusick 	VATTR_NULL(&vattr);
97737580Smckusick 	vattr.va_size = 0;
97837728Smckusick 	VOP_SETATTR(vp, &vattr, u.u_cred);
9797818Sroot 	u.u_acflag |= ACORE;
98037580Smckusick 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
98138394Smckusick 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
98237580Smckusick 	if (error == 0)
98337580Smckusick 		error = vn_rdwr(UIO_WRITE, vp,
98439513Skarels 		    (caddr_t)ctob(dptov(p, 0)),
98538394Smckusick 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
98638394Smckusick 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
98737580Smckusick 	if (error == 0)
98837580Smckusick 		error = vn_rdwr(UIO_WRITE, vp,
98939513Skarels 		    (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
99026354Skarels 		    (int)ctob(u.u_ssize),
99138394Smckusick 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
99238394Smckusick 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
99338394Smckusick 	vput(vp);
99437580Smckusick 	return (error);
9957421Sroot }
99639513Skarels 
99739513Skarels /*
99839513Skarels  * Nonexistent system call-- signal process (may want to handle it).
99939513Skarels  * Flag error in case process won't see signal immediately (blocked or ignored).
100039513Skarels  */
100139513Skarels nosys()
100239513Skarels {
100339513Skarels 
100439513Skarels 	psignal(u.u_procp, SIGSYS);
100539513Skarels 	u.u_error = EINVAL;
100639513Skarels }
1007