xref: /freebsd/sys/kern/kern_sig.c (revision 9b3b1c5fdf07ad8d088a5730514acf00f01252c0)
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>
671005a129SJohn Baldwin #include <sys/syslog.h>
68d66a5066SPeter Wemm #include <sys/sysent.h>
69c87e2930SDavid Greenman #include <sys/sysctl.h>
70c5edb423SSean Eric Fagan #include <sys/malloc.h>
7106ae1e91SMatthew Dillon #include <sys/unistd.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <machine/cpu.h>
74df8bae1dSRodney W. Grimes 
756f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
766f841fb7SMarcel Moolenaar 
774d77a549SAlfred Perlstein static int coredump(struct thread *);
784d77a549SAlfred Perlstein static int do_sigaction(struct proc *p, int sig, struct sigaction *act,
794d77a549SAlfred Perlstein 			struct sigaction *oact, int old);
804d77a549SAlfred Perlstein static int do_sigprocmask(struct proc *p, int how, sigset_t *set,
814d77a549SAlfred Perlstein 			  sigset_t *oset, int old);
824d77a549SAlfred Perlstein static char *expand_name(const char *, uid_t, pid_t);
839c1ab3e0SJohn Baldwin static int killpg1(struct thread *td, int sig, int pgid, int all);
844d77a549SAlfred Perlstein static int sig_ffs(sigset_t *set);
854d77a549SAlfred Perlstein static int sigprop(int sig);
864d77a549SAlfred Perlstein static void stop(struct proc *);
8726f9a767SRodney W. Grimes 
88cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
89cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
90cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
91cb679c38SJonathan Lemon 
92cb679c38SJonathan Lemon struct filterops sig_filtops =
93cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
94cb679c38SJonathan Lemon 
9557308494SJoerg Wunsch static int	kern_logsigexit = 1;
963d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
973d177f46SBill Fumerola     &kern_logsigexit, 0,
983d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
9957308494SJoerg Wunsch 
1002b87b6d4SRobert Watson /*
1012b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1022b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1032b87b6d4SRobert Watson  * in the right situations.
1042b87b6d4SRobert Watson  */
1052b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1062b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1072b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1082b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1092b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1102b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1112b87b6d4SRobert Watson 
11222d4b0fbSJohn Polstra int sugid_coredump;
1133d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1143d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
115c87e2930SDavid Greenman 
116e5a28db9SPaul Saab static int	do_coredump = 1;
117e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
118e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
119e5a28db9SPaul Saab 
1202c42a146SMarcel Moolenaar /*
1212c42a146SMarcel Moolenaar  * Signal properties and actions.
1222c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1232c42a146SMarcel Moolenaar  * according to the following properties:
1242c42a146SMarcel Moolenaar  */
1252c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1262c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1272c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1282c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1292c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1302c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1312c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
132df8bae1dSRodney W. Grimes 
1332c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1342c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1352c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1362c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1372c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1382c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1392c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1412c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1422c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1462c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1472c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1482c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1492c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1502c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1512c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1522c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1532c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1542c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1552c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1562c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1572c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1582c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1592c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1602c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1612c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1622c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1632c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1642c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1652c42a146SMarcel Moolenaar };
1662c42a146SMarcel Moolenaar 
167fbbeeb6cSBruce Evans /*
168fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
169fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
170fbbeeb6cSBruce Evans  * action, the process stops in issignal().
171fbbeeb6cSBruce Evans  *
17233510ef1SBruce Evans  * MP SAFE.
173fbbeeb6cSBruce Evans  */
174fbbeeb6cSBruce Evans int
175fbbeeb6cSBruce Evans CURSIG(struct proc *p)
176fbbeeb6cSBruce Evans {
177fbbeeb6cSBruce Evans 
1782ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
179179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
180179235b3SBruce Evans 	return (SIGPENDING(p) ? issignal(p) : 0);
181fbbeeb6cSBruce Evans }
182fbbeeb6cSBruce Evans 
18379065dbaSBruce Evans /*
18479065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
18579065dbaSBruce Evans  * mode.  This must be called whenever a signal is added to p_siglist or
18679065dbaSBruce Evans  * unmasked in p_sigmask.
18779065dbaSBruce Evans  */
18879065dbaSBruce Evans void
18979065dbaSBruce Evans signotify(struct proc *p)
19079065dbaSBruce Evans {
19179065dbaSBruce Evans 
19279065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
19379065dbaSBruce Evans 	mtx_lock_spin(&sched_lock);
19479065dbaSBruce Evans 	if (SIGPENDING(p)) {
19579065dbaSBruce Evans 		p->p_sflag |= PS_NEEDSIGCHK;
19679065dbaSBruce Evans 		p->p_kse.ke_flags |= KEF_ASTPENDING;	/* XXXKSE */
19779065dbaSBruce Evans 	}
19879065dbaSBruce Evans 	mtx_unlock_spin(&sched_lock);
19979065dbaSBruce Evans }
20079065dbaSBruce Evans 
2016f841fb7SMarcel Moolenaar static __inline int
2026f841fb7SMarcel Moolenaar sigprop(int sig)
2032c42a146SMarcel Moolenaar {
2046f841fb7SMarcel Moolenaar 
2052c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2062c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2072c42a146SMarcel Moolenaar 	return (0);
208df8bae1dSRodney W. Grimes }
2092c42a146SMarcel Moolenaar 
2106f841fb7SMarcel Moolenaar static __inline int
2116f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2122c42a146SMarcel Moolenaar {
2132c42a146SMarcel Moolenaar 	int i;
2142c42a146SMarcel Moolenaar 
2156f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2162c42a146SMarcel Moolenaar 		if (set->__bits[i])
2172c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
218df8bae1dSRodney W. Grimes 	return (0);
219df8bae1dSRodney W. Grimes }
220df8bae1dSRodney W. Grimes 
2212c42a146SMarcel Moolenaar /*
2222c42a146SMarcel Moolenaar  * do_sigaction
2232c42a146SMarcel Moolenaar  * sigaction
2242c42a146SMarcel Moolenaar  * osigaction
2252c42a146SMarcel Moolenaar  */
2262c42a146SMarcel Moolenaar static int
227645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
2282c42a146SMarcel Moolenaar 	struct proc *p;
2292c42a146SMarcel Moolenaar 	register int sig;
2302c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
231645682fdSLuoqi Chen 	int old;
232df8bae1dSRodney W. Grimes {
233628d2653SJohn Baldwin 	register struct sigacts *ps;
234df8bae1dSRodney W. Grimes 
2352899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2362c42a146SMarcel Moolenaar 		return (EINVAL);
2372c42a146SMarcel Moolenaar 
238628d2653SJohn Baldwin 	PROC_LOCK(p);
239628d2653SJohn Baldwin 	ps = p->p_sigacts;
2402c42a146SMarcel Moolenaar 	if (oact) {
2412c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2422c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2432c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2442c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2452c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2462c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2472c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2482c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2492c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2502c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2512c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2522c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2532c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
254645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2552c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
256645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2572c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2582c42a146SMarcel Moolenaar 	}
2592c42a146SMarcel Moolenaar 	if (act) {
2602c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
261628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
262628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2632c42a146SMarcel Moolenaar 			return (EINVAL);
264628d2653SJohn Baldwin 		}
2652c42a146SMarcel Moolenaar 
266df8bae1dSRodney W. Grimes 		/*
267df8bae1dSRodney W. Grimes 		 * Change setting atomically.
268df8bae1dSRodney W. Grimes 		 */
2692c42a146SMarcel Moolenaar 
2702c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2712c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2722c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
273aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
274aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
27580f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
27680f42b55SIan Dowse 		} else {
27780f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2782c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2792c42a146SMarcel Moolenaar 		}
2802c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2812c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
282df8bae1dSRodney W. Grimes 		else
2832c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2842c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2852c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
286df8bae1dSRodney W. Grimes 		else
2872c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2882c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2892c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2901e41c1b5SSteven Wallace 		else
2912c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2922c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2932c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
294289ccde0SPeter Wemm 		else
2952c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
296df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2972c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2982c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
299df8bae1dSRodney W. Grimes 		else
3008c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
301df8bae1dSRodney W. Grimes #endif
3022c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3032c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
304645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
3056626c604SJulian Elischer 			else
306645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
307ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
308245f17d4SJoerg Wunsch 				/*
3092c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3102c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3112c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3122c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
313245f17d4SJoerg Wunsch 				 */
314245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
315645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3166626c604SJulian Elischer 				else
317645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
318245f17d4SJoerg Wunsch 			} else
319645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
320ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
321ba1551caSIan Dowse 				p->p_procsig->ps_flag |= PS_CLDSIGIGN;
322ba1551caSIan Dowse 			else
323ba1551caSIan Dowse 				p->p_procsig->ps_flag &= ~PS_CLDSIGIGN;
324df8bae1dSRodney W. Grimes 		}
325df8bae1dSRodney W. Grimes 		/*
326df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3272c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3282c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3292c42a146SMarcel Moolenaar 		 * have to restart the process.
330df8bae1dSRodney W. Grimes 		 */
3312c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3322c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3332c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3342c42a146SMarcel Moolenaar 			/* never to be seen again */
3352c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3362c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3372c42a146SMarcel Moolenaar 				/* easier in psignal */
3382c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3392c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
340645682fdSLuoqi Chen 		} else {
3412c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3422c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3432c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3442c42a146SMarcel Moolenaar 			else
3452c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3462c42a146SMarcel Moolenaar 		}
347e8ebc08fSPeter Wemm #ifdef COMPAT_43
348645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
349645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
350645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
351645682fdSLuoqi Chen 		else
352645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
353e8ebc08fSPeter Wemm #endif
354df8bae1dSRodney W. Grimes 	}
355628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3562c42a146SMarcel Moolenaar 	return (0);
3572c42a146SMarcel Moolenaar }
3582c42a146SMarcel Moolenaar 
3592c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3602c42a146SMarcel Moolenaar struct sigaction_args {
3612c42a146SMarcel Moolenaar 	int	sig;
3622c42a146SMarcel Moolenaar 	struct	sigaction *act;
3632c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3642c42a146SMarcel Moolenaar };
3652c42a146SMarcel Moolenaar #endif
366fb99ab88SMatthew Dillon /*
367fb99ab88SMatthew Dillon  * MPSAFE
368fb99ab88SMatthew Dillon  */
3692c42a146SMarcel Moolenaar /* ARGSUSED */
3702c42a146SMarcel Moolenaar int
371b40ce416SJulian Elischer sigaction(td, uap)
372b40ce416SJulian Elischer 	struct thread *td;
3732c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3742c42a146SMarcel Moolenaar {
375b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3762c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3772c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3782c42a146SMarcel Moolenaar 	int error;
3792c42a146SMarcel Moolenaar 
380fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
381fb99ab88SMatthew Dillon 
3826f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3836f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3842c42a146SMarcel Moolenaar 	if (actp) {
3856f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3862c42a146SMarcel Moolenaar 		if (error)
387fb99ab88SMatthew Dillon 			goto done2;
3882c42a146SMarcel Moolenaar 	}
389645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3902c42a146SMarcel Moolenaar 	if (oactp && !error) {
3916f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3922c42a146SMarcel Moolenaar 	}
393fb99ab88SMatthew Dillon done2:
394fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
3952c42a146SMarcel Moolenaar 	return (error);
3962c42a146SMarcel Moolenaar }
3972c42a146SMarcel Moolenaar 
39831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3992c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4002c42a146SMarcel Moolenaar struct osigaction_args {
4012c42a146SMarcel Moolenaar 	int	signum;
4022c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4032c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4042c42a146SMarcel Moolenaar };
4052c42a146SMarcel Moolenaar #endif
406fb99ab88SMatthew Dillon /*
407fb99ab88SMatthew Dillon  * MPSAFE
408fb99ab88SMatthew Dillon  */
4092c42a146SMarcel Moolenaar /* ARGSUSED */
4102c42a146SMarcel Moolenaar int
411b40ce416SJulian Elischer osigaction(td, uap)
412b40ce416SJulian Elischer 	struct thread *td;
4132c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4142c42a146SMarcel Moolenaar {
415b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
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 	}
437645682fdSLuoqi Chen 	error = do_sigaction(p, 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 {
690b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
691df8bae1dSRodney W. Grimes 	struct sigvec vec;
6922c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6932c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6942c42a146SMarcel Moolenaar 	int error;
695df8bae1dSRodney W. Grimes 
6966f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6976f841fb7SMarcel Moolenaar 		return (EINVAL);
6986f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6996f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
7002c42a146SMarcel Moolenaar 	if (nsap) {
7016f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
7022c42a146SMarcel Moolenaar 		if (error)
703df8bae1dSRodney W. Grimes 			return (error);
7042c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
7052c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
7062c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
7072c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
708df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
7092c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
710df8bae1dSRodney W. Grimes #endif
711df8bae1dSRodney W. Grimes 	}
712fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
713645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
714fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
7152c42a146SMarcel Moolenaar 	if (osap && !error) {
7162c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7172c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7182c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7192c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7202c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7212c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7222c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7232c42a146SMarcel Moolenaar #endif
7246f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
7252c42a146SMarcel Moolenaar 	}
7262c42a146SMarcel Moolenaar 	return (error);
727df8bae1dSRodney W. Grimes }
728df8bae1dSRodney W. Grimes 
729d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
730df8bae1dSRodney W. Grimes struct osigblock_args {
731df8bae1dSRodney W. Grimes 	int	mask;
732df8bae1dSRodney W. Grimes };
733d2d3e875SBruce Evans #endif
734fb99ab88SMatthew Dillon /*
735fb99ab88SMatthew Dillon  * MPSAFE
736fb99ab88SMatthew Dillon  */
73726f9a767SRodney W. Grimes int
738b40ce416SJulian Elischer osigblock(td, uap)
739b40ce416SJulian Elischer 	register struct thread *td;
740df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
741df8bae1dSRodney W. Grimes {
742b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7432c42a146SMarcel Moolenaar 	sigset_t set;
744df8bae1dSRodney W. Grimes 
7452c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7462c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
747fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
748628d2653SJohn Baldwin 	PROC_LOCK(p);
749b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
7502c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
751628d2653SJohn Baldwin 	PROC_UNLOCK(p);
752fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
753df8bae1dSRodney W. Grimes 	return (0);
754df8bae1dSRodney W. Grimes }
755df8bae1dSRodney W. Grimes 
756d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
757df8bae1dSRodney W. Grimes struct osigsetmask_args {
758df8bae1dSRodney W. Grimes 	int	mask;
759df8bae1dSRodney W. Grimes };
760d2d3e875SBruce Evans #endif
761fb99ab88SMatthew Dillon /*
762fb99ab88SMatthew Dillon  * MPSAFE
763fb99ab88SMatthew Dillon  */
76426f9a767SRodney W. Grimes int
765b40ce416SJulian Elischer osigsetmask(td, uap)
766b40ce416SJulian Elischer 	struct thread *td;
767df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
768df8bae1dSRodney W. Grimes {
769b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7702c42a146SMarcel Moolenaar 	sigset_t set;
771df8bae1dSRodney W. Grimes 
7722c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7732c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
774fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
775628d2653SJohn Baldwin 	PROC_LOCK(p);
776b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
777645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
77879065dbaSBruce Evans 	signotify(p);
779628d2653SJohn Baldwin 	PROC_UNLOCK(p);
780fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
781df8bae1dSRodney W. Grimes 	return (0);
782df8bae1dSRodney W. Grimes }
783df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
784df8bae1dSRodney W. Grimes 
785df8bae1dSRodney W. Grimes /*
786df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
787df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
788df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
789b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
790b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
791b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
792df8bae1dSRodney W. Grimes  */
793d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
794df8bae1dSRodney W. Grimes struct sigsuspend_args {
7952c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
796df8bae1dSRodney W. Grimes };
797d2d3e875SBruce Evans #endif
798fb99ab88SMatthew Dillon /*
799fb99ab88SMatthew Dillon  * MPSAFE
800fb99ab88SMatthew Dillon  */
801df8bae1dSRodney W. Grimes /* ARGSUSED */
80226f9a767SRodney W. Grimes int
803b40ce416SJulian Elischer sigsuspend(td, uap)
804b40ce416SJulian Elischer 	struct thread *td;
805df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
806df8bae1dSRodney W. Grimes {
807b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
8082c42a146SMarcel Moolenaar 	sigset_t mask;
809628d2653SJohn Baldwin 	register struct sigacts *ps;
8102c42a146SMarcel Moolenaar 	int error;
8112c42a146SMarcel Moolenaar 
8126f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
8132c42a146SMarcel Moolenaar 	if (error)
8142c42a146SMarcel Moolenaar 		return (error);
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	/*
817645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
818df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
819df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
820df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
821df8bae1dSRodney W. Grimes 	 * to indicate this.
822df8bae1dSRodney W. Grimes 	 */
823fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
824628d2653SJohn Baldwin 	PROC_LOCK(p);
825628d2653SJohn Baldwin 	ps = p->p_sigacts;
8266626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
827645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
828645682fdSLuoqi Chen 
829645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
8302c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
83179065dbaSBruce Evans 	signotify(p);
832628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
8332c42a146SMarcel Moolenaar 		/* void */;
834628d2653SJohn Baldwin 	PROC_UNLOCK(p);
835fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
8362c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
8372c42a146SMarcel Moolenaar 	return (EINTR);
8382c42a146SMarcel Moolenaar }
8392c42a146SMarcel Moolenaar 
84031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8412c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8422c42a146SMarcel Moolenaar struct osigsuspend_args {
8432c42a146SMarcel Moolenaar 	osigset_t mask;
8442c42a146SMarcel Moolenaar };
8452c42a146SMarcel Moolenaar #endif
846fb99ab88SMatthew Dillon /*
847fb99ab88SMatthew Dillon  * MPSAFE
848fb99ab88SMatthew Dillon  */
8492c42a146SMarcel Moolenaar /* ARGSUSED */
8502c42a146SMarcel Moolenaar int
851b40ce416SJulian Elischer osigsuspend(td, uap)
852b40ce416SJulian Elischer 	struct thread *td;
8532c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
8542c42a146SMarcel Moolenaar {
855b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
856645682fdSLuoqi Chen 	sigset_t mask;
857628d2653SJohn Baldwin 	register struct sigacts *ps;
8582c42a146SMarcel Moolenaar 
859fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
860628d2653SJohn Baldwin 	PROC_LOCK(p);
861628d2653SJohn Baldwin 	ps = p->p_sigacts;
8622c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
863645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
864645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
865645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
866645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
86779065dbaSBruce Evans 	signotify(p);
868628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
869df8bae1dSRodney W. Grimes 		/* void */;
870628d2653SJohn Baldwin 	PROC_UNLOCK(p);
871fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
872df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
873df8bae1dSRodney W. Grimes 	return (EINTR);
874df8bae1dSRodney W. Grimes }
87531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
876df8bae1dSRodney W. Grimes 
877df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
878d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
879df8bae1dSRodney W. Grimes struct osigstack_args {
880df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
881df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
882df8bae1dSRodney W. Grimes };
883d2d3e875SBruce Evans #endif
884fb99ab88SMatthew Dillon /*
885fb99ab88SMatthew Dillon  * MPSAFE
886fb99ab88SMatthew Dillon  */
887df8bae1dSRodney W. Grimes /* ARGSUSED */
88826f9a767SRodney W. Grimes int
889b40ce416SJulian Elischer osigstack(td, uap)
890b40ce416SJulian Elischer 	struct thread *td;
891df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
892df8bae1dSRodney W. Grimes {
893b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
894df8bae1dSRodney W. Grimes 	struct sigstack ss;
895fb99ab88SMatthew Dillon 	int error = 0;
896fb99ab88SMatthew Dillon 
897fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
898df8bae1dSRodney W. Grimes 
899d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
900628d2653SJohn Baldwin 		PROC_LOCK(p);
901645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
902b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
903628d2653SJohn Baldwin 		PROC_UNLOCK(p);
904d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
905d034d459SMarcel Moolenaar 		if (error)
906fb99ab88SMatthew Dillon 			goto done2;
907d034d459SMarcel Moolenaar 	}
908d034d459SMarcel Moolenaar 
909d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
910d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
911fb99ab88SMatthew Dillon 			goto done2;
912628d2653SJohn Baldwin 		PROC_LOCK(p);
913645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
914645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
915645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
916645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
917628d2653SJohn Baldwin 		PROC_UNLOCK(p);
918df8bae1dSRodney W. Grimes 	}
919fb99ab88SMatthew Dillon done2:
920fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
921fb99ab88SMatthew Dillon 	return (error);
922df8bae1dSRodney W. Grimes }
923df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
924df8bae1dSRodney W. Grimes 
925d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
926df8bae1dSRodney W. Grimes struct sigaltstack_args {
9272c42a146SMarcel Moolenaar 	stack_t	*ss;
9282c42a146SMarcel Moolenaar 	stack_t	*oss;
929df8bae1dSRodney W. Grimes };
930d2d3e875SBruce Evans #endif
931fb99ab88SMatthew Dillon /*
932fb99ab88SMatthew Dillon  * MPSAFE
933fb99ab88SMatthew Dillon  */
934df8bae1dSRodney W. Grimes /* ARGSUSED */
93526f9a767SRodney W. Grimes int
936b40ce416SJulian Elischer sigaltstack(td, uap)
937b40ce416SJulian Elischer 	struct thread *td;
938df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
939df8bae1dSRodney W. Grimes {
940b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9412c42a146SMarcel Moolenaar 	stack_t ss;
942fb99ab88SMatthew Dillon 	int oonstack;
943fb99ab88SMatthew Dillon 	int error = 0;
944fb99ab88SMatthew Dillon 
945fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
946df8bae1dSRodney W. Grimes 
947b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
948d034d459SMarcel Moolenaar 
949d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
950628d2653SJohn Baldwin 		PROC_LOCK(p);
951d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
952d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
953d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
954628d2653SJohn Baldwin 		PROC_UNLOCK(p);
955d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
956fb99ab88SMatthew Dillon 			goto done2;
957df8bae1dSRodney W. Grimes 	}
958d034d459SMarcel Moolenaar 
959d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
960fb99ab88SMatthew Dillon 		if (oonstack) {
961fb99ab88SMatthew Dillon 			error = EPERM;
962fb99ab88SMatthew Dillon 			goto done2;
963fb99ab88SMatthew Dillon 		}
964d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
965fb99ab88SMatthew Dillon 			goto done2;
966fb99ab88SMatthew Dillon 		if ((ss.ss_flags & ~SS_DISABLE) != 0) {
967fb99ab88SMatthew Dillon 			error = EINVAL;
968fb99ab88SMatthew Dillon 			goto done2;
969fb99ab88SMatthew Dillon 		}
970d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
971fb99ab88SMatthew Dillon 			if (ss.ss_size < p->p_sysent->sv_minsigstksz) {
972fb99ab88SMatthew Dillon 				error = ENOMEM;
973fb99ab88SMatthew Dillon 				goto done2;
974fb99ab88SMatthew Dillon 			}
975628d2653SJohn Baldwin 			PROC_LOCK(p);
976645682fdSLuoqi Chen 			p->p_sigstk = ss;
977d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
978628d2653SJohn Baldwin 			PROC_UNLOCK(p);
979628d2653SJohn Baldwin 		} else {
980628d2653SJohn Baldwin 			PROC_LOCK(p);
981d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
982628d2653SJohn Baldwin 			PROC_UNLOCK(p);
983628d2653SJohn Baldwin 		}
984d034d459SMarcel Moolenaar 	}
985fb99ab88SMatthew Dillon done2:
986fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
987fb99ab88SMatthew Dillon 	return (error);
988df8bae1dSRodney W. Grimes }
989df8bae1dSRodney W. Grimes 
990d93f860cSPoul-Henning Kamp /*
991d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
992d93f860cSPoul-Henning Kamp  * cp is calling process.
993d93f860cSPoul-Henning Kamp  */
994d93f860cSPoul-Henning Kamp int
9959c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
9969c1ab3e0SJohn Baldwin 	register struct thread *td;
9972c42a146SMarcel Moolenaar 	int sig, pgid, all;
998d93f860cSPoul-Henning Kamp {
999d93f860cSPoul-Henning Kamp 	register struct proc *p;
1000d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1001d93f860cSPoul-Henning Kamp 	int nfound = 0;
1002d93f860cSPoul-Henning Kamp 
1003553629ebSJake Burkholder 	if (all) {
1004d93f860cSPoul-Henning Kamp 		/*
1005d93f860cSPoul-Henning Kamp 		 * broadcast
1006d93f860cSPoul-Henning Kamp 		 */
10071005a129SJohn Baldwin 		sx_slock(&allproc_lock);
10082e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1009628d2653SJohn Baldwin 			PROC_LOCK(p);
10109c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
10119c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1012628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1013628d2653SJohn Baldwin 				continue;
1014628d2653SJohn Baldwin 			}
10159c1ab3e0SJohn Baldwin 			if (p_cansignal(td->td_proc, p, sig) == 0) {
1016d93f860cSPoul-Henning Kamp 				nfound++;
101733a9ed9dSJohn Baldwin 				if (sig)
10182c42a146SMarcel Moolenaar 					psignal(p, sig);
1019628d2653SJohn Baldwin 			}
102033a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1021d93f860cSPoul-Henning Kamp 		}
10221005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1023553629ebSJake Burkholder 	} else {
1024ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1025f591779bSSeigo Tanimura 		if (pgid == 0) {
1026d93f860cSPoul-Henning Kamp 			/*
1027d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1028d93f860cSPoul-Henning Kamp 			 */
10299c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1030f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1031f591779bSSeigo Tanimura 		} else {
1032d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1033f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1034ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1035d93f860cSPoul-Henning Kamp 				return (ESRCH);
1036d93f860cSPoul-Henning Kamp 			}
1037f591779bSSeigo Tanimura 		}
1038ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
10392e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1040628d2653SJohn Baldwin 			PROC_LOCK(p);
1041628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1042628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1043628d2653SJohn Baldwin 				continue;
1044628d2653SJohn Baldwin 			}
1045628d2653SJohn Baldwin 			if (p->p_stat == SZOMB) {
104633a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1047628d2653SJohn Baldwin 				continue;
1048628d2653SJohn Baldwin 			}
10499c1ab3e0SJohn Baldwin 			if (p_cansignal(td->td_proc, p, sig) == 0) {
1050d93f860cSPoul-Henning Kamp 				nfound++;
105133a9ed9dSJohn Baldwin 				if (sig)
10522c42a146SMarcel Moolenaar 					psignal(p, sig);
1053628d2653SJohn Baldwin 			}
105433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1055d93f860cSPoul-Henning Kamp 		}
1056f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1057d93f860cSPoul-Henning Kamp 	}
1058d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1059d93f860cSPoul-Henning Kamp }
1060d93f860cSPoul-Henning Kamp 
1061d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1062df8bae1dSRodney W. Grimes struct kill_args {
1063df8bae1dSRodney W. Grimes 	int	pid;
1064df8bae1dSRodney W. Grimes 	int	signum;
1065df8bae1dSRodney W. Grimes };
1066d2d3e875SBruce Evans #endif
1067fb99ab88SMatthew Dillon /*
1068fb99ab88SMatthew Dillon  * MPSAFE
1069fb99ab88SMatthew Dillon  */
1070df8bae1dSRodney W. Grimes /* ARGSUSED */
107126f9a767SRodney W. Grimes int
1072b40ce416SJulian Elischer kill(td, uap)
1073b40ce416SJulian Elischer 	register struct thread *td;
1074df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1075df8bae1dSRodney W. Grimes {
1076df8bae1dSRodney W. Grimes 	register struct proc *p;
1077fb99ab88SMatthew Dillon 	int error = 0;
1078df8bae1dSRodney W. Grimes 
10796c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1080df8bae1dSRodney W. Grimes 		return (EINVAL);
1081fb99ab88SMatthew Dillon 
1082fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1083df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1084df8bae1dSRodney W. Grimes 		/* kill single process */
1085fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1086fb99ab88SMatthew Dillon 			error = ESRCH;
10879c1ab3e0SJohn Baldwin 		} else if (p_cansignal(td->td_proc, p, uap->signum)) {
108833a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1089fb99ab88SMatthew Dillon 			error = EPERM;
1090fb99ab88SMatthew Dillon 		} else {
109133a9ed9dSJohn Baldwin 			if (uap->signum)
1092df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1093628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1094fb99ab88SMatthew Dillon 			error = 0;
1095df8bae1dSRodney W. Grimes 		}
1096fb99ab88SMatthew Dillon 	} else {
1097df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1098df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
10999c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 1);
1100fb99ab88SMatthew Dillon 			break;
1101df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
11029c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 0);
1103fb99ab88SMatthew Dillon 			break;
1104df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
11059c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, -uap->pid, 0);
1106fb99ab88SMatthew Dillon 			break;
1107df8bae1dSRodney W. Grimes 		}
1108fb99ab88SMatthew Dillon 	}
1109fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1110fb99ab88SMatthew Dillon 	return(error);
1111df8bae1dSRodney W. Grimes }
1112df8bae1dSRodney W. Grimes 
1113df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1114d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1115df8bae1dSRodney W. Grimes struct okillpg_args {
1116df8bae1dSRodney W. Grimes 	int	pgid;
1117df8bae1dSRodney W. Grimes 	int	signum;
1118df8bae1dSRodney W. Grimes };
1119d2d3e875SBruce Evans #endif
1120fb99ab88SMatthew Dillon /*
1121fb99ab88SMatthew Dillon  * MPSAFE
1122fb99ab88SMatthew Dillon  */
1123df8bae1dSRodney W. Grimes /* ARGSUSED */
112426f9a767SRodney W. Grimes int
1125b40ce416SJulian Elischer okillpg(td, uap)
1126b40ce416SJulian Elischer 	struct thread *td;
1127df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1128df8bae1dSRodney W. Grimes {
1129fb99ab88SMatthew Dillon 	int error;
1130df8bae1dSRodney W. Grimes 
11316c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1132df8bae1dSRodney W. Grimes 		return (EINVAL);
1133fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
11349c1ab3e0SJohn Baldwin 	error = killpg1(td, uap->signum, uap->pgid, 0);
1135fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1136fb99ab88SMatthew Dillon 	return (error);
1137df8bae1dSRodney W. Grimes }
1138df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1139df8bae1dSRodney W. Grimes 
1140df8bae1dSRodney W. Grimes /*
1141df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1142df8bae1dSRodney W. Grimes  */
1143df8bae1dSRodney W. Grimes void
11442c42a146SMarcel Moolenaar gsignal(pgid, sig)
11452c42a146SMarcel Moolenaar 	int pgid, sig;
1146df8bae1dSRodney W. Grimes {
1147df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1148df8bae1dSRodney W. Grimes 
1149f591779bSSeigo Tanimura 	if (pgid != 0) {
1150ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1151f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1152ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1153f591779bSSeigo Tanimura 		if (pgrp != NULL) {
11542c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1155f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1156f591779bSSeigo Tanimura 		}
1157f591779bSSeigo Tanimura 	}
1158df8bae1dSRodney W. Grimes }
1159df8bae1dSRodney W. Grimes 
1160df8bae1dSRodney W. Grimes /*
1161df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1162df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1163df8bae1dSRodney W. Grimes  */
1164df8bae1dSRodney W. Grimes void
11652c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1166df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
11672c42a146SMarcel Moolenaar 	int sig, checkctty;
1168df8bae1dSRodney W. Grimes {
1169df8bae1dSRodney W. Grimes 	register struct proc *p;
1170df8bae1dSRodney W. Grimes 
1171628d2653SJohn Baldwin 	if (pgrp) {
1172f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1173628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1174628d2653SJohn Baldwin 			PROC_LOCK(p);
1175df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
11762c42a146SMarcel Moolenaar 				psignal(p, sig);
1177628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1178628d2653SJohn Baldwin 		}
1179628d2653SJohn Baldwin 	}
1180df8bae1dSRodney W. Grimes }
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes /*
1183df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1184df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1185df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1186356861dbSMatthew Dillon  *
1187356861dbSMatthew Dillon  * MPSAFE
1188df8bae1dSRodney W. Grimes  */
1189df8bae1dSRodney W. Grimes void
11902c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1191df8bae1dSRodney W. Grimes 	struct proc *p;
11922c42a146SMarcel Moolenaar 	register int sig;
11938674077aSJeffrey Hsu 	u_long code;
1194df8bae1dSRodney W. Grimes {
1195df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1196df8bae1dSRodney W. Grimes 
1197356861dbSMatthew Dillon 	mtx_lock(&Giant);
1198628d2653SJohn Baldwin 	PROC_LOCK(p);
11992c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1200d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1201df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1202df8bae1dSRodney W. Grimes #ifdef KTRACE
1203df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
12042c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
12052c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1206df8bae1dSRodney W. Grimes #endif
12072c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
12082c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
12092c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
12102c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
12112c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
12122c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1213289ccde0SPeter Wemm 			/*
12142c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1215289ccde0SPeter Wemm 			 */
12162c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
12172c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
12182c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
12192c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
12202c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1221dedc04feSPeter Wemm 		}
12226f841fb7SMarcel Moolenaar 	} else {
12236626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
12242c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
12252c42a146SMarcel Moolenaar 		psignal(p, sig);
1226df8bae1dSRodney W. Grimes 	}
1227628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1228356861dbSMatthew Dillon 	mtx_unlock(&Giant);
1229df8bae1dSRodney W. Grimes }
1230df8bae1dSRodney W. Grimes 
1231df8bae1dSRodney W. Grimes /*
1232df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1233df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1234df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1235df8bae1dSRodney W. Grimes  *
1236df8bae1dSRodney W. Grimes  * Exceptions:
1237df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1238df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1239df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1240df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1241df8bae1dSRodney W. Grimes  *
1242df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1243df8bae1dSRodney W. Grimes  */
1244df8bae1dSRodney W. Grimes void
12452c42a146SMarcel Moolenaar psignal(p, sig)
1246df8bae1dSRodney W. Grimes 	register struct proc *p;
12472c42a146SMarcel Moolenaar 	register int sig;
1248df8bae1dSRodney W. Grimes {
124940447cd4SJohn Baldwin 	register int prop;
1250df8bae1dSRodney W. Grimes 	register sig_t action;
1251b40ce416SJulian Elischer 	struct thread *td;
12520f5c7c4bSPoul-Henning Kamp #ifdef SMP
1253b40ce416SJulian Elischer 	struct ksegrp *kg;
12540f5c7c4bSPoul-Henning Kamp #endif
1255df8bae1dSRodney W. Grimes 
12562899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
12572899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
12582c42a146SMarcel Moolenaar 
1259628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1260cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1261cb679c38SJonathan Lemon 
12622c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1263df8bae1dSRodney W. Grimes 
1264df8bae1dSRodney W. Grimes 	/*
12652a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
12662a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
12672a024a2bSSean Eric Fagan 	 * a chance, as well.
1268df8bae1dSRodney W. Grimes 	 */
1269b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1270df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1271b40ce416SJulian Elischer 	} else {
1272df8bae1dSRodney W. Grimes 		/*
1273df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1274df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1275df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1276df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1277df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1278df8bae1dSRodney W. Grimes 		 */
1279628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1280df8bae1dSRodney W. Grimes 			return;
12812c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1282df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
12832c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1284df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1285df8bae1dSRodney W. Grimes 		else
1286df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1287df8bae1dSRodney W. Grimes 	}
1288df8bae1dSRodney W. Grimes 
1289b40ce416SJulian Elischer 	/*
1290b40ce416SJulian Elischer 	 * bring the priority of a process up if we want it to get
1291b40ce416SJulian Elischer 	 * killed in this lifetime.
1292b40ce416SJulian Elischer 	 * XXXKSE think if a better way to do this.
1293b40ce416SJulian Elischer 	 *
1294b40ce416SJulian Elischer 	 * What we need to do is see if there is a thread that will
1295b40ce416SJulian Elischer 	 * be able to accept the signal. e.g.
1296b40ce416SJulian Elischer 	 * FOREACH_THREAD_IN_PROC() {
1297b40ce416SJulian Elischer 	 *	if runnable, we're done
1298b40ce416SJulian Elischer 	 *	else pick one at random.
1299b40ce416SJulian Elischer 	 * }
1300b40ce416SJulian Elischer 	 */
1301b40ce416SJulian Elischer 	/* XXXKSE
1302b40ce416SJulian Elischer 	 * For now there is one thread per proc.
1303b40ce416SJulian Elischer 	 * Effectively select one sucker thread..
1304b40ce416SJulian Elischer 	 */
1305079b7badSJulian Elischer 	td = FIRST_THREAD_IN_PROC(p);
13069ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1307b40ce416SJulian Elischer 	if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) &&
1308b40ce416SJulian Elischer 	    (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0))
1309b40ce416SJulian Elischer 		p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */
13109ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1311df8bae1dSRodney W. Grimes 
1312df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
13132c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1314df8bae1dSRodney W. Grimes 
1315df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1316df8bae1dSRodney W. Grimes 		/*
1317df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1318df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1319df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1320df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1321df8bae1dSRodney W. Grimes 		 */
1322df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1323628d2653SJohn Baldwin 		    action == SIG_DFL)
1324df8bae1dSRodney W. Grimes 		        return;
13252c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1326df8bae1dSRodney W. Grimes 	}
13272c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
13289c1ab3e0SJohn Baldwin 	mtx_lock_spin(&sched_lock);
132979065dbaSBruce Evans 	signotify(p);
1330df8bae1dSRodney W. Grimes 
1331df8bae1dSRodney W. Grimes 	/*
1332df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1333df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1334df8bae1dSRodney W. Grimes 	 */
13351c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
13369ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1337df8bae1dSRodney W. Grimes 		return;
13381c32c37cSJohn Baldwin 	}
1339b40ce416SJulian Elischer 
1340df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1341df8bae1dSRodney W. Grimes 
1342df8bae1dSRodney W. Grimes 	case SSLEEP:
1343df8bae1dSRodney W. Grimes 		/*
1344df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1345df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1346df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1347df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1348df8bae1dSRodney W. Grimes 		 */
13499c1ab3e0SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1350df8bae1dSRodney W. Grimes 			goto out;
1351df8bae1dSRodney W. Grimes 		/*
1352df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1353df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1354df8bae1dSRodney W. Grimes 		 * for the parent.
1355df8bae1dSRodney W. Grimes 		 */
1356df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1357df8bae1dSRodney W. Grimes 			goto run;
1358df8bae1dSRodney W. Grimes 		/*
1359df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1360df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1361df8bae1dSRodney W. Grimes 		 * be awakened.
1362df8bae1dSRodney W. Grimes 		 */
1363df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
13642c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1365df8bae1dSRodney W. Grimes 			goto out;
1366df8bae1dSRodney W. Grimes 		}
1367df8bae1dSRodney W. Grimes 		/*
1368df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1369df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1370df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1371df8bae1dSRodney W. Grimes 		 * cause the process to run.
1372df8bae1dSRodney W. Grimes 		 */
1373df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
13743e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1375df8bae1dSRodney W. Grimes 				goto runfast;
1376df8bae1dSRodney W. Grimes 			/*
1377df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1378df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1379df8bae1dSRodney W. Grimes 			 */
1380df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1381df8bae1dSRodney W. Grimes 				goto out;
13829c1ab3e0SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
13832c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
13842c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1385628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1386f591779bSSeigo Tanimura 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1387df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1388628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
13895b3047d5SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1390df8bae1dSRodney W. Grimes 			stop(p);
1391df8bae1dSRodney W. Grimes 			goto out;
13923e6831f5SJohn Baldwin 		} else
1393df8bae1dSRodney W. Grimes 			goto runfast;
1394df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1395df8bae1dSRodney W. Grimes 
1396df8bae1dSRodney W. Grimes 	case SSTOP:
1397df8bae1dSRodney W. Grimes 		/*
1398df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1399df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1400df8bae1dSRodney W. Grimes 		 */
140140447cd4SJohn Baldwin 		if (p->p_flag & P_TRACED)
1402df8bae1dSRodney W. Grimes 			goto out;
1403df8bae1dSRodney W. Grimes 
1404df8bae1dSRodney W. Grimes 		/*
1405df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1406df8bae1dSRodney W. Grimes 		 */
14072c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1408df8bae1dSRodney W. Grimes 			goto runfast;
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1411df8bae1dSRodney W. Grimes 			/*
1412df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1413df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1414df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1415df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1416df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1417df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1418df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1419df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1420df8bae1dSRodney W. Grimes 			 */
1421df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
14222c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1423df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1424df8bae1dSRodney W. Grimes 				goto runfast;
1425b40ce416SJulian Elischer 			/*
1426b40ce416SJulian Elischer 			 * XXXKSE
1427b40ce416SJulian Elischer 			 * do this for each thread.
1428b40ce416SJulian Elischer 			 */
1429b40ce416SJulian Elischer 			if (p->p_flag & P_KSES) {
1430b40ce416SJulian Elischer 				mtx_assert(&sched_lock,
1431b40ce416SJulian Elischer 				    MA_OWNED | MA_NOTRECURSED);
1432b40ce416SJulian Elischer 				FOREACH_THREAD_IN_PROC(p, td) {
1433b40ce416SJulian Elischer 					if (td->td_wchan == NULL) {
1434b40ce416SJulian Elischer 						setrunnable(td); /* XXXKSE */
1435b40ce416SJulian Elischer 					} else {
1436b40ce416SJulian Elischer 						/* mark it as sleeping */
1437b40ce416SJulian Elischer 					}
1438b40ce416SJulian Elischer 				}
1439b40ce416SJulian Elischer 			} else {
1440079b7badSJulian Elischer 				if (td->td_wchan == NULL)
1441df8bae1dSRodney W. Grimes 					goto run;
1442df8bae1dSRodney W. Grimes 				p->p_stat = SSLEEP;
1443b40ce416SJulian Elischer 			}
1444fdd4e5c6SJulian Elischer 			goto out;
1445df8bae1dSRodney W. Grimes 		}
1446df8bae1dSRodney W. Grimes 
1447df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1448df8bae1dSRodney W. Grimes 			/*
1449df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1450df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1451df8bae1dSRodney W. Grimes 			 */
14522c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1453df8bae1dSRodney W. Grimes 			goto out;
1454df8bae1dSRodney W. Grimes 		}
1455df8bae1dSRodney W. Grimes 
1456df8bae1dSRodney W. Grimes 		/*
1457df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1458df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1459df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1460df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1461b40ce416SJulian Elischer 		 * XXXKSE should we wake ALL blocked threads?
1462df8bae1dSRodney W. Grimes 		 */
1463b40ce416SJulian Elischer 		if (p->p_flag & P_KSES) {
1464b40ce416SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1465b40ce416SJulian Elischer 				if (td->td_wchan && (td->td_flags & TDF_SINTR)){
1466b40ce416SJulian Elischer 					if (td->td_flags & TDF_CVWAITQ)
1467b40ce416SJulian Elischer 						cv_waitq_remove(td);
1468238510fcSJason Evans 					else
1469b40ce416SJulian Elischer 						unsleep(td); /* XXXKSE */
1470b40ce416SJulian Elischer 				}
1471b40ce416SJulian Elischer 			}
1472b40ce416SJulian Elischer 		} else {
1473b40ce416SJulian Elischer 			if (td->td_wchan && td->td_flags & TDF_SINTR) {
1474b40ce416SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
1475b40ce416SJulian Elischer 					cv_waitq_remove(td);
1476b40ce416SJulian Elischer 				else
1477b40ce416SJulian Elischer 					unsleep(td); /* XXXKSE */
1478b40ce416SJulian Elischer 			}
1479238510fcSJason Evans 		}
1480df8bae1dSRodney W. Grimes 		goto out;
1481df8bae1dSRodney W. Grimes 
1482df8bae1dSRodney W. Grimes 	default:
1483df8bae1dSRodney W. Grimes 		/*
1484df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1485df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1486df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1487df8bae1dSRodney W. Grimes 		 */
1488d2ef4060SBruce Evans 		if (p->p_stat == SRUN) {
14893163861cSTor Egge #ifdef SMP
1490b40ce416SJulian Elischer 			struct kse *ke;
1491b40ce416SJulian Elischer 			struct thread *td = curthread;
1492b40ce416SJulian Elischer /* we should only deliver to one thread.. but which one? */
1493b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(p, kg) {
1494b40ce416SJulian Elischer 				FOREACH_KSE_IN_GROUP(kg, ke) {
1495b40ce416SJulian Elischer 					if (ke->ke_thread == td) {
1496b40ce416SJulian Elischer 						continue;
1497b40ce416SJulian Elischer 					}
1498b40ce416SJulian Elischer 					forward_signal(ke->ke_thread);
1499b40ce416SJulian Elischer 				}
1500b40ce416SJulian Elischer 			}
15013163861cSTor Egge #endif
15026caa8a15SJohn Baldwin 		}
1503df8bae1dSRodney W. Grimes 		goto out;
1504df8bae1dSRodney W. Grimes 	}
1505df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1506df8bae1dSRodney W. Grimes 
1507df8bae1dSRodney W. Grimes runfast:
1508df8bae1dSRodney W. Grimes 	/*
1509df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1510b40ce416SJulian Elischer 	 * XXXKSE Should we make them all run fast?
1511b40ce416SJulian Elischer 	 * Maybe just one would be enough?
1512df8bae1dSRodney W. Grimes 	 */
15132c100766SJulian Elischer 
15142c100766SJulian Elischer 	if (FIRST_THREAD_IN_PROC(p)->td_priority > PUSER) {
15152c100766SJulian Elischer 		FIRST_THREAD_IN_PROC(p)->td_priority = PUSER;
1516b40ce416SJulian Elischer 	}
1517df8bae1dSRodney W. Grimes run:
15183e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
15193e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1520b40ce416SJulian Elischer 	setrunnable(td); /* XXXKSE */
15213e6831f5SJohn Baldwin out:
15229c1ab3e0SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
15239c1ab3e0SJohn Baldwin 
15249c1ab3e0SJohn Baldwin 	/* Once we get here, sched_lock should not be owned. */
15253e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1526df8bae1dSRodney W. Grimes }
1527df8bae1dSRodney W. Grimes 
1528df8bae1dSRodney W. Grimes /*
1529df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1530df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1531df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1532df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1533df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1534df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1535df8bae1dSRodney W. Grimes  * sequence is
1536df8bae1dSRodney W. Grimes  *
15372c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
15382c42a146SMarcel Moolenaar  *		postsig(sig);
1539df8bae1dSRodney W. Grimes  */
154026f9a767SRodney W. Grimes int
1541df8bae1dSRodney W. Grimes issignal(p)
1542df8bae1dSRodney W. Grimes 	register struct proc *p;
1543df8bae1dSRodney W. Grimes {
15442c42a146SMarcel Moolenaar 	sigset_t mask;
15452c42a146SMarcel Moolenaar 	register int sig, prop;
1546df8bae1dSRodney W. Grimes 
1547628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1548df8bae1dSRodney W. Grimes 	for (;;) {
15492a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
15502a024a2bSSean Eric Fagan 
15512c42a146SMarcel Moolenaar 		mask = p->p_siglist;
15522c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1553df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
15542c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
15552c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1556df8bae1dSRodney W. Grimes 			return (0);
15572c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
15582c42a146SMarcel Moolenaar 		prop = sigprop(sig);
15592a024a2bSSean Eric Fagan 
1560628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
15612a024a2bSSean Eric Fagan 
1562df8bae1dSRodney W. Grimes 		/*
1563df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1564df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1565df8bae1dSRodney W. Grimes 		 */
15662c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
15672c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1568df8bae1dSRodney W. Grimes 			continue;
1569df8bae1dSRodney W. Grimes 		}
1570df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1571df8bae1dSRodney W. Grimes 			/*
1572df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1573df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1574df8bae1dSRodney W. Grimes 			 */
15752c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1576628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1577df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1578628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
1579df8bae1dSRodney W. Grimes 			do {
15809ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
15815b3047d5SJohn Baldwin 				stop(p);
1582c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1583c86b6ff5SJohn Baldwin 				DROP_GIANT();
15842ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1585df8bae1dSRodney W. Grimes 				mi_switch();
15869ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
158720cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1588628d2653SJohn Baldwin 				PROC_LOCK(p);
15892a024a2bSSean Eric Fagan 			} while (!trace_req(p)
15902a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1591df8bae1dSRodney W. Grimes 
1592df8bae1dSRodney W. Grimes 			/*
1593df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1594df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1595df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1596df8bae1dSRodney W. Grimes 			 */
1597df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1598df8bae1dSRodney W. Grimes 				continue;
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes 			/*
1601df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1602df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1603df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1604df8bae1dSRodney W. Grimes 			 */
16052c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
16062c42a146SMarcel Moolenaar 			sig = p->p_xstat;
16072c42a146SMarcel Moolenaar 			if (sig == 0)
1608df8bae1dSRodney W. Grimes 				continue;
1609df8bae1dSRodney W. Grimes 
1610df8bae1dSRodney W. Grimes 			/*
1611df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1612df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1613df8bae1dSRodney W. Grimes 			 */
16142c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
16152c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1616df8bae1dSRodney W. Grimes 				continue;
1617df8bae1dSRodney W. Grimes 		}
1618df8bae1dSRodney W. Grimes 
1619df8bae1dSRodney W. Grimes 		/*
1620df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1621df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1622df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1623df8bae1dSRodney W. Grimes 		 */
16242c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1625df8bae1dSRodney W. Grimes 
16260b53fbe8SBruce Evans 		case (int)SIG_DFL:
1627df8bae1dSRodney W. Grimes 			/*
1628df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1629df8bae1dSRodney W. Grimes 			 */
1630df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1631df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1632df8bae1dSRodney W. Grimes 				/*
1633df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1634df8bae1dSRodney W. Grimes 				 * in init? XXX
1635df8bae1dSRodney W. Grimes 				 */
1636d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
16372c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1638df8bae1dSRodney W. Grimes #endif
1639df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1640df8bae1dSRodney W. Grimes 			}
1641df8bae1dSRodney W. Grimes 			/*
1642df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1643df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1644df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1645df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1646df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1647df8bae1dSRodney W. Grimes 			 */
1648df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1649df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1650df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1651df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
1652df8bae1dSRodney W. Grimes 					break;	/* == ignore */
16532c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1654628d2653SJohn Baldwin 				PROC_LOCK(p->p_pptr);
1655f591779bSSeigo Tanimura 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1656df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1657628d2653SJohn Baldwin 				PROC_UNLOCK(p->p_pptr);
16589ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
16595b3047d5SJohn Baldwin 				stop(p);
1660c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1661c86b6ff5SJohn Baldwin 				DROP_GIANT();
16622ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1663df8bae1dSRodney W. Grimes 				mi_switch();
16649ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
166520cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1666628d2653SJohn Baldwin 				PROC_LOCK(p);
1667df8bae1dSRodney W. Grimes 				break;
1668df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1669df8bae1dSRodney W. Grimes 				/*
1670df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1671df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1672df8bae1dSRodney W. Grimes 				 */
1673df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1674df8bae1dSRodney W. Grimes 			} else
16752c42a146SMarcel Moolenaar 				return (sig);
1676df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1677df8bae1dSRodney W. Grimes 
16780b53fbe8SBruce Evans 		case (int)SIG_IGN:
1679df8bae1dSRodney W. Grimes 			/*
1680df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1681df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1682df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1683df8bae1dSRodney W. Grimes 			 */
1684df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1685df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1686df8bae1dSRodney W. Grimes 				printf("issignal\n");
1687df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1688df8bae1dSRodney W. Grimes 
1689df8bae1dSRodney W. Grimes 		default:
1690df8bae1dSRodney W. Grimes 			/*
1691df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1692df8bae1dSRodney W. Grimes 			 * postsig() process it.
1693df8bae1dSRodney W. Grimes 			 */
16942c42a146SMarcel Moolenaar 			return (sig);
1695df8bae1dSRodney W. Grimes 		}
16962c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1697df8bae1dSRodney W. Grimes 	}
1698df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1699df8bae1dSRodney W. Grimes }
1700df8bae1dSRodney W. Grimes 
1701df8bae1dSRodney W. Grimes /*
1702df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1703df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
17045b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
17055b3047d5SJohn Baldwin  * lock held.
1706df8bae1dSRodney W. Grimes  */
17075b3047d5SJohn Baldwin static void
1708df8bae1dSRodney W. Grimes stop(p)
1709df8bae1dSRodney W. Grimes 	register struct proc *p;
1710df8bae1dSRodney W. Grimes {
1711df8bae1dSRodney W. Grimes 
1712628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
17135b3047d5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
1714df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1715df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1716df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1717df8bae1dSRodney W. Grimes }
1718df8bae1dSRodney W. Grimes 
1719df8bae1dSRodney W. Grimes /*
1720df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1721df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1722df8bae1dSRodney W. Grimes  */
1723df8bae1dSRodney W. Grimes void
17242c42a146SMarcel Moolenaar postsig(sig)
17252c42a146SMarcel Moolenaar 	register int sig;
1726df8bae1dSRodney W. Grimes {
1727b40ce416SJulian Elischer 	struct thread *td = curthread;
1728b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1729628d2653SJohn Baldwin 	struct sigacts *ps;
17302c42a146SMarcel Moolenaar 	sig_t action;
17312c42a146SMarcel Moolenaar 	sigset_t returnmask;
17322c42a146SMarcel Moolenaar 	int code;
1733df8bae1dSRodney W. Grimes 
17342c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
17355526d2d9SEivind Eklund 
17362ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1737628d2653SJohn Baldwin 	ps = p->p_sigacts;
17382c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
17392c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1740df8bae1dSRodney W. Grimes #ifdef KTRACE
1741df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1742645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
17432c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1744df8bae1dSRodney W. Grimes #endif
1745628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
17462a024a2bSSean Eric Fagan 
1747df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1748df8bae1dSRodney W. Grimes 		/*
1749df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1750df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1751df8bae1dSRodney W. Grimes 		 */
1752b40ce416SJulian Elischer 		sigexit(td, sig);
1753df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1754df8bae1dSRodney W. Grimes 	} else {
1755df8bae1dSRodney W. Grimes 		/*
1756df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1757df8bae1dSRodney W. Grimes 		 */
17582c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
17595526d2d9SEivind Eklund 		    ("postsig action"));
1760df8bae1dSRodney W. Grimes 		/*
1761df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1762645682fdSLuoqi Chen 		 * occurrences of this signal.
1763df8bae1dSRodney W. Grimes 		 *
1764645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1765df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1766645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1767df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1768df8bae1dSRodney W. Grimes 		 */
1769645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
17706626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1771645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1772df8bae1dSRodney W. Grimes 		} else
1773df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
17742c42a146SMarcel Moolenaar 
17752c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
17762c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
17772c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
17782c42a146SMarcel Moolenaar 
17792c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1780289ccde0SPeter Wemm 			/*
17812c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1782289ccde0SPeter Wemm 			 */
17832c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
17842c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
17852c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
17862c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
17872c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1788dedc04feSPeter Wemm 		}
1789df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
17902c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1791df8bae1dSRodney W. Grimes 			code = 0;
1792df8bae1dSRodney W. Grimes 		} else {
17936626c604SJulian Elischer 			code = p->p_code;
17946626c604SJulian Elischer 			p->p_code = 0;
17956626c604SJulian Elischer 			p->p_sig = 0;
1796df8bae1dSRodney W. Grimes 		}
17972c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1798df8bae1dSRodney W. Grimes 	}
1799df8bae1dSRodney W. Grimes }
1800df8bae1dSRodney W. Grimes 
1801df8bae1dSRodney W. Grimes /*
1802df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1803df8bae1dSRodney W. Grimes  */
180426f9a767SRodney W. Grimes void
1805df8bae1dSRodney W. Grimes killproc(p, why)
1806df8bae1dSRodney W. Grimes 	struct proc *p;
1807df8bae1dSRodney W. Grimes 	char *why;
1808df8bae1dSRodney W. Grimes {
18099081e5e8SJohn Baldwin 
18109081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18110384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
18120384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1813729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1814b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1815df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1816df8bae1dSRodney W. Grimes }
1817df8bae1dSRodney W. Grimes 
1818df8bae1dSRodney W. Grimes /*
1819df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1820df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1821df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1822df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1823df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1824df8bae1dSRodney W. Grimes  * does not return.
1825df8bae1dSRodney W. Grimes  */
182626f9a767SRodney W. Grimes void
1827b40ce416SJulian Elischer sigexit(td, sig)
1828b40ce416SJulian Elischer 	struct thread *td;
18292c42a146SMarcel Moolenaar 	int sig;
1830df8bae1dSRodney W. Grimes {
1831b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1832df8bae1dSRodney W. Grimes 
1833628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1834df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
18352c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
18362c42a146SMarcel Moolenaar 		p->p_sig = sig;
1837c364e17eSAndrey A. Chernov 		/*
1838c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1839c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1840c364e17eSAndrey A. Chernov 		 * these messages.)
1841c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1842c364e17eSAndrey A. Chernov 		 */
1843628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1844c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1845c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1846b40ce416SJulian Elischer 		if (coredump(td) == 0)
18472c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
184857308494SJoerg Wunsch 		if (kern_logsigexit)
184957308494SJoerg Wunsch 			log(LOG_INFO,
185057308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
18513d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
18529c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
18532c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
18542c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1855c31146a1SJohn Baldwin 	} else {
1856628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1857628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1858628d2653SJohn Baldwin 			mtx_lock(&Giant);
1859c31146a1SJohn Baldwin 	}
1860b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1861df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1862df8bae1dSRodney W. Grimes }
1863df8bae1dSRodney W. Grimes 
1864c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1865c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1866c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1867c5edb423SSean Eric Fagan 
1868c5edb423SSean Eric Fagan /*
1869c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1870c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1871c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1872c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1873c5edb423SSean Eric Fagan  *	%P	process id (pid)
1874c5edb423SSean Eric Fagan  *	%U	user id (uid)
1875c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1876c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1877c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1878c5edb423SSean Eric Fagan  */
1879c5edb423SSean Eric Fagan 
1880fca666a1SJulian Elischer static char *
1881c5edb423SSean Eric Fagan expand_name(name, uid, pid)
188287f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1883c5edb423SSean Eric Fagan 	char *temp;
1884c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1885c5edb423SSean Eric Fagan 	int i, n;
1886c5edb423SSean Eric Fagan 	char *format = corefilename;
1887ce38ca0fSAlfred Perlstein 	size_t namelen;
1888c5edb423SSean Eric Fagan 
1889ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
18900bfe2990SEivind Eklund 	if (temp == NULL)
18910bfe2990SEivind Eklund 		return NULL;
1892ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1893ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1894c5edb423SSean Eric Fagan 		int l;
1895c5edb423SSean Eric Fagan 		switch (format[i]) {
1896c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1897c5edb423SSean Eric Fagan 			i++;
1898c5edb423SSean Eric Fagan 			switch (format[i]) {
1899c5edb423SSean Eric Fagan 			case '%':
1900c5edb423SSean Eric Fagan 				temp[n++] = '%';
1901c5edb423SSean Eric Fagan 				break;
1902c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1903ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
190487f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1905c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1906c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1907c5edb423SSean Eric Fagan 					return NULL;
1908c5edb423SSean Eric Fagan 				}
1909ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1910ce38ca0fSAlfred Perlstein 				n += namelen;
1911c5edb423SSean Eric Fagan 				break;
1912c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1913ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1914c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
191587f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1916c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1917c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1918c5edb423SSean Eric Fagan 					return NULL;
1919c5edb423SSean Eric Fagan 				}
1920c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1921c5edb423SSean Eric Fagan 				n += l;
1922c5edb423SSean Eric Fagan 				break;
1923c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1924ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1925c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
192687f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1927c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1928c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1929c5edb423SSean Eric Fagan 					return NULL;
1930c5edb423SSean Eric Fagan 				}
1931c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1932c5edb423SSean Eric Fagan 				n += l;
1933c5edb423SSean Eric Fagan 				break;
1934c5edb423SSean Eric Fagan 			default:
1935c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1936c5edb423SSean Eric Fagan 			}
1937c5edb423SSean Eric Fagan 			break;
1938c5edb423SSean Eric Fagan 		default:
1939c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1940c5edb423SSean Eric Fagan 		}
1941c5edb423SSean Eric Fagan 	}
1942ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1943c5edb423SSean Eric Fagan 	return temp;
1944c5edb423SSean Eric Fagan }
1945c5edb423SSean Eric Fagan 
1946df8bae1dSRodney W. Grimes /*
1947fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1948fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1949fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1950fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1951fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
19525da271f5SRobert Watson  *
19535da271f5SRobert Watson  * XXX: VOP_GETATTR() here requires holding the vnode lock.
1954fca666a1SJulian Elischer  */
1955fca666a1SJulian Elischer 
1956fca666a1SJulian Elischer static int
1957b40ce416SJulian Elischer coredump(struct thread *td)
1958fca666a1SJulian Elischer {
1959b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1960fca666a1SJulian Elischer 	register struct vnode *vp;
19619c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
196206ae1e91SMatthew Dillon 	struct flock lf;
1963fca666a1SJulian Elischer 	struct nameidata nd;
1964fca666a1SJulian Elischer 	struct vattr vattr;
1965e6796b67SKirk McKusick 	int error, error1, flags;
1966f2a2857bSKirk McKusick 	struct mount *mp;
1967fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1968fca666a1SJulian Elischer 	off_t limit;
1969fca666a1SJulian Elischer 
1970628d2653SJohn Baldwin 	PROC_LOCK(p);
1971628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1972fca666a1SJulian Elischer 
1973628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1974628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1975fca666a1SJulian Elischer 		return (EFAULT);
1976628d2653SJohn Baldwin 	}
1977fca666a1SJulian Elischer 
1978fca666a1SJulian Elischer 	/*
197935a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
198035a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
198135a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
198235a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
198335a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
198435a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1985fca666a1SJulian Elischer 	 */
198635a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1987628d2653SJohn Baldwin 	if (limit == 0) {
1988628d2653SJohn Baldwin 		PROC_UNLOCK(p);
198935a2598fSSean Eric Fagan 		return 0;
1990628d2653SJohn Baldwin 	}
1991628d2653SJohn Baldwin 	PROC_UNLOCK(p);
199235a2598fSSean Eric Fagan 
1993f2a2857bSKirk McKusick restart:
19949c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
1995ccdbd10cSPeter Pentchev 	if (name == NULL)
1996ccdbd10cSPeter Pentchev 		return (EINVAL);
1997b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
1998e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1999e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2000fca666a1SJulian Elischer 	free(name, M_TEMP);
2001fca666a1SJulian Elischer 	if (error)
2002fca666a1SJulian Elischer 		return (error);
2003762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2004fca666a1SJulian Elischer 	vp = nd.ni_vp;
200506ae1e91SMatthew Dillon 
2006b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
200706ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
200806ae1e91SMatthew Dillon 	lf.l_start = 0;
200906ae1e91SMatthew Dillon 	lf.l_len = 0;
201006ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
201106ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
201206ae1e91SMatthew Dillon 	if (error)
201306ae1e91SMatthew Dillon 		goto out2;
201406ae1e91SMatthew Dillon 
201506ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
201606ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
201706ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2018b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2019f2a2857bSKirk McKusick 			return (error);
2020f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2021f2a2857bSKirk McKusick 			return (error);
2022f2a2857bSKirk McKusick 		goto restart;
2023f2a2857bSKirk McKusick 	}
2024fca666a1SJulian Elischer 
2025fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
2026fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
2027b40ce416SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2028fca666a1SJulian Elischer 		error = EFAULT;
202906ae1e91SMatthew Dillon 		goto out1;
2030fca666a1SJulian Elischer 	}
2031fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2032fca666a1SJulian Elischer 	vattr.va_size = 0;
203388b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2034b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2035b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
203688b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2037628d2653SJohn Baldwin 	PROC_LOCK(p);
2038fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2039628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2040fca666a1SJulian Elischer 
2041fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2042b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2043fca666a1SJulian Elischer 	  ENOSYS;
2044fca666a1SJulian Elischer 
204506ae1e91SMatthew Dillon out1:
204606ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
204706ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2048f2a2857bSKirk McKusick 	vn_finished_write(mp);
204906ae1e91SMatthew Dillon out2:
2050b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2051fca666a1SJulian Elischer 	if (error == 0)
2052fca666a1SJulian Elischer 		error = error1;
2053fca666a1SJulian Elischer 	return (error);
2054fca666a1SJulian Elischer }
2055fca666a1SJulian Elischer 
2056fca666a1SJulian Elischer /*
2057df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2058df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2059df8bae1dSRodney W. Grimes  */
2060d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2061df8bae1dSRodney W. Grimes struct nosys_args {
2062df8bae1dSRodney W. Grimes 	int	dummy;
2063df8bae1dSRodney W. Grimes };
2064d2d3e875SBruce Evans #endif
2065fb99ab88SMatthew Dillon /*
2066fb99ab88SMatthew Dillon  * MPSAFE
2067fb99ab88SMatthew Dillon  */
2068df8bae1dSRodney W. Grimes /* ARGSUSED */
206926f9a767SRodney W. Grimes int
2070b40ce416SJulian Elischer nosys(td, args)
2071b40ce416SJulian Elischer 	struct thread *td;
2072df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2073df8bae1dSRodney W. Grimes {
2074b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2075b40ce416SJulian Elischer 
2076fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2077628d2653SJohn Baldwin 	PROC_LOCK(p);
2078df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2079628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2080fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2081df8bae1dSRodney W. Grimes 	return (EINVAL);
2082df8bae1dSRodney W. Grimes }
2083831d27a9SDon Lewis 
2084831d27a9SDon Lewis /*
208548f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2086831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2087831d27a9SDon Lewis  */
2088831d27a9SDon Lewis void
2089f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2090f1320723SAlfred Perlstein 	struct sigio **sigiop;
20912c42a146SMarcel Moolenaar 	int sig, checkctty;
2092831d27a9SDon Lewis {
2093f1320723SAlfred Perlstein 	struct sigio *sigio;
2094831d27a9SDon Lewis 
2095f1320723SAlfred Perlstein 	SIGIO_LOCK();
2096f1320723SAlfred Perlstein 	sigio = *sigiop;
2097f1320723SAlfred Perlstein 	if (sigio == NULL) {
2098f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2099f1320723SAlfred Perlstein 		return;
2100f1320723SAlfred Perlstein 	}
2101831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2102628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
21032b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
21042c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2105628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2106831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2107831d27a9SDon Lewis 		struct proc *p;
2108831d27a9SDon Lewis 
2109f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2110628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2111628d2653SJohn Baldwin 			PROC_LOCK(p);
21122b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2113831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
21142c42a146SMarcel Moolenaar 				psignal(p, sig);
2115628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2116628d2653SJohn Baldwin 		}
2117f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2118831d27a9SDon Lewis 	}
2119f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2120831d27a9SDon Lewis }
2121cb679c38SJonathan Lemon 
2122cb679c38SJonathan Lemon static int
2123cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2124cb679c38SJonathan Lemon {
2125cb679c38SJonathan Lemon 	struct proc *p = curproc;
2126cb679c38SJonathan Lemon 
2127cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2128cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2129cb679c38SJonathan Lemon 
2130628d2653SJohn Baldwin 	PROC_LOCK(p);
2131cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2132628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2133cb679c38SJonathan Lemon 
2134cb679c38SJonathan Lemon 	return (0);
2135cb679c38SJonathan Lemon }
2136cb679c38SJonathan Lemon 
2137cb679c38SJonathan Lemon static void
2138cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2139cb679c38SJonathan Lemon {
2140cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2141cb679c38SJonathan Lemon 
2142628d2653SJohn Baldwin 	PROC_LOCK(p);
2143e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2144628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2145cb679c38SJonathan Lemon }
2146cb679c38SJonathan Lemon 
2147cb679c38SJonathan Lemon /*
2148cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2149cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2150cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2151cb679c38SJonathan Lemon  * isn't worth the trouble.
2152cb679c38SJonathan Lemon  */
2153cb679c38SJonathan Lemon static int
2154cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2155cb679c38SJonathan Lemon {
2156cb679c38SJonathan Lemon 
2157cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2158cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2159cb679c38SJonathan Lemon 
2160cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2161cb679c38SJonathan Lemon 			kn->kn_data++;
2162cb679c38SJonathan Lemon 	}
2163cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2164cb679c38SJonathan Lemon }
2165