xref: /freebsd/sys/kern/kern_sig.c (revision 8f19eb88dff40008a1ef7524390e8a00f114781a)
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 
766f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
776f841fb7SMarcel Moolenaar 
784d77a549SAlfred Perlstein static int	coredump(struct thread *);
794d77a549SAlfred Perlstein static int	do_sigprocmask(struct proc *p, int how, sigset_t *set,
804d77a549SAlfred Perlstein 			sigset_t *oset, int old);
814d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
829c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
834d77a549SAlfred Perlstein static int	sig_ffs(sigset_t *set);
844d77a549SAlfred Perlstein static int	sigprop(int sig);
854d77a549SAlfred Perlstein static void	stop(struct proc *);
86e602ba25SJulian Elischer static void	tdsignal(struct thread *td, int sig, sig_t action);
87cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
88cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
89cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
90cb679c38SJonathan Lemon 
91cb679c38SJonathan Lemon struct filterops sig_filtops =
92cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
93cb679c38SJonathan Lemon 
9457308494SJoerg Wunsch static int	kern_logsigexit = 1;
953d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
963d177f46SBill Fumerola     &kern_logsigexit, 0,
973d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
9857308494SJoerg Wunsch 
992b87b6d4SRobert Watson /*
1002b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1012b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1022b87b6d4SRobert Watson  * in the right situations.
1032b87b6d4SRobert Watson  */
1042b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1052b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1062b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1072b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1082b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1092b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1102b87b6d4SRobert Watson 
11122d4b0fbSJohn Polstra int sugid_coredump;
1123d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1133d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
114c87e2930SDavid Greenman 
115e5a28db9SPaul Saab static int	do_coredump = 1;
116e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
117e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
118e5a28db9SPaul Saab 
1192c42a146SMarcel Moolenaar /*
1202c42a146SMarcel Moolenaar  * Signal properties and actions.
1212c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1222c42a146SMarcel Moolenaar  * according to the following properties:
1232c42a146SMarcel Moolenaar  */
1242c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1252c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1262c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1272c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1282c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1292c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1302c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
131df8bae1dSRodney W. Grimes 
1322c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1332c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1342c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1352c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1362c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1372c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1382c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1392c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1412c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1452c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1462c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1472c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1482c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1492c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1502c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1512c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1522c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1532c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1542c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1552c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1562c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1572c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1582c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1592c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1602c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1612c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1622c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1632c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1642c42a146SMarcel Moolenaar };
1652c42a146SMarcel Moolenaar 
166fbbeeb6cSBruce Evans /*
167fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
168fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
169fbbeeb6cSBruce Evans  * action, the process stops in issignal().
170e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
171fbbeeb6cSBruce Evans  *
17233510ef1SBruce Evans  * MP SAFE.
173fbbeeb6cSBruce Evans  */
174fbbeeb6cSBruce Evans int
175e602ba25SJulian Elischer cursig(struct thread *td)
176fbbeeb6cSBruce Evans {
177e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
178fbbeeb6cSBruce Evans 
1792ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
180179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
181e602ba25SJulian Elischer 	return (SIGPENDING(p) ? issignal(td) : 0);
182fbbeeb6cSBruce Evans }
183fbbeeb6cSBruce Evans 
18479065dbaSBruce Evans /*
18579065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
18679065dbaSBruce Evans  * mode.  This must be called whenever a signal is added to p_siglist or
18779065dbaSBruce Evans  * unmasked in p_sigmask.
18879065dbaSBruce Evans  */
18979065dbaSBruce Evans void
19079065dbaSBruce Evans signotify(struct proc *p)
19179065dbaSBruce Evans {
19279065dbaSBruce Evans 
19379065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
19479065dbaSBruce Evans 	mtx_lock_spin(&sched_lock);
19579065dbaSBruce Evans 	if (SIGPENDING(p)) {
19679065dbaSBruce Evans 		p->p_sflag |= PS_NEEDSIGCHK;
19779065dbaSBruce Evans 		p->p_kse.ke_flags |= KEF_ASTPENDING;	/* XXXKSE */
19879065dbaSBruce Evans 	}
19979065dbaSBruce Evans 	mtx_unlock_spin(&sched_lock);
20079065dbaSBruce Evans }
20179065dbaSBruce Evans 
2026f841fb7SMarcel Moolenaar static __inline int
2036f841fb7SMarcel Moolenaar sigprop(int sig)
2042c42a146SMarcel Moolenaar {
2056f841fb7SMarcel Moolenaar 
2062c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2072c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2082c42a146SMarcel Moolenaar 	return (0);
209df8bae1dSRodney W. Grimes }
2102c42a146SMarcel Moolenaar 
2116f841fb7SMarcel Moolenaar static __inline int
2126f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2132c42a146SMarcel Moolenaar {
2142c42a146SMarcel Moolenaar 	int i;
2152c42a146SMarcel Moolenaar 
2166f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2172c42a146SMarcel Moolenaar 		if (set->__bits[i])
2182c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
219df8bae1dSRodney W. Grimes 	return (0);
220df8bae1dSRodney W. Grimes }
221df8bae1dSRodney W. Grimes 
2222c42a146SMarcel Moolenaar /*
2238f19eb88SIan Dowse  * kern_sigaction
2242c42a146SMarcel Moolenaar  * sigaction
2252c42a146SMarcel Moolenaar  * osigaction
2262c42a146SMarcel Moolenaar  */
2278f19eb88SIan Dowse int
2288f19eb88SIan Dowse kern_sigaction(td, sig, act, oact, old)
2298f19eb88SIan Dowse 	struct thread *td;
2302c42a146SMarcel Moolenaar 	register int sig;
2312c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
232645682fdSLuoqi Chen 	int old;
233df8bae1dSRodney W. Grimes {
234628d2653SJohn Baldwin 	register struct sigacts *ps;
2358f19eb88SIan Dowse 	struct proc *p = td->td_proc;
236df8bae1dSRodney W. Grimes 
2372899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2382c42a146SMarcel Moolenaar 		return (EINVAL);
2392c42a146SMarcel Moolenaar 
240628d2653SJohn Baldwin 	PROC_LOCK(p);
241628d2653SJohn Baldwin 	ps = p->p_sigacts;
2422c42a146SMarcel Moolenaar 	if (oact) {
2432c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2442c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2452c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2462c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2472c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2482c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2492c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2502c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2512c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2522c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2532c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2542c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2552c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
256645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2572c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
258645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2592c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2602c42a146SMarcel Moolenaar 	}
2612c42a146SMarcel Moolenaar 	if (act) {
2622c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
263628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
264628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2652c42a146SMarcel Moolenaar 			return (EINVAL);
266628d2653SJohn Baldwin 		}
2672c42a146SMarcel Moolenaar 
268df8bae1dSRodney W. Grimes 		/*
269df8bae1dSRodney W. Grimes 		 * Change setting atomically.
270df8bae1dSRodney W. Grimes 		 */
2712c42a146SMarcel Moolenaar 
2722c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2732c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2742c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
275aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
276aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
27780f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
27880f42b55SIan Dowse 		} else {
27980f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2802c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2812c42a146SMarcel Moolenaar 		}
2822c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2832c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
284df8bae1dSRodney W. Grimes 		else
2852c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2862c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2872c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
288df8bae1dSRodney W. Grimes 		else
2892c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2902c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2912c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2921e41c1b5SSteven Wallace 		else
2932c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2942c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2952c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
296289ccde0SPeter Wemm 		else
2972c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
298df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2992c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3002c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
301df8bae1dSRodney W. Grimes 		else
3028c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
303df8bae1dSRodney W. Grimes #endif
3042c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3052c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
306645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
3076626c604SJulian Elischer 			else
308645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
309ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
310245f17d4SJoerg Wunsch 				/*
3112c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3122c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3132c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3142c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
315245f17d4SJoerg Wunsch 				 */
316245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
317645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3186626c604SJulian Elischer 				else
319645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
320245f17d4SJoerg Wunsch 			} else
321645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
322ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
323ba1551caSIan Dowse 				p->p_procsig->ps_flag |= PS_CLDSIGIGN;
324ba1551caSIan Dowse 			else
325ba1551caSIan Dowse 				p->p_procsig->ps_flag &= ~PS_CLDSIGIGN;
326df8bae1dSRodney W. Grimes 		}
327df8bae1dSRodney W. Grimes 		/*
328df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3292c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3302c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3312c42a146SMarcel Moolenaar 		 * have to restart the process.
332df8bae1dSRodney W. Grimes 		 */
3332c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3342c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3352c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3362c42a146SMarcel Moolenaar 			/* never to be seen again */
3372c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3382c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3392c42a146SMarcel Moolenaar 				/* easier in psignal */
3402c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3412c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
342645682fdSLuoqi Chen 		} else {
3432c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3442c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3452c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3462c42a146SMarcel Moolenaar 			else
3472c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3482c42a146SMarcel Moolenaar 		}
349e8ebc08fSPeter Wemm #ifdef COMPAT_43
350645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
351645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
352645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
353645682fdSLuoqi Chen 		else
354645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
355e8ebc08fSPeter Wemm #endif
356df8bae1dSRodney W. Grimes 	}
357628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3582c42a146SMarcel Moolenaar 	return (0);
3592c42a146SMarcel Moolenaar }
3602c42a146SMarcel Moolenaar 
3612c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3622c42a146SMarcel Moolenaar struct sigaction_args {
3632c42a146SMarcel Moolenaar 	int	sig;
3642c42a146SMarcel Moolenaar 	struct	sigaction *act;
3652c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3662c42a146SMarcel Moolenaar };
3672c42a146SMarcel Moolenaar #endif
368fb99ab88SMatthew Dillon /*
369fb99ab88SMatthew Dillon  * MPSAFE
370fb99ab88SMatthew Dillon  */
3712c42a146SMarcel Moolenaar /* ARGSUSED */
3722c42a146SMarcel Moolenaar int
373b40ce416SJulian Elischer sigaction(td, uap)
374b40ce416SJulian Elischer 	struct thread *td;
3752c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3762c42a146SMarcel Moolenaar {
3772c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3782c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3792c42a146SMarcel Moolenaar 	int error;
3802c42a146SMarcel Moolenaar 
381fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
382fb99ab88SMatthew Dillon 
3836f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3846f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3852c42a146SMarcel Moolenaar 	if (actp) {
3866f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3872c42a146SMarcel Moolenaar 		if (error)
388fb99ab88SMatthew Dillon 			goto done2;
3892c42a146SMarcel Moolenaar 	}
3908f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
3912c42a146SMarcel Moolenaar 	if (oactp && !error) {
3926f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3932c42a146SMarcel Moolenaar 	}
394fb99ab88SMatthew Dillon done2:
395fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
3962c42a146SMarcel Moolenaar 	return (error);
3972c42a146SMarcel Moolenaar }
3982c42a146SMarcel Moolenaar 
39931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4002c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4012c42a146SMarcel Moolenaar struct osigaction_args {
4022c42a146SMarcel Moolenaar 	int	signum;
4032c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4042c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4052c42a146SMarcel Moolenaar };
4062c42a146SMarcel Moolenaar #endif
407fb99ab88SMatthew Dillon /*
408fb99ab88SMatthew Dillon  * MPSAFE
409fb99ab88SMatthew Dillon  */
4102c42a146SMarcel Moolenaar /* ARGSUSED */
4112c42a146SMarcel Moolenaar int
412b40ce416SJulian Elischer osigaction(td, uap)
413b40ce416SJulian Elischer 	struct thread *td;
4142c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4152c42a146SMarcel Moolenaar {
4162c42a146SMarcel Moolenaar 	struct osigaction sa;
4172c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4182c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4192c42a146SMarcel Moolenaar 	int error;
4202c42a146SMarcel Moolenaar 
4216f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4226f841fb7SMarcel Moolenaar 		return (EINVAL);
423fb99ab88SMatthew Dillon 
4246f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4256f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
426fb99ab88SMatthew Dillon 
427fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
428fb99ab88SMatthew Dillon 
4292c42a146SMarcel Moolenaar 	if (nsap) {
4306f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4312c42a146SMarcel Moolenaar 		if (error)
432fb99ab88SMatthew Dillon 			goto done2;
4332c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4342c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4352c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4362c42a146SMarcel Moolenaar 	}
4378f19eb88SIan Dowse 	error = kern_sigaction(td, uap->signum, nsap, osap, 1);
4382c42a146SMarcel Moolenaar 	if (osap && !error) {
4392c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4402c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4412c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4426f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4432c42a146SMarcel Moolenaar 	}
444fb99ab88SMatthew Dillon done2:
445fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
4462c42a146SMarcel Moolenaar 	return (error);
4472c42a146SMarcel Moolenaar }
44831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes /*
451df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
452df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
453df8bae1dSRodney W. Grimes  */
454df8bae1dSRodney W. Grimes void
455df8bae1dSRodney W. Grimes siginit(p)
456df8bae1dSRodney W. Grimes 	struct proc *p;
457df8bae1dSRodney W. Grimes {
458df8bae1dSRodney W. Grimes 	register int i;
459df8bae1dSRodney W. Grimes 
460628d2653SJohn Baldwin 	PROC_LOCK(p);
4612c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4622c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4632c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
464628d2653SJohn Baldwin 	PROC_UNLOCK(p);
465df8bae1dSRodney W. Grimes }
466df8bae1dSRodney W. Grimes 
467df8bae1dSRodney W. Grimes /*
468df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
469df8bae1dSRodney W. Grimes  */
470df8bae1dSRodney W. Grimes void
471df8bae1dSRodney W. Grimes execsigs(p)
472df8bae1dSRodney W. Grimes 	register struct proc *p;
473df8bae1dSRodney W. Grimes {
474628d2653SJohn Baldwin 	register struct sigacts *ps;
4752c42a146SMarcel Moolenaar 	register int sig;
476df8bae1dSRodney W. Grimes 
477df8bae1dSRodney W. Grimes 	/*
478df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
479df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
480df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
481df8bae1dSRodney W. Grimes 	 */
4829b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
483628d2653SJohn Baldwin 	ps = p->p_sigacts;
4842c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4852c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4862c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4872c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4882c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4892c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4902c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
491df8bae1dSRodney W. Grimes 		}
4922c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
493df8bae1dSRodney W. Grimes 	}
494df8bae1dSRodney W. Grimes 	/*
495df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
496df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
497df8bae1dSRodney W. Grimes 	 */
498645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
499645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
500645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
5013b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
50280e907a1SPeter Wemm 	/*
50380e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
50480e907a1SPeter Wemm 	 */
505ba1551caSIan Dowse 	p->p_procsig->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
506c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
507c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
508df8bae1dSRodney W. Grimes }
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes /*
511628d2653SJohn Baldwin  * do_sigprocmask()
5127c8fdcbdSMatthew Dillon  *
513628d2653SJohn Baldwin  *	Manipulate signal mask.
514df8bae1dSRodney W. Grimes  */
5152c42a146SMarcel Moolenaar static int
516645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
5172c42a146SMarcel Moolenaar 	struct proc *p;
5182c42a146SMarcel Moolenaar 	int how;
5192c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
520645682fdSLuoqi Chen 	int old;
5212c42a146SMarcel Moolenaar {
5222c42a146SMarcel Moolenaar 	int error;
5232c42a146SMarcel Moolenaar 
524628d2653SJohn Baldwin 	PROC_LOCK(p);
5252c42a146SMarcel Moolenaar 	if (oset != NULL)
5262c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
5272c42a146SMarcel Moolenaar 
5282c42a146SMarcel Moolenaar 	error = 0;
5292c42a146SMarcel Moolenaar 	if (set != NULL) {
5302c42a146SMarcel Moolenaar 		switch (how) {
5312c42a146SMarcel Moolenaar 		case SIG_BLOCK:
532645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
5332c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
5342c42a146SMarcel Moolenaar 			break;
5352c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
5362c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
53779065dbaSBruce Evans 			signotify(p);
5382c42a146SMarcel Moolenaar 			break;
5392c42a146SMarcel Moolenaar 		case SIG_SETMASK:
540645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
541645682fdSLuoqi Chen 			if (old)
542645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
543645682fdSLuoqi Chen 			else
5442c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
54579065dbaSBruce Evans 			signotify(p);
5462c42a146SMarcel Moolenaar 			break;
5472c42a146SMarcel Moolenaar 		default:
5482c42a146SMarcel Moolenaar 			error = EINVAL;
5492c42a146SMarcel Moolenaar 			break;
5502c42a146SMarcel Moolenaar 		}
5512c42a146SMarcel Moolenaar 	}
552628d2653SJohn Baldwin 	PROC_UNLOCK(p);
5532c42a146SMarcel Moolenaar 	return (error);
5542c42a146SMarcel Moolenaar }
5552c42a146SMarcel Moolenaar 
5567c8fdcbdSMatthew Dillon /*
557b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
5587c8fdcbdSMatthew Dillon  */
5597c8fdcbdSMatthew Dillon 
560d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
561df8bae1dSRodney W. Grimes struct sigprocmask_args {
562df8bae1dSRodney W. Grimes 	int	how;
5632c42a146SMarcel Moolenaar 	const sigset_t *set;
5642c42a146SMarcel Moolenaar 	sigset_t *oset;
565df8bae1dSRodney W. Grimes };
566d2d3e875SBruce Evans #endif
56726f9a767SRodney W. Grimes int
568b40ce416SJulian Elischer sigprocmask(td, uap)
569b40ce416SJulian Elischer 	register struct thread *td;
570df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
571df8bae1dSRodney W. Grimes {
572b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
5732c42a146SMarcel Moolenaar 	sigset_t set, oset;
5742c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5752c42a146SMarcel Moolenaar 	int error;
576df8bae1dSRodney W. Grimes 
5776f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5786f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5792c42a146SMarcel Moolenaar 	if (setp) {
5806f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5812c42a146SMarcel Moolenaar 		if (error)
5822c42a146SMarcel Moolenaar 			return (error);
583df8bae1dSRodney W. Grimes 	}
584645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5852c42a146SMarcel Moolenaar 	if (osetp && !error) {
5866f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5872c42a146SMarcel Moolenaar 	}
5882c42a146SMarcel Moolenaar 	return (error);
5892c42a146SMarcel Moolenaar }
5902c42a146SMarcel Moolenaar 
59131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5927c8fdcbdSMatthew Dillon /*
5937c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5947c8fdcbdSMatthew Dillon  */
5952c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5962c42a146SMarcel Moolenaar struct osigprocmask_args {
5972c42a146SMarcel Moolenaar 	int	how;
5982c42a146SMarcel Moolenaar 	osigset_t mask;
5992c42a146SMarcel Moolenaar };
6002c42a146SMarcel Moolenaar #endif
6012c42a146SMarcel Moolenaar int
602b40ce416SJulian Elischer osigprocmask(td, uap)
603b40ce416SJulian Elischer 	register struct thread *td;
6042c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
6052c42a146SMarcel Moolenaar {
606b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
6072c42a146SMarcel Moolenaar 	sigset_t set, oset;
6082c42a146SMarcel Moolenaar 	int error;
6092c42a146SMarcel Moolenaar 
6102c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
611645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
612b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
613df8bae1dSRodney W. Grimes 	return (error);
614df8bae1dSRodney W. Grimes }
61531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
616df8bae1dSRodney W. Grimes 
617d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
618df8bae1dSRodney W. Grimes struct sigpending_args {
6192c42a146SMarcel Moolenaar 	sigset_t	*set;
620df8bae1dSRodney W. Grimes };
621d2d3e875SBruce Evans #endif
622fb99ab88SMatthew Dillon /*
623fb99ab88SMatthew Dillon  * MPSAFE
624fb99ab88SMatthew Dillon  */
625df8bae1dSRodney W. Grimes /* ARGSUSED */
62626f9a767SRodney W. Grimes int
627b40ce416SJulian Elischer sigpending(td, uap)
628b40ce416SJulian Elischer 	struct thread *td;
629df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
630df8bae1dSRodney W. Grimes {
631b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
632628d2653SJohn Baldwin 	sigset_t siglist;
633fb99ab88SMatthew Dillon 	int error;
634df8bae1dSRodney W. Grimes 
635fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
636628d2653SJohn Baldwin 	PROC_LOCK(p);
637628d2653SJohn Baldwin 	siglist = p->p_siglist;
638628d2653SJohn Baldwin 	PROC_UNLOCK(p);
639fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
640fb99ab88SMatthew Dillon 	error = copyout(&siglist, uap->set, sizeof(sigset_t));
641fb99ab88SMatthew Dillon 	return(error);
6422c42a146SMarcel Moolenaar }
6432c42a146SMarcel Moolenaar 
64431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6452c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6462c42a146SMarcel Moolenaar struct osigpending_args {
6472c42a146SMarcel Moolenaar 	int	dummy;
6482c42a146SMarcel Moolenaar };
6492c42a146SMarcel Moolenaar #endif
650fb99ab88SMatthew Dillon /*
651fb99ab88SMatthew Dillon  * MPSAFE
652fb99ab88SMatthew Dillon  */
6532c42a146SMarcel Moolenaar /* ARGSUSED */
6542c42a146SMarcel Moolenaar int
655b40ce416SJulian Elischer osigpending(td, uap)
656b40ce416SJulian Elischer 	struct thread *td;
6572c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6582c42a146SMarcel Moolenaar {
659b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
660b40ce416SJulian Elischer 
661fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
662628d2653SJohn Baldwin 	PROC_LOCK(p);
663b40ce416SJulian Elischer 	SIG2OSIG(p->p_siglist, td->td_retval[0]);
664628d2653SJohn Baldwin 	PROC_UNLOCK(p);
665fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
666df8bae1dSRodney W. Grimes 	return (0);
667df8bae1dSRodney W. Grimes }
66831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
671df8bae1dSRodney W. Grimes /*
672df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
673df8bae1dSRodney W. Grimes  */
674d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
675df8bae1dSRodney W. Grimes struct osigvec_args {
676df8bae1dSRodney W. Grimes 	int	signum;
677df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
678df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
679df8bae1dSRodney W. Grimes };
680d2d3e875SBruce Evans #endif
681fb99ab88SMatthew Dillon /*
682fb99ab88SMatthew Dillon  * MPSAFE
683fb99ab88SMatthew Dillon  */
684df8bae1dSRodney W. Grimes /* ARGSUSED */
68526f9a767SRodney W. Grimes int
686b40ce416SJulian Elischer osigvec(td, uap)
687b40ce416SJulian Elischer 	struct thread *td;
688df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
689df8bae1dSRodney W. Grimes {
690df8bae1dSRodney W. Grimes 	struct sigvec vec;
6912c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6922c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6932c42a146SMarcel Moolenaar 	int error;
694df8bae1dSRodney W. Grimes 
6956f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6966f841fb7SMarcel Moolenaar 		return (EINVAL);
6976f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6986f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6992c42a146SMarcel Moolenaar 	if (nsap) {
7006f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
7012c42a146SMarcel Moolenaar 		if (error)
702df8bae1dSRodney W. Grimes 			return (error);
7032c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
7042c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
7052c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
7062c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
707df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
7082c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
709df8bae1dSRodney W. Grimes #endif
710df8bae1dSRodney W. Grimes 	}
711fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
7128f19eb88SIan Dowse 	error = kern_sigaction(td, uap->signum, nsap, osap, 1);
713fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
7142c42a146SMarcel Moolenaar 	if (osap && !error) {
7152c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7162c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7172c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7182c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7192c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7202c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7212c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7222c42a146SMarcel Moolenaar #endif
7236f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
7242c42a146SMarcel Moolenaar 	}
7252c42a146SMarcel Moolenaar 	return (error);
726df8bae1dSRodney W. Grimes }
727df8bae1dSRodney W. Grimes 
728d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
729df8bae1dSRodney W. Grimes struct osigblock_args {
730df8bae1dSRodney W. Grimes 	int	mask;
731df8bae1dSRodney W. Grimes };
732d2d3e875SBruce Evans #endif
733fb99ab88SMatthew Dillon /*
734fb99ab88SMatthew Dillon  * MPSAFE
735fb99ab88SMatthew Dillon  */
73626f9a767SRodney W. Grimes int
737b40ce416SJulian Elischer osigblock(td, uap)
738b40ce416SJulian Elischer 	register struct thread *td;
739df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
740df8bae1dSRodney W. Grimes {
741b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7422c42a146SMarcel Moolenaar 	sigset_t set;
743df8bae1dSRodney W. Grimes 
7442c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7452c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
746fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
747628d2653SJohn Baldwin 	PROC_LOCK(p);
748b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
7492c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
750628d2653SJohn Baldwin 	PROC_UNLOCK(p);
751fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
752df8bae1dSRodney W. Grimes 	return (0);
753df8bae1dSRodney W. Grimes }
754df8bae1dSRodney W. Grimes 
755d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
756df8bae1dSRodney W. Grimes struct osigsetmask_args {
757df8bae1dSRodney W. Grimes 	int	mask;
758df8bae1dSRodney W. Grimes };
759d2d3e875SBruce Evans #endif
760fb99ab88SMatthew Dillon /*
761fb99ab88SMatthew Dillon  * MPSAFE
762fb99ab88SMatthew Dillon  */
76326f9a767SRodney W. Grimes int
764b40ce416SJulian Elischer osigsetmask(td, uap)
765b40ce416SJulian Elischer 	struct thread *td;
766df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
767df8bae1dSRodney W. Grimes {
768b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7692c42a146SMarcel Moolenaar 	sigset_t set;
770df8bae1dSRodney W. Grimes 
7712c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7722c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
773fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
774628d2653SJohn Baldwin 	PROC_LOCK(p);
775b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
776645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
77779065dbaSBruce Evans 	signotify(p);
778628d2653SJohn Baldwin 	PROC_UNLOCK(p);
779fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
780df8bae1dSRodney W. Grimes 	return (0);
781df8bae1dSRodney W. Grimes }
782df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
783df8bae1dSRodney W. Grimes 
784df8bae1dSRodney W. Grimes /*
785df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
786df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
787df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
788b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
789b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
790b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
791df8bae1dSRodney W. Grimes  */
792d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
793df8bae1dSRodney W. Grimes struct sigsuspend_args {
7942c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
795df8bae1dSRodney W. Grimes };
796d2d3e875SBruce Evans #endif
797fb99ab88SMatthew Dillon /*
798fb99ab88SMatthew Dillon  * MPSAFE
799fb99ab88SMatthew Dillon  */
800df8bae1dSRodney W. Grimes /* ARGSUSED */
80126f9a767SRodney W. Grimes int
802b40ce416SJulian Elischer sigsuspend(td, uap)
803b40ce416SJulian Elischer 	struct thread *td;
804df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
805df8bae1dSRodney W. Grimes {
8062c42a146SMarcel Moolenaar 	sigset_t mask;
8072c42a146SMarcel Moolenaar 	int error;
8082c42a146SMarcel Moolenaar 
8096f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
8102c42a146SMarcel Moolenaar 	if (error)
8112c42a146SMarcel Moolenaar 		return (error);
8128f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
8138f19eb88SIan Dowse }
8148f19eb88SIan Dowse 
8158f19eb88SIan Dowse int
8168f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
8178f19eb88SIan Dowse {
8188f19eb88SIan Dowse 	struct proc *p = td->td_proc;
8198f19eb88SIan Dowse 	register struct sigacts *ps;
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes 	/*
822645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
823df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
824df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
825df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
826df8bae1dSRodney W. Grimes 	 * to indicate this.
827df8bae1dSRodney W. Grimes 	 */
828fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
829628d2653SJohn Baldwin 	PROC_LOCK(p);
830628d2653SJohn Baldwin 	ps = p->p_sigacts;
8316626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
832645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
833645682fdSLuoqi Chen 
834645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
8352c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
83679065dbaSBruce Evans 	signotify(p);
83701609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
8382c42a146SMarcel Moolenaar 		/* void */;
839628d2653SJohn Baldwin 	PROC_UNLOCK(p);
840fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
8412c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
8422c42a146SMarcel Moolenaar 	return (EINTR);
8432c42a146SMarcel Moolenaar }
8442c42a146SMarcel Moolenaar 
84531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8462c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8472c42a146SMarcel Moolenaar struct osigsuspend_args {
8482c42a146SMarcel Moolenaar 	osigset_t mask;
8492c42a146SMarcel Moolenaar };
8502c42a146SMarcel Moolenaar #endif
851fb99ab88SMatthew Dillon /*
852fb99ab88SMatthew Dillon  * MPSAFE
853fb99ab88SMatthew Dillon  */
8542c42a146SMarcel Moolenaar /* ARGSUSED */
8552c42a146SMarcel Moolenaar int
856b40ce416SJulian Elischer osigsuspend(td, uap)
857b40ce416SJulian Elischer 	struct thread *td;
8582c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
8592c42a146SMarcel Moolenaar {
860b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
861645682fdSLuoqi Chen 	sigset_t mask;
862628d2653SJohn Baldwin 	register struct sigacts *ps;
8632c42a146SMarcel Moolenaar 
864fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
865628d2653SJohn Baldwin 	PROC_LOCK(p);
866628d2653SJohn Baldwin 	ps = p->p_sigacts;
8672c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
868645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
869645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
870645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
871645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
87279065dbaSBruce Evans 	signotify(p);
87301609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
874df8bae1dSRodney W. Grimes 		/* void */;
875628d2653SJohn Baldwin 	PROC_UNLOCK(p);
876fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
877df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
878df8bae1dSRodney W. Grimes 	return (EINTR);
879df8bae1dSRodney W. Grimes }
88031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
883d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
884df8bae1dSRodney W. Grimes struct osigstack_args {
885df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
886df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
887df8bae1dSRodney W. Grimes };
888d2d3e875SBruce Evans #endif
889fb99ab88SMatthew Dillon /*
890fb99ab88SMatthew Dillon  * MPSAFE
891fb99ab88SMatthew Dillon  */
892df8bae1dSRodney W. Grimes /* ARGSUSED */
89326f9a767SRodney W. Grimes int
894b40ce416SJulian Elischer osigstack(td, uap)
895b40ce416SJulian Elischer 	struct thread *td;
896df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
897df8bae1dSRodney W. Grimes {
898b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
899df8bae1dSRodney W. Grimes 	struct sigstack ss;
900fb99ab88SMatthew Dillon 	int error = 0;
901fb99ab88SMatthew Dillon 
902fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
903df8bae1dSRodney W. Grimes 
904d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
905628d2653SJohn Baldwin 		PROC_LOCK(p);
906645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
907b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
908628d2653SJohn Baldwin 		PROC_UNLOCK(p);
909d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
910d034d459SMarcel Moolenaar 		if (error)
911fb99ab88SMatthew Dillon 			goto done2;
912d034d459SMarcel Moolenaar 	}
913d034d459SMarcel Moolenaar 
914d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
915d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
916fb99ab88SMatthew Dillon 			goto done2;
917628d2653SJohn Baldwin 		PROC_LOCK(p);
918645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
919645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
920645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
921645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
922628d2653SJohn Baldwin 		PROC_UNLOCK(p);
923df8bae1dSRodney W. Grimes 	}
924fb99ab88SMatthew Dillon done2:
925fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
926fb99ab88SMatthew Dillon 	return (error);
927df8bae1dSRodney W. Grimes }
928df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
929df8bae1dSRodney W. Grimes 
930d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
931df8bae1dSRodney W. Grimes struct sigaltstack_args {
9322c42a146SMarcel Moolenaar 	stack_t	*ss;
9332c42a146SMarcel Moolenaar 	stack_t	*oss;
934df8bae1dSRodney W. Grimes };
935d2d3e875SBruce Evans #endif
936fb99ab88SMatthew Dillon /*
937fb99ab88SMatthew Dillon  * MPSAFE
938fb99ab88SMatthew Dillon  */
939df8bae1dSRodney W. Grimes /* ARGSUSED */
94026f9a767SRodney W. Grimes int
941b40ce416SJulian Elischer sigaltstack(td, uap)
942b40ce416SJulian Elischer 	struct thread *td;
943df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
944df8bae1dSRodney W. Grimes {
9458f19eb88SIan Dowse 	stack_t ss, oss;
9468f19eb88SIan Dowse 	int error;
9478f19eb88SIan Dowse 
9488f19eb88SIan Dowse 	if (uap->ss != NULL) {
9498f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
9508f19eb88SIan Dowse 		if (error)
9518f19eb88SIan Dowse 			return (error);
9528f19eb88SIan Dowse 	}
9538f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
9548f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
9558f19eb88SIan Dowse 	if (error)
9568f19eb88SIan Dowse 		return (error);
9578f19eb88SIan Dowse 	if (uap->oss != NULL)
9588f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
9598f19eb88SIan Dowse 	return (error);
9608f19eb88SIan Dowse }
9618f19eb88SIan Dowse 
9628f19eb88SIan Dowse int
9638f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
9648f19eb88SIan Dowse {
965b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
966fb99ab88SMatthew Dillon 	int oonstack;
967fb99ab88SMatthew Dillon 	int error = 0;
968fb99ab88SMatthew Dillon 
969fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
970df8bae1dSRodney W. Grimes 
971b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
972d034d459SMarcel Moolenaar 
9738f19eb88SIan Dowse 	if (oss != NULL) {
974628d2653SJohn Baldwin 		PROC_LOCK(p);
9758f19eb88SIan Dowse 		*oss = p->p_sigstk;
9768f19eb88SIan Dowse 		oss->ss_flags = (p->p_flag & P_ALTSTACK)
977d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
978628d2653SJohn Baldwin 		PROC_UNLOCK(p);
979df8bae1dSRodney W. Grimes 	}
980d034d459SMarcel Moolenaar 
9818f19eb88SIan Dowse 	if (ss != NULL) {
982fb99ab88SMatthew Dillon 		if (oonstack) {
983fb99ab88SMatthew Dillon 			error = EPERM;
984fb99ab88SMatthew Dillon 			goto done2;
985fb99ab88SMatthew Dillon 		}
9868f19eb88SIan Dowse 		if ((ss->ss_flags & ~SS_DISABLE) != 0) {
987fb99ab88SMatthew Dillon 			error = EINVAL;
988fb99ab88SMatthew Dillon 			goto done2;
989fb99ab88SMatthew Dillon 		}
9908f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
9918f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
992fb99ab88SMatthew Dillon 				error = ENOMEM;
993fb99ab88SMatthew Dillon 				goto done2;
994fb99ab88SMatthew Dillon 			}
995628d2653SJohn Baldwin 			PROC_LOCK(p);
9968f19eb88SIan Dowse 			p->p_sigstk = *ss;
997d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
998628d2653SJohn Baldwin 			PROC_UNLOCK(p);
999628d2653SJohn Baldwin 		} else {
1000628d2653SJohn Baldwin 			PROC_LOCK(p);
1001d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
1002628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1003628d2653SJohn Baldwin 		}
1004d034d459SMarcel Moolenaar 	}
1005fb99ab88SMatthew Dillon done2:
1006fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1007fb99ab88SMatthew Dillon 	return (error);
1008df8bae1dSRodney W. Grimes }
1009df8bae1dSRodney W. Grimes 
1010d93f860cSPoul-Henning Kamp /*
1011d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1012d93f860cSPoul-Henning Kamp  * cp is calling process.
1013d93f860cSPoul-Henning Kamp  */
1014d93f860cSPoul-Henning Kamp int
10159c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
10169c1ab3e0SJohn Baldwin 	register struct thread *td;
10172c42a146SMarcel Moolenaar 	int sig, pgid, all;
1018d93f860cSPoul-Henning Kamp {
1019d93f860cSPoul-Henning Kamp 	register struct proc *p;
1020d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1021d93f860cSPoul-Henning Kamp 	int nfound = 0;
1022d93f860cSPoul-Henning Kamp 
1023553629ebSJake Burkholder 	if (all) {
1024d93f860cSPoul-Henning Kamp 		/*
1025d93f860cSPoul-Henning Kamp 		 * broadcast
1026d93f860cSPoul-Henning Kamp 		 */
10271005a129SJohn Baldwin 		sx_slock(&allproc_lock);
10282e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1029628d2653SJohn Baldwin 			PROC_LOCK(p);
10309c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
10319c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1032628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1033628d2653SJohn Baldwin 				continue;
1034628d2653SJohn Baldwin 			}
1035f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1036d93f860cSPoul-Henning Kamp 				nfound++;
103733a9ed9dSJohn Baldwin 				if (sig)
10382c42a146SMarcel Moolenaar 					psignal(p, sig);
1039628d2653SJohn Baldwin 			}
104033a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1041d93f860cSPoul-Henning Kamp 		}
10421005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1043553629ebSJake Burkholder 	} else {
1044ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1045f591779bSSeigo Tanimura 		if (pgid == 0) {
1046d93f860cSPoul-Henning Kamp 			/*
1047d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1048d93f860cSPoul-Henning Kamp 			 */
10499c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1050f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1051f591779bSSeigo Tanimura 		} else {
1052d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1053f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1054ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1055d93f860cSPoul-Henning Kamp 				return (ESRCH);
1056d93f860cSPoul-Henning Kamp 			}
1057f591779bSSeigo Tanimura 		}
1058ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
10592e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1060628d2653SJohn Baldwin 			PROC_LOCK(p);
1061628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1062628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1063628d2653SJohn Baldwin 				continue;
1064628d2653SJohn Baldwin 			}
1065e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
106633a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1067628d2653SJohn Baldwin 				continue;
1068628d2653SJohn Baldwin 			}
1069f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1070d93f860cSPoul-Henning Kamp 				nfound++;
107133a9ed9dSJohn Baldwin 				if (sig)
10722c42a146SMarcel Moolenaar 					psignal(p, sig);
1073628d2653SJohn Baldwin 			}
107433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1075d93f860cSPoul-Henning Kamp 		}
1076f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1077d93f860cSPoul-Henning Kamp 	}
1078d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1079d93f860cSPoul-Henning Kamp }
1080d93f860cSPoul-Henning Kamp 
1081d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1082df8bae1dSRodney W. Grimes struct kill_args {
1083df8bae1dSRodney W. Grimes 	int	pid;
1084df8bae1dSRodney W. Grimes 	int	signum;
1085df8bae1dSRodney W. Grimes };
1086d2d3e875SBruce Evans #endif
1087fb99ab88SMatthew Dillon /*
1088fb99ab88SMatthew Dillon  * MPSAFE
1089fb99ab88SMatthew Dillon  */
1090df8bae1dSRodney W. Grimes /* ARGSUSED */
109126f9a767SRodney W. Grimes int
1092b40ce416SJulian Elischer kill(td, uap)
1093b40ce416SJulian Elischer 	register struct thread *td;
1094df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1095df8bae1dSRodney W. Grimes {
1096df8bae1dSRodney W. Grimes 	register struct proc *p;
1097fb99ab88SMatthew Dillon 	int error = 0;
1098df8bae1dSRodney W. Grimes 
10996c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1100df8bae1dSRodney W. Grimes 		return (EINVAL);
1101fb99ab88SMatthew Dillon 
1102fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1103df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1104df8bae1dSRodney W. Grimes 		/* kill single process */
1105fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1106fb99ab88SMatthew Dillon 			error = ESRCH;
1107f44d9e24SJohn Baldwin 		} else if ((error = p_cansignal(td, p, uap->signum)) != 0) {
110833a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1109fb99ab88SMatthew Dillon 		} else {
111033a9ed9dSJohn Baldwin 			if (uap->signum)
1111df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1112628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1113fb99ab88SMatthew Dillon 			error = 0;
1114df8bae1dSRodney W. Grimes 		}
1115fb99ab88SMatthew Dillon 	} else {
1116df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1117df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
11189c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 1);
1119fb99ab88SMatthew Dillon 			break;
1120df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
11219c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 0);
1122fb99ab88SMatthew Dillon 			break;
1123df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
11249c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, -uap->pid, 0);
1125fb99ab88SMatthew Dillon 			break;
1126df8bae1dSRodney W. Grimes 		}
1127fb99ab88SMatthew Dillon 	}
1128fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1129fb99ab88SMatthew Dillon 	return(error);
1130df8bae1dSRodney W. Grimes }
1131df8bae1dSRodney W. Grimes 
1132df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1133d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1134df8bae1dSRodney W. Grimes struct okillpg_args {
1135df8bae1dSRodney W. Grimes 	int	pgid;
1136df8bae1dSRodney W. Grimes 	int	signum;
1137df8bae1dSRodney W. Grimes };
1138d2d3e875SBruce Evans #endif
1139fb99ab88SMatthew Dillon /*
1140fb99ab88SMatthew Dillon  * MPSAFE
1141fb99ab88SMatthew Dillon  */
1142df8bae1dSRodney W. Grimes /* ARGSUSED */
114326f9a767SRodney W. Grimes int
1144b40ce416SJulian Elischer okillpg(td, uap)
1145b40ce416SJulian Elischer 	struct thread *td;
1146df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1147df8bae1dSRodney W. Grimes {
1148fb99ab88SMatthew Dillon 	int error;
1149df8bae1dSRodney W. Grimes 
11506c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1151df8bae1dSRodney W. Grimes 		return (EINVAL);
1152fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
11539c1ab3e0SJohn Baldwin 	error = killpg1(td, uap->signum, uap->pgid, 0);
1154fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1155fb99ab88SMatthew Dillon 	return (error);
1156df8bae1dSRodney W. Grimes }
1157df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1158df8bae1dSRodney W. Grimes 
1159df8bae1dSRodney W. Grimes /*
1160df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1161df8bae1dSRodney W. Grimes  */
1162df8bae1dSRodney W. Grimes void
11632c42a146SMarcel Moolenaar gsignal(pgid, sig)
11642c42a146SMarcel Moolenaar 	int pgid, sig;
1165df8bae1dSRodney W. Grimes {
1166df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1167df8bae1dSRodney W. Grimes 
1168f591779bSSeigo Tanimura 	if (pgid != 0) {
1169ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1170f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1171ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1172f591779bSSeigo Tanimura 		if (pgrp != NULL) {
11732c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1174f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1175f591779bSSeigo Tanimura 		}
1176f591779bSSeigo Tanimura 	}
1177df8bae1dSRodney W. Grimes }
1178df8bae1dSRodney W. Grimes 
1179df8bae1dSRodney W. Grimes /*
1180df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1181df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1182df8bae1dSRodney W. Grimes  */
1183df8bae1dSRodney W. Grimes void
11842c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1185df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
11862c42a146SMarcel Moolenaar 	int sig, checkctty;
1187df8bae1dSRodney W. Grimes {
1188df8bae1dSRodney W. Grimes 	register struct proc *p;
1189df8bae1dSRodney W. Grimes 
1190628d2653SJohn Baldwin 	if (pgrp) {
1191f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1192628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1193628d2653SJohn Baldwin 			PROC_LOCK(p);
1194df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
11952c42a146SMarcel Moolenaar 				psignal(p, sig);
1196628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1197628d2653SJohn Baldwin 		}
1198628d2653SJohn Baldwin 	}
1199df8bae1dSRodney W. Grimes }
1200df8bae1dSRodney W. Grimes 
1201df8bae1dSRodney W. Grimes /*
1202df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1203df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1204df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1205356861dbSMatthew Dillon  *
1206356861dbSMatthew Dillon  * MPSAFE
1207df8bae1dSRodney W. Grimes  */
1208df8bae1dSRodney W. Grimes void
12092c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1210df8bae1dSRodney W. Grimes 	struct proc *p;
12112c42a146SMarcel Moolenaar 	register int sig;
12128674077aSJeffrey Hsu 	u_long code;
1213df8bae1dSRodney W. Grimes {
1214df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1215df8bae1dSRodney W. Grimes 
1216628d2653SJohn Baldwin 	PROC_LOCK(p);
12172c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1218d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1219df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1220df8bae1dSRodney W. Grimes #ifdef KTRACE
1221374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1222374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
12232c42a146SMarcel Moolenaar 			    &p->p_sigmask, code);
1224df8bae1dSRodney W. Grimes #endif
12252c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
12262c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
12272c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
12282c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
12292c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
12302c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1231289ccde0SPeter Wemm 			/*
12328f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1233289ccde0SPeter Wemm 			 */
12342c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
12352c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
12362c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
12372c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
12382c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1239dedc04feSPeter Wemm 		}
12406f841fb7SMarcel Moolenaar 	} else {
12416626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
12422c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
12432c42a146SMarcel Moolenaar 		psignal(p, sig);
1244df8bae1dSRodney W. Grimes 	}
1245628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1246df8bae1dSRodney W. Grimes }
1247df8bae1dSRodney W. Grimes 
1248df8bae1dSRodney W. Grimes /*
1249df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1250df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1251df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1252df8bae1dSRodney W. Grimes  *
1253df8bae1dSRodney W. Grimes  * Exceptions:
1254df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1255df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1256df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1257df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1258df8bae1dSRodney W. Grimes  *
1259df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1260df8bae1dSRodney W. Grimes  */
1261df8bae1dSRodney W. Grimes void
12622c42a146SMarcel Moolenaar psignal(p, sig)
1263df8bae1dSRodney W. Grimes 	register struct proc *p;
12642c42a146SMarcel Moolenaar 	register int sig;
1265df8bae1dSRodney W. Grimes {
1266df8bae1dSRodney W. Grimes 	register sig_t action;
1267b40ce416SJulian Elischer 	struct thread *td;
1268e602ba25SJulian Elischer 	register int prop;
1269e602ba25SJulian Elischer 
1270df8bae1dSRodney W. Grimes 
12712899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
12722899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
12732c42a146SMarcel Moolenaar 
1274628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1275cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1276cb679c38SJonathan Lemon 
12772c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1278df8bae1dSRodney W. Grimes 	/*
12792a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
12802a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
12812a024a2bSSean Eric Fagan 	 * a chance, as well.
1282df8bae1dSRodney W. Grimes 	 */
1283b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1284df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1285b40ce416SJulian Elischer 	} else {
1286df8bae1dSRodney W. Grimes 		/*
1287df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1288df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1289df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1290df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1291df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1292df8bae1dSRodney W. Grimes 		 */
1293628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1294df8bae1dSRodney W. Grimes 			return;
12952c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1296df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
12972c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1298df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1299df8bae1dSRodney W. Grimes 		else
1300df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1301df8bae1dSRodney W. Grimes 	}
1302df8bae1dSRodney W. Grimes 
1303df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
13042c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1305df8bae1dSRodney W. Grimes 
1306df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1307df8bae1dSRodney W. Grimes 		/*
1308df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1309df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1310df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1311df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1312df8bae1dSRodney W. Grimes 		 */
1313e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1314e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1315e602ba25SJulian Elischer 		    (action == SIG_DFL))
1316df8bae1dSRodney W. Grimes 		        return;
13172c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
13186933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1319df8bae1dSRodney W. Grimes 	}
13202c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1321aa0fa334SJulian Elischer 	signotify(p);			/* uses schedlock */
1322df8bae1dSRodney W. Grimes 
1323df8bae1dSRodney W. Grimes 	/*
1324e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1325e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1326e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1327e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1328e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1329e602ba25SJulian Elischer 	 * We try do the per-process part here.
1330df8bae1dSRodney W. Grimes 	 */
1331e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1332e602ba25SJulian Elischer 		/*
1333e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1334e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1335e602ba25SJulian Elischer 		 */
1336e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1337e602ba25SJulian Elischer 			/*
1338e602ba25SJulian Elischer 			 * The traced process is already stopped,
1339e602ba25SJulian Elischer 			 * so no further action is necessary.
1340e602ba25SJulian Elischer 			 * No signal can restart us.
1341e602ba25SJulian Elischer 			 */
1342e602ba25SJulian Elischer 			goto out;
13431c32c37cSJohn Baldwin 		}
1344b40ce416SJulian Elischer 
1345e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1346df8bae1dSRodney W. Grimes 			/*
1347e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1348e602ba25SJulian Elischer 			 * It will die elsewhere.
1349e602ba25SJulian Elischer 			 * All threads must be restarted.
1350df8bae1dSRodney W. Grimes 			 */
1351e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1352e602ba25SJulian Elischer 			goto runfast;
1353e602ba25SJulian Elischer 		}
1354e602ba25SJulian Elischer 
1355e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1356e602ba25SJulian Elischer 			/*
1357e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
1358e602ba25SJulian Elischer 			 * process but don't leave the signal in p_siglist as
1359e602ba25SJulian Elischer 			 * it has no further action.  If SIGCONT is held, we
1360e602ba25SJulian Elischer 			 * continue the process and leave the signal in
1361e602ba25SJulian Elischer 			 * p_siglist.  If the process catches SIGCONT, let it
1362e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1363e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1364e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1365e602ba25SJulian Elischer 			 */
1366e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_SGNL;
13676933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1368e602ba25SJulian Elischer 			if (action == SIG_DFL) {
1369e602ba25SJulian Elischer 				SIGDELSET(p->p_siglist, sig);
1370e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1371e602ba25SJulian Elischer 				/*
1372e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1373e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1374e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1375e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1376e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1377e602ba25SJulian Elischer 				 * process, we need to make sure that the
1378e602ba25SJulian Elischer 				 * single thread is runnable asap.
1379e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1380e602ba25SJulian Elischer 				 */
1381e602ba25SJulian Elischer 				goto runfast;
1382e602ba25SJulian Elischer 			}
1383e602ba25SJulian Elischer 			/*
1384e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1385e602ba25SJulian Elischer 			 */
1386e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
138704774f23SJulian Elischer 			thread_unsuspend(p);
1388e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1389e602ba25SJulian Elischer 			goto out;
1390e602ba25SJulian Elischer 		}
1391e602ba25SJulian Elischer 
1392e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1393e602ba25SJulian Elischer 			/*
1394e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1395e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
139604774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1397e602ba25SJulian Elischer 			 */
139804774f23SJulian Elischer 			p->p_flag |= P_STOPPED_SGNL;
1399e602ba25SJulian Elischer 			SIGDELSET(p->p_siglist, sig);
1400e602ba25SJulian Elischer 			goto out;
1401e602ba25SJulian Elischer 		}
1402e602ba25SJulian Elischer 
1403e602ba25SJulian Elischer 		/*
1404e602ba25SJulian Elischer 		 * All other kinds of signals:
1405e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1406e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1407e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
140804774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1409e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1410e602ba25SJulian Elischer 		 */
1411e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
1412e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
1413e602ba25SJulian Elischer 			if (td->td_wchan && (td->td_flags & TDF_SINTR)) {
1414e602ba25SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
1415e602ba25SJulian Elischer 					cv_waitq_remove(td);
1416e602ba25SJulian Elischer 				else
1417e602ba25SJulian Elischer 					unsleep(td);
1418e602ba25SJulian Elischer 				setrunnable(td);
1419e602ba25SJulian Elischer 			}
1420e602ba25SJulian Elischer 		}
1421e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1422df8bae1dSRodney W. Grimes 		goto out;
1423df8bae1dSRodney W. Grimes 		/*
1424e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1425e602ba25SJulian Elischer 		 * Shouldn't they abort too?
142604774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
142704774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1428df8bae1dSRodney W. Grimes 		 */
1429e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
1430e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1431df8bae1dSRodney W. Grimes 			/*
1432e602ba25SJulian Elischer 			 * Already active, don't need to start again.
1433df8bae1dSRodney W. Grimes 			 */
14342c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1435df8bae1dSRodney W. Grimes 			goto out;
1436df8bae1dSRodney W. Grimes 		}
1437df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1438721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
1439721e5910SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td)
1440721e5910SJulian Elischer 				tdsignal(td, sig, action);
1441721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1442df8bae1dSRodney W. Grimes 			goto out;
144304774f23SJulian Elischer 		}
1444721e5910SJulian Elischer 		else
1445df8bae1dSRodney W. Grimes 			goto runfast;
1446df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1447e602ba25SJulian Elischer 	} else {
1448e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
14492c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);
1450e602ba25SJulian Elischer 		goto out;
1451e602ba25SJulian Elischer 	}
1452e602ba25SJulian Elischer 
1453b40ce416SJulian Elischer 	/*
1454e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1455e602ba25SJulian Elischer 	 * running threads.
1456b40ce416SJulian Elischer 	 */
1457e602ba25SJulian Elischer 
1458e602ba25SJulian Elischer runfast:
1459aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
1460e602ba25SJulian Elischer 	FOREACH_THREAD_IN_PROC(p, td)
1461e602ba25SJulian Elischer 		tdsignal(td, sig, action);
1462e602ba25SJulian Elischer 	thread_unsuspend(p);
1463e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1464e602ba25SJulian Elischer out:
1465e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1466e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1467e602ba25SJulian Elischer }
1468e602ba25SJulian Elischer 
1469e602ba25SJulian Elischer /*
1470e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1471e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1472e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1473e602ba25SJulian Elischer  */
1474e602ba25SJulian Elischer static void
1475e602ba25SJulian Elischer tdsignal(struct thread *td, int sig, sig_t action)
1476e602ba25SJulian Elischer {
1477e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1478e602ba25SJulian Elischer 	register int prop;
1479e602ba25SJulian Elischer 
1480aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1481e602ba25SJulian Elischer 	prop = sigprop(sig);
1482e602ba25SJulian Elischer 	/*
1483aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1484e602ba25SJulian Elischer 	 * killed in this lifetime.
1485e602ba25SJulian Elischer 	 */
1486e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1487e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1488e602ba25SJulian Elischer 			td->td_priority = PUSER;
1489b40ce416SJulian Elischer 		}
1490b40ce416SJulian Elischer 	}
1491e602ba25SJulian Elischer 
1492e602ba25SJulian Elischer 	/*
1493e602ba25SJulian Elischer 	 * Defer further processing for signals which are held,
1494e602ba25SJulian Elischer 	 * except that stopped processes must be continued by SIGCONT.
1495e602ba25SJulian Elischer 	 */
1496e602ba25SJulian Elischer 	if (action == SIG_HOLD) {
1497aa0fa334SJulian Elischer 		return;
1498e602ba25SJulian Elischer 	}
1499e602ba25SJulian Elischer 	if (td->td_state == TDS_SLP) {
1500e602ba25SJulian Elischer 		/*
1501e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1502e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1503e602ba25SJulian Elischer 		 * be noticed when the process returns through
1504e602ba25SJulian Elischer 		 * trap() or syscall().
1505e602ba25SJulian Elischer 		 */
1506e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1507aa0fa334SJulian Elischer 			return;
1508e602ba25SJulian Elischer 		}
1509e602ba25SJulian Elischer 		/*
1510e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1511e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1512e602ba25SJulian Elischer 		 * for its parent.
1513e602ba25SJulian Elischer 		 */
1514e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1515e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1516aa0fa334SJulian Elischer 		} else {
1517aa0fa334SJulian Elischer 
1518df8bae1dSRodney W. Grimes 			/*
1519e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1520e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1521e602ba25SJulian Elischer 			 * be awakened.
1522df8bae1dSRodney W. Grimes 			 */
1523e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
15242c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1525aa0fa334SJulian Elischer 				return;
1526df8bae1dSRodney W. Grimes 			}
1527df8bae1dSRodney W. Grimes 
1528aa0fa334SJulian Elischer 			/*
1529aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1530aa0fa334SJulian Elischer 			 */
1531aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
1532aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1533aa0fa334SJulian Elischer 			}
1534aa0fa334SJulian Elischer 		}
1535aa0fa334SJulian Elischer 		setrunnable(td);
1536aa0fa334SJulian Elischer 	}
1537aa0fa334SJulian Elischer #ifdef SMP
1538aa0fa334SJulian Elischer 	  else {
1539df8bae1dSRodney W. Grimes 		/*
1540e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
1541df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1542df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1543df8bae1dSRodney W. Grimes 		 */
15440ac3b636SAndrew Gallatin 		if (td->td_state == TDS_RUNNING && td != curthread) {
1545e602ba25SJulian Elischer 			forward_signal(td);
1546aa0fa334SJulian Elischer 		}
15470ac3b636SAndrew Gallatin 	}
15483163861cSTor Egge #endif
15496caa8a15SJohn Baldwin }
1550df8bae1dSRodney W. Grimes 
1551df8bae1dSRodney W. Grimes /*
1552df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1553df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1554df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1555df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1556df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1557628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
1558df8bae1dSRodney W. Grimes  * sequence is
1559df8bae1dSRodney W. Grimes  *
1560e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
15612c42a146SMarcel Moolenaar  *		postsig(sig);
1562df8bae1dSRodney W. Grimes  */
156326f9a767SRodney W. Grimes int
1564e602ba25SJulian Elischer issignal(td)
1565e602ba25SJulian Elischer 	struct thread *td;
1566df8bae1dSRodney W. Grimes {
1567e602ba25SJulian Elischer 	struct proc *p;
15682c42a146SMarcel Moolenaar 	sigset_t mask;
15692c42a146SMarcel Moolenaar 	register int sig, prop;
1570df8bae1dSRodney W. Grimes 
1571e602ba25SJulian Elischer 	p = td->td_proc;
1572628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1573b39f3284SJulian Elischer 	WITNESS_SLEEP(1, &p->p_mtx.mtx_object);
1574df8bae1dSRodney W. Grimes 	for (;;) {
15752a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
15762a024a2bSSean Eric Fagan 
15772c42a146SMarcel Moolenaar 		mask = p->p_siglist;
15782c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1579df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
15802c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
1581ca18d53eSChad David 		if (SIGISEMPTY(mask))		/* no signal to send */
1582df8bae1dSRodney W. Grimes 			return (0);
15832c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
15842c42a146SMarcel Moolenaar 		prop = sigprop(sig);
15852a024a2bSSean Eric Fagan 
1586628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
15872a024a2bSSean Eric Fagan 
1588df8bae1dSRodney W. Grimes 		/*
1589df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1590df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1591df8bae1dSRodney W. Grimes 		 */
15922c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
15932c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1594df8bae1dSRodney W. Grimes 			continue;
1595df8bae1dSRodney W. Grimes 		}
1596df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1597df8bae1dSRodney W. Grimes 			/*
1598d8f4f6a4SJonathan Mini 			 * If traced, always stop.
1599df8bae1dSRodney W. Grimes 			 */
16002c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1601628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1602df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1603628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
16049ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
16054d492b43SJulian Elischer 			stop(p);	/* uses schedlock too eventually */
1606e602ba25SJulian Elischer 			td->td_state = TDS_UNQUEUED;
1607c86b6ff5SJohn Baldwin 			PROC_UNLOCK(p);
1608c86b6ff5SJohn Baldwin 			DROP_GIANT();
16092ad7d304SJohn Baldwin 			p->p_stats->p_ru.ru_nivcsw++;
1610df8bae1dSRodney W. Grimes 			mi_switch();
16119ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
161220cdcc5bSJohn Baldwin 			PICKUP_GIANT();
1613628d2653SJohn Baldwin 			PROC_LOCK(p);
1614df8bae1dSRodney W. Grimes 
1615df8bae1dSRodney W. Grimes 			/*
1616df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1617df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1618df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1619df8bae1dSRodney W. Grimes 			 */
1620df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1621df8bae1dSRodney W. Grimes 				continue;
1622df8bae1dSRodney W. Grimes 
1623df8bae1dSRodney W. Grimes 			/*
1624df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1625df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1626df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1627df8bae1dSRodney W. Grimes 			 */
16282c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
16292c42a146SMarcel Moolenaar 			sig = p->p_xstat;
16302c42a146SMarcel Moolenaar 			if (sig == 0)
1631df8bae1dSRodney W. Grimes 				continue;
1632df8bae1dSRodney W. Grimes 
1633df8bae1dSRodney W. Grimes 			/*
1634df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1635df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1636df8bae1dSRodney W. Grimes 			 */
16372c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
16382c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1639df8bae1dSRodney W. Grimes 				continue;
16401c530be4SBruce Evans 			signotify(p);
1641df8bae1dSRodney W. Grimes 		}
1642df8bae1dSRodney W. Grimes 
1643df8bae1dSRodney W. Grimes 		/*
1644df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1645df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1646df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1647df8bae1dSRodney W. Grimes 		 */
16482c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1649df8bae1dSRodney W. Grimes 
16500b53fbe8SBruce Evans 		case (int)SIG_DFL:
1651df8bae1dSRodney W. Grimes 			/*
1652df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1653df8bae1dSRodney W. Grimes 			 */
1654df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1655df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1656df8bae1dSRodney W. Grimes 				/*
1657df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1658df8bae1dSRodney W. Grimes 				 * in init? XXX
1659df8bae1dSRodney W. Grimes 				 */
1660d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
16612c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1662df8bae1dSRodney W. Grimes #endif
1663df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1664df8bae1dSRodney W. Grimes 			}
1665df8bae1dSRodney W. Grimes 			/*
1666df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1667df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1668df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1669df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1670df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1671df8bae1dSRodney W. Grimes 			 */
1672df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1673df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1674df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1675df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
1676df8bae1dSRodney W. Grimes 					break;	/* == ignore */
16772c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1678721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
1679721e5910SJulian Elischer 				if (p->p_suspcount+1 == p->p_numthreads) {
1680721e5910SJulian Elischer 					mtx_unlock_spin(&sched_lock);
1681628d2653SJohn Baldwin 					PROC_LOCK(p->p_pptr);
1682e602ba25SJulian Elischer 					if ((p->p_pptr->p_procsig->ps_flag &
1683e602ba25SJulian Elischer 				    		PS_NOCLDSTOP) == 0) {
1684df8bae1dSRodney W. Grimes 						psignal(p->p_pptr, SIGCHLD);
1685e602ba25SJulian Elischer 					}
1686628d2653SJohn Baldwin 					PROC_UNLOCK(p->p_pptr);
1687d9d6e34fSJulian Elischer 					mtx_lock_spin(&sched_lock);
1688721e5910SJulian Elischer 				}
16895b3047d5SJohn Baldwin 				stop(p);
1690721e5910SJulian Elischer 				p->p_suspcount++;
1691721e5910SJulian Elischer 				td->td_state = TDS_SUSPENDED;
1692721e5910SJulian Elischer 				TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
1693721e5910SJulian Elischer 				PROC_UNLOCK(p);
1694721e5910SJulian Elischer 				DROP_GIANT();
1695721e5910SJulian Elischer 				p->p_stats->p_ru.ru_nivcsw++;
1696721e5910SJulian Elischer 				mi_switch();
1697721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
1698721e5910SJulian Elischer 				PICKUP_GIANT();
1699721e5910SJulian Elischer 				PROC_LOCK(p);
1700df8bae1dSRodney W. Grimes 				break;
1701e602ba25SJulian Elischer 			} else
1702e602ba25SJulian Elischer 			     if (prop & SA_IGNORE) {
1703df8bae1dSRodney W. Grimes 				/*
1704df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1705df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1706df8bae1dSRodney W. Grimes 				 */
1707df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1708df8bae1dSRodney W. Grimes 			} else
17092c42a146SMarcel Moolenaar 				return (sig);
1710df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1711df8bae1dSRodney W. Grimes 
17120b53fbe8SBruce Evans 		case (int)SIG_IGN:
1713df8bae1dSRodney W. Grimes 			/*
1714df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1715df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1716df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1717df8bae1dSRodney W. Grimes 			 */
1718df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1719df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1720df8bae1dSRodney W. Grimes 				printf("issignal\n");
1721df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1722df8bae1dSRodney W. Grimes 
1723df8bae1dSRodney W. Grimes 		default:
1724df8bae1dSRodney W. Grimes 			/*
1725df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1726df8bae1dSRodney W. Grimes 			 * postsig() process it.
1727df8bae1dSRodney W. Grimes 			 */
17282c42a146SMarcel Moolenaar 			return (sig);
1729df8bae1dSRodney W. Grimes 		}
17302c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1731df8bae1dSRodney W. Grimes 	}
1732df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1733df8bae1dSRodney W. Grimes }
1734df8bae1dSRodney W. Grimes 
1735df8bae1dSRodney W. Grimes /*
1736df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1737df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
17385b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
17395b3047d5SJohn Baldwin  * lock held.
1740df8bae1dSRodney W. Grimes  */
17415b3047d5SJohn Baldwin static void
1742df8bae1dSRodney W. Grimes stop(p)
1743df8bae1dSRodney W. Grimes 	register struct proc *p;
1744df8bae1dSRodney W. Grimes {
1745df8bae1dSRodney W. Grimes 
1746628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1747e602ba25SJulian Elischer 	p->p_flag |= P_STOPPED_SGNL;
1748df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
174901609114SAlfred Perlstein 	wakeup(p->p_pptr);
1750df8bae1dSRodney W. Grimes }
1751df8bae1dSRodney W. Grimes 
1752df8bae1dSRodney W. Grimes /*
1753df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1754df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1755df8bae1dSRodney W. Grimes  */
1756df8bae1dSRodney W. Grimes void
17572c42a146SMarcel Moolenaar postsig(sig)
17582c42a146SMarcel Moolenaar 	register int sig;
1759df8bae1dSRodney W. Grimes {
1760b40ce416SJulian Elischer 	struct thread *td = curthread;
1761b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1762628d2653SJohn Baldwin 	struct sigacts *ps;
17632c42a146SMarcel Moolenaar 	sig_t action;
17642c42a146SMarcel Moolenaar 	sigset_t returnmask;
17652c42a146SMarcel Moolenaar 	int code;
1766df8bae1dSRodney W. Grimes 
17672c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
17685526d2d9SEivind Eklund 
17692ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1770628d2653SJohn Baldwin 	ps = p->p_sigacts;
17712c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
17722c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1773df8bae1dSRodney W. Grimes #ifdef KTRACE
1774374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
1775374a15aaSJohn Baldwin 		ktrpsig(sig, action, p->p_flag & P_OLDMASK ?
17762c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1777df8bae1dSRodney W. Grimes #endif
1778628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
17792a024a2bSSean Eric Fagan 
1780df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1781df8bae1dSRodney W. Grimes 		/*
1782df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1783df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1784df8bae1dSRodney W. Grimes 		 */
1785b40ce416SJulian Elischer 		sigexit(td, sig);
1786df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1787df8bae1dSRodney W. Grimes 	} else {
1788df8bae1dSRodney W. Grimes 		/*
1789df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1790df8bae1dSRodney W. Grimes 		 */
17912c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
17925526d2d9SEivind Eklund 		    ("postsig action"));
1793df8bae1dSRodney W. Grimes 		/*
1794df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1795645682fdSLuoqi Chen 		 * occurrences of this signal.
1796df8bae1dSRodney W. Grimes 		 *
1797645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1798df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1799645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1800df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1801df8bae1dSRodney W. Grimes 		 */
1802645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
18036626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1804645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1805df8bae1dSRodney W. Grimes 		} else
1806df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
18072c42a146SMarcel Moolenaar 
18082c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
18092c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
18102c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
18112c42a146SMarcel Moolenaar 
18122c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1813289ccde0SPeter Wemm 			/*
18148f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1815289ccde0SPeter Wemm 			 */
18162c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
18172c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
18182c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
18192c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
18202c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1821dedc04feSPeter Wemm 		}
1822df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
18232c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1824df8bae1dSRodney W. Grimes 			code = 0;
1825df8bae1dSRodney W. Grimes 		} else {
18266626c604SJulian Elischer 			code = p->p_code;
18276626c604SJulian Elischer 			p->p_code = 0;
18286626c604SJulian Elischer 			p->p_sig = 0;
1829df8bae1dSRodney W. Grimes 		}
18302c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1831df8bae1dSRodney W. Grimes 	}
1832df8bae1dSRodney W. Grimes }
1833df8bae1dSRodney W. Grimes 
1834df8bae1dSRodney W. Grimes /*
1835df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1836df8bae1dSRodney W. Grimes  */
183726f9a767SRodney W. Grimes void
1838df8bae1dSRodney W. Grimes killproc(p, why)
1839df8bae1dSRodney W. Grimes 	struct proc *p;
1840df8bae1dSRodney W. Grimes 	char *why;
1841df8bae1dSRodney W. Grimes {
18429081e5e8SJohn Baldwin 
18439081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18440384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
18450384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1846729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1847b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1848df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1849df8bae1dSRodney W. Grimes }
1850df8bae1dSRodney W. Grimes 
1851df8bae1dSRodney W. Grimes /*
1852df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1853df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1854df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1855df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1856df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1857df8bae1dSRodney W. Grimes  * does not return.
1858df8bae1dSRodney W. Grimes  */
185926f9a767SRodney W. Grimes void
1860b40ce416SJulian Elischer sigexit(td, sig)
1861b40ce416SJulian Elischer 	struct thread *td;
18622c42a146SMarcel Moolenaar 	int sig;
1863df8bae1dSRodney W. Grimes {
1864b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1865df8bae1dSRodney W. Grimes 
1866628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1867df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
18682c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
18692c42a146SMarcel Moolenaar 		p->p_sig = sig;
1870c364e17eSAndrey A. Chernov 		/*
1871c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1872c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1873c364e17eSAndrey A. Chernov 		 * these messages.)
1874c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1875c364e17eSAndrey A. Chernov 		 */
1876628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1877c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1878c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1879b40ce416SJulian Elischer 		if (coredump(td) == 0)
18802c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
188157308494SJoerg Wunsch 		if (kern_logsigexit)
188257308494SJoerg Wunsch 			log(LOG_INFO,
188357308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
18843d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
18859c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
18862c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
18872c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1888c31146a1SJohn Baldwin 	} else {
1889628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1890628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1891628d2653SJohn Baldwin 			mtx_lock(&Giant);
1892c31146a1SJohn Baldwin 	}
1893b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1894df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1895df8bae1dSRodney W. Grimes }
1896df8bae1dSRodney W. Grimes 
1897c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1898c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1899c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1900c5edb423SSean Eric Fagan 
1901c5edb423SSean Eric Fagan /*
1902c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1903c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1904c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1905c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1906c5edb423SSean Eric Fagan  *	%P	process id (pid)
1907c5edb423SSean Eric Fagan  *	%U	user id (uid)
1908c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1909c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1910c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1911c5edb423SSean Eric Fagan  */
1912c5edb423SSean Eric Fagan 
1913fca666a1SJulian Elischer static char *
1914c5edb423SSean Eric Fagan expand_name(name, uid, pid)
19158b43b535SAlfred Perlstein 	const char *name;
19168b43b535SAlfred Perlstein 	uid_t uid;
19178b43b535SAlfred Perlstein 	pid_t pid;
19188b43b535SAlfred Perlstein {
19198b43b535SAlfred Perlstein 	const char *format, *appendstr;
1920c5edb423SSean Eric Fagan 	char *temp;
1921c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
19228b43b535SAlfred Perlstein 	size_t i, l, n;
1923c5edb423SSean Eric Fagan 
19248b43b535SAlfred Perlstein 	format = corefilename;
19258b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
19260bfe2990SEivind Eklund 	if (temp == NULL)
19278b43b535SAlfred Perlstein 		return (NULL);
1928ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1929c5edb423SSean Eric Fagan 		switch (format[i]) {
1930c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1931c5edb423SSean Eric Fagan 			i++;
1932c5edb423SSean Eric Fagan 			switch (format[i]) {
1933c5edb423SSean Eric Fagan 			case '%':
19348b43b535SAlfred Perlstein 				appendstr = "%";
1935c5edb423SSean Eric Fagan 				break;
1936c5edb423SSean Eric Fagan 			case 'N':	/* process name */
19378b43b535SAlfred Perlstein 				appendstr = name;
1938c5edb423SSean Eric Fagan 				break;
1939c5edb423SSean Eric Fagan 			case 'P':	/* process id */
19408b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
19418b43b535SAlfred Perlstein 				appendstr = buf;
1942c5edb423SSean Eric Fagan 				break;
1943c5edb423SSean Eric Fagan 			case 'U':	/* user id */
19448b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
19458b43b535SAlfred Perlstein 				appendstr = buf;
1946c5edb423SSean Eric Fagan 				break;
1947c5edb423SSean Eric Fagan 			default:
19488b43b535SAlfred Perlstein 				appendstr = "";
19498b43b535SAlfred Perlstein 			  	log(LOG_ERR,
19508b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
19518b43b535SAlfred Perlstein 				    format[i], format);
1952c5edb423SSean Eric Fagan 			}
19538b43b535SAlfred Perlstein 			l = strlen(appendstr);
19548b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
19558b43b535SAlfred Perlstein 				goto toolong;
19568b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
19578b43b535SAlfred Perlstein 			n += l;
1958c5edb423SSean Eric Fagan 			break;
1959c5edb423SSean Eric Fagan 		default:
1960c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1961c5edb423SSean Eric Fagan 		}
1962c5edb423SSean Eric Fagan 	}
19638b43b535SAlfred Perlstein 	if (format[i] != '\0')
19648b43b535SAlfred Perlstein 		goto toolong;
19658b43b535SAlfred Perlstein 	return (temp);
19668b43b535SAlfred Perlstein toolong:
19678b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
19688b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
19698b43b535SAlfred Perlstein 	free(temp, M_TEMP);
19708b43b535SAlfred Perlstein 	return (NULL);
1971c5edb423SSean Eric Fagan }
1972c5edb423SSean Eric Fagan 
1973df8bae1dSRodney W. Grimes /*
1974fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1975fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1976fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1977fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1978fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1979fca666a1SJulian Elischer  */
1980fca666a1SJulian Elischer 
1981fca666a1SJulian Elischer static int
1982b40ce416SJulian Elischer coredump(struct thread *td)
1983fca666a1SJulian Elischer {
1984b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1985fca666a1SJulian Elischer 	register struct vnode *vp;
19869c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
198706ae1e91SMatthew Dillon 	struct flock lf;
1988fca666a1SJulian Elischer 	struct nameidata nd;
1989fca666a1SJulian Elischer 	struct vattr vattr;
1990e6796b67SKirk McKusick 	int error, error1, flags;
1991f2a2857bSKirk McKusick 	struct mount *mp;
1992fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1993fca666a1SJulian Elischer 	off_t limit;
1994fca666a1SJulian Elischer 
1995628d2653SJohn Baldwin 	PROC_LOCK(p);
1996628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1997fca666a1SJulian Elischer 
1998628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1999628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2000fca666a1SJulian Elischer 		return (EFAULT);
2001628d2653SJohn Baldwin 	}
2002fca666a1SJulian Elischer 
2003fca666a1SJulian Elischer 	/*
200435a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
200535a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
200635a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
200735a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
200835a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
200935a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2010fca666a1SJulian Elischer 	 */
201135a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2012628d2653SJohn Baldwin 	if (limit == 0) {
2013628d2653SJohn Baldwin 		PROC_UNLOCK(p);
201435a2598fSSean Eric Fagan 		return 0;
2015628d2653SJohn Baldwin 	}
2016628d2653SJohn Baldwin 	PROC_UNLOCK(p);
201735a2598fSSean Eric Fagan 
2018f2a2857bSKirk McKusick restart:
20199c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2020ccdbd10cSPeter Pentchev 	if (name == NULL)
2021ccdbd10cSPeter Pentchev 		return (EINVAL);
2022b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2023e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2024e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2025fca666a1SJulian Elischer 	free(name, M_TEMP);
2026fca666a1SJulian Elischer 	if (error)
2027fca666a1SJulian Elischer 		return (error);
2028762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2029fca666a1SJulian Elischer 	vp = nd.ni_vp;
203006ae1e91SMatthew Dillon 
2031832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2032832dafadSDon Lewis 	if (vp->v_type != VREG ||
2033832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2034832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2035832dafadSDon Lewis 		error = EFAULT;
2036832dafadSDon Lewis 		goto out2;
2037832dafadSDon Lewis 	}
2038832dafadSDon Lewis 
2039b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
204006ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
204106ae1e91SMatthew Dillon 	lf.l_start = 0;
204206ae1e91SMatthew Dillon 	lf.l_len = 0;
204306ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
204406ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
204506ae1e91SMatthew Dillon 	if (error)
204606ae1e91SMatthew Dillon 		goto out2;
204706ae1e91SMatthew Dillon 
204806ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
204906ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
205006ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2051b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2052f2a2857bSKirk McKusick 			return (error);
2053f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2054f2a2857bSKirk McKusick 			return (error);
2055f2a2857bSKirk McKusick 		goto restart;
2056f2a2857bSKirk McKusick 	}
2057fca666a1SJulian Elischer 
2058fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2059fca666a1SJulian Elischer 	vattr.va_size = 0;
206088b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2061b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2062b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
206388b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2064628d2653SJohn Baldwin 	PROC_LOCK(p);
2065fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2066628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2067fca666a1SJulian Elischer 
2068fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2069b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2070fca666a1SJulian Elischer 	  ENOSYS;
2071fca666a1SJulian Elischer 
207206ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
207306ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2074f2a2857bSKirk McKusick 	vn_finished_write(mp);
207506ae1e91SMatthew Dillon out2:
2076b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2077fca666a1SJulian Elischer 	if (error == 0)
2078fca666a1SJulian Elischer 		error = error1;
2079fca666a1SJulian Elischer 	return (error);
2080fca666a1SJulian Elischer }
2081fca666a1SJulian Elischer 
2082fca666a1SJulian Elischer /*
2083df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2084df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2085df8bae1dSRodney W. Grimes  */
2086d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2087df8bae1dSRodney W. Grimes struct nosys_args {
2088df8bae1dSRodney W. Grimes 	int	dummy;
2089df8bae1dSRodney W. Grimes };
2090d2d3e875SBruce Evans #endif
2091fb99ab88SMatthew Dillon /*
2092fb99ab88SMatthew Dillon  * MPSAFE
2093fb99ab88SMatthew Dillon  */
2094df8bae1dSRodney W. Grimes /* ARGSUSED */
209526f9a767SRodney W. Grimes int
2096b40ce416SJulian Elischer nosys(td, args)
2097b40ce416SJulian Elischer 	struct thread *td;
2098df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2099df8bae1dSRodney W. Grimes {
2100b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2101b40ce416SJulian Elischer 
2102fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2103628d2653SJohn Baldwin 	PROC_LOCK(p);
2104df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2105628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2106fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2107f5216b9aSBruce Evans 	return (ENOSYS);
2108df8bae1dSRodney W. Grimes }
2109831d27a9SDon Lewis 
2110831d27a9SDon Lewis /*
211148f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2112831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2113831d27a9SDon Lewis  */
2114831d27a9SDon Lewis void
2115f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2116f1320723SAlfred Perlstein 	struct sigio **sigiop;
21172c42a146SMarcel Moolenaar 	int sig, checkctty;
2118831d27a9SDon Lewis {
2119f1320723SAlfred Perlstein 	struct sigio *sigio;
2120831d27a9SDon Lewis 
2121f1320723SAlfred Perlstein 	SIGIO_LOCK();
2122f1320723SAlfred Perlstein 	sigio = *sigiop;
2123f1320723SAlfred Perlstein 	if (sigio == NULL) {
2124f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2125f1320723SAlfred Perlstein 		return;
2126f1320723SAlfred Perlstein 	}
2127831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2128628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
21292b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
21302c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2131628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2132831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2133831d27a9SDon Lewis 		struct proc *p;
2134831d27a9SDon Lewis 
2135f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2136628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2137628d2653SJohn Baldwin 			PROC_LOCK(p);
21382b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2139831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
21402c42a146SMarcel Moolenaar 				psignal(p, sig);
2141628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2142628d2653SJohn Baldwin 		}
2143f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2144831d27a9SDon Lewis 	}
2145f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2146831d27a9SDon Lewis }
2147cb679c38SJonathan Lemon 
2148cb679c38SJonathan Lemon static int
2149cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2150cb679c38SJonathan Lemon {
2151cb679c38SJonathan Lemon 	struct proc *p = curproc;
2152cb679c38SJonathan Lemon 
2153cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2154cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2155cb679c38SJonathan Lemon 
2156628d2653SJohn Baldwin 	PROC_LOCK(p);
2157cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2158628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2159cb679c38SJonathan Lemon 
2160cb679c38SJonathan Lemon 	return (0);
2161cb679c38SJonathan Lemon }
2162cb679c38SJonathan Lemon 
2163cb679c38SJonathan Lemon static void
2164cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2165cb679c38SJonathan Lemon {
2166cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2167cb679c38SJonathan Lemon 
2168628d2653SJohn Baldwin 	PROC_LOCK(p);
2169e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2170628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2171cb679c38SJonathan Lemon }
2172cb679c38SJonathan Lemon 
2173cb679c38SJonathan Lemon /*
2174cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2175cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2176cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2177cb679c38SJonathan Lemon  * isn't worth the trouble.
2178cb679c38SJonathan Lemon  */
2179cb679c38SJonathan Lemon static int
2180cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2181cb679c38SJonathan Lemon {
2182cb679c38SJonathan Lemon 
2183cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2184cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2185cb679c38SJonathan Lemon 
2186cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2187cb679c38SJonathan Lemon 			kn->kn_data++;
2188cb679c38SJonathan Lemon 	}
2189cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2190cb679c38SJonathan Lemon }
2191