xref: /freebsd/sys/kern/kern_sig.c (revision cbf4e354ec2e443527cf031282f960cd0fd1a7ad)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
405591b823SEivind Eklund #include "opt_compat.h"
41db6a20e2SGarrett Wollman #include "opt_ktrace.h"
42db6a20e2SGarrett Wollman 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
4436240ea5SDoug Rabson #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
46df8bae1dSRodney W. Grimes #include <sys/vnode.h>
47df8bae1dSRodney W. Grimes #include <sys/acct.h>
48238510fcSJason Evans #include <sys/condvar.h>
49854dc8c2SJohn Baldwin #include <sys/event.h>
50854dc8c2SJohn Baldwin #include <sys/fcntl.h>
51854dc8c2SJohn Baldwin #include <sys/kernel.h>
529dde3bc9SDavid Xu #include <sys/kse.h>
530384fff8SJason Evans #include <sys/ktr.h>
54df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
55854dc8c2SJohn Baldwin #include <sys/lock.h>
56854dc8c2SJohn Baldwin #include <sys/malloc.h>
57854dc8c2SJohn Baldwin #include <sys/mutex.h>
58854dc8c2SJohn Baldwin #include <sys/namei.h>
59854dc8c2SJohn Baldwin #include <sys/proc.h>
60854dc8c2SJohn Baldwin #include <sys/pioctl.h>
61c31146a1SJohn Baldwin #include <sys/resourcevar.h>
6244f3b092SJohn Baldwin #include <sys/sleepqueue.h>
636caa8a15SJohn Baldwin #include <sys/smp.h>
64df8bae1dSRodney W. Grimes #include <sys/stat.h>
651005a129SJohn Baldwin #include <sys/sx.h>
668f19eb88SIan Dowse #include <sys/syscallsubr.h>
67c87e2930SDavid Greenman #include <sys/sysctl.h>
68854dc8c2SJohn Baldwin #include <sys/sysent.h>
69854dc8c2SJohn Baldwin #include <sys/syslog.h>
70854dc8c2SJohn Baldwin #include <sys/sysproto.h>
7106ae1e91SMatthew Dillon #include <sys/unistd.h>
72854dc8c2SJohn Baldwin #include <sys/wait.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <machine/cpu.h>
75df8bae1dSRodney W. Grimes 
7623eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
7723eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
7823eeeff7SPeter Wemm #endif
7923eeeff7SPeter Wemm 
806f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
816f841fb7SMarcel Moolenaar 
824d77a549SAlfred Perlstein static int	coredump(struct thread *);
834d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
849c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
856711f10fSJohn Baldwin static int	issignal(struct thread *p);
864d77a549SAlfred Perlstein static int	sigprop(int sig);
874d77a549SAlfred Perlstein static void	stop(struct proc *);
884093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
89cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
90cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
91cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
924093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
93a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
94a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
95c197abc4SMike Makonnen static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
96cb679c38SJonathan Lemon 
97cb679c38SJonathan Lemon struct filterops sig_filtops =
98cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
99cb679c38SJonathan Lemon 
10057308494SJoerg Wunsch static int	kern_logsigexit = 1;
1013d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1023d177f46SBill Fumerola     &kern_logsigexit, 0,
1033d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10457308494SJoerg Wunsch 
1052b87b6d4SRobert Watson /*
1062b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1072b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1082b87b6d4SRobert Watson  * in the right situations.
1092b87b6d4SRobert Watson  */
1102b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1112b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1122b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1142b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1152b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1162b87b6d4SRobert Watson 
11722d4b0fbSJohn Polstra int sugid_coredump;
1183d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1193d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
120c87e2930SDavid Greenman 
121e5a28db9SPaul Saab static int	do_coredump = 1;
122e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
123e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
124e5a28db9SPaul Saab 
1252c42a146SMarcel Moolenaar /*
1262c42a146SMarcel Moolenaar  * Signal properties and actions.
1272c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1282c42a146SMarcel Moolenaar  * according to the following properties:
1292c42a146SMarcel Moolenaar  */
1302c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1312c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1322c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1332c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1342c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1352c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1362c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
137da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
138df8bae1dSRodney W. Grimes 
1392c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
140da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
141da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
142da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
146da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
148da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1502c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1512c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
152da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
153da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
154da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
155da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
156da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
157da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
158da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
159da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
160da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
161da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
162da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1632c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1642c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
165da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
166da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
167da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
168da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
169da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
170da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1712c42a146SMarcel Moolenaar };
1722c42a146SMarcel Moolenaar 
173fbbeeb6cSBruce Evans /*
174fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
175fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
176fbbeeb6cSBruce Evans  * action, the process stops in issignal().
177e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
178fbbeeb6cSBruce Evans  *
17933510ef1SBruce Evans  * MP SAFE.
180fbbeeb6cSBruce Evans  */
181fbbeeb6cSBruce Evans int
182e602ba25SJulian Elischer cursig(struct thread *td)
183fbbeeb6cSBruce Evans {
184c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
18590af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
186179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1874093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
188fbbeeb6cSBruce Evans }
189fbbeeb6cSBruce Evans 
19079065dbaSBruce Evans /*
19179065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1924093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1934093529dSJeff Roberson  * unmasked in td_sigmask.
19479065dbaSBruce Evans  */
19579065dbaSBruce Evans void
1964093529dSJeff Roberson signotify(struct thread *td)
19779065dbaSBruce Evans {
1984093529dSJeff Roberson 	struct proc *p;
1999dde3bc9SDavid Xu 	sigset_t set, saved;
2004093529dSJeff Roberson 
2014093529dSJeff Roberson 	p = td->td_proc;
20279065dbaSBruce Evans 
20379065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2044093529dSJeff Roberson 
2054093529dSJeff Roberson 	/*
2064093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2074093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2084093529dSJeff Roberson 	 */
2094093529dSJeff Roberson 	set = p->p_siglist;
2109dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
2119dde3bc9SDavid Xu 		saved = p->p_siglist;
2124093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2134093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2144093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2154093529dSJeff Roberson 
2168b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
21779065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2184093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
21979065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
22079065dbaSBruce Evans 	}
2219dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2229dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
2239dde3bc9SDavid Xu 			return;
2249dde3bc9SDavid Xu 		else {
2259dde3bc9SDavid Xu 			/* pending set changed */
2269dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
2279dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
2289dde3bc9SDavid Xu 		}
2299dde3bc9SDavid Xu 	}
2308b94a061SJohn Baldwin }
2318b94a061SJohn Baldwin 
2328b94a061SJohn Baldwin int
2338b94a061SJohn Baldwin sigonstack(size_t sp)
2348b94a061SJohn Baldwin {
235a30ec4b9SDavid Xu 	struct thread *td = curthread;
2368b94a061SJohn Baldwin 
237a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
2381930e303SPoul-Henning Kamp #if defined(COMPAT_43)
239a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
240a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
241a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
2428b94a061SJohn Baldwin #else
243a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
2448b94a061SJohn Baldwin #endif
2458b94a061SJohn Baldwin 	    : 0);
2468b94a061SJohn Baldwin }
24779065dbaSBruce Evans 
2486f841fb7SMarcel Moolenaar static __inline int
2496f841fb7SMarcel Moolenaar sigprop(int sig)
2502c42a146SMarcel Moolenaar {
2516f841fb7SMarcel Moolenaar 
2522c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2532c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2542c42a146SMarcel Moolenaar 	return (0);
255df8bae1dSRodney W. Grimes }
2562c42a146SMarcel Moolenaar 
2574093529dSJeff Roberson int
2586f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2592c42a146SMarcel Moolenaar {
2602c42a146SMarcel Moolenaar 	int i;
2612c42a146SMarcel Moolenaar 
2626f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2632c42a146SMarcel Moolenaar 		if (set->__bits[i])
2642c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
265df8bae1dSRodney W. Grimes 	return (0);
266df8bae1dSRodney W. Grimes }
267df8bae1dSRodney W. Grimes 
2682c42a146SMarcel Moolenaar /*
2698f19eb88SIan Dowse  * kern_sigaction
2702c42a146SMarcel Moolenaar  * sigaction
27123eeeff7SPeter Wemm  * freebsd4_sigaction
2722c42a146SMarcel Moolenaar  * osigaction
27325d6dc06SJohn Baldwin  *
27425d6dc06SJohn Baldwin  * MPSAFE
2752c42a146SMarcel Moolenaar  */
2768f19eb88SIan Dowse int
27723eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2788f19eb88SIan Dowse 	struct thread *td;
2792c42a146SMarcel Moolenaar 	register int sig;
2802c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
28123eeeff7SPeter Wemm 	int flags;
282df8bae1dSRodney W. Grimes {
28390af4afaSJohn Baldwin 	struct sigacts *ps;
2844093529dSJeff Roberson 	struct thread *td0;
2858f19eb88SIan Dowse 	struct proc *p = td->td_proc;
286df8bae1dSRodney W. Grimes 
2872899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2882c42a146SMarcel Moolenaar 		return (EINVAL);
2892c42a146SMarcel Moolenaar 
290628d2653SJohn Baldwin 	PROC_LOCK(p);
291628d2653SJohn Baldwin 	ps = p->p_sigacts;
29290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2932c42a146SMarcel Moolenaar 	if (oact) {
2942c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2952c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2962c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2972c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2982c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2992c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
3002c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
3012c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
3022c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
3032c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
3042c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
3052c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
3062c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
30790af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
3082c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
30990af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
3102c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
3112c42a146SMarcel Moolenaar 	}
3122c42a146SMarcel Moolenaar 	if (act) {
3132c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
314628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
31590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
316628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3172c42a146SMarcel Moolenaar 			return (EINVAL);
318628d2653SJohn Baldwin 		}
3192c42a146SMarcel Moolenaar 
320df8bae1dSRodney W. Grimes 		/*
321df8bae1dSRodney W. Grimes 		 * Change setting atomically.
322df8bae1dSRodney W. Grimes 		 */
3232c42a146SMarcel Moolenaar 
3242c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3252c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3262c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
327aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
328aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
32980f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
33080f42b55SIan Dowse 		} else {
33180f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3322c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3332c42a146SMarcel Moolenaar 		}
3342c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3352c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
336df8bae1dSRodney W. Grimes 		else
3372c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3382c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3392c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
340df8bae1dSRodney W. Grimes 		else
3412c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3422c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3432c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3441e41c1b5SSteven Wallace 		else
3452c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3462c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3472c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
348289ccde0SPeter Wemm 		else
3492c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
3502c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3512c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
35290af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3536626c604SJulian Elischer 			else
35490af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
355ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
356245f17d4SJoerg Wunsch 				/*
3572c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3582c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3592c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3602c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
361245f17d4SJoerg Wunsch 				 */
362245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
36390af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3646626c604SJulian Elischer 				else
36590af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
366245f17d4SJoerg Wunsch 			} else
36790af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
368ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
36990af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
370ba1551caSIan Dowse 			else
37190af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
372df8bae1dSRodney W. Grimes 		}
373df8bae1dSRodney W. Grimes 		/*
37490af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3752c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
37690af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3772c42a146SMarcel Moolenaar 		 * have to restart the process.
378df8bae1dSRodney W. Grimes 		 */
3792c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3802c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3812c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3829dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
3839dde3bc9SDavid Xu 			     SIGISMEMBER(p->p_siglist, sig)) {
3849dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
3859dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
3869dde3bc9SDavid Xu 			}
3872c42a146SMarcel Moolenaar 			/* never to be seen again */
3881d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
389a9a48d68SDavid Xu 			mtx_lock_spin(&sched_lock);
3904093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3914093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
392a9a48d68SDavid Xu 			mtx_unlock_spin(&sched_lock);
3932c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3942c42a146SMarcel Moolenaar 				/* easier in psignal */
39590af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
39690af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
397645682fdSLuoqi Chen 		} else {
39890af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
3992c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
40090af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
4012c42a146SMarcel Moolenaar 			else
40290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
4032c42a146SMarcel Moolenaar 		}
40423eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
40523eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
40623eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
40723eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
40823eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
40923eeeff7SPeter Wemm 		else
41023eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
41123eeeff7SPeter Wemm #endif
412e8ebc08fSPeter Wemm #ifdef COMPAT_43
413645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
41423eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41523eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
416645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
417645682fdSLuoqi Chen 		else
418645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
419e8ebc08fSPeter Wemm #endif
420df8bae1dSRodney W. Grimes 	}
42190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
422628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4232c42a146SMarcel Moolenaar 	return (0);
4242c42a146SMarcel Moolenaar }
4252c42a146SMarcel Moolenaar 
4262c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4272c42a146SMarcel Moolenaar struct sigaction_args {
4282c42a146SMarcel Moolenaar 	int	sig;
4292c42a146SMarcel Moolenaar 	struct	sigaction *act;
4302c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4312c42a146SMarcel Moolenaar };
4322c42a146SMarcel Moolenaar #endif
433fb99ab88SMatthew Dillon /*
434fb99ab88SMatthew Dillon  * MPSAFE
435fb99ab88SMatthew Dillon  */
4362c42a146SMarcel Moolenaar int
437b40ce416SJulian Elischer sigaction(td, uap)
438b40ce416SJulian Elischer 	struct thread *td;
4392c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4402c42a146SMarcel Moolenaar {
4412c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4422c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4432c42a146SMarcel Moolenaar 	int error;
4442c42a146SMarcel Moolenaar 
4456f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4466f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4472c42a146SMarcel Moolenaar 	if (actp) {
4486f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4492c42a146SMarcel Moolenaar 		if (error)
45044443757STim J. Robbins 			return (error);
4512c42a146SMarcel Moolenaar 	}
4528f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
45325d6dc06SJohn Baldwin 	if (oactp && !error)
4546f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4552c42a146SMarcel Moolenaar 	return (error);
4562c42a146SMarcel Moolenaar }
4572c42a146SMarcel Moolenaar 
45823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
45923eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
46023eeeff7SPeter Wemm struct freebsd4_sigaction_args {
46123eeeff7SPeter Wemm 	int	sig;
46223eeeff7SPeter Wemm 	struct	sigaction *act;
46323eeeff7SPeter Wemm 	struct	sigaction *oact;
46423eeeff7SPeter Wemm };
46523eeeff7SPeter Wemm #endif
46623eeeff7SPeter Wemm /*
46723eeeff7SPeter Wemm  * MPSAFE
46823eeeff7SPeter Wemm  */
46923eeeff7SPeter Wemm int
47023eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
47123eeeff7SPeter Wemm 	struct thread *td;
47223eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
47323eeeff7SPeter Wemm {
47423eeeff7SPeter Wemm 	struct sigaction act, oact;
47523eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
47623eeeff7SPeter Wemm 	int error;
47723eeeff7SPeter Wemm 
47823eeeff7SPeter Wemm 
47923eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
48023eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
48123eeeff7SPeter Wemm 	if (actp) {
48223eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
48323eeeff7SPeter Wemm 		if (error)
48444443757STim J. Robbins 			return (error);
48523eeeff7SPeter Wemm 	}
48623eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
487a14e1189SJohn Baldwin 	if (oactp && !error)
48823eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
48923eeeff7SPeter Wemm 	return (error);
49023eeeff7SPeter Wemm }
49123eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
49223eeeff7SPeter Wemm 
49331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4942c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4952c42a146SMarcel Moolenaar struct osigaction_args {
4962c42a146SMarcel Moolenaar 	int	signum;
4972c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4982c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4992c42a146SMarcel Moolenaar };
5002c42a146SMarcel Moolenaar #endif
501fb99ab88SMatthew Dillon /*
502fb99ab88SMatthew Dillon  * MPSAFE
503fb99ab88SMatthew Dillon  */
5042c42a146SMarcel Moolenaar int
505b40ce416SJulian Elischer osigaction(td, uap)
506b40ce416SJulian Elischer 	struct thread *td;
5072c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
5082c42a146SMarcel Moolenaar {
5092c42a146SMarcel Moolenaar 	struct osigaction sa;
5102c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5112c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5122c42a146SMarcel Moolenaar 	int error;
5132c42a146SMarcel Moolenaar 
5146f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5156f841fb7SMarcel Moolenaar 		return (EINVAL);
516fb99ab88SMatthew Dillon 
5176f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5186f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
519fb99ab88SMatthew Dillon 
5202c42a146SMarcel Moolenaar 	if (nsap) {
5216f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5222c42a146SMarcel Moolenaar 		if (error)
52344443757STim J. Robbins 			return (error);
5242c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5252c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5262c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5272c42a146SMarcel Moolenaar 	}
52823eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5292c42a146SMarcel Moolenaar 	if (osap && !error) {
5302c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5312c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5322c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5336f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5342c42a146SMarcel Moolenaar 	}
5352c42a146SMarcel Moolenaar 	return (error);
5362c42a146SMarcel Moolenaar }
53723eeeff7SPeter Wemm 
53823eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
53923eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
54023eeeff7SPeter Wemm int
54123eeeff7SPeter Wemm osigreturn(td, uap)
54223eeeff7SPeter Wemm 	struct thread *td;
54323eeeff7SPeter Wemm 	struct osigreturn_args *uap;
54423eeeff7SPeter Wemm {
54523eeeff7SPeter Wemm 
54623eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
54723eeeff7SPeter Wemm }
54823eeeff7SPeter Wemm #endif
54931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes /*
552df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
553df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
554df8bae1dSRodney W. Grimes  */
555df8bae1dSRodney W. Grimes void
556df8bae1dSRodney W. Grimes siginit(p)
557df8bae1dSRodney W. Grimes 	struct proc *p;
558df8bae1dSRodney W. Grimes {
559df8bae1dSRodney W. Grimes 	register int i;
56090af4afaSJohn Baldwin 	struct sigacts *ps;
561df8bae1dSRodney W. Grimes 
562628d2653SJohn Baldwin 	PROC_LOCK(p);
56390af4afaSJohn Baldwin 	ps = p->p_sigacts;
56490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5652c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5662c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
56790af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
56890af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
569628d2653SJohn Baldwin 	PROC_UNLOCK(p);
570df8bae1dSRodney W. Grimes }
571df8bae1dSRodney W. Grimes 
572df8bae1dSRodney W. Grimes /*
573df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
574df8bae1dSRodney W. Grimes  */
575df8bae1dSRodney W. Grimes void
576a30ec4b9SDavid Xu execsigs(struct proc *p)
577df8bae1dSRodney W. Grimes {
578a30ec4b9SDavid Xu 	struct sigacts *ps;
579a30ec4b9SDavid Xu 	int sig;
580a30ec4b9SDavid Xu 	struct thread *td;
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes 	/*
583df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5844093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
585df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
586df8bae1dSRodney W. Grimes 	 */
5879b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
588a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
589628d2653SJohn Baldwin 	ps = p->p_sigacts;
59090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
59190af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
59290af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
59390af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
5942c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5952c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
59690af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
5971d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
5984093529dSJeff Roberson 			/*
5994093529dSJeff Roberson 			 * There is only one thread at this point.
6004093529dSJeff Roberson 			 */
601a30ec4b9SDavid Xu 			SIGDELSET(td->td_siglist, sig);
602df8bae1dSRodney W. Grimes 		}
6032c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
604df8bae1dSRodney W. Grimes 	}
605df8bae1dSRodney W. Grimes 	/*
606df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
607df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
608df8bae1dSRodney W. Grimes 	 */
609a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
610a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
611a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
612a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
61380e907a1SPeter Wemm 	/*
61480e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
61580e907a1SPeter Wemm 	 */
61690af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
617c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
618c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
61990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
620df8bae1dSRodney W. Grimes }
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes /*
623e77daab1SJohn Baldwin  * kern_sigprocmask()
6247c8fdcbdSMatthew Dillon  *
625628d2653SJohn Baldwin  *	Manipulate signal mask.
626df8bae1dSRodney W. Grimes  */
627e77daab1SJohn Baldwin int
628e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6294093529dSJeff Roberson 	struct thread *td;
6302c42a146SMarcel Moolenaar 	int how;
6312c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
632645682fdSLuoqi Chen 	int old;
6332c42a146SMarcel Moolenaar {
6342c42a146SMarcel Moolenaar 	int error;
6352c42a146SMarcel Moolenaar 
6364093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6372c42a146SMarcel Moolenaar 	if (oset != NULL)
6384093529dSJeff Roberson 		*oset = td->td_sigmask;
6392c42a146SMarcel Moolenaar 
6402c42a146SMarcel Moolenaar 	error = 0;
6412c42a146SMarcel Moolenaar 	if (set != NULL) {
6422c42a146SMarcel Moolenaar 		switch (how) {
6432c42a146SMarcel Moolenaar 		case SIG_BLOCK:
644645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6454093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6462c42a146SMarcel Moolenaar 			break;
6472c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6484093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6494093529dSJeff Roberson 			signotify(td);
6502c42a146SMarcel Moolenaar 			break;
6512c42a146SMarcel Moolenaar 		case SIG_SETMASK:
652645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
653645682fdSLuoqi Chen 			if (old)
6544093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
655645682fdSLuoqi Chen 			else
6564093529dSJeff Roberson 				td->td_sigmask = *set;
6574093529dSJeff Roberson 			signotify(td);
6582c42a146SMarcel Moolenaar 			break;
6592c42a146SMarcel Moolenaar 		default:
6602c42a146SMarcel Moolenaar 			error = EINVAL;
6612c42a146SMarcel Moolenaar 			break;
6622c42a146SMarcel Moolenaar 		}
6632c42a146SMarcel Moolenaar 	}
6644093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6652c42a146SMarcel Moolenaar 	return (error);
6662c42a146SMarcel Moolenaar }
6672c42a146SMarcel Moolenaar 
6687c8fdcbdSMatthew Dillon /*
669e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6707c8fdcbdSMatthew Dillon  */
6717c8fdcbdSMatthew Dillon 
672d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
673df8bae1dSRodney W. Grimes struct sigprocmask_args {
674df8bae1dSRodney W. Grimes 	int	how;
6752c42a146SMarcel Moolenaar 	const sigset_t *set;
6762c42a146SMarcel Moolenaar 	sigset_t *oset;
677df8bae1dSRodney W. Grimes };
678d2d3e875SBruce Evans #endif
67926f9a767SRodney W. Grimes int
680b40ce416SJulian Elischer sigprocmask(td, uap)
681b40ce416SJulian Elischer 	register struct thread *td;
682df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
683df8bae1dSRodney W. Grimes {
6842c42a146SMarcel Moolenaar 	sigset_t set, oset;
6852c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6862c42a146SMarcel Moolenaar 	int error;
687df8bae1dSRodney W. Grimes 
6886f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6896f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6902c42a146SMarcel Moolenaar 	if (setp) {
6916f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6922c42a146SMarcel Moolenaar 		if (error)
6932c42a146SMarcel Moolenaar 			return (error);
694df8bae1dSRodney W. Grimes 	}
695e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
6962c42a146SMarcel Moolenaar 	if (osetp && !error) {
6976f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
6982c42a146SMarcel Moolenaar 	}
6992c42a146SMarcel Moolenaar 	return (error);
7002c42a146SMarcel Moolenaar }
7012c42a146SMarcel Moolenaar 
70231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7037c8fdcbdSMatthew Dillon /*
7047c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
7057c8fdcbdSMatthew Dillon  */
7062c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7072c42a146SMarcel Moolenaar struct osigprocmask_args {
7082c42a146SMarcel Moolenaar 	int	how;
7092c42a146SMarcel Moolenaar 	osigset_t mask;
7102c42a146SMarcel Moolenaar };
7112c42a146SMarcel Moolenaar #endif
7122c42a146SMarcel Moolenaar int
713b40ce416SJulian Elischer osigprocmask(td, uap)
714b40ce416SJulian Elischer 	register struct thread *td;
7152c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7162c42a146SMarcel Moolenaar {
7172c42a146SMarcel Moolenaar 	sigset_t set, oset;
7182c42a146SMarcel Moolenaar 	int error;
7192c42a146SMarcel Moolenaar 
7202c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
721e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
722b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
723df8bae1dSRodney W. Grimes 	return (error);
724df8bae1dSRodney W. Grimes }
72531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
726df8bae1dSRodney W. Grimes 
727d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
728df8bae1dSRodney W. Grimes struct sigpending_args {
7292c42a146SMarcel Moolenaar 	sigset_t	*set;
730df8bae1dSRodney W. Grimes };
731d2d3e875SBruce Evans #endif
732fb99ab88SMatthew Dillon /*
733fb99ab88SMatthew Dillon  * MPSAFE
734fb99ab88SMatthew Dillon  */
73526f9a767SRodney W. Grimes int
736a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
737a447cd8bSJeff Roberson {
738a447cd8bSJeff Roberson 	siginfo_t info;
739a447cd8bSJeff Roberson 	sigset_t set;
740a447cd8bSJeff Roberson 	int error;
741a447cd8bSJeff Roberson 
742a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
74336939a0aSDavid Xu 	if (error) {
74436939a0aSDavid Xu 		td->td_retval[0] = error;
74536939a0aSDavid Xu 		return (0);
74636939a0aSDavid Xu 	}
747a447cd8bSJeff Roberson 
748a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
74936939a0aSDavid Xu 	if (error) {
75036939a0aSDavid Xu 		if (error == ERESTART)
751a447cd8bSJeff Roberson 			return (error);
75236939a0aSDavid Xu 		td->td_retval[0] = error;
75336939a0aSDavid Xu 		return (0);
75436939a0aSDavid Xu 	}
755a447cd8bSJeff Roberson 
756a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
757a447cd8bSJeff Roberson 	/* Repost if we got an error. */
75818440c7fSJohn Baldwin 	if (error && info.si_signo) {
75918440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
760c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
76118440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
76218440c7fSJohn Baldwin 	}
76336939a0aSDavid Xu 	td->td_retval[0] = error;
76436939a0aSDavid Xu 	return (0);
765a447cd8bSJeff Roberson }
766a447cd8bSJeff Roberson /*
767a447cd8bSJeff Roberson  * MPSAFE
768a447cd8bSJeff Roberson  */
769a447cd8bSJeff Roberson int
770a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
771a447cd8bSJeff Roberson {
772a447cd8bSJeff Roberson 	struct timespec ts;
773a447cd8bSJeff Roberson 	struct timespec *timeout;
774a447cd8bSJeff Roberson 	sigset_t set;
775a447cd8bSJeff Roberson 	siginfo_t info;
776a447cd8bSJeff Roberson 	int error;
777a447cd8bSJeff Roberson 
778a447cd8bSJeff Roberson 	if (uap->timeout) {
779a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
780a447cd8bSJeff Roberson 		if (error)
781a447cd8bSJeff Roberson 			return (error);
782a447cd8bSJeff Roberson 
783a447cd8bSJeff Roberson 		timeout = &ts;
784a447cd8bSJeff Roberson 	} else
785a447cd8bSJeff Roberson 		timeout = NULL;
786a447cd8bSJeff Roberson 
787a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
788a447cd8bSJeff Roberson 	if (error)
789a447cd8bSJeff Roberson 		return (error);
790a447cd8bSJeff Roberson 
791a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
792a447cd8bSJeff Roberson 	if (error)
793a447cd8bSJeff Roberson 		return (error);
7949dde3bc9SDavid Xu 
795418228dfSDavid Xu 	if (uap->info)
796a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
797a447cd8bSJeff Roberson 	/* Repost if we got an error. */
79818440c7fSJohn Baldwin 	if (error && info.si_signo) {
79918440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
800c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
80118440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
802418228dfSDavid Xu 	} else {
803418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
80418440c7fSJohn Baldwin 	}
805a447cd8bSJeff Roberson 	return (error);
806a447cd8bSJeff Roberson }
807a447cd8bSJeff Roberson 
808a447cd8bSJeff Roberson /*
809a447cd8bSJeff Roberson  * MPSAFE
810a447cd8bSJeff Roberson  */
811a447cd8bSJeff Roberson int
812a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
813a447cd8bSJeff Roberson {
814a447cd8bSJeff Roberson 	siginfo_t info;
815a447cd8bSJeff Roberson 	sigset_t set;
816a447cd8bSJeff Roberson 	int error;
817a447cd8bSJeff Roberson 
818a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
819a447cd8bSJeff Roberson 	if (error)
820a447cd8bSJeff Roberson 		return (error);
821a447cd8bSJeff Roberson 
822a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
823a447cd8bSJeff Roberson 	if (error)
824a447cd8bSJeff Roberson 		return (error);
825a447cd8bSJeff Roberson 
826418228dfSDavid Xu 	if (uap->info)
827a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
828a447cd8bSJeff Roberson 	/* Repost if we got an error. */
82918440c7fSJohn Baldwin 	if (error && info.si_signo) {
83018440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
831c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
83218440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
833418228dfSDavid Xu 	} else {
834418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
83518440c7fSJohn Baldwin 	}
836a447cd8bSJeff Roberson 	return (error);
837a447cd8bSJeff Roberson }
838a447cd8bSJeff Roberson 
839a447cd8bSJeff Roberson static int
8403074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
841a447cd8bSJeff Roberson     struct timespec *timeout)
842a447cd8bSJeff Roberson {
8433074d1b4SDavid Xu 	struct sigacts *ps;
8443074d1b4SDavid Xu 	sigset_t savedmask, sigset;
845a447cd8bSJeff Roberson 	struct proc *p;
846a447cd8bSJeff Roberson 	int error;
847a447cd8bSJeff Roberson 	int sig;
848a447cd8bSJeff Roberson 	int hz;
8493074d1b4SDavid Xu 	int i;
850a447cd8bSJeff Roberson 
851a447cd8bSJeff Roberson 	p = td->td_proc;
852a447cd8bSJeff Roberson 	error = 0;
853a447cd8bSJeff Roberson 	sig = 0;
8543074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
855a447cd8bSJeff Roberson 
856a447cd8bSJeff Roberson 	PROC_LOCK(p);
857a447cd8bSJeff Roberson 	ps = p->p_sigacts;
8583074d1b4SDavid Xu 	savedmask = td->td_sigmask;
8593074d1b4SDavid Xu 
8603074d1b4SDavid Xu again:
8613074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
8623074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
8633074d1b4SDavid Xu 			continue;
8643074d1b4SDavid Xu 		if (SIGISMEMBER(td->td_siglist, i)) {
865418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
866418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8673074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
86890af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
869a447cd8bSJeff Roberson 			sig = cursig(td);
8703074d1b4SDavid Xu 			i = 0;
8713074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8723074d1b4SDavid Xu 		} else if (SIGISMEMBER(p->p_siglist, i)) {
8733074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
8743074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
8753074d1b4SDavid Xu 				wakeup(&p->p_siglist);
8763074d1b4SDavid Xu 			}
8773074d1b4SDavid Xu 			SIGDELSET(p->p_siglist, i);
8783074d1b4SDavid Xu 			SIGADDSET(td->td_siglist, i);
8793074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
8803074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8813074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
8823074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
8833074d1b4SDavid Xu 			sig = cursig(td);
8843074d1b4SDavid Xu 			i = 0;
8853074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8863074d1b4SDavid Xu 		}
8873074d1b4SDavid Xu 		if (sig) {
8883074d1b4SDavid Xu 			td->td_sigmask = savedmask;
8893074d1b4SDavid Xu 			signotify(td);
890a447cd8bSJeff Roberson 			goto out;
8913074d1b4SDavid Xu 		}
8923074d1b4SDavid Xu 	}
8933074d1b4SDavid Xu 	if (error)
8943074d1b4SDavid Xu 		goto out;
8953074d1b4SDavid Xu 
8963074d1b4SDavid Xu 	td->td_sigmask = savedmask;
8973074d1b4SDavid Xu 	signotify(td);
8983074d1b4SDavid Xu 	sigset = td->td_siglist;
8993074d1b4SDavid Xu 	SIGSETOR(sigset, p->p_siglist);
9003074d1b4SDavid Xu 	SIGSETAND(sigset, waitset);
9013074d1b4SDavid Xu 	if (!SIGISEMPTY(sigset))
9023074d1b4SDavid Xu 		goto again;
903a447cd8bSJeff Roberson 
904a447cd8bSJeff Roberson 	/*
905a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
906a447cd8bSJeff Roberson 	 * signals.
907a447cd8bSJeff Roberson 	 */
908a447cd8bSJeff Roberson 	if (timeout) {
909a447cd8bSJeff Roberson 		struct timeval tv;
910a447cd8bSJeff Roberson 
911a6ca4808SMike Makonnen 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
912a447cd8bSJeff Roberson 			error = EINVAL;
913a447cd8bSJeff Roberson 			goto out;
914a447cd8bSJeff Roberson 		}
9153074d1b4SDavid Xu 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
9163074d1b4SDavid Xu 			error = EAGAIN;
9173074d1b4SDavid Xu 			goto out;
9183074d1b4SDavid Xu 		}
919a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
920a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
921a447cd8bSJeff Roberson 	} else
922a447cd8bSJeff Roberson 		hz = 0;
923a447cd8bSJeff Roberson 
9243074d1b4SDavid Xu 	td->td_waitset = &waitset;
92586b5e563SDag-Erling Smørgrav 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
9263074d1b4SDavid Xu 	td->td_waitset = NULL;
9273074d1b4SDavid Xu 	if (error == 0) /* surplus wakeup ? */
9283074d1b4SDavid Xu 		error = EINTR;
9293074d1b4SDavid Xu 	goto again;
9309dde3bc9SDavid Xu 
931a447cd8bSJeff Roberson out:
932a447cd8bSJeff Roberson 	if (sig) {
933a447cd8bSJeff Roberson 		sig_t action;
934a447cd8bSJeff Roberson 
9353074d1b4SDavid Xu 		error = 0;
9363074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
937a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
93890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
939a447cd8bSJeff Roberson #ifdef KTRACE
940a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
9413074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
942a447cd8bSJeff Roberson #endif
943a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
944a447cd8bSJeff Roberson 
945a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
946a447cd8bSJeff Roberson 		info->si_signo = sig;
947a447cd8bSJeff Roberson 		info->si_code = 0;
9483074d1b4SDavid Xu 	}
949a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
950a447cd8bSJeff Roberson 	return (error);
951a447cd8bSJeff Roberson }
952a447cd8bSJeff Roberson 
953a447cd8bSJeff Roberson /*
954a447cd8bSJeff Roberson  * MPSAFE
955a447cd8bSJeff Roberson  */
956a447cd8bSJeff Roberson int
957b40ce416SJulian Elischer sigpending(td, uap)
958b40ce416SJulian Elischer 	struct thread *td;
959df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
960df8bae1dSRodney W. Grimes {
961b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
962628d2653SJohn Baldwin 	sigset_t siglist;
963df8bae1dSRodney W. Grimes 
964628d2653SJohn Baldwin 	PROC_LOCK(p);
9651d9c5696SJuli Mallett 	siglist = p->p_siglist;
9664093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
967628d2653SJohn Baldwin 	PROC_UNLOCK(p);
96848e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9692c42a146SMarcel Moolenaar }
9702c42a146SMarcel Moolenaar 
97131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9722c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9732c42a146SMarcel Moolenaar struct osigpending_args {
9742c42a146SMarcel Moolenaar 	int	dummy;
9752c42a146SMarcel Moolenaar };
9762c42a146SMarcel Moolenaar #endif
977fb99ab88SMatthew Dillon /*
978fb99ab88SMatthew Dillon  * MPSAFE
979fb99ab88SMatthew Dillon  */
9802c42a146SMarcel Moolenaar int
981b40ce416SJulian Elischer osigpending(td, uap)
982b40ce416SJulian Elischer 	struct thread *td;
9832c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9842c42a146SMarcel Moolenaar {
985b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9864093529dSJeff Roberson 	sigset_t siglist;
987b40ce416SJulian Elischer 
988628d2653SJohn Baldwin 	PROC_LOCK(p);
9894093529dSJeff Roberson 	siglist = p->p_siglist;
9904093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
991628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9929d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
993df8bae1dSRodney W. Grimes 	return (0);
994df8bae1dSRodney W. Grimes }
99531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
996df8bae1dSRodney W. Grimes 
9971930e303SPoul-Henning Kamp #if defined(COMPAT_43)
998df8bae1dSRodney W. Grimes /*
999df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1000df8bae1dSRodney W. Grimes  */
1001d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1002df8bae1dSRodney W. Grimes struct osigvec_args {
1003df8bae1dSRodney W. Grimes 	int	signum;
1004df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1005df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1006df8bae1dSRodney W. Grimes };
1007d2d3e875SBruce Evans #endif
1008fb99ab88SMatthew Dillon /*
1009fb99ab88SMatthew Dillon  * MPSAFE
1010fb99ab88SMatthew Dillon  */
1011df8bae1dSRodney W. Grimes /* ARGSUSED */
101226f9a767SRodney W. Grimes int
1013b40ce416SJulian Elischer osigvec(td, uap)
1014b40ce416SJulian Elischer 	struct thread *td;
1015df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1016df8bae1dSRodney W. Grimes {
1017df8bae1dSRodney W. Grimes 	struct sigvec vec;
10182c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
10192c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
10202c42a146SMarcel Moolenaar 	int error;
1021df8bae1dSRodney W. Grimes 
10226f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10236f841fb7SMarcel Moolenaar 		return (EINVAL);
10246f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
10256f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
10262c42a146SMarcel Moolenaar 	if (nsap) {
10276f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
10282c42a146SMarcel Moolenaar 		if (error)
1029df8bae1dSRodney W. Grimes 			return (error);
10302c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
10312c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
10322c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
10332c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1034df8bae1dSRodney W. Grimes 	}
10355edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10362c42a146SMarcel Moolenaar 	if (osap && !error) {
10372c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
10382c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
10392c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
10402c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
10412c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
10426f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10432c42a146SMarcel Moolenaar 	}
10442c42a146SMarcel Moolenaar 	return (error);
1045df8bae1dSRodney W. Grimes }
1046df8bae1dSRodney W. Grimes 
1047d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1048df8bae1dSRodney W. Grimes struct osigblock_args {
1049df8bae1dSRodney W. Grimes 	int	mask;
1050df8bae1dSRodney W. Grimes };
1051d2d3e875SBruce Evans #endif
1052fb99ab88SMatthew Dillon /*
1053fb99ab88SMatthew Dillon  * MPSAFE
1054fb99ab88SMatthew Dillon  */
105526f9a767SRodney W. Grimes int
1056b40ce416SJulian Elischer osigblock(td, uap)
1057b40ce416SJulian Elischer 	register struct thread *td;
1058df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1059df8bae1dSRodney W. Grimes {
1060b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10612c42a146SMarcel Moolenaar 	sigset_t set;
1062df8bae1dSRodney W. Grimes 
10632c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10642c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1065628d2653SJohn Baldwin 	PROC_LOCK(p);
10664093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10674093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1068628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1069df8bae1dSRodney W. Grimes 	return (0);
1070df8bae1dSRodney W. Grimes }
1071df8bae1dSRodney W. Grimes 
1072d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1073df8bae1dSRodney W. Grimes struct osigsetmask_args {
1074df8bae1dSRodney W. Grimes 	int	mask;
1075df8bae1dSRodney W. Grimes };
1076d2d3e875SBruce Evans #endif
1077fb99ab88SMatthew Dillon /*
1078fb99ab88SMatthew Dillon  * MPSAFE
1079fb99ab88SMatthew Dillon  */
108026f9a767SRodney W. Grimes int
1081b40ce416SJulian Elischer osigsetmask(td, uap)
1082b40ce416SJulian Elischer 	struct thread *td;
1083df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1084df8bae1dSRodney W. Grimes {
1085b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10862c42a146SMarcel Moolenaar 	sigset_t set;
1087df8bae1dSRodney W. Grimes 
10882c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10892c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1090628d2653SJohn Baldwin 	PROC_LOCK(p);
10914093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10924093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10934093529dSJeff Roberson 	signotify(td);
1094628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1095df8bae1dSRodney W. Grimes 	return (0);
1096df8bae1dSRodney W. Grimes }
10971930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes /*
1100df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
110197563428SAlexander Kabaev  * in the meantime.
1102b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1103b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1104b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1105df8bae1dSRodney W. Grimes  */
1106d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1107df8bae1dSRodney W. Grimes struct sigsuspend_args {
11082c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1109df8bae1dSRodney W. Grimes };
1110d2d3e875SBruce Evans #endif
1111fb99ab88SMatthew Dillon /*
1112fb99ab88SMatthew Dillon  * MPSAFE
1113fb99ab88SMatthew Dillon  */
1114df8bae1dSRodney W. Grimes /* ARGSUSED */
111526f9a767SRodney W. Grimes int
1116b40ce416SJulian Elischer sigsuspend(td, uap)
1117b40ce416SJulian Elischer 	struct thread *td;
1118df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1119df8bae1dSRodney W. Grimes {
11202c42a146SMarcel Moolenaar 	sigset_t mask;
11212c42a146SMarcel Moolenaar 	int error;
11222c42a146SMarcel Moolenaar 
11236f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11242c42a146SMarcel Moolenaar 	if (error)
11252c42a146SMarcel Moolenaar 		return (error);
11268f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11278f19eb88SIan Dowse }
11288f19eb88SIan Dowse 
11298f19eb88SIan Dowse int
11308f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11318f19eb88SIan Dowse {
11328f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes 	/*
1135645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1136df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1137df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1138df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1139df8bae1dSRodney W. Grimes 	 * to indicate this.
1140df8bae1dSRodney W. Grimes 	 */
1141628d2653SJohn Baldwin 	PROC_LOCK(p);
11424093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11435e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1144645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11454093529dSJeff Roberson 	td->td_sigmask = mask;
11464093529dSJeff Roberson 	signotify(td);
114786b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11482c42a146SMarcel Moolenaar 		/* void */;
1149628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11502c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11512c42a146SMarcel Moolenaar 	return (EINTR);
11522c42a146SMarcel Moolenaar }
11532c42a146SMarcel Moolenaar 
115431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
115597563428SAlexander Kabaev /*
115697563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
115797563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
115897563428SAlexander Kabaev  */
11592c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11602c42a146SMarcel Moolenaar struct osigsuspend_args {
11612c42a146SMarcel Moolenaar 	osigset_t mask;
11622c42a146SMarcel Moolenaar };
11632c42a146SMarcel Moolenaar #endif
1164fb99ab88SMatthew Dillon /*
1165fb99ab88SMatthew Dillon  * MPSAFE
1166fb99ab88SMatthew Dillon  */
11672c42a146SMarcel Moolenaar /* ARGSUSED */
11682c42a146SMarcel Moolenaar int
1169b40ce416SJulian Elischer osigsuspend(td, uap)
1170b40ce416SJulian Elischer 	struct thread *td;
11712c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11722c42a146SMarcel Moolenaar {
1173b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1174645682fdSLuoqi Chen 	sigset_t mask;
11752c42a146SMarcel Moolenaar 
1176628d2653SJohn Baldwin 	PROC_LOCK(p);
11774093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11785e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1179645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1180645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11814093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11824093529dSJeff Roberson 	signotify(td);
118386b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1184df8bae1dSRodney W. Grimes 		/* void */;
1185628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1186df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1187df8bae1dSRodney W. Grimes 	return (EINTR);
1188df8bae1dSRodney W. Grimes }
118931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1190df8bae1dSRodney W. Grimes 
11911930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1192d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1193df8bae1dSRodney W. Grimes struct osigstack_args {
1194df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1195df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1196df8bae1dSRodney W. Grimes };
1197d2d3e875SBruce Evans #endif
1198fb99ab88SMatthew Dillon /*
1199fb99ab88SMatthew Dillon  * MPSAFE
1200fb99ab88SMatthew Dillon  */
1201df8bae1dSRodney W. Grimes /* ARGSUSED */
120226f9a767SRodney W. Grimes int
1203b40ce416SJulian Elischer osigstack(td, uap)
1204b40ce416SJulian Elischer 	struct thread *td;
1205df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1206df8bae1dSRodney W. Grimes {
12075afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1208fb99ab88SMatthew Dillon 	int error = 0;
1209fb99ab88SMatthew Dillon 
1210d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12115afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12125afe0c99SJohn Baldwin 		if (error)
12135afe0c99SJohn Baldwin 			return (error);
1214df8bae1dSRodney W. Grimes 	}
1215a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12165afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12175afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1218a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1219a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1220a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1221a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12225afe0c99SJohn Baldwin 	}
12235afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12245afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12255afe0c99SJohn Baldwin 
1226fb99ab88SMatthew Dillon 	return (error);
1227df8bae1dSRodney W. Grimes }
12281930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1229df8bae1dSRodney W. Grimes 
1230d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1231df8bae1dSRodney W. Grimes struct sigaltstack_args {
12322c42a146SMarcel Moolenaar 	stack_t	*ss;
12332c42a146SMarcel Moolenaar 	stack_t	*oss;
1234df8bae1dSRodney W. Grimes };
1235d2d3e875SBruce Evans #endif
1236fb99ab88SMatthew Dillon /*
1237fb99ab88SMatthew Dillon  * MPSAFE
1238fb99ab88SMatthew Dillon  */
1239df8bae1dSRodney W. Grimes /* ARGSUSED */
124026f9a767SRodney W. Grimes int
1241b40ce416SJulian Elischer sigaltstack(td, uap)
1242b40ce416SJulian Elischer 	struct thread *td;
1243df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1244df8bae1dSRodney W. Grimes {
12458f19eb88SIan Dowse 	stack_t ss, oss;
12468f19eb88SIan Dowse 	int error;
12478f19eb88SIan Dowse 
12488f19eb88SIan Dowse 	if (uap->ss != NULL) {
12498f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12508f19eb88SIan Dowse 		if (error)
12518f19eb88SIan Dowse 			return (error);
12528f19eb88SIan Dowse 	}
12538f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12548f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12558f19eb88SIan Dowse 	if (error)
12568f19eb88SIan Dowse 		return (error);
12578f19eb88SIan Dowse 	if (uap->oss != NULL)
12588f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12598f19eb88SIan Dowse 	return (error);
12608f19eb88SIan Dowse }
12618f19eb88SIan Dowse 
12628f19eb88SIan Dowse int
12638f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12648f19eb88SIan Dowse {
1265b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1266fb99ab88SMatthew Dillon 	int oonstack;
1267fb99ab88SMatthew Dillon 
1268b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1269d034d459SMarcel Moolenaar 
12708f19eb88SIan Dowse 	if (oss != NULL) {
1271a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1272a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1273d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1274df8bae1dSRodney W. Grimes 	}
1275d034d459SMarcel Moolenaar 
12768f19eb88SIan Dowse 	if (ss != NULL) {
1277a30ec4b9SDavid Xu 		if (oonstack)
1278cf60731bSJohn Baldwin 			return (EPERM);
1279a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1280cf60731bSJohn Baldwin 			return (EINVAL);
12818f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12828f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1283cf60731bSJohn Baldwin 				return (ENOMEM);
1284fb99ab88SMatthew Dillon 			}
1285a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1286a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1287628d2653SJohn Baldwin 		} else {
1288a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1289628d2653SJohn Baldwin 		}
1290d034d459SMarcel Moolenaar 	}
1291cf60731bSJohn Baldwin 	return (0);
1292df8bae1dSRodney W. Grimes }
1293df8bae1dSRodney W. Grimes 
1294d93f860cSPoul-Henning Kamp /*
1295d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1296d93f860cSPoul-Henning Kamp  * cp is calling process.
1297d93f860cSPoul-Henning Kamp  */
129837c84183SPoul-Henning Kamp static int
12999c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
13009c1ab3e0SJohn Baldwin 	register struct thread *td;
13012c42a146SMarcel Moolenaar 	int sig, pgid, all;
1302d93f860cSPoul-Henning Kamp {
1303d93f860cSPoul-Henning Kamp 	register struct proc *p;
1304d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1305d93f860cSPoul-Henning Kamp 	int nfound = 0;
1306d93f860cSPoul-Henning Kamp 
1307553629ebSJake Burkholder 	if (all) {
1308d93f860cSPoul-Henning Kamp 		/*
1309d93f860cSPoul-Henning Kamp 		 * broadcast
1310d93f860cSPoul-Henning Kamp 		 */
13111005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13122e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1313628d2653SJohn Baldwin 			PROC_LOCK(p);
13149c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13159c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1316628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1317628d2653SJohn Baldwin 				continue;
1318628d2653SJohn Baldwin 			}
1319f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1320d93f860cSPoul-Henning Kamp 				nfound++;
132133a9ed9dSJohn Baldwin 				if (sig)
13222c42a146SMarcel Moolenaar 					psignal(p, sig);
1323628d2653SJohn Baldwin 			}
132433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1325d93f860cSPoul-Henning Kamp 		}
13261005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1327553629ebSJake Burkholder 	} else {
1328ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1329f591779bSSeigo Tanimura 		if (pgid == 0) {
1330d93f860cSPoul-Henning Kamp 			/*
1331d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1332d93f860cSPoul-Henning Kamp 			 */
13339c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1334f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1335f591779bSSeigo Tanimura 		} else {
1336d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1337f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1338ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1339d93f860cSPoul-Henning Kamp 				return (ESRCH);
1340d93f860cSPoul-Henning Kamp 			}
1341f591779bSSeigo Tanimura 		}
1342ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13432e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1344628d2653SJohn Baldwin 			PROC_LOCK(p);
1345628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1346628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1347628d2653SJohn Baldwin 				continue;
1348628d2653SJohn Baldwin 			}
1349e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
135033a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1351628d2653SJohn Baldwin 				continue;
1352628d2653SJohn Baldwin 			}
1353f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1354d93f860cSPoul-Henning Kamp 				nfound++;
135533a9ed9dSJohn Baldwin 				if (sig)
13562c42a146SMarcel Moolenaar 					psignal(p, sig);
1357628d2653SJohn Baldwin 			}
135833a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1359d93f860cSPoul-Henning Kamp 		}
1360f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1361d93f860cSPoul-Henning Kamp 	}
1362d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1363d93f860cSPoul-Henning Kamp }
1364d93f860cSPoul-Henning Kamp 
1365d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1366df8bae1dSRodney W. Grimes struct kill_args {
1367df8bae1dSRodney W. Grimes 	int	pid;
1368df8bae1dSRodney W. Grimes 	int	signum;
1369df8bae1dSRodney W. Grimes };
1370d2d3e875SBruce Evans #endif
1371fb99ab88SMatthew Dillon /*
1372fb99ab88SMatthew Dillon  * MPSAFE
1373fb99ab88SMatthew Dillon  */
1374df8bae1dSRodney W. Grimes /* ARGSUSED */
137526f9a767SRodney W. Grimes int
1376b40ce416SJulian Elischer kill(td, uap)
1377b40ce416SJulian Elischer 	register struct thread *td;
1378df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1379df8bae1dSRodney W. Grimes {
1380df8bae1dSRodney W. Grimes 	register struct proc *p;
138190af4afaSJohn Baldwin 	int error;
1382df8bae1dSRodney W. Grimes 
13836c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1384df8bae1dSRodney W. Grimes 		return (EINVAL);
1385fb99ab88SMatthew Dillon 
1386df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1387df8bae1dSRodney W. Grimes 		/* kill single process */
138890af4afaSJohn Baldwin 		if ((p = pfind(uap->pid)) == NULL)
138990af4afaSJohn Baldwin 			return (ESRCH);
139090af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
139190af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1392df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1393628d2653SJohn Baldwin 		PROC_UNLOCK(p);
139490af4afaSJohn Baldwin 		return (error);
1395df8bae1dSRodney W. Grimes 	}
1396df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1397df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
139890af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1399df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
140090af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1401df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
140290af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1403df8bae1dSRodney W. Grimes 	}
140490af4afaSJohn Baldwin 	/* NOTREACHED */
1405df8bae1dSRodney W. Grimes }
1406df8bae1dSRodney W. Grimes 
14071930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1408d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1409df8bae1dSRodney W. Grimes struct okillpg_args {
1410df8bae1dSRodney W. Grimes 	int	pgid;
1411df8bae1dSRodney W. Grimes 	int	signum;
1412df8bae1dSRodney W. Grimes };
1413d2d3e875SBruce Evans #endif
1414fb99ab88SMatthew Dillon /*
1415fb99ab88SMatthew Dillon  * MPSAFE
1416fb99ab88SMatthew Dillon  */
1417df8bae1dSRodney W. Grimes /* ARGSUSED */
141826f9a767SRodney W. Grimes int
1419b40ce416SJulian Elischer okillpg(td, uap)
1420b40ce416SJulian Elischer 	struct thread *td;
1421df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1422df8bae1dSRodney W. Grimes {
1423df8bae1dSRodney W. Grimes 
14246c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1425df8bae1dSRodney W. Grimes 		return (EINVAL);
142690af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1427df8bae1dSRodney W. Grimes }
14281930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1429df8bae1dSRodney W. Grimes 
1430df8bae1dSRodney W. Grimes /*
1431df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1432df8bae1dSRodney W. Grimes  */
1433df8bae1dSRodney W. Grimes void
14342c42a146SMarcel Moolenaar gsignal(pgid, sig)
14352c42a146SMarcel Moolenaar 	int pgid, sig;
1436df8bae1dSRodney W. Grimes {
1437df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1438df8bae1dSRodney W. Grimes 
1439f591779bSSeigo Tanimura 	if (pgid != 0) {
1440ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1441f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1442ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1443f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14442c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1445f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1446f591779bSSeigo Tanimura 		}
1447f591779bSSeigo Tanimura 	}
1448df8bae1dSRodney W. Grimes }
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes /*
1451df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1452df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1453df8bae1dSRodney W. Grimes  */
1454df8bae1dSRodney W. Grimes void
14552c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1456df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14572c42a146SMarcel Moolenaar 	int sig, checkctty;
1458df8bae1dSRodney W. Grimes {
1459df8bae1dSRodney W. Grimes 	register struct proc *p;
1460df8bae1dSRodney W. Grimes 
1461628d2653SJohn Baldwin 	if (pgrp) {
1462f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1463628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1464628d2653SJohn Baldwin 			PROC_LOCK(p);
1465df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14662c42a146SMarcel Moolenaar 				psignal(p, sig);
1467628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1468628d2653SJohn Baldwin 		}
1469628d2653SJohn Baldwin 	}
1470df8bae1dSRodney W. Grimes }
1471df8bae1dSRodney W. Grimes 
1472df8bae1dSRodney W. Grimes /*
14731bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1474df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1475df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1476356861dbSMatthew Dillon  *
1477356861dbSMatthew Dillon  * MPSAFE
1478df8bae1dSRodney W. Grimes  */
1479df8bae1dSRodney W. Grimes void
14801bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1481df8bae1dSRodney W. Grimes {
14821bf4700bSJeff Roberson 	struct sigacts *ps;
14831bf4700bSJeff Roberson 	struct proc *p;
14849dde3bc9SDavid Xu 	siginfo_t siginfo;
14859dde3bc9SDavid Xu 	int error;
14861bf4700bSJeff Roberson 
14871bf4700bSJeff Roberson 	p = td->td_proc;
1488aa0aa7a1STim J. Robbins 	if (td->td_pflags & TDP_SA) {
14897eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
14909dde3bc9SDavid Xu 			thread_user_enter(p, td);
1491628d2653SJohn Baldwin 		PROC_LOCK(p);
14929dde3bc9SDavid Xu 		SIGDELSET(td->td_sigmask, sig);
14939dde3bc9SDavid Xu 		mtx_lock_spin(&sched_lock);
14949dde3bc9SDavid Xu 		/*
14959dde3bc9SDavid Xu 		 * Force scheduling an upcall, so UTS has chance to
14969dde3bc9SDavid Xu 		 * process the signal before thread runs again in
14979dde3bc9SDavid Xu 		 * userland.
14989dde3bc9SDavid Xu 		 */
14999dde3bc9SDavid Xu 		if (td->td_upcall)
15009dde3bc9SDavid Xu 			td->td_upcall->ku_flags |= KUF_DOUPCALL;
15019dde3bc9SDavid Xu 		mtx_unlock_spin(&sched_lock);
1502432b45deSDavid Xu 	} else {
15039dde3bc9SDavid Xu 		PROC_LOCK(p);
15049dde3bc9SDavid Xu 	}
1505ef3dab76STim J. Robbins 	ps = p->p_sigacts;
150690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
150790af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15084093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1509df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1510df8bae1dSRodney W. Grimes #ifdef KTRACE
1511374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1512374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15134093529dSJeff Roberson 			    &td->td_sigmask, code);
1514df8bae1dSRodney W. Grimes #endif
1515aa0aa7a1STim J. Robbins 		if (!(td->td_pflags & TDP_SA))
15169dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15179dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15184093529dSJeff Roberson 				&td->td_sigmask, code);
1519cbf4e354SDavid Xu 		else if (td->td_mailbox == NULL) {
1520cbf4e354SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1521cbf4e354SDavid Xu 			/* UTS caused a sync signal */
1522cbf4e354SDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
1523cbf4e354SDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
1524cbf4e354SDavid Xu 			sigexit(td, sig);
1525cbf4e354SDavid Xu 		} else {
15264b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15279dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1528cbf4e354SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
15299dde3bc9SDavid Xu 			PROC_UNLOCK(p);
15309dde3bc9SDavid Xu 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
15319dde3bc9SDavid Xu 			    sizeof(siginfo));
15329dde3bc9SDavid Xu 			PROC_LOCK(p);
1533432b45deSDavid Xu 			/* UTS memory corrupted */
15349dde3bc9SDavid Xu 			if (error)
1535cbf4e354SDavid Xu 				sigexit(td, SIGSEGV);
15369dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15379dde3bc9SDavid Xu 		}
15384093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15392c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15404093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15412c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1542289ccde0SPeter Wemm 			/*
15438f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1544289ccde0SPeter Wemm 			 */
154590af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15462c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15472c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
154890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15492c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1550dedc04feSPeter Wemm 		}
155190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15526f841fb7SMarcel Moolenaar 	} else {
155390af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15546626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15552c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1556c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1557df8bae1dSRodney W. Grimes 	}
1558628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1559df8bae1dSRodney W. Grimes }
1560df8bae1dSRodney W. Grimes 
15614093529dSJeff Roberson static struct thread *
15624093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15634093529dSJeff Roberson {
15643074d1b4SDavid Xu 	struct thread *td, *signal_td;
15654093529dSJeff Roberson 
15664093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15674093529dSJeff Roberson 
15684093529dSJeff Roberson 	/*
15693074d1b4SDavid Xu 	 * First find a thread in sigwait state and signal belongs to
15703074d1b4SDavid Xu 	 * its wait set. POSIX's arguments is that speed of delivering signal
15713074d1b4SDavid Xu 	 * to sigwait thread is faster than delivering signal to user stack.
15723074d1b4SDavid Xu 	 * If we can not find sigwait thread, then find the first thread in
15733074d1b4SDavid Xu 	 * the proc that doesn't have this signal masked, an exception is
15743074d1b4SDavid Xu 	 * if current thread is sending signal to its process, and it does not
15753074d1b4SDavid Xu 	 * mask the signal, it should get the signal, this is another fast
15763074d1b4SDavid Xu 	 * way to deliver signal.
15774093529dSJeff Roberson 	 */
15783074d1b4SDavid Xu 	signal_td = NULL;
1579a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15803074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15813074d1b4SDavid Xu 		if (td->td_waitset != NULL &&
1582a9a48d68SDavid Xu 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1583a9a48d68SDavid Xu 				mtx_unlock_spin(&sched_lock);
15844093529dSJeff Roberson 				return (td);
1585a9a48d68SDavid Xu 		}
15863074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15873074d1b4SDavid Xu 			if (td == curthread)
15883074d1b4SDavid Xu 				signal_td = curthread;
15893074d1b4SDavid Xu 			else if (signal_td == NULL)
15903074d1b4SDavid Xu 				signal_td = td;
15913074d1b4SDavid Xu 		}
15923074d1b4SDavid Xu 	}
15933074d1b4SDavid Xu 	if (signal_td == NULL)
15943074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1595a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
15963074d1b4SDavid Xu 	return (signal_td);
15974093529dSJeff Roberson }
15984093529dSJeff Roberson 
1599df8bae1dSRodney W. Grimes /*
1600df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1601df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1602df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1603df8bae1dSRodney W. Grimes  *
1604df8bae1dSRodney W. Grimes  * Exceptions:
1605df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1606df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1607df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1608df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1609df8bae1dSRodney W. Grimes  *
1610df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
161190af4afaSJohn Baldwin  *
161290af4afaSJohn Baldwin  * MPSAFE
1613df8bae1dSRodney W. Grimes  */
1614df8bae1dSRodney W. Grimes void
16154093529dSJeff Roberson psignal(struct proc *p, int sig)
1616df8bae1dSRodney W. Grimes {
1617b40ce416SJulian Elischer 	struct thread *td;
16184093529dSJeff Roberson 	int prop;
16194093529dSJeff Roberson 
162041b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
162141b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
162241b3077aSJacques Vidrine 
16234093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
16244093529dSJeff Roberson 	prop = sigprop(sig);
16254093529dSJeff Roberson 
16264093529dSJeff Roberson 	/*
16274093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16284093529dSJeff Roberson 	 */
16294093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16304093529dSJeff Roberson 
1631c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16324093529dSJeff Roberson }
16334093529dSJeff Roberson 
163490af4afaSJohn Baldwin /*
163590af4afaSJohn Baldwin  * MPSAFE
163690af4afaSJohn Baldwin  */
16374093529dSJeff Roberson void
1638c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16394093529dSJeff Roberson {
16409dde3bc9SDavid Xu 	sigset_t saved;
16419dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16429dde3bc9SDavid Xu 
16439dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16449dde3bc9SDavid Xu 		saved = p->p_siglist;
1645c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16469dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16479dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
16489dde3bc9SDavid Xu 			return;
16499dde3bc9SDavid Xu 		else {
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);
1674cb679c38SJonathan Lemon 	KNOTE(&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 if (td->td_waitset != NULL &&
16893074d1b4SDavid Xu 			SIGISMEMBER(*(td->td_waitset), sig))
16903074d1b4SDavid Xu 			siglist = &td->td_siglist;
16913074d1b4SDavid Xu 		else
16923074d1b4SDavid Xu 			siglist = &p->p_siglist;
16933074d1b4SDavid Xu 	}
16944093529dSJeff Roberson 
1695df8bae1dSRodney W. Grimes 	/*
16962a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
16972a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
16982a024a2bSSean Eric Fagan 	 * a chance, as well.
1699df8bae1dSRodney W. Grimes 	 */
1700b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1701df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1702b40ce416SJulian Elischer 	} else {
1703df8bae1dSRodney W. Grimes 		/*
1704df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1705df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
170690af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1707df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1708df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1709df8bae1dSRodney W. Grimes 		 */
171090af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
171190af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
171290af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
171390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1714df8bae1dSRodney W. Grimes 			return;
171590af4afaSJohn Baldwin 		}
17163074d1b4SDavid Xu 		if (((td->td_waitset == NULL) &&
17173074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig)) ||
17183074d1b4SDavid Xu 		    ((td->td_waitset != NULL) &&
17193074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig) &&
17203074d1b4SDavid Xu 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1721df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
172290af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1723df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1724df8bae1dSRodney W. Grimes 		else
1725df8bae1dSRodney W. Grimes 			action = SIG_DFL;
172690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1727df8bae1dSRodney W. Grimes 	}
1728df8bae1dSRodney W. Grimes 
17294093529dSJeff Roberson 	if (prop & SA_CONT) {
17301d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17314093529dSJeff Roberson 		/*
17324093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17334093529dSJeff Roberson 		 * the proc's siglist.
17344093529dSJeff Roberson 		 */
1735a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17364093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17374093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1738a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17394093529dSJeff Roberson 	}
1740df8bae1dSRodney W. Grimes 
1741df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1742df8bae1dSRodney W. Grimes 		/*
1743df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1744df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1745df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1746df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1747df8bae1dSRodney W. Grimes 		 */
1748e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1749e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1750e602ba25SJulian Elischer 		    (action == SIG_DFL))
1751df8bae1dSRodney W. Grimes 		        return;
17521d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1753a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17544093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17554093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1756a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17576933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1758df8bae1dSRodney W. Grimes 	}
17593074d1b4SDavid Xu 
17604093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17614093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17623074d1b4SDavid Xu 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
17633074d1b4SDavid Xu 	    action != SIG_HOLD) {
17643074d1b4SDavid Xu 		td->td_waitset = NULL;
17653074d1b4SDavid Xu 	}
17663074d1b4SDavid Xu 
17675312b1c7SDavid Xu 	/*
17685312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17695312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17705312b1c7SDavid Xu 	 */
17715312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17725312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17735312b1c7SDavid Xu 		return;
1774df8bae1dSRodney W. Grimes 	/*
1775e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1776e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1777e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1778e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1779e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1780e602ba25SJulian Elischer 	 * We try do the per-process part here.
1781df8bae1dSRodney W. Grimes 	 */
1782e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1783e602ba25SJulian Elischer 		/*
1784e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1785e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1786e602ba25SJulian Elischer 		 */
1787e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1788e602ba25SJulian Elischer 			/*
1789e602ba25SJulian Elischer 			 * The traced process is already stopped,
1790e602ba25SJulian Elischer 			 * so no further action is necessary.
1791e602ba25SJulian Elischer 			 * No signal can restart us.
1792e602ba25SJulian Elischer 			 */
1793e602ba25SJulian Elischer 			goto out;
17941c32c37cSJohn Baldwin 		}
1795b40ce416SJulian Elischer 
1796e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1797df8bae1dSRodney W. Grimes 			/*
1798e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1799e602ba25SJulian Elischer 			 * It will die elsewhere.
1800e602ba25SJulian Elischer 			 * All threads must be restarted.
1801df8bae1dSRodney W. Grimes 			 */
1802e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1803e602ba25SJulian Elischer 			goto runfast;
1804e602ba25SJulian Elischer 		}
1805e602ba25SJulian Elischer 
1806e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1807e602ba25SJulian Elischer 			/*
1808e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18094093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18101d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1811e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18124093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1813e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1814e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1815e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1816e602ba25SJulian Elischer 			 */
18171279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18186933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1819e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18204093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1821e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1822e602ba25SJulian Elischer 				/*
1823e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1824e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1825e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1826e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1827e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1828e602ba25SJulian Elischer 				 * process, we need to make sure that the
1829e602ba25SJulian Elischer 				 * single thread is runnable asap.
1830e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1831e602ba25SJulian Elischer 				 */
1832e602ba25SJulian Elischer 				goto runfast;
1833e602ba25SJulian Elischer 			}
1834e602ba25SJulian Elischer 			/*
1835e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1836e602ba25SJulian Elischer 			 */
1837e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
183804774f23SJulian Elischer 			thread_unsuspend(p);
1839e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1840e602ba25SJulian Elischer 			goto out;
1841e602ba25SJulian Elischer 		}
1842e602ba25SJulian Elischer 
1843e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1844e602ba25SJulian Elischer 			/*
1845e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1846e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
184704774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1848e602ba25SJulian Elischer 			 */
18491279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18504093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1851e602ba25SJulian Elischer 			goto out;
1852e602ba25SJulian Elischer 		}
1853e602ba25SJulian Elischer 
1854e602ba25SJulian Elischer 		/*
1855e602ba25SJulian Elischer 		 * All other kinds of signals:
1856e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1857e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1858e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
185904774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1860e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1861e602ba25SJulian Elischer 		 */
1862e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
186344f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
186444f3b092SJohn Baldwin 			sleepq_abort(td);
1865e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1866df8bae1dSRodney W. Grimes 		goto out;
1867df8bae1dSRodney W. Grimes 		/*
18689a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
18699a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
1870df8bae1dSRodney W. Grimes 		 */
1871e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
187235c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
187335c32a76SDavid Xu 			!(prop & SA_STOP)) {
1874721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18754093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1876721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1877df8bae1dSRodney W. Grimes 			goto out;
187804774f23SJulian Elischer 		}
187935c32a76SDavid Xu 		if (prop & SA_STOP) {
188035c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
188135c32a76SDavid Xu 				goto out;
1882e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1883e574e444SDavid Xu 			p->p_xstat = sig;
1884cbf4e354SDavid Xu 			p->p_xthread = td;
188535c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
18864093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
18874093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1888062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1889062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
18904093529dSJeff Roberson 					thread_suspend_one(td0);
1891062cf543SDavid Xu 				} else if (td != td0) {
1892062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1893062cf543SDavid Xu 				}
189435c32a76SDavid Xu 			}
1895e574e444SDavid Xu 			thread_stopped(p);
18964093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1897e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
18984093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
18994093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
19004093529dSJeff Roberson 			}
190135c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
190235c32a76SDavid Xu 			goto out;
190335c32a76SDavid Xu 		}
1904721e5910SJulian Elischer 		else
1905df8bae1dSRodney W. Grimes 			goto runfast;
1906df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1907e602ba25SJulian Elischer 	} else {
1908e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19094093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1910e602ba25SJulian Elischer 		goto out;
1911e602ba25SJulian Elischer 	}
1912e602ba25SJulian Elischer 
1913b40ce416SJulian Elischer 	/*
1914e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1915e602ba25SJulian Elischer 	 * running threads.
1916b40ce416SJulian Elischer 	 */
1917e602ba25SJulian Elischer 
1918e602ba25SJulian Elischer runfast:
1919aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19204093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1921e602ba25SJulian Elischer 	thread_unsuspend(p);
1922e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1923e602ba25SJulian Elischer out:
1924e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1925e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1926e602ba25SJulian Elischer }
1927e602ba25SJulian Elischer 
1928e602ba25SJulian Elischer /*
1929e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1930e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
1931e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1932e602ba25SJulian Elischer  */
1933e602ba25SJulian Elischer static void
19344093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1935e602ba25SJulian Elischer {
1936e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1937e602ba25SJulian Elischer 	register int prop;
1938e602ba25SJulian Elischer 
19398b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1940aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1941e602ba25SJulian Elischer 	prop = sigprop(sig);
1942a4c2da15SBruce Evans 
1943e602ba25SJulian Elischer 	/*
1944aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1945e602ba25SJulian Elischer 	 * killed in this lifetime.
1946e602ba25SJulian Elischer 	 */
1947a4c2da15SBruce Evans 	if (action == SIG_DFL && (prop & SA_KILL)) {
1948a4c2da15SBruce Evans 		if (td->td_priority > PUSER)
1949e602ba25SJulian Elischer 			td->td_priority = PUSER;
1950b40ce416SJulian Elischer 	}
1951a4c2da15SBruce Evans 
195280c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
1953e602ba25SJulian Elischer 		/*
1954e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1955e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1956e602ba25SJulian Elischer 		 * be noticed when the process returns through
1957e602ba25SJulian Elischer 		 * trap() or syscall().
1958e602ba25SJulian Elischer 		 */
195944f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1960aa0fa334SJulian Elischer 			return;
1961e602ba25SJulian Elischer 		/*
1962e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1963e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1964e602ba25SJulian Elischer 		 * for its parent.
1965e602ba25SJulian Elischer 		 */
1966e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1967e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1968aa0fa334SJulian Elischer 		} else {
1969df8bae1dSRodney W. Grimes 			/*
1970e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1971e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1972e602ba25SJulian Elischer 			 * be awakened.
1973df8bae1dSRodney W. Grimes 			 */
1974e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19751d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19764093529dSJeff Roberson 				/*
19774093529dSJeff Roberson 				 * It may be on either list in this state.
19784093529dSJeff Roberson 				 * Remove from both for now.
19794093529dSJeff Roberson 				 */
19804093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1981aa0fa334SJulian Elischer 				return;
1982df8bae1dSRodney W. Grimes 			}
1983df8bae1dSRodney W. Grimes 
1984aa0fa334SJulian Elischer 			/*
1985a4c2da15SBruce Evans 			 * Give low priority threads a better chance to run.
1986aa0fa334SJulian Elischer 			 */
198744f3b092SJohn Baldwin 			if (td->td_priority > PUSER)
1988aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1989aa0fa334SJulian Elischer 		}
199044f3b092SJohn Baldwin 		sleepq_abort(td);
1991a4c2da15SBruce Evans 	} else {
1992df8bae1dSRodney W. Grimes 		/*
1993a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
1994df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1995df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1996df8bae1dSRodney W. Grimes 		 */
1997a4c2da15SBruce Evans #ifdef SMP
199844f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
1999e602ba25SJulian Elischer 			forward_signal(td);
20003163861cSTor Egge #endif
20016caa8a15SJohn Baldwin 	}
2002a4c2da15SBruce Evans }
2003df8bae1dSRodney W. Grimes 
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 /*
2252df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2253df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
22544a3b3dcbSColin Percival  * on the run queue.  Must be called with the proc p locked.
2255df8bae1dSRodney W. Grimes  */
22565b3047d5SJohn Baldwin static void
2257e574e444SDavid Xu stop(struct proc *p)
2258df8bae1dSRodney W. Grimes {
2259df8bae1dSRodney W. Grimes 
2260628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22611279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2262df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
226301609114SAlfred Perlstein 	wakeup(p->p_pptr);
2264df8bae1dSRodney W. Grimes }
2265df8bae1dSRodney W. Grimes 
226690af4afaSJohn Baldwin /*
226790af4afaSJohn Baldwin  * MPSAFE
226890af4afaSJohn Baldwin  */
2269e574e444SDavid Xu void
2270e574e444SDavid Xu thread_stopped(struct proc *p)
2271e574e444SDavid Xu {
2272e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
227390af4afaSJohn Baldwin 	struct sigacts *ps;
2274e574e444SDavid Xu 	int n;
2275e574e444SDavid Xu 
2276e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2277e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2278e574e444SDavid Xu 	n = p->p_suspcount;
2279e574e444SDavid Xu 	if (p == p1)
2280e574e444SDavid Xu 		n++;
2281e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2282e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2283e574e444SDavid Xu 		stop(p);
2284e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
228590af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
228690af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
228790af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
228890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2289e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
229090af4afaSJohn Baldwin 		} else
229190af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2292e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2293e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2294e574e444SDavid Xu 	}
2295e574e444SDavid Xu }
2296e574e444SDavid Xu 
2297df8bae1dSRodney W. Grimes /*
2298df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2299df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2300df8bae1dSRodney W. Grimes  */
2301df8bae1dSRodney W. Grimes void
23022c42a146SMarcel Moolenaar postsig(sig)
23032c42a146SMarcel Moolenaar 	register int sig;
2304df8bae1dSRodney W. Grimes {
2305b40ce416SJulian Elischer 	struct thread *td = curthread;
2306b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2307628d2653SJohn Baldwin 	struct sigacts *ps;
23082c42a146SMarcel Moolenaar 	sig_t action;
23092c42a146SMarcel Moolenaar 	sigset_t returnmask;
23102c42a146SMarcel Moolenaar 	int code;
2311df8bae1dSRodney W. Grimes 
23122c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
23135526d2d9SEivind Eklund 
23142ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2315628d2653SJohn Baldwin 	ps = p->p_sigacts;
231690af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
23174093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
23182c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2319df8bae1dSRodney W. Grimes #ifdef KTRACE
2320374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
23215e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
23224093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2323df8bae1dSRodney W. Grimes #endif
2324047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2325047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2326047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2327047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2328047aa39bSRobert Watson 	}
23292a024a2bSSean Eric Fagan 
2330cbf4e354SDavid Xu 	if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2331df8bae1dSRodney W. Grimes 		/*
2332df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2333df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2334df8bae1dSRodney W. Grimes 		 */
233590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2336b40ce416SJulian Elischer 		sigexit(td, sig);
2337df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2338df8bae1dSRodney W. Grimes 	} else {
2339cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA) {
2340432b45deSDavid Xu 			if (sig == SIGKILL) {
2341432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2342432b45deSDavid Xu 				sigexit(td, sig);
2343432b45deSDavid Xu 			}
2344432b45deSDavid Xu 		}
2345432b45deSDavid Xu 
2346df8bae1dSRodney W. Grimes 		/*
2347df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2348df8bae1dSRodney W. Grimes 		 */
23494093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23505526d2d9SEivind Eklund 		    ("postsig action"));
2351df8bae1dSRodney W. Grimes 		/*
2352df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2353645682fdSLuoqi Chen 		 * occurrences of this signal.
2354df8bae1dSRodney W. Grimes 		 *
2355645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2356df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2357645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2358df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2359df8bae1dSRodney W. Grimes 		 */
23605e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23614093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23625e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2363df8bae1dSRodney W. Grimes 		} else
23644093529dSJeff Roberson 			returnmask = td->td_sigmask;
23652c42a146SMarcel Moolenaar 
23664093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23672c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23684093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23692c42a146SMarcel Moolenaar 
23702c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2371289ccde0SPeter Wemm 			/*
23728f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2373289ccde0SPeter Wemm 			 */
237490af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23752c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23762c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
237790af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23782c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2379dedc04feSPeter Wemm 		}
2380df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23812c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2382df8bae1dSRodney W. Grimes 			code = 0;
2383df8bae1dSRodney W. Grimes 		} else {
23846626c604SJulian Elischer 			code = p->p_code;
23856626c604SJulian Elischer 			p->p_code = 0;
23866626c604SJulian Elischer 			p->p_sig = 0;
2387df8bae1dSRodney W. Grimes 		}
2388cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA)
238958a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
239058a3c273SJeff Roberson 		else
239158a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
239258a3c273SJeff Roberson 			    &returnmask, code);
2393df8bae1dSRodney W. Grimes 	}
2394df8bae1dSRodney W. Grimes }
2395df8bae1dSRodney W. Grimes 
2396df8bae1dSRodney W. Grimes /*
2397df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2398df8bae1dSRodney W. Grimes  */
239926f9a767SRodney W. Grimes void
2400df8bae1dSRodney W. Grimes killproc(p, why)
2401df8bae1dSRodney W. Grimes 	struct proc *p;
2402df8bae1dSRodney W. Grimes 	char *why;
2403df8bae1dSRodney W. Grimes {
24049081e5e8SJohn Baldwin 
24059081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
24060384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
24070384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2408729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2409b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2410df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2411df8bae1dSRodney W. Grimes }
2412df8bae1dSRodney W. Grimes 
2413df8bae1dSRodney W. Grimes /*
2414df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2415df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2416df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2417df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2418df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2419df8bae1dSRodney W. Grimes  * does not return.
242090af4afaSJohn Baldwin  *
242190af4afaSJohn Baldwin  * MPSAFE
2422df8bae1dSRodney W. Grimes  */
242326f9a767SRodney W. Grimes void
2424b40ce416SJulian Elischer sigexit(td, sig)
2425b40ce416SJulian Elischer 	struct thread *td;
24262c42a146SMarcel Moolenaar 	int sig;
2427df8bae1dSRodney W. Grimes {
2428b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2429df8bae1dSRodney W. Grimes 
2430628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2431df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
24322c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
24332c42a146SMarcel Moolenaar 		p->p_sig = sig;
2434c364e17eSAndrey A. Chernov 		/*
2435c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2436c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2437c364e17eSAndrey A. Chernov 		 * these messages.)
2438c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
24394ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2440c364e17eSAndrey A. Chernov 		 */
2441b40ce416SJulian Elischer 		if (coredump(td) == 0)
24422c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
244357308494SJoerg Wunsch 		if (kern_logsigexit)
244457308494SJoerg Wunsch 			log(LOG_INFO,
244557308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24463d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24479c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24482c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24492c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
24504ae89b95SJohn Baldwin 	} else
2451628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2452b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2453df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2454df8bae1dSRodney W. Grimes }
2455df8bae1dSRodney W. Grimes 
2456c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2457c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2458c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2459c5edb423SSean Eric Fagan 
2460c5edb423SSean Eric Fagan /*
2461c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2462c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2463c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2464c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2465c5edb423SSean Eric Fagan  *	%P	process id (pid)
2466c5edb423SSean Eric Fagan  *	%U	user id (uid)
2467c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2468c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2469c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2470c5edb423SSean Eric Fagan  */
2471c5edb423SSean Eric Fagan 
2472fca666a1SJulian Elischer static char *
2473c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24748b43b535SAlfred Perlstein 	const char *name;
24758b43b535SAlfred Perlstein 	uid_t uid;
24768b43b535SAlfred Perlstein 	pid_t pid;
24778b43b535SAlfred Perlstein {
24788b43b535SAlfred Perlstein 	const char *format, *appendstr;
2479c5edb423SSean Eric Fagan 	char *temp;
2480c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24818b43b535SAlfred Perlstein 	size_t i, l, n;
2482c5edb423SSean Eric Fagan 
24838b43b535SAlfred Perlstein 	format = corefilename;
24848b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24850bfe2990SEivind Eklund 	if (temp == NULL)
24868b43b535SAlfred Perlstein 		return (NULL);
2487ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2488c5edb423SSean Eric Fagan 		switch (format[i]) {
2489c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2490c5edb423SSean Eric Fagan 			i++;
2491c5edb423SSean Eric Fagan 			switch (format[i]) {
2492c5edb423SSean Eric Fagan 			case '%':
24938b43b535SAlfred Perlstein 				appendstr = "%";
2494c5edb423SSean Eric Fagan 				break;
2495c5edb423SSean Eric Fagan 			case 'N':	/* process name */
24968b43b535SAlfred Perlstein 				appendstr = name;
2497c5edb423SSean Eric Fagan 				break;
2498c5edb423SSean Eric Fagan 			case 'P':	/* process id */
24998b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
25008b43b535SAlfred Perlstein 				appendstr = buf;
2501c5edb423SSean Eric Fagan 				break;
2502c5edb423SSean Eric Fagan 			case 'U':	/* user id */
25038b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
25048b43b535SAlfred Perlstein 				appendstr = buf;
2505c5edb423SSean Eric Fagan 				break;
2506c5edb423SSean Eric Fagan 			default:
25078b43b535SAlfred Perlstein 				appendstr = "";
25088b43b535SAlfred Perlstein 			  	log(LOG_ERR,
25098b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
25108b43b535SAlfred Perlstein 				    format[i], format);
2511c5edb423SSean Eric Fagan 			}
25128b43b535SAlfred Perlstein 			l = strlen(appendstr);
25138b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
25148b43b535SAlfred Perlstein 				goto toolong;
25158b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
25168b43b535SAlfred Perlstein 			n += l;
2517c5edb423SSean Eric Fagan 			break;
2518c5edb423SSean Eric Fagan 		default:
2519c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2520c5edb423SSean Eric Fagan 		}
2521c5edb423SSean Eric Fagan 	}
25228b43b535SAlfred Perlstein 	if (format[i] != '\0')
25238b43b535SAlfred Perlstein 		goto toolong;
25248b43b535SAlfred Perlstein 	return (temp);
25258b43b535SAlfred Perlstein toolong:
25268b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
25278b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
25288b43b535SAlfred Perlstein 	free(temp, M_TEMP);
25298b43b535SAlfred Perlstein 	return (NULL);
2530c5edb423SSean Eric Fagan }
2531c5edb423SSean Eric Fagan 
2532df8bae1dSRodney W. Grimes /*
2533fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2534fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2535fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2536fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2537fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2538fca666a1SJulian Elischer  */
2539fca666a1SJulian Elischer 
2540fca666a1SJulian Elischer static int
2541b40ce416SJulian Elischer coredump(struct thread *td)
2542fca666a1SJulian Elischer {
2543b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2544fca666a1SJulian Elischer 	register struct vnode *vp;
25459c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
254606ae1e91SMatthew Dillon 	struct flock lf;
2547fca666a1SJulian Elischer 	struct nameidata nd;
2548fca666a1SJulian Elischer 	struct vattr vattr;
2549c447f5b2SRobert Watson 	int error, error1, flags, locked;
2550f2a2857bSKirk McKusick 	struct mount *mp;
2551fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2552fca666a1SJulian Elischer 	off_t limit;
2553fca666a1SJulian Elischer 
25544ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2555628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2556fca666a1SJulian Elischer 
2557628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2558628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2559fca666a1SJulian Elischer 		return (EFAULT);
2560628d2653SJohn Baldwin 	}
2561fca666a1SJulian Elischer 
2562fca666a1SJulian Elischer 	/*
256335a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
256435a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
256535a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
256635a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
256735a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
256835a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2569fca666a1SJulian Elischer 	 */
257091d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2571628d2653SJohn Baldwin 	PROC_UNLOCK(p);
257291d5354aSJohn Baldwin 	if (limit == 0)
257391d5354aSJohn Baldwin 		return (EFBIG);
257435a2598fSSean Eric Fagan 
25754ae89b95SJohn Baldwin 	mtx_lock(&Giant);
2576f2a2857bSKirk McKusick restart:
25779c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
25784ae89b95SJohn Baldwin 	if (name == NULL) {
25794ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2580ccdbd10cSPeter Pentchev 		return (EINVAL);
25814ae89b95SJohn Baldwin 	}
2582b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2583e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25847c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2585fca666a1SJulian Elischer 	free(name, M_TEMP);
25864ae89b95SJohn Baldwin 	if (error) {
25874ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2588fca666a1SJulian Elischer 		return (error);
25894ae89b95SJohn Baldwin 	}
2590762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2591fca666a1SJulian Elischer 	vp = nd.ni_vp;
259206ae1e91SMatthew Dillon 
2593832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2594832dafadSDon Lewis 	if (vp->v_type != VREG ||
2595832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2596832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2597832dafadSDon Lewis 		error = EFAULT;
25984ae89b95SJohn Baldwin 		goto out;
2599832dafadSDon Lewis 	}
2600832dafadSDon Lewis 
2601b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
260206ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
260306ae1e91SMatthew Dillon 	lf.l_start = 0;
260406ae1e91SMatthew Dillon 	lf.l_len = 0;
260506ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2606c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
260706ae1e91SMatthew Dillon 
260806ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
260906ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
261036bbf86bSRobert Watson 		if (locked)
261106ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2612b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2613f2a2857bSKirk McKusick 			return (error);
2614f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2615f2a2857bSKirk McKusick 			return (error);
2616f2a2857bSKirk McKusick 		goto restart;
2617f2a2857bSKirk McKusick 	}
2618fca666a1SJulian Elischer 
2619fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2620fca666a1SJulian Elischer 	vattr.va_size = 0;
262188b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2622b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2623b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
262488b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2625628d2653SJohn Baldwin 	PROC_LOCK(p);
2626fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2627628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2628fca666a1SJulian Elischer 
2629fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2630b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2631fca666a1SJulian Elischer 	  ENOSYS;
2632fca666a1SJulian Elischer 
2633c447f5b2SRobert Watson 	if (locked) {
263406ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
263506ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2636c447f5b2SRobert Watson 	}
2637f2a2857bSKirk McKusick 	vn_finished_write(mp);
26384ae89b95SJohn Baldwin out:
2639b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
26404ae89b95SJohn Baldwin 	mtx_unlock(&Giant);
2641fca666a1SJulian Elischer 	if (error == 0)
2642fca666a1SJulian Elischer 		error = error1;
2643fca666a1SJulian Elischer 	return (error);
2644fca666a1SJulian Elischer }
2645fca666a1SJulian Elischer 
2646fca666a1SJulian Elischer /*
2647df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2648df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2649df8bae1dSRodney W. Grimes  */
2650d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2651df8bae1dSRodney W. Grimes struct nosys_args {
2652df8bae1dSRodney W. Grimes 	int	dummy;
2653df8bae1dSRodney W. Grimes };
2654d2d3e875SBruce Evans #endif
2655fb99ab88SMatthew Dillon /*
2656fb99ab88SMatthew Dillon  * MPSAFE
2657fb99ab88SMatthew Dillon  */
2658df8bae1dSRodney W. Grimes /* ARGSUSED */
265926f9a767SRodney W. Grimes int
2660b40ce416SJulian Elischer nosys(td, args)
2661b40ce416SJulian Elischer 	struct thread *td;
2662df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2663df8bae1dSRodney W. Grimes {
2664b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2665b40ce416SJulian Elischer 
2666628d2653SJohn Baldwin 	PROC_LOCK(p);
2667df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2668628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2669f5216b9aSBruce Evans 	return (ENOSYS);
2670df8bae1dSRodney W. Grimes }
2671831d27a9SDon Lewis 
2672831d27a9SDon Lewis /*
267348f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2674831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2675831d27a9SDon Lewis  */
2676831d27a9SDon Lewis void
2677f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2678f1320723SAlfred Perlstein 	struct sigio **sigiop;
26792c42a146SMarcel Moolenaar 	int sig, checkctty;
2680831d27a9SDon Lewis {
2681f1320723SAlfred Perlstein 	struct sigio *sigio;
2682831d27a9SDon Lewis 
2683f1320723SAlfred Perlstein 	SIGIO_LOCK();
2684f1320723SAlfred Perlstein 	sigio = *sigiop;
2685f1320723SAlfred Perlstein 	if (sigio == NULL) {
2686f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2687f1320723SAlfred Perlstein 		return;
2688f1320723SAlfred Perlstein 	}
2689831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2690628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
26912b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
26922c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2693628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2694831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2695831d27a9SDon Lewis 		struct proc *p;
2696831d27a9SDon Lewis 
2697f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2698628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2699628d2653SJohn Baldwin 			PROC_LOCK(p);
27002b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2701831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
27022c42a146SMarcel Moolenaar 				psignal(p, sig);
2703628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2704628d2653SJohn Baldwin 		}
2705f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2706831d27a9SDon Lewis 	}
2707f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2708831d27a9SDon Lewis }
2709cb679c38SJonathan Lemon 
2710cb679c38SJonathan Lemon static int
2711cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2712cb679c38SJonathan Lemon {
2713cb679c38SJonathan Lemon 	struct proc *p = curproc;
2714cb679c38SJonathan Lemon 
2715cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2716cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2717cb679c38SJonathan Lemon 
2718628d2653SJohn Baldwin 	PROC_LOCK(p);
2719cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2720628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2721cb679c38SJonathan Lemon 
2722cb679c38SJonathan Lemon 	return (0);
2723cb679c38SJonathan Lemon }
2724cb679c38SJonathan Lemon 
2725cb679c38SJonathan Lemon static void
2726cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2727cb679c38SJonathan Lemon {
2728cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2729cb679c38SJonathan Lemon 
2730628d2653SJohn Baldwin 	PROC_LOCK(p);
2731e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2732628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2733cb679c38SJonathan Lemon }
2734cb679c38SJonathan Lemon 
2735cb679c38SJonathan Lemon /*
2736cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2737cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2738cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2739cb679c38SJonathan Lemon  * isn't worth the trouble.
2740cb679c38SJonathan Lemon  */
2741cb679c38SJonathan Lemon static int
2742cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2743cb679c38SJonathan Lemon {
2744cb679c38SJonathan Lemon 
2745cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2746cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2747cb679c38SJonathan Lemon 
2748cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2749cb679c38SJonathan Lemon 			kn->kn_data++;
2750cb679c38SJonathan Lemon 	}
2751cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2752cb679c38SJonathan Lemon }
275390af4afaSJohn Baldwin 
275490af4afaSJohn Baldwin struct sigacts *
275590af4afaSJohn Baldwin sigacts_alloc(void)
275690af4afaSJohn Baldwin {
275790af4afaSJohn Baldwin 	struct sigacts *ps;
275890af4afaSJohn Baldwin 
275990af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
276090af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
276190af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
276290af4afaSJohn Baldwin 	return (ps);
276390af4afaSJohn Baldwin }
276490af4afaSJohn Baldwin 
276590af4afaSJohn Baldwin void
276690af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
276790af4afaSJohn Baldwin {
276890af4afaSJohn Baldwin 
276990af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
277090af4afaSJohn Baldwin 	ps->ps_refcnt--;
277190af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
277290af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
277390af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
277490af4afaSJohn Baldwin 	} else
277590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
277690af4afaSJohn Baldwin }
277790af4afaSJohn Baldwin 
277890af4afaSJohn Baldwin struct sigacts *
277990af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
278090af4afaSJohn Baldwin {
278190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
278290af4afaSJohn Baldwin 	ps->ps_refcnt++;
278390af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
278490af4afaSJohn Baldwin 	return (ps);
278590af4afaSJohn Baldwin }
278690af4afaSJohn Baldwin 
278790af4afaSJohn Baldwin void
278890af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
278990af4afaSJohn Baldwin {
279090af4afaSJohn Baldwin 
279190af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
279290af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
279390af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
279490af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
279590af4afaSJohn Baldwin }
279690af4afaSJohn Baldwin 
279790af4afaSJohn Baldwin int
279890af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
279990af4afaSJohn Baldwin {
280090af4afaSJohn Baldwin 	int shared;
280190af4afaSJohn Baldwin 
280290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
280390af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
280490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
280590af4afaSJohn Baldwin 	return (shared);
280690af4afaSJohn Baldwin }
2807