xref: /freebsd/sys/kern/kern_sig.c (revision 1930e303cfa1b9fe2c50fdad0d1ba6143311a53c)
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  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
405591b823SEivind Eklund #include "opt_compat.h"
41db6a20e2SGarrett Wollman #include "opt_ktrace.h"
42db6a20e2SGarrett Wollman 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
4436240ea5SDoug Rabson #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
46df8bae1dSRodney W. Grimes #include <sys/vnode.h>
47df8bae1dSRodney W. Grimes #include <sys/acct.h>
48238510fcSJason Evans #include <sys/condvar.h>
49854dc8c2SJohn Baldwin #include <sys/event.h>
50854dc8c2SJohn Baldwin #include <sys/fcntl.h>
51854dc8c2SJohn Baldwin #include <sys/kernel.h>
529dde3bc9SDavid Xu #include <sys/kse.h>
530384fff8SJason Evans #include <sys/ktr.h>
54df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
55854dc8c2SJohn Baldwin #include <sys/lock.h>
56854dc8c2SJohn Baldwin #include <sys/malloc.h>
57854dc8c2SJohn Baldwin #include <sys/mutex.h>
58854dc8c2SJohn Baldwin #include <sys/namei.h>
59854dc8c2SJohn Baldwin #include <sys/proc.h>
60854dc8c2SJohn Baldwin #include <sys/pioctl.h>
61c31146a1SJohn Baldwin #include <sys/resourcevar.h>
6244f3b092SJohn Baldwin #include <sys/sleepqueue.h>
636caa8a15SJohn Baldwin #include <sys/smp.h>
64df8bae1dSRodney W. Grimes #include <sys/stat.h>
651005a129SJohn Baldwin #include <sys/sx.h>
668f19eb88SIan Dowse #include <sys/syscallsubr.h>
67c87e2930SDavid Greenman #include <sys/sysctl.h>
68854dc8c2SJohn Baldwin #include <sys/sysent.h>
69854dc8c2SJohn Baldwin #include <sys/syslog.h>
70854dc8c2SJohn Baldwin #include <sys/sysproto.h>
7106ae1e91SMatthew Dillon #include <sys/unistd.h>
72854dc8c2SJohn Baldwin #include <sys/wait.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);
856711f10fSJohn Baldwin static int	issignal(struct thread *p);
864d77a549SAlfred Perlstein static int	sigprop(int sig);
874d77a549SAlfred Perlstein static void	stop(struct proc *);
884093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
89cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
90cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
91cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
924093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
93a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
94a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
95c197abc4SMike Makonnen static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
96cb679c38SJonathan Lemon 
97cb679c38SJonathan Lemon struct filterops sig_filtops =
98cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
99cb679c38SJonathan Lemon 
10057308494SJoerg Wunsch static int	kern_logsigexit = 1;
1013d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1023d177f46SBill Fumerola     &kern_logsigexit, 0,
1033d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10457308494SJoerg Wunsch 
1052b87b6d4SRobert Watson /*
1062b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1072b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1082b87b6d4SRobert Watson  * in the right situations.
1092b87b6d4SRobert Watson  */
1102b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1112b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1122b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1142b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1152b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1162b87b6d4SRobert Watson 
11722d4b0fbSJohn Polstra int sugid_coredump;
1183d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1193d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
120c87e2930SDavid Greenman 
121e5a28db9SPaul Saab static int	do_coredump = 1;
122e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
123e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
124e5a28db9SPaul Saab 
1252c42a146SMarcel Moolenaar /*
1262c42a146SMarcel Moolenaar  * Signal properties and actions.
1272c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1282c42a146SMarcel Moolenaar  * according to the following properties:
1292c42a146SMarcel Moolenaar  */
1302c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1312c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1322c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1332c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1342c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1352c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1362c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
137da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
138df8bae1dSRodney W. Grimes 
1392c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
140da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
141da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
142da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
146da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
148da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1502c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1512c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
152da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
153da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
154da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
155da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
156da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
157da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
158da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
159da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
160da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
161da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
162da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1632c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1642c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
165da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
166da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
167da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
168da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
169da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
170da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1712c42a146SMarcel Moolenaar };
1722c42a146SMarcel Moolenaar 
173fbbeeb6cSBruce Evans /*
174fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
175fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
176fbbeeb6cSBruce Evans  * action, the process stops in issignal().
177e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
178fbbeeb6cSBruce Evans  *
17933510ef1SBruce Evans  * MP SAFE.
180fbbeeb6cSBruce Evans  */
181fbbeeb6cSBruce Evans int
182e602ba25SJulian Elischer cursig(struct thread *td)
183fbbeeb6cSBruce Evans {
184c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
18590af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
186179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1874093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
188fbbeeb6cSBruce Evans }
189fbbeeb6cSBruce Evans 
19079065dbaSBruce Evans /*
19179065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1924093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1934093529dSJeff Roberson  * unmasked in td_sigmask.
19479065dbaSBruce Evans  */
19579065dbaSBruce Evans void
1964093529dSJeff Roberson signotify(struct thread *td)
19779065dbaSBruce Evans {
1984093529dSJeff Roberson 	struct proc *p;
1999dde3bc9SDavid Xu 	sigset_t set, saved;
2004093529dSJeff Roberson 
2014093529dSJeff Roberson 	p = td->td_proc;
20279065dbaSBruce Evans 
20379065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2044093529dSJeff Roberson 
2054093529dSJeff Roberson 	/*
2064093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2074093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2084093529dSJeff Roberson 	 */
2094093529dSJeff Roberson 	set = p->p_siglist;
2109dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
2119dde3bc9SDavid Xu 		saved = p->p_siglist;
2124093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2134093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2144093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2154093529dSJeff Roberson 
2168b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
21779065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2184093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
21979065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
22079065dbaSBruce Evans 	}
2219dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2229dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
2239dde3bc9SDavid Xu 			return;
2249dde3bc9SDavid Xu 		else {
2259dde3bc9SDavid Xu 			/* pending set changed */
2269dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
2279dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
2289dde3bc9SDavid Xu 		}
2299dde3bc9SDavid Xu 	}
2308b94a061SJohn Baldwin }
2318b94a061SJohn Baldwin 
2328b94a061SJohn Baldwin int
2338b94a061SJohn Baldwin sigonstack(size_t sp)
2348b94a061SJohn Baldwin {
235a30ec4b9SDavid Xu 	struct thread *td = curthread;
2368b94a061SJohn Baldwin 
237a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
2381930e303SPoul-Henning Kamp #if defined(COMPAT_43)
239a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
240a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
241a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
2428b94a061SJohn Baldwin #else
243a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
2448b94a061SJohn Baldwin #endif
2458b94a061SJohn Baldwin 	    : 0);
2468b94a061SJohn Baldwin }
24779065dbaSBruce Evans 
2486f841fb7SMarcel Moolenaar static __inline int
2496f841fb7SMarcel Moolenaar sigprop(int sig)
2502c42a146SMarcel Moolenaar {
2516f841fb7SMarcel Moolenaar 
2522c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2532c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2542c42a146SMarcel Moolenaar 	return (0);
255df8bae1dSRodney W. Grimes }
2562c42a146SMarcel Moolenaar 
2574093529dSJeff Roberson int
2586f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2592c42a146SMarcel Moolenaar {
2602c42a146SMarcel Moolenaar 	int i;
2612c42a146SMarcel Moolenaar 
2626f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2632c42a146SMarcel Moolenaar 		if (set->__bits[i])
2642c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
265df8bae1dSRodney W. Grimes 	return (0);
266df8bae1dSRodney W. Grimes }
267df8bae1dSRodney W. Grimes 
2682c42a146SMarcel Moolenaar /*
2698f19eb88SIan Dowse  * kern_sigaction
2702c42a146SMarcel Moolenaar  * sigaction
27123eeeff7SPeter Wemm  * freebsd4_sigaction
2722c42a146SMarcel Moolenaar  * osigaction
27325d6dc06SJohn Baldwin  *
27425d6dc06SJohn Baldwin  * MPSAFE
2752c42a146SMarcel Moolenaar  */
2768f19eb88SIan Dowse int
27723eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2788f19eb88SIan Dowse 	struct thread *td;
2792c42a146SMarcel Moolenaar 	register int sig;
2802c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
28123eeeff7SPeter Wemm 	int flags;
282df8bae1dSRodney W. Grimes {
28390af4afaSJohn Baldwin 	struct sigacts *ps;
2844093529dSJeff Roberson 	struct thread *td0;
2858f19eb88SIan Dowse 	struct proc *p = td->td_proc;
286df8bae1dSRodney W. Grimes 
2872899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2882c42a146SMarcel Moolenaar 		return (EINVAL);
2892c42a146SMarcel Moolenaar 
290628d2653SJohn Baldwin 	PROC_LOCK(p);
291628d2653SJohn Baldwin 	ps = p->p_sigacts;
29290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2932c42a146SMarcel Moolenaar 	if (oact) {
2942c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2952c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2962c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2972c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2982c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2992c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
3002c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
3012c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
3022c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
3032c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
3042c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
3052c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
3062c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
30790af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
3082c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
30990af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
3102c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
3112c42a146SMarcel Moolenaar 	}
3122c42a146SMarcel Moolenaar 	if (act) {
3132c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
314628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
31590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
316628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3172c42a146SMarcel Moolenaar 			return (EINVAL);
318628d2653SJohn Baldwin 		}
3192c42a146SMarcel Moolenaar 
320df8bae1dSRodney W. Grimes 		/*
321df8bae1dSRodney W. Grimes 		 * Change setting atomically.
322df8bae1dSRodney W. Grimes 		 */
3232c42a146SMarcel Moolenaar 
3242c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3252c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3262c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
327aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
328aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
32980f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
33080f42b55SIan Dowse 		} else {
33180f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3322c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3332c42a146SMarcel Moolenaar 		}
3342c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3352c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
336df8bae1dSRodney W. Grimes 		else
3372c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3382c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3392c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
340df8bae1dSRodney W. Grimes 		else
3412c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3422c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3432c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3441e41c1b5SSteven Wallace 		else
3452c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3462c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3472c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
348289ccde0SPeter Wemm 		else
3492c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
3502c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3512c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
35290af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3536626c604SJulian Elischer 			else
35490af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
355ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
356245f17d4SJoerg Wunsch 				/*
3572c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3582c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3592c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3602c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
361245f17d4SJoerg Wunsch 				 */
362245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
36390af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3646626c604SJulian Elischer 				else
36590af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
366245f17d4SJoerg Wunsch 			} else
36790af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
368ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
36990af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
370ba1551caSIan Dowse 			else
37190af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
372df8bae1dSRodney W. Grimes 		}
373df8bae1dSRodney W. Grimes 		/*
37490af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3752c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
37690af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3772c42a146SMarcel Moolenaar 		 * have to restart the process.
378df8bae1dSRodney W. Grimes 		 */
3792c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3802c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3812c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3829dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
3839dde3bc9SDavid Xu 			     SIGISMEMBER(p->p_siglist, sig)) {
3849dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
3859dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
3869dde3bc9SDavid Xu 			}
3872c42a146SMarcel Moolenaar 			/* never to be seen again */
3881d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
389a9a48d68SDavid Xu 			mtx_lock_spin(&sched_lock);
3904093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3914093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
392a9a48d68SDavid Xu 			mtx_unlock_spin(&sched_lock);
3932c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3942c42a146SMarcel Moolenaar 				/* easier in psignal */
39590af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
39690af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
397645682fdSLuoqi Chen 		} else {
39890af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
3992c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
40090af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
4012c42a146SMarcel Moolenaar 			else
40290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
4032c42a146SMarcel Moolenaar 		}
40423eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
40523eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
40623eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
40723eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
40823eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
40923eeeff7SPeter Wemm 		else
41023eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
41123eeeff7SPeter Wemm #endif
412e8ebc08fSPeter Wemm #ifdef COMPAT_43
413645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
41423eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41523eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
416645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
417645682fdSLuoqi Chen 		else
418645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
419e8ebc08fSPeter Wemm #endif
420df8bae1dSRodney W. Grimes 	}
42190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
422628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4232c42a146SMarcel Moolenaar 	return (0);
4242c42a146SMarcel Moolenaar }
4252c42a146SMarcel Moolenaar 
4262c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4272c42a146SMarcel Moolenaar struct sigaction_args {
4282c42a146SMarcel Moolenaar 	int	sig;
4292c42a146SMarcel Moolenaar 	struct	sigaction *act;
4302c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4312c42a146SMarcel Moolenaar };
4322c42a146SMarcel Moolenaar #endif
433fb99ab88SMatthew Dillon /*
434fb99ab88SMatthew Dillon  * MPSAFE
435fb99ab88SMatthew Dillon  */
4362c42a146SMarcel Moolenaar int
437b40ce416SJulian Elischer sigaction(td, uap)
438b40ce416SJulian Elischer 	struct thread *td;
4392c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4402c42a146SMarcel Moolenaar {
4412c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4422c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4432c42a146SMarcel Moolenaar 	int error;
4442c42a146SMarcel Moolenaar 
4456f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4466f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4472c42a146SMarcel Moolenaar 	if (actp) {
4486f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4492c42a146SMarcel Moolenaar 		if (error)
45044443757STim J. Robbins 			return (error);
4512c42a146SMarcel Moolenaar 	}
4528f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
45325d6dc06SJohn Baldwin 	if (oactp && !error)
4546f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4552c42a146SMarcel Moolenaar 	return (error);
4562c42a146SMarcel Moolenaar }
4572c42a146SMarcel Moolenaar 
45823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
45923eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
46023eeeff7SPeter Wemm struct freebsd4_sigaction_args {
46123eeeff7SPeter Wemm 	int	sig;
46223eeeff7SPeter Wemm 	struct	sigaction *act;
46323eeeff7SPeter Wemm 	struct	sigaction *oact;
46423eeeff7SPeter Wemm };
46523eeeff7SPeter Wemm #endif
46623eeeff7SPeter Wemm /*
46723eeeff7SPeter Wemm  * MPSAFE
46823eeeff7SPeter Wemm  */
46923eeeff7SPeter Wemm int
47023eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
47123eeeff7SPeter Wemm 	struct thread *td;
47223eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
47323eeeff7SPeter Wemm {
47423eeeff7SPeter Wemm 	struct sigaction act, oact;
47523eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
47623eeeff7SPeter Wemm 	int error;
47723eeeff7SPeter Wemm 
47823eeeff7SPeter Wemm 
47923eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
48023eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
48123eeeff7SPeter Wemm 	if (actp) {
48223eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
48323eeeff7SPeter Wemm 		if (error)
48444443757STim J. Robbins 			return (error);
48523eeeff7SPeter Wemm 	}
48623eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
487a14e1189SJohn Baldwin 	if (oactp && !error)
48823eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
48923eeeff7SPeter Wemm 	return (error);
49023eeeff7SPeter Wemm }
49123eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
49223eeeff7SPeter Wemm 
49331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4942c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4952c42a146SMarcel Moolenaar struct osigaction_args {
4962c42a146SMarcel Moolenaar 	int	signum;
4972c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4982c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4992c42a146SMarcel Moolenaar };
5002c42a146SMarcel Moolenaar #endif
501fb99ab88SMatthew Dillon /*
502fb99ab88SMatthew Dillon  * MPSAFE
503fb99ab88SMatthew Dillon  */
5042c42a146SMarcel Moolenaar int
505b40ce416SJulian Elischer osigaction(td, uap)
506b40ce416SJulian Elischer 	struct thread *td;
5072c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
5082c42a146SMarcel Moolenaar {
5092c42a146SMarcel Moolenaar 	struct osigaction sa;
5102c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5112c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5122c42a146SMarcel Moolenaar 	int error;
5132c42a146SMarcel Moolenaar 
5146f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5156f841fb7SMarcel Moolenaar 		return (EINVAL);
516fb99ab88SMatthew Dillon 
5176f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5186f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
519fb99ab88SMatthew Dillon 
5202c42a146SMarcel Moolenaar 	if (nsap) {
5216f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5222c42a146SMarcel Moolenaar 		if (error)
52344443757STim J. Robbins 			return (error);
5242c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5252c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5262c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5272c42a146SMarcel Moolenaar 	}
52823eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5292c42a146SMarcel Moolenaar 	if (osap && !error) {
5302c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5312c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5322c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5336f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5342c42a146SMarcel Moolenaar 	}
5352c42a146SMarcel Moolenaar 	return (error);
5362c42a146SMarcel Moolenaar }
53723eeeff7SPeter Wemm 
53823eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
53923eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
54023eeeff7SPeter Wemm int
54123eeeff7SPeter Wemm osigreturn(td, uap)
54223eeeff7SPeter Wemm 	struct thread *td;
54323eeeff7SPeter Wemm 	struct osigreturn_args *uap;
54423eeeff7SPeter Wemm {
54523eeeff7SPeter Wemm 
54623eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
54723eeeff7SPeter Wemm }
54823eeeff7SPeter Wemm #endif
54931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes /*
552df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
553df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
554df8bae1dSRodney W. Grimes  */
555df8bae1dSRodney W. Grimes void
556df8bae1dSRodney W. Grimes siginit(p)
557df8bae1dSRodney W. Grimes 	struct proc *p;
558df8bae1dSRodney W. Grimes {
559df8bae1dSRodney W. Grimes 	register int i;
56090af4afaSJohn Baldwin 	struct sigacts *ps;
561df8bae1dSRodney W. Grimes 
562628d2653SJohn Baldwin 	PROC_LOCK(p);
56390af4afaSJohn Baldwin 	ps = p->p_sigacts;
56490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5652c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5662c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
56790af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
56890af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
569628d2653SJohn Baldwin 	PROC_UNLOCK(p);
570df8bae1dSRodney W. Grimes }
571df8bae1dSRodney W. Grimes 
572df8bae1dSRodney W. Grimes /*
573df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
574df8bae1dSRodney W. Grimes  */
575df8bae1dSRodney W. Grimes void
576a30ec4b9SDavid Xu execsigs(struct proc *p)
577df8bae1dSRodney W. Grimes {
578a30ec4b9SDavid Xu 	struct sigacts *ps;
579a30ec4b9SDavid Xu 	int sig;
580a30ec4b9SDavid Xu 	struct thread *td;
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes 	/*
583df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5844093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
585df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
586df8bae1dSRodney W. Grimes 	 */
5879b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
588a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
589628d2653SJohn Baldwin 	ps = p->p_sigacts;
59090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
59190af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
59290af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
59390af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
5942c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5952c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
59690af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
5971d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
5984093529dSJeff Roberson 			/*
5994093529dSJeff Roberson 			 * There is only one thread at this point.
6004093529dSJeff Roberson 			 */
601a30ec4b9SDavid Xu 			SIGDELSET(td->td_siglist, sig);
602df8bae1dSRodney W. Grimes 		}
6032c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
604df8bae1dSRodney W. Grimes 	}
605df8bae1dSRodney W. Grimes 	/*
606df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
607df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
608df8bae1dSRodney W. Grimes 	 */
609a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
610a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
611a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
612a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
61380e907a1SPeter Wemm 	/*
61480e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
61580e907a1SPeter Wemm 	 */
61690af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
617c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
618c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
61990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
620df8bae1dSRodney W. Grimes }
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes /*
623e77daab1SJohn Baldwin  * kern_sigprocmask()
6247c8fdcbdSMatthew Dillon  *
625628d2653SJohn Baldwin  *	Manipulate signal mask.
626df8bae1dSRodney W. Grimes  */
627e77daab1SJohn Baldwin int
628e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6294093529dSJeff Roberson 	struct thread *td;
6302c42a146SMarcel Moolenaar 	int how;
6312c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
632645682fdSLuoqi Chen 	int old;
6332c42a146SMarcel Moolenaar {
6342c42a146SMarcel Moolenaar 	int error;
6352c42a146SMarcel Moolenaar 
6364093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6372c42a146SMarcel Moolenaar 	if (oset != NULL)
6384093529dSJeff Roberson 		*oset = td->td_sigmask;
6392c42a146SMarcel Moolenaar 
6402c42a146SMarcel Moolenaar 	error = 0;
6412c42a146SMarcel Moolenaar 	if (set != NULL) {
6422c42a146SMarcel Moolenaar 		switch (how) {
6432c42a146SMarcel Moolenaar 		case SIG_BLOCK:
644645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6454093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6462c42a146SMarcel Moolenaar 			break;
6472c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6484093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6494093529dSJeff Roberson 			signotify(td);
6502c42a146SMarcel Moolenaar 			break;
6512c42a146SMarcel Moolenaar 		case SIG_SETMASK:
652645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
653645682fdSLuoqi Chen 			if (old)
6544093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
655645682fdSLuoqi Chen 			else
6564093529dSJeff Roberson 				td->td_sigmask = *set;
6574093529dSJeff Roberson 			signotify(td);
6582c42a146SMarcel Moolenaar 			break;
6592c42a146SMarcel Moolenaar 		default:
6602c42a146SMarcel Moolenaar 			error = EINVAL;
6612c42a146SMarcel Moolenaar 			break;
6622c42a146SMarcel Moolenaar 		}
6632c42a146SMarcel Moolenaar 	}
6644093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6652c42a146SMarcel Moolenaar 	return (error);
6662c42a146SMarcel Moolenaar }
6672c42a146SMarcel Moolenaar 
6687c8fdcbdSMatthew Dillon /*
669e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6707c8fdcbdSMatthew Dillon  */
6717c8fdcbdSMatthew Dillon 
672d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
673df8bae1dSRodney W. Grimes struct sigprocmask_args {
674df8bae1dSRodney W. Grimes 	int	how;
6752c42a146SMarcel Moolenaar 	const sigset_t *set;
6762c42a146SMarcel Moolenaar 	sigset_t *oset;
677df8bae1dSRodney W. Grimes };
678d2d3e875SBruce Evans #endif
67926f9a767SRodney W. Grimes int
680b40ce416SJulian Elischer sigprocmask(td, uap)
681b40ce416SJulian Elischer 	register struct thread *td;
682df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
683df8bae1dSRodney W. Grimes {
6842c42a146SMarcel Moolenaar 	sigset_t set, oset;
6852c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6862c42a146SMarcel Moolenaar 	int error;
687df8bae1dSRodney W. Grimes 
6886f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6896f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6902c42a146SMarcel Moolenaar 	if (setp) {
6916f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6922c42a146SMarcel Moolenaar 		if (error)
6932c42a146SMarcel Moolenaar 			return (error);
694df8bae1dSRodney W. Grimes 	}
695e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
6962c42a146SMarcel Moolenaar 	if (osetp && !error) {
6976f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
6982c42a146SMarcel Moolenaar 	}
6992c42a146SMarcel Moolenaar 	return (error);
7002c42a146SMarcel Moolenaar }
7012c42a146SMarcel Moolenaar 
70231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7037c8fdcbdSMatthew Dillon /*
7047c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
7057c8fdcbdSMatthew Dillon  */
7062c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7072c42a146SMarcel Moolenaar struct osigprocmask_args {
7082c42a146SMarcel Moolenaar 	int	how;
7092c42a146SMarcel Moolenaar 	osigset_t mask;
7102c42a146SMarcel Moolenaar };
7112c42a146SMarcel Moolenaar #endif
7122c42a146SMarcel Moolenaar int
713b40ce416SJulian Elischer osigprocmask(td, uap)
714b40ce416SJulian Elischer 	register struct thread *td;
7152c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7162c42a146SMarcel Moolenaar {
7172c42a146SMarcel Moolenaar 	sigset_t set, oset;
7182c42a146SMarcel Moolenaar 	int error;
7192c42a146SMarcel Moolenaar 
7202c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
721e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
722b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
723df8bae1dSRodney W. Grimes 	return (error);
724df8bae1dSRodney W. Grimes }
72531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
726df8bae1dSRodney W. Grimes 
727d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
728df8bae1dSRodney W. Grimes struct sigpending_args {
7292c42a146SMarcel Moolenaar 	sigset_t	*set;
730df8bae1dSRodney W. Grimes };
731d2d3e875SBruce Evans #endif
732fb99ab88SMatthew Dillon /*
733fb99ab88SMatthew Dillon  * MPSAFE
734fb99ab88SMatthew Dillon  */
73526f9a767SRodney W. Grimes int
736a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
737a447cd8bSJeff Roberson {
738a447cd8bSJeff Roberson 	siginfo_t info;
739a447cd8bSJeff Roberson 	sigset_t set;
740a447cd8bSJeff Roberson 	int error;
741a447cd8bSJeff Roberson 
742a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
74336939a0aSDavid Xu 	if (error) {
74436939a0aSDavid Xu 		td->td_retval[0] = error;
74536939a0aSDavid Xu 		return (0);
74636939a0aSDavid Xu 	}
747a447cd8bSJeff Roberson 
748a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
74936939a0aSDavid Xu 	if (error) {
75036939a0aSDavid Xu 		if (error == ERESTART)
751a447cd8bSJeff Roberson 			return (error);
75236939a0aSDavid Xu 		td->td_retval[0] = error;
75336939a0aSDavid Xu 		return (0);
75436939a0aSDavid Xu 	}
755a447cd8bSJeff Roberson 
756a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
757a447cd8bSJeff Roberson 	/* Repost if we got an error. */
75818440c7fSJohn Baldwin 	if (error && info.si_signo) {
75918440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
760c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
76118440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
76218440c7fSJohn Baldwin 	}
76336939a0aSDavid Xu 	td->td_retval[0] = error;
76436939a0aSDavid Xu 	return (0);
765a447cd8bSJeff Roberson }
766a447cd8bSJeff Roberson /*
767a447cd8bSJeff Roberson  * MPSAFE
768a447cd8bSJeff Roberson  */
769a447cd8bSJeff Roberson int
770a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
771a447cd8bSJeff Roberson {
772a447cd8bSJeff Roberson 	struct timespec ts;
773a447cd8bSJeff Roberson 	struct timespec *timeout;
774a447cd8bSJeff Roberson 	sigset_t set;
775a447cd8bSJeff Roberson 	siginfo_t info;
776a447cd8bSJeff Roberson 	int error;
777a447cd8bSJeff Roberson 
778a447cd8bSJeff Roberson 	if (uap->timeout) {
779a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
780a447cd8bSJeff Roberson 		if (error)
781a447cd8bSJeff Roberson 			return (error);
782a447cd8bSJeff Roberson 
783a447cd8bSJeff Roberson 		timeout = &ts;
784a447cd8bSJeff Roberson 	} else
785a447cd8bSJeff Roberson 		timeout = NULL;
786a447cd8bSJeff Roberson 
787a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
788a447cd8bSJeff Roberson 	if (error)
789a447cd8bSJeff Roberson 		return (error);
790a447cd8bSJeff Roberson 
791a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
792a447cd8bSJeff Roberson 	if (error)
793a447cd8bSJeff Roberson 		return (error);
7949dde3bc9SDavid Xu 
795418228dfSDavid Xu 	if (uap->info)
796a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
797a447cd8bSJeff Roberson 	/* Repost if we got an error. */
79818440c7fSJohn Baldwin 	if (error && info.si_signo) {
79918440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
800c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
80118440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
802418228dfSDavid Xu 	} else {
803418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
80418440c7fSJohn Baldwin 	}
805a447cd8bSJeff Roberson 	return (error);
806a447cd8bSJeff Roberson }
807a447cd8bSJeff Roberson 
808a447cd8bSJeff Roberson /*
809a447cd8bSJeff Roberson  * MPSAFE
810a447cd8bSJeff Roberson  */
811a447cd8bSJeff Roberson int
812a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
813a447cd8bSJeff Roberson {
814a447cd8bSJeff Roberson 	siginfo_t info;
815a447cd8bSJeff Roberson 	sigset_t set;
816a447cd8bSJeff Roberson 	int error;
817a447cd8bSJeff Roberson 
818a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
819a447cd8bSJeff Roberson 	if (error)
820a447cd8bSJeff Roberson 		return (error);
821a447cd8bSJeff Roberson 
822a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
823a447cd8bSJeff Roberson 	if (error)
824a447cd8bSJeff Roberson 		return (error);
825a447cd8bSJeff Roberson 
826418228dfSDavid Xu 	if (uap->info)
827a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
828a447cd8bSJeff Roberson 	/* Repost if we got an error. */
82918440c7fSJohn Baldwin 	if (error && info.si_signo) {
83018440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
831c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
83218440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
833418228dfSDavid Xu 	} else {
834418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
83518440c7fSJohn Baldwin 	}
836a447cd8bSJeff Roberson 	return (error);
837a447cd8bSJeff Roberson }
838a447cd8bSJeff Roberson 
839a447cd8bSJeff Roberson static int
8403074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
841a447cd8bSJeff Roberson     struct timespec *timeout)
842a447cd8bSJeff Roberson {
8433074d1b4SDavid Xu 	struct sigacts *ps;
8443074d1b4SDavid Xu 	sigset_t savedmask, sigset;
845a447cd8bSJeff Roberson 	struct proc *p;
846a447cd8bSJeff Roberson 	int error;
847a447cd8bSJeff Roberson 	int sig;
848a447cd8bSJeff Roberson 	int hz;
8493074d1b4SDavid Xu 	int i;
850a447cd8bSJeff Roberson 
851a447cd8bSJeff Roberson 	p = td->td_proc;
852a447cd8bSJeff Roberson 	error = 0;
853a447cd8bSJeff Roberson 	sig = 0;
8543074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
855a447cd8bSJeff Roberson 
856a447cd8bSJeff Roberson 	PROC_LOCK(p);
857a447cd8bSJeff Roberson 	ps = p->p_sigacts;
8583074d1b4SDavid Xu 	savedmask = td->td_sigmask;
8593074d1b4SDavid Xu 
8603074d1b4SDavid Xu again:
8613074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
8623074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
8633074d1b4SDavid Xu 			continue;
8643074d1b4SDavid Xu 		if (SIGISMEMBER(td->td_siglist, i)) {
865418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
866418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8673074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
86890af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
869a447cd8bSJeff Roberson 			sig = cursig(td);
8703074d1b4SDavid Xu 			i = 0;
8713074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8723074d1b4SDavid Xu 		} else if (SIGISMEMBER(p->p_siglist, i)) {
8733074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
8743074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
8753074d1b4SDavid Xu 				wakeup(&p->p_siglist);
8763074d1b4SDavid Xu 			}
8773074d1b4SDavid Xu 			SIGDELSET(p->p_siglist, i);
8783074d1b4SDavid Xu 			SIGADDSET(td->td_siglist, i);
8793074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
8803074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8813074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
8823074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
8833074d1b4SDavid Xu 			sig = cursig(td);
8843074d1b4SDavid Xu 			i = 0;
8853074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8863074d1b4SDavid Xu 		}
8873074d1b4SDavid Xu 		if (sig) {
8883074d1b4SDavid Xu 			td->td_sigmask = savedmask;
8893074d1b4SDavid Xu 			signotify(td);
890a447cd8bSJeff Roberson 			goto out;
8913074d1b4SDavid Xu 		}
8923074d1b4SDavid Xu 	}
8933074d1b4SDavid Xu 	if (error)
8943074d1b4SDavid Xu 		goto out;
8953074d1b4SDavid Xu 
8963074d1b4SDavid Xu 	td->td_sigmask = savedmask;
8973074d1b4SDavid Xu 	signotify(td);
8983074d1b4SDavid Xu 	sigset = td->td_siglist;
8993074d1b4SDavid Xu 	SIGSETOR(sigset, p->p_siglist);
9003074d1b4SDavid Xu 	SIGSETAND(sigset, waitset);
9013074d1b4SDavid Xu 	if (!SIGISEMPTY(sigset))
9023074d1b4SDavid Xu 		goto again;
903a447cd8bSJeff Roberson 
904a447cd8bSJeff Roberson 	/*
905a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
906a447cd8bSJeff Roberson 	 * signals.
907a447cd8bSJeff Roberson 	 */
908a447cd8bSJeff Roberson 	if (timeout) {
909a447cd8bSJeff Roberson 		struct timeval tv;
910a447cd8bSJeff Roberson 
911a6ca4808SMike Makonnen 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
912a447cd8bSJeff Roberson 			error = EINVAL;
913a447cd8bSJeff Roberson 			goto out;
914a447cd8bSJeff Roberson 		}
9153074d1b4SDavid Xu 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
9163074d1b4SDavid Xu 			error = EAGAIN;
9173074d1b4SDavid Xu 			goto out;
9183074d1b4SDavid Xu 		}
919a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
920a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
921a447cd8bSJeff Roberson 	} else
922a447cd8bSJeff Roberson 		hz = 0;
923a447cd8bSJeff Roberson 
9243074d1b4SDavid Xu 	td->td_waitset = &waitset;
92586b5e563SDag-Erling Smørgrav 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
9263074d1b4SDavid Xu 	td->td_waitset = NULL;
9273074d1b4SDavid Xu 	if (error == 0) /* surplus wakeup ? */
9283074d1b4SDavid Xu 		error = EINTR;
9293074d1b4SDavid Xu 	goto again;
9309dde3bc9SDavid Xu 
931a447cd8bSJeff Roberson out:
932a447cd8bSJeff Roberson 	if (sig) {
933a447cd8bSJeff Roberson 		sig_t action;
934a447cd8bSJeff Roberson 
9353074d1b4SDavid Xu 		error = 0;
9363074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
937a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
93890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
939a447cd8bSJeff Roberson #ifdef KTRACE
940a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
9413074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
942a447cd8bSJeff Roberson #endif
943a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
944a447cd8bSJeff Roberson 
945a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
946a447cd8bSJeff Roberson 		info->si_signo = sig;
947a447cd8bSJeff Roberson 		info->si_code = 0;
9483074d1b4SDavid Xu 	}
949a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
950a447cd8bSJeff Roberson 	return (error);
951a447cd8bSJeff Roberson }
952a447cd8bSJeff Roberson 
953a447cd8bSJeff Roberson /*
954a447cd8bSJeff Roberson  * MPSAFE
955a447cd8bSJeff Roberson  */
956a447cd8bSJeff Roberson int
957b40ce416SJulian Elischer sigpending(td, uap)
958b40ce416SJulian Elischer 	struct thread *td;
959df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
960df8bae1dSRodney W. Grimes {
961b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
962628d2653SJohn Baldwin 	sigset_t siglist;
963df8bae1dSRodney W. Grimes 
964628d2653SJohn Baldwin 	PROC_LOCK(p);
9651d9c5696SJuli Mallett 	siglist = p->p_siglist;
9664093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
967628d2653SJohn Baldwin 	PROC_UNLOCK(p);
96848e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9692c42a146SMarcel Moolenaar }
9702c42a146SMarcel Moolenaar 
97131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9722c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9732c42a146SMarcel Moolenaar struct osigpending_args {
9742c42a146SMarcel Moolenaar 	int	dummy;
9752c42a146SMarcel Moolenaar };
9762c42a146SMarcel Moolenaar #endif
977fb99ab88SMatthew Dillon /*
978fb99ab88SMatthew Dillon  * MPSAFE
979fb99ab88SMatthew Dillon  */
9802c42a146SMarcel Moolenaar int
981b40ce416SJulian Elischer osigpending(td, uap)
982b40ce416SJulian Elischer 	struct thread *td;
9832c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9842c42a146SMarcel Moolenaar {
985b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9864093529dSJeff Roberson 	sigset_t siglist;
987b40ce416SJulian Elischer 
988628d2653SJohn Baldwin 	PROC_LOCK(p);
9894093529dSJeff Roberson 	siglist = p->p_siglist;
9904093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
991628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9929d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
993df8bae1dSRodney W. Grimes 	return (0);
994df8bae1dSRodney W. Grimes }
99531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
996df8bae1dSRodney W. Grimes 
9971930e303SPoul-Henning Kamp #if defined(COMPAT_43)
998df8bae1dSRodney W. Grimes /*
999df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1000df8bae1dSRodney W. Grimes  */
1001d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1002df8bae1dSRodney W. Grimes struct osigvec_args {
1003df8bae1dSRodney W. Grimes 	int	signum;
1004df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1005df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1006df8bae1dSRodney W. Grimes };
1007d2d3e875SBruce Evans #endif
1008fb99ab88SMatthew Dillon /*
1009fb99ab88SMatthew Dillon  * MPSAFE
1010fb99ab88SMatthew Dillon  */
1011df8bae1dSRodney W. Grimes /* ARGSUSED */
101226f9a767SRodney W. Grimes int
1013b40ce416SJulian Elischer osigvec(td, uap)
1014b40ce416SJulian Elischer 	struct thread *td;
1015df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1016df8bae1dSRodney W. Grimes {
1017df8bae1dSRodney W. Grimes 	struct sigvec vec;
10182c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
10192c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
10202c42a146SMarcel Moolenaar 	int error;
1021df8bae1dSRodney W. Grimes 
10226f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10236f841fb7SMarcel Moolenaar 		return (EINVAL);
10246f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
10256f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
10262c42a146SMarcel Moolenaar 	if (nsap) {
10276f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
10282c42a146SMarcel Moolenaar 		if (error)
1029df8bae1dSRodney W. Grimes 			return (error);
10302c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
10312c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
10322c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
10332c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1034df8bae1dSRodney W. Grimes 	}
10355edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10362c42a146SMarcel Moolenaar 	if (osap && !error) {
10372c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
10382c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
10392c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
10402c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
10412c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
10426f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10432c42a146SMarcel Moolenaar 	}
10442c42a146SMarcel Moolenaar 	return (error);
1045df8bae1dSRodney W. Grimes }
1046df8bae1dSRodney W. Grimes 
1047d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1048df8bae1dSRodney W. Grimes struct osigblock_args {
1049df8bae1dSRodney W. Grimes 	int	mask;
1050df8bae1dSRodney W. Grimes };
1051d2d3e875SBruce Evans #endif
1052fb99ab88SMatthew Dillon /*
1053fb99ab88SMatthew Dillon  * MPSAFE
1054fb99ab88SMatthew Dillon  */
105526f9a767SRodney W. Grimes int
1056b40ce416SJulian Elischer osigblock(td, uap)
1057b40ce416SJulian Elischer 	register struct thread *td;
1058df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1059df8bae1dSRodney W. Grimes {
1060b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10612c42a146SMarcel Moolenaar 	sigset_t set;
1062df8bae1dSRodney W. Grimes 
10632c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10642c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1065628d2653SJohn Baldwin 	PROC_LOCK(p);
10664093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10674093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1068628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1069df8bae1dSRodney W. Grimes 	return (0);
1070df8bae1dSRodney W. Grimes }
1071df8bae1dSRodney W. Grimes 
1072d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1073df8bae1dSRodney W. Grimes struct osigsetmask_args {
1074df8bae1dSRodney W. Grimes 	int	mask;
1075df8bae1dSRodney W. Grimes };
1076d2d3e875SBruce Evans #endif
1077fb99ab88SMatthew Dillon /*
1078fb99ab88SMatthew Dillon  * MPSAFE
1079fb99ab88SMatthew Dillon  */
108026f9a767SRodney W. Grimes int
1081b40ce416SJulian Elischer osigsetmask(td, uap)
1082b40ce416SJulian Elischer 	struct thread *td;
1083df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1084df8bae1dSRodney W. Grimes {
1085b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10862c42a146SMarcel Moolenaar 	sigset_t set;
1087df8bae1dSRodney W. Grimes 
10882c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10892c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1090628d2653SJohn Baldwin 	PROC_LOCK(p);
10914093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10924093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10934093529dSJeff Roberson 	signotify(td);
1094628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1095df8bae1dSRodney W. Grimes 	return (0);
1096df8bae1dSRodney W. Grimes }
10971930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes /*
1100df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
110197563428SAlexander Kabaev  * in the meantime.
1102b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1103b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1104b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1105df8bae1dSRodney W. Grimes  */
1106d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1107df8bae1dSRodney W. Grimes struct sigsuspend_args {
11082c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1109df8bae1dSRodney W. Grimes };
1110d2d3e875SBruce Evans #endif
1111fb99ab88SMatthew Dillon /*
1112fb99ab88SMatthew Dillon  * MPSAFE
1113fb99ab88SMatthew Dillon  */
1114df8bae1dSRodney W. Grimes /* ARGSUSED */
111526f9a767SRodney W. Grimes int
1116b40ce416SJulian Elischer sigsuspend(td, uap)
1117b40ce416SJulian Elischer 	struct thread *td;
1118df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1119df8bae1dSRodney W. Grimes {
11202c42a146SMarcel Moolenaar 	sigset_t mask;
11212c42a146SMarcel Moolenaar 	int error;
11222c42a146SMarcel Moolenaar 
11236f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11242c42a146SMarcel Moolenaar 	if (error)
11252c42a146SMarcel Moolenaar 		return (error);
11268f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11278f19eb88SIan Dowse }
11288f19eb88SIan Dowse 
11298f19eb88SIan Dowse int
11308f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11318f19eb88SIan Dowse {
11328f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes 	/*
1135645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1136df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1137df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1138df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1139df8bae1dSRodney W. Grimes 	 * to indicate this.
1140df8bae1dSRodney W. Grimes 	 */
1141628d2653SJohn Baldwin 	PROC_LOCK(p);
11424093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11435e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1144645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11454093529dSJeff Roberson 	td->td_sigmask = mask;
11464093529dSJeff Roberson 	signotify(td);
114786b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11482c42a146SMarcel Moolenaar 		/* void */;
1149628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11502c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11512c42a146SMarcel Moolenaar 	return (EINTR);
11522c42a146SMarcel Moolenaar }
11532c42a146SMarcel Moolenaar 
115431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
115597563428SAlexander Kabaev /*
115697563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
115797563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
115897563428SAlexander Kabaev  */
11592c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11602c42a146SMarcel Moolenaar struct osigsuspend_args {
11612c42a146SMarcel Moolenaar 	osigset_t mask;
11622c42a146SMarcel Moolenaar };
11632c42a146SMarcel Moolenaar #endif
1164fb99ab88SMatthew Dillon /*
1165fb99ab88SMatthew Dillon  * MPSAFE
1166fb99ab88SMatthew Dillon  */
11672c42a146SMarcel Moolenaar /* ARGSUSED */
11682c42a146SMarcel Moolenaar int
1169b40ce416SJulian Elischer osigsuspend(td, uap)
1170b40ce416SJulian Elischer 	struct thread *td;
11712c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11722c42a146SMarcel Moolenaar {
1173b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1174645682fdSLuoqi Chen 	sigset_t mask;
11752c42a146SMarcel Moolenaar 
1176628d2653SJohn Baldwin 	PROC_LOCK(p);
11774093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11785e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1179645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1180645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11814093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11824093529dSJeff Roberson 	signotify(td);
118386b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1184df8bae1dSRodney W. Grimes 		/* void */;
1185628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1186df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1187df8bae1dSRodney W. Grimes 	return (EINTR);
1188df8bae1dSRodney W. Grimes }
118931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1190df8bae1dSRodney W. Grimes 
11911930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1192d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1193df8bae1dSRodney W. Grimes struct osigstack_args {
1194df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1195df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1196df8bae1dSRodney W. Grimes };
1197d2d3e875SBruce Evans #endif
1198fb99ab88SMatthew Dillon /*
1199fb99ab88SMatthew Dillon  * MPSAFE
1200fb99ab88SMatthew Dillon  */
1201df8bae1dSRodney W. Grimes /* ARGSUSED */
120226f9a767SRodney W. Grimes int
1203b40ce416SJulian Elischer osigstack(td, uap)
1204b40ce416SJulian Elischer 	struct thread *td;
1205df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1206df8bae1dSRodney W. Grimes {
12075afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1208fb99ab88SMatthew Dillon 	int error = 0;
1209fb99ab88SMatthew Dillon 
1210d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12115afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12125afe0c99SJohn Baldwin 		if (error)
12135afe0c99SJohn Baldwin 			return (error);
1214df8bae1dSRodney W. Grimes 	}
1215a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12165afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12175afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1218a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1219a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1220a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1221a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12225afe0c99SJohn Baldwin 	}
12235afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12245afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12255afe0c99SJohn Baldwin 
1226fb99ab88SMatthew Dillon 	return (error);
1227df8bae1dSRodney W. Grimes }
12281930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1229df8bae1dSRodney W. Grimes 
1230d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1231df8bae1dSRodney W. Grimes struct sigaltstack_args {
12322c42a146SMarcel Moolenaar 	stack_t	*ss;
12332c42a146SMarcel Moolenaar 	stack_t	*oss;
1234df8bae1dSRodney W. Grimes };
1235d2d3e875SBruce Evans #endif
1236fb99ab88SMatthew Dillon /*
1237fb99ab88SMatthew Dillon  * MPSAFE
1238fb99ab88SMatthew Dillon  */
1239df8bae1dSRodney W. Grimes /* ARGSUSED */
124026f9a767SRodney W. Grimes int
1241b40ce416SJulian Elischer sigaltstack(td, uap)
1242b40ce416SJulian Elischer 	struct thread *td;
1243df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1244df8bae1dSRodney W. Grimes {
12458f19eb88SIan Dowse 	stack_t ss, oss;
12468f19eb88SIan Dowse 	int error;
12478f19eb88SIan Dowse 
12488f19eb88SIan Dowse 	if (uap->ss != NULL) {
12498f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12508f19eb88SIan Dowse 		if (error)
12518f19eb88SIan Dowse 			return (error);
12528f19eb88SIan Dowse 	}
12538f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12548f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12558f19eb88SIan Dowse 	if (error)
12568f19eb88SIan Dowse 		return (error);
12578f19eb88SIan Dowse 	if (uap->oss != NULL)
12588f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12598f19eb88SIan Dowse 	return (error);
12608f19eb88SIan Dowse }
12618f19eb88SIan Dowse 
12628f19eb88SIan Dowse int
12638f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12648f19eb88SIan Dowse {
1265b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1266fb99ab88SMatthew Dillon 	int oonstack;
1267fb99ab88SMatthew Dillon 
1268b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1269d034d459SMarcel Moolenaar 
12708f19eb88SIan Dowse 	if (oss != NULL) {
1271a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1272a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1273d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1274df8bae1dSRodney W. Grimes 	}
1275d034d459SMarcel Moolenaar 
12768f19eb88SIan Dowse 	if (ss != NULL) {
1277a30ec4b9SDavid Xu 		if (oonstack)
1278cf60731bSJohn Baldwin 			return (EPERM);
1279a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1280cf60731bSJohn Baldwin 			return (EINVAL);
12818f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12828f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1283cf60731bSJohn Baldwin 				return (ENOMEM);
1284fb99ab88SMatthew Dillon 			}
1285a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1286a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1287628d2653SJohn Baldwin 		} else {
1288a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1289628d2653SJohn Baldwin 		}
1290d034d459SMarcel Moolenaar 	}
1291cf60731bSJohn Baldwin 	return (0);
1292df8bae1dSRodney W. Grimes }
1293df8bae1dSRodney W. Grimes 
1294d93f860cSPoul-Henning Kamp /*
1295d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1296d93f860cSPoul-Henning Kamp  * cp is calling process.
1297d93f860cSPoul-Henning Kamp  */
129837c84183SPoul-Henning Kamp static int
12999c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
13009c1ab3e0SJohn Baldwin 	register struct thread *td;
13012c42a146SMarcel Moolenaar 	int sig, pgid, all;
1302d93f860cSPoul-Henning Kamp {
1303d93f860cSPoul-Henning Kamp 	register struct proc *p;
1304d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1305d93f860cSPoul-Henning Kamp 	int nfound = 0;
1306d93f860cSPoul-Henning Kamp 
1307553629ebSJake Burkholder 	if (all) {
1308d93f860cSPoul-Henning Kamp 		/*
1309d93f860cSPoul-Henning Kamp 		 * broadcast
1310d93f860cSPoul-Henning Kamp 		 */
13111005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13122e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1313628d2653SJohn Baldwin 			PROC_LOCK(p);
13149c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13159c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1316628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1317628d2653SJohn Baldwin 				continue;
1318628d2653SJohn Baldwin 			}
1319f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1320d93f860cSPoul-Henning Kamp 				nfound++;
132133a9ed9dSJohn Baldwin 				if (sig)
13222c42a146SMarcel Moolenaar 					psignal(p, sig);
1323628d2653SJohn Baldwin 			}
132433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1325d93f860cSPoul-Henning Kamp 		}
13261005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1327553629ebSJake Burkholder 	} else {
1328ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1329f591779bSSeigo Tanimura 		if (pgid == 0) {
1330d93f860cSPoul-Henning Kamp 			/*
1331d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1332d93f860cSPoul-Henning Kamp 			 */
13339c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1334f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1335f591779bSSeigo Tanimura 		} else {
1336d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1337f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1338ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1339d93f860cSPoul-Henning Kamp 				return (ESRCH);
1340d93f860cSPoul-Henning Kamp 			}
1341f591779bSSeigo Tanimura 		}
1342ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13432e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1344628d2653SJohn Baldwin 			PROC_LOCK(p);
1345628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1346628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1347628d2653SJohn Baldwin 				continue;
1348628d2653SJohn Baldwin 			}
1349e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
135033a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1351628d2653SJohn Baldwin 				continue;
1352628d2653SJohn Baldwin 			}
1353f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1354d93f860cSPoul-Henning Kamp 				nfound++;
135533a9ed9dSJohn Baldwin 				if (sig)
13562c42a146SMarcel Moolenaar 					psignal(p, sig);
1357628d2653SJohn Baldwin 			}
135833a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1359d93f860cSPoul-Henning Kamp 		}
1360f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1361d93f860cSPoul-Henning Kamp 	}
1362d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1363d93f860cSPoul-Henning Kamp }
1364d93f860cSPoul-Henning Kamp 
1365d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1366df8bae1dSRodney W. Grimes struct kill_args {
1367df8bae1dSRodney W. Grimes 	int	pid;
1368df8bae1dSRodney W. Grimes 	int	signum;
1369df8bae1dSRodney W. Grimes };
1370d2d3e875SBruce Evans #endif
1371fb99ab88SMatthew Dillon /*
1372fb99ab88SMatthew Dillon  * MPSAFE
1373fb99ab88SMatthew Dillon  */
1374df8bae1dSRodney W. Grimes /* ARGSUSED */
137526f9a767SRodney W. Grimes int
1376b40ce416SJulian Elischer kill(td, uap)
1377b40ce416SJulian Elischer 	register struct thread *td;
1378df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1379df8bae1dSRodney W. Grimes {
1380df8bae1dSRodney W. Grimes 	register struct proc *p;
138190af4afaSJohn Baldwin 	int error;
1382df8bae1dSRodney W. Grimes 
13836c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1384df8bae1dSRodney W. Grimes 		return (EINVAL);
1385fb99ab88SMatthew Dillon 
1386df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1387df8bae1dSRodney W. Grimes 		/* kill single process */
138890af4afaSJohn Baldwin 		if ((p = pfind(uap->pid)) == NULL)
138990af4afaSJohn Baldwin 			return (ESRCH);
139090af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
139190af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1392df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1393628d2653SJohn Baldwin 		PROC_UNLOCK(p);
139490af4afaSJohn Baldwin 		return (error);
1395df8bae1dSRodney W. Grimes 	}
1396df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1397df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
139890af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1399df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
140090af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1401df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
140290af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1403df8bae1dSRodney W. Grimes 	}
140490af4afaSJohn Baldwin 	/* NOTREACHED */
1405df8bae1dSRodney W. Grimes }
1406df8bae1dSRodney W. Grimes 
14071930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1408d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1409df8bae1dSRodney W. Grimes struct okillpg_args {
1410df8bae1dSRodney W. Grimes 	int	pgid;
1411df8bae1dSRodney W. Grimes 	int	signum;
1412df8bae1dSRodney W. Grimes };
1413d2d3e875SBruce Evans #endif
1414fb99ab88SMatthew Dillon /*
1415fb99ab88SMatthew Dillon  * MPSAFE
1416fb99ab88SMatthew Dillon  */
1417df8bae1dSRodney W. Grimes /* ARGSUSED */
141826f9a767SRodney W. Grimes int
1419b40ce416SJulian Elischer okillpg(td, uap)
1420b40ce416SJulian Elischer 	struct thread *td;
1421df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1422df8bae1dSRodney W. Grimes {
1423df8bae1dSRodney W. Grimes 
14246c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1425df8bae1dSRodney W. Grimes 		return (EINVAL);
142690af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1427df8bae1dSRodney W. Grimes }
14281930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1429df8bae1dSRodney W. Grimes 
1430df8bae1dSRodney W. Grimes /*
1431df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1432df8bae1dSRodney W. Grimes  */
1433df8bae1dSRodney W. Grimes void
14342c42a146SMarcel Moolenaar gsignal(pgid, sig)
14352c42a146SMarcel Moolenaar 	int pgid, sig;
1436df8bae1dSRodney W. Grimes {
1437df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1438df8bae1dSRodney W. Grimes 
1439f591779bSSeigo Tanimura 	if (pgid != 0) {
1440ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1441f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1442ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1443f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14442c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1445f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1446f591779bSSeigo Tanimura 		}
1447f591779bSSeigo Tanimura 	}
1448df8bae1dSRodney W. Grimes }
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes /*
1451df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1452df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1453df8bae1dSRodney W. Grimes  */
1454df8bae1dSRodney W. Grimes void
14552c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1456df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14572c42a146SMarcel Moolenaar 	int sig, checkctty;
1458df8bae1dSRodney W. Grimes {
1459df8bae1dSRodney W. Grimes 	register struct proc *p;
1460df8bae1dSRodney W. Grimes 
1461628d2653SJohn Baldwin 	if (pgrp) {
1462f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1463628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1464628d2653SJohn Baldwin 			PROC_LOCK(p);
1465df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14662c42a146SMarcel Moolenaar 				psignal(p, sig);
1467628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1468628d2653SJohn Baldwin 		}
1469628d2653SJohn Baldwin 	}
1470df8bae1dSRodney W. Grimes }
1471df8bae1dSRodney W. Grimes 
1472df8bae1dSRodney W. Grimes /*
14731bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1474df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1475df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1476356861dbSMatthew Dillon  *
1477356861dbSMatthew Dillon  * MPSAFE
1478df8bae1dSRodney W. Grimes  */
1479df8bae1dSRodney W. Grimes void
14801bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1481df8bae1dSRodney W. Grimes {
14821bf4700bSJeff Roberson 	struct sigacts *ps;
14831bf4700bSJeff Roberson 	struct proc *p;
14849dde3bc9SDavid Xu 	siginfo_t siginfo;
14859dde3bc9SDavid Xu 	int error;
14861bf4700bSJeff Roberson 
14871bf4700bSJeff Roberson 	p = td->td_proc;
1488aa0aa7a1STim J. Robbins 	if (td->td_pflags & TDP_SA) {
14897eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
14909dde3bc9SDavid Xu 			thread_user_enter(p, td);
1491628d2653SJohn Baldwin 		PROC_LOCK(p);
14929dde3bc9SDavid Xu 		if (td->td_mailbox) {
14939dde3bc9SDavid Xu 			SIGDELSET(td->td_sigmask, sig);
14949dde3bc9SDavid Xu 			mtx_lock_spin(&sched_lock);
14959dde3bc9SDavid Xu 			/*
14969dde3bc9SDavid Xu 			 * Force scheduling an upcall, so UTS has chance to
14979dde3bc9SDavid Xu 			 * process the signal before thread runs again in
14989dde3bc9SDavid Xu 			 * userland.
14999dde3bc9SDavid Xu 			 */
15009dde3bc9SDavid Xu 			if (td->td_upcall)
15019dde3bc9SDavid Xu 				td->td_upcall->ku_flags |= KUF_DOUPCALL;
15029dde3bc9SDavid Xu 			mtx_unlock_spin(&sched_lock);
1503432b45deSDavid Xu 		} else {
1504432b45deSDavid Xu 			/* UTS caused a sync signal */
15051fc434dcSDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
15061fc434dcSDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
15071fc434dcSDavid Xu 			sigexit(td, sig);
15089dde3bc9SDavid Xu 		}
15099dde3bc9SDavid Xu 	} else {
15109dde3bc9SDavid Xu 		PROC_LOCK(p);
15119dde3bc9SDavid Xu 	}
1512ef3dab76STim J. Robbins 	ps = p->p_sigacts;
151390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
151490af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15154093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1516df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1517df8bae1dSRodney W. Grimes #ifdef KTRACE
1518374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1519374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15204093529dSJeff Roberson 			    &td->td_sigmask, code);
1521df8bae1dSRodney W. Grimes #endif
1522aa0aa7a1STim J. Robbins 		if (!(td->td_pflags & TDP_SA))
15239dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15249dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15254093529dSJeff Roberson 				&td->td_sigmask, code);
15269dde3bc9SDavid Xu 		else {
15274b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15289dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
15299dde3bc9SDavid Xu 			PROC_UNLOCK(p);
15309dde3bc9SDavid Xu 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
15319dde3bc9SDavid Xu 			    sizeof(siginfo));
15329dde3bc9SDavid Xu 			PROC_LOCK(p);
1533432b45deSDavid Xu 			/* UTS memory corrupted */
15349dde3bc9SDavid Xu 			if (error)
15359dde3bc9SDavid Xu 				sigexit(td, SIGILL);
15369dde3bc9SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
15379dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15389dde3bc9SDavid Xu 		}
15394093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15402c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15414093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15422c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1543289ccde0SPeter Wemm 			/*
15448f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1545289ccde0SPeter Wemm 			 */
154690af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15472c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15482c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
154990af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15502c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1551dedc04feSPeter Wemm 		}
155290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15536f841fb7SMarcel Moolenaar 	} else {
155490af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15556626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15562c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1557c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1558df8bae1dSRodney W. Grimes 	}
1559628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1560df8bae1dSRodney W. Grimes }
1561df8bae1dSRodney W. Grimes 
15624093529dSJeff Roberson static struct thread *
15634093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15644093529dSJeff Roberson {
15653074d1b4SDavid Xu 	struct thread *td, *signal_td;
15664093529dSJeff Roberson 
15674093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15684093529dSJeff Roberson 
15694093529dSJeff Roberson 	/*
15703074d1b4SDavid Xu 	 * First find a thread in sigwait state and signal belongs to
15713074d1b4SDavid Xu 	 * its wait set. POSIX's arguments is that speed of delivering signal
15723074d1b4SDavid Xu 	 * to sigwait thread is faster than delivering signal to user stack.
15733074d1b4SDavid Xu 	 * If we can not find sigwait thread, then find the first thread in
15743074d1b4SDavid Xu 	 * the proc that doesn't have this signal masked, an exception is
15753074d1b4SDavid Xu 	 * if current thread is sending signal to its process, and it does not
15763074d1b4SDavid Xu 	 * mask the signal, it should get the signal, this is another fast
15773074d1b4SDavid Xu 	 * way to deliver signal.
15784093529dSJeff Roberson 	 */
15793074d1b4SDavid Xu 	signal_td = NULL;
1580a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15813074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15823074d1b4SDavid Xu 		if (td->td_waitset != NULL &&
1583a9a48d68SDavid Xu 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1584a9a48d68SDavid Xu 				mtx_unlock_spin(&sched_lock);
15854093529dSJeff Roberson 				return (td);
1586a9a48d68SDavid Xu 		}
15873074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15883074d1b4SDavid Xu 			if (td == curthread)
15893074d1b4SDavid Xu 				signal_td = curthread;
15903074d1b4SDavid Xu 			else if (signal_td == NULL)
15913074d1b4SDavid Xu 				signal_td = td;
15923074d1b4SDavid Xu 		}
15933074d1b4SDavid Xu 	}
15943074d1b4SDavid Xu 	if (signal_td == NULL)
15953074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1596a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
15973074d1b4SDavid Xu 	return (signal_td);
15984093529dSJeff Roberson }
15994093529dSJeff Roberson 
1600df8bae1dSRodney W. Grimes /*
1601df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1602df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1603df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1604df8bae1dSRodney W. Grimes  *
1605df8bae1dSRodney W. Grimes  * Exceptions:
1606df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1607df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1608df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1609df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1610df8bae1dSRodney W. Grimes  *
1611df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
161290af4afaSJohn Baldwin  *
161390af4afaSJohn Baldwin  * MPSAFE
1614df8bae1dSRodney W. Grimes  */
1615df8bae1dSRodney W. Grimes void
16164093529dSJeff Roberson psignal(struct proc *p, int sig)
1617df8bae1dSRodney W. Grimes {
1618b40ce416SJulian Elischer 	struct thread *td;
16194093529dSJeff Roberson 	int prop;
16204093529dSJeff Roberson 
162141b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
162241b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
162341b3077aSJacques Vidrine 
16244093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
16254093529dSJeff Roberson 	prop = sigprop(sig);
16264093529dSJeff Roberson 
16274093529dSJeff Roberson 	/*
16284093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16294093529dSJeff Roberson 	 */
16304093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16314093529dSJeff Roberson 
1632c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16334093529dSJeff Roberson }
16344093529dSJeff Roberson 
163590af4afaSJohn Baldwin /*
163690af4afaSJohn Baldwin  * MPSAFE
163790af4afaSJohn Baldwin  */
16384093529dSJeff Roberson void
1639c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16404093529dSJeff Roberson {
16419dde3bc9SDavid Xu 	sigset_t saved;
16429dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16439dde3bc9SDavid Xu 
16449dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16459dde3bc9SDavid Xu 		saved = p->p_siglist;
1646c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16479dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16489dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
16499dde3bc9SDavid Xu 			return;
16509dde3bc9SDavid Xu 		else {
16519dde3bc9SDavid Xu 			/* pending set changed */
16529dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
16539dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
16549dde3bc9SDavid Xu 		}
16559dde3bc9SDavid Xu 	}
16569dde3bc9SDavid Xu }
16579dde3bc9SDavid Xu 
16589dde3bc9SDavid Xu static void
1659c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target)
16609dde3bc9SDavid Xu {
16614093529dSJeff Roberson 	struct proc *p;
16624093529dSJeff Roberson 	register sig_t action;
16634093529dSJeff Roberson 	sigset_t *siglist;
16644093529dSJeff Roberson 	struct thread *td0;
1665e602ba25SJulian Elischer 	register int prop;
166690af4afaSJohn Baldwin 	struct sigacts *ps;
1667df8bae1dSRodney W. Grimes 
166841b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
166941b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
16704093529dSJeff Roberson 
16714093529dSJeff Roberson 	p = td->td_proc;
167290af4afaSJohn Baldwin 	ps = p->p_sigacts;
16732c42a146SMarcel Moolenaar 
1674628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1675cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1676cb679c38SJonathan Lemon 
16772c42a146SMarcel Moolenaar 	prop = sigprop(sig);
16784093529dSJeff Roberson 
16794093529dSJeff Roberson 	/*
168014b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
168114b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
168214b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
16834093529dSJeff Roberson 	 */
16843074d1b4SDavid Xu 	if (target == SIGTARGET_TD) {
16853074d1b4SDavid Xu 		siglist = &td->td_siglist;
16863074d1b4SDavid Xu 	} else {
16873074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig))
16883074d1b4SDavid Xu 			siglist = &td->td_siglist;
16893074d1b4SDavid Xu 		else if (td->td_waitset != NULL &&
16903074d1b4SDavid Xu 			SIGISMEMBER(*(td->td_waitset), sig))
16913074d1b4SDavid Xu 			siglist = &td->td_siglist;
16923074d1b4SDavid Xu 		else
16933074d1b4SDavid Xu 			siglist = &p->p_siglist;
16943074d1b4SDavid Xu 	}
16954093529dSJeff Roberson 
1696df8bae1dSRodney W. Grimes 	/*
16972a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
16982a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
16992a024a2bSSean Eric Fagan 	 * a chance, as well.
1700df8bae1dSRodney W. Grimes 	 */
1701b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1702df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1703b40ce416SJulian Elischer 	} else {
1704df8bae1dSRodney W. Grimes 		/*
1705df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1706df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
170790af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1708df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1709df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1710df8bae1dSRodney W. Grimes 		 */
171190af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
171290af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
171390af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
171490af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1715df8bae1dSRodney W. Grimes 			return;
171690af4afaSJohn Baldwin 		}
17173074d1b4SDavid Xu 		if (((td->td_waitset == NULL) &&
17183074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig)) ||
17193074d1b4SDavid Xu 		    ((td->td_waitset != NULL) &&
17203074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig) &&
17213074d1b4SDavid Xu 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1722df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
172390af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1724df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1725df8bae1dSRodney W. Grimes 		else
1726df8bae1dSRodney W. Grimes 			action = SIG_DFL;
172790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1728df8bae1dSRodney W. Grimes 	}
1729df8bae1dSRodney W. Grimes 
17304093529dSJeff Roberson 	if (prop & SA_CONT) {
17311d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17324093529dSJeff Roberson 		/*
17334093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17344093529dSJeff Roberson 		 * the proc's siglist.
17354093529dSJeff Roberson 		 */
1736a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17374093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17384093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1739a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17404093529dSJeff Roberson 	}
1741df8bae1dSRodney W. Grimes 
1742df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1743df8bae1dSRodney W. Grimes 		/*
1744df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1745df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1746df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1747df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1748df8bae1dSRodney W. Grimes 		 */
1749e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1750e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1751e602ba25SJulian Elischer 		    (action == SIG_DFL))
1752df8bae1dSRodney W. Grimes 		        return;
17531d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1754a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17554093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17564093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1757a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17586933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1759df8bae1dSRodney W. Grimes 	}
17603074d1b4SDavid Xu 
17614093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17624093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17633074d1b4SDavid Xu 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
17643074d1b4SDavid Xu 	    action != SIG_HOLD) {
17653074d1b4SDavid Xu 		td->td_waitset = NULL;
17663074d1b4SDavid Xu 	}
17673074d1b4SDavid Xu 
17685312b1c7SDavid Xu 	/*
17695312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17705312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17715312b1c7SDavid Xu 	 */
17725312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17735312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17745312b1c7SDavid Xu 		return;
1775df8bae1dSRodney W. Grimes 	/*
1776e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1777e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1778e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1779e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1780e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1781e602ba25SJulian Elischer 	 * We try do the per-process part here.
1782df8bae1dSRodney W. Grimes 	 */
1783e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1784e602ba25SJulian Elischer 		/*
1785e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1786e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1787e602ba25SJulian Elischer 		 */
1788e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1789e602ba25SJulian Elischer 			/*
1790e602ba25SJulian Elischer 			 * The traced process is already stopped,
1791e602ba25SJulian Elischer 			 * so no further action is necessary.
1792e602ba25SJulian Elischer 			 * No signal can restart us.
1793e602ba25SJulian Elischer 			 */
1794e602ba25SJulian Elischer 			goto out;
17951c32c37cSJohn Baldwin 		}
1796b40ce416SJulian Elischer 
1797e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1798df8bae1dSRodney W. Grimes 			/*
1799e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1800e602ba25SJulian Elischer 			 * It will die elsewhere.
1801e602ba25SJulian Elischer 			 * All threads must be restarted.
1802df8bae1dSRodney W. Grimes 			 */
1803e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1804e602ba25SJulian Elischer 			goto runfast;
1805e602ba25SJulian Elischer 		}
1806e602ba25SJulian Elischer 
1807e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1808e602ba25SJulian Elischer 			/*
1809e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18104093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18111d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1812e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18134093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1814e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1815e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1816e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1817e602ba25SJulian Elischer 			 */
18181279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18196933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1820e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18214093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1822e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1823e602ba25SJulian Elischer 				/*
1824e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1825e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1826e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1827e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1828e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1829e602ba25SJulian Elischer 				 * process, we need to make sure that the
1830e602ba25SJulian Elischer 				 * single thread is runnable asap.
1831e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1832e602ba25SJulian Elischer 				 */
1833e602ba25SJulian Elischer 				goto runfast;
1834e602ba25SJulian Elischer 			}
1835e602ba25SJulian Elischer 			/*
1836e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1837e602ba25SJulian Elischer 			 */
1838e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
183904774f23SJulian Elischer 			thread_unsuspend(p);
1840e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1841e602ba25SJulian Elischer 			goto out;
1842e602ba25SJulian Elischer 		}
1843e602ba25SJulian Elischer 
1844e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1845e602ba25SJulian Elischer 			/*
1846e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1847e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
184804774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1849e602ba25SJulian Elischer 			 */
18501279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18514093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1852e602ba25SJulian Elischer 			goto out;
1853e602ba25SJulian Elischer 		}
1854e602ba25SJulian Elischer 
1855e602ba25SJulian Elischer 		/*
1856e602ba25SJulian Elischer 		 * All other kinds of signals:
1857e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1858e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1859e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
186004774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1861e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1862e602ba25SJulian Elischer 		 */
1863e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
186444f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
186544f3b092SJohn Baldwin 			sleepq_abort(td);
1866e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1867df8bae1dSRodney W. Grimes 		goto out;
1868df8bae1dSRodney W. Grimes 		/*
18699a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
18709a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
1871df8bae1dSRodney W. Grimes 		 */
1872e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
187335c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
187435c32a76SDavid Xu 			!(prop & SA_STOP)) {
1875721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18764093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1877721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1878df8bae1dSRodney W. Grimes 			goto out;
187904774f23SJulian Elischer 		}
188035c32a76SDavid Xu 		if (prop & SA_STOP) {
188135c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
188235c32a76SDavid Xu 				goto out;
1883e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1884e574e444SDavid Xu 			p->p_xstat = sig;
188535c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
18864093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
18874093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1888062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1889062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
18904093529dSJeff Roberson 					thread_suspend_one(td0);
1891062cf543SDavid Xu 				} else if (td != td0) {
1892062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1893062cf543SDavid Xu 				}
189435c32a76SDavid Xu 			}
1895e574e444SDavid Xu 			thread_stopped(p);
18964093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1897e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
18984093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
18994093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
19004093529dSJeff Roberson 			}
190135c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
190235c32a76SDavid Xu 			goto out;
190335c32a76SDavid Xu 		}
1904721e5910SJulian Elischer 		else
1905df8bae1dSRodney W. Grimes 			goto runfast;
1906df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1907e602ba25SJulian Elischer 	} else {
1908e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19094093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1910e602ba25SJulian Elischer 		goto out;
1911e602ba25SJulian Elischer 	}
1912e602ba25SJulian Elischer 
1913b40ce416SJulian Elischer 	/*
1914e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1915e602ba25SJulian Elischer 	 * running threads.
1916b40ce416SJulian Elischer 	 */
1917e602ba25SJulian Elischer 
1918e602ba25SJulian Elischer runfast:
1919aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19204093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1921e602ba25SJulian Elischer 	thread_unsuspend(p);
1922e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1923e602ba25SJulian Elischer out:
1924e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1925e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1926e602ba25SJulian Elischer }
1927e602ba25SJulian Elischer 
1928e602ba25SJulian Elischer /*
1929e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1930e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
1931e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1932e602ba25SJulian Elischer  */
1933e602ba25SJulian Elischer static void
19344093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1935e602ba25SJulian Elischer {
1936e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1937e602ba25SJulian Elischer 	register int prop;
1938e602ba25SJulian Elischer 
19398b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1940aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1941e602ba25SJulian Elischer 	prop = sigprop(sig);
1942a4c2da15SBruce Evans 
1943e602ba25SJulian Elischer 	/*
1944aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1945e602ba25SJulian Elischer 	 * killed in this lifetime.
1946e602ba25SJulian Elischer 	 */
1947a4c2da15SBruce Evans 	if (action == SIG_DFL && (prop & SA_KILL)) {
1948a4c2da15SBruce Evans 		if (td->td_priority > PUSER)
1949e602ba25SJulian Elischer 			td->td_priority = PUSER;
1950b40ce416SJulian Elischer 	}
1951a4c2da15SBruce Evans 
195280c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
1953e602ba25SJulian Elischer 		/*
1954e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1955e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1956e602ba25SJulian Elischer 		 * be noticed when the process returns through
1957e602ba25SJulian Elischer 		 * trap() or syscall().
1958e602ba25SJulian Elischer 		 */
195944f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1960aa0fa334SJulian Elischer 			return;
1961e602ba25SJulian Elischer 		/*
1962e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1963e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1964e602ba25SJulian Elischer 		 * for its parent.
1965e602ba25SJulian Elischer 		 */
1966e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1967e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1968aa0fa334SJulian Elischer 		} else {
1969df8bae1dSRodney W. Grimes 			/*
1970e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1971e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1972e602ba25SJulian Elischer 			 * be awakened.
1973df8bae1dSRodney W. Grimes 			 */
1974e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19751d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19764093529dSJeff Roberson 				/*
19774093529dSJeff Roberson 				 * It may be on either list in this state.
19784093529dSJeff Roberson 				 * Remove from both for now.
19794093529dSJeff Roberson 				 */
19804093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1981aa0fa334SJulian Elischer 				return;
1982df8bae1dSRodney W. Grimes 			}
1983df8bae1dSRodney W. Grimes 
1984aa0fa334SJulian Elischer 			/*
1985a4c2da15SBruce Evans 			 * Give low priority threads a better chance to run.
1986aa0fa334SJulian Elischer 			 */
198744f3b092SJohn Baldwin 			if (td->td_priority > PUSER)
1988aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1989aa0fa334SJulian Elischer 		}
199044f3b092SJohn Baldwin 		sleepq_abort(td);
1991a4c2da15SBruce Evans 	} else {
1992df8bae1dSRodney W. Grimes 		/*
1993a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
1994df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1995df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1996df8bae1dSRodney W. Grimes 		 */
1997a4c2da15SBruce Evans #ifdef SMP
199844f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
1999e602ba25SJulian Elischer 			forward_signal(td);
20003163861cSTor Egge #endif
20016caa8a15SJohn Baldwin 	}
2002a4c2da15SBruce Evans }
2003df8bae1dSRodney W. Grimes 
20044cc9f52fSRobert Drehmel void
20054cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
20064cc9f52fSRobert Drehmel {
20074cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
20084cc9f52fSRobert Drehmel 
200930a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20104cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
20114cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
20124cc9f52fSRobert Drehmel 
20134cc9f52fSRobert Drehmel 	p->p_xstat = sig;
20144cc9f52fSRobert Drehmel 	PROC_LOCK(p->p_pptr);
20154cc9f52fSRobert Drehmel 	psignal(p->p_pptr, SIGCHLD);
20164cc9f52fSRobert Drehmel 	PROC_UNLOCK(p->p_pptr);
20174a3b3dcbSColin Percival 	stop(p);
20184cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
20194cc9f52fSRobert Drehmel 	thread_suspend_one(td);
20204cc9f52fSRobert Drehmel 	PROC_UNLOCK(p);
20214cc9f52fSRobert Drehmel 	DROP_GIANT();
202229bcc451SJeff Roberson 	mi_switch(SW_INVOL);
20234cc9f52fSRobert Drehmel 	mtx_unlock_spin(&sched_lock);
20244cc9f52fSRobert Drehmel 	PICKUP_GIANT();
20254cc9f52fSRobert Drehmel }
20264cc9f52fSRobert Drehmel 
2027df8bae1dSRodney W. Grimes /*
2028df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2029df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2030df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2031df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2032df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2033628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2034df8bae1dSRodney W. Grimes  * sequence is
2035df8bae1dSRodney W. Grimes  *
2036e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
20372c42a146SMarcel Moolenaar  *		postsig(sig);
2038df8bae1dSRodney W. Grimes  */
20396711f10fSJohn Baldwin static int
2040e602ba25SJulian Elischer issignal(td)
2041e602ba25SJulian Elischer 	struct thread *td;
2042df8bae1dSRodney W. Grimes {
2043e602ba25SJulian Elischer 	struct proc *p;
204490af4afaSJohn Baldwin 	struct sigacts *ps;
20454093529dSJeff Roberson 	sigset_t sigpending;
2046062cf543SDavid Xu 	int sig, prop;
2047062cf543SDavid Xu 	struct thread *td0;
2048df8bae1dSRodney W. Grimes 
2049e602ba25SJulian Elischer 	p = td->td_proc;
205090af4afaSJohn Baldwin 	ps = p->p_sigacts;
205190af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2052628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2053df8bae1dSRodney W. Grimes 	for (;;) {
20542a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
20552a024a2bSSean Eric Fagan 
20564093529dSJeff Roberson 		sigpending = td->td_siglist;
20574093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
20584093529dSJeff Roberson 
2059df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
20604093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
20614093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2062df8bae1dSRodney W. Grimes 			return (0);
20634093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
20642a024a2bSSean Eric Fagan 
2065047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2066047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2067047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2068047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2069047aa39bSRobert Watson 		}
20702a024a2bSSean Eric Fagan 
2071df8bae1dSRodney W. Grimes 		/*
2072df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2073df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2074df8bae1dSRodney W. Grimes 		 */
207590af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
20764093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
2077df8bae1dSRodney W. Grimes 			continue;
2078df8bae1dSRodney W. Grimes 		}
2079df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2080df8bae1dSRodney W. Grimes 			/*
2081d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2082df8bae1dSRodney W. Grimes 			 */
208390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
20844cc9f52fSRobert Drehmel 			ptracestop(td, sig);
2085628d2653SJohn Baldwin 			PROC_LOCK(p);
208690af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2087df8bae1dSRodney W. Grimes 
2088df8bae1dSRodney W. Grimes 			/*
2089df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
2090df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
2091df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
2092df8bae1dSRodney W. Grimes 			 */
20934093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
20942c42a146SMarcel Moolenaar 			sig = p->p_xstat;
20952c42a146SMarcel Moolenaar 			if (sig == 0)
2096df8bae1dSRodney W. Grimes 				continue;
2097df8bae1dSRodney W. Grimes 
2098df8bae1dSRodney W. Grimes 			/*
20998d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
21008d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
21018d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
21028d542cb5SDavid E. O'Brien 			 */
21038d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
21048d542cb5SDavid E. O'Brien 				continue;
21058d542cb5SDavid E. O'Brien 
21068d542cb5SDavid E. O'Brien 			/*
21074093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
21081d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
2109df8bae1dSRodney W. Grimes 			 */
21104093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
21114093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
2112df8bae1dSRodney W. Grimes 				continue;
21134093529dSJeff Roberson 			signotify(td);
2114df8bae1dSRodney W. Grimes 		}
2115df8bae1dSRodney W. Grimes 
21168d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
21178d542cb5SDavid E. O'Brien 
2118df8bae1dSRodney W. Grimes 		/*
2119df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2120df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2121df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2122df8bae1dSRodney W. Grimes 		 */
2123d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2124df8bae1dSRodney W. Grimes 
2125d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2126df8bae1dSRodney W. Grimes 			/*
2127df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2128df8bae1dSRodney W. Grimes 			 */
2129df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2130df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2131df8bae1dSRodney W. Grimes 				/*
2132df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2133df8bae1dSRodney W. Grimes 				 * in init? XXX
2134df8bae1dSRodney W. Grimes 				 */
2135d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
21362c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2137df8bae1dSRodney W. Grimes #endif
2138df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2139df8bae1dSRodney W. Grimes 			}
2140df8bae1dSRodney W. Grimes 			/*
2141df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2142df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2143df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2144df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2145df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2146df8bae1dSRodney W. Grimes 			 */
2147df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2148df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2149df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2150df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2151df8bae1dSRodney W. Grimes 					break;	/* == ignore */
215290af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
215390af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
215490af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2155e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
21562c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2157721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2158062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2159062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2160062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2161062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2162062cf543SDavid Xu 						thread_suspend_one(td0);
2163062cf543SDavid Xu 					} else if (td != td0) {
2164062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2165062cf543SDavid Xu 					}
2166062cf543SDavid Xu 				}
2167e574e444SDavid Xu 				thread_stopped(p);
216871fad9fdSJulian Elischer 				thread_suspend_one(td);
2169721e5910SJulian Elischer 				PROC_UNLOCK(p);
2170721e5910SJulian Elischer 				DROP_GIANT();
217129bcc451SJeff Roberson 				mi_switch(SW_INVOL);
2172721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2173721e5910SJulian Elischer 				PICKUP_GIANT();
2174721e5910SJulian Elischer 				PROC_LOCK(p);
217590af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2176df8bae1dSRodney W. Grimes 				break;
217721b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2178df8bae1dSRodney W. Grimes 				/*
2179df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2180df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2181df8bae1dSRodney W. Grimes 				 */
2182df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2183df8bae1dSRodney W. Grimes 			} else
21842c42a146SMarcel Moolenaar 				return (sig);
2185df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2186df8bae1dSRodney W. Grimes 
2187d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2188df8bae1dSRodney W. Grimes 			/*
2189df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2190df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2191df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2192df8bae1dSRodney W. Grimes 			 */
2193df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2194df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2195df8bae1dSRodney W. Grimes 				printf("issignal\n");
2196df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2197df8bae1dSRodney W. Grimes 
2198df8bae1dSRodney W. Grimes 		default:
2199df8bae1dSRodney W. Grimes 			/*
2200df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2201df8bae1dSRodney W. Grimes 			 * postsig() process it.
2202df8bae1dSRodney W. Grimes 			 */
22032c42a146SMarcel Moolenaar 			return (sig);
2204df8bae1dSRodney W. Grimes 		}
22054093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2206df8bae1dSRodney W. Grimes 	}
2207df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2208df8bae1dSRodney W. Grimes }
2209df8bae1dSRodney W. Grimes 
2210df8bae1dSRodney W. Grimes /*
2211df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2212df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
22134a3b3dcbSColin Percival  * on the run queue.  Must be called with the proc p locked.
2214df8bae1dSRodney W. Grimes  */
22155b3047d5SJohn Baldwin static void
2216e574e444SDavid Xu stop(struct proc *p)
2217df8bae1dSRodney W. Grimes {
2218df8bae1dSRodney W. Grimes 
2219628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22201279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2221df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
222201609114SAlfred Perlstein 	wakeup(p->p_pptr);
2223df8bae1dSRodney W. Grimes }
2224df8bae1dSRodney W. Grimes 
222590af4afaSJohn Baldwin /*
222690af4afaSJohn Baldwin  * MPSAFE
222790af4afaSJohn Baldwin  */
2228e574e444SDavid Xu void
2229e574e444SDavid Xu thread_stopped(struct proc *p)
2230e574e444SDavid Xu {
2231e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
223290af4afaSJohn Baldwin 	struct sigacts *ps;
2233e574e444SDavid Xu 	int n;
2234e574e444SDavid Xu 
2235e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2236e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2237e574e444SDavid Xu 	n = p->p_suspcount;
2238e574e444SDavid Xu 	if (p == p1)
2239e574e444SDavid Xu 		n++;
2240e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2241e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2242e574e444SDavid Xu 		stop(p);
2243e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
224490af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
224590af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
224690af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
224790af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2248e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
224990af4afaSJohn Baldwin 		} else
225090af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2251e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2252e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2253e574e444SDavid Xu 	}
2254e574e444SDavid Xu }
2255e574e444SDavid Xu 
2256df8bae1dSRodney W. Grimes /*
2257df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2258df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2259df8bae1dSRodney W. Grimes  */
2260df8bae1dSRodney W. Grimes void
22612c42a146SMarcel Moolenaar postsig(sig)
22622c42a146SMarcel Moolenaar 	register int sig;
2263df8bae1dSRodney W. Grimes {
2264b40ce416SJulian Elischer 	struct thread *td = curthread;
2265b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2266628d2653SJohn Baldwin 	struct sigacts *ps;
22672c42a146SMarcel Moolenaar 	sig_t action;
22682c42a146SMarcel Moolenaar 	sigset_t returnmask;
22692c42a146SMarcel Moolenaar 	int code;
2270df8bae1dSRodney W. Grimes 
22712c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
22725526d2d9SEivind Eklund 
22732ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2274628d2653SJohn Baldwin 	ps = p->p_sigacts;
227590af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
22764093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
22772c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2278df8bae1dSRodney W. Grimes #ifdef KTRACE
2279374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
22805e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
22814093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2282df8bae1dSRodney W. Grimes #endif
2283047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2284047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2285047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2286047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2287047aa39bSRobert Watson 	}
22882a024a2bSSean Eric Fagan 
2289aa0aa7a1STim J. Robbins 	if (!(td->td_pflags & TDP_SA && td->td_mailbox) &&
2290432b45deSDavid Xu 	    action == SIG_DFL) {
2291df8bae1dSRodney W. Grimes 		/*
2292df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2293df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2294df8bae1dSRodney W. Grimes 		 */
229590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2296b40ce416SJulian Elischer 		sigexit(td, sig);
2297df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2298df8bae1dSRodney W. Grimes 	} else {
2299aa0aa7a1STim J. Robbins 		if (td->td_pflags & TDP_SA && td->td_mailbox) {
2300432b45deSDavid Xu 			if (sig == SIGKILL) {
2301432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2302432b45deSDavid Xu 				sigexit(td, sig);
2303432b45deSDavid Xu 			}
2304432b45deSDavid Xu 		}
2305432b45deSDavid Xu 
2306df8bae1dSRodney W. Grimes 		/*
2307df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2308df8bae1dSRodney W. Grimes 		 */
23094093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23105526d2d9SEivind Eklund 		    ("postsig action"));
2311df8bae1dSRodney W. Grimes 		/*
2312df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2313645682fdSLuoqi Chen 		 * occurrences of this signal.
2314df8bae1dSRodney W. Grimes 		 *
2315645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2316df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2317645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2318df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2319df8bae1dSRodney W. Grimes 		 */
23205e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23214093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23225e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2323df8bae1dSRodney W. Grimes 		} else
23244093529dSJeff Roberson 			returnmask = td->td_sigmask;
23252c42a146SMarcel Moolenaar 
23264093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23272c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23284093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23292c42a146SMarcel Moolenaar 
23302c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2331289ccde0SPeter Wemm 			/*
23328f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2333289ccde0SPeter Wemm 			 */
233490af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23352c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23362c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
233790af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23382c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2339dedc04feSPeter Wemm 		}
2340df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23412c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2342df8bae1dSRodney W. Grimes 			code = 0;
2343df8bae1dSRodney W. Grimes 		} else {
23446626c604SJulian Elischer 			code = p->p_code;
23456626c604SJulian Elischer 			p->p_code = 0;
23466626c604SJulian Elischer 			p->p_sig = 0;
2347df8bae1dSRodney W. Grimes 		}
2348aa0aa7a1STim J. Robbins 		if (td->td_pflags & TDP_SA && td->td_mailbox)
234958a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
235058a3c273SJeff Roberson 		else
235158a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
235258a3c273SJeff Roberson 			    &returnmask, code);
2353df8bae1dSRodney W. Grimes 	}
2354df8bae1dSRodney W. Grimes }
2355df8bae1dSRodney W. Grimes 
2356df8bae1dSRodney W. Grimes /*
2357df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2358df8bae1dSRodney W. Grimes  */
235926f9a767SRodney W. Grimes void
2360df8bae1dSRodney W. Grimes killproc(p, why)
2361df8bae1dSRodney W. Grimes 	struct proc *p;
2362df8bae1dSRodney W. Grimes 	char *why;
2363df8bae1dSRodney W. Grimes {
23649081e5e8SJohn Baldwin 
23659081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
23660384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
23670384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2368729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2369b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2370df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2371df8bae1dSRodney W. Grimes }
2372df8bae1dSRodney W. Grimes 
2373df8bae1dSRodney W. Grimes /*
2374df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2375df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2376df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2377df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2378df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2379df8bae1dSRodney W. Grimes  * does not return.
238090af4afaSJohn Baldwin  *
238190af4afaSJohn Baldwin  * MPSAFE
2382df8bae1dSRodney W. Grimes  */
238326f9a767SRodney W. Grimes void
2384b40ce416SJulian Elischer sigexit(td, sig)
2385b40ce416SJulian Elischer 	struct thread *td;
23862c42a146SMarcel Moolenaar 	int sig;
2387df8bae1dSRodney W. Grimes {
2388b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2389df8bae1dSRodney W. Grimes 
2390628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2391df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
23922c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
23932c42a146SMarcel Moolenaar 		p->p_sig = sig;
2394c364e17eSAndrey A. Chernov 		/*
2395c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2396c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2397c364e17eSAndrey A. Chernov 		 * these messages.)
2398c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
23994ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2400c364e17eSAndrey A. Chernov 		 */
2401b40ce416SJulian Elischer 		if (coredump(td) == 0)
24022c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
240357308494SJoerg Wunsch 		if (kern_logsigexit)
240457308494SJoerg Wunsch 			log(LOG_INFO,
240557308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24063d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24079c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24082c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24092c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
24104ae89b95SJohn Baldwin 	} else
2411628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2412b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2413df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2414df8bae1dSRodney W. Grimes }
2415df8bae1dSRodney W. Grimes 
2416c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2417c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2418c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2419c5edb423SSean Eric Fagan 
2420c5edb423SSean Eric Fagan /*
2421c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2422c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2423c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2424c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2425c5edb423SSean Eric Fagan  *	%P	process id (pid)
2426c5edb423SSean Eric Fagan  *	%U	user id (uid)
2427c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2428c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2429c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2430c5edb423SSean Eric Fagan  */
2431c5edb423SSean Eric Fagan 
2432fca666a1SJulian Elischer static char *
2433c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24348b43b535SAlfred Perlstein 	const char *name;
24358b43b535SAlfred Perlstein 	uid_t uid;
24368b43b535SAlfred Perlstein 	pid_t pid;
24378b43b535SAlfred Perlstein {
24388b43b535SAlfred Perlstein 	const char *format, *appendstr;
2439c5edb423SSean Eric Fagan 	char *temp;
2440c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24418b43b535SAlfred Perlstein 	size_t i, l, n;
2442c5edb423SSean Eric Fagan 
24438b43b535SAlfred Perlstein 	format = corefilename;
24448b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24450bfe2990SEivind Eklund 	if (temp == NULL)
24468b43b535SAlfred Perlstein 		return (NULL);
2447ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2448c5edb423SSean Eric Fagan 		switch (format[i]) {
2449c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2450c5edb423SSean Eric Fagan 			i++;
2451c5edb423SSean Eric Fagan 			switch (format[i]) {
2452c5edb423SSean Eric Fagan 			case '%':
24538b43b535SAlfred Perlstein 				appendstr = "%";
2454c5edb423SSean Eric Fagan 				break;
2455c5edb423SSean Eric Fagan 			case 'N':	/* process name */
24568b43b535SAlfred Perlstein 				appendstr = name;
2457c5edb423SSean Eric Fagan 				break;
2458c5edb423SSean Eric Fagan 			case 'P':	/* process id */
24598b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
24608b43b535SAlfred Perlstein 				appendstr = buf;
2461c5edb423SSean Eric Fagan 				break;
2462c5edb423SSean Eric Fagan 			case 'U':	/* user id */
24638b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
24648b43b535SAlfred Perlstein 				appendstr = buf;
2465c5edb423SSean Eric Fagan 				break;
2466c5edb423SSean Eric Fagan 			default:
24678b43b535SAlfred Perlstein 				appendstr = "";
24688b43b535SAlfred Perlstein 			  	log(LOG_ERR,
24698b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
24708b43b535SAlfred Perlstein 				    format[i], format);
2471c5edb423SSean Eric Fagan 			}
24728b43b535SAlfred Perlstein 			l = strlen(appendstr);
24738b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
24748b43b535SAlfred Perlstein 				goto toolong;
24758b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
24768b43b535SAlfred Perlstein 			n += l;
2477c5edb423SSean Eric Fagan 			break;
2478c5edb423SSean Eric Fagan 		default:
2479c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2480c5edb423SSean Eric Fagan 		}
2481c5edb423SSean Eric Fagan 	}
24828b43b535SAlfred Perlstein 	if (format[i] != '\0')
24838b43b535SAlfred Perlstein 		goto toolong;
24848b43b535SAlfred Perlstein 	return (temp);
24858b43b535SAlfred Perlstein toolong:
24868b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
24878b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
24888b43b535SAlfred Perlstein 	free(temp, M_TEMP);
24898b43b535SAlfred Perlstein 	return (NULL);
2490c5edb423SSean Eric Fagan }
2491c5edb423SSean Eric Fagan 
2492df8bae1dSRodney W. Grimes /*
2493fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2494fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2495fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2496fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2497fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2498fca666a1SJulian Elischer  */
2499fca666a1SJulian Elischer 
2500fca666a1SJulian Elischer static int
2501b40ce416SJulian Elischer coredump(struct thread *td)
2502fca666a1SJulian Elischer {
2503b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2504fca666a1SJulian Elischer 	register struct vnode *vp;
25059c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
250606ae1e91SMatthew Dillon 	struct flock lf;
2507fca666a1SJulian Elischer 	struct nameidata nd;
2508fca666a1SJulian Elischer 	struct vattr vattr;
2509c447f5b2SRobert Watson 	int error, error1, flags, locked;
2510f2a2857bSKirk McKusick 	struct mount *mp;
2511fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2512fca666a1SJulian Elischer 	off_t limit;
2513fca666a1SJulian Elischer 
25144ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2515628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2516fca666a1SJulian Elischer 
2517628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2518628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2519fca666a1SJulian Elischer 		return (EFAULT);
2520628d2653SJohn Baldwin 	}
2521fca666a1SJulian Elischer 
2522fca666a1SJulian Elischer 	/*
252335a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
252435a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
252535a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
252635a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
252735a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
252835a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2529fca666a1SJulian Elischer 	 */
253091d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2531628d2653SJohn Baldwin 	PROC_UNLOCK(p);
253291d5354aSJohn Baldwin 	if (limit == 0)
253391d5354aSJohn Baldwin 		return (EFBIG);
253435a2598fSSean Eric Fagan 
25354ae89b95SJohn Baldwin 	mtx_lock(&Giant);
2536f2a2857bSKirk McKusick restart:
25379c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
25384ae89b95SJohn Baldwin 	if (name == NULL) {
25394ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2540ccdbd10cSPeter Pentchev 		return (EINVAL);
25414ae89b95SJohn Baldwin 	}
2542b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2543e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25447c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2545fca666a1SJulian Elischer 	free(name, M_TEMP);
25464ae89b95SJohn Baldwin 	if (error) {
25474ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2548fca666a1SJulian Elischer 		return (error);
25494ae89b95SJohn Baldwin 	}
2550762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2551fca666a1SJulian Elischer 	vp = nd.ni_vp;
255206ae1e91SMatthew Dillon 
2553832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2554832dafadSDon Lewis 	if (vp->v_type != VREG ||
2555832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2556832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2557832dafadSDon Lewis 		error = EFAULT;
25584ae89b95SJohn Baldwin 		goto out;
2559832dafadSDon Lewis 	}
2560832dafadSDon Lewis 
2561b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
256206ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
256306ae1e91SMatthew Dillon 	lf.l_start = 0;
256406ae1e91SMatthew Dillon 	lf.l_len = 0;
256506ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2566c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
256706ae1e91SMatthew Dillon 
256806ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
256906ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
257036bbf86bSRobert Watson 		if (locked)
257106ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2572b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2573f2a2857bSKirk McKusick 			return (error);
2574f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2575f2a2857bSKirk McKusick 			return (error);
2576f2a2857bSKirk McKusick 		goto restart;
2577f2a2857bSKirk McKusick 	}
2578fca666a1SJulian Elischer 
2579fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2580fca666a1SJulian Elischer 	vattr.va_size = 0;
258188b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2582b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2583b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
258488b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2585628d2653SJohn Baldwin 	PROC_LOCK(p);
2586fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2587628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2588fca666a1SJulian Elischer 
2589fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2590b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2591fca666a1SJulian Elischer 	  ENOSYS;
2592fca666a1SJulian Elischer 
2593c447f5b2SRobert Watson 	if (locked) {
259406ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
259506ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2596c447f5b2SRobert Watson 	}
2597f2a2857bSKirk McKusick 	vn_finished_write(mp);
25984ae89b95SJohn Baldwin out:
2599b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
26004ae89b95SJohn Baldwin 	mtx_unlock(&Giant);
2601fca666a1SJulian Elischer 	if (error == 0)
2602fca666a1SJulian Elischer 		error = error1;
2603fca666a1SJulian Elischer 	return (error);
2604fca666a1SJulian Elischer }
2605fca666a1SJulian Elischer 
2606fca666a1SJulian Elischer /*
2607df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2608df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2609df8bae1dSRodney W. Grimes  */
2610d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2611df8bae1dSRodney W. Grimes struct nosys_args {
2612df8bae1dSRodney W. Grimes 	int	dummy;
2613df8bae1dSRodney W. Grimes };
2614d2d3e875SBruce Evans #endif
2615fb99ab88SMatthew Dillon /*
2616fb99ab88SMatthew Dillon  * MPSAFE
2617fb99ab88SMatthew Dillon  */
2618df8bae1dSRodney W. Grimes /* ARGSUSED */
261926f9a767SRodney W. Grimes int
2620b40ce416SJulian Elischer nosys(td, args)
2621b40ce416SJulian Elischer 	struct thread *td;
2622df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2623df8bae1dSRodney W. Grimes {
2624b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2625b40ce416SJulian Elischer 
2626628d2653SJohn Baldwin 	PROC_LOCK(p);
2627df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2628628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2629f5216b9aSBruce Evans 	return (ENOSYS);
2630df8bae1dSRodney W. Grimes }
2631831d27a9SDon Lewis 
2632831d27a9SDon Lewis /*
263348f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2634831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2635831d27a9SDon Lewis  */
2636831d27a9SDon Lewis void
2637f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2638f1320723SAlfred Perlstein 	struct sigio **sigiop;
26392c42a146SMarcel Moolenaar 	int sig, checkctty;
2640831d27a9SDon Lewis {
2641f1320723SAlfred Perlstein 	struct sigio *sigio;
2642831d27a9SDon Lewis 
2643f1320723SAlfred Perlstein 	SIGIO_LOCK();
2644f1320723SAlfred Perlstein 	sigio = *sigiop;
2645f1320723SAlfred Perlstein 	if (sigio == NULL) {
2646f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2647f1320723SAlfred Perlstein 		return;
2648f1320723SAlfred Perlstein 	}
2649831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2650628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
26512b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
26522c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2653628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2654831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2655831d27a9SDon Lewis 		struct proc *p;
2656831d27a9SDon Lewis 
2657f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2658628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2659628d2653SJohn Baldwin 			PROC_LOCK(p);
26602b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2661831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
26622c42a146SMarcel Moolenaar 				psignal(p, sig);
2663628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2664628d2653SJohn Baldwin 		}
2665f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2666831d27a9SDon Lewis 	}
2667f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2668831d27a9SDon Lewis }
2669cb679c38SJonathan Lemon 
2670cb679c38SJonathan Lemon static int
2671cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2672cb679c38SJonathan Lemon {
2673cb679c38SJonathan Lemon 	struct proc *p = curproc;
2674cb679c38SJonathan Lemon 
2675cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2676cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2677cb679c38SJonathan Lemon 
2678628d2653SJohn Baldwin 	PROC_LOCK(p);
2679cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2680628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2681cb679c38SJonathan Lemon 
2682cb679c38SJonathan Lemon 	return (0);
2683cb679c38SJonathan Lemon }
2684cb679c38SJonathan Lemon 
2685cb679c38SJonathan Lemon static void
2686cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2687cb679c38SJonathan Lemon {
2688cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2689cb679c38SJonathan Lemon 
2690628d2653SJohn Baldwin 	PROC_LOCK(p);
2691e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2692628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2693cb679c38SJonathan Lemon }
2694cb679c38SJonathan Lemon 
2695cb679c38SJonathan Lemon /*
2696cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2697cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2698cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2699cb679c38SJonathan Lemon  * isn't worth the trouble.
2700cb679c38SJonathan Lemon  */
2701cb679c38SJonathan Lemon static int
2702cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2703cb679c38SJonathan Lemon {
2704cb679c38SJonathan Lemon 
2705cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2706cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2707cb679c38SJonathan Lemon 
2708cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2709cb679c38SJonathan Lemon 			kn->kn_data++;
2710cb679c38SJonathan Lemon 	}
2711cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2712cb679c38SJonathan Lemon }
271390af4afaSJohn Baldwin 
271490af4afaSJohn Baldwin struct sigacts *
271590af4afaSJohn Baldwin sigacts_alloc(void)
271690af4afaSJohn Baldwin {
271790af4afaSJohn Baldwin 	struct sigacts *ps;
271890af4afaSJohn Baldwin 
271990af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
272090af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
272190af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
272290af4afaSJohn Baldwin 	return (ps);
272390af4afaSJohn Baldwin }
272490af4afaSJohn Baldwin 
272590af4afaSJohn Baldwin void
272690af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
272790af4afaSJohn Baldwin {
272890af4afaSJohn Baldwin 
272990af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
273090af4afaSJohn Baldwin 	ps->ps_refcnt--;
273190af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
273290af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
273390af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
273490af4afaSJohn Baldwin 	} else
273590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
273690af4afaSJohn Baldwin }
273790af4afaSJohn Baldwin 
273890af4afaSJohn Baldwin struct sigacts *
273990af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
274090af4afaSJohn Baldwin {
274190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
274290af4afaSJohn Baldwin 	ps->ps_refcnt++;
274390af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
274490af4afaSJohn Baldwin 	return (ps);
274590af4afaSJohn Baldwin }
274690af4afaSJohn Baldwin 
274790af4afaSJohn Baldwin void
274890af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
274990af4afaSJohn Baldwin {
275090af4afaSJohn Baldwin 
275190af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
275290af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
275390af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
275490af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
275590af4afaSJohn Baldwin }
275690af4afaSJohn Baldwin 
275790af4afaSJohn Baldwin int
275890af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
275990af4afaSJohn Baldwin {
276090af4afaSJohn Baldwin 	int shared;
276190af4afaSJohn Baldwin 
276290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
276390af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
276490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
276590af4afaSJohn Baldwin 	return (shared);
276690af4afaSJohn Baldwin }
2767