xref: /freebsd/sys/kern/kern_sig.c (revision 5526d2d920eb17b1507499f35b275b486f7fe8d0)
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
395526d2d9SEivind Eklund  * $Id: kern_sig.c,v 1.51 1998/12/19 02:55:33 julian Exp $
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #define	SIGPROP		/* include signal properties table */
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47c87e2930SDavid Greenman #include <sys/kernel.h>
48d2d3e875SBruce Evans #include <sys/sysproto.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>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
542a024a2bSSean Eric Fagan #include <sys/pioctl.h>
55df8bae1dSRodney W. Grimes #include <sys/systm.h>
56df8bae1dSRodney W. Grimes #include <sys/acct.h>
573ac4d1efSBruce Evans #include <sys/fcntl.h>
58df8bae1dSRodney W. Grimes #include <sys/wait.h>
59df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
60df8bae1dSRodney W. Grimes #include <sys/syslog.h>
61df8bae1dSRodney W. Grimes #include <sys/stat.h>
62d66a5066SPeter Wemm #include <sys/sysent.h>
63c87e2930SDavid Greenman #include <sys/sysctl.h>
64c5edb423SSean Eric Fagan #include <sys/malloc.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <machine/cpu.h>
673163861cSTor Egge #ifdef SMP
683163861cSTor Egge #include <machine/smp.h>
693163861cSTor Egge #endif
70df8bae1dSRodney W. Grimes 
7187b6de2bSPoul-Henning Kamp static int killpg1	__P((struct proc *cp, int signum, int pgid, int all));
728a2d9f50SBruce Evans static void setsigvec	__P((struct proc *p, int signum, struct sigaction *sa));
7387b6de2bSPoul-Henning Kamp static void stop	__P((struct proc *));
7426f9a767SRodney W. Grimes 
7557308494SJoerg Wunsch static int	kern_logsigexit = 1;
7657308494SJoerg Wunsch SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, &kern_logsigexit, 0, "");
7757308494SJoerg Wunsch 
78df8bae1dSRodney W. Grimes /*
79df8bae1dSRodney W. Grimes  * Can process p, with pcred pc, send the signal signum to process q?
80df8bae1dSRodney W. Grimes  */
81df8bae1dSRodney W. Grimes #define CANSIGNAL(p, pc, q, signum) \
82df8bae1dSRodney W. Grimes 	((pc)->pc_ucred->cr_uid == 0 || \
83df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
84df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
85df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
86df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
87df8bae1dSRodney W. Grimes 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
88df8bae1dSRodney W. Grimes 
89831d27a9SDon Lewis /*
90831d27a9SDon Lewis  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
91831d27a9SDon Lewis  */
92831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \
93831d27a9SDon Lewis 	((uc)->cr_uid == 0 || \
94831d27a9SDon Lewis 	    (ruid) == (q)->p_cred->p_ruid || \
95831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_cred->p_ruid || \
96831d27a9SDon Lewis 	    (ruid) == (q)->p_ucred->cr_uid || \
97831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
98831d27a9SDon Lewis 
9922d4b0fbSJohn Polstra int sugid_coredump;
100c87e2930SDavid Greenman SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, &sugid_coredump, 0, "");
101c87e2930SDavid Greenman 
102d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
103df8bae1dSRodney W. Grimes struct sigaction_args {
104df8bae1dSRodney W. Grimes 	int	signum;
105df8bae1dSRodney W. Grimes 	struct	sigaction *nsa;
106df8bae1dSRodney W. Grimes 	struct	sigaction *osa;
107df8bae1dSRodney W. Grimes };
108d2d3e875SBruce Evans #endif
109df8bae1dSRodney W. Grimes /* ARGSUSED */
11026f9a767SRodney W. Grimes int
111cb226aaaSPoul-Henning Kamp sigaction(p, uap)
112df8bae1dSRodney W. Grimes 	struct proc *p;
113df8bae1dSRodney W. Grimes 	register struct sigaction_args *uap;
114df8bae1dSRodney W. Grimes {
115df8bae1dSRodney W. Grimes 	struct sigaction vec;
116df8bae1dSRodney W. Grimes 	register struct sigaction *sa;
117df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
118df8bae1dSRodney W. Grimes 	register int signum;
119df8bae1dSRodney W. Grimes 	int bit, error;
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes 	signum = uap->signum;
122e6eeb36dSBruce Evans 	if (signum <= 0 || signum >= NSIG)
123df8bae1dSRodney W. Grimes 		return (EINVAL);
124df8bae1dSRodney W. Grimes 	sa = &vec;
125df8bae1dSRodney W. Grimes 	if (uap->osa) {
126df8bae1dSRodney W. Grimes 		sa->sa_handler = ps->ps_sigact[signum];
127df8bae1dSRodney W. Grimes 		sa->sa_mask = ps->ps_catchmask[signum];
128df8bae1dSRodney W. Grimes 		bit = sigmask(signum);
129df8bae1dSRodney W. Grimes 		sa->sa_flags = 0;
130df8bae1dSRodney W. Grimes 		if ((ps->ps_sigonstack & bit) != 0)
131df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_ONSTACK;
132df8bae1dSRodney W. Grimes 		if ((ps->ps_sigintr & bit) == 0)
133df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_RESTART;
134dedc04feSPeter Wemm 		if ((ps->ps_sigreset & bit) != 0)
135dedc04feSPeter Wemm 			sa->sa_flags |= SA_RESETHAND;
136289ccde0SPeter Wemm 		if ((ps->ps_signodefer & bit) != 0)
137dedc04feSPeter Wemm 			sa->sa_flags |= SA_NODEFER;
1386626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
139289ccde0SPeter Wemm 		if (signum == SIGCHLD && p->p_flag & P_NOCLDSTOP)
1406626c604SJulian Elischer #else
1416626c604SJulian Elischer 		if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
1426626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
143289ccde0SPeter Wemm 			sa->sa_flags |= SA_NOCLDSTOP;
1446626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
145245f17d4SJoerg Wunsch 		if (signum == SIGCHLD && p->p_flag & P_NOCLDWAIT)
1466626c604SJulian Elischer #else
1476626c604SJulian Elischer 		if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDWAIT)
1486626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
149245f17d4SJoerg Wunsch 			sa->sa_flags |= SA_NOCLDWAIT;
150bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa,
151bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
152df8bae1dSRodney W. Grimes 			return (error);
153df8bae1dSRodney W. Grimes 	}
154df8bae1dSRodney W. Grimes 	if (uap->nsa) {
155bb56ec4aSPoul-Henning Kamp 		if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
156bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
157df8bae1dSRodney W. Grimes 			return (error);
158e6eeb36dSBruce Evans 		if ((signum == SIGKILL || signum == SIGSTOP) &&
159e6eeb36dSBruce Evans 		    sa->sa_handler != SIG_DFL)
160e6eeb36dSBruce Evans 			return (EINVAL);
161df8bae1dSRodney W. Grimes 		setsigvec(p, signum, sa);
162df8bae1dSRodney W. Grimes 	}
163df8bae1dSRodney W. Grimes 	return (0);
164df8bae1dSRodney W. Grimes }
165df8bae1dSRodney W. Grimes 
1668a2d9f50SBruce Evans static void
167df8bae1dSRodney W. Grimes setsigvec(p, signum, sa)
168df8bae1dSRodney W. Grimes 	register struct proc *p;
169df8bae1dSRodney W. Grimes 	int signum;
170df8bae1dSRodney W. Grimes 	register struct sigaction *sa;
171df8bae1dSRodney W. Grimes {
172df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
173df8bae1dSRodney W. Grimes 	register int bit;
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes 	bit = sigmask(signum);
176df8bae1dSRodney W. Grimes 	/*
177df8bae1dSRodney W. Grimes 	 * Change setting atomically.
178df8bae1dSRodney W. Grimes 	 */
179df8bae1dSRodney W. Grimes 	(void) splhigh();
180df8bae1dSRodney W. Grimes 	ps->ps_sigact[signum] = sa->sa_handler;
181df8bae1dSRodney W. Grimes 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
182df8bae1dSRodney W. Grimes 	if ((sa->sa_flags & SA_RESTART) == 0)
183df8bae1dSRodney W. Grimes 		ps->ps_sigintr |= bit;
184df8bae1dSRodney W. Grimes 	else
185df8bae1dSRodney W. Grimes 		ps->ps_sigintr &= ~bit;
186df8bae1dSRodney W. Grimes 	if (sa->sa_flags & SA_ONSTACK)
187df8bae1dSRodney W. Grimes 		ps->ps_sigonstack |= bit;
188df8bae1dSRodney W. Grimes 	else
189df8bae1dSRodney W. Grimes 		ps->ps_sigonstack &= ~bit;
190dedc04feSPeter Wemm 	if (sa->sa_flags & SA_RESETHAND)
191dedc04feSPeter Wemm 		ps->ps_sigreset |= bit;
1921e41c1b5SSteven Wallace 	else
193dedc04feSPeter Wemm 		ps->ps_sigreset &= ~bit;
194289ccde0SPeter Wemm 	if (sa->sa_flags & SA_NODEFER)
195289ccde0SPeter Wemm 		ps->ps_signodefer |= bit;
196289ccde0SPeter Wemm 	else
197289ccde0SPeter Wemm 		ps->ps_signodefer &= ~bit;
198df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
199df8bae1dSRodney W. Grimes 	if (sa->sa_flags & SA_USERTRAMP)
200df8bae1dSRodney W. Grimes 		ps->ps_usertramp |= bit;
201df8bae1dSRodney W. Grimes 	else
202df8bae1dSRodney W. Grimes 		ps->ps_usertramp &= ~bit;
203df8bae1dSRodney W. Grimes #endif
204df8bae1dSRodney W. Grimes 	if (signum == SIGCHLD) {
205df8bae1dSRodney W. Grimes 		if (sa->sa_flags & SA_NOCLDSTOP)
2066626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
207df8bae1dSRodney W. Grimes 			p->p_flag |= P_NOCLDSTOP;
208df8bae1dSRodney W. Grimes 		else
209df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_NOCLDSTOP;
2106626c604SJulian Elischer #else
2116626c604SJulian Elischer 			p->p_procsig->ps_flag |= P_NOCLDSTOP;
2126626c604SJulian Elischer 		else
2136626c604SJulian Elischer 			p->p_procsig->ps_flag &= ~P_NOCLDSTOP;
2146626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
215245f17d4SJoerg Wunsch 		if (sa->sa_flags & SA_NOCLDWAIT) {
216245f17d4SJoerg Wunsch 			/*
217245f17d4SJoerg Wunsch 			 * Paranoia: since SA_NOCLDWAIT is implemented by
218245f17d4SJoerg Wunsch 			 * reparenting the dying child to PID 1 (and
219245f17d4SJoerg Wunsch 			 * trust it to reap the zombie), PID 1 itself is
220245f17d4SJoerg Wunsch 			 * forbidden to set SA_NOCLDWAIT.
221245f17d4SJoerg Wunsch 			 */
222245f17d4SJoerg Wunsch 			if (p->p_pid == 1)
2236626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
224245f17d4SJoerg Wunsch 				p->p_flag &= ~P_NOCLDWAIT;
225245f17d4SJoerg Wunsch 			else
226245f17d4SJoerg Wunsch 				p->p_flag |= P_NOCLDWAIT;
2276626c604SJulian Elischer #else
2286626c604SJulian Elischer 				p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
2296626c604SJulian Elischer 			else
2306626c604SJulian Elischer 				p->p_procsig->ps_flag |= P_NOCLDWAIT;
2316626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
232245f17d4SJoerg Wunsch 		} else
2336626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
234245f17d4SJoerg Wunsch 			p->p_flag &= ~P_NOCLDWAIT;
2356626c604SJulian Elischer #else
2366626c604SJulian Elischer 			p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
2376626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
238df8bae1dSRodney W. Grimes 	}
239df8bae1dSRodney W. Grimes 	/*
240df8bae1dSRodney W. Grimes 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
241df8bae1dSRodney W. Grimes 	 * and for signals set to SIG_DFL where the default is to ignore.
242df8bae1dSRodney W. Grimes 	 * However, don't put SIGCONT in p_sigignore,
243df8bae1dSRodney W. Grimes 	 * as we have to restart the process.
244df8bae1dSRodney W. Grimes 	 */
245df8bae1dSRodney W. Grimes 	if (sa->sa_handler == SIG_IGN ||
246df8bae1dSRodney W. Grimes 	    (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
247df8bae1dSRodney W. Grimes 		p->p_siglist &= ~bit;		/* never to be seen again */
248df8bae1dSRodney W. Grimes 		if (signum != SIGCONT)
249df8bae1dSRodney W. Grimes 			p->p_sigignore |= bit;	/* easier in psignal */
250df8bae1dSRodney W. Grimes 		p->p_sigcatch &= ~bit;
251df8bae1dSRodney W. Grimes 	} else {
252df8bae1dSRodney W. Grimes 		p->p_sigignore &= ~bit;
253df8bae1dSRodney W. Grimes 		if (sa->sa_handler == SIG_DFL)
254df8bae1dSRodney W. Grimes 			p->p_sigcatch &= ~bit;
255df8bae1dSRodney W. Grimes 		else
256df8bae1dSRodney W. Grimes 			p->p_sigcatch |= bit;
257df8bae1dSRodney W. Grimes 	}
258df8bae1dSRodney W. Grimes 	(void) spl0();
259df8bae1dSRodney W. Grimes }
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes /*
262df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
263df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
264df8bae1dSRodney W. Grimes  */
265df8bae1dSRodney W. Grimes void
266df8bae1dSRodney W. Grimes siginit(p)
267df8bae1dSRodney W. Grimes 	struct proc *p;
268df8bae1dSRodney W. Grimes {
269df8bae1dSRodney W. Grimes 	register int i;
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes 	for (i = 0; i < NSIG; i++)
272df8bae1dSRodney W. Grimes 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
273df8bae1dSRodney W. Grimes 			p->p_sigignore |= sigmask(i);
274df8bae1dSRodney W. Grimes }
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes /*
277df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
278df8bae1dSRodney W. Grimes  */
279df8bae1dSRodney W. Grimes void
280df8bae1dSRodney W. Grimes execsigs(p)
281df8bae1dSRodney W. Grimes 	register struct proc *p;
282df8bae1dSRodney W. Grimes {
283df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
284df8bae1dSRodney W. Grimes 	register int nc, mask;
285df8bae1dSRodney W. Grimes 
286df8bae1dSRodney W. Grimes 	/*
287df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
288df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
289df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
290df8bae1dSRodney W. Grimes 	 */
291df8bae1dSRodney W. Grimes 	while (p->p_sigcatch) {
292df8bae1dSRodney W. Grimes 		nc = ffs((long)p->p_sigcatch);
293df8bae1dSRodney W. Grimes 		mask = sigmask(nc);
294df8bae1dSRodney W. Grimes 		p->p_sigcatch &= ~mask;
295df8bae1dSRodney W. Grimes 		if (sigprop[nc] & SA_IGNORE) {
296df8bae1dSRodney W. Grimes 			if (nc != SIGCONT)
297df8bae1dSRodney W. Grimes 				p->p_sigignore |= mask;
298df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
299df8bae1dSRodney W. Grimes 		}
300df8bae1dSRodney W. Grimes 		ps->ps_sigact[nc] = SIG_DFL;
301df8bae1dSRodney W. Grimes 	}
302df8bae1dSRodney W. Grimes 	/*
303df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
304df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
305df8bae1dSRodney W. Grimes 	 */
306d66a5066SPeter Wemm 	ps->ps_sigstk.ss_flags = SS_DISABLE;
307df8bae1dSRodney W. Grimes 	ps->ps_sigstk.ss_size = 0;
308aa98692fSAndreas Schulz 	ps->ps_sigstk.ss_sp = 0;
309df8bae1dSRodney W. Grimes 	ps->ps_flags = 0;
310df8bae1dSRodney W. Grimes }
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes /*
313df8bae1dSRodney W. Grimes  * Manipulate signal mask.
314df8bae1dSRodney W. Grimes  * Note that we receive new mask, not pointer,
315df8bae1dSRodney W. Grimes  * and return old mask as return value;
316df8bae1dSRodney W. Grimes  * the library stub does the rest.
317df8bae1dSRodney W. Grimes  */
318d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
319df8bae1dSRodney W. Grimes struct sigprocmask_args {
320df8bae1dSRodney W. Grimes 	int	how;
321df8bae1dSRodney W. Grimes 	sigset_t mask;
322df8bae1dSRodney W. Grimes };
323d2d3e875SBruce Evans #endif
32426f9a767SRodney W. Grimes int
325cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
326df8bae1dSRodney W. Grimes 	register struct proc *p;
327df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
328df8bae1dSRodney W. Grimes {
329df8bae1dSRodney W. Grimes 	int error = 0;
330df8bae1dSRodney W. Grimes 
331cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
332df8bae1dSRodney W. Grimes 	(void) splhigh();
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes 	switch (uap->how) {
335df8bae1dSRodney W. Grimes 	case SIG_BLOCK:
336df8bae1dSRodney W. Grimes 		p->p_sigmask |= uap->mask &~ sigcantmask;
337df8bae1dSRodney W. Grimes 		break;
338df8bae1dSRodney W. Grimes 
339df8bae1dSRodney W. Grimes 	case SIG_UNBLOCK:
340df8bae1dSRodney W. Grimes 		p->p_sigmask &= ~uap->mask;
341df8bae1dSRodney W. Grimes 		break;
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes 	case SIG_SETMASK:
344df8bae1dSRodney W. Grimes 		p->p_sigmask = uap->mask &~ sigcantmask;
345df8bae1dSRodney W. Grimes 		break;
346df8bae1dSRodney W. Grimes 
347df8bae1dSRodney W. Grimes 	default:
348df8bae1dSRodney W. Grimes 		error = EINVAL;
349df8bae1dSRodney W. Grimes 		break;
350df8bae1dSRodney W. Grimes 	}
351df8bae1dSRodney W. Grimes 	(void) spl0();
352df8bae1dSRodney W. Grimes 	return (error);
353df8bae1dSRodney W. Grimes }
354df8bae1dSRodney W. Grimes 
355d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
356df8bae1dSRodney W. Grimes struct sigpending_args {
357df8bae1dSRodney W. Grimes 	int	dummy;
358df8bae1dSRodney W. Grimes };
359d2d3e875SBruce Evans #endif
360df8bae1dSRodney W. Grimes /* ARGSUSED */
36126f9a767SRodney W. Grimes int
362cb226aaaSPoul-Henning Kamp sigpending(p, uap)
363df8bae1dSRodney W. Grimes 	struct proc *p;
364df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
365df8bae1dSRodney W. Grimes {
366df8bae1dSRodney W. Grimes 
367cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_siglist;
368df8bae1dSRodney W. Grimes 	return (0);
369df8bae1dSRodney W. Grimes }
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
372df8bae1dSRodney W. Grimes /*
373df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
374df8bae1dSRodney W. Grimes  */
375d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
376df8bae1dSRodney W. Grimes struct osigvec_args {
377df8bae1dSRodney W. Grimes 	int	signum;
378df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
379df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
380df8bae1dSRodney W. Grimes };
381d2d3e875SBruce Evans #endif
382df8bae1dSRodney W. Grimes /* ARGSUSED */
38326f9a767SRodney W. Grimes int
384cb226aaaSPoul-Henning Kamp osigvec(p, uap)
385df8bae1dSRodney W. Grimes 	struct proc *p;
386df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
387df8bae1dSRodney W. Grimes {
388df8bae1dSRodney W. Grimes 	struct sigvec vec;
389df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
390df8bae1dSRodney W. Grimes 	register struct sigvec *sv;
391df8bae1dSRodney W. Grimes 	register int signum;
392df8bae1dSRodney W. Grimes 	int bit, error;
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes 	signum = uap->signum;
395e6eeb36dSBruce Evans 	if (signum <= 0 || signum >= NSIG)
396df8bae1dSRodney W. Grimes 		return (EINVAL);
397df8bae1dSRodney W. Grimes 	sv = &vec;
398df8bae1dSRodney W. Grimes 	if (uap->osv) {
399df8bae1dSRodney W. Grimes 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
400df8bae1dSRodney W. Grimes 		sv->sv_mask = ps->ps_catchmask[signum];
401df8bae1dSRodney W. Grimes 		bit = sigmask(signum);
402df8bae1dSRodney W. Grimes 		sv->sv_flags = 0;
403df8bae1dSRodney W. Grimes 		if ((ps->ps_sigonstack & bit) != 0)
404df8bae1dSRodney W. Grimes 			sv->sv_flags |= SV_ONSTACK;
405df8bae1dSRodney W. Grimes 		if ((ps->ps_sigintr & bit) != 0)
406df8bae1dSRodney W. Grimes 			sv->sv_flags |= SV_INTERRUPT;
407289ccde0SPeter Wemm 		if ((ps->ps_sigreset & bit) != 0)
408289ccde0SPeter Wemm 			sv->sv_flags |= SV_RESETHAND;
409289ccde0SPeter Wemm 		if ((ps->ps_signodefer & bit) != 0)
410289ccde0SPeter Wemm 			sv->sv_flags |= SV_NODEFER;
411df8bae1dSRodney W. Grimes #ifndef COMPAT_SUNOS
4126626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
413289ccde0SPeter Wemm 		if (signum == SIGCHLD && p->p_flag & P_NOCLDSTOP)
4146626c604SJulian Elischer #else
4156626c604SJulian Elischer 		if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
4166626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
417289ccde0SPeter Wemm 			sv->sv_flags |= SV_NOCLDSTOP;
418df8bae1dSRodney W. Grimes #endif
419bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv,
420bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
421df8bae1dSRodney W. Grimes 			return (error);
422df8bae1dSRodney W. Grimes 	}
423df8bae1dSRodney W. Grimes 	if (uap->nsv) {
424bb56ec4aSPoul-Henning Kamp 		if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
425bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
426df8bae1dSRodney W. Grimes 			return (error);
427e6eeb36dSBruce Evans 		if ((signum == SIGKILL || signum == SIGSTOP) &&
428e6eeb36dSBruce Evans 		    sv->sv_handler != SIG_DFL)
429e6eeb36dSBruce Evans 			return (EINVAL);
430df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
431df8bae1dSRodney W. Grimes 		sv->sv_flags |= SA_USERTRAMP;
432df8bae1dSRodney W. Grimes #endif
433df8bae1dSRodney W. Grimes 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
434df8bae1dSRodney W. Grimes 		setsigvec(p, signum, (struct sigaction *)sv);
435df8bae1dSRodney W. Grimes 	}
436df8bae1dSRodney W. Grimes 	return (0);
437df8bae1dSRodney W. Grimes }
438df8bae1dSRodney W. Grimes 
439d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
440df8bae1dSRodney W. Grimes struct osigblock_args {
441df8bae1dSRodney W. Grimes 	int	mask;
442df8bae1dSRodney W. Grimes };
443d2d3e875SBruce Evans #endif
44426f9a767SRodney W. Grimes int
445cb226aaaSPoul-Henning Kamp osigblock(p, uap)
446df8bae1dSRodney W. Grimes 	register struct proc *p;
447df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
448df8bae1dSRodney W. Grimes {
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes 	(void) splhigh();
451cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
452df8bae1dSRodney W. Grimes 	p->p_sigmask |= uap->mask &~ sigcantmask;
453df8bae1dSRodney W. Grimes 	(void) spl0();
454df8bae1dSRodney W. Grimes 	return (0);
455df8bae1dSRodney W. Grimes }
456df8bae1dSRodney W. Grimes 
457d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
458df8bae1dSRodney W. Grimes struct osigsetmask_args {
459df8bae1dSRodney W. Grimes 	int	mask;
460df8bae1dSRodney W. Grimes };
461d2d3e875SBruce Evans #endif
46226f9a767SRodney W. Grimes int
463cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
464df8bae1dSRodney W. Grimes 	struct proc *p;
465df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
466df8bae1dSRodney W. Grimes {
467df8bae1dSRodney W. Grimes 
468df8bae1dSRodney W. Grimes 	(void) splhigh();
469cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
470df8bae1dSRodney W. Grimes 	p->p_sigmask = uap->mask &~ sigcantmask;
471df8bae1dSRodney W. Grimes 	(void) spl0();
472df8bae1dSRodney W. Grimes 	return (0);
473df8bae1dSRodney W. Grimes }
474df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
475df8bae1dSRodney W. Grimes 
476df8bae1dSRodney W. Grimes /*
477df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
478df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
479df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
480df8bae1dSRodney W. Grimes  */
481d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
482df8bae1dSRodney W. Grimes struct sigsuspend_args {
483df8bae1dSRodney W. Grimes 	sigset_t mask;
484df8bae1dSRodney W. Grimes };
485d2d3e875SBruce Evans #endif
486df8bae1dSRodney W. Grimes /* ARGSUSED */
48726f9a767SRodney W. Grimes int
488cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
489df8bae1dSRodney W. Grimes 	register struct proc *p;
490df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
491df8bae1dSRodney W. Grimes {
492df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
493df8bae1dSRodney W. Grimes 
494df8bae1dSRodney W. Grimes 	/*
495df8bae1dSRodney W. Grimes 	 * When returning from sigpause, we want
496df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
497df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
498df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
499df8bae1dSRodney W. Grimes 	 * to indicate this.
500df8bae1dSRodney W. Grimes 	 */
5016626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
502df8bae1dSRodney W. Grimes 	ps->ps_oldmask = p->p_sigmask;
503df8bae1dSRodney W. Grimes 	ps->ps_flags |= SAS_OLDMASK;
5046626c604SJulian Elischer #else
5056626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
5066626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
507df8bae1dSRodney W. Grimes 	p->p_sigmask = uap->mask &~ sigcantmask;
508df8bae1dSRodney W. Grimes 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
509df8bae1dSRodney W. Grimes 		/* void */;
510df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
511df8bae1dSRodney W. Grimes 	return (EINTR);
512df8bae1dSRodney W. Grimes }
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
515d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
516df8bae1dSRodney W. Grimes struct osigstack_args {
517df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
518df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
519df8bae1dSRodney W. Grimes };
520d2d3e875SBruce Evans #endif
521df8bae1dSRodney W. Grimes /* ARGSUSED */
52226f9a767SRodney W. Grimes int
523cb226aaaSPoul-Henning Kamp osigstack(p, uap)
524df8bae1dSRodney W. Grimes 	struct proc *p;
525df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
526df8bae1dSRodney W. Grimes {
527df8bae1dSRodney W. Grimes 	struct sigstack ss;
528df8bae1dSRodney W. Grimes 	struct sigacts *psp;
529df8bae1dSRodney W. Grimes 	int error = 0;
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes 	psp = p->p_sigacts;
532aa98692fSAndreas Schulz 	ss.ss_sp = psp->ps_sigstk.ss_sp;
533d66a5066SPeter Wemm 	ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
534df8bae1dSRodney W. Grimes 	if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
535df8bae1dSRodney W. Grimes 	    sizeof (struct sigstack))))
536df8bae1dSRodney W. Grimes 		return (error);
537df8bae1dSRodney W. Grimes 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
538df8bae1dSRodney W. Grimes 	    sizeof (ss))) == 0) {
539aa98692fSAndreas Schulz 		psp->ps_sigstk.ss_sp = ss.ss_sp;
540df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_size = 0;
541d66a5066SPeter Wemm 		psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
542df8bae1dSRodney W. Grimes 		psp->ps_flags |= SAS_ALTSTACK;
543df8bae1dSRodney W. Grimes 	}
544df8bae1dSRodney W. Grimes 	return (error);
545df8bae1dSRodney W. Grimes }
546df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
547df8bae1dSRodney W. Grimes 
548d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
549df8bae1dSRodney W. Grimes struct sigaltstack_args {
550df8bae1dSRodney W. Grimes 	struct	sigaltstack *nss;
551df8bae1dSRodney W. Grimes 	struct	sigaltstack *oss;
552df8bae1dSRodney W. Grimes };
553d2d3e875SBruce Evans #endif
554df8bae1dSRodney W. Grimes /* ARGSUSED */
55526f9a767SRodney W. Grimes int
556cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
557df8bae1dSRodney W. Grimes 	struct proc *p;
558df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
559df8bae1dSRodney W. Grimes {
560df8bae1dSRodney W. Grimes 	struct sigacts *psp;
561df8bae1dSRodney W. Grimes 	struct sigaltstack ss;
562df8bae1dSRodney W. Grimes 	int error;
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 	psp = p->p_sigacts;
565df8bae1dSRodney W. Grimes 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
566d66a5066SPeter Wemm 		psp->ps_sigstk.ss_flags |= SS_DISABLE;
567df8bae1dSRodney W. Grimes 	if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
568df8bae1dSRodney W. Grimes 	    (caddr_t)uap->oss, sizeof (struct sigaltstack))))
569df8bae1dSRodney W. Grimes 		return (error);
570df8bae1dSRodney W. Grimes 	if (uap->nss == 0)
571df8bae1dSRodney W. Grimes 		return (0);
572bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss))))
573df8bae1dSRodney W. Grimes 		return (error);
574d66a5066SPeter Wemm 	if (ss.ss_flags & SS_DISABLE) {
575d66a5066SPeter Wemm 		if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
576df8bae1dSRodney W. Grimes 			return (EINVAL);
577df8bae1dSRodney W. Grimes 		psp->ps_flags &= ~SAS_ALTSTACK;
578df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_flags = ss.ss_flags;
579df8bae1dSRodney W. Grimes 		return (0);
580df8bae1dSRodney W. Grimes 	}
581df8bae1dSRodney W. Grimes 	if (ss.ss_size < MINSIGSTKSZ)
582df8bae1dSRodney W. Grimes 		return (ENOMEM);
583df8bae1dSRodney W. Grimes 	psp->ps_flags |= SAS_ALTSTACK;
584df8bae1dSRodney W. Grimes 	psp->ps_sigstk= ss;
585df8bae1dSRodney W. Grimes 	return (0);
586df8bae1dSRodney W. Grimes }
587df8bae1dSRodney W. Grimes 
588d93f860cSPoul-Henning Kamp /*
589d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
590d93f860cSPoul-Henning Kamp  * cp is calling process.
591d93f860cSPoul-Henning Kamp  */
592d93f860cSPoul-Henning Kamp int
593d93f860cSPoul-Henning Kamp killpg1(cp, signum, pgid, all)
594d93f860cSPoul-Henning Kamp 	register struct proc *cp;
595d93f860cSPoul-Henning Kamp 	int signum, pgid, all;
596d93f860cSPoul-Henning Kamp {
597d93f860cSPoul-Henning Kamp 	register struct proc *p;
598d93f860cSPoul-Henning Kamp 	register struct pcred *pc = cp->p_cred;
599d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
600d93f860cSPoul-Henning Kamp 	int nfound = 0;
601d93f860cSPoul-Henning Kamp 
602d93f860cSPoul-Henning Kamp 	if (all)
603d93f860cSPoul-Henning Kamp 		/*
604d93f860cSPoul-Henning Kamp 		 * broadcast
605d93f860cSPoul-Henning Kamp 		 */
606b75356e1SJeffrey Hsu 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
607d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
608d93f860cSPoul-Henning Kamp 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
609d93f860cSPoul-Henning Kamp 				continue;
610d93f860cSPoul-Henning Kamp 			nfound++;
611d93f860cSPoul-Henning Kamp 			if (signum)
612d93f860cSPoul-Henning Kamp 				psignal(p, signum);
613d93f860cSPoul-Henning Kamp 		}
614d93f860cSPoul-Henning Kamp 	else {
615d93f860cSPoul-Henning Kamp 		if (pgid == 0)
616d93f860cSPoul-Henning Kamp 			/*
617d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
618d93f860cSPoul-Henning Kamp 			 */
619d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
620d93f860cSPoul-Henning Kamp 		else {
621d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
622d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
623d93f860cSPoul-Henning Kamp 				return (ESRCH);
624d93f860cSPoul-Henning Kamp 		}
625b75356e1SJeffrey Hsu 		for (p = pgrp->pg_members.lh_first; p != 0;
626b75356e1SJeffrey Hsu 		     p = p->p_pglist.le_next) {
627d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
628d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
629d93f860cSPoul-Henning Kamp 			    !CANSIGNAL(cp, pc, p, signum))
630d93f860cSPoul-Henning Kamp 				continue;
631d93f860cSPoul-Henning Kamp 			nfound++;
632d93f860cSPoul-Henning Kamp 			if (signum)
633d93f860cSPoul-Henning Kamp 				psignal(p, signum);
634d93f860cSPoul-Henning Kamp 		}
635d93f860cSPoul-Henning Kamp 	}
636d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
637d93f860cSPoul-Henning Kamp }
638d93f860cSPoul-Henning Kamp 
639d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
640df8bae1dSRodney W. Grimes struct kill_args {
641df8bae1dSRodney W. Grimes 	int	pid;
642df8bae1dSRodney W. Grimes 	int	signum;
643df8bae1dSRodney W. Grimes };
644d2d3e875SBruce Evans #endif
645df8bae1dSRodney W. Grimes /* ARGSUSED */
64626f9a767SRodney W. Grimes int
647cb226aaaSPoul-Henning Kamp kill(cp, uap)
648df8bae1dSRodney W. Grimes 	register struct proc *cp;
649df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
650df8bae1dSRodney W. Grimes {
651df8bae1dSRodney W. Grimes 	register struct proc *p;
652df8bae1dSRodney W. Grimes 	register struct pcred *pc = cp->p_cred;
653df8bae1dSRodney W. Grimes 
654df8bae1dSRodney W. Grimes 	if ((u_int)uap->signum >= NSIG)
655df8bae1dSRodney W. Grimes 		return (EINVAL);
656df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
657df8bae1dSRodney W. Grimes 		/* kill single process */
658df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
659df8bae1dSRodney W. Grimes 			return (ESRCH);
660df8bae1dSRodney W. Grimes 		if (!CANSIGNAL(cp, pc, p, uap->signum))
661df8bae1dSRodney W. Grimes 			return (EPERM);
662df8bae1dSRodney W. Grimes 		if (uap->signum)
663df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
664df8bae1dSRodney W. Grimes 		return (0);
665df8bae1dSRodney W. Grimes 	}
666df8bae1dSRodney W. Grimes 	switch (uap->pid) {
667df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
668df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
669df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
670df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
671df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
672df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
673df8bae1dSRodney W. Grimes 	}
674df8bae1dSRodney W. Grimes 	/* NOTREACHED */
675df8bae1dSRodney W. Grimes }
676df8bae1dSRodney W. Grimes 
677df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
678d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
679df8bae1dSRodney W. Grimes struct okillpg_args {
680df8bae1dSRodney W. Grimes 	int	pgid;
681df8bae1dSRodney W. Grimes 	int	signum;
682df8bae1dSRodney W. Grimes };
683d2d3e875SBruce Evans #endif
684df8bae1dSRodney W. Grimes /* ARGSUSED */
68526f9a767SRodney W. Grimes int
686cb226aaaSPoul-Henning Kamp okillpg(p, uap)
687df8bae1dSRodney W. Grimes 	struct proc *p;
688df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
689df8bae1dSRodney W. Grimes {
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes 	if ((u_int)uap->signum >= NSIG)
692df8bae1dSRodney W. Grimes 		return (EINVAL);
693df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
694df8bae1dSRodney W. Grimes }
695df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes /*
698df8bae1dSRodney W. Grimes  * Send a signal to a process group.
699df8bae1dSRodney W. Grimes  */
700df8bae1dSRodney W. Grimes void
701df8bae1dSRodney W. Grimes gsignal(pgid, signum)
702df8bae1dSRodney W. Grimes 	int pgid, signum;
703df8bae1dSRodney W. Grimes {
704df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
705df8bae1dSRodney W. Grimes 
706df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
707df8bae1dSRodney W. Grimes 		pgsignal(pgrp, signum, 0);
708df8bae1dSRodney W. Grimes }
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes /*
711df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
712df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
713df8bae1dSRodney W. Grimes  */
714df8bae1dSRodney W. Grimes void
715df8bae1dSRodney W. Grimes pgsignal(pgrp, signum, checkctty)
716df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
717df8bae1dSRodney W. Grimes 	int signum, checkctty;
718df8bae1dSRodney W. Grimes {
719df8bae1dSRodney W. Grimes 	register struct proc *p;
720df8bae1dSRodney W. Grimes 
721df8bae1dSRodney W. Grimes 	if (pgrp)
722b75356e1SJeffrey Hsu 		for (p = pgrp->pg_members.lh_first; p != 0;
723b75356e1SJeffrey Hsu 		     p = p->p_pglist.le_next)
724df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
725df8bae1dSRodney W. Grimes 				psignal(p, signum);
726df8bae1dSRodney W. Grimes }
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes /*
729df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
730df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
731df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
732df8bae1dSRodney W. Grimes  */
733df8bae1dSRodney W. Grimes void
734df8bae1dSRodney W. Grimes trapsignal(p, signum, code)
735df8bae1dSRodney W. Grimes 	struct proc *p;
736df8bae1dSRodney W. Grimes 	register int signum;
7378674077aSJeffrey Hsu 	u_long code;
738df8bae1dSRodney W. Grimes {
739df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
740df8bae1dSRodney W. Grimes 	int mask;
741df8bae1dSRodney W. Grimes 
742df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
743df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
744df8bae1dSRodney W. Grimes 	    (p->p_sigmask & mask) == 0) {
745df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
746df8bae1dSRodney W. Grimes #ifdef KTRACE
747df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
748df8bae1dSRodney W. Grimes 			ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
749df8bae1dSRodney W. Grimes 				p->p_sigmask, code);
750df8bae1dSRodney W. Grimes #endif
751d66a5066SPeter Wemm 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum,
752d66a5066SPeter Wemm 						p->p_sigmask, code);
753289ccde0SPeter Wemm 		p->p_sigmask |= ps->ps_catchmask[signum] |
754289ccde0SPeter Wemm 				(mask & ~ps->ps_signodefer);
755dedc04feSPeter Wemm 		if ((ps->ps_sigreset & mask) != 0) {
756289ccde0SPeter Wemm 			/*
757289ccde0SPeter Wemm 			 * See setsigvec() for origin of this code.
758289ccde0SPeter Wemm 			 */
759dedc04feSPeter Wemm 			p->p_sigcatch &= ~mask;
760dedc04feSPeter Wemm 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
761dedc04feSPeter Wemm 				p->p_sigignore |= mask;
762dedc04feSPeter Wemm 			ps->ps_sigact[signum] = SIG_DFL;
763dedc04feSPeter Wemm 		}
764df8bae1dSRodney W. Grimes 	} else {
7656626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
766df8bae1dSRodney W. Grimes 		ps->ps_code = code;	/* XXX for core dump/debugger */
7678674077aSJeffrey Hsu 		ps->ps_sig = signum;	/* XXX to verify code */
7686626c604SJulian Elischer #else
7696626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
7706626c604SJulian Elischer 		p->p_sig = signum;	/* XXX to verify code */
7716626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
772df8bae1dSRodney W. Grimes 		psignal(p, signum);
773df8bae1dSRodney W. Grimes 	}
774df8bae1dSRodney W. Grimes }
775df8bae1dSRodney W. Grimes 
776df8bae1dSRodney W. Grimes /*
777df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
778df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
779df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
780df8bae1dSRodney W. Grimes  *
781df8bae1dSRodney W. Grimes  * Exceptions:
782df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
783df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
784df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
785df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
786df8bae1dSRodney W. Grimes  *
787df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
788df8bae1dSRodney W. Grimes  */
789df8bae1dSRodney W. Grimes void
790df8bae1dSRodney W. Grimes psignal(p, signum)
791df8bae1dSRodney W. Grimes 	register struct proc *p;
792df8bae1dSRodney W. Grimes 	register int signum;
793df8bae1dSRodney W. Grimes {
794df8bae1dSRodney W. Grimes 	register int s, prop;
795df8bae1dSRodney W. Grimes 	register sig_t action;
796df8bae1dSRodney W. Grimes 	int mask;
797df8bae1dSRodney W. Grimes 
7982a024a2bSSean Eric Fagan 	if ((u_int)signum >= NSIG || signum == 0) {
7992a024a2bSSean Eric Fagan 		printf("psignal: signum %d\n", signum);
800df8bae1dSRodney W. Grimes 		panic("psignal signal number");
8012a024a2bSSean Eric Fagan 	}
802df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
803df8bae1dSRodney W. Grimes 	prop = sigprop[signum];
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 	/*
8062a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
8072a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
8082a024a2bSSean Eric Fagan 	 * a chance, as well.
809df8bae1dSRodney W. Grimes 	 */
8102a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
811df8bae1dSRodney W. Grimes 		action = SIG_DFL;
812df8bae1dSRodney W. Grimes 	else {
813df8bae1dSRodney W. Grimes 		/*
814df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
815df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
816df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
817df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
818df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
819df8bae1dSRodney W. Grimes 		 */
8206626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
821df8bae1dSRodney W. Grimes 		if (p->p_sigignore & mask)
8226626c604SJulian Elischer #else
8236626c604SJulian Elischer 		if ((p->p_sigignore & mask) || (p->p_flag & P_WEXIT))
8246626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
825df8bae1dSRodney W. Grimes 			return;
826df8bae1dSRodney W. Grimes 		if (p->p_sigmask & mask)
827df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
828df8bae1dSRodney W. Grimes 		else if (p->p_sigcatch & mask)
829df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
830df8bae1dSRodney W. Grimes 		else
831df8bae1dSRodney W. Grimes 			action = SIG_DFL;
832df8bae1dSRodney W. Grimes 	}
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
835df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
836df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
837df8bae1dSRodney W. Grimes 
838df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
839df8bae1dSRodney W. Grimes 		p->p_siglist &= ~stopsigmask;
840df8bae1dSRodney W. Grimes 
841df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
842df8bae1dSRodney W. Grimes 		/*
843df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
844df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
845df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
846df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
847df8bae1dSRodney W. Grimes 		 */
848df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
849df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
850df8bae1dSRodney W. Grimes 		        return;
851df8bae1dSRodney W. Grimes 		p->p_siglist &= ~contsigmask;
852df8bae1dSRodney W. Grimes 	}
853df8bae1dSRodney W. Grimes 	p->p_siglist |= mask;
854df8bae1dSRodney W. Grimes 
855df8bae1dSRodney W. Grimes 	/*
856df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
857df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
858df8bae1dSRodney W. Grimes 	 */
859df8bae1dSRodney W. Grimes 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
860df8bae1dSRodney W. Grimes 		return;
861df8bae1dSRodney W. Grimes 	s = splhigh();
862df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
863df8bae1dSRodney W. Grimes 
864df8bae1dSRodney W. Grimes 	case SSLEEP:
865df8bae1dSRodney W. Grimes 		/*
866df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
867df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
868df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
869df8bae1dSRodney W. Grimes 		 * trap() or syscall().
870df8bae1dSRodney W. Grimes 		 */
871df8bae1dSRodney W. Grimes 		if ((p->p_flag & P_SINTR) == 0)
872df8bae1dSRodney W. Grimes 			goto out;
873df8bae1dSRodney W. Grimes 		/*
874df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
875df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
876df8bae1dSRodney W. Grimes 		 * for the parent.
877df8bae1dSRodney W. Grimes 		 */
878df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
879df8bae1dSRodney W. Grimes 			goto run;
880df8bae1dSRodney W. Grimes 		/*
881df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
882df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
883df8bae1dSRodney W. Grimes 		 * be awakened.
884df8bae1dSRodney W. Grimes 		 */
885df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
886df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
887df8bae1dSRodney W. Grimes 			goto out;
888df8bae1dSRodney W. Grimes 		}
889df8bae1dSRodney W. Grimes 		/*
890df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
891df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
892df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
893df8bae1dSRodney W. Grimes 		 * cause the process to run.
894df8bae1dSRodney W. Grimes 		 */
895df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
896df8bae1dSRodney W. Grimes 			if (action != SIG_DFL)
897df8bae1dSRodney W. Grimes 				goto runfast;
898df8bae1dSRodney W. Grimes 			/*
899df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
900df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
901df8bae1dSRodney W. Grimes 			 */
902df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
903df8bae1dSRodney W. Grimes 				goto out;
904df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
905df8bae1dSRodney W. Grimes 			p->p_xstat = signum;
9066626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
907df8bae1dSRodney W. Grimes 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
9086626c604SJulian Elischer #else
9096626c604SJulian Elischer 			if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
9106626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
911df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
912df8bae1dSRodney W. Grimes 			stop(p);
913df8bae1dSRodney W. Grimes 			goto out;
914df8bae1dSRodney W. Grimes 		} else
915df8bae1dSRodney W. Grimes 			goto runfast;
916df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
917df8bae1dSRodney W. Grimes 
918df8bae1dSRodney W. Grimes 	case SSTOP:
919df8bae1dSRodney W. Grimes 		/*
920df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
921df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
922df8bae1dSRodney W. Grimes 		 */
923df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
924df8bae1dSRodney W. Grimes 			goto out;
925df8bae1dSRodney W. Grimes 
926df8bae1dSRodney W. Grimes 		/*
927df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
928df8bae1dSRodney W. Grimes 		 */
929df8bae1dSRodney W. Grimes 		if (signum == SIGKILL)
930df8bae1dSRodney W. Grimes 			goto runfast;
931df8bae1dSRodney W. Grimes 
932df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
933df8bae1dSRodney W. Grimes 			/*
934df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
935df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
936df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
937df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
938df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
939df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
940df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
941df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
942df8bae1dSRodney W. Grimes 			 */
943df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
944df8bae1dSRodney W. Grimes 				p->p_siglist &= ~mask;
945df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
946df8bae1dSRodney W. Grimes 				goto runfast;
947df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
948df8bae1dSRodney W. Grimes 				goto run;
949df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
950df8bae1dSRodney W. Grimes 			goto out;
951df8bae1dSRodney W. Grimes 		}
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
954df8bae1dSRodney W. Grimes 			/*
955df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
956df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
957df8bae1dSRodney W. Grimes 			 */
958df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;		/* take it away */
959df8bae1dSRodney W. Grimes 			goto out;
960df8bae1dSRodney W. Grimes 		}
961df8bae1dSRodney W. Grimes 
962df8bae1dSRodney W. Grimes 		/*
963df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
964df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
965df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
966df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
967df8bae1dSRodney W. Grimes 		 */
968df8bae1dSRodney W. Grimes 		if (p->p_wchan && p->p_flag & P_SINTR)
969df8bae1dSRodney W. Grimes 			unsleep(p);
970df8bae1dSRodney W. Grimes 		goto out;
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	default:
973df8bae1dSRodney W. Grimes 		/*
974df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
975df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
976df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
977df8bae1dSRodney W. Grimes 		 */
978df8bae1dSRodney W. Grimes 		if (p == curproc)
979df8bae1dSRodney W. Grimes 			signotify(p);
9803163861cSTor Egge #ifdef SMP
9813163861cSTor Egge 		else if (p->p_stat == SRUN)
9823163861cSTor Egge 			forward_signal(p);
9833163861cSTor Egge #endif
984df8bae1dSRodney W. Grimes 		goto out;
985df8bae1dSRodney W. Grimes 	}
986df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
987df8bae1dSRodney W. Grimes 
988df8bae1dSRodney W. Grimes runfast:
989df8bae1dSRodney W. Grimes 	/*
990df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
991df8bae1dSRodney W. Grimes 	 */
992df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
993df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
994df8bae1dSRodney W. Grimes run:
995df8bae1dSRodney W. Grimes 	setrunnable(p);
996df8bae1dSRodney W. Grimes out:
997df8bae1dSRodney W. Grimes 	splx(s);
998df8bae1dSRodney W. Grimes }
999df8bae1dSRodney W. Grimes 
1000df8bae1dSRodney W. Grimes /*
1001df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1002df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1003df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1004df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1005df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1006df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1007df8bae1dSRodney W. Grimes  * sequence is
1008df8bae1dSRodney W. Grimes  *
1009df8bae1dSRodney W. Grimes  *	while (signum = CURSIG(curproc))
1010df8bae1dSRodney W. Grimes  *		postsig(signum);
1011df8bae1dSRodney W. Grimes  */
101226f9a767SRodney W. Grimes int
1013df8bae1dSRodney W. Grimes issignal(p)
1014df8bae1dSRodney W. Grimes 	register struct proc *p;
1015df8bae1dSRodney W. Grimes {
1016df8bae1dSRodney W. Grimes 	register int signum, mask, prop;
1017df8bae1dSRodney W. Grimes 
1018df8bae1dSRodney W. Grimes 	for (;;) {
10192a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
10202a024a2bSSean Eric Fagan 
1021df8bae1dSRodney W. Grimes 		mask = p->p_siglist & ~p->p_sigmask;
1022df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
1023df8bae1dSRodney W. Grimes 			mask &= ~stopsigmask;
1024df8bae1dSRodney W. Grimes 		if (mask == 0)	 	/* no signal to send */
1025df8bae1dSRodney W. Grimes 			return (0);
1026df8bae1dSRodney W. Grimes 		signum = ffs((long)mask);
1027df8bae1dSRodney W. Grimes 		mask = sigmask(signum);
1028df8bae1dSRodney W. Grimes 		prop = sigprop[signum];
10292a024a2bSSean Eric Fagan 
10302a024a2bSSean Eric Fagan 		STOPEVENT(p, S_SIG, signum);
10312a024a2bSSean Eric Fagan 
1032df8bae1dSRodney W. Grimes 		/*
1033df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1034df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1035df8bae1dSRodney W. Grimes 		 */
10362a024a2bSSean Eric Fagan 		if ((mask & p->p_sigignore) && (traced == 0)) {
1037df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
1038df8bae1dSRodney W. Grimes 			continue;
1039df8bae1dSRodney W. Grimes 		}
1040df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1041df8bae1dSRodney W. Grimes 			/*
1042df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1043df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1044df8bae1dSRodney W. Grimes 			 */
1045df8bae1dSRodney W. Grimes 			p->p_xstat = signum;
1046df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1047df8bae1dSRodney W. Grimes 			do {
1048df8bae1dSRodney W. Grimes 				stop(p);
1049df8bae1dSRodney W. Grimes 				mi_switch();
10502a024a2bSSean Eric Fagan 			} while (!trace_req(p)
10512a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1052df8bae1dSRodney W. Grimes 
1053df8bae1dSRodney W. Grimes 			/*
1054df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1055df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1056df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1057df8bae1dSRodney W. Grimes 			 */
1058df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1059df8bae1dSRodney W. Grimes 				continue;
1060df8bae1dSRodney W. Grimes 
1061df8bae1dSRodney W. Grimes 			/*
1062df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1063df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1064df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1065df8bae1dSRodney W. Grimes 			 */
1066df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;	/* clear the old signal */
1067df8bae1dSRodney W. Grimes 			signum = p->p_xstat;
1068df8bae1dSRodney W. Grimes 			if (signum == 0)
1069df8bae1dSRodney W. Grimes 				continue;
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 			/*
1072df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1073df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1074df8bae1dSRodney W. Grimes 			 */
1075df8bae1dSRodney W. Grimes 			mask = sigmask(signum);
1076df8bae1dSRodney W. Grimes 			p->p_siglist |= mask;
1077df8bae1dSRodney W. Grimes 			if (p->p_sigmask & mask)
1078df8bae1dSRodney W. Grimes 				continue;
1079df8bae1dSRodney W. Grimes 		}
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes 		/*
1082df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1083df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1084df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1085df8bae1dSRodney W. Grimes 		 */
1086a23d65bfSBruce Evans 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[signum]) {
1087df8bae1dSRodney W. Grimes 
10880b53fbe8SBruce Evans 		case (int)SIG_DFL:
1089df8bae1dSRodney W. Grimes 			/*
1090df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1091df8bae1dSRodney W. Grimes 			 */
1092df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1093df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1094df8bae1dSRodney W. Grimes 				/*
1095df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1096df8bae1dSRodney W. Grimes 				 * in init? XXX
1097df8bae1dSRodney W. Grimes 				 */
1098d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
1099d93f860cSPoul-Henning Kamp 					(u_long)p->p_pid, signum);
1100df8bae1dSRodney W. Grimes #endif
1101df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1102df8bae1dSRodney W. Grimes 			}
1103df8bae1dSRodney W. Grimes 			/*
1104df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1105df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1106df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1107df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1108df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1109df8bae1dSRodney W. Grimes 			 */
1110df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1111df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1112df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1113df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1114df8bae1dSRodney W. Grimes 					break;	/* == ignore */
1115df8bae1dSRodney W. Grimes 				p->p_xstat = signum;
1116df8bae1dSRodney W. Grimes 				stop(p);
11176626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
1118df8bae1dSRodney W. Grimes 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
11196626c604SJulian Elischer #else
11206626c604SJulian Elischer 				if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
11216626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
1122df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1123df8bae1dSRodney W. Grimes 				mi_switch();
1124df8bae1dSRodney W. Grimes 				break;
1125df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1126df8bae1dSRodney W. Grimes 				/*
1127df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1128df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1129df8bae1dSRodney W. Grimes 				 */
1130df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1131df8bae1dSRodney W. Grimes 			} else
1132df8bae1dSRodney W. Grimes 				return (signum);
1133df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1134df8bae1dSRodney W. Grimes 
11350b53fbe8SBruce Evans 		case (int)SIG_IGN:
1136df8bae1dSRodney W. Grimes 			/*
1137df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1138df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1139df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1140df8bae1dSRodney W. Grimes 			 */
1141df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1142df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1143df8bae1dSRodney W. Grimes 				printf("issignal\n");
1144df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1145df8bae1dSRodney W. Grimes 
1146df8bae1dSRodney W. Grimes 		default:
1147df8bae1dSRodney W. Grimes 			/*
1148df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1149df8bae1dSRodney W. Grimes 			 * postsig() process it.
1150df8bae1dSRodney W. Grimes 			 */
1151df8bae1dSRodney W. Grimes 			return (signum);
1152df8bae1dSRodney W. Grimes 		}
1153df8bae1dSRodney W. Grimes 		p->p_siglist &= ~mask;		/* take the signal! */
1154df8bae1dSRodney W. Grimes 	}
1155df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1156df8bae1dSRodney W. Grimes }
1157df8bae1dSRodney W. Grimes 
1158df8bae1dSRodney W. Grimes /*
1159df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1160df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
1161df8bae1dSRodney W. Grimes  * on the run queue.
1162df8bae1dSRodney W. Grimes  */
116326f9a767SRodney W. Grimes void
1164df8bae1dSRodney W. Grimes stop(p)
1165df8bae1dSRodney W. Grimes 	register struct proc *p;
1166df8bae1dSRodney W. Grimes {
1167df8bae1dSRodney W. Grimes 
1168df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1169df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1170df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1171df8bae1dSRodney W. Grimes }
1172df8bae1dSRodney W. Grimes 
1173df8bae1dSRodney W. Grimes /*
1174df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1175df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1176df8bae1dSRodney W. Grimes  */
1177df8bae1dSRodney W. Grimes void
1178df8bae1dSRodney W. Grimes postsig(signum)
1179df8bae1dSRodney W. Grimes 	register int signum;
1180df8bae1dSRodney W. Grimes {
1181df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
1182df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1183df8bae1dSRodney W. Grimes 	register sig_t action;
1184df8bae1dSRodney W. Grimes 	int code, mask, returnmask;
1185df8bae1dSRodney W. Grimes 
11865526d2d9SEivind Eklund 	KASSERT(signum != 0, ("postsig"));
11875526d2d9SEivind Eklund 
1188df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
1189df8bae1dSRodney W. Grimes 	p->p_siglist &= ~mask;
1190df8bae1dSRodney W. Grimes 	action = ps->ps_sigact[signum];
1191df8bae1dSRodney W. Grimes #ifdef KTRACE
1192df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1193df8bae1dSRodney W. Grimes 		ktrpsig(p->p_tracep,
11946626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
1195df8bae1dSRodney W. Grimes 		    signum, action, ps->ps_flags & SAS_OLDMASK ?
1196df8bae1dSRodney W. Grimes 		    ps->ps_oldmask : p->p_sigmask, 0);
11976626c604SJulian Elischer #else
11986626c604SJulian Elischer 		    signum, action, p->p_oldsigmask ?
11996626c604SJulian Elischer 		    p->p_oldsigmask : p->p_sigmask, 0);
12006626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
1201df8bae1dSRodney W. Grimes #endif
12022a024a2bSSean Eric Fagan 	STOPEVENT(p, S_SIG, signum);
12032a024a2bSSean Eric Fagan 
1204df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1205df8bae1dSRodney W. Grimes 		/*
1206df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1207df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1208df8bae1dSRodney W. Grimes 		 */
1209df8bae1dSRodney W. Grimes 		sigexit(p, signum);
1210df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1211df8bae1dSRodney W. Grimes 	} else {
1212df8bae1dSRodney W. Grimes 		/*
1213df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1214df8bae1dSRodney W. Grimes 		 */
12155526d2d9SEivind Eklund 		KASSERT(action != SIG_IGN && (p->p_sigmask & mask) == 0,
12165526d2d9SEivind Eklund 			("postsig action"));
1217df8bae1dSRodney W. Grimes 		/*
1218df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1219df8bae1dSRodney W. Grimes 		 * occurences of this signal.
1220df8bae1dSRodney W. Grimes 		 *
1221df8bae1dSRodney W. Grimes 		 * Special case: user has done a sigpause.  Here the
1222df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1223df8bae1dSRodney W. Grimes 		 * mask from before the sigpause is what we want
1224df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1225df8bae1dSRodney W. Grimes 		 */
1226df8bae1dSRodney W. Grimes 		(void) splhigh();
12276626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
1228df8bae1dSRodney W. Grimes 		if (ps->ps_flags & SAS_OLDMASK) {
1229df8bae1dSRodney W. Grimes 			returnmask = ps->ps_oldmask;
1230df8bae1dSRodney W. Grimes 			ps->ps_flags &= ~SAS_OLDMASK;
12316626c604SJulian Elischer #else
12326626c604SJulian Elischer 		if (p->p_oldsigmask) {
12336626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
12346626c604SJulian Elischer 			p->p_oldsigmask = 0;
12356626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
1236df8bae1dSRodney W. Grimes 		} else
1237df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
1238289ccde0SPeter Wemm 		p->p_sigmask |= ps->ps_catchmask[signum] |
1239289ccde0SPeter Wemm 				(mask & ~ps->ps_signodefer);
1240dedc04feSPeter Wemm 		if ((ps->ps_sigreset & mask) != 0) {
1241289ccde0SPeter Wemm 			/*
1242289ccde0SPeter Wemm 			 * See setsigvec() for origin of this code.
1243289ccde0SPeter Wemm 			 */
1244dedc04feSPeter Wemm 			p->p_sigcatch &= ~mask;
1245dedc04feSPeter Wemm 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1246dedc04feSPeter Wemm 				p->p_sigignore |= mask;
1247dedc04feSPeter Wemm 			ps->ps_sigact[signum] = SIG_DFL;
1248dedc04feSPeter Wemm 		}
1249df8bae1dSRodney W. Grimes 		(void) spl0();
1250df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
12516626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
1252df8bae1dSRodney W. Grimes 		if (ps->ps_sig != signum) {
12536626c604SJulian Elischer #else
12546626c604SJulian Elischer 		if (p->p_sig != signum) {
12556626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
1256df8bae1dSRodney W. Grimes 			code = 0;
1257df8bae1dSRodney W. Grimes 		} else {
12586626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
1259df8bae1dSRodney W. Grimes 			code = ps->ps_code;
1260df8bae1dSRodney W. Grimes 			ps->ps_code = 0;
12618674077aSJeffrey Hsu 			ps->ps_sig = 0;
12626626c604SJulian Elischer #else
12636626c604SJulian Elischer 			code = p->p_code;
12646626c604SJulian Elischer 			p->p_code = 0;
12656626c604SJulian Elischer 			p->p_sig = 0;
12666626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
1267df8bae1dSRodney W. Grimes 		}
1268d66a5066SPeter Wemm 		(*p->p_sysent->sv_sendsig)(action, signum, returnmask, code);
1269df8bae1dSRodney W. Grimes 	}
1270df8bae1dSRodney W. Grimes }
1271df8bae1dSRodney W. Grimes 
1272df8bae1dSRodney W. Grimes /*
1273df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1274df8bae1dSRodney W. Grimes  */
127526f9a767SRodney W. Grimes void
1276df8bae1dSRodney W. Grimes killproc(p, why)
1277df8bae1dSRodney W. Grimes 	struct proc *p;
1278df8bae1dSRodney W. Grimes 	char *why;
1279df8bae1dSRodney W. Grimes {
1280729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1281729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1282df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1283df8bae1dSRodney W. Grimes }
1284df8bae1dSRodney W. Grimes 
1285df8bae1dSRodney W. Grimes /*
1286df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1287df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1288df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1289df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1290df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1291df8bae1dSRodney W. Grimes  * does not return.
1292df8bae1dSRodney W. Grimes  */
129326f9a767SRodney W. Grimes void
1294df8bae1dSRodney W. Grimes sigexit(p, signum)
1295df8bae1dSRodney W. Grimes 	register struct proc *p;
1296df8bae1dSRodney W. Grimes 	int signum;
1297df8bae1dSRodney W. Grimes {
1298df8bae1dSRodney W. Grimes 
1299df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
1300df8bae1dSRodney W. Grimes 	if (sigprop[signum] & SA_CORE) {
13016626c604SJulian Elischer #ifndef COMPAT_LINUX_THREADS
1302df8bae1dSRodney W. Grimes 		p->p_sigacts->ps_sig = signum;
13036626c604SJulian Elischer #else
13046626c604SJulian Elischer 		p->p_sig = signum;
13056626c604SJulian Elischer #endif /* COMPAT_LINUX_THREADS */
1306c364e17eSAndrey A. Chernov 		/*
1307c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1308c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1309c364e17eSAndrey A. Chernov 		 * these messages.)
1310c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1311c364e17eSAndrey A. Chernov 		 */
131222d4b0fbSJohn Polstra 		if (p->p_sysent->sv_coredump != NULL &&
131322d4b0fbSJohn Polstra 		    (*p->p_sysent->sv_coredump)(p) == 0)
1314df8bae1dSRodney W. Grimes 			signum |= WCOREFLAG;
131557308494SJoerg Wunsch 		if (kern_logsigexit)
131657308494SJoerg Wunsch 			log(LOG_INFO,
131757308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
13183d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
13193d1b21c6SAndrey A. Chernov 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
13203d1b21c6SAndrey A. Chernov 			    signum &~ WCOREFLAG,
13213d1b21c6SAndrey A. Chernov 			    signum & WCOREFLAG ? " (core dumped)" : "");
1322df8bae1dSRodney W. Grimes 	}
1323df8bae1dSRodney W. Grimes 	exit1(p, W_EXITCODE(0, signum));
1324df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1325df8bae1dSRodney W. Grimes }
1326df8bae1dSRodney W. Grimes 
1327c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1328c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1329c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1330c5edb423SSean Eric Fagan 
1331c5edb423SSean Eric Fagan /*
1332c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1333c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1334c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1335c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1336c5edb423SSean Eric Fagan  *	%P	process id (pid)
1337c5edb423SSean Eric Fagan  *	%U	user id (uid)
1338c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1339c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1340c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1341c5edb423SSean Eric Fagan  */
1342c5edb423SSean Eric Fagan 
134322d4b0fbSJohn Polstra char *
1344c5edb423SSean Eric Fagan expand_name(name, uid, pid)
1345c5edb423SSean Eric Fagan const char *name; int uid; int pid; {
1346c5edb423SSean Eric Fagan 	char *temp;
1347c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1348c5edb423SSean Eric Fagan 	int i, n;
1349c5edb423SSean Eric Fagan 	char *format = corefilename;
1350c5edb423SSean Eric Fagan 
1351c5edb423SSean Eric Fagan 	temp = malloc(MAXPATHLEN + 3, M_TEMP, M_NOWAIT);
13520bfe2990SEivind Eklund 	if (temp == NULL)
13530bfe2990SEivind Eklund 		return NULL;
1354c5edb423SSean Eric Fagan 	bzero(temp, MAXPATHLEN+3);
1355c5edb423SSean Eric Fagan 	for (i = 0, n = 0; i < MAXPATHLEN && format[i]; i++) {
1356c5edb423SSean Eric Fagan 		int l;
1357c5edb423SSean Eric Fagan 		switch (format[i]) {
1358c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1359c5edb423SSean Eric Fagan 			i++;
1360c5edb423SSean Eric Fagan 			switch (format[i]) {
1361c5edb423SSean Eric Fagan 			case '%':
1362c5edb423SSean Eric Fagan 				temp[n++] = '%';
1363c5edb423SSean Eric Fagan 				break;
1364c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1365c5edb423SSean Eric Fagan 				l = strlen(name);
1366c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
1367c5edb423SSean Eric Fagan 					log(LOG_ERR, "pid %d (%s), uid (%d):  Path `%s%s' is too long\n",
1368c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1369c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1370c5edb423SSean Eric Fagan 					return NULL;
1371c5edb423SSean Eric Fagan 				}
1372c5edb423SSean Eric Fagan 				memcpy(temp+n, name, l);
1373c5edb423SSean Eric Fagan 				n += l;
1374c5edb423SSean Eric Fagan 				break;
1375c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1376c5edb423SSean Eric Fagan 				sprintf(buf, "%u", pid);
1377c5edb423SSean Eric Fagan 				l = strlen(buf);
1378c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
1379c5edb423SSean Eric Fagan 					log(LOG_ERR, "pid %d (%s), uid (%d):  Path `%s%s' is too long\n",
1380c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1381c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1382c5edb423SSean Eric Fagan 					return NULL;
1383c5edb423SSean Eric Fagan 				}
1384c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1385c5edb423SSean Eric Fagan 				n += l;
1386c5edb423SSean Eric Fagan 				break;
1387c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1388c5edb423SSean Eric Fagan 				sprintf(buf, "%u", uid);
1389c5edb423SSean Eric Fagan 				l = strlen(buf);
1390c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
1391c5edb423SSean Eric Fagan 					log(LOG_ERR, "pid %d (%s), uid (%d):  Path `%s%s' is too long\n",
1392c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1393c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1394c5edb423SSean Eric Fagan 					return NULL;
1395c5edb423SSean Eric Fagan 				}
1396c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1397c5edb423SSean Eric Fagan 				n += l;
1398c5edb423SSean Eric Fagan 				break;
1399c5edb423SSean Eric Fagan 			default:
1400c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1401c5edb423SSean Eric Fagan 			}
1402c5edb423SSean Eric Fagan 			break;
1403c5edb423SSean Eric Fagan 		default:
1404c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1405c5edb423SSean Eric Fagan 		}
1406c5edb423SSean Eric Fagan 	}
1407c5edb423SSean Eric Fagan 	return temp;
1408c5edb423SSean Eric Fagan }
1409c5edb423SSean Eric Fagan 
1410df8bae1dSRodney W. Grimes /*
1411df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1412df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1413df8bae1dSRodney W. Grimes  */
1414d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1415df8bae1dSRodney W. Grimes struct nosys_args {
1416df8bae1dSRodney W. Grimes 	int	dummy;
1417df8bae1dSRodney W. Grimes };
1418d2d3e875SBruce Evans #endif
1419df8bae1dSRodney W. Grimes /* ARGSUSED */
142026f9a767SRodney W. Grimes int
1421cb226aaaSPoul-Henning Kamp nosys(p, args)
1422df8bae1dSRodney W. Grimes 	struct proc *p;
1423df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1424df8bae1dSRodney W. Grimes {
1425df8bae1dSRodney W. Grimes 
1426df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1427df8bae1dSRodney W. Grimes 	return (EINVAL);
1428df8bae1dSRodney W. Grimes }
1429831d27a9SDon Lewis 
1430831d27a9SDon Lewis /*
1431831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1432831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1433831d27a9SDon Lewis  */
1434831d27a9SDon Lewis void
1435831d27a9SDon Lewis pgsigio(sigio, signum, checkctty)
1436831d27a9SDon Lewis 	struct sigio *sigio;
1437831d27a9SDon Lewis 	int signum, checkctty;
1438831d27a9SDon Lewis {
1439831d27a9SDon Lewis 	if (sigio == NULL)
1440831d27a9SDon Lewis 		return;
1441831d27a9SDon Lewis 
1442831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1443831d27a9SDon Lewis 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1444831d27a9SDon Lewis 		             sigio->sio_proc))
1445831d27a9SDon Lewis 			psignal(sigio->sio_proc, signum);
1446831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1447831d27a9SDon Lewis 		struct proc *p;
1448831d27a9SDon Lewis 
1449831d27a9SDon Lewis 		for (p = sigio->sio_pgrp->pg_members.lh_first; p != NULL;
1450831d27a9SDon Lewis 		     p = p->p_pglist.le_next)
1451831d27a9SDon Lewis 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1452831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1453831d27a9SDon Lewis 				psignal(p, signum);
1454831d27a9SDon Lewis 	}
1455831d27a9SDon Lewis }
1456