xref: /freebsd/sys/kern/kern_sig.c (revision a4c2da1503833334ff94e3b573e87bdf183035f0)
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) ?
2388b94a061SJohn Baldwin #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
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);
350df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3512c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3522c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
353df8bae1dSRodney W. Grimes 		else
3548c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
355df8bae1dSRodney W. Grimes #endif
3562c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3572c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
35890af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3596626c604SJulian Elischer 			else
36090af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
361ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
362245f17d4SJoerg Wunsch 				/*
3632c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3642c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3652c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3662c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
367245f17d4SJoerg Wunsch 				 */
368245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
36990af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3706626c604SJulian Elischer 				else
37190af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
372245f17d4SJoerg Wunsch 			} else
37390af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
374ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
37590af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
376ba1551caSIan Dowse 			else
37790af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
378df8bae1dSRodney W. Grimes 		}
379df8bae1dSRodney W. Grimes 		/*
38090af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3812c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
38290af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3832c42a146SMarcel Moolenaar 		 * have to restart the process.
384df8bae1dSRodney W. Grimes 		 */
3852c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3862c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3872c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3889dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
3899dde3bc9SDavid Xu 			     SIGISMEMBER(p->p_siglist, sig)) {
3909dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
3919dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
3929dde3bc9SDavid Xu 			}
3932c42a146SMarcel Moolenaar 			/* never to be seen again */
3941d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
395a9a48d68SDavid Xu 			mtx_lock_spin(&sched_lock);
3964093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3974093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
398a9a48d68SDavid Xu 			mtx_unlock_spin(&sched_lock);
3992c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4002c42a146SMarcel Moolenaar 				/* easier in psignal */
40190af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
40290af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
403645682fdSLuoqi Chen 		} else {
40490af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
4052c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
40690af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
4072c42a146SMarcel Moolenaar 			else
40890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
4092c42a146SMarcel Moolenaar 		}
41023eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
41123eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
41223eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41323eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
41423eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
41523eeeff7SPeter Wemm 		else
41623eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
41723eeeff7SPeter Wemm #endif
418e8ebc08fSPeter Wemm #ifdef COMPAT_43
419645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
42023eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
42123eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
422645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
423645682fdSLuoqi Chen 		else
424645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
425e8ebc08fSPeter Wemm #endif
426df8bae1dSRodney W. Grimes 	}
42790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
428628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4292c42a146SMarcel Moolenaar 	return (0);
4302c42a146SMarcel Moolenaar }
4312c42a146SMarcel Moolenaar 
4322c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4332c42a146SMarcel Moolenaar struct sigaction_args {
4342c42a146SMarcel Moolenaar 	int	sig;
4352c42a146SMarcel Moolenaar 	struct	sigaction *act;
4362c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4372c42a146SMarcel Moolenaar };
4382c42a146SMarcel Moolenaar #endif
439fb99ab88SMatthew Dillon /*
440fb99ab88SMatthew Dillon  * MPSAFE
441fb99ab88SMatthew Dillon  */
4422c42a146SMarcel Moolenaar int
443b40ce416SJulian Elischer sigaction(td, uap)
444b40ce416SJulian Elischer 	struct thread *td;
4452c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4462c42a146SMarcel Moolenaar {
4472c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4482c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4492c42a146SMarcel Moolenaar 	int error;
4502c42a146SMarcel Moolenaar 
4516f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4526f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4532c42a146SMarcel Moolenaar 	if (actp) {
4546f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4552c42a146SMarcel Moolenaar 		if (error)
45644443757STim J. Robbins 			return (error);
4572c42a146SMarcel Moolenaar 	}
4588f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
45925d6dc06SJohn Baldwin 	if (oactp && !error)
4606f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4612c42a146SMarcel Moolenaar 	return (error);
4622c42a146SMarcel Moolenaar }
4632c42a146SMarcel Moolenaar 
46423eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
46523eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
46623eeeff7SPeter Wemm struct freebsd4_sigaction_args {
46723eeeff7SPeter Wemm 	int	sig;
46823eeeff7SPeter Wemm 	struct	sigaction *act;
46923eeeff7SPeter Wemm 	struct	sigaction *oact;
47023eeeff7SPeter Wemm };
47123eeeff7SPeter Wemm #endif
47223eeeff7SPeter Wemm /*
47323eeeff7SPeter Wemm  * MPSAFE
47423eeeff7SPeter Wemm  */
47523eeeff7SPeter Wemm int
47623eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
47723eeeff7SPeter Wemm 	struct thread *td;
47823eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
47923eeeff7SPeter Wemm {
48023eeeff7SPeter Wemm 	struct sigaction act, oact;
48123eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
48223eeeff7SPeter Wemm 	int error;
48323eeeff7SPeter Wemm 
48423eeeff7SPeter Wemm 
48523eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
48623eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
48723eeeff7SPeter Wemm 	if (actp) {
48823eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
48923eeeff7SPeter Wemm 		if (error)
49044443757STim J. Robbins 			return (error);
49123eeeff7SPeter Wemm 	}
49223eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
493a14e1189SJohn Baldwin 	if (oactp && !error)
49423eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
49523eeeff7SPeter Wemm 	return (error);
49623eeeff7SPeter Wemm }
49723eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
49823eeeff7SPeter Wemm 
49931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5002c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5012c42a146SMarcel Moolenaar struct osigaction_args {
5022c42a146SMarcel Moolenaar 	int	signum;
5032c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
5042c42a146SMarcel Moolenaar 	struct	osigaction *osa;
5052c42a146SMarcel Moolenaar };
5062c42a146SMarcel Moolenaar #endif
507fb99ab88SMatthew Dillon /*
508fb99ab88SMatthew Dillon  * MPSAFE
509fb99ab88SMatthew Dillon  */
5102c42a146SMarcel Moolenaar int
511b40ce416SJulian Elischer osigaction(td, uap)
512b40ce416SJulian Elischer 	struct thread *td;
5132c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
5142c42a146SMarcel Moolenaar {
5152c42a146SMarcel Moolenaar 	struct osigaction sa;
5162c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5172c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5182c42a146SMarcel Moolenaar 	int error;
5192c42a146SMarcel Moolenaar 
5206f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5216f841fb7SMarcel Moolenaar 		return (EINVAL);
522fb99ab88SMatthew Dillon 
5236f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5246f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
525fb99ab88SMatthew Dillon 
5262c42a146SMarcel Moolenaar 	if (nsap) {
5276f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5282c42a146SMarcel Moolenaar 		if (error)
52944443757STim J. Robbins 			return (error);
5302c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5312c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5322c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5332c42a146SMarcel Moolenaar 	}
53423eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5352c42a146SMarcel Moolenaar 	if (osap && !error) {
5362c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5372c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5382c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5396f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5402c42a146SMarcel Moolenaar 	}
5412c42a146SMarcel Moolenaar 	return (error);
5422c42a146SMarcel Moolenaar }
54323eeeff7SPeter Wemm 
54423eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
54523eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
54623eeeff7SPeter Wemm int
54723eeeff7SPeter Wemm osigreturn(td, uap)
54823eeeff7SPeter Wemm 	struct thread *td;
54923eeeff7SPeter Wemm 	struct osigreturn_args *uap;
55023eeeff7SPeter Wemm {
55123eeeff7SPeter Wemm 
55223eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
55323eeeff7SPeter Wemm }
55423eeeff7SPeter Wemm #endif
55531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes /*
558df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
559df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
560df8bae1dSRodney W. Grimes  */
561df8bae1dSRodney W. Grimes void
562df8bae1dSRodney W. Grimes siginit(p)
563df8bae1dSRodney W. Grimes 	struct proc *p;
564df8bae1dSRodney W. Grimes {
565df8bae1dSRodney W. Grimes 	register int i;
56690af4afaSJohn Baldwin 	struct sigacts *ps;
567df8bae1dSRodney W. Grimes 
568628d2653SJohn Baldwin 	PROC_LOCK(p);
56990af4afaSJohn Baldwin 	ps = p->p_sigacts;
57090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5712c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5722c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
57390af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
57490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
575628d2653SJohn Baldwin 	PROC_UNLOCK(p);
576df8bae1dSRodney W. Grimes }
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes /*
579df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
580df8bae1dSRodney W. Grimes  */
581df8bae1dSRodney W. Grimes void
582a30ec4b9SDavid Xu execsigs(struct proc *p)
583df8bae1dSRodney W. Grimes {
584a30ec4b9SDavid Xu 	struct sigacts *ps;
585a30ec4b9SDavid Xu 	int sig;
586a30ec4b9SDavid Xu 	struct thread *td;
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 	/*
589df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5904093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
591df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
592df8bae1dSRodney W. Grimes 	 */
5939b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
594a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
595628d2653SJohn Baldwin 	ps = p->p_sigacts;
59690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
59790af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
59890af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
59990af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
6002c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
6012c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
60290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
6031d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
6044093529dSJeff Roberson 			/*
6054093529dSJeff Roberson 			 * There is only one thread at this point.
6064093529dSJeff Roberson 			 */
607a30ec4b9SDavid Xu 			SIGDELSET(td->td_siglist, sig);
608df8bae1dSRodney W. Grimes 		}
6092c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
610df8bae1dSRodney W. Grimes 	}
611df8bae1dSRodney W. Grimes 	/*
612df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
613df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
614df8bae1dSRodney W. Grimes 	 */
615a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
616a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
617a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
618a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
61980e907a1SPeter Wemm 	/*
62080e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
62180e907a1SPeter Wemm 	 */
62290af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
623c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
624c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
62590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
626df8bae1dSRodney W. Grimes }
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes /*
629e77daab1SJohn Baldwin  * kern_sigprocmask()
6307c8fdcbdSMatthew Dillon  *
631628d2653SJohn Baldwin  *	Manipulate signal mask.
632df8bae1dSRodney W. Grimes  */
633e77daab1SJohn Baldwin int
634e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6354093529dSJeff Roberson 	struct thread *td;
6362c42a146SMarcel Moolenaar 	int how;
6372c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
638645682fdSLuoqi Chen 	int old;
6392c42a146SMarcel Moolenaar {
6402c42a146SMarcel Moolenaar 	int error;
6412c42a146SMarcel Moolenaar 
6424093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6432c42a146SMarcel Moolenaar 	if (oset != NULL)
6444093529dSJeff Roberson 		*oset = td->td_sigmask;
6452c42a146SMarcel Moolenaar 
6462c42a146SMarcel Moolenaar 	error = 0;
6472c42a146SMarcel Moolenaar 	if (set != NULL) {
6482c42a146SMarcel Moolenaar 		switch (how) {
6492c42a146SMarcel Moolenaar 		case SIG_BLOCK:
650645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6514093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6522c42a146SMarcel Moolenaar 			break;
6532c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6544093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6554093529dSJeff Roberson 			signotify(td);
6562c42a146SMarcel Moolenaar 			break;
6572c42a146SMarcel Moolenaar 		case SIG_SETMASK:
658645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
659645682fdSLuoqi Chen 			if (old)
6604093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
661645682fdSLuoqi Chen 			else
6624093529dSJeff Roberson 				td->td_sigmask = *set;
6634093529dSJeff Roberson 			signotify(td);
6642c42a146SMarcel Moolenaar 			break;
6652c42a146SMarcel Moolenaar 		default:
6662c42a146SMarcel Moolenaar 			error = EINVAL;
6672c42a146SMarcel Moolenaar 			break;
6682c42a146SMarcel Moolenaar 		}
6692c42a146SMarcel Moolenaar 	}
6704093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6712c42a146SMarcel Moolenaar 	return (error);
6722c42a146SMarcel Moolenaar }
6732c42a146SMarcel Moolenaar 
6747c8fdcbdSMatthew Dillon /*
675e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6767c8fdcbdSMatthew Dillon  */
6777c8fdcbdSMatthew Dillon 
678d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
679df8bae1dSRodney W. Grimes struct sigprocmask_args {
680df8bae1dSRodney W. Grimes 	int	how;
6812c42a146SMarcel Moolenaar 	const sigset_t *set;
6822c42a146SMarcel Moolenaar 	sigset_t *oset;
683df8bae1dSRodney W. Grimes };
684d2d3e875SBruce Evans #endif
68526f9a767SRodney W. Grimes int
686b40ce416SJulian Elischer sigprocmask(td, uap)
687b40ce416SJulian Elischer 	register struct thread *td;
688df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
689df8bae1dSRodney W. Grimes {
6902c42a146SMarcel Moolenaar 	sigset_t set, oset;
6912c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6922c42a146SMarcel Moolenaar 	int error;
693df8bae1dSRodney W. Grimes 
6946f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6956f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6962c42a146SMarcel Moolenaar 	if (setp) {
6976f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6982c42a146SMarcel Moolenaar 		if (error)
6992c42a146SMarcel Moolenaar 			return (error);
700df8bae1dSRodney W. Grimes 	}
701e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
7022c42a146SMarcel Moolenaar 	if (osetp && !error) {
7036f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
7042c42a146SMarcel Moolenaar 	}
7052c42a146SMarcel Moolenaar 	return (error);
7062c42a146SMarcel Moolenaar }
7072c42a146SMarcel Moolenaar 
70831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7097c8fdcbdSMatthew Dillon /*
7107c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
7117c8fdcbdSMatthew Dillon  */
7122c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7132c42a146SMarcel Moolenaar struct osigprocmask_args {
7142c42a146SMarcel Moolenaar 	int	how;
7152c42a146SMarcel Moolenaar 	osigset_t mask;
7162c42a146SMarcel Moolenaar };
7172c42a146SMarcel Moolenaar #endif
7182c42a146SMarcel Moolenaar int
719b40ce416SJulian Elischer osigprocmask(td, uap)
720b40ce416SJulian Elischer 	register struct thread *td;
7212c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7222c42a146SMarcel Moolenaar {
7232c42a146SMarcel Moolenaar 	sigset_t set, oset;
7242c42a146SMarcel Moolenaar 	int error;
7252c42a146SMarcel Moolenaar 
7262c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
727e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
728b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
729df8bae1dSRodney W. Grimes 	return (error);
730df8bae1dSRodney W. Grimes }
73131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
732df8bae1dSRodney W. Grimes 
733d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
734df8bae1dSRodney W. Grimes struct sigpending_args {
7352c42a146SMarcel Moolenaar 	sigset_t	*set;
736df8bae1dSRodney W. Grimes };
737d2d3e875SBruce Evans #endif
738fb99ab88SMatthew Dillon /*
739fb99ab88SMatthew Dillon  * MPSAFE
740fb99ab88SMatthew Dillon  */
74126f9a767SRodney W. Grimes int
742a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
743a447cd8bSJeff Roberson {
744a447cd8bSJeff Roberson 	siginfo_t info;
745a447cd8bSJeff Roberson 	sigset_t set;
746a447cd8bSJeff Roberson 	int error;
747a447cd8bSJeff Roberson 
748a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
749a447cd8bSJeff Roberson 	if (error)
750a447cd8bSJeff Roberson 		return (error);
751a447cd8bSJeff Roberson 
752a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
753a447cd8bSJeff Roberson 	if (error)
754a447cd8bSJeff Roberson 		return (error);
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 	}
763a447cd8bSJeff Roberson 	return (error);
764a447cd8bSJeff Roberson }
765a447cd8bSJeff Roberson /*
766a447cd8bSJeff Roberson  * MPSAFE
767a447cd8bSJeff Roberson  */
768a447cd8bSJeff Roberson int
769a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
770a447cd8bSJeff Roberson {
771a447cd8bSJeff Roberson 	struct timespec ts;
772a447cd8bSJeff Roberson 	struct timespec *timeout;
773a447cd8bSJeff Roberson 	sigset_t set;
774a447cd8bSJeff Roberson 	siginfo_t info;
775a447cd8bSJeff Roberson 	int error;
776a447cd8bSJeff Roberson 
777a447cd8bSJeff Roberson 	if (uap->timeout) {
778a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
779a447cd8bSJeff Roberson 		if (error)
780a447cd8bSJeff Roberson 			return (error);
781a447cd8bSJeff Roberson 
782a447cd8bSJeff Roberson 		timeout = &ts;
783a447cd8bSJeff Roberson 	} else
784a447cd8bSJeff Roberson 		timeout = NULL;
785a447cd8bSJeff Roberson 
786a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
787a447cd8bSJeff Roberson 	if (error)
788a447cd8bSJeff Roberson 		return (error);
789a447cd8bSJeff Roberson 
790a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
791a447cd8bSJeff Roberson 	if (error)
792a447cd8bSJeff Roberson 		return (error);
7939dde3bc9SDavid Xu 
794418228dfSDavid Xu 	if (uap->info)
795a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
796a447cd8bSJeff Roberson 	/* Repost if we got an error. */
79718440c7fSJohn Baldwin 	if (error && info.si_signo) {
79818440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
799c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
80018440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
801418228dfSDavid Xu 	} else {
802418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
80318440c7fSJohn Baldwin 	}
804a447cd8bSJeff Roberson 	return (error);
805a447cd8bSJeff Roberson }
806a447cd8bSJeff Roberson 
807a447cd8bSJeff Roberson /*
808a447cd8bSJeff Roberson  * MPSAFE
809a447cd8bSJeff Roberson  */
810a447cd8bSJeff Roberson int
811a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
812a447cd8bSJeff Roberson {
813a447cd8bSJeff Roberson 	siginfo_t info;
814a447cd8bSJeff Roberson 	sigset_t set;
815a447cd8bSJeff Roberson 	int error;
816a447cd8bSJeff Roberson 
817a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
818a447cd8bSJeff Roberson 	if (error)
819a447cd8bSJeff Roberson 		return (error);
820a447cd8bSJeff Roberson 
821a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
822a447cd8bSJeff Roberson 	if (error)
823a447cd8bSJeff Roberson 		return (error);
824a447cd8bSJeff Roberson 
825418228dfSDavid Xu 	if (uap->info)
826a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
827a447cd8bSJeff Roberson 	/* Repost if we got an error. */
82818440c7fSJohn Baldwin 	if (error && info.si_signo) {
82918440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
830c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
83118440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
832418228dfSDavid Xu 	} else {
833418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
83418440c7fSJohn Baldwin 	}
835a447cd8bSJeff Roberson 	return (error);
836a447cd8bSJeff Roberson }
837a447cd8bSJeff Roberson 
838a447cd8bSJeff Roberson static int
8393074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
840a447cd8bSJeff Roberson     struct timespec *timeout)
841a447cd8bSJeff Roberson {
8423074d1b4SDavid Xu 	struct sigacts *ps;
8433074d1b4SDavid Xu 	sigset_t savedmask, sigset;
844a447cd8bSJeff Roberson 	struct proc *p;
845a447cd8bSJeff Roberson 	int error;
846a447cd8bSJeff Roberson 	int sig;
847a447cd8bSJeff Roberson 	int hz;
8483074d1b4SDavid Xu 	int i;
849a447cd8bSJeff Roberson 
850a447cd8bSJeff Roberson 	p = td->td_proc;
851a447cd8bSJeff Roberson 	error = 0;
852a447cd8bSJeff Roberson 	sig = 0;
8533074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
854a447cd8bSJeff Roberson 
855a447cd8bSJeff Roberson 	PROC_LOCK(p);
856a447cd8bSJeff Roberson 	ps = p->p_sigacts;
8573074d1b4SDavid Xu 	savedmask = td->td_sigmask;
8583074d1b4SDavid Xu 
8593074d1b4SDavid Xu again:
8603074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
8613074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
8623074d1b4SDavid Xu 			continue;
8633074d1b4SDavid Xu 		if (SIGISMEMBER(td->td_siglist, i)) {
864418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
865418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8663074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
86790af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
868a447cd8bSJeff Roberson 			sig = cursig(td);
8693074d1b4SDavid Xu 			i = 0;
8703074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8713074d1b4SDavid Xu 		} else if (SIGISMEMBER(p->p_siglist, i)) {
8723074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
8733074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
8743074d1b4SDavid Xu 				wakeup(&p->p_siglist);
8753074d1b4SDavid Xu 			}
8763074d1b4SDavid Xu 			SIGDELSET(p->p_siglist, i);
8773074d1b4SDavid Xu 			SIGADDSET(td->td_siglist, i);
8783074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
8793074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8803074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
8813074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
8823074d1b4SDavid Xu 			sig = cursig(td);
8833074d1b4SDavid Xu 			i = 0;
8843074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8853074d1b4SDavid Xu 		}
8863074d1b4SDavid Xu 		if (sig) {
8873074d1b4SDavid Xu 			td->td_sigmask = savedmask;
8883074d1b4SDavid Xu 			signotify(td);
889a447cd8bSJeff Roberson 			goto out;
8903074d1b4SDavid Xu 		}
8913074d1b4SDavid Xu 	}
8923074d1b4SDavid Xu 	if (error)
8933074d1b4SDavid Xu 		goto out;
8943074d1b4SDavid Xu 
8953074d1b4SDavid Xu 	td->td_sigmask = savedmask;
8963074d1b4SDavid Xu 	signotify(td);
8973074d1b4SDavid Xu 	sigset = td->td_siglist;
8983074d1b4SDavid Xu 	SIGSETOR(sigset, p->p_siglist);
8993074d1b4SDavid Xu 	SIGSETAND(sigset, waitset);
9003074d1b4SDavid Xu 	if (!SIGISEMPTY(sigset))
9013074d1b4SDavid Xu 		goto again;
902a447cd8bSJeff Roberson 
903a447cd8bSJeff Roberson 	/*
904a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
905a447cd8bSJeff Roberson 	 * signals.
906a447cd8bSJeff Roberson 	 */
907a447cd8bSJeff Roberson 	if (timeout) {
908a447cd8bSJeff Roberson 		struct timeval tv;
909a447cd8bSJeff Roberson 
910a6ca4808SMike Makonnen 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
911a447cd8bSJeff Roberson 			error = EINVAL;
912a447cd8bSJeff Roberson 			goto out;
913a447cd8bSJeff Roberson 		}
9143074d1b4SDavid Xu 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
9153074d1b4SDavid Xu 			error = EAGAIN;
9163074d1b4SDavid Xu 			goto out;
9173074d1b4SDavid Xu 		}
918a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
919a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
920a447cd8bSJeff Roberson 	} else
921a447cd8bSJeff Roberson 		hz = 0;
922a447cd8bSJeff Roberson 
9233074d1b4SDavid Xu 	td->td_waitset = &waitset;
92486b5e563SDag-Erling Smørgrav 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
9253074d1b4SDavid Xu 	td->td_waitset = NULL;
9263074d1b4SDavid Xu 	if (error == 0) /* surplus wakeup ? */
9273074d1b4SDavid Xu 		error = EINTR;
9283074d1b4SDavid Xu 	goto again;
9299dde3bc9SDavid Xu 
930a447cd8bSJeff Roberson out:
931a447cd8bSJeff Roberson 	if (sig) {
932a447cd8bSJeff Roberson 		sig_t action;
933a447cd8bSJeff Roberson 
9343074d1b4SDavid Xu 		error = 0;
9353074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
936a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
93790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
938a447cd8bSJeff Roberson #ifdef KTRACE
939a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
9403074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
941a447cd8bSJeff Roberson #endif
942a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
943a447cd8bSJeff Roberson 
944a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
945a447cd8bSJeff Roberson 		info->si_signo = sig;
946a447cd8bSJeff Roberson 		info->si_code = 0;
9473074d1b4SDavid Xu 	}
948a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
949a447cd8bSJeff Roberson 	return (error);
950a447cd8bSJeff Roberson }
951a447cd8bSJeff Roberson 
952a447cd8bSJeff Roberson /*
953a447cd8bSJeff Roberson  * MPSAFE
954a447cd8bSJeff Roberson  */
955a447cd8bSJeff Roberson int
956b40ce416SJulian Elischer sigpending(td, uap)
957b40ce416SJulian Elischer 	struct thread *td;
958df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
959df8bae1dSRodney W. Grimes {
960b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
961628d2653SJohn Baldwin 	sigset_t siglist;
962df8bae1dSRodney W. Grimes 
963628d2653SJohn Baldwin 	PROC_LOCK(p);
9641d9c5696SJuli Mallett 	siglist = p->p_siglist;
9654093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
966628d2653SJohn Baldwin 	PROC_UNLOCK(p);
96748e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9682c42a146SMarcel Moolenaar }
9692c42a146SMarcel Moolenaar 
97031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9712c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9722c42a146SMarcel Moolenaar struct osigpending_args {
9732c42a146SMarcel Moolenaar 	int	dummy;
9742c42a146SMarcel Moolenaar };
9752c42a146SMarcel Moolenaar #endif
976fb99ab88SMatthew Dillon /*
977fb99ab88SMatthew Dillon  * MPSAFE
978fb99ab88SMatthew Dillon  */
9792c42a146SMarcel Moolenaar int
980b40ce416SJulian Elischer osigpending(td, uap)
981b40ce416SJulian Elischer 	struct thread *td;
9822c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9832c42a146SMarcel Moolenaar {
984b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9854093529dSJeff Roberson 	sigset_t siglist;
986b40ce416SJulian Elischer 
987628d2653SJohn Baldwin 	PROC_LOCK(p);
9884093529dSJeff Roberson 	siglist = p->p_siglist;
9894093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
990628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9919d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
992df8bae1dSRodney W. Grimes 	return (0);
993df8bae1dSRodney W. Grimes }
99431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
995df8bae1dSRodney W. Grimes 
996df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
997df8bae1dSRodney W. Grimes /*
998df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
999df8bae1dSRodney W. Grimes  */
1000d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1001df8bae1dSRodney W. Grimes struct osigvec_args {
1002df8bae1dSRodney W. Grimes 	int	signum;
1003df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1004df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1005df8bae1dSRodney W. Grimes };
1006d2d3e875SBruce Evans #endif
1007fb99ab88SMatthew Dillon /*
1008fb99ab88SMatthew Dillon  * MPSAFE
1009fb99ab88SMatthew Dillon  */
1010df8bae1dSRodney W. Grimes /* ARGSUSED */
101126f9a767SRodney W. Grimes int
1012b40ce416SJulian Elischer osigvec(td, uap)
1013b40ce416SJulian Elischer 	struct thread *td;
1014df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1015df8bae1dSRodney W. Grimes {
1016df8bae1dSRodney W. Grimes 	struct sigvec vec;
10172c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
10182c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
10192c42a146SMarcel Moolenaar 	int error;
1020df8bae1dSRodney W. Grimes 
10216f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10226f841fb7SMarcel Moolenaar 		return (EINVAL);
10236f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
10246f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
10252c42a146SMarcel Moolenaar 	if (nsap) {
10266f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
10272c42a146SMarcel Moolenaar 		if (error)
1028df8bae1dSRodney W. Grimes 			return (error);
10292c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
10302c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
10312c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
10322c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1033df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
10342c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
1035df8bae1dSRodney W. Grimes #endif
1036df8bae1dSRodney W. Grimes 	}
10375edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10382c42a146SMarcel Moolenaar 	if (osap && !error) {
10392c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
10402c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
10412c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
10422c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
10432c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
10442c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
10452c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
10462c42a146SMarcel Moolenaar #endif
10476f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10482c42a146SMarcel Moolenaar 	}
10492c42a146SMarcel Moolenaar 	return (error);
1050df8bae1dSRodney W. Grimes }
1051df8bae1dSRodney W. Grimes 
1052d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1053df8bae1dSRodney W. Grimes struct osigblock_args {
1054df8bae1dSRodney W. Grimes 	int	mask;
1055df8bae1dSRodney W. Grimes };
1056d2d3e875SBruce Evans #endif
1057fb99ab88SMatthew Dillon /*
1058fb99ab88SMatthew Dillon  * MPSAFE
1059fb99ab88SMatthew Dillon  */
106026f9a767SRodney W. Grimes int
1061b40ce416SJulian Elischer osigblock(td, uap)
1062b40ce416SJulian Elischer 	register struct thread *td;
1063df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1064df8bae1dSRodney W. Grimes {
1065b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10662c42a146SMarcel Moolenaar 	sigset_t set;
1067df8bae1dSRodney W. Grimes 
10682c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10692c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1070628d2653SJohn Baldwin 	PROC_LOCK(p);
10714093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10724093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1073628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1074df8bae1dSRodney W. Grimes 	return (0);
1075df8bae1dSRodney W. Grimes }
1076df8bae1dSRodney W. Grimes 
1077d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1078df8bae1dSRodney W. Grimes struct osigsetmask_args {
1079df8bae1dSRodney W. Grimes 	int	mask;
1080df8bae1dSRodney W. Grimes };
1081d2d3e875SBruce Evans #endif
1082fb99ab88SMatthew Dillon /*
1083fb99ab88SMatthew Dillon  * MPSAFE
1084fb99ab88SMatthew Dillon  */
108526f9a767SRodney W. Grimes int
1086b40ce416SJulian Elischer osigsetmask(td, uap)
1087b40ce416SJulian Elischer 	struct thread *td;
1088df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1089df8bae1dSRodney W. Grimes {
1090b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10912c42a146SMarcel Moolenaar 	sigset_t set;
1092df8bae1dSRodney W. Grimes 
10932c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10942c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1095628d2653SJohn Baldwin 	PROC_LOCK(p);
10964093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10974093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10984093529dSJeff Roberson 	signotify(td);
1099628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1100df8bae1dSRodney W. Grimes 	return (0);
1101df8bae1dSRodney W. Grimes }
1102df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1103df8bae1dSRodney W. Grimes 
1104df8bae1dSRodney W. Grimes /*
1105df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
110697563428SAlexander Kabaev  * in the meantime.
1107b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1108b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1109b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1110df8bae1dSRodney W. Grimes  */
1111d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1112df8bae1dSRodney W. Grimes struct sigsuspend_args {
11132c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1114df8bae1dSRodney W. Grimes };
1115d2d3e875SBruce Evans #endif
1116fb99ab88SMatthew Dillon /*
1117fb99ab88SMatthew Dillon  * MPSAFE
1118fb99ab88SMatthew Dillon  */
1119df8bae1dSRodney W. Grimes /* ARGSUSED */
112026f9a767SRodney W. Grimes int
1121b40ce416SJulian Elischer sigsuspend(td, uap)
1122b40ce416SJulian Elischer 	struct thread *td;
1123df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1124df8bae1dSRodney W. Grimes {
11252c42a146SMarcel Moolenaar 	sigset_t mask;
11262c42a146SMarcel Moolenaar 	int error;
11272c42a146SMarcel Moolenaar 
11286f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11292c42a146SMarcel Moolenaar 	if (error)
11302c42a146SMarcel Moolenaar 		return (error);
11318f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11328f19eb88SIan Dowse }
11338f19eb88SIan Dowse 
11348f19eb88SIan Dowse int
11358f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11368f19eb88SIan Dowse {
11378f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 	/*
1140645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1141df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1142df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1143df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1144df8bae1dSRodney W. Grimes 	 * to indicate this.
1145df8bae1dSRodney W. Grimes 	 */
1146628d2653SJohn Baldwin 	PROC_LOCK(p);
11474093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11485e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1149645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11504093529dSJeff Roberson 	td->td_sigmask = mask;
11514093529dSJeff Roberson 	signotify(td);
115286b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11532c42a146SMarcel Moolenaar 		/* void */;
1154628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11552c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11562c42a146SMarcel Moolenaar 	return (EINTR);
11572c42a146SMarcel Moolenaar }
11582c42a146SMarcel Moolenaar 
115931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
116097563428SAlexander Kabaev /*
116197563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
116297563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
116397563428SAlexander Kabaev  */
11642c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11652c42a146SMarcel Moolenaar struct osigsuspend_args {
11662c42a146SMarcel Moolenaar 	osigset_t mask;
11672c42a146SMarcel Moolenaar };
11682c42a146SMarcel Moolenaar #endif
1169fb99ab88SMatthew Dillon /*
1170fb99ab88SMatthew Dillon  * MPSAFE
1171fb99ab88SMatthew Dillon  */
11722c42a146SMarcel Moolenaar /* ARGSUSED */
11732c42a146SMarcel Moolenaar int
1174b40ce416SJulian Elischer osigsuspend(td, uap)
1175b40ce416SJulian Elischer 	struct thread *td;
11762c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11772c42a146SMarcel Moolenaar {
1178b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1179645682fdSLuoqi Chen 	sigset_t mask;
11802c42a146SMarcel Moolenaar 
1181628d2653SJohn Baldwin 	PROC_LOCK(p);
11824093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11835e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1184645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1185645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11864093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11874093529dSJeff Roberson 	signotify(td);
118886b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1189df8bae1dSRodney W. Grimes 		/* void */;
1190628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1191df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1192df8bae1dSRodney W. Grimes 	return (EINTR);
1193df8bae1dSRodney W. Grimes }
119431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1197d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1198df8bae1dSRodney W. Grimes struct osigstack_args {
1199df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1200df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1201df8bae1dSRodney W. Grimes };
1202d2d3e875SBruce Evans #endif
1203fb99ab88SMatthew Dillon /*
1204fb99ab88SMatthew Dillon  * MPSAFE
1205fb99ab88SMatthew Dillon  */
1206df8bae1dSRodney W. Grimes /* ARGSUSED */
120726f9a767SRodney W. Grimes int
1208b40ce416SJulian Elischer osigstack(td, uap)
1209b40ce416SJulian Elischer 	struct thread *td;
1210df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1211df8bae1dSRodney W. Grimes {
12125afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1213fb99ab88SMatthew Dillon 	int error = 0;
1214fb99ab88SMatthew Dillon 
1215d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12165afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12175afe0c99SJohn Baldwin 		if (error)
12185afe0c99SJohn Baldwin 			return (error);
1219df8bae1dSRodney W. Grimes 	}
1220a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12215afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12225afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1223a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1224a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1225a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1226a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12275afe0c99SJohn Baldwin 	}
12285afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12295afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12305afe0c99SJohn Baldwin 
1231fb99ab88SMatthew Dillon 	return (error);
1232df8bae1dSRodney W. Grimes }
1233df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1234df8bae1dSRodney W. Grimes 
1235d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1236df8bae1dSRodney W. Grimes struct sigaltstack_args {
12372c42a146SMarcel Moolenaar 	stack_t	*ss;
12382c42a146SMarcel Moolenaar 	stack_t	*oss;
1239df8bae1dSRodney W. Grimes };
1240d2d3e875SBruce Evans #endif
1241fb99ab88SMatthew Dillon /*
1242fb99ab88SMatthew Dillon  * MPSAFE
1243fb99ab88SMatthew Dillon  */
1244df8bae1dSRodney W. Grimes /* ARGSUSED */
124526f9a767SRodney W. Grimes int
1246b40ce416SJulian Elischer sigaltstack(td, uap)
1247b40ce416SJulian Elischer 	struct thread *td;
1248df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1249df8bae1dSRodney W. Grimes {
12508f19eb88SIan Dowse 	stack_t ss, oss;
12518f19eb88SIan Dowse 	int error;
12528f19eb88SIan Dowse 
12538f19eb88SIan Dowse 	if (uap->ss != NULL) {
12548f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12558f19eb88SIan Dowse 		if (error)
12568f19eb88SIan Dowse 			return (error);
12578f19eb88SIan Dowse 	}
12588f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12598f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12608f19eb88SIan Dowse 	if (error)
12618f19eb88SIan Dowse 		return (error);
12628f19eb88SIan Dowse 	if (uap->oss != NULL)
12638f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12648f19eb88SIan Dowse 	return (error);
12658f19eb88SIan Dowse }
12668f19eb88SIan Dowse 
12678f19eb88SIan Dowse int
12688f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12698f19eb88SIan Dowse {
1270b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1271fb99ab88SMatthew Dillon 	int oonstack;
1272fb99ab88SMatthew Dillon 
1273b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1274d034d459SMarcel Moolenaar 
12758f19eb88SIan Dowse 	if (oss != NULL) {
1276a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1277a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1278d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1279df8bae1dSRodney W. Grimes 	}
1280d034d459SMarcel Moolenaar 
12818f19eb88SIan Dowse 	if (ss != NULL) {
1282a30ec4b9SDavid Xu 		if (oonstack)
1283cf60731bSJohn Baldwin 			return (EPERM);
1284a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1285cf60731bSJohn Baldwin 			return (EINVAL);
12868f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12878f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1288cf60731bSJohn Baldwin 				return (ENOMEM);
1289fb99ab88SMatthew Dillon 			}
1290a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1291a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1292628d2653SJohn Baldwin 		} else {
1293a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1294628d2653SJohn Baldwin 		}
1295d034d459SMarcel Moolenaar 	}
1296cf60731bSJohn Baldwin 	return (0);
1297df8bae1dSRodney W. Grimes }
1298df8bae1dSRodney W. Grimes 
1299d93f860cSPoul-Henning Kamp /*
1300d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1301d93f860cSPoul-Henning Kamp  * cp is calling process.
1302d93f860cSPoul-Henning Kamp  */
130337c84183SPoul-Henning Kamp static int
13049c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
13059c1ab3e0SJohn Baldwin 	register struct thread *td;
13062c42a146SMarcel Moolenaar 	int sig, pgid, all;
1307d93f860cSPoul-Henning Kamp {
1308d93f860cSPoul-Henning Kamp 	register struct proc *p;
1309d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1310d93f860cSPoul-Henning Kamp 	int nfound = 0;
1311d93f860cSPoul-Henning Kamp 
1312553629ebSJake Burkholder 	if (all) {
1313d93f860cSPoul-Henning Kamp 		/*
1314d93f860cSPoul-Henning Kamp 		 * broadcast
1315d93f860cSPoul-Henning Kamp 		 */
13161005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13172e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1318628d2653SJohn Baldwin 			PROC_LOCK(p);
13199c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13209c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1321628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1322628d2653SJohn Baldwin 				continue;
1323628d2653SJohn Baldwin 			}
1324f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1325d93f860cSPoul-Henning Kamp 				nfound++;
132633a9ed9dSJohn Baldwin 				if (sig)
13272c42a146SMarcel Moolenaar 					psignal(p, sig);
1328628d2653SJohn Baldwin 			}
132933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1330d93f860cSPoul-Henning Kamp 		}
13311005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1332553629ebSJake Burkholder 	} else {
1333ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1334f591779bSSeigo Tanimura 		if (pgid == 0) {
1335d93f860cSPoul-Henning Kamp 			/*
1336d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1337d93f860cSPoul-Henning Kamp 			 */
13389c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1339f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1340f591779bSSeigo Tanimura 		} else {
1341d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1342f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1343ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1344d93f860cSPoul-Henning Kamp 				return (ESRCH);
1345d93f860cSPoul-Henning Kamp 			}
1346f591779bSSeigo Tanimura 		}
1347ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13482e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1349628d2653SJohn Baldwin 			PROC_LOCK(p);
1350628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1351628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1352628d2653SJohn Baldwin 				continue;
1353628d2653SJohn Baldwin 			}
1354e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
135533a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1356628d2653SJohn Baldwin 				continue;
1357628d2653SJohn Baldwin 			}
1358f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1359d93f860cSPoul-Henning Kamp 				nfound++;
136033a9ed9dSJohn Baldwin 				if (sig)
13612c42a146SMarcel Moolenaar 					psignal(p, sig);
1362628d2653SJohn Baldwin 			}
136333a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1364d93f860cSPoul-Henning Kamp 		}
1365f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1366d93f860cSPoul-Henning Kamp 	}
1367d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1368d93f860cSPoul-Henning Kamp }
1369d93f860cSPoul-Henning Kamp 
1370d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1371df8bae1dSRodney W. Grimes struct kill_args {
1372df8bae1dSRodney W. Grimes 	int	pid;
1373df8bae1dSRodney W. Grimes 	int	signum;
1374df8bae1dSRodney W. Grimes };
1375d2d3e875SBruce Evans #endif
1376fb99ab88SMatthew Dillon /*
1377fb99ab88SMatthew Dillon  * MPSAFE
1378fb99ab88SMatthew Dillon  */
1379df8bae1dSRodney W. Grimes /* ARGSUSED */
138026f9a767SRodney W. Grimes int
1381b40ce416SJulian Elischer kill(td, uap)
1382b40ce416SJulian Elischer 	register struct thread *td;
1383df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1384df8bae1dSRodney W. Grimes {
1385df8bae1dSRodney W. Grimes 	register struct proc *p;
138690af4afaSJohn Baldwin 	int error;
1387df8bae1dSRodney W. Grimes 
13886c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1389df8bae1dSRodney W. Grimes 		return (EINVAL);
1390fb99ab88SMatthew Dillon 
1391df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1392df8bae1dSRodney W. Grimes 		/* kill single process */
139390af4afaSJohn Baldwin 		if ((p = pfind(uap->pid)) == NULL)
139490af4afaSJohn Baldwin 			return (ESRCH);
139590af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
139690af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1397df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1398628d2653SJohn Baldwin 		PROC_UNLOCK(p);
139990af4afaSJohn Baldwin 		return (error);
1400df8bae1dSRodney W. Grimes 	}
1401df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1402df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
140390af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1404df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
140590af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1406df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
140790af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1408df8bae1dSRodney W. Grimes 	}
140990af4afaSJohn Baldwin 	/* NOTREACHED */
1410df8bae1dSRodney W. Grimes }
1411df8bae1dSRodney W. Grimes 
1412df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1413d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1414df8bae1dSRodney W. Grimes struct okillpg_args {
1415df8bae1dSRodney W. Grimes 	int	pgid;
1416df8bae1dSRodney W. Grimes 	int	signum;
1417df8bae1dSRodney W. Grimes };
1418d2d3e875SBruce Evans #endif
1419fb99ab88SMatthew Dillon /*
1420fb99ab88SMatthew Dillon  * MPSAFE
1421fb99ab88SMatthew Dillon  */
1422df8bae1dSRodney W. Grimes /* ARGSUSED */
142326f9a767SRodney W. Grimes int
1424b40ce416SJulian Elischer okillpg(td, uap)
1425b40ce416SJulian Elischer 	struct thread *td;
1426df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1427df8bae1dSRodney W. Grimes {
1428df8bae1dSRodney W. Grimes 
14296c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1430df8bae1dSRodney W. Grimes 		return (EINVAL);
143190af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1432df8bae1dSRodney W. Grimes }
1433df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1434df8bae1dSRodney W. Grimes 
1435df8bae1dSRodney W. Grimes /*
1436df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1437df8bae1dSRodney W. Grimes  */
1438df8bae1dSRodney W. Grimes void
14392c42a146SMarcel Moolenaar gsignal(pgid, sig)
14402c42a146SMarcel Moolenaar 	int pgid, sig;
1441df8bae1dSRodney W. Grimes {
1442df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1443df8bae1dSRodney W. Grimes 
1444f591779bSSeigo Tanimura 	if (pgid != 0) {
1445ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1446f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1447ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1448f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14492c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1450f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1451f591779bSSeigo Tanimura 		}
1452f591779bSSeigo Tanimura 	}
1453df8bae1dSRodney W. Grimes }
1454df8bae1dSRodney W. Grimes 
1455df8bae1dSRodney W. Grimes /*
1456df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1457df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1458df8bae1dSRodney W. Grimes  */
1459df8bae1dSRodney W. Grimes void
14602c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1461df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14622c42a146SMarcel Moolenaar 	int sig, checkctty;
1463df8bae1dSRodney W. Grimes {
1464df8bae1dSRodney W. Grimes 	register struct proc *p;
1465df8bae1dSRodney W. Grimes 
1466628d2653SJohn Baldwin 	if (pgrp) {
1467f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1468628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1469628d2653SJohn Baldwin 			PROC_LOCK(p);
1470df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14712c42a146SMarcel Moolenaar 				psignal(p, sig);
1472628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1473628d2653SJohn Baldwin 		}
1474628d2653SJohn Baldwin 	}
1475df8bae1dSRodney W. Grimes }
1476df8bae1dSRodney W. Grimes 
1477df8bae1dSRodney W. Grimes /*
14781bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1479df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1480df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1481356861dbSMatthew Dillon  *
1482356861dbSMatthew Dillon  * MPSAFE
1483df8bae1dSRodney W. Grimes  */
1484df8bae1dSRodney W. Grimes void
14851bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1486df8bae1dSRodney W. Grimes {
14871bf4700bSJeff Roberson 	struct sigacts *ps;
14881bf4700bSJeff Roberson 	struct proc *p;
14899dde3bc9SDavid Xu 	siginfo_t siginfo;
14909dde3bc9SDavid Xu 	int error;
14911bf4700bSJeff Roberson 
14921bf4700bSJeff Roberson 	p = td->td_proc;
14939dde3bc9SDavid Xu 	if (td->td_flags & TDF_SA) {
14947eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
14959dde3bc9SDavid Xu 			thread_user_enter(p, td);
1496628d2653SJohn Baldwin 		PROC_LOCK(p);
14979dde3bc9SDavid Xu 		if (td->td_mailbox) {
14989dde3bc9SDavid Xu 			SIGDELSET(td->td_sigmask, sig);
14999dde3bc9SDavid Xu 			mtx_lock_spin(&sched_lock);
15009dde3bc9SDavid Xu 			/*
15019dde3bc9SDavid Xu 			 * Force scheduling an upcall, so UTS has chance to
15029dde3bc9SDavid Xu 			 * process the signal before thread runs again in
15039dde3bc9SDavid Xu 			 * userland.
15049dde3bc9SDavid Xu 			 */
15059dde3bc9SDavid Xu 			if (td->td_upcall)
15069dde3bc9SDavid Xu 				td->td_upcall->ku_flags |= KUF_DOUPCALL;
15079dde3bc9SDavid Xu 			mtx_unlock_spin(&sched_lock);
1508432b45deSDavid Xu 		} else {
1509432b45deSDavid Xu 			/* UTS caused a sync signal */
15101fc434dcSDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
15111fc434dcSDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
15121fc434dcSDavid Xu 			sigexit(td, sig);
15139dde3bc9SDavid Xu 		}
15149dde3bc9SDavid Xu 	} else {
15159dde3bc9SDavid Xu 		PROC_LOCK(p);
15169dde3bc9SDavid Xu 	}
1517ef3dab76STim J. Robbins 	ps = p->p_sigacts;
151890af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
151990af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15204093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1521df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1522df8bae1dSRodney W. Grimes #ifdef KTRACE
1523374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1524374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15254093529dSJeff Roberson 			    &td->td_sigmask, code);
1526df8bae1dSRodney W. Grimes #endif
15279dde3bc9SDavid Xu 		if (!(td->td_flags & TDF_SA))
15289dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15299dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15304093529dSJeff Roberson 				&td->td_sigmask, code);
15319dde3bc9SDavid Xu 		else {
15324b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15339dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
15349dde3bc9SDavid Xu 			PROC_UNLOCK(p);
15359dde3bc9SDavid Xu 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
15369dde3bc9SDavid Xu 			    sizeof(siginfo));
15379dde3bc9SDavid Xu 			PROC_LOCK(p);
1538432b45deSDavid Xu 			/* UTS memory corrupted */
15399dde3bc9SDavid Xu 			if (error)
15409dde3bc9SDavid Xu 				sigexit(td, SIGILL);
15419dde3bc9SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
15429dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15439dde3bc9SDavid Xu 		}
15444093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15452c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15464093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15472c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1548289ccde0SPeter Wemm 			/*
15498f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1550289ccde0SPeter Wemm 			 */
155190af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15522c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15532c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
155490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15552c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1556dedc04feSPeter Wemm 		}
155790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15586f841fb7SMarcel Moolenaar 	} else {
155990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15606626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15612c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1562c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1563df8bae1dSRodney W. Grimes 	}
1564628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1565df8bae1dSRodney W. Grimes }
1566df8bae1dSRodney W. Grimes 
15674093529dSJeff Roberson static struct thread *
15684093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15694093529dSJeff Roberson {
15703074d1b4SDavid Xu 	struct thread *td, *signal_td;
15714093529dSJeff Roberson 
15724093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15734093529dSJeff Roberson 
15744093529dSJeff Roberson 	/*
15753074d1b4SDavid Xu 	 * First find a thread in sigwait state and signal belongs to
15763074d1b4SDavid Xu 	 * its wait set. POSIX's arguments is that speed of delivering signal
15773074d1b4SDavid Xu 	 * to sigwait thread is faster than delivering signal to user stack.
15783074d1b4SDavid Xu 	 * If we can not find sigwait thread, then find the first thread in
15793074d1b4SDavid Xu 	 * the proc that doesn't have this signal masked, an exception is
15803074d1b4SDavid Xu 	 * if current thread is sending signal to its process, and it does not
15813074d1b4SDavid Xu 	 * mask the signal, it should get the signal, this is another fast
15823074d1b4SDavid Xu 	 * way to deliver signal.
15834093529dSJeff Roberson 	 */
15843074d1b4SDavid Xu 	signal_td = NULL;
1585a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15863074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15873074d1b4SDavid Xu 		if (td->td_waitset != NULL &&
1588a9a48d68SDavid Xu 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1589a9a48d68SDavid Xu 				mtx_unlock_spin(&sched_lock);
15904093529dSJeff Roberson 				return (td);
1591a9a48d68SDavid Xu 		}
15923074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15933074d1b4SDavid Xu 			if (td == curthread)
15943074d1b4SDavid Xu 				signal_td = curthread;
15953074d1b4SDavid Xu 			else if (signal_td == NULL)
15963074d1b4SDavid Xu 				signal_td = td;
15973074d1b4SDavid Xu 		}
15983074d1b4SDavid Xu 	}
15993074d1b4SDavid Xu 	if (signal_td == NULL)
16003074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1601a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
16023074d1b4SDavid Xu 	return (signal_td);
16034093529dSJeff Roberson }
16044093529dSJeff Roberson 
1605df8bae1dSRodney W. Grimes /*
1606df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1607df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1608df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1609df8bae1dSRodney W. Grimes  *
1610df8bae1dSRodney W. Grimes  * Exceptions:
1611df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1612df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1613df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1614df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1615df8bae1dSRodney W. Grimes  *
1616df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
161790af4afaSJohn Baldwin  *
161890af4afaSJohn Baldwin  * MPSAFE
1619df8bae1dSRodney W. Grimes  */
1620df8bae1dSRodney W. Grimes void
16214093529dSJeff Roberson psignal(struct proc *p, int sig)
1622df8bae1dSRodney W. Grimes {
1623b40ce416SJulian Elischer 	struct thread *td;
16244093529dSJeff Roberson 	int prop;
16254093529dSJeff Roberson 
162641b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
162741b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
162841b3077aSJacques Vidrine 
16294093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
16304093529dSJeff Roberson 	prop = sigprop(sig);
16314093529dSJeff Roberson 
16324093529dSJeff Roberson 	/*
16334093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16344093529dSJeff Roberson 	 */
16354093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16364093529dSJeff Roberson 
1637c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16384093529dSJeff Roberson }
16394093529dSJeff Roberson 
164090af4afaSJohn Baldwin /*
164190af4afaSJohn Baldwin  * MPSAFE
164290af4afaSJohn Baldwin  */
16434093529dSJeff Roberson void
1644c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16454093529dSJeff Roberson {
16469dde3bc9SDavid Xu 	sigset_t saved;
16479dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16489dde3bc9SDavid Xu 
16499dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16509dde3bc9SDavid Xu 		saved = p->p_siglist;
1651c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16529dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16539dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
16549dde3bc9SDavid Xu 			return;
16559dde3bc9SDavid Xu 		else {
16569dde3bc9SDavid Xu 			/* pending set changed */
16579dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
16589dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
16599dde3bc9SDavid Xu 		}
16609dde3bc9SDavid Xu 	}
16619dde3bc9SDavid Xu }
16629dde3bc9SDavid Xu 
16639dde3bc9SDavid Xu static void
1664c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target)
16659dde3bc9SDavid Xu {
16664093529dSJeff Roberson 	struct proc *p;
16674093529dSJeff Roberson 	register sig_t action;
16684093529dSJeff Roberson 	sigset_t *siglist;
16694093529dSJeff Roberson 	struct thread *td0;
1670e602ba25SJulian Elischer 	register int prop;
167190af4afaSJohn Baldwin 	struct sigacts *ps;
1672df8bae1dSRodney W. Grimes 
167341b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
167441b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
16754093529dSJeff Roberson 
16764093529dSJeff Roberson 	p = td->td_proc;
167790af4afaSJohn Baldwin 	ps = p->p_sigacts;
16782c42a146SMarcel Moolenaar 
1679628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1680cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1681cb679c38SJonathan Lemon 
16822c42a146SMarcel Moolenaar 	prop = sigprop(sig);
16834093529dSJeff Roberson 
16844093529dSJeff Roberson 	/*
168514b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
168614b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
168714b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
16884093529dSJeff Roberson 	 */
16893074d1b4SDavid Xu 	if (target == SIGTARGET_TD) {
16903074d1b4SDavid Xu 		siglist = &td->td_siglist;
16913074d1b4SDavid Xu 	} else {
16923074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig))
16933074d1b4SDavid Xu 			siglist = &td->td_siglist;
16943074d1b4SDavid Xu 		else if (td->td_waitset != NULL &&
16953074d1b4SDavid Xu 			SIGISMEMBER(*(td->td_waitset), sig))
16963074d1b4SDavid Xu 			siglist = &td->td_siglist;
16973074d1b4SDavid Xu 		else
16983074d1b4SDavid Xu 			siglist = &p->p_siglist;
16993074d1b4SDavid Xu 	}
17004093529dSJeff Roberson 
1701df8bae1dSRodney W. Grimes 	/*
17022a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
17032a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
17042a024a2bSSean Eric Fagan 	 * a chance, as well.
1705df8bae1dSRodney W. Grimes 	 */
1706b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1707df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1708b40ce416SJulian Elischer 	} else {
1709df8bae1dSRodney W. Grimes 		/*
1710df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1711df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
171290af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1713df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1714df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1715df8bae1dSRodney W. Grimes 		 */
171690af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
171790af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
171890af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
171990af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1720df8bae1dSRodney W. Grimes 			return;
172190af4afaSJohn Baldwin 		}
17223074d1b4SDavid Xu 		if (((td->td_waitset == NULL) &&
17233074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig)) ||
17243074d1b4SDavid Xu 		    ((td->td_waitset != NULL) &&
17253074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig) &&
17263074d1b4SDavid Xu 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1727df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
172890af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1729df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1730df8bae1dSRodney W. Grimes 		else
1731df8bae1dSRodney W. Grimes 			action = SIG_DFL;
173290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1733df8bae1dSRodney W. Grimes 	}
1734df8bae1dSRodney W. Grimes 
17354093529dSJeff Roberson 	if (prop & SA_CONT) {
17361d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17374093529dSJeff Roberson 		/*
17384093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17394093529dSJeff Roberson 		 * the proc's siglist.
17404093529dSJeff Roberson 		 */
1741a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17424093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17434093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1744a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17454093529dSJeff Roberson 	}
1746df8bae1dSRodney W. Grimes 
1747df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1748df8bae1dSRodney W. Grimes 		/*
1749df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1750df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1751df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1752df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1753df8bae1dSRodney W. Grimes 		 */
1754e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1755e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1756e602ba25SJulian Elischer 		    (action == SIG_DFL))
1757df8bae1dSRodney W. Grimes 		        return;
17581d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1759a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17604093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17614093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1762a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17636933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1764df8bae1dSRodney W. Grimes 	}
17653074d1b4SDavid Xu 
17664093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17674093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17683074d1b4SDavid Xu 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
17693074d1b4SDavid Xu 	    action != SIG_HOLD) {
17703074d1b4SDavid Xu 		td->td_waitset = NULL;
17713074d1b4SDavid Xu 	}
17723074d1b4SDavid Xu 
17735312b1c7SDavid Xu 	/*
17745312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17755312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17765312b1c7SDavid Xu 	 */
17775312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17785312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17795312b1c7SDavid Xu 		return;
1780df8bae1dSRodney W. Grimes 	/*
1781e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1782e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1783e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1784e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1785e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1786e602ba25SJulian Elischer 	 * We try do the per-process part here.
1787df8bae1dSRodney W. Grimes 	 */
1788e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1789e602ba25SJulian Elischer 		/*
1790e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1791e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1792e602ba25SJulian Elischer 		 */
1793e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1794e602ba25SJulian Elischer 			/*
1795e602ba25SJulian Elischer 			 * The traced process is already stopped,
1796e602ba25SJulian Elischer 			 * so no further action is necessary.
1797e602ba25SJulian Elischer 			 * No signal can restart us.
1798e602ba25SJulian Elischer 			 */
1799e602ba25SJulian Elischer 			goto out;
18001c32c37cSJohn Baldwin 		}
1801b40ce416SJulian Elischer 
1802e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1803df8bae1dSRodney W. Grimes 			/*
1804e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1805e602ba25SJulian Elischer 			 * It will die elsewhere.
1806e602ba25SJulian Elischer 			 * All threads must be restarted.
1807df8bae1dSRodney W. Grimes 			 */
1808e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1809e602ba25SJulian Elischer 			goto runfast;
1810e602ba25SJulian Elischer 		}
1811e602ba25SJulian Elischer 
1812e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1813e602ba25SJulian Elischer 			/*
1814e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18154093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18161d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1817e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18184093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1819e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1820e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1821e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1822e602ba25SJulian Elischer 			 */
18231279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18246933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1825e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18264093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1827e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1828e602ba25SJulian Elischer 				/*
1829e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1830e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1831e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1832e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1833e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1834e602ba25SJulian Elischer 				 * process, we need to make sure that the
1835e602ba25SJulian Elischer 				 * single thread is runnable asap.
1836e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1837e602ba25SJulian Elischer 				 */
1838e602ba25SJulian Elischer 				goto runfast;
1839e602ba25SJulian Elischer 			}
1840e602ba25SJulian Elischer 			/*
1841e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1842e602ba25SJulian Elischer 			 */
1843e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
184404774f23SJulian Elischer 			thread_unsuspend(p);
1845e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1846e602ba25SJulian Elischer 			goto out;
1847e602ba25SJulian Elischer 		}
1848e602ba25SJulian Elischer 
1849e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1850e602ba25SJulian Elischer 			/*
1851e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1852e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
185304774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1854e602ba25SJulian Elischer 			 */
18551279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18564093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1857e602ba25SJulian Elischer 			goto out;
1858e602ba25SJulian Elischer 		}
1859e602ba25SJulian Elischer 
1860e602ba25SJulian Elischer 		/*
1861e602ba25SJulian Elischer 		 * All other kinds of signals:
1862e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1863e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1864e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
186504774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1866e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1867e602ba25SJulian Elischer 		 */
1868e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
186944f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
187044f3b092SJohn Baldwin 			sleepq_abort(td);
1871e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1872df8bae1dSRodney W. Grimes 		goto out;
1873df8bae1dSRodney W. Grimes 		/*
18749a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
18759a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
1876df8bae1dSRodney W. Grimes 		 */
1877e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
187835c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
187935c32a76SDavid Xu 			!(prop & SA_STOP)) {
1880721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18814093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1882721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1883df8bae1dSRodney W. Grimes 			goto out;
188404774f23SJulian Elischer 		}
188535c32a76SDavid Xu 		if (prop & SA_STOP) {
188635c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
188735c32a76SDavid Xu 				goto out;
1888e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1889e574e444SDavid Xu 			p->p_xstat = sig;
189035c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
18914093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
18924093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1893062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1894062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
18954093529dSJeff Roberson 					thread_suspend_one(td0);
1896062cf543SDavid Xu 				} else if (td != td0) {
1897062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1898062cf543SDavid Xu 				}
189935c32a76SDavid Xu 			}
1900e574e444SDavid Xu 			thread_stopped(p);
19014093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1902e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
19034093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
19044093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
19054093529dSJeff Roberson 			}
190635c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
190735c32a76SDavid Xu 			goto out;
190835c32a76SDavid Xu 		}
1909721e5910SJulian Elischer 		else
1910df8bae1dSRodney W. Grimes 			goto runfast;
1911df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1912e602ba25SJulian Elischer 	} else {
1913e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19144093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1915e602ba25SJulian Elischer 		goto out;
1916e602ba25SJulian Elischer 	}
1917e602ba25SJulian Elischer 
1918b40ce416SJulian Elischer 	/*
1919e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1920e602ba25SJulian Elischer 	 * running threads.
1921b40ce416SJulian Elischer 	 */
1922e602ba25SJulian Elischer 
1923e602ba25SJulian Elischer runfast:
1924aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19254093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1926e602ba25SJulian Elischer 	thread_unsuspend(p);
1927e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1928e602ba25SJulian Elischer out:
1929e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1930e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1931e602ba25SJulian Elischer }
1932e602ba25SJulian Elischer 
1933e602ba25SJulian Elischer /*
1934e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1935e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
1936e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1937e602ba25SJulian Elischer  */
1938e602ba25SJulian Elischer static void
19394093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1940e602ba25SJulian Elischer {
1941e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1942e602ba25SJulian Elischer 	register int prop;
1943e602ba25SJulian Elischer 
19448b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1945aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1946e602ba25SJulian Elischer 	prop = sigprop(sig);
1947a4c2da15SBruce Evans 
1948e602ba25SJulian Elischer 	/*
1949aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1950e602ba25SJulian Elischer 	 * killed in this lifetime.
1951e602ba25SJulian Elischer 	 */
1952a4c2da15SBruce Evans 	if (action == SIG_DFL && (prop & SA_KILL)) {
1953a4c2da15SBruce Evans 		if (td->td_priority > PUSER)
1954e602ba25SJulian Elischer 			td->td_priority = PUSER;
1955b40ce416SJulian Elischer 	}
1956a4c2da15SBruce Evans 
195780c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
1958e602ba25SJulian Elischer 		/*
1959e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1960e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1961e602ba25SJulian Elischer 		 * be noticed when the process returns through
1962e602ba25SJulian Elischer 		 * trap() or syscall().
1963e602ba25SJulian Elischer 		 */
196444f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1965aa0fa334SJulian Elischer 			return;
1966e602ba25SJulian Elischer 		/*
1967e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1968e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1969e602ba25SJulian Elischer 		 * for its parent.
1970e602ba25SJulian Elischer 		 */
1971e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1972e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1973aa0fa334SJulian Elischer 		} else {
1974df8bae1dSRodney W. Grimes 			/*
1975e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1976e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1977e602ba25SJulian Elischer 			 * be awakened.
1978df8bae1dSRodney W. Grimes 			 */
1979e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19801d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19814093529dSJeff Roberson 				/*
19824093529dSJeff Roberson 				 * It may be on either list in this state.
19834093529dSJeff Roberson 				 * Remove from both for now.
19844093529dSJeff Roberson 				 */
19854093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1986aa0fa334SJulian Elischer 				return;
1987df8bae1dSRodney W. Grimes 			}
1988df8bae1dSRodney W. Grimes 
1989aa0fa334SJulian Elischer 			/*
1990a4c2da15SBruce Evans 			 * Give low priority threads a better chance to run.
1991aa0fa334SJulian Elischer 			 */
199244f3b092SJohn Baldwin 			if (td->td_priority > PUSER)
1993aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1994aa0fa334SJulian Elischer 		}
199544f3b092SJohn Baldwin 		sleepq_abort(td);
1996a4c2da15SBruce Evans 	} else {
1997df8bae1dSRodney W. Grimes 		/*
1998a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
1999df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2000df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2001df8bae1dSRodney W. Grimes 		 */
2002a4c2da15SBruce Evans #ifdef SMP
200344f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
2004e602ba25SJulian Elischer 			forward_signal(td);
20053163861cSTor Egge #endif
20066caa8a15SJohn Baldwin 	}
2007a4c2da15SBruce Evans }
2008df8bae1dSRodney W. Grimes 
20094cc9f52fSRobert Drehmel void
20104cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
20114cc9f52fSRobert Drehmel {
20124cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
20134cc9f52fSRobert Drehmel 
201430a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20154cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
20164cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
20174cc9f52fSRobert Drehmel 
20184cc9f52fSRobert Drehmel 	p->p_xstat = sig;
20194cc9f52fSRobert Drehmel 	PROC_LOCK(p->p_pptr);
20204cc9f52fSRobert Drehmel 	psignal(p->p_pptr, SIGCHLD);
20214cc9f52fSRobert Drehmel 	PROC_UNLOCK(p->p_pptr);
20224a3b3dcbSColin Percival 	stop(p);
20234cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
20244cc9f52fSRobert Drehmel 	thread_suspend_one(td);
20254cc9f52fSRobert Drehmel 	PROC_UNLOCK(p);
20264cc9f52fSRobert Drehmel 	DROP_GIANT();
202729bcc451SJeff Roberson 	mi_switch(SW_INVOL);
20284cc9f52fSRobert Drehmel 	mtx_unlock_spin(&sched_lock);
20294cc9f52fSRobert Drehmel 	PICKUP_GIANT();
20304cc9f52fSRobert Drehmel }
20314cc9f52fSRobert Drehmel 
2032df8bae1dSRodney W. Grimes /*
2033df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2034df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2035df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2036df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2037df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2038628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2039df8bae1dSRodney W. Grimes  * sequence is
2040df8bae1dSRodney W. Grimes  *
2041e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
20422c42a146SMarcel Moolenaar  *		postsig(sig);
2043df8bae1dSRodney W. Grimes  */
20446711f10fSJohn Baldwin static int
2045e602ba25SJulian Elischer issignal(td)
2046e602ba25SJulian Elischer 	struct thread *td;
2047df8bae1dSRodney W. Grimes {
2048e602ba25SJulian Elischer 	struct proc *p;
204990af4afaSJohn Baldwin 	struct sigacts *ps;
20504093529dSJeff Roberson 	sigset_t sigpending;
2051062cf543SDavid Xu 	int sig, prop;
2052062cf543SDavid Xu 	struct thread *td0;
2053df8bae1dSRodney W. Grimes 
2054e602ba25SJulian Elischer 	p = td->td_proc;
205590af4afaSJohn Baldwin 	ps = p->p_sigacts;
205690af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2057628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2058df8bae1dSRodney W. Grimes 	for (;;) {
20592a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
20602a024a2bSSean Eric Fagan 
20614093529dSJeff Roberson 		sigpending = td->td_siglist;
20624093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
20634093529dSJeff Roberson 
2064df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
20654093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
20664093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2067df8bae1dSRodney W. Grimes 			return (0);
20684093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
20692a024a2bSSean Eric Fagan 
2070047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2071047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2072047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2073047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2074047aa39bSRobert Watson 		}
20752a024a2bSSean Eric Fagan 
2076df8bae1dSRodney W. Grimes 		/*
2077df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2078df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2079df8bae1dSRodney W. Grimes 		 */
208090af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
20814093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
2082df8bae1dSRodney W. Grimes 			continue;
2083df8bae1dSRodney W. Grimes 		}
2084df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2085df8bae1dSRodney W. Grimes 			/*
2086d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2087df8bae1dSRodney W. Grimes 			 */
208890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
20894cc9f52fSRobert Drehmel 			ptracestop(td, sig);
2090628d2653SJohn Baldwin 			PROC_LOCK(p);
209190af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2092df8bae1dSRodney W. Grimes 
2093df8bae1dSRodney W. Grimes 			/*
2094df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
2095df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
2096df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
2097df8bae1dSRodney W. Grimes 			 */
20984093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
20992c42a146SMarcel Moolenaar 			sig = p->p_xstat;
21002c42a146SMarcel Moolenaar 			if (sig == 0)
2101df8bae1dSRodney W. Grimes 				continue;
2102df8bae1dSRodney W. Grimes 
2103df8bae1dSRodney W. Grimes 			/*
21048d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
21058d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
21068d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
21078d542cb5SDavid E. O'Brien 			 */
21088d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
21098d542cb5SDavid E. O'Brien 				continue;
21108d542cb5SDavid E. O'Brien 
21118d542cb5SDavid E. O'Brien 			/*
21124093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
21131d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
2114df8bae1dSRodney W. Grimes 			 */
21154093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
21164093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
2117df8bae1dSRodney W. Grimes 				continue;
21184093529dSJeff Roberson 			signotify(td);
2119df8bae1dSRodney W. Grimes 		}
2120df8bae1dSRodney W. Grimes 
21218d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
21228d542cb5SDavid E. O'Brien 
2123df8bae1dSRodney W. Grimes 		/*
2124df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2125df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2126df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2127df8bae1dSRodney W. Grimes 		 */
2128d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2129df8bae1dSRodney W. Grimes 
2130d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2131df8bae1dSRodney W. Grimes 			/*
2132df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2133df8bae1dSRodney W. Grimes 			 */
2134df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2135df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2136df8bae1dSRodney W. Grimes 				/*
2137df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2138df8bae1dSRodney W. Grimes 				 * in init? XXX
2139df8bae1dSRodney W. Grimes 				 */
2140d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
21412c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2142df8bae1dSRodney W. Grimes #endif
2143df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2144df8bae1dSRodney W. Grimes 			}
2145df8bae1dSRodney W. Grimes 			/*
2146df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2147df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2148df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2149df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2150df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2151df8bae1dSRodney W. Grimes 			 */
2152df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2153df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2154df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2155df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2156df8bae1dSRodney W. Grimes 					break;	/* == ignore */
215790af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
215890af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
215990af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2160e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
21612c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2162721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2163062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2164062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2165062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2166062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2167062cf543SDavid Xu 						thread_suspend_one(td0);
2168062cf543SDavid Xu 					} else if (td != td0) {
2169062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2170062cf543SDavid Xu 					}
2171062cf543SDavid Xu 				}
2172e574e444SDavid Xu 				thread_stopped(p);
217371fad9fdSJulian Elischer 				thread_suspend_one(td);
2174721e5910SJulian Elischer 				PROC_UNLOCK(p);
2175721e5910SJulian Elischer 				DROP_GIANT();
217629bcc451SJeff Roberson 				mi_switch(SW_INVOL);
2177721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2178721e5910SJulian Elischer 				PICKUP_GIANT();
2179721e5910SJulian Elischer 				PROC_LOCK(p);
218090af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2181df8bae1dSRodney W. Grimes 				break;
218221b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2183df8bae1dSRodney W. Grimes 				/*
2184df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2185df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2186df8bae1dSRodney W. Grimes 				 */
2187df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2188df8bae1dSRodney W. Grimes 			} else
21892c42a146SMarcel Moolenaar 				return (sig);
2190df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2191df8bae1dSRodney W. Grimes 
2192d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2193df8bae1dSRodney W. Grimes 			/*
2194df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2195df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2196df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2197df8bae1dSRodney W. Grimes 			 */
2198df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2199df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2200df8bae1dSRodney W. Grimes 				printf("issignal\n");
2201df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2202df8bae1dSRodney W. Grimes 
2203df8bae1dSRodney W. Grimes 		default:
2204df8bae1dSRodney W. Grimes 			/*
2205df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2206df8bae1dSRodney W. Grimes 			 * postsig() process it.
2207df8bae1dSRodney W. Grimes 			 */
22082c42a146SMarcel Moolenaar 			return (sig);
2209df8bae1dSRodney W. Grimes 		}
22104093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2211df8bae1dSRodney W. Grimes 	}
2212df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2213df8bae1dSRodney W. Grimes }
2214df8bae1dSRodney W. Grimes 
2215df8bae1dSRodney W. Grimes /*
2216df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2217df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
22184a3b3dcbSColin Percival  * on the run queue.  Must be called with the proc p locked.
2219df8bae1dSRodney W. Grimes  */
22205b3047d5SJohn Baldwin static void
2221e574e444SDavid Xu stop(struct proc *p)
2222df8bae1dSRodney W. Grimes {
2223df8bae1dSRodney W. Grimes 
2224628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22251279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2226df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
222701609114SAlfred Perlstein 	wakeup(p->p_pptr);
2228df8bae1dSRodney W. Grimes }
2229df8bae1dSRodney W. Grimes 
223090af4afaSJohn Baldwin /*
223190af4afaSJohn Baldwin  * MPSAFE
223290af4afaSJohn Baldwin  */
2233e574e444SDavid Xu void
2234e574e444SDavid Xu thread_stopped(struct proc *p)
2235e574e444SDavid Xu {
2236e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
223790af4afaSJohn Baldwin 	struct sigacts *ps;
2238e574e444SDavid Xu 	int n;
2239e574e444SDavid Xu 
2240e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2241e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2242e574e444SDavid Xu 	n = p->p_suspcount;
2243e574e444SDavid Xu 	if (p == p1)
2244e574e444SDavid Xu 		n++;
2245e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2246e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2247e574e444SDavid Xu 		stop(p);
2248e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
224990af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
225090af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
225190af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
225290af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2253e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
225490af4afaSJohn Baldwin 		} else
225590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2256e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2257e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2258e574e444SDavid Xu 	}
2259e574e444SDavid Xu }
2260e574e444SDavid Xu 
2261df8bae1dSRodney W. Grimes /*
2262df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2263df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2264df8bae1dSRodney W. Grimes  */
2265df8bae1dSRodney W. Grimes void
22662c42a146SMarcel Moolenaar postsig(sig)
22672c42a146SMarcel Moolenaar 	register int sig;
2268df8bae1dSRodney W. Grimes {
2269b40ce416SJulian Elischer 	struct thread *td = curthread;
2270b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2271628d2653SJohn Baldwin 	struct sigacts *ps;
22722c42a146SMarcel Moolenaar 	sig_t action;
22732c42a146SMarcel Moolenaar 	sigset_t returnmask;
22742c42a146SMarcel Moolenaar 	int code;
2275df8bae1dSRodney W. Grimes 
22762c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
22775526d2d9SEivind Eklund 
22782ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2279628d2653SJohn Baldwin 	ps = p->p_sigacts;
228090af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
22814093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
22822c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2283df8bae1dSRodney W. Grimes #ifdef KTRACE
2284374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
22855e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
22864093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2287df8bae1dSRodney W. Grimes #endif
2288047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2289047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2290047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2291047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2292047aa39bSRobert Watson 	}
22932a024a2bSSean Eric Fagan 
2294432b45deSDavid Xu 	if (!(td->td_flags & TDF_SA && td->td_mailbox) &&
2295432b45deSDavid Xu 	    action == SIG_DFL) {
2296df8bae1dSRodney W. Grimes 		/*
2297df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2298df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2299df8bae1dSRodney W. Grimes 		 */
230090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2301b40ce416SJulian Elischer 		sigexit(td, sig);
2302df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2303df8bae1dSRodney W. Grimes 	} else {
2304432b45deSDavid Xu 		if (td->td_flags & TDF_SA && td->td_mailbox) {
2305432b45deSDavid Xu 			if (sig == SIGKILL) {
2306432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2307432b45deSDavid Xu 				sigexit(td, sig);
2308432b45deSDavid Xu 			}
2309432b45deSDavid Xu 		}
2310432b45deSDavid Xu 
2311df8bae1dSRodney W. Grimes 		/*
2312df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2313df8bae1dSRodney W. Grimes 		 */
23144093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23155526d2d9SEivind Eklund 		    ("postsig action"));
2316df8bae1dSRodney W. Grimes 		/*
2317df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2318645682fdSLuoqi Chen 		 * occurrences of this signal.
2319df8bae1dSRodney W. Grimes 		 *
2320645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2321df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2322645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2323df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2324df8bae1dSRodney W. Grimes 		 */
23255e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23264093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23275e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2328df8bae1dSRodney W. Grimes 		} else
23294093529dSJeff Roberson 			returnmask = td->td_sigmask;
23302c42a146SMarcel Moolenaar 
23314093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23322c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23334093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23342c42a146SMarcel Moolenaar 
23352c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2336289ccde0SPeter Wemm 			/*
23378f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2338289ccde0SPeter Wemm 			 */
233990af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23402c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23412c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
234290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23432c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2344dedc04feSPeter Wemm 		}
2345df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23462c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2347df8bae1dSRodney W. Grimes 			code = 0;
2348df8bae1dSRodney W. Grimes 		} else {
23496626c604SJulian Elischer 			code = p->p_code;
23506626c604SJulian Elischer 			p->p_code = 0;
23516626c604SJulian Elischer 			p->p_sig = 0;
2352df8bae1dSRodney W. Grimes 		}
2353432b45deSDavid Xu 		if (td->td_flags & TDF_SA && td->td_mailbox)
235458a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
235558a3c273SJeff Roberson 		else
235658a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
235758a3c273SJeff Roberson 			    &returnmask, code);
2358df8bae1dSRodney W. Grimes 	}
2359df8bae1dSRodney W. Grimes }
2360df8bae1dSRodney W. Grimes 
2361df8bae1dSRodney W. Grimes /*
2362df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2363df8bae1dSRodney W. Grimes  */
236426f9a767SRodney W. Grimes void
2365df8bae1dSRodney W. Grimes killproc(p, why)
2366df8bae1dSRodney W. Grimes 	struct proc *p;
2367df8bae1dSRodney W. Grimes 	char *why;
2368df8bae1dSRodney W. Grimes {
23699081e5e8SJohn Baldwin 
23709081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
23710384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
23720384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2373729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2374b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2375df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2376df8bae1dSRodney W. Grimes }
2377df8bae1dSRodney W. Grimes 
2378df8bae1dSRodney W. Grimes /*
2379df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2380df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2381df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2382df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2383df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2384df8bae1dSRodney W. Grimes  * does not return.
238590af4afaSJohn Baldwin  *
238690af4afaSJohn Baldwin  * MPSAFE
2387df8bae1dSRodney W. Grimes  */
238826f9a767SRodney W. Grimes void
2389b40ce416SJulian Elischer sigexit(td, sig)
2390b40ce416SJulian Elischer 	struct thread *td;
23912c42a146SMarcel Moolenaar 	int sig;
2392df8bae1dSRodney W. Grimes {
2393b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2394df8bae1dSRodney W. Grimes 
2395628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2396df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
23972c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
23982c42a146SMarcel Moolenaar 		p->p_sig = sig;
2399c364e17eSAndrey A. Chernov 		/*
2400c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2401c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2402c364e17eSAndrey A. Chernov 		 * these messages.)
2403c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
24044ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2405c364e17eSAndrey A. Chernov 		 */
2406b40ce416SJulian Elischer 		if (coredump(td) == 0)
24072c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
240857308494SJoerg Wunsch 		if (kern_logsigexit)
240957308494SJoerg Wunsch 			log(LOG_INFO,
241057308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24113d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24129c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24132c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24142c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
24154ae89b95SJohn Baldwin 	} else
2416628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2417b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2418df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2419df8bae1dSRodney W. Grimes }
2420df8bae1dSRodney W. Grimes 
2421c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2422c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2423c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2424c5edb423SSean Eric Fagan 
2425c5edb423SSean Eric Fagan /*
2426c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2427c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2428c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2429c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2430c5edb423SSean Eric Fagan  *	%P	process id (pid)
2431c5edb423SSean Eric Fagan  *	%U	user id (uid)
2432c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2433c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2434c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2435c5edb423SSean Eric Fagan  */
2436c5edb423SSean Eric Fagan 
2437fca666a1SJulian Elischer static char *
2438c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24398b43b535SAlfred Perlstein 	const char *name;
24408b43b535SAlfred Perlstein 	uid_t uid;
24418b43b535SAlfred Perlstein 	pid_t pid;
24428b43b535SAlfred Perlstein {
24438b43b535SAlfred Perlstein 	const char *format, *appendstr;
2444c5edb423SSean Eric Fagan 	char *temp;
2445c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24468b43b535SAlfred Perlstein 	size_t i, l, n;
2447c5edb423SSean Eric Fagan 
24488b43b535SAlfred Perlstein 	format = corefilename;
24498b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24500bfe2990SEivind Eklund 	if (temp == NULL)
24518b43b535SAlfred Perlstein 		return (NULL);
2452ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2453c5edb423SSean Eric Fagan 		switch (format[i]) {
2454c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2455c5edb423SSean Eric Fagan 			i++;
2456c5edb423SSean Eric Fagan 			switch (format[i]) {
2457c5edb423SSean Eric Fagan 			case '%':
24588b43b535SAlfred Perlstein 				appendstr = "%";
2459c5edb423SSean Eric Fagan 				break;
2460c5edb423SSean Eric Fagan 			case 'N':	/* process name */
24618b43b535SAlfred Perlstein 				appendstr = name;
2462c5edb423SSean Eric Fagan 				break;
2463c5edb423SSean Eric Fagan 			case 'P':	/* process id */
24648b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
24658b43b535SAlfred Perlstein 				appendstr = buf;
2466c5edb423SSean Eric Fagan 				break;
2467c5edb423SSean Eric Fagan 			case 'U':	/* user id */
24688b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
24698b43b535SAlfred Perlstein 				appendstr = buf;
2470c5edb423SSean Eric Fagan 				break;
2471c5edb423SSean Eric Fagan 			default:
24728b43b535SAlfred Perlstein 				appendstr = "";
24738b43b535SAlfred Perlstein 			  	log(LOG_ERR,
24748b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
24758b43b535SAlfred Perlstein 				    format[i], format);
2476c5edb423SSean Eric Fagan 			}
24778b43b535SAlfred Perlstein 			l = strlen(appendstr);
24788b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
24798b43b535SAlfred Perlstein 				goto toolong;
24808b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
24818b43b535SAlfred Perlstein 			n += l;
2482c5edb423SSean Eric Fagan 			break;
2483c5edb423SSean Eric Fagan 		default:
2484c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2485c5edb423SSean Eric Fagan 		}
2486c5edb423SSean Eric Fagan 	}
24878b43b535SAlfred Perlstein 	if (format[i] != '\0')
24888b43b535SAlfred Perlstein 		goto toolong;
24898b43b535SAlfred Perlstein 	return (temp);
24908b43b535SAlfred Perlstein toolong:
24918b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
24928b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
24938b43b535SAlfred Perlstein 	free(temp, M_TEMP);
24948b43b535SAlfred Perlstein 	return (NULL);
2495c5edb423SSean Eric Fagan }
2496c5edb423SSean Eric Fagan 
2497df8bae1dSRodney W. Grimes /*
2498fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2499fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2500fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2501fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2502fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2503fca666a1SJulian Elischer  */
2504fca666a1SJulian Elischer 
2505fca666a1SJulian Elischer static int
2506b40ce416SJulian Elischer coredump(struct thread *td)
2507fca666a1SJulian Elischer {
2508b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2509fca666a1SJulian Elischer 	register struct vnode *vp;
25109c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
251106ae1e91SMatthew Dillon 	struct flock lf;
2512fca666a1SJulian Elischer 	struct nameidata nd;
2513fca666a1SJulian Elischer 	struct vattr vattr;
2514c447f5b2SRobert Watson 	int error, error1, flags, locked;
2515f2a2857bSKirk McKusick 	struct mount *mp;
2516fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2517fca666a1SJulian Elischer 	off_t limit;
2518fca666a1SJulian Elischer 
25194ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2520628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2521fca666a1SJulian Elischer 
2522628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2523628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2524fca666a1SJulian Elischer 		return (EFAULT);
2525628d2653SJohn Baldwin 	}
2526fca666a1SJulian Elischer 
2527fca666a1SJulian Elischer 	/*
252835a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
252935a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
253035a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
253135a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
253235a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
253335a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2534fca666a1SJulian Elischer 	 */
253591d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2536628d2653SJohn Baldwin 	PROC_UNLOCK(p);
253791d5354aSJohn Baldwin 	if (limit == 0)
253891d5354aSJohn Baldwin 		return (EFBIG);
253935a2598fSSean Eric Fagan 
25404ae89b95SJohn Baldwin 	mtx_lock(&Giant);
2541f2a2857bSKirk McKusick restart:
25429c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
25434ae89b95SJohn Baldwin 	if (name == NULL) {
25444ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2545ccdbd10cSPeter Pentchev 		return (EINVAL);
25464ae89b95SJohn Baldwin 	}
2547b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2548e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25497c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2550fca666a1SJulian Elischer 	free(name, M_TEMP);
25514ae89b95SJohn Baldwin 	if (error) {
25524ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2553fca666a1SJulian Elischer 		return (error);
25544ae89b95SJohn Baldwin 	}
2555762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2556fca666a1SJulian Elischer 	vp = nd.ni_vp;
255706ae1e91SMatthew Dillon 
2558832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2559832dafadSDon Lewis 	if (vp->v_type != VREG ||
2560832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2561832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2562832dafadSDon Lewis 		error = EFAULT;
25634ae89b95SJohn Baldwin 		goto out;
2564832dafadSDon Lewis 	}
2565832dafadSDon Lewis 
2566b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
256706ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
256806ae1e91SMatthew Dillon 	lf.l_start = 0;
256906ae1e91SMatthew Dillon 	lf.l_len = 0;
257006ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2571c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
257206ae1e91SMatthew Dillon 
257306ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
257406ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
257536bbf86bSRobert Watson 		if (locked)
257606ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2577b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2578f2a2857bSKirk McKusick 			return (error);
2579f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2580f2a2857bSKirk McKusick 			return (error);
2581f2a2857bSKirk McKusick 		goto restart;
2582f2a2857bSKirk McKusick 	}
2583fca666a1SJulian Elischer 
2584fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2585fca666a1SJulian Elischer 	vattr.va_size = 0;
258688b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2587b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2588b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
258988b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2590628d2653SJohn Baldwin 	PROC_LOCK(p);
2591fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2592628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2593fca666a1SJulian Elischer 
2594fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2595b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2596fca666a1SJulian Elischer 	  ENOSYS;
2597fca666a1SJulian Elischer 
2598c447f5b2SRobert Watson 	if (locked) {
259906ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
260006ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2601c447f5b2SRobert Watson 	}
2602f2a2857bSKirk McKusick 	vn_finished_write(mp);
26034ae89b95SJohn Baldwin out:
2604b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
26054ae89b95SJohn Baldwin 	mtx_unlock(&Giant);
2606fca666a1SJulian Elischer 	if (error == 0)
2607fca666a1SJulian Elischer 		error = error1;
2608fca666a1SJulian Elischer 	return (error);
2609fca666a1SJulian Elischer }
2610fca666a1SJulian Elischer 
2611fca666a1SJulian Elischer /*
2612df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2613df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2614df8bae1dSRodney W. Grimes  */
2615d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2616df8bae1dSRodney W. Grimes struct nosys_args {
2617df8bae1dSRodney W. Grimes 	int	dummy;
2618df8bae1dSRodney W. Grimes };
2619d2d3e875SBruce Evans #endif
2620fb99ab88SMatthew Dillon /*
2621fb99ab88SMatthew Dillon  * MPSAFE
2622fb99ab88SMatthew Dillon  */
2623df8bae1dSRodney W. Grimes /* ARGSUSED */
262426f9a767SRodney W. Grimes int
2625b40ce416SJulian Elischer nosys(td, args)
2626b40ce416SJulian Elischer 	struct thread *td;
2627df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2628df8bae1dSRodney W. Grimes {
2629b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2630b40ce416SJulian Elischer 
2631628d2653SJohn Baldwin 	PROC_LOCK(p);
2632df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2633628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2634f5216b9aSBruce Evans 	return (ENOSYS);
2635df8bae1dSRodney W. Grimes }
2636831d27a9SDon Lewis 
2637831d27a9SDon Lewis /*
263848f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2639831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2640831d27a9SDon Lewis  */
2641831d27a9SDon Lewis void
2642f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2643f1320723SAlfred Perlstein 	struct sigio **sigiop;
26442c42a146SMarcel Moolenaar 	int sig, checkctty;
2645831d27a9SDon Lewis {
2646f1320723SAlfred Perlstein 	struct sigio *sigio;
2647831d27a9SDon Lewis 
2648f1320723SAlfred Perlstein 	SIGIO_LOCK();
2649f1320723SAlfred Perlstein 	sigio = *sigiop;
2650f1320723SAlfred Perlstein 	if (sigio == NULL) {
2651f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2652f1320723SAlfred Perlstein 		return;
2653f1320723SAlfred Perlstein 	}
2654831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2655628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
26562b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
26572c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2658628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2659831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2660831d27a9SDon Lewis 		struct proc *p;
2661831d27a9SDon Lewis 
2662f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2663628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2664628d2653SJohn Baldwin 			PROC_LOCK(p);
26652b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2666831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
26672c42a146SMarcel Moolenaar 				psignal(p, sig);
2668628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2669628d2653SJohn Baldwin 		}
2670f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2671831d27a9SDon Lewis 	}
2672f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2673831d27a9SDon Lewis }
2674cb679c38SJonathan Lemon 
2675cb679c38SJonathan Lemon static int
2676cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2677cb679c38SJonathan Lemon {
2678cb679c38SJonathan Lemon 	struct proc *p = curproc;
2679cb679c38SJonathan Lemon 
2680cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2681cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2682cb679c38SJonathan Lemon 
2683628d2653SJohn Baldwin 	PROC_LOCK(p);
2684cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2685628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2686cb679c38SJonathan Lemon 
2687cb679c38SJonathan Lemon 	return (0);
2688cb679c38SJonathan Lemon }
2689cb679c38SJonathan Lemon 
2690cb679c38SJonathan Lemon static void
2691cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2692cb679c38SJonathan Lemon {
2693cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2694cb679c38SJonathan Lemon 
2695628d2653SJohn Baldwin 	PROC_LOCK(p);
2696e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2697628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2698cb679c38SJonathan Lemon }
2699cb679c38SJonathan Lemon 
2700cb679c38SJonathan Lemon /*
2701cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2702cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2703cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2704cb679c38SJonathan Lemon  * isn't worth the trouble.
2705cb679c38SJonathan Lemon  */
2706cb679c38SJonathan Lemon static int
2707cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2708cb679c38SJonathan Lemon {
2709cb679c38SJonathan Lemon 
2710cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2711cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2712cb679c38SJonathan Lemon 
2713cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2714cb679c38SJonathan Lemon 			kn->kn_data++;
2715cb679c38SJonathan Lemon 	}
2716cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2717cb679c38SJonathan Lemon }
271890af4afaSJohn Baldwin 
271990af4afaSJohn Baldwin struct sigacts *
272090af4afaSJohn Baldwin sigacts_alloc(void)
272190af4afaSJohn Baldwin {
272290af4afaSJohn Baldwin 	struct sigacts *ps;
272390af4afaSJohn Baldwin 
272490af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
272590af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
272690af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
272790af4afaSJohn Baldwin 	return (ps);
272890af4afaSJohn Baldwin }
272990af4afaSJohn Baldwin 
273090af4afaSJohn Baldwin void
273190af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
273290af4afaSJohn Baldwin {
273390af4afaSJohn Baldwin 
273490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
273590af4afaSJohn Baldwin 	ps->ps_refcnt--;
273690af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
273790af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
273890af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
273990af4afaSJohn Baldwin 	} else
274090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
274190af4afaSJohn Baldwin }
274290af4afaSJohn Baldwin 
274390af4afaSJohn Baldwin struct sigacts *
274490af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
274590af4afaSJohn Baldwin {
274690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
274790af4afaSJohn Baldwin 	ps->ps_refcnt++;
274890af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
274990af4afaSJohn Baldwin 	return (ps);
275090af4afaSJohn Baldwin }
275190af4afaSJohn Baldwin 
275290af4afaSJohn Baldwin void
275390af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
275490af4afaSJohn Baldwin {
275590af4afaSJohn Baldwin 
275690af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
275790af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
275890af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
275990af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
276090af4afaSJohn Baldwin }
276190af4afaSJohn Baldwin 
276290af4afaSJohn Baldwin int
276390af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
276490af4afaSJohn Baldwin {
276590af4afaSJohn Baldwin 	int shared;
276690af4afaSJohn Baldwin 
276790af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
276890af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
276990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
277090af4afaSJohn Baldwin 	return (shared);
277190af4afaSJohn Baldwin }
2772