xref: /freebsd/sys/kern/kern_sig.c (revision 5e26dcb560c86b80e764f2c20c24176dc8e301a6)
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
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
4636240ea5SDoug Rabson #include <sys/systm.h>
47df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
49df8bae1dSRodney W. Grimes #include <sys/acct.h>
50238510fcSJason Evans #include <sys/condvar.h>
51854dc8c2SJohn Baldwin #include <sys/event.h>
52854dc8c2SJohn Baldwin #include <sys/fcntl.h>
53854dc8c2SJohn Baldwin #include <sys/kernel.h>
540384fff8SJason Evans #include <sys/ktr.h>
55df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
56854dc8c2SJohn Baldwin #include <sys/lock.h>
57854dc8c2SJohn Baldwin #include <sys/malloc.h>
58854dc8c2SJohn Baldwin #include <sys/mutex.h>
59854dc8c2SJohn Baldwin #include <sys/namei.h>
60854dc8c2SJohn Baldwin #include <sys/proc.h>
61854dc8c2SJohn Baldwin #include <sys/pioctl.h>
62c31146a1SJohn Baldwin #include <sys/resourcevar.h>
636caa8a15SJohn Baldwin #include <sys/smp.h>
64df8bae1dSRodney W. Grimes #include <sys/stat.h>
651005a129SJohn Baldwin #include <sys/sx.h>
668f19eb88SIan Dowse #include <sys/syscallsubr.h>
67c87e2930SDavid Greenman #include <sys/sysctl.h>
68854dc8c2SJohn Baldwin #include <sys/sysent.h>
69854dc8c2SJohn Baldwin #include <sys/syslog.h>
70854dc8c2SJohn Baldwin #include <sys/sysproto.h>
7106ae1e91SMatthew Dillon #include <sys/unistd.h>
72854dc8c2SJohn Baldwin #include <sys/wait.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <machine/cpu.h>
75df8bae1dSRodney W. Grimes 
7623eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
7723eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
7823eeeff7SPeter Wemm #endif
7923eeeff7SPeter Wemm 
806f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
816f841fb7SMarcel Moolenaar 
824d77a549SAlfred Perlstein static int	coredump(struct thread *);
834d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
849c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
856711f10fSJohn Baldwin static int	issignal(struct thread *p);
864d77a549SAlfred Perlstein static int	sigprop(int sig);
874d77a549SAlfred Perlstein static void	stop(struct proc *);
884093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
89cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
90cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
91cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
924093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
93a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
94a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
95cb679c38SJonathan Lemon 
96cb679c38SJonathan Lemon struct filterops sig_filtops =
97cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
98cb679c38SJonathan Lemon 
9957308494SJoerg Wunsch static int	kern_logsigexit = 1;
1003d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1013d177f46SBill Fumerola     &kern_logsigexit, 0,
1023d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10357308494SJoerg Wunsch 
1042b87b6d4SRobert Watson /*
1052b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1062b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1072b87b6d4SRobert Watson  * in the right situations.
1082b87b6d4SRobert Watson  */
1092b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1102b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1112b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1122b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1142b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1152b87b6d4SRobert Watson 
11622d4b0fbSJohn Polstra int sugid_coredump;
1173d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1183d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
119c87e2930SDavid Greenman 
120e5a28db9SPaul Saab static int	do_coredump = 1;
121e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
122e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
123e5a28db9SPaul Saab 
1242c42a146SMarcel Moolenaar /*
1252c42a146SMarcel Moolenaar  * Signal properties and actions.
1262c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1272c42a146SMarcel Moolenaar  * according to the following properties:
1282c42a146SMarcel Moolenaar  */
1292c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1302c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1312c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1322c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1332c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1342c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1352c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
136da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
137df8bae1dSRodney W. Grimes 
1382c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
139da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
140da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
141da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
145da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1462c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
147da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1502c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
151da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
152da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
153da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
154da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
155da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
156da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
157da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
158da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
159da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
160da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
161da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1622c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1632c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
164da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
165da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
166da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
167da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
168da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
169da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1702c42a146SMarcel Moolenaar };
1712c42a146SMarcel Moolenaar 
172fbbeeb6cSBruce Evans /*
173fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
174fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
175fbbeeb6cSBruce Evans  * action, the process stops in issignal().
176e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
177fbbeeb6cSBruce Evans  *
17833510ef1SBruce Evans  * MP SAFE.
179fbbeeb6cSBruce Evans  */
180fbbeeb6cSBruce Evans int
181e602ba25SJulian Elischer cursig(struct thread *td)
182fbbeeb6cSBruce Evans {
183c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
18490af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
185179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1864093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
187fbbeeb6cSBruce Evans }
188fbbeeb6cSBruce Evans 
18979065dbaSBruce Evans /*
19079065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1914093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1924093529dSJeff Roberson  * unmasked in td_sigmask.
19379065dbaSBruce Evans  */
19479065dbaSBruce Evans void
1954093529dSJeff Roberson signotify(struct thread *td)
19679065dbaSBruce Evans {
1974093529dSJeff Roberson 	struct proc *p;
1984093529dSJeff Roberson 	sigset_t set;
1994093529dSJeff Roberson 
2004093529dSJeff Roberson 	p = td->td_proc;
20179065dbaSBruce Evans 
20279065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2034093529dSJeff Roberson 
2044093529dSJeff Roberson 	/*
2054093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2064093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2074093529dSJeff Roberson 	 */
2084093529dSJeff Roberson 	set = p->p_siglist;
2094093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2104093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2114093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2124093529dSJeff Roberson 
2138b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
21479065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2154093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
21679065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
21779065dbaSBruce Evans 	}
2188b94a061SJohn Baldwin }
2198b94a061SJohn Baldwin 
2208b94a061SJohn Baldwin int
2218b94a061SJohn Baldwin sigonstack(size_t sp)
2228b94a061SJohn Baldwin {
2238b94a061SJohn Baldwin 	struct proc *p = curthread->td_proc;
2248b94a061SJohn Baldwin 
2258b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2268b94a061SJohn Baldwin 	return ((p->p_flag & P_ALTSTACK) ?
2278b94a061SJohn Baldwin #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2288b94a061SJohn Baldwin 	    ((p->p_sigstk.ss_size == 0) ? (p->p_sigstk.ss_flags & SS_ONSTACK) :
2298b94a061SJohn Baldwin 		((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size))
2308b94a061SJohn Baldwin #else
2318b94a061SJohn Baldwin 	    ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size)
2328b94a061SJohn Baldwin #endif
2338b94a061SJohn Baldwin 	    : 0);
2348b94a061SJohn Baldwin }
23579065dbaSBruce Evans 
2366f841fb7SMarcel Moolenaar static __inline int
2376f841fb7SMarcel Moolenaar sigprop(int sig)
2382c42a146SMarcel Moolenaar {
2396f841fb7SMarcel Moolenaar 
2402c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2412c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2422c42a146SMarcel Moolenaar 	return (0);
243df8bae1dSRodney W. Grimes }
2442c42a146SMarcel Moolenaar 
2454093529dSJeff Roberson int
2466f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2472c42a146SMarcel Moolenaar {
2482c42a146SMarcel Moolenaar 	int i;
2492c42a146SMarcel Moolenaar 
2506f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2512c42a146SMarcel Moolenaar 		if (set->__bits[i])
2522c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
253df8bae1dSRodney W. Grimes 	return (0);
254df8bae1dSRodney W. Grimes }
255df8bae1dSRodney W. Grimes 
2562c42a146SMarcel Moolenaar /*
2578f19eb88SIan Dowse  * kern_sigaction
2582c42a146SMarcel Moolenaar  * sigaction
25923eeeff7SPeter Wemm  * freebsd4_sigaction
2602c42a146SMarcel Moolenaar  * osigaction
26125d6dc06SJohn Baldwin  *
26225d6dc06SJohn Baldwin  * MPSAFE
2632c42a146SMarcel Moolenaar  */
2648f19eb88SIan Dowse int
26523eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2668f19eb88SIan Dowse 	struct thread *td;
2672c42a146SMarcel Moolenaar 	register int sig;
2682c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
26923eeeff7SPeter Wemm 	int flags;
270df8bae1dSRodney W. Grimes {
27190af4afaSJohn Baldwin 	struct sigacts *ps;
2724093529dSJeff Roberson 	struct thread *td0;
2738f19eb88SIan Dowse 	struct proc *p = td->td_proc;
274df8bae1dSRodney W. Grimes 
2752899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2762c42a146SMarcel Moolenaar 		return (EINVAL);
2772c42a146SMarcel Moolenaar 
278628d2653SJohn Baldwin 	PROC_LOCK(p);
279628d2653SJohn Baldwin 	ps = p->p_sigacts;
28090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2812c42a146SMarcel Moolenaar 	if (oact) {
2822c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2832c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2842c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2852c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2862c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2872c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2882c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2892c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2902c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2912c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2922c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2932c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2942c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
29590af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
2962c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
29790af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
2982c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2992c42a146SMarcel Moolenaar 	}
3002c42a146SMarcel Moolenaar 	if (act) {
3012c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
302628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
30390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
304628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3052c42a146SMarcel Moolenaar 			return (EINVAL);
306628d2653SJohn Baldwin 		}
3072c42a146SMarcel Moolenaar 
308df8bae1dSRodney W. Grimes 		/*
309df8bae1dSRodney W. Grimes 		 * Change setting atomically.
310df8bae1dSRodney W. Grimes 		 */
3112c42a146SMarcel Moolenaar 
3122c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3132c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3142c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
315aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
316aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
31780f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
31880f42b55SIan Dowse 		} else {
31980f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3202c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3212c42a146SMarcel Moolenaar 		}
3222c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3232c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
324df8bae1dSRodney W. Grimes 		else
3252c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3262c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3272c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
328df8bae1dSRodney W. Grimes 		else
3292c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3302c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3312c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3321e41c1b5SSteven Wallace 		else
3332c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3342c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3352c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
336289ccde0SPeter Wemm 		else
3372c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
338df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3392c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3402c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
341df8bae1dSRodney W. Grimes 		else
3428c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
343df8bae1dSRodney W. Grimes #endif
3442c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3452c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
34690af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3476626c604SJulian Elischer 			else
34890af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
349ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
350245f17d4SJoerg Wunsch 				/*
3512c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3522c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3532c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3542c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
355245f17d4SJoerg Wunsch 				 */
356245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
35790af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3586626c604SJulian Elischer 				else
35990af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
360245f17d4SJoerg Wunsch 			} else
36190af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
362ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
36390af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
364ba1551caSIan Dowse 			else
36590af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
366df8bae1dSRodney W. Grimes 		}
367df8bae1dSRodney W. Grimes 		/*
36890af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3692c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
37090af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3712c42a146SMarcel Moolenaar 		 * have to restart the process.
372df8bae1dSRodney W. Grimes 		 */
3732c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3742c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3752c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3762c42a146SMarcel Moolenaar 			/* never to be seen again */
3771d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
3784093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3794093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
3802c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3812c42a146SMarcel Moolenaar 				/* easier in psignal */
38290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
38390af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
384645682fdSLuoqi Chen 		} else {
38590af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
3862c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
38790af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
3882c42a146SMarcel Moolenaar 			else
38990af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
3902c42a146SMarcel Moolenaar 		}
39123eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
39223eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
39323eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
39423eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
39523eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
39623eeeff7SPeter Wemm 		else
39723eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
39823eeeff7SPeter Wemm #endif
399e8ebc08fSPeter Wemm #ifdef COMPAT_43
400645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
40123eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
40223eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
403645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
404645682fdSLuoqi Chen 		else
405645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
406e8ebc08fSPeter Wemm #endif
407df8bae1dSRodney W. Grimes 	}
40890af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
409628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4102c42a146SMarcel Moolenaar 	return (0);
4112c42a146SMarcel Moolenaar }
4122c42a146SMarcel Moolenaar 
4132c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4142c42a146SMarcel Moolenaar struct sigaction_args {
4152c42a146SMarcel Moolenaar 	int	sig;
4162c42a146SMarcel Moolenaar 	struct	sigaction *act;
4172c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4182c42a146SMarcel Moolenaar };
4192c42a146SMarcel Moolenaar #endif
420fb99ab88SMatthew Dillon /*
421fb99ab88SMatthew Dillon  * MPSAFE
422fb99ab88SMatthew Dillon  */
4232c42a146SMarcel Moolenaar int
424b40ce416SJulian Elischer sigaction(td, uap)
425b40ce416SJulian Elischer 	struct thread *td;
4262c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4272c42a146SMarcel Moolenaar {
4282c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4292c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4302c42a146SMarcel Moolenaar 	int error;
4312c42a146SMarcel Moolenaar 
4326f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4336f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4342c42a146SMarcel Moolenaar 	if (actp) {
4356f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4362c42a146SMarcel Moolenaar 		if (error)
43744443757STim J. Robbins 			return (error);
4382c42a146SMarcel Moolenaar 	}
4398f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
44025d6dc06SJohn Baldwin 	if (oactp && !error)
4416f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4422c42a146SMarcel Moolenaar 	return (error);
4432c42a146SMarcel Moolenaar }
4442c42a146SMarcel Moolenaar 
44523eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
44623eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
44723eeeff7SPeter Wemm struct freebsd4_sigaction_args {
44823eeeff7SPeter Wemm 	int	sig;
44923eeeff7SPeter Wemm 	struct	sigaction *act;
45023eeeff7SPeter Wemm 	struct	sigaction *oact;
45123eeeff7SPeter Wemm };
45223eeeff7SPeter Wemm #endif
45323eeeff7SPeter Wemm /*
45423eeeff7SPeter Wemm  * MPSAFE
45523eeeff7SPeter Wemm  */
45623eeeff7SPeter Wemm int
45723eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
45823eeeff7SPeter Wemm 	struct thread *td;
45923eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
46023eeeff7SPeter Wemm {
46123eeeff7SPeter Wemm 	struct sigaction act, oact;
46223eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
46323eeeff7SPeter Wemm 	int error;
46423eeeff7SPeter Wemm 
46523eeeff7SPeter Wemm 
46623eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
46723eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
46823eeeff7SPeter Wemm 	if (actp) {
46923eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
47023eeeff7SPeter Wemm 		if (error)
47144443757STim J. Robbins 			return (error);
47223eeeff7SPeter Wemm 	}
47323eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
474a14e1189SJohn Baldwin 	if (oactp && !error)
47523eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
47623eeeff7SPeter Wemm 	return (error);
47723eeeff7SPeter Wemm }
47823eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
47923eeeff7SPeter Wemm 
48031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4812c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4822c42a146SMarcel Moolenaar struct osigaction_args {
4832c42a146SMarcel Moolenaar 	int	signum;
4842c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4852c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4862c42a146SMarcel Moolenaar };
4872c42a146SMarcel Moolenaar #endif
488fb99ab88SMatthew Dillon /*
489fb99ab88SMatthew Dillon  * MPSAFE
490fb99ab88SMatthew Dillon  */
4912c42a146SMarcel Moolenaar int
492b40ce416SJulian Elischer osigaction(td, uap)
493b40ce416SJulian Elischer 	struct thread *td;
4942c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4952c42a146SMarcel Moolenaar {
4962c42a146SMarcel Moolenaar 	struct osigaction sa;
4972c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4982c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4992c42a146SMarcel Moolenaar 	int error;
5002c42a146SMarcel Moolenaar 
5016f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5026f841fb7SMarcel Moolenaar 		return (EINVAL);
503fb99ab88SMatthew Dillon 
5046f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5056f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
506fb99ab88SMatthew Dillon 
5072c42a146SMarcel Moolenaar 	if (nsap) {
5086f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5092c42a146SMarcel Moolenaar 		if (error)
51044443757STim J. Robbins 			return (error);
5112c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5122c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5132c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5142c42a146SMarcel Moolenaar 	}
51523eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5162c42a146SMarcel Moolenaar 	if (osap && !error) {
5172c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5182c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5192c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5206f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5212c42a146SMarcel Moolenaar 	}
5222c42a146SMarcel Moolenaar 	return (error);
5232c42a146SMarcel Moolenaar }
52423eeeff7SPeter Wemm 
52523eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
52623eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
52723eeeff7SPeter Wemm int
52823eeeff7SPeter Wemm osigreturn(td, uap)
52923eeeff7SPeter Wemm 	struct thread *td;
53023eeeff7SPeter Wemm 	struct osigreturn_args *uap;
53123eeeff7SPeter Wemm {
53223eeeff7SPeter Wemm 
53323eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
53423eeeff7SPeter Wemm }
53523eeeff7SPeter Wemm #endif
53631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
537df8bae1dSRodney W. Grimes 
538df8bae1dSRodney W. Grimes /*
539df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
540df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
541df8bae1dSRodney W. Grimes  */
542df8bae1dSRodney W. Grimes void
543df8bae1dSRodney W. Grimes siginit(p)
544df8bae1dSRodney W. Grimes 	struct proc *p;
545df8bae1dSRodney W. Grimes {
546df8bae1dSRodney W. Grimes 	register int i;
54790af4afaSJohn Baldwin 	struct sigacts *ps;
548df8bae1dSRodney W. Grimes 
549628d2653SJohn Baldwin 	PROC_LOCK(p);
55090af4afaSJohn Baldwin 	ps = p->p_sigacts;
55190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5522c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5532c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
55490af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
55590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
556628d2653SJohn Baldwin 	PROC_UNLOCK(p);
557df8bae1dSRodney W. Grimes }
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes /*
560df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
561df8bae1dSRodney W. Grimes  */
562df8bae1dSRodney W. Grimes void
563df8bae1dSRodney W. Grimes execsigs(p)
564df8bae1dSRodney W. Grimes 	register struct proc *p;
565df8bae1dSRodney W. Grimes {
566628d2653SJohn Baldwin 	register struct sigacts *ps;
5672c42a146SMarcel Moolenaar 	register int sig;
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 	/*
570df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5714093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
572df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
573df8bae1dSRodney W. Grimes 	 */
5749b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
575628d2653SJohn Baldwin 	ps = p->p_sigacts;
57690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
57790af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
57890af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
57990af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
5802c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5812c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
58290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
5831d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
5844093529dSJeff Roberson 			/*
5854093529dSJeff Roberson 			 * There is only one thread at this point.
5864093529dSJeff Roberson 			 */
5874093529dSJeff Roberson 			SIGDELSET(FIRST_THREAD_IN_PROC(p)->td_siglist, sig);
588df8bae1dSRodney W. Grimes 		}
5892c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
590df8bae1dSRodney W. Grimes 	}
591df8bae1dSRodney W. Grimes 	/*
5924093529dSJeff Roberson 	 * Clear out the td's sigmask.  Normal processes use the proc sigmask.
5934093529dSJeff Roberson 	 */
5944093529dSJeff Roberson 	SIGEMPTYSET(FIRST_THREAD_IN_PROC(p)->td_sigmask);
5954093529dSJeff Roberson 	/*
596df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
597df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
598df8bae1dSRodney W. Grimes 	 */
599645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
600645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
601645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
6023b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
60380e907a1SPeter Wemm 	/*
60480e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
60580e907a1SPeter Wemm 	 */
60690af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
607c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
608c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
60990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
610df8bae1dSRodney W. Grimes }
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes /*
613e77daab1SJohn Baldwin  * kern_sigprocmask()
6147c8fdcbdSMatthew Dillon  *
615628d2653SJohn Baldwin  *	Manipulate signal mask.
616df8bae1dSRodney W. Grimes  */
617e77daab1SJohn Baldwin int
618e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6194093529dSJeff Roberson 	struct thread *td;
6202c42a146SMarcel Moolenaar 	int how;
6212c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
622645682fdSLuoqi Chen 	int old;
6232c42a146SMarcel Moolenaar {
6242c42a146SMarcel Moolenaar 	int error;
6252c42a146SMarcel Moolenaar 
6264093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6272c42a146SMarcel Moolenaar 	if (oset != NULL)
6284093529dSJeff Roberson 		*oset = td->td_sigmask;
6292c42a146SMarcel Moolenaar 
6302c42a146SMarcel Moolenaar 	error = 0;
6312c42a146SMarcel Moolenaar 	if (set != NULL) {
6322c42a146SMarcel Moolenaar 		switch (how) {
6332c42a146SMarcel Moolenaar 		case SIG_BLOCK:
634645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6354093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6362c42a146SMarcel Moolenaar 			break;
6372c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6384093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6394093529dSJeff Roberson 			signotify(td);
6402c42a146SMarcel Moolenaar 			break;
6412c42a146SMarcel Moolenaar 		case SIG_SETMASK:
642645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
643645682fdSLuoqi Chen 			if (old)
6444093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
645645682fdSLuoqi Chen 			else
6464093529dSJeff Roberson 				td->td_sigmask = *set;
6474093529dSJeff Roberson 			signotify(td);
6482c42a146SMarcel Moolenaar 			break;
6492c42a146SMarcel Moolenaar 		default:
6502c42a146SMarcel Moolenaar 			error = EINVAL;
6512c42a146SMarcel Moolenaar 			break;
6522c42a146SMarcel Moolenaar 		}
6532c42a146SMarcel Moolenaar 	}
6544093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6552c42a146SMarcel Moolenaar 	return (error);
6562c42a146SMarcel Moolenaar }
6572c42a146SMarcel Moolenaar 
6587c8fdcbdSMatthew Dillon /*
659e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6607c8fdcbdSMatthew Dillon  */
6617c8fdcbdSMatthew Dillon 
662d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
663df8bae1dSRodney W. Grimes struct sigprocmask_args {
664df8bae1dSRodney W. Grimes 	int	how;
6652c42a146SMarcel Moolenaar 	const sigset_t *set;
6662c42a146SMarcel Moolenaar 	sigset_t *oset;
667df8bae1dSRodney W. Grimes };
668d2d3e875SBruce Evans #endif
66926f9a767SRodney W. Grimes int
670b40ce416SJulian Elischer sigprocmask(td, uap)
671b40ce416SJulian Elischer 	register struct thread *td;
672df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
673df8bae1dSRodney W. Grimes {
6742c42a146SMarcel Moolenaar 	sigset_t set, oset;
6752c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6762c42a146SMarcel Moolenaar 	int error;
677df8bae1dSRodney W. Grimes 
6786f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6796f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6802c42a146SMarcel Moolenaar 	if (setp) {
6816f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6822c42a146SMarcel Moolenaar 		if (error)
6832c42a146SMarcel Moolenaar 			return (error);
684df8bae1dSRodney W. Grimes 	}
685e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
6862c42a146SMarcel Moolenaar 	if (osetp && !error) {
6876f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
6882c42a146SMarcel Moolenaar 	}
6892c42a146SMarcel Moolenaar 	return (error);
6902c42a146SMarcel Moolenaar }
6912c42a146SMarcel Moolenaar 
69231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6937c8fdcbdSMatthew Dillon /*
6947c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
6957c8fdcbdSMatthew Dillon  */
6962c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6972c42a146SMarcel Moolenaar struct osigprocmask_args {
6982c42a146SMarcel Moolenaar 	int	how;
6992c42a146SMarcel Moolenaar 	osigset_t mask;
7002c42a146SMarcel Moolenaar };
7012c42a146SMarcel Moolenaar #endif
7022c42a146SMarcel Moolenaar int
703b40ce416SJulian Elischer osigprocmask(td, uap)
704b40ce416SJulian Elischer 	register struct thread *td;
7052c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7062c42a146SMarcel Moolenaar {
7072c42a146SMarcel Moolenaar 	sigset_t set, oset;
7082c42a146SMarcel Moolenaar 	int error;
7092c42a146SMarcel Moolenaar 
7102c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
711e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
712b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
713df8bae1dSRodney W. Grimes 	return (error);
714df8bae1dSRodney W. Grimes }
71531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
716df8bae1dSRodney W. Grimes 
717d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
718df8bae1dSRodney W. Grimes struct sigpending_args {
7192c42a146SMarcel Moolenaar 	sigset_t	*set;
720df8bae1dSRodney W. Grimes };
721d2d3e875SBruce Evans #endif
722fb99ab88SMatthew Dillon /*
723fb99ab88SMatthew Dillon  * MPSAFE
724fb99ab88SMatthew Dillon  */
72526f9a767SRodney W. Grimes int
726a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
727a447cd8bSJeff Roberson {
728a447cd8bSJeff Roberson 	siginfo_t info;
729a447cd8bSJeff Roberson 	sigset_t set;
730a447cd8bSJeff Roberson 	int error;
731a447cd8bSJeff Roberson 
732a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
733a447cd8bSJeff Roberson 	if (error)
734a447cd8bSJeff Roberson 		return (error);
735a447cd8bSJeff Roberson 
736a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
737a447cd8bSJeff Roberson 	if (error)
738a447cd8bSJeff Roberson 		return (error);
739a447cd8bSJeff Roberson 
740a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
741a447cd8bSJeff Roberson 	/* Repost if we got an error. */
74218440c7fSJohn Baldwin 	if (error && info.si_signo) {
74318440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
744a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
74518440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
74618440c7fSJohn Baldwin 	}
747a447cd8bSJeff Roberson 	return (error);
748a447cd8bSJeff Roberson }
749a447cd8bSJeff Roberson /*
750a447cd8bSJeff Roberson  * MPSAFE
751a447cd8bSJeff Roberson  */
752a447cd8bSJeff Roberson int
753a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
754a447cd8bSJeff Roberson {
755a447cd8bSJeff Roberson 	struct timespec ts;
756a447cd8bSJeff Roberson 	struct timespec *timeout;
757a447cd8bSJeff Roberson 	sigset_t set;
758a447cd8bSJeff Roberson 	siginfo_t info;
759a447cd8bSJeff Roberson 	int error;
760a447cd8bSJeff Roberson 
761a447cd8bSJeff Roberson 	if (uap->timeout) {
762a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
763a447cd8bSJeff Roberson 		if (error)
764a447cd8bSJeff Roberson 			return (error);
765a447cd8bSJeff Roberson 
766a447cd8bSJeff Roberson 		timeout = &ts;
767a447cd8bSJeff Roberson 	} else
768a447cd8bSJeff Roberson 		timeout = NULL;
769a447cd8bSJeff Roberson 
770a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
771a447cd8bSJeff Roberson 	if (error)
772a447cd8bSJeff Roberson 		return (error);
773a447cd8bSJeff Roberson 
774a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
775a447cd8bSJeff Roberson 	if (error)
776a447cd8bSJeff Roberson 		return (error);
777a447cd8bSJeff Roberson 
778a447cd8bSJeff Roberson 	error = copyout(&info, uap->info, sizeof(info));
779a447cd8bSJeff Roberson 	/* Repost if we got an error. */
78018440c7fSJohn Baldwin 	if (error && info.si_signo) {
78118440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
782a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
78318440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
78418440c7fSJohn Baldwin 	}
785a447cd8bSJeff Roberson 	return (error);
786a447cd8bSJeff Roberson }
787a447cd8bSJeff Roberson 
788a447cd8bSJeff Roberson /*
789a447cd8bSJeff Roberson  * MPSAFE
790a447cd8bSJeff Roberson  */
791a447cd8bSJeff Roberson int
792a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
793a447cd8bSJeff Roberson {
794a447cd8bSJeff Roberson 	siginfo_t info;
795a447cd8bSJeff Roberson 	sigset_t set;
796a447cd8bSJeff Roberson 	int error;
797a447cd8bSJeff Roberson 
798a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
799a447cd8bSJeff Roberson 	if (error)
800a447cd8bSJeff Roberson 		return (error);
801a447cd8bSJeff Roberson 
802a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
803a447cd8bSJeff Roberson 	if (error)
804a447cd8bSJeff Roberson 		return (error);
805a447cd8bSJeff Roberson 
806a447cd8bSJeff Roberson 	error = copyout(&info, uap->info, sizeof(info));
807a447cd8bSJeff Roberson 	/* Repost if we got an error. */
80818440c7fSJohn Baldwin 	if (error && info.si_signo) {
80918440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
810a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
81118440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
81218440c7fSJohn Baldwin 	}
813a447cd8bSJeff Roberson 	return (error);
814a447cd8bSJeff Roberson }
815a447cd8bSJeff Roberson 
816a447cd8bSJeff Roberson static int
817a447cd8bSJeff Roberson kern_sigtimedwait(struct thread *td, sigset_t set, siginfo_t *info,
818a447cd8bSJeff Roberson     struct timespec *timeout)
819a447cd8bSJeff Roberson {
820a447cd8bSJeff Roberson 	register struct sigacts *ps;
821a447cd8bSJeff Roberson 	sigset_t oldmask;
822a447cd8bSJeff Roberson 	struct proc *p;
823a447cd8bSJeff Roberson 	int error;
824a447cd8bSJeff Roberson 	int sig;
825a447cd8bSJeff Roberson 	int hz;
826a447cd8bSJeff Roberson 
827a447cd8bSJeff Roberson 	p = td->td_proc;
828a447cd8bSJeff Roberson 	error = 0;
829a447cd8bSJeff Roberson 	sig = 0;
830a447cd8bSJeff Roberson 	SIG_CANTMASK(set);
831a447cd8bSJeff Roberson 
832a447cd8bSJeff Roberson 	PROC_LOCK(p);
833a447cd8bSJeff Roberson 	ps = p->p_sigacts;
834a447cd8bSJeff Roberson 	oldmask = td->td_sigmask;
835a447cd8bSJeff Roberson 	td->td_sigmask = set;
836a447cd8bSJeff Roberson 	signotify(td);
837a447cd8bSJeff Roberson 
83890af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
839a447cd8bSJeff Roberson 	sig = cursig(td);
840a447cd8bSJeff Roberson 	if (sig)
841a447cd8bSJeff Roberson 		goto out;
842a447cd8bSJeff Roberson 
843a447cd8bSJeff Roberson 	/*
844a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
845a447cd8bSJeff Roberson 	 * signals.
846a447cd8bSJeff Roberson 	 */
847a447cd8bSJeff Roberson 	if (timeout) {
848a447cd8bSJeff Roberson 		struct timeval tv;
849a447cd8bSJeff Roberson 
850a447cd8bSJeff Roberson 		if (timeout->tv_nsec > 1000000000) {
851a447cd8bSJeff Roberson 			error = EINVAL;
852a447cd8bSJeff Roberson 			goto out;
853a447cd8bSJeff Roberson 		}
854a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
855a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
856a447cd8bSJeff Roberson 	} else
857a447cd8bSJeff Roberson 		hz = 0;
858a447cd8bSJeff Roberson 
85990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
860a447cd8bSJeff Roberson 	error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", hz);
86190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
862a447cd8bSJeff Roberson 	if (error == EINTR)
863a447cd8bSJeff Roberson 		error = 0;
864a447cd8bSJeff Roberson 	else if (error)
865a447cd8bSJeff Roberson 		goto out;
866a447cd8bSJeff Roberson 
867a447cd8bSJeff Roberson 	sig = cursig(td);
868a447cd8bSJeff Roberson out:
869a447cd8bSJeff Roberson 	td->td_sigmask = oldmask;
870a447cd8bSJeff Roberson 	if (sig) {
871a447cd8bSJeff Roberson 		sig_t action;
872a447cd8bSJeff Roberson 
873a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
87490af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
875a447cd8bSJeff Roberson #ifdef KTRACE
876a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
8775e26dcb5SJohn Baldwin 			ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
878a447cd8bSJeff Roberson 			    &td->td_oldsigmask : &td->td_sigmask, 0);
879a447cd8bSJeff Roberson #endif
880a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
881a447cd8bSJeff Roberson 
882a447cd8bSJeff Roberson 		if (action == SIG_DFL)
883a447cd8bSJeff Roberson 			sigexit(td, sig);
884a447cd8bSJeff Roberson 			/* NOTREACHED */
885a447cd8bSJeff Roberson 
886a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
887a447cd8bSJeff Roberson 		info->si_signo = sig;
888a447cd8bSJeff Roberson 		info->si_code = 0;
88990af4afaSJohn Baldwin 	} else
89090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
891a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
892a447cd8bSJeff Roberson 	return (error);
893a447cd8bSJeff Roberson }
894a447cd8bSJeff Roberson 
895a447cd8bSJeff Roberson /*
896a447cd8bSJeff Roberson  * MPSAFE
897a447cd8bSJeff Roberson  */
898a447cd8bSJeff Roberson int
899b40ce416SJulian Elischer sigpending(td, uap)
900b40ce416SJulian Elischer 	struct thread *td;
901df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
902df8bae1dSRodney W. Grimes {
903b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
904628d2653SJohn Baldwin 	sigset_t siglist;
905df8bae1dSRodney W. Grimes 
906628d2653SJohn Baldwin 	PROC_LOCK(p);
9071d9c5696SJuli Mallett 	siglist = p->p_siglist;
9084093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
909628d2653SJohn Baldwin 	PROC_UNLOCK(p);
91048e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9112c42a146SMarcel Moolenaar }
9122c42a146SMarcel Moolenaar 
91331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9142c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9152c42a146SMarcel Moolenaar struct osigpending_args {
9162c42a146SMarcel Moolenaar 	int	dummy;
9172c42a146SMarcel Moolenaar };
9182c42a146SMarcel Moolenaar #endif
919fb99ab88SMatthew Dillon /*
920fb99ab88SMatthew Dillon  * MPSAFE
921fb99ab88SMatthew Dillon  */
9222c42a146SMarcel Moolenaar int
923b40ce416SJulian Elischer osigpending(td, uap)
924b40ce416SJulian Elischer 	struct thread *td;
9252c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9262c42a146SMarcel Moolenaar {
927b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9284093529dSJeff Roberson 	sigset_t siglist;
929b40ce416SJulian Elischer 
930628d2653SJohn Baldwin 	PROC_LOCK(p);
9314093529dSJeff Roberson 	siglist = p->p_siglist;
9324093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
933628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9349d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
935df8bae1dSRodney W. Grimes 	return (0);
936df8bae1dSRodney W. Grimes }
93731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
940df8bae1dSRodney W. Grimes /*
941df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
942df8bae1dSRodney W. Grimes  */
943d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
944df8bae1dSRodney W. Grimes struct osigvec_args {
945df8bae1dSRodney W. Grimes 	int	signum;
946df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
947df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
948df8bae1dSRodney W. Grimes };
949d2d3e875SBruce Evans #endif
950fb99ab88SMatthew Dillon /*
951fb99ab88SMatthew Dillon  * MPSAFE
952fb99ab88SMatthew Dillon  */
953df8bae1dSRodney W. Grimes /* ARGSUSED */
95426f9a767SRodney W. Grimes int
955b40ce416SJulian Elischer osigvec(td, uap)
956b40ce416SJulian Elischer 	struct thread *td;
957df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
958df8bae1dSRodney W. Grimes {
959df8bae1dSRodney W. Grimes 	struct sigvec vec;
9602c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
9612c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
9622c42a146SMarcel Moolenaar 	int error;
963df8bae1dSRodney W. Grimes 
9646f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
9656f841fb7SMarcel Moolenaar 		return (EINVAL);
9666f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
9676f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
9682c42a146SMarcel Moolenaar 	if (nsap) {
9696f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
9702c42a146SMarcel Moolenaar 		if (error)
971df8bae1dSRodney W. Grimes 			return (error);
9722c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
9732c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
9742c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
9752c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
976df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
9772c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
978df8bae1dSRodney W. Grimes #endif
979df8bae1dSRodney W. Grimes 	}
9805edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
9812c42a146SMarcel Moolenaar 	if (osap && !error) {
9822c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
9832c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
9842c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
9852c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
9862c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
9872c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
9882c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
9892c42a146SMarcel Moolenaar #endif
9906f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
9912c42a146SMarcel Moolenaar 	}
9922c42a146SMarcel Moolenaar 	return (error);
993df8bae1dSRodney W. Grimes }
994df8bae1dSRodney W. Grimes 
995d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
996df8bae1dSRodney W. Grimes struct osigblock_args {
997df8bae1dSRodney W. Grimes 	int	mask;
998df8bae1dSRodney W. Grimes };
999d2d3e875SBruce Evans #endif
1000fb99ab88SMatthew Dillon /*
1001fb99ab88SMatthew Dillon  * MPSAFE
1002fb99ab88SMatthew Dillon  */
100326f9a767SRodney W. Grimes int
1004b40ce416SJulian Elischer osigblock(td, uap)
1005b40ce416SJulian Elischer 	register struct thread *td;
1006df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1007df8bae1dSRodney W. Grimes {
1008b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10092c42a146SMarcel Moolenaar 	sigset_t set;
1010df8bae1dSRodney W. Grimes 
10112c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10122c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1013628d2653SJohn Baldwin 	PROC_LOCK(p);
10144093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10154093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1016628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1017df8bae1dSRodney W. Grimes 	return (0);
1018df8bae1dSRodney W. Grimes }
1019df8bae1dSRodney W. Grimes 
1020d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1021df8bae1dSRodney W. Grimes struct osigsetmask_args {
1022df8bae1dSRodney W. Grimes 	int	mask;
1023df8bae1dSRodney W. Grimes };
1024d2d3e875SBruce Evans #endif
1025fb99ab88SMatthew Dillon /*
1026fb99ab88SMatthew Dillon  * MPSAFE
1027fb99ab88SMatthew Dillon  */
102826f9a767SRodney W. Grimes int
1029b40ce416SJulian Elischer osigsetmask(td, uap)
1030b40ce416SJulian Elischer 	struct thread *td;
1031df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1032df8bae1dSRodney W. Grimes {
1033b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10342c42a146SMarcel Moolenaar 	sigset_t set;
1035df8bae1dSRodney W. Grimes 
10362c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10372c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1038628d2653SJohn Baldwin 	PROC_LOCK(p);
10394093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10404093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10414093529dSJeff Roberson 	signotify(td);
1042628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1043df8bae1dSRodney W. Grimes 	return (0);
1044df8bae1dSRodney W. Grimes }
1045df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes /*
1048df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
1049df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
1050df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
1051b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1052b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1053b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1054df8bae1dSRodney W. Grimes  */
1055d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1056df8bae1dSRodney W. Grimes struct sigsuspend_args {
10572c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1058df8bae1dSRodney W. Grimes };
1059d2d3e875SBruce Evans #endif
1060fb99ab88SMatthew Dillon /*
1061fb99ab88SMatthew Dillon  * MPSAFE
1062fb99ab88SMatthew Dillon  */
1063df8bae1dSRodney W. Grimes /* ARGSUSED */
106426f9a767SRodney W. Grimes int
1065b40ce416SJulian Elischer sigsuspend(td, uap)
1066b40ce416SJulian Elischer 	struct thread *td;
1067df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1068df8bae1dSRodney W. Grimes {
10692c42a146SMarcel Moolenaar 	sigset_t mask;
10702c42a146SMarcel Moolenaar 	int error;
10712c42a146SMarcel Moolenaar 
10726f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
10732c42a146SMarcel Moolenaar 	if (error)
10742c42a146SMarcel Moolenaar 		return (error);
10758f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
10768f19eb88SIan Dowse }
10778f19eb88SIan Dowse 
10788f19eb88SIan Dowse int
10798f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
10808f19eb88SIan Dowse {
10818f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1082df8bae1dSRodney W. Grimes 
1083df8bae1dSRodney W. Grimes 	/*
1084645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1085df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1086df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1087df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1088df8bae1dSRodney W. Grimes 	 * to indicate this.
1089df8bae1dSRodney W. Grimes 	 */
1090628d2653SJohn Baldwin 	PROC_LOCK(p);
10914093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
10925e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1093645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
10944093529dSJeff Roberson 	td->td_sigmask = mask;
10954093529dSJeff Roberson 	signotify(td);
1096b1bf1c3aSJohn Baldwin 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
10972c42a146SMarcel Moolenaar 		/* void */;
1098628d2653SJohn Baldwin 	PROC_UNLOCK(p);
10992c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11002c42a146SMarcel Moolenaar 	return (EINTR);
11012c42a146SMarcel Moolenaar }
11022c42a146SMarcel Moolenaar 
110331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
11042c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11052c42a146SMarcel Moolenaar struct osigsuspend_args {
11062c42a146SMarcel Moolenaar 	osigset_t mask;
11072c42a146SMarcel Moolenaar };
11082c42a146SMarcel Moolenaar #endif
1109fb99ab88SMatthew Dillon /*
1110fb99ab88SMatthew Dillon  * MPSAFE
1111fb99ab88SMatthew Dillon  */
11122c42a146SMarcel Moolenaar /* ARGSUSED */
11132c42a146SMarcel Moolenaar int
1114b40ce416SJulian Elischer osigsuspend(td, uap)
1115b40ce416SJulian Elischer 	struct thread *td;
11162c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11172c42a146SMarcel Moolenaar {
1118b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1119645682fdSLuoqi Chen 	sigset_t mask;
11202c42a146SMarcel Moolenaar 
1121628d2653SJohn Baldwin 	PROC_LOCK(p);
11224093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11235e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1124645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1125645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11264093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11274093529dSJeff Roberson 	signotify(td);
1128b1bf1c3aSJohn Baldwin 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1129df8bae1dSRodney W. Grimes 		/* void */;
1130628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1131df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1132df8bae1dSRodney W. Grimes 	return (EINTR);
1133df8bae1dSRodney W. Grimes }
113431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1137d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1138df8bae1dSRodney W. Grimes struct osigstack_args {
1139df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1140df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1141df8bae1dSRodney W. Grimes };
1142d2d3e875SBruce Evans #endif
1143fb99ab88SMatthew Dillon /*
1144fb99ab88SMatthew Dillon  * MPSAFE
1145fb99ab88SMatthew Dillon  */
1146df8bae1dSRodney W. Grimes /* ARGSUSED */
114726f9a767SRodney W. Grimes int
1148b40ce416SJulian Elischer osigstack(td, uap)
1149b40ce416SJulian Elischer 	struct thread *td;
1150df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1151df8bae1dSRodney W. Grimes {
1152b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
11535afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1154fb99ab88SMatthew Dillon 	int error = 0;
1155fb99ab88SMatthew Dillon 
1156d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
11575afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
11585afe0c99SJohn Baldwin 		if (error)
11595afe0c99SJohn Baldwin 			return (error);
1160df8bae1dSRodney W. Grimes 	}
11615afe0c99SJohn Baldwin 	PROC_LOCK(p);
11625afe0c99SJohn Baldwin 	oss.ss_sp = p->p_sigstk.ss_sp;
11635afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
11645afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
11655afe0c99SJohn Baldwin 		p->p_sigstk.ss_sp = nss.ss_sp;
11665afe0c99SJohn Baldwin 		p->p_sigstk.ss_size = 0;
11675afe0c99SJohn Baldwin 		p->p_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
11685afe0c99SJohn Baldwin 		p->p_flag |= P_ALTSTACK;
11695afe0c99SJohn Baldwin 	}
11705afe0c99SJohn Baldwin 	PROC_UNLOCK(p);
11715afe0c99SJohn Baldwin 	if (uap->oss != NULL)
11725afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
11735afe0c99SJohn Baldwin 
1174fb99ab88SMatthew Dillon 	return (error);
1175df8bae1dSRodney W. Grimes }
1176df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1177df8bae1dSRodney W. Grimes 
1178d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1179df8bae1dSRodney W. Grimes struct sigaltstack_args {
11802c42a146SMarcel Moolenaar 	stack_t	*ss;
11812c42a146SMarcel Moolenaar 	stack_t	*oss;
1182df8bae1dSRodney W. Grimes };
1183d2d3e875SBruce Evans #endif
1184fb99ab88SMatthew Dillon /*
1185fb99ab88SMatthew Dillon  * MPSAFE
1186fb99ab88SMatthew Dillon  */
1187df8bae1dSRodney W. Grimes /* ARGSUSED */
118826f9a767SRodney W. Grimes int
1189b40ce416SJulian Elischer sigaltstack(td, uap)
1190b40ce416SJulian Elischer 	struct thread *td;
1191df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1192df8bae1dSRodney W. Grimes {
11938f19eb88SIan Dowse 	stack_t ss, oss;
11948f19eb88SIan Dowse 	int error;
11958f19eb88SIan Dowse 
11968f19eb88SIan Dowse 	if (uap->ss != NULL) {
11978f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
11988f19eb88SIan Dowse 		if (error)
11998f19eb88SIan Dowse 			return (error);
12008f19eb88SIan Dowse 	}
12018f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12028f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12038f19eb88SIan Dowse 	if (error)
12048f19eb88SIan Dowse 		return (error);
12058f19eb88SIan Dowse 	if (uap->oss != NULL)
12068f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12078f19eb88SIan Dowse 	return (error);
12088f19eb88SIan Dowse }
12098f19eb88SIan Dowse 
12108f19eb88SIan Dowse int
12118f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12128f19eb88SIan Dowse {
1213b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1214fb99ab88SMatthew Dillon 	int oonstack;
1215fb99ab88SMatthew Dillon 
121606ce69a7SDavid Xu 	PROC_LOCK(p);
1217b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1218d034d459SMarcel Moolenaar 
12198f19eb88SIan Dowse 	if (oss != NULL) {
12208f19eb88SIan Dowse 		*oss = p->p_sigstk;
12218f19eb88SIan Dowse 		oss->ss_flags = (p->p_flag & P_ALTSTACK)
1222d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1223df8bae1dSRodney W. Grimes 	}
1224d034d459SMarcel Moolenaar 
12258f19eb88SIan Dowse 	if (ss != NULL) {
1226fb99ab88SMatthew Dillon 		if (oonstack) {
1227cf60731bSJohn Baldwin 			PROC_UNLOCK(p);
1228cf60731bSJohn Baldwin 			return (EPERM);
1229fb99ab88SMatthew Dillon 		}
12308f19eb88SIan Dowse 		if ((ss->ss_flags & ~SS_DISABLE) != 0) {
1231cf60731bSJohn Baldwin 			PROC_UNLOCK(p);
1232cf60731bSJohn Baldwin 			return (EINVAL);
1233fb99ab88SMatthew Dillon 		}
12348f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12358f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1236cf60731bSJohn Baldwin 				PROC_UNLOCK(p);
1237cf60731bSJohn Baldwin 				return (ENOMEM);
1238fb99ab88SMatthew Dillon 			}
12398f19eb88SIan Dowse 			p->p_sigstk = *ss;
1240d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
1241628d2653SJohn Baldwin 		} else {
1242d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
1243628d2653SJohn Baldwin 		}
1244d034d459SMarcel Moolenaar 	}
124506ce69a7SDavid Xu 	PROC_UNLOCK(p);
1246cf60731bSJohn Baldwin 	return (0);
1247df8bae1dSRodney W. Grimes }
1248df8bae1dSRodney W. Grimes 
1249d93f860cSPoul-Henning Kamp /*
1250d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1251d93f860cSPoul-Henning Kamp  * cp is calling process.
1252d93f860cSPoul-Henning Kamp  */
125337c84183SPoul-Henning Kamp static int
12549c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
12559c1ab3e0SJohn Baldwin 	register struct thread *td;
12562c42a146SMarcel Moolenaar 	int sig, pgid, all;
1257d93f860cSPoul-Henning Kamp {
1258d93f860cSPoul-Henning Kamp 	register struct proc *p;
1259d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1260d93f860cSPoul-Henning Kamp 	int nfound = 0;
1261d93f860cSPoul-Henning Kamp 
1262553629ebSJake Burkholder 	if (all) {
1263d93f860cSPoul-Henning Kamp 		/*
1264d93f860cSPoul-Henning Kamp 		 * broadcast
1265d93f860cSPoul-Henning Kamp 		 */
12661005a129SJohn Baldwin 		sx_slock(&allproc_lock);
12672e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1268628d2653SJohn Baldwin 			PROC_LOCK(p);
12699c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
12709c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1271628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1272628d2653SJohn Baldwin 				continue;
1273628d2653SJohn Baldwin 			}
1274f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1275d93f860cSPoul-Henning Kamp 				nfound++;
127633a9ed9dSJohn Baldwin 				if (sig)
12772c42a146SMarcel Moolenaar 					psignal(p, sig);
1278628d2653SJohn Baldwin 			}
127933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1280d93f860cSPoul-Henning Kamp 		}
12811005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1282553629ebSJake Burkholder 	} else {
1283ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1284f591779bSSeigo Tanimura 		if (pgid == 0) {
1285d93f860cSPoul-Henning Kamp 			/*
1286d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1287d93f860cSPoul-Henning Kamp 			 */
12889c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1289f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1290f591779bSSeigo Tanimura 		} else {
1291d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1292f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1293ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1294d93f860cSPoul-Henning Kamp 				return (ESRCH);
1295d93f860cSPoul-Henning Kamp 			}
1296f591779bSSeigo Tanimura 		}
1297ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
12982e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1299628d2653SJohn Baldwin 			PROC_LOCK(p);
1300628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1301628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1302628d2653SJohn Baldwin 				continue;
1303628d2653SJohn Baldwin 			}
1304e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
130533a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1306628d2653SJohn Baldwin 				continue;
1307628d2653SJohn Baldwin 			}
1308f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1309d93f860cSPoul-Henning Kamp 				nfound++;
131033a9ed9dSJohn Baldwin 				if (sig)
13112c42a146SMarcel Moolenaar 					psignal(p, sig);
1312628d2653SJohn Baldwin 			}
131333a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1314d93f860cSPoul-Henning Kamp 		}
1315f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1316d93f860cSPoul-Henning Kamp 	}
1317d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1318d93f860cSPoul-Henning Kamp }
1319d93f860cSPoul-Henning Kamp 
1320d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1321df8bae1dSRodney W. Grimes struct kill_args {
1322df8bae1dSRodney W. Grimes 	int	pid;
1323df8bae1dSRodney W. Grimes 	int	signum;
1324df8bae1dSRodney W. Grimes };
1325d2d3e875SBruce Evans #endif
1326fb99ab88SMatthew Dillon /*
1327fb99ab88SMatthew Dillon  * MPSAFE
1328fb99ab88SMatthew Dillon  */
1329df8bae1dSRodney W. Grimes /* ARGSUSED */
133026f9a767SRodney W. Grimes int
1331b40ce416SJulian Elischer kill(td, uap)
1332b40ce416SJulian Elischer 	register struct thread *td;
1333df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1334df8bae1dSRodney W. Grimes {
1335df8bae1dSRodney W. Grimes 	register struct proc *p;
133690af4afaSJohn Baldwin 	int error;
1337df8bae1dSRodney W. Grimes 
13386c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1339df8bae1dSRodney W. Grimes 		return (EINVAL);
1340fb99ab88SMatthew Dillon 
1341df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1342df8bae1dSRodney W. Grimes 		/* kill single process */
134390af4afaSJohn Baldwin 		if ((p = pfind(uap->pid)) == NULL)
134490af4afaSJohn Baldwin 			return (ESRCH);
134590af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
134690af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1347df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1348628d2653SJohn Baldwin 		PROC_UNLOCK(p);
134990af4afaSJohn Baldwin 		return (error);
1350df8bae1dSRodney W. Grimes 	}
1351df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1352df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
135390af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1354df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
135590af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1356df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
135790af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1358df8bae1dSRodney W. Grimes 	}
135990af4afaSJohn Baldwin 	/* NOTREACHED */
1360df8bae1dSRodney W. Grimes }
1361df8bae1dSRodney W. Grimes 
1362df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1363d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1364df8bae1dSRodney W. Grimes struct okillpg_args {
1365df8bae1dSRodney W. Grimes 	int	pgid;
1366df8bae1dSRodney W. Grimes 	int	signum;
1367df8bae1dSRodney W. Grimes };
1368d2d3e875SBruce Evans #endif
1369fb99ab88SMatthew Dillon /*
1370fb99ab88SMatthew Dillon  * MPSAFE
1371fb99ab88SMatthew Dillon  */
1372df8bae1dSRodney W. Grimes /* ARGSUSED */
137326f9a767SRodney W. Grimes int
1374b40ce416SJulian Elischer okillpg(td, uap)
1375b40ce416SJulian Elischer 	struct thread *td;
1376df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1377df8bae1dSRodney W. Grimes {
1378df8bae1dSRodney W. Grimes 
13796c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1380df8bae1dSRodney W. Grimes 		return (EINVAL);
138190af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1382df8bae1dSRodney W. Grimes }
1383df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1384df8bae1dSRodney W. Grimes 
1385df8bae1dSRodney W. Grimes /*
1386df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1387df8bae1dSRodney W. Grimes  */
1388df8bae1dSRodney W. Grimes void
13892c42a146SMarcel Moolenaar gsignal(pgid, sig)
13902c42a146SMarcel Moolenaar 	int pgid, sig;
1391df8bae1dSRodney W. Grimes {
1392df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1393df8bae1dSRodney W. Grimes 
1394f591779bSSeigo Tanimura 	if (pgid != 0) {
1395ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1396f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1397ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1398f591779bSSeigo Tanimura 		if (pgrp != NULL) {
13992c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1400f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1401f591779bSSeigo Tanimura 		}
1402f591779bSSeigo Tanimura 	}
1403df8bae1dSRodney W. Grimes }
1404df8bae1dSRodney W. Grimes 
1405df8bae1dSRodney W. Grimes /*
1406df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1407df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1408df8bae1dSRodney W. Grimes  */
1409df8bae1dSRodney W. Grimes void
14102c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1411df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14122c42a146SMarcel Moolenaar 	int sig, checkctty;
1413df8bae1dSRodney W. Grimes {
1414df8bae1dSRodney W. Grimes 	register struct proc *p;
1415df8bae1dSRodney W. Grimes 
1416628d2653SJohn Baldwin 	if (pgrp) {
1417f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1418628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1419628d2653SJohn Baldwin 			PROC_LOCK(p);
1420df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14212c42a146SMarcel Moolenaar 				psignal(p, sig);
1422628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1423628d2653SJohn Baldwin 		}
1424628d2653SJohn Baldwin 	}
1425df8bae1dSRodney W. Grimes }
1426df8bae1dSRodney W. Grimes 
1427df8bae1dSRodney W. Grimes /*
14281bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1429df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1430df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1431356861dbSMatthew Dillon  *
1432356861dbSMatthew Dillon  * MPSAFE
1433df8bae1dSRodney W. Grimes  */
1434df8bae1dSRodney W. Grimes void
14351bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1436df8bae1dSRodney W. Grimes {
14371bf4700bSJeff Roberson 	struct sigacts *ps;
14381bf4700bSJeff Roberson 	struct proc *p;
14391bf4700bSJeff Roberson 
14401bf4700bSJeff Roberson 	p = td->td_proc;
1441df8bae1dSRodney W. Grimes 
1442628d2653SJohn Baldwin 	PROC_LOCK(p);
1443ef3dab76STim J. Robbins 	ps = p->p_sigacts;
144490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
144590af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
14464093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1447df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1448df8bae1dSRodney W. Grimes #ifdef KTRACE
1449374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1450374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
14514093529dSJeff Roberson 			    &td->td_sigmask, code);
1452df8bae1dSRodney W. Grimes #endif
1453a88b260aSJuli Mallett 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
14544093529dSJeff Roberson 						&td->td_sigmask, code);
14554093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
14562c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
14574093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
14582c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1459289ccde0SPeter Wemm 			/*
14608f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1461289ccde0SPeter Wemm 			 */
146290af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
14632c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
14642c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
146590af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
14662c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1467dedc04feSPeter Wemm 		}
146890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
14696f841fb7SMarcel Moolenaar 	} else {
147090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
14716626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
14722c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
14734093529dSJeff Roberson 		tdsignal(td, sig);
1474df8bae1dSRodney W. Grimes 	}
1475628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1476df8bae1dSRodney W. Grimes }
1477df8bae1dSRodney W. Grimes 
14784093529dSJeff Roberson static struct thread *
14794093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
14804093529dSJeff Roberson {
14814093529dSJeff Roberson 	struct thread *td;
14824093529dSJeff Roberson 
14834093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
14844093529dSJeff Roberson 
14854093529dSJeff Roberson 	/*
14864093529dSJeff Roberson 	 * If we know the signal is bound for a specific thread then we
14874093529dSJeff Roberson 	 * assume that we are in that threads context.  This is the case
14884093529dSJeff Roberson 	 * for SIGXCPU, SIGILL, etc.  Otherwise someone did a kill() from
14894093529dSJeff Roberson 	 * userland and the real thread doesn't actually matter.
14904093529dSJeff Roberson 	 */
14914093529dSJeff Roberson 	if ((prop & SA_PROC) != 0 && curthread->td_proc == p)
14924093529dSJeff Roberson 		return (curthread);
14934093529dSJeff Roberson 
14944093529dSJeff Roberson 	/*
14954093529dSJeff Roberson 	 * We should search for the first thread that is blocked in
14964093529dSJeff Roberson 	 * sigsuspend with this signal unmasked.
14974093529dSJeff Roberson 	 */
14984093529dSJeff Roberson 
14994093529dSJeff Roberson 	/* XXX */
15004093529dSJeff Roberson 
15014093529dSJeff Roberson 	/*
15024093529dSJeff Roberson 	 * Find the first thread in the proc that doesn't have this signal
15034093529dSJeff Roberson 	 * masked.
15044093529dSJeff Roberson 	 */
15054093529dSJeff Roberson 	FOREACH_THREAD_IN_PROC(p, td)
15064093529dSJeff Roberson 		if (!SIGISMEMBER(td->td_sigmask, sig))
15074093529dSJeff Roberson 			return (td);
15084093529dSJeff Roberson 
15094093529dSJeff Roberson 	return (FIRST_THREAD_IN_PROC(p));
15104093529dSJeff Roberson }
15114093529dSJeff Roberson 
1512df8bae1dSRodney W. Grimes /*
1513df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1514df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1515df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1516df8bae1dSRodney W. Grimes  *
1517df8bae1dSRodney W. Grimes  * Exceptions:
1518df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1519df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1520df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1521df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1522df8bae1dSRodney W. Grimes  *
1523df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
152490af4afaSJohn Baldwin  *
152590af4afaSJohn Baldwin  * MPSAFE
1526df8bae1dSRodney W. Grimes  */
1527df8bae1dSRodney W. Grimes void
15284093529dSJeff Roberson psignal(struct proc *p, int sig)
1529df8bae1dSRodney W. Grimes {
1530b40ce416SJulian Elischer 	struct thread *td;
15314093529dSJeff Roberson 	int prop;
15324093529dSJeff Roberson 
15334093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15344093529dSJeff Roberson 	prop = sigprop(sig);
15354093529dSJeff Roberson 
15364093529dSJeff Roberson 	/*
15374093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
15384093529dSJeff Roberson 	 */
15394093529dSJeff Roberson 	td = sigtd(p, sig, prop);
15404093529dSJeff Roberson 
15414093529dSJeff Roberson 	tdsignal(td, sig);
15424093529dSJeff Roberson }
15434093529dSJeff Roberson 
154490af4afaSJohn Baldwin /*
154590af4afaSJohn Baldwin  * MPSAFE
154690af4afaSJohn Baldwin  */
15474093529dSJeff Roberson void
15484093529dSJeff Roberson tdsignal(struct thread *td, int sig)
15494093529dSJeff Roberson {
15504093529dSJeff Roberson 	struct proc *p;
15514093529dSJeff Roberson 	register sig_t action;
15524093529dSJeff Roberson 	sigset_t *siglist;
15534093529dSJeff Roberson 	struct thread *td0;
1554e602ba25SJulian Elischer 	register int prop;
155590af4afaSJohn Baldwin 	struct sigacts *ps;
1556df8bae1dSRodney W. Grimes 
15572899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
15584093529dSJeff Roberson 	    ("tdsignal(): invalid signal %d\n", sig));
15594093529dSJeff Roberson 
15604093529dSJeff Roberson 	p = td->td_proc;
156190af4afaSJohn Baldwin 	ps = p->p_sigacts;
15622c42a146SMarcel Moolenaar 
1563628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1564cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1565cb679c38SJonathan Lemon 
15662c42a146SMarcel Moolenaar 	prop = sigprop(sig);
15674093529dSJeff Roberson 
15684093529dSJeff Roberson 	/*
15694093529dSJeff Roberson 	 * If this thread is blocking this signal then we'll leave it in the
15704093529dSJeff Roberson 	 * proc so that we can find it in the first thread that unblocks it.
15714093529dSJeff Roberson 	 */
15724093529dSJeff Roberson 	if (SIGISMEMBER(td->td_sigmask, sig))
15734093529dSJeff Roberson 		siglist = &p->p_siglist;
15744093529dSJeff Roberson 	else
15754093529dSJeff Roberson 		siglist = &td->td_siglist;
15764093529dSJeff Roberson 
1577df8bae1dSRodney W. Grimes 	/*
15782a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
15792a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
15802a024a2bSSean Eric Fagan 	 * a chance, as well.
1581df8bae1dSRodney W. Grimes 	 */
1582b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1583df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1584b40ce416SJulian Elischer 	} else {
1585df8bae1dSRodney W. Grimes 		/*
1586df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1587df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
158890af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1589df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1590df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1591df8bae1dSRodney W. Grimes 		 */
159290af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
159390af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
159490af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
159590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1596df8bae1dSRodney W. Grimes 			return;
159790af4afaSJohn Baldwin 		}
15984093529dSJeff Roberson 		if (SIGISMEMBER(td->td_sigmask, sig))
1599df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
160090af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1601df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1602df8bae1dSRodney W. Grimes 		else
1603df8bae1dSRodney W. Grimes 			action = SIG_DFL;
160490af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1605df8bae1dSRodney W. Grimes 	}
1606df8bae1dSRodney W. Grimes 
16074093529dSJeff Roberson 	if (prop & SA_CONT) {
16081d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
16094093529dSJeff Roberson 		/*
16104093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
16114093529dSJeff Roberson 		 * the proc's siglist.
16124093529dSJeff Roberson 		 */
16134093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
16144093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
16154093529dSJeff Roberson 	}
1616df8bae1dSRodney W. Grimes 
1617df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1618df8bae1dSRodney W. Grimes 		/*
1619df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1620df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1621df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1622df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1623df8bae1dSRodney W. Grimes 		 */
1624e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1625e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1626e602ba25SJulian Elischer 		    (action == SIG_DFL))
1627df8bae1dSRodney W. Grimes 		        return;
16281d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
16294093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
16304093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
16316933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1632df8bae1dSRodney W. Grimes 	}
16334093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
16344093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
16355312b1c7SDavid Xu 	/*
16365312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
16375312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
16385312b1c7SDavid Xu 	 */
16395312b1c7SDavid Xu 	if (action == SIG_HOLD &&
16405312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
16415312b1c7SDavid Xu 		return;
1642df8bae1dSRodney W. Grimes 	/*
1643e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1644e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1645e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1646e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1647e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1648e602ba25SJulian Elischer 	 * We try do the per-process part here.
1649df8bae1dSRodney W. Grimes 	 */
1650e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1651e602ba25SJulian Elischer 		/*
1652e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1653e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1654e602ba25SJulian Elischer 		 */
1655e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1656e602ba25SJulian Elischer 			/*
1657e602ba25SJulian Elischer 			 * The traced process is already stopped,
1658e602ba25SJulian Elischer 			 * so no further action is necessary.
1659e602ba25SJulian Elischer 			 * No signal can restart us.
1660e602ba25SJulian Elischer 			 */
1661e602ba25SJulian Elischer 			goto out;
16621c32c37cSJohn Baldwin 		}
1663b40ce416SJulian Elischer 
1664e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1665df8bae1dSRodney W. Grimes 			/*
1666e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1667e602ba25SJulian Elischer 			 * It will die elsewhere.
1668e602ba25SJulian Elischer 			 * All threads must be restarted.
1669df8bae1dSRodney W. Grimes 			 */
1670e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1671e602ba25SJulian Elischer 			goto runfast;
1672e602ba25SJulian Elischer 		}
1673e602ba25SJulian Elischer 
1674e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1675e602ba25SJulian Elischer 			/*
1676e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
16774093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
16781d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1679e602ba25SJulian Elischer 			 * continue the process and leave the signal in
16804093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1681e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1682e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1683e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1684e602ba25SJulian Elischer 			 */
16851279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
16866933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1687e602ba25SJulian Elischer 			if (action == SIG_DFL) {
16884093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1689e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1690e602ba25SJulian Elischer 				/*
1691e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1692e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1693e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1694e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1695e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1696e602ba25SJulian Elischer 				 * process, we need to make sure that the
1697e602ba25SJulian Elischer 				 * single thread is runnable asap.
1698e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1699e602ba25SJulian Elischer 				 */
1700e602ba25SJulian Elischer 				goto runfast;
1701e602ba25SJulian Elischer 			}
1702e602ba25SJulian Elischer 			/*
1703e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1704e602ba25SJulian Elischer 			 */
1705e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
170604774f23SJulian Elischer 			thread_unsuspend(p);
1707e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1708e602ba25SJulian Elischer 			goto out;
1709e602ba25SJulian Elischer 		}
1710e602ba25SJulian Elischer 
1711e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1712e602ba25SJulian Elischer 			/*
1713e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1714e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
171504774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1716e602ba25SJulian Elischer 			 */
17171279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
17184093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1719e602ba25SJulian Elischer 			goto out;
1720e602ba25SJulian Elischer 		}
1721e602ba25SJulian Elischer 
1722e602ba25SJulian Elischer 		/*
1723e602ba25SJulian Elischer 		 * All other kinds of signals:
1724e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1725e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1726e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
172704774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1728e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1729e602ba25SJulian Elischer 		 */
1730e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
173171fad9fdSJulian Elischer 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1732e602ba25SJulian Elischer 			if (td->td_flags & TDF_CVWAITQ)
173371fad9fdSJulian Elischer 				cv_abort(td);
1734e602ba25SJulian Elischer 			else
173571fad9fdSJulian Elischer 				abortsleep(td);
1736e602ba25SJulian Elischer 		}
1737e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1738df8bae1dSRodney W. Grimes 		goto out;
1739df8bae1dSRodney W. Grimes 		/*
1740e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1741e602ba25SJulian Elischer 		 * Shouldn't they abort too?
174204774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
174304774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1744df8bae1dSRodney W. Grimes 		 */
1745e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
174635c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
174735c32a76SDavid Xu 			!(prop & SA_STOP)) {
1748721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
17494093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1750721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1751df8bae1dSRodney W. Grimes 			goto out;
175204774f23SJulian Elischer 		}
175335c32a76SDavid Xu 		if (prop & SA_STOP) {
175435c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
175535c32a76SDavid Xu 				goto out;
1756e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1757e574e444SDavid Xu 			p->p_xstat = sig;
175835c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
17594093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
17604093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
176135c32a76SDavid Xu 					(td->td_flags & TDF_SINTR))
17624093529dSJeff Roberson 					thread_suspend_one(td0);
176335c32a76SDavid Xu 			}
1764e574e444SDavid Xu 			thread_stopped(p);
17654093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1766e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
17674093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
17684093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
17694093529dSJeff Roberson 			}
177035c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
177135c32a76SDavid Xu 			goto out;
177235c32a76SDavid Xu 		}
1773721e5910SJulian Elischer 		else
1774df8bae1dSRodney W. Grimes 			goto runfast;
1775df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1776e602ba25SJulian Elischer 	} else {
1777e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
17784093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1779e602ba25SJulian Elischer 		goto out;
1780e602ba25SJulian Elischer 	}
1781e602ba25SJulian Elischer 
1782b40ce416SJulian Elischer 	/*
1783e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1784e602ba25SJulian Elischer 	 * running threads.
1785b40ce416SJulian Elischer 	 */
1786e602ba25SJulian Elischer 
1787e602ba25SJulian Elischer runfast:
1788aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
17894093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1790e602ba25SJulian Elischer 	thread_unsuspend(p);
1791e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1792e602ba25SJulian Elischer out:
1793e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1794e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1795e602ba25SJulian Elischer }
1796e602ba25SJulian Elischer 
1797e602ba25SJulian Elischer /*
1798e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1799e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1800e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1801e602ba25SJulian Elischer  */
1802e602ba25SJulian Elischer static void
18034093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1804e602ba25SJulian Elischer {
1805e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1806e602ba25SJulian Elischer 	register int prop;
1807e602ba25SJulian Elischer 
18088b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1809aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1810e602ba25SJulian Elischer 	prop = sigprop(sig);
1811e602ba25SJulian Elischer 	/*
1812aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1813e602ba25SJulian Elischer 	 * killed in this lifetime.
1814e602ba25SJulian Elischer 	 */
1815e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1816e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1817e602ba25SJulian Elischer 			td->td_priority = PUSER;
1818b40ce416SJulian Elischer 		}
1819b40ce416SJulian Elischer 	}
182071fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1821e602ba25SJulian Elischer 		/*
1822e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1823e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1824e602ba25SJulian Elischer 		 * be noticed when the process returns through
1825e602ba25SJulian Elischer 		 * trap() or syscall().
1826e602ba25SJulian Elischer 		 */
1827e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1828aa0fa334SJulian Elischer 			return;
1829e602ba25SJulian Elischer 		}
1830e602ba25SJulian Elischer 		/*
1831e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1832e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1833e602ba25SJulian Elischer 		 * for its parent.
1834e602ba25SJulian Elischer 		 */
1835e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1836e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1837aa0fa334SJulian Elischer 		} else {
1838aa0fa334SJulian Elischer 
1839df8bae1dSRodney W. Grimes 			/*
1840e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1841e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1842e602ba25SJulian Elischer 			 * be awakened.
1843df8bae1dSRodney W. Grimes 			 */
1844e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
18451d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
18464093529dSJeff Roberson 				/*
18474093529dSJeff Roberson 				 * It may be on either list in this state.
18484093529dSJeff Roberson 				 * Remove from both for now.
18494093529dSJeff Roberson 				 */
18504093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1851aa0fa334SJulian Elischer 				return;
1852df8bae1dSRodney W. Grimes 			}
1853df8bae1dSRodney W. Grimes 
1854aa0fa334SJulian Elischer 			/*
1855aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1856aa0fa334SJulian Elischer 			 */
1857aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
1858aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1859aa0fa334SJulian Elischer 			}
1860aa0fa334SJulian Elischer 		}
186171fad9fdSJulian Elischer 		if (td->td_flags & TDF_CVWAITQ)
186271fad9fdSJulian Elischer 			cv_abort(td);
186371fad9fdSJulian Elischer 		else
186471fad9fdSJulian Elischer 			abortsleep(td);
1865aa0fa334SJulian Elischer 	}
1866aa0fa334SJulian Elischer #ifdef SMP
1867aa0fa334SJulian Elischer 	  else {
1868df8bae1dSRodney W. Grimes 		/*
1869e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
1870df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1871df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1872df8bae1dSRodney W. Grimes 		 */
187371fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td) && td != curthread) {
1874e602ba25SJulian Elischer 			forward_signal(td);
1875aa0fa334SJulian Elischer 		}
18760ac3b636SAndrew Gallatin 	  }
18773163861cSTor Egge #endif
18786caa8a15SJohn Baldwin }
1879df8bae1dSRodney W. Grimes 
1880df8bae1dSRodney W. Grimes /*
1881df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1882df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1883df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1884df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1885df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1886628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
1887df8bae1dSRodney W. Grimes  * sequence is
1888df8bae1dSRodney W. Grimes  *
1889e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
18902c42a146SMarcel Moolenaar  *		postsig(sig);
1891df8bae1dSRodney W. Grimes  */
18926711f10fSJohn Baldwin static int
1893e602ba25SJulian Elischer issignal(td)
1894e602ba25SJulian Elischer 	struct thread *td;
1895df8bae1dSRodney W. Grimes {
1896e602ba25SJulian Elischer 	struct proc *p;
189790af4afaSJohn Baldwin 	struct sigacts *ps;
18984093529dSJeff Roberson 	sigset_t sigpending;
18992c42a146SMarcel Moolenaar 	register int sig, prop;
1900df8bae1dSRodney W. Grimes 
1901e602ba25SJulian Elischer 	p = td->td_proc;
190290af4afaSJohn Baldwin 	ps = p->p_sigacts;
190390af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
1904628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1905df8bae1dSRodney W. Grimes 	for (;;) {
19062a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
19072a024a2bSSean Eric Fagan 
19084093529dSJeff Roberson 		sigpending = td->td_siglist;
19094093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
19104093529dSJeff Roberson 
1911df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
19124093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
19134093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
1914df8bae1dSRodney W. Grimes 			return (0);
19154093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
19162a024a2bSSean Eric Fagan 
1917628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
19182a024a2bSSean Eric Fagan 
1919df8bae1dSRodney W. Grimes 		/*
1920df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1921df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1922df8bae1dSRodney W. Grimes 		 */
192390af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
19244093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
1925df8bae1dSRodney W. Grimes 			continue;
1926df8bae1dSRodney W. Grimes 		}
1927df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1928df8bae1dSRodney W. Grimes 			/*
1929d8f4f6a4SJonathan Mini 			 * If traced, always stop.
1930df8bae1dSRodney W. Grimes 			 */
193190af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
193290af4afaSJohn Baldwin 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
193390af4afaSJohn Baldwin 			    &p->p_mtx.mtx_object, "Stopping for traced signal");
19342c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1935628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1936df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1937628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
19389ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
19394d492b43SJulian Elischer 			stop(p);	/* uses schedlock too eventually */
194071fad9fdSJulian Elischer 			thread_suspend_one(td);
1941c86b6ff5SJohn Baldwin 			PROC_UNLOCK(p);
1942c86b6ff5SJohn Baldwin 			DROP_GIANT();
19432ad7d304SJohn Baldwin 			p->p_stats->p_ru.ru_nivcsw++;
1944df8bae1dSRodney W. Grimes 			mi_switch();
19459ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
194620cdcc5bSJohn Baldwin 			PICKUP_GIANT();
1947628d2653SJohn Baldwin 			PROC_LOCK(p);
194890af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
1949df8bae1dSRodney W. Grimes 
1950df8bae1dSRodney W. Grimes 			/*
1951df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1952df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1953df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1954df8bae1dSRodney W. Grimes 			 */
19554093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
19562c42a146SMarcel Moolenaar 			sig = p->p_xstat;
19572c42a146SMarcel Moolenaar 			if (sig == 0)
1958df8bae1dSRodney W. Grimes 				continue;
1959df8bae1dSRodney W. Grimes 
1960df8bae1dSRodney W. Grimes 			/*
19618d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
19628d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
19638d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
19648d542cb5SDavid E. O'Brien 			 */
19658d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
19668d542cb5SDavid E. O'Brien 				continue;
19678d542cb5SDavid E. O'Brien 
19688d542cb5SDavid E. O'Brien 			/*
19694093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
19701d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
1971df8bae1dSRodney W. Grimes 			 */
19724093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
19734093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
1974df8bae1dSRodney W. Grimes 				continue;
19754093529dSJeff Roberson 			signotify(td);
1976df8bae1dSRodney W. Grimes 		}
1977df8bae1dSRodney W. Grimes 
19788d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
19798d542cb5SDavid E. O'Brien 
1980df8bae1dSRodney W. Grimes 		/*
1981df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1982df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1983df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1984df8bae1dSRodney W. Grimes 		 */
1985d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1986df8bae1dSRodney W. Grimes 
1987d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
1988df8bae1dSRodney W. Grimes 			/*
1989df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1990df8bae1dSRodney W. Grimes 			 */
1991df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1992df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1993df8bae1dSRodney W. Grimes 				/*
1994df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1995df8bae1dSRodney W. Grimes 				 * in init? XXX
1996df8bae1dSRodney W. Grimes 				 */
1997d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
19982c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1999df8bae1dSRodney W. Grimes #endif
2000df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2001df8bae1dSRodney W. Grimes 			}
2002df8bae1dSRodney W. Grimes 			/*
2003df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2004df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2005df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2006df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2007df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2008df8bae1dSRodney W. Grimes 			 */
2009df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2010df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2011df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2012df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2013df8bae1dSRodney W. Grimes 					break;	/* == ignore */
201490af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
201590af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
201690af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2017e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
20182c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2019721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2020e574e444SDavid Xu 				thread_stopped(p);
202171fad9fdSJulian Elischer 				thread_suspend_one(td);
2022721e5910SJulian Elischer 				PROC_UNLOCK(p);
2023721e5910SJulian Elischer 				DROP_GIANT();
2024721e5910SJulian Elischer 				p->p_stats->p_ru.ru_nivcsw++;
2025721e5910SJulian Elischer 				mi_switch();
2026721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2027721e5910SJulian Elischer 				PICKUP_GIANT();
2028721e5910SJulian Elischer 				PROC_LOCK(p);
202990af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2030df8bae1dSRodney W. Grimes 				break;
203121b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2032df8bae1dSRodney W. Grimes 				/*
2033df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2034df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2035df8bae1dSRodney W. Grimes 				 */
2036df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2037df8bae1dSRodney W. Grimes 			} else
20382c42a146SMarcel Moolenaar 				return (sig);
2039df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2040df8bae1dSRodney W. Grimes 
2041d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2042df8bae1dSRodney W. Grimes 			/*
2043df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2044df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2045df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2046df8bae1dSRodney W. Grimes 			 */
2047df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2048df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2049df8bae1dSRodney W. Grimes 				printf("issignal\n");
2050df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2051df8bae1dSRodney W. Grimes 
2052df8bae1dSRodney W. Grimes 		default:
2053df8bae1dSRodney W. Grimes 			/*
2054df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2055df8bae1dSRodney W. Grimes 			 * postsig() process it.
2056df8bae1dSRodney W. Grimes 			 */
20572c42a146SMarcel Moolenaar 			return (sig);
2058df8bae1dSRodney W. Grimes 		}
20594093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2060df8bae1dSRodney W. Grimes 	}
2061df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2062df8bae1dSRodney W. Grimes }
2063df8bae1dSRodney W. Grimes 
2064df8bae1dSRodney W. Grimes /*
2065df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2066df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
20675b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
20685b3047d5SJohn Baldwin  * lock held.
2069df8bae1dSRodney W. Grimes  */
20705b3047d5SJohn Baldwin static void
2071e574e444SDavid Xu stop(struct proc *p)
2072df8bae1dSRodney W. Grimes {
2073df8bae1dSRodney W. Grimes 
2074628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
20751279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2076df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
207701609114SAlfred Perlstein 	wakeup(p->p_pptr);
2078df8bae1dSRodney W. Grimes }
2079df8bae1dSRodney W. Grimes 
208090af4afaSJohn Baldwin /*
208190af4afaSJohn Baldwin  * MPSAFE
208290af4afaSJohn Baldwin  */
2083e574e444SDavid Xu void
2084e574e444SDavid Xu thread_stopped(struct proc *p)
2085e574e444SDavid Xu {
2086e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
208790af4afaSJohn Baldwin 	struct sigacts *ps;
2088e574e444SDavid Xu 	int n;
2089e574e444SDavid Xu 
2090e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2091e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2092e574e444SDavid Xu 	n = p->p_suspcount;
2093e574e444SDavid Xu 	if (p == p1)
2094e574e444SDavid Xu 		n++;
2095e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2096e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2097e574e444SDavid Xu 		stop(p);
2098e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
209990af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
210090af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
210190af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
210290af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2103e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
210490af4afaSJohn Baldwin 		} else
210590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2106e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2107e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2108e574e444SDavid Xu 	}
2109e574e444SDavid Xu }
2110e574e444SDavid Xu 
2111df8bae1dSRodney W. Grimes /*
2112df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2113df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2114df8bae1dSRodney W. Grimes  */
2115df8bae1dSRodney W. Grimes void
21162c42a146SMarcel Moolenaar postsig(sig)
21172c42a146SMarcel Moolenaar 	register int sig;
2118df8bae1dSRodney W. Grimes {
2119b40ce416SJulian Elischer 	struct thread *td = curthread;
2120b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2121628d2653SJohn Baldwin 	struct sigacts *ps;
21222c42a146SMarcel Moolenaar 	sig_t action;
21232c42a146SMarcel Moolenaar 	sigset_t returnmask;
21242c42a146SMarcel Moolenaar 	int code;
2125df8bae1dSRodney W. Grimes 
21262c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
21275526d2d9SEivind Eklund 
21282ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2129628d2653SJohn Baldwin 	ps = p->p_sigacts;
213090af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
21314093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
21322c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2133df8bae1dSRodney W. Grimes #ifdef KTRACE
2134374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
21355e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
21364093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2137df8bae1dSRodney W. Grimes #endif
2138628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
21392a024a2bSSean Eric Fagan 
2140df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
2141df8bae1dSRodney W. Grimes 		/*
2142df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2143df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2144df8bae1dSRodney W. Grimes 		 */
214590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2146b40ce416SJulian Elischer 		sigexit(td, sig);
2147df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2148df8bae1dSRodney W. Grimes 	} else {
2149df8bae1dSRodney W. Grimes 		/*
2150df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2151df8bae1dSRodney W. Grimes 		 */
21524093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
21535526d2d9SEivind Eklund 		    ("postsig action"));
2154df8bae1dSRodney W. Grimes 		/*
2155df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2156645682fdSLuoqi Chen 		 * occurrences of this signal.
2157df8bae1dSRodney W. Grimes 		 *
2158645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2159df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2160645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2161df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2162df8bae1dSRodney W. Grimes 		 */
21635e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
21644093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
21655e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2166df8bae1dSRodney W. Grimes 		} else
21674093529dSJeff Roberson 			returnmask = td->td_sigmask;
21682c42a146SMarcel Moolenaar 
21694093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
21702c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
21714093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
21722c42a146SMarcel Moolenaar 
21732c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2174289ccde0SPeter Wemm 			/*
21758f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2176289ccde0SPeter Wemm 			 */
217790af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
21782c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
21792c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
218090af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
21812c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2182dedc04feSPeter Wemm 		}
2183df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
21842c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2185df8bae1dSRodney W. Grimes 			code = 0;
2186df8bae1dSRodney W. Grimes 		} else {
21876626c604SJulian Elischer 			code = p->p_code;
21886626c604SJulian Elischer 			p->p_code = 0;
21896626c604SJulian Elischer 			p->p_sig = 0;
2190df8bae1dSRodney W. Grimes 		}
2191ac2e4153SJulian Elischer 		if (p->p_flag & P_THREADED)
219258a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
219358a3c273SJeff Roberson 		else
219458a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
219558a3c273SJeff Roberson 			    &returnmask, code);
2196df8bae1dSRodney W. Grimes 	}
2197df8bae1dSRodney W. Grimes }
2198df8bae1dSRodney W. Grimes 
2199df8bae1dSRodney W. Grimes /*
2200df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2201df8bae1dSRodney W. Grimes  */
220226f9a767SRodney W. Grimes void
2203df8bae1dSRodney W. Grimes killproc(p, why)
2204df8bae1dSRodney W. Grimes 	struct proc *p;
2205df8bae1dSRodney W. Grimes 	char *why;
2206df8bae1dSRodney W. Grimes {
22079081e5e8SJohn Baldwin 
22089081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22090384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
22100384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2211729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2212b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2213df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2214df8bae1dSRodney W. Grimes }
2215df8bae1dSRodney W. Grimes 
2216df8bae1dSRodney W. Grimes /*
2217df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2218df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2219df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2220df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2221df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2222df8bae1dSRodney W. Grimes  * does not return.
222390af4afaSJohn Baldwin  *
222490af4afaSJohn Baldwin  * MPSAFE
2225df8bae1dSRodney W. Grimes  */
222626f9a767SRodney W. Grimes void
2227b40ce416SJulian Elischer sigexit(td, sig)
2228b40ce416SJulian Elischer 	struct thread *td;
22292c42a146SMarcel Moolenaar 	int sig;
2230df8bae1dSRodney W. Grimes {
2231b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2232df8bae1dSRodney W. Grimes 
2233628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2234df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
22352c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
22362c42a146SMarcel Moolenaar 		p->p_sig = sig;
2237c364e17eSAndrey A. Chernov 		/*
2238c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2239c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2240c364e17eSAndrey A. Chernov 		 * these messages.)
2241c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
2242c364e17eSAndrey A. Chernov 		 */
2243628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2244c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
2245c31146a1SJohn Baldwin 			mtx_lock(&Giant);
2246b40ce416SJulian Elischer 		if (coredump(td) == 0)
22472c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
224857308494SJoerg Wunsch 		if (kern_logsigexit)
224957308494SJoerg Wunsch 			log(LOG_INFO,
225057308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
22513d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
22529c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
22532c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
22542c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
2255c31146a1SJohn Baldwin 	} else {
2256628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2257628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
2258628d2653SJohn Baldwin 			mtx_lock(&Giant);
2259c31146a1SJohn Baldwin 	}
2260b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2261df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2262df8bae1dSRodney W. Grimes }
2263df8bae1dSRodney W. Grimes 
2264c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2265c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2266c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2267c5edb423SSean Eric Fagan 
2268c5edb423SSean Eric Fagan /*
2269c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2270c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2271c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2272c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2273c5edb423SSean Eric Fagan  *	%P	process id (pid)
2274c5edb423SSean Eric Fagan  *	%U	user id (uid)
2275c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2276c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2277c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2278c5edb423SSean Eric Fagan  */
2279c5edb423SSean Eric Fagan 
2280fca666a1SJulian Elischer static char *
2281c5edb423SSean Eric Fagan expand_name(name, uid, pid)
22828b43b535SAlfred Perlstein 	const char *name;
22838b43b535SAlfred Perlstein 	uid_t uid;
22848b43b535SAlfred Perlstein 	pid_t pid;
22858b43b535SAlfred Perlstein {
22868b43b535SAlfred Perlstein 	const char *format, *appendstr;
2287c5edb423SSean Eric Fagan 	char *temp;
2288c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
22898b43b535SAlfred Perlstein 	size_t i, l, n;
2290c5edb423SSean Eric Fagan 
22918b43b535SAlfred Perlstein 	format = corefilename;
22928b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
22930bfe2990SEivind Eklund 	if (temp == NULL)
22948b43b535SAlfred Perlstein 		return (NULL);
2295ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2296c5edb423SSean Eric Fagan 		switch (format[i]) {
2297c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2298c5edb423SSean Eric Fagan 			i++;
2299c5edb423SSean Eric Fagan 			switch (format[i]) {
2300c5edb423SSean Eric Fagan 			case '%':
23018b43b535SAlfred Perlstein 				appendstr = "%";
2302c5edb423SSean Eric Fagan 				break;
2303c5edb423SSean Eric Fagan 			case 'N':	/* process name */
23048b43b535SAlfred Perlstein 				appendstr = name;
2305c5edb423SSean Eric Fagan 				break;
2306c5edb423SSean Eric Fagan 			case 'P':	/* process id */
23078b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
23088b43b535SAlfred Perlstein 				appendstr = buf;
2309c5edb423SSean Eric Fagan 				break;
2310c5edb423SSean Eric Fagan 			case 'U':	/* user id */
23118b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
23128b43b535SAlfred Perlstein 				appendstr = buf;
2313c5edb423SSean Eric Fagan 				break;
2314c5edb423SSean Eric Fagan 			default:
23158b43b535SAlfred Perlstein 				appendstr = "";
23168b43b535SAlfred Perlstein 			  	log(LOG_ERR,
23178b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
23188b43b535SAlfred Perlstein 				    format[i], format);
2319c5edb423SSean Eric Fagan 			}
23208b43b535SAlfred Perlstein 			l = strlen(appendstr);
23218b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
23228b43b535SAlfred Perlstein 				goto toolong;
23238b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
23248b43b535SAlfred Perlstein 			n += l;
2325c5edb423SSean Eric Fagan 			break;
2326c5edb423SSean Eric Fagan 		default:
2327c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2328c5edb423SSean Eric Fagan 		}
2329c5edb423SSean Eric Fagan 	}
23308b43b535SAlfred Perlstein 	if (format[i] != '\0')
23318b43b535SAlfred Perlstein 		goto toolong;
23328b43b535SAlfred Perlstein 	return (temp);
23338b43b535SAlfred Perlstein toolong:
23348b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
23358b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
23368b43b535SAlfred Perlstein 	free(temp, M_TEMP);
23378b43b535SAlfred Perlstein 	return (NULL);
2338c5edb423SSean Eric Fagan }
2339c5edb423SSean Eric Fagan 
2340df8bae1dSRodney W. Grimes /*
2341fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2342fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2343fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2344fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2345fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2346fca666a1SJulian Elischer  */
2347fca666a1SJulian Elischer 
2348fca666a1SJulian Elischer static int
2349b40ce416SJulian Elischer coredump(struct thread *td)
2350fca666a1SJulian Elischer {
2351b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2352fca666a1SJulian Elischer 	register struct vnode *vp;
23539c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
235406ae1e91SMatthew Dillon 	struct flock lf;
2355fca666a1SJulian Elischer 	struct nameidata nd;
2356fca666a1SJulian Elischer 	struct vattr vattr;
2357e6796b67SKirk McKusick 	int error, error1, flags;
2358f2a2857bSKirk McKusick 	struct mount *mp;
2359fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2360fca666a1SJulian Elischer 	off_t limit;
2361fca666a1SJulian Elischer 
2362628d2653SJohn Baldwin 	PROC_LOCK(p);
2363628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2364fca666a1SJulian Elischer 
2365628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2366628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2367fca666a1SJulian Elischer 		return (EFAULT);
2368628d2653SJohn Baldwin 	}
2369fca666a1SJulian Elischer 
2370fca666a1SJulian Elischer 	/*
237135a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
237235a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
237335a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
237435a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
237535a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
237635a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2377fca666a1SJulian Elischer 	 */
237835a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2379628d2653SJohn Baldwin 	if (limit == 0) {
2380628d2653SJohn Baldwin 		PROC_UNLOCK(p);
238135a2598fSSean Eric Fagan 		return 0;
2382628d2653SJohn Baldwin 	}
2383628d2653SJohn Baldwin 	PROC_UNLOCK(p);
238435a2598fSSean Eric Fagan 
2385f2a2857bSKirk McKusick restart:
23869c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2387ccdbd10cSPeter Pentchev 	if (name == NULL)
2388ccdbd10cSPeter Pentchev 		return (EINVAL);
2389b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2390e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2391e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2392fca666a1SJulian Elischer 	free(name, M_TEMP);
2393fca666a1SJulian Elischer 	if (error)
2394fca666a1SJulian Elischer 		return (error);
2395762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2396fca666a1SJulian Elischer 	vp = nd.ni_vp;
239706ae1e91SMatthew Dillon 
2398832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2399832dafadSDon Lewis 	if (vp->v_type != VREG ||
2400832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2401832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2402832dafadSDon Lewis 		error = EFAULT;
2403832dafadSDon Lewis 		goto out2;
2404832dafadSDon Lewis 	}
2405832dafadSDon Lewis 
2406b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
240706ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
240806ae1e91SMatthew Dillon 	lf.l_start = 0;
240906ae1e91SMatthew Dillon 	lf.l_len = 0;
241006ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
241106ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
241206ae1e91SMatthew Dillon 	if (error)
241306ae1e91SMatthew Dillon 		goto out2;
241406ae1e91SMatthew Dillon 
241506ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
241606ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
241706ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2418b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2419f2a2857bSKirk McKusick 			return (error);
2420f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2421f2a2857bSKirk McKusick 			return (error);
2422f2a2857bSKirk McKusick 		goto restart;
2423f2a2857bSKirk McKusick 	}
2424fca666a1SJulian Elischer 
2425fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2426fca666a1SJulian Elischer 	vattr.va_size = 0;
242788b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2428b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2429b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
243088b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2431628d2653SJohn Baldwin 	PROC_LOCK(p);
2432fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2433628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2434fca666a1SJulian Elischer 
2435fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2436b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2437fca666a1SJulian Elischer 	  ENOSYS;
2438fca666a1SJulian Elischer 
243906ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
244006ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2441f2a2857bSKirk McKusick 	vn_finished_write(mp);
244206ae1e91SMatthew Dillon out2:
2443b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2444fca666a1SJulian Elischer 	if (error == 0)
2445fca666a1SJulian Elischer 		error = error1;
2446fca666a1SJulian Elischer 	return (error);
2447fca666a1SJulian Elischer }
2448fca666a1SJulian Elischer 
2449fca666a1SJulian Elischer /*
2450df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2451df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2452df8bae1dSRodney W. Grimes  */
2453d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2454df8bae1dSRodney W. Grimes struct nosys_args {
2455df8bae1dSRodney W. Grimes 	int	dummy;
2456df8bae1dSRodney W. Grimes };
2457d2d3e875SBruce Evans #endif
2458fb99ab88SMatthew Dillon /*
2459fb99ab88SMatthew Dillon  * MPSAFE
2460fb99ab88SMatthew Dillon  */
2461df8bae1dSRodney W. Grimes /* ARGSUSED */
246226f9a767SRodney W. Grimes int
2463b40ce416SJulian Elischer nosys(td, args)
2464b40ce416SJulian Elischer 	struct thread *td;
2465df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2466df8bae1dSRodney W. Grimes {
2467b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2468b40ce416SJulian Elischer 
2469628d2653SJohn Baldwin 	PROC_LOCK(p);
2470df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2471628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2472f5216b9aSBruce Evans 	return (ENOSYS);
2473df8bae1dSRodney W. Grimes }
2474831d27a9SDon Lewis 
2475831d27a9SDon Lewis /*
247648f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2477831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2478831d27a9SDon Lewis  */
2479831d27a9SDon Lewis void
2480f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2481f1320723SAlfred Perlstein 	struct sigio **sigiop;
24822c42a146SMarcel Moolenaar 	int sig, checkctty;
2483831d27a9SDon Lewis {
2484f1320723SAlfred Perlstein 	struct sigio *sigio;
2485831d27a9SDon Lewis 
2486f1320723SAlfred Perlstein 	SIGIO_LOCK();
2487f1320723SAlfred Perlstein 	sigio = *sigiop;
2488f1320723SAlfred Perlstein 	if (sigio == NULL) {
2489f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2490f1320723SAlfred Perlstein 		return;
2491f1320723SAlfred Perlstein 	}
2492831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2493628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
24942b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
24952c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2496628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2497831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2498831d27a9SDon Lewis 		struct proc *p;
2499831d27a9SDon Lewis 
2500f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2501628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2502628d2653SJohn Baldwin 			PROC_LOCK(p);
25032b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2504831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
25052c42a146SMarcel Moolenaar 				psignal(p, sig);
2506628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2507628d2653SJohn Baldwin 		}
2508f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2509831d27a9SDon Lewis 	}
2510f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2511831d27a9SDon Lewis }
2512cb679c38SJonathan Lemon 
2513cb679c38SJonathan Lemon static int
2514cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2515cb679c38SJonathan Lemon {
2516cb679c38SJonathan Lemon 	struct proc *p = curproc;
2517cb679c38SJonathan Lemon 
2518cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2519cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2520cb679c38SJonathan Lemon 
2521628d2653SJohn Baldwin 	PROC_LOCK(p);
2522cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2523628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2524cb679c38SJonathan Lemon 
2525cb679c38SJonathan Lemon 	return (0);
2526cb679c38SJonathan Lemon }
2527cb679c38SJonathan Lemon 
2528cb679c38SJonathan Lemon static void
2529cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2530cb679c38SJonathan Lemon {
2531cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2532cb679c38SJonathan Lemon 
2533628d2653SJohn Baldwin 	PROC_LOCK(p);
2534e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2535628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2536cb679c38SJonathan Lemon }
2537cb679c38SJonathan Lemon 
2538cb679c38SJonathan Lemon /*
2539cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2540cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2541cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2542cb679c38SJonathan Lemon  * isn't worth the trouble.
2543cb679c38SJonathan Lemon  */
2544cb679c38SJonathan Lemon static int
2545cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2546cb679c38SJonathan Lemon {
2547cb679c38SJonathan Lemon 
2548cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2549cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2550cb679c38SJonathan Lemon 
2551cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2552cb679c38SJonathan Lemon 			kn->kn_data++;
2553cb679c38SJonathan Lemon 	}
2554cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2555cb679c38SJonathan Lemon }
255690af4afaSJohn Baldwin 
255790af4afaSJohn Baldwin struct sigacts *
255890af4afaSJohn Baldwin sigacts_alloc(void)
255990af4afaSJohn Baldwin {
256090af4afaSJohn Baldwin 	struct sigacts *ps;
256190af4afaSJohn Baldwin 
256290af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
256390af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
256490af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
256590af4afaSJohn Baldwin 	return (ps);
256690af4afaSJohn Baldwin }
256790af4afaSJohn Baldwin 
256890af4afaSJohn Baldwin void
256990af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
257090af4afaSJohn Baldwin {
257190af4afaSJohn Baldwin 
257290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
257390af4afaSJohn Baldwin 	ps->ps_refcnt--;
257490af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
257590af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
257690af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
257790af4afaSJohn Baldwin 	} else
257890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
257990af4afaSJohn Baldwin }
258090af4afaSJohn Baldwin 
258190af4afaSJohn Baldwin struct sigacts *
258290af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
258390af4afaSJohn Baldwin {
258490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
258590af4afaSJohn Baldwin 	ps->ps_refcnt++;
258690af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
258790af4afaSJohn Baldwin 	return (ps);
258890af4afaSJohn Baldwin }
258990af4afaSJohn Baldwin 
259090af4afaSJohn Baldwin void
259190af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
259290af4afaSJohn Baldwin {
259390af4afaSJohn Baldwin 
259490af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
259590af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
259690af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
259790af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
259890af4afaSJohn Baldwin }
259990af4afaSJohn Baldwin 
260090af4afaSJohn Baldwin int
260190af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
260290af4afaSJohn Baldwin {
260390af4afaSJohn Baldwin 	int shared;
260490af4afaSJohn Baldwin 
260590af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
260690af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
260790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
260890af4afaSJohn Baldwin 	return (shared);
260990af4afaSJohn Baldwin }
2610