xref: /freebsd/sys/kern/kern_sig.c (revision 553629ebc915f2377f1f2ce1bf02349db973e5ec)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46c87e2930SDavid Greenman #include <sys/kernel.h>
47d2d3e875SBruce Evans #include <sys/sysproto.h>
4836240ea5SDoug Rabson #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
51df8bae1dSRodney W. Grimes #include <sys/namei.h>
52df8bae1dSRodney W. Grimes #include <sys/vnode.h>
53cb679c38SJonathan Lemon #include <sys/event.h>
54df8bae1dSRodney W. Grimes #include <sys/proc.h>
552a024a2bSSean Eric Fagan #include <sys/pioctl.h>
56df8bae1dSRodney W. Grimes #include <sys/acct.h>
573ac4d1efSBruce Evans #include <sys/fcntl.h>
5835e0e5b3SJohn Baldwin #include <sys/ipl.h>
5935e0e5b3SJohn Baldwin #include <sys/mutex.h>
60df8bae1dSRodney W. Grimes #include <sys/wait.h>
610384fff8SJason Evans #include <sys/ktr.h>
62df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
63df8bae1dSRodney W. Grimes #include <sys/syslog.h>
64df8bae1dSRodney W. Grimes #include <sys/stat.h>
65d66a5066SPeter Wemm #include <sys/sysent.h>
66c87e2930SDavid Greenman #include <sys/sysctl.h>
67c5edb423SSean Eric Fagan #include <sys/malloc.h>
68df8bae1dSRodney W. Grimes 
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 
169fbbeeb6cSBruce Evans /*
170fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
171fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
172fbbeeb6cSBruce Evans  * action, the process stops in issignal().
173fbbeeb6cSBruce Evans  *
17433510ef1SBruce Evans  * MP SAFE.
175fbbeeb6cSBruce Evans  */
176fbbeeb6cSBruce Evans int
177fbbeeb6cSBruce Evans CURSIG(struct proc *p)
178fbbeeb6cSBruce Evans {
179fbbeeb6cSBruce Evans 	sigset_t tmpset;
180fbbeeb6cSBruce Evans 	int r;
181fbbeeb6cSBruce Evans 
18233510ef1SBruce Evans 	if (SIGISEMPTY(p->p_siglist))
18333510ef1SBruce Evans 		return (0);
184fbbeeb6cSBruce Evans 	tmpset = p->p_siglist;
185fbbeeb6cSBruce Evans 	SIGSETNAND(tmpset, p->p_sigmask);
18633510ef1SBruce Evans 	if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
187fbbeeb6cSBruce Evans 		return (0);
188fbbeeb6cSBruce Evans 	mtx_enter(&Giant, MTX_DEF);
189fbbeeb6cSBruce Evans 	r = issignal(p);
190fbbeeb6cSBruce Evans 	mtx_exit(&Giant, MTX_DEF);
191fbbeeb6cSBruce Evans 	return (r);
192fbbeeb6cSBruce Evans }
193fbbeeb6cSBruce Evans 
1946f841fb7SMarcel Moolenaar static __inline int
1956f841fb7SMarcel Moolenaar sigprop(int sig)
1962c42a146SMarcel Moolenaar {
1976f841fb7SMarcel Moolenaar 
1982c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
1992c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2002c42a146SMarcel Moolenaar 	return (0);
201df8bae1dSRodney W. Grimes }
2022c42a146SMarcel Moolenaar 
2036f841fb7SMarcel Moolenaar static __inline int
2046f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2052c42a146SMarcel Moolenaar {
2062c42a146SMarcel Moolenaar 	int i;
2072c42a146SMarcel Moolenaar 
2086f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2092c42a146SMarcel Moolenaar 		if (set->__bits[i])
2102c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
211df8bae1dSRodney W. Grimes 	return (0);
212df8bae1dSRodney W. Grimes }
213df8bae1dSRodney W. Grimes 
2142c42a146SMarcel Moolenaar /*
2152c42a146SMarcel Moolenaar  * do_sigaction
2162c42a146SMarcel Moolenaar  * sigaction
2172c42a146SMarcel Moolenaar  * osigaction
2182c42a146SMarcel Moolenaar  */
2192c42a146SMarcel Moolenaar static int
220645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
2212c42a146SMarcel Moolenaar 	struct proc *p;
2222c42a146SMarcel Moolenaar 	register int sig;
2232c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
224645682fdSLuoqi Chen 	int old;
225df8bae1dSRodney W. Grimes {
226df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
227df8bae1dSRodney W. Grimes 
2282c42a146SMarcel Moolenaar 	if (sig <= 0 || sig > _SIG_MAXSIG)
2292c42a146SMarcel Moolenaar 		return (EINVAL);
2302c42a146SMarcel Moolenaar 
2312c42a146SMarcel Moolenaar 	if (oact) {
2322c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2332c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2342c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2352c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2362c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2372c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2382c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2392c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2402c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2412c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2422c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2432c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2442c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
245645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2462c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
247645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2482c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2492c42a146SMarcel Moolenaar 	}
2502c42a146SMarcel Moolenaar 	if (act) {
2512c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
2522c42a146SMarcel Moolenaar 		    act->sa_handler != SIG_DFL)
2532c42a146SMarcel Moolenaar 			return (EINVAL);
2542c42a146SMarcel Moolenaar 
255df8bae1dSRodney W. Grimes 		/*
256df8bae1dSRodney W. Grimes 		 * Change setting atomically.
257df8bae1dSRodney W. Grimes 		 */
258df8bae1dSRodney W. Grimes 		(void) splhigh();
2592c42a146SMarcel Moolenaar 
2602c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2612c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2622c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
2632c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2642c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_siginfo, sig);
2656f841fb7SMarcel Moolenaar 		} else {
2662c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] =
2672c42a146SMarcel Moolenaar 			    (__sighandler_t *)act->sa_sigaction;
2682c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2692c42a146SMarcel Moolenaar 		}
2702c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2712c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
272df8bae1dSRodney W. Grimes 		else
2732c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2742c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2752c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
276df8bae1dSRodney W. Grimes 		else
2772c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2782c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2792c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2801e41c1b5SSteven Wallace 		else
2812c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2822c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2832c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
284289ccde0SPeter Wemm 		else
2852c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
286df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2872c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2882c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
289df8bae1dSRodney W. Grimes 		else
2902c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_usertramp, seg);
291df8bae1dSRodney W. Grimes #endif
2922c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2932c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
294645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
2956626c604SJulian Elischer 			else
296645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
2972c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDWAIT) {
298245f17d4SJoerg Wunsch 				/*
2992c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3002c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3012c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3022c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
303245f17d4SJoerg Wunsch 				 */
304245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
305645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3066626c604SJulian Elischer 				else
307645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
308245f17d4SJoerg Wunsch 			} else
309645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
310df8bae1dSRodney W. Grimes 		}
311df8bae1dSRodney W. Grimes 		/*
312df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3132c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3142c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3152c42a146SMarcel Moolenaar 		 * have to restart the process.
316df8bae1dSRodney W. Grimes 		 */
3172c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3182c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3192c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3202c42a146SMarcel Moolenaar 			/* never to be seen again */
3212c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
3222c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3232c42a146SMarcel Moolenaar 				/* easier in psignal */
3242c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3252c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
326645682fdSLuoqi Chen 		} else {
3272c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3282c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3292c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3302c42a146SMarcel Moolenaar 			else
3312c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3322c42a146SMarcel Moolenaar 		}
333645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
334645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
335645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
336645682fdSLuoqi Chen 		else
337645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
3382c42a146SMarcel Moolenaar 
339df8bae1dSRodney W. Grimes 		(void) spl0();
340df8bae1dSRodney W. Grimes 	}
3412c42a146SMarcel Moolenaar 	return (0);
3422c42a146SMarcel Moolenaar }
3432c42a146SMarcel Moolenaar 
3442c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3452c42a146SMarcel Moolenaar struct sigaction_args {
3462c42a146SMarcel Moolenaar 	int	sig;
3472c42a146SMarcel Moolenaar 	struct	sigaction *act;
3482c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3492c42a146SMarcel Moolenaar };
3502c42a146SMarcel Moolenaar #endif
3512c42a146SMarcel Moolenaar /* ARGSUSED */
3522c42a146SMarcel Moolenaar int
3532c42a146SMarcel Moolenaar sigaction(p, uap)
3542c42a146SMarcel Moolenaar 	struct proc *p;
3552c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3562c42a146SMarcel Moolenaar {
3572c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3582c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3592c42a146SMarcel Moolenaar 	int error;
3602c42a146SMarcel Moolenaar 
3616f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3626f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3632c42a146SMarcel Moolenaar 	if (actp) {
3646f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3652c42a146SMarcel Moolenaar 		if (error)
3662c42a146SMarcel Moolenaar 			return (error);
3672c42a146SMarcel Moolenaar 	}
368645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3692c42a146SMarcel Moolenaar 	if (oactp && !error) {
3706f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3712c42a146SMarcel Moolenaar 	}
3722c42a146SMarcel Moolenaar 	return (error);
3732c42a146SMarcel Moolenaar }
3742c42a146SMarcel Moolenaar 
37531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
3762c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3772c42a146SMarcel Moolenaar struct osigaction_args {
3782c42a146SMarcel Moolenaar 	int	signum;
3792c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3802c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3812c42a146SMarcel Moolenaar };
3822c42a146SMarcel Moolenaar #endif
3832c42a146SMarcel Moolenaar /* ARGSUSED */
3842c42a146SMarcel Moolenaar int
3852c42a146SMarcel Moolenaar osigaction(p, uap)
3862c42a146SMarcel Moolenaar 	struct proc *p;
3872c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3882c42a146SMarcel Moolenaar {
3892c42a146SMarcel Moolenaar 	struct osigaction sa;
3902c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
3912c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
3922c42a146SMarcel Moolenaar 	int error;
3932c42a146SMarcel Moolenaar 
3946f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
3956f841fb7SMarcel Moolenaar 		return (EINVAL);
3966f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
3976f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
3982c42a146SMarcel Moolenaar 	if (nsap) {
3996f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4002c42a146SMarcel Moolenaar 		if (error)
4012c42a146SMarcel Moolenaar 			return (error);
4022c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4032c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4042c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4052c42a146SMarcel Moolenaar 	}
406645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
4072c42a146SMarcel Moolenaar 	if (osap && !error) {
4082c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4092c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4102c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4116f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4122c42a146SMarcel Moolenaar 	}
4132c42a146SMarcel Moolenaar 	return (error);
4142c42a146SMarcel Moolenaar }
41531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes /*
418df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
419df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
420df8bae1dSRodney W. Grimes  */
421df8bae1dSRodney W. Grimes void
422df8bae1dSRodney W. Grimes siginit(p)
423df8bae1dSRodney W. Grimes 	struct proc *p;
424df8bae1dSRodney W. Grimes {
425df8bae1dSRodney W. Grimes 	register int i;
426df8bae1dSRodney W. Grimes 
4272c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4282c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4292c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
430df8bae1dSRodney W. Grimes }
431df8bae1dSRodney W. Grimes 
432df8bae1dSRodney W. Grimes /*
433df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
434df8bae1dSRodney W. Grimes  */
435df8bae1dSRodney W. Grimes void
436df8bae1dSRodney W. Grimes execsigs(p)
437df8bae1dSRodney W. Grimes 	register struct proc *p;
438df8bae1dSRodney W. Grimes {
439df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
4402c42a146SMarcel Moolenaar 	register int sig;
441df8bae1dSRodney W. Grimes 
442df8bae1dSRodney W. Grimes 	/*
443df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
444df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
445df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
446df8bae1dSRodney W. Grimes 	 */
4472c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4482c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4492c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4502c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4512c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4522c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4532c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
454df8bae1dSRodney W. Grimes 		}
4552c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
456df8bae1dSRodney W. Grimes 	}
457df8bae1dSRodney W. Grimes 	/*
458df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
459df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
460df8bae1dSRodney W. Grimes 	 */
461645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
462645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
463645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
46480e907a1SPeter Wemm 	/*
46580e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
46680e907a1SPeter Wemm 	 */
467645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
468df8bae1dSRodney W. Grimes }
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes /*
4717c8fdcbdSMatthew Dillon  * do_sigprocmask() - MP SAFE ONLY IF p == curproc
4727c8fdcbdSMatthew Dillon  *
4737c8fdcbdSMatthew Dillon  *	Manipulate signal mask.  This routine is MP SAFE *ONLY* if
4747c8fdcbdSMatthew Dillon  *	p == curproc.  Also remember that in order to remain MP SAFE
4757c8fdcbdSMatthew Dillon  *	no spl*() calls may be made.
476df8bae1dSRodney W. Grimes  */
4772c42a146SMarcel Moolenaar static int
478645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
4792c42a146SMarcel Moolenaar 	struct proc *p;
4802c42a146SMarcel Moolenaar 	int how;
4812c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
482645682fdSLuoqi Chen 	int old;
4832c42a146SMarcel Moolenaar {
4842c42a146SMarcel Moolenaar 	int error;
4852c42a146SMarcel Moolenaar 
4862c42a146SMarcel Moolenaar 	if (oset != NULL)
4872c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
4882c42a146SMarcel Moolenaar 
4892c42a146SMarcel Moolenaar 	error = 0;
4902c42a146SMarcel Moolenaar 	if (set != NULL) {
4912c42a146SMarcel Moolenaar 		switch (how) {
4922c42a146SMarcel Moolenaar 		case SIG_BLOCK:
493645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
4942c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
4952c42a146SMarcel Moolenaar 			break;
4962c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
4972c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
4982c42a146SMarcel Moolenaar 			break;
4992c42a146SMarcel Moolenaar 		case SIG_SETMASK:
500645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
501645682fdSLuoqi Chen 			if (old)
502645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
503645682fdSLuoqi Chen 			else
5042c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
5052c42a146SMarcel Moolenaar 			break;
5062c42a146SMarcel Moolenaar 		default:
5072c42a146SMarcel Moolenaar 			error = EINVAL;
5082c42a146SMarcel Moolenaar 			break;
5092c42a146SMarcel Moolenaar 		}
5102c42a146SMarcel Moolenaar 	}
5112c42a146SMarcel Moolenaar 	return (error);
5122c42a146SMarcel Moolenaar }
5132c42a146SMarcel Moolenaar 
5147c8fdcbdSMatthew Dillon /*
5157c8fdcbdSMatthew Dillon  * sigprocmask() - MP SAFE
5167c8fdcbdSMatthew Dillon  */
5177c8fdcbdSMatthew Dillon 
518d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
519df8bae1dSRodney W. Grimes struct sigprocmask_args {
520df8bae1dSRodney W. Grimes 	int	how;
5212c42a146SMarcel Moolenaar 	const sigset_t *set;
5222c42a146SMarcel Moolenaar 	sigset_t *oset;
523df8bae1dSRodney W. Grimes };
524d2d3e875SBruce Evans #endif
52526f9a767SRodney W. Grimes int
526cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
527df8bae1dSRodney W. Grimes 	register struct proc *p;
528df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
529df8bae1dSRodney W. Grimes {
5302c42a146SMarcel Moolenaar 	sigset_t set, oset;
5312c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5322c42a146SMarcel Moolenaar 	int error;
533df8bae1dSRodney W. Grimes 
5346f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5356f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5362c42a146SMarcel Moolenaar 	if (setp) {
5376f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5382c42a146SMarcel Moolenaar 		if (error)
5392c42a146SMarcel Moolenaar 			return (error);
540df8bae1dSRodney W. Grimes 	}
541645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5422c42a146SMarcel Moolenaar 	if (osetp && !error) {
5436f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5442c42a146SMarcel Moolenaar 	}
5452c42a146SMarcel Moolenaar 	return (error);
5462c42a146SMarcel Moolenaar }
5472c42a146SMarcel Moolenaar 
54831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5497c8fdcbdSMatthew Dillon /*
5507c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5517c8fdcbdSMatthew Dillon  */
5522c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5532c42a146SMarcel Moolenaar struct osigprocmask_args {
5542c42a146SMarcel Moolenaar 	int	how;
5552c42a146SMarcel Moolenaar 	osigset_t mask;
5562c42a146SMarcel Moolenaar };
5572c42a146SMarcel Moolenaar #endif
5582c42a146SMarcel Moolenaar int
5592c42a146SMarcel Moolenaar osigprocmask(p, uap)
5602c42a146SMarcel Moolenaar 	register struct proc *p;
5612c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5622c42a146SMarcel Moolenaar {
5632c42a146SMarcel Moolenaar 	sigset_t set, oset;
5642c42a146SMarcel Moolenaar 	int error;
5652c42a146SMarcel Moolenaar 
5662c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
567645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
5682c42a146SMarcel Moolenaar 	SIG2OSIG(oset, p->p_retval[0]);
569df8bae1dSRodney W. Grimes 	return (error);
570df8bae1dSRodney W. Grimes }
57131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
572df8bae1dSRodney W. Grimes 
573d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
574df8bae1dSRodney W. Grimes struct sigpending_args {
5752c42a146SMarcel Moolenaar 	sigset_t	*set;
576df8bae1dSRodney W. Grimes };
577d2d3e875SBruce Evans #endif
578df8bae1dSRodney W. Grimes /* ARGSUSED */
57926f9a767SRodney W. Grimes int
580cb226aaaSPoul-Henning Kamp sigpending(p, uap)
581df8bae1dSRodney W. Grimes 	struct proc *p;
582df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
583df8bae1dSRodney W. Grimes {
584df8bae1dSRodney W. Grimes 
5856f841fb7SMarcel Moolenaar 	return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
5862c42a146SMarcel Moolenaar }
5872c42a146SMarcel Moolenaar 
58831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
5892c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5902c42a146SMarcel Moolenaar struct osigpending_args {
5912c42a146SMarcel Moolenaar 	int	dummy;
5922c42a146SMarcel Moolenaar };
5932c42a146SMarcel Moolenaar #endif
5942c42a146SMarcel Moolenaar /* ARGSUSED */
5952c42a146SMarcel Moolenaar int
5962c42a146SMarcel Moolenaar osigpending(p, uap)
5972c42a146SMarcel Moolenaar 	struct proc *p;
5982c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
5992c42a146SMarcel Moolenaar {
6002c42a146SMarcel Moolenaar 
6012c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_siglist, p->p_retval[0]);
602df8bae1dSRodney W. Grimes 	return (0);
603df8bae1dSRodney W. Grimes }
60431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
605df8bae1dSRodney W. Grimes 
606df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
607df8bae1dSRodney W. Grimes /*
608df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
609df8bae1dSRodney W. Grimes  */
610d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
611df8bae1dSRodney W. Grimes struct osigvec_args {
612df8bae1dSRodney W. Grimes 	int	signum;
613df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
614df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
615df8bae1dSRodney W. Grimes };
616d2d3e875SBruce Evans #endif
617df8bae1dSRodney W. Grimes /* ARGSUSED */
61826f9a767SRodney W. Grimes int
619cb226aaaSPoul-Henning Kamp osigvec(p, uap)
620df8bae1dSRodney W. Grimes 	struct proc *p;
621df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
622df8bae1dSRodney W. Grimes {
623df8bae1dSRodney W. Grimes 	struct sigvec vec;
6242c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
6252c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
6262c42a146SMarcel Moolenaar 	int error;
627df8bae1dSRodney W. Grimes 
6286f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
6296f841fb7SMarcel Moolenaar 		return (EINVAL);
6306f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6316f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6322c42a146SMarcel Moolenaar 	if (nsap) {
6336f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
6342c42a146SMarcel Moolenaar 		if (error)
635df8bae1dSRodney W. Grimes 			return (error);
6362c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
6372c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
6382c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
6392c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
640df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
6412c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
642df8bae1dSRodney W. Grimes #endif
643df8bae1dSRodney W. Grimes 	}
644645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
6452c42a146SMarcel Moolenaar 	if (osap && !error) {
6462c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
6472c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
6482c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
6492c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
6502c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
6512c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
6522c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
6532c42a146SMarcel Moolenaar #endif
6546f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
6552c42a146SMarcel Moolenaar 	}
6562c42a146SMarcel Moolenaar 	return (error);
657df8bae1dSRodney W. Grimes }
658df8bae1dSRodney W. Grimes 
659d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
660df8bae1dSRodney W. Grimes struct osigblock_args {
661df8bae1dSRodney W. Grimes 	int	mask;
662df8bae1dSRodney W. Grimes };
663d2d3e875SBruce Evans #endif
66426f9a767SRodney W. Grimes int
665cb226aaaSPoul-Henning Kamp osigblock(p, uap)
666df8bae1dSRodney W. Grimes 	register struct proc *p;
667df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
668df8bae1dSRodney W. Grimes {
6692c42a146SMarcel Moolenaar 	sigset_t set;
670df8bae1dSRodney W. Grimes 
6712c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6722c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
673df8bae1dSRodney W. Grimes 	(void) splhigh();
6742c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
6752c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
676df8bae1dSRodney W. Grimes 	(void) spl0();
677df8bae1dSRodney W. Grimes 	return (0);
678df8bae1dSRodney W. Grimes }
679df8bae1dSRodney W. Grimes 
680d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
681df8bae1dSRodney W. Grimes struct osigsetmask_args {
682df8bae1dSRodney W. Grimes 	int	mask;
683df8bae1dSRodney W. Grimes };
684d2d3e875SBruce Evans #endif
68526f9a767SRodney W. Grimes int
686cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
687df8bae1dSRodney W. Grimes 	struct proc *p;
688df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
689df8bae1dSRodney W. Grimes {
6902c42a146SMarcel Moolenaar 	sigset_t set;
691df8bae1dSRodney W. Grimes 
6922c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6932c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
694df8bae1dSRodney W. Grimes 	(void) splhigh();
6952c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
696645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
697df8bae1dSRodney W. Grimes 	(void) spl0();
698df8bae1dSRodney W. Grimes 	return (0);
699df8bae1dSRodney W. Grimes }
700df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
701df8bae1dSRodney W. Grimes 
702df8bae1dSRodney W. Grimes /*
703df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
704df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
705df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
706df8bae1dSRodney W. Grimes  */
707d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
708df8bae1dSRodney W. Grimes struct sigsuspend_args {
7092c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
710df8bae1dSRodney W. Grimes };
711d2d3e875SBruce Evans #endif
712df8bae1dSRodney W. Grimes /* ARGSUSED */
71326f9a767SRodney W. Grimes int
714cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
715df8bae1dSRodney W. Grimes 	register struct proc *p;
716df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
717df8bae1dSRodney W. Grimes {
7182c42a146SMarcel Moolenaar 	sigset_t mask;
719df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
7202c42a146SMarcel Moolenaar 	int error;
7212c42a146SMarcel Moolenaar 
7226f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
7232c42a146SMarcel Moolenaar 	if (error)
7242c42a146SMarcel Moolenaar 		return (error);
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes 	/*
727645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
728df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
729df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
730df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
731df8bae1dSRodney W. Grimes 	 * to indicate this.
732df8bae1dSRodney W. Grimes 	 */
7336626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
734645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
735645682fdSLuoqi Chen 
736645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
7372c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
7382c42a146SMarcel Moolenaar 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
7392c42a146SMarcel Moolenaar 		/* void */;
7402c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
7412c42a146SMarcel Moolenaar 	return (EINTR);
7422c42a146SMarcel Moolenaar }
7432c42a146SMarcel Moolenaar 
74431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7452c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7462c42a146SMarcel Moolenaar struct osigsuspend_args {
7472c42a146SMarcel Moolenaar 	osigset_t mask;
7482c42a146SMarcel Moolenaar };
7492c42a146SMarcel Moolenaar #endif
7502c42a146SMarcel Moolenaar /* ARGSUSED */
7512c42a146SMarcel Moolenaar int
7522c42a146SMarcel Moolenaar osigsuspend(p, uap)
7532c42a146SMarcel Moolenaar 	register struct proc *p;
7542c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
7552c42a146SMarcel Moolenaar {
756645682fdSLuoqi Chen 	sigset_t mask;
7572c42a146SMarcel Moolenaar 	register struct sigacts *ps = p->p_sigacts;
7582c42a146SMarcel Moolenaar 
7592c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
760645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
761645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
762645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
763645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
764645682fdSLuoqi Chen 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
765df8bae1dSRodney W. Grimes 		/* void */;
766df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
767df8bae1dSRodney W. Grimes 	return (EINTR);
768df8bae1dSRodney W. Grimes }
76931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
770df8bae1dSRodney W. Grimes 
771df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
772d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
773df8bae1dSRodney W. Grimes struct osigstack_args {
774df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
775df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
776df8bae1dSRodney W. Grimes };
777d2d3e875SBruce Evans #endif
778df8bae1dSRodney W. Grimes /* ARGSUSED */
77926f9a767SRodney W. Grimes int
780cb226aaaSPoul-Henning Kamp osigstack(p, uap)
781df8bae1dSRodney W. Grimes 	struct proc *p;
782df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
783df8bae1dSRodney W. Grimes {
784df8bae1dSRodney W. Grimes 	struct sigstack ss;
785df8bae1dSRodney W. Grimes 	int error = 0;
786df8bae1dSRodney W. Grimes 
787645682fdSLuoqi Chen 	ss.ss_sp = p->p_sigstk.ss_sp;
788645682fdSLuoqi Chen 	ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
7896f841fb7SMarcel Moolenaar 	if (uap->oss && (error = copyout(&ss, uap->oss,
790df8bae1dSRodney W. Grimes 	    sizeof(struct sigstack))))
791df8bae1dSRodney W. Grimes 		return (error);
7926f841fb7SMarcel Moolenaar 	if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
793645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
794645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
795645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
796645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
797df8bae1dSRodney W. Grimes 	}
798df8bae1dSRodney W. Grimes 	return (error);
799df8bae1dSRodney W. Grimes }
800df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
801df8bae1dSRodney W. Grimes 
802d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
803df8bae1dSRodney W. Grimes struct sigaltstack_args {
8042c42a146SMarcel Moolenaar 	stack_t	*ss;
8052c42a146SMarcel Moolenaar 	stack_t	*oss;
806df8bae1dSRodney W. Grimes };
807d2d3e875SBruce Evans #endif
808df8bae1dSRodney W. Grimes /* ARGSUSED */
80926f9a767SRodney W. Grimes int
810cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
811df8bae1dSRodney W. Grimes 	struct proc *p;
812df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
813df8bae1dSRodney W. Grimes {
8142c42a146SMarcel Moolenaar 	stack_t ss;
815df8bae1dSRodney W. Grimes 	int error;
816df8bae1dSRodney W. Grimes 
817645682fdSLuoqi Chen 	if ((p->p_flag & P_ALTSTACK) == 0)
818645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= SS_DISABLE;
8196f841fb7SMarcel Moolenaar 	if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
8206f841fb7SMarcel Moolenaar 	    sizeof(stack_t))))
821df8bae1dSRodney W. Grimes 		return (error);
8222c42a146SMarcel Moolenaar 	if (uap->ss == 0)
823df8bae1dSRodney W. Grimes 		return (0);
8246f841fb7SMarcel Moolenaar 	if ((error = copyin(uap->ss, &ss, sizeof(ss))))
825df8bae1dSRodney W. Grimes 		return (error);
826d66a5066SPeter Wemm 	if (ss.ss_flags & SS_DISABLE) {
827645682fdSLuoqi Chen 		if (p->p_sigstk.ss_flags & SS_ONSTACK)
828df8bae1dSRodney W. Grimes 			return (EINVAL);
829645682fdSLuoqi Chen 		p->p_flag &= ~P_ALTSTACK;
830645682fdSLuoqi Chen 		p->p_sigstk.ss_flags = ss.ss_flags;
831df8bae1dSRodney W. Grimes 		return (0);
832df8bae1dSRodney W. Grimes 	}
833806d7daaSMarcel Moolenaar 	if (ss.ss_size < p->p_sysent->sv_minsigstksz)
834df8bae1dSRodney W. Grimes 		return (ENOMEM);
835645682fdSLuoqi Chen 	p->p_flag |= P_ALTSTACK;
836645682fdSLuoqi Chen 	p->p_sigstk = ss;
837df8bae1dSRodney W. Grimes 	return (0);
838df8bae1dSRodney W. Grimes }
839df8bae1dSRodney W. Grimes 
840d93f860cSPoul-Henning Kamp /*
841d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
842d93f860cSPoul-Henning Kamp  * cp is calling process.
843d93f860cSPoul-Henning Kamp  */
844d93f860cSPoul-Henning Kamp int
8452c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
846d93f860cSPoul-Henning Kamp 	register struct proc *cp;
8472c42a146SMarcel Moolenaar 	int sig, pgid, all;
848d93f860cSPoul-Henning Kamp {
849d93f860cSPoul-Henning Kamp 	register struct proc *p;
850d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
851d93f860cSPoul-Henning Kamp 	int nfound = 0;
852d93f860cSPoul-Henning Kamp 
853553629ebSJake Burkholder 	if (all) {
854d93f860cSPoul-Henning Kamp 		/*
855d93f860cSPoul-Henning Kamp 		 * broadcast
856d93f860cSPoul-Henning Kamp 		 */
857553629ebSJake Burkholder 		lockmgr(&allproc_lock, LK_SHARED, NULL, CURPROC);
8582e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
859d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
860a9e0361bSPoul-Henning Kamp 			    p == cp || !CANSIGNAL(cp, p, sig))
861d93f860cSPoul-Henning Kamp 				continue;
862d93f860cSPoul-Henning Kamp 			nfound++;
8632c42a146SMarcel Moolenaar 			if (sig)
8642c42a146SMarcel Moolenaar 				psignal(p, sig);
865d93f860cSPoul-Henning Kamp 		}
866553629ebSJake Burkholder 		lockmgr(&allproc_lock, LK_RELEASE, NULL, CURPROC);
867553629ebSJake Burkholder 	} else {
868d93f860cSPoul-Henning Kamp 		if (pgid == 0)
869d93f860cSPoul-Henning Kamp 			/*
870d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
871d93f860cSPoul-Henning Kamp 			 */
872d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
873d93f860cSPoul-Henning Kamp 		else {
874d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
875d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
876d93f860cSPoul-Henning Kamp 				return (ESRCH);
877d93f860cSPoul-Henning Kamp 		}
8782e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
879d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
880d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
881a9e0361bSPoul-Henning Kamp 			    !CANSIGNAL(cp, p, sig))
882d93f860cSPoul-Henning Kamp 				continue;
883d93f860cSPoul-Henning Kamp 			nfound++;
8842c42a146SMarcel Moolenaar 			if (sig)
8852c42a146SMarcel Moolenaar 				psignal(p, sig);
886d93f860cSPoul-Henning Kamp 		}
887d93f860cSPoul-Henning Kamp 	}
888d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
889d93f860cSPoul-Henning Kamp }
890d93f860cSPoul-Henning Kamp 
891d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
892df8bae1dSRodney W. Grimes struct kill_args {
893df8bae1dSRodney W. Grimes 	int	pid;
894df8bae1dSRodney W. Grimes 	int	signum;
895df8bae1dSRodney W. Grimes };
896d2d3e875SBruce Evans #endif
897df8bae1dSRodney W. Grimes /* ARGSUSED */
89826f9a767SRodney W. Grimes int
899cb226aaaSPoul-Henning Kamp kill(cp, uap)
900df8bae1dSRodney W. Grimes 	register struct proc *cp;
901df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
902df8bae1dSRodney W. Grimes {
903df8bae1dSRodney W. Grimes 	register struct proc *p;
904df8bae1dSRodney W. Grimes 
9052c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
906df8bae1dSRodney W. Grimes 		return (EINVAL);
907df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
908df8bae1dSRodney W. Grimes 		/* kill single process */
909df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
910df8bae1dSRodney W. Grimes 			return (ESRCH);
911a9e0361bSPoul-Henning Kamp 		if (!CANSIGNAL(cp, p, uap->signum))
912df8bae1dSRodney W. Grimes 			return (EPERM);
913df8bae1dSRodney W. Grimes 		if (uap->signum)
914df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
915df8bae1dSRodney W. Grimes 		return (0);
916df8bae1dSRodney W. Grimes 	}
917df8bae1dSRodney W. Grimes 	switch (uap->pid) {
918df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
919df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
920df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
921df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
922df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
923df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
924df8bae1dSRodney W. Grimes 	}
925df8bae1dSRodney W. Grimes 	/* NOTREACHED */
926df8bae1dSRodney W. Grimes }
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
929d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
930df8bae1dSRodney W. Grimes struct okillpg_args {
931df8bae1dSRodney W. Grimes 	int	pgid;
932df8bae1dSRodney W. Grimes 	int	signum;
933df8bae1dSRodney W. Grimes };
934d2d3e875SBruce Evans #endif
935df8bae1dSRodney W. Grimes /* ARGSUSED */
93626f9a767SRodney W. Grimes int
937cb226aaaSPoul-Henning Kamp okillpg(p, uap)
938df8bae1dSRodney W. Grimes 	struct proc *p;
939df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
940df8bae1dSRodney W. Grimes {
941df8bae1dSRodney W. Grimes 
9422c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
943df8bae1dSRodney W. Grimes 		return (EINVAL);
944df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
945df8bae1dSRodney W. Grimes }
946df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes /*
949df8bae1dSRodney W. Grimes  * Send a signal to a process group.
950df8bae1dSRodney W. Grimes  */
951df8bae1dSRodney W. Grimes void
9522c42a146SMarcel Moolenaar gsignal(pgid, sig)
9532c42a146SMarcel Moolenaar 	int pgid, sig;
954df8bae1dSRodney W. Grimes {
955df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
956df8bae1dSRodney W. Grimes 
957df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
9582c42a146SMarcel Moolenaar 		pgsignal(pgrp, sig, 0);
959df8bae1dSRodney W. Grimes }
960df8bae1dSRodney W. Grimes 
961df8bae1dSRodney W. Grimes /*
962df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
963df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
964df8bae1dSRodney W. Grimes  */
965df8bae1dSRodney W. Grimes void
9662c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
967df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
9682c42a146SMarcel Moolenaar 	int sig, checkctty;
969df8bae1dSRodney W. Grimes {
970df8bae1dSRodney W. Grimes 	register struct proc *p;
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	if (pgrp)
9732e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
974df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
9752c42a146SMarcel Moolenaar 				psignal(p, sig);
976df8bae1dSRodney W. Grimes }
977df8bae1dSRodney W. Grimes 
978df8bae1dSRodney W. Grimes /*
979df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
980df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
981df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
982df8bae1dSRodney W. Grimes  */
983df8bae1dSRodney W. Grimes void
9842c42a146SMarcel Moolenaar trapsignal(p, sig, code)
985df8bae1dSRodney W. Grimes 	struct proc *p;
9862c42a146SMarcel Moolenaar 	register int sig;
9878674077aSJeffrey Hsu 	u_long code;
988df8bae1dSRodney W. Grimes {
989df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
990df8bae1dSRodney W. Grimes 
9912c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
9922c42a146SMarcel Moolenaar 	    SIGISMEMBER(p->p_sigmask, sig)) {
993df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
994df8bae1dSRodney W. Grimes #ifdef KTRACE
995df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
9962c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
9972c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
998df8bae1dSRodney W. Grimes #endif
9992c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
10002c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
10012c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
10022c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
10032c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
10042c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1005289ccde0SPeter Wemm 			/*
10062c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1007289ccde0SPeter Wemm 			 */
10082c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
10092c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
10102c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
10112c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
10122c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1013dedc04feSPeter Wemm 		}
10146f841fb7SMarcel Moolenaar 	} else {
10156626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
10162c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
10172c42a146SMarcel Moolenaar 		psignal(p, sig);
1018df8bae1dSRodney W. Grimes 	}
1019df8bae1dSRodney W. Grimes }
1020df8bae1dSRodney W. Grimes 
1021df8bae1dSRodney W. Grimes /*
1022df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1023df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1024df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1025df8bae1dSRodney W. Grimes  *
1026df8bae1dSRodney W. Grimes  * Exceptions:
1027df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1028df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1029df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1030df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1031df8bae1dSRodney W. Grimes  *
1032df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1033df8bae1dSRodney W. Grimes  */
1034df8bae1dSRodney W. Grimes void
10352c42a146SMarcel Moolenaar psignal(p, sig)
1036df8bae1dSRodney W. Grimes 	register struct proc *p;
10372c42a146SMarcel Moolenaar 	register int sig;
1038df8bae1dSRodney W. Grimes {
1039df8bae1dSRodney W. Grimes 	register int s, prop;
1040df8bae1dSRodney W. Grimes 	register sig_t action;
1041df8bae1dSRodney W. Grimes 
10422c42a146SMarcel Moolenaar 	if (sig > _SIG_MAXSIG || sig <= 0) {
10432c42a146SMarcel Moolenaar 		printf("psignal: signal %d\n", sig);
1044df8bae1dSRodney W. Grimes 		panic("psignal signal number");
10452a024a2bSSean Eric Fagan 	}
10462c42a146SMarcel Moolenaar 
1047cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1048cb679c38SJonathan Lemon 
10492c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes 	/*
10522a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
10532a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
10542a024a2bSSean Eric Fagan 	 * a chance, as well.
1055df8bae1dSRodney W. Grimes 	 */
10562a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1057df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1058df8bae1dSRodney W. Grimes 	else {
1059df8bae1dSRodney W. Grimes 		/*
1060df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1061df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1062df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1063df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1064df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1065df8bae1dSRodney W. Grimes 		 */
10662c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1067df8bae1dSRodney W. Grimes 			return;
10682c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1069df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
10702c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1071df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1072df8bae1dSRodney W. Grimes 		else
1073df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1074df8bae1dSRodney W. Grimes 	}
1075df8bae1dSRodney W. Grimes 
1076df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1077df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
1078df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
1079df8bae1dSRodney W. Grimes 
1080df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
10812c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1082df8bae1dSRodney W. Grimes 
1083df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1084df8bae1dSRodney W. Grimes 		/*
1085df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1086df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1087df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1088df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1089df8bae1dSRodney W. Grimes 		 */
1090df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1091df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
1092df8bae1dSRodney W. Grimes 		        return;
10932c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1094df8bae1dSRodney W. Grimes 	}
10952c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1096df8bae1dSRodney W. Grimes 
1097df8bae1dSRodney W. Grimes 	/*
1098df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1099df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1100df8bae1dSRodney W. Grimes 	 */
11012c42a146SMarcel Moolenaar 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1102df8bae1dSRodney W. Grimes 		return;
1103df8bae1dSRodney W. Grimes 	s = splhigh();
1104df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1105df8bae1dSRodney W. Grimes 
1106df8bae1dSRodney W. Grimes 	case SSLEEP:
1107df8bae1dSRodney W. Grimes 		/*
1108df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1109df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1110df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1111df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1112df8bae1dSRodney W. Grimes 		 */
1113df8bae1dSRodney W. Grimes 		if ((p->p_flag & P_SINTR) == 0)
1114df8bae1dSRodney W. Grimes 			goto out;
1115df8bae1dSRodney W. Grimes 		/*
1116df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1117df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1118df8bae1dSRodney W. Grimes 		 * for the parent.
1119df8bae1dSRodney W. Grimes 		 */
1120df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1121df8bae1dSRodney W. Grimes 			goto run;
1122df8bae1dSRodney W. Grimes 		/*
1123df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1124df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1125df8bae1dSRodney W. Grimes 		 * be awakened.
1126df8bae1dSRodney W. Grimes 		 */
1127df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
11282c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1129df8bae1dSRodney W. Grimes 			goto out;
1130df8bae1dSRodney W. Grimes 		}
1131df8bae1dSRodney W. Grimes 		/*
1132df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1133df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1134df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1135df8bae1dSRodney W. Grimes 		 * cause the process to run.
1136df8bae1dSRodney W. Grimes 		 */
1137df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1138df8bae1dSRodney W. Grimes 			if (action != SIG_DFL)
1139df8bae1dSRodney W. Grimes 				goto runfast;
1140df8bae1dSRodney W. Grimes 			/*
1141df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1142df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1143df8bae1dSRodney W. Grimes 			 */
1144df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1145df8bae1dSRodney W. Grimes 				goto out;
11462c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
11472c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1148645682fdSLuoqi Chen 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1149df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1150df8bae1dSRodney W. Grimes 			stop(p);
1151df8bae1dSRodney W. Grimes 			goto out;
1152df8bae1dSRodney W. Grimes 		} else
1153df8bae1dSRodney W. Grimes 			goto runfast;
1154df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
1155df8bae1dSRodney W. Grimes 
1156df8bae1dSRodney W. Grimes 	case SSTOP:
1157df8bae1dSRodney W. Grimes 		/*
1158df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1159df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1160df8bae1dSRodney W. Grimes 		 */
1161df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1162df8bae1dSRodney W. Grimes 			goto out;
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes 		/*
1165df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1166df8bae1dSRodney W. Grimes 		 */
11672c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1168df8bae1dSRodney W. Grimes 			goto runfast;
1169df8bae1dSRodney W. Grimes 
1170df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1171df8bae1dSRodney W. Grimes 			/*
1172df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1173df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1174df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1175df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1176df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1177df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1178df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1179df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1180df8bae1dSRodney W. Grimes 			 */
1181df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
11822c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1183df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1184df8bae1dSRodney W. Grimes 				goto runfast;
1185df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
1186df8bae1dSRodney W. Grimes 				goto run;
1187df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
1188df8bae1dSRodney W. Grimes 			goto out;
1189df8bae1dSRodney W. Grimes 		}
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1192df8bae1dSRodney W. Grimes 			/*
1193df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1194df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1195df8bae1dSRodney W. Grimes 			 */
11962c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1197df8bae1dSRodney W. Grimes 			goto out;
1198df8bae1dSRodney W. Grimes 		}
1199df8bae1dSRodney W. Grimes 
1200df8bae1dSRodney W. Grimes 		/*
1201df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1202df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1203df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1204df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1205df8bae1dSRodney W. Grimes 		 */
1206df8bae1dSRodney W. Grimes 		if (p->p_wchan && p->p_flag & P_SINTR)
1207df8bae1dSRodney W. Grimes 			unsleep(p);
1208df8bae1dSRodney W. Grimes 		goto out;
1209df8bae1dSRodney W. Grimes 
1210df8bae1dSRodney W. Grimes 	default:
1211df8bae1dSRodney W. Grimes 		/*
1212df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1213df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1214df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1215df8bae1dSRodney W. Grimes 		 */
1216df8bae1dSRodney W. Grimes 		if (p == curproc)
1217df8bae1dSRodney W. Grimes 			signotify(p);
12183163861cSTor Egge #ifdef SMP
12193163861cSTor Egge 		else if (p->p_stat == SRUN)
12203163861cSTor Egge 			forward_signal(p);
12213163861cSTor Egge #endif
1222df8bae1dSRodney W. Grimes 		goto out;
1223df8bae1dSRodney W. Grimes 	}
1224df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes runfast:
1227df8bae1dSRodney W. Grimes 	/*
1228df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1229df8bae1dSRodney W. Grimes 	 */
1230df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
1231df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
1232df8bae1dSRodney W. Grimes run:
1233df8bae1dSRodney W. Grimes 	setrunnable(p);
1234df8bae1dSRodney W. Grimes out:
1235df8bae1dSRodney W. Grimes 	splx(s);
1236df8bae1dSRodney W. Grimes }
1237df8bae1dSRodney W. Grimes 
1238df8bae1dSRodney W. Grimes /*
1239df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1240df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1241df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1242df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1243df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1244df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1245df8bae1dSRodney W. Grimes  * sequence is
1246df8bae1dSRodney W. Grimes  *
12472c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
12482c42a146SMarcel Moolenaar  *		postsig(sig);
1249df8bae1dSRodney W. Grimes  */
125026f9a767SRodney W. Grimes int
1251df8bae1dSRodney W. Grimes issignal(p)
1252df8bae1dSRodney W. Grimes 	register struct proc *p;
1253df8bae1dSRodney W. Grimes {
12542c42a146SMarcel Moolenaar 	sigset_t mask;
12552c42a146SMarcel Moolenaar 	register int sig, prop;
1256df8bae1dSRodney W. Grimes 
1257df8bae1dSRodney W. Grimes 	for (;;) {
12582a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
12592a024a2bSSean Eric Fagan 
12602c42a146SMarcel Moolenaar 		mask = p->p_siglist;
12612c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1262df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
12632c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
12642c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))	 	/* no signal to send */
1265df8bae1dSRodney W. Grimes 			return (0);
12662c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
12672c42a146SMarcel Moolenaar 		prop = sigprop(sig);
12682a024a2bSSean Eric Fagan 
12692c42a146SMarcel Moolenaar 		STOPEVENT(p, S_SIG, sig);
12702a024a2bSSean Eric Fagan 
1271df8bae1dSRodney W. Grimes 		/*
1272df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1273df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1274df8bae1dSRodney W. Grimes 		 */
12752c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
12762c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1277df8bae1dSRodney W. Grimes 			continue;
1278df8bae1dSRodney W. Grimes 		}
1279df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1280df8bae1dSRodney W. Grimes 			/*
1281df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1282df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1283df8bae1dSRodney W. Grimes 			 */
12842c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1285df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1286df8bae1dSRodney W. Grimes 			do {
1287df8bae1dSRodney W. Grimes 				stop(p);
128820cdcc5bSJohn Baldwin 				mtx_enter(&sched_lock, MTX_SPIN);
12897da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
1290df8bae1dSRodney W. Grimes 				mi_switch();
129120cdcc5bSJohn Baldwin 				mtx_exit(&sched_lock, MTX_SPIN);
129220cdcc5bSJohn Baldwin 				PICKUP_GIANT();
12932a024a2bSSean Eric Fagan 			} while (!trace_req(p)
12942a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1295df8bae1dSRodney W. Grimes 
1296df8bae1dSRodney W. Grimes 			/*
1297df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1298df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1299df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1300df8bae1dSRodney W. Grimes 			 */
1301df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1302df8bae1dSRodney W. Grimes 				continue;
1303df8bae1dSRodney W. Grimes 
1304df8bae1dSRodney W. Grimes 			/*
1305df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1306df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1307df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1308df8bae1dSRodney W. Grimes 			 */
13092c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
13102c42a146SMarcel Moolenaar 			sig = p->p_xstat;
13112c42a146SMarcel Moolenaar 			if (sig == 0)
1312df8bae1dSRodney W. Grimes 				continue;
1313df8bae1dSRodney W. Grimes 
1314df8bae1dSRodney W. Grimes 			/*
1315df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1316df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1317df8bae1dSRodney W. Grimes 			 */
13182c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
13192c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1320df8bae1dSRodney W. Grimes 				continue;
1321df8bae1dSRodney W. Grimes 		}
1322df8bae1dSRodney W. Grimes 
1323df8bae1dSRodney W. Grimes 		/*
1324df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1325df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1326df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1327df8bae1dSRodney W. Grimes 		 */
13282c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1329df8bae1dSRodney W. Grimes 
13300b53fbe8SBruce Evans 		case (int)SIG_DFL:
1331df8bae1dSRodney W. Grimes 			/*
1332df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1333df8bae1dSRodney W. Grimes 			 */
1334df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1335df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1336df8bae1dSRodney W. Grimes 				/*
1337df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1338df8bae1dSRodney W. Grimes 				 * in init? XXX
1339df8bae1dSRodney W. Grimes 				 */
1340d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
13412c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1342df8bae1dSRodney W. Grimes #endif
1343df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1344df8bae1dSRodney W. Grimes 			}
1345df8bae1dSRodney W. Grimes 			/*
1346df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1347df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1348df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1349df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1350df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1351df8bae1dSRodney W. Grimes 			 */
1352df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1353df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1354df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1355df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1356df8bae1dSRodney W. Grimes 					break;	/* == ignore */
13572c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1358df8bae1dSRodney W. Grimes 				stop(p);
1359645682fdSLuoqi Chen 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1360df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
136120cdcc5bSJohn Baldwin 				mtx_enter(&sched_lock, MTX_SPIN);
13627da6f977SJake Burkholder 				DROP_GIANT_NOSWITCH();
1363df8bae1dSRodney W. Grimes 				mi_switch();
136420cdcc5bSJohn Baldwin 				mtx_exit(&sched_lock, MTX_SPIN);
136520cdcc5bSJohn Baldwin 				PICKUP_GIANT();
1366df8bae1dSRodney W. Grimes 				break;
1367df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1368df8bae1dSRodney W. Grimes 				/*
1369df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1370df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1371df8bae1dSRodney W. Grimes 				 */
1372df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1373df8bae1dSRodney W. Grimes 			} else
13742c42a146SMarcel Moolenaar 				return (sig);
1375df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1376df8bae1dSRodney W. Grimes 
13770b53fbe8SBruce Evans 		case (int)SIG_IGN:
1378df8bae1dSRodney W. Grimes 			/*
1379df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1380df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1381df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1382df8bae1dSRodney W. Grimes 			 */
1383df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1384df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1385df8bae1dSRodney W. Grimes 				printf("issignal\n");
1386df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1387df8bae1dSRodney W. Grimes 
1388df8bae1dSRodney W. Grimes 		default:
1389df8bae1dSRodney W. Grimes 			/*
1390df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1391df8bae1dSRodney W. Grimes 			 * postsig() process it.
1392df8bae1dSRodney W. Grimes 			 */
13932c42a146SMarcel Moolenaar 			return (sig);
1394df8bae1dSRodney W. Grimes 		}
13952c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1396df8bae1dSRodney W. Grimes 	}
1397df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1398df8bae1dSRodney W. Grimes }
1399df8bae1dSRodney W. Grimes 
1400df8bae1dSRodney W. Grimes /*
1401df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1402df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
1403df8bae1dSRodney W. Grimes  * on the run queue.
1404df8bae1dSRodney W. Grimes  */
140526f9a767SRodney W. Grimes void
1406df8bae1dSRodney W. Grimes stop(p)
1407df8bae1dSRodney W. Grimes 	register struct proc *p;
1408df8bae1dSRodney W. Grimes {
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1411df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1412df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1413df8bae1dSRodney W. Grimes }
1414df8bae1dSRodney W. Grimes 
1415df8bae1dSRodney W. Grimes /*
1416df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1417df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1418df8bae1dSRodney W. Grimes  */
1419df8bae1dSRodney W. Grimes void
14202c42a146SMarcel Moolenaar postsig(sig)
14212c42a146SMarcel Moolenaar 	register int sig;
1422df8bae1dSRodney W. Grimes {
1423df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
14242c42a146SMarcel Moolenaar 	struct sigacts *ps = p->p_sigacts;
14252c42a146SMarcel Moolenaar 	sig_t action;
14262c42a146SMarcel Moolenaar 	sigset_t returnmask;
14272c42a146SMarcel Moolenaar 	int code;
1428df8bae1dSRodney W. Grimes 
14292c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
14305526d2d9SEivind Eklund 
14312c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
14322c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1433df8bae1dSRodney W. Grimes #ifdef KTRACE
1434df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1435645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
14362c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1437df8bae1dSRodney W. Grimes #endif
14382c42a146SMarcel Moolenaar 	STOPEVENT(p, S_SIG, sig);
14392a024a2bSSean Eric Fagan 
1440df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1441df8bae1dSRodney W. Grimes 		/*
1442df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1443df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1444df8bae1dSRodney W. Grimes 		 */
14452c42a146SMarcel Moolenaar 		sigexit(p, sig);
1446df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1447df8bae1dSRodney W. Grimes 	} else {
1448df8bae1dSRodney W. Grimes 		/*
1449df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1450df8bae1dSRodney W. Grimes 		 */
14512c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
14525526d2d9SEivind Eklund 		    ("postsig action"));
1453df8bae1dSRodney W. Grimes 		/*
1454df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1455645682fdSLuoqi Chen 		 * occurrences of this signal.
1456df8bae1dSRodney W. Grimes 		 *
1457645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1458df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1459645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1460df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1461df8bae1dSRodney W. Grimes 		 */
1462df8bae1dSRodney W. Grimes 		(void) splhigh();
1463645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
14646626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1465645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1466df8bae1dSRodney W. Grimes 		} else
1467df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
14682c42a146SMarcel Moolenaar 
14692c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
14702c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
14712c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
14722c42a146SMarcel Moolenaar 
14732c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1474289ccde0SPeter Wemm 			/*
14752c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1476289ccde0SPeter Wemm 			 */
14772c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
14782c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
14792c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
14802c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
14812c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1482dedc04feSPeter Wemm 		}
1483df8bae1dSRodney W. Grimes 		(void) spl0();
1484df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
14852c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1486df8bae1dSRodney W. Grimes 			code = 0;
1487df8bae1dSRodney W. Grimes 		} else {
14886626c604SJulian Elischer 			code = p->p_code;
14896626c604SJulian Elischer 			p->p_code = 0;
14906626c604SJulian Elischer 			p->p_sig = 0;
1491df8bae1dSRodney W. Grimes 		}
14922c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1493df8bae1dSRodney W. Grimes 	}
1494df8bae1dSRodney W. Grimes }
1495df8bae1dSRodney W. Grimes 
1496df8bae1dSRodney W. Grimes /*
1497df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1498df8bae1dSRodney W. Grimes  */
149926f9a767SRodney W. Grimes void
1500df8bae1dSRodney W. Grimes killproc(p, why)
1501df8bae1dSRodney W. Grimes 	struct proc *p;
1502df8bae1dSRodney W. Grimes 	char *why;
1503df8bae1dSRodney W. Grimes {
15040384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
15050384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1506729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1507729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1508df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1509df8bae1dSRodney W. Grimes }
1510df8bae1dSRodney W. Grimes 
1511df8bae1dSRodney W. Grimes /*
1512df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1513df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1514df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1515df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1516df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1517df8bae1dSRodney W. Grimes  * does not return.
1518df8bae1dSRodney W. Grimes  */
151926f9a767SRodney W. Grimes void
15202c42a146SMarcel Moolenaar sigexit(p, sig)
1521df8bae1dSRodney W. Grimes 	register struct proc *p;
15222c42a146SMarcel Moolenaar 	int sig;
1523df8bae1dSRodney W. Grimes {
1524df8bae1dSRodney W. Grimes 
1525df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
15262c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
15272c42a146SMarcel Moolenaar 		p->p_sig = sig;
1528c364e17eSAndrey A. Chernov 		/*
1529c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1530c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1531c364e17eSAndrey A. Chernov 		 * these messages.)
1532c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1533c364e17eSAndrey A. Chernov 		 */
1534fca666a1SJulian Elischer 		if (coredump(p) == 0)
15352c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
153657308494SJoerg Wunsch 		if (kern_logsigexit)
153757308494SJoerg Wunsch 			log(LOG_INFO,
153857308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
15393d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
15403d1b21c6SAndrey A. Chernov 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
15412c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
15422c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1543df8bae1dSRodney W. Grimes 	}
15442c42a146SMarcel Moolenaar 	exit1(p, W_EXITCODE(0, sig));
1545df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1546df8bae1dSRodney W. Grimes }
1547df8bae1dSRodney W. Grimes 
1548c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1549c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1550c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1551c5edb423SSean Eric Fagan 
1552c5edb423SSean Eric Fagan /*
1553c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1554c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1555c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1556c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1557c5edb423SSean Eric Fagan  *	%P	process id (pid)
1558c5edb423SSean Eric Fagan  *	%U	user id (uid)
1559c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1560c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1561c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1562c5edb423SSean Eric Fagan  */
1563c5edb423SSean Eric Fagan 
1564fca666a1SJulian Elischer static char *
1565c5edb423SSean Eric Fagan expand_name(name, uid, pid)
156687f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1567c5edb423SSean Eric Fagan 	char *temp;
1568c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1569c5edb423SSean Eric Fagan 	int i, n;
1570c5edb423SSean Eric Fagan 	char *format = corefilename;
1571ce38ca0fSAlfred Perlstein 	size_t namelen;
1572c5edb423SSean Eric Fagan 
1573ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
15740bfe2990SEivind Eklund 	if (temp == NULL)
15750bfe2990SEivind Eklund 		return NULL;
1576ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1577ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1578c5edb423SSean Eric Fagan 		int l;
1579c5edb423SSean Eric Fagan 		switch (format[i]) {
1580c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1581c5edb423SSean Eric Fagan 			i++;
1582c5edb423SSean Eric Fagan 			switch (format[i]) {
1583c5edb423SSean Eric Fagan 			case '%':
1584c5edb423SSean Eric Fagan 				temp[n++] = '%';
1585c5edb423SSean Eric Fagan 				break;
1586c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1587ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
158887f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1589c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1590c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1591c5edb423SSean Eric Fagan 					return NULL;
1592c5edb423SSean Eric Fagan 				}
1593ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1594ce38ca0fSAlfred Perlstein 				n += namelen;
1595c5edb423SSean Eric Fagan 				break;
1596c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1597ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1598c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
159987f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1600c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1601c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1602c5edb423SSean Eric Fagan 					return NULL;
1603c5edb423SSean Eric Fagan 				}
1604c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1605c5edb423SSean Eric Fagan 				n += l;
1606c5edb423SSean Eric Fagan 				break;
1607c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1608ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1609c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
161087f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1611c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1612c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1613c5edb423SSean Eric Fagan 					return NULL;
1614c5edb423SSean Eric Fagan 				}
1615c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1616c5edb423SSean Eric Fagan 				n += l;
1617c5edb423SSean Eric Fagan 				break;
1618c5edb423SSean Eric Fagan 			default:
1619c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1620c5edb423SSean Eric Fagan 			}
1621c5edb423SSean Eric Fagan 			break;
1622c5edb423SSean Eric Fagan 		default:
1623c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1624c5edb423SSean Eric Fagan 		}
1625c5edb423SSean Eric Fagan 	}
1626ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1627c5edb423SSean Eric Fagan 	return temp;
1628c5edb423SSean Eric Fagan }
1629c5edb423SSean Eric Fagan 
1630df8bae1dSRodney W. Grimes /*
1631fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1632fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1633fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1634fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1635fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1636fca666a1SJulian Elischer  */
1637fca666a1SJulian Elischer 
1638fca666a1SJulian Elischer static int
1639fca666a1SJulian Elischer coredump(p)
1640fca666a1SJulian Elischer 	register struct proc *p;
1641fca666a1SJulian Elischer {
1642fca666a1SJulian Elischer 	register struct vnode *vp;
1643da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
1644fca666a1SJulian Elischer 	struct nameidata nd;
1645fca666a1SJulian Elischer 	struct vattr vattr;
1646e6796b67SKirk McKusick 	int error, error1, flags;
1647f2a2857bSKirk McKusick 	struct mount *mp;
1648fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1649fca666a1SJulian Elischer 	off_t limit;
1650fca666a1SJulian Elischer 
1651fca666a1SJulian Elischer 	STOPEVENT(p, S_CORE, 0);
1652fca666a1SJulian Elischer 
1653e5a28db9SPaul Saab 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1654fca666a1SJulian Elischer 		return (EFAULT);
1655fca666a1SJulian Elischer 
1656fca666a1SJulian Elischer 	/*
165735a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
165835a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
165935a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
166035a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
166135a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
166235a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1663fca666a1SJulian Elischer 	 */
166435a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
166535a2598fSSean Eric Fagan 	if (limit == 0)
166635a2598fSSean Eric Fagan 		return 0;
166735a2598fSSean Eric Fagan 
1668f2a2857bSKirk McKusick restart:
1669fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1670fca666a1SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1671e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1672e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1673fca666a1SJulian Elischer 	free(name, M_TEMP);
1674fca666a1SJulian Elischer 	if (error)
1675fca666a1SJulian Elischer 		return (error);
1676762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1677fca666a1SJulian Elischer 	vp = nd.ni_vp;
1678f2a2857bSKirk McKusick 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1679f2a2857bSKirk McKusick 		VOP_UNLOCK(vp, 0, p);
1680f2a2857bSKirk McKusick 		if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
1681f2a2857bSKirk McKusick 			return (error);
1682f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1683f2a2857bSKirk McKusick 			return (error);
1684f2a2857bSKirk McKusick 		goto restart;
1685f2a2857bSKirk McKusick 	}
1686fca666a1SJulian Elischer 
1687fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
1688fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
1689fca666a1SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1690fca666a1SJulian Elischer 		error = EFAULT;
1691fca666a1SJulian Elischer 		goto out;
1692fca666a1SJulian Elischer 	}
1693fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
1694fca666a1SJulian Elischer 	vattr.va_size = 0;
1695fca666a1SJulian Elischer 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1696fca666a1SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, p);
1697fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
1698fca666a1SJulian Elischer 
1699fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
1700fca666a1SJulian Elischer 	  p->p_sysent->sv_coredump(p, vp, limit) :
1701fca666a1SJulian Elischer 	  ENOSYS;
1702fca666a1SJulian Elischer 
1703fca666a1SJulian Elischer out:
1704fca666a1SJulian Elischer 	VOP_UNLOCK(vp, 0, p);
1705f2a2857bSKirk McKusick 	vn_finished_write(mp);
1706fca666a1SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, p);
1707fca666a1SJulian Elischer 	if (error == 0)
1708fca666a1SJulian Elischer 		error = error1;
1709fca666a1SJulian Elischer 	return (error);
1710fca666a1SJulian Elischer }
1711fca666a1SJulian Elischer 
1712fca666a1SJulian Elischer /*
1713df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1714df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1715df8bae1dSRodney W. Grimes  */
1716d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1717df8bae1dSRodney W. Grimes struct nosys_args {
1718df8bae1dSRodney W. Grimes 	int	dummy;
1719df8bae1dSRodney W. Grimes };
1720d2d3e875SBruce Evans #endif
1721df8bae1dSRodney W. Grimes /* ARGSUSED */
172226f9a767SRodney W. Grimes int
1723cb226aaaSPoul-Henning Kamp nosys(p, args)
1724df8bae1dSRodney W. Grimes 	struct proc *p;
1725df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1726df8bae1dSRodney W. Grimes {
1727df8bae1dSRodney W. Grimes 
1728df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1729df8bae1dSRodney W. Grimes 	return (EINVAL);
1730df8bae1dSRodney W. Grimes }
1731831d27a9SDon Lewis 
1732831d27a9SDon Lewis /*
1733831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1734831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1735831d27a9SDon Lewis  */
1736831d27a9SDon Lewis void
17372c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
1738831d27a9SDon Lewis 	struct sigio *sigio;
17392c42a146SMarcel Moolenaar 	int sig, checkctty;
1740831d27a9SDon Lewis {
1741831d27a9SDon Lewis 	if (sigio == NULL)
1742831d27a9SDon Lewis 		return;
1743831d27a9SDon Lewis 
1744831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1745831d27a9SDon Lewis 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1746831d27a9SDon Lewis 		             sigio->sio_proc))
17472c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
1748831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1749831d27a9SDon Lewis 		struct proc *p;
1750831d27a9SDon Lewis 
17512e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1752831d27a9SDon Lewis 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1753831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
17542c42a146SMarcel Moolenaar 				psignal(p, sig);
1755831d27a9SDon Lewis 	}
1756831d27a9SDon Lewis }
1757cb679c38SJonathan Lemon 
1758cb679c38SJonathan Lemon static int
1759cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
1760cb679c38SJonathan Lemon {
1761cb679c38SJonathan Lemon 	struct proc *p = curproc;
1762cb679c38SJonathan Lemon 
1763cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
1764cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1765cb679c38SJonathan Lemon 
1766cb679c38SJonathan Lemon 	/* XXX lock the proc here while adding to the list? */
1767cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1768cb679c38SJonathan Lemon 
1769cb679c38SJonathan Lemon 	return (0);
1770cb679c38SJonathan Lemon }
1771cb679c38SJonathan Lemon 
1772cb679c38SJonathan Lemon static void
1773cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
1774cb679c38SJonathan Lemon {
1775cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
1776cb679c38SJonathan Lemon 
1777e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1778cb679c38SJonathan Lemon }
1779cb679c38SJonathan Lemon 
1780cb679c38SJonathan Lemon /*
1781cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
1782cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
1783cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
1784cb679c38SJonathan Lemon  * isn't worth the trouble.
1785cb679c38SJonathan Lemon  */
1786cb679c38SJonathan Lemon static int
1787cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
1788cb679c38SJonathan Lemon {
1789cb679c38SJonathan Lemon 
1790cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
1791cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
1792cb679c38SJonathan Lemon 
1793cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
1794cb679c38SJonathan Lemon 			kn->kn_data++;
1795cb679c38SJonathan Lemon 	}
1796cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
1797cb679c38SJonathan Lemon }
1798