xref: /freebsd/sys/kern/kern_sig.c (revision 418228df2424390acd50e7b79091ffc457ad91f4)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41677b542eSDavid E. O'Brien #include <sys/cdefs.h>
42677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
43677b542eSDavid E. O'Brien 
445591b823SEivind Eklund #include "opt_compat.h"
45db6a20e2SGarrett Wollman #include "opt_ktrace.h"
46db6a20e2SGarrett Wollman 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
4836240ea5SDoug Rabson #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/vnode.h>
51df8bae1dSRodney W. Grimes #include <sys/acct.h>
52238510fcSJason Evans #include <sys/condvar.h>
53854dc8c2SJohn Baldwin #include <sys/event.h>
54854dc8c2SJohn Baldwin #include <sys/fcntl.h>
55854dc8c2SJohn Baldwin #include <sys/kernel.h>
560384fff8SJason Evans #include <sys/ktr.h>
57df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
58854dc8c2SJohn Baldwin #include <sys/lock.h>
59854dc8c2SJohn Baldwin #include <sys/malloc.h>
60854dc8c2SJohn Baldwin #include <sys/mutex.h>
61854dc8c2SJohn Baldwin #include <sys/namei.h>
62854dc8c2SJohn Baldwin #include <sys/proc.h>
63854dc8c2SJohn Baldwin #include <sys/pioctl.h>
64c31146a1SJohn Baldwin #include <sys/resourcevar.h>
656caa8a15SJohn Baldwin #include <sys/smp.h>
66df8bae1dSRodney W. Grimes #include <sys/stat.h>
671005a129SJohn Baldwin #include <sys/sx.h>
688f19eb88SIan Dowse #include <sys/syscallsubr.h>
69c87e2930SDavid Greenman #include <sys/sysctl.h>
70854dc8c2SJohn Baldwin #include <sys/sysent.h>
71854dc8c2SJohn Baldwin #include <sys/syslog.h>
72854dc8c2SJohn Baldwin #include <sys/sysproto.h>
7306ae1e91SMatthew Dillon #include <sys/unistd.h>
74854dc8c2SJohn Baldwin #include <sys/wait.h>
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <machine/cpu.h>
77df8bae1dSRodney W. Grimes 
7823eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
7923eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
8023eeeff7SPeter Wemm #endif
8123eeeff7SPeter Wemm 
826f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
836f841fb7SMarcel Moolenaar 
844d77a549SAlfred Perlstein static int	coredump(struct thread *);
854d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
869c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
876711f10fSJohn Baldwin static int	issignal(struct thread *p);
884d77a549SAlfred Perlstein static int	sigprop(int sig);
894d77a549SAlfred Perlstein static void	stop(struct proc *);
904093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
91cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
92cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
93cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
944093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
95a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
96a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
97cb679c38SJonathan Lemon 
98cb679c38SJonathan Lemon struct filterops sig_filtops =
99cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
100cb679c38SJonathan Lemon 
10157308494SJoerg Wunsch static int	kern_logsigexit = 1;
1023d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1033d177f46SBill Fumerola     &kern_logsigexit, 0,
1043d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10557308494SJoerg Wunsch 
1062b87b6d4SRobert Watson /*
1072b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1082b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1092b87b6d4SRobert Watson  * in the right situations.
1102b87b6d4SRobert Watson  */
1112b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1122b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1142b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1152b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1162b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1172b87b6d4SRobert Watson 
11822d4b0fbSJohn Polstra int sugid_coredump;
1193d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1203d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
121c87e2930SDavid Greenman 
122e5a28db9SPaul Saab static int	do_coredump = 1;
123e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
124e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
125e5a28db9SPaul Saab 
1262c42a146SMarcel Moolenaar /*
1272c42a146SMarcel Moolenaar  * Signal properties and actions.
1282c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1292c42a146SMarcel Moolenaar  * according to the following properties:
1302c42a146SMarcel Moolenaar  */
1312c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1322c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1332c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1342c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1352c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1362c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1372c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
138da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
139df8bae1dSRodney W. Grimes 
1402c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
141da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
142da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
143da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1462c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
147da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
149da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1502c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1512c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1522c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
153da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
154da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
155da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
156da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
157da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
158da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
159da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
160da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
161da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
162da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
163da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1642c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1652c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
166da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
167da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
168da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
169da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
170da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
171da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1722c42a146SMarcel Moolenaar };
1732c42a146SMarcel Moolenaar 
174fbbeeb6cSBruce Evans /*
175fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
176fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
177fbbeeb6cSBruce Evans  * action, the process stops in issignal().
178e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
179fbbeeb6cSBruce Evans  *
18033510ef1SBruce Evans  * MP SAFE.
181fbbeeb6cSBruce Evans  */
182fbbeeb6cSBruce Evans int
183e602ba25SJulian Elischer cursig(struct thread *td)
184fbbeeb6cSBruce Evans {
185c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
18690af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
187179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1884093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
189fbbeeb6cSBruce Evans }
190fbbeeb6cSBruce Evans 
19179065dbaSBruce Evans /*
19279065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1934093529dSJeff Roberson  * mode.  This must be called whenever a signal is added to td_siglist or
1944093529dSJeff Roberson  * unmasked in td_sigmask.
19579065dbaSBruce Evans  */
19679065dbaSBruce Evans void
1974093529dSJeff Roberson signotify(struct thread *td)
19879065dbaSBruce Evans {
1994093529dSJeff Roberson 	struct proc *p;
2004093529dSJeff Roberson 	sigset_t set;
2014093529dSJeff Roberson 
2024093529dSJeff Roberson 	p = td->td_proc;
20379065dbaSBruce Evans 
20479065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
2054093529dSJeff Roberson 
2064093529dSJeff Roberson 	/*
2074093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
2084093529dSJeff Roberson 	 * previously masked by all threads to our siglist.
2094093529dSJeff Roberson 	 */
2104093529dSJeff Roberson 	set = p->p_siglist;
2114093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
2124093529dSJeff Roberson 	SIGSETNAND(p->p_siglist, set);
2134093529dSJeff Roberson 	SIGSETOR(td->td_siglist, set);
2144093529dSJeff Roberson 
2158b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
21679065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
2174093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
21879065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
21979065dbaSBruce Evans 	}
2208b94a061SJohn Baldwin }
2218b94a061SJohn Baldwin 
2228b94a061SJohn Baldwin int
2238b94a061SJohn Baldwin sigonstack(size_t sp)
2248b94a061SJohn Baldwin {
2258b94a061SJohn Baldwin 	struct proc *p = curthread->td_proc;
2268b94a061SJohn Baldwin 
2278b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2288b94a061SJohn Baldwin 	return ((p->p_flag & P_ALTSTACK) ?
2298b94a061SJohn Baldwin #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2308b94a061SJohn Baldwin 	    ((p->p_sigstk.ss_size == 0) ? (p->p_sigstk.ss_flags & SS_ONSTACK) :
2318b94a061SJohn Baldwin 		((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size))
2328b94a061SJohn Baldwin #else
2338b94a061SJohn Baldwin 	    ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size)
2348b94a061SJohn Baldwin #endif
2358b94a061SJohn Baldwin 	    : 0);
2368b94a061SJohn Baldwin }
23779065dbaSBruce Evans 
2386f841fb7SMarcel Moolenaar static __inline int
2396f841fb7SMarcel Moolenaar sigprop(int sig)
2402c42a146SMarcel Moolenaar {
2416f841fb7SMarcel Moolenaar 
2422c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2432c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2442c42a146SMarcel Moolenaar 	return (0);
245df8bae1dSRodney W. Grimes }
2462c42a146SMarcel Moolenaar 
2474093529dSJeff Roberson int
2486f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2492c42a146SMarcel Moolenaar {
2502c42a146SMarcel Moolenaar 	int i;
2512c42a146SMarcel Moolenaar 
2526f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2532c42a146SMarcel Moolenaar 		if (set->__bits[i])
2542c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
255df8bae1dSRodney W. Grimes 	return (0);
256df8bae1dSRodney W. Grimes }
257df8bae1dSRodney W. Grimes 
2582c42a146SMarcel Moolenaar /*
2598f19eb88SIan Dowse  * kern_sigaction
2602c42a146SMarcel Moolenaar  * sigaction
26123eeeff7SPeter Wemm  * freebsd4_sigaction
2622c42a146SMarcel Moolenaar  * osigaction
26325d6dc06SJohn Baldwin  *
26425d6dc06SJohn Baldwin  * MPSAFE
2652c42a146SMarcel Moolenaar  */
2668f19eb88SIan Dowse int
26723eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2688f19eb88SIan Dowse 	struct thread *td;
2692c42a146SMarcel Moolenaar 	register int sig;
2702c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
27123eeeff7SPeter Wemm 	int flags;
272df8bae1dSRodney W. Grimes {
27390af4afaSJohn Baldwin 	struct sigacts *ps;
2744093529dSJeff Roberson 	struct thread *td0;
2758f19eb88SIan Dowse 	struct proc *p = td->td_proc;
276df8bae1dSRodney W. Grimes 
2772899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2782c42a146SMarcel Moolenaar 		return (EINVAL);
2792c42a146SMarcel Moolenaar 
280628d2653SJohn Baldwin 	PROC_LOCK(p);
281628d2653SJohn Baldwin 	ps = p->p_sigacts;
28290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2832c42a146SMarcel Moolenaar 	if (oact) {
2842c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2852c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2862c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2872c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2882c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2892c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2902c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2912c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2922c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2932c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2942c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2952c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2962c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
29790af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
2982c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
29990af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
3002c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
3012c42a146SMarcel Moolenaar 	}
3022c42a146SMarcel Moolenaar 	if (act) {
3032c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
304628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
30590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
306628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3072c42a146SMarcel Moolenaar 			return (EINVAL);
308628d2653SJohn Baldwin 		}
3092c42a146SMarcel Moolenaar 
310df8bae1dSRodney W. Grimes 		/*
311df8bae1dSRodney W. Grimes 		 * Change setting atomically.
312df8bae1dSRodney W. Grimes 		 */
3132c42a146SMarcel Moolenaar 
3142c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
3152c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
3162c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
317aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
318aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
31980f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
32080f42b55SIan Dowse 		} else {
32180f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3222c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3232c42a146SMarcel Moolenaar 		}
3242c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3252c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
326df8bae1dSRodney W. Grimes 		else
3272c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3282c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3292c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
330df8bae1dSRodney W. Grimes 		else
3312c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3322c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3332c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3341e41c1b5SSteven Wallace 		else
3352c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3362c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3372c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
338289ccde0SPeter Wemm 		else
3392c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
340df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3412c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3422c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
343df8bae1dSRodney W. Grimes 		else
3448c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
345df8bae1dSRodney W. Grimes #endif
3462c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3472c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
34890af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
3496626c604SJulian Elischer 			else
35090af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
351ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
352245f17d4SJoerg Wunsch 				/*
3532c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3542c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3552c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3562c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
357245f17d4SJoerg Wunsch 				 */
358245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
35990af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
3606626c604SJulian Elischer 				else
36190af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
362245f17d4SJoerg Wunsch 			} else
36390af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
364ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
36590af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
366ba1551caSIan Dowse 			else
36790af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
368df8bae1dSRodney W. Grimes 		}
369df8bae1dSRodney W. Grimes 		/*
37090af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
3712c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
37290af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
3732c42a146SMarcel Moolenaar 		 * have to restart the process.
374df8bae1dSRodney W. Grimes 		 */
3752c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3762c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3772c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3782c42a146SMarcel Moolenaar 			/* never to be seen again */
3791d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
3804093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3814093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
3822c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3832c42a146SMarcel Moolenaar 				/* easier in psignal */
38490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
38590af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
386645682fdSLuoqi Chen 		} else {
38790af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
3882c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
38990af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
3902c42a146SMarcel Moolenaar 			else
39190af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
3922c42a146SMarcel Moolenaar 		}
39323eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
39423eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
39523eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
39623eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
39723eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
39823eeeff7SPeter Wemm 		else
39923eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
40023eeeff7SPeter Wemm #endif
401e8ebc08fSPeter Wemm #ifdef COMPAT_43
402645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
40323eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
40423eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
405645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
406645682fdSLuoqi Chen 		else
407645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
408e8ebc08fSPeter Wemm #endif
409df8bae1dSRodney W. Grimes 	}
41090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
411628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4122c42a146SMarcel Moolenaar 	return (0);
4132c42a146SMarcel Moolenaar }
4142c42a146SMarcel Moolenaar 
4152c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4162c42a146SMarcel Moolenaar struct sigaction_args {
4172c42a146SMarcel Moolenaar 	int	sig;
4182c42a146SMarcel Moolenaar 	struct	sigaction *act;
4192c42a146SMarcel Moolenaar 	struct	sigaction *oact;
4202c42a146SMarcel Moolenaar };
4212c42a146SMarcel Moolenaar #endif
422fb99ab88SMatthew Dillon /*
423fb99ab88SMatthew Dillon  * MPSAFE
424fb99ab88SMatthew Dillon  */
4252c42a146SMarcel Moolenaar int
426b40ce416SJulian Elischer sigaction(td, uap)
427b40ce416SJulian Elischer 	struct thread *td;
4282c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4292c42a146SMarcel Moolenaar {
4302c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4312c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4322c42a146SMarcel Moolenaar 	int error;
4332c42a146SMarcel Moolenaar 
4346f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4356f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4362c42a146SMarcel Moolenaar 	if (actp) {
4376f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4382c42a146SMarcel Moolenaar 		if (error)
43944443757STim J. Robbins 			return (error);
4402c42a146SMarcel Moolenaar 	}
4418f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
44225d6dc06SJohn Baldwin 	if (oactp && !error)
4436f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4442c42a146SMarcel Moolenaar 	return (error);
4452c42a146SMarcel Moolenaar }
4462c42a146SMarcel Moolenaar 
44723eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
44823eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
44923eeeff7SPeter Wemm struct freebsd4_sigaction_args {
45023eeeff7SPeter Wemm 	int	sig;
45123eeeff7SPeter Wemm 	struct	sigaction *act;
45223eeeff7SPeter Wemm 	struct	sigaction *oact;
45323eeeff7SPeter Wemm };
45423eeeff7SPeter Wemm #endif
45523eeeff7SPeter Wemm /*
45623eeeff7SPeter Wemm  * MPSAFE
45723eeeff7SPeter Wemm  */
45823eeeff7SPeter Wemm int
45923eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
46023eeeff7SPeter Wemm 	struct thread *td;
46123eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
46223eeeff7SPeter Wemm {
46323eeeff7SPeter Wemm 	struct sigaction act, oact;
46423eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
46523eeeff7SPeter Wemm 	int error;
46623eeeff7SPeter Wemm 
46723eeeff7SPeter Wemm 
46823eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
46923eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
47023eeeff7SPeter Wemm 	if (actp) {
47123eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
47223eeeff7SPeter Wemm 		if (error)
47344443757STim J. Robbins 			return (error);
47423eeeff7SPeter Wemm 	}
47523eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
476a14e1189SJohn Baldwin 	if (oactp && !error)
47723eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
47823eeeff7SPeter Wemm 	return (error);
47923eeeff7SPeter Wemm }
48023eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
48123eeeff7SPeter Wemm 
48231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4832c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4842c42a146SMarcel Moolenaar struct osigaction_args {
4852c42a146SMarcel Moolenaar 	int	signum;
4862c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4872c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4882c42a146SMarcel Moolenaar };
4892c42a146SMarcel Moolenaar #endif
490fb99ab88SMatthew Dillon /*
491fb99ab88SMatthew Dillon  * MPSAFE
492fb99ab88SMatthew Dillon  */
4932c42a146SMarcel Moolenaar int
494b40ce416SJulian Elischer osigaction(td, uap)
495b40ce416SJulian Elischer 	struct thread *td;
4962c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4972c42a146SMarcel Moolenaar {
4982c42a146SMarcel Moolenaar 	struct osigaction sa;
4992c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5002c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5012c42a146SMarcel Moolenaar 	int error;
5022c42a146SMarcel Moolenaar 
5036f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5046f841fb7SMarcel Moolenaar 		return (EINVAL);
505fb99ab88SMatthew Dillon 
5066f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
5076f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
508fb99ab88SMatthew Dillon 
5092c42a146SMarcel Moolenaar 	if (nsap) {
5106f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
5112c42a146SMarcel Moolenaar 		if (error)
51244443757STim J. Robbins 			return (error);
5132c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
5142c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
5152c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5162c42a146SMarcel Moolenaar 	}
51723eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5182c42a146SMarcel Moolenaar 	if (osap && !error) {
5192c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5202c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5212c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5226f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5232c42a146SMarcel Moolenaar 	}
5242c42a146SMarcel Moolenaar 	return (error);
5252c42a146SMarcel Moolenaar }
52623eeeff7SPeter Wemm 
52723eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
52823eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
52923eeeff7SPeter Wemm int
53023eeeff7SPeter Wemm osigreturn(td, uap)
53123eeeff7SPeter Wemm 	struct thread *td;
53223eeeff7SPeter Wemm 	struct osigreturn_args *uap;
53323eeeff7SPeter Wemm {
53423eeeff7SPeter Wemm 
53523eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
53623eeeff7SPeter Wemm }
53723eeeff7SPeter Wemm #endif
53831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
539df8bae1dSRodney W. Grimes 
540df8bae1dSRodney W. Grimes /*
541df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
542df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
543df8bae1dSRodney W. Grimes  */
544df8bae1dSRodney W. Grimes void
545df8bae1dSRodney W. Grimes siginit(p)
546df8bae1dSRodney W. Grimes 	struct proc *p;
547df8bae1dSRodney W. Grimes {
548df8bae1dSRodney W. Grimes 	register int i;
54990af4afaSJohn Baldwin 	struct sigacts *ps;
550df8bae1dSRodney W. Grimes 
551628d2653SJohn Baldwin 	PROC_LOCK(p);
55290af4afaSJohn Baldwin 	ps = p->p_sigacts;
55390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
5542c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5552c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
55690af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
55790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
558628d2653SJohn Baldwin 	PROC_UNLOCK(p);
559df8bae1dSRodney W. Grimes }
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes /*
562df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
563df8bae1dSRodney W. Grimes  */
564df8bae1dSRodney W. Grimes void
565df8bae1dSRodney W. Grimes execsigs(p)
566df8bae1dSRodney W. Grimes 	register struct proc *p;
567df8bae1dSRodney W. Grimes {
568628d2653SJohn Baldwin 	register struct sigacts *ps;
5692c42a146SMarcel Moolenaar 	register int sig;
570df8bae1dSRodney W. Grimes 
571df8bae1dSRodney W. Grimes 	/*
572df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5734093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
574df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
575df8bae1dSRodney W. Grimes 	 */
5769b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
577628d2653SJohn Baldwin 	ps = p->p_sigacts;
57890af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
57990af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
58090af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
58190af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
5822c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5832c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
58490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
5851d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
5864093529dSJeff Roberson 			/*
5874093529dSJeff Roberson 			 * There is only one thread at this point.
5884093529dSJeff Roberson 			 */
5894093529dSJeff Roberson 			SIGDELSET(FIRST_THREAD_IN_PROC(p)->td_siglist, sig);
590df8bae1dSRodney W. Grimes 		}
5912c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
592df8bae1dSRodney W. Grimes 	}
593df8bae1dSRodney W. Grimes 	/*
5944093529dSJeff Roberson 	 * Clear out the td's sigmask.  Normal processes use the proc sigmask.
5954093529dSJeff Roberson 	 */
5964093529dSJeff Roberson 	SIGEMPTYSET(FIRST_THREAD_IN_PROC(p)->td_sigmask);
5974093529dSJeff Roberson 	/*
598df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
599df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
600df8bae1dSRodney W. Grimes 	 */
601645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
602645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
603645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
6043b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
60580e907a1SPeter Wemm 	/*
60680e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
60780e907a1SPeter Wemm 	 */
60890af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
609c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
610c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
61190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
612df8bae1dSRodney W. Grimes }
613df8bae1dSRodney W. Grimes 
614df8bae1dSRodney W. Grimes /*
615e77daab1SJohn Baldwin  * kern_sigprocmask()
6167c8fdcbdSMatthew Dillon  *
617628d2653SJohn Baldwin  *	Manipulate signal mask.
618df8bae1dSRodney W. Grimes  */
619e77daab1SJohn Baldwin int
620e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
6214093529dSJeff Roberson 	struct thread *td;
6222c42a146SMarcel Moolenaar 	int how;
6232c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
624645682fdSLuoqi Chen 	int old;
6252c42a146SMarcel Moolenaar {
6262c42a146SMarcel Moolenaar 	int error;
6272c42a146SMarcel Moolenaar 
6284093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6292c42a146SMarcel Moolenaar 	if (oset != NULL)
6304093529dSJeff Roberson 		*oset = td->td_sigmask;
6312c42a146SMarcel Moolenaar 
6322c42a146SMarcel Moolenaar 	error = 0;
6332c42a146SMarcel Moolenaar 	if (set != NULL) {
6342c42a146SMarcel Moolenaar 		switch (how) {
6352c42a146SMarcel Moolenaar 		case SIG_BLOCK:
636645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6374093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6382c42a146SMarcel Moolenaar 			break;
6392c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6404093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6414093529dSJeff Roberson 			signotify(td);
6422c42a146SMarcel Moolenaar 			break;
6432c42a146SMarcel Moolenaar 		case SIG_SETMASK:
644645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
645645682fdSLuoqi Chen 			if (old)
6464093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
647645682fdSLuoqi Chen 			else
6484093529dSJeff Roberson 				td->td_sigmask = *set;
6494093529dSJeff Roberson 			signotify(td);
6502c42a146SMarcel Moolenaar 			break;
6512c42a146SMarcel Moolenaar 		default:
6522c42a146SMarcel Moolenaar 			error = EINVAL;
6532c42a146SMarcel Moolenaar 			break;
6542c42a146SMarcel Moolenaar 		}
6552c42a146SMarcel Moolenaar 	}
6564093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6572c42a146SMarcel Moolenaar 	return (error);
6582c42a146SMarcel Moolenaar }
6592c42a146SMarcel Moolenaar 
6607c8fdcbdSMatthew Dillon /*
661e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
6627c8fdcbdSMatthew Dillon  */
6637c8fdcbdSMatthew Dillon 
664d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
665df8bae1dSRodney W. Grimes struct sigprocmask_args {
666df8bae1dSRodney W. Grimes 	int	how;
6672c42a146SMarcel Moolenaar 	const sigset_t *set;
6682c42a146SMarcel Moolenaar 	sigset_t *oset;
669df8bae1dSRodney W. Grimes };
670d2d3e875SBruce Evans #endif
67126f9a767SRodney W. Grimes int
672b40ce416SJulian Elischer sigprocmask(td, uap)
673b40ce416SJulian Elischer 	register struct thread *td;
674df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
675df8bae1dSRodney W. Grimes {
6762c42a146SMarcel Moolenaar 	sigset_t set, oset;
6772c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6782c42a146SMarcel Moolenaar 	int error;
679df8bae1dSRodney W. Grimes 
6806f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6816f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6822c42a146SMarcel Moolenaar 	if (setp) {
6836f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6842c42a146SMarcel Moolenaar 		if (error)
6852c42a146SMarcel Moolenaar 			return (error);
686df8bae1dSRodney W. Grimes 	}
687e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
6882c42a146SMarcel Moolenaar 	if (osetp && !error) {
6896f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
6902c42a146SMarcel Moolenaar 	}
6912c42a146SMarcel Moolenaar 	return (error);
6922c42a146SMarcel Moolenaar }
6932c42a146SMarcel Moolenaar 
69431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6957c8fdcbdSMatthew Dillon /*
6967c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
6977c8fdcbdSMatthew Dillon  */
6982c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6992c42a146SMarcel Moolenaar struct osigprocmask_args {
7002c42a146SMarcel Moolenaar 	int	how;
7012c42a146SMarcel Moolenaar 	osigset_t mask;
7022c42a146SMarcel Moolenaar };
7032c42a146SMarcel Moolenaar #endif
7042c42a146SMarcel Moolenaar int
705b40ce416SJulian Elischer osigprocmask(td, uap)
706b40ce416SJulian Elischer 	register struct thread *td;
7072c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
7082c42a146SMarcel Moolenaar {
7092c42a146SMarcel Moolenaar 	sigset_t set, oset;
7102c42a146SMarcel Moolenaar 	int error;
7112c42a146SMarcel Moolenaar 
7122c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
713e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
714b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
715df8bae1dSRodney W. Grimes 	return (error);
716df8bae1dSRodney W. Grimes }
71731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
718df8bae1dSRodney W. Grimes 
719d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
720df8bae1dSRodney W. Grimes struct sigpending_args {
7212c42a146SMarcel Moolenaar 	sigset_t	*set;
722df8bae1dSRodney W. Grimes };
723d2d3e875SBruce Evans #endif
724fb99ab88SMatthew Dillon /*
725fb99ab88SMatthew Dillon  * MPSAFE
726fb99ab88SMatthew Dillon  */
72726f9a767SRodney W. Grimes int
728a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
729a447cd8bSJeff Roberson {
730a447cd8bSJeff Roberson 	siginfo_t info;
731a447cd8bSJeff Roberson 	sigset_t set;
732a447cd8bSJeff Roberson 	int error;
733a447cd8bSJeff Roberson 
734a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
735a447cd8bSJeff Roberson 	if (error)
736a447cd8bSJeff Roberson 		return (error);
737a447cd8bSJeff Roberson 
738a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
739a447cd8bSJeff Roberson 	if (error)
740a447cd8bSJeff Roberson 		return (error);
741a447cd8bSJeff Roberson 
742a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
743a447cd8bSJeff Roberson 	/* Repost if we got an error. */
74418440c7fSJohn Baldwin 	if (error && info.si_signo) {
74518440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
746a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
74718440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
74818440c7fSJohn Baldwin 	}
749a447cd8bSJeff Roberson 	return (error);
750a447cd8bSJeff Roberson }
751a447cd8bSJeff Roberson /*
752a447cd8bSJeff Roberson  * MPSAFE
753a447cd8bSJeff Roberson  */
754a447cd8bSJeff Roberson int
755a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
756a447cd8bSJeff Roberson {
757a447cd8bSJeff Roberson 	struct timespec ts;
758a447cd8bSJeff Roberson 	struct timespec *timeout;
759a447cd8bSJeff Roberson 	sigset_t set;
760a447cd8bSJeff Roberson 	siginfo_t info;
761a447cd8bSJeff Roberson 	int error;
762a447cd8bSJeff Roberson 
763a447cd8bSJeff Roberson 	if (uap->timeout) {
764a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
765a447cd8bSJeff Roberson 		if (error)
766a447cd8bSJeff Roberson 			return (error);
767a447cd8bSJeff Roberson 
768a447cd8bSJeff Roberson 		timeout = &ts;
769a447cd8bSJeff Roberson 	} else
770a447cd8bSJeff Roberson 		timeout = NULL;
771a447cd8bSJeff Roberson 
772a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
773a447cd8bSJeff Roberson 	if (error)
774a447cd8bSJeff Roberson 		return (error);
775a447cd8bSJeff Roberson 
776a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
777a447cd8bSJeff Roberson 	if (error)
778a447cd8bSJeff Roberson 		return (error);
779418228dfSDavid Xu 	if (uap->info)
780a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
781a447cd8bSJeff Roberson 	/* Repost if we got an error. */
78218440c7fSJohn Baldwin 	if (error && info.si_signo) {
78318440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
784a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
78518440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
786418228dfSDavid Xu 	} else {
787418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
78818440c7fSJohn Baldwin 	}
789a447cd8bSJeff Roberson 	return (error);
790a447cd8bSJeff Roberson }
791a447cd8bSJeff Roberson 
792a447cd8bSJeff Roberson /*
793a447cd8bSJeff Roberson  * MPSAFE
794a447cd8bSJeff Roberson  */
795a447cd8bSJeff Roberson int
796a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
797a447cd8bSJeff Roberson {
798a447cd8bSJeff Roberson 	siginfo_t info;
799a447cd8bSJeff Roberson 	sigset_t set;
800a447cd8bSJeff Roberson 	int error;
801a447cd8bSJeff Roberson 
802a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
803a447cd8bSJeff Roberson 	if (error)
804a447cd8bSJeff Roberson 		return (error);
805a447cd8bSJeff Roberson 
806a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
807a447cd8bSJeff Roberson 	if (error)
808a447cd8bSJeff Roberson 		return (error);
809a447cd8bSJeff Roberson 
810418228dfSDavid Xu 	if (uap->info)
811a447cd8bSJeff Roberson 		error = copyout(&info, uap->info, sizeof(info));
812a447cd8bSJeff Roberson 	/* Repost if we got an error. */
81318440c7fSJohn Baldwin 	if (error && info.si_signo) {
81418440c7fSJohn Baldwin 		PROC_LOCK(td->td_proc);
815a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
81618440c7fSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
817418228dfSDavid Xu 	} else {
818418228dfSDavid Xu 		td->td_retval[0] = info.si_signo;
81918440c7fSJohn Baldwin 	}
820a447cd8bSJeff Roberson 	return (error);
821a447cd8bSJeff Roberson }
822a447cd8bSJeff Roberson 
823a447cd8bSJeff Roberson static int
824a447cd8bSJeff Roberson kern_sigtimedwait(struct thread *td, sigset_t set, siginfo_t *info,
825a447cd8bSJeff Roberson     struct timespec *timeout)
826a447cd8bSJeff Roberson {
827a447cd8bSJeff Roberson 	register struct sigacts *ps;
828a447cd8bSJeff Roberson 	sigset_t oldmask;
829a447cd8bSJeff Roberson 	struct proc *p;
830a447cd8bSJeff Roberson 	int error;
831a447cd8bSJeff Roberson 	int sig;
832a447cd8bSJeff Roberson 	int hz;
833a447cd8bSJeff Roberson 
834a447cd8bSJeff Roberson 	p = td->td_proc;
835a447cd8bSJeff Roberson 	error = 0;
836a447cd8bSJeff Roberson 	sig = 0;
837a447cd8bSJeff Roberson 	SIG_CANTMASK(set);
838a447cd8bSJeff Roberson 
839a447cd8bSJeff Roberson 	PROC_LOCK(p);
840a447cd8bSJeff Roberson 	ps = p->p_sigacts;
841a447cd8bSJeff Roberson 	oldmask = td->td_sigmask;
842418228dfSDavid Xu 	SIGFILLSET(td->td_sigmask);
843418228dfSDavid Xu 	SIG_CANTMASK(td->td_sigmask);
844418228dfSDavid Xu 	SIGSETNAND(td->td_sigmask, set);
845a447cd8bSJeff Roberson 	signotify(td);
846a447cd8bSJeff Roberson 
84790af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
848a447cd8bSJeff Roberson 	sig = cursig(td);
849a447cd8bSJeff Roberson 	if (sig)
850a447cd8bSJeff Roberson 		goto out;
851a447cd8bSJeff Roberson 
852a447cd8bSJeff Roberson 	/*
853a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
854a447cd8bSJeff Roberson 	 * signals.
855a447cd8bSJeff Roberson 	 */
856a447cd8bSJeff Roberson 	if (timeout) {
857a447cd8bSJeff Roberson 		struct timeval tv;
858a447cd8bSJeff Roberson 
859a447cd8bSJeff Roberson 		if (timeout->tv_nsec > 1000000000) {
860a447cd8bSJeff Roberson 			error = EINVAL;
861a447cd8bSJeff Roberson 			goto out;
862a447cd8bSJeff Roberson 		}
863a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
864a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
865a447cd8bSJeff Roberson 	} else
866a447cd8bSJeff Roberson 		hz = 0;
867a447cd8bSJeff Roberson 
86890af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
869a447cd8bSJeff Roberson 	error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", hz);
87090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
871a447cd8bSJeff Roberson 	if (error == EINTR)
872a447cd8bSJeff Roberson 		error = 0;
873a447cd8bSJeff Roberson 	else if (error)
874a447cd8bSJeff Roberson 		goto out;
875a447cd8bSJeff Roberson 
876a447cd8bSJeff Roberson 	sig = cursig(td);
877a447cd8bSJeff Roberson out:
878a447cd8bSJeff Roberson 	td->td_sigmask = oldmask;
879a447cd8bSJeff Roberson 	if (sig) {
880a447cd8bSJeff Roberson 		sig_t action;
881a447cd8bSJeff Roberson 
882a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
88390af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
884a447cd8bSJeff Roberson #ifdef KTRACE
885a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
8865e26dcb5SJohn Baldwin 			ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
887a447cd8bSJeff Roberson 			    &td->td_oldsigmask : &td->td_sigmask, 0);
888a447cd8bSJeff Roberson #endif
889a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
890a447cd8bSJeff Roberson 
891a447cd8bSJeff Roberson 		if (action == SIG_DFL)
892a447cd8bSJeff Roberson 			sigexit(td, sig);
893a447cd8bSJeff Roberson 			/* NOTREACHED */
894a447cd8bSJeff Roberson 
895a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
896a447cd8bSJeff Roberson 		info->si_signo = sig;
897a447cd8bSJeff Roberson 		info->si_code = 0;
89890af4afaSJohn Baldwin 	} else
89990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
900a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
901a447cd8bSJeff Roberson 	return (error);
902a447cd8bSJeff Roberson }
903a447cd8bSJeff Roberson 
904a447cd8bSJeff Roberson /*
905a447cd8bSJeff Roberson  * MPSAFE
906a447cd8bSJeff Roberson  */
907a447cd8bSJeff Roberson int
908b40ce416SJulian Elischer sigpending(td, uap)
909b40ce416SJulian Elischer 	struct thread *td;
910df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
911df8bae1dSRodney W. Grimes {
912b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
913628d2653SJohn Baldwin 	sigset_t siglist;
914df8bae1dSRodney W. Grimes 
915628d2653SJohn Baldwin 	PROC_LOCK(p);
9161d9c5696SJuli Mallett 	siglist = p->p_siglist;
9174093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
918628d2653SJohn Baldwin 	PROC_UNLOCK(p);
91948e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
9202c42a146SMarcel Moolenaar }
9212c42a146SMarcel Moolenaar 
92231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9232c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9242c42a146SMarcel Moolenaar struct osigpending_args {
9252c42a146SMarcel Moolenaar 	int	dummy;
9262c42a146SMarcel Moolenaar };
9272c42a146SMarcel Moolenaar #endif
928fb99ab88SMatthew Dillon /*
929fb99ab88SMatthew Dillon  * MPSAFE
930fb99ab88SMatthew Dillon  */
9312c42a146SMarcel Moolenaar int
932b40ce416SJulian Elischer osigpending(td, uap)
933b40ce416SJulian Elischer 	struct thread *td;
9342c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9352c42a146SMarcel Moolenaar {
936b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9374093529dSJeff Roberson 	sigset_t siglist;
938b40ce416SJulian Elischer 
939628d2653SJohn Baldwin 	PROC_LOCK(p);
9404093529dSJeff Roberson 	siglist = p->p_siglist;
9414093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
942628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9439d8643ecSJohn Baldwin 	SIG2OSIG(siglist, td->td_retval[0]);
944df8bae1dSRodney W. Grimes 	return (0);
945df8bae1dSRodney W. Grimes }
94631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
949df8bae1dSRodney W. Grimes /*
950df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
951df8bae1dSRodney W. Grimes  */
952d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
953df8bae1dSRodney W. Grimes struct osigvec_args {
954df8bae1dSRodney W. Grimes 	int	signum;
955df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
956df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
957df8bae1dSRodney W. Grimes };
958d2d3e875SBruce Evans #endif
959fb99ab88SMatthew Dillon /*
960fb99ab88SMatthew Dillon  * MPSAFE
961fb99ab88SMatthew Dillon  */
962df8bae1dSRodney W. Grimes /* ARGSUSED */
96326f9a767SRodney W. Grimes int
964b40ce416SJulian Elischer osigvec(td, uap)
965b40ce416SJulian Elischer 	struct thread *td;
966df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
967df8bae1dSRodney W. Grimes {
968df8bae1dSRodney W. Grimes 	struct sigvec vec;
9692c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
9702c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
9712c42a146SMarcel Moolenaar 	int error;
972df8bae1dSRodney W. Grimes 
9736f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
9746f841fb7SMarcel Moolenaar 		return (EINVAL);
9756f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
9766f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
9772c42a146SMarcel Moolenaar 	if (nsap) {
9786f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
9792c42a146SMarcel Moolenaar 		if (error)
980df8bae1dSRodney W. Grimes 			return (error);
9812c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
9822c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
9832c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
9842c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
985df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
9862c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
987df8bae1dSRodney W. Grimes #endif
988df8bae1dSRodney W. Grimes 	}
9895edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
9902c42a146SMarcel Moolenaar 	if (osap && !error) {
9912c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
9922c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
9932c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
9942c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
9952c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
9962c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
9972c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
9982c42a146SMarcel Moolenaar #endif
9996f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
10002c42a146SMarcel Moolenaar 	}
10012c42a146SMarcel Moolenaar 	return (error);
1002df8bae1dSRodney W. Grimes }
1003df8bae1dSRodney W. Grimes 
1004d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1005df8bae1dSRodney W. Grimes struct osigblock_args {
1006df8bae1dSRodney W. Grimes 	int	mask;
1007df8bae1dSRodney W. Grimes };
1008d2d3e875SBruce Evans #endif
1009fb99ab88SMatthew Dillon /*
1010fb99ab88SMatthew Dillon  * MPSAFE
1011fb99ab88SMatthew Dillon  */
101226f9a767SRodney W. Grimes int
1013b40ce416SJulian Elischer osigblock(td, uap)
1014b40ce416SJulian Elischer 	register struct thread *td;
1015df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1016df8bae1dSRodney W. Grimes {
1017b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10182c42a146SMarcel Moolenaar 	sigset_t set;
1019df8bae1dSRodney W. Grimes 
10202c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10212c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1022628d2653SJohn Baldwin 	PROC_LOCK(p);
10234093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10244093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1025628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1026df8bae1dSRodney W. Grimes 	return (0);
1027df8bae1dSRodney W. Grimes }
1028df8bae1dSRodney W. Grimes 
1029d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1030df8bae1dSRodney W. Grimes struct osigsetmask_args {
1031df8bae1dSRodney W. Grimes 	int	mask;
1032df8bae1dSRodney W. Grimes };
1033d2d3e875SBruce Evans #endif
1034fb99ab88SMatthew Dillon /*
1035fb99ab88SMatthew Dillon  * MPSAFE
1036fb99ab88SMatthew Dillon  */
103726f9a767SRodney W. Grimes int
1038b40ce416SJulian Elischer osigsetmask(td, uap)
1039b40ce416SJulian Elischer 	struct thread *td;
1040df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1041df8bae1dSRodney W. Grimes {
1042b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10432c42a146SMarcel Moolenaar 	sigset_t set;
1044df8bae1dSRodney W. Grimes 
10452c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10462c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1047628d2653SJohn Baldwin 	PROC_LOCK(p);
10484093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10494093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10504093529dSJeff Roberson 	signotify(td);
1051628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1052df8bae1dSRodney W. Grimes 	return (0);
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes /*
1057df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
1058df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
1059df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
1060b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1061b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1062b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1063df8bae1dSRodney W. Grimes  */
1064d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1065df8bae1dSRodney W. Grimes struct sigsuspend_args {
10662c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1067df8bae1dSRodney W. Grimes };
1068d2d3e875SBruce Evans #endif
1069fb99ab88SMatthew Dillon /*
1070fb99ab88SMatthew Dillon  * MPSAFE
1071fb99ab88SMatthew Dillon  */
1072df8bae1dSRodney W. Grimes /* ARGSUSED */
107326f9a767SRodney W. Grimes int
1074b40ce416SJulian Elischer sigsuspend(td, uap)
1075b40ce416SJulian Elischer 	struct thread *td;
1076df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1077df8bae1dSRodney W. Grimes {
10782c42a146SMarcel Moolenaar 	sigset_t mask;
10792c42a146SMarcel Moolenaar 	int error;
10802c42a146SMarcel Moolenaar 
10816f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
10822c42a146SMarcel Moolenaar 	if (error)
10832c42a146SMarcel Moolenaar 		return (error);
10848f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
10858f19eb88SIan Dowse }
10868f19eb88SIan Dowse 
10878f19eb88SIan Dowse int
10888f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
10898f19eb88SIan Dowse {
10908f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1091df8bae1dSRodney W. Grimes 
1092df8bae1dSRodney W. Grimes 	/*
1093645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1094df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1095df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1096df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1097df8bae1dSRodney W. Grimes 	 * to indicate this.
1098df8bae1dSRodney W. Grimes 	 */
1099628d2653SJohn Baldwin 	PROC_LOCK(p);
11004093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11015e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1102645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11034093529dSJeff Roberson 	td->td_sigmask = mask;
11044093529dSJeff Roberson 	signotify(td);
1105b1bf1c3aSJohn Baldwin 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
11062c42a146SMarcel Moolenaar 		/* void */;
1107628d2653SJohn Baldwin 	PROC_UNLOCK(p);
11082c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
11092c42a146SMarcel Moolenaar 	return (EINTR);
11102c42a146SMarcel Moolenaar }
11112c42a146SMarcel Moolenaar 
111231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
11132c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11142c42a146SMarcel Moolenaar struct osigsuspend_args {
11152c42a146SMarcel Moolenaar 	osigset_t mask;
11162c42a146SMarcel Moolenaar };
11172c42a146SMarcel Moolenaar #endif
1118fb99ab88SMatthew Dillon /*
1119fb99ab88SMatthew Dillon  * MPSAFE
1120fb99ab88SMatthew Dillon  */
11212c42a146SMarcel Moolenaar /* ARGSUSED */
11222c42a146SMarcel Moolenaar int
1123b40ce416SJulian Elischer osigsuspend(td, uap)
1124b40ce416SJulian Elischer 	struct thread *td;
11252c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11262c42a146SMarcel Moolenaar {
1127b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1128645682fdSLuoqi Chen 	sigset_t mask;
11292c42a146SMarcel Moolenaar 
1130628d2653SJohn Baldwin 	PROC_LOCK(p);
11314093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11325e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1133645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1134645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11354093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11364093529dSJeff Roberson 	signotify(td);
1137b1bf1c3aSJohn Baldwin 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1138df8bae1dSRodney W. Grimes 		/* void */;
1139628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1140df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1141df8bae1dSRodney W. Grimes 	return (EINTR);
1142df8bae1dSRodney W. Grimes }
114331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1144df8bae1dSRodney W. Grimes 
1145df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1146d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1147df8bae1dSRodney W. Grimes struct osigstack_args {
1148df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1149df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1150df8bae1dSRodney W. Grimes };
1151d2d3e875SBruce Evans #endif
1152fb99ab88SMatthew Dillon /*
1153fb99ab88SMatthew Dillon  * MPSAFE
1154fb99ab88SMatthew Dillon  */
1155df8bae1dSRodney W. Grimes /* ARGSUSED */
115626f9a767SRodney W. Grimes int
1157b40ce416SJulian Elischer osigstack(td, uap)
1158b40ce416SJulian Elischer 	struct thread *td;
1159df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1160df8bae1dSRodney W. Grimes {
1161b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
11625afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1163fb99ab88SMatthew Dillon 	int error = 0;
1164fb99ab88SMatthew Dillon 
1165d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
11665afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
11675afe0c99SJohn Baldwin 		if (error)
11685afe0c99SJohn Baldwin 			return (error);
1169df8bae1dSRodney W. Grimes 	}
11705afe0c99SJohn Baldwin 	PROC_LOCK(p);
11715afe0c99SJohn Baldwin 	oss.ss_sp = p->p_sigstk.ss_sp;
11725afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
11735afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
11745afe0c99SJohn Baldwin 		p->p_sigstk.ss_sp = nss.ss_sp;
11755afe0c99SJohn Baldwin 		p->p_sigstk.ss_size = 0;
11765afe0c99SJohn Baldwin 		p->p_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
11775afe0c99SJohn Baldwin 		p->p_flag |= P_ALTSTACK;
11785afe0c99SJohn Baldwin 	}
11795afe0c99SJohn Baldwin 	PROC_UNLOCK(p);
11805afe0c99SJohn Baldwin 	if (uap->oss != NULL)
11815afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
11825afe0c99SJohn Baldwin 
1183fb99ab88SMatthew Dillon 	return (error);
1184df8bae1dSRodney W. Grimes }
1185df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1186df8bae1dSRodney W. Grimes 
1187d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1188df8bae1dSRodney W. Grimes struct sigaltstack_args {
11892c42a146SMarcel Moolenaar 	stack_t	*ss;
11902c42a146SMarcel Moolenaar 	stack_t	*oss;
1191df8bae1dSRodney W. Grimes };
1192d2d3e875SBruce Evans #endif
1193fb99ab88SMatthew Dillon /*
1194fb99ab88SMatthew Dillon  * MPSAFE
1195fb99ab88SMatthew Dillon  */
1196df8bae1dSRodney W. Grimes /* ARGSUSED */
119726f9a767SRodney W. Grimes int
1198b40ce416SJulian Elischer sigaltstack(td, uap)
1199b40ce416SJulian Elischer 	struct thread *td;
1200df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1201df8bae1dSRodney W. Grimes {
12028f19eb88SIan Dowse 	stack_t ss, oss;
12038f19eb88SIan Dowse 	int error;
12048f19eb88SIan Dowse 
12058f19eb88SIan Dowse 	if (uap->ss != NULL) {
12068f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
12078f19eb88SIan Dowse 		if (error)
12088f19eb88SIan Dowse 			return (error);
12098f19eb88SIan Dowse 	}
12108f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12118f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12128f19eb88SIan Dowse 	if (error)
12138f19eb88SIan Dowse 		return (error);
12148f19eb88SIan Dowse 	if (uap->oss != NULL)
12158f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12168f19eb88SIan Dowse 	return (error);
12178f19eb88SIan Dowse }
12188f19eb88SIan Dowse 
12198f19eb88SIan Dowse int
12208f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12218f19eb88SIan Dowse {
1222b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1223fb99ab88SMatthew Dillon 	int oonstack;
1224fb99ab88SMatthew Dillon 
122506ce69a7SDavid Xu 	PROC_LOCK(p);
1226b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1227d034d459SMarcel Moolenaar 
12288f19eb88SIan Dowse 	if (oss != NULL) {
12298f19eb88SIan Dowse 		*oss = p->p_sigstk;
12308f19eb88SIan Dowse 		oss->ss_flags = (p->p_flag & P_ALTSTACK)
1231d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1232df8bae1dSRodney W. Grimes 	}
1233d034d459SMarcel Moolenaar 
12348f19eb88SIan Dowse 	if (ss != NULL) {
1235fb99ab88SMatthew Dillon 		if (oonstack) {
1236cf60731bSJohn Baldwin 			PROC_UNLOCK(p);
1237cf60731bSJohn Baldwin 			return (EPERM);
1238fb99ab88SMatthew Dillon 		}
12398f19eb88SIan Dowse 		if ((ss->ss_flags & ~SS_DISABLE) != 0) {
1240cf60731bSJohn Baldwin 			PROC_UNLOCK(p);
1241cf60731bSJohn Baldwin 			return (EINVAL);
1242fb99ab88SMatthew Dillon 		}
12438f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12448f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1245cf60731bSJohn Baldwin 				PROC_UNLOCK(p);
1246cf60731bSJohn Baldwin 				return (ENOMEM);
1247fb99ab88SMatthew Dillon 			}
12488f19eb88SIan Dowse 			p->p_sigstk = *ss;
1249d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
1250628d2653SJohn Baldwin 		} else {
1251d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
1252628d2653SJohn Baldwin 		}
1253d034d459SMarcel Moolenaar 	}
125406ce69a7SDavid Xu 	PROC_UNLOCK(p);
1255cf60731bSJohn Baldwin 	return (0);
1256df8bae1dSRodney W. Grimes }
1257df8bae1dSRodney W. Grimes 
1258d93f860cSPoul-Henning Kamp /*
1259d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1260d93f860cSPoul-Henning Kamp  * cp is calling process.
1261d93f860cSPoul-Henning Kamp  */
126237c84183SPoul-Henning Kamp static int
12639c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
12649c1ab3e0SJohn Baldwin 	register struct thread *td;
12652c42a146SMarcel Moolenaar 	int sig, pgid, all;
1266d93f860cSPoul-Henning Kamp {
1267d93f860cSPoul-Henning Kamp 	register struct proc *p;
1268d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1269d93f860cSPoul-Henning Kamp 	int nfound = 0;
1270d93f860cSPoul-Henning Kamp 
1271553629ebSJake Burkholder 	if (all) {
1272d93f860cSPoul-Henning Kamp 		/*
1273d93f860cSPoul-Henning Kamp 		 * broadcast
1274d93f860cSPoul-Henning Kamp 		 */
12751005a129SJohn Baldwin 		sx_slock(&allproc_lock);
12762e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1277628d2653SJohn Baldwin 			PROC_LOCK(p);
12789c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
12799c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1280628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1281628d2653SJohn Baldwin 				continue;
1282628d2653SJohn Baldwin 			}
1283f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1284d93f860cSPoul-Henning Kamp 				nfound++;
128533a9ed9dSJohn Baldwin 				if (sig)
12862c42a146SMarcel Moolenaar 					psignal(p, sig);
1287628d2653SJohn Baldwin 			}
128833a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1289d93f860cSPoul-Henning Kamp 		}
12901005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1291553629ebSJake Burkholder 	} else {
1292ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1293f591779bSSeigo Tanimura 		if (pgid == 0) {
1294d93f860cSPoul-Henning Kamp 			/*
1295d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1296d93f860cSPoul-Henning Kamp 			 */
12979c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1298f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1299f591779bSSeigo Tanimura 		} else {
1300d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1301f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1302ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1303d93f860cSPoul-Henning Kamp 				return (ESRCH);
1304d93f860cSPoul-Henning Kamp 			}
1305f591779bSSeigo Tanimura 		}
1306ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13072e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1308628d2653SJohn Baldwin 			PROC_LOCK(p);
1309628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1310628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1311628d2653SJohn Baldwin 				continue;
1312628d2653SJohn Baldwin 			}
1313e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
131433a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1315628d2653SJohn Baldwin 				continue;
1316628d2653SJohn Baldwin 			}
1317f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1318d93f860cSPoul-Henning Kamp 				nfound++;
131933a9ed9dSJohn Baldwin 				if (sig)
13202c42a146SMarcel Moolenaar 					psignal(p, sig);
1321628d2653SJohn Baldwin 			}
132233a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1323d93f860cSPoul-Henning Kamp 		}
1324f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1325d93f860cSPoul-Henning Kamp 	}
1326d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1327d93f860cSPoul-Henning Kamp }
1328d93f860cSPoul-Henning Kamp 
1329d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1330df8bae1dSRodney W. Grimes struct kill_args {
1331df8bae1dSRodney W. Grimes 	int	pid;
1332df8bae1dSRodney W. Grimes 	int	signum;
1333df8bae1dSRodney W. Grimes };
1334d2d3e875SBruce Evans #endif
1335fb99ab88SMatthew Dillon /*
1336fb99ab88SMatthew Dillon  * MPSAFE
1337fb99ab88SMatthew Dillon  */
1338df8bae1dSRodney W. Grimes /* ARGSUSED */
133926f9a767SRodney W. Grimes int
1340b40ce416SJulian Elischer kill(td, uap)
1341b40ce416SJulian Elischer 	register struct thread *td;
1342df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1343df8bae1dSRodney W. Grimes {
1344df8bae1dSRodney W. Grimes 	register struct proc *p;
134590af4afaSJohn Baldwin 	int error;
1346df8bae1dSRodney W. Grimes 
13476c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1348df8bae1dSRodney W. Grimes 		return (EINVAL);
1349fb99ab88SMatthew Dillon 
1350df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1351df8bae1dSRodney W. Grimes 		/* kill single process */
135290af4afaSJohn Baldwin 		if ((p = pfind(uap->pid)) == NULL)
135390af4afaSJohn Baldwin 			return (ESRCH);
135490af4afaSJohn Baldwin 		error = p_cansignal(td, p, uap->signum);
135590af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1356df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1357628d2653SJohn Baldwin 		PROC_UNLOCK(p);
135890af4afaSJohn Baldwin 		return (error);
1359df8bae1dSRodney W. Grimes 	}
1360df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1361df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
136290af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 1));
1363df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
136490af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, 0, 0));
1365df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
136690af4afaSJohn Baldwin 		return (killpg1(td, uap->signum, -uap->pid, 0));
1367df8bae1dSRodney W. Grimes 	}
136890af4afaSJohn Baldwin 	/* NOTREACHED */
1369df8bae1dSRodney W. Grimes }
1370df8bae1dSRodney W. Grimes 
1371df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1372d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1373df8bae1dSRodney W. Grimes struct okillpg_args {
1374df8bae1dSRodney W. Grimes 	int	pgid;
1375df8bae1dSRodney W. Grimes 	int	signum;
1376df8bae1dSRodney W. Grimes };
1377d2d3e875SBruce Evans #endif
1378fb99ab88SMatthew Dillon /*
1379fb99ab88SMatthew Dillon  * MPSAFE
1380fb99ab88SMatthew Dillon  */
1381df8bae1dSRodney W. Grimes /* ARGSUSED */
138226f9a767SRodney W. Grimes int
1383b40ce416SJulian Elischer okillpg(td, uap)
1384b40ce416SJulian Elischer 	struct thread *td;
1385df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1386df8bae1dSRodney W. Grimes {
1387df8bae1dSRodney W. Grimes 
13886c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1389df8bae1dSRodney W. Grimes 		return (EINVAL);
139090af4afaSJohn Baldwin 	return (killpg1(td, uap->signum, uap->pgid, 0));
1391df8bae1dSRodney W. Grimes }
1392df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1393df8bae1dSRodney W. Grimes 
1394df8bae1dSRodney W. Grimes /*
1395df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1396df8bae1dSRodney W. Grimes  */
1397df8bae1dSRodney W. Grimes void
13982c42a146SMarcel Moolenaar gsignal(pgid, sig)
13992c42a146SMarcel Moolenaar 	int pgid, sig;
1400df8bae1dSRodney W. Grimes {
1401df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1402df8bae1dSRodney W. Grimes 
1403f591779bSSeigo Tanimura 	if (pgid != 0) {
1404ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1405f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1406ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1407f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14082c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1409f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1410f591779bSSeigo Tanimura 		}
1411f591779bSSeigo Tanimura 	}
1412df8bae1dSRodney W. Grimes }
1413df8bae1dSRodney W. Grimes 
1414df8bae1dSRodney W. Grimes /*
1415df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1416df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1417df8bae1dSRodney W. Grimes  */
1418df8bae1dSRodney W. Grimes void
14192c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1420df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14212c42a146SMarcel Moolenaar 	int sig, checkctty;
1422df8bae1dSRodney W. Grimes {
1423df8bae1dSRodney W. Grimes 	register struct proc *p;
1424df8bae1dSRodney W. Grimes 
1425628d2653SJohn Baldwin 	if (pgrp) {
1426f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1427628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1428628d2653SJohn Baldwin 			PROC_LOCK(p);
1429df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14302c42a146SMarcel Moolenaar 				psignal(p, sig);
1431628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1432628d2653SJohn Baldwin 		}
1433628d2653SJohn Baldwin 	}
1434df8bae1dSRodney W. Grimes }
1435df8bae1dSRodney W. Grimes 
1436df8bae1dSRodney W. Grimes /*
14371bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1438df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1439df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1440356861dbSMatthew Dillon  *
1441356861dbSMatthew Dillon  * MPSAFE
1442df8bae1dSRodney W. Grimes  */
1443df8bae1dSRodney W. Grimes void
14441bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1445df8bae1dSRodney W. Grimes {
14461bf4700bSJeff Roberson 	struct sigacts *ps;
14471bf4700bSJeff Roberson 	struct proc *p;
14481bf4700bSJeff Roberson 
14491bf4700bSJeff Roberson 	p = td->td_proc;
1450df8bae1dSRodney W. Grimes 
1451628d2653SJohn Baldwin 	PROC_LOCK(p);
1452ef3dab76STim J. Robbins 	ps = p->p_sigacts;
145390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
145490af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
14554093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1456df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1457df8bae1dSRodney W. Grimes #ifdef KTRACE
1458374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1459374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
14604093529dSJeff Roberson 			    &td->td_sigmask, code);
1461df8bae1dSRodney W. Grimes #endif
1462a88b260aSJuli Mallett 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
14634093529dSJeff Roberson 						&td->td_sigmask, code);
14644093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
14652c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
14664093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
14672c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1468289ccde0SPeter Wemm 			/*
14698f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1470289ccde0SPeter Wemm 			 */
147190af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
14722c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
14732c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
147490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
14752c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1476dedc04feSPeter Wemm 		}
147790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
14786f841fb7SMarcel Moolenaar 	} else {
147990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
14806626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
14812c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
14824093529dSJeff Roberson 		tdsignal(td, sig);
1483df8bae1dSRodney W. Grimes 	}
1484628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1485df8bae1dSRodney W. Grimes }
1486df8bae1dSRodney W. Grimes 
14874093529dSJeff Roberson static struct thread *
14884093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
14894093529dSJeff Roberson {
14904093529dSJeff Roberson 	struct thread *td;
14914093529dSJeff Roberson 
14924093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
14934093529dSJeff Roberson 
14944093529dSJeff Roberson 	/*
14954093529dSJeff Roberson 	 * If we know the signal is bound for a specific thread then we
14964093529dSJeff Roberson 	 * assume that we are in that threads context.  This is the case
14974093529dSJeff Roberson 	 * for SIGXCPU, SIGILL, etc.  Otherwise someone did a kill() from
14984093529dSJeff Roberson 	 * userland and the real thread doesn't actually matter.
14994093529dSJeff Roberson 	 */
15004093529dSJeff Roberson 	if ((prop & SA_PROC) != 0 && curthread->td_proc == p)
15014093529dSJeff Roberson 		return (curthread);
15024093529dSJeff Roberson 
15034093529dSJeff Roberson 	/*
15044093529dSJeff Roberson 	 * We should search for the first thread that is blocked in
15054093529dSJeff Roberson 	 * sigsuspend with this signal unmasked.
15064093529dSJeff Roberson 	 */
15074093529dSJeff Roberson 
15084093529dSJeff Roberson 	/* XXX */
15094093529dSJeff Roberson 
15104093529dSJeff Roberson 	/*
15114093529dSJeff Roberson 	 * Find the first thread in the proc that doesn't have this signal
15124093529dSJeff Roberson 	 * masked.
15134093529dSJeff Roberson 	 */
15144093529dSJeff Roberson 	FOREACH_THREAD_IN_PROC(p, td)
15154093529dSJeff Roberson 		if (!SIGISMEMBER(td->td_sigmask, sig))
15164093529dSJeff Roberson 			return (td);
15174093529dSJeff Roberson 
15184093529dSJeff Roberson 	return (FIRST_THREAD_IN_PROC(p));
15194093529dSJeff Roberson }
15204093529dSJeff Roberson 
1521df8bae1dSRodney W. Grimes /*
1522df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1523df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1524df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1525df8bae1dSRodney W. Grimes  *
1526df8bae1dSRodney W. Grimes  * Exceptions:
1527df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1528df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1529df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1530df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1531df8bae1dSRodney W. Grimes  *
1532df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
153390af4afaSJohn Baldwin  *
153490af4afaSJohn Baldwin  * MPSAFE
1535df8bae1dSRodney W. Grimes  */
1536df8bae1dSRodney W. Grimes void
15374093529dSJeff Roberson psignal(struct proc *p, int sig)
1538df8bae1dSRodney W. Grimes {
1539b40ce416SJulian Elischer 	struct thread *td;
15404093529dSJeff Roberson 	int prop;
15414093529dSJeff Roberson 
15424093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15434093529dSJeff Roberson 	prop = sigprop(sig);
15444093529dSJeff Roberson 
15454093529dSJeff Roberson 	/*
15464093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
15474093529dSJeff Roberson 	 */
15484093529dSJeff Roberson 	td = sigtd(p, sig, prop);
15494093529dSJeff Roberson 
15504093529dSJeff Roberson 	tdsignal(td, sig);
15514093529dSJeff Roberson }
15524093529dSJeff Roberson 
155390af4afaSJohn Baldwin /*
155490af4afaSJohn Baldwin  * MPSAFE
155590af4afaSJohn Baldwin  */
15564093529dSJeff Roberson void
15574093529dSJeff Roberson tdsignal(struct thread *td, int sig)
15584093529dSJeff Roberson {
15594093529dSJeff Roberson 	struct proc *p;
15604093529dSJeff Roberson 	register sig_t action;
15614093529dSJeff Roberson 	sigset_t *siglist;
15624093529dSJeff Roberson 	struct thread *td0;
1563e602ba25SJulian Elischer 	register int prop;
156490af4afaSJohn Baldwin 	struct sigacts *ps;
1565df8bae1dSRodney W. Grimes 
15662899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
15674093529dSJeff Roberson 	    ("tdsignal(): invalid signal %d\n", sig));
15684093529dSJeff Roberson 
15694093529dSJeff Roberson 	p = td->td_proc;
157090af4afaSJohn Baldwin 	ps = p->p_sigacts;
15712c42a146SMarcel Moolenaar 
1572628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1573cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1574cb679c38SJonathan Lemon 
15752c42a146SMarcel Moolenaar 	prop = sigprop(sig);
15764093529dSJeff Roberson 
15774093529dSJeff Roberson 	/*
15784093529dSJeff Roberson 	 * If this thread is blocking this signal then we'll leave it in the
15794093529dSJeff Roberson 	 * proc so that we can find it in the first thread that unblocks it.
15804093529dSJeff Roberson 	 */
15814093529dSJeff Roberson 	if (SIGISMEMBER(td->td_sigmask, sig))
15824093529dSJeff Roberson 		siglist = &p->p_siglist;
15834093529dSJeff Roberson 	else
15844093529dSJeff Roberson 		siglist = &td->td_siglist;
15854093529dSJeff Roberson 
1586df8bae1dSRodney W. Grimes 	/*
15872a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
15882a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
15892a024a2bSSean Eric Fagan 	 * a chance, as well.
1590df8bae1dSRodney W. Grimes 	 */
1591b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1592df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1593b40ce416SJulian Elischer 	} else {
1594df8bae1dSRodney W. Grimes 		/*
1595df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1596df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
159790af4afaSJohn Baldwin 		 * (Note: we don't set SIGCONT in ps_sigignore,
1598df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1599df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1600df8bae1dSRodney W. Grimes 		 */
160190af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
160290af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
160390af4afaSJohn Baldwin 		    (p->p_flag & P_WEXIT)) {
160490af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
1605df8bae1dSRodney W. Grimes 			return;
160690af4afaSJohn Baldwin 		}
16074093529dSJeff Roberson 		if (SIGISMEMBER(td->td_sigmask, sig))
1608df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
160990af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1610df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1611df8bae1dSRodney W. Grimes 		else
1612df8bae1dSRodney W. Grimes 			action = SIG_DFL;
161390af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1614df8bae1dSRodney W. Grimes 	}
1615df8bae1dSRodney W. Grimes 
16164093529dSJeff Roberson 	if (prop & SA_CONT) {
16171d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
16184093529dSJeff Roberson 		/*
16194093529dSJeff Roberson 		 * XXX Should investigate leaving STOP and CONT sigs only in
16204093529dSJeff Roberson 		 * the proc's siglist.
16214093529dSJeff Roberson 		 */
16224093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
16234093529dSJeff Roberson 			SIG_STOPSIGMASK(td0->td_siglist);
16244093529dSJeff Roberson 	}
1625df8bae1dSRodney W. Grimes 
1626df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1627df8bae1dSRodney W. Grimes 		/*
1628df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1629df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1630df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1631df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1632df8bae1dSRodney W. Grimes 		 */
1633e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1634e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1635e602ba25SJulian Elischer 		    (action == SIG_DFL))
1636df8bae1dSRodney W. Grimes 		        return;
16371d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
16384093529dSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td0)
16394093529dSJeff Roberson 			SIG_CONTSIGMASK(td0->td_siglist);
16406933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1641df8bae1dSRodney W. Grimes 	}
16424093529dSJeff Roberson 	SIGADDSET(*siglist, sig);
16434093529dSJeff Roberson 	signotify(td);			/* uses schedlock */
16445312b1c7SDavid Xu 	/*
16455312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
16465312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
16475312b1c7SDavid Xu 	 */
16485312b1c7SDavid Xu 	if (action == SIG_HOLD &&
16495312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
16505312b1c7SDavid Xu 		return;
1651df8bae1dSRodney W. Grimes 	/*
1652e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1653e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1654e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1655e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1656e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1657e602ba25SJulian Elischer 	 * We try do the per-process part here.
1658df8bae1dSRodney W. Grimes 	 */
1659e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1660e602ba25SJulian Elischer 		/*
1661e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1662e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1663e602ba25SJulian Elischer 		 */
1664e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1665e602ba25SJulian Elischer 			/*
1666e602ba25SJulian Elischer 			 * The traced process is already stopped,
1667e602ba25SJulian Elischer 			 * so no further action is necessary.
1668e602ba25SJulian Elischer 			 * No signal can restart us.
1669e602ba25SJulian Elischer 			 */
1670e602ba25SJulian Elischer 			goto out;
16711c32c37cSJohn Baldwin 		}
1672b40ce416SJulian Elischer 
1673e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1674df8bae1dSRodney W. Grimes 			/*
1675e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1676e602ba25SJulian Elischer 			 * It will die elsewhere.
1677e602ba25SJulian Elischer 			 * All threads must be restarted.
1678df8bae1dSRodney W. Grimes 			 */
1679e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1680e602ba25SJulian Elischer 			goto runfast;
1681e602ba25SJulian Elischer 		}
1682e602ba25SJulian Elischer 
1683e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1684e602ba25SJulian Elischer 			/*
1685e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
16864093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
16871d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1688e602ba25SJulian Elischer 			 * continue the process and leave the signal in
16894093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1690e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1691e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1692e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1693e602ba25SJulian Elischer 			 */
16941279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
16956933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1696e602ba25SJulian Elischer 			if (action == SIG_DFL) {
16974093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1698e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1699e602ba25SJulian Elischer 				/*
1700e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1701e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1702e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1703e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1704e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1705e602ba25SJulian Elischer 				 * process, we need to make sure that the
1706e602ba25SJulian Elischer 				 * single thread is runnable asap.
1707e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1708e602ba25SJulian Elischer 				 */
1709e602ba25SJulian Elischer 				goto runfast;
1710e602ba25SJulian Elischer 			}
1711e602ba25SJulian Elischer 			/*
1712e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1713e602ba25SJulian Elischer 			 */
1714e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
171504774f23SJulian Elischer 			thread_unsuspend(p);
1716e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1717e602ba25SJulian Elischer 			goto out;
1718e602ba25SJulian Elischer 		}
1719e602ba25SJulian Elischer 
1720e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1721e602ba25SJulian Elischer 			/*
1722e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1723e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
172404774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1725e602ba25SJulian Elischer 			 */
17261279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
17274093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1728e602ba25SJulian Elischer 			goto out;
1729e602ba25SJulian Elischer 		}
1730e602ba25SJulian Elischer 
1731e602ba25SJulian Elischer 		/*
1732e602ba25SJulian Elischer 		 * All other kinds of signals:
1733e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1734e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1735e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
173604774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1737e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1738e602ba25SJulian Elischer 		 */
1739e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
174071fad9fdSJulian Elischer 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1741e602ba25SJulian Elischer 			if (td->td_flags & TDF_CVWAITQ)
174271fad9fdSJulian Elischer 				cv_abort(td);
1743e602ba25SJulian Elischer 			else
174471fad9fdSJulian Elischer 				abortsleep(td);
1745e602ba25SJulian Elischer 		}
1746e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1747df8bae1dSRodney W. Grimes 		goto out;
1748df8bae1dSRodney W. Grimes 		/*
1749e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1750e602ba25SJulian Elischer 		 * Shouldn't they abort too?
175104774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
175204774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1753df8bae1dSRodney W. Grimes 		 */
1754e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
175535c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
175635c32a76SDavid Xu 			!(prop & SA_STOP)) {
1757721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
17584093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1759721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1760df8bae1dSRodney W. Grimes 			goto out;
176104774f23SJulian Elischer 		}
176235c32a76SDavid Xu 		if (prop & SA_STOP) {
176335c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
176435c32a76SDavid Xu 				goto out;
1765e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1766e574e444SDavid Xu 			p->p_xstat = sig;
176735c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
17684093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
17694093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
1770062cf543SDavid Xu 				    (td0->td_flags & TDF_SINTR) &&
1771062cf543SDavid Xu 				    !TD_IS_SUSPENDED(td0)) {
17724093529dSJeff Roberson 					thread_suspend_one(td0);
1773062cf543SDavid Xu 				} else if (td != td0) {
1774062cf543SDavid Xu 					td0->td_flags |= TDF_ASTPENDING;
1775062cf543SDavid Xu 				}
177635c32a76SDavid Xu 			}
1777e574e444SDavid Xu 			thread_stopped(p);
17784093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1779e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
17804093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
17814093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
17824093529dSJeff Roberson 			}
178335c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
178435c32a76SDavid Xu 			goto out;
178535c32a76SDavid Xu 		}
1786721e5910SJulian Elischer 		else
1787df8bae1dSRodney W. Grimes 			goto runfast;
1788df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1789e602ba25SJulian Elischer 	} else {
1790e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
17914093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1792e602ba25SJulian Elischer 		goto out;
1793e602ba25SJulian Elischer 	}
1794e602ba25SJulian Elischer 
1795b40ce416SJulian Elischer 	/*
1796e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1797e602ba25SJulian Elischer 	 * running threads.
1798b40ce416SJulian Elischer 	 */
1799e602ba25SJulian Elischer 
1800e602ba25SJulian Elischer runfast:
1801aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
18024093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1803e602ba25SJulian Elischer 	thread_unsuspend(p);
1804e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1805e602ba25SJulian Elischer out:
1806e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1807e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1808e602ba25SJulian Elischer }
1809e602ba25SJulian Elischer 
1810e602ba25SJulian Elischer /*
1811e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1812e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1813e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1814e602ba25SJulian Elischer  */
1815e602ba25SJulian Elischer static void
18164093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1817e602ba25SJulian Elischer {
1818e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1819e602ba25SJulian Elischer 	register int prop;
1820e602ba25SJulian Elischer 
18218b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1822aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1823e602ba25SJulian Elischer 	prop = sigprop(sig);
1824e602ba25SJulian Elischer 	/*
1825aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1826e602ba25SJulian Elischer 	 * killed in this lifetime.
1827e602ba25SJulian Elischer 	 */
1828e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1829e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1830e602ba25SJulian Elischer 			td->td_priority = PUSER;
1831b40ce416SJulian Elischer 		}
1832b40ce416SJulian Elischer 	}
183371fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1834e602ba25SJulian Elischer 		/*
1835e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1836e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1837e602ba25SJulian Elischer 		 * be noticed when the process returns through
1838e602ba25SJulian Elischer 		 * trap() or syscall().
1839e602ba25SJulian Elischer 		 */
1840e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1841aa0fa334SJulian Elischer 			return;
1842e602ba25SJulian Elischer 		}
1843e602ba25SJulian Elischer 		/*
1844e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1845e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1846e602ba25SJulian Elischer 		 * for its parent.
1847e602ba25SJulian Elischer 		 */
1848e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1849e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1850aa0fa334SJulian Elischer 		} else {
1851aa0fa334SJulian Elischer 
1852df8bae1dSRodney W. Grimes 			/*
1853e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1854e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1855e602ba25SJulian Elischer 			 * be awakened.
1856df8bae1dSRodney W. Grimes 			 */
1857e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
18581d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
18594093529dSJeff Roberson 				/*
18604093529dSJeff Roberson 				 * It may be on either list in this state.
18614093529dSJeff Roberson 				 * Remove from both for now.
18624093529dSJeff Roberson 				 */
18634093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1864aa0fa334SJulian Elischer 				return;
1865df8bae1dSRodney W. Grimes 			}
1866df8bae1dSRodney W. Grimes 
1867aa0fa334SJulian Elischer 			/*
1868aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1869aa0fa334SJulian Elischer 			 */
1870aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
1871aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1872aa0fa334SJulian Elischer 			}
1873aa0fa334SJulian Elischer 		}
187471fad9fdSJulian Elischer 		if (td->td_flags & TDF_CVWAITQ)
187571fad9fdSJulian Elischer 			cv_abort(td);
187671fad9fdSJulian Elischer 		else
187771fad9fdSJulian Elischer 			abortsleep(td);
1878aa0fa334SJulian Elischer 	}
1879aa0fa334SJulian Elischer #ifdef SMP
1880aa0fa334SJulian Elischer 	  else {
1881df8bae1dSRodney W. Grimes 		/*
1882e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
1883df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1884df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1885df8bae1dSRodney W. Grimes 		 */
188671fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td) && td != curthread) {
1887e602ba25SJulian Elischer 			forward_signal(td);
1888aa0fa334SJulian Elischer 		}
18890ac3b636SAndrew Gallatin 	  }
18903163861cSTor Egge #endif
18916caa8a15SJohn Baldwin }
1892df8bae1dSRodney W. Grimes 
1893df8bae1dSRodney W. Grimes /*
1894df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1895df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1896df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1897df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1898df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1899628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
1900df8bae1dSRodney W. Grimes  * sequence is
1901df8bae1dSRodney W. Grimes  *
1902e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
19032c42a146SMarcel Moolenaar  *		postsig(sig);
1904df8bae1dSRodney W. Grimes  */
19056711f10fSJohn Baldwin static int
1906e602ba25SJulian Elischer issignal(td)
1907e602ba25SJulian Elischer 	struct thread *td;
1908df8bae1dSRodney W. Grimes {
1909e602ba25SJulian Elischer 	struct proc *p;
191090af4afaSJohn Baldwin 	struct sigacts *ps;
19114093529dSJeff Roberson 	sigset_t sigpending;
1912062cf543SDavid Xu 	int sig, prop;
1913062cf543SDavid Xu 	struct thread *td0;
1914df8bae1dSRodney W. Grimes 
1915e602ba25SJulian Elischer 	p = td->td_proc;
191690af4afaSJohn Baldwin 	ps = p->p_sigacts;
191790af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
1918628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1919df8bae1dSRodney W. Grimes 	for (;;) {
19202a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
19212a024a2bSSean Eric Fagan 
19224093529dSJeff Roberson 		sigpending = td->td_siglist;
19234093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
19244093529dSJeff Roberson 
1925df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
19264093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
19274093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
1928df8bae1dSRodney W. Grimes 			return (0);
19294093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
19302a024a2bSSean Eric Fagan 
1931628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
19322a024a2bSSean Eric Fagan 
1933df8bae1dSRodney W. Grimes 		/*
1934df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1935df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1936df8bae1dSRodney W. Grimes 		 */
193790af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
19384093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
1939df8bae1dSRodney W. Grimes 			continue;
1940df8bae1dSRodney W. Grimes 		}
1941df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1942df8bae1dSRodney W. Grimes 			/*
1943d8f4f6a4SJonathan Mini 			 * If traced, always stop.
1944df8bae1dSRodney W. Grimes 			 */
194590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
194690af4afaSJohn Baldwin 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
194790af4afaSJohn Baldwin 			    &p->p_mtx.mtx_object, "Stopping for traced signal");
19482c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1949628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1950df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1951628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
19529ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
19534d492b43SJulian Elischer 			stop(p);	/* uses schedlock too eventually */
195471fad9fdSJulian Elischer 			thread_suspend_one(td);
1955c86b6ff5SJohn Baldwin 			PROC_UNLOCK(p);
1956c86b6ff5SJohn Baldwin 			DROP_GIANT();
19572ad7d304SJohn Baldwin 			p->p_stats->p_ru.ru_nivcsw++;
1958df8bae1dSRodney W. Grimes 			mi_switch();
19599ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
196020cdcc5bSJohn Baldwin 			PICKUP_GIANT();
1961628d2653SJohn Baldwin 			PROC_LOCK(p);
196290af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
1963df8bae1dSRodney W. Grimes 
1964df8bae1dSRodney W. Grimes 			/*
1965df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1966df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1967df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1968df8bae1dSRodney W. Grimes 			 */
19694093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
19702c42a146SMarcel Moolenaar 			sig = p->p_xstat;
19712c42a146SMarcel Moolenaar 			if (sig == 0)
1972df8bae1dSRodney W. Grimes 				continue;
1973df8bae1dSRodney W. Grimes 
1974df8bae1dSRodney W. Grimes 			/*
19758d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
19768d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
19778d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
19788d542cb5SDavid E. O'Brien 			 */
19798d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
19808d542cb5SDavid E. O'Brien 				continue;
19818d542cb5SDavid E. O'Brien 
19828d542cb5SDavid E. O'Brien 			/*
19834093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
19841d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
1985df8bae1dSRodney W. Grimes 			 */
19864093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
19874093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
1988df8bae1dSRodney W. Grimes 				continue;
19894093529dSJeff Roberson 			signotify(td);
1990df8bae1dSRodney W. Grimes 		}
1991df8bae1dSRodney W. Grimes 
19928d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
19938d542cb5SDavid E. O'Brien 
1994df8bae1dSRodney W. Grimes 		/*
1995df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1996df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1997df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1998df8bae1dSRodney W. Grimes 		 */
1999d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2000df8bae1dSRodney W. Grimes 
2001d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2002df8bae1dSRodney W. Grimes 			/*
2003df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2004df8bae1dSRodney W. Grimes 			 */
2005df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2006df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2007df8bae1dSRodney W. Grimes 				/*
2008df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2009df8bae1dSRodney W. Grimes 				 * in init? XXX
2010df8bae1dSRodney W. Grimes 				 */
2011d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
20122c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2013df8bae1dSRodney W. Grimes #endif
2014df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2015df8bae1dSRodney W. Grimes 			}
2016df8bae1dSRodney W. Grimes 			/*
2017df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2018df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2019df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2020df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2021df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2022df8bae1dSRodney W. Grimes 			 */
2023df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2024df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2025df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2026df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2027df8bae1dSRodney W. Grimes 					break;	/* == ignore */
202890af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
202990af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
203090af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2031e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
20322c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2033721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2034062cf543SDavid Xu 				FOREACH_THREAD_IN_PROC(p, td0) {
2035062cf543SDavid Xu 					if (TD_IS_SLEEPING(td0) &&
2036062cf543SDavid Xu 					    (td0->td_flags & TDF_SINTR) &&
2037062cf543SDavid Xu 					    !TD_IS_SUSPENDED(td0)) {
2038062cf543SDavid Xu 						thread_suspend_one(td0);
2039062cf543SDavid Xu 					} else if (td != td0) {
2040062cf543SDavid Xu 						td0->td_flags |= TDF_ASTPENDING;
2041062cf543SDavid Xu 					}
2042062cf543SDavid Xu 				}
2043e574e444SDavid Xu 				thread_stopped(p);
204471fad9fdSJulian Elischer 				thread_suspend_one(td);
2045721e5910SJulian Elischer 				PROC_UNLOCK(p);
2046721e5910SJulian Elischer 				DROP_GIANT();
2047721e5910SJulian Elischer 				p->p_stats->p_ru.ru_nivcsw++;
2048721e5910SJulian Elischer 				mi_switch();
2049721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2050721e5910SJulian Elischer 				PICKUP_GIANT();
2051721e5910SJulian Elischer 				PROC_LOCK(p);
205290af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2053df8bae1dSRodney W. Grimes 				break;
205421b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2055df8bae1dSRodney W. Grimes 				/*
2056df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2057df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2058df8bae1dSRodney W. Grimes 				 */
2059df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2060df8bae1dSRodney W. Grimes 			} else
20612c42a146SMarcel Moolenaar 				return (sig);
2062df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2063df8bae1dSRodney W. Grimes 
2064d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2065df8bae1dSRodney W. Grimes 			/*
2066df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2067df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2068df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2069df8bae1dSRodney W. Grimes 			 */
2070df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2071df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2072df8bae1dSRodney W. Grimes 				printf("issignal\n");
2073df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2074df8bae1dSRodney W. Grimes 
2075df8bae1dSRodney W. Grimes 		default:
2076df8bae1dSRodney W. Grimes 			/*
2077df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2078df8bae1dSRodney W. Grimes 			 * postsig() process it.
2079df8bae1dSRodney W. Grimes 			 */
20802c42a146SMarcel Moolenaar 			return (sig);
2081df8bae1dSRodney W. Grimes 		}
20824093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2083df8bae1dSRodney W. Grimes 	}
2084df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2085df8bae1dSRodney W. Grimes }
2086df8bae1dSRodney W. Grimes 
2087df8bae1dSRodney W. Grimes /*
2088df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2089df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
20905b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
20915b3047d5SJohn Baldwin  * lock held.
2092df8bae1dSRodney W. Grimes  */
20935b3047d5SJohn Baldwin static void
2094e574e444SDavid Xu stop(struct proc *p)
2095df8bae1dSRodney W. Grimes {
2096df8bae1dSRodney W. Grimes 
2097628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
20981279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2099df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
210001609114SAlfred Perlstein 	wakeup(p->p_pptr);
2101df8bae1dSRodney W. Grimes }
2102df8bae1dSRodney W. Grimes 
210390af4afaSJohn Baldwin /*
210490af4afaSJohn Baldwin  * MPSAFE
210590af4afaSJohn Baldwin  */
2106e574e444SDavid Xu void
2107e574e444SDavid Xu thread_stopped(struct proc *p)
2108e574e444SDavid Xu {
2109e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
211090af4afaSJohn Baldwin 	struct sigacts *ps;
2111e574e444SDavid Xu 	int n;
2112e574e444SDavid Xu 
2113e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2114e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2115e574e444SDavid Xu 	n = p->p_suspcount;
2116e574e444SDavid Xu 	if (p == p1)
2117e574e444SDavid Xu 		n++;
2118e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2119e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2120e574e444SDavid Xu 		stop(p);
2121e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
212290af4afaSJohn Baldwin 		ps = p->p_pptr->p_sigacts;
212390af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
212490af4afaSJohn Baldwin 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
212590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2126e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
212790af4afaSJohn Baldwin 		} else
212890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2129e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2130e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2131e574e444SDavid Xu 	}
2132e574e444SDavid Xu }
2133e574e444SDavid Xu 
2134df8bae1dSRodney W. Grimes /*
2135df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2136df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2137df8bae1dSRodney W. Grimes  */
2138df8bae1dSRodney W. Grimes void
21392c42a146SMarcel Moolenaar postsig(sig)
21402c42a146SMarcel Moolenaar 	register int sig;
2141df8bae1dSRodney W. Grimes {
2142b40ce416SJulian Elischer 	struct thread *td = curthread;
2143b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2144628d2653SJohn Baldwin 	struct sigacts *ps;
21452c42a146SMarcel Moolenaar 	sig_t action;
21462c42a146SMarcel Moolenaar 	sigset_t returnmask;
21472c42a146SMarcel Moolenaar 	int code;
2148df8bae1dSRodney W. Grimes 
21492c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
21505526d2d9SEivind Eklund 
21512ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2152628d2653SJohn Baldwin 	ps = p->p_sigacts;
215390af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
21544093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
21552c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2156df8bae1dSRodney W. Grimes #ifdef KTRACE
2157374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
21585e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
21594093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2160df8bae1dSRodney W. Grimes #endif
2161628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
21622a024a2bSSean Eric Fagan 
2163df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
2164df8bae1dSRodney W. Grimes 		/*
2165df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2166df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2167df8bae1dSRodney W. Grimes 		 */
216890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2169b40ce416SJulian Elischer 		sigexit(td, sig);
2170df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2171df8bae1dSRodney W. Grimes 	} else {
2172df8bae1dSRodney W. Grimes 		/*
2173df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2174df8bae1dSRodney W. Grimes 		 */
21754093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
21765526d2d9SEivind Eklund 		    ("postsig action"));
2177df8bae1dSRodney W. Grimes 		/*
2178df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2179645682fdSLuoqi Chen 		 * occurrences of this signal.
2180df8bae1dSRodney W. Grimes 		 *
2181645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2182df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2183645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2184df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2185df8bae1dSRodney W. Grimes 		 */
21865e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
21874093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
21885e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2189df8bae1dSRodney W. Grimes 		} else
21904093529dSJeff Roberson 			returnmask = td->td_sigmask;
21912c42a146SMarcel Moolenaar 
21924093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
21932c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
21944093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
21952c42a146SMarcel Moolenaar 
21962c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2197289ccde0SPeter Wemm 			/*
21988f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2199289ccde0SPeter Wemm 			 */
220090af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
22012c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
22022c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
220390af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
22042c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2205dedc04feSPeter Wemm 		}
2206df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
22072c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2208df8bae1dSRodney W. Grimes 			code = 0;
2209df8bae1dSRodney W. Grimes 		} else {
22106626c604SJulian Elischer 			code = p->p_code;
22116626c604SJulian Elischer 			p->p_code = 0;
22126626c604SJulian Elischer 			p->p_sig = 0;
2213df8bae1dSRodney W. Grimes 		}
2214cd4f6ebbSDavid Xu 		if (td->td_flags & TDF_SA)
221558a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
221658a3c273SJeff Roberson 		else
221758a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
221858a3c273SJeff Roberson 			    &returnmask, code);
2219df8bae1dSRodney W. Grimes 	}
2220df8bae1dSRodney W. Grimes }
2221df8bae1dSRodney W. Grimes 
2222df8bae1dSRodney W. Grimes /*
2223df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2224df8bae1dSRodney W. Grimes  */
222526f9a767SRodney W. Grimes void
2226df8bae1dSRodney W. Grimes killproc(p, why)
2227df8bae1dSRodney W. Grimes 	struct proc *p;
2228df8bae1dSRodney W. Grimes 	char *why;
2229df8bae1dSRodney W. Grimes {
22309081e5e8SJohn Baldwin 
22319081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22320384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
22330384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2234729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2235b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2236df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2237df8bae1dSRodney W. Grimes }
2238df8bae1dSRodney W. Grimes 
2239df8bae1dSRodney W. Grimes /*
2240df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2241df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2242df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2243df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2244df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2245df8bae1dSRodney W. Grimes  * does not return.
224690af4afaSJohn Baldwin  *
224790af4afaSJohn Baldwin  * MPSAFE
2248df8bae1dSRodney W. Grimes  */
224926f9a767SRodney W. Grimes void
2250b40ce416SJulian Elischer sigexit(td, sig)
2251b40ce416SJulian Elischer 	struct thread *td;
22522c42a146SMarcel Moolenaar 	int sig;
2253df8bae1dSRodney W. Grimes {
2254b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2255df8bae1dSRodney W. Grimes 
2256628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2257df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
22582c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
22592c42a146SMarcel Moolenaar 		p->p_sig = sig;
2260c364e17eSAndrey A. Chernov 		/*
2261c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2262c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2263c364e17eSAndrey A. Chernov 		 * these messages.)
2264c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
2265c364e17eSAndrey A. Chernov 		 */
2266628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2267c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
2268c31146a1SJohn Baldwin 			mtx_lock(&Giant);
2269b40ce416SJulian Elischer 		if (coredump(td) == 0)
22702c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
227157308494SJoerg Wunsch 		if (kern_logsigexit)
227257308494SJoerg Wunsch 			log(LOG_INFO,
227357308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
22743d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
22759c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
22762c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
22772c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
2278c31146a1SJohn Baldwin 	} else {
2279628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2280628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
2281628d2653SJohn Baldwin 			mtx_lock(&Giant);
2282c31146a1SJohn Baldwin 	}
2283b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2284df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2285df8bae1dSRodney W. Grimes }
2286df8bae1dSRodney W. Grimes 
2287c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2288c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2289c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2290c5edb423SSean Eric Fagan 
2291c5edb423SSean Eric Fagan /*
2292c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2293c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2294c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2295c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2296c5edb423SSean Eric Fagan  *	%P	process id (pid)
2297c5edb423SSean Eric Fagan  *	%U	user id (uid)
2298c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2299c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2300c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2301c5edb423SSean Eric Fagan  */
2302c5edb423SSean Eric Fagan 
2303fca666a1SJulian Elischer static char *
2304c5edb423SSean Eric Fagan expand_name(name, uid, pid)
23058b43b535SAlfred Perlstein 	const char *name;
23068b43b535SAlfred Perlstein 	uid_t uid;
23078b43b535SAlfred Perlstein 	pid_t pid;
23088b43b535SAlfred Perlstein {
23098b43b535SAlfred Perlstein 	const char *format, *appendstr;
2310c5edb423SSean Eric Fagan 	char *temp;
2311c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
23128b43b535SAlfred Perlstein 	size_t i, l, n;
2313c5edb423SSean Eric Fagan 
23148b43b535SAlfred Perlstein 	format = corefilename;
23158b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
23160bfe2990SEivind Eklund 	if (temp == NULL)
23178b43b535SAlfred Perlstein 		return (NULL);
2318ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2319c5edb423SSean Eric Fagan 		switch (format[i]) {
2320c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2321c5edb423SSean Eric Fagan 			i++;
2322c5edb423SSean Eric Fagan 			switch (format[i]) {
2323c5edb423SSean Eric Fagan 			case '%':
23248b43b535SAlfred Perlstein 				appendstr = "%";
2325c5edb423SSean Eric Fagan 				break;
2326c5edb423SSean Eric Fagan 			case 'N':	/* process name */
23278b43b535SAlfred Perlstein 				appendstr = name;
2328c5edb423SSean Eric Fagan 				break;
2329c5edb423SSean Eric Fagan 			case 'P':	/* process id */
23308b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
23318b43b535SAlfred Perlstein 				appendstr = buf;
2332c5edb423SSean Eric Fagan 				break;
2333c5edb423SSean Eric Fagan 			case 'U':	/* user id */
23348b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
23358b43b535SAlfred Perlstein 				appendstr = buf;
2336c5edb423SSean Eric Fagan 				break;
2337c5edb423SSean Eric Fagan 			default:
23388b43b535SAlfred Perlstein 				appendstr = "";
23398b43b535SAlfred Perlstein 			  	log(LOG_ERR,
23408b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
23418b43b535SAlfred Perlstein 				    format[i], format);
2342c5edb423SSean Eric Fagan 			}
23438b43b535SAlfred Perlstein 			l = strlen(appendstr);
23448b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
23458b43b535SAlfred Perlstein 				goto toolong;
23468b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
23478b43b535SAlfred Perlstein 			n += l;
2348c5edb423SSean Eric Fagan 			break;
2349c5edb423SSean Eric Fagan 		default:
2350c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2351c5edb423SSean Eric Fagan 		}
2352c5edb423SSean Eric Fagan 	}
23538b43b535SAlfred Perlstein 	if (format[i] != '\0')
23548b43b535SAlfred Perlstein 		goto toolong;
23558b43b535SAlfred Perlstein 	return (temp);
23568b43b535SAlfred Perlstein toolong:
23578b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
23588b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
23598b43b535SAlfred Perlstein 	free(temp, M_TEMP);
23608b43b535SAlfred Perlstein 	return (NULL);
2361c5edb423SSean Eric Fagan }
2362c5edb423SSean Eric Fagan 
2363df8bae1dSRodney W. Grimes /*
2364fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2365fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2366fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2367fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2368fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2369fca666a1SJulian Elischer  */
2370fca666a1SJulian Elischer 
2371fca666a1SJulian Elischer static int
2372b40ce416SJulian Elischer coredump(struct thread *td)
2373fca666a1SJulian Elischer {
2374b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2375fca666a1SJulian Elischer 	register struct vnode *vp;
23769c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
237706ae1e91SMatthew Dillon 	struct flock lf;
2378fca666a1SJulian Elischer 	struct nameidata nd;
2379fca666a1SJulian Elischer 	struct vattr vattr;
2380e6796b67SKirk McKusick 	int error, error1, flags;
2381f2a2857bSKirk McKusick 	struct mount *mp;
2382fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2383fca666a1SJulian Elischer 	off_t limit;
2384fca666a1SJulian Elischer 
2385628d2653SJohn Baldwin 	PROC_LOCK(p);
2386628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2387fca666a1SJulian Elischer 
2388628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2389628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2390fca666a1SJulian Elischer 		return (EFAULT);
2391628d2653SJohn Baldwin 	}
2392fca666a1SJulian Elischer 
2393fca666a1SJulian Elischer 	/*
239435a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
239535a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
239635a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
239735a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
239835a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
239935a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2400fca666a1SJulian Elischer 	 */
240135a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2402628d2653SJohn Baldwin 	if (limit == 0) {
2403628d2653SJohn Baldwin 		PROC_UNLOCK(p);
240435a2598fSSean Eric Fagan 		return 0;
2405628d2653SJohn Baldwin 	}
2406628d2653SJohn Baldwin 	PROC_UNLOCK(p);
240735a2598fSSean Eric Fagan 
2408f2a2857bSKirk McKusick restart:
24099c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2410ccdbd10cSPeter Pentchev 	if (name == NULL)
2411ccdbd10cSPeter Pentchev 		return (EINVAL);
2412b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2413e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2414e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2415fca666a1SJulian Elischer 	free(name, M_TEMP);
2416fca666a1SJulian Elischer 	if (error)
2417fca666a1SJulian Elischer 		return (error);
2418762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2419fca666a1SJulian Elischer 	vp = nd.ni_vp;
242006ae1e91SMatthew Dillon 
2421832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2422832dafadSDon Lewis 	if (vp->v_type != VREG ||
2423832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2424832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2425832dafadSDon Lewis 		error = EFAULT;
2426832dafadSDon Lewis 		goto out2;
2427832dafadSDon Lewis 	}
2428832dafadSDon Lewis 
2429b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
243006ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
243106ae1e91SMatthew Dillon 	lf.l_start = 0;
243206ae1e91SMatthew Dillon 	lf.l_len = 0;
243306ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
243406ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
243506ae1e91SMatthew Dillon 	if (error)
243606ae1e91SMatthew Dillon 		goto out2;
243706ae1e91SMatthew Dillon 
243806ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
243906ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
244006ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2441b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2442f2a2857bSKirk McKusick 			return (error);
2443f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2444f2a2857bSKirk McKusick 			return (error);
2445f2a2857bSKirk McKusick 		goto restart;
2446f2a2857bSKirk McKusick 	}
2447fca666a1SJulian Elischer 
2448fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2449fca666a1SJulian Elischer 	vattr.va_size = 0;
245088b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2451b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2452b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
245388b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2454628d2653SJohn Baldwin 	PROC_LOCK(p);
2455fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2456628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2457fca666a1SJulian Elischer 
2458fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2459b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2460fca666a1SJulian Elischer 	  ENOSYS;
2461fca666a1SJulian Elischer 
246206ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
246306ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2464f2a2857bSKirk McKusick 	vn_finished_write(mp);
246506ae1e91SMatthew Dillon out2:
2466b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2467fca666a1SJulian Elischer 	if (error == 0)
2468fca666a1SJulian Elischer 		error = error1;
2469fca666a1SJulian Elischer 	return (error);
2470fca666a1SJulian Elischer }
2471fca666a1SJulian Elischer 
2472fca666a1SJulian Elischer /*
2473df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2474df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2475df8bae1dSRodney W. Grimes  */
2476d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2477df8bae1dSRodney W. Grimes struct nosys_args {
2478df8bae1dSRodney W. Grimes 	int	dummy;
2479df8bae1dSRodney W. Grimes };
2480d2d3e875SBruce Evans #endif
2481fb99ab88SMatthew Dillon /*
2482fb99ab88SMatthew Dillon  * MPSAFE
2483fb99ab88SMatthew Dillon  */
2484df8bae1dSRodney W. Grimes /* ARGSUSED */
248526f9a767SRodney W. Grimes int
2486b40ce416SJulian Elischer nosys(td, args)
2487b40ce416SJulian Elischer 	struct thread *td;
2488df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2489df8bae1dSRodney W. Grimes {
2490b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2491b40ce416SJulian Elischer 
2492628d2653SJohn Baldwin 	PROC_LOCK(p);
2493df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2494628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2495f5216b9aSBruce Evans 	return (ENOSYS);
2496df8bae1dSRodney W. Grimes }
2497831d27a9SDon Lewis 
2498831d27a9SDon Lewis /*
249948f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2500831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2501831d27a9SDon Lewis  */
2502831d27a9SDon Lewis void
2503f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2504f1320723SAlfred Perlstein 	struct sigio **sigiop;
25052c42a146SMarcel Moolenaar 	int sig, checkctty;
2506831d27a9SDon Lewis {
2507f1320723SAlfred Perlstein 	struct sigio *sigio;
2508831d27a9SDon Lewis 
2509f1320723SAlfred Perlstein 	SIGIO_LOCK();
2510f1320723SAlfred Perlstein 	sigio = *sigiop;
2511f1320723SAlfred Perlstein 	if (sigio == NULL) {
2512f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2513f1320723SAlfred Perlstein 		return;
2514f1320723SAlfred Perlstein 	}
2515831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2516628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
25172b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
25182c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2519628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2520831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2521831d27a9SDon Lewis 		struct proc *p;
2522831d27a9SDon Lewis 
2523f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2524628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2525628d2653SJohn Baldwin 			PROC_LOCK(p);
25262b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2527831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
25282c42a146SMarcel Moolenaar 				psignal(p, sig);
2529628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2530628d2653SJohn Baldwin 		}
2531f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2532831d27a9SDon Lewis 	}
2533f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2534831d27a9SDon Lewis }
2535cb679c38SJonathan Lemon 
2536cb679c38SJonathan Lemon static int
2537cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2538cb679c38SJonathan Lemon {
2539cb679c38SJonathan Lemon 	struct proc *p = curproc;
2540cb679c38SJonathan Lemon 
2541cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2542cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2543cb679c38SJonathan Lemon 
2544628d2653SJohn Baldwin 	PROC_LOCK(p);
2545cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2546628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2547cb679c38SJonathan Lemon 
2548cb679c38SJonathan Lemon 	return (0);
2549cb679c38SJonathan Lemon }
2550cb679c38SJonathan Lemon 
2551cb679c38SJonathan Lemon static void
2552cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2553cb679c38SJonathan Lemon {
2554cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2555cb679c38SJonathan Lemon 
2556628d2653SJohn Baldwin 	PROC_LOCK(p);
2557e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2558628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2559cb679c38SJonathan Lemon }
2560cb679c38SJonathan Lemon 
2561cb679c38SJonathan Lemon /*
2562cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2563cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2564cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2565cb679c38SJonathan Lemon  * isn't worth the trouble.
2566cb679c38SJonathan Lemon  */
2567cb679c38SJonathan Lemon static int
2568cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2569cb679c38SJonathan Lemon {
2570cb679c38SJonathan Lemon 
2571cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2572cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2573cb679c38SJonathan Lemon 
2574cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2575cb679c38SJonathan Lemon 			kn->kn_data++;
2576cb679c38SJonathan Lemon 	}
2577cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2578cb679c38SJonathan Lemon }
257990af4afaSJohn Baldwin 
258090af4afaSJohn Baldwin struct sigacts *
258190af4afaSJohn Baldwin sigacts_alloc(void)
258290af4afaSJohn Baldwin {
258390af4afaSJohn Baldwin 	struct sigacts *ps;
258490af4afaSJohn Baldwin 
258590af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
258690af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
258790af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
258890af4afaSJohn Baldwin 	return (ps);
258990af4afaSJohn Baldwin }
259090af4afaSJohn Baldwin 
259190af4afaSJohn Baldwin void
259290af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
259390af4afaSJohn Baldwin {
259490af4afaSJohn Baldwin 
259590af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
259690af4afaSJohn Baldwin 	ps->ps_refcnt--;
259790af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
259890af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
259990af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
260090af4afaSJohn Baldwin 	} else
260190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
260290af4afaSJohn Baldwin }
260390af4afaSJohn Baldwin 
260490af4afaSJohn Baldwin struct sigacts *
260590af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
260690af4afaSJohn Baldwin {
260790af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
260890af4afaSJohn Baldwin 	ps->ps_refcnt++;
260990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
261090af4afaSJohn Baldwin 	return (ps);
261190af4afaSJohn Baldwin }
261290af4afaSJohn Baldwin 
261390af4afaSJohn Baldwin void
261490af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
261590af4afaSJohn Baldwin {
261690af4afaSJohn Baldwin 
261790af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
261890af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
261990af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
262090af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
262190af4afaSJohn Baldwin }
262290af4afaSJohn Baldwin 
262390af4afaSJohn Baldwin int
262490af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
262590af4afaSJohn Baldwin {
262690af4afaSJohn Baldwin 	int shared;
262790af4afaSJohn Baldwin 
262890af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
262990af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
263090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
263190af4afaSJohn Baldwin 	return (shared);
263290af4afaSJohn Baldwin }
2633