xref: /freebsd/sys/kern/kern_sig.c (revision 4d77a549fed13746cc4edf84e511b385c1754f3d)
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 	sigset_t tmpset;
178fbbeeb6cSBruce Evans 
1792ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18033510ef1SBruce Evans 	if (SIGISEMPTY(p->p_siglist))
1812ad7d304SJohn Baldwin 		return (0);
182fbbeeb6cSBruce Evans 	tmpset = p->p_siglist;
183fbbeeb6cSBruce Evans 	SIGSETNAND(tmpset, p->p_sigmask);
18433510ef1SBruce Evans 	if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
1852ad7d304SJohn Baldwin 		return (0);
1862ad7d304SJohn Baldwin 	return (issignal(p));
187fbbeeb6cSBruce Evans }
188fbbeeb6cSBruce Evans 
1896f841fb7SMarcel Moolenaar static __inline int
1906f841fb7SMarcel Moolenaar sigprop(int sig)
1912c42a146SMarcel Moolenaar {
1926f841fb7SMarcel Moolenaar 
1932c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
1942c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
1952c42a146SMarcel Moolenaar 	return (0);
196df8bae1dSRodney W. Grimes }
1972c42a146SMarcel Moolenaar 
1986f841fb7SMarcel Moolenaar static __inline int
1996f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2002c42a146SMarcel Moolenaar {
2012c42a146SMarcel Moolenaar 	int i;
2022c42a146SMarcel Moolenaar 
2036f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2042c42a146SMarcel Moolenaar 		if (set->__bits[i])
2052c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
206df8bae1dSRodney W. Grimes 	return (0);
207df8bae1dSRodney W. Grimes }
208df8bae1dSRodney W. Grimes 
2092c42a146SMarcel Moolenaar /*
2102c42a146SMarcel Moolenaar  * do_sigaction
2112c42a146SMarcel Moolenaar  * sigaction
2122c42a146SMarcel Moolenaar  * osigaction
2132c42a146SMarcel Moolenaar  */
2142c42a146SMarcel Moolenaar static int
215645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
2162c42a146SMarcel Moolenaar 	struct proc *p;
2172c42a146SMarcel Moolenaar 	register int sig;
2182c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
219645682fdSLuoqi Chen 	int old;
220df8bae1dSRodney W. Grimes {
221628d2653SJohn Baldwin 	register struct sigacts *ps;
222df8bae1dSRodney W. Grimes 
2232899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2242c42a146SMarcel Moolenaar 		return (EINVAL);
2252c42a146SMarcel Moolenaar 
226628d2653SJohn Baldwin 	PROC_LOCK(p);
227628d2653SJohn Baldwin 	ps = p->p_sigacts;
2282c42a146SMarcel Moolenaar 	if (oact) {
2292c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2302c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2312c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2322c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2332c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2342c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2352c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2362c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2372c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2382c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2392c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2402c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2412c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
242645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2432c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
244645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2452c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2462c42a146SMarcel Moolenaar 	}
2472c42a146SMarcel Moolenaar 	if (act) {
2482c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
249628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
250628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2512c42a146SMarcel Moolenaar 			return (EINVAL);
252628d2653SJohn Baldwin 		}
2532c42a146SMarcel Moolenaar 
254df8bae1dSRodney W. Grimes 		/*
255df8bae1dSRodney W. Grimes 		 * Change setting atomically.
256df8bae1dSRodney W. Grimes 		 */
2572c42a146SMarcel Moolenaar 
2582c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2592c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2602c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
261aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
262aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
26380f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
26480f42b55SIan Dowse 		} else {
26580f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2662c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2672c42a146SMarcel Moolenaar 		}
2682c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2692c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
270df8bae1dSRodney W. Grimes 		else
2712c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2722c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2732c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
274df8bae1dSRodney W. Grimes 		else
2752c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2762c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2772c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2781e41c1b5SSteven Wallace 		else
2792c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2802c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2812c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
282289ccde0SPeter Wemm 		else
2832c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
284df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2852c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2862c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
287df8bae1dSRodney W. Grimes 		else
2888c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
289df8bae1dSRodney W. Grimes #endif
2902c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2912c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
292645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
2936626c604SJulian Elischer 			else
294645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
295aa7a4daeSPeter Wemm 			if ((act->sa_flags & SA_NOCLDWAIT) ||
296aa7a4daeSPeter Wemm 			    ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) {
297245f17d4SJoerg Wunsch 				/*
2982c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
2992c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3002c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3012c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
302245f17d4SJoerg Wunsch 				 */
303245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
304645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3056626c604SJulian Elischer 				else
306645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
307245f17d4SJoerg Wunsch 			} else
308645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
309df8bae1dSRodney W. Grimes 		}
310df8bae1dSRodney W. Grimes 		/*
311df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3122c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3132c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3142c42a146SMarcel Moolenaar 		 * have to restart the process.
315df8bae1dSRodney W. Grimes 		 */
3162c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3172c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3182c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3192c42a146SMarcel Moolenaar 			/* never to be seen again */
3202c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3212c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3222c42a146SMarcel Moolenaar 				/* easier in psignal */
3232c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3242c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
325645682fdSLuoqi Chen 		} else {
3262c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3272c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3282c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3292c42a146SMarcel Moolenaar 			else
3302c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3312c42a146SMarcel Moolenaar 		}
332e8ebc08fSPeter Wemm #ifdef COMPAT_43
333645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
334645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
335645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
336645682fdSLuoqi Chen 		else
337645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
338e8ebc08fSPeter Wemm #endif
339df8bae1dSRodney W. Grimes 	}
340628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3412c42a146SMarcel Moolenaar 	return (0);
3422c42a146SMarcel Moolenaar }
3432c42a146SMarcel Moolenaar 
3442c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3452c42a146SMarcel Moolenaar struct sigaction_args {
3462c42a146SMarcel Moolenaar 	int	sig;
3472c42a146SMarcel Moolenaar 	struct	sigaction *act;
3482c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3492c42a146SMarcel Moolenaar };
3502c42a146SMarcel Moolenaar #endif
351fb99ab88SMatthew Dillon /*
352fb99ab88SMatthew Dillon  * MPSAFE
353fb99ab88SMatthew Dillon  */
3542c42a146SMarcel Moolenaar /* ARGSUSED */
3552c42a146SMarcel Moolenaar int
356b40ce416SJulian Elischer sigaction(td, uap)
357b40ce416SJulian Elischer 	struct thread *td;
3582c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3592c42a146SMarcel Moolenaar {
360b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3612c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3622c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3632c42a146SMarcel Moolenaar 	int error;
3642c42a146SMarcel Moolenaar 
365fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
366fb99ab88SMatthew Dillon 
3676f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3686f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3692c42a146SMarcel Moolenaar 	if (actp) {
3706f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3712c42a146SMarcel Moolenaar 		if (error)
372fb99ab88SMatthew Dillon 			goto done2;
3732c42a146SMarcel Moolenaar 	}
374645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3752c42a146SMarcel Moolenaar 	if (oactp && !error) {
3766f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3772c42a146SMarcel Moolenaar 	}
378fb99ab88SMatthew Dillon done2:
379fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
3802c42a146SMarcel Moolenaar 	return (error);
3812c42a146SMarcel Moolenaar }
3822c42a146SMarcel Moolenaar 
38331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3842c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3852c42a146SMarcel Moolenaar struct osigaction_args {
3862c42a146SMarcel Moolenaar 	int	signum;
3872c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3882c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3892c42a146SMarcel Moolenaar };
3902c42a146SMarcel Moolenaar #endif
391fb99ab88SMatthew Dillon /*
392fb99ab88SMatthew Dillon  * MPSAFE
393fb99ab88SMatthew Dillon  */
3942c42a146SMarcel Moolenaar /* ARGSUSED */
3952c42a146SMarcel Moolenaar int
396b40ce416SJulian Elischer osigaction(td, uap)
397b40ce416SJulian Elischer 	struct thread *td;
3982c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3992c42a146SMarcel Moolenaar {
400b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
4012c42a146SMarcel Moolenaar 	struct osigaction sa;
4022c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4032c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4042c42a146SMarcel Moolenaar 	int error;
4052c42a146SMarcel Moolenaar 
4066f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4076f841fb7SMarcel Moolenaar 		return (EINVAL);
408fb99ab88SMatthew Dillon 
4096f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4106f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
411fb99ab88SMatthew Dillon 
412fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
413fb99ab88SMatthew Dillon 
4142c42a146SMarcel Moolenaar 	if (nsap) {
4156f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4162c42a146SMarcel Moolenaar 		if (error)
417fb99ab88SMatthew Dillon 			goto done2;
4182c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4192c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4202c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4212c42a146SMarcel Moolenaar 	}
422645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
4232c42a146SMarcel Moolenaar 	if (osap && !error) {
4242c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4252c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4262c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4276f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4282c42a146SMarcel Moolenaar 	}
429fb99ab88SMatthew Dillon done2:
430fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
4312c42a146SMarcel Moolenaar 	return (error);
4322c42a146SMarcel Moolenaar }
43331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes /*
436df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
437df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
438df8bae1dSRodney W. Grimes  */
439df8bae1dSRodney W. Grimes void
440df8bae1dSRodney W. Grimes siginit(p)
441df8bae1dSRodney W. Grimes 	struct proc *p;
442df8bae1dSRodney W. Grimes {
443df8bae1dSRodney W. Grimes 	register int i;
444df8bae1dSRodney W. Grimes 
445628d2653SJohn Baldwin 	PROC_LOCK(p);
4462c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4472c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4482c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
449628d2653SJohn Baldwin 	PROC_UNLOCK(p);
450df8bae1dSRodney W. Grimes }
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes /*
453df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
454df8bae1dSRodney W. Grimes  */
455df8bae1dSRodney W. Grimes void
456df8bae1dSRodney W. Grimes execsigs(p)
457df8bae1dSRodney W. Grimes 	register struct proc *p;
458df8bae1dSRodney W. Grimes {
459628d2653SJohn Baldwin 	register struct sigacts *ps;
4602c42a146SMarcel Moolenaar 	register int sig;
461df8bae1dSRodney W. Grimes 
462df8bae1dSRodney W. Grimes 	/*
463df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
464df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
465df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
466df8bae1dSRodney W. Grimes 	 */
467628d2653SJohn Baldwin 	PROC_LOCK(p);
468628d2653SJohn Baldwin 	ps = p->p_sigacts;
4692c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4702c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4712c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4722c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4732c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4742c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4752c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
476df8bae1dSRodney W. Grimes 		}
4772c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
478df8bae1dSRodney W. Grimes 	}
479df8bae1dSRodney W. Grimes 	/*
480df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
481df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
482df8bae1dSRodney W. Grimes 	 */
483645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
484645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
485645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
4863b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
48780e907a1SPeter Wemm 	/*
48880e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
48980e907a1SPeter Wemm 	 */
490645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
491c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
492c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
493628d2653SJohn Baldwin 	PROC_UNLOCK(p);
494df8bae1dSRodney W. Grimes }
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes /*
497628d2653SJohn Baldwin  * do_sigprocmask()
4987c8fdcbdSMatthew Dillon  *
499628d2653SJohn Baldwin  *	Manipulate signal mask.
500df8bae1dSRodney W. Grimes  */
5012c42a146SMarcel Moolenaar static int
502645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
5032c42a146SMarcel Moolenaar 	struct proc *p;
5042c42a146SMarcel Moolenaar 	int how;
5052c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
506645682fdSLuoqi Chen 	int old;
5072c42a146SMarcel Moolenaar {
5082c42a146SMarcel Moolenaar 	int error;
5092c42a146SMarcel Moolenaar 
510628d2653SJohn Baldwin 	PROC_LOCK(p);
5112c42a146SMarcel Moolenaar 	if (oset != NULL)
5122c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
5132c42a146SMarcel Moolenaar 
5142c42a146SMarcel Moolenaar 	error = 0;
5152c42a146SMarcel Moolenaar 	if (set != NULL) {
5162c42a146SMarcel Moolenaar 		switch (how) {
5172c42a146SMarcel Moolenaar 		case SIG_BLOCK:
518645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
5192c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
5202c42a146SMarcel Moolenaar 			break;
5212c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
5222c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
5232c42a146SMarcel Moolenaar 			break;
5242c42a146SMarcel Moolenaar 		case SIG_SETMASK:
525645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
526645682fdSLuoqi Chen 			if (old)
527645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
528645682fdSLuoqi Chen 			else
5292c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
5302c42a146SMarcel Moolenaar 			break;
5312c42a146SMarcel Moolenaar 		default:
5322c42a146SMarcel Moolenaar 			error = EINVAL;
5332c42a146SMarcel Moolenaar 			break;
5342c42a146SMarcel Moolenaar 		}
5352c42a146SMarcel Moolenaar 	}
536628d2653SJohn Baldwin 	PROC_UNLOCK(p);
5372c42a146SMarcel Moolenaar 	return (error);
5382c42a146SMarcel Moolenaar }
5392c42a146SMarcel Moolenaar 
5407c8fdcbdSMatthew Dillon /*
541b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
5427c8fdcbdSMatthew Dillon  */
5437c8fdcbdSMatthew Dillon 
544d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
545df8bae1dSRodney W. Grimes struct sigprocmask_args {
546df8bae1dSRodney W. Grimes 	int	how;
5472c42a146SMarcel Moolenaar 	const sigset_t *set;
5482c42a146SMarcel Moolenaar 	sigset_t *oset;
549df8bae1dSRodney W. Grimes };
550d2d3e875SBruce Evans #endif
55126f9a767SRodney W. Grimes int
552b40ce416SJulian Elischer sigprocmask(td, uap)
553b40ce416SJulian Elischer 	register struct thread *td;
554df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
555df8bae1dSRodney W. Grimes {
556b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
5572c42a146SMarcel Moolenaar 	sigset_t set, oset;
5582c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5592c42a146SMarcel Moolenaar 	int error;
560df8bae1dSRodney W. Grimes 
5616f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5626f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5632c42a146SMarcel Moolenaar 	if (setp) {
5646f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5652c42a146SMarcel Moolenaar 		if (error)
5662c42a146SMarcel Moolenaar 			return (error);
567df8bae1dSRodney W. Grimes 	}
568645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5692c42a146SMarcel Moolenaar 	if (osetp && !error) {
5706f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5712c42a146SMarcel Moolenaar 	}
5722c42a146SMarcel Moolenaar 	return (error);
5732c42a146SMarcel Moolenaar }
5742c42a146SMarcel Moolenaar 
57531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5767c8fdcbdSMatthew Dillon /*
5777c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5787c8fdcbdSMatthew Dillon  */
5792c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5802c42a146SMarcel Moolenaar struct osigprocmask_args {
5812c42a146SMarcel Moolenaar 	int	how;
5822c42a146SMarcel Moolenaar 	osigset_t mask;
5832c42a146SMarcel Moolenaar };
5842c42a146SMarcel Moolenaar #endif
5852c42a146SMarcel Moolenaar int
586b40ce416SJulian Elischer osigprocmask(td, uap)
587b40ce416SJulian Elischer 	register struct thread *td;
5882c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5892c42a146SMarcel Moolenaar {
590b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
5912c42a146SMarcel Moolenaar 	sigset_t set, oset;
5922c42a146SMarcel Moolenaar 	int error;
5932c42a146SMarcel Moolenaar 
5942c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
595645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
596b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
597df8bae1dSRodney W. Grimes 	return (error);
598df8bae1dSRodney W. Grimes }
59931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
600df8bae1dSRodney W. Grimes 
601d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
602df8bae1dSRodney W. Grimes struct sigpending_args {
6032c42a146SMarcel Moolenaar 	sigset_t	*set;
604df8bae1dSRodney W. Grimes };
605d2d3e875SBruce Evans #endif
606fb99ab88SMatthew Dillon /*
607fb99ab88SMatthew Dillon  * MPSAFE
608fb99ab88SMatthew Dillon  */
609df8bae1dSRodney W. Grimes /* ARGSUSED */
61026f9a767SRodney W. Grimes int
611b40ce416SJulian Elischer sigpending(td, uap)
612b40ce416SJulian Elischer 	struct thread *td;
613df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
614df8bae1dSRodney W. Grimes {
615b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
616628d2653SJohn Baldwin 	sigset_t siglist;
617fb99ab88SMatthew Dillon 	int error;
618df8bae1dSRodney W. Grimes 
619fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
620628d2653SJohn Baldwin 	PROC_LOCK(p);
621628d2653SJohn Baldwin 	siglist = p->p_siglist;
622628d2653SJohn Baldwin 	PROC_UNLOCK(p);
623fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
624fb99ab88SMatthew Dillon 	error = copyout(&siglist, uap->set, sizeof(sigset_t));
625fb99ab88SMatthew Dillon 	return(error);
6262c42a146SMarcel Moolenaar }
6272c42a146SMarcel Moolenaar 
62831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6292c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6302c42a146SMarcel Moolenaar struct osigpending_args {
6312c42a146SMarcel Moolenaar 	int	dummy;
6322c42a146SMarcel Moolenaar };
6332c42a146SMarcel Moolenaar #endif
634fb99ab88SMatthew Dillon /*
635fb99ab88SMatthew Dillon  * MPSAFE
636fb99ab88SMatthew Dillon  */
6372c42a146SMarcel Moolenaar /* ARGSUSED */
6382c42a146SMarcel Moolenaar int
639b40ce416SJulian Elischer osigpending(td, uap)
640b40ce416SJulian Elischer 	struct thread *td;
6412c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
6422c42a146SMarcel Moolenaar {
643b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
644b40ce416SJulian Elischer 
645fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
646628d2653SJohn Baldwin 	PROC_LOCK(p);
647b40ce416SJulian Elischer 	SIG2OSIG(p->p_siglist, td->td_retval[0]);
648628d2653SJohn Baldwin 	PROC_UNLOCK(p);
649fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
650df8bae1dSRodney W. Grimes 	return (0);
651df8bae1dSRodney W. Grimes }
65231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
653df8bae1dSRodney W. Grimes 
654df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
655df8bae1dSRodney W. Grimes /*
656df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
657df8bae1dSRodney W. Grimes  */
658d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
659df8bae1dSRodney W. Grimes struct osigvec_args {
660df8bae1dSRodney W. Grimes 	int	signum;
661df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
662df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
663df8bae1dSRodney W. Grimes };
664d2d3e875SBruce Evans #endif
665fb99ab88SMatthew Dillon /*
666fb99ab88SMatthew Dillon  * MPSAFE
667fb99ab88SMatthew Dillon  */
668df8bae1dSRodney W. Grimes /* ARGSUSED */
66926f9a767SRodney W. Grimes int
670b40ce416SJulian Elischer osigvec(td, uap)
671b40ce416SJulian Elischer 	struct thread *td;
672df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
673df8bae1dSRodney W. Grimes {
674b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
675df8bae1dSRodney W. Grimes 	struct sigvec vec;
6762c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6772c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6782c42a146SMarcel Moolenaar 	int error;
679df8bae1dSRodney W. Grimes 
6806f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6816f841fb7SMarcel Moolenaar 		return (EINVAL);
6826f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6836f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6842c42a146SMarcel Moolenaar 	if (nsap) {
6856f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
6862c42a146SMarcel Moolenaar 		if (error)
687df8bae1dSRodney W. Grimes 			return (error);
6882c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
6892c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
6902c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
6912c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
692df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
6932c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
694df8bae1dSRodney W. Grimes #endif
695df8bae1dSRodney W. Grimes 	}
696fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
697645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
698fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
6992c42a146SMarcel Moolenaar 	if (osap && !error) {
7002c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7012c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7022c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7032c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7042c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7052c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7062c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7072c42a146SMarcel Moolenaar #endif
7086f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
7092c42a146SMarcel Moolenaar 	}
7102c42a146SMarcel Moolenaar 	return (error);
711df8bae1dSRodney W. Grimes }
712df8bae1dSRodney W. Grimes 
713d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
714df8bae1dSRodney W. Grimes struct osigblock_args {
715df8bae1dSRodney W. Grimes 	int	mask;
716df8bae1dSRodney W. Grimes };
717d2d3e875SBruce Evans #endif
718fb99ab88SMatthew Dillon /*
719fb99ab88SMatthew Dillon  * MPSAFE
720fb99ab88SMatthew Dillon  */
72126f9a767SRodney W. Grimes int
722b40ce416SJulian Elischer osigblock(td, uap)
723b40ce416SJulian Elischer 	register struct thread *td;
724df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
725df8bae1dSRodney W. Grimes {
726b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7272c42a146SMarcel Moolenaar 	sigset_t set;
728df8bae1dSRodney W. Grimes 
7292c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7302c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
731fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
732628d2653SJohn Baldwin 	PROC_LOCK(p);
733b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
7342c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
735628d2653SJohn Baldwin 	PROC_UNLOCK(p);
736fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
737df8bae1dSRodney W. Grimes 	return (0);
738df8bae1dSRodney W. Grimes }
739df8bae1dSRodney W. Grimes 
740d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
741df8bae1dSRodney W. Grimes struct osigsetmask_args {
742df8bae1dSRodney W. Grimes 	int	mask;
743df8bae1dSRodney W. Grimes };
744d2d3e875SBruce Evans #endif
745fb99ab88SMatthew Dillon /*
746fb99ab88SMatthew Dillon  * MPSAFE
747fb99ab88SMatthew Dillon  */
74826f9a767SRodney W. Grimes int
749b40ce416SJulian Elischer osigsetmask(td, uap)
750b40ce416SJulian Elischer 	struct thread *td;
751df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
752df8bae1dSRodney W. Grimes {
753b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7542c42a146SMarcel Moolenaar 	sigset_t set;
755df8bae1dSRodney W. Grimes 
7562c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
7572c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
758fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
759628d2653SJohn Baldwin 	PROC_LOCK(p);
760b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
761645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
762628d2653SJohn Baldwin 	PROC_UNLOCK(p);
763fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
764df8bae1dSRodney W. Grimes 	return (0);
765df8bae1dSRodney W. Grimes }
766df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
767df8bae1dSRodney W. Grimes 
768df8bae1dSRodney W. Grimes /*
769df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
770df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
771df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
772b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
773b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
774b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
775df8bae1dSRodney W. Grimes  */
776d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
777df8bae1dSRodney W. Grimes struct sigsuspend_args {
7782c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
779df8bae1dSRodney W. Grimes };
780d2d3e875SBruce Evans #endif
781fb99ab88SMatthew Dillon /*
782fb99ab88SMatthew Dillon  * MPSAFE
783fb99ab88SMatthew Dillon  */
784df8bae1dSRodney W. Grimes /* ARGSUSED */
78526f9a767SRodney W. Grimes int
786b40ce416SJulian Elischer sigsuspend(td, uap)
787b40ce416SJulian Elischer 	struct thread *td;
788df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
789df8bae1dSRodney W. Grimes {
790b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7912c42a146SMarcel Moolenaar 	sigset_t mask;
792628d2653SJohn Baldwin 	register struct sigacts *ps;
7932c42a146SMarcel Moolenaar 	int error;
7942c42a146SMarcel Moolenaar 
7956f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
7962c42a146SMarcel Moolenaar 	if (error)
7972c42a146SMarcel Moolenaar 		return (error);
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes 	/*
800645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
801df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
802df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
803df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
804df8bae1dSRodney W. Grimes 	 * to indicate this.
805df8bae1dSRodney W. Grimes 	 */
806fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
807628d2653SJohn Baldwin 	PROC_LOCK(p);
808628d2653SJohn Baldwin 	ps = p->p_sigacts;
8096626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
810645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
811645682fdSLuoqi Chen 
812645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
8132c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
814628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
8152c42a146SMarcel Moolenaar 		/* void */;
816628d2653SJohn Baldwin 	PROC_UNLOCK(p);
817fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
8182c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
8192c42a146SMarcel Moolenaar 	return (EINTR);
8202c42a146SMarcel Moolenaar }
8212c42a146SMarcel Moolenaar 
82231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8232c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8242c42a146SMarcel Moolenaar struct osigsuspend_args {
8252c42a146SMarcel Moolenaar 	osigset_t mask;
8262c42a146SMarcel Moolenaar };
8272c42a146SMarcel Moolenaar #endif
828fb99ab88SMatthew Dillon /*
829fb99ab88SMatthew Dillon  * MPSAFE
830fb99ab88SMatthew Dillon  */
8312c42a146SMarcel Moolenaar /* ARGSUSED */
8322c42a146SMarcel Moolenaar int
833b40ce416SJulian Elischer osigsuspend(td, uap)
834b40ce416SJulian Elischer 	struct thread *td;
8352c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
8362c42a146SMarcel Moolenaar {
837b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
838645682fdSLuoqi Chen 	sigset_t mask;
839628d2653SJohn Baldwin 	register struct sigacts *ps;
8402c42a146SMarcel Moolenaar 
841fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
842628d2653SJohn Baldwin 	PROC_LOCK(p);
843628d2653SJohn Baldwin 	ps = p->p_sigacts;
8442c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
845645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
846645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
847645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
848645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
849628d2653SJohn Baldwin 	while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
850df8bae1dSRodney W. Grimes 		/* void */;
851628d2653SJohn Baldwin 	PROC_UNLOCK(p);
852fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
853df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
854df8bae1dSRodney W. Grimes 	return (EINTR);
855df8bae1dSRodney W. Grimes }
85631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
857df8bae1dSRodney W. Grimes 
858df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
859d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
860df8bae1dSRodney W. Grimes struct osigstack_args {
861df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
862df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
863df8bae1dSRodney W. Grimes };
864d2d3e875SBruce Evans #endif
865fb99ab88SMatthew Dillon /*
866fb99ab88SMatthew Dillon  * MPSAFE
867fb99ab88SMatthew Dillon  */
868df8bae1dSRodney W. Grimes /* ARGSUSED */
86926f9a767SRodney W. Grimes int
870b40ce416SJulian Elischer osigstack(td, uap)
871b40ce416SJulian Elischer 	struct thread *td;
872df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
873df8bae1dSRodney W. Grimes {
874b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
875df8bae1dSRodney W. Grimes 	struct sigstack ss;
876fb99ab88SMatthew Dillon 	int error = 0;
877fb99ab88SMatthew Dillon 
878fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
879df8bae1dSRodney W. Grimes 
880d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
881628d2653SJohn Baldwin 		PROC_LOCK(p);
882645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
883b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
884628d2653SJohn Baldwin 		PROC_UNLOCK(p);
885d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
886d034d459SMarcel Moolenaar 		if (error)
887fb99ab88SMatthew Dillon 			goto done2;
888d034d459SMarcel Moolenaar 	}
889d034d459SMarcel Moolenaar 
890d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
891d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
892fb99ab88SMatthew Dillon 			goto done2;
893628d2653SJohn Baldwin 		PROC_LOCK(p);
894645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
895645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
896645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
897645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
898628d2653SJohn Baldwin 		PROC_UNLOCK(p);
899df8bae1dSRodney W. Grimes 	}
900fb99ab88SMatthew Dillon done2:
901fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
902fb99ab88SMatthew Dillon 	return (error);
903df8bae1dSRodney W. Grimes }
904df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
905df8bae1dSRodney W. Grimes 
906d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
907df8bae1dSRodney W. Grimes struct sigaltstack_args {
9082c42a146SMarcel Moolenaar 	stack_t	*ss;
9092c42a146SMarcel Moolenaar 	stack_t	*oss;
910df8bae1dSRodney W. Grimes };
911d2d3e875SBruce Evans #endif
912fb99ab88SMatthew Dillon /*
913fb99ab88SMatthew Dillon  * MPSAFE
914fb99ab88SMatthew Dillon  */
915df8bae1dSRodney W. Grimes /* ARGSUSED */
91626f9a767SRodney W. Grimes int
917b40ce416SJulian Elischer sigaltstack(td, uap)
918b40ce416SJulian Elischer 	struct thread *td;
919df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
920df8bae1dSRodney W. Grimes {
921b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
9222c42a146SMarcel Moolenaar 	stack_t ss;
923fb99ab88SMatthew Dillon 	int oonstack;
924fb99ab88SMatthew Dillon 	int error = 0;
925fb99ab88SMatthew Dillon 
926fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
927df8bae1dSRodney W. Grimes 
928b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
929d034d459SMarcel Moolenaar 
930d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
931628d2653SJohn Baldwin 		PROC_LOCK(p);
932d034d459SMarcel Moolenaar 		ss = p->p_sigstk;
933d034d459SMarcel Moolenaar 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
934d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
935628d2653SJohn Baldwin 		PROC_UNLOCK(p);
936d034d459SMarcel Moolenaar 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
937fb99ab88SMatthew Dillon 			goto done2;
938df8bae1dSRodney W. Grimes 	}
939d034d459SMarcel Moolenaar 
940d034d459SMarcel Moolenaar 	if (uap->ss != NULL) {
941fb99ab88SMatthew Dillon 		if (oonstack) {
942fb99ab88SMatthew Dillon 			error = EPERM;
943fb99ab88SMatthew Dillon 			goto done2;
944fb99ab88SMatthew Dillon 		}
945d034d459SMarcel Moolenaar 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
946fb99ab88SMatthew Dillon 			goto done2;
947fb99ab88SMatthew Dillon 		if ((ss.ss_flags & ~SS_DISABLE) != 0) {
948fb99ab88SMatthew Dillon 			error = EINVAL;
949fb99ab88SMatthew Dillon 			goto done2;
950fb99ab88SMatthew Dillon 		}
951d034d459SMarcel Moolenaar 		if (!(ss.ss_flags & SS_DISABLE)) {
952fb99ab88SMatthew Dillon 			if (ss.ss_size < p->p_sysent->sv_minsigstksz) {
953fb99ab88SMatthew Dillon 				error = ENOMEM;
954fb99ab88SMatthew Dillon 				goto done2;
955fb99ab88SMatthew Dillon 			}
956628d2653SJohn Baldwin 			PROC_LOCK(p);
957645682fdSLuoqi Chen 			p->p_sigstk = ss;
958d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
959628d2653SJohn Baldwin 			PROC_UNLOCK(p);
960628d2653SJohn Baldwin 		} else {
961628d2653SJohn Baldwin 			PROC_LOCK(p);
962d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
963628d2653SJohn Baldwin 			PROC_UNLOCK(p);
964628d2653SJohn Baldwin 		}
965d034d459SMarcel Moolenaar 	}
966fb99ab88SMatthew Dillon done2:
967fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
968fb99ab88SMatthew Dillon 	return (error);
969df8bae1dSRodney W. Grimes }
970df8bae1dSRodney W. Grimes 
971d93f860cSPoul-Henning Kamp /*
972d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
973d93f860cSPoul-Henning Kamp  * cp is calling process.
974d93f860cSPoul-Henning Kamp  */
975d93f860cSPoul-Henning Kamp int
9762c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
977d93f860cSPoul-Henning Kamp 	register struct proc *cp;
9782c42a146SMarcel Moolenaar 	int sig, pgid, all;
979d93f860cSPoul-Henning Kamp {
980d93f860cSPoul-Henning Kamp 	register struct proc *p;
981d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
982d93f860cSPoul-Henning Kamp 	int nfound = 0;
983d93f860cSPoul-Henning Kamp 
984553629ebSJake Burkholder 	if (all) {
985d93f860cSPoul-Henning Kamp 		/*
986d93f860cSPoul-Henning Kamp 		 * broadcast
987d93f860cSPoul-Henning Kamp 		 */
9881005a129SJohn Baldwin 		sx_slock(&allproc_lock);
9892e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
990628d2653SJohn Baldwin 			PROC_LOCK(p);
991628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) {
992628d2653SJohn Baldwin 				PROC_UNLOCK(p);
993628d2653SJohn Baldwin 				continue;
994628d2653SJohn Baldwin 			}
99533a9ed9dSJohn Baldwin 			if (p_cansignal(cp, p, sig) == 0) {
996d93f860cSPoul-Henning Kamp 				nfound++;
99733a9ed9dSJohn Baldwin 				if (sig)
9982c42a146SMarcel Moolenaar 					psignal(p, sig);
999628d2653SJohn Baldwin 			}
100033a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1001d93f860cSPoul-Henning Kamp 		}
10021005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1003553629ebSJake Burkholder 	} else {
1004f591779bSSeigo Tanimura 		PGRPSESS_SLOCK();
1005f591779bSSeigo Tanimura 		if (pgid == 0) {
1006d93f860cSPoul-Henning Kamp 			/*
1007d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1008d93f860cSPoul-Henning Kamp 			 */
1009d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
1010f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1011f591779bSSeigo Tanimura 		} else {
1012d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1013f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1014f591779bSSeigo Tanimura 				PGRPSESS_SUNLOCK();
1015d93f860cSPoul-Henning Kamp 				return (ESRCH);
1016d93f860cSPoul-Henning Kamp 			}
1017f591779bSSeigo Tanimura 		}
1018f591779bSSeigo Tanimura 		PGRPSESS_SUNLOCK();
10192e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1020628d2653SJohn Baldwin 			PROC_LOCK(p);
1021628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1022628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1023628d2653SJohn Baldwin 				continue;
1024628d2653SJohn Baldwin 			}
1025628d2653SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1026628d2653SJohn Baldwin 			if (p->p_stat == SZOMB) {
1027628d2653SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
102833a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1029628d2653SJohn Baldwin 				continue;
1030628d2653SJohn Baldwin 			}
1031628d2653SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
103233a9ed9dSJohn Baldwin 			if (p_cansignal(cp, p, sig) == 0) {
1033d93f860cSPoul-Henning Kamp 				nfound++;
103433a9ed9dSJohn Baldwin 				if (sig)
10352c42a146SMarcel Moolenaar 					psignal(p, sig);
1036628d2653SJohn Baldwin 			}
103733a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1038d93f860cSPoul-Henning Kamp 		}
1039f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1040d93f860cSPoul-Henning Kamp 	}
1041d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1042d93f860cSPoul-Henning Kamp }
1043d93f860cSPoul-Henning Kamp 
1044d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1045df8bae1dSRodney W. Grimes struct kill_args {
1046df8bae1dSRodney W. Grimes 	int	pid;
1047df8bae1dSRodney W. Grimes 	int	signum;
1048df8bae1dSRodney W. Grimes };
1049d2d3e875SBruce Evans #endif
1050fb99ab88SMatthew Dillon /*
1051fb99ab88SMatthew Dillon  * MPSAFE
1052fb99ab88SMatthew Dillon  */
1053df8bae1dSRodney W. Grimes /* ARGSUSED */
105426f9a767SRodney W. Grimes int
1055b40ce416SJulian Elischer kill(td, uap)
1056b40ce416SJulian Elischer 	register struct thread *td;
1057df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1058df8bae1dSRodney W. Grimes {
1059b40ce416SJulian Elischer 	register struct proc *cp = td->td_proc;
1060df8bae1dSRodney W. Grimes 	register struct proc *p;
1061fb99ab88SMatthew Dillon 	int error = 0;
1062df8bae1dSRodney W. Grimes 
10636c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1064df8bae1dSRodney W. Grimes 		return (EINVAL);
1065fb99ab88SMatthew Dillon 
1066fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1067df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1068df8bae1dSRodney W. Grimes 		/* kill single process */
1069fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1070fb99ab88SMatthew Dillon 			error = ESRCH;
1071fb99ab88SMatthew Dillon 		} else if (p_cansignal(cp, p, uap->signum)) {
107233a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1073fb99ab88SMatthew Dillon 			error = EPERM;
1074fb99ab88SMatthew Dillon 		} else {
107533a9ed9dSJohn Baldwin 			if (uap->signum)
1076df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1077628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1078fb99ab88SMatthew Dillon 			error = 0;
1079df8bae1dSRodney W. Grimes 		}
1080fb99ab88SMatthew Dillon 	} else {
1081df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1082df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
1083fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, 0, 1);
1084fb99ab88SMatthew Dillon 			break;
1085df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
1086fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, 0, 0);
1087fb99ab88SMatthew Dillon 			break;
1088df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
1089fb99ab88SMatthew Dillon 			error = killpg1(cp, uap->signum, -uap->pid, 0);
1090fb99ab88SMatthew Dillon 			break;
1091df8bae1dSRodney W. Grimes 		}
1092fb99ab88SMatthew Dillon 	}
1093fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1094fb99ab88SMatthew Dillon 	return(error);
1095df8bae1dSRodney W. Grimes }
1096df8bae1dSRodney W. Grimes 
1097df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1098d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1099df8bae1dSRodney W. Grimes struct okillpg_args {
1100df8bae1dSRodney W. Grimes 	int	pgid;
1101df8bae1dSRodney W. Grimes 	int	signum;
1102df8bae1dSRodney W. Grimes };
1103d2d3e875SBruce Evans #endif
1104fb99ab88SMatthew Dillon /*
1105fb99ab88SMatthew Dillon  * MPSAFE
1106fb99ab88SMatthew Dillon  */
1107df8bae1dSRodney W. Grimes /* ARGSUSED */
110826f9a767SRodney W. Grimes int
1109b40ce416SJulian Elischer okillpg(td, uap)
1110b40ce416SJulian Elischer 	struct thread *td;
1111df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1112df8bae1dSRodney W. Grimes {
1113fb99ab88SMatthew Dillon 	int error;
1114df8bae1dSRodney W. Grimes 
11156c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1116df8bae1dSRodney W. Grimes 		return (EINVAL);
1117fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1118b40ce416SJulian Elischer 	error = killpg1(td->td_proc, uap->signum, uap->pgid, 0);
1119fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1120fb99ab88SMatthew Dillon 	return (error);
1121df8bae1dSRodney W. Grimes }
1122df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1123df8bae1dSRodney W. Grimes 
1124df8bae1dSRodney W. Grimes /*
1125df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1126df8bae1dSRodney W. Grimes  */
1127df8bae1dSRodney W. Grimes void
11282c42a146SMarcel Moolenaar gsignal(pgid, sig)
11292c42a146SMarcel Moolenaar 	int pgid, sig;
1130df8bae1dSRodney W. Grimes {
1131df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1132df8bae1dSRodney W. Grimes 
1133f591779bSSeigo Tanimura 	if (pgid != 0) {
1134f591779bSSeigo Tanimura 		PGRPSESS_SLOCK();
1135f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1136f591779bSSeigo Tanimura 		PGRPSESS_SUNLOCK();
1137f591779bSSeigo Tanimura 		if (pgrp != NULL) {
11382c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1139f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1140f591779bSSeigo Tanimura 		}
1141f591779bSSeigo Tanimura 	}
1142df8bae1dSRodney W. Grimes }
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes /*
1145df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1146df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1147df8bae1dSRodney W. Grimes  */
1148df8bae1dSRodney W. Grimes void
11492c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1150df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
11512c42a146SMarcel Moolenaar 	int sig, checkctty;
1152df8bae1dSRodney W. Grimes {
1153df8bae1dSRodney W. Grimes 	register struct proc *p;
1154df8bae1dSRodney W. Grimes 
1155628d2653SJohn Baldwin 	if (pgrp) {
1156f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1157628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1158628d2653SJohn Baldwin 			PROC_LOCK(p);
1159df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
11602c42a146SMarcel Moolenaar 				psignal(p, sig);
1161628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1162628d2653SJohn Baldwin 		}
1163628d2653SJohn Baldwin 	}
1164df8bae1dSRodney W. Grimes }
1165df8bae1dSRodney W. Grimes 
1166df8bae1dSRodney W. Grimes /*
1167df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1168df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1169df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1170356861dbSMatthew Dillon  *
1171356861dbSMatthew Dillon  * MPSAFE
1172df8bae1dSRodney W. Grimes  */
1173df8bae1dSRodney W. Grimes void
11742c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1175df8bae1dSRodney W. Grimes 	struct proc *p;
11762c42a146SMarcel Moolenaar 	register int sig;
11778674077aSJeffrey Hsu 	u_long code;
1178df8bae1dSRodney W. Grimes {
1179df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1180df8bae1dSRodney W. Grimes 
1181356861dbSMatthew Dillon 	mtx_lock(&Giant);
1182628d2653SJohn Baldwin 	PROC_LOCK(p);
11832c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1184d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1185df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1186df8bae1dSRodney W. Grimes #ifdef KTRACE
1187df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
11882c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
11892c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
1190df8bae1dSRodney W. Grimes #endif
11912c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
11922c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
11932c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
11942c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
11952c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
11962c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1197289ccde0SPeter Wemm 			/*
11982c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1199289ccde0SPeter Wemm 			 */
12002c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
12012c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
12022c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
12032c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
12042c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1205dedc04feSPeter Wemm 		}
12066f841fb7SMarcel Moolenaar 	} else {
12076626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
12082c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
12092c42a146SMarcel Moolenaar 		psignal(p, sig);
1210df8bae1dSRodney W. Grimes 	}
1211628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1212356861dbSMatthew Dillon 	mtx_unlock(&Giant);
1213df8bae1dSRodney W. Grimes }
1214df8bae1dSRodney W. Grimes 
1215df8bae1dSRodney W. Grimes /*
1216df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1217df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1218df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1219df8bae1dSRodney W. Grimes  *
1220df8bae1dSRodney W. Grimes  * Exceptions:
1221df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1222df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1223df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1224df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1225df8bae1dSRodney W. Grimes  *
1226df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1227df8bae1dSRodney W. Grimes  */
1228df8bae1dSRodney W. Grimes void
12292c42a146SMarcel Moolenaar psignal(p, sig)
1230df8bae1dSRodney W. Grimes 	register struct proc *p;
12312c42a146SMarcel Moolenaar 	register int sig;
1232df8bae1dSRodney W. Grimes {
123340447cd4SJohn Baldwin 	register int prop;
1234df8bae1dSRodney W. Grimes 	register sig_t action;
1235b40ce416SJulian Elischer 	struct thread *td;
12360f5c7c4bSPoul-Henning Kamp #ifdef SMP
1237b40ce416SJulian Elischer 	struct ksegrp *kg;
12380f5c7c4bSPoul-Henning Kamp #endif
1239df8bae1dSRodney W. Grimes 
12402899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
12412899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
12422c42a146SMarcel Moolenaar 
1243628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1244cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1245cb679c38SJonathan Lemon 
12462c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1247df8bae1dSRodney W. Grimes 
1248df8bae1dSRodney W. Grimes 	/*
12492a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
12502a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
12512a024a2bSSean Eric Fagan 	 * a chance, as well.
1252df8bae1dSRodney W. Grimes 	 */
1253b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1254df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1255b40ce416SJulian Elischer 	} else {
1256df8bae1dSRodney W. Grimes 		/*
1257df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1258df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1259df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1260df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1261df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1262df8bae1dSRodney W. Grimes 		 */
1263628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1264df8bae1dSRodney W. Grimes 			return;
12652c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1266df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
12672c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1268df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1269df8bae1dSRodney W. Grimes 		else
1270df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1271df8bae1dSRodney W. Grimes 	}
1272df8bae1dSRodney W. Grimes 
1273b40ce416SJulian Elischer 	/*
1274b40ce416SJulian Elischer 	 * bring the priority of a process up if we want it to get
1275b40ce416SJulian Elischer 	 * killed in this lifetime.
1276b40ce416SJulian Elischer 	 * XXXKSE think if a better way to do this.
1277b40ce416SJulian Elischer 	 *
1278b40ce416SJulian Elischer 	 * What we need to do is see if there is a thread that will
1279b40ce416SJulian Elischer 	 * be able to accept the signal. e.g.
1280b40ce416SJulian Elischer 	 * FOREACH_THREAD_IN_PROC() {
1281b40ce416SJulian Elischer 	 *	if runnable, we're done
1282b40ce416SJulian Elischer 	 *	else pick one at random.
1283b40ce416SJulian Elischer 	 * }
1284b40ce416SJulian Elischer 	 */
1285b40ce416SJulian Elischer 	/* XXXKSE
1286b40ce416SJulian Elischer 	 * For now there is one thread per proc.
1287b40ce416SJulian Elischer 	 * Effectively select one sucker thread..
1288b40ce416SJulian Elischer 	 */
1289079b7badSJulian Elischer 	td = FIRST_THREAD_IN_PROC(p);
12909ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1291b40ce416SJulian Elischer 	if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) &&
1292b40ce416SJulian Elischer 	    (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0))
1293b40ce416SJulian Elischer 		p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */
12949ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
1295df8bae1dSRodney W. Grimes 
1296df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
12972c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1298df8bae1dSRodney W. Grimes 
1299df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1300df8bae1dSRodney W. Grimes 		/*
1301df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1302df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1303df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1304df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1305df8bae1dSRodney W. Grimes 		 */
1306df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1307628d2653SJohn Baldwin 		    action == SIG_DFL)
1308df8bae1dSRodney W. Grimes 		        return;
13092c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1310df8bae1dSRodney W. Grimes 	}
13112c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1312df8bae1dSRodney W. Grimes 
1313df8bae1dSRodney W. Grimes 	/*
1314df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1315df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1316df8bae1dSRodney W. Grimes 	 */
13179ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
13181c32c37cSJohn Baldwin 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
13199ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1320df8bae1dSRodney W. Grimes 		return;
13211c32c37cSJohn Baldwin 	}
1322b40ce416SJulian Elischer 
1323df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1324df8bae1dSRodney W. Grimes 
1325df8bae1dSRodney W. Grimes 	case SSLEEP:
1326df8bae1dSRodney W. Grimes 		/*
1327df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1328df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1329df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1330df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1331df8bae1dSRodney W. Grimes 		 */
1332b40ce416SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
13339ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1334df8bae1dSRodney W. Grimes 			goto out;
13353e6831f5SJohn Baldwin 		}
1336df8bae1dSRodney W. Grimes 		/*
1337df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1338df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1339df8bae1dSRodney W. Grimes 		 * for the parent.
1340df8bae1dSRodney W. Grimes 		 */
1341df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1342df8bae1dSRodney W. Grimes 			goto run;
13439ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1344df8bae1dSRodney W. Grimes 		/*
1345df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1346df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1347df8bae1dSRodney W. Grimes 		 * be awakened.
1348df8bae1dSRodney W. Grimes 		 */
1349df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
13502c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1351df8bae1dSRodney W. Grimes 			goto out;
1352df8bae1dSRodney W. Grimes 		}
1353df8bae1dSRodney W. Grimes 		/*
1354df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1355df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1356df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1357df8bae1dSRodney W. Grimes 		 * cause the process to run.
1358df8bae1dSRodney W. Grimes 		 */
1359df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
13603e6831f5SJohn Baldwin 			if (action != SIG_DFL)
1361df8bae1dSRodney W. Grimes 				goto runfast;
1362df8bae1dSRodney W. Grimes 			/*
1363df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1364df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1365df8bae1dSRodney W. Grimes 			 */
1366df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1367df8bae1dSRodney W. Grimes 				goto out;
13682c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
13692c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1370628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1371f591779bSSeigo Tanimura 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1372df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1373628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
13745b3047d5SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1375df8bae1dSRodney W. Grimes 			stop(p);
13765b3047d5SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
1377df8bae1dSRodney W. Grimes 			goto out;
13783e6831f5SJohn Baldwin 		} else
1379df8bae1dSRodney W. Grimes 			goto runfast;
1380df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1381df8bae1dSRodney W. Grimes 
1382df8bae1dSRodney W. Grimes 	case SSTOP:
13839ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1384df8bae1dSRodney W. Grimes 		/*
1385df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1386df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1387df8bae1dSRodney W. Grimes 		 */
138840447cd4SJohn Baldwin 		if (p->p_flag & P_TRACED)
1389df8bae1dSRodney W. Grimes 			goto out;
1390df8bae1dSRodney W. Grimes 
1391df8bae1dSRodney W. Grimes 		/*
1392df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1393df8bae1dSRodney W. Grimes 		 */
13942c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1395df8bae1dSRodney W. Grimes 			goto runfast;
1396df8bae1dSRodney W. Grimes 
1397df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1398df8bae1dSRodney W. Grimes 			/*
1399df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1400df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1401df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1402df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1403df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1404df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1405df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1406df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1407df8bae1dSRodney W. Grimes 			 */
1408df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
14092c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1410df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1411df8bae1dSRodney W. Grimes 				goto runfast;
14129ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1413b40ce416SJulian Elischer 			/*
1414b40ce416SJulian Elischer 			 * XXXKSE
1415b40ce416SJulian Elischer 			 * do this for each thread.
1416b40ce416SJulian Elischer 			 */
1417b40ce416SJulian Elischer 			if (p->p_flag & P_KSES) {
1418b40ce416SJulian Elischer 				mtx_assert(&sched_lock,
1419b40ce416SJulian Elischer 				    MA_OWNED | MA_NOTRECURSED);
1420b40ce416SJulian Elischer 				FOREACH_THREAD_IN_PROC(p, td) {
1421b40ce416SJulian Elischer 					if (td->td_wchan == NULL) {
1422b40ce416SJulian Elischer 						setrunnable(td); /* XXXKSE */
1423b40ce416SJulian Elischer 					} else {
1424b40ce416SJulian Elischer 						/* mark it as sleeping */
1425b40ce416SJulian Elischer 					}
1426b40ce416SJulian Elischer 				}
1427b40ce416SJulian Elischer 				mtx_unlock_spin(&sched_lock);
1428b40ce416SJulian Elischer 			} else {
1429079b7badSJulian Elischer 				if (td->td_wchan == NULL)
1430df8bae1dSRodney W. Grimes 					goto run;
1431df8bae1dSRodney W. Grimes 				p->p_stat = SSLEEP;
14329ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
1433b40ce416SJulian Elischer 			}
1434fdd4e5c6SJulian Elischer 			goto out;
1435df8bae1dSRodney W. Grimes 		}
1436df8bae1dSRodney W. Grimes 
1437df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1438df8bae1dSRodney W. Grimes 			/*
1439df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1440df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1441df8bae1dSRodney W. Grimes 			 */
14422c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1443df8bae1dSRodney W. Grimes 			goto out;
1444df8bae1dSRodney W. Grimes 		}
1445df8bae1dSRodney W. Grimes 
1446df8bae1dSRodney W. Grimes 		/*
1447df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1448df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1449df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1450df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1451b40ce416SJulian Elischer 		 * XXXKSE should we wake ALL blocked threads?
1452df8bae1dSRodney W. Grimes 		 */
14539ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
1454b40ce416SJulian Elischer 		if (p->p_flag & P_KSES) {
1455b40ce416SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1456b40ce416SJulian Elischer 				if (td->td_wchan && (td->td_flags & TDF_SINTR)){
1457b40ce416SJulian Elischer 					if (td->td_flags & TDF_CVWAITQ)
1458b40ce416SJulian Elischer 						cv_waitq_remove(td);
1459238510fcSJason Evans 					else
1460b40ce416SJulian Elischer 						unsleep(td); /* XXXKSE */
1461b40ce416SJulian Elischer 				}
1462b40ce416SJulian Elischer 			}
1463b40ce416SJulian Elischer 		} else {
1464b40ce416SJulian Elischer 			if (td->td_wchan && td->td_flags & TDF_SINTR) {
1465b40ce416SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
1466b40ce416SJulian Elischer 					cv_waitq_remove(td);
1467b40ce416SJulian Elischer 				else
1468b40ce416SJulian Elischer 					unsleep(td); /* XXXKSE */
1469b40ce416SJulian Elischer 			}
1470238510fcSJason Evans 		}
14719ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1472df8bae1dSRodney W. Grimes 		goto out;
1473df8bae1dSRodney W. Grimes 
1474df8bae1dSRodney W. Grimes 	default:
1475df8bae1dSRodney W. Grimes 		/*
1476df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1477df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1478df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1479df8bae1dSRodney W. Grimes 		 */
1480d2ef4060SBruce Evans 		if (p->p_stat == SRUN) {
14813163861cSTor Egge #ifdef SMP
1482b40ce416SJulian Elischer 			struct kse *ke;
1483b40ce416SJulian Elischer 			struct thread *td = curthread;
1484b40ce416SJulian Elischer 			signotify(&p->p_kse);  /* XXXKSE */
1485b40ce416SJulian Elischer /* we should only deliver to one thread.. but which one? */
1486b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(p, kg) {
1487b40ce416SJulian Elischer 				FOREACH_KSE_IN_GROUP(kg, ke) {
1488b40ce416SJulian Elischer 					if (ke->ke_thread == td) {
1489b40ce416SJulian Elischer 						continue;
1490b40ce416SJulian Elischer 					}
1491b40ce416SJulian Elischer 					forward_signal(ke->ke_thread);
1492b40ce416SJulian Elischer 				}
1493b40ce416SJulian Elischer 			}
1494b40ce416SJulian Elischer #else
1495b40ce416SJulian Elischer 			signotify(&p->p_kse);  /* XXXKSE */
14963163861cSTor Egge #endif
14976caa8a15SJohn Baldwin 		}
14989ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1499df8bae1dSRodney W. Grimes 		goto out;
1500df8bae1dSRodney W. Grimes 	}
1501df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes runfast:
1504df8bae1dSRodney W. Grimes 	/*
1505df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1506b40ce416SJulian Elischer 	 * XXXKSE Should we make them all run fast?
1507b40ce416SJulian Elischer 	 * Maybe just one would be enough?
1508df8bae1dSRodney W. Grimes 	 */
15099ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
15102c100766SJulian Elischer 
15112c100766SJulian Elischer 	if (FIRST_THREAD_IN_PROC(p)->td_priority > PUSER) {
15122c100766SJulian Elischer 		FIRST_THREAD_IN_PROC(p)->td_priority = PUSER;
1513b40ce416SJulian Elischer 	}
1514df8bae1dSRodney W. Grimes run:
15153e6831f5SJohn Baldwin 	/* If we jump here, sched_lock has to be owned. */
15163e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1517b40ce416SJulian Elischer 	setrunnable(td); /* XXXKSE */
15189ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
15193e6831f5SJohn Baldwin out:
15203e6831f5SJohn Baldwin 	/* If we jump here, sched_lock should not be owned. */
15213e6831f5SJohn Baldwin 	mtx_assert(&sched_lock, MA_NOTOWNED);
1522df8bae1dSRodney W. Grimes }
1523df8bae1dSRodney W. Grimes 
1524df8bae1dSRodney W. Grimes /*
1525df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1526df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1527df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1528df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1529df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1530df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1531df8bae1dSRodney W. Grimes  * sequence is
1532df8bae1dSRodney W. Grimes  *
15332c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
15342c42a146SMarcel Moolenaar  *		postsig(sig);
1535df8bae1dSRodney W. Grimes  */
153626f9a767SRodney W. Grimes int
1537df8bae1dSRodney W. Grimes issignal(p)
1538df8bae1dSRodney W. Grimes 	register struct proc *p;
1539df8bae1dSRodney W. Grimes {
15402c42a146SMarcel Moolenaar 	sigset_t mask;
15412c42a146SMarcel Moolenaar 	register int sig, prop;
1542df8bae1dSRodney W. Grimes 
1543628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1544df8bae1dSRodney W. Grimes 	for (;;) {
15452a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
15462a024a2bSSean Eric Fagan 
15472c42a146SMarcel Moolenaar 		mask = p->p_siglist;
15482c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1549df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
15502c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
15512c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1552df8bae1dSRodney W. Grimes 			return (0);
15532c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
15542c42a146SMarcel Moolenaar 		prop = sigprop(sig);
15552a024a2bSSean Eric Fagan 
1556628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
15572a024a2bSSean Eric Fagan 
1558df8bae1dSRodney W. Grimes 		/*
1559df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1560df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1561df8bae1dSRodney W. Grimes 		 */
15622c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
15632c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1564df8bae1dSRodney W. Grimes 			continue;
1565df8bae1dSRodney W. Grimes 		}
1566df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1567df8bae1dSRodney W. Grimes 			/*
1568df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1569df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1570df8bae1dSRodney W. Grimes 			 */
15712c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1572628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1573df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1574628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
1575df8bae1dSRodney W. Grimes 			do {
15769ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
15775b3047d5SJohn Baldwin 				stop(p);
1578c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1579c86b6ff5SJohn Baldwin 				DROP_GIANT();
15802ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1581df8bae1dSRodney W. Grimes 				mi_switch();
15829ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
158320cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1584628d2653SJohn Baldwin 				PROC_LOCK(p);
15852a024a2bSSean Eric Fagan 			} while (!trace_req(p)
15862a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1587df8bae1dSRodney W. Grimes 
1588df8bae1dSRodney W. Grimes 			/*
1589df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1590df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1591df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1592df8bae1dSRodney W. Grimes 			 */
1593df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1594df8bae1dSRodney W. Grimes 				continue;
1595df8bae1dSRodney W. Grimes 
1596df8bae1dSRodney W. Grimes 			/*
1597df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1598df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1599df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1600df8bae1dSRodney W. Grimes 			 */
16012c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
16022c42a146SMarcel Moolenaar 			sig = p->p_xstat;
16032c42a146SMarcel Moolenaar 			if (sig == 0)
1604df8bae1dSRodney W. Grimes 				continue;
1605df8bae1dSRodney W. Grimes 
1606df8bae1dSRodney W. Grimes 			/*
1607df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1608df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1609df8bae1dSRodney W. Grimes 			 */
16102c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
16112c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1612df8bae1dSRodney W. Grimes 				continue;
1613df8bae1dSRodney W. Grimes 		}
1614df8bae1dSRodney W. Grimes 
1615df8bae1dSRodney W. Grimes 		/*
1616df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1617df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1618df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1619df8bae1dSRodney W. Grimes 		 */
16202c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1621df8bae1dSRodney W. Grimes 
16220b53fbe8SBruce Evans 		case (int)SIG_DFL:
1623df8bae1dSRodney W. Grimes 			/*
1624df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1625df8bae1dSRodney W. Grimes 			 */
1626df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1627df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1628df8bae1dSRodney W. Grimes 				/*
1629df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1630df8bae1dSRodney W. Grimes 				 * in init? XXX
1631df8bae1dSRodney W. Grimes 				 */
1632d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
16332c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1634df8bae1dSRodney W. Grimes #endif
1635df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1636df8bae1dSRodney W. Grimes 			}
1637df8bae1dSRodney W. Grimes 			/*
1638df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1639df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1640df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1641df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1642df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1643df8bae1dSRodney W. Grimes 			 */
1644df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1645df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1646df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1647df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
1648df8bae1dSRodney W. Grimes 					break;	/* == ignore */
16492c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1650628d2653SJohn Baldwin 				PROC_LOCK(p->p_pptr);
1651f591779bSSeigo Tanimura 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1652df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1653628d2653SJohn Baldwin 				PROC_UNLOCK(p->p_pptr);
16549ed346baSBosko Milekic 				mtx_lock_spin(&sched_lock);
16555b3047d5SJohn Baldwin 				stop(p);
1656c86b6ff5SJohn Baldwin 				PROC_UNLOCK(p);
1657c86b6ff5SJohn Baldwin 				DROP_GIANT();
16582ad7d304SJohn Baldwin 				p->p_stats->p_ru.ru_nivcsw++;
1659df8bae1dSRodney W. Grimes 				mi_switch();
16609ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
166120cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1662628d2653SJohn Baldwin 				PROC_LOCK(p);
1663df8bae1dSRodney W. Grimes 				break;
1664df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1665df8bae1dSRodney W. Grimes 				/*
1666df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1667df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1668df8bae1dSRodney W. Grimes 				 */
1669df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1670df8bae1dSRodney W. Grimes 			} else
16712c42a146SMarcel Moolenaar 				return (sig);
1672df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1673df8bae1dSRodney W. Grimes 
16740b53fbe8SBruce Evans 		case (int)SIG_IGN:
1675df8bae1dSRodney W. Grimes 			/*
1676df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1677df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1678df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1679df8bae1dSRodney W. Grimes 			 */
1680df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1681df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1682df8bae1dSRodney W. Grimes 				printf("issignal\n");
1683df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1684df8bae1dSRodney W. Grimes 
1685df8bae1dSRodney W. Grimes 		default:
1686df8bae1dSRodney W. Grimes 			/*
1687df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1688df8bae1dSRodney W. Grimes 			 * postsig() process it.
1689df8bae1dSRodney W. Grimes 			 */
16902c42a146SMarcel Moolenaar 			return (sig);
1691df8bae1dSRodney W. Grimes 		}
16922c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1693df8bae1dSRodney W. Grimes 	}
1694df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1695df8bae1dSRodney W. Grimes }
1696df8bae1dSRodney W. Grimes 
1697df8bae1dSRodney W. Grimes /*
1698df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1699df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
17005b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
17015b3047d5SJohn Baldwin  * lock held.
1702df8bae1dSRodney W. Grimes  */
17035b3047d5SJohn Baldwin static void
1704df8bae1dSRodney W. Grimes stop(p)
1705df8bae1dSRodney W. Grimes 	register struct proc *p;
1706df8bae1dSRodney W. Grimes {
1707df8bae1dSRodney W. Grimes 
1708628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
17095b3047d5SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
1710df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1711df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1712df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1713df8bae1dSRodney W. Grimes }
1714df8bae1dSRodney W. Grimes 
1715df8bae1dSRodney W. Grimes /*
1716df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1717df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1718df8bae1dSRodney W. Grimes  */
1719df8bae1dSRodney W. Grimes void
17202c42a146SMarcel Moolenaar postsig(sig)
17212c42a146SMarcel Moolenaar 	register int sig;
1722df8bae1dSRodney W. Grimes {
1723b40ce416SJulian Elischer 	struct thread *td = curthread;
1724b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1725628d2653SJohn Baldwin 	struct sigacts *ps;
17262c42a146SMarcel Moolenaar 	sig_t action;
17272c42a146SMarcel Moolenaar 	sigset_t returnmask;
17282c42a146SMarcel Moolenaar 	int code;
1729df8bae1dSRodney W. Grimes 
17302c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
17315526d2d9SEivind Eklund 
17322ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1733628d2653SJohn Baldwin 	ps = p->p_sigacts;
17342c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
17352c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1736df8bae1dSRodney W. Grimes #ifdef KTRACE
1737df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1738645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
17392c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1740df8bae1dSRodney W. Grimes #endif
1741628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
17422a024a2bSSean Eric Fagan 
1743df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1744df8bae1dSRodney W. Grimes 		/*
1745df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1746df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1747df8bae1dSRodney W. Grimes 		 */
1748b40ce416SJulian Elischer 		sigexit(td, sig);
1749df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1750df8bae1dSRodney W. Grimes 	} else {
1751df8bae1dSRodney W. Grimes 		/*
1752df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1753df8bae1dSRodney W. Grimes 		 */
17542c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
17555526d2d9SEivind Eklund 		    ("postsig action"));
1756df8bae1dSRodney W. Grimes 		/*
1757df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1758645682fdSLuoqi Chen 		 * occurrences of this signal.
1759df8bae1dSRodney W. Grimes 		 *
1760645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1761df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1762645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1763df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1764df8bae1dSRodney W. Grimes 		 */
1765645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
17666626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1767645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1768df8bae1dSRodney W. Grimes 		} else
1769df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
17702c42a146SMarcel Moolenaar 
17712c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
17722c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
17732c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
17742c42a146SMarcel Moolenaar 
17752c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1776289ccde0SPeter Wemm 			/*
17772c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1778289ccde0SPeter Wemm 			 */
17792c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
17802c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
17812c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
17822c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
17832c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1784dedc04feSPeter Wemm 		}
1785df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
17862c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1787df8bae1dSRodney W. Grimes 			code = 0;
1788df8bae1dSRodney W. Grimes 		} else {
17896626c604SJulian Elischer 			code = p->p_code;
17906626c604SJulian Elischer 			p->p_code = 0;
17916626c604SJulian Elischer 			p->p_sig = 0;
1792df8bae1dSRodney W. Grimes 		}
17932c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1794df8bae1dSRodney W. Grimes 	}
1795df8bae1dSRodney W. Grimes }
1796df8bae1dSRodney W. Grimes 
1797df8bae1dSRodney W. Grimes /*
1798df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1799df8bae1dSRodney W. Grimes  */
180026f9a767SRodney W. Grimes void
1801df8bae1dSRodney W. Grimes killproc(p, why)
1802df8bae1dSRodney W. Grimes 	struct proc *p;
1803df8bae1dSRodney W. Grimes 	char *why;
1804df8bae1dSRodney W. Grimes {
18059081e5e8SJohn Baldwin 
18069081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18070384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
18080384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1809729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1810b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1811df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1812df8bae1dSRodney W. Grimes }
1813df8bae1dSRodney W. Grimes 
1814df8bae1dSRodney W. Grimes /*
1815df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1816df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1817df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1818df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1819df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1820df8bae1dSRodney W. Grimes  * does not return.
1821df8bae1dSRodney W. Grimes  */
182226f9a767SRodney W. Grimes void
1823b40ce416SJulian Elischer sigexit(td, sig)
1824b40ce416SJulian Elischer 	struct thread *td;
18252c42a146SMarcel Moolenaar 	int sig;
1826df8bae1dSRodney W. Grimes {
1827b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1828df8bae1dSRodney W. Grimes 
1829628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1830df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
18312c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
18322c42a146SMarcel Moolenaar 		p->p_sig = sig;
1833c364e17eSAndrey A. Chernov 		/*
1834c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1835c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1836c364e17eSAndrey A. Chernov 		 * these messages.)
1837c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1838c364e17eSAndrey A. Chernov 		 */
1839628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1840c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1841c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1842b40ce416SJulian Elischer 		if (coredump(td) == 0)
18432c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
184457308494SJoerg Wunsch 		if (kern_logsigexit)
184557308494SJoerg Wunsch 			log(LOG_INFO,
184657308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
18473d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
1848b1fc0ec1SRobert Watson 			    p->p_ucred ? p->p_ucred->cr_uid : -1,
18492c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
18502c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1851c31146a1SJohn Baldwin 	} else {
1852628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1853628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1854628d2653SJohn Baldwin 			mtx_lock(&Giant);
1855c31146a1SJohn Baldwin 	}
1856b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1857df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1858df8bae1dSRodney W. Grimes }
1859df8bae1dSRodney W. Grimes 
1860c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1861c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1862c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1863c5edb423SSean Eric Fagan 
1864c5edb423SSean Eric Fagan /*
1865c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1866c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1867c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1868c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1869c5edb423SSean Eric Fagan  *	%P	process id (pid)
1870c5edb423SSean Eric Fagan  *	%U	user id (uid)
1871c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1872c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1873c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1874c5edb423SSean Eric Fagan  */
1875c5edb423SSean Eric Fagan 
1876fca666a1SJulian Elischer static char *
1877c5edb423SSean Eric Fagan expand_name(name, uid, pid)
187887f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1879c5edb423SSean Eric Fagan 	char *temp;
1880c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1881c5edb423SSean Eric Fagan 	int i, n;
1882c5edb423SSean Eric Fagan 	char *format = corefilename;
1883ce38ca0fSAlfred Perlstein 	size_t namelen;
1884c5edb423SSean Eric Fagan 
1885ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
18860bfe2990SEivind Eklund 	if (temp == NULL)
18870bfe2990SEivind Eklund 		return NULL;
1888ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1889ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1890c5edb423SSean Eric Fagan 		int l;
1891c5edb423SSean Eric Fagan 		switch (format[i]) {
1892c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1893c5edb423SSean Eric Fagan 			i++;
1894c5edb423SSean Eric Fagan 			switch (format[i]) {
1895c5edb423SSean Eric Fagan 			case '%':
1896c5edb423SSean Eric Fagan 				temp[n++] = '%';
1897c5edb423SSean Eric Fagan 				break;
1898c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1899ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
190087f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1901c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1902c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1903c5edb423SSean Eric Fagan 					return NULL;
1904c5edb423SSean Eric Fagan 				}
1905ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1906ce38ca0fSAlfred Perlstein 				n += namelen;
1907c5edb423SSean Eric Fagan 				break;
1908c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1909ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1910c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
191187f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1912c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1913c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1914c5edb423SSean Eric Fagan 					return NULL;
1915c5edb423SSean Eric Fagan 				}
1916c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1917c5edb423SSean Eric Fagan 				n += l;
1918c5edb423SSean Eric Fagan 				break;
1919c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1920ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1921c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
192287f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1923c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1924c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1925c5edb423SSean Eric Fagan 					return NULL;
1926c5edb423SSean Eric Fagan 				}
1927c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1928c5edb423SSean Eric Fagan 				n += l;
1929c5edb423SSean Eric Fagan 				break;
1930c5edb423SSean Eric Fagan 			default:
1931c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1932c5edb423SSean Eric Fagan 			}
1933c5edb423SSean Eric Fagan 			break;
1934c5edb423SSean Eric Fagan 		default:
1935c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1936c5edb423SSean Eric Fagan 		}
1937c5edb423SSean Eric Fagan 	}
1938ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1939c5edb423SSean Eric Fagan 	return temp;
1940c5edb423SSean Eric Fagan }
1941c5edb423SSean Eric Fagan 
1942df8bae1dSRodney W. Grimes /*
1943fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1944fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1945fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1946fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1947fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
19485da271f5SRobert Watson  *
19495da271f5SRobert Watson  * XXX: VOP_GETATTR() here requires holding the vnode lock.
1950fca666a1SJulian Elischer  */
1951fca666a1SJulian Elischer 
1952fca666a1SJulian Elischer static int
1953b40ce416SJulian Elischer coredump(struct thread *td)
1954fca666a1SJulian Elischer {
1955b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1956fca666a1SJulian Elischer 	register struct vnode *vp;
1957da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
195806ae1e91SMatthew Dillon 	struct flock lf;
1959fca666a1SJulian Elischer 	struct nameidata nd;
1960fca666a1SJulian Elischer 	struct vattr vattr;
1961e6796b67SKirk McKusick 	int error, error1, flags;
1962f2a2857bSKirk McKusick 	struct mount *mp;
1963fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1964fca666a1SJulian Elischer 	off_t limit;
1965fca666a1SJulian Elischer 
1966628d2653SJohn Baldwin 	PROC_LOCK(p);
1967628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
1968fca666a1SJulian Elischer 
1969628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1970628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1971fca666a1SJulian Elischer 		return (EFAULT);
1972628d2653SJohn Baldwin 	}
1973fca666a1SJulian Elischer 
1974fca666a1SJulian Elischer 	/*
197535a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
197635a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
197735a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
197835a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
197935a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
198035a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1981fca666a1SJulian Elischer 	 */
198235a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1983628d2653SJohn Baldwin 	if (limit == 0) {
1984628d2653SJohn Baldwin 		PROC_UNLOCK(p);
198535a2598fSSean Eric Fagan 		return 0;
1986628d2653SJohn Baldwin 	}
1987628d2653SJohn Baldwin 	PROC_UNLOCK(p);
198835a2598fSSean Eric Fagan 
1989f2a2857bSKirk McKusick restart:
1990fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1991ccdbd10cSPeter Pentchev 	if (name == NULL)
1992ccdbd10cSPeter Pentchev 		return (EINVAL);
1993b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
1994e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1995e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1996fca666a1SJulian Elischer 	free(name, M_TEMP);
1997fca666a1SJulian Elischer 	if (error)
1998fca666a1SJulian Elischer 		return (error);
1999762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2000fca666a1SJulian Elischer 	vp = nd.ni_vp;
200106ae1e91SMatthew Dillon 
2002b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
200306ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
200406ae1e91SMatthew Dillon 	lf.l_start = 0;
200506ae1e91SMatthew Dillon 	lf.l_len = 0;
200606ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
200706ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
200806ae1e91SMatthew Dillon 	if (error)
200906ae1e91SMatthew Dillon 		goto out2;
201006ae1e91SMatthew Dillon 
201106ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
201206ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
201306ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2014b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2015f2a2857bSKirk McKusick 			return (error);
2016f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2017f2a2857bSKirk McKusick 			return (error);
2018f2a2857bSKirk McKusick 		goto restart;
2019f2a2857bSKirk McKusick 	}
2020fca666a1SJulian Elischer 
2021fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
2022fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
2023b40ce416SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2024fca666a1SJulian Elischer 		error = EFAULT;
202506ae1e91SMatthew Dillon 		goto out1;
2026fca666a1SJulian Elischer 	}
2027fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2028fca666a1SJulian Elischer 	vattr.va_size = 0;
202988b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2030b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2031b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
203288b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2033628d2653SJohn Baldwin 	PROC_LOCK(p);
2034fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2035628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2036fca666a1SJulian Elischer 
2037fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2038b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2039fca666a1SJulian Elischer 	  ENOSYS;
2040fca666a1SJulian Elischer 
204106ae1e91SMatthew Dillon out1:
204206ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
204306ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2044f2a2857bSKirk McKusick 	vn_finished_write(mp);
204506ae1e91SMatthew Dillon out2:
2046b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2047fca666a1SJulian Elischer 	if (error == 0)
2048fca666a1SJulian Elischer 		error = error1;
2049fca666a1SJulian Elischer 	return (error);
2050fca666a1SJulian Elischer }
2051fca666a1SJulian Elischer 
2052fca666a1SJulian Elischer /*
2053df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2054df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2055df8bae1dSRodney W. Grimes  */
2056d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2057df8bae1dSRodney W. Grimes struct nosys_args {
2058df8bae1dSRodney W. Grimes 	int	dummy;
2059df8bae1dSRodney W. Grimes };
2060d2d3e875SBruce Evans #endif
2061fb99ab88SMatthew Dillon /*
2062fb99ab88SMatthew Dillon  * MPSAFE
2063fb99ab88SMatthew Dillon  */
2064df8bae1dSRodney W. Grimes /* ARGSUSED */
206526f9a767SRodney W. Grimes int
2066b40ce416SJulian Elischer nosys(td, args)
2067b40ce416SJulian Elischer 	struct thread *td;
2068df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2069df8bae1dSRodney W. Grimes {
2070b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2071b40ce416SJulian Elischer 
2072fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2073628d2653SJohn Baldwin 	PROC_LOCK(p);
2074df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2075628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2076fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2077df8bae1dSRodney W. Grimes 	return (EINVAL);
2078df8bae1dSRodney W. Grimes }
2079831d27a9SDon Lewis 
2080831d27a9SDon Lewis /*
208148f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2082831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2083831d27a9SDon Lewis  */
2084831d27a9SDon Lewis void
20852c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
2086831d27a9SDon Lewis 	struct sigio *sigio;
20872c42a146SMarcel Moolenaar 	int sig, checkctty;
2088831d27a9SDon Lewis {
2089831d27a9SDon Lewis 	if (sigio == NULL)
2090831d27a9SDon Lewis 		return;
2091831d27a9SDon Lewis 
2092831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2093628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
20942b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
20952c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2096628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2097831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2098831d27a9SDon Lewis 		struct proc *p;
2099831d27a9SDon Lewis 
2100f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2101628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2102628d2653SJohn Baldwin 			PROC_LOCK(p);
21032b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2104831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
21052c42a146SMarcel Moolenaar 				psignal(p, sig);
2106628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2107628d2653SJohn Baldwin 		}
2108f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2109831d27a9SDon Lewis 	}
2110831d27a9SDon Lewis }
2111cb679c38SJonathan Lemon 
2112cb679c38SJonathan Lemon static int
2113cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2114cb679c38SJonathan Lemon {
2115cb679c38SJonathan Lemon 	struct proc *p = curproc;
2116cb679c38SJonathan Lemon 
2117cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2118cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2119cb679c38SJonathan Lemon 
2120628d2653SJohn Baldwin 	PROC_LOCK(p);
2121cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2122628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2123cb679c38SJonathan Lemon 
2124cb679c38SJonathan Lemon 	return (0);
2125cb679c38SJonathan Lemon }
2126cb679c38SJonathan Lemon 
2127cb679c38SJonathan Lemon static void
2128cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2129cb679c38SJonathan Lemon {
2130cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2131cb679c38SJonathan Lemon 
2132628d2653SJohn Baldwin 	PROC_LOCK(p);
2133e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2134628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2135cb679c38SJonathan Lemon }
2136cb679c38SJonathan Lemon 
2137cb679c38SJonathan Lemon /*
2138cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2139cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2140cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2141cb679c38SJonathan Lemon  * isn't worth the trouble.
2142cb679c38SJonathan Lemon  */
2143cb679c38SJonathan Lemon static int
2144cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2145cb679c38SJonathan Lemon {
2146cb679c38SJonathan Lemon 
2147cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2148cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2149cb679c38SJonathan Lemon 
2150cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2151cb679c38SJonathan Lemon 			kn->kn_data++;
2152cb679c38SJonathan Lemon 	}
2153cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2154cb679c38SJonathan Lemon }
2155