xref: /freebsd/sys/kern/kern_sig.c (revision 238510fc467906d3ba189907de9e90c41f542b1f)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46c87e2930SDavid Greenman #include <sys/kernel.h>
47d2d3e875SBruce Evans #include <sys/sysproto.h>
4836240ea5SDoug Rabson #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
51df8bae1dSRodney W. Grimes #include <sys/namei.h>
52df8bae1dSRodney W. Grimes #include <sys/vnode.h>
53cb679c38SJonathan Lemon #include <sys/event.h>
54df8bae1dSRodney W. Grimes #include <sys/proc.h>
552a024a2bSSean Eric Fagan #include <sys/pioctl.h>
56df8bae1dSRodney W. Grimes #include <sys/acct.h>
573ac4d1efSBruce Evans #include <sys/fcntl.h>
5835e0e5b3SJohn Baldwin #include <sys/ipl.h>
59238510fcSJason Evans #include <sys/condvar.h>
6035e0e5b3SJohn Baldwin #include <sys/mutex.h>
61df8bae1dSRodney W. Grimes #include <sys/wait.h>
620384fff8SJason Evans #include <sys/ktr.h>
63df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
64df8bae1dSRodney W. Grimes #include <sys/syslog.h>
65df8bae1dSRodney W. Grimes #include <sys/stat.h>
66d66a5066SPeter Wemm #include <sys/sysent.h>
67c87e2930SDavid Greenman #include <sys/sysctl.h>
68c5edb423SSean Eric Fagan #include <sys/malloc.h>
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <machine/cpu.h>
713163861cSTor Egge #include <machine/smp.h>
72df8bae1dSRodney W. Grimes 
736f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
746f841fb7SMarcel Moolenaar 
756f841fb7SMarcel Moolenaar static int coredump	__P((struct proc *));
762c42a146SMarcel Moolenaar static int do_sigaction	__P((struct proc *p, int sig, struct sigaction *act,
77645682fdSLuoqi Chen 			     struct sigaction *oact, int old));
782c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
79645682fdSLuoqi Chen 			       sigset_t *oset, int old));
80f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t));
816f841fb7SMarcel Moolenaar static int killpg1	__P((struct proc *cp, int sig, int pgid, int all));
826f841fb7SMarcel Moolenaar static int sig_ffs	__P((sigset_t *set));
836f841fb7SMarcel Moolenaar static int sigprop	__P((int sig));
846f841fb7SMarcel Moolenaar static void stop	__P((struct proc *));
8526f9a767SRodney W. Grimes 
86cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
87cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
88cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
89cb679c38SJonathan Lemon 
90cb679c38SJonathan Lemon struct filterops sig_filtops =
91cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
92cb679c38SJonathan Lemon 
9357308494SJoerg Wunsch static int	kern_logsigexit = 1;
943d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
953d177f46SBill Fumerola     &kern_logsigexit, 0,
963d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
9757308494SJoerg Wunsch 
98df8bae1dSRodney W. Grimes /*
992c42a146SMarcel Moolenaar  * Can process p, with pcred pc, send the signal sig to process q?
100df8bae1dSRodney W. Grimes  */
101a9e0361bSPoul-Henning Kamp #define CANSIGNAL(p, q, sig) \
102387d2c03SRobert Watson 	(!p_can(p, q, P_CAN_KILL, NULL) || \
103a9e0361bSPoul-Henning Kamp 	((sig) == SIGCONT && (q)->p_session == (p)->p_session))
104df8bae1dSRodney W. Grimes 
105831d27a9SDon Lewis /*
106831d27a9SDon Lewis  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
107831d27a9SDon Lewis  */
108831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \
109831d27a9SDon Lewis 	((uc)->cr_uid == 0 || \
110831d27a9SDon Lewis 	    (ruid) == (q)->p_cred->p_ruid || \
111831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_cred->p_ruid || \
112831d27a9SDon Lewis 	    (ruid) == (q)->p_ucred->cr_uid || \
113831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
114831d27a9SDon Lewis 
11522d4b0fbSJohn Polstra int sugid_coredump;
1163d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1173d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
118c87e2930SDavid Greenman 
119e5a28db9SPaul Saab static int	do_coredump = 1;
120e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
121e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
122e5a28db9SPaul Saab 
1232c42a146SMarcel Moolenaar /*
1242c42a146SMarcel Moolenaar  * Signal properties and actions.
1252c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1262c42a146SMarcel Moolenaar  * according to the following properties:
1272c42a146SMarcel Moolenaar  */
1282c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1292c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1302c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1312c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1322c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1332c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1342c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
135df8bae1dSRodney W. Grimes 
1362c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1372c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1382c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1392c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1412c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1452c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1462c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1492c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1502c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1512c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1522c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1532c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1542c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1552c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1562c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1572c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1582c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1592c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1602c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1612c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1622c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1632c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1642c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1652c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1662c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1672c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1682c42a146SMarcel Moolenaar };
1692c42a146SMarcel Moolenaar 
170fbbeeb6cSBruce Evans /*
171fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
172fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
173fbbeeb6cSBruce Evans  * action, the process stops in issignal().
174fbbeeb6cSBruce Evans  *
17533510ef1SBruce Evans  * MP SAFE.
176fbbeeb6cSBruce Evans  */
177fbbeeb6cSBruce Evans int
178fbbeeb6cSBruce Evans CURSIG(struct proc *p)
179fbbeeb6cSBruce Evans {
180fbbeeb6cSBruce Evans 	sigset_t tmpset;
181fbbeeb6cSBruce Evans 	int r;
182fbbeeb6cSBruce Evans 
18333510ef1SBruce Evans 	if (SIGISEMPTY(p->p_siglist))
18433510ef1SBruce Evans 		return (0);
185fbbeeb6cSBruce Evans 	tmpset = p->p_siglist;
186fbbeeb6cSBruce Evans 	SIGSETNAND(tmpset, p->p_sigmask);
18733510ef1SBruce Evans 	if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
188fbbeeb6cSBruce Evans 		return (0);
189fbbeeb6cSBruce Evans 	mtx_enter(&Giant, MTX_DEF);
190fbbeeb6cSBruce Evans 	r = issignal(p);
191fbbeeb6cSBruce Evans 	mtx_exit(&Giant, MTX_DEF);
192fbbeeb6cSBruce Evans 	return (r);
193fbbeeb6cSBruce Evans }
194fbbeeb6cSBruce Evans 
1956f841fb7SMarcel Moolenaar static __inline int
1966f841fb7SMarcel Moolenaar sigprop(int sig)
1972c42a146SMarcel Moolenaar {
1986f841fb7SMarcel Moolenaar 
1992c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2002c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2012c42a146SMarcel Moolenaar 	return (0);
202df8bae1dSRodney W. Grimes }
2032c42a146SMarcel Moolenaar 
2046f841fb7SMarcel Moolenaar static __inline int
2056f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2062c42a146SMarcel Moolenaar {
2072c42a146SMarcel Moolenaar 	int i;
2082c42a146SMarcel Moolenaar 
2096f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2102c42a146SMarcel Moolenaar 		if (set->__bits[i])
2112c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
212df8bae1dSRodney W. Grimes 	return (0);
213df8bae1dSRodney W. Grimes }
214df8bae1dSRodney W. Grimes 
2152c42a146SMarcel Moolenaar /*
2162c42a146SMarcel Moolenaar  * do_sigaction
2172c42a146SMarcel Moolenaar  * sigaction
2182c42a146SMarcel Moolenaar  * osigaction
2192c42a146SMarcel Moolenaar  */
2202c42a146SMarcel Moolenaar static int
221645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
2222c42a146SMarcel Moolenaar 	struct proc *p;
2232c42a146SMarcel Moolenaar 	register int sig;
2242c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
225645682fdSLuoqi Chen 	int old;
226df8bae1dSRodney W. Grimes {
227df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
228df8bae1dSRodney W. Grimes 
2292c42a146SMarcel Moolenaar 	if (sig <= 0 || sig > _SIG_MAXSIG)
2302c42a146SMarcel Moolenaar 		return (EINVAL);
2312c42a146SMarcel Moolenaar 
2322c42a146SMarcel Moolenaar 	if (oact) {
2332c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2342c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2352c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2362c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2372c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2382c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2392c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2402c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2412c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2422c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2432c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2442c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2452c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
246645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2472c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
248645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2492c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2502c42a146SMarcel Moolenaar 	}
2512c42a146SMarcel Moolenaar 	if (act) {
2522c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
2532c42a146SMarcel Moolenaar 		    act->sa_handler != SIG_DFL)
2542c42a146SMarcel Moolenaar 			return (EINVAL);
2552c42a146SMarcel Moolenaar 
256df8bae1dSRodney W. Grimes 		/*
257df8bae1dSRodney W. Grimes 		 * Change setting atomically.
258df8bae1dSRodney W. Grimes 		 */
259df8bae1dSRodney W. Grimes 		(void) splhigh();
2602c42a146SMarcel Moolenaar 
2612c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2622c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2632c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
2642c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2652c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_siginfo, sig);
2666f841fb7SMarcel Moolenaar 		} else {
2672c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] =
2682c42a146SMarcel Moolenaar 			    (__sighandler_t *)act->sa_sigaction;
2692c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2702c42a146SMarcel Moolenaar 		}
2712c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2722c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
273df8bae1dSRodney W. Grimes 		else
2742c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2752c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2762c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
277df8bae1dSRodney W. Grimes 		else
2782c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2792c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2802c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2811e41c1b5SSteven Wallace 		else
2822c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2832c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2842c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
285289ccde0SPeter Wemm 		else
2862c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
287df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2882c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2892c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
290df8bae1dSRodney W. Grimes 		else
2912c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_usertramp, seg);
292df8bae1dSRodney W. Grimes #endif
2932c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2942c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
295645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
2966626c604SJulian Elischer 			else
297645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
2982c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDWAIT) {
299245f17d4SJoerg Wunsch 				/*
3002c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3012c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3022c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3032c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
304245f17d4SJoerg Wunsch 				 */
305245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
306645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3076626c604SJulian Elischer 				else
308645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
309245f17d4SJoerg Wunsch 			} else
310645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
311df8bae1dSRodney W. Grimes 		}
312df8bae1dSRodney W. Grimes 		/*
313df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3142c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3152c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3162c42a146SMarcel Moolenaar 		 * have to restart the process.
317df8bae1dSRodney W. Grimes 		 */
3182c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3192c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3202c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3212c42a146SMarcel Moolenaar 			/* never to be seen again */
3222c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3232c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3242c42a146SMarcel Moolenaar 				/* easier in psignal */
3252c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3262c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
327645682fdSLuoqi Chen 		} else {
3282c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3292c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3302c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3312c42a146SMarcel Moolenaar 			else
3322c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3332c42a146SMarcel Moolenaar 		}
334645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
335645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
336645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
337645682fdSLuoqi Chen 		else
338645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
3392c42a146SMarcel Moolenaar 
340df8bae1dSRodney W. Grimes 		(void) spl0();
341df8bae1dSRodney W. Grimes 	}
3422c42a146SMarcel Moolenaar 	return (0);
3432c42a146SMarcel Moolenaar }
3442c42a146SMarcel Moolenaar 
3452c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3462c42a146SMarcel Moolenaar struct sigaction_args {
3472c42a146SMarcel Moolenaar 	int	sig;
3482c42a146SMarcel Moolenaar 	struct	sigaction *act;
3492c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3502c42a146SMarcel Moolenaar };
3512c42a146SMarcel Moolenaar #endif
3522c42a146SMarcel Moolenaar /* ARGSUSED */
3532c42a146SMarcel Moolenaar int
3542c42a146SMarcel Moolenaar sigaction(p, uap)
3552c42a146SMarcel Moolenaar 	struct proc *p;
3562c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3572c42a146SMarcel Moolenaar {
3582c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3592c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3602c42a146SMarcel Moolenaar 	int error;
3612c42a146SMarcel Moolenaar 
3626f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3636f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3642c42a146SMarcel Moolenaar 	if (actp) {
3656f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3662c42a146SMarcel Moolenaar 		if (error)
3672c42a146SMarcel Moolenaar 			return (error);
3682c42a146SMarcel Moolenaar 	}
369645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3702c42a146SMarcel Moolenaar 	if (oactp && !error) {
3716f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3722c42a146SMarcel Moolenaar 	}
3732c42a146SMarcel Moolenaar 	return (error);
3742c42a146SMarcel Moolenaar }
3752c42a146SMarcel Moolenaar 
37631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3772c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3782c42a146SMarcel Moolenaar struct osigaction_args {
3792c42a146SMarcel Moolenaar 	int	signum;
3802c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3812c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3822c42a146SMarcel Moolenaar };
3832c42a146SMarcel Moolenaar #endif
3842c42a146SMarcel Moolenaar /* ARGSUSED */
3852c42a146SMarcel Moolenaar int
3862c42a146SMarcel Moolenaar osigaction(p, uap)
3872c42a146SMarcel Moolenaar 	struct proc *p;
3882c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3892c42a146SMarcel Moolenaar {
3902c42a146SMarcel Moolenaar 	struct osigaction sa;
3912c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
3922c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
3932c42a146SMarcel Moolenaar 	int error;
3942c42a146SMarcel Moolenaar 
3956f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
3966f841fb7SMarcel Moolenaar 		return (EINVAL);
3976f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
3986f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
3992c42a146SMarcel Moolenaar 	if (nsap) {
4006f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4012c42a146SMarcel Moolenaar 		if (error)
4022c42a146SMarcel Moolenaar 			return (error);
4032c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4042c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4052c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4062c42a146SMarcel Moolenaar 	}
407645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
4082c42a146SMarcel Moolenaar 	if (osap && !error) {
4092c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4102c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4112c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4126f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4132c42a146SMarcel Moolenaar 	}
4142c42a146SMarcel Moolenaar 	return (error);
4152c42a146SMarcel Moolenaar }
41631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes /*
419df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
420df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
421df8bae1dSRodney W. Grimes  */
422df8bae1dSRodney W. Grimes void
423df8bae1dSRodney W. Grimes siginit(p)
424df8bae1dSRodney W. Grimes 	struct proc *p;
425df8bae1dSRodney W. Grimes {
426df8bae1dSRodney W. Grimes 	register int i;
427df8bae1dSRodney W. Grimes 
4282c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4292c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4302c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
431df8bae1dSRodney W. Grimes }
432df8bae1dSRodney W. Grimes 
433df8bae1dSRodney W. Grimes /*
434df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
435df8bae1dSRodney W. Grimes  */
436df8bae1dSRodney W. Grimes void
437df8bae1dSRodney W. Grimes execsigs(p)
438df8bae1dSRodney W. Grimes 	register struct proc *p;
439df8bae1dSRodney W. Grimes {
440df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
4412c42a146SMarcel Moolenaar 	register int sig;
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes 	/*
444df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
445df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
446df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
447df8bae1dSRodney W. Grimes 	 */
4482c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4492c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4502c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4512c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4522c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4532c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4542c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
455df8bae1dSRodney W. Grimes 		}
4562c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
457df8bae1dSRodney W. Grimes 	}
458df8bae1dSRodney W. Grimes 	/*
459df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
460df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
461df8bae1dSRodney W. Grimes 	 */
462645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
463645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
464645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
46580e907a1SPeter Wemm 	/*
46680e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
46780e907a1SPeter Wemm 	 */
468645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
469df8bae1dSRodney W. Grimes }
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes /*
4727c8fdcbdSMatthew Dillon  * do_sigprocmask() - MP SAFE ONLY IF p == curproc
4737c8fdcbdSMatthew Dillon  *
4747c8fdcbdSMatthew Dillon  *	Manipulate signal mask.  This routine is MP SAFE *ONLY* if
4757c8fdcbdSMatthew Dillon  *	p == curproc.  Also remember that in order to remain MP SAFE
4767c8fdcbdSMatthew Dillon  *	no spl*() calls may be made.
477df8bae1dSRodney W. Grimes  */
4782c42a146SMarcel Moolenaar static int
479645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
4802c42a146SMarcel Moolenaar 	struct proc *p;
4812c42a146SMarcel Moolenaar 	int how;
4822c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
483645682fdSLuoqi Chen 	int old;
4842c42a146SMarcel Moolenaar {
4852c42a146SMarcel Moolenaar 	int error;
4862c42a146SMarcel Moolenaar 
4872c42a146SMarcel Moolenaar 	if (oset != NULL)
4882c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
4892c42a146SMarcel Moolenaar 
4902c42a146SMarcel Moolenaar 	error = 0;
4912c42a146SMarcel Moolenaar 	if (set != NULL) {
4922c42a146SMarcel Moolenaar 		switch (how) {
4932c42a146SMarcel Moolenaar 		case SIG_BLOCK:
494645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
4952c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
4962c42a146SMarcel Moolenaar 			break;
4972c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
4982c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
4992c42a146SMarcel Moolenaar 			break;
5002c42a146SMarcel Moolenaar 		case SIG_SETMASK:
501645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
502645682fdSLuoqi Chen 			if (old)
503645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
504645682fdSLuoqi Chen 			else
5052c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
5062c42a146SMarcel Moolenaar 			break;
5072c42a146SMarcel Moolenaar 		default:
5082c42a146SMarcel Moolenaar 			error = EINVAL;
5092c42a146SMarcel Moolenaar 			break;
5102c42a146SMarcel Moolenaar 		}
5112c42a146SMarcel Moolenaar 	}
5122c42a146SMarcel Moolenaar 	return (error);
5132c42a146SMarcel Moolenaar }
5142c42a146SMarcel Moolenaar 
5157c8fdcbdSMatthew Dillon /*
5167c8fdcbdSMatthew Dillon  * sigprocmask() - MP SAFE
5177c8fdcbdSMatthew Dillon  */
5187c8fdcbdSMatthew Dillon 
519d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
520df8bae1dSRodney W. Grimes struct sigprocmask_args {
521df8bae1dSRodney W. Grimes 	int	how;
5222c42a146SMarcel Moolenaar 	const sigset_t *set;
5232c42a146SMarcel Moolenaar 	sigset_t *oset;
524df8bae1dSRodney W. Grimes };
525d2d3e875SBruce Evans #endif
52626f9a767SRodney W. Grimes int
527cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
528df8bae1dSRodney W. Grimes 	register struct proc *p;
529df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
530df8bae1dSRodney W. Grimes {
5312c42a146SMarcel Moolenaar 	sigset_t set, oset;
5322c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5332c42a146SMarcel Moolenaar 	int error;
534df8bae1dSRodney W. Grimes 
5356f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5366f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5372c42a146SMarcel Moolenaar 	if (setp) {
5386f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5392c42a146SMarcel Moolenaar 		if (error)
5402c42a146SMarcel Moolenaar 			return (error);
541df8bae1dSRodney W. Grimes 	}
542645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5432c42a146SMarcel Moolenaar 	if (osetp && !error) {
5446f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5452c42a146SMarcel Moolenaar 	}
5462c42a146SMarcel Moolenaar 	return (error);
5472c42a146SMarcel Moolenaar }
5482c42a146SMarcel Moolenaar 
54931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5507c8fdcbdSMatthew Dillon /*
5517c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5527c8fdcbdSMatthew Dillon  */
5532c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5542c42a146SMarcel Moolenaar struct osigprocmask_args {
5552c42a146SMarcel Moolenaar 	int	how;
5562c42a146SMarcel Moolenaar 	osigset_t mask;
5572c42a146SMarcel Moolenaar };
5582c42a146SMarcel Moolenaar #endif
5592c42a146SMarcel Moolenaar int
5602c42a146SMarcel Moolenaar osigprocmask(p, uap)
5612c42a146SMarcel Moolenaar 	register struct proc *p;
5622c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5632c42a146SMarcel Moolenaar {
5642c42a146SMarcel Moolenaar 	sigset_t set, oset;
5652c42a146SMarcel Moolenaar 	int error;
5662c42a146SMarcel Moolenaar 
5672c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
568645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
5692c42a146SMarcel Moolenaar 	SIG2OSIG(oset, p->p_retval[0]);
570df8bae1dSRodney W. Grimes 	return (error);
571df8bae1dSRodney W. Grimes }
57231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
573df8bae1dSRodney W. Grimes 
574d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
575df8bae1dSRodney W. Grimes struct sigpending_args {
5762c42a146SMarcel Moolenaar 	sigset_t	*set;
577df8bae1dSRodney W. Grimes };
578d2d3e875SBruce Evans #endif
579df8bae1dSRodney W. Grimes /* ARGSUSED */
58026f9a767SRodney W. Grimes int
581cb226aaaSPoul-Henning Kamp sigpending(p, uap)
582df8bae1dSRodney W. Grimes 	struct proc *p;
583df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
584df8bae1dSRodney W. Grimes {
585df8bae1dSRodney W. Grimes 
5866f841fb7SMarcel Moolenaar 	return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
5872c42a146SMarcel Moolenaar }
5882c42a146SMarcel Moolenaar 
58931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5902c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5912c42a146SMarcel Moolenaar struct osigpending_args {
5922c42a146SMarcel Moolenaar 	int	dummy;
5932c42a146SMarcel Moolenaar };
5942c42a146SMarcel Moolenaar #endif
5952c42a146SMarcel Moolenaar /* ARGSUSED */
5962c42a146SMarcel Moolenaar int
5972c42a146SMarcel Moolenaar osigpending(p, uap)
5982c42a146SMarcel Moolenaar 	struct proc *p;
5992c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6002c42a146SMarcel Moolenaar {
6012c42a146SMarcel Moolenaar 
6022c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_siglist, p->p_retval[0]);
603df8bae1dSRodney W. Grimes 	return (0);
604df8bae1dSRodney W. Grimes }
60531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
608df8bae1dSRodney W. Grimes /*
609df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
610df8bae1dSRodney W. Grimes  */
611d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
612df8bae1dSRodney W. Grimes struct osigvec_args {
613df8bae1dSRodney W. Grimes 	int	signum;
614df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
615df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
616df8bae1dSRodney W. Grimes };
617d2d3e875SBruce Evans #endif
618df8bae1dSRodney W. Grimes /* ARGSUSED */
61926f9a767SRodney W. Grimes int
620cb226aaaSPoul-Henning Kamp osigvec(p, uap)
621df8bae1dSRodney W. Grimes 	struct proc *p;
622df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
623df8bae1dSRodney W. Grimes {
624df8bae1dSRodney W. Grimes 	struct sigvec vec;
6252c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6262c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6272c42a146SMarcel Moolenaar 	int error;
628df8bae1dSRodney W. Grimes 
6296f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6306f841fb7SMarcel Moolenaar 		return (EINVAL);
6316f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6326f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6332c42a146SMarcel Moolenaar 	if (nsap) {
6346f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
6352c42a146SMarcel Moolenaar 		if (error)
636df8bae1dSRodney W. Grimes 			return (error);
6372c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
6382c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
6392c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
6402c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
641df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
6422c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
643df8bae1dSRodney W. Grimes #endif
644df8bae1dSRodney W. Grimes 	}
645645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
6462c42a146SMarcel Moolenaar 	if (osap && !error) {
6472c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
6482c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
6492c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
6502c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
6512c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
6522c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
6532c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
6542c42a146SMarcel Moolenaar #endif
6556f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
6562c42a146SMarcel Moolenaar 	}
6572c42a146SMarcel Moolenaar 	return (error);
658df8bae1dSRodney W. Grimes }
659df8bae1dSRodney W. Grimes 
660d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
661df8bae1dSRodney W. Grimes struct osigblock_args {
662df8bae1dSRodney W. Grimes 	int	mask;
663df8bae1dSRodney W. Grimes };
664d2d3e875SBruce Evans #endif
66526f9a767SRodney W. Grimes int
666cb226aaaSPoul-Henning Kamp osigblock(p, uap)
667df8bae1dSRodney W. Grimes 	register struct proc *p;
668df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
669df8bae1dSRodney W. Grimes {
6702c42a146SMarcel Moolenaar 	sigset_t set;
671df8bae1dSRodney W. Grimes 
6722c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6732c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
674df8bae1dSRodney W. Grimes 	(void) splhigh();
6752c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
6762c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
677df8bae1dSRodney W. Grimes 	(void) spl0();
678df8bae1dSRodney W. Grimes 	return (0);
679df8bae1dSRodney W. Grimes }
680df8bae1dSRodney W. Grimes 
681d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
682df8bae1dSRodney W. Grimes struct osigsetmask_args {
683df8bae1dSRodney W. Grimes 	int	mask;
684df8bae1dSRodney W. Grimes };
685d2d3e875SBruce Evans #endif
68626f9a767SRodney W. Grimes int
687cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
688df8bae1dSRodney W. Grimes 	struct proc *p;
689df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
690df8bae1dSRodney W. Grimes {
6912c42a146SMarcel Moolenaar 	sigset_t set;
692df8bae1dSRodney W. Grimes 
6932c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6942c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
695df8bae1dSRodney W. Grimes 	(void) splhigh();
6962c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
697645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
698df8bae1dSRodney W. Grimes 	(void) spl0();
699df8bae1dSRodney W. Grimes 	return (0);
700df8bae1dSRodney W. Grimes }
701df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
702df8bae1dSRodney W. Grimes 
703df8bae1dSRodney W. Grimes /*
704df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
705df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
706df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
707df8bae1dSRodney W. Grimes  */
708d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
709df8bae1dSRodney W. Grimes struct sigsuspend_args {
7102c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
711df8bae1dSRodney W. Grimes };
712d2d3e875SBruce Evans #endif
713df8bae1dSRodney W. Grimes /* ARGSUSED */
71426f9a767SRodney W. Grimes int
715cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
716df8bae1dSRodney W. Grimes 	register struct proc *p;
717df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
718df8bae1dSRodney W. Grimes {
7192c42a146SMarcel Moolenaar 	sigset_t mask;
720df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
7212c42a146SMarcel Moolenaar 	int error;
7222c42a146SMarcel Moolenaar 
7236f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
7242c42a146SMarcel Moolenaar 	if (error)
7252c42a146SMarcel Moolenaar 		return (error);
726df8bae1dSRodney W. Grimes 
727df8bae1dSRodney W. Grimes 	/*
728645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
729df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
730df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
731df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
732df8bae1dSRodney W. Grimes 	 * to indicate this.
733df8bae1dSRodney W. Grimes 	 */
7346626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
735645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
736645682fdSLuoqi Chen 
737645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
7382c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
7392c42a146SMarcel Moolenaar 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
7402c42a146SMarcel Moolenaar 		/* void */;
7412c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
7422c42a146SMarcel Moolenaar 	return (EINTR);
7432c42a146SMarcel Moolenaar }
7442c42a146SMarcel Moolenaar 
74531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7462c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7472c42a146SMarcel Moolenaar struct osigsuspend_args {
7482c42a146SMarcel Moolenaar 	osigset_t mask;
7492c42a146SMarcel Moolenaar };
7502c42a146SMarcel Moolenaar #endif
7512c42a146SMarcel Moolenaar /* ARGSUSED */
7522c42a146SMarcel Moolenaar int
7532c42a146SMarcel Moolenaar osigsuspend(p, uap)
7542c42a146SMarcel Moolenaar 	register struct proc *p;
7552c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
7562c42a146SMarcel Moolenaar {
757645682fdSLuoqi Chen 	sigset_t mask;
7582c42a146SMarcel Moolenaar 	register struct sigacts *ps = p->p_sigacts;
7592c42a146SMarcel Moolenaar 
7602c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
761645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
762645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
763645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
764645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
765645682fdSLuoqi Chen 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
766df8bae1dSRodney W. Grimes 		/* void */;
767df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
768df8bae1dSRodney W. Grimes 	return (EINTR);
769df8bae1dSRodney W. Grimes }
77031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
773d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
774df8bae1dSRodney W. Grimes struct osigstack_args {
775df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
776df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
777df8bae1dSRodney W. Grimes };
778d2d3e875SBruce Evans #endif
779df8bae1dSRodney W. Grimes /* ARGSUSED */
78026f9a767SRodney W. Grimes int
781cb226aaaSPoul-Henning Kamp osigstack(p, uap)
782df8bae1dSRodney W. Grimes 	struct proc *p;
783df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
784df8bae1dSRodney W. Grimes {
785df8bae1dSRodney W. Grimes 	struct sigstack ss;
786d034d459SMarcel Moolenaar 	int error;
787df8bae1dSRodney W. Grimes 
788d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
789645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
790d034d459SMarcel Moolenaar 		ss.ss_onstack = sigonstack(cpu_getstack(p));
791d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
792d034d459SMarcel Moolenaar 		if (error)
793df8bae1dSRodney W. Grimes 			return (error);
794d034d459SMarcel Moolenaar 	}
795d034d459SMarcel Moolenaar 
796d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
797d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
798d034d459SMarcel Moolenaar 			return (error);
799645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
800645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
801645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
802645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
803df8bae1dSRodney W. Grimes 	}
804d034d459SMarcel Moolenaar 	return (0);
805df8bae1dSRodney W. Grimes }
806df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
807df8bae1dSRodney W. Grimes 
808d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
809df8bae1dSRodney W. Grimes struct sigaltstack_args {
8102c42a146SMarcel Moolenaar 	stack_t	*ss;
8112c42a146SMarcel Moolenaar 	stack_t	*oss;
812df8bae1dSRodney W. Grimes };
813d2d3e875SBruce Evans #endif
814df8bae1dSRodney W. Grimes /* ARGSUSED */
81526f9a767SRodney W. Grimes int
816cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
817df8bae1dSRodney W. Grimes 	struct proc *p;
818df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
819df8bae1dSRodney W. Grimes {
8202c42a146SMarcel Moolenaar 	stack_t ss;
821d034d459SMarcel Moolenaar 	int error, oonstack;
822df8bae1dSRodney W. Grimes 
823d034d459SMarcel Moolenaar 	oonstack = sigonstack(cpu_getstack(p));
824d034d459SMarcel Moolenaar 
825d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
826d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
827d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
828d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
829d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
830df8bae1dSRodney W. Grimes 			return (error);
831df8bae1dSRodney W. Grimes 	}
832d034d459SMarcel Moolenaar 
833d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
834d034d459SMarcel Moolenaar 		if (oonstack)
835d034d459SMarcel Moolenaar 			return (EPERM);
836d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
837d034d459SMarcel Moolenaar 			return (error);
838d034d459SMarcel Moolenaar 		if ((ss.ss_flags & ~SS_DISABLE) != 0)
839d034d459SMarcel Moolenaar 			return (EINVAL);
840d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
841806d7daaSMarcel Moolenaar 			if (ss.ss_size < p->p_sysent->sv_minsigstksz)
842df8bae1dSRodney W. Grimes 				return (ENOMEM);
843645682fdSLuoqi Chen 			p->p_sigstk = ss;
844d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
845d034d459SMarcel Moolenaar 		} else
846d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
847d034d459SMarcel Moolenaar 	}
848df8bae1dSRodney W. Grimes 	return (0);
849df8bae1dSRodney W. Grimes }
850df8bae1dSRodney W. Grimes 
851d93f860cSPoul-Henning Kamp /*
852d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
853d93f860cSPoul-Henning Kamp  * cp is calling process.
854d93f860cSPoul-Henning Kamp  */
855d93f860cSPoul-Henning Kamp int
8562c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
857d93f860cSPoul-Henning Kamp 	register struct proc *cp;
8582c42a146SMarcel Moolenaar 	int sig, pgid, all;
859d93f860cSPoul-Henning Kamp {
860d93f860cSPoul-Henning Kamp 	register struct proc *p;
861d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
862d93f860cSPoul-Henning Kamp 	int nfound = 0;
863d93f860cSPoul-Henning Kamp 
864553629ebSJake Burkholder 	if (all) {
865d93f860cSPoul-Henning Kamp 		/*
866d93f860cSPoul-Henning Kamp 		 * broadcast
867d93f860cSPoul-Henning Kamp 		 */
868c0c25570SJake Burkholder 		ALLPROC_LOCK(AP_SHARED);
8692e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
870d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
871a9e0361bSPoul-Henning Kamp 			    p == cp || !CANSIGNAL(cp, p, sig))
872d93f860cSPoul-Henning Kamp 				continue;
873d93f860cSPoul-Henning Kamp 			nfound++;
8742c42a146SMarcel Moolenaar 			if (sig)
8752c42a146SMarcel Moolenaar 				psignal(p, sig);
876d93f860cSPoul-Henning Kamp 		}
877c0c25570SJake Burkholder 		ALLPROC_LOCK(AP_RELEASE);
878553629ebSJake Burkholder 	} else {
879d93f860cSPoul-Henning Kamp 		if (pgid == 0)
880d93f860cSPoul-Henning Kamp 			/*
881d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
882d93f860cSPoul-Henning Kamp 			 */
883d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
884d93f860cSPoul-Henning Kamp 		else {
885d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
886d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
887d93f860cSPoul-Henning Kamp 				return (ESRCH);
888d93f860cSPoul-Henning Kamp 		}
8892e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
890d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
891d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
892a9e0361bSPoul-Henning Kamp 			    !CANSIGNAL(cp, p, sig))
893d93f860cSPoul-Henning Kamp 				continue;
894d93f860cSPoul-Henning Kamp 			nfound++;
8952c42a146SMarcel Moolenaar 			if (sig)
8962c42a146SMarcel Moolenaar 				psignal(p, sig);
897d93f860cSPoul-Henning Kamp 		}
898d93f860cSPoul-Henning Kamp 	}
899d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
900d93f860cSPoul-Henning Kamp }
901d93f860cSPoul-Henning Kamp 
902d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
903df8bae1dSRodney W. Grimes struct kill_args {
904df8bae1dSRodney W. Grimes 	int	pid;
905df8bae1dSRodney W. Grimes 	int	signum;
906df8bae1dSRodney W. Grimes };
907d2d3e875SBruce Evans #endif
908df8bae1dSRodney W. Grimes /* ARGSUSED */
90926f9a767SRodney W. Grimes int
910cb226aaaSPoul-Henning Kamp kill(cp, uap)
911df8bae1dSRodney W. Grimes 	register struct proc *cp;
912df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
913df8bae1dSRodney W. Grimes {
914df8bae1dSRodney W. Grimes 	register struct proc *p;
915df8bae1dSRodney W. Grimes 
9162c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
917df8bae1dSRodney W. Grimes 		return (EINVAL);
918df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
919df8bae1dSRodney W. Grimes 		/* kill single process */
920df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
921df8bae1dSRodney W. Grimes 			return (ESRCH);
922a9e0361bSPoul-Henning Kamp 		if (!CANSIGNAL(cp, p, uap->signum))
923df8bae1dSRodney W. Grimes 			return (EPERM);
924df8bae1dSRodney W. Grimes 		if (uap->signum)
925df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
926df8bae1dSRodney W. Grimes 		return (0);
927df8bae1dSRodney W. Grimes 	}
928df8bae1dSRodney W. Grimes 	switch (uap->pid) {
929df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
930df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
931df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
932df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
933df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
934df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
935df8bae1dSRodney W. Grimes 	}
936df8bae1dSRodney W. Grimes 	/* NOTREACHED */
937df8bae1dSRodney W. Grimes }
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
940d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
941df8bae1dSRodney W. Grimes struct okillpg_args {
942df8bae1dSRodney W. Grimes 	int	pgid;
943df8bae1dSRodney W. Grimes 	int	signum;
944df8bae1dSRodney W. Grimes };
945d2d3e875SBruce Evans #endif
946df8bae1dSRodney W. Grimes /* ARGSUSED */
94726f9a767SRodney W. Grimes int
948cb226aaaSPoul-Henning Kamp okillpg(p, uap)
949df8bae1dSRodney W. Grimes 	struct proc *p;
950df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
951df8bae1dSRodney W. Grimes {
952df8bae1dSRodney W. Grimes 
9532c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
954df8bae1dSRodney W. Grimes 		return (EINVAL);
955df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
956df8bae1dSRodney W. Grimes }
957df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
958df8bae1dSRodney W. Grimes 
959df8bae1dSRodney W. Grimes /*
960df8bae1dSRodney W. Grimes  * Send a signal to a process group.
961df8bae1dSRodney W. Grimes  */
962df8bae1dSRodney W. Grimes void
9632c42a146SMarcel Moolenaar gsignal(pgid, sig)
9642c42a146SMarcel Moolenaar 	int pgid, sig;
965df8bae1dSRodney W. Grimes {
966df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
967df8bae1dSRodney W. Grimes 
968df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
9692c42a146SMarcel Moolenaar 		pgsignal(pgrp, sig, 0);
970df8bae1dSRodney W. Grimes }
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes /*
973df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
974df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
975df8bae1dSRodney W. Grimes  */
976df8bae1dSRodney W. Grimes void
9772c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
978df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
9792c42a146SMarcel Moolenaar 	int sig, checkctty;
980df8bae1dSRodney W. Grimes {
981df8bae1dSRodney W. Grimes 	register struct proc *p;
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	if (pgrp)
9842e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
985df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
9862c42a146SMarcel Moolenaar 				psignal(p, sig);
987df8bae1dSRodney W. Grimes }
988df8bae1dSRodney W. Grimes 
989df8bae1dSRodney W. Grimes /*
990df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
991df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
992df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
993df8bae1dSRodney W. Grimes  */
994df8bae1dSRodney W. Grimes void
9952c42a146SMarcel Moolenaar trapsignal(p, sig, code)
996df8bae1dSRodney W. Grimes 	struct proc *p;
9972c42a146SMarcel Moolenaar 	register int sig;
9988674077aSJeffrey Hsu 	u_long code;
999df8bae1dSRodney W. Grimes {
1000df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1001df8bae1dSRodney W. Grimes 
10022c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1003d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1004df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1005df8bae1dSRodney W. Grimes #ifdef KTRACE
1006df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
10072c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
10082c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1009df8bae1dSRodney W. Grimes #endif
10102c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
10112c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
10122c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
10132c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
10142c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
10152c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1016289ccde0SPeter Wemm 			/*
10172c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1018289ccde0SPeter Wemm 			 */
10192c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
10202c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
10212c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
10222c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
10232c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1024dedc04feSPeter Wemm 		}
10256f841fb7SMarcel Moolenaar 	} else {
10266626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
10272c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
10282c42a146SMarcel Moolenaar 		psignal(p, sig);
1029df8bae1dSRodney W. Grimes 	}
1030df8bae1dSRodney W. Grimes }
1031df8bae1dSRodney W. Grimes 
1032df8bae1dSRodney W. Grimes /*
1033df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1034df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1035df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1036df8bae1dSRodney W. Grimes  *
1037df8bae1dSRodney W. Grimes  * Exceptions:
1038df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1039df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1040df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1041df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1042df8bae1dSRodney W. Grimes  *
1043df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1044df8bae1dSRodney W. Grimes  */
1045df8bae1dSRodney W. Grimes void
10462c42a146SMarcel Moolenaar psignal(p, sig)
1047df8bae1dSRodney W. Grimes 	register struct proc *p;
10482c42a146SMarcel Moolenaar 	register int sig;
1049df8bae1dSRodney W. Grimes {
1050df8bae1dSRodney W. Grimes 	register int s, prop;
1051df8bae1dSRodney W. Grimes 	register sig_t action;
1052df8bae1dSRodney W. Grimes 
10532c42a146SMarcel Moolenaar 	if (sig > _SIG_MAXSIG || sig <= 0) {
10542c42a146SMarcel Moolenaar 		printf("psignal: signal %d\n", sig);
1055df8bae1dSRodney W. Grimes 		panic("psignal signal number");
10562a024a2bSSean Eric Fagan 	}
10572c42a146SMarcel Moolenaar 
1058cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1059cb679c38SJonathan Lemon 
10602c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1061df8bae1dSRodney W. Grimes 
1062df8bae1dSRodney W. Grimes 	/*
10632a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
10642a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
10652a024a2bSSean Eric Fagan 	 * a chance, as well.
1066df8bae1dSRodney W. Grimes 	 */
10672a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1068df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1069df8bae1dSRodney W. Grimes 	else {
1070df8bae1dSRodney W. Grimes 		/*
1071df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1072df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1073df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1074df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1075df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1076df8bae1dSRodney W. Grimes 		 */
10772c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1078df8bae1dSRodney W. Grimes 			return;
10792c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1080df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
10812c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1082df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1083df8bae1dSRodney W. Grimes 		else
1084df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1085df8bae1dSRodney W. Grimes 	}
1086df8bae1dSRodney W. Grimes 
10875192404aSJohn Baldwin 	mtx_enter(&sched_lock, MTX_SPIN);
1088df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1089df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
1090df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
10915192404aSJohn Baldwin 	mtx_exit(&sched_lock, MTX_SPIN);
1092df8bae1dSRodney W. Grimes 
1093df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
10942c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1095df8bae1dSRodney W. Grimes 
1096df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1097df8bae1dSRodney W. Grimes 		/*
1098df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1099df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1100df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1101df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1102df8bae1dSRodney W. Grimes 		 */
1103df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1104df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
1105df8bae1dSRodney W. Grimes 		        return;
11062c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1107df8bae1dSRodney W. Grimes 	}
11082c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes 	/*
1111df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1112df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1113df8bae1dSRodney W. Grimes 	 */
11141c32c37cSJohn Baldwin 	mtx_enter(&sched_lock, MTX_SPIN);
11151c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
11161c32c37cSJohn Baldwin 		mtx_exit(&sched_lock, MTX_SPIN);
1117df8bae1dSRodney W. Grimes 		return;
11181c32c37cSJohn Baldwin 	}
1119df8bae1dSRodney W. Grimes 	s = splhigh();
1120df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1121df8bae1dSRodney W. Grimes 
1122df8bae1dSRodney W. Grimes 	case SSLEEP:
1123df8bae1dSRodney W. Grimes 		/*
1124df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1125df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1126df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1127df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1128df8bae1dSRodney W. Grimes 		 */
11293e6831f5SJohn Baldwin 		if ((p->p_flag & P_SINTR) == 0) {
11303e6831f5SJohn Baldwin 			mtx_exit(&sched_lock, MTX_SPIN);
1131df8bae1dSRodney W. Grimes 			goto out;
11323e6831f5SJohn Baldwin 		}
1133df8bae1dSRodney W. Grimes 		/*
1134df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1135df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1136df8bae1dSRodney W. Grimes 		 * for the parent.
1137df8bae1dSRodney W. Grimes 		 */
1138df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1139df8bae1dSRodney W. Grimes 			goto run;
11403e6831f5SJohn Baldwin 		mtx_exit(&sched_lock, MTX_SPIN);
1141df8bae1dSRodney W. Grimes 		/*
1142df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1143df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1144df8bae1dSRodney W. Grimes 		 * be awakened.
1145df8bae1dSRodney W. Grimes 		 */
1146df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
11472c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1148df8bae1dSRodney W. Grimes 			goto out;
1149df8bae1dSRodney W. Grimes 		}
1150df8bae1dSRodney W. Grimes 		/*
1151df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1152df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1153df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1154df8bae1dSRodney W. Grimes 		 * cause the process to run.
1155df8bae1dSRodney W. Grimes 		 */
1156df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
11573e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1158df8bae1dSRodney W. Grimes 				goto runfast;
1159df8bae1dSRodney W. Grimes 			/*
1160df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1161df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1162df8bae1dSRodney W. Grimes 			 */
1163df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1164df8bae1dSRodney W. Grimes 				goto out;
11652c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
11662c42a146SMarcel Moolenaar 			p->p_xstat = sig;
116798f03f90SJake Burkholder 			PROCTREE_LOCK(PT_SHARED);
1168645682fdSLuoqi Chen 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1169df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1170df8bae1dSRodney W. Grimes 			stop(p);
117198f03f90SJake Burkholder 			PROCTREE_LOCK(PT_RELEASE);
1172df8bae1dSRodney W. Grimes 			goto out;
11733e6831f5SJohn Baldwin 		} else
1174df8bae1dSRodney W. Grimes 			goto runfast;
1175df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1176df8bae1dSRodney W. Grimes 
1177df8bae1dSRodney W. Grimes 	case SSTOP:
1178df8bae1dSRodney W. Grimes 		/*
1179df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1180df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1181df8bae1dSRodney W. Grimes 		 */
11823e6831f5SJohn Baldwin 		if (p->p_flag & P_TRACED) {
11833e6831f5SJohn Baldwin 			mtx_exit(&sched_lock, MTX_SPIN);
1184df8bae1dSRodney W. Grimes 			goto out;
11853e6831f5SJohn Baldwin 		}
11863e6831f5SJohn Baldwin 		mtx_exit(&sched_lock, MTX_SPIN);
1187df8bae1dSRodney W. Grimes 
1188df8bae1dSRodney W. Grimes 		/*
1189df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1190df8bae1dSRodney W. Grimes 		 */
11912c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1192df8bae1dSRodney W. Grimes 			goto runfast;
1193df8bae1dSRodney W. Grimes 
1194df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1195df8bae1dSRodney W. Grimes 			/*
1196df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1197df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1198df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1199df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1200df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1201df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1202df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1203df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1204df8bae1dSRodney W. Grimes 			 */
1205df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
12062c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1207df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1208df8bae1dSRodney W. Grimes 				goto runfast;
12093e6831f5SJohn Baldwin 			mtx_enter(&sched_lock, MTX_SPIN);
1210df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
1211df8bae1dSRodney W. Grimes 				goto run;
1212df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
12133e6831f5SJohn Baldwin 			mtx_exit(&sched_lock, MTX_SPIN);
1214df8bae1dSRodney W. Grimes 			goto out;
1215df8bae1dSRodney W. Grimes 		}
1216df8bae1dSRodney W. Grimes 
1217df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1218df8bae1dSRodney W. Grimes 			/*
1219df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1220df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1221df8bae1dSRodney W. Grimes 			 */
12222c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1223df8bae1dSRodney W. Grimes 			goto out;
1224df8bae1dSRodney W. Grimes 		}
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes 		/*
1227df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1228df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1229df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1230df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1231df8bae1dSRodney W. Grimes 		 */
12324bfba0cfSJohn Baldwin 		mtx_enter(&sched_lock, MTX_SPIN);
1233238510fcSJason Evans 		if (p->p_wchan && p->p_flag & P_SINTR) {
1234238510fcSJason Evans 			if (p->p_flag & P_CVWAITQ)
1235238510fcSJason Evans 				cv_waitq_remove(p);
1236238510fcSJason Evans 			else
1237df8bae1dSRodney W. Grimes 				unsleep(p);
1238238510fcSJason Evans 		}
12393e6831f5SJohn Baldwin 		mtx_exit(&sched_lock, MTX_SPIN);
1240df8bae1dSRodney W. Grimes 		goto out;
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes 	default:
1243df8bae1dSRodney W. Grimes 		/*
1244df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1245df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1246df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1247df8bae1dSRodney W. Grimes 		 */
12484bfba0cfSJohn Baldwin 		if (p == curproc) {
12494bfba0cfSJohn Baldwin 			mtx_exit(&sched_lock, MTX_SPIN);
1250df8bae1dSRodney W. Grimes 			signotify(p);
12514bfba0cfSJohn Baldwin 		}
12523163861cSTor Egge #ifdef SMP
12534bfba0cfSJohn Baldwin 		else if (p->p_stat == SRUN) {
12544bfba0cfSJohn Baldwin 			mtx_exit(&sched_lock, MTX_SPIN);
12553163861cSTor Egge 			forward_signal(p);
12564bfba0cfSJohn Baldwin 		}
12573163861cSTor Egge #endif
12583e6831f5SJohn Baldwin 		else
12593e6831f5SJohn Baldwin 			mtx_exit(&sched_lock, MTX_SPIN);
1260df8bae1dSRodney W. Grimes 		goto out;
1261df8bae1dSRodney W. Grimes 	}
1262df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1263df8bae1dSRodney W. Grimes 
1264df8bae1dSRodney W. Grimes runfast:
1265df8bae1dSRodney W. Grimes 	/*
1266df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1267df8bae1dSRodney W. Grimes 	 */
12683e6831f5SJohn Baldwin 	mtx_enter(&sched_lock, MTX_SPIN);
1269df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
1270df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
1271df8bae1dSRodney W. Grimes run:
12723e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
12733e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1274df8bae1dSRodney W. Grimes 	setrunnable(p);
12751c32c37cSJohn Baldwin 	mtx_exit(&sched_lock, MTX_SPIN);
12763e6831f5SJohn Baldwin out:
12773e6831f5SJohn Baldwin 	/* If we jump here, sched_lock should not be owned. */
12783e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1279df8bae1dSRodney W. Grimes 	splx(s);
1280df8bae1dSRodney W. Grimes }
1281df8bae1dSRodney W. Grimes 
1282df8bae1dSRodney W. Grimes /*
1283df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1284df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1285df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1286df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1287df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1288df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1289df8bae1dSRodney W. Grimes  * sequence is
1290df8bae1dSRodney W. Grimes  *
12912c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
12922c42a146SMarcel Moolenaar  *		postsig(sig);
1293df8bae1dSRodney W. Grimes  */
129426f9a767SRodney W. Grimes int
1295df8bae1dSRodney W. Grimes issignal(p)
1296df8bae1dSRodney W. Grimes 	register struct proc *p;
1297df8bae1dSRodney W. Grimes {
12982c42a146SMarcel Moolenaar 	sigset_t mask;
12992c42a146SMarcel Moolenaar 	register int sig, prop;
1300df8bae1dSRodney W. Grimes 
1301df8bae1dSRodney W. Grimes 	for (;;) {
13022a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
13032a024a2bSSean Eric Fagan 
13042c42a146SMarcel Moolenaar 		mask = p->p_siglist;
13052c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1306df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
13072c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
13082c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))	 	/* no signal to send */
1309df8bae1dSRodney W. Grimes 			return (0);
13102c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
13112c42a146SMarcel Moolenaar 		prop = sigprop(sig);
13122a024a2bSSean Eric Fagan 
13132c42a146SMarcel Moolenaar 		STOPEVENT(p, S_SIG, sig);
13142a024a2bSSean Eric Fagan 
1315df8bae1dSRodney W. Grimes 		/*
1316df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1317df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1318df8bae1dSRodney W. Grimes 		 */
13192c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
13202c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1321df8bae1dSRodney W. Grimes 			continue;
1322df8bae1dSRodney W. Grimes 		}
1323df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1324df8bae1dSRodney W. Grimes 			/*
1325df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1326df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1327df8bae1dSRodney W. Grimes 			 */
13282c42a146SMarcel Moolenaar 			p->p_xstat = sig;
132998f03f90SJake Burkholder 			PROCTREE_LOCK(PT_SHARED);
1330df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1331df8bae1dSRodney W. Grimes 			do {
1332df8bae1dSRodney W. Grimes 				stop(p);
133398f03f90SJake Burkholder 				PROCTREE_LOCK(PT_RELEASE);
133420cdcc5bSJohn Baldwin 				mtx_enter(&sched_lock, MTX_SPIN);
13357da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
1336df8bae1dSRodney W. Grimes 				mi_switch();
133720cdcc5bSJohn Baldwin 				mtx_exit(&sched_lock, MTX_SPIN);
133820cdcc5bSJohn Baldwin 				PICKUP_GIANT();
133998f03f90SJake Burkholder 				PROCTREE_LOCK(PT_SHARED);
13402a024a2bSSean Eric Fagan 			} while (!trace_req(p)
13412a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1342ef829407SJohn Baldwin 			PROCTREE_LOCK(PT_RELEASE);
1343df8bae1dSRodney W. Grimes 
1344df8bae1dSRodney W. Grimes 			/*
1345df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1346df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1347df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1348df8bae1dSRodney W. Grimes 			 */
1349df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1350df8bae1dSRodney W. Grimes 				continue;
1351df8bae1dSRodney W. Grimes 
1352df8bae1dSRodney W. Grimes 			/*
1353df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1354df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1355df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1356df8bae1dSRodney W. Grimes 			 */
13572c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
13582c42a146SMarcel Moolenaar 			sig = p->p_xstat;
13592c42a146SMarcel Moolenaar 			if (sig == 0)
1360df8bae1dSRodney W. Grimes 				continue;
1361df8bae1dSRodney W. Grimes 
1362df8bae1dSRodney W. Grimes 			/*
1363df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1364df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1365df8bae1dSRodney W. Grimes 			 */
13662c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
13672c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1368df8bae1dSRodney W. Grimes 				continue;
1369df8bae1dSRodney W. Grimes 		}
1370df8bae1dSRodney W. Grimes 
1371df8bae1dSRodney W. Grimes 		/*
1372df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1373df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1374df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1375df8bae1dSRodney W. Grimes 		 */
13762c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1377df8bae1dSRodney W. Grimes 
13780b53fbe8SBruce Evans 		case (int)SIG_DFL:
1379df8bae1dSRodney W. Grimes 			/*
1380df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1381df8bae1dSRodney W. Grimes 			 */
1382df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1383df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1384df8bae1dSRodney W. Grimes 				/*
1385df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1386df8bae1dSRodney W. Grimes 				 * in init? XXX
1387df8bae1dSRodney W. Grimes 				 */
1388d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
13892c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1390df8bae1dSRodney W. Grimes #endif
1391df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1392df8bae1dSRodney W. Grimes 			}
1393df8bae1dSRodney W. Grimes 			/*
1394df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1395df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1396df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1397df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1398df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1399df8bae1dSRodney W. Grimes 			 */
1400df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1401df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1402df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1403df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1404df8bae1dSRodney W. Grimes 					break;	/* == ignore */
14052c42a146SMarcel Moolenaar 				p->p_xstat = sig;
140698f03f90SJake Burkholder 				PROCTREE_LOCK(PT_SHARED);
1407df8bae1dSRodney W. Grimes 				stop(p);
1408645682fdSLuoqi Chen 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1409df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
141098f03f90SJake Burkholder 				PROCTREE_LOCK(PT_RELEASE);
141120cdcc5bSJohn Baldwin 				mtx_enter(&sched_lock, MTX_SPIN);
14127da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
1413df8bae1dSRodney W. Grimes 				mi_switch();
141420cdcc5bSJohn Baldwin 				mtx_exit(&sched_lock, MTX_SPIN);
141520cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1416df8bae1dSRodney W. Grimes 				break;
1417df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1418df8bae1dSRodney W. Grimes 				/*
1419df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1420df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1421df8bae1dSRodney W. Grimes 				 */
1422df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1423df8bae1dSRodney W. Grimes 			} else
14242c42a146SMarcel Moolenaar 				return (sig);
1425df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1426df8bae1dSRodney W. Grimes 
14270b53fbe8SBruce Evans 		case (int)SIG_IGN:
1428df8bae1dSRodney W. Grimes 			/*
1429df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1430df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1431df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1432df8bae1dSRodney W. Grimes 			 */
1433df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1434df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1435df8bae1dSRodney W. Grimes 				printf("issignal\n");
1436df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1437df8bae1dSRodney W. Grimes 
1438df8bae1dSRodney W. Grimes 		default:
1439df8bae1dSRodney W. Grimes 			/*
1440df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1441df8bae1dSRodney W. Grimes 			 * postsig() process it.
1442df8bae1dSRodney W. Grimes 			 */
14432c42a146SMarcel Moolenaar 			return (sig);
1444df8bae1dSRodney W. Grimes 		}
14452c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1446df8bae1dSRodney W. Grimes 	}
1447df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1448df8bae1dSRodney W. Grimes }
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes /*
1451df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1452df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
145398f03f90SJake Burkholder  * on the run queue.  Must be called with at least a shared hold of the
145498f03f90SJake Burkholder  * proctree lock.
1455df8bae1dSRodney W. Grimes  */
145626f9a767SRodney W. Grimes void
1457df8bae1dSRodney W. Grimes stop(p)
1458df8bae1dSRodney W. Grimes 	register struct proc *p;
1459df8bae1dSRodney W. Grimes {
1460df8bae1dSRodney W. Grimes 
146198f03f90SJake Burkholder 	PROCTREE_ASSERT(PT_SHARED);
14621c32c37cSJohn Baldwin 	mtx_enter(&sched_lock, MTX_SPIN);
1463df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1464df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1465df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
14661c32c37cSJohn Baldwin 	mtx_exit(&sched_lock, MTX_SPIN);
1467df8bae1dSRodney W. Grimes }
1468df8bae1dSRodney W. Grimes 
1469df8bae1dSRodney W. Grimes /*
1470df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1471df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1472df8bae1dSRodney W. Grimes  */
1473df8bae1dSRodney W. Grimes void
14742c42a146SMarcel Moolenaar postsig(sig)
14752c42a146SMarcel Moolenaar 	register int sig;
1476df8bae1dSRodney W. Grimes {
1477df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
14782c42a146SMarcel Moolenaar 	struct sigacts *ps = p->p_sigacts;
14792c42a146SMarcel Moolenaar 	sig_t action;
14802c42a146SMarcel Moolenaar 	sigset_t returnmask;
14812c42a146SMarcel Moolenaar 	int code;
1482df8bae1dSRodney W. Grimes 
14832c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
14845526d2d9SEivind Eklund 
14852c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
14862c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1487df8bae1dSRodney W. Grimes #ifdef KTRACE
1488df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1489645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
14902c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1491df8bae1dSRodney W. Grimes #endif
14922c42a146SMarcel Moolenaar 	STOPEVENT(p, S_SIG, sig);
14932a024a2bSSean Eric Fagan 
1494df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1495df8bae1dSRodney W. Grimes 		/*
1496df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1497df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1498df8bae1dSRodney W. Grimes 		 */
14992c42a146SMarcel Moolenaar 		sigexit(p, sig);
1500df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1501df8bae1dSRodney W. Grimes 	} else {
1502df8bae1dSRodney W. Grimes 		/*
1503df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1504df8bae1dSRodney W. Grimes 		 */
15052c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
15065526d2d9SEivind Eklund 		    ("postsig action"));
1507df8bae1dSRodney W. Grimes 		/*
1508df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1509645682fdSLuoqi Chen 		 * occurrences of this signal.
1510df8bae1dSRodney W. Grimes 		 *
1511645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1512df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1513645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1514df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1515df8bae1dSRodney W. Grimes 		 */
1516df8bae1dSRodney W. Grimes 		(void) splhigh();
1517645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
15186626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1519645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1520df8bae1dSRodney W. Grimes 		} else
1521df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
15222c42a146SMarcel Moolenaar 
15232c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15242c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15252c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
15262c42a146SMarcel Moolenaar 
15272c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1528289ccde0SPeter Wemm 			/*
15292c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1530289ccde0SPeter Wemm 			 */
15312c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
15322c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15332c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
15342c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
15352c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1536dedc04feSPeter Wemm 		}
1537df8bae1dSRodney W. Grimes 		(void) spl0();
1538df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
15392c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1540df8bae1dSRodney W. Grimes 			code = 0;
1541df8bae1dSRodney W. Grimes 		} else {
15426626c604SJulian Elischer 			code = p->p_code;
15436626c604SJulian Elischer 			p->p_code = 0;
15446626c604SJulian Elischer 			p->p_sig = 0;
1545df8bae1dSRodney W. Grimes 		}
15462c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1547df8bae1dSRodney W. Grimes 	}
1548df8bae1dSRodney W. Grimes }
1549df8bae1dSRodney W. Grimes 
1550df8bae1dSRodney W. Grimes /*
1551df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1552df8bae1dSRodney W. Grimes  */
155326f9a767SRodney W. Grimes void
1554df8bae1dSRodney W. Grimes killproc(p, why)
1555df8bae1dSRodney W. Grimes 	struct proc *p;
1556df8bae1dSRodney W. Grimes 	char *why;
1557df8bae1dSRodney W. Grimes {
15580384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
15590384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1560729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1561729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1562df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1563df8bae1dSRodney W. Grimes }
1564df8bae1dSRodney W. Grimes 
1565df8bae1dSRodney W. Grimes /*
1566df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1567df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1568df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1569df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1570df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1571df8bae1dSRodney W. Grimes  * does not return.
1572df8bae1dSRodney W. Grimes  */
157326f9a767SRodney W. Grimes void
15742c42a146SMarcel Moolenaar sigexit(p, sig)
1575df8bae1dSRodney W. Grimes 	register struct proc *p;
15762c42a146SMarcel Moolenaar 	int sig;
1577df8bae1dSRodney W. Grimes {
1578df8bae1dSRodney W. Grimes 
1579df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
15802c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
15812c42a146SMarcel Moolenaar 		p->p_sig = sig;
1582c364e17eSAndrey A. Chernov 		/*
1583c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1584c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1585c364e17eSAndrey A. Chernov 		 * these messages.)
1586c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1587c364e17eSAndrey A. Chernov 		 */
1588fca666a1SJulian Elischer 		if (coredump(p) == 0)
15892c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
159057308494SJoerg Wunsch 		if (kern_logsigexit)
159157308494SJoerg Wunsch 			log(LOG_INFO,
159257308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
15933d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
15943d1b21c6SAndrey A. Chernov 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
15952c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
15962c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1597df8bae1dSRodney W. Grimes 	}
15982c42a146SMarcel Moolenaar 	exit1(p, W_EXITCODE(0, sig));
1599df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1600df8bae1dSRodney W. Grimes }
1601df8bae1dSRodney W. Grimes 
1602c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1603c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1604c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1605c5edb423SSean Eric Fagan 
1606c5edb423SSean Eric Fagan /*
1607c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1608c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1609c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1610c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1611c5edb423SSean Eric Fagan  *	%P	process id (pid)
1612c5edb423SSean Eric Fagan  *	%U	user id (uid)
1613c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1614c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1615c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1616c5edb423SSean Eric Fagan  */
1617c5edb423SSean Eric Fagan 
1618fca666a1SJulian Elischer static char *
1619c5edb423SSean Eric Fagan expand_name(name, uid, pid)
162087f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1621c5edb423SSean Eric Fagan 	char *temp;
1622c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1623c5edb423SSean Eric Fagan 	int i, n;
1624c5edb423SSean Eric Fagan 	char *format = corefilename;
1625ce38ca0fSAlfred Perlstein 	size_t namelen;
1626c5edb423SSean Eric Fagan 
1627ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
16280bfe2990SEivind Eklund 	if (temp == NULL)
16290bfe2990SEivind Eklund 		return NULL;
1630ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1631ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1632c5edb423SSean Eric Fagan 		int l;
1633c5edb423SSean Eric Fagan 		switch (format[i]) {
1634c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1635c5edb423SSean Eric Fagan 			i++;
1636c5edb423SSean Eric Fagan 			switch (format[i]) {
1637c5edb423SSean Eric Fagan 			case '%':
1638c5edb423SSean Eric Fagan 				temp[n++] = '%';
1639c5edb423SSean Eric Fagan 				break;
1640c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1641ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
164287f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1643c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1644c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1645c5edb423SSean Eric Fagan 					return NULL;
1646c5edb423SSean Eric Fagan 				}
1647ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1648ce38ca0fSAlfred Perlstein 				n += namelen;
1649c5edb423SSean Eric Fagan 				break;
1650c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1651ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1652c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
165387f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1654c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1655c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1656c5edb423SSean Eric Fagan 					return NULL;
1657c5edb423SSean Eric Fagan 				}
1658c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1659c5edb423SSean Eric Fagan 				n += l;
1660c5edb423SSean Eric Fagan 				break;
1661c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1662ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1663c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
166487f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1665c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1666c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1667c5edb423SSean Eric Fagan 					return NULL;
1668c5edb423SSean Eric Fagan 				}
1669c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1670c5edb423SSean Eric Fagan 				n += l;
1671c5edb423SSean Eric Fagan 				break;
1672c5edb423SSean Eric Fagan 			default:
1673c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1674c5edb423SSean Eric Fagan 			}
1675c5edb423SSean Eric Fagan 			break;
1676c5edb423SSean Eric Fagan 		default:
1677c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1678c5edb423SSean Eric Fagan 		}
1679c5edb423SSean Eric Fagan 	}
1680ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1681c5edb423SSean Eric Fagan 	return temp;
1682c5edb423SSean Eric Fagan }
1683c5edb423SSean Eric Fagan 
1684df8bae1dSRodney W. Grimes /*
1685fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1686fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1687fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1688fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1689fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1690fca666a1SJulian Elischer  */
1691fca666a1SJulian Elischer 
1692fca666a1SJulian Elischer static int
1693fca666a1SJulian Elischer coredump(p)
1694fca666a1SJulian Elischer 	register struct proc *p;
1695fca666a1SJulian Elischer {
1696fca666a1SJulian Elischer 	register struct vnode *vp;
1697da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
1698fca666a1SJulian Elischer 	struct nameidata nd;
1699fca666a1SJulian Elischer 	struct vattr vattr;
1700e6796b67SKirk McKusick 	int error, error1, flags;
1701f2a2857bSKirk McKusick 	struct mount *mp;
1702fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1703fca666a1SJulian Elischer 	off_t limit;
1704fca666a1SJulian Elischer 
1705fca666a1SJulian Elischer 	STOPEVENT(p, S_CORE, 0);
1706fca666a1SJulian Elischer 
1707e5a28db9SPaul Saab 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1708fca666a1SJulian Elischer 		return (EFAULT);
1709fca666a1SJulian Elischer 
1710fca666a1SJulian Elischer 	/*
171135a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
171235a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
171335a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
171435a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
171535a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
171635a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1717fca666a1SJulian Elischer 	 */
171835a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
171935a2598fSSean Eric Fagan 	if (limit == 0)
172035a2598fSSean Eric Fagan 		return 0;
172135a2598fSSean Eric Fagan 
1722f2a2857bSKirk McKusick restart:
1723fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1724fca666a1SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1725e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1726e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1727fca666a1SJulian Elischer 	free(name, M_TEMP);
1728fca666a1SJulian Elischer 	if (error)
1729fca666a1SJulian Elischer 		return (error);
1730762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1731fca666a1SJulian Elischer 	vp = nd.ni_vp;
1732f2a2857bSKirk McKusick 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1733f2a2857bSKirk McKusick 		VOP_UNLOCK(vp, 0, p);
1734f2a2857bSKirk McKusick 		if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
1735f2a2857bSKirk McKusick 			return (error);
1736f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1737f2a2857bSKirk McKusick 			return (error);
1738f2a2857bSKirk McKusick 		goto restart;
1739f2a2857bSKirk McKusick 	}
1740fca666a1SJulian Elischer 
1741fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
1742fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
1743fca666a1SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1744fca666a1SJulian Elischer 		error = EFAULT;
1745fca666a1SJulian Elischer 		goto out;
1746fca666a1SJulian Elischer 	}
1747fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
1748fca666a1SJulian Elischer 	vattr.va_size = 0;
1749fca666a1SJulian Elischer 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1750fca666a1SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, p);
1751fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
1752fca666a1SJulian Elischer 
1753fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
1754fca666a1SJulian Elischer 	  p->p_sysent->sv_coredump(p, vp, limit) :
1755fca666a1SJulian Elischer 	  ENOSYS;
1756fca666a1SJulian Elischer 
1757fca666a1SJulian Elischer out:
1758fca666a1SJulian Elischer 	VOP_UNLOCK(vp, 0, p);
1759f2a2857bSKirk McKusick 	vn_finished_write(mp);
1760fca666a1SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, p);
1761fca666a1SJulian Elischer 	if (error == 0)
1762fca666a1SJulian Elischer 		error = error1;
1763fca666a1SJulian Elischer 	return (error);
1764fca666a1SJulian Elischer }
1765fca666a1SJulian Elischer 
1766fca666a1SJulian Elischer /*
1767df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1768df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1769df8bae1dSRodney W. Grimes  */
1770d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1771df8bae1dSRodney W. Grimes struct nosys_args {
1772df8bae1dSRodney W. Grimes 	int	dummy;
1773df8bae1dSRodney W. Grimes };
1774d2d3e875SBruce Evans #endif
1775df8bae1dSRodney W. Grimes /* ARGSUSED */
177626f9a767SRodney W. Grimes int
1777cb226aaaSPoul-Henning Kamp nosys(p, args)
1778df8bae1dSRodney W. Grimes 	struct proc *p;
1779df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1780df8bae1dSRodney W. Grimes {
1781df8bae1dSRodney W. Grimes 
1782df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1783df8bae1dSRodney W. Grimes 	return (EINVAL);
1784df8bae1dSRodney W. Grimes }
1785831d27a9SDon Lewis 
1786831d27a9SDon Lewis /*
1787831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1788831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1789831d27a9SDon Lewis  */
1790831d27a9SDon Lewis void
17912c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
1792831d27a9SDon Lewis 	struct sigio *sigio;
17932c42a146SMarcel Moolenaar 	int sig, checkctty;
1794831d27a9SDon Lewis {
1795831d27a9SDon Lewis 	if (sigio == NULL)
1796831d27a9SDon Lewis 		return;
1797831d27a9SDon Lewis 
1798831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1799831d27a9SDon Lewis 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1800831d27a9SDon Lewis 		             sigio->sio_proc))
18012c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
1802831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1803831d27a9SDon Lewis 		struct proc *p;
1804831d27a9SDon Lewis 
18052e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1806831d27a9SDon Lewis 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1807831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
18082c42a146SMarcel Moolenaar 				psignal(p, sig);
1809831d27a9SDon Lewis 	}
1810831d27a9SDon Lewis }
1811cb679c38SJonathan Lemon 
1812cb679c38SJonathan Lemon static int
1813cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
1814cb679c38SJonathan Lemon {
1815cb679c38SJonathan Lemon 	struct proc *p = curproc;
1816cb679c38SJonathan Lemon 
1817cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
1818cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1819cb679c38SJonathan Lemon 
1820cb679c38SJonathan Lemon 	/* XXX lock the proc here while adding to the list? */
1821cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1822cb679c38SJonathan Lemon 
1823cb679c38SJonathan Lemon 	return (0);
1824cb679c38SJonathan Lemon }
1825cb679c38SJonathan Lemon 
1826cb679c38SJonathan Lemon static void
1827cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
1828cb679c38SJonathan Lemon {
1829cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
1830cb679c38SJonathan Lemon 
1831e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1832cb679c38SJonathan Lemon }
1833cb679c38SJonathan Lemon 
1834cb679c38SJonathan Lemon /*
1835cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
1836cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
1837cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
1838cb679c38SJonathan Lemon  * isn't worth the trouble.
1839cb679c38SJonathan Lemon  */
1840cb679c38SJonathan Lemon static int
1841cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
1842cb679c38SJonathan Lemon {
1843cb679c38SJonathan Lemon 
1844cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
1845cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
1846cb679c38SJonathan Lemon 
1847cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
1848cb679c38SJonathan Lemon 			kn->kn_data++;
1849cb679c38SJonathan Lemon 	}
1850cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
1851cb679c38SJonathan Lemon }
1852