xref: /freebsd/sys/kern/kern_sig.c (revision c31146a14e393cecdf6697f5dd3bf110597623e2)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46c87e2930SDavid Greenman #include <sys/kernel.h>
47d2d3e875SBruce Evans #include <sys/sysproto.h>
4836240ea5SDoug Rabson #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/namei.h>
51df8bae1dSRodney W. Grimes #include <sys/vnode.h>
52cb679c38SJonathan Lemon #include <sys/event.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
542a024a2bSSean Eric Fagan #include <sys/pioctl.h>
55df8bae1dSRodney W. Grimes #include <sys/acct.h>
563ac4d1efSBruce Evans #include <sys/fcntl.h>
5735e0e5b3SJohn Baldwin #include <sys/ipl.h>
58238510fcSJason Evans #include <sys/condvar.h>
59c31146a1SJohn Baldwin #include <sys/lock.h>
6035e0e5b3SJohn Baldwin #include <sys/mutex.h>
61df8bae1dSRodney W. Grimes #include <sys/wait.h>
620384fff8SJason Evans #include <sys/ktr.h>
63df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
64c31146a1SJohn Baldwin #include <sys/resourcevar.h>
65df8bae1dSRodney W. Grimes #include <sys/syslog.h>
66df8bae1dSRodney W. Grimes #include <sys/stat.h>
67d66a5066SPeter Wemm #include <sys/sysent.h>
68c87e2930SDavid Greenman #include <sys/sysctl.h>
69c5edb423SSean Eric Fagan #include <sys/malloc.h>
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <machine/cpu.h>
723163861cSTor Egge #include <machine/smp.h>
73df8bae1dSRodney W. Grimes 
746f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
756f841fb7SMarcel Moolenaar 
766f841fb7SMarcel Moolenaar static int coredump	__P((struct proc *));
772c42a146SMarcel Moolenaar static int do_sigaction	__P((struct proc *p, int sig, struct sigaction *act,
78645682fdSLuoqi Chen 			     struct sigaction *oact, int old));
792c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
80645682fdSLuoqi Chen 			       sigset_t *oset, int old));
81f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t));
826f841fb7SMarcel Moolenaar static int killpg1	__P((struct proc *cp, int sig, int pgid, int all));
836f841fb7SMarcel Moolenaar static int sig_ffs	__P((sigset_t *set));
846f841fb7SMarcel Moolenaar static int sigprop	__P((int sig));
856f841fb7SMarcel Moolenaar static void stop	__P((struct proc *));
8626f9a767SRodney W. Grimes 
87cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
88cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
89cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
90cb679c38SJonathan Lemon 
91cb679c38SJonathan Lemon struct filterops sig_filtops =
92cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
93cb679c38SJonathan Lemon 
9457308494SJoerg Wunsch static int	kern_logsigexit = 1;
953d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
963d177f46SBill Fumerola     &kern_logsigexit, 0,
973d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
9857308494SJoerg Wunsch 
99df8bae1dSRodney W. Grimes /*
1002c42a146SMarcel Moolenaar  * Can process p, with pcred pc, send the signal sig to process q?
101df8bae1dSRodney W. Grimes  */
102a9e0361bSPoul-Henning Kamp #define CANSIGNAL(p, q, sig) \
103387d2c03SRobert Watson 	(!p_can(p, q, P_CAN_KILL, NULL) || \
104a9e0361bSPoul-Henning Kamp 	((sig) == SIGCONT && (q)->p_session == (p)->p_session))
105df8bae1dSRodney W. Grimes 
106831d27a9SDon Lewis /*
107831d27a9SDon Lewis  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
108831d27a9SDon Lewis  */
109831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \
110831d27a9SDon Lewis 	((uc)->cr_uid == 0 || \
111831d27a9SDon Lewis 	    (ruid) == (q)->p_cred->p_ruid || \
112831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_cred->p_ruid || \
113831d27a9SDon Lewis 	    (ruid) == (q)->p_ucred->cr_uid || \
114831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
115831d27a9SDon Lewis 
11622d4b0fbSJohn Polstra int sugid_coredump;
1173d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1183d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
119c87e2930SDavid Greenman 
120e5a28db9SPaul Saab static int	do_coredump = 1;
121e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
122e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
123e5a28db9SPaul Saab 
1242c42a146SMarcel Moolenaar /*
1252c42a146SMarcel Moolenaar  * Signal properties and actions.
1262c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1272c42a146SMarcel Moolenaar  * according to the following properties:
1282c42a146SMarcel Moolenaar  */
1292c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1302c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1312c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1322c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1332c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1342c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1352c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
136df8bae1dSRodney W. Grimes 
1372c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1382c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1392c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1412c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1462c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1502c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1512c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1522c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1532c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1542c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1552c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1562c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1572c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1582c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1592c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1602c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1612c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1622c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1632c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1642c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1652c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1662c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1672c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1682c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1692c42a146SMarcel Moolenaar };
1702c42a146SMarcel Moolenaar 
171fbbeeb6cSBruce Evans /*
172fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
173fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
174fbbeeb6cSBruce Evans  * action, the process stops in issignal().
175fbbeeb6cSBruce Evans  *
17633510ef1SBruce Evans  * MP SAFE.
177fbbeeb6cSBruce Evans  */
178fbbeeb6cSBruce Evans int
179fbbeeb6cSBruce Evans CURSIG(struct proc *p)
180fbbeeb6cSBruce Evans {
181fbbeeb6cSBruce Evans 	sigset_t tmpset;
182628d2653SJohn Baldwin 	int r = 0;
183fbbeeb6cSBruce Evans 
184628d2653SJohn Baldwin 	PROC_LOCK(p);
18533510ef1SBruce Evans 	if (SIGISEMPTY(p->p_siglist))
186628d2653SJohn Baldwin 		goto out;
187fbbeeb6cSBruce Evans 	tmpset = p->p_siglist;
188fbbeeb6cSBruce Evans 	SIGSETNAND(tmpset, p->p_sigmask);
18933510ef1SBruce Evans 	if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
190628d2653SJohn Baldwin 		goto out;
191fbbeeb6cSBruce Evans 	r = issignal(p);
192628d2653SJohn Baldwin out:
193628d2653SJohn Baldwin 	PROC_UNLOCK(p);
194fbbeeb6cSBruce Evans 	return (r);
195fbbeeb6cSBruce Evans }
196fbbeeb6cSBruce Evans 
1976f841fb7SMarcel Moolenaar static __inline int
1986f841fb7SMarcel Moolenaar sigprop(int sig)
1992c42a146SMarcel Moolenaar {
2006f841fb7SMarcel Moolenaar 
2012c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2022c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2032c42a146SMarcel Moolenaar 	return (0);
204df8bae1dSRodney W. Grimes }
2052c42a146SMarcel Moolenaar 
2066f841fb7SMarcel Moolenaar static __inline int
2076f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2082c42a146SMarcel Moolenaar {
2092c42a146SMarcel Moolenaar 	int i;
2102c42a146SMarcel Moolenaar 
2116f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2122c42a146SMarcel Moolenaar 		if (set->__bits[i])
2132c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
214df8bae1dSRodney W. Grimes 	return (0);
215df8bae1dSRodney W. Grimes }
216df8bae1dSRodney W. Grimes 
2172c42a146SMarcel Moolenaar /*
2182c42a146SMarcel Moolenaar  * do_sigaction
2192c42a146SMarcel Moolenaar  * sigaction
2202c42a146SMarcel Moolenaar  * osigaction
2212c42a146SMarcel Moolenaar  */
2222c42a146SMarcel Moolenaar static int
223645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
2242c42a146SMarcel Moolenaar 	struct proc *p;
2252c42a146SMarcel Moolenaar 	register int sig;
2262c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
227645682fdSLuoqi Chen 	int old;
228df8bae1dSRodney W. Grimes {
229628d2653SJohn Baldwin 	register struct sigacts *ps;
230df8bae1dSRodney W. Grimes 
2312c42a146SMarcel Moolenaar 	if (sig <= 0 || sig > _SIG_MAXSIG)
2322c42a146SMarcel Moolenaar 		return (EINVAL);
2332c42a146SMarcel Moolenaar 
234628d2653SJohn Baldwin 	PROC_LOCK(p);
235628d2653SJohn Baldwin 	ps = p->p_sigacts;
2362c42a146SMarcel Moolenaar 	if (oact) {
2372c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2382c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2392c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2402c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2412c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2422c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2432c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2442c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2452c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2462c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2472c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2482c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2492c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
250645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2512c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
252645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2532c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2542c42a146SMarcel Moolenaar 	}
2552c42a146SMarcel Moolenaar 	if (act) {
2562c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
257628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
258628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2592c42a146SMarcel Moolenaar 			return (EINVAL);
260628d2653SJohn Baldwin 		}
2612c42a146SMarcel Moolenaar 
262df8bae1dSRodney W. Grimes 		/*
263df8bae1dSRodney W. Grimes 		 * Change setting atomically.
264df8bae1dSRodney W. Grimes 		 */
2652c42a146SMarcel Moolenaar 
2662c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2672c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2682c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
2692c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2702c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_siginfo, sig);
2716f841fb7SMarcel Moolenaar 		} else {
2722c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] =
2732c42a146SMarcel Moolenaar 			    (__sighandler_t *)act->sa_sigaction;
2742c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2752c42a146SMarcel Moolenaar 		}
2762c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2772c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
278df8bae1dSRodney W. Grimes 		else
2792c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2802c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2812c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
282df8bae1dSRodney W. Grimes 		else
2832c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2842c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2852c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2861e41c1b5SSteven Wallace 		else
2872c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2882c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2892c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
290289ccde0SPeter Wemm 		else
2912c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
292df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2932c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2942c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
295df8bae1dSRodney W. Grimes 		else
2962c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_usertramp, seg);
297df8bae1dSRodney W. Grimes #endif
2982c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2992c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
300645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
3016626c604SJulian Elischer 			else
302645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
3032c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDWAIT) {
304245f17d4SJoerg Wunsch 				/*
3052c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3062c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3072c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3082c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
309245f17d4SJoerg Wunsch 				 */
310245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
311645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3126626c604SJulian Elischer 				else
313645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
314245f17d4SJoerg Wunsch 			} else
315645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
316df8bae1dSRodney W. Grimes 		}
317df8bae1dSRodney W. Grimes 		/*
318df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3192c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3202c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3212c42a146SMarcel Moolenaar 		 * have to restart the process.
322df8bae1dSRodney W. Grimes 		 */
3232c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3242c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3252c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3262c42a146SMarcel Moolenaar 			/* never to be seen again */
3272c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3282c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3292c42a146SMarcel Moolenaar 				/* easier in psignal */
3302c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3312c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
332645682fdSLuoqi Chen 		} else {
3332c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3342c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3352c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3362c42a146SMarcel Moolenaar 			else
3372c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3382c42a146SMarcel Moolenaar 		}
339645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
340645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
341645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
342645682fdSLuoqi Chen 		else
343645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
344df8bae1dSRodney W. Grimes 	}
345628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3462c42a146SMarcel Moolenaar 	return (0);
3472c42a146SMarcel Moolenaar }
3482c42a146SMarcel Moolenaar 
3492c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3502c42a146SMarcel Moolenaar struct sigaction_args {
3512c42a146SMarcel Moolenaar 	int	sig;
3522c42a146SMarcel Moolenaar 	struct	sigaction *act;
3532c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3542c42a146SMarcel Moolenaar };
3552c42a146SMarcel Moolenaar #endif
3562c42a146SMarcel Moolenaar /* ARGSUSED */
3572c42a146SMarcel Moolenaar int
3582c42a146SMarcel Moolenaar sigaction(p, uap)
3592c42a146SMarcel Moolenaar 	struct proc *p;
3602c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3612c42a146SMarcel Moolenaar {
3622c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3632c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3642c42a146SMarcel Moolenaar 	int error;
3652c42a146SMarcel Moolenaar 
3666f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3676f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3682c42a146SMarcel Moolenaar 	if (actp) {
3696f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3702c42a146SMarcel Moolenaar 		if (error)
3712c42a146SMarcel Moolenaar 			return (error);
3722c42a146SMarcel Moolenaar 	}
373645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3742c42a146SMarcel Moolenaar 	if (oactp && !error) {
3756f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3762c42a146SMarcel Moolenaar 	}
3772c42a146SMarcel Moolenaar 	return (error);
3782c42a146SMarcel Moolenaar }
3792c42a146SMarcel Moolenaar 
38031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3812c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3822c42a146SMarcel Moolenaar struct osigaction_args {
3832c42a146SMarcel Moolenaar 	int	signum;
3842c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3852c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3862c42a146SMarcel Moolenaar };
3872c42a146SMarcel Moolenaar #endif
3882c42a146SMarcel Moolenaar /* ARGSUSED */
3892c42a146SMarcel Moolenaar int
3902c42a146SMarcel Moolenaar osigaction(p, uap)
3912c42a146SMarcel Moolenaar 	struct proc *p;
3922c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3932c42a146SMarcel Moolenaar {
3942c42a146SMarcel Moolenaar 	struct osigaction sa;
3952c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
3962c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
3972c42a146SMarcel Moolenaar 	int error;
3982c42a146SMarcel Moolenaar 
3996f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4006f841fb7SMarcel Moolenaar 		return (EINVAL);
4016f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4026f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
4032c42a146SMarcel Moolenaar 	if (nsap) {
4046f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4052c42a146SMarcel Moolenaar 		if (error)
4062c42a146SMarcel Moolenaar 			return (error);
4072c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4082c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4092c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4102c42a146SMarcel Moolenaar 	}
411645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
4122c42a146SMarcel Moolenaar 	if (osap && !error) {
4132c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4142c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4152c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4166f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4172c42a146SMarcel Moolenaar 	}
4182c42a146SMarcel Moolenaar 	return (error);
4192c42a146SMarcel Moolenaar }
42031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes /*
423df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
424df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
425df8bae1dSRodney W. Grimes  */
426df8bae1dSRodney W. Grimes void
427df8bae1dSRodney W. Grimes siginit(p)
428df8bae1dSRodney W. Grimes 	struct proc *p;
429df8bae1dSRodney W. Grimes {
430df8bae1dSRodney W. Grimes 	register int i;
431df8bae1dSRodney W. Grimes 
432628d2653SJohn Baldwin 	PROC_LOCK(p);
4332c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4342c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4352c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
436628d2653SJohn Baldwin 	PROC_UNLOCK(p);
437df8bae1dSRodney W. Grimes }
438df8bae1dSRodney W. Grimes 
439df8bae1dSRodney W. Grimes /*
440df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
441df8bae1dSRodney W. Grimes  */
442df8bae1dSRodney W. Grimes void
443df8bae1dSRodney W. Grimes execsigs(p)
444df8bae1dSRodney W. Grimes 	register struct proc *p;
445df8bae1dSRodney W. Grimes {
446628d2653SJohn Baldwin 	register struct sigacts *ps;
4472c42a146SMarcel Moolenaar 	register int sig;
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	/*
450df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
451df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
452df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
453df8bae1dSRodney W. Grimes 	 */
454628d2653SJohn Baldwin 	PROC_LOCK(p);
455628d2653SJohn Baldwin 	ps = p->p_sigacts;
4562c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4572c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4582c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4592c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4602c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4612c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4622c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
463df8bae1dSRodney W. Grimes 		}
4642c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
465df8bae1dSRodney W. Grimes 	}
466df8bae1dSRodney W. Grimes 	/*
467df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
468df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
469df8bae1dSRodney W. Grimes 	 */
470645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
471645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
472645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
47380e907a1SPeter Wemm 	/*
47480e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
47580e907a1SPeter Wemm 	 */
476645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
477628d2653SJohn Baldwin 	PROC_UNLOCK(p);
478df8bae1dSRodney W. Grimes }
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes /*
481628d2653SJohn Baldwin  * do_sigprocmask()
4827c8fdcbdSMatthew Dillon  *
483628d2653SJohn Baldwin  *	Manipulate signal mask.
484df8bae1dSRodney W. Grimes  */
4852c42a146SMarcel Moolenaar static int
486645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
4872c42a146SMarcel Moolenaar 	struct proc *p;
4882c42a146SMarcel Moolenaar 	int how;
4892c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
490645682fdSLuoqi Chen 	int old;
4912c42a146SMarcel Moolenaar {
4922c42a146SMarcel Moolenaar 	int error;
4932c42a146SMarcel Moolenaar 
494628d2653SJohn Baldwin 	PROC_LOCK(p);
4952c42a146SMarcel Moolenaar 	if (oset != NULL)
4962c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
4972c42a146SMarcel Moolenaar 
4982c42a146SMarcel Moolenaar 	error = 0;
4992c42a146SMarcel Moolenaar 	if (set != NULL) {
5002c42a146SMarcel Moolenaar 		switch (how) {
5012c42a146SMarcel Moolenaar 		case SIG_BLOCK:
502645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
5032c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
5042c42a146SMarcel Moolenaar 			break;
5052c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
5062c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
5072c42a146SMarcel Moolenaar 			break;
5082c42a146SMarcel Moolenaar 		case SIG_SETMASK:
509645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
510645682fdSLuoqi Chen 			if (old)
511645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
512645682fdSLuoqi Chen 			else
5132c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
5142c42a146SMarcel Moolenaar 			break;
5152c42a146SMarcel Moolenaar 		default:
5162c42a146SMarcel Moolenaar 			error = EINVAL;
5172c42a146SMarcel Moolenaar 			break;
5182c42a146SMarcel Moolenaar 		}
5192c42a146SMarcel Moolenaar 	}
520628d2653SJohn Baldwin 	PROC_UNLOCK(p);
5212c42a146SMarcel Moolenaar 	return (error);
5222c42a146SMarcel Moolenaar }
5232c42a146SMarcel Moolenaar 
5247c8fdcbdSMatthew Dillon /*
5257c8fdcbdSMatthew Dillon  * sigprocmask() - MP SAFE
5267c8fdcbdSMatthew Dillon  */
5277c8fdcbdSMatthew Dillon 
528d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
529df8bae1dSRodney W. Grimes struct sigprocmask_args {
530df8bae1dSRodney W. Grimes 	int	how;
5312c42a146SMarcel Moolenaar 	const sigset_t *set;
5322c42a146SMarcel Moolenaar 	sigset_t *oset;
533df8bae1dSRodney W. Grimes };
534d2d3e875SBruce Evans #endif
53526f9a767SRodney W. Grimes int
536cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
537df8bae1dSRodney W. Grimes 	register struct proc *p;
538df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
539df8bae1dSRodney W. Grimes {
5402c42a146SMarcel Moolenaar 	sigset_t set, oset;
5412c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5422c42a146SMarcel Moolenaar 	int error;
543df8bae1dSRodney W. Grimes 
5446f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5456f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5462c42a146SMarcel Moolenaar 	if (setp) {
5476f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5482c42a146SMarcel Moolenaar 		if (error)
5492c42a146SMarcel Moolenaar 			return (error);
550df8bae1dSRodney W. Grimes 	}
551645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5522c42a146SMarcel Moolenaar 	if (osetp && !error) {
5536f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5542c42a146SMarcel Moolenaar 	}
5552c42a146SMarcel Moolenaar 	return (error);
5562c42a146SMarcel Moolenaar }
5572c42a146SMarcel Moolenaar 
55831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5597c8fdcbdSMatthew Dillon /*
5607c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5617c8fdcbdSMatthew Dillon  */
5622c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5632c42a146SMarcel Moolenaar struct osigprocmask_args {
5642c42a146SMarcel Moolenaar 	int	how;
5652c42a146SMarcel Moolenaar 	osigset_t mask;
5662c42a146SMarcel Moolenaar };
5672c42a146SMarcel Moolenaar #endif
5682c42a146SMarcel Moolenaar int
5692c42a146SMarcel Moolenaar osigprocmask(p, uap)
5702c42a146SMarcel Moolenaar 	register struct proc *p;
5712c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5722c42a146SMarcel Moolenaar {
5732c42a146SMarcel Moolenaar 	sigset_t set, oset;
5742c42a146SMarcel Moolenaar 	int error;
5752c42a146SMarcel Moolenaar 
5762c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
577645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
5782c42a146SMarcel Moolenaar 	SIG2OSIG(oset, p->p_retval[0]);
579df8bae1dSRodney W. Grimes 	return (error);
580df8bae1dSRodney W. Grimes }
58131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
582df8bae1dSRodney W. Grimes 
583d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
584df8bae1dSRodney W. Grimes struct sigpending_args {
5852c42a146SMarcel Moolenaar 	sigset_t	*set;
586df8bae1dSRodney W. Grimes };
587d2d3e875SBruce Evans #endif
588df8bae1dSRodney W. Grimes /* ARGSUSED */
58926f9a767SRodney W. Grimes int
590cb226aaaSPoul-Henning Kamp sigpending(p, uap)
591df8bae1dSRodney W. Grimes 	struct proc *p;
592df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
593df8bae1dSRodney W. Grimes {
594628d2653SJohn Baldwin 	sigset_t siglist;
595df8bae1dSRodney W. Grimes 
596628d2653SJohn Baldwin 	PROC_LOCK(p);
597628d2653SJohn Baldwin 	siglist = p->p_siglist;
598628d2653SJohn Baldwin 	PROC_UNLOCK(p);
599628d2653SJohn Baldwin 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
6002c42a146SMarcel Moolenaar }
6012c42a146SMarcel Moolenaar 
60231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6032c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6042c42a146SMarcel Moolenaar struct osigpending_args {
6052c42a146SMarcel Moolenaar 	int	dummy;
6062c42a146SMarcel Moolenaar };
6072c42a146SMarcel Moolenaar #endif
6082c42a146SMarcel Moolenaar /* ARGSUSED */
6092c42a146SMarcel Moolenaar int
6102c42a146SMarcel Moolenaar osigpending(p, uap)
6112c42a146SMarcel Moolenaar 	struct proc *p;
6122c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6132c42a146SMarcel Moolenaar {
6142c42a146SMarcel Moolenaar 
615628d2653SJohn Baldwin 	PROC_LOCK(p);
6162c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_siglist, p->p_retval[0]);
617628d2653SJohn Baldwin 	PROC_UNLOCK(p);
618df8bae1dSRodney W. Grimes 	return (0);
619df8bae1dSRodney W. Grimes }
62031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
623df8bae1dSRodney W. Grimes /*
624df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
625df8bae1dSRodney W. Grimes  */
626d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
627df8bae1dSRodney W. Grimes struct osigvec_args {
628df8bae1dSRodney W. Grimes 	int	signum;
629df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
630df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
631df8bae1dSRodney W. Grimes };
632d2d3e875SBruce Evans #endif
633df8bae1dSRodney W. Grimes /* ARGSUSED */
63426f9a767SRodney W. Grimes int
635cb226aaaSPoul-Henning Kamp osigvec(p, uap)
636df8bae1dSRodney W. Grimes 	struct proc *p;
637df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
638df8bae1dSRodney W. Grimes {
639df8bae1dSRodney W. Grimes 	struct sigvec vec;
6402c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6412c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6422c42a146SMarcel Moolenaar 	int error;
643df8bae1dSRodney W. Grimes 
6446f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6456f841fb7SMarcel Moolenaar 		return (EINVAL);
6466f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6476f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6482c42a146SMarcel Moolenaar 	if (nsap) {
6496f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
6502c42a146SMarcel Moolenaar 		if (error)
651df8bae1dSRodney W. Grimes 			return (error);
6522c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
6532c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
6542c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
6552c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
656df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
6572c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
658df8bae1dSRodney W. Grimes #endif
659df8bae1dSRodney W. Grimes 	}
660645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
6612c42a146SMarcel Moolenaar 	if (osap && !error) {
6622c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
6632c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
6642c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
6652c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
6662c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
6672c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
6682c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
6692c42a146SMarcel Moolenaar #endif
6706f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
6712c42a146SMarcel Moolenaar 	}
6722c42a146SMarcel Moolenaar 	return (error);
673df8bae1dSRodney W. Grimes }
674df8bae1dSRodney W. Grimes 
675d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
676df8bae1dSRodney W. Grimes struct osigblock_args {
677df8bae1dSRodney W. Grimes 	int	mask;
678df8bae1dSRodney W. Grimes };
679d2d3e875SBruce Evans #endif
68026f9a767SRodney W. Grimes int
681cb226aaaSPoul-Henning Kamp osigblock(p, uap)
682df8bae1dSRodney W. Grimes 	register struct proc *p;
683df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
684df8bae1dSRodney W. Grimes {
6852c42a146SMarcel Moolenaar 	sigset_t set;
686df8bae1dSRodney W. Grimes 
6872c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6882c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
689628d2653SJohn Baldwin 	PROC_LOCK(p);
6902c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
6912c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
692628d2653SJohn Baldwin 	PROC_UNLOCK(p);
693df8bae1dSRodney W. Grimes 	return (0);
694df8bae1dSRodney W. Grimes }
695df8bae1dSRodney W. Grimes 
696d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
697df8bae1dSRodney W. Grimes struct osigsetmask_args {
698df8bae1dSRodney W. Grimes 	int	mask;
699df8bae1dSRodney W. Grimes };
700d2d3e875SBruce Evans #endif
70126f9a767SRodney W. Grimes int
702cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
703df8bae1dSRodney W. Grimes 	struct proc *p;
704df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
705df8bae1dSRodney W. Grimes {
7062c42a146SMarcel Moolenaar 	sigset_t set;
707df8bae1dSRodney W. Grimes 
7082c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7092c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
710628d2653SJohn Baldwin 	PROC_LOCK(p);
7112c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
712645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
713628d2653SJohn Baldwin 	PROC_UNLOCK(p);
714df8bae1dSRodney W. Grimes 	return (0);
715df8bae1dSRodney W. Grimes }
716df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes /*
719df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
720df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
721df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
722df8bae1dSRodney W. Grimes  */
723d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
724df8bae1dSRodney W. Grimes struct sigsuspend_args {
7252c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
726df8bae1dSRodney W. Grimes };
727d2d3e875SBruce Evans #endif
728df8bae1dSRodney W. Grimes /* ARGSUSED */
72926f9a767SRodney W. Grimes int
730cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
731df8bae1dSRodney W. Grimes 	register struct proc *p;
732df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
733df8bae1dSRodney W. Grimes {
7342c42a146SMarcel Moolenaar 	sigset_t mask;
735628d2653SJohn Baldwin 	register struct sigacts *ps;
7362c42a146SMarcel Moolenaar 	int error;
7372c42a146SMarcel Moolenaar 
7386f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
7392c42a146SMarcel Moolenaar 	if (error)
7402c42a146SMarcel Moolenaar 		return (error);
741df8bae1dSRodney W. Grimes 
742df8bae1dSRodney W. Grimes 	/*
743645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
744df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
745df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
746df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
747df8bae1dSRodney W. Grimes 	 * to indicate this.
748df8bae1dSRodney W. Grimes 	 */
749628d2653SJohn Baldwin 	PROC_LOCK(p);
750628d2653SJohn Baldwin 	ps = p->p_sigacts;
7516626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
752645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
753645682fdSLuoqi Chen 
754645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
7552c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
756628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
7572c42a146SMarcel Moolenaar 		/* void */;
758628d2653SJohn Baldwin 	PROC_UNLOCK(p);
7592c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
7602c42a146SMarcel Moolenaar 	return (EINTR);
7612c42a146SMarcel Moolenaar }
7622c42a146SMarcel Moolenaar 
76331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7642c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7652c42a146SMarcel Moolenaar struct osigsuspend_args {
7662c42a146SMarcel Moolenaar 	osigset_t mask;
7672c42a146SMarcel Moolenaar };
7682c42a146SMarcel Moolenaar #endif
7692c42a146SMarcel Moolenaar /* ARGSUSED */
7702c42a146SMarcel Moolenaar int
7712c42a146SMarcel Moolenaar osigsuspend(p, uap)
7722c42a146SMarcel Moolenaar 	register struct proc *p;
7732c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
7742c42a146SMarcel Moolenaar {
775645682fdSLuoqi Chen 	sigset_t mask;
776628d2653SJohn Baldwin 	register struct sigacts *ps;
7772c42a146SMarcel Moolenaar 
778628d2653SJohn Baldwin 	PROC_LOCK(p);
779628d2653SJohn Baldwin 	ps = p->p_sigacts;
7802c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
781645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
782645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
783645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
784645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
785628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
786df8bae1dSRodney W. Grimes 		/* void */;
787628d2653SJohn Baldwin 	PROC_UNLOCK(p);
788df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
789df8bae1dSRodney W. Grimes 	return (EINTR);
790df8bae1dSRodney W. Grimes }
79131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
792df8bae1dSRodney W. Grimes 
793df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
794d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
795df8bae1dSRodney W. Grimes struct osigstack_args {
796df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
797df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
798df8bae1dSRodney W. Grimes };
799d2d3e875SBruce Evans #endif
800df8bae1dSRodney W. Grimes /* ARGSUSED */
80126f9a767SRodney W. Grimes int
802cb226aaaSPoul-Henning Kamp osigstack(p, uap)
803df8bae1dSRodney W. Grimes 	struct proc *p;
804df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
805df8bae1dSRodney W. Grimes {
806df8bae1dSRodney W. Grimes 	struct sigstack ss;
807d034d459SMarcel Moolenaar 	int error;
808df8bae1dSRodney W. Grimes 
809d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
810628d2653SJohn Baldwin 		PROC_LOCK(p);
811645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
812d034d459SMarcel Moolenaar 		ss.ss_onstack = sigonstack(cpu_getstack(p));
813628d2653SJohn Baldwin 		PROC_UNLOCK(p);
814d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
815d034d459SMarcel Moolenaar 		if (error)
816df8bae1dSRodney W. Grimes 			return (error);
817d034d459SMarcel Moolenaar 	}
818d034d459SMarcel Moolenaar 
819d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
820d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
821d034d459SMarcel Moolenaar 			return (error);
822628d2653SJohn Baldwin 		PROC_LOCK(p);
823645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
824645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
825645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
826645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
827628d2653SJohn Baldwin 		PROC_UNLOCK(p);
828df8bae1dSRodney W. Grimes 	}
829d034d459SMarcel Moolenaar 	return (0);
830df8bae1dSRodney W. Grimes }
831df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
832df8bae1dSRodney W. Grimes 
833d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
834df8bae1dSRodney W. Grimes struct sigaltstack_args {
8352c42a146SMarcel Moolenaar 	stack_t	*ss;
8362c42a146SMarcel Moolenaar 	stack_t	*oss;
837df8bae1dSRodney W. Grimes };
838d2d3e875SBruce Evans #endif
839df8bae1dSRodney W. Grimes /* ARGSUSED */
84026f9a767SRodney W. Grimes int
841cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
842df8bae1dSRodney W. Grimes 	struct proc *p;
843df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
844df8bae1dSRodney W. Grimes {
8452c42a146SMarcel Moolenaar 	stack_t ss;
846d034d459SMarcel Moolenaar 	int error, oonstack;
847df8bae1dSRodney W. Grimes 
848d034d459SMarcel Moolenaar 	oonstack = sigonstack(cpu_getstack(p));
849d034d459SMarcel Moolenaar 
850d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
851628d2653SJohn Baldwin 		PROC_LOCK(p);
852d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
853d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
854d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
855628d2653SJohn Baldwin 		PROC_UNLOCK(p);
856d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
857df8bae1dSRodney W. Grimes 			return (error);
858df8bae1dSRodney W. Grimes 	}
859d034d459SMarcel Moolenaar 
860d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
861d034d459SMarcel Moolenaar 		if (oonstack)
862d034d459SMarcel Moolenaar 			return (EPERM);
863d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
864d034d459SMarcel Moolenaar 			return (error);
865d034d459SMarcel Moolenaar 		if ((ss.ss_flags & ~SS_DISABLE) != 0)
866d034d459SMarcel Moolenaar 			return (EINVAL);
867d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
868806d7daaSMarcel Moolenaar 			if (ss.ss_size < p->p_sysent->sv_minsigstksz)
869df8bae1dSRodney W. Grimes 				return (ENOMEM);
870628d2653SJohn Baldwin 			PROC_LOCK(p);
871645682fdSLuoqi Chen 			p->p_sigstk = ss;
872d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
873628d2653SJohn Baldwin 			PROC_UNLOCK(p);
874628d2653SJohn Baldwin 		} else {
875628d2653SJohn Baldwin 			PROC_LOCK(p);
876d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
877628d2653SJohn Baldwin 			PROC_UNLOCK(p);
878628d2653SJohn Baldwin 		}
879d034d459SMarcel Moolenaar 	}
880df8bae1dSRodney W. Grimes 	return (0);
881df8bae1dSRodney W. Grimes }
882df8bae1dSRodney W. Grimes 
883d93f860cSPoul-Henning Kamp /*
884d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
885d93f860cSPoul-Henning Kamp  * cp is calling process.
886d93f860cSPoul-Henning Kamp  */
887d93f860cSPoul-Henning Kamp int
8882c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
889d93f860cSPoul-Henning Kamp 	register struct proc *cp;
8902c42a146SMarcel Moolenaar 	int sig, pgid, all;
891d93f860cSPoul-Henning Kamp {
892d93f860cSPoul-Henning Kamp 	register struct proc *p;
893d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
894d93f860cSPoul-Henning Kamp 	int nfound = 0;
895d93f860cSPoul-Henning Kamp 
896553629ebSJake Burkholder 	if (all) {
897d93f860cSPoul-Henning Kamp 		/*
898d93f860cSPoul-Henning Kamp 		 * broadcast
899d93f860cSPoul-Henning Kamp 		 */
900c0c25570SJake Burkholder 		ALLPROC_LOCK(AP_SHARED);
9012e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
902628d2653SJohn Baldwin 			PROC_LOCK(p);
903628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) {
904628d2653SJohn Baldwin 				PROC_UNLOCK(p);
905628d2653SJohn Baldwin 				continue;
906628d2653SJohn Baldwin 			}
907628d2653SJohn Baldwin 			PROC_UNLOCK(p);
908628d2653SJohn Baldwin 			/*
909628d2653SJohn Baldwin 			 * XXX: this locking needs work.. specifically the
910628d2653SJohn Baldwin 			 * session checks..
911628d2653SJohn Baldwin 			 */
912628d2653SJohn Baldwin 			if (!CANSIGNAL(cp, p, sig))
913d93f860cSPoul-Henning Kamp 				continue;
914d93f860cSPoul-Henning Kamp 			nfound++;
915628d2653SJohn Baldwin 			if (sig) {
916628d2653SJohn Baldwin 				PROC_LOCK(p);
9172c42a146SMarcel Moolenaar 				psignal(p, sig);
918628d2653SJohn Baldwin 				PROC_UNLOCK(p);
919628d2653SJohn Baldwin 			}
920d93f860cSPoul-Henning Kamp 		}
921c0c25570SJake Burkholder 		ALLPROC_LOCK(AP_RELEASE);
922553629ebSJake Burkholder 	} else {
923d93f860cSPoul-Henning Kamp 		if (pgid == 0)
924d93f860cSPoul-Henning Kamp 			/*
925d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
926d93f860cSPoul-Henning Kamp 			 */
927d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
928d93f860cSPoul-Henning Kamp 		else {
929d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
930d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
931d93f860cSPoul-Henning Kamp 				return (ESRCH);
932d93f860cSPoul-Henning Kamp 		}
9332e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
934628d2653SJohn Baldwin 			PROC_LOCK(p);
935628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
936628d2653SJohn Baldwin 				PROC_UNLOCK(p);
937628d2653SJohn Baldwin 				continue;
938628d2653SJohn Baldwin 			}
939628d2653SJohn Baldwin 			PROC_UNLOCK(p);
940628d2653SJohn Baldwin 			mtx_lock_spin(&sched_lock);
941628d2653SJohn Baldwin 			if (p->p_stat == SZOMB) {
942628d2653SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
943628d2653SJohn Baldwin 				continue;
944628d2653SJohn Baldwin 			}
945628d2653SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
946628d2653SJohn Baldwin 			/* XXX: locking b0rked */
947628d2653SJohn Baldwin 			if (!CANSIGNAL(cp, p, sig))
948d93f860cSPoul-Henning Kamp 				continue;
949d93f860cSPoul-Henning Kamp 			nfound++;
950628d2653SJohn Baldwin 			if (sig) {
951628d2653SJohn Baldwin 				PROC_LOCK(p);
9522c42a146SMarcel Moolenaar 				psignal(p, sig);
953628d2653SJohn Baldwin 				PROC_UNLOCK(p);
954628d2653SJohn Baldwin 			}
955d93f860cSPoul-Henning Kamp 		}
956d93f860cSPoul-Henning Kamp 	}
957d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
958d93f860cSPoul-Henning Kamp }
959d93f860cSPoul-Henning Kamp 
960d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
961df8bae1dSRodney W. Grimes struct kill_args {
962df8bae1dSRodney W. Grimes 	int	pid;
963df8bae1dSRodney W. Grimes 	int	signum;
964df8bae1dSRodney W. Grimes };
965d2d3e875SBruce Evans #endif
966df8bae1dSRodney W. Grimes /* ARGSUSED */
96726f9a767SRodney W. Grimes int
968cb226aaaSPoul-Henning Kamp kill(cp, uap)
969df8bae1dSRodney W. Grimes 	register struct proc *cp;
970df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
971df8bae1dSRodney W. Grimes {
972df8bae1dSRodney W. Grimes 	register struct proc *p;
973df8bae1dSRodney W. Grimes 
9742c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
975df8bae1dSRodney W. Grimes 		return (EINVAL);
976df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
977df8bae1dSRodney W. Grimes 		/* kill single process */
978df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
979df8bae1dSRodney W. Grimes 			return (ESRCH);
980628d2653SJohn Baldwin 		/* XXX: locking b0rked */
981a9e0361bSPoul-Henning Kamp 		if (!CANSIGNAL(cp, p, uap->signum))
982df8bae1dSRodney W. Grimes 			return (EPERM);
983628d2653SJohn Baldwin 		if (uap->signum) {
984628d2653SJohn Baldwin 			PROC_LOCK(p);
985df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
986628d2653SJohn Baldwin 			PROC_UNLOCK(p);
987628d2653SJohn Baldwin 		}
988df8bae1dSRodney W. Grimes 		return (0);
989df8bae1dSRodney W. Grimes 	}
990df8bae1dSRodney W. Grimes 	switch (uap->pid) {
991df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
992df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
993df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
994df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
995df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
996df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
997df8bae1dSRodney W. Grimes 	}
998df8bae1dSRodney W. Grimes 	/* NOTREACHED */
999df8bae1dSRodney W. Grimes }
1000df8bae1dSRodney W. Grimes 
1001df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1002d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1003df8bae1dSRodney W. Grimes struct okillpg_args {
1004df8bae1dSRodney W. Grimes 	int	pgid;
1005df8bae1dSRodney W. Grimes 	int	signum;
1006df8bae1dSRodney W. Grimes };
1007d2d3e875SBruce Evans #endif
1008df8bae1dSRodney W. Grimes /* ARGSUSED */
100926f9a767SRodney W. Grimes int
1010cb226aaaSPoul-Henning Kamp okillpg(p, uap)
1011df8bae1dSRodney W. Grimes 	struct proc *p;
1012df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1013df8bae1dSRodney W. Grimes {
1014df8bae1dSRodney W. Grimes 
10152c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
1016df8bae1dSRodney W. Grimes 		return (EINVAL);
1017df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
1018df8bae1dSRodney W. Grimes }
1019df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1020df8bae1dSRodney W. Grimes 
1021df8bae1dSRodney W. Grimes /*
1022df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1023df8bae1dSRodney W. Grimes  */
1024df8bae1dSRodney W. Grimes void
10252c42a146SMarcel Moolenaar gsignal(pgid, sig)
10262c42a146SMarcel Moolenaar 	int pgid, sig;
1027df8bae1dSRodney W. Grimes {
1028df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
10312c42a146SMarcel Moolenaar 		pgsignal(pgrp, sig, 0);
1032df8bae1dSRodney W. Grimes }
1033df8bae1dSRodney W. Grimes 
1034df8bae1dSRodney W. Grimes /*
1035df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1036df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1037df8bae1dSRodney W. Grimes  */
1038df8bae1dSRodney W. Grimes void
10392c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1040df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
10412c42a146SMarcel Moolenaar 	int sig, checkctty;
1042df8bae1dSRodney W. Grimes {
1043df8bae1dSRodney W. Grimes 	register struct proc *p;
1044df8bae1dSRodney W. Grimes 
1045628d2653SJohn Baldwin 	if (pgrp) {
1046628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1047628d2653SJohn Baldwin 			PROC_LOCK(p);
1048df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
10492c42a146SMarcel Moolenaar 				psignal(p, sig);
1050628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1051628d2653SJohn Baldwin 		}
1052628d2653SJohn Baldwin 	}
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes /*
1056df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1057df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1058df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1059df8bae1dSRodney W. Grimes  */
1060df8bae1dSRodney W. Grimes void
10612c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1062df8bae1dSRodney W. Grimes 	struct proc *p;
10632c42a146SMarcel Moolenaar 	register int sig;
10648674077aSJeffrey Hsu 	u_long code;
1065df8bae1dSRodney W. Grimes {
1066df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1067df8bae1dSRodney W. Grimes 
1068628d2653SJohn Baldwin 	PROC_LOCK(p);
10692c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1070d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1071df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1072df8bae1dSRodney W. Grimes #ifdef KTRACE
1073df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
10742c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
10752c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1076df8bae1dSRodney W. Grimes #endif
1077628d2653SJohn Baldwin 		PROC_UNLOCK(p);	/* XXX ??? */
10782c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
10792c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
1080628d2653SJohn Baldwin 		PROC_LOCK(p);
10812c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
10822c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
10832c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
10842c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1085289ccde0SPeter Wemm 			/*
10862c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1087289ccde0SPeter Wemm 			 */
10882c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
10892c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
10902c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
10912c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
10922c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1093dedc04feSPeter Wemm 		}
10946f841fb7SMarcel Moolenaar 	} else {
10956626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
10962c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
10972c42a146SMarcel Moolenaar 		psignal(p, sig);
1098df8bae1dSRodney W. Grimes 	}
1099628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1100df8bae1dSRodney W. Grimes }
1101df8bae1dSRodney W. Grimes 
1102df8bae1dSRodney W. Grimes /*
1103df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1104df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1105df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1106df8bae1dSRodney W. Grimes  *
1107df8bae1dSRodney W. Grimes  * Exceptions:
1108df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1109df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1110df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1111df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1112df8bae1dSRodney W. Grimes  *
1113df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1114df8bae1dSRodney W. Grimes  */
1115df8bae1dSRodney W. Grimes void
11162c42a146SMarcel Moolenaar psignal(p, sig)
1117df8bae1dSRodney W. Grimes 	register struct proc *p;
11182c42a146SMarcel Moolenaar 	register int sig;
1119df8bae1dSRodney W. Grimes {
112040447cd4SJohn Baldwin 	register int prop;
1121df8bae1dSRodney W. Grimes 	register sig_t action;
1122df8bae1dSRodney W. Grimes 
11232c42a146SMarcel Moolenaar 	if (sig > _SIG_MAXSIG || sig <= 0) {
11242c42a146SMarcel Moolenaar 		printf("psignal: signal %d\n", sig);
1125df8bae1dSRodney W. Grimes 		panic("psignal signal number");
11262a024a2bSSean Eric Fagan 	}
11272c42a146SMarcel Moolenaar 
1128628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1129cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1130cb679c38SJonathan Lemon 
11312c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1132df8bae1dSRodney W. Grimes 
1133df8bae1dSRodney W. Grimes 	/*
11342a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
11352a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
11362a024a2bSSean Eric Fagan 	 * a chance, as well.
1137df8bae1dSRodney W. Grimes 	 */
11382a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1139df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1140df8bae1dSRodney W. Grimes 	else {
1141df8bae1dSRodney W. Grimes 		/*
1142df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1143df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1144df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1145df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1146df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1147df8bae1dSRodney W. Grimes 		 */
1148628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1149df8bae1dSRodney W. Grimes 			return;
11502c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1151df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
11522c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1153df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1154df8bae1dSRodney W. Grimes 		else
1155df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1156df8bae1dSRodney W. Grimes 	}
1157df8bae1dSRodney W. Grimes 
11589ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1159df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1160df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
1161df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
11629ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
11652c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1166df8bae1dSRodney W. Grimes 
1167df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1168df8bae1dSRodney W. Grimes 		/*
1169df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1170df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1171df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1172df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1173df8bae1dSRodney W. Grimes 		 */
1174df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1175628d2653SJohn Baldwin 		    action == SIG_DFL)
1176df8bae1dSRodney W. Grimes 		        return;
11772c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1178df8bae1dSRodney W. Grimes 	}
11792c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1180df8bae1dSRodney W. Grimes 
1181df8bae1dSRodney W. Grimes 	/*
1182df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1183df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1184df8bae1dSRodney W. Grimes 	 */
11859ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
11861c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
11879ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1188df8bae1dSRodney W. Grimes 		return;
11891c32c37cSJohn Baldwin 	}
1190df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1191df8bae1dSRodney W. Grimes 
1192df8bae1dSRodney W. Grimes 	case SSLEEP:
1193df8bae1dSRodney W. Grimes 		/*
1194df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1195df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1196df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1197df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1198df8bae1dSRodney W. Grimes 		 */
119940447cd4SJohn Baldwin 		if ((p->p_sflag & PS_SINTR) == 0) {
12009ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1201df8bae1dSRodney W. Grimes 			goto out;
12023e6831f5SJohn Baldwin 		}
1203df8bae1dSRodney W. Grimes 		/*
1204df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1205df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1206df8bae1dSRodney W. Grimes 		 * for the parent.
1207df8bae1dSRodney W. Grimes 		 */
1208df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1209df8bae1dSRodney W. Grimes 			goto run;
12109ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1211df8bae1dSRodney W. Grimes 		/*
1212df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1213df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1214df8bae1dSRodney W. Grimes 		 * be awakened.
1215df8bae1dSRodney W. Grimes 		 */
1216df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
12172c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1218df8bae1dSRodney W. Grimes 			goto out;
1219df8bae1dSRodney W. Grimes 		}
1220df8bae1dSRodney W. Grimes 		/*
1221df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1222df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1223df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1224df8bae1dSRodney W. Grimes 		 * cause the process to run.
1225df8bae1dSRodney W. Grimes 		 */
1226df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
12273e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1228df8bae1dSRodney W. Grimes 				goto runfast;
1229df8bae1dSRodney W. Grimes 			/*
1230df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1231df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1232df8bae1dSRodney W. Grimes 			 */
1233df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1234df8bae1dSRodney W. Grimes 				goto out;
12352c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
12362c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1237628d2653SJohn Baldwin 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) {
1238628d2653SJohn Baldwin 				PROC_LOCK(p->p_pptr);
1239df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1240628d2653SJohn Baldwin 				PROC_UNLOCK(p->p_pptr);
1241628d2653SJohn Baldwin 			}
1242df8bae1dSRodney W. Grimes 			stop(p);
1243df8bae1dSRodney W. Grimes 			goto out;
12443e6831f5SJohn Baldwin 		} else
1245df8bae1dSRodney W. Grimes 			goto runfast;
1246df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1247df8bae1dSRodney W. Grimes 
1248df8bae1dSRodney W. Grimes 	case SSTOP:
12499ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1250df8bae1dSRodney W. Grimes 		/*
1251df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1252df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1253df8bae1dSRodney W. Grimes 		 */
125440447cd4SJohn Baldwin 		if (p->p_flag & P_TRACED)
1255df8bae1dSRodney W. Grimes 			goto out;
1256df8bae1dSRodney W. Grimes 
1257df8bae1dSRodney W. Grimes 		/*
1258df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1259df8bae1dSRodney W. Grimes 		 */
12602c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1261df8bae1dSRodney W. Grimes 			goto runfast;
1262df8bae1dSRodney W. Grimes 
1263df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1264df8bae1dSRodney W. Grimes 			/*
1265df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1266df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1267df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1268df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1269df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1270df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1271df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1272df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1273df8bae1dSRodney W. Grimes 			 */
1274df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
12752c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1276df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1277df8bae1dSRodney W. Grimes 				goto runfast;
12789ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
127940447cd4SJohn Baldwin 			if (p->p_wchan == NULL)
1280df8bae1dSRodney W. Grimes 				goto run;
1281df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
12829ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1283df8bae1dSRodney W. Grimes 			goto out;
1284df8bae1dSRodney W. Grimes 		}
1285df8bae1dSRodney W. Grimes 
1286df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1287df8bae1dSRodney W. Grimes 			/*
1288df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1289df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1290df8bae1dSRodney W. Grimes 			 */
12912c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1292df8bae1dSRodney W. Grimes 			goto out;
1293df8bae1dSRodney W. Grimes 		}
1294df8bae1dSRodney W. Grimes 
1295df8bae1dSRodney W. Grimes 		/*
1296df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1297df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1298df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1299df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1300df8bae1dSRodney W. Grimes 		 */
13019ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
130240447cd4SJohn Baldwin 		if (p->p_wchan && p->p_sflag & PS_SINTR) {
130340447cd4SJohn Baldwin 			if (p->p_sflag & PS_CVWAITQ)
1304238510fcSJason Evans 				cv_waitq_remove(p);
1305238510fcSJason Evans 			else
1306df8bae1dSRodney W. Grimes 				unsleep(p);
1307238510fcSJason Evans 		}
13089ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1309df8bae1dSRodney W. Grimes 		goto out;
1310df8bae1dSRodney W. Grimes 
1311df8bae1dSRodney W. Grimes 	default:
1312df8bae1dSRodney W. Grimes 		/*
1313df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1314df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1315df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1316df8bae1dSRodney W. Grimes 		 */
1317d2ef4060SBruce Evans 		if (p->p_stat == SRUN) {
1318df8bae1dSRodney W. Grimes 			signotify(p);
1319142ba5f3SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
13203163861cSTor Egge #ifdef SMP
13213163861cSTor Egge 			forward_signal(p);
13223163861cSTor Egge #endif
1323d2ef4060SBruce Evans 		} else
13249ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1325df8bae1dSRodney W. Grimes 		goto out;
1326df8bae1dSRodney W. Grimes 	}
1327df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes runfast:
1330df8bae1dSRodney W. Grimes 	/*
1331df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1332df8bae1dSRodney W. Grimes 	 */
13339ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1334d5a08a60SJake Burkholder 	if (p->p_pri.pri_level > PUSER)
1335d5a08a60SJake Burkholder 		p->p_pri.pri_level = PUSER;
1336df8bae1dSRodney W. Grimes run:
13373e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
13383e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1339df8bae1dSRodney W. Grimes 	setrunnable(p);
13409ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
13413e6831f5SJohn Baldwin out:
13423e6831f5SJohn Baldwin 	/* If we jump here, sched_lock should not be owned. */
13433e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1344df8bae1dSRodney W. Grimes }
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes /*
1347df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1348df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1349df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1350df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1351df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1352df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1353df8bae1dSRodney W. Grimes  * sequence is
1354df8bae1dSRodney W. Grimes  *
13552c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
13562c42a146SMarcel Moolenaar  *		postsig(sig);
1357df8bae1dSRodney W. Grimes  */
135826f9a767SRodney W. Grimes int
1359df8bae1dSRodney W. Grimes issignal(p)
1360df8bae1dSRodney W. Grimes 	register struct proc *p;
1361df8bae1dSRodney W. Grimes {
13622c42a146SMarcel Moolenaar 	sigset_t mask;
13632c42a146SMarcel Moolenaar 	register int sig, prop;
1364df8bae1dSRodney W. Grimes 
1365628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1366df8bae1dSRodney W. Grimes 	for (;;) {
13672a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
13682a024a2bSSean Eric Fagan 
13692c42a146SMarcel Moolenaar 		mask = p->p_siglist;
13702c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1371df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
13722c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
13732c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1374df8bae1dSRodney W. Grimes 			return (0);
13752c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
13762c42a146SMarcel Moolenaar 		prop = sigprop(sig);
13772a024a2bSSean Eric Fagan 
1378628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
13792a024a2bSSean Eric Fagan 
1380df8bae1dSRodney W. Grimes 		/*
1381df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1382df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1383df8bae1dSRodney W. Grimes 		 */
13842c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
13852c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1386df8bae1dSRodney W. Grimes 			continue;
1387df8bae1dSRodney W. Grimes 		}
1388df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1389df8bae1dSRodney W. Grimes 			/*
1390df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1391df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1392df8bae1dSRodney W. Grimes 			 */
13932c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1394628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1395df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1396628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
1397df8bae1dSRodney W. Grimes 			do {
1398df8bae1dSRodney W. Grimes 				stop(p);
13999ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
1400628d2653SJohn Baldwin 				PROC_UNLOCK_NOSWITCH(p);
14017da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
1402df8bae1dSRodney W. Grimes 				mi_switch();
14039ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
140420cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1405628d2653SJohn Baldwin 				PROC_LOCK(p);
14062a024a2bSSean Eric Fagan 			} while (!trace_req(p)
14072a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1408df8bae1dSRodney W. Grimes 
1409df8bae1dSRodney W. Grimes 			/*
1410df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1411df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1412df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1413df8bae1dSRodney W. Grimes 			 */
1414df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1415df8bae1dSRodney W. Grimes 				continue;
1416df8bae1dSRodney W. Grimes 
1417df8bae1dSRodney W. Grimes 			/*
1418df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1419df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1420df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1421df8bae1dSRodney W. Grimes 			 */
14222c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
14232c42a146SMarcel Moolenaar 			sig = p->p_xstat;
14242c42a146SMarcel Moolenaar 			if (sig == 0)
1425df8bae1dSRodney W. Grimes 				continue;
1426df8bae1dSRodney W. Grimes 
1427df8bae1dSRodney W. Grimes 			/*
1428df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1429df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1430df8bae1dSRodney W. Grimes 			 */
14312c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
14322c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1433df8bae1dSRodney W. Grimes 				continue;
1434df8bae1dSRodney W. Grimes 		}
1435df8bae1dSRodney W. Grimes 
1436df8bae1dSRodney W. Grimes 		/*
1437df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1438df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1439df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1440df8bae1dSRodney W. Grimes 		 */
14412c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1442df8bae1dSRodney W. Grimes 
14430b53fbe8SBruce Evans 		case (int)SIG_DFL:
1444df8bae1dSRodney W. Grimes 			/*
1445df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1446df8bae1dSRodney W. Grimes 			 */
1447df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1448df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1449df8bae1dSRodney W. Grimes 				/*
1450df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1451df8bae1dSRodney W. Grimes 				 * in init? XXX
1452df8bae1dSRodney W. Grimes 				 */
1453d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
14542c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1455df8bae1dSRodney W. Grimes #endif
1456df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1457df8bae1dSRodney W. Grimes 			}
1458df8bae1dSRodney W. Grimes 			/*
1459df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1460df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1461df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1462df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1463df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1464df8bae1dSRodney W. Grimes 			 */
1465df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1466df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1467df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1468df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1469df8bae1dSRodney W. Grimes 					break;	/* == ignore */
14702c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1471df8bae1dSRodney W. Grimes 				stop(p);
1472628d2653SJohn Baldwin 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) {
1473628d2653SJohn Baldwin 					PROC_LOCK(p->p_pptr);
1474df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1475628d2653SJohn Baldwin 					PROC_UNLOCK(p->p_pptr);
1476628d2653SJohn Baldwin 				}
14779ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
1478628d2653SJohn Baldwin 				PROC_UNLOCK_NOSWITCH(p);
14797da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
1480df8bae1dSRodney W. Grimes 				mi_switch();
14819ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
148220cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1483628d2653SJohn Baldwin 				PROC_LOCK(p);
1484df8bae1dSRodney W. Grimes 				break;
1485df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1486df8bae1dSRodney W. Grimes 				/*
1487df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1488df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1489df8bae1dSRodney W. Grimes 				 */
1490df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1491df8bae1dSRodney W. Grimes 			} else
14922c42a146SMarcel Moolenaar 				return (sig);
1493df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1494df8bae1dSRodney W. Grimes 
14950b53fbe8SBruce Evans 		case (int)SIG_IGN:
1496df8bae1dSRodney W. Grimes 			/*
1497df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1498df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1499df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1500df8bae1dSRodney W. Grimes 			 */
1501df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1502df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1503df8bae1dSRodney W. Grimes 				printf("issignal\n");
1504df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1505df8bae1dSRodney W. Grimes 
1506df8bae1dSRodney W. Grimes 		default:
1507df8bae1dSRodney W. Grimes 			/*
1508df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1509df8bae1dSRodney W. Grimes 			 * postsig() process it.
1510df8bae1dSRodney W. Grimes 			 */
15112c42a146SMarcel Moolenaar 			return (sig);
1512df8bae1dSRodney W. Grimes 		}
15132c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1514df8bae1dSRodney W. Grimes 	}
1515df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1516df8bae1dSRodney W. Grimes }
1517df8bae1dSRodney W. Grimes 
1518df8bae1dSRodney W. Grimes /*
1519df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1520df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
152198f03f90SJake Burkholder  * on the run queue.  Must be called with at least a shared hold of the
152298f03f90SJake Burkholder  * proctree lock.
1523df8bae1dSRodney W. Grimes  */
152426f9a767SRodney W. Grimes void
1525df8bae1dSRodney W. Grimes stop(p)
1526df8bae1dSRodney W. Grimes 	register struct proc *p;
1527df8bae1dSRodney W. Grimes {
1528df8bae1dSRodney W. Grimes 
1529628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
15309ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1531df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1532df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1533df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
15349ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1535df8bae1dSRodney W. Grimes }
1536df8bae1dSRodney W. Grimes 
1537df8bae1dSRodney W. Grimes /*
1538df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1539df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1540df8bae1dSRodney W. Grimes  */
1541df8bae1dSRodney W. Grimes void
15422c42a146SMarcel Moolenaar postsig(sig)
15432c42a146SMarcel Moolenaar 	register int sig;
1544df8bae1dSRodney W. Grimes {
1545df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
1546628d2653SJohn Baldwin 	struct sigacts *ps;
15472c42a146SMarcel Moolenaar 	sig_t action;
15482c42a146SMarcel Moolenaar 	sigset_t returnmask;
15492c42a146SMarcel Moolenaar 	int code;
1550df8bae1dSRodney W. Grimes 
15512c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
15525526d2d9SEivind Eklund 
1553628d2653SJohn Baldwin 	PROC_LOCK(p);
1554628d2653SJohn Baldwin 	ps = p->p_sigacts;
15552c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
15562c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1557df8bae1dSRodney W. Grimes #ifdef KTRACE
1558df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1559645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
15602c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1561df8bae1dSRodney W. Grimes #endif
1562628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
15632a024a2bSSean Eric Fagan 
1564df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1565df8bae1dSRodney W. Grimes 		/*
1566df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1567df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1568df8bae1dSRodney W. Grimes 		 */
15692c42a146SMarcel Moolenaar 		sigexit(p, sig);
1570df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1571df8bae1dSRodney W. Grimes 	} else {
1572df8bae1dSRodney W. Grimes 		/*
1573df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1574df8bae1dSRodney W. Grimes 		 */
15752c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
15765526d2d9SEivind Eklund 		    ("postsig action"));
1577df8bae1dSRodney W. Grimes 		/*
1578df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1579645682fdSLuoqi Chen 		 * occurrences of this signal.
1580df8bae1dSRodney W. Grimes 		 *
1581645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1582df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1583645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1584df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1585df8bae1dSRodney W. Grimes 		 */
1586645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
15876626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1588645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1589df8bae1dSRodney W. Grimes 		} else
1590df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
15912c42a146SMarcel Moolenaar 
15922c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15932c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15942c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
15952c42a146SMarcel Moolenaar 
15962c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1597289ccde0SPeter Wemm 			/*
15982c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1599289ccde0SPeter Wemm 			 */
16002c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
16012c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
16022c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
16032c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
16042c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1605dedc04feSPeter Wemm 		}
1606df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
16072c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1608df8bae1dSRodney W. Grimes 			code = 0;
1609df8bae1dSRodney W. Grimes 		} else {
16106626c604SJulian Elischer 			code = p->p_code;
16116626c604SJulian Elischer 			p->p_code = 0;
16126626c604SJulian Elischer 			p->p_sig = 0;
1613df8bae1dSRodney W. Grimes 		}
1614628d2653SJohn Baldwin 		PROC_UNLOCK(p);
16152c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1616df8bae1dSRodney W. Grimes 	}
1617df8bae1dSRodney W. Grimes }
1618df8bae1dSRodney W. Grimes 
1619df8bae1dSRodney W. Grimes /*
1620df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1621df8bae1dSRodney W. Grimes  */
162226f9a767SRodney W. Grimes void
1623df8bae1dSRodney W. Grimes killproc(p, why)
1624df8bae1dSRodney W. Grimes 	struct proc *p;
1625df8bae1dSRodney W. Grimes 	char *why;
1626df8bae1dSRodney W. Grimes {
16270384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
16280384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1629729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1630729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1631628d2653SJohn Baldwin 	PROC_LOCK(p);
1632df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1633628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1634df8bae1dSRodney W. Grimes }
1635df8bae1dSRodney W. Grimes 
1636df8bae1dSRodney W. Grimes /*
1637df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1638df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1639df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1640df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1641df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1642df8bae1dSRodney W. Grimes  * does not return.
1643df8bae1dSRodney W. Grimes  */
164426f9a767SRodney W. Grimes void
16452c42a146SMarcel Moolenaar sigexit(p, sig)
1646df8bae1dSRodney W. Grimes 	register struct proc *p;
16472c42a146SMarcel Moolenaar 	int sig;
1648df8bae1dSRodney W. Grimes {
1649df8bae1dSRodney W. Grimes 
1650628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1651df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
16522c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
16532c42a146SMarcel Moolenaar 		p->p_sig = sig;
1654c364e17eSAndrey A. Chernov 		/*
1655c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1656c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1657c364e17eSAndrey A. Chernov 		 * these messages.)
1658c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1659c364e17eSAndrey A. Chernov 		 */
1660628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1661c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1662c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1663fca666a1SJulian Elischer 		if (coredump(p) == 0)
16642c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
166557308494SJoerg Wunsch 		if (kern_logsigexit)
166657308494SJoerg Wunsch 			log(LOG_INFO,
166757308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
16683d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
16693d1b21c6SAndrey A. Chernov 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
16702c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
16712c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1672c31146a1SJohn Baldwin 	} else {
1673628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1674628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1675628d2653SJohn Baldwin 			mtx_lock(&Giant);
1676c31146a1SJohn Baldwin 	}
16772c42a146SMarcel Moolenaar 	exit1(p, W_EXITCODE(0, sig));
1678df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1679df8bae1dSRodney W. Grimes }
1680df8bae1dSRodney W. Grimes 
1681c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1682c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1683c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1684c5edb423SSean Eric Fagan 
1685c5edb423SSean Eric Fagan /*
1686c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1687c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1688c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1689c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1690c5edb423SSean Eric Fagan  *	%P	process id (pid)
1691c5edb423SSean Eric Fagan  *	%U	user id (uid)
1692c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1693c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1694c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1695c5edb423SSean Eric Fagan  */
1696c5edb423SSean Eric Fagan 
1697fca666a1SJulian Elischer static char *
1698c5edb423SSean Eric Fagan expand_name(name, uid, pid)
169987f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1700c5edb423SSean Eric Fagan 	char *temp;
1701c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1702c5edb423SSean Eric Fagan 	int i, n;
1703c5edb423SSean Eric Fagan 	char *format = corefilename;
1704ce38ca0fSAlfred Perlstein 	size_t namelen;
1705c5edb423SSean Eric Fagan 
1706ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
17070bfe2990SEivind Eklund 	if (temp == NULL)
17080bfe2990SEivind Eklund 		return NULL;
1709ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1710ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1711c5edb423SSean Eric Fagan 		int l;
1712c5edb423SSean Eric Fagan 		switch (format[i]) {
1713c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1714c5edb423SSean Eric Fagan 			i++;
1715c5edb423SSean Eric Fagan 			switch (format[i]) {
1716c5edb423SSean Eric Fagan 			case '%':
1717c5edb423SSean Eric Fagan 				temp[n++] = '%';
1718c5edb423SSean Eric Fagan 				break;
1719c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1720ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
172187f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1722c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1723c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1724c5edb423SSean Eric Fagan 					return NULL;
1725c5edb423SSean Eric Fagan 				}
1726ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1727ce38ca0fSAlfred Perlstein 				n += namelen;
1728c5edb423SSean Eric Fagan 				break;
1729c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1730ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1731c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
173287f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1733c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1734c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1735c5edb423SSean Eric Fagan 					return NULL;
1736c5edb423SSean Eric Fagan 				}
1737c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1738c5edb423SSean Eric Fagan 				n += l;
1739c5edb423SSean Eric Fagan 				break;
1740c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1741ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1742c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
174387f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1744c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1745c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1746c5edb423SSean Eric Fagan 					return NULL;
1747c5edb423SSean Eric Fagan 				}
1748c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1749c5edb423SSean Eric Fagan 				n += l;
1750c5edb423SSean Eric Fagan 				break;
1751c5edb423SSean Eric Fagan 			default:
1752c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1753c5edb423SSean Eric Fagan 			}
1754c5edb423SSean Eric Fagan 			break;
1755c5edb423SSean Eric Fagan 		default:
1756c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1757c5edb423SSean Eric Fagan 		}
1758c5edb423SSean Eric Fagan 	}
1759ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1760c5edb423SSean Eric Fagan 	return temp;
1761c5edb423SSean Eric Fagan }
1762c5edb423SSean Eric Fagan 
1763df8bae1dSRodney W. Grimes /*
1764fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1765fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1766fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1767fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1768fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1769fca666a1SJulian Elischer  */
1770fca666a1SJulian Elischer 
1771fca666a1SJulian Elischer static int
1772fca666a1SJulian Elischer coredump(p)
1773fca666a1SJulian Elischer 	register struct proc *p;
1774fca666a1SJulian Elischer {
1775fca666a1SJulian Elischer 	register struct vnode *vp;
1776da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
1777fca666a1SJulian Elischer 	struct nameidata nd;
1778fca666a1SJulian Elischer 	struct vattr vattr;
1779e6796b67SKirk McKusick 	int error, error1, flags;
1780f2a2857bSKirk McKusick 	struct mount *mp;
1781fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1782fca666a1SJulian Elischer 	off_t limit;
1783fca666a1SJulian Elischer 
1784628d2653SJohn Baldwin 	PROC_LOCK(p);
1785628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1786fca666a1SJulian Elischer 
1787628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1788628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1789fca666a1SJulian Elischer 		return (EFAULT);
1790628d2653SJohn Baldwin 	}
1791fca666a1SJulian Elischer 
1792fca666a1SJulian Elischer 	/*
179335a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
179435a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
179535a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
179635a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
179735a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
179835a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1799fca666a1SJulian Elischer 	 */
180035a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1801628d2653SJohn Baldwin 	if (limit == 0) {
1802628d2653SJohn Baldwin 		PROC_UNLOCK(p);
180335a2598fSSean Eric Fagan 		return 0;
1804628d2653SJohn Baldwin 	}
1805628d2653SJohn Baldwin 	PROC_UNLOCK(p);
180635a2598fSSean Eric Fagan 
1807f2a2857bSKirk McKusick restart:
1808fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1809fca666a1SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1810e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1811e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1812fca666a1SJulian Elischer 	free(name, M_TEMP);
1813fca666a1SJulian Elischer 	if (error)
1814fca666a1SJulian Elischer 		return (error);
1815762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1816fca666a1SJulian Elischer 	vp = nd.ni_vp;
1817f2a2857bSKirk McKusick 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1818f2a2857bSKirk McKusick 		VOP_UNLOCK(vp, 0, p);
1819f2a2857bSKirk McKusick 		if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
1820f2a2857bSKirk McKusick 			return (error);
1821f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1822f2a2857bSKirk McKusick 			return (error);
1823f2a2857bSKirk McKusick 		goto restart;
1824f2a2857bSKirk McKusick 	}
1825fca666a1SJulian Elischer 
1826fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
1827fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
1828fca666a1SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1829fca666a1SJulian Elischer 		error = EFAULT;
1830fca666a1SJulian Elischer 		goto out;
1831fca666a1SJulian Elischer 	}
1832fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
1833fca666a1SJulian Elischer 	vattr.va_size = 0;
1834fca666a1SJulian Elischer 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1835fca666a1SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, p);
1836628d2653SJohn Baldwin 	PROC_LOCK(p);
1837fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
1838628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1839fca666a1SJulian Elischer 
1840fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
1841fca666a1SJulian Elischer 	  p->p_sysent->sv_coredump(p, vp, limit) :
1842fca666a1SJulian Elischer 	  ENOSYS;
1843fca666a1SJulian Elischer 
1844fca666a1SJulian Elischer out:
1845fca666a1SJulian Elischer 	VOP_UNLOCK(vp, 0, p);
1846f2a2857bSKirk McKusick 	vn_finished_write(mp);
1847fca666a1SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, p);
1848fca666a1SJulian Elischer 	if (error == 0)
1849fca666a1SJulian Elischer 		error = error1;
1850fca666a1SJulian Elischer 	return (error);
1851fca666a1SJulian Elischer }
1852fca666a1SJulian Elischer 
1853fca666a1SJulian Elischer /*
1854df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1855df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1856df8bae1dSRodney W. Grimes  */
1857d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1858df8bae1dSRodney W. Grimes struct nosys_args {
1859df8bae1dSRodney W. Grimes 	int	dummy;
1860df8bae1dSRodney W. Grimes };
1861d2d3e875SBruce Evans #endif
1862df8bae1dSRodney W. Grimes /* ARGSUSED */
186326f9a767SRodney W. Grimes int
1864cb226aaaSPoul-Henning Kamp nosys(p, args)
1865df8bae1dSRodney W. Grimes 	struct proc *p;
1866df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1867df8bae1dSRodney W. Grimes {
1868df8bae1dSRodney W. Grimes 
1869628d2653SJohn Baldwin 	PROC_LOCK(p);
1870df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1871628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1872df8bae1dSRodney W. Grimes 	return (EINVAL);
1873df8bae1dSRodney W. Grimes }
1874831d27a9SDon Lewis 
1875831d27a9SDon Lewis /*
1876831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1877831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1878831d27a9SDon Lewis  */
1879831d27a9SDon Lewis void
18802c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
1881831d27a9SDon Lewis 	struct sigio *sigio;
18822c42a146SMarcel Moolenaar 	int sig, checkctty;
1883831d27a9SDon Lewis {
1884831d27a9SDon Lewis 	if (sigio == NULL)
1885831d27a9SDon Lewis 		return;
1886831d27a9SDon Lewis 
1887831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1888628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
1889831d27a9SDon Lewis 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1890831d27a9SDon Lewis 		             sigio->sio_proc))
18912c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
1892628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
1893831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1894831d27a9SDon Lewis 		struct proc *p;
1895831d27a9SDon Lewis 
1896628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
1897628d2653SJohn Baldwin 			PROC_LOCK(p);
1898831d27a9SDon Lewis 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1899831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
19002c42a146SMarcel Moolenaar 				psignal(p, sig);
1901628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1902628d2653SJohn Baldwin 		}
1903831d27a9SDon Lewis 	}
1904831d27a9SDon Lewis }
1905cb679c38SJonathan Lemon 
1906cb679c38SJonathan Lemon static int
1907cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
1908cb679c38SJonathan Lemon {
1909cb679c38SJonathan Lemon 	struct proc *p = curproc;
1910cb679c38SJonathan Lemon 
1911cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
1912cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1913cb679c38SJonathan Lemon 
1914628d2653SJohn Baldwin 	PROC_LOCK(p);
1915cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1916628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1917cb679c38SJonathan Lemon 
1918cb679c38SJonathan Lemon 	return (0);
1919cb679c38SJonathan Lemon }
1920cb679c38SJonathan Lemon 
1921cb679c38SJonathan Lemon static void
1922cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
1923cb679c38SJonathan Lemon {
1924cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
1925cb679c38SJonathan Lemon 
1926628d2653SJohn Baldwin 	PROC_LOCK(p);
1927e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1928628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1929cb679c38SJonathan Lemon }
1930cb679c38SJonathan Lemon 
1931cb679c38SJonathan Lemon /*
1932cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
1933cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
1934cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
1935cb679c38SJonathan Lemon  * isn't worth the trouble.
1936cb679c38SJonathan Lemon  */
1937cb679c38SJonathan Lemon static int
1938cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
1939cb679c38SJonathan Lemon {
1940cb679c38SJonathan Lemon 
1941cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
1942cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
1943cb679c38SJonathan Lemon 
1944cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
1945cb679c38SJonathan Lemon 			kn->kn_data++;
1946cb679c38SJonathan Lemon 	}
1947cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
1948cb679c38SJonathan Lemon }
1949