xref: /freebsd/sys/kern/kern_sig.c (revision fca666a142a43f8df465b0c681b64cf940a229da)
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 #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 *));
74fca666a1SJulian Elischer static char *expand_name	__P((const char *, uid_t, int));
75fca666a1SJulian Elischer static int coredump	__P((struct proc *));
7626f9a767SRodney W. Grimes 
7757308494SJoerg Wunsch static int	kern_logsigexit = 1;
783d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
793d177f46SBill Fumerola     &kern_logsigexit, 0,
803d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
8157308494SJoerg Wunsch 
82df8bae1dSRodney W. Grimes /*
83df8bae1dSRodney W. Grimes  * Can process p, with pcred pc, send the signal signum to process q?
84df8bae1dSRodney W. Grimes  */
85df8bae1dSRodney W. Grimes #define CANSIGNAL(p, pc, q, signum) \
8675c13541SPoul-Henning Kamp 	(PRISON_CHECK(p, q) && ((pc)->pc_ucred->cr_uid == 0 || \
87df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
88df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
89df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
90df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
9175c13541SPoul-Henning Kamp 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session)))
92df8bae1dSRodney W. Grimes 
93831d27a9SDon Lewis /*
94831d27a9SDon Lewis  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
95831d27a9SDon Lewis  */
96831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \
97831d27a9SDon Lewis 	((uc)->cr_uid == 0 || \
98831d27a9SDon Lewis 	    (ruid) == (q)->p_cred->p_ruid || \
99831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_cred->p_ruid || \
100831d27a9SDon Lewis 	    (ruid) == (q)->p_ucred->cr_uid || \
101831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
102831d27a9SDon Lewis 
10322d4b0fbSJohn Polstra int sugid_coredump;
1043d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1053d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
106c87e2930SDavid Greenman 
107d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
108df8bae1dSRodney W. Grimes struct sigaction_args {
109df8bae1dSRodney W. Grimes 	int	signum;
110df8bae1dSRodney W. Grimes 	struct	sigaction *nsa;
111df8bae1dSRodney W. Grimes 	struct	sigaction *osa;
112df8bae1dSRodney W. Grimes };
113d2d3e875SBruce Evans #endif
114df8bae1dSRodney W. Grimes /* ARGSUSED */
11526f9a767SRodney W. Grimes int
116cb226aaaSPoul-Henning Kamp sigaction(p, uap)
117df8bae1dSRodney W. Grimes 	struct proc *p;
118df8bae1dSRodney W. Grimes 	register struct sigaction_args *uap;
119df8bae1dSRodney W. Grimes {
120df8bae1dSRodney W. Grimes 	struct sigaction vec;
121df8bae1dSRodney W. Grimes 	register struct sigaction *sa;
122df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
123df8bae1dSRodney W. Grimes 	register int signum;
124df8bae1dSRodney W. Grimes 	int bit, error;
125df8bae1dSRodney W. Grimes 
126df8bae1dSRodney W. Grimes 	signum = uap->signum;
127e6eeb36dSBruce Evans 	if (signum <= 0 || signum >= NSIG)
128df8bae1dSRodney W. Grimes 		return (EINVAL);
129df8bae1dSRodney W. Grimes 	sa = &vec;
130df8bae1dSRodney W. Grimes 	if (uap->osa) {
131aff66c54SMartin Cracauer 		bit = sigmask(signum);
132aff66c54SMartin Cracauer 		if ((ps->ps_siginfo & bit) != 0)
133aff66c54SMartin Cracauer 			sa->sa_sigaction =
134aff66c54SMartin Cracauer 			    (__siginfohandler_t *)ps->ps_sigact[signum];
135aff66c54SMartin Cracauer 		else
136df8bae1dSRodney W. Grimes 			sa->sa_handler = ps->ps_sigact[signum];
137df8bae1dSRodney W. Grimes 		sa->sa_mask = ps->ps_catchmask[signum];
138df8bae1dSRodney W. Grimes 		sa->sa_flags = 0;
139df8bae1dSRodney W. Grimes 		if ((ps->ps_sigonstack & bit) != 0)
140df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_ONSTACK;
141df8bae1dSRodney W. Grimes 		if ((ps->ps_sigintr & bit) == 0)
142df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_RESTART;
143dedc04feSPeter Wemm 		if ((ps->ps_sigreset & bit) != 0)
144dedc04feSPeter Wemm 			sa->sa_flags |= SA_RESETHAND;
145289ccde0SPeter Wemm 		if ((ps->ps_signodefer & bit) != 0)
146dedc04feSPeter Wemm 			sa->sa_flags |= SA_NODEFER;
147aff66c54SMartin Cracauer 		if ((ps->ps_siginfo & bit) != 0)
148aff66c54SMartin Cracauer 			sa->sa_flags |= SA_SIGINFO;
1496626c604SJulian Elischer 		if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
150289ccde0SPeter Wemm 			sa->sa_flags |= SA_NOCLDSTOP;
1516626c604SJulian Elischer 		if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDWAIT)
152245f17d4SJoerg Wunsch 			sa->sa_flags |= SA_NOCLDWAIT;
153bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa,
154bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
155df8bae1dSRodney W. Grimes 			return (error);
156df8bae1dSRodney W. Grimes 	}
157df8bae1dSRodney W. Grimes 	if (uap->nsa) {
158bb56ec4aSPoul-Henning Kamp 		if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
159bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
160df8bae1dSRodney W. Grimes 			return (error);
161e6eeb36dSBruce Evans 		if ((signum == SIGKILL || signum == SIGSTOP) &&
16284300d62SMartin Cracauer 		    sa->sa_handler != SIG_DFL)
163e6eeb36dSBruce Evans 			return (EINVAL);
164df8bae1dSRodney W. Grimes 		setsigvec(p, signum, sa);
165df8bae1dSRodney W. Grimes 	}
166df8bae1dSRodney W. Grimes 	return (0);
167df8bae1dSRodney W. Grimes }
168df8bae1dSRodney W. Grimes 
1698a2d9f50SBruce Evans static void
170df8bae1dSRodney W. Grimes setsigvec(p, signum, sa)
171df8bae1dSRodney W. Grimes 	register struct proc *p;
172df8bae1dSRodney W. Grimes 	int signum;
173df8bae1dSRodney W. Grimes 	register struct sigaction *sa;
174df8bae1dSRodney W. Grimes {
175df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
176df8bae1dSRodney W. Grimes 	register int bit;
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes 	bit = sigmask(signum);
179df8bae1dSRodney W. Grimes 	/*
180df8bae1dSRodney W. Grimes 	 * Change setting atomically.
181df8bae1dSRodney W. Grimes 	 */
182df8bae1dSRodney W. Grimes 	(void) splhigh();
183df8bae1dSRodney W. Grimes 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
184aff66c54SMartin Cracauer 	if (sa->sa_flags & SA_SIGINFO) {
185aff66c54SMartin Cracauer 		ps->ps_sigact[signum] = sa->sa_handler;
186aff66c54SMartin Cracauer 		ps->ps_siginfo |= bit;
187aff66c54SMartin Cracauer 	} else {
188aff66c54SMartin Cracauer 		ps->ps_sigact[signum] = (__sighandler_t *)sa->sa_sigaction;
189aff66c54SMartin Cracauer 		ps->ps_siginfo &= ~bit;
190aff66c54SMartin Cracauer 	}
191df8bae1dSRodney W. Grimes 	if ((sa->sa_flags & SA_RESTART) == 0)
192df8bae1dSRodney W. Grimes 		ps->ps_sigintr |= bit;
193df8bae1dSRodney W. Grimes 	else
194df8bae1dSRodney W. Grimes 		ps->ps_sigintr &= ~bit;
195df8bae1dSRodney W. Grimes 	if (sa->sa_flags & SA_ONSTACK)
196df8bae1dSRodney W. Grimes 		ps->ps_sigonstack |= bit;
197df8bae1dSRodney W. Grimes 	else
198df8bae1dSRodney W. Grimes 		ps->ps_sigonstack &= ~bit;
199dedc04feSPeter Wemm 	if (sa->sa_flags & SA_RESETHAND)
200dedc04feSPeter Wemm 		ps->ps_sigreset |= bit;
2011e41c1b5SSteven Wallace 	else
202dedc04feSPeter Wemm 		ps->ps_sigreset &= ~bit;
203289ccde0SPeter Wemm 	if (sa->sa_flags & SA_NODEFER)
204289ccde0SPeter Wemm 		ps->ps_signodefer |= bit;
205289ccde0SPeter Wemm 	else
206289ccde0SPeter Wemm 		ps->ps_signodefer &= ~bit;
207df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
208df8bae1dSRodney W. Grimes 	if (sa->sa_flags & SA_USERTRAMP)
209df8bae1dSRodney W. Grimes 		ps->ps_usertramp |= bit;
210df8bae1dSRodney W. Grimes 	else
211df8bae1dSRodney W. Grimes 		ps->ps_usertramp &= ~bit;
212df8bae1dSRodney W. Grimes #endif
213df8bae1dSRodney W. Grimes 	if (signum == SIGCHLD) {
214df8bae1dSRodney W. Grimes 		if (sa->sa_flags & SA_NOCLDSTOP)
2156626c604SJulian Elischer 			p->p_procsig->ps_flag |= P_NOCLDSTOP;
2166626c604SJulian Elischer 		else
2176626c604SJulian Elischer 			p->p_procsig->ps_flag &= ~P_NOCLDSTOP;
218245f17d4SJoerg Wunsch 		if (sa->sa_flags & SA_NOCLDWAIT) {
219245f17d4SJoerg Wunsch 			/*
220245f17d4SJoerg Wunsch 			 * Paranoia: since SA_NOCLDWAIT is implemented by
221245f17d4SJoerg Wunsch 			 * reparenting the dying child to PID 1 (and
222245f17d4SJoerg Wunsch 			 * trust it to reap the zombie), PID 1 itself is
223245f17d4SJoerg Wunsch 			 * forbidden to set SA_NOCLDWAIT.
224245f17d4SJoerg Wunsch 			 */
225245f17d4SJoerg Wunsch 			if (p->p_pid == 1)
2266626c604SJulian Elischer 				p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
2276626c604SJulian Elischer 			else
2286626c604SJulian Elischer 				p->p_procsig->ps_flag |= P_NOCLDWAIT;
229245f17d4SJoerg Wunsch 		} else
2306626c604SJulian Elischer 			p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
231df8bae1dSRodney W. Grimes 	}
232df8bae1dSRodney W. Grimes 	/*
233df8bae1dSRodney W. Grimes 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
234df8bae1dSRodney W. Grimes 	 * and for signals set to SIG_DFL where the default is to ignore.
235df8bae1dSRodney W. Grimes 	 * However, don't put SIGCONT in p_sigignore,
236df8bae1dSRodney W. Grimes 	 * as we have to restart the process.
237df8bae1dSRodney W. Grimes 	 */
238aff66c54SMartin Cracauer 	if (ps->ps_sigact[signum] == SIG_IGN ||
239aff66c54SMartin Cracauer 	    (sigprop[signum] & SA_IGNORE && ps->ps_sigact[signum] == SIG_DFL)) {
240df8bae1dSRodney W. Grimes 		p->p_siglist &= ~bit;		/* never to be seen again */
241df8bae1dSRodney W. Grimes 		if (signum != SIGCONT)
242df8bae1dSRodney W. Grimes 			p->p_sigignore |= bit;	/* easier in psignal */
243df8bae1dSRodney W. Grimes 		p->p_sigcatch &= ~bit;
244df8bae1dSRodney W. Grimes 	} else {
245df8bae1dSRodney W. Grimes 		p->p_sigignore &= ~bit;
246aff66c54SMartin Cracauer 		if (ps->ps_sigact[signum] == SIG_DFL)
247df8bae1dSRodney W. Grimes 			p->p_sigcatch &= ~bit;
248df8bae1dSRodney W. Grimes 		else
249df8bae1dSRodney W. Grimes 			p->p_sigcatch |= bit;
250df8bae1dSRodney W. Grimes 	}
251df8bae1dSRodney W. Grimes 	(void) spl0();
252df8bae1dSRodney W. Grimes }
253df8bae1dSRodney W. Grimes 
254df8bae1dSRodney W. Grimes /*
255df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
256df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
257df8bae1dSRodney W. Grimes  */
258df8bae1dSRodney W. Grimes void
259df8bae1dSRodney W. Grimes siginit(p)
260df8bae1dSRodney W. Grimes 	struct proc *p;
261df8bae1dSRodney W. Grimes {
262df8bae1dSRodney W. Grimes 	register int i;
263df8bae1dSRodney W. Grimes 
264df8bae1dSRodney W. Grimes 	for (i = 0; i < NSIG; i++)
265df8bae1dSRodney W. Grimes 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
266df8bae1dSRodney W. Grimes 			p->p_sigignore |= sigmask(i);
267df8bae1dSRodney W. Grimes }
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes /*
270df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
271df8bae1dSRodney W. Grimes  */
272df8bae1dSRodney W. Grimes void
273df8bae1dSRodney W. Grimes execsigs(p)
274df8bae1dSRodney W. Grimes 	register struct proc *p;
275df8bae1dSRodney W. Grimes {
276df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
277df8bae1dSRodney W. Grimes 	register int nc, mask;
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes 	/*
280df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
281df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
282df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
283df8bae1dSRodney W. Grimes 	 */
284df8bae1dSRodney W. Grimes 	while (p->p_sigcatch) {
285df8bae1dSRodney W. Grimes 		nc = ffs((long)p->p_sigcatch);
286df8bae1dSRodney W. Grimes 		mask = sigmask(nc);
287df8bae1dSRodney W. Grimes 		p->p_sigcatch &= ~mask;
288df8bae1dSRodney W. Grimes 		if (sigprop[nc] & SA_IGNORE) {
289df8bae1dSRodney W. Grimes 			if (nc != SIGCONT)
290df8bae1dSRodney W. Grimes 				p->p_sigignore |= mask;
291df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
292df8bae1dSRodney W. Grimes 		}
293df8bae1dSRodney W. Grimes 		ps->ps_sigact[nc] = SIG_DFL;
294df8bae1dSRodney W. Grimes 	}
295df8bae1dSRodney W. Grimes 	/*
296df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
297df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
298df8bae1dSRodney W. Grimes 	 */
299d66a5066SPeter Wemm 	ps->ps_sigstk.ss_flags = SS_DISABLE;
300df8bae1dSRodney W. Grimes 	ps->ps_sigstk.ss_size = 0;
301aa98692fSAndreas Schulz 	ps->ps_sigstk.ss_sp = 0;
302df8bae1dSRodney W. Grimes 	ps->ps_flags = 0;
30380e907a1SPeter Wemm 	/*
30480e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
30580e907a1SPeter Wemm 	 */
30680e907a1SPeter Wemm 	p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
307df8bae1dSRodney W. Grimes }
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes /*
310df8bae1dSRodney W. Grimes  * Manipulate signal mask.
311df8bae1dSRodney W. Grimes  * Note that we receive new mask, not pointer,
312df8bae1dSRodney W. Grimes  * and return old mask as return value;
313df8bae1dSRodney W. Grimes  * the library stub does the rest.
314df8bae1dSRodney W. Grimes  */
315d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
316df8bae1dSRodney W. Grimes struct sigprocmask_args {
317df8bae1dSRodney W. Grimes 	int	how;
318df8bae1dSRodney W. Grimes 	sigset_t mask;
319df8bae1dSRodney W. Grimes };
320d2d3e875SBruce Evans #endif
32126f9a767SRodney W. Grimes int
322cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
323df8bae1dSRodney W. Grimes 	register struct proc *p;
324df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
325df8bae1dSRodney W. Grimes {
326df8bae1dSRodney W. Grimes 	int error = 0;
327df8bae1dSRodney W. Grimes 
328cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
329df8bae1dSRodney W. Grimes 	(void) splhigh();
330df8bae1dSRodney W. Grimes 
331df8bae1dSRodney W. Grimes 	switch (uap->how) {
332df8bae1dSRodney W. Grimes 	case SIG_BLOCK:
333df8bae1dSRodney W. Grimes 		p->p_sigmask |= uap->mask &~ sigcantmask;
334df8bae1dSRodney W. Grimes 		break;
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	case SIG_UNBLOCK:
337df8bae1dSRodney W. Grimes 		p->p_sigmask &= ~uap->mask;
338df8bae1dSRodney W. Grimes 		break;
339df8bae1dSRodney W. Grimes 
340df8bae1dSRodney W. Grimes 	case SIG_SETMASK:
341df8bae1dSRodney W. Grimes 		p->p_sigmask = uap->mask &~ sigcantmask;
342df8bae1dSRodney W. Grimes 		break;
343df8bae1dSRodney W. Grimes 
344df8bae1dSRodney W. Grimes 	default:
345df8bae1dSRodney W. Grimes 		error = EINVAL;
346df8bae1dSRodney W. Grimes 		break;
347df8bae1dSRodney W. Grimes 	}
348df8bae1dSRodney W. Grimes 	(void) spl0();
349df8bae1dSRodney W. Grimes 	return (error);
350df8bae1dSRodney W. Grimes }
351df8bae1dSRodney W. Grimes 
352d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
353df8bae1dSRodney W. Grimes struct sigpending_args {
354df8bae1dSRodney W. Grimes 	int	dummy;
355df8bae1dSRodney W. Grimes };
356d2d3e875SBruce Evans #endif
357df8bae1dSRodney W. Grimes /* ARGSUSED */
35826f9a767SRodney W. Grimes int
359cb226aaaSPoul-Henning Kamp sigpending(p, uap)
360df8bae1dSRodney W. Grimes 	struct proc *p;
361df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
362df8bae1dSRodney W. Grimes {
363df8bae1dSRodney W. Grimes 
364cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_siglist;
365df8bae1dSRodney W. Grimes 	return (0);
366df8bae1dSRodney W. Grimes }
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
369df8bae1dSRodney W. Grimes /*
370df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
371df8bae1dSRodney W. Grimes  */
372d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
373df8bae1dSRodney W. Grimes struct osigvec_args {
374df8bae1dSRodney W. Grimes 	int	signum;
375df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
376df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
377df8bae1dSRodney W. Grimes };
378d2d3e875SBruce Evans #endif
379df8bae1dSRodney W. Grimes /* ARGSUSED */
38026f9a767SRodney W. Grimes int
381cb226aaaSPoul-Henning Kamp osigvec(p, uap)
382df8bae1dSRodney W. Grimes 	struct proc *p;
383df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
384df8bae1dSRodney W. Grimes {
385df8bae1dSRodney W. Grimes 	struct sigvec vec;
386df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
387df8bae1dSRodney W. Grimes 	register struct sigvec *sv;
388df8bae1dSRodney W. Grimes 	register int signum;
389df8bae1dSRodney W. Grimes 	int bit, error;
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes 	signum = uap->signum;
392e6eeb36dSBruce Evans 	if (signum <= 0 || signum >= NSIG)
393df8bae1dSRodney W. Grimes 		return (EINVAL);
394df8bae1dSRodney W. Grimes 	sv = &vec;
395df8bae1dSRodney W. Grimes 	if (uap->osv) {
396df8bae1dSRodney W. Grimes 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
397df8bae1dSRodney W. Grimes 		sv->sv_mask = ps->ps_catchmask[signum];
398df8bae1dSRodney W. Grimes 		bit = sigmask(signum);
399df8bae1dSRodney W. Grimes 		sv->sv_flags = 0;
400df8bae1dSRodney W. Grimes 		if ((ps->ps_sigonstack & bit) != 0)
401df8bae1dSRodney W. Grimes 			sv->sv_flags |= SV_ONSTACK;
402df8bae1dSRodney W. Grimes 		if ((ps->ps_sigintr & bit) != 0)
403df8bae1dSRodney W. Grimes 			sv->sv_flags |= SV_INTERRUPT;
404289ccde0SPeter Wemm 		if ((ps->ps_sigreset & bit) != 0)
405289ccde0SPeter Wemm 			sv->sv_flags |= SV_RESETHAND;
406289ccde0SPeter Wemm 		if ((ps->ps_signodefer & bit) != 0)
407289ccde0SPeter Wemm 			sv->sv_flags |= SV_NODEFER;
408aff66c54SMartin Cracauer 		if ((ps->ps_siginfo & bit) != 0)
409aff66c54SMartin Cracauer 			sv->sv_flags |= SV_SIGINFO;
410df8bae1dSRodney W. Grimes #ifndef COMPAT_SUNOS
4116626c604SJulian Elischer 		if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
412289ccde0SPeter Wemm 			sv->sv_flags |= SV_NOCLDSTOP;
413df8bae1dSRodney W. Grimes #endif
414bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv,
415bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
416df8bae1dSRodney W. Grimes 			return (error);
417df8bae1dSRodney W. Grimes 	}
418df8bae1dSRodney W. Grimes 	if (uap->nsv) {
419bb56ec4aSPoul-Henning Kamp 		if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
420bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
421df8bae1dSRodney W. Grimes 			return (error);
422e6eeb36dSBruce Evans 		if ((signum == SIGKILL || signum == SIGSTOP) &&
423e6eeb36dSBruce Evans 		    sv->sv_handler != SIG_DFL)
424e6eeb36dSBruce Evans 			return (EINVAL);
425df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
426df8bae1dSRodney W. Grimes 		sv->sv_flags |= SA_USERTRAMP;
427df8bae1dSRodney W. Grimes #endif
428df8bae1dSRodney W. Grimes 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
429df8bae1dSRodney W. Grimes 		setsigvec(p, signum, (struct sigaction *)sv);
430df8bae1dSRodney W. Grimes 	}
431df8bae1dSRodney W. Grimes 	return (0);
432df8bae1dSRodney W. Grimes }
433df8bae1dSRodney W. Grimes 
434d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
435df8bae1dSRodney W. Grimes struct osigblock_args {
436df8bae1dSRodney W. Grimes 	int	mask;
437df8bae1dSRodney W. Grimes };
438d2d3e875SBruce Evans #endif
43926f9a767SRodney W. Grimes int
440cb226aaaSPoul-Henning Kamp osigblock(p, uap)
441df8bae1dSRodney W. Grimes 	register struct proc *p;
442df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
443df8bae1dSRodney W. Grimes {
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes 	(void) splhigh();
446cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
447df8bae1dSRodney W. Grimes 	p->p_sigmask |= uap->mask &~ sigcantmask;
448df8bae1dSRodney W. Grimes 	(void) spl0();
449df8bae1dSRodney W. Grimes 	return (0);
450df8bae1dSRodney W. Grimes }
451df8bae1dSRodney W. Grimes 
452d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
453df8bae1dSRodney W. Grimes struct osigsetmask_args {
454df8bae1dSRodney W. Grimes 	int	mask;
455df8bae1dSRodney W. Grimes };
456d2d3e875SBruce Evans #endif
45726f9a767SRodney W. Grimes int
458cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
459df8bae1dSRodney W. Grimes 	struct proc *p;
460df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
461df8bae1dSRodney W. Grimes {
462df8bae1dSRodney W. Grimes 
463df8bae1dSRodney W. Grimes 	(void) splhigh();
464cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
465df8bae1dSRodney W. Grimes 	p->p_sigmask = uap->mask &~ sigcantmask;
466df8bae1dSRodney W. Grimes 	(void) spl0();
467df8bae1dSRodney W. Grimes 	return (0);
468df8bae1dSRodney W. Grimes }
469df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes /*
472df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
473df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
474df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
475df8bae1dSRodney W. Grimes  */
476d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
477df8bae1dSRodney W. Grimes struct sigsuspend_args {
478df8bae1dSRodney W. Grimes 	sigset_t mask;
479df8bae1dSRodney W. Grimes };
480d2d3e875SBruce Evans #endif
481df8bae1dSRodney W. Grimes /* ARGSUSED */
48226f9a767SRodney W. Grimes int
483cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
484df8bae1dSRodney W. Grimes 	register struct proc *p;
485df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
486df8bae1dSRodney W. Grimes {
487df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 	/*
490df8bae1dSRodney W. Grimes 	 * When returning from sigpause, we want
491df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
492df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
493df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
494df8bae1dSRodney W. Grimes 	 * to indicate this.
495df8bae1dSRodney W. Grimes 	 */
4966626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
497df8bae1dSRodney W. Grimes 	p->p_sigmask = uap->mask &~ sigcantmask;
498df8bae1dSRodney W. Grimes 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
499df8bae1dSRodney W. Grimes 		/* void */;
500df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
501df8bae1dSRodney W. Grimes 	return (EINTR);
502df8bae1dSRodney W. Grimes }
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
505d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
506df8bae1dSRodney W. Grimes struct osigstack_args {
507df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
508df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
509df8bae1dSRodney W. Grimes };
510d2d3e875SBruce Evans #endif
511df8bae1dSRodney W. Grimes /* ARGSUSED */
51226f9a767SRodney W. Grimes int
513cb226aaaSPoul-Henning Kamp osigstack(p, uap)
514df8bae1dSRodney W. Grimes 	struct proc *p;
515df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
516df8bae1dSRodney W. Grimes {
517df8bae1dSRodney W. Grimes 	struct sigstack ss;
518df8bae1dSRodney W. Grimes 	struct sigacts *psp;
519df8bae1dSRodney W. Grimes 	int error = 0;
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 	psp = p->p_sigacts;
522aa98692fSAndreas Schulz 	ss.ss_sp = psp->ps_sigstk.ss_sp;
523d66a5066SPeter Wemm 	ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
524df8bae1dSRodney W. Grimes 	if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
525df8bae1dSRodney W. Grimes 	    sizeof (struct sigstack))))
526df8bae1dSRodney W. Grimes 		return (error);
527df8bae1dSRodney W. Grimes 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
528df8bae1dSRodney W. Grimes 	    sizeof (ss))) == 0) {
529aa98692fSAndreas Schulz 		psp->ps_sigstk.ss_sp = ss.ss_sp;
530df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_size = 0;
531d66a5066SPeter Wemm 		psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
532df8bae1dSRodney W. Grimes 		psp->ps_flags |= SAS_ALTSTACK;
533df8bae1dSRodney W. Grimes 	}
534df8bae1dSRodney W. Grimes 	return (error);
535df8bae1dSRodney W. Grimes }
536df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
537df8bae1dSRodney W. Grimes 
538d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
539df8bae1dSRodney W. Grimes struct sigaltstack_args {
540df8bae1dSRodney W. Grimes 	struct	sigaltstack *nss;
541df8bae1dSRodney W. Grimes 	struct	sigaltstack *oss;
542df8bae1dSRodney W. Grimes };
543d2d3e875SBruce Evans #endif
544df8bae1dSRodney W. Grimes /* ARGSUSED */
54526f9a767SRodney W. Grimes int
546cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
547df8bae1dSRodney W. Grimes 	struct proc *p;
548df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
549df8bae1dSRodney W. Grimes {
550df8bae1dSRodney W. Grimes 	struct sigacts *psp;
551df8bae1dSRodney W. Grimes 	struct sigaltstack ss;
552df8bae1dSRodney W. Grimes 	int error;
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes 	psp = p->p_sigacts;
555df8bae1dSRodney W. Grimes 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
556d66a5066SPeter Wemm 		psp->ps_sigstk.ss_flags |= SS_DISABLE;
557df8bae1dSRodney W. Grimes 	if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
558df8bae1dSRodney W. Grimes 	    (caddr_t)uap->oss, sizeof (struct sigaltstack))))
559df8bae1dSRodney W. Grimes 		return (error);
560df8bae1dSRodney W. Grimes 	if (uap->nss == 0)
561df8bae1dSRodney W. Grimes 		return (0);
562bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss))))
563df8bae1dSRodney W. Grimes 		return (error);
564d66a5066SPeter Wemm 	if (ss.ss_flags & SS_DISABLE) {
565d66a5066SPeter Wemm 		if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
566df8bae1dSRodney W. Grimes 			return (EINVAL);
567df8bae1dSRodney W. Grimes 		psp->ps_flags &= ~SAS_ALTSTACK;
568df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_flags = ss.ss_flags;
569df8bae1dSRodney W. Grimes 		return (0);
570df8bae1dSRodney W. Grimes 	}
571df8bae1dSRodney W. Grimes 	if (ss.ss_size < MINSIGSTKSZ)
572df8bae1dSRodney W. Grimes 		return (ENOMEM);
573df8bae1dSRodney W. Grimes 	psp->ps_flags |= SAS_ALTSTACK;
574df8bae1dSRodney W. Grimes 	psp->ps_sigstk= ss;
575df8bae1dSRodney W. Grimes 	return (0);
576df8bae1dSRodney W. Grimes }
577df8bae1dSRodney W. Grimes 
578d93f860cSPoul-Henning Kamp /*
579d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
580d93f860cSPoul-Henning Kamp  * cp is calling process.
581d93f860cSPoul-Henning Kamp  */
582d93f860cSPoul-Henning Kamp int
583d93f860cSPoul-Henning Kamp killpg1(cp, signum, pgid, all)
584d93f860cSPoul-Henning Kamp 	register struct proc *cp;
585d93f860cSPoul-Henning Kamp 	int signum, pgid, all;
586d93f860cSPoul-Henning Kamp {
587d93f860cSPoul-Henning Kamp 	register struct proc *p;
588d93f860cSPoul-Henning Kamp 	register struct pcred *pc = cp->p_cred;
589d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
590d93f860cSPoul-Henning Kamp 	int nfound = 0;
591d93f860cSPoul-Henning Kamp 
592d93f860cSPoul-Henning Kamp 	if (all)
593d93f860cSPoul-Henning Kamp 		/*
594d93f860cSPoul-Henning Kamp 		 * broadcast
595d93f860cSPoul-Henning Kamp 		 */
596b75356e1SJeffrey Hsu 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
597d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
598d93f860cSPoul-Henning Kamp 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
599d93f860cSPoul-Henning Kamp 				continue;
600d93f860cSPoul-Henning Kamp 			nfound++;
601d93f860cSPoul-Henning Kamp 			if (signum)
602d93f860cSPoul-Henning Kamp 				psignal(p, signum);
603d93f860cSPoul-Henning Kamp 		}
604d93f860cSPoul-Henning Kamp 	else {
605d93f860cSPoul-Henning Kamp 		if (pgid == 0)
606d93f860cSPoul-Henning Kamp 			/*
607d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
608d93f860cSPoul-Henning Kamp 			 */
609d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
610d93f860cSPoul-Henning Kamp 		else {
611d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
612d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
613d93f860cSPoul-Henning Kamp 				return (ESRCH);
614d93f860cSPoul-Henning Kamp 		}
615b75356e1SJeffrey Hsu 		for (p = pgrp->pg_members.lh_first; p != 0;
616b75356e1SJeffrey Hsu 		     p = p->p_pglist.le_next) {
617d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
618d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
619d93f860cSPoul-Henning Kamp 			    !CANSIGNAL(cp, pc, p, signum))
620d93f860cSPoul-Henning Kamp 				continue;
621d93f860cSPoul-Henning Kamp 			nfound++;
622d93f860cSPoul-Henning Kamp 			if (signum)
623d93f860cSPoul-Henning Kamp 				psignal(p, signum);
624d93f860cSPoul-Henning Kamp 		}
625d93f860cSPoul-Henning Kamp 	}
626d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
627d93f860cSPoul-Henning Kamp }
628d93f860cSPoul-Henning Kamp 
629d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
630df8bae1dSRodney W. Grimes struct kill_args {
631df8bae1dSRodney W. Grimes 	int	pid;
632df8bae1dSRodney W. Grimes 	int	signum;
633df8bae1dSRodney W. Grimes };
634d2d3e875SBruce Evans #endif
635df8bae1dSRodney W. Grimes /* ARGSUSED */
63626f9a767SRodney W. Grimes int
637cb226aaaSPoul-Henning Kamp kill(cp, uap)
638df8bae1dSRodney W. Grimes 	register struct proc *cp;
639df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
640df8bae1dSRodney W. Grimes {
641df8bae1dSRodney W. Grimes 	register struct proc *p;
642df8bae1dSRodney W. Grimes 	register struct pcred *pc = cp->p_cred;
643df8bae1dSRodney W. Grimes 
644df8bae1dSRodney W. Grimes 	if ((u_int)uap->signum >= NSIG)
645df8bae1dSRodney W. Grimes 		return (EINVAL);
646df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
647df8bae1dSRodney W. Grimes 		/* kill single process */
648df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
649df8bae1dSRodney W. Grimes 			return (ESRCH);
650df8bae1dSRodney W. Grimes 		if (!CANSIGNAL(cp, pc, p, uap->signum))
651df8bae1dSRodney W. Grimes 			return (EPERM);
652df8bae1dSRodney W. Grimes 		if (uap->signum)
653df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
654df8bae1dSRodney W. Grimes 		return (0);
655df8bae1dSRodney W. Grimes 	}
656df8bae1dSRodney W. Grimes 	switch (uap->pid) {
657df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
658df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
659df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
660df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
661df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
662df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
663df8bae1dSRodney W. Grimes 	}
664df8bae1dSRodney W. Grimes 	/* NOTREACHED */
665df8bae1dSRodney W. Grimes }
666df8bae1dSRodney W. Grimes 
667df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
668d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
669df8bae1dSRodney W. Grimes struct okillpg_args {
670df8bae1dSRodney W. Grimes 	int	pgid;
671df8bae1dSRodney W. Grimes 	int	signum;
672df8bae1dSRodney W. Grimes };
673d2d3e875SBruce Evans #endif
674df8bae1dSRodney W. Grimes /* ARGSUSED */
67526f9a767SRodney W. Grimes int
676cb226aaaSPoul-Henning Kamp okillpg(p, uap)
677df8bae1dSRodney W. Grimes 	struct proc *p;
678df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
679df8bae1dSRodney W. Grimes {
680df8bae1dSRodney W. Grimes 
681df8bae1dSRodney W. Grimes 	if ((u_int)uap->signum >= NSIG)
682df8bae1dSRodney W. Grimes 		return (EINVAL);
683df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
684df8bae1dSRodney W. Grimes }
685df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
686df8bae1dSRodney W. Grimes 
687df8bae1dSRodney W. Grimes /*
688df8bae1dSRodney W. Grimes  * Send a signal to a process group.
689df8bae1dSRodney W. Grimes  */
690df8bae1dSRodney W. Grimes void
691df8bae1dSRodney W. Grimes gsignal(pgid, signum)
692df8bae1dSRodney W. Grimes 	int pgid, signum;
693df8bae1dSRodney W. Grimes {
694df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
697df8bae1dSRodney W. Grimes 		pgsignal(pgrp, signum, 0);
698df8bae1dSRodney W. Grimes }
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes /*
701df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
702df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
703df8bae1dSRodney W. Grimes  */
704df8bae1dSRodney W. Grimes void
705df8bae1dSRodney W. Grimes pgsignal(pgrp, signum, checkctty)
706df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
707df8bae1dSRodney W. Grimes 	int signum, checkctty;
708df8bae1dSRodney W. Grimes {
709df8bae1dSRodney W. Grimes 	register struct proc *p;
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes 	if (pgrp)
712b75356e1SJeffrey Hsu 		for (p = pgrp->pg_members.lh_first; p != 0;
713b75356e1SJeffrey Hsu 		     p = p->p_pglist.le_next)
714df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
715df8bae1dSRodney W. Grimes 				psignal(p, signum);
716df8bae1dSRodney W. Grimes }
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes /*
719df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
720df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
721df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
722df8bae1dSRodney W. Grimes  */
723df8bae1dSRodney W. Grimes void
724df8bae1dSRodney W. Grimes trapsignal(p, signum, code)
725df8bae1dSRodney W. Grimes 	struct proc *p;
726df8bae1dSRodney W. Grimes 	register int signum;
7278674077aSJeffrey Hsu 	u_long code;
728df8bae1dSRodney W. Grimes {
729df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
730df8bae1dSRodney W. Grimes 	int mask;
731df8bae1dSRodney W. Grimes 
732df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
733df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
734df8bae1dSRodney W. Grimes 	    (p->p_sigmask & mask) == 0) {
735df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
736df8bae1dSRodney W. Grimes #ifdef KTRACE
737df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
738df8bae1dSRodney W. Grimes 			ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
739df8bae1dSRodney W. Grimes 				p->p_sigmask, code);
740df8bae1dSRodney W. Grimes #endif
741d66a5066SPeter Wemm 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum,
742d66a5066SPeter Wemm 						p->p_sigmask, code);
743289ccde0SPeter Wemm 		p->p_sigmask |= ps->ps_catchmask[signum] |
744289ccde0SPeter Wemm 				(mask & ~ps->ps_signodefer);
745dedc04feSPeter Wemm 		if ((ps->ps_sigreset & mask) != 0) {
746289ccde0SPeter Wemm 			/*
747289ccde0SPeter Wemm 			 * See setsigvec() for origin of this code.
748289ccde0SPeter Wemm 			 */
749dedc04feSPeter Wemm 			p->p_sigcatch &= ~mask;
750dedc04feSPeter Wemm 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
751dedc04feSPeter Wemm 				p->p_sigignore |= mask;
752dedc04feSPeter Wemm 			ps->ps_sigact[signum] = SIG_DFL;
753dedc04feSPeter Wemm 		}
754df8bae1dSRodney W. Grimes 	} else {
7556626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
7566626c604SJulian Elischer 		p->p_sig = signum;	/* XXX to verify code */
757df8bae1dSRodney W. Grimes 		psignal(p, signum);
758df8bae1dSRodney W. Grimes 	}
759df8bae1dSRodney W. Grimes }
760df8bae1dSRodney W. Grimes 
761df8bae1dSRodney W. Grimes /*
762df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
763df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
764df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
765df8bae1dSRodney W. Grimes  *
766df8bae1dSRodney W. Grimes  * Exceptions:
767df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
768df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
769df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
770df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
771df8bae1dSRodney W. Grimes  *
772df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
773df8bae1dSRodney W. Grimes  */
774df8bae1dSRodney W. Grimes void
775df8bae1dSRodney W. Grimes psignal(p, signum)
776df8bae1dSRodney W. Grimes 	register struct proc *p;
777df8bae1dSRodney W. Grimes 	register int signum;
778df8bae1dSRodney W. Grimes {
779df8bae1dSRodney W. Grimes 	register int s, prop;
780df8bae1dSRodney W. Grimes 	register sig_t action;
781df8bae1dSRodney W. Grimes 	int mask;
782df8bae1dSRodney W. Grimes 
7832a024a2bSSean Eric Fagan 	if ((u_int)signum >= NSIG || signum == 0) {
7842a024a2bSSean Eric Fagan 		printf("psignal: signum %d\n", signum);
785df8bae1dSRodney W. Grimes 		panic("psignal signal number");
7862a024a2bSSean Eric Fagan 	}
787df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
788df8bae1dSRodney W. Grimes 	prop = sigprop[signum];
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes 	/*
7912a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
7922a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
7932a024a2bSSean Eric Fagan 	 * a chance, as well.
794df8bae1dSRodney W. Grimes 	 */
7952a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
796df8bae1dSRodney W. Grimes 		action = SIG_DFL;
797df8bae1dSRodney W. Grimes 	else {
798df8bae1dSRodney W. Grimes 		/*
799df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
800df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
801df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
802df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
803df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
804df8bae1dSRodney W. Grimes 		 */
8056626c604SJulian Elischer 		if ((p->p_sigignore & mask) || (p->p_flag & P_WEXIT))
806df8bae1dSRodney W. Grimes 			return;
807df8bae1dSRodney W. Grimes 		if (p->p_sigmask & mask)
808df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
809df8bae1dSRodney W. Grimes 		else if (p->p_sigcatch & mask)
810df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
811df8bae1dSRodney W. Grimes 		else
812df8bae1dSRodney W. Grimes 			action = SIG_DFL;
813df8bae1dSRodney W. Grimes 	}
814df8bae1dSRodney W. Grimes 
815df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
816df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
817df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
820df8bae1dSRodney W. Grimes 		p->p_siglist &= ~stopsigmask;
821df8bae1dSRodney W. Grimes 
822df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
823df8bae1dSRodney W. Grimes 		/*
824df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
825df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
826df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
827df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
828df8bae1dSRodney W. Grimes 		 */
829df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
830df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
831df8bae1dSRodney W. Grimes 		        return;
832df8bae1dSRodney W. Grimes 		p->p_siglist &= ~contsigmask;
833df8bae1dSRodney W. Grimes 	}
834df8bae1dSRodney W. Grimes 	p->p_siglist |= mask;
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes 	/*
837df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
838df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
839df8bae1dSRodney W. Grimes 	 */
840df8bae1dSRodney W. Grimes 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
841df8bae1dSRodney W. Grimes 		return;
842df8bae1dSRodney W. Grimes 	s = splhigh();
843df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
844df8bae1dSRodney W. Grimes 
845df8bae1dSRodney W. Grimes 	case SSLEEP:
846df8bae1dSRodney W. Grimes 		/*
847df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
848df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
849df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
850df8bae1dSRodney W. Grimes 		 * trap() or syscall().
851df8bae1dSRodney W. Grimes 		 */
852df8bae1dSRodney W. Grimes 		if ((p->p_flag & P_SINTR) == 0)
853df8bae1dSRodney W. Grimes 			goto out;
854df8bae1dSRodney W. Grimes 		/*
855df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
856df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
857df8bae1dSRodney W. Grimes 		 * for the parent.
858df8bae1dSRodney W. Grimes 		 */
859df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
860df8bae1dSRodney W. Grimes 			goto run;
861df8bae1dSRodney W. Grimes 		/*
862df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
863df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
864df8bae1dSRodney W. Grimes 		 * be awakened.
865df8bae1dSRodney W. Grimes 		 */
866df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
867df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
868df8bae1dSRodney W. Grimes 			goto out;
869df8bae1dSRodney W. Grimes 		}
870df8bae1dSRodney W. Grimes 		/*
871df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
872df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
873df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
874df8bae1dSRodney W. Grimes 		 * cause the process to run.
875df8bae1dSRodney W. Grimes 		 */
876df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
877df8bae1dSRodney W. Grimes 			if (action != SIG_DFL)
878df8bae1dSRodney W. Grimes 				goto runfast;
879df8bae1dSRodney W. Grimes 			/*
880df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
881df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
882df8bae1dSRodney W. Grimes 			 */
883df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
884df8bae1dSRodney W. Grimes 				goto out;
885df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
886df8bae1dSRodney W. Grimes 			p->p_xstat = signum;
8876626c604SJulian Elischer 			if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
888df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
889df8bae1dSRodney W. Grimes 			stop(p);
890df8bae1dSRodney W. Grimes 			goto out;
891df8bae1dSRodney W. Grimes 		} else
892df8bae1dSRodney W. Grimes 			goto runfast;
893df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
894df8bae1dSRodney W. Grimes 
895df8bae1dSRodney W. Grimes 	case SSTOP:
896df8bae1dSRodney W. Grimes 		/*
897df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
898df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
899df8bae1dSRodney W. Grimes 		 */
900df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
901df8bae1dSRodney W. Grimes 			goto out;
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes 		/*
904df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
905df8bae1dSRodney W. Grimes 		 */
906df8bae1dSRodney W. Grimes 		if (signum == SIGKILL)
907df8bae1dSRodney W. Grimes 			goto runfast;
908df8bae1dSRodney W. Grimes 
909df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
910df8bae1dSRodney W. Grimes 			/*
911df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
912df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
913df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
914df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
915df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
916df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
917df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
918df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
919df8bae1dSRodney W. Grimes 			 */
920df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
921df8bae1dSRodney W. Grimes 				p->p_siglist &= ~mask;
922df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
923df8bae1dSRodney W. Grimes 				goto runfast;
924df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
925df8bae1dSRodney W. Grimes 				goto run;
926df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
927df8bae1dSRodney W. Grimes 			goto out;
928df8bae1dSRodney W. Grimes 		}
929df8bae1dSRodney W. Grimes 
930df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
931df8bae1dSRodney W. Grimes 			/*
932df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
933df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
934df8bae1dSRodney W. Grimes 			 */
935df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;		/* take it away */
936df8bae1dSRodney W. Grimes 			goto out;
937df8bae1dSRodney W. Grimes 		}
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes 		/*
940df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
941df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
942df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
943df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
944df8bae1dSRodney W. Grimes 		 */
945df8bae1dSRodney W. Grimes 		if (p->p_wchan && p->p_flag & P_SINTR)
946df8bae1dSRodney W. Grimes 			unsleep(p);
947df8bae1dSRodney W. Grimes 		goto out;
948df8bae1dSRodney W. Grimes 
949df8bae1dSRodney W. Grimes 	default:
950df8bae1dSRodney W. Grimes 		/*
951df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
952df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
953df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
954df8bae1dSRodney W. Grimes 		 */
955df8bae1dSRodney W. Grimes 		if (p == curproc)
956df8bae1dSRodney W. Grimes 			signotify(p);
9573163861cSTor Egge #ifdef SMP
9583163861cSTor Egge 		else if (p->p_stat == SRUN)
9593163861cSTor Egge 			forward_signal(p);
9603163861cSTor Egge #endif
961df8bae1dSRodney W. Grimes 		goto out;
962df8bae1dSRodney W. Grimes 	}
963df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
964df8bae1dSRodney W. Grimes 
965df8bae1dSRodney W. Grimes runfast:
966df8bae1dSRodney W. Grimes 	/*
967df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
968df8bae1dSRodney W. Grimes 	 */
969df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
970df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
971df8bae1dSRodney W. Grimes run:
972df8bae1dSRodney W. Grimes 	setrunnable(p);
973df8bae1dSRodney W. Grimes out:
974df8bae1dSRodney W. Grimes 	splx(s);
975df8bae1dSRodney W. Grimes }
976df8bae1dSRodney W. Grimes 
977df8bae1dSRodney W. Grimes /*
978df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
979df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
980df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
981df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
982df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
983df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
984df8bae1dSRodney W. Grimes  * sequence is
985df8bae1dSRodney W. Grimes  *
986df8bae1dSRodney W. Grimes  *	while (signum = CURSIG(curproc))
987df8bae1dSRodney W. Grimes  *		postsig(signum);
988df8bae1dSRodney W. Grimes  */
98926f9a767SRodney W. Grimes int
990df8bae1dSRodney W. Grimes issignal(p)
991df8bae1dSRodney W. Grimes 	register struct proc *p;
992df8bae1dSRodney W. Grimes {
993df8bae1dSRodney W. Grimes 	register int signum, mask, prop;
994df8bae1dSRodney W. Grimes 
995df8bae1dSRodney W. Grimes 	for (;;) {
9962a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
9972a024a2bSSean Eric Fagan 
998df8bae1dSRodney W. Grimes 		mask = p->p_siglist & ~p->p_sigmask;
999df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
1000df8bae1dSRodney W. Grimes 			mask &= ~stopsigmask;
1001df8bae1dSRodney W. Grimes 		if (mask == 0)	 	/* no signal to send */
1002df8bae1dSRodney W. Grimes 			return (0);
1003df8bae1dSRodney W. Grimes 		signum = ffs((long)mask);
1004df8bae1dSRodney W. Grimes 		mask = sigmask(signum);
1005df8bae1dSRodney W. Grimes 		prop = sigprop[signum];
10062a024a2bSSean Eric Fagan 
10072a024a2bSSean Eric Fagan 		STOPEVENT(p, S_SIG, signum);
10082a024a2bSSean Eric Fagan 
1009df8bae1dSRodney W. Grimes 		/*
1010df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1011df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1012df8bae1dSRodney W. Grimes 		 */
10132a024a2bSSean Eric Fagan 		if ((mask & p->p_sigignore) && (traced == 0)) {
1014df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
1015df8bae1dSRodney W. Grimes 			continue;
1016df8bae1dSRodney W. Grimes 		}
1017df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1018df8bae1dSRodney W. Grimes 			/*
1019df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1020df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1021df8bae1dSRodney W. Grimes 			 */
1022df8bae1dSRodney W. Grimes 			p->p_xstat = signum;
1023df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1024df8bae1dSRodney W. Grimes 			do {
1025df8bae1dSRodney W. Grimes 				stop(p);
1026df8bae1dSRodney W. Grimes 				mi_switch();
10272a024a2bSSean Eric Fagan 			} while (!trace_req(p)
10282a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes 			/*
1031df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1032df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1033df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1034df8bae1dSRodney W. Grimes 			 */
1035df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1036df8bae1dSRodney W. Grimes 				continue;
1037df8bae1dSRodney W. Grimes 
1038df8bae1dSRodney W. Grimes 			/*
1039df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1040df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1041df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1042df8bae1dSRodney W. Grimes 			 */
1043df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;	/* clear the old signal */
1044df8bae1dSRodney W. Grimes 			signum = p->p_xstat;
1045df8bae1dSRodney W. Grimes 			if (signum == 0)
1046df8bae1dSRodney W. Grimes 				continue;
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes 			/*
1049df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1050df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1051df8bae1dSRodney W. Grimes 			 */
1052df8bae1dSRodney W. Grimes 			mask = sigmask(signum);
1053df8bae1dSRodney W. Grimes 			p->p_siglist |= mask;
1054df8bae1dSRodney W. Grimes 			if (p->p_sigmask & mask)
1055df8bae1dSRodney W. Grimes 				continue;
1056df8bae1dSRodney W. Grimes 		}
1057df8bae1dSRodney W. Grimes 
1058df8bae1dSRodney W. Grimes 		/*
1059df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1060df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1061df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1062df8bae1dSRodney W. Grimes 		 */
1063a23d65bfSBruce Evans 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[signum]) {
1064df8bae1dSRodney W. Grimes 
10650b53fbe8SBruce Evans 		case (int)SIG_DFL:
1066df8bae1dSRodney W. Grimes 			/*
1067df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1068df8bae1dSRodney W. Grimes 			 */
1069df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1070df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1071df8bae1dSRodney W. Grimes 				/*
1072df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1073df8bae1dSRodney W. Grimes 				 * in init? XXX
1074df8bae1dSRodney W. Grimes 				 */
1075d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
1076d93f860cSPoul-Henning Kamp 					(u_long)p->p_pid, signum);
1077df8bae1dSRodney W. Grimes #endif
1078df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1079df8bae1dSRodney W. Grimes 			}
1080df8bae1dSRodney W. Grimes 			/*
1081df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1082df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1083df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1084df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1085df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1086df8bae1dSRodney W. Grimes 			 */
1087df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1088df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1089df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1090df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1091df8bae1dSRodney W. Grimes 					break;	/* == ignore */
1092df8bae1dSRodney W. Grimes 				p->p_xstat = signum;
1093df8bae1dSRodney W. Grimes 				stop(p);
10946626c604SJulian Elischer 				if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
1095df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1096df8bae1dSRodney W. Grimes 				mi_switch();
1097df8bae1dSRodney W. Grimes 				break;
1098df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1099df8bae1dSRodney W. Grimes 				/*
1100df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1101df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1102df8bae1dSRodney W. Grimes 				 */
1103df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1104df8bae1dSRodney W. Grimes 			} else
1105df8bae1dSRodney W. Grimes 				return (signum);
1106df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1107df8bae1dSRodney W. Grimes 
11080b53fbe8SBruce Evans 		case (int)SIG_IGN:
1109df8bae1dSRodney W. Grimes 			/*
1110df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1111df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1112df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1113df8bae1dSRodney W. Grimes 			 */
1114df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1115df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1116df8bae1dSRodney W. Grimes 				printf("issignal\n");
1117df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1118df8bae1dSRodney W. Grimes 
1119df8bae1dSRodney W. Grimes 		default:
1120df8bae1dSRodney W. Grimes 			/*
1121df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1122df8bae1dSRodney W. Grimes 			 * postsig() process it.
1123df8bae1dSRodney W. Grimes 			 */
1124df8bae1dSRodney W. Grimes 			return (signum);
1125df8bae1dSRodney W. Grimes 		}
1126df8bae1dSRodney W. Grimes 		p->p_siglist &= ~mask;		/* take the signal! */
1127df8bae1dSRodney W. Grimes 	}
1128df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1129df8bae1dSRodney W. Grimes }
1130df8bae1dSRodney W. Grimes 
1131df8bae1dSRodney W. Grimes /*
1132df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1133df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
1134df8bae1dSRodney W. Grimes  * on the run queue.
1135df8bae1dSRodney W. Grimes  */
113626f9a767SRodney W. Grimes void
1137df8bae1dSRodney W. Grimes stop(p)
1138df8bae1dSRodney W. Grimes 	register struct proc *p;
1139df8bae1dSRodney W. Grimes {
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1142df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1143df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1144df8bae1dSRodney W. Grimes }
1145df8bae1dSRodney W. Grimes 
1146df8bae1dSRodney W. Grimes /*
1147df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1148df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1149df8bae1dSRodney W. Grimes  */
1150df8bae1dSRodney W. Grimes void
1151df8bae1dSRodney W. Grimes postsig(signum)
1152df8bae1dSRodney W. Grimes 	register int signum;
1153df8bae1dSRodney W. Grimes {
1154df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
1155df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1156df8bae1dSRodney W. Grimes 	register sig_t action;
1157df8bae1dSRodney W. Grimes 	int code, mask, returnmask;
1158df8bae1dSRodney W. Grimes 
11595526d2d9SEivind Eklund 	KASSERT(signum != 0, ("postsig"));
11605526d2d9SEivind Eklund 
1161df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
1162df8bae1dSRodney W. Grimes 	p->p_siglist &= ~mask;
1163df8bae1dSRodney W. Grimes 	action = ps->ps_sigact[signum];
1164df8bae1dSRodney W. Grimes #ifdef KTRACE
1165df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1166df8bae1dSRodney W. Grimes 		ktrpsig(p->p_tracep,
11676626c604SJulian Elischer 		    signum, action, p->p_oldsigmask ?
11686626c604SJulian Elischer 		    p->p_oldsigmask : p->p_sigmask, 0);
1169df8bae1dSRodney W. Grimes #endif
11702a024a2bSSean Eric Fagan 	STOPEVENT(p, S_SIG, signum);
11712a024a2bSSean Eric Fagan 
1172df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1173df8bae1dSRodney W. Grimes 		/*
1174df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1175df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1176df8bae1dSRodney W. Grimes 		 */
1177df8bae1dSRodney W. Grimes 		sigexit(p, signum);
1178df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1179df8bae1dSRodney W. Grimes 	} else {
1180df8bae1dSRodney W. Grimes 		/*
1181df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1182df8bae1dSRodney W. Grimes 		 */
11835526d2d9SEivind Eklund 		KASSERT(action != SIG_IGN && (p->p_sigmask & mask) == 0,
11845526d2d9SEivind Eklund 		    ("postsig action"));
1185df8bae1dSRodney W. Grimes 		/*
1186df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1187df8bae1dSRodney W. Grimes 		 * occurences of this signal.
1188df8bae1dSRodney W. Grimes 		 *
1189df8bae1dSRodney W. Grimes 		 * Special case: user has done a sigpause.  Here the
1190df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1191df8bae1dSRodney W. Grimes 		 * mask from before the sigpause is what we want
1192df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1193df8bae1dSRodney W. Grimes 		 */
1194df8bae1dSRodney W. Grimes 		(void) splhigh();
11956626c604SJulian Elischer 		if (p->p_oldsigmask) {
11966626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
11976626c604SJulian Elischer 			p->p_oldsigmask = 0;
1198df8bae1dSRodney W. Grimes 		} else
1199df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
1200289ccde0SPeter Wemm 		p->p_sigmask |= ps->ps_catchmask[signum] |
1201289ccde0SPeter Wemm 				(mask & ~ps->ps_signodefer);
1202dedc04feSPeter Wemm 		if ((ps->ps_sigreset & mask) != 0) {
1203289ccde0SPeter Wemm 			/*
1204289ccde0SPeter Wemm 			 * See setsigvec() for origin of this code.
1205289ccde0SPeter Wemm 			 */
1206dedc04feSPeter Wemm 			p->p_sigcatch &= ~mask;
1207dedc04feSPeter Wemm 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1208dedc04feSPeter Wemm 				p->p_sigignore |= mask;
1209dedc04feSPeter Wemm 			ps->ps_sigact[signum] = SIG_DFL;
1210dedc04feSPeter Wemm 		}
1211df8bae1dSRodney W. Grimes 		(void) spl0();
1212df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
12136626c604SJulian Elischer 		if (p->p_sig != signum) {
1214df8bae1dSRodney W. Grimes 			code = 0;
1215df8bae1dSRodney W. Grimes 		} else {
12166626c604SJulian Elischer 			code = p->p_code;
12176626c604SJulian Elischer 			p->p_code = 0;
12186626c604SJulian Elischer 			p->p_sig = 0;
1219df8bae1dSRodney W. Grimes 		}
1220d66a5066SPeter Wemm 		(*p->p_sysent->sv_sendsig)(action, signum, returnmask, code);
1221df8bae1dSRodney W. Grimes 	}
1222df8bae1dSRodney W. Grimes }
1223df8bae1dSRodney W. Grimes 
1224df8bae1dSRodney W. Grimes /*
1225df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1226df8bae1dSRodney W. Grimes  */
122726f9a767SRodney W. Grimes void
1228df8bae1dSRodney W. Grimes killproc(p, why)
1229df8bae1dSRodney W. Grimes 	struct proc *p;
1230df8bae1dSRodney W. Grimes 	char *why;
1231df8bae1dSRodney W. Grimes {
1232729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1233729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1234df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1235df8bae1dSRodney W. Grimes }
1236df8bae1dSRodney W. Grimes 
1237df8bae1dSRodney W. Grimes /*
1238df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1239df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1240df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1241df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1242df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1243df8bae1dSRodney W. Grimes  * does not return.
1244df8bae1dSRodney W. Grimes  */
124526f9a767SRodney W. Grimes void
1246df8bae1dSRodney W. Grimes sigexit(p, signum)
1247df8bae1dSRodney W. Grimes 	register struct proc *p;
1248df8bae1dSRodney W. Grimes 	int signum;
1249df8bae1dSRodney W. Grimes {
1250df8bae1dSRodney W. Grimes 
1251df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
1252df8bae1dSRodney W. Grimes 	if (sigprop[signum] & SA_CORE) {
12536626c604SJulian Elischer 		p->p_sig = signum;
1254c364e17eSAndrey A. Chernov 		/*
1255c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1256c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1257c364e17eSAndrey A. Chernov 		 * these messages.)
1258c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1259c364e17eSAndrey A. Chernov 		 */
1260fca666a1SJulian Elischer 		if (coredump(p) == 0)
1261df8bae1dSRodney W. Grimes 			signum |= WCOREFLAG;
126257308494SJoerg Wunsch 		if (kern_logsigexit)
126357308494SJoerg Wunsch 			log(LOG_INFO,
126457308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
12653d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
12663d1b21c6SAndrey A. Chernov 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
12673d1b21c6SAndrey A. Chernov 			    signum &~ WCOREFLAG,
12683d1b21c6SAndrey A. Chernov 			    signum & WCOREFLAG ? " (core dumped)" : "");
1269df8bae1dSRodney W. Grimes 	}
1270df8bae1dSRodney W. Grimes 	exit1(p, W_EXITCODE(0, signum));
1271df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1272df8bae1dSRodney W. Grimes }
1273df8bae1dSRodney W. Grimes 
1274c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1275c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1276c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1277c5edb423SSean Eric Fagan 
1278c5edb423SSean Eric Fagan /*
1279c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1280c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1281c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1282c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1283c5edb423SSean Eric Fagan  *	%P	process id (pid)
1284c5edb423SSean Eric Fagan  *	%U	user id (uid)
1285c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1286c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1287c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1288c5edb423SSean Eric Fagan  */
1289c5edb423SSean Eric Fagan 
1290fca666a1SJulian Elischer static char *
1291c5edb423SSean Eric Fagan expand_name(name, uid, pid)
129287f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1293c5edb423SSean Eric Fagan 	char *temp;
1294c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1295c5edb423SSean Eric Fagan 	int i, n;
1296c5edb423SSean Eric Fagan 	char *format = corefilename;
1297ce38ca0fSAlfred Perlstein 	size_t namelen;
1298c5edb423SSean Eric Fagan 
1299ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
13000bfe2990SEivind Eklund 	if (temp == NULL)
13010bfe2990SEivind Eklund 		return NULL;
1302ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1303ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1304c5edb423SSean Eric Fagan 		int l;
1305c5edb423SSean Eric Fagan 		switch (format[i]) {
1306c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1307c5edb423SSean Eric Fagan 			i++;
1308c5edb423SSean Eric Fagan 			switch (format[i]) {
1309c5edb423SSean Eric Fagan 			case '%':
1310c5edb423SSean Eric Fagan 				temp[n++] = '%';
1311c5edb423SSean Eric Fagan 				break;
1312c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1313ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
131487f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1315c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1316c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1317c5edb423SSean Eric Fagan 					return NULL;
1318c5edb423SSean Eric Fagan 				}
1319ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1320ce38ca0fSAlfred Perlstein 				n += namelen;
1321c5edb423SSean Eric Fagan 				break;
1322c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1323ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1324c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
132587f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1326c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1327c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1328c5edb423SSean Eric Fagan 					return NULL;
1329c5edb423SSean Eric Fagan 				}
1330c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1331c5edb423SSean Eric Fagan 				n += l;
1332c5edb423SSean Eric Fagan 				break;
1333c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1334ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1335c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
133687f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1337c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1338c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1339c5edb423SSean Eric Fagan 					return NULL;
1340c5edb423SSean Eric Fagan 				}
1341c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1342c5edb423SSean Eric Fagan 				n += l;
1343c5edb423SSean Eric Fagan 				break;
1344c5edb423SSean Eric Fagan 			default:
1345c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1346c5edb423SSean Eric Fagan 			}
1347c5edb423SSean Eric Fagan 			break;
1348c5edb423SSean Eric Fagan 		default:
1349c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1350c5edb423SSean Eric Fagan 		}
1351c5edb423SSean Eric Fagan 	}
1352ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1353c5edb423SSean Eric Fagan 	return temp;
1354c5edb423SSean Eric Fagan }
1355c5edb423SSean Eric Fagan 
1356df8bae1dSRodney W. Grimes /*
1357fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1358fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1359fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1360fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1361fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1362fca666a1SJulian Elischer  */
1363fca666a1SJulian Elischer 
1364fca666a1SJulian Elischer static int
1365fca666a1SJulian Elischer coredump(p)
1366fca666a1SJulian Elischer 	register struct proc *p;
1367fca666a1SJulian Elischer {
1368fca666a1SJulian Elischer 	register struct vnode *vp;
1369fca666a1SJulian Elischer 	register struct ucred *cred = p->p_cred->pc_ucred;
1370fca666a1SJulian Elischer 	struct nameidata nd;
1371fca666a1SJulian Elischer 	struct vattr vattr;
1372fca666a1SJulian Elischer 	int error, error1;
1373fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1374fca666a1SJulian Elischer 	off_t limit;
1375fca666a1SJulian Elischer 
1376fca666a1SJulian Elischer 	STOPEVENT(p, S_CORE, 0);
1377fca666a1SJulian Elischer 
1378fca666a1SJulian Elischer 	if ((sugid_coredump == 0) && p->p_flag & P_SUGID)
1379fca666a1SJulian Elischer 		return (EFAULT);
1380fca666a1SJulian Elischer 
1381fca666a1SJulian Elischer 	/*
1382fca666a1SJulian Elischer 	 * Note that this layout means that limit checking is done
1383fca666a1SJulian Elischer 	 * AFTER the corefile name is created.  This could happen
1384fca666a1SJulian Elischer 	 * other ways as well, so I'm not too worried about it, but
1385fca666a1SJulian Elischer 	 * it is potentially confusing.
1386fca666a1SJulian Elischer 	 */
1387fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1388fca666a1SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1389fca666a1SJulian Elischer 	error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1390fca666a1SJulian Elischer 	free(name, M_TEMP);
1391fca666a1SJulian Elischer 	if (error)
1392fca666a1SJulian Elischer 		return (error);
1393fca666a1SJulian Elischer 	vp = nd.ni_vp;
1394fca666a1SJulian Elischer 
1395fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
1396fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
1397fca666a1SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1398fca666a1SJulian Elischer 		error = EFAULT;
1399fca666a1SJulian Elischer 		goto out;
1400fca666a1SJulian Elischer 	}
1401fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
1402fca666a1SJulian Elischer 	vattr.va_size = 0;
1403fca666a1SJulian Elischer 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1404fca666a1SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, p);
1405fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
1406fca666a1SJulian Elischer 
1407fca666a1SJulian Elischer 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1408fca666a1SJulian Elischer 
1409fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
1410fca666a1SJulian Elischer 	  p->p_sysent->sv_coredump(p, vp, limit) :
1411fca666a1SJulian Elischer 	  ENOSYS;
1412fca666a1SJulian Elischer 
1413fca666a1SJulian Elischer out:
1414fca666a1SJulian Elischer 	VOP_UNLOCK(vp, 0, p);
1415fca666a1SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, p);
1416fca666a1SJulian Elischer 	if (error == 0)
1417fca666a1SJulian Elischer 		error = error1;
1418fca666a1SJulian Elischer 	return (error);
1419fca666a1SJulian Elischer }
1420fca666a1SJulian Elischer 
1421fca666a1SJulian Elischer /*
1422df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1423df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1424df8bae1dSRodney W. Grimes  */
1425d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1426df8bae1dSRodney W. Grimes struct nosys_args {
1427df8bae1dSRodney W. Grimes 	int	dummy;
1428df8bae1dSRodney W. Grimes };
1429d2d3e875SBruce Evans #endif
1430df8bae1dSRodney W. Grimes /* ARGSUSED */
143126f9a767SRodney W. Grimes int
1432cb226aaaSPoul-Henning Kamp nosys(p, args)
1433df8bae1dSRodney W. Grimes 	struct proc *p;
1434df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1435df8bae1dSRodney W. Grimes {
1436df8bae1dSRodney W. Grimes 
1437df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1438df8bae1dSRodney W. Grimes 	return (EINVAL);
1439df8bae1dSRodney W. Grimes }
1440831d27a9SDon Lewis 
1441831d27a9SDon Lewis /*
1442831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1443831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1444831d27a9SDon Lewis  */
1445831d27a9SDon Lewis void
1446831d27a9SDon Lewis pgsigio(sigio, signum, checkctty)
1447831d27a9SDon Lewis 	struct sigio *sigio;
1448831d27a9SDon Lewis 	int signum, checkctty;
1449831d27a9SDon Lewis {
1450831d27a9SDon Lewis 	if (sigio == NULL)
1451831d27a9SDon Lewis 		return;
1452831d27a9SDon Lewis 
1453831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1454831d27a9SDon Lewis 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1455831d27a9SDon Lewis 		             sigio->sio_proc))
1456831d27a9SDon Lewis 			psignal(sigio->sio_proc, signum);
1457831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1458831d27a9SDon Lewis 		struct proc *p;
1459831d27a9SDon Lewis 
1460831d27a9SDon Lewis 		for (p = sigio->sio_pgrp->pg_members.lh_first; p != NULL;
1461831d27a9SDon Lewis 		     p = p->p_pglist.le_next)
1462831d27a9SDon Lewis 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1463831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1464831d27a9SDon Lewis 				psignal(p, signum);
1465831d27a9SDon Lewis 	}
1466831d27a9SDon Lewis }
1467