xref: /freebsd/sys/kern/kern_sig.c (revision 2e3c8fcbd03b568879cf99db149e31002841cb8c)
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>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
532a024a2bSSean Eric Fagan #include <sys/pioctl.h>
54df8bae1dSRodney W. Grimes #include <sys/systm.h>
55df8bae1dSRodney W. Grimes #include <sys/acct.h>
563ac4d1efSBruce Evans #include <sys/fcntl.h>
57df8bae1dSRodney W. Grimes #include <sys/wait.h>
58df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
59df8bae1dSRodney W. Grimes #include <sys/syslog.h>
60df8bae1dSRodney W. Grimes #include <sys/stat.h>
61d66a5066SPeter Wemm #include <sys/sysent.h>
62c87e2930SDavid Greenman #include <sys/sysctl.h>
63c5edb423SSean Eric Fagan #include <sys/malloc.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <machine/cpu.h>
663163861cSTor Egge #ifdef SMP
673163861cSTor Egge #include <machine/smp.h>
683163861cSTor Egge #endif
69df8bae1dSRodney W. Grimes 
706f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
716f841fb7SMarcel Moolenaar 
726f841fb7SMarcel Moolenaar static int coredump	__P((struct proc *));
732c42a146SMarcel Moolenaar static int do_sigaction	__P((struct proc *p, int sig, struct sigaction *act,
74645682fdSLuoqi Chen 			     struct sigaction *oact, int old));
752c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
76645682fdSLuoqi Chen 			       sigset_t *oset, int old));
77f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t));
786f841fb7SMarcel Moolenaar static int killpg1	__P((struct proc *cp, int sig, int pgid, int all));
796f841fb7SMarcel Moolenaar static int sig_ffs	__P((sigset_t *set));
806f841fb7SMarcel Moolenaar static int sigprop	__P((int sig));
816f841fb7SMarcel Moolenaar static void stop	__P((struct proc *));
8226f9a767SRodney W. Grimes 
8357308494SJoerg Wunsch static int	kern_logsigexit = 1;
843d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
853d177f46SBill Fumerola     &kern_logsigexit, 0,
863d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
8757308494SJoerg Wunsch 
88df8bae1dSRodney W. Grimes /*
892c42a146SMarcel Moolenaar  * Can process p, with pcred pc, send the signal sig to process q?
90df8bae1dSRodney W. Grimes  */
912c42a146SMarcel Moolenaar #define CANSIGNAL(p, pc, q, sig) \
9275c13541SPoul-Henning Kamp 	(PRISON_CHECK(p, q) && ((pc)->pc_ucred->cr_uid == 0 || \
93df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
94df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
95df8bae1dSRodney W. Grimes 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
96df8bae1dSRodney W. Grimes 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
972c42a146SMarcel Moolenaar 	    ((sig) == SIGCONT && (q)->p_session == (p)->p_session)))
98df8bae1dSRodney W. Grimes 
99831d27a9SDon Lewis /*
100831d27a9SDon Lewis  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
101831d27a9SDon Lewis  */
102831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \
103831d27a9SDon Lewis 	((uc)->cr_uid == 0 || \
104831d27a9SDon Lewis 	    (ruid) == (q)->p_cred->p_ruid || \
105831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_cred->p_ruid || \
106831d27a9SDon Lewis 	    (ruid) == (q)->p_ucred->cr_uid || \
107831d27a9SDon Lewis 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
108831d27a9SDon Lewis 
10922d4b0fbSJohn Polstra int sugid_coredump;
1103d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1113d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
112c87e2930SDavid Greenman 
1132c42a146SMarcel Moolenaar /*
1142c42a146SMarcel Moolenaar  * Signal properties and actions.
1152c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1162c42a146SMarcel Moolenaar  * according to the following properties:
1172c42a146SMarcel Moolenaar  */
1182c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1192c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1202c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1212c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1222c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1232c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1242c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
125df8bae1dSRodney W. Grimes 
1262c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1272c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1282c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1292c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1302c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1312c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1322c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1332c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1342c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1352c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1362c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1372c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1382c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1392c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1402c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1412c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1422c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1432c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1442c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1452c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1462c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1472c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1482c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1492c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1502c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1512c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1522c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1532c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1542c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1552c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1562c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1572c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1582c42a146SMarcel Moolenaar };
1592c42a146SMarcel Moolenaar 
1606f841fb7SMarcel Moolenaar static __inline int
1616f841fb7SMarcel Moolenaar sigprop(int sig)
1622c42a146SMarcel Moolenaar {
1636f841fb7SMarcel Moolenaar 
1642c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
1652c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
1662c42a146SMarcel Moolenaar 	return (0);
167df8bae1dSRodney W. Grimes }
1682c42a146SMarcel Moolenaar 
1696f841fb7SMarcel Moolenaar static __inline int
1706f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
1712c42a146SMarcel Moolenaar {
1722c42a146SMarcel Moolenaar 	int i;
1732c42a146SMarcel Moolenaar 
1746f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
1752c42a146SMarcel Moolenaar 		if (set->__bits[i])
1762c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
177df8bae1dSRodney W. Grimes 	return (0);
178df8bae1dSRodney W. Grimes }
179df8bae1dSRodney W. Grimes 
1802c42a146SMarcel Moolenaar /*
1812c42a146SMarcel Moolenaar  * do_sigaction
1822c42a146SMarcel Moolenaar  * sigaction
1832c42a146SMarcel Moolenaar  * osigaction
1842c42a146SMarcel Moolenaar  */
1852c42a146SMarcel Moolenaar static int
186645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old)
1872c42a146SMarcel Moolenaar 	struct proc *p;
1882c42a146SMarcel Moolenaar 	register int sig;
1892c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
190645682fdSLuoqi Chen 	int old;
191df8bae1dSRodney W. Grimes {
192df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
193df8bae1dSRodney W. Grimes 
1942c42a146SMarcel Moolenaar 	if (sig <= 0 || sig > _SIG_MAXSIG)
1952c42a146SMarcel Moolenaar 		return (EINVAL);
1962c42a146SMarcel Moolenaar 
1972c42a146SMarcel Moolenaar 	if (oact) {
1982c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
1992c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2002c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2012c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2022c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2032c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2042c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2052c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2062c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2072c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2082c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2092c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2102c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
211645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2122c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
213645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2142c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2152c42a146SMarcel Moolenaar 	}
2162c42a146SMarcel Moolenaar 	if (act) {
2172c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
2182c42a146SMarcel Moolenaar 		    act->sa_handler != SIG_DFL)
2192c42a146SMarcel Moolenaar 			return (EINVAL);
2202c42a146SMarcel Moolenaar 
221df8bae1dSRodney W. Grimes 		/*
222df8bae1dSRodney W. Grimes 		 * Change setting atomically.
223df8bae1dSRodney W. Grimes 		 */
224df8bae1dSRodney W. Grimes 		(void) splhigh();
2252c42a146SMarcel Moolenaar 
2262c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2272c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2282c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
2292c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2302c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_siginfo, sig);
2316f841fb7SMarcel Moolenaar 		} else {
2322c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] =
2332c42a146SMarcel Moolenaar 			    (__sighandler_t *)act->sa_sigaction;
2342c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2352c42a146SMarcel Moolenaar 		}
2362c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2372c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
238df8bae1dSRodney W. Grimes 		else
2392c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2402c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2412c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
242df8bae1dSRodney W. Grimes 		else
2432c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
2442c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
2452c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
2461e41c1b5SSteven Wallace 		else
2472c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
2482c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
2492c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
250289ccde0SPeter Wemm 		else
2512c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
252df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
2532c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
2542c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
255df8bae1dSRodney W. Grimes 		else
2562c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_usertramp, seg);
257df8bae1dSRodney W. Grimes #endif
2582c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
2592c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
260645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
2616626c604SJulian Elischer 			else
262645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
2632c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDWAIT) {
264245f17d4SJoerg Wunsch 				/*
2652c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
2662c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
2672c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
2682c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
269245f17d4SJoerg Wunsch 				 */
270245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
271645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
2726626c604SJulian Elischer 				else
273645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
274245f17d4SJoerg Wunsch 			} else
275645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
276df8bae1dSRodney W. Grimes 		}
277df8bae1dSRodney W. Grimes 		/*
278df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
2792c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
2802c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
2812c42a146SMarcel Moolenaar 		 * have to restart the process.
282df8bae1dSRodney W. Grimes 		 */
2832c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
2842c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
2852c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
2862c42a146SMarcel Moolenaar 			/* never to be seen again */
2872c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
2882c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
2892c42a146SMarcel Moolenaar 				/* easier in psignal */
2902c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
2912c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
292645682fdSLuoqi Chen 		} else {
2932c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
2942c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
2952c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
2962c42a146SMarcel Moolenaar 			else
2972c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
2982c42a146SMarcel Moolenaar 		}
299645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
300645682fdSLuoqi Chen 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
301645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
302645682fdSLuoqi Chen 		else
303645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
3042c42a146SMarcel Moolenaar 
305df8bae1dSRodney W. Grimes 		(void) spl0();
306df8bae1dSRodney W. Grimes 	}
3072c42a146SMarcel Moolenaar 	return (0);
3082c42a146SMarcel Moolenaar }
3092c42a146SMarcel Moolenaar 
3102c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3112c42a146SMarcel Moolenaar struct sigaction_args {
3122c42a146SMarcel Moolenaar 	int	sig;
3132c42a146SMarcel Moolenaar 	struct	sigaction *act;
3142c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3152c42a146SMarcel Moolenaar };
3162c42a146SMarcel Moolenaar #endif
3172c42a146SMarcel Moolenaar /* ARGSUSED */
3182c42a146SMarcel Moolenaar int
3192c42a146SMarcel Moolenaar sigaction(p, uap)
3202c42a146SMarcel Moolenaar 	struct proc *p;
3212c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3222c42a146SMarcel Moolenaar {
3232c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3242c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3252c42a146SMarcel Moolenaar 	int error;
3262c42a146SMarcel Moolenaar 
3276f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
3286f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
3292c42a146SMarcel Moolenaar 	if (actp) {
3306f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
3312c42a146SMarcel Moolenaar 		if (error)
3322c42a146SMarcel Moolenaar 			return (error);
3332c42a146SMarcel Moolenaar 	}
334645682fdSLuoqi Chen 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
3352c42a146SMarcel Moolenaar 	if (oactp && !error) {
3366f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
3372c42a146SMarcel Moolenaar 	}
3382c42a146SMarcel Moolenaar 	return (error);
3392c42a146SMarcel Moolenaar }
3402c42a146SMarcel Moolenaar 
3412c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3422c42a146SMarcel Moolenaar struct osigaction_args {
3432c42a146SMarcel Moolenaar 	int	signum;
3442c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
3452c42a146SMarcel Moolenaar 	struct	osigaction *osa;
3462c42a146SMarcel Moolenaar };
3472c42a146SMarcel Moolenaar #endif
3482c42a146SMarcel Moolenaar /* ARGSUSED */
3492c42a146SMarcel Moolenaar int
3502c42a146SMarcel Moolenaar osigaction(p, uap)
3512c42a146SMarcel Moolenaar 	struct proc *p;
3522c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
3532c42a146SMarcel Moolenaar {
3542c42a146SMarcel Moolenaar 	struct osigaction sa;
3552c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
3562c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
3572c42a146SMarcel Moolenaar 	int error;
3582c42a146SMarcel Moolenaar 
3596f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
3606f841fb7SMarcel Moolenaar 		return (EINVAL);
3616f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
3626f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
3632c42a146SMarcel Moolenaar 	if (nsap) {
3646f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
3652c42a146SMarcel Moolenaar 		if (error)
3662c42a146SMarcel Moolenaar 			return (error);
3672c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
3682c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
3692c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
3702c42a146SMarcel Moolenaar 	}
371645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
3722c42a146SMarcel Moolenaar 	if (osap && !error) {
3732c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
3742c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
3752c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
3766f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
3772c42a146SMarcel Moolenaar 	}
3782c42a146SMarcel Moolenaar 	return (error);
3792c42a146SMarcel Moolenaar }
380df8bae1dSRodney W. Grimes 
381df8bae1dSRodney W. Grimes /*
382df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
383df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
384df8bae1dSRodney W. Grimes  */
385df8bae1dSRodney W. Grimes void
386df8bae1dSRodney W. Grimes siginit(p)
387df8bae1dSRodney W. Grimes 	struct proc *p;
388df8bae1dSRodney W. Grimes {
389df8bae1dSRodney W. Grimes 	register int i;
390df8bae1dSRodney W. Grimes 
3912c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
3922c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
3932c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
394df8bae1dSRodney W. Grimes }
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes /*
397df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
398df8bae1dSRodney W. Grimes  */
399df8bae1dSRodney W. Grimes void
400df8bae1dSRodney W. Grimes execsigs(p)
401df8bae1dSRodney W. Grimes 	register struct proc *p;
402df8bae1dSRodney W. Grimes {
403df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
4042c42a146SMarcel Moolenaar 	register int sig;
405df8bae1dSRodney W. Grimes 
406df8bae1dSRodney W. Grimes 	/*
407df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
408df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
409df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
410df8bae1dSRodney W. Grimes 	 */
4112c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
4122c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
4132c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
4142c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
4152c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
4162c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
4172c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
418df8bae1dSRodney W. Grimes 		}
4192c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
420df8bae1dSRodney W. Grimes 	}
421df8bae1dSRodney W. Grimes 	/*
422df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
423df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
424df8bae1dSRodney W. Grimes 	 */
425645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
426645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
427645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
42880e907a1SPeter Wemm 	/*
42980e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
43080e907a1SPeter Wemm 	 */
431645682fdSLuoqi Chen 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
432df8bae1dSRodney W. Grimes }
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes /*
435df8bae1dSRodney W. Grimes  * Manipulate signal mask.
436df8bae1dSRodney W. Grimes  * Note that we receive new mask, not pointer,
437df8bae1dSRodney W. Grimes  * and return old mask as return value;
438df8bae1dSRodney W. Grimes  * the library stub does the rest.
439df8bae1dSRodney W. Grimes  */
4402c42a146SMarcel Moolenaar static int
441645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
4422c42a146SMarcel Moolenaar 	struct proc *p;
4432c42a146SMarcel Moolenaar 	int how;
4442c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
445645682fdSLuoqi Chen 	int old;
4462c42a146SMarcel Moolenaar {
4472c42a146SMarcel Moolenaar 	int error;
4482c42a146SMarcel Moolenaar 
4492c42a146SMarcel Moolenaar 	if (oset != NULL)
4502c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
4512c42a146SMarcel Moolenaar 
4522c42a146SMarcel Moolenaar 	error = 0;
4532c42a146SMarcel Moolenaar 	if (set != NULL) {
4542c42a146SMarcel Moolenaar 		(void) splhigh();
4552c42a146SMarcel Moolenaar 		switch (how) {
4562c42a146SMarcel Moolenaar 		case SIG_BLOCK:
457645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
4582c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
4592c42a146SMarcel Moolenaar 			break;
4602c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
4612c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
4622c42a146SMarcel Moolenaar 			break;
4632c42a146SMarcel Moolenaar 		case SIG_SETMASK:
464645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
465645682fdSLuoqi Chen 			if (old)
466645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
467645682fdSLuoqi Chen 			else
4682c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
4692c42a146SMarcel Moolenaar 			break;
4702c42a146SMarcel Moolenaar 		default:
4712c42a146SMarcel Moolenaar 			error = EINVAL;
4722c42a146SMarcel Moolenaar 			break;
4732c42a146SMarcel Moolenaar 		}
4742c42a146SMarcel Moolenaar 		(void) spl0();
4752c42a146SMarcel Moolenaar 	}
4762c42a146SMarcel Moolenaar 	return (error);
4772c42a146SMarcel Moolenaar }
4782c42a146SMarcel Moolenaar 
479d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
480df8bae1dSRodney W. Grimes struct sigprocmask_args {
481df8bae1dSRodney W. Grimes 	int	how;
4822c42a146SMarcel Moolenaar 	const sigset_t *set;
4832c42a146SMarcel Moolenaar 	sigset_t *oset;
484df8bae1dSRodney W. Grimes };
485d2d3e875SBruce Evans #endif
48626f9a767SRodney W. Grimes int
487cb226aaaSPoul-Henning Kamp sigprocmask(p, uap)
488df8bae1dSRodney W. Grimes 	register struct proc *p;
489df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
490df8bae1dSRodney W. Grimes {
4912c42a146SMarcel Moolenaar 	sigset_t set, oset;
4922c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
4932c42a146SMarcel Moolenaar 	int error;
494df8bae1dSRodney W. Grimes 
4956f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
4966f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
4972c42a146SMarcel Moolenaar 	if (setp) {
4986f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
4992c42a146SMarcel Moolenaar 		if (error)
5002c42a146SMarcel Moolenaar 			return (error);
501df8bae1dSRodney W. Grimes 	}
502645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
5032c42a146SMarcel Moolenaar 	if (osetp && !error) {
5046f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
5052c42a146SMarcel Moolenaar 	}
5062c42a146SMarcel Moolenaar 	return (error);
5072c42a146SMarcel Moolenaar }
5082c42a146SMarcel Moolenaar 
5092c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5102c42a146SMarcel Moolenaar struct osigprocmask_args {
5112c42a146SMarcel Moolenaar 	int	how;
5122c42a146SMarcel Moolenaar 	osigset_t mask;
5132c42a146SMarcel Moolenaar };
5142c42a146SMarcel Moolenaar #endif
5152c42a146SMarcel Moolenaar int
5162c42a146SMarcel Moolenaar osigprocmask(p, uap)
5172c42a146SMarcel Moolenaar 	register struct proc *p;
5182c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
5192c42a146SMarcel Moolenaar {
5202c42a146SMarcel Moolenaar 	sigset_t set, oset;
5212c42a146SMarcel Moolenaar 	int error;
5222c42a146SMarcel Moolenaar 
5232c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
524645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
5252c42a146SMarcel Moolenaar 	SIG2OSIG(oset, p->p_retval[0]);
526df8bae1dSRodney W. Grimes 	return (error);
527df8bae1dSRodney W. Grimes }
528df8bae1dSRodney W. Grimes 
529d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
530df8bae1dSRodney W. Grimes struct sigpending_args {
5312c42a146SMarcel Moolenaar 	sigset_t	*set;
532df8bae1dSRodney W. Grimes };
533d2d3e875SBruce Evans #endif
534df8bae1dSRodney W. Grimes /* ARGSUSED */
53526f9a767SRodney W. Grimes int
536cb226aaaSPoul-Henning Kamp sigpending(p, uap)
537df8bae1dSRodney W. Grimes 	struct proc *p;
538df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
539df8bae1dSRodney W. Grimes {
540df8bae1dSRodney W. Grimes 
5416f841fb7SMarcel Moolenaar 	return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
5422c42a146SMarcel Moolenaar }
5432c42a146SMarcel Moolenaar 
5442c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
5452c42a146SMarcel Moolenaar struct osigpending_args {
5462c42a146SMarcel Moolenaar 	int	dummy;
5472c42a146SMarcel Moolenaar };
5482c42a146SMarcel Moolenaar #endif
5492c42a146SMarcel Moolenaar /* ARGSUSED */
5502c42a146SMarcel Moolenaar int
5512c42a146SMarcel Moolenaar osigpending(p, uap)
5522c42a146SMarcel Moolenaar 	struct proc *p;
5532c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
5542c42a146SMarcel Moolenaar {
5552c42a146SMarcel Moolenaar 
5562c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_siglist, p->p_retval[0]);
557df8bae1dSRodney W. Grimes 	return (0);
558df8bae1dSRodney W. Grimes }
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
561df8bae1dSRodney W. Grimes /*
562df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
563df8bae1dSRodney W. Grimes  */
564d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
565df8bae1dSRodney W. Grimes struct osigvec_args {
566df8bae1dSRodney W. Grimes 	int	signum;
567df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
568df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
569df8bae1dSRodney W. Grimes };
570d2d3e875SBruce Evans #endif
571df8bae1dSRodney W. Grimes /* ARGSUSED */
57226f9a767SRodney W. Grimes int
573cb226aaaSPoul-Henning Kamp osigvec(p, uap)
574df8bae1dSRodney W. Grimes 	struct proc *p;
575df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
576df8bae1dSRodney W. Grimes {
577df8bae1dSRodney W. Grimes 	struct sigvec vec;
5782c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
5792c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
5802c42a146SMarcel Moolenaar 	int error;
581df8bae1dSRodney W. Grimes 
5826f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
5836f841fb7SMarcel Moolenaar 		return (EINVAL);
5846f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
5856f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
5862c42a146SMarcel Moolenaar 	if (nsap) {
5876f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
5882c42a146SMarcel Moolenaar 		if (error)
589df8bae1dSRodney W. Grimes 			return (error);
5902c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
5912c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
5922c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
5932c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
594df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
5952c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
596df8bae1dSRodney W. Grimes #endif
597df8bae1dSRodney W. Grimes 	}
598645682fdSLuoqi Chen 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
5992c42a146SMarcel Moolenaar 	if (osap && !error) {
6002c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
6012c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
6022c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
6032c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
6042c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
6052c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
6062c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
6072c42a146SMarcel Moolenaar #endif
6086f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
6092c42a146SMarcel Moolenaar 	}
6102c42a146SMarcel Moolenaar 	return (error);
611df8bae1dSRodney W. Grimes }
612df8bae1dSRodney W. Grimes 
613d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
614df8bae1dSRodney W. Grimes struct osigblock_args {
615df8bae1dSRodney W. Grimes 	int	mask;
616df8bae1dSRodney W. Grimes };
617d2d3e875SBruce Evans #endif
61826f9a767SRodney W. Grimes int
619cb226aaaSPoul-Henning Kamp osigblock(p, uap)
620df8bae1dSRodney W. Grimes 	register struct proc *p;
621df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
622df8bae1dSRodney W. Grimes {
6232c42a146SMarcel Moolenaar 	sigset_t set;
624df8bae1dSRodney W. Grimes 
6252c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6262c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
627df8bae1dSRodney W. Grimes 	(void) splhigh();
6282c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
6292c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
630df8bae1dSRodney W. Grimes 	(void) spl0();
631df8bae1dSRodney W. Grimes 	return (0);
632df8bae1dSRodney W. Grimes }
633df8bae1dSRodney W. Grimes 
634d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
635df8bae1dSRodney W. Grimes struct osigsetmask_args {
636df8bae1dSRodney W. Grimes 	int	mask;
637df8bae1dSRodney W. Grimes };
638d2d3e875SBruce Evans #endif
63926f9a767SRodney W. Grimes int
640cb226aaaSPoul-Henning Kamp osigsetmask(p, uap)
641df8bae1dSRodney W. Grimes 	struct proc *p;
642df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
643df8bae1dSRodney W. Grimes {
6442c42a146SMarcel Moolenaar 	sigset_t set;
645df8bae1dSRodney W. Grimes 
6462c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
6472c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
648df8bae1dSRodney W. Grimes 	(void) splhigh();
6492c42a146SMarcel Moolenaar 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
650645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
651df8bae1dSRodney W. Grimes 	(void) spl0();
652df8bae1dSRodney W. Grimes 	return (0);
653df8bae1dSRodney W. Grimes }
654df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
655df8bae1dSRodney W. Grimes 
656df8bae1dSRodney W. Grimes /*
657df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
658df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
659df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
660df8bae1dSRodney W. Grimes  */
661d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
662df8bae1dSRodney W. Grimes struct sigsuspend_args {
6632c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
664df8bae1dSRodney W. Grimes };
665d2d3e875SBruce Evans #endif
666df8bae1dSRodney W. Grimes /* ARGSUSED */
66726f9a767SRodney W. Grimes int
668cb226aaaSPoul-Henning Kamp sigsuspend(p, uap)
669df8bae1dSRodney W. Grimes 	register struct proc *p;
670df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
671df8bae1dSRodney W. Grimes {
6722c42a146SMarcel Moolenaar 	sigset_t mask;
673df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
6742c42a146SMarcel Moolenaar 	int error;
6752c42a146SMarcel Moolenaar 
6766f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
6772c42a146SMarcel Moolenaar 	if (error)
6782c42a146SMarcel Moolenaar 		return (error);
679df8bae1dSRodney W. Grimes 
680df8bae1dSRodney W. Grimes 	/*
681645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
682df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
683df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
684df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
685df8bae1dSRodney W. Grimes 	 * to indicate this.
686df8bae1dSRodney W. Grimes 	 */
6876626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
688645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
689645682fdSLuoqi Chen 
690645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
6912c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
6922c42a146SMarcel Moolenaar 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
6932c42a146SMarcel Moolenaar 		/* void */;
6942c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
6952c42a146SMarcel Moolenaar 	return (EINTR);
6962c42a146SMarcel Moolenaar }
6972c42a146SMarcel Moolenaar 
6982c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6992c42a146SMarcel Moolenaar struct osigsuspend_args {
7002c42a146SMarcel Moolenaar 	osigset_t mask;
7012c42a146SMarcel Moolenaar };
7022c42a146SMarcel Moolenaar #endif
7032c42a146SMarcel Moolenaar /* ARGSUSED */
7042c42a146SMarcel Moolenaar int
7052c42a146SMarcel Moolenaar osigsuspend(p, uap)
7062c42a146SMarcel Moolenaar 	register struct proc *p;
7072c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
7082c42a146SMarcel Moolenaar {
709645682fdSLuoqi Chen 	sigset_t mask;
7102c42a146SMarcel Moolenaar 	register struct sigacts *ps = p->p_sigacts;
7112c42a146SMarcel Moolenaar 
7122c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
713645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
714645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
715645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
716645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
717645682fdSLuoqi Chen 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
718df8bae1dSRodney W. Grimes 		/* void */;
719df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
720df8bae1dSRodney W. Grimes 	return (EINTR);
721df8bae1dSRodney W. Grimes }
722df8bae1dSRodney W. Grimes 
723df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
724d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
725df8bae1dSRodney W. Grimes struct osigstack_args {
726df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
727df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
728df8bae1dSRodney W. Grimes };
729d2d3e875SBruce Evans #endif
730df8bae1dSRodney W. Grimes /* ARGSUSED */
73126f9a767SRodney W. Grimes int
732cb226aaaSPoul-Henning Kamp osigstack(p, uap)
733df8bae1dSRodney W. Grimes 	struct proc *p;
734df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
735df8bae1dSRodney W. Grimes {
736df8bae1dSRodney W. Grimes 	struct sigstack ss;
737df8bae1dSRodney W. Grimes 	int error = 0;
738df8bae1dSRodney W. Grimes 
739645682fdSLuoqi Chen 	ss.ss_sp = p->p_sigstk.ss_sp;
740645682fdSLuoqi Chen 	ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
7416f841fb7SMarcel Moolenaar 	if (uap->oss && (error = copyout(&ss, uap->oss,
742df8bae1dSRodney W. Grimes 	    sizeof(struct sigstack))))
743df8bae1dSRodney W. Grimes 		return (error);
7446f841fb7SMarcel Moolenaar 	if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
745645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
746645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
747645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
748645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
749df8bae1dSRodney W. Grimes 	}
750df8bae1dSRodney W. Grimes 	return (error);
751df8bae1dSRodney W. Grimes }
752df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
753df8bae1dSRodney W. Grimes 
754d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
755df8bae1dSRodney W. Grimes struct sigaltstack_args {
7562c42a146SMarcel Moolenaar 	stack_t	*ss;
7572c42a146SMarcel Moolenaar 	stack_t	*oss;
758df8bae1dSRodney W. Grimes };
759d2d3e875SBruce Evans #endif
760df8bae1dSRodney W. Grimes /* ARGSUSED */
76126f9a767SRodney W. Grimes int
762cb226aaaSPoul-Henning Kamp sigaltstack(p, uap)
763df8bae1dSRodney W. Grimes 	struct proc *p;
764df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
765df8bae1dSRodney W. Grimes {
7662c42a146SMarcel Moolenaar 	stack_t ss;
767df8bae1dSRodney W. Grimes 	int error;
768df8bae1dSRodney W. Grimes 
769645682fdSLuoqi Chen 	if ((p->p_flag & P_ALTSTACK) == 0)
770645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= SS_DISABLE;
7716f841fb7SMarcel Moolenaar 	if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
7726f841fb7SMarcel Moolenaar 	    sizeof(stack_t))))
773df8bae1dSRodney W. Grimes 		return (error);
7742c42a146SMarcel Moolenaar 	if (uap->ss == 0)
775df8bae1dSRodney W. Grimes 		return (0);
7766f841fb7SMarcel Moolenaar 	if ((error = copyin(uap->ss, &ss, sizeof(ss))))
777df8bae1dSRodney W. Grimes 		return (error);
778d66a5066SPeter Wemm 	if (ss.ss_flags & SS_DISABLE) {
779645682fdSLuoqi Chen 		if (p->p_sigstk.ss_flags & SS_ONSTACK)
780df8bae1dSRodney W. Grimes 			return (EINVAL);
781645682fdSLuoqi Chen 		p->p_flag &= ~P_ALTSTACK;
782645682fdSLuoqi Chen 		p->p_sigstk.ss_flags = ss.ss_flags;
783df8bae1dSRodney W. Grimes 		return (0);
784df8bae1dSRodney W. Grimes 	}
785df8bae1dSRodney W. Grimes 	if (ss.ss_size < MINSIGSTKSZ)
786df8bae1dSRodney W. Grimes 		return (ENOMEM);
787645682fdSLuoqi Chen 	p->p_flag |= P_ALTSTACK;
788645682fdSLuoqi Chen 	p->p_sigstk = ss;
789df8bae1dSRodney W. Grimes 	return (0);
790df8bae1dSRodney W. Grimes }
791df8bae1dSRodney W. Grimes 
792d93f860cSPoul-Henning Kamp /*
793d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
794d93f860cSPoul-Henning Kamp  * cp is calling process.
795d93f860cSPoul-Henning Kamp  */
796d93f860cSPoul-Henning Kamp int
7972c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all)
798d93f860cSPoul-Henning Kamp 	register struct proc *cp;
7992c42a146SMarcel Moolenaar 	int sig, pgid, all;
800d93f860cSPoul-Henning Kamp {
801d93f860cSPoul-Henning Kamp 	register struct proc *p;
802d93f860cSPoul-Henning Kamp 	register struct pcred *pc = cp->p_cred;
803d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
804d93f860cSPoul-Henning Kamp 	int nfound = 0;
805d93f860cSPoul-Henning Kamp 
806d93f860cSPoul-Henning Kamp 	if (all)
807d93f860cSPoul-Henning Kamp 		/*
808d93f860cSPoul-Henning Kamp 		 * broadcast
809d93f860cSPoul-Henning Kamp 		 */
8102e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
811d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
8122c42a146SMarcel Moolenaar 			    p == cp || !CANSIGNAL(cp, pc, p, sig))
813d93f860cSPoul-Henning Kamp 				continue;
814d93f860cSPoul-Henning Kamp 			nfound++;
8152c42a146SMarcel Moolenaar 			if (sig)
8162c42a146SMarcel Moolenaar 				psignal(p, sig);
817d93f860cSPoul-Henning Kamp 		}
818d93f860cSPoul-Henning Kamp 	else {
819d93f860cSPoul-Henning Kamp 		if (pgid == 0)
820d93f860cSPoul-Henning Kamp 			/*
821d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
822d93f860cSPoul-Henning Kamp 			 */
823d93f860cSPoul-Henning Kamp 			pgrp = cp->p_pgrp;
824d93f860cSPoul-Henning Kamp 		else {
825d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
826d93f860cSPoul-Henning Kamp 			if (pgrp == NULL)
827d93f860cSPoul-Henning Kamp 				return (ESRCH);
828d93f860cSPoul-Henning Kamp 		}
8292e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
830d93f860cSPoul-Henning Kamp 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
831d93f860cSPoul-Henning Kamp 			    p->p_stat == SZOMB ||
8322c42a146SMarcel Moolenaar 			    !CANSIGNAL(cp, pc, p, sig))
833d93f860cSPoul-Henning Kamp 				continue;
834d93f860cSPoul-Henning Kamp 			nfound++;
8352c42a146SMarcel Moolenaar 			if (sig)
8362c42a146SMarcel Moolenaar 				psignal(p, sig);
837d93f860cSPoul-Henning Kamp 		}
838d93f860cSPoul-Henning Kamp 	}
839d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
840d93f860cSPoul-Henning Kamp }
841d93f860cSPoul-Henning Kamp 
842d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
843df8bae1dSRodney W. Grimes struct kill_args {
844df8bae1dSRodney W. Grimes 	int	pid;
845df8bae1dSRodney W. Grimes 	int	signum;
846df8bae1dSRodney W. Grimes };
847d2d3e875SBruce Evans #endif
848df8bae1dSRodney W. Grimes /* ARGSUSED */
84926f9a767SRodney W. Grimes int
850cb226aaaSPoul-Henning Kamp kill(cp, uap)
851df8bae1dSRodney W. Grimes 	register struct proc *cp;
852df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
853df8bae1dSRodney W. Grimes {
854df8bae1dSRodney W. Grimes 	register struct proc *p;
855df8bae1dSRodney W. Grimes 	register struct pcred *pc = cp->p_cred;
856df8bae1dSRodney W. Grimes 
8572c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
858df8bae1dSRodney W. Grimes 		return (EINVAL);
859df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
860df8bae1dSRodney W. Grimes 		/* kill single process */
861df8bae1dSRodney W. Grimes 		if ((p = pfind(uap->pid)) == NULL)
862df8bae1dSRodney W. Grimes 			return (ESRCH);
863df8bae1dSRodney W. Grimes 		if (!CANSIGNAL(cp, pc, p, uap->signum))
864df8bae1dSRodney W. Grimes 			return (EPERM);
865df8bae1dSRodney W. Grimes 		if (uap->signum)
866df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
867df8bae1dSRodney W. Grimes 		return (0);
868df8bae1dSRodney W. Grimes 	}
869df8bae1dSRodney W. Grimes 	switch (uap->pid) {
870df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
871df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 1));
872df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
873df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, 0, 0));
874df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
875df8bae1dSRodney W. Grimes 		return (killpg1(cp, uap->signum, -uap->pid, 0));
876df8bae1dSRodney W. Grimes 	}
877df8bae1dSRodney W. Grimes 	/* NOTREACHED */
878df8bae1dSRodney W. Grimes }
879df8bae1dSRodney W. Grimes 
880df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
881d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
882df8bae1dSRodney W. Grimes struct okillpg_args {
883df8bae1dSRodney W. Grimes 	int	pgid;
884df8bae1dSRodney W. Grimes 	int	signum;
885df8bae1dSRodney W. Grimes };
886d2d3e875SBruce Evans #endif
887df8bae1dSRodney W. Grimes /* ARGSUSED */
88826f9a767SRodney W. Grimes int
889cb226aaaSPoul-Henning Kamp okillpg(p, uap)
890df8bae1dSRodney W. Grimes 	struct proc *p;
891df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
892df8bae1dSRodney W. Grimes {
893df8bae1dSRodney W. Grimes 
8942c42a146SMarcel Moolenaar 	if ((u_int)uap->signum > _SIG_MAXSIG)
895df8bae1dSRodney W. Grimes 		return (EINVAL);
896df8bae1dSRodney W. Grimes 	return (killpg1(p, uap->signum, uap->pgid, 0));
897df8bae1dSRodney W. Grimes }
898df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
899df8bae1dSRodney W. Grimes 
900df8bae1dSRodney W. Grimes /*
901df8bae1dSRodney W. Grimes  * Send a signal to a process group.
902df8bae1dSRodney W. Grimes  */
903df8bae1dSRodney W. Grimes void
9042c42a146SMarcel Moolenaar gsignal(pgid, sig)
9052c42a146SMarcel Moolenaar 	int pgid, sig;
906df8bae1dSRodney W. Grimes {
907df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
908df8bae1dSRodney W. Grimes 
909df8bae1dSRodney W. Grimes 	if (pgid && (pgrp = pgfind(pgid)))
9102c42a146SMarcel Moolenaar 		pgsignal(pgrp, sig, 0);
911df8bae1dSRodney W. Grimes }
912df8bae1dSRodney W. Grimes 
913df8bae1dSRodney W. Grimes /*
914df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
915df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
916df8bae1dSRodney W. Grimes  */
917df8bae1dSRodney W. Grimes void
9182c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
919df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
9202c42a146SMarcel Moolenaar 	int sig, checkctty;
921df8bae1dSRodney W. Grimes {
922df8bae1dSRodney W. Grimes 	register struct proc *p;
923df8bae1dSRodney W. Grimes 
924df8bae1dSRodney W. Grimes 	if (pgrp)
9252e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
926df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
9272c42a146SMarcel Moolenaar 				psignal(p, sig);
928df8bae1dSRodney W. Grimes }
929df8bae1dSRodney W. Grimes 
930df8bae1dSRodney W. Grimes /*
931df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
932df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
933df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
934df8bae1dSRodney W. Grimes  */
935df8bae1dSRodney W. Grimes void
9362c42a146SMarcel Moolenaar trapsignal(p, sig, code)
937df8bae1dSRodney W. Grimes 	struct proc *p;
9382c42a146SMarcel Moolenaar 	register int sig;
9398674077aSJeffrey Hsu 	u_long code;
940df8bae1dSRodney W. Grimes {
941df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
942df8bae1dSRodney W. Grimes 
9432c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
9442c42a146SMarcel Moolenaar 	    SIGISMEMBER(p->p_sigmask, sig)) {
945df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
946df8bae1dSRodney W. Grimes #ifdef KTRACE
947df8bae1dSRodney W. Grimes 		if (KTRPOINT(p, KTR_PSIG))
9482c42a146SMarcel Moolenaar 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
9492c42a146SMarcel Moolenaar 				&p->p_sigmask, code);
950df8bae1dSRodney W. Grimes #endif
9512c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
9522c42a146SMarcel Moolenaar 						&p->p_sigmask, code);
9532c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
9542c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
9552c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
9562c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
957289ccde0SPeter Wemm 			/*
9582c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
959289ccde0SPeter Wemm 			 */
9602c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
9612c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
9622c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
9632c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
9642c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
965dedc04feSPeter Wemm 		}
9666f841fb7SMarcel Moolenaar 	} else {
9676626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
9682c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
9692c42a146SMarcel Moolenaar 		psignal(p, sig);
970df8bae1dSRodney W. Grimes 	}
971df8bae1dSRodney W. Grimes }
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes /*
974df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
975df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
976df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
977df8bae1dSRodney W. Grimes  *
978df8bae1dSRodney W. Grimes  * Exceptions:
979df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
980df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
981df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
982df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
983df8bae1dSRodney W. Grimes  *
984df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
985df8bae1dSRodney W. Grimes  */
986df8bae1dSRodney W. Grimes void
9872c42a146SMarcel Moolenaar psignal(p, sig)
988df8bae1dSRodney W. Grimes 	register struct proc *p;
9892c42a146SMarcel Moolenaar 	register int sig;
990df8bae1dSRodney W. Grimes {
991df8bae1dSRodney W. Grimes 	register int s, prop;
992df8bae1dSRodney W. Grimes 	register sig_t action;
993df8bae1dSRodney W. Grimes 
9942c42a146SMarcel Moolenaar 	if (sig > _SIG_MAXSIG || sig <= 0) {
9952c42a146SMarcel Moolenaar 		printf("psignal: signal %d\n", sig);
996df8bae1dSRodney W. Grimes 		panic("psignal signal number");
9972a024a2bSSean Eric Fagan 	}
9982c42a146SMarcel Moolenaar 
9992c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1000df8bae1dSRodney W. Grimes 
1001df8bae1dSRodney W. Grimes 	/*
10022a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
10032a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
10042a024a2bSSean Eric Fagan 	 * a chance, as well.
1005df8bae1dSRodney W. Grimes 	 */
10062a024a2bSSean Eric Fagan 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1007df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1008df8bae1dSRodney W. Grimes 	else {
1009df8bae1dSRodney W. Grimes 		/*
1010df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1011df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1012df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1013df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1014df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1015df8bae1dSRodney W. Grimes 		 */
10162c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1017df8bae1dSRodney W. Grimes 			return;
10182c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1019df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
10202c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1021df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1022df8bae1dSRodney W. Grimes 		else
1023df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1024df8bae1dSRodney W. Grimes 	}
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1027df8bae1dSRodney W. Grimes 	    (p->p_flag & P_TRACED) == 0)
1028df8bae1dSRodney W. Grimes 		p->p_nice = NZERO;
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
10312c42a146SMarcel Moolenaar 		SIG_STOPSIGMASK(p->p_siglist);
1032df8bae1dSRodney W. Grimes 
1033df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1034df8bae1dSRodney W. Grimes 		/*
1035df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1036df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1037df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1038df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1039df8bae1dSRodney W. Grimes 		 */
1040df8bae1dSRodney W. Grimes 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1041df8bae1dSRodney W. Grimes 		    action == SIG_DFL)
1042df8bae1dSRodney W. Grimes 		        return;
10432c42a146SMarcel Moolenaar 		SIG_CONTSIGMASK(p->p_siglist);
1044df8bae1dSRodney W. Grimes 	}
10452c42a146SMarcel Moolenaar 	SIGADDSET(p->p_siglist, sig);
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes 	/*
1048df8bae1dSRodney W. Grimes 	 * Defer further processing for signals which are held,
1049df8bae1dSRodney W. Grimes 	 * except that stopped processes must be continued by SIGCONT.
1050df8bae1dSRodney W. Grimes 	 */
10512c42a146SMarcel Moolenaar 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1052df8bae1dSRodney W. Grimes 		return;
1053df8bae1dSRodney W. Grimes 	s = splhigh();
1054df8bae1dSRodney W. Grimes 	switch (p->p_stat) {
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes 	case SSLEEP:
1057df8bae1dSRodney W. Grimes 		/*
1058df8bae1dSRodney W. Grimes 		 * If process is sleeping uninterruptibly
1059df8bae1dSRodney W. Grimes 		 * we can't interrupt the sleep... the signal will
1060df8bae1dSRodney W. Grimes 		 * be noticed when the process returns through
1061df8bae1dSRodney W. Grimes 		 * trap() or syscall().
1062df8bae1dSRodney W. Grimes 		 */
1063df8bae1dSRodney W. Grimes 		if ((p->p_flag & P_SINTR) == 0)
1064df8bae1dSRodney W. Grimes 			goto out;
1065df8bae1dSRodney W. Grimes 		/*
1066df8bae1dSRodney W. Grimes 		 * Process is sleeping and traced... make it runnable
1067df8bae1dSRodney W. Grimes 		 * so it can discover the signal in issignal() and stop
1068df8bae1dSRodney W. Grimes 		 * for the parent.
1069df8bae1dSRodney W. Grimes 		 */
1070df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1071df8bae1dSRodney W. Grimes 			goto run;
1072df8bae1dSRodney W. Grimes 		/*
1073df8bae1dSRodney W. Grimes 		 * If SIGCONT is default (or ignored) and process is
1074df8bae1dSRodney W. Grimes 		 * asleep, we are finished; the process should not
1075df8bae1dSRodney W. Grimes 		 * be awakened.
1076df8bae1dSRodney W. Grimes 		 */
1077df8bae1dSRodney W. Grimes 		if ((prop & SA_CONT) && action == SIG_DFL) {
10782c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1079df8bae1dSRodney W. Grimes 			goto out;
1080df8bae1dSRodney W. Grimes 		}
1081df8bae1dSRodney W. Grimes 		/*
1082df8bae1dSRodney W. Grimes 		 * When a sleeping process receives a stop
1083df8bae1dSRodney W. Grimes 		 * signal, process immediately if possible.
1084df8bae1dSRodney W. Grimes 		 * All other (caught or default) signals
1085df8bae1dSRodney W. Grimes 		 * cause the process to run.
1086df8bae1dSRodney W. Grimes 		 */
1087df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1088df8bae1dSRodney W. Grimes 			if (action != SIG_DFL)
1089df8bae1dSRodney W. Grimes 				goto runfast;
1090df8bae1dSRodney W. Grimes 			/*
1091df8bae1dSRodney W. Grimes 			 * If a child holding parent blocked,
1092df8bae1dSRodney W. Grimes 			 * stopping could cause deadlock.
1093df8bae1dSRodney W. Grimes 			 */
1094df8bae1dSRodney W. Grimes 			if (p->p_flag & P_PPWAIT)
1095df8bae1dSRodney W. Grimes 				goto out;
10962c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
10972c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1098645682fdSLuoqi Chen 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1099df8bae1dSRodney W. Grimes 				psignal(p->p_pptr, SIGCHLD);
1100df8bae1dSRodney W. Grimes 			stop(p);
1101df8bae1dSRodney W. Grimes 			goto out;
1102df8bae1dSRodney W. Grimes 		} else
1103df8bae1dSRodney W. Grimes 			goto runfast;
1104df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
1105df8bae1dSRodney W. Grimes 
1106df8bae1dSRodney W. Grimes 	case SSTOP:
1107df8bae1dSRodney W. Grimes 		/*
1108df8bae1dSRodney W. Grimes 		 * If traced process is already stopped,
1109df8bae1dSRodney W. Grimes 		 * then no further action is necessary.
1110df8bae1dSRodney W. Grimes 		 */
1111df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED)
1112df8bae1dSRodney W. Grimes 			goto out;
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes 		/*
1115df8bae1dSRodney W. Grimes 		 * Kill signal always sets processes running.
1116df8bae1dSRodney W. Grimes 		 */
11172c42a146SMarcel Moolenaar 		if (sig == SIGKILL)
1118df8bae1dSRodney W. Grimes 			goto runfast;
1119df8bae1dSRodney W. Grimes 
1120df8bae1dSRodney W. Grimes 		if (prop & SA_CONT) {
1121df8bae1dSRodney W. Grimes 			/*
1122df8bae1dSRodney W. Grimes 			 * If SIGCONT is default (or ignored), we continue the
1123df8bae1dSRodney W. Grimes 			 * process but don't leave the signal in p_siglist, as
1124df8bae1dSRodney W. Grimes 			 * it has no further action.  If SIGCONT is held, we
1125df8bae1dSRodney W. Grimes 			 * continue the process and leave the signal in
1126df8bae1dSRodney W. Grimes 			 * p_siglist.  If the process catches SIGCONT, let it
1127df8bae1dSRodney W. Grimes 			 * handle the signal itself.  If it isn't waiting on
1128df8bae1dSRodney W. Grimes 			 * an event, then it goes back to run state.
1129df8bae1dSRodney W. Grimes 			 * Otherwise, process goes back to sleep state.
1130df8bae1dSRodney W. Grimes 			 */
1131df8bae1dSRodney W. Grimes 			if (action == SIG_DFL)
11322c42a146SMarcel Moolenaar 				SIGDELSET(p->p_siglist, sig);
1133df8bae1dSRodney W. Grimes 			if (action == SIG_CATCH)
1134df8bae1dSRodney W. Grimes 				goto runfast;
1135df8bae1dSRodney W. Grimes 			if (p->p_wchan == 0)
1136df8bae1dSRodney W. Grimes 				goto run;
1137df8bae1dSRodney W. Grimes 			p->p_stat = SSLEEP;
1138df8bae1dSRodney W. Grimes 			goto out;
1139df8bae1dSRodney W. Grimes 		}
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes 		if (prop & SA_STOP) {
1142df8bae1dSRodney W. Grimes 			/*
1143df8bae1dSRodney W. Grimes 			 * Already stopped, don't need to stop again.
1144df8bae1dSRodney W. Grimes 			 * (If we did the shell could get confused.)
1145df8bae1dSRodney W. Grimes 			 */
11462c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1147df8bae1dSRodney W. Grimes 			goto out;
1148df8bae1dSRodney W. Grimes 		}
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes 		/*
1151df8bae1dSRodney W. Grimes 		 * If process is sleeping interruptibly, then simulate a
1152df8bae1dSRodney W. Grimes 		 * wakeup so that when it is continued, it will be made
1153df8bae1dSRodney W. Grimes 		 * runnable and can look at the signal.  But don't make
1154df8bae1dSRodney W. Grimes 		 * the process runnable, leave it stopped.
1155df8bae1dSRodney W. Grimes 		 */
1156df8bae1dSRodney W. Grimes 		if (p->p_wchan && p->p_flag & P_SINTR)
1157df8bae1dSRodney W. Grimes 			unsleep(p);
1158df8bae1dSRodney W. Grimes 		goto out;
1159df8bae1dSRodney W. Grimes 
1160df8bae1dSRodney W. Grimes 	default:
1161df8bae1dSRodney W. Grimes 		/*
1162df8bae1dSRodney W. Grimes 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1163df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1164df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1165df8bae1dSRodney W. Grimes 		 */
1166df8bae1dSRodney W. Grimes 		if (p == curproc)
1167df8bae1dSRodney W. Grimes 			signotify(p);
11683163861cSTor Egge #ifdef SMP
11693163861cSTor Egge 		else if (p->p_stat == SRUN)
11703163861cSTor Egge 			forward_signal(p);
11713163861cSTor Egge #endif
1172df8bae1dSRodney W. Grimes 		goto out;
1173df8bae1dSRodney W. Grimes 	}
1174df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes runfast:
1177df8bae1dSRodney W. Grimes 	/*
1178df8bae1dSRodney W. Grimes 	 * Raise priority to at least PUSER.
1179df8bae1dSRodney W. Grimes 	 */
1180df8bae1dSRodney W. Grimes 	if (p->p_priority > PUSER)
1181df8bae1dSRodney W. Grimes 		p->p_priority = PUSER;
1182df8bae1dSRodney W. Grimes run:
1183df8bae1dSRodney W. Grimes 	setrunnable(p);
1184df8bae1dSRodney W. Grimes out:
1185df8bae1dSRodney W. Grimes 	splx(s);
1186df8bae1dSRodney W. Grimes }
1187df8bae1dSRodney W. Grimes 
1188df8bae1dSRodney W. Grimes /*
1189df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1190df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1191df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1192df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1193df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1194df8bae1dSRodney W. Grimes  * by checking the pending signal masks in the CURSIG macro.) The normal call
1195df8bae1dSRodney W. Grimes  * sequence is
1196df8bae1dSRodney W. Grimes  *
11972c42a146SMarcel Moolenaar  *	while (sig = CURSIG(curproc))
11982c42a146SMarcel Moolenaar  *		postsig(sig);
1199df8bae1dSRodney W. Grimes  */
120026f9a767SRodney W. Grimes int
1201df8bae1dSRodney W. Grimes issignal(p)
1202df8bae1dSRodney W. Grimes 	register struct proc *p;
1203df8bae1dSRodney W. Grimes {
12042c42a146SMarcel Moolenaar 	sigset_t mask;
12052c42a146SMarcel Moolenaar 	register int sig, prop;
1206df8bae1dSRodney W. Grimes 
1207df8bae1dSRodney W. Grimes 	for (;;) {
12082a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
12092a024a2bSSean Eric Fagan 
12102c42a146SMarcel Moolenaar 		mask = p->p_siglist;
12112c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1212df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
12132c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
12142c42a146SMarcel Moolenaar 		if (!SIGNOTEMPTY(mask))	 	/* no signal to send */
1215df8bae1dSRodney W. Grimes 			return (0);
12162c42a146SMarcel Moolenaar 		sig = sig_ffs(&mask);
12172c42a146SMarcel Moolenaar 		prop = sigprop(sig);
12182a024a2bSSean Eric Fagan 
12192c42a146SMarcel Moolenaar 		STOPEVENT(p, S_SIG, sig);
12202a024a2bSSean Eric Fagan 
1221df8bae1dSRodney W. Grimes 		/*
1222df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1223df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1224df8bae1dSRodney W. Grimes 		 */
12252c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
12262c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);
1227df8bae1dSRodney W. Grimes 			continue;
1228df8bae1dSRodney W. Grimes 		}
1229df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1230df8bae1dSRodney W. Grimes 			/*
1231df8bae1dSRodney W. Grimes 			 * If traced, always stop, and stay
1232df8bae1dSRodney W. Grimes 			 * stopped until released by the parent.
1233df8bae1dSRodney W. Grimes 			 */
12342c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1235df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1236df8bae1dSRodney W. Grimes 			do {
1237df8bae1dSRodney W. Grimes 				stop(p);
1238df8bae1dSRodney W. Grimes 				mi_switch();
12392a024a2bSSean Eric Fagan 			} while (!trace_req(p)
12402a024a2bSSean Eric Fagan 				 && p->p_flag & P_TRACED);
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes 			/*
1243df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1244df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1245df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1246df8bae1dSRodney W. Grimes 			 */
1247df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1248df8bae1dSRodney W. Grimes 				continue;
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes 			/*
1251df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1252df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1253df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1254df8bae1dSRodney W. Grimes 			 */
12552c42a146SMarcel Moolenaar 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
12562c42a146SMarcel Moolenaar 			sig = p->p_xstat;
12572c42a146SMarcel Moolenaar 			if (sig == 0)
1258df8bae1dSRodney W. Grimes 				continue;
1259df8bae1dSRodney W. Grimes 
1260df8bae1dSRodney W. Grimes 			/*
1261df8bae1dSRodney W. Grimes 			 * Put the new signal into p_siglist.  If the
1262df8bae1dSRodney W. Grimes 			 * signal is being masked, look for other signals.
1263df8bae1dSRodney W. Grimes 			 */
12642c42a146SMarcel Moolenaar 			SIGADDSET(p->p_siglist, sig);
12652c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1266df8bae1dSRodney W. Grimes 				continue;
1267df8bae1dSRodney W. Grimes 		}
1268df8bae1dSRodney W. Grimes 
1269df8bae1dSRodney W. Grimes 		/*
1270df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1271df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1272df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1273df8bae1dSRodney W. Grimes 		 */
12742c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1275df8bae1dSRodney W. Grimes 
12760b53fbe8SBruce Evans 		case (int)SIG_DFL:
1277df8bae1dSRodney W. Grimes 			/*
1278df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1279df8bae1dSRodney W. Grimes 			 */
1280df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1281df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1282df8bae1dSRodney W. Grimes 				/*
1283df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1284df8bae1dSRodney W. Grimes 				 * in init? XXX
1285df8bae1dSRodney W. Grimes 				 */
1286d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
12872c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1288df8bae1dSRodney W. Grimes #endif
1289df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1290df8bae1dSRodney W. Grimes 			}
1291df8bae1dSRodney W. Grimes 			/*
1292df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1293df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1294df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1295df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1296df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1297df8bae1dSRodney W. Grimes 			 */
1298df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1299df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1300df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1301df8bae1dSRodney W. Grimes 				    prop & SA_TTYSTOP))
1302df8bae1dSRodney W. Grimes 					break;	/* == ignore */
13032c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1304df8bae1dSRodney W. Grimes 				stop(p);
1305645682fdSLuoqi Chen 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1306df8bae1dSRodney W. Grimes 					psignal(p->p_pptr, SIGCHLD);
1307df8bae1dSRodney W. Grimes 				mi_switch();
1308df8bae1dSRodney W. Grimes 				break;
1309df8bae1dSRodney W. Grimes 			} else if (prop & SA_IGNORE) {
1310df8bae1dSRodney W. Grimes 				/*
1311df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1312df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1313df8bae1dSRodney W. Grimes 				 */
1314df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1315df8bae1dSRodney W. Grimes 			} else
13162c42a146SMarcel Moolenaar 				return (sig);
1317df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1318df8bae1dSRodney W. Grimes 
13190b53fbe8SBruce Evans 		case (int)SIG_IGN:
1320df8bae1dSRodney W. Grimes 			/*
1321df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1322df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1323df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1324df8bae1dSRodney W. Grimes 			 */
1325df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1326df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1327df8bae1dSRodney W. Grimes 				printf("issignal\n");
1328df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1329df8bae1dSRodney W. Grimes 
1330df8bae1dSRodney W. Grimes 		default:
1331df8bae1dSRodney W. Grimes 			/*
1332df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1333df8bae1dSRodney W. Grimes 			 * postsig() process it.
1334df8bae1dSRodney W. Grimes 			 */
13352c42a146SMarcel Moolenaar 			return (sig);
1336df8bae1dSRodney W. Grimes 		}
13372c42a146SMarcel Moolenaar 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1338df8bae1dSRodney W. Grimes 	}
1339df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1340df8bae1dSRodney W. Grimes }
1341df8bae1dSRodney W. Grimes 
1342df8bae1dSRodney W. Grimes /*
1343df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1344df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
1345df8bae1dSRodney W. Grimes  * on the run queue.
1346df8bae1dSRodney W. Grimes  */
134726f9a767SRodney W. Grimes void
1348df8bae1dSRodney W. Grimes stop(p)
1349df8bae1dSRodney W. Grimes 	register struct proc *p;
1350df8bae1dSRodney W. Grimes {
1351df8bae1dSRodney W. Grimes 
1352df8bae1dSRodney W. Grimes 	p->p_stat = SSTOP;
1353df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
1354df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
1355df8bae1dSRodney W. Grimes }
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes /*
1358df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1359df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1360df8bae1dSRodney W. Grimes  */
1361df8bae1dSRodney W. Grimes void
13622c42a146SMarcel Moolenaar postsig(sig)
13632c42a146SMarcel Moolenaar 	register int sig;
1364df8bae1dSRodney W. Grimes {
1365df8bae1dSRodney W. Grimes 	register struct proc *p = curproc;
13662c42a146SMarcel Moolenaar 	struct sigacts *ps = p->p_sigacts;
13672c42a146SMarcel Moolenaar 	sig_t action;
13682c42a146SMarcel Moolenaar 	sigset_t returnmask;
13692c42a146SMarcel Moolenaar 	int code;
1370df8bae1dSRodney W. Grimes 
13712c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
13725526d2d9SEivind Eklund 
13732c42a146SMarcel Moolenaar 	SIGDELSET(p->p_siglist, sig);
13742c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1375df8bae1dSRodney W. Grimes #ifdef KTRACE
1376df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_PSIG))
1377645682fdSLuoqi Chen 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
13782c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1379df8bae1dSRodney W. Grimes #endif
13802c42a146SMarcel Moolenaar 	STOPEVENT(p, S_SIG, sig);
13812a024a2bSSean Eric Fagan 
1382df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1383df8bae1dSRodney W. Grimes 		/*
1384df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1385df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1386df8bae1dSRodney W. Grimes 		 */
13872c42a146SMarcel Moolenaar 		sigexit(p, sig);
1388df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1389df8bae1dSRodney W. Grimes 	} else {
1390df8bae1dSRodney W. Grimes 		/*
1391df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1392df8bae1dSRodney W. Grimes 		 */
13932c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
13945526d2d9SEivind Eklund 		    ("postsig action"));
1395df8bae1dSRodney W. Grimes 		/*
1396df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1397645682fdSLuoqi Chen 		 * occurrences of this signal.
1398df8bae1dSRodney W. Grimes 		 *
1399645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1400df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1401645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1402df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1403df8bae1dSRodney W. Grimes 		 */
1404df8bae1dSRodney W. Grimes 		(void) splhigh();
1405645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
14066626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1407645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1408df8bae1dSRodney W. Grimes 		} else
1409df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
14102c42a146SMarcel Moolenaar 
14112c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
14122c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
14132c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
14142c42a146SMarcel Moolenaar 
14152c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1416289ccde0SPeter Wemm 			/*
14172c42a146SMarcel Moolenaar 			 * See do_sigaction() for origin of this code.
1418289ccde0SPeter Wemm 			 */
14192c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
14202c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
14212c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
14222c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
14232c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1424dedc04feSPeter Wemm 		}
1425df8bae1dSRodney W. Grimes 		(void) spl0();
1426df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
14272c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1428df8bae1dSRodney W. Grimes 			code = 0;
1429df8bae1dSRodney W. Grimes 		} else {
14306626c604SJulian Elischer 			code = p->p_code;
14316626c604SJulian Elischer 			p->p_code = 0;
14326626c604SJulian Elischer 			p->p_sig = 0;
1433df8bae1dSRodney W. Grimes 		}
14342c42a146SMarcel Moolenaar 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1435df8bae1dSRodney W. Grimes 	}
1436df8bae1dSRodney W. Grimes }
1437df8bae1dSRodney W. Grimes 
1438df8bae1dSRodney W. Grimes /*
1439df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1440df8bae1dSRodney W. Grimes  */
144126f9a767SRodney W. Grimes void
1442df8bae1dSRodney W. Grimes killproc(p, why)
1443df8bae1dSRodney W. Grimes 	struct proc *p;
1444df8bae1dSRodney W. Grimes 	char *why;
1445df8bae1dSRodney W. Grimes {
1446729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1447729b1e51SDavid Greenman 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1448df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1449df8bae1dSRodney W. Grimes }
1450df8bae1dSRodney W. Grimes 
1451df8bae1dSRodney W. Grimes /*
1452df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1453df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1454df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1455df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1456df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1457df8bae1dSRodney W. Grimes  * does not return.
1458df8bae1dSRodney W. Grimes  */
145926f9a767SRodney W. Grimes void
14602c42a146SMarcel Moolenaar sigexit(p, sig)
1461df8bae1dSRodney W. Grimes 	register struct proc *p;
14622c42a146SMarcel Moolenaar 	int sig;
1463df8bae1dSRodney W. Grimes {
1464df8bae1dSRodney W. Grimes 
1465df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
14662c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
14672c42a146SMarcel Moolenaar 		p->p_sig = sig;
1468c364e17eSAndrey A. Chernov 		/*
1469c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1470c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1471c364e17eSAndrey A. Chernov 		 * these messages.)
1472c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1473c364e17eSAndrey A. Chernov 		 */
1474fca666a1SJulian Elischer 		if (coredump(p) == 0)
14752c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
147657308494SJoerg Wunsch 		if (kern_logsigexit)
147757308494SJoerg Wunsch 			log(LOG_INFO,
147857308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
14793d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
14803d1b21c6SAndrey A. Chernov 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
14812c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
14822c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1483df8bae1dSRodney W. Grimes 	}
14842c42a146SMarcel Moolenaar 	exit1(p, W_EXITCODE(0, sig));
1485df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1486df8bae1dSRodney W. Grimes }
1487df8bae1dSRodney W. Grimes 
1488c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1489c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1490c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1491c5edb423SSean Eric Fagan 
1492c5edb423SSean Eric Fagan /*
1493c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1494c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1495c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1496c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1497c5edb423SSean Eric Fagan  *	%P	process id (pid)
1498c5edb423SSean Eric Fagan  *	%U	user id (uid)
1499c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1500c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1501c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1502c5edb423SSean Eric Fagan  */
1503c5edb423SSean Eric Fagan 
1504fca666a1SJulian Elischer static char *
1505c5edb423SSean Eric Fagan expand_name(name, uid, pid)
150687f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; {
1507c5edb423SSean Eric Fagan 	char *temp;
1508c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1509c5edb423SSean Eric Fagan 	int i, n;
1510c5edb423SSean Eric Fagan 	char *format = corefilename;
1511ce38ca0fSAlfred Perlstein 	size_t namelen;
1512c5edb423SSean Eric Fagan 
1513ce38ca0fSAlfred Perlstein 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
15140bfe2990SEivind Eklund 	if (temp == NULL)
15150bfe2990SEivind Eklund 		return NULL;
1516ce38ca0fSAlfred Perlstein 	namelen = strlen(name);
1517ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1518c5edb423SSean Eric Fagan 		int l;
1519c5edb423SSean Eric Fagan 		switch (format[i]) {
1520c5edb423SSean Eric Fagan 		case '%':	/* Format character */
1521c5edb423SSean Eric Fagan 			i++;
1522c5edb423SSean Eric Fagan 			switch (format[i]) {
1523c5edb423SSean Eric Fagan 			case '%':
1524c5edb423SSean Eric Fagan 				temp[n++] = '%';
1525c5edb423SSean Eric Fagan 				break;
1526c5edb423SSean Eric Fagan 			case 'N':	/* process name */
1527ce38ca0fSAlfred Perlstein 				if ((n + namelen) > MAXPATHLEN) {
152887f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1529c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1530c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1531c5edb423SSean Eric Fagan 					return NULL;
1532c5edb423SSean Eric Fagan 				}
1533ce38ca0fSAlfred Perlstein 				memcpy(temp+n, name, namelen);
1534ce38ca0fSAlfred Perlstein 				n += namelen;
1535c5edb423SSean Eric Fagan 				break;
1536c5edb423SSean Eric Fagan 			case 'P':	/* process id */
1537ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", pid);
1538c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
153987f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1540c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1541c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1542c5edb423SSean Eric Fagan 					return NULL;
1543c5edb423SSean Eric Fagan 				}
1544c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1545c5edb423SSean Eric Fagan 				n += l;
1546c5edb423SSean Eric Fagan 				break;
1547c5edb423SSean Eric Fagan 			case 'U':	/* user id */
1548ce38ca0fSAlfred Perlstein 				l = sprintf(buf, "%u", uid);
1549c5edb423SSean Eric Fagan 				if ((n + l) > MAXPATHLEN) {
155087f1de5fSBill Fumerola 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1551c5edb423SSean Eric Fagan 					    pid, name, uid, temp, name);
1552c5edb423SSean Eric Fagan 					free(temp, M_TEMP);
1553c5edb423SSean Eric Fagan 					return NULL;
1554c5edb423SSean Eric Fagan 				}
1555c5edb423SSean Eric Fagan 				memcpy(temp+n, buf, l);
1556c5edb423SSean Eric Fagan 				n += l;
1557c5edb423SSean Eric Fagan 				break;
1558c5edb423SSean Eric Fagan 			default:
1559c5edb423SSean Eric Fagan 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1560c5edb423SSean Eric Fagan 			}
1561c5edb423SSean Eric Fagan 			break;
1562c5edb423SSean Eric Fagan 		default:
1563c5edb423SSean Eric Fagan 			temp[n++] = format[i];
1564c5edb423SSean Eric Fagan 		}
1565c5edb423SSean Eric Fagan 	}
1566ce38ca0fSAlfred Perlstein 	temp[n] = '\0';
1567c5edb423SSean Eric Fagan 	return temp;
1568c5edb423SSean Eric Fagan }
1569c5edb423SSean Eric Fagan 
1570df8bae1dSRodney W. Grimes /*
1571fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
1572fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
1573fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
1574fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
1575fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
1576fca666a1SJulian Elischer  */
1577fca666a1SJulian Elischer 
1578fca666a1SJulian Elischer static int
1579fca666a1SJulian Elischer coredump(p)
1580fca666a1SJulian Elischer 	register struct proc *p;
1581fca666a1SJulian Elischer {
1582fca666a1SJulian Elischer 	register struct vnode *vp;
1583fca666a1SJulian Elischer 	register struct ucred *cred = p->p_cred->pc_ucred;
1584fca666a1SJulian Elischer 	struct nameidata nd;
1585fca666a1SJulian Elischer 	struct vattr vattr;
1586fca666a1SJulian Elischer 	int error, error1;
1587fca666a1SJulian Elischer 	char *name;			/* name of corefile */
1588fca666a1SJulian Elischer 	off_t limit;
1589fca666a1SJulian Elischer 
1590fca666a1SJulian Elischer 	STOPEVENT(p, S_CORE, 0);
1591fca666a1SJulian Elischer 
1592fca666a1SJulian Elischer 	if ((sugid_coredump == 0) && p->p_flag & P_SUGID)
1593fca666a1SJulian Elischer 		return (EFAULT);
1594fca666a1SJulian Elischer 
1595fca666a1SJulian Elischer 	/*
159635a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
159735a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
159835a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
159935a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
160035a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
160135a2598fSSean Eric Fagan 	 * if it is larger than the limit.
1602fca666a1SJulian Elischer 	 */
160335a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
160435a2598fSSean Eric Fagan 	if (limit == 0)
160535a2598fSSean Eric Fagan 		return 0;
160635a2598fSSean Eric Fagan 
1607fca666a1SJulian Elischer 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1608fca666a1SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1609fca666a1SJulian Elischer 	error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1610fca666a1SJulian Elischer 	free(name, M_TEMP);
1611fca666a1SJulian Elischer 	if (error)
1612fca666a1SJulian Elischer 		return (error);
1613fca666a1SJulian Elischer 	vp = nd.ni_vp;
1614fca666a1SJulian Elischer 
1615fca666a1SJulian Elischer 	/* Don't dump to non-regular files or files with links. */
1616fca666a1SJulian Elischer 	if (vp->v_type != VREG ||
1617fca666a1SJulian Elischer 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1618fca666a1SJulian Elischer 		error = EFAULT;
1619fca666a1SJulian Elischer 		goto out;
1620fca666a1SJulian Elischer 	}
1621fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
1622fca666a1SJulian Elischer 	vattr.va_size = 0;
1623fca666a1SJulian Elischer 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1624fca666a1SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, p);
1625fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
1626fca666a1SJulian Elischer 
1627fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
1628fca666a1SJulian Elischer 	  p->p_sysent->sv_coredump(p, vp, limit) :
1629fca666a1SJulian Elischer 	  ENOSYS;
1630fca666a1SJulian Elischer 
1631fca666a1SJulian Elischer out:
1632fca666a1SJulian Elischer 	VOP_UNLOCK(vp, 0, p);
1633fca666a1SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, p);
1634fca666a1SJulian Elischer 	if (error == 0)
1635fca666a1SJulian Elischer 		error = error1;
1636fca666a1SJulian Elischer 	return (error);
1637fca666a1SJulian Elischer }
1638fca666a1SJulian Elischer 
1639fca666a1SJulian Elischer /*
1640df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
1641df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
1642df8bae1dSRodney W. Grimes  */
1643d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1644df8bae1dSRodney W. Grimes struct nosys_args {
1645df8bae1dSRodney W. Grimes 	int	dummy;
1646df8bae1dSRodney W. Grimes };
1647d2d3e875SBruce Evans #endif
1648df8bae1dSRodney W. Grimes /* ARGSUSED */
164926f9a767SRodney W. Grimes int
1650cb226aaaSPoul-Henning Kamp nosys(p, args)
1651df8bae1dSRodney W. Grimes 	struct proc *p;
1652df8bae1dSRodney W. Grimes 	struct nosys_args *args;
1653df8bae1dSRodney W. Grimes {
1654df8bae1dSRodney W. Grimes 
1655df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
1656df8bae1dSRodney W. Grimes 	return (EINVAL);
1657df8bae1dSRodney W. Grimes }
1658831d27a9SDon Lewis 
1659831d27a9SDon Lewis /*
1660831d27a9SDon Lewis  * Send a signal to a SIGIO or SIGURG to a process or process group using
1661831d27a9SDon Lewis  * stored credentials rather than those of the current process.
1662831d27a9SDon Lewis  */
1663831d27a9SDon Lewis void
16642c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty)
1665831d27a9SDon Lewis 	struct sigio *sigio;
16662c42a146SMarcel Moolenaar 	int sig, checkctty;
1667831d27a9SDon Lewis {
1668831d27a9SDon Lewis 	if (sigio == NULL)
1669831d27a9SDon Lewis 		return;
1670831d27a9SDon Lewis 
1671831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
1672831d27a9SDon Lewis 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1673831d27a9SDon Lewis 		             sigio->sio_proc))
16742c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
1675831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
1676831d27a9SDon Lewis 		struct proc *p;
1677831d27a9SDon Lewis 
16782e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1679831d27a9SDon Lewis 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1680831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
16812c42a146SMarcel Moolenaar 				psignal(p, sig);
1682831d27a9SDon Lewis 	}
1683831d27a9SDon Lewis }
1684