xref: /freebsd/sys/kern/kern_sig.c (revision 2899d6063893d97ce6b60b94f1d26cfa343a3dd7)
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>
7106ae1e91SMatthew Dillon #include <sys/unistd.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <machine/cpu.h>
74df8bae1dSRodney W. Grimes 
756f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
766f841fb7SMarcel Moolenaar 
77b40ce416SJulian Elischer static int coredump	__P((struct thread *));
782c42a146SMarcel Moolenaar static int do_sigaction	__P((struct proc *p, int sig, struct sigaction *act,
79645682fdSLuoqi Chen 			     struct sigaction *oact, int old));
802c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
81645682fdSLuoqi Chen 			       sigset_t *oset, int old));
82f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t));
836f841fb7SMarcel Moolenaar static int killpg1	__P((struct proc *cp, int sig, int pgid, int all));
846f841fb7SMarcel Moolenaar static int sig_ffs	__P((sigset_t *set));
856f841fb7SMarcel Moolenaar static int sigprop	__P((int sig));
866f841fb7SMarcel Moolenaar static void stop	__P((struct proc *));
8726f9a767SRodney W. Grimes 
88cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
89cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
90cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
91cb679c38SJonathan Lemon 
92cb679c38SJonathan Lemon struct filterops sig_filtops =
93cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
94cb679c38SJonathan Lemon 
9557308494SJoerg Wunsch static int	kern_logsigexit = 1;
963d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
973d177f46SBill Fumerola     &kern_logsigexit, 0,
983d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
9957308494SJoerg Wunsch 
100df8bae1dSRodney W. Grimes /*
101b1fc0ec1SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1029844fbc3SRobert Watson  * XXX: should use suser(), p_cansignal().
103831d27a9SDon Lewis  */
104b1fc0ec1SRobert Watson #define CANSIGIO(cr1, cr2) \
105b1fc0ec1SRobert Watson 	((cr1)->cr_uid == 0 || \
1069844fbc3SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1079844fbc3SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1089844fbc3SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1099844fbc3SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
110831d27a9SDon Lewis 
11122d4b0fbSJohn Polstra int sugid_coredump;
1123d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1133d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
114c87e2930SDavid Greenman 
115e5a28db9SPaul Saab static int	do_coredump = 1;
116e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
117e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
118e5a28db9SPaul Saab 
1192c42a146SMarcel Moolenaar /*
1202c42a146SMarcel Moolenaar  * Signal properties and actions.
1212c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1222c42a146SMarcel Moolenaar  * according to the following properties:
1232c42a146SMarcel Moolenaar  */
1242c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1252c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1262c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1272c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1282c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1292c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1302c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
131df8bae1dSRodney W. Grimes 
1322c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1332c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1342c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1352c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1362c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1372c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1382c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1392c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1412c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1452c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1462c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1472c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1482c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1492c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1502c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1512c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1522c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1532c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1542c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1552c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1562c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1572c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1582c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1592c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1602c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1612c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1622c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1632c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1642c42a146SMarcel Moolenaar };
1652c42a146SMarcel Moolenaar 
166fbbeeb6cSBruce Evans /*
167fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
168fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
169fbbeeb6cSBruce Evans  * action, the process stops in issignal().
170fbbeeb6cSBruce Evans  *
17133510ef1SBruce Evans  * MP SAFE.
172fbbeeb6cSBruce Evans  */
173fbbeeb6cSBruce Evans int
174fbbeeb6cSBruce Evans CURSIG(struct proc *p)
175fbbeeb6cSBruce Evans {
176fbbeeb6cSBruce Evans 	sigset_t tmpset;
177fbbeeb6cSBruce Evans 
1782ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
17933510ef1SBruce Evans 	if (SIGISEMPTY(p->p_siglist))
1802ad7d304SJohn Baldwin 		return (0);
181fbbeeb6cSBruce Evans 	tmpset = p->p_siglist;
182fbbeeb6cSBruce Evans 	SIGSETNAND(tmpset, p->p_sigmask);
18333510ef1SBruce Evans 	if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
1842ad7d304SJohn Baldwin 		return (0);
1852ad7d304SJohn Baldwin 	return (issignal(p));
186fbbeeb6cSBruce Evans }
187fbbeeb6cSBruce Evans 
1886f841fb7SMarcel Moolenaar static __inline int
1896f841fb7SMarcel Moolenaar sigprop(int sig)
1902c42a146SMarcel Moolenaar {
1916f841fb7SMarcel Moolenaar 
1922c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
1932c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
1942c42a146SMarcel Moolenaar 	return (0);
195df8bae1dSRodney W. Grimes }
1962c42a146SMarcel Moolenaar 
1976f841fb7SMarcel Moolenaar static __inline int
1986f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
1992c42a146SMarcel Moolenaar {
2002c42a146SMarcel Moolenaar 	int i;
2012c42a146SMarcel Moolenaar 
2026f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2032c42a146SMarcel Moolenaar 		if (set->__bits[i])
2042c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
205df8bae1dSRodney W. Grimes 	return (0);
206df8bae1dSRodney W. Grimes }
207df8bae1dSRodney W. Grimes 
2082c42a146SMarcel Moolenaar /*
2092c42a146SMarcel Moolenaar  * do_sigaction
2102c42a146SMarcel Moolenaar  * sigaction
2112c42a146SMarcel Moolenaar  * osigaction
2122c42a146SMarcel Moolenaar  */
2132c42a146SMarcel Moolenaar static int
214645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
2152c42a146SMarcel Moolenaar 	struct proc *p;
2162c42a146SMarcel Moolenaar 	register int sig;
2172c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
218645682fdSLuoqi Chen 	int old;
219df8bae1dSRodney W. Grimes {
220628d2653SJohn Baldwin 	register struct sigacts *ps;
221df8bae1dSRodney W. Grimes 
2222899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2232c42a146SMarcel Moolenaar 		return (EINVAL);
2242c42a146SMarcel Moolenaar 
225628d2653SJohn Baldwin 	PROC_LOCK(p);
226628d2653SJohn Baldwin 	ps = p->p_sigacts;
2272c42a146SMarcel Moolenaar 	if (oact) {
2282c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2292c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2302c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2312c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2322c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2332c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2342c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2352c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2362c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2372c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2382c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2392c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2402c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
241645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2422c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
243645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2442c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2452c42a146SMarcel Moolenaar 	}
2462c42a146SMarcel Moolenaar 	if (act) {
2472c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
248628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
249628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2502c42a146SMarcel Moolenaar 			return (EINVAL);
251628d2653SJohn Baldwin 		}
2522c42a146SMarcel Moolenaar 
253df8bae1dSRodney W. Grimes 		/*
254df8bae1dSRodney W. Grimes 		 * Change setting atomically.
255df8bae1dSRodney W. Grimes 		 */
2562c42a146SMarcel Moolenaar 
2572c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2582c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2592c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
260aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
261aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
26280f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
26380f42b55SIan Dowse 		} else {
26480f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2652c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2662c42a146SMarcel Moolenaar 		}
2672c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2682c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
269df8bae1dSRodney W. Grimes 		else
2702c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2712c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2722c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
273df8bae1dSRodney W. Grimes 		else
2742c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2752c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2762c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2771e41c1b5SSteven Wallace 		else
2782c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2792c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2802c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
281289ccde0SPeter Wemm 		else
2822c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
283df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2842c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2852c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
286df8bae1dSRodney W. Grimes 		else
2872c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_usertramp, seg);
288df8bae1dSRodney W. Grimes #endif
2892c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2902c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
291645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
2926626c604SJulian Elischer 			else
293645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
294aa7a4daeSPeter Wemm 			if ((act->sa_flags & SA_NOCLDWAIT) ||
295aa7a4daeSPeter Wemm 			    ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) {
296245f17d4SJoerg Wunsch 				/*
2972c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
2982c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
2992c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3002c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
301245f17d4SJoerg Wunsch 				 */
302245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
303645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3046626c604SJulian Elischer 				else
305645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
306245f17d4SJoerg Wunsch 			} else
307645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
308df8bae1dSRodney W. Grimes 		}
309df8bae1dSRodney W. Grimes 		/*
310df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3112c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3122c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3132c42a146SMarcel Moolenaar 		 * have to restart the process.
314df8bae1dSRodney W. Grimes 		 */
3152c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3162c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3172c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3182c42a146SMarcel Moolenaar 			/* never to be seen again */
3192c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3202c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3212c42a146SMarcel Moolenaar 				/* easier in psignal */
3222c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3232c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
324645682fdSLuoqi Chen 		} else {
3252c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3262c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3272c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3282c42a146SMarcel Moolenaar 			else
3292c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3302c42a146SMarcel Moolenaar 		}
331e8ebc08fSPeter Wemm #ifdef COMPAT_43
332645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
333645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
334645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
335645682fdSLuoqi Chen 		else
336645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
337e8ebc08fSPeter Wemm #endif
338df8bae1dSRodney W. Grimes 	}
339628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3402c42a146SMarcel Moolenaar 	return (0);
3412c42a146SMarcel Moolenaar }
3422c42a146SMarcel Moolenaar 
3432c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3442c42a146SMarcel Moolenaar struct sigaction_args {
3452c42a146SMarcel Moolenaar 	int	sig;
3462c42a146SMarcel Moolenaar 	struct	sigaction *act;
3472c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3482c42a146SMarcel Moolenaar };
3492c42a146SMarcel Moolenaar #endif
350fb99ab88SMatthew Dillon /*
351fb99ab88SMatthew Dillon  * MPSAFE
352fb99ab88SMatthew Dillon  */
3532c42a146SMarcel Moolenaar /* ARGSUSED */
3542c42a146SMarcel Moolenaar int
355b40ce416SJulian Elischer sigaction(td, uap)
356b40ce416SJulian Elischer 	struct thread *td;
3572c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3582c42a146SMarcel Moolenaar {
359b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3602c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3612c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3622c42a146SMarcel Moolenaar 	int error;
3632c42a146SMarcel Moolenaar 
364fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
365fb99ab88SMatthew Dillon 
3666f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3676f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3682c42a146SMarcel Moolenaar 	if (actp) {
3696f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3702c42a146SMarcel Moolenaar 		if (error)
371fb99ab88SMatthew Dillon 			goto done2;
3722c42a146SMarcel Moolenaar 	}
373645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3742c42a146SMarcel Moolenaar 	if (oactp && !error) {
3756f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3762c42a146SMarcel Moolenaar 	}
377fb99ab88SMatthew Dillon done2:
378fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
3792c42a146SMarcel Moolenaar 	return (error);
3802c42a146SMarcel Moolenaar }
3812c42a146SMarcel Moolenaar 
38231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3832c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3842c42a146SMarcel Moolenaar struct osigaction_args {
3852c42a146SMarcel Moolenaar 	int	signum;
3862c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3872c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3882c42a146SMarcel Moolenaar };
3892c42a146SMarcel Moolenaar #endif
390fb99ab88SMatthew Dillon /*
391fb99ab88SMatthew Dillon  * MPSAFE
392fb99ab88SMatthew Dillon  */
3932c42a146SMarcel Moolenaar /* ARGSUSED */
3942c42a146SMarcel Moolenaar int
395b40ce416SJulian Elischer osigaction(td, uap)
396b40ce416SJulian Elischer 	struct thread *td;
3972c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3982c42a146SMarcel Moolenaar {
399b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
4002c42a146SMarcel Moolenaar 	struct osigaction sa;
4012c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4022c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4032c42a146SMarcel Moolenaar 	int error;
4042c42a146SMarcel Moolenaar 
4056f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4066f841fb7SMarcel Moolenaar 		return (EINVAL);
407fb99ab88SMatthew Dillon 
4086f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4096f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
410fb99ab88SMatthew Dillon 
411fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
412fb99ab88SMatthew Dillon 
4132c42a146SMarcel Moolenaar 	if (nsap) {
4146f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4152c42a146SMarcel Moolenaar 		if (error)
416fb99ab88SMatthew Dillon 			goto done2;
4172c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4182c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4192c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4202c42a146SMarcel Moolenaar 	}
421645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
4222c42a146SMarcel Moolenaar 	if (osap && !error) {
4232c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4242c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4252c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4266f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4272c42a146SMarcel Moolenaar 	}
428fb99ab88SMatthew Dillon done2:
429fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
4302c42a146SMarcel Moolenaar 	return (error);
4312c42a146SMarcel Moolenaar }
43231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes /*
435df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
436df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
437df8bae1dSRodney W. Grimes  */
438df8bae1dSRodney W. Grimes void
439df8bae1dSRodney W. Grimes siginit(p)
440df8bae1dSRodney W. Grimes 	struct proc *p;
441df8bae1dSRodney W. Grimes {
442df8bae1dSRodney W. Grimes 	register int i;
443df8bae1dSRodney W. Grimes 
444628d2653SJohn Baldwin 	PROC_LOCK(p);
4452c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4462c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4472c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
448628d2653SJohn Baldwin 	PROC_UNLOCK(p);
449df8bae1dSRodney W. Grimes }
450df8bae1dSRodney W. Grimes 
451df8bae1dSRodney W. Grimes /*
452df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
453df8bae1dSRodney W. Grimes  */
454df8bae1dSRodney W. Grimes void
455df8bae1dSRodney W. Grimes execsigs(p)
456df8bae1dSRodney W. Grimes 	register struct proc *p;
457df8bae1dSRodney W. Grimes {
458628d2653SJohn Baldwin 	register struct sigacts *ps;
4592c42a146SMarcel Moolenaar 	register int sig;
460df8bae1dSRodney W. Grimes 
461df8bae1dSRodney W. Grimes 	/*
462df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
463df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
464df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
465df8bae1dSRodney W. Grimes 	 */
466628d2653SJohn Baldwin 	PROC_LOCK(p);
467628d2653SJohn Baldwin 	ps = p->p_sigacts;
4682c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4692c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4702c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4712c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4722c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4732c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4742c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
475df8bae1dSRodney W. Grimes 		}
4762c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
477df8bae1dSRodney W. Grimes 	}
478df8bae1dSRodney W. Grimes 	/*
479df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
480df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
481df8bae1dSRodney W. Grimes 	 */
482645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
483645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
484645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
4853b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
48680e907a1SPeter Wemm 	/*
48780e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
48880e907a1SPeter Wemm 	 */
489645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
490c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
491c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
492628d2653SJohn Baldwin 	PROC_UNLOCK(p);
493df8bae1dSRodney W. Grimes }
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes /*
496628d2653SJohn Baldwin  * do_sigprocmask()
4977c8fdcbdSMatthew Dillon  *
498628d2653SJohn Baldwin  *	Manipulate signal mask.
499df8bae1dSRodney W. Grimes  */
5002c42a146SMarcel Moolenaar static int
501645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
5022c42a146SMarcel Moolenaar 	struct proc *p;
5032c42a146SMarcel Moolenaar 	int how;
5042c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
505645682fdSLuoqi Chen 	int old;
5062c42a146SMarcel Moolenaar {
5072c42a146SMarcel Moolenaar 	int error;
5082c42a146SMarcel Moolenaar 
509628d2653SJohn Baldwin 	PROC_LOCK(p);
5102c42a146SMarcel Moolenaar 	if (oset != NULL)
5112c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
5122c42a146SMarcel Moolenaar 
5132c42a146SMarcel Moolenaar 	error = 0;
5142c42a146SMarcel Moolenaar 	if (set != NULL) {
5152c42a146SMarcel Moolenaar 		switch (how) {
5162c42a146SMarcel Moolenaar 		case SIG_BLOCK:
517645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
5182c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
5192c42a146SMarcel Moolenaar 			break;
5202c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
5212c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
5222c42a146SMarcel Moolenaar 			break;
5232c42a146SMarcel Moolenaar 		case SIG_SETMASK:
524645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
525645682fdSLuoqi Chen 			if (old)
526645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
527645682fdSLuoqi Chen 			else
5282c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
5292c42a146SMarcel Moolenaar 			break;
5302c42a146SMarcel Moolenaar 		default:
5312c42a146SMarcel Moolenaar 			error = EINVAL;
5322c42a146SMarcel Moolenaar 			break;
5332c42a146SMarcel Moolenaar 		}
5342c42a146SMarcel Moolenaar 	}
535628d2653SJohn Baldwin 	PROC_UNLOCK(p);
5362c42a146SMarcel Moolenaar 	return (error);
5372c42a146SMarcel Moolenaar }
5382c42a146SMarcel Moolenaar 
5397c8fdcbdSMatthew Dillon /*
540b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
5417c8fdcbdSMatthew Dillon  */
5427c8fdcbdSMatthew Dillon 
543d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
544df8bae1dSRodney W. Grimes struct sigprocmask_args {
545df8bae1dSRodney W. Grimes 	int	how;
5462c42a146SMarcel Moolenaar 	const sigset_t *set;
5472c42a146SMarcel Moolenaar 	sigset_t *oset;
548df8bae1dSRodney W. Grimes };
549d2d3e875SBruce Evans #endif
55026f9a767SRodney W. Grimes int
551b40ce416SJulian Elischer sigprocmask(td, uap)
552b40ce416SJulian Elischer 	register struct thread *td;
553df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
554df8bae1dSRodney W. Grimes {
555b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
5562c42a146SMarcel Moolenaar 	sigset_t set, oset;
5572c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5582c42a146SMarcel Moolenaar 	int error;
559df8bae1dSRodney W. Grimes 
5606f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5616f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5622c42a146SMarcel Moolenaar 	if (setp) {
5636f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5642c42a146SMarcel Moolenaar 		if (error)
5652c42a146SMarcel Moolenaar 			return (error);
566df8bae1dSRodney W. Grimes 	}
567645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5682c42a146SMarcel Moolenaar 	if (osetp && !error) {
5696f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5702c42a146SMarcel Moolenaar 	}
5712c42a146SMarcel Moolenaar 	return (error);
5722c42a146SMarcel Moolenaar }
5732c42a146SMarcel Moolenaar 
57431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5757c8fdcbdSMatthew Dillon /*
5767c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5777c8fdcbdSMatthew Dillon  */
5782c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5792c42a146SMarcel Moolenaar struct osigprocmask_args {
5802c42a146SMarcel Moolenaar 	int	how;
5812c42a146SMarcel Moolenaar 	osigset_t mask;
5822c42a146SMarcel Moolenaar };
5832c42a146SMarcel Moolenaar #endif
5842c42a146SMarcel Moolenaar int
585b40ce416SJulian Elischer osigprocmask(td, uap)
586b40ce416SJulian Elischer 	register struct thread *td;
5872c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5882c42a146SMarcel Moolenaar {
589b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
5902c42a146SMarcel Moolenaar 	sigset_t set, oset;
5912c42a146SMarcel Moolenaar 	int error;
5922c42a146SMarcel Moolenaar 
5932c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
594645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
595b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
596df8bae1dSRodney W. Grimes 	return (error);
597df8bae1dSRodney W. Grimes }
59831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
599df8bae1dSRodney W. Grimes 
600d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
601df8bae1dSRodney W. Grimes struct sigpending_args {
6022c42a146SMarcel Moolenaar 	sigset_t	*set;
603df8bae1dSRodney W. Grimes };
604d2d3e875SBruce Evans #endif
605fb99ab88SMatthew Dillon /*
606fb99ab88SMatthew Dillon  * MPSAFE
607fb99ab88SMatthew Dillon  */
608df8bae1dSRodney W. Grimes /* ARGSUSED */
60926f9a767SRodney W. Grimes int
610b40ce416SJulian Elischer sigpending(td, uap)
611b40ce416SJulian Elischer 	struct thread *td;
612df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
613df8bae1dSRodney W. Grimes {
614b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
615628d2653SJohn Baldwin 	sigset_t siglist;
616fb99ab88SMatthew Dillon 	int error;
617df8bae1dSRodney W. Grimes 
618fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
619628d2653SJohn Baldwin 	PROC_LOCK(p);
620628d2653SJohn Baldwin 	siglist = p->p_siglist;
621628d2653SJohn Baldwin 	PROC_UNLOCK(p);
622fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
623fb99ab88SMatthew Dillon 	error = copyout(&siglist, uap->set, sizeof(sigset_t));
624fb99ab88SMatthew Dillon 	return(error);
6252c42a146SMarcel Moolenaar }
6262c42a146SMarcel Moolenaar 
62731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6282c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6292c42a146SMarcel Moolenaar struct osigpending_args {
6302c42a146SMarcel Moolenaar 	int	dummy;
6312c42a146SMarcel Moolenaar };
6322c42a146SMarcel Moolenaar #endif
633fb99ab88SMatthew Dillon /*
634fb99ab88SMatthew Dillon  * MPSAFE
635fb99ab88SMatthew Dillon  */
6362c42a146SMarcel Moolenaar /* ARGSUSED */
6372c42a146SMarcel Moolenaar int
638b40ce416SJulian Elischer osigpending(td, uap)
639b40ce416SJulian Elischer 	struct thread *td;
6402c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6412c42a146SMarcel Moolenaar {
642b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
643b40ce416SJulian Elischer 
644fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
645628d2653SJohn Baldwin 	PROC_LOCK(p);
646b40ce416SJulian Elischer 	SIG2OSIG(p->p_siglist, td->td_retval[0]);
647628d2653SJohn Baldwin 	PROC_UNLOCK(p);
648fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
649df8bae1dSRodney W. Grimes 	return (0);
650df8bae1dSRodney W. Grimes }
65131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
654df8bae1dSRodney W. Grimes /*
655df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
656df8bae1dSRodney W. Grimes  */
657d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
658df8bae1dSRodney W. Grimes struct osigvec_args {
659df8bae1dSRodney W. Grimes 	int	signum;
660df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
661df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
662df8bae1dSRodney W. Grimes };
663d2d3e875SBruce Evans #endif
664fb99ab88SMatthew Dillon /*
665fb99ab88SMatthew Dillon  * MPSAFE
666fb99ab88SMatthew Dillon  */
667df8bae1dSRodney W. Grimes /* ARGSUSED */
66826f9a767SRodney W. Grimes int
669b40ce416SJulian Elischer osigvec(td, uap)
670b40ce416SJulian Elischer 	struct thread *td;
671df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
672df8bae1dSRodney W. Grimes {
673b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
674df8bae1dSRodney W. Grimes 	struct sigvec vec;
6752c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6762c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6772c42a146SMarcel Moolenaar 	int error;
678df8bae1dSRodney W. Grimes 
6796f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6806f841fb7SMarcel Moolenaar 		return (EINVAL);
6816f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6826f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6832c42a146SMarcel Moolenaar 	if (nsap) {
6846f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
6852c42a146SMarcel Moolenaar 		if (error)
686df8bae1dSRodney W. Grimes 			return (error);
6872c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
6882c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
6892c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
6902c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
691df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
6922c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
693df8bae1dSRodney W. Grimes #endif
694df8bae1dSRodney W. Grimes 	}
695fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
696645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
697fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
6982c42a146SMarcel Moolenaar 	if (osap && !error) {
6992c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7002c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7012c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7022c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7032c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7042c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7052c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7062c42a146SMarcel Moolenaar #endif
7076f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
7082c42a146SMarcel Moolenaar 	}
7092c42a146SMarcel Moolenaar 	return (error);
710df8bae1dSRodney W. Grimes }
711df8bae1dSRodney W. Grimes 
712d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
713df8bae1dSRodney W. Grimes struct osigblock_args {
714df8bae1dSRodney W. Grimes 	int	mask;
715df8bae1dSRodney W. Grimes };
716d2d3e875SBruce Evans #endif
717fb99ab88SMatthew Dillon /*
718fb99ab88SMatthew Dillon  * MPSAFE
719fb99ab88SMatthew Dillon  */
72026f9a767SRodney W. Grimes int
721b40ce416SJulian Elischer osigblock(td, uap)
722b40ce416SJulian Elischer 	register struct thread *td;
723df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
724df8bae1dSRodney W. Grimes {
725b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7262c42a146SMarcel Moolenaar 	sigset_t set;
727df8bae1dSRodney W. Grimes 
7282c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7292c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
730fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
731628d2653SJohn Baldwin 	PROC_LOCK(p);
732b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
7332c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
734628d2653SJohn Baldwin 	PROC_UNLOCK(p);
735fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
736df8bae1dSRodney W. Grimes 	return (0);
737df8bae1dSRodney W. Grimes }
738df8bae1dSRodney W. Grimes 
739d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
740df8bae1dSRodney W. Grimes struct osigsetmask_args {
741df8bae1dSRodney W. Grimes 	int	mask;
742df8bae1dSRodney W. Grimes };
743d2d3e875SBruce Evans #endif
744fb99ab88SMatthew Dillon /*
745fb99ab88SMatthew Dillon  * MPSAFE
746fb99ab88SMatthew Dillon  */
74726f9a767SRodney W. Grimes int
748b40ce416SJulian Elischer osigsetmask(td, uap)
749b40ce416SJulian Elischer 	struct thread *td;
750df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
751df8bae1dSRodney W. Grimes {
752b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7532c42a146SMarcel Moolenaar 	sigset_t set;
754df8bae1dSRodney W. Grimes 
7552c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7562c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
757fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
758628d2653SJohn Baldwin 	PROC_LOCK(p);
759b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
760645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
761628d2653SJohn Baldwin 	PROC_UNLOCK(p);
762fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
763df8bae1dSRodney W. Grimes 	return (0);
764df8bae1dSRodney W. Grimes }
765df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
766df8bae1dSRodney W. Grimes 
767df8bae1dSRodney W. Grimes /*
768df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
769df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
770df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
771b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
772b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
773b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
774df8bae1dSRodney W. Grimes  */
775d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
776df8bae1dSRodney W. Grimes struct sigsuspend_args {
7772c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
778df8bae1dSRodney W. Grimes };
779d2d3e875SBruce Evans #endif
780fb99ab88SMatthew Dillon /*
781fb99ab88SMatthew Dillon  * MPSAFE
782fb99ab88SMatthew Dillon  */
783df8bae1dSRodney W. Grimes /* ARGSUSED */
78426f9a767SRodney W. Grimes int
785b40ce416SJulian Elischer sigsuspend(td, uap)
786b40ce416SJulian Elischer 	struct thread *td;
787df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
788df8bae1dSRodney W. Grimes {
789b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7902c42a146SMarcel Moolenaar 	sigset_t mask;
791628d2653SJohn Baldwin 	register struct sigacts *ps;
7922c42a146SMarcel Moolenaar 	int error;
7932c42a146SMarcel Moolenaar 
7946f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
7952c42a146SMarcel Moolenaar 	if (error)
7962c42a146SMarcel Moolenaar 		return (error);
797df8bae1dSRodney W. Grimes 
798df8bae1dSRodney W. Grimes 	/*
799645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
800df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
801df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
802df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
803df8bae1dSRodney W. Grimes 	 * to indicate this.
804df8bae1dSRodney W. Grimes 	 */
805fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
806628d2653SJohn Baldwin 	PROC_LOCK(p);
807628d2653SJohn Baldwin 	ps = p->p_sigacts;
8086626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
809645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
810645682fdSLuoqi Chen 
811645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
8122c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
813628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
8142c42a146SMarcel Moolenaar 		/* void */;
815628d2653SJohn Baldwin 	PROC_UNLOCK(p);
816fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
8172c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
8182c42a146SMarcel Moolenaar 	return (EINTR);
8192c42a146SMarcel Moolenaar }
8202c42a146SMarcel Moolenaar 
82131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8222c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8232c42a146SMarcel Moolenaar struct osigsuspend_args {
8242c42a146SMarcel Moolenaar 	osigset_t mask;
8252c42a146SMarcel Moolenaar };
8262c42a146SMarcel Moolenaar #endif
827fb99ab88SMatthew Dillon /*
828fb99ab88SMatthew Dillon  * MPSAFE
829fb99ab88SMatthew Dillon  */
8302c42a146SMarcel Moolenaar /* ARGSUSED */
8312c42a146SMarcel Moolenaar int
832b40ce416SJulian Elischer osigsuspend(td, uap)
833b40ce416SJulian Elischer 	struct thread *td;
8342c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
8352c42a146SMarcel Moolenaar {
836b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
837645682fdSLuoqi Chen 	sigset_t mask;
838628d2653SJohn Baldwin 	register struct sigacts *ps;
8392c42a146SMarcel Moolenaar 
840fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
841628d2653SJohn Baldwin 	PROC_LOCK(p);
842628d2653SJohn Baldwin 	ps = p->p_sigacts;
8432c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
844645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
845645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
846645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
847645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
848628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
849df8bae1dSRodney W. Grimes 		/* void */;
850628d2653SJohn Baldwin 	PROC_UNLOCK(p);
851fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
852df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
853df8bae1dSRodney W. Grimes 	return (EINTR);
854df8bae1dSRodney W. Grimes }
85531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
856df8bae1dSRodney W. Grimes 
857df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
858d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
859df8bae1dSRodney W. Grimes struct osigstack_args {
860df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
861df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
862df8bae1dSRodney W. Grimes };
863d2d3e875SBruce Evans #endif
864fb99ab88SMatthew Dillon /*
865fb99ab88SMatthew Dillon  * MPSAFE
866fb99ab88SMatthew Dillon  */
867df8bae1dSRodney W. Grimes /* ARGSUSED */
86826f9a767SRodney W. Grimes int
869b40ce416SJulian Elischer osigstack(td, uap)
870b40ce416SJulian Elischer 	struct thread *td;
871df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
872df8bae1dSRodney W. Grimes {
873b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
874df8bae1dSRodney W. Grimes 	struct sigstack ss;
875fb99ab88SMatthew Dillon 	int error = 0;
876fb99ab88SMatthew Dillon 
877fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
878df8bae1dSRodney W. Grimes 
879d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
880628d2653SJohn Baldwin 		PROC_LOCK(p);
881645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
882b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
883628d2653SJohn Baldwin 		PROC_UNLOCK(p);
884d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
885d034d459SMarcel Moolenaar 		if (error)
886fb99ab88SMatthew Dillon 			goto done2;
887d034d459SMarcel Moolenaar 	}
888d034d459SMarcel Moolenaar 
889d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
890d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
891fb99ab88SMatthew Dillon 			goto done2;
892628d2653SJohn Baldwin 		PROC_LOCK(p);
893645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
894645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
895645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
896645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
897628d2653SJohn Baldwin 		PROC_UNLOCK(p);
898df8bae1dSRodney W. Grimes 	}
899fb99ab88SMatthew Dillon done2:
900fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
901fb99ab88SMatthew Dillon 	return (error);
902df8bae1dSRodney W. Grimes }
903df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
904df8bae1dSRodney W. Grimes 
905d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
906df8bae1dSRodney W. Grimes struct sigaltstack_args {
9072c42a146SMarcel Moolenaar 	stack_t	*ss;
9082c42a146SMarcel Moolenaar 	stack_t	*oss;
909df8bae1dSRodney W. Grimes };
910d2d3e875SBruce Evans #endif
911fb99ab88SMatthew Dillon /*
912fb99ab88SMatthew Dillon  * MPSAFE
913fb99ab88SMatthew Dillon  */
914df8bae1dSRodney W. Grimes /* ARGSUSED */
91526f9a767SRodney W. Grimes int
916b40ce416SJulian Elischer sigaltstack(td, uap)
917b40ce416SJulian Elischer 	struct thread *td;
918df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
919df8bae1dSRodney W. Grimes {
920b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9212c42a146SMarcel Moolenaar 	stack_t ss;
922fb99ab88SMatthew Dillon 	int oonstack;
923fb99ab88SMatthew Dillon 	int error = 0;
924fb99ab88SMatthew Dillon 
925fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
926df8bae1dSRodney W. Grimes 
927b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
928d034d459SMarcel Moolenaar 
929d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
930628d2653SJohn Baldwin 		PROC_LOCK(p);
931d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
932d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
933d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
934628d2653SJohn Baldwin 		PROC_UNLOCK(p);
935d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
936fb99ab88SMatthew Dillon 			goto done2;
937df8bae1dSRodney W. Grimes 	}
938d034d459SMarcel Moolenaar 
939d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
940fb99ab88SMatthew Dillon 		if (oonstack) {
941fb99ab88SMatthew Dillon 			error = EPERM;
942fb99ab88SMatthew Dillon 			goto done2;
943fb99ab88SMatthew Dillon 		}
944d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
945fb99ab88SMatthew Dillon 			goto done2;
946fb99ab88SMatthew Dillon 		if ((ss.ss_flags & ~SS_DISABLE) != 0) {
947fb99ab88SMatthew Dillon 			error = EINVAL;
948fb99ab88SMatthew Dillon 			goto done2;
949fb99ab88SMatthew Dillon 		}
950d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
951fb99ab88SMatthew Dillon 			if (ss.ss_size < p->p_sysent->sv_minsigstksz) {
952fb99ab88SMatthew Dillon 				error = ENOMEM;
953fb99ab88SMatthew Dillon 				goto done2;
954fb99ab88SMatthew Dillon 			}
955628d2653SJohn Baldwin 			PROC_LOCK(p);
956645682fdSLuoqi Chen 			p->p_sigstk = ss;
957d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
958628d2653SJohn Baldwin 			PROC_UNLOCK(p);
959628d2653SJohn Baldwin 		} else {
960628d2653SJohn Baldwin 			PROC_LOCK(p);
961d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
962628d2653SJohn Baldwin 			PROC_UNLOCK(p);
963628d2653SJohn Baldwin 		}
964d034d459SMarcel Moolenaar 	}
965fb99ab88SMatthew Dillon done2:
966fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
967fb99ab88SMatthew Dillon 	return (error);
968df8bae1dSRodney W. Grimes }
969df8bae1dSRodney W. Grimes 
970d93f860cSPoul-Henning Kamp /*
971d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
972d93f860cSPoul-Henning Kamp  * cp is calling process.
973d93f860cSPoul-Henning Kamp  */
974d93f860cSPoul-Henning Kamp int
9752c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
976d93f860cSPoul-Henning Kamp 	register struct proc *cp;
9772c42a146SMarcel Moolenaar 	int sig, pgid, all;
978d93f860cSPoul-Henning Kamp {
979d93f860cSPoul-Henning Kamp 	register struct proc *p;
980d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
981d93f860cSPoul-Henning Kamp 	int nfound = 0;
982d93f860cSPoul-Henning Kamp 
983553629ebSJake Burkholder 	if (all) {
984d93f860cSPoul-Henning Kamp 		/*
985d93f860cSPoul-Henning Kamp 		 * broadcast
986d93f860cSPoul-Henning Kamp 		 */
9871005a129SJohn Baldwin 		sx_slock(&allproc_lock);
9882e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
989628d2653SJohn Baldwin 			PROC_LOCK(p);
990628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) {
991628d2653SJohn Baldwin 				PROC_UNLOCK(p);
992628d2653SJohn Baldwin 				continue;
993628d2653SJohn Baldwin 			}
99433a9ed9dSJohn Baldwin 			if (p_cansignal(cp, p, sig) == 0) {
995d93f860cSPoul-Henning Kamp 				nfound++;
99633a9ed9dSJohn Baldwin 				if (sig)
9972c42a146SMarcel Moolenaar 					psignal(p, sig);
998628d2653SJohn Baldwin 			}
99933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1000d93f860cSPoul-Henning Kamp 		}
10011005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1002553629ebSJake Burkholder 	} else {
1003d93f860cSPoul-Henning Kamp 		if (pgid == 0)
1004d93f860cSPoul-Henning Kamp 			/*
1005d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1006d93f860cSPoul-Henning Kamp 			 */
1007d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
1008d93f860cSPoul-Henning Kamp 		else {
1009d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1010d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
1011d93f860cSPoul-Henning Kamp 				return (ESRCH);
1012d93f860cSPoul-Henning Kamp 		}
10132e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1014628d2653SJohn Baldwin 			PROC_LOCK(p);
1015628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1016628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1017628d2653SJohn Baldwin 				continue;
1018628d2653SJohn Baldwin 			}
1019628d2653SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1020628d2653SJohn Baldwin 			if (p->p_stat == SZOMB) {
1021628d2653SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
102233a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1023628d2653SJohn Baldwin 				continue;
1024628d2653SJohn Baldwin 			}
1025628d2653SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
102633a9ed9dSJohn Baldwin 			if (p_cansignal(cp, p, sig) == 0) {
1027d93f860cSPoul-Henning Kamp 				nfound++;
102833a9ed9dSJohn Baldwin 				if (sig)
10292c42a146SMarcel Moolenaar 					psignal(p, sig);
1030628d2653SJohn Baldwin 			}
103133a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1032d93f860cSPoul-Henning Kamp 		}
1033d93f860cSPoul-Henning Kamp 	}
1034d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1035d93f860cSPoul-Henning Kamp }
1036d93f860cSPoul-Henning Kamp 
1037d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1038df8bae1dSRodney W. Grimes struct kill_args {
1039df8bae1dSRodney W. Grimes 	int	pid;
1040df8bae1dSRodney W. Grimes 	int	signum;
1041df8bae1dSRodney W. Grimes };
1042d2d3e875SBruce Evans #endif
1043fb99ab88SMatthew Dillon /*
1044fb99ab88SMatthew Dillon  * MPSAFE
1045fb99ab88SMatthew Dillon  */
1046df8bae1dSRodney W. Grimes /* ARGSUSED */
104726f9a767SRodney W. Grimes int
1048b40ce416SJulian Elischer kill(td, uap)
1049b40ce416SJulian Elischer 	register struct thread *td;
1050df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1051df8bae1dSRodney W. Grimes {
1052b40ce416SJulian Elischer 	register struct proc *cp = td->td_proc;
1053df8bae1dSRodney W. Grimes 	register struct proc *p;
1054fb99ab88SMatthew Dillon 	int error = 0;
1055df8bae1dSRodney W. Grimes 
10562899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(uap->signum))
1057df8bae1dSRodney W. Grimes 		return (EINVAL);
1058fb99ab88SMatthew Dillon 
1059fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1060df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1061df8bae1dSRodney W. Grimes 		/* kill single process */
1062fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1063fb99ab88SMatthew Dillon 			error = ESRCH;
1064fb99ab88SMatthew Dillon 		} else if (p_cansignal(cp, p, uap->signum)) {
106533a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1066fb99ab88SMatthew Dillon 			error = EPERM;
1067fb99ab88SMatthew Dillon 		} else {
106833a9ed9dSJohn Baldwin 			if (uap->signum)
1069df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1070628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1071fb99ab88SMatthew Dillon 			error = 0;
1072df8bae1dSRodney W. Grimes 		}
1073fb99ab88SMatthew Dillon 	} else {
1074df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1075df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
1076fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, 0, 1);
1077fb99ab88SMatthew Dillon 			break;
1078df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
1079fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, 0, 0);
1080fb99ab88SMatthew Dillon 			break;
1081df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
1082fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, -uap->pid, 0);
1083fb99ab88SMatthew Dillon 			break;
1084df8bae1dSRodney W. Grimes 		}
1085fb99ab88SMatthew Dillon 	}
1086fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1087fb99ab88SMatthew Dillon 	return(error);
1088df8bae1dSRodney W. Grimes }
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1091d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1092df8bae1dSRodney W. Grimes struct okillpg_args {
1093df8bae1dSRodney W. Grimes 	int	pgid;
1094df8bae1dSRodney W. Grimes 	int	signum;
1095df8bae1dSRodney W. Grimes };
1096d2d3e875SBruce Evans #endif
1097fb99ab88SMatthew Dillon /*
1098fb99ab88SMatthew Dillon  * MPSAFE
1099fb99ab88SMatthew Dillon  */
1100df8bae1dSRodney W. Grimes /* ARGSUSED */
110126f9a767SRodney W. Grimes int
1102b40ce416SJulian Elischer okillpg(td, uap)
1103b40ce416SJulian Elischer 	struct thread *td;
1104df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1105df8bae1dSRodney W. Grimes {
1106fb99ab88SMatthew Dillon 	int error;
1107df8bae1dSRodney W. Grimes 
11082899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(uap->signum))
1109df8bae1dSRodney W. Grimes 		return (EINVAL);
1110fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1111b40ce416SJulian Elischer 	error = killpg1(td->td_proc, uap->signum, uap->pgid, 0);
1112fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1113fb99ab88SMatthew Dillon 	return (error);
1114df8bae1dSRodney W. Grimes }
1115df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1116df8bae1dSRodney W. Grimes 
1117df8bae1dSRodney W. Grimes /*
1118df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1119df8bae1dSRodney W. Grimes  */
1120df8bae1dSRodney W. Grimes void
11212c42a146SMarcel Moolenaar gsignal(pgid, sig)
11222c42a146SMarcel Moolenaar 	int pgid, sig;
1123df8bae1dSRodney W. Grimes {
1124df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
11272c42a146SMarcel Moolenaar 		pgsignal(pgrp, sig, 0);
1128df8bae1dSRodney W. Grimes }
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes /*
1131df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1132df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1133df8bae1dSRodney W. Grimes  */
1134df8bae1dSRodney W. Grimes void
11352c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1136df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
11372c42a146SMarcel Moolenaar 	int sig, checkctty;
1138df8bae1dSRodney W. Grimes {
1139df8bae1dSRodney W. Grimes 	register struct proc *p;
1140df8bae1dSRodney W. Grimes 
1141628d2653SJohn Baldwin 	if (pgrp) {
1142628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1143628d2653SJohn Baldwin 			PROC_LOCK(p);
1144df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
11452c42a146SMarcel Moolenaar 				psignal(p, sig);
1146628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1147628d2653SJohn Baldwin 		}
1148628d2653SJohn Baldwin 	}
1149df8bae1dSRodney W. Grimes }
1150df8bae1dSRodney W. Grimes 
1151df8bae1dSRodney W. Grimes /*
1152df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1153df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1154df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1155356861dbSMatthew Dillon  *
1156356861dbSMatthew Dillon  * MPSAFE
1157df8bae1dSRodney W. Grimes  */
1158df8bae1dSRodney W. Grimes void
11592c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1160df8bae1dSRodney W. Grimes 	struct proc *p;
11612c42a146SMarcel Moolenaar 	register int sig;
11628674077aSJeffrey Hsu 	u_long code;
1163df8bae1dSRodney W. Grimes {
1164df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1165df8bae1dSRodney W. Grimes 
1166356861dbSMatthew Dillon 	mtx_lock(&Giant);
1167628d2653SJohn Baldwin 	PROC_LOCK(p);
11682c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1169d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1170df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1171df8bae1dSRodney W. Grimes #ifdef KTRACE
1172df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
11732c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
11742c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1175df8bae1dSRodney W. Grimes #endif
11762c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
11772c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
11782c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
11792c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
11802c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
11812c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1182289ccde0SPeter Wemm 			/*
11832c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1184289ccde0SPeter Wemm 			 */
11852c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
11862c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
11872c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
11882c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
11892c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1190dedc04feSPeter Wemm 		}
11916f841fb7SMarcel Moolenaar 	} else {
11926626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
11932c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
11942c42a146SMarcel Moolenaar 		psignal(p, sig);
1195df8bae1dSRodney W. Grimes 	}
1196628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1197356861dbSMatthew Dillon 	mtx_unlock(&Giant);
1198df8bae1dSRodney W. Grimes }
1199df8bae1dSRodney W. Grimes 
1200df8bae1dSRodney W. Grimes /*
1201df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1202df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1203df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1204df8bae1dSRodney W. Grimes  *
1205df8bae1dSRodney W. Grimes  * Exceptions:
1206df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1207df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1208df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1209df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1210df8bae1dSRodney W. Grimes  *
1211df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1212df8bae1dSRodney W. Grimes  */
1213df8bae1dSRodney W. Grimes void
12142c42a146SMarcel Moolenaar psignal(p, sig)
1215df8bae1dSRodney W. Grimes 	register struct proc *p;
12162c42a146SMarcel Moolenaar 	register int sig;
1217df8bae1dSRodney W. Grimes {
121840447cd4SJohn Baldwin 	register int prop;
1219df8bae1dSRodney W. Grimes 	register sig_t action;
1220b40ce416SJulian Elischer 	struct thread *td;
1221b40ce416SJulian Elischer 	struct ksegrp *kg;
1222df8bae1dSRodney W. Grimes 
12232899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
12242899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
12252c42a146SMarcel Moolenaar 
1226628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1227cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1228cb679c38SJonathan Lemon 
12292c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1230df8bae1dSRodney W. Grimes 
1231df8bae1dSRodney W. Grimes 	/*
12322a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
12332a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
12342a024a2bSSean Eric Fagan 	 * a chance, as well.
1235df8bae1dSRodney W. Grimes 	 */
1236b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1237df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1238b40ce416SJulian Elischer 	} else {
1239df8bae1dSRodney W. Grimes 		/*
1240df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1241df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1242df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1243df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1244df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1245df8bae1dSRodney W. Grimes 		 */
1246628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1247df8bae1dSRodney W. Grimes 			return;
12482c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1249df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
12502c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1251df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1252df8bae1dSRodney W. Grimes 		else
1253df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1254df8bae1dSRodney W. Grimes 	}
1255df8bae1dSRodney W. Grimes 
1256b40ce416SJulian Elischer 	/*
1257b40ce416SJulian Elischer 	 * bring the priority of a process up if we want it to get
1258b40ce416SJulian Elischer 	 * killed in this lifetime.
1259b40ce416SJulian Elischer 	 * XXXKSE think if a better way to do this.
1260b40ce416SJulian Elischer 	 *
1261b40ce416SJulian Elischer 	 * What we need to do is see if there is a thread that will
1262b40ce416SJulian Elischer 	 * be able to accept the signal. e.g.
1263b40ce416SJulian Elischer 	 * FOREACH_THREAD_IN_PROC() {
1264b40ce416SJulian Elischer 	 *	if runnable, we're done
1265b40ce416SJulian Elischer 	 *	else pick one at random.
1266b40ce416SJulian Elischer 	 * }
1267b40ce416SJulian Elischer 	 */
1268b40ce416SJulian Elischer 	/* XXXKSE
1269b40ce416SJulian Elischer 	 * For now there is one thread per proc.
1270b40ce416SJulian Elischer 	 * Effectively select one sucker thread..
1271b40ce416SJulian Elischer 	 */
1272b40ce416SJulian Elischer 	td = &p->p_thread;
12739ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1274b40ce416SJulian Elischer 	if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) &&
1275b40ce416SJulian Elischer 	    (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0))
1276b40ce416SJulian Elischer 		p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */
12779ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1278df8bae1dSRodney W. Grimes 
1279df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
12802c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1281df8bae1dSRodney W. Grimes 
1282df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1283df8bae1dSRodney W. Grimes 		/*
1284df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1285df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1286df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1287df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1288df8bae1dSRodney W. Grimes 		 */
1289df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1290628d2653SJohn Baldwin 		    action == SIG_DFL)
1291df8bae1dSRodney W. Grimes 		        return;
12922c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1293df8bae1dSRodney W. Grimes 	}
12942c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1295df8bae1dSRodney W. Grimes 
1296df8bae1dSRodney W. Grimes 	/*
1297df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1298df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1299df8bae1dSRodney W. Grimes 	 */
13009ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
13011c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
13029ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1303df8bae1dSRodney W. Grimes 		return;
13041c32c37cSJohn Baldwin 	}
1305b40ce416SJulian Elischer 
1306df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1307df8bae1dSRodney W. Grimes 
1308df8bae1dSRodney W. Grimes 	case SSLEEP:
1309df8bae1dSRodney W. Grimes 		/*
1310df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1311df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1312df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1313df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1314df8bae1dSRodney W. Grimes 		 */
1315b40ce416SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
13169ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1317df8bae1dSRodney W. Grimes 			goto out;
13183e6831f5SJohn Baldwin 		}
1319df8bae1dSRodney W. Grimes 		/*
1320df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1321df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1322df8bae1dSRodney W. Grimes 		 * for the parent.
1323df8bae1dSRodney W. Grimes 		 */
1324df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1325df8bae1dSRodney W. Grimes 			goto run;
13269ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1327df8bae1dSRodney W. Grimes 		/*
1328df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1329df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1330df8bae1dSRodney W. Grimes 		 * be awakened.
1331df8bae1dSRodney W. Grimes 		 */
1332df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
13332c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1334df8bae1dSRodney W. Grimes 			goto out;
1335df8bae1dSRodney W. Grimes 		}
1336df8bae1dSRodney W. Grimes 		/*
1337df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1338df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1339df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1340df8bae1dSRodney W. Grimes 		 * cause the process to run.
1341df8bae1dSRodney W. Grimes 		 */
1342df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
13433e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1344df8bae1dSRodney W. Grimes 				goto runfast;
1345df8bae1dSRodney W. Grimes 			/*
1346df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1347df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1348df8bae1dSRodney W. Grimes 			 */
1349df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1350df8bae1dSRodney W. Grimes 				goto out;
13512c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
13522c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1353628d2653SJohn Baldwin 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) {
1354628d2653SJohn Baldwin 				PROC_LOCK(p->p_pptr);
1355df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1356628d2653SJohn Baldwin 				PROC_UNLOCK(p->p_pptr);
1357628d2653SJohn Baldwin 			}
13585b3047d5SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1359df8bae1dSRodney W. Grimes 			stop(p);
13605b3047d5SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
1361df8bae1dSRodney W. Grimes 			goto out;
13623e6831f5SJohn Baldwin 		} else
1363df8bae1dSRodney W. Grimes 			goto runfast;
1364df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1365df8bae1dSRodney W. Grimes 
1366df8bae1dSRodney W. Grimes 	case SSTOP:
13679ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1368df8bae1dSRodney W. Grimes 		/*
1369df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1370df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1371df8bae1dSRodney W. Grimes 		 */
137240447cd4SJohn Baldwin 		if (p->p_flag & P_TRACED)
1373df8bae1dSRodney W. Grimes 			goto out;
1374df8bae1dSRodney W. Grimes 
1375df8bae1dSRodney W. Grimes 		/*
1376df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1377df8bae1dSRodney W. Grimes 		 */
13782c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1379df8bae1dSRodney W. Grimes 			goto runfast;
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1382df8bae1dSRodney W. Grimes 			/*
1383df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1384df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1385df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1386df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1387df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1388df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1389df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1390df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1391df8bae1dSRodney W. Grimes 			 */
1392df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
13932c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1394df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1395df8bae1dSRodney W. Grimes 				goto runfast;
13969ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1397b40ce416SJulian Elischer 			/*
1398b40ce416SJulian Elischer 			 * XXXKSE
1399b40ce416SJulian Elischer 			 * do this for each thread.
1400b40ce416SJulian Elischer 			 */
1401b40ce416SJulian Elischer 			if (p->p_flag & P_KSES) {
1402b40ce416SJulian Elischer 				mtx_assert(&sched_lock,
1403b40ce416SJulian Elischer 				    MA_OWNED | MA_NOTRECURSED);
1404b40ce416SJulian Elischer 				FOREACH_THREAD_IN_PROC(p, td) {
1405b40ce416SJulian Elischer 					if (td->td_wchan == NULL) {
1406b40ce416SJulian Elischer 						setrunnable(td); /* XXXKSE */
1407b40ce416SJulian Elischer 					} else {
1408b40ce416SJulian Elischer 						/* mark it as sleeping */
1409b40ce416SJulian Elischer 					}
1410b40ce416SJulian Elischer 				}
1411b40ce416SJulian Elischer 				mtx_unlock_spin(&sched_lock);
1412b40ce416SJulian Elischer 			} else {
1413b40ce416SJulian Elischer 				if (p->p_thread.td_wchan == NULL)
1414df8bae1dSRodney W. Grimes 					goto run;
1415df8bae1dSRodney W. Grimes 				p->p_stat = SSLEEP;
14169ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
1417b40ce416SJulian Elischer 			}
1418fdd4e5c6SJulian Elischer 			goto out;
1419df8bae1dSRodney W. Grimes 		}
1420df8bae1dSRodney W. Grimes 
1421df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1422df8bae1dSRodney W. Grimes 			/*
1423df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1424df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1425df8bae1dSRodney W. Grimes 			 */
14262c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1427df8bae1dSRodney W. Grimes 			goto out;
1428df8bae1dSRodney W. Grimes 		}
1429df8bae1dSRodney W. Grimes 
1430df8bae1dSRodney W. Grimes 		/*
1431df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1432df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1433df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1434df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1435b40ce416SJulian Elischer 		 * XXXKSE should we wake ALL blocked threads?
1436df8bae1dSRodney W. Grimes 		 */
14379ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
1438b40ce416SJulian Elischer 		if (p->p_flag & P_KSES) {
1439b40ce416SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1440b40ce416SJulian Elischer 				if (td->td_wchan && (td->td_flags & TDF_SINTR)){
1441b40ce416SJulian Elischer 					if (td->td_flags & TDF_CVWAITQ)
1442b40ce416SJulian Elischer 						cv_waitq_remove(td);
1443238510fcSJason Evans 					else
1444b40ce416SJulian Elischer 						unsleep(td); /* XXXKSE */
1445b40ce416SJulian Elischer 				}
1446b40ce416SJulian Elischer 			}
1447b40ce416SJulian Elischer 		} else {
1448b40ce416SJulian Elischer 			if (td->td_wchan && td->td_flags & TDF_SINTR) {
1449b40ce416SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
1450b40ce416SJulian Elischer 					cv_waitq_remove(td);
1451b40ce416SJulian Elischer 				else
1452b40ce416SJulian Elischer 					unsleep(td); /* XXXKSE */
1453b40ce416SJulian Elischer 			}
1454238510fcSJason Evans 		}
14559ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1456df8bae1dSRodney W. Grimes 		goto out;
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 	default:
1459df8bae1dSRodney W. Grimes 		/*
1460df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1461df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1462df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1463df8bae1dSRodney W. Grimes 		 */
1464d2ef4060SBruce Evans 		if (p->p_stat == SRUN) {
14653163861cSTor Egge #ifdef SMP
1466b40ce416SJulian Elischer 			struct kse *ke;
1467b40ce416SJulian Elischer 			struct thread *td = curthread;
1468b40ce416SJulian Elischer 			signotify(&p->p_kse);  /* XXXKSE */
1469b40ce416SJulian Elischer /* we should only deliver to one thread.. but which one? */
1470b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(p, kg) {
1471b40ce416SJulian Elischer 				FOREACH_KSE_IN_GROUP(kg, ke) {
1472b40ce416SJulian Elischer 					if (ke->ke_thread == td) {
1473b40ce416SJulian Elischer 						continue;
1474b40ce416SJulian Elischer 					}
1475b40ce416SJulian Elischer 					forward_signal(ke->ke_thread);
1476b40ce416SJulian Elischer 				}
1477b40ce416SJulian Elischer 			}
1478b40ce416SJulian Elischer #else
1479b40ce416SJulian Elischer 			signotify(&p->p_kse);  /* XXXKSE */
14803163861cSTor Egge #endif
14816caa8a15SJohn Baldwin 		}
14829ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1483df8bae1dSRodney W. Grimes 		goto out;
1484df8bae1dSRodney W. Grimes 	}
1485df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1486df8bae1dSRodney W. Grimes 
1487df8bae1dSRodney W. Grimes runfast:
1488df8bae1dSRodney W. Grimes 	/*
1489df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1490b40ce416SJulian Elischer 	 * XXXKSE Should we make them all run fast?
1491b40ce416SJulian Elischer 	 * Maybe just one would be enough?
1492df8bae1dSRodney W. Grimes 	 */
14939ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1494b40ce416SJulian Elischer 	FOREACH_KSEGRP_IN_PROC(p, kg) {
1495b40ce416SJulian Elischer 		if (kg->kg_pri.pri_level > PUSER) {
1496b40ce416SJulian Elischer 			kg->kg_pri.pri_level = PUSER;
1497b40ce416SJulian Elischer 		}
1498b40ce416SJulian Elischer 	}
1499df8bae1dSRodney W. Grimes run:
15003e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
15013e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1502b40ce416SJulian Elischer 	setrunnable(td); /* XXXKSE */
15039ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
15043e6831f5SJohn Baldwin out:
15053e6831f5SJohn Baldwin 	/* If we jump here, sched_lock should not be owned. */
15063e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1507df8bae1dSRodney W. Grimes }
1508df8bae1dSRodney W. Grimes 
1509df8bae1dSRodney W. Grimes /*
1510df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1511df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1512df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1513df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1514df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1515df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1516df8bae1dSRodney W. Grimes  * sequence is
1517df8bae1dSRodney W. Grimes  *
15182c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
15192c42a146SMarcel Moolenaar  *		postsig(sig);
1520df8bae1dSRodney W. Grimes  */
152126f9a767SRodney W. Grimes int
1522df8bae1dSRodney W. Grimes issignal(p)
1523df8bae1dSRodney W. Grimes 	register struct proc *p;
1524df8bae1dSRodney W. Grimes {
15252c42a146SMarcel Moolenaar 	sigset_t mask;
15262c42a146SMarcel Moolenaar 	register int sig, prop;
1527df8bae1dSRodney W. Grimes 
1528628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1529df8bae1dSRodney W. Grimes 	for (;;) {
15302a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
15312a024a2bSSean Eric Fagan 
15322c42a146SMarcel Moolenaar 		mask = p->p_siglist;
15332c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1534df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
15352c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
15362c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1537df8bae1dSRodney W. Grimes 			return (0);
15382c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
15392c42a146SMarcel Moolenaar 		prop = sigprop(sig);
15402a024a2bSSean Eric Fagan 
1541628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
15422a024a2bSSean Eric Fagan 
1543df8bae1dSRodney W. Grimes 		/*
1544df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1545df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1546df8bae1dSRodney W. Grimes 		 */
15472c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
15482c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1549df8bae1dSRodney W. Grimes 			continue;
1550df8bae1dSRodney W. Grimes 		}
1551df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1552df8bae1dSRodney W. Grimes 			/*
1553df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1554df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1555df8bae1dSRodney W. Grimes 			 */
15562c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1557628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1558df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1559628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
1560df8bae1dSRodney W. Grimes 			do {
15619ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
15625b3047d5SJohn Baldwin 				stop(p);
1563628d2653SJohn Baldwin 				PROC_UNLOCK_NOSWITCH(p);
15647da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
15652ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1566df8bae1dSRodney W. Grimes 				mi_switch();
15679ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
156820cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1569628d2653SJohn Baldwin 				PROC_LOCK(p);
15702a024a2bSSean Eric Fagan 			} while (!trace_req(p)
15712a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1572df8bae1dSRodney W. Grimes 
1573df8bae1dSRodney W. Grimes 			/*
1574df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1575df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1576df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1577df8bae1dSRodney W. Grimes 			 */
1578df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1579df8bae1dSRodney W. Grimes 				continue;
1580df8bae1dSRodney W. Grimes 
1581df8bae1dSRodney W. Grimes 			/*
1582df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1583df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1584df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1585df8bae1dSRodney W. Grimes 			 */
15862c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
15872c42a146SMarcel Moolenaar 			sig = p->p_xstat;
15882c42a146SMarcel Moolenaar 			if (sig == 0)
1589df8bae1dSRodney W. Grimes 				continue;
1590df8bae1dSRodney W. Grimes 
1591df8bae1dSRodney W. Grimes 			/*
1592df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1593df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1594df8bae1dSRodney W. Grimes 			 */
15952c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
15962c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1597df8bae1dSRodney W. Grimes 				continue;
1598df8bae1dSRodney W. Grimes 		}
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes 		/*
1601df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1602df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1603df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1604df8bae1dSRodney W. Grimes 		 */
16052c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1606df8bae1dSRodney W. Grimes 
16070b53fbe8SBruce Evans 		case (int)SIG_DFL:
1608df8bae1dSRodney W. Grimes 			/*
1609df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1610df8bae1dSRodney W. Grimes 			 */
1611df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1612df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1613df8bae1dSRodney W. Grimes 				/*
1614df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1615df8bae1dSRodney W. Grimes 				 * in init? XXX
1616df8bae1dSRodney W. Grimes 				 */
1617d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
16182c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1619df8bae1dSRodney W. Grimes #endif
1620df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1621df8bae1dSRodney W. Grimes 			}
1622df8bae1dSRodney W. Grimes 			/*
1623df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1624df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1625df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1626df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1627df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1628df8bae1dSRodney W. Grimes 			 */
1629df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1630df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1631df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1632df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1633df8bae1dSRodney W. Grimes 					break;	/* == ignore */
16342c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1635628d2653SJohn Baldwin 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) {
1636628d2653SJohn Baldwin 					PROC_LOCK(p->p_pptr);
1637df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1638628d2653SJohn Baldwin 					PROC_UNLOCK(p->p_pptr);
1639628d2653SJohn Baldwin 				}
16409ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
16415b3047d5SJohn Baldwin 				stop(p);
1642628d2653SJohn Baldwin 				PROC_UNLOCK_NOSWITCH(p);
16437da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
16442ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1645df8bae1dSRodney W. Grimes 				mi_switch();
16469ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
164720cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1648628d2653SJohn Baldwin 				PROC_LOCK(p);
1649df8bae1dSRodney W. Grimes 				break;
1650df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1651df8bae1dSRodney W. Grimes 				/*
1652df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1653df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1654df8bae1dSRodney W. Grimes 				 */
1655df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1656df8bae1dSRodney W. Grimes 			} else
16572c42a146SMarcel Moolenaar 				return (sig);
1658df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1659df8bae1dSRodney W. Grimes 
16600b53fbe8SBruce Evans 		case (int)SIG_IGN:
1661df8bae1dSRodney W. Grimes 			/*
1662df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1663df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1664df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1665df8bae1dSRodney W. Grimes 			 */
1666df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1667df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1668df8bae1dSRodney W. Grimes 				printf("issignal\n");
1669df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1670df8bae1dSRodney W. Grimes 
1671df8bae1dSRodney W. Grimes 		default:
1672df8bae1dSRodney W. Grimes 			/*
1673df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1674df8bae1dSRodney W. Grimes 			 * postsig() process it.
1675df8bae1dSRodney W. Grimes 			 */
16762c42a146SMarcel Moolenaar 			return (sig);
1677df8bae1dSRodney W. Grimes 		}
16782c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1679df8bae1dSRodney W. Grimes 	}
1680df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1681df8bae1dSRodney W. Grimes }
1682df8bae1dSRodney W. Grimes 
1683df8bae1dSRodney W. Grimes /*
1684df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1685df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
16865b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
16875b3047d5SJohn Baldwin  * lock held.
1688df8bae1dSRodney W. Grimes  */
16895b3047d5SJohn Baldwin static void
1690df8bae1dSRodney W. Grimes stop(p)
1691df8bae1dSRodney W. Grimes 	register struct proc *p;
1692df8bae1dSRodney W. Grimes {
1693df8bae1dSRodney W. Grimes 
1694628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
16955b3047d5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
1696df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1697df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1698df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1699df8bae1dSRodney W. Grimes }
1700df8bae1dSRodney W. Grimes 
1701df8bae1dSRodney W. Grimes /*
1702df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1703df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1704df8bae1dSRodney W. Grimes  */
1705df8bae1dSRodney W. Grimes void
17062c42a146SMarcel Moolenaar postsig(sig)
17072c42a146SMarcel Moolenaar 	register int sig;
1708df8bae1dSRodney W. Grimes {
1709b40ce416SJulian Elischer 	struct thread *td = curthread;
1710b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1711628d2653SJohn Baldwin 	struct sigacts *ps;
17122c42a146SMarcel Moolenaar 	sig_t action;
17132c42a146SMarcel Moolenaar 	sigset_t returnmask;
17142c42a146SMarcel Moolenaar 	int code;
1715df8bae1dSRodney W. Grimes 
17162c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
17175526d2d9SEivind Eklund 
17182ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1719628d2653SJohn Baldwin 	ps = p->p_sigacts;
17202c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
17212c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1722df8bae1dSRodney W. Grimes #ifdef KTRACE
1723df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1724645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
17252c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1726df8bae1dSRodney W. Grimes #endif
1727628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
17282a024a2bSSean Eric Fagan 
1729df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1730df8bae1dSRodney W. Grimes 		/*
1731df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1732df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1733df8bae1dSRodney W. Grimes 		 */
1734b40ce416SJulian Elischer 		sigexit(td, sig);
1735df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1736df8bae1dSRodney W. Grimes 	} else {
1737df8bae1dSRodney W. Grimes 		/*
1738df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1739df8bae1dSRodney W. Grimes 		 */
17402c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
17415526d2d9SEivind Eklund 		    ("postsig action"));
1742df8bae1dSRodney W. Grimes 		/*
1743df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1744645682fdSLuoqi Chen 		 * occurrences of this signal.
1745df8bae1dSRodney W. Grimes 		 *
1746645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1747df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1748645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1749df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1750df8bae1dSRodney W. Grimes 		 */
1751645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
17526626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1753645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1754df8bae1dSRodney W. Grimes 		} else
1755df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
17562c42a146SMarcel Moolenaar 
17572c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
17582c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
17592c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
17602c42a146SMarcel Moolenaar 
17612c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1762289ccde0SPeter Wemm 			/*
17632c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1764289ccde0SPeter Wemm 			 */
17652c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
17662c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
17672c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
17682c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
17692c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1770dedc04feSPeter Wemm 		}
1771df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
17722c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1773df8bae1dSRodney W. Grimes 			code = 0;
1774df8bae1dSRodney W. Grimes 		} else {
17756626c604SJulian Elischer 			code = p->p_code;
17766626c604SJulian Elischer 			p->p_code = 0;
17776626c604SJulian Elischer 			p->p_sig = 0;
1778df8bae1dSRodney W. Grimes 		}
17792c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1780df8bae1dSRodney W. Grimes 	}
1781df8bae1dSRodney W. Grimes }
1782df8bae1dSRodney W. Grimes 
1783df8bae1dSRodney W. Grimes /*
1784df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1785df8bae1dSRodney W. Grimes  */
178626f9a767SRodney W. Grimes void
1787df8bae1dSRodney W. Grimes killproc(p, why)
1788df8bae1dSRodney W. Grimes 	struct proc *p;
1789df8bae1dSRodney W. Grimes 	char *why;
1790df8bae1dSRodney W. Grimes {
17919081e5e8SJohn Baldwin 
17929081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
17930384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
17940384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1795729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1796b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1797df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1798df8bae1dSRodney W. Grimes }
1799df8bae1dSRodney W. Grimes 
1800df8bae1dSRodney W. Grimes /*
1801df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1802df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1803df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1804df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1805df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1806df8bae1dSRodney W. Grimes  * does not return.
1807df8bae1dSRodney W. Grimes  */
180826f9a767SRodney W. Grimes void
1809b40ce416SJulian Elischer sigexit(td, sig)
1810b40ce416SJulian Elischer 	struct thread *td;
18112c42a146SMarcel Moolenaar 	int sig;
1812df8bae1dSRodney W. Grimes {
1813b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1814df8bae1dSRodney W. Grimes 
1815628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1816df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
18172c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
18182c42a146SMarcel Moolenaar 		p->p_sig = sig;
1819c364e17eSAndrey A. Chernov 		/*
1820c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1821c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1822c364e17eSAndrey A. Chernov 		 * these messages.)
1823c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1824c364e17eSAndrey A. Chernov 		 */
1825628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1826c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1827c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1828b40ce416SJulian Elischer 		if (coredump(td) == 0)
18292c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
183057308494SJoerg Wunsch 		if (kern_logsigexit)
183157308494SJoerg Wunsch 			log(LOG_INFO,
183257308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
18333d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
1834b1fc0ec1SRobert Watson 			    p->p_ucred ? p->p_ucred->cr_uid : -1,
18352c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
18362c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1837c31146a1SJohn Baldwin 	} else {
1838628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1839628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1840628d2653SJohn Baldwin 			mtx_lock(&Giant);
1841c31146a1SJohn Baldwin 	}
1842b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1843df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1844df8bae1dSRodney W. Grimes }
1845df8bae1dSRodney W. Grimes 
1846c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1847c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1848c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1849c5edb423SSean Eric Fagan 
1850c5edb423SSean Eric Fagan /*
1851c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1852c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1853c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1854c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1855c5edb423SSean Eric Fagan  *	%P	process id (pid)
1856c5edb423SSean Eric Fagan  *	%U	user id (uid)
1857c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1858c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1859c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1860c5edb423SSean Eric Fagan  */
1861c5edb423SSean Eric Fagan 
1862fca666a1SJulian Elischer static char *
1863c5edb423SSean Eric Fagan expand_name(name, uid, pid)
186487f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1865c5edb423SSean Eric Fagan 	char *temp;
1866c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1867c5edb423SSean Eric Fagan 	int i, n;
1868c5edb423SSean Eric Fagan 	char *format = corefilename;
1869ce38ca0fSAlfred Perlstein 	size_t namelen;
1870c5edb423SSean Eric Fagan 
1871ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
18720bfe2990SEivind Eklund 	if (temp == NULL)
18730bfe2990SEivind Eklund 		return NULL;
1874ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1875ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1876c5edb423SSean Eric Fagan 		int l;
1877c5edb423SSean Eric Fagan 		switch (format[i]) {
1878c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1879c5edb423SSean Eric Fagan 			i++;
1880c5edb423SSean Eric Fagan 			switch (format[i]) {
1881c5edb423SSean Eric Fagan 			case '%':
1882c5edb423SSean Eric Fagan 				temp[n++] = '%';
1883c5edb423SSean Eric Fagan 				break;
1884c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1885ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
188687f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1887c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1888c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1889c5edb423SSean Eric Fagan 					return NULL;
1890c5edb423SSean Eric Fagan 				}
1891ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1892ce38ca0fSAlfred Perlstein 				n += namelen;
1893c5edb423SSean Eric Fagan 				break;
1894c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1895ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1896c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
189787f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1898c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1899c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1900c5edb423SSean Eric Fagan 					return NULL;
1901c5edb423SSean Eric Fagan 				}
1902c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1903c5edb423SSean Eric Fagan 				n += l;
1904c5edb423SSean Eric Fagan 				break;
1905c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1906ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1907c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
190887f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1909c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1910c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1911c5edb423SSean Eric Fagan 					return NULL;
1912c5edb423SSean Eric Fagan 				}
1913c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1914c5edb423SSean Eric Fagan 				n += l;
1915c5edb423SSean Eric Fagan 				break;
1916c5edb423SSean Eric Fagan 			default:
1917c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1918c5edb423SSean Eric Fagan 			}
1919c5edb423SSean Eric Fagan 			break;
1920c5edb423SSean Eric Fagan 		default:
1921c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1922c5edb423SSean Eric Fagan 		}
1923c5edb423SSean Eric Fagan 	}
1924ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1925c5edb423SSean Eric Fagan 	return temp;
1926c5edb423SSean Eric Fagan }
1927c5edb423SSean Eric Fagan 
1928df8bae1dSRodney W. Grimes /*
1929fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1930fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1931fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1932fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1933fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1934fca666a1SJulian Elischer  */
1935fca666a1SJulian Elischer 
1936fca666a1SJulian Elischer static int
1937b40ce416SJulian Elischer coredump(struct thread *td)
1938fca666a1SJulian Elischer {
1939b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1940fca666a1SJulian Elischer 	register struct vnode *vp;
1941da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
194206ae1e91SMatthew Dillon 	struct flock lf;
1943fca666a1SJulian Elischer 	struct nameidata nd;
1944fca666a1SJulian Elischer 	struct vattr vattr;
1945e6796b67SKirk McKusick 	int error, error1, flags;
1946f2a2857bSKirk McKusick 	struct mount *mp;
1947fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1948fca666a1SJulian Elischer 	off_t limit;
1949fca666a1SJulian Elischer 
1950628d2653SJohn Baldwin 	PROC_LOCK(p);
1951628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1952fca666a1SJulian Elischer 
1953628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1954628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1955fca666a1SJulian Elischer 		return (EFAULT);
1956628d2653SJohn Baldwin 	}
1957fca666a1SJulian Elischer 
1958fca666a1SJulian Elischer 	/*
195935a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
196035a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
196135a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
196235a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
196335a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
196435a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1965fca666a1SJulian Elischer 	 */
196635a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1967628d2653SJohn Baldwin 	if (limit == 0) {
1968628d2653SJohn Baldwin 		PROC_UNLOCK(p);
196935a2598fSSean Eric Fagan 		return 0;
1970628d2653SJohn Baldwin 	}
1971628d2653SJohn Baldwin 	PROC_UNLOCK(p);
197235a2598fSSean Eric Fagan 
1973f2a2857bSKirk McKusick restart:
1974fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1975ccdbd10cSPeter Pentchev 	if (name == NULL)
1976ccdbd10cSPeter Pentchev 		return (EINVAL);
1977b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
1978e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1979e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1980fca666a1SJulian Elischer 	free(name, M_TEMP);
1981fca666a1SJulian Elischer 	if (error)
1982fca666a1SJulian Elischer 		return (error);
1983762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1984fca666a1SJulian Elischer 	vp = nd.ni_vp;
198506ae1e91SMatthew Dillon 
1986b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
198706ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
198806ae1e91SMatthew Dillon 	lf.l_start = 0;
198906ae1e91SMatthew Dillon 	lf.l_len = 0;
199006ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
199106ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
199206ae1e91SMatthew Dillon 	if (error)
199306ae1e91SMatthew Dillon 		goto out2;
199406ae1e91SMatthew Dillon 
199506ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
199606ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
199706ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1998b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
1999f2a2857bSKirk McKusick 			return (error);
2000f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2001f2a2857bSKirk McKusick 			return (error);
2002f2a2857bSKirk McKusick 		goto restart;
2003f2a2857bSKirk McKusick 	}
2004fca666a1SJulian Elischer 
2005fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
2006fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
2007b40ce416SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2008fca666a1SJulian Elischer 		error = EFAULT;
200906ae1e91SMatthew Dillon 		goto out1;
2010fca666a1SJulian Elischer 	}
2011fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2012fca666a1SJulian Elischer 	vattr.va_size = 0;
201388b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2014b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2015b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
201688b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2017628d2653SJohn Baldwin 	PROC_LOCK(p);
2018fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2019628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2020fca666a1SJulian Elischer 
2021fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2022b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2023fca666a1SJulian Elischer 	  ENOSYS;
2024fca666a1SJulian Elischer 
202506ae1e91SMatthew Dillon out1:
202606ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
202706ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2028f2a2857bSKirk McKusick 	vn_finished_write(mp);
202906ae1e91SMatthew Dillon out2:
2030b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2031fca666a1SJulian Elischer 	if (error == 0)
2032fca666a1SJulian Elischer 		error = error1;
2033fca666a1SJulian Elischer 	return (error);
2034fca666a1SJulian Elischer }
2035fca666a1SJulian Elischer 
2036fca666a1SJulian Elischer /*
2037df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2038df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2039df8bae1dSRodney W. Grimes  */
2040d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2041df8bae1dSRodney W. Grimes struct nosys_args {
2042df8bae1dSRodney W. Grimes 	int	dummy;
2043df8bae1dSRodney W. Grimes };
2044d2d3e875SBruce Evans #endif
2045fb99ab88SMatthew Dillon /*
2046fb99ab88SMatthew Dillon  * MPSAFE
2047fb99ab88SMatthew Dillon  */
2048df8bae1dSRodney W. Grimes /* ARGSUSED */
204926f9a767SRodney W. Grimes int
2050b40ce416SJulian Elischer nosys(td, args)
2051b40ce416SJulian Elischer 	struct thread *td;
2052df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2053df8bae1dSRodney W. Grimes {
2054b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2055b40ce416SJulian Elischer 
2056fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2057628d2653SJohn Baldwin 	PROC_LOCK(p);
2058df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2059628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2060fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2061df8bae1dSRodney W. Grimes 	return (EINVAL);
2062df8bae1dSRodney W. Grimes }
2063831d27a9SDon Lewis 
2064831d27a9SDon Lewis /*
2065831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
2066831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2067831d27a9SDon Lewis  */
2068831d27a9SDon Lewis void
20692c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
2070831d27a9SDon Lewis 	struct sigio *sigio;
20712c42a146SMarcel Moolenaar 	int sig, checkctty;
2072831d27a9SDon Lewis {
2073831d27a9SDon Lewis 	if (sigio == NULL)
2074831d27a9SDon Lewis 		return;
2075831d27a9SDon Lewis 
2076831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2077628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
2078b1fc0ec1SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
20792c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2080628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2081831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2082831d27a9SDon Lewis 		struct proc *p;
2083831d27a9SDon Lewis 
2084628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2085628d2653SJohn Baldwin 			PROC_LOCK(p);
2086b1fc0ec1SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2087831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
20882c42a146SMarcel Moolenaar 				psignal(p, sig);
2089628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2090628d2653SJohn Baldwin 		}
2091831d27a9SDon Lewis 	}
2092831d27a9SDon Lewis }
2093cb679c38SJonathan Lemon 
2094cb679c38SJonathan Lemon static int
2095cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2096cb679c38SJonathan Lemon {
2097cb679c38SJonathan Lemon 	struct proc *p = curproc;
2098cb679c38SJonathan Lemon 
2099cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2100cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2101cb679c38SJonathan Lemon 
2102628d2653SJohn Baldwin 	PROC_LOCK(p);
2103cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2104628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2105cb679c38SJonathan Lemon 
2106cb679c38SJonathan Lemon 	return (0);
2107cb679c38SJonathan Lemon }
2108cb679c38SJonathan Lemon 
2109cb679c38SJonathan Lemon static void
2110cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2111cb679c38SJonathan Lemon {
2112cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2113cb679c38SJonathan Lemon 
2114628d2653SJohn Baldwin 	PROC_LOCK(p);
2115e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2116628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2117cb679c38SJonathan Lemon }
2118cb679c38SJonathan Lemon 
2119cb679c38SJonathan Lemon /*
2120cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2121cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2122cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2123cb679c38SJonathan Lemon  * isn't worth the trouble.
2124cb679c38SJonathan Lemon  */
2125cb679c38SJonathan Lemon static int
2126cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2127cb679c38SJonathan Lemon {
2128cb679c38SJonathan Lemon 
2129cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2130cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2131cb679c38SJonathan Lemon 
2132cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2133cb679c38SJonathan Lemon 			kn->kn_data++;
2134cb679c38SJonathan Lemon 	}
2135cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2136cb679c38SJonathan Lemon }
2137