xref: /freebsd/sys/kern/kern_sig.c (revision d2d3e8751c3b7831f7064ccf400978b75b696547)
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
39d2d3e875SBruce Evans  * $Id: kern_sig.c,v 1.12 1995/10/19 19:15:23 swallace Exp $
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42df8bae1dSRodney W. Grimes #define	SIGPROP		/* include signal properties table */
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44d2d3e875SBruce Evans #include <sys/sysproto.h>
45df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
46df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
47df8bae1dSRodney W. Grimes #include <sys/namei.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
49df8bae1dSRodney W. Grimes #include <sys/proc.h>
50df8bae1dSRodney W. Grimes #include <sys/systm.h>
51df8bae1dSRodney W. Grimes #include <sys/timeb.h>
52df8bae1dSRodney W. Grimes #include <sys/times.h>
53df8bae1dSRodney W. Grimes #include <sys/buf.h>
54df8bae1dSRodney W. Grimes #include <sys/acct.h>
55df8bae1dSRodney W. Grimes #include <sys/file.h>
56df8bae1dSRodney W. Grimes #include <sys/kernel.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>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <machine/cpu.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <vm/vm.h>
65df8bae1dSRodney W. Grimes #include <sys/user.h>		/* for coredump */
66df8bae1dSRodney W. Grimes 
6726f9a767SRodney W. Grimes void setsigvec	__P((struct proc *, int, struct sigaction *));
6826f9a767SRodney W. Grimes void stop	__P((struct proc *));
6926f9a767SRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  * Can process p, with pcred pc, send the signal signum to process q?
72df8bae1dSRodney W. Grimes  */
73df8bae1dSRodney W. Grimes #define CANSIGNAL(p, pc, q, signum) \
74df8bae1dSRodney W. Grimes 	((pc)->pc_ucred->cr_uid == 0 || \
75df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
76df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
77df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
78df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
79df8bae1dSRodney W. Grimes 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
80df8bae1dSRodney W. Grimes 
81d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
82df8bae1dSRodney W. Grimes struct sigaction_args {
83df8bae1dSRodney W. Grimes 	int	signum;
84df8bae1dSRodney W. Grimes 	struct	sigaction *nsa;
85df8bae1dSRodney W. Grimes 	struct	sigaction *osa;
86df8bae1dSRodney W. Grimes };
87d2d3e875SBruce Evans #endif
88df8bae1dSRodney W. Grimes /* ARGSUSED */
8926f9a767SRodney W. Grimes int
90df8bae1dSRodney W. Grimes sigaction(p, uap, retval)
91df8bae1dSRodney W. Grimes 	struct proc *p;
92df8bae1dSRodney W. Grimes 	register struct sigaction_args *uap;
93df8bae1dSRodney W. Grimes 	int *retval;
94df8bae1dSRodney W. Grimes {
95df8bae1dSRodney W. Grimes 	struct sigaction vec;
96df8bae1dSRodney W. Grimes 	register struct sigaction *sa;
97df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
98df8bae1dSRodney W. Grimes 	register int signum;
99df8bae1dSRodney W. Grimes 	int bit, error;
100df8bae1dSRodney W. Grimes 
101df8bae1dSRodney W. Grimes 	signum = uap->signum;
102df8bae1dSRodney W. Grimes 	if (signum <= 0 || signum >= NSIG ||
103df8bae1dSRodney W. Grimes 	    signum == SIGKILL || signum == SIGSTOP)
104df8bae1dSRodney W. Grimes 		return (EINVAL);
105df8bae1dSRodney W. Grimes 	sa = &vec;
106df8bae1dSRodney W. Grimes 	if (uap->osa) {
107df8bae1dSRodney W. Grimes 		sa->sa_handler = ps->ps_sigact[signum];
108df8bae1dSRodney W. Grimes 		sa->sa_mask = ps->ps_catchmask[signum];
109df8bae1dSRodney W. Grimes 		bit = sigmask(signum);
110df8bae1dSRodney W. Grimes 		sa->sa_flags = 0;
111df8bae1dSRodney W. Grimes 		if ((ps->ps_sigonstack & bit) != 0)
112df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_ONSTACK;
113df8bae1dSRodney W. Grimes 		if ((ps->ps_sigintr & bit) == 0)
114df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_RESTART;
1151e41c1b5SSteven Wallace 		if ((ps->ps_nodefer & bit) != 0)
1161e41c1b5SSteven Wallace 			sa->sa_flags |= SA_NODEFER;
117df8bae1dSRodney W. Grimes 		if (p->p_flag & P_NOCLDSTOP)
118df8bae1dSRodney W. Grimes 			sa->sa_flags |= SA_NOCLDSTOP;
119bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa,
120bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
121df8bae1dSRodney W. Grimes 			return (error);
122df8bae1dSRodney W. Grimes 	}
123df8bae1dSRodney W. Grimes 	if (uap->nsa) {
124bb56ec4aSPoul-Henning Kamp 		if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
125bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
126df8bae1dSRodney W. Grimes 			return (error);
127df8bae1dSRodney W. Grimes 		setsigvec(p, signum, sa);
128df8bae1dSRodney W. Grimes 	}
129df8bae1dSRodney W. Grimes 	return (0);
130df8bae1dSRodney W. Grimes }
131df8bae1dSRodney W. Grimes 
13226f9a767SRodney W. Grimes void
133df8bae1dSRodney W. Grimes setsigvec(p, signum, sa)
134df8bae1dSRodney W. Grimes 	register struct proc *p;
135df8bae1dSRodney W. Grimes 	int signum;
136df8bae1dSRodney W. Grimes 	register struct sigaction *sa;
137df8bae1dSRodney W. Grimes {
138df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
139df8bae1dSRodney W. Grimes 	register int bit;
140df8bae1dSRodney W. Grimes 
141df8bae1dSRodney W. Grimes 	bit = sigmask(signum);
142df8bae1dSRodney W. Grimes 	/*
143df8bae1dSRodney W. Grimes 	 * Change setting atomically.
144df8bae1dSRodney W. Grimes 	 */
145df8bae1dSRodney W. Grimes 	(void) splhigh();
146df8bae1dSRodney W. Grimes 	ps->ps_sigact[signum] = sa->sa_handler;
147df8bae1dSRodney W. Grimes 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
148df8bae1dSRodney W. Grimes 	if ((sa->sa_flags & SA_RESTART) == 0)
149df8bae1dSRodney W. Grimes 		ps->ps_sigintr |= bit;
150df8bae1dSRodney W. Grimes 	else
151df8bae1dSRodney W. Grimes 		ps->ps_sigintr &= ~bit;
152df8bae1dSRodney W. Grimes 	if (sa->sa_flags & SA_ONSTACK)
153df8bae1dSRodney W. Grimes 		ps->ps_sigonstack |= bit;
154df8bae1dSRodney W. Grimes 	else
155df8bae1dSRodney W. Grimes 		ps->ps_sigonstack &= ~bit;
1561e41c1b5SSteven Wallace 	if (sa->sa_flags & SA_NODEFER)
1571e41c1b5SSteven Wallace 		ps->ps_nodefer |= bit;
1581e41c1b5SSteven Wallace 	else
1591e41c1b5SSteven Wallace 		ps->ps_nodefer &= ~bit;
160df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
161df8bae1dSRodney W. Grimes 	if (sa->sa_flags & SA_USERTRAMP)
162df8bae1dSRodney W. Grimes 		ps->ps_usertramp |= bit;
163df8bae1dSRodney W. Grimes 	else
164df8bae1dSRodney W. Grimes 		ps->ps_usertramp &= ~bit;
165df8bae1dSRodney W. Grimes #endif
166df8bae1dSRodney W. Grimes 	if (signum == SIGCHLD) {
167df8bae1dSRodney W. Grimes 		if (sa->sa_flags & SA_NOCLDSTOP)
168df8bae1dSRodney W. Grimes 			p->p_flag |= P_NOCLDSTOP;
169df8bae1dSRodney W. Grimes 		else
170df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_NOCLDSTOP;
171df8bae1dSRodney W. Grimes 	}
172df8bae1dSRodney W. Grimes 	/*
173df8bae1dSRodney W. Grimes 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
174df8bae1dSRodney W. Grimes 	 * and for signals set to SIG_DFL where the default is to ignore.
175df8bae1dSRodney W. Grimes 	 * However, don't put SIGCONT in p_sigignore,
176df8bae1dSRodney W. Grimes 	 * as we have to restart the process.
177df8bae1dSRodney W. Grimes 	 */
178df8bae1dSRodney W. Grimes 	if (sa->sa_handler == SIG_IGN ||
179df8bae1dSRodney W. Grimes 	    (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
180df8bae1dSRodney W. Grimes 		p->p_siglist &= ~bit;		/* never to be seen again */
181df8bae1dSRodney W. Grimes 		if (signum != SIGCONT)
182df8bae1dSRodney W. Grimes 			p->p_sigignore |= bit;	/* easier in psignal */
183df8bae1dSRodney W. Grimes 		p->p_sigcatch &= ~bit;
184df8bae1dSRodney W. Grimes 	} else {
185df8bae1dSRodney W. Grimes 		p->p_sigignore &= ~bit;
186df8bae1dSRodney W. Grimes 		if (sa->sa_handler == SIG_DFL)
187df8bae1dSRodney W. Grimes 			p->p_sigcatch &= ~bit;
188df8bae1dSRodney W. Grimes 		else
189df8bae1dSRodney W. Grimes 			p->p_sigcatch |= bit;
190df8bae1dSRodney W. Grimes 	}
191df8bae1dSRodney W. Grimes 	(void) spl0();
192df8bae1dSRodney W. Grimes }
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes /*
195df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
196df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
197df8bae1dSRodney W. Grimes  */
198df8bae1dSRodney W. Grimes void
199df8bae1dSRodney W. Grimes siginit(p)
200df8bae1dSRodney W. Grimes 	struct proc *p;
201df8bae1dSRodney W. Grimes {
202df8bae1dSRodney W. Grimes 	register int i;
203df8bae1dSRodney W. Grimes 
204df8bae1dSRodney W. Grimes 	for (i = 0; i < NSIG; i++)
205df8bae1dSRodney W. Grimes 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
206df8bae1dSRodney W. Grimes 			p->p_sigignore |= sigmask(i);
207df8bae1dSRodney W. Grimes }
208df8bae1dSRodney W. Grimes 
209df8bae1dSRodney W. Grimes /*
210df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
211df8bae1dSRodney W. Grimes  */
212df8bae1dSRodney W. Grimes void
213df8bae1dSRodney W. Grimes execsigs(p)
214df8bae1dSRodney W. Grimes 	register struct proc *p;
215df8bae1dSRodney W. Grimes {
216df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
217df8bae1dSRodney W. Grimes 	register int nc, mask;
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes 	/*
220df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
221df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
222df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
223df8bae1dSRodney W. Grimes 	 */
224df8bae1dSRodney W. Grimes 	while (p->p_sigcatch) {
225df8bae1dSRodney W. Grimes 		nc = ffs((long)p->p_sigcatch);
226df8bae1dSRodney W. Grimes 		mask = sigmask(nc);
227df8bae1dSRodney W. Grimes 		p->p_sigcatch &= ~mask;
228df8bae1dSRodney W. Grimes 		if (sigprop[nc] & SA_IGNORE) {
229df8bae1dSRodney W. Grimes 			if (nc != SIGCONT)
230df8bae1dSRodney W. Grimes 				p->p_sigignore |= mask;
231df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
232df8bae1dSRodney W. Grimes 		}
233df8bae1dSRodney W. Grimes 		ps->ps_sigact[nc] = SIG_DFL;
234df8bae1dSRodney W. Grimes 	}
235df8bae1dSRodney W. Grimes 	/*
236df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
237df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
238df8bae1dSRodney W. Grimes 	 */
239df8bae1dSRodney W. Grimes 	ps->ps_sigstk.ss_flags = SA_DISABLE;
240df8bae1dSRodney W. Grimes 	ps->ps_sigstk.ss_size = 0;
241aa98692fSAndreas Schulz 	ps->ps_sigstk.ss_sp = 0;
242df8bae1dSRodney W. Grimes 	ps->ps_flags = 0;
243df8bae1dSRodney W. Grimes }
244df8bae1dSRodney W. Grimes 
245df8bae1dSRodney W. Grimes /*
246df8bae1dSRodney W. Grimes  * Manipulate signal mask.
247df8bae1dSRodney W. Grimes  * Note that we receive new mask, not pointer,
248df8bae1dSRodney W. Grimes  * and return old mask as return value;
249df8bae1dSRodney W. Grimes  * the library stub does the rest.
250df8bae1dSRodney W. Grimes  */
251d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
252df8bae1dSRodney W. Grimes struct sigprocmask_args {
253df8bae1dSRodney W. Grimes 	int	how;
254df8bae1dSRodney W. Grimes 	sigset_t mask;
255df8bae1dSRodney W. Grimes };
256d2d3e875SBruce Evans #endif
25726f9a767SRodney W. Grimes int
258df8bae1dSRodney W. Grimes sigprocmask(p, uap, retval)
259df8bae1dSRodney W. Grimes 	register struct proc *p;
260df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
261df8bae1dSRodney W. Grimes 	int *retval;
262df8bae1dSRodney W. Grimes {
263df8bae1dSRodney W. Grimes 	int error = 0;
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes 	*retval = p->p_sigmask;
266df8bae1dSRodney W. Grimes 	(void) splhigh();
267df8bae1dSRodney W. Grimes 
268df8bae1dSRodney W. Grimes 	switch (uap->how) {
269df8bae1dSRodney W. Grimes 	case SIG_BLOCK:
270df8bae1dSRodney W. Grimes 		p->p_sigmask |= uap->mask &~ sigcantmask;
271df8bae1dSRodney W. Grimes 		break;
272df8bae1dSRodney W. Grimes 
273df8bae1dSRodney W. Grimes 	case SIG_UNBLOCK:
274df8bae1dSRodney W. Grimes 		p->p_sigmask &= ~uap->mask;
275df8bae1dSRodney W. Grimes 		break;
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes 	case SIG_SETMASK:
278df8bae1dSRodney W. Grimes 		p->p_sigmask = uap->mask &~ sigcantmask;
279df8bae1dSRodney W. Grimes 		break;
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes 	default:
282df8bae1dSRodney W. Grimes 		error = EINVAL;
283df8bae1dSRodney W. Grimes 		break;
284df8bae1dSRodney W. Grimes 	}
285df8bae1dSRodney W. Grimes 	(void) spl0();
286df8bae1dSRodney W. Grimes 	return (error);
287df8bae1dSRodney W. Grimes }
288df8bae1dSRodney W. Grimes 
289d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
290df8bae1dSRodney W. Grimes struct sigpending_args {
291df8bae1dSRodney W. Grimes 	int	dummy;
292df8bae1dSRodney W. Grimes };
293d2d3e875SBruce Evans #endif
294df8bae1dSRodney W. Grimes /* ARGSUSED */
29526f9a767SRodney W. Grimes int
296df8bae1dSRodney W. Grimes sigpending(p, uap, retval)
297df8bae1dSRodney W. Grimes 	struct proc *p;
298df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
299df8bae1dSRodney W. Grimes 	int *retval;
300df8bae1dSRodney W. Grimes {
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes 	*retval = p->p_siglist;
303df8bae1dSRodney W. Grimes 	return (0);
304df8bae1dSRodney W. Grimes }
305df8bae1dSRodney W. Grimes 
306df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
307df8bae1dSRodney W. Grimes /*
308df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
309df8bae1dSRodney W. Grimes  */
310d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
311df8bae1dSRodney W. Grimes struct osigvec_args {
312df8bae1dSRodney W. Grimes 	int	signum;
313df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
314df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
315df8bae1dSRodney W. Grimes };
316d2d3e875SBruce Evans #endif
317df8bae1dSRodney W. Grimes /* ARGSUSED */
31826f9a767SRodney W. Grimes int
319df8bae1dSRodney W. Grimes osigvec(p, uap, retval)
320df8bae1dSRodney W. Grimes 	struct proc *p;
321df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
322df8bae1dSRodney W. Grimes 	int *retval;
323df8bae1dSRodney W. Grimes {
324df8bae1dSRodney W. Grimes 	struct sigvec vec;
325df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
326df8bae1dSRodney W. Grimes 	register struct sigvec *sv;
327df8bae1dSRodney W. Grimes 	register int signum;
328df8bae1dSRodney W. Grimes 	int bit, error;
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 	signum = uap->signum;
331df8bae1dSRodney W. Grimes 	if (signum <= 0 || signum >= NSIG ||
332df8bae1dSRodney W. Grimes 	    signum == SIGKILL || signum == SIGSTOP)
333df8bae1dSRodney W. Grimes 		return (EINVAL);
334df8bae1dSRodney W. Grimes 	sv = &vec;
335df8bae1dSRodney W. Grimes 	if (uap->osv) {
336df8bae1dSRodney W. Grimes 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
337df8bae1dSRodney W. Grimes 		sv->sv_mask = ps->ps_catchmask[signum];
338df8bae1dSRodney W. Grimes 		bit = sigmask(signum);
339df8bae1dSRodney W. Grimes 		sv->sv_flags = 0;
340df8bae1dSRodney W. Grimes 		if ((ps->ps_sigonstack & bit) != 0)
341df8bae1dSRodney W. Grimes 			sv->sv_flags |= SV_ONSTACK;
342df8bae1dSRodney W. Grimes 		if ((ps->ps_sigintr & bit) != 0)
343df8bae1dSRodney W. Grimes 			sv->sv_flags |= SV_INTERRUPT;
344df8bae1dSRodney W. Grimes #ifndef COMPAT_SUNOS
345df8bae1dSRodney W. Grimes 		if (p->p_flag & P_NOCLDSTOP)
346df8bae1dSRodney W. Grimes 			sv->sv_flags |= SA_NOCLDSTOP;
347df8bae1dSRodney W. Grimes #endif
348bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv,
349bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
350df8bae1dSRodney W. Grimes 			return (error);
351df8bae1dSRodney W. Grimes 	}
352df8bae1dSRodney W. Grimes 	if (uap->nsv) {
353bb56ec4aSPoul-Henning Kamp 		if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
354bb56ec4aSPoul-Henning Kamp 		    sizeof (vec))))
355df8bae1dSRodney W. Grimes 			return (error);
356df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
357df8bae1dSRodney W. Grimes 		/*
358df8bae1dSRodney W. Grimes 		 * SunOS uses this bit (4, aka SA_DISABLE) as SV_RESETHAND,
359df8bae1dSRodney W. Grimes 		 * `reset to SIG_DFL on delivery'. We have no such option
360df8bae1dSRodney W. Grimes 		 * now or ever!
361df8bae1dSRodney W. Grimes 		 */
362df8bae1dSRodney W. Grimes 		if (sv->sv_flags & SA_DISABLE)
363df8bae1dSRodney W. Grimes 			return (EINVAL);
364df8bae1dSRodney W. Grimes 		sv->sv_flags |= SA_USERTRAMP;
365df8bae1dSRodney W. Grimes #endif
366df8bae1dSRodney W. Grimes 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
367df8bae1dSRodney W. Grimes 		setsigvec(p, signum, (struct sigaction *)sv);
368df8bae1dSRodney W. Grimes 	}
369df8bae1dSRodney W. Grimes 	return (0);
370df8bae1dSRodney W. Grimes }
371df8bae1dSRodney W. Grimes 
372d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
373df8bae1dSRodney W. Grimes struct osigblock_args {
374df8bae1dSRodney W. Grimes 	int	mask;
375df8bae1dSRodney W. Grimes };
376d2d3e875SBruce Evans #endif
37726f9a767SRodney W. Grimes int
378df8bae1dSRodney W. Grimes osigblock(p, uap, retval)
379df8bae1dSRodney W. Grimes 	register struct proc *p;
380df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
381df8bae1dSRodney W. Grimes 	int *retval;
382df8bae1dSRodney W. Grimes {
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 	(void) splhigh();
385df8bae1dSRodney W. Grimes 	*retval = p->p_sigmask;
386df8bae1dSRodney W. Grimes 	p->p_sigmask |= uap->mask &~ sigcantmask;
387df8bae1dSRodney W. Grimes 	(void) spl0();
388df8bae1dSRodney W. Grimes 	return (0);
389df8bae1dSRodney W. Grimes }
390df8bae1dSRodney W. Grimes 
391d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
392df8bae1dSRodney W. Grimes struct osigsetmask_args {
393df8bae1dSRodney W. Grimes 	int	mask;
394df8bae1dSRodney W. Grimes };
395d2d3e875SBruce Evans #endif
39626f9a767SRodney W. Grimes int
397df8bae1dSRodney W. Grimes osigsetmask(p, uap, retval)
398df8bae1dSRodney W. Grimes 	struct proc *p;
399df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
400df8bae1dSRodney W. Grimes 	int *retval;
401df8bae1dSRodney W. Grimes {
402df8bae1dSRodney W. Grimes 
403df8bae1dSRodney W. Grimes 	(void) splhigh();
404df8bae1dSRodney W. Grimes 	*retval = p->p_sigmask;
405df8bae1dSRodney W. Grimes 	p->p_sigmask = uap->mask &~ sigcantmask;
406df8bae1dSRodney W. Grimes 	(void) spl0();
407df8bae1dSRodney W. Grimes 	return (0);
408df8bae1dSRodney W. Grimes }
409df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes /*
412df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
413df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
414df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
415df8bae1dSRodney W. Grimes  */
416d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
417df8bae1dSRodney W. Grimes struct sigsuspend_args {
418df8bae1dSRodney W. Grimes 	sigset_t mask;
419df8bae1dSRodney W. Grimes };
420d2d3e875SBruce Evans #endif
421df8bae1dSRodney W. Grimes /* ARGSUSED */
42226f9a767SRodney W. Grimes int
423df8bae1dSRodney W. Grimes sigsuspend(p, uap, retval)
424df8bae1dSRodney W. Grimes 	register struct proc *p;
425df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
426df8bae1dSRodney W. Grimes 	int *retval;
427df8bae1dSRodney W. Grimes {
428df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 	/*
431df8bae1dSRodney W. Grimes 	 * When returning from sigpause, we want
432df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
433df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
434df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
435df8bae1dSRodney W. Grimes 	 * to indicate this.
436df8bae1dSRodney W. Grimes 	 */
437df8bae1dSRodney W. Grimes 	ps->ps_oldmask = p->p_sigmask;
438df8bae1dSRodney W. Grimes 	ps->ps_flags |= SAS_OLDMASK;
439df8bae1dSRodney W. Grimes 	p->p_sigmask = uap->mask &~ sigcantmask;
440df8bae1dSRodney W. Grimes 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
441df8bae1dSRodney W. Grimes 		/* void */;
442df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
443df8bae1dSRodney W. Grimes 	return (EINTR);
444df8bae1dSRodney W. Grimes }
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
447d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
448df8bae1dSRodney W. Grimes struct osigstack_args {
449df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
450df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
451df8bae1dSRodney W. Grimes };
452d2d3e875SBruce Evans #endif
453df8bae1dSRodney W. Grimes /* ARGSUSED */
45426f9a767SRodney W. Grimes int
455df8bae1dSRodney W. Grimes osigstack(p, uap, retval)
456df8bae1dSRodney W. Grimes 	struct proc *p;
457df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
458df8bae1dSRodney W. Grimes 	int *retval;
459df8bae1dSRodney W. Grimes {
460df8bae1dSRodney W. Grimes 	struct sigstack ss;
461df8bae1dSRodney W. Grimes 	struct sigacts *psp;
462df8bae1dSRodney W. Grimes 	int error = 0;
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes 	psp = p->p_sigacts;
465aa98692fSAndreas Schulz 	ss.ss_sp = psp->ps_sigstk.ss_sp;
466df8bae1dSRodney W. Grimes 	ss.ss_onstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
467df8bae1dSRodney W. Grimes 	if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
468df8bae1dSRodney W. Grimes 	    sizeof (struct sigstack))))
469df8bae1dSRodney W. Grimes 		return (error);
470df8bae1dSRodney W. Grimes 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
471df8bae1dSRodney W. Grimes 	    sizeof (ss))) == 0) {
472aa98692fSAndreas Schulz 		psp->ps_sigstk.ss_sp = ss.ss_sp;
473df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_size = 0;
474df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_flags |= ss.ss_onstack & SA_ONSTACK;
475df8bae1dSRodney W. Grimes 		psp->ps_flags |= SAS_ALTSTACK;
476df8bae1dSRodney W. Grimes 	}
477df8bae1dSRodney W. Grimes 	return (error);
478df8bae1dSRodney W. Grimes }
479df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
480df8bae1dSRodney W. Grimes 
481d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
482df8bae1dSRodney W. Grimes struct sigaltstack_args {
483df8bae1dSRodney W. Grimes 	struct	sigaltstack *nss;
484df8bae1dSRodney W. Grimes 	struct	sigaltstack *oss;
485df8bae1dSRodney W. Grimes };
486d2d3e875SBruce Evans #endif
487df8bae1dSRodney W. Grimes /* ARGSUSED */
48826f9a767SRodney W. Grimes int
489df8bae1dSRodney W. Grimes sigaltstack(p, uap, retval)
490df8bae1dSRodney W. Grimes 	struct proc *p;
491df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
492df8bae1dSRodney W. Grimes 	int *retval;
493df8bae1dSRodney W. Grimes {
494df8bae1dSRodney W. Grimes 	struct sigacts *psp;
495df8bae1dSRodney W. Grimes 	struct sigaltstack ss;
496df8bae1dSRodney W. Grimes 	int error;
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 	psp = p->p_sigacts;
499df8bae1dSRodney W. Grimes 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
500df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_flags |= SA_DISABLE;
501df8bae1dSRodney W. Grimes 	if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
502df8bae1dSRodney W. Grimes 	    (caddr_t)uap->oss, sizeof (struct sigaltstack))))
503df8bae1dSRodney W. Grimes 		return (error);
504df8bae1dSRodney W. Grimes 	if (uap->nss == 0)
505df8bae1dSRodney W. Grimes 		return (0);
506bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss))))
507df8bae1dSRodney W. Grimes 		return (error);
508df8bae1dSRodney W. Grimes 	if (ss.ss_flags & SA_DISABLE) {
509df8bae1dSRodney W. Grimes 		if (psp->ps_sigstk.ss_flags & SA_ONSTACK)
510df8bae1dSRodney W. Grimes 			return (EINVAL);
511df8bae1dSRodney W. Grimes 		psp->ps_flags &= ~SAS_ALTSTACK;
512df8bae1dSRodney W. Grimes 		psp->ps_sigstk.ss_flags = ss.ss_flags;
513df8bae1dSRodney W. Grimes 		return (0);
514df8bae1dSRodney W. Grimes 	}
515df8bae1dSRodney W. Grimes 	if (ss.ss_size < MINSIGSTKSZ)
516df8bae1dSRodney W. Grimes 		return (ENOMEM);
517df8bae1dSRodney W. Grimes 	psp->ps_flags |= SAS_ALTSTACK;
518df8bae1dSRodney W. Grimes 	psp->ps_sigstk= ss;
519df8bae1dSRodney W. Grimes 	return (0);
520df8bae1dSRodney W. Grimes }
521df8bae1dSRodney W. Grimes 
522d93f860cSPoul-Henning Kamp /*
523d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
524d93f860cSPoul-Henning Kamp  * cp is calling process.
525d93f860cSPoul-Henning Kamp  */
526d93f860cSPoul-Henning Kamp int
527d93f860cSPoul-Henning Kamp killpg1(cp, signum, pgid, all)
528d93f860cSPoul-Henning Kamp 	register struct proc *cp;
529d93f860cSPoul-Henning Kamp 	int signum, pgid, all;
530d93f860cSPoul-Henning Kamp {
531d93f860cSPoul-Henning Kamp 	register struct proc *p;
532d93f860cSPoul-Henning Kamp 	register struct pcred *pc = cp->p_cred;
533d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
534d93f860cSPoul-Henning Kamp 	int nfound = 0;
535d93f860cSPoul-Henning Kamp 
536d93f860cSPoul-Henning Kamp 	if (all)
537d93f860cSPoul-Henning Kamp 		/*
538d93f860cSPoul-Henning Kamp 		 * broadcast
539d93f860cSPoul-Henning Kamp 		 */
540d93f860cSPoul-Henning Kamp 		for (p = (struct proc *)allproc; p != NULL; p = p->p_next) {
541d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
542d93f860cSPoul-Henning Kamp 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
543d93f860cSPoul-Henning Kamp 				continue;
544d93f860cSPoul-Henning Kamp 			nfound++;
545d93f860cSPoul-Henning Kamp 			if (signum)
546d93f860cSPoul-Henning Kamp 				psignal(p, signum);
547d93f860cSPoul-Henning Kamp 		}
548d93f860cSPoul-Henning Kamp 	else {
549d93f860cSPoul-Henning Kamp 		if (pgid == 0)
550d93f860cSPoul-Henning Kamp 			/*
551d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
552d93f860cSPoul-Henning Kamp 			 */
553d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
554d93f860cSPoul-Henning Kamp 		else {
555d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
556d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
557d93f860cSPoul-Henning Kamp 				return (ESRCH);
558d93f860cSPoul-Henning Kamp 		}
559d93f860cSPoul-Henning Kamp 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
560d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
561d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
562d93f860cSPoul-Henning Kamp 			    !CANSIGNAL(cp, pc, p, signum))
563d93f860cSPoul-Henning Kamp 				continue;
564d93f860cSPoul-Henning Kamp 			nfound++;
565d93f860cSPoul-Henning Kamp 			if (signum)
566d93f860cSPoul-Henning Kamp 				psignal(p, signum);
567d93f860cSPoul-Henning Kamp 		}
568d93f860cSPoul-Henning Kamp 	}
569d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
570d93f860cSPoul-Henning Kamp }
571d93f860cSPoul-Henning Kamp 
572d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
573df8bae1dSRodney W. Grimes struct kill_args {
574df8bae1dSRodney W. Grimes 	int	pid;
575df8bae1dSRodney W. Grimes 	int	signum;
576df8bae1dSRodney W. Grimes };
577d2d3e875SBruce Evans #endif
578df8bae1dSRodney W. Grimes /* ARGSUSED */
57926f9a767SRodney W. Grimes int
580df8bae1dSRodney W. Grimes kill(cp, uap, retval)
581df8bae1dSRodney W. Grimes 	register struct proc *cp;
582df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
583df8bae1dSRodney W. Grimes 	int *retval;
584df8bae1dSRodney W. Grimes {
585df8bae1dSRodney W. Grimes 	register struct proc *p;
586df8bae1dSRodney W. Grimes 	register struct pcred *pc = cp->p_cred;
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 	if ((u_int)uap->signum >= NSIG)
589df8bae1dSRodney W. Grimes 		return (EINVAL);
590df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
591df8bae1dSRodney W. Grimes 		/* kill single process */
592df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
593df8bae1dSRodney W. Grimes 			return (ESRCH);
594df8bae1dSRodney W. Grimes 		if (!CANSIGNAL(cp, pc, p, uap->signum))
595df8bae1dSRodney W. Grimes 			return (EPERM);
596df8bae1dSRodney W. Grimes 		if (uap->signum)
597df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
598df8bae1dSRodney W. Grimes 		return (0);
599df8bae1dSRodney W. Grimes 	}
600df8bae1dSRodney W. Grimes 	switch (uap->pid) {
601df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
602df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
603df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
604df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
605df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
606df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
607df8bae1dSRodney W. Grimes 	}
608df8bae1dSRodney W. Grimes 	/* NOTREACHED */
609df8bae1dSRodney W. Grimes }
610df8bae1dSRodney W. Grimes 
611df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
612d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
613df8bae1dSRodney W. Grimes struct okillpg_args {
614df8bae1dSRodney W. Grimes 	int	pgid;
615df8bae1dSRodney W. Grimes 	int	signum;
616df8bae1dSRodney W. Grimes };
617d2d3e875SBruce Evans #endif
618df8bae1dSRodney W. Grimes /* ARGSUSED */
61926f9a767SRodney W. Grimes int
620df8bae1dSRodney W. Grimes okillpg(p, uap, retval)
621df8bae1dSRodney W. Grimes 	struct proc *p;
622df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
623df8bae1dSRodney W. Grimes 	int *retval;
624df8bae1dSRodney W. Grimes {
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes 	if ((u_int)uap->signum >= NSIG)
627df8bae1dSRodney W. Grimes 		return (EINVAL);
628df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
629df8bae1dSRodney W. Grimes }
630df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
631df8bae1dSRodney W. Grimes 
632df8bae1dSRodney W. Grimes /*
633df8bae1dSRodney W. Grimes  * Send a signal to a process group.
634df8bae1dSRodney W. Grimes  */
635df8bae1dSRodney W. Grimes void
636df8bae1dSRodney W. Grimes gsignal(pgid, signum)
637df8bae1dSRodney W. Grimes 	int pgid, signum;
638df8bae1dSRodney W. Grimes {
639df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
642df8bae1dSRodney W. Grimes 		pgsignal(pgrp, signum, 0);
643df8bae1dSRodney W. Grimes }
644df8bae1dSRodney W. Grimes 
645df8bae1dSRodney W. Grimes /*
646df8bae1dSRodney W. Grimes  * Send a signal to a  process group.  If checktty is 1,
647df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
648df8bae1dSRodney W. Grimes  */
649df8bae1dSRodney W. Grimes void
650df8bae1dSRodney W. Grimes pgsignal(pgrp, signum, checkctty)
651df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
652df8bae1dSRodney W. Grimes 	int signum, checkctty;
653df8bae1dSRodney W. Grimes {
654df8bae1dSRodney W. Grimes 	register struct proc *p;
655df8bae1dSRodney W. Grimes 
656df8bae1dSRodney W. Grimes 	if (pgrp)
657df8bae1dSRodney W. Grimes 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
658df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
659df8bae1dSRodney W. Grimes 				psignal(p, signum);
660df8bae1dSRodney W. Grimes }
661df8bae1dSRodney W. Grimes 
662df8bae1dSRodney W. Grimes /*
663df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
664df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
665df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
666df8bae1dSRodney W. Grimes  */
667df8bae1dSRodney W. Grimes void
668df8bae1dSRodney W. Grimes trapsignal(p, signum, code)
669df8bae1dSRodney W. Grimes 	struct proc *p;
670df8bae1dSRodney W. Grimes 	register int signum;
671df8bae1dSRodney W. Grimes 	u_int code;
672df8bae1dSRodney W. Grimes {
673df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
674df8bae1dSRodney W. Grimes 	int mask;
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
677df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
678df8bae1dSRodney W. Grimes 	    (p->p_sigmask & mask) == 0) {
679df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
680df8bae1dSRodney W. Grimes #ifdef KTRACE
681df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
682df8bae1dSRodney W. Grimes 			ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
683df8bae1dSRodney W. Grimes 				p->p_sigmask, code);
684df8bae1dSRodney W. Grimes #endif
685df8bae1dSRodney W. Grimes 		sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, code);
6861e41c1b5SSteven Wallace 		p->p_sigmask |= ps->ps_catchmask[signum];
6871e41c1b5SSteven Wallace 		p->p_sigmask |= ps->ps_catchmask[signum] |
6881e41c1b5SSteven Wallace 			(mask & ~ps->ps_nodefer);
689df8bae1dSRodney W. Grimes 	} else {
690df8bae1dSRodney W. Grimes 		ps->ps_code = code;	/* XXX for core dump/debugger */
691df8bae1dSRodney W. Grimes 		psignal(p, signum);
692df8bae1dSRodney W. Grimes 	}
693df8bae1dSRodney W. Grimes }
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes /*
696df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
697df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
698df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
699df8bae1dSRodney W. Grimes  *
700df8bae1dSRodney W. Grimes  * Exceptions:
701df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
702df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
703df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
704df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
705df8bae1dSRodney W. Grimes  *
706df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
707df8bae1dSRodney W. Grimes  */
708df8bae1dSRodney W. Grimes void
709df8bae1dSRodney W. Grimes psignal(p, signum)
710df8bae1dSRodney W. Grimes 	register struct proc *p;
711df8bae1dSRodney W. Grimes 	register int signum;
712df8bae1dSRodney W. Grimes {
713df8bae1dSRodney W. Grimes 	register int s, prop;
714df8bae1dSRodney W. Grimes 	register sig_t action;
715df8bae1dSRodney W. Grimes 	int mask;
716df8bae1dSRodney W. Grimes 
717df8bae1dSRodney W. Grimes 	if ((u_int)signum >= NSIG || signum == 0)
718df8bae1dSRodney W. Grimes 		panic("psignal signal number");
719df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
720df8bae1dSRodney W. Grimes 	prop = sigprop[signum];
721df8bae1dSRodney W. Grimes 
722df8bae1dSRodney W. Grimes 	/*
723df8bae1dSRodney W. Grimes 	 * If proc is traced, always give parent a chance.
724df8bae1dSRodney W. Grimes 	 */
725df8bae1dSRodney W. Grimes 	if (p->p_flag & P_TRACED)
726df8bae1dSRodney W. Grimes 		action = SIG_DFL;
727df8bae1dSRodney W. Grimes 	else {
728df8bae1dSRodney W. Grimes 		/*
729df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
730df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
731df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
732df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
733df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
734df8bae1dSRodney W. Grimes 		 */
735df8bae1dSRodney W. Grimes 		if (p->p_sigignore & mask)
736df8bae1dSRodney W. Grimes 			return;
737df8bae1dSRodney W. Grimes 		if (p->p_sigmask & mask)
738df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
739df8bae1dSRodney W. Grimes 		else if (p->p_sigcatch & mask)
740df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
741df8bae1dSRodney W. Grimes 		else
742df8bae1dSRodney W. Grimes 			action = SIG_DFL;
743df8bae1dSRodney W. Grimes 	}
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
746df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
747df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
748df8bae1dSRodney W. Grimes 
749df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
750df8bae1dSRodney W. Grimes 		p->p_siglist &= ~stopsigmask;
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
753df8bae1dSRodney W. Grimes 		/*
754df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
755df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
756df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
757df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
758df8bae1dSRodney W. Grimes 		 */
759df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
760df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
761df8bae1dSRodney W. Grimes 		        return;
762df8bae1dSRodney W. Grimes 		p->p_siglist &= ~contsigmask;
763df8bae1dSRodney W. Grimes 	}
764df8bae1dSRodney W. Grimes 	p->p_siglist |= mask;
765df8bae1dSRodney W. Grimes 
766df8bae1dSRodney W. Grimes 	/*
767df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
768df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
769df8bae1dSRodney W. Grimes 	 */
770df8bae1dSRodney W. Grimes 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
771df8bae1dSRodney W. Grimes 		return;
772df8bae1dSRodney W. Grimes 	s = splhigh();
773df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
774df8bae1dSRodney W. Grimes 
775df8bae1dSRodney W. Grimes 	case SSLEEP:
776df8bae1dSRodney W. Grimes 		/*
777df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
778df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
779df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
780df8bae1dSRodney W. Grimes 		 * trap() or syscall().
781df8bae1dSRodney W. Grimes 		 */
782df8bae1dSRodney W. Grimes 		if ((p->p_flag & P_SINTR) == 0)
783df8bae1dSRodney W. Grimes 			goto out;
784df8bae1dSRodney W. Grimes 		/*
785df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
786df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
787df8bae1dSRodney W. Grimes 		 * for the parent.
788df8bae1dSRodney W. Grimes 		 */
789df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
790df8bae1dSRodney W. Grimes 			goto run;
791df8bae1dSRodney W. Grimes 		/*
792df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
793df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
794df8bae1dSRodney W. Grimes 		 * be awakened.
795df8bae1dSRodney W. Grimes 		 */
796df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
797df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
798df8bae1dSRodney W. Grimes 			goto out;
799df8bae1dSRodney W. Grimes 		}
800df8bae1dSRodney W. Grimes 		/*
801df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
802df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
803df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
804df8bae1dSRodney W. Grimes 		 * cause the process to run.
805df8bae1dSRodney W. Grimes 		 */
806df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
807df8bae1dSRodney W. Grimes 			if (action != SIG_DFL)
808df8bae1dSRodney W. Grimes 				goto runfast;
809df8bae1dSRodney W. Grimes 			/*
810df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
811df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
812df8bae1dSRodney W. Grimes 			 */
813df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
814df8bae1dSRodney W. Grimes 				goto out;
815df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
816df8bae1dSRodney W. Grimes 			p->p_xstat = signum;
817df8bae1dSRodney W. Grimes 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
818df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
819df8bae1dSRodney W. Grimes 			stop(p);
820df8bae1dSRodney W. Grimes 			goto out;
821df8bae1dSRodney W. Grimes 		} else
822df8bae1dSRodney W. Grimes 			goto runfast;
823df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
824df8bae1dSRodney W. Grimes 
825df8bae1dSRodney W. Grimes 	case SSTOP:
826df8bae1dSRodney W. Grimes 		/*
827df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
828df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
829df8bae1dSRodney W. Grimes 		 */
830df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
831df8bae1dSRodney W. Grimes 			goto out;
832df8bae1dSRodney W. Grimes 
833df8bae1dSRodney W. Grimes 		/*
834df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
835df8bae1dSRodney W. Grimes 		 */
836df8bae1dSRodney W. Grimes 		if (signum == SIGKILL)
837df8bae1dSRodney W. Grimes 			goto runfast;
838df8bae1dSRodney W. Grimes 
839df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
840df8bae1dSRodney W. Grimes 			/*
841df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
842df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
843df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
844df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
845df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
846df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
847df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
848df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
849df8bae1dSRodney W. Grimes 			 */
850df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
851df8bae1dSRodney W. Grimes 				p->p_siglist &= ~mask;
852df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
853df8bae1dSRodney W. Grimes 				goto runfast;
854df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
855df8bae1dSRodney W. Grimes 				goto run;
856df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
857df8bae1dSRodney W. Grimes 			goto out;
858df8bae1dSRodney W. Grimes 		}
859df8bae1dSRodney W. Grimes 
860df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
861df8bae1dSRodney W. Grimes 			/*
862df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
863df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
864df8bae1dSRodney W. Grimes 			 */
865df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;		/* take it away */
866df8bae1dSRodney W. Grimes 			goto out;
867df8bae1dSRodney W. Grimes 		}
868df8bae1dSRodney W. Grimes 
869df8bae1dSRodney W. Grimes 		/*
870df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
871df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
872df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
873df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
874df8bae1dSRodney W. Grimes 		 */
875df8bae1dSRodney W. Grimes 		if (p->p_wchan && p->p_flag & P_SINTR)
876df8bae1dSRodney W. Grimes 			unsleep(p);
877df8bae1dSRodney W. Grimes 		goto out;
878df8bae1dSRodney W. Grimes 
879df8bae1dSRodney W. Grimes 	default:
880df8bae1dSRodney W. Grimes 		/*
881df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
882df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
883df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
884df8bae1dSRodney W. Grimes 		 */
885df8bae1dSRodney W. Grimes 		if (p == curproc)
886df8bae1dSRodney W. Grimes 			signotify(p);
887df8bae1dSRodney W. Grimes 		goto out;
888df8bae1dSRodney W. Grimes 	}
889df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
890df8bae1dSRodney W. Grimes 
891df8bae1dSRodney W. Grimes runfast:
892df8bae1dSRodney W. Grimes 	/*
893df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
894df8bae1dSRodney W. Grimes 	 */
895df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
896df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
897df8bae1dSRodney W. Grimes run:
898df8bae1dSRodney W. Grimes 	setrunnable(p);
899df8bae1dSRodney W. Grimes out:
900df8bae1dSRodney W. Grimes 	splx(s);
901df8bae1dSRodney W. Grimes }
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes /*
904df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
905df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
906df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
907df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
908df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
909df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
910df8bae1dSRodney W. Grimes  * sequence is
911df8bae1dSRodney W. Grimes  *
912df8bae1dSRodney W. Grimes  *	while (signum = CURSIG(curproc))
913df8bae1dSRodney W. Grimes  *		postsig(signum);
914df8bae1dSRodney W. Grimes  */
91526f9a767SRodney W. Grimes int
916df8bae1dSRodney W. Grimes issignal(p)
917df8bae1dSRodney W. Grimes 	register struct proc *p;
918df8bae1dSRodney W. Grimes {
919df8bae1dSRodney W. Grimes 	register int signum, mask, prop;
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes 	for (;;) {
922df8bae1dSRodney W. Grimes 		mask = p->p_siglist & ~p->p_sigmask;
923df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
924df8bae1dSRodney W. Grimes 			mask &= ~stopsigmask;
925df8bae1dSRodney W. Grimes 		if (mask == 0)	 	/* no signal to send */
926df8bae1dSRodney W. Grimes 			return (0);
927df8bae1dSRodney W. Grimes 		signum = ffs((long)mask);
928df8bae1dSRodney W. Grimes 		mask = sigmask(signum);
929df8bae1dSRodney W. Grimes 		prop = sigprop[signum];
930df8bae1dSRodney W. Grimes 		/*
931df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
932df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
933df8bae1dSRodney W. Grimes 		 */
934df8bae1dSRodney W. Grimes 		if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) {
935df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;
936df8bae1dSRodney W. Grimes 			continue;
937df8bae1dSRodney W. Grimes 		}
938df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
939df8bae1dSRodney W. Grimes 			/*
940df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
941df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
942df8bae1dSRodney W. Grimes 			 */
943df8bae1dSRodney W. Grimes 			p->p_xstat = signum;
944df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
945df8bae1dSRodney W. Grimes 			do {
946df8bae1dSRodney W. Grimes 				stop(p);
947df8bae1dSRodney W. Grimes 				mi_switch();
948df8bae1dSRodney W. Grimes 			} while (!trace_req(p) && p->p_flag & P_TRACED);
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 			/*
951df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
952df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
953df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
954df8bae1dSRodney W. Grimes 			 */
955df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
956df8bae1dSRodney W. Grimes 				continue;
957df8bae1dSRodney W. Grimes 
958df8bae1dSRodney W. Grimes 			/*
959df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
960df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
961df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
962df8bae1dSRodney W. Grimes 			 */
963df8bae1dSRodney W. Grimes 			p->p_siglist &= ~mask;	/* clear the old signal */
964df8bae1dSRodney W. Grimes 			signum = p->p_xstat;
965df8bae1dSRodney W. Grimes 			if (signum == 0)
966df8bae1dSRodney W. Grimes 				continue;
967df8bae1dSRodney W. Grimes 
968df8bae1dSRodney W. Grimes 			/*
969df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
970df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
971df8bae1dSRodney W. Grimes 			 */
972df8bae1dSRodney W. Grimes 			mask = sigmask(signum);
973df8bae1dSRodney W. Grimes 			p->p_siglist |= mask;
974df8bae1dSRodney W. Grimes 			if (p->p_sigmask & mask)
975df8bae1dSRodney W. Grimes 				continue;
976df8bae1dSRodney W. Grimes 		}
977df8bae1dSRodney W. Grimes 
978df8bae1dSRodney W. Grimes 		/*
979df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
980df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
981df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
982df8bae1dSRodney W. Grimes 		 */
983df8bae1dSRodney W. Grimes 		switch ((int)p->p_sigacts->ps_sigact[signum]) {
984df8bae1dSRodney W. Grimes 
9850b53fbe8SBruce Evans 		case (int)SIG_DFL:
986df8bae1dSRodney W. Grimes 			/*
987df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
988df8bae1dSRodney W. Grimes 			 */
989df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
990df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
991df8bae1dSRodney W. Grimes 				/*
992df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
993df8bae1dSRodney W. Grimes 				 * in init? XXX
994df8bae1dSRodney W. Grimes 				 */
995d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
996d93f860cSPoul-Henning Kamp 					(u_long)p->p_pid, signum);
997df8bae1dSRodney W. Grimes #endif
998df8bae1dSRodney W. Grimes 				break;		/* == ignore */
999df8bae1dSRodney W. Grimes 			}
1000df8bae1dSRodney W. Grimes 			/*
1001df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1002df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1003df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1004df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1005df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1006df8bae1dSRodney W. Grimes 			 */
1007df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1008df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1009df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1010df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1011df8bae1dSRodney W. Grimes 					break;	/* == ignore */
1012df8bae1dSRodney W. Grimes 				p->p_xstat = signum;
1013df8bae1dSRodney W. Grimes 				stop(p);
1014df8bae1dSRodney W. Grimes 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1015df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1016df8bae1dSRodney W. Grimes 				mi_switch();
1017df8bae1dSRodney W. Grimes 				break;
1018df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1019df8bae1dSRodney W. Grimes 				/*
1020df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1021df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1022df8bae1dSRodney W. Grimes 				 */
1023df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1024df8bae1dSRodney W. Grimes 			} else
1025df8bae1dSRodney W. Grimes 				return (signum);
1026df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1027df8bae1dSRodney W. Grimes 
10280b53fbe8SBruce Evans 		case (int)SIG_IGN:
1029df8bae1dSRodney W. Grimes 			/*
1030df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1031df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1032df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1033df8bae1dSRodney W. Grimes 			 */
1034df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1035df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1036df8bae1dSRodney W. Grimes 				printf("issignal\n");
1037df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1038df8bae1dSRodney W. Grimes 
1039df8bae1dSRodney W. Grimes 		default:
1040df8bae1dSRodney W. Grimes 			/*
1041df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1042df8bae1dSRodney W. Grimes 			 * postsig() process it.
1043df8bae1dSRodney W. Grimes 			 */
1044df8bae1dSRodney W. Grimes 			return (signum);
1045df8bae1dSRodney W. Grimes 		}
1046df8bae1dSRodney W. Grimes 		p->p_siglist &= ~mask;		/* take the signal! */
1047df8bae1dSRodney W. Grimes 	}
1048df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1049df8bae1dSRodney W. Grimes }
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes /*
1052df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1053df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
1054df8bae1dSRodney W. Grimes  * on the run queue.
1055df8bae1dSRodney W. Grimes  */
105626f9a767SRodney W. Grimes void
1057df8bae1dSRodney W. Grimes stop(p)
1058df8bae1dSRodney W. Grimes 	register struct proc *p;
1059df8bae1dSRodney W. Grimes {
1060df8bae1dSRodney W. Grimes 
1061df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1062df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1063df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1064df8bae1dSRodney W. Grimes }
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes /*
1067df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1068df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1069df8bae1dSRodney W. Grimes  */
1070df8bae1dSRodney W. Grimes void
1071df8bae1dSRodney W. Grimes postsig(signum)
1072df8bae1dSRodney W. Grimes 	register int signum;
1073df8bae1dSRodney W. Grimes {
1074df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
1075df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1076df8bae1dSRodney W. Grimes 	register sig_t action;
1077df8bae1dSRodney W. Grimes 	int code, mask, returnmask;
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1080df8bae1dSRodney W. Grimes 	if (signum == 0)
1081df8bae1dSRodney W. Grimes 		panic("postsig");
1082df8bae1dSRodney W. Grimes #endif
1083df8bae1dSRodney W. Grimes 	mask = sigmask(signum);
1084df8bae1dSRodney W. Grimes 	p->p_siglist &= ~mask;
1085df8bae1dSRodney W. Grimes 	action = ps->ps_sigact[signum];
1086df8bae1dSRodney W. Grimes #ifdef KTRACE
1087df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1088df8bae1dSRodney W. Grimes 		ktrpsig(p->p_tracep,
1089df8bae1dSRodney W. Grimes 		    signum, action, ps->ps_flags & SAS_OLDMASK ?
1090df8bae1dSRodney W. Grimes 		    ps->ps_oldmask : p->p_sigmask, 0);
1091df8bae1dSRodney W. Grimes #endif
1092df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1093df8bae1dSRodney W. Grimes 		/*
1094df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1095df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1096df8bae1dSRodney W. Grimes 		 */
1097df8bae1dSRodney W. Grimes 		sigexit(p, signum);
1098df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1099df8bae1dSRodney W. Grimes 	} else {
1100df8bae1dSRodney W. Grimes 		/*
1101df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1102df8bae1dSRodney W. Grimes 		 */
1103df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1104df8bae1dSRodney W. Grimes 		if (action == SIG_IGN || (p->p_sigmask & mask))
1105df8bae1dSRodney W. Grimes 			panic("postsig action");
1106df8bae1dSRodney W. Grimes #endif
1107df8bae1dSRodney W. Grimes 		/*
1108df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1109df8bae1dSRodney W. Grimes 		 * occurences of this signal.
1110df8bae1dSRodney W. Grimes 		 *
1111df8bae1dSRodney W. Grimes 		 * Special case: user has done a sigpause.  Here the
1112df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1113df8bae1dSRodney W. Grimes 		 * mask from before the sigpause is what we want
1114df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1115df8bae1dSRodney W. Grimes 		 */
1116df8bae1dSRodney W. Grimes 		(void) splhigh();
1117df8bae1dSRodney W. Grimes 		if (ps->ps_flags & SAS_OLDMASK) {
1118df8bae1dSRodney W. Grimes 			returnmask = ps->ps_oldmask;
1119df8bae1dSRodney W. Grimes 			ps->ps_flags &= ~SAS_OLDMASK;
1120df8bae1dSRodney W. Grimes 		} else
1121df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
11221e41c1b5SSteven Wallace 		p->p_sigmask |= ps->ps_catchmask[signum] |
11231e41c1b5SSteven Wallace 			(mask & ~ps->ps_nodefer);
1124df8bae1dSRodney W. Grimes 		(void) spl0();
1125df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1126df8bae1dSRodney W. Grimes 		if (ps->ps_sig != signum) {
1127df8bae1dSRodney W. Grimes 			code = 0;
1128df8bae1dSRodney W. Grimes 		} else {
1129df8bae1dSRodney W. Grimes 			code = ps->ps_code;
1130df8bae1dSRodney W. Grimes 			ps->ps_code = 0;
1131df8bae1dSRodney W. Grimes 		}
1132df8bae1dSRodney W. Grimes 		sendsig(action, signum, returnmask, code);
1133df8bae1dSRodney W. Grimes 	}
1134df8bae1dSRodney W. Grimes }
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes /*
1137df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1138df8bae1dSRodney W. Grimes  */
113926f9a767SRodney W. Grimes void
1140df8bae1dSRodney W. Grimes killproc(p, why)
1141df8bae1dSRodney W. Grimes 	struct proc *p;
1142df8bae1dSRodney W. Grimes 	char *why;
1143df8bae1dSRodney W. Grimes {
1144df8bae1dSRodney W. Grimes 
1145df8bae1dSRodney W. Grimes 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1146df8bae1dSRodney W. Grimes 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1147df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1148df8bae1dSRodney W. Grimes }
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes /*
1151df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1152df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1153df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1154df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1155df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1156df8bae1dSRodney W. Grimes  * does not return.
1157df8bae1dSRodney W. Grimes  */
115826f9a767SRodney W. Grimes void
1159df8bae1dSRodney W. Grimes sigexit(p, signum)
1160df8bae1dSRodney W. Grimes 	register struct proc *p;
1161df8bae1dSRodney W. Grimes 	int signum;
1162df8bae1dSRodney W. Grimes {
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
1165df8bae1dSRodney W. Grimes 	if (sigprop[signum] & SA_CORE) {
1166df8bae1dSRodney W. Grimes 		p->p_sigacts->ps_sig = signum;
1167c364e17eSAndrey A. Chernov 		/*
1168c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1169c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1170c364e17eSAndrey A. Chernov 		 * these messages.)
1171c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1172c364e17eSAndrey A. Chernov 		 */
1173c364e17eSAndrey A. Chernov 		log(LOG_INFO, "pid %d: %s: uid %d: exited on signal %d\n",
1174c364e17eSAndrey A. Chernov 			p->p_pid, p->p_comm, p->p_ucred->cr_uid, signum);
1175df8bae1dSRodney W. Grimes 		if (coredump(p) == 0)
1176df8bae1dSRodney W. Grimes 			signum |= WCOREFLAG;
1177df8bae1dSRodney W. Grimes 	}
1178df8bae1dSRodney W. Grimes 	exit1(p, W_EXITCODE(0, signum));
1179df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1180df8bae1dSRodney W. Grimes }
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes /*
1183df8bae1dSRodney W. Grimes  * Dump core, into a file named "progname.core", unless the process was
1184df8bae1dSRodney W. Grimes  * setuid/setgid.
1185df8bae1dSRodney W. Grimes  */
118626f9a767SRodney W. Grimes int
1187df8bae1dSRodney W. Grimes coredump(p)
1188df8bae1dSRodney W. Grimes 	register struct proc *p;
1189df8bae1dSRodney W. Grimes {
1190df8bae1dSRodney W. Grimes 	register struct vnode *vp;
1191df8bae1dSRodney W. Grimes 	register struct pcred *pcred = p->p_cred;
1192df8bae1dSRodney W. Grimes 	register struct ucred *cred = pcred->pc_ucred;
1193df8bae1dSRodney W. Grimes 	register struct vmspace *vm = p->p_vmspace;
1194df8bae1dSRodney W. Grimes 	struct nameidata nd;
1195df8bae1dSRodney W. Grimes 	struct vattr vattr;
1196df8bae1dSRodney W. Grimes 	int error, error1;
1197df8bae1dSRodney W. Grimes 	char name[MAXCOMLEN+6];		/* progname.core */
1198df8bae1dSRodney W. Grimes 
1199df8bae1dSRodney W. Grimes 	if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid)
1200df8bae1dSRodney W. Grimes 		return (EFAULT);
1201df8bae1dSRodney W. Grimes 	if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
1202df8bae1dSRodney W. Grimes 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1203df8bae1dSRodney W. Grimes 		return (EFAULT);
1204df8bae1dSRodney W. Grimes 	sprintf(name, "%s.core", p->p_comm);
1205df8bae1dSRodney W. Grimes 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1206bb56ec4aSPoul-Henning Kamp 	if ((error = vn_open(&nd,
120708ee5d13SAndrey A. Chernov 	    O_CREAT | FWRITE, S_IRUSR | S_IWUSR)))
1208df8bae1dSRodney W. Grimes 		return (error);
1209df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
1210df8bae1dSRodney W. Grimes 
1211df8bae1dSRodney W. Grimes 	/* Don't dump to non-regular files or files with links. */
1212df8bae1dSRodney W. Grimes 	if (vp->v_type != VREG ||
1213df8bae1dSRodney W. Grimes 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1214df8bae1dSRodney W. Grimes 		error = EFAULT;
1215df8bae1dSRodney W. Grimes 		goto out;
1216df8bae1dSRodney W. Grimes 	}
1217df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
1218df8bae1dSRodney W. Grimes 	vattr.va_size = 0;
1219df8bae1dSRodney W. Grimes 	LEASE_CHECK(vp, p, cred, LEASE_WRITE);
1220df8bae1dSRodney W. Grimes 	VOP_SETATTR(vp, &vattr, cred, p);
1221df8bae1dSRodney W. Grimes 	p->p_acflag |= ACORE;
1222df8bae1dSRodney W. Grimes 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1223df8bae1dSRodney W. Grimes 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1224df8bae1dSRodney W. Grimes 	error = cpu_coredump(p, vp, cred);
1225df8bae1dSRodney W. Grimes 	if (error == 0)
1226df8bae1dSRodney W. Grimes 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1227df8bae1dSRodney W. Grimes 		    (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1228df8bae1dSRodney W. Grimes 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1229df8bae1dSRodney W. Grimes 	if (error == 0)
1230df8bae1dSRodney W. Grimes 		error = vn_rdwr(UIO_WRITE, vp,
1231df8bae1dSRodney W. Grimes 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1232df8bae1dSRodney W. Grimes 		    round_page(ctob(vm->vm_ssize)),
1233df8bae1dSRodney W. Grimes 		    (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
1234df8bae1dSRodney W. Grimes 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1235df8bae1dSRodney W. Grimes out:
1236df8bae1dSRodney W. Grimes 	VOP_UNLOCK(vp);
1237df8bae1dSRodney W. Grimes 	error1 = vn_close(vp, FWRITE, cred, p);
1238df8bae1dSRodney W. Grimes 	if (error == 0)
1239df8bae1dSRodney W. Grimes 		error = error1;
1240df8bae1dSRodney W. Grimes 	return (error);
1241df8bae1dSRodney W. Grimes }
1242df8bae1dSRodney W. Grimes 
1243df8bae1dSRodney W. Grimes /*
1244df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1245df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1246df8bae1dSRodney W. Grimes  */
1247d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1248df8bae1dSRodney W. Grimes struct nosys_args {
1249df8bae1dSRodney W. Grimes 	int	dummy;
1250df8bae1dSRodney W. Grimes };
1251d2d3e875SBruce Evans #endif
1252df8bae1dSRodney W. Grimes /* ARGSUSED */
125326f9a767SRodney W. Grimes int
1254df8bae1dSRodney W. Grimes nosys(p, args, retval)
1255df8bae1dSRodney W. Grimes 	struct proc *p;
1256df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1257df8bae1dSRodney W. Grimes 	int *retval;
1258df8bae1dSRodney W. Grimes {
1259df8bae1dSRodney W. Grimes 
1260df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1261df8bae1dSRodney W. Grimes 	return (EINVAL);
1262df8bae1dSRodney W. Grimes }
1263