xref: /freebsd/sys/kern/kern_sig.c (revision a30ec4b99c208c75dbc84b201beb239ba895d391)
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
1109df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
1110df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
1111b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1112b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1113b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1114df8bae1dSRodney W. Grimes  */
1115d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1116df8bae1dSRodney W. Grimes struct sigsuspend_args {
11172c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1118df8bae1dSRodney W. Grimes };
1119d2d3e875SBruce Evans #endif
1120fb99ab88SMatthew Dillon /*
1121fb99ab88SMatthew Dillon  * MPSAFE
1122fb99ab88SMatthew Dillon  */
1123df8bae1dSRodney W. Grimes /* ARGSUSED */
112426f9a767SRodney W. Grimes int
1125b40ce416SJulian Elischer sigsuspend(td, uap)
1126b40ce416SJulian Elischer 	struct thread *td;
1127df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1128df8bae1dSRodney W. Grimes {
11292c42a146SMarcel Moolenaar 	sigset_t mask;
11302c42a146SMarcel Moolenaar 	int error;
11312c42a146SMarcel Moolenaar 
11326f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
11332c42a146SMarcel Moolenaar 	if (error)
11342c42a146SMarcel Moolenaar 		return (error);
11358f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
11368f19eb88SIan Dowse }
11378f19eb88SIan Dowse 
11388f19eb88SIan Dowse int
11398f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
11408f19eb88SIan Dowse {
11418f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 	/*
1144645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1145df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1146df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1147df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1148df8bae1dSRodney W. Grimes 	 * to indicate this.
1149df8bae1dSRodney W. Grimes 	 */
1150628d2653SJohn Baldwin 	PROC_LOCK(p);
11514093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11525e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1153645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11544093529dSJeff Roberson 	td->td_sigmask = mask;
11554093529dSJeff Roberson 	signotify(td);
1156b1bf1c3aSJohn Baldwin 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11572c42a146SMarcel Moolenaar 		/* void */;
1158628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11592c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11602c42a146SMarcel Moolenaar 	return (EINTR);
11612c42a146SMarcel Moolenaar }
11622c42a146SMarcel Moolenaar 
116331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
11642c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11652c42a146SMarcel Moolenaar struct osigsuspend_args {
11662c42a146SMarcel Moolenaar 	osigset_t mask;
11672c42a146SMarcel Moolenaar };
11682c42a146SMarcel Moolenaar #endif
1169fb99ab88SMatthew Dillon /*
1170fb99ab88SMatthew Dillon  * MPSAFE
1171fb99ab88SMatthew Dillon  */
11722c42a146SMarcel Moolenaar /* ARGSUSED */
11732c42a146SMarcel Moolenaar int
1174b40ce416SJulian Elischer osigsuspend(td, uap)
1175b40ce416SJulian Elischer 	struct thread *td;
11762c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11772c42a146SMarcel Moolenaar {
1178b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1179645682fdSLuoqi Chen 	sigset_t mask;
11802c42a146SMarcel Moolenaar 
1181628d2653SJohn Baldwin 	PROC_LOCK(p);
11824093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11835e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1184645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1185645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11864093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11874093529dSJeff Roberson 	signotify(td);
1188b1bf1c3aSJohn Baldwin 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1189df8bae1dSRodney W. Grimes 		/* void */;
1190628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1191df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1192df8bae1dSRodney W. Grimes 	return (EINTR);
1193df8bae1dSRodney W. Grimes }
119431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1197d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1198df8bae1dSRodney W. Grimes struct osigstack_args {
1199df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1200df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1201df8bae1dSRodney W. Grimes };
1202d2d3e875SBruce Evans #endif
1203fb99ab88SMatthew Dillon /*
1204fb99ab88SMatthew Dillon  * MPSAFE
1205fb99ab88SMatthew Dillon  */
1206df8bae1dSRodney W. Grimes /* ARGSUSED */
120726f9a767SRodney W. Grimes int
1208b40ce416SJulian Elischer osigstack(td, uap)
1209b40ce416SJulian Elischer 	struct thread *td;
1210df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1211df8bae1dSRodney W. Grimes {
12125afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1213fb99ab88SMatthew Dillon 	int error = 0;
1214fb99ab88SMatthew Dillon 
1215d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
12165afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
12175afe0c99SJohn Baldwin 		if (error)
12185afe0c99SJohn Baldwin 			return (error);
1219df8bae1dSRodney W. Grimes 	}
1220a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
12215afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
12225afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1223a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1224a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1225a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1226a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
12275afe0c99SJohn Baldwin 	}
12285afe0c99SJohn Baldwin 	if (uap->oss != NULL)
12295afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
12305afe0c99SJohn Baldwin 
1231fb99ab88SMatthew Dillon 	return (error);
1232df8bae1dSRodney W. Grimes }
1233df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1234df8bae1dSRodney W. Grimes 
1235d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1236df8bae1dSRodney W. Grimes struct sigaltstack_args {
12372c42a146SMarcel Moolenaar 	stack_t	*ss;
12382c42a146SMarcel Moolenaar 	stack_t	*oss;
1239df8bae1dSRodney W. Grimes };
1240d2d3e875SBruce Evans #endif
1241fb99ab88SMatthew Dillon /*
1242fb99ab88SMatthew Dillon  * MPSAFE
1243fb99ab88SMatthew Dillon  */
1244df8bae1dSRodney W. Grimes /* ARGSUSED */
124526f9a767SRodney W. Grimes int
1246b40ce416SJulian Elischer sigaltstack(td, uap)
1247b40ce416SJulian Elischer 	struct thread *td;
1248df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1249df8bae1dSRodney W. Grimes {
12508f19eb88SIan Dowse 	stack_t ss, oss;
12518f19eb88SIan Dowse 	int error;
12528f19eb88SIan Dowse 
12538f19eb88SIan Dowse 	if (uap->ss != NULL) {
12548f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12558f19eb88SIan Dowse 		if (error)
12568f19eb88SIan Dowse 			return (error);
12578f19eb88SIan Dowse 	}
12588f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12598f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12608f19eb88SIan Dowse 	if (error)
12618f19eb88SIan Dowse 		return (error);
12628f19eb88SIan Dowse 	if (uap->oss != NULL)
12638f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12648f19eb88SIan Dowse 	return (error);
12658f19eb88SIan Dowse }
12668f19eb88SIan Dowse 
12678f19eb88SIan Dowse int
12688f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12698f19eb88SIan Dowse {
1270b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1271fb99ab88SMatthew Dillon 	int oonstack;
1272fb99ab88SMatthew Dillon 
1273b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1274d034d459SMarcel Moolenaar 
12758f19eb88SIan Dowse 	if (oss != NULL) {
1276a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1277a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1278d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1279df8bae1dSRodney W. Grimes 	}
1280d034d459SMarcel Moolenaar 
12818f19eb88SIan Dowse 	if (ss != NULL) {
1282a30ec4b9SDavid Xu 		if (oonstack)
1283cf60731bSJohn Baldwin 			return (EPERM);
1284a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1285cf60731bSJohn Baldwin 			return (EINVAL);
12868f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12878f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1288cf60731bSJohn Baldwin 				return (ENOMEM);
1289fb99ab88SMatthew Dillon 			}
1290a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1291a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1292628d2653SJohn Baldwin 		} else {
1293a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1294628d2653SJohn Baldwin 		}
1295d034d459SMarcel Moolenaar 	}
1296cf60731bSJohn Baldwin 	return (0);
1297df8bae1dSRodney W. Grimes }
1298df8bae1dSRodney W. Grimes 
1299d93f860cSPoul-Henning Kamp /*
1300d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1301d93f860cSPoul-Henning Kamp  * cp is calling process.
1302d93f860cSPoul-Henning Kamp  */
130337c84183SPoul-Henning Kamp static int
13049c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
13059c1ab3e0SJohn Baldwin 	register struct thread *td;
13062c42a146SMarcel Moolenaar 	int sig, pgid, all;
1307d93f860cSPoul-Henning Kamp {
1308d93f860cSPoul-Henning Kamp 	register struct proc *p;
1309d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1310d93f860cSPoul-Henning Kamp 	int nfound = 0;
1311d93f860cSPoul-Henning Kamp 
1312553629ebSJake Burkholder 	if (all) {
1313d93f860cSPoul-Henning Kamp 		/*
1314d93f860cSPoul-Henning Kamp 		 * broadcast
1315d93f860cSPoul-Henning Kamp 		 */
13161005a129SJohn Baldwin 		sx_slock(&allproc_lock);
13172e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1318628d2653SJohn Baldwin 			PROC_LOCK(p);
13199c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
13209c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1321628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1322628d2653SJohn Baldwin 				continue;
1323628d2653SJohn Baldwin 			}
1324f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1325d93f860cSPoul-Henning Kamp 				nfound++;
132633a9ed9dSJohn Baldwin 				if (sig)
13272c42a146SMarcel Moolenaar 					psignal(p, sig);
1328628d2653SJohn Baldwin 			}
132933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1330d93f860cSPoul-Henning Kamp 		}
13311005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1332553629ebSJake Burkholder 	} else {
1333ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1334f591779bSSeigo Tanimura 		if (pgid == 0) {
1335d93f860cSPoul-Henning Kamp 			/*
1336d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1337d93f860cSPoul-Henning Kamp 			 */
13389c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1339f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1340f591779bSSeigo Tanimura 		} else {
1341d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1342f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1343ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1344d93f860cSPoul-Henning Kamp 				return (ESRCH);
1345d93f860cSPoul-Henning Kamp 			}
1346f591779bSSeigo Tanimura 		}
1347ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13482e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1349628d2653SJohn Baldwin 			PROC_LOCK(p);
1350628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1351628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1352628d2653SJohn Baldwin 				continue;
1353628d2653SJohn Baldwin 			}
1354e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
135533a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1356628d2653SJohn Baldwin 				continue;
1357628d2653SJohn Baldwin 			}
1358f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1359d93f860cSPoul-Henning Kamp 				nfound++;
136033a9ed9dSJohn Baldwin 				if (sig)
13612c42a146SMarcel Moolenaar 					psignal(p, sig);
1362628d2653SJohn Baldwin 			}
136333a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1364d93f860cSPoul-Henning Kamp 		}
1365f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1366d93f860cSPoul-Henning Kamp 	}
1367d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1368d93f860cSPoul-Henning Kamp }
1369d93f860cSPoul-Henning Kamp 
1370d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1371df8bae1dSRodney W. Grimes struct kill_args {
1372df8bae1dSRodney W. Grimes 	int	pid;
1373df8bae1dSRodney W. Grimes 	int	signum;
1374df8bae1dSRodney W. Grimes };
1375d2d3e875SBruce Evans #endif
1376fb99ab88SMatthew Dillon /*
1377fb99ab88SMatthew Dillon  * MPSAFE
1378fb99ab88SMatthew Dillon  */
1379df8bae1dSRodney W. Grimes /* ARGSUSED */
138026f9a767SRodney W. Grimes int
1381b40ce416SJulian Elischer kill(td, uap)
1382b40ce416SJulian Elischer 	register struct thread *td;
1383df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1384df8bae1dSRodney W. Grimes {
1385df8bae1dSRodney W. Grimes 	register struct proc *p;
138690af4afaSJohn Baldwin 	int error;
1387df8bae1dSRodney W. Grimes 
13886c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1389df8bae1dSRodney W. Grimes 		return (EINVAL);
1390fb99ab88SMatthew Dillon 
1391df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1392df8bae1dSRodney W. Grimes 		/* kill single process */
139390af4afaSJohn Baldwin 		if ((p = pfind(uap->pid)) == NULL)
139490af4afaSJohn Baldwin 			return (ESRCH);
139590af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
139690af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1397df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1398628d2653SJohn Baldwin 		PROC_UNLOCK(p);
139990af4afaSJohn Baldwin 		return (error);
1400df8bae1dSRodney W. Grimes 	}
1401df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1402df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
140390af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1404df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
140590af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1406df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
140790af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1408df8bae1dSRodney W. Grimes 	}
140990af4afaSJohn Baldwin 	/* NOTREACHED */
1410df8bae1dSRodney W. Grimes }
1411df8bae1dSRodney W. Grimes 
1412df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1413d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1414df8bae1dSRodney W. Grimes struct okillpg_args {
1415df8bae1dSRodney W. Grimes 	int	pgid;
1416df8bae1dSRodney W. Grimes 	int	signum;
1417df8bae1dSRodney W. Grimes };
1418d2d3e875SBruce Evans #endif
1419fb99ab88SMatthew Dillon /*
1420fb99ab88SMatthew Dillon  * MPSAFE
1421fb99ab88SMatthew Dillon  */
1422df8bae1dSRodney W. Grimes /* ARGSUSED */
142326f9a767SRodney W. Grimes int
1424b40ce416SJulian Elischer okillpg(td, uap)
1425b40ce416SJulian Elischer 	struct thread *td;
1426df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1427df8bae1dSRodney W. Grimes {
1428df8bae1dSRodney W. Grimes 
14296c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1430df8bae1dSRodney W. Grimes 		return (EINVAL);
143190af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1432df8bae1dSRodney W. Grimes }
1433df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1434df8bae1dSRodney W. Grimes 
1435df8bae1dSRodney W. Grimes /*
1436df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1437df8bae1dSRodney W. Grimes  */
1438df8bae1dSRodney W. Grimes void
14392c42a146SMarcel Moolenaar gsignal(pgid, sig)
14402c42a146SMarcel Moolenaar 	int pgid, sig;
1441df8bae1dSRodney W. Grimes {
1442df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1443df8bae1dSRodney W. Grimes 
1444f591779bSSeigo Tanimura 	if (pgid != 0) {
1445ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1446f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1447ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1448f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14492c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1450f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1451f591779bSSeigo Tanimura 		}
1452f591779bSSeigo Tanimura 	}
1453df8bae1dSRodney W. Grimes }
1454df8bae1dSRodney W. Grimes 
1455df8bae1dSRodney W. Grimes /*
1456df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1457df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1458df8bae1dSRodney W. Grimes  */
1459df8bae1dSRodney W. Grimes void
14602c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1461df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14622c42a146SMarcel Moolenaar 	int sig, checkctty;
1463df8bae1dSRodney W. Grimes {
1464df8bae1dSRodney W. Grimes 	register struct proc *p;
1465df8bae1dSRodney W. Grimes 
1466628d2653SJohn Baldwin 	if (pgrp) {
1467f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1468628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1469628d2653SJohn Baldwin 			PROC_LOCK(p);
1470df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14712c42a146SMarcel Moolenaar 				psignal(p, sig);
1472628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1473628d2653SJohn Baldwin 		}
1474628d2653SJohn Baldwin 	}
1475df8bae1dSRodney W. Grimes }
1476df8bae1dSRodney W. Grimes 
1477df8bae1dSRodney W. Grimes /*
14781bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1479df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1480df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1481356861dbSMatthew Dillon  *
1482356861dbSMatthew Dillon  * MPSAFE
1483df8bae1dSRodney W. Grimes  */
1484df8bae1dSRodney W. Grimes void
14851bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1486df8bae1dSRodney W. Grimes {
14871bf4700bSJeff Roberson 	struct sigacts *ps;
14881bf4700bSJeff Roberson 	struct proc *p;
14899dde3bc9SDavid Xu 	siginfo_t siginfo;
14909dde3bc9SDavid Xu 	int error;
14911bf4700bSJeff Roberson 
14921bf4700bSJeff Roberson 	p = td->td_proc;
14939dde3bc9SDavid Xu 	if (td->td_flags & TDF_SA) {
14947eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
14959dde3bc9SDavid Xu 			thread_user_enter(p, td);
1496628d2653SJohn Baldwin 		PROC_LOCK(p);
14979dde3bc9SDavid Xu 		if (td->td_mailbox) {
14989dde3bc9SDavid Xu 			SIGDELSET(td->td_sigmask, sig);
14999dde3bc9SDavid Xu 			mtx_lock_spin(&sched_lock);
15009dde3bc9SDavid Xu 			/*
15019dde3bc9SDavid Xu 			 * Force scheduling an upcall, so UTS has chance to
15029dde3bc9SDavid Xu 			 * process the signal before thread runs again in
15039dde3bc9SDavid Xu 			 * userland.
15049dde3bc9SDavid Xu 			 */
15059dde3bc9SDavid Xu 			if (td->td_upcall)
15069dde3bc9SDavid Xu 				td->td_upcall->ku_flags |= KUF_DOUPCALL;
15079dde3bc9SDavid Xu 			mtx_unlock_spin(&sched_lock);
1508432b45deSDavid Xu 		} else {
1509432b45deSDavid Xu 			/* UTS caused a sync signal */
15101fc434dcSDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
15111fc434dcSDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
15121fc434dcSDavid Xu 			sigexit(td, sig);
15139dde3bc9SDavid Xu 		}
15149dde3bc9SDavid Xu 	} else {
15159dde3bc9SDavid Xu 		PROC_LOCK(p);
15169dde3bc9SDavid Xu 	}
1517ef3dab76STim J. Robbins 	ps = p->p_sigacts;
151890af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
151990af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
15204093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1521df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1522df8bae1dSRodney W. Grimes #ifdef KTRACE
1523374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1524374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
15254093529dSJeff Roberson 			    &td->td_sigmask, code);
1526df8bae1dSRodney W. Grimes #endif
15279dde3bc9SDavid Xu 		if (!(td->td_flags & TDF_SA))
15289dde3bc9SDavid Xu 			(*p->p_sysent->sv_sendsig)(
15299dde3bc9SDavid Xu 				ps->ps_sigact[_SIG_IDX(sig)], sig,
15304093529dSJeff Roberson 				&td->td_sigmask, code);
15319dde3bc9SDavid Xu 		else {
15324b7d5d84SDavid Xu 			cpu_thread_siginfo(sig, code, &siginfo);
15339dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
15349dde3bc9SDavid Xu 			PROC_UNLOCK(p);
15359dde3bc9SDavid Xu 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
15369dde3bc9SDavid Xu 			    sizeof(siginfo));
15379dde3bc9SDavid Xu 			PROC_LOCK(p);
1538432b45deSDavid Xu 			/* UTS memory corrupted */
15399dde3bc9SDavid Xu 			if (error)
15409dde3bc9SDavid Xu 				sigexit(td, SIGILL);
15419dde3bc9SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
15429dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
15439dde3bc9SDavid Xu 		}
15444093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
15452c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
15464093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
15472c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1548289ccde0SPeter Wemm 			/*
15498f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1550289ccde0SPeter Wemm 			 */
155190af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
15522c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
15532c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
155490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
15552c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1556dedc04feSPeter Wemm 		}
155790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15586f841fb7SMarcel Moolenaar 	} else {
155990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
15606626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
15612c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1562c197abc4SMike Makonnen 		tdsignal(td, sig, SIGTARGET_TD);
1563df8bae1dSRodney W. Grimes 	}
1564628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1565df8bae1dSRodney W. Grimes }
1566df8bae1dSRodney W. Grimes 
15674093529dSJeff Roberson static struct thread *
15684093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15694093529dSJeff Roberson {
15703074d1b4SDavid Xu 	struct thread *td, *signal_td;
15714093529dSJeff Roberson 
15724093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15734093529dSJeff Roberson 
15744093529dSJeff Roberson 	/*
15753074d1b4SDavid Xu 	 * First find a thread in sigwait state and signal belongs to
15763074d1b4SDavid Xu 	 * its wait set. POSIX's arguments is that speed of delivering signal
15773074d1b4SDavid Xu 	 * to sigwait thread is faster than delivering signal to user stack.
15783074d1b4SDavid Xu 	 * If we can not find sigwait thread, then find the first thread in
15793074d1b4SDavid Xu 	 * the proc that doesn't have this signal masked, an exception is
15803074d1b4SDavid Xu 	 * if current thread is sending signal to its process, and it does not
15813074d1b4SDavid Xu 	 * mask the signal, it should get the signal, this is another fast
15823074d1b4SDavid Xu 	 * way to deliver signal.
15834093529dSJeff Roberson 	 */
15843074d1b4SDavid Xu 	signal_td = NULL;
1585a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
15863074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
15873074d1b4SDavid Xu 		if (td->td_waitset != NULL &&
1588a9a48d68SDavid Xu 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1589a9a48d68SDavid Xu 				mtx_unlock_spin(&sched_lock);
15904093529dSJeff Roberson 				return (td);
1591a9a48d68SDavid Xu 		}
15923074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
15933074d1b4SDavid Xu 			if (td == curthread)
15943074d1b4SDavid Xu 				signal_td = curthread;
15953074d1b4SDavid Xu 			else if (signal_td == NULL)
15963074d1b4SDavid Xu 				signal_td = td;
15973074d1b4SDavid Xu 		}
15983074d1b4SDavid Xu 	}
15993074d1b4SDavid Xu 	if (signal_td == NULL)
16003074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1601a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
16023074d1b4SDavid Xu 	return (signal_td);
16034093529dSJeff Roberson }
16044093529dSJeff Roberson 
1605df8bae1dSRodney W. Grimes /*
1606df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1607df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1608df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1609df8bae1dSRodney W. Grimes  *
1610df8bae1dSRodney W. Grimes  * Exceptions:
1611df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1612df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1613df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1614df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1615df8bae1dSRodney W. Grimes  *
1616df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
161790af4afaSJohn Baldwin  *
161890af4afaSJohn Baldwin  * MPSAFE
1619df8bae1dSRodney W. Grimes  */
1620df8bae1dSRodney W. Grimes void
16214093529dSJeff Roberson psignal(struct proc *p, int sig)
1622df8bae1dSRodney W. Grimes {
1623b40ce416SJulian Elischer 	struct thread *td;
16244093529dSJeff Roberson 	int prop;
16254093529dSJeff Roberson 
162641b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
162741b3077aSJacques Vidrine 		panic("psignal(): invalid signal");
162841b3077aSJacques Vidrine 
16294093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
16304093529dSJeff Roberson 	prop = sigprop(sig);
16314093529dSJeff Roberson 
16324093529dSJeff Roberson 	/*
16334093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
16344093529dSJeff Roberson 	 */
16354093529dSJeff Roberson 	td = sigtd(p, sig, prop);
16364093529dSJeff Roberson 
1637c197abc4SMike Makonnen 	tdsignal(td, sig, SIGTARGET_P);
16384093529dSJeff Roberson }
16394093529dSJeff Roberson 
164090af4afaSJohn Baldwin /*
164190af4afaSJohn Baldwin  * MPSAFE
164290af4afaSJohn Baldwin  */
16434093529dSJeff Roberson void
1644c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target)
16454093529dSJeff Roberson {
16469dde3bc9SDavid Xu 	sigset_t saved;
16479dde3bc9SDavid Xu 	struct proc *p = td->td_proc;
16489dde3bc9SDavid Xu 
16499dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
16509dde3bc9SDavid Xu 		saved = p->p_siglist;
1651c197abc4SMike Makonnen 	do_tdsignal(td, sig, target);
16529dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
16539dde3bc9SDavid Xu 		if (SIGSETEQ(saved, p->p_siglist))
16549dde3bc9SDavid Xu 			return;
16559dde3bc9SDavid Xu 		else {
16569dde3bc9SDavid Xu 			/* pending set changed */
16579dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
16589dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
16599dde3bc9SDavid Xu 		}
16609dde3bc9SDavid Xu 	}
16619dde3bc9SDavid Xu }
16629dde3bc9SDavid Xu 
16639dde3bc9SDavid Xu static void
1664c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target)
16659dde3bc9SDavid Xu {
16664093529dSJeff Roberson 	struct proc *p;
16674093529dSJeff Roberson 	register sig_t action;
16684093529dSJeff Roberson 	sigset_t *siglist;
16694093529dSJeff Roberson 	struct thread *td0;
1670e602ba25SJulian Elischer 	register int prop;
167190af4afaSJohn Baldwin 	struct sigacts *ps;
1672df8bae1dSRodney W. Grimes 
167341b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
167441b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
16754093529dSJeff Roberson 
16764093529dSJeff Roberson 	p = td->td_proc;
167790af4afaSJohn Baldwin 	ps = p->p_sigacts;
16782c42a146SMarcel Moolenaar 
1679628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1680cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1681cb679c38SJonathan Lemon 
16822c42a146SMarcel Moolenaar 	prop = sigprop(sig);
16834093529dSJeff Roberson 
16844093529dSJeff Roberson 	/*
168514b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
168614b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
168714b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
16884093529dSJeff Roberson 	 */
16893074d1b4SDavid Xu 	if (target == SIGTARGET_TD) {
16903074d1b4SDavid Xu 		siglist = &td->td_siglist;
16913074d1b4SDavid Xu 	} else {
16923074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig))
16933074d1b4SDavid Xu 			siglist = &td->td_siglist;
16943074d1b4SDavid Xu 		else if (td->td_waitset != NULL &&
16953074d1b4SDavid Xu 			SIGISMEMBER(*(td->td_waitset), sig))
16963074d1b4SDavid Xu 			siglist = &td->td_siglist;
16973074d1b4SDavid Xu 		else
16983074d1b4SDavid Xu 			siglist = &p->p_siglist;
16993074d1b4SDavid Xu 	}
17004093529dSJeff Roberson 
1701df8bae1dSRodney W. Grimes 	/*
17022a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
17032a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
17042a024a2bSSean Eric Fagan 	 * a chance, as well.
1705df8bae1dSRodney W. Grimes 	 */
1706b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1707df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1708b40ce416SJulian Elischer 	} else {
1709df8bae1dSRodney W. Grimes 		/*
1710df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1711df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
171290af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1713df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1714df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1715df8bae1dSRodney W. Grimes 		 */
171690af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
171790af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
171890af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
171990af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1720df8bae1dSRodney W. Grimes 			return;
172190af4afaSJohn Baldwin 		}
17223074d1b4SDavid Xu 		if (((td->td_waitset == NULL) &&
17233074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig)) ||
17243074d1b4SDavid Xu 		    ((td->td_waitset != NULL) &&
17253074d1b4SDavid Xu 		     SIGISMEMBER(td->td_sigmask, sig) &&
17263074d1b4SDavid Xu 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1727df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
172890af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1729df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1730df8bae1dSRodney W. Grimes 		else
1731df8bae1dSRodney W. Grimes 			action = SIG_DFL;
173290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1733df8bae1dSRodney W. Grimes 	}
1734df8bae1dSRodney W. Grimes 
17354093529dSJeff Roberson 	if (prop & SA_CONT) {
17361d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
17374093529dSJeff Roberson 		/*
17384093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
17394093529dSJeff Roberson 		 * the proc's siglist.
17404093529dSJeff Roberson 		 */
1741a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17424093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17434093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
1744a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17454093529dSJeff Roberson 	}
1746df8bae1dSRodney W. Grimes 
1747df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1748df8bae1dSRodney W. Grimes 		/*
1749df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1750df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1751df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1752df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1753df8bae1dSRodney W. Grimes 		 */
1754e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1755e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1756e602ba25SJulian Elischer 		    (action == SIG_DFL))
1757df8bae1dSRodney W. Grimes 		        return;
17581d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
1759a9a48d68SDavid Xu 		mtx_lock_spin(&sched_lock);
17604093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
17614093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
1762a9a48d68SDavid Xu 		mtx_unlock_spin(&sched_lock);
17636933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1764df8bae1dSRodney W. Grimes 	}
17653074d1b4SDavid Xu 
17664093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
17674093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
17683074d1b4SDavid Xu 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
17693074d1b4SDavid Xu 	    action != SIG_HOLD) {
17703074d1b4SDavid Xu 		td->td_waitset = NULL;
17713074d1b4SDavid Xu 	}
17723074d1b4SDavid Xu 
17735312b1c7SDavid Xu 	/*
17745312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
17755312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
17765312b1c7SDavid Xu 	 */
17775312b1c7SDavid Xu 	if (action == SIG_HOLD &&
17785312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
17795312b1c7SDavid Xu 		return;
1780df8bae1dSRodney W. Grimes 	/*
1781e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1782e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1783e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1784e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1785e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1786e602ba25SJulian Elischer 	 * We try do the per-process part here.
1787df8bae1dSRodney W. Grimes 	 */
1788e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1789e602ba25SJulian Elischer 		/*
1790e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1791e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1792e602ba25SJulian Elischer 		 */
1793e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1794e602ba25SJulian Elischer 			/*
1795e602ba25SJulian Elischer 			 * The traced process is already stopped,
1796e602ba25SJulian Elischer 			 * so no further action is necessary.
1797e602ba25SJulian Elischer 			 * No signal can restart us.
1798e602ba25SJulian Elischer 			 */
1799e602ba25SJulian Elischer 			goto out;
18001c32c37cSJohn Baldwin 		}
1801b40ce416SJulian Elischer 
1802e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1803df8bae1dSRodney W. Grimes 			/*
1804e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1805e602ba25SJulian Elischer 			 * It will die elsewhere.
1806e602ba25SJulian Elischer 			 * All threads must be restarted.
1807df8bae1dSRodney W. Grimes 			 */
1808e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1809e602ba25SJulian Elischer 			goto runfast;
1810e602ba25SJulian Elischer 		}
1811e602ba25SJulian Elischer 
1812e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1813e602ba25SJulian Elischer 			/*
1814e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
18154093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
18161d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1817e602ba25SJulian Elischer 			 * continue the process and leave the signal in
18184093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1819e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1820e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1821e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1822e602ba25SJulian Elischer 			 */
18231279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
18246933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1825e602ba25SJulian Elischer 			if (action == SIG_DFL) {
18264093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1827e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1828e602ba25SJulian Elischer 				/*
1829e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1830e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1831e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1832e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1833e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1834e602ba25SJulian Elischer 				 * process, we need to make sure that the
1835e602ba25SJulian Elischer 				 * single thread is runnable asap.
1836e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1837e602ba25SJulian Elischer 				 */
1838e602ba25SJulian Elischer 				goto runfast;
1839e602ba25SJulian Elischer 			}
1840e602ba25SJulian Elischer 			/*
1841e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1842e602ba25SJulian Elischer 			 */
1843e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
184404774f23SJulian Elischer 			thread_unsuspend(p);
1845e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1846e602ba25SJulian Elischer 			goto out;
1847e602ba25SJulian Elischer 		}
1848e602ba25SJulian Elischer 
1849e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1850e602ba25SJulian Elischer 			/*
1851e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1852e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
185304774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1854e602ba25SJulian Elischer 			 */
18551279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
18564093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1857e602ba25SJulian Elischer 			goto out;
1858e602ba25SJulian Elischer 		}
1859e602ba25SJulian Elischer 
1860e602ba25SJulian Elischer 		/*
1861e602ba25SJulian Elischer 		 * All other kinds of signals:
1862e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1863e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1864e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
186504774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1866e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1867e602ba25SJulian Elischer 		 */
1868e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
186971fad9fdSJulian Elischer 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1870e602ba25SJulian Elischer 			if (td->td_flags & TDF_CVWAITQ)
187171fad9fdSJulian Elischer 				cv_abort(td);
1872e602ba25SJulian Elischer 			else
187371fad9fdSJulian Elischer 				abortsleep(td);
1874e602ba25SJulian Elischer 		}
1875e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1876df8bae1dSRodney W. Grimes 		goto out;
1877df8bae1dSRodney W. Grimes 		/*
1878e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1879e602ba25SJulian Elischer 		 * Shouldn't they abort too?
188004774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
188104774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1882df8bae1dSRodney W. Grimes 		 */
1883e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
188435c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
188535c32a76SDavid Xu 			!(prop & SA_STOP)) {
1886721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
18874093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1888721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1889df8bae1dSRodney W. Grimes 			goto out;
189004774f23SJulian Elischer 		}
189135c32a76SDavid Xu 		if (prop & SA_STOP) {
189235c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
189335c32a76SDavid Xu 				goto out;
1894e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1895e574e444SDavid Xu 			p->p_xstat = sig;
189635c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
18974093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
18984093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1899062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1900062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
19014093529dSJeff Roberson 					thread_suspend_one(td0);
1902062cf543SDavid Xu 				} else if (td != td0) {
1903062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1904062cf543SDavid Xu 				}
190535c32a76SDavid Xu 			}
1906e574e444SDavid Xu 			thread_stopped(p);
19074093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1908e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
19094093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
19104093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
19114093529dSJeff Roberson 			}
191235c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
191335c32a76SDavid Xu 			goto out;
191435c32a76SDavid Xu 		}
1915721e5910SJulian Elischer 		else
1916df8bae1dSRodney W. Grimes 			goto runfast;
1917df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1918e602ba25SJulian Elischer 	} else {
1919e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
19204093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1921e602ba25SJulian Elischer 		goto out;
1922e602ba25SJulian Elischer 	}
1923e602ba25SJulian Elischer 
1924b40ce416SJulian Elischer 	/*
1925e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1926e602ba25SJulian Elischer 	 * running threads.
1927b40ce416SJulian Elischer 	 */
1928e602ba25SJulian Elischer 
1929e602ba25SJulian Elischer runfast:
1930aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
19314093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1932e602ba25SJulian Elischer 	thread_unsuspend(p);
1933e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1934e602ba25SJulian Elischer out:
1935e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1936e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1937e602ba25SJulian Elischer }
1938e602ba25SJulian Elischer 
1939e602ba25SJulian Elischer /*
1940e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1941e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1942e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1943e602ba25SJulian Elischer  */
1944e602ba25SJulian Elischer static void
19454093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1946e602ba25SJulian Elischer {
1947e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1948e602ba25SJulian Elischer 	register int prop;
1949e602ba25SJulian Elischer 
19508b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1951aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1952e602ba25SJulian Elischer 	prop = sigprop(sig);
1953e602ba25SJulian Elischer 	/*
1954aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1955e602ba25SJulian Elischer 	 * killed in this lifetime.
1956e602ba25SJulian Elischer 	 */
1957e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1958e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1959e602ba25SJulian Elischer 			td->td_priority = PUSER;
1960b40ce416SJulian Elischer 		}
1961b40ce416SJulian Elischer 	}
196271fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1963e602ba25SJulian Elischer 		/*
1964e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1965e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1966e602ba25SJulian Elischer 		 * be noticed when the process returns through
1967e602ba25SJulian Elischer 		 * trap() or syscall().
1968e602ba25SJulian Elischer 		 */
1969e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1970aa0fa334SJulian Elischer 			return;
1971e602ba25SJulian Elischer 		}
1972e602ba25SJulian Elischer 		/*
1973e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1974e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1975e602ba25SJulian Elischer 		 * for its parent.
1976e602ba25SJulian Elischer 		 */
1977e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1978e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1979aa0fa334SJulian Elischer 		} else {
1980aa0fa334SJulian Elischer 
1981df8bae1dSRodney W. Grimes 			/*
1982e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1983e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1984e602ba25SJulian Elischer 			 * be awakened.
1985df8bae1dSRodney W. Grimes 			 */
1986e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
19871d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
19884093529dSJeff Roberson 				/*
19894093529dSJeff Roberson 				 * It may be on either list in this state.
19904093529dSJeff Roberson 				 * Remove from both for now.
19914093529dSJeff Roberson 				 */
19924093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1993aa0fa334SJulian Elischer 				return;
1994df8bae1dSRodney W. Grimes 			}
1995df8bae1dSRodney W. Grimes 
1996aa0fa334SJulian Elischer 			/*
1997aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1998aa0fa334SJulian Elischer 			 */
1999aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
2000aa0fa334SJulian Elischer 				td->td_priority = PUSER;
2001aa0fa334SJulian Elischer 			}
2002aa0fa334SJulian Elischer 		}
200371fad9fdSJulian Elischer 		if (td->td_flags & TDF_CVWAITQ)
200471fad9fdSJulian Elischer 			cv_abort(td);
200571fad9fdSJulian Elischer 		else
200671fad9fdSJulian Elischer 			abortsleep(td);
2007aa0fa334SJulian Elischer 	}
2008aa0fa334SJulian Elischer #ifdef SMP
2009aa0fa334SJulian Elischer 	  else {
2010df8bae1dSRodney W. Grimes 		/*
2011e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
2012df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2013df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2014df8bae1dSRodney W. Grimes 		 */
201571fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td) && td != curthread) {
2016e602ba25SJulian Elischer 			forward_signal(td);
2017aa0fa334SJulian Elischer 		}
20180ac3b636SAndrew Gallatin 	  }
20193163861cSTor Egge #endif
20206caa8a15SJohn Baldwin }
2021df8bae1dSRodney W. Grimes 
20224cc9f52fSRobert Drehmel void
20234cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
20244cc9f52fSRobert Drehmel {
20254cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
20264cc9f52fSRobert Drehmel 
20274cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
20284cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
20294cc9f52fSRobert Drehmel 
20304cc9f52fSRobert Drehmel 	p->p_xstat = sig;
20314cc9f52fSRobert Drehmel 	PROC_LOCK(p->p_pptr);
20324cc9f52fSRobert Drehmel 	psignal(p->p_pptr, SIGCHLD);
20334cc9f52fSRobert Drehmel 	PROC_UNLOCK(p->p_pptr);
20344cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
20354cc9f52fSRobert Drehmel 	stop(p);	/* uses schedlock too eventually */
20364cc9f52fSRobert Drehmel 	thread_suspend_one(td);
20374cc9f52fSRobert Drehmel 	PROC_UNLOCK(p);
20384cc9f52fSRobert Drehmel 	DROP_GIANT();
20394cc9f52fSRobert Drehmel 	p->p_stats->p_ru.ru_nivcsw++;
20404cc9f52fSRobert Drehmel 	mi_switch();
20414cc9f52fSRobert Drehmel 	mtx_unlock_spin(&sched_lock);
20424cc9f52fSRobert Drehmel 	PICKUP_GIANT();
20434cc9f52fSRobert Drehmel }
20444cc9f52fSRobert Drehmel 
2045df8bae1dSRodney W. Grimes /*
2046df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2047df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2048df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2049df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2050df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2051628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2052df8bae1dSRodney W. Grimes  * sequence is
2053df8bae1dSRodney W. Grimes  *
2054e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
20552c42a146SMarcel Moolenaar  *		postsig(sig);
2056df8bae1dSRodney W. Grimes  */
20576711f10fSJohn Baldwin static int
2058e602ba25SJulian Elischer issignal(td)
2059e602ba25SJulian Elischer 	struct thread *td;
2060df8bae1dSRodney W. Grimes {
2061e602ba25SJulian Elischer 	struct proc *p;
206290af4afaSJohn Baldwin 	struct sigacts *ps;
20634093529dSJeff Roberson 	sigset_t sigpending;
2064062cf543SDavid Xu 	int sig, prop;
2065062cf543SDavid Xu 	struct thread *td0;
2066df8bae1dSRodney W. Grimes 
2067e602ba25SJulian Elischer 	p = td->td_proc;
206890af4afaSJohn Baldwin 	ps = p->p_sigacts;
206990af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2070628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2071df8bae1dSRodney W. Grimes 	for (;;) {
20722a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
20732a024a2bSSean Eric Fagan 
20744093529dSJeff Roberson 		sigpending = td->td_siglist;
20754093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
20764093529dSJeff Roberson 
2077df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
20784093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
20794093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2080df8bae1dSRodney W. Grimes 			return (0);
20814093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
20822a024a2bSSean Eric Fagan 
2083628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
20842a024a2bSSean Eric Fagan 
2085df8bae1dSRodney W. Grimes 		/*
2086df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2087df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2088df8bae1dSRodney W. Grimes 		 */
208990af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
20904093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
2091df8bae1dSRodney W. Grimes 			continue;
2092df8bae1dSRodney W. Grimes 		}
2093df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2094df8bae1dSRodney W. Grimes 			/*
2095d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2096df8bae1dSRodney W. Grimes 			 */
209790af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
20984cc9f52fSRobert Drehmel 			ptracestop(td, sig);
2099628d2653SJohn Baldwin 			PROC_LOCK(p);
210090af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2101df8bae1dSRodney W. Grimes 
2102df8bae1dSRodney W. Grimes 			/*
2103df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
2104df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
2105df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
2106df8bae1dSRodney W. Grimes 			 */
21074093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
21082c42a146SMarcel Moolenaar 			sig = p->p_xstat;
21092c42a146SMarcel Moolenaar 			if (sig == 0)
2110df8bae1dSRodney W. Grimes 				continue;
2111df8bae1dSRodney W. Grimes 
2112df8bae1dSRodney W. Grimes 			/*
21138d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
21148d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
21158d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
21168d542cb5SDavid E. O'Brien 			 */
21178d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
21188d542cb5SDavid E. O'Brien 				continue;
21198d542cb5SDavid E. O'Brien 
21208d542cb5SDavid E. O'Brien 			/*
21214093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
21221d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
2123df8bae1dSRodney W. Grimes 			 */
21244093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
21254093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
2126df8bae1dSRodney W. Grimes 				continue;
21274093529dSJeff Roberson 			signotify(td);
2128df8bae1dSRodney W. Grimes 		}
2129df8bae1dSRodney W. Grimes 
21308d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
21318d542cb5SDavid E. O'Brien 
2132df8bae1dSRodney W. Grimes 		/*
2133df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2134df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2135df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2136df8bae1dSRodney W. Grimes 		 */
2137d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2138df8bae1dSRodney W. Grimes 
2139d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2140df8bae1dSRodney W. Grimes 			/*
2141df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2142df8bae1dSRodney W. Grimes 			 */
2143df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2144df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2145df8bae1dSRodney W. Grimes 				/*
2146df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2147df8bae1dSRodney W. Grimes 				 * in init? XXX
2148df8bae1dSRodney W. Grimes 				 */
2149d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
21502c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2151df8bae1dSRodney W. Grimes #endif
2152df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2153df8bae1dSRodney W. Grimes 			}
2154df8bae1dSRodney W. Grimes 			/*
2155df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2156df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2157df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2158df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2159df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2160df8bae1dSRodney W. Grimes 			 */
2161df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2162df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2163df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2164df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2165df8bae1dSRodney W. Grimes 					break;	/* == ignore */
216690af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
216790af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
216890af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2169e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
21702c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2171721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2172062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2173062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2174062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2175062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2176062cf543SDavid Xu 						thread_suspend_one(td0);
2177062cf543SDavid Xu 					} else if (td != td0) {
2178062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2179062cf543SDavid Xu 					}
2180062cf543SDavid Xu 				}
2181e574e444SDavid Xu 				thread_stopped(p);
218271fad9fdSJulian Elischer 				thread_suspend_one(td);
2183721e5910SJulian Elischer 				PROC_UNLOCK(p);
2184721e5910SJulian Elischer 				DROP_GIANT();
2185721e5910SJulian Elischer 				p->p_stats->p_ru.ru_nivcsw++;
2186721e5910SJulian Elischer 				mi_switch();
2187721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2188721e5910SJulian Elischer 				PICKUP_GIANT();
2189721e5910SJulian Elischer 				PROC_LOCK(p);
219090af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2191df8bae1dSRodney W. Grimes 				break;
219221b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2193df8bae1dSRodney W. Grimes 				/*
2194df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2195df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2196df8bae1dSRodney W. Grimes 				 */
2197df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2198df8bae1dSRodney W. Grimes 			} else
21992c42a146SMarcel Moolenaar 				return (sig);
2200df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2201df8bae1dSRodney W. Grimes 
2202d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2203df8bae1dSRodney W. Grimes 			/*
2204df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2205df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2206df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2207df8bae1dSRodney W. Grimes 			 */
2208df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2209df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2210df8bae1dSRodney W. Grimes 				printf("issignal\n");
2211df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2212df8bae1dSRodney W. Grimes 
2213df8bae1dSRodney W. Grimes 		default:
2214df8bae1dSRodney W. Grimes 			/*
2215df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2216df8bae1dSRodney W. Grimes 			 * postsig() process it.
2217df8bae1dSRodney W. Grimes 			 */
22182c42a146SMarcel Moolenaar 			return (sig);
2219df8bae1dSRodney W. Grimes 		}
22204093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2221df8bae1dSRodney W. Grimes 	}
2222df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2223df8bae1dSRodney W. Grimes }
2224df8bae1dSRodney W. Grimes 
2225df8bae1dSRodney W. Grimes /*
2226df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2227df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
22285b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
22295b3047d5SJohn Baldwin  * lock held.
2230df8bae1dSRodney W. Grimes  */
22315b3047d5SJohn Baldwin static void
2232e574e444SDavid Xu stop(struct proc *p)
2233df8bae1dSRodney W. Grimes {
2234df8bae1dSRodney W. Grimes 
2235628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22361279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2237df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
223801609114SAlfred Perlstein 	wakeup(p->p_pptr);
2239df8bae1dSRodney W. Grimes }
2240df8bae1dSRodney W. Grimes 
224190af4afaSJohn Baldwin /*
224290af4afaSJohn Baldwin  * MPSAFE
224390af4afaSJohn Baldwin  */
2244e574e444SDavid Xu void
2245e574e444SDavid Xu thread_stopped(struct proc *p)
2246e574e444SDavid Xu {
2247e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
224890af4afaSJohn Baldwin 	struct sigacts *ps;
2249e574e444SDavid Xu 	int n;
2250e574e444SDavid Xu 
2251e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2252e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2253e574e444SDavid Xu 	n = p->p_suspcount;
2254e574e444SDavid Xu 	if (p == p1)
2255e574e444SDavid Xu 		n++;
2256e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2257e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2258e574e444SDavid Xu 		stop(p);
2259e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
226090af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
226190af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
226290af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
226390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2264e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
226590af4afaSJohn Baldwin 		} else
226690af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2267e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2268e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2269e574e444SDavid Xu 	}
2270e574e444SDavid Xu }
2271e574e444SDavid Xu 
2272df8bae1dSRodney W. Grimes /*
2273df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2274df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2275df8bae1dSRodney W. Grimes  */
2276df8bae1dSRodney W. Grimes void
22772c42a146SMarcel Moolenaar postsig(sig)
22782c42a146SMarcel Moolenaar 	register int sig;
2279df8bae1dSRodney W. Grimes {
2280b40ce416SJulian Elischer 	struct thread *td = curthread;
2281b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2282628d2653SJohn Baldwin 	struct sigacts *ps;
22832c42a146SMarcel Moolenaar 	sig_t action;
22842c42a146SMarcel Moolenaar 	sigset_t returnmask;
22852c42a146SMarcel Moolenaar 	int code;
2286df8bae1dSRodney W. Grimes 
22872c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
22885526d2d9SEivind Eklund 
22892ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2290628d2653SJohn Baldwin 	ps = p->p_sigacts;
229190af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
22924093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
22932c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2294df8bae1dSRodney W. Grimes #ifdef KTRACE
2295374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
22965e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
22974093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2298df8bae1dSRodney W. Grimes #endif
2299628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
23002a024a2bSSean Eric Fagan 
2301432b45deSDavid Xu 	if (!(td->td_flags & TDF_SA && td->td_mailbox) &&
2302432b45deSDavid Xu 	    action == SIG_DFL) {
2303df8bae1dSRodney W. Grimes 		/*
2304df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2305df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2306df8bae1dSRodney W. Grimes 		 */
230790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2308b40ce416SJulian Elischer 		sigexit(td, sig);
2309df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2310df8bae1dSRodney W. Grimes 	} else {
2311432b45deSDavid Xu 		if (td->td_flags & TDF_SA && td->td_mailbox) {
2312432b45deSDavid Xu 			if (sig == SIGKILL) {
2313432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2314432b45deSDavid Xu 				sigexit(td, sig);
2315432b45deSDavid Xu 			}
2316432b45deSDavid Xu 		}
2317432b45deSDavid Xu 
2318df8bae1dSRodney W. Grimes 		/*
2319df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2320df8bae1dSRodney W. Grimes 		 */
23214093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
23225526d2d9SEivind Eklund 		    ("postsig action"));
2323df8bae1dSRodney W. Grimes 		/*
2324df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2325645682fdSLuoqi Chen 		 * occurrences of this signal.
2326df8bae1dSRodney W. Grimes 		 *
2327645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2328df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2329645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2330df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2331df8bae1dSRodney W. Grimes 		 */
23325e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
23334093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
23345e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2335df8bae1dSRodney W. Grimes 		} else
23364093529dSJeff Roberson 			returnmask = td->td_sigmask;
23372c42a146SMarcel Moolenaar 
23384093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
23392c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
23404093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
23412c42a146SMarcel Moolenaar 
23422c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2343289ccde0SPeter Wemm 			/*
23448f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2345289ccde0SPeter Wemm 			 */
234690af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
23472c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
23482c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
234990af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
23502c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2351dedc04feSPeter Wemm 		}
2352df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
23532c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2354df8bae1dSRodney W. Grimes 			code = 0;
2355df8bae1dSRodney W. Grimes 		} else {
23566626c604SJulian Elischer 			code = p->p_code;
23576626c604SJulian Elischer 			p->p_code = 0;
23586626c604SJulian Elischer 			p->p_sig = 0;
2359df8bae1dSRodney W. Grimes 		}
2360432b45deSDavid Xu 		if (td->td_flags & TDF_SA && td->td_mailbox)
236158a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
236258a3c273SJeff Roberson 		else
236358a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
236458a3c273SJeff Roberson 			    &returnmask, code);
2365df8bae1dSRodney W. Grimes 	}
2366df8bae1dSRodney W. Grimes }
2367df8bae1dSRodney W. Grimes 
2368df8bae1dSRodney W. Grimes /*
2369df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2370df8bae1dSRodney W. Grimes  */
237126f9a767SRodney W. Grimes void
2372df8bae1dSRodney W. Grimes killproc(p, why)
2373df8bae1dSRodney W. Grimes 	struct proc *p;
2374df8bae1dSRodney W. Grimes 	char *why;
2375df8bae1dSRodney W. Grimes {
23769081e5e8SJohn Baldwin 
23779081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
23780384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
23790384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2380729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2381b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2382df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2383df8bae1dSRodney W. Grimes }
2384df8bae1dSRodney W. Grimes 
2385df8bae1dSRodney W. Grimes /*
2386df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2387df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2388df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2389df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2390df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2391df8bae1dSRodney W. Grimes  * does not return.
239290af4afaSJohn Baldwin  *
239390af4afaSJohn Baldwin  * MPSAFE
2394df8bae1dSRodney W. Grimes  */
239526f9a767SRodney W. Grimes void
2396b40ce416SJulian Elischer sigexit(td, sig)
2397b40ce416SJulian Elischer 	struct thread *td;
23982c42a146SMarcel Moolenaar 	int sig;
2399df8bae1dSRodney W. Grimes {
2400b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2401df8bae1dSRodney W. Grimes 
2402628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2403df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
24042c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
24052c42a146SMarcel Moolenaar 		p->p_sig = sig;
2406c364e17eSAndrey A. Chernov 		/*
2407c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2408c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2409c364e17eSAndrey A. Chernov 		 * these messages.)
2410c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
2411c364e17eSAndrey A. Chernov 		 */
2412628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2413c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
2414c31146a1SJohn Baldwin 			mtx_lock(&Giant);
2415b40ce416SJulian Elischer 		if (coredump(td) == 0)
24162c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
241757308494SJoerg Wunsch 		if (kern_logsigexit)
241857308494SJoerg Wunsch 			log(LOG_INFO,
241957308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
24203d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
24219c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
24222c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
24232c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
2424c31146a1SJohn Baldwin 	} else {
2425628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2426628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
2427628d2653SJohn Baldwin 			mtx_lock(&Giant);
2428c31146a1SJohn Baldwin 	}
2429b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2430df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2431df8bae1dSRodney W. Grimes }
2432df8bae1dSRodney W. Grimes 
2433c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2434c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2435c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2436c5edb423SSean Eric Fagan 
2437c5edb423SSean Eric Fagan /*
2438c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2439c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2440c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2441c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2442c5edb423SSean Eric Fagan  *	%P	process id (pid)
2443c5edb423SSean Eric Fagan  *	%U	user id (uid)
2444c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2445c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2446c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2447c5edb423SSean Eric Fagan  */
2448c5edb423SSean Eric Fagan 
2449fca666a1SJulian Elischer static char *
2450c5edb423SSean Eric Fagan expand_name(name, uid, pid)
24518b43b535SAlfred Perlstein 	const char *name;
24528b43b535SAlfred Perlstein 	uid_t uid;
24538b43b535SAlfred Perlstein 	pid_t pid;
24548b43b535SAlfred Perlstein {
24558b43b535SAlfred Perlstein 	const char *format, *appendstr;
2456c5edb423SSean Eric Fagan 	char *temp;
2457c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
24588b43b535SAlfred Perlstein 	size_t i, l, n;
2459c5edb423SSean Eric Fagan 
24608b43b535SAlfred Perlstein 	format = corefilename;
24618b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
24620bfe2990SEivind Eklund 	if (temp == NULL)
24638b43b535SAlfred Perlstein 		return (NULL);
2464ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2465c5edb423SSean Eric Fagan 		switch (format[i]) {
2466c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2467c5edb423SSean Eric Fagan 			i++;
2468c5edb423SSean Eric Fagan 			switch (format[i]) {
2469c5edb423SSean Eric Fagan 			case '%':
24708b43b535SAlfred Perlstein 				appendstr = "%";
2471c5edb423SSean Eric Fagan 				break;
2472c5edb423SSean Eric Fagan 			case 'N':	/* process name */
24738b43b535SAlfred Perlstein 				appendstr = name;
2474c5edb423SSean Eric Fagan 				break;
2475c5edb423SSean Eric Fagan 			case 'P':	/* process id */
24768b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
24778b43b535SAlfred Perlstein 				appendstr = buf;
2478c5edb423SSean Eric Fagan 				break;
2479c5edb423SSean Eric Fagan 			case 'U':	/* user id */
24808b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
24818b43b535SAlfred Perlstein 				appendstr = buf;
2482c5edb423SSean Eric Fagan 				break;
2483c5edb423SSean Eric Fagan 			default:
24848b43b535SAlfred Perlstein 				appendstr = "";
24858b43b535SAlfred Perlstein 			  	log(LOG_ERR,
24868b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
24878b43b535SAlfred Perlstein 				    format[i], format);
2488c5edb423SSean Eric Fagan 			}
24898b43b535SAlfred Perlstein 			l = strlen(appendstr);
24908b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
24918b43b535SAlfred Perlstein 				goto toolong;
24928b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
24938b43b535SAlfred Perlstein 			n += l;
2494c5edb423SSean Eric Fagan 			break;
2495c5edb423SSean Eric Fagan 		default:
2496c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2497c5edb423SSean Eric Fagan 		}
2498c5edb423SSean Eric Fagan 	}
24998b43b535SAlfred Perlstein 	if (format[i] != '\0')
25008b43b535SAlfred Perlstein 		goto toolong;
25018b43b535SAlfred Perlstein 	return (temp);
25028b43b535SAlfred Perlstein toolong:
25038b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
25048b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
25058b43b535SAlfred Perlstein 	free(temp, M_TEMP);
25068b43b535SAlfred Perlstein 	return (NULL);
2507c5edb423SSean Eric Fagan }
2508c5edb423SSean Eric Fagan 
2509df8bae1dSRodney W. Grimes /*
2510fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2511fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2512fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2513fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2514fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2515fca666a1SJulian Elischer  */
2516fca666a1SJulian Elischer 
2517fca666a1SJulian Elischer static int
2518b40ce416SJulian Elischer coredump(struct thread *td)
2519fca666a1SJulian Elischer {
2520b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2521fca666a1SJulian Elischer 	register struct vnode *vp;
25229c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
252306ae1e91SMatthew Dillon 	struct flock lf;
2524fca666a1SJulian Elischer 	struct nameidata nd;
2525fca666a1SJulian Elischer 	struct vattr vattr;
2526c447f5b2SRobert Watson 	int error, error1, flags, locked;
2527f2a2857bSKirk McKusick 	struct mount *mp;
2528fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2529fca666a1SJulian Elischer 	off_t limit;
2530fca666a1SJulian Elischer 
2531628d2653SJohn Baldwin 	PROC_LOCK(p);
2532628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2533fca666a1SJulian Elischer 
2534628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2535628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2536fca666a1SJulian Elischer 		return (EFAULT);
2537628d2653SJohn Baldwin 	}
2538fca666a1SJulian Elischer 
2539fca666a1SJulian Elischer 	/*
254035a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
254135a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
254235a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
254335a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
254435a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
254535a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2546fca666a1SJulian Elischer 	 */
254735a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2548628d2653SJohn Baldwin 	if (limit == 0) {
2549628d2653SJohn Baldwin 		PROC_UNLOCK(p);
255035a2598fSSean Eric Fagan 		return 0;
2551628d2653SJohn Baldwin 	}
2552628d2653SJohn Baldwin 	PROC_UNLOCK(p);
255335a2598fSSean Eric Fagan 
2554f2a2857bSKirk McKusick restart:
25559c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2556ccdbd10cSPeter Pentchev 	if (name == NULL)
2557ccdbd10cSPeter Pentchev 		return (EINVAL);
2558b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2559e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
25607c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2561fca666a1SJulian Elischer 	free(name, M_TEMP);
2562fca666a1SJulian Elischer 	if (error)
2563fca666a1SJulian Elischer 		return (error);
2564762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2565fca666a1SJulian Elischer 	vp = nd.ni_vp;
256606ae1e91SMatthew Dillon 
2567832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2568832dafadSDon Lewis 	if (vp->v_type != VREG ||
2569832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2570832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2571832dafadSDon Lewis 		error = EFAULT;
2572832dafadSDon Lewis 		goto out2;
2573832dafadSDon Lewis 	}
2574832dafadSDon Lewis 
2575b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
257606ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
257706ae1e91SMatthew Dillon 	lf.l_start = 0;
257806ae1e91SMatthew Dillon 	lf.l_len = 0;
257906ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
2580c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
258106ae1e91SMatthew Dillon 
258206ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
258306ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
258436bbf86bSRobert Watson 		if (locked)
258506ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2586b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2587f2a2857bSKirk McKusick 			return (error);
2588f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2589f2a2857bSKirk McKusick 			return (error);
2590f2a2857bSKirk McKusick 		goto restart;
2591f2a2857bSKirk McKusick 	}
2592fca666a1SJulian Elischer 
2593fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2594fca666a1SJulian Elischer 	vattr.va_size = 0;
259588b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2596b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2597b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
259888b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2599628d2653SJohn Baldwin 	PROC_LOCK(p);
2600fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2601628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2602fca666a1SJulian Elischer 
2603fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2604b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2605fca666a1SJulian Elischer 	  ENOSYS;
2606fca666a1SJulian Elischer 
2607c447f5b2SRobert Watson 	if (locked) {
260806ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
260906ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2610c447f5b2SRobert Watson 	}
2611f2a2857bSKirk McKusick 	vn_finished_write(mp);
261206ae1e91SMatthew Dillon out2:
2613b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2614fca666a1SJulian Elischer 	if (error == 0)
2615fca666a1SJulian Elischer 		error = error1;
2616fca666a1SJulian Elischer 	return (error);
2617fca666a1SJulian Elischer }
2618fca666a1SJulian Elischer 
2619fca666a1SJulian Elischer /*
2620df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2621df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2622df8bae1dSRodney W. Grimes  */
2623d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2624df8bae1dSRodney W. Grimes struct nosys_args {
2625df8bae1dSRodney W. Grimes 	int	dummy;
2626df8bae1dSRodney W. Grimes };
2627d2d3e875SBruce Evans #endif
2628fb99ab88SMatthew Dillon /*
2629fb99ab88SMatthew Dillon  * MPSAFE
2630fb99ab88SMatthew Dillon  */
2631df8bae1dSRodney W. Grimes /* ARGSUSED */
263226f9a767SRodney W. Grimes int
2633b40ce416SJulian Elischer nosys(td, args)
2634b40ce416SJulian Elischer 	struct thread *td;
2635df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2636df8bae1dSRodney W. Grimes {
2637b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2638b40ce416SJulian Elischer 
2639628d2653SJohn Baldwin 	PROC_LOCK(p);
2640df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2641628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2642f5216b9aSBruce Evans 	return (ENOSYS);
2643df8bae1dSRodney W. Grimes }
2644831d27a9SDon Lewis 
2645831d27a9SDon Lewis /*
264648f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2647831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2648831d27a9SDon Lewis  */
2649831d27a9SDon Lewis void
2650f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2651f1320723SAlfred Perlstein 	struct sigio **sigiop;
26522c42a146SMarcel Moolenaar 	int sig, checkctty;
2653831d27a9SDon Lewis {
2654f1320723SAlfred Perlstein 	struct sigio *sigio;
2655831d27a9SDon Lewis 
2656f1320723SAlfred Perlstein 	SIGIO_LOCK();
2657f1320723SAlfred Perlstein 	sigio = *sigiop;
2658f1320723SAlfred Perlstein 	if (sigio == NULL) {
2659f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2660f1320723SAlfred Perlstein 		return;
2661f1320723SAlfred Perlstein 	}
2662831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2663628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
26642b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
26652c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2666628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2667831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2668831d27a9SDon Lewis 		struct proc *p;
2669831d27a9SDon Lewis 
2670f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2671628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2672628d2653SJohn Baldwin 			PROC_LOCK(p);
26732b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2674831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
26752c42a146SMarcel Moolenaar 				psignal(p, sig);
2676628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2677628d2653SJohn Baldwin 		}
2678f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2679831d27a9SDon Lewis 	}
2680f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2681831d27a9SDon Lewis }
2682cb679c38SJonathan Lemon 
2683cb679c38SJonathan Lemon static int
2684cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2685cb679c38SJonathan Lemon {
2686cb679c38SJonathan Lemon 	struct proc *p = curproc;
2687cb679c38SJonathan Lemon 
2688cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2689cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2690cb679c38SJonathan Lemon 
2691628d2653SJohn Baldwin 	PROC_LOCK(p);
2692cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2693628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2694cb679c38SJonathan Lemon 
2695cb679c38SJonathan Lemon 	return (0);
2696cb679c38SJonathan Lemon }
2697cb679c38SJonathan Lemon 
2698cb679c38SJonathan Lemon static void
2699cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2700cb679c38SJonathan Lemon {
2701cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2702cb679c38SJonathan Lemon 
2703628d2653SJohn Baldwin 	PROC_LOCK(p);
2704e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2705628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2706cb679c38SJonathan Lemon }
2707cb679c38SJonathan Lemon 
2708cb679c38SJonathan Lemon /*
2709cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2710cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2711cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2712cb679c38SJonathan Lemon  * isn't worth the trouble.
2713cb679c38SJonathan Lemon  */
2714cb679c38SJonathan Lemon static int
2715cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2716cb679c38SJonathan Lemon {
2717cb679c38SJonathan Lemon 
2718cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2719cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2720cb679c38SJonathan Lemon 
2721cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2722cb679c38SJonathan Lemon 			kn->kn_data++;
2723cb679c38SJonathan Lemon 	}
2724cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2725cb679c38SJonathan Lemon }
272690af4afaSJohn Baldwin 
272790af4afaSJohn Baldwin struct sigacts *
272890af4afaSJohn Baldwin sigacts_alloc(void)
272990af4afaSJohn Baldwin {
273090af4afaSJohn Baldwin 	struct sigacts *ps;
273190af4afaSJohn Baldwin 
273290af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
273390af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
273490af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
273590af4afaSJohn Baldwin 	return (ps);
273690af4afaSJohn Baldwin }
273790af4afaSJohn Baldwin 
273890af4afaSJohn Baldwin void
273990af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
274090af4afaSJohn Baldwin {
274190af4afaSJohn Baldwin 
274290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
274390af4afaSJohn Baldwin 	ps->ps_refcnt--;
274490af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
274590af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
274690af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
274790af4afaSJohn Baldwin 	} else
274890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
274990af4afaSJohn Baldwin }
275090af4afaSJohn Baldwin 
275190af4afaSJohn Baldwin struct sigacts *
275290af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
275390af4afaSJohn Baldwin {
275490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
275590af4afaSJohn Baldwin 	ps->ps_refcnt++;
275690af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
275790af4afaSJohn Baldwin 	return (ps);
275890af4afaSJohn Baldwin }
275990af4afaSJohn Baldwin 
276090af4afaSJohn Baldwin void
276190af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
276290af4afaSJohn Baldwin {
276390af4afaSJohn Baldwin 
276490af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
276590af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
276690af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
276790af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
276890af4afaSJohn Baldwin }
276990af4afaSJohn Baldwin 
277090af4afaSJohn Baldwin int
277190af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
277290af4afaSJohn Baldwin {
277390af4afaSJohn Baldwin 	int shared;
277490af4afaSJohn Baldwin 
277590af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
277690af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
277790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
277890af4afaSJohn Baldwin 	return (shared);
277990af4afaSJohn Baldwin }
2780