xref: /freebsd/sys/kern/kern_sig.c (revision 9a6a4cb50d40500639f6a784990913942393aba9)
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  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41677b542eSDavid E. O'Brien #include <sys/cdefs.h>
42677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
43677b542eSDavid E. O'Brien 
445591b823SEivind Eklund #include "opt_compat.h"
45db6a20e2SGarrett Wollman #include "opt_ktrace.h"
46db6a20e2SGarrett Wollman 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
4836240ea5SDoug Rabson #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/vnode.h>
51df8bae1dSRodney W. Grimes #include <sys/acct.h>
52238510fcSJason Evans #include <sys/condvar.h>
53854dc8c2SJohn Baldwin #include <sys/event.h>
54854dc8c2SJohn Baldwin #include <sys/fcntl.h>
55854dc8c2SJohn Baldwin #include <sys/kernel.h>
569dde3bc9SDavid Xu #include <sys/kse.h>
570384fff8SJason Evans #include <sys/ktr.h>
58df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
59854dc8c2SJohn Baldwin #include <sys/lock.h>
60854dc8c2SJohn Baldwin #include <sys/malloc.h>
61854dc8c2SJohn Baldwin #include <sys/mutex.h>
62854dc8c2SJohn Baldwin #include <sys/namei.h>
63854dc8c2SJohn Baldwin #include <sys/proc.h>
64854dc8c2SJohn Baldwin #include <sys/pioctl.h>
65c31146a1SJohn Baldwin #include <sys/resourcevar.h>
6644f3b092SJohn Baldwin #include <sys/sleepqueue.h>
676caa8a15SJohn Baldwin #include <sys/smp.h>
68df8bae1dSRodney W. Grimes #include <sys/stat.h>
691005a129SJohn Baldwin #include <sys/sx.h>
708f19eb88SIan Dowse #include <sys/syscallsubr.h>
71c87e2930SDavid Greenman #include <sys/sysctl.h>
72854dc8c2SJohn Baldwin #include <sys/sysent.h>
73854dc8c2SJohn Baldwin #include <sys/syslog.h>
74854dc8c2SJohn Baldwin #include <sys/sysproto.h>
7506ae1e91SMatthew Dillon #include <sys/unistd.h>
76854dc8c2SJohn Baldwin #include <sys/wait.h>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <machine/cpu.h>
79df8bae1dSRodney W. Grimes 
8023eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
8123eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
8223eeeff7SPeter Wemm #endif
8323eeeff7SPeter Wemm 
846f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
856f841fb7SMarcel Moolenaar 
864d77a549SAlfred Perlstein static int	coredump(struct thread *);
874d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
889c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
896711f10fSJohn Baldwin static int	issignal(struct thread *p);
904d77a549SAlfred Perlstein static int	sigprop(int sig);
914d77a549SAlfred Perlstein static void	stop(struct proc *);
924093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
93cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
94cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
95cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
964093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
97a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
98a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
99c197abc4SMike Makonnen static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
100cb679c38SJonathan Lemon 
101cb679c38SJonathan Lemon struct filterops sig_filtops =
102cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
103cb679c38SJonathan Lemon 
10457308494SJoerg Wunsch static int	kern_logsigexit = 1;
1053d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1063d177f46SBill Fumerola     &kern_logsigexit, 0,
1073d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10857308494SJoerg Wunsch 
1092b87b6d4SRobert Watson /*
1102b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1112b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1122b87b6d4SRobert Watson  * in the right situations.
1132b87b6d4SRobert Watson  */
1142b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1152b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1162b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1172b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1182b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1192b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1202b87b6d4SRobert Watson 
12122d4b0fbSJohn Polstra int sugid_coredump;
1223d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1233d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
124c87e2930SDavid Greenman 
125e5a28db9SPaul Saab static int	do_coredump = 1;
126e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
127e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
128e5a28db9SPaul Saab 
1292c42a146SMarcel Moolenaar /*
1302c42a146SMarcel Moolenaar  * Signal properties and actions.
1312c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1322c42a146SMarcel Moolenaar  * according to the following properties:
1332c42a146SMarcel Moolenaar  */
1342c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1352c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1362c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1372c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1382c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1392c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1402c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
141da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
142df8bae1dSRodney W. Grimes 
1432c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
144da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
145da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
146da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
150da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1512c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
152da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1532c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1542c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1552c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
156da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
157da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
158da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
159da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
160da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
161da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
162da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
163da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
164da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
165da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
166da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1672c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1682c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
169da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
170da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
171da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
172da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
173da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
174da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1752c42a146SMarcel Moolenaar };
1762c42a146SMarcel Moolenaar 
177fbbeeb6cSBruce Evans /*
178fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
179fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
180fbbeeb6cSBruce Evans  * action, the process stops in issignal().
181e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
182fbbeeb6cSBruce Evans  *
18333510ef1SBruce Evans  * MP SAFE.
184fbbeeb6cSBruce Evans  */
185fbbeeb6cSBruce Evans int
186e602ba25SJulian Elischer cursig(struct thread *td)
187fbbeeb6cSBruce Evans {
188c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
18990af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
190179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1914093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
192fbbeeb6cSBruce Evans }
193fbbeeb6cSBruce Evans 
19479065dbaSBruce Evans /*
19579065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1964093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1974093529dSJeff Roberson  * unmasked in td_sigmask.
19879065dbaSBruce Evans  */
19979065dbaSBruce Evans void
2004093529dSJeff Roberson signotify(struct thread *td)
20179065dbaSBruce Evans {
2024093529dSJeff Roberson 	struct proc *p;
2039dde3bc9SDavid Xu 	sigset_t set, saved;
2044093529dSJeff Roberson 
2054093529dSJeff Roberson 	p = td->td_proc;
20679065dbaSBruce Evans 
20779065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2084093529dSJeff Roberson 
2094093529dSJeff Roberson 	/*
2104093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2114093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2124093529dSJeff Roberson 	 */
2134093529dSJeff Roberson 	set = p->p_siglist;
2149dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
2159dde3bc9SDavid Xu 		saved = p->p_siglist;
2164093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2174093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2184093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2194093529dSJeff Roberson 
2208b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
22179065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2224093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
22379065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
22479065dbaSBruce Evans 	}
2259dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2269dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
2279dde3bc9SDavid Xu 			return;
2289dde3bc9SDavid Xu 		else {
2299dde3bc9SDavid Xu 			/* pending set changed */
2309dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
2319dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
2329dde3bc9SDavid Xu 		}
2339dde3bc9SDavid Xu 	}
2348b94a061SJohn Baldwin }
2358b94a061SJohn Baldwin 
2368b94a061SJohn Baldwin int
2378b94a061SJohn Baldwin sigonstack(size_t sp)
2388b94a061SJohn Baldwin {
239a30ec4b9SDavid Xu 	struct thread *td = curthread;
2408b94a061SJohn Baldwin 
241a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
2428b94a061SJohn Baldwin #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
243a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
244a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
245a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
2468b94a061SJohn Baldwin #else
247a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
2488b94a061SJohn Baldwin #endif
2498b94a061SJohn Baldwin 	    : 0);
2508b94a061SJohn Baldwin }
25179065dbaSBruce Evans 
2526f841fb7SMarcel Moolenaar static __inline int
2536f841fb7SMarcel Moolenaar sigprop(int sig)
2542c42a146SMarcel Moolenaar {
2556f841fb7SMarcel Moolenaar 
2562c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2572c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2582c42a146SMarcel Moolenaar 	return (0);
259df8bae1dSRodney W. Grimes }
2602c42a146SMarcel Moolenaar 
2614093529dSJeff Roberson int
2626f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2632c42a146SMarcel Moolenaar {
2642c42a146SMarcel Moolenaar 	int i;
2652c42a146SMarcel Moolenaar 
2666f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2672c42a146SMarcel Moolenaar 		if (set->__bits[i])
2682c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
269df8bae1dSRodney W. Grimes 	return (0);
270df8bae1dSRodney W. Grimes }
271df8bae1dSRodney W. Grimes 
2722c42a146SMarcel Moolenaar /*
2738f19eb88SIan Dowse  * kern_sigaction
2742c42a146SMarcel Moolenaar  * sigaction
27523eeeff7SPeter Wemm  * freebsd4_sigaction
2762c42a146SMarcel Moolenaar  * osigaction
27725d6dc06SJohn Baldwin  *
27825d6dc06SJohn Baldwin  * MPSAFE
2792c42a146SMarcel Moolenaar  */
2808f19eb88SIan Dowse int
28123eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2828f19eb88SIan Dowse 	struct thread *td;
2832c42a146SMarcel Moolenaar 	register int sig;
2842c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
28523eeeff7SPeter Wemm 	int flags;
286df8bae1dSRodney W. Grimes {
28790af4afaSJohn Baldwin 	struct sigacts *ps;
2884093529dSJeff Roberson 	struct thread *td0;
2898f19eb88SIan Dowse 	struct proc *p = td->td_proc;
290df8bae1dSRodney W. Grimes 
2912899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2922c42a146SMarcel Moolenaar 		return (EINVAL);
2932c42a146SMarcel Moolenaar 
294628d2653SJohn Baldwin 	PROC_LOCK(p);
295628d2653SJohn Baldwin 	ps = p->p_sigacts;
29690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2972c42a146SMarcel Moolenaar 	if (oact) {
2982c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2992c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
3002c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
3012c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
3022c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
3032c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
3042c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
3052c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
3062c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
3072c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
3082c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
3092c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
3102c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
31190af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
3122c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
31390af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
3142c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
3152c42a146SMarcel Moolenaar 	}
3162c42a146SMarcel Moolenaar 	if (act) {
3172c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
318628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
31990af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
320628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3212c42a146SMarcel Moolenaar 			return (EINVAL);
322628d2653SJohn Baldwin 		}
3232c42a146SMarcel Moolenaar 
324df8bae1dSRodney W. Grimes 		/*
325df8bae1dSRodney W. Grimes 		 * Change setting atomically.
326df8bae1dSRodney W. Grimes 		 */
3272c42a146SMarcel Moolenaar 
3282c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3292c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3302c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
331aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
332aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
33380f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
33480f42b55SIan Dowse 		} else {
33580f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3362c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3372c42a146SMarcel Moolenaar 		}
3382c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3392c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
340df8bae1dSRodney W. Grimes 		else
3412c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3422c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3432c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
344df8bae1dSRodney W. Grimes 		else
3452c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3462c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3472c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3481e41c1b5SSteven Wallace 		else
3492c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3502c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3512c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
352289ccde0SPeter Wemm 		else
3532c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
354df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3552c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3562c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
357df8bae1dSRodney W. Grimes 		else
3588c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
359df8bae1dSRodney W. Grimes #endif
3602c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3612c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
36290af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3636626c604SJulian Elischer 			else
36490af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
365ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
366245f17d4SJoerg Wunsch 				/*
3672c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3682c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3692c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3702c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
371245f17d4SJoerg Wunsch 				 */
372245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
37390af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3746626c604SJulian Elischer 				else
37590af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
376245f17d4SJoerg Wunsch 			} else
37790af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
378ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
37990af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
380ba1551caSIan Dowse 			else
38190af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
382df8bae1dSRodney W. Grimes 		}
383df8bae1dSRodney W. Grimes 		/*
38490af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3852c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
38690af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3872c42a146SMarcel Moolenaar 		 * have to restart the process.
388df8bae1dSRodney W. Grimes 		 */
3892c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3902c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3912c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3929dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
3939dde3bc9SDavid Xu 			     SIGISMEMBER(p->p_siglist, sig)) {
3949dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
3959dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
3969dde3bc9SDavid Xu 			}
3972c42a146SMarcel Moolenaar 			/* never to be seen again */
3981d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
399a9a48d68SDavid Xu 			mtx_lock_spin(&sched_lock);
4004093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
4014093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
402a9a48d68SDavid Xu 			mtx_unlock_spin(&sched_lock);
4032c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4042c42a146SMarcel Moolenaar 				/* easier in psignal */
40590af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
40690af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
407645682fdSLuoqi Chen 		} else {
40890af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
4092c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
41090af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
4112c42a146SMarcel Moolenaar 			else
41290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
4132c42a146SMarcel Moolenaar 		}
41423eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
41523eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
41623eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41723eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
41823eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
41923eeeff7SPeter Wemm 		else
42023eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
42123eeeff7SPeter Wemm #endif
422e8ebc08fSPeter Wemm #ifdef COMPAT_43
423645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
42423eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
42523eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
426645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
427645682fdSLuoqi Chen 		else
428645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
429e8ebc08fSPeter Wemm #endif
430df8bae1dSRodney W. Grimes 	}
43190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
432628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4332c42a146SMarcel Moolenaar 	return (0);
4342c42a146SMarcel Moolenaar }
4352c42a146SMarcel Moolenaar 
4362c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4372c42a146SMarcel Moolenaar struct sigaction_args {
4382c42a146SMarcel Moolenaar 	int	sig;
4392c42a146SMarcel Moolenaar 	struct	sigaction *act;
4402c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4412c42a146SMarcel Moolenaar };
4422c42a146SMarcel Moolenaar #endif
443fb99ab88SMatthew Dillon /*
444fb99ab88SMatthew Dillon  * MPSAFE
445fb99ab88SMatthew Dillon  */
4462c42a146SMarcel Moolenaar int
447b40ce416SJulian Elischer sigaction(td, uap)
448b40ce416SJulian Elischer 	struct thread *td;
4492c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4502c42a146SMarcel Moolenaar {
4512c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4522c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4532c42a146SMarcel Moolenaar 	int error;
4542c42a146SMarcel Moolenaar 
4556f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4566f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4572c42a146SMarcel Moolenaar 	if (actp) {
4586f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4592c42a146SMarcel Moolenaar 		if (error)
46044443757STim J. Robbins 			return (error);
4612c42a146SMarcel Moolenaar 	}
4628f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
46325d6dc06SJohn Baldwin 	if (oactp && !error)
4646f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4652c42a146SMarcel Moolenaar 	return (error);
4662c42a146SMarcel Moolenaar }
4672c42a146SMarcel Moolenaar 
46823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
46923eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
47023eeeff7SPeter Wemm struct freebsd4_sigaction_args {
47123eeeff7SPeter Wemm 	int	sig;
47223eeeff7SPeter Wemm 	struct	sigaction *act;
47323eeeff7SPeter Wemm 	struct	sigaction *oact;
47423eeeff7SPeter Wemm };
47523eeeff7SPeter Wemm #endif
47623eeeff7SPeter Wemm /*
47723eeeff7SPeter Wemm  * MPSAFE
47823eeeff7SPeter Wemm  */
47923eeeff7SPeter Wemm int
48023eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
48123eeeff7SPeter Wemm 	struct thread *td;
48223eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
48323eeeff7SPeter Wemm {
48423eeeff7SPeter Wemm 	struct sigaction act, oact;
48523eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
48623eeeff7SPeter Wemm 	int error;
48723eeeff7SPeter Wemm 
48823eeeff7SPeter Wemm 
48923eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
49023eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
49123eeeff7SPeter Wemm 	if (actp) {
49223eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
49323eeeff7SPeter Wemm 		if (error)
49444443757STim J. Robbins 			return (error);
49523eeeff7SPeter Wemm 	}
49623eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
497a14e1189SJohn Baldwin 	if (oactp && !error)
49823eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
49923eeeff7SPeter Wemm 	return (error);
50023eeeff7SPeter Wemm }
50123eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
50223eeeff7SPeter Wemm 
50331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5042c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5052c42a146SMarcel Moolenaar struct osigaction_args {
5062c42a146SMarcel Moolenaar 	int	signum;
5072c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
5082c42a146SMarcel Moolenaar 	struct	osigaction *osa;
5092c42a146SMarcel Moolenaar };
5102c42a146SMarcel Moolenaar #endif
511fb99ab88SMatthew Dillon /*
512fb99ab88SMatthew Dillon  * MPSAFE
513fb99ab88SMatthew Dillon  */
5142c42a146SMarcel Moolenaar int
515b40ce416SJulian Elischer osigaction(td, uap)
516b40ce416SJulian Elischer 	struct thread *td;
5172c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
5182c42a146SMarcel Moolenaar {
5192c42a146SMarcel Moolenaar 	struct osigaction sa;
5202c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5212c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5222c42a146SMarcel Moolenaar 	int error;
5232c42a146SMarcel Moolenaar 
5246f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5256f841fb7SMarcel Moolenaar 		return (EINVAL);
526fb99ab88SMatthew Dillon 
5276f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5286f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
529fb99ab88SMatthew Dillon 
5302c42a146SMarcel Moolenaar 	if (nsap) {
5316f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5322c42a146SMarcel Moolenaar 		if (error)
53344443757STim J. Robbins 			return (error);
5342c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5352c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5362c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5372c42a146SMarcel Moolenaar 	}
53823eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5392c42a146SMarcel Moolenaar 	if (osap && !error) {
5402c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5412c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5422c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5436f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5442c42a146SMarcel Moolenaar 	}
5452c42a146SMarcel Moolenaar 	return (error);
5462c42a146SMarcel Moolenaar }
54723eeeff7SPeter Wemm 
54823eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
54923eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
55023eeeff7SPeter Wemm int
55123eeeff7SPeter Wemm osigreturn(td, uap)
55223eeeff7SPeter Wemm 	struct thread *td;
55323eeeff7SPeter Wemm 	struct osigreturn_args *uap;
55423eeeff7SPeter Wemm {
55523eeeff7SPeter Wemm 
55623eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
55723eeeff7SPeter Wemm }
55823eeeff7SPeter Wemm #endif
55931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes /*
562df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
563df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
564df8bae1dSRodney W. Grimes  */
565df8bae1dSRodney W. Grimes void
566df8bae1dSRodney W. Grimes siginit(p)
567df8bae1dSRodney W. Grimes 	struct proc *p;
568df8bae1dSRodney W. Grimes {
569df8bae1dSRodney W. Grimes 	register int i;
57090af4afaSJohn Baldwin 	struct sigacts *ps;
571df8bae1dSRodney W. Grimes 
572628d2653SJohn Baldwin 	PROC_LOCK(p);
57390af4afaSJohn Baldwin 	ps = p->p_sigacts;
57490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5752c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5762c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
57790af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
57890af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
579628d2653SJohn Baldwin 	PROC_UNLOCK(p);
580df8bae1dSRodney W. Grimes }
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes /*
583df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
584df8bae1dSRodney W. Grimes  */
585df8bae1dSRodney W. Grimes void
586a30ec4b9SDavid Xu execsigs(struct proc *p)
587df8bae1dSRodney W. Grimes {
588a30ec4b9SDavid Xu 	struct sigacts *ps;
589a30ec4b9SDavid Xu 	int sig;
590a30ec4b9SDavid Xu 	struct thread *td;
591df8bae1dSRodney W. Grimes 
592df8bae1dSRodney W. Grimes 	/*
593df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5944093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
595df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
596df8bae1dSRodney W. Grimes 	 */
5979b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
598a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
599628d2653SJohn Baldwin 	ps = p->p_sigacts;
60090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
60190af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
60290af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
60390af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
6042c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
6052c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
60690af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
6071d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
6084093529dSJeff Roberson 			/*
6094093529dSJeff Roberson 			 * There is only one thread at this point.
6104093529dSJeff Roberson 			 */
611a30ec4b9SDavid Xu 			SIGDELSET(td->td_siglist, sig);
612df8bae1dSRodney W. Grimes 		}
6132c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
614df8bae1dSRodney W. Grimes 	}
615df8bae1dSRodney W. Grimes 	/*
616df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
617df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
618df8bae1dSRodney W. Grimes 	 */
619a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
620a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
621a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
622a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
62380e907a1SPeter Wemm 	/*
62480e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
62580e907a1SPeter Wemm 	 */
62690af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
627c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
628c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
62990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
630df8bae1dSRodney W. Grimes }
631df8bae1dSRodney W. Grimes 
632df8bae1dSRodney W. Grimes /*
633e77daab1SJohn Baldwin  * kern_sigprocmask()
6347c8fdcbdSMatthew Dillon  *
635628d2653SJohn Baldwin  *	Manipulate signal mask.
636df8bae1dSRodney W. Grimes  */
637e77daab1SJohn Baldwin int
638e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6394093529dSJeff Roberson 	struct thread *td;
6402c42a146SMarcel Moolenaar 	int how;
6412c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
642645682fdSLuoqi Chen 	int old;
6432c42a146SMarcel Moolenaar {
6442c42a146SMarcel Moolenaar 	int error;
6452c42a146SMarcel Moolenaar 
6464093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6472c42a146SMarcel Moolenaar 	if (oset != NULL)
6484093529dSJeff Roberson 		*oset = td->td_sigmask;
6492c42a146SMarcel Moolenaar 
6502c42a146SMarcel Moolenaar 	error = 0;
6512c42a146SMarcel Moolenaar 	if (set != NULL) {
6522c42a146SMarcel Moolenaar 		switch (how) {
6532c42a146SMarcel Moolenaar 		case SIG_BLOCK:
654645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6554093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6562c42a146SMarcel Moolenaar 			break;
6572c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6584093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6594093529dSJeff Roberson 			signotify(td);
6602c42a146SMarcel Moolenaar 			break;
6612c42a146SMarcel Moolenaar 		case SIG_SETMASK:
662645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
663645682fdSLuoqi Chen 			if (old)
6644093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
665645682fdSLuoqi Chen 			else
6664093529dSJeff Roberson 				td->td_sigmask = *set;
6674093529dSJeff Roberson 			signotify(td);
6682c42a146SMarcel Moolenaar 			break;
6692c42a146SMarcel Moolenaar 		default:
6702c42a146SMarcel Moolenaar 			error = EINVAL;
6712c42a146SMarcel Moolenaar 			break;
6722c42a146SMarcel Moolenaar 		}
6732c42a146SMarcel Moolenaar 	}
6744093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6752c42a146SMarcel Moolenaar 	return (error);
6762c42a146SMarcel Moolenaar }
6772c42a146SMarcel Moolenaar 
6787c8fdcbdSMatthew Dillon /*
679e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6807c8fdcbdSMatthew Dillon  */
6817c8fdcbdSMatthew Dillon 
682d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
683df8bae1dSRodney W. Grimes struct sigprocmask_args {
684df8bae1dSRodney W. Grimes 	int	how;
6852c42a146SMarcel Moolenaar 	const sigset_t *set;
6862c42a146SMarcel Moolenaar 	sigset_t *oset;
687df8bae1dSRodney W. Grimes };
688d2d3e875SBruce Evans #endif
68926f9a767SRodney W. Grimes int
690b40ce416SJulian Elischer sigprocmask(td, uap)
691b40ce416SJulian Elischer 	register struct thread *td;
692df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
693df8bae1dSRodney W. Grimes {
6942c42a146SMarcel Moolenaar 	sigset_t set, oset;
6952c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6962c42a146SMarcel Moolenaar 	int error;
697df8bae1dSRodney W. Grimes 
6986f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6996f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
7002c42a146SMarcel Moolenaar 	if (setp) {
7016f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
7022c42a146SMarcel Moolenaar 		if (error)
7032c42a146SMarcel Moolenaar 			return (error);
704df8bae1dSRodney W. Grimes 	}
705e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
7062c42a146SMarcel Moolenaar 	if (osetp && !error) {
7076f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
7082c42a146SMarcel Moolenaar 	}
7092c42a146SMarcel Moolenaar 	return (error);
7102c42a146SMarcel Moolenaar }
7112c42a146SMarcel Moolenaar 
71231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7137c8fdcbdSMatthew Dillon /*
7147c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
7157c8fdcbdSMatthew Dillon  */
7162c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7172c42a146SMarcel Moolenaar struct osigprocmask_args {
7182c42a146SMarcel Moolenaar 	int	how;
7192c42a146SMarcel Moolenaar 	osigset_t mask;
7202c42a146SMarcel Moolenaar };
7212c42a146SMarcel Moolenaar #endif
7222c42a146SMarcel Moolenaar int
723b40ce416SJulian Elischer osigprocmask(td, uap)
724b40ce416SJulian Elischer 	register struct thread *td;
7252c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7262c42a146SMarcel Moolenaar {
7272c42a146SMarcel Moolenaar 	sigset_t set, oset;
7282c42a146SMarcel Moolenaar 	int error;
7292c42a146SMarcel Moolenaar 
7302c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
731e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
732b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
733df8bae1dSRodney W. Grimes 	return (error);
734df8bae1dSRodney W. Grimes }
73531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
736df8bae1dSRodney W. Grimes 
737d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
738df8bae1dSRodney W. Grimes struct sigpending_args {
7392c42a146SMarcel Moolenaar 	sigset_t	*set;
740df8bae1dSRodney W. Grimes };
741d2d3e875SBruce Evans #endif
742fb99ab88SMatthew Dillon /*
743fb99ab88SMatthew Dillon  * MPSAFE
744fb99ab88SMatthew Dillon  */
74526f9a767SRodney W. Grimes int
746a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
747a447cd8bSJeff Roberson {
748a447cd8bSJeff Roberson 	siginfo_t info;
749a447cd8bSJeff Roberson 	sigset_t set;
750a447cd8bSJeff Roberson 	int error;
751a447cd8bSJeff Roberson 
752a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
753a447cd8bSJeff Roberson 	if (error)
754a447cd8bSJeff Roberson 		return (error);
755a447cd8bSJeff Roberson 
756a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
757a447cd8bSJeff Roberson 	if (error)
758a447cd8bSJeff Roberson 		return (error);
759a447cd8bSJeff Roberson 
760a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
761a447cd8bSJeff Roberson 	/* Repost if we got an error. */
76218440c7fSJohn Baldwin 	if (error && info.si_signo) {
76318440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
764c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
76518440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
76618440c7fSJohn Baldwin 	}
767a447cd8bSJeff Roberson 	return (error);
768a447cd8bSJeff Roberson }
769a447cd8bSJeff Roberson /*
770a447cd8bSJeff Roberson  * MPSAFE
771a447cd8bSJeff Roberson  */
772a447cd8bSJeff Roberson int
773a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
774a447cd8bSJeff Roberson {
775a447cd8bSJeff Roberson 	struct timespec ts;
776a447cd8bSJeff Roberson 	struct timespec *timeout;
777a447cd8bSJeff Roberson 	sigset_t set;
778a447cd8bSJeff Roberson 	siginfo_t info;
779a447cd8bSJeff Roberson 	int error;
780a447cd8bSJeff Roberson 
781a447cd8bSJeff Roberson 	if (uap->timeout) {
782a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
783a447cd8bSJeff Roberson 		if (error)
784a447cd8bSJeff Roberson 			return (error);
785a447cd8bSJeff Roberson 
786a447cd8bSJeff Roberson 		timeout = &ts;
787a447cd8bSJeff Roberson 	} else
788a447cd8bSJeff Roberson 		timeout = NULL;
789a447cd8bSJeff Roberson 
790a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
791a447cd8bSJeff Roberson 	if (error)
792a447cd8bSJeff Roberson 		return (error);
793a447cd8bSJeff Roberson 
794a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
795a447cd8bSJeff Roberson 	if (error)
796a447cd8bSJeff Roberson 		return (error);
7979dde3bc9SDavid Xu 
798418228dfSDavid Xu 	if (uap->info)
799a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
800a447cd8bSJeff Roberson 	/* Repost if we got an error. */
80118440c7fSJohn Baldwin 	if (error && info.si_signo) {
80218440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
803c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
80418440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
805418228dfSDavid Xu 	} else {
806418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
80718440c7fSJohn Baldwin 	}
808a447cd8bSJeff Roberson 	return (error);
809a447cd8bSJeff Roberson }
810a447cd8bSJeff Roberson 
811a447cd8bSJeff Roberson /*
812a447cd8bSJeff Roberson  * MPSAFE
813a447cd8bSJeff Roberson  */
814a447cd8bSJeff Roberson int
815a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
816a447cd8bSJeff Roberson {
817a447cd8bSJeff Roberson 	siginfo_t info;
818a447cd8bSJeff Roberson 	sigset_t set;
819a447cd8bSJeff Roberson 	int error;
820a447cd8bSJeff Roberson 
821a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
822a447cd8bSJeff Roberson 	if (error)
823a447cd8bSJeff Roberson 		return (error);
824a447cd8bSJeff Roberson 
825a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
826a447cd8bSJeff Roberson 	if (error)
827a447cd8bSJeff Roberson 		return (error);
828a447cd8bSJeff Roberson 
829418228dfSDavid Xu 	if (uap->info)
830a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
831a447cd8bSJeff Roberson 	/* Repost if we got an error. */
83218440c7fSJohn Baldwin 	if (error && info.si_signo) {
83318440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
834c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
83518440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
836418228dfSDavid Xu 	} else {
837418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
83818440c7fSJohn Baldwin 	}
839a447cd8bSJeff Roberson 	return (error);
840a447cd8bSJeff Roberson }
841a447cd8bSJeff Roberson 
842a447cd8bSJeff Roberson static int
8433074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
844a447cd8bSJeff Roberson     struct timespec *timeout)
845a447cd8bSJeff Roberson {
8463074d1b4SDavid Xu 	struct sigacts *ps;
8473074d1b4SDavid Xu 	sigset_t savedmask, sigset;
848a447cd8bSJeff Roberson 	struct proc *p;
849a447cd8bSJeff Roberson 	int error;
850a447cd8bSJeff Roberson 	int sig;
851a447cd8bSJeff Roberson 	int hz;
8523074d1b4SDavid Xu 	int i;
853a447cd8bSJeff Roberson 
854a447cd8bSJeff Roberson 	p = td->td_proc;
855a447cd8bSJeff Roberson 	error = 0;
856a447cd8bSJeff Roberson 	sig = 0;
8573074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
858a447cd8bSJeff Roberson 
859a447cd8bSJeff Roberson 	PROC_LOCK(p);
860a447cd8bSJeff Roberson 	ps = p->p_sigacts;
8613074d1b4SDavid Xu 	savedmask = td->td_sigmask;
8623074d1b4SDavid Xu 
8633074d1b4SDavid Xu again:
8643074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
8653074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
8663074d1b4SDavid Xu 			continue;
8673074d1b4SDavid Xu 		if (SIGISMEMBER(td->td_siglist, i)) {
868418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
869418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8703074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
87190af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
872a447cd8bSJeff Roberson 			sig = cursig(td);
8733074d1b4SDavid Xu 			i = 0;
8743074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8753074d1b4SDavid Xu 		} else if (SIGISMEMBER(p->p_siglist, i)) {
8763074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
8773074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
8783074d1b4SDavid Xu 				wakeup(&p->p_siglist);
8793074d1b4SDavid Xu 			}
8803074d1b4SDavid Xu 			SIGDELSET(p->p_siglist, i);
8813074d1b4SDavid Xu 			SIGADDSET(td->td_siglist, i);
8823074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
8833074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8843074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
8853074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
8863074d1b4SDavid Xu 			sig = cursig(td);
8873074d1b4SDavid Xu 			i = 0;
8883074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8893074d1b4SDavid Xu 		}
8903074d1b4SDavid Xu 		if (sig) {
8913074d1b4SDavid Xu 			td->td_sigmask = savedmask;
8923074d1b4SDavid Xu 			signotify(td);
893a447cd8bSJeff Roberson 			goto out;
8943074d1b4SDavid Xu 		}
8953074d1b4SDavid Xu 	}
8963074d1b4SDavid Xu 	if (error)
8973074d1b4SDavid Xu 		goto out;
8983074d1b4SDavid Xu 
8993074d1b4SDavid Xu 	td->td_sigmask = savedmask;
9003074d1b4SDavid Xu 	signotify(td);
9013074d1b4SDavid Xu 	sigset = td->td_siglist;
9023074d1b4SDavid Xu 	SIGSETOR(sigset, p->p_siglist);
9033074d1b4SDavid Xu 	SIGSETAND(sigset, waitset);
9043074d1b4SDavid Xu 	if (!SIGISEMPTY(sigset))
9053074d1b4SDavid Xu 		goto again;
906a447cd8bSJeff Roberson 
907a447cd8bSJeff Roberson 	/*
908a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
909a447cd8bSJeff Roberson 	 * signals.
910a447cd8bSJeff Roberson 	 */
911a447cd8bSJeff Roberson 	if (timeout) {
912a447cd8bSJeff Roberson 		struct timeval tv;
913a447cd8bSJeff Roberson 
914a6ca4808SMike Makonnen 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
915a447cd8bSJeff Roberson 			error = EINVAL;
916a447cd8bSJeff Roberson 			goto out;
917a447cd8bSJeff Roberson 		}
9183074d1b4SDavid Xu 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
9193074d1b4SDavid Xu 			error = EAGAIN;
9203074d1b4SDavid Xu 			goto out;
9213074d1b4SDavid Xu 		}
922a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
923a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
924a447cd8bSJeff Roberson 	} else
925a447cd8bSJeff Roberson 		hz = 0;
926a447cd8bSJeff Roberson 
9273074d1b4SDavid Xu 	td->td_waitset = &waitset;
92886b5e563SDag-Erling Smørgrav 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
9293074d1b4SDavid Xu 	td->td_waitset = NULL;
9303074d1b4SDavid Xu 	if (error == 0) /* surplus wakeup ? */
9313074d1b4SDavid Xu 		error = EINTR;
9323074d1b4SDavid Xu 	goto again;
9339dde3bc9SDavid Xu 
934a447cd8bSJeff Roberson out:
935a447cd8bSJeff Roberson 	if (sig) {
936a447cd8bSJeff Roberson 		sig_t action;
937a447cd8bSJeff Roberson 
9383074d1b4SDavid Xu 		error = 0;
9393074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
940a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
94190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
942a447cd8bSJeff Roberson #ifdef KTRACE
943a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
9443074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
945a447cd8bSJeff Roberson #endif
946a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
947a447cd8bSJeff Roberson 
948a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
949a447cd8bSJeff Roberson 		info->si_signo = sig;
950a447cd8bSJeff Roberson 		info->si_code = 0;
9513074d1b4SDavid Xu 	}
952a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
953a447cd8bSJeff Roberson 	return (error);
954a447cd8bSJeff Roberson }
955a447cd8bSJeff Roberson 
956a447cd8bSJeff Roberson /*
957a447cd8bSJeff Roberson  * MPSAFE
958a447cd8bSJeff Roberson  */
959a447cd8bSJeff Roberson int
960b40ce416SJulian Elischer sigpending(td, uap)
961b40ce416SJulian Elischer 	struct thread *td;
962df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
963df8bae1dSRodney W. Grimes {
964b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
965628d2653SJohn Baldwin 	sigset_t siglist;
966df8bae1dSRodney W. Grimes 
967628d2653SJohn Baldwin 	PROC_LOCK(p);
9681d9c5696SJuli Mallett 	siglist = p->p_siglist;
9694093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
970628d2653SJohn Baldwin 	PROC_UNLOCK(p);
97148e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9722c42a146SMarcel Moolenaar }
9732c42a146SMarcel Moolenaar 
97431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9752c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9762c42a146SMarcel Moolenaar struct osigpending_args {
9772c42a146SMarcel Moolenaar 	int	dummy;
9782c42a146SMarcel Moolenaar };
9792c42a146SMarcel Moolenaar #endif
980fb99ab88SMatthew Dillon /*
981fb99ab88SMatthew Dillon  * MPSAFE
982fb99ab88SMatthew Dillon  */
9832c42a146SMarcel Moolenaar int
984b40ce416SJulian Elischer osigpending(td, uap)
985b40ce416SJulian Elischer 	struct thread *td;
9862c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9872c42a146SMarcel Moolenaar {
988b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9894093529dSJeff Roberson 	sigset_t siglist;
990b40ce416SJulian Elischer 
991628d2653SJohn Baldwin 	PROC_LOCK(p);
9924093529dSJeff Roberson 	siglist = p->p_siglist;
9934093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
994628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9959d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
996df8bae1dSRodney W. Grimes 	return (0);
997df8bae1dSRodney W. Grimes }
99831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
999df8bae1dSRodney W. Grimes 
1000df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1001df8bae1dSRodney W. Grimes /*
1002df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1003df8bae1dSRodney W. Grimes  */
1004d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1005df8bae1dSRodney W. Grimes struct osigvec_args {
1006df8bae1dSRodney W. Grimes 	int	signum;
1007df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1008df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1009df8bae1dSRodney W. Grimes };
1010d2d3e875SBruce Evans #endif
1011fb99ab88SMatthew Dillon /*
1012fb99ab88SMatthew Dillon  * MPSAFE
1013fb99ab88SMatthew Dillon  */
1014df8bae1dSRodney W. Grimes /* ARGSUSED */
101526f9a767SRodney W. Grimes int
1016b40ce416SJulian Elischer osigvec(td, uap)
1017b40ce416SJulian Elischer 	struct thread *td;
1018df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1019df8bae1dSRodney W. Grimes {
1020df8bae1dSRodney W. Grimes 	struct sigvec vec;
10212c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
10222c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
10232c42a146SMarcel Moolenaar 	int error;
1024df8bae1dSRodney W. Grimes 
10256f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10266f841fb7SMarcel Moolenaar 		return (EINVAL);
10276f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
10286f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
10292c42a146SMarcel Moolenaar 	if (nsap) {
10306f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
10312c42a146SMarcel Moolenaar 		if (error)
1032df8bae1dSRodney W. Grimes 			return (error);
10332c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
10342c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
10352c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
10362c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1037df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
10382c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
1039df8bae1dSRodney W. Grimes #endif
1040df8bae1dSRodney W. Grimes 	}
10415edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10422c42a146SMarcel Moolenaar 	if (osap && !error) {
10432c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
10442c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
10452c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
10462c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
10472c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
10482c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
10492c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
10502c42a146SMarcel Moolenaar #endif
10516f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10522c42a146SMarcel Moolenaar 	}
10532c42a146SMarcel Moolenaar 	return (error);
1054df8bae1dSRodney W. Grimes }
1055df8bae1dSRodney W. Grimes 
1056d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1057df8bae1dSRodney W. Grimes struct osigblock_args {
1058df8bae1dSRodney W. Grimes 	int	mask;
1059df8bae1dSRodney W. Grimes };
1060d2d3e875SBruce Evans #endif
1061fb99ab88SMatthew Dillon /*
1062fb99ab88SMatthew Dillon  * MPSAFE
1063fb99ab88SMatthew Dillon  */
106426f9a767SRodney W. Grimes int
1065b40ce416SJulian Elischer osigblock(td, uap)
1066b40ce416SJulian Elischer 	register struct thread *td;
1067df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1068df8bae1dSRodney W. Grimes {
1069b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10702c42a146SMarcel Moolenaar 	sigset_t set;
1071df8bae1dSRodney W. Grimes 
10722c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10732c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1074628d2653SJohn Baldwin 	PROC_LOCK(p);
10754093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10764093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1077628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1078df8bae1dSRodney W. Grimes 	return (0);
1079df8bae1dSRodney W. Grimes }
1080df8bae1dSRodney W. Grimes 
1081d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1082df8bae1dSRodney W. Grimes struct osigsetmask_args {
1083df8bae1dSRodney W. Grimes 	int	mask;
1084df8bae1dSRodney W. Grimes };
1085d2d3e875SBruce Evans #endif
1086fb99ab88SMatthew Dillon /*
1087fb99ab88SMatthew Dillon  * MPSAFE
1088fb99ab88SMatthew Dillon  */
108926f9a767SRodney W. Grimes int
1090b40ce416SJulian Elischer osigsetmask(td, uap)
1091b40ce416SJulian Elischer 	struct thread *td;
1092df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1093df8bae1dSRodney W. Grimes {
1094b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10952c42a146SMarcel Moolenaar 	sigset_t set;
1096df8bae1dSRodney W. Grimes 
10972c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10982c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1099628d2653SJohn Baldwin 	PROC_LOCK(p);
11004093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
11014093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
11024093529dSJeff Roberson 	signotify(td);
1103628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1104df8bae1dSRodney W. Grimes 	return (0);
1105df8bae1dSRodney W. Grimes }
1106df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1107df8bae1dSRodney W. Grimes 
1108df8bae1dSRodney W. Grimes /*
1109df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
111097563428SAlexander Kabaev  * in the meantime.
1111b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1112b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1113b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1114df8bae1dSRodney W. Grimes  */
1115d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1116df8bae1dSRodney W. Grimes struct sigsuspend_args {
11172c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1118df8bae1dSRodney W. Grimes };
1119d2d3e875SBruce Evans #endif
1120fb99ab88SMatthew Dillon /*
1121fb99ab88SMatthew Dillon  * MPSAFE
1122fb99ab88SMatthew Dillon  */
1123df8bae1dSRodney W. Grimes /* ARGSUSED */
112426f9a767SRodney W. Grimes int
1125b40ce416SJulian Elischer sigsuspend(td, uap)
1126b40ce416SJulian Elischer 	struct thread *td;
1127df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1128df8bae1dSRodney W. Grimes {
11292c42a146SMarcel Moolenaar 	sigset_t mask;
11302c42a146SMarcel Moolenaar 	int error;
11312c42a146SMarcel Moolenaar 
11326f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11332c42a146SMarcel Moolenaar 	if (error)
11342c42a146SMarcel Moolenaar 		return (error);
11358f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11368f19eb88SIan Dowse }
11378f19eb88SIan Dowse 
11388f19eb88SIan Dowse int
11398f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11408f19eb88SIan Dowse {
11418f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 	/*
1144645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1145df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1146df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1147df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1148df8bae1dSRodney W. Grimes 	 * to indicate this.
1149df8bae1dSRodney W. Grimes 	 */
1150628d2653SJohn Baldwin 	PROC_LOCK(p);
11514093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11525e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1153645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11544093529dSJeff Roberson 	td->td_sigmask = mask;
11554093529dSJeff Roberson 	signotify(td);
115686b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11572c42a146SMarcel Moolenaar 		/* void */;
1158628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11592c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11602c42a146SMarcel Moolenaar 	return (EINTR);
11612c42a146SMarcel Moolenaar }
11622c42a146SMarcel Moolenaar 
116331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
116497563428SAlexander Kabaev /*
116597563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
116697563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
116797563428SAlexander Kabaev  */
11682c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11692c42a146SMarcel Moolenaar struct osigsuspend_args {
11702c42a146SMarcel Moolenaar 	osigset_t mask;
11712c42a146SMarcel Moolenaar };
11722c42a146SMarcel Moolenaar #endif
1173fb99ab88SMatthew Dillon /*
1174fb99ab88SMatthew Dillon  * MPSAFE
1175fb99ab88SMatthew Dillon  */
11762c42a146SMarcel Moolenaar /* ARGSUSED */
11772c42a146SMarcel Moolenaar int
1178b40ce416SJulian Elischer osigsuspend(td, uap)
1179b40ce416SJulian Elischer 	struct thread *td;
11802c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11812c42a146SMarcel Moolenaar {
1182b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1183645682fdSLuoqi Chen 	sigset_t mask;
11842c42a146SMarcel Moolenaar 
1185628d2653SJohn Baldwin 	PROC_LOCK(p);
11864093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11875e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1188645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1189645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11904093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11914093529dSJeff Roberson 	signotify(td);
119286b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1193df8bae1dSRodney W. Grimes 		/* void */;
1194628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1195df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1196df8bae1dSRodney W. Grimes 	return (EINTR);
1197df8bae1dSRodney W. Grimes }
119831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1199df8bae1dSRodney W. Grimes 
1200df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1201d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1202df8bae1dSRodney W. Grimes struct osigstack_args {
1203df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1204df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1205df8bae1dSRodney W. Grimes };
1206d2d3e875SBruce Evans #endif
1207fb99ab88SMatthew Dillon /*
1208fb99ab88SMatthew Dillon  * MPSAFE
1209fb99ab88SMatthew Dillon  */
1210df8bae1dSRodney W. Grimes /* ARGSUSED */
121126f9a767SRodney W. Grimes int
1212b40ce416SJulian Elischer osigstack(td, uap)
1213b40ce416SJulian Elischer 	struct thread *td;
1214df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1215df8bae1dSRodney W. Grimes {
12165afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1217fb99ab88SMatthew Dillon 	int error = 0;
1218fb99ab88SMatthew Dillon 
1219d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12205afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12215afe0c99SJohn Baldwin 		if (error)
12225afe0c99SJohn Baldwin 			return (error);
1223df8bae1dSRodney W. Grimes 	}
1224a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12255afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12265afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1227a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1228a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1229a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1230a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12315afe0c99SJohn Baldwin 	}
12325afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12335afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12345afe0c99SJohn Baldwin 
1235fb99ab88SMatthew Dillon 	return (error);
1236df8bae1dSRodney W. Grimes }
1237df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1238df8bae1dSRodney W. Grimes 
1239d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1240df8bae1dSRodney W. Grimes struct sigaltstack_args {
12412c42a146SMarcel Moolenaar 	stack_t	*ss;
12422c42a146SMarcel Moolenaar 	stack_t	*oss;
1243df8bae1dSRodney W. Grimes };
1244d2d3e875SBruce Evans #endif
1245fb99ab88SMatthew Dillon /*
1246fb99ab88SMatthew Dillon  * MPSAFE
1247fb99ab88SMatthew Dillon  */
1248df8bae1dSRodney W. Grimes /* ARGSUSED */
124926f9a767SRodney W. Grimes int
1250b40ce416SJulian Elischer sigaltstack(td, uap)
1251b40ce416SJulian Elischer 	struct thread *td;
1252df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1253df8bae1dSRodney W. Grimes {
12548f19eb88SIan Dowse 	stack_t ss, oss;
12558f19eb88SIan Dowse 	int error;
12568f19eb88SIan Dowse 
12578f19eb88SIan Dowse 	if (uap->ss != NULL) {
12588f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12598f19eb88SIan Dowse 		if (error)
12608f19eb88SIan Dowse 			return (error);
12618f19eb88SIan Dowse 	}
12628f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12638f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12648f19eb88SIan Dowse 	if (error)
12658f19eb88SIan Dowse 		return (error);
12668f19eb88SIan Dowse 	if (uap->oss != NULL)
12678f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12688f19eb88SIan Dowse 	return (error);
12698f19eb88SIan Dowse }
12708f19eb88SIan Dowse 
12718f19eb88SIan Dowse int
12728f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12738f19eb88SIan Dowse {
1274b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1275fb99ab88SMatthew Dillon 	int oonstack;
1276fb99ab88SMatthew Dillon 
1277b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1278d034d459SMarcel Moolenaar 
12798f19eb88SIan Dowse 	if (oss != NULL) {
1280a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1281a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1282d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1283df8bae1dSRodney W. Grimes 	}
1284d034d459SMarcel Moolenaar 
12858f19eb88SIan Dowse 	if (ss != NULL) {
1286a30ec4b9SDavid Xu 		if (oonstack)
1287cf60731bSJohn Baldwin 			return (EPERM);
1288a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1289cf60731bSJohn Baldwin 			return (EINVAL);
12908f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12918f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1292cf60731bSJohn Baldwin 				return (ENOMEM);
1293fb99ab88SMatthew Dillon 			}
1294a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1295a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1296628d2653SJohn Baldwin 		} else {
1297a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1298628d2653SJohn Baldwin 		}
1299d034d459SMarcel Moolenaar 	}
1300cf60731bSJohn Baldwin 	return (0);
1301df8bae1dSRodney W. Grimes }
1302df8bae1dSRodney W. Grimes 
1303d93f860cSPoul-Henning Kamp /*
1304d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1305d93f860cSPoul-Henning Kamp  * cp is calling process.
1306d93f860cSPoul-Henning Kamp  */
130737c84183SPoul-Henning Kamp static int
13089c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
13099c1ab3e0SJohn Baldwin 	register struct thread *td;
13102c42a146SMarcel Moolenaar 	int sig, pgid, all;
1311d93f860cSPoul-Henning Kamp {
1312d93f860cSPoul-Henning Kamp 	register struct proc *p;
1313d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1314d93f860cSPoul-Henning Kamp 	int nfound = 0;
1315d93f860cSPoul-Henning Kamp 
1316553629ebSJake Burkholder 	if (all) {
1317d93f860cSPoul-Henning Kamp 		/*
1318d93f860cSPoul-Henning Kamp 		 * broadcast
1319d93f860cSPoul-Henning Kamp 		 */
13201005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13212e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1322628d2653SJohn Baldwin 			PROC_LOCK(p);
13239c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13249c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1325628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1326628d2653SJohn Baldwin 				continue;
1327628d2653SJohn Baldwin 			}
1328f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1329d93f860cSPoul-Henning Kamp 				nfound++;
133033a9ed9dSJohn Baldwin 				if (sig)
13312c42a146SMarcel Moolenaar 					psignal(p, sig);
1332628d2653SJohn Baldwin 			}
133333a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1334d93f860cSPoul-Henning Kamp 		}
13351005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1336553629ebSJake Burkholder 	} else {
1337ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1338f591779bSSeigo Tanimura 		if (pgid == 0) {
1339d93f860cSPoul-Henning Kamp 			/*
1340d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1341d93f860cSPoul-Henning Kamp 			 */
13429c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1343f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1344f591779bSSeigo Tanimura 		} else {
1345d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1346f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1347ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1348d93f860cSPoul-Henning Kamp 				return (ESRCH);
1349d93f860cSPoul-Henning Kamp 			}
1350f591779bSSeigo Tanimura 		}
1351ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13522e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1353628d2653SJohn Baldwin 			PROC_LOCK(p);
1354628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1355628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1356628d2653SJohn Baldwin 				continue;
1357628d2653SJohn Baldwin 			}
1358e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
135933a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1360628d2653SJohn Baldwin 				continue;
1361628d2653SJohn Baldwin 			}
1362f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1363d93f860cSPoul-Henning Kamp 				nfound++;
136433a9ed9dSJohn Baldwin 				if (sig)
13652c42a146SMarcel Moolenaar 					psignal(p, sig);
1366628d2653SJohn Baldwin 			}
136733a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1368d93f860cSPoul-Henning Kamp 		}
1369f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1370d93f860cSPoul-Henning Kamp 	}
1371d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1372d93f860cSPoul-Henning Kamp }
1373d93f860cSPoul-Henning Kamp 
1374d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1375df8bae1dSRodney W. Grimes struct kill_args {
1376df8bae1dSRodney W. Grimes 	int	pid;
1377df8bae1dSRodney W. Grimes 	int	signum;
1378df8bae1dSRodney W. Grimes };
1379d2d3e875SBruce Evans #endif
1380fb99ab88SMatthew Dillon /*
1381fb99ab88SMatthew Dillon  * MPSAFE
1382fb99ab88SMatthew Dillon  */
1383df8bae1dSRodney W. Grimes /* ARGSUSED */
138426f9a767SRodney W. Grimes int
1385b40ce416SJulian Elischer kill(td, uap)
1386b40ce416SJulian Elischer 	register struct thread *td;
1387df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1388df8bae1dSRodney W. Grimes {
1389df8bae1dSRodney W. Grimes 	register struct proc *p;
139090af4afaSJohn Baldwin 	int error;
1391df8bae1dSRodney W. Grimes 
13926c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1393df8bae1dSRodney W. Grimes 		return (EINVAL);
1394fb99ab88SMatthew Dillon 
1395df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1396df8bae1dSRodney W. Grimes 		/* kill single process */
139790af4afaSJohn Baldwin 		if ((p = pfind(uap->pid)) == NULL)
139890af4afaSJohn Baldwin 			return (ESRCH);
139990af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
140090af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1401df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1402628d2653SJohn Baldwin 		PROC_UNLOCK(p);
140390af4afaSJohn Baldwin 		return (error);
1404df8bae1dSRodney W. Grimes 	}
1405df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1406df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
140790af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1408df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
140990af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1410df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
141190af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1412df8bae1dSRodney W. Grimes 	}
141390af4afaSJohn Baldwin 	/* NOTREACHED */
1414df8bae1dSRodney W. Grimes }
1415df8bae1dSRodney W. Grimes 
1416df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1417d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1418df8bae1dSRodney W. Grimes struct okillpg_args {
1419df8bae1dSRodney W. Grimes 	int	pgid;
1420df8bae1dSRodney W. Grimes 	int	signum;
1421df8bae1dSRodney W. Grimes };
1422d2d3e875SBruce Evans #endif
1423fb99ab88SMatthew Dillon /*
1424fb99ab88SMatthew Dillon  * MPSAFE
1425fb99ab88SMatthew Dillon  */
1426df8bae1dSRodney W. Grimes /* ARGSUSED */
142726f9a767SRodney W. Grimes int
1428b40ce416SJulian Elischer okillpg(td, uap)
1429b40ce416SJulian Elischer 	struct thread *td;
1430df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1431df8bae1dSRodney W. Grimes {
1432df8bae1dSRodney W. Grimes 
14336c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1434df8bae1dSRodney W. Grimes 		return (EINVAL);
143590af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1436df8bae1dSRodney W. Grimes }
1437df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1438df8bae1dSRodney W. Grimes 
1439df8bae1dSRodney W. Grimes /*
1440df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1441df8bae1dSRodney W. Grimes  */
1442df8bae1dSRodney W. Grimes void
14432c42a146SMarcel Moolenaar gsignal(pgid, sig)
14442c42a146SMarcel Moolenaar 	int pgid, sig;
1445df8bae1dSRodney W. Grimes {
1446df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1447df8bae1dSRodney W. Grimes 
1448f591779bSSeigo Tanimura 	if (pgid != 0) {
1449ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1450f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1451ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1452f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14532c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1454f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1455f591779bSSeigo Tanimura 		}
1456f591779bSSeigo Tanimura 	}
1457df8bae1dSRodney W. Grimes }
1458df8bae1dSRodney W. Grimes 
1459df8bae1dSRodney W. Grimes /*
1460df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1461df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1462df8bae1dSRodney W. Grimes  */
1463df8bae1dSRodney W. Grimes void
14642c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1465df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14662c42a146SMarcel Moolenaar 	int sig, checkctty;
1467df8bae1dSRodney W. Grimes {
1468df8bae1dSRodney W. Grimes 	register struct proc *p;
1469df8bae1dSRodney W. Grimes 
1470628d2653SJohn Baldwin 	if (pgrp) {
1471f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1472628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1473628d2653SJohn Baldwin 			PROC_LOCK(p);
1474df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14752c42a146SMarcel Moolenaar 				psignal(p, sig);
1476628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1477628d2653SJohn Baldwin 		}
1478628d2653SJohn Baldwin 	}
1479df8bae1dSRodney W. Grimes }
1480df8bae1dSRodney W. Grimes 
1481df8bae1dSRodney W. Grimes /*
14821bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1483df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1484df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1485356861dbSMatthew Dillon  *
1486356861dbSMatthew Dillon  * MPSAFE
1487df8bae1dSRodney W. Grimes  */
1488df8bae1dSRodney W. Grimes void
14891bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1490df8bae1dSRodney W. Grimes {
14911bf4700bSJeff Roberson 	struct sigacts *ps;
14921bf4700bSJeff Roberson 	struct proc *p;
14939dde3bc9SDavid Xu 	siginfo_t siginfo;
14949dde3bc9SDavid Xu 	int error;
14951bf4700bSJeff Roberson 
14961bf4700bSJeff Roberson 	p = td->td_proc;
14979dde3bc9SDavid Xu 	if (td->td_flags & TDF_SA) {
14987eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
14999dde3bc9SDavid Xu 			thread_user_enter(p, td);
1500628d2653SJohn Baldwin 		PROC_LOCK(p);
15019dde3bc9SDavid Xu 		if (td->td_mailbox) {
15029dde3bc9SDavid Xu 			SIGDELSET(td->td_sigmask, sig);
15039dde3bc9SDavid Xu 			mtx_lock_spin(&sched_lock);
15049dde3bc9SDavid Xu 			/*
15059dde3bc9SDavid Xu 			 * Force scheduling an upcall, so UTS has chance to
15069dde3bc9SDavid Xu 			 * process the signal before thread runs again in
15079dde3bc9SDavid Xu 			 * userland.
15089dde3bc9SDavid Xu 			 */
15099dde3bc9SDavid Xu 			if (td->td_upcall)
15109dde3bc9SDavid Xu 				td->td_upcall->ku_flags |= KUF_DOUPCALL;
15119dde3bc9SDavid Xu 			mtx_unlock_spin(&sched_lock);
1512432b45deSDavid Xu 		} else {
1513432b45deSDavid Xu 			/* UTS caused a sync signal */
15141fc434dcSDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
15151fc434dcSDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
15161fc434dcSDavid Xu 			sigexit(td, sig);
15179dde3bc9SDavid Xu 		}
15189dde3bc9SDavid Xu 	} else {
15199dde3bc9SDavid Xu 		PROC_LOCK(p);
15209dde3bc9SDavid Xu 	}
1521ef3dab76STim J. Robbins 	ps = p->p_sigacts;
152290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
152390af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15244093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1525df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1526df8bae1dSRodney W. Grimes #ifdef KTRACE
1527374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1528374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15294093529dSJeff Roberson 			    &td->td_sigmask, code);
1530df8bae1dSRodney W. Grimes #endif
15319dde3bc9SDavid Xu 		if (!(td->td_flags & TDF_SA))
15329dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15339dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15344093529dSJeff Roberson 				&td->td_sigmask, code);
15359dde3bc9SDavid Xu 		else {
15364b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15379dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
15389dde3bc9SDavid Xu 			PROC_UNLOCK(p);
15399dde3bc9SDavid Xu 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
15409dde3bc9SDavid Xu 			    sizeof(siginfo));
15419dde3bc9SDavid Xu 			PROC_LOCK(p);
1542432b45deSDavid Xu 			/* UTS memory corrupted */
15439dde3bc9SDavid Xu 			if (error)
15449dde3bc9SDavid Xu 				sigexit(td, SIGILL);
15459dde3bc9SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
15469dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15479dde3bc9SDavid Xu 		}
15484093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15492c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15504093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15512c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1552289ccde0SPeter Wemm 			/*
15538f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1554289ccde0SPeter Wemm 			 */
155590af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15562c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15572c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
155890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15592c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1560dedc04feSPeter Wemm 		}
156190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15626f841fb7SMarcel Moolenaar 	} else {
156390af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15646626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15652c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1566c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1567df8bae1dSRodney W. Grimes 	}
1568628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1569df8bae1dSRodney W. Grimes }
1570df8bae1dSRodney W. Grimes 
15714093529dSJeff Roberson static struct thread *
15724093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15734093529dSJeff Roberson {
15743074d1b4SDavid Xu 	struct thread *td, *signal_td;
15754093529dSJeff Roberson 
15764093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15774093529dSJeff Roberson 
15784093529dSJeff Roberson 	/*
15793074d1b4SDavid Xu 	 * First find a thread in sigwait state and signal belongs to
15803074d1b4SDavid Xu 	 * its wait set. POSIX's arguments is that speed of delivering signal
15813074d1b4SDavid Xu 	 * to sigwait thread is faster than delivering signal to user stack.
15823074d1b4SDavid Xu 	 * If we can not find sigwait thread, then find the first thread in
15833074d1b4SDavid Xu 	 * the proc that doesn't have this signal masked, an exception is
15843074d1b4SDavid Xu 	 * if current thread is sending signal to its process, and it does not
15853074d1b4SDavid Xu 	 * mask the signal, it should get the signal, this is another fast
15863074d1b4SDavid Xu 	 * way to deliver signal.
15874093529dSJeff Roberson 	 */
15883074d1b4SDavid Xu 	signal_td = NULL;
1589a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15903074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15913074d1b4SDavid Xu 		if (td->td_waitset != NULL &&
1592a9a48d68SDavid Xu 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1593a9a48d68SDavid Xu 				mtx_unlock_spin(&sched_lock);
15944093529dSJeff Roberson 				return (td);
1595a9a48d68SDavid Xu 		}
15963074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15973074d1b4SDavid Xu 			if (td == curthread)
15983074d1b4SDavid Xu 				signal_td = curthread;
15993074d1b4SDavid Xu 			else if (signal_td == NULL)
16003074d1b4SDavid Xu 				signal_td = td;
16013074d1b4SDavid Xu 		}
16023074d1b4SDavid Xu 	}
16033074d1b4SDavid Xu 	if (signal_td == NULL)
16043074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1605a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
16063074d1b4SDavid Xu 	return (signal_td);
16074093529dSJeff Roberson }
16084093529dSJeff Roberson 
1609df8bae1dSRodney W. Grimes /*
1610df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1611df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1612df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1613df8bae1dSRodney W. Grimes  *
1614df8bae1dSRodney W. Grimes  * Exceptions:
1615df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1616df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1617df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1618df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1619df8bae1dSRodney W. Grimes  *
1620df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
162190af4afaSJohn Baldwin  *
162290af4afaSJohn Baldwin  * MPSAFE
1623df8bae1dSRodney W. Grimes  */
1624df8bae1dSRodney W. Grimes void
16254093529dSJeff Roberson psignal(struct proc *p, int sig)
1626df8bae1dSRodney W. Grimes {
1627b40ce416SJulian Elischer 	struct thread *td;
16284093529dSJeff Roberson 	int prop;
16294093529dSJeff Roberson 
163041b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
163141b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
163241b3077aSJacques Vidrine 
16334093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
16344093529dSJeff Roberson 	prop = sigprop(sig);
16354093529dSJeff Roberson 
16364093529dSJeff Roberson 	/*
16374093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16384093529dSJeff Roberson 	 */
16394093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16404093529dSJeff Roberson 
1641c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16424093529dSJeff Roberson }
16434093529dSJeff Roberson 
164490af4afaSJohn Baldwin /*
164590af4afaSJohn Baldwin  * MPSAFE
164690af4afaSJohn Baldwin  */
16474093529dSJeff Roberson void
1648c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16494093529dSJeff Roberson {
16509dde3bc9SDavid Xu 	sigset_t saved;
16519dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16529dde3bc9SDavid Xu 
16539dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16549dde3bc9SDavid Xu 		saved = p->p_siglist;
1655c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16569dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16579dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
16589dde3bc9SDavid Xu 			return;
16599dde3bc9SDavid Xu 		else {
16609dde3bc9SDavid Xu 			/* pending set changed */
16619dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
16629dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
16639dde3bc9SDavid Xu 		}
16649dde3bc9SDavid Xu 	}
16659dde3bc9SDavid Xu }
16669dde3bc9SDavid Xu 
16679dde3bc9SDavid Xu static void
1668c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target)
16699dde3bc9SDavid Xu {
16704093529dSJeff Roberson 	struct proc *p;
16714093529dSJeff Roberson 	register sig_t action;
16724093529dSJeff Roberson 	sigset_t *siglist;
16734093529dSJeff Roberson 	struct thread *td0;
1674e602ba25SJulian Elischer 	register int prop;
167590af4afaSJohn Baldwin 	struct sigacts *ps;
1676df8bae1dSRodney W. Grimes 
167741b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
167841b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
16794093529dSJeff Roberson 
16804093529dSJeff Roberson 	p = td->td_proc;
168190af4afaSJohn Baldwin 	ps = p->p_sigacts;
16822c42a146SMarcel Moolenaar 
1683628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1684cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1685cb679c38SJonathan Lemon 
16862c42a146SMarcel Moolenaar 	prop = sigprop(sig);
16874093529dSJeff Roberson 
16884093529dSJeff Roberson 	/*
168914b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
169014b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
169114b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
16924093529dSJeff Roberson 	 */
16933074d1b4SDavid Xu 	if (target == SIGTARGET_TD) {
16943074d1b4SDavid Xu 		siglist = &td->td_siglist;
16953074d1b4SDavid Xu 	} else {
16963074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig))
16973074d1b4SDavid Xu 			siglist = &td->td_siglist;
16983074d1b4SDavid Xu 		else if (td->td_waitset != NULL &&
16993074d1b4SDavid Xu 			SIGISMEMBER(*(td->td_waitset), sig))
17003074d1b4SDavid Xu 			siglist = &td->td_siglist;
17013074d1b4SDavid Xu 		else
17023074d1b4SDavid Xu 			siglist = &p->p_siglist;
17033074d1b4SDavid Xu 	}
17044093529dSJeff Roberson 
1705df8bae1dSRodney W. Grimes 	/*
17062a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
17072a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
17082a024a2bSSean Eric Fagan 	 * a chance, as well.
1709df8bae1dSRodney W. Grimes 	 */
1710b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1711df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1712b40ce416SJulian Elischer 	} else {
1713df8bae1dSRodney W. Grimes 		/*
1714df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1715df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
171690af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1717df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1718df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1719df8bae1dSRodney W. Grimes 		 */
172090af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
172190af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
172290af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
172390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1724df8bae1dSRodney W. Grimes 			return;
172590af4afaSJohn Baldwin 		}
17263074d1b4SDavid Xu 		if (((td->td_waitset == NULL) &&
17273074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig)) ||
17283074d1b4SDavid Xu 		    ((td->td_waitset != NULL) &&
17293074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig) &&
17303074d1b4SDavid Xu 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1731df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
173290af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1733df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1734df8bae1dSRodney W. Grimes 		else
1735df8bae1dSRodney W. Grimes 			action = SIG_DFL;
173690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1737df8bae1dSRodney W. Grimes 	}
1738df8bae1dSRodney W. Grimes 
17394093529dSJeff Roberson 	if (prop & SA_CONT) {
17401d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17414093529dSJeff Roberson 		/*
17424093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17434093529dSJeff Roberson 		 * the proc's siglist.
17444093529dSJeff Roberson 		 */
1745a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17464093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17474093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1748a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17494093529dSJeff Roberson 	}
1750df8bae1dSRodney W. Grimes 
1751df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1752df8bae1dSRodney W. Grimes 		/*
1753df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1754df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1755df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1756df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1757df8bae1dSRodney W. Grimes 		 */
1758e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1759e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1760e602ba25SJulian Elischer 		    (action == SIG_DFL))
1761df8bae1dSRodney W. Grimes 		        return;
17621d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1763a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17644093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17654093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1766a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17676933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1768df8bae1dSRodney W. Grimes 	}
17693074d1b4SDavid Xu 
17704093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17714093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17723074d1b4SDavid Xu 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
17733074d1b4SDavid Xu 	    action != SIG_HOLD) {
17743074d1b4SDavid Xu 		td->td_waitset = NULL;
17753074d1b4SDavid Xu 	}
17763074d1b4SDavid Xu 
17775312b1c7SDavid Xu 	/*
17785312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17795312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17805312b1c7SDavid Xu 	 */
17815312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17825312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17835312b1c7SDavid Xu 		return;
1784df8bae1dSRodney W. Grimes 	/*
1785e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1786e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1787e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1788e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1789e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1790e602ba25SJulian Elischer 	 * We try do the per-process part here.
1791df8bae1dSRodney W. Grimes 	 */
1792e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1793e602ba25SJulian Elischer 		/*
1794e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1795e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1796e602ba25SJulian Elischer 		 */
1797e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1798e602ba25SJulian Elischer 			/*
1799e602ba25SJulian Elischer 			 * The traced process is already stopped,
1800e602ba25SJulian Elischer 			 * so no further action is necessary.
1801e602ba25SJulian Elischer 			 * No signal can restart us.
1802e602ba25SJulian Elischer 			 */
1803e602ba25SJulian Elischer 			goto out;
18041c32c37cSJohn Baldwin 		}
1805b40ce416SJulian Elischer 
1806e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1807df8bae1dSRodney W. Grimes 			/*
1808e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1809e602ba25SJulian Elischer 			 * It will die elsewhere.
1810e602ba25SJulian Elischer 			 * All threads must be restarted.
1811df8bae1dSRodney W. Grimes 			 */
1812e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1813e602ba25SJulian Elischer 			goto runfast;
1814e602ba25SJulian Elischer 		}
1815e602ba25SJulian Elischer 
1816e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1817e602ba25SJulian Elischer 			/*
1818e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18194093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18201d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1821e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18224093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1823e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1824e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1825e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1826e602ba25SJulian Elischer 			 */
18271279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18286933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1829e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18304093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1831e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1832e602ba25SJulian Elischer 				/*
1833e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1834e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1835e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1836e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1837e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1838e602ba25SJulian Elischer 				 * process, we need to make sure that the
1839e602ba25SJulian Elischer 				 * single thread is runnable asap.
1840e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1841e602ba25SJulian Elischer 				 */
1842e602ba25SJulian Elischer 				goto runfast;
1843e602ba25SJulian Elischer 			}
1844e602ba25SJulian Elischer 			/*
1845e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1846e602ba25SJulian Elischer 			 */
1847e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
184804774f23SJulian Elischer 			thread_unsuspend(p);
1849e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1850e602ba25SJulian Elischer 			goto out;
1851e602ba25SJulian Elischer 		}
1852e602ba25SJulian Elischer 
1853e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1854e602ba25SJulian Elischer 			/*
1855e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1856e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
185704774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1858e602ba25SJulian Elischer 			 */
18591279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18604093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1861e602ba25SJulian Elischer 			goto out;
1862e602ba25SJulian Elischer 		}
1863e602ba25SJulian Elischer 
1864e602ba25SJulian Elischer 		/*
1865e602ba25SJulian Elischer 		 * All other kinds of signals:
1866e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1867e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1868e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
186904774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1870e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1871e602ba25SJulian Elischer 		 */
1872e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
187344f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
187444f3b092SJohn Baldwin 			sleepq_abort(td);
1875e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1876df8bae1dSRodney W. Grimes 		goto out;
1877df8bae1dSRodney W. Grimes 		/*
18789a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
18799a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
1880df8bae1dSRodney W. Grimes 		 */
1881e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
188235c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
188335c32a76SDavid Xu 			!(prop & SA_STOP)) {
1884721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18854093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1886721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1887df8bae1dSRodney W. Grimes 			goto out;
188804774f23SJulian Elischer 		}
188935c32a76SDavid Xu 		if (prop & SA_STOP) {
189035c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
189135c32a76SDavid Xu 				goto out;
1892e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1893e574e444SDavid Xu 			p->p_xstat = sig;
189435c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
18954093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
18964093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1897062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1898062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
18994093529dSJeff Roberson 					thread_suspend_one(td0);
1900062cf543SDavid Xu 				} else if (td != td0) {
1901062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1902062cf543SDavid Xu 				}
190335c32a76SDavid Xu 			}
1904e574e444SDavid Xu 			thread_stopped(p);
19054093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1906e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
19074093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
19084093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
19094093529dSJeff Roberson 			}
191035c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
191135c32a76SDavid Xu 			goto out;
191235c32a76SDavid Xu 		}
1913721e5910SJulian Elischer 		else
1914df8bae1dSRodney W. Grimes 			goto runfast;
1915df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1916e602ba25SJulian Elischer 	} else {
1917e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19184093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1919e602ba25SJulian Elischer 		goto out;
1920e602ba25SJulian Elischer 	}
1921e602ba25SJulian Elischer 
1922b40ce416SJulian Elischer 	/*
1923e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1924e602ba25SJulian Elischer 	 * running threads.
1925b40ce416SJulian Elischer 	 */
1926e602ba25SJulian Elischer 
1927e602ba25SJulian Elischer runfast:
1928aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19294093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1930e602ba25SJulian Elischer 	thread_unsuspend(p);
1931e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1932e602ba25SJulian Elischer out:
1933e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1934e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1935e602ba25SJulian Elischer }
1936e602ba25SJulian Elischer 
1937e602ba25SJulian Elischer /*
1938e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1939e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1940e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1941e602ba25SJulian Elischer  */
1942e602ba25SJulian Elischer static void
19434093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1944e602ba25SJulian Elischer {
1945e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1946e602ba25SJulian Elischer 	register int prop;
1947e602ba25SJulian Elischer 
19488b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1949aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1950e602ba25SJulian Elischer 	prop = sigprop(sig);
1951e602ba25SJulian Elischer 	/*
1952aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1953e602ba25SJulian Elischer 	 * killed in this lifetime.
1954e602ba25SJulian Elischer 	 */
1955e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1956e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1957e602ba25SJulian Elischer 			td->td_priority = PUSER;
1958b40ce416SJulian Elischer 		}
1959b40ce416SJulian Elischer 	}
196071fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1961e602ba25SJulian Elischer 		/*
1962e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1963e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1964e602ba25SJulian Elischer 		 * be noticed when the process returns through
1965e602ba25SJulian Elischer 		 * trap() or syscall().
1966e602ba25SJulian Elischer 		 */
196744f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1968aa0fa334SJulian Elischer 			return;
1969e602ba25SJulian Elischer 		/*
1970e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1971e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1972e602ba25SJulian Elischer 		 * for its parent.
1973e602ba25SJulian Elischer 		 */
1974e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1975e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1976aa0fa334SJulian Elischer 		} else {
1977aa0fa334SJulian Elischer 
1978df8bae1dSRodney W. Grimes 			/*
1979e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1980e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1981e602ba25SJulian Elischer 			 * be awakened.
1982df8bae1dSRodney W. Grimes 			 */
1983e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19841d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19854093529dSJeff Roberson 				/*
19864093529dSJeff Roberson 				 * It may be on either list in this state.
19874093529dSJeff Roberson 				 * Remove from both for now.
19884093529dSJeff Roberson 				 */
19894093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1990aa0fa334SJulian Elischer 				return;
1991df8bae1dSRodney W. Grimes 			}
1992df8bae1dSRodney W. Grimes 
1993aa0fa334SJulian Elischer 			/*
1994aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1995aa0fa334SJulian Elischer 			 */
199644f3b092SJohn Baldwin 			if (td->td_priority > PUSER)
1997aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1998aa0fa334SJulian Elischer 		}
199944f3b092SJohn Baldwin 		sleepq_abort(td);
2000aa0fa334SJulian Elischer 	}
2001aa0fa334SJulian Elischer #ifdef SMP
2002aa0fa334SJulian Elischer 	  else {
2003df8bae1dSRodney W. Grimes 		/*
2004e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
2005df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2006df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2007df8bae1dSRodney W. Grimes 		 */
200844f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
2009e602ba25SJulian Elischer 			forward_signal(td);
2010aa0fa334SJulian Elischer 	  }
20113163861cSTor Egge #endif
20126caa8a15SJohn Baldwin }
2013df8bae1dSRodney W. Grimes 
20144cc9f52fSRobert Drehmel void
20154cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
20164cc9f52fSRobert Drehmel {
20174cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
20184cc9f52fSRobert Drehmel 
201930a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20204cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
20214cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
20224cc9f52fSRobert Drehmel 
20234cc9f52fSRobert Drehmel 	p->p_xstat = sig;
20244cc9f52fSRobert Drehmel 	PROC_LOCK(p->p_pptr);
20254cc9f52fSRobert Drehmel 	psignal(p->p_pptr, SIGCHLD);
20264cc9f52fSRobert Drehmel 	PROC_UNLOCK(p->p_pptr);
20274cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
20284cc9f52fSRobert Drehmel 	stop(p);	/* uses schedlock too eventually */
20294cc9f52fSRobert Drehmel 	thread_suspend_one(td);
20304cc9f52fSRobert Drehmel 	PROC_UNLOCK(p);
20314cc9f52fSRobert Drehmel 	DROP_GIANT();
203229bcc451SJeff Roberson 	mi_switch(SW_INVOL);
20334cc9f52fSRobert Drehmel 	mtx_unlock_spin(&sched_lock);
20344cc9f52fSRobert Drehmel 	PICKUP_GIANT();
20354cc9f52fSRobert Drehmel }
20364cc9f52fSRobert Drehmel 
2037df8bae1dSRodney W. Grimes /*
2038df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2039df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2040df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2041df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2042df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2043628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2044df8bae1dSRodney W. Grimes  * sequence is
2045df8bae1dSRodney W. Grimes  *
2046e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
20472c42a146SMarcel Moolenaar  *		postsig(sig);
2048df8bae1dSRodney W. Grimes  */
20496711f10fSJohn Baldwin static int
2050e602ba25SJulian Elischer issignal(td)
2051e602ba25SJulian Elischer 	struct thread *td;
2052df8bae1dSRodney W. Grimes {
2053e602ba25SJulian Elischer 	struct proc *p;
205490af4afaSJohn Baldwin 	struct sigacts *ps;
20554093529dSJeff Roberson 	sigset_t sigpending;
2056062cf543SDavid Xu 	int sig, prop;
2057062cf543SDavid Xu 	struct thread *td0;
2058df8bae1dSRodney W. Grimes 
2059e602ba25SJulian Elischer 	p = td->td_proc;
206090af4afaSJohn Baldwin 	ps = p->p_sigacts;
206190af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2062628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2063df8bae1dSRodney W. Grimes 	for (;;) {
20642a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
20652a024a2bSSean Eric Fagan 
20664093529dSJeff Roberson 		sigpending = td->td_siglist;
20674093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
20684093529dSJeff Roberson 
2069df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
20704093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
20714093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2072df8bae1dSRodney W. Grimes 			return (0);
20734093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
20742a024a2bSSean Eric Fagan 
2075047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2076047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2077047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2078047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2079047aa39bSRobert Watson 		}
20802a024a2bSSean Eric Fagan 
2081df8bae1dSRodney W. Grimes 		/*
2082df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2083df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2084df8bae1dSRodney W. Grimes 		 */
208590af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
20864093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
2087df8bae1dSRodney W. Grimes 			continue;
2088df8bae1dSRodney W. Grimes 		}
2089df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2090df8bae1dSRodney W. Grimes 			/*
2091d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2092df8bae1dSRodney W. Grimes 			 */
209390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
20944cc9f52fSRobert Drehmel 			ptracestop(td, sig);
2095628d2653SJohn Baldwin 			PROC_LOCK(p);
209690af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2097df8bae1dSRodney W. Grimes 
2098df8bae1dSRodney W. Grimes 			/*
2099df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
2100df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
2101df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
2102df8bae1dSRodney W. Grimes 			 */
21034093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
21042c42a146SMarcel Moolenaar 			sig = p->p_xstat;
21052c42a146SMarcel Moolenaar 			if (sig == 0)
2106df8bae1dSRodney W. Grimes 				continue;
2107df8bae1dSRodney W. Grimes 
2108df8bae1dSRodney W. Grimes 			/*
21098d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
21108d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
21118d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
21128d542cb5SDavid E. O'Brien 			 */
21138d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
21148d542cb5SDavid E. O'Brien 				continue;
21158d542cb5SDavid E. O'Brien 
21168d542cb5SDavid E. O'Brien 			/*
21174093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
21181d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
2119df8bae1dSRodney W. Grimes 			 */
21204093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
21214093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
2122df8bae1dSRodney W. Grimes 				continue;
21234093529dSJeff Roberson 			signotify(td);
2124df8bae1dSRodney W. Grimes 		}
2125df8bae1dSRodney W. Grimes 
21268d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
21278d542cb5SDavid E. O'Brien 
2128df8bae1dSRodney W. Grimes 		/*
2129df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2130df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2131df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2132df8bae1dSRodney W. Grimes 		 */
2133d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2134df8bae1dSRodney W. Grimes 
2135d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2136df8bae1dSRodney W. Grimes 			/*
2137df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2138df8bae1dSRodney W. Grimes 			 */
2139df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2140df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2141df8bae1dSRodney W. Grimes 				/*
2142df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2143df8bae1dSRodney W. Grimes 				 * in init? XXX
2144df8bae1dSRodney W. Grimes 				 */
2145d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
21462c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2147df8bae1dSRodney W. Grimes #endif
2148df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2149df8bae1dSRodney W. Grimes 			}
2150df8bae1dSRodney W. Grimes 			/*
2151df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2152df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2153df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2154df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2155df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2156df8bae1dSRodney W. Grimes 			 */
2157df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2158df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2159df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2160df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2161df8bae1dSRodney W. Grimes 					break;	/* == ignore */
216290af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
216390af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
216490af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2165e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
21662c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2167721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2168062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2169062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2170062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2171062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2172062cf543SDavid Xu 						thread_suspend_one(td0);
2173062cf543SDavid Xu 					} else if (td != td0) {
2174062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2175062cf543SDavid Xu 					}
2176062cf543SDavid Xu 				}
2177e574e444SDavid Xu 				thread_stopped(p);
217871fad9fdSJulian Elischer 				thread_suspend_one(td);
2179721e5910SJulian Elischer 				PROC_UNLOCK(p);
2180721e5910SJulian Elischer 				DROP_GIANT();
218129bcc451SJeff Roberson 				mi_switch(SW_INVOL);
2182721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2183721e5910SJulian Elischer 				PICKUP_GIANT();
2184721e5910SJulian Elischer 				PROC_LOCK(p);
218590af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2186df8bae1dSRodney W. Grimes 				break;
218721b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2188df8bae1dSRodney W. Grimes 				/*
2189df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2190df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2191df8bae1dSRodney W. Grimes 				 */
2192df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2193df8bae1dSRodney W. Grimes 			} else
21942c42a146SMarcel Moolenaar 				return (sig);
2195df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2196df8bae1dSRodney W. Grimes 
2197d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2198df8bae1dSRodney W. Grimes 			/*
2199df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2200df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2201df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2202df8bae1dSRodney W. Grimes 			 */
2203df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2204df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2205df8bae1dSRodney W. Grimes 				printf("issignal\n");
2206df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2207df8bae1dSRodney W. Grimes 
2208df8bae1dSRodney W. Grimes 		default:
2209df8bae1dSRodney W. Grimes 			/*
2210df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2211df8bae1dSRodney W. Grimes 			 * postsig() process it.
2212df8bae1dSRodney W. Grimes 			 */
22132c42a146SMarcel Moolenaar 			return (sig);
2214df8bae1dSRodney W. Grimes 		}
22154093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2216df8bae1dSRodney W. Grimes 	}
2217df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2218df8bae1dSRodney W. Grimes }
2219df8bae1dSRodney W. Grimes 
2220df8bae1dSRodney W. Grimes /*
2221df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2222df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
22235b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
22245b3047d5SJohn Baldwin  * lock held.
2225df8bae1dSRodney W. Grimes  */
22265b3047d5SJohn Baldwin static void
2227e574e444SDavid Xu stop(struct proc *p)
2228df8bae1dSRodney W. Grimes {
2229df8bae1dSRodney W. Grimes 
2230628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22311279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2232df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
223301609114SAlfred Perlstein 	wakeup(p->p_pptr);
2234df8bae1dSRodney W. Grimes }
2235df8bae1dSRodney W. Grimes 
223690af4afaSJohn Baldwin /*
223790af4afaSJohn Baldwin  * MPSAFE
223890af4afaSJohn Baldwin  */
2239e574e444SDavid Xu void
2240e574e444SDavid Xu thread_stopped(struct proc *p)
2241e574e444SDavid Xu {
2242e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
224390af4afaSJohn Baldwin 	struct sigacts *ps;
2244e574e444SDavid Xu 	int n;
2245e574e444SDavid Xu 
2246e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2247e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2248e574e444SDavid Xu 	n = p->p_suspcount;
2249e574e444SDavid Xu 	if (p == p1)
2250e574e444SDavid Xu 		n++;
2251e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2252e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2253e574e444SDavid Xu 		stop(p);
2254e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
225590af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
225690af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
225790af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
225890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2259e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
226090af4afaSJohn Baldwin 		} else
226190af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2262e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2263e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2264e574e444SDavid Xu 	}
2265e574e444SDavid Xu }
2266e574e444SDavid Xu 
2267df8bae1dSRodney W. Grimes /*
2268df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2269df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2270df8bae1dSRodney W. Grimes  */
2271df8bae1dSRodney W. Grimes void
22722c42a146SMarcel Moolenaar postsig(sig)
22732c42a146SMarcel Moolenaar 	register int sig;
2274df8bae1dSRodney W. Grimes {
2275b40ce416SJulian Elischer 	struct thread *td = curthread;
2276b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2277628d2653SJohn Baldwin 	struct sigacts *ps;
22782c42a146SMarcel Moolenaar 	sig_t action;
22792c42a146SMarcel Moolenaar 	sigset_t returnmask;
22802c42a146SMarcel Moolenaar 	int code;
2281df8bae1dSRodney W. Grimes 
22822c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
22835526d2d9SEivind Eklund 
22842ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2285628d2653SJohn Baldwin 	ps = p->p_sigacts;
228690af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
22874093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
22882c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2289df8bae1dSRodney W. Grimes #ifdef KTRACE
2290374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
22915e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
22924093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2293df8bae1dSRodney W. Grimes #endif
2294047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2295047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2296047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2297047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2298047aa39bSRobert Watson 	}
22992a024a2bSSean Eric Fagan 
2300432b45deSDavid Xu 	if (!(td->td_flags & TDF_SA && td->td_mailbox) &&
2301432b45deSDavid Xu 	    action == SIG_DFL) {
2302df8bae1dSRodney W. Grimes 		/*
2303df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2304df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2305df8bae1dSRodney W. Grimes 		 */
230690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2307b40ce416SJulian Elischer 		sigexit(td, sig);
2308df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2309df8bae1dSRodney W. Grimes 	} else {
2310432b45deSDavid Xu 		if (td->td_flags & TDF_SA && td->td_mailbox) {
2311432b45deSDavid Xu 			if (sig == SIGKILL) {
2312432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2313432b45deSDavid Xu 				sigexit(td, sig);
2314432b45deSDavid Xu 			}
2315432b45deSDavid Xu 		}
2316432b45deSDavid Xu 
2317df8bae1dSRodney W. Grimes 		/*
2318df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2319df8bae1dSRodney W. Grimes 		 */
23204093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23215526d2d9SEivind Eklund 		    ("postsig action"));
2322df8bae1dSRodney W. Grimes 		/*
2323df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2324645682fdSLuoqi Chen 		 * occurrences of this signal.
2325df8bae1dSRodney W. Grimes 		 *
2326645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2327df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2328645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2329df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2330df8bae1dSRodney W. Grimes 		 */
23315e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23324093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23335e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2334df8bae1dSRodney W. Grimes 		} else
23354093529dSJeff Roberson 			returnmask = td->td_sigmask;
23362c42a146SMarcel Moolenaar 
23374093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23382c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23394093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23402c42a146SMarcel Moolenaar 
23412c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2342289ccde0SPeter Wemm 			/*
23438f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2344289ccde0SPeter Wemm 			 */
234590af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23462c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23472c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
234890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23492c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2350dedc04feSPeter Wemm 		}
2351df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23522c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2353df8bae1dSRodney W. Grimes 			code = 0;
2354df8bae1dSRodney W. Grimes 		} else {
23556626c604SJulian Elischer 			code = p->p_code;
23566626c604SJulian Elischer 			p->p_code = 0;
23576626c604SJulian Elischer 			p->p_sig = 0;
2358df8bae1dSRodney W. Grimes 		}
2359432b45deSDavid Xu 		if (td->td_flags & TDF_SA && td->td_mailbox)
236058a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
236158a3c273SJeff Roberson 		else
236258a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
236358a3c273SJeff Roberson 			    &returnmask, code);
2364df8bae1dSRodney W. Grimes 	}
2365df8bae1dSRodney W. Grimes }
2366df8bae1dSRodney W. Grimes 
2367df8bae1dSRodney W. Grimes /*
2368df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2369df8bae1dSRodney W. Grimes  */
237026f9a767SRodney W. Grimes void
2371df8bae1dSRodney W. Grimes killproc(p, why)
2372df8bae1dSRodney W. Grimes 	struct proc *p;
2373df8bae1dSRodney W. Grimes 	char *why;
2374df8bae1dSRodney W. Grimes {
23759081e5e8SJohn Baldwin 
23769081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
23770384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
23780384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2379729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2380b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2381df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2382df8bae1dSRodney W. Grimes }
2383df8bae1dSRodney W. Grimes 
2384df8bae1dSRodney W. Grimes /*
2385df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2386df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2387df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2388df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2389df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2390df8bae1dSRodney W. Grimes  * does not return.
239190af4afaSJohn Baldwin  *
239290af4afaSJohn Baldwin  * MPSAFE
2393df8bae1dSRodney W. Grimes  */
239426f9a767SRodney W. Grimes void
2395b40ce416SJulian Elischer sigexit(td, sig)
2396b40ce416SJulian Elischer 	struct thread *td;
23972c42a146SMarcel Moolenaar 	int sig;
2398df8bae1dSRodney W. Grimes {
2399b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2400df8bae1dSRodney W. Grimes 
2401628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2402df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
24032c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
24042c42a146SMarcel Moolenaar 		p->p_sig = sig;
2405c364e17eSAndrey A. Chernov 		/*
2406c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2407c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2408c364e17eSAndrey A. Chernov 		 * these messages.)
2409c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
24104ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2411c364e17eSAndrey A. Chernov 		 */
2412b40ce416SJulian Elischer 		if (coredump(td) == 0)
24132c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
241457308494SJoerg Wunsch 		if (kern_logsigexit)
241557308494SJoerg Wunsch 			log(LOG_INFO,
241657308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24173d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24189c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24192c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24202c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
24214ae89b95SJohn Baldwin 	} else
2422628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2423b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2424df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2425df8bae1dSRodney W. Grimes }
2426df8bae1dSRodney W. Grimes 
2427c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2428c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2429c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2430c5edb423SSean Eric Fagan 
2431c5edb423SSean Eric Fagan /*
2432c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2433c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2434c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2435c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2436c5edb423SSean Eric Fagan  *	%P	process id (pid)
2437c5edb423SSean Eric Fagan  *	%U	user id (uid)
2438c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2439c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2440c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2441c5edb423SSean Eric Fagan  */
2442c5edb423SSean Eric Fagan 
2443fca666a1SJulian Elischer static char *
2444c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24458b43b535SAlfred Perlstein 	const char *name;
24468b43b535SAlfred Perlstein 	uid_t uid;
24478b43b535SAlfred Perlstein 	pid_t pid;
24488b43b535SAlfred Perlstein {
24498b43b535SAlfred Perlstein 	const char *format, *appendstr;
2450c5edb423SSean Eric Fagan 	char *temp;
2451c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24528b43b535SAlfred Perlstein 	size_t i, l, n;
2453c5edb423SSean Eric Fagan 
24548b43b535SAlfred Perlstein 	format = corefilename;
24558b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24560bfe2990SEivind Eklund 	if (temp == NULL)
24578b43b535SAlfred Perlstein 		return (NULL);
2458ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2459c5edb423SSean Eric Fagan 		switch (format[i]) {
2460c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2461c5edb423SSean Eric Fagan 			i++;
2462c5edb423SSean Eric Fagan 			switch (format[i]) {
2463c5edb423SSean Eric Fagan 			case '%':
24648b43b535SAlfred Perlstein 				appendstr = "%";
2465c5edb423SSean Eric Fagan 				break;
2466c5edb423SSean Eric Fagan 			case 'N':	/* process name */
24678b43b535SAlfred Perlstein 				appendstr = name;
2468c5edb423SSean Eric Fagan 				break;
2469c5edb423SSean Eric Fagan 			case 'P':	/* process id */
24708b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
24718b43b535SAlfred Perlstein 				appendstr = buf;
2472c5edb423SSean Eric Fagan 				break;
2473c5edb423SSean Eric Fagan 			case 'U':	/* user id */
24748b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
24758b43b535SAlfred Perlstein 				appendstr = buf;
2476c5edb423SSean Eric Fagan 				break;
2477c5edb423SSean Eric Fagan 			default:
24788b43b535SAlfred Perlstein 				appendstr = "";
24798b43b535SAlfred Perlstein 			  	log(LOG_ERR,
24808b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
24818b43b535SAlfred Perlstein 				    format[i], format);
2482c5edb423SSean Eric Fagan 			}
24838b43b535SAlfred Perlstein 			l = strlen(appendstr);
24848b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
24858b43b535SAlfred Perlstein 				goto toolong;
24868b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
24878b43b535SAlfred Perlstein 			n += l;
2488c5edb423SSean Eric Fagan 			break;
2489c5edb423SSean Eric Fagan 		default:
2490c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2491c5edb423SSean Eric Fagan 		}
2492c5edb423SSean Eric Fagan 	}
24938b43b535SAlfred Perlstein 	if (format[i] != '\0')
24948b43b535SAlfred Perlstein 		goto toolong;
24958b43b535SAlfred Perlstein 	return (temp);
24968b43b535SAlfred Perlstein toolong:
24978b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
24988b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
24998b43b535SAlfred Perlstein 	free(temp, M_TEMP);
25008b43b535SAlfred Perlstein 	return (NULL);
2501c5edb423SSean Eric Fagan }
2502c5edb423SSean Eric Fagan 
2503df8bae1dSRodney W. Grimes /*
2504fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2505fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2506fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2507fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2508fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2509fca666a1SJulian Elischer  */
2510fca666a1SJulian Elischer 
2511fca666a1SJulian Elischer static int
2512b40ce416SJulian Elischer coredump(struct thread *td)
2513fca666a1SJulian Elischer {
2514b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2515fca666a1SJulian Elischer 	register struct vnode *vp;
25169c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
251706ae1e91SMatthew Dillon 	struct flock lf;
2518fca666a1SJulian Elischer 	struct nameidata nd;
2519fca666a1SJulian Elischer 	struct vattr vattr;
2520c447f5b2SRobert Watson 	int error, error1, flags, locked;
2521f2a2857bSKirk McKusick 	struct mount *mp;
2522fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2523fca666a1SJulian Elischer 	off_t limit;
2524fca666a1SJulian Elischer 
25254ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2526628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2527fca666a1SJulian Elischer 
2528628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2529628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2530fca666a1SJulian Elischer 		return (EFAULT);
2531628d2653SJohn Baldwin 	}
2532fca666a1SJulian Elischer 
2533fca666a1SJulian Elischer 	/*
253435a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
253535a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
253635a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
253735a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
253835a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
253935a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2540fca666a1SJulian Elischer 	 */
254191d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2542628d2653SJohn Baldwin 	PROC_UNLOCK(p);
254391d5354aSJohn Baldwin 	if (limit == 0)
254491d5354aSJohn Baldwin 		return (EFBIG);
254535a2598fSSean Eric Fagan 
25464ae89b95SJohn Baldwin 	mtx_lock(&Giant);
2547f2a2857bSKirk McKusick restart:
25489c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
25494ae89b95SJohn Baldwin 	if (name == NULL) {
25504ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2551ccdbd10cSPeter Pentchev 		return (EINVAL);
25524ae89b95SJohn Baldwin 	}
2553b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2554e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25557c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2556fca666a1SJulian Elischer 	free(name, M_TEMP);
25574ae89b95SJohn Baldwin 	if (error) {
25584ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
2559fca666a1SJulian Elischer 		return (error);
25604ae89b95SJohn Baldwin 	}
2561762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2562fca666a1SJulian Elischer 	vp = nd.ni_vp;
256306ae1e91SMatthew Dillon 
2564832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2565832dafadSDon Lewis 	if (vp->v_type != VREG ||
2566832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2567832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2568832dafadSDon Lewis 		error = EFAULT;
25694ae89b95SJohn Baldwin 		goto out;
2570832dafadSDon Lewis 	}
2571832dafadSDon Lewis 
2572b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
257306ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
257406ae1e91SMatthew Dillon 	lf.l_start = 0;
257506ae1e91SMatthew Dillon 	lf.l_len = 0;
257606ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2577c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
257806ae1e91SMatthew Dillon 
257906ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
258006ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
258136bbf86bSRobert Watson 		if (locked)
258206ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2583b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2584f2a2857bSKirk McKusick 			return (error);
2585f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2586f2a2857bSKirk McKusick 			return (error);
2587f2a2857bSKirk McKusick 		goto restart;
2588f2a2857bSKirk McKusick 	}
2589fca666a1SJulian Elischer 
2590fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2591fca666a1SJulian Elischer 	vattr.va_size = 0;
259288b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2593b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2594b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
259588b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2596628d2653SJohn Baldwin 	PROC_LOCK(p);
2597fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2598628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2599fca666a1SJulian Elischer 
2600fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2601b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2602fca666a1SJulian Elischer 	  ENOSYS;
2603fca666a1SJulian Elischer 
2604c447f5b2SRobert Watson 	if (locked) {
260506ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
260606ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2607c447f5b2SRobert Watson 	}
2608f2a2857bSKirk McKusick 	vn_finished_write(mp);
26094ae89b95SJohn Baldwin out:
2610b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
26114ae89b95SJohn Baldwin 	mtx_unlock(&Giant);
2612fca666a1SJulian Elischer 	if (error == 0)
2613fca666a1SJulian Elischer 		error = error1;
2614fca666a1SJulian Elischer 	return (error);
2615fca666a1SJulian Elischer }
2616fca666a1SJulian Elischer 
2617fca666a1SJulian Elischer /*
2618df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2619df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2620df8bae1dSRodney W. Grimes  */
2621d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2622df8bae1dSRodney W. Grimes struct nosys_args {
2623df8bae1dSRodney W. Grimes 	int	dummy;
2624df8bae1dSRodney W. Grimes };
2625d2d3e875SBruce Evans #endif
2626fb99ab88SMatthew Dillon /*
2627fb99ab88SMatthew Dillon  * MPSAFE
2628fb99ab88SMatthew Dillon  */
2629df8bae1dSRodney W. Grimes /* ARGSUSED */
263026f9a767SRodney W. Grimes int
2631b40ce416SJulian Elischer nosys(td, args)
2632b40ce416SJulian Elischer 	struct thread *td;
2633df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2634df8bae1dSRodney W. Grimes {
2635b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2636b40ce416SJulian Elischer 
2637628d2653SJohn Baldwin 	PROC_LOCK(p);
2638df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2639628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2640f5216b9aSBruce Evans 	return (ENOSYS);
2641df8bae1dSRodney W. Grimes }
2642831d27a9SDon Lewis 
2643831d27a9SDon Lewis /*
264448f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2645831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2646831d27a9SDon Lewis  */
2647831d27a9SDon Lewis void
2648f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2649f1320723SAlfred Perlstein 	struct sigio **sigiop;
26502c42a146SMarcel Moolenaar 	int sig, checkctty;
2651831d27a9SDon Lewis {
2652f1320723SAlfred Perlstein 	struct sigio *sigio;
2653831d27a9SDon Lewis 
2654f1320723SAlfred Perlstein 	SIGIO_LOCK();
2655f1320723SAlfred Perlstein 	sigio = *sigiop;
2656f1320723SAlfred Perlstein 	if (sigio == NULL) {
2657f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2658f1320723SAlfred Perlstein 		return;
2659f1320723SAlfred Perlstein 	}
2660831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2661628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
26622b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
26632c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2664628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2665831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2666831d27a9SDon Lewis 		struct proc *p;
2667831d27a9SDon Lewis 
2668f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2669628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2670628d2653SJohn Baldwin 			PROC_LOCK(p);
26712b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2672831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
26732c42a146SMarcel Moolenaar 				psignal(p, sig);
2674628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2675628d2653SJohn Baldwin 		}
2676f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2677831d27a9SDon Lewis 	}
2678f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2679831d27a9SDon Lewis }
2680cb679c38SJonathan Lemon 
2681cb679c38SJonathan Lemon static int
2682cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2683cb679c38SJonathan Lemon {
2684cb679c38SJonathan Lemon 	struct proc *p = curproc;
2685cb679c38SJonathan Lemon 
2686cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2687cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2688cb679c38SJonathan Lemon 
2689628d2653SJohn Baldwin 	PROC_LOCK(p);
2690cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2691628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2692cb679c38SJonathan Lemon 
2693cb679c38SJonathan Lemon 	return (0);
2694cb679c38SJonathan Lemon }
2695cb679c38SJonathan Lemon 
2696cb679c38SJonathan Lemon static void
2697cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2698cb679c38SJonathan Lemon {
2699cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2700cb679c38SJonathan Lemon 
2701628d2653SJohn Baldwin 	PROC_LOCK(p);
2702e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2703628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2704cb679c38SJonathan Lemon }
2705cb679c38SJonathan Lemon 
2706cb679c38SJonathan Lemon /*
2707cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2708cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2709cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2710cb679c38SJonathan Lemon  * isn't worth the trouble.
2711cb679c38SJonathan Lemon  */
2712cb679c38SJonathan Lemon static int
2713cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2714cb679c38SJonathan Lemon {
2715cb679c38SJonathan Lemon 
2716cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2717cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2718cb679c38SJonathan Lemon 
2719cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2720cb679c38SJonathan Lemon 			kn->kn_data++;
2721cb679c38SJonathan Lemon 	}
2722cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2723cb679c38SJonathan Lemon }
272490af4afaSJohn Baldwin 
272590af4afaSJohn Baldwin struct sigacts *
272690af4afaSJohn Baldwin sigacts_alloc(void)
272790af4afaSJohn Baldwin {
272890af4afaSJohn Baldwin 	struct sigacts *ps;
272990af4afaSJohn Baldwin 
273090af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
273190af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
273290af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
273390af4afaSJohn Baldwin 	return (ps);
273490af4afaSJohn Baldwin }
273590af4afaSJohn Baldwin 
273690af4afaSJohn Baldwin void
273790af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
273890af4afaSJohn Baldwin {
273990af4afaSJohn Baldwin 
274090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
274190af4afaSJohn Baldwin 	ps->ps_refcnt--;
274290af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
274390af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
274490af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
274590af4afaSJohn Baldwin 	} else
274690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
274790af4afaSJohn Baldwin }
274890af4afaSJohn Baldwin 
274990af4afaSJohn Baldwin struct sigacts *
275090af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
275190af4afaSJohn Baldwin {
275290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
275390af4afaSJohn Baldwin 	ps->ps_refcnt++;
275490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
275590af4afaSJohn Baldwin 	return (ps);
275690af4afaSJohn Baldwin }
275790af4afaSJohn Baldwin 
275890af4afaSJohn Baldwin void
275990af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
276090af4afaSJohn Baldwin {
276190af4afaSJohn Baldwin 
276290af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
276390af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
276490af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
276590af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
276690af4afaSJohn Baldwin }
276790af4afaSJohn Baldwin 
276890af4afaSJohn Baldwin int
276990af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
277090af4afaSJohn Baldwin {
277190af4afaSJohn Baldwin 	int shared;
277290af4afaSJohn Baldwin 
277390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
277490af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
277590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
277690af4afaSJohn Baldwin 	return (shared);
277790af4afaSJohn Baldwin }
2778