xref: /freebsd/sys/kern/kern_sig.c (revision 3163861c7bb17056051e94ed6a8e34108a4d7ce0)
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
393163861cSTor Egge  * $Id: kern_sig.c,v 1.39 1998/02/06 12:13:24 eivind 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>
47d2d3e875SBruce Evans #include <sys/sysproto.h>
48df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
50df8bae1dSRodney W. Grimes #include <sys/namei.h>
51df8bae1dSRodney W. Grimes #include <sys/vnode.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
532a024a2bSSean Eric Fagan #include <sys/pioctl.h>
54df8bae1dSRodney W. Grimes #include <sys/systm.h>
55df8bae1dSRodney W. Grimes #include <sys/acct.h>
563ac4d1efSBruce Evans #include <sys/fcntl.h>
57df8bae1dSRodney W. Grimes #include <sys/wait.h>
58df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
59df8bae1dSRodney W. Grimes #include <sys/syslog.h>
60df8bae1dSRodney W. Grimes #include <sys/stat.h>
61d66a5066SPeter Wemm #include <sys/sysent.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <machine/cpu.h>
643163861cSTor Egge #ifdef SMP
653163861cSTor Egge #include <machine/smp.h>
663163861cSTor Egge #endif
67df8bae1dSRodney W. Grimes 
688a2d9f50SBruce Evans /* All these for coredump() only. */
69df8bae1dSRodney W. Grimes #include <vm/vm.h>
70efeaf95aSDavid Greenman #include <vm/vm_param.h>
71996c772fSJohn Dyson #include <sys/lock.h>
72efeaf95aSDavid Greenman #include <vm/pmap.h>
73efeaf95aSDavid Greenman #include <vm/vm_map.h>
748a2d9f50SBruce Evans #include <sys/user.h>
75df8bae1dSRodney W. Grimes 
7687b6de2bSPoul-Henning Kamp static int coredump     __P((struct proc *p));
7787b6de2bSPoul-Henning Kamp static int killpg1	__P((struct proc *cp, int signum, int pgid, int all));
788a2d9f50SBruce Evans static void setsigvec	__P((struct proc *p, int signum, struct sigaction *sa));
7987b6de2bSPoul-Henning Kamp static void stop	__P((struct proc *));
8026f9a767SRodney W. Grimes 
81df8bae1dSRodney W. Grimes /*
82df8bae1dSRodney W. Grimes  * Can process p, with pcred pc, send the signal signum to process q?
83df8bae1dSRodney W. Grimes  */
84df8bae1dSRodney W. Grimes #define CANSIGNAL(p, pc, q, signum) \
85df8bae1dSRodney W. Grimes 	((pc)->pc_ucred->cr_uid == 0 || \
86df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
87df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
88df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
89df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
90df8bae1dSRodney W. Grimes 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
91df8bae1dSRodney W. Grimes 
92d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
93df8bae1dSRodney W. Grimes struct sigaction_args {
94df8bae1dSRodney W. Grimes 	int	signum;
95df8bae1dSRodney W. Grimes 	struct	sigaction *nsa;
96df8bae1dSRodney W. Grimes 	struct	sigaction *osa;
97df8bae1dSRodney W. Grimes };
98d2d3e875SBruce Evans #endif
99df8bae1dSRodney W. Grimes /* ARGSUSED */
10026f9a767SRodney W. Grimes int
101cb226aaaSPoul-Henning Kamp sigaction(p, uap)
102df8bae1dSRodney W. Grimes 	struct proc *p;
103df8bae1dSRodney W. Grimes 	register struct sigaction_args *uap;
104df8bae1dSRodney W. Grimes {
105df8bae1dSRodney W. Grimes 	struct sigaction vec;
106df8bae1dSRodney W. Grimes 	register struct sigaction *sa;
107df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
108df8bae1dSRodney W. Grimes 	register int signum;
109df8bae1dSRodney W. Grimes 	int bit, error;
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes 	signum = uap->signum;
112e6eeb36dSBruce Evans 	if (signum <= 0 || signum >= NSIG)
113df8bae1dSRodney W. Grimes 		return (EINVAL);
114df8bae1dSRodney W. Grimes 	sa = &vec;
115df8bae1dSRodney W. Grimes 	if (uap->osa) {
116df8bae1dSRodney W. Grimes 		sa->sa_handler = ps->ps_sigact[signum];
117df8bae1dSRodney W. Grimes 		sa->sa_mask = ps->ps_catchmask[signum];
118df8bae1dSRodney W. Grimes 		bit = sigmask(signum);
119df8bae1dSRodney W. Grimes 		sa->sa_flags = 0;
120df8bae1dSRodney W. Grimes 		if ((ps->ps_sigonstack & bit) != 0)
121df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_ONSTACK;
122df8bae1dSRodney W. Grimes 		if ((ps->ps_sigintr & bit) == 0)
123df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_RESTART;
124dedc04feSPeter Wemm 		if ((ps->ps_sigreset & bit) != 0)
125dedc04feSPeter Wemm 			sa->sa_flags |= SA_RESETHAND;
126289ccde0SPeter Wemm 		if ((ps->ps_signodefer & bit) != 0)
127dedc04feSPeter Wemm 			sa->sa_flags |= SA_NODEFER;
128289ccde0SPeter Wemm 		if (signum == SIGCHLD && p->p_flag & P_NOCLDSTOP)
129289ccde0SPeter Wemm 			sa->sa_flags |= SA_NOCLDSTOP;
130245f17d4SJoerg Wunsch 		if (signum == SIGCHLD && p->p_flag & P_NOCLDWAIT)
131245f17d4SJoerg Wunsch 			sa->sa_flags |= SA_NOCLDWAIT;
132bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa,
133bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
134df8bae1dSRodney W. Grimes 			return (error);
135df8bae1dSRodney W. Grimes 	}
136df8bae1dSRodney W. Grimes 	if (uap->nsa) {
137bb56ec4aSPoul-Henning Kamp 		if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
138bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
139df8bae1dSRodney W. Grimes 			return (error);
140e6eeb36dSBruce Evans 		if ((signum == SIGKILL || signum == SIGSTOP) &&
141e6eeb36dSBruce Evans 		    sa->sa_handler != SIG_DFL)
142e6eeb36dSBruce Evans 			return (EINVAL);
143df8bae1dSRodney W. Grimes 		setsigvec(p, signum, sa);
144df8bae1dSRodney W. Grimes 	}
145df8bae1dSRodney W. Grimes 	return (0);
146df8bae1dSRodney W. Grimes }
147df8bae1dSRodney W. Grimes 
1488a2d9f50SBruce Evans static void
149df8bae1dSRodney W. Grimes setsigvec(p, signum, sa)
150df8bae1dSRodney W. Grimes 	register struct proc *p;
151df8bae1dSRodney W. Grimes 	int signum;
152df8bae1dSRodney W. Grimes 	register struct sigaction *sa;
153df8bae1dSRodney W. Grimes {
154df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
155df8bae1dSRodney W. Grimes 	register int bit;
156df8bae1dSRodney W. Grimes 
157df8bae1dSRodney W. Grimes 	bit = sigmask(signum);
158df8bae1dSRodney W. Grimes 	/*
159df8bae1dSRodney W. Grimes 	 * Change setting atomically.
160df8bae1dSRodney W. Grimes 	 */
161df8bae1dSRodney W. Grimes 	(void) splhigh();
162df8bae1dSRodney W. Grimes 	ps->ps_sigact[signum] = sa->sa_handler;
163df8bae1dSRodney W. Grimes 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
164df8bae1dSRodney W. Grimes 	if ((sa->sa_flags & SA_RESTART) == 0)
165df8bae1dSRodney W. Grimes 		ps->ps_sigintr |= bit;
166df8bae1dSRodney W. Grimes 	else
167df8bae1dSRodney W. Grimes 		ps->ps_sigintr &= ~bit;
168df8bae1dSRodney W. Grimes 	if (sa->sa_flags & SA_ONSTACK)
169df8bae1dSRodney W. Grimes 		ps->ps_sigonstack |= bit;
170df8bae1dSRodney W. Grimes 	else
171df8bae1dSRodney W. Grimes 		ps->ps_sigonstack &= ~bit;
172dedc04feSPeter Wemm 	if (sa->sa_flags & SA_RESETHAND)
173dedc04feSPeter Wemm 		ps->ps_sigreset |= bit;
1741e41c1b5SSteven Wallace 	else
175dedc04feSPeter Wemm 		ps->ps_sigreset &= ~bit;
176289ccde0SPeter Wemm 	if (sa->sa_flags & SA_NODEFER)
177289ccde0SPeter Wemm 		ps->ps_signodefer |= bit;
178289ccde0SPeter Wemm 	else
179289ccde0SPeter Wemm 		ps->ps_signodefer &= ~bit;
180df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
181df8bae1dSRodney W. Grimes 	if (sa->sa_flags & SA_USERTRAMP)
182df8bae1dSRodney W. Grimes 		ps->ps_usertramp |= bit;
183df8bae1dSRodney W. Grimes 	else
184df8bae1dSRodney W. Grimes 		ps->ps_usertramp &= ~bit;
185df8bae1dSRodney W. Grimes #endif
186df8bae1dSRodney W. Grimes 	if (signum == SIGCHLD) {
187df8bae1dSRodney W. Grimes 		if (sa->sa_flags & SA_NOCLDSTOP)
188df8bae1dSRodney W. Grimes 			p->p_flag |= P_NOCLDSTOP;
189df8bae1dSRodney W. Grimes 		else
190df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_NOCLDSTOP;
191245f17d4SJoerg Wunsch 		if (sa->sa_flags & SA_NOCLDWAIT) {
192245f17d4SJoerg Wunsch 			/*
193245f17d4SJoerg Wunsch 			 * Paranoia: since SA_NOCLDWAIT is implemented by
194245f17d4SJoerg Wunsch 			 * reparenting the dying child to PID 1 (and
195245f17d4SJoerg Wunsch 			 * trust it to reap the zombie), PID 1 itself is
196245f17d4SJoerg Wunsch 			 * forbidden to set SA_NOCLDWAIT.
197245f17d4SJoerg Wunsch 			 */
198245f17d4SJoerg Wunsch 			if (p->p_pid == 1)
199245f17d4SJoerg Wunsch 				p->p_flag &= ~P_NOCLDWAIT;
200245f17d4SJoerg Wunsch 			else
201245f17d4SJoerg Wunsch 				p->p_flag |= P_NOCLDWAIT;
202245f17d4SJoerg Wunsch 		} else
203245f17d4SJoerg Wunsch 			p->p_flag &= ~P_NOCLDWAIT;
204df8bae1dSRodney W. Grimes 	}
205df8bae1dSRodney W. Grimes 	/*
206df8bae1dSRodney W. Grimes 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
207df8bae1dSRodney W. Grimes 	 * and for signals set to SIG_DFL where the default is to ignore.
208df8bae1dSRodney W. Grimes 	 * However, don't put SIGCONT in p_sigignore,
209df8bae1dSRodney W. Grimes 	 * as we have to restart the process.
210df8bae1dSRodney W. Grimes 	 */
211df8bae1dSRodney W. Grimes 	if (sa->sa_handler == SIG_IGN ||
212df8bae1dSRodney W. Grimes 	    (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
213df8bae1dSRodney W. Grimes 		p->p_siglist &= ~bit;		/* never to be seen again */
214df8bae1dSRodney W. Grimes 		if (signum != SIGCONT)
215df8bae1dSRodney W. Grimes 			p->p_sigignore |= bit;	/* easier in psignal */
216df8bae1dSRodney W. Grimes 		p->p_sigcatch &= ~bit;
217df8bae1dSRodney W. Grimes 	} else {
218df8bae1dSRodney W. Grimes 		p->p_sigignore &= ~bit;
219df8bae1dSRodney W. Grimes 		if (sa->sa_handler == SIG_DFL)
220df8bae1dSRodney W. Grimes 			p->p_sigcatch &= ~bit;
221df8bae1dSRodney W. Grimes 		else
222df8bae1dSRodney W. Grimes 			p->p_sigcatch |= bit;
223df8bae1dSRodney W. Grimes 	}
224df8bae1dSRodney W. Grimes 	(void) spl0();
225df8bae1dSRodney W. Grimes }
226df8bae1dSRodney W. Grimes 
227df8bae1dSRodney W. Grimes /*
228df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
229df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
230df8bae1dSRodney W. Grimes  */
231df8bae1dSRodney W. Grimes void
232df8bae1dSRodney W. Grimes siginit(p)
233df8bae1dSRodney W. Grimes 	struct proc *p;
234df8bae1dSRodney W. Grimes {
235df8bae1dSRodney W. Grimes 	register int i;
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes 	for (i = 0; i < NSIG; i++)
238df8bae1dSRodney W. Grimes 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
239df8bae1dSRodney W. Grimes 			p->p_sigignore |= sigmask(i);
240df8bae1dSRodney W. Grimes }
241df8bae1dSRodney W. Grimes 
242df8bae1dSRodney W. Grimes /*
243df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
244df8bae1dSRodney W. Grimes  */
245df8bae1dSRodney W. Grimes void
246df8bae1dSRodney W. Grimes execsigs(p)
247df8bae1dSRodney W. Grimes 	register struct proc *p;
248df8bae1dSRodney W. Grimes {
249df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
250df8bae1dSRodney W. Grimes 	register int nc, mask;
251df8bae1dSRodney W. Grimes 
252df8bae1dSRodney W. Grimes 	/*
253df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
254df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
255df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
256df8bae1dSRodney W. Grimes 	 */
257df8bae1dSRodney W. Grimes 	while (p->p_sigcatch) {
258df8bae1dSRodney W. Grimes 		nc = ffs((long)p->p_sigcatch);
259df8bae1dSRodney W. Grimes 		mask = sigmask(nc);
260df8bae1dSRodney W. Grimes 		p->p_sigcatch &= ~mask;
261df8bae1dSRodney W. Grimes 		if (sigprop[nc] & SA_IGNORE) {
262df8bae1dSRodney W. Grimes 			if (nc != SIGCONT)
263df8bae1dSRodney W. Grimes 				p->p_sigignore |= mask;
264df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
265df8bae1dSRodney W. Grimes 		}
266df8bae1dSRodney W. Grimes 		ps->ps_sigact[nc] = SIG_DFL;
267df8bae1dSRodney W. Grimes 	}
268df8bae1dSRodney W. Grimes 	/*
269df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
270df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
271df8bae1dSRodney W. Grimes 	 */
272d66a5066SPeter Wemm 	ps->ps_sigstk.ss_flags = SS_DISABLE;
273df8bae1dSRodney W. Grimes 	ps->ps_sigstk.ss_size = 0;
274aa98692fSAndreas Schulz 	ps->ps_sigstk.ss_sp = 0;
275df8bae1dSRodney W. Grimes 	ps->ps_flags = 0;
276df8bae1dSRodney W. Grimes }
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes /*
279df8bae1dSRodney W. Grimes  * Manipulate signal mask.
280df8bae1dSRodney W. Grimes  * Note that we receive new mask, not pointer,
281df8bae1dSRodney W. Grimes  * and return old mask as return value;
282df8bae1dSRodney W. Grimes  * the library stub does the rest.
283df8bae1dSRodney W. Grimes  */
284d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
285df8bae1dSRodney W. Grimes struct sigprocmask_args {
286df8bae1dSRodney W. Grimes 	int	how;
287df8bae1dSRodney W. Grimes 	sigset_t mask;
288df8bae1dSRodney W. Grimes };
289d2d3e875SBruce Evans #endif
29026f9a767SRodney W. Grimes int
291cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
292df8bae1dSRodney W. Grimes 	register struct proc *p;
293df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
294df8bae1dSRodney W. Grimes {
295df8bae1dSRodney W. Grimes 	int error = 0;
296df8bae1dSRodney W. Grimes 
297cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
298df8bae1dSRodney W. Grimes 	(void) splhigh();
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 	switch (uap->how) {
301df8bae1dSRodney W. Grimes 	case SIG_BLOCK:
302df8bae1dSRodney W. Grimes 		p->p_sigmask |= uap->mask &~ sigcantmask;
303df8bae1dSRodney W. Grimes 		break;
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 	case SIG_UNBLOCK:
306df8bae1dSRodney W. Grimes 		p->p_sigmask &= ~uap->mask;
307df8bae1dSRodney W. Grimes 		break;
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes 	case SIG_SETMASK:
310df8bae1dSRodney W. Grimes 		p->p_sigmask = uap->mask &~ sigcantmask;
311df8bae1dSRodney W. Grimes 		break;
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes 	default:
314df8bae1dSRodney W. Grimes 		error = EINVAL;
315df8bae1dSRodney W. Grimes 		break;
316df8bae1dSRodney W. Grimes 	}
317df8bae1dSRodney W. Grimes 	(void) spl0();
318df8bae1dSRodney W. Grimes 	return (error);
319df8bae1dSRodney W. Grimes }
320df8bae1dSRodney W. Grimes 
321d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
322df8bae1dSRodney W. Grimes struct sigpending_args {
323df8bae1dSRodney W. Grimes 	int	dummy;
324df8bae1dSRodney W. Grimes };
325d2d3e875SBruce Evans #endif
326df8bae1dSRodney W. Grimes /* ARGSUSED */
32726f9a767SRodney W. Grimes int
328cb226aaaSPoul-Henning Kamp sigpending(p, uap)
329df8bae1dSRodney W. Grimes 	struct proc *p;
330df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
331df8bae1dSRodney W. Grimes {
332df8bae1dSRodney W. Grimes 
333cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_siglist;
334df8bae1dSRodney W. Grimes 	return (0);
335df8bae1dSRodney W. Grimes }
336df8bae1dSRodney W. Grimes 
337df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
338df8bae1dSRodney W. Grimes /*
339df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
340df8bae1dSRodney W. Grimes  */
341d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
342df8bae1dSRodney W. Grimes struct osigvec_args {
343df8bae1dSRodney W. Grimes 	int	signum;
344df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
345df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
346df8bae1dSRodney W. Grimes };
347d2d3e875SBruce Evans #endif
348df8bae1dSRodney W. Grimes /* ARGSUSED */
34926f9a767SRodney W. Grimes int
350cb226aaaSPoul-Henning Kamp osigvec(p, uap)
351df8bae1dSRodney W. Grimes 	struct proc *p;
352df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
353df8bae1dSRodney W. Grimes {
354df8bae1dSRodney W. Grimes 	struct sigvec vec;
355df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
356df8bae1dSRodney W. Grimes 	register struct sigvec *sv;
357df8bae1dSRodney W. Grimes 	register int signum;
358df8bae1dSRodney W. Grimes 	int bit, error;
359df8bae1dSRodney W. Grimes 
360df8bae1dSRodney W. Grimes 	signum = uap->signum;
361e6eeb36dSBruce Evans 	if (signum <= 0 || signum >= NSIG)
362df8bae1dSRodney W. Grimes 		return (EINVAL);
363df8bae1dSRodney W. Grimes 	sv = &vec;
364df8bae1dSRodney W. Grimes 	if (uap->osv) {
365df8bae1dSRodney W. Grimes 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
366df8bae1dSRodney W. Grimes 		sv->sv_mask = ps->ps_catchmask[signum];
367df8bae1dSRodney W. Grimes 		bit = sigmask(signum);
368df8bae1dSRodney W. Grimes 		sv->sv_flags = 0;
369df8bae1dSRodney W. Grimes 		if ((ps->ps_sigonstack & bit) != 0)
370df8bae1dSRodney W. Grimes 			sv->sv_flags |= SV_ONSTACK;
371df8bae1dSRodney W. Grimes 		if ((ps->ps_sigintr & bit) != 0)
372df8bae1dSRodney W. Grimes 			sv->sv_flags |= SV_INTERRUPT;
373289ccde0SPeter Wemm 		if ((ps->ps_sigreset & bit) != 0)
374289ccde0SPeter Wemm 			sv->sv_flags |= SV_RESETHAND;
375289ccde0SPeter Wemm 		if ((ps->ps_signodefer & bit) != 0)
376289ccde0SPeter Wemm 			sv->sv_flags |= SV_NODEFER;
377df8bae1dSRodney W. Grimes #ifndef COMPAT_SUNOS
378289ccde0SPeter Wemm 		if (signum == SIGCHLD && p->p_flag & P_NOCLDSTOP)
379289ccde0SPeter Wemm 			sv->sv_flags |= SV_NOCLDSTOP;
380df8bae1dSRodney W. Grimes #endif
381bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv,
382bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
383df8bae1dSRodney W. Grimes 			return (error);
384df8bae1dSRodney W. Grimes 	}
385df8bae1dSRodney W. Grimes 	if (uap->nsv) {
386bb56ec4aSPoul-Henning Kamp 		if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
387bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
388df8bae1dSRodney W. Grimes 			return (error);
389e6eeb36dSBruce Evans 		if ((signum == SIGKILL || signum == SIGSTOP) &&
390e6eeb36dSBruce Evans 		    sv->sv_handler != SIG_DFL)
391e6eeb36dSBruce Evans 			return (EINVAL);
392df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
393df8bae1dSRodney W. Grimes 		sv->sv_flags |= SA_USERTRAMP;
394df8bae1dSRodney W. Grimes #endif
395df8bae1dSRodney W. Grimes 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
396df8bae1dSRodney W. Grimes 		setsigvec(p, signum, (struct sigaction *)sv);
397df8bae1dSRodney W. Grimes 	}
398df8bae1dSRodney W. Grimes 	return (0);
399df8bae1dSRodney W. Grimes }
400df8bae1dSRodney W. Grimes 
401d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
402df8bae1dSRodney W. Grimes struct osigblock_args {
403df8bae1dSRodney W. Grimes 	int	mask;
404df8bae1dSRodney W. Grimes };
405d2d3e875SBruce Evans #endif
40626f9a767SRodney W. Grimes int
407cb226aaaSPoul-Henning Kamp osigblock(p, uap)
408df8bae1dSRodney W. Grimes 	register struct proc *p;
409df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
410df8bae1dSRodney W. Grimes {
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	(void) splhigh();
413cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
414df8bae1dSRodney W. Grimes 	p->p_sigmask |= uap->mask &~ sigcantmask;
415df8bae1dSRodney W. Grimes 	(void) spl0();
416df8bae1dSRodney W. Grimes 	return (0);
417df8bae1dSRodney W. Grimes }
418df8bae1dSRodney W. Grimes 
419d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
420df8bae1dSRodney W. Grimes struct osigsetmask_args {
421df8bae1dSRodney W. Grimes 	int	mask;
422df8bae1dSRodney W. Grimes };
423d2d3e875SBruce Evans #endif
42426f9a767SRodney W. Grimes int
425cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
426df8bae1dSRodney W. Grimes 	struct proc *p;
427df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
428df8bae1dSRodney W. Grimes {
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 	(void) splhigh();
431cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = p->p_sigmask;
432df8bae1dSRodney W. Grimes 	p->p_sigmask = uap->mask &~ sigcantmask;
433df8bae1dSRodney W. Grimes 	(void) spl0();
434df8bae1dSRodney W. Grimes 	return (0);
435df8bae1dSRodney W. Grimes }
436df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
437df8bae1dSRodney W. Grimes 
438df8bae1dSRodney W. Grimes /*
439df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
440df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
441df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
442df8bae1dSRodney W. Grimes  */
443d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
444df8bae1dSRodney W. Grimes struct sigsuspend_args {
445df8bae1dSRodney W. Grimes 	sigset_t mask;
446df8bae1dSRodney W. Grimes };
447d2d3e875SBruce Evans #endif
448df8bae1dSRodney W. Grimes /* ARGSUSED */
44926f9a767SRodney W. Grimes int
450cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
451df8bae1dSRodney W. Grimes 	register struct proc *p;
452df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
453df8bae1dSRodney W. Grimes {
454df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
455df8bae1dSRodney W. Grimes 
456df8bae1dSRodney W. Grimes 	/*
457df8bae1dSRodney W. Grimes 	 * When returning from sigpause, we want
458df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
459df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
460df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
461df8bae1dSRodney W. Grimes 	 * to indicate this.
462df8bae1dSRodney W. Grimes 	 */
463df8bae1dSRodney W. Grimes 	ps->ps_oldmask = p->p_sigmask;
464df8bae1dSRodney W. Grimes 	ps->ps_flags |= SAS_OLDMASK;
465df8bae1dSRodney W. Grimes 	p->p_sigmask = uap->mask &~ sigcantmask;
466df8bae1dSRodney W. Grimes 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
467df8bae1dSRodney W. Grimes 		/* void */;
468df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
469df8bae1dSRodney W. Grimes 	return (EINTR);
470df8bae1dSRodney W. Grimes }
471df8bae1dSRodney W. Grimes 
472df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
473d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
474df8bae1dSRodney W. Grimes struct osigstack_args {
475df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
476df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
477df8bae1dSRodney W. Grimes };
478d2d3e875SBruce Evans #endif
479df8bae1dSRodney W. Grimes /* ARGSUSED */
48026f9a767SRodney W. Grimes int
481cb226aaaSPoul-Henning Kamp osigstack(p, uap)
482df8bae1dSRodney W. Grimes 	struct proc *p;
483df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
484df8bae1dSRodney W. Grimes {
485df8bae1dSRodney W. Grimes 	struct sigstack ss;
486df8bae1dSRodney W. Grimes 	struct sigacts *psp;
487df8bae1dSRodney W. Grimes 	int error = 0;
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 	psp = p->p_sigacts;
490aa98692fSAndreas Schulz 	ss.ss_sp = psp->ps_sigstk.ss_sp;
491d66a5066SPeter Wemm 	ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
492df8bae1dSRodney W. Grimes 	if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
493df8bae1dSRodney W. Grimes 	    sizeof (struct sigstack))))
494df8bae1dSRodney W. Grimes 		return (error);
495df8bae1dSRodney W. Grimes 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
496df8bae1dSRodney W. Grimes 	    sizeof (ss))) == 0) {
497aa98692fSAndreas Schulz 		psp->ps_sigstk.ss_sp = ss.ss_sp;
498df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_size = 0;
499d66a5066SPeter Wemm 		psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
500df8bae1dSRodney W. Grimes 		psp->ps_flags |= SAS_ALTSTACK;
501df8bae1dSRodney W. Grimes 	}
502df8bae1dSRodney W. Grimes 	return (error);
503df8bae1dSRodney W. Grimes }
504df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
505df8bae1dSRodney W. Grimes 
506d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
507df8bae1dSRodney W. Grimes struct sigaltstack_args {
508df8bae1dSRodney W. Grimes 	struct	sigaltstack *nss;
509df8bae1dSRodney W. Grimes 	struct	sigaltstack *oss;
510df8bae1dSRodney W. Grimes };
511d2d3e875SBruce Evans #endif
512df8bae1dSRodney W. Grimes /* ARGSUSED */
51326f9a767SRodney W. Grimes int
514cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
515df8bae1dSRodney W. Grimes 	struct proc *p;
516df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
517df8bae1dSRodney W. Grimes {
518df8bae1dSRodney W. Grimes 	struct sigacts *psp;
519df8bae1dSRodney W. Grimes 	struct sigaltstack ss;
520df8bae1dSRodney W. Grimes 	int error;
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes 	psp = p->p_sigacts;
523df8bae1dSRodney W. Grimes 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
524d66a5066SPeter Wemm 		psp->ps_sigstk.ss_flags |= SS_DISABLE;
525df8bae1dSRodney W. Grimes 	if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
526df8bae1dSRodney W. Grimes 	    (caddr_t)uap->oss, sizeof (struct sigaltstack))))
527df8bae1dSRodney W. Grimes 		return (error);
528df8bae1dSRodney W. Grimes 	if (uap->nss == 0)
529df8bae1dSRodney W. Grimes 		return (0);
530bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss))))
531df8bae1dSRodney W. Grimes 		return (error);
532d66a5066SPeter Wemm 	if (ss.ss_flags & SS_DISABLE) {
533d66a5066SPeter Wemm 		if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
534df8bae1dSRodney W. Grimes 			return (EINVAL);
535df8bae1dSRodney W. Grimes 		psp->ps_flags &= ~SAS_ALTSTACK;
536df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_flags = ss.ss_flags;
537df8bae1dSRodney W. Grimes 		return (0);
538df8bae1dSRodney W. Grimes 	}
539df8bae1dSRodney W. Grimes 	if (ss.ss_size < MINSIGSTKSZ)
540df8bae1dSRodney W. Grimes 		return (ENOMEM);
541df8bae1dSRodney W. Grimes 	psp->ps_flags |= SAS_ALTSTACK;
542df8bae1dSRodney W. Grimes 	psp->ps_sigstk= ss;
543df8bae1dSRodney W. Grimes 	return (0);
544df8bae1dSRodney W. Grimes }
545df8bae1dSRodney W. Grimes 
546d93f860cSPoul-Henning Kamp /*
547d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
548d93f860cSPoul-Henning Kamp  * cp is calling process.
549d93f860cSPoul-Henning Kamp  */
550d93f860cSPoul-Henning Kamp int
551d93f860cSPoul-Henning Kamp killpg1(cp, signum, pgid, all)
552d93f860cSPoul-Henning Kamp 	register struct proc *cp;
553d93f860cSPoul-Henning Kamp 	int signum, pgid, all;
554d93f860cSPoul-Henning Kamp {
555d93f860cSPoul-Henning Kamp 	register struct proc *p;
556d93f860cSPoul-Henning Kamp 	register struct pcred *pc = cp->p_cred;
557d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
558d93f860cSPoul-Henning Kamp 	int nfound = 0;
559d93f860cSPoul-Henning Kamp 
560d93f860cSPoul-Henning Kamp 	if (all)
561d93f860cSPoul-Henning Kamp 		/*
562d93f860cSPoul-Henning Kamp 		 * broadcast
563d93f860cSPoul-Henning Kamp 		 */
564b75356e1SJeffrey Hsu 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
565d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
566d93f860cSPoul-Henning Kamp 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
567d93f860cSPoul-Henning Kamp 				continue;
568d93f860cSPoul-Henning Kamp 			nfound++;
569d93f860cSPoul-Henning Kamp 			if (signum)
570d93f860cSPoul-Henning Kamp 				psignal(p, signum);
571d93f860cSPoul-Henning Kamp 		}
572d93f860cSPoul-Henning Kamp 	else {
573d93f860cSPoul-Henning Kamp 		if (pgid == 0)
574d93f860cSPoul-Henning Kamp 			/*
575d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
576d93f860cSPoul-Henning Kamp 			 */
577d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
578d93f860cSPoul-Henning Kamp 		else {
579d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
580d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
581d93f860cSPoul-Henning Kamp 				return (ESRCH);
582d93f860cSPoul-Henning Kamp 		}
583b75356e1SJeffrey Hsu 		for (p = pgrp->pg_members.lh_first; p != 0;
584b75356e1SJeffrey Hsu 		     p = p->p_pglist.le_next) {
585d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
586d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
587d93f860cSPoul-Henning Kamp 			    !CANSIGNAL(cp, pc, p, signum))
588d93f860cSPoul-Henning Kamp 				continue;
589d93f860cSPoul-Henning Kamp 			nfound++;
590d93f860cSPoul-Henning Kamp 			if (signum)
591d93f860cSPoul-Henning Kamp 				psignal(p, signum);
592d93f860cSPoul-Henning Kamp 		}
593d93f860cSPoul-Henning Kamp 	}
594d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
595d93f860cSPoul-Henning Kamp }
596d93f860cSPoul-Henning Kamp 
597d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
598df8bae1dSRodney W. Grimes struct kill_args {
599df8bae1dSRodney W. Grimes 	int	pid;
600df8bae1dSRodney W. Grimes 	int	signum;
601df8bae1dSRodney W. Grimes };
602d2d3e875SBruce Evans #endif
603df8bae1dSRodney W. Grimes /* ARGSUSED */
60426f9a767SRodney W. Grimes int
605cb226aaaSPoul-Henning Kamp kill(cp, uap)
606df8bae1dSRodney W. Grimes 	register struct proc *cp;
607df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
608df8bae1dSRodney W. Grimes {
609df8bae1dSRodney W. Grimes 	register struct proc *p;
610df8bae1dSRodney W. Grimes 	register struct pcred *pc = cp->p_cred;
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 	if ((u_int)uap->signum >= NSIG)
613df8bae1dSRodney W. Grimes 		return (EINVAL);
614df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
615df8bae1dSRodney W. Grimes 		/* kill single process */
616df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
617df8bae1dSRodney W. Grimes 			return (ESRCH);
618df8bae1dSRodney W. Grimes 		if (!CANSIGNAL(cp, pc, p, uap->signum))
619df8bae1dSRodney W. Grimes 			return (EPERM);
620df8bae1dSRodney W. Grimes 		if (uap->signum)
621df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
622df8bae1dSRodney W. Grimes 		return (0);
623df8bae1dSRodney W. Grimes 	}
624df8bae1dSRodney W. Grimes 	switch (uap->pid) {
625df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
626df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
627df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
628df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
629df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
630df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
631df8bae1dSRodney W. Grimes 	}
632df8bae1dSRodney W. Grimes 	/* NOTREACHED */
633df8bae1dSRodney W. Grimes }
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
636d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
637df8bae1dSRodney W. Grimes struct okillpg_args {
638df8bae1dSRodney W. Grimes 	int	pgid;
639df8bae1dSRodney W. Grimes 	int	signum;
640df8bae1dSRodney W. Grimes };
641d2d3e875SBruce Evans #endif
642df8bae1dSRodney W. Grimes /* ARGSUSED */
64326f9a767SRodney W. Grimes int
644cb226aaaSPoul-Henning Kamp okillpg(p, uap)
645df8bae1dSRodney W. Grimes 	struct proc *p;
646df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
647df8bae1dSRodney W. Grimes {
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes 	if ((u_int)uap->signum >= NSIG)
650df8bae1dSRodney W. Grimes 		return (EINVAL);
651df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
652df8bae1dSRodney W. Grimes }
653df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes /*
656df8bae1dSRodney W. Grimes  * Send a signal to a process group.
657df8bae1dSRodney W. Grimes  */
658df8bae1dSRodney W. Grimes void
659df8bae1dSRodney W. Grimes gsignal(pgid, signum)
660df8bae1dSRodney W. Grimes 	int pgid, signum;
661df8bae1dSRodney W. Grimes {
662df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
665df8bae1dSRodney W. Grimes 		pgsignal(pgrp, signum, 0);
666df8bae1dSRodney W. Grimes }
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes /*
669df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
670df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
671df8bae1dSRodney W. Grimes  */
672df8bae1dSRodney W. Grimes void
673df8bae1dSRodney W. Grimes pgsignal(pgrp, signum, checkctty)
674df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
675df8bae1dSRodney W. Grimes 	int signum, checkctty;
676df8bae1dSRodney W. Grimes {
677df8bae1dSRodney W. Grimes 	register struct proc *p;
678df8bae1dSRodney W. Grimes 
679df8bae1dSRodney W. Grimes 	if (pgrp)
680b75356e1SJeffrey Hsu 		for (p = pgrp->pg_members.lh_first; p != 0;
681b75356e1SJeffrey Hsu 		     p = p->p_pglist.le_next)
682df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
683df8bae1dSRodney W. Grimes 				psignal(p, signum);
684df8bae1dSRodney W. Grimes }
685df8bae1dSRodney W. Grimes 
686df8bae1dSRodney W. Grimes /*
687df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
688df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
689df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
690df8bae1dSRodney W. Grimes  */
691df8bae1dSRodney W. Grimes void
692df8bae1dSRodney W. Grimes trapsignal(p, signum, code)
693df8bae1dSRodney W. Grimes 	struct proc *p;
694df8bae1dSRodney W. Grimes 	register int signum;
6958674077aSJeffrey Hsu 	u_long code;
696df8bae1dSRodney W. Grimes {
697df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
698df8bae1dSRodney W. Grimes 	int mask;
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
701df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
702df8bae1dSRodney W. Grimes 	    (p->p_sigmask & mask) == 0) {
703df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
704df8bae1dSRodney W. Grimes #ifdef KTRACE
705df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
706df8bae1dSRodney W. Grimes 			ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
707df8bae1dSRodney W. Grimes 				p->p_sigmask, code);
708df8bae1dSRodney W. Grimes #endif
709d66a5066SPeter Wemm 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum,
710d66a5066SPeter Wemm 						p->p_sigmask, code);
711289ccde0SPeter Wemm 		p->p_sigmask |= ps->ps_catchmask[signum] |
712289ccde0SPeter Wemm 				(mask & ~ps->ps_signodefer);
713dedc04feSPeter Wemm 		if ((ps->ps_sigreset & mask) != 0) {
714289ccde0SPeter Wemm 			/*
715289ccde0SPeter Wemm 			 * See setsigvec() for origin of this code.
716289ccde0SPeter Wemm 			 */
717dedc04feSPeter Wemm 			p->p_sigcatch &= ~mask;
718dedc04feSPeter Wemm 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
719dedc04feSPeter Wemm 				p->p_sigignore |= mask;
720dedc04feSPeter Wemm 			ps->ps_sigact[signum] = SIG_DFL;
721dedc04feSPeter Wemm 		}
722df8bae1dSRodney W. Grimes 	} else {
723df8bae1dSRodney W. Grimes 		ps->ps_code = code;	/* XXX for core dump/debugger */
7248674077aSJeffrey Hsu 		ps->ps_sig = signum;	/* XXX to verify code */
725df8bae1dSRodney W. Grimes 		psignal(p, signum);
726df8bae1dSRodney W. Grimes 	}
727df8bae1dSRodney W. Grimes }
728df8bae1dSRodney W. Grimes 
729df8bae1dSRodney W. Grimes /*
730df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
731df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
732df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
733df8bae1dSRodney W. Grimes  *
734df8bae1dSRodney W. Grimes  * Exceptions:
735df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
736df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
737df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
738df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
739df8bae1dSRodney W. Grimes  *
740df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
741df8bae1dSRodney W. Grimes  */
742df8bae1dSRodney W. Grimes void
743df8bae1dSRodney W. Grimes psignal(p, signum)
744df8bae1dSRodney W. Grimes 	register struct proc *p;
745df8bae1dSRodney W. Grimes 	register int signum;
746df8bae1dSRodney W. Grimes {
747df8bae1dSRodney W. Grimes 	register int s, prop;
748df8bae1dSRodney W. Grimes 	register sig_t action;
749df8bae1dSRodney W. Grimes 	int mask;
750df8bae1dSRodney W. Grimes 
7512a024a2bSSean Eric Fagan 	if ((u_int)signum >= NSIG || signum == 0) {
7522a024a2bSSean Eric Fagan 		printf("psignal: signum %d\n", signum);
753df8bae1dSRodney W. Grimes 		panic("psignal signal number");
7542a024a2bSSean Eric Fagan 	}
755df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
756df8bae1dSRodney W. Grimes 	prop = sigprop[signum];
757df8bae1dSRodney W. Grimes 
758df8bae1dSRodney W. Grimes 	/*
7592a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
7602a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
7612a024a2bSSean Eric Fagan 	 * a chance, as well.
762df8bae1dSRodney W. Grimes 	 */
7632a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
764df8bae1dSRodney W. Grimes 		action = SIG_DFL;
765df8bae1dSRodney W. Grimes 	else {
766df8bae1dSRodney W. Grimes 		/*
767df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
768df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
769df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
770df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
771df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
772df8bae1dSRodney W. Grimes 		 */
773df8bae1dSRodney W. Grimes 		if (p->p_sigignore & mask)
774df8bae1dSRodney W. Grimes 			return;
775df8bae1dSRodney W. Grimes 		if (p->p_sigmask & mask)
776df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
777df8bae1dSRodney W. Grimes 		else if (p->p_sigcatch & mask)
778df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
779df8bae1dSRodney W. Grimes 		else
780df8bae1dSRodney W. Grimes 			action = SIG_DFL;
781df8bae1dSRodney W. Grimes 	}
782df8bae1dSRodney W. Grimes 
783df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
784df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
785df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
786df8bae1dSRodney W. Grimes 
787df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
788df8bae1dSRodney W. Grimes 		p->p_siglist &= ~stopsigmask;
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
791df8bae1dSRodney W. Grimes 		/*
792df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
793df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
794df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
795df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
796df8bae1dSRodney W. Grimes 		 */
797df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
798df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
799df8bae1dSRodney W. Grimes 		        return;
800df8bae1dSRodney W. Grimes 		p->p_siglist &= ~contsigmask;
801df8bae1dSRodney W. Grimes 	}
802df8bae1dSRodney W. Grimes 	p->p_siglist |= mask;
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes 	/*
805df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
806df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
807df8bae1dSRodney W. Grimes 	 */
808df8bae1dSRodney W. Grimes 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
809df8bae1dSRodney W. Grimes 		return;
810df8bae1dSRodney W. Grimes 	s = splhigh();
811df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
812df8bae1dSRodney W. Grimes 
813df8bae1dSRodney W. Grimes 	case SSLEEP:
814df8bae1dSRodney W. Grimes 		/*
815df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
816df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
817df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
818df8bae1dSRodney W. Grimes 		 * trap() or syscall().
819df8bae1dSRodney W. Grimes 		 */
820df8bae1dSRodney W. Grimes 		if ((p->p_flag & P_SINTR) == 0)
821df8bae1dSRodney W. Grimes 			goto out;
822df8bae1dSRodney W. Grimes 		/*
823df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
824df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
825df8bae1dSRodney W. Grimes 		 * for the parent.
826df8bae1dSRodney W. Grimes 		 */
827df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
828df8bae1dSRodney W. Grimes 			goto run;
829df8bae1dSRodney W. Grimes 		/*
830df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
831df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
832df8bae1dSRodney W. Grimes 		 * be awakened.
833df8bae1dSRodney W. Grimes 		 */
834df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
835df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
836df8bae1dSRodney W. Grimes 			goto out;
837df8bae1dSRodney W. Grimes 		}
838df8bae1dSRodney W. Grimes 		/*
839df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
840df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
841df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
842df8bae1dSRodney W. Grimes 		 * cause the process to run.
843df8bae1dSRodney W. Grimes 		 */
844df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
845df8bae1dSRodney W. Grimes 			if (action != SIG_DFL)
846df8bae1dSRodney W. Grimes 				goto runfast;
847df8bae1dSRodney W. Grimes 			/*
848df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
849df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
850df8bae1dSRodney W. Grimes 			 */
851df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
852df8bae1dSRodney W. Grimes 				goto out;
853df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
854df8bae1dSRodney W. Grimes 			p->p_xstat = signum;
855df8bae1dSRodney W. Grimes 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
856df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
857df8bae1dSRodney W. Grimes 			stop(p);
858df8bae1dSRodney W. Grimes 			goto out;
859df8bae1dSRodney W. Grimes 		} else
860df8bae1dSRodney W. Grimes 			goto runfast;
861df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
862df8bae1dSRodney W. Grimes 
863df8bae1dSRodney W. Grimes 	case SSTOP:
864df8bae1dSRodney W. Grimes 		/*
865df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
866df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
867df8bae1dSRodney W. Grimes 		 */
868df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
869df8bae1dSRodney W. Grimes 			goto out;
870df8bae1dSRodney W. Grimes 
871df8bae1dSRodney W. Grimes 		/*
872df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
873df8bae1dSRodney W. Grimes 		 */
874df8bae1dSRodney W. Grimes 		if (signum == SIGKILL)
875df8bae1dSRodney W. Grimes 			goto runfast;
876df8bae1dSRodney W. Grimes 
877df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
878df8bae1dSRodney W. Grimes 			/*
879df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
880df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
881df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
882df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
883df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
884df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
885df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
886df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
887df8bae1dSRodney W. Grimes 			 */
888df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
889df8bae1dSRodney W. Grimes 				p->p_siglist &= ~mask;
890df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
891df8bae1dSRodney W. Grimes 				goto runfast;
892df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
893df8bae1dSRodney W. Grimes 				goto run;
894df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
895df8bae1dSRodney W. Grimes 			goto out;
896df8bae1dSRodney W. Grimes 		}
897df8bae1dSRodney W. Grimes 
898df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
899df8bae1dSRodney W. Grimes 			/*
900df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
901df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
902df8bae1dSRodney W. Grimes 			 */
903df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;		/* take it away */
904df8bae1dSRodney W. Grimes 			goto out;
905df8bae1dSRodney W. Grimes 		}
906df8bae1dSRodney W. Grimes 
907df8bae1dSRodney W. Grimes 		/*
908df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
909df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
910df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
911df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
912df8bae1dSRodney W. Grimes 		 */
913df8bae1dSRodney W. Grimes 		if (p->p_wchan && p->p_flag & P_SINTR)
914df8bae1dSRodney W. Grimes 			unsleep(p);
915df8bae1dSRodney W. Grimes 		goto out;
916df8bae1dSRodney W. Grimes 
917df8bae1dSRodney W. Grimes 	default:
918df8bae1dSRodney W. Grimes 		/*
919df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
920df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
921df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
922df8bae1dSRodney W. Grimes 		 */
923df8bae1dSRodney W. Grimes 		if (p == curproc)
924df8bae1dSRodney W. Grimes 			signotify(p);
9253163861cSTor Egge #ifdef SMP
9263163861cSTor Egge 		else if (p->p_stat == SRUN)
9273163861cSTor Egge 			forward_signal(p);
9283163861cSTor Egge #endif
929df8bae1dSRodney W. Grimes 		goto out;
930df8bae1dSRodney W. Grimes 	}
931df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes runfast:
934df8bae1dSRodney W. Grimes 	/*
935df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
936df8bae1dSRodney W. Grimes 	 */
937df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
938df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
939df8bae1dSRodney W. Grimes run:
940df8bae1dSRodney W. Grimes 	setrunnable(p);
941df8bae1dSRodney W. Grimes out:
942df8bae1dSRodney W. Grimes 	splx(s);
943df8bae1dSRodney W. Grimes }
944df8bae1dSRodney W. Grimes 
945df8bae1dSRodney W. Grimes /*
946df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
947df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
948df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
949df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
950df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
951df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
952df8bae1dSRodney W. Grimes  * sequence is
953df8bae1dSRodney W. Grimes  *
954df8bae1dSRodney W. Grimes  *	while (signum = CURSIG(curproc))
955df8bae1dSRodney W. Grimes  *		postsig(signum);
956df8bae1dSRodney W. Grimes  */
95726f9a767SRodney W. Grimes int
958df8bae1dSRodney W. Grimes issignal(p)
959df8bae1dSRodney W. Grimes 	register struct proc *p;
960df8bae1dSRodney W. Grimes {
961df8bae1dSRodney W. Grimes 	register int signum, mask, prop;
962df8bae1dSRodney W. Grimes 
963df8bae1dSRodney W. Grimes 	for (;;) {
9642a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
9652a024a2bSSean Eric Fagan 
966df8bae1dSRodney W. Grimes 		mask = p->p_siglist & ~p->p_sigmask;
967df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
968df8bae1dSRodney W. Grimes 			mask &= ~stopsigmask;
969df8bae1dSRodney W. Grimes 		if (mask == 0)	 	/* no signal to send */
970df8bae1dSRodney W. Grimes 			return (0);
971df8bae1dSRodney W. Grimes 		signum = ffs((long)mask);
972df8bae1dSRodney W. Grimes 		mask = sigmask(signum);
973df8bae1dSRodney W. Grimes 		prop = sigprop[signum];
9742a024a2bSSean Eric Fagan 
9752a024a2bSSean Eric Fagan 		STOPEVENT(p, S_SIG, signum);
9762a024a2bSSean Eric Fagan 
977df8bae1dSRodney W. Grimes 		/*
978df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
979df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
980df8bae1dSRodney W. Grimes 		 */
9812a024a2bSSean Eric Fagan 		if ((mask & p->p_sigignore) && (traced == 0)) {
982df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
983df8bae1dSRodney W. Grimes 			continue;
984df8bae1dSRodney W. Grimes 		}
985df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
986df8bae1dSRodney W. Grimes 			/*
987df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
988df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
989df8bae1dSRodney W. Grimes 			 */
990df8bae1dSRodney W. Grimes 			p->p_xstat = signum;
991df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
992df8bae1dSRodney W. Grimes 			do {
993df8bae1dSRodney W. Grimes 				stop(p);
994df8bae1dSRodney W. Grimes 				mi_switch();
9952a024a2bSSean Eric Fagan 			} while (!trace_req(p)
9962a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 			/*
999df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1000df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1001df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1002df8bae1dSRodney W. Grimes 			 */
1003df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1004df8bae1dSRodney W. Grimes 				continue;
1005df8bae1dSRodney W. Grimes 
1006df8bae1dSRodney W. Grimes 			/*
1007df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1008df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1009df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1010df8bae1dSRodney W. Grimes 			 */
1011df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;	/* clear the old signal */
1012df8bae1dSRodney W. Grimes 			signum = p->p_xstat;
1013df8bae1dSRodney W. Grimes 			if (signum == 0)
1014df8bae1dSRodney W. Grimes 				continue;
1015df8bae1dSRodney W. Grimes 
1016df8bae1dSRodney W. Grimes 			/*
1017df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1018df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1019df8bae1dSRodney W. Grimes 			 */
1020df8bae1dSRodney W. Grimes 			mask = sigmask(signum);
1021df8bae1dSRodney W. Grimes 			p->p_siglist |= mask;
1022df8bae1dSRodney W. Grimes 			if (p->p_sigmask & mask)
1023df8bae1dSRodney W. Grimes 				continue;
1024df8bae1dSRodney W. Grimes 		}
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes 		/*
1027df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1028df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1029df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1030df8bae1dSRodney W. Grimes 		 */
1031df8bae1dSRodney W. Grimes 		switch ((int)p->p_sigacts->ps_sigact[signum]) {
1032df8bae1dSRodney W. Grimes 
10330b53fbe8SBruce Evans 		case (int)SIG_DFL:
1034df8bae1dSRodney W. Grimes 			/*
1035df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1036df8bae1dSRodney W. Grimes 			 */
1037df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1038df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1039df8bae1dSRodney W. Grimes 				/*
1040df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1041df8bae1dSRodney W. Grimes 				 * in init? XXX
1042df8bae1dSRodney W. Grimes 				 */
1043d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
1044d93f860cSPoul-Henning Kamp 					(u_long)p->p_pid, signum);
1045df8bae1dSRodney W. Grimes #endif
1046df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1047df8bae1dSRodney W. Grimes 			}
1048df8bae1dSRodney W. Grimes 			/*
1049df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1050df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1051df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1052df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1053df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1054df8bae1dSRodney W. Grimes 			 */
1055df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1056df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1057df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1058df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1059df8bae1dSRodney W. Grimes 					break;	/* == ignore */
1060df8bae1dSRodney W. Grimes 				p->p_xstat = signum;
1061df8bae1dSRodney W. Grimes 				stop(p);
1062df8bae1dSRodney W. Grimes 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1063df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1064df8bae1dSRodney W. Grimes 				mi_switch();
1065df8bae1dSRodney W. Grimes 				break;
1066df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1067df8bae1dSRodney W. Grimes 				/*
1068df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1069df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1070df8bae1dSRodney W. Grimes 				 */
1071df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1072df8bae1dSRodney W. Grimes 			} else
1073df8bae1dSRodney W. Grimes 				return (signum);
1074df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1075df8bae1dSRodney W. Grimes 
10760b53fbe8SBruce Evans 		case (int)SIG_IGN:
1077df8bae1dSRodney W. Grimes 			/*
1078df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1079df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1080df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1081df8bae1dSRodney W. Grimes 			 */
1082df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1083df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1084df8bae1dSRodney W. Grimes 				printf("issignal\n");
1085df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1086df8bae1dSRodney W. Grimes 
1087df8bae1dSRodney W. Grimes 		default:
1088df8bae1dSRodney W. Grimes 			/*
1089df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1090df8bae1dSRodney W. Grimes 			 * postsig() process it.
1091df8bae1dSRodney W. Grimes 			 */
1092df8bae1dSRodney W. Grimes 			return (signum);
1093df8bae1dSRodney W. Grimes 		}
1094df8bae1dSRodney W. Grimes 		p->p_siglist &= ~mask;		/* take the signal! */
1095df8bae1dSRodney W. Grimes 	}
1096df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1097df8bae1dSRodney W. Grimes }
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes /*
1100df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1101df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
1102df8bae1dSRodney W. Grimes  * on the run queue.
1103df8bae1dSRodney W. Grimes  */
110426f9a767SRodney W. Grimes void
1105df8bae1dSRodney W. Grimes stop(p)
1106df8bae1dSRodney W. Grimes 	register struct proc *p;
1107df8bae1dSRodney W. Grimes {
1108df8bae1dSRodney W. Grimes 
1109df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1110df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1111df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1112df8bae1dSRodney W. Grimes }
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes /*
1115df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1116df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1117df8bae1dSRodney W. Grimes  */
1118df8bae1dSRodney W. Grimes void
1119df8bae1dSRodney W. Grimes postsig(signum)
1120df8bae1dSRodney W. Grimes 	register int signum;
1121df8bae1dSRodney W. Grimes {
1122df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
1123df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1124df8bae1dSRodney W. Grimes 	register sig_t action;
1125df8bae1dSRodney W. Grimes 	int code, mask, returnmask;
1126df8bae1dSRodney W. Grimes 
1127df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1128df8bae1dSRodney W. Grimes 	if (signum == 0)
1129df8bae1dSRodney W. Grimes 		panic("postsig");
1130df8bae1dSRodney W. Grimes #endif
1131df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
1132df8bae1dSRodney W. Grimes 	p->p_siglist &= ~mask;
1133df8bae1dSRodney W. Grimes 	action = ps->ps_sigact[signum];
1134df8bae1dSRodney W. Grimes #ifdef KTRACE
1135df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1136df8bae1dSRodney W. Grimes 		ktrpsig(p->p_tracep,
1137df8bae1dSRodney W. Grimes 		    signum, action, ps->ps_flags & SAS_OLDMASK ?
1138df8bae1dSRodney W. Grimes 		    ps->ps_oldmask : p->p_sigmask, 0);
1139df8bae1dSRodney W. Grimes #endif
11402a024a2bSSean Eric Fagan 	STOPEVENT(p, S_SIG, signum);
11412a024a2bSSean Eric Fagan 
1142df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1143df8bae1dSRodney W. Grimes 		/*
1144df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1145df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1146df8bae1dSRodney W. Grimes 		 */
1147df8bae1dSRodney W. Grimes 		sigexit(p, signum);
1148df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1149df8bae1dSRodney W. Grimes 	} else {
1150df8bae1dSRodney W. Grimes 		/*
1151df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1152df8bae1dSRodney W. Grimes 		 */
1153df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1154df8bae1dSRodney W. Grimes 		if (action == SIG_IGN || (p->p_sigmask & mask))
1155df8bae1dSRodney W. Grimes 			panic("postsig action");
1156df8bae1dSRodney W. Grimes #endif
1157df8bae1dSRodney W. Grimes 		/*
1158df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1159df8bae1dSRodney W. Grimes 		 * occurences of this signal.
1160df8bae1dSRodney W. Grimes 		 *
1161df8bae1dSRodney W. Grimes 		 * Special case: user has done a sigpause.  Here the
1162df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1163df8bae1dSRodney W. Grimes 		 * mask from before the sigpause is what we want
1164df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1165df8bae1dSRodney W. Grimes 		 */
1166df8bae1dSRodney W. Grimes 		(void) splhigh();
1167df8bae1dSRodney W. Grimes 		if (ps->ps_flags & SAS_OLDMASK) {
1168df8bae1dSRodney W. Grimes 			returnmask = ps->ps_oldmask;
1169df8bae1dSRodney W. Grimes 			ps->ps_flags &= ~SAS_OLDMASK;
1170df8bae1dSRodney W. Grimes 		} else
1171df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
1172289ccde0SPeter Wemm 		p->p_sigmask |= ps->ps_catchmask[signum] |
1173289ccde0SPeter Wemm 				(mask & ~ps->ps_signodefer);
1174dedc04feSPeter Wemm 		if ((ps->ps_sigreset & mask) != 0) {
1175289ccde0SPeter Wemm 			/*
1176289ccde0SPeter Wemm 			 * See setsigvec() for origin of this code.
1177289ccde0SPeter Wemm 			 */
1178dedc04feSPeter Wemm 			p->p_sigcatch &= ~mask;
1179dedc04feSPeter Wemm 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1180dedc04feSPeter Wemm 				p->p_sigignore |= mask;
1181dedc04feSPeter Wemm 			ps->ps_sigact[signum] = SIG_DFL;
1182dedc04feSPeter Wemm 		}
1183df8bae1dSRodney W. Grimes 		(void) spl0();
1184df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1185df8bae1dSRodney W. Grimes 		if (ps->ps_sig != signum) {
1186df8bae1dSRodney W. Grimes 			code = 0;
1187df8bae1dSRodney W. Grimes 		} else {
1188df8bae1dSRodney W. Grimes 			code = ps->ps_code;
1189df8bae1dSRodney W. Grimes 			ps->ps_code = 0;
11908674077aSJeffrey Hsu 			ps->ps_sig = 0;
1191df8bae1dSRodney W. Grimes 		}
1192d66a5066SPeter Wemm 		(*p->p_sysent->sv_sendsig)(action, signum, returnmask, code);
1193df8bae1dSRodney W. Grimes 	}
1194df8bae1dSRodney W. Grimes }
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes /*
1197df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1198df8bae1dSRodney W. Grimes  */
119926f9a767SRodney W. Grimes void
1200df8bae1dSRodney W. Grimes killproc(p, why)
1201df8bae1dSRodney W. Grimes 	struct proc *p;
1202df8bae1dSRodney W. Grimes 	char *why;
1203df8bae1dSRodney W. Grimes {
1204729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1205729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1206df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1207df8bae1dSRodney W. Grimes }
1208df8bae1dSRodney W. Grimes 
1209df8bae1dSRodney W. Grimes /*
1210df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1211df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1212df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1213df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1214df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1215df8bae1dSRodney W. Grimes  * does not return.
1216df8bae1dSRodney W. Grimes  */
121726f9a767SRodney W. Grimes void
1218df8bae1dSRodney W. Grimes sigexit(p, signum)
1219df8bae1dSRodney W. Grimes 	register struct proc *p;
1220df8bae1dSRodney W. Grimes 	int signum;
1221df8bae1dSRodney W. Grimes {
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
1224df8bae1dSRodney W. Grimes 	if (sigprop[signum] & SA_CORE) {
1225df8bae1dSRodney W. Grimes 		p->p_sigacts->ps_sig = signum;
1226c364e17eSAndrey A. Chernov 		/*
1227c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1228c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1229c364e17eSAndrey A. Chernov 		 * these messages.)
1230c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1231c364e17eSAndrey A. Chernov 		 */
1232df8bae1dSRodney W. Grimes 		if (coredump(p) == 0)
1233df8bae1dSRodney W. Grimes 			signum |= WCOREFLAG;
12343d1b21c6SAndrey A. Chernov 		log(LOG_INFO, "pid %d (%s), uid %d: exited on signal %d%s\n",
12353d1b21c6SAndrey A. Chernov 			p->p_pid, p->p_comm,
12363d1b21c6SAndrey A. Chernov 			p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
12373d1b21c6SAndrey A. Chernov 			signum &~ WCOREFLAG,
12383d1b21c6SAndrey A. Chernov 			signum & WCOREFLAG ? " (core dumped)" : "");
1239df8bae1dSRodney W. Grimes 	}
1240df8bae1dSRodney W. Grimes 	exit1(p, W_EXITCODE(0, signum));
1241df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1242df8bae1dSRodney W. Grimes }
1243df8bae1dSRodney W. Grimes 
1244df8bae1dSRodney W. Grimes /*
1245df8bae1dSRodney W. Grimes  * Dump core, into a file named "progname.core", unless the process was
1246df8bae1dSRodney W. Grimes  * setuid/setgid.
1247df8bae1dSRodney W. Grimes  */
124887b6de2bSPoul-Henning Kamp static int
1249df8bae1dSRodney W. Grimes coredump(p)
1250df8bae1dSRodney W. Grimes 	register struct proc *p;
1251df8bae1dSRodney W. Grimes {
1252df8bae1dSRodney W. Grimes 	register struct vnode *vp;
12538713ad74SDavid Greenman 	register struct ucred *cred = p->p_cred->pc_ucred;
1254df8bae1dSRodney W. Grimes 	register struct vmspace *vm = p->p_vmspace;
1255df8bae1dSRodney W. Grimes 	struct nameidata nd;
1256df8bae1dSRodney W. Grimes 	struct vattr vattr;
1257df8bae1dSRodney W. Grimes 	int error, error1;
1258df8bae1dSRodney W. Grimes 	char name[MAXCOMLEN+6];		/* progname.core */
1259df8bae1dSRodney W. Grimes 
12602a024a2bSSean Eric Fagan 	STOPEVENT(p, S_CORE, 0);
12612a024a2bSSean Eric Fagan 
1262d66a5066SPeter Wemm 	if (p->p_flag & P_SUGID)
1263d66a5066SPeter Wemm 		return (EFAULT);
1264df8bae1dSRodney W. Grimes 	if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
1265df8bae1dSRodney W. Grimes 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1266df8bae1dSRodney W. Grimes 		return (EFAULT);
1267df8bae1dSRodney W. Grimes 	sprintf(name, "%s.core", p->p_comm);
1268df8bae1dSRodney W. Grimes 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1269bb56ec4aSPoul-Henning Kamp 	if ((error = vn_open(&nd,
127008ee5d13SAndrey A. Chernov 	    O_CREAT | FWRITE, S_IRUSR | S_IWUSR)))
1271df8bae1dSRodney W. Grimes 		return (error);
1272df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
1273df8bae1dSRodney W. Grimes 
1274df8bae1dSRodney W. Grimes 	/* Don't dump to non-regular files or files with links. */
1275df8bae1dSRodney W. Grimes 	if (vp->v_type != VREG ||
1276df8bae1dSRodney W. Grimes 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1277df8bae1dSRodney W. Grimes 		error = EFAULT;
1278df8bae1dSRodney W. Grimes 		goto out;
1279df8bae1dSRodney W. Grimes 	}
1280df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
1281df8bae1dSRodney W. Grimes 	vattr.va_size = 0;
1282996c772fSJohn Dyson 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1283df8bae1dSRodney W. Grimes 	VOP_SETATTR(vp, &vattr, cred, p);
1284df8bae1dSRodney W. Grimes 	p->p_acflag |= ACORE;
1285df8bae1dSRodney W. Grimes 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1286df8bae1dSRodney W. Grimes 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1287df8bae1dSRodney W. Grimes 	error = cpu_coredump(p, vp, cred);
1288df8bae1dSRodney W. Grimes 	if (error == 0)
1289df8bae1dSRodney W. Grimes 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1290df8bae1dSRodney W. Grimes 		    (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1291df8bae1dSRodney W. Grimes 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1292df8bae1dSRodney W. Grimes 	if (error == 0)
1293df8bae1dSRodney W. Grimes 		error = vn_rdwr(UIO_WRITE, vp,
1294df8bae1dSRodney W. Grimes 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1295df8bae1dSRodney W. Grimes 		    round_page(ctob(vm->vm_ssize)),
1296df8bae1dSRodney W. Grimes 		    (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
1297df8bae1dSRodney W. Grimes 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1298df8bae1dSRodney W. Grimes out:
1299996c772fSJohn Dyson 	VOP_UNLOCK(vp, 0, p);
1300df8bae1dSRodney W. Grimes 	error1 = vn_close(vp, FWRITE, cred, p);
1301df8bae1dSRodney W. Grimes 	if (error == 0)
1302df8bae1dSRodney W. Grimes 		error = error1;
1303df8bae1dSRodney W. Grimes 	return (error);
1304df8bae1dSRodney W. Grimes }
1305df8bae1dSRodney W. Grimes 
1306df8bae1dSRodney W. Grimes /*
1307df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1308df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1309df8bae1dSRodney W. Grimes  */
1310d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1311df8bae1dSRodney W. Grimes struct nosys_args {
1312df8bae1dSRodney W. Grimes 	int	dummy;
1313df8bae1dSRodney W. Grimes };
1314d2d3e875SBruce Evans #endif
1315df8bae1dSRodney W. Grimes /* ARGSUSED */
131626f9a767SRodney W. Grimes int
1317cb226aaaSPoul-Henning Kamp nosys(p, args)
1318df8bae1dSRodney W. Grimes 	struct proc *p;
1319df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1320df8bae1dSRodney W. Grimes {
1321df8bae1dSRodney W. Grimes 
1322df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1323df8bae1dSRodney W. Grimes 	return (EINVAL);
1324df8bae1dSRodney W. Grimes }
1325