xref: /freebsd/sys/kern/kern_sig.c (revision 0384fff8c5b098545c3db311b0e0aa1ec4c9ae7e)
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>
48df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/resourcevar.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/systm.h>
56df8bae1dSRodney W. Grimes #include <sys/acct.h>
573ac4d1efSBruce Evans #include <sys/fcntl.h>
58df8bae1dSRodney W. Grimes #include <sys/wait.h>
590384fff8SJason Evans #include <sys/ktr.h>
60df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
61df8bae1dSRodney W. Grimes #include <sys/syslog.h>
62df8bae1dSRodney W. Grimes #include <sys/stat.h>
63d66a5066SPeter Wemm #include <sys/sysent.h>
64c87e2930SDavid Greenman #include <sys/sysctl.h>
65c5edb423SSean Eric Fagan #include <sys/malloc.h>
66df8bae1dSRodney W. Grimes 
67762e6b85SEivind Eklund 
6836e9f877SMatthew Dillon #include <machine/ipl.h>
69df8bae1dSRodney W. Grimes #include <machine/cpu.h>
703163861cSTor Egge #include <machine/smp.h>
71df8bae1dSRodney W. Grimes 
726f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
736f841fb7SMarcel Moolenaar 
746f841fb7SMarcel Moolenaar static int coredump	__P((struct proc *));
752c42a146SMarcel Moolenaar static int do_sigaction	__P((struct proc *p, int sig, struct sigaction *act,
76645682fdSLuoqi Chen 			     struct sigaction *oact, int old));
772c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
78645682fdSLuoqi Chen 			       sigset_t *oset, int old));
79f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t));
806f841fb7SMarcel Moolenaar static int killpg1	__P((struct proc *cp, int sig, int pgid, int all));
816f841fb7SMarcel Moolenaar static int sig_ffs	__P((sigset_t *set));
826f841fb7SMarcel Moolenaar static int sigprop	__P((int sig));
836f841fb7SMarcel Moolenaar static void stop	__P((struct proc *));
8426f9a767SRodney W. Grimes 
85cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
86cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
87cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
88cb679c38SJonathan Lemon 
89cb679c38SJonathan Lemon struct filterops sig_filtops =
90cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
91cb679c38SJonathan Lemon 
9257308494SJoerg Wunsch static int	kern_logsigexit = 1;
933d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
943d177f46SBill Fumerola     &kern_logsigexit, 0,
953d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
9657308494SJoerg Wunsch 
97df8bae1dSRodney W. Grimes /*
982c42a146SMarcel Moolenaar  * Can process p, with pcred pc, send the signal sig to process q?
99df8bae1dSRodney W. Grimes  */
100a9e0361bSPoul-Henning Kamp #define CANSIGNAL(p, q, sig) \
101387d2c03SRobert Watson 	(!p_can(p, q, P_CAN_KILL, NULL) || \
102a9e0361bSPoul-Henning Kamp 	((sig) == SIGCONT && (q)->p_session == (p)->p_session))
103df8bae1dSRodney W. Grimes 
104831d27a9SDon Lewis /*
105831d27a9SDon Lewis  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
106831d27a9SDon Lewis  */
107831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \
108831d27a9SDon Lewis 	((uc)->cr_uid == 0 || \
109831d27a9SDon Lewis 	    (ruid) == (q)->p_cred->p_ruid || \
110831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_cred->p_ruid || \
111831d27a9SDon Lewis 	    (ruid) == (q)->p_ucred->cr_uid || \
112831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
113831d27a9SDon Lewis 
11422d4b0fbSJohn Polstra int sugid_coredump;
1153d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1163d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
117c87e2930SDavid Greenman 
118e5a28db9SPaul Saab static int	do_coredump = 1;
119e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
120e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
121e5a28db9SPaul Saab 
1222c42a146SMarcel Moolenaar /*
1232c42a146SMarcel Moolenaar  * Signal properties and actions.
1242c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1252c42a146SMarcel Moolenaar  * according to the following properties:
1262c42a146SMarcel Moolenaar  */
1272c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1282c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1292c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1302c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1312c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1322c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1332c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
134df8bae1dSRodney W. Grimes 
1352c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1362c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1372c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1382c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1392c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1412c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1442c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1462c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1482c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1492c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1502c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1512c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1522c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1532c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1542c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1552c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1562c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1572c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1582c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1592c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1602c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1612c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1622c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1632c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1642c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1652c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1662c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1672c42a146SMarcel Moolenaar };
1682c42a146SMarcel Moolenaar 
1696f841fb7SMarcel Moolenaar static __inline int
1706f841fb7SMarcel Moolenaar sigprop(int sig)
1712c42a146SMarcel Moolenaar {
1726f841fb7SMarcel Moolenaar 
1732c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
1742c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
1752c42a146SMarcel Moolenaar 	return (0);
176df8bae1dSRodney W. Grimes }
1772c42a146SMarcel Moolenaar 
1786f841fb7SMarcel Moolenaar static __inline int
1796f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
1802c42a146SMarcel Moolenaar {
1812c42a146SMarcel Moolenaar 	int i;
1822c42a146SMarcel Moolenaar 
1836f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
1842c42a146SMarcel Moolenaar 		if (set->__bits[i])
1852c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
186df8bae1dSRodney W. Grimes 	return (0);
187df8bae1dSRodney W. Grimes }
188df8bae1dSRodney W. Grimes 
1892c42a146SMarcel Moolenaar /*
1902c42a146SMarcel Moolenaar  * do_sigaction
1912c42a146SMarcel Moolenaar  * sigaction
1922c42a146SMarcel Moolenaar  * osigaction
1932c42a146SMarcel Moolenaar  */
1942c42a146SMarcel Moolenaar static int
195645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
1962c42a146SMarcel Moolenaar 	struct proc *p;
1972c42a146SMarcel Moolenaar 	register int sig;
1982c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
199645682fdSLuoqi Chen 	int old;
200df8bae1dSRodney W. Grimes {
201df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
202df8bae1dSRodney W. Grimes 
2032c42a146SMarcel Moolenaar 	if (sig <= 0 || sig > _SIG_MAXSIG)
2042c42a146SMarcel Moolenaar 		return (EINVAL);
2052c42a146SMarcel Moolenaar 
2062c42a146SMarcel Moolenaar 	if (oact) {
2072c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2082c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2092c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2102c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2112c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2122c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2132c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2142c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2152c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2162c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2172c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2182c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2192c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
220645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2212c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
222645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2232c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2242c42a146SMarcel Moolenaar 	}
2252c42a146SMarcel Moolenaar 	if (act) {
2262c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
2272c42a146SMarcel Moolenaar 		    act->sa_handler != SIG_DFL)
2282c42a146SMarcel Moolenaar 			return (EINVAL);
2292c42a146SMarcel Moolenaar 
230df8bae1dSRodney W. Grimes 		/*
231df8bae1dSRodney W. Grimes 		 * Change setting atomically.
232df8bae1dSRodney W. Grimes 		 */
233df8bae1dSRodney W. Grimes 		(void) splhigh();
2342c42a146SMarcel Moolenaar 
2352c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2362c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2372c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
2382c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2392c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_siginfo, sig);
2406f841fb7SMarcel Moolenaar 		} else {
2412c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] =
2422c42a146SMarcel Moolenaar 			    (__sighandler_t *)act->sa_sigaction;
2432c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2442c42a146SMarcel Moolenaar 		}
2452c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2462c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
247df8bae1dSRodney W. Grimes 		else
2482c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2492c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2502c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
251df8bae1dSRodney W. Grimes 		else
2522c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2532c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2542c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2551e41c1b5SSteven Wallace 		else
2562c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2572c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2582c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
259289ccde0SPeter Wemm 		else
2602c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
261df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2622c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2632c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
264df8bae1dSRodney W. Grimes 		else
2652c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_usertramp, seg);
266df8bae1dSRodney W. Grimes #endif
2672c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2682c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
269645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
2706626c604SJulian Elischer 			else
271645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
2722c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDWAIT) {
273245f17d4SJoerg Wunsch 				/*
2742c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
2752c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
2762c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
2772c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
278245f17d4SJoerg Wunsch 				 */
279245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
280645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
2816626c604SJulian Elischer 				else
282645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
283245f17d4SJoerg Wunsch 			} else
284645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
285df8bae1dSRodney W. Grimes 		}
286df8bae1dSRodney W. Grimes 		/*
287df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
2882c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
2892c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
2902c42a146SMarcel Moolenaar 		 * have to restart the process.
291df8bae1dSRodney W. Grimes 		 */
2922c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
2932c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
2942c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
2952c42a146SMarcel Moolenaar 			/* never to be seen again */
2962c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
2972c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
2982c42a146SMarcel Moolenaar 				/* easier in psignal */
2992c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3002c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
301645682fdSLuoqi Chen 		} else {
3022c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3032c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3042c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3052c42a146SMarcel Moolenaar 			else
3062c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3072c42a146SMarcel Moolenaar 		}
308645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
309645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
310645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
311645682fdSLuoqi Chen 		else
312645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
3132c42a146SMarcel Moolenaar 
314df8bae1dSRodney W. Grimes 		(void) spl0();
315df8bae1dSRodney W. Grimes 	}
3162c42a146SMarcel Moolenaar 	return (0);
3172c42a146SMarcel Moolenaar }
3182c42a146SMarcel Moolenaar 
3192c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3202c42a146SMarcel Moolenaar struct sigaction_args {
3212c42a146SMarcel Moolenaar 	int	sig;
3222c42a146SMarcel Moolenaar 	struct	sigaction *act;
3232c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3242c42a146SMarcel Moolenaar };
3252c42a146SMarcel Moolenaar #endif
3262c42a146SMarcel Moolenaar /* ARGSUSED */
3272c42a146SMarcel Moolenaar int
3282c42a146SMarcel Moolenaar sigaction(p, uap)
3292c42a146SMarcel Moolenaar 	struct proc *p;
3302c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3312c42a146SMarcel Moolenaar {
3322c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3332c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3342c42a146SMarcel Moolenaar 	int error;
3352c42a146SMarcel Moolenaar 
3366f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3376f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3382c42a146SMarcel Moolenaar 	if (actp) {
3396f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3402c42a146SMarcel Moolenaar 		if (error)
3412c42a146SMarcel Moolenaar 			return (error);
3422c42a146SMarcel Moolenaar 	}
343645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3442c42a146SMarcel Moolenaar 	if (oactp && !error) {
3456f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3462c42a146SMarcel Moolenaar 	}
3472c42a146SMarcel Moolenaar 	return (error);
3482c42a146SMarcel Moolenaar }
3492c42a146SMarcel Moolenaar 
35031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3512c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3522c42a146SMarcel Moolenaar struct osigaction_args {
3532c42a146SMarcel Moolenaar 	int	signum;
3542c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3552c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3562c42a146SMarcel Moolenaar };
3572c42a146SMarcel Moolenaar #endif
3582c42a146SMarcel Moolenaar /* ARGSUSED */
3592c42a146SMarcel Moolenaar int
3602c42a146SMarcel Moolenaar osigaction(p, uap)
3612c42a146SMarcel Moolenaar 	struct proc *p;
3622c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3632c42a146SMarcel Moolenaar {
3642c42a146SMarcel Moolenaar 	struct osigaction sa;
3652c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
3662c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
3672c42a146SMarcel Moolenaar 	int error;
3682c42a146SMarcel Moolenaar 
3696f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
3706f841fb7SMarcel Moolenaar 		return (EINVAL);
3716f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
3726f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
3732c42a146SMarcel Moolenaar 	if (nsap) {
3746f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
3752c42a146SMarcel Moolenaar 		if (error)
3762c42a146SMarcel Moolenaar 			return (error);
3772c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
3782c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
3792c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
3802c42a146SMarcel Moolenaar 	}
381645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
3822c42a146SMarcel Moolenaar 	if (osap && !error) {
3832c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
3842c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
3852c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
3866f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
3872c42a146SMarcel Moolenaar 	}
3882c42a146SMarcel Moolenaar 	return (error);
3892c42a146SMarcel Moolenaar }
39031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
391df8bae1dSRodney W. Grimes 
392df8bae1dSRodney W. Grimes /*
393df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
394df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
395df8bae1dSRodney W. Grimes  */
396df8bae1dSRodney W. Grimes void
397df8bae1dSRodney W. Grimes siginit(p)
398df8bae1dSRodney W. Grimes 	struct proc *p;
399df8bae1dSRodney W. Grimes {
400df8bae1dSRodney W. Grimes 	register int i;
401df8bae1dSRodney W. Grimes 
4022c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4032c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4042c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
405df8bae1dSRodney W. Grimes }
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes /*
408df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
409df8bae1dSRodney W. Grimes  */
410df8bae1dSRodney W. Grimes void
411df8bae1dSRodney W. Grimes execsigs(p)
412df8bae1dSRodney W. Grimes 	register struct proc *p;
413df8bae1dSRodney W. Grimes {
414df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
4152c42a146SMarcel Moolenaar 	register int sig;
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	/*
418df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
419df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
420df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
421df8bae1dSRodney W. Grimes 	 */
4222c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4232c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4242c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4252c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4262c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4272c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4282c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
429df8bae1dSRodney W. Grimes 		}
4302c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
431df8bae1dSRodney W. Grimes 	}
432df8bae1dSRodney W. Grimes 	/*
433df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
434df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
435df8bae1dSRodney W. Grimes 	 */
436645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
437645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
438645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
43980e907a1SPeter Wemm 	/*
44080e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
44180e907a1SPeter Wemm 	 */
442645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
443df8bae1dSRodney W. Grimes }
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes /*
4467c8fdcbdSMatthew Dillon  * do_sigprocmask() - MP SAFE ONLY IF p == curproc
4477c8fdcbdSMatthew Dillon  *
4487c8fdcbdSMatthew Dillon  *	Manipulate signal mask.  This routine is MP SAFE *ONLY* if
4497c8fdcbdSMatthew Dillon  *	p == curproc.  Also remember that in order to remain MP SAFE
4507c8fdcbdSMatthew Dillon  *	no spl*() calls may be made.
451df8bae1dSRodney W. Grimes  */
4522c42a146SMarcel Moolenaar static int
453645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
4542c42a146SMarcel Moolenaar 	struct proc *p;
4552c42a146SMarcel Moolenaar 	int how;
4562c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
457645682fdSLuoqi Chen 	int old;
4582c42a146SMarcel Moolenaar {
4592c42a146SMarcel Moolenaar 	int error;
4602c42a146SMarcel Moolenaar 
4612c42a146SMarcel Moolenaar 	if (oset != NULL)
4622c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
4632c42a146SMarcel Moolenaar 
4642c42a146SMarcel Moolenaar 	error = 0;
4652c42a146SMarcel Moolenaar 	if (set != NULL) {
4662c42a146SMarcel Moolenaar 		switch (how) {
4672c42a146SMarcel Moolenaar 		case SIG_BLOCK:
468645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
4692c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
4702c42a146SMarcel Moolenaar 			break;
4712c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
4722c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
4732c42a146SMarcel Moolenaar 			break;
4742c42a146SMarcel Moolenaar 		case SIG_SETMASK:
475645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
476645682fdSLuoqi Chen 			if (old)
477645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
478645682fdSLuoqi Chen 			else
4792c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
4802c42a146SMarcel Moolenaar 			break;
4812c42a146SMarcel Moolenaar 		default:
4822c42a146SMarcel Moolenaar 			error = EINVAL;
4832c42a146SMarcel Moolenaar 			break;
4842c42a146SMarcel Moolenaar 		}
4852c42a146SMarcel Moolenaar 	}
4862c42a146SMarcel Moolenaar 	return (error);
4872c42a146SMarcel Moolenaar }
4882c42a146SMarcel Moolenaar 
4897c8fdcbdSMatthew Dillon /*
4907c8fdcbdSMatthew Dillon  * sigprocmask() - MP SAFE
4917c8fdcbdSMatthew Dillon  */
4927c8fdcbdSMatthew Dillon 
493d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
494df8bae1dSRodney W. Grimes struct sigprocmask_args {
495df8bae1dSRodney W. Grimes 	int	how;
4962c42a146SMarcel Moolenaar 	const sigset_t *set;
4972c42a146SMarcel Moolenaar 	sigset_t *oset;
498df8bae1dSRodney W. Grimes };
499d2d3e875SBruce Evans #endif
50026f9a767SRodney W. Grimes int
501cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
502df8bae1dSRodney W. Grimes 	register struct proc *p;
503df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
504df8bae1dSRodney W. Grimes {
5052c42a146SMarcel Moolenaar 	sigset_t set, oset;
5062c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5072c42a146SMarcel Moolenaar 	int error;
508df8bae1dSRodney W. Grimes 
5096f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5106f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5112c42a146SMarcel Moolenaar 	if (setp) {
5126f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5132c42a146SMarcel Moolenaar 		if (error)
5142c42a146SMarcel Moolenaar 			return (error);
515df8bae1dSRodney W. Grimes 	}
516645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5172c42a146SMarcel Moolenaar 	if (osetp && !error) {
5186f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5192c42a146SMarcel Moolenaar 	}
5202c42a146SMarcel Moolenaar 	return (error);
5212c42a146SMarcel Moolenaar }
5222c42a146SMarcel Moolenaar 
52331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5247c8fdcbdSMatthew Dillon /*
5257c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5267c8fdcbdSMatthew Dillon  */
5272c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5282c42a146SMarcel Moolenaar struct osigprocmask_args {
5292c42a146SMarcel Moolenaar 	int	how;
5302c42a146SMarcel Moolenaar 	osigset_t mask;
5312c42a146SMarcel Moolenaar };
5322c42a146SMarcel Moolenaar #endif
5332c42a146SMarcel Moolenaar int
5342c42a146SMarcel Moolenaar osigprocmask(p, uap)
5352c42a146SMarcel Moolenaar 	register struct proc *p;
5362c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5372c42a146SMarcel Moolenaar {
5382c42a146SMarcel Moolenaar 	sigset_t set, oset;
5392c42a146SMarcel Moolenaar 	int error;
5402c42a146SMarcel Moolenaar 
5412c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
542645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
5432c42a146SMarcel Moolenaar 	SIG2OSIG(oset, p->p_retval[0]);
544df8bae1dSRodney W. Grimes 	return (error);
545df8bae1dSRodney W. Grimes }
54631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
547df8bae1dSRodney W. Grimes 
548d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
549df8bae1dSRodney W. Grimes struct sigpending_args {
5502c42a146SMarcel Moolenaar 	sigset_t	*set;
551df8bae1dSRodney W. Grimes };
552d2d3e875SBruce Evans #endif
553df8bae1dSRodney W. Grimes /* ARGSUSED */
55426f9a767SRodney W. Grimes int
555cb226aaaSPoul-Henning Kamp sigpending(p, uap)
556df8bae1dSRodney W. Grimes 	struct proc *p;
557df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
558df8bae1dSRodney W. Grimes {
559df8bae1dSRodney W. Grimes 
5606f841fb7SMarcel Moolenaar 	return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
5612c42a146SMarcel Moolenaar }
5622c42a146SMarcel Moolenaar 
56331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5642c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5652c42a146SMarcel Moolenaar struct osigpending_args {
5662c42a146SMarcel Moolenaar 	int	dummy;
5672c42a146SMarcel Moolenaar };
5682c42a146SMarcel Moolenaar #endif
5692c42a146SMarcel Moolenaar /* ARGSUSED */
5702c42a146SMarcel Moolenaar int
5712c42a146SMarcel Moolenaar osigpending(p, uap)
5722c42a146SMarcel Moolenaar 	struct proc *p;
5732c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
5742c42a146SMarcel Moolenaar {
5752c42a146SMarcel Moolenaar 
5762c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_siglist, p->p_retval[0]);
577df8bae1dSRodney W. Grimes 	return (0);
578df8bae1dSRodney W. Grimes }
57931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
582df8bae1dSRodney W. Grimes /*
583df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
584df8bae1dSRodney W. Grimes  */
585d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
586df8bae1dSRodney W. Grimes struct osigvec_args {
587df8bae1dSRodney W. Grimes 	int	signum;
588df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
589df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
590df8bae1dSRodney W. Grimes };
591d2d3e875SBruce Evans #endif
592df8bae1dSRodney W. Grimes /* ARGSUSED */
59326f9a767SRodney W. Grimes int
594cb226aaaSPoul-Henning Kamp osigvec(p, uap)
595df8bae1dSRodney W. Grimes 	struct proc *p;
596df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
597df8bae1dSRodney W. Grimes {
598df8bae1dSRodney W. Grimes 	struct sigvec vec;
5992c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6002c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6012c42a146SMarcel Moolenaar 	int error;
602df8bae1dSRodney W. Grimes 
6036f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6046f841fb7SMarcel Moolenaar 		return (EINVAL);
6056f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6066f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6072c42a146SMarcel Moolenaar 	if (nsap) {
6086f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
6092c42a146SMarcel Moolenaar 		if (error)
610df8bae1dSRodney W. Grimes 			return (error);
6112c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
6122c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
6132c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
6142c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
615df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
6162c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
617df8bae1dSRodney W. Grimes #endif
618df8bae1dSRodney W. Grimes 	}
619645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
6202c42a146SMarcel Moolenaar 	if (osap && !error) {
6212c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
6222c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
6232c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
6242c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
6252c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
6262c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
6272c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
6282c42a146SMarcel Moolenaar #endif
6296f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
6302c42a146SMarcel Moolenaar 	}
6312c42a146SMarcel Moolenaar 	return (error);
632df8bae1dSRodney W. Grimes }
633df8bae1dSRodney W. Grimes 
634d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
635df8bae1dSRodney W. Grimes struct osigblock_args {
636df8bae1dSRodney W. Grimes 	int	mask;
637df8bae1dSRodney W. Grimes };
638d2d3e875SBruce Evans #endif
63926f9a767SRodney W. Grimes int
640cb226aaaSPoul-Henning Kamp osigblock(p, uap)
641df8bae1dSRodney W. Grimes 	register struct proc *p;
642df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
643df8bae1dSRodney W. Grimes {
6442c42a146SMarcel Moolenaar 	sigset_t set;
645df8bae1dSRodney W. Grimes 
6462c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6472c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
648df8bae1dSRodney W. Grimes 	(void) splhigh();
6492c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
6502c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
651df8bae1dSRodney W. Grimes 	(void) spl0();
652df8bae1dSRodney W. Grimes 	return (0);
653df8bae1dSRodney W. Grimes }
654df8bae1dSRodney W. Grimes 
655d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
656df8bae1dSRodney W. Grimes struct osigsetmask_args {
657df8bae1dSRodney W. Grimes 	int	mask;
658df8bae1dSRodney W. Grimes };
659d2d3e875SBruce Evans #endif
66026f9a767SRodney W. Grimes int
661cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
662df8bae1dSRodney W. Grimes 	struct proc *p;
663df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
664df8bae1dSRodney W. Grimes {
6652c42a146SMarcel Moolenaar 	sigset_t set;
666df8bae1dSRodney W. Grimes 
6672c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6682c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
669df8bae1dSRodney W. Grimes 	(void) splhigh();
6702c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
671645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
672df8bae1dSRodney W. Grimes 	(void) spl0();
673df8bae1dSRodney W. Grimes 	return (0);
674df8bae1dSRodney W. Grimes }
675df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
676df8bae1dSRodney W. Grimes 
677df8bae1dSRodney W. Grimes /*
678df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
679df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
680df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
681df8bae1dSRodney W. Grimes  */
682d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
683df8bae1dSRodney W. Grimes struct sigsuspend_args {
6842c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
685df8bae1dSRodney W. Grimes };
686d2d3e875SBruce Evans #endif
687df8bae1dSRodney W. Grimes /* ARGSUSED */
68826f9a767SRodney W. Grimes int
689cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
690df8bae1dSRodney W. Grimes 	register struct proc *p;
691df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
692df8bae1dSRodney W. Grimes {
6932c42a146SMarcel Moolenaar 	sigset_t mask;
694df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
6952c42a146SMarcel Moolenaar 	int error;
6962c42a146SMarcel Moolenaar 
6976f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
6982c42a146SMarcel Moolenaar 	if (error)
6992c42a146SMarcel Moolenaar 		return (error);
700df8bae1dSRodney W. Grimes 
701df8bae1dSRodney W. Grimes 	/*
702645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
703df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
704df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
705df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
706df8bae1dSRodney W. Grimes 	 * to indicate this.
707df8bae1dSRodney W. Grimes 	 */
7086626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
709645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
710645682fdSLuoqi Chen 
711645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
7122c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
7132c42a146SMarcel Moolenaar 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
7142c42a146SMarcel Moolenaar 		/* void */;
7152c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
7162c42a146SMarcel Moolenaar 	return (EINTR);
7172c42a146SMarcel Moolenaar }
7182c42a146SMarcel Moolenaar 
71931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7202c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7212c42a146SMarcel Moolenaar struct osigsuspend_args {
7222c42a146SMarcel Moolenaar 	osigset_t mask;
7232c42a146SMarcel Moolenaar };
7242c42a146SMarcel Moolenaar #endif
7252c42a146SMarcel Moolenaar /* ARGSUSED */
7262c42a146SMarcel Moolenaar int
7272c42a146SMarcel Moolenaar osigsuspend(p, uap)
7282c42a146SMarcel Moolenaar 	register struct proc *p;
7292c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
7302c42a146SMarcel Moolenaar {
731645682fdSLuoqi Chen 	sigset_t mask;
7322c42a146SMarcel Moolenaar 	register struct sigacts *ps = p->p_sigacts;
7332c42a146SMarcel Moolenaar 
7342c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
735645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
736645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
737645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
738645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
739645682fdSLuoqi Chen 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
740df8bae1dSRodney W. Grimes 		/* void */;
741df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
742df8bae1dSRodney W. Grimes 	return (EINTR);
743df8bae1dSRodney W. Grimes }
74431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
745df8bae1dSRodney W. Grimes 
746df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
747d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
748df8bae1dSRodney W. Grimes struct osigstack_args {
749df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
750df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
751df8bae1dSRodney W. Grimes };
752d2d3e875SBruce Evans #endif
753df8bae1dSRodney W. Grimes /* ARGSUSED */
75426f9a767SRodney W. Grimes int
755cb226aaaSPoul-Henning Kamp osigstack(p, uap)
756df8bae1dSRodney W. Grimes 	struct proc *p;
757df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
758df8bae1dSRodney W. Grimes {
759df8bae1dSRodney W. Grimes 	struct sigstack ss;
760df8bae1dSRodney W. Grimes 	int error = 0;
761df8bae1dSRodney W. Grimes 
762645682fdSLuoqi Chen 	ss.ss_sp = p->p_sigstk.ss_sp;
763645682fdSLuoqi Chen 	ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
7646f841fb7SMarcel Moolenaar 	if (uap->oss && (error = copyout(&ss, uap->oss,
765df8bae1dSRodney W. Grimes 	    sizeof(struct sigstack))))
766df8bae1dSRodney W. Grimes 		return (error);
7676f841fb7SMarcel Moolenaar 	if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
768645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
769645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
770645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
771645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
772df8bae1dSRodney W. Grimes 	}
773df8bae1dSRodney W. Grimes 	return (error);
774df8bae1dSRodney W. Grimes }
775df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
776df8bae1dSRodney W. Grimes 
777d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
778df8bae1dSRodney W. Grimes struct sigaltstack_args {
7792c42a146SMarcel Moolenaar 	stack_t	*ss;
7802c42a146SMarcel Moolenaar 	stack_t	*oss;
781df8bae1dSRodney W. Grimes };
782d2d3e875SBruce Evans #endif
783df8bae1dSRodney W. Grimes /* ARGSUSED */
78426f9a767SRodney W. Grimes int
785cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
786df8bae1dSRodney W. Grimes 	struct proc *p;
787df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
788df8bae1dSRodney W. Grimes {
7892c42a146SMarcel Moolenaar 	stack_t ss;
790df8bae1dSRodney W. Grimes 	int error;
791df8bae1dSRodney W. Grimes 
792645682fdSLuoqi Chen 	if ((p->p_flag & P_ALTSTACK) == 0)
793645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= SS_DISABLE;
7946f841fb7SMarcel Moolenaar 	if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
7956f841fb7SMarcel Moolenaar 	    sizeof(stack_t))))
796df8bae1dSRodney W. Grimes 		return (error);
7972c42a146SMarcel Moolenaar 	if (uap->ss == 0)
798df8bae1dSRodney W. Grimes 		return (0);
7996f841fb7SMarcel Moolenaar 	if ((error = copyin(uap->ss, &ss, sizeof(ss))))
800df8bae1dSRodney W. Grimes 		return (error);
801d66a5066SPeter Wemm 	if (ss.ss_flags & SS_DISABLE) {
802645682fdSLuoqi Chen 		if (p->p_sigstk.ss_flags & SS_ONSTACK)
803df8bae1dSRodney W. Grimes 			return (EINVAL);
804645682fdSLuoqi Chen 		p->p_flag &= ~P_ALTSTACK;
805645682fdSLuoqi Chen 		p->p_sigstk.ss_flags = ss.ss_flags;
806df8bae1dSRodney W. Grimes 		return (0);
807df8bae1dSRodney W. Grimes 	}
808df8bae1dSRodney W. Grimes 	if (ss.ss_size < MINSIGSTKSZ)
809df8bae1dSRodney W. Grimes 		return (ENOMEM);
810645682fdSLuoqi Chen 	p->p_flag |= P_ALTSTACK;
811645682fdSLuoqi Chen 	p->p_sigstk = ss;
812df8bae1dSRodney W. Grimes 	return (0);
813df8bae1dSRodney W. Grimes }
814df8bae1dSRodney W. Grimes 
815d93f860cSPoul-Henning Kamp /*
816d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
817d93f860cSPoul-Henning Kamp  * cp is calling process.
818d93f860cSPoul-Henning Kamp  */
819d93f860cSPoul-Henning Kamp int
8202c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
821d93f860cSPoul-Henning Kamp 	register struct proc *cp;
8222c42a146SMarcel Moolenaar 	int sig, pgid, all;
823d93f860cSPoul-Henning Kamp {
824d93f860cSPoul-Henning Kamp 	register struct proc *p;
825d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
826d93f860cSPoul-Henning Kamp 	int nfound = 0;
827d93f860cSPoul-Henning Kamp 
828d93f860cSPoul-Henning Kamp 	if (all)
829d93f860cSPoul-Henning Kamp 		/*
830d93f860cSPoul-Henning Kamp 		 * broadcast
831d93f860cSPoul-Henning Kamp 		 */
8322e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
833d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
834a9e0361bSPoul-Henning Kamp 			    p == cp || !CANSIGNAL(cp, p, sig))
835d93f860cSPoul-Henning Kamp 				continue;
836d93f860cSPoul-Henning Kamp 			nfound++;
8372c42a146SMarcel Moolenaar 			if (sig)
8382c42a146SMarcel Moolenaar 				psignal(p, sig);
839d93f860cSPoul-Henning Kamp 		}
840d93f860cSPoul-Henning Kamp 	else {
841d93f860cSPoul-Henning Kamp 		if (pgid == 0)
842d93f860cSPoul-Henning Kamp 			/*
843d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
844d93f860cSPoul-Henning Kamp 			 */
845d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
846d93f860cSPoul-Henning Kamp 		else {
847d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
848d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
849d93f860cSPoul-Henning Kamp 				return (ESRCH);
850d93f860cSPoul-Henning Kamp 		}
8512e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
852d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
853d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
854a9e0361bSPoul-Henning Kamp 			    !CANSIGNAL(cp, p, sig))
855d93f860cSPoul-Henning Kamp 				continue;
856d93f860cSPoul-Henning Kamp 			nfound++;
8572c42a146SMarcel Moolenaar 			if (sig)
8582c42a146SMarcel Moolenaar 				psignal(p, sig);
859d93f860cSPoul-Henning Kamp 		}
860d93f860cSPoul-Henning Kamp 	}
861d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
862d93f860cSPoul-Henning Kamp }
863d93f860cSPoul-Henning Kamp 
864d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
865df8bae1dSRodney W. Grimes struct kill_args {
866df8bae1dSRodney W. Grimes 	int	pid;
867df8bae1dSRodney W. Grimes 	int	signum;
868df8bae1dSRodney W. Grimes };
869d2d3e875SBruce Evans #endif
870df8bae1dSRodney W. Grimes /* ARGSUSED */
87126f9a767SRodney W. Grimes int
872cb226aaaSPoul-Henning Kamp kill(cp, uap)
873df8bae1dSRodney W. Grimes 	register struct proc *cp;
874df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
875df8bae1dSRodney W. Grimes {
876df8bae1dSRodney W. Grimes 	register struct proc *p;
877df8bae1dSRodney W. Grimes 
8782c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
879df8bae1dSRodney W. Grimes 		return (EINVAL);
880df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
881df8bae1dSRodney W. Grimes 		/* kill single process */
882df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
883df8bae1dSRodney W. Grimes 			return (ESRCH);
884a9e0361bSPoul-Henning Kamp 		if (!CANSIGNAL(cp, p, uap->signum))
885df8bae1dSRodney W. Grimes 			return (EPERM);
886df8bae1dSRodney W. Grimes 		if (uap->signum)
887df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
888df8bae1dSRodney W. Grimes 		return (0);
889df8bae1dSRodney W. Grimes 	}
890df8bae1dSRodney W. Grimes 	switch (uap->pid) {
891df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
892df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
893df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
894df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
895df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
896df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
897df8bae1dSRodney W. Grimes 	}
898df8bae1dSRodney W. Grimes 	/* NOTREACHED */
899df8bae1dSRodney W. Grimes }
900df8bae1dSRodney W. Grimes 
901df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
902d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
903df8bae1dSRodney W. Grimes struct okillpg_args {
904df8bae1dSRodney W. Grimes 	int	pgid;
905df8bae1dSRodney W. Grimes 	int	signum;
906df8bae1dSRodney W. Grimes };
907d2d3e875SBruce Evans #endif
908df8bae1dSRodney W. Grimes /* ARGSUSED */
90926f9a767SRodney W. Grimes int
910cb226aaaSPoul-Henning Kamp okillpg(p, uap)
911df8bae1dSRodney W. Grimes 	struct proc *p;
912df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
913df8bae1dSRodney W. Grimes {
914df8bae1dSRodney W. Grimes 
9152c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
916df8bae1dSRodney W. Grimes 		return (EINVAL);
917df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
918df8bae1dSRodney W. Grimes }
919df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes /*
922df8bae1dSRodney W. Grimes  * Send a signal to a process group.
923df8bae1dSRodney W. Grimes  */
924df8bae1dSRodney W. Grimes void
9252c42a146SMarcel Moolenaar gsignal(pgid, sig)
9262c42a146SMarcel Moolenaar 	int pgid, sig;
927df8bae1dSRodney W. Grimes {
928df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
929df8bae1dSRodney W. Grimes 
930df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
9312c42a146SMarcel Moolenaar 		pgsignal(pgrp, sig, 0);
932df8bae1dSRodney W. Grimes }
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes /*
935df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
936df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
937df8bae1dSRodney W. Grimes  */
938df8bae1dSRodney W. Grimes void
9392c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
940df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
9412c42a146SMarcel Moolenaar 	int sig, checkctty;
942df8bae1dSRodney W. Grimes {
943df8bae1dSRodney W. Grimes 	register struct proc *p;
944df8bae1dSRodney W. Grimes 
945df8bae1dSRodney W. Grimes 	if (pgrp)
9462e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
947df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
9482c42a146SMarcel Moolenaar 				psignal(p, sig);
949df8bae1dSRodney W. Grimes }
950df8bae1dSRodney W. Grimes 
951df8bae1dSRodney W. Grimes /*
952df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
953df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
954df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
955df8bae1dSRodney W. Grimes  */
956df8bae1dSRodney W. Grimes void
9572c42a146SMarcel Moolenaar trapsignal(p, sig, code)
958df8bae1dSRodney W. Grimes 	struct proc *p;
9592c42a146SMarcel Moolenaar 	register int sig;
9608674077aSJeffrey Hsu 	u_long code;
961df8bae1dSRodney W. Grimes {
962df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
963df8bae1dSRodney W. Grimes 
9642c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
9652c42a146SMarcel Moolenaar 	    SIGISMEMBER(p->p_sigmask, sig)) {
966df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
967df8bae1dSRodney W. Grimes #ifdef KTRACE
968df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
9692c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
9702c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
971df8bae1dSRodney W. Grimes #endif
9722c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
9732c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
9742c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
9752c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
9762c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
9772c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
978289ccde0SPeter Wemm 			/*
9792c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
980289ccde0SPeter Wemm 			 */
9812c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
9822c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
9832c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
9842c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
9852c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
986dedc04feSPeter Wemm 		}
9876f841fb7SMarcel Moolenaar 	} else {
9886626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
9892c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
9902c42a146SMarcel Moolenaar 		psignal(p, sig);
991df8bae1dSRodney W. Grimes 	}
992df8bae1dSRodney W. Grimes }
993df8bae1dSRodney W. Grimes 
994df8bae1dSRodney W. Grimes /*
995df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
996df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
997df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
998df8bae1dSRodney W. Grimes  *
999df8bae1dSRodney W. Grimes  * Exceptions:
1000df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1001df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1002df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1003df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1004df8bae1dSRodney W. Grimes  *
1005df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1006df8bae1dSRodney W. Grimes  */
1007df8bae1dSRodney W. Grimes void
10082c42a146SMarcel Moolenaar psignal(p, sig)
1009df8bae1dSRodney W. Grimes 	register struct proc *p;
10102c42a146SMarcel Moolenaar 	register int sig;
1011df8bae1dSRodney W. Grimes {
1012df8bae1dSRodney W. Grimes 	register int s, prop;
1013df8bae1dSRodney W. Grimes 	register sig_t action;
1014df8bae1dSRodney W. Grimes 
10152c42a146SMarcel Moolenaar 	if (sig > _SIG_MAXSIG || sig <= 0) {
10162c42a146SMarcel Moolenaar 		printf("psignal: signal %d\n", sig);
1017df8bae1dSRodney W. Grimes 		panic("psignal signal number");
10182a024a2bSSean Eric Fagan 	}
10192c42a146SMarcel Moolenaar 
1020cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1021cb679c38SJonathan Lemon 
10222c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1023df8bae1dSRodney W. Grimes 
1024df8bae1dSRodney W. Grimes 	/*
10252a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
10262a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
10272a024a2bSSean Eric Fagan 	 * a chance, as well.
1028df8bae1dSRodney W. Grimes 	 */
10292a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1030df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1031df8bae1dSRodney W. Grimes 	else {
1032df8bae1dSRodney W. Grimes 		/*
1033df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1034df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1035df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1036df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1037df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1038df8bae1dSRodney W. Grimes 		 */
10392c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1040df8bae1dSRodney W. Grimes 			return;
10412c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1042df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
10432c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1044df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1045df8bae1dSRodney W. Grimes 		else
1046df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1047df8bae1dSRodney W. Grimes 	}
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1050df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
1051df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
1052df8bae1dSRodney W. Grimes 
1053df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
10542c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1057df8bae1dSRodney W. Grimes 		/*
1058df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1059df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1060df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1061df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1062df8bae1dSRodney W. Grimes 		 */
1063df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1064df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
1065df8bae1dSRodney W. Grimes 		        return;
10662c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1067df8bae1dSRodney W. Grimes 	}
10682c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1069df8bae1dSRodney W. Grimes 
1070df8bae1dSRodney W. Grimes 	/*
1071df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1072df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1073df8bae1dSRodney W. Grimes 	 */
10742c42a146SMarcel Moolenaar 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1075df8bae1dSRodney W. Grimes 		return;
1076df8bae1dSRodney W. Grimes 	s = splhigh();
1077df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes 	case SSLEEP:
1080df8bae1dSRodney W. Grimes 		/*
1081df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1082df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1083df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1084df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1085df8bae1dSRodney W. Grimes 		 */
1086df8bae1dSRodney W. Grimes 		if ((p->p_flag & P_SINTR) == 0)
1087df8bae1dSRodney W. Grimes 			goto out;
1088df8bae1dSRodney W. Grimes 		/*
1089df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1090df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1091df8bae1dSRodney W. Grimes 		 * for the parent.
1092df8bae1dSRodney W. Grimes 		 */
1093df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1094df8bae1dSRodney W. Grimes 			goto run;
1095df8bae1dSRodney W. Grimes 		/*
1096df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1097df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1098df8bae1dSRodney W. Grimes 		 * be awakened.
1099df8bae1dSRodney W. Grimes 		 */
1100df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
11012c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1102df8bae1dSRodney W. Grimes 			goto out;
1103df8bae1dSRodney W. Grimes 		}
1104df8bae1dSRodney W. Grimes 		/*
1105df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1106df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1107df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1108df8bae1dSRodney W. Grimes 		 * cause the process to run.
1109df8bae1dSRodney W. Grimes 		 */
1110df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1111df8bae1dSRodney W. Grimes 			if (action != SIG_DFL)
1112df8bae1dSRodney W. Grimes 				goto runfast;
1113df8bae1dSRodney W. Grimes 			/*
1114df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1115df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1116df8bae1dSRodney W. Grimes 			 */
1117df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1118df8bae1dSRodney W. Grimes 				goto out;
11192c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
11202c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1121645682fdSLuoqi Chen 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1122df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1123df8bae1dSRodney W. Grimes 			stop(p);
1124df8bae1dSRodney W. Grimes 			goto out;
1125df8bae1dSRodney W. Grimes 		} else
1126df8bae1dSRodney W. Grimes 			goto runfast;
1127df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
1128df8bae1dSRodney W. Grimes 
1129df8bae1dSRodney W. Grimes 	case SSTOP:
1130df8bae1dSRodney W. Grimes 		/*
1131df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1132df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1133df8bae1dSRodney W. Grimes 		 */
1134df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1135df8bae1dSRodney W. Grimes 			goto out;
1136df8bae1dSRodney W. Grimes 
1137df8bae1dSRodney W. Grimes 		/*
1138df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1139df8bae1dSRodney W. Grimes 		 */
11402c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1141df8bae1dSRodney W. Grimes 			goto runfast;
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1144df8bae1dSRodney W. Grimes 			/*
1145df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1146df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1147df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1148df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1149df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1150df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1151df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1152df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1153df8bae1dSRodney W. Grimes 			 */
1154df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
11552c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1156df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1157df8bae1dSRodney W. Grimes 				goto runfast;
1158df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
1159df8bae1dSRodney W. Grimes 				goto run;
1160df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
1161df8bae1dSRodney W. Grimes 			goto out;
1162df8bae1dSRodney W. Grimes 		}
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1165df8bae1dSRodney W. Grimes 			/*
1166df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1167df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1168df8bae1dSRodney W. Grimes 			 */
11692c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1170df8bae1dSRodney W. Grimes 			goto out;
1171df8bae1dSRodney W. Grimes 		}
1172df8bae1dSRodney W. Grimes 
1173df8bae1dSRodney W. Grimes 		/*
1174df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1175df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1176df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1177df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1178df8bae1dSRodney W. Grimes 		 */
1179df8bae1dSRodney W. Grimes 		if (p->p_wchan && p->p_flag & P_SINTR)
1180df8bae1dSRodney W. Grimes 			unsleep(p);
1181df8bae1dSRodney W. Grimes 		goto out;
1182df8bae1dSRodney W. Grimes 
1183df8bae1dSRodney W. Grimes 	default:
1184df8bae1dSRodney W. Grimes 		/*
1185df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1186df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1187df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1188df8bae1dSRodney W. Grimes 		 */
1189df8bae1dSRodney W. Grimes 		if (p == curproc)
1190df8bae1dSRodney W. Grimes 			signotify(p);
11913163861cSTor Egge #ifdef SMP
11923163861cSTor Egge 		else if (p->p_stat == SRUN)
11933163861cSTor Egge 			forward_signal(p);
11943163861cSTor Egge #endif
1195df8bae1dSRodney W. Grimes 		goto out;
1196df8bae1dSRodney W. Grimes 	}
1197df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1198df8bae1dSRodney W. Grimes 
1199df8bae1dSRodney W. Grimes runfast:
1200df8bae1dSRodney W. Grimes 	/*
1201df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1202df8bae1dSRodney W. Grimes 	 */
1203df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
1204df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
1205df8bae1dSRodney W. Grimes run:
1206df8bae1dSRodney W. Grimes 	setrunnable(p);
1207df8bae1dSRodney W. Grimes out:
1208df8bae1dSRodney W. Grimes 	splx(s);
1209df8bae1dSRodney W. Grimes }
1210df8bae1dSRodney W. Grimes 
1211df8bae1dSRodney W. Grimes /*
1212df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1213df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1214df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1215df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1216df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1217df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1218df8bae1dSRodney W. Grimes  * sequence is
1219df8bae1dSRodney W. Grimes  *
12202c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
12212c42a146SMarcel Moolenaar  *		postsig(sig);
1222df8bae1dSRodney W. Grimes  */
122326f9a767SRodney W. Grimes int
1224df8bae1dSRodney W. Grimes issignal(p)
1225df8bae1dSRodney W. Grimes 	register struct proc *p;
1226df8bae1dSRodney W. Grimes {
12272c42a146SMarcel Moolenaar 	sigset_t mask;
12282c42a146SMarcel Moolenaar 	register int sig, prop;
1229df8bae1dSRodney W. Grimes 
1230df8bae1dSRodney W. Grimes 	for (;;) {
12312a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
12322a024a2bSSean Eric Fagan 
12332c42a146SMarcel Moolenaar 		mask = p->p_siglist;
12342c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1235df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
12362c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
12372c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))	 	/* no signal to send */
1238df8bae1dSRodney W. Grimes 			return (0);
12392c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
12402c42a146SMarcel Moolenaar 		prop = sigprop(sig);
12412a024a2bSSean Eric Fagan 
12422c42a146SMarcel Moolenaar 		STOPEVENT(p, S_SIG, sig);
12432a024a2bSSean Eric Fagan 
1244df8bae1dSRodney W. Grimes 		/*
1245df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1246df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1247df8bae1dSRodney W. Grimes 		 */
12482c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
12492c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1250df8bae1dSRodney W. Grimes 			continue;
1251df8bae1dSRodney W. Grimes 		}
1252df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1253df8bae1dSRodney W. Grimes 			/*
1254df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1255df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1256df8bae1dSRodney W. Grimes 			 */
12572c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1258df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1259df8bae1dSRodney W. Grimes 			do {
1260df8bae1dSRodney W. Grimes 				stop(p);
1261df8bae1dSRodney W. Grimes 				mi_switch();
12622a024a2bSSean Eric Fagan 			} while (!trace_req(p)
12632a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1264df8bae1dSRodney W. Grimes 
1265df8bae1dSRodney W. Grimes 			/*
1266df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1267df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1268df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1269df8bae1dSRodney W. Grimes 			 */
1270df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1271df8bae1dSRodney W. Grimes 				continue;
1272df8bae1dSRodney W. Grimes 
1273df8bae1dSRodney W. Grimes 			/*
1274df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1275df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1276df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1277df8bae1dSRodney W. Grimes 			 */
12782c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
12792c42a146SMarcel Moolenaar 			sig = p->p_xstat;
12802c42a146SMarcel Moolenaar 			if (sig == 0)
1281df8bae1dSRodney W. Grimes 				continue;
1282df8bae1dSRodney W. Grimes 
1283df8bae1dSRodney W. Grimes 			/*
1284df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1285df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1286df8bae1dSRodney W. Grimes 			 */
12872c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
12882c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1289df8bae1dSRodney W. Grimes 				continue;
1290df8bae1dSRodney W. Grimes 		}
1291df8bae1dSRodney W. Grimes 
1292df8bae1dSRodney W. Grimes 		/*
1293df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1294df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1295df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1296df8bae1dSRodney W. Grimes 		 */
12972c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1298df8bae1dSRodney W. Grimes 
12990b53fbe8SBruce Evans 		case (int)SIG_DFL:
1300df8bae1dSRodney W. Grimes 			/*
1301df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1302df8bae1dSRodney W. Grimes 			 */
1303df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1304df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1305df8bae1dSRodney W. Grimes 				/*
1306df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1307df8bae1dSRodney W. Grimes 				 * in init? XXX
1308df8bae1dSRodney W. Grimes 				 */
1309d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
13102c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1311df8bae1dSRodney W. Grimes #endif
1312df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1313df8bae1dSRodney W. Grimes 			}
1314df8bae1dSRodney W. Grimes 			/*
1315df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1316df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1317df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1318df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1319df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1320df8bae1dSRodney W. Grimes 			 */
1321df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1322df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1323df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1324df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1325df8bae1dSRodney W. Grimes 					break;	/* == ignore */
13262c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1327df8bae1dSRodney W. Grimes 				stop(p);
1328645682fdSLuoqi Chen 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1329df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1330df8bae1dSRodney W. Grimes 				mi_switch();
1331df8bae1dSRodney W. Grimes 				break;
1332df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1333df8bae1dSRodney W. Grimes 				/*
1334df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1335df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1336df8bae1dSRodney W. Grimes 				 */
1337df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1338df8bae1dSRodney W. Grimes 			} else
13392c42a146SMarcel Moolenaar 				return (sig);
1340df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1341df8bae1dSRodney W. Grimes 
13420b53fbe8SBruce Evans 		case (int)SIG_IGN:
1343df8bae1dSRodney W. Grimes 			/*
1344df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1345df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1346df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1347df8bae1dSRodney W. Grimes 			 */
1348df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1349df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1350df8bae1dSRodney W. Grimes 				printf("issignal\n");
1351df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1352df8bae1dSRodney W. Grimes 
1353df8bae1dSRodney W. Grimes 		default:
1354df8bae1dSRodney W. Grimes 			/*
1355df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1356df8bae1dSRodney W. Grimes 			 * postsig() process it.
1357df8bae1dSRodney W. Grimes 			 */
13582c42a146SMarcel Moolenaar 			return (sig);
1359df8bae1dSRodney W. Grimes 		}
13602c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1361df8bae1dSRodney W. Grimes 	}
1362df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1363df8bae1dSRodney W. Grimes }
1364df8bae1dSRodney W. Grimes 
1365df8bae1dSRodney W. Grimes /*
1366df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1367df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
1368df8bae1dSRodney W. Grimes  * on the run queue.
1369df8bae1dSRodney W. Grimes  */
137026f9a767SRodney W. Grimes void
1371df8bae1dSRodney W. Grimes stop(p)
1372df8bae1dSRodney W. Grimes 	register struct proc *p;
1373df8bae1dSRodney W. Grimes {
1374df8bae1dSRodney W. Grimes 
1375df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1376df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1377df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1378df8bae1dSRodney W. Grimes }
1379df8bae1dSRodney W. Grimes 
1380df8bae1dSRodney W. Grimes /*
1381df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1382df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1383df8bae1dSRodney W. Grimes  */
1384df8bae1dSRodney W. Grimes void
13852c42a146SMarcel Moolenaar postsig(sig)
13862c42a146SMarcel Moolenaar 	register int sig;
1387df8bae1dSRodney W. Grimes {
1388df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
13892c42a146SMarcel Moolenaar 	struct sigacts *ps = p->p_sigacts;
13902c42a146SMarcel Moolenaar 	sig_t action;
13912c42a146SMarcel Moolenaar 	sigset_t returnmask;
13922c42a146SMarcel Moolenaar 	int code;
1393df8bae1dSRodney W. Grimes 
13942c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
13955526d2d9SEivind Eklund 
13962c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
13972c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1398df8bae1dSRodney W. Grimes #ifdef KTRACE
1399df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1400645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
14012c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1402df8bae1dSRodney W. Grimes #endif
14032c42a146SMarcel Moolenaar 	STOPEVENT(p, S_SIG, sig);
14042a024a2bSSean Eric Fagan 
1405df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1406df8bae1dSRodney W. Grimes 		/*
1407df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1408df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1409df8bae1dSRodney W. Grimes 		 */
14102c42a146SMarcel Moolenaar 		sigexit(p, sig);
1411df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1412df8bae1dSRodney W. Grimes 	} else {
1413df8bae1dSRodney W. Grimes 		/*
1414df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1415df8bae1dSRodney W. Grimes 		 */
14162c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
14175526d2d9SEivind Eklund 		    ("postsig action"));
1418df8bae1dSRodney W. Grimes 		/*
1419df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1420645682fdSLuoqi Chen 		 * occurrences of this signal.
1421df8bae1dSRodney W. Grimes 		 *
1422645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1423df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1424645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1425df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1426df8bae1dSRodney W. Grimes 		 */
1427df8bae1dSRodney W. Grimes 		(void) splhigh();
1428645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
14296626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1430645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1431df8bae1dSRodney W. Grimes 		} else
1432df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
14332c42a146SMarcel Moolenaar 
14342c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
14352c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
14362c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
14372c42a146SMarcel Moolenaar 
14382c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1439289ccde0SPeter Wemm 			/*
14402c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1441289ccde0SPeter Wemm 			 */
14422c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
14432c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
14442c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
14452c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
14462c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1447dedc04feSPeter Wemm 		}
1448df8bae1dSRodney W. Grimes 		(void) spl0();
1449df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
14502c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1451df8bae1dSRodney W. Grimes 			code = 0;
1452df8bae1dSRodney W. Grimes 		} else {
14536626c604SJulian Elischer 			code = p->p_code;
14546626c604SJulian Elischer 			p->p_code = 0;
14556626c604SJulian Elischer 			p->p_sig = 0;
1456df8bae1dSRodney W. Grimes 		}
14572c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1458df8bae1dSRodney W. Grimes 	}
1459df8bae1dSRodney W. Grimes }
1460df8bae1dSRodney W. Grimes 
1461df8bae1dSRodney W. Grimes /*
1462df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1463df8bae1dSRodney W. Grimes  */
146426f9a767SRodney W. Grimes void
1465df8bae1dSRodney W. Grimes killproc(p, why)
1466df8bae1dSRodney W. Grimes 	struct proc *p;
1467df8bae1dSRodney W. Grimes 	char *why;
1468df8bae1dSRodney W. Grimes {
14690384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
14700384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1471729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1472729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1473df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1474df8bae1dSRodney W. Grimes }
1475df8bae1dSRodney W. Grimes 
1476df8bae1dSRodney W. Grimes /*
1477df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1478df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1479df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1480df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1481df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1482df8bae1dSRodney W. Grimes  * does not return.
1483df8bae1dSRodney W. Grimes  */
148426f9a767SRodney W. Grimes void
14852c42a146SMarcel Moolenaar sigexit(p, sig)
1486df8bae1dSRodney W. Grimes 	register struct proc *p;
14872c42a146SMarcel Moolenaar 	int sig;
1488df8bae1dSRodney W. Grimes {
1489df8bae1dSRodney W. Grimes 
1490df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
14912c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
14922c42a146SMarcel Moolenaar 		p->p_sig = sig;
1493c364e17eSAndrey A. Chernov 		/*
1494c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1495c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1496c364e17eSAndrey A. Chernov 		 * these messages.)
1497c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1498c364e17eSAndrey A. Chernov 		 */
1499fca666a1SJulian Elischer 		if (coredump(p) == 0)
15002c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
150157308494SJoerg Wunsch 		if (kern_logsigexit)
150257308494SJoerg Wunsch 			log(LOG_INFO,
150357308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
15043d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
15053d1b21c6SAndrey A. Chernov 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
15062c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
15072c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1508df8bae1dSRodney W. Grimes 	}
15092c42a146SMarcel Moolenaar 	exit1(p, W_EXITCODE(0, sig));
1510df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1511df8bae1dSRodney W. Grimes }
1512df8bae1dSRodney W. Grimes 
1513c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1514c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1515c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1516c5edb423SSean Eric Fagan 
1517c5edb423SSean Eric Fagan /*
1518c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1519c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1520c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1521c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1522c5edb423SSean Eric Fagan  *	%P	process id (pid)
1523c5edb423SSean Eric Fagan  *	%U	user id (uid)
1524c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1525c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1526c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1527c5edb423SSean Eric Fagan  */
1528c5edb423SSean Eric Fagan 
1529fca666a1SJulian Elischer static char *
1530c5edb423SSean Eric Fagan expand_name(name, uid, pid)
153187f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1532c5edb423SSean Eric Fagan 	char *temp;
1533c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1534c5edb423SSean Eric Fagan 	int i, n;
1535c5edb423SSean Eric Fagan 	char *format = corefilename;
1536ce38ca0fSAlfred Perlstein 	size_t namelen;
1537c5edb423SSean Eric Fagan 
1538ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
15390bfe2990SEivind Eklund 	if (temp == NULL)
15400bfe2990SEivind Eklund 		return NULL;
1541ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1542ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1543c5edb423SSean Eric Fagan 		int l;
1544c5edb423SSean Eric Fagan 		switch (format[i]) {
1545c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1546c5edb423SSean Eric Fagan 			i++;
1547c5edb423SSean Eric Fagan 			switch (format[i]) {
1548c5edb423SSean Eric Fagan 			case '%':
1549c5edb423SSean Eric Fagan 				temp[n++] = '%';
1550c5edb423SSean Eric Fagan 				break;
1551c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1552ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
155387f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1554c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1555c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1556c5edb423SSean Eric Fagan 					return NULL;
1557c5edb423SSean Eric Fagan 				}
1558ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1559ce38ca0fSAlfred Perlstein 				n += namelen;
1560c5edb423SSean Eric Fagan 				break;
1561c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1562ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1563c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
156487f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1565c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1566c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1567c5edb423SSean Eric Fagan 					return NULL;
1568c5edb423SSean Eric Fagan 				}
1569c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1570c5edb423SSean Eric Fagan 				n += l;
1571c5edb423SSean Eric Fagan 				break;
1572c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1573ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1574c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
157587f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1576c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1577c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1578c5edb423SSean Eric Fagan 					return NULL;
1579c5edb423SSean Eric Fagan 				}
1580c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1581c5edb423SSean Eric Fagan 				n += l;
1582c5edb423SSean Eric Fagan 				break;
1583c5edb423SSean Eric Fagan 			default:
1584c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1585c5edb423SSean Eric Fagan 			}
1586c5edb423SSean Eric Fagan 			break;
1587c5edb423SSean Eric Fagan 		default:
1588c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1589c5edb423SSean Eric Fagan 		}
1590c5edb423SSean Eric Fagan 	}
1591ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1592c5edb423SSean Eric Fagan 	return temp;
1593c5edb423SSean Eric Fagan }
1594c5edb423SSean Eric Fagan 
1595df8bae1dSRodney W. Grimes /*
1596fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1597fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1598fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1599fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1600fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1601fca666a1SJulian Elischer  */
1602fca666a1SJulian Elischer 
1603fca666a1SJulian Elischer static int
1604fca666a1SJulian Elischer coredump(p)
1605fca666a1SJulian Elischer 	register struct proc *p;
1606fca666a1SJulian Elischer {
1607fca666a1SJulian Elischer 	register struct vnode *vp;
1608da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
1609fca666a1SJulian Elischer 	struct nameidata nd;
1610fca666a1SJulian Elischer 	struct vattr vattr;
1611e6796b67SKirk McKusick 	int error, error1, flags;
1612f2a2857bSKirk McKusick 	struct mount *mp;
1613fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1614fca666a1SJulian Elischer 	off_t limit;
1615fca666a1SJulian Elischer 
1616fca666a1SJulian Elischer 	STOPEVENT(p, S_CORE, 0);
1617fca666a1SJulian Elischer 
1618e5a28db9SPaul Saab 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1619fca666a1SJulian Elischer 		return (EFAULT);
1620fca666a1SJulian Elischer 
1621fca666a1SJulian Elischer 	/*
162235a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
162335a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
162435a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
162535a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
162635a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
162735a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1628fca666a1SJulian Elischer 	 */
162935a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
163035a2598fSSean Eric Fagan 	if (limit == 0)
163135a2598fSSean Eric Fagan 		return 0;
163235a2598fSSean Eric Fagan 
1633f2a2857bSKirk McKusick restart:
1634fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1635fca666a1SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1636e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1637e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1638fca666a1SJulian Elischer 	free(name, M_TEMP);
1639fca666a1SJulian Elischer 	if (error)
1640fca666a1SJulian Elischer 		return (error);
1641762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1642fca666a1SJulian Elischer 	vp = nd.ni_vp;
1643f2a2857bSKirk McKusick 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1644f2a2857bSKirk McKusick 		VOP_UNLOCK(vp, 0, p);
1645f2a2857bSKirk McKusick 		if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
1646f2a2857bSKirk McKusick 			return (error);
1647f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1648f2a2857bSKirk McKusick 			return (error);
1649f2a2857bSKirk McKusick 		goto restart;
1650f2a2857bSKirk McKusick 	}
1651fca666a1SJulian Elischer 
1652fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
1653fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
1654fca666a1SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1655fca666a1SJulian Elischer 		error = EFAULT;
1656fca666a1SJulian Elischer 		goto out;
1657fca666a1SJulian Elischer 	}
1658fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
1659fca666a1SJulian Elischer 	vattr.va_size = 0;
1660fca666a1SJulian Elischer 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1661fca666a1SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, p);
1662fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
1663fca666a1SJulian Elischer 
1664fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
1665fca666a1SJulian Elischer 	  p->p_sysent->sv_coredump(p, vp, limit) :
1666fca666a1SJulian Elischer 	  ENOSYS;
1667fca666a1SJulian Elischer 
1668fca666a1SJulian Elischer out:
1669fca666a1SJulian Elischer 	VOP_UNLOCK(vp, 0, p);
1670f2a2857bSKirk McKusick 	vn_finished_write(mp);
1671fca666a1SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, p);
1672fca666a1SJulian Elischer 	if (error == 0)
1673fca666a1SJulian Elischer 		error = error1;
1674fca666a1SJulian Elischer 	return (error);
1675fca666a1SJulian Elischer }
1676fca666a1SJulian Elischer 
1677fca666a1SJulian Elischer /*
1678df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1679df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1680df8bae1dSRodney W. Grimes  */
1681d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1682df8bae1dSRodney W. Grimes struct nosys_args {
1683df8bae1dSRodney W. Grimes 	int	dummy;
1684df8bae1dSRodney W. Grimes };
1685d2d3e875SBruce Evans #endif
1686df8bae1dSRodney W. Grimes /* ARGSUSED */
168726f9a767SRodney W. Grimes int
1688cb226aaaSPoul-Henning Kamp nosys(p, args)
1689df8bae1dSRodney W. Grimes 	struct proc *p;
1690df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1691df8bae1dSRodney W. Grimes {
1692df8bae1dSRodney W. Grimes 
1693df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1694df8bae1dSRodney W. Grimes 	return (EINVAL);
1695df8bae1dSRodney W. Grimes }
1696831d27a9SDon Lewis 
1697831d27a9SDon Lewis /*
1698831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1699831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1700831d27a9SDon Lewis  */
1701831d27a9SDon Lewis void
17022c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
1703831d27a9SDon Lewis 	struct sigio *sigio;
17042c42a146SMarcel Moolenaar 	int sig, checkctty;
1705831d27a9SDon Lewis {
1706831d27a9SDon Lewis 	if (sigio == NULL)
1707831d27a9SDon Lewis 		return;
1708831d27a9SDon Lewis 
1709831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1710831d27a9SDon Lewis 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1711831d27a9SDon Lewis 		             sigio->sio_proc))
17122c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
1713831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1714831d27a9SDon Lewis 		struct proc *p;
1715831d27a9SDon Lewis 
17162e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1717831d27a9SDon Lewis 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1718831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
17192c42a146SMarcel Moolenaar 				psignal(p, sig);
1720831d27a9SDon Lewis 	}
1721831d27a9SDon Lewis }
1722cb679c38SJonathan Lemon 
1723cb679c38SJonathan Lemon static int
1724cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
1725cb679c38SJonathan Lemon {
1726cb679c38SJonathan Lemon 	struct proc *p = curproc;
1727cb679c38SJonathan Lemon 
1728cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
1729cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1730cb679c38SJonathan Lemon 
1731cb679c38SJonathan Lemon 	/* XXX lock the proc here while adding to the list? */
1732cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1733cb679c38SJonathan Lemon 
1734cb679c38SJonathan Lemon 	return (0);
1735cb679c38SJonathan Lemon }
1736cb679c38SJonathan Lemon 
1737cb679c38SJonathan Lemon static void
1738cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
1739cb679c38SJonathan Lemon {
1740cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
1741cb679c38SJonathan Lemon 
1742e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1743cb679c38SJonathan Lemon }
1744cb679c38SJonathan Lemon 
1745cb679c38SJonathan Lemon /*
1746cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
1747cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
1748cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
1749cb679c38SJonathan Lemon  * isn't worth the trouble.
1750cb679c38SJonathan Lemon  */
1751cb679c38SJonathan Lemon static int
1752cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
1753cb679c38SJonathan Lemon {
1754cb679c38SJonathan Lemon 
1755cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
1756cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
1757cb679c38SJonathan Lemon 
1758cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
1759cb679c38SJonathan Lemon 			kn->kn_data++;
1760cb679c38SJonathan Lemon 	}
1761cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
1762cb679c38SJonathan Lemon }
1763