xref: /freebsd/sys/kern/kern_sig.c (revision 8b94a0616d79a6f8cd2cafa72df7411bf2b2d316)
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>
678f19eb88SIan Dowse #include <sys/syscallsubr.h>
681005a129SJohn Baldwin #include <sys/syslog.h>
69d66a5066SPeter Wemm #include <sys/sysent.h>
70c87e2930SDavid Greenman #include <sys/sysctl.h>
71c5edb423SSean Eric Fagan #include <sys/malloc.h>
7206ae1e91SMatthew Dillon #include <sys/unistd.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <machine/cpu.h>
75df8bae1dSRodney W. Grimes 
7623eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
7723eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
7823eeeff7SPeter Wemm #endif
7923eeeff7SPeter Wemm 
806f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
816f841fb7SMarcel Moolenaar 
824d77a549SAlfred Perlstein static int	coredump(struct thread *);
834d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
849c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
854d77a549SAlfred Perlstein static int	sigprop(int sig);
864d77a549SAlfred Perlstein static void	stop(struct proc *);
874093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
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);
914093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
92a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
93a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
94cb679c38SJonathan Lemon 
95cb679c38SJonathan Lemon struct filterops sig_filtops =
96cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
97cb679c38SJonathan Lemon 
9857308494SJoerg Wunsch static int	kern_logsigexit = 1;
993d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1003d177f46SBill Fumerola     &kern_logsigexit, 0,
1013d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10257308494SJoerg Wunsch 
1032b87b6d4SRobert Watson /*
1042b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1052b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1062b87b6d4SRobert Watson  * in the right situations.
1072b87b6d4SRobert Watson  */
1082b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1092b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1102b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1112b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1122b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1142b87b6d4SRobert Watson 
11522d4b0fbSJohn Polstra int sugid_coredump;
1163d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1173d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
118c87e2930SDavid Greenman 
119e5a28db9SPaul Saab static int	do_coredump = 1;
120e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
121e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
122e5a28db9SPaul Saab 
1232c42a146SMarcel Moolenaar /*
1242c42a146SMarcel Moolenaar  * Signal properties and actions.
1252c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1262c42a146SMarcel Moolenaar  * according to the following properties:
1272c42a146SMarcel Moolenaar  */
1282c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1292c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1302c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1312c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1322c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1332c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1342c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
135da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
136df8bae1dSRodney W. Grimes 
1372c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
138da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
139da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
140da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1412c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
144da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
146da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
150da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
151da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
152da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
153da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
154da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
155da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
156da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
157da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
158da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
159da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
160da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1612c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1622c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
163da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
164da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
165da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
166da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
167da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
168da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1692c42a146SMarcel Moolenaar };
1702c42a146SMarcel Moolenaar 
171fbbeeb6cSBruce Evans /*
172fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
173fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
174fbbeeb6cSBruce Evans  * action, the process stops in issignal().
175e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
176fbbeeb6cSBruce Evans  *
17733510ef1SBruce Evans  * MP SAFE.
178fbbeeb6cSBruce Evans  */
179fbbeeb6cSBruce Evans int
180e602ba25SJulian Elischer cursig(struct thread *td)
181fbbeeb6cSBruce Evans {
182c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
183179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1844093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
185fbbeeb6cSBruce Evans }
186fbbeeb6cSBruce Evans 
18779065dbaSBruce Evans /*
18879065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1894093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1904093529dSJeff Roberson  * unmasked in td_sigmask.
19179065dbaSBruce Evans  */
19279065dbaSBruce Evans void
1934093529dSJeff Roberson signotify(struct thread *td)
19479065dbaSBruce Evans {
1954093529dSJeff Roberson 	struct proc *p;
1964093529dSJeff Roberson 	sigset_t set;
1974093529dSJeff Roberson 
1984093529dSJeff Roberson 	p = td->td_proc;
19979065dbaSBruce Evans 
20079065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2014093529dSJeff Roberson 
2024093529dSJeff Roberson 	/*
2034093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2044093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2054093529dSJeff Roberson 	 */
2064093529dSJeff Roberson 	set = p->p_siglist;
2074093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2084093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2094093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2104093529dSJeff Roberson 
2118b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
21279065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2134093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
21479065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
21579065dbaSBruce Evans 	}
2168b94a061SJohn Baldwin }
2178b94a061SJohn Baldwin 
2188b94a061SJohn Baldwin int
2198b94a061SJohn Baldwin sigonstack(size_t sp)
2208b94a061SJohn Baldwin {
2218b94a061SJohn Baldwin 	struct proc *p = curthread->td_proc;
2228b94a061SJohn Baldwin 
2238b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2248b94a061SJohn Baldwin 	return ((p->p_flag & P_ALTSTACK) ?
2258b94a061SJohn Baldwin #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2268b94a061SJohn Baldwin 	    ((p->p_sigstk.ss_size == 0) ? (p->p_sigstk.ss_flags & SS_ONSTACK) :
2278b94a061SJohn Baldwin 		((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size))
2288b94a061SJohn Baldwin #else
2298b94a061SJohn Baldwin 	    ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size)
2308b94a061SJohn Baldwin #endif
2318b94a061SJohn Baldwin 	    : 0);
2328b94a061SJohn Baldwin }
23379065dbaSBruce Evans 
2346f841fb7SMarcel Moolenaar static __inline int
2356f841fb7SMarcel Moolenaar sigprop(int sig)
2362c42a146SMarcel Moolenaar {
2376f841fb7SMarcel Moolenaar 
2382c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2392c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2402c42a146SMarcel Moolenaar 	return (0);
241df8bae1dSRodney W. Grimes }
2422c42a146SMarcel Moolenaar 
2434093529dSJeff Roberson int
2446f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2452c42a146SMarcel Moolenaar {
2462c42a146SMarcel Moolenaar 	int i;
2472c42a146SMarcel Moolenaar 
2486f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2492c42a146SMarcel Moolenaar 		if (set->__bits[i])
2502c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
251df8bae1dSRodney W. Grimes 	return (0);
252df8bae1dSRodney W. Grimes }
253df8bae1dSRodney W. Grimes 
2542c42a146SMarcel Moolenaar /*
2558f19eb88SIan Dowse  * kern_sigaction
2562c42a146SMarcel Moolenaar  * sigaction
25723eeeff7SPeter Wemm  * freebsd4_sigaction
2582c42a146SMarcel Moolenaar  * osigaction
2592c42a146SMarcel Moolenaar  */
2608f19eb88SIan Dowse int
26123eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2628f19eb88SIan Dowse 	struct thread *td;
2632c42a146SMarcel Moolenaar 	register int sig;
2642c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
26523eeeff7SPeter Wemm 	int flags;
266df8bae1dSRodney W. Grimes {
267628d2653SJohn Baldwin 	register struct sigacts *ps;
2684093529dSJeff Roberson 	struct thread *td0;
2698f19eb88SIan Dowse 	struct proc *p = td->td_proc;
270df8bae1dSRodney W. Grimes 
2712899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2722c42a146SMarcel Moolenaar 		return (EINVAL);
2732c42a146SMarcel Moolenaar 
274628d2653SJohn Baldwin 	PROC_LOCK(p);
275628d2653SJohn Baldwin 	ps = p->p_sigacts;
2762c42a146SMarcel Moolenaar 	if (oact) {
2772c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2782c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2792c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2802c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2812c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2822c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2832c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2842c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2852c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2862c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2872c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2882c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2892c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
290645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2912c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
292645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2932c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2942c42a146SMarcel Moolenaar 	}
2952c42a146SMarcel Moolenaar 	if (act) {
2962c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
297628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
298628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2992c42a146SMarcel Moolenaar 			return (EINVAL);
300628d2653SJohn Baldwin 		}
3012c42a146SMarcel Moolenaar 
302df8bae1dSRodney W. Grimes 		/*
303df8bae1dSRodney W. Grimes 		 * Change setting atomically.
304df8bae1dSRodney W. Grimes 		 */
3052c42a146SMarcel Moolenaar 
3062c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3072c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3082c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
309aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
310aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
31180f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
31280f42b55SIan Dowse 		} else {
31380f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3142c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3152c42a146SMarcel Moolenaar 		}
3162c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3172c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
318df8bae1dSRodney W. Grimes 		else
3192c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3202c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3212c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
322df8bae1dSRodney W. Grimes 		else
3232c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3242c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3252c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3261e41c1b5SSteven Wallace 		else
3272c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3282c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3292c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
330289ccde0SPeter Wemm 		else
3312c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
332df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3332c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3342c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
335df8bae1dSRodney W. Grimes 		else
3368c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
337df8bae1dSRodney W. Grimes #endif
3382c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3392c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
340645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
3416626c604SJulian Elischer 			else
342645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
343ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
344245f17d4SJoerg Wunsch 				/*
3452c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3462c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3472c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3482c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
349245f17d4SJoerg Wunsch 				 */
350245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
351645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3526626c604SJulian Elischer 				else
353645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
354245f17d4SJoerg Wunsch 			} else
355645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
356ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
357ba1551caSIan Dowse 				p->p_procsig->ps_flag |= PS_CLDSIGIGN;
358ba1551caSIan Dowse 			else
359ba1551caSIan Dowse 				p->p_procsig->ps_flag &= ~PS_CLDSIGIGN;
360df8bae1dSRodney W. Grimes 		}
361df8bae1dSRodney W. Grimes 		/*
362df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3632c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3642c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3652c42a146SMarcel Moolenaar 		 * have to restart the process.
366df8bae1dSRodney W. Grimes 		 */
3672c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3682c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3692c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3702c42a146SMarcel Moolenaar 			/* never to be seen again */
3711d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
3724093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3734093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
3742c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3752c42a146SMarcel Moolenaar 				/* easier in psignal */
3762c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3772c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
378645682fdSLuoqi Chen 		} else {
3792c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3802c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3812c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3822c42a146SMarcel Moolenaar 			else
3832c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3842c42a146SMarcel Moolenaar 		}
38523eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
38623eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
38723eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
38823eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
38923eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
39023eeeff7SPeter Wemm 		else
39123eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
39223eeeff7SPeter Wemm #endif
393e8ebc08fSPeter Wemm #ifdef COMPAT_43
394645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
39523eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
39623eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
397645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
398645682fdSLuoqi Chen 		else
399645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
400e8ebc08fSPeter Wemm #endif
401df8bae1dSRodney W. Grimes 	}
402628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4032c42a146SMarcel Moolenaar 	return (0);
4042c42a146SMarcel Moolenaar }
4052c42a146SMarcel Moolenaar 
4062c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4072c42a146SMarcel Moolenaar struct sigaction_args {
4082c42a146SMarcel Moolenaar 	int	sig;
4092c42a146SMarcel Moolenaar 	struct	sigaction *act;
4102c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4112c42a146SMarcel Moolenaar };
4122c42a146SMarcel Moolenaar #endif
413fb99ab88SMatthew Dillon /*
414fb99ab88SMatthew Dillon  * MPSAFE
415fb99ab88SMatthew Dillon  */
4162c42a146SMarcel Moolenaar int
417b40ce416SJulian Elischer sigaction(td, uap)
418b40ce416SJulian Elischer 	struct thread *td;
4192c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4202c42a146SMarcel Moolenaar {
4212c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4222c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4232c42a146SMarcel Moolenaar 	int error;
4242c42a146SMarcel Moolenaar 
4256f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4266f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4272c42a146SMarcel Moolenaar 	if (actp) {
4286f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4292c42a146SMarcel Moolenaar 		if (error)
43044443757STim J. Robbins 			return (error);
4312c42a146SMarcel Moolenaar 	}
43244443757STim J. Robbins 	mtx_lock(&Giant);
4338f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
43444443757STim J. Robbins 	mtx_unlock(&Giant);
4352c42a146SMarcel Moolenaar 	if (oactp && !error) {
4366f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4372c42a146SMarcel Moolenaar 	}
4382c42a146SMarcel Moolenaar 	return (error);
4392c42a146SMarcel Moolenaar }
4402c42a146SMarcel Moolenaar 
44123eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
44223eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
44323eeeff7SPeter Wemm struct freebsd4_sigaction_args {
44423eeeff7SPeter Wemm 	int	sig;
44523eeeff7SPeter Wemm 	struct	sigaction *act;
44623eeeff7SPeter Wemm 	struct	sigaction *oact;
44723eeeff7SPeter Wemm };
44823eeeff7SPeter Wemm #endif
44923eeeff7SPeter Wemm /*
45023eeeff7SPeter Wemm  * MPSAFE
45123eeeff7SPeter Wemm  */
45223eeeff7SPeter Wemm int
45323eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
45423eeeff7SPeter Wemm 	struct thread *td;
45523eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
45623eeeff7SPeter Wemm {
45723eeeff7SPeter Wemm 	struct sigaction act, oact;
45823eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
45923eeeff7SPeter Wemm 	int error;
46023eeeff7SPeter Wemm 
46123eeeff7SPeter Wemm 
46223eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
46323eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
46423eeeff7SPeter Wemm 	if (actp) {
46523eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
46623eeeff7SPeter Wemm 		if (error)
46744443757STim J. Robbins 			return (error);
46823eeeff7SPeter Wemm 	}
46944443757STim J. Robbins 	mtx_lock(&Giant);
47023eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
47144443757STim J. Robbins 	mtx_unlock(&Giant);
47223eeeff7SPeter Wemm 	if (oactp && !error) {
47323eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
47423eeeff7SPeter Wemm 	}
47523eeeff7SPeter Wemm 	return (error);
47623eeeff7SPeter Wemm }
47723eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
47823eeeff7SPeter Wemm 
47931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4802c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4812c42a146SMarcel Moolenaar struct osigaction_args {
4822c42a146SMarcel Moolenaar 	int	signum;
4832c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4842c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4852c42a146SMarcel Moolenaar };
4862c42a146SMarcel Moolenaar #endif
487fb99ab88SMatthew Dillon /*
488fb99ab88SMatthew Dillon  * MPSAFE
489fb99ab88SMatthew Dillon  */
4902c42a146SMarcel Moolenaar int
491b40ce416SJulian Elischer osigaction(td, uap)
492b40ce416SJulian Elischer 	struct thread *td;
4932c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4942c42a146SMarcel Moolenaar {
4952c42a146SMarcel Moolenaar 	struct osigaction sa;
4962c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4972c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4982c42a146SMarcel Moolenaar 	int error;
4992c42a146SMarcel Moolenaar 
5006f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5016f841fb7SMarcel Moolenaar 		return (EINVAL);
502fb99ab88SMatthew Dillon 
5036f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5046f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
505fb99ab88SMatthew Dillon 
5062c42a146SMarcel Moolenaar 	if (nsap) {
5076f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5082c42a146SMarcel Moolenaar 		if (error)
50944443757STim J. Robbins 			return (error);
5102c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5112c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5122c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5132c42a146SMarcel Moolenaar 	}
51444443757STim J. Robbins 	mtx_lock(&Giant);
51523eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
51644443757STim J. Robbins 	mtx_unlock(&Giant);
5172c42a146SMarcel Moolenaar 	if (osap && !error) {
5182c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5192c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5202c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5216f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5222c42a146SMarcel Moolenaar 	}
5232c42a146SMarcel Moolenaar 	return (error);
5242c42a146SMarcel Moolenaar }
52523eeeff7SPeter Wemm 
52623eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
52723eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
52823eeeff7SPeter Wemm int
52923eeeff7SPeter Wemm osigreturn(td, uap)
53023eeeff7SPeter Wemm 	struct thread *td;
53123eeeff7SPeter Wemm 	struct osigreturn_args *uap;
53223eeeff7SPeter Wemm {
53323eeeff7SPeter Wemm 
53423eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
53523eeeff7SPeter Wemm }
53623eeeff7SPeter Wemm #endif
53731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
538df8bae1dSRodney W. Grimes 
539df8bae1dSRodney W. Grimes /*
540df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
541df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
542df8bae1dSRodney W. Grimes  */
543df8bae1dSRodney W. Grimes void
544df8bae1dSRodney W. Grimes siginit(p)
545df8bae1dSRodney W. Grimes 	struct proc *p;
546df8bae1dSRodney W. Grimes {
547df8bae1dSRodney W. Grimes 	register int i;
548df8bae1dSRodney W. Grimes 
549628d2653SJohn Baldwin 	PROC_LOCK(p);
5502c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5512c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
5522c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
553628d2653SJohn Baldwin 	PROC_UNLOCK(p);
554df8bae1dSRodney W. Grimes }
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes /*
557df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
558df8bae1dSRodney W. Grimes  */
559df8bae1dSRodney W. Grimes void
560df8bae1dSRodney W. Grimes execsigs(p)
561df8bae1dSRodney W. Grimes 	register struct proc *p;
562df8bae1dSRodney W. Grimes {
563628d2653SJohn Baldwin 	register struct sigacts *ps;
5642c42a146SMarcel Moolenaar 	register int sig;
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 	/*
567df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5684093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
569df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
570df8bae1dSRodney W. Grimes 	 */
5719b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
572628d2653SJohn Baldwin 	ps = p->p_sigacts;
5732c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
5742c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
5752c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
5762c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5772c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
5782c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
5791d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
5804093529dSJeff Roberson 			/*
5814093529dSJeff Roberson 			 * There is only one thread at this point.
5824093529dSJeff Roberson 			 */
5834093529dSJeff Roberson 			SIGDELSET(FIRST_THREAD_IN_PROC(p)->td_siglist, sig);
584df8bae1dSRodney W. Grimes 		}
5852c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
586df8bae1dSRodney W. Grimes 	}
587df8bae1dSRodney W. Grimes 	/*
5884093529dSJeff Roberson 	 * Clear out the td's sigmask.  Normal processes use the proc sigmask.
5894093529dSJeff Roberson 	 */
5904093529dSJeff Roberson 	SIGEMPTYSET(FIRST_THREAD_IN_PROC(p)->td_sigmask);
5914093529dSJeff Roberson 	/*
592df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
593df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
594df8bae1dSRodney W. Grimes 	 */
595645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
596645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
597645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
5983b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
59980e907a1SPeter Wemm 	/*
60080e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
60180e907a1SPeter Wemm 	 */
602ba1551caSIan Dowse 	p->p_procsig->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
603c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
604c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
605df8bae1dSRodney W. Grimes }
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes /*
608e77daab1SJohn Baldwin  * kern_sigprocmask()
6097c8fdcbdSMatthew Dillon  *
610628d2653SJohn Baldwin  *	Manipulate signal mask.
611df8bae1dSRodney W. Grimes  */
612e77daab1SJohn Baldwin int
613e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6144093529dSJeff Roberson 	struct thread *td;
6152c42a146SMarcel Moolenaar 	int how;
6162c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
617645682fdSLuoqi Chen 	int old;
6182c42a146SMarcel Moolenaar {
6192c42a146SMarcel Moolenaar 	int error;
6202c42a146SMarcel Moolenaar 
6214093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6222c42a146SMarcel Moolenaar 	if (oset != NULL)
6234093529dSJeff Roberson 		*oset = td->td_sigmask;
6242c42a146SMarcel Moolenaar 
6252c42a146SMarcel Moolenaar 	error = 0;
6262c42a146SMarcel Moolenaar 	if (set != NULL) {
6272c42a146SMarcel Moolenaar 		switch (how) {
6282c42a146SMarcel Moolenaar 		case SIG_BLOCK:
629645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6304093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6312c42a146SMarcel Moolenaar 			break;
6322c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6334093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6344093529dSJeff Roberson 			signotify(td);
6352c42a146SMarcel Moolenaar 			break;
6362c42a146SMarcel Moolenaar 		case SIG_SETMASK:
637645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
638645682fdSLuoqi Chen 			if (old)
6394093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
640645682fdSLuoqi Chen 			else
6414093529dSJeff Roberson 				td->td_sigmask = *set;
6424093529dSJeff Roberson 			signotify(td);
6432c42a146SMarcel Moolenaar 			break;
6442c42a146SMarcel Moolenaar 		default:
6452c42a146SMarcel Moolenaar 			error = EINVAL;
6462c42a146SMarcel Moolenaar 			break;
6472c42a146SMarcel Moolenaar 		}
6482c42a146SMarcel Moolenaar 	}
6494093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6502c42a146SMarcel Moolenaar 	return (error);
6512c42a146SMarcel Moolenaar }
6522c42a146SMarcel Moolenaar 
6537c8fdcbdSMatthew Dillon /*
654e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6557c8fdcbdSMatthew Dillon  */
6567c8fdcbdSMatthew Dillon 
657d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
658df8bae1dSRodney W. Grimes struct sigprocmask_args {
659df8bae1dSRodney W. Grimes 	int	how;
6602c42a146SMarcel Moolenaar 	const sigset_t *set;
6612c42a146SMarcel Moolenaar 	sigset_t *oset;
662df8bae1dSRodney W. Grimes };
663d2d3e875SBruce Evans #endif
66426f9a767SRodney W. Grimes int
665b40ce416SJulian Elischer sigprocmask(td, uap)
666b40ce416SJulian Elischer 	register struct thread *td;
667df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
668df8bae1dSRodney W. Grimes {
6692c42a146SMarcel Moolenaar 	sigset_t set, oset;
6702c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6712c42a146SMarcel Moolenaar 	int error;
672df8bae1dSRodney W. Grimes 
6736f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6746f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6752c42a146SMarcel Moolenaar 	if (setp) {
6766f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6772c42a146SMarcel Moolenaar 		if (error)
6782c42a146SMarcel Moolenaar 			return (error);
679df8bae1dSRodney W. Grimes 	}
680e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
6812c42a146SMarcel Moolenaar 	if (osetp && !error) {
6826f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
6832c42a146SMarcel Moolenaar 	}
6842c42a146SMarcel Moolenaar 	return (error);
6852c42a146SMarcel Moolenaar }
6862c42a146SMarcel Moolenaar 
68731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6887c8fdcbdSMatthew Dillon /*
6897c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
6907c8fdcbdSMatthew Dillon  */
6912c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6922c42a146SMarcel Moolenaar struct osigprocmask_args {
6932c42a146SMarcel Moolenaar 	int	how;
6942c42a146SMarcel Moolenaar 	osigset_t mask;
6952c42a146SMarcel Moolenaar };
6962c42a146SMarcel Moolenaar #endif
6972c42a146SMarcel Moolenaar int
698b40ce416SJulian Elischer osigprocmask(td, uap)
699b40ce416SJulian Elischer 	register struct thread *td;
7002c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7012c42a146SMarcel Moolenaar {
7022c42a146SMarcel Moolenaar 	sigset_t set, oset;
7032c42a146SMarcel Moolenaar 	int error;
7042c42a146SMarcel Moolenaar 
7052c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
706e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
707b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
708df8bae1dSRodney W. Grimes 	return (error);
709df8bae1dSRodney W. Grimes }
71031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
711df8bae1dSRodney W. Grimes 
712d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
713df8bae1dSRodney W. Grimes struct sigpending_args {
7142c42a146SMarcel Moolenaar 	sigset_t	*set;
715df8bae1dSRodney W. Grimes };
716d2d3e875SBruce Evans #endif
717fb99ab88SMatthew Dillon /*
718fb99ab88SMatthew Dillon  * MPSAFE
719fb99ab88SMatthew Dillon  */
72026f9a767SRodney W. Grimes int
721a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
722a447cd8bSJeff Roberson {
723a447cd8bSJeff Roberson 	siginfo_t info;
724a447cd8bSJeff Roberson 	sigset_t set;
725a447cd8bSJeff Roberson 	int error;
726a447cd8bSJeff Roberson 
727a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
728a447cd8bSJeff Roberson 	if (error)
729a447cd8bSJeff Roberson 		return (error);
730a447cd8bSJeff Roberson 
731a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
732a447cd8bSJeff Roberson 	if (error)
733a447cd8bSJeff Roberson 		return (error);
734a447cd8bSJeff Roberson 
735a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
736a447cd8bSJeff Roberson 	/* Repost if we got an error. */
737a447cd8bSJeff Roberson 	if (error && info.si_signo)
738a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
739a447cd8bSJeff Roberson 
740a447cd8bSJeff Roberson 	return (error);
741a447cd8bSJeff Roberson }
742a447cd8bSJeff Roberson /*
743a447cd8bSJeff Roberson  * MPSAFE
744a447cd8bSJeff Roberson  */
745a447cd8bSJeff Roberson int
746a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
747a447cd8bSJeff Roberson {
748a447cd8bSJeff Roberson 	struct timespec ts;
749a447cd8bSJeff Roberson 	struct timespec *timeout;
750a447cd8bSJeff Roberson 	sigset_t set;
751a447cd8bSJeff Roberson 	siginfo_t info;
752a447cd8bSJeff Roberson 	int error;
753a447cd8bSJeff Roberson 
754a447cd8bSJeff Roberson 	if (uap->timeout) {
755a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
756a447cd8bSJeff Roberson 		if (error)
757a447cd8bSJeff Roberson 			return (error);
758a447cd8bSJeff Roberson 
759a447cd8bSJeff Roberson 		timeout = &ts;
760a447cd8bSJeff Roberson 	} else
761a447cd8bSJeff Roberson 		timeout = NULL;
762a447cd8bSJeff Roberson 
763a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
764a447cd8bSJeff Roberson 	if (error)
765a447cd8bSJeff Roberson 		return (error);
766a447cd8bSJeff Roberson 
767a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
768a447cd8bSJeff Roberson 	if (error)
769a447cd8bSJeff Roberson 		return (error);
770a447cd8bSJeff Roberson 
771a447cd8bSJeff Roberson 	error = copyout(&info, uap->info, sizeof(info));
772a447cd8bSJeff Roberson 	/* Repost if we got an error. */
773a447cd8bSJeff Roberson 	if (error && info.si_signo)
774a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
775a447cd8bSJeff Roberson 
776a447cd8bSJeff Roberson 	return (error);
777a447cd8bSJeff Roberson }
778a447cd8bSJeff Roberson 
779a447cd8bSJeff Roberson /*
780a447cd8bSJeff Roberson  * MPSAFE
781a447cd8bSJeff Roberson  */
782a447cd8bSJeff Roberson int
783a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
784a447cd8bSJeff Roberson {
785a447cd8bSJeff Roberson 	siginfo_t info;
786a447cd8bSJeff Roberson 	sigset_t set;
787a447cd8bSJeff Roberson 	int error;
788a447cd8bSJeff Roberson 
789a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
790a447cd8bSJeff Roberson 	if (error)
791a447cd8bSJeff Roberson 		return (error);
792a447cd8bSJeff Roberson 
793a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
794a447cd8bSJeff Roberson 	if (error)
795a447cd8bSJeff Roberson 		return (error);
796a447cd8bSJeff Roberson 
797a447cd8bSJeff Roberson 	error = copyout(&info, uap->info, sizeof(info));
798a447cd8bSJeff Roberson 	/* Repost if we got an error. */
799a447cd8bSJeff Roberson 	if (error && info.si_signo)
800a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
801a447cd8bSJeff Roberson 
802a447cd8bSJeff Roberson 	return (error);
803a447cd8bSJeff Roberson }
804a447cd8bSJeff Roberson 
805a447cd8bSJeff Roberson static int
806a447cd8bSJeff Roberson kern_sigtimedwait(struct thread *td, sigset_t set, siginfo_t *info,
807a447cd8bSJeff Roberson     struct timespec *timeout)
808a447cd8bSJeff Roberson {
809a447cd8bSJeff Roberson 	register struct sigacts *ps;
810a447cd8bSJeff Roberson 	sigset_t oldmask;
811a447cd8bSJeff Roberson 	struct proc *p;
812a447cd8bSJeff Roberson 	int error;
813a447cd8bSJeff Roberson 	int sig;
814a447cd8bSJeff Roberson 	int hz;
815a447cd8bSJeff Roberson 
816a447cd8bSJeff Roberson 	p = td->td_proc;
817a447cd8bSJeff Roberson 	error = 0;
818a447cd8bSJeff Roberson 	sig = 0;
819a447cd8bSJeff Roberson 	SIG_CANTMASK(set);
820a447cd8bSJeff Roberson 
821a447cd8bSJeff Roberson 	mtx_lock(&Giant);
822a447cd8bSJeff Roberson 	PROC_LOCK(p);
823a447cd8bSJeff Roberson 
824a447cd8bSJeff Roberson 	ps = p->p_sigacts;
825a447cd8bSJeff Roberson 	oldmask = td->td_sigmask;
826a447cd8bSJeff Roberson 	td->td_sigmask = set;
827a447cd8bSJeff Roberson 	signotify(td);
828a447cd8bSJeff Roberson 
829a447cd8bSJeff Roberson 	sig = cursig(td);
830a447cd8bSJeff Roberson 	if (sig)
831a447cd8bSJeff Roberson 		goto out;
832a447cd8bSJeff Roberson 
833a447cd8bSJeff Roberson 	/*
834a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
835a447cd8bSJeff Roberson 	 * signals.
836a447cd8bSJeff Roberson 	 */
837a447cd8bSJeff Roberson 	if (timeout) {
838a447cd8bSJeff Roberson 		struct timeval tv;
839a447cd8bSJeff Roberson 
840a447cd8bSJeff Roberson 		if (timeout->tv_nsec > 1000000000) {
841a447cd8bSJeff Roberson 			error = EINVAL;
842a447cd8bSJeff Roberson 			goto out;
843a447cd8bSJeff Roberson 		}
844a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
845a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
846a447cd8bSJeff Roberson 	} else
847a447cd8bSJeff Roberson 		hz = 0;
848a447cd8bSJeff Roberson 
849a447cd8bSJeff Roberson 	error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", hz);
850a447cd8bSJeff Roberson 	if (error == EINTR)
851a447cd8bSJeff Roberson 		error = 0;
852a447cd8bSJeff Roberson 	else if (error)
853a447cd8bSJeff Roberson 		goto out;
854a447cd8bSJeff Roberson 
855a447cd8bSJeff Roberson 	sig = cursig(td);
856a447cd8bSJeff Roberson out:
857a447cd8bSJeff Roberson 	td->td_sigmask = oldmask;
858a447cd8bSJeff Roberson 	if (sig) {
859a447cd8bSJeff Roberson 		sig_t action;
860a447cd8bSJeff Roberson 
861a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
862a447cd8bSJeff Roberson #ifdef KTRACE
863a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
864a447cd8bSJeff Roberson 			ktrpsig(sig, action, td->td_flags & TDF_OLDMASK ?
865a447cd8bSJeff Roberson 			    &td->td_oldsigmask : &td->td_sigmask, 0);
866a447cd8bSJeff Roberson #endif
867a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
868a447cd8bSJeff Roberson 
869a447cd8bSJeff Roberson 		if (action == SIG_DFL)
870a447cd8bSJeff Roberson 			sigexit(td, sig);
871a447cd8bSJeff Roberson 			/* NOTREACHED */
872a447cd8bSJeff Roberson 
873a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
874a447cd8bSJeff Roberson 		info->si_signo = sig;
875a447cd8bSJeff Roberson 		info->si_code = 0;
876a447cd8bSJeff Roberson 	}
877a447cd8bSJeff Roberson 
878a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
879a447cd8bSJeff Roberson 	mtx_unlock(&Giant);
880a447cd8bSJeff Roberson 
881a447cd8bSJeff Roberson 	return (error);
882a447cd8bSJeff Roberson }
883a447cd8bSJeff Roberson 
884a447cd8bSJeff Roberson /*
885a447cd8bSJeff Roberson  * MPSAFE
886a447cd8bSJeff Roberson  */
887a447cd8bSJeff Roberson int
888b40ce416SJulian Elischer sigpending(td, uap)
889b40ce416SJulian Elischer 	struct thread *td;
890df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
891df8bae1dSRodney W. Grimes {
892b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
893628d2653SJohn Baldwin 	sigset_t siglist;
894df8bae1dSRodney W. Grimes 
895628d2653SJohn Baldwin 	PROC_LOCK(p);
8961d9c5696SJuli Mallett 	siglist = p->p_siglist;
8974093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
898628d2653SJohn Baldwin 	PROC_UNLOCK(p);
89948e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9002c42a146SMarcel Moolenaar }
9012c42a146SMarcel Moolenaar 
90231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9032c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9042c42a146SMarcel Moolenaar struct osigpending_args {
9052c42a146SMarcel Moolenaar 	int	dummy;
9062c42a146SMarcel Moolenaar };
9072c42a146SMarcel Moolenaar #endif
908fb99ab88SMatthew Dillon /*
909fb99ab88SMatthew Dillon  * MPSAFE
910fb99ab88SMatthew Dillon  */
9112c42a146SMarcel Moolenaar int
912b40ce416SJulian Elischer osigpending(td, uap)
913b40ce416SJulian Elischer 	struct thread *td;
9142c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9152c42a146SMarcel Moolenaar {
916b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9174093529dSJeff Roberson 	sigset_t siglist;
918b40ce416SJulian Elischer 
919628d2653SJohn Baldwin 	PROC_LOCK(p);
9204093529dSJeff Roberson 	siglist = p->p_siglist;
9214093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
922628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9239d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
924df8bae1dSRodney W. Grimes 	return (0);
925df8bae1dSRodney W. Grimes }
92631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
929df8bae1dSRodney W. Grimes /*
930df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
931df8bae1dSRodney W. Grimes  */
932d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
933df8bae1dSRodney W. Grimes struct osigvec_args {
934df8bae1dSRodney W. Grimes 	int	signum;
935df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
936df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
937df8bae1dSRodney W. Grimes };
938d2d3e875SBruce Evans #endif
939fb99ab88SMatthew Dillon /*
940fb99ab88SMatthew Dillon  * MPSAFE
941fb99ab88SMatthew Dillon  */
942df8bae1dSRodney W. Grimes /* ARGSUSED */
94326f9a767SRodney W. Grimes int
944b40ce416SJulian Elischer osigvec(td, uap)
945b40ce416SJulian Elischer 	struct thread *td;
946df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
947df8bae1dSRodney W. Grimes {
948df8bae1dSRodney W. Grimes 	struct sigvec vec;
9492c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
9502c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
9512c42a146SMarcel Moolenaar 	int error;
952df8bae1dSRodney W. Grimes 
9536f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
9546f841fb7SMarcel Moolenaar 		return (EINVAL);
9556f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
9566f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
9572c42a146SMarcel Moolenaar 	if (nsap) {
9586f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
9592c42a146SMarcel Moolenaar 		if (error)
960df8bae1dSRodney W. Grimes 			return (error);
9612c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
9622c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
9632c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
9642c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
965df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
9662c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
967df8bae1dSRodney W. Grimes #endif
968df8bae1dSRodney W. Grimes 	}
969fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
9705edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
971fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
9722c42a146SMarcel Moolenaar 	if (osap && !error) {
9732c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
9742c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
9752c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
9762c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
9772c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
9782c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
9792c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
9802c42a146SMarcel Moolenaar #endif
9816f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
9822c42a146SMarcel Moolenaar 	}
9832c42a146SMarcel Moolenaar 	return (error);
984df8bae1dSRodney W. Grimes }
985df8bae1dSRodney W. Grimes 
986d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
987df8bae1dSRodney W. Grimes struct osigblock_args {
988df8bae1dSRodney W. Grimes 	int	mask;
989df8bae1dSRodney W. Grimes };
990d2d3e875SBruce Evans #endif
991fb99ab88SMatthew Dillon /*
992fb99ab88SMatthew Dillon  * MPSAFE
993fb99ab88SMatthew Dillon  */
99426f9a767SRodney W. Grimes int
995b40ce416SJulian Elischer osigblock(td, uap)
996b40ce416SJulian Elischer 	register struct thread *td;
997df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
998df8bae1dSRodney W. Grimes {
999b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10002c42a146SMarcel Moolenaar 	sigset_t set;
1001df8bae1dSRodney W. Grimes 
10022c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10032c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1004fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1005628d2653SJohn Baldwin 	PROC_LOCK(p);
10064093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10074093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1008628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1009fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1010df8bae1dSRodney W. Grimes 	return (0);
1011df8bae1dSRodney W. Grimes }
1012df8bae1dSRodney W. Grimes 
1013d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1014df8bae1dSRodney W. Grimes struct osigsetmask_args {
1015df8bae1dSRodney W. Grimes 	int	mask;
1016df8bae1dSRodney W. Grimes };
1017d2d3e875SBruce Evans #endif
1018fb99ab88SMatthew Dillon /*
1019fb99ab88SMatthew Dillon  * MPSAFE
1020fb99ab88SMatthew Dillon  */
102126f9a767SRodney W. Grimes int
1022b40ce416SJulian Elischer osigsetmask(td, uap)
1023b40ce416SJulian Elischer 	struct thread *td;
1024df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1025df8bae1dSRodney W. Grimes {
1026b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10272c42a146SMarcel Moolenaar 	sigset_t set;
1028df8bae1dSRodney W. Grimes 
10292c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10302c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1031fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1032628d2653SJohn Baldwin 	PROC_LOCK(p);
10334093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10344093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10354093529dSJeff Roberson 	signotify(td);
1036628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1037fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1038df8bae1dSRodney W. Grimes 	return (0);
1039df8bae1dSRodney W. Grimes }
1040df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1041df8bae1dSRodney W. Grimes 
1042df8bae1dSRodney W. Grimes /*
1043df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
1044df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
1045df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
1046b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1047b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1048b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1049df8bae1dSRodney W. Grimes  */
1050d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1051df8bae1dSRodney W. Grimes struct sigsuspend_args {
10522c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1053df8bae1dSRodney W. Grimes };
1054d2d3e875SBruce Evans #endif
1055fb99ab88SMatthew Dillon /*
1056fb99ab88SMatthew Dillon  * MPSAFE
1057fb99ab88SMatthew Dillon  */
1058df8bae1dSRodney W. Grimes /* ARGSUSED */
105926f9a767SRodney W. Grimes int
1060b40ce416SJulian Elischer sigsuspend(td, uap)
1061b40ce416SJulian Elischer 	struct thread *td;
1062df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1063df8bae1dSRodney W. Grimes {
10642c42a146SMarcel Moolenaar 	sigset_t mask;
10652c42a146SMarcel Moolenaar 	int error;
10662c42a146SMarcel Moolenaar 
10676f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
10682c42a146SMarcel Moolenaar 	if (error)
10692c42a146SMarcel Moolenaar 		return (error);
10708f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
10718f19eb88SIan Dowse }
10728f19eb88SIan Dowse 
10738f19eb88SIan Dowse int
10748f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
10758f19eb88SIan Dowse {
10768f19eb88SIan Dowse 	struct proc *p = td->td_proc;
10778f19eb88SIan Dowse 	register struct sigacts *ps;
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes 	/*
1080645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1081df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1082df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1083df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1084df8bae1dSRodney W. Grimes 	 * to indicate this.
1085df8bae1dSRodney W. Grimes 	 */
1086fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1087628d2653SJohn Baldwin 	PROC_LOCK(p);
1088628d2653SJohn Baldwin 	ps = p->p_sigacts;
10894093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
10904093529dSJeff Roberson 	mtx_lock_spin(&sched_lock);
10914093529dSJeff Roberson 	td->td_flags |= TDF_OLDMASK;
10924093529dSJeff Roberson 	mtx_unlock_spin(&sched_lock);
1093645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
10944093529dSJeff Roberson 	td->td_sigmask = mask;
10954093529dSJeff Roberson 	signotify(td);
109601609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
10972c42a146SMarcel Moolenaar 		/* void */;
1098628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1099fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
11002c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11012c42a146SMarcel Moolenaar 	return (EINTR);
11022c42a146SMarcel Moolenaar }
11032c42a146SMarcel Moolenaar 
110431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
11052c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11062c42a146SMarcel Moolenaar struct osigsuspend_args {
11072c42a146SMarcel Moolenaar 	osigset_t mask;
11082c42a146SMarcel Moolenaar };
11092c42a146SMarcel Moolenaar #endif
1110fb99ab88SMatthew Dillon /*
1111fb99ab88SMatthew Dillon  * MPSAFE
1112fb99ab88SMatthew Dillon  */
11132c42a146SMarcel Moolenaar /* ARGSUSED */
11142c42a146SMarcel Moolenaar int
1115b40ce416SJulian Elischer osigsuspend(td, uap)
1116b40ce416SJulian Elischer 	struct thread *td;
11172c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11182c42a146SMarcel Moolenaar {
1119b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1120645682fdSLuoqi Chen 	sigset_t mask;
1121628d2653SJohn Baldwin 	register struct sigacts *ps;
11222c42a146SMarcel Moolenaar 
1123fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1124628d2653SJohn Baldwin 	PROC_LOCK(p);
1125628d2653SJohn Baldwin 	ps = p->p_sigacts;
11264093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11274093529dSJeff Roberson 	mtx_lock_spin(&sched_lock);
11284093529dSJeff Roberson 	td->td_flags |= TDF_OLDMASK;
11294093529dSJeff Roberson 	mtx_unlock_spin(&sched_lock);
1130645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1131645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11324093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11334093529dSJeff Roberson 	signotify(td);
113401609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1135df8bae1dSRodney W. Grimes 		/* void */;
1136628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1137fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1138df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1139df8bae1dSRodney W. Grimes 	return (EINTR);
1140df8bae1dSRodney W. Grimes }
114131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1144d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1145df8bae1dSRodney W. Grimes struct osigstack_args {
1146df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1147df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1148df8bae1dSRodney W. Grimes };
1149d2d3e875SBruce Evans #endif
1150fb99ab88SMatthew Dillon /*
1151fb99ab88SMatthew Dillon  * MPSAFE
1152fb99ab88SMatthew Dillon  */
1153df8bae1dSRodney W. Grimes /* ARGSUSED */
115426f9a767SRodney W. Grimes int
1155b40ce416SJulian Elischer osigstack(td, uap)
1156b40ce416SJulian Elischer 	struct thread *td;
1157df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1158df8bae1dSRodney W. Grimes {
1159b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1160df8bae1dSRodney W. Grimes 	struct sigstack ss;
1161fb99ab88SMatthew Dillon 	int error = 0;
1162fb99ab88SMatthew Dillon 
1163fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1164df8bae1dSRodney W. Grimes 
1165d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
1166628d2653SJohn Baldwin 		PROC_LOCK(p);
1167645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
1168b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
1169628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1170d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
1171d034d459SMarcel Moolenaar 		if (error)
1172fb99ab88SMatthew Dillon 			goto done2;
1173d034d459SMarcel Moolenaar 	}
1174d034d459SMarcel Moolenaar 
1175d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
1176d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
1177fb99ab88SMatthew Dillon 			goto done2;
1178628d2653SJohn Baldwin 		PROC_LOCK(p);
1179645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
1180645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
1181645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
1182645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
1183628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1184df8bae1dSRodney W. Grimes 	}
1185fb99ab88SMatthew Dillon done2:
1186fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1187fb99ab88SMatthew Dillon 	return (error);
1188df8bae1dSRodney W. Grimes }
1189df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1190df8bae1dSRodney W. Grimes 
1191d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1192df8bae1dSRodney W. Grimes struct sigaltstack_args {
11932c42a146SMarcel Moolenaar 	stack_t	*ss;
11942c42a146SMarcel Moolenaar 	stack_t	*oss;
1195df8bae1dSRodney W. Grimes };
1196d2d3e875SBruce Evans #endif
1197fb99ab88SMatthew Dillon /*
1198fb99ab88SMatthew Dillon  * MPSAFE
1199fb99ab88SMatthew Dillon  */
1200df8bae1dSRodney W. Grimes /* ARGSUSED */
120126f9a767SRodney W. Grimes int
1202b40ce416SJulian Elischer sigaltstack(td, uap)
1203b40ce416SJulian Elischer 	struct thread *td;
1204df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1205df8bae1dSRodney W. Grimes {
12068f19eb88SIan Dowse 	stack_t ss, oss;
12078f19eb88SIan Dowse 	int error;
12088f19eb88SIan Dowse 
12098f19eb88SIan Dowse 	if (uap->ss != NULL) {
12108f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12118f19eb88SIan Dowse 		if (error)
12128f19eb88SIan Dowse 			return (error);
12138f19eb88SIan Dowse 	}
12148f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12158f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12168f19eb88SIan Dowse 	if (error)
12178f19eb88SIan Dowse 		return (error);
12188f19eb88SIan Dowse 	if (uap->oss != NULL)
12198f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12208f19eb88SIan Dowse 	return (error);
12218f19eb88SIan Dowse }
12228f19eb88SIan Dowse 
12238f19eb88SIan Dowse int
12248f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12258f19eb88SIan Dowse {
1226b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1227fb99ab88SMatthew Dillon 	int oonstack;
1228fb99ab88SMatthew Dillon 	int error = 0;
1229fb99ab88SMatthew Dillon 
1230fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1231df8bae1dSRodney W. Grimes 
1232b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1233d034d459SMarcel Moolenaar 
12348f19eb88SIan Dowse 	if (oss != NULL) {
1235628d2653SJohn Baldwin 		PROC_LOCK(p);
12368f19eb88SIan Dowse 		*oss = p->p_sigstk;
12378f19eb88SIan Dowse 		oss->ss_flags = (p->p_flag & P_ALTSTACK)
1238d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1239628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1240df8bae1dSRodney W. Grimes 	}
1241d034d459SMarcel Moolenaar 
12428f19eb88SIan Dowse 	if (ss != NULL) {
1243fb99ab88SMatthew Dillon 		if (oonstack) {
1244fb99ab88SMatthew Dillon 			error = EPERM;
1245fb99ab88SMatthew Dillon 			goto done2;
1246fb99ab88SMatthew Dillon 		}
12478f19eb88SIan Dowse 		if ((ss->ss_flags & ~SS_DISABLE) != 0) {
1248fb99ab88SMatthew Dillon 			error = EINVAL;
1249fb99ab88SMatthew Dillon 			goto done2;
1250fb99ab88SMatthew Dillon 		}
12518f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12528f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1253fb99ab88SMatthew Dillon 				error = ENOMEM;
1254fb99ab88SMatthew Dillon 				goto done2;
1255fb99ab88SMatthew Dillon 			}
1256628d2653SJohn Baldwin 			PROC_LOCK(p);
12578f19eb88SIan Dowse 			p->p_sigstk = *ss;
1258d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
1259628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1260628d2653SJohn Baldwin 		} else {
1261628d2653SJohn Baldwin 			PROC_LOCK(p);
1262d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
1263628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1264628d2653SJohn Baldwin 		}
1265d034d459SMarcel Moolenaar 	}
1266fb99ab88SMatthew Dillon done2:
1267fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1268fb99ab88SMatthew Dillon 	return (error);
1269df8bae1dSRodney W. Grimes }
1270df8bae1dSRodney W. Grimes 
1271d93f860cSPoul-Henning Kamp /*
1272d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1273d93f860cSPoul-Henning Kamp  * cp is calling process.
1274d93f860cSPoul-Henning Kamp  */
127537c84183SPoul-Henning Kamp static int
12769c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
12779c1ab3e0SJohn Baldwin 	register struct thread *td;
12782c42a146SMarcel Moolenaar 	int sig, pgid, all;
1279d93f860cSPoul-Henning Kamp {
1280d93f860cSPoul-Henning Kamp 	register struct proc *p;
1281d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1282d93f860cSPoul-Henning Kamp 	int nfound = 0;
1283d93f860cSPoul-Henning Kamp 
1284553629ebSJake Burkholder 	if (all) {
1285d93f860cSPoul-Henning Kamp 		/*
1286d93f860cSPoul-Henning Kamp 		 * broadcast
1287d93f860cSPoul-Henning Kamp 		 */
12881005a129SJohn Baldwin 		sx_slock(&allproc_lock);
12892e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1290628d2653SJohn Baldwin 			PROC_LOCK(p);
12919c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
12929c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1293628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1294628d2653SJohn Baldwin 				continue;
1295628d2653SJohn Baldwin 			}
1296f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1297d93f860cSPoul-Henning Kamp 				nfound++;
129833a9ed9dSJohn Baldwin 				if (sig)
12992c42a146SMarcel Moolenaar 					psignal(p, sig);
1300628d2653SJohn Baldwin 			}
130133a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1302d93f860cSPoul-Henning Kamp 		}
13031005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1304553629ebSJake Burkholder 	} else {
1305ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1306f591779bSSeigo Tanimura 		if (pgid == 0) {
1307d93f860cSPoul-Henning Kamp 			/*
1308d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1309d93f860cSPoul-Henning Kamp 			 */
13109c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1311f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1312f591779bSSeigo Tanimura 		} else {
1313d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1314f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1315ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1316d93f860cSPoul-Henning Kamp 				return (ESRCH);
1317d93f860cSPoul-Henning Kamp 			}
1318f591779bSSeigo Tanimura 		}
1319ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13202e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1321628d2653SJohn Baldwin 			PROC_LOCK(p);
1322628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1323628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1324628d2653SJohn Baldwin 				continue;
1325628d2653SJohn Baldwin 			}
1326e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
132733a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1328628d2653SJohn Baldwin 				continue;
1329628d2653SJohn Baldwin 			}
1330f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1331d93f860cSPoul-Henning Kamp 				nfound++;
133233a9ed9dSJohn Baldwin 				if (sig)
13332c42a146SMarcel Moolenaar 					psignal(p, sig);
1334628d2653SJohn Baldwin 			}
133533a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1336d93f860cSPoul-Henning Kamp 		}
1337f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1338d93f860cSPoul-Henning Kamp 	}
1339d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1340d93f860cSPoul-Henning Kamp }
1341d93f860cSPoul-Henning Kamp 
1342d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1343df8bae1dSRodney W. Grimes struct kill_args {
1344df8bae1dSRodney W. Grimes 	int	pid;
1345df8bae1dSRodney W. Grimes 	int	signum;
1346df8bae1dSRodney W. Grimes };
1347d2d3e875SBruce Evans #endif
1348fb99ab88SMatthew Dillon /*
1349fb99ab88SMatthew Dillon  * MPSAFE
1350fb99ab88SMatthew Dillon  */
1351df8bae1dSRodney W. Grimes /* ARGSUSED */
135226f9a767SRodney W. Grimes int
1353b40ce416SJulian Elischer kill(td, uap)
1354b40ce416SJulian Elischer 	register struct thread *td;
1355df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1356df8bae1dSRodney W. Grimes {
1357df8bae1dSRodney W. Grimes 	register struct proc *p;
1358fb99ab88SMatthew Dillon 	int error = 0;
1359df8bae1dSRodney W. Grimes 
13606c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1361df8bae1dSRodney W. Grimes 		return (EINVAL);
1362fb99ab88SMatthew Dillon 
1363fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1364df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1365df8bae1dSRodney W. Grimes 		/* kill single process */
1366fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1367fb99ab88SMatthew Dillon 			error = ESRCH;
1368f44d9e24SJohn Baldwin 		} else if ((error = p_cansignal(td, p, uap->signum)) != 0) {
136933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1370fb99ab88SMatthew Dillon 		} else {
137133a9ed9dSJohn Baldwin 			if (uap->signum)
1372df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1373628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1374fb99ab88SMatthew Dillon 			error = 0;
1375df8bae1dSRodney W. Grimes 		}
1376fb99ab88SMatthew Dillon 	} else {
1377df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1378df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
13799c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 1);
1380fb99ab88SMatthew Dillon 			break;
1381df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
13829c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 0);
1383fb99ab88SMatthew Dillon 			break;
1384df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
13859c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, -uap->pid, 0);
1386fb99ab88SMatthew Dillon 			break;
1387df8bae1dSRodney W. Grimes 		}
1388fb99ab88SMatthew Dillon 	}
1389fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1390fb99ab88SMatthew Dillon 	return(error);
1391df8bae1dSRodney W. Grimes }
1392df8bae1dSRodney W. Grimes 
1393df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1394d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1395df8bae1dSRodney W. Grimes struct okillpg_args {
1396df8bae1dSRodney W. Grimes 	int	pgid;
1397df8bae1dSRodney W. Grimes 	int	signum;
1398df8bae1dSRodney W. Grimes };
1399d2d3e875SBruce Evans #endif
1400fb99ab88SMatthew Dillon /*
1401fb99ab88SMatthew Dillon  * MPSAFE
1402fb99ab88SMatthew Dillon  */
1403df8bae1dSRodney W. Grimes /* ARGSUSED */
140426f9a767SRodney W. Grimes int
1405b40ce416SJulian Elischer okillpg(td, uap)
1406b40ce416SJulian Elischer 	struct thread *td;
1407df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1408df8bae1dSRodney W. Grimes {
1409fb99ab88SMatthew Dillon 	int error;
1410df8bae1dSRodney W. Grimes 
14116c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1412df8bae1dSRodney W. Grimes 		return (EINVAL);
1413fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
14149c1ab3e0SJohn Baldwin 	error = killpg1(td, uap->signum, uap->pgid, 0);
1415fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1416fb99ab88SMatthew Dillon 	return (error);
1417df8bae1dSRodney W. Grimes }
1418df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1419df8bae1dSRodney W. Grimes 
1420df8bae1dSRodney W. Grimes /*
1421df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1422df8bae1dSRodney W. Grimes  */
1423df8bae1dSRodney W. Grimes void
14242c42a146SMarcel Moolenaar gsignal(pgid, sig)
14252c42a146SMarcel Moolenaar 	int pgid, sig;
1426df8bae1dSRodney W. Grimes {
1427df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1428df8bae1dSRodney W. Grimes 
1429f591779bSSeigo Tanimura 	if (pgid != 0) {
1430ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1431f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1432ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1433f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14342c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1435f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1436f591779bSSeigo Tanimura 		}
1437f591779bSSeigo Tanimura 	}
1438df8bae1dSRodney W. Grimes }
1439df8bae1dSRodney W. Grimes 
1440df8bae1dSRodney W. Grimes /*
1441df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1442df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1443df8bae1dSRodney W. Grimes  */
1444df8bae1dSRodney W. Grimes void
14452c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1446df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14472c42a146SMarcel Moolenaar 	int sig, checkctty;
1448df8bae1dSRodney W. Grimes {
1449df8bae1dSRodney W. Grimes 	register struct proc *p;
1450df8bae1dSRodney W. Grimes 
1451628d2653SJohn Baldwin 	if (pgrp) {
1452f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1453628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1454628d2653SJohn Baldwin 			PROC_LOCK(p);
1455df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14562c42a146SMarcel Moolenaar 				psignal(p, sig);
1457628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1458628d2653SJohn Baldwin 		}
1459628d2653SJohn Baldwin 	}
1460df8bae1dSRodney W. Grimes }
1461df8bae1dSRodney W. Grimes 
1462df8bae1dSRodney W. Grimes /*
14631bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1464df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1465df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1466356861dbSMatthew Dillon  *
1467356861dbSMatthew Dillon  * MPSAFE
1468df8bae1dSRodney W. Grimes  */
1469df8bae1dSRodney W. Grimes void
14701bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1471df8bae1dSRodney W. Grimes {
14721bf4700bSJeff Roberson 	struct sigacts *ps;
14731bf4700bSJeff Roberson 	struct proc *p;
14741bf4700bSJeff Roberson 
14751bf4700bSJeff Roberson 	p = td->td_proc;
1476df8bae1dSRodney W. Grimes 
1477628d2653SJohn Baldwin 	PROC_LOCK(p);
1478ef3dab76STim J. Robbins 	ps = p->p_sigacts;
14792c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
14804093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1481df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1482df8bae1dSRodney W. Grimes #ifdef KTRACE
1483374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1484374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
14854093529dSJeff Roberson 			    &td->td_sigmask, code);
1486df8bae1dSRodney W. Grimes #endif
1487a88b260aSJuli Mallett 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
14884093529dSJeff Roberson 						&td->td_sigmask, code);
14894093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
14902c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
14914093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
14922c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1493289ccde0SPeter Wemm 			/*
14948f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1495289ccde0SPeter Wemm 			 */
14962c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
14972c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
14982c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
14992c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
15002c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1501dedc04feSPeter Wemm 		}
15026f841fb7SMarcel Moolenaar 	} else {
15036626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15042c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
15054093529dSJeff Roberson 		tdsignal(td, sig);
1506df8bae1dSRodney W. Grimes 	}
1507628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1508df8bae1dSRodney W. Grimes }
1509df8bae1dSRodney W. Grimes 
15104093529dSJeff Roberson static struct thread *
15114093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15124093529dSJeff Roberson {
15134093529dSJeff Roberson 	struct thread *td;
15144093529dSJeff Roberson 
15154093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15164093529dSJeff Roberson 
15174093529dSJeff Roberson 	/*
15184093529dSJeff Roberson 	 * If we know the signal is bound for a specific thread then we
15194093529dSJeff Roberson 	 * assume that we are in that threads context.  This is the case
15204093529dSJeff Roberson 	 * for SIGXCPU, SIGILL, etc.  Otherwise someone did a kill() from
15214093529dSJeff Roberson 	 * userland and the real thread doesn't actually matter.
15224093529dSJeff Roberson 	 */
15234093529dSJeff Roberson 	if ((prop & SA_PROC) != 0 && curthread->td_proc == p)
15244093529dSJeff Roberson 		return (curthread);
15254093529dSJeff Roberson 
15264093529dSJeff Roberson 	/*
15274093529dSJeff Roberson 	 * We should search for the first thread that is blocked in
15284093529dSJeff Roberson 	 * sigsuspend with this signal unmasked.
15294093529dSJeff Roberson 	 */
15304093529dSJeff Roberson 
15314093529dSJeff Roberson 	/* XXX */
15324093529dSJeff Roberson 
15334093529dSJeff Roberson 	/*
15344093529dSJeff Roberson 	 * Find the first thread in the proc that doesn't have this signal
15354093529dSJeff Roberson 	 * masked.
15364093529dSJeff Roberson 	 */
15374093529dSJeff Roberson 	FOREACH_THREAD_IN_PROC(p, td)
15384093529dSJeff Roberson 		if (!SIGISMEMBER(td->td_sigmask, sig))
15394093529dSJeff Roberson 			return (td);
15404093529dSJeff Roberson 
15414093529dSJeff Roberson 	return (FIRST_THREAD_IN_PROC(p));
15424093529dSJeff Roberson }
15434093529dSJeff Roberson 
1544df8bae1dSRodney W. Grimes /*
1545df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1546df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1547df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1548df8bae1dSRodney W. Grimes  *
1549df8bae1dSRodney W. Grimes  * Exceptions:
1550df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1551df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1552df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1553df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1554df8bae1dSRodney W. Grimes  *
1555df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1556df8bae1dSRodney W. Grimes  */
1557df8bae1dSRodney W. Grimes void
15584093529dSJeff Roberson psignal(struct proc *p, int sig)
1559df8bae1dSRodney W. Grimes {
1560b40ce416SJulian Elischer 	struct thread *td;
15614093529dSJeff Roberson 	int prop;
15624093529dSJeff Roberson 
15634093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15644093529dSJeff Roberson 	prop = sigprop(sig);
15654093529dSJeff Roberson 
15664093529dSJeff Roberson 	/*
15674093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
15684093529dSJeff Roberson 	 */
15694093529dSJeff Roberson 	td = sigtd(p, sig, prop);
15704093529dSJeff Roberson 
15714093529dSJeff Roberson 	tdsignal(td, sig);
15724093529dSJeff Roberson }
15734093529dSJeff Roberson 
15744093529dSJeff Roberson void
15754093529dSJeff Roberson tdsignal(struct thread *td, int sig)
15764093529dSJeff Roberson {
15774093529dSJeff Roberson 	struct proc *p;
15784093529dSJeff Roberson 	register sig_t action;
15794093529dSJeff Roberson 	sigset_t *siglist;
15804093529dSJeff Roberson 	struct thread *td0;
1581e602ba25SJulian Elischer 	register int prop;
1582e602ba25SJulian Elischer 
1583df8bae1dSRodney W. Grimes 
15842899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
15854093529dSJeff Roberson 	    ("tdsignal(): invalid signal %d\n", sig));
15864093529dSJeff Roberson 
15874093529dSJeff Roberson 	p = td->td_proc;
15882c42a146SMarcel Moolenaar 
1589628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1590cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1591cb679c38SJonathan Lemon 
15922c42a146SMarcel Moolenaar 	prop = sigprop(sig);
15934093529dSJeff Roberson 
15944093529dSJeff Roberson 	/*
15954093529dSJeff Roberson 	 * If this thread is blocking this signal then we'll leave it in the
15964093529dSJeff Roberson 	 * proc so that we can find it in the first thread that unblocks it.
15974093529dSJeff Roberson 	 */
15984093529dSJeff Roberson 	if (SIGISMEMBER(td->td_sigmask, sig))
15994093529dSJeff Roberson 		siglist = &p->p_siglist;
16004093529dSJeff Roberson 	else
16014093529dSJeff Roberson 		siglist = &td->td_siglist;
16024093529dSJeff Roberson 
1603df8bae1dSRodney W. Grimes 	/*
16042a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
16052a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
16062a024a2bSSean Eric Fagan 	 * a chance, as well.
1607df8bae1dSRodney W. Grimes 	 */
1608b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1609df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1610b40ce416SJulian Elischer 	} else {
1611df8bae1dSRodney W. Grimes 		/*
1612df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1613df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1614df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1615df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1616df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1617df8bae1dSRodney W. Grimes 		 */
1618628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1619df8bae1dSRodney W. Grimes 			return;
16204093529dSJeff Roberson 		if (SIGISMEMBER(td->td_sigmask, sig))
1621df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
16222c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1623df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1624df8bae1dSRodney W. Grimes 		else
1625df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1626df8bae1dSRodney W. Grimes 	}
1627df8bae1dSRodney W. Grimes 
16284093529dSJeff Roberson 	if (prop & SA_CONT) {
16291d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
16304093529dSJeff Roberson 		/*
16314093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
16324093529dSJeff Roberson 		 * the proc's siglist.
16334093529dSJeff Roberson 		 */
16344093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
16354093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
16364093529dSJeff Roberson 	}
1637df8bae1dSRodney W. Grimes 
1638df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1639df8bae1dSRodney W. Grimes 		/*
1640df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1641df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1642df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1643df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1644df8bae1dSRodney W. Grimes 		 */
1645e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1646e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1647e602ba25SJulian Elischer 		    (action == SIG_DFL))
1648df8bae1dSRodney W. Grimes 		        return;
16491d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
16504093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
16514093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
16526933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1653df8bae1dSRodney W. Grimes 	}
16544093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
16554093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
16565312b1c7SDavid Xu 	/*
16575312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
16585312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
16595312b1c7SDavid Xu 	 */
16605312b1c7SDavid Xu 	if (action == SIG_HOLD &&
16615312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
16625312b1c7SDavid Xu 		return;
1663df8bae1dSRodney W. Grimes 	/*
1664e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1665e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1666e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1667e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1668e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1669e602ba25SJulian Elischer 	 * We try do the per-process part here.
1670df8bae1dSRodney W. Grimes 	 */
1671e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1672e602ba25SJulian Elischer 		/*
1673e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1674e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1675e602ba25SJulian Elischer 		 */
1676e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1677e602ba25SJulian Elischer 			/*
1678e602ba25SJulian Elischer 			 * The traced process is already stopped,
1679e602ba25SJulian Elischer 			 * so no further action is necessary.
1680e602ba25SJulian Elischer 			 * No signal can restart us.
1681e602ba25SJulian Elischer 			 */
1682e602ba25SJulian Elischer 			goto out;
16831c32c37cSJohn Baldwin 		}
1684b40ce416SJulian Elischer 
1685e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1686df8bae1dSRodney W. Grimes 			/*
1687e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1688e602ba25SJulian Elischer 			 * It will die elsewhere.
1689e602ba25SJulian Elischer 			 * All threads must be restarted.
1690df8bae1dSRodney W. Grimes 			 */
1691e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1692e602ba25SJulian Elischer 			goto runfast;
1693e602ba25SJulian Elischer 		}
1694e602ba25SJulian Elischer 
1695e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1696e602ba25SJulian Elischer 			/*
1697e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
16984093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
16991d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1700e602ba25SJulian Elischer 			 * continue the process and leave the signal in
17014093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1702e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1703e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1704e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1705e602ba25SJulian Elischer 			 */
17061279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
17076933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1708e602ba25SJulian Elischer 			if (action == SIG_DFL) {
17094093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1710e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1711e602ba25SJulian Elischer 				/*
1712e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1713e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1714e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1715e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1716e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1717e602ba25SJulian Elischer 				 * process, we need to make sure that the
1718e602ba25SJulian Elischer 				 * single thread is runnable asap.
1719e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1720e602ba25SJulian Elischer 				 */
1721e602ba25SJulian Elischer 				goto runfast;
1722e602ba25SJulian Elischer 			}
1723e602ba25SJulian Elischer 			/*
1724e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1725e602ba25SJulian Elischer 			 */
1726e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
172704774f23SJulian Elischer 			thread_unsuspend(p);
1728e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1729e602ba25SJulian Elischer 			goto out;
1730e602ba25SJulian Elischer 		}
1731e602ba25SJulian Elischer 
1732e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1733e602ba25SJulian Elischer 			/*
1734e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1735e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
173604774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1737e602ba25SJulian Elischer 			 */
17381279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
17394093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1740e602ba25SJulian Elischer 			goto out;
1741e602ba25SJulian Elischer 		}
1742e602ba25SJulian Elischer 
1743e602ba25SJulian Elischer 		/*
1744e602ba25SJulian Elischer 		 * All other kinds of signals:
1745e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1746e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1747e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
174804774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1749e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1750e602ba25SJulian Elischer 		 */
1751e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
175271fad9fdSJulian Elischer 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1753e602ba25SJulian Elischer 			if (td->td_flags & TDF_CVWAITQ)
175471fad9fdSJulian Elischer 				cv_abort(td);
1755e602ba25SJulian Elischer 			else
175671fad9fdSJulian Elischer 				abortsleep(td);
1757e602ba25SJulian Elischer 		}
1758e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1759df8bae1dSRodney W. Grimes 		goto out;
1760df8bae1dSRodney W. Grimes 		/*
1761e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1762e602ba25SJulian Elischer 		 * Shouldn't they abort too?
176304774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
176404774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1765df8bae1dSRodney W. Grimes 		 */
1766e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
176735c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
176835c32a76SDavid Xu 			!(prop & SA_STOP)) {
1769721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
17704093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1771721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1772df8bae1dSRodney W. Grimes 			goto out;
177304774f23SJulian Elischer 		}
177435c32a76SDavid Xu 		if (prop & SA_STOP) {
177535c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
177635c32a76SDavid Xu 				goto out;
1777e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1778e574e444SDavid Xu 			p->p_xstat = sig;
177935c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
17804093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
17814093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
178235c32a76SDavid Xu 					(td->td_flags & TDF_SINTR))
17834093529dSJeff Roberson 					thread_suspend_one(td0);
178435c32a76SDavid Xu 			}
1785e574e444SDavid Xu 			thread_stopped(p);
17864093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1787e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
17884093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
17894093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
17904093529dSJeff Roberson 			}
179135c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
179235c32a76SDavid Xu 			goto out;
179335c32a76SDavid Xu 		}
1794721e5910SJulian Elischer 		else
1795df8bae1dSRodney W. Grimes 			goto runfast;
1796df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1797e602ba25SJulian Elischer 	} else {
1798e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
17994093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1800e602ba25SJulian Elischer 		goto out;
1801e602ba25SJulian Elischer 	}
1802e602ba25SJulian Elischer 
1803b40ce416SJulian Elischer 	/*
1804e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1805e602ba25SJulian Elischer 	 * running threads.
1806b40ce416SJulian Elischer 	 */
1807e602ba25SJulian Elischer 
1808e602ba25SJulian Elischer runfast:
1809aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
18104093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1811e602ba25SJulian Elischer 	thread_unsuspend(p);
1812e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1813e602ba25SJulian Elischer out:
1814e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1815e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1816e602ba25SJulian Elischer }
1817e602ba25SJulian Elischer 
1818e602ba25SJulian Elischer /*
1819e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1820e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1821e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1822e602ba25SJulian Elischer  */
1823e602ba25SJulian Elischer static void
18244093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1825e602ba25SJulian Elischer {
1826e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1827e602ba25SJulian Elischer 	register int prop;
1828e602ba25SJulian Elischer 
18298b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1830aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1831e602ba25SJulian Elischer 	prop = sigprop(sig);
1832e602ba25SJulian Elischer 	/*
1833aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1834e602ba25SJulian Elischer 	 * killed in this lifetime.
1835e602ba25SJulian Elischer 	 */
1836e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1837e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1838e602ba25SJulian Elischer 			td->td_priority = PUSER;
1839b40ce416SJulian Elischer 		}
1840b40ce416SJulian Elischer 	}
184171fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1842e602ba25SJulian Elischer 		/*
1843e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1844e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1845e602ba25SJulian Elischer 		 * be noticed when the process returns through
1846e602ba25SJulian Elischer 		 * trap() or syscall().
1847e602ba25SJulian Elischer 		 */
1848e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1849aa0fa334SJulian Elischer 			return;
1850e602ba25SJulian Elischer 		}
1851e602ba25SJulian Elischer 		/*
1852e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1853e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1854e602ba25SJulian Elischer 		 * for its parent.
1855e602ba25SJulian Elischer 		 */
1856e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1857e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1858aa0fa334SJulian Elischer 		} else {
1859aa0fa334SJulian Elischer 
1860df8bae1dSRodney W. Grimes 			/*
1861e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1862e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1863e602ba25SJulian Elischer 			 * be awakened.
1864df8bae1dSRodney W. Grimes 			 */
1865e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
18661d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
18674093529dSJeff Roberson 				/*
18684093529dSJeff Roberson 				 * It may be on either list in this state.
18694093529dSJeff Roberson 				 * Remove from both for now.
18704093529dSJeff Roberson 				 */
18714093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1872aa0fa334SJulian Elischer 				return;
1873df8bae1dSRodney W. Grimes 			}
1874df8bae1dSRodney W. Grimes 
1875aa0fa334SJulian Elischer 			/*
1876aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1877aa0fa334SJulian Elischer 			 */
1878aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
1879aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1880aa0fa334SJulian Elischer 			}
1881aa0fa334SJulian Elischer 		}
188271fad9fdSJulian Elischer 		if (td->td_flags & TDF_CVWAITQ)
188371fad9fdSJulian Elischer 			cv_abort(td);
188471fad9fdSJulian Elischer 		else
188571fad9fdSJulian Elischer 			abortsleep(td);
1886aa0fa334SJulian Elischer 	}
1887aa0fa334SJulian Elischer #ifdef SMP
1888aa0fa334SJulian Elischer 	  else {
1889df8bae1dSRodney W. Grimes 		/*
1890e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
1891df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1892df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1893df8bae1dSRodney W. Grimes 		 */
189471fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td) && td != curthread) {
1895e602ba25SJulian Elischer 			forward_signal(td);
1896aa0fa334SJulian Elischer 		}
18970ac3b636SAndrew Gallatin 	  }
18983163861cSTor Egge #endif
18996caa8a15SJohn Baldwin }
1900df8bae1dSRodney W. Grimes 
1901df8bae1dSRodney W. Grimes /*
1902df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1903df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1904df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1905df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1906df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1907628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
1908df8bae1dSRodney W. Grimes  * sequence is
1909df8bae1dSRodney W. Grimes  *
1910e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
19112c42a146SMarcel Moolenaar  *		postsig(sig);
1912df8bae1dSRodney W. Grimes  */
191326f9a767SRodney W. Grimes int
1914e602ba25SJulian Elischer issignal(td)
1915e602ba25SJulian Elischer 	struct thread *td;
1916df8bae1dSRodney W. Grimes {
1917e602ba25SJulian Elischer 	struct proc *p;
19184093529dSJeff Roberson 	sigset_t sigpending;
19192c42a146SMarcel Moolenaar 	register int sig, prop;
1920df8bae1dSRodney W. Grimes 
1921e602ba25SJulian Elischer 	p = td->td_proc;
1922628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
192326306795SJohn Baldwin 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &p->p_mtx.mtx_object,
192426306795SJohn Baldwin 	    "Checking for signals");
1925df8bae1dSRodney W. Grimes 	for (;;) {
19262a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
19272a024a2bSSean Eric Fagan 
19284093529dSJeff Roberson 		sigpending = td->td_siglist;
19294093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
19304093529dSJeff Roberson 
1931df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
19324093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
19334093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
1934df8bae1dSRodney W. Grimes 			return (0);
19354093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
19362c42a146SMarcel Moolenaar 		prop = sigprop(sig);
19372a024a2bSSean Eric Fagan 
1938628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
19392a024a2bSSean Eric Fagan 
1940df8bae1dSRodney W. Grimes 		/*
1941df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1942df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1943df8bae1dSRodney W. Grimes 		 */
19442c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
19454093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
1946df8bae1dSRodney W. Grimes 			continue;
1947df8bae1dSRodney W. Grimes 		}
1948df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1949df8bae1dSRodney W. Grimes 			/*
1950d8f4f6a4SJonathan Mini 			 * If traced, always stop.
1951df8bae1dSRodney W. Grimes 			 */
19522c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1953628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1954df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1955628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
19569ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
19574d492b43SJulian Elischer 			stop(p);	/* uses schedlock too eventually */
195871fad9fdSJulian Elischer 			thread_suspend_one(td);
1959c86b6ff5SJohn Baldwin 			PROC_UNLOCK(p);
1960c86b6ff5SJohn Baldwin 			DROP_GIANT();
19612ad7d304SJohn Baldwin 			p->p_stats->p_ru.ru_nivcsw++;
1962df8bae1dSRodney W. Grimes 			mi_switch();
19639ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
196420cdcc5bSJohn Baldwin 			PICKUP_GIANT();
1965628d2653SJohn Baldwin 			PROC_LOCK(p);
1966df8bae1dSRodney W. Grimes 
1967df8bae1dSRodney W. Grimes 			/*
1968df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1969df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1970df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1971df8bae1dSRodney W. Grimes 			 */
1972df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1973df8bae1dSRodney W. Grimes 				continue;
1974df8bae1dSRodney W. Grimes 
1975df8bae1dSRodney W. Grimes 			/*
1976df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1977df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1978df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1979df8bae1dSRodney W. Grimes 			 */
19804093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
19812c42a146SMarcel Moolenaar 			sig = p->p_xstat;
19822c42a146SMarcel Moolenaar 			if (sig == 0)
1983df8bae1dSRodney W. Grimes 				continue;
1984df8bae1dSRodney W. Grimes 
1985df8bae1dSRodney W. Grimes 			/*
19864093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
19871d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
1988df8bae1dSRodney W. Grimes 			 */
19894093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
19904093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
1991df8bae1dSRodney W. Grimes 				continue;
19924093529dSJeff Roberson 			signotify(td);
1993df8bae1dSRodney W. Grimes 		}
1994df8bae1dSRodney W. Grimes 
1995df8bae1dSRodney W. Grimes 		/*
1996df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1997df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1998df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1999df8bae1dSRodney W. Grimes 		 */
2000d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2001df8bae1dSRodney W. Grimes 
2002d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2003df8bae1dSRodney W. Grimes 			/*
2004df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2005df8bae1dSRodney W. Grimes 			 */
2006df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2007df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2008df8bae1dSRodney W. Grimes 				/*
2009df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2010df8bae1dSRodney W. Grimes 				 * in init? XXX
2011df8bae1dSRodney W. Grimes 				 */
2012d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
20132c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2014df8bae1dSRodney W. Grimes #endif
2015df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2016df8bae1dSRodney W. Grimes 			}
2017df8bae1dSRodney W. Grimes 			/*
2018df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2019df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2020df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2021df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2022df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2023df8bae1dSRodney W. Grimes 			 */
2024df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2025df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2026df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2027df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2028df8bae1dSRodney W. Grimes 					break;	/* == ignore */
2029e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
20302c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2031721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2032e574e444SDavid Xu 				thread_stopped(p);
203371fad9fdSJulian Elischer 				thread_suspend_one(td);
2034721e5910SJulian Elischer 				PROC_UNLOCK(p);
2035721e5910SJulian Elischer 				DROP_GIANT();
2036721e5910SJulian Elischer 				p->p_stats->p_ru.ru_nivcsw++;
2037721e5910SJulian Elischer 				mi_switch();
2038721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2039721e5910SJulian Elischer 				PICKUP_GIANT();
2040721e5910SJulian Elischer 				PROC_LOCK(p);
2041df8bae1dSRodney W. Grimes 				break;
204221b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2043df8bae1dSRodney W. Grimes 				/*
2044df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2045df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2046df8bae1dSRodney W. Grimes 				 */
2047df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2048df8bae1dSRodney W. Grimes 			} else
20492c42a146SMarcel Moolenaar 				return (sig);
2050df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2051df8bae1dSRodney W. Grimes 
2052d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2053df8bae1dSRodney W. Grimes 			/*
2054df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2055df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2056df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2057df8bae1dSRodney W. Grimes 			 */
2058df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2059df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2060df8bae1dSRodney W. Grimes 				printf("issignal\n");
2061df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2062df8bae1dSRodney W. Grimes 
2063df8bae1dSRodney W. Grimes 		default:
2064df8bae1dSRodney W. Grimes 			/*
2065df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2066df8bae1dSRodney W. Grimes 			 * postsig() process it.
2067df8bae1dSRodney W. Grimes 			 */
20682c42a146SMarcel Moolenaar 			return (sig);
2069df8bae1dSRodney W. Grimes 		}
20704093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2071df8bae1dSRodney W. Grimes 	}
2072df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2073df8bae1dSRodney W. Grimes }
2074df8bae1dSRodney W. Grimes 
2075df8bae1dSRodney W. Grimes /*
2076df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2077df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
20785b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
20795b3047d5SJohn Baldwin  * lock held.
2080df8bae1dSRodney W. Grimes  */
20815b3047d5SJohn Baldwin static void
2082e574e444SDavid Xu stop(struct proc *p)
2083df8bae1dSRodney W. Grimes {
2084df8bae1dSRodney W. Grimes 
2085628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
20861279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2087df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
208801609114SAlfred Perlstein 	wakeup(p->p_pptr);
2089df8bae1dSRodney W. Grimes }
2090df8bae1dSRodney W. Grimes 
2091e574e444SDavid Xu void
2092e574e444SDavid Xu thread_stopped(struct proc *p)
2093e574e444SDavid Xu {
2094e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
2095e574e444SDavid Xu 	int n;
2096e574e444SDavid Xu 
2097e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2098e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2099e574e444SDavid Xu 	n = p->p_suspcount;
2100e574e444SDavid Xu 	if (p == p1)
2101e574e444SDavid Xu 		n++;
2102e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2103e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2104e574e444SDavid Xu 		stop(p);
2105e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
2106e574e444SDavid Xu 		if ((p->p_pptr->p_procsig->ps_flag &
2107e574e444SDavid Xu 			PS_NOCLDSTOP) == 0) {
2108e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
2109e574e444SDavid Xu 		}
2110e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2111e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2112e574e444SDavid Xu 	}
2113e574e444SDavid Xu }
2114e574e444SDavid Xu 
2115df8bae1dSRodney W. Grimes /*
2116df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2117df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2118df8bae1dSRodney W. Grimes  */
2119df8bae1dSRodney W. Grimes void
21202c42a146SMarcel Moolenaar postsig(sig)
21212c42a146SMarcel Moolenaar 	register int sig;
2122df8bae1dSRodney W. Grimes {
2123b40ce416SJulian Elischer 	struct thread *td = curthread;
2124b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2125628d2653SJohn Baldwin 	struct sigacts *ps;
21262c42a146SMarcel Moolenaar 	sig_t action;
21272c42a146SMarcel Moolenaar 	sigset_t returnmask;
21282c42a146SMarcel Moolenaar 	int code;
2129df8bae1dSRodney W. Grimes 
21302c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
21315526d2d9SEivind Eklund 
21322ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2133628d2653SJohn Baldwin 	ps = p->p_sigacts;
21344093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
21352c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2136df8bae1dSRodney W. Grimes #ifdef KTRACE
2137374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
21384093529dSJeff Roberson 		ktrpsig(sig, action, td->td_flags & TDF_OLDMASK ?
21394093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2140df8bae1dSRodney W. Grimes #endif
2141628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
21422a024a2bSSean Eric Fagan 
2143df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
2144df8bae1dSRodney W. Grimes 		/*
2145df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2146df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2147df8bae1dSRodney W. Grimes 		 */
2148b40ce416SJulian Elischer 		sigexit(td, sig);
2149df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2150df8bae1dSRodney W. Grimes 	} else {
2151df8bae1dSRodney W. Grimes 		/*
2152df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2153df8bae1dSRodney W. Grimes 		 */
21544093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
21555526d2d9SEivind Eklund 		    ("postsig action"));
2156df8bae1dSRodney W. Grimes 		/*
2157df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2158645682fdSLuoqi Chen 		 * occurrences of this signal.
2159df8bae1dSRodney W. Grimes 		 *
2160645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2161df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2162645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2163df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2164df8bae1dSRodney W. Grimes 		 */
21654093529dSJeff Roberson 		if (td->td_flags & TDF_OLDMASK) {
21664093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
21678b94a061SJohn Baldwin 			mtx_lock_spin(&sched_lock);
21684093529dSJeff Roberson 			td->td_flags &= ~TDF_OLDMASK;
21698b94a061SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
2170df8bae1dSRodney W. Grimes 		} else
21714093529dSJeff Roberson 			returnmask = td->td_sigmask;
21722c42a146SMarcel Moolenaar 
21734093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
21742c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
21754093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
21762c42a146SMarcel Moolenaar 
21772c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2178289ccde0SPeter Wemm 			/*
21798f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2180289ccde0SPeter Wemm 			 */
21812c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
21822c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
21832c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
21842c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
21852c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2186dedc04feSPeter Wemm 		}
2187df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
21882c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2189df8bae1dSRodney W. Grimes 			code = 0;
2190df8bae1dSRodney W. Grimes 		} else {
21916626c604SJulian Elischer 			code = p->p_code;
21926626c604SJulian Elischer 			p->p_code = 0;
21936626c604SJulian Elischer 			p->p_sig = 0;
2194df8bae1dSRodney W. Grimes 		}
2195ac2e4153SJulian Elischer 		if (p->p_flag & P_THREADED)
219658a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
219758a3c273SJeff Roberson 		else
219858a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
219958a3c273SJeff Roberson 			    &returnmask, code);
2200df8bae1dSRodney W. Grimes 	}
2201df8bae1dSRodney W. Grimes }
2202df8bae1dSRodney W. Grimes 
2203df8bae1dSRodney W. Grimes /*
2204df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2205df8bae1dSRodney W. Grimes  */
220626f9a767SRodney W. Grimes void
2207df8bae1dSRodney W. Grimes killproc(p, why)
2208df8bae1dSRodney W. Grimes 	struct proc *p;
2209df8bae1dSRodney W. Grimes 	char *why;
2210df8bae1dSRodney W. Grimes {
22119081e5e8SJohn Baldwin 
22129081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22130384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
22140384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2215729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2216b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2217df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2218df8bae1dSRodney W. Grimes }
2219df8bae1dSRodney W. Grimes 
2220df8bae1dSRodney W. Grimes /*
2221df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2222df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2223df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2224df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2225df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2226df8bae1dSRodney W. Grimes  * does not return.
2227df8bae1dSRodney W. Grimes  */
222826f9a767SRodney W. Grimes void
2229b40ce416SJulian Elischer sigexit(td, sig)
2230b40ce416SJulian Elischer 	struct thread *td;
22312c42a146SMarcel Moolenaar 	int sig;
2232df8bae1dSRodney W. Grimes {
2233b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2234df8bae1dSRodney W. Grimes 
2235628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2236df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
22372c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
22382c42a146SMarcel Moolenaar 		p->p_sig = sig;
2239c364e17eSAndrey A. Chernov 		/*
2240c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2241c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2242c364e17eSAndrey A. Chernov 		 * these messages.)
2243c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
2244c364e17eSAndrey A. Chernov 		 */
2245628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2246c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
2247c31146a1SJohn Baldwin 			mtx_lock(&Giant);
2248b40ce416SJulian Elischer 		if (coredump(td) == 0)
22492c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
225057308494SJoerg Wunsch 		if (kern_logsigexit)
225157308494SJoerg Wunsch 			log(LOG_INFO,
225257308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
22533d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
22549c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
22552c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
22562c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
2257c31146a1SJohn Baldwin 	} else {
2258628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2259628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
2260628d2653SJohn Baldwin 			mtx_lock(&Giant);
2261c31146a1SJohn Baldwin 	}
2262b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2263df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2264df8bae1dSRodney W. Grimes }
2265df8bae1dSRodney W. Grimes 
2266c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2267c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2268c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2269c5edb423SSean Eric Fagan 
2270c5edb423SSean Eric Fagan /*
2271c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2272c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2273c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2274c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2275c5edb423SSean Eric Fagan  *	%P	process id (pid)
2276c5edb423SSean Eric Fagan  *	%U	user id (uid)
2277c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2278c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2279c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2280c5edb423SSean Eric Fagan  */
2281c5edb423SSean Eric Fagan 
2282fca666a1SJulian Elischer static char *
2283c5edb423SSean Eric Fagan expand_name(name, uid, pid)
22848b43b535SAlfred Perlstein 	const char *name;
22858b43b535SAlfred Perlstein 	uid_t uid;
22868b43b535SAlfred Perlstein 	pid_t pid;
22878b43b535SAlfred Perlstein {
22888b43b535SAlfred Perlstein 	const char *format, *appendstr;
2289c5edb423SSean Eric Fagan 	char *temp;
2290c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
22918b43b535SAlfred Perlstein 	size_t i, l, n;
2292c5edb423SSean Eric Fagan 
22938b43b535SAlfred Perlstein 	format = corefilename;
22948b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
22950bfe2990SEivind Eklund 	if (temp == NULL)
22968b43b535SAlfred Perlstein 		return (NULL);
2297ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2298c5edb423SSean Eric Fagan 		switch (format[i]) {
2299c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2300c5edb423SSean Eric Fagan 			i++;
2301c5edb423SSean Eric Fagan 			switch (format[i]) {
2302c5edb423SSean Eric Fagan 			case '%':
23038b43b535SAlfred Perlstein 				appendstr = "%";
2304c5edb423SSean Eric Fagan 				break;
2305c5edb423SSean Eric Fagan 			case 'N':	/* process name */
23068b43b535SAlfred Perlstein 				appendstr = name;
2307c5edb423SSean Eric Fagan 				break;
2308c5edb423SSean Eric Fagan 			case 'P':	/* process id */
23098b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
23108b43b535SAlfred Perlstein 				appendstr = buf;
2311c5edb423SSean Eric Fagan 				break;
2312c5edb423SSean Eric Fagan 			case 'U':	/* user id */
23138b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
23148b43b535SAlfred Perlstein 				appendstr = buf;
2315c5edb423SSean Eric Fagan 				break;
2316c5edb423SSean Eric Fagan 			default:
23178b43b535SAlfred Perlstein 				appendstr = "";
23188b43b535SAlfred Perlstein 			  	log(LOG_ERR,
23198b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
23208b43b535SAlfred Perlstein 				    format[i], format);
2321c5edb423SSean Eric Fagan 			}
23228b43b535SAlfred Perlstein 			l = strlen(appendstr);
23238b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
23248b43b535SAlfred Perlstein 				goto toolong;
23258b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
23268b43b535SAlfred Perlstein 			n += l;
2327c5edb423SSean Eric Fagan 			break;
2328c5edb423SSean Eric Fagan 		default:
2329c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2330c5edb423SSean Eric Fagan 		}
2331c5edb423SSean Eric Fagan 	}
23328b43b535SAlfred Perlstein 	if (format[i] != '\0')
23338b43b535SAlfred Perlstein 		goto toolong;
23348b43b535SAlfred Perlstein 	return (temp);
23358b43b535SAlfred Perlstein toolong:
23368b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
23378b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
23388b43b535SAlfred Perlstein 	free(temp, M_TEMP);
23398b43b535SAlfred Perlstein 	return (NULL);
2340c5edb423SSean Eric Fagan }
2341c5edb423SSean Eric Fagan 
2342df8bae1dSRodney W. Grimes /*
2343fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2344fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2345fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2346fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2347fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2348fca666a1SJulian Elischer  */
2349fca666a1SJulian Elischer 
2350fca666a1SJulian Elischer static int
2351b40ce416SJulian Elischer coredump(struct thread *td)
2352fca666a1SJulian Elischer {
2353b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2354fca666a1SJulian Elischer 	register struct vnode *vp;
23559c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
235606ae1e91SMatthew Dillon 	struct flock lf;
2357fca666a1SJulian Elischer 	struct nameidata nd;
2358fca666a1SJulian Elischer 	struct vattr vattr;
2359e6796b67SKirk McKusick 	int error, error1, flags;
2360f2a2857bSKirk McKusick 	struct mount *mp;
2361fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2362fca666a1SJulian Elischer 	off_t limit;
2363fca666a1SJulian Elischer 
2364628d2653SJohn Baldwin 	PROC_LOCK(p);
2365628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2366fca666a1SJulian Elischer 
2367628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2368628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2369fca666a1SJulian Elischer 		return (EFAULT);
2370628d2653SJohn Baldwin 	}
2371fca666a1SJulian Elischer 
2372fca666a1SJulian Elischer 	/*
237335a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
237435a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
237535a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
237635a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
237735a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
237835a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2379fca666a1SJulian Elischer 	 */
238035a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2381628d2653SJohn Baldwin 	if (limit == 0) {
2382628d2653SJohn Baldwin 		PROC_UNLOCK(p);
238335a2598fSSean Eric Fagan 		return 0;
2384628d2653SJohn Baldwin 	}
2385628d2653SJohn Baldwin 	PROC_UNLOCK(p);
238635a2598fSSean Eric Fagan 
2387f2a2857bSKirk McKusick restart:
23889c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2389ccdbd10cSPeter Pentchev 	if (name == NULL)
2390ccdbd10cSPeter Pentchev 		return (EINVAL);
2391b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2392e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2393e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2394fca666a1SJulian Elischer 	free(name, M_TEMP);
2395fca666a1SJulian Elischer 	if (error)
2396fca666a1SJulian Elischer 		return (error);
2397762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2398fca666a1SJulian Elischer 	vp = nd.ni_vp;
239906ae1e91SMatthew Dillon 
2400832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2401832dafadSDon Lewis 	if (vp->v_type != VREG ||
2402832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2403832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2404832dafadSDon Lewis 		error = EFAULT;
2405832dafadSDon Lewis 		goto out2;
2406832dafadSDon Lewis 	}
2407832dafadSDon Lewis 
2408b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
240906ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
241006ae1e91SMatthew Dillon 	lf.l_start = 0;
241106ae1e91SMatthew Dillon 	lf.l_len = 0;
241206ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
241306ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
241406ae1e91SMatthew Dillon 	if (error)
241506ae1e91SMatthew Dillon 		goto out2;
241606ae1e91SMatthew Dillon 
241706ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
241806ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
241906ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2420b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2421f2a2857bSKirk McKusick 			return (error);
2422f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2423f2a2857bSKirk McKusick 			return (error);
2424f2a2857bSKirk McKusick 		goto restart;
2425f2a2857bSKirk McKusick 	}
2426fca666a1SJulian Elischer 
2427fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2428fca666a1SJulian Elischer 	vattr.va_size = 0;
242988b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2430b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2431b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
243288b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2433628d2653SJohn Baldwin 	PROC_LOCK(p);
2434fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2435628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2436fca666a1SJulian Elischer 
2437fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2438b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2439fca666a1SJulian Elischer 	  ENOSYS;
2440fca666a1SJulian Elischer 
244106ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
244206ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2443f2a2857bSKirk McKusick 	vn_finished_write(mp);
244406ae1e91SMatthew Dillon out2:
2445b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2446fca666a1SJulian Elischer 	if (error == 0)
2447fca666a1SJulian Elischer 		error = error1;
2448fca666a1SJulian Elischer 	return (error);
2449fca666a1SJulian Elischer }
2450fca666a1SJulian Elischer 
2451fca666a1SJulian Elischer /*
2452df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2453df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2454df8bae1dSRodney W. Grimes  */
2455d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2456df8bae1dSRodney W. Grimes struct nosys_args {
2457df8bae1dSRodney W. Grimes 	int	dummy;
2458df8bae1dSRodney W. Grimes };
2459d2d3e875SBruce Evans #endif
2460fb99ab88SMatthew Dillon /*
2461fb99ab88SMatthew Dillon  * MPSAFE
2462fb99ab88SMatthew Dillon  */
2463df8bae1dSRodney W. Grimes /* ARGSUSED */
246426f9a767SRodney W. Grimes int
2465b40ce416SJulian Elischer nosys(td, args)
2466b40ce416SJulian Elischer 	struct thread *td;
2467df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2468df8bae1dSRodney W. Grimes {
2469b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2470b40ce416SJulian Elischer 
2471fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2472628d2653SJohn Baldwin 	PROC_LOCK(p);
2473df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2474628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2475fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2476f5216b9aSBruce Evans 	return (ENOSYS);
2477df8bae1dSRodney W. Grimes }
2478831d27a9SDon Lewis 
2479831d27a9SDon Lewis /*
248048f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2481831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2482831d27a9SDon Lewis  */
2483831d27a9SDon Lewis void
2484f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2485f1320723SAlfred Perlstein 	struct sigio **sigiop;
24862c42a146SMarcel Moolenaar 	int sig, checkctty;
2487831d27a9SDon Lewis {
2488f1320723SAlfred Perlstein 	struct sigio *sigio;
2489831d27a9SDon Lewis 
2490f1320723SAlfred Perlstein 	SIGIO_LOCK();
2491f1320723SAlfred Perlstein 	sigio = *sigiop;
2492f1320723SAlfred Perlstein 	if (sigio == NULL) {
2493f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2494f1320723SAlfred Perlstein 		return;
2495f1320723SAlfred Perlstein 	}
2496831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2497628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
24982b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
24992c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2500628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2501831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2502831d27a9SDon Lewis 		struct proc *p;
2503831d27a9SDon Lewis 
2504f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2505628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2506628d2653SJohn Baldwin 			PROC_LOCK(p);
25072b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2508831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
25092c42a146SMarcel Moolenaar 				psignal(p, sig);
2510628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2511628d2653SJohn Baldwin 		}
2512f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2513831d27a9SDon Lewis 	}
2514f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2515831d27a9SDon Lewis }
2516cb679c38SJonathan Lemon 
2517cb679c38SJonathan Lemon static int
2518cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2519cb679c38SJonathan Lemon {
2520cb679c38SJonathan Lemon 	struct proc *p = curproc;
2521cb679c38SJonathan Lemon 
2522cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2523cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2524cb679c38SJonathan Lemon 
2525628d2653SJohn Baldwin 	PROC_LOCK(p);
2526cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2527628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2528cb679c38SJonathan Lemon 
2529cb679c38SJonathan Lemon 	return (0);
2530cb679c38SJonathan Lemon }
2531cb679c38SJonathan Lemon 
2532cb679c38SJonathan Lemon static void
2533cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2534cb679c38SJonathan Lemon {
2535cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2536cb679c38SJonathan Lemon 
2537628d2653SJohn Baldwin 	PROC_LOCK(p);
2538e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2539628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2540cb679c38SJonathan Lemon }
2541cb679c38SJonathan Lemon 
2542cb679c38SJonathan Lemon /*
2543cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2544cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2545cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2546cb679c38SJonathan Lemon  * isn't worth the trouble.
2547cb679c38SJonathan Lemon  */
2548cb679c38SJonathan Lemon static int
2549cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2550cb679c38SJonathan Lemon {
2551cb679c38SJonathan Lemon 
2552cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2553cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2554cb679c38SJonathan Lemon 
2555cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2556cb679c38SJonathan Lemon 			kn->kn_data++;
2557cb679c38SJonathan Lemon 	}
2558cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2559cb679c38SJonathan Lemon }
2560