xref: /freebsd/sys/kern/kern_sig.c (revision b3a4fb14b35d905528d0eade13c2c573360d1ccc)
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>
62b3a4fb14SDavid Xu #include <sys/sched.h>
6344f3b092SJohn Baldwin #include <sys/sleepqueue.h>
646caa8a15SJohn Baldwin #include <sys/smp.h>
65df8bae1dSRodney W. Grimes #include <sys/stat.h>
661005a129SJohn Baldwin #include <sys/sx.h>
678f19eb88SIan Dowse #include <sys/syscallsubr.h>
68c87e2930SDavid Greenman #include <sys/sysctl.h>
69854dc8c2SJohn Baldwin #include <sys/sysent.h>
70854dc8c2SJohn Baldwin #include <sys/syslog.h>
71854dc8c2SJohn Baldwin #include <sys/sysproto.h>
7206ae1e91SMatthew Dillon #include <sys/unistd.h>
73854dc8c2SJohn Baldwin #include <sys/wait.h>
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes #include <machine/cpu.h>
76df8bae1dSRodney W. Grimes 
7723eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
7823eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
7923eeeff7SPeter Wemm #endif
8023eeeff7SPeter Wemm 
816f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
826f841fb7SMarcel Moolenaar 
834d77a549SAlfred Perlstein static int	coredump(struct thread *);
844d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
859c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
866711f10fSJohn Baldwin static int	issignal(struct thread *p);
874d77a549SAlfred Perlstein static int	sigprop(int sig);
884d77a549SAlfred Perlstein static void	stop(struct proc *);
894093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
90cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
91cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
92cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
934093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
94a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
95a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
96c197abc4SMike Makonnen static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
97cb679c38SJonathan Lemon 
98cb679c38SJonathan Lemon struct filterops sig_filtops =
99cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
100cb679c38SJonathan Lemon 
10157308494SJoerg Wunsch static int	kern_logsigexit = 1;
1023d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1033d177f46SBill Fumerola     &kern_logsigexit, 0,
1043d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10557308494SJoerg Wunsch 
1062b87b6d4SRobert Watson /*
1072b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1082b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1092b87b6d4SRobert Watson  * in the right situations.
1102b87b6d4SRobert Watson  */
1112b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1122b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1142b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1152b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1162b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1172b87b6d4SRobert Watson 
11822d4b0fbSJohn Polstra int sugid_coredump;
1193d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1203d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
121c87e2930SDavid Greenman 
122e5a28db9SPaul Saab static int	do_coredump = 1;
123e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
124e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
125e5a28db9SPaul Saab 
1266141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
1276141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
1286141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
1296141e04aSJohn-Mark Gurney 
1302c42a146SMarcel Moolenaar /*
1312c42a146SMarcel Moolenaar  * Signal properties and actions.
1322c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1332c42a146SMarcel Moolenaar  * according to the following properties:
1342c42a146SMarcel Moolenaar  */
1352c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1362c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1372c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1382c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1392c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1402c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1412c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
142da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
143df8bae1dSRodney W. Grimes 
1442c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
145da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
146da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
147da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1502c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
151da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1522c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
153da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1542c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1552c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1562c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
157da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
158da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
159da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
160da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
161da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
162da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
163da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
164da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
165da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
166da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
167da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1682c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1692c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
170da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
171da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
172da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
173da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
174da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
175da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1762c42a146SMarcel Moolenaar };
1772c42a146SMarcel Moolenaar 
178fbbeeb6cSBruce Evans /*
179fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
180fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
181fbbeeb6cSBruce Evans  * action, the process stops in issignal().
182e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
183fbbeeb6cSBruce Evans  *
18433510ef1SBruce Evans  * MP SAFE.
185fbbeeb6cSBruce Evans  */
186fbbeeb6cSBruce Evans int
187e602ba25SJulian Elischer cursig(struct thread *td)
188fbbeeb6cSBruce Evans {
189c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
19090af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
191179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1924093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
193fbbeeb6cSBruce Evans }
194fbbeeb6cSBruce Evans 
19579065dbaSBruce Evans /*
19679065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1974093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1984093529dSJeff Roberson  * unmasked in td_sigmask.
19979065dbaSBruce Evans  */
20079065dbaSBruce Evans void
2014093529dSJeff Roberson signotify(struct thread *td)
20279065dbaSBruce Evans {
2034093529dSJeff Roberson 	struct proc *p;
2049dde3bc9SDavid Xu 	sigset_t set, saved;
2054093529dSJeff Roberson 
2064093529dSJeff Roberson 	p = td->td_proc;
20779065dbaSBruce Evans 
20879065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2094093529dSJeff Roberson 
2104093529dSJeff Roberson 	/*
2114093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2124093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2134093529dSJeff Roberson 	 */
2144093529dSJeff Roberson 	set = p->p_siglist;
2159dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
2169dde3bc9SDavid Xu 		saved = p->p_siglist;
2174093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2184093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2194093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2204093529dSJeff Roberson 
2218b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
22279065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2234093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
22479065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
22579065dbaSBruce Evans 	}
2269dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2276dbc0850SJohn Baldwin 		if (!SIGSETEQ(saved, p->p_siglist)) {
2289dde3bc9SDavid Xu 			/* pending set changed */
2299dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
2309dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
2319dde3bc9SDavid Xu 		}
2329dde3bc9SDavid Xu 	}
2338b94a061SJohn Baldwin }
2348b94a061SJohn Baldwin 
2358b94a061SJohn Baldwin int
2368b94a061SJohn Baldwin sigonstack(size_t sp)
2378b94a061SJohn Baldwin {
238a30ec4b9SDavid Xu 	struct thread *td = curthread;
2398b94a061SJohn Baldwin 
240a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
2411930e303SPoul-Henning Kamp #if defined(COMPAT_43)
242a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
243a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
244a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
2458b94a061SJohn Baldwin #else
246a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
2478b94a061SJohn Baldwin #endif
2488b94a061SJohn Baldwin 	    : 0);
2498b94a061SJohn Baldwin }
25079065dbaSBruce Evans 
2516f841fb7SMarcel Moolenaar static __inline int
2526f841fb7SMarcel Moolenaar sigprop(int sig)
2532c42a146SMarcel Moolenaar {
2546f841fb7SMarcel Moolenaar 
2552c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2562c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2572c42a146SMarcel Moolenaar 	return (0);
258df8bae1dSRodney W. Grimes }
2592c42a146SMarcel Moolenaar 
2604093529dSJeff Roberson int
2616f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2622c42a146SMarcel Moolenaar {
2632c42a146SMarcel Moolenaar 	int i;
2642c42a146SMarcel Moolenaar 
2656f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2662c42a146SMarcel Moolenaar 		if (set->__bits[i])
2672c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
268df8bae1dSRodney W. Grimes 	return (0);
269df8bae1dSRodney W. Grimes }
270df8bae1dSRodney W. Grimes 
2712c42a146SMarcel Moolenaar /*
2728f19eb88SIan Dowse  * kern_sigaction
2732c42a146SMarcel Moolenaar  * sigaction
27423eeeff7SPeter Wemm  * freebsd4_sigaction
2752c42a146SMarcel Moolenaar  * osigaction
27625d6dc06SJohn Baldwin  *
27725d6dc06SJohn Baldwin  * MPSAFE
2782c42a146SMarcel Moolenaar  */
2798f19eb88SIan Dowse int
28023eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2818f19eb88SIan Dowse 	struct thread *td;
2822c42a146SMarcel Moolenaar 	register int sig;
2832c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
28423eeeff7SPeter Wemm 	int flags;
285df8bae1dSRodney W. Grimes {
28690af4afaSJohn Baldwin 	struct sigacts *ps;
2874093529dSJeff Roberson 	struct thread *td0;
2888f19eb88SIan Dowse 	struct proc *p = td->td_proc;
289df8bae1dSRodney W. Grimes 
2902899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2912c42a146SMarcel Moolenaar 		return (EINVAL);
2922c42a146SMarcel Moolenaar 
293628d2653SJohn Baldwin 	PROC_LOCK(p);
294628d2653SJohn Baldwin 	ps = p->p_sigacts;
29590af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2962c42a146SMarcel Moolenaar 	if (oact) {
2972c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2982c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2992c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
3002c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
3012c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
3022c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
3032c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
3042c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
3052c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
3062c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
3072c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
3082c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
3092c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
31090af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
3112c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
31290af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
3132c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
3142c42a146SMarcel Moolenaar 	}
3152c42a146SMarcel Moolenaar 	if (act) {
3162c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
317628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
31890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
319628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3202c42a146SMarcel Moolenaar 			return (EINVAL);
321628d2653SJohn Baldwin 		}
3222c42a146SMarcel Moolenaar 
323df8bae1dSRodney W. Grimes 		/*
324df8bae1dSRodney W. Grimes 		 * Change setting atomically.
325df8bae1dSRodney W. Grimes 		 */
3262c42a146SMarcel Moolenaar 
3272c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3282c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3292c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
330aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
331aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
33280f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
33380f42b55SIan Dowse 		} else {
33480f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3352c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3362c42a146SMarcel Moolenaar 		}
3372c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3382c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
339df8bae1dSRodney W. Grimes 		else
3402c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3412c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3422c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
343df8bae1dSRodney W. Grimes 		else
3442c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3452c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3462c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3471e41c1b5SSteven Wallace 		else
3482c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3492c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3502c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
351289ccde0SPeter Wemm 		else
3522c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
3532c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3542c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
35590af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3566626c604SJulian Elischer 			else
35790af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
358ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
359245f17d4SJoerg Wunsch 				/*
3602c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3612c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3622c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3632c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
364245f17d4SJoerg Wunsch 				 */
365245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
36690af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3676626c604SJulian Elischer 				else
36890af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
369245f17d4SJoerg Wunsch 			} else
37090af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
371ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
37290af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
373ba1551caSIan Dowse 			else
37490af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
375df8bae1dSRodney W. Grimes 		}
376df8bae1dSRodney W. Grimes 		/*
37790af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3782c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
37990af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3802c42a146SMarcel Moolenaar 		 * have to restart the process.
381df8bae1dSRodney W. Grimes 		 */
3822c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3832c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3842c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3859dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
3869dde3bc9SDavid Xu 			     SIGISMEMBER(p->p_siglist, sig)) {
3879dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
3889dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
3899dde3bc9SDavid Xu 			}
3902c42a146SMarcel Moolenaar 			/* never to be seen again */
3911d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
392a9a48d68SDavid Xu 			mtx_lock_spin(&sched_lock);
3934093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3944093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
395a9a48d68SDavid Xu 			mtx_unlock_spin(&sched_lock);
3962c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3972c42a146SMarcel Moolenaar 				/* easier in psignal */
39890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
39990af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
400645682fdSLuoqi Chen 		} else {
40190af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
4022c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
40390af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
4042c42a146SMarcel Moolenaar 			else
40590af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
4062c42a146SMarcel Moolenaar 		}
40723eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
40823eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
40923eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41023eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
41123eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
41223eeeff7SPeter Wemm 		else
41323eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
41423eeeff7SPeter Wemm #endif
415e8ebc08fSPeter Wemm #ifdef COMPAT_43
416645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
41723eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41823eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
419645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
420645682fdSLuoqi Chen 		else
421645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
422e8ebc08fSPeter Wemm #endif
423df8bae1dSRodney W. Grimes 	}
42490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
425628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4262c42a146SMarcel Moolenaar 	return (0);
4272c42a146SMarcel Moolenaar }
4282c42a146SMarcel Moolenaar 
4292c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4302c42a146SMarcel Moolenaar struct sigaction_args {
4312c42a146SMarcel Moolenaar 	int	sig;
4322c42a146SMarcel Moolenaar 	struct	sigaction *act;
4332c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4342c42a146SMarcel Moolenaar };
4352c42a146SMarcel Moolenaar #endif
436fb99ab88SMatthew Dillon /*
437fb99ab88SMatthew Dillon  * MPSAFE
438fb99ab88SMatthew Dillon  */
4392c42a146SMarcel Moolenaar int
440b40ce416SJulian Elischer sigaction(td, uap)
441b40ce416SJulian Elischer 	struct thread *td;
4422c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4432c42a146SMarcel Moolenaar {
4442c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4452c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4462c42a146SMarcel Moolenaar 	int error;
4472c42a146SMarcel Moolenaar 
4486f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4496f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4502c42a146SMarcel Moolenaar 	if (actp) {
4516f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4522c42a146SMarcel Moolenaar 		if (error)
45344443757STim J. Robbins 			return (error);
4542c42a146SMarcel Moolenaar 	}
4558f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
45625d6dc06SJohn Baldwin 	if (oactp && !error)
4576f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4582c42a146SMarcel Moolenaar 	return (error);
4592c42a146SMarcel Moolenaar }
4602c42a146SMarcel Moolenaar 
46123eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
46223eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
46323eeeff7SPeter Wemm struct freebsd4_sigaction_args {
46423eeeff7SPeter Wemm 	int	sig;
46523eeeff7SPeter Wemm 	struct	sigaction *act;
46623eeeff7SPeter Wemm 	struct	sigaction *oact;
46723eeeff7SPeter Wemm };
46823eeeff7SPeter Wemm #endif
46923eeeff7SPeter Wemm /*
47023eeeff7SPeter Wemm  * MPSAFE
47123eeeff7SPeter Wemm  */
47223eeeff7SPeter Wemm int
47323eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
47423eeeff7SPeter Wemm 	struct thread *td;
47523eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
47623eeeff7SPeter Wemm {
47723eeeff7SPeter Wemm 	struct sigaction act, oact;
47823eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
47923eeeff7SPeter Wemm 	int error;
48023eeeff7SPeter Wemm 
48123eeeff7SPeter Wemm 
48223eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
48323eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
48423eeeff7SPeter Wemm 	if (actp) {
48523eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
48623eeeff7SPeter Wemm 		if (error)
48744443757STim J. Robbins 			return (error);
48823eeeff7SPeter Wemm 	}
48923eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
490a14e1189SJohn Baldwin 	if (oactp && !error)
49123eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
49223eeeff7SPeter Wemm 	return (error);
49323eeeff7SPeter Wemm }
49423eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
49523eeeff7SPeter Wemm 
49631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4972c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4982c42a146SMarcel Moolenaar struct osigaction_args {
4992c42a146SMarcel Moolenaar 	int	signum;
5002c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
5012c42a146SMarcel Moolenaar 	struct	osigaction *osa;
5022c42a146SMarcel Moolenaar };
5032c42a146SMarcel Moolenaar #endif
504fb99ab88SMatthew Dillon /*
505fb99ab88SMatthew Dillon  * MPSAFE
506fb99ab88SMatthew Dillon  */
5072c42a146SMarcel Moolenaar int
508b40ce416SJulian Elischer osigaction(td, uap)
509b40ce416SJulian Elischer 	struct thread *td;
5102c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
5112c42a146SMarcel Moolenaar {
5122c42a146SMarcel Moolenaar 	struct osigaction sa;
5132c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5142c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5152c42a146SMarcel Moolenaar 	int error;
5162c42a146SMarcel Moolenaar 
5176f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5186f841fb7SMarcel Moolenaar 		return (EINVAL);
519fb99ab88SMatthew Dillon 
5206f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5216f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
522fb99ab88SMatthew Dillon 
5232c42a146SMarcel Moolenaar 	if (nsap) {
5246f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5252c42a146SMarcel Moolenaar 		if (error)
52644443757STim J. Robbins 			return (error);
5272c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5282c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5292c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5302c42a146SMarcel Moolenaar 	}
53123eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5322c42a146SMarcel Moolenaar 	if (osap && !error) {
5332c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5342c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5352c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5366f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5372c42a146SMarcel Moolenaar 	}
5382c42a146SMarcel Moolenaar 	return (error);
5392c42a146SMarcel Moolenaar }
54023eeeff7SPeter Wemm 
54123eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
54223eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
54323eeeff7SPeter Wemm int
54423eeeff7SPeter Wemm osigreturn(td, uap)
54523eeeff7SPeter Wemm 	struct thread *td;
54623eeeff7SPeter Wemm 	struct osigreturn_args *uap;
54723eeeff7SPeter Wemm {
54823eeeff7SPeter Wemm 
54923eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
55023eeeff7SPeter Wemm }
55123eeeff7SPeter Wemm #endif
55231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes /*
555df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
556df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
557df8bae1dSRodney W. Grimes  */
558df8bae1dSRodney W. Grimes void
559df8bae1dSRodney W. Grimes siginit(p)
560df8bae1dSRodney W. Grimes 	struct proc *p;
561df8bae1dSRodney W. Grimes {
562df8bae1dSRodney W. Grimes 	register int i;
56390af4afaSJohn Baldwin 	struct sigacts *ps;
564df8bae1dSRodney W. Grimes 
565628d2653SJohn Baldwin 	PROC_LOCK(p);
56690af4afaSJohn Baldwin 	ps = p->p_sigacts;
56790af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5682c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5692c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
57090af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
57190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
572628d2653SJohn Baldwin 	PROC_UNLOCK(p);
573df8bae1dSRodney W. Grimes }
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes /*
576df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
577df8bae1dSRodney W. Grimes  */
578df8bae1dSRodney W. Grimes void
579a30ec4b9SDavid Xu execsigs(struct proc *p)
580df8bae1dSRodney W. Grimes {
581a30ec4b9SDavid Xu 	struct sigacts *ps;
582a30ec4b9SDavid Xu 	int sig;
583a30ec4b9SDavid Xu 	struct thread *td;
584df8bae1dSRodney W. Grimes 
585df8bae1dSRodney W. Grimes 	/*
586df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5874093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
588df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
589df8bae1dSRodney W. Grimes 	 */
5909b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
591a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
592628d2653SJohn Baldwin 	ps = p->p_sigacts;
59390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
59490af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
59590af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
59690af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
5972c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5982c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
59990af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
6001d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
6014093529dSJeff Roberson 			/*
6024093529dSJeff Roberson 			 * There is only one thread at this point.
6034093529dSJeff Roberson 			 */
604a30ec4b9SDavid Xu 			SIGDELSET(td->td_siglist, sig);
605df8bae1dSRodney W. Grimes 		}
6062c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
607df8bae1dSRodney W. Grimes 	}
608df8bae1dSRodney W. Grimes 	/*
609df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
610df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
611df8bae1dSRodney W. Grimes 	 */
612a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
613a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
614a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
615a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
61680e907a1SPeter Wemm 	/*
61780e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
61880e907a1SPeter Wemm 	 */
61990af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
620c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
621c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
62290af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
623df8bae1dSRodney W. Grimes }
624df8bae1dSRodney W. Grimes 
625df8bae1dSRodney W. Grimes /*
626e77daab1SJohn Baldwin  * kern_sigprocmask()
6277c8fdcbdSMatthew Dillon  *
628628d2653SJohn Baldwin  *	Manipulate signal mask.
629df8bae1dSRodney W. Grimes  */
630e77daab1SJohn Baldwin int
631e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6324093529dSJeff Roberson 	struct thread *td;
6332c42a146SMarcel Moolenaar 	int how;
6342c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
635645682fdSLuoqi Chen 	int old;
6362c42a146SMarcel Moolenaar {
6372c42a146SMarcel Moolenaar 	int error;
6382c42a146SMarcel Moolenaar 
6394093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6402c42a146SMarcel Moolenaar 	if (oset != NULL)
6414093529dSJeff Roberson 		*oset = td->td_sigmask;
6422c42a146SMarcel Moolenaar 
6432c42a146SMarcel Moolenaar 	error = 0;
6442c42a146SMarcel Moolenaar 	if (set != NULL) {
6452c42a146SMarcel Moolenaar 		switch (how) {
6462c42a146SMarcel Moolenaar 		case SIG_BLOCK:
647645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6484093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6492c42a146SMarcel Moolenaar 			break;
6502c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6514093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6524093529dSJeff Roberson 			signotify(td);
6532c42a146SMarcel Moolenaar 			break;
6542c42a146SMarcel Moolenaar 		case SIG_SETMASK:
655645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
656645682fdSLuoqi Chen 			if (old)
6574093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
658645682fdSLuoqi Chen 			else
6594093529dSJeff Roberson 				td->td_sigmask = *set;
6604093529dSJeff Roberson 			signotify(td);
6612c42a146SMarcel Moolenaar 			break;
6622c42a146SMarcel Moolenaar 		default:
6632c42a146SMarcel Moolenaar 			error = EINVAL;
6642c42a146SMarcel Moolenaar 			break;
6652c42a146SMarcel Moolenaar 		}
6662c42a146SMarcel Moolenaar 	}
6674093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6682c42a146SMarcel Moolenaar 	return (error);
6692c42a146SMarcel Moolenaar }
6702c42a146SMarcel Moolenaar 
6717c8fdcbdSMatthew Dillon /*
672e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6737c8fdcbdSMatthew Dillon  */
6747c8fdcbdSMatthew Dillon 
675d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
676df8bae1dSRodney W. Grimes struct sigprocmask_args {
677df8bae1dSRodney W. Grimes 	int	how;
6782c42a146SMarcel Moolenaar 	const sigset_t *set;
6792c42a146SMarcel Moolenaar 	sigset_t *oset;
680df8bae1dSRodney W. Grimes };
681d2d3e875SBruce Evans #endif
68226f9a767SRodney W. Grimes int
683b40ce416SJulian Elischer sigprocmask(td, uap)
684b40ce416SJulian Elischer 	register struct thread *td;
685df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
686df8bae1dSRodney W. Grimes {
6872c42a146SMarcel Moolenaar 	sigset_t set, oset;
6882c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6892c42a146SMarcel Moolenaar 	int error;
690df8bae1dSRodney W. Grimes 
6916f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6926f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6932c42a146SMarcel Moolenaar 	if (setp) {
6946f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6952c42a146SMarcel Moolenaar 		if (error)
6962c42a146SMarcel Moolenaar 			return (error);
697df8bae1dSRodney W. Grimes 	}
698e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
6992c42a146SMarcel Moolenaar 	if (osetp && !error) {
7006f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
7012c42a146SMarcel Moolenaar 	}
7022c42a146SMarcel Moolenaar 	return (error);
7032c42a146SMarcel Moolenaar }
7042c42a146SMarcel Moolenaar 
70531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7067c8fdcbdSMatthew Dillon /*
7077c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
7087c8fdcbdSMatthew Dillon  */
7092c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7102c42a146SMarcel Moolenaar struct osigprocmask_args {
7112c42a146SMarcel Moolenaar 	int	how;
7122c42a146SMarcel Moolenaar 	osigset_t mask;
7132c42a146SMarcel Moolenaar };
7142c42a146SMarcel Moolenaar #endif
7152c42a146SMarcel Moolenaar int
716b40ce416SJulian Elischer osigprocmask(td, uap)
717b40ce416SJulian Elischer 	register struct thread *td;
7182c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7192c42a146SMarcel Moolenaar {
7202c42a146SMarcel Moolenaar 	sigset_t set, oset;
7212c42a146SMarcel Moolenaar 	int error;
7222c42a146SMarcel Moolenaar 
7232c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
724e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
725b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
726df8bae1dSRodney W. Grimes 	return (error);
727df8bae1dSRodney W. Grimes }
72831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
729df8bae1dSRodney W. Grimes 
730d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
731df8bae1dSRodney W. Grimes struct sigpending_args {
7322c42a146SMarcel Moolenaar 	sigset_t	*set;
733df8bae1dSRodney W. Grimes };
734d2d3e875SBruce Evans #endif
735fb99ab88SMatthew Dillon /*
736fb99ab88SMatthew Dillon  * MPSAFE
737fb99ab88SMatthew Dillon  */
73826f9a767SRodney W. Grimes int
739a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
740a447cd8bSJeff Roberson {
741a447cd8bSJeff Roberson 	siginfo_t info;
742a447cd8bSJeff Roberson 	sigset_t set;
743a447cd8bSJeff Roberson 	int error;
744a447cd8bSJeff Roberson 
745a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
74636939a0aSDavid Xu 	if (error) {
74736939a0aSDavid Xu 		td->td_retval[0] = error;
74836939a0aSDavid Xu 		return (0);
74936939a0aSDavid Xu 	}
750a447cd8bSJeff Roberson 
751a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
75236939a0aSDavid Xu 	if (error) {
75336939a0aSDavid Xu 		if (error == ERESTART)
754a447cd8bSJeff Roberson 			return (error);
75536939a0aSDavid Xu 		td->td_retval[0] = error;
75636939a0aSDavid Xu 		return (0);
75736939a0aSDavid Xu 	}
758a447cd8bSJeff Roberson 
759a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
760a447cd8bSJeff Roberson 	/* Repost if we got an error. */
76118440c7fSJohn Baldwin 	if (error && info.si_signo) {
76218440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
763c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
76418440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
76518440c7fSJohn Baldwin 	}
76636939a0aSDavid Xu 	td->td_retval[0] = error;
76736939a0aSDavid Xu 	return (0);
768a447cd8bSJeff Roberson }
769a447cd8bSJeff Roberson /*
770a447cd8bSJeff Roberson  * MPSAFE
771a447cd8bSJeff Roberson  */
772a447cd8bSJeff Roberson int
773a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
774a447cd8bSJeff Roberson {
775a447cd8bSJeff Roberson 	struct timespec ts;
776a447cd8bSJeff Roberson 	struct timespec *timeout;
777a447cd8bSJeff Roberson 	sigset_t set;
778a447cd8bSJeff Roberson 	siginfo_t info;
779a447cd8bSJeff Roberson 	int error;
780a447cd8bSJeff Roberson 
781a447cd8bSJeff Roberson 	if (uap->timeout) {
782a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
783a447cd8bSJeff Roberson 		if (error)
784a447cd8bSJeff Roberson 			return (error);
785a447cd8bSJeff Roberson 
786a447cd8bSJeff Roberson 		timeout = &ts;
787a447cd8bSJeff Roberson 	} else
788a447cd8bSJeff Roberson 		timeout = NULL;
789a447cd8bSJeff Roberson 
790a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
791a447cd8bSJeff Roberson 	if (error)
792a447cd8bSJeff Roberson 		return (error);
793a447cd8bSJeff Roberson 
794a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
795a447cd8bSJeff Roberson 	if (error)
796a447cd8bSJeff Roberson 		return (error);
7979dde3bc9SDavid Xu 
798418228dfSDavid Xu 	if (uap->info)
799a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
800a447cd8bSJeff Roberson 	/* Repost if we got an error. */
80118440c7fSJohn Baldwin 	if (error && info.si_signo) {
80218440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
803c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
80418440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
805418228dfSDavid Xu 	} else {
806418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
80718440c7fSJohn Baldwin 	}
808a447cd8bSJeff Roberson 	return (error);
809a447cd8bSJeff Roberson }
810a447cd8bSJeff Roberson 
811a447cd8bSJeff Roberson /*
812a447cd8bSJeff Roberson  * MPSAFE
813a447cd8bSJeff Roberson  */
814a447cd8bSJeff Roberson int
815a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
816a447cd8bSJeff Roberson {
817a447cd8bSJeff Roberson 	siginfo_t info;
818a447cd8bSJeff Roberson 	sigset_t set;
819a447cd8bSJeff Roberson 	int error;
820a447cd8bSJeff Roberson 
821a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
822a447cd8bSJeff Roberson 	if (error)
823a447cd8bSJeff Roberson 		return (error);
824a447cd8bSJeff Roberson 
825a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
826a447cd8bSJeff Roberson 	if (error)
827a447cd8bSJeff Roberson 		return (error);
828a447cd8bSJeff Roberson 
829418228dfSDavid Xu 	if (uap->info)
830a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
831a447cd8bSJeff Roberson 	/* Repost if we got an error. */
83218440c7fSJohn Baldwin 	if (error && info.si_signo) {
83318440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
834c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
83518440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
836418228dfSDavid Xu 	} else {
837418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
83818440c7fSJohn Baldwin 	}
839a447cd8bSJeff Roberson 	return (error);
840a447cd8bSJeff Roberson }
841a447cd8bSJeff Roberson 
842a447cd8bSJeff Roberson static int
8433074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
844a447cd8bSJeff Roberson     struct timespec *timeout)
845a447cd8bSJeff Roberson {
8463074d1b4SDavid Xu 	struct sigacts *ps;
8473074d1b4SDavid Xu 	sigset_t savedmask, sigset;
848a447cd8bSJeff Roberson 	struct proc *p;
849a447cd8bSJeff Roberson 	int error;
850a447cd8bSJeff Roberson 	int sig;
851a447cd8bSJeff Roberson 	int hz;
8523074d1b4SDavid Xu 	int i;
853a447cd8bSJeff Roberson 
854a447cd8bSJeff Roberson 	p = td->td_proc;
855a447cd8bSJeff Roberson 	error = 0;
856a447cd8bSJeff Roberson 	sig = 0;
8573074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
858a447cd8bSJeff Roberson 
859a447cd8bSJeff Roberson 	PROC_LOCK(p);
860a447cd8bSJeff Roberson 	ps = p->p_sigacts;
8613074d1b4SDavid Xu 	savedmask = td->td_sigmask;
8623074d1b4SDavid Xu 
8633074d1b4SDavid Xu again:
8643074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
8653074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
8663074d1b4SDavid Xu 			continue;
8673074d1b4SDavid Xu 		if (SIGISMEMBER(td->td_siglist, i)) {
868418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
869418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8703074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
87190af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
872a447cd8bSJeff Roberson 			sig = cursig(td);
8733074d1b4SDavid Xu 			i = 0;
8743074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8753074d1b4SDavid Xu 		} else if (SIGISMEMBER(p->p_siglist, i)) {
8763074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
8773074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
8783074d1b4SDavid Xu 				wakeup(&p->p_siglist);
8793074d1b4SDavid Xu 			}
8803074d1b4SDavid Xu 			SIGDELSET(p->p_siglist, i);
8813074d1b4SDavid Xu 			SIGADDSET(td->td_siglist, i);
8823074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
8833074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8843074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
8853074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
8863074d1b4SDavid Xu 			sig = cursig(td);
8873074d1b4SDavid Xu 			i = 0;
8883074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8893074d1b4SDavid Xu 		}
8903074d1b4SDavid Xu 		if (sig) {
8913074d1b4SDavid Xu 			td->td_sigmask = savedmask;
8923074d1b4SDavid Xu 			signotify(td);
893a447cd8bSJeff Roberson 			goto out;
8943074d1b4SDavid Xu 		}
8953074d1b4SDavid Xu 	}
8963074d1b4SDavid Xu 	if (error)
8973074d1b4SDavid Xu 		goto out;
8983074d1b4SDavid Xu 
8993074d1b4SDavid Xu 	td->td_sigmask = savedmask;
9003074d1b4SDavid Xu 	signotify(td);
9013074d1b4SDavid Xu 	sigset = td->td_siglist;
9023074d1b4SDavid Xu 	SIGSETOR(sigset, p->p_siglist);
9033074d1b4SDavid Xu 	SIGSETAND(sigset, waitset);
9043074d1b4SDavid Xu 	if (!SIGISEMPTY(sigset))
9053074d1b4SDavid Xu 		goto again;
906a447cd8bSJeff Roberson 
907a447cd8bSJeff Roberson 	/*
908a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
909a447cd8bSJeff Roberson 	 * signals.
910a447cd8bSJeff Roberson 	 */
911a447cd8bSJeff Roberson 	if (timeout) {
912a447cd8bSJeff Roberson 		struct timeval tv;
913a447cd8bSJeff Roberson 
914a6ca4808SMike Makonnen 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
915a447cd8bSJeff Roberson 			error = EINVAL;
916a447cd8bSJeff Roberson 			goto out;
917a447cd8bSJeff Roberson 		}
9183074d1b4SDavid Xu 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
9193074d1b4SDavid Xu 			error = EAGAIN;
9203074d1b4SDavid Xu 			goto out;
9213074d1b4SDavid Xu 		}
922a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
923a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
924a447cd8bSJeff Roberson 	} else
925a447cd8bSJeff Roberson 		hz = 0;
926a447cd8bSJeff Roberson 
9273074d1b4SDavid Xu 	td->td_waitset = &waitset;
92886b5e563SDag-Erling Smørgrav 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
9293074d1b4SDavid Xu 	td->td_waitset = NULL;
9303074d1b4SDavid Xu 	if (error == 0) /* surplus wakeup ? */
9313074d1b4SDavid Xu 		error = EINTR;
9323074d1b4SDavid Xu 	goto again;
9339dde3bc9SDavid Xu 
934a447cd8bSJeff Roberson out:
935a447cd8bSJeff Roberson 	if (sig) {
936a447cd8bSJeff Roberson 		sig_t action;
937a447cd8bSJeff Roberson 
9383074d1b4SDavid Xu 		error = 0;
9393074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
940a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
94190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
942a447cd8bSJeff Roberson #ifdef KTRACE
943a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
9443074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
945a447cd8bSJeff Roberson #endif
946a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
947a447cd8bSJeff Roberson 
948a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
949a447cd8bSJeff Roberson 		info->si_signo = sig;
950a447cd8bSJeff Roberson 		info->si_code = 0;
9513074d1b4SDavid Xu 	}
952a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
953a447cd8bSJeff Roberson 	return (error);
954a447cd8bSJeff Roberson }
955a447cd8bSJeff Roberson 
956a447cd8bSJeff Roberson /*
957a447cd8bSJeff Roberson  * MPSAFE
958a447cd8bSJeff Roberson  */
959a447cd8bSJeff Roberson int
960b40ce416SJulian Elischer sigpending(td, uap)
961b40ce416SJulian Elischer 	struct thread *td;
962df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
963df8bae1dSRodney W. Grimes {
964b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
965628d2653SJohn Baldwin 	sigset_t siglist;
966df8bae1dSRodney W. Grimes 
967628d2653SJohn Baldwin 	PROC_LOCK(p);
9681d9c5696SJuli Mallett 	siglist = p->p_siglist;
9694093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
970628d2653SJohn Baldwin 	PROC_UNLOCK(p);
97148e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9722c42a146SMarcel Moolenaar }
9732c42a146SMarcel Moolenaar 
97431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9752c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9762c42a146SMarcel Moolenaar struct osigpending_args {
9772c42a146SMarcel Moolenaar 	int	dummy;
9782c42a146SMarcel Moolenaar };
9792c42a146SMarcel Moolenaar #endif
980fb99ab88SMatthew Dillon /*
981fb99ab88SMatthew Dillon  * MPSAFE
982fb99ab88SMatthew Dillon  */
9832c42a146SMarcel Moolenaar int
984b40ce416SJulian Elischer osigpending(td, uap)
985b40ce416SJulian Elischer 	struct thread *td;
9862c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9872c42a146SMarcel Moolenaar {
988b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9894093529dSJeff Roberson 	sigset_t siglist;
990b40ce416SJulian Elischer 
991628d2653SJohn Baldwin 	PROC_LOCK(p);
9924093529dSJeff Roberson 	siglist = p->p_siglist;
9934093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
994628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9959d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
996df8bae1dSRodney W. Grimes 	return (0);
997df8bae1dSRodney W. Grimes }
99831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
999df8bae1dSRodney W. Grimes 
10001930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1001df8bae1dSRodney W. Grimes /*
1002df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1003df8bae1dSRodney W. Grimes  */
1004d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1005df8bae1dSRodney W. Grimes struct osigvec_args {
1006df8bae1dSRodney W. Grimes 	int	signum;
1007df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1008df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1009df8bae1dSRodney W. Grimes };
1010d2d3e875SBruce Evans #endif
1011fb99ab88SMatthew Dillon /*
1012fb99ab88SMatthew Dillon  * MPSAFE
1013fb99ab88SMatthew Dillon  */
1014df8bae1dSRodney W. Grimes /* ARGSUSED */
101526f9a767SRodney W. Grimes int
1016b40ce416SJulian Elischer osigvec(td, uap)
1017b40ce416SJulian Elischer 	struct thread *td;
1018df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1019df8bae1dSRodney W. Grimes {
1020df8bae1dSRodney W. Grimes 	struct sigvec vec;
10212c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
10222c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
10232c42a146SMarcel Moolenaar 	int error;
1024df8bae1dSRodney W. Grimes 
10256f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10266f841fb7SMarcel Moolenaar 		return (EINVAL);
10276f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
10286f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
10292c42a146SMarcel Moolenaar 	if (nsap) {
10306f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
10312c42a146SMarcel Moolenaar 		if (error)
1032df8bae1dSRodney W. Grimes 			return (error);
10332c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
10342c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
10352c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
10362c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1037df8bae1dSRodney W. Grimes 	}
10385edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10392c42a146SMarcel Moolenaar 	if (osap && !error) {
10402c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
10412c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
10422c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
10432c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
10442c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
10456f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10462c42a146SMarcel Moolenaar 	}
10472c42a146SMarcel Moolenaar 	return (error);
1048df8bae1dSRodney W. Grimes }
1049df8bae1dSRodney W. Grimes 
1050d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1051df8bae1dSRodney W. Grimes struct osigblock_args {
1052df8bae1dSRodney W. Grimes 	int	mask;
1053df8bae1dSRodney W. Grimes };
1054d2d3e875SBruce Evans #endif
1055fb99ab88SMatthew Dillon /*
1056fb99ab88SMatthew Dillon  * MPSAFE
1057fb99ab88SMatthew Dillon  */
105826f9a767SRodney W. Grimes int
1059b40ce416SJulian Elischer osigblock(td, uap)
1060b40ce416SJulian Elischer 	register struct thread *td;
1061df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1062df8bae1dSRodney W. Grimes {
1063b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10642c42a146SMarcel Moolenaar 	sigset_t set;
1065df8bae1dSRodney W. Grimes 
10662c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10672c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1068628d2653SJohn Baldwin 	PROC_LOCK(p);
10694093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10704093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1071628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1072df8bae1dSRodney W. Grimes 	return (0);
1073df8bae1dSRodney W. Grimes }
1074df8bae1dSRodney W. Grimes 
1075d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1076df8bae1dSRodney W. Grimes struct osigsetmask_args {
1077df8bae1dSRodney W. Grimes 	int	mask;
1078df8bae1dSRodney W. Grimes };
1079d2d3e875SBruce Evans #endif
1080fb99ab88SMatthew Dillon /*
1081fb99ab88SMatthew Dillon  * MPSAFE
1082fb99ab88SMatthew Dillon  */
108326f9a767SRodney W. Grimes int
1084b40ce416SJulian Elischer osigsetmask(td, uap)
1085b40ce416SJulian Elischer 	struct thread *td;
1086df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1087df8bae1dSRodney W. Grimes {
1088b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10892c42a146SMarcel Moolenaar 	sigset_t set;
1090df8bae1dSRodney W. Grimes 
10912c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10922c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1093628d2653SJohn Baldwin 	PROC_LOCK(p);
10944093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10954093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10964093529dSJeff Roberson 	signotify(td);
1097628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1098df8bae1dSRodney W. Grimes 	return (0);
1099df8bae1dSRodney W. Grimes }
11001930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1101df8bae1dSRodney W. Grimes 
1102df8bae1dSRodney W. Grimes /*
1103df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
110497563428SAlexander Kabaev  * in the meantime.
1105b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1106b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1107b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1108df8bae1dSRodney W. Grimes  */
1109d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1110df8bae1dSRodney W. Grimes struct sigsuspend_args {
11112c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1112df8bae1dSRodney W. Grimes };
1113d2d3e875SBruce Evans #endif
1114fb99ab88SMatthew Dillon /*
1115fb99ab88SMatthew Dillon  * MPSAFE
1116fb99ab88SMatthew Dillon  */
1117df8bae1dSRodney W. Grimes /* ARGSUSED */
111826f9a767SRodney W. Grimes int
1119b40ce416SJulian Elischer sigsuspend(td, uap)
1120b40ce416SJulian Elischer 	struct thread *td;
1121df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1122df8bae1dSRodney W. Grimes {
11232c42a146SMarcel Moolenaar 	sigset_t mask;
11242c42a146SMarcel Moolenaar 	int error;
11252c42a146SMarcel Moolenaar 
11266f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11272c42a146SMarcel Moolenaar 	if (error)
11282c42a146SMarcel Moolenaar 		return (error);
11298f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11308f19eb88SIan Dowse }
11318f19eb88SIan Dowse 
11328f19eb88SIan Dowse int
11338f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11348f19eb88SIan Dowse {
11358f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1136df8bae1dSRodney W. Grimes 
1137df8bae1dSRodney W. Grimes 	/*
1138645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1139df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1140df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1141df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1142df8bae1dSRodney W. Grimes 	 * to indicate this.
1143df8bae1dSRodney W. Grimes 	 */
1144628d2653SJohn Baldwin 	PROC_LOCK(p);
11454093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11465e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1147645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11484093529dSJeff Roberson 	td->td_sigmask = mask;
11494093529dSJeff Roberson 	signotify(td);
115086b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11512c42a146SMarcel Moolenaar 		/* void */;
1152628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11532c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11542c42a146SMarcel Moolenaar 	return (EINTR);
11552c42a146SMarcel Moolenaar }
11562c42a146SMarcel Moolenaar 
115731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
115897563428SAlexander Kabaev /*
115997563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
116097563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
116197563428SAlexander Kabaev  */
11622c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11632c42a146SMarcel Moolenaar struct osigsuspend_args {
11642c42a146SMarcel Moolenaar 	osigset_t mask;
11652c42a146SMarcel Moolenaar };
11662c42a146SMarcel Moolenaar #endif
1167fb99ab88SMatthew Dillon /*
1168fb99ab88SMatthew Dillon  * MPSAFE
1169fb99ab88SMatthew Dillon  */
11702c42a146SMarcel Moolenaar /* ARGSUSED */
11712c42a146SMarcel Moolenaar int
1172b40ce416SJulian Elischer osigsuspend(td, uap)
1173b40ce416SJulian Elischer 	struct thread *td;
11742c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11752c42a146SMarcel Moolenaar {
1176b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1177645682fdSLuoqi Chen 	sigset_t mask;
11782c42a146SMarcel Moolenaar 
1179628d2653SJohn Baldwin 	PROC_LOCK(p);
11804093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11815e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1182645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1183645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11844093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11854093529dSJeff Roberson 	signotify(td);
118686b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1187df8bae1dSRodney W. Grimes 		/* void */;
1188628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1189df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1190df8bae1dSRodney W. Grimes 	return (EINTR);
1191df8bae1dSRodney W. Grimes }
119231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1193df8bae1dSRodney W. Grimes 
11941930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1195d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1196df8bae1dSRodney W. Grimes struct osigstack_args {
1197df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1198df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1199df8bae1dSRodney W. Grimes };
1200d2d3e875SBruce Evans #endif
1201fb99ab88SMatthew Dillon /*
1202fb99ab88SMatthew Dillon  * MPSAFE
1203fb99ab88SMatthew Dillon  */
1204df8bae1dSRodney W. Grimes /* ARGSUSED */
120526f9a767SRodney W. Grimes int
1206b40ce416SJulian Elischer osigstack(td, uap)
1207b40ce416SJulian Elischer 	struct thread *td;
1208df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1209df8bae1dSRodney W. Grimes {
12105afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1211fb99ab88SMatthew Dillon 	int error = 0;
1212fb99ab88SMatthew Dillon 
1213d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12145afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12155afe0c99SJohn Baldwin 		if (error)
12165afe0c99SJohn Baldwin 			return (error);
1217df8bae1dSRodney W. Grimes 	}
1218a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12195afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12205afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1221a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1222a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1223a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1224a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12255afe0c99SJohn Baldwin 	}
12265afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12275afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12285afe0c99SJohn Baldwin 
1229fb99ab88SMatthew Dillon 	return (error);
1230df8bae1dSRodney W. Grimes }
12311930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1232df8bae1dSRodney W. Grimes 
1233d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1234df8bae1dSRodney W. Grimes struct sigaltstack_args {
12352c42a146SMarcel Moolenaar 	stack_t	*ss;
12362c42a146SMarcel Moolenaar 	stack_t	*oss;
1237df8bae1dSRodney W. Grimes };
1238d2d3e875SBruce Evans #endif
1239fb99ab88SMatthew Dillon /*
1240fb99ab88SMatthew Dillon  * MPSAFE
1241fb99ab88SMatthew Dillon  */
1242df8bae1dSRodney W. Grimes /* ARGSUSED */
124326f9a767SRodney W. Grimes int
1244b40ce416SJulian Elischer sigaltstack(td, uap)
1245b40ce416SJulian Elischer 	struct thread *td;
1246df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1247df8bae1dSRodney W. Grimes {
12488f19eb88SIan Dowse 	stack_t ss, oss;
12498f19eb88SIan Dowse 	int error;
12508f19eb88SIan Dowse 
12518f19eb88SIan Dowse 	if (uap->ss != NULL) {
12528f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12538f19eb88SIan Dowse 		if (error)
12548f19eb88SIan Dowse 			return (error);
12558f19eb88SIan Dowse 	}
12568f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12578f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12588f19eb88SIan Dowse 	if (error)
12598f19eb88SIan Dowse 		return (error);
12608f19eb88SIan Dowse 	if (uap->oss != NULL)
12618f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12628f19eb88SIan Dowse 	return (error);
12638f19eb88SIan Dowse }
12648f19eb88SIan Dowse 
12658f19eb88SIan Dowse int
12668f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12678f19eb88SIan Dowse {
1268b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1269fb99ab88SMatthew Dillon 	int oonstack;
1270fb99ab88SMatthew Dillon 
1271b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1272d034d459SMarcel Moolenaar 
12738f19eb88SIan Dowse 	if (oss != NULL) {
1274a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1275a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1276d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1277df8bae1dSRodney W. Grimes 	}
1278d034d459SMarcel Moolenaar 
12798f19eb88SIan Dowse 	if (ss != NULL) {
1280a30ec4b9SDavid Xu 		if (oonstack)
1281cf60731bSJohn Baldwin 			return (EPERM);
1282a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1283cf60731bSJohn Baldwin 			return (EINVAL);
12848f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12858f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1286cf60731bSJohn Baldwin 				return (ENOMEM);
1287fb99ab88SMatthew Dillon 			}
1288a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1289a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1290628d2653SJohn Baldwin 		} else {
1291a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1292628d2653SJohn Baldwin 		}
1293d034d459SMarcel Moolenaar 	}
1294cf60731bSJohn Baldwin 	return (0);
1295df8bae1dSRodney W. Grimes }
1296df8bae1dSRodney W. Grimes 
1297d93f860cSPoul-Henning Kamp /*
1298d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1299d93f860cSPoul-Henning Kamp  * cp is calling process.
1300d93f860cSPoul-Henning Kamp  */
130137c84183SPoul-Henning Kamp static int
13029c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
13039c1ab3e0SJohn Baldwin 	register struct thread *td;
13042c42a146SMarcel Moolenaar 	int sig, pgid, all;
1305d93f860cSPoul-Henning Kamp {
1306d93f860cSPoul-Henning Kamp 	register struct proc *p;
1307d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1308d93f860cSPoul-Henning Kamp 	int nfound = 0;
1309d93f860cSPoul-Henning Kamp 
1310553629ebSJake Burkholder 	if (all) {
1311d93f860cSPoul-Henning Kamp 		/*
1312d93f860cSPoul-Henning Kamp 		 * broadcast
1313d93f860cSPoul-Henning Kamp 		 */
13141005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13152e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1316628d2653SJohn Baldwin 			PROC_LOCK(p);
13179c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13189c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1319628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1320628d2653SJohn Baldwin 				continue;
1321628d2653SJohn Baldwin 			}
1322f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1323d93f860cSPoul-Henning Kamp 				nfound++;
132433a9ed9dSJohn Baldwin 				if (sig)
13252c42a146SMarcel Moolenaar 					psignal(p, sig);
1326628d2653SJohn Baldwin 			}
132733a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1328d93f860cSPoul-Henning Kamp 		}
13291005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1330553629ebSJake Burkholder 	} else {
1331ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1332f591779bSSeigo Tanimura 		if (pgid == 0) {
1333d93f860cSPoul-Henning Kamp 			/*
1334d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1335d93f860cSPoul-Henning Kamp 			 */
13369c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1337f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1338f591779bSSeigo Tanimura 		} else {
1339d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1340f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1341ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1342d93f860cSPoul-Henning Kamp 				return (ESRCH);
1343d93f860cSPoul-Henning Kamp 			}
1344f591779bSSeigo Tanimura 		}
1345ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13462e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1347628d2653SJohn Baldwin 			PROC_LOCK(p);
1348628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1349628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1350628d2653SJohn Baldwin 				continue;
1351628d2653SJohn Baldwin 			}
1352f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1353d93f860cSPoul-Henning Kamp 				nfound++;
135433a9ed9dSJohn Baldwin 				if (sig)
13552c42a146SMarcel Moolenaar 					psignal(p, sig);
1356628d2653SJohn Baldwin 			}
135733a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1358d93f860cSPoul-Henning Kamp 		}
1359f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1360d93f860cSPoul-Henning Kamp 	}
1361d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1362d93f860cSPoul-Henning Kamp }
1363d93f860cSPoul-Henning Kamp 
1364d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1365df8bae1dSRodney W. Grimes struct kill_args {
1366df8bae1dSRodney W. Grimes 	int	pid;
1367df8bae1dSRodney W. Grimes 	int	signum;
1368df8bae1dSRodney W. Grimes };
1369d2d3e875SBruce Evans #endif
1370fb99ab88SMatthew Dillon /*
1371fb99ab88SMatthew Dillon  * MPSAFE
1372fb99ab88SMatthew Dillon  */
1373df8bae1dSRodney W. Grimes /* ARGSUSED */
137426f9a767SRodney W. Grimes int
1375b40ce416SJulian Elischer kill(td, uap)
1376b40ce416SJulian Elischer 	register struct thread *td;
1377df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1378df8bae1dSRodney W. Grimes {
1379df8bae1dSRodney W. Grimes 	register struct proc *p;
138090af4afaSJohn Baldwin 	int error;
1381df8bae1dSRodney W. Grimes 
13826c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1383df8bae1dSRodney W. Grimes 		return (EINVAL);
1384fb99ab88SMatthew Dillon 
1385df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1386df8bae1dSRodney W. Grimes 		/* kill single process */
13870b011ea3SPawel Jakub Dawidek 		if ((p = pfind(uap->pid)) == NULL) {
138824b2151fSPawel Jakub Dawidek 			if ((p = zpfind(uap->pid)) == NULL)
138990af4afaSJohn Baldwin 				return (ESRCH);
13900b011ea3SPawel Jakub Dawidek 		}
139190af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
139290af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1393df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1394628d2653SJohn Baldwin 		PROC_UNLOCK(p);
139590af4afaSJohn Baldwin 		return (error);
1396df8bae1dSRodney W. Grimes 	}
1397df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1398df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
139990af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1400df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
140190af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1402df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
140390af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1404df8bae1dSRodney W. Grimes 	}
140590af4afaSJohn Baldwin 	/* NOTREACHED */
1406df8bae1dSRodney W. Grimes }
1407df8bae1dSRodney W. Grimes 
14081930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1409d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1410df8bae1dSRodney W. Grimes struct okillpg_args {
1411df8bae1dSRodney W. Grimes 	int	pgid;
1412df8bae1dSRodney W. Grimes 	int	signum;
1413df8bae1dSRodney W. Grimes };
1414d2d3e875SBruce Evans #endif
1415fb99ab88SMatthew Dillon /*
1416fb99ab88SMatthew Dillon  * MPSAFE
1417fb99ab88SMatthew Dillon  */
1418df8bae1dSRodney W. Grimes /* ARGSUSED */
141926f9a767SRodney W. Grimes int
1420b40ce416SJulian Elischer okillpg(td, uap)
1421b40ce416SJulian Elischer 	struct thread *td;
1422df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1423df8bae1dSRodney W. Grimes {
1424df8bae1dSRodney W. Grimes 
14256c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1426df8bae1dSRodney W. Grimes 		return (EINVAL);
142790af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1428df8bae1dSRodney W. Grimes }
14291930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1430df8bae1dSRodney W. Grimes 
1431df8bae1dSRodney W. Grimes /*
1432df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1433df8bae1dSRodney W. Grimes  */
1434df8bae1dSRodney W. Grimes void
14352c42a146SMarcel Moolenaar gsignal(pgid, sig)
14362c42a146SMarcel Moolenaar 	int pgid, sig;
1437df8bae1dSRodney W. Grimes {
1438df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1439df8bae1dSRodney W. Grimes 
1440f591779bSSeigo Tanimura 	if (pgid != 0) {
1441ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1442f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1443ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1444f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14452c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1446f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1447f591779bSSeigo Tanimura 		}
1448f591779bSSeigo Tanimura 	}
1449df8bae1dSRodney W. Grimes }
1450df8bae1dSRodney W. Grimes 
1451df8bae1dSRodney W. Grimes /*
1452df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1453df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1454df8bae1dSRodney W. Grimes  */
1455df8bae1dSRodney W. Grimes void
14562c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1457df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14582c42a146SMarcel Moolenaar 	int sig, checkctty;
1459df8bae1dSRodney W. Grimes {
1460df8bae1dSRodney W. Grimes 	register struct proc *p;
1461df8bae1dSRodney W. Grimes 
1462628d2653SJohn Baldwin 	if (pgrp) {
1463f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1464628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1465628d2653SJohn Baldwin 			PROC_LOCK(p);
1466df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14672c42a146SMarcel Moolenaar 				psignal(p, sig);
1468628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1469628d2653SJohn Baldwin 		}
1470628d2653SJohn Baldwin 	}
1471df8bae1dSRodney W. Grimes }
1472df8bae1dSRodney W. Grimes 
1473df8bae1dSRodney W. Grimes /*
14741bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1475df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1476df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1477356861dbSMatthew Dillon  *
1478356861dbSMatthew Dillon  * MPSAFE
1479df8bae1dSRodney W. Grimes  */
1480df8bae1dSRodney W. Grimes void
14811bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1482df8bae1dSRodney W. Grimes {
14831bf4700bSJeff Roberson 	struct sigacts *ps;
14841bf4700bSJeff Roberson 	struct proc *p;
14859dde3bc9SDavid Xu 	siginfo_t siginfo;
14869dde3bc9SDavid Xu 	int error;
14871bf4700bSJeff Roberson 
14881bf4700bSJeff Roberson 	p = td->td_proc;
1489aa0aa7a1STim J. Robbins 	if (td->td_pflags & TDP_SA) {
14907eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
14915995adc2SJulian Elischer 			thread_user_enter(td);
1492628d2653SJohn Baldwin 		PROC_LOCK(p);
14939dde3bc9SDavid Xu 		SIGDELSET(td->td_sigmask, sig);
14949dde3bc9SDavid Xu 		mtx_lock_spin(&sched_lock);
14959dde3bc9SDavid Xu 		/*
14969dde3bc9SDavid Xu 		 * Force scheduling an upcall, so UTS has chance to
14979dde3bc9SDavid Xu 		 * process the signal before thread runs again in
14989dde3bc9SDavid Xu 		 * userland.
14999dde3bc9SDavid Xu 		 */
15009dde3bc9SDavid Xu 		if (td->td_upcall)
15019dde3bc9SDavid Xu 			td->td_upcall->ku_flags |= KUF_DOUPCALL;
15029dde3bc9SDavid Xu 		mtx_unlock_spin(&sched_lock);
1503432b45deSDavid Xu 	} else {
15049dde3bc9SDavid Xu 		PROC_LOCK(p);
15059dde3bc9SDavid Xu 	}
1506ef3dab76STim J. Robbins 	ps = p->p_sigacts;
150790af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
150890af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15094093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1510df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1511df8bae1dSRodney W. Grimes #ifdef KTRACE
1512374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1513374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15144093529dSJeff Roberson 			    &td->td_sigmask, code);
1515df8bae1dSRodney W. Grimes #endif
1516aa0aa7a1STim J. Robbins 		if (!(td->td_pflags & TDP_SA))
15179dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15189dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15194093529dSJeff Roberson 				&td->td_sigmask, code);
1520cbf4e354SDavid Xu 		else if (td->td_mailbox == NULL) {
1521cbf4e354SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1522cbf4e354SDavid Xu 			/* UTS caused a sync signal */
1523cbf4e354SDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
1524cbf4e354SDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
1525cbf4e354SDavid Xu 			sigexit(td, sig);
1526cbf4e354SDavid Xu 		} else {
15274b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15289dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1529cbf4e354SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
15309dde3bc9SDavid Xu 			PROC_UNLOCK(p);
15319dde3bc9SDavid Xu 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
15329dde3bc9SDavid Xu 			    sizeof(siginfo));
15339dde3bc9SDavid Xu 			PROC_LOCK(p);
1534432b45deSDavid Xu 			/* UTS memory corrupted */
15359dde3bc9SDavid Xu 			if (error)
1536cbf4e354SDavid Xu 				sigexit(td, SIGSEGV);
15379dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15389dde3bc9SDavid Xu 		}
15394093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15402c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15414093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15422c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1543289ccde0SPeter Wemm 			/*
15448f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1545289ccde0SPeter Wemm 			 */
154690af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15472c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15482c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
154990af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15502c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1551dedc04feSPeter Wemm 		}
155290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15536f841fb7SMarcel Moolenaar 	} else {
155490af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15556626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15562c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1557c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1558df8bae1dSRodney W. Grimes 	}
1559628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1560df8bae1dSRodney W. Grimes }
1561df8bae1dSRodney W. Grimes 
15624093529dSJeff Roberson static struct thread *
15634093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15644093529dSJeff Roberson {
15653074d1b4SDavid Xu 	struct thread *td, *signal_td;
15664093529dSJeff Roberson 
15674093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15684093529dSJeff Roberson 
15694093529dSJeff Roberson 	/*
15703074d1b4SDavid Xu 	 * First find a thread in sigwait state and signal belongs to
15713074d1b4SDavid Xu 	 * its wait set. POSIX's arguments is that speed of delivering signal
15723074d1b4SDavid Xu 	 * to sigwait thread is faster than delivering signal to user stack.
15733074d1b4SDavid Xu 	 * If we can not find sigwait thread, then find the first thread in
15743074d1b4SDavid Xu 	 * the proc that doesn't have this signal masked, an exception is
15753074d1b4SDavid Xu 	 * if current thread is sending signal to its process, and it does not
15763074d1b4SDavid Xu 	 * mask the signal, it should get the signal, this is another fast
15773074d1b4SDavid Xu 	 * way to deliver signal.
15784093529dSJeff Roberson 	 */
15793074d1b4SDavid Xu 	signal_td = NULL;
1580a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15813074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15823074d1b4SDavid Xu 		if (td->td_waitset != NULL &&
1583a9a48d68SDavid Xu 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1584a9a48d68SDavid Xu 				mtx_unlock_spin(&sched_lock);
15854093529dSJeff Roberson 				return (td);
1586a9a48d68SDavid Xu 		}
15873074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15883074d1b4SDavid Xu 			if (td == curthread)
15893074d1b4SDavid Xu 				signal_td = curthread;
15903074d1b4SDavid Xu 			else if (signal_td == NULL)
15913074d1b4SDavid Xu 				signal_td = td;
15923074d1b4SDavid Xu 		}
15933074d1b4SDavid Xu 	}
15943074d1b4SDavid Xu 	if (signal_td == NULL)
15953074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1596a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
15973074d1b4SDavid Xu 	return (signal_td);
15984093529dSJeff Roberson }
15994093529dSJeff Roberson 
1600df8bae1dSRodney W. Grimes /*
1601df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1602df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1603df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1604df8bae1dSRodney W. Grimes  *
1605df8bae1dSRodney W. Grimes  * Exceptions:
1606df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1607df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1608df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1609df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1610df8bae1dSRodney W. Grimes  *
1611df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
161290af4afaSJohn Baldwin  *
161390af4afaSJohn Baldwin  * MPSAFE
1614df8bae1dSRodney W. Grimes  */
1615df8bae1dSRodney W. Grimes void
16164093529dSJeff Roberson psignal(struct proc *p, int sig)
1617df8bae1dSRodney W. Grimes {
1618b40ce416SJulian Elischer 	struct thread *td;
16194093529dSJeff Roberson 	int prop;
16204093529dSJeff Roberson 
162141b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
162241b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
162341b3077aSJacques Vidrine 
16244093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
162524b2151fSPawel Jakub Dawidek 	/*
162624b2151fSPawel Jakub Dawidek 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
162724b2151fSPawel Jakub Dawidek 	 */
162824b2151fSPawel Jakub Dawidek 	if (p->p_state == PRS_ZOMBIE)
162924b2151fSPawel Jakub Dawidek 		return;
16304093529dSJeff Roberson 	prop = sigprop(sig);
16314093529dSJeff Roberson 
16324093529dSJeff Roberson 	/*
16334093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16344093529dSJeff Roberson 	 */
16354093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16364093529dSJeff Roberson 
1637c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16384093529dSJeff Roberson }
16394093529dSJeff Roberson 
164090af4afaSJohn Baldwin /*
164190af4afaSJohn Baldwin  * MPSAFE
164290af4afaSJohn Baldwin  */
16434093529dSJeff Roberson void
1644c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16454093529dSJeff Roberson {
16469dde3bc9SDavid Xu 	sigset_t saved;
16479dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16489dde3bc9SDavid Xu 
16499dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16509dde3bc9SDavid Xu 		saved = p->p_siglist;
1651c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16529dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16536dbc0850SJohn Baldwin 		if (!SIGSETEQ(saved, p->p_siglist)) {
16549dde3bc9SDavid Xu 			/* pending set changed */
16559dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
16569dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
16579dde3bc9SDavid Xu 		}
16589dde3bc9SDavid Xu 	}
16599dde3bc9SDavid Xu }
16609dde3bc9SDavid Xu 
16619dde3bc9SDavid Xu static void
1662c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target)
16639dde3bc9SDavid Xu {
16644093529dSJeff Roberson 	struct proc *p;
16654093529dSJeff Roberson 	register sig_t action;
16664093529dSJeff Roberson 	sigset_t *siglist;
16674093529dSJeff Roberson 	struct thread *td0;
1668e602ba25SJulian Elischer 	register int prop;
166990af4afaSJohn Baldwin 	struct sigacts *ps;
1670df8bae1dSRodney W. Grimes 
167141b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
167241b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
16734093529dSJeff Roberson 
16744093529dSJeff Roberson 	p = td->td_proc;
167590af4afaSJohn Baldwin 	ps = p->p_sigacts;
16762c42a146SMarcel Moolenaar 
1677628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1678ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
1679cb679c38SJonathan Lemon 
16802c42a146SMarcel Moolenaar 	prop = sigprop(sig);
16814093529dSJeff Roberson 
16824093529dSJeff Roberson 	/*
168314b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
168414b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
168514b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
16864093529dSJeff Roberson 	 */
16873074d1b4SDavid Xu 	if (target == SIGTARGET_TD) {
16883074d1b4SDavid Xu 		siglist = &td->td_siglist;
16893074d1b4SDavid Xu 	} else {
16903074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig))
16913074d1b4SDavid Xu 			siglist = &td->td_siglist;
16923074d1b4SDavid Xu 		else if (td->td_waitset != NULL &&
16933074d1b4SDavid Xu 			SIGISMEMBER(*(td->td_waitset), sig))
16943074d1b4SDavid Xu 			siglist = &td->td_siglist;
16953074d1b4SDavid Xu 		else
16963074d1b4SDavid Xu 			siglist = &p->p_siglist;
16973074d1b4SDavid Xu 	}
16984093529dSJeff Roberson 
1699df8bae1dSRodney W. Grimes 	/*
17002a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
17012a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
17022a024a2bSSean Eric Fagan 	 * a chance, as well.
1703df8bae1dSRodney W. Grimes 	 */
1704b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1705df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1706b40ce416SJulian Elischer 	} else {
1707df8bae1dSRodney W. Grimes 		/*
1708df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1709df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
171090af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1711df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1712df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1713df8bae1dSRodney W. Grimes 		 */
171490af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
171590af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
171690af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
171790af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1718df8bae1dSRodney W. Grimes 			return;
171990af4afaSJohn Baldwin 		}
17203074d1b4SDavid Xu 		if (((td->td_waitset == NULL) &&
17213074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig)) ||
17223074d1b4SDavid Xu 		    ((td->td_waitset != NULL) &&
17233074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig) &&
17243074d1b4SDavid Xu 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1725df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
172690af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1727df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1728df8bae1dSRodney W. Grimes 		else
1729df8bae1dSRodney W. Grimes 			action = SIG_DFL;
173090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1731df8bae1dSRodney W. Grimes 	}
1732df8bae1dSRodney W. Grimes 
17334093529dSJeff Roberson 	if (prop & SA_CONT) {
17341d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17354093529dSJeff Roberson 		/*
17364093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17374093529dSJeff Roberson 		 * the proc's siglist.
17384093529dSJeff Roberson 		 */
1739a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17404093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17414093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1742a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17434093529dSJeff Roberson 	}
1744df8bae1dSRodney W. Grimes 
1745df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1746df8bae1dSRodney W. Grimes 		/*
1747df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1748df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1749df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1750df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1751df8bae1dSRodney W. Grimes 		 */
1752e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1753e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1754e602ba25SJulian Elischer 		    (action == SIG_DFL))
1755df8bae1dSRodney W. Grimes 		        return;
17561d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1757a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17584093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17594093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1760a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17616933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1762df8bae1dSRodney W. Grimes 	}
17633074d1b4SDavid Xu 
17644093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17654093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17663074d1b4SDavid Xu 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
17673074d1b4SDavid Xu 	    action != SIG_HOLD) {
17683074d1b4SDavid Xu 		td->td_waitset = NULL;
17693074d1b4SDavid Xu 	}
17703074d1b4SDavid Xu 
17715312b1c7SDavid Xu 	/*
17725312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17735312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17745312b1c7SDavid Xu 	 */
17755312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17765312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17775312b1c7SDavid Xu 		return;
1778df8bae1dSRodney W. Grimes 	/*
177950434413SAlfred Perlstein 	 * SIGKILL: Remove procfs STOPEVENTs.
178050434413SAlfred Perlstein 	 */
178150434413SAlfred Perlstein 	if (sig == SIGKILL) {
178250434413SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCBIC */
178350434413SAlfred Perlstein 		p->p_stops = 0;
178450434413SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCCONT */
178550434413SAlfred Perlstein 		p->p_step = 0;
178650434413SAlfred Perlstein 		wakeup(&p->p_step);
178750434413SAlfred Perlstein 	}
178850434413SAlfred Perlstein 	/*
1789e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1790e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1791e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1792e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1793e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1794e602ba25SJulian Elischer 	 * We try do the per-process part here.
1795df8bae1dSRodney W. Grimes 	 */
1796e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1797e602ba25SJulian Elischer 		/*
1798e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1799e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1800e602ba25SJulian Elischer 		 */
1801e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1802e602ba25SJulian Elischer 			/*
1803e602ba25SJulian Elischer 			 * The traced process is already stopped,
1804e602ba25SJulian Elischer 			 * so no further action is necessary.
1805e602ba25SJulian Elischer 			 * No signal can restart us.
1806e602ba25SJulian Elischer 			 */
1807e602ba25SJulian Elischer 			goto out;
18081c32c37cSJohn Baldwin 		}
1809b40ce416SJulian Elischer 
1810e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1811df8bae1dSRodney W. Grimes 			/*
1812e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1813e602ba25SJulian Elischer 			 * It will die elsewhere.
1814e602ba25SJulian Elischer 			 * All threads must be restarted.
1815df8bae1dSRodney W. Grimes 			 */
1816482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
1817e602ba25SJulian Elischer 			goto runfast;
1818e602ba25SJulian Elischer 		}
1819e602ba25SJulian Elischer 
1820e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1821e602ba25SJulian Elischer 			/*
1822e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18234093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18241d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1825e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18264093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1827e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1828e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1829e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1830e602ba25SJulian Elischer 			 */
18311279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18326933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1833e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18344093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1835e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1836e602ba25SJulian Elischer 				/*
1837e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1838e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1839e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1840e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1841e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1842e602ba25SJulian Elischer 				 * process, we need to make sure that the
1843e602ba25SJulian Elischer 				 * single thread is runnable asap.
1844e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1845e602ba25SJulian Elischer 				 */
1846e602ba25SJulian Elischer 				goto runfast;
1847e602ba25SJulian Elischer 			}
1848e602ba25SJulian Elischer 			/*
1849e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1850e602ba25SJulian Elischer 			 */
1851e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
185204774f23SJulian Elischer 			thread_unsuspend(p);
1853e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1854e602ba25SJulian Elischer 			goto out;
1855e602ba25SJulian Elischer 		}
1856e602ba25SJulian Elischer 
1857e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1858e602ba25SJulian Elischer 			/*
1859e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1860e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
186104774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1862e602ba25SJulian Elischer 			 */
18631279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18644093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1865e602ba25SJulian Elischer 			goto out;
1866e602ba25SJulian Elischer 		}
1867e602ba25SJulian Elischer 
1868e602ba25SJulian Elischer 		/*
1869e602ba25SJulian Elischer 		 * All other kinds of signals:
1870e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1871e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1872e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
187304774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1874e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1875e602ba25SJulian Elischer 		 */
1876e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
187744f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
187844f3b092SJohn Baldwin 			sleepq_abort(td);
1879e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1880df8bae1dSRodney W. Grimes 		goto out;
1881df8bae1dSRodney W. Grimes 		/*
18829a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
18839a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
1884df8bae1dSRodney W. Grimes 		 */
1885e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
188635c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
188735c32a76SDavid Xu 			!(prop & SA_STOP)) {
1888721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18894093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1890721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1891df8bae1dSRodney W. Grimes 			goto out;
189204774f23SJulian Elischer 		}
189335c32a76SDavid Xu 		if (prop & SA_STOP) {
189435c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
189535c32a76SDavid Xu 				goto out;
1896e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1897e574e444SDavid Xu 			p->p_xstat = sig;
1898cbf4e354SDavid Xu 			p->p_xthread = td;
189935c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
19004093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
19014093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1902062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1903062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
19044093529dSJeff Roberson 					thread_suspend_one(td0);
1905062cf543SDavid Xu 				} else if (td != td0) {
1906062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1907062cf543SDavid Xu 				}
190835c32a76SDavid Xu 			}
1909e574e444SDavid Xu 			thread_stopped(p);
19104093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1911e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
19124093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
19134093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
19144093529dSJeff Roberson 			}
191535c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
191635c32a76SDavid Xu 			goto out;
191735c32a76SDavid Xu 		}
1918721e5910SJulian Elischer 		else
1919df8bae1dSRodney W. Grimes 			goto runfast;
1920df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1921e602ba25SJulian Elischer 	} else {
1922e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19234093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1924e602ba25SJulian Elischer 		goto out;
1925e602ba25SJulian Elischer 	}
1926e602ba25SJulian Elischer 
1927b40ce416SJulian Elischer 	/*
1928e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1929e602ba25SJulian Elischer 	 * running threads.
1930b40ce416SJulian Elischer 	 */
1931e602ba25SJulian Elischer 
1932e602ba25SJulian Elischer runfast:
1933aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19344093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1935e602ba25SJulian Elischer 	thread_unsuspend(p);
1936e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1937e602ba25SJulian Elischer out:
1938e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1939e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1940e602ba25SJulian Elischer }
1941e602ba25SJulian Elischer 
1942e602ba25SJulian Elischer /*
1943e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1944e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
1945e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1946e602ba25SJulian Elischer  */
1947e602ba25SJulian Elischer static void
19484093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1949e602ba25SJulian Elischer {
1950e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1951e602ba25SJulian Elischer 	register int prop;
1952e602ba25SJulian Elischer 
19538b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1954aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1955e602ba25SJulian Elischer 	prop = sigprop(sig);
1956a4c2da15SBruce Evans 
1957e602ba25SJulian Elischer 	/*
1958aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1959e602ba25SJulian Elischer 	 * killed in this lifetime.
1960e602ba25SJulian Elischer 	 */
1961a4c2da15SBruce Evans 	if (action == SIG_DFL && (prop & SA_KILL)) {
1962a4c2da15SBruce Evans 		if (td->td_priority > PUSER)
1963b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
1964b40ce416SJulian Elischer 	}
1965a4c2da15SBruce Evans 
196680c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
1967e602ba25SJulian Elischer 		/*
1968e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1969e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1970e602ba25SJulian Elischer 		 * be noticed when the process returns through
1971e602ba25SJulian Elischer 		 * trap() or syscall().
1972e602ba25SJulian Elischer 		 */
197344f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1974aa0fa334SJulian Elischer 			return;
1975e602ba25SJulian Elischer 		/*
1976e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1977e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1978e602ba25SJulian Elischer 		 * for its parent.
1979e602ba25SJulian Elischer 		 */
1980e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1981e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1982aa0fa334SJulian Elischer 		} else {
1983df8bae1dSRodney W. Grimes 			/*
1984e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1985e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1986e602ba25SJulian Elischer 			 * be awakened.
1987df8bae1dSRodney W. Grimes 			 */
1988e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19891d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19904093529dSJeff Roberson 				/*
19914093529dSJeff Roberson 				 * It may be on either list in this state.
19924093529dSJeff Roberson 				 * Remove from both for now.
19934093529dSJeff Roberson 				 */
19944093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1995aa0fa334SJulian Elischer 				return;
1996df8bae1dSRodney W. Grimes 			}
1997df8bae1dSRodney W. Grimes 
1998aa0fa334SJulian Elischer 			/*
1999a4c2da15SBruce Evans 			 * Give low priority threads a better chance to run.
2000aa0fa334SJulian Elischer 			 */
200144f3b092SJohn Baldwin 			if (td->td_priority > PUSER)
2002b3a4fb14SDavid Xu 				sched_prio(td, PUSER);
2003aa0fa334SJulian Elischer 		}
200444f3b092SJohn Baldwin 		sleepq_abort(td);
2005a4c2da15SBruce Evans 	} else {
2006df8bae1dSRodney W. Grimes 		/*
2007a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
2008df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2009df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2010df8bae1dSRodney W. Grimes 		 */
2011a4c2da15SBruce Evans #ifdef SMP
201244f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
2013e602ba25SJulian Elischer 			forward_signal(td);
20143163861cSTor Egge #endif
20156caa8a15SJohn Baldwin 	}
2016a4c2da15SBruce Evans }
2017df8bae1dSRodney W. Grimes 
2018cbf4e354SDavid Xu int
20194cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
20204cc9f52fSRobert Drehmel {
20214cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
2022cbf4e354SDavid Xu 	struct thread *td0;
20234cc9f52fSRobert Drehmel 
202430a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20254cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
20264cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
20274cc9f52fSRobert Drehmel 
20284cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
2029cbf4e354SDavid Xu 	td->td_flags |= TDF_XSIG;
2030cbf4e354SDavid Xu 	mtx_unlock_spin(&sched_lock);
2031cbf4e354SDavid Xu 	td->td_xsig = sig;
2032cbf4e354SDavid Xu 	while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2033cbf4e354SDavid Xu 		if (p->p_flag & P_SINGLE_EXIT) {
2034cbf4e354SDavid Xu 			mtx_lock_spin(&sched_lock);
2035cbf4e354SDavid Xu 			td->td_flags &= ~TDF_XSIG;
2036cbf4e354SDavid Xu 			mtx_unlock_spin(&sched_lock);
2037cbf4e354SDavid Xu 			return (sig);
2038cbf4e354SDavid Xu 		}
2039cbf4e354SDavid Xu 		/*
2040cbf4e354SDavid Xu 		 * Just make wait() to work, the last stopped thread
2041cbf4e354SDavid Xu 		 * will win.
2042cbf4e354SDavid Xu 		 */
2043cbf4e354SDavid Xu 		p->p_xstat = sig;
2044cbf4e354SDavid Xu 		p->p_xthread = td;
2045cbf4e354SDavid Xu 		p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2046cbf4e354SDavid Xu 		mtx_lock_spin(&sched_lock);
2047cbf4e354SDavid Xu 		FOREACH_THREAD_IN_PROC(p, td0) {
2048cbf4e354SDavid Xu 			if (TD_IS_SLEEPING(td0) &&
2049cbf4e354SDavid Xu 			    (td0->td_flags & TDF_SINTR) &&
2050cbf4e354SDavid Xu 			    !TD_IS_SUSPENDED(td0)) {
2051cbf4e354SDavid Xu 				thread_suspend_one(td0);
2052cbf4e354SDavid Xu 			} else if (td != td0) {
2053cbf4e354SDavid Xu 				td0->td_flags |= TDF_ASTPENDING;
2054cbf4e354SDavid Xu 			}
2055cbf4e354SDavid Xu 		}
2056cbf4e354SDavid Xu stopme:
2057cbf4e354SDavid Xu 		thread_stopped(p);
20584cc9f52fSRobert Drehmel 		thread_suspend_one(td);
20594cc9f52fSRobert Drehmel 		PROC_UNLOCK(p);
20604cc9f52fSRobert Drehmel 		DROP_GIANT();
2061cbf4e354SDavid Xu 		mi_switch(SW_VOL, NULL);
20624cc9f52fSRobert Drehmel 		mtx_unlock_spin(&sched_lock);
20634cc9f52fSRobert Drehmel 		PICKUP_GIANT();
2064cbf4e354SDavid Xu 		PROC_LOCK(p);
2065cbf4e354SDavid Xu 		if (!(p->p_flag & P_TRACED))
2066cbf4e354SDavid Xu 			break;
2067cbf4e354SDavid Xu 		if (td->td_flags & TDF_DBSUSPEND) {
2068cbf4e354SDavid Xu 			if (p->p_flag & P_SINGLE_EXIT)
2069cbf4e354SDavid Xu 				break;
2070cbf4e354SDavid Xu 			mtx_lock_spin(&sched_lock);
2071cbf4e354SDavid Xu 			goto stopme;
2072cbf4e354SDavid Xu 		}
2073cbf4e354SDavid Xu 	}
2074cbf4e354SDavid Xu 	return (td->td_xsig);
20754cc9f52fSRobert Drehmel }
20764cc9f52fSRobert Drehmel 
2077df8bae1dSRodney W. Grimes /*
2078df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2079df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2080df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2081df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2082df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2083628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2084df8bae1dSRodney W. Grimes  * sequence is
2085df8bae1dSRodney W. Grimes  *
2086e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
20872c42a146SMarcel Moolenaar  *		postsig(sig);
2088df8bae1dSRodney W. Grimes  */
20896711f10fSJohn Baldwin static int
2090e602ba25SJulian Elischer issignal(td)
2091e602ba25SJulian Elischer 	struct thread *td;
2092df8bae1dSRodney W. Grimes {
2093e602ba25SJulian Elischer 	struct proc *p;
209490af4afaSJohn Baldwin 	struct sigacts *ps;
20954093529dSJeff Roberson 	sigset_t sigpending;
2096cbf4e354SDavid Xu 	int sig, prop, newsig;
2097062cf543SDavid Xu 	struct thread *td0;
2098df8bae1dSRodney W. Grimes 
2099e602ba25SJulian Elischer 	p = td->td_proc;
210090af4afaSJohn Baldwin 	ps = p->p_sigacts;
210190af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2102628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2103df8bae1dSRodney W. Grimes 	for (;;) {
21042a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
21052a024a2bSSean Eric Fagan 
21064093529dSJeff Roberson 		sigpending = td->td_siglist;
21074093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
21084093529dSJeff Roberson 
2109df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
21104093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
21114093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2112df8bae1dSRodney W. Grimes 			return (0);
21134093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
21142a024a2bSSean Eric Fagan 
2115047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2116047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2117047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2118047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2119047aa39bSRobert Watson 		}
21202a024a2bSSean Eric Fagan 
2121df8bae1dSRodney W. Grimes 		/*
2122df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2123df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2124df8bae1dSRodney W. Grimes 		 */
212590af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
21264093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
2127cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2128cbf4e354SDavid Xu 				SIGADDSET(td->td_sigmask, sig);
2129df8bae1dSRodney W. Grimes 			continue;
2130df8bae1dSRodney W. Grimes 		}
2131df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2132df8bae1dSRodney W. Grimes 			/*
2133d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2134df8bae1dSRodney W. Grimes 			 */
213590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2136cbf4e354SDavid Xu 			newsig = ptracestop(td, sig);
213790af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2138df8bae1dSRodney W. Grimes 
2139df8bae1dSRodney W. Grimes 			/*
2140df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
2141df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
2142df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
2143df8bae1dSRodney W. Grimes 			 */
21444093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
2145cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2146cbf4e354SDavid Xu 				SIGADDSET(td->td_sigmask, sig);
2147cbf4e354SDavid Xu 			if (newsig == 0)
2148df8bae1dSRodney W. Grimes 				continue;
2149cbf4e354SDavid Xu 			sig = newsig;
2150df8bae1dSRodney W. Grimes 			/*
21518d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
21528d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
21538d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
21548d542cb5SDavid E. O'Brien 			 */
21558d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
21568d542cb5SDavid E. O'Brien 				continue;
21578d542cb5SDavid E. O'Brien 
21588d542cb5SDavid E. O'Brien 			/*
21594093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
21601d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
2161df8bae1dSRodney W. Grimes 			 */
21624093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
2163cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2164cbf4e354SDavid Xu 				SIGDELSET(td->td_sigmask, sig);
21654093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
2166df8bae1dSRodney W. Grimes 				continue;
21674093529dSJeff Roberson 			signotify(td);
2168df8bae1dSRodney W. Grimes 		}
2169df8bae1dSRodney W. Grimes 
21708d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
21718d542cb5SDavid E. O'Brien 
2172df8bae1dSRodney W. Grimes 		/*
2173df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2174df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2175df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2176df8bae1dSRodney W. Grimes 		 */
2177d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2178df8bae1dSRodney W. Grimes 
2179d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2180df8bae1dSRodney W. Grimes 			/*
2181df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2182df8bae1dSRodney W. Grimes 			 */
2183df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2184df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2185df8bae1dSRodney W. Grimes 				/*
2186df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2187df8bae1dSRodney W. Grimes 				 * in init? XXX
2188df8bae1dSRodney W. Grimes 				 */
2189d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
21902c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2191df8bae1dSRodney W. Grimes #endif
2192df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2193df8bae1dSRodney W. Grimes 			}
2194df8bae1dSRodney W. Grimes 			/*
2195df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2196df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2197df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2198df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2199df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2200df8bae1dSRodney W. Grimes 			 */
2201df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2202df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2203df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2204df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2205df8bae1dSRodney W. Grimes 					break;	/* == ignore */
220690af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
220790af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
220890af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2209e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
22102c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2211cbf4e354SDavid Xu 				p->p_xthread = td;
2212721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2213062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2214062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2215062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2216062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2217062cf543SDavid Xu 						thread_suspend_one(td0);
2218062cf543SDavid Xu 					} else if (td != td0) {
2219062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2220062cf543SDavid Xu 					}
2221062cf543SDavid Xu 				}
2222e574e444SDavid Xu 				thread_stopped(p);
222371fad9fdSJulian Elischer 				thread_suspend_one(td);
2224721e5910SJulian Elischer 				PROC_UNLOCK(p);
2225721e5910SJulian Elischer 				DROP_GIANT();
2226bf0acc27SJohn Baldwin 				mi_switch(SW_INVOL, NULL);
2227721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2228721e5910SJulian Elischer 				PICKUP_GIANT();
2229721e5910SJulian Elischer 				PROC_LOCK(p);
223090af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2231df8bae1dSRodney W. Grimes 				break;
223221b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2233df8bae1dSRodney W. Grimes 				/*
2234df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2235df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2236df8bae1dSRodney W. Grimes 				 */
2237df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2238df8bae1dSRodney W. Grimes 			} else
22392c42a146SMarcel Moolenaar 				return (sig);
2240df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2241df8bae1dSRodney W. Grimes 
2242d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2243df8bae1dSRodney W. Grimes 			/*
2244df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2245df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2246df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2247df8bae1dSRodney W. Grimes 			 */
2248df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2249df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2250df8bae1dSRodney W. Grimes 				printf("issignal\n");
2251df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2252df8bae1dSRodney W. Grimes 
2253df8bae1dSRodney W. Grimes 		default:
2254df8bae1dSRodney W. Grimes 			/*
2255df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2256df8bae1dSRodney W. Grimes 			 * postsig() process it.
2257df8bae1dSRodney W. Grimes 			 */
22582c42a146SMarcel Moolenaar 			return (sig);
2259df8bae1dSRodney W. Grimes 		}
22604093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2261df8bae1dSRodney W. Grimes 	}
2262df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2263df8bae1dSRodney W. Grimes }
2264df8bae1dSRodney W. Grimes 
2265df8bae1dSRodney W. Grimes /*
2266df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2267df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
22684a3b3dcbSColin Percival  * on the run queue.  Must be called with the proc p locked.
2269df8bae1dSRodney W. Grimes  */
22705b3047d5SJohn Baldwin static void
2271e574e444SDavid Xu stop(struct proc *p)
2272df8bae1dSRodney W. Grimes {
2273df8bae1dSRodney W. Grimes 
2274628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22751279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2276df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
227701609114SAlfred Perlstein 	wakeup(p->p_pptr);
2278df8bae1dSRodney W. Grimes }
2279df8bae1dSRodney W. Grimes 
228090af4afaSJohn Baldwin /*
228190af4afaSJohn Baldwin  * MPSAFE
228290af4afaSJohn Baldwin  */
2283e574e444SDavid Xu void
2284e574e444SDavid Xu thread_stopped(struct proc *p)
2285e574e444SDavid Xu {
2286e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
228790af4afaSJohn Baldwin 	struct sigacts *ps;
2288e574e444SDavid Xu 	int n;
2289e574e444SDavid Xu 
2290e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2291e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2292e574e444SDavid Xu 	n = p->p_suspcount;
2293e574e444SDavid Xu 	if (p == p1)
2294e574e444SDavid Xu 		n++;
2295e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2296e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2297e574e444SDavid Xu 		stop(p);
2298e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
229990af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
230090af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
230190af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
230290af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2303e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
230490af4afaSJohn Baldwin 		} else
230590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2306e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2307e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2308e574e444SDavid Xu 	}
2309e574e444SDavid Xu }
2310e574e444SDavid Xu 
2311df8bae1dSRodney W. Grimes /*
2312df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2313df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2314df8bae1dSRodney W. Grimes  */
2315df8bae1dSRodney W. Grimes void
23162c42a146SMarcel Moolenaar postsig(sig)
23172c42a146SMarcel Moolenaar 	register int sig;
2318df8bae1dSRodney W. Grimes {
2319b40ce416SJulian Elischer 	struct thread *td = curthread;
2320b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2321628d2653SJohn Baldwin 	struct sigacts *ps;
23222c42a146SMarcel Moolenaar 	sig_t action;
23232c42a146SMarcel Moolenaar 	sigset_t returnmask;
23242c42a146SMarcel Moolenaar 	int code;
2325df8bae1dSRodney W. Grimes 
23262c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
23275526d2d9SEivind Eklund 
23282ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2329628d2653SJohn Baldwin 	ps = p->p_sigacts;
233090af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
23314093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
23322c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2333df8bae1dSRodney W. Grimes #ifdef KTRACE
2334374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
23355e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
23364093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2337df8bae1dSRodney W. Grimes #endif
2338047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2339047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2340047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2341047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2342047aa39bSRobert Watson 	}
23432a024a2bSSean Eric Fagan 
2344cbf4e354SDavid Xu 	if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2345df8bae1dSRodney W. Grimes 		/*
2346df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2347df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2348df8bae1dSRodney W. Grimes 		 */
234990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2350b40ce416SJulian Elischer 		sigexit(td, sig);
2351df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2352df8bae1dSRodney W. Grimes 	} else {
2353cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA) {
2354432b45deSDavid Xu 			if (sig == SIGKILL) {
2355432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2356432b45deSDavid Xu 				sigexit(td, sig);
2357432b45deSDavid Xu 			}
2358432b45deSDavid Xu 		}
2359432b45deSDavid Xu 
2360df8bae1dSRodney W. Grimes 		/*
2361df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2362df8bae1dSRodney W. Grimes 		 */
23634093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23645526d2d9SEivind Eklund 		    ("postsig action"));
2365df8bae1dSRodney W. Grimes 		/*
2366df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2367645682fdSLuoqi Chen 		 * occurrences of this signal.
2368df8bae1dSRodney W. Grimes 		 *
2369645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2370df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2371645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2372df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2373df8bae1dSRodney W. Grimes 		 */
23745e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23754093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23765e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2377df8bae1dSRodney W. Grimes 		} else
23784093529dSJeff Roberson 			returnmask = td->td_sigmask;
23792c42a146SMarcel Moolenaar 
23804093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23812c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23824093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23832c42a146SMarcel Moolenaar 
23842c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2385289ccde0SPeter Wemm 			/*
23868f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2387289ccde0SPeter Wemm 			 */
238890af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23892c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23902c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
239190af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23922c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2393dedc04feSPeter Wemm 		}
2394df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23952c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2396df8bae1dSRodney W. Grimes 			code = 0;
2397df8bae1dSRodney W. Grimes 		} else {
23986626c604SJulian Elischer 			code = p->p_code;
23996626c604SJulian Elischer 			p->p_code = 0;
24006626c604SJulian Elischer 			p->p_sig = 0;
2401df8bae1dSRodney W. Grimes 		}
2402cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA)
240358a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
240458a3c273SJeff Roberson 		else
240558a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
240658a3c273SJeff Roberson 			    &returnmask, code);
2407df8bae1dSRodney W. Grimes 	}
2408df8bae1dSRodney W. Grimes }
2409df8bae1dSRodney W. Grimes 
2410df8bae1dSRodney W. Grimes /*
2411df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2412df8bae1dSRodney W. Grimes  */
241326f9a767SRodney W. Grimes void
2414df8bae1dSRodney W. Grimes killproc(p, why)
2415df8bae1dSRodney W. Grimes 	struct proc *p;
2416df8bae1dSRodney W. Grimes 	char *why;
2417df8bae1dSRodney W. Grimes {
24189081e5e8SJohn Baldwin 
24199081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
24200384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
24210384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2422729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2423b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2424df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2425df8bae1dSRodney W. Grimes }
2426df8bae1dSRodney W. Grimes 
2427df8bae1dSRodney W. Grimes /*
2428df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2429df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2430df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2431df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2432df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2433df8bae1dSRodney W. Grimes  * does not return.
243490af4afaSJohn Baldwin  *
243590af4afaSJohn Baldwin  * MPSAFE
2436df8bae1dSRodney W. Grimes  */
243726f9a767SRodney W. Grimes void
2438b40ce416SJulian Elischer sigexit(td, sig)
2439b40ce416SJulian Elischer 	struct thread *td;
24402c42a146SMarcel Moolenaar 	int sig;
2441df8bae1dSRodney W. Grimes {
2442b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2443df8bae1dSRodney W. Grimes 
2444628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2445df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
24462c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
24472c42a146SMarcel Moolenaar 		p->p_sig = sig;
2448c364e17eSAndrey A. Chernov 		/*
2449c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2450c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2451c364e17eSAndrey A. Chernov 		 * these messages.)
2452c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
24534ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2454c364e17eSAndrey A. Chernov 		 */
2455b40ce416SJulian Elischer 		if (coredump(td) == 0)
24562c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
245757308494SJoerg Wunsch 		if (kern_logsigexit)
245857308494SJoerg Wunsch 			log(LOG_INFO,
245957308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24603d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24619c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24622c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24632c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
24644ae89b95SJohn Baldwin 	} else
2465628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2466b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2467df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2468df8bae1dSRodney W. Grimes }
2469df8bae1dSRodney W. Grimes 
2470c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2471c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2472c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2473c5edb423SSean Eric Fagan 
2474c5edb423SSean Eric Fagan /*
2475c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2476c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2477c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2478c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2479c5edb423SSean Eric Fagan  *	%P	process id (pid)
2480c5edb423SSean Eric Fagan  *	%U	user id (uid)
2481c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2482c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2483c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2484c5edb423SSean Eric Fagan  */
2485c5edb423SSean Eric Fagan 
2486fca666a1SJulian Elischer static char *
2487c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24888b43b535SAlfred Perlstein 	const char *name;
24898b43b535SAlfred Perlstein 	uid_t uid;
24908b43b535SAlfred Perlstein 	pid_t pid;
24918b43b535SAlfred Perlstein {
24928b43b535SAlfred Perlstein 	const char *format, *appendstr;
2493c5edb423SSean Eric Fagan 	char *temp;
2494c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24958b43b535SAlfred Perlstein 	size_t i, l, n;
2496c5edb423SSean Eric Fagan 
24978b43b535SAlfred Perlstein 	format = corefilename;
24988b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24990bfe2990SEivind Eklund 	if (temp == NULL)
25008b43b535SAlfred Perlstein 		return (NULL);
2501ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2502c5edb423SSean Eric Fagan 		switch (format[i]) {
2503c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2504c5edb423SSean Eric Fagan 			i++;
2505c5edb423SSean Eric Fagan 			switch (format[i]) {
2506c5edb423SSean Eric Fagan 			case '%':
25078b43b535SAlfred Perlstein 				appendstr = "%";
2508c5edb423SSean Eric Fagan 				break;
2509c5edb423SSean Eric Fagan 			case 'N':	/* process name */
25108b43b535SAlfred Perlstein 				appendstr = name;
2511c5edb423SSean Eric Fagan 				break;
2512c5edb423SSean Eric Fagan 			case 'P':	/* process id */
25138b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
25148b43b535SAlfred Perlstein 				appendstr = buf;
2515c5edb423SSean Eric Fagan 				break;
2516c5edb423SSean Eric Fagan 			case 'U':	/* user id */
25178b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
25188b43b535SAlfred Perlstein 				appendstr = buf;
2519c5edb423SSean Eric Fagan 				break;
2520c5edb423SSean Eric Fagan 			default:
25218b43b535SAlfred Perlstein 				appendstr = "";
25228b43b535SAlfred Perlstein 			  	log(LOG_ERR,
25238b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
25248b43b535SAlfred Perlstein 				    format[i], format);
2525c5edb423SSean Eric Fagan 			}
25268b43b535SAlfred Perlstein 			l = strlen(appendstr);
25278b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
25288b43b535SAlfred Perlstein 				goto toolong;
25298b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
25308b43b535SAlfred Perlstein 			n += l;
2531c5edb423SSean Eric Fagan 			break;
2532c5edb423SSean Eric Fagan 		default:
2533c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2534c5edb423SSean Eric Fagan 		}
2535c5edb423SSean Eric Fagan 	}
25368b43b535SAlfred Perlstein 	if (format[i] != '\0')
25378b43b535SAlfred Perlstein 		goto toolong;
25388b43b535SAlfred Perlstein 	return (temp);
25398b43b535SAlfred Perlstein toolong:
25408b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
25418b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
25428b43b535SAlfred Perlstein 	free(temp, M_TEMP);
25438b43b535SAlfred Perlstein 	return (NULL);
2544c5edb423SSean Eric Fagan }
2545c5edb423SSean Eric Fagan 
2546df8bae1dSRodney W. Grimes /*
2547fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2548fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2549fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2550fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2551fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2552fca666a1SJulian Elischer  */
2553fca666a1SJulian Elischer 
2554fca666a1SJulian Elischer static int
2555b40ce416SJulian Elischer coredump(struct thread *td)
2556fca666a1SJulian Elischer {
2557b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2558fca666a1SJulian Elischer 	register struct vnode *vp;
25599c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
256006ae1e91SMatthew Dillon 	struct flock lf;
2561fca666a1SJulian Elischer 	struct nameidata nd;
2562fca666a1SJulian Elischer 	struct vattr vattr;
2563c447f5b2SRobert Watson 	int error, error1, flags, locked;
2564f2a2857bSKirk McKusick 	struct mount *mp;
2565fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2566fca666a1SJulian Elischer 	off_t limit;
2567fca666a1SJulian Elischer 
25684ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2569628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2570fca666a1SJulian Elischer 
2571628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2572628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2573fca666a1SJulian Elischer 		return (EFAULT);
2574628d2653SJohn Baldwin 	}
2575fca666a1SJulian Elischer 
2576fca666a1SJulian Elischer 	/*
257735a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
257835a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
257935a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
258035a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
258135a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
258235a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2583fca666a1SJulian Elischer 	 */
258491d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2585628d2653SJohn Baldwin 	PROC_UNLOCK(p);
258691d5354aSJohn Baldwin 	if (limit == 0)
258791d5354aSJohn Baldwin 		return (EFBIG);
258835a2598fSSean Eric Fagan 
25894ae89b95SJohn Baldwin 	mtx_lock(&Giant);
2590f2a2857bSKirk McKusick restart:
25919c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
25924ae89b95SJohn Baldwin 	if (name == NULL) {
25934ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2594ccdbd10cSPeter Pentchev 		return (EINVAL);
25954ae89b95SJohn Baldwin 	}
2596b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2597e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25987c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2599fca666a1SJulian Elischer 	free(name, M_TEMP);
26004ae89b95SJohn Baldwin 	if (error) {
26014ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2602fca666a1SJulian Elischer 		return (error);
26034ae89b95SJohn Baldwin 	}
2604762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2605fca666a1SJulian Elischer 	vp = nd.ni_vp;
260606ae1e91SMatthew Dillon 
2607832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2608832dafadSDon Lewis 	if (vp->v_type != VREG ||
2609832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2610832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2611832dafadSDon Lewis 		error = EFAULT;
26124ae89b95SJohn Baldwin 		goto out;
2613832dafadSDon Lewis 	}
2614832dafadSDon Lewis 
2615b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
261606ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
261706ae1e91SMatthew Dillon 	lf.l_start = 0;
261806ae1e91SMatthew Dillon 	lf.l_len = 0;
261906ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2620c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
262106ae1e91SMatthew Dillon 
262206ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
262306ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
262436bbf86bSRobert Watson 		if (locked)
262506ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2626b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2627f2a2857bSKirk McKusick 			return (error);
2628f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2629f2a2857bSKirk McKusick 			return (error);
2630f2a2857bSKirk McKusick 		goto restart;
2631f2a2857bSKirk McKusick 	}
2632fca666a1SJulian Elischer 
2633fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2634fca666a1SJulian Elischer 	vattr.va_size = 0;
26356141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
26366141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
263788b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2638b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2639b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
264088b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2641628d2653SJohn Baldwin 	PROC_LOCK(p);
2642fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2643628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2644fca666a1SJulian Elischer 
2645fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2646b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2647fca666a1SJulian Elischer 	  ENOSYS;
2648fca666a1SJulian Elischer 
2649c447f5b2SRobert Watson 	if (locked) {
265006ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
265106ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2652c447f5b2SRobert Watson 	}
2653f2a2857bSKirk McKusick 	vn_finished_write(mp);
26544ae89b95SJohn Baldwin out:
2655b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
26564ae89b95SJohn Baldwin 	mtx_unlock(&Giant);
2657fca666a1SJulian Elischer 	if (error == 0)
2658fca666a1SJulian Elischer 		error = error1;
2659fca666a1SJulian Elischer 	return (error);
2660fca666a1SJulian Elischer }
2661fca666a1SJulian Elischer 
2662fca666a1SJulian Elischer /*
2663df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2664df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2665df8bae1dSRodney W. Grimes  */
2666d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2667df8bae1dSRodney W. Grimes struct nosys_args {
2668df8bae1dSRodney W. Grimes 	int	dummy;
2669df8bae1dSRodney W. Grimes };
2670d2d3e875SBruce Evans #endif
2671fb99ab88SMatthew Dillon /*
2672fb99ab88SMatthew Dillon  * MPSAFE
2673fb99ab88SMatthew Dillon  */
2674df8bae1dSRodney W. Grimes /* ARGSUSED */
267526f9a767SRodney W. Grimes int
2676b40ce416SJulian Elischer nosys(td, args)
2677b40ce416SJulian Elischer 	struct thread *td;
2678df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2679df8bae1dSRodney W. Grimes {
2680b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2681b40ce416SJulian Elischer 
2682628d2653SJohn Baldwin 	PROC_LOCK(p);
2683df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2684628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2685f5216b9aSBruce Evans 	return (ENOSYS);
2686df8bae1dSRodney W. Grimes }
2687831d27a9SDon Lewis 
2688831d27a9SDon Lewis /*
268948f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2690831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2691831d27a9SDon Lewis  */
2692831d27a9SDon Lewis void
2693f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2694f1320723SAlfred Perlstein 	struct sigio **sigiop;
26952c42a146SMarcel Moolenaar 	int sig, checkctty;
2696831d27a9SDon Lewis {
2697f1320723SAlfred Perlstein 	struct sigio *sigio;
2698831d27a9SDon Lewis 
2699f1320723SAlfred Perlstein 	SIGIO_LOCK();
2700f1320723SAlfred Perlstein 	sigio = *sigiop;
2701f1320723SAlfred Perlstein 	if (sigio == NULL) {
2702f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2703f1320723SAlfred Perlstein 		return;
2704f1320723SAlfred Perlstein 	}
2705831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2706628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
27072b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
27082c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2709628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2710831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2711831d27a9SDon Lewis 		struct proc *p;
2712831d27a9SDon Lewis 
2713f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2714628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2715628d2653SJohn Baldwin 			PROC_LOCK(p);
27162b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2717831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
27182c42a146SMarcel Moolenaar 				psignal(p, sig);
2719628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2720628d2653SJohn Baldwin 		}
2721f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2722831d27a9SDon Lewis 	}
2723f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2724831d27a9SDon Lewis }
2725cb679c38SJonathan Lemon 
2726cb679c38SJonathan Lemon static int
2727cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2728cb679c38SJonathan Lemon {
2729cb679c38SJonathan Lemon 	struct proc *p = curproc;
2730cb679c38SJonathan Lemon 
2731cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2732cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2733cb679c38SJonathan Lemon 
2734ad3b9257SJohn-Mark Gurney 	knlist_add(&p->p_klist, kn, 0);
2735cb679c38SJonathan Lemon 
2736cb679c38SJonathan Lemon 	return (0);
2737cb679c38SJonathan Lemon }
2738cb679c38SJonathan Lemon 
2739cb679c38SJonathan Lemon static void
2740cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2741cb679c38SJonathan Lemon {
2742cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2743cb679c38SJonathan Lemon 
2744ad3b9257SJohn-Mark Gurney 	knlist_remove(&p->p_klist, kn, 0);
2745cb679c38SJonathan Lemon }
2746cb679c38SJonathan Lemon 
2747cb679c38SJonathan Lemon /*
2748cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2749cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2750cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2751cb679c38SJonathan Lemon  * isn't worth the trouble.
2752cb679c38SJonathan Lemon  */
2753cb679c38SJonathan Lemon static int
2754cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2755cb679c38SJonathan Lemon {
2756cb679c38SJonathan Lemon 
2757cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2758cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2759cb679c38SJonathan Lemon 
2760cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2761cb679c38SJonathan Lemon 			kn->kn_data++;
2762cb679c38SJonathan Lemon 	}
2763cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2764cb679c38SJonathan Lemon }
276590af4afaSJohn Baldwin 
276690af4afaSJohn Baldwin struct sigacts *
276790af4afaSJohn Baldwin sigacts_alloc(void)
276890af4afaSJohn Baldwin {
276990af4afaSJohn Baldwin 	struct sigacts *ps;
277090af4afaSJohn Baldwin 
277190af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
277290af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
277390af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
277490af4afaSJohn Baldwin 	return (ps);
277590af4afaSJohn Baldwin }
277690af4afaSJohn Baldwin 
277790af4afaSJohn Baldwin void
277890af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
277990af4afaSJohn Baldwin {
278090af4afaSJohn Baldwin 
278190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
278290af4afaSJohn Baldwin 	ps->ps_refcnt--;
278390af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
278490af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
278590af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
278690af4afaSJohn Baldwin 	} else
278790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
278890af4afaSJohn Baldwin }
278990af4afaSJohn Baldwin 
279090af4afaSJohn Baldwin struct sigacts *
279190af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
279290af4afaSJohn Baldwin {
279390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
279490af4afaSJohn Baldwin 	ps->ps_refcnt++;
279590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
279690af4afaSJohn Baldwin 	return (ps);
279790af4afaSJohn Baldwin }
279890af4afaSJohn Baldwin 
279990af4afaSJohn Baldwin void
280090af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
280190af4afaSJohn Baldwin {
280290af4afaSJohn Baldwin 
280390af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
280490af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
280590af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
280690af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
280790af4afaSJohn Baldwin }
280890af4afaSJohn Baldwin 
280990af4afaSJohn Baldwin int
281090af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
281190af4afaSJohn Baldwin {
281290af4afaSJohn Baldwin 	int shared;
281390af4afaSJohn Baldwin 
281490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
281590af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
281690af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
281790af4afaSJohn Baldwin 	return (shared);
281890af4afaSJohn Baldwin }
2819