xref: /freebsd/sys/kern/kern_sig.c (revision aa7a4dae6dac90cc7142de2483bd99638a60f1ae)
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>
57238510fcSJason Evans #include <sys/condvar.h>
58c31146a1SJohn Baldwin #include <sys/lock.h>
5935e0e5b3SJohn Baldwin #include <sys/mutex.h>
60df8bae1dSRodney W. Grimes #include <sys/wait.h>
610384fff8SJason Evans #include <sys/ktr.h>
62df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
63c31146a1SJohn Baldwin #include <sys/resourcevar.h>
646caa8a15SJohn Baldwin #include <sys/smp.h>
65df8bae1dSRodney W. Grimes #include <sys/stat.h>
661005a129SJohn Baldwin #include <sys/sx.h>
671005a129SJohn Baldwin #include <sys/syslog.h>
68d66a5066SPeter Wemm #include <sys/sysent.h>
69c87e2930SDavid Greenman #include <sys/sysctl.h>
70c5edb423SSean Eric Fagan #include <sys/malloc.h>
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes #include <machine/cpu.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 /*
100b1fc0ec1SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
101831d27a9SDon Lewis  */
102b1fc0ec1SRobert Watson #define CANSIGIO(cr1, cr2) \
103b1fc0ec1SRobert Watson 	((cr1)->cr_uid == 0 || \
104b1fc0ec1SRobert Watson 	    (cr2)->cr_ruid == (cr2)->cr_ruid || \
105b1fc0ec1SRobert Watson 	    (cr2)->cr_uid == (cr2)->cr_ruid || \
106b1fc0ec1SRobert Watson 	    (cr2)->cr_ruid == (cr2)->cr_uid || \
107b1fc0ec1SRobert Watson 	    (cr2)->cr_uid == (cr2)->cr_uid)
108831d27a9SDon Lewis 
10922d4b0fbSJohn Polstra int sugid_coredump;
1103d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1113d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
112c87e2930SDavid Greenman 
113e5a28db9SPaul Saab static int	do_coredump = 1;
114e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
115e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
116e5a28db9SPaul Saab 
1172c42a146SMarcel Moolenaar /*
1182c42a146SMarcel Moolenaar  * Signal properties and actions.
1192c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1202c42a146SMarcel Moolenaar  * according to the following properties:
1212c42a146SMarcel Moolenaar  */
1222c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1232c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1242c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1252c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1262c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1272c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1282c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
129df8bae1dSRodney W. Grimes 
1302c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1312c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1322c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1332c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1342c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1352c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1362c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1372c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1382c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1392c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1412c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1432c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1442c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1452c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1462c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1472c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1482c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1492c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1502c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1512c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1522c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1532c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1542c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1552c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1562c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1572c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1582c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1592c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1602c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1612c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1622c42a146SMarcel Moolenaar };
1632c42a146SMarcel Moolenaar 
164fbbeeb6cSBruce Evans /*
165fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
166fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
167fbbeeb6cSBruce Evans  * action, the process stops in issignal().
168fbbeeb6cSBruce Evans  *
16933510ef1SBruce Evans  * MP SAFE.
170fbbeeb6cSBruce Evans  */
171fbbeeb6cSBruce Evans int
172fbbeeb6cSBruce Evans CURSIG(struct proc *p)
173fbbeeb6cSBruce Evans {
174fbbeeb6cSBruce Evans 	sigset_t tmpset;
175fbbeeb6cSBruce Evans 
1762ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
17733510ef1SBruce Evans 	if (SIGISEMPTY(p->p_siglist))
1782ad7d304SJohn Baldwin 		return (0);
179fbbeeb6cSBruce Evans 	tmpset = p->p_siglist;
180fbbeeb6cSBruce Evans 	SIGSETNAND(tmpset, p->p_sigmask);
18133510ef1SBruce Evans 	if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
1822ad7d304SJohn Baldwin 		return (0);
1832ad7d304SJohn Baldwin 	return (issignal(p));
184fbbeeb6cSBruce Evans }
185fbbeeb6cSBruce Evans 
1866f841fb7SMarcel Moolenaar static __inline int
1876f841fb7SMarcel Moolenaar sigprop(int sig)
1882c42a146SMarcel Moolenaar {
1896f841fb7SMarcel Moolenaar 
1902c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
1912c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
1922c42a146SMarcel Moolenaar 	return (0);
193df8bae1dSRodney W. Grimes }
1942c42a146SMarcel Moolenaar 
1956f841fb7SMarcel Moolenaar static __inline int
1966f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
1972c42a146SMarcel Moolenaar {
1982c42a146SMarcel Moolenaar 	int i;
1992c42a146SMarcel Moolenaar 
2006f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2012c42a146SMarcel Moolenaar 		if (set->__bits[i])
2022c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
203df8bae1dSRodney W. Grimes 	return (0);
204df8bae1dSRodney W. Grimes }
205df8bae1dSRodney W. Grimes 
2062c42a146SMarcel Moolenaar /*
2072c42a146SMarcel Moolenaar  * do_sigaction
2082c42a146SMarcel Moolenaar  * sigaction
2092c42a146SMarcel Moolenaar  * osigaction
2102c42a146SMarcel Moolenaar  */
2112c42a146SMarcel Moolenaar static int
212645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
2132c42a146SMarcel Moolenaar 	struct proc *p;
2142c42a146SMarcel Moolenaar 	register int sig;
2152c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
216645682fdSLuoqi Chen 	int old;
217df8bae1dSRodney W. Grimes {
218628d2653SJohn Baldwin 	register struct sigacts *ps;
219df8bae1dSRodney W. Grimes 
2202c42a146SMarcel Moolenaar 	if (sig <= 0 || sig > _SIG_MAXSIG)
2212c42a146SMarcel Moolenaar 		return (EINVAL);
2222c42a146SMarcel Moolenaar 
223628d2653SJohn Baldwin 	PROC_LOCK(p);
224628d2653SJohn Baldwin 	ps = p->p_sigacts;
2252c42a146SMarcel Moolenaar 	if (oact) {
2262c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2272c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2282c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2292c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2302c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2312c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2322c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2332c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2342c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2352c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2362c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2372c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2382c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
239645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2402c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
241645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2422c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2432c42a146SMarcel Moolenaar 	}
2442c42a146SMarcel Moolenaar 	if (act) {
2452c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
246628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
247628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2482c42a146SMarcel Moolenaar 			return (EINVAL);
249628d2653SJohn Baldwin 		}
2502c42a146SMarcel Moolenaar 
251df8bae1dSRodney W. Grimes 		/*
252df8bae1dSRodney W. Grimes 		 * Change setting atomically.
253df8bae1dSRodney W. Grimes 		 */
2542c42a146SMarcel Moolenaar 
2552c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2562c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2572c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
258aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2594fec48c6SMatthew Dillon 			SIGADDSET(ps->ps_siginfo, sig);
2604fec48c6SMatthew Dillon 		} else {
261aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
262aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
2632c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2642c42a146SMarcel Moolenaar 		}
2652c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2662c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
267df8bae1dSRodney W. Grimes 		else
2682c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2692c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2702c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
271df8bae1dSRodney W. Grimes 		else
2722c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2732c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2742c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2751e41c1b5SSteven Wallace 		else
2762c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2772c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2782c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
279289ccde0SPeter Wemm 		else
2802c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
281df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2822c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2832c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
284df8bae1dSRodney W. Grimes 		else
2852c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_usertramp, seg);
286df8bae1dSRodney W. Grimes #endif
2872c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2882c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
289645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
2906626c604SJulian Elischer 			else
291645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
292aa7a4daeSPeter Wemm 			if ((act->sa_flags & SA_NOCLDWAIT) ||
293aa7a4daeSPeter Wemm 			    ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) {
294245f17d4SJoerg Wunsch 				/*
2952c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
2962c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
2972c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
2982c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
299245f17d4SJoerg Wunsch 				 */
300245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
301645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3026626c604SJulian Elischer 				else
303645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
304245f17d4SJoerg Wunsch 			} else
305645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
306df8bae1dSRodney W. Grimes 		}
307df8bae1dSRodney W. Grimes 		/*
308df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3092c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3102c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3112c42a146SMarcel Moolenaar 		 * have to restart the process.
312df8bae1dSRodney W. Grimes 		 */
3132c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3142c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3152c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3162c42a146SMarcel Moolenaar 			/* never to be seen again */
3172c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3182c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3192c42a146SMarcel Moolenaar 				/* easier in psignal */
3202c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3212c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
322645682fdSLuoqi Chen 		} else {
3232c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3242c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3252c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3262c42a146SMarcel Moolenaar 			else
3272c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3282c42a146SMarcel Moolenaar 		}
329645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
330645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
331645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
332645682fdSLuoqi Chen 		else
333645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
334df8bae1dSRodney W. Grimes 	}
335628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3362c42a146SMarcel Moolenaar 	return (0);
3372c42a146SMarcel Moolenaar }
3382c42a146SMarcel Moolenaar 
3392c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3402c42a146SMarcel Moolenaar struct sigaction_args {
3412c42a146SMarcel Moolenaar 	int	sig;
3422c42a146SMarcel Moolenaar 	struct	sigaction *act;
3432c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3442c42a146SMarcel Moolenaar };
3452c42a146SMarcel Moolenaar #endif
3462c42a146SMarcel Moolenaar /* ARGSUSED */
3472c42a146SMarcel Moolenaar int
3482c42a146SMarcel Moolenaar sigaction(p, uap)
3492c42a146SMarcel Moolenaar 	struct proc *p;
3502c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3512c42a146SMarcel Moolenaar {
3522c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3532c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3542c42a146SMarcel Moolenaar 	int error;
3552c42a146SMarcel Moolenaar 
3566f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3576f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3582c42a146SMarcel Moolenaar 	if (actp) {
3596f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3602c42a146SMarcel Moolenaar 		if (error)
3612c42a146SMarcel Moolenaar 			return (error);
3622c42a146SMarcel Moolenaar 	}
363645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3642c42a146SMarcel Moolenaar 	if (oactp && !error) {
3656f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3662c42a146SMarcel Moolenaar 	}
3672c42a146SMarcel Moolenaar 	return (error);
3682c42a146SMarcel Moolenaar }
3692c42a146SMarcel Moolenaar 
37031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3712c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3722c42a146SMarcel Moolenaar struct osigaction_args {
3732c42a146SMarcel Moolenaar 	int	signum;
3742c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3752c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3762c42a146SMarcel Moolenaar };
3772c42a146SMarcel Moolenaar #endif
3782c42a146SMarcel Moolenaar /* ARGSUSED */
3792c42a146SMarcel Moolenaar int
3802c42a146SMarcel Moolenaar osigaction(p, uap)
3812c42a146SMarcel Moolenaar 	struct proc *p;
3822c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3832c42a146SMarcel Moolenaar {
3842c42a146SMarcel Moolenaar 	struct osigaction sa;
3852c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
3862c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
3872c42a146SMarcel Moolenaar 	int error;
3882c42a146SMarcel Moolenaar 
3896f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
3906f841fb7SMarcel Moolenaar 		return (EINVAL);
3916f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
3926f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
3932c42a146SMarcel Moolenaar 	if (nsap) {
3946f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
3952c42a146SMarcel Moolenaar 		if (error)
3962c42a146SMarcel Moolenaar 			return (error);
3972c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
3982c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
3992c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4002c42a146SMarcel Moolenaar 	}
401645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
4022c42a146SMarcel Moolenaar 	if (osap && !error) {
4032c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4042c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4052c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4066f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4072c42a146SMarcel Moolenaar 	}
4082c42a146SMarcel Moolenaar 	return (error);
4092c42a146SMarcel Moolenaar }
41031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes /*
413df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
414df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
415df8bae1dSRodney W. Grimes  */
416df8bae1dSRodney W. Grimes void
417df8bae1dSRodney W. Grimes siginit(p)
418df8bae1dSRodney W. Grimes 	struct proc *p;
419df8bae1dSRodney W. Grimes {
420df8bae1dSRodney W. Grimes 	register int i;
421df8bae1dSRodney W. Grimes 
422628d2653SJohn Baldwin 	PROC_LOCK(p);
4232c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4242c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4252c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
426628d2653SJohn Baldwin 	PROC_UNLOCK(p);
427df8bae1dSRodney W. Grimes }
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes /*
430df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
431df8bae1dSRodney W. Grimes  */
432df8bae1dSRodney W. Grimes void
433df8bae1dSRodney W. Grimes execsigs(p)
434df8bae1dSRodney W. Grimes 	register struct proc *p;
435df8bae1dSRodney W. Grimes {
436628d2653SJohn Baldwin 	register struct sigacts *ps;
4372c42a146SMarcel Moolenaar 	register int sig;
438df8bae1dSRodney W. Grimes 
439df8bae1dSRodney W. Grimes 	/*
440df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
441df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
442df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
443df8bae1dSRodney W. Grimes 	 */
444628d2653SJohn Baldwin 	PROC_LOCK(p);
445628d2653SJohn Baldwin 	ps = p->p_sigacts;
4462c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4472c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4482c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4492c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4502c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4512c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4522c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
453df8bae1dSRodney W. Grimes 		}
4542c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
455df8bae1dSRodney W. Grimes 	}
456df8bae1dSRodney W. Grimes 	/*
457df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
458df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
459df8bae1dSRodney W. Grimes 	 */
460645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
461645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
462645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
4633b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
46480e907a1SPeter Wemm 	/*
46580e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
46680e907a1SPeter Wemm 	 */
467645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
468c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
469c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
470628d2653SJohn Baldwin 	PROC_UNLOCK(p);
471df8bae1dSRodney W. Grimes }
472df8bae1dSRodney W. Grimes 
473df8bae1dSRodney W. Grimes /*
474628d2653SJohn Baldwin  * do_sigprocmask()
4757c8fdcbdSMatthew Dillon  *
476628d2653SJohn Baldwin  *	Manipulate signal mask.
477df8bae1dSRodney W. Grimes  */
4782c42a146SMarcel Moolenaar static int
479645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
4802c42a146SMarcel Moolenaar 	struct proc *p;
4812c42a146SMarcel Moolenaar 	int how;
4822c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
483645682fdSLuoqi Chen 	int old;
4842c42a146SMarcel Moolenaar {
4852c42a146SMarcel Moolenaar 	int error;
4862c42a146SMarcel Moolenaar 
487628d2653SJohn Baldwin 	PROC_LOCK(p);
4882c42a146SMarcel Moolenaar 	if (oset != NULL)
4892c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
4902c42a146SMarcel Moolenaar 
4912c42a146SMarcel Moolenaar 	error = 0;
4922c42a146SMarcel Moolenaar 	if (set != NULL) {
4932c42a146SMarcel Moolenaar 		switch (how) {
4942c42a146SMarcel Moolenaar 		case SIG_BLOCK:
495645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
4962c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
4972c42a146SMarcel Moolenaar 			break;
4982c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
4992c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
5002c42a146SMarcel Moolenaar 			break;
5012c42a146SMarcel Moolenaar 		case SIG_SETMASK:
502645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
503645682fdSLuoqi Chen 			if (old)
504645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
505645682fdSLuoqi Chen 			else
5062c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
5072c42a146SMarcel Moolenaar 			break;
5082c42a146SMarcel Moolenaar 		default:
5092c42a146SMarcel Moolenaar 			error = EINVAL;
5102c42a146SMarcel Moolenaar 			break;
5112c42a146SMarcel Moolenaar 		}
5122c42a146SMarcel Moolenaar 	}
513628d2653SJohn Baldwin 	PROC_UNLOCK(p);
5142c42a146SMarcel Moolenaar 	return (error);
5152c42a146SMarcel Moolenaar }
5162c42a146SMarcel Moolenaar 
5177c8fdcbdSMatthew Dillon /*
5187c8fdcbdSMatthew Dillon  * sigprocmask() - MP SAFE
5197c8fdcbdSMatthew Dillon  */
5207c8fdcbdSMatthew Dillon 
521d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
522df8bae1dSRodney W. Grimes struct sigprocmask_args {
523df8bae1dSRodney W. Grimes 	int	how;
5242c42a146SMarcel Moolenaar 	const sigset_t *set;
5252c42a146SMarcel Moolenaar 	sigset_t *oset;
526df8bae1dSRodney W. Grimes };
527d2d3e875SBruce Evans #endif
52826f9a767SRodney W. Grimes int
529cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
530df8bae1dSRodney W. Grimes 	register struct proc *p;
531df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
532df8bae1dSRodney W. Grimes {
5332c42a146SMarcel Moolenaar 	sigset_t set, oset;
5342c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5352c42a146SMarcel Moolenaar 	int error;
536df8bae1dSRodney W. Grimes 
5376f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5386f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5392c42a146SMarcel Moolenaar 	if (setp) {
5406f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5412c42a146SMarcel Moolenaar 		if (error)
5422c42a146SMarcel Moolenaar 			return (error);
543df8bae1dSRodney W. Grimes 	}
544645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5452c42a146SMarcel Moolenaar 	if (osetp && !error) {
5466f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5472c42a146SMarcel Moolenaar 	}
5482c42a146SMarcel Moolenaar 	return (error);
5492c42a146SMarcel Moolenaar }
5502c42a146SMarcel Moolenaar 
55131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5527c8fdcbdSMatthew Dillon /*
5537c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5547c8fdcbdSMatthew Dillon  */
5552c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5562c42a146SMarcel Moolenaar struct osigprocmask_args {
5572c42a146SMarcel Moolenaar 	int	how;
5582c42a146SMarcel Moolenaar 	osigset_t mask;
5592c42a146SMarcel Moolenaar };
5602c42a146SMarcel Moolenaar #endif
5612c42a146SMarcel Moolenaar int
5622c42a146SMarcel Moolenaar osigprocmask(p, uap)
5632c42a146SMarcel Moolenaar 	register struct proc *p;
5642c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5652c42a146SMarcel Moolenaar {
5662c42a146SMarcel Moolenaar 	sigset_t set, oset;
5672c42a146SMarcel Moolenaar 	int error;
5682c42a146SMarcel Moolenaar 
5692c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
570645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
5712c42a146SMarcel Moolenaar 	SIG2OSIG(oset, p->p_retval[0]);
572df8bae1dSRodney W. Grimes 	return (error);
573df8bae1dSRodney W. Grimes }
57431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
575df8bae1dSRodney W. Grimes 
576d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
577df8bae1dSRodney W. Grimes struct sigpending_args {
5782c42a146SMarcel Moolenaar 	sigset_t	*set;
579df8bae1dSRodney W. Grimes };
580d2d3e875SBruce Evans #endif
581df8bae1dSRodney W. Grimes /* ARGSUSED */
58226f9a767SRodney W. Grimes int
583cb226aaaSPoul-Henning Kamp sigpending(p, uap)
584df8bae1dSRodney W. Grimes 	struct proc *p;
585df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
586df8bae1dSRodney W. Grimes {
587628d2653SJohn Baldwin 	sigset_t siglist;
588df8bae1dSRodney W. Grimes 
589628d2653SJohn Baldwin 	PROC_LOCK(p);
590628d2653SJohn Baldwin 	siglist = p->p_siglist;
591628d2653SJohn Baldwin 	PROC_UNLOCK(p);
592628d2653SJohn Baldwin 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
5932c42a146SMarcel Moolenaar }
5942c42a146SMarcel Moolenaar 
59531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5962c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5972c42a146SMarcel Moolenaar struct osigpending_args {
5982c42a146SMarcel Moolenaar 	int	dummy;
5992c42a146SMarcel Moolenaar };
6002c42a146SMarcel Moolenaar #endif
6012c42a146SMarcel Moolenaar /* ARGSUSED */
6022c42a146SMarcel Moolenaar int
6032c42a146SMarcel Moolenaar osigpending(p, uap)
6042c42a146SMarcel Moolenaar 	struct proc *p;
6052c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6062c42a146SMarcel Moolenaar {
6072c42a146SMarcel Moolenaar 
608628d2653SJohn Baldwin 	PROC_LOCK(p);
6092c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_siglist, p->p_retval[0]);
610628d2653SJohn Baldwin 	PROC_UNLOCK(p);
611df8bae1dSRodney W. Grimes 	return (0);
612df8bae1dSRodney W. Grimes }
61331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
616df8bae1dSRodney W. Grimes /*
617df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
618df8bae1dSRodney W. Grimes  */
619d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
620df8bae1dSRodney W. Grimes struct osigvec_args {
621df8bae1dSRodney W. Grimes 	int	signum;
622df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
623df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
624df8bae1dSRodney W. Grimes };
625d2d3e875SBruce Evans #endif
626df8bae1dSRodney W. Grimes /* ARGSUSED */
62726f9a767SRodney W. Grimes int
628cb226aaaSPoul-Henning Kamp osigvec(p, uap)
629df8bae1dSRodney W. Grimes 	struct proc *p;
630df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
631df8bae1dSRodney W. Grimes {
632df8bae1dSRodney W. Grimes 	struct sigvec vec;
6332c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6342c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6352c42a146SMarcel Moolenaar 	int error;
636df8bae1dSRodney W. Grimes 
6376f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6386f841fb7SMarcel Moolenaar 		return (EINVAL);
6396f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6406f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6412c42a146SMarcel Moolenaar 	if (nsap) {
6426f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
6432c42a146SMarcel Moolenaar 		if (error)
644df8bae1dSRodney W. Grimes 			return (error);
6452c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
6462c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
6472c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
6482c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
649df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
6502c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
651df8bae1dSRodney W. Grimes #endif
652df8bae1dSRodney W. Grimes 	}
653645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
6542c42a146SMarcel Moolenaar 	if (osap && !error) {
6552c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
6562c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
6572c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
6582c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
6592c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
6602c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
6612c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
6622c42a146SMarcel Moolenaar #endif
6636f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
6642c42a146SMarcel Moolenaar 	}
6652c42a146SMarcel Moolenaar 	return (error);
666df8bae1dSRodney W. Grimes }
667df8bae1dSRodney W. Grimes 
668d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
669df8bae1dSRodney W. Grimes struct osigblock_args {
670df8bae1dSRodney W. Grimes 	int	mask;
671df8bae1dSRodney W. Grimes };
672d2d3e875SBruce Evans #endif
67326f9a767SRodney W. Grimes int
674cb226aaaSPoul-Henning Kamp osigblock(p, uap)
675df8bae1dSRodney W. Grimes 	register struct proc *p;
676df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
677df8bae1dSRodney W. Grimes {
6782c42a146SMarcel Moolenaar 	sigset_t set;
679df8bae1dSRodney W. Grimes 
6802c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6812c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
682628d2653SJohn Baldwin 	PROC_LOCK(p);
6832c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
6842c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
685628d2653SJohn Baldwin 	PROC_UNLOCK(p);
686df8bae1dSRodney W. Grimes 	return (0);
687df8bae1dSRodney W. Grimes }
688df8bae1dSRodney W. Grimes 
689d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
690df8bae1dSRodney W. Grimes struct osigsetmask_args {
691df8bae1dSRodney W. Grimes 	int	mask;
692df8bae1dSRodney W. Grimes };
693d2d3e875SBruce Evans #endif
69426f9a767SRodney W. Grimes int
695cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
696df8bae1dSRodney W. Grimes 	struct proc *p;
697df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
698df8bae1dSRodney W. Grimes {
6992c42a146SMarcel Moolenaar 	sigset_t set;
700df8bae1dSRodney W. Grimes 
7012c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7022c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
703628d2653SJohn Baldwin 	PROC_LOCK(p);
7042c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
705645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
706628d2653SJohn Baldwin 	PROC_UNLOCK(p);
707df8bae1dSRodney W. Grimes 	return (0);
708df8bae1dSRodney W. Grimes }
709df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes /*
712df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
713df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
714df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
715df8bae1dSRodney W. Grimes  */
716d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
717df8bae1dSRodney W. Grimes struct sigsuspend_args {
7182c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
719df8bae1dSRodney W. Grimes };
720d2d3e875SBruce Evans #endif
721df8bae1dSRodney W. Grimes /* ARGSUSED */
72226f9a767SRodney W. Grimes int
723cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
724df8bae1dSRodney W. Grimes 	register struct proc *p;
725df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
726df8bae1dSRodney W. Grimes {
7272c42a146SMarcel Moolenaar 	sigset_t mask;
728628d2653SJohn Baldwin 	register struct sigacts *ps;
7292c42a146SMarcel Moolenaar 	int error;
7302c42a146SMarcel Moolenaar 
7316f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
7322c42a146SMarcel Moolenaar 	if (error)
7332c42a146SMarcel Moolenaar 		return (error);
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes 	/*
736645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
737df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
738df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
739df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
740df8bae1dSRodney W. Grimes 	 * to indicate this.
741df8bae1dSRodney W. Grimes 	 */
742628d2653SJohn Baldwin 	PROC_LOCK(p);
743628d2653SJohn Baldwin 	ps = p->p_sigacts;
7446626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
745645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
746645682fdSLuoqi Chen 
747645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
7482c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
749628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
7502c42a146SMarcel Moolenaar 		/* void */;
751628d2653SJohn Baldwin 	PROC_UNLOCK(p);
7522c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
7532c42a146SMarcel Moolenaar 	return (EINTR);
7542c42a146SMarcel Moolenaar }
7552c42a146SMarcel Moolenaar 
75631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7572c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7582c42a146SMarcel Moolenaar struct osigsuspend_args {
7592c42a146SMarcel Moolenaar 	osigset_t mask;
7602c42a146SMarcel Moolenaar };
7612c42a146SMarcel Moolenaar #endif
7622c42a146SMarcel Moolenaar /* ARGSUSED */
7632c42a146SMarcel Moolenaar int
7642c42a146SMarcel Moolenaar osigsuspend(p, uap)
7652c42a146SMarcel Moolenaar 	register struct proc *p;
7662c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
7672c42a146SMarcel Moolenaar {
768645682fdSLuoqi Chen 	sigset_t mask;
769628d2653SJohn Baldwin 	register struct sigacts *ps;
7702c42a146SMarcel Moolenaar 
771628d2653SJohn Baldwin 	PROC_LOCK(p);
772628d2653SJohn Baldwin 	ps = p->p_sigacts;
7732c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
774645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
775645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
776645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
777645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
778628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
779df8bae1dSRodney W. Grimes 		/* void */;
780628d2653SJohn Baldwin 	PROC_UNLOCK(p);
781df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
782df8bae1dSRodney W. Grimes 	return (EINTR);
783df8bae1dSRodney W. Grimes }
78431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
787d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
788df8bae1dSRodney W. Grimes struct osigstack_args {
789df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
790df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
791df8bae1dSRodney W. Grimes };
792d2d3e875SBruce Evans #endif
793df8bae1dSRodney W. Grimes /* ARGSUSED */
79426f9a767SRodney W. Grimes int
795cb226aaaSPoul-Henning Kamp osigstack(p, uap)
796df8bae1dSRodney W. Grimes 	struct proc *p;
797df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
798df8bae1dSRodney W. Grimes {
799df8bae1dSRodney W. Grimes 	struct sigstack ss;
800d034d459SMarcel Moolenaar 	int error;
801df8bae1dSRodney W. Grimes 
802d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
803628d2653SJohn Baldwin 		PROC_LOCK(p);
804645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
805d034d459SMarcel Moolenaar 		ss.ss_onstack = sigonstack(cpu_getstack(p));
806628d2653SJohn Baldwin 		PROC_UNLOCK(p);
807d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
808d034d459SMarcel Moolenaar 		if (error)
809df8bae1dSRodney W. Grimes 			return (error);
810d034d459SMarcel Moolenaar 	}
811d034d459SMarcel Moolenaar 
812d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
813d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
814d034d459SMarcel Moolenaar 			return (error);
815628d2653SJohn Baldwin 		PROC_LOCK(p);
816645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
817645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
818645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
819645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
820628d2653SJohn Baldwin 		PROC_UNLOCK(p);
821df8bae1dSRodney W. Grimes 	}
822d034d459SMarcel Moolenaar 	return (0);
823df8bae1dSRodney W. Grimes }
824df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
825df8bae1dSRodney W. Grimes 
826d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
827df8bae1dSRodney W. Grimes struct sigaltstack_args {
8282c42a146SMarcel Moolenaar 	stack_t	*ss;
8292c42a146SMarcel Moolenaar 	stack_t	*oss;
830df8bae1dSRodney W. Grimes };
831d2d3e875SBruce Evans #endif
832df8bae1dSRodney W. Grimes /* ARGSUSED */
83326f9a767SRodney W. Grimes int
834cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
835df8bae1dSRodney W. Grimes 	struct proc *p;
836df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
837df8bae1dSRodney W. Grimes {
8382c42a146SMarcel Moolenaar 	stack_t ss;
839d034d459SMarcel Moolenaar 	int error, oonstack;
840df8bae1dSRodney W. Grimes 
841d034d459SMarcel Moolenaar 	oonstack = sigonstack(cpu_getstack(p));
842d034d459SMarcel Moolenaar 
843d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
844628d2653SJohn Baldwin 		PROC_LOCK(p);
845d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
846d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
847d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
848628d2653SJohn Baldwin 		PROC_UNLOCK(p);
849d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
850df8bae1dSRodney W. Grimes 			return (error);
851df8bae1dSRodney W. Grimes 	}
852d034d459SMarcel Moolenaar 
853d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
854d034d459SMarcel Moolenaar 		if (oonstack)
855d034d459SMarcel Moolenaar 			return (EPERM);
856d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
857d034d459SMarcel Moolenaar 			return (error);
858d034d459SMarcel Moolenaar 		if ((ss.ss_flags & ~SS_DISABLE) != 0)
859d034d459SMarcel Moolenaar 			return (EINVAL);
860d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
861806d7daaSMarcel Moolenaar 			if (ss.ss_size < p->p_sysent->sv_minsigstksz)
862df8bae1dSRodney W. Grimes 				return (ENOMEM);
863628d2653SJohn Baldwin 			PROC_LOCK(p);
864645682fdSLuoqi Chen 			p->p_sigstk = ss;
865d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
866628d2653SJohn Baldwin 			PROC_UNLOCK(p);
867628d2653SJohn Baldwin 		} else {
868628d2653SJohn Baldwin 			PROC_LOCK(p);
869d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
870628d2653SJohn Baldwin 			PROC_UNLOCK(p);
871628d2653SJohn Baldwin 		}
872d034d459SMarcel Moolenaar 	}
873df8bae1dSRodney W. Grimes 	return (0);
874df8bae1dSRodney W. Grimes }
875df8bae1dSRodney W. Grimes 
876d93f860cSPoul-Henning Kamp /*
877d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
878d93f860cSPoul-Henning Kamp  * cp is calling process.
879d93f860cSPoul-Henning Kamp  */
880d93f860cSPoul-Henning Kamp int
8812c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
882d93f860cSPoul-Henning Kamp 	register struct proc *cp;
8832c42a146SMarcel Moolenaar 	int sig, pgid, all;
884d93f860cSPoul-Henning Kamp {
885d93f860cSPoul-Henning Kamp 	register struct proc *p;
886d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
887d93f860cSPoul-Henning Kamp 	int nfound = 0;
888d93f860cSPoul-Henning Kamp 
889553629ebSJake Burkholder 	if (all) {
890d93f860cSPoul-Henning Kamp 		/*
891d93f860cSPoul-Henning Kamp 		 * broadcast
892d93f860cSPoul-Henning Kamp 		 */
8931005a129SJohn Baldwin 		sx_slock(&allproc_lock);
8942e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
895628d2653SJohn Baldwin 			PROC_LOCK(p);
896628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) {
897628d2653SJohn Baldwin 				PROC_UNLOCK(p);
898628d2653SJohn Baldwin 				continue;
899628d2653SJohn Baldwin 			}
90033a9ed9dSJohn Baldwin 			if (p_cansignal(cp, p, sig) == 0) {
901d93f860cSPoul-Henning Kamp 				nfound++;
90233a9ed9dSJohn Baldwin 				if (sig)
9032c42a146SMarcel Moolenaar 					psignal(p, sig);
904628d2653SJohn Baldwin 			}
90533a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
906d93f860cSPoul-Henning Kamp 		}
9071005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
908553629ebSJake Burkholder 	} else {
909d93f860cSPoul-Henning Kamp 		if (pgid == 0)
910d93f860cSPoul-Henning Kamp 			/*
911d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
912d93f860cSPoul-Henning Kamp 			 */
913d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
914d93f860cSPoul-Henning Kamp 		else {
915d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
916d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
917d93f860cSPoul-Henning Kamp 				return (ESRCH);
918d93f860cSPoul-Henning Kamp 		}
9192e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
920628d2653SJohn Baldwin 			PROC_LOCK(p);
921628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
922628d2653SJohn Baldwin 				PROC_UNLOCK(p);
923628d2653SJohn Baldwin 				continue;
924628d2653SJohn Baldwin 			}
925628d2653SJohn Baldwin 			mtx_lock_spin(&sched_lock);
926628d2653SJohn Baldwin 			if (p->p_stat == SZOMB) {
927628d2653SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
92833a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
929628d2653SJohn Baldwin 				continue;
930628d2653SJohn Baldwin 			}
931628d2653SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
93233a9ed9dSJohn Baldwin 			if (p_cansignal(cp, p, sig) == 0) {
933d93f860cSPoul-Henning Kamp 				nfound++;
93433a9ed9dSJohn Baldwin 				if (sig)
9352c42a146SMarcel Moolenaar 					psignal(p, sig);
936628d2653SJohn Baldwin 			}
93733a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
938d93f860cSPoul-Henning Kamp 		}
939d93f860cSPoul-Henning Kamp 	}
940d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
941d93f860cSPoul-Henning Kamp }
942d93f860cSPoul-Henning Kamp 
943d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
944df8bae1dSRodney W. Grimes struct kill_args {
945df8bae1dSRodney W. Grimes 	int	pid;
946df8bae1dSRodney W. Grimes 	int	signum;
947df8bae1dSRodney W. Grimes };
948d2d3e875SBruce Evans #endif
949df8bae1dSRodney W. Grimes /* ARGSUSED */
95026f9a767SRodney W. Grimes int
951cb226aaaSPoul-Henning Kamp kill(cp, uap)
952df8bae1dSRodney W. Grimes 	register struct proc *cp;
953df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
954df8bae1dSRodney W. Grimes {
955df8bae1dSRodney W. Grimes 	register struct proc *p;
956df8bae1dSRodney W. Grimes 
9572c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
958df8bae1dSRodney W. Grimes 		return (EINVAL);
959df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
960df8bae1dSRodney W. Grimes 		/* kill single process */
961df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
962df8bae1dSRodney W. Grimes 			return (ESRCH);
96333a9ed9dSJohn Baldwin 		if (p_cansignal(cp, p, uap->signum)) {
96433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
965df8bae1dSRodney W. Grimes 			return (EPERM);
96633a9ed9dSJohn Baldwin 		}
96733a9ed9dSJohn Baldwin 		if (uap->signum)
968df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
969628d2653SJohn Baldwin 		PROC_UNLOCK(p);
970df8bae1dSRodney W. Grimes 		return (0);
971df8bae1dSRodney W. Grimes 	}
972df8bae1dSRodney W. Grimes 	switch (uap->pid) {
973df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
974df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
975df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
976df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
977df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
978df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
979df8bae1dSRodney W. Grimes 	}
980df8bae1dSRodney W. Grimes 	/* NOTREACHED */
981df8bae1dSRodney W. Grimes }
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
984d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
985df8bae1dSRodney W. Grimes struct okillpg_args {
986df8bae1dSRodney W. Grimes 	int	pgid;
987df8bae1dSRodney W. Grimes 	int	signum;
988df8bae1dSRodney W. Grimes };
989d2d3e875SBruce Evans #endif
990df8bae1dSRodney W. Grimes /* ARGSUSED */
99126f9a767SRodney W. Grimes int
992cb226aaaSPoul-Henning Kamp okillpg(p, uap)
993df8bae1dSRodney W. Grimes 	struct proc *p;
994df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
995df8bae1dSRodney W. Grimes {
996df8bae1dSRodney W. Grimes 
9972c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
998df8bae1dSRodney W. Grimes 		return (EINVAL);
999df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
1000df8bae1dSRodney W. Grimes }
1001df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1002df8bae1dSRodney W. Grimes 
1003df8bae1dSRodney W. Grimes /*
1004df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1005df8bae1dSRodney W. Grimes  */
1006df8bae1dSRodney W. Grimes void
10072c42a146SMarcel Moolenaar gsignal(pgid, sig)
10082c42a146SMarcel Moolenaar 	int pgid, sig;
1009df8bae1dSRodney W. Grimes {
1010df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1011df8bae1dSRodney W. Grimes 
1012df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
10132c42a146SMarcel Moolenaar 		pgsignal(pgrp, sig, 0);
1014df8bae1dSRodney W. Grimes }
1015df8bae1dSRodney W. Grimes 
1016df8bae1dSRodney W. Grimes /*
1017df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1018df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1019df8bae1dSRodney W. Grimes  */
1020df8bae1dSRodney W. Grimes void
10212c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1022df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
10232c42a146SMarcel Moolenaar 	int sig, checkctty;
1024df8bae1dSRodney W. Grimes {
1025df8bae1dSRodney W. Grimes 	register struct proc *p;
1026df8bae1dSRodney W. Grimes 
1027628d2653SJohn Baldwin 	if (pgrp) {
1028628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1029628d2653SJohn Baldwin 			PROC_LOCK(p);
1030df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
10312c42a146SMarcel Moolenaar 				psignal(p, sig);
1032628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1033628d2653SJohn Baldwin 		}
1034628d2653SJohn Baldwin 	}
1035df8bae1dSRodney W. Grimes }
1036df8bae1dSRodney W. Grimes 
1037df8bae1dSRodney W. Grimes /*
1038df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1039df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1040df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1041df8bae1dSRodney W. Grimes  */
1042df8bae1dSRodney W. Grimes void
10432c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1044df8bae1dSRodney W. Grimes 	struct proc *p;
10452c42a146SMarcel Moolenaar 	register int sig;
10468674077aSJeffrey Hsu 	u_long code;
1047df8bae1dSRodney W. Grimes {
1048df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1049df8bae1dSRodney W. Grimes 
1050628d2653SJohn Baldwin 	PROC_LOCK(p);
10512c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1052d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1053df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1054df8bae1dSRodney W. Grimes #ifdef KTRACE
1055df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
10562c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
10572c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1058df8bae1dSRodney W. Grimes #endif
1059628d2653SJohn Baldwin 		PROC_UNLOCK(p);	/* XXX ??? */
10602c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
10612c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
1062628d2653SJohn Baldwin 		PROC_LOCK(p);
10632c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
10642c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
10652c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
10662c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1067289ccde0SPeter Wemm 			/*
10682c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1069289ccde0SPeter Wemm 			 */
10702c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
10712c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
10722c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
10732c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
10742c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1075dedc04feSPeter Wemm 		}
10766f841fb7SMarcel Moolenaar 	} else {
10776626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
10782c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
10792c42a146SMarcel Moolenaar 		psignal(p, sig);
1080df8bae1dSRodney W. Grimes 	}
1081628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1082df8bae1dSRodney W. Grimes }
1083df8bae1dSRodney W. Grimes 
1084df8bae1dSRodney W. Grimes /*
1085df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1086df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1087df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1088df8bae1dSRodney W. Grimes  *
1089df8bae1dSRodney W. Grimes  * Exceptions:
1090df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1091df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1092df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1093df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1094df8bae1dSRodney W. Grimes  *
1095df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1096df8bae1dSRodney W. Grimes  */
1097df8bae1dSRodney W. Grimes void
10982c42a146SMarcel Moolenaar psignal(p, sig)
1099df8bae1dSRodney W. Grimes 	register struct proc *p;
11002c42a146SMarcel Moolenaar 	register int sig;
1101df8bae1dSRodney W. Grimes {
110240447cd4SJohn Baldwin 	register int prop;
1103df8bae1dSRodney W. Grimes 	register sig_t action;
1104df8bae1dSRodney W. Grimes 
11052c42a146SMarcel Moolenaar 	if (sig > _SIG_MAXSIG || sig <= 0) {
11062c42a146SMarcel Moolenaar 		printf("psignal: signal %d\n", sig);
1107df8bae1dSRodney W. Grimes 		panic("psignal signal number");
11082a024a2bSSean Eric Fagan 	}
11092c42a146SMarcel Moolenaar 
1110628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1111cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1112cb679c38SJonathan Lemon 
11132c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes 	/*
11162a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
11172a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
11182a024a2bSSean Eric Fagan 	 * a chance, as well.
1119df8bae1dSRodney W. Grimes 	 */
11202a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1121df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1122df8bae1dSRodney W. Grimes 	else {
1123df8bae1dSRodney W. Grimes 		/*
1124df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1125df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1126df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1127df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1128df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1129df8bae1dSRodney W. Grimes 		 */
1130628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1131df8bae1dSRodney W. Grimes 			return;
11322c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1133df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
11342c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1135df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1136df8bae1dSRodney W. Grimes 		else
1137df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1138df8bae1dSRodney W. Grimes 	}
1139df8bae1dSRodney W. Grimes 
11409ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1141df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1142df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
1143df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
11449ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1145df8bae1dSRodney W. Grimes 
1146df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
11472c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1150df8bae1dSRodney W. Grimes 		/*
1151df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1152df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1153df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1154df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1155df8bae1dSRodney W. Grimes 		 */
1156df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1157628d2653SJohn Baldwin 		    action == SIG_DFL)
1158df8bae1dSRodney W. Grimes 		        return;
11592c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1160df8bae1dSRodney W. Grimes 	}
11612c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1162df8bae1dSRodney W. Grimes 
1163df8bae1dSRodney W. Grimes 	/*
1164df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1165df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1166df8bae1dSRodney W. Grimes 	 */
11679ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
11681c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
11699ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1170df8bae1dSRodney W. Grimes 		return;
11711c32c37cSJohn Baldwin 	}
1172df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1173df8bae1dSRodney W. Grimes 
1174df8bae1dSRodney W. Grimes 	case SSLEEP:
1175df8bae1dSRodney W. Grimes 		/*
1176df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1177df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1178df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1179df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1180df8bae1dSRodney W. Grimes 		 */
118140447cd4SJohn Baldwin 		if ((p->p_sflag & PS_SINTR) == 0) {
11829ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1183df8bae1dSRodney W. Grimes 			goto out;
11843e6831f5SJohn Baldwin 		}
1185df8bae1dSRodney W. Grimes 		/*
1186df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1187df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1188df8bae1dSRodney W. Grimes 		 * for the parent.
1189df8bae1dSRodney W. Grimes 		 */
1190df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1191df8bae1dSRodney W. Grimes 			goto run;
11929ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1193df8bae1dSRodney W. Grimes 		/*
1194df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1195df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1196df8bae1dSRodney W. Grimes 		 * be awakened.
1197df8bae1dSRodney W. Grimes 		 */
1198df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
11992c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1200df8bae1dSRodney W. Grimes 			goto out;
1201df8bae1dSRodney W. Grimes 		}
1202df8bae1dSRodney W. Grimes 		/*
1203df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1204df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1205df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1206df8bae1dSRodney W. Grimes 		 * cause the process to run.
1207df8bae1dSRodney W. Grimes 		 */
1208df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
12093e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1210df8bae1dSRodney W. Grimes 				goto runfast;
1211df8bae1dSRodney W. Grimes 			/*
1212df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1213df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1214df8bae1dSRodney W. Grimes 			 */
1215df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1216df8bae1dSRodney W. Grimes 				goto out;
12172c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
12182c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1219628d2653SJohn Baldwin 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) {
1220628d2653SJohn Baldwin 				PROC_LOCK(p->p_pptr);
1221df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1222628d2653SJohn Baldwin 				PROC_UNLOCK(p->p_pptr);
1223628d2653SJohn Baldwin 			}
12245b3047d5SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1225df8bae1dSRodney W. Grimes 			stop(p);
12265b3047d5SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
1227df8bae1dSRodney W. Grimes 			goto out;
12283e6831f5SJohn Baldwin 		} else
1229df8bae1dSRodney W. Grimes 			goto runfast;
1230df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes 	case SSTOP:
12339ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1234df8bae1dSRodney W. Grimes 		/*
1235df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1236df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1237df8bae1dSRodney W. Grimes 		 */
123840447cd4SJohn Baldwin 		if (p->p_flag & P_TRACED)
1239df8bae1dSRodney W. Grimes 			goto out;
1240df8bae1dSRodney W. Grimes 
1241df8bae1dSRodney W. Grimes 		/*
1242df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1243df8bae1dSRodney W. Grimes 		 */
12442c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1245df8bae1dSRodney W. Grimes 			goto runfast;
1246df8bae1dSRodney W. Grimes 
1247df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1248df8bae1dSRodney W. Grimes 			/*
1249df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1250df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1251df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1252df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1253df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1254df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1255df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1256df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1257df8bae1dSRodney W. Grimes 			 */
1258df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
12592c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1260df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1261df8bae1dSRodney W. Grimes 				goto runfast;
12629ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
126340447cd4SJohn Baldwin 			if (p->p_wchan == NULL)
1264df8bae1dSRodney W. Grimes 				goto run;
1265df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
12669ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1267df8bae1dSRodney W. Grimes 			goto out;
1268df8bae1dSRodney W. Grimes 		}
1269df8bae1dSRodney W. Grimes 
1270df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1271df8bae1dSRodney W. Grimes 			/*
1272df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1273df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1274df8bae1dSRodney W. Grimes 			 */
12752c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1276df8bae1dSRodney W. Grimes 			goto out;
1277df8bae1dSRodney W. Grimes 		}
1278df8bae1dSRodney W. Grimes 
1279df8bae1dSRodney W. Grimes 		/*
1280df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1281df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1282df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1283df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1284df8bae1dSRodney W. Grimes 		 */
12859ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
128640447cd4SJohn Baldwin 		if (p->p_wchan && p->p_sflag & PS_SINTR) {
128740447cd4SJohn Baldwin 			if (p->p_sflag & PS_CVWAITQ)
1288238510fcSJason Evans 				cv_waitq_remove(p);
1289238510fcSJason Evans 			else
1290df8bae1dSRodney W. Grimes 				unsleep(p);
1291238510fcSJason Evans 		}
12929ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1293df8bae1dSRodney W. Grimes 		goto out;
1294df8bae1dSRodney W. Grimes 
1295df8bae1dSRodney W. Grimes 	default:
1296df8bae1dSRodney W. Grimes 		/*
1297df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1298df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1299df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1300df8bae1dSRodney W. Grimes 		 */
1301d2ef4060SBruce Evans 		if (p->p_stat == SRUN) {
1302df8bae1dSRodney W. Grimes 			signotify(p);
13033163861cSTor Egge #ifdef SMP
13043163861cSTor Egge 			forward_signal(p);
13053163861cSTor Egge #endif
13066caa8a15SJohn Baldwin 		}
13079ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1308df8bae1dSRodney W. Grimes 		goto out;
1309df8bae1dSRodney W. Grimes 	}
1310df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1311df8bae1dSRodney W. Grimes 
1312df8bae1dSRodney W. Grimes runfast:
1313df8bae1dSRodney W. Grimes 	/*
1314df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1315df8bae1dSRodney W. Grimes 	 */
13169ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1317d5a08a60SJake Burkholder 	if (p->p_pri.pri_level > PUSER)
1318d5a08a60SJake Burkholder 		p->p_pri.pri_level = PUSER;
1319df8bae1dSRodney W. Grimes run:
13203e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
13213e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1322df8bae1dSRodney W. Grimes 	setrunnable(p);
13239ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
13243e6831f5SJohn Baldwin out:
13253e6831f5SJohn Baldwin 	/* If we jump here, sched_lock should not be owned. */
13263e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1327df8bae1dSRodney W. Grimes }
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes /*
1330df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1331df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1332df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1333df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1334df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1335df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1336df8bae1dSRodney W. Grimes  * sequence is
1337df8bae1dSRodney W. Grimes  *
13382c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
13392c42a146SMarcel Moolenaar  *		postsig(sig);
1340df8bae1dSRodney W. Grimes  */
134126f9a767SRodney W. Grimes int
1342df8bae1dSRodney W. Grimes issignal(p)
1343df8bae1dSRodney W. Grimes 	register struct proc *p;
1344df8bae1dSRodney W. Grimes {
13452c42a146SMarcel Moolenaar 	sigset_t mask;
13462c42a146SMarcel Moolenaar 	register int sig, prop;
1347df8bae1dSRodney W. Grimes 
1348628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1349df8bae1dSRodney W. Grimes 	for (;;) {
13502a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
13512a024a2bSSean Eric Fagan 
13522c42a146SMarcel Moolenaar 		mask = p->p_siglist;
13532c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1354df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
13552c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
13562c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1357df8bae1dSRodney W. Grimes 			return (0);
13582c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
13592c42a146SMarcel Moolenaar 		prop = sigprop(sig);
13602a024a2bSSean Eric Fagan 
1361628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
13622a024a2bSSean Eric Fagan 
1363df8bae1dSRodney W. Grimes 		/*
1364df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1365df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1366df8bae1dSRodney W. Grimes 		 */
13672c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
13682c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1369df8bae1dSRodney W. Grimes 			continue;
1370df8bae1dSRodney W. Grimes 		}
1371df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1372df8bae1dSRodney W. Grimes 			/*
1373df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1374df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1375df8bae1dSRodney W. Grimes 			 */
13762c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1377628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1378df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1379628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
1380df8bae1dSRodney W. Grimes 			do {
13819ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
13825b3047d5SJohn Baldwin 				stop(p);
1383628d2653SJohn Baldwin 				PROC_UNLOCK_NOSWITCH(p);
13847da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
13852ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1386df8bae1dSRodney W. Grimes 				mi_switch();
13879ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
138820cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1389628d2653SJohn Baldwin 				PROC_LOCK(p);
13902a024a2bSSean Eric Fagan 			} while (!trace_req(p)
13912a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1392df8bae1dSRodney W. Grimes 
1393df8bae1dSRodney W. Grimes 			/*
1394df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1395df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1396df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1397df8bae1dSRodney W. Grimes 			 */
1398df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1399df8bae1dSRodney W. Grimes 				continue;
1400df8bae1dSRodney W. Grimes 
1401df8bae1dSRodney W. Grimes 			/*
1402df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1403df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1404df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1405df8bae1dSRodney W. Grimes 			 */
14062c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
14072c42a146SMarcel Moolenaar 			sig = p->p_xstat;
14082c42a146SMarcel Moolenaar 			if (sig == 0)
1409df8bae1dSRodney W. Grimes 				continue;
1410df8bae1dSRodney W. Grimes 
1411df8bae1dSRodney W. Grimes 			/*
1412df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1413df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1414df8bae1dSRodney W. Grimes 			 */
14152c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
14162c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1417df8bae1dSRodney W. Grimes 				continue;
1418df8bae1dSRodney W. Grimes 		}
1419df8bae1dSRodney W. Grimes 
1420df8bae1dSRodney W. Grimes 		/*
1421df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1422df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1423df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1424df8bae1dSRodney W. Grimes 		 */
14252c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1426df8bae1dSRodney W. Grimes 
14270b53fbe8SBruce Evans 		case (int)SIG_DFL:
1428df8bae1dSRodney W. Grimes 			/*
1429df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1430df8bae1dSRodney W. Grimes 			 */
1431df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1432df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1433df8bae1dSRodney W. Grimes 				/*
1434df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1435df8bae1dSRodney W. Grimes 				 * in init? XXX
1436df8bae1dSRodney W. Grimes 				 */
1437d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
14382c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1439df8bae1dSRodney W. Grimes #endif
1440df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1441df8bae1dSRodney W. Grimes 			}
1442df8bae1dSRodney W. Grimes 			/*
1443df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1444df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1445df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1446df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1447df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1448df8bae1dSRodney W. Grimes 			 */
1449df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1450df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1451df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1452df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1453df8bae1dSRodney W. Grimes 					break;	/* == ignore */
14542c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1455628d2653SJohn Baldwin 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) {
1456628d2653SJohn Baldwin 					PROC_LOCK(p->p_pptr);
1457df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1458628d2653SJohn Baldwin 					PROC_UNLOCK(p->p_pptr);
1459628d2653SJohn Baldwin 				}
14609ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
14615b3047d5SJohn Baldwin 				stop(p);
1462628d2653SJohn Baldwin 				PROC_UNLOCK_NOSWITCH(p);
14637da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
14642ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1465df8bae1dSRodney W. Grimes 				mi_switch();
14669ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
146720cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1468628d2653SJohn Baldwin 				PROC_LOCK(p);
1469df8bae1dSRodney W. Grimes 				break;
1470df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1471df8bae1dSRodney W. Grimes 				/*
1472df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1473df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1474df8bae1dSRodney W. Grimes 				 */
1475df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1476df8bae1dSRodney W. Grimes 			} else
14772c42a146SMarcel Moolenaar 				return (sig);
1478df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1479df8bae1dSRodney W. Grimes 
14800b53fbe8SBruce Evans 		case (int)SIG_IGN:
1481df8bae1dSRodney W. Grimes 			/*
1482df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1483df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1484df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1485df8bae1dSRodney W. Grimes 			 */
1486df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1487df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1488df8bae1dSRodney W. Grimes 				printf("issignal\n");
1489df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1490df8bae1dSRodney W. Grimes 
1491df8bae1dSRodney W. Grimes 		default:
1492df8bae1dSRodney W. Grimes 			/*
1493df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1494df8bae1dSRodney W. Grimes 			 * postsig() process it.
1495df8bae1dSRodney W. Grimes 			 */
14962c42a146SMarcel Moolenaar 			return (sig);
1497df8bae1dSRodney W. Grimes 		}
14982c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1499df8bae1dSRodney W. Grimes 	}
1500df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1501df8bae1dSRodney W. Grimes }
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes /*
1504df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1505df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
15065b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
15075b3047d5SJohn Baldwin  * lock held.
1508df8bae1dSRodney W. Grimes  */
15095b3047d5SJohn Baldwin static void
1510df8bae1dSRodney W. Grimes stop(p)
1511df8bae1dSRodney W. Grimes 	register struct proc *p;
1512df8bae1dSRodney W. Grimes {
1513df8bae1dSRodney W. Grimes 
1514628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
15155b3047d5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
1516df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1517df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1518df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1519df8bae1dSRodney W. Grimes }
1520df8bae1dSRodney W. Grimes 
1521df8bae1dSRodney W. Grimes /*
1522df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1523df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1524df8bae1dSRodney W. Grimes  */
1525df8bae1dSRodney W. Grimes void
15262c42a146SMarcel Moolenaar postsig(sig)
15272c42a146SMarcel Moolenaar 	register int sig;
1528df8bae1dSRodney W. Grimes {
1529df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
1530628d2653SJohn Baldwin 	struct sigacts *ps;
15312c42a146SMarcel Moolenaar 	sig_t action;
15322c42a146SMarcel Moolenaar 	sigset_t returnmask;
15332c42a146SMarcel Moolenaar 	int code;
1534df8bae1dSRodney W. Grimes 
15352c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
15365526d2d9SEivind Eklund 
15372ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1538628d2653SJohn Baldwin 	ps = p->p_sigacts;
15392c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
15402c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1541df8bae1dSRodney W. Grimes #ifdef KTRACE
1542df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1543645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
15442c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1545df8bae1dSRodney W. Grimes #endif
1546628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
15472a024a2bSSean Eric Fagan 
1548df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1549df8bae1dSRodney W. Grimes 		/*
1550df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1551df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1552df8bae1dSRodney W. Grimes 		 */
15532c42a146SMarcel Moolenaar 		sigexit(p, sig);
1554df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1555df8bae1dSRodney W. Grimes 	} else {
1556df8bae1dSRodney W. Grimes 		/*
1557df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1558df8bae1dSRodney W. Grimes 		 */
15592c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
15605526d2d9SEivind Eklund 		    ("postsig action"));
1561df8bae1dSRodney W. Grimes 		/*
1562df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1563645682fdSLuoqi Chen 		 * occurrences of this signal.
1564df8bae1dSRodney W. Grimes 		 *
1565645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1566df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1567645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1568df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1569df8bae1dSRodney W. Grimes 		 */
1570645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
15716626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1572645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1573df8bae1dSRodney W. Grimes 		} else
1574df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
15752c42a146SMarcel Moolenaar 
15762c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15772c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15782c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
15792c42a146SMarcel Moolenaar 
15802c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1581289ccde0SPeter Wemm 			/*
15822c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1583289ccde0SPeter Wemm 			 */
15842c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
15852c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15862c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
15872c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
15882c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1589dedc04feSPeter Wemm 		}
1590df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
15912c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1592df8bae1dSRodney W. Grimes 			code = 0;
1593df8bae1dSRodney W. Grimes 		} else {
15946626c604SJulian Elischer 			code = p->p_code;
15956626c604SJulian Elischer 			p->p_code = 0;
15966626c604SJulian Elischer 			p->p_sig = 0;
1597df8bae1dSRodney W. Grimes 		}
1598628d2653SJohn Baldwin 		PROC_UNLOCK(p);
15992c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
16002ad7d304SJohn Baldwin 		PROC_LOCK(p);
1601df8bae1dSRodney W. Grimes 	}
1602df8bae1dSRodney W. Grimes }
1603df8bae1dSRodney W. Grimes 
1604df8bae1dSRodney W. Grimes /*
1605df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1606df8bae1dSRodney W. Grimes  */
160726f9a767SRodney W. Grimes void
1608df8bae1dSRodney W. Grimes killproc(p, why)
1609df8bae1dSRodney W. Grimes 	struct proc *p;
1610df8bae1dSRodney W. Grimes 	char *why;
1611df8bae1dSRodney W. Grimes {
16129081e5e8SJohn Baldwin 
16139081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
16140384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
16150384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1616729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1617b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1618df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1619df8bae1dSRodney W. Grimes }
1620df8bae1dSRodney W. Grimes 
1621df8bae1dSRodney W. Grimes /*
1622df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1623df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1624df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1625df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1626df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1627df8bae1dSRodney W. Grimes  * does not return.
1628df8bae1dSRodney W. Grimes  */
162926f9a767SRodney W. Grimes void
16302c42a146SMarcel Moolenaar sigexit(p, sig)
1631df8bae1dSRodney W. Grimes 	register struct proc *p;
16322c42a146SMarcel Moolenaar 	int sig;
1633df8bae1dSRodney W. Grimes {
1634df8bae1dSRodney W. Grimes 
1635628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1636df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
16372c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
16382c42a146SMarcel Moolenaar 		p->p_sig = sig;
1639c364e17eSAndrey A. Chernov 		/*
1640c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1641c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1642c364e17eSAndrey A. Chernov 		 * these messages.)
1643c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1644c364e17eSAndrey A. Chernov 		 */
1645628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1646c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1647c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1648fca666a1SJulian Elischer 		if (coredump(p) == 0)
16492c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
165057308494SJoerg Wunsch 		if (kern_logsigexit)
165157308494SJoerg Wunsch 			log(LOG_INFO,
165257308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
16533d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
1654b1fc0ec1SRobert Watson 			    p->p_ucred ? p->p_ucred->cr_uid : -1,
16552c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
16562c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1657c31146a1SJohn Baldwin 	} else {
1658628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1659628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1660628d2653SJohn Baldwin 			mtx_lock(&Giant);
1661c31146a1SJohn Baldwin 	}
16622c42a146SMarcel Moolenaar 	exit1(p, W_EXITCODE(0, sig));
1663df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1664df8bae1dSRodney W. Grimes }
1665df8bae1dSRodney W. Grimes 
1666c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1667c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1668c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1669c5edb423SSean Eric Fagan 
1670c5edb423SSean Eric Fagan /*
1671c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1672c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1673c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1674c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1675c5edb423SSean Eric Fagan  *	%P	process id (pid)
1676c5edb423SSean Eric Fagan  *	%U	user id (uid)
1677c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1678c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1679c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1680c5edb423SSean Eric Fagan  */
1681c5edb423SSean Eric Fagan 
1682fca666a1SJulian Elischer static char *
1683c5edb423SSean Eric Fagan expand_name(name, uid, pid)
168487f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1685c5edb423SSean Eric Fagan 	char *temp;
1686c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1687c5edb423SSean Eric Fagan 	int i, n;
1688c5edb423SSean Eric Fagan 	char *format = corefilename;
1689ce38ca0fSAlfred Perlstein 	size_t namelen;
1690c5edb423SSean Eric Fagan 
1691ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
16920bfe2990SEivind Eklund 	if (temp == NULL)
16930bfe2990SEivind Eklund 		return NULL;
1694ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1695ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1696c5edb423SSean Eric Fagan 		int l;
1697c5edb423SSean Eric Fagan 		switch (format[i]) {
1698c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1699c5edb423SSean Eric Fagan 			i++;
1700c5edb423SSean Eric Fagan 			switch (format[i]) {
1701c5edb423SSean Eric Fagan 			case '%':
1702c5edb423SSean Eric Fagan 				temp[n++] = '%';
1703c5edb423SSean Eric Fagan 				break;
1704c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1705ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
170687f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1707c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1708c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1709c5edb423SSean Eric Fagan 					return NULL;
1710c5edb423SSean Eric Fagan 				}
1711ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1712ce38ca0fSAlfred Perlstein 				n += namelen;
1713c5edb423SSean Eric Fagan 				break;
1714c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1715ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1716c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
171787f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1718c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1719c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1720c5edb423SSean Eric Fagan 					return NULL;
1721c5edb423SSean Eric Fagan 				}
1722c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1723c5edb423SSean Eric Fagan 				n += l;
1724c5edb423SSean Eric Fagan 				break;
1725c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1726ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1727c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
172887f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1729c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1730c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1731c5edb423SSean Eric Fagan 					return NULL;
1732c5edb423SSean Eric Fagan 				}
1733c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1734c5edb423SSean Eric Fagan 				n += l;
1735c5edb423SSean Eric Fagan 				break;
1736c5edb423SSean Eric Fagan 			default:
1737c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1738c5edb423SSean Eric Fagan 			}
1739c5edb423SSean Eric Fagan 			break;
1740c5edb423SSean Eric Fagan 		default:
1741c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1742c5edb423SSean Eric Fagan 		}
1743c5edb423SSean Eric Fagan 	}
1744ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1745c5edb423SSean Eric Fagan 	return temp;
1746c5edb423SSean Eric Fagan }
1747c5edb423SSean Eric Fagan 
1748df8bae1dSRodney W. Grimes /*
1749fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1750fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1751fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1752fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1753fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1754fca666a1SJulian Elischer  */
1755fca666a1SJulian Elischer 
1756fca666a1SJulian Elischer static int
1757fca666a1SJulian Elischer coredump(p)
1758fca666a1SJulian Elischer 	register struct proc *p;
1759fca666a1SJulian Elischer {
1760fca666a1SJulian Elischer 	register struct vnode *vp;
1761da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
1762fca666a1SJulian Elischer 	struct nameidata nd;
1763fca666a1SJulian Elischer 	struct vattr vattr;
1764e6796b67SKirk McKusick 	int error, error1, flags;
1765f2a2857bSKirk McKusick 	struct mount *mp;
1766fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1767fca666a1SJulian Elischer 	off_t limit;
1768fca666a1SJulian Elischer 
1769628d2653SJohn Baldwin 	PROC_LOCK(p);
1770628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1771fca666a1SJulian Elischer 
1772628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1773628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1774fca666a1SJulian Elischer 		return (EFAULT);
1775628d2653SJohn Baldwin 	}
1776fca666a1SJulian Elischer 
1777fca666a1SJulian Elischer 	/*
177835a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
177935a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
178035a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
178135a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
178235a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
178335a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1784fca666a1SJulian Elischer 	 */
178535a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1786628d2653SJohn Baldwin 	if (limit == 0) {
1787628d2653SJohn Baldwin 		PROC_UNLOCK(p);
178835a2598fSSean Eric Fagan 		return 0;
1789628d2653SJohn Baldwin 	}
1790628d2653SJohn Baldwin 	PROC_UNLOCK(p);
179135a2598fSSean Eric Fagan 
1792f2a2857bSKirk McKusick restart:
1793fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1794fca666a1SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1795e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1796e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1797fca666a1SJulian Elischer 	free(name, M_TEMP);
1798fca666a1SJulian Elischer 	if (error)
1799fca666a1SJulian Elischer 		return (error);
1800762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1801fca666a1SJulian Elischer 	vp = nd.ni_vp;
1802f2a2857bSKirk McKusick 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1803f2a2857bSKirk McKusick 		VOP_UNLOCK(vp, 0, p);
1804f2a2857bSKirk McKusick 		if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
1805f2a2857bSKirk McKusick 			return (error);
1806f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1807f2a2857bSKirk McKusick 			return (error);
1808f2a2857bSKirk McKusick 		goto restart;
1809f2a2857bSKirk McKusick 	}
1810fca666a1SJulian Elischer 
1811fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
1812fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
1813fca666a1SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1814fca666a1SJulian Elischer 		error = EFAULT;
1815fca666a1SJulian Elischer 		goto out;
1816fca666a1SJulian Elischer 	}
1817fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
1818fca666a1SJulian Elischer 	vattr.va_size = 0;
1819fca666a1SJulian Elischer 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1820fca666a1SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, p);
1821628d2653SJohn Baldwin 	PROC_LOCK(p);
1822fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
1823628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1824fca666a1SJulian Elischer 
1825fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
1826fca666a1SJulian Elischer 	  p->p_sysent->sv_coredump(p, vp, limit) :
1827fca666a1SJulian Elischer 	  ENOSYS;
1828fca666a1SJulian Elischer 
1829fca666a1SJulian Elischer out:
1830fca666a1SJulian Elischer 	VOP_UNLOCK(vp, 0, p);
1831f2a2857bSKirk McKusick 	vn_finished_write(mp);
1832fca666a1SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, p);
1833fca666a1SJulian Elischer 	if (error == 0)
1834fca666a1SJulian Elischer 		error = error1;
1835fca666a1SJulian Elischer 	return (error);
1836fca666a1SJulian Elischer }
1837fca666a1SJulian Elischer 
1838fca666a1SJulian Elischer /*
1839df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1840df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1841df8bae1dSRodney W. Grimes  */
1842d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1843df8bae1dSRodney W. Grimes struct nosys_args {
1844df8bae1dSRodney W. Grimes 	int	dummy;
1845df8bae1dSRodney W. Grimes };
1846d2d3e875SBruce Evans #endif
1847df8bae1dSRodney W. Grimes /* ARGSUSED */
184826f9a767SRodney W. Grimes int
1849cb226aaaSPoul-Henning Kamp nosys(p, args)
1850df8bae1dSRodney W. Grimes 	struct proc *p;
1851df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1852df8bae1dSRodney W. Grimes {
1853df8bae1dSRodney W. Grimes 
1854628d2653SJohn Baldwin 	PROC_LOCK(p);
1855df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1856628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1857df8bae1dSRodney W. Grimes 	return (EINVAL);
1858df8bae1dSRodney W. Grimes }
1859831d27a9SDon Lewis 
1860831d27a9SDon Lewis /*
1861831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1862831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1863831d27a9SDon Lewis  */
1864831d27a9SDon Lewis void
18652c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
1866831d27a9SDon Lewis 	struct sigio *sigio;
18672c42a146SMarcel Moolenaar 	int sig, checkctty;
1868831d27a9SDon Lewis {
1869831d27a9SDon Lewis 	if (sigio == NULL)
1870831d27a9SDon Lewis 		return;
1871831d27a9SDon Lewis 
1872831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1873628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
1874b1fc0ec1SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
18752c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
1876628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
1877831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1878831d27a9SDon Lewis 		struct proc *p;
1879831d27a9SDon Lewis 
1880628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
1881628d2653SJohn Baldwin 			PROC_LOCK(p);
1882b1fc0ec1SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
1883831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
18842c42a146SMarcel Moolenaar 				psignal(p, sig);
1885628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1886628d2653SJohn Baldwin 		}
1887831d27a9SDon Lewis 	}
1888831d27a9SDon Lewis }
1889cb679c38SJonathan Lemon 
1890cb679c38SJonathan Lemon static int
1891cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
1892cb679c38SJonathan Lemon {
1893cb679c38SJonathan Lemon 	struct proc *p = curproc;
1894cb679c38SJonathan Lemon 
1895cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
1896cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1897cb679c38SJonathan Lemon 
1898628d2653SJohn Baldwin 	PROC_LOCK(p);
1899cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1900628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1901cb679c38SJonathan Lemon 
1902cb679c38SJonathan Lemon 	return (0);
1903cb679c38SJonathan Lemon }
1904cb679c38SJonathan Lemon 
1905cb679c38SJonathan Lemon static void
1906cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
1907cb679c38SJonathan Lemon {
1908cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
1909cb679c38SJonathan Lemon 
1910628d2653SJohn Baldwin 	PROC_LOCK(p);
1911e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1912628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1913cb679c38SJonathan Lemon }
1914cb679c38SJonathan Lemon 
1915cb679c38SJonathan Lemon /*
1916cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
1917cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
1918cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
1919cb679c38SJonathan Lemon  * isn't worth the trouble.
1920cb679c38SJonathan Lemon  */
1921cb679c38SJonathan Lemon static int
1922cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
1923cb679c38SJonathan Lemon {
1924cb679c38SJonathan Lemon 
1925cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
1926cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
1927cb679c38SJonathan Lemon 
1928cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
1929cb679c38SJonathan Lemon 			kn->kn_data++;
1930cb679c38SJonathan Lemon 	}
1931cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
1932cb679c38SJonathan Lemon }
1933