xref: /freebsd/sys/kern/kern_sig.c (revision a447cd8b287c5c3557278ece86822d2db4d71768)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46c87e2930SDavid Greenman #include <sys/kernel.h>
47d2d3e875SBruce Evans #include <sys/sysproto.h>
4836240ea5SDoug Rabson #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/namei.h>
51df8bae1dSRodney W. Grimes #include <sys/vnode.h>
52cb679c38SJonathan Lemon #include <sys/event.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
542a024a2bSSean Eric Fagan #include <sys/pioctl.h>
55df8bae1dSRodney W. Grimes #include <sys/acct.h>
563ac4d1efSBruce Evans #include <sys/fcntl.h>
57238510fcSJason Evans #include <sys/condvar.h>
58c31146a1SJohn Baldwin #include <sys/lock.h>
5935e0e5b3SJohn Baldwin #include <sys/mutex.h>
60df8bae1dSRodney W. Grimes #include <sys/wait.h>
610384fff8SJason Evans #include <sys/ktr.h>
62df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
63c31146a1SJohn Baldwin #include <sys/resourcevar.h>
646caa8a15SJohn Baldwin #include <sys/smp.h>
65df8bae1dSRodney W. Grimes #include <sys/stat.h>
661005a129SJohn Baldwin #include <sys/sx.h>
678f19eb88SIan Dowse #include <sys/syscallsubr.h>
681005a129SJohn Baldwin #include <sys/syslog.h>
69d66a5066SPeter Wemm #include <sys/sysent.h>
70c87e2930SDavid Greenman #include <sys/sysctl.h>
71c5edb423SSean Eric Fagan #include <sys/malloc.h>
7206ae1e91SMatthew Dillon #include <sys/unistd.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <machine/cpu.h>
75df8bae1dSRodney W. Grimes 
7623eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
7723eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
7823eeeff7SPeter Wemm #endif
7923eeeff7SPeter Wemm 
806f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
816f841fb7SMarcel Moolenaar 
824d77a549SAlfred Perlstein static int	coredump(struct thread *);
834d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
849c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
854d77a549SAlfred Perlstein static int	sigprop(int sig);
864d77a549SAlfred Perlstein static void	stop(struct proc *);
874093529dSJeff Roberson static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
88cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
89cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
90cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
914093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
92a447cd8bSJeff Roberson static int	do_sigprocmask(struct thread *td, int how,
93a447cd8bSJeff Roberson 				sigset_t *set, sigset_t *oset, int old);
94a447cd8bSJeff Roberson static int	kern_sigtimedwait(struct thread *td, sigset_t set,
95a447cd8bSJeff Roberson 				siginfo_t *info, struct timespec *timeout);
96cb679c38SJonathan Lemon 
97cb679c38SJonathan Lemon struct filterops sig_filtops =
98cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
99cb679c38SJonathan Lemon 
10057308494SJoerg Wunsch static int	kern_logsigexit = 1;
1013d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1023d177f46SBill Fumerola     &kern_logsigexit, 0,
1033d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10457308494SJoerg Wunsch 
1052b87b6d4SRobert Watson /*
1062b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1072b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1082b87b6d4SRobert Watson  * in the right situations.
1092b87b6d4SRobert Watson  */
1102b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1112b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1122b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1142b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1152b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1162b87b6d4SRobert Watson 
11722d4b0fbSJohn Polstra int sugid_coredump;
1183d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1193d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
120c87e2930SDavid Greenman 
121e5a28db9SPaul Saab static int	do_coredump = 1;
122e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
123e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
124e5a28db9SPaul Saab 
1252c42a146SMarcel Moolenaar /*
1262c42a146SMarcel Moolenaar  * Signal properties and actions.
1272c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1282c42a146SMarcel Moolenaar  * according to the following properties:
1292c42a146SMarcel Moolenaar  */
1302c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1312c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1322c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1332c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1342c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1352c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1362c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
137da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
138df8bae1dSRodney W. Grimes 
1392c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
140da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
141da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
142da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
146da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
148da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1502c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1512c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
152da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
153da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
154da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
155da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
156da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
157da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
158da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
159da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
160da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
161da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
162da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
1632c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
1642c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
165da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
166da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
167da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
168da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
169da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
170da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
1712c42a146SMarcel Moolenaar };
1722c42a146SMarcel Moolenaar 
173fbbeeb6cSBruce Evans /*
174fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
175fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
176fbbeeb6cSBruce Evans  * action, the process stops in issignal().
177e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
178fbbeeb6cSBruce Evans  *
17933510ef1SBruce Evans  * MP SAFE.
180fbbeeb6cSBruce Evans  */
181fbbeeb6cSBruce Evans int
182e602ba25SJulian Elischer cursig(struct thread *td)
183fbbeeb6cSBruce Evans {
184e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
185fbbeeb6cSBruce Evans 
1862ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, 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 
21579065dbaSBruce Evans 	mtx_lock_spin(&sched_lock);
2164093529dSJeff Roberson 	if (SIGPENDING(td))
2174093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
21879065dbaSBruce Evans 	mtx_unlock_spin(&sched_lock);
21979065dbaSBruce Evans }
22079065dbaSBruce Evans 
2216f841fb7SMarcel Moolenaar static __inline int
2226f841fb7SMarcel Moolenaar sigprop(int sig)
2232c42a146SMarcel Moolenaar {
2246f841fb7SMarcel Moolenaar 
2252c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2262c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2272c42a146SMarcel Moolenaar 	return (0);
228df8bae1dSRodney W. Grimes }
2292c42a146SMarcel Moolenaar 
2304093529dSJeff Roberson int
2316f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2322c42a146SMarcel Moolenaar {
2332c42a146SMarcel Moolenaar 	int i;
2342c42a146SMarcel Moolenaar 
2356f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2362c42a146SMarcel Moolenaar 		if (set->__bits[i])
2372c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
238df8bae1dSRodney W. Grimes 	return (0);
239df8bae1dSRodney W. Grimes }
240df8bae1dSRodney W. Grimes 
2412c42a146SMarcel Moolenaar /*
2428f19eb88SIan Dowse  * kern_sigaction
2432c42a146SMarcel Moolenaar  * sigaction
24423eeeff7SPeter Wemm  * freebsd4_sigaction
2452c42a146SMarcel Moolenaar  * osigaction
2462c42a146SMarcel Moolenaar  */
2478f19eb88SIan Dowse int
24823eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2498f19eb88SIan Dowse 	struct thread *td;
2502c42a146SMarcel Moolenaar 	register int sig;
2512c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
25223eeeff7SPeter Wemm 	int flags;
253df8bae1dSRodney W. Grimes {
254628d2653SJohn Baldwin 	register struct sigacts *ps;
2554093529dSJeff Roberson 	struct thread *td0;
2568f19eb88SIan Dowse 	struct proc *p = td->td_proc;
257df8bae1dSRodney W. Grimes 
2582899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2592c42a146SMarcel Moolenaar 		return (EINVAL);
2602c42a146SMarcel Moolenaar 
261628d2653SJohn Baldwin 	PROC_LOCK(p);
262628d2653SJohn Baldwin 	ps = p->p_sigacts;
2632c42a146SMarcel Moolenaar 	if (oact) {
2642c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2652c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2662c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2672c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2682c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2692c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2702c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2712c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2722c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2732c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2742c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2752c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2762c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
277645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2782c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
279645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2802c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2812c42a146SMarcel Moolenaar 	}
2822c42a146SMarcel Moolenaar 	if (act) {
2832c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
284628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
285628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2862c42a146SMarcel Moolenaar 			return (EINVAL);
287628d2653SJohn Baldwin 		}
2882c42a146SMarcel Moolenaar 
289df8bae1dSRodney W. Grimes 		/*
290df8bae1dSRodney W. Grimes 		 * Change setting atomically.
291df8bae1dSRodney W. Grimes 		 */
2922c42a146SMarcel Moolenaar 
2932c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2942c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2952c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
296aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
297aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
29880f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
29980f42b55SIan Dowse 		} else {
30080f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
3012c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
3022c42a146SMarcel Moolenaar 		}
3032c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
3042c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
305df8bae1dSRodney W. Grimes 		else
3062c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3072c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3082c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
309df8bae1dSRodney W. Grimes 		else
3102c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3112c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3122c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3131e41c1b5SSteven Wallace 		else
3142c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3152c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3162c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
317289ccde0SPeter Wemm 		else
3182c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
319df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3202c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3212c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
322df8bae1dSRodney W. Grimes 		else
3238c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
324df8bae1dSRodney W. Grimes #endif
3252c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3262c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
327645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
3286626c604SJulian Elischer 			else
329645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
330ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
331245f17d4SJoerg Wunsch 				/*
3322c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3332c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3342c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3352c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
336245f17d4SJoerg Wunsch 				 */
337245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
338645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3396626c604SJulian Elischer 				else
340645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
341245f17d4SJoerg Wunsch 			} else
342645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
343ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
344ba1551caSIan Dowse 				p->p_procsig->ps_flag |= PS_CLDSIGIGN;
345ba1551caSIan Dowse 			else
346ba1551caSIan Dowse 				p->p_procsig->ps_flag &= ~PS_CLDSIGIGN;
347df8bae1dSRodney W. Grimes 		}
348df8bae1dSRodney W. Grimes 		/*
349df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3502c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3512c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3522c42a146SMarcel Moolenaar 		 * have to restart the process.
353df8bae1dSRodney W. Grimes 		 */
3542c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3552c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3562c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3572c42a146SMarcel Moolenaar 			/* never to be seen again */
3581d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
3594093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0)
3604093529dSJeff Roberson 				SIGDELSET(td0->td_siglist, sig);
3612c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3622c42a146SMarcel Moolenaar 				/* easier in psignal */
3632c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3642c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
365645682fdSLuoqi Chen 		} else {
3662c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3672c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3682c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3692c42a146SMarcel Moolenaar 			else
3702c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3712c42a146SMarcel Moolenaar 		}
37223eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
37323eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
37423eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
37523eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
37623eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
37723eeeff7SPeter Wemm 		else
37823eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
37923eeeff7SPeter Wemm #endif
380e8ebc08fSPeter Wemm #ifdef COMPAT_43
381645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
38223eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
38323eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
384645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
385645682fdSLuoqi Chen 		else
386645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
387e8ebc08fSPeter Wemm #endif
388df8bae1dSRodney W. Grimes 	}
389628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3902c42a146SMarcel Moolenaar 	return (0);
3912c42a146SMarcel Moolenaar }
3922c42a146SMarcel Moolenaar 
3932c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3942c42a146SMarcel Moolenaar struct sigaction_args {
3952c42a146SMarcel Moolenaar 	int	sig;
3962c42a146SMarcel Moolenaar 	struct	sigaction *act;
3972c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3982c42a146SMarcel Moolenaar };
3992c42a146SMarcel Moolenaar #endif
400fb99ab88SMatthew Dillon /*
401fb99ab88SMatthew Dillon  * MPSAFE
402fb99ab88SMatthew Dillon  */
4032c42a146SMarcel Moolenaar int
404b40ce416SJulian Elischer sigaction(td, uap)
405b40ce416SJulian Elischer 	struct thread *td;
4062c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4072c42a146SMarcel Moolenaar {
4082c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4092c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4102c42a146SMarcel Moolenaar 	int error;
4112c42a146SMarcel Moolenaar 
4126f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4136f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4142c42a146SMarcel Moolenaar 	if (actp) {
4156f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4162c42a146SMarcel Moolenaar 		if (error)
41744443757STim J. Robbins 			return (error);
4182c42a146SMarcel Moolenaar 	}
41944443757STim J. Robbins 	mtx_lock(&Giant);
4208f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
42144443757STim J. Robbins 	mtx_unlock(&Giant);
4222c42a146SMarcel Moolenaar 	if (oactp && !error) {
4236f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4242c42a146SMarcel Moolenaar 	}
4252c42a146SMarcel Moolenaar 	return (error);
4262c42a146SMarcel Moolenaar }
4272c42a146SMarcel Moolenaar 
42823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
42923eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
43023eeeff7SPeter Wemm struct freebsd4_sigaction_args {
43123eeeff7SPeter Wemm 	int	sig;
43223eeeff7SPeter Wemm 	struct	sigaction *act;
43323eeeff7SPeter Wemm 	struct	sigaction *oact;
43423eeeff7SPeter Wemm };
43523eeeff7SPeter Wemm #endif
43623eeeff7SPeter Wemm /*
43723eeeff7SPeter Wemm  * MPSAFE
43823eeeff7SPeter Wemm  */
43923eeeff7SPeter Wemm int
44023eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
44123eeeff7SPeter Wemm 	struct thread *td;
44223eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
44323eeeff7SPeter Wemm {
44423eeeff7SPeter Wemm 	struct sigaction act, oact;
44523eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
44623eeeff7SPeter Wemm 	int error;
44723eeeff7SPeter Wemm 
44823eeeff7SPeter Wemm 
44923eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
45023eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
45123eeeff7SPeter Wemm 	if (actp) {
45223eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
45323eeeff7SPeter Wemm 		if (error)
45444443757STim J. Robbins 			return (error);
45523eeeff7SPeter Wemm 	}
45644443757STim J. Robbins 	mtx_lock(&Giant);
45723eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
45844443757STim J. Robbins 	mtx_unlock(&Giant);
45923eeeff7SPeter Wemm 	if (oactp && !error) {
46023eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
46123eeeff7SPeter Wemm 	}
46223eeeff7SPeter Wemm 	return (error);
46323eeeff7SPeter Wemm }
46423eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
46523eeeff7SPeter Wemm 
46631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4672c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4682c42a146SMarcel Moolenaar struct osigaction_args {
4692c42a146SMarcel Moolenaar 	int	signum;
4702c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4712c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4722c42a146SMarcel Moolenaar };
4732c42a146SMarcel Moolenaar #endif
474fb99ab88SMatthew Dillon /*
475fb99ab88SMatthew Dillon  * MPSAFE
476fb99ab88SMatthew Dillon  */
4772c42a146SMarcel Moolenaar int
478b40ce416SJulian Elischer osigaction(td, uap)
479b40ce416SJulian Elischer 	struct thread *td;
4802c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4812c42a146SMarcel Moolenaar {
4822c42a146SMarcel Moolenaar 	struct osigaction sa;
4832c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4842c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4852c42a146SMarcel Moolenaar 	int error;
4862c42a146SMarcel Moolenaar 
4876f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4886f841fb7SMarcel Moolenaar 		return (EINVAL);
489fb99ab88SMatthew Dillon 
4906f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4916f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
492fb99ab88SMatthew Dillon 
4932c42a146SMarcel Moolenaar 	if (nsap) {
4946f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4952c42a146SMarcel Moolenaar 		if (error)
49644443757STim J. Robbins 			return (error);
4972c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4982c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4992c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5002c42a146SMarcel Moolenaar 	}
50144443757STim J. Robbins 	mtx_lock(&Giant);
50223eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
50344443757STim J. Robbins 	mtx_unlock(&Giant);
5042c42a146SMarcel Moolenaar 	if (osap && !error) {
5052c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5062c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5072c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5086f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5092c42a146SMarcel Moolenaar 	}
5102c42a146SMarcel Moolenaar 	return (error);
5112c42a146SMarcel Moolenaar }
51223eeeff7SPeter Wemm 
51323eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
51423eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
51523eeeff7SPeter Wemm int
51623eeeff7SPeter Wemm osigreturn(td, uap)
51723eeeff7SPeter Wemm 	struct thread *td;
51823eeeff7SPeter Wemm 	struct osigreturn_args *uap;
51923eeeff7SPeter Wemm {
52023eeeff7SPeter Wemm 
52123eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
52223eeeff7SPeter Wemm }
52323eeeff7SPeter Wemm #endif
52431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
525df8bae1dSRodney W. Grimes 
526df8bae1dSRodney W. Grimes /*
527df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
528df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
529df8bae1dSRodney W. Grimes  */
530df8bae1dSRodney W. Grimes void
531df8bae1dSRodney W. Grimes siginit(p)
532df8bae1dSRodney W. Grimes 	struct proc *p;
533df8bae1dSRodney W. Grimes {
534df8bae1dSRodney W. Grimes 	register int i;
535df8bae1dSRodney W. Grimes 
536628d2653SJohn Baldwin 	PROC_LOCK(p);
5372c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5382c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
5392c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
540628d2653SJohn Baldwin 	PROC_UNLOCK(p);
541df8bae1dSRodney W. Grimes }
542df8bae1dSRodney W. Grimes 
543df8bae1dSRodney W. Grimes /*
544df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
545df8bae1dSRodney W. Grimes  */
546df8bae1dSRodney W. Grimes void
547df8bae1dSRodney W. Grimes execsigs(p)
548df8bae1dSRodney W. Grimes 	register struct proc *p;
549df8bae1dSRodney W. Grimes {
550628d2653SJohn Baldwin 	register struct sigacts *ps;
5512c42a146SMarcel Moolenaar 	register int sig;
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 	/*
554df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
5554093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
556df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
557df8bae1dSRodney W. Grimes 	 */
5589b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
559628d2653SJohn Baldwin 	ps = p->p_sigacts;
5602c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
5612c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
5622c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
5632c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5642c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
5652c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
5661d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
5674093529dSJeff Roberson 			/*
5684093529dSJeff Roberson 			 * There is only one thread at this point.
5694093529dSJeff Roberson 			 */
5704093529dSJeff Roberson 			SIGDELSET(FIRST_THREAD_IN_PROC(p)->td_siglist, sig);
571df8bae1dSRodney W. Grimes 		}
5722c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
573df8bae1dSRodney W. Grimes 	}
574df8bae1dSRodney W. Grimes 	/*
5754093529dSJeff Roberson 	 * Clear out the td's sigmask.  Normal processes use the proc sigmask.
5764093529dSJeff Roberson 	 */
5774093529dSJeff Roberson 	SIGEMPTYSET(FIRST_THREAD_IN_PROC(p)->td_sigmask);
5784093529dSJeff Roberson 	/*
579df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
580df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
581df8bae1dSRodney W. Grimes 	 */
582645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
583645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
584645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
5853b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
58680e907a1SPeter Wemm 	/*
58780e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
58880e907a1SPeter Wemm 	 */
589ba1551caSIan Dowse 	p->p_procsig->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
590c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
591c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
592df8bae1dSRodney W. Grimes }
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes /*
595628d2653SJohn Baldwin  * do_sigprocmask()
5967c8fdcbdSMatthew Dillon  *
597628d2653SJohn Baldwin  *	Manipulate signal mask.
598df8bae1dSRodney W. Grimes  */
599a447cd8bSJeff Roberson static int
6004093529dSJeff Roberson do_sigprocmask(td, how, set, oset, old)
6014093529dSJeff Roberson 	struct thread *td;
6022c42a146SMarcel Moolenaar 	int how;
6032c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
604645682fdSLuoqi Chen 	int old;
6052c42a146SMarcel Moolenaar {
6062c42a146SMarcel Moolenaar 	int error;
6072c42a146SMarcel Moolenaar 
6084093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
6092c42a146SMarcel Moolenaar 	if (oset != NULL)
6104093529dSJeff Roberson 		*oset = td->td_sigmask;
6112c42a146SMarcel Moolenaar 
6122c42a146SMarcel Moolenaar 	error = 0;
6132c42a146SMarcel Moolenaar 	if (set != NULL) {
6142c42a146SMarcel Moolenaar 		switch (how) {
6152c42a146SMarcel Moolenaar 		case SIG_BLOCK:
616645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6174093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
6182c42a146SMarcel Moolenaar 			break;
6192c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6204093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
6214093529dSJeff Roberson 			signotify(td);
6222c42a146SMarcel Moolenaar 			break;
6232c42a146SMarcel Moolenaar 		case SIG_SETMASK:
624645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
625645682fdSLuoqi Chen 			if (old)
6264093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
627645682fdSLuoqi Chen 			else
6284093529dSJeff Roberson 				td->td_sigmask = *set;
6294093529dSJeff Roberson 			signotify(td);
6302c42a146SMarcel Moolenaar 			break;
6312c42a146SMarcel Moolenaar 		default:
6322c42a146SMarcel Moolenaar 			error = EINVAL;
6332c42a146SMarcel Moolenaar 			break;
6342c42a146SMarcel Moolenaar 		}
6352c42a146SMarcel Moolenaar 	}
6364093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
6372c42a146SMarcel Moolenaar 	return (error);
6382c42a146SMarcel Moolenaar }
6392c42a146SMarcel Moolenaar 
6407c8fdcbdSMatthew Dillon /*
641b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
6427c8fdcbdSMatthew Dillon  */
6437c8fdcbdSMatthew Dillon 
644d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
645df8bae1dSRodney W. Grimes struct sigprocmask_args {
646df8bae1dSRodney W. Grimes 	int	how;
6472c42a146SMarcel Moolenaar 	const sigset_t *set;
6482c42a146SMarcel Moolenaar 	sigset_t *oset;
649df8bae1dSRodney W. Grimes };
650d2d3e875SBruce Evans #endif
65126f9a767SRodney W. Grimes int
652b40ce416SJulian Elischer sigprocmask(td, uap)
653b40ce416SJulian Elischer 	register struct thread *td;
654df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
655df8bae1dSRodney W. Grimes {
6562c42a146SMarcel Moolenaar 	sigset_t set, oset;
6572c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6582c42a146SMarcel Moolenaar 	int error;
659df8bae1dSRodney W. Grimes 
6606f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6616f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6622c42a146SMarcel Moolenaar 	if (setp) {
6636f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6642c42a146SMarcel Moolenaar 		if (error)
6652c42a146SMarcel Moolenaar 			return (error);
666df8bae1dSRodney W. Grimes 	}
6674093529dSJeff Roberson 	error = do_sigprocmask(td, uap->how, setp, osetp, 0);
6682c42a146SMarcel Moolenaar 	if (osetp && !error) {
6696f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
6702c42a146SMarcel Moolenaar 	}
6712c42a146SMarcel Moolenaar 	return (error);
6722c42a146SMarcel Moolenaar }
6732c42a146SMarcel Moolenaar 
67431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6757c8fdcbdSMatthew Dillon /*
6767c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
6777c8fdcbdSMatthew Dillon  */
6782c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6792c42a146SMarcel Moolenaar struct osigprocmask_args {
6802c42a146SMarcel Moolenaar 	int	how;
6812c42a146SMarcel Moolenaar 	osigset_t mask;
6822c42a146SMarcel Moolenaar };
6832c42a146SMarcel Moolenaar #endif
6842c42a146SMarcel Moolenaar int
685b40ce416SJulian Elischer osigprocmask(td, uap)
686b40ce416SJulian Elischer 	register struct thread *td;
6872c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
6882c42a146SMarcel Moolenaar {
6892c42a146SMarcel Moolenaar 	sigset_t set, oset;
6902c42a146SMarcel Moolenaar 	int error;
6912c42a146SMarcel Moolenaar 
6922c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6934093529dSJeff Roberson 	error = do_sigprocmask(td, uap->how, &set, &oset, 1);
694b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
695df8bae1dSRodney W. Grimes 	return (error);
696df8bae1dSRodney W. Grimes }
69731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
698df8bae1dSRodney W. Grimes 
699d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
700df8bae1dSRodney W. Grimes struct sigpending_args {
701a447cd8bSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
7022c42a146SMarcel Moolenaar 	sigset_t	*set;
703df8bae1dSRodney W. Grimes };
704d2d3e875SBruce Evans #endif
705fb99ab88SMatthew Dillon /*
706fb99ab88SMatthew Dillon  * MPSAFE
707fb99ab88SMatthew Dillon  */
70826f9a767SRodney W. Grimes int
709a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
710a447cd8bSJeff Roberson {
711a447cd8bSJeff Roberson 	siginfo_t info;
712a447cd8bSJeff Roberson 	sigset_t set;
713a447cd8bSJeff Roberson 	int error;
714a447cd8bSJeff Roberson 
715a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
716a447cd8bSJeff Roberson 	if (error)
717a447cd8bSJeff Roberson 		return (error);
718a447cd8bSJeff Roberson 
719a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
720a447cd8bSJeff Roberson 	if (error)
721a447cd8bSJeff Roberson 		return (error);
722a447cd8bSJeff Roberson 
723a447cd8bSJeff Roberson 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
724a447cd8bSJeff Roberson 	/* Repost if we got an error. */
725a447cd8bSJeff Roberson 	if (error && info.si_signo)
726a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
727a447cd8bSJeff Roberson 
728a447cd8bSJeff Roberson 	return (error);
729a447cd8bSJeff Roberson }
730a447cd8bSJeff Roberson /*
731a447cd8bSJeff Roberson  * MPSAFE
732a447cd8bSJeff Roberson  */
733a447cd8bSJeff Roberson int
734a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
735a447cd8bSJeff Roberson {
736a447cd8bSJeff Roberson 	struct timespec ts;
737a447cd8bSJeff Roberson 	struct timespec *timeout;
738a447cd8bSJeff Roberson 	sigset_t set;
739a447cd8bSJeff Roberson 	siginfo_t info;
740a447cd8bSJeff Roberson 	int error;
741a447cd8bSJeff Roberson 
742a447cd8bSJeff Roberson 	if (uap->timeout) {
743a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
744a447cd8bSJeff Roberson 		if (error)
745a447cd8bSJeff Roberson 			return (error);
746a447cd8bSJeff Roberson 
747a447cd8bSJeff Roberson 		timeout = &ts;
748a447cd8bSJeff Roberson 	} else
749a447cd8bSJeff Roberson 		timeout = NULL;
750a447cd8bSJeff Roberson 
751a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
752a447cd8bSJeff Roberson 	if (error)
753a447cd8bSJeff Roberson 		return (error);
754a447cd8bSJeff Roberson 
755a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, timeout);
756a447cd8bSJeff Roberson 	if (error)
757a447cd8bSJeff Roberson 		return (error);
758a447cd8bSJeff Roberson 
759a447cd8bSJeff Roberson 	error = copyout(&info, uap->info, sizeof(info));
760a447cd8bSJeff Roberson 	/* Repost if we got an error. */
761a447cd8bSJeff Roberson 	if (error && info.si_signo)
762a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
763a447cd8bSJeff Roberson 
764a447cd8bSJeff Roberson 	return (error);
765a447cd8bSJeff Roberson }
766a447cd8bSJeff Roberson 
767a447cd8bSJeff Roberson /*
768a447cd8bSJeff Roberson  * MPSAFE
769a447cd8bSJeff Roberson  */
770a447cd8bSJeff Roberson int
771a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
772a447cd8bSJeff Roberson {
773a447cd8bSJeff Roberson 	siginfo_t info;
774a447cd8bSJeff Roberson 	sigset_t set;
775a447cd8bSJeff Roberson 	int error;
776a447cd8bSJeff Roberson 
777a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
778a447cd8bSJeff Roberson 	if (error)
779a447cd8bSJeff Roberson 		return (error);
780a447cd8bSJeff Roberson 
781a447cd8bSJeff Roberson 	error = kern_sigtimedwait(td, set, &info, NULL);
782a447cd8bSJeff Roberson 	if (error)
783a447cd8bSJeff Roberson 		return (error);
784a447cd8bSJeff Roberson 
785a447cd8bSJeff Roberson 	error = copyout(&info, uap->info, sizeof(info));
786a447cd8bSJeff Roberson 	/* Repost if we got an error. */
787a447cd8bSJeff Roberson 	if (error && info.si_signo)
788a447cd8bSJeff Roberson 		tdsignal(td, info.si_signo);
789a447cd8bSJeff Roberson 
790a447cd8bSJeff Roberson 	return (error);
791a447cd8bSJeff Roberson }
792a447cd8bSJeff Roberson 
793a447cd8bSJeff Roberson static int
794a447cd8bSJeff Roberson kern_sigtimedwait(struct thread *td, sigset_t set, siginfo_t *info,
795a447cd8bSJeff Roberson     struct timespec *timeout)
796a447cd8bSJeff Roberson {
797a447cd8bSJeff Roberson 	register struct sigacts *ps;
798a447cd8bSJeff Roberson 	sigset_t oldmask;
799a447cd8bSJeff Roberson 	struct proc *p;
800a447cd8bSJeff Roberson 	int error;
801a447cd8bSJeff Roberson 	int sig;
802a447cd8bSJeff Roberson 	int hz;
803a447cd8bSJeff Roberson 
804a447cd8bSJeff Roberson 	p = td->td_proc;
805a447cd8bSJeff Roberson 	error = 0;
806a447cd8bSJeff Roberson 	sig = 0;
807a447cd8bSJeff Roberson 	SIG_CANTMASK(set);
808a447cd8bSJeff Roberson 
809a447cd8bSJeff Roberson 	mtx_lock(&Giant);
810a447cd8bSJeff Roberson 	PROC_LOCK(p);
811a447cd8bSJeff Roberson 
812a447cd8bSJeff Roberson 	ps = p->p_sigacts;
813a447cd8bSJeff Roberson 	oldmask = td->td_sigmask;
814a447cd8bSJeff Roberson 	td->td_sigmask = set;
815a447cd8bSJeff Roberson 	signotify(td);
816a447cd8bSJeff Roberson 
817a447cd8bSJeff Roberson 	sig = cursig(td);
818a447cd8bSJeff Roberson 	if (sig)
819a447cd8bSJeff Roberson 		goto out;
820a447cd8bSJeff Roberson 
821a447cd8bSJeff Roberson 	/*
822a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
823a447cd8bSJeff Roberson 	 * signals.
824a447cd8bSJeff Roberson 	 */
825a447cd8bSJeff Roberson 	if (timeout) {
826a447cd8bSJeff Roberson 		struct timeval tv;
827a447cd8bSJeff Roberson 
828a447cd8bSJeff Roberson 		if (timeout->tv_nsec > 1000000000) {
829a447cd8bSJeff Roberson 			error = EINVAL;
830a447cd8bSJeff Roberson 			goto out;
831a447cd8bSJeff Roberson 		}
832a447cd8bSJeff Roberson 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
833a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
834a447cd8bSJeff Roberson 	} else
835a447cd8bSJeff Roberson 		hz = 0;
836a447cd8bSJeff Roberson 
837a447cd8bSJeff Roberson 	error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", hz);
838a447cd8bSJeff Roberson 	if (error == EINTR)
839a447cd8bSJeff Roberson 		error = 0;
840a447cd8bSJeff Roberson 	else if (error)
841a447cd8bSJeff Roberson 		goto out;
842a447cd8bSJeff Roberson 
843a447cd8bSJeff Roberson 	sig = cursig(td);
844a447cd8bSJeff Roberson out:
845a447cd8bSJeff Roberson 	td->td_sigmask = oldmask;
846a447cd8bSJeff Roberson 	if (sig) {
847a447cd8bSJeff Roberson 		sig_t action;
848a447cd8bSJeff Roberson 
849a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
850a447cd8bSJeff Roberson #ifdef KTRACE
851a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
852a447cd8bSJeff Roberson 			ktrpsig(sig, action, td->td_flags & TDF_OLDMASK ?
853a447cd8bSJeff Roberson 			    &td->td_oldsigmask : &td->td_sigmask, 0);
854a447cd8bSJeff Roberson #endif
855a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
856a447cd8bSJeff Roberson 
857a447cd8bSJeff Roberson 		if (action == SIG_DFL)
858a447cd8bSJeff Roberson 			sigexit(td, sig);
859a447cd8bSJeff Roberson 			/* NOTREACHED */
860a447cd8bSJeff Roberson 
861a447cd8bSJeff Roberson 		SIGDELSET(td->td_siglist, sig);
862a447cd8bSJeff Roberson 		info->si_signo = sig;
863a447cd8bSJeff Roberson 		info->si_code = 0;
864a447cd8bSJeff Roberson 	}
865a447cd8bSJeff Roberson 
866a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
867a447cd8bSJeff Roberson 	mtx_unlock(&Giant);
868a447cd8bSJeff Roberson 
869a447cd8bSJeff Roberson 	return (error);
870a447cd8bSJeff Roberson }
871a447cd8bSJeff Roberson 
872a447cd8bSJeff Roberson /*
873a447cd8bSJeff Roberson  * MPSAFE
874a447cd8bSJeff Roberson  */
875a447cd8bSJeff Roberson int
876b40ce416SJulian Elischer sigpending(td, uap)
877b40ce416SJulian Elischer 	struct thread *td;
878df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
879df8bae1dSRodney W. Grimes {
880b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
881628d2653SJohn Baldwin 	sigset_t siglist;
882df8bae1dSRodney W. Grimes 
883628d2653SJohn Baldwin 	PROC_LOCK(p);
8841d9c5696SJuli Mallett 	siglist = p->p_siglist;
8854093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
886628d2653SJohn Baldwin 	PROC_UNLOCK(p);
88748e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
8882c42a146SMarcel Moolenaar }
8892c42a146SMarcel Moolenaar 
89031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8912c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8922c42a146SMarcel Moolenaar struct osigpending_args {
8932c42a146SMarcel Moolenaar 	int	dummy;
8942c42a146SMarcel Moolenaar };
8952c42a146SMarcel Moolenaar #endif
896fb99ab88SMatthew Dillon /*
897fb99ab88SMatthew Dillon  * MPSAFE
898fb99ab88SMatthew Dillon  */
8992c42a146SMarcel Moolenaar int
900b40ce416SJulian Elischer osigpending(td, uap)
901b40ce416SJulian Elischer 	struct thread *td;
9022c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
9032c42a146SMarcel Moolenaar {
904b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9054093529dSJeff Roberson 	sigset_t siglist;
906b40ce416SJulian Elischer 
907628d2653SJohn Baldwin 	PROC_LOCK(p);
9084093529dSJeff Roberson 	siglist = p->p_siglist;
9094093529dSJeff Roberson 	SIGSETOR(siglist, td->td_siglist);
9104093529dSJeff Roberson 	SIG2OSIG(siglist, td->td_retval[0]);
911628d2653SJohn Baldwin 	PROC_UNLOCK(p);
912df8bae1dSRodney W. Grimes 	return (0);
913df8bae1dSRodney W. Grimes }
91431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
915df8bae1dSRodney W. Grimes 
916df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
917df8bae1dSRodney W. Grimes /*
918df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
919df8bae1dSRodney W. Grimes  */
920d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
921df8bae1dSRodney W. Grimes struct osigvec_args {
922df8bae1dSRodney W. Grimes 	int	signum;
923df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
924df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
925df8bae1dSRodney W. Grimes };
926d2d3e875SBruce Evans #endif
927fb99ab88SMatthew Dillon /*
928fb99ab88SMatthew Dillon  * MPSAFE
929fb99ab88SMatthew Dillon  */
930df8bae1dSRodney W. Grimes /* ARGSUSED */
93126f9a767SRodney W. Grimes int
932b40ce416SJulian Elischer osigvec(td, uap)
933b40ce416SJulian Elischer 	struct thread *td;
934df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
935df8bae1dSRodney W. Grimes {
936df8bae1dSRodney W. Grimes 	struct sigvec vec;
9372c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
9382c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
9392c42a146SMarcel Moolenaar 	int error;
940df8bae1dSRodney W. Grimes 
9416f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
9426f841fb7SMarcel Moolenaar 		return (EINVAL);
9436f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
9446f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
9452c42a146SMarcel Moolenaar 	if (nsap) {
9466f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
9472c42a146SMarcel Moolenaar 		if (error)
948df8bae1dSRodney W. Grimes 			return (error);
9492c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
9502c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
9512c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
9522c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
953df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
9542c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
955df8bae1dSRodney W. Grimes #endif
956df8bae1dSRodney W. Grimes 	}
957fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
9588f19eb88SIan Dowse 	error = kern_sigaction(td, uap->signum, nsap, osap, 1);
959fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
9602c42a146SMarcel Moolenaar 	if (osap && !error) {
9612c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
9622c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
9632c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
9642c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
9652c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
9662c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
9672c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
9682c42a146SMarcel Moolenaar #endif
9696f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
9702c42a146SMarcel Moolenaar 	}
9712c42a146SMarcel Moolenaar 	return (error);
972df8bae1dSRodney W. Grimes }
973df8bae1dSRodney W. Grimes 
974d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
975df8bae1dSRodney W. Grimes struct osigblock_args {
976df8bae1dSRodney W. Grimes 	int	mask;
977df8bae1dSRodney W. Grimes };
978d2d3e875SBruce Evans #endif
979fb99ab88SMatthew Dillon /*
980fb99ab88SMatthew Dillon  * MPSAFE
981fb99ab88SMatthew Dillon  */
98226f9a767SRodney W. Grimes int
983b40ce416SJulian Elischer osigblock(td, uap)
984b40ce416SJulian Elischer 	register struct thread *td;
985df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
986df8bae1dSRodney W. Grimes {
987b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9882c42a146SMarcel Moolenaar 	sigset_t set;
989df8bae1dSRodney W. Grimes 
9902c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
9912c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
992fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
993628d2653SJohn Baldwin 	PROC_LOCK(p);
9944093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
9954093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
996628d2653SJohn Baldwin 	PROC_UNLOCK(p);
997fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
998df8bae1dSRodney W. Grimes 	return (0);
999df8bae1dSRodney W. Grimes }
1000df8bae1dSRodney W. Grimes 
1001d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1002df8bae1dSRodney W. Grimes struct osigsetmask_args {
1003df8bae1dSRodney W. Grimes 	int	mask;
1004df8bae1dSRodney W. Grimes };
1005d2d3e875SBruce Evans #endif
1006fb99ab88SMatthew Dillon /*
1007fb99ab88SMatthew Dillon  * MPSAFE
1008fb99ab88SMatthew Dillon  */
100926f9a767SRodney W. Grimes int
1010b40ce416SJulian Elischer osigsetmask(td, uap)
1011b40ce416SJulian Elischer 	struct thread *td;
1012df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1013df8bae1dSRodney W. Grimes {
1014b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10152c42a146SMarcel Moolenaar 	sigset_t set;
1016df8bae1dSRodney W. Grimes 
10172c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
10182c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1019fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1020628d2653SJohn Baldwin 	PROC_LOCK(p);
10214093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
10224093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
10234093529dSJeff Roberson 	signotify(td);
1024628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1025fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1026df8bae1dSRodney W. Grimes 	return (0);
1027df8bae1dSRodney W. Grimes }
1028df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes /*
1031df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
1032df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
1033df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
1034b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
1035b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
1036b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
1037df8bae1dSRodney W. Grimes  */
1038d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1039df8bae1dSRodney W. Grimes struct sigsuspend_args {
10402c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1041df8bae1dSRodney W. Grimes };
1042d2d3e875SBruce Evans #endif
1043fb99ab88SMatthew Dillon /*
1044fb99ab88SMatthew Dillon  * MPSAFE
1045fb99ab88SMatthew Dillon  */
1046df8bae1dSRodney W. Grimes /* ARGSUSED */
104726f9a767SRodney W. Grimes int
1048b40ce416SJulian Elischer sigsuspend(td, uap)
1049b40ce416SJulian Elischer 	struct thread *td;
1050df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1051df8bae1dSRodney W. Grimes {
10522c42a146SMarcel Moolenaar 	sigset_t mask;
10532c42a146SMarcel Moolenaar 	int error;
10542c42a146SMarcel Moolenaar 
10556f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
10562c42a146SMarcel Moolenaar 	if (error)
10572c42a146SMarcel Moolenaar 		return (error);
10588f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
10598f19eb88SIan Dowse }
10608f19eb88SIan Dowse 
10618f19eb88SIan Dowse int
10628f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
10638f19eb88SIan Dowse {
10648f19eb88SIan Dowse 	struct proc *p = td->td_proc;
10658f19eb88SIan Dowse 	register struct sigacts *ps;
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes 	/*
1068645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1069df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1070df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1071df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1072df8bae1dSRodney W. Grimes 	 * to indicate this.
1073df8bae1dSRodney W. Grimes 	 */
1074fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1075628d2653SJohn Baldwin 	PROC_LOCK(p);
1076628d2653SJohn Baldwin 	ps = p->p_sigacts;
10774093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
10784093529dSJeff Roberson 	mtx_lock_spin(&sched_lock);
10794093529dSJeff Roberson 	td->td_flags |= TDF_OLDMASK;
10804093529dSJeff Roberson 	mtx_unlock_spin(&sched_lock);
1081645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
10824093529dSJeff Roberson 	td->td_sigmask = mask;
10834093529dSJeff Roberson 	signotify(td);
108401609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
10852c42a146SMarcel Moolenaar 		/* void */;
1086628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1087fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
10882c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
10892c42a146SMarcel Moolenaar 	return (EINTR);
10902c42a146SMarcel Moolenaar }
10912c42a146SMarcel Moolenaar 
109231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
10932c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
10942c42a146SMarcel Moolenaar struct osigsuspend_args {
10952c42a146SMarcel Moolenaar 	osigset_t mask;
10962c42a146SMarcel Moolenaar };
10972c42a146SMarcel Moolenaar #endif
1098fb99ab88SMatthew Dillon /*
1099fb99ab88SMatthew Dillon  * MPSAFE
1100fb99ab88SMatthew Dillon  */
11012c42a146SMarcel Moolenaar /* ARGSUSED */
11022c42a146SMarcel Moolenaar int
1103b40ce416SJulian Elischer osigsuspend(td, uap)
1104b40ce416SJulian Elischer 	struct thread *td;
11052c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
11062c42a146SMarcel Moolenaar {
1107b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1108645682fdSLuoqi Chen 	sigset_t mask;
1109628d2653SJohn Baldwin 	register struct sigacts *ps;
11102c42a146SMarcel Moolenaar 
1111fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1112628d2653SJohn Baldwin 	PROC_LOCK(p);
1113628d2653SJohn Baldwin 	ps = p->p_sigacts;
11144093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
11154093529dSJeff Roberson 	mtx_lock_spin(&sched_lock);
11164093529dSJeff Roberson 	td->td_flags |= TDF_OLDMASK;
11174093529dSJeff Roberson 	mtx_unlock_spin(&sched_lock);
1118645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1119645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
11204093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
11214093529dSJeff Roberson 	signotify(td);
112201609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1123df8bae1dSRodney W. Grimes 		/* void */;
1124628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1125fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1126df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1127df8bae1dSRodney W. Grimes 	return (EINTR);
1128df8bae1dSRodney W. Grimes }
112931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1130df8bae1dSRodney W. Grimes 
1131df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1132d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1133df8bae1dSRodney W. Grimes struct osigstack_args {
1134df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1135df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1136df8bae1dSRodney W. Grimes };
1137d2d3e875SBruce Evans #endif
1138fb99ab88SMatthew Dillon /*
1139fb99ab88SMatthew Dillon  * MPSAFE
1140fb99ab88SMatthew Dillon  */
1141df8bae1dSRodney W. Grimes /* ARGSUSED */
114226f9a767SRodney W. Grimes int
1143b40ce416SJulian Elischer osigstack(td, uap)
1144b40ce416SJulian Elischer 	struct thread *td;
1145df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1146df8bae1dSRodney W. Grimes {
1147b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1148df8bae1dSRodney W. Grimes 	struct sigstack ss;
1149fb99ab88SMatthew Dillon 	int error = 0;
1150fb99ab88SMatthew Dillon 
1151fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1152df8bae1dSRodney W. Grimes 
1153d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
1154628d2653SJohn Baldwin 		PROC_LOCK(p);
1155645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
1156b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
1157628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1158d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
1159d034d459SMarcel Moolenaar 		if (error)
1160fb99ab88SMatthew Dillon 			goto done2;
1161d034d459SMarcel Moolenaar 	}
1162d034d459SMarcel Moolenaar 
1163d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
1164d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
1165fb99ab88SMatthew Dillon 			goto done2;
1166628d2653SJohn Baldwin 		PROC_LOCK(p);
1167645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
1168645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
1169645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
1170645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
1171628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1172df8bae1dSRodney W. Grimes 	}
1173fb99ab88SMatthew Dillon done2:
1174fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1175fb99ab88SMatthew Dillon 	return (error);
1176df8bae1dSRodney W. Grimes }
1177df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1178df8bae1dSRodney W. Grimes 
1179d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1180df8bae1dSRodney W. Grimes struct sigaltstack_args {
11812c42a146SMarcel Moolenaar 	stack_t	*ss;
11822c42a146SMarcel Moolenaar 	stack_t	*oss;
1183df8bae1dSRodney W. Grimes };
1184d2d3e875SBruce Evans #endif
1185fb99ab88SMatthew Dillon /*
1186fb99ab88SMatthew Dillon  * MPSAFE
1187fb99ab88SMatthew Dillon  */
1188df8bae1dSRodney W. Grimes /* ARGSUSED */
118926f9a767SRodney W. Grimes int
1190b40ce416SJulian Elischer sigaltstack(td, uap)
1191b40ce416SJulian Elischer 	struct thread *td;
1192df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1193df8bae1dSRodney W. Grimes {
11948f19eb88SIan Dowse 	stack_t ss, oss;
11958f19eb88SIan Dowse 	int error;
11968f19eb88SIan Dowse 
11978f19eb88SIan Dowse 	if (uap->ss != NULL) {
11988f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
11998f19eb88SIan Dowse 		if (error)
12008f19eb88SIan Dowse 			return (error);
12018f19eb88SIan Dowse 	}
12028f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
12038f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
12048f19eb88SIan Dowse 	if (error)
12058f19eb88SIan Dowse 		return (error);
12068f19eb88SIan Dowse 	if (uap->oss != NULL)
12078f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
12088f19eb88SIan Dowse 	return (error);
12098f19eb88SIan Dowse }
12108f19eb88SIan Dowse 
12118f19eb88SIan Dowse int
12128f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
12138f19eb88SIan Dowse {
1214b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1215fb99ab88SMatthew Dillon 	int oonstack;
1216fb99ab88SMatthew Dillon 	int error = 0;
1217fb99ab88SMatthew Dillon 
1218fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1219df8bae1dSRodney W. Grimes 
1220b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1221d034d459SMarcel Moolenaar 
12228f19eb88SIan Dowse 	if (oss != NULL) {
1223628d2653SJohn Baldwin 		PROC_LOCK(p);
12248f19eb88SIan Dowse 		*oss = p->p_sigstk;
12258f19eb88SIan Dowse 		oss->ss_flags = (p->p_flag & P_ALTSTACK)
1226d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1227628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1228df8bae1dSRodney W. Grimes 	}
1229d034d459SMarcel Moolenaar 
12308f19eb88SIan Dowse 	if (ss != NULL) {
1231fb99ab88SMatthew Dillon 		if (oonstack) {
1232fb99ab88SMatthew Dillon 			error = EPERM;
1233fb99ab88SMatthew Dillon 			goto done2;
1234fb99ab88SMatthew Dillon 		}
12358f19eb88SIan Dowse 		if ((ss->ss_flags & ~SS_DISABLE) != 0) {
1236fb99ab88SMatthew Dillon 			error = EINVAL;
1237fb99ab88SMatthew Dillon 			goto done2;
1238fb99ab88SMatthew Dillon 		}
12398f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
12408f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1241fb99ab88SMatthew Dillon 				error = ENOMEM;
1242fb99ab88SMatthew Dillon 				goto done2;
1243fb99ab88SMatthew Dillon 			}
1244628d2653SJohn Baldwin 			PROC_LOCK(p);
12458f19eb88SIan Dowse 			p->p_sigstk = *ss;
1246d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
1247628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1248628d2653SJohn Baldwin 		} else {
1249628d2653SJohn Baldwin 			PROC_LOCK(p);
1250d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
1251628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1252628d2653SJohn Baldwin 		}
1253d034d459SMarcel Moolenaar 	}
1254fb99ab88SMatthew Dillon done2:
1255fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1256fb99ab88SMatthew Dillon 	return (error);
1257df8bae1dSRodney W. Grimes }
1258df8bae1dSRodney W. Grimes 
1259d93f860cSPoul-Henning Kamp /*
1260d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1261d93f860cSPoul-Henning Kamp  * cp is calling process.
1262d93f860cSPoul-Henning Kamp  */
126337c84183SPoul-Henning Kamp static int
12649c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
12659c1ab3e0SJohn Baldwin 	register struct thread *td;
12662c42a146SMarcel Moolenaar 	int sig, pgid, all;
1267d93f860cSPoul-Henning Kamp {
1268d93f860cSPoul-Henning Kamp 	register struct proc *p;
1269d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1270d93f860cSPoul-Henning Kamp 	int nfound = 0;
1271d93f860cSPoul-Henning Kamp 
1272553629ebSJake Burkholder 	if (all) {
1273d93f860cSPoul-Henning Kamp 		/*
1274d93f860cSPoul-Henning Kamp 		 * broadcast
1275d93f860cSPoul-Henning Kamp 		 */
12761005a129SJohn Baldwin 		sx_slock(&allproc_lock);
12772e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1278628d2653SJohn Baldwin 			PROC_LOCK(p);
12799c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
12809c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1281628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1282628d2653SJohn Baldwin 				continue;
1283628d2653SJohn Baldwin 			}
1284f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1285d93f860cSPoul-Henning Kamp 				nfound++;
128633a9ed9dSJohn Baldwin 				if (sig)
12872c42a146SMarcel Moolenaar 					psignal(p, sig);
1288628d2653SJohn Baldwin 			}
128933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1290d93f860cSPoul-Henning Kamp 		}
12911005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1292553629ebSJake Burkholder 	} else {
1293ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1294f591779bSSeigo Tanimura 		if (pgid == 0) {
1295d93f860cSPoul-Henning Kamp 			/*
1296d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1297d93f860cSPoul-Henning Kamp 			 */
12989c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1299f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1300f591779bSSeigo Tanimura 		} else {
1301d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1302f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1303ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1304d93f860cSPoul-Henning Kamp 				return (ESRCH);
1305d93f860cSPoul-Henning Kamp 			}
1306f591779bSSeigo Tanimura 		}
1307ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
13082e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1309628d2653SJohn Baldwin 			PROC_LOCK(p);
1310628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1311628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1312628d2653SJohn Baldwin 				continue;
1313628d2653SJohn Baldwin 			}
1314e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
131533a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1316628d2653SJohn Baldwin 				continue;
1317628d2653SJohn Baldwin 			}
1318f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1319d93f860cSPoul-Henning Kamp 				nfound++;
132033a9ed9dSJohn Baldwin 				if (sig)
13212c42a146SMarcel Moolenaar 					psignal(p, sig);
1322628d2653SJohn Baldwin 			}
132333a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1324d93f860cSPoul-Henning Kamp 		}
1325f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1326d93f860cSPoul-Henning Kamp 	}
1327d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1328d93f860cSPoul-Henning Kamp }
1329d93f860cSPoul-Henning Kamp 
1330d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1331df8bae1dSRodney W. Grimes struct kill_args {
1332df8bae1dSRodney W. Grimes 	int	pid;
1333df8bae1dSRodney W. Grimes 	int	signum;
1334df8bae1dSRodney W. Grimes };
1335d2d3e875SBruce Evans #endif
1336fb99ab88SMatthew Dillon /*
1337fb99ab88SMatthew Dillon  * MPSAFE
1338fb99ab88SMatthew Dillon  */
1339df8bae1dSRodney W. Grimes /* ARGSUSED */
134026f9a767SRodney W. Grimes int
1341b40ce416SJulian Elischer kill(td, uap)
1342b40ce416SJulian Elischer 	register struct thread *td;
1343df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1344df8bae1dSRodney W. Grimes {
1345df8bae1dSRodney W. Grimes 	register struct proc *p;
1346fb99ab88SMatthew Dillon 	int error = 0;
1347df8bae1dSRodney W. Grimes 
13486c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1349df8bae1dSRodney W. Grimes 		return (EINVAL);
1350fb99ab88SMatthew Dillon 
1351fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1352df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1353df8bae1dSRodney W. Grimes 		/* kill single process */
1354fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1355fb99ab88SMatthew Dillon 			error = ESRCH;
1356f44d9e24SJohn Baldwin 		} else if ((error = p_cansignal(td, p, uap->signum)) != 0) {
135733a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1358fb99ab88SMatthew Dillon 		} else {
135933a9ed9dSJohn Baldwin 			if (uap->signum)
1360df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1361628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1362fb99ab88SMatthew Dillon 			error = 0;
1363df8bae1dSRodney W. Grimes 		}
1364fb99ab88SMatthew Dillon 	} else {
1365df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1366df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
13679c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 1);
1368fb99ab88SMatthew Dillon 			break;
1369df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
13709c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 0);
1371fb99ab88SMatthew Dillon 			break;
1372df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
13739c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, -uap->pid, 0);
1374fb99ab88SMatthew Dillon 			break;
1375df8bae1dSRodney W. Grimes 		}
1376fb99ab88SMatthew Dillon 	}
1377fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1378fb99ab88SMatthew Dillon 	return(error);
1379df8bae1dSRodney W. Grimes }
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1382d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1383df8bae1dSRodney W. Grimes struct okillpg_args {
1384df8bae1dSRodney W. Grimes 	int	pgid;
1385df8bae1dSRodney W. Grimes 	int	signum;
1386df8bae1dSRodney W. Grimes };
1387d2d3e875SBruce Evans #endif
1388fb99ab88SMatthew Dillon /*
1389fb99ab88SMatthew Dillon  * MPSAFE
1390fb99ab88SMatthew Dillon  */
1391df8bae1dSRodney W. Grimes /* ARGSUSED */
139226f9a767SRodney W. Grimes int
1393b40ce416SJulian Elischer okillpg(td, uap)
1394b40ce416SJulian Elischer 	struct thread *td;
1395df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1396df8bae1dSRodney W. Grimes {
1397fb99ab88SMatthew Dillon 	int error;
1398df8bae1dSRodney W. Grimes 
13996c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1400df8bae1dSRodney W. Grimes 		return (EINVAL);
1401fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
14029c1ab3e0SJohn Baldwin 	error = killpg1(td, uap->signum, uap->pgid, 0);
1403fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1404fb99ab88SMatthew Dillon 	return (error);
1405df8bae1dSRodney W. Grimes }
1406df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1407df8bae1dSRodney W. Grimes 
1408df8bae1dSRodney W. Grimes /*
1409df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1410df8bae1dSRodney W. Grimes  */
1411df8bae1dSRodney W. Grimes void
14122c42a146SMarcel Moolenaar gsignal(pgid, sig)
14132c42a146SMarcel Moolenaar 	int pgid, sig;
1414df8bae1dSRodney W. Grimes {
1415df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1416df8bae1dSRodney W. Grimes 
1417f591779bSSeigo Tanimura 	if (pgid != 0) {
1418ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1419f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1420ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1421f591779bSSeigo Tanimura 		if (pgrp != NULL) {
14222c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1423f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1424f591779bSSeigo Tanimura 		}
1425f591779bSSeigo Tanimura 	}
1426df8bae1dSRodney W. Grimes }
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes /*
1429df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1430df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1431df8bae1dSRodney W. Grimes  */
1432df8bae1dSRodney W. Grimes void
14332c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1434df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
14352c42a146SMarcel Moolenaar 	int sig, checkctty;
1436df8bae1dSRodney W. Grimes {
1437df8bae1dSRodney W. Grimes 	register struct proc *p;
1438df8bae1dSRodney W. Grimes 
1439628d2653SJohn Baldwin 	if (pgrp) {
1440f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1441628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1442628d2653SJohn Baldwin 			PROC_LOCK(p);
1443df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
14442c42a146SMarcel Moolenaar 				psignal(p, sig);
1445628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1446628d2653SJohn Baldwin 		}
1447628d2653SJohn Baldwin 	}
1448df8bae1dSRodney W. Grimes }
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes /*
14511bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1452df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1453df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1454356861dbSMatthew Dillon  *
1455356861dbSMatthew Dillon  * MPSAFE
1456df8bae1dSRodney W. Grimes  */
1457df8bae1dSRodney W. Grimes void
14581bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code)
1459df8bae1dSRodney W. Grimes {
14601bf4700bSJeff Roberson 	struct sigacts *ps;
14611bf4700bSJeff Roberson 	struct proc *p;
14621bf4700bSJeff Roberson 
14631bf4700bSJeff Roberson 	p = td->td_proc;
1464df8bae1dSRodney W. Grimes 
1465628d2653SJohn Baldwin 	PROC_LOCK(p);
1466ef3dab76STim J. Robbins 	ps = p->p_sigacts;
14672c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
14684093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1469df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1470df8bae1dSRodney W. Grimes #ifdef KTRACE
1471374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1472374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
14734093529dSJeff Roberson 			    &td->td_sigmask, code);
1474df8bae1dSRodney W. Grimes #endif
1475a88b260aSJuli Mallett 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
14764093529dSJeff Roberson 						&td->td_sigmask, code);
14774093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
14782c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
14794093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
14802c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1481289ccde0SPeter Wemm 			/*
14828f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1483289ccde0SPeter Wemm 			 */
14842c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
14852c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
14862c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
14872c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
14882c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1489dedc04feSPeter Wemm 		}
14906f841fb7SMarcel Moolenaar 	} else {
14916626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
14922c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
14934093529dSJeff Roberson 		tdsignal(td, sig);
1494df8bae1dSRodney W. Grimes 	}
1495628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1496df8bae1dSRodney W. Grimes }
1497df8bae1dSRodney W. Grimes 
14984093529dSJeff Roberson static struct thread *
14994093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
15004093529dSJeff Roberson {
15014093529dSJeff Roberson 	struct thread *td;
15024093529dSJeff Roberson 
15034093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15044093529dSJeff Roberson 
15054093529dSJeff Roberson 	/*
15064093529dSJeff Roberson 	 * If we know the signal is bound for a specific thread then we
15074093529dSJeff Roberson 	 * assume that we are in that threads context.  This is the case
15084093529dSJeff Roberson 	 * for SIGXCPU, SIGILL, etc.  Otherwise someone did a kill() from
15094093529dSJeff Roberson 	 * userland and the real thread doesn't actually matter.
15104093529dSJeff Roberson 	 */
15114093529dSJeff Roberson 	if ((prop & SA_PROC) != 0 && curthread->td_proc == p)
15124093529dSJeff Roberson 		return (curthread);
15134093529dSJeff Roberson 
15144093529dSJeff Roberson 	/*
15154093529dSJeff Roberson 	 * We should search for the first thread that is blocked in
15164093529dSJeff Roberson 	 * sigsuspend with this signal unmasked.
15174093529dSJeff Roberson 	 */
15184093529dSJeff Roberson 
15194093529dSJeff Roberson 	/* XXX */
15204093529dSJeff Roberson 
15214093529dSJeff Roberson 	/*
15224093529dSJeff Roberson 	 * Find the first thread in the proc that doesn't have this signal
15234093529dSJeff Roberson 	 * masked.
15244093529dSJeff Roberson 	 */
15254093529dSJeff Roberson 	FOREACH_THREAD_IN_PROC(p, td)
15264093529dSJeff Roberson 		if (!SIGISMEMBER(td->td_sigmask, sig))
15274093529dSJeff Roberson 			return (td);
15284093529dSJeff Roberson 
15294093529dSJeff Roberson 	return (FIRST_THREAD_IN_PROC(p));
15304093529dSJeff Roberson }
15314093529dSJeff Roberson 
1532df8bae1dSRodney W. Grimes /*
1533df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1534df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1535df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1536df8bae1dSRodney W. Grimes  *
1537df8bae1dSRodney W. Grimes  * Exceptions:
1538df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1539df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1540df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1541df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1542df8bae1dSRodney W. Grimes  *
1543df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1544df8bae1dSRodney W. Grimes  */
1545df8bae1dSRodney W. Grimes void
15464093529dSJeff Roberson psignal(struct proc *p, int sig)
1547df8bae1dSRodney W. Grimes {
1548b40ce416SJulian Elischer 	struct thread *td;
15494093529dSJeff Roberson 	int prop;
15504093529dSJeff Roberson 
15514093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
15524093529dSJeff Roberson 	prop = sigprop(sig);
15534093529dSJeff Roberson 
15544093529dSJeff Roberson 	/*
15554093529dSJeff Roberson 	 * Find a thread to deliver the signal to.
15564093529dSJeff Roberson 	 */
15574093529dSJeff Roberson 	td = sigtd(p, sig, prop);
15584093529dSJeff Roberson 
15594093529dSJeff Roberson 	tdsignal(td, sig);
15604093529dSJeff Roberson }
15614093529dSJeff Roberson 
15624093529dSJeff Roberson void
15634093529dSJeff Roberson tdsignal(struct thread *td, int sig)
15644093529dSJeff Roberson {
15654093529dSJeff Roberson 	struct proc *p;
15664093529dSJeff Roberson 	register sig_t action;
15674093529dSJeff Roberson 	sigset_t *siglist;
15684093529dSJeff Roberson 	struct thread *td0;
1569e602ba25SJulian Elischer 	register int prop;
1570e602ba25SJulian Elischer 
1571df8bae1dSRodney W. Grimes 
15722899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
15734093529dSJeff Roberson 	    ("tdsignal(): invalid signal %d\n", sig));
15744093529dSJeff Roberson 
15754093529dSJeff Roberson 	p = td->td_proc;
15762c42a146SMarcel Moolenaar 
1577628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1578cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1579cb679c38SJonathan Lemon 
15802c42a146SMarcel Moolenaar 	prop = sigprop(sig);
15814093529dSJeff Roberson 
15824093529dSJeff Roberson 	/*
15834093529dSJeff Roberson 	 * If this thread is blocking this signal then we'll leave it in the
15844093529dSJeff Roberson 	 * proc so that we can find it in the first thread that unblocks it.
15854093529dSJeff Roberson 	 */
15864093529dSJeff Roberson 	if (SIGISMEMBER(td->td_sigmask, sig))
15874093529dSJeff Roberson 		siglist = &p->p_siglist;
15884093529dSJeff Roberson 	else
15894093529dSJeff Roberson 		siglist = &td->td_siglist;
15904093529dSJeff Roberson 
1591df8bae1dSRodney W. Grimes 	/*
15922a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
15932a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
15942a024a2bSSean Eric Fagan 	 * a chance, as well.
1595df8bae1dSRodney W. Grimes 	 */
1596b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1597df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1598b40ce416SJulian Elischer 	} else {
1599df8bae1dSRodney W. Grimes 		/*
1600df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1601df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1602df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1603df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1604df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1605df8bae1dSRodney W. Grimes 		 */
1606628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1607df8bae1dSRodney W. Grimes 			return;
16084093529dSJeff Roberson 		if (SIGISMEMBER(td->td_sigmask, sig))
1609df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
16102c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1611df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1612df8bae1dSRodney W. Grimes 		else
1613df8bae1dSRodney W. Grimes 			action = SIG_DFL;
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 */
1644df8bae1dSRodney W. Grimes 
1645df8bae1dSRodney W. Grimes 	/*
1646e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1647e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1648e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1649e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1650e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1651e602ba25SJulian Elischer 	 * We try do the per-process part here.
1652df8bae1dSRodney W. Grimes 	 */
1653e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1654e602ba25SJulian Elischer 		/*
1655e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1656e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1657e602ba25SJulian Elischer 		 */
1658e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1659e602ba25SJulian Elischer 			/*
1660e602ba25SJulian Elischer 			 * The traced process is already stopped,
1661e602ba25SJulian Elischer 			 * so no further action is necessary.
1662e602ba25SJulian Elischer 			 * No signal can restart us.
1663e602ba25SJulian Elischer 			 */
1664e602ba25SJulian Elischer 			goto out;
16651c32c37cSJohn Baldwin 		}
1666b40ce416SJulian Elischer 
1667e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1668df8bae1dSRodney W. Grimes 			/*
1669e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1670e602ba25SJulian Elischer 			 * It will die elsewhere.
1671e602ba25SJulian Elischer 			 * All threads must be restarted.
1672df8bae1dSRodney W. Grimes 			 */
1673e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1674e602ba25SJulian Elischer 			goto runfast;
1675e602ba25SJulian Elischer 		}
1676e602ba25SJulian Elischer 
1677e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1678e602ba25SJulian Elischer 			/*
1679e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
16804093529dSJeff Roberson 			 * process but don't leave the signal in siglist as
16811d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1682e602ba25SJulian Elischer 			 * continue the process and leave the signal in
16834093529dSJeff Roberson 			 * siglist.  If the process catches SIGCONT, let it
1684e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1685e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1686e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1687e602ba25SJulian Elischer 			 */
16881279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
16896933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1690e602ba25SJulian Elischer 			if (action == SIG_DFL) {
16914093529dSJeff Roberson 				SIGDELSET(*siglist, sig);
1692e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1693e602ba25SJulian Elischer 				/*
1694e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1695e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1696e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1697e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1698e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1699e602ba25SJulian Elischer 				 * process, we need to make sure that the
1700e602ba25SJulian Elischer 				 * single thread is runnable asap.
1701e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1702e602ba25SJulian Elischer 				 */
1703e602ba25SJulian Elischer 				goto runfast;
1704e602ba25SJulian Elischer 			}
1705e602ba25SJulian Elischer 			/*
1706e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1707e602ba25SJulian Elischer 			 */
1708e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
170904774f23SJulian Elischer 			thread_unsuspend(p);
1710e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1711e602ba25SJulian Elischer 			goto out;
1712e602ba25SJulian Elischer 		}
1713e602ba25SJulian Elischer 
1714e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1715e602ba25SJulian Elischer 			/*
1716e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1717e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
171804774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1719e602ba25SJulian Elischer 			 */
17201279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
17214093529dSJeff Roberson 			SIGDELSET(*siglist, sig);
1722e602ba25SJulian Elischer 			goto out;
1723e602ba25SJulian Elischer 		}
1724e602ba25SJulian Elischer 
1725e602ba25SJulian Elischer 		/*
1726e602ba25SJulian Elischer 		 * All other kinds of signals:
1727e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1728e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1729e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
173004774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1731e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1732e602ba25SJulian Elischer 		 */
1733e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
173471fad9fdSJulian Elischer 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1735e602ba25SJulian Elischer 			if (td->td_flags & TDF_CVWAITQ)
173671fad9fdSJulian Elischer 				cv_abort(td);
1737e602ba25SJulian Elischer 			else
173871fad9fdSJulian Elischer 				abortsleep(td);
1739e602ba25SJulian Elischer 		}
1740e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1741df8bae1dSRodney W. Grimes 		goto out;
1742df8bae1dSRodney W. Grimes 		/*
1743e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1744e602ba25SJulian Elischer 		 * Shouldn't they abort too?
174504774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
174604774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1747df8bae1dSRodney W. Grimes 		 */
1748e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
174935c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
175035c32a76SDavid Xu 			!(prop & SA_STOP)) {
1751721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
17524093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
1753721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1754df8bae1dSRodney W. Grimes 			goto out;
175504774f23SJulian Elischer 		}
175635c32a76SDavid Xu 		if (prop & SA_STOP) {
175735c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
175835c32a76SDavid Xu 				goto out;
1759e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
1760e574e444SDavid Xu 			p->p_xstat = sig;
176135c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
17624093529dSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, td0) {
17634093529dSJeff Roberson 				if (TD_IS_SLEEPING(td0) &&
176435c32a76SDavid Xu 					(td->td_flags & TDF_SINTR))
17654093529dSJeff Roberson 					thread_suspend_one(td0);
176635c32a76SDavid Xu 			}
1767e574e444SDavid Xu 			thread_stopped(p);
17684093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
1769e574e444SDavid Xu 				SIGDELSET(p->p_siglist, p->p_xstat);
17704093529dSJeff Roberson 				FOREACH_THREAD_IN_PROC(p, td0)
17714093529dSJeff Roberson 					SIGDELSET(td0->td_siglist, p->p_xstat);
17724093529dSJeff Roberson 			}
177335c32a76SDavid Xu 			mtx_unlock_spin(&sched_lock);
177435c32a76SDavid Xu 			goto out;
177535c32a76SDavid Xu 		}
1776721e5910SJulian Elischer 		else
1777df8bae1dSRodney W. Grimes 			goto runfast;
1778df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1779e602ba25SJulian Elischer 	} else {
1780e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
17814093529dSJeff Roberson 		SIGDELSET(*siglist, sig);
1782e602ba25SJulian Elischer 		goto out;
1783e602ba25SJulian Elischer 	}
1784e602ba25SJulian Elischer 
1785b40ce416SJulian Elischer 	/*
1786e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1787e602ba25SJulian Elischer 	 * running threads.
1788b40ce416SJulian Elischer 	 */
1789e602ba25SJulian Elischer 
1790e602ba25SJulian Elischer runfast:
1791aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
17924093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
1793e602ba25SJulian Elischer 	thread_unsuspend(p);
1794e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1795e602ba25SJulian Elischer out:
1796e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1797e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1798e602ba25SJulian Elischer }
1799e602ba25SJulian Elischer 
1800e602ba25SJulian Elischer /*
1801e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1802e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1803e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1804e602ba25SJulian Elischer  */
1805e602ba25SJulian Elischer static void
18064093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
1807e602ba25SJulian Elischer {
1808e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1809e602ba25SJulian Elischer 	register int prop;
1810e602ba25SJulian Elischer 
1811aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1812e602ba25SJulian Elischer 	prop = sigprop(sig);
1813e602ba25SJulian Elischer 	/*
1814aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1815e602ba25SJulian Elischer 	 * killed in this lifetime.
1816e602ba25SJulian Elischer 	 */
1817e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1818e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1819e602ba25SJulian Elischer 			td->td_priority = PUSER;
1820b40ce416SJulian Elischer 		}
1821b40ce416SJulian Elischer 	}
1822e602ba25SJulian Elischer 
1823e602ba25SJulian Elischer 	/*
1824e602ba25SJulian Elischer 	 * Defer further processing for signals which are held,
1825e602ba25SJulian Elischer 	 * except that stopped processes must be continued by SIGCONT.
1826e602ba25SJulian Elischer 	 */
1827e602ba25SJulian Elischer 	if (action == SIG_HOLD) {
1828aa0fa334SJulian Elischer 		return;
1829e602ba25SJulian Elischer 	}
183071fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1831e602ba25SJulian Elischer 		/*
1832e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1833e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1834e602ba25SJulian Elischer 		 * be noticed when the process returns through
1835e602ba25SJulian Elischer 		 * trap() or syscall().
1836e602ba25SJulian Elischer 		 */
1837e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1838aa0fa334SJulian Elischer 			return;
1839e602ba25SJulian Elischer 		}
1840e602ba25SJulian Elischer 		/*
1841e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1842e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1843e602ba25SJulian Elischer 		 * for its parent.
1844e602ba25SJulian Elischer 		 */
1845e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1846e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1847aa0fa334SJulian Elischer 		} else {
1848aa0fa334SJulian Elischer 
1849df8bae1dSRodney W. Grimes 			/*
1850e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1851e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1852e602ba25SJulian Elischer 			 * be awakened.
1853df8bae1dSRodney W. Grimes 			 */
1854e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
18551d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
18564093529dSJeff Roberson 				/*
18574093529dSJeff Roberson 				 * It may be on either list in this state.
18584093529dSJeff Roberson 				 * Remove from both for now.
18594093529dSJeff Roberson 				 */
18604093529dSJeff Roberson 				SIGDELSET(td->td_siglist, sig);
1861aa0fa334SJulian Elischer 				return;
1862df8bae1dSRodney W. Grimes 			}
1863df8bae1dSRodney W. Grimes 
1864aa0fa334SJulian Elischer 			/*
1865aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1866aa0fa334SJulian Elischer 			 */
1867aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
1868aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1869aa0fa334SJulian Elischer 			}
1870aa0fa334SJulian Elischer 		}
187171fad9fdSJulian Elischer 		if (td->td_flags & TDF_CVWAITQ)
187271fad9fdSJulian Elischer 			cv_abort(td);
187371fad9fdSJulian Elischer 		else
187471fad9fdSJulian Elischer 			abortsleep(td);
1875aa0fa334SJulian Elischer 	}
1876aa0fa334SJulian Elischer #ifdef SMP
1877aa0fa334SJulian Elischer 	  else {
1878df8bae1dSRodney W. Grimes 		/*
1879e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
1880df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1881df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1882df8bae1dSRodney W. Grimes 		 */
188371fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td) && td != curthread) {
1884e602ba25SJulian Elischer 			forward_signal(td);
1885aa0fa334SJulian Elischer 		}
18860ac3b636SAndrew Gallatin 	  }
18873163861cSTor Egge #endif
18886caa8a15SJohn Baldwin }
1889df8bae1dSRodney W. Grimes 
1890df8bae1dSRodney W. Grimes /*
1891df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1892df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1893df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1894df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1895df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1896628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
1897df8bae1dSRodney W. Grimes  * sequence is
1898df8bae1dSRodney W. Grimes  *
1899e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
19002c42a146SMarcel Moolenaar  *		postsig(sig);
1901df8bae1dSRodney W. Grimes  */
190226f9a767SRodney W. Grimes int
1903e602ba25SJulian Elischer issignal(td)
1904e602ba25SJulian Elischer 	struct thread *td;
1905df8bae1dSRodney W. Grimes {
1906e602ba25SJulian Elischer 	struct proc *p;
19074093529dSJeff Roberson 	sigset_t sigpending;
19082c42a146SMarcel Moolenaar 	register int sig, prop;
1909df8bae1dSRodney W. Grimes 
1910e602ba25SJulian Elischer 	p = td->td_proc;
1911628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
191226306795SJohn Baldwin 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &p->p_mtx.mtx_object,
191326306795SJohn Baldwin 	    "Checking for signals");
1914df8bae1dSRodney W. Grimes 	for (;;) {
19152a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
19162a024a2bSSean Eric Fagan 
19174093529dSJeff Roberson 		sigpending = td->td_siglist;
19184093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
19194093529dSJeff Roberson 
1920df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
19214093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
19224093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
1923df8bae1dSRodney W. Grimes 			return (0);
19244093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
19252c42a146SMarcel Moolenaar 		prop = sigprop(sig);
19262a024a2bSSean Eric Fagan 
1927628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
19282a024a2bSSean Eric Fagan 
1929df8bae1dSRodney W. Grimes 		/*
1930df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1931df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1932df8bae1dSRodney W. Grimes 		 */
19332c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
19344093529dSJeff Roberson 			SIGDELSET(td->td_siglist, sig);
1935df8bae1dSRodney W. Grimes 			continue;
1936df8bae1dSRodney W. Grimes 		}
1937df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1938df8bae1dSRodney W. Grimes 			/*
1939d8f4f6a4SJonathan Mini 			 * If traced, always stop.
1940df8bae1dSRodney W. Grimes 			 */
19412c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1942628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1943df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1944628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
19459ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
19464d492b43SJulian Elischer 			stop(p);	/* uses schedlock too eventually */
194771fad9fdSJulian Elischer 			thread_suspend_one(td);
1948c86b6ff5SJohn Baldwin 			PROC_UNLOCK(p);
1949c86b6ff5SJohn Baldwin 			DROP_GIANT();
19502ad7d304SJohn Baldwin 			p->p_stats->p_ru.ru_nivcsw++;
1951df8bae1dSRodney W. Grimes 			mi_switch();
19529ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
195320cdcc5bSJohn Baldwin 			PICKUP_GIANT();
1954628d2653SJohn Baldwin 			PROC_LOCK(p);
1955df8bae1dSRodney W. Grimes 
1956df8bae1dSRodney W. Grimes 			/*
1957df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1958df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1959df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1960df8bae1dSRodney W. Grimes 			 */
1961df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1962df8bae1dSRodney W. Grimes 				continue;
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 			/*
19754093529dSJeff Roberson 			 * Put the new signal into td_siglist.  If the
19761d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
1977df8bae1dSRodney W. Grimes 			 */
19784093529dSJeff Roberson 			SIGADDSET(td->td_siglist, sig);
19794093529dSJeff Roberson 			if (SIGISMEMBER(td->td_sigmask, sig))
1980df8bae1dSRodney W. Grimes 				continue;
19814093529dSJeff Roberson 			signotify(td);
1982df8bae1dSRodney W. Grimes 		}
1983df8bae1dSRodney W. Grimes 
1984df8bae1dSRodney W. Grimes 		/*
1985df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1986df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1987df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1988df8bae1dSRodney W. Grimes 		 */
1989d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1990df8bae1dSRodney W. Grimes 
1991d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
1992df8bae1dSRodney W. Grimes 			/*
1993df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1994df8bae1dSRodney W. Grimes 			 */
1995df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1996df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1997df8bae1dSRodney W. Grimes 				/*
1998df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1999df8bae1dSRodney W. Grimes 				 * in init? XXX
2000df8bae1dSRodney W. Grimes 				 */
2001d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
20022c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2003df8bae1dSRodney W. Grimes #endif
2004df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2005df8bae1dSRodney W. Grimes 			}
2006df8bae1dSRodney W. Grimes 			/*
2007df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2008df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2009df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2010df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2011df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2012df8bae1dSRodney W. Grimes 			 */
2013df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2014df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2015df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2016df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2017df8bae1dSRodney W. Grimes 					break;	/* == ignore */
2018e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
20192c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2020721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2021e574e444SDavid Xu 				thread_stopped(p);
202271fad9fdSJulian Elischer 				thread_suspend_one(td);
2023721e5910SJulian Elischer 				PROC_UNLOCK(p);
2024721e5910SJulian Elischer 				DROP_GIANT();
2025721e5910SJulian Elischer 				p->p_stats->p_ru.ru_nivcsw++;
2026721e5910SJulian Elischer 				mi_switch();
2027721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2028721e5910SJulian Elischer 				PICKUP_GIANT();
2029721e5910SJulian Elischer 				PROC_LOCK(p);
2030df8bae1dSRodney W. Grimes 				break;
203121b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2032df8bae1dSRodney W. Grimes 				/*
2033df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2034df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2035df8bae1dSRodney W. Grimes 				 */
2036df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2037df8bae1dSRodney W. Grimes 			} else
20382c42a146SMarcel Moolenaar 				return (sig);
2039df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2040df8bae1dSRodney W. Grimes 
2041d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2042df8bae1dSRodney W. Grimes 			/*
2043df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2044df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2045df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2046df8bae1dSRodney W. Grimes 			 */
2047df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2048df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2049df8bae1dSRodney W. Grimes 				printf("issignal\n");
2050df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2051df8bae1dSRodney W. Grimes 
2052df8bae1dSRodney W. Grimes 		default:
2053df8bae1dSRodney W. Grimes 			/*
2054df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2055df8bae1dSRodney W. Grimes 			 * postsig() process it.
2056df8bae1dSRodney W. Grimes 			 */
20572c42a146SMarcel Moolenaar 			return (sig);
2058df8bae1dSRodney W. Grimes 		}
20594093529dSJeff Roberson 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2060df8bae1dSRodney W. Grimes 	}
2061df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2062df8bae1dSRodney W. Grimes }
2063df8bae1dSRodney W. Grimes 
2064df8bae1dSRodney W. Grimes /*
2065df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
2066df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
20675b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
20685b3047d5SJohn Baldwin  * lock held.
2069df8bae1dSRodney W. Grimes  */
20705b3047d5SJohn Baldwin static void
2071e574e444SDavid Xu stop(struct proc *p)
2072df8bae1dSRodney W. Grimes {
2073df8bae1dSRodney W. Grimes 
2074628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
20751279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
2076df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
207701609114SAlfred Perlstein 	wakeup(p->p_pptr);
2078df8bae1dSRodney W. Grimes }
2079df8bae1dSRodney W. Grimes 
2080e574e444SDavid Xu void
2081e574e444SDavid Xu thread_stopped(struct proc *p)
2082e574e444SDavid Xu {
2083e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
2084e574e444SDavid Xu 	int n;
2085e574e444SDavid Xu 
2086e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2087e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2088e574e444SDavid Xu 	n = p->p_suspcount;
2089e574e444SDavid Xu 	if (p == p1)
2090e574e444SDavid Xu 		n++;
2091e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2092e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2093e574e444SDavid Xu 		stop(p);
2094e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
2095e574e444SDavid Xu 		if ((p->p_pptr->p_procsig->ps_flag &
2096e574e444SDavid Xu 			PS_NOCLDSTOP) == 0) {
2097e574e444SDavid Xu 			psignal(p->p_pptr, SIGCHLD);
2098e574e444SDavid Xu 		}
2099e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2100e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2101e574e444SDavid Xu 	}
2102e574e444SDavid Xu }
2103e574e444SDavid Xu 
2104df8bae1dSRodney W. Grimes /*
2105df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2106df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2107df8bae1dSRodney W. Grimes  */
2108df8bae1dSRodney W. Grimes void
21092c42a146SMarcel Moolenaar postsig(sig)
21102c42a146SMarcel Moolenaar 	register int sig;
2111df8bae1dSRodney W. Grimes {
2112b40ce416SJulian Elischer 	struct thread *td = curthread;
2113b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2114628d2653SJohn Baldwin 	struct sigacts *ps;
21152c42a146SMarcel Moolenaar 	sig_t action;
21162c42a146SMarcel Moolenaar 	sigset_t returnmask;
21172c42a146SMarcel Moolenaar 	int code;
2118df8bae1dSRodney W. Grimes 
21192c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
21205526d2d9SEivind Eklund 
21212ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2122628d2653SJohn Baldwin 	ps = p->p_sigacts;
21234093529dSJeff Roberson 	SIGDELSET(td->td_siglist, sig);
21242c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2125df8bae1dSRodney W. Grimes #ifdef KTRACE
2126374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
21274093529dSJeff Roberson 		ktrpsig(sig, action, td->td_flags & TDF_OLDMASK ?
21284093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2129df8bae1dSRodney W. Grimes #endif
2130628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
21312a024a2bSSean Eric Fagan 
2132df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
2133df8bae1dSRodney W. Grimes 		/*
2134df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2135df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2136df8bae1dSRodney W. Grimes 		 */
2137b40ce416SJulian Elischer 		sigexit(td, sig);
2138df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2139df8bae1dSRodney W. Grimes 	} else {
2140df8bae1dSRodney W. Grimes 		/*
2141df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2142df8bae1dSRodney W. Grimes 		 */
21434093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
21445526d2d9SEivind Eklund 		    ("postsig action"));
2145df8bae1dSRodney W. Grimes 		/*
2146df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2147645682fdSLuoqi Chen 		 * occurrences of this signal.
2148df8bae1dSRodney W. Grimes 		 *
2149645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2150df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2151645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2152df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2153df8bae1dSRodney W. Grimes 		 */
21544093529dSJeff Roberson 		mtx_lock_spin(&sched_lock);
21554093529dSJeff Roberson 		if (td->td_flags & TDF_OLDMASK) {
21564093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
21574093529dSJeff Roberson 			td->td_flags &= ~TDF_OLDMASK;
2158df8bae1dSRodney W. Grimes 		} else
21594093529dSJeff Roberson 			returnmask = td->td_sigmask;
21604093529dSJeff Roberson 		mtx_unlock_spin(&sched_lock);
21612c42a146SMarcel Moolenaar 
21624093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
21632c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
21644093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
21652c42a146SMarcel Moolenaar 
21662c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2167289ccde0SPeter Wemm 			/*
21688f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2169289ccde0SPeter Wemm 			 */
21702c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
21712c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
21722c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
21732c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
21742c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2175dedc04feSPeter Wemm 		}
2176df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
21772c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2178df8bae1dSRodney W. Grimes 			code = 0;
2179df8bae1dSRodney W. Grimes 		} else {
21806626c604SJulian Elischer 			code = p->p_code;
21816626c604SJulian Elischer 			p->p_code = 0;
21826626c604SJulian Elischer 			p->p_sig = 0;
2183df8bae1dSRodney W. Grimes 		}
2184ac2e4153SJulian Elischer 		if (p->p_flag & P_THREADED)
218558a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
218658a3c273SJeff Roberson 		else
218758a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
218858a3c273SJeff Roberson 			    &returnmask, code);
2189df8bae1dSRodney W. Grimes 	}
2190df8bae1dSRodney W. Grimes }
2191df8bae1dSRodney W. Grimes 
2192df8bae1dSRodney W. Grimes /*
2193df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2194df8bae1dSRodney W. Grimes  */
219526f9a767SRodney W. Grimes void
2196df8bae1dSRodney W. Grimes killproc(p, why)
2197df8bae1dSRodney W. Grimes 	struct proc *p;
2198df8bae1dSRodney W. Grimes 	char *why;
2199df8bae1dSRodney W. Grimes {
22009081e5e8SJohn Baldwin 
22019081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
22020384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
22030384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2204729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2205b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2206df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2207df8bae1dSRodney W. Grimes }
2208df8bae1dSRodney W. Grimes 
2209df8bae1dSRodney W. Grimes /*
2210df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2211df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2212df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2213df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2214df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2215df8bae1dSRodney W. Grimes  * does not return.
2216df8bae1dSRodney W. Grimes  */
221726f9a767SRodney W. Grimes void
2218b40ce416SJulian Elischer sigexit(td, sig)
2219b40ce416SJulian Elischer 	struct thread *td;
22202c42a146SMarcel Moolenaar 	int sig;
2221df8bae1dSRodney W. Grimes {
2222b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2223df8bae1dSRodney W. Grimes 
2224628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2225df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
22262c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
22272c42a146SMarcel Moolenaar 		p->p_sig = sig;
2228c364e17eSAndrey A. Chernov 		/*
2229c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2230c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2231c364e17eSAndrey A. Chernov 		 * these messages.)
2232c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
2233c364e17eSAndrey A. Chernov 		 */
2234628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2235c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
2236c31146a1SJohn Baldwin 			mtx_lock(&Giant);
2237b40ce416SJulian Elischer 		if (coredump(td) == 0)
22382c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
223957308494SJoerg Wunsch 		if (kern_logsigexit)
224057308494SJoerg Wunsch 			log(LOG_INFO,
224157308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
22423d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
22439c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
22442c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
22452c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
2246c31146a1SJohn Baldwin 	} else {
2247628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2248628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
2249628d2653SJohn Baldwin 			mtx_lock(&Giant);
2250c31146a1SJohn Baldwin 	}
2251b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2252df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2253df8bae1dSRodney W. Grimes }
2254df8bae1dSRodney W. Grimes 
2255c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2256c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2257c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2258c5edb423SSean Eric Fagan 
2259c5edb423SSean Eric Fagan /*
2260c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2261c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2262c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2263c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2264c5edb423SSean Eric Fagan  *	%P	process id (pid)
2265c5edb423SSean Eric Fagan  *	%U	user id (uid)
2266c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2267c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2268c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2269c5edb423SSean Eric Fagan  */
2270c5edb423SSean Eric Fagan 
2271fca666a1SJulian Elischer static char *
2272c5edb423SSean Eric Fagan expand_name(name, uid, pid)
22738b43b535SAlfred Perlstein 	const char *name;
22748b43b535SAlfred Perlstein 	uid_t uid;
22758b43b535SAlfred Perlstein 	pid_t pid;
22768b43b535SAlfred Perlstein {
22778b43b535SAlfred Perlstein 	const char *format, *appendstr;
2278c5edb423SSean Eric Fagan 	char *temp;
2279c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
22808b43b535SAlfred Perlstein 	size_t i, l, n;
2281c5edb423SSean Eric Fagan 
22828b43b535SAlfred Perlstein 	format = corefilename;
22838b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
22840bfe2990SEivind Eklund 	if (temp == NULL)
22858b43b535SAlfred Perlstein 		return (NULL);
2286ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2287c5edb423SSean Eric Fagan 		switch (format[i]) {
2288c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2289c5edb423SSean Eric Fagan 			i++;
2290c5edb423SSean Eric Fagan 			switch (format[i]) {
2291c5edb423SSean Eric Fagan 			case '%':
22928b43b535SAlfred Perlstein 				appendstr = "%";
2293c5edb423SSean Eric Fagan 				break;
2294c5edb423SSean Eric Fagan 			case 'N':	/* process name */
22958b43b535SAlfred Perlstein 				appendstr = name;
2296c5edb423SSean Eric Fagan 				break;
2297c5edb423SSean Eric Fagan 			case 'P':	/* process id */
22988b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
22998b43b535SAlfred Perlstein 				appendstr = buf;
2300c5edb423SSean Eric Fagan 				break;
2301c5edb423SSean Eric Fagan 			case 'U':	/* user id */
23028b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
23038b43b535SAlfred Perlstein 				appendstr = buf;
2304c5edb423SSean Eric Fagan 				break;
2305c5edb423SSean Eric Fagan 			default:
23068b43b535SAlfred Perlstein 				appendstr = "";
23078b43b535SAlfred Perlstein 			  	log(LOG_ERR,
23088b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
23098b43b535SAlfred Perlstein 				    format[i], format);
2310c5edb423SSean Eric Fagan 			}
23118b43b535SAlfred Perlstein 			l = strlen(appendstr);
23128b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
23138b43b535SAlfred Perlstein 				goto toolong;
23148b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
23158b43b535SAlfred Perlstein 			n += l;
2316c5edb423SSean Eric Fagan 			break;
2317c5edb423SSean Eric Fagan 		default:
2318c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2319c5edb423SSean Eric Fagan 		}
2320c5edb423SSean Eric Fagan 	}
23218b43b535SAlfred Perlstein 	if (format[i] != '\0')
23228b43b535SAlfred Perlstein 		goto toolong;
23238b43b535SAlfred Perlstein 	return (temp);
23248b43b535SAlfred Perlstein toolong:
23258b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
23268b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
23278b43b535SAlfred Perlstein 	free(temp, M_TEMP);
23288b43b535SAlfred Perlstein 	return (NULL);
2329c5edb423SSean Eric Fagan }
2330c5edb423SSean Eric Fagan 
2331df8bae1dSRodney W. Grimes /*
2332fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2333fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2334fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2335fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2336fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2337fca666a1SJulian Elischer  */
2338fca666a1SJulian Elischer 
2339fca666a1SJulian Elischer static int
2340b40ce416SJulian Elischer coredump(struct thread *td)
2341fca666a1SJulian Elischer {
2342b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2343fca666a1SJulian Elischer 	register struct vnode *vp;
23449c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
234506ae1e91SMatthew Dillon 	struct flock lf;
2346fca666a1SJulian Elischer 	struct nameidata nd;
2347fca666a1SJulian Elischer 	struct vattr vattr;
2348e6796b67SKirk McKusick 	int error, error1, flags;
2349f2a2857bSKirk McKusick 	struct mount *mp;
2350fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2351fca666a1SJulian Elischer 	off_t limit;
2352fca666a1SJulian Elischer 
2353628d2653SJohn Baldwin 	PROC_LOCK(p);
2354628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2355fca666a1SJulian Elischer 
2356628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2357628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2358fca666a1SJulian Elischer 		return (EFAULT);
2359628d2653SJohn Baldwin 	}
2360fca666a1SJulian Elischer 
2361fca666a1SJulian Elischer 	/*
236235a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
236335a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
236435a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
236535a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
236635a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
236735a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2368fca666a1SJulian Elischer 	 */
236935a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2370628d2653SJohn Baldwin 	if (limit == 0) {
2371628d2653SJohn Baldwin 		PROC_UNLOCK(p);
237235a2598fSSean Eric Fagan 		return 0;
2373628d2653SJohn Baldwin 	}
2374628d2653SJohn Baldwin 	PROC_UNLOCK(p);
237535a2598fSSean Eric Fagan 
2376f2a2857bSKirk McKusick restart:
23779c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2378ccdbd10cSPeter Pentchev 	if (name == NULL)
2379ccdbd10cSPeter Pentchev 		return (EINVAL);
2380b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2381e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2382e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2383fca666a1SJulian Elischer 	free(name, M_TEMP);
2384fca666a1SJulian Elischer 	if (error)
2385fca666a1SJulian Elischer 		return (error);
2386762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2387fca666a1SJulian Elischer 	vp = nd.ni_vp;
238806ae1e91SMatthew Dillon 
2389832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2390832dafadSDon Lewis 	if (vp->v_type != VREG ||
2391832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2392832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2393832dafadSDon Lewis 		error = EFAULT;
2394832dafadSDon Lewis 		goto out2;
2395832dafadSDon Lewis 	}
2396832dafadSDon Lewis 
2397b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
239806ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
239906ae1e91SMatthew Dillon 	lf.l_start = 0;
240006ae1e91SMatthew Dillon 	lf.l_len = 0;
240106ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
240206ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
240306ae1e91SMatthew Dillon 	if (error)
240406ae1e91SMatthew Dillon 		goto out2;
240506ae1e91SMatthew Dillon 
240606ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
240706ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
240806ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2409b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2410f2a2857bSKirk McKusick 			return (error);
2411f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2412f2a2857bSKirk McKusick 			return (error);
2413f2a2857bSKirk McKusick 		goto restart;
2414f2a2857bSKirk McKusick 	}
2415fca666a1SJulian Elischer 
2416fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2417fca666a1SJulian Elischer 	vattr.va_size = 0;
241888b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2419b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2420b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
242188b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2422628d2653SJohn Baldwin 	PROC_LOCK(p);
2423fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2424628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2425fca666a1SJulian Elischer 
2426fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2427b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2428fca666a1SJulian Elischer 	  ENOSYS;
2429fca666a1SJulian Elischer 
243006ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
243106ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2432f2a2857bSKirk McKusick 	vn_finished_write(mp);
243306ae1e91SMatthew Dillon out2:
2434b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2435fca666a1SJulian Elischer 	if (error == 0)
2436fca666a1SJulian Elischer 		error = error1;
2437fca666a1SJulian Elischer 	return (error);
2438fca666a1SJulian Elischer }
2439fca666a1SJulian Elischer 
2440fca666a1SJulian Elischer /*
2441df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2442df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2443df8bae1dSRodney W. Grimes  */
2444d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2445df8bae1dSRodney W. Grimes struct nosys_args {
2446df8bae1dSRodney W. Grimes 	int	dummy;
2447df8bae1dSRodney W. Grimes };
2448d2d3e875SBruce Evans #endif
2449fb99ab88SMatthew Dillon /*
2450fb99ab88SMatthew Dillon  * MPSAFE
2451fb99ab88SMatthew Dillon  */
2452df8bae1dSRodney W. Grimes /* ARGSUSED */
245326f9a767SRodney W. Grimes int
2454b40ce416SJulian Elischer nosys(td, args)
2455b40ce416SJulian Elischer 	struct thread *td;
2456df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2457df8bae1dSRodney W. Grimes {
2458b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2459b40ce416SJulian Elischer 
2460fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2461628d2653SJohn Baldwin 	PROC_LOCK(p);
2462df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2463628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2464fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2465f5216b9aSBruce Evans 	return (ENOSYS);
2466df8bae1dSRodney W. Grimes }
2467831d27a9SDon Lewis 
2468831d27a9SDon Lewis /*
246948f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2470831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2471831d27a9SDon Lewis  */
2472831d27a9SDon Lewis void
2473f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2474f1320723SAlfred Perlstein 	struct sigio **sigiop;
24752c42a146SMarcel Moolenaar 	int sig, checkctty;
2476831d27a9SDon Lewis {
2477f1320723SAlfred Perlstein 	struct sigio *sigio;
2478831d27a9SDon Lewis 
2479f1320723SAlfred Perlstein 	SIGIO_LOCK();
2480f1320723SAlfred Perlstein 	sigio = *sigiop;
2481f1320723SAlfred Perlstein 	if (sigio == NULL) {
2482f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2483f1320723SAlfred Perlstein 		return;
2484f1320723SAlfred Perlstein 	}
2485831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2486628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
24872b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
24882c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2489628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2490831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2491831d27a9SDon Lewis 		struct proc *p;
2492831d27a9SDon Lewis 
2493f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2494628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2495628d2653SJohn Baldwin 			PROC_LOCK(p);
24962b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2497831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
24982c42a146SMarcel Moolenaar 				psignal(p, sig);
2499628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2500628d2653SJohn Baldwin 		}
2501f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2502831d27a9SDon Lewis 	}
2503f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2504831d27a9SDon Lewis }
2505cb679c38SJonathan Lemon 
2506cb679c38SJonathan Lemon static int
2507cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2508cb679c38SJonathan Lemon {
2509cb679c38SJonathan Lemon 	struct proc *p = curproc;
2510cb679c38SJonathan Lemon 
2511cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2512cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2513cb679c38SJonathan Lemon 
2514628d2653SJohn Baldwin 	PROC_LOCK(p);
2515cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2516628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2517cb679c38SJonathan Lemon 
2518cb679c38SJonathan Lemon 	return (0);
2519cb679c38SJonathan Lemon }
2520cb679c38SJonathan Lemon 
2521cb679c38SJonathan Lemon static void
2522cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2523cb679c38SJonathan Lemon {
2524cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2525cb679c38SJonathan Lemon 
2526628d2653SJohn Baldwin 	PROC_LOCK(p);
2527e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2528628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2529cb679c38SJonathan Lemon }
2530cb679c38SJonathan Lemon 
2531cb679c38SJonathan Lemon /*
2532cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2533cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2534cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2535cb679c38SJonathan Lemon  * isn't worth the trouble.
2536cb679c38SJonathan Lemon  */
2537cb679c38SJonathan Lemon static int
2538cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2539cb679c38SJonathan Lemon {
2540cb679c38SJonathan Lemon 
2541cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2542cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2543cb679c38SJonathan Lemon 
2544cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2545cb679c38SJonathan Lemon 			kn->kn_data++;
2546cb679c38SJonathan Lemon 	}
2547cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2548cb679c38SJonathan Lemon }
2549