xref: /freebsd/sys/kern/kern_sig.c (revision cb679c385ef325d11f162e09b604df7532fc8009)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46c87e2930SDavid Greenman #include <sys/kernel.h>
47d2d3e875SBruce Evans #include <sys/sysproto.h>
48df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
50df8bae1dSRodney W. Grimes #include <sys/namei.h>
51df8bae1dSRodney W. Grimes #include <sys/vnode.h>
52cb679c38SJonathan Lemon #include <sys/event.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
542a024a2bSSean Eric Fagan #include <sys/pioctl.h>
55df8bae1dSRodney W. Grimes #include <sys/systm.h>
56df8bae1dSRodney W. Grimes #include <sys/acct.h>
573ac4d1efSBruce Evans #include <sys/fcntl.h>
58df8bae1dSRodney W. Grimes #include <sys/wait.h>
59df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
60df8bae1dSRodney W. Grimes #include <sys/syslog.h>
61df8bae1dSRodney W. Grimes #include <sys/stat.h>
62d66a5066SPeter Wemm #include <sys/sysent.h>
63c87e2930SDavid Greenman #include <sys/sysctl.h>
64c5edb423SSean Eric Fagan #include <sys/malloc.h>
65df8bae1dSRodney W. Grimes 
66762e6b85SEivind Eklund #include <vm/vm_zone.h>
67762e6b85SEivind Eklund 
6836e9f877SMatthew Dillon #include <machine/ipl.h>
69df8bae1dSRodney W. Grimes #include <machine/cpu.h>
703163861cSTor Egge #include <machine/smp.h>
71df8bae1dSRodney W. Grimes 
726f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
736f841fb7SMarcel Moolenaar 
746f841fb7SMarcel Moolenaar static int coredump	__P((struct proc *));
752c42a146SMarcel Moolenaar static int do_sigaction	__P((struct proc *p, int sig, struct sigaction *act,
76645682fdSLuoqi Chen 			     struct sigaction *oact, int old));
772c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
78645682fdSLuoqi Chen 			       sigset_t *oset, int old));
79f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t));
806f841fb7SMarcel Moolenaar static int killpg1	__P((struct proc *cp, int sig, int pgid, int all));
816f841fb7SMarcel Moolenaar static int sig_ffs	__P((sigset_t *set));
826f841fb7SMarcel Moolenaar static int sigprop	__P((int sig));
836f841fb7SMarcel Moolenaar static void stop	__P((struct proc *));
8426f9a767SRodney W. Grimes 
85cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
86cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
87cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
88cb679c38SJonathan Lemon 
89cb679c38SJonathan Lemon struct filterops sig_filtops =
90cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
91cb679c38SJonathan Lemon 
9257308494SJoerg Wunsch static int	kern_logsigexit = 1;
933d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
943d177f46SBill Fumerola     &kern_logsigexit, 0,
953d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
9657308494SJoerg Wunsch 
97df8bae1dSRodney W. Grimes /*
982c42a146SMarcel Moolenaar  * Can process p, with pcred pc, send the signal sig to process q?
99df8bae1dSRodney W. Grimes  */
100a9e0361bSPoul-Henning Kamp #define CANSIGNAL(p, q, sig) \
101a9e0361bSPoul-Henning Kamp 	(!p_trespass(p, q) || \
102a9e0361bSPoul-Henning Kamp 	((sig) == SIGCONT && (q)->p_session == (p)->p_session))
103df8bae1dSRodney W. Grimes 
104831d27a9SDon Lewis /*
105831d27a9SDon Lewis  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
106831d27a9SDon Lewis  */
107831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \
108831d27a9SDon Lewis 	((uc)->cr_uid == 0 || \
109831d27a9SDon Lewis 	    (ruid) == (q)->p_cred->p_ruid || \
110831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_cred->p_ruid || \
111831d27a9SDon Lewis 	    (ruid) == (q)->p_ucred->cr_uid || \
112831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
113831d27a9SDon Lewis 
11422d4b0fbSJohn Polstra int sugid_coredump;
1153d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1163d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
117c87e2930SDavid Greenman 
118e5a28db9SPaul Saab static int	do_coredump = 1;
119e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
120e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
121e5a28db9SPaul Saab 
1222c42a146SMarcel Moolenaar /*
1232c42a146SMarcel Moolenaar  * Signal properties and actions.
1242c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1252c42a146SMarcel Moolenaar  * according to the following properties:
1262c42a146SMarcel Moolenaar  */
1272c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1282c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1292c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1302c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1312c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1322c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1332c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
134df8bae1dSRodney W. Grimes 
1352c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1362c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1372c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1382c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1392c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1412c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1442c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1462c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1482c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1492c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1502c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1512c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1522c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1532c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1542c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1552c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1562c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1572c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1582c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1592c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1602c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1612c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1622c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1632c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1642c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1652c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1662c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1672c42a146SMarcel Moolenaar };
1682c42a146SMarcel Moolenaar 
1696f841fb7SMarcel Moolenaar static __inline int
1706f841fb7SMarcel Moolenaar sigprop(int sig)
1712c42a146SMarcel Moolenaar {
1726f841fb7SMarcel Moolenaar 
1732c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
1742c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
1752c42a146SMarcel Moolenaar 	return (0);
176df8bae1dSRodney W. Grimes }
1772c42a146SMarcel Moolenaar 
1786f841fb7SMarcel Moolenaar static __inline int
1796f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
1802c42a146SMarcel Moolenaar {
1812c42a146SMarcel Moolenaar 	int i;
1822c42a146SMarcel Moolenaar 
1836f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
1842c42a146SMarcel Moolenaar 		if (set->__bits[i])
1852c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
186df8bae1dSRodney W. Grimes 	return (0);
187df8bae1dSRodney W. Grimes }
188df8bae1dSRodney W. Grimes 
1892c42a146SMarcel Moolenaar /*
1902c42a146SMarcel Moolenaar  * do_sigaction
1912c42a146SMarcel Moolenaar  * sigaction
1922c42a146SMarcel Moolenaar  * osigaction
1932c42a146SMarcel Moolenaar  */
1942c42a146SMarcel Moolenaar static int
195645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
1962c42a146SMarcel Moolenaar 	struct proc *p;
1972c42a146SMarcel Moolenaar 	register int sig;
1982c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
199645682fdSLuoqi Chen 	int old;
200df8bae1dSRodney W. Grimes {
201df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
202df8bae1dSRodney W. Grimes 
2032c42a146SMarcel Moolenaar 	if (sig <= 0 || sig > _SIG_MAXSIG)
2042c42a146SMarcel Moolenaar 		return (EINVAL);
2052c42a146SMarcel Moolenaar 
2062c42a146SMarcel Moolenaar 	if (oact) {
2072c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2082c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2092c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2102c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2112c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2122c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2132c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2142c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2152c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2162c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2172c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2182c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2192c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
220645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2212c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
222645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2232c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2242c42a146SMarcel Moolenaar 	}
2252c42a146SMarcel Moolenaar 	if (act) {
2262c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
2272c42a146SMarcel Moolenaar 		    act->sa_handler != SIG_DFL)
2282c42a146SMarcel Moolenaar 			return (EINVAL);
2292c42a146SMarcel Moolenaar 
230df8bae1dSRodney W. Grimes 		/*
231df8bae1dSRodney W. Grimes 		 * Change setting atomically.
232df8bae1dSRodney W. Grimes 		 */
233df8bae1dSRodney W. Grimes 		(void) splhigh();
2342c42a146SMarcel Moolenaar 
2352c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2362c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2372c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
2382c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2392c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_siginfo, sig);
2406f841fb7SMarcel Moolenaar 		} else {
2412c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] =
2422c42a146SMarcel Moolenaar 			    (__sighandler_t *)act->sa_sigaction;
2432c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2442c42a146SMarcel Moolenaar 		}
2452c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2462c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
247df8bae1dSRodney W. Grimes 		else
2482c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2492c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2502c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
251df8bae1dSRodney W. Grimes 		else
2522c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2532c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2542c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2551e41c1b5SSteven Wallace 		else
2562c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2572c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2582c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
259289ccde0SPeter Wemm 		else
2602c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
261df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2622c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2632c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
264df8bae1dSRodney W. Grimes 		else
2652c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_usertramp, seg);
266df8bae1dSRodney W. Grimes #endif
2672c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2682c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
269645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
2706626c604SJulian Elischer 			else
271645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
2722c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDWAIT) {
273245f17d4SJoerg Wunsch 				/*
2742c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
2752c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
2762c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
2772c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
278245f17d4SJoerg Wunsch 				 */
279245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
280645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
2816626c604SJulian Elischer 				else
282645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
283245f17d4SJoerg Wunsch 			} else
284645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
285df8bae1dSRodney W. Grimes 		}
286df8bae1dSRodney W. Grimes 		/*
287df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
2882c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
2892c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
2902c42a146SMarcel Moolenaar 		 * have to restart the process.
291df8bae1dSRodney W. Grimes 		 */
2922c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
2932c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
2942c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
2952c42a146SMarcel Moolenaar 			/* never to be seen again */
2962c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
2972c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
2982c42a146SMarcel Moolenaar 				/* easier in psignal */
2992c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3002c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
301645682fdSLuoqi Chen 		} else {
3022c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3032c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3042c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3052c42a146SMarcel Moolenaar 			else
3062c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3072c42a146SMarcel Moolenaar 		}
308645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
309645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
310645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
311645682fdSLuoqi Chen 		else
312645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
3132c42a146SMarcel Moolenaar 
314df8bae1dSRodney W. Grimes 		(void) spl0();
315df8bae1dSRodney W. Grimes 	}
3162c42a146SMarcel Moolenaar 	return (0);
3172c42a146SMarcel Moolenaar }
3182c42a146SMarcel Moolenaar 
3192c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3202c42a146SMarcel Moolenaar struct sigaction_args {
3212c42a146SMarcel Moolenaar 	int	sig;
3222c42a146SMarcel Moolenaar 	struct	sigaction *act;
3232c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3242c42a146SMarcel Moolenaar };
3252c42a146SMarcel Moolenaar #endif
3262c42a146SMarcel Moolenaar /* ARGSUSED */
3272c42a146SMarcel Moolenaar int
3282c42a146SMarcel Moolenaar sigaction(p, uap)
3292c42a146SMarcel Moolenaar 	struct proc *p;
3302c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3312c42a146SMarcel Moolenaar {
3322c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3332c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3342c42a146SMarcel Moolenaar 	int error;
3352c42a146SMarcel Moolenaar 
3366f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3376f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3382c42a146SMarcel Moolenaar 	if (actp) {
3396f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3402c42a146SMarcel Moolenaar 		if (error)
3412c42a146SMarcel Moolenaar 			return (error);
3422c42a146SMarcel Moolenaar 	}
343645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3442c42a146SMarcel Moolenaar 	if (oactp && !error) {
3456f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3462c42a146SMarcel Moolenaar 	}
3472c42a146SMarcel Moolenaar 	return (error);
3482c42a146SMarcel Moolenaar }
3492c42a146SMarcel Moolenaar 
3502c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3512c42a146SMarcel Moolenaar struct osigaction_args {
3522c42a146SMarcel Moolenaar 	int	signum;
3532c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3542c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3552c42a146SMarcel Moolenaar };
3562c42a146SMarcel Moolenaar #endif
3572c42a146SMarcel Moolenaar /* ARGSUSED */
3582c42a146SMarcel Moolenaar int
3592c42a146SMarcel Moolenaar osigaction(p, uap)
3602c42a146SMarcel Moolenaar 	struct proc *p;
3612c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3622c42a146SMarcel Moolenaar {
3632c42a146SMarcel Moolenaar 	struct osigaction sa;
3642c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
3652c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
3662c42a146SMarcel Moolenaar 	int error;
3672c42a146SMarcel Moolenaar 
3686f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
3696f841fb7SMarcel Moolenaar 		return (EINVAL);
3706f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
3716f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
3722c42a146SMarcel Moolenaar 	if (nsap) {
3736f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
3742c42a146SMarcel Moolenaar 		if (error)
3752c42a146SMarcel Moolenaar 			return (error);
3762c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
3772c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
3782c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
3792c42a146SMarcel Moolenaar 	}
380645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
3812c42a146SMarcel Moolenaar 	if (osap && !error) {
3822c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
3832c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
3842c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
3856f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
3862c42a146SMarcel Moolenaar 	}
3872c42a146SMarcel Moolenaar 	return (error);
3882c42a146SMarcel Moolenaar }
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes /*
391df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
392df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
393df8bae1dSRodney W. Grimes  */
394df8bae1dSRodney W. Grimes void
395df8bae1dSRodney W. Grimes siginit(p)
396df8bae1dSRodney W. Grimes 	struct proc *p;
397df8bae1dSRodney W. Grimes {
398df8bae1dSRodney W. Grimes 	register int i;
399df8bae1dSRodney W. Grimes 
4002c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
4012c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
4022c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
403df8bae1dSRodney W. Grimes }
404df8bae1dSRodney W. Grimes 
405df8bae1dSRodney W. Grimes /*
406df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
407df8bae1dSRodney W. Grimes  */
408df8bae1dSRodney W. Grimes void
409df8bae1dSRodney W. Grimes execsigs(p)
410df8bae1dSRodney W. Grimes 	register struct proc *p;
411df8bae1dSRodney W. Grimes {
412df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
4132c42a146SMarcel Moolenaar 	register int sig;
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes 	/*
416df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
417df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
418df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
419df8bae1dSRodney W. Grimes 	 */
4202c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4212c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4222c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4232c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4242c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4252c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4262c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
427df8bae1dSRodney W. Grimes 		}
4282c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
429df8bae1dSRodney W. Grimes 	}
430df8bae1dSRodney W. Grimes 	/*
431df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
432df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
433df8bae1dSRodney W. Grimes 	 */
434645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
435645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
436645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
43780e907a1SPeter Wemm 	/*
43880e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
43980e907a1SPeter Wemm 	 */
440645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
441df8bae1dSRodney W. Grimes }
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes /*
4447c8fdcbdSMatthew Dillon  * do_sigprocmask() - MP SAFE ONLY IF p == curproc
4457c8fdcbdSMatthew Dillon  *
4467c8fdcbdSMatthew Dillon  *	Manipulate signal mask.  This routine is MP SAFE *ONLY* if
4477c8fdcbdSMatthew Dillon  *	p == curproc.  Also remember that in order to remain MP SAFE
4487c8fdcbdSMatthew Dillon  *	no spl*() calls may be made.
449df8bae1dSRodney W. Grimes  */
4502c42a146SMarcel Moolenaar static int
451645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
4522c42a146SMarcel Moolenaar 	struct proc *p;
4532c42a146SMarcel Moolenaar 	int how;
4542c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
455645682fdSLuoqi Chen 	int old;
4562c42a146SMarcel Moolenaar {
4572c42a146SMarcel Moolenaar 	int error;
4582c42a146SMarcel Moolenaar 
4592c42a146SMarcel Moolenaar 	if (oset != NULL)
4602c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
4612c42a146SMarcel Moolenaar 
4622c42a146SMarcel Moolenaar 	error = 0;
4632c42a146SMarcel Moolenaar 	if (set != NULL) {
4642c42a146SMarcel Moolenaar 		switch (how) {
4652c42a146SMarcel Moolenaar 		case SIG_BLOCK:
466645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
4672c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
4682c42a146SMarcel Moolenaar 			break;
4692c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
4702c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
4712c42a146SMarcel Moolenaar 			break;
4722c42a146SMarcel Moolenaar 		case SIG_SETMASK:
473645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
474645682fdSLuoqi Chen 			if (old)
475645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
476645682fdSLuoqi Chen 			else
4772c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
4782c42a146SMarcel Moolenaar 			break;
4792c42a146SMarcel Moolenaar 		default:
4802c42a146SMarcel Moolenaar 			error = EINVAL;
4812c42a146SMarcel Moolenaar 			break;
4822c42a146SMarcel Moolenaar 		}
4832c42a146SMarcel Moolenaar 	}
4842c42a146SMarcel Moolenaar 	return (error);
4852c42a146SMarcel Moolenaar }
4862c42a146SMarcel Moolenaar 
4877c8fdcbdSMatthew Dillon /*
4887c8fdcbdSMatthew Dillon  * sigprocmask() - MP SAFE
4897c8fdcbdSMatthew Dillon  */
4907c8fdcbdSMatthew Dillon 
491d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
492df8bae1dSRodney W. Grimes struct sigprocmask_args {
493df8bae1dSRodney W. Grimes 	int	how;
4942c42a146SMarcel Moolenaar 	const sigset_t *set;
4952c42a146SMarcel Moolenaar 	sigset_t *oset;
496df8bae1dSRodney W. Grimes };
497d2d3e875SBruce Evans #endif
49826f9a767SRodney W. Grimes int
499cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
500df8bae1dSRodney W. Grimes 	register struct proc *p;
501df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
502df8bae1dSRodney W. Grimes {
5032c42a146SMarcel Moolenaar 	sigset_t set, oset;
5042c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
5052c42a146SMarcel Moolenaar 	int error;
506df8bae1dSRodney W. Grimes 
5076f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
5086f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
5092c42a146SMarcel Moolenaar 	if (setp) {
5106f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
5112c42a146SMarcel Moolenaar 		if (error)
5122c42a146SMarcel Moolenaar 			return (error);
513df8bae1dSRodney W. Grimes 	}
514645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5152c42a146SMarcel Moolenaar 	if (osetp && !error) {
5166f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5172c42a146SMarcel Moolenaar 	}
5182c42a146SMarcel Moolenaar 	return (error);
5192c42a146SMarcel Moolenaar }
5202c42a146SMarcel Moolenaar 
5217c8fdcbdSMatthew Dillon /*
5227c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
5237c8fdcbdSMatthew Dillon  */
5247c8fdcbdSMatthew Dillon 
5252c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5262c42a146SMarcel Moolenaar struct osigprocmask_args {
5272c42a146SMarcel Moolenaar 	int	how;
5282c42a146SMarcel Moolenaar 	osigset_t mask;
5292c42a146SMarcel Moolenaar };
5302c42a146SMarcel Moolenaar #endif
5312c42a146SMarcel Moolenaar int
5322c42a146SMarcel Moolenaar osigprocmask(p, uap)
5332c42a146SMarcel Moolenaar 	register struct proc *p;
5342c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5352c42a146SMarcel Moolenaar {
5362c42a146SMarcel Moolenaar 	sigset_t set, oset;
5372c42a146SMarcel Moolenaar 	int error;
5382c42a146SMarcel Moolenaar 
5392c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
540645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
5412c42a146SMarcel Moolenaar 	SIG2OSIG(oset, p->p_retval[0]);
542df8bae1dSRodney W. Grimes 	return (error);
543df8bae1dSRodney W. Grimes }
544df8bae1dSRodney W. Grimes 
545d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
546df8bae1dSRodney W. Grimes struct sigpending_args {
5472c42a146SMarcel Moolenaar 	sigset_t	*set;
548df8bae1dSRodney W. Grimes };
549d2d3e875SBruce Evans #endif
550df8bae1dSRodney W. Grimes /* ARGSUSED */
55126f9a767SRodney W. Grimes int
552cb226aaaSPoul-Henning Kamp sigpending(p, uap)
553df8bae1dSRodney W. Grimes 	struct proc *p;
554df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
555df8bae1dSRodney W. Grimes {
556df8bae1dSRodney W. Grimes 
5576f841fb7SMarcel Moolenaar 	return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
5582c42a146SMarcel Moolenaar }
5592c42a146SMarcel Moolenaar 
5602c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5612c42a146SMarcel Moolenaar struct osigpending_args {
5622c42a146SMarcel Moolenaar 	int	dummy;
5632c42a146SMarcel Moolenaar };
5642c42a146SMarcel Moolenaar #endif
5652c42a146SMarcel Moolenaar /* ARGSUSED */
5662c42a146SMarcel Moolenaar int
5672c42a146SMarcel Moolenaar osigpending(p, uap)
5682c42a146SMarcel Moolenaar 	struct proc *p;
5692c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
5702c42a146SMarcel Moolenaar {
5712c42a146SMarcel Moolenaar 
5722c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_siglist, p->p_retval[0]);
573df8bae1dSRodney W. Grimes 	return (0);
574df8bae1dSRodney W. Grimes }
575df8bae1dSRodney W. Grimes 
576df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
577df8bae1dSRodney W. Grimes /*
578df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
579df8bae1dSRodney W. Grimes  */
580d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
581df8bae1dSRodney W. Grimes struct osigvec_args {
582df8bae1dSRodney W. Grimes 	int	signum;
583df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
584df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
585df8bae1dSRodney W. Grimes };
586d2d3e875SBruce Evans #endif
587df8bae1dSRodney W. Grimes /* ARGSUSED */
58826f9a767SRodney W. Grimes int
589cb226aaaSPoul-Henning Kamp osigvec(p, uap)
590df8bae1dSRodney W. Grimes 	struct proc *p;
591df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
592df8bae1dSRodney W. Grimes {
593df8bae1dSRodney W. Grimes 	struct sigvec vec;
5942c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5952c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5962c42a146SMarcel Moolenaar 	int error;
597df8bae1dSRodney W. Grimes 
5986f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5996f841fb7SMarcel Moolenaar 		return (EINVAL);
6006f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
6016f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
6022c42a146SMarcel Moolenaar 	if (nsap) {
6036f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
6042c42a146SMarcel Moolenaar 		if (error)
605df8bae1dSRodney W. Grimes 			return (error);
6062c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
6072c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
6082c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
6092c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
610df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
6112c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
612df8bae1dSRodney W. Grimes #endif
613df8bae1dSRodney W. Grimes 	}
614645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
6152c42a146SMarcel Moolenaar 	if (osap && !error) {
6162c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
6172c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
6182c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
6192c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
6202c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
6212c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
6222c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
6232c42a146SMarcel Moolenaar #endif
6246f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
6252c42a146SMarcel Moolenaar 	}
6262c42a146SMarcel Moolenaar 	return (error);
627df8bae1dSRodney W. Grimes }
628df8bae1dSRodney W. Grimes 
629d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
630df8bae1dSRodney W. Grimes struct osigblock_args {
631df8bae1dSRodney W. Grimes 	int	mask;
632df8bae1dSRodney W. Grimes };
633d2d3e875SBruce Evans #endif
63426f9a767SRodney W. Grimes int
635cb226aaaSPoul-Henning Kamp osigblock(p, uap)
636df8bae1dSRodney W. Grimes 	register struct proc *p;
637df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
638df8bae1dSRodney W. Grimes {
6392c42a146SMarcel Moolenaar 	sigset_t set;
640df8bae1dSRodney W. Grimes 
6412c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6422c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
643df8bae1dSRodney W. Grimes 	(void) splhigh();
6442c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
6452c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
646df8bae1dSRodney W. Grimes 	(void) spl0();
647df8bae1dSRodney W. Grimes 	return (0);
648df8bae1dSRodney W. Grimes }
649df8bae1dSRodney W. Grimes 
650d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
651df8bae1dSRodney W. Grimes struct osigsetmask_args {
652df8bae1dSRodney W. Grimes 	int	mask;
653df8bae1dSRodney W. Grimes };
654d2d3e875SBruce Evans #endif
65526f9a767SRodney W. Grimes int
656cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
657df8bae1dSRodney W. Grimes 	struct proc *p;
658df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
659df8bae1dSRodney W. Grimes {
6602c42a146SMarcel Moolenaar 	sigset_t set;
661df8bae1dSRodney W. Grimes 
6622c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6632c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
664df8bae1dSRodney W. Grimes 	(void) splhigh();
6652c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
666645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
667df8bae1dSRodney W. Grimes 	(void) spl0();
668df8bae1dSRodney W. Grimes 	return (0);
669df8bae1dSRodney W. Grimes }
670df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes /*
673df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
674df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
675df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
676df8bae1dSRodney W. Grimes  */
677d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
678df8bae1dSRodney W. Grimes struct sigsuspend_args {
6792c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
680df8bae1dSRodney W. Grimes };
681d2d3e875SBruce Evans #endif
682df8bae1dSRodney W. Grimes /* ARGSUSED */
68326f9a767SRodney W. Grimes int
684cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
685df8bae1dSRodney W. Grimes 	register struct proc *p;
686df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
687df8bae1dSRodney W. Grimes {
6882c42a146SMarcel Moolenaar 	sigset_t mask;
689df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
6902c42a146SMarcel Moolenaar 	int error;
6912c42a146SMarcel Moolenaar 
6926f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
6932c42a146SMarcel Moolenaar 	if (error)
6942c42a146SMarcel Moolenaar 		return (error);
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes 	/*
697645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
698df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
699df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
700df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
701df8bae1dSRodney W. Grimes 	 * to indicate this.
702df8bae1dSRodney W. Grimes 	 */
7036626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
704645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
705645682fdSLuoqi Chen 
706645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
7072c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
7082c42a146SMarcel Moolenaar 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
7092c42a146SMarcel Moolenaar 		/* void */;
7102c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
7112c42a146SMarcel Moolenaar 	return (EINTR);
7122c42a146SMarcel Moolenaar }
7132c42a146SMarcel Moolenaar 
7142c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7152c42a146SMarcel Moolenaar struct osigsuspend_args {
7162c42a146SMarcel Moolenaar 	osigset_t mask;
7172c42a146SMarcel Moolenaar };
7182c42a146SMarcel Moolenaar #endif
7192c42a146SMarcel Moolenaar /* ARGSUSED */
7202c42a146SMarcel Moolenaar int
7212c42a146SMarcel Moolenaar osigsuspend(p, uap)
7222c42a146SMarcel Moolenaar 	register struct proc *p;
7232c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
7242c42a146SMarcel Moolenaar {
725645682fdSLuoqi Chen 	sigset_t mask;
7262c42a146SMarcel Moolenaar 	register struct sigacts *ps = p->p_sigacts;
7272c42a146SMarcel Moolenaar 
7282c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
729645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
730645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
731645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
732645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
733645682fdSLuoqi Chen 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
734df8bae1dSRodney W. Grimes 		/* void */;
735df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
736df8bae1dSRodney W. Grimes 	return (EINTR);
737df8bae1dSRodney W. Grimes }
738df8bae1dSRodney W. Grimes 
739df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
740d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
741df8bae1dSRodney W. Grimes struct osigstack_args {
742df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
743df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
744df8bae1dSRodney W. Grimes };
745d2d3e875SBruce Evans #endif
746df8bae1dSRodney W. Grimes /* ARGSUSED */
74726f9a767SRodney W. Grimes int
748cb226aaaSPoul-Henning Kamp osigstack(p, uap)
749df8bae1dSRodney W. Grimes 	struct proc *p;
750df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
751df8bae1dSRodney W. Grimes {
752df8bae1dSRodney W. Grimes 	struct sigstack ss;
753df8bae1dSRodney W. Grimes 	int error = 0;
754df8bae1dSRodney W. Grimes 
755645682fdSLuoqi Chen 	ss.ss_sp = p->p_sigstk.ss_sp;
756645682fdSLuoqi Chen 	ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
7576f841fb7SMarcel Moolenaar 	if (uap->oss && (error = copyout(&ss, uap->oss,
758df8bae1dSRodney W. Grimes 	    sizeof(struct sigstack))))
759df8bae1dSRodney W. Grimes 		return (error);
7606f841fb7SMarcel Moolenaar 	if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
761645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
762645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
763645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
764645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
765df8bae1dSRodney W. Grimes 	}
766df8bae1dSRodney W. Grimes 	return (error);
767df8bae1dSRodney W. Grimes }
768df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
769df8bae1dSRodney W. Grimes 
770d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
771df8bae1dSRodney W. Grimes struct sigaltstack_args {
7722c42a146SMarcel Moolenaar 	stack_t	*ss;
7732c42a146SMarcel Moolenaar 	stack_t	*oss;
774df8bae1dSRodney W. Grimes };
775d2d3e875SBruce Evans #endif
776df8bae1dSRodney W. Grimes /* ARGSUSED */
77726f9a767SRodney W. Grimes int
778cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
779df8bae1dSRodney W. Grimes 	struct proc *p;
780df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
781df8bae1dSRodney W. Grimes {
7822c42a146SMarcel Moolenaar 	stack_t ss;
783df8bae1dSRodney W. Grimes 	int error;
784df8bae1dSRodney W. Grimes 
785645682fdSLuoqi Chen 	if ((p->p_flag & P_ALTSTACK) == 0)
786645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= SS_DISABLE;
7876f841fb7SMarcel Moolenaar 	if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
7886f841fb7SMarcel Moolenaar 	    sizeof(stack_t))))
789df8bae1dSRodney W. Grimes 		return (error);
7902c42a146SMarcel Moolenaar 	if (uap->ss == 0)
791df8bae1dSRodney W. Grimes 		return (0);
7926f841fb7SMarcel Moolenaar 	if ((error = copyin(uap->ss, &ss, sizeof(ss))))
793df8bae1dSRodney W. Grimes 		return (error);
794d66a5066SPeter Wemm 	if (ss.ss_flags & SS_DISABLE) {
795645682fdSLuoqi Chen 		if (p->p_sigstk.ss_flags & SS_ONSTACK)
796df8bae1dSRodney W. Grimes 			return (EINVAL);
797645682fdSLuoqi Chen 		p->p_flag &= ~P_ALTSTACK;
798645682fdSLuoqi Chen 		p->p_sigstk.ss_flags = ss.ss_flags;
799df8bae1dSRodney W. Grimes 		return (0);
800df8bae1dSRodney W. Grimes 	}
801df8bae1dSRodney W. Grimes 	if (ss.ss_size < MINSIGSTKSZ)
802df8bae1dSRodney W. Grimes 		return (ENOMEM);
803645682fdSLuoqi Chen 	p->p_flag |= P_ALTSTACK;
804645682fdSLuoqi Chen 	p->p_sigstk = ss;
805df8bae1dSRodney W. Grimes 	return (0);
806df8bae1dSRodney W. Grimes }
807df8bae1dSRodney W. Grimes 
808d93f860cSPoul-Henning Kamp /*
809d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
810d93f860cSPoul-Henning Kamp  * cp is calling process.
811d93f860cSPoul-Henning Kamp  */
812d93f860cSPoul-Henning Kamp int
8132c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
814d93f860cSPoul-Henning Kamp 	register struct proc *cp;
8152c42a146SMarcel Moolenaar 	int sig, pgid, all;
816d93f860cSPoul-Henning Kamp {
817d93f860cSPoul-Henning Kamp 	register struct proc *p;
818d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
819d93f860cSPoul-Henning Kamp 	int nfound = 0;
820d93f860cSPoul-Henning Kamp 
821d93f860cSPoul-Henning Kamp 	if (all)
822d93f860cSPoul-Henning Kamp 		/*
823d93f860cSPoul-Henning Kamp 		 * broadcast
824d93f860cSPoul-Henning Kamp 		 */
8252e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
826d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
827a9e0361bSPoul-Henning Kamp 			    p == cp || !CANSIGNAL(cp, p, sig))
828d93f860cSPoul-Henning Kamp 				continue;
829d93f860cSPoul-Henning Kamp 			nfound++;
8302c42a146SMarcel Moolenaar 			if (sig)
8312c42a146SMarcel Moolenaar 				psignal(p, sig);
832d93f860cSPoul-Henning Kamp 		}
833d93f860cSPoul-Henning Kamp 	else {
834d93f860cSPoul-Henning Kamp 		if (pgid == 0)
835d93f860cSPoul-Henning Kamp 			/*
836d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
837d93f860cSPoul-Henning Kamp 			 */
838d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
839d93f860cSPoul-Henning Kamp 		else {
840d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
841d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
842d93f860cSPoul-Henning Kamp 				return (ESRCH);
843d93f860cSPoul-Henning Kamp 		}
8442e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
845d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
846d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
847a9e0361bSPoul-Henning Kamp 			    !CANSIGNAL(cp, p, sig))
848d93f860cSPoul-Henning Kamp 				continue;
849d93f860cSPoul-Henning Kamp 			nfound++;
8502c42a146SMarcel Moolenaar 			if (sig)
8512c42a146SMarcel Moolenaar 				psignal(p, sig);
852d93f860cSPoul-Henning Kamp 		}
853d93f860cSPoul-Henning Kamp 	}
854d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
855d93f860cSPoul-Henning Kamp }
856d93f860cSPoul-Henning Kamp 
857d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
858df8bae1dSRodney W. Grimes struct kill_args {
859df8bae1dSRodney W. Grimes 	int	pid;
860df8bae1dSRodney W. Grimes 	int	signum;
861df8bae1dSRodney W. Grimes };
862d2d3e875SBruce Evans #endif
863df8bae1dSRodney W. Grimes /* ARGSUSED */
86426f9a767SRodney W. Grimes int
865cb226aaaSPoul-Henning Kamp kill(cp, uap)
866df8bae1dSRodney W. Grimes 	register struct proc *cp;
867df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
868df8bae1dSRodney W. Grimes {
869df8bae1dSRodney W. Grimes 	register struct proc *p;
870df8bae1dSRodney W. Grimes 
8712c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
872df8bae1dSRodney W. Grimes 		return (EINVAL);
873df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
874df8bae1dSRodney W. Grimes 		/* kill single process */
875df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
876df8bae1dSRodney W. Grimes 			return (ESRCH);
877a9e0361bSPoul-Henning Kamp 		if (!CANSIGNAL(cp, p, uap->signum))
878df8bae1dSRodney W. Grimes 			return (EPERM);
879df8bae1dSRodney W. Grimes 		if (uap->signum)
880df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
881df8bae1dSRodney W. Grimes 		return (0);
882df8bae1dSRodney W. Grimes 	}
883df8bae1dSRodney W. Grimes 	switch (uap->pid) {
884df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
885df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
886df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
887df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
888df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
889df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
890df8bae1dSRodney W. Grimes 	}
891df8bae1dSRodney W. Grimes 	/* NOTREACHED */
892df8bae1dSRodney W. Grimes }
893df8bae1dSRodney W. Grimes 
894df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
895d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
896df8bae1dSRodney W. Grimes struct okillpg_args {
897df8bae1dSRodney W. Grimes 	int	pgid;
898df8bae1dSRodney W. Grimes 	int	signum;
899df8bae1dSRodney W. Grimes };
900d2d3e875SBruce Evans #endif
901df8bae1dSRodney W. Grimes /* ARGSUSED */
90226f9a767SRodney W. Grimes int
903cb226aaaSPoul-Henning Kamp okillpg(p, uap)
904df8bae1dSRodney W. Grimes 	struct proc *p;
905df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
906df8bae1dSRodney W. Grimes {
907df8bae1dSRodney W. Grimes 
9082c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
909df8bae1dSRodney W. Grimes 		return (EINVAL);
910df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
911df8bae1dSRodney W. Grimes }
912df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
913df8bae1dSRodney W. Grimes 
914df8bae1dSRodney W. Grimes /*
915df8bae1dSRodney W. Grimes  * Send a signal to a process group.
916df8bae1dSRodney W. Grimes  */
917df8bae1dSRodney W. Grimes void
9182c42a146SMarcel Moolenaar gsignal(pgid, sig)
9192c42a146SMarcel Moolenaar 	int pgid, sig;
920df8bae1dSRodney W. Grimes {
921df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
922df8bae1dSRodney W. Grimes 
923df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
9242c42a146SMarcel Moolenaar 		pgsignal(pgrp, sig, 0);
925df8bae1dSRodney W. Grimes }
926df8bae1dSRodney W. Grimes 
927df8bae1dSRodney W. Grimes /*
928df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
929df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
930df8bae1dSRodney W. Grimes  */
931df8bae1dSRodney W. Grimes void
9322c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
933df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
9342c42a146SMarcel Moolenaar 	int sig, checkctty;
935df8bae1dSRodney W. Grimes {
936df8bae1dSRodney W. Grimes 	register struct proc *p;
937df8bae1dSRodney W. Grimes 
938df8bae1dSRodney W. Grimes 	if (pgrp)
9392e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
940df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
9412c42a146SMarcel Moolenaar 				psignal(p, sig);
942df8bae1dSRodney W. Grimes }
943df8bae1dSRodney W. Grimes 
944df8bae1dSRodney W. Grimes /*
945df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
946df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
947df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
948df8bae1dSRodney W. Grimes  */
949df8bae1dSRodney W. Grimes void
9502c42a146SMarcel Moolenaar trapsignal(p, sig, code)
951df8bae1dSRodney W. Grimes 	struct proc *p;
9522c42a146SMarcel Moolenaar 	register int sig;
9538674077aSJeffrey Hsu 	u_long code;
954df8bae1dSRodney W. Grimes {
955df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
956df8bae1dSRodney W. Grimes 
9572c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
9582c42a146SMarcel Moolenaar 	    SIGISMEMBER(p->p_sigmask, sig)) {
959df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
960df8bae1dSRodney W. Grimes #ifdef KTRACE
961df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
9622c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
9632c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
964df8bae1dSRodney W. Grimes #endif
9652c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
9662c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
9672c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
9682c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
9692c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
9702c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
971289ccde0SPeter Wemm 			/*
9722c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
973289ccde0SPeter Wemm 			 */
9742c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
9752c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
9762c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
9772c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
9782c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
979dedc04feSPeter Wemm 		}
9806f841fb7SMarcel Moolenaar 	} else {
9816626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
9822c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
9832c42a146SMarcel Moolenaar 		psignal(p, sig);
984df8bae1dSRodney W. Grimes 	}
985df8bae1dSRodney W. Grimes }
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes /*
988df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
989df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
990df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
991df8bae1dSRodney W. Grimes  *
992df8bae1dSRodney W. Grimes  * Exceptions:
993df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
994df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
995df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
996df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
997df8bae1dSRodney W. Grimes  *
998df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
999df8bae1dSRodney W. Grimes  */
1000df8bae1dSRodney W. Grimes void
10012c42a146SMarcel Moolenaar psignal(p, sig)
1002df8bae1dSRodney W. Grimes 	register struct proc *p;
10032c42a146SMarcel Moolenaar 	register int sig;
1004df8bae1dSRodney W. Grimes {
1005df8bae1dSRodney W. Grimes 	register int s, prop;
1006df8bae1dSRodney W. Grimes 	register sig_t action;
1007df8bae1dSRodney W. Grimes 
10082c42a146SMarcel Moolenaar 	if (sig > _SIG_MAXSIG || sig <= 0) {
10092c42a146SMarcel Moolenaar 		printf("psignal: signal %d\n", sig);
1010df8bae1dSRodney W. Grimes 		panic("psignal signal number");
10112a024a2bSSean Eric Fagan 	}
10122c42a146SMarcel Moolenaar 
1013cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1014cb679c38SJonathan Lemon 
10152c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1016df8bae1dSRodney W. Grimes 
1017df8bae1dSRodney W. Grimes 	/*
10182a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
10192a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
10202a024a2bSSean Eric Fagan 	 * a chance, as well.
1021df8bae1dSRodney W. Grimes 	 */
10222a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1023df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1024df8bae1dSRodney W. Grimes 	else {
1025df8bae1dSRodney W. Grimes 		/*
1026df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1027df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1028df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1029df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1030df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1031df8bae1dSRodney W. Grimes 		 */
10322c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1033df8bae1dSRodney W. Grimes 			return;
10342c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1035df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
10362c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1037df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1038df8bae1dSRodney W. Grimes 		else
1039df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1040df8bae1dSRodney W. Grimes 	}
1041df8bae1dSRodney W. Grimes 
1042df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1043df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
1044df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
1045df8bae1dSRodney W. Grimes 
1046df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
10472c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1050df8bae1dSRodney W. Grimes 		/*
1051df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1052df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1053df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1054df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1055df8bae1dSRodney W. Grimes 		 */
1056df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1057df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
1058df8bae1dSRodney W. Grimes 		        return;
10592c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1060df8bae1dSRodney W. Grimes 	}
10612c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1062df8bae1dSRodney W. Grimes 
1063df8bae1dSRodney W. Grimes 	/*
1064df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1065df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1066df8bae1dSRodney W. Grimes 	 */
10672c42a146SMarcel Moolenaar 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1068df8bae1dSRodney W. Grimes 		return;
1069df8bae1dSRodney W. Grimes 	s = splhigh();
1070df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1071df8bae1dSRodney W. Grimes 
1072df8bae1dSRodney W. Grimes 	case SSLEEP:
1073df8bae1dSRodney W. Grimes 		/*
1074df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1075df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1076df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1077df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1078df8bae1dSRodney W. Grimes 		 */
1079df8bae1dSRodney W. Grimes 		if ((p->p_flag & P_SINTR) == 0)
1080df8bae1dSRodney W. Grimes 			goto out;
1081df8bae1dSRodney W. Grimes 		/*
1082df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1083df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1084df8bae1dSRodney W. Grimes 		 * for the parent.
1085df8bae1dSRodney W. Grimes 		 */
1086df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1087df8bae1dSRodney W. Grimes 			goto run;
1088df8bae1dSRodney W. Grimes 		/*
1089df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1090df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1091df8bae1dSRodney W. Grimes 		 * be awakened.
1092df8bae1dSRodney W. Grimes 		 */
1093df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
10942c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1095df8bae1dSRodney W. Grimes 			goto out;
1096df8bae1dSRodney W. Grimes 		}
1097df8bae1dSRodney W. Grimes 		/*
1098df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1099df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1100df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1101df8bae1dSRodney W. Grimes 		 * cause the process to run.
1102df8bae1dSRodney W. Grimes 		 */
1103df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1104df8bae1dSRodney W. Grimes 			if (action != SIG_DFL)
1105df8bae1dSRodney W. Grimes 				goto runfast;
1106df8bae1dSRodney W. Grimes 			/*
1107df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1108df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1109df8bae1dSRodney W. Grimes 			 */
1110df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1111df8bae1dSRodney W. Grimes 				goto out;
11122c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
11132c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1114645682fdSLuoqi Chen 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1115df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1116df8bae1dSRodney W. Grimes 			stop(p);
1117df8bae1dSRodney W. Grimes 			goto out;
1118df8bae1dSRodney W. Grimes 		} else
1119df8bae1dSRodney W. Grimes 			goto runfast;
1120df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
1121df8bae1dSRodney W. Grimes 
1122df8bae1dSRodney W. Grimes 	case SSTOP:
1123df8bae1dSRodney W. Grimes 		/*
1124df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1125df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1126df8bae1dSRodney W. Grimes 		 */
1127df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1128df8bae1dSRodney W. Grimes 			goto out;
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes 		/*
1131df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1132df8bae1dSRodney W. Grimes 		 */
11332c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1134df8bae1dSRodney W. Grimes 			goto runfast;
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1137df8bae1dSRodney W. Grimes 			/*
1138df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1139df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1140df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1141df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1142df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1143df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1144df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1145df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1146df8bae1dSRodney W. Grimes 			 */
1147df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
11482c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1149df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1150df8bae1dSRodney W. Grimes 				goto runfast;
1151df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
1152df8bae1dSRodney W. Grimes 				goto run;
1153df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
1154df8bae1dSRodney W. Grimes 			goto out;
1155df8bae1dSRodney W. Grimes 		}
1156df8bae1dSRodney W. Grimes 
1157df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1158df8bae1dSRodney W. Grimes 			/*
1159df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1160df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1161df8bae1dSRodney W. Grimes 			 */
11622c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1163df8bae1dSRodney W. Grimes 			goto out;
1164df8bae1dSRodney W. Grimes 		}
1165df8bae1dSRodney W. Grimes 
1166df8bae1dSRodney W. Grimes 		/*
1167df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1168df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1169df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1170df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1171df8bae1dSRodney W. Grimes 		 */
1172df8bae1dSRodney W. Grimes 		if (p->p_wchan && p->p_flag & P_SINTR)
1173df8bae1dSRodney W. Grimes 			unsleep(p);
1174df8bae1dSRodney W. Grimes 		goto out;
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes 	default:
1177df8bae1dSRodney W. Grimes 		/*
1178df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1179df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1180df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1181df8bae1dSRodney W. Grimes 		 */
1182df8bae1dSRodney W. Grimes 		if (p == curproc)
1183df8bae1dSRodney W. Grimes 			signotify(p);
11843163861cSTor Egge #ifdef SMP
11853163861cSTor Egge 		else if (p->p_stat == SRUN)
11863163861cSTor Egge 			forward_signal(p);
11873163861cSTor Egge #endif
1188df8bae1dSRodney W. Grimes 		goto out;
1189df8bae1dSRodney W. Grimes 	}
1190df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1191df8bae1dSRodney W. Grimes 
1192df8bae1dSRodney W. Grimes runfast:
1193df8bae1dSRodney W. Grimes 	/*
1194df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1195df8bae1dSRodney W. Grimes 	 */
1196df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
1197df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
1198df8bae1dSRodney W. Grimes run:
1199df8bae1dSRodney W. Grimes 	setrunnable(p);
1200df8bae1dSRodney W. Grimes out:
1201df8bae1dSRodney W. Grimes 	splx(s);
1202df8bae1dSRodney W. Grimes }
1203df8bae1dSRodney W. Grimes 
1204df8bae1dSRodney W. Grimes /*
1205df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1206df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1207df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1208df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1209df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1210df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1211df8bae1dSRodney W. Grimes  * sequence is
1212df8bae1dSRodney W. Grimes  *
12132c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
12142c42a146SMarcel Moolenaar  *		postsig(sig);
1215df8bae1dSRodney W. Grimes  */
121626f9a767SRodney W. Grimes int
1217df8bae1dSRodney W. Grimes issignal(p)
1218df8bae1dSRodney W. Grimes 	register struct proc *p;
1219df8bae1dSRodney W. Grimes {
12202c42a146SMarcel Moolenaar 	sigset_t mask;
12212c42a146SMarcel Moolenaar 	register int sig, prop;
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 	for (;;) {
12242a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
12252a024a2bSSean Eric Fagan 
12262c42a146SMarcel Moolenaar 		mask = p->p_siglist;
12272c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1228df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
12292c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
12302c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))	 	/* no signal to send */
1231df8bae1dSRodney W. Grimes 			return (0);
12322c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
12332c42a146SMarcel Moolenaar 		prop = sigprop(sig);
12342a024a2bSSean Eric Fagan 
12352c42a146SMarcel Moolenaar 		STOPEVENT(p, S_SIG, sig);
12362a024a2bSSean Eric Fagan 
1237df8bae1dSRodney W. Grimes 		/*
1238df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1239df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1240df8bae1dSRodney W. Grimes 		 */
12412c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
12422c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1243df8bae1dSRodney W. Grimes 			continue;
1244df8bae1dSRodney W. Grimes 		}
1245df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1246df8bae1dSRodney W. Grimes 			/*
1247df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1248df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1249df8bae1dSRodney W. Grimes 			 */
12502c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1251df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1252df8bae1dSRodney W. Grimes 			do {
1253df8bae1dSRodney W. Grimes 				stop(p);
1254df8bae1dSRodney W. Grimes 				mi_switch();
12552a024a2bSSean Eric Fagan 			} while (!trace_req(p)
12562a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1257df8bae1dSRodney W. Grimes 
1258df8bae1dSRodney W. Grimes 			/*
1259df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1260df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1261df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1262df8bae1dSRodney W. Grimes 			 */
1263df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1264df8bae1dSRodney W. Grimes 				continue;
1265df8bae1dSRodney W. Grimes 
1266df8bae1dSRodney W. Grimes 			/*
1267df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1268df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1269df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1270df8bae1dSRodney W. Grimes 			 */
12712c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
12722c42a146SMarcel Moolenaar 			sig = p->p_xstat;
12732c42a146SMarcel Moolenaar 			if (sig == 0)
1274df8bae1dSRodney W. Grimes 				continue;
1275df8bae1dSRodney W. Grimes 
1276df8bae1dSRodney W. Grimes 			/*
1277df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1278df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1279df8bae1dSRodney W. Grimes 			 */
12802c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
12812c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1282df8bae1dSRodney W. Grimes 				continue;
1283df8bae1dSRodney W. Grimes 		}
1284df8bae1dSRodney W. Grimes 
1285df8bae1dSRodney W. Grimes 		/*
1286df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1287df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1288df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1289df8bae1dSRodney W. Grimes 		 */
12902c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1291df8bae1dSRodney W. Grimes 
12920b53fbe8SBruce Evans 		case (int)SIG_DFL:
1293df8bae1dSRodney W. Grimes 			/*
1294df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1295df8bae1dSRodney W. Grimes 			 */
1296df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1297df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1298df8bae1dSRodney W. Grimes 				/*
1299df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1300df8bae1dSRodney W. Grimes 				 * in init? XXX
1301df8bae1dSRodney W. Grimes 				 */
1302d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
13032c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1304df8bae1dSRodney W. Grimes #endif
1305df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1306df8bae1dSRodney W. Grimes 			}
1307df8bae1dSRodney W. Grimes 			/*
1308df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1309df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1310df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1311df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1312df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1313df8bae1dSRodney W. Grimes 			 */
1314df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1315df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1316df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1317df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1318df8bae1dSRodney W. Grimes 					break;	/* == ignore */
13192c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1320df8bae1dSRodney W. Grimes 				stop(p);
1321645682fdSLuoqi Chen 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1322df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1323df8bae1dSRodney W. Grimes 				mi_switch();
1324df8bae1dSRodney W. Grimes 				break;
1325df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1326df8bae1dSRodney W. Grimes 				/*
1327df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1328df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1329df8bae1dSRodney W. Grimes 				 */
1330df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1331df8bae1dSRodney W. Grimes 			} else
13322c42a146SMarcel Moolenaar 				return (sig);
1333df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1334df8bae1dSRodney W. Grimes 
13350b53fbe8SBruce Evans 		case (int)SIG_IGN:
1336df8bae1dSRodney W. Grimes 			/*
1337df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1338df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1339df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1340df8bae1dSRodney W. Grimes 			 */
1341df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1342df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1343df8bae1dSRodney W. Grimes 				printf("issignal\n");
1344df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes 		default:
1347df8bae1dSRodney W. Grimes 			/*
1348df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1349df8bae1dSRodney W. Grimes 			 * postsig() process it.
1350df8bae1dSRodney W. Grimes 			 */
13512c42a146SMarcel Moolenaar 			return (sig);
1352df8bae1dSRodney W. Grimes 		}
13532c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1354df8bae1dSRodney W. Grimes 	}
1355df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1356df8bae1dSRodney W. Grimes }
1357df8bae1dSRodney W. Grimes 
1358df8bae1dSRodney W. Grimes /*
1359df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1360df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
1361df8bae1dSRodney W. Grimes  * on the run queue.
1362df8bae1dSRodney W. Grimes  */
136326f9a767SRodney W. Grimes void
1364df8bae1dSRodney W. Grimes stop(p)
1365df8bae1dSRodney W. Grimes 	register struct proc *p;
1366df8bae1dSRodney W. Grimes {
1367df8bae1dSRodney W. Grimes 
1368df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1369df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1370df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1371df8bae1dSRodney W. Grimes }
1372df8bae1dSRodney W. Grimes 
1373df8bae1dSRodney W. Grimes /*
1374df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1375df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1376df8bae1dSRodney W. Grimes  */
1377df8bae1dSRodney W. Grimes void
13782c42a146SMarcel Moolenaar postsig(sig)
13792c42a146SMarcel Moolenaar 	register int sig;
1380df8bae1dSRodney W. Grimes {
1381df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
13822c42a146SMarcel Moolenaar 	struct sigacts *ps = p->p_sigacts;
13832c42a146SMarcel Moolenaar 	sig_t action;
13842c42a146SMarcel Moolenaar 	sigset_t returnmask;
13852c42a146SMarcel Moolenaar 	int code;
1386df8bae1dSRodney W. Grimes 
13872c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
13885526d2d9SEivind Eklund 
13892c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
13902c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1391df8bae1dSRodney W. Grimes #ifdef KTRACE
1392df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1393645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
13942c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1395df8bae1dSRodney W. Grimes #endif
13962c42a146SMarcel Moolenaar 	STOPEVENT(p, S_SIG, sig);
13972a024a2bSSean Eric Fagan 
1398df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1399df8bae1dSRodney W. Grimes 		/*
1400df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1401df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1402df8bae1dSRodney W. Grimes 		 */
14032c42a146SMarcel Moolenaar 		sigexit(p, sig);
1404df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1405df8bae1dSRodney W. Grimes 	} else {
1406df8bae1dSRodney W. Grimes 		/*
1407df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1408df8bae1dSRodney W. Grimes 		 */
14092c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
14105526d2d9SEivind Eklund 		    ("postsig action"));
1411df8bae1dSRodney W. Grimes 		/*
1412df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1413645682fdSLuoqi Chen 		 * occurrences of this signal.
1414df8bae1dSRodney W. Grimes 		 *
1415645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1416df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1417645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1418df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1419df8bae1dSRodney W. Grimes 		 */
1420df8bae1dSRodney W. Grimes 		(void) splhigh();
1421645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
14226626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1423645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1424df8bae1dSRodney W. Grimes 		} else
1425df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
14262c42a146SMarcel Moolenaar 
14272c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
14282c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
14292c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
14302c42a146SMarcel Moolenaar 
14312c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1432289ccde0SPeter Wemm 			/*
14332c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1434289ccde0SPeter Wemm 			 */
14352c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
14362c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
14372c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
14382c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
14392c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1440dedc04feSPeter Wemm 		}
1441df8bae1dSRodney W. Grimes 		(void) spl0();
1442df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
14432c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1444df8bae1dSRodney W. Grimes 			code = 0;
1445df8bae1dSRodney W. Grimes 		} else {
14466626c604SJulian Elischer 			code = p->p_code;
14476626c604SJulian Elischer 			p->p_code = 0;
14486626c604SJulian Elischer 			p->p_sig = 0;
1449df8bae1dSRodney W. Grimes 		}
14502c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1451df8bae1dSRodney W. Grimes 	}
1452df8bae1dSRodney W. Grimes }
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes /*
1455df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1456df8bae1dSRodney W. Grimes  */
145726f9a767SRodney W. Grimes void
1458df8bae1dSRodney W. Grimes killproc(p, why)
1459df8bae1dSRodney W. Grimes 	struct proc *p;
1460df8bae1dSRodney W. Grimes 	char *why;
1461df8bae1dSRodney W. Grimes {
1462729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1463729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1464df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1465df8bae1dSRodney W. Grimes }
1466df8bae1dSRodney W. Grimes 
1467df8bae1dSRodney W. Grimes /*
1468df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1469df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1470df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1471df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1472df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1473df8bae1dSRodney W. Grimes  * does not return.
1474df8bae1dSRodney W. Grimes  */
147526f9a767SRodney W. Grimes void
14762c42a146SMarcel Moolenaar sigexit(p, sig)
1477df8bae1dSRodney W. Grimes 	register struct proc *p;
14782c42a146SMarcel Moolenaar 	int sig;
1479df8bae1dSRodney W. Grimes {
1480df8bae1dSRodney W. Grimes 
1481df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
14822c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
14832c42a146SMarcel Moolenaar 		p->p_sig = sig;
1484c364e17eSAndrey A. Chernov 		/*
1485c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1486c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1487c364e17eSAndrey A. Chernov 		 * these messages.)
1488c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1489c364e17eSAndrey A. Chernov 		 */
1490fca666a1SJulian Elischer 		if (coredump(p) == 0)
14912c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
149257308494SJoerg Wunsch 		if (kern_logsigexit)
149357308494SJoerg Wunsch 			log(LOG_INFO,
149457308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
14953d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
14963d1b21c6SAndrey A. Chernov 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
14972c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
14982c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1499df8bae1dSRodney W. Grimes 	}
15002c42a146SMarcel Moolenaar 	exit1(p, W_EXITCODE(0, sig));
1501df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1502df8bae1dSRodney W. Grimes }
1503df8bae1dSRodney W. Grimes 
1504c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1505c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1506c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1507c5edb423SSean Eric Fagan 
1508c5edb423SSean Eric Fagan /*
1509c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1510c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1511c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1512c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1513c5edb423SSean Eric Fagan  *	%P	process id (pid)
1514c5edb423SSean Eric Fagan  *	%U	user id (uid)
1515c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1516c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1517c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1518c5edb423SSean Eric Fagan  */
1519c5edb423SSean Eric Fagan 
1520fca666a1SJulian Elischer static char *
1521c5edb423SSean Eric Fagan expand_name(name, uid, pid)
152287f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1523c5edb423SSean Eric Fagan 	char *temp;
1524c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1525c5edb423SSean Eric Fagan 	int i, n;
1526c5edb423SSean Eric Fagan 	char *format = corefilename;
1527ce38ca0fSAlfred Perlstein 	size_t namelen;
1528c5edb423SSean Eric Fagan 
1529ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
15300bfe2990SEivind Eklund 	if (temp == NULL)
15310bfe2990SEivind Eklund 		return NULL;
1532ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1533ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1534c5edb423SSean Eric Fagan 		int l;
1535c5edb423SSean Eric Fagan 		switch (format[i]) {
1536c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1537c5edb423SSean Eric Fagan 			i++;
1538c5edb423SSean Eric Fagan 			switch (format[i]) {
1539c5edb423SSean Eric Fagan 			case '%':
1540c5edb423SSean Eric Fagan 				temp[n++] = '%';
1541c5edb423SSean Eric Fagan 				break;
1542c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1543ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
154487f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1545c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1546c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1547c5edb423SSean Eric Fagan 					return NULL;
1548c5edb423SSean Eric Fagan 				}
1549ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1550ce38ca0fSAlfred Perlstein 				n += namelen;
1551c5edb423SSean Eric Fagan 				break;
1552c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1553ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1554c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
155587f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1556c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1557c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1558c5edb423SSean Eric Fagan 					return NULL;
1559c5edb423SSean Eric Fagan 				}
1560c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1561c5edb423SSean Eric Fagan 				n += l;
1562c5edb423SSean Eric Fagan 				break;
1563c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1564ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1565c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
156687f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1567c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1568c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1569c5edb423SSean Eric Fagan 					return NULL;
1570c5edb423SSean Eric Fagan 				}
1571c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1572c5edb423SSean Eric Fagan 				n += l;
1573c5edb423SSean Eric Fagan 				break;
1574c5edb423SSean Eric Fagan 			default:
1575c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1576c5edb423SSean Eric Fagan 			}
1577c5edb423SSean Eric Fagan 			break;
1578c5edb423SSean Eric Fagan 		default:
1579c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1580c5edb423SSean Eric Fagan 		}
1581c5edb423SSean Eric Fagan 	}
1582ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1583c5edb423SSean Eric Fagan 	return temp;
1584c5edb423SSean Eric Fagan }
1585c5edb423SSean Eric Fagan 
1586df8bae1dSRodney W. Grimes /*
1587fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1588fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1589fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1590fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1591fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1592fca666a1SJulian Elischer  */
1593fca666a1SJulian Elischer 
1594fca666a1SJulian Elischer static int
1595fca666a1SJulian Elischer coredump(p)
1596fca666a1SJulian Elischer 	register struct proc *p;
1597fca666a1SJulian Elischer {
1598fca666a1SJulian Elischer 	register struct vnode *vp;
1599da654d90SPoul-Henning Kamp 	register struct ucred *cred = p->p_ucred;
1600fca666a1SJulian Elischer 	struct nameidata nd;
1601fca666a1SJulian Elischer 	struct vattr vattr;
1602fca666a1SJulian Elischer 	int error, error1;
1603fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1604fca666a1SJulian Elischer 	off_t limit;
1605fca666a1SJulian Elischer 
1606fca666a1SJulian Elischer 	STOPEVENT(p, S_CORE, 0);
1607fca666a1SJulian Elischer 
1608e5a28db9SPaul Saab 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1609fca666a1SJulian Elischer 		return (EFAULT);
1610fca666a1SJulian Elischer 
1611fca666a1SJulian Elischer 	/*
161235a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
161335a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
161435a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
161535a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
161635a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
161735a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1618fca666a1SJulian Elischer 	 */
161935a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
162035a2598fSSean Eric Fagan 	if (limit == 0)
162135a2598fSSean Eric Fagan 		return 0;
162235a2598fSSean Eric Fagan 
1623fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1624fca666a1SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1625fca666a1SJulian Elischer 	error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1626fca666a1SJulian Elischer 	free(name, M_TEMP);
1627fca666a1SJulian Elischer 	if (error)
1628fca666a1SJulian Elischer 		return (error);
1629762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1630fca666a1SJulian Elischer 	vp = nd.ni_vp;
1631fca666a1SJulian Elischer 
1632fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
1633fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
1634fca666a1SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1635fca666a1SJulian Elischer 		error = EFAULT;
1636fca666a1SJulian Elischer 		goto out;
1637fca666a1SJulian Elischer 	}
1638fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
1639fca666a1SJulian Elischer 	vattr.va_size = 0;
1640fca666a1SJulian Elischer 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1641fca666a1SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, p);
1642fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
1643fca666a1SJulian Elischer 
1644fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
1645fca666a1SJulian Elischer 	  p->p_sysent->sv_coredump(p, vp, limit) :
1646fca666a1SJulian Elischer 	  ENOSYS;
1647fca666a1SJulian Elischer 
1648fca666a1SJulian Elischer out:
1649fca666a1SJulian Elischer 	VOP_UNLOCK(vp, 0, p);
1650fca666a1SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, p);
1651fca666a1SJulian Elischer 	if (error == 0)
1652fca666a1SJulian Elischer 		error = error1;
1653fca666a1SJulian Elischer 	return (error);
1654fca666a1SJulian Elischer }
1655fca666a1SJulian Elischer 
1656fca666a1SJulian Elischer /*
1657df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1658df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1659df8bae1dSRodney W. Grimes  */
1660d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1661df8bae1dSRodney W. Grimes struct nosys_args {
1662df8bae1dSRodney W. Grimes 	int	dummy;
1663df8bae1dSRodney W. Grimes };
1664d2d3e875SBruce Evans #endif
1665df8bae1dSRodney W. Grimes /* ARGSUSED */
166626f9a767SRodney W. Grimes int
1667cb226aaaSPoul-Henning Kamp nosys(p, args)
1668df8bae1dSRodney W. Grimes 	struct proc *p;
1669df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1670df8bae1dSRodney W. Grimes {
1671df8bae1dSRodney W. Grimes 
1672df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1673df8bae1dSRodney W. Grimes 	return (EINVAL);
1674df8bae1dSRodney W. Grimes }
1675831d27a9SDon Lewis 
1676831d27a9SDon Lewis /*
1677831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1678831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1679831d27a9SDon Lewis  */
1680831d27a9SDon Lewis void
16812c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
1682831d27a9SDon Lewis 	struct sigio *sigio;
16832c42a146SMarcel Moolenaar 	int sig, checkctty;
1684831d27a9SDon Lewis {
1685831d27a9SDon Lewis 	if (sigio == NULL)
1686831d27a9SDon Lewis 		return;
1687831d27a9SDon Lewis 
1688831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1689831d27a9SDon Lewis 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1690831d27a9SDon Lewis 		             sigio->sio_proc))
16912c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
1692831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1693831d27a9SDon Lewis 		struct proc *p;
1694831d27a9SDon Lewis 
16952e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1696831d27a9SDon Lewis 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1697831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
16982c42a146SMarcel Moolenaar 				psignal(p, sig);
1699831d27a9SDon Lewis 	}
1700831d27a9SDon Lewis }
1701cb679c38SJonathan Lemon 
1702cb679c38SJonathan Lemon static int
1703cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
1704cb679c38SJonathan Lemon {
1705cb679c38SJonathan Lemon 	struct proc *p = curproc;
1706cb679c38SJonathan Lemon 
1707cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
1708cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1709cb679c38SJonathan Lemon 
1710cb679c38SJonathan Lemon 	/* XXX lock the proc here while adding to the list? */
1711cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1712cb679c38SJonathan Lemon 
1713cb679c38SJonathan Lemon 	return (0);
1714cb679c38SJonathan Lemon }
1715cb679c38SJonathan Lemon 
1716cb679c38SJonathan Lemon static void
1717cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
1718cb679c38SJonathan Lemon {
1719cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
1720cb679c38SJonathan Lemon 
1721cb679c38SJonathan Lemon 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1722cb679c38SJonathan Lemon }
1723cb679c38SJonathan Lemon 
1724cb679c38SJonathan Lemon /*
1725cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
1726cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
1727cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
1728cb679c38SJonathan Lemon  * isn't worth the trouble.
1729cb679c38SJonathan Lemon  */
1730cb679c38SJonathan Lemon static int
1731cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
1732cb679c38SJonathan Lemon {
1733cb679c38SJonathan Lemon 
1734cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
1735cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
1736cb679c38SJonathan Lemon 
1737cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
1738cb679c38SJonathan Lemon 			kn->kn_data++;
1739cb679c38SJonathan Lemon 	}
1740cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
1741cb679c38SJonathan Lemon }
1742