xref: /freebsd/sys/kern/kern_sig.c (revision 79065dba2a88baf780767cbb1bb31d4525a08528)
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);
834d77a549SAlfred Perlstein static int killpg1(struct proc *cp, 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_assert(&sched_lock, MA_NOTOWNED);
19479065dbaSBruce Evans 	mtx_lock_spin(&sched_lock);
19579065dbaSBruce Evans 	if (SIGPENDING(p)) {
19679065dbaSBruce Evans 		p->p_sflag |= PS_NEEDSIGCHK;
19779065dbaSBruce Evans 		p->p_kse.ke_flags |= KEF_ASTPENDING;	/* XXXKSE */
19879065dbaSBruce Evans 	}
19979065dbaSBruce Evans 	mtx_unlock_spin(&sched_lock);
20079065dbaSBruce Evans }
20179065dbaSBruce Evans 
2026f841fb7SMarcel Moolenaar static __inline int
2036f841fb7SMarcel Moolenaar sigprop(int sig)
2042c42a146SMarcel Moolenaar {
2056f841fb7SMarcel Moolenaar 
2062c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2072c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2082c42a146SMarcel Moolenaar 	return (0);
209df8bae1dSRodney W. Grimes }
2102c42a146SMarcel Moolenaar 
2116f841fb7SMarcel Moolenaar static __inline int
2126f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2132c42a146SMarcel Moolenaar {
2142c42a146SMarcel Moolenaar 	int i;
2152c42a146SMarcel Moolenaar 
2166f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2172c42a146SMarcel Moolenaar 		if (set->__bits[i])
2182c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
219df8bae1dSRodney W. Grimes 	return (0);
220df8bae1dSRodney W. Grimes }
221df8bae1dSRodney W. Grimes 
2222c42a146SMarcel Moolenaar /*
2232c42a146SMarcel Moolenaar  * do_sigaction
2242c42a146SMarcel Moolenaar  * sigaction
2252c42a146SMarcel Moolenaar  * osigaction
2262c42a146SMarcel Moolenaar  */
2272c42a146SMarcel Moolenaar static int
228645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
2292c42a146SMarcel Moolenaar 	struct proc *p;
2302c42a146SMarcel Moolenaar 	register int sig;
2312c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
232645682fdSLuoqi Chen 	int old;
233df8bae1dSRodney W. Grimes {
234628d2653SJohn Baldwin 	register struct sigacts *ps;
235df8bae1dSRodney W. Grimes 
2362899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2372c42a146SMarcel Moolenaar 		return (EINVAL);
2382c42a146SMarcel Moolenaar 
239628d2653SJohn Baldwin 	PROC_LOCK(p);
240628d2653SJohn Baldwin 	ps = p->p_sigacts;
2412c42a146SMarcel Moolenaar 	if (oact) {
2422c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2432c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2442c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2452c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2462c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2472c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2482c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2492c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2502c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2512c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2522c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2532c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2542c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
255645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2562c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
257645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2582c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2592c42a146SMarcel Moolenaar 	}
2602c42a146SMarcel Moolenaar 	if (act) {
2612c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
262628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
263628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2642c42a146SMarcel Moolenaar 			return (EINVAL);
265628d2653SJohn Baldwin 		}
2662c42a146SMarcel Moolenaar 
267df8bae1dSRodney W. Grimes 		/*
268df8bae1dSRodney W. Grimes 		 * Change setting atomically.
269df8bae1dSRodney W. Grimes 		 */
2702c42a146SMarcel Moolenaar 
2712c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2722c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2732c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
274aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
275aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
27680f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
27780f42b55SIan Dowse 		} else {
27880f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2792c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2802c42a146SMarcel Moolenaar 		}
2812c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2822c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
283df8bae1dSRodney W. Grimes 		else
2842c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2852c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2862c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
287df8bae1dSRodney W. Grimes 		else
2882c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2892c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2902c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2911e41c1b5SSteven Wallace 		else
2922c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2932c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2942c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
295289ccde0SPeter Wemm 		else
2962c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
297df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2982c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2992c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
300df8bae1dSRodney W. Grimes 		else
3018c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
302df8bae1dSRodney W. Grimes #endif
3032c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3042c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
305645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
3066626c604SJulian Elischer 			else
307645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
308aa7a4daeSPeter Wemm 			if ((act->sa_flags & SA_NOCLDWAIT) ||
309aa7a4daeSPeter Wemm 			    ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) {
310245f17d4SJoerg Wunsch 				/*
3112c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3122c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3132c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3142c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
315245f17d4SJoerg Wunsch 				 */
316245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
317645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3186626c604SJulian Elischer 				else
319645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
320245f17d4SJoerg Wunsch 			} else
321645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
322df8bae1dSRodney W. Grimes 		}
323df8bae1dSRodney W. Grimes 		/*
324df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3252c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3262c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3272c42a146SMarcel Moolenaar 		 * have to restart the process.
328df8bae1dSRodney W. Grimes 		 */
3292c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3302c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3312c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3322c42a146SMarcel Moolenaar 			/* never to be seen again */
3332c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3342c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3352c42a146SMarcel Moolenaar 				/* easier in psignal */
3362c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3372c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
338645682fdSLuoqi Chen 		} else {
3392c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3402c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3412c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3422c42a146SMarcel Moolenaar 			else
3432c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3442c42a146SMarcel Moolenaar 		}
345e8ebc08fSPeter Wemm #ifdef COMPAT_43
346645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
347645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
348645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
349645682fdSLuoqi Chen 		else
350645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
351e8ebc08fSPeter Wemm #endif
352df8bae1dSRodney W. Grimes 	}
353628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3542c42a146SMarcel Moolenaar 	return (0);
3552c42a146SMarcel Moolenaar }
3562c42a146SMarcel Moolenaar 
3572c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3582c42a146SMarcel Moolenaar struct sigaction_args {
3592c42a146SMarcel Moolenaar 	int	sig;
3602c42a146SMarcel Moolenaar 	struct	sigaction *act;
3612c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3622c42a146SMarcel Moolenaar };
3632c42a146SMarcel Moolenaar #endif
364fb99ab88SMatthew Dillon /*
365fb99ab88SMatthew Dillon  * MPSAFE
366fb99ab88SMatthew Dillon  */
3672c42a146SMarcel Moolenaar /* ARGSUSED */
3682c42a146SMarcel Moolenaar int
369b40ce416SJulian Elischer sigaction(td, uap)
370b40ce416SJulian Elischer 	struct thread *td;
3712c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3722c42a146SMarcel Moolenaar {
373b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3742c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3752c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3762c42a146SMarcel Moolenaar 	int error;
3772c42a146SMarcel Moolenaar 
378fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
379fb99ab88SMatthew Dillon 
3806f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3816f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3822c42a146SMarcel Moolenaar 	if (actp) {
3836f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3842c42a146SMarcel Moolenaar 		if (error)
385fb99ab88SMatthew Dillon 			goto done2;
3862c42a146SMarcel Moolenaar 	}
387645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3882c42a146SMarcel Moolenaar 	if (oactp && !error) {
3896f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3902c42a146SMarcel Moolenaar 	}
391fb99ab88SMatthew Dillon done2:
392fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
3932c42a146SMarcel Moolenaar 	return (error);
3942c42a146SMarcel Moolenaar }
3952c42a146SMarcel Moolenaar 
39631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3972c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3982c42a146SMarcel Moolenaar struct osigaction_args {
3992c42a146SMarcel Moolenaar 	int	signum;
4002c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4012c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4022c42a146SMarcel Moolenaar };
4032c42a146SMarcel Moolenaar #endif
404fb99ab88SMatthew Dillon /*
405fb99ab88SMatthew Dillon  * MPSAFE
406fb99ab88SMatthew Dillon  */
4072c42a146SMarcel Moolenaar /* ARGSUSED */
4082c42a146SMarcel Moolenaar int
409b40ce416SJulian Elischer osigaction(td, uap)
410b40ce416SJulian Elischer 	struct thread *td;
4112c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4122c42a146SMarcel Moolenaar {
413b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
4142c42a146SMarcel Moolenaar 	struct osigaction sa;
4152c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4162c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4172c42a146SMarcel Moolenaar 	int error;
4182c42a146SMarcel Moolenaar 
4196f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4206f841fb7SMarcel Moolenaar 		return (EINVAL);
421fb99ab88SMatthew Dillon 
4226f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4236f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
424fb99ab88SMatthew Dillon 
425fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
426fb99ab88SMatthew Dillon 
4272c42a146SMarcel Moolenaar 	if (nsap) {
4286f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4292c42a146SMarcel Moolenaar 		if (error)
430fb99ab88SMatthew Dillon 			goto done2;
4312c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4322c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4332c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4342c42a146SMarcel Moolenaar 	}
435645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
4362c42a146SMarcel Moolenaar 	if (osap && !error) {
4372c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4382c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4392c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4406f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4412c42a146SMarcel Moolenaar 	}
442fb99ab88SMatthew Dillon done2:
443fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
4442c42a146SMarcel Moolenaar 	return (error);
4452c42a146SMarcel Moolenaar }
44631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes /*
449df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
450df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
451df8bae1dSRodney W. Grimes  */
452df8bae1dSRodney W. Grimes void
453df8bae1dSRodney W. Grimes siginit(p)
454df8bae1dSRodney W. Grimes 	struct proc *p;
455df8bae1dSRodney W. Grimes {
456df8bae1dSRodney W. Grimes 	register int i;
457df8bae1dSRodney W. Grimes 
458628d2653SJohn Baldwin 	PROC_LOCK(p);
4592c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4602c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4612c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
462628d2653SJohn Baldwin 	PROC_UNLOCK(p);
463df8bae1dSRodney W. Grimes }
464df8bae1dSRodney W. Grimes 
465df8bae1dSRodney W. Grimes /*
466df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
467df8bae1dSRodney W. Grimes  */
468df8bae1dSRodney W. Grimes void
469df8bae1dSRodney W. Grimes execsigs(p)
470df8bae1dSRodney W. Grimes 	register struct proc *p;
471df8bae1dSRodney W. Grimes {
472628d2653SJohn Baldwin 	register struct sigacts *ps;
4732c42a146SMarcel Moolenaar 	register int sig;
474df8bae1dSRodney W. Grimes 
475df8bae1dSRodney W. Grimes 	/*
476df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
477df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
478df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
479df8bae1dSRodney W. Grimes 	 */
480628d2653SJohn Baldwin 	PROC_LOCK(p);
481628d2653SJohn Baldwin 	ps = p->p_sigacts;
4822c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4832c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4842c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4852c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4862c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4872c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4882c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
489df8bae1dSRodney W. Grimes 		}
4902c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
491df8bae1dSRodney W. Grimes 	}
492df8bae1dSRodney W. Grimes 	/*
493df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
494df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
495df8bae1dSRodney W. Grimes 	 */
496645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
497645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
498645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
4993b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
50080e907a1SPeter Wemm 	/*
50180e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
50280e907a1SPeter Wemm 	 */
503645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
504c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
505c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
506628d2653SJohn Baldwin 	PROC_UNLOCK(p);
507df8bae1dSRodney W. Grimes }
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes /*
510628d2653SJohn Baldwin  * do_sigprocmask()
5117c8fdcbdSMatthew Dillon  *
512628d2653SJohn Baldwin  *	Manipulate signal mask.
513df8bae1dSRodney W. Grimes  */
5142c42a146SMarcel Moolenaar static int
515645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
5162c42a146SMarcel Moolenaar 	struct proc *p;
5172c42a146SMarcel Moolenaar 	int how;
5182c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
519645682fdSLuoqi Chen 	int old;
5202c42a146SMarcel Moolenaar {
5212c42a146SMarcel Moolenaar 	int error;
5222c42a146SMarcel Moolenaar 
523628d2653SJohn Baldwin 	PROC_LOCK(p);
5242c42a146SMarcel Moolenaar 	if (oset != NULL)
5252c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
5262c42a146SMarcel Moolenaar 
5272c42a146SMarcel Moolenaar 	error = 0;
5282c42a146SMarcel Moolenaar 	if (set != NULL) {
5292c42a146SMarcel Moolenaar 		switch (how) {
5302c42a146SMarcel Moolenaar 		case SIG_BLOCK:
531645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
5322c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
5332c42a146SMarcel Moolenaar 			break;
5342c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
5352c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
53679065dbaSBruce Evans 			signotify(p);
5372c42a146SMarcel Moolenaar 			break;
5382c42a146SMarcel Moolenaar 		case SIG_SETMASK:
539645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
540645682fdSLuoqi Chen 			if (old)
541645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
542645682fdSLuoqi Chen 			else
5432c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
54479065dbaSBruce Evans 			signotify(p);
5452c42a146SMarcel Moolenaar 			break;
5462c42a146SMarcel Moolenaar 		default:
5472c42a146SMarcel Moolenaar 			error = EINVAL;
5482c42a146SMarcel Moolenaar 			break;
5492c42a146SMarcel Moolenaar 		}
5502c42a146SMarcel Moolenaar 	}
551628d2653SJohn Baldwin 	PROC_UNLOCK(p);
5522c42a146SMarcel Moolenaar 	return (error);
5532c42a146SMarcel Moolenaar }
5542c42a146SMarcel Moolenaar 
5557c8fdcbdSMatthew Dillon /*
556b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
5577c8fdcbdSMatthew Dillon  */
5587c8fdcbdSMatthew Dillon 
559d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
560df8bae1dSRodney W. Grimes struct sigprocmask_args {
561df8bae1dSRodney W. Grimes 	int	how;
5622c42a146SMarcel Moolenaar 	const sigset_t *set;
5632c42a146SMarcel Moolenaar 	sigset_t *oset;
564df8bae1dSRodney W. Grimes };
565d2d3e875SBruce Evans #endif
56626f9a767SRodney W. Grimes int
567b40ce416SJulian Elischer sigprocmask(td, uap)
568b40ce416SJulian Elischer 	register struct thread *td;
569df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
570df8bae1dSRodney W. Grimes {
571b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
5722c42a146SMarcel Moolenaar 	sigset_t set, oset;
5732c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5742c42a146SMarcel Moolenaar 	int error;
575df8bae1dSRodney W. Grimes 
5766f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5776f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5782c42a146SMarcel Moolenaar 	if (setp) {
5796f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5802c42a146SMarcel Moolenaar 		if (error)
5812c42a146SMarcel Moolenaar 			return (error);
582df8bae1dSRodney W. Grimes 	}
583645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5842c42a146SMarcel Moolenaar 	if (osetp && !error) {
5856f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5862c42a146SMarcel Moolenaar 	}
5872c42a146SMarcel Moolenaar 	return (error);
5882c42a146SMarcel Moolenaar }
5892c42a146SMarcel Moolenaar 
59031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5917c8fdcbdSMatthew Dillon /*
5927c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5937c8fdcbdSMatthew Dillon  */
5942c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5952c42a146SMarcel Moolenaar struct osigprocmask_args {
5962c42a146SMarcel Moolenaar 	int	how;
5972c42a146SMarcel Moolenaar 	osigset_t mask;
5982c42a146SMarcel Moolenaar };
5992c42a146SMarcel Moolenaar #endif
6002c42a146SMarcel Moolenaar int
601b40ce416SJulian Elischer osigprocmask(td, uap)
602b40ce416SJulian Elischer 	register struct thread *td;
6032c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
6042c42a146SMarcel Moolenaar {
605b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
6062c42a146SMarcel Moolenaar 	sigset_t set, oset;
6072c42a146SMarcel Moolenaar 	int error;
6082c42a146SMarcel Moolenaar 
6092c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
610645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
611b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
612df8bae1dSRodney W. Grimes 	return (error);
613df8bae1dSRodney W. Grimes }
61431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
615df8bae1dSRodney W. Grimes 
616d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
617df8bae1dSRodney W. Grimes struct sigpending_args {
6182c42a146SMarcel Moolenaar 	sigset_t	*set;
619df8bae1dSRodney W. Grimes };
620d2d3e875SBruce Evans #endif
621fb99ab88SMatthew Dillon /*
622fb99ab88SMatthew Dillon  * MPSAFE
623fb99ab88SMatthew Dillon  */
624df8bae1dSRodney W. Grimes /* ARGSUSED */
62526f9a767SRodney W. Grimes int
626b40ce416SJulian Elischer sigpending(td, uap)
627b40ce416SJulian Elischer 	struct thread *td;
628df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
629df8bae1dSRodney W. Grimes {
630b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
631628d2653SJohn Baldwin 	sigset_t siglist;
632fb99ab88SMatthew Dillon 	int error;
633df8bae1dSRodney W. Grimes 
634fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
635628d2653SJohn Baldwin 	PROC_LOCK(p);
636628d2653SJohn Baldwin 	siglist = p->p_siglist;
637628d2653SJohn Baldwin 	PROC_UNLOCK(p);
638fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
639fb99ab88SMatthew Dillon 	error = copyout(&siglist, uap->set, sizeof(sigset_t));
640fb99ab88SMatthew Dillon 	return(error);
6412c42a146SMarcel Moolenaar }
6422c42a146SMarcel Moolenaar 
64331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6442c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6452c42a146SMarcel Moolenaar struct osigpending_args {
6462c42a146SMarcel Moolenaar 	int	dummy;
6472c42a146SMarcel Moolenaar };
6482c42a146SMarcel Moolenaar #endif
649fb99ab88SMatthew Dillon /*
650fb99ab88SMatthew Dillon  * MPSAFE
651fb99ab88SMatthew Dillon  */
6522c42a146SMarcel Moolenaar /* ARGSUSED */
6532c42a146SMarcel Moolenaar int
654b40ce416SJulian Elischer osigpending(td, uap)
655b40ce416SJulian Elischer 	struct thread *td;
6562c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6572c42a146SMarcel Moolenaar {
658b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
659b40ce416SJulian Elischer 
660fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
661628d2653SJohn Baldwin 	PROC_LOCK(p);
662b40ce416SJulian Elischer 	SIG2OSIG(p->p_siglist, td->td_retval[0]);
663628d2653SJohn Baldwin 	PROC_UNLOCK(p);
664fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
665df8bae1dSRodney W. Grimes 	return (0);
666df8bae1dSRodney W. Grimes }
66731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
670df8bae1dSRodney W. Grimes /*
671df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
672df8bae1dSRodney W. Grimes  */
673d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
674df8bae1dSRodney W. Grimes struct osigvec_args {
675df8bae1dSRodney W. Grimes 	int	signum;
676df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
677df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
678df8bae1dSRodney W. Grimes };
679d2d3e875SBruce Evans #endif
680fb99ab88SMatthew Dillon /*
681fb99ab88SMatthew Dillon  * MPSAFE
682fb99ab88SMatthew Dillon  */
683df8bae1dSRodney W. Grimes /* ARGSUSED */
68426f9a767SRodney W. Grimes int
685b40ce416SJulian Elischer osigvec(td, uap)
686b40ce416SJulian Elischer 	struct thread *td;
687df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
688df8bae1dSRodney W. Grimes {
689b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
690df8bae1dSRodney W. Grimes 	struct sigvec vec;
6912c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6922c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6932c42a146SMarcel Moolenaar 	int error;
694df8bae1dSRodney W. Grimes 
6956f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6966f841fb7SMarcel Moolenaar 		return (EINVAL);
6976f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6986f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6992c42a146SMarcel Moolenaar 	if (nsap) {
7006f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
7012c42a146SMarcel Moolenaar 		if (error)
702df8bae1dSRodney W. Grimes 			return (error);
7032c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
7042c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
7052c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
7062c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
707df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
7082c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
709df8bae1dSRodney W. Grimes #endif
710df8bae1dSRodney W. Grimes 	}
711fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
712645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
713fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
7142c42a146SMarcel Moolenaar 	if (osap && !error) {
7152c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7162c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7172c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7182c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7192c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7202c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7212c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7222c42a146SMarcel Moolenaar #endif
7236f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
7242c42a146SMarcel Moolenaar 	}
7252c42a146SMarcel Moolenaar 	return (error);
726df8bae1dSRodney W. Grimes }
727df8bae1dSRodney W. Grimes 
728d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
729df8bae1dSRodney W. Grimes struct osigblock_args {
730df8bae1dSRodney W. Grimes 	int	mask;
731df8bae1dSRodney W. Grimes };
732d2d3e875SBruce Evans #endif
733fb99ab88SMatthew Dillon /*
734fb99ab88SMatthew Dillon  * MPSAFE
735fb99ab88SMatthew Dillon  */
73626f9a767SRodney W. Grimes int
737b40ce416SJulian Elischer osigblock(td, uap)
738b40ce416SJulian Elischer 	register struct thread *td;
739df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
740df8bae1dSRodney W. Grimes {
741b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7422c42a146SMarcel Moolenaar 	sigset_t set;
743df8bae1dSRodney W. Grimes 
7442c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7452c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
746fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
747628d2653SJohn Baldwin 	PROC_LOCK(p);
748b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
7492c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
750628d2653SJohn Baldwin 	PROC_UNLOCK(p);
751fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
752df8bae1dSRodney W. Grimes 	return (0);
753df8bae1dSRodney W. Grimes }
754df8bae1dSRodney W. Grimes 
755d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
756df8bae1dSRodney W. Grimes struct osigsetmask_args {
757df8bae1dSRodney W. Grimes 	int	mask;
758df8bae1dSRodney W. Grimes };
759d2d3e875SBruce Evans #endif
760fb99ab88SMatthew Dillon /*
761fb99ab88SMatthew Dillon  * MPSAFE
762fb99ab88SMatthew Dillon  */
76326f9a767SRodney W. Grimes int
764b40ce416SJulian Elischer osigsetmask(td, uap)
765b40ce416SJulian Elischer 	struct thread *td;
766df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
767df8bae1dSRodney W. Grimes {
768b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7692c42a146SMarcel Moolenaar 	sigset_t set;
770df8bae1dSRodney W. Grimes 
7712c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7722c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
773fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
774628d2653SJohn Baldwin 	PROC_LOCK(p);
775b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
776645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
77779065dbaSBruce Evans 	signotify(p);
778628d2653SJohn Baldwin 	PROC_UNLOCK(p);
779fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
780df8bae1dSRodney W. Grimes 	return (0);
781df8bae1dSRodney W. Grimes }
782df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
783df8bae1dSRodney W. Grimes 
784df8bae1dSRodney W. Grimes /*
785df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
786df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
787df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
788b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
789b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
790b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
791df8bae1dSRodney W. Grimes  */
792d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
793df8bae1dSRodney W. Grimes struct sigsuspend_args {
7942c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
795df8bae1dSRodney W. Grimes };
796d2d3e875SBruce Evans #endif
797fb99ab88SMatthew Dillon /*
798fb99ab88SMatthew Dillon  * MPSAFE
799fb99ab88SMatthew Dillon  */
800df8bae1dSRodney W. Grimes /* ARGSUSED */
80126f9a767SRodney W. Grimes int
802b40ce416SJulian Elischer sigsuspend(td, uap)
803b40ce416SJulian Elischer 	struct thread *td;
804df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
805df8bae1dSRodney W. Grimes {
806b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
8072c42a146SMarcel Moolenaar 	sigset_t mask;
808628d2653SJohn Baldwin 	register struct sigacts *ps;
8092c42a146SMarcel Moolenaar 	int error;
8102c42a146SMarcel Moolenaar 
8116f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
8122c42a146SMarcel Moolenaar 	if (error)
8132c42a146SMarcel Moolenaar 		return (error);
814df8bae1dSRodney W. Grimes 
815df8bae1dSRodney W. Grimes 	/*
816645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
817df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
818df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
819df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
820df8bae1dSRodney W. Grimes 	 * to indicate this.
821df8bae1dSRodney W. Grimes 	 */
822fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
823628d2653SJohn Baldwin 	PROC_LOCK(p);
824628d2653SJohn Baldwin 	ps = p->p_sigacts;
8256626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
826645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
827645682fdSLuoqi Chen 
828645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
8292c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
83079065dbaSBruce Evans 	signotify(p);
831628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
8322c42a146SMarcel Moolenaar 		/* void */;
833628d2653SJohn Baldwin 	PROC_UNLOCK(p);
834fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
8352c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
8362c42a146SMarcel Moolenaar 	return (EINTR);
8372c42a146SMarcel Moolenaar }
8382c42a146SMarcel Moolenaar 
83931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8402c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8412c42a146SMarcel Moolenaar struct osigsuspend_args {
8422c42a146SMarcel Moolenaar 	osigset_t mask;
8432c42a146SMarcel Moolenaar };
8442c42a146SMarcel Moolenaar #endif
845fb99ab88SMatthew Dillon /*
846fb99ab88SMatthew Dillon  * MPSAFE
847fb99ab88SMatthew Dillon  */
8482c42a146SMarcel Moolenaar /* ARGSUSED */
8492c42a146SMarcel Moolenaar int
850b40ce416SJulian Elischer osigsuspend(td, uap)
851b40ce416SJulian Elischer 	struct thread *td;
8522c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
8532c42a146SMarcel Moolenaar {
854b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
855645682fdSLuoqi Chen 	sigset_t mask;
856628d2653SJohn Baldwin 	register struct sigacts *ps;
8572c42a146SMarcel Moolenaar 
858fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
859628d2653SJohn Baldwin 	PROC_LOCK(p);
860628d2653SJohn Baldwin 	ps = p->p_sigacts;
8612c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
862645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
863645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
864645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
865645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
86679065dbaSBruce Evans 	signotify(p);
867628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
868df8bae1dSRodney W. Grimes 		/* void */;
869628d2653SJohn Baldwin 	PROC_UNLOCK(p);
870fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
871df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
872df8bae1dSRodney W. Grimes 	return (EINTR);
873df8bae1dSRodney W. Grimes }
87431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
875df8bae1dSRodney W. Grimes 
876df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
877d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
878df8bae1dSRodney W. Grimes struct osigstack_args {
879df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
880df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
881df8bae1dSRodney W. Grimes };
882d2d3e875SBruce Evans #endif
883fb99ab88SMatthew Dillon /*
884fb99ab88SMatthew Dillon  * MPSAFE
885fb99ab88SMatthew Dillon  */
886df8bae1dSRodney W. Grimes /* ARGSUSED */
88726f9a767SRodney W. Grimes int
888b40ce416SJulian Elischer osigstack(td, uap)
889b40ce416SJulian Elischer 	struct thread *td;
890df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
891df8bae1dSRodney W. Grimes {
892b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
893df8bae1dSRodney W. Grimes 	struct sigstack ss;
894fb99ab88SMatthew Dillon 	int error = 0;
895fb99ab88SMatthew Dillon 
896fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
897df8bae1dSRodney W. Grimes 
898d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
899628d2653SJohn Baldwin 		PROC_LOCK(p);
900645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
901b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
902628d2653SJohn Baldwin 		PROC_UNLOCK(p);
903d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
904d034d459SMarcel Moolenaar 		if (error)
905fb99ab88SMatthew Dillon 			goto done2;
906d034d459SMarcel Moolenaar 	}
907d034d459SMarcel Moolenaar 
908d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
909d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
910fb99ab88SMatthew Dillon 			goto done2;
911628d2653SJohn Baldwin 		PROC_LOCK(p);
912645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
913645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
914645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
915645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
916628d2653SJohn Baldwin 		PROC_UNLOCK(p);
917df8bae1dSRodney W. Grimes 	}
918fb99ab88SMatthew Dillon done2:
919fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
920fb99ab88SMatthew Dillon 	return (error);
921df8bae1dSRodney W. Grimes }
922df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
923df8bae1dSRodney W. Grimes 
924d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
925df8bae1dSRodney W. Grimes struct sigaltstack_args {
9262c42a146SMarcel Moolenaar 	stack_t	*ss;
9272c42a146SMarcel Moolenaar 	stack_t	*oss;
928df8bae1dSRodney W. Grimes };
929d2d3e875SBruce Evans #endif
930fb99ab88SMatthew Dillon /*
931fb99ab88SMatthew Dillon  * MPSAFE
932fb99ab88SMatthew Dillon  */
933df8bae1dSRodney W. Grimes /* ARGSUSED */
93426f9a767SRodney W. Grimes int
935b40ce416SJulian Elischer sigaltstack(td, uap)
936b40ce416SJulian Elischer 	struct thread *td;
937df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
938df8bae1dSRodney W. Grimes {
939b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9402c42a146SMarcel Moolenaar 	stack_t ss;
941fb99ab88SMatthew Dillon 	int oonstack;
942fb99ab88SMatthew Dillon 	int error = 0;
943fb99ab88SMatthew Dillon 
944fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
945df8bae1dSRodney W. Grimes 
946b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
947d034d459SMarcel Moolenaar 
948d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
949628d2653SJohn Baldwin 		PROC_LOCK(p);
950d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
951d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
952d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
953628d2653SJohn Baldwin 		PROC_UNLOCK(p);
954d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
955fb99ab88SMatthew Dillon 			goto done2;
956df8bae1dSRodney W. Grimes 	}
957d034d459SMarcel Moolenaar 
958d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
959fb99ab88SMatthew Dillon 		if (oonstack) {
960fb99ab88SMatthew Dillon 			error = EPERM;
961fb99ab88SMatthew Dillon 			goto done2;
962fb99ab88SMatthew Dillon 		}
963d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
964fb99ab88SMatthew Dillon 			goto done2;
965fb99ab88SMatthew Dillon 		if ((ss.ss_flags & ~SS_DISABLE) != 0) {
966fb99ab88SMatthew Dillon 			error = EINVAL;
967fb99ab88SMatthew Dillon 			goto done2;
968fb99ab88SMatthew Dillon 		}
969d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
970fb99ab88SMatthew Dillon 			if (ss.ss_size < p->p_sysent->sv_minsigstksz) {
971fb99ab88SMatthew Dillon 				error = ENOMEM;
972fb99ab88SMatthew Dillon 				goto done2;
973fb99ab88SMatthew Dillon 			}
974628d2653SJohn Baldwin 			PROC_LOCK(p);
975645682fdSLuoqi Chen 			p->p_sigstk = ss;
976d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
977628d2653SJohn Baldwin 			PROC_UNLOCK(p);
978628d2653SJohn Baldwin 		} else {
979628d2653SJohn Baldwin 			PROC_LOCK(p);
980d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
981628d2653SJohn Baldwin 			PROC_UNLOCK(p);
982628d2653SJohn Baldwin 		}
983d034d459SMarcel Moolenaar 	}
984fb99ab88SMatthew Dillon done2:
985fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
986fb99ab88SMatthew Dillon 	return (error);
987df8bae1dSRodney W. Grimes }
988df8bae1dSRodney W. Grimes 
989d93f860cSPoul-Henning Kamp /*
990d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
991d93f860cSPoul-Henning Kamp  * cp is calling process.
992d93f860cSPoul-Henning Kamp  */
993d93f860cSPoul-Henning Kamp int
9942c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
995d93f860cSPoul-Henning Kamp 	register struct proc *cp;
9962c42a146SMarcel Moolenaar 	int sig, pgid, all;
997d93f860cSPoul-Henning Kamp {
998d93f860cSPoul-Henning Kamp 	register struct proc *p;
999d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1000d93f860cSPoul-Henning Kamp 	int nfound = 0;
1001d93f860cSPoul-Henning Kamp 
1002553629ebSJake Burkholder 	if (all) {
1003d93f860cSPoul-Henning Kamp 		/*
1004d93f860cSPoul-Henning Kamp 		 * broadcast
1005d93f860cSPoul-Henning Kamp 		 */
10061005a129SJohn Baldwin 		sx_slock(&allproc_lock);
10072e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1008628d2653SJohn Baldwin 			PROC_LOCK(p);
1009628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) {
1010628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1011628d2653SJohn Baldwin 				continue;
1012628d2653SJohn Baldwin 			}
101333a9ed9dSJohn Baldwin 			if (p_cansignal(cp, 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 {
1022f591779bSSeigo Tanimura 		PGRPSESS_SLOCK();
1023f591779bSSeigo Tanimura 		if (pgid == 0) {
1024d93f860cSPoul-Henning Kamp 			/*
1025d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1026d93f860cSPoul-Henning Kamp 			 */
1027d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
1028f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1029f591779bSSeigo Tanimura 		} else {
1030d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1031f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1032f591779bSSeigo Tanimura 				PGRPSESS_SUNLOCK();
1033d93f860cSPoul-Henning Kamp 				return (ESRCH);
1034d93f860cSPoul-Henning Kamp 			}
1035f591779bSSeigo Tanimura 		}
1036f591779bSSeigo Tanimura 		PGRPSESS_SUNLOCK();
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 			mtx_lock_spin(&sched_lock);
1044628d2653SJohn Baldwin 			if (p->p_stat == SZOMB) {
1045628d2653SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
104633a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1047628d2653SJohn Baldwin 				continue;
1048628d2653SJohn Baldwin 			}
1049628d2653SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
105033a9ed9dSJohn Baldwin 			if (p_cansignal(cp, p, sig) == 0) {
1051d93f860cSPoul-Henning Kamp 				nfound++;
105233a9ed9dSJohn Baldwin 				if (sig)
10532c42a146SMarcel Moolenaar 					psignal(p, sig);
1054628d2653SJohn Baldwin 			}
105533a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1056d93f860cSPoul-Henning Kamp 		}
1057f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1058d93f860cSPoul-Henning Kamp 	}
1059d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1060d93f860cSPoul-Henning Kamp }
1061d93f860cSPoul-Henning Kamp 
1062d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1063df8bae1dSRodney W. Grimes struct kill_args {
1064df8bae1dSRodney W. Grimes 	int	pid;
1065df8bae1dSRodney W. Grimes 	int	signum;
1066df8bae1dSRodney W. Grimes };
1067d2d3e875SBruce Evans #endif
1068fb99ab88SMatthew Dillon /*
1069fb99ab88SMatthew Dillon  * MPSAFE
1070fb99ab88SMatthew Dillon  */
1071df8bae1dSRodney W. Grimes /* ARGSUSED */
107226f9a767SRodney W. Grimes int
1073b40ce416SJulian Elischer kill(td, uap)
1074b40ce416SJulian Elischer 	register struct thread *td;
1075df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1076df8bae1dSRodney W. Grimes {
1077b40ce416SJulian Elischer 	register struct proc *cp = td->td_proc;
1078df8bae1dSRodney W. Grimes 	register struct proc *p;
1079fb99ab88SMatthew Dillon 	int error = 0;
1080df8bae1dSRodney W. Grimes 
10816c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1082df8bae1dSRodney W. Grimes 		return (EINVAL);
1083fb99ab88SMatthew Dillon 
1084fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1085df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1086df8bae1dSRodney W. Grimes 		/* kill single process */
1087fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1088fb99ab88SMatthew Dillon 			error = ESRCH;
1089fb99ab88SMatthew Dillon 		} else if (p_cansignal(cp, p, uap->signum)) {
109033a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1091fb99ab88SMatthew Dillon 			error = EPERM;
1092fb99ab88SMatthew Dillon 		} else {
109333a9ed9dSJohn Baldwin 			if (uap->signum)
1094df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1095628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1096fb99ab88SMatthew Dillon 			error = 0;
1097df8bae1dSRodney W. Grimes 		}
1098fb99ab88SMatthew Dillon 	} else {
1099df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1100df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
1101fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, 0, 1);
1102fb99ab88SMatthew Dillon 			break;
1103df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
1104fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, 0, 0);
1105fb99ab88SMatthew Dillon 			break;
1106df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
1107fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, -uap->pid, 0);
1108fb99ab88SMatthew Dillon 			break;
1109df8bae1dSRodney W. Grimes 		}
1110fb99ab88SMatthew Dillon 	}
1111fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1112fb99ab88SMatthew Dillon 	return(error);
1113df8bae1dSRodney W. Grimes }
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1116d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1117df8bae1dSRodney W. Grimes struct okillpg_args {
1118df8bae1dSRodney W. Grimes 	int	pgid;
1119df8bae1dSRodney W. Grimes 	int	signum;
1120df8bae1dSRodney W. Grimes };
1121d2d3e875SBruce Evans #endif
1122fb99ab88SMatthew Dillon /*
1123fb99ab88SMatthew Dillon  * MPSAFE
1124fb99ab88SMatthew Dillon  */
1125df8bae1dSRodney W. Grimes /* ARGSUSED */
112626f9a767SRodney W. Grimes int
1127b40ce416SJulian Elischer okillpg(td, uap)
1128b40ce416SJulian Elischer 	struct thread *td;
1129df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1130df8bae1dSRodney W. Grimes {
1131fb99ab88SMatthew Dillon 	int error;
1132df8bae1dSRodney W. Grimes 
11336c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1134df8bae1dSRodney W. Grimes 		return (EINVAL);
1135fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1136b40ce416SJulian Elischer 	error = killpg1(td->td_proc, uap->signum, uap->pgid, 0);
1137fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1138fb99ab88SMatthew Dillon 	return (error);
1139df8bae1dSRodney W. Grimes }
1140df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes /*
1143df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1144df8bae1dSRodney W. Grimes  */
1145df8bae1dSRodney W. Grimes void
11462c42a146SMarcel Moolenaar gsignal(pgid, sig)
11472c42a146SMarcel Moolenaar 	int pgid, sig;
1148df8bae1dSRodney W. Grimes {
1149df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1150df8bae1dSRodney W. Grimes 
1151f591779bSSeigo Tanimura 	if (pgid != 0) {
1152f591779bSSeigo Tanimura 		PGRPSESS_SLOCK();
1153f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1154f591779bSSeigo Tanimura 		PGRPSESS_SUNLOCK();
1155f591779bSSeigo Tanimura 		if (pgrp != NULL) {
11562c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1157f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1158f591779bSSeigo Tanimura 		}
1159f591779bSSeigo Tanimura 	}
1160df8bae1dSRodney W. Grimes }
1161df8bae1dSRodney W. Grimes 
1162df8bae1dSRodney W. Grimes /*
1163df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1164df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1165df8bae1dSRodney W. Grimes  */
1166df8bae1dSRodney W. Grimes void
11672c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1168df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
11692c42a146SMarcel Moolenaar 	int sig, checkctty;
1170df8bae1dSRodney W. Grimes {
1171df8bae1dSRodney W. Grimes 	register struct proc *p;
1172df8bae1dSRodney W. Grimes 
1173628d2653SJohn Baldwin 	if (pgrp) {
1174f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1175628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1176628d2653SJohn Baldwin 			PROC_LOCK(p);
1177df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
11782c42a146SMarcel Moolenaar 				psignal(p, sig);
1179628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1180628d2653SJohn Baldwin 		}
1181628d2653SJohn Baldwin 	}
1182df8bae1dSRodney W. Grimes }
1183df8bae1dSRodney W. Grimes 
1184df8bae1dSRodney W. Grimes /*
1185df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1186df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1187df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1188356861dbSMatthew Dillon  *
1189356861dbSMatthew Dillon  * MPSAFE
1190df8bae1dSRodney W. Grimes  */
1191df8bae1dSRodney W. Grimes void
11922c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1193df8bae1dSRodney W. Grimes 	struct proc *p;
11942c42a146SMarcel Moolenaar 	register int sig;
11958674077aSJeffrey Hsu 	u_long code;
1196df8bae1dSRodney W. Grimes {
1197df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1198df8bae1dSRodney W. Grimes 
1199356861dbSMatthew Dillon 	mtx_lock(&Giant);
1200628d2653SJohn Baldwin 	PROC_LOCK(p);
12012c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1202d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1203df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1204df8bae1dSRodney W. Grimes #ifdef KTRACE
1205df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
12062c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
12072c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1208df8bae1dSRodney W. Grimes #endif
12092c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
12102c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
12112c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
12122c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
12132c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
12142c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1215289ccde0SPeter Wemm 			/*
12162c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1217289ccde0SPeter Wemm 			 */
12182c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
12192c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
12202c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
12212c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
12222c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1223dedc04feSPeter Wemm 		}
12246f841fb7SMarcel Moolenaar 	} else {
12256626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
12262c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
12272c42a146SMarcel Moolenaar 		psignal(p, sig);
1228df8bae1dSRodney W. Grimes 	}
1229628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1230356861dbSMatthew Dillon 	mtx_unlock(&Giant);
1231df8bae1dSRodney W. Grimes }
1232df8bae1dSRodney W. Grimes 
1233df8bae1dSRodney W. Grimes /*
1234df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1235df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1236df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1237df8bae1dSRodney W. Grimes  *
1238df8bae1dSRodney W. Grimes  * Exceptions:
1239df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1240df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1241df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1242df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1243df8bae1dSRodney W. Grimes  *
1244df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1245df8bae1dSRodney W. Grimes  */
1246df8bae1dSRodney W. Grimes void
12472c42a146SMarcel Moolenaar psignal(p, sig)
1248df8bae1dSRodney W. Grimes 	register struct proc *p;
12492c42a146SMarcel Moolenaar 	register int sig;
1250df8bae1dSRodney W. Grimes {
125140447cd4SJohn Baldwin 	register int prop;
1252df8bae1dSRodney W. Grimes 	register sig_t action;
1253b40ce416SJulian Elischer 	struct thread *td;
12540f5c7c4bSPoul-Henning Kamp #ifdef SMP
1255b40ce416SJulian Elischer 	struct ksegrp *kg;
12560f5c7c4bSPoul-Henning Kamp #endif
1257df8bae1dSRodney W. Grimes 
12582899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
12592899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
12602c42a146SMarcel Moolenaar 
1261628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1262cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1263cb679c38SJonathan Lemon 
12642c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1265df8bae1dSRodney W. Grimes 
1266df8bae1dSRodney W. Grimes 	/*
12672a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
12682a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
12692a024a2bSSean Eric Fagan 	 * a chance, as well.
1270df8bae1dSRodney W. Grimes 	 */
1271b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1272df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1273b40ce416SJulian Elischer 	} else {
1274df8bae1dSRodney W. Grimes 		/*
1275df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1276df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1277df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1278df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1279df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1280df8bae1dSRodney W. Grimes 		 */
1281628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1282df8bae1dSRodney W. Grimes 			return;
12832c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1284df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
12852c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1286df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1287df8bae1dSRodney W. Grimes 		else
1288df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1289df8bae1dSRodney W. Grimes 	}
1290df8bae1dSRodney W. Grimes 
1291b40ce416SJulian Elischer 	/*
1292b40ce416SJulian Elischer 	 * bring the priority of a process up if we want it to get
1293b40ce416SJulian Elischer 	 * killed in this lifetime.
1294b40ce416SJulian Elischer 	 * XXXKSE think if a better way to do this.
1295b40ce416SJulian Elischer 	 *
1296b40ce416SJulian Elischer 	 * What we need to do is see if there is a thread that will
1297b40ce416SJulian Elischer 	 * be able to accept the signal. e.g.
1298b40ce416SJulian Elischer 	 * FOREACH_THREAD_IN_PROC() {
1299b40ce416SJulian Elischer 	 *	if runnable, we're done
1300b40ce416SJulian Elischer 	 *	else pick one at random.
1301b40ce416SJulian Elischer 	 * }
1302b40ce416SJulian Elischer 	 */
1303b40ce416SJulian Elischer 	/* XXXKSE
1304b40ce416SJulian Elischer 	 * For now there is one thread per proc.
1305b40ce416SJulian Elischer 	 * Effectively select one sucker thread..
1306b40ce416SJulian Elischer 	 */
1307079b7badSJulian Elischer 	td = FIRST_THREAD_IN_PROC(p);
13089ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1309b40ce416SJulian Elischer 	if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) &&
1310b40ce416SJulian Elischer 	    (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0))
1311b40ce416SJulian Elischer 		p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */
13129ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1313df8bae1dSRodney W. Grimes 
1314df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
13152c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1316df8bae1dSRodney W. Grimes 
1317df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1318df8bae1dSRodney W. Grimes 		/*
1319df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1320df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1321df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1322df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1323df8bae1dSRodney W. Grimes 		 */
1324df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1325628d2653SJohn Baldwin 		    action == SIG_DFL)
1326df8bae1dSRodney W. Grimes 		        return;
13272c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1328df8bae1dSRodney W. Grimes 	}
13292c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
133079065dbaSBruce Evans 	signotify(p);
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 	/*
1333df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1334df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1335df8bae1dSRodney W. Grimes 	 */
13369ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
13371c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
13389ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1339df8bae1dSRodney W. Grimes 		return;
13401c32c37cSJohn Baldwin 	}
1341b40ce416SJulian Elischer 
1342df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1343df8bae1dSRodney W. Grimes 
1344df8bae1dSRodney W. Grimes 	case SSLEEP:
1345df8bae1dSRodney W. Grimes 		/*
1346df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1347df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1348df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1349df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1350df8bae1dSRodney W. Grimes 		 */
1351b40ce416SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
13529ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1353df8bae1dSRodney W. Grimes 			goto out;
13543e6831f5SJohn Baldwin 		}
1355df8bae1dSRodney W. Grimes 		/*
1356df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1357df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1358df8bae1dSRodney W. Grimes 		 * for the parent.
1359df8bae1dSRodney W. Grimes 		 */
1360df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1361df8bae1dSRodney W. Grimes 			goto run;
13629ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1363df8bae1dSRodney W. Grimes 		/*
1364df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1365df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1366df8bae1dSRodney W. Grimes 		 * be awakened.
1367df8bae1dSRodney W. Grimes 		 */
1368df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
13692c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1370df8bae1dSRodney W. Grimes 			goto out;
1371df8bae1dSRodney W. Grimes 		}
1372df8bae1dSRodney W. Grimes 		/*
1373df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1374df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1375df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1376df8bae1dSRodney W. Grimes 		 * cause the process to run.
1377df8bae1dSRodney W. Grimes 		 */
1378df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
13793e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1380df8bae1dSRodney W. Grimes 				goto runfast;
1381df8bae1dSRodney W. Grimes 			/*
1382df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1383df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1384df8bae1dSRodney W. Grimes 			 */
1385df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1386df8bae1dSRodney W. Grimes 				goto out;
13872c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
13882c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1389628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1390f591779bSSeigo Tanimura 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1391df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1392628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
13935b3047d5SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1394df8bae1dSRodney W. Grimes 			stop(p);
13955b3047d5SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
1396df8bae1dSRodney W. Grimes 			goto out;
13973e6831f5SJohn Baldwin 		} else
1398df8bae1dSRodney W. Grimes 			goto runfast;
1399df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1400df8bae1dSRodney W. Grimes 
1401df8bae1dSRodney W. Grimes 	case SSTOP:
14029ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1403df8bae1dSRodney W. Grimes 		/*
1404df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1405df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1406df8bae1dSRodney W. Grimes 		 */
140740447cd4SJohn Baldwin 		if (p->p_flag & P_TRACED)
1408df8bae1dSRodney W. Grimes 			goto out;
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 		/*
1411df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1412df8bae1dSRodney W. Grimes 		 */
14132c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1414df8bae1dSRodney W. Grimes 			goto runfast;
1415df8bae1dSRodney W. Grimes 
1416df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1417df8bae1dSRodney W. Grimes 			/*
1418df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1419df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1420df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1421df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1422df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1423df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1424df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1425df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1426df8bae1dSRodney W. Grimes 			 */
1427df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
14282c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1429df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1430df8bae1dSRodney W. Grimes 				goto runfast;
14319ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1432b40ce416SJulian Elischer 			/*
1433b40ce416SJulian Elischer 			 * XXXKSE
1434b40ce416SJulian Elischer 			 * do this for each thread.
1435b40ce416SJulian Elischer 			 */
1436b40ce416SJulian Elischer 			if (p->p_flag & P_KSES) {
1437b40ce416SJulian Elischer 				mtx_assert(&sched_lock,
1438b40ce416SJulian Elischer 				    MA_OWNED | MA_NOTRECURSED);
1439b40ce416SJulian Elischer 				FOREACH_THREAD_IN_PROC(p, td) {
1440b40ce416SJulian Elischer 					if (td->td_wchan == NULL) {
1441b40ce416SJulian Elischer 						setrunnable(td); /* XXXKSE */
1442b40ce416SJulian Elischer 					} else {
1443b40ce416SJulian Elischer 						/* mark it as sleeping */
1444b40ce416SJulian Elischer 					}
1445b40ce416SJulian Elischer 				}
1446b40ce416SJulian Elischer 				mtx_unlock_spin(&sched_lock);
1447b40ce416SJulian Elischer 			} else {
1448079b7badSJulian Elischer 				if (td->td_wchan == NULL)
1449df8bae1dSRodney W. Grimes 					goto run;
1450df8bae1dSRodney W. Grimes 				p->p_stat = SSLEEP;
14519ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
1452b40ce416SJulian Elischer 			}
1453fdd4e5c6SJulian Elischer 			goto out;
1454df8bae1dSRodney W. Grimes 		}
1455df8bae1dSRodney W. Grimes 
1456df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1457df8bae1dSRodney W. Grimes 			/*
1458df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1459df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1460df8bae1dSRodney W. Grimes 			 */
14612c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1462df8bae1dSRodney W. Grimes 			goto out;
1463df8bae1dSRodney W. Grimes 		}
1464df8bae1dSRodney W. Grimes 
1465df8bae1dSRodney W. Grimes 		/*
1466df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1467df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1468df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1469df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1470b40ce416SJulian Elischer 		 * XXXKSE should we wake ALL blocked threads?
1471df8bae1dSRodney W. Grimes 		 */
14729ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
1473b40ce416SJulian Elischer 		if (p->p_flag & P_KSES) {
1474b40ce416SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1475b40ce416SJulian Elischer 				if (td->td_wchan && (td->td_flags & TDF_SINTR)){
1476b40ce416SJulian Elischer 					if (td->td_flags & TDF_CVWAITQ)
1477b40ce416SJulian Elischer 						cv_waitq_remove(td);
1478238510fcSJason Evans 					else
1479b40ce416SJulian Elischer 						unsleep(td); /* XXXKSE */
1480b40ce416SJulian Elischer 				}
1481b40ce416SJulian Elischer 			}
1482b40ce416SJulian Elischer 		} else {
1483b40ce416SJulian Elischer 			if (td->td_wchan && td->td_flags & TDF_SINTR) {
1484b40ce416SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
1485b40ce416SJulian Elischer 					cv_waitq_remove(td);
1486b40ce416SJulian Elischer 				else
1487b40ce416SJulian Elischer 					unsleep(td); /* XXXKSE */
1488b40ce416SJulian Elischer 			}
1489238510fcSJason Evans 		}
14909ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1491df8bae1dSRodney W. Grimes 		goto out;
1492df8bae1dSRodney W. Grimes 
1493df8bae1dSRodney W. Grimes 	default:
1494df8bae1dSRodney W. Grimes 		/*
1495df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1496df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1497df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1498df8bae1dSRodney W. Grimes 		 */
1499d2ef4060SBruce Evans 		if (p->p_stat == SRUN) {
15003163861cSTor Egge #ifdef SMP
1501b40ce416SJulian Elischer 			struct kse *ke;
1502b40ce416SJulian Elischer 			struct thread *td = curthread;
1503b40ce416SJulian Elischer /* we should only deliver to one thread.. but which one? */
1504b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(p, kg) {
1505b40ce416SJulian Elischer 				FOREACH_KSE_IN_GROUP(kg, ke) {
1506b40ce416SJulian Elischer 					if (ke->ke_thread == td) {
1507b40ce416SJulian Elischer 						continue;
1508b40ce416SJulian Elischer 					}
1509b40ce416SJulian Elischer 					forward_signal(ke->ke_thread);
1510b40ce416SJulian Elischer 				}
1511b40ce416SJulian Elischer 			}
15123163861cSTor Egge #endif
15136caa8a15SJohn Baldwin 		}
15149ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1515df8bae1dSRodney W. Grimes 		goto out;
1516df8bae1dSRodney W. Grimes 	}
1517df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1518df8bae1dSRodney W. Grimes 
1519df8bae1dSRodney W. Grimes runfast:
1520df8bae1dSRodney W. Grimes 	/*
1521df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1522b40ce416SJulian Elischer 	 * XXXKSE Should we make them all run fast?
1523b40ce416SJulian Elischer 	 * Maybe just one would be enough?
1524df8bae1dSRodney W. Grimes 	 */
15259ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
15262c100766SJulian Elischer 
15272c100766SJulian Elischer 	if (FIRST_THREAD_IN_PROC(p)->td_priority > PUSER) {
15282c100766SJulian Elischer 		FIRST_THREAD_IN_PROC(p)->td_priority = PUSER;
1529b40ce416SJulian Elischer 	}
1530df8bae1dSRodney W. Grimes run:
15313e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
15323e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1533b40ce416SJulian Elischer 	setrunnable(td); /* XXXKSE */
15349ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
15353e6831f5SJohn Baldwin out:
15363e6831f5SJohn Baldwin 	/* If we jump here, sched_lock should not be owned. */
15373e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1538df8bae1dSRodney W. Grimes }
1539df8bae1dSRodney W. Grimes 
1540df8bae1dSRodney W. Grimes /*
1541df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1542df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1543df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1544df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1545df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1546df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1547df8bae1dSRodney W. Grimes  * sequence is
1548df8bae1dSRodney W. Grimes  *
15492c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
15502c42a146SMarcel Moolenaar  *		postsig(sig);
1551df8bae1dSRodney W. Grimes  */
155226f9a767SRodney W. Grimes int
1553df8bae1dSRodney W. Grimes issignal(p)
1554df8bae1dSRodney W. Grimes 	register struct proc *p;
1555df8bae1dSRodney W. Grimes {
15562c42a146SMarcel Moolenaar 	sigset_t mask;
15572c42a146SMarcel Moolenaar 	register int sig, prop;
1558df8bae1dSRodney W. Grimes 
1559628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1560df8bae1dSRodney W. Grimes 	for (;;) {
15612a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
15622a024a2bSSean Eric Fagan 
15632c42a146SMarcel Moolenaar 		mask = p->p_siglist;
15642c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1565df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
15662c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
15672c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1568df8bae1dSRodney W. Grimes 			return (0);
15692c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
15702c42a146SMarcel Moolenaar 		prop = sigprop(sig);
15712a024a2bSSean Eric Fagan 
1572628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
15732a024a2bSSean Eric Fagan 
1574df8bae1dSRodney W. Grimes 		/*
1575df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1576df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1577df8bae1dSRodney W. Grimes 		 */
15782c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
15792c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1580df8bae1dSRodney W. Grimes 			continue;
1581df8bae1dSRodney W. Grimes 		}
1582df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1583df8bae1dSRodney W. Grimes 			/*
1584df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1585df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1586df8bae1dSRodney W. Grimes 			 */
15872c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1588628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1589df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1590628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
1591df8bae1dSRodney W. Grimes 			do {
15929ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
15935b3047d5SJohn Baldwin 				stop(p);
1594c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1595c86b6ff5SJohn Baldwin 				DROP_GIANT();
15962ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1597df8bae1dSRodney W. Grimes 				mi_switch();
15989ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
159920cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1600628d2653SJohn Baldwin 				PROC_LOCK(p);
16012a024a2bSSean Eric Fagan 			} while (!trace_req(p)
16022a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1603df8bae1dSRodney W. Grimes 
1604df8bae1dSRodney W. Grimes 			/*
1605df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1606df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1607df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1608df8bae1dSRodney W. Grimes 			 */
1609df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1610df8bae1dSRodney W. Grimes 				continue;
1611df8bae1dSRodney W. Grimes 
1612df8bae1dSRodney W. Grimes 			/*
1613df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1614df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1615df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1616df8bae1dSRodney W. Grimes 			 */
16172c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
16182c42a146SMarcel Moolenaar 			sig = p->p_xstat;
16192c42a146SMarcel Moolenaar 			if (sig == 0)
1620df8bae1dSRodney W. Grimes 				continue;
1621df8bae1dSRodney W. Grimes 
1622df8bae1dSRodney W. Grimes 			/*
1623df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1624df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1625df8bae1dSRodney W. Grimes 			 */
16262c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
16272c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1628df8bae1dSRodney W. Grimes 				continue;
1629df8bae1dSRodney W. Grimes 		}
1630df8bae1dSRodney W. Grimes 
1631df8bae1dSRodney W. Grimes 		/*
1632df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1633df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1634df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1635df8bae1dSRodney W. Grimes 		 */
16362c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1637df8bae1dSRodney W. Grimes 
16380b53fbe8SBruce Evans 		case (int)SIG_DFL:
1639df8bae1dSRodney W. Grimes 			/*
1640df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1641df8bae1dSRodney W. Grimes 			 */
1642df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1643df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1644df8bae1dSRodney W. Grimes 				/*
1645df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1646df8bae1dSRodney W. Grimes 				 * in init? XXX
1647df8bae1dSRodney W. Grimes 				 */
1648d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
16492c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1650df8bae1dSRodney W. Grimes #endif
1651df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1652df8bae1dSRodney W. Grimes 			}
1653df8bae1dSRodney W. Grimes 			/*
1654df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1655df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1656df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1657df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1658df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1659df8bae1dSRodney W. Grimes 			 */
1660df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1661df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1662df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1663df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
1664df8bae1dSRodney W. Grimes 					break;	/* == ignore */
16652c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1666628d2653SJohn Baldwin 				PROC_LOCK(p->p_pptr);
1667f591779bSSeigo Tanimura 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1668df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1669628d2653SJohn Baldwin 				PROC_UNLOCK(p->p_pptr);
16709ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
16715b3047d5SJohn Baldwin 				stop(p);
1672c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1673c86b6ff5SJohn Baldwin 				DROP_GIANT();
16742ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1675df8bae1dSRodney W. Grimes 				mi_switch();
16769ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
167720cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1678628d2653SJohn Baldwin 				PROC_LOCK(p);
1679df8bae1dSRodney W. Grimes 				break;
1680df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1681df8bae1dSRodney W. Grimes 				/*
1682df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1683df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1684df8bae1dSRodney W. Grimes 				 */
1685df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1686df8bae1dSRodney W. Grimes 			} else
16872c42a146SMarcel Moolenaar 				return (sig);
1688df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1689df8bae1dSRodney W. Grimes 
16900b53fbe8SBruce Evans 		case (int)SIG_IGN:
1691df8bae1dSRodney W. Grimes 			/*
1692df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1693df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1694df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1695df8bae1dSRodney W. Grimes 			 */
1696df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1697df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1698df8bae1dSRodney W. Grimes 				printf("issignal\n");
1699df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1700df8bae1dSRodney W. Grimes 
1701df8bae1dSRodney W. Grimes 		default:
1702df8bae1dSRodney W. Grimes 			/*
1703df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1704df8bae1dSRodney W. Grimes 			 * postsig() process it.
1705df8bae1dSRodney W. Grimes 			 */
17062c42a146SMarcel Moolenaar 			return (sig);
1707df8bae1dSRodney W. Grimes 		}
17082c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1709df8bae1dSRodney W. Grimes 	}
1710df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1711df8bae1dSRodney W. Grimes }
1712df8bae1dSRodney W. Grimes 
1713df8bae1dSRodney W. Grimes /*
1714df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1715df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
17165b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
17175b3047d5SJohn Baldwin  * lock held.
1718df8bae1dSRodney W. Grimes  */
17195b3047d5SJohn Baldwin static void
1720df8bae1dSRodney W. Grimes stop(p)
1721df8bae1dSRodney W. Grimes 	register struct proc *p;
1722df8bae1dSRodney W. Grimes {
1723df8bae1dSRodney W. Grimes 
1724628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
17255b3047d5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
1726df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1727df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1728df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1729df8bae1dSRodney W. Grimes }
1730df8bae1dSRodney W. Grimes 
1731df8bae1dSRodney W. Grimes /*
1732df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1733df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1734df8bae1dSRodney W. Grimes  */
1735df8bae1dSRodney W. Grimes void
17362c42a146SMarcel Moolenaar postsig(sig)
17372c42a146SMarcel Moolenaar 	register int sig;
1738df8bae1dSRodney W. Grimes {
1739b40ce416SJulian Elischer 	struct thread *td = curthread;
1740b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1741628d2653SJohn Baldwin 	struct sigacts *ps;
17422c42a146SMarcel Moolenaar 	sig_t action;
17432c42a146SMarcel Moolenaar 	sigset_t returnmask;
17442c42a146SMarcel Moolenaar 	int code;
1745df8bae1dSRodney W. Grimes 
17462c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
17475526d2d9SEivind Eklund 
17482ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1749628d2653SJohn Baldwin 	ps = p->p_sigacts;
17502c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
17512c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1752df8bae1dSRodney W. Grimes #ifdef KTRACE
1753df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1754645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
17552c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1756df8bae1dSRodney W. Grimes #endif
1757628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
17582a024a2bSSean Eric Fagan 
1759df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1760df8bae1dSRodney W. Grimes 		/*
1761df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1762df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1763df8bae1dSRodney W. Grimes 		 */
1764b40ce416SJulian Elischer 		sigexit(td, sig);
1765df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1766df8bae1dSRodney W. Grimes 	} else {
1767df8bae1dSRodney W. Grimes 		/*
1768df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1769df8bae1dSRodney W. Grimes 		 */
17702c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
17715526d2d9SEivind Eklund 		    ("postsig action"));
1772df8bae1dSRodney W. Grimes 		/*
1773df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1774645682fdSLuoqi Chen 		 * occurrences of this signal.
1775df8bae1dSRodney W. Grimes 		 *
1776645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1777df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1778645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1779df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1780df8bae1dSRodney W. Grimes 		 */
1781645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
17826626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1783645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1784df8bae1dSRodney W. Grimes 		} else
1785df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
17862c42a146SMarcel Moolenaar 
17872c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
17882c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
17892c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
17902c42a146SMarcel Moolenaar 
17912c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1792289ccde0SPeter Wemm 			/*
17932c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1794289ccde0SPeter Wemm 			 */
17952c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
17962c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
17972c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
17982c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
17992c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1800dedc04feSPeter Wemm 		}
1801df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
18022c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1803df8bae1dSRodney W. Grimes 			code = 0;
1804df8bae1dSRodney W. Grimes 		} else {
18056626c604SJulian Elischer 			code = p->p_code;
18066626c604SJulian Elischer 			p->p_code = 0;
18076626c604SJulian Elischer 			p->p_sig = 0;
1808df8bae1dSRodney W. Grimes 		}
18092c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1810df8bae1dSRodney W. Grimes 	}
1811df8bae1dSRodney W. Grimes }
1812df8bae1dSRodney W. Grimes 
1813df8bae1dSRodney W. Grimes /*
1814df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1815df8bae1dSRodney W. Grimes  */
181626f9a767SRodney W. Grimes void
1817df8bae1dSRodney W. Grimes killproc(p, why)
1818df8bae1dSRodney W. Grimes 	struct proc *p;
1819df8bae1dSRodney W. Grimes 	char *why;
1820df8bae1dSRodney W. Grimes {
18219081e5e8SJohn Baldwin 
18229081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18230384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
18240384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1825729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1826b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1827df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1828df8bae1dSRodney W. Grimes }
1829df8bae1dSRodney W. Grimes 
1830df8bae1dSRodney W. Grimes /*
1831df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1832df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1833df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1834df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1835df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1836df8bae1dSRodney W. Grimes  * does not return.
1837df8bae1dSRodney W. Grimes  */
183826f9a767SRodney W. Grimes void
1839b40ce416SJulian Elischer sigexit(td, sig)
1840b40ce416SJulian Elischer 	struct thread *td;
18412c42a146SMarcel Moolenaar 	int sig;
1842df8bae1dSRodney W. Grimes {
1843b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1844df8bae1dSRodney W. Grimes 
1845628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1846df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
18472c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
18482c42a146SMarcel Moolenaar 		p->p_sig = sig;
1849c364e17eSAndrey A. Chernov 		/*
1850c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1851c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1852c364e17eSAndrey A. Chernov 		 * these messages.)
1853c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1854c364e17eSAndrey A. Chernov 		 */
1855628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1856c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1857c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1858b40ce416SJulian Elischer 		if (coredump(td) == 0)
18592c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
186057308494SJoerg Wunsch 		if (kern_logsigexit)
186157308494SJoerg Wunsch 			log(LOG_INFO,
186257308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
18633d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
1864b1fc0ec1SRobert Watson 			    p->p_ucred ? p->p_ucred->cr_uid : -1,
18652c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
18662c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1867c31146a1SJohn Baldwin 	} else {
1868628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1869628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1870628d2653SJohn Baldwin 			mtx_lock(&Giant);
1871c31146a1SJohn Baldwin 	}
1872b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1873df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1874df8bae1dSRodney W. Grimes }
1875df8bae1dSRodney W. Grimes 
1876c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1877c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1878c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1879c5edb423SSean Eric Fagan 
1880c5edb423SSean Eric Fagan /*
1881c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1882c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1883c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1884c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1885c5edb423SSean Eric Fagan  *	%P	process id (pid)
1886c5edb423SSean Eric Fagan  *	%U	user id (uid)
1887c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1888c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1889c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1890c5edb423SSean Eric Fagan  */
1891c5edb423SSean Eric Fagan 
1892fca666a1SJulian Elischer static char *
1893c5edb423SSean Eric Fagan expand_name(name, uid, pid)
189487f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1895c5edb423SSean Eric Fagan 	char *temp;
1896c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1897c5edb423SSean Eric Fagan 	int i, n;
1898c5edb423SSean Eric Fagan 	char *format = corefilename;
1899ce38ca0fSAlfred Perlstein 	size_t namelen;
1900c5edb423SSean Eric Fagan 
1901ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
19020bfe2990SEivind Eklund 	if (temp == NULL)
19030bfe2990SEivind Eklund 		return NULL;
1904ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1905ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1906c5edb423SSean Eric Fagan 		int l;
1907c5edb423SSean Eric Fagan 		switch (format[i]) {
1908c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1909c5edb423SSean Eric Fagan 			i++;
1910c5edb423SSean Eric Fagan 			switch (format[i]) {
1911c5edb423SSean Eric Fagan 			case '%':
1912c5edb423SSean Eric Fagan 				temp[n++] = '%';
1913c5edb423SSean Eric Fagan 				break;
1914c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1915ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
191687f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1917c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1918c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1919c5edb423SSean Eric Fagan 					return NULL;
1920c5edb423SSean Eric Fagan 				}
1921ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1922ce38ca0fSAlfred Perlstein 				n += namelen;
1923c5edb423SSean Eric Fagan 				break;
1924c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1925ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1926c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
192787f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1928c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1929c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1930c5edb423SSean Eric Fagan 					return NULL;
1931c5edb423SSean Eric Fagan 				}
1932c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1933c5edb423SSean Eric Fagan 				n += l;
1934c5edb423SSean Eric Fagan 				break;
1935c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1936ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1937c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
193887f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1939c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1940c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1941c5edb423SSean Eric Fagan 					return NULL;
1942c5edb423SSean Eric Fagan 				}
1943c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1944c5edb423SSean Eric Fagan 				n += l;
1945c5edb423SSean Eric Fagan 				break;
1946c5edb423SSean Eric Fagan 			default:
1947c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1948c5edb423SSean Eric Fagan 			}
1949c5edb423SSean Eric Fagan 			break;
1950c5edb423SSean Eric Fagan 		default:
1951c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1952c5edb423SSean Eric Fagan 		}
1953c5edb423SSean Eric Fagan 	}
1954ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1955c5edb423SSean Eric Fagan 	return temp;
1956c5edb423SSean Eric Fagan }
1957c5edb423SSean Eric Fagan 
1958df8bae1dSRodney W. Grimes /*
1959fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1960fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1961fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1962fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1963fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
19645da271f5SRobert Watson  *
19655da271f5SRobert Watson  * XXX: VOP_GETATTR() here requires holding the vnode lock.
1966fca666a1SJulian Elischer  */
1967fca666a1SJulian Elischer 
1968fca666a1SJulian Elischer static int
1969b40ce416SJulian Elischer coredump(struct thread *td)
1970fca666a1SJulian Elischer {
1971b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1972fca666a1SJulian Elischer 	register struct vnode *vp;
1973da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
197406ae1e91SMatthew Dillon 	struct flock lf;
1975fca666a1SJulian Elischer 	struct nameidata nd;
1976fca666a1SJulian Elischer 	struct vattr vattr;
1977e6796b67SKirk McKusick 	int error, error1, flags;
1978f2a2857bSKirk McKusick 	struct mount *mp;
1979fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1980fca666a1SJulian Elischer 	off_t limit;
1981fca666a1SJulian Elischer 
1982628d2653SJohn Baldwin 	PROC_LOCK(p);
1983628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1984fca666a1SJulian Elischer 
1985628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1986628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1987fca666a1SJulian Elischer 		return (EFAULT);
1988628d2653SJohn Baldwin 	}
1989fca666a1SJulian Elischer 
1990fca666a1SJulian Elischer 	/*
199135a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
199235a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
199335a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
199435a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
199535a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
199635a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1997fca666a1SJulian Elischer 	 */
199835a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1999628d2653SJohn Baldwin 	if (limit == 0) {
2000628d2653SJohn Baldwin 		PROC_UNLOCK(p);
200135a2598fSSean Eric Fagan 		return 0;
2002628d2653SJohn Baldwin 	}
2003628d2653SJohn Baldwin 	PROC_UNLOCK(p);
200435a2598fSSean Eric Fagan 
2005f2a2857bSKirk McKusick restart:
2006fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
2007ccdbd10cSPeter Pentchev 	if (name == NULL)
2008ccdbd10cSPeter Pentchev 		return (EINVAL);
2009b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2010e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2011e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2012fca666a1SJulian Elischer 	free(name, M_TEMP);
2013fca666a1SJulian Elischer 	if (error)
2014fca666a1SJulian Elischer 		return (error);
2015762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2016fca666a1SJulian Elischer 	vp = nd.ni_vp;
201706ae1e91SMatthew Dillon 
2018b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
201906ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
202006ae1e91SMatthew Dillon 	lf.l_start = 0;
202106ae1e91SMatthew Dillon 	lf.l_len = 0;
202206ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
202306ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
202406ae1e91SMatthew Dillon 	if (error)
202506ae1e91SMatthew Dillon 		goto out2;
202606ae1e91SMatthew Dillon 
202706ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
202806ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
202906ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2030b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2031f2a2857bSKirk McKusick 			return (error);
2032f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2033f2a2857bSKirk McKusick 			return (error);
2034f2a2857bSKirk McKusick 		goto restart;
2035f2a2857bSKirk McKusick 	}
2036fca666a1SJulian Elischer 
2037fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
2038fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
2039b40ce416SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2040fca666a1SJulian Elischer 		error = EFAULT;
204106ae1e91SMatthew Dillon 		goto out1;
2042fca666a1SJulian Elischer 	}
2043fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2044fca666a1SJulian Elischer 	vattr.va_size = 0;
204588b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2046b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2047b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
204888b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2049628d2653SJohn Baldwin 	PROC_LOCK(p);
2050fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2051628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2052fca666a1SJulian Elischer 
2053fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2054b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2055fca666a1SJulian Elischer 	  ENOSYS;
2056fca666a1SJulian Elischer 
205706ae1e91SMatthew Dillon out1:
205806ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
205906ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2060f2a2857bSKirk McKusick 	vn_finished_write(mp);
206106ae1e91SMatthew Dillon out2:
2062b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2063fca666a1SJulian Elischer 	if (error == 0)
2064fca666a1SJulian Elischer 		error = error1;
2065fca666a1SJulian Elischer 	return (error);
2066fca666a1SJulian Elischer }
2067fca666a1SJulian Elischer 
2068fca666a1SJulian Elischer /*
2069df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2070df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2071df8bae1dSRodney W. Grimes  */
2072d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2073df8bae1dSRodney W. Grimes struct nosys_args {
2074df8bae1dSRodney W. Grimes 	int	dummy;
2075df8bae1dSRodney W. Grimes };
2076d2d3e875SBruce Evans #endif
2077fb99ab88SMatthew Dillon /*
2078fb99ab88SMatthew Dillon  * MPSAFE
2079fb99ab88SMatthew Dillon  */
2080df8bae1dSRodney W. Grimes /* ARGSUSED */
208126f9a767SRodney W. Grimes int
2082b40ce416SJulian Elischer nosys(td, args)
2083b40ce416SJulian Elischer 	struct thread *td;
2084df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2085df8bae1dSRodney W. Grimes {
2086b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2087b40ce416SJulian Elischer 
2088fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2089628d2653SJohn Baldwin 	PROC_LOCK(p);
2090df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2091628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2092fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2093df8bae1dSRodney W. Grimes 	return (EINVAL);
2094df8bae1dSRodney W. Grimes }
2095831d27a9SDon Lewis 
2096831d27a9SDon Lewis /*
209748f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2098831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2099831d27a9SDon Lewis  */
2100831d27a9SDon Lewis void
21012c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
2102831d27a9SDon Lewis 	struct sigio *sigio;
21032c42a146SMarcel Moolenaar 	int sig, checkctty;
2104831d27a9SDon Lewis {
2105831d27a9SDon Lewis 	if (sigio == NULL)
2106831d27a9SDon Lewis 		return;
2107831d27a9SDon Lewis 
2108831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2109628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
21102b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
21112c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2112628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2113831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2114831d27a9SDon Lewis 		struct proc *p;
2115831d27a9SDon Lewis 
2116f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2117628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2118628d2653SJohn Baldwin 			PROC_LOCK(p);
21192b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2120831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
21212c42a146SMarcel Moolenaar 				psignal(p, sig);
2122628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2123628d2653SJohn Baldwin 		}
2124f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2125831d27a9SDon Lewis 	}
2126831d27a9SDon Lewis }
2127cb679c38SJonathan Lemon 
2128cb679c38SJonathan Lemon static int
2129cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2130cb679c38SJonathan Lemon {
2131cb679c38SJonathan Lemon 	struct proc *p = curproc;
2132cb679c38SJonathan Lemon 
2133cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2134cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2135cb679c38SJonathan Lemon 
2136628d2653SJohn Baldwin 	PROC_LOCK(p);
2137cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2138628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2139cb679c38SJonathan Lemon 
2140cb679c38SJonathan Lemon 	return (0);
2141cb679c38SJonathan Lemon }
2142cb679c38SJonathan Lemon 
2143cb679c38SJonathan Lemon static void
2144cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2145cb679c38SJonathan Lemon {
2146cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2147cb679c38SJonathan Lemon 
2148628d2653SJohn Baldwin 	PROC_LOCK(p);
2149e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2150628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2151cb679c38SJonathan Lemon }
2152cb679c38SJonathan Lemon 
2153cb679c38SJonathan Lemon /*
2154cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2155cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2156cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2157cb679c38SJonathan Lemon  * isn't worth the trouble.
2158cb679c38SJonathan Lemon  */
2159cb679c38SJonathan Lemon static int
2160cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2161cb679c38SJonathan Lemon {
2162cb679c38SJonathan Lemon 
2163cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2164cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2165cb679c38SJonathan Lemon 
2166cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2167cb679c38SJonathan Lemon 			kn->kn_data++;
2168cb679c38SJonathan Lemon 	}
2169cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2170cb679c38SJonathan Lemon }
2171