xref: /freebsd/sys/kern/kern_sig.c (revision 482d099c50770c94a6e17a66ce3f6d5d817c3e90)
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 
1256141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
1266141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
1276141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
1286141e04aSJohn-Mark Gurney 
1292c42a146SMarcel Moolenaar /*
1302c42a146SMarcel Moolenaar  * Signal properties and actions.
1312c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1322c42a146SMarcel Moolenaar  * according to the following properties:
1332c42a146SMarcel Moolenaar  */
1342c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1352c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1362c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1372c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1382c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1392c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1402c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
141da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
142df8bae1dSRodney W. Grimes 
1432c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
144da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
145da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
146da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
150da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1512c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
152da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1532c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1542c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1552c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
156da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
157da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
158da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
159da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
160da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
161da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
162da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
163da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
164da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
165da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
166da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1672c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1682c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
169da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
170da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
171da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
172da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
173da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
174da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1752c42a146SMarcel Moolenaar };
1762c42a146SMarcel Moolenaar 
177fbbeeb6cSBruce Evans /*
178fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
179fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
180fbbeeb6cSBruce Evans  * action, the process stops in issignal().
181e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
182fbbeeb6cSBruce Evans  *
18333510ef1SBruce Evans  * MP SAFE.
184fbbeeb6cSBruce Evans  */
185fbbeeb6cSBruce Evans int
186e602ba25SJulian Elischer cursig(struct thread *td)
187fbbeeb6cSBruce Evans {
188c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
18990af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
190179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1914093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
192fbbeeb6cSBruce Evans }
193fbbeeb6cSBruce Evans 
19479065dbaSBruce Evans /*
19579065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1964093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1974093529dSJeff Roberson  * unmasked in td_sigmask.
19879065dbaSBruce Evans  */
19979065dbaSBruce Evans void
2004093529dSJeff Roberson signotify(struct thread *td)
20179065dbaSBruce Evans {
2024093529dSJeff Roberson 	struct proc *p;
2039dde3bc9SDavid Xu 	sigset_t set, saved;
2044093529dSJeff Roberson 
2054093529dSJeff Roberson 	p = td->td_proc;
20679065dbaSBruce Evans 
20779065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2084093529dSJeff Roberson 
2094093529dSJeff Roberson 	/*
2104093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2114093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2124093529dSJeff Roberson 	 */
2134093529dSJeff Roberson 	set = p->p_siglist;
2149dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
2159dde3bc9SDavid Xu 		saved = p->p_siglist;
2164093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2174093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2184093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2194093529dSJeff Roberson 
2208b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
22179065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2224093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
22379065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
22479065dbaSBruce Evans 	}
2259dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2266dbc0850SJohn Baldwin 		if (!SIGSETEQ(saved, p->p_siglist)) {
2279dde3bc9SDavid Xu 			/* pending set changed */
2289dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
2299dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
2309dde3bc9SDavid Xu 		}
2319dde3bc9SDavid Xu 	}
2328b94a061SJohn Baldwin }
2338b94a061SJohn Baldwin 
2348b94a061SJohn Baldwin int
2358b94a061SJohn Baldwin sigonstack(size_t sp)
2368b94a061SJohn Baldwin {
237a30ec4b9SDavid Xu 	struct thread *td = curthread;
2388b94a061SJohn Baldwin 
239a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
2401930e303SPoul-Henning Kamp #if defined(COMPAT_43)
241a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
242a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
243a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
2448b94a061SJohn Baldwin #else
245a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
2468b94a061SJohn Baldwin #endif
2478b94a061SJohn Baldwin 	    : 0);
2488b94a061SJohn Baldwin }
24979065dbaSBruce Evans 
2506f841fb7SMarcel Moolenaar static __inline int
2516f841fb7SMarcel Moolenaar sigprop(int sig)
2522c42a146SMarcel Moolenaar {
2536f841fb7SMarcel Moolenaar 
2542c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2552c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2562c42a146SMarcel Moolenaar 	return (0);
257df8bae1dSRodney W. Grimes }
2582c42a146SMarcel Moolenaar 
2594093529dSJeff Roberson int
2606f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2612c42a146SMarcel Moolenaar {
2622c42a146SMarcel Moolenaar 	int i;
2632c42a146SMarcel Moolenaar 
2646f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2652c42a146SMarcel Moolenaar 		if (set->__bits[i])
2662c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
267df8bae1dSRodney W. Grimes 	return (0);
268df8bae1dSRodney W. Grimes }
269df8bae1dSRodney W. Grimes 
2702c42a146SMarcel Moolenaar /*
2718f19eb88SIan Dowse  * kern_sigaction
2722c42a146SMarcel Moolenaar  * sigaction
27323eeeff7SPeter Wemm  * freebsd4_sigaction
2742c42a146SMarcel Moolenaar  * osigaction
27525d6dc06SJohn Baldwin  *
27625d6dc06SJohn Baldwin  * MPSAFE
2772c42a146SMarcel Moolenaar  */
2788f19eb88SIan Dowse int
27923eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2808f19eb88SIan Dowse 	struct thread *td;
2812c42a146SMarcel Moolenaar 	register int sig;
2822c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
28323eeeff7SPeter Wemm 	int flags;
284df8bae1dSRodney W. Grimes {
28590af4afaSJohn Baldwin 	struct sigacts *ps;
2864093529dSJeff Roberson 	struct thread *td0;
2878f19eb88SIan Dowse 	struct proc *p = td->td_proc;
288df8bae1dSRodney W. Grimes 
2892899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2902c42a146SMarcel Moolenaar 		return (EINVAL);
2912c42a146SMarcel Moolenaar 
292628d2653SJohn Baldwin 	PROC_LOCK(p);
293628d2653SJohn Baldwin 	ps = p->p_sigacts;
29490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2952c42a146SMarcel Moolenaar 	if (oact) {
2962c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2972c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2982c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2992c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
3002c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
3012c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
3022c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
3032c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
3042c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
3052c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
3062c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
3072c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
3082c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
30990af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
3102c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
31190af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
3122c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
3132c42a146SMarcel Moolenaar 	}
3142c42a146SMarcel Moolenaar 	if (act) {
3152c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
316628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
31790af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
318628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3192c42a146SMarcel Moolenaar 			return (EINVAL);
320628d2653SJohn Baldwin 		}
3212c42a146SMarcel Moolenaar 
322df8bae1dSRodney W. Grimes 		/*
323df8bae1dSRodney W. Grimes 		 * Change setting atomically.
324df8bae1dSRodney W. Grimes 		 */
3252c42a146SMarcel Moolenaar 
3262c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3272c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3282c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
329aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
330aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
33180f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
33280f42b55SIan Dowse 		} else {
33380f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3342c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3352c42a146SMarcel Moolenaar 		}
3362c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3372c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
338df8bae1dSRodney W. Grimes 		else
3392c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3402c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3412c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
342df8bae1dSRodney W. Grimes 		else
3432c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3442c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3452c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3461e41c1b5SSteven Wallace 		else
3472c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3482c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3492c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
350289ccde0SPeter Wemm 		else
3512c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
3522c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3532c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
35490af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3556626c604SJulian Elischer 			else
35690af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
357ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
358245f17d4SJoerg Wunsch 				/*
3592c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3602c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3612c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3622c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
363245f17d4SJoerg Wunsch 				 */
364245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
36590af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3666626c604SJulian Elischer 				else
36790af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
368245f17d4SJoerg Wunsch 			} else
36990af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
370ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
37190af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
372ba1551caSIan Dowse 			else
37390af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
374df8bae1dSRodney W. Grimes 		}
375df8bae1dSRodney W. Grimes 		/*
37690af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3772c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
37890af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3792c42a146SMarcel Moolenaar 		 * have to restart the process.
380df8bae1dSRodney W. Grimes 		 */
3812c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3822c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3832c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3849dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
3859dde3bc9SDavid Xu 			     SIGISMEMBER(p->p_siglist, sig)) {
3869dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
3879dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
3889dde3bc9SDavid Xu 			}
3892c42a146SMarcel Moolenaar 			/* never to be seen again */
3901d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
391a9a48d68SDavid Xu 			mtx_lock_spin(&sched_lock);
3924093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3934093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
394a9a48d68SDavid Xu 			mtx_unlock_spin(&sched_lock);
3952c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3962c42a146SMarcel Moolenaar 				/* easier in psignal */
39790af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
39890af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
399645682fdSLuoqi Chen 		} else {
40090af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
4012c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
40290af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
4032c42a146SMarcel Moolenaar 			else
40490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
4052c42a146SMarcel Moolenaar 		}
40623eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
40723eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
40823eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
40923eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
41023eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
41123eeeff7SPeter Wemm 		else
41223eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
41323eeeff7SPeter Wemm #endif
414e8ebc08fSPeter Wemm #ifdef COMPAT_43
415645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
41623eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41723eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
418645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
419645682fdSLuoqi Chen 		else
420645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
421e8ebc08fSPeter Wemm #endif
422df8bae1dSRodney W. Grimes 	}
42390af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
424628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4252c42a146SMarcel Moolenaar 	return (0);
4262c42a146SMarcel Moolenaar }
4272c42a146SMarcel Moolenaar 
4282c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4292c42a146SMarcel Moolenaar struct sigaction_args {
4302c42a146SMarcel Moolenaar 	int	sig;
4312c42a146SMarcel Moolenaar 	struct	sigaction *act;
4322c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4332c42a146SMarcel Moolenaar };
4342c42a146SMarcel Moolenaar #endif
435fb99ab88SMatthew Dillon /*
436fb99ab88SMatthew Dillon  * MPSAFE
437fb99ab88SMatthew Dillon  */
4382c42a146SMarcel Moolenaar int
439b40ce416SJulian Elischer sigaction(td, uap)
440b40ce416SJulian Elischer 	struct thread *td;
4412c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4422c42a146SMarcel Moolenaar {
4432c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4442c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4452c42a146SMarcel Moolenaar 	int error;
4462c42a146SMarcel Moolenaar 
4476f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4486f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4492c42a146SMarcel Moolenaar 	if (actp) {
4506f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4512c42a146SMarcel Moolenaar 		if (error)
45244443757STim J. Robbins 			return (error);
4532c42a146SMarcel Moolenaar 	}
4548f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
45525d6dc06SJohn Baldwin 	if (oactp && !error)
4566f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4572c42a146SMarcel Moolenaar 	return (error);
4582c42a146SMarcel Moolenaar }
4592c42a146SMarcel Moolenaar 
46023eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
46123eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
46223eeeff7SPeter Wemm struct freebsd4_sigaction_args {
46323eeeff7SPeter Wemm 	int	sig;
46423eeeff7SPeter Wemm 	struct	sigaction *act;
46523eeeff7SPeter Wemm 	struct	sigaction *oact;
46623eeeff7SPeter Wemm };
46723eeeff7SPeter Wemm #endif
46823eeeff7SPeter Wemm /*
46923eeeff7SPeter Wemm  * MPSAFE
47023eeeff7SPeter Wemm  */
47123eeeff7SPeter Wemm int
47223eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
47323eeeff7SPeter Wemm 	struct thread *td;
47423eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
47523eeeff7SPeter Wemm {
47623eeeff7SPeter Wemm 	struct sigaction act, oact;
47723eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
47823eeeff7SPeter Wemm 	int error;
47923eeeff7SPeter Wemm 
48023eeeff7SPeter Wemm 
48123eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
48223eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
48323eeeff7SPeter Wemm 	if (actp) {
48423eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
48523eeeff7SPeter Wemm 		if (error)
48644443757STim J. Robbins 			return (error);
48723eeeff7SPeter Wemm 	}
48823eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
489a14e1189SJohn Baldwin 	if (oactp && !error)
49023eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
49123eeeff7SPeter Wemm 	return (error);
49223eeeff7SPeter Wemm }
49323eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
49423eeeff7SPeter Wemm 
49531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4962c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4972c42a146SMarcel Moolenaar struct osigaction_args {
4982c42a146SMarcel Moolenaar 	int	signum;
4992c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
5002c42a146SMarcel Moolenaar 	struct	osigaction *osa;
5012c42a146SMarcel Moolenaar };
5022c42a146SMarcel Moolenaar #endif
503fb99ab88SMatthew Dillon /*
504fb99ab88SMatthew Dillon  * MPSAFE
505fb99ab88SMatthew Dillon  */
5062c42a146SMarcel Moolenaar int
507b40ce416SJulian Elischer osigaction(td, uap)
508b40ce416SJulian Elischer 	struct thread *td;
5092c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
5102c42a146SMarcel Moolenaar {
5112c42a146SMarcel Moolenaar 	struct osigaction sa;
5122c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5132c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5142c42a146SMarcel Moolenaar 	int error;
5152c42a146SMarcel Moolenaar 
5166f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5176f841fb7SMarcel Moolenaar 		return (EINVAL);
518fb99ab88SMatthew Dillon 
5196f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5206f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
521fb99ab88SMatthew Dillon 
5222c42a146SMarcel Moolenaar 	if (nsap) {
5236f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5242c42a146SMarcel Moolenaar 		if (error)
52544443757STim J. Robbins 			return (error);
5262c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5272c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5282c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5292c42a146SMarcel Moolenaar 	}
53023eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5312c42a146SMarcel Moolenaar 	if (osap && !error) {
5322c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5332c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5342c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5356f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5362c42a146SMarcel Moolenaar 	}
5372c42a146SMarcel Moolenaar 	return (error);
5382c42a146SMarcel Moolenaar }
53923eeeff7SPeter Wemm 
54023eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
54123eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
54223eeeff7SPeter Wemm int
54323eeeff7SPeter Wemm osigreturn(td, uap)
54423eeeff7SPeter Wemm 	struct thread *td;
54523eeeff7SPeter Wemm 	struct osigreturn_args *uap;
54623eeeff7SPeter Wemm {
54723eeeff7SPeter Wemm 
54823eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
54923eeeff7SPeter Wemm }
55023eeeff7SPeter Wemm #endif
55131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes /*
554df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
555df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
556df8bae1dSRodney W. Grimes  */
557df8bae1dSRodney W. Grimes void
558df8bae1dSRodney W. Grimes siginit(p)
559df8bae1dSRodney W. Grimes 	struct proc *p;
560df8bae1dSRodney W. Grimes {
561df8bae1dSRodney W. Grimes 	register int i;
56290af4afaSJohn Baldwin 	struct sigacts *ps;
563df8bae1dSRodney W. Grimes 
564628d2653SJohn Baldwin 	PROC_LOCK(p);
56590af4afaSJohn Baldwin 	ps = p->p_sigacts;
56690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5672c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5682c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
56990af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
57090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
571628d2653SJohn Baldwin 	PROC_UNLOCK(p);
572df8bae1dSRodney W. Grimes }
573df8bae1dSRodney W. Grimes 
574df8bae1dSRodney W. Grimes /*
575df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
576df8bae1dSRodney W. Grimes  */
577df8bae1dSRodney W. Grimes void
578a30ec4b9SDavid Xu execsigs(struct proc *p)
579df8bae1dSRodney W. Grimes {
580a30ec4b9SDavid Xu 	struct sigacts *ps;
581a30ec4b9SDavid Xu 	int sig;
582a30ec4b9SDavid Xu 	struct thread *td;
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 	/*
585df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5864093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
587df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
588df8bae1dSRodney W. Grimes 	 */
5899b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
590a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
591628d2653SJohn Baldwin 	ps = p->p_sigacts;
59290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
59390af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
59490af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
59590af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
5962c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5972c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
59890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
5991d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
6004093529dSJeff Roberson 			/*
6014093529dSJeff Roberson 			 * There is only one thread at this point.
6024093529dSJeff Roberson 			 */
603a30ec4b9SDavid Xu 			SIGDELSET(td->td_siglist, sig);
604df8bae1dSRodney W. Grimes 		}
6052c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
606df8bae1dSRodney W. Grimes 	}
607df8bae1dSRodney W. Grimes 	/*
608df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
609df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
610df8bae1dSRodney W. Grimes 	 */
611a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
612a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
613a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
614a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
61580e907a1SPeter Wemm 	/*
61680e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
61780e907a1SPeter Wemm 	 */
61890af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
619c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
620c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
62190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
622df8bae1dSRodney W. Grimes }
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes /*
625e77daab1SJohn Baldwin  * kern_sigprocmask()
6267c8fdcbdSMatthew Dillon  *
627628d2653SJohn Baldwin  *	Manipulate signal mask.
628df8bae1dSRodney W. Grimes  */
629e77daab1SJohn Baldwin int
630e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6314093529dSJeff Roberson 	struct thread *td;
6322c42a146SMarcel Moolenaar 	int how;
6332c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
634645682fdSLuoqi Chen 	int old;
6352c42a146SMarcel Moolenaar {
6362c42a146SMarcel Moolenaar 	int error;
6372c42a146SMarcel Moolenaar 
6384093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6392c42a146SMarcel Moolenaar 	if (oset != NULL)
6404093529dSJeff Roberson 		*oset = td->td_sigmask;
6412c42a146SMarcel Moolenaar 
6422c42a146SMarcel Moolenaar 	error = 0;
6432c42a146SMarcel Moolenaar 	if (set != NULL) {
6442c42a146SMarcel Moolenaar 		switch (how) {
6452c42a146SMarcel Moolenaar 		case SIG_BLOCK:
646645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6474093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6482c42a146SMarcel Moolenaar 			break;
6492c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6504093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6514093529dSJeff Roberson 			signotify(td);
6522c42a146SMarcel Moolenaar 			break;
6532c42a146SMarcel Moolenaar 		case SIG_SETMASK:
654645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
655645682fdSLuoqi Chen 			if (old)
6564093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
657645682fdSLuoqi Chen 			else
6584093529dSJeff Roberson 				td->td_sigmask = *set;
6594093529dSJeff Roberson 			signotify(td);
6602c42a146SMarcel Moolenaar 			break;
6612c42a146SMarcel Moolenaar 		default:
6622c42a146SMarcel Moolenaar 			error = EINVAL;
6632c42a146SMarcel Moolenaar 			break;
6642c42a146SMarcel Moolenaar 		}
6652c42a146SMarcel Moolenaar 	}
6664093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6672c42a146SMarcel Moolenaar 	return (error);
6682c42a146SMarcel Moolenaar }
6692c42a146SMarcel Moolenaar 
6707c8fdcbdSMatthew Dillon /*
671e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6727c8fdcbdSMatthew Dillon  */
6737c8fdcbdSMatthew Dillon 
674d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
675df8bae1dSRodney W. Grimes struct sigprocmask_args {
676df8bae1dSRodney W. Grimes 	int	how;
6772c42a146SMarcel Moolenaar 	const sigset_t *set;
6782c42a146SMarcel Moolenaar 	sigset_t *oset;
679df8bae1dSRodney W. Grimes };
680d2d3e875SBruce Evans #endif
68126f9a767SRodney W. Grimes int
682b40ce416SJulian Elischer sigprocmask(td, uap)
683b40ce416SJulian Elischer 	register struct thread *td;
684df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
685df8bae1dSRodney W. Grimes {
6862c42a146SMarcel Moolenaar 	sigset_t set, oset;
6872c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6882c42a146SMarcel Moolenaar 	int error;
689df8bae1dSRodney W. Grimes 
6906f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6916f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6922c42a146SMarcel Moolenaar 	if (setp) {
6936f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6942c42a146SMarcel Moolenaar 		if (error)
6952c42a146SMarcel Moolenaar 			return (error);
696df8bae1dSRodney W. Grimes 	}
697e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
6982c42a146SMarcel Moolenaar 	if (osetp && !error) {
6996f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
7002c42a146SMarcel Moolenaar 	}
7012c42a146SMarcel Moolenaar 	return (error);
7022c42a146SMarcel Moolenaar }
7032c42a146SMarcel Moolenaar 
70431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7057c8fdcbdSMatthew Dillon /*
7067c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
7077c8fdcbdSMatthew Dillon  */
7082c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7092c42a146SMarcel Moolenaar struct osigprocmask_args {
7102c42a146SMarcel Moolenaar 	int	how;
7112c42a146SMarcel Moolenaar 	osigset_t mask;
7122c42a146SMarcel Moolenaar };
7132c42a146SMarcel Moolenaar #endif
7142c42a146SMarcel Moolenaar int
715b40ce416SJulian Elischer osigprocmask(td, uap)
716b40ce416SJulian Elischer 	register struct thread *td;
7172c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7182c42a146SMarcel Moolenaar {
7192c42a146SMarcel Moolenaar 	sigset_t set, oset;
7202c42a146SMarcel Moolenaar 	int error;
7212c42a146SMarcel Moolenaar 
7222c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
723e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
724b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
725df8bae1dSRodney W. Grimes 	return (error);
726df8bae1dSRodney W. Grimes }
72731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
728df8bae1dSRodney W. Grimes 
729d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
730df8bae1dSRodney W. Grimes struct sigpending_args {
7312c42a146SMarcel Moolenaar 	sigset_t	*set;
732df8bae1dSRodney W. Grimes };
733d2d3e875SBruce Evans #endif
734fb99ab88SMatthew Dillon /*
735fb99ab88SMatthew Dillon  * MPSAFE
736fb99ab88SMatthew Dillon  */
73726f9a767SRodney W. Grimes int
738a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
739a447cd8bSJeff Roberson {
740a447cd8bSJeff Roberson 	siginfo_t info;
741a447cd8bSJeff Roberson 	sigset_t set;
742a447cd8bSJeff Roberson 	int error;
743a447cd8bSJeff Roberson 
744a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
74536939a0aSDavid Xu 	if (error) {
74636939a0aSDavid Xu 		td->td_retval[0] = error;
74736939a0aSDavid Xu 		return (0);
74836939a0aSDavid Xu 	}
749a447cd8bSJeff Roberson 
750a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
75136939a0aSDavid Xu 	if (error) {
75236939a0aSDavid Xu 		if (error == ERESTART)
753a447cd8bSJeff Roberson 			return (error);
75436939a0aSDavid Xu 		td->td_retval[0] = error;
75536939a0aSDavid Xu 		return (0);
75636939a0aSDavid Xu 	}
757a447cd8bSJeff Roberson 
758a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
759a447cd8bSJeff Roberson 	/* Repost if we got an error. */
76018440c7fSJohn Baldwin 	if (error && info.si_signo) {
76118440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
762c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
76318440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
76418440c7fSJohn Baldwin 	}
76536939a0aSDavid Xu 	td->td_retval[0] = error;
76636939a0aSDavid Xu 	return (0);
767a447cd8bSJeff Roberson }
768a447cd8bSJeff Roberson /*
769a447cd8bSJeff Roberson  * MPSAFE
770a447cd8bSJeff Roberson  */
771a447cd8bSJeff Roberson int
772a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
773a447cd8bSJeff Roberson {
774a447cd8bSJeff Roberson 	struct timespec ts;
775a447cd8bSJeff Roberson 	struct timespec *timeout;
776a447cd8bSJeff Roberson 	sigset_t set;
777a447cd8bSJeff Roberson 	siginfo_t info;
778a447cd8bSJeff Roberson 	int error;
779a447cd8bSJeff Roberson 
780a447cd8bSJeff Roberson 	if (uap->timeout) {
781a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
782a447cd8bSJeff Roberson 		if (error)
783a447cd8bSJeff Roberson 			return (error);
784a447cd8bSJeff Roberson 
785a447cd8bSJeff Roberson 		timeout = &ts;
786a447cd8bSJeff Roberson 	} else
787a447cd8bSJeff Roberson 		timeout = NULL;
788a447cd8bSJeff Roberson 
789a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
790a447cd8bSJeff Roberson 	if (error)
791a447cd8bSJeff Roberson 		return (error);
792a447cd8bSJeff Roberson 
793a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
794a447cd8bSJeff Roberson 	if (error)
795a447cd8bSJeff Roberson 		return (error);
7969dde3bc9SDavid Xu 
797418228dfSDavid Xu 	if (uap->info)
798a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
799a447cd8bSJeff Roberson 	/* Repost if we got an error. */
80018440c7fSJohn Baldwin 	if (error && info.si_signo) {
80118440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
802c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
80318440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
804418228dfSDavid Xu 	} else {
805418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
80618440c7fSJohn Baldwin 	}
807a447cd8bSJeff Roberson 	return (error);
808a447cd8bSJeff Roberson }
809a447cd8bSJeff Roberson 
810a447cd8bSJeff Roberson /*
811a447cd8bSJeff Roberson  * MPSAFE
812a447cd8bSJeff Roberson  */
813a447cd8bSJeff Roberson int
814a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
815a447cd8bSJeff Roberson {
816a447cd8bSJeff Roberson 	siginfo_t info;
817a447cd8bSJeff Roberson 	sigset_t set;
818a447cd8bSJeff Roberson 	int error;
819a447cd8bSJeff Roberson 
820a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
821a447cd8bSJeff Roberson 	if (error)
822a447cd8bSJeff Roberson 		return (error);
823a447cd8bSJeff Roberson 
824a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
825a447cd8bSJeff Roberson 	if (error)
826a447cd8bSJeff Roberson 		return (error);
827a447cd8bSJeff Roberson 
828418228dfSDavid Xu 	if (uap->info)
829a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
830a447cd8bSJeff Roberson 	/* Repost if we got an error. */
83118440c7fSJohn Baldwin 	if (error && info.si_signo) {
83218440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
833c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
83418440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
835418228dfSDavid Xu 	} else {
836418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
83718440c7fSJohn Baldwin 	}
838a447cd8bSJeff Roberson 	return (error);
839a447cd8bSJeff Roberson }
840a447cd8bSJeff Roberson 
841a447cd8bSJeff Roberson static int
8423074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
843a447cd8bSJeff Roberson     struct timespec *timeout)
844a447cd8bSJeff Roberson {
8453074d1b4SDavid Xu 	struct sigacts *ps;
8463074d1b4SDavid Xu 	sigset_t savedmask, sigset;
847a447cd8bSJeff Roberson 	struct proc *p;
848a447cd8bSJeff Roberson 	int error;
849a447cd8bSJeff Roberson 	int sig;
850a447cd8bSJeff Roberson 	int hz;
8513074d1b4SDavid Xu 	int i;
852a447cd8bSJeff Roberson 
853a447cd8bSJeff Roberson 	p = td->td_proc;
854a447cd8bSJeff Roberson 	error = 0;
855a447cd8bSJeff Roberson 	sig = 0;
8563074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
857a447cd8bSJeff Roberson 
858a447cd8bSJeff Roberson 	PROC_LOCK(p);
859a447cd8bSJeff Roberson 	ps = p->p_sigacts;
8603074d1b4SDavid Xu 	savedmask = td->td_sigmask;
8613074d1b4SDavid Xu 
8623074d1b4SDavid Xu again:
8633074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
8643074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
8653074d1b4SDavid Xu 			continue;
8663074d1b4SDavid Xu 		if (SIGISMEMBER(td->td_siglist, i)) {
867418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
868418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8693074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
87090af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
871a447cd8bSJeff Roberson 			sig = cursig(td);
8723074d1b4SDavid Xu 			i = 0;
8733074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8743074d1b4SDavid Xu 		} else if (SIGISMEMBER(p->p_siglist, i)) {
8753074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
8763074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
8773074d1b4SDavid Xu 				wakeup(&p->p_siglist);
8783074d1b4SDavid Xu 			}
8793074d1b4SDavid Xu 			SIGDELSET(p->p_siglist, i);
8803074d1b4SDavid Xu 			SIGADDSET(td->td_siglist, i);
8813074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
8823074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8833074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
8843074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
8853074d1b4SDavid Xu 			sig = cursig(td);
8863074d1b4SDavid Xu 			i = 0;
8873074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8883074d1b4SDavid Xu 		}
8893074d1b4SDavid Xu 		if (sig) {
8903074d1b4SDavid Xu 			td->td_sigmask = savedmask;
8913074d1b4SDavid Xu 			signotify(td);
892a447cd8bSJeff Roberson 			goto out;
8933074d1b4SDavid Xu 		}
8943074d1b4SDavid Xu 	}
8953074d1b4SDavid Xu 	if (error)
8963074d1b4SDavid Xu 		goto out;
8973074d1b4SDavid Xu 
8983074d1b4SDavid Xu 	td->td_sigmask = savedmask;
8993074d1b4SDavid Xu 	signotify(td);
9003074d1b4SDavid Xu 	sigset = td->td_siglist;
9013074d1b4SDavid Xu 	SIGSETOR(sigset, p->p_siglist);
9023074d1b4SDavid Xu 	SIGSETAND(sigset, waitset);
9033074d1b4SDavid Xu 	if (!SIGISEMPTY(sigset))
9043074d1b4SDavid Xu 		goto again;
905a447cd8bSJeff Roberson 
906a447cd8bSJeff Roberson 	/*
907a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
908a447cd8bSJeff Roberson 	 * signals.
909a447cd8bSJeff Roberson 	 */
910a447cd8bSJeff Roberson 	if (timeout) {
911a447cd8bSJeff Roberson 		struct timeval tv;
912a447cd8bSJeff Roberson 
913a6ca4808SMike Makonnen 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
914a447cd8bSJeff Roberson 			error = EINVAL;
915a447cd8bSJeff Roberson 			goto out;
916a447cd8bSJeff Roberson 		}
9173074d1b4SDavid Xu 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
9183074d1b4SDavid Xu 			error = EAGAIN;
9193074d1b4SDavid Xu 			goto out;
9203074d1b4SDavid Xu 		}
921a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
922a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
923a447cd8bSJeff Roberson 	} else
924a447cd8bSJeff Roberson 		hz = 0;
925a447cd8bSJeff Roberson 
9263074d1b4SDavid Xu 	td->td_waitset = &waitset;
92786b5e563SDag-Erling Smørgrav 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
9283074d1b4SDavid Xu 	td->td_waitset = NULL;
9293074d1b4SDavid Xu 	if (error == 0) /* surplus wakeup ? */
9303074d1b4SDavid Xu 		error = EINTR;
9313074d1b4SDavid Xu 	goto again;
9329dde3bc9SDavid Xu 
933a447cd8bSJeff Roberson out:
934a447cd8bSJeff Roberson 	if (sig) {
935a447cd8bSJeff Roberson 		sig_t action;
936a447cd8bSJeff Roberson 
9373074d1b4SDavid Xu 		error = 0;
9383074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
939a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
94090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
941a447cd8bSJeff Roberson #ifdef KTRACE
942a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
9433074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
944a447cd8bSJeff Roberson #endif
945a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
946a447cd8bSJeff Roberson 
947a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
948a447cd8bSJeff Roberson 		info->si_signo = sig;
949a447cd8bSJeff Roberson 		info->si_code = 0;
9503074d1b4SDavid Xu 	}
951a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
952a447cd8bSJeff Roberson 	return (error);
953a447cd8bSJeff Roberson }
954a447cd8bSJeff Roberson 
955a447cd8bSJeff Roberson /*
956a447cd8bSJeff Roberson  * MPSAFE
957a447cd8bSJeff Roberson  */
958a447cd8bSJeff Roberson int
959b40ce416SJulian Elischer sigpending(td, uap)
960b40ce416SJulian Elischer 	struct thread *td;
961df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
962df8bae1dSRodney W. Grimes {
963b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
964628d2653SJohn Baldwin 	sigset_t siglist;
965df8bae1dSRodney W. Grimes 
966628d2653SJohn Baldwin 	PROC_LOCK(p);
9671d9c5696SJuli Mallett 	siglist = p->p_siglist;
9684093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
969628d2653SJohn Baldwin 	PROC_UNLOCK(p);
97048e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9712c42a146SMarcel Moolenaar }
9722c42a146SMarcel Moolenaar 
97331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9742c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9752c42a146SMarcel Moolenaar struct osigpending_args {
9762c42a146SMarcel Moolenaar 	int	dummy;
9772c42a146SMarcel Moolenaar };
9782c42a146SMarcel Moolenaar #endif
979fb99ab88SMatthew Dillon /*
980fb99ab88SMatthew Dillon  * MPSAFE
981fb99ab88SMatthew Dillon  */
9822c42a146SMarcel Moolenaar int
983b40ce416SJulian Elischer osigpending(td, uap)
984b40ce416SJulian Elischer 	struct thread *td;
9852c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9862c42a146SMarcel Moolenaar {
987b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9884093529dSJeff Roberson 	sigset_t siglist;
989b40ce416SJulian Elischer 
990628d2653SJohn Baldwin 	PROC_LOCK(p);
9914093529dSJeff Roberson 	siglist = p->p_siglist;
9924093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
993628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9949d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
995df8bae1dSRodney W. Grimes 	return (0);
996df8bae1dSRodney W. Grimes }
99731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
998df8bae1dSRodney W. Grimes 
9991930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1000df8bae1dSRodney W. Grimes /*
1001df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1002df8bae1dSRodney W. Grimes  */
1003d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1004df8bae1dSRodney W. Grimes struct osigvec_args {
1005df8bae1dSRodney W. Grimes 	int	signum;
1006df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1007df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1008df8bae1dSRodney W. Grimes };
1009d2d3e875SBruce Evans #endif
1010fb99ab88SMatthew Dillon /*
1011fb99ab88SMatthew Dillon  * MPSAFE
1012fb99ab88SMatthew Dillon  */
1013df8bae1dSRodney W. Grimes /* ARGSUSED */
101426f9a767SRodney W. Grimes int
1015b40ce416SJulian Elischer osigvec(td, uap)
1016b40ce416SJulian Elischer 	struct thread *td;
1017df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1018df8bae1dSRodney W. Grimes {
1019df8bae1dSRodney W. Grimes 	struct sigvec vec;
10202c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
10212c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
10222c42a146SMarcel Moolenaar 	int error;
1023df8bae1dSRodney W. Grimes 
10246f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10256f841fb7SMarcel Moolenaar 		return (EINVAL);
10266f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
10276f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
10282c42a146SMarcel Moolenaar 	if (nsap) {
10296f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
10302c42a146SMarcel Moolenaar 		if (error)
1031df8bae1dSRodney W. Grimes 			return (error);
10322c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
10332c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
10342c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
10352c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1036df8bae1dSRodney W. Grimes 	}
10375edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10382c42a146SMarcel Moolenaar 	if (osap && !error) {
10392c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
10402c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
10412c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
10422c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
10432c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
10446f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10452c42a146SMarcel Moolenaar 	}
10462c42a146SMarcel Moolenaar 	return (error);
1047df8bae1dSRodney W. Grimes }
1048df8bae1dSRodney W. Grimes 
1049d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1050df8bae1dSRodney W. Grimes struct osigblock_args {
1051df8bae1dSRodney W. Grimes 	int	mask;
1052df8bae1dSRodney W. Grimes };
1053d2d3e875SBruce Evans #endif
1054fb99ab88SMatthew Dillon /*
1055fb99ab88SMatthew Dillon  * MPSAFE
1056fb99ab88SMatthew Dillon  */
105726f9a767SRodney W. Grimes int
1058b40ce416SJulian Elischer osigblock(td, uap)
1059b40ce416SJulian Elischer 	register struct thread *td;
1060df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1061df8bae1dSRodney W. Grimes {
1062b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10632c42a146SMarcel Moolenaar 	sigset_t set;
1064df8bae1dSRodney W. Grimes 
10652c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10662c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1067628d2653SJohn Baldwin 	PROC_LOCK(p);
10684093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10694093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1070628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1071df8bae1dSRodney W. Grimes 	return (0);
1072df8bae1dSRodney W. Grimes }
1073df8bae1dSRodney W. Grimes 
1074d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1075df8bae1dSRodney W. Grimes struct osigsetmask_args {
1076df8bae1dSRodney W. Grimes 	int	mask;
1077df8bae1dSRodney W. Grimes };
1078d2d3e875SBruce Evans #endif
1079fb99ab88SMatthew Dillon /*
1080fb99ab88SMatthew Dillon  * MPSAFE
1081fb99ab88SMatthew Dillon  */
108226f9a767SRodney W. Grimes int
1083b40ce416SJulian Elischer osigsetmask(td, uap)
1084b40ce416SJulian Elischer 	struct thread *td;
1085df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1086df8bae1dSRodney W. Grimes {
1087b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10882c42a146SMarcel Moolenaar 	sigset_t set;
1089df8bae1dSRodney W. Grimes 
10902c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10912c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1092628d2653SJohn Baldwin 	PROC_LOCK(p);
10934093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10944093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10954093529dSJeff Roberson 	signotify(td);
1096628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1097df8bae1dSRodney W. Grimes 	return (0);
1098df8bae1dSRodney W. Grimes }
10991930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1100df8bae1dSRodney W. Grimes 
1101df8bae1dSRodney W. Grimes /*
1102df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
110397563428SAlexander Kabaev  * in the meantime.
1104b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1105b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1106b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1107df8bae1dSRodney W. Grimes  */
1108d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1109df8bae1dSRodney W. Grimes struct sigsuspend_args {
11102c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1111df8bae1dSRodney W. Grimes };
1112d2d3e875SBruce Evans #endif
1113fb99ab88SMatthew Dillon /*
1114fb99ab88SMatthew Dillon  * MPSAFE
1115fb99ab88SMatthew Dillon  */
1116df8bae1dSRodney W. Grimes /* ARGSUSED */
111726f9a767SRodney W. Grimes int
1118b40ce416SJulian Elischer sigsuspend(td, uap)
1119b40ce416SJulian Elischer 	struct thread *td;
1120df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1121df8bae1dSRodney W. Grimes {
11222c42a146SMarcel Moolenaar 	sigset_t mask;
11232c42a146SMarcel Moolenaar 	int error;
11242c42a146SMarcel Moolenaar 
11256f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11262c42a146SMarcel Moolenaar 	if (error)
11272c42a146SMarcel Moolenaar 		return (error);
11288f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11298f19eb88SIan Dowse }
11308f19eb88SIan Dowse 
11318f19eb88SIan Dowse int
11328f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11338f19eb88SIan Dowse {
11348f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes 	/*
1137645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1138df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1139df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1140df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1141df8bae1dSRodney W. Grimes 	 * to indicate this.
1142df8bae1dSRodney W. Grimes 	 */
1143628d2653SJohn Baldwin 	PROC_LOCK(p);
11444093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11455e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1146645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11474093529dSJeff Roberson 	td->td_sigmask = mask;
11484093529dSJeff Roberson 	signotify(td);
114986b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11502c42a146SMarcel Moolenaar 		/* void */;
1151628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11522c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11532c42a146SMarcel Moolenaar 	return (EINTR);
11542c42a146SMarcel Moolenaar }
11552c42a146SMarcel Moolenaar 
115631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
115797563428SAlexander Kabaev /*
115897563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
115997563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
116097563428SAlexander Kabaev  */
11612c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11622c42a146SMarcel Moolenaar struct osigsuspend_args {
11632c42a146SMarcel Moolenaar 	osigset_t mask;
11642c42a146SMarcel Moolenaar };
11652c42a146SMarcel Moolenaar #endif
1166fb99ab88SMatthew Dillon /*
1167fb99ab88SMatthew Dillon  * MPSAFE
1168fb99ab88SMatthew Dillon  */
11692c42a146SMarcel Moolenaar /* ARGSUSED */
11702c42a146SMarcel Moolenaar int
1171b40ce416SJulian Elischer osigsuspend(td, uap)
1172b40ce416SJulian Elischer 	struct thread *td;
11732c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11742c42a146SMarcel Moolenaar {
1175b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1176645682fdSLuoqi Chen 	sigset_t mask;
11772c42a146SMarcel Moolenaar 
1178628d2653SJohn Baldwin 	PROC_LOCK(p);
11794093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11805e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1181645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1182645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11834093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11844093529dSJeff Roberson 	signotify(td);
118586b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1186df8bae1dSRodney W. Grimes 		/* void */;
1187628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1188df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1189df8bae1dSRodney W. Grimes 	return (EINTR);
1190df8bae1dSRodney W. Grimes }
119131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1192df8bae1dSRodney W. Grimes 
11931930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1194d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1195df8bae1dSRodney W. Grimes struct osigstack_args {
1196df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1197df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1198df8bae1dSRodney W. Grimes };
1199d2d3e875SBruce Evans #endif
1200fb99ab88SMatthew Dillon /*
1201fb99ab88SMatthew Dillon  * MPSAFE
1202fb99ab88SMatthew Dillon  */
1203df8bae1dSRodney W. Grimes /* ARGSUSED */
120426f9a767SRodney W. Grimes int
1205b40ce416SJulian Elischer osigstack(td, uap)
1206b40ce416SJulian Elischer 	struct thread *td;
1207df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1208df8bae1dSRodney W. Grimes {
12095afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1210fb99ab88SMatthew Dillon 	int error = 0;
1211fb99ab88SMatthew Dillon 
1212d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12135afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12145afe0c99SJohn Baldwin 		if (error)
12155afe0c99SJohn Baldwin 			return (error);
1216df8bae1dSRodney W. Grimes 	}
1217a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12185afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12195afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1220a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1221a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1222a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1223a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12245afe0c99SJohn Baldwin 	}
12255afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12265afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12275afe0c99SJohn Baldwin 
1228fb99ab88SMatthew Dillon 	return (error);
1229df8bae1dSRodney W. Grimes }
12301930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1231df8bae1dSRodney W. Grimes 
1232d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1233df8bae1dSRodney W. Grimes struct sigaltstack_args {
12342c42a146SMarcel Moolenaar 	stack_t	*ss;
12352c42a146SMarcel Moolenaar 	stack_t	*oss;
1236df8bae1dSRodney W. Grimes };
1237d2d3e875SBruce Evans #endif
1238fb99ab88SMatthew Dillon /*
1239fb99ab88SMatthew Dillon  * MPSAFE
1240fb99ab88SMatthew Dillon  */
1241df8bae1dSRodney W. Grimes /* ARGSUSED */
124226f9a767SRodney W. Grimes int
1243b40ce416SJulian Elischer sigaltstack(td, uap)
1244b40ce416SJulian Elischer 	struct thread *td;
1245df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1246df8bae1dSRodney W. Grimes {
12478f19eb88SIan Dowse 	stack_t ss, oss;
12488f19eb88SIan Dowse 	int error;
12498f19eb88SIan Dowse 
12508f19eb88SIan Dowse 	if (uap->ss != NULL) {
12518f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12528f19eb88SIan Dowse 		if (error)
12538f19eb88SIan Dowse 			return (error);
12548f19eb88SIan Dowse 	}
12558f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12568f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12578f19eb88SIan Dowse 	if (error)
12588f19eb88SIan Dowse 		return (error);
12598f19eb88SIan Dowse 	if (uap->oss != NULL)
12608f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12618f19eb88SIan Dowse 	return (error);
12628f19eb88SIan Dowse }
12638f19eb88SIan Dowse 
12648f19eb88SIan Dowse int
12658f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12668f19eb88SIan Dowse {
1267b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1268fb99ab88SMatthew Dillon 	int oonstack;
1269fb99ab88SMatthew Dillon 
1270b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1271d034d459SMarcel Moolenaar 
12728f19eb88SIan Dowse 	if (oss != NULL) {
1273a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1274a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1275d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1276df8bae1dSRodney W. Grimes 	}
1277d034d459SMarcel Moolenaar 
12788f19eb88SIan Dowse 	if (ss != NULL) {
1279a30ec4b9SDavid Xu 		if (oonstack)
1280cf60731bSJohn Baldwin 			return (EPERM);
1281a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1282cf60731bSJohn Baldwin 			return (EINVAL);
12838f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12848f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1285cf60731bSJohn Baldwin 				return (ENOMEM);
1286fb99ab88SMatthew Dillon 			}
1287a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1288a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1289628d2653SJohn Baldwin 		} else {
1290a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1291628d2653SJohn Baldwin 		}
1292d034d459SMarcel Moolenaar 	}
1293cf60731bSJohn Baldwin 	return (0);
1294df8bae1dSRodney W. Grimes }
1295df8bae1dSRodney W. Grimes 
1296d93f860cSPoul-Henning Kamp /*
1297d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1298d93f860cSPoul-Henning Kamp  * cp is calling process.
1299d93f860cSPoul-Henning Kamp  */
130037c84183SPoul-Henning Kamp static int
13019c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
13029c1ab3e0SJohn Baldwin 	register struct thread *td;
13032c42a146SMarcel Moolenaar 	int sig, pgid, all;
1304d93f860cSPoul-Henning Kamp {
1305d93f860cSPoul-Henning Kamp 	register struct proc *p;
1306d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1307d93f860cSPoul-Henning Kamp 	int nfound = 0;
1308d93f860cSPoul-Henning Kamp 
1309553629ebSJake Burkholder 	if (all) {
1310d93f860cSPoul-Henning Kamp 		/*
1311d93f860cSPoul-Henning Kamp 		 * broadcast
1312d93f860cSPoul-Henning Kamp 		 */
13131005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13142e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1315628d2653SJohn Baldwin 			PROC_LOCK(p);
13169c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13179c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1318628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1319628d2653SJohn Baldwin 				continue;
1320628d2653SJohn Baldwin 			}
1321f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1322d93f860cSPoul-Henning Kamp 				nfound++;
132333a9ed9dSJohn Baldwin 				if (sig)
13242c42a146SMarcel Moolenaar 					psignal(p, sig);
1325628d2653SJohn Baldwin 			}
132633a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1327d93f860cSPoul-Henning Kamp 		}
13281005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1329553629ebSJake Burkholder 	} else {
1330ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1331f591779bSSeigo Tanimura 		if (pgid == 0) {
1332d93f860cSPoul-Henning Kamp 			/*
1333d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1334d93f860cSPoul-Henning Kamp 			 */
13359c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1336f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1337f591779bSSeigo Tanimura 		} else {
1338d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1339f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1340ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1341d93f860cSPoul-Henning Kamp 				return (ESRCH);
1342d93f860cSPoul-Henning Kamp 			}
1343f591779bSSeigo Tanimura 		}
1344ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13452e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1346628d2653SJohn Baldwin 			PROC_LOCK(p);
1347628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1348628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1349628d2653SJohn Baldwin 				continue;
1350628d2653SJohn Baldwin 			}
1351f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1352d93f860cSPoul-Henning Kamp 				nfound++;
135333a9ed9dSJohn Baldwin 				if (sig)
13542c42a146SMarcel Moolenaar 					psignal(p, sig);
1355628d2653SJohn Baldwin 			}
135633a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1357d93f860cSPoul-Henning Kamp 		}
1358f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1359d93f860cSPoul-Henning Kamp 	}
1360d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1361d93f860cSPoul-Henning Kamp }
1362d93f860cSPoul-Henning Kamp 
1363d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1364df8bae1dSRodney W. Grimes struct kill_args {
1365df8bae1dSRodney W. Grimes 	int	pid;
1366df8bae1dSRodney W. Grimes 	int	signum;
1367df8bae1dSRodney W. Grimes };
1368d2d3e875SBruce Evans #endif
1369fb99ab88SMatthew Dillon /*
1370fb99ab88SMatthew Dillon  * MPSAFE
1371fb99ab88SMatthew Dillon  */
1372df8bae1dSRodney W. Grimes /* ARGSUSED */
137326f9a767SRodney W. Grimes int
1374b40ce416SJulian Elischer kill(td, uap)
1375b40ce416SJulian Elischer 	register struct thread *td;
1376df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1377df8bae1dSRodney W. Grimes {
1378df8bae1dSRodney W. Grimes 	register struct proc *p;
137990af4afaSJohn Baldwin 	int error;
1380df8bae1dSRodney W. Grimes 
13816c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1382df8bae1dSRodney W. Grimes 		return (EINVAL);
1383fb99ab88SMatthew Dillon 
1384df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1385df8bae1dSRodney W. Grimes 		/* kill single process */
13860b011ea3SPawel Jakub Dawidek 		if ((p = pfind(uap->pid)) == NULL) {
138724b2151fSPawel Jakub Dawidek 			if ((p = zpfind(uap->pid)) == NULL)
138890af4afaSJohn Baldwin 				return (ESRCH);
13890b011ea3SPawel Jakub Dawidek 		}
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)
14905995adc2SJulian Elischer 			thread_user_enter(td);
1491628d2653SJohn Baldwin 		PROC_LOCK(p);
14929dde3bc9SDavid Xu 		SIGDELSET(td->td_sigmask, sig);
14939dde3bc9SDavid Xu 		mtx_lock_spin(&sched_lock);
14949dde3bc9SDavid Xu 		/*
14959dde3bc9SDavid Xu 		 * Force scheduling an upcall, so UTS has chance to
14969dde3bc9SDavid Xu 		 * process the signal before thread runs again in
14979dde3bc9SDavid Xu 		 * userland.
14989dde3bc9SDavid Xu 		 */
14999dde3bc9SDavid Xu 		if (td->td_upcall)
15009dde3bc9SDavid Xu 			td->td_upcall->ku_flags |= KUF_DOUPCALL;
15019dde3bc9SDavid Xu 		mtx_unlock_spin(&sched_lock);
1502432b45deSDavid Xu 	} else {
15039dde3bc9SDavid Xu 		PROC_LOCK(p);
15049dde3bc9SDavid Xu 	}
1505ef3dab76STim J. Robbins 	ps = p->p_sigacts;
150690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
150790af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15084093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1509df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1510df8bae1dSRodney W. Grimes #ifdef KTRACE
1511374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1512374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15134093529dSJeff Roberson 			    &td->td_sigmask, code);
1514df8bae1dSRodney W. Grimes #endif
1515aa0aa7a1STim J. Robbins 		if (!(td->td_pflags & TDP_SA))
15169dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15179dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15184093529dSJeff Roberson 				&td->td_sigmask, code);
1519cbf4e354SDavid Xu 		else if (td->td_mailbox == NULL) {
1520cbf4e354SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1521cbf4e354SDavid Xu 			/* UTS caused a sync signal */
1522cbf4e354SDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
1523cbf4e354SDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
1524cbf4e354SDavid Xu 			sigexit(td, sig);
1525cbf4e354SDavid Xu 		} else {
15264b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15279dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1528cbf4e354SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
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)
1535cbf4e354SDavid Xu 				sigexit(td, SIGSEGV);
15369dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15379dde3bc9SDavid Xu 		}
15384093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15392c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15404093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15412c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1542289ccde0SPeter Wemm 			/*
15438f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1544289ccde0SPeter Wemm 			 */
154590af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15462c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15472c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
154890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15492c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1550dedc04feSPeter Wemm 		}
155190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15526f841fb7SMarcel Moolenaar 	} else {
155390af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15546626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15552c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1556c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1557df8bae1dSRodney W. Grimes 	}
1558628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1559df8bae1dSRodney W. Grimes }
1560df8bae1dSRodney W. Grimes 
15614093529dSJeff Roberson static struct thread *
15624093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15634093529dSJeff Roberson {
15643074d1b4SDavid Xu 	struct thread *td, *signal_td;
15654093529dSJeff Roberson 
15664093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15674093529dSJeff Roberson 
15684093529dSJeff Roberson 	/*
15693074d1b4SDavid Xu 	 * First find a thread in sigwait state and signal belongs to
15703074d1b4SDavid Xu 	 * its wait set. POSIX's arguments is that speed of delivering signal
15713074d1b4SDavid Xu 	 * to sigwait thread is faster than delivering signal to user stack.
15723074d1b4SDavid Xu 	 * If we can not find sigwait thread, then find the first thread in
15733074d1b4SDavid Xu 	 * the proc that doesn't have this signal masked, an exception is
15743074d1b4SDavid Xu 	 * if current thread is sending signal to its process, and it does not
15753074d1b4SDavid Xu 	 * mask the signal, it should get the signal, this is another fast
15763074d1b4SDavid Xu 	 * way to deliver signal.
15774093529dSJeff Roberson 	 */
15783074d1b4SDavid Xu 	signal_td = NULL;
1579a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15803074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15813074d1b4SDavid Xu 		if (td->td_waitset != NULL &&
1582a9a48d68SDavid Xu 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1583a9a48d68SDavid Xu 				mtx_unlock_spin(&sched_lock);
15844093529dSJeff Roberson 				return (td);
1585a9a48d68SDavid Xu 		}
15863074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15873074d1b4SDavid Xu 			if (td == curthread)
15883074d1b4SDavid Xu 				signal_td = curthread;
15893074d1b4SDavid Xu 			else if (signal_td == NULL)
15903074d1b4SDavid Xu 				signal_td = td;
15913074d1b4SDavid Xu 		}
15923074d1b4SDavid Xu 	}
15933074d1b4SDavid Xu 	if (signal_td == NULL)
15943074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1595a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
15963074d1b4SDavid Xu 	return (signal_td);
15974093529dSJeff Roberson }
15984093529dSJeff Roberson 
1599df8bae1dSRodney W. Grimes /*
1600df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1601df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1602df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1603df8bae1dSRodney W. Grimes  *
1604df8bae1dSRodney W. Grimes  * Exceptions:
1605df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1606df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1607df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1608df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1609df8bae1dSRodney W. Grimes  *
1610df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
161190af4afaSJohn Baldwin  *
161290af4afaSJohn Baldwin  * MPSAFE
1613df8bae1dSRodney W. Grimes  */
1614df8bae1dSRodney W. Grimes void
16154093529dSJeff Roberson psignal(struct proc *p, int sig)
1616df8bae1dSRodney W. Grimes {
1617b40ce416SJulian Elischer 	struct thread *td;
16184093529dSJeff Roberson 	int prop;
16194093529dSJeff Roberson 
162041b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
162141b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
162241b3077aSJacques Vidrine 
16234093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
162424b2151fSPawel Jakub Dawidek 	/*
162524b2151fSPawel Jakub Dawidek 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
162624b2151fSPawel Jakub Dawidek 	 */
162724b2151fSPawel Jakub Dawidek 	if (p->p_state == PRS_ZOMBIE)
162824b2151fSPawel Jakub Dawidek 		return;
16294093529dSJeff Roberson 	prop = sigprop(sig);
16304093529dSJeff Roberson 
16314093529dSJeff Roberson 	/*
16324093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16334093529dSJeff Roberson 	 */
16344093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16354093529dSJeff Roberson 
1636c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16374093529dSJeff Roberson }
16384093529dSJeff Roberson 
163990af4afaSJohn Baldwin /*
164090af4afaSJohn Baldwin  * MPSAFE
164190af4afaSJohn Baldwin  */
16424093529dSJeff Roberson void
1643c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16444093529dSJeff Roberson {
16459dde3bc9SDavid Xu 	sigset_t saved;
16469dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16479dde3bc9SDavid Xu 
16489dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16499dde3bc9SDavid Xu 		saved = p->p_siglist;
1650c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16519dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16526dbc0850SJohn Baldwin 		if (!SIGSETEQ(saved, p->p_siglist)) {
16539dde3bc9SDavid Xu 			/* pending set changed */
16549dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
16559dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
16569dde3bc9SDavid Xu 		}
16579dde3bc9SDavid Xu 	}
16589dde3bc9SDavid Xu }
16599dde3bc9SDavid Xu 
16609dde3bc9SDavid Xu static void
1661c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target)
16629dde3bc9SDavid Xu {
16634093529dSJeff Roberson 	struct proc *p;
16644093529dSJeff Roberson 	register sig_t action;
16654093529dSJeff Roberson 	sigset_t *siglist;
16664093529dSJeff Roberson 	struct thread *td0;
1667e602ba25SJulian Elischer 	register int prop;
166890af4afaSJohn Baldwin 	struct sigacts *ps;
1669df8bae1dSRodney W. Grimes 
167041b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
167141b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
16724093529dSJeff Roberson 
16734093529dSJeff Roberson 	p = td->td_proc;
167490af4afaSJohn Baldwin 	ps = p->p_sigacts;
16752c42a146SMarcel Moolenaar 
1676628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1677ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
1678cb679c38SJonathan Lemon 
16792c42a146SMarcel Moolenaar 	prop = sigprop(sig);
16804093529dSJeff Roberson 
16814093529dSJeff Roberson 	/*
168214b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
168314b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
168414b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
16854093529dSJeff Roberson 	 */
16863074d1b4SDavid Xu 	if (target == SIGTARGET_TD) {
16873074d1b4SDavid Xu 		siglist = &td->td_siglist;
16883074d1b4SDavid Xu 	} else {
16893074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig))
16903074d1b4SDavid Xu 			siglist = &td->td_siglist;
16913074d1b4SDavid Xu 		else if (td->td_waitset != NULL &&
16923074d1b4SDavid Xu 			SIGISMEMBER(*(td->td_waitset), sig))
16933074d1b4SDavid Xu 			siglist = &td->td_siglist;
16943074d1b4SDavid Xu 		else
16953074d1b4SDavid Xu 			siglist = &p->p_siglist;
16963074d1b4SDavid Xu 	}
16974093529dSJeff Roberson 
1698df8bae1dSRodney W. Grimes 	/*
16992a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
17002a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
17012a024a2bSSean Eric Fagan 	 * a chance, as well.
1702df8bae1dSRodney W. Grimes 	 */
1703b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1704df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1705b40ce416SJulian Elischer 	} else {
1706df8bae1dSRodney W. Grimes 		/*
1707df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1708df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
170990af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1710df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1711df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1712df8bae1dSRodney W. Grimes 		 */
171390af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
171490af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
171590af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
171690af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1717df8bae1dSRodney W. Grimes 			return;
171890af4afaSJohn Baldwin 		}
17193074d1b4SDavid Xu 		if (((td->td_waitset == NULL) &&
17203074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig)) ||
17213074d1b4SDavid Xu 		    ((td->td_waitset != NULL) &&
17223074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig) &&
17233074d1b4SDavid Xu 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1724df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
172590af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1726df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1727df8bae1dSRodney W. Grimes 		else
1728df8bae1dSRodney W. Grimes 			action = SIG_DFL;
172990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1730df8bae1dSRodney W. Grimes 	}
1731df8bae1dSRodney W. Grimes 
17324093529dSJeff Roberson 	if (prop & SA_CONT) {
17331d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17344093529dSJeff Roberson 		/*
17354093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17364093529dSJeff Roberson 		 * the proc's siglist.
17374093529dSJeff Roberson 		 */
1738a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17394093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17404093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1741a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17424093529dSJeff Roberson 	}
1743df8bae1dSRodney W. Grimes 
1744df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1745df8bae1dSRodney W. Grimes 		/*
1746df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1747df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1748df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1749df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1750df8bae1dSRodney W. Grimes 		 */
1751e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1752e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1753e602ba25SJulian Elischer 		    (action == SIG_DFL))
1754df8bae1dSRodney W. Grimes 		        return;
17551d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1756a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17574093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17584093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1759a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17606933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1761df8bae1dSRodney W. Grimes 	}
17623074d1b4SDavid Xu 
17634093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17644093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17653074d1b4SDavid Xu 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
17663074d1b4SDavid Xu 	    action != SIG_HOLD) {
17673074d1b4SDavid Xu 		td->td_waitset = NULL;
17683074d1b4SDavid Xu 	}
17693074d1b4SDavid Xu 
17705312b1c7SDavid Xu 	/*
17715312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17725312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17735312b1c7SDavid Xu 	 */
17745312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17755312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17765312b1c7SDavid Xu 		return;
1777df8bae1dSRodney W. Grimes 	/*
177850434413SAlfred Perlstein 	 * SIGKILL: Remove procfs STOPEVENTs.
177950434413SAlfred Perlstein 	 */
178050434413SAlfred Perlstein 	if (sig == SIGKILL) {
178150434413SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCBIC */
178250434413SAlfred Perlstein 		p->p_stops = 0;
178350434413SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCCONT */
178450434413SAlfred Perlstein 		p->p_step = 0;
178550434413SAlfred Perlstein 		wakeup(&p->p_step);
178650434413SAlfred Perlstein 	}
178750434413SAlfred Perlstein 	/*
1788e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1789e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1790e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1791e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1792e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1793e602ba25SJulian Elischer 	 * We try do the per-process part here.
1794df8bae1dSRodney W. Grimes 	 */
1795e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1796e602ba25SJulian Elischer 		/*
1797e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1798e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1799e602ba25SJulian Elischer 		 */
1800e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1801e602ba25SJulian Elischer 			/*
1802e602ba25SJulian Elischer 			 * The traced process is already stopped,
1803e602ba25SJulian Elischer 			 * so no further action is necessary.
1804e602ba25SJulian Elischer 			 * No signal can restart us.
1805e602ba25SJulian Elischer 			 */
1806e602ba25SJulian Elischer 			goto out;
18071c32c37cSJohn Baldwin 		}
1808b40ce416SJulian Elischer 
1809e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1810df8bae1dSRodney W. Grimes 			/*
1811e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1812e602ba25SJulian Elischer 			 * It will die elsewhere.
1813e602ba25SJulian Elischer 			 * All threads must be restarted.
1814df8bae1dSRodney W. Grimes 			 */
1815482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
1816e602ba25SJulian Elischer 			goto runfast;
1817e602ba25SJulian Elischer 		}
1818e602ba25SJulian Elischer 
1819e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1820e602ba25SJulian Elischer 			/*
1821e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18224093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18231d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1824e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18254093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1826e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1827e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1828e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1829e602ba25SJulian Elischer 			 */
18301279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18316933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1832e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18334093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1834e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1835e602ba25SJulian Elischer 				/*
1836e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1837e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1838e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1839e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1840e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1841e602ba25SJulian Elischer 				 * process, we need to make sure that the
1842e602ba25SJulian Elischer 				 * single thread is runnable asap.
1843e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1844e602ba25SJulian Elischer 				 */
1845e602ba25SJulian Elischer 				goto runfast;
1846e602ba25SJulian Elischer 			}
1847e602ba25SJulian Elischer 			/*
1848e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1849e602ba25SJulian Elischer 			 */
1850e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
185104774f23SJulian Elischer 			thread_unsuspend(p);
1852e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1853e602ba25SJulian Elischer 			goto out;
1854e602ba25SJulian Elischer 		}
1855e602ba25SJulian Elischer 
1856e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1857e602ba25SJulian Elischer 			/*
1858e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1859e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
186004774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1861e602ba25SJulian Elischer 			 */
18621279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18634093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1864e602ba25SJulian Elischer 			goto out;
1865e602ba25SJulian Elischer 		}
1866e602ba25SJulian Elischer 
1867e602ba25SJulian Elischer 		/*
1868e602ba25SJulian Elischer 		 * All other kinds of signals:
1869e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1870e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1871e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
187204774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1873e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1874e602ba25SJulian Elischer 		 */
1875e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
187644f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
187744f3b092SJohn Baldwin 			sleepq_abort(td);
1878e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1879df8bae1dSRodney W. Grimes 		goto out;
1880df8bae1dSRodney W. Grimes 		/*
18819a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
18829a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
1883df8bae1dSRodney W. Grimes 		 */
1884e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
188535c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
188635c32a76SDavid Xu 			!(prop & SA_STOP)) {
1887721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18884093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1889721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1890df8bae1dSRodney W. Grimes 			goto out;
189104774f23SJulian Elischer 		}
189235c32a76SDavid Xu 		if (prop & SA_STOP) {
189335c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
189435c32a76SDavid Xu 				goto out;
1895e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1896e574e444SDavid Xu 			p->p_xstat = sig;
1897cbf4e354SDavid Xu 			p->p_xthread = td;
189835c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
18994093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
19004093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1901062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1902062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
19034093529dSJeff Roberson 					thread_suspend_one(td0);
1904062cf543SDavid Xu 				} else if (td != td0) {
1905062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1906062cf543SDavid Xu 				}
190735c32a76SDavid Xu 			}
1908e574e444SDavid Xu 			thread_stopped(p);
19094093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1910e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
19114093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
19124093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
19134093529dSJeff Roberson 			}
191435c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
191535c32a76SDavid Xu 			goto out;
191635c32a76SDavid Xu 		}
1917721e5910SJulian Elischer 		else
1918df8bae1dSRodney W. Grimes 			goto runfast;
1919df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1920e602ba25SJulian Elischer 	} else {
1921e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19224093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1923e602ba25SJulian Elischer 		goto out;
1924e602ba25SJulian Elischer 	}
1925e602ba25SJulian Elischer 
1926b40ce416SJulian Elischer 	/*
1927e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1928e602ba25SJulian Elischer 	 * running threads.
1929b40ce416SJulian Elischer 	 */
1930e602ba25SJulian Elischer 
1931e602ba25SJulian Elischer runfast:
1932aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19334093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1934e602ba25SJulian Elischer 	thread_unsuspend(p);
1935e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1936e602ba25SJulian Elischer out:
1937e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1938e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1939e602ba25SJulian Elischer }
1940e602ba25SJulian Elischer 
1941e602ba25SJulian Elischer /*
1942e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1943e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
1944e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1945e602ba25SJulian Elischer  */
1946e602ba25SJulian Elischer static void
19474093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1948e602ba25SJulian Elischer {
1949e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1950e602ba25SJulian Elischer 	register int prop;
1951e602ba25SJulian Elischer 
19528b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1953aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1954e602ba25SJulian Elischer 	prop = sigprop(sig);
1955a4c2da15SBruce Evans 
1956e602ba25SJulian Elischer 	/*
1957aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1958e602ba25SJulian Elischer 	 * killed in this lifetime.
1959e602ba25SJulian Elischer 	 */
1960a4c2da15SBruce Evans 	if (action == SIG_DFL && (prop & SA_KILL)) {
1961a4c2da15SBruce Evans 		if (td->td_priority > PUSER)
1962e602ba25SJulian Elischer 			td->td_priority = PUSER;
1963b40ce416SJulian Elischer 	}
1964a4c2da15SBruce Evans 
196580c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
1966e602ba25SJulian Elischer 		/*
1967e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1968e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1969e602ba25SJulian Elischer 		 * be noticed when the process returns through
1970e602ba25SJulian Elischer 		 * trap() or syscall().
1971e602ba25SJulian Elischer 		 */
197244f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1973aa0fa334SJulian Elischer 			return;
1974e602ba25SJulian Elischer 		/*
1975e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1976e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1977e602ba25SJulian Elischer 		 * for its parent.
1978e602ba25SJulian Elischer 		 */
1979e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1980e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1981aa0fa334SJulian Elischer 		} else {
1982df8bae1dSRodney W. Grimes 			/*
1983e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1984e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1985e602ba25SJulian Elischer 			 * be awakened.
1986df8bae1dSRodney W. Grimes 			 */
1987e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19881d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19894093529dSJeff Roberson 				/*
19904093529dSJeff Roberson 				 * It may be on either list in this state.
19914093529dSJeff Roberson 				 * Remove from both for now.
19924093529dSJeff Roberson 				 */
19934093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1994aa0fa334SJulian Elischer 				return;
1995df8bae1dSRodney W. Grimes 			}
1996df8bae1dSRodney W. Grimes 
1997aa0fa334SJulian Elischer 			/*
1998a4c2da15SBruce Evans 			 * Give low priority threads a better chance to run.
1999aa0fa334SJulian Elischer 			 */
200044f3b092SJohn Baldwin 			if (td->td_priority > PUSER)
2001aa0fa334SJulian Elischer 				td->td_priority = PUSER;
2002aa0fa334SJulian Elischer 		}
200344f3b092SJohn Baldwin 		sleepq_abort(td);
2004a4c2da15SBruce Evans 	} else {
2005df8bae1dSRodney W. Grimes 		/*
2006a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
2007df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2008df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2009df8bae1dSRodney W. Grimes 		 */
2010a4c2da15SBruce Evans #ifdef SMP
201144f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
2012e602ba25SJulian Elischer 			forward_signal(td);
20133163861cSTor Egge #endif
20146caa8a15SJohn Baldwin 	}
2015a4c2da15SBruce Evans }
2016df8bae1dSRodney W. Grimes 
2017cbf4e354SDavid Xu int
20184cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
20194cc9f52fSRobert Drehmel {
20204cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
2021cbf4e354SDavid Xu 	struct thread *td0;
20224cc9f52fSRobert Drehmel 
202330a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20244cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
20254cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
20264cc9f52fSRobert Drehmel 
20274cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
2028cbf4e354SDavid Xu 	td->td_flags |= TDF_XSIG;
2029cbf4e354SDavid Xu 	mtx_unlock_spin(&sched_lock);
2030cbf4e354SDavid Xu 	td->td_xsig = sig;
2031cbf4e354SDavid Xu 	while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2032cbf4e354SDavid Xu 		if (p->p_flag & P_SINGLE_EXIT) {
2033cbf4e354SDavid Xu 			mtx_lock_spin(&sched_lock);
2034cbf4e354SDavid Xu 			td->td_flags &= ~TDF_XSIG;
2035cbf4e354SDavid Xu 			mtx_unlock_spin(&sched_lock);
2036cbf4e354SDavid Xu 			return (sig);
2037cbf4e354SDavid Xu 		}
2038cbf4e354SDavid Xu 		/*
2039cbf4e354SDavid Xu 		 * Just make wait() to work, the last stopped thread
2040cbf4e354SDavid Xu 		 * will win.
2041cbf4e354SDavid Xu 		 */
2042cbf4e354SDavid Xu 		p->p_xstat = sig;
2043cbf4e354SDavid Xu 		p->p_xthread = td;
2044cbf4e354SDavid Xu 		p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2045cbf4e354SDavid Xu 		mtx_lock_spin(&sched_lock);
2046cbf4e354SDavid Xu 		FOREACH_THREAD_IN_PROC(p, td0) {
2047cbf4e354SDavid Xu 			if (TD_IS_SLEEPING(td0) &&
2048cbf4e354SDavid Xu 			    (td0->td_flags & TDF_SINTR) &&
2049cbf4e354SDavid Xu 			    !TD_IS_SUSPENDED(td0)) {
2050cbf4e354SDavid Xu 				thread_suspend_one(td0);
2051cbf4e354SDavid Xu 			} else if (td != td0) {
2052cbf4e354SDavid Xu 				td0->td_flags |= TDF_ASTPENDING;
2053cbf4e354SDavid Xu 			}
2054cbf4e354SDavid Xu 		}
2055cbf4e354SDavid Xu stopme:
2056cbf4e354SDavid Xu 		thread_stopped(p);
20574cc9f52fSRobert Drehmel 		thread_suspend_one(td);
20584cc9f52fSRobert Drehmel 		PROC_UNLOCK(p);
20594cc9f52fSRobert Drehmel 		DROP_GIANT();
2060cbf4e354SDavid Xu 		mi_switch(SW_VOL, NULL);
20614cc9f52fSRobert Drehmel 		mtx_unlock_spin(&sched_lock);
20624cc9f52fSRobert Drehmel 		PICKUP_GIANT();
2063cbf4e354SDavid Xu 		PROC_LOCK(p);
2064cbf4e354SDavid Xu 		if (!(p->p_flag & P_TRACED))
2065cbf4e354SDavid Xu 			break;
2066cbf4e354SDavid Xu 		if (td->td_flags & TDF_DBSUSPEND) {
2067cbf4e354SDavid Xu 			if (p->p_flag & P_SINGLE_EXIT)
2068cbf4e354SDavid Xu 				break;
2069cbf4e354SDavid Xu 			mtx_lock_spin(&sched_lock);
2070cbf4e354SDavid Xu 			goto stopme;
2071cbf4e354SDavid Xu 		}
2072cbf4e354SDavid Xu 	}
2073cbf4e354SDavid Xu 	return (td->td_xsig);
20744cc9f52fSRobert Drehmel }
20754cc9f52fSRobert Drehmel 
2076df8bae1dSRodney W. Grimes /*
2077df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2078df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2079df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2080df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2081df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2082628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2083df8bae1dSRodney W. Grimes  * sequence is
2084df8bae1dSRodney W. Grimes  *
2085e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
20862c42a146SMarcel Moolenaar  *		postsig(sig);
2087df8bae1dSRodney W. Grimes  */
20886711f10fSJohn Baldwin static int
2089e602ba25SJulian Elischer issignal(td)
2090e602ba25SJulian Elischer 	struct thread *td;
2091df8bae1dSRodney W. Grimes {
2092e602ba25SJulian Elischer 	struct proc *p;
209390af4afaSJohn Baldwin 	struct sigacts *ps;
20944093529dSJeff Roberson 	sigset_t sigpending;
2095cbf4e354SDavid Xu 	int sig, prop, newsig;
2096062cf543SDavid Xu 	struct thread *td0;
2097df8bae1dSRodney W. Grimes 
2098e602ba25SJulian Elischer 	p = td->td_proc;
209990af4afaSJohn Baldwin 	ps = p->p_sigacts;
210090af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2101628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2102df8bae1dSRodney W. Grimes 	for (;;) {
21032a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
21042a024a2bSSean Eric Fagan 
21054093529dSJeff Roberson 		sigpending = td->td_siglist;
21064093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
21074093529dSJeff Roberson 
2108df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
21094093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
21104093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2111df8bae1dSRodney W. Grimes 			return (0);
21124093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
21132a024a2bSSean Eric Fagan 
2114047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2115047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2116047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2117047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2118047aa39bSRobert Watson 		}
21192a024a2bSSean Eric Fagan 
2120df8bae1dSRodney W. Grimes 		/*
2121df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2122df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2123df8bae1dSRodney W. Grimes 		 */
212490af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
21254093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
2126cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2127cbf4e354SDavid Xu 				SIGADDSET(td->td_sigmask, sig);
2128df8bae1dSRodney W. Grimes 			continue;
2129df8bae1dSRodney W. Grimes 		}
2130df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2131df8bae1dSRodney W. Grimes 			/*
2132d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2133df8bae1dSRodney W. Grimes 			 */
213490af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2135cbf4e354SDavid Xu 			newsig = ptracestop(td, sig);
213690af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2137df8bae1dSRodney W. Grimes 
2138df8bae1dSRodney W. Grimes 			/*
2139df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
2140df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
2141df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
2142df8bae1dSRodney W. Grimes 			 */
21434093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
2144cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2145cbf4e354SDavid Xu 				SIGADDSET(td->td_sigmask, sig);
2146cbf4e354SDavid Xu 			if (newsig == 0)
2147df8bae1dSRodney W. Grimes 				continue;
2148cbf4e354SDavid Xu 			sig = newsig;
2149df8bae1dSRodney W. Grimes 			/*
21508d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
21518d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
21528d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
21538d542cb5SDavid E. O'Brien 			 */
21548d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
21558d542cb5SDavid E. O'Brien 				continue;
21568d542cb5SDavid E. O'Brien 
21578d542cb5SDavid E. O'Brien 			/*
21584093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
21591d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
2160df8bae1dSRodney W. Grimes 			 */
21614093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
2162cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2163cbf4e354SDavid Xu 				SIGDELSET(td->td_sigmask, sig);
21644093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
2165df8bae1dSRodney W. Grimes 				continue;
21664093529dSJeff Roberson 			signotify(td);
2167df8bae1dSRodney W. Grimes 		}
2168df8bae1dSRodney W. Grimes 
21698d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
21708d542cb5SDavid E. O'Brien 
2171df8bae1dSRodney W. Grimes 		/*
2172df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2173df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2174df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2175df8bae1dSRodney W. Grimes 		 */
2176d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2177df8bae1dSRodney W. Grimes 
2178d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2179df8bae1dSRodney W. Grimes 			/*
2180df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2181df8bae1dSRodney W. Grimes 			 */
2182df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2183df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2184df8bae1dSRodney W. Grimes 				/*
2185df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2186df8bae1dSRodney W. Grimes 				 * in init? XXX
2187df8bae1dSRodney W. Grimes 				 */
2188d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
21892c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2190df8bae1dSRodney W. Grimes #endif
2191df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2192df8bae1dSRodney W. Grimes 			}
2193df8bae1dSRodney W. Grimes 			/*
2194df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2195df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2196df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2197df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2198df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2199df8bae1dSRodney W. Grimes 			 */
2200df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2201df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2202df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2203df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2204df8bae1dSRodney W. Grimes 					break;	/* == ignore */
220590af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
220690af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
220790af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2208e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
22092c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2210cbf4e354SDavid Xu 				p->p_xthread = td;
2211721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2212062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2213062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2214062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2215062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2216062cf543SDavid Xu 						thread_suspend_one(td0);
2217062cf543SDavid Xu 					} else if (td != td0) {
2218062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2219062cf543SDavid Xu 					}
2220062cf543SDavid Xu 				}
2221e574e444SDavid Xu 				thread_stopped(p);
222271fad9fdSJulian Elischer 				thread_suspend_one(td);
2223721e5910SJulian Elischer 				PROC_UNLOCK(p);
2224721e5910SJulian Elischer 				DROP_GIANT();
2225bf0acc27SJohn Baldwin 				mi_switch(SW_INVOL, NULL);
2226721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2227721e5910SJulian Elischer 				PICKUP_GIANT();
2228721e5910SJulian Elischer 				PROC_LOCK(p);
222990af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2230df8bae1dSRodney W. Grimes 				break;
223121b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2232df8bae1dSRodney W. Grimes 				/*
2233df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2234df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2235df8bae1dSRodney W. Grimes 				 */
2236df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2237df8bae1dSRodney W. Grimes 			} else
22382c42a146SMarcel Moolenaar 				return (sig);
2239df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2240df8bae1dSRodney W. Grimes 
2241d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2242df8bae1dSRodney W. Grimes 			/*
2243df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2244df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2245df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2246df8bae1dSRodney W. Grimes 			 */
2247df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2248df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2249df8bae1dSRodney W. Grimes 				printf("issignal\n");
2250df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2251df8bae1dSRodney W. Grimes 
2252df8bae1dSRodney W. Grimes 		default:
2253df8bae1dSRodney W. Grimes 			/*
2254df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2255df8bae1dSRodney W. Grimes 			 * postsig() process it.
2256df8bae1dSRodney W. Grimes 			 */
22572c42a146SMarcel Moolenaar 			return (sig);
2258df8bae1dSRodney W. Grimes 		}
22594093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2260df8bae1dSRodney W. Grimes 	}
2261df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2262df8bae1dSRodney W. Grimes }
2263df8bae1dSRodney W. Grimes 
2264df8bae1dSRodney W. Grimes /*
2265df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2266df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
22674a3b3dcbSColin Percival  * on the run queue.  Must be called with the proc p locked.
2268df8bae1dSRodney W. Grimes  */
22695b3047d5SJohn Baldwin static void
2270e574e444SDavid Xu stop(struct proc *p)
2271df8bae1dSRodney W. Grimes {
2272df8bae1dSRodney W. Grimes 
2273628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22741279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2275df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
227601609114SAlfred Perlstein 	wakeup(p->p_pptr);
2277df8bae1dSRodney W. Grimes }
2278df8bae1dSRodney W. Grimes 
227990af4afaSJohn Baldwin /*
228090af4afaSJohn Baldwin  * MPSAFE
228190af4afaSJohn Baldwin  */
2282e574e444SDavid Xu void
2283e574e444SDavid Xu thread_stopped(struct proc *p)
2284e574e444SDavid Xu {
2285e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
228690af4afaSJohn Baldwin 	struct sigacts *ps;
2287e574e444SDavid Xu 	int n;
2288e574e444SDavid Xu 
2289e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2290e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2291e574e444SDavid Xu 	n = p->p_suspcount;
2292e574e444SDavid Xu 	if (p == p1)
2293e574e444SDavid Xu 		n++;
2294e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2295e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2296e574e444SDavid Xu 		stop(p);
2297e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
229890af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
229990af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
230090af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
230190af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2302e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
230390af4afaSJohn Baldwin 		} else
230490af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2305e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2306e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2307e574e444SDavid Xu 	}
2308e574e444SDavid Xu }
2309e574e444SDavid Xu 
2310df8bae1dSRodney W. Grimes /*
2311df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2312df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2313df8bae1dSRodney W. Grimes  */
2314df8bae1dSRodney W. Grimes void
23152c42a146SMarcel Moolenaar postsig(sig)
23162c42a146SMarcel Moolenaar 	register int sig;
2317df8bae1dSRodney W. Grimes {
2318b40ce416SJulian Elischer 	struct thread *td = curthread;
2319b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2320628d2653SJohn Baldwin 	struct sigacts *ps;
23212c42a146SMarcel Moolenaar 	sig_t action;
23222c42a146SMarcel Moolenaar 	sigset_t returnmask;
23232c42a146SMarcel Moolenaar 	int code;
2324df8bae1dSRodney W. Grimes 
23252c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
23265526d2d9SEivind Eklund 
23272ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2328628d2653SJohn Baldwin 	ps = p->p_sigacts;
232990af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
23304093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
23312c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2332df8bae1dSRodney W. Grimes #ifdef KTRACE
2333374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
23345e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
23354093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2336df8bae1dSRodney W. Grimes #endif
2337047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2338047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2339047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2340047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2341047aa39bSRobert Watson 	}
23422a024a2bSSean Eric Fagan 
2343cbf4e354SDavid Xu 	if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2344df8bae1dSRodney W. Grimes 		/*
2345df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2346df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2347df8bae1dSRodney W. Grimes 		 */
234890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2349b40ce416SJulian Elischer 		sigexit(td, sig);
2350df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2351df8bae1dSRodney W. Grimes 	} else {
2352cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA) {
2353432b45deSDavid Xu 			if (sig == SIGKILL) {
2354432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2355432b45deSDavid Xu 				sigexit(td, sig);
2356432b45deSDavid Xu 			}
2357432b45deSDavid Xu 		}
2358432b45deSDavid Xu 
2359df8bae1dSRodney W. Grimes 		/*
2360df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2361df8bae1dSRodney W. Grimes 		 */
23624093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23635526d2d9SEivind Eklund 		    ("postsig action"));
2364df8bae1dSRodney W. Grimes 		/*
2365df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2366645682fdSLuoqi Chen 		 * occurrences of this signal.
2367df8bae1dSRodney W. Grimes 		 *
2368645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2369df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2370645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2371df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2372df8bae1dSRodney W. Grimes 		 */
23735e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23744093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23755e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2376df8bae1dSRodney W. Grimes 		} else
23774093529dSJeff Roberson 			returnmask = td->td_sigmask;
23782c42a146SMarcel Moolenaar 
23794093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23802c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23814093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23822c42a146SMarcel Moolenaar 
23832c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2384289ccde0SPeter Wemm 			/*
23858f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2386289ccde0SPeter Wemm 			 */
238790af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23882c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23892c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
239090af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23912c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2392dedc04feSPeter Wemm 		}
2393df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23942c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2395df8bae1dSRodney W. Grimes 			code = 0;
2396df8bae1dSRodney W. Grimes 		} else {
23976626c604SJulian Elischer 			code = p->p_code;
23986626c604SJulian Elischer 			p->p_code = 0;
23996626c604SJulian Elischer 			p->p_sig = 0;
2400df8bae1dSRodney W. Grimes 		}
2401cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA)
240258a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
240358a3c273SJeff Roberson 		else
240458a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
240558a3c273SJeff Roberson 			    &returnmask, code);
2406df8bae1dSRodney W. Grimes 	}
2407df8bae1dSRodney W. Grimes }
2408df8bae1dSRodney W. Grimes 
2409df8bae1dSRodney W. Grimes /*
2410df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2411df8bae1dSRodney W. Grimes  */
241226f9a767SRodney W. Grimes void
2413df8bae1dSRodney W. Grimes killproc(p, why)
2414df8bae1dSRodney W. Grimes 	struct proc *p;
2415df8bae1dSRodney W. Grimes 	char *why;
2416df8bae1dSRodney W. Grimes {
24179081e5e8SJohn Baldwin 
24189081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
24190384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
24200384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2421729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2422b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2423df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2424df8bae1dSRodney W. Grimes }
2425df8bae1dSRodney W. Grimes 
2426df8bae1dSRodney W. Grimes /*
2427df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2428df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2429df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2430df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2431df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2432df8bae1dSRodney W. Grimes  * does not return.
243390af4afaSJohn Baldwin  *
243490af4afaSJohn Baldwin  * MPSAFE
2435df8bae1dSRodney W. Grimes  */
243626f9a767SRodney W. Grimes void
2437b40ce416SJulian Elischer sigexit(td, sig)
2438b40ce416SJulian Elischer 	struct thread *td;
24392c42a146SMarcel Moolenaar 	int sig;
2440df8bae1dSRodney W. Grimes {
2441b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2442df8bae1dSRodney W. Grimes 
2443628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2444df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
24452c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
24462c42a146SMarcel Moolenaar 		p->p_sig = sig;
2447c364e17eSAndrey A. Chernov 		/*
2448c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2449c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2450c364e17eSAndrey A. Chernov 		 * these messages.)
2451c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
24524ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2453c364e17eSAndrey A. Chernov 		 */
2454b40ce416SJulian Elischer 		if (coredump(td) == 0)
24552c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
245657308494SJoerg Wunsch 		if (kern_logsigexit)
245757308494SJoerg Wunsch 			log(LOG_INFO,
245857308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24593d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24609c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24612c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24622c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
24634ae89b95SJohn Baldwin 	} else
2464628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2465b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2466df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2467df8bae1dSRodney W. Grimes }
2468df8bae1dSRodney W. Grimes 
2469c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2470c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2471c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2472c5edb423SSean Eric Fagan 
2473c5edb423SSean Eric Fagan /*
2474c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2475c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2476c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2477c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2478c5edb423SSean Eric Fagan  *	%P	process id (pid)
2479c5edb423SSean Eric Fagan  *	%U	user id (uid)
2480c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2481c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2482c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2483c5edb423SSean Eric Fagan  */
2484c5edb423SSean Eric Fagan 
2485fca666a1SJulian Elischer static char *
2486c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24878b43b535SAlfred Perlstein 	const char *name;
24888b43b535SAlfred Perlstein 	uid_t uid;
24898b43b535SAlfred Perlstein 	pid_t pid;
24908b43b535SAlfred Perlstein {
24918b43b535SAlfred Perlstein 	const char *format, *appendstr;
2492c5edb423SSean Eric Fagan 	char *temp;
2493c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24948b43b535SAlfred Perlstein 	size_t i, l, n;
2495c5edb423SSean Eric Fagan 
24968b43b535SAlfred Perlstein 	format = corefilename;
24978b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24980bfe2990SEivind Eklund 	if (temp == NULL)
24998b43b535SAlfred Perlstein 		return (NULL);
2500ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2501c5edb423SSean Eric Fagan 		switch (format[i]) {
2502c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2503c5edb423SSean Eric Fagan 			i++;
2504c5edb423SSean Eric Fagan 			switch (format[i]) {
2505c5edb423SSean Eric Fagan 			case '%':
25068b43b535SAlfred Perlstein 				appendstr = "%";
2507c5edb423SSean Eric Fagan 				break;
2508c5edb423SSean Eric Fagan 			case 'N':	/* process name */
25098b43b535SAlfred Perlstein 				appendstr = name;
2510c5edb423SSean Eric Fagan 				break;
2511c5edb423SSean Eric Fagan 			case 'P':	/* process id */
25128b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
25138b43b535SAlfred Perlstein 				appendstr = buf;
2514c5edb423SSean Eric Fagan 				break;
2515c5edb423SSean Eric Fagan 			case 'U':	/* user id */
25168b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
25178b43b535SAlfred Perlstein 				appendstr = buf;
2518c5edb423SSean Eric Fagan 				break;
2519c5edb423SSean Eric Fagan 			default:
25208b43b535SAlfred Perlstein 				appendstr = "";
25218b43b535SAlfred Perlstein 			  	log(LOG_ERR,
25228b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
25238b43b535SAlfred Perlstein 				    format[i], format);
2524c5edb423SSean Eric Fagan 			}
25258b43b535SAlfred Perlstein 			l = strlen(appendstr);
25268b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
25278b43b535SAlfred Perlstein 				goto toolong;
25288b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
25298b43b535SAlfred Perlstein 			n += l;
2530c5edb423SSean Eric Fagan 			break;
2531c5edb423SSean Eric Fagan 		default:
2532c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2533c5edb423SSean Eric Fagan 		}
2534c5edb423SSean Eric Fagan 	}
25358b43b535SAlfred Perlstein 	if (format[i] != '\0')
25368b43b535SAlfred Perlstein 		goto toolong;
25378b43b535SAlfred Perlstein 	return (temp);
25388b43b535SAlfred Perlstein toolong:
25398b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
25408b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
25418b43b535SAlfred Perlstein 	free(temp, M_TEMP);
25428b43b535SAlfred Perlstein 	return (NULL);
2543c5edb423SSean Eric Fagan }
2544c5edb423SSean Eric Fagan 
2545df8bae1dSRodney W. Grimes /*
2546fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2547fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2548fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2549fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2550fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2551fca666a1SJulian Elischer  */
2552fca666a1SJulian Elischer 
2553fca666a1SJulian Elischer static int
2554b40ce416SJulian Elischer coredump(struct thread *td)
2555fca666a1SJulian Elischer {
2556b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2557fca666a1SJulian Elischer 	register struct vnode *vp;
25589c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
255906ae1e91SMatthew Dillon 	struct flock lf;
2560fca666a1SJulian Elischer 	struct nameidata nd;
2561fca666a1SJulian Elischer 	struct vattr vattr;
2562c447f5b2SRobert Watson 	int error, error1, flags, locked;
2563f2a2857bSKirk McKusick 	struct mount *mp;
2564fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2565fca666a1SJulian Elischer 	off_t limit;
2566fca666a1SJulian Elischer 
25674ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2568628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2569fca666a1SJulian Elischer 
2570628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2571628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2572fca666a1SJulian Elischer 		return (EFAULT);
2573628d2653SJohn Baldwin 	}
2574fca666a1SJulian Elischer 
2575fca666a1SJulian Elischer 	/*
257635a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
257735a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
257835a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
257935a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
258035a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
258135a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2582fca666a1SJulian Elischer 	 */
258391d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2584628d2653SJohn Baldwin 	PROC_UNLOCK(p);
258591d5354aSJohn Baldwin 	if (limit == 0)
258691d5354aSJohn Baldwin 		return (EFBIG);
258735a2598fSSean Eric Fagan 
25884ae89b95SJohn Baldwin 	mtx_lock(&Giant);
2589f2a2857bSKirk McKusick restart:
25909c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
25914ae89b95SJohn Baldwin 	if (name == NULL) {
25924ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2593ccdbd10cSPeter Pentchev 		return (EINVAL);
25944ae89b95SJohn Baldwin 	}
2595b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2596e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25977c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2598fca666a1SJulian Elischer 	free(name, M_TEMP);
25994ae89b95SJohn Baldwin 	if (error) {
26004ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2601fca666a1SJulian Elischer 		return (error);
26024ae89b95SJohn Baldwin 	}
2603762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2604fca666a1SJulian Elischer 	vp = nd.ni_vp;
260506ae1e91SMatthew Dillon 
2606832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2607832dafadSDon Lewis 	if (vp->v_type != VREG ||
2608832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2609832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2610832dafadSDon Lewis 		error = EFAULT;
26114ae89b95SJohn Baldwin 		goto out;
2612832dafadSDon Lewis 	}
2613832dafadSDon Lewis 
2614b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
261506ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
261606ae1e91SMatthew Dillon 	lf.l_start = 0;
261706ae1e91SMatthew Dillon 	lf.l_len = 0;
261806ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2619c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
262006ae1e91SMatthew Dillon 
262106ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
262206ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
262336bbf86bSRobert Watson 		if (locked)
262406ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2625b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2626f2a2857bSKirk McKusick 			return (error);
2627f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2628f2a2857bSKirk McKusick 			return (error);
2629f2a2857bSKirk McKusick 		goto restart;
2630f2a2857bSKirk McKusick 	}
2631fca666a1SJulian Elischer 
2632fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2633fca666a1SJulian Elischer 	vattr.va_size = 0;
26346141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
26356141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
263688b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2637b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2638b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
263988b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2640628d2653SJohn Baldwin 	PROC_LOCK(p);
2641fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2642628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2643fca666a1SJulian Elischer 
2644fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2645b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2646fca666a1SJulian Elischer 	  ENOSYS;
2647fca666a1SJulian Elischer 
2648c447f5b2SRobert Watson 	if (locked) {
264906ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
265006ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2651c447f5b2SRobert Watson 	}
2652f2a2857bSKirk McKusick 	vn_finished_write(mp);
26534ae89b95SJohn Baldwin out:
2654b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
26554ae89b95SJohn Baldwin 	mtx_unlock(&Giant);
2656fca666a1SJulian Elischer 	if (error == 0)
2657fca666a1SJulian Elischer 		error = error1;
2658fca666a1SJulian Elischer 	return (error);
2659fca666a1SJulian Elischer }
2660fca666a1SJulian Elischer 
2661fca666a1SJulian Elischer /*
2662df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2663df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2664df8bae1dSRodney W. Grimes  */
2665d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2666df8bae1dSRodney W. Grimes struct nosys_args {
2667df8bae1dSRodney W. Grimes 	int	dummy;
2668df8bae1dSRodney W. Grimes };
2669d2d3e875SBruce Evans #endif
2670fb99ab88SMatthew Dillon /*
2671fb99ab88SMatthew Dillon  * MPSAFE
2672fb99ab88SMatthew Dillon  */
2673df8bae1dSRodney W. Grimes /* ARGSUSED */
267426f9a767SRodney W. Grimes int
2675b40ce416SJulian Elischer nosys(td, args)
2676b40ce416SJulian Elischer 	struct thread *td;
2677df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2678df8bae1dSRodney W. Grimes {
2679b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2680b40ce416SJulian Elischer 
2681628d2653SJohn Baldwin 	PROC_LOCK(p);
2682df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2683628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2684f5216b9aSBruce Evans 	return (ENOSYS);
2685df8bae1dSRodney W. Grimes }
2686831d27a9SDon Lewis 
2687831d27a9SDon Lewis /*
268848f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2689831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2690831d27a9SDon Lewis  */
2691831d27a9SDon Lewis void
2692f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2693f1320723SAlfred Perlstein 	struct sigio **sigiop;
26942c42a146SMarcel Moolenaar 	int sig, checkctty;
2695831d27a9SDon Lewis {
2696f1320723SAlfred Perlstein 	struct sigio *sigio;
2697831d27a9SDon Lewis 
2698f1320723SAlfred Perlstein 	SIGIO_LOCK();
2699f1320723SAlfred Perlstein 	sigio = *sigiop;
2700f1320723SAlfred Perlstein 	if (sigio == NULL) {
2701f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2702f1320723SAlfred Perlstein 		return;
2703f1320723SAlfred Perlstein 	}
2704831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2705628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
27062b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
27072c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2708628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2709831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2710831d27a9SDon Lewis 		struct proc *p;
2711831d27a9SDon Lewis 
2712f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2713628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2714628d2653SJohn Baldwin 			PROC_LOCK(p);
27152b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2716831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
27172c42a146SMarcel Moolenaar 				psignal(p, sig);
2718628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2719628d2653SJohn Baldwin 		}
2720f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2721831d27a9SDon Lewis 	}
2722f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2723831d27a9SDon Lewis }
2724cb679c38SJonathan Lemon 
2725cb679c38SJonathan Lemon static int
2726cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2727cb679c38SJonathan Lemon {
2728cb679c38SJonathan Lemon 	struct proc *p = curproc;
2729cb679c38SJonathan Lemon 
2730cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2731cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2732cb679c38SJonathan Lemon 
2733ad3b9257SJohn-Mark Gurney 	knlist_add(&p->p_klist, kn, 0);
2734cb679c38SJonathan Lemon 
2735cb679c38SJonathan Lemon 	return (0);
2736cb679c38SJonathan Lemon }
2737cb679c38SJonathan Lemon 
2738cb679c38SJonathan Lemon static void
2739cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2740cb679c38SJonathan Lemon {
2741cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2742cb679c38SJonathan Lemon 
2743ad3b9257SJohn-Mark Gurney 	knlist_remove(&p->p_klist, kn, 0);
2744cb679c38SJonathan Lemon }
2745cb679c38SJonathan Lemon 
2746cb679c38SJonathan Lemon /*
2747cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2748cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2749cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2750cb679c38SJonathan Lemon  * isn't worth the trouble.
2751cb679c38SJonathan Lemon  */
2752cb679c38SJonathan Lemon static int
2753cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2754cb679c38SJonathan Lemon {
2755cb679c38SJonathan Lemon 
2756cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2757cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2758cb679c38SJonathan Lemon 
2759cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2760cb679c38SJonathan Lemon 			kn->kn_data++;
2761cb679c38SJonathan Lemon 	}
2762cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2763cb679c38SJonathan Lemon }
276490af4afaSJohn Baldwin 
276590af4afaSJohn Baldwin struct sigacts *
276690af4afaSJohn Baldwin sigacts_alloc(void)
276790af4afaSJohn Baldwin {
276890af4afaSJohn Baldwin 	struct sigacts *ps;
276990af4afaSJohn Baldwin 
277090af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
277190af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
277290af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
277390af4afaSJohn Baldwin 	return (ps);
277490af4afaSJohn Baldwin }
277590af4afaSJohn Baldwin 
277690af4afaSJohn Baldwin void
277790af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
277890af4afaSJohn Baldwin {
277990af4afaSJohn Baldwin 
278090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
278190af4afaSJohn Baldwin 	ps->ps_refcnt--;
278290af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
278390af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
278490af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
278590af4afaSJohn Baldwin 	} else
278690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
278790af4afaSJohn Baldwin }
278890af4afaSJohn Baldwin 
278990af4afaSJohn Baldwin struct sigacts *
279090af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
279190af4afaSJohn Baldwin {
279290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
279390af4afaSJohn Baldwin 	ps->ps_refcnt++;
279490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
279590af4afaSJohn Baldwin 	return (ps);
279690af4afaSJohn Baldwin }
279790af4afaSJohn Baldwin 
279890af4afaSJohn Baldwin void
279990af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
280090af4afaSJohn Baldwin {
280190af4afaSJohn Baldwin 
280290af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
280390af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
280490af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
280590af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
280690af4afaSJohn Baldwin }
280790af4afaSJohn Baldwin 
280890af4afaSJohn Baldwin int
280990af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
281090af4afaSJohn Baldwin {
281190af4afaSJohn Baldwin 	int shared;
281290af4afaSJohn Baldwin 
281390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
281490af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
281590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
281690af4afaSJohn Baldwin 	return (shared);
281790af4afaSJohn Baldwin }
2818