xref: /freebsd/sys/kern/kern_sig.c (revision 91d5354a2ce810d848eca6ecf9da1027aeb2be6d)
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>
666caa8a15SJohn Baldwin #include <sys/smp.h>
67df8bae1dSRodney W. Grimes #include <sys/stat.h>
681005a129SJohn Baldwin #include <sys/sx.h>
698f19eb88SIan Dowse #include <sys/syscallsubr.h>
70c87e2930SDavid Greenman #include <sys/sysctl.h>
71854dc8c2SJohn Baldwin #include <sys/sysent.h>
72854dc8c2SJohn Baldwin #include <sys/syslog.h>
73854dc8c2SJohn Baldwin #include <sys/sysproto.h>
7406ae1e91SMatthew Dillon #include <sys/unistd.h>
75854dc8c2SJohn Baldwin #include <sys/wait.h>
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes #include <machine/cpu.h>
78df8bae1dSRodney W. Grimes 
7923eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
8023eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
8123eeeff7SPeter Wemm #endif
8223eeeff7SPeter Wemm 
836f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
846f841fb7SMarcel Moolenaar 
854d77a549SAlfred Perlstein static int	coredump(struct thread *);
864d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
879c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
886711f10fSJohn Baldwin static int	issignal(struct thread *p);
894d77a549SAlfred Perlstein static int	sigprop(int sig);
904d77a549SAlfred Perlstein static void	stop(struct proc *);
914093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
92cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
93cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
94cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
954093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
96a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
97a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
98c197abc4SMike Makonnen static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
99cb679c38SJonathan Lemon 
100cb679c38SJonathan Lemon struct filterops sig_filtops =
101cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
102cb679c38SJonathan Lemon 
10357308494SJoerg Wunsch static int	kern_logsigexit = 1;
1043d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1053d177f46SBill Fumerola     &kern_logsigexit, 0,
1063d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10757308494SJoerg Wunsch 
1082b87b6d4SRobert Watson /*
1092b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1102b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1112b87b6d4SRobert Watson  * in the right situations.
1122b87b6d4SRobert Watson  */
1132b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1142b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1152b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1162b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1172b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1182b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1192b87b6d4SRobert Watson 
12022d4b0fbSJohn Polstra int sugid_coredump;
1213d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1223d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
123c87e2930SDavid Greenman 
124e5a28db9SPaul Saab static int	do_coredump = 1;
125e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
126e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
127e5a28db9SPaul Saab 
1282c42a146SMarcel Moolenaar /*
1292c42a146SMarcel Moolenaar  * Signal properties and actions.
1302c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1312c42a146SMarcel Moolenaar  * according to the following properties:
1322c42a146SMarcel Moolenaar  */
1332c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1342c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1352c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1362c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1372c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1382c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1392c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
140da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
141df8bae1dSRodney W. Grimes 
1422c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
143da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
144da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
145da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1462c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
149da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1502c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
151da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1522c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1532c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1542c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
155da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
156da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
157da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
158da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
159da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
160da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
161da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
162da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
163da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
164da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
165da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1662c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1672c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
168da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
169da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
170da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
171da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
172da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
173da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1742c42a146SMarcel Moolenaar };
1752c42a146SMarcel Moolenaar 
176fbbeeb6cSBruce Evans /*
177fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
178fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
179fbbeeb6cSBruce Evans  * action, the process stops in issignal().
180e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
181fbbeeb6cSBruce Evans  *
18233510ef1SBruce Evans  * MP SAFE.
183fbbeeb6cSBruce Evans  */
184fbbeeb6cSBruce Evans int
185e602ba25SJulian Elischer cursig(struct thread *td)
186fbbeeb6cSBruce Evans {
187c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
18890af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
189179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1904093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
191fbbeeb6cSBruce Evans }
192fbbeeb6cSBruce Evans 
19379065dbaSBruce Evans /*
19479065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1954093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1964093529dSJeff Roberson  * unmasked in td_sigmask.
19779065dbaSBruce Evans  */
19879065dbaSBruce Evans void
1994093529dSJeff Roberson signotify(struct thread *td)
20079065dbaSBruce Evans {
2014093529dSJeff Roberson 	struct proc *p;
2029dde3bc9SDavid Xu 	sigset_t set, saved;
2034093529dSJeff Roberson 
2044093529dSJeff Roberson 	p = td->td_proc;
20579065dbaSBruce Evans 
20679065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2074093529dSJeff Roberson 
2084093529dSJeff Roberson 	/*
2094093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2104093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2114093529dSJeff Roberson 	 */
2124093529dSJeff Roberson 	set = p->p_siglist;
2139dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
2149dde3bc9SDavid Xu 		saved = p->p_siglist;
2154093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2164093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2174093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2184093529dSJeff Roberson 
2198b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
22079065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2214093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
22279065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
22379065dbaSBruce Evans 	}
2249dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2259dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
2269dde3bc9SDavid Xu 			return;
2279dde3bc9SDavid Xu 		else {
2289dde3bc9SDavid Xu 			/* pending set changed */
2299dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
2309dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
2319dde3bc9SDavid Xu 		}
2329dde3bc9SDavid Xu 	}
2338b94a061SJohn Baldwin }
2348b94a061SJohn Baldwin 
2358b94a061SJohn Baldwin int
2368b94a061SJohn Baldwin sigonstack(size_t sp)
2378b94a061SJohn Baldwin {
238a30ec4b9SDavid Xu 	struct thread *td = curthread;
2398b94a061SJohn Baldwin 
240a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
2418b94a061SJohn Baldwin #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
242a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
243a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
244a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
2458b94a061SJohn Baldwin #else
246a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
2478b94a061SJohn Baldwin #endif
2488b94a061SJohn Baldwin 	    : 0);
2498b94a061SJohn Baldwin }
25079065dbaSBruce Evans 
2516f841fb7SMarcel Moolenaar static __inline int
2526f841fb7SMarcel Moolenaar sigprop(int sig)
2532c42a146SMarcel Moolenaar {
2546f841fb7SMarcel Moolenaar 
2552c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2562c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2572c42a146SMarcel Moolenaar 	return (0);
258df8bae1dSRodney W. Grimes }
2592c42a146SMarcel Moolenaar 
2604093529dSJeff Roberson int
2616f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2622c42a146SMarcel Moolenaar {
2632c42a146SMarcel Moolenaar 	int i;
2642c42a146SMarcel Moolenaar 
2656f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2662c42a146SMarcel Moolenaar 		if (set->__bits[i])
2672c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
268df8bae1dSRodney W. Grimes 	return (0);
269df8bae1dSRodney W. Grimes }
270df8bae1dSRodney W. Grimes 
2712c42a146SMarcel Moolenaar /*
2728f19eb88SIan Dowse  * kern_sigaction
2732c42a146SMarcel Moolenaar  * sigaction
27423eeeff7SPeter Wemm  * freebsd4_sigaction
2752c42a146SMarcel Moolenaar  * osigaction
27625d6dc06SJohn Baldwin  *
27725d6dc06SJohn Baldwin  * MPSAFE
2782c42a146SMarcel Moolenaar  */
2798f19eb88SIan Dowse int
28023eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2818f19eb88SIan Dowse 	struct thread *td;
2822c42a146SMarcel Moolenaar 	register int sig;
2832c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
28423eeeff7SPeter Wemm 	int flags;
285df8bae1dSRodney W. Grimes {
28690af4afaSJohn Baldwin 	struct sigacts *ps;
2874093529dSJeff Roberson 	struct thread *td0;
2888f19eb88SIan Dowse 	struct proc *p = td->td_proc;
289df8bae1dSRodney W. Grimes 
2902899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2912c42a146SMarcel Moolenaar 		return (EINVAL);
2922c42a146SMarcel Moolenaar 
293628d2653SJohn Baldwin 	PROC_LOCK(p);
294628d2653SJohn Baldwin 	ps = p->p_sigacts;
29590af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2962c42a146SMarcel Moolenaar 	if (oact) {
2972c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2982c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2992c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
3002c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
3012c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
3022c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
3032c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
3042c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
3052c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
3062c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
3072c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
3082c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
3092c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
31090af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
3112c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
31290af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
3132c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
3142c42a146SMarcel Moolenaar 	}
3152c42a146SMarcel Moolenaar 	if (act) {
3162c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
317628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
31890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
319628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3202c42a146SMarcel Moolenaar 			return (EINVAL);
321628d2653SJohn Baldwin 		}
3222c42a146SMarcel Moolenaar 
323df8bae1dSRodney W. Grimes 		/*
324df8bae1dSRodney W. Grimes 		 * Change setting atomically.
325df8bae1dSRodney W. Grimes 		 */
3262c42a146SMarcel Moolenaar 
3272c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3282c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3292c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
330aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
331aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
33280f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
33380f42b55SIan Dowse 		} else {
33480f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3352c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3362c42a146SMarcel Moolenaar 		}
3372c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3382c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
339df8bae1dSRodney W. Grimes 		else
3402c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3412c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3422c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
343df8bae1dSRodney W. Grimes 		else
3442c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3452c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3462c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3471e41c1b5SSteven Wallace 		else
3482c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3492c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3502c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
351289ccde0SPeter Wemm 		else
3522c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
353df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3542c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3552c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
356df8bae1dSRodney W. Grimes 		else
3578c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
358df8bae1dSRodney W. Grimes #endif
3592c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3602c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
36190af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3626626c604SJulian Elischer 			else
36390af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
364ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
365245f17d4SJoerg Wunsch 				/*
3662c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3672c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3682c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3692c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
370245f17d4SJoerg Wunsch 				 */
371245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
37290af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3736626c604SJulian Elischer 				else
37490af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
375245f17d4SJoerg Wunsch 			} else
37690af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
377ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
37890af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
379ba1551caSIan Dowse 			else
38090af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
381df8bae1dSRodney W. Grimes 		}
382df8bae1dSRodney W. Grimes 		/*
38390af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3842c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
38590af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3862c42a146SMarcel Moolenaar 		 * have to restart the process.
387df8bae1dSRodney W. Grimes 		 */
3882c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3892c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3902c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3919dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
3929dde3bc9SDavid Xu 			     SIGISMEMBER(p->p_siglist, sig)) {
3939dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
3949dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
3959dde3bc9SDavid Xu 			}
3962c42a146SMarcel Moolenaar 			/* never to be seen again */
3971d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
398a9a48d68SDavid Xu 			mtx_lock_spin(&sched_lock);
3994093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
4004093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
401a9a48d68SDavid Xu 			mtx_unlock_spin(&sched_lock);
4022c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4032c42a146SMarcel Moolenaar 				/* easier in psignal */
40490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
40590af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
406645682fdSLuoqi Chen 		} else {
40790af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
4082c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
40990af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
4102c42a146SMarcel Moolenaar 			else
41190af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
4122c42a146SMarcel Moolenaar 		}
41323eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
41423eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
41523eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
41623eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
41723eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
41823eeeff7SPeter Wemm 		else
41923eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
42023eeeff7SPeter Wemm #endif
421e8ebc08fSPeter Wemm #ifdef COMPAT_43
422645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
42323eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
42423eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
425645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
426645682fdSLuoqi Chen 		else
427645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
428e8ebc08fSPeter Wemm #endif
429df8bae1dSRodney W. Grimes 	}
43090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
431628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4322c42a146SMarcel Moolenaar 	return (0);
4332c42a146SMarcel Moolenaar }
4342c42a146SMarcel Moolenaar 
4352c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4362c42a146SMarcel Moolenaar struct sigaction_args {
4372c42a146SMarcel Moolenaar 	int	sig;
4382c42a146SMarcel Moolenaar 	struct	sigaction *act;
4392c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4402c42a146SMarcel Moolenaar };
4412c42a146SMarcel Moolenaar #endif
442fb99ab88SMatthew Dillon /*
443fb99ab88SMatthew Dillon  * MPSAFE
444fb99ab88SMatthew Dillon  */
4452c42a146SMarcel Moolenaar int
446b40ce416SJulian Elischer sigaction(td, uap)
447b40ce416SJulian Elischer 	struct thread *td;
4482c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4492c42a146SMarcel Moolenaar {
4502c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4512c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4522c42a146SMarcel Moolenaar 	int error;
4532c42a146SMarcel Moolenaar 
4546f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4556f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4562c42a146SMarcel Moolenaar 	if (actp) {
4576f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4582c42a146SMarcel Moolenaar 		if (error)
45944443757STim J. Robbins 			return (error);
4602c42a146SMarcel Moolenaar 	}
4618f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
46225d6dc06SJohn Baldwin 	if (oactp && !error)
4636f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4642c42a146SMarcel Moolenaar 	return (error);
4652c42a146SMarcel Moolenaar }
4662c42a146SMarcel Moolenaar 
46723eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
46823eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
46923eeeff7SPeter Wemm struct freebsd4_sigaction_args {
47023eeeff7SPeter Wemm 	int	sig;
47123eeeff7SPeter Wemm 	struct	sigaction *act;
47223eeeff7SPeter Wemm 	struct	sigaction *oact;
47323eeeff7SPeter Wemm };
47423eeeff7SPeter Wemm #endif
47523eeeff7SPeter Wemm /*
47623eeeff7SPeter Wemm  * MPSAFE
47723eeeff7SPeter Wemm  */
47823eeeff7SPeter Wemm int
47923eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
48023eeeff7SPeter Wemm 	struct thread *td;
48123eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
48223eeeff7SPeter Wemm {
48323eeeff7SPeter Wemm 	struct sigaction act, oact;
48423eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
48523eeeff7SPeter Wemm 	int error;
48623eeeff7SPeter Wemm 
48723eeeff7SPeter Wemm 
48823eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
48923eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
49023eeeff7SPeter Wemm 	if (actp) {
49123eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
49223eeeff7SPeter Wemm 		if (error)
49344443757STim J. Robbins 			return (error);
49423eeeff7SPeter Wemm 	}
49523eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
496a14e1189SJohn Baldwin 	if (oactp && !error)
49723eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
49823eeeff7SPeter Wemm 	return (error);
49923eeeff7SPeter Wemm }
50023eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
50123eeeff7SPeter Wemm 
50231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5032c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5042c42a146SMarcel Moolenaar struct osigaction_args {
5052c42a146SMarcel Moolenaar 	int	signum;
5062c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
5072c42a146SMarcel Moolenaar 	struct	osigaction *osa;
5082c42a146SMarcel Moolenaar };
5092c42a146SMarcel Moolenaar #endif
510fb99ab88SMatthew Dillon /*
511fb99ab88SMatthew Dillon  * MPSAFE
512fb99ab88SMatthew Dillon  */
5132c42a146SMarcel Moolenaar int
514b40ce416SJulian Elischer osigaction(td, uap)
515b40ce416SJulian Elischer 	struct thread *td;
5162c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
5172c42a146SMarcel Moolenaar {
5182c42a146SMarcel Moolenaar 	struct osigaction sa;
5192c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5202c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5212c42a146SMarcel Moolenaar 	int error;
5222c42a146SMarcel Moolenaar 
5236f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5246f841fb7SMarcel Moolenaar 		return (EINVAL);
525fb99ab88SMatthew Dillon 
5266f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5276f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
528fb99ab88SMatthew Dillon 
5292c42a146SMarcel Moolenaar 	if (nsap) {
5306f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5312c42a146SMarcel Moolenaar 		if (error)
53244443757STim J. Robbins 			return (error);
5332c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5342c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5352c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5362c42a146SMarcel Moolenaar 	}
53723eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5382c42a146SMarcel Moolenaar 	if (osap && !error) {
5392c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5402c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5412c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5426f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5432c42a146SMarcel Moolenaar 	}
5442c42a146SMarcel Moolenaar 	return (error);
5452c42a146SMarcel Moolenaar }
54623eeeff7SPeter Wemm 
54723eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
54823eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
54923eeeff7SPeter Wemm int
55023eeeff7SPeter Wemm osigreturn(td, uap)
55123eeeff7SPeter Wemm 	struct thread *td;
55223eeeff7SPeter Wemm 	struct osigreturn_args *uap;
55323eeeff7SPeter Wemm {
55423eeeff7SPeter Wemm 
55523eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
55623eeeff7SPeter Wemm }
55723eeeff7SPeter Wemm #endif
55831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes /*
561df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
562df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
563df8bae1dSRodney W. Grimes  */
564df8bae1dSRodney W. Grimes void
565df8bae1dSRodney W. Grimes siginit(p)
566df8bae1dSRodney W. Grimes 	struct proc *p;
567df8bae1dSRodney W. Grimes {
568df8bae1dSRodney W. Grimes 	register int i;
56990af4afaSJohn Baldwin 	struct sigacts *ps;
570df8bae1dSRodney W. Grimes 
571628d2653SJohn Baldwin 	PROC_LOCK(p);
57290af4afaSJohn Baldwin 	ps = p->p_sigacts;
57390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5742c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5752c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
57690af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
57790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
578628d2653SJohn Baldwin 	PROC_UNLOCK(p);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes /*
582df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
583df8bae1dSRodney W. Grimes  */
584df8bae1dSRodney W. Grimes void
585a30ec4b9SDavid Xu execsigs(struct proc *p)
586df8bae1dSRodney W. Grimes {
587a30ec4b9SDavid Xu 	struct sigacts *ps;
588a30ec4b9SDavid Xu 	int sig;
589a30ec4b9SDavid Xu 	struct thread *td;
590df8bae1dSRodney W. Grimes 
591df8bae1dSRodney W. Grimes 	/*
592df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5934093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
594df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
595df8bae1dSRodney W. Grimes 	 */
5969b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
597a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
598628d2653SJohn Baldwin 	ps = p->p_sigacts;
59990af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
60090af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
60190af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
60290af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
6032c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
6042c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
60590af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
6061d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
6074093529dSJeff Roberson 			/*
6084093529dSJeff Roberson 			 * There is only one thread at this point.
6094093529dSJeff Roberson 			 */
610a30ec4b9SDavid Xu 			SIGDELSET(td->td_siglist, sig);
611df8bae1dSRodney W. Grimes 		}
6122c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
613df8bae1dSRodney W. Grimes 	}
614df8bae1dSRodney W. Grimes 	/*
615df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
616df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
617df8bae1dSRodney W. Grimes 	 */
618a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
619a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
620a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
621a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
62280e907a1SPeter Wemm 	/*
62380e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
62480e907a1SPeter Wemm 	 */
62590af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
626c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
627c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
62890af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
629df8bae1dSRodney W. Grimes }
630df8bae1dSRodney W. Grimes 
631df8bae1dSRodney W. Grimes /*
632e77daab1SJohn Baldwin  * kern_sigprocmask()
6337c8fdcbdSMatthew Dillon  *
634628d2653SJohn Baldwin  *	Manipulate signal mask.
635df8bae1dSRodney W. Grimes  */
636e77daab1SJohn Baldwin int
637e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6384093529dSJeff Roberson 	struct thread *td;
6392c42a146SMarcel Moolenaar 	int how;
6402c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
641645682fdSLuoqi Chen 	int old;
6422c42a146SMarcel Moolenaar {
6432c42a146SMarcel Moolenaar 	int error;
6442c42a146SMarcel Moolenaar 
6454093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6462c42a146SMarcel Moolenaar 	if (oset != NULL)
6474093529dSJeff Roberson 		*oset = td->td_sigmask;
6482c42a146SMarcel Moolenaar 
6492c42a146SMarcel Moolenaar 	error = 0;
6502c42a146SMarcel Moolenaar 	if (set != NULL) {
6512c42a146SMarcel Moolenaar 		switch (how) {
6522c42a146SMarcel Moolenaar 		case SIG_BLOCK:
653645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6544093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6552c42a146SMarcel Moolenaar 			break;
6562c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6574093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6584093529dSJeff Roberson 			signotify(td);
6592c42a146SMarcel Moolenaar 			break;
6602c42a146SMarcel Moolenaar 		case SIG_SETMASK:
661645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
662645682fdSLuoqi Chen 			if (old)
6634093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
664645682fdSLuoqi Chen 			else
6654093529dSJeff Roberson 				td->td_sigmask = *set;
6664093529dSJeff Roberson 			signotify(td);
6672c42a146SMarcel Moolenaar 			break;
6682c42a146SMarcel Moolenaar 		default:
6692c42a146SMarcel Moolenaar 			error = EINVAL;
6702c42a146SMarcel Moolenaar 			break;
6712c42a146SMarcel Moolenaar 		}
6722c42a146SMarcel Moolenaar 	}
6734093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6742c42a146SMarcel Moolenaar 	return (error);
6752c42a146SMarcel Moolenaar }
6762c42a146SMarcel Moolenaar 
6777c8fdcbdSMatthew Dillon /*
678e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6797c8fdcbdSMatthew Dillon  */
6807c8fdcbdSMatthew Dillon 
681d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
682df8bae1dSRodney W. Grimes struct sigprocmask_args {
683df8bae1dSRodney W. Grimes 	int	how;
6842c42a146SMarcel Moolenaar 	const sigset_t *set;
6852c42a146SMarcel Moolenaar 	sigset_t *oset;
686df8bae1dSRodney W. Grimes };
687d2d3e875SBruce Evans #endif
68826f9a767SRodney W. Grimes int
689b40ce416SJulian Elischer sigprocmask(td, uap)
690b40ce416SJulian Elischer 	register struct thread *td;
691df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
692df8bae1dSRodney W. Grimes {
6932c42a146SMarcel Moolenaar 	sigset_t set, oset;
6942c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6952c42a146SMarcel Moolenaar 	int error;
696df8bae1dSRodney W. Grimes 
6976f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6986f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6992c42a146SMarcel Moolenaar 	if (setp) {
7006f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
7012c42a146SMarcel Moolenaar 		if (error)
7022c42a146SMarcel Moolenaar 			return (error);
703df8bae1dSRodney W. Grimes 	}
704e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
7052c42a146SMarcel Moolenaar 	if (osetp && !error) {
7066f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
7072c42a146SMarcel Moolenaar 	}
7082c42a146SMarcel Moolenaar 	return (error);
7092c42a146SMarcel Moolenaar }
7102c42a146SMarcel Moolenaar 
71131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7127c8fdcbdSMatthew Dillon /*
7137c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
7147c8fdcbdSMatthew Dillon  */
7152c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7162c42a146SMarcel Moolenaar struct osigprocmask_args {
7172c42a146SMarcel Moolenaar 	int	how;
7182c42a146SMarcel Moolenaar 	osigset_t mask;
7192c42a146SMarcel Moolenaar };
7202c42a146SMarcel Moolenaar #endif
7212c42a146SMarcel Moolenaar int
722b40ce416SJulian Elischer osigprocmask(td, uap)
723b40ce416SJulian Elischer 	register struct thread *td;
7242c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7252c42a146SMarcel Moolenaar {
7262c42a146SMarcel Moolenaar 	sigset_t set, oset;
7272c42a146SMarcel Moolenaar 	int error;
7282c42a146SMarcel Moolenaar 
7292c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
730e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
731b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
732df8bae1dSRodney W. Grimes 	return (error);
733df8bae1dSRodney W. Grimes }
73431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
735df8bae1dSRodney W. Grimes 
736d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
737df8bae1dSRodney W. Grimes struct sigpending_args {
7382c42a146SMarcel Moolenaar 	sigset_t	*set;
739df8bae1dSRodney W. Grimes };
740d2d3e875SBruce Evans #endif
741fb99ab88SMatthew Dillon /*
742fb99ab88SMatthew Dillon  * MPSAFE
743fb99ab88SMatthew Dillon  */
74426f9a767SRodney W. Grimes int
745a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
746a447cd8bSJeff Roberson {
747a447cd8bSJeff Roberson 	siginfo_t info;
748a447cd8bSJeff Roberson 	sigset_t set;
749a447cd8bSJeff Roberson 	int error;
750a447cd8bSJeff Roberson 
751a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
752a447cd8bSJeff Roberson 	if (error)
753a447cd8bSJeff Roberson 		return (error);
754a447cd8bSJeff Roberson 
755a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
756a447cd8bSJeff Roberson 	if (error)
757a447cd8bSJeff Roberson 		return (error);
758a447cd8bSJeff Roberson 
759a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
760a447cd8bSJeff Roberson 	/* Repost if we got an error. */
76118440c7fSJohn Baldwin 	if (error && info.si_signo) {
76218440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
763c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
76418440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
76518440c7fSJohn Baldwin 	}
766a447cd8bSJeff Roberson 	return (error);
767a447cd8bSJeff Roberson }
768a447cd8bSJeff Roberson /*
769a447cd8bSJeff Roberson  * MPSAFE
770a447cd8bSJeff Roberson  */
771a447cd8bSJeff Roberson int
772a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
773a447cd8bSJeff Roberson {
774a447cd8bSJeff Roberson 	struct timespec ts;
775a447cd8bSJeff Roberson 	struct timespec *timeout;
776a447cd8bSJeff Roberson 	sigset_t set;
777a447cd8bSJeff Roberson 	siginfo_t info;
778a447cd8bSJeff Roberson 	int error;
779a447cd8bSJeff Roberson 
780a447cd8bSJeff Roberson 	if (uap->timeout) {
781a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
782a447cd8bSJeff Roberson 		if (error)
783a447cd8bSJeff Roberson 			return (error);
784a447cd8bSJeff Roberson 
785a447cd8bSJeff Roberson 		timeout = &ts;
786a447cd8bSJeff Roberson 	} else
787a447cd8bSJeff Roberson 		timeout = NULL;
788a447cd8bSJeff Roberson 
789a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
790a447cd8bSJeff Roberson 	if (error)
791a447cd8bSJeff Roberson 		return (error);
792a447cd8bSJeff Roberson 
793a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
794a447cd8bSJeff Roberson 	if (error)
795a447cd8bSJeff Roberson 		return (error);
7969dde3bc9SDavid Xu 
797418228dfSDavid Xu 	if (uap->info)
798a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
799a447cd8bSJeff Roberson 	/* Repost if we got an error. */
80018440c7fSJohn Baldwin 	if (error && info.si_signo) {
80118440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
802c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
80318440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
804418228dfSDavid Xu 	} else {
805418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
80618440c7fSJohn Baldwin 	}
807a447cd8bSJeff Roberson 	return (error);
808a447cd8bSJeff Roberson }
809a447cd8bSJeff Roberson 
810a447cd8bSJeff Roberson /*
811a447cd8bSJeff Roberson  * MPSAFE
812a447cd8bSJeff Roberson  */
813a447cd8bSJeff Roberson int
814a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
815a447cd8bSJeff Roberson {
816a447cd8bSJeff Roberson 	siginfo_t info;
817a447cd8bSJeff Roberson 	sigset_t set;
818a447cd8bSJeff Roberson 	int error;
819a447cd8bSJeff Roberson 
820a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
821a447cd8bSJeff Roberson 	if (error)
822a447cd8bSJeff Roberson 		return (error);
823a447cd8bSJeff Roberson 
824a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
825a447cd8bSJeff Roberson 	if (error)
826a447cd8bSJeff Roberson 		return (error);
827a447cd8bSJeff Roberson 
828418228dfSDavid Xu 	if (uap->info)
829a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
830a447cd8bSJeff Roberson 	/* Repost if we got an error. */
83118440c7fSJohn Baldwin 	if (error && info.si_signo) {
83218440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
833c197abc4SMike Makonnen 		tdsignal(td, info.si_signo, SIGTARGET_TD);
83418440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
835418228dfSDavid Xu 	} else {
836418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
83718440c7fSJohn Baldwin 	}
838a447cd8bSJeff Roberson 	return (error);
839a447cd8bSJeff Roberson }
840a447cd8bSJeff Roberson 
841a447cd8bSJeff Roberson static int
8423074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
843a447cd8bSJeff Roberson     struct timespec *timeout)
844a447cd8bSJeff Roberson {
8453074d1b4SDavid Xu 	struct sigacts *ps;
8463074d1b4SDavid Xu 	sigset_t savedmask, sigset;
847a447cd8bSJeff Roberson 	struct proc *p;
848a447cd8bSJeff Roberson 	int error;
849a447cd8bSJeff Roberson 	int sig;
850a447cd8bSJeff Roberson 	int hz;
8513074d1b4SDavid Xu 	int i;
852a447cd8bSJeff Roberson 
853a447cd8bSJeff Roberson 	p = td->td_proc;
854a447cd8bSJeff Roberson 	error = 0;
855a447cd8bSJeff Roberson 	sig = 0;
8563074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
857a447cd8bSJeff Roberson 
858a447cd8bSJeff Roberson 	PROC_LOCK(p);
859a447cd8bSJeff Roberson 	ps = p->p_sigacts;
8603074d1b4SDavid Xu 	savedmask = td->td_sigmask;
8613074d1b4SDavid Xu 
8623074d1b4SDavid Xu again:
8633074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
8643074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
8653074d1b4SDavid Xu 			continue;
8663074d1b4SDavid Xu 		if (SIGISMEMBER(td->td_siglist, i)) {
867418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
868418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8693074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
87090af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
871a447cd8bSJeff Roberson 			sig = cursig(td);
8723074d1b4SDavid Xu 			i = 0;
8733074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8743074d1b4SDavid Xu 		} else if (SIGISMEMBER(p->p_siglist, i)) {
8753074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
8763074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
8773074d1b4SDavid Xu 				wakeup(&p->p_siglist);
8783074d1b4SDavid Xu 			}
8793074d1b4SDavid Xu 			SIGDELSET(p->p_siglist, i);
8803074d1b4SDavid Xu 			SIGADDSET(td->td_siglist, i);
8813074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
8823074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
8833074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
8843074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
8853074d1b4SDavid Xu 			sig = cursig(td);
8863074d1b4SDavid Xu 			i = 0;
8873074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
8883074d1b4SDavid Xu 		}
8893074d1b4SDavid Xu 		if (sig) {
8903074d1b4SDavid Xu 			td->td_sigmask = savedmask;
8913074d1b4SDavid Xu 			signotify(td);
892a447cd8bSJeff Roberson 			goto out;
8933074d1b4SDavid Xu 		}
8943074d1b4SDavid Xu 	}
8953074d1b4SDavid Xu 	if (error)
8963074d1b4SDavid Xu 		goto out;
8973074d1b4SDavid Xu 
8983074d1b4SDavid Xu 	td->td_sigmask = savedmask;
8993074d1b4SDavid Xu 	signotify(td);
9003074d1b4SDavid Xu 	sigset = td->td_siglist;
9013074d1b4SDavid Xu 	SIGSETOR(sigset, p->p_siglist);
9023074d1b4SDavid Xu 	SIGSETAND(sigset, waitset);
9033074d1b4SDavid Xu 	if (!SIGISEMPTY(sigset))
9043074d1b4SDavid Xu 		goto again;
905a447cd8bSJeff Roberson 
906a447cd8bSJeff Roberson 	/*
907a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
908a447cd8bSJeff Roberson 	 * signals.
909a447cd8bSJeff Roberson 	 */
910a447cd8bSJeff Roberson 	if (timeout) {
911a447cd8bSJeff Roberson 		struct timeval tv;
912a447cd8bSJeff Roberson 
913a6ca4808SMike Makonnen 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
914a447cd8bSJeff Roberson 			error = EINVAL;
915a447cd8bSJeff Roberson 			goto out;
916a447cd8bSJeff Roberson 		}
9173074d1b4SDavid Xu 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
9183074d1b4SDavid Xu 			error = EAGAIN;
9193074d1b4SDavid Xu 			goto out;
9203074d1b4SDavid Xu 		}
921a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
922a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
923a447cd8bSJeff Roberson 	} else
924a447cd8bSJeff Roberson 		hz = 0;
925a447cd8bSJeff Roberson 
9263074d1b4SDavid Xu 	td->td_waitset = &waitset;
9273074d1b4SDavid Xu 	error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
9283074d1b4SDavid Xu 	td->td_waitset = NULL;
9293074d1b4SDavid Xu 	if (error == 0) /* surplus wakeup ? */
9303074d1b4SDavid Xu 		error = EINTR;
9313074d1b4SDavid Xu 	goto again;
9329dde3bc9SDavid Xu 
933a447cd8bSJeff Roberson out:
934a447cd8bSJeff Roberson 	if (sig) {
935a447cd8bSJeff Roberson 		sig_t action;
936a447cd8bSJeff Roberson 
9373074d1b4SDavid Xu 		error = 0;
9383074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
939a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
94090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
941a447cd8bSJeff Roberson #ifdef KTRACE
942a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
9433074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
944a447cd8bSJeff Roberson #endif
945a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
946a447cd8bSJeff Roberson 
947a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
948a447cd8bSJeff Roberson 		info->si_signo = sig;
949a447cd8bSJeff Roberson 		info->si_code = 0;
9503074d1b4SDavid Xu 	}
951a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
952a447cd8bSJeff Roberson 	return (error);
953a447cd8bSJeff Roberson }
954a447cd8bSJeff Roberson 
955a447cd8bSJeff Roberson /*
956a447cd8bSJeff Roberson  * MPSAFE
957a447cd8bSJeff Roberson  */
958a447cd8bSJeff Roberson int
959b40ce416SJulian Elischer sigpending(td, uap)
960b40ce416SJulian Elischer 	struct thread *td;
961df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
962df8bae1dSRodney W. Grimes {
963b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
964628d2653SJohn Baldwin 	sigset_t siglist;
965df8bae1dSRodney W. Grimes 
966628d2653SJohn Baldwin 	PROC_LOCK(p);
9671d9c5696SJuli Mallett 	siglist = p->p_siglist;
9684093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
969628d2653SJohn Baldwin 	PROC_UNLOCK(p);
97048e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9712c42a146SMarcel Moolenaar }
9722c42a146SMarcel Moolenaar 
97331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9742c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9752c42a146SMarcel Moolenaar struct osigpending_args {
9762c42a146SMarcel Moolenaar 	int	dummy;
9772c42a146SMarcel Moolenaar };
9782c42a146SMarcel Moolenaar #endif
979fb99ab88SMatthew Dillon /*
980fb99ab88SMatthew Dillon  * MPSAFE
981fb99ab88SMatthew Dillon  */
9822c42a146SMarcel Moolenaar int
983b40ce416SJulian Elischer osigpending(td, uap)
984b40ce416SJulian Elischer 	struct thread *td;
9852c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9862c42a146SMarcel Moolenaar {
987b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9884093529dSJeff Roberson 	sigset_t siglist;
989b40ce416SJulian Elischer 
990628d2653SJohn Baldwin 	PROC_LOCK(p);
9914093529dSJeff Roberson 	siglist = p->p_siglist;
9924093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
993628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9949d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
995df8bae1dSRodney W. Grimes 	return (0);
996df8bae1dSRodney W. Grimes }
99731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
998df8bae1dSRodney W. Grimes 
999df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1000df8bae1dSRodney W. Grimes /*
1001df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1002df8bae1dSRodney W. Grimes  */
1003d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1004df8bae1dSRodney W. Grimes struct osigvec_args {
1005df8bae1dSRodney W. Grimes 	int	signum;
1006df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1007df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1008df8bae1dSRodney W. Grimes };
1009d2d3e875SBruce Evans #endif
1010fb99ab88SMatthew Dillon /*
1011fb99ab88SMatthew Dillon  * MPSAFE
1012fb99ab88SMatthew Dillon  */
1013df8bae1dSRodney W. Grimes /* ARGSUSED */
101426f9a767SRodney W. Grimes int
1015b40ce416SJulian Elischer osigvec(td, uap)
1016b40ce416SJulian Elischer 	struct thread *td;
1017df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1018df8bae1dSRodney W. Grimes {
1019df8bae1dSRodney W. Grimes 	struct sigvec vec;
10202c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
10212c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
10222c42a146SMarcel Moolenaar 	int error;
1023df8bae1dSRodney W. Grimes 
10246f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10256f841fb7SMarcel Moolenaar 		return (EINVAL);
10266f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
10276f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
10282c42a146SMarcel Moolenaar 	if (nsap) {
10296f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
10302c42a146SMarcel Moolenaar 		if (error)
1031df8bae1dSRodney W. Grimes 			return (error);
10322c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
10332c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
10342c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
10352c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1036df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
10372c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
1038df8bae1dSRodney W. Grimes #endif
1039df8bae1dSRodney W. Grimes 	}
10405edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10412c42a146SMarcel Moolenaar 	if (osap && !error) {
10422c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
10432c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
10442c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
10452c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
10462c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
10472c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
10482c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
10492c42a146SMarcel Moolenaar #endif
10506f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10512c42a146SMarcel Moolenaar 	}
10522c42a146SMarcel Moolenaar 	return (error);
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes 
1055d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1056df8bae1dSRodney W. Grimes struct osigblock_args {
1057df8bae1dSRodney W. Grimes 	int	mask;
1058df8bae1dSRodney W. Grimes };
1059d2d3e875SBruce Evans #endif
1060fb99ab88SMatthew Dillon /*
1061fb99ab88SMatthew Dillon  * MPSAFE
1062fb99ab88SMatthew Dillon  */
106326f9a767SRodney W. Grimes int
1064b40ce416SJulian Elischer osigblock(td, uap)
1065b40ce416SJulian Elischer 	register struct thread *td;
1066df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1067df8bae1dSRodney W. Grimes {
1068b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10692c42a146SMarcel Moolenaar 	sigset_t set;
1070df8bae1dSRodney W. Grimes 
10712c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10722c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1073628d2653SJohn Baldwin 	PROC_LOCK(p);
10744093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10754093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1076628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1077df8bae1dSRodney W. Grimes 	return (0);
1078df8bae1dSRodney W. Grimes }
1079df8bae1dSRodney W. Grimes 
1080d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1081df8bae1dSRodney W. Grimes struct osigsetmask_args {
1082df8bae1dSRodney W. Grimes 	int	mask;
1083df8bae1dSRodney W. Grimes };
1084d2d3e875SBruce Evans #endif
1085fb99ab88SMatthew Dillon /*
1086fb99ab88SMatthew Dillon  * MPSAFE
1087fb99ab88SMatthew Dillon  */
108826f9a767SRodney W. Grimes int
1089b40ce416SJulian Elischer osigsetmask(td, uap)
1090b40ce416SJulian Elischer 	struct thread *td;
1091df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1092df8bae1dSRodney W. Grimes {
1093b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10942c42a146SMarcel Moolenaar 	sigset_t set;
1095df8bae1dSRodney W. Grimes 
10962c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10972c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1098628d2653SJohn Baldwin 	PROC_LOCK(p);
10994093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
11004093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
11014093529dSJeff Roberson 	signotify(td);
1102628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1103df8bae1dSRodney W. Grimes 	return (0);
1104df8bae1dSRodney W. Grimes }
1105df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1106df8bae1dSRodney W. Grimes 
1107df8bae1dSRodney W. Grimes /*
1108df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
110997563428SAlexander Kabaev  * in the meantime.
1110b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1111b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1112b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1113df8bae1dSRodney W. Grimes  */
1114d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1115df8bae1dSRodney W. Grimes struct sigsuspend_args {
11162c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1117df8bae1dSRodney W. Grimes };
1118d2d3e875SBruce Evans #endif
1119fb99ab88SMatthew Dillon /*
1120fb99ab88SMatthew Dillon  * MPSAFE
1121fb99ab88SMatthew Dillon  */
1122df8bae1dSRodney W. Grimes /* ARGSUSED */
112326f9a767SRodney W. Grimes int
1124b40ce416SJulian Elischer sigsuspend(td, uap)
1125b40ce416SJulian Elischer 	struct thread *td;
1126df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1127df8bae1dSRodney W. Grimes {
11282c42a146SMarcel Moolenaar 	sigset_t mask;
11292c42a146SMarcel Moolenaar 	int error;
11302c42a146SMarcel Moolenaar 
11316f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11322c42a146SMarcel Moolenaar 	if (error)
11332c42a146SMarcel Moolenaar 		return (error);
11348f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11358f19eb88SIan Dowse }
11368f19eb88SIan Dowse 
11378f19eb88SIan Dowse int
11388f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11398f19eb88SIan Dowse {
11408f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes 	/*
1143645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1144df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1145df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1146df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1147df8bae1dSRodney W. Grimes 	 * to indicate this.
1148df8bae1dSRodney W. Grimes 	 */
1149628d2653SJohn Baldwin 	PROC_LOCK(p);
11504093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11515e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1152645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11534093529dSJeff Roberson 	td->td_sigmask = mask;
11544093529dSJeff Roberson 	signotify(td);
1155b1bf1c3aSJohn Baldwin 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11562c42a146SMarcel Moolenaar 		/* void */;
1157628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11582c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11592c42a146SMarcel Moolenaar 	return (EINTR);
11602c42a146SMarcel Moolenaar }
11612c42a146SMarcel Moolenaar 
116231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
116397563428SAlexander Kabaev /*
116497563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
116597563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
116697563428SAlexander Kabaev  */
11672c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11682c42a146SMarcel Moolenaar struct osigsuspend_args {
11692c42a146SMarcel Moolenaar 	osigset_t mask;
11702c42a146SMarcel Moolenaar };
11712c42a146SMarcel Moolenaar #endif
1172fb99ab88SMatthew Dillon /*
1173fb99ab88SMatthew Dillon  * MPSAFE
1174fb99ab88SMatthew Dillon  */
11752c42a146SMarcel Moolenaar /* ARGSUSED */
11762c42a146SMarcel Moolenaar int
1177b40ce416SJulian Elischer osigsuspend(td, uap)
1178b40ce416SJulian Elischer 	struct thread *td;
11792c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11802c42a146SMarcel Moolenaar {
1181b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1182645682fdSLuoqi Chen 	sigset_t mask;
11832c42a146SMarcel Moolenaar 
1184628d2653SJohn Baldwin 	PROC_LOCK(p);
11854093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11865e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1187645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1188645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11894093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11904093529dSJeff Roberson 	signotify(td);
1191b1bf1c3aSJohn Baldwin 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1192df8bae1dSRodney W. Grimes 		/* void */;
1193628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1194df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1195df8bae1dSRodney W. Grimes 	return (EINTR);
1196df8bae1dSRodney W. Grimes }
119731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1198df8bae1dSRodney W. Grimes 
1199df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1200d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1201df8bae1dSRodney W. Grimes struct osigstack_args {
1202df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1203df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1204df8bae1dSRodney W. Grimes };
1205d2d3e875SBruce Evans #endif
1206fb99ab88SMatthew Dillon /*
1207fb99ab88SMatthew Dillon  * MPSAFE
1208fb99ab88SMatthew Dillon  */
1209df8bae1dSRodney W. Grimes /* ARGSUSED */
121026f9a767SRodney W. Grimes int
1211b40ce416SJulian Elischer osigstack(td, uap)
1212b40ce416SJulian Elischer 	struct thread *td;
1213df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1214df8bae1dSRodney W. Grimes {
12155afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1216fb99ab88SMatthew Dillon 	int error = 0;
1217fb99ab88SMatthew Dillon 
1218d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12195afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12205afe0c99SJohn Baldwin 		if (error)
12215afe0c99SJohn Baldwin 			return (error);
1222df8bae1dSRodney W. Grimes 	}
1223a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12245afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12255afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1226a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1227a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1228a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1229a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12305afe0c99SJohn Baldwin 	}
12315afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12325afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12335afe0c99SJohn Baldwin 
1234fb99ab88SMatthew Dillon 	return (error);
1235df8bae1dSRodney W. Grimes }
1236df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1237df8bae1dSRodney W. Grimes 
1238d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1239df8bae1dSRodney W. Grimes struct sigaltstack_args {
12402c42a146SMarcel Moolenaar 	stack_t	*ss;
12412c42a146SMarcel Moolenaar 	stack_t	*oss;
1242df8bae1dSRodney W. Grimes };
1243d2d3e875SBruce Evans #endif
1244fb99ab88SMatthew Dillon /*
1245fb99ab88SMatthew Dillon  * MPSAFE
1246fb99ab88SMatthew Dillon  */
1247df8bae1dSRodney W. Grimes /* ARGSUSED */
124826f9a767SRodney W. Grimes int
1249b40ce416SJulian Elischer sigaltstack(td, uap)
1250b40ce416SJulian Elischer 	struct thread *td;
1251df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1252df8bae1dSRodney W. Grimes {
12538f19eb88SIan Dowse 	stack_t ss, oss;
12548f19eb88SIan Dowse 	int error;
12558f19eb88SIan Dowse 
12568f19eb88SIan Dowse 	if (uap->ss != NULL) {
12578f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12588f19eb88SIan Dowse 		if (error)
12598f19eb88SIan Dowse 			return (error);
12608f19eb88SIan Dowse 	}
12618f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12628f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12638f19eb88SIan Dowse 	if (error)
12648f19eb88SIan Dowse 		return (error);
12658f19eb88SIan Dowse 	if (uap->oss != NULL)
12668f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12678f19eb88SIan Dowse 	return (error);
12688f19eb88SIan Dowse }
12698f19eb88SIan Dowse 
12708f19eb88SIan Dowse int
12718f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12728f19eb88SIan Dowse {
1273b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1274fb99ab88SMatthew Dillon 	int oonstack;
1275fb99ab88SMatthew Dillon 
1276b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1277d034d459SMarcel Moolenaar 
12788f19eb88SIan Dowse 	if (oss != NULL) {
1279a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1280a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1281d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1282df8bae1dSRodney W. Grimes 	}
1283d034d459SMarcel Moolenaar 
12848f19eb88SIan Dowse 	if (ss != NULL) {
1285a30ec4b9SDavid Xu 		if (oonstack)
1286cf60731bSJohn Baldwin 			return (EPERM);
1287a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1288cf60731bSJohn Baldwin 			return (EINVAL);
12898f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12908f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1291cf60731bSJohn Baldwin 				return (ENOMEM);
1292fb99ab88SMatthew Dillon 			}
1293a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1294a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1295628d2653SJohn Baldwin 		} else {
1296a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1297628d2653SJohn Baldwin 		}
1298d034d459SMarcel Moolenaar 	}
1299cf60731bSJohn Baldwin 	return (0);
1300df8bae1dSRodney W. Grimes }
1301df8bae1dSRodney W. Grimes 
1302d93f860cSPoul-Henning Kamp /*
1303d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1304d93f860cSPoul-Henning Kamp  * cp is calling process.
1305d93f860cSPoul-Henning Kamp  */
130637c84183SPoul-Henning Kamp static int
13079c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
13089c1ab3e0SJohn Baldwin 	register struct thread *td;
13092c42a146SMarcel Moolenaar 	int sig, pgid, all;
1310d93f860cSPoul-Henning Kamp {
1311d93f860cSPoul-Henning Kamp 	register struct proc *p;
1312d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1313d93f860cSPoul-Henning Kamp 	int nfound = 0;
1314d93f860cSPoul-Henning Kamp 
1315553629ebSJake Burkholder 	if (all) {
1316d93f860cSPoul-Henning Kamp 		/*
1317d93f860cSPoul-Henning Kamp 		 * broadcast
1318d93f860cSPoul-Henning Kamp 		 */
13191005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13202e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1321628d2653SJohn Baldwin 			PROC_LOCK(p);
13229c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13239c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1324628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1325628d2653SJohn Baldwin 				continue;
1326628d2653SJohn Baldwin 			}
1327f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1328d93f860cSPoul-Henning Kamp 				nfound++;
132933a9ed9dSJohn Baldwin 				if (sig)
13302c42a146SMarcel Moolenaar 					psignal(p, sig);
1331628d2653SJohn Baldwin 			}
133233a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1333d93f860cSPoul-Henning Kamp 		}
13341005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1335553629ebSJake Burkholder 	} else {
1336ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1337f591779bSSeigo Tanimura 		if (pgid == 0) {
1338d93f860cSPoul-Henning Kamp 			/*
1339d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1340d93f860cSPoul-Henning Kamp 			 */
13419c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1342f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1343f591779bSSeigo Tanimura 		} else {
1344d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1345f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1346ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1347d93f860cSPoul-Henning Kamp 				return (ESRCH);
1348d93f860cSPoul-Henning Kamp 			}
1349f591779bSSeigo Tanimura 		}
1350ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13512e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1352628d2653SJohn Baldwin 			PROC_LOCK(p);
1353628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1354628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1355628d2653SJohn Baldwin 				continue;
1356628d2653SJohn Baldwin 			}
1357e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
135833a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1359628d2653SJohn Baldwin 				continue;
1360628d2653SJohn Baldwin 			}
1361f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1362d93f860cSPoul-Henning Kamp 				nfound++;
136333a9ed9dSJohn Baldwin 				if (sig)
13642c42a146SMarcel Moolenaar 					psignal(p, sig);
1365628d2653SJohn Baldwin 			}
136633a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1367d93f860cSPoul-Henning Kamp 		}
1368f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1369d93f860cSPoul-Henning Kamp 	}
1370d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1371d93f860cSPoul-Henning Kamp }
1372d93f860cSPoul-Henning Kamp 
1373d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1374df8bae1dSRodney W. Grimes struct kill_args {
1375df8bae1dSRodney W. Grimes 	int	pid;
1376df8bae1dSRodney W. Grimes 	int	signum;
1377df8bae1dSRodney W. Grimes };
1378d2d3e875SBruce Evans #endif
1379fb99ab88SMatthew Dillon /*
1380fb99ab88SMatthew Dillon  * MPSAFE
1381fb99ab88SMatthew Dillon  */
1382df8bae1dSRodney W. Grimes /* ARGSUSED */
138326f9a767SRodney W. Grimes int
1384b40ce416SJulian Elischer kill(td, uap)
1385b40ce416SJulian Elischer 	register struct thread *td;
1386df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1387df8bae1dSRodney W. Grimes {
1388df8bae1dSRodney W. Grimes 	register struct proc *p;
138990af4afaSJohn Baldwin 	int error;
1390df8bae1dSRodney W. Grimes 
13916c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1392df8bae1dSRodney W. Grimes 		return (EINVAL);
1393fb99ab88SMatthew Dillon 
1394df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1395df8bae1dSRodney W. Grimes 		/* kill single process */
139690af4afaSJohn Baldwin 		if ((p = pfind(uap->pid)) == NULL)
139790af4afaSJohn Baldwin 			return (ESRCH);
139890af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
139990af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1400df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1401628d2653SJohn Baldwin 		PROC_UNLOCK(p);
140290af4afaSJohn Baldwin 		return (error);
1403df8bae1dSRodney W. Grimes 	}
1404df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1405df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
140690af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1407df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
140890af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1409df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
141090af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1411df8bae1dSRodney W. Grimes 	}
141290af4afaSJohn Baldwin 	/* NOTREACHED */
1413df8bae1dSRodney W. Grimes }
1414df8bae1dSRodney W. Grimes 
1415df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1416d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1417df8bae1dSRodney W. Grimes struct okillpg_args {
1418df8bae1dSRodney W. Grimes 	int	pgid;
1419df8bae1dSRodney W. Grimes 	int	signum;
1420df8bae1dSRodney W. Grimes };
1421d2d3e875SBruce Evans #endif
1422fb99ab88SMatthew Dillon /*
1423fb99ab88SMatthew Dillon  * MPSAFE
1424fb99ab88SMatthew Dillon  */
1425df8bae1dSRodney W. Grimes /* ARGSUSED */
142626f9a767SRodney W. Grimes int
1427b40ce416SJulian Elischer okillpg(td, uap)
1428b40ce416SJulian Elischer 	struct thread *td;
1429df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1430df8bae1dSRodney W. Grimes {
1431df8bae1dSRodney W. Grimes 
14326c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1433df8bae1dSRodney W. Grimes 		return (EINVAL);
143490af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1435df8bae1dSRodney W. Grimes }
1436df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1437df8bae1dSRodney W. Grimes 
1438df8bae1dSRodney W. Grimes /*
1439df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1440df8bae1dSRodney W. Grimes  */
1441df8bae1dSRodney W. Grimes void
14422c42a146SMarcel Moolenaar gsignal(pgid, sig)
14432c42a146SMarcel Moolenaar 	int pgid, sig;
1444df8bae1dSRodney W. Grimes {
1445df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1446df8bae1dSRodney W. Grimes 
1447f591779bSSeigo Tanimura 	if (pgid != 0) {
1448ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1449f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1450ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1451f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14522c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1453f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1454f591779bSSeigo Tanimura 		}
1455f591779bSSeigo Tanimura 	}
1456df8bae1dSRodney W. Grimes }
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes /*
1459df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1460df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1461df8bae1dSRodney W. Grimes  */
1462df8bae1dSRodney W. Grimes void
14632c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1464df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14652c42a146SMarcel Moolenaar 	int sig, checkctty;
1466df8bae1dSRodney W. Grimes {
1467df8bae1dSRodney W. Grimes 	register struct proc *p;
1468df8bae1dSRodney W. Grimes 
1469628d2653SJohn Baldwin 	if (pgrp) {
1470f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1471628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1472628d2653SJohn Baldwin 			PROC_LOCK(p);
1473df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14742c42a146SMarcel Moolenaar 				psignal(p, sig);
1475628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1476628d2653SJohn Baldwin 		}
1477628d2653SJohn Baldwin 	}
1478df8bae1dSRodney W. Grimes }
1479df8bae1dSRodney W. Grimes 
1480df8bae1dSRodney W. Grimes /*
14811bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1482df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1483df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1484356861dbSMatthew Dillon  *
1485356861dbSMatthew Dillon  * MPSAFE
1486df8bae1dSRodney W. Grimes  */
1487df8bae1dSRodney W. Grimes void
14881bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1489df8bae1dSRodney W. Grimes {
14901bf4700bSJeff Roberson 	struct sigacts *ps;
14911bf4700bSJeff Roberson 	struct proc *p;
14929dde3bc9SDavid Xu 	siginfo_t siginfo;
14939dde3bc9SDavid Xu 	int error;
14941bf4700bSJeff Roberson 
14951bf4700bSJeff Roberson 	p = td->td_proc;
14969dde3bc9SDavid Xu 	if (td->td_flags & TDF_SA) {
14977eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
14989dde3bc9SDavid Xu 			thread_user_enter(p, td);
1499628d2653SJohn Baldwin 		PROC_LOCK(p);
15009dde3bc9SDavid Xu 		if (td->td_mailbox) {
15019dde3bc9SDavid Xu 			SIGDELSET(td->td_sigmask, sig);
15029dde3bc9SDavid Xu 			mtx_lock_spin(&sched_lock);
15039dde3bc9SDavid Xu 			/*
15049dde3bc9SDavid Xu 			 * Force scheduling an upcall, so UTS has chance to
15059dde3bc9SDavid Xu 			 * process the signal before thread runs again in
15069dde3bc9SDavid Xu 			 * userland.
15079dde3bc9SDavid Xu 			 */
15089dde3bc9SDavid Xu 			if (td->td_upcall)
15099dde3bc9SDavid Xu 				td->td_upcall->ku_flags |= KUF_DOUPCALL;
15109dde3bc9SDavid Xu 			mtx_unlock_spin(&sched_lock);
1511432b45deSDavid Xu 		} else {
1512432b45deSDavid Xu 			/* UTS caused a sync signal */
15131fc434dcSDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
15141fc434dcSDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
15151fc434dcSDavid Xu 			sigexit(td, sig);
15169dde3bc9SDavid Xu 		}
15179dde3bc9SDavid Xu 	} else {
15189dde3bc9SDavid Xu 		PROC_LOCK(p);
15199dde3bc9SDavid Xu 	}
1520ef3dab76STim J. Robbins 	ps = p->p_sigacts;
152190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
152290af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15234093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1524df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1525df8bae1dSRodney W. Grimes #ifdef KTRACE
1526374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1527374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15284093529dSJeff Roberson 			    &td->td_sigmask, code);
1529df8bae1dSRodney W. Grimes #endif
15309dde3bc9SDavid Xu 		if (!(td->td_flags & TDF_SA))
15319dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15329dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15334093529dSJeff Roberson 				&td->td_sigmask, code);
15349dde3bc9SDavid Xu 		else {
15354b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15369dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
15379dde3bc9SDavid Xu 			PROC_UNLOCK(p);
15389dde3bc9SDavid Xu 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
15399dde3bc9SDavid Xu 			    sizeof(siginfo));
15409dde3bc9SDavid Xu 			PROC_LOCK(p);
1541432b45deSDavid Xu 			/* UTS memory corrupted */
15429dde3bc9SDavid Xu 			if (error)
15439dde3bc9SDavid Xu 				sigexit(td, SIGILL);
15449dde3bc9SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
15459dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15469dde3bc9SDavid Xu 		}
15474093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15482c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15494093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15502c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1551289ccde0SPeter Wemm 			/*
15528f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1553289ccde0SPeter Wemm 			 */
155490af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15552c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15562c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
155790af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15582c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1559dedc04feSPeter Wemm 		}
156090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15616f841fb7SMarcel Moolenaar 	} else {
156290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15636626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15642c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1565c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1566df8bae1dSRodney W. Grimes 	}
1567628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1568df8bae1dSRodney W. Grimes }
1569df8bae1dSRodney W. Grimes 
15704093529dSJeff Roberson static struct thread *
15714093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15724093529dSJeff Roberson {
15733074d1b4SDavid Xu 	struct thread *td, *signal_td;
15744093529dSJeff Roberson 
15754093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15764093529dSJeff Roberson 
15774093529dSJeff Roberson 	/*
15783074d1b4SDavid Xu 	 * First find a thread in sigwait state and signal belongs to
15793074d1b4SDavid Xu 	 * its wait set. POSIX's arguments is that speed of delivering signal
15803074d1b4SDavid Xu 	 * to sigwait thread is faster than delivering signal to user stack.
15813074d1b4SDavid Xu 	 * If we can not find sigwait thread, then find the first thread in
15823074d1b4SDavid Xu 	 * the proc that doesn't have this signal masked, an exception is
15833074d1b4SDavid Xu 	 * if current thread is sending signal to its process, and it does not
15843074d1b4SDavid Xu 	 * mask the signal, it should get the signal, this is another fast
15853074d1b4SDavid Xu 	 * way to deliver signal.
15864093529dSJeff Roberson 	 */
15873074d1b4SDavid Xu 	signal_td = NULL;
1588a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15893074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15903074d1b4SDavid Xu 		if (td->td_waitset != NULL &&
1591a9a48d68SDavid Xu 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1592a9a48d68SDavid Xu 				mtx_unlock_spin(&sched_lock);
15934093529dSJeff Roberson 				return (td);
1594a9a48d68SDavid Xu 		}
15953074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15963074d1b4SDavid Xu 			if (td == curthread)
15973074d1b4SDavid Xu 				signal_td = curthread;
15983074d1b4SDavid Xu 			else if (signal_td == NULL)
15993074d1b4SDavid Xu 				signal_td = td;
16003074d1b4SDavid Xu 		}
16013074d1b4SDavid Xu 	}
16023074d1b4SDavid Xu 	if (signal_td == NULL)
16033074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1604a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
16053074d1b4SDavid Xu 	return (signal_td);
16064093529dSJeff Roberson }
16074093529dSJeff Roberson 
1608df8bae1dSRodney W. Grimes /*
1609df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1610df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1611df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1612df8bae1dSRodney W. Grimes  *
1613df8bae1dSRodney W. Grimes  * Exceptions:
1614df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1615df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1616df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1617df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1618df8bae1dSRodney W. Grimes  *
1619df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
162090af4afaSJohn Baldwin  *
162190af4afaSJohn Baldwin  * MPSAFE
1622df8bae1dSRodney W. Grimes  */
1623df8bae1dSRodney W. Grimes void
16244093529dSJeff Roberson psignal(struct proc *p, int sig)
1625df8bae1dSRodney W. Grimes {
1626b40ce416SJulian Elischer 	struct thread *td;
16274093529dSJeff Roberson 	int prop;
16284093529dSJeff Roberson 
162941b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
163041b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
163141b3077aSJacques Vidrine 
16324093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
16334093529dSJeff Roberson 	prop = sigprop(sig);
16344093529dSJeff Roberson 
16354093529dSJeff Roberson 	/*
16364093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16374093529dSJeff Roberson 	 */
16384093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16394093529dSJeff Roberson 
1640c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16414093529dSJeff Roberson }
16424093529dSJeff Roberson 
164390af4afaSJohn Baldwin /*
164490af4afaSJohn Baldwin  * MPSAFE
164590af4afaSJohn Baldwin  */
16464093529dSJeff Roberson void
1647c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16484093529dSJeff Roberson {
16499dde3bc9SDavid Xu 	sigset_t saved;
16509dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16519dde3bc9SDavid Xu 
16529dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16539dde3bc9SDavid Xu 		saved = p->p_siglist;
1654c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16559dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16569dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
16579dde3bc9SDavid Xu 			return;
16589dde3bc9SDavid Xu 		else {
16599dde3bc9SDavid Xu 			/* pending set changed */
16609dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
16619dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
16629dde3bc9SDavid Xu 		}
16639dde3bc9SDavid Xu 	}
16649dde3bc9SDavid Xu }
16659dde3bc9SDavid Xu 
16669dde3bc9SDavid Xu static void
1667c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target)
16689dde3bc9SDavid Xu {
16694093529dSJeff Roberson 	struct proc *p;
16704093529dSJeff Roberson 	register sig_t action;
16714093529dSJeff Roberson 	sigset_t *siglist;
16724093529dSJeff Roberson 	struct thread *td0;
1673e602ba25SJulian Elischer 	register int prop;
167490af4afaSJohn Baldwin 	struct sigacts *ps;
1675df8bae1dSRodney W. Grimes 
167641b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
167741b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
16784093529dSJeff Roberson 
16794093529dSJeff Roberson 	p = td->td_proc;
168090af4afaSJohn Baldwin 	ps = p->p_sigacts;
16812c42a146SMarcel Moolenaar 
1682628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1683cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1684cb679c38SJonathan Lemon 
16852c42a146SMarcel Moolenaar 	prop = sigprop(sig);
16864093529dSJeff Roberson 
16874093529dSJeff Roberson 	/*
168814b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
168914b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
169014b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
16914093529dSJeff Roberson 	 */
16923074d1b4SDavid Xu 	if (target == SIGTARGET_TD) {
16933074d1b4SDavid Xu 		siglist = &td->td_siglist;
16943074d1b4SDavid Xu 	} else {
16953074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig))
16963074d1b4SDavid Xu 			siglist = &td->td_siglist;
16973074d1b4SDavid Xu 		else if (td->td_waitset != NULL &&
16983074d1b4SDavid Xu 			SIGISMEMBER(*(td->td_waitset), sig))
16993074d1b4SDavid Xu 			siglist = &td->td_siglist;
17003074d1b4SDavid Xu 		else
17013074d1b4SDavid Xu 			siglist = &p->p_siglist;
17023074d1b4SDavid Xu 	}
17034093529dSJeff Roberson 
1704df8bae1dSRodney W. Grimes 	/*
17052a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
17062a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
17072a024a2bSSean Eric Fagan 	 * a chance, as well.
1708df8bae1dSRodney W. Grimes 	 */
1709b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1710df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1711b40ce416SJulian Elischer 	} else {
1712df8bae1dSRodney W. Grimes 		/*
1713df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1714df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
171590af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1716df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1717df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1718df8bae1dSRodney W. Grimes 		 */
171990af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
172090af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
172190af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
172290af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1723df8bae1dSRodney W. Grimes 			return;
172490af4afaSJohn Baldwin 		}
17253074d1b4SDavid Xu 		if (((td->td_waitset == NULL) &&
17263074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig)) ||
17273074d1b4SDavid Xu 		    ((td->td_waitset != NULL) &&
17283074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig) &&
17293074d1b4SDavid Xu 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1730df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
173190af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1732df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1733df8bae1dSRodney W. Grimes 		else
1734df8bae1dSRodney W. Grimes 			action = SIG_DFL;
173590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1736df8bae1dSRodney W. Grimes 	}
1737df8bae1dSRodney W. Grimes 
17384093529dSJeff Roberson 	if (prop & SA_CONT) {
17391d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17404093529dSJeff Roberson 		/*
17414093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17424093529dSJeff Roberson 		 * the proc's siglist.
17434093529dSJeff Roberson 		 */
1744a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17454093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17464093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1747a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17484093529dSJeff Roberson 	}
1749df8bae1dSRodney W. Grimes 
1750df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1751df8bae1dSRodney W. Grimes 		/*
1752df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1753df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1754df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1755df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1756df8bae1dSRodney W. Grimes 		 */
1757e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1758e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1759e602ba25SJulian Elischer 		    (action == SIG_DFL))
1760df8bae1dSRodney W. Grimes 		        return;
17611d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1762a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17634093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17644093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1765a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17666933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1767df8bae1dSRodney W. Grimes 	}
17683074d1b4SDavid Xu 
17694093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17704093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17713074d1b4SDavid Xu 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
17723074d1b4SDavid Xu 	    action != SIG_HOLD) {
17733074d1b4SDavid Xu 		td->td_waitset = NULL;
17743074d1b4SDavid Xu 	}
17753074d1b4SDavid Xu 
17765312b1c7SDavid Xu 	/*
17775312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17785312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17795312b1c7SDavid Xu 	 */
17805312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17815312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17825312b1c7SDavid Xu 		return;
1783df8bae1dSRodney W. Grimes 	/*
1784e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1785e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1786e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1787e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1788e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1789e602ba25SJulian Elischer 	 * We try do the per-process part here.
1790df8bae1dSRodney W. Grimes 	 */
1791e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1792e602ba25SJulian Elischer 		/*
1793e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1794e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1795e602ba25SJulian Elischer 		 */
1796e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1797e602ba25SJulian Elischer 			/*
1798e602ba25SJulian Elischer 			 * The traced process is already stopped,
1799e602ba25SJulian Elischer 			 * so no further action is necessary.
1800e602ba25SJulian Elischer 			 * No signal can restart us.
1801e602ba25SJulian Elischer 			 */
1802e602ba25SJulian Elischer 			goto out;
18031c32c37cSJohn Baldwin 		}
1804b40ce416SJulian Elischer 
1805e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1806df8bae1dSRodney W. Grimes 			/*
1807e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1808e602ba25SJulian Elischer 			 * It will die elsewhere.
1809e602ba25SJulian Elischer 			 * All threads must be restarted.
1810df8bae1dSRodney W. Grimes 			 */
1811e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1812e602ba25SJulian Elischer 			goto runfast;
1813e602ba25SJulian Elischer 		}
1814e602ba25SJulian Elischer 
1815e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1816e602ba25SJulian Elischer 			/*
1817e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18184093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18191d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1820e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18214093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1822e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1823e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1824e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1825e602ba25SJulian Elischer 			 */
18261279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18276933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1828e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18294093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1830e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1831e602ba25SJulian Elischer 				/*
1832e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1833e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1834e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1835e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1836e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1837e602ba25SJulian Elischer 				 * process, we need to make sure that the
1838e602ba25SJulian Elischer 				 * single thread is runnable asap.
1839e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1840e602ba25SJulian Elischer 				 */
1841e602ba25SJulian Elischer 				goto runfast;
1842e602ba25SJulian Elischer 			}
1843e602ba25SJulian Elischer 			/*
1844e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1845e602ba25SJulian Elischer 			 */
1846e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
184704774f23SJulian Elischer 			thread_unsuspend(p);
1848e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1849e602ba25SJulian Elischer 			goto out;
1850e602ba25SJulian Elischer 		}
1851e602ba25SJulian Elischer 
1852e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1853e602ba25SJulian Elischer 			/*
1854e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1855e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
185604774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1857e602ba25SJulian Elischer 			 */
18581279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18594093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1860e602ba25SJulian Elischer 			goto out;
1861e602ba25SJulian Elischer 		}
1862e602ba25SJulian Elischer 
1863e602ba25SJulian Elischer 		/*
1864e602ba25SJulian Elischer 		 * All other kinds of signals:
1865e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1866e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1867e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
186804774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1869e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1870e602ba25SJulian Elischer 		 */
1871e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
187271fad9fdSJulian Elischer 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1873e602ba25SJulian Elischer 			if (td->td_flags & TDF_CVWAITQ)
187471fad9fdSJulian Elischer 				cv_abort(td);
1875e602ba25SJulian Elischer 			else
187671fad9fdSJulian Elischer 				abortsleep(td);
1877e602ba25SJulian Elischer 		}
1878e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1879df8bae1dSRodney W. Grimes 		goto out;
1880df8bae1dSRodney W. Grimes 		/*
1881e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1882e602ba25SJulian Elischer 		 * Shouldn't they abort too?
188304774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
188404774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1885df8bae1dSRodney W. Grimes 		 */
1886e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
188735c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
188835c32a76SDavid Xu 			!(prop & SA_STOP)) {
1889721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18904093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1891721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1892df8bae1dSRodney W. Grimes 			goto out;
189304774f23SJulian Elischer 		}
189435c32a76SDavid Xu 		if (prop & SA_STOP) {
189535c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
189635c32a76SDavid Xu 				goto out;
1897e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1898e574e444SDavid Xu 			p->p_xstat = sig;
189935c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
19004093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
19014093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1902062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1903062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
19044093529dSJeff Roberson 					thread_suspend_one(td0);
1905062cf543SDavid Xu 				} else if (td != td0) {
1906062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1907062cf543SDavid Xu 				}
190835c32a76SDavid Xu 			}
1909e574e444SDavid Xu 			thread_stopped(p);
19104093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1911e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
19124093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
19134093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
19144093529dSJeff Roberson 			}
191535c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
191635c32a76SDavid Xu 			goto out;
191735c32a76SDavid Xu 		}
1918721e5910SJulian Elischer 		else
1919df8bae1dSRodney W. Grimes 			goto runfast;
1920df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1921e602ba25SJulian Elischer 	} else {
1922e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19234093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1924e602ba25SJulian Elischer 		goto out;
1925e602ba25SJulian Elischer 	}
1926e602ba25SJulian Elischer 
1927b40ce416SJulian Elischer 	/*
1928e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1929e602ba25SJulian Elischer 	 * running threads.
1930b40ce416SJulian Elischer 	 */
1931e602ba25SJulian Elischer 
1932e602ba25SJulian Elischer runfast:
1933aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19344093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1935e602ba25SJulian Elischer 	thread_unsuspend(p);
1936e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1937e602ba25SJulian Elischer out:
1938e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1939e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1940e602ba25SJulian Elischer }
1941e602ba25SJulian Elischer 
1942e602ba25SJulian Elischer /*
1943e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1944e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1945e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1946e602ba25SJulian Elischer  */
1947e602ba25SJulian Elischer static void
19484093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1949e602ba25SJulian Elischer {
1950e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1951e602ba25SJulian Elischer 	register int prop;
1952e602ba25SJulian Elischer 
19538b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1954aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1955e602ba25SJulian Elischer 	prop = sigprop(sig);
1956e602ba25SJulian Elischer 	/*
1957aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1958e602ba25SJulian Elischer 	 * killed in this lifetime.
1959e602ba25SJulian Elischer 	 */
1960e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1961e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1962e602ba25SJulian Elischer 			td->td_priority = PUSER;
1963b40ce416SJulian Elischer 		}
1964b40ce416SJulian Elischer 	}
196571fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1966e602ba25SJulian Elischer 		/*
1967e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1968e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1969e602ba25SJulian Elischer 		 * be noticed when the process returns through
1970e602ba25SJulian Elischer 		 * trap() or syscall().
1971e602ba25SJulian Elischer 		 */
1972e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1973aa0fa334SJulian Elischer 			return;
1974e602ba25SJulian Elischer 		}
1975e602ba25SJulian Elischer 		/*
1976e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1977e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1978e602ba25SJulian Elischer 		 * for its parent.
1979e602ba25SJulian Elischer 		 */
1980e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1981e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1982aa0fa334SJulian Elischer 		} else {
1983aa0fa334SJulian Elischer 
1984df8bae1dSRodney W. Grimes 			/*
1985e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1986e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1987e602ba25SJulian Elischer 			 * be awakened.
1988df8bae1dSRodney W. Grimes 			 */
1989e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19901d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19914093529dSJeff Roberson 				/*
19924093529dSJeff Roberson 				 * It may be on either list in this state.
19934093529dSJeff Roberson 				 * Remove from both for now.
19944093529dSJeff Roberson 				 */
19954093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1996aa0fa334SJulian Elischer 				return;
1997df8bae1dSRodney W. Grimes 			}
1998df8bae1dSRodney W. Grimes 
1999aa0fa334SJulian Elischer 			/*
2000aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
2001aa0fa334SJulian Elischer 			 */
2002aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
2003aa0fa334SJulian Elischer 				td->td_priority = PUSER;
2004aa0fa334SJulian Elischer 			}
2005aa0fa334SJulian Elischer 		}
200671fad9fdSJulian Elischer 		if (td->td_flags & TDF_CVWAITQ)
200771fad9fdSJulian Elischer 			cv_abort(td);
200871fad9fdSJulian Elischer 		else
200971fad9fdSJulian Elischer 			abortsleep(td);
2010aa0fa334SJulian Elischer 	}
2011aa0fa334SJulian Elischer #ifdef SMP
2012aa0fa334SJulian Elischer 	  else {
2013df8bae1dSRodney W. Grimes 		/*
2014e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
2015df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2016df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2017df8bae1dSRodney W. Grimes 		 */
201871fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td) && td != curthread) {
2019e602ba25SJulian Elischer 			forward_signal(td);
2020aa0fa334SJulian Elischer 		}
20210ac3b636SAndrew Gallatin 	  }
20223163861cSTor Egge #endif
20236caa8a15SJohn Baldwin }
2024df8bae1dSRodney W. Grimes 
20254cc9f52fSRobert Drehmel void
20264cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
20274cc9f52fSRobert Drehmel {
20284cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
20294cc9f52fSRobert Drehmel 
203030a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20314cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
20324cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
20334cc9f52fSRobert Drehmel 
20344cc9f52fSRobert Drehmel 	p->p_xstat = sig;
20354cc9f52fSRobert Drehmel 	PROC_LOCK(p->p_pptr);
20364cc9f52fSRobert Drehmel 	psignal(p->p_pptr, SIGCHLD);
20374cc9f52fSRobert Drehmel 	PROC_UNLOCK(p->p_pptr);
20384cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
20394cc9f52fSRobert Drehmel 	stop(p);	/* uses schedlock too eventually */
20404cc9f52fSRobert Drehmel 	thread_suspend_one(td);
20414cc9f52fSRobert Drehmel 	PROC_UNLOCK(p);
20424cc9f52fSRobert Drehmel 	DROP_GIANT();
204329bcc451SJeff Roberson 	mi_switch(SW_INVOL);
20444cc9f52fSRobert Drehmel 	mtx_unlock_spin(&sched_lock);
20454cc9f52fSRobert Drehmel 	PICKUP_GIANT();
20464cc9f52fSRobert Drehmel }
20474cc9f52fSRobert Drehmel 
2048df8bae1dSRodney W. Grimes /*
2049df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2050df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2051df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2052df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2053df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2054628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2055df8bae1dSRodney W. Grimes  * sequence is
2056df8bae1dSRodney W. Grimes  *
2057e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
20582c42a146SMarcel Moolenaar  *		postsig(sig);
2059df8bae1dSRodney W. Grimes  */
20606711f10fSJohn Baldwin static int
2061e602ba25SJulian Elischer issignal(td)
2062e602ba25SJulian Elischer 	struct thread *td;
2063df8bae1dSRodney W. Grimes {
2064e602ba25SJulian Elischer 	struct proc *p;
206590af4afaSJohn Baldwin 	struct sigacts *ps;
20664093529dSJeff Roberson 	sigset_t sigpending;
2067062cf543SDavid Xu 	int sig, prop;
2068062cf543SDavid Xu 	struct thread *td0;
2069df8bae1dSRodney W. Grimes 
2070e602ba25SJulian Elischer 	p = td->td_proc;
207190af4afaSJohn Baldwin 	ps = p->p_sigacts;
207290af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2073628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2074df8bae1dSRodney W. Grimes 	for (;;) {
20752a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
20762a024a2bSSean Eric Fagan 
20774093529dSJeff Roberson 		sigpending = td->td_siglist;
20784093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
20794093529dSJeff Roberson 
2080df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
20814093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
20824093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2083df8bae1dSRodney W. Grimes 			return (0);
20844093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
20852a024a2bSSean Eric Fagan 
2086047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2087047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2088047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2089047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2090047aa39bSRobert Watson 		}
20912a024a2bSSean Eric Fagan 
2092df8bae1dSRodney W. Grimes 		/*
2093df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2094df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2095df8bae1dSRodney W. Grimes 		 */
209690af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
20974093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
2098df8bae1dSRodney W. Grimes 			continue;
2099df8bae1dSRodney W. Grimes 		}
2100df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2101df8bae1dSRodney W. Grimes 			/*
2102d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2103df8bae1dSRodney W. Grimes 			 */
210490af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
21054cc9f52fSRobert Drehmel 			ptracestop(td, sig);
2106628d2653SJohn Baldwin 			PROC_LOCK(p);
210790af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2108df8bae1dSRodney W. Grimes 
2109df8bae1dSRodney W. Grimes 			/*
2110df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
2111df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
2112df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
2113df8bae1dSRodney W. Grimes 			 */
21144093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
21152c42a146SMarcel Moolenaar 			sig = p->p_xstat;
21162c42a146SMarcel Moolenaar 			if (sig == 0)
2117df8bae1dSRodney W. Grimes 				continue;
2118df8bae1dSRodney W. Grimes 
2119df8bae1dSRodney W. Grimes 			/*
21208d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
21218d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
21228d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
21238d542cb5SDavid E. O'Brien 			 */
21248d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
21258d542cb5SDavid E. O'Brien 				continue;
21268d542cb5SDavid E. O'Brien 
21278d542cb5SDavid E. O'Brien 			/*
21284093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
21291d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
2130df8bae1dSRodney W. Grimes 			 */
21314093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
21324093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
2133df8bae1dSRodney W. Grimes 				continue;
21344093529dSJeff Roberson 			signotify(td);
2135df8bae1dSRodney W. Grimes 		}
2136df8bae1dSRodney W. Grimes 
21378d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
21388d542cb5SDavid E. O'Brien 
2139df8bae1dSRodney W. Grimes 		/*
2140df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2141df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2142df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2143df8bae1dSRodney W. Grimes 		 */
2144d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2145df8bae1dSRodney W. Grimes 
2146d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2147df8bae1dSRodney W. Grimes 			/*
2148df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2149df8bae1dSRodney W. Grimes 			 */
2150df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2151df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2152df8bae1dSRodney W. Grimes 				/*
2153df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2154df8bae1dSRodney W. Grimes 				 * in init? XXX
2155df8bae1dSRodney W. Grimes 				 */
2156d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
21572c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2158df8bae1dSRodney W. Grimes #endif
2159df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2160df8bae1dSRodney W. Grimes 			}
2161df8bae1dSRodney W. Grimes 			/*
2162df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2163df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2164df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2165df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2166df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2167df8bae1dSRodney W. Grimes 			 */
2168df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2169df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2170df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2171df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2172df8bae1dSRodney W. Grimes 					break;	/* == ignore */
217390af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
217490af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
217590af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2176e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
21772c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2178721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2179062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2180062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2181062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2182062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2183062cf543SDavid Xu 						thread_suspend_one(td0);
2184062cf543SDavid Xu 					} else if (td != td0) {
2185062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2186062cf543SDavid Xu 					}
2187062cf543SDavid Xu 				}
2188e574e444SDavid Xu 				thread_stopped(p);
218971fad9fdSJulian Elischer 				thread_suspend_one(td);
2190721e5910SJulian Elischer 				PROC_UNLOCK(p);
2191721e5910SJulian Elischer 				DROP_GIANT();
219229bcc451SJeff Roberson 				mi_switch(SW_INVOL);
2193721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2194721e5910SJulian Elischer 				PICKUP_GIANT();
2195721e5910SJulian Elischer 				PROC_LOCK(p);
219690af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2197df8bae1dSRodney W. Grimes 				break;
219821b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2199df8bae1dSRodney W. Grimes 				/*
2200df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2201df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2202df8bae1dSRodney W. Grimes 				 */
2203df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2204df8bae1dSRodney W. Grimes 			} else
22052c42a146SMarcel Moolenaar 				return (sig);
2206df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2207df8bae1dSRodney W. Grimes 
2208d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2209df8bae1dSRodney W. Grimes 			/*
2210df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2211df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2212df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2213df8bae1dSRodney W. Grimes 			 */
2214df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2215df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2216df8bae1dSRodney W. Grimes 				printf("issignal\n");
2217df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2218df8bae1dSRodney W. Grimes 
2219df8bae1dSRodney W. Grimes 		default:
2220df8bae1dSRodney W. Grimes 			/*
2221df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2222df8bae1dSRodney W. Grimes 			 * postsig() process it.
2223df8bae1dSRodney W. Grimes 			 */
22242c42a146SMarcel Moolenaar 			return (sig);
2225df8bae1dSRodney W. Grimes 		}
22264093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2227df8bae1dSRodney W. Grimes 	}
2228df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2229df8bae1dSRodney W. Grimes }
2230df8bae1dSRodney W. Grimes 
2231df8bae1dSRodney W. Grimes /*
2232df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2233df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
22345b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
22355b3047d5SJohn Baldwin  * lock held.
2236df8bae1dSRodney W. Grimes  */
22375b3047d5SJohn Baldwin static void
2238e574e444SDavid Xu stop(struct proc *p)
2239df8bae1dSRodney W. Grimes {
2240df8bae1dSRodney W. Grimes 
2241628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22421279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2243df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
224401609114SAlfred Perlstein 	wakeup(p->p_pptr);
2245df8bae1dSRodney W. Grimes }
2246df8bae1dSRodney W. Grimes 
224790af4afaSJohn Baldwin /*
224890af4afaSJohn Baldwin  * MPSAFE
224990af4afaSJohn Baldwin  */
2250e574e444SDavid Xu void
2251e574e444SDavid Xu thread_stopped(struct proc *p)
2252e574e444SDavid Xu {
2253e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
225490af4afaSJohn Baldwin 	struct sigacts *ps;
2255e574e444SDavid Xu 	int n;
2256e574e444SDavid Xu 
2257e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2258e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2259e574e444SDavid Xu 	n = p->p_suspcount;
2260e574e444SDavid Xu 	if (p == p1)
2261e574e444SDavid Xu 		n++;
2262e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2263e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2264e574e444SDavid Xu 		stop(p);
2265e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
226690af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
226790af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
226890af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
226990af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2270e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
227190af4afaSJohn Baldwin 		} else
227290af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2273e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2274e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2275e574e444SDavid Xu 	}
2276e574e444SDavid Xu }
2277e574e444SDavid Xu 
2278df8bae1dSRodney W. Grimes /*
2279df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2280df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2281df8bae1dSRodney W. Grimes  */
2282df8bae1dSRodney W. Grimes void
22832c42a146SMarcel Moolenaar postsig(sig)
22842c42a146SMarcel Moolenaar 	register int sig;
2285df8bae1dSRodney W. Grimes {
2286b40ce416SJulian Elischer 	struct thread *td = curthread;
2287b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2288628d2653SJohn Baldwin 	struct sigacts *ps;
22892c42a146SMarcel Moolenaar 	sig_t action;
22902c42a146SMarcel Moolenaar 	sigset_t returnmask;
22912c42a146SMarcel Moolenaar 	int code;
2292df8bae1dSRodney W. Grimes 
22932c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
22945526d2d9SEivind Eklund 
22952ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2296628d2653SJohn Baldwin 	ps = p->p_sigacts;
229790af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
22984093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
22992c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2300df8bae1dSRodney W. Grimes #ifdef KTRACE
2301374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
23025e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
23034093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2304df8bae1dSRodney W. Grimes #endif
2305047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2306047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2307047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2308047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2309047aa39bSRobert Watson 	}
23102a024a2bSSean Eric Fagan 
2311432b45deSDavid Xu 	if (!(td->td_flags & TDF_SA && td->td_mailbox) &&
2312432b45deSDavid Xu 	    action == SIG_DFL) {
2313df8bae1dSRodney W. Grimes 		/*
2314df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2315df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2316df8bae1dSRodney W. Grimes 		 */
231790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2318b40ce416SJulian Elischer 		sigexit(td, sig);
2319df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2320df8bae1dSRodney W. Grimes 	} else {
2321432b45deSDavid Xu 		if (td->td_flags & TDF_SA && td->td_mailbox) {
2322432b45deSDavid Xu 			if (sig == SIGKILL) {
2323432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2324432b45deSDavid Xu 				sigexit(td, sig);
2325432b45deSDavid Xu 			}
2326432b45deSDavid Xu 		}
2327432b45deSDavid Xu 
2328df8bae1dSRodney W. Grimes 		/*
2329df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2330df8bae1dSRodney W. Grimes 		 */
23314093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23325526d2d9SEivind Eklund 		    ("postsig action"));
2333df8bae1dSRodney W. Grimes 		/*
2334df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2335645682fdSLuoqi Chen 		 * occurrences of this signal.
2336df8bae1dSRodney W. Grimes 		 *
2337645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2338df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2339645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2340df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2341df8bae1dSRodney W. Grimes 		 */
23425e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23434093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23445e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2345df8bae1dSRodney W. Grimes 		} else
23464093529dSJeff Roberson 			returnmask = td->td_sigmask;
23472c42a146SMarcel Moolenaar 
23484093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23492c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23504093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23512c42a146SMarcel Moolenaar 
23522c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2353289ccde0SPeter Wemm 			/*
23548f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2355289ccde0SPeter Wemm 			 */
235690af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23572c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23582c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
235990af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23602c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2361dedc04feSPeter Wemm 		}
2362df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23632c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2364df8bae1dSRodney W. Grimes 			code = 0;
2365df8bae1dSRodney W. Grimes 		} else {
23666626c604SJulian Elischer 			code = p->p_code;
23676626c604SJulian Elischer 			p->p_code = 0;
23686626c604SJulian Elischer 			p->p_sig = 0;
2369df8bae1dSRodney W. Grimes 		}
2370432b45deSDavid Xu 		if (td->td_flags & TDF_SA && td->td_mailbox)
237158a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
237258a3c273SJeff Roberson 		else
237358a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
237458a3c273SJeff Roberson 			    &returnmask, code);
2375df8bae1dSRodney W. Grimes 	}
2376df8bae1dSRodney W. Grimes }
2377df8bae1dSRodney W. Grimes 
2378df8bae1dSRodney W. Grimes /*
2379df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2380df8bae1dSRodney W. Grimes  */
238126f9a767SRodney W. Grimes void
2382df8bae1dSRodney W. Grimes killproc(p, why)
2383df8bae1dSRodney W. Grimes 	struct proc *p;
2384df8bae1dSRodney W. Grimes 	char *why;
2385df8bae1dSRodney W. Grimes {
23869081e5e8SJohn Baldwin 
23879081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
23880384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
23890384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2390729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2391b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2392df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2393df8bae1dSRodney W. Grimes }
2394df8bae1dSRodney W. Grimes 
2395df8bae1dSRodney W. Grimes /*
2396df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2397df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2398df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2399df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2400df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2401df8bae1dSRodney W. Grimes  * does not return.
240290af4afaSJohn Baldwin  *
240390af4afaSJohn Baldwin  * MPSAFE
2404df8bae1dSRodney W. Grimes  */
240526f9a767SRodney W. Grimes void
2406b40ce416SJulian Elischer sigexit(td, sig)
2407b40ce416SJulian Elischer 	struct thread *td;
24082c42a146SMarcel Moolenaar 	int sig;
2409df8bae1dSRodney W. Grimes {
2410b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2411df8bae1dSRodney W. Grimes 
2412628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2413df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
24142c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
24152c42a146SMarcel Moolenaar 		p->p_sig = sig;
2416c364e17eSAndrey A. Chernov 		/*
2417c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2418c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2419c364e17eSAndrey A. Chernov 		 * these messages.)
2420c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
2421c364e17eSAndrey A. Chernov 		 */
2422628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2423c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
2424c31146a1SJohn Baldwin 			mtx_lock(&Giant);
2425b40ce416SJulian Elischer 		if (coredump(td) == 0)
24262c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
242757308494SJoerg Wunsch 		if (kern_logsigexit)
242857308494SJoerg Wunsch 			log(LOG_INFO,
242957308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24303d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24319c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24322c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24332c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
2434c31146a1SJohn Baldwin 	} else {
2435628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2436628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
2437628d2653SJohn Baldwin 			mtx_lock(&Giant);
2438c31146a1SJohn Baldwin 	}
2439b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2440df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2441df8bae1dSRodney W. Grimes }
2442df8bae1dSRodney W. Grimes 
2443c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2444c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2445c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2446c5edb423SSean Eric Fagan 
2447c5edb423SSean Eric Fagan /*
2448c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2449c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2450c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2451c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2452c5edb423SSean Eric Fagan  *	%P	process id (pid)
2453c5edb423SSean Eric Fagan  *	%U	user id (uid)
2454c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2455c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2456c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2457c5edb423SSean Eric Fagan  */
2458c5edb423SSean Eric Fagan 
2459fca666a1SJulian Elischer static char *
2460c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24618b43b535SAlfred Perlstein 	const char *name;
24628b43b535SAlfred Perlstein 	uid_t uid;
24638b43b535SAlfred Perlstein 	pid_t pid;
24648b43b535SAlfred Perlstein {
24658b43b535SAlfred Perlstein 	const char *format, *appendstr;
2466c5edb423SSean Eric Fagan 	char *temp;
2467c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24688b43b535SAlfred Perlstein 	size_t i, l, n;
2469c5edb423SSean Eric Fagan 
24708b43b535SAlfred Perlstein 	format = corefilename;
24718b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24720bfe2990SEivind Eklund 	if (temp == NULL)
24738b43b535SAlfred Perlstein 		return (NULL);
2474ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2475c5edb423SSean Eric Fagan 		switch (format[i]) {
2476c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2477c5edb423SSean Eric Fagan 			i++;
2478c5edb423SSean Eric Fagan 			switch (format[i]) {
2479c5edb423SSean Eric Fagan 			case '%':
24808b43b535SAlfred Perlstein 				appendstr = "%";
2481c5edb423SSean Eric Fagan 				break;
2482c5edb423SSean Eric Fagan 			case 'N':	/* process name */
24838b43b535SAlfred Perlstein 				appendstr = name;
2484c5edb423SSean Eric Fagan 				break;
2485c5edb423SSean Eric Fagan 			case 'P':	/* process id */
24868b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
24878b43b535SAlfred Perlstein 				appendstr = buf;
2488c5edb423SSean Eric Fagan 				break;
2489c5edb423SSean Eric Fagan 			case 'U':	/* user id */
24908b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
24918b43b535SAlfred Perlstein 				appendstr = buf;
2492c5edb423SSean Eric Fagan 				break;
2493c5edb423SSean Eric Fagan 			default:
24948b43b535SAlfred Perlstein 				appendstr = "";
24958b43b535SAlfred Perlstein 			  	log(LOG_ERR,
24968b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
24978b43b535SAlfred Perlstein 				    format[i], format);
2498c5edb423SSean Eric Fagan 			}
24998b43b535SAlfred Perlstein 			l = strlen(appendstr);
25008b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
25018b43b535SAlfred Perlstein 				goto toolong;
25028b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
25038b43b535SAlfred Perlstein 			n += l;
2504c5edb423SSean Eric Fagan 			break;
2505c5edb423SSean Eric Fagan 		default:
2506c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2507c5edb423SSean Eric Fagan 		}
2508c5edb423SSean Eric Fagan 	}
25098b43b535SAlfred Perlstein 	if (format[i] != '\0')
25108b43b535SAlfred Perlstein 		goto toolong;
25118b43b535SAlfred Perlstein 	return (temp);
25128b43b535SAlfred Perlstein toolong:
25138b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
25148b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
25158b43b535SAlfred Perlstein 	free(temp, M_TEMP);
25168b43b535SAlfred Perlstein 	return (NULL);
2517c5edb423SSean Eric Fagan }
2518c5edb423SSean Eric Fagan 
2519df8bae1dSRodney W. Grimes /*
2520fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2521fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2522fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2523fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2524fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2525fca666a1SJulian Elischer  */
2526fca666a1SJulian Elischer 
2527fca666a1SJulian Elischer static int
2528b40ce416SJulian Elischer coredump(struct thread *td)
2529fca666a1SJulian Elischer {
2530b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2531fca666a1SJulian Elischer 	register struct vnode *vp;
25329c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
253306ae1e91SMatthew Dillon 	struct flock lf;
2534fca666a1SJulian Elischer 	struct nameidata nd;
2535fca666a1SJulian Elischer 	struct vattr vattr;
2536c447f5b2SRobert Watson 	int error, error1, flags, locked;
2537f2a2857bSKirk McKusick 	struct mount *mp;
2538fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2539fca666a1SJulian Elischer 	off_t limit;
2540fca666a1SJulian Elischer 
2541628d2653SJohn Baldwin 	PROC_LOCK(p);
2542628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2543fca666a1SJulian Elischer 
2544628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2545628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2546fca666a1SJulian Elischer 		return (EFAULT);
2547628d2653SJohn Baldwin 	}
2548fca666a1SJulian Elischer 
2549fca666a1SJulian Elischer 	/*
255035a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
255135a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
255235a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
255335a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
255435a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
255535a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2556fca666a1SJulian Elischer 	 */
255791d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2558628d2653SJohn Baldwin 	PROC_UNLOCK(p);
255991d5354aSJohn Baldwin 	if (limit == 0)
256091d5354aSJohn Baldwin 		return (EFBIG);
256135a2598fSSean Eric Fagan 
2562f2a2857bSKirk McKusick restart:
25639c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2564ccdbd10cSPeter Pentchev 	if (name == NULL)
2565ccdbd10cSPeter Pentchev 		return (EINVAL);
2566b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2567e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25687c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2569fca666a1SJulian Elischer 	free(name, M_TEMP);
2570fca666a1SJulian Elischer 	if (error)
2571fca666a1SJulian Elischer 		return (error);
2572762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2573fca666a1SJulian Elischer 	vp = nd.ni_vp;
257406ae1e91SMatthew Dillon 
2575832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2576832dafadSDon Lewis 	if (vp->v_type != VREG ||
2577832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2578832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2579832dafadSDon Lewis 		error = EFAULT;
2580832dafadSDon Lewis 		goto out2;
2581832dafadSDon Lewis 	}
2582832dafadSDon Lewis 
2583b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
258406ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
258506ae1e91SMatthew Dillon 	lf.l_start = 0;
258606ae1e91SMatthew Dillon 	lf.l_len = 0;
258706ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2588c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
258906ae1e91SMatthew Dillon 
259006ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
259106ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
259236bbf86bSRobert Watson 		if (locked)
259306ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2594b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2595f2a2857bSKirk McKusick 			return (error);
2596f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2597f2a2857bSKirk McKusick 			return (error);
2598f2a2857bSKirk McKusick 		goto restart;
2599f2a2857bSKirk McKusick 	}
2600fca666a1SJulian Elischer 
2601fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2602fca666a1SJulian Elischer 	vattr.va_size = 0;
260388b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2604b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2605b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
260688b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2607628d2653SJohn Baldwin 	PROC_LOCK(p);
2608fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2609628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2610fca666a1SJulian Elischer 
2611fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2612b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2613fca666a1SJulian Elischer 	  ENOSYS;
2614fca666a1SJulian Elischer 
2615c447f5b2SRobert Watson 	if (locked) {
261606ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
261706ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2618c447f5b2SRobert Watson 	}
2619f2a2857bSKirk McKusick 	vn_finished_write(mp);
262006ae1e91SMatthew Dillon out2:
2621b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2622fca666a1SJulian Elischer 	if (error == 0)
2623fca666a1SJulian Elischer 		error = error1;
2624fca666a1SJulian Elischer 	return (error);
2625fca666a1SJulian Elischer }
2626fca666a1SJulian Elischer 
2627fca666a1SJulian Elischer /*
2628df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2629df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2630df8bae1dSRodney W. Grimes  */
2631d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2632df8bae1dSRodney W. Grimes struct nosys_args {
2633df8bae1dSRodney W. Grimes 	int	dummy;
2634df8bae1dSRodney W. Grimes };
2635d2d3e875SBruce Evans #endif
2636fb99ab88SMatthew Dillon /*
2637fb99ab88SMatthew Dillon  * MPSAFE
2638fb99ab88SMatthew Dillon  */
2639df8bae1dSRodney W. Grimes /* ARGSUSED */
264026f9a767SRodney W. Grimes int
2641b40ce416SJulian Elischer nosys(td, args)
2642b40ce416SJulian Elischer 	struct thread *td;
2643df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2644df8bae1dSRodney W. Grimes {
2645b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2646b40ce416SJulian Elischer 
2647628d2653SJohn Baldwin 	PROC_LOCK(p);
2648df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2649628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2650f5216b9aSBruce Evans 	return (ENOSYS);
2651df8bae1dSRodney W. Grimes }
2652831d27a9SDon Lewis 
2653831d27a9SDon Lewis /*
265448f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2655831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2656831d27a9SDon Lewis  */
2657831d27a9SDon Lewis void
2658f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2659f1320723SAlfred Perlstein 	struct sigio **sigiop;
26602c42a146SMarcel Moolenaar 	int sig, checkctty;
2661831d27a9SDon Lewis {
2662f1320723SAlfred Perlstein 	struct sigio *sigio;
2663831d27a9SDon Lewis 
2664f1320723SAlfred Perlstein 	SIGIO_LOCK();
2665f1320723SAlfred Perlstein 	sigio = *sigiop;
2666f1320723SAlfred Perlstein 	if (sigio == NULL) {
2667f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2668f1320723SAlfred Perlstein 		return;
2669f1320723SAlfred Perlstein 	}
2670831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2671628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
26722b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
26732c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2674628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2675831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2676831d27a9SDon Lewis 		struct proc *p;
2677831d27a9SDon Lewis 
2678f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2679628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2680628d2653SJohn Baldwin 			PROC_LOCK(p);
26812b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2682831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
26832c42a146SMarcel Moolenaar 				psignal(p, sig);
2684628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2685628d2653SJohn Baldwin 		}
2686f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2687831d27a9SDon Lewis 	}
2688f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2689831d27a9SDon Lewis }
2690cb679c38SJonathan Lemon 
2691cb679c38SJonathan Lemon static int
2692cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2693cb679c38SJonathan Lemon {
2694cb679c38SJonathan Lemon 	struct proc *p = curproc;
2695cb679c38SJonathan Lemon 
2696cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2697cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2698cb679c38SJonathan Lemon 
2699628d2653SJohn Baldwin 	PROC_LOCK(p);
2700cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2701628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2702cb679c38SJonathan Lemon 
2703cb679c38SJonathan Lemon 	return (0);
2704cb679c38SJonathan Lemon }
2705cb679c38SJonathan Lemon 
2706cb679c38SJonathan Lemon static void
2707cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2708cb679c38SJonathan Lemon {
2709cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2710cb679c38SJonathan Lemon 
2711628d2653SJohn Baldwin 	PROC_LOCK(p);
2712e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2713628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2714cb679c38SJonathan Lemon }
2715cb679c38SJonathan Lemon 
2716cb679c38SJonathan Lemon /*
2717cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2718cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2719cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2720cb679c38SJonathan Lemon  * isn't worth the trouble.
2721cb679c38SJonathan Lemon  */
2722cb679c38SJonathan Lemon static int
2723cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2724cb679c38SJonathan Lemon {
2725cb679c38SJonathan Lemon 
2726cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2727cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2728cb679c38SJonathan Lemon 
2729cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2730cb679c38SJonathan Lemon 			kn->kn_data++;
2731cb679c38SJonathan Lemon 	}
2732cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2733cb679c38SJonathan Lemon }
273490af4afaSJohn Baldwin 
273590af4afaSJohn Baldwin struct sigacts *
273690af4afaSJohn Baldwin sigacts_alloc(void)
273790af4afaSJohn Baldwin {
273890af4afaSJohn Baldwin 	struct sigacts *ps;
273990af4afaSJohn Baldwin 
274090af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
274190af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
274290af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
274390af4afaSJohn Baldwin 	return (ps);
274490af4afaSJohn Baldwin }
274590af4afaSJohn Baldwin 
274690af4afaSJohn Baldwin void
274790af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
274890af4afaSJohn Baldwin {
274990af4afaSJohn Baldwin 
275090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
275190af4afaSJohn Baldwin 	ps->ps_refcnt--;
275290af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
275390af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
275490af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
275590af4afaSJohn Baldwin 	} else
275690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
275790af4afaSJohn Baldwin }
275890af4afaSJohn Baldwin 
275990af4afaSJohn Baldwin struct sigacts *
276090af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
276190af4afaSJohn Baldwin {
276290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
276390af4afaSJohn Baldwin 	ps->ps_refcnt++;
276490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
276590af4afaSJohn Baldwin 	return (ps);
276690af4afaSJohn Baldwin }
276790af4afaSJohn Baldwin 
276890af4afaSJohn Baldwin void
276990af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
277090af4afaSJohn Baldwin {
277190af4afaSJohn Baldwin 
277290af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
277390af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
277490af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
277590af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
277690af4afaSJohn Baldwin }
277790af4afaSJohn Baldwin 
277890af4afaSJohn Baldwin int
277990af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
278090af4afaSJohn Baldwin {
278190af4afaSJohn Baldwin 	int shared;
278290af4afaSJohn Baldwin 
278390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
278490af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
278590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
278690af4afaSJohn Baldwin 	return (shared);
278790af4afaSJohn Baldwin }
2788