xref: /freebsd/sys/kern/kern_sig.c (revision ba626c1db26edacaf665cdfd199cfe6480c7fb9c)
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;
307aa7a4daeSPeter Wemm 			if ((act->sa_flags & SA_NOCLDWAIT) ||
308aa7a4daeSPeter Wemm 			    ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) {
309245f17d4SJoerg Wunsch 				/*
3102c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3112c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3122c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3132c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
314245f17d4SJoerg Wunsch 				 */
315245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
316645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3176626c604SJulian Elischer 				else
318645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
319245f17d4SJoerg Wunsch 			} else
320645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
321df8bae1dSRodney W. Grimes 		}
322df8bae1dSRodney W. Grimes 		/*
323df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3242c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3252c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3262c42a146SMarcel Moolenaar 		 * have to restart the process.
327df8bae1dSRodney W. Grimes 		 */
3282c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3292c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3302c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3312c42a146SMarcel Moolenaar 			/* never to be seen again */
3322c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3332c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3342c42a146SMarcel Moolenaar 				/* easier in psignal */
3352c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3362c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
337645682fdSLuoqi Chen 		} else {
3382c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3392c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3402c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3412c42a146SMarcel Moolenaar 			else
3422c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3432c42a146SMarcel Moolenaar 		}
344e8ebc08fSPeter Wemm #ifdef COMPAT_43
345645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
346645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
347645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
348645682fdSLuoqi Chen 		else
349645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
350e8ebc08fSPeter Wemm #endif
351df8bae1dSRodney W. Grimes 	}
352628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3532c42a146SMarcel Moolenaar 	return (0);
3542c42a146SMarcel Moolenaar }
3552c42a146SMarcel Moolenaar 
3562c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3572c42a146SMarcel Moolenaar struct sigaction_args {
3582c42a146SMarcel Moolenaar 	int	sig;
3592c42a146SMarcel Moolenaar 	struct	sigaction *act;
3602c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3612c42a146SMarcel Moolenaar };
3622c42a146SMarcel Moolenaar #endif
363fb99ab88SMatthew Dillon /*
364fb99ab88SMatthew Dillon  * MPSAFE
365fb99ab88SMatthew Dillon  */
3662c42a146SMarcel Moolenaar /* ARGSUSED */
3672c42a146SMarcel Moolenaar int
368b40ce416SJulian Elischer sigaction(td, uap)
369b40ce416SJulian Elischer 	struct thread *td;
3702c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3712c42a146SMarcel Moolenaar {
372b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3732c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3742c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3752c42a146SMarcel Moolenaar 	int error;
3762c42a146SMarcel Moolenaar 
377fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
378fb99ab88SMatthew Dillon 
3796f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3806f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3812c42a146SMarcel Moolenaar 	if (actp) {
3826f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3832c42a146SMarcel Moolenaar 		if (error)
384fb99ab88SMatthew Dillon 			goto done2;
3852c42a146SMarcel Moolenaar 	}
386645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3872c42a146SMarcel Moolenaar 	if (oactp && !error) {
3886f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3892c42a146SMarcel Moolenaar 	}
390fb99ab88SMatthew Dillon done2:
391fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
3922c42a146SMarcel Moolenaar 	return (error);
3932c42a146SMarcel Moolenaar }
3942c42a146SMarcel Moolenaar 
39531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3962c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3972c42a146SMarcel Moolenaar struct osigaction_args {
3982c42a146SMarcel Moolenaar 	int	signum;
3992c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4002c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4012c42a146SMarcel Moolenaar };
4022c42a146SMarcel Moolenaar #endif
403fb99ab88SMatthew Dillon /*
404fb99ab88SMatthew Dillon  * MPSAFE
405fb99ab88SMatthew Dillon  */
4062c42a146SMarcel Moolenaar /* ARGSUSED */
4072c42a146SMarcel Moolenaar int
408b40ce416SJulian Elischer osigaction(td, uap)
409b40ce416SJulian Elischer 	struct thread *td;
4102c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4112c42a146SMarcel Moolenaar {
412b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
4132c42a146SMarcel Moolenaar 	struct osigaction sa;
4142c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4152c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4162c42a146SMarcel Moolenaar 	int error;
4172c42a146SMarcel Moolenaar 
4186f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4196f841fb7SMarcel Moolenaar 		return (EINVAL);
420fb99ab88SMatthew Dillon 
4216f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4226f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
423fb99ab88SMatthew Dillon 
424fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
425fb99ab88SMatthew Dillon 
4262c42a146SMarcel Moolenaar 	if (nsap) {
4276f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4282c42a146SMarcel Moolenaar 		if (error)
429fb99ab88SMatthew Dillon 			goto done2;
4302c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4312c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4322c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4332c42a146SMarcel Moolenaar 	}
434645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
4352c42a146SMarcel Moolenaar 	if (osap && !error) {
4362c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4372c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4382c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4396f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4402c42a146SMarcel Moolenaar 	}
441fb99ab88SMatthew Dillon done2:
442fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
4432c42a146SMarcel Moolenaar 	return (error);
4442c42a146SMarcel Moolenaar }
44531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
446df8bae1dSRodney W. Grimes 
447df8bae1dSRodney W. Grimes /*
448df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
449df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
450df8bae1dSRodney W. Grimes  */
451df8bae1dSRodney W. Grimes void
452df8bae1dSRodney W. Grimes siginit(p)
453df8bae1dSRodney W. Grimes 	struct proc *p;
454df8bae1dSRodney W. Grimes {
455df8bae1dSRodney W. Grimes 	register int i;
456df8bae1dSRodney W. Grimes 
457628d2653SJohn Baldwin 	PROC_LOCK(p);
4582c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4592c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4602c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
461628d2653SJohn Baldwin 	PROC_UNLOCK(p);
462df8bae1dSRodney W. Grimes }
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes /*
465df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
466df8bae1dSRodney W. Grimes  */
467df8bae1dSRodney W. Grimes void
468df8bae1dSRodney W. Grimes execsigs(p)
469df8bae1dSRodney W. Grimes 	register struct proc *p;
470df8bae1dSRodney W. Grimes {
471628d2653SJohn Baldwin 	register struct sigacts *ps;
4722c42a146SMarcel Moolenaar 	register int sig;
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes 	/*
475df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
476df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
477df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
478df8bae1dSRodney W. Grimes 	 */
479628d2653SJohn Baldwin 	PROC_LOCK(p);
480628d2653SJohn Baldwin 	ps = p->p_sigacts;
4812c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4822c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4832c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4842c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4852c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4862c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4872c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
488df8bae1dSRodney W. Grimes 		}
4892c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
490df8bae1dSRodney W. Grimes 	}
491df8bae1dSRodney W. Grimes 	/*
492df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
493df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
494df8bae1dSRodney W. Grimes 	 */
495645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
496645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
497645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
4983b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
49980e907a1SPeter Wemm 	/*
50080e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
50180e907a1SPeter Wemm 	 */
502645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
503c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
504c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
505628d2653SJohn Baldwin 	PROC_UNLOCK(p);
506df8bae1dSRodney W. Grimes }
507df8bae1dSRodney W. Grimes 
508df8bae1dSRodney W. Grimes /*
509628d2653SJohn Baldwin  * do_sigprocmask()
5107c8fdcbdSMatthew Dillon  *
511628d2653SJohn Baldwin  *	Manipulate signal mask.
512df8bae1dSRodney W. Grimes  */
5132c42a146SMarcel Moolenaar static int
514645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
5152c42a146SMarcel Moolenaar 	struct proc *p;
5162c42a146SMarcel Moolenaar 	int how;
5172c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
518645682fdSLuoqi Chen 	int old;
5192c42a146SMarcel Moolenaar {
5202c42a146SMarcel Moolenaar 	int error;
5212c42a146SMarcel Moolenaar 
522628d2653SJohn Baldwin 	PROC_LOCK(p);
5232c42a146SMarcel Moolenaar 	if (oset != NULL)
5242c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
5252c42a146SMarcel Moolenaar 
5262c42a146SMarcel Moolenaar 	error = 0;
5272c42a146SMarcel Moolenaar 	if (set != NULL) {
5282c42a146SMarcel Moolenaar 		switch (how) {
5292c42a146SMarcel Moolenaar 		case SIG_BLOCK:
530645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
5312c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
5322c42a146SMarcel Moolenaar 			break;
5332c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
5342c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
53579065dbaSBruce Evans 			signotify(p);
5362c42a146SMarcel Moolenaar 			break;
5372c42a146SMarcel Moolenaar 		case SIG_SETMASK:
538645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
539645682fdSLuoqi Chen 			if (old)
540645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
541645682fdSLuoqi Chen 			else
5422c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
54379065dbaSBruce Evans 			signotify(p);
5442c42a146SMarcel Moolenaar 			break;
5452c42a146SMarcel Moolenaar 		default:
5462c42a146SMarcel Moolenaar 			error = EINVAL;
5472c42a146SMarcel Moolenaar 			break;
5482c42a146SMarcel Moolenaar 		}
5492c42a146SMarcel Moolenaar 	}
550628d2653SJohn Baldwin 	PROC_UNLOCK(p);
5512c42a146SMarcel Moolenaar 	return (error);
5522c42a146SMarcel Moolenaar }
5532c42a146SMarcel Moolenaar 
5547c8fdcbdSMatthew Dillon /*
555b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
5567c8fdcbdSMatthew Dillon  */
5577c8fdcbdSMatthew Dillon 
558d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
559df8bae1dSRodney W. Grimes struct sigprocmask_args {
560df8bae1dSRodney W. Grimes 	int	how;
5612c42a146SMarcel Moolenaar 	const sigset_t *set;
5622c42a146SMarcel Moolenaar 	sigset_t *oset;
563df8bae1dSRodney W. Grimes };
564d2d3e875SBruce Evans #endif
56526f9a767SRodney W. Grimes int
566b40ce416SJulian Elischer sigprocmask(td, uap)
567b40ce416SJulian Elischer 	register struct thread *td;
568df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
569df8bae1dSRodney W. Grimes {
570b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
5712c42a146SMarcel Moolenaar 	sigset_t set, oset;
5722c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5732c42a146SMarcel Moolenaar 	int error;
574df8bae1dSRodney W. Grimes 
5756f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5766f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5772c42a146SMarcel Moolenaar 	if (setp) {
5786f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5792c42a146SMarcel Moolenaar 		if (error)
5802c42a146SMarcel Moolenaar 			return (error);
581df8bae1dSRodney W. Grimes 	}
582645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5832c42a146SMarcel Moolenaar 	if (osetp && !error) {
5846f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5852c42a146SMarcel Moolenaar 	}
5862c42a146SMarcel Moolenaar 	return (error);
5872c42a146SMarcel Moolenaar }
5882c42a146SMarcel Moolenaar 
58931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5907c8fdcbdSMatthew Dillon /*
5917c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5927c8fdcbdSMatthew Dillon  */
5932c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5942c42a146SMarcel Moolenaar struct osigprocmask_args {
5952c42a146SMarcel Moolenaar 	int	how;
5962c42a146SMarcel Moolenaar 	osigset_t mask;
5972c42a146SMarcel Moolenaar };
5982c42a146SMarcel Moolenaar #endif
5992c42a146SMarcel Moolenaar int
600b40ce416SJulian Elischer osigprocmask(td, uap)
601b40ce416SJulian Elischer 	register struct thread *td;
6022c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
6032c42a146SMarcel Moolenaar {
604b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
6052c42a146SMarcel Moolenaar 	sigset_t set, oset;
6062c42a146SMarcel Moolenaar 	int error;
6072c42a146SMarcel Moolenaar 
6082c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
609645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
610b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
611df8bae1dSRodney W. Grimes 	return (error);
612df8bae1dSRodney W. Grimes }
61331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
614df8bae1dSRodney W. Grimes 
615d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
616df8bae1dSRodney W. Grimes struct sigpending_args {
6172c42a146SMarcel Moolenaar 	sigset_t	*set;
618df8bae1dSRodney W. Grimes };
619d2d3e875SBruce Evans #endif
620fb99ab88SMatthew Dillon /*
621fb99ab88SMatthew Dillon  * MPSAFE
622fb99ab88SMatthew Dillon  */
623df8bae1dSRodney W. Grimes /* ARGSUSED */
62426f9a767SRodney W. Grimes int
625b40ce416SJulian Elischer sigpending(td, uap)
626b40ce416SJulian Elischer 	struct thread *td;
627df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
628df8bae1dSRodney W. Grimes {
629b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
630628d2653SJohn Baldwin 	sigset_t siglist;
631fb99ab88SMatthew Dillon 	int error;
632df8bae1dSRodney W. Grimes 
633fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
634628d2653SJohn Baldwin 	PROC_LOCK(p);
635628d2653SJohn Baldwin 	siglist = p->p_siglist;
636628d2653SJohn Baldwin 	PROC_UNLOCK(p);
637fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
638fb99ab88SMatthew Dillon 	error = copyout(&siglist, uap->set, sizeof(sigset_t));
639fb99ab88SMatthew Dillon 	return(error);
6402c42a146SMarcel Moolenaar }
6412c42a146SMarcel Moolenaar 
64231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6432c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6442c42a146SMarcel Moolenaar struct osigpending_args {
6452c42a146SMarcel Moolenaar 	int	dummy;
6462c42a146SMarcel Moolenaar };
6472c42a146SMarcel Moolenaar #endif
648fb99ab88SMatthew Dillon /*
649fb99ab88SMatthew Dillon  * MPSAFE
650fb99ab88SMatthew Dillon  */
6512c42a146SMarcel Moolenaar /* ARGSUSED */
6522c42a146SMarcel Moolenaar int
653b40ce416SJulian Elischer osigpending(td, uap)
654b40ce416SJulian Elischer 	struct thread *td;
6552c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6562c42a146SMarcel Moolenaar {
657b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
658b40ce416SJulian Elischer 
659fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
660628d2653SJohn Baldwin 	PROC_LOCK(p);
661b40ce416SJulian Elischer 	SIG2OSIG(p->p_siglist, td->td_retval[0]);
662628d2653SJohn Baldwin 	PROC_UNLOCK(p);
663fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
664df8bae1dSRodney W. Grimes 	return (0);
665df8bae1dSRodney W. Grimes }
66631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
669df8bae1dSRodney W. Grimes /*
670df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
671df8bae1dSRodney W. Grimes  */
672d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
673df8bae1dSRodney W. Grimes struct osigvec_args {
674df8bae1dSRodney W. Grimes 	int	signum;
675df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
676df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
677df8bae1dSRodney W. Grimes };
678d2d3e875SBruce Evans #endif
679fb99ab88SMatthew Dillon /*
680fb99ab88SMatthew Dillon  * MPSAFE
681fb99ab88SMatthew Dillon  */
682df8bae1dSRodney W. Grimes /* ARGSUSED */
68326f9a767SRodney W. Grimes int
684b40ce416SJulian Elischer osigvec(td, uap)
685b40ce416SJulian Elischer 	struct thread *td;
686df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
687df8bae1dSRodney W. Grimes {
688b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
689df8bae1dSRodney W. Grimes 	struct sigvec vec;
6902c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6912c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6922c42a146SMarcel Moolenaar 	int error;
693df8bae1dSRodney W. Grimes 
6946f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6956f841fb7SMarcel Moolenaar 		return (EINVAL);
6966f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6976f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6982c42a146SMarcel Moolenaar 	if (nsap) {
6996f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
7002c42a146SMarcel Moolenaar 		if (error)
701df8bae1dSRodney W. Grimes 			return (error);
7022c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
7032c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
7042c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
7052c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
706df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
7072c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
708df8bae1dSRodney W. Grimes #endif
709df8bae1dSRodney W. Grimes 	}
710fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
711645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
712fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
7132c42a146SMarcel Moolenaar 	if (osap && !error) {
7142c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7152c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7162c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7172c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7182c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7192c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7202c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7212c42a146SMarcel Moolenaar #endif
7226f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
7232c42a146SMarcel Moolenaar 	}
7242c42a146SMarcel Moolenaar 	return (error);
725df8bae1dSRodney W. Grimes }
726df8bae1dSRodney W. Grimes 
727d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
728df8bae1dSRodney W. Grimes struct osigblock_args {
729df8bae1dSRodney W. Grimes 	int	mask;
730df8bae1dSRodney W. Grimes };
731d2d3e875SBruce Evans #endif
732fb99ab88SMatthew Dillon /*
733fb99ab88SMatthew Dillon  * MPSAFE
734fb99ab88SMatthew Dillon  */
73526f9a767SRodney W. Grimes int
736b40ce416SJulian Elischer osigblock(td, uap)
737b40ce416SJulian Elischer 	register struct thread *td;
738df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
739df8bae1dSRodney W. Grimes {
740b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7412c42a146SMarcel Moolenaar 	sigset_t set;
742df8bae1dSRodney W. Grimes 
7432c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7442c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
745fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
746628d2653SJohn Baldwin 	PROC_LOCK(p);
747b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
7482c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
749628d2653SJohn Baldwin 	PROC_UNLOCK(p);
750fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
751df8bae1dSRodney W. Grimes 	return (0);
752df8bae1dSRodney W. Grimes }
753df8bae1dSRodney W. Grimes 
754d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
755df8bae1dSRodney W. Grimes struct osigsetmask_args {
756df8bae1dSRodney W. Grimes 	int	mask;
757df8bae1dSRodney W. Grimes };
758d2d3e875SBruce Evans #endif
759fb99ab88SMatthew Dillon /*
760fb99ab88SMatthew Dillon  * MPSAFE
761fb99ab88SMatthew Dillon  */
76226f9a767SRodney W. Grimes int
763b40ce416SJulian Elischer osigsetmask(td, uap)
764b40ce416SJulian Elischer 	struct thread *td;
765df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
766df8bae1dSRodney W. Grimes {
767b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7682c42a146SMarcel Moolenaar 	sigset_t set;
769df8bae1dSRodney W. Grimes 
7702c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7712c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
772fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
773628d2653SJohn Baldwin 	PROC_LOCK(p);
774b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
775645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
77679065dbaSBruce Evans 	signotify(p);
777628d2653SJohn Baldwin 	PROC_UNLOCK(p);
778fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
779df8bae1dSRodney W. Grimes 	return (0);
780df8bae1dSRodney W. Grimes }
781df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
782df8bae1dSRodney W. Grimes 
783df8bae1dSRodney W. Grimes /*
784df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
785df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
786df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
787b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
788b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
789b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
790df8bae1dSRodney W. Grimes  */
791d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
792df8bae1dSRodney W. Grimes struct sigsuspend_args {
7932c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
794df8bae1dSRodney W. Grimes };
795d2d3e875SBruce Evans #endif
796fb99ab88SMatthew Dillon /*
797fb99ab88SMatthew Dillon  * MPSAFE
798fb99ab88SMatthew Dillon  */
799df8bae1dSRodney W. Grimes /* ARGSUSED */
80026f9a767SRodney W. Grimes int
801b40ce416SJulian Elischer sigsuspend(td, uap)
802b40ce416SJulian Elischer 	struct thread *td;
803df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
804df8bae1dSRodney W. Grimes {
805b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
8062c42a146SMarcel Moolenaar 	sigset_t mask;
807628d2653SJohn Baldwin 	register struct sigacts *ps;
8082c42a146SMarcel Moolenaar 	int error;
8092c42a146SMarcel Moolenaar 
8106f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
8112c42a146SMarcel Moolenaar 	if (error)
8122c42a146SMarcel Moolenaar 		return (error);
813df8bae1dSRodney W. Grimes 
814df8bae1dSRodney W. Grimes 	/*
815645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
816df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
817df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
818df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
819df8bae1dSRodney W. Grimes 	 * to indicate this.
820df8bae1dSRodney W. Grimes 	 */
821fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
822628d2653SJohn Baldwin 	PROC_LOCK(p);
823628d2653SJohn Baldwin 	ps = p->p_sigacts;
8246626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
825645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
826645682fdSLuoqi Chen 
827645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
8282c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
82979065dbaSBruce Evans 	signotify(p);
830628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
8312c42a146SMarcel Moolenaar 		/* void */;
832628d2653SJohn Baldwin 	PROC_UNLOCK(p);
833fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
8342c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
8352c42a146SMarcel Moolenaar 	return (EINTR);
8362c42a146SMarcel Moolenaar }
8372c42a146SMarcel Moolenaar 
83831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8392c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8402c42a146SMarcel Moolenaar struct osigsuspend_args {
8412c42a146SMarcel Moolenaar 	osigset_t mask;
8422c42a146SMarcel Moolenaar };
8432c42a146SMarcel Moolenaar #endif
844fb99ab88SMatthew Dillon /*
845fb99ab88SMatthew Dillon  * MPSAFE
846fb99ab88SMatthew Dillon  */
8472c42a146SMarcel Moolenaar /* ARGSUSED */
8482c42a146SMarcel Moolenaar int
849b40ce416SJulian Elischer osigsuspend(td, uap)
850b40ce416SJulian Elischer 	struct thread *td;
8512c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
8522c42a146SMarcel Moolenaar {
853b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
854645682fdSLuoqi Chen 	sigset_t mask;
855628d2653SJohn Baldwin 	register struct sigacts *ps;
8562c42a146SMarcel Moolenaar 
857fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
858628d2653SJohn Baldwin 	PROC_LOCK(p);
859628d2653SJohn Baldwin 	ps = p->p_sigacts;
8602c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
861645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
862645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
863645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
864645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
86579065dbaSBruce Evans 	signotify(p);
866628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
867df8bae1dSRodney W. Grimes 		/* void */;
868628d2653SJohn Baldwin 	PROC_UNLOCK(p);
869fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
870df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
871df8bae1dSRodney W. Grimes 	return (EINTR);
872df8bae1dSRodney W. Grimes }
87331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
874df8bae1dSRodney W. Grimes 
875df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
876d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
877df8bae1dSRodney W. Grimes struct osigstack_args {
878df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
879df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
880df8bae1dSRodney W. Grimes };
881d2d3e875SBruce Evans #endif
882fb99ab88SMatthew Dillon /*
883fb99ab88SMatthew Dillon  * MPSAFE
884fb99ab88SMatthew Dillon  */
885df8bae1dSRodney W. Grimes /* ARGSUSED */
88626f9a767SRodney W. Grimes int
887b40ce416SJulian Elischer osigstack(td, uap)
888b40ce416SJulian Elischer 	struct thread *td;
889df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
890df8bae1dSRodney W. Grimes {
891b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
892df8bae1dSRodney W. Grimes 	struct sigstack ss;
893fb99ab88SMatthew Dillon 	int error = 0;
894fb99ab88SMatthew Dillon 
895fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
896df8bae1dSRodney W. Grimes 
897d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
898628d2653SJohn Baldwin 		PROC_LOCK(p);
899645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
900b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
901628d2653SJohn Baldwin 		PROC_UNLOCK(p);
902d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
903d034d459SMarcel Moolenaar 		if (error)
904fb99ab88SMatthew Dillon 			goto done2;
905d034d459SMarcel Moolenaar 	}
906d034d459SMarcel Moolenaar 
907d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
908d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
909fb99ab88SMatthew Dillon 			goto done2;
910628d2653SJohn Baldwin 		PROC_LOCK(p);
911645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
912645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
913645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
914645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
915628d2653SJohn Baldwin 		PROC_UNLOCK(p);
916df8bae1dSRodney W. Grimes 	}
917fb99ab88SMatthew Dillon done2:
918fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
919fb99ab88SMatthew Dillon 	return (error);
920df8bae1dSRodney W. Grimes }
921df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
922df8bae1dSRodney W. Grimes 
923d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
924df8bae1dSRodney W. Grimes struct sigaltstack_args {
9252c42a146SMarcel Moolenaar 	stack_t	*ss;
9262c42a146SMarcel Moolenaar 	stack_t	*oss;
927df8bae1dSRodney W. Grimes };
928d2d3e875SBruce Evans #endif
929fb99ab88SMatthew Dillon /*
930fb99ab88SMatthew Dillon  * MPSAFE
931fb99ab88SMatthew Dillon  */
932df8bae1dSRodney W. Grimes /* ARGSUSED */
93326f9a767SRodney W. Grimes int
934b40ce416SJulian Elischer sigaltstack(td, uap)
935b40ce416SJulian Elischer 	struct thread *td;
936df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
937df8bae1dSRodney W. Grimes {
938b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9392c42a146SMarcel Moolenaar 	stack_t ss;
940fb99ab88SMatthew Dillon 	int oonstack;
941fb99ab88SMatthew Dillon 	int error = 0;
942fb99ab88SMatthew Dillon 
943fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
944df8bae1dSRodney W. Grimes 
945b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
946d034d459SMarcel Moolenaar 
947d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
948628d2653SJohn Baldwin 		PROC_LOCK(p);
949d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
950d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
951d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
952628d2653SJohn Baldwin 		PROC_UNLOCK(p);
953d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
954fb99ab88SMatthew Dillon 			goto done2;
955df8bae1dSRodney W. Grimes 	}
956d034d459SMarcel Moolenaar 
957d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
958fb99ab88SMatthew Dillon 		if (oonstack) {
959fb99ab88SMatthew Dillon 			error = EPERM;
960fb99ab88SMatthew Dillon 			goto done2;
961fb99ab88SMatthew Dillon 		}
962d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
963fb99ab88SMatthew Dillon 			goto done2;
964fb99ab88SMatthew Dillon 		if ((ss.ss_flags & ~SS_DISABLE) != 0) {
965fb99ab88SMatthew Dillon 			error = EINVAL;
966fb99ab88SMatthew Dillon 			goto done2;
967fb99ab88SMatthew Dillon 		}
968d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
969fb99ab88SMatthew Dillon 			if (ss.ss_size < p->p_sysent->sv_minsigstksz) {
970fb99ab88SMatthew Dillon 				error = ENOMEM;
971fb99ab88SMatthew Dillon 				goto done2;
972fb99ab88SMatthew Dillon 			}
973628d2653SJohn Baldwin 			PROC_LOCK(p);
974645682fdSLuoqi Chen 			p->p_sigstk = ss;
975d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
976628d2653SJohn Baldwin 			PROC_UNLOCK(p);
977628d2653SJohn Baldwin 		} else {
978628d2653SJohn Baldwin 			PROC_LOCK(p);
979d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
980628d2653SJohn Baldwin 			PROC_UNLOCK(p);
981628d2653SJohn Baldwin 		}
982d034d459SMarcel Moolenaar 	}
983fb99ab88SMatthew Dillon done2:
984fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
985fb99ab88SMatthew Dillon 	return (error);
986df8bae1dSRodney W. Grimes }
987df8bae1dSRodney W. Grimes 
988d93f860cSPoul-Henning Kamp /*
989d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
990d93f860cSPoul-Henning Kamp  * cp is calling process.
991d93f860cSPoul-Henning Kamp  */
992d93f860cSPoul-Henning Kamp int
9939c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
9949c1ab3e0SJohn Baldwin 	register struct thread *td;
9952c42a146SMarcel Moolenaar 	int sig, pgid, all;
996d93f860cSPoul-Henning Kamp {
997d93f860cSPoul-Henning Kamp 	register struct proc *p;
998d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
999d93f860cSPoul-Henning Kamp 	int nfound = 0;
1000d93f860cSPoul-Henning Kamp 
1001553629ebSJake Burkholder 	if (all) {
1002d93f860cSPoul-Henning Kamp 		/*
1003d93f860cSPoul-Henning Kamp 		 * broadcast
1004d93f860cSPoul-Henning Kamp 		 */
10051005a129SJohn Baldwin 		sx_slock(&allproc_lock);
10062e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1007628d2653SJohn Baldwin 			PROC_LOCK(p);
10089c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
10099c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1010628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1011628d2653SJohn Baldwin 				continue;
1012628d2653SJohn Baldwin 			}
10139c1ab3e0SJohn Baldwin 			if (p_cansignal(td->td_proc, p, sig) == 0) {
1014d93f860cSPoul-Henning Kamp 				nfound++;
101533a9ed9dSJohn Baldwin 				if (sig)
10162c42a146SMarcel Moolenaar 					psignal(p, sig);
1017628d2653SJohn Baldwin 			}
101833a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1019d93f860cSPoul-Henning Kamp 		}
10201005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1021553629ebSJake Burkholder 	} else {
1022ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1023f591779bSSeigo Tanimura 		if (pgid == 0) {
1024d93f860cSPoul-Henning Kamp 			/*
1025d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1026d93f860cSPoul-Henning Kamp 			 */
10279c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1028f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1029f591779bSSeigo Tanimura 		} else {
1030d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1031f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1032ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1033d93f860cSPoul-Henning Kamp 				return (ESRCH);
1034d93f860cSPoul-Henning Kamp 			}
1035f591779bSSeigo Tanimura 		}
1036ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
10372e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1038628d2653SJohn Baldwin 			PROC_LOCK(p);
1039628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1040628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1041628d2653SJohn Baldwin 				continue;
1042628d2653SJohn Baldwin 			}
1043628d2653SJohn Baldwin 			if (p->p_stat == SZOMB) {
104433a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1045628d2653SJohn Baldwin 				continue;
1046628d2653SJohn Baldwin 			}
10479c1ab3e0SJohn Baldwin 			if (p_cansignal(td->td_proc, p, sig) == 0) {
1048d93f860cSPoul-Henning Kamp 				nfound++;
104933a9ed9dSJohn Baldwin 				if (sig)
10502c42a146SMarcel Moolenaar 					psignal(p, sig);
1051628d2653SJohn Baldwin 			}
105233a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1053d93f860cSPoul-Henning Kamp 		}
1054f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1055d93f860cSPoul-Henning Kamp 	}
1056d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1057d93f860cSPoul-Henning Kamp }
1058d93f860cSPoul-Henning Kamp 
1059d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1060df8bae1dSRodney W. Grimes struct kill_args {
1061df8bae1dSRodney W. Grimes 	int	pid;
1062df8bae1dSRodney W. Grimes 	int	signum;
1063df8bae1dSRodney W. Grimes };
1064d2d3e875SBruce Evans #endif
1065fb99ab88SMatthew Dillon /*
1066fb99ab88SMatthew Dillon  * MPSAFE
1067fb99ab88SMatthew Dillon  */
1068df8bae1dSRodney W. Grimes /* ARGSUSED */
106926f9a767SRodney W. Grimes int
1070b40ce416SJulian Elischer kill(td, uap)
1071b40ce416SJulian Elischer 	register struct thread *td;
1072df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1073df8bae1dSRodney W. Grimes {
1074df8bae1dSRodney W. Grimes 	register struct proc *p;
1075fb99ab88SMatthew Dillon 	int error = 0;
1076df8bae1dSRodney W. Grimes 
10776c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1078df8bae1dSRodney W. Grimes 		return (EINVAL);
1079fb99ab88SMatthew Dillon 
1080fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1081df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1082df8bae1dSRodney W. Grimes 		/* kill single process */
1083fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1084fb99ab88SMatthew Dillon 			error = ESRCH;
10859c1ab3e0SJohn Baldwin 		} else if (p_cansignal(td->td_proc, p, uap->signum)) {
108633a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1087fb99ab88SMatthew Dillon 			error = EPERM;
1088fb99ab88SMatthew Dillon 		} else {
108933a9ed9dSJohn Baldwin 			if (uap->signum)
1090df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1091628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1092fb99ab88SMatthew Dillon 			error = 0;
1093df8bae1dSRodney W. Grimes 		}
1094fb99ab88SMatthew Dillon 	} else {
1095df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1096df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
10979c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 1);
1098fb99ab88SMatthew Dillon 			break;
1099df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
11009c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 0);
1101fb99ab88SMatthew Dillon 			break;
1102df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
11039c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, -uap->pid, 0);
1104fb99ab88SMatthew Dillon 			break;
1105df8bae1dSRodney W. Grimes 		}
1106fb99ab88SMatthew Dillon 	}
1107fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1108fb99ab88SMatthew Dillon 	return(error);
1109df8bae1dSRodney W. Grimes }
1110df8bae1dSRodney W. Grimes 
1111df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1112d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1113df8bae1dSRodney W. Grimes struct okillpg_args {
1114df8bae1dSRodney W. Grimes 	int	pgid;
1115df8bae1dSRodney W. Grimes 	int	signum;
1116df8bae1dSRodney W. Grimes };
1117d2d3e875SBruce Evans #endif
1118fb99ab88SMatthew Dillon /*
1119fb99ab88SMatthew Dillon  * MPSAFE
1120fb99ab88SMatthew Dillon  */
1121df8bae1dSRodney W. Grimes /* ARGSUSED */
112226f9a767SRodney W. Grimes int
1123b40ce416SJulian Elischer okillpg(td, uap)
1124b40ce416SJulian Elischer 	struct thread *td;
1125df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1126df8bae1dSRodney W. Grimes {
1127fb99ab88SMatthew Dillon 	int error;
1128df8bae1dSRodney W. Grimes 
11296c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1130df8bae1dSRodney W. Grimes 		return (EINVAL);
1131fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
11329c1ab3e0SJohn Baldwin 	error = killpg1(td, uap->signum, uap->pgid, 0);
1133fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1134fb99ab88SMatthew Dillon 	return (error);
1135df8bae1dSRodney W. Grimes }
1136df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1137df8bae1dSRodney W. Grimes 
1138df8bae1dSRodney W. Grimes /*
1139df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1140df8bae1dSRodney W. Grimes  */
1141df8bae1dSRodney W. Grimes void
11422c42a146SMarcel Moolenaar gsignal(pgid, sig)
11432c42a146SMarcel Moolenaar 	int pgid, sig;
1144df8bae1dSRodney W. Grimes {
1145df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1146df8bae1dSRodney W. Grimes 
1147f591779bSSeigo Tanimura 	if (pgid != 0) {
1148ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1149f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1150ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1151f591779bSSeigo Tanimura 		if (pgrp != NULL) {
11522c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1153f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1154f591779bSSeigo Tanimura 		}
1155f591779bSSeigo Tanimura 	}
1156df8bae1dSRodney W. Grimes }
1157df8bae1dSRodney W. Grimes 
1158df8bae1dSRodney W. Grimes /*
1159df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1160df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1161df8bae1dSRodney W. Grimes  */
1162df8bae1dSRodney W. Grimes void
11632c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1164df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
11652c42a146SMarcel Moolenaar 	int sig, checkctty;
1166df8bae1dSRodney W. Grimes {
1167df8bae1dSRodney W. Grimes 	register struct proc *p;
1168df8bae1dSRodney W. Grimes 
1169628d2653SJohn Baldwin 	if (pgrp) {
1170f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1171628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1172628d2653SJohn Baldwin 			PROC_LOCK(p);
1173df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
11742c42a146SMarcel Moolenaar 				psignal(p, sig);
1175628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1176628d2653SJohn Baldwin 		}
1177628d2653SJohn Baldwin 	}
1178df8bae1dSRodney W. Grimes }
1179df8bae1dSRodney W. Grimes 
1180df8bae1dSRodney W. Grimes /*
1181df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1182df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1183df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1184356861dbSMatthew Dillon  *
1185356861dbSMatthew Dillon  * MPSAFE
1186df8bae1dSRodney W. Grimes  */
1187df8bae1dSRodney W. Grimes void
11882c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1189df8bae1dSRodney W. Grimes 	struct proc *p;
11902c42a146SMarcel Moolenaar 	register int sig;
11918674077aSJeffrey Hsu 	u_long code;
1192df8bae1dSRodney W. Grimes {
1193df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1194df8bae1dSRodney W. Grimes 
1195356861dbSMatthew Dillon 	mtx_lock(&Giant);
1196628d2653SJohn Baldwin 	PROC_LOCK(p);
11972c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1198d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1199df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1200df8bae1dSRodney W. Grimes #ifdef KTRACE
1201df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
12022c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
12032c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1204df8bae1dSRodney W. Grimes #endif
12052c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
12062c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
12072c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
12082c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
12092c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
12102c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1211289ccde0SPeter Wemm 			/*
12122c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1213289ccde0SPeter Wemm 			 */
12142c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
12152c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
12162c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
12172c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
12182c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1219dedc04feSPeter Wemm 		}
12206f841fb7SMarcel Moolenaar 	} else {
12216626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
12222c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
12232c42a146SMarcel Moolenaar 		psignal(p, sig);
1224df8bae1dSRodney W. Grimes 	}
1225628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1226356861dbSMatthew Dillon 	mtx_unlock(&Giant);
1227df8bae1dSRodney W. Grimes }
1228df8bae1dSRodney W. Grimes 
1229df8bae1dSRodney W. Grimes /*
1230df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1231df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1232df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1233df8bae1dSRodney W. Grimes  *
1234df8bae1dSRodney W. Grimes  * Exceptions:
1235df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1236df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1237df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1238df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1239df8bae1dSRodney W. Grimes  *
1240df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1241df8bae1dSRodney W. Grimes  */
1242df8bae1dSRodney W. Grimes void
12432c42a146SMarcel Moolenaar psignal(p, sig)
1244df8bae1dSRodney W. Grimes 	register struct proc *p;
12452c42a146SMarcel Moolenaar 	register int sig;
1246df8bae1dSRodney W. Grimes {
124740447cd4SJohn Baldwin 	register int prop;
1248df8bae1dSRodney W. Grimes 	register sig_t action;
1249b40ce416SJulian Elischer 	struct thread *td;
12500f5c7c4bSPoul-Henning Kamp #ifdef SMP
1251b40ce416SJulian Elischer 	struct ksegrp *kg;
12520f5c7c4bSPoul-Henning Kamp #endif
1253df8bae1dSRodney W. Grimes 
12542899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
12552899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
12562c42a146SMarcel Moolenaar 
1257628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1258cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1259cb679c38SJonathan Lemon 
12602c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1261df8bae1dSRodney W. Grimes 
1262df8bae1dSRodney W. Grimes 	/*
12632a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
12642a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
12652a024a2bSSean Eric Fagan 	 * a chance, as well.
1266df8bae1dSRodney W. Grimes 	 */
1267b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1268df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1269b40ce416SJulian Elischer 	} else {
1270df8bae1dSRodney W. Grimes 		/*
1271df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1272df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1273df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1274df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1275df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1276df8bae1dSRodney W. Grimes 		 */
1277628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1278df8bae1dSRodney W. Grimes 			return;
12792c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1280df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
12812c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1282df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1283df8bae1dSRodney W. Grimes 		else
1284df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1285df8bae1dSRodney W. Grimes 	}
1286df8bae1dSRodney W. Grimes 
1287b40ce416SJulian Elischer 	/*
1288b40ce416SJulian Elischer 	 * bring the priority of a process up if we want it to get
1289b40ce416SJulian Elischer 	 * killed in this lifetime.
1290b40ce416SJulian Elischer 	 * XXXKSE think if a better way to do this.
1291b40ce416SJulian Elischer 	 *
1292b40ce416SJulian Elischer 	 * What we need to do is see if there is a thread that will
1293b40ce416SJulian Elischer 	 * be able to accept the signal. e.g.
1294b40ce416SJulian Elischer 	 * FOREACH_THREAD_IN_PROC() {
1295b40ce416SJulian Elischer 	 *	if runnable, we're done
1296b40ce416SJulian Elischer 	 *	else pick one at random.
1297b40ce416SJulian Elischer 	 * }
1298b40ce416SJulian Elischer 	 */
1299b40ce416SJulian Elischer 	/* XXXKSE
1300b40ce416SJulian Elischer 	 * For now there is one thread per proc.
1301b40ce416SJulian Elischer 	 * Effectively select one sucker thread..
1302b40ce416SJulian Elischer 	 */
1303079b7badSJulian Elischer 	td = FIRST_THREAD_IN_PROC(p);
13049ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1305b40ce416SJulian Elischer 	if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) &&
1306b40ce416SJulian Elischer 	    (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0))
1307b40ce416SJulian Elischer 		p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */
13089ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1309df8bae1dSRodney W. Grimes 
1310df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
13112c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1312df8bae1dSRodney W. Grimes 
1313df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1314df8bae1dSRodney W. Grimes 		/*
1315df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1316df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1317df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1318df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1319df8bae1dSRodney W. Grimes 		 */
1320df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1321628d2653SJohn Baldwin 		    action == SIG_DFL)
1322df8bae1dSRodney W. Grimes 		        return;
13232c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1324df8bae1dSRodney W. Grimes 	}
13252c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
13269c1ab3e0SJohn Baldwin 	mtx_lock_spin(&sched_lock);
132779065dbaSBruce Evans 	signotify(p);
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes 	/*
1330df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1331df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1332df8bae1dSRodney W. Grimes 	 */
13331c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
13349ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1335df8bae1dSRodney W. Grimes 		return;
13361c32c37cSJohn Baldwin 	}
1337b40ce416SJulian Elischer 
1338df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1339df8bae1dSRodney W. Grimes 
1340df8bae1dSRodney W. Grimes 	case SSLEEP:
1341df8bae1dSRodney W. Grimes 		/*
1342df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1343df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1344df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1345df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1346df8bae1dSRodney W. Grimes 		 */
13479c1ab3e0SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
1348df8bae1dSRodney W. Grimes 			goto out;
1349df8bae1dSRodney W. Grimes 		/*
1350df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1351df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1352df8bae1dSRodney W. Grimes 		 * for the parent.
1353df8bae1dSRodney W. Grimes 		 */
1354df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1355df8bae1dSRodney W. Grimes 			goto run;
1356df8bae1dSRodney W. Grimes 		/*
1357df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1358df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1359df8bae1dSRodney W. Grimes 		 * be awakened.
1360df8bae1dSRodney W. Grimes 		 */
1361df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
13622c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1363df8bae1dSRodney W. Grimes 			goto out;
1364df8bae1dSRodney W. Grimes 		}
1365df8bae1dSRodney W. Grimes 		/*
1366df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1367df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1368df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1369df8bae1dSRodney W. Grimes 		 * cause the process to run.
1370df8bae1dSRodney W. Grimes 		 */
1371df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
13723e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1373df8bae1dSRodney W. Grimes 				goto runfast;
1374df8bae1dSRodney W. Grimes 			/*
1375df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1376df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1377df8bae1dSRodney W. Grimes 			 */
1378df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1379df8bae1dSRodney W. Grimes 				goto out;
13809c1ab3e0SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
13812c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
13822c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1383628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1384f591779bSSeigo Tanimura 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1385df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1386628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
13875b3047d5SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1388df8bae1dSRodney W. Grimes 			stop(p);
1389df8bae1dSRodney W. Grimes 			goto out;
13903e6831f5SJohn Baldwin 		} else
1391df8bae1dSRodney W. Grimes 			goto runfast;
1392df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1393df8bae1dSRodney W. Grimes 
1394df8bae1dSRodney W. Grimes 	case SSTOP:
1395df8bae1dSRodney W. Grimes 		/*
1396df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1397df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1398df8bae1dSRodney W. Grimes 		 */
139940447cd4SJohn Baldwin 		if (p->p_flag & P_TRACED)
1400df8bae1dSRodney W. Grimes 			goto out;
1401df8bae1dSRodney W. Grimes 
1402df8bae1dSRodney W. Grimes 		/*
1403df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1404df8bae1dSRodney W. Grimes 		 */
14052c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1406df8bae1dSRodney W. Grimes 			goto runfast;
1407df8bae1dSRodney W. Grimes 
1408df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1409df8bae1dSRodney W. Grimes 			/*
1410df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1411df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1412df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1413df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1414df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1415df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1416df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1417df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1418df8bae1dSRodney W. Grimes 			 */
1419df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
14202c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1421df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1422df8bae1dSRodney W. Grimes 				goto runfast;
1423b40ce416SJulian Elischer 			/*
1424b40ce416SJulian Elischer 			 * XXXKSE
1425b40ce416SJulian Elischer 			 * do this for each thread.
1426b40ce416SJulian Elischer 			 */
1427b40ce416SJulian Elischer 			if (p->p_flag & P_KSES) {
1428b40ce416SJulian Elischer 				mtx_assert(&sched_lock,
1429b40ce416SJulian Elischer 				    MA_OWNED | MA_NOTRECURSED);
1430b40ce416SJulian Elischer 				FOREACH_THREAD_IN_PROC(p, td) {
1431b40ce416SJulian Elischer 					if (td->td_wchan == NULL) {
1432b40ce416SJulian Elischer 						setrunnable(td); /* XXXKSE */
1433b40ce416SJulian Elischer 					} else {
1434b40ce416SJulian Elischer 						/* mark it as sleeping */
1435b40ce416SJulian Elischer 					}
1436b40ce416SJulian Elischer 				}
1437b40ce416SJulian Elischer 			} else {
1438079b7badSJulian Elischer 				if (td->td_wchan == NULL)
1439df8bae1dSRodney W. Grimes 					goto run;
1440df8bae1dSRodney W. Grimes 				p->p_stat = SSLEEP;
1441b40ce416SJulian Elischer 			}
1442fdd4e5c6SJulian Elischer 			goto out;
1443df8bae1dSRodney W. Grimes 		}
1444df8bae1dSRodney W. Grimes 
1445df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1446df8bae1dSRodney W. Grimes 			/*
1447df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1448df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1449df8bae1dSRodney W. Grimes 			 */
14502c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1451df8bae1dSRodney W. Grimes 			goto out;
1452df8bae1dSRodney W. Grimes 		}
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes 		/*
1455df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1456df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1457df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1458df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1459b40ce416SJulian Elischer 		 * XXXKSE should we wake ALL blocked threads?
1460df8bae1dSRodney W. Grimes 		 */
1461b40ce416SJulian Elischer 		if (p->p_flag & P_KSES) {
1462b40ce416SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1463b40ce416SJulian Elischer 				if (td->td_wchan && (td->td_flags & TDF_SINTR)){
1464b40ce416SJulian Elischer 					if (td->td_flags & TDF_CVWAITQ)
1465b40ce416SJulian Elischer 						cv_waitq_remove(td);
1466238510fcSJason Evans 					else
1467b40ce416SJulian Elischer 						unsleep(td); /* XXXKSE */
1468b40ce416SJulian Elischer 				}
1469b40ce416SJulian Elischer 			}
1470b40ce416SJulian Elischer 		} else {
1471b40ce416SJulian Elischer 			if (td->td_wchan && td->td_flags & TDF_SINTR) {
1472b40ce416SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
1473b40ce416SJulian Elischer 					cv_waitq_remove(td);
1474b40ce416SJulian Elischer 				else
1475b40ce416SJulian Elischer 					unsleep(td); /* XXXKSE */
1476b40ce416SJulian Elischer 			}
1477238510fcSJason Evans 		}
1478df8bae1dSRodney W. Grimes 		goto out;
1479df8bae1dSRodney W. Grimes 
1480df8bae1dSRodney W. Grimes 	default:
1481df8bae1dSRodney W. Grimes 		/*
1482df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1483df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1484df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1485df8bae1dSRodney W. Grimes 		 */
1486d2ef4060SBruce Evans 		if (p->p_stat == SRUN) {
14873163861cSTor Egge #ifdef SMP
1488b40ce416SJulian Elischer 			struct kse *ke;
1489b40ce416SJulian Elischer 			struct thread *td = curthread;
1490b40ce416SJulian Elischer /* we should only deliver to one thread.. but which one? */
1491b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(p, kg) {
1492b40ce416SJulian Elischer 				FOREACH_KSE_IN_GROUP(kg, ke) {
1493b40ce416SJulian Elischer 					if (ke->ke_thread == td) {
1494b40ce416SJulian Elischer 						continue;
1495b40ce416SJulian Elischer 					}
1496b40ce416SJulian Elischer 					forward_signal(ke->ke_thread);
1497b40ce416SJulian Elischer 				}
1498b40ce416SJulian Elischer 			}
14993163861cSTor Egge #endif
15006caa8a15SJohn Baldwin 		}
1501df8bae1dSRodney W. Grimes 		goto out;
1502df8bae1dSRodney W. Grimes 	}
1503df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1504df8bae1dSRodney W. Grimes 
1505df8bae1dSRodney W. Grimes runfast:
1506df8bae1dSRodney W. Grimes 	/*
1507df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1508b40ce416SJulian Elischer 	 * XXXKSE Should we make them all run fast?
1509b40ce416SJulian Elischer 	 * Maybe just one would be enough?
1510df8bae1dSRodney W. Grimes 	 */
15112c100766SJulian Elischer 
15122c100766SJulian Elischer 	if (FIRST_THREAD_IN_PROC(p)->td_priority > PUSER) {
15132c100766SJulian Elischer 		FIRST_THREAD_IN_PROC(p)->td_priority = PUSER;
1514b40ce416SJulian Elischer 	}
1515df8bae1dSRodney W. Grimes run:
15163e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
15173e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1518b40ce416SJulian Elischer 	setrunnable(td); /* XXXKSE */
15193e6831f5SJohn Baldwin out:
15209c1ab3e0SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
15219c1ab3e0SJohn Baldwin 
15229c1ab3e0SJohn Baldwin 	/* Once we get here, sched_lock should not be owned. */
15233e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1524df8bae1dSRodney W. Grimes }
1525df8bae1dSRodney W. Grimes 
1526df8bae1dSRodney W. Grimes /*
1527df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1528df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1529df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1530df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1531df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1532df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1533df8bae1dSRodney W. Grimes  * sequence is
1534df8bae1dSRodney W. Grimes  *
15352c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
15362c42a146SMarcel Moolenaar  *		postsig(sig);
1537df8bae1dSRodney W. Grimes  */
153826f9a767SRodney W. Grimes int
1539df8bae1dSRodney W. Grimes issignal(p)
1540df8bae1dSRodney W. Grimes 	register struct proc *p;
1541df8bae1dSRodney W. Grimes {
15422c42a146SMarcel Moolenaar 	sigset_t mask;
15432c42a146SMarcel Moolenaar 	register int sig, prop;
1544df8bae1dSRodney W. Grimes 
1545628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1546df8bae1dSRodney W. Grimes 	for (;;) {
15472a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
15482a024a2bSSean Eric Fagan 
15492c42a146SMarcel Moolenaar 		mask = p->p_siglist;
15502c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1551df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
15522c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
15532c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1554df8bae1dSRodney W. Grimes 			return (0);
15552c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
15562c42a146SMarcel Moolenaar 		prop = sigprop(sig);
15572a024a2bSSean Eric Fagan 
1558628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
15592a024a2bSSean Eric Fagan 
1560df8bae1dSRodney W. Grimes 		/*
1561df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1562df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1563df8bae1dSRodney W. Grimes 		 */
15642c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
15652c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1566df8bae1dSRodney W. Grimes 			continue;
1567df8bae1dSRodney W. Grimes 		}
1568df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1569df8bae1dSRodney W. Grimes 			/*
1570df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1571df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1572df8bae1dSRodney W. Grimes 			 */
15732c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1574628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1575df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1576628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
1577df8bae1dSRodney W. Grimes 			do {
15789ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
15795b3047d5SJohn Baldwin 				stop(p);
1580c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1581c86b6ff5SJohn Baldwin 				DROP_GIANT();
15822ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1583df8bae1dSRodney W. Grimes 				mi_switch();
15849ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
158520cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1586628d2653SJohn Baldwin 				PROC_LOCK(p);
15872a024a2bSSean Eric Fagan 			} while (!trace_req(p)
15882a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1589df8bae1dSRodney W. Grimes 
1590df8bae1dSRodney W. Grimes 			/*
1591df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1592df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1593df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1594df8bae1dSRodney W. Grimes 			 */
1595df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1596df8bae1dSRodney W. Grimes 				continue;
1597df8bae1dSRodney W. Grimes 
1598df8bae1dSRodney W. Grimes 			/*
1599df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1600df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1601df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1602df8bae1dSRodney W. Grimes 			 */
16032c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
16042c42a146SMarcel Moolenaar 			sig = p->p_xstat;
16052c42a146SMarcel Moolenaar 			if (sig == 0)
1606df8bae1dSRodney W. Grimes 				continue;
1607df8bae1dSRodney W. Grimes 
1608df8bae1dSRodney W. Grimes 			/*
1609df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1610df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1611df8bae1dSRodney W. Grimes 			 */
16122c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
16132c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1614df8bae1dSRodney W. Grimes 				continue;
1615df8bae1dSRodney W. Grimes 		}
1616df8bae1dSRodney W. Grimes 
1617df8bae1dSRodney W. Grimes 		/*
1618df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1619df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1620df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1621df8bae1dSRodney W. Grimes 		 */
16222c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1623df8bae1dSRodney W. Grimes 
16240b53fbe8SBruce Evans 		case (int)SIG_DFL:
1625df8bae1dSRodney W. Grimes 			/*
1626df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1627df8bae1dSRodney W. Grimes 			 */
1628df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1629df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1630df8bae1dSRodney W. Grimes 				/*
1631df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1632df8bae1dSRodney W. Grimes 				 * in init? XXX
1633df8bae1dSRodney W. Grimes 				 */
1634d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
16352c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1636df8bae1dSRodney W. Grimes #endif
1637df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1638df8bae1dSRodney W. Grimes 			}
1639df8bae1dSRodney W. Grimes 			/*
1640df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1641df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1642df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1643df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1644df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1645df8bae1dSRodney W. Grimes 			 */
1646df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1647df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1648df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1649df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
1650df8bae1dSRodney W. Grimes 					break;	/* == ignore */
16512c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1652628d2653SJohn Baldwin 				PROC_LOCK(p->p_pptr);
1653f591779bSSeigo Tanimura 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1654df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1655628d2653SJohn Baldwin 				PROC_UNLOCK(p->p_pptr);
16569ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
16575b3047d5SJohn Baldwin 				stop(p);
1658c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1659c86b6ff5SJohn Baldwin 				DROP_GIANT();
16602ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1661df8bae1dSRodney W. Grimes 				mi_switch();
16629ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
166320cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1664628d2653SJohn Baldwin 				PROC_LOCK(p);
1665df8bae1dSRodney W. Grimes 				break;
1666df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1667df8bae1dSRodney W. Grimes 				/*
1668df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1669df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1670df8bae1dSRodney W. Grimes 				 */
1671df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1672df8bae1dSRodney W. Grimes 			} else
16732c42a146SMarcel Moolenaar 				return (sig);
1674df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1675df8bae1dSRodney W. Grimes 
16760b53fbe8SBruce Evans 		case (int)SIG_IGN:
1677df8bae1dSRodney W. Grimes 			/*
1678df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1679df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1680df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1681df8bae1dSRodney W. Grimes 			 */
1682df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1683df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1684df8bae1dSRodney W. Grimes 				printf("issignal\n");
1685df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1686df8bae1dSRodney W. Grimes 
1687df8bae1dSRodney W. Grimes 		default:
1688df8bae1dSRodney W. Grimes 			/*
1689df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1690df8bae1dSRodney W. Grimes 			 * postsig() process it.
1691df8bae1dSRodney W. Grimes 			 */
16922c42a146SMarcel Moolenaar 			return (sig);
1693df8bae1dSRodney W. Grimes 		}
16942c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1695df8bae1dSRodney W. Grimes 	}
1696df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1697df8bae1dSRodney W. Grimes }
1698df8bae1dSRodney W. Grimes 
1699df8bae1dSRodney W. Grimes /*
1700df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1701df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
17025b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
17035b3047d5SJohn Baldwin  * lock held.
1704df8bae1dSRodney W. Grimes  */
17055b3047d5SJohn Baldwin static void
1706df8bae1dSRodney W. Grimes stop(p)
1707df8bae1dSRodney W. Grimes 	register struct proc *p;
1708df8bae1dSRodney W. Grimes {
1709df8bae1dSRodney W. Grimes 
1710628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
17115b3047d5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
1712df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1713df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1714df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1715df8bae1dSRodney W. Grimes }
1716df8bae1dSRodney W. Grimes 
1717df8bae1dSRodney W. Grimes /*
1718df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1719df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1720df8bae1dSRodney W. Grimes  */
1721df8bae1dSRodney W. Grimes void
17222c42a146SMarcel Moolenaar postsig(sig)
17232c42a146SMarcel Moolenaar 	register int sig;
1724df8bae1dSRodney W. Grimes {
1725b40ce416SJulian Elischer 	struct thread *td = curthread;
1726b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1727628d2653SJohn Baldwin 	struct sigacts *ps;
17282c42a146SMarcel Moolenaar 	sig_t action;
17292c42a146SMarcel Moolenaar 	sigset_t returnmask;
17302c42a146SMarcel Moolenaar 	int code;
1731df8bae1dSRodney W. Grimes 
17322c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
17335526d2d9SEivind Eklund 
17342ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1735628d2653SJohn Baldwin 	ps = p->p_sigacts;
17362c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
17372c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1738df8bae1dSRodney W. Grimes #ifdef KTRACE
1739df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1740645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
17412c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1742df8bae1dSRodney W. Grimes #endif
1743628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
17442a024a2bSSean Eric Fagan 
1745df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1746df8bae1dSRodney W. Grimes 		/*
1747df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1748df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1749df8bae1dSRodney W. Grimes 		 */
1750b40ce416SJulian Elischer 		sigexit(td, sig);
1751df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1752df8bae1dSRodney W. Grimes 	} else {
1753df8bae1dSRodney W. Grimes 		/*
1754df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1755df8bae1dSRodney W. Grimes 		 */
17562c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
17575526d2d9SEivind Eklund 		    ("postsig action"));
1758df8bae1dSRodney W. Grimes 		/*
1759df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1760645682fdSLuoqi Chen 		 * occurrences of this signal.
1761df8bae1dSRodney W. Grimes 		 *
1762645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1763df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1764645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1765df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1766df8bae1dSRodney W. Grimes 		 */
1767645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
17686626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1769645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1770df8bae1dSRodney W. Grimes 		} else
1771df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
17722c42a146SMarcel Moolenaar 
17732c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
17742c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
17752c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
17762c42a146SMarcel Moolenaar 
17772c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1778289ccde0SPeter Wemm 			/*
17792c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1780289ccde0SPeter Wemm 			 */
17812c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
17822c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
17832c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
17842c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
17852c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1786dedc04feSPeter Wemm 		}
1787df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
17882c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1789df8bae1dSRodney W. Grimes 			code = 0;
1790df8bae1dSRodney W. Grimes 		} else {
17916626c604SJulian Elischer 			code = p->p_code;
17926626c604SJulian Elischer 			p->p_code = 0;
17936626c604SJulian Elischer 			p->p_sig = 0;
1794df8bae1dSRodney W. Grimes 		}
17952c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1796df8bae1dSRodney W. Grimes 	}
1797df8bae1dSRodney W. Grimes }
1798df8bae1dSRodney W. Grimes 
1799df8bae1dSRodney W. Grimes /*
1800df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1801df8bae1dSRodney W. Grimes  */
180226f9a767SRodney W. Grimes void
1803df8bae1dSRodney W. Grimes killproc(p, why)
1804df8bae1dSRodney W. Grimes 	struct proc *p;
1805df8bae1dSRodney W. Grimes 	char *why;
1806df8bae1dSRodney W. Grimes {
18079081e5e8SJohn Baldwin 
18089081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18090384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
18100384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1811729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1812b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1813df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1814df8bae1dSRodney W. Grimes }
1815df8bae1dSRodney W. Grimes 
1816df8bae1dSRodney W. Grimes /*
1817df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1818df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1819df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1820df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1821df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1822df8bae1dSRodney W. Grimes  * does not return.
1823df8bae1dSRodney W. Grimes  */
182426f9a767SRodney W. Grimes void
1825b40ce416SJulian Elischer sigexit(td, sig)
1826b40ce416SJulian Elischer 	struct thread *td;
18272c42a146SMarcel Moolenaar 	int sig;
1828df8bae1dSRodney W. Grimes {
1829b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1830df8bae1dSRodney W. Grimes 
1831628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1832df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
18332c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
18342c42a146SMarcel Moolenaar 		p->p_sig = sig;
1835c364e17eSAndrey A. Chernov 		/*
1836c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1837c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1838c364e17eSAndrey A. Chernov 		 * these messages.)
1839c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1840c364e17eSAndrey A. Chernov 		 */
1841628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1842c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1843c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1844b40ce416SJulian Elischer 		if (coredump(td) == 0)
18452c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
184657308494SJoerg Wunsch 		if (kern_logsigexit)
184757308494SJoerg Wunsch 			log(LOG_INFO,
184857308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
18493d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
18509c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
18512c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
18522c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1853c31146a1SJohn Baldwin 	} else {
1854628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1855628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1856628d2653SJohn Baldwin 			mtx_lock(&Giant);
1857c31146a1SJohn Baldwin 	}
1858b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1859df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1860df8bae1dSRodney W. Grimes }
1861df8bae1dSRodney W. Grimes 
1862c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1863c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1864c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1865c5edb423SSean Eric Fagan 
1866c5edb423SSean Eric Fagan /*
1867c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1868c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1869c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1870c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1871c5edb423SSean Eric Fagan  *	%P	process id (pid)
1872c5edb423SSean Eric Fagan  *	%U	user id (uid)
1873c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1874c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1875c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1876c5edb423SSean Eric Fagan  */
1877c5edb423SSean Eric Fagan 
1878fca666a1SJulian Elischer static char *
1879c5edb423SSean Eric Fagan expand_name(name, uid, pid)
188087f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1881c5edb423SSean Eric Fagan 	char *temp;
1882c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1883c5edb423SSean Eric Fagan 	int i, n;
1884c5edb423SSean Eric Fagan 	char *format = corefilename;
1885ce38ca0fSAlfred Perlstein 	size_t namelen;
1886c5edb423SSean Eric Fagan 
1887ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
18880bfe2990SEivind Eklund 	if (temp == NULL)
18890bfe2990SEivind Eklund 		return NULL;
1890ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1891ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1892c5edb423SSean Eric Fagan 		int l;
1893c5edb423SSean Eric Fagan 		switch (format[i]) {
1894c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1895c5edb423SSean Eric Fagan 			i++;
1896c5edb423SSean Eric Fagan 			switch (format[i]) {
1897c5edb423SSean Eric Fagan 			case '%':
1898c5edb423SSean Eric Fagan 				temp[n++] = '%';
1899c5edb423SSean Eric Fagan 				break;
1900c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1901ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
190287f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1903c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1904c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1905c5edb423SSean Eric Fagan 					return NULL;
1906c5edb423SSean Eric Fagan 				}
1907ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1908ce38ca0fSAlfred Perlstein 				n += namelen;
1909c5edb423SSean Eric Fagan 				break;
1910c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1911ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1912c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
191387f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1914c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1915c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1916c5edb423SSean Eric Fagan 					return NULL;
1917c5edb423SSean Eric Fagan 				}
1918c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1919c5edb423SSean Eric Fagan 				n += l;
1920c5edb423SSean Eric Fagan 				break;
1921c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1922ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1923c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
192487f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1925c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1926c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1927c5edb423SSean Eric Fagan 					return NULL;
1928c5edb423SSean Eric Fagan 				}
1929c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1930c5edb423SSean Eric Fagan 				n += l;
1931c5edb423SSean Eric Fagan 				break;
1932c5edb423SSean Eric Fagan 			default:
1933c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1934c5edb423SSean Eric Fagan 			}
1935c5edb423SSean Eric Fagan 			break;
1936c5edb423SSean Eric Fagan 		default:
1937c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1938c5edb423SSean Eric Fagan 		}
1939c5edb423SSean Eric Fagan 	}
1940ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1941c5edb423SSean Eric Fagan 	return temp;
1942c5edb423SSean Eric Fagan }
1943c5edb423SSean Eric Fagan 
1944df8bae1dSRodney W. Grimes /*
1945fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1946fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1947fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1948fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1949fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
19505da271f5SRobert Watson  *
19515da271f5SRobert Watson  * XXX: VOP_GETATTR() here requires holding the vnode lock.
1952fca666a1SJulian Elischer  */
1953fca666a1SJulian Elischer 
1954fca666a1SJulian Elischer static int
1955b40ce416SJulian Elischer coredump(struct thread *td)
1956fca666a1SJulian Elischer {
1957b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1958fca666a1SJulian Elischer 	register struct vnode *vp;
19599c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
196006ae1e91SMatthew Dillon 	struct flock lf;
1961fca666a1SJulian Elischer 	struct nameidata nd;
1962fca666a1SJulian Elischer 	struct vattr vattr;
1963e6796b67SKirk McKusick 	int error, error1, flags;
1964f2a2857bSKirk McKusick 	struct mount *mp;
1965fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1966fca666a1SJulian Elischer 	off_t limit;
1967fca666a1SJulian Elischer 
1968628d2653SJohn Baldwin 	PROC_LOCK(p);
1969628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1970fca666a1SJulian Elischer 
1971628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1972628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1973fca666a1SJulian Elischer 		return (EFAULT);
1974628d2653SJohn Baldwin 	}
1975fca666a1SJulian Elischer 
1976fca666a1SJulian Elischer 	/*
197735a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
197835a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
197935a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
198035a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
198135a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
198235a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1983fca666a1SJulian Elischer 	 */
198435a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1985628d2653SJohn Baldwin 	if (limit == 0) {
1986628d2653SJohn Baldwin 		PROC_UNLOCK(p);
198735a2598fSSean Eric Fagan 		return 0;
1988628d2653SJohn Baldwin 	}
1989628d2653SJohn Baldwin 	PROC_UNLOCK(p);
199035a2598fSSean Eric Fagan 
1991f2a2857bSKirk McKusick restart:
19929c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
1993ccdbd10cSPeter Pentchev 	if (name == NULL)
1994ccdbd10cSPeter Pentchev 		return (EINVAL);
1995b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
1996e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1997e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1998fca666a1SJulian Elischer 	free(name, M_TEMP);
1999fca666a1SJulian Elischer 	if (error)
2000fca666a1SJulian Elischer 		return (error);
2001762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2002fca666a1SJulian Elischer 	vp = nd.ni_vp;
200306ae1e91SMatthew Dillon 
2004b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
200506ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
200606ae1e91SMatthew Dillon 	lf.l_start = 0;
200706ae1e91SMatthew Dillon 	lf.l_len = 0;
200806ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
200906ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
201006ae1e91SMatthew Dillon 	if (error)
201106ae1e91SMatthew Dillon 		goto out2;
201206ae1e91SMatthew Dillon 
201306ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
201406ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
201506ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2016b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2017f2a2857bSKirk McKusick 			return (error);
2018f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2019f2a2857bSKirk McKusick 			return (error);
2020f2a2857bSKirk McKusick 		goto restart;
2021f2a2857bSKirk McKusick 	}
2022fca666a1SJulian Elischer 
2023fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
2024fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
2025b40ce416SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2026fca666a1SJulian Elischer 		error = EFAULT;
202706ae1e91SMatthew Dillon 		goto out1;
2028fca666a1SJulian Elischer 	}
2029fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2030fca666a1SJulian Elischer 	vattr.va_size = 0;
203188b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2032b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2033b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
203488b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2035628d2653SJohn Baldwin 	PROC_LOCK(p);
2036fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2037628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2038fca666a1SJulian Elischer 
2039fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2040b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2041fca666a1SJulian Elischer 	  ENOSYS;
2042fca666a1SJulian Elischer 
204306ae1e91SMatthew Dillon out1:
204406ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
204506ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2046f2a2857bSKirk McKusick 	vn_finished_write(mp);
204706ae1e91SMatthew Dillon out2:
2048b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2049fca666a1SJulian Elischer 	if (error == 0)
2050fca666a1SJulian Elischer 		error = error1;
2051fca666a1SJulian Elischer 	return (error);
2052fca666a1SJulian Elischer }
2053fca666a1SJulian Elischer 
2054fca666a1SJulian Elischer /*
2055df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2056df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2057df8bae1dSRodney W. Grimes  */
2058d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2059df8bae1dSRodney W. Grimes struct nosys_args {
2060df8bae1dSRodney W. Grimes 	int	dummy;
2061df8bae1dSRodney W. Grimes };
2062d2d3e875SBruce Evans #endif
2063fb99ab88SMatthew Dillon /*
2064fb99ab88SMatthew Dillon  * MPSAFE
2065fb99ab88SMatthew Dillon  */
2066df8bae1dSRodney W. Grimes /* ARGSUSED */
206726f9a767SRodney W. Grimes int
2068b40ce416SJulian Elischer nosys(td, args)
2069b40ce416SJulian Elischer 	struct thread *td;
2070df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2071df8bae1dSRodney W. Grimes {
2072b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2073b40ce416SJulian Elischer 
2074fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2075628d2653SJohn Baldwin 	PROC_LOCK(p);
2076df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2077628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2078fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2079df8bae1dSRodney W. Grimes 	return (EINVAL);
2080df8bae1dSRodney W. Grimes }
2081831d27a9SDon Lewis 
2082831d27a9SDon Lewis /*
208348f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2084831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2085831d27a9SDon Lewis  */
2086831d27a9SDon Lewis void
20872c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
2088831d27a9SDon Lewis 	struct sigio *sigio;
20892c42a146SMarcel Moolenaar 	int sig, checkctty;
2090831d27a9SDon Lewis {
2091831d27a9SDon Lewis 	if (sigio == NULL)
2092831d27a9SDon Lewis 		return;
2093831d27a9SDon Lewis 
2094831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2095628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
20962b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
20972c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2098628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2099831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2100831d27a9SDon Lewis 		struct proc *p;
2101831d27a9SDon Lewis 
2102f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2103628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2104628d2653SJohn Baldwin 			PROC_LOCK(p);
21052b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2106831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
21072c42a146SMarcel Moolenaar 				psignal(p, sig);
2108628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2109628d2653SJohn Baldwin 		}
2110f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2111831d27a9SDon Lewis 	}
2112831d27a9SDon Lewis }
2113cb679c38SJonathan Lemon 
2114cb679c38SJonathan Lemon static int
2115cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2116cb679c38SJonathan Lemon {
2117cb679c38SJonathan Lemon 	struct proc *p = curproc;
2118cb679c38SJonathan Lemon 
2119cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2120cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2121cb679c38SJonathan Lemon 
2122628d2653SJohn Baldwin 	PROC_LOCK(p);
2123cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2124628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2125cb679c38SJonathan Lemon 
2126cb679c38SJonathan Lemon 	return (0);
2127cb679c38SJonathan Lemon }
2128cb679c38SJonathan Lemon 
2129cb679c38SJonathan Lemon static void
2130cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2131cb679c38SJonathan Lemon {
2132cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2133cb679c38SJonathan Lemon 
2134628d2653SJohn Baldwin 	PROC_LOCK(p);
2135e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2136628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2137cb679c38SJonathan Lemon }
2138cb679c38SJonathan Lemon 
2139cb679c38SJonathan Lemon /*
2140cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2141cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2142cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2143cb679c38SJonathan Lemon  * isn't worth the trouble.
2144cb679c38SJonathan Lemon  */
2145cb679c38SJonathan Lemon static int
2146cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2147cb679c38SJonathan Lemon {
2148cb679c38SJonathan Lemon 
2149cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2150cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2151cb679c38SJonathan Lemon 
2152cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2153cb679c38SJonathan Lemon 			kn->kn_data++;
2154cb679c38SJonathan Lemon 	}
2155cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2156cb679c38SJonathan Lemon }
2157