xref: /freebsd/sys/kern/kern_sig.c (revision ba1551ca813b624dfdaf8d66876adb3e024a3207)
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 	 */
482628d2653SJohn Baldwin 	PROC_LOCK(p);
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;
508628d2653SJohn Baldwin 	PROC_UNLOCK(p);
509df8bae1dSRodney W. Grimes }
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes /*
512628d2653SJohn Baldwin  * do_sigprocmask()
5137c8fdcbdSMatthew Dillon  *
514628d2653SJohn Baldwin  *	Manipulate signal mask.
515df8bae1dSRodney W. Grimes  */
5162c42a146SMarcel Moolenaar static int
517645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
5182c42a146SMarcel Moolenaar 	struct proc *p;
5192c42a146SMarcel Moolenaar 	int how;
5202c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
521645682fdSLuoqi Chen 	int old;
5222c42a146SMarcel Moolenaar {
5232c42a146SMarcel Moolenaar 	int error;
5242c42a146SMarcel Moolenaar 
525628d2653SJohn Baldwin 	PROC_LOCK(p);
5262c42a146SMarcel Moolenaar 	if (oset != NULL)
5272c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
5282c42a146SMarcel Moolenaar 
5292c42a146SMarcel Moolenaar 	error = 0;
5302c42a146SMarcel Moolenaar 	if (set != NULL) {
5312c42a146SMarcel Moolenaar 		switch (how) {
5322c42a146SMarcel Moolenaar 		case SIG_BLOCK:
533645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
5342c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
5352c42a146SMarcel Moolenaar 			break;
5362c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
5372c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
53879065dbaSBruce Evans 			signotify(p);
5392c42a146SMarcel Moolenaar 			break;
5402c42a146SMarcel Moolenaar 		case SIG_SETMASK:
541645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
542645682fdSLuoqi Chen 			if (old)
543645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
544645682fdSLuoqi Chen 			else
5452c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
54679065dbaSBruce Evans 			signotify(p);
5472c42a146SMarcel Moolenaar 			break;
5482c42a146SMarcel Moolenaar 		default:
5492c42a146SMarcel Moolenaar 			error = EINVAL;
5502c42a146SMarcel Moolenaar 			break;
5512c42a146SMarcel Moolenaar 		}
5522c42a146SMarcel Moolenaar 	}
553628d2653SJohn Baldwin 	PROC_UNLOCK(p);
5542c42a146SMarcel Moolenaar 	return (error);
5552c42a146SMarcel Moolenaar }
5562c42a146SMarcel Moolenaar 
5577c8fdcbdSMatthew Dillon /*
558b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
5597c8fdcbdSMatthew Dillon  */
5607c8fdcbdSMatthew Dillon 
561d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
562df8bae1dSRodney W. Grimes struct sigprocmask_args {
563df8bae1dSRodney W. Grimes 	int	how;
5642c42a146SMarcel Moolenaar 	const sigset_t *set;
5652c42a146SMarcel Moolenaar 	sigset_t *oset;
566df8bae1dSRodney W. Grimes };
567d2d3e875SBruce Evans #endif
56826f9a767SRodney W. Grimes int
569b40ce416SJulian Elischer sigprocmask(td, uap)
570b40ce416SJulian Elischer 	register struct thread *td;
571df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
572df8bae1dSRodney W. Grimes {
573b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
5742c42a146SMarcel Moolenaar 	sigset_t set, oset;
5752c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5762c42a146SMarcel Moolenaar 	int error;
577df8bae1dSRodney W. Grimes 
5786f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5796f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5802c42a146SMarcel Moolenaar 	if (setp) {
5816f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5822c42a146SMarcel Moolenaar 		if (error)
5832c42a146SMarcel Moolenaar 			return (error);
584df8bae1dSRodney W. Grimes 	}
585645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5862c42a146SMarcel Moolenaar 	if (osetp && !error) {
5876f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5882c42a146SMarcel Moolenaar 	}
5892c42a146SMarcel Moolenaar 	return (error);
5902c42a146SMarcel Moolenaar }
5912c42a146SMarcel Moolenaar 
59231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5937c8fdcbdSMatthew Dillon /*
5947c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5957c8fdcbdSMatthew Dillon  */
5962c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5972c42a146SMarcel Moolenaar struct osigprocmask_args {
5982c42a146SMarcel Moolenaar 	int	how;
5992c42a146SMarcel Moolenaar 	osigset_t mask;
6002c42a146SMarcel Moolenaar };
6012c42a146SMarcel Moolenaar #endif
6022c42a146SMarcel Moolenaar int
603b40ce416SJulian Elischer osigprocmask(td, uap)
604b40ce416SJulian Elischer 	register struct thread *td;
6052c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
6062c42a146SMarcel Moolenaar {
607b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
6082c42a146SMarcel Moolenaar 	sigset_t set, oset;
6092c42a146SMarcel Moolenaar 	int error;
6102c42a146SMarcel Moolenaar 
6112c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
612645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
613b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
614df8bae1dSRodney W. Grimes 	return (error);
615df8bae1dSRodney W. Grimes }
61631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
617df8bae1dSRodney W. Grimes 
618d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
619df8bae1dSRodney W. Grimes struct sigpending_args {
6202c42a146SMarcel Moolenaar 	sigset_t	*set;
621df8bae1dSRodney W. Grimes };
622d2d3e875SBruce Evans #endif
623fb99ab88SMatthew Dillon /*
624fb99ab88SMatthew Dillon  * MPSAFE
625fb99ab88SMatthew Dillon  */
626df8bae1dSRodney W. Grimes /* ARGSUSED */
62726f9a767SRodney W. Grimes int
628b40ce416SJulian Elischer sigpending(td, uap)
629b40ce416SJulian Elischer 	struct thread *td;
630df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
631df8bae1dSRodney W. Grimes {
632b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
633628d2653SJohn Baldwin 	sigset_t siglist;
634fb99ab88SMatthew Dillon 	int error;
635df8bae1dSRodney W. Grimes 
636fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
637628d2653SJohn Baldwin 	PROC_LOCK(p);
638628d2653SJohn Baldwin 	siglist = p->p_siglist;
639628d2653SJohn Baldwin 	PROC_UNLOCK(p);
640fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
641fb99ab88SMatthew Dillon 	error = copyout(&siglist, uap->set, sizeof(sigset_t));
642fb99ab88SMatthew Dillon 	return(error);
6432c42a146SMarcel Moolenaar }
6442c42a146SMarcel Moolenaar 
64531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6462c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6472c42a146SMarcel Moolenaar struct osigpending_args {
6482c42a146SMarcel Moolenaar 	int	dummy;
6492c42a146SMarcel Moolenaar };
6502c42a146SMarcel Moolenaar #endif
651fb99ab88SMatthew Dillon /*
652fb99ab88SMatthew Dillon  * MPSAFE
653fb99ab88SMatthew Dillon  */
6542c42a146SMarcel Moolenaar /* ARGSUSED */
6552c42a146SMarcel Moolenaar int
656b40ce416SJulian Elischer osigpending(td, uap)
657b40ce416SJulian Elischer 	struct thread *td;
6582c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6592c42a146SMarcel Moolenaar {
660b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
661b40ce416SJulian Elischer 
662fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
663628d2653SJohn Baldwin 	PROC_LOCK(p);
664b40ce416SJulian Elischer 	SIG2OSIG(p->p_siglist, td->td_retval[0]);
665628d2653SJohn Baldwin 	PROC_UNLOCK(p);
666fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
667df8bae1dSRodney W. Grimes 	return (0);
668df8bae1dSRodney W. Grimes }
66931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
672df8bae1dSRodney W. Grimes /*
673df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
674df8bae1dSRodney W. Grimes  */
675d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
676df8bae1dSRodney W. Grimes struct osigvec_args {
677df8bae1dSRodney W. Grimes 	int	signum;
678df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
679df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
680df8bae1dSRodney W. Grimes };
681d2d3e875SBruce Evans #endif
682fb99ab88SMatthew Dillon /*
683fb99ab88SMatthew Dillon  * MPSAFE
684fb99ab88SMatthew Dillon  */
685df8bae1dSRodney W. Grimes /* ARGSUSED */
68626f9a767SRodney W. Grimes int
687b40ce416SJulian Elischer osigvec(td, uap)
688b40ce416SJulian Elischer 	struct thread *td;
689df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
690df8bae1dSRodney W. Grimes {
691b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
692df8bae1dSRodney W. Grimes 	struct sigvec vec;
6932c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6942c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6952c42a146SMarcel Moolenaar 	int error;
696df8bae1dSRodney W. Grimes 
6976f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6986f841fb7SMarcel Moolenaar 		return (EINVAL);
6996f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
7006f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
7012c42a146SMarcel Moolenaar 	if (nsap) {
7026f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
7032c42a146SMarcel Moolenaar 		if (error)
704df8bae1dSRodney W. Grimes 			return (error);
7052c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
7062c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
7072c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
7082c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
709df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
7102c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
711df8bae1dSRodney W. Grimes #endif
712df8bae1dSRodney W. Grimes 	}
713fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
714645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
715fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
7162c42a146SMarcel Moolenaar 	if (osap && !error) {
7172c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7182c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7192c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7202c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7212c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7222c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7232c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7242c42a146SMarcel Moolenaar #endif
7256f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
7262c42a146SMarcel Moolenaar 	}
7272c42a146SMarcel Moolenaar 	return (error);
728df8bae1dSRodney W. Grimes }
729df8bae1dSRodney W. Grimes 
730d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
731df8bae1dSRodney W. Grimes struct osigblock_args {
732df8bae1dSRodney W. Grimes 	int	mask;
733df8bae1dSRodney W. Grimes };
734d2d3e875SBruce Evans #endif
735fb99ab88SMatthew Dillon /*
736fb99ab88SMatthew Dillon  * MPSAFE
737fb99ab88SMatthew Dillon  */
73826f9a767SRodney W. Grimes int
739b40ce416SJulian Elischer osigblock(td, uap)
740b40ce416SJulian Elischer 	register struct thread *td;
741df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
742df8bae1dSRodney W. Grimes {
743b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7442c42a146SMarcel Moolenaar 	sigset_t set;
745df8bae1dSRodney W. Grimes 
7462c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7472c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
748fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
749628d2653SJohn Baldwin 	PROC_LOCK(p);
750b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
7512c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
752628d2653SJohn Baldwin 	PROC_UNLOCK(p);
753fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
754df8bae1dSRodney W. Grimes 	return (0);
755df8bae1dSRodney W. Grimes }
756df8bae1dSRodney W. Grimes 
757d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
758df8bae1dSRodney W. Grimes struct osigsetmask_args {
759df8bae1dSRodney W. Grimes 	int	mask;
760df8bae1dSRodney W. Grimes };
761d2d3e875SBruce Evans #endif
762fb99ab88SMatthew Dillon /*
763fb99ab88SMatthew Dillon  * MPSAFE
764fb99ab88SMatthew Dillon  */
76526f9a767SRodney W. Grimes int
766b40ce416SJulian Elischer osigsetmask(td, uap)
767b40ce416SJulian Elischer 	struct thread *td;
768df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
769df8bae1dSRodney W. Grimes {
770b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7712c42a146SMarcel Moolenaar 	sigset_t set;
772df8bae1dSRodney W. Grimes 
7732c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7742c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
775fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
776628d2653SJohn Baldwin 	PROC_LOCK(p);
777b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
778645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
77979065dbaSBruce Evans 	signotify(p);
780628d2653SJohn Baldwin 	PROC_UNLOCK(p);
781fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
782df8bae1dSRodney W. Grimes 	return (0);
783df8bae1dSRodney W. Grimes }
784df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes /*
787df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
788df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
789df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
790b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
791b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
792b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
793df8bae1dSRodney W. Grimes  */
794d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
795df8bae1dSRodney W. Grimes struct sigsuspend_args {
7962c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
797df8bae1dSRodney W. Grimes };
798d2d3e875SBruce Evans #endif
799fb99ab88SMatthew Dillon /*
800fb99ab88SMatthew Dillon  * MPSAFE
801fb99ab88SMatthew Dillon  */
802df8bae1dSRodney W. Grimes /* ARGSUSED */
80326f9a767SRodney W. Grimes int
804b40ce416SJulian Elischer sigsuspend(td, uap)
805b40ce416SJulian Elischer 	struct thread *td;
806df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
807df8bae1dSRodney W. Grimes {
808b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
8092c42a146SMarcel Moolenaar 	sigset_t mask;
810628d2653SJohn Baldwin 	register struct sigacts *ps;
8112c42a146SMarcel Moolenaar 	int error;
8122c42a146SMarcel Moolenaar 
8136f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
8142c42a146SMarcel Moolenaar 	if (error)
8152c42a146SMarcel Moolenaar 		return (error);
816df8bae1dSRodney W. Grimes 
817df8bae1dSRodney W. Grimes 	/*
818645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
819df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
820df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
821df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
822df8bae1dSRodney W. Grimes 	 * to indicate this.
823df8bae1dSRodney W. Grimes 	 */
824fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
825628d2653SJohn Baldwin 	PROC_LOCK(p);
826628d2653SJohn Baldwin 	ps = p->p_sigacts;
8276626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
828645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
829645682fdSLuoqi Chen 
830645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
8312c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
83279065dbaSBruce Evans 	signotify(p);
833628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
8342c42a146SMarcel Moolenaar 		/* void */;
835628d2653SJohn Baldwin 	PROC_UNLOCK(p);
836fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
8372c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
8382c42a146SMarcel Moolenaar 	return (EINTR);
8392c42a146SMarcel Moolenaar }
8402c42a146SMarcel Moolenaar 
84131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8422c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8432c42a146SMarcel Moolenaar struct osigsuspend_args {
8442c42a146SMarcel Moolenaar 	osigset_t mask;
8452c42a146SMarcel Moolenaar };
8462c42a146SMarcel Moolenaar #endif
847fb99ab88SMatthew Dillon /*
848fb99ab88SMatthew Dillon  * MPSAFE
849fb99ab88SMatthew Dillon  */
8502c42a146SMarcel Moolenaar /* ARGSUSED */
8512c42a146SMarcel Moolenaar int
852b40ce416SJulian Elischer osigsuspend(td, uap)
853b40ce416SJulian Elischer 	struct thread *td;
8542c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
8552c42a146SMarcel Moolenaar {
856b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
857645682fdSLuoqi Chen 	sigset_t mask;
858628d2653SJohn Baldwin 	register struct sigacts *ps;
8592c42a146SMarcel Moolenaar 
860fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
861628d2653SJohn Baldwin 	PROC_LOCK(p);
862628d2653SJohn Baldwin 	ps = p->p_sigacts;
8632c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
864645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
865645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
866645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
867645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
86879065dbaSBruce Evans 	signotify(p);
869628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
870df8bae1dSRodney W. Grimes 		/* void */;
871628d2653SJohn Baldwin 	PROC_UNLOCK(p);
872fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
873df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
874df8bae1dSRodney W. Grimes 	return (EINTR);
875df8bae1dSRodney W. Grimes }
87631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
877df8bae1dSRodney W. Grimes 
878df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
879d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
880df8bae1dSRodney W. Grimes struct osigstack_args {
881df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
882df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
883df8bae1dSRodney W. Grimes };
884d2d3e875SBruce Evans #endif
885fb99ab88SMatthew Dillon /*
886fb99ab88SMatthew Dillon  * MPSAFE
887fb99ab88SMatthew Dillon  */
888df8bae1dSRodney W. Grimes /* ARGSUSED */
88926f9a767SRodney W. Grimes int
890b40ce416SJulian Elischer osigstack(td, uap)
891b40ce416SJulian Elischer 	struct thread *td;
892df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
893df8bae1dSRodney W. Grimes {
894b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
895df8bae1dSRodney W. Grimes 	struct sigstack ss;
896fb99ab88SMatthew Dillon 	int error = 0;
897fb99ab88SMatthew Dillon 
898fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
899df8bae1dSRodney W. Grimes 
900d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
901628d2653SJohn Baldwin 		PROC_LOCK(p);
902645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
903b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
904628d2653SJohn Baldwin 		PROC_UNLOCK(p);
905d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
906d034d459SMarcel Moolenaar 		if (error)
907fb99ab88SMatthew Dillon 			goto done2;
908d034d459SMarcel Moolenaar 	}
909d034d459SMarcel Moolenaar 
910d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
911d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
912fb99ab88SMatthew Dillon 			goto done2;
913628d2653SJohn Baldwin 		PROC_LOCK(p);
914645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
915645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
916645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
917645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
918628d2653SJohn Baldwin 		PROC_UNLOCK(p);
919df8bae1dSRodney W. Grimes 	}
920fb99ab88SMatthew Dillon done2:
921fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
922fb99ab88SMatthew Dillon 	return (error);
923df8bae1dSRodney W. Grimes }
924df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
925df8bae1dSRodney W. Grimes 
926d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
927df8bae1dSRodney W. Grimes struct sigaltstack_args {
9282c42a146SMarcel Moolenaar 	stack_t	*ss;
9292c42a146SMarcel Moolenaar 	stack_t	*oss;
930df8bae1dSRodney W. Grimes };
931d2d3e875SBruce Evans #endif
932fb99ab88SMatthew Dillon /*
933fb99ab88SMatthew Dillon  * MPSAFE
934fb99ab88SMatthew Dillon  */
935df8bae1dSRodney W. Grimes /* ARGSUSED */
93626f9a767SRodney W. Grimes int
937b40ce416SJulian Elischer sigaltstack(td, uap)
938b40ce416SJulian Elischer 	struct thread *td;
939df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
940df8bae1dSRodney W. Grimes {
941b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9422c42a146SMarcel Moolenaar 	stack_t ss;
943fb99ab88SMatthew Dillon 	int oonstack;
944fb99ab88SMatthew Dillon 	int error = 0;
945fb99ab88SMatthew Dillon 
946fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
947df8bae1dSRodney W. Grimes 
948b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
949d034d459SMarcel Moolenaar 
950d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
951628d2653SJohn Baldwin 		PROC_LOCK(p);
952d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
953d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
954d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
955628d2653SJohn Baldwin 		PROC_UNLOCK(p);
956d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
957fb99ab88SMatthew Dillon 			goto done2;
958df8bae1dSRodney W. Grimes 	}
959d034d459SMarcel Moolenaar 
960d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
961fb99ab88SMatthew Dillon 		if (oonstack) {
962fb99ab88SMatthew Dillon 			error = EPERM;
963fb99ab88SMatthew Dillon 			goto done2;
964fb99ab88SMatthew Dillon 		}
965d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
966fb99ab88SMatthew Dillon 			goto done2;
967fb99ab88SMatthew Dillon 		if ((ss.ss_flags & ~SS_DISABLE) != 0) {
968fb99ab88SMatthew Dillon 			error = EINVAL;
969fb99ab88SMatthew Dillon 			goto done2;
970fb99ab88SMatthew Dillon 		}
971d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
972fb99ab88SMatthew Dillon 			if (ss.ss_size < p->p_sysent->sv_minsigstksz) {
973fb99ab88SMatthew Dillon 				error = ENOMEM;
974fb99ab88SMatthew Dillon 				goto done2;
975fb99ab88SMatthew Dillon 			}
976628d2653SJohn Baldwin 			PROC_LOCK(p);
977645682fdSLuoqi Chen 			p->p_sigstk = ss;
978d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
979628d2653SJohn Baldwin 			PROC_UNLOCK(p);
980628d2653SJohn Baldwin 		} else {
981628d2653SJohn Baldwin 			PROC_LOCK(p);
982d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
983628d2653SJohn Baldwin 			PROC_UNLOCK(p);
984628d2653SJohn Baldwin 		}
985d034d459SMarcel Moolenaar 	}
986fb99ab88SMatthew Dillon done2:
987fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
988fb99ab88SMatthew Dillon 	return (error);
989df8bae1dSRodney W. Grimes }
990df8bae1dSRodney W. Grimes 
991d93f860cSPoul-Henning Kamp /*
992d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
993d93f860cSPoul-Henning Kamp  * cp is calling process.
994d93f860cSPoul-Henning Kamp  */
995d93f860cSPoul-Henning Kamp int
9969c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
9979c1ab3e0SJohn Baldwin 	register struct thread *td;
9982c42a146SMarcel Moolenaar 	int sig, pgid, all;
999d93f860cSPoul-Henning Kamp {
1000d93f860cSPoul-Henning Kamp 	register struct proc *p;
1001d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1002d93f860cSPoul-Henning Kamp 	int nfound = 0;
1003d93f860cSPoul-Henning Kamp 
1004553629ebSJake Burkholder 	if (all) {
1005d93f860cSPoul-Henning Kamp 		/*
1006d93f860cSPoul-Henning Kamp 		 * broadcast
1007d93f860cSPoul-Henning Kamp 		 */
10081005a129SJohn Baldwin 		sx_slock(&allproc_lock);
10092e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1010628d2653SJohn Baldwin 			PROC_LOCK(p);
10119c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
10129c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1013628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1014628d2653SJohn Baldwin 				continue;
1015628d2653SJohn Baldwin 			}
10169c1ab3e0SJohn Baldwin 			if (p_cansignal(td->td_proc, p, sig) == 0) {
1017d93f860cSPoul-Henning Kamp 				nfound++;
101833a9ed9dSJohn Baldwin 				if (sig)
10192c42a146SMarcel Moolenaar 					psignal(p, sig);
1020628d2653SJohn Baldwin 			}
102133a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1022d93f860cSPoul-Henning Kamp 		}
10231005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1024553629ebSJake Burkholder 	} else {
1025ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1026f591779bSSeigo Tanimura 		if (pgid == 0) {
1027d93f860cSPoul-Henning Kamp 			/*
1028d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1029d93f860cSPoul-Henning Kamp 			 */
10309c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1031f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1032f591779bSSeigo Tanimura 		} else {
1033d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1034f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1035ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1036d93f860cSPoul-Henning Kamp 				return (ESRCH);
1037d93f860cSPoul-Henning Kamp 			}
1038f591779bSSeigo Tanimura 		}
1039ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
10402e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1041628d2653SJohn Baldwin 			PROC_LOCK(p);
1042628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1043628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1044628d2653SJohn Baldwin 				continue;
1045628d2653SJohn Baldwin 			}
1046628d2653SJohn Baldwin 			if (p->p_stat == SZOMB) {
104733a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1048628d2653SJohn Baldwin 				continue;
1049628d2653SJohn Baldwin 			}
10509c1ab3e0SJohn Baldwin 			if (p_cansignal(td->td_proc, p, sig) == 0) {
1051d93f860cSPoul-Henning Kamp 				nfound++;
105233a9ed9dSJohn Baldwin 				if (sig)
10532c42a146SMarcel Moolenaar 					psignal(p, sig);
1054628d2653SJohn Baldwin 			}
105533a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1056d93f860cSPoul-Henning Kamp 		}
1057f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1058d93f860cSPoul-Henning Kamp 	}
1059d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1060d93f860cSPoul-Henning Kamp }
1061d93f860cSPoul-Henning Kamp 
1062d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1063df8bae1dSRodney W. Grimes struct kill_args {
1064df8bae1dSRodney W. Grimes 	int	pid;
1065df8bae1dSRodney W. Grimes 	int	signum;
1066df8bae1dSRodney W. Grimes };
1067d2d3e875SBruce Evans #endif
1068fb99ab88SMatthew Dillon /*
1069fb99ab88SMatthew Dillon  * MPSAFE
1070fb99ab88SMatthew Dillon  */
1071df8bae1dSRodney W. Grimes /* ARGSUSED */
107226f9a767SRodney W. Grimes int
1073b40ce416SJulian Elischer kill(td, uap)
1074b40ce416SJulian Elischer 	register struct thread *td;
1075df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1076df8bae1dSRodney W. Grimes {
1077df8bae1dSRodney W. Grimes 	register struct proc *p;
1078fb99ab88SMatthew Dillon 	int error = 0;
1079df8bae1dSRodney W. Grimes 
10806c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1081df8bae1dSRodney W. Grimes 		return (EINVAL);
1082fb99ab88SMatthew Dillon 
1083fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1084df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1085df8bae1dSRodney W. Grimes 		/* kill single process */
1086fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1087fb99ab88SMatthew Dillon 			error = ESRCH;
10889c1ab3e0SJohn Baldwin 		} else if (p_cansignal(td->td_proc, p, uap->signum)) {
108933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1090fb99ab88SMatthew Dillon 			error = EPERM;
1091fb99ab88SMatthew Dillon 		} else {
109233a9ed9dSJohn Baldwin 			if (uap->signum)
1093df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1094628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1095fb99ab88SMatthew Dillon 			error = 0;
1096df8bae1dSRodney W. Grimes 		}
1097fb99ab88SMatthew Dillon 	} else {
1098df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1099df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
11009c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 1);
1101fb99ab88SMatthew Dillon 			break;
1102df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
11039c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 0);
1104fb99ab88SMatthew Dillon 			break;
1105df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
11069c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, -uap->pid, 0);
1107fb99ab88SMatthew Dillon 			break;
1108df8bae1dSRodney W. Grimes 		}
1109fb99ab88SMatthew Dillon 	}
1110fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1111fb99ab88SMatthew Dillon 	return(error);
1112df8bae1dSRodney W. Grimes }
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1115d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1116df8bae1dSRodney W. Grimes struct okillpg_args {
1117df8bae1dSRodney W. Grimes 	int	pgid;
1118df8bae1dSRodney W. Grimes 	int	signum;
1119df8bae1dSRodney W. Grimes };
1120d2d3e875SBruce Evans #endif
1121fb99ab88SMatthew Dillon /*
1122fb99ab88SMatthew Dillon  * MPSAFE
1123fb99ab88SMatthew Dillon  */
1124df8bae1dSRodney W. Grimes /* ARGSUSED */
112526f9a767SRodney W. Grimes int
1126b40ce416SJulian Elischer okillpg(td, uap)
1127b40ce416SJulian Elischer 	struct thread *td;
1128df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1129df8bae1dSRodney W. Grimes {
1130fb99ab88SMatthew Dillon 	int error;
1131df8bae1dSRodney W. Grimes 
11326c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1133df8bae1dSRodney W. Grimes 		return (EINVAL);
1134fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
11359c1ab3e0SJohn Baldwin 	error = killpg1(td, uap->signum, uap->pgid, 0);
1136fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1137fb99ab88SMatthew Dillon 	return (error);
1138df8bae1dSRodney W. Grimes }
1139df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes /*
1142df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1143df8bae1dSRodney W. Grimes  */
1144df8bae1dSRodney W. Grimes void
11452c42a146SMarcel Moolenaar gsignal(pgid, sig)
11462c42a146SMarcel Moolenaar 	int pgid, sig;
1147df8bae1dSRodney W. Grimes {
1148df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1149df8bae1dSRodney W. Grimes 
1150f591779bSSeigo Tanimura 	if (pgid != 0) {
1151ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1152f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1153ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1154f591779bSSeigo Tanimura 		if (pgrp != NULL) {
11552c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1156f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1157f591779bSSeigo Tanimura 		}
1158f591779bSSeigo Tanimura 	}
1159df8bae1dSRodney W. Grimes }
1160df8bae1dSRodney W. Grimes 
1161df8bae1dSRodney W. Grimes /*
1162df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1163df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1164df8bae1dSRodney W. Grimes  */
1165df8bae1dSRodney W. Grimes void
11662c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1167df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
11682c42a146SMarcel Moolenaar 	int sig, checkctty;
1169df8bae1dSRodney W. Grimes {
1170df8bae1dSRodney W. Grimes 	register struct proc *p;
1171df8bae1dSRodney W. Grimes 
1172628d2653SJohn Baldwin 	if (pgrp) {
1173f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1174628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1175628d2653SJohn Baldwin 			PROC_LOCK(p);
1176df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
11772c42a146SMarcel Moolenaar 				psignal(p, sig);
1178628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1179628d2653SJohn Baldwin 		}
1180628d2653SJohn Baldwin 	}
1181df8bae1dSRodney W. Grimes }
1182df8bae1dSRodney W. Grimes 
1183df8bae1dSRodney W. Grimes /*
1184df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1185df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1186df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1187356861dbSMatthew Dillon  *
1188356861dbSMatthew Dillon  * MPSAFE
1189df8bae1dSRodney W. Grimes  */
1190df8bae1dSRodney W. Grimes void
11912c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1192df8bae1dSRodney W. Grimes 	struct proc *p;
11932c42a146SMarcel Moolenaar 	register int sig;
11948674077aSJeffrey Hsu 	u_long code;
1195df8bae1dSRodney W. Grimes {
1196df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1197df8bae1dSRodney W. Grimes 
1198356861dbSMatthew Dillon 	mtx_lock(&Giant);
1199628d2653SJohn Baldwin 	PROC_LOCK(p);
12002c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1201d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1202df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1203df8bae1dSRodney W. Grimes #ifdef KTRACE
1204df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
12052c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
12062c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1207df8bae1dSRodney W. Grimes #endif
12082c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
12092c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
12102c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
12112c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
12122c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
12132c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1214289ccde0SPeter Wemm 			/*
12152c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1216289ccde0SPeter Wemm 			 */
12172c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
12182c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
12192c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
12202c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
12212c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1222dedc04feSPeter Wemm 		}
12236f841fb7SMarcel Moolenaar 	} else {
12246626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
12252c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
12262c42a146SMarcel Moolenaar 		psignal(p, sig);
1227df8bae1dSRodney W. Grimes 	}
1228628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1229356861dbSMatthew Dillon 	mtx_unlock(&Giant);
1230df8bae1dSRodney W. Grimes }
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes /*
1233df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1234df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1235df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1236df8bae1dSRodney W. Grimes  *
1237df8bae1dSRodney W. Grimes  * Exceptions:
1238df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1239df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1240df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1241df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1242df8bae1dSRodney W. Grimes  *
1243df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1244df8bae1dSRodney W. Grimes  */
1245df8bae1dSRodney W. Grimes void
12462c42a146SMarcel Moolenaar psignal(p, sig)
1247df8bae1dSRodney W. Grimes 	register struct proc *p;
12482c42a146SMarcel Moolenaar 	register int sig;
1249df8bae1dSRodney W. Grimes {
125040447cd4SJohn Baldwin 	register int prop;
1251df8bae1dSRodney W. Grimes 	register sig_t action;
1252b40ce416SJulian Elischer 	struct thread *td;
12530f5c7c4bSPoul-Henning Kamp #ifdef SMP
1254b40ce416SJulian Elischer 	struct ksegrp *kg;
12550f5c7c4bSPoul-Henning Kamp #endif
1256df8bae1dSRodney W. Grimes 
12572899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
12582899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
12592c42a146SMarcel Moolenaar 
1260628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1261cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1262cb679c38SJonathan Lemon 
12632c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1264df8bae1dSRodney W. Grimes 
1265df8bae1dSRodney W. Grimes 	/*
12662a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
12672a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
12682a024a2bSSean Eric Fagan 	 * a chance, as well.
1269df8bae1dSRodney W. Grimes 	 */
1270b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1271df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1272b40ce416SJulian Elischer 	} else {
1273df8bae1dSRodney W. Grimes 		/*
1274df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1275df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1276df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1277df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1278df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1279df8bae1dSRodney W. Grimes 		 */
1280628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1281df8bae1dSRodney W. Grimes 			return;
12822c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1283df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
12842c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1285df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1286df8bae1dSRodney W. Grimes 		else
1287df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1288df8bae1dSRodney W. Grimes 	}
1289df8bae1dSRodney W. Grimes 
1290b40ce416SJulian Elischer 	/*
1291b40ce416SJulian Elischer 	 * bring the priority of a process up if we want it to get
1292b40ce416SJulian Elischer 	 * killed in this lifetime.
1293b40ce416SJulian Elischer 	 * XXXKSE think if a better way to do this.
1294b40ce416SJulian Elischer 	 *
1295b40ce416SJulian Elischer 	 * What we need to do is see if there is a thread that will
1296b40ce416SJulian Elischer 	 * be able to accept the signal. e.g.
1297b40ce416SJulian Elischer 	 * FOREACH_THREAD_IN_PROC() {
1298b40ce416SJulian Elischer 	 *	if runnable, we're done
1299b40ce416SJulian Elischer 	 *	else pick one at random.
1300b40ce416SJulian Elischer 	 * }
1301b40ce416SJulian Elischer 	 */
1302b40ce416SJulian Elischer 	/* XXXKSE
1303b40ce416SJulian Elischer 	 * For now there is one thread per proc.
1304b40ce416SJulian Elischer 	 * Effectively select one sucker thread..
1305b40ce416SJulian Elischer 	 */
1306079b7badSJulian Elischer 	td = FIRST_THREAD_IN_PROC(p);
13079ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1308b40ce416SJulian Elischer 	if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) &&
1309b40ce416SJulian Elischer 	    (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0))
1310b40ce416SJulian Elischer 		p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */
13119ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1312df8bae1dSRodney W. Grimes 
1313df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
13142c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1315df8bae1dSRodney W. Grimes 
1316df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1317df8bae1dSRodney W. Grimes 		/*
1318df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1319df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1320df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1321df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1322df8bae1dSRodney W. Grimes 		 */
1323df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1324628d2653SJohn Baldwin 		    action == SIG_DFL)
1325df8bae1dSRodney W. Grimes 		        return;
13262c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1327df8bae1dSRodney W. Grimes 	}
13282c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
13299c1ab3e0SJohn Baldwin 	mtx_lock_spin(&sched_lock);
133079065dbaSBruce Evans 	signotify(p);
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 	/*
1333df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1334df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1335df8bae1dSRodney W. Grimes 	 */
13361c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
13379ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1338df8bae1dSRodney W. Grimes 		return;
13391c32c37cSJohn Baldwin 	}
1340b40ce416SJulian Elischer 
1341df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1342df8bae1dSRodney W. Grimes 
1343df8bae1dSRodney W. Grimes 	case SSLEEP:
1344df8bae1dSRodney W. Grimes 		/*
1345df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1346df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1347df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1348df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1349df8bae1dSRodney W. Grimes 		 */
13509c1ab3e0SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1351df8bae1dSRodney W. Grimes 			goto out;
1352df8bae1dSRodney W. Grimes 		/*
1353df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1354df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1355df8bae1dSRodney W. Grimes 		 * for the parent.
1356df8bae1dSRodney W. Grimes 		 */
1357df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1358df8bae1dSRodney W. Grimes 			goto run;
1359df8bae1dSRodney W. Grimes 		/*
1360df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1361df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1362df8bae1dSRodney W. Grimes 		 * be awakened.
1363df8bae1dSRodney W. Grimes 		 */
1364df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
13652c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1366df8bae1dSRodney W. Grimes 			goto out;
1367df8bae1dSRodney W. Grimes 		}
1368df8bae1dSRodney W. Grimes 		/*
1369df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1370df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1371df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1372df8bae1dSRodney W. Grimes 		 * cause the process to run.
1373df8bae1dSRodney W. Grimes 		 */
1374df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
13753e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1376df8bae1dSRodney W. Grimes 				goto runfast;
1377df8bae1dSRodney W. Grimes 			/*
1378df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1379df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1380df8bae1dSRodney W. Grimes 			 */
1381df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1382df8bae1dSRodney W. Grimes 				goto out;
13839c1ab3e0SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
13842c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
13852c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1386628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1387f591779bSSeigo Tanimura 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1388df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1389628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
13905b3047d5SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1391df8bae1dSRodney W. Grimes 			stop(p);
1392df8bae1dSRodney W. Grimes 			goto out;
13933e6831f5SJohn Baldwin 		} else
1394df8bae1dSRodney W. Grimes 			goto runfast;
1395df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1396df8bae1dSRodney W. Grimes 
1397df8bae1dSRodney W. Grimes 	case SSTOP:
1398df8bae1dSRodney W. Grimes 		/*
1399df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1400df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1401df8bae1dSRodney W. Grimes 		 */
140240447cd4SJohn Baldwin 		if (p->p_flag & P_TRACED)
1403df8bae1dSRodney W. Grimes 			goto out;
1404df8bae1dSRodney W. Grimes 
1405df8bae1dSRodney W. Grimes 		/*
1406df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1407df8bae1dSRodney W. Grimes 		 */
14082c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1409df8bae1dSRodney W. Grimes 			goto runfast;
1410df8bae1dSRodney W. Grimes 
1411df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1412df8bae1dSRodney W. Grimes 			/*
1413df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1414df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1415df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1416df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1417df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1418df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1419df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1420df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1421df8bae1dSRodney W. Grimes 			 */
1422df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
14232c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1424df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1425df8bae1dSRodney W. Grimes 				goto runfast;
1426b40ce416SJulian Elischer 			/*
1427b40ce416SJulian Elischer 			 * XXXKSE
1428b40ce416SJulian Elischer 			 * do this for each thread.
1429b40ce416SJulian Elischer 			 */
1430b40ce416SJulian Elischer 			if (p->p_flag & P_KSES) {
1431b40ce416SJulian Elischer 				mtx_assert(&sched_lock,
1432b40ce416SJulian Elischer 				    MA_OWNED | MA_NOTRECURSED);
1433b40ce416SJulian Elischer 				FOREACH_THREAD_IN_PROC(p, td) {
1434b40ce416SJulian Elischer 					if (td->td_wchan == NULL) {
1435b40ce416SJulian Elischer 						setrunnable(td); /* XXXKSE */
1436b40ce416SJulian Elischer 					} else {
1437b40ce416SJulian Elischer 						/* mark it as sleeping */
1438b40ce416SJulian Elischer 					}
1439b40ce416SJulian Elischer 				}
1440b40ce416SJulian Elischer 			} else {
1441079b7badSJulian Elischer 				if (td->td_wchan == NULL)
1442df8bae1dSRodney W. Grimes 					goto run;
1443df8bae1dSRodney W. Grimes 				p->p_stat = SSLEEP;
1444b40ce416SJulian Elischer 			}
1445fdd4e5c6SJulian Elischer 			goto out;
1446df8bae1dSRodney W. Grimes 		}
1447df8bae1dSRodney W. Grimes 
1448df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1449df8bae1dSRodney W. Grimes 			/*
1450df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1451df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1452df8bae1dSRodney W. Grimes 			 */
14532c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1454df8bae1dSRodney W. Grimes 			goto out;
1455df8bae1dSRodney W. Grimes 		}
1456df8bae1dSRodney W. Grimes 
1457df8bae1dSRodney W. Grimes 		/*
1458df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1459df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1460df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1461df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1462b40ce416SJulian Elischer 		 * XXXKSE should we wake ALL blocked threads?
1463df8bae1dSRodney W. Grimes 		 */
1464b40ce416SJulian Elischer 		if (p->p_flag & P_KSES) {
1465b40ce416SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1466b40ce416SJulian Elischer 				if (td->td_wchan && (td->td_flags & TDF_SINTR)){
1467b40ce416SJulian Elischer 					if (td->td_flags & TDF_CVWAITQ)
1468b40ce416SJulian Elischer 						cv_waitq_remove(td);
1469238510fcSJason Evans 					else
1470b40ce416SJulian Elischer 						unsleep(td); /* XXXKSE */
1471b40ce416SJulian Elischer 				}
1472b40ce416SJulian Elischer 			}
1473b40ce416SJulian Elischer 		} else {
1474b40ce416SJulian Elischer 			if (td->td_wchan && td->td_flags & TDF_SINTR) {
1475b40ce416SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
1476b40ce416SJulian Elischer 					cv_waitq_remove(td);
1477b40ce416SJulian Elischer 				else
1478b40ce416SJulian Elischer 					unsleep(td); /* XXXKSE */
1479b40ce416SJulian Elischer 			}
1480238510fcSJason Evans 		}
1481df8bae1dSRodney W. Grimes 		goto out;
1482df8bae1dSRodney W. Grimes 
1483df8bae1dSRodney W. Grimes 	default:
1484df8bae1dSRodney W. Grimes 		/*
1485df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1486df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1487df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1488df8bae1dSRodney W. Grimes 		 */
1489d2ef4060SBruce Evans 		if (p->p_stat == SRUN) {
14903163861cSTor Egge #ifdef SMP
1491b40ce416SJulian Elischer 			struct kse *ke;
1492b40ce416SJulian Elischer 			struct thread *td = curthread;
1493b40ce416SJulian Elischer /* we should only deliver to one thread.. but which one? */
1494b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(p, kg) {
1495b40ce416SJulian Elischer 				FOREACH_KSE_IN_GROUP(kg, ke) {
1496b40ce416SJulian Elischer 					if (ke->ke_thread == td) {
1497b40ce416SJulian Elischer 						continue;
1498b40ce416SJulian Elischer 					}
1499b40ce416SJulian Elischer 					forward_signal(ke->ke_thread);
1500b40ce416SJulian Elischer 				}
1501b40ce416SJulian Elischer 			}
15023163861cSTor Egge #endif
15036caa8a15SJohn Baldwin 		}
1504df8bae1dSRodney W. Grimes 		goto out;
1505df8bae1dSRodney W. Grimes 	}
1506df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1507df8bae1dSRodney W. Grimes 
1508df8bae1dSRodney W. Grimes runfast:
1509df8bae1dSRodney W. Grimes 	/*
1510df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1511b40ce416SJulian Elischer 	 * XXXKSE Should we make them all run fast?
1512b40ce416SJulian Elischer 	 * Maybe just one would be enough?
1513df8bae1dSRodney W. Grimes 	 */
15142c100766SJulian Elischer 
15152c100766SJulian Elischer 	if (FIRST_THREAD_IN_PROC(p)->td_priority > PUSER) {
15162c100766SJulian Elischer 		FIRST_THREAD_IN_PROC(p)->td_priority = PUSER;
1517b40ce416SJulian Elischer 	}
1518df8bae1dSRodney W. Grimes run:
15193e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
15203e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1521b40ce416SJulian Elischer 	setrunnable(td); /* XXXKSE */
15223e6831f5SJohn Baldwin out:
15239c1ab3e0SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
15249c1ab3e0SJohn Baldwin 
15259c1ab3e0SJohn Baldwin 	/* Once we get here, sched_lock should not be owned. */
15263e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1527df8bae1dSRodney W. Grimes }
1528df8bae1dSRodney W. Grimes 
1529df8bae1dSRodney W. Grimes /*
1530df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1531df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1532df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1533df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1534df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1535df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1536df8bae1dSRodney W. Grimes  * sequence is
1537df8bae1dSRodney W. Grimes  *
15382c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
15392c42a146SMarcel Moolenaar  *		postsig(sig);
1540df8bae1dSRodney W. Grimes  */
154126f9a767SRodney W. Grimes int
1542df8bae1dSRodney W. Grimes issignal(p)
1543df8bae1dSRodney W. Grimes 	register struct proc *p;
1544df8bae1dSRodney W. Grimes {
15452c42a146SMarcel Moolenaar 	sigset_t mask;
15462c42a146SMarcel Moolenaar 	register int sig, prop;
1547df8bae1dSRodney W. Grimes 
1548628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1549df8bae1dSRodney W. Grimes 	for (;;) {
15502a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
15512a024a2bSSean Eric Fagan 
15522c42a146SMarcel Moolenaar 		mask = p->p_siglist;
15532c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1554df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
15552c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
15562c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1557df8bae1dSRodney W. Grimes 			return (0);
15582c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
15592c42a146SMarcel Moolenaar 		prop = sigprop(sig);
15602a024a2bSSean Eric Fagan 
1561628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
15622a024a2bSSean Eric Fagan 
1563df8bae1dSRodney W. Grimes 		/*
1564df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1565df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1566df8bae1dSRodney W. Grimes 		 */
15672c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
15682c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1569df8bae1dSRodney W. Grimes 			continue;
1570df8bae1dSRodney W. Grimes 		}
1571df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1572df8bae1dSRodney W. Grimes 			/*
1573df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1574df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1575df8bae1dSRodney W. Grimes 			 */
15762c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1577628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1578df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1579628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
1580df8bae1dSRodney W. Grimes 			do {
15819ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
15825b3047d5SJohn Baldwin 				stop(p);
1583c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1584c86b6ff5SJohn Baldwin 				DROP_GIANT();
15852ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1586df8bae1dSRodney W. Grimes 				mi_switch();
15879ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
158820cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1589628d2653SJohn Baldwin 				PROC_LOCK(p);
15902a024a2bSSean Eric Fagan 			} while (!trace_req(p)
15912a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1592df8bae1dSRodney W. Grimes 
1593df8bae1dSRodney W. Grimes 			/*
1594df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1595df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1596df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1597df8bae1dSRodney W. Grimes 			 */
1598df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1599df8bae1dSRodney W. Grimes 				continue;
1600df8bae1dSRodney W. Grimes 
1601df8bae1dSRodney W. Grimes 			/*
1602df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1603df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1604df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1605df8bae1dSRodney W. Grimes 			 */
16062c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
16072c42a146SMarcel Moolenaar 			sig = p->p_xstat;
16082c42a146SMarcel Moolenaar 			if (sig == 0)
1609df8bae1dSRodney W. Grimes 				continue;
1610df8bae1dSRodney W. Grimes 
1611df8bae1dSRodney W. Grimes 			/*
1612df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1613df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1614df8bae1dSRodney W. Grimes 			 */
16152c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
16162c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1617df8bae1dSRodney W. Grimes 				continue;
1618df8bae1dSRodney W. Grimes 		}
1619df8bae1dSRodney W. Grimes 
1620df8bae1dSRodney W. Grimes 		/*
1621df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1622df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1623df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1624df8bae1dSRodney W. Grimes 		 */
16252c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1626df8bae1dSRodney W. Grimes 
16270b53fbe8SBruce Evans 		case (int)SIG_DFL:
1628df8bae1dSRodney W. Grimes 			/*
1629df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1630df8bae1dSRodney W. Grimes 			 */
1631df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1632df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1633df8bae1dSRodney W. Grimes 				/*
1634df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1635df8bae1dSRodney W. Grimes 				 * in init? XXX
1636df8bae1dSRodney W. Grimes 				 */
1637d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
16382c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1639df8bae1dSRodney W. Grimes #endif
1640df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1641df8bae1dSRodney W. Grimes 			}
1642df8bae1dSRodney W. Grimes 			/*
1643df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1644df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1645df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1646df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1647df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1648df8bae1dSRodney W. Grimes 			 */
1649df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1650df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1651df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1652df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
1653df8bae1dSRodney W. Grimes 					break;	/* == ignore */
16542c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1655628d2653SJohn Baldwin 				PROC_LOCK(p->p_pptr);
1656f591779bSSeigo Tanimura 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1657df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1658628d2653SJohn Baldwin 				PROC_UNLOCK(p->p_pptr);
16599ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
16605b3047d5SJohn Baldwin 				stop(p);
1661c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1662c86b6ff5SJohn Baldwin 				DROP_GIANT();
16632ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1664df8bae1dSRodney W. Grimes 				mi_switch();
16659ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
166620cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1667628d2653SJohn Baldwin 				PROC_LOCK(p);
1668df8bae1dSRodney W. Grimes 				break;
1669df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1670df8bae1dSRodney W. Grimes 				/*
1671df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1672df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1673df8bae1dSRodney W. Grimes 				 */
1674df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1675df8bae1dSRodney W. Grimes 			} else
16762c42a146SMarcel Moolenaar 				return (sig);
1677df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1678df8bae1dSRodney W. Grimes 
16790b53fbe8SBruce Evans 		case (int)SIG_IGN:
1680df8bae1dSRodney W. Grimes 			/*
1681df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1682df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1683df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1684df8bae1dSRodney W. Grimes 			 */
1685df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1686df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1687df8bae1dSRodney W. Grimes 				printf("issignal\n");
1688df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1689df8bae1dSRodney W. Grimes 
1690df8bae1dSRodney W. Grimes 		default:
1691df8bae1dSRodney W. Grimes 			/*
1692df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1693df8bae1dSRodney W. Grimes 			 * postsig() process it.
1694df8bae1dSRodney W. Grimes 			 */
16952c42a146SMarcel Moolenaar 			return (sig);
1696df8bae1dSRodney W. Grimes 		}
16972c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1698df8bae1dSRodney W. Grimes 	}
1699df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1700df8bae1dSRodney W. Grimes }
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes /*
1703df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1704df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
17055b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
17065b3047d5SJohn Baldwin  * lock held.
1707df8bae1dSRodney W. Grimes  */
17085b3047d5SJohn Baldwin static void
1709df8bae1dSRodney W. Grimes stop(p)
1710df8bae1dSRodney W. Grimes 	register struct proc *p;
1711df8bae1dSRodney W. Grimes {
1712df8bae1dSRodney W. Grimes 
1713628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
17145b3047d5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
1715df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1716df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1717df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1718df8bae1dSRodney W. Grimes }
1719df8bae1dSRodney W. Grimes 
1720df8bae1dSRodney W. Grimes /*
1721df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1722df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1723df8bae1dSRodney W. Grimes  */
1724df8bae1dSRodney W. Grimes void
17252c42a146SMarcel Moolenaar postsig(sig)
17262c42a146SMarcel Moolenaar 	register int sig;
1727df8bae1dSRodney W. Grimes {
1728b40ce416SJulian Elischer 	struct thread *td = curthread;
1729b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1730628d2653SJohn Baldwin 	struct sigacts *ps;
17312c42a146SMarcel Moolenaar 	sig_t action;
17322c42a146SMarcel Moolenaar 	sigset_t returnmask;
17332c42a146SMarcel Moolenaar 	int code;
1734df8bae1dSRodney W. Grimes 
17352c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
17365526d2d9SEivind Eklund 
17372ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1738628d2653SJohn Baldwin 	ps = p->p_sigacts;
17392c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
17402c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1741df8bae1dSRodney W. Grimes #ifdef KTRACE
1742df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1743645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
17442c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1745df8bae1dSRodney W. Grimes #endif
1746628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
17472a024a2bSSean Eric Fagan 
1748df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1749df8bae1dSRodney W. Grimes 		/*
1750df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1751df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1752df8bae1dSRodney W. Grimes 		 */
1753b40ce416SJulian Elischer 		sigexit(td, sig);
1754df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1755df8bae1dSRodney W. Grimes 	} else {
1756df8bae1dSRodney W. Grimes 		/*
1757df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1758df8bae1dSRodney W. Grimes 		 */
17592c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
17605526d2d9SEivind Eklund 		    ("postsig action"));
1761df8bae1dSRodney W. Grimes 		/*
1762df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1763645682fdSLuoqi Chen 		 * occurrences of this signal.
1764df8bae1dSRodney W. Grimes 		 *
1765645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1766df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1767645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1768df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1769df8bae1dSRodney W. Grimes 		 */
1770645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
17716626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1772645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1773df8bae1dSRodney W. Grimes 		} else
1774df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
17752c42a146SMarcel Moolenaar 
17762c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
17772c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
17782c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
17792c42a146SMarcel Moolenaar 
17802c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1781289ccde0SPeter Wemm 			/*
17822c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1783289ccde0SPeter Wemm 			 */
17842c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
17852c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
17862c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
17872c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
17882c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1789dedc04feSPeter Wemm 		}
1790df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
17912c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1792df8bae1dSRodney W. Grimes 			code = 0;
1793df8bae1dSRodney W. Grimes 		} else {
17946626c604SJulian Elischer 			code = p->p_code;
17956626c604SJulian Elischer 			p->p_code = 0;
17966626c604SJulian Elischer 			p->p_sig = 0;
1797df8bae1dSRodney W. Grimes 		}
17982c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1799df8bae1dSRodney W. Grimes 	}
1800df8bae1dSRodney W. Grimes }
1801df8bae1dSRodney W. Grimes 
1802df8bae1dSRodney W. Grimes /*
1803df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1804df8bae1dSRodney W. Grimes  */
180526f9a767SRodney W. Grimes void
1806df8bae1dSRodney W. Grimes killproc(p, why)
1807df8bae1dSRodney W. Grimes 	struct proc *p;
1808df8bae1dSRodney W. Grimes 	char *why;
1809df8bae1dSRodney W. Grimes {
18109081e5e8SJohn Baldwin 
18119081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18120384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
18130384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1814729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1815b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1816df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1817df8bae1dSRodney W. Grimes }
1818df8bae1dSRodney W. Grimes 
1819df8bae1dSRodney W. Grimes /*
1820df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1821df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1822df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1823df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1824df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1825df8bae1dSRodney W. Grimes  * does not return.
1826df8bae1dSRodney W. Grimes  */
182726f9a767SRodney W. Grimes void
1828b40ce416SJulian Elischer sigexit(td, sig)
1829b40ce416SJulian Elischer 	struct thread *td;
18302c42a146SMarcel Moolenaar 	int sig;
1831df8bae1dSRodney W. Grimes {
1832b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1833df8bae1dSRodney W. Grimes 
1834628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1835df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
18362c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
18372c42a146SMarcel Moolenaar 		p->p_sig = sig;
1838c364e17eSAndrey A. Chernov 		/*
1839c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1840c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1841c364e17eSAndrey A. Chernov 		 * these messages.)
1842c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1843c364e17eSAndrey A. Chernov 		 */
1844628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1845c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1846c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1847b40ce416SJulian Elischer 		if (coredump(td) == 0)
18482c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
184957308494SJoerg Wunsch 		if (kern_logsigexit)
185057308494SJoerg Wunsch 			log(LOG_INFO,
185157308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
18523d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
18539c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
18542c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
18552c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1856c31146a1SJohn Baldwin 	} else {
1857628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1858628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1859628d2653SJohn Baldwin 			mtx_lock(&Giant);
1860c31146a1SJohn Baldwin 	}
1861b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1862df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1863df8bae1dSRodney W. Grimes }
1864df8bae1dSRodney W. Grimes 
1865c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1866c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1867c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1868c5edb423SSean Eric Fagan 
1869c5edb423SSean Eric Fagan /*
1870c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1871c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1872c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1873c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1874c5edb423SSean Eric Fagan  *	%P	process id (pid)
1875c5edb423SSean Eric Fagan  *	%U	user id (uid)
1876c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1877c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1878c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1879c5edb423SSean Eric Fagan  */
1880c5edb423SSean Eric Fagan 
1881fca666a1SJulian Elischer static char *
1882c5edb423SSean Eric Fagan expand_name(name, uid, pid)
188387f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1884c5edb423SSean Eric Fagan 	char *temp;
1885c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1886c5edb423SSean Eric Fagan 	int i, n;
1887c5edb423SSean Eric Fagan 	char *format = corefilename;
1888ce38ca0fSAlfred Perlstein 	size_t namelen;
1889c5edb423SSean Eric Fagan 
1890ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
18910bfe2990SEivind Eklund 	if (temp == NULL)
18920bfe2990SEivind Eklund 		return NULL;
1893ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1894ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1895c5edb423SSean Eric Fagan 		int l;
1896c5edb423SSean Eric Fagan 		switch (format[i]) {
1897c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1898c5edb423SSean Eric Fagan 			i++;
1899c5edb423SSean Eric Fagan 			switch (format[i]) {
1900c5edb423SSean Eric Fagan 			case '%':
1901c5edb423SSean Eric Fagan 				temp[n++] = '%';
1902c5edb423SSean Eric Fagan 				break;
1903c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1904ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
190587f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1906c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1907c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1908c5edb423SSean Eric Fagan 					return NULL;
1909c5edb423SSean Eric Fagan 				}
1910ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1911ce38ca0fSAlfred Perlstein 				n += namelen;
1912c5edb423SSean Eric Fagan 				break;
1913c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1914ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1915c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
191687f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1917c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1918c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1919c5edb423SSean Eric Fagan 					return NULL;
1920c5edb423SSean Eric Fagan 				}
1921c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1922c5edb423SSean Eric Fagan 				n += l;
1923c5edb423SSean Eric Fagan 				break;
1924c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1925ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1926c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
192787f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1928c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1929c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1930c5edb423SSean Eric Fagan 					return NULL;
1931c5edb423SSean Eric Fagan 				}
1932c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1933c5edb423SSean Eric Fagan 				n += l;
1934c5edb423SSean Eric Fagan 				break;
1935c5edb423SSean Eric Fagan 			default:
1936c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1937c5edb423SSean Eric Fagan 			}
1938c5edb423SSean Eric Fagan 			break;
1939c5edb423SSean Eric Fagan 		default:
1940c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1941c5edb423SSean Eric Fagan 		}
1942c5edb423SSean Eric Fagan 	}
1943ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1944c5edb423SSean Eric Fagan 	return temp;
1945c5edb423SSean Eric Fagan }
1946c5edb423SSean Eric Fagan 
1947df8bae1dSRodney W. Grimes /*
1948fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1949fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1950fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1951fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1952fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
19535da271f5SRobert Watson  *
19545da271f5SRobert Watson  * XXX: VOP_GETATTR() here requires holding the vnode lock.
1955fca666a1SJulian Elischer  */
1956fca666a1SJulian Elischer 
1957fca666a1SJulian Elischer static int
1958b40ce416SJulian Elischer coredump(struct thread *td)
1959fca666a1SJulian Elischer {
1960b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1961fca666a1SJulian Elischer 	register struct vnode *vp;
19629c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
196306ae1e91SMatthew Dillon 	struct flock lf;
1964fca666a1SJulian Elischer 	struct nameidata nd;
1965fca666a1SJulian Elischer 	struct vattr vattr;
1966e6796b67SKirk McKusick 	int error, error1, flags;
1967f2a2857bSKirk McKusick 	struct mount *mp;
1968fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1969fca666a1SJulian Elischer 	off_t limit;
1970fca666a1SJulian Elischer 
1971628d2653SJohn Baldwin 	PROC_LOCK(p);
1972628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1973fca666a1SJulian Elischer 
1974628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1975628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1976fca666a1SJulian Elischer 		return (EFAULT);
1977628d2653SJohn Baldwin 	}
1978fca666a1SJulian Elischer 
1979fca666a1SJulian Elischer 	/*
198035a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
198135a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
198235a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
198335a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
198435a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
198535a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1986fca666a1SJulian Elischer 	 */
198735a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1988628d2653SJohn Baldwin 	if (limit == 0) {
1989628d2653SJohn Baldwin 		PROC_UNLOCK(p);
199035a2598fSSean Eric Fagan 		return 0;
1991628d2653SJohn Baldwin 	}
1992628d2653SJohn Baldwin 	PROC_UNLOCK(p);
199335a2598fSSean Eric Fagan 
1994f2a2857bSKirk McKusick restart:
19959c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
1996ccdbd10cSPeter Pentchev 	if (name == NULL)
1997ccdbd10cSPeter Pentchev 		return (EINVAL);
1998b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
1999e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2000e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2001fca666a1SJulian Elischer 	free(name, M_TEMP);
2002fca666a1SJulian Elischer 	if (error)
2003fca666a1SJulian Elischer 		return (error);
2004762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2005fca666a1SJulian Elischer 	vp = nd.ni_vp;
200606ae1e91SMatthew Dillon 
2007b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
200806ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
200906ae1e91SMatthew Dillon 	lf.l_start = 0;
201006ae1e91SMatthew Dillon 	lf.l_len = 0;
201106ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
201206ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
201306ae1e91SMatthew Dillon 	if (error)
201406ae1e91SMatthew Dillon 		goto out2;
201506ae1e91SMatthew Dillon 
201606ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
201706ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
201806ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2019b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2020f2a2857bSKirk McKusick 			return (error);
2021f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2022f2a2857bSKirk McKusick 			return (error);
2023f2a2857bSKirk McKusick 		goto restart;
2024f2a2857bSKirk McKusick 	}
2025fca666a1SJulian Elischer 
2026fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
2027fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
2028b40ce416SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2029fca666a1SJulian Elischer 		error = EFAULT;
203006ae1e91SMatthew Dillon 		goto out1;
2031fca666a1SJulian Elischer 	}
2032fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2033fca666a1SJulian Elischer 	vattr.va_size = 0;
203488b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2035b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2036b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
203788b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2038628d2653SJohn Baldwin 	PROC_LOCK(p);
2039fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2040628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2041fca666a1SJulian Elischer 
2042fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2043b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2044fca666a1SJulian Elischer 	  ENOSYS;
2045fca666a1SJulian Elischer 
204606ae1e91SMatthew Dillon out1:
204706ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
204806ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2049f2a2857bSKirk McKusick 	vn_finished_write(mp);
205006ae1e91SMatthew Dillon out2:
2051b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2052fca666a1SJulian Elischer 	if (error == 0)
2053fca666a1SJulian Elischer 		error = error1;
2054fca666a1SJulian Elischer 	return (error);
2055fca666a1SJulian Elischer }
2056fca666a1SJulian Elischer 
2057fca666a1SJulian Elischer /*
2058df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2059df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2060df8bae1dSRodney W. Grimes  */
2061d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2062df8bae1dSRodney W. Grimes struct nosys_args {
2063df8bae1dSRodney W. Grimes 	int	dummy;
2064df8bae1dSRodney W. Grimes };
2065d2d3e875SBruce Evans #endif
2066fb99ab88SMatthew Dillon /*
2067fb99ab88SMatthew Dillon  * MPSAFE
2068fb99ab88SMatthew Dillon  */
2069df8bae1dSRodney W. Grimes /* ARGSUSED */
207026f9a767SRodney W. Grimes int
2071b40ce416SJulian Elischer nosys(td, args)
2072b40ce416SJulian Elischer 	struct thread *td;
2073df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2074df8bae1dSRodney W. Grimes {
2075b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2076b40ce416SJulian Elischer 
2077fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2078628d2653SJohn Baldwin 	PROC_LOCK(p);
2079df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2080628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2081fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2082df8bae1dSRodney W. Grimes 	return (EINVAL);
2083df8bae1dSRodney W. Grimes }
2084831d27a9SDon Lewis 
2085831d27a9SDon Lewis /*
208648f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2087831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2088831d27a9SDon Lewis  */
2089831d27a9SDon Lewis void
20902c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
2091831d27a9SDon Lewis 	struct sigio *sigio;
20922c42a146SMarcel Moolenaar 	int sig, checkctty;
2093831d27a9SDon Lewis {
2094831d27a9SDon Lewis 	if (sigio == NULL)
2095831d27a9SDon Lewis 		return;
2096831d27a9SDon Lewis 
2097831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2098628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
20992b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
21002c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2101628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2102831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2103831d27a9SDon Lewis 		struct proc *p;
2104831d27a9SDon Lewis 
2105f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2106628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2107628d2653SJohn Baldwin 			PROC_LOCK(p);
21082b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2109831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
21102c42a146SMarcel Moolenaar 				psignal(p, sig);
2111628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2112628d2653SJohn Baldwin 		}
2113f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2114831d27a9SDon Lewis 	}
2115831d27a9SDon Lewis }
2116cb679c38SJonathan Lemon 
2117cb679c38SJonathan Lemon static int
2118cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2119cb679c38SJonathan Lemon {
2120cb679c38SJonathan Lemon 	struct proc *p = curproc;
2121cb679c38SJonathan Lemon 
2122cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2123cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2124cb679c38SJonathan Lemon 
2125628d2653SJohn Baldwin 	PROC_LOCK(p);
2126cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2127628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2128cb679c38SJonathan Lemon 
2129cb679c38SJonathan Lemon 	return (0);
2130cb679c38SJonathan Lemon }
2131cb679c38SJonathan Lemon 
2132cb679c38SJonathan Lemon static void
2133cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2134cb679c38SJonathan Lemon {
2135cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2136cb679c38SJonathan Lemon 
2137628d2653SJohn Baldwin 	PROC_LOCK(p);
2138e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2139628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2140cb679c38SJonathan Lemon }
2141cb679c38SJonathan Lemon 
2142cb679c38SJonathan Lemon /*
2143cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2144cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2145cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2146cb679c38SJonathan Lemon  * isn't worth the trouble.
2147cb679c38SJonathan Lemon  */
2148cb679c38SJonathan Lemon static int
2149cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2150cb679c38SJonathan Lemon {
2151cb679c38SJonathan Lemon 
2152cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2153cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2154cb679c38SJonathan Lemon 
2155cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2156cb679c38SJonathan Lemon 			kn->kn_data++;
2157cb679c38SJonathan Lemon 	}
2158cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2159cb679c38SJonathan Lemon }
2160