xref: /freebsd/sys/kern/kern_sig.c (revision 407948a530b8c08803de44536578ac593dea53c4)
19454b2d8SWarner Losh /*-
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);
851a88a252SMaxim Sobolev 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);
884093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
89cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
90cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
91cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
924093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
93a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
94a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
95c197abc4SMike Makonnen static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
96cb679c38SJonathan Lemon 
97cb679c38SJonathan Lemon struct filterops sig_filtops =
98cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
99cb679c38SJonathan Lemon 
10057308494SJoerg Wunsch static int	kern_logsigexit = 1;
1013d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1023d177f46SBill Fumerola     &kern_logsigexit, 0,
1033d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10457308494SJoerg Wunsch 
1052b87b6d4SRobert Watson /*
1062b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1072b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1082b87b6d4SRobert Watson  * in the right situations.
1092b87b6d4SRobert Watson  */
1102b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1112b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1122b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1142b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1152b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1162b87b6d4SRobert Watson 
11722d4b0fbSJohn Polstra int sugid_coredump;
1183d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1193d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
120c87e2930SDavid Greenman 
121e5a28db9SPaul Saab static int	do_coredump = 1;
122e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
123e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
124e5a28db9SPaul Saab 
1256141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
1266141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
1276141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
1286141e04aSJohn-Mark Gurney 
1292c42a146SMarcel Moolenaar /*
1302c42a146SMarcel Moolenaar  * Signal properties and actions.
1312c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1322c42a146SMarcel Moolenaar  * according to the following properties:
1332c42a146SMarcel Moolenaar  */
1342c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1352c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1362c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1372c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1382c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1392c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1402c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
141da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
142df8bae1dSRodney W. Grimes 
1432c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
144da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
145da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
146da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
150da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1512c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
152da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1532c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1542c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1552c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
156da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
157da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
158da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
159da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
160da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
161da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
162da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
163da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
164da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
165da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
166da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1672c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1682c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
169da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
170da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
171da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
172da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
173da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
174da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1752c42a146SMarcel Moolenaar };
1762c42a146SMarcel Moolenaar 
177fbbeeb6cSBruce Evans /*
178fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
179fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
180fbbeeb6cSBruce Evans  * action, the process stops in issignal().
181e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
182fbbeeb6cSBruce Evans  *
18333510ef1SBruce Evans  * MP SAFE.
184fbbeeb6cSBruce Evans  */
185fbbeeb6cSBruce Evans int
186e602ba25SJulian Elischer cursig(struct thread *td)
187fbbeeb6cSBruce Evans {
188c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
18990af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
190179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1914093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
192fbbeeb6cSBruce Evans }
193fbbeeb6cSBruce Evans 
19479065dbaSBruce Evans /*
19579065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1964093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1974093529dSJeff Roberson  * unmasked in td_sigmask.
19879065dbaSBruce Evans  */
19979065dbaSBruce Evans void
2004093529dSJeff Roberson signotify(struct thread *td)
20179065dbaSBruce Evans {
2024093529dSJeff Roberson 	struct proc *p;
2039dde3bc9SDavid Xu 	sigset_t set, saved;
2044093529dSJeff Roberson 
2054093529dSJeff Roberson 	p = td->td_proc;
20679065dbaSBruce Evans 
20779065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2084093529dSJeff Roberson 
2094093529dSJeff Roberson 	/*
2104093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2114093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2124093529dSJeff Roberson 	 */
2134093529dSJeff Roberson 	set = p->p_siglist;
2149dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
2159dde3bc9SDavid Xu 		saved = p->p_siglist;
2164093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2174093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2184093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2194093529dSJeff Roberson 
2208b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
22179065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2224093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
22379065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
22479065dbaSBruce Evans 	}
2259dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2266dbc0850SJohn Baldwin 		if (!SIGSETEQ(saved, p->p_siglist)) {
2279dde3bc9SDavid Xu 			/* pending set changed */
2289dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
2299dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
2309dde3bc9SDavid Xu 		}
2319dde3bc9SDavid Xu 	}
2328b94a061SJohn Baldwin }
2338b94a061SJohn Baldwin 
2348b94a061SJohn Baldwin int
2358b94a061SJohn Baldwin sigonstack(size_t sp)
2368b94a061SJohn Baldwin {
237a30ec4b9SDavid Xu 	struct thread *td = curthread;
2388b94a061SJohn Baldwin 
239a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
2401930e303SPoul-Henning Kamp #if defined(COMPAT_43)
241a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
242a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
243a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
2448b94a061SJohn Baldwin #else
245a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
2468b94a061SJohn Baldwin #endif
2478b94a061SJohn Baldwin 	    : 0);
2488b94a061SJohn Baldwin }
24979065dbaSBruce Evans 
2506f841fb7SMarcel Moolenaar static __inline int
2516f841fb7SMarcel Moolenaar sigprop(int sig)
2522c42a146SMarcel Moolenaar {
2536f841fb7SMarcel Moolenaar 
2542c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2552c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2562c42a146SMarcel Moolenaar 	return (0);
257df8bae1dSRodney W. Grimes }
2582c42a146SMarcel Moolenaar 
2594093529dSJeff Roberson int
2606f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2612c42a146SMarcel Moolenaar {
2622c42a146SMarcel Moolenaar 	int i;
2632c42a146SMarcel Moolenaar 
2646f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2652c42a146SMarcel Moolenaar 		if (set->__bits[i])
2662c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
267df8bae1dSRodney W. Grimes 	return (0);
268df8bae1dSRodney W. Grimes }
269df8bae1dSRodney W. Grimes 
2702c42a146SMarcel Moolenaar /*
2718f19eb88SIan Dowse  * kern_sigaction
2722c42a146SMarcel Moolenaar  * sigaction
27323eeeff7SPeter Wemm  * freebsd4_sigaction
2742c42a146SMarcel Moolenaar  * osigaction
27525d6dc06SJohn Baldwin  *
27625d6dc06SJohn Baldwin  * MPSAFE
2772c42a146SMarcel Moolenaar  */
2788f19eb88SIan Dowse int
27923eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2808f19eb88SIan Dowse 	struct thread *td;
2812c42a146SMarcel Moolenaar 	register int sig;
2822c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
28323eeeff7SPeter Wemm 	int flags;
284df8bae1dSRodney W. Grimes {
28590af4afaSJohn Baldwin 	struct sigacts *ps;
2864093529dSJeff Roberson 	struct thread *td0;
2878f19eb88SIan Dowse 	struct proc *p = td->td_proc;
288df8bae1dSRodney W. Grimes 
2892899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2902c42a146SMarcel Moolenaar 		return (EINVAL);
2912c42a146SMarcel Moolenaar 
292628d2653SJohn Baldwin 	PROC_LOCK(p);
293628d2653SJohn Baldwin 	ps = p->p_sigacts;
29490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2952c42a146SMarcel Moolenaar 	if (oact) {
2962c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2972c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2982c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2992c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
3002c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
3012c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
3022c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
3032c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
3042c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
3052c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
3062c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
3072c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
3082c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
30990af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
3102c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
31190af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
3122c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
3132c42a146SMarcel Moolenaar 	}
3142c42a146SMarcel Moolenaar 	if (act) {
3152c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
316628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
31790af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
318628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3192c42a146SMarcel Moolenaar 			return (EINVAL);
320628d2653SJohn Baldwin 		}
3212c42a146SMarcel Moolenaar 
322df8bae1dSRodney W. Grimes 		/*
323df8bae1dSRodney W. Grimes 		 * Change setting atomically.
324df8bae1dSRodney W. Grimes 		 */
3252c42a146SMarcel Moolenaar 
3262c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3272c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3282c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
329aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
330aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
33180f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
33280f42b55SIan Dowse 		} else {
33380f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3342c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3352c42a146SMarcel Moolenaar 		}
3362c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3372c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
338df8bae1dSRodney W. Grimes 		else
3392c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3402c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3412c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
342df8bae1dSRodney W. Grimes 		else
3432c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3442c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3452c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3461e41c1b5SSteven Wallace 		else
3472c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3482c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3492c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
350289ccde0SPeter Wemm 		else
3512c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
3522c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3532c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
35490af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3556626c604SJulian Elischer 			else
35690af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
357ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
358245f17d4SJoerg Wunsch 				/*
3592c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3602c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3612c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3622c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
363245f17d4SJoerg Wunsch 				 */
364245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
36590af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3666626c604SJulian Elischer 				else
36790af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
368245f17d4SJoerg Wunsch 			} else
36990af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
370ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
37190af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
372ba1551caSIan Dowse 			else
37390af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
374df8bae1dSRodney W. Grimes 		}
375df8bae1dSRodney W. Grimes 		/*
37690af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3772c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
37890af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3792c42a146SMarcel Moolenaar 		 * have to restart the process.
380df8bae1dSRodney W. Grimes 		 */
3812c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3822c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3832c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3849dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
3859dde3bc9SDavid Xu 			     SIGISMEMBER(p->p_siglist, sig)) {
3869dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
3879dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
3889dde3bc9SDavid Xu 			}
3892c42a146SMarcel Moolenaar 			/* never to be seen again */
3901d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
391a9a48d68SDavid Xu 			mtx_lock_spin(&sched_lock);
3924093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3934093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
394a9a48d68SDavid Xu 			mtx_unlock_spin(&sched_lock);
3952c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3962c42a146SMarcel Moolenaar 				/* easier in psignal */
39790af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
39890af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
399645682fdSLuoqi Chen 		} else {
40090af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
4012c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
40290af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
4032c42a146SMarcel Moolenaar 			else
40490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
4052c42a146SMarcel Moolenaar 		}
40623eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
40723eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
40823eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
40923eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
41023eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
41123eeeff7SPeter Wemm 		else
41223eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
41323eeeff7SPeter Wemm #endif
414e8ebc08fSPeter Wemm #ifdef COMPAT_43
415645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
41623eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41723eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
418645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
419645682fdSLuoqi Chen 		else
420645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
421e8ebc08fSPeter Wemm #endif
422df8bae1dSRodney W. Grimes 	}
42390af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
424628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4252c42a146SMarcel Moolenaar 	return (0);
4262c42a146SMarcel Moolenaar }
4272c42a146SMarcel Moolenaar 
4282c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4292c42a146SMarcel Moolenaar struct sigaction_args {
4302c42a146SMarcel Moolenaar 	int	sig;
4312c42a146SMarcel Moolenaar 	struct	sigaction *act;
4322c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4332c42a146SMarcel Moolenaar };
4342c42a146SMarcel Moolenaar #endif
435fb99ab88SMatthew Dillon /*
436fb99ab88SMatthew Dillon  * MPSAFE
437fb99ab88SMatthew Dillon  */
4382c42a146SMarcel Moolenaar int
439b40ce416SJulian Elischer sigaction(td, uap)
440b40ce416SJulian Elischer 	struct thread *td;
4412c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4422c42a146SMarcel Moolenaar {
4432c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4442c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4452c42a146SMarcel Moolenaar 	int error;
4462c42a146SMarcel Moolenaar 
4476f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4486f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4492c42a146SMarcel Moolenaar 	if (actp) {
4506f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4512c42a146SMarcel Moolenaar 		if (error)
45244443757STim J. Robbins 			return (error);
4532c42a146SMarcel Moolenaar 	}
4548f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
45525d6dc06SJohn Baldwin 	if (oactp && !error)
4566f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4572c42a146SMarcel Moolenaar 	return (error);
4582c42a146SMarcel Moolenaar }
4592c42a146SMarcel Moolenaar 
46023eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
46123eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
46223eeeff7SPeter Wemm struct freebsd4_sigaction_args {
46323eeeff7SPeter Wemm 	int	sig;
46423eeeff7SPeter Wemm 	struct	sigaction *act;
46523eeeff7SPeter Wemm 	struct	sigaction *oact;
46623eeeff7SPeter Wemm };
46723eeeff7SPeter Wemm #endif
46823eeeff7SPeter Wemm /*
46923eeeff7SPeter Wemm  * MPSAFE
47023eeeff7SPeter Wemm  */
47123eeeff7SPeter Wemm int
47223eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
47323eeeff7SPeter Wemm 	struct thread *td;
47423eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
47523eeeff7SPeter Wemm {
47623eeeff7SPeter Wemm 	struct sigaction act, oact;
47723eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
47823eeeff7SPeter Wemm 	int error;
47923eeeff7SPeter Wemm 
48023eeeff7SPeter Wemm 
48123eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
48223eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
48323eeeff7SPeter Wemm 	if (actp) {
48423eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
48523eeeff7SPeter Wemm 		if (error)
48644443757STim J. Robbins 			return (error);
48723eeeff7SPeter Wemm 	}
48823eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
489a14e1189SJohn Baldwin 	if (oactp && !error)
49023eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
49123eeeff7SPeter Wemm 	return (error);
49223eeeff7SPeter Wemm }
49323eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
49423eeeff7SPeter Wemm 
49531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4962c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4972c42a146SMarcel Moolenaar struct osigaction_args {
4982c42a146SMarcel Moolenaar 	int	signum;
4992c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
5002c42a146SMarcel Moolenaar 	struct	osigaction *osa;
5012c42a146SMarcel Moolenaar };
5022c42a146SMarcel Moolenaar #endif
503fb99ab88SMatthew Dillon /*
504fb99ab88SMatthew Dillon  * MPSAFE
505fb99ab88SMatthew Dillon  */
5062c42a146SMarcel Moolenaar int
507b40ce416SJulian Elischer osigaction(td, uap)
508b40ce416SJulian Elischer 	struct thread *td;
5092c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
5102c42a146SMarcel Moolenaar {
5112c42a146SMarcel Moolenaar 	struct osigaction sa;
5122c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5132c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5142c42a146SMarcel Moolenaar 	int error;
5152c42a146SMarcel Moolenaar 
5166f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5176f841fb7SMarcel Moolenaar 		return (EINVAL);
518fb99ab88SMatthew Dillon 
5196f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5206f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
521fb99ab88SMatthew Dillon 
5222c42a146SMarcel Moolenaar 	if (nsap) {
5236f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5242c42a146SMarcel Moolenaar 		if (error)
52544443757STim J. Robbins 			return (error);
5262c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5272c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5282c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5292c42a146SMarcel Moolenaar 	}
53023eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5312c42a146SMarcel Moolenaar 	if (osap && !error) {
5322c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5332c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5342c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5356f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5362c42a146SMarcel Moolenaar 	}
5372c42a146SMarcel Moolenaar 	return (error);
5382c42a146SMarcel Moolenaar }
53923eeeff7SPeter Wemm 
54023eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
54123eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
54223eeeff7SPeter Wemm int
54323eeeff7SPeter Wemm osigreturn(td, uap)
54423eeeff7SPeter Wemm 	struct thread *td;
54523eeeff7SPeter Wemm 	struct osigreturn_args *uap;
54623eeeff7SPeter Wemm {
54723eeeff7SPeter Wemm 
54823eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
54923eeeff7SPeter Wemm }
55023eeeff7SPeter Wemm #endif
55131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes /*
554df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
555df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
556df8bae1dSRodney W. Grimes  */
557df8bae1dSRodney W. Grimes void
558df8bae1dSRodney W. Grimes siginit(p)
559df8bae1dSRodney W. Grimes 	struct proc *p;
560df8bae1dSRodney W. Grimes {
561df8bae1dSRodney W. Grimes 	register int i;
56290af4afaSJohn Baldwin 	struct sigacts *ps;
563df8bae1dSRodney W. Grimes 
564628d2653SJohn Baldwin 	PROC_LOCK(p);
56590af4afaSJohn Baldwin 	ps = p->p_sigacts;
56690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5672c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5682c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
56990af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
57090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
571628d2653SJohn Baldwin 	PROC_UNLOCK(p);
572df8bae1dSRodney W. Grimes }
573df8bae1dSRodney W. Grimes 
574df8bae1dSRodney W. Grimes /*
575df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
576df8bae1dSRodney W. Grimes  */
577df8bae1dSRodney W. Grimes void
578a30ec4b9SDavid Xu execsigs(struct proc *p)
579df8bae1dSRodney W. Grimes {
580a30ec4b9SDavid Xu 	struct sigacts *ps;
581a30ec4b9SDavid Xu 	int sig;
582a30ec4b9SDavid Xu 	struct thread *td;
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 	/*
585df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5864093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
587df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
588df8bae1dSRodney W. Grimes 	 */
5899b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
590a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
591628d2653SJohn Baldwin 	ps = p->p_sigacts;
59290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
59390af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
59490af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
59590af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
5962c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5972c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
59890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
5991d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
6004093529dSJeff Roberson 			/*
6014093529dSJeff Roberson 			 * There is only one thread at this point.
6024093529dSJeff Roberson 			 */
603a30ec4b9SDavid Xu 			SIGDELSET(td->td_siglist, sig);
604df8bae1dSRodney W. Grimes 		}
6052c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
606df8bae1dSRodney W. Grimes 	}
607df8bae1dSRodney W. Grimes 	/*
608df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
609df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
610df8bae1dSRodney W. Grimes 	 */
611a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
612a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
613a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
614a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
61580e907a1SPeter Wemm 	/*
61680e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
61780e907a1SPeter Wemm 	 */
61890af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
619c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
620c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
62190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
622df8bae1dSRodney W. Grimes }
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes /*
625e77daab1SJohn Baldwin  * kern_sigprocmask()
6267c8fdcbdSMatthew Dillon  *
627628d2653SJohn Baldwin  *	Manipulate signal mask.
628df8bae1dSRodney W. Grimes  */
629e77daab1SJohn Baldwin int
630e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6314093529dSJeff Roberson 	struct thread *td;
6322c42a146SMarcel Moolenaar 	int how;
6332c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
634645682fdSLuoqi Chen 	int old;
6352c42a146SMarcel Moolenaar {
6362c42a146SMarcel Moolenaar 	int error;
6372c42a146SMarcel Moolenaar 
6384093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6392c42a146SMarcel Moolenaar 	if (oset != NULL)
6404093529dSJeff Roberson 		*oset = td->td_sigmask;
6412c42a146SMarcel Moolenaar 
6422c42a146SMarcel Moolenaar 	error = 0;
6432c42a146SMarcel Moolenaar 	if (set != NULL) {
6442c42a146SMarcel Moolenaar 		switch (how) {
6452c42a146SMarcel Moolenaar 		case SIG_BLOCK:
646645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6474093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6482c42a146SMarcel Moolenaar 			break;
6492c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6504093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6514093529dSJeff Roberson 			signotify(td);
6522c42a146SMarcel Moolenaar 			break;
6532c42a146SMarcel Moolenaar 		case SIG_SETMASK:
654645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
655645682fdSLuoqi Chen 			if (old)
6564093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
657645682fdSLuoqi Chen 			else
6584093529dSJeff Roberson 				td->td_sigmask = *set;
6594093529dSJeff Roberson 			signotify(td);
6602c42a146SMarcel Moolenaar 			break;
6612c42a146SMarcel Moolenaar 		default:
6622c42a146SMarcel Moolenaar 			error = EINVAL;
6632c42a146SMarcel Moolenaar 			break;
6642c42a146SMarcel Moolenaar 		}
6652c42a146SMarcel Moolenaar 	}
6664093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6672c42a146SMarcel Moolenaar 	return (error);
6682c42a146SMarcel Moolenaar }
6692c42a146SMarcel Moolenaar 
6707c8fdcbdSMatthew Dillon /*
671e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6727c8fdcbdSMatthew Dillon  */
6737c8fdcbdSMatthew Dillon 
674d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
675df8bae1dSRodney W. Grimes struct sigprocmask_args {
676df8bae1dSRodney W. Grimes 	int	how;
6772c42a146SMarcel Moolenaar 	const sigset_t *set;
6782c42a146SMarcel Moolenaar 	sigset_t *oset;
679df8bae1dSRodney W. Grimes };
680d2d3e875SBruce Evans #endif
68126f9a767SRodney W. Grimes int
682b40ce416SJulian Elischer sigprocmask(td, uap)
683b40ce416SJulian Elischer 	register struct thread *td;
684df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
685df8bae1dSRodney W. Grimes {
6862c42a146SMarcel Moolenaar 	sigset_t set, oset;
6872c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6882c42a146SMarcel Moolenaar 	int error;
689df8bae1dSRodney W. Grimes 
6906f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6916f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6922c42a146SMarcel Moolenaar 	if (setp) {
6936f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6942c42a146SMarcel Moolenaar 		if (error)
6952c42a146SMarcel Moolenaar 			return (error);
696df8bae1dSRodney W. Grimes 	}
697e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
6982c42a146SMarcel Moolenaar 	if (osetp && !error) {
6996f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
7002c42a146SMarcel Moolenaar 	}
7012c42a146SMarcel Moolenaar 	return (error);
7022c42a146SMarcel Moolenaar }
7032c42a146SMarcel Moolenaar 
70431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7057c8fdcbdSMatthew Dillon /*
7067c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
7077c8fdcbdSMatthew Dillon  */
7082c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7092c42a146SMarcel Moolenaar struct osigprocmask_args {
7102c42a146SMarcel Moolenaar 	int	how;
7112c42a146SMarcel Moolenaar 	osigset_t mask;
7122c42a146SMarcel Moolenaar };
7132c42a146SMarcel Moolenaar #endif
7142c42a146SMarcel Moolenaar int
715b40ce416SJulian Elischer osigprocmask(td, uap)
716b40ce416SJulian Elischer 	register struct thread *td;
7172c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7182c42a146SMarcel Moolenaar {
7192c42a146SMarcel Moolenaar 	sigset_t set, oset;
7202c42a146SMarcel Moolenaar 	int error;
7212c42a146SMarcel Moolenaar 
7222c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
723e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
724b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
725df8bae1dSRodney W. Grimes 	return (error);
726df8bae1dSRodney W. Grimes }
72731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
728df8bae1dSRodney W. Grimes 
729d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
730df8bae1dSRodney W. Grimes struct sigpending_args {
7312c42a146SMarcel Moolenaar 	sigset_t	*set;
732df8bae1dSRodney W. Grimes };
733d2d3e875SBruce Evans #endif
734fb99ab88SMatthew Dillon /*
735fb99ab88SMatthew Dillon  * MPSAFE
736fb99ab88SMatthew Dillon  */
73726f9a767SRodney W. Grimes int
738a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
739a447cd8bSJeff Roberson {
740a447cd8bSJeff Roberson 	siginfo_t info;
741a447cd8bSJeff Roberson 	sigset_t set;
742a447cd8bSJeff Roberson 	int error;
743a447cd8bSJeff Roberson 
744a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
74536939a0aSDavid Xu 	if (error) {
74636939a0aSDavid Xu 		td->td_retval[0] = error;
74736939a0aSDavid Xu 		return (0);
74836939a0aSDavid Xu 	}
749a447cd8bSJeff Roberson 
750a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
75136939a0aSDavid Xu 	if (error) {
75236939a0aSDavid Xu 		if (error == ERESTART)
753a447cd8bSJeff Roberson 			return (error);
75436939a0aSDavid Xu 		td->td_retval[0] = error;
75536939a0aSDavid Xu 		return (0);
75636939a0aSDavid Xu 	}
757a447cd8bSJeff Roberson 
758a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
759a447cd8bSJeff Roberson 	/* Repost if we got an error. */
76018440c7fSJohn Baldwin 	if (error && info.si_signo) {
76118440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
762c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
76318440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
76418440c7fSJohn Baldwin 	}
76536939a0aSDavid Xu 	td->td_retval[0] = error;
76636939a0aSDavid Xu 	return (0);
767a447cd8bSJeff Roberson }
768a447cd8bSJeff Roberson /*
769a447cd8bSJeff Roberson  * MPSAFE
770a447cd8bSJeff Roberson  */
771a447cd8bSJeff Roberson int
772a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
773a447cd8bSJeff Roberson {
774a447cd8bSJeff Roberson 	struct timespec ts;
775a447cd8bSJeff Roberson 	struct timespec *timeout;
776a447cd8bSJeff Roberson 	sigset_t set;
777a447cd8bSJeff Roberson 	siginfo_t info;
778a447cd8bSJeff Roberson 	int error;
779a447cd8bSJeff Roberson 
780a447cd8bSJeff Roberson 	if (uap->timeout) {
781a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
782a447cd8bSJeff Roberson 		if (error)
783a447cd8bSJeff Roberson 			return (error);
784a447cd8bSJeff Roberson 
785a447cd8bSJeff Roberson 		timeout = &ts;
786a447cd8bSJeff Roberson 	} else
787a447cd8bSJeff Roberson 		timeout = NULL;
788a447cd8bSJeff Roberson 
789a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
790a447cd8bSJeff Roberson 	if (error)
791a447cd8bSJeff Roberson 		return (error);
792a447cd8bSJeff Roberson 
793a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
794a447cd8bSJeff Roberson 	if (error)
795a447cd8bSJeff Roberson 		return (error);
7969dde3bc9SDavid Xu 
797418228dfSDavid Xu 	if (uap->info)
798a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
799a447cd8bSJeff Roberson 	/* Repost if we got an error. */
80018440c7fSJohn Baldwin 	if (error && info.si_signo) {
80118440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
802c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
80318440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
804418228dfSDavid Xu 	} else {
805418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
80618440c7fSJohn Baldwin 	}
807a447cd8bSJeff Roberson 	return (error);
808a447cd8bSJeff Roberson }
809a447cd8bSJeff Roberson 
810a447cd8bSJeff Roberson /*
811a447cd8bSJeff Roberson  * MPSAFE
812a447cd8bSJeff Roberson  */
813a447cd8bSJeff Roberson int
814a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
815a447cd8bSJeff Roberson {
816a447cd8bSJeff Roberson 	siginfo_t info;
817a447cd8bSJeff Roberson 	sigset_t set;
818a447cd8bSJeff Roberson 	int error;
819a447cd8bSJeff Roberson 
820a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
821a447cd8bSJeff Roberson 	if (error)
822a447cd8bSJeff Roberson 		return (error);
823a447cd8bSJeff Roberson 
824a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
825a447cd8bSJeff Roberson 	if (error)
826a447cd8bSJeff Roberson 		return (error);
827a447cd8bSJeff Roberson 
828418228dfSDavid Xu 	if (uap->info)
829a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
830a447cd8bSJeff Roberson 	/* Repost if we got an error. */
83118440c7fSJohn Baldwin 	if (error && info.si_signo) {
83218440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
833c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
83418440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
835418228dfSDavid Xu 	} else {
836418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
83718440c7fSJohn Baldwin 	}
838a447cd8bSJeff Roberson 	return (error);
839a447cd8bSJeff Roberson }
840a447cd8bSJeff Roberson 
841a447cd8bSJeff Roberson static int
8423074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
843a447cd8bSJeff Roberson     struct timespec *timeout)
844a447cd8bSJeff Roberson {
8453074d1b4SDavid Xu 	struct sigacts *ps;
8466675b36eSDavid Xu 	sigset_t savedmask;
847a447cd8bSJeff Roberson 	struct proc *p;
8481089f031SDavid Xu 	int error, sig, hz, i, timevalid = 0;
8491089f031SDavid Xu 	struct timespec rts, ets, ts;
8501089f031SDavid Xu 	struct timeval tv;
851a447cd8bSJeff Roberson 
852a447cd8bSJeff Roberson 	p = td->td_proc;
853a447cd8bSJeff Roberson 	error = 0;
854a447cd8bSJeff Roberson 	sig = 0;
8553074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
856a447cd8bSJeff Roberson 
857a447cd8bSJeff Roberson 	PROC_LOCK(p);
858a447cd8bSJeff Roberson 	ps = p->p_sigacts;
8593074d1b4SDavid Xu 	savedmask = td->td_sigmask;
8601089f031SDavid Xu 	if (timeout) {
8611089f031SDavid Xu 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
8621089f031SDavid Xu 			timevalid = 1;
8631089f031SDavid Xu 			getnanouptime(&rts);
8641089f031SDavid Xu 		 	ets = rts;
8651089f031SDavid Xu 			timespecadd(&ets, timeout);
8661089f031SDavid Xu 		}
8671089f031SDavid Xu 	}
8683074d1b4SDavid Xu 
8693074d1b4SDavid Xu again:
8703074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
8713074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
8723074d1b4SDavid Xu 			continue;
8733074d1b4SDavid Xu 		if (SIGISMEMBER(td->td_siglist, i)) {
874418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
875418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8763074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
87790af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
878a447cd8bSJeff Roberson 			sig = cursig(td);
8793074d1b4SDavid Xu 			i = 0;
8803074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8813074d1b4SDavid Xu 		} else if (SIGISMEMBER(p->p_siglist, i)) {
8823074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
8833074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
8843074d1b4SDavid Xu 				wakeup(&p->p_siglist);
8853074d1b4SDavid Xu 			}
8863074d1b4SDavid Xu 			SIGDELSET(p->p_siglist, i);
8873074d1b4SDavid Xu 			SIGADDSET(td->td_siglist, i);
8883074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
8893074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8903074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
8913074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
8923074d1b4SDavid Xu 			sig = cursig(td);
8933074d1b4SDavid Xu 			i = 0;
8943074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8953074d1b4SDavid Xu 		}
8966675b36eSDavid Xu 		if (sig)
897a447cd8bSJeff Roberson 			goto out;
8983074d1b4SDavid Xu 	}
8993074d1b4SDavid Xu 	if (error)
9003074d1b4SDavid Xu 		goto out;
9013074d1b4SDavid Xu 
902a447cd8bSJeff Roberson 	/*
903a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
904a447cd8bSJeff Roberson 	 * signals.
905a447cd8bSJeff Roberson 	 */
906a447cd8bSJeff Roberson 	if (timeout) {
9071089f031SDavid Xu 		if (!timevalid) {
908a447cd8bSJeff Roberson 			error = EINVAL;
909a447cd8bSJeff Roberson 			goto out;
910a447cd8bSJeff Roberson 		}
9111089f031SDavid Xu 		getnanouptime(&rts);
9121089f031SDavid Xu 		if (timespeccmp(&rts, &ets, >=)) {
9133074d1b4SDavid Xu 			error = EAGAIN;
9143074d1b4SDavid Xu 			goto out;
9153074d1b4SDavid Xu 		}
9161089f031SDavid Xu 		ts = ets;
9171089f031SDavid Xu 		timespecsub(&ts, &rts);
9181089f031SDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
919a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
920a447cd8bSJeff Roberson 	} else
921a447cd8bSJeff Roberson 		hz = 0;
922a447cd8bSJeff Roberson 
9236675b36eSDavid Xu 	td->td_sigmask = savedmask;
9246675b36eSDavid Xu 	SIGSETNAND(td->td_sigmask, waitset);
9256675b36eSDavid Xu 	signotify(td);
92686b5e563SDag-Erling Smørgrav 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
9271089f031SDavid Xu 	if (timeout) {
9281089f031SDavid Xu 		if (error == ERESTART) {
9291089f031SDavid Xu 			/* timeout can not be restarted. */
9303074d1b4SDavid Xu 			error = EINTR;
9311089f031SDavid Xu 		} else if (error == EAGAIN) {
9321089f031SDavid Xu 			/* will calculate timeout by ourself. */
9331089f031SDavid Xu 			error = 0;
9341089f031SDavid Xu 		}
9351089f031SDavid Xu 	}
9363074d1b4SDavid Xu 	goto again;
9379dde3bc9SDavid Xu 
938a447cd8bSJeff Roberson out:
9396675b36eSDavid Xu 	td->td_sigmask = savedmask;
9406675b36eSDavid Xu 	signotify(td);
941a447cd8bSJeff Roberson 	if (sig) {
942a447cd8bSJeff Roberson 		sig_t action;
943a447cd8bSJeff Roberson 
9443074d1b4SDavid Xu 		error = 0;
9453074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
946a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
94790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
948a447cd8bSJeff Roberson #ifdef KTRACE
949a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
9503074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
951a447cd8bSJeff Roberson #endif
952a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
953a447cd8bSJeff Roberson 
954a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
9551089f031SDavid Xu 		bzero(info, sizeof(*info));
956a447cd8bSJeff Roberson 		info->si_signo = sig;
957a447cd8bSJeff Roberson 		info->si_code = 0;
9583074d1b4SDavid Xu 	}
959a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
960a447cd8bSJeff Roberson 	return (error);
961a447cd8bSJeff Roberson }
962a447cd8bSJeff Roberson 
963a447cd8bSJeff Roberson /*
964a447cd8bSJeff Roberson  * MPSAFE
965a447cd8bSJeff Roberson  */
966a447cd8bSJeff Roberson int
967b40ce416SJulian Elischer sigpending(td, uap)
968b40ce416SJulian Elischer 	struct thread *td;
969df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
970df8bae1dSRodney W. Grimes {
971b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
972628d2653SJohn Baldwin 	sigset_t siglist;
973df8bae1dSRodney W. Grimes 
974628d2653SJohn Baldwin 	PROC_LOCK(p);
9751d9c5696SJuli Mallett 	siglist = p->p_siglist;
9764093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
977628d2653SJohn Baldwin 	PROC_UNLOCK(p);
97848e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9792c42a146SMarcel Moolenaar }
9802c42a146SMarcel Moolenaar 
98131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9822c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9832c42a146SMarcel Moolenaar struct osigpending_args {
9842c42a146SMarcel Moolenaar 	int	dummy;
9852c42a146SMarcel Moolenaar };
9862c42a146SMarcel Moolenaar #endif
987fb99ab88SMatthew Dillon /*
988fb99ab88SMatthew Dillon  * MPSAFE
989fb99ab88SMatthew Dillon  */
9902c42a146SMarcel Moolenaar int
991b40ce416SJulian Elischer osigpending(td, uap)
992b40ce416SJulian Elischer 	struct thread *td;
9932c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9942c42a146SMarcel Moolenaar {
995b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9964093529dSJeff Roberson 	sigset_t siglist;
997b40ce416SJulian Elischer 
998628d2653SJohn Baldwin 	PROC_LOCK(p);
9994093529dSJeff Roberson 	siglist = p->p_siglist;
10004093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
1001628d2653SJohn Baldwin 	PROC_UNLOCK(p);
10029d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
1003df8bae1dSRodney W. Grimes 	return (0);
1004df8bae1dSRodney W. Grimes }
100531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1006df8bae1dSRodney W. Grimes 
10071930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1008df8bae1dSRodney W. Grimes /*
1009df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1010df8bae1dSRodney W. Grimes  */
1011d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1012df8bae1dSRodney W. Grimes struct osigvec_args {
1013df8bae1dSRodney W. Grimes 	int	signum;
1014df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1015df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1016df8bae1dSRodney W. Grimes };
1017d2d3e875SBruce Evans #endif
1018fb99ab88SMatthew Dillon /*
1019fb99ab88SMatthew Dillon  * MPSAFE
1020fb99ab88SMatthew Dillon  */
1021df8bae1dSRodney W. Grimes /* ARGSUSED */
102226f9a767SRodney W. Grimes int
1023b40ce416SJulian Elischer osigvec(td, uap)
1024b40ce416SJulian Elischer 	struct thread *td;
1025df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1026df8bae1dSRodney W. Grimes {
1027df8bae1dSRodney W. Grimes 	struct sigvec vec;
10282c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
10292c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
10302c42a146SMarcel Moolenaar 	int error;
1031df8bae1dSRodney W. Grimes 
10326f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10336f841fb7SMarcel Moolenaar 		return (EINVAL);
10346f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
10356f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
10362c42a146SMarcel Moolenaar 	if (nsap) {
10376f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
10382c42a146SMarcel Moolenaar 		if (error)
1039df8bae1dSRodney W. Grimes 			return (error);
10402c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
10412c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
10422c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
10432c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1044df8bae1dSRodney W. Grimes 	}
10455edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10462c42a146SMarcel Moolenaar 	if (osap && !error) {
10472c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
10482c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
10492c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
10502c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
10512c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
10526f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10532c42a146SMarcel Moolenaar 	}
10542c42a146SMarcel Moolenaar 	return (error);
1055df8bae1dSRodney W. Grimes }
1056df8bae1dSRodney W. Grimes 
1057d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1058df8bae1dSRodney W. Grimes struct osigblock_args {
1059df8bae1dSRodney W. Grimes 	int	mask;
1060df8bae1dSRodney W. Grimes };
1061d2d3e875SBruce Evans #endif
1062fb99ab88SMatthew Dillon /*
1063fb99ab88SMatthew Dillon  * MPSAFE
1064fb99ab88SMatthew Dillon  */
106526f9a767SRodney W. Grimes int
1066b40ce416SJulian Elischer osigblock(td, uap)
1067b40ce416SJulian Elischer 	register struct thread *td;
1068df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1069df8bae1dSRodney W. Grimes {
1070b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10712c42a146SMarcel Moolenaar 	sigset_t set;
1072df8bae1dSRodney W. Grimes 
10732c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10742c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1075628d2653SJohn Baldwin 	PROC_LOCK(p);
10764093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10774093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1078628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1079df8bae1dSRodney W. Grimes 	return (0);
1080df8bae1dSRodney W. Grimes }
1081df8bae1dSRodney W. Grimes 
1082d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1083df8bae1dSRodney W. Grimes struct osigsetmask_args {
1084df8bae1dSRodney W. Grimes 	int	mask;
1085df8bae1dSRodney W. Grimes };
1086d2d3e875SBruce Evans #endif
1087fb99ab88SMatthew Dillon /*
1088fb99ab88SMatthew Dillon  * MPSAFE
1089fb99ab88SMatthew Dillon  */
109026f9a767SRodney W. Grimes int
1091b40ce416SJulian Elischer osigsetmask(td, uap)
1092b40ce416SJulian Elischer 	struct thread *td;
1093df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1094df8bae1dSRodney W. Grimes {
1095b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10962c42a146SMarcel Moolenaar 	sigset_t set;
1097df8bae1dSRodney W. Grimes 
10982c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10992c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1100628d2653SJohn Baldwin 	PROC_LOCK(p);
11014093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
11024093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
11034093529dSJeff Roberson 	signotify(td);
1104628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1105df8bae1dSRodney W. Grimes 	return (0);
1106df8bae1dSRodney W. Grimes }
11071930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1108df8bae1dSRodney W. Grimes 
1109df8bae1dSRodney W. Grimes /*
1110df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
111197563428SAlexander Kabaev  * in the meantime.
1112b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1113b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1114b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1115df8bae1dSRodney W. Grimes  */
1116d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1117df8bae1dSRodney W. Grimes struct sigsuspend_args {
11182c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1119df8bae1dSRodney W. Grimes };
1120d2d3e875SBruce Evans #endif
1121fb99ab88SMatthew Dillon /*
1122fb99ab88SMatthew Dillon  * MPSAFE
1123fb99ab88SMatthew Dillon  */
1124df8bae1dSRodney W. Grimes /* ARGSUSED */
112526f9a767SRodney W. Grimes int
1126b40ce416SJulian Elischer sigsuspend(td, uap)
1127b40ce416SJulian Elischer 	struct thread *td;
1128df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1129df8bae1dSRodney W. Grimes {
11302c42a146SMarcel Moolenaar 	sigset_t mask;
11312c42a146SMarcel Moolenaar 	int error;
11322c42a146SMarcel Moolenaar 
11336f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11342c42a146SMarcel Moolenaar 	if (error)
11352c42a146SMarcel Moolenaar 		return (error);
11368f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11378f19eb88SIan Dowse }
11388f19eb88SIan Dowse 
11398f19eb88SIan Dowse int
11408f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11418f19eb88SIan Dowse {
11428f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 	/*
1145645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1146df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1147df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1148df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1149df8bae1dSRodney W. Grimes 	 * to indicate this.
1150df8bae1dSRodney W. Grimes 	 */
1151628d2653SJohn Baldwin 	PROC_LOCK(p);
11524093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11535e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1154645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11554093529dSJeff Roberson 	td->td_sigmask = mask;
11564093529dSJeff Roberson 	signotify(td);
115786b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11582c42a146SMarcel Moolenaar 		/* void */;
1159628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11602c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11612c42a146SMarcel Moolenaar 	return (EINTR);
11622c42a146SMarcel Moolenaar }
11632c42a146SMarcel Moolenaar 
116431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
116597563428SAlexander Kabaev /*
116697563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
116797563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
116897563428SAlexander Kabaev  */
11692c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11702c42a146SMarcel Moolenaar struct osigsuspend_args {
11712c42a146SMarcel Moolenaar 	osigset_t mask;
11722c42a146SMarcel Moolenaar };
11732c42a146SMarcel Moolenaar #endif
1174fb99ab88SMatthew Dillon /*
1175fb99ab88SMatthew Dillon  * MPSAFE
1176fb99ab88SMatthew Dillon  */
11772c42a146SMarcel Moolenaar /* ARGSUSED */
11782c42a146SMarcel Moolenaar int
1179b40ce416SJulian Elischer osigsuspend(td, uap)
1180b40ce416SJulian Elischer 	struct thread *td;
11812c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11822c42a146SMarcel Moolenaar {
1183b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1184645682fdSLuoqi Chen 	sigset_t mask;
11852c42a146SMarcel Moolenaar 
1186628d2653SJohn Baldwin 	PROC_LOCK(p);
11874093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11885e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1189645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1190645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11914093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11924093529dSJeff Roberson 	signotify(td);
119386b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1194df8bae1dSRodney W. Grimes 		/* void */;
1195628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1196df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1197df8bae1dSRodney W. Grimes 	return (EINTR);
1198df8bae1dSRodney W. Grimes }
119931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1200df8bae1dSRodney W. Grimes 
12011930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1202d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1203df8bae1dSRodney W. Grimes struct osigstack_args {
1204df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1205df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1206df8bae1dSRodney W. Grimes };
1207d2d3e875SBruce Evans #endif
1208fb99ab88SMatthew Dillon /*
1209fb99ab88SMatthew Dillon  * MPSAFE
1210fb99ab88SMatthew Dillon  */
1211df8bae1dSRodney W. Grimes /* ARGSUSED */
121226f9a767SRodney W. Grimes int
1213b40ce416SJulian Elischer osigstack(td, uap)
1214b40ce416SJulian Elischer 	struct thread *td;
1215df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1216df8bae1dSRodney W. Grimes {
12175afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1218fb99ab88SMatthew Dillon 	int error = 0;
1219fb99ab88SMatthew Dillon 
1220d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12215afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12225afe0c99SJohn Baldwin 		if (error)
12235afe0c99SJohn Baldwin 			return (error);
1224df8bae1dSRodney W. Grimes 	}
1225a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12265afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12275afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1228a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1229a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1230a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1231a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12325afe0c99SJohn Baldwin 	}
12335afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12345afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12355afe0c99SJohn Baldwin 
1236fb99ab88SMatthew Dillon 	return (error);
1237df8bae1dSRodney W. Grimes }
12381930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1239df8bae1dSRodney W. Grimes 
1240d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1241df8bae1dSRodney W. Grimes struct sigaltstack_args {
12422c42a146SMarcel Moolenaar 	stack_t	*ss;
12432c42a146SMarcel Moolenaar 	stack_t	*oss;
1244df8bae1dSRodney W. Grimes };
1245d2d3e875SBruce Evans #endif
1246fb99ab88SMatthew Dillon /*
1247fb99ab88SMatthew Dillon  * MPSAFE
1248fb99ab88SMatthew Dillon  */
1249df8bae1dSRodney W. Grimes /* ARGSUSED */
125026f9a767SRodney W. Grimes int
1251b40ce416SJulian Elischer sigaltstack(td, uap)
1252b40ce416SJulian Elischer 	struct thread *td;
1253df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1254df8bae1dSRodney W. Grimes {
12558f19eb88SIan Dowse 	stack_t ss, oss;
12568f19eb88SIan Dowse 	int error;
12578f19eb88SIan Dowse 
12588f19eb88SIan Dowse 	if (uap->ss != NULL) {
12598f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12608f19eb88SIan Dowse 		if (error)
12618f19eb88SIan Dowse 			return (error);
12628f19eb88SIan Dowse 	}
12638f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12648f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12658f19eb88SIan Dowse 	if (error)
12668f19eb88SIan Dowse 		return (error);
12678f19eb88SIan Dowse 	if (uap->oss != NULL)
12688f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12698f19eb88SIan Dowse 	return (error);
12708f19eb88SIan Dowse }
12718f19eb88SIan Dowse 
12728f19eb88SIan Dowse int
12738f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12748f19eb88SIan Dowse {
1275b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1276fb99ab88SMatthew Dillon 	int oonstack;
1277fb99ab88SMatthew Dillon 
1278b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1279d034d459SMarcel Moolenaar 
12808f19eb88SIan Dowse 	if (oss != NULL) {
1281a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1282a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1283d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1284df8bae1dSRodney W. Grimes 	}
1285d034d459SMarcel Moolenaar 
12868f19eb88SIan Dowse 	if (ss != NULL) {
1287a30ec4b9SDavid Xu 		if (oonstack)
1288cf60731bSJohn Baldwin 			return (EPERM);
1289a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1290cf60731bSJohn Baldwin 			return (EINVAL);
12918f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12928f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1293cf60731bSJohn Baldwin 				return (ENOMEM);
1294fb99ab88SMatthew Dillon 			}
1295a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1296a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1297628d2653SJohn Baldwin 		} else {
1298a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1299628d2653SJohn Baldwin 		}
1300d034d459SMarcel Moolenaar 	}
1301cf60731bSJohn Baldwin 	return (0);
1302df8bae1dSRodney W. Grimes }
1303df8bae1dSRodney W. Grimes 
1304d93f860cSPoul-Henning Kamp /*
1305d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1306d93f860cSPoul-Henning Kamp  * cp is calling process.
1307d93f860cSPoul-Henning Kamp  */
130837c84183SPoul-Henning Kamp static int
13091a88a252SMaxim Sobolev killpg1(td, sig, pgid, all)
13109c1ab3e0SJohn Baldwin 	register struct thread *td;
13111a88a252SMaxim Sobolev 	int sig, pgid, all;
1312d93f860cSPoul-Henning Kamp {
1313d93f860cSPoul-Henning Kamp 	register struct proc *p;
1314d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1315d93f860cSPoul-Henning Kamp 	int nfound = 0;
1316d93f860cSPoul-Henning Kamp 
1317553629ebSJake Burkholder 	if (all) {
1318d93f860cSPoul-Henning Kamp 		/*
1319d93f860cSPoul-Henning Kamp 		 * broadcast
1320d93f860cSPoul-Henning Kamp 		 */
13211005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13222e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1323628d2653SJohn Baldwin 			PROC_LOCK(p);
13249c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13259c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1326628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1327628d2653SJohn Baldwin 				continue;
1328628d2653SJohn Baldwin 			}
13291a88a252SMaxim Sobolev 			if (p_cansignal(td, p, sig) == 0) {
1330d93f860cSPoul-Henning Kamp 				nfound++;
133133a9ed9dSJohn Baldwin 				if (sig)
13322c42a146SMarcel Moolenaar 					psignal(p, sig);
1333628d2653SJohn Baldwin 			}
133433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1335d93f860cSPoul-Henning Kamp 		}
13361005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1337553629ebSJake Burkholder 	} else {
1338ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1339f591779bSSeigo Tanimura 		if (pgid == 0) {
1340d93f860cSPoul-Henning Kamp 			/*
1341d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1342d93f860cSPoul-Henning Kamp 			 */
13439c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1344f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1345f591779bSSeigo Tanimura 		} else {
1346d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1347f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1348ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1349d93f860cSPoul-Henning Kamp 				return (ESRCH);
1350d93f860cSPoul-Henning Kamp 			}
1351f591779bSSeigo Tanimura 		}
1352ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13532e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1354628d2653SJohn Baldwin 			PROC_LOCK(p);
1355628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1356628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1357628d2653SJohn Baldwin 				continue;
1358628d2653SJohn Baldwin 			}
13591a88a252SMaxim Sobolev 			if (p_cansignal(td, p, sig) == 0) {
1360d93f860cSPoul-Henning Kamp 				nfound++;
136133a9ed9dSJohn Baldwin 				if (sig)
13622c42a146SMarcel Moolenaar 					psignal(p, sig);
1363628d2653SJohn Baldwin 			}
136433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1365d93f860cSPoul-Henning Kamp 		}
1366f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1367d93f860cSPoul-Henning Kamp 	}
1368d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1369d93f860cSPoul-Henning Kamp }
1370d93f860cSPoul-Henning Kamp 
1371d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1372df8bae1dSRodney W. Grimes struct kill_args {
1373df8bae1dSRodney W. Grimes 	int	pid;
1374df8bae1dSRodney W. Grimes 	int	signum;
1375df8bae1dSRodney W. Grimes };
1376d2d3e875SBruce Evans #endif
1377fb99ab88SMatthew Dillon /*
1378fb99ab88SMatthew Dillon  * MPSAFE
1379fb99ab88SMatthew Dillon  */
1380df8bae1dSRodney W. Grimes /* ARGSUSED */
138126f9a767SRodney W. Grimes int
1382b40ce416SJulian Elischer kill(td, uap)
1383b40ce416SJulian Elischer 	register struct thread *td;
1384df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1385df8bae1dSRodney W. Grimes {
1386df8bae1dSRodney W. Grimes 	register struct proc *p;
138790af4afaSJohn Baldwin 	int error;
1388df8bae1dSRodney W. Grimes 
13896c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1390df8bae1dSRodney W. Grimes 		return (EINVAL);
1391fb99ab88SMatthew Dillon 
1392df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1393df8bae1dSRodney W. Grimes 		/* kill single process */
13940b011ea3SPawel Jakub Dawidek 		if ((p = pfind(uap->pid)) == NULL) {
139524b2151fSPawel Jakub Dawidek 			if ((p = zpfind(uap->pid)) == NULL)
139690af4afaSJohn Baldwin 				return (ESRCH);
13970b011ea3SPawel Jakub Dawidek 		}
13981a88a252SMaxim Sobolev 		error = p_cansignal(td, p, uap->signum);
139990af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1400df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1401628d2653SJohn Baldwin 		PROC_UNLOCK(p);
140290af4afaSJohn Baldwin 		return (error);
1403df8bae1dSRodney W. Grimes 	}
1404df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1405df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
14061a88a252SMaxim Sobolev 		return (killpg1(td, uap->signum, 0, 1));
1407df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
14081a88a252SMaxim Sobolev 		return (killpg1(td, uap->signum, 0, 0));
1409df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
14101a88a252SMaxim Sobolev 		return (killpg1(td, uap->signum, -uap->pid, 0));
1411df8bae1dSRodney W. Grimes 	}
141290af4afaSJohn Baldwin 	/* NOTREACHED */
1413df8bae1dSRodney W. Grimes }
1414df8bae1dSRodney W. Grimes 
14151930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1416d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1417df8bae1dSRodney W. Grimes struct okillpg_args {
1418df8bae1dSRodney W. Grimes 	int	pgid;
1419df8bae1dSRodney W. Grimes 	int	signum;
1420df8bae1dSRodney W. Grimes };
1421d2d3e875SBruce Evans #endif
1422fb99ab88SMatthew Dillon /*
1423fb99ab88SMatthew Dillon  * MPSAFE
1424fb99ab88SMatthew Dillon  */
1425df8bae1dSRodney W. Grimes /* ARGSUSED */
142626f9a767SRodney W. Grimes int
1427b40ce416SJulian Elischer okillpg(td, uap)
1428b40ce416SJulian Elischer 	struct thread *td;
1429df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1430df8bae1dSRodney W. Grimes {
1431df8bae1dSRodney W. Grimes 
14326c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1433df8bae1dSRodney W. Grimes 		return (EINVAL);
14341a88a252SMaxim Sobolev 	return (killpg1(td, uap->signum, uap->pgid, 0));
1435df8bae1dSRodney W. Grimes }
14361930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1437df8bae1dSRodney W. Grimes 
1438df8bae1dSRodney W. Grimes /*
1439df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1440df8bae1dSRodney W. Grimes  */
1441df8bae1dSRodney W. Grimes void
14422c42a146SMarcel Moolenaar gsignal(pgid, sig)
14432c42a146SMarcel Moolenaar 	int pgid, sig;
1444df8bae1dSRodney W. Grimes {
1445df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1446df8bae1dSRodney W. Grimes 
1447f591779bSSeigo Tanimura 	if (pgid != 0) {
1448ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1449f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1450ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1451f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14522c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1453f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1454f591779bSSeigo Tanimura 		}
1455f591779bSSeigo Tanimura 	}
1456df8bae1dSRodney W. Grimes }
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes /*
1459df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1460df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1461df8bae1dSRodney W. Grimes  */
1462df8bae1dSRodney W. Grimes void
14632c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1464df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14652c42a146SMarcel Moolenaar 	int sig, checkctty;
1466df8bae1dSRodney W. Grimes {
1467df8bae1dSRodney W. Grimes 	register struct proc *p;
1468df8bae1dSRodney W. Grimes 
1469628d2653SJohn Baldwin 	if (pgrp) {
1470f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1471628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1472628d2653SJohn Baldwin 			PROC_LOCK(p);
1473df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14742c42a146SMarcel Moolenaar 				psignal(p, sig);
1475628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1476628d2653SJohn Baldwin 		}
1477628d2653SJohn Baldwin 	}
1478df8bae1dSRodney W. Grimes }
1479df8bae1dSRodney W. Grimes 
1480df8bae1dSRodney W. Grimes /*
14811bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1482df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1483df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1484356861dbSMatthew Dillon  *
1485356861dbSMatthew Dillon  * MPSAFE
1486df8bae1dSRodney W. Grimes  */
1487df8bae1dSRodney W. Grimes void
14881bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1489df8bae1dSRodney W. Grimes {
14901bf4700bSJeff Roberson 	struct sigacts *ps;
14911bf4700bSJeff Roberson 	struct proc *p;
14929dde3bc9SDavid Xu 	siginfo_t siginfo;
14939dde3bc9SDavid Xu 	int error;
14941bf4700bSJeff Roberson 
14951bf4700bSJeff Roberson 	p = td->td_proc;
1496aa0aa7a1STim J. Robbins 	if (td->td_pflags & TDP_SA) {
14977eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
14985995adc2SJulian Elischer 			thread_user_enter(td);
1499628d2653SJohn Baldwin 		PROC_LOCK(p);
15009dde3bc9SDavid Xu 		SIGDELSET(td->td_sigmask, sig);
15019dde3bc9SDavid Xu 		mtx_lock_spin(&sched_lock);
15029dde3bc9SDavid Xu 		/*
15039dde3bc9SDavid Xu 		 * Force scheduling an upcall, so UTS has chance to
15049dde3bc9SDavid Xu 		 * process the signal before thread runs again in
15059dde3bc9SDavid Xu 		 * userland.
15069dde3bc9SDavid Xu 		 */
15079dde3bc9SDavid Xu 		if (td->td_upcall)
15089dde3bc9SDavid Xu 			td->td_upcall->ku_flags |= KUF_DOUPCALL;
15099dde3bc9SDavid Xu 		mtx_unlock_spin(&sched_lock);
1510432b45deSDavid Xu 	} else {
15119dde3bc9SDavid Xu 		PROC_LOCK(p);
15129dde3bc9SDavid Xu 	}
1513ef3dab76STim J. Robbins 	ps = p->p_sigacts;
151490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
151590af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15164093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1517df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1518df8bae1dSRodney W. Grimes #ifdef KTRACE
1519374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1520374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15214093529dSJeff Roberson 			    &td->td_sigmask, code);
1522df8bae1dSRodney W. Grimes #endif
1523aa0aa7a1STim J. Robbins 		if (!(td->td_pflags & TDP_SA))
15249dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15259dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15264093529dSJeff Roberson 				&td->td_sigmask, code);
1527cbf4e354SDavid Xu 		else if (td->td_mailbox == NULL) {
1528cbf4e354SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1529cbf4e354SDavid Xu 			/* UTS caused a sync signal */
1530cbf4e354SDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
1531cbf4e354SDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
1532cbf4e354SDavid Xu 			sigexit(td, sig);
1533cbf4e354SDavid Xu 		} else {
15344b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15359dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1536cbf4e354SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
15379dde3bc9SDavid Xu 			PROC_UNLOCK(p);
15389dde3bc9SDavid Xu 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
15399dde3bc9SDavid Xu 			    sizeof(siginfo));
15409dde3bc9SDavid Xu 			PROC_LOCK(p);
1541432b45deSDavid Xu 			/* UTS memory corrupted */
15429dde3bc9SDavid Xu 			if (error)
1543cbf4e354SDavid Xu 				sigexit(td, SIGSEGV);
15449dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15459dde3bc9SDavid Xu 		}
15464093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15472c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15484093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15492c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1550289ccde0SPeter Wemm 			/*
15518f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1552289ccde0SPeter Wemm 			 */
155390af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15542c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15552c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
155690af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15572c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1558dedc04feSPeter Wemm 		}
155990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15606f841fb7SMarcel Moolenaar 	} else {
156190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15626626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15632c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1564c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1565df8bae1dSRodney W. Grimes 	}
1566628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1567df8bae1dSRodney W. Grimes }
1568df8bae1dSRodney W. Grimes 
15694093529dSJeff Roberson static struct thread *
15704093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15714093529dSJeff Roberson {
15723074d1b4SDavid Xu 	struct thread *td, *signal_td;
15734093529dSJeff Roberson 
15744093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15754093529dSJeff Roberson 
15764093529dSJeff Roberson 	/*
1577627451c1SDavid Xu 	 * Check if current thread can handle the signal without
1578627451c1SDavid Xu 	 * switching conetxt to another thread.
15794093529dSJeff Roberson 	 */
1580627451c1SDavid Xu 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1581627451c1SDavid Xu 		return (curthread);
15823074d1b4SDavid Xu 	signal_td = NULL;
1583a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15843074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15853074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15863074d1b4SDavid Xu 			signal_td = td;
1587627451c1SDavid Xu 			break;
15883074d1b4SDavid Xu 		}
15893074d1b4SDavid Xu 	}
15903074d1b4SDavid Xu 	if (signal_td == NULL)
15913074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1592a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
15933074d1b4SDavid Xu 	return (signal_td);
15944093529dSJeff Roberson }
15954093529dSJeff Roberson 
1596df8bae1dSRodney W. Grimes /*
1597df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1598df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1599df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1600df8bae1dSRodney W. Grimes  *
1601df8bae1dSRodney W. Grimes  * Exceptions:
1602df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1603df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1604df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1605df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1606df8bae1dSRodney W. Grimes  *
1607df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
160890af4afaSJohn Baldwin  *
160990af4afaSJohn Baldwin  * MPSAFE
1610df8bae1dSRodney W. Grimes  */
1611df8bae1dSRodney W. Grimes void
16124093529dSJeff Roberson psignal(struct proc *p, int sig)
1613df8bae1dSRodney W. Grimes {
1614b40ce416SJulian Elischer 	struct thread *td;
16154093529dSJeff Roberson 	int prop;
16164093529dSJeff Roberson 
161741b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
161841b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
161941b3077aSJacques Vidrine 
16204093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
162124b2151fSPawel Jakub Dawidek 	/*
162224b2151fSPawel Jakub Dawidek 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
162324b2151fSPawel Jakub Dawidek 	 */
162424b2151fSPawel Jakub Dawidek 	if (p->p_state == PRS_ZOMBIE)
162524b2151fSPawel Jakub Dawidek 		return;
16264093529dSJeff Roberson 	prop = sigprop(sig);
16274093529dSJeff Roberson 
16284093529dSJeff Roberson 	/*
16294093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16304093529dSJeff Roberson 	 */
16314093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16324093529dSJeff Roberson 
1633c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16344093529dSJeff Roberson }
16354093529dSJeff Roberson 
163690af4afaSJohn Baldwin /*
163790af4afaSJohn Baldwin  * MPSAFE
163890af4afaSJohn Baldwin  */
16394093529dSJeff Roberson void
1640c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16414093529dSJeff Roberson {
16429dde3bc9SDavid Xu 	sigset_t saved;
16439dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16449dde3bc9SDavid Xu 
16459dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16469dde3bc9SDavid Xu 		saved = p->p_siglist;
1647c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16489dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16496dbc0850SJohn Baldwin 		if (!SIGSETEQ(saved, p->p_siglist)) {
16509dde3bc9SDavid Xu 			/* pending set changed */
16519dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
16529dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
16539dde3bc9SDavid Xu 		}
16549dde3bc9SDavid Xu 	}
16559dde3bc9SDavid Xu }
16569dde3bc9SDavid Xu 
16579dde3bc9SDavid Xu static void
1658c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target)
16599dde3bc9SDavid Xu {
16604093529dSJeff Roberson 	struct proc *p;
16614093529dSJeff Roberson 	register sig_t action;
16624093529dSJeff Roberson 	sigset_t *siglist;
16634093529dSJeff Roberson 	struct thread *td0;
1664e602ba25SJulian Elischer 	register int prop;
166590af4afaSJohn Baldwin 	struct sigacts *ps;
1666df8bae1dSRodney W. Grimes 
166741b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
166841b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
16694093529dSJeff Roberson 
16704093529dSJeff Roberson 	p = td->td_proc;
167190af4afaSJohn Baldwin 	ps = p->p_sigacts;
16722c42a146SMarcel Moolenaar 
1673628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1674ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
1675cb679c38SJonathan Lemon 
16762c42a146SMarcel Moolenaar 	prop = sigprop(sig);
16774093529dSJeff Roberson 
16784093529dSJeff Roberson 	/*
167914b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
168014b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
168114b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
16824093529dSJeff Roberson 	 */
16833074d1b4SDavid Xu 	if (target == SIGTARGET_TD) {
16843074d1b4SDavid Xu 		siglist = &td->td_siglist;
16853074d1b4SDavid Xu 	} else {
16863074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig))
16873074d1b4SDavid Xu 			siglist = &td->td_siglist;
16883074d1b4SDavid Xu 		else
16893074d1b4SDavid Xu 			siglist = &p->p_siglist;
16903074d1b4SDavid Xu 	}
16914093529dSJeff Roberson 
1692df8bae1dSRodney W. Grimes 	/*
16932a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
16942a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
16952a024a2bSSean Eric Fagan 	 * a chance, as well.
1696df8bae1dSRodney W. Grimes 	 */
1697b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1698df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1699b40ce416SJulian Elischer 	} else {
1700df8bae1dSRodney W. Grimes 		/*
1701df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1702df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
170390af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1704df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1705df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1706df8bae1dSRodney W. Grimes 		 */
170790af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
170890af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
170990af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
171090af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1711df8bae1dSRodney W. Grimes 			return;
171290af4afaSJohn Baldwin 		}
17136675b36eSDavid Xu 		if (SIGISMEMBER(td->td_sigmask, sig))
1714df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
171590af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1716df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1717df8bae1dSRodney W. Grimes 		else
1718df8bae1dSRodney W. Grimes 			action = SIG_DFL;
171990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1720df8bae1dSRodney W. Grimes 	}
1721df8bae1dSRodney W. Grimes 
17224093529dSJeff Roberson 	if (prop & SA_CONT) {
17231d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17244093529dSJeff Roberson 		/*
17254093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17264093529dSJeff Roberson 		 * the proc's siglist.
17274093529dSJeff Roberson 		 */
1728a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17294093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17304093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1731a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17324093529dSJeff Roberson 	}
1733df8bae1dSRodney W. Grimes 
1734df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1735df8bae1dSRodney W. Grimes 		/*
1736df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1737df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1738df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1739df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1740df8bae1dSRodney W. Grimes 		 */
1741e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1742e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1743e602ba25SJulian Elischer 		    (action == SIG_DFL))
1744df8bae1dSRodney W. Grimes 		        return;
17451d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1746a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17474093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17484093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1749a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17506933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1751df8bae1dSRodney W. Grimes 	}
17523074d1b4SDavid Xu 
17534093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17544093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17555312b1c7SDavid Xu 	/*
17565312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17575312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17585312b1c7SDavid Xu 	 */
17595312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17605312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17615312b1c7SDavid Xu 		return;
1762df8bae1dSRodney W. Grimes 	/*
176390d75f78SAlfred Perlstein 	 * SIGKILL: Remove procfs STOPEVENTs.
176490d75f78SAlfred Perlstein 	 */
176590d75f78SAlfred Perlstein 	if (sig == SIGKILL) {
176690d75f78SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCBIC */
176790d75f78SAlfred Perlstein 		p->p_stops = 0;
176890d75f78SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCCONT */
176990d75f78SAlfred Perlstein 		p->p_step = 0;
177090d75f78SAlfred Perlstein 		wakeup(&p->p_step);
177190d75f78SAlfred Perlstein 	}
177290d75f78SAlfred Perlstein 	/*
1773e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1774e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1775e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1776e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1777e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1778e602ba25SJulian Elischer 	 * We try do the per-process part here.
1779df8bae1dSRodney W. Grimes 	 */
1780e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1781e602ba25SJulian Elischer 		/*
1782e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1783e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1784e602ba25SJulian Elischer 		 */
1785e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1786e602ba25SJulian Elischer 			/*
1787e602ba25SJulian Elischer 			 * The traced process is already stopped,
1788e602ba25SJulian Elischer 			 * so no further action is necessary.
1789e602ba25SJulian Elischer 			 * No signal can restart us.
1790e602ba25SJulian Elischer 			 */
1791e602ba25SJulian Elischer 			goto out;
17921c32c37cSJohn Baldwin 		}
1793b40ce416SJulian Elischer 
1794e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1795df8bae1dSRodney W. Grimes 			/*
1796e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1797e602ba25SJulian Elischer 			 * It will die elsewhere.
1798e602ba25SJulian Elischer 			 * All threads must be restarted.
1799df8bae1dSRodney W. Grimes 			 */
1800482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
1801e602ba25SJulian Elischer 			goto runfast;
1802e602ba25SJulian Elischer 		}
1803e602ba25SJulian Elischer 
1804e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1805e602ba25SJulian Elischer 			/*
1806e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18074093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18081d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1809e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18104093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1811e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1812e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1813e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1814e602ba25SJulian Elischer 			 */
18151279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18166933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1817e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18184093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1819e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1820e602ba25SJulian Elischer 				/*
1821e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1822e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1823e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1824e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1825e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1826e602ba25SJulian Elischer 				 * process, we need to make sure that the
1827e602ba25SJulian Elischer 				 * single thread is runnable asap.
1828e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1829e602ba25SJulian Elischer 				 */
1830e602ba25SJulian Elischer 				goto runfast;
1831e602ba25SJulian Elischer 			}
1832e602ba25SJulian Elischer 			/*
1833e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1834e602ba25SJulian Elischer 			 */
1835e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
183604774f23SJulian Elischer 			thread_unsuspend(p);
1837e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1838e602ba25SJulian Elischer 			goto out;
1839e602ba25SJulian Elischer 		}
1840e602ba25SJulian Elischer 
1841e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1842e602ba25SJulian Elischer 			/*
1843e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1844e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
184504774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1846e602ba25SJulian Elischer 			 */
18471279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18484093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1849e602ba25SJulian Elischer 			goto out;
1850e602ba25SJulian Elischer 		}
1851e602ba25SJulian Elischer 
1852e602ba25SJulian Elischer 		/*
1853e602ba25SJulian Elischer 		 * All other kinds of signals:
1854e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1855e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1856e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
185704774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1858e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1859e602ba25SJulian Elischer 		 */
1860e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
186144f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
186244f3b092SJohn Baldwin 			sleepq_abort(td);
1863e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1864df8bae1dSRodney W. Grimes 		goto out;
1865df8bae1dSRodney W. Grimes 		/*
18669a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
18679a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
1868df8bae1dSRodney W. Grimes 		 */
1869e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
187035c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
187135c32a76SDavid Xu 			!(prop & SA_STOP)) {
1872721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18734093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1874721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1875df8bae1dSRodney W. Grimes 			goto out;
187604774f23SJulian Elischer 		}
187735c32a76SDavid Xu 		if (prop & SA_STOP) {
187835c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
187935c32a76SDavid Xu 				goto out;
1880e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1881e574e444SDavid Xu 			p->p_xstat = sig;
1882cbf4e354SDavid Xu 			p->p_xthread = td;
188335c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
18844093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
18854093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1886062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1887062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
18884093529dSJeff Roberson 					thread_suspend_one(td0);
1889062cf543SDavid Xu 				} else if (td != td0) {
1890062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1891062cf543SDavid Xu 				}
189235c32a76SDavid Xu 			}
1893e574e444SDavid Xu 			thread_stopped(p);
18944093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1895e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
18964093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
18974093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
18984093529dSJeff Roberson 			}
189935c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
190035c32a76SDavid Xu 			goto out;
190135c32a76SDavid Xu 		}
1902721e5910SJulian Elischer 		else
1903df8bae1dSRodney W. Grimes 			goto runfast;
1904df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1905e602ba25SJulian Elischer 	} else {
1906e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19074093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1908e602ba25SJulian Elischer 		goto out;
1909e602ba25SJulian Elischer 	}
1910e602ba25SJulian Elischer 
1911b40ce416SJulian Elischer 	/*
1912e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1913e602ba25SJulian Elischer 	 * running threads.
1914b40ce416SJulian Elischer 	 */
1915e602ba25SJulian Elischer 
1916e602ba25SJulian Elischer runfast:
1917aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19184093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1919e602ba25SJulian Elischer 	thread_unsuspend(p);
1920e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1921e602ba25SJulian Elischer out:
1922e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1923e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1924e602ba25SJulian Elischer }
1925e602ba25SJulian Elischer 
1926e602ba25SJulian Elischer /*
1927e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1928e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
1929e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1930e602ba25SJulian Elischer  */
1931e602ba25SJulian Elischer static void
19324093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1933e602ba25SJulian Elischer {
1934e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1935e602ba25SJulian Elischer 	register int prop;
1936e602ba25SJulian Elischer 
19378b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1938aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1939e602ba25SJulian Elischer 	prop = sigprop(sig);
1940a4c2da15SBruce Evans 
1941e602ba25SJulian Elischer 	/*
1942aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1943e602ba25SJulian Elischer 	 * killed in this lifetime.
1944e602ba25SJulian Elischer 	 */
1945a4c2da15SBruce Evans 	if (action == SIG_DFL && (prop & SA_KILL)) {
19463ef6ac33SJeff Roberson 		if (p->p_nice > 0)
19473ef6ac33SJeff Roberson 			sched_nice(td->td_proc, 0);
1948a4c2da15SBruce Evans 		if (td->td_priority > PUSER)
1949b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
1950b40ce416SJulian Elischer 	}
1951a4c2da15SBruce Evans 
195280c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
1953e602ba25SJulian Elischer 		/*
1954e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1955e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1956e602ba25SJulian Elischer 		 * be noticed when the process returns through
1957e602ba25SJulian Elischer 		 * trap() or syscall().
1958e602ba25SJulian Elischer 		 */
195944f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1960aa0fa334SJulian Elischer 			return;
1961e602ba25SJulian Elischer 		/*
1962e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1963e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1964e602ba25SJulian Elischer 		 * for its parent.
1965e602ba25SJulian Elischer 		 */
1966e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1967e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1968aa0fa334SJulian Elischer 		} else {
1969df8bae1dSRodney W. Grimes 			/*
1970e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1971e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1972e602ba25SJulian Elischer 			 * be awakened.
1973df8bae1dSRodney W. Grimes 			 */
1974e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19751d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19764093529dSJeff Roberson 				/*
19774093529dSJeff Roberson 				 * It may be on either list in this state.
19784093529dSJeff Roberson 				 * Remove from both for now.
19794093529dSJeff Roberson 				 */
19804093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1981aa0fa334SJulian Elischer 				return;
1982df8bae1dSRodney W. Grimes 			}
1983df8bae1dSRodney W. Grimes 
1984aa0fa334SJulian Elischer 			/*
1985a4c2da15SBruce Evans 			 * Give low priority threads a better chance to run.
1986aa0fa334SJulian Elischer 			 */
198744f3b092SJohn Baldwin 			if (td->td_priority > PUSER)
1988b3a4fb14SDavid Xu 				sched_prio(td, PUSER);
1989aa0fa334SJulian Elischer 		}
199044f3b092SJohn Baldwin 		sleepq_abort(td);
1991a4c2da15SBruce Evans 	} else {
1992df8bae1dSRodney W. Grimes 		/*
1993a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
1994df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1995df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1996df8bae1dSRodney W. Grimes 		 */
1997a4c2da15SBruce Evans #ifdef SMP
199844f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
1999e602ba25SJulian Elischer 			forward_signal(td);
20003163861cSTor Egge #endif
20016caa8a15SJohn Baldwin 	}
2002a4c2da15SBruce Evans }
2003df8bae1dSRodney W. Grimes 
2004cbf4e354SDavid Xu int
20054cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
20064cc9f52fSRobert Drehmel {
20074cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
2008cbf4e354SDavid Xu 	struct thread *td0;
20094cc9f52fSRobert Drehmel 
201030a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20114cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
20124cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
20134cc9f52fSRobert Drehmel 
20144cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
2015cbf4e354SDavid Xu 	td->td_flags |= TDF_XSIG;
2016cbf4e354SDavid Xu 	mtx_unlock_spin(&sched_lock);
2017cbf4e354SDavid Xu 	td->td_xsig = sig;
2018cbf4e354SDavid Xu 	while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2019cbf4e354SDavid Xu 		if (p->p_flag & P_SINGLE_EXIT) {
2020cbf4e354SDavid Xu 			mtx_lock_spin(&sched_lock);
2021cbf4e354SDavid Xu 			td->td_flags &= ~TDF_XSIG;
2022cbf4e354SDavid Xu 			mtx_unlock_spin(&sched_lock);
2023cbf4e354SDavid Xu 			return (sig);
2024cbf4e354SDavid Xu 		}
2025cbf4e354SDavid Xu 		/*
2026cbf4e354SDavid Xu 		 * Just make wait() to work, the last stopped thread
2027cbf4e354SDavid Xu 		 * will win.
2028cbf4e354SDavid Xu 		 */
2029cbf4e354SDavid Xu 		p->p_xstat = sig;
2030cbf4e354SDavid Xu 		p->p_xthread = td;
2031cbf4e354SDavid Xu 		p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2032cbf4e354SDavid Xu 		mtx_lock_spin(&sched_lock);
2033cbf4e354SDavid Xu 		FOREACH_THREAD_IN_PROC(p, td0) {
2034cbf4e354SDavid Xu 			if (TD_IS_SLEEPING(td0) &&
2035cbf4e354SDavid Xu 			    (td0->td_flags & TDF_SINTR) &&
2036cbf4e354SDavid Xu 			    !TD_IS_SUSPENDED(td0)) {
2037cbf4e354SDavid Xu 				thread_suspend_one(td0);
2038cbf4e354SDavid Xu 			} else if (td != td0) {
2039cbf4e354SDavid Xu 				td0->td_flags |= TDF_ASTPENDING;
2040cbf4e354SDavid Xu 			}
2041cbf4e354SDavid Xu 		}
2042cbf4e354SDavid Xu stopme:
2043cbf4e354SDavid Xu 		thread_stopped(p);
20444cc9f52fSRobert Drehmel 		thread_suspend_one(td);
20454cc9f52fSRobert Drehmel 		PROC_UNLOCK(p);
20464cc9f52fSRobert Drehmel 		DROP_GIANT();
2047cbf4e354SDavid Xu 		mi_switch(SW_VOL, NULL);
20484cc9f52fSRobert Drehmel 		mtx_unlock_spin(&sched_lock);
20494cc9f52fSRobert Drehmel 		PICKUP_GIANT();
2050cbf4e354SDavid Xu 		PROC_LOCK(p);
2051cbf4e354SDavid Xu 		if (!(p->p_flag & P_TRACED))
2052cbf4e354SDavid Xu 			break;
2053cbf4e354SDavid Xu 		if (td->td_flags & TDF_DBSUSPEND) {
2054cbf4e354SDavid Xu 			if (p->p_flag & P_SINGLE_EXIT)
2055cbf4e354SDavid Xu 				break;
2056cbf4e354SDavid Xu 			mtx_lock_spin(&sched_lock);
2057cbf4e354SDavid Xu 			goto stopme;
2058cbf4e354SDavid Xu 		}
2059cbf4e354SDavid Xu 	}
2060cbf4e354SDavid Xu 	return (td->td_xsig);
20614cc9f52fSRobert Drehmel }
20624cc9f52fSRobert Drehmel 
2063df8bae1dSRodney W. Grimes /*
2064df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2065df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2066df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2067df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2068df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2069628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2070df8bae1dSRodney W. Grimes  * sequence is
2071df8bae1dSRodney W. Grimes  *
2072e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
20732c42a146SMarcel Moolenaar  *		postsig(sig);
2074df8bae1dSRodney W. Grimes  */
20756711f10fSJohn Baldwin static int
2076e602ba25SJulian Elischer issignal(td)
2077e602ba25SJulian Elischer 	struct thread *td;
2078df8bae1dSRodney W. Grimes {
2079e602ba25SJulian Elischer 	struct proc *p;
208090af4afaSJohn Baldwin 	struct sigacts *ps;
20814093529dSJeff Roberson 	sigset_t sigpending;
2082cbf4e354SDavid Xu 	int sig, prop, newsig;
2083062cf543SDavid Xu 	struct thread *td0;
2084df8bae1dSRodney W. Grimes 
2085e602ba25SJulian Elischer 	p = td->td_proc;
208690af4afaSJohn Baldwin 	ps = p->p_sigacts;
208790af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2088628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2089df8bae1dSRodney W. Grimes 	for (;;) {
20902a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
20912a024a2bSSean Eric Fagan 
20924093529dSJeff Roberson 		sigpending = td->td_siglist;
20934093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
20944093529dSJeff Roberson 
2095df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
20964093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
20974093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2098df8bae1dSRodney W. Grimes 			return (0);
20994093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
21002a024a2bSSean Eric Fagan 
2101047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2102047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2103047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2104047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2105047aa39bSRobert Watson 		}
21062a024a2bSSean Eric Fagan 
2107df8bae1dSRodney W. Grimes 		/*
2108df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2109df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2110df8bae1dSRodney W. Grimes 		 */
211190af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
21124093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
2113cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2114cbf4e354SDavid Xu 				SIGADDSET(td->td_sigmask, sig);
2115df8bae1dSRodney W. Grimes 			continue;
2116df8bae1dSRodney W. Grimes 		}
2117df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2118df8bae1dSRodney W. Grimes 			/*
2119d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2120df8bae1dSRodney W. Grimes 			 */
212190af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2122cbf4e354SDavid Xu 			newsig = ptracestop(td, sig);
212390af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2124df8bae1dSRodney W. Grimes 
2125df8bae1dSRodney W. Grimes 			/*
2126df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
2127df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
2128df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
2129df8bae1dSRodney W. Grimes 			 */
21304093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
2131cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2132cbf4e354SDavid Xu 				SIGADDSET(td->td_sigmask, sig);
2133cbf4e354SDavid Xu 			if (newsig == 0)
2134df8bae1dSRodney W. Grimes 				continue;
2135cbf4e354SDavid Xu 			sig = newsig;
2136df8bae1dSRodney W. Grimes 			/*
21378d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
21388d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
21398d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
21408d542cb5SDavid E. O'Brien 			 */
21418d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
21428d542cb5SDavid E. O'Brien 				continue;
21438d542cb5SDavid E. O'Brien 
21448d542cb5SDavid E. O'Brien 			/*
21454093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
21461d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
2147df8bae1dSRodney W. Grimes 			 */
21484093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
2149cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2150cbf4e354SDavid Xu 				SIGDELSET(td->td_sigmask, sig);
21514093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
2152df8bae1dSRodney W. Grimes 				continue;
21534093529dSJeff Roberson 			signotify(td);
2154df8bae1dSRodney W. Grimes 		}
2155df8bae1dSRodney W. Grimes 
21568d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
21578d542cb5SDavid E. O'Brien 
2158df8bae1dSRodney W. Grimes 		/*
2159df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2160df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2161df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2162df8bae1dSRodney W. Grimes 		 */
2163d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2164df8bae1dSRodney W. Grimes 
2165d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2166df8bae1dSRodney W. Grimes 			/*
2167df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2168df8bae1dSRodney W. Grimes 			 */
2169df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2170df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2171df8bae1dSRodney W. Grimes 				/*
2172df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2173df8bae1dSRodney W. Grimes 				 * in init? XXX
2174df8bae1dSRodney W. Grimes 				 */
2175d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
21762c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2177df8bae1dSRodney W. Grimes #endif
2178df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2179df8bae1dSRodney W. Grimes 			}
2180df8bae1dSRodney W. Grimes 			/*
2181df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2182df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2183df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2184df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2185df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2186df8bae1dSRodney W. Grimes 			 */
2187df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2188df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2189df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2190df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2191df8bae1dSRodney W. Grimes 					break;	/* == ignore */
219290af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
219390af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
219490af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2195e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
21962c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2197cbf4e354SDavid Xu 				p->p_xthread = td;
2198721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2199062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2200062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2201062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2202062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2203062cf543SDavid Xu 						thread_suspend_one(td0);
2204062cf543SDavid Xu 					} else if (td != td0) {
2205062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2206062cf543SDavid Xu 					}
2207062cf543SDavid Xu 				}
2208e574e444SDavid Xu 				thread_stopped(p);
220971fad9fdSJulian Elischer 				thread_suspend_one(td);
2210721e5910SJulian Elischer 				PROC_UNLOCK(p);
2211721e5910SJulian Elischer 				DROP_GIANT();
2212bf0acc27SJohn Baldwin 				mi_switch(SW_INVOL, NULL);
2213721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2214721e5910SJulian Elischer 				PICKUP_GIANT();
2215721e5910SJulian Elischer 				PROC_LOCK(p);
221690af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2217df8bae1dSRodney W. Grimes 				break;
221821b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2219df8bae1dSRodney W. Grimes 				/*
2220df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2221df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2222df8bae1dSRodney W. Grimes 				 */
2223df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2224df8bae1dSRodney W. Grimes 			} else
22252c42a146SMarcel Moolenaar 				return (sig);
2226df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2227df8bae1dSRodney W. Grimes 
2228d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2229df8bae1dSRodney W. Grimes 			/*
2230df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2231df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2232df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2233df8bae1dSRodney W. Grimes 			 */
2234df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2235df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2236df8bae1dSRodney W. Grimes 				printf("issignal\n");
2237df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2238df8bae1dSRodney W. Grimes 
2239df8bae1dSRodney W. Grimes 		default:
2240df8bae1dSRodney W. Grimes 			/*
2241df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2242df8bae1dSRodney W. Grimes 			 * postsig() process it.
2243df8bae1dSRodney W. Grimes 			 */
22442c42a146SMarcel Moolenaar 			return (sig);
2245df8bae1dSRodney W. Grimes 		}
22464093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2247df8bae1dSRodney W. Grimes 	}
2248df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2249df8bae1dSRodney W. Grimes }
2250df8bae1dSRodney W. Grimes 
2251df8bae1dSRodney W. Grimes /*
225290af4afaSJohn Baldwin  * MPSAFE
225390af4afaSJohn Baldwin  */
2254e574e444SDavid Xu void
2255e574e444SDavid Xu thread_stopped(struct proc *p)
2256e574e444SDavid Xu {
2257e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
225890af4afaSJohn Baldwin 	struct sigacts *ps;
2259e574e444SDavid Xu 	int n;
2260e574e444SDavid Xu 
2261e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2262e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2263e574e444SDavid Xu 	n = p->p_suspcount;
2264e574e444SDavid Xu 	if (p == p1)
2265e574e444SDavid Xu 		n++;
2266e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2267e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2268407948a5SDavid Xu 		p->p_flag &= ~P_WAITED;
2269e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
2270407948a5SDavid Xu 		/*
2271407948a5SDavid Xu 		 * Wake up parent sleeping in kern_wait(), also send
2272407948a5SDavid Xu 		 * SIGCHLD to parent, but SIGCHLD does not guarantee
2273407948a5SDavid Xu 		 * that parent will awake, because parent may masked
2274407948a5SDavid Xu 		 * the signal.
2275407948a5SDavid Xu 		 */
2276407948a5SDavid Xu 		p->p_pptr->p_flag |= P_STATCHILD;
2277407948a5SDavid Xu 		wakeup(p->p_pptr);
227890af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
227990af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
228090af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
228190af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2282e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
228390af4afaSJohn Baldwin 		} else
228490af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2285e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2286e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2287e574e444SDavid Xu 	}
2288e574e444SDavid Xu }
2289e574e444SDavid Xu 
2290df8bae1dSRodney W. Grimes /*
2291df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2292df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2293df8bae1dSRodney W. Grimes  */
2294df8bae1dSRodney W. Grimes void
22952c42a146SMarcel Moolenaar postsig(sig)
22962c42a146SMarcel Moolenaar 	register int sig;
2297df8bae1dSRodney W. Grimes {
2298b40ce416SJulian Elischer 	struct thread *td = curthread;
2299b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2300628d2653SJohn Baldwin 	struct sigacts *ps;
23012c42a146SMarcel Moolenaar 	sig_t action;
23022c42a146SMarcel Moolenaar 	sigset_t returnmask;
23032c42a146SMarcel Moolenaar 	int code;
2304df8bae1dSRodney W. Grimes 
23052c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
23065526d2d9SEivind Eklund 
23072ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2308628d2653SJohn Baldwin 	ps = p->p_sigacts;
230990af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
23104093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
23112c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2312df8bae1dSRodney W. Grimes #ifdef KTRACE
2313374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
23145e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
23154093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2316df8bae1dSRodney W. Grimes #endif
2317047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2318047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2319047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2320047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2321047aa39bSRobert Watson 	}
23222a024a2bSSean Eric Fagan 
2323cbf4e354SDavid Xu 	if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2324df8bae1dSRodney W. Grimes 		/*
2325df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2326df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2327df8bae1dSRodney W. Grimes 		 */
232890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2329b40ce416SJulian Elischer 		sigexit(td, sig);
2330df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2331df8bae1dSRodney W. Grimes 	} else {
2332cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA) {
2333432b45deSDavid Xu 			if (sig == SIGKILL) {
2334432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2335432b45deSDavid Xu 				sigexit(td, sig);
2336432b45deSDavid Xu 			}
2337432b45deSDavid Xu 		}
2338432b45deSDavid Xu 
2339df8bae1dSRodney W. Grimes 		/*
2340df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2341df8bae1dSRodney W. Grimes 		 */
23424093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23435526d2d9SEivind Eklund 		    ("postsig action"));
2344df8bae1dSRodney W. Grimes 		/*
2345df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2346645682fdSLuoqi Chen 		 * occurrences of this signal.
2347df8bae1dSRodney W. Grimes 		 *
2348645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2349df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2350645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2351df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2352df8bae1dSRodney W. Grimes 		 */
23535e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23544093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23555e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2356df8bae1dSRodney W. Grimes 		} else
23574093529dSJeff Roberson 			returnmask = td->td_sigmask;
23582c42a146SMarcel Moolenaar 
23594093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23602c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23614093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23622c42a146SMarcel Moolenaar 
23632c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2364289ccde0SPeter Wemm 			/*
23658f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2366289ccde0SPeter Wemm 			 */
236790af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23682c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23692c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
237090af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23712c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2372dedc04feSPeter Wemm 		}
2373df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23742c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2375df8bae1dSRodney W. Grimes 			code = 0;
2376df8bae1dSRodney W. Grimes 		} else {
23776626c604SJulian Elischer 			code = p->p_code;
23786626c604SJulian Elischer 			p->p_code = 0;
23796626c604SJulian Elischer 			p->p_sig = 0;
2380df8bae1dSRodney W. Grimes 		}
2381cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA)
238258a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
238358a3c273SJeff Roberson 		else
238458a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
238558a3c273SJeff Roberson 			    &returnmask, code);
2386df8bae1dSRodney W. Grimes 	}
2387df8bae1dSRodney W. Grimes }
2388df8bae1dSRodney W. Grimes 
2389df8bae1dSRodney W. Grimes /*
2390df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2391df8bae1dSRodney W. Grimes  */
239226f9a767SRodney W. Grimes void
2393df8bae1dSRodney W. Grimes killproc(p, why)
2394df8bae1dSRodney W. Grimes 	struct proc *p;
2395df8bae1dSRodney W. Grimes 	char *why;
2396df8bae1dSRodney W. Grimes {
23979081e5e8SJohn Baldwin 
23989081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
23990384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
24000384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2401729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2402b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2403df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2404df8bae1dSRodney W. Grimes }
2405df8bae1dSRodney W. Grimes 
2406df8bae1dSRodney W. Grimes /*
2407df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2408df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2409df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2410df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2411df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2412df8bae1dSRodney W. Grimes  * does not return.
241390af4afaSJohn Baldwin  *
241490af4afaSJohn Baldwin  * MPSAFE
2415df8bae1dSRodney W. Grimes  */
241626f9a767SRodney W. Grimes void
2417b40ce416SJulian Elischer sigexit(td, sig)
2418b40ce416SJulian Elischer 	struct thread *td;
24192c42a146SMarcel Moolenaar 	int sig;
2420df8bae1dSRodney W. Grimes {
2421b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2422df8bae1dSRodney W. Grimes 
2423628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2424df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
2425f97c3df1SDavid Schultz 	/*
2426f97c3df1SDavid Schultz 	 * We must be single-threading to generate a core dump.  This
2427f97c3df1SDavid Schultz 	 * ensures that the registers in the core file are up-to-date.
2428f97c3df1SDavid Schultz 	 * Also, the ELF dump handler assumes that the thread list doesn't
2429f97c3df1SDavid Schultz 	 * change out from under it.
2430f97c3df1SDavid Schultz 	 *
2431f97c3df1SDavid Schultz 	 * XXX If another thread attempts to single-thread before us
2432f97c3df1SDavid Schultz 	 *     (e.g. via fork()), we won't get a dump at all.
2433f97c3df1SDavid Schultz 	 */
2434f97c3df1SDavid Schultz 	if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
24352c42a146SMarcel Moolenaar 		p->p_sig = sig;
2436c364e17eSAndrey A. Chernov 		/*
2437c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2438c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2439c364e17eSAndrey A. Chernov 		 * these messages.)
2440c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
24414ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2442c364e17eSAndrey A. Chernov 		 */
2443b40ce416SJulian Elischer 		if (coredump(td) == 0)
24442c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
244557308494SJoerg Wunsch 		if (kern_logsigexit)
244657308494SJoerg Wunsch 			log(LOG_INFO,
244757308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24483d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24499c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24502c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24512c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
24524ae89b95SJohn Baldwin 	} else
2453628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2454b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2455df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2456df8bae1dSRodney W. Grimes }
2457df8bae1dSRodney W. Grimes 
24586d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"};
2459c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2460c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2461c5edb423SSean Eric Fagan 
2462c5edb423SSean Eric Fagan /*
2463c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2464c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2465c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2466c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2467c5edb423SSean Eric Fagan  *	%P	process id (pid)
2468c5edb423SSean Eric Fagan  *	%U	user id (uid)
2469c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2470c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2471c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2472c5edb423SSean Eric Fagan  */
2473c5edb423SSean Eric Fagan 
2474fca666a1SJulian Elischer static char *
2475c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24768b43b535SAlfred Perlstein 	const char *name;
24778b43b535SAlfred Perlstein 	uid_t uid;
24788b43b535SAlfred Perlstein 	pid_t pid;
24798b43b535SAlfred Perlstein {
24808b43b535SAlfred Perlstein 	const char *format, *appendstr;
2481c5edb423SSean Eric Fagan 	char *temp;
2482c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24838b43b535SAlfred Perlstein 	size_t i, l, n;
2484c5edb423SSean Eric Fagan 
24858b43b535SAlfred Perlstein 	format = corefilename;
24868b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24870bfe2990SEivind Eklund 	if (temp == NULL)
24888b43b535SAlfred Perlstein 		return (NULL);
2489ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2490c5edb423SSean Eric Fagan 		switch (format[i]) {
2491c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2492c5edb423SSean Eric Fagan 			i++;
2493c5edb423SSean Eric Fagan 			switch (format[i]) {
2494c5edb423SSean Eric Fagan 			case '%':
24958b43b535SAlfred Perlstein 				appendstr = "%";
2496c5edb423SSean Eric Fagan 				break;
2497c5edb423SSean Eric Fagan 			case 'N':	/* process name */
24988b43b535SAlfred Perlstein 				appendstr = name;
2499c5edb423SSean Eric Fagan 				break;
2500c5edb423SSean Eric Fagan 			case 'P':	/* process id */
25018b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
25028b43b535SAlfred Perlstein 				appendstr = buf;
2503c5edb423SSean Eric Fagan 				break;
2504c5edb423SSean Eric Fagan 			case 'U':	/* user id */
25058b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
25068b43b535SAlfred Perlstein 				appendstr = buf;
2507c5edb423SSean Eric Fagan 				break;
2508c5edb423SSean Eric Fagan 			default:
25098b43b535SAlfred Perlstein 				appendstr = "";
25108b43b535SAlfred Perlstein 			  	log(LOG_ERR,
25118b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
25128b43b535SAlfred Perlstein 				    format[i], format);
2513c5edb423SSean Eric Fagan 			}
25148b43b535SAlfred Perlstein 			l = strlen(appendstr);
25158b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
25168b43b535SAlfred Perlstein 				goto toolong;
25178b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
25188b43b535SAlfred Perlstein 			n += l;
2519c5edb423SSean Eric Fagan 			break;
2520c5edb423SSean Eric Fagan 		default:
2521c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2522c5edb423SSean Eric Fagan 		}
2523c5edb423SSean Eric Fagan 	}
25248b43b535SAlfred Perlstein 	if (format[i] != '\0')
25258b43b535SAlfred Perlstein 		goto toolong;
25268b43b535SAlfred Perlstein 	return (temp);
25278b43b535SAlfred Perlstein toolong:
25288b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
25298b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
25308b43b535SAlfred Perlstein 	free(temp, M_TEMP);
25318b43b535SAlfred Perlstein 	return (NULL);
2532c5edb423SSean Eric Fagan }
2533c5edb423SSean Eric Fagan 
2534df8bae1dSRodney W. Grimes /*
2535fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2536fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2537fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2538fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2539fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2540fca666a1SJulian Elischer  */
2541fca666a1SJulian Elischer 
2542fca666a1SJulian Elischer static int
2543b40ce416SJulian Elischer coredump(struct thread *td)
2544fca666a1SJulian Elischer {
2545b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2546fca666a1SJulian Elischer 	register struct vnode *vp;
25479c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
254806ae1e91SMatthew Dillon 	struct flock lf;
2549fca666a1SJulian Elischer 	struct nameidata nd;
2550fca666a1SJulian Elischer 	struct vattr vattr;
2551c447f5b2SRobert Watson 	int error, error1, flags, locked;
2552f2a2857bSKirk McKusick 	struct mount *mp;
2553fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2554fca666a1SJulian Elischer 	off_t limit;
2555fca666a1SJulian Elischer 
25564ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2557f97c3df1SDavid Schultz 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
2558628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2559fca666a1SJulian Elischer 
2560628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2561628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2562fca666a1SJulian Elischer 		return (EFAULT);
2563628d2653SJohn Baldwin 	}
2564fca666a1SJulian Elischer 
2565fca666a1SJulian Elischer 	/*
256635a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
256735a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
256835a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
256935a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
257035a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
257135a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2572fca666a1SJulian Elischer 	 */
257391d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2574628d2653SJohn Baldwin 	PROC_UNLOCK(p);
257591d5354aSJohn Baldwin 	if (limit == 0)
257691d5354aSJohn Baldwin 		return (EFBIG);
257735a2598fSSean Eric Fagan 
25784ae89b95SJohn Baldwin 	mtx_lock(&Giant);
2579f2a2857bSKirk McKusick restart:
25809c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
25814ae89b95SJohn Baldwin 	if (name == NULL) {
25824ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2583ccdbd10cSPeter Pentchev 		return (EINVAL);
25844ae89b95SJohn Baldwin 	}
2585b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2586e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25877c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2588fca666a1SJulian Elischer 	free(name, M_TEMP);
25894ae89b95SJohn Baldwin 	if (error) {
25904ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2591fca666a1SJulian Elischer 		return (error);
25924ae89b95SJohn Baldwin 	}
2593762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2594fca666a1SJulian Elischer 	vp = nd.ni_vp;
259506ae1e91SMatthew Dillon 
2596832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2597832dafadSDon Lewis 	if (vp->v_type != VREG ||
2598832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2599832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2600832dafadSDon Lewis 		error = EFAULT;
26014ae89b95SJohn Baldwin 		goto out;
2602832dafadSDon Lewis 	}
2603832dafadSDon Lewis 
2604b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
260506ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
260606ae1e91SMatthew Dillon 	lf.l_start = 0;
260706ae1e91SMatthew Dillon 	lf.l_len = 0;
260806ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2609c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
261006ae1e91SMatthew Dillon 
261106ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
261206ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
261336bbf86bSRobert Watson 		if (locked)
261406ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2615b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2616f2a2857bSKirk McKusick 			return (error);
2617f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2618f2a2857bSKirk McKusick 			return (error);
2619f2a2857bSKirk McKusick 		goto restart;
2620f2a2857bSKirk McKusick 	}
2621fca666a1SJulian Elischer 
2622fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2623fca666a1SJulian Elischer 	vattr.va_size = 0;
26246141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
26256141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
262688b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2627b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2628b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
262988b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2630628d2653SJohn Baldwin 	PROC_LOCK(p);
2631fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2632628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2633fca666a1SJulian Elischer 
2634fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2635b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2636fca666a1SJulian Elischer 	  ENOSYS;
2637fca666a1SJulian Elischer 
2638c447f5b2SRobert Watson 	if (locked) {
263906ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
264006ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2641c447f5b2SRobert Watson 	}
2642f2a2857bSKirk McKusick 	vn_finished_write(mp);
26434ae89b95SJohn Baldwin out:
2644b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
26454ae89b95SJohn Baldwin 	mtx_unlock(&Giant);
2646fca666a1SJulian Elischer 	if (error == 0)
2647fca666a1SJulian Elischer 		error = error1;
2648fca666a1SJulian Elischer 	return (error);
2649fca666a1SJulian Elischer }
2650fca666a1SJulian Elischer 
2651fca666a1SJulian Elischer /*
2652df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2653df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2654df8bae1dSRodney W. Grimes  */
2655d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2656df8bae1dSRodney W. Grimes struct nosys_args {
2657df8bae1dSRodney W. Grimes 	int	dummy;
2658df8bae1dSRodney W. Grimes };
2659d2d3e875SBruce Evans #endif
2660fb99ab88SMatthew Dillon /*
2661fb99ab88SMatthew Dillon  * MPSAFE
2662fb99ab88SMatthew Dillon  */
2663df8bae1dSRodney W. Grimes /* ARGSUSED */
266426f9a767SRodney W. Grimes int
2665b40ce416SJulian Elischer nosys(td, args)
2666b40ce416SJulian Elischer 	struct thread *td;
2667df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2668df8bae1dSRodney W. Grimes {
2669b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2670b40ce416SJulian Elischer 
2671628d2653SJohn Baldwin 	PROC_LOCK(p);
2672df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2673628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2674f5216b9aSBruce Evans 	return (ENOSYS);
2675df8bae1dSRodney W. Grimes }
2676831d27a9SDon Lewis 
2677831d27a9SDon Lewis /*
267848f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2679831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2680831d27a9SDon Lewis  */
2681831d27a9SDon Lewis void
2682f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2683f1320723SAlfred Perlstein 	struct sigio **sigiop;
26842c42a146SMarcel Moolenaar 	int sig, checkctty;
2685831d27a9SDon Lewis {
2686f1320723SAlfred Perlstein 	struct sigio *sigio;
2687831d27a9SDon Lewis 
2688f1320723SAlfred Perlstein 	SIGIO_LOCK();
2689f1320723SAlfred Perlstein 	sigio = *sigiop;
2690f1320723SAlfred Perlstein 	if (sigio == NULL) {
2691f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2692f1320723SAlfred Perlstein 		return;
2693f1320723SAlfred Perlstein 	}
2694831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2695628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
26962b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
26972c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2698628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2699831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2700831d27a9SDon Lewis 		struct proc *p;
2701831d27a9SDon Lewis 
2702f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2703628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2704628d2653SJohn Baldwin 			PROC_LOCK(p);
27052b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2706831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
27072c42a146SMarcel Moolenaar 				psignal(p, sig);
2708628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2709628d2653SJohn Baldwin 		}
2710f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2711831d27a9SDon Lewis 	}
2712f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2713831d27a9SDon Lewis }
2714cb679c38SJonathan Lemon 
2715cb679c38SJonathan Lemon static int
2716cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2717cb679c38SJonathan Lemon {
2718cb679c38SJonathan Lemon 	struct proc *p = curproc;
2719cb679c38SJonathan Lemon 
2720cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2721cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2722cb679c38SJonathan Lemon 
2723ad3b9257SJohn-Mark Gurney 	knlist_add(&p->p_klist, kn, 0);
2724cb679c38SJonathan Lemon 
2725cb679c38SJonathan Lemon 	return (0);
2726cb679c38SJonathan Lemon }
2727cb679c38SJonathan Lemon 
2728cb679c38SJonathan Lemon static void
2729cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2730cb679c38SJonathan Lemon {
2731cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2732cb679c38SJonathan Lemon 
2733ad3b9257SJohn-Mark Gurney 	knlist_remove(&p->p_klist, kn, 0);
2734cb679c38SJonathan Lemon }
2735cb679c38SJonathan Lemon 
2736cb679c38SJonathan Lemon /*
2737cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2738cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2739cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2740cb679c38SJonathan Lemon  * isn't worth the trouble.
2741cb679c38SJonathan Lemon  */
2742cb679c38SJonathan Lemon static int
2743cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2744cb679c38SJonathan Lemon {
2745cb679c38SJonathan Lemon 
2746cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2747cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2748cb679c38SJonathan Lemon 
2749cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2750cb679c38SJonathan Lemon 			kn->kn_data++;
2751cb679c38SJonathan Lemon 	}
2752cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2753cb679c38SJonathan Lemon }
275490af4afaSJohn Baldwin 
275590af4afaSJohn Baldwin struct sigacts *
275690af4afaSJohn Baldwin sigacts_alloc(void)
275790af4afaSJohn Baldwin {
275890af4afaSJohn Baldwin 	struct sigacts *ps;
275990af4afaSJohn Baldwin 
276090af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
276190af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
276290af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
276390af4afaSJohn Baldwin 	return (ps);
276490af4afaSJohn Baldwin }
276590af4afaSJohn Baldwin 
276690af4afaSJohn Baldwin void
276790af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
276890af4afaSJohn Baldwin {
276990af4afaSJohn Baldwin 
277090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
277190af4afaSJohn Baldwin 	ps->ps_refcnt--;
277290af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
277390af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
277490af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
277590af4afaSJohn Baldwin 	} else
277690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
277790af4afaSJohn Baldwin }
277890af4afaSJohn Baldwin 
277990af4afaSJohn Baldwin struct sigacts *
278090af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
278190af4afaSJohn Baldwin {
278290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
278390af4afaSJohn Baldwin 	ps->ps_refcnt++;
278490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
278590af4afaSJohn Baldwin 	return (ps);
278690af4afaSJohn Baldwin }
278790af4afaSJohn Baldwin 
278890af4afaSJohn Baldwin void
278990af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
279090af4afaSJohn Baldwin {
279190af4afaSJohn Baldwin 
279290af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
279390af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
279490af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
279590af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
279690af4afaSJohn Baldwin }
279790af4afaSJohn Baldwin 
279890af4afaSJohn Baldwin int
279990af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
280090af4afaSJohn Baldwin {
280190af4afaSJohn Baldwin 	int shared;
280290af4afaSJohn Baldwin 
280390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
280490af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
280590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
280690af4afaSJohn Baldwin 	return (shared);
280790af4afaSJohn Baldwin }
2808