xref: /freebsd/sys/kern/kern_sig.c (revision ef3dab76bfd7bed70b62cf19a5f7f346ff54e7fc)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46c87e2930SDavid Greenman #include <sys/kernel.h>
47d2d3e875SBruce Evans #include <sys/sysproto.h>
4836240ea5SDoug Rabson #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/namei.h>
51df8bae1dSRodney W. Grimes #include <sys/vnode.h>
52cb679c38SJonathan Lemon #include <sys/event.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
542a024a2bSSean Eric Fagan #include <sys/pioctl.h>
55df8bae1dSRodney W. Grimes #include <sys/acct.h>
563ac4d1efSBruce Evans #include <sys/fcntl.h>
57238510fcSJason Evans #include <sys/condvar.h>
58c31146a1SJohn Baldwin #include <sys/lock.h>
5935e0e5b3SJohn Baldwin #include <sys/mutex.h>
60df8bae1dSRodney W. Grimes #include <sys/wait.h>
610384fff8SJason Evans #include <sys/ktr.h>
62df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
63c31146a1SJohn Baldwin #include <sys/resourcevar.h>
646caa8a15SJohn Baldwin #include <sys/smp.h>
65df8bae1dSRodney W. Grimes #include <sys/stat.h>
661005a129SJohn Baldwin #include <sys/sx.h>
678f19eb88SIan Dowse #include <sys/syscallsubr.h>
681005a129SJohn Baldwin #include <sys/syslog.h>
69d66a5066SPeter Wemm #include <sys/sysent.h>
70c87e2930SDavid Greenman #include <sys/sysctl.h>
71c5edb423SSean Eric Fagan #include <sys/malloc.h>
7206ae1e91SMatthew Dillon #include <sys/unistd.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <machine/cpu.h>
75df8bae1dSRodney W. Grimes 
7623eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
7723eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
7823eeeff7SPeter Wemm #endif
7923eeeff7SPeter Wemm 
806f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
816f841fb7SMarcel Moolenaar 
824d77a549SAlfred Perlstein static int	coredump(struct thread *);
834d77a549SAlfred Perlstein static int	do_sigprocmask(struct proc *p, int how, sigset_t *set,
844d77a549SAlfred Perlstein 			sigset_t *oset, int old);
854d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
869c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
874d77a549SAlfred Perlstein static int	sig_ffs(sigset_t *set);
884d77a549SAlfred Perlstein static int	sigprop(int sig);
894d77a549SAlfred Perlstein static void	stop(struct proc *);
90e602ba25SJulian Elischer static void	tdsignal(struct thread *td, int sig, sig_t action);
91cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
92cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
93cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
94cb679c38SJonathan Lemon 
95cb679c38SJonathan Lemon struct filterops sig_filtops =
96cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
97cb679c38SJonathan Lemon 
9857308494SJoerg Wunsch static int	kern_logsigexit = 1;
993d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1003d177f46SBill Fumerola     &kern_logsigexit, 0,
1013d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10257308494SJoerg Wunsch 
1032b87b6d4SRobert Watson /*
1042b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1052b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1062b87b6d4SRobert Watson  * in the right situations.
1072b87b6d4SRobert Watson  */
1082b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1092b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1102b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1112b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1122b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1142b87b6d4SRobert Watson 
11522d4b0fbSJohn Polstra int sugid_coredump;
1163d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1173d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
118c87e2930SDavid Greenman 
119e5a28db9SPaul Saab static int	do_coredump = 1;
120e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
121e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
122e5a28db9SPaul Saab 
1232c42a146SMarcel Moolenaar /*
1242c42a146SMarcel Moolenaar  * Signal properties and actions.
1252c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1262c42a146SMarcel Moolenaar  * according to the following properties:
1272c42a146SMarcel Moolenaar  */
1282c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1292c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1302c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1312c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1322c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1332c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1342c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
135df8bae1dSRodney W. Grimes 
1362c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1372c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1382c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1392c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1402c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1412c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1452c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1462c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1482c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1492c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1502c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1512c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1522c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1532c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1542c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1552c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1562c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1572c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1582c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1592c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1602c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1612c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1622c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1632c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1642c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1652c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1662c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1672c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1682c42a146SMarcel Moolenaar };
1692c42a146SMarcel Moolenaar 
170fbbeeb6cSBruce Evans /*
171fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
172fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
173fbbeeb6cSBruce Evans  * action, the process stops in issignal().
174e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
175fbbeeb6cSBruce Evans  *
17633510ef1SBruce Evans  * MP SAFE.
177fbbeeb6cSBruce Evans  */
178fbbeeb6cSBruce Evans int
179e602ba25SJulian Elischer cursig(struct thread *td)
180fbbeeb6cSBruce Evans {
181e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
182fbbeeb6cSBruce Evans 
1832ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
184179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1851d9c5696SJuli Mallett 	return (SIGPENDING(p) ? issignal(td) : 0);
186fbbeeb6cSBruce Evans }
187fbbeeb6cSBruce Evans 
18879065dbaSBruce Evans /*
18979065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1901d9c5696SJuli Mallett  * mode.  This must be called whenever a signal is added to p_siglist or
19179065dbaSBruce Evans  * unmasked in p_sigmask.
19279065dbaSBruce Evans  */
19379065dbaSBruce Evans void
19479065dbaSBruce Evans signotify(struct proc *p)
19579065dbaSBruce Evans {
1964a338afdSJulian Elischer 	struct thread *td;
1974f0db5e0SJulian Elischer 	struct ksegrp *kg;
19879065dbaSBruce Evans 
19979065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
20079065dbaSBruce Evans 	mtx_lock_spin(&sched_lock);
2011d9c5696SJuli Mallett 	if (SIGPENDING(p)) {
20279065dbaSBruce Evans 		p->p_sflag |= PS_NEEDSIGCHK;
2034f0db5e0SJulian Elischer 		/* XXXKSE for now punish all KSEs */
2044f0db5e0SJulian Elischer 		FOREACH_KSEGRP_IN_PROC(p, kg) {
2054a338afdSJulian Elischer 			FOREACH_THREAD_IN_GROUP(kg, td) {
2064a338afdSJulian Elischer 				td->td_flags |= TDF_ASTPENDING;
2074f0db5e0SJulian Elischer 			}
2084f0db5e0SJulian Elischer 		}
20979065dbaSBruce Evans 	}
21079065dbaSBruce Evans 	mtx_unlock_spin(&sched_lock);
21179065dbaSBruce Evans }
21279065dbaSBruce Evans 
2136f841fb7SMarcel Moolenaar static __inline int
2146f841fb7SMarcel Moolenaar sigprop(int sig)
2152c42a146SMarcel Moolenaar {
2166f841fb7SMarcel Moolenaar 
2172c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2182c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2192c42a146SMarcel Moolenaar 	return (0);
220df8bae1dSRodney W. Grimes }
2212c42a146SMarcel Moolenaar 
2226f841fb7SMarcel Moolenaar static __inline int
2236f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2242c42a146SMarcel Moolenaar {
2252c42a146SMarcel Moolenaar 	int i;
2262c42a146SMarcel Moolenaar 
2276f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2282c42a146SMarcel Moolenaar 		if (set->__bits[i])
2292c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
230df8bae1dSRodney W. Grimes 	return (0);
231df8bae1dSRodney W. Grimes }
232df8bae1dSRodney W. Grimes 
2332c42a146SMarcel Moolenaar /*
2348f19eb88SIan Dowse  * kern_sigaction
2352c42a146SMarcel Moolenaar  * sigaction
23623eeeff7SPeter Wemm  * freebsd4_sigaction
2372c42a146SMarcel Moolenaar  * osigaction
2382c42a146SMarcel Moolenaar  */
2398f19eb88SIan Dowse int
24023eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2418f19eb88SIan Dowse 	struct thread *td;
2422c42a146SMarcel Moolenaar 	register int sig;
2432c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
24423eeeff7SPeter Wemm 	int flags;
245df8bae1dSRodney W. Grimes {
246628d2653SJohn Baldwin 	register struct sigacts *ps;
2478f19eb88SIan Dowse 	struct proc *p = td->td_proc;
248df8bae1dSRodney W. Grimes 
2492899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2502c42a146SMarcel Moolenaar 		return (EINVAL);
2512c42a146SMarcel Moolenaar 
252628d2653SJohn Baldwin 	PROC_LOCK(p);
253628d2653SJohn Baldwin 	ps = p->p_sigacts;
2542c42a146SMarcel Moolenaar 	if (oact) {
2552c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2562c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2572c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2582c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2592c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2602c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2612c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2622c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2632c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2642c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2652c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2662c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2672c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
268645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2692c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
270645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2712c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2722c42a146SMarcel Moolenaar 	}
2732c42a146SMarcel Moolenaar 	if (act) {
2742c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
275628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
276628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2772c42a146SMarcel Moolenaar 			return (EINVAL);
278628d2653SJohn Baldwin 		}
2792c42a146SMarcel Moolenaar 
280df8bae1dSRodney W. Grimes 		/*
281df8bae1dSRodney W. Grimes 		 * Change setting atomically.
282df8bae1dSRodney W. Grimes 		 */
2832c42a146SMarcel Moolenaar 
2842c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2852c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2862c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
287aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
288aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
28980f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
29080f42b55SIan Dowse 		} else {
29180f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2922c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2932c42a146SMarcel Moolenaar 		}
2942c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2952c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
296df8bae1dSRodney W. Grimes 		else
2972c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
2982c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
2992c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
300df8bae1dSRodney W. Grimes 		else
3012c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3022c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3032c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3041e41c1b5SSteven Wallace 		else
3052c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3062c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3072c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
308289ccde0SPeter Wemm 		else
3092c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
310df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3112c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3122c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
313df8bae1dSRodney W. Grimes 		else
3148c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
315df8bae1dSRodney W. Grimes #endif
3162c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3172c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
318645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
3196626c604SJulian Elischer 			else
320645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
321ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
322245f17d4SJoerg Wunsch 				/*
3232c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3242c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3252c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3262c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
327245f17d4SJoerg Wunsch 				 */
328245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
329645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3306626c604SJulian Elischer 				else
331645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
332245f17d4SJoerg Wunsch 			} else
333645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
334ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
335ba1551caSIan Dowse 				p->p_procsig->ps_flag |= PS_CLDSIGIGN;
336ba1551caSIan Dowse 			else
337ba1551caSIan Dowse 				p->p_procsig->ps_flag &= ~PS_CLDSIGIGN;
338df8bae1dSRodney W. Grimes 		}
339df8bae1dSRodney W. Grimes 		/*
340df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3412c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3422c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3432c42a146SMarcel Moolenaar 		 * have to restart the process.
344df8bae1dSRodney W. Grimes 		 */
3452c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3462c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3472c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3482c42a146SMarcel Moolenaar 			/* never to be seen again */
3491d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
3502c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3512c42a146SMarcel Moolenaar 				/* easier in psignal */
3522c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3532c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
354645682fdSLuoqi Chen 		} else {
3552c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3562c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3572c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3582c42a146SMarcel Moolenaar 			else
3592c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3602c42a146SMarcel Moolenaar 		}
36123eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
36223eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
36323eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
36423eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
36523eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
36623eeeff7SPeter Wemm 		else
36723eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
36823eeeff7SPeter Wemm #endif
369e8ebc08fSPeter Wemm #ifdef COMPAT_43
370645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
37123eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
37223eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
373645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
374645682fdSLuoqi Chen 		else
375645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
376e8ebc08fSPeter Wemm #endif
377df8bae1dSRodney W. Grimes 	}
378628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3792c42a146SMarcel Moolenaar 	return (0);
3802c42a146SMarcel Moolenaar }
3812c42a146SMarcel Moolenaar 
3822c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3832c42a146SMarcel Moolenaar struct sigaction_args {
3842c42a146SMarcel Moolenaar 	int	sig;
3852c42a146SMarcel Moolenaar 	struct	sigaction *act;
3862c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3872c42a146SMarcel Moolenaar };
3882c42a146SMarcel Moolenaar #endif
389fb99ab88SMatthew Dillon /*
390fb99ab88SMatthew Dillon  * MPSAFE
391fb99ab88SMatthew Dillon  */
3922c42a146SMarcel Moolenaar int
393b40ce416SJulian Elischer sigaction(td, uap)
394b40ce416SJulian Elischer 	struct thread *td;
3952c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
3962c42a146SMarcel Moolenaar {
3972c42a146SMarcel Moolenaar 	struct sigaction act, oact;
3982c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
3992c42a146SMarcel Moolenaar 	int error;
4002c42a146SMarcel Moolenaar 
4016f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4026f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4032c42a146SMarcel Moolenaar 	if (actp) {
4046f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4052c42a146SMarcel Moolenaar 		if (error)
40644443757STim J. Robbins 			return (error);
4072c42a146SMarcel Moolenaar 	}
40844443757STim J. Robbins 	mtx_lock(&Giant);
4098f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
41044443757STim J. Robbins 	mtx_unlock(&Giant);
4112c42a146SMarcel Moolenaar 	if (oactp && !error) {
4126f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4132c42a146SMarcel Moolenaar 	}
4142c42a146SMarcel Moolenaar 	return (error);
4152c42a146SMarcel Moolenaar }
4162c42a146SMarcel Moolenaar 
41723eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
41823eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
41923eeeff7SPeter Wemm struct freebsd4_sigaction_args {
42023eeeff7SPeter Wemm 	int	sig;
42123eeeff7SPeter Wemm 	struct	sigaction *act;
42223eeeff7SPeter Wemm 	struct	sigaction *oact;
42323eeeff7SPeter Wemm };
42423eeeff7SPeter Wemm #endif
42523eeeff7SPeter Wemm /*
42623eeeff7SPeter Wemm  * MPSAFE
42723eeeff7SPeter Wemm  */
42823eeeff7SPeter Wemm int
42923eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
43023eeeff7SPeter Wemm 	struct thread *td;
43123eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
43223eeeff7SPeter Wemm {
43323eeeff7SPeter Wemm 	struct sigaction act, oact;
43423eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
43523eeeff7SPeter Wemm 	int error;
43623eeeff7SPeter Wemm 
43723eeeff7SPeter Wemm 
43823eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
43923eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
44023eeeff7SPeter Wemm 	if (actp) {
44123eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
44223eeeff7SPeter Wemm 		if (error)
44344443757STim J. Robbins 			return (error);
44423eeeff7SPeter Wemm 	}
44544443757STim J. Robbins 	mtx_lock(&Giant);
44623eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
44744443757STim J. Robbins 	mtx_unlock(&Giant);
44823eeeff7SPeter Wemm 	if (oactp && !error) {
44923eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
45023eeeff7SPeter Wemm 	}
45123eeeff7SPeter Wemm 	return (error);
45223eeeff7SPeter Wemm }
45323eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
45423eeeff7SPeter Wemm 
45531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4562c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4572c42a146SMarcel Moolenaar struct osigaction_args {
4582c42a146SMarcel Moolenaar 	int	signum;
4592c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4602c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4612c42a146SMarcel Moolenaar };
4622c42a146SMarcel Moolenaar #endif
463fb99ab88SMatthew Dillon /*
464fb99ab88SMatthew Dillon  * MPSAFE
465fb99ab88SMatthew Dillon  */
4662c42a146SMarcel Moolenaar int
467b40ce416SJulian Elischer osigaction(td, uap)
468b40ce416SJulian Elischer 	struct thread *td;
4692c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4702c42a146SMarcel Moolenaar {
4712c42a146SMarcel Moolenaar 	struct osigaction sa;
4722c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4732c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4742c42a146SMarcel Moolenaar 	int error;
4752c42a146SMarcel Moolenaar 
4766f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4776f841fb7SMarcel Moolenaar 		return (EINVAL);
478fb99ab88SMatthew Dillon 
4796f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4806f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
481fb99ab88SMatthew Dillon 
4822c42a146SMarcel Moolenaar 	if (nsap) {
4836f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4842c42a146SMarcel Moolenaar 		if (error)
48544443757STim J. Robbins 			return (error);
4862c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4872c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4882c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
4892c42a146SMarcel Moolenaar 	}
49044443757STim J. Robbins 	mtx_lock(&Giant);
49123eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
49244443757STim J. Robbins 	mtx_unlock(&Giant);
4932c42a146SMarcel Moolenaar 	if (osap && !error) {
4942c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
4952c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
4962c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
4976f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
4982c42a146SMarcel Moolenaar 	}
4992c42a146SMarcel Moolenaar 	return (error);
5002c42a146SMarcel Moolenaar }
50123eeeff7SPeter Wemm 
50223eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
50323eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
50423eeeff7SPeter Wemm int
50523eeeff7SPeter Wemm osigreturn(td, uap)
50623eeeff7SPeter Wemm 	struct thread *td;
50723eeeff7SPeter Wemm 	struct osigreturn_args *uap;
50823eeeff7SPeter Wemm {
50923eeeff7SPeter Wemm 
51023eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
51123eeeff7SPeter Wemm }
51223eeeff7SPeter Wemm #endif
51331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes /*
516df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
517df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
518df8bae1dSRodney W. Grimes  */
519df8bae1dSRodney W. Grimes void
520df8bae1dSRodney W. Grimes siginit(p)
521df8bae1dSRodney W. Grimes 	struct proc *p;
522df8bae1dSRodney W. Grimes {
523df8bae1dSRodney W. Grimes 	register int i;
524df8bae1dSRodney W. Grimes 
525628d2653SJohn Baldwin 	PROC_LOCK(p);
5262c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5272c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
5282c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
529628d2653SJohn Baldwin 	PROC_UNLOCK(p);
530df8bae1dSRodney W. Grimes }
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes /*
533df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
534df8bae1dSRodney W. Grimes  */
535df8bae1dSRodney W. Grimes void
536df8bae1dSRodney W. Grimes execsigs(p)
537df8bae1dSRodney W. Grimes 	register struct proc *p;
538df8bae1dSRodney W. Grimes {
539628d2653SJohn Baldwin 	register struct sigacts *ps;
5402c42a146SMarcel Moolenaar 	register int sig;
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes 	/*
543df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
544df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
545df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
546df8bae1dSRodney W. Grimes 	 */
5479b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
548628d2653SJohn Baldwin 	ps = p->p_sigacts;
5492c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
5502c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
5512c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
5522c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5532c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
5542c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
5551d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
556df8bae1dSRodney W. Grimes 		}
5572c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
558df8bae1dSRodney W. Grimes 	}
559df8bae1dSRodney W. Grimes 	/*
560df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
561df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
562df8bae1dSRodney W. Grimes 	 */
563645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
564645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
565645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
5663b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
56780e907a1SPeter Wemm 	/*
56880e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
56980e907a1SPeter Wemm 	 */
570ba1551caSIan Dowse 	p->p_procsig->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
571c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
572c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
573df8bae1dSRodney W. Grimes }
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes /*
576628d2653SJohn Baldwin  * do_sigprocmask()
5777c8fdcbdSMatthew Dillon  *
578628d2653SJohn Baldwin  *	Manipulate signal mask.
579df8bae1dSRodney W. Grimes  */
5802c42a146SMarcel Moolenaar static int
581645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
5822c42a146SMarcel Moolenaar 	struct proc *p;
5832c42a146SMarcel Moolenaar 	int how;
5842c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
585645682fdSLuoqi Chen 	int old;
5862c42a146SMarcel Moolenaar {
5872c42a146SMarcel Moolenaar 	int error;
5882c42a146SMarcel Moolenaar 
589628d2653SJohn Baldwin 	PROC_LOCK(p);
5902c42a146SMarcel Moolenaar 	if (oset != NULL)
5912c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
5922c42a146SMarcel Moolenaar 
5932c42a146SMarcel Moolenaar 	error = 0;
5942c42a146SMarcel Moolenaar 	if (set != NULL) {
5952c42a146SMarcel Moolenaar 		switch (how) {
5962c42a146SMarcel Moolenaar 		case SIG_BLOCK:
597645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
5982c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
5992c42a146SMarcel Moolenaar 			break;
6002c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6012c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
60279065dbaSBruce Evans 			signotify(p);
6032c42a146SMarcel Moolenaar 			break;
6042c42a146SMarcel Moolenaar 		case SIG_SETMASK:
605645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
606645682fdSLuoqi Chen 			if (old)
607645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
608645682fdSLuoqi Chen 			else
6092c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
61079065dbaSBruce Evans 			signotify(p);
6112c42a146SMarcel Moolenaar 			break;
6122c42a146SMarcel Moolenaar 		default:
6132c42a146SMarcel Moolenaar 			error = EINVAL;
6142c42a146SMarcel Moolenaar 			break;
6152c42a146SMarcel Moolenaar 		}
6162c42a146SMarcel Moolenaar 	}
617628d2653SJohn Baldwin 	PROC_UNLOCK(p);
6182c42a146SMarcel Moolenaar 	return (error);
6192c42a146SMarcel Moolenaar }
6202c42a146SMarcel Moolenaar 
6217c8fdcbdSMatthew Dillon /*
622b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
6237c8fdcbdSMatthew Dillon  */
6247c8fdcbdSMatthew Dillon 
625d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
626df8bae1dSRodney W. Grimes struct sigprocmask_args {
627df8bae1dSRodney W. Grimes 	int	how;
6282c42a146SMarcel Moolenaar 	const sigset_t *set;
6292c42a146SMarcel Moolenaar 	sigset_t *oset;
630df8bae1dSRodney W. Grimes };
631d2d3e875SBruce Evans #endif
63226f9a767SRodney W. Grimes int
633b40ce416SJulian Elischer sigprocmask(td, uap)
634b40ce416SJulian Elischer 	register struct thread *td;
635df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
636df8bae1dSRodney W. Grimes {
637b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
6382c42a146SMarcel Moolenaar 	sigset_t set, oset;
6392c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6402c42a146SMarcel Moolenaar 	int error;
641df8bae1dSRodney W. Grimes 
6426f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6436f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6442c42a146SMarcel Moolenaar 	if (setp) {
6456f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6462c42a146SMarcel Moolenaar 		if (error)
6472c42a146SMarcel Moolenaar 			return (error);
648df8bae1dSRodney W. Grimes 	}
649645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
6502c42a146SMarcel Moolenaar 	if (osetp && !error) {
6516f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
6522c42a146SMarcel Moolenaar 	}
6532c42a146SMarcel Moolenaar 	return (error);
6542c42a146SMarcel Moolenaar }
6552c42a146SMarcel Moolenaar 
65631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6577c8fdcbdSMatthew Dillon /*
6587c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
6597c8fdcbdSMatthew Dillon  */
6602c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6612c42a146SMarcel Moolenaar struct osigprocmask_args {
6622c42a146SMarcel Moolenaar 	int	how;
6632c42a146SMarcel Moolenaar 	osigset_t mask;
6642c42a146SMarcel Moolenaar };
6652c42a146SMarcel Moolenaar #endif
6662c42a146SMarcel Moolenaar int
667b40ce416SJulian Elischer osigprocmask(td, uap)
668b40ce416SJulian Elischer 	register struct thread *td;
6692c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
6702c42a146SMarcel Moolenaar {
671b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
6722c42a146SMarcel Moolenaar 	sigset_t set, oset;
6732c42a146SMarcel Moolenaar 	int error;
6742c42a146SMarcel Moolenaar 
6752c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
676645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
677b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
678df8bae1dSRodney W. Grimes 	return (error);
679df8bae1dSRodney W. Grimes }
68031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
681df8bae1dSRodney W. Grimes 
682d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
683df8bae1dSRodney W. Grimes struct sigpending_args {
6842c42a146SMarcel Moolenaar 	sigset_t	*set;
685df8bae1dSRodney W. Grimes };
686d2d3e875SBruce Evans #endif
687fb99ab88SMatthew Dillon /*
688fb99ab88SMatthew Dillon  * MPSAFE
689fb99ab88SMatthew Dillon  */
69026f9a767SRodney W. Grimes int
691b40ce416SJulian Elischer sigpending(td, uap)
692b40ce416SJulian Elischer 	struct thread *td;
693df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
694df8bae1dSRodney W. Grimes {
695b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
696628d2653SJohn Baldwin 	sigset_t siglist;
697df8bae1dSRodney W. Grimes 
698628d2653SJohn Baldwin 	PROC_LOCK(p);
6991d9c5696SJuli Mallett 	siglist = p->p_siglist;
700628d2653SJohn Baldwin 	PROC_UNLOCK(p);
70148e8f774STim J. Robbins 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
7022c42a146SMarcel Moolenaar }
7032c42a146SMarcel Moolenaar 
70431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7052c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7062c42a146SMarcel Moolenaar struct osigpending_args {
7072c42a146SMarcel Moolenaar 	int	dummy;
7082c42a146SMarcel Moolenaar };
7092c42a146SMarcel Moolenaar #endif
710fb99ab88SMatthew Dillon /*
711fb99ab88SMatthew Dillon  * MPSAFE
712fb99ab88SMatthew Dillon  */
7132c42a146SMarcel Moolenaar int
714b40ce416SJulian Elischer osigpending(td, uap)
715b40ce416SJulian Elischer 	struct thread *td;
7162c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
7172c42a146SMarcel Moolenaar {
718b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
719b40ce416SJulian Elischer 
720628d2653SJohn Baldwin 	PROC_LOCK(p);
7211d9c5696SJuli Mallett 	SIG2OSIG(p->p_siglist, td->td_retval[0]);
722628d2653SJohn Baldwin 	PROC_UNLOCK(p);
723df8bae1dSRodney W. Grimes 	return (0);
724df8bae1dSRodney W. Grimes }
72531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
726df8bae1dSRodney W. Grimes 
727df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
728df8bae1dSRodney W. Grimes /*
729df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
730df8bae1dSRodney W. Grimes  */
731d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
732df8bae1dSRodney W. Grimes struct osigvec_args {
733df8bae1dSRodney W. Grimes 	int	signum;
734df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
735df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
736df8bae1dSRodney W. Grimes };
737d2d3e875SBruce Evans #endif
738fb99ab88SMatthew Dillon /*
739fb99ab88SMatthew Dillon  * MPSAFE
740fb99ab88SMatthew Dillon  */
741df8bae1dSRodney W. Grimes /* ARGSUSED */
74226f9a767SRodney W. Grimes int
743b40ce416SJulian Elischer osigvec(td, uap)
744b40ce416SJulian Elischer 	struct thread *td;
745df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
746df8bae1dSRodney W. Grimes {
747df8bae1dSRodney W. Grimes 	struct sigvec vec;
7482c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
7492c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
7502c42a146SMarcel Moolenaar 	int error;
751df8bae1dSRodney W. Grimes 
7526f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
7536f841fb7SMarcel Moolenaar 		return (EINVAL);
7546f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
7556f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
7562c42a146SMarcel Moolenaar 	if (nsap) {
7576f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
7582c42a146SMarcel Moolenaar 		if (error)
759df8bae1dSRodney W. Grimes 			return (error);
7602c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
7612c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
7622c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
7632c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
764df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
7652c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
766df8bae1dSRodney W. Grimes #endif
767df8bae1dSRodney W. Grimes 	}
768fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
7698f19eb88SIan Dowse 	error = kern_sigaction(td, uap->signum, nsap, osap, 1);
770fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
7712c42a146SMarcel Moolenaar 	if (osap && !error) {
7722c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7732c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7742c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7752c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7762c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7772c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7782c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7792c42a146SMarcel Moolenaar #endif
7806f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
7812c42a146SMarcel Moolenaar 	}
7822c42a146SMarcel Moolenaar 	return (error);
783df8bae1dSRodney W. Grimes }
784df8bae1dSRodney W. Grimes 
785d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
786df8bae1dSRodney W. Grimes struct osigblock_args {
787df8bae1dSRodney W. Grimes 	int	mask;
788df8bae1dSRodney W. Grimes };
789d2d3e875SBruce Evans #endif
790fb99ab88SMatthew Dillon /*
791fb99ab88SMatthew Dillon  * MPSAFE
792fb99ab88SMatthew Dillon  */
79326f9a767SRodney W. Grimes int
794b40ce416SJulian Elischer osigblock(td, uap)
795b40ce416SJulian Elischer 	register struct thread *td;
796df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
797df8bae1dSRodney W. Grimes {
798b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
7992c42a146SMarcel Moolenaar 	sigset_t set;
800df8bae1dSRodney W. Grimes 
8012c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
8022c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
803fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
804628d2653SJohn Baldwin 	PROC_LOCK(p);
805b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
8062c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
807628d2653SJohn Baldwin 	PROC_UNLOCK(p);
808fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
809df8bae1dSRodney W. Grimes 	return (0);
810df8bae1dSRodney W. Grimes }
811df8bae1dSRodney W. Grimes 
812d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
813df8bae1dSRodney W. Grimes struct osigsetmask_args {
814df8bae1dSRodney W. Grimes 	int	mask;
815df8bae1dSRodney W. Grimes };
816d2d3e875SBruce Evans #endif
817fb99ab88SMatthew Dillon /*
818fb99ab88SMatthew Dillon  * MPSAFE
819fb99ab88SMatthew Dillon  */
82026f9a767SRodney W. Grimes int
821b40ce416SJulian Elischer osigsetmask(td, uap)
822b40ce416SJulian Elischer 	struct thread *td;
823df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
824df8bae1dSRodney W. Grimes {
825b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
8262c42a146SMarcel Moolenaar 	sigset_t set;
827df8bae1dSRodney W. Grimes 
8282c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
8292c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
830fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
831628d2653SJohn Baldwin 	PROC_LOCK(p);
832b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
833645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
83479065dbaSBruce Evans 	signotify(p);
835628d2653SJohn Baldwin 	PROC_UNLOCK(p);
836fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
837df8bae1dSRodney W. Grimes 	return (0);
838df8bae1dSRodney W. Grimes }
839df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
840df8bae1dSRodney W. Grimes 
841df8bae1dSRodney W. Grimes /*
842df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
843df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
844df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
845b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
846b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
847b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
848df8bae1dSRodney W. Grimes  */
849d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
850df8bae1dSRodney W. Grimes struct sigsuspend_args {
8512c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
852df8bae1dSRodney W. Grimes };
853d2d3e875SBruce Evans #endif
854fb99ab88SMatthew Dillon /*
855fb99ab88SMatthew Dillon  * MPSAFE
856fb99ab88SMatthew Dillon  */
857df8bae1dSRodney W. Grimes /* ARGSUSED */
85826f9a767SRodney W. Grimes int
859b40ce416SJulian Elischer sigsuspend(td, uap)
860b40ce416SJulian Elischer 	struct thread *td;
861df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
862df8bae1dSRodney W. Grimes {
8632c42a146SMarcel Moolenaar 	sigset_t mask;
8642c42a146SMarcel Moolenaar 	int error;
8652c42a146SMarcel Moolenaar 
8666f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
8672c42a146SMarcel Moolenaar 	if (error)
8682c42a146SMarcel Moolenaar 		return (error);
8698f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
8708f19eb88SIan Dowse }
8718f19eb88SIan Dowse 
8728f19eb88SIan Dowse int
8738f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
8748f19eb88SIan Dowse {
8758f19eb88SIan Dowse 	struct proc *p = td->td_proc;
8768f19eb88SIan Dowse 	register struct sigacts *ps;
877df8bae1dSRodney W. Grimes 
878df8bae1dSRodney W. Grimes 	/*
879645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
880df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
881df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
882df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
883df8bae1dSRodney W. Grimes 	 * to indicate this.
884df8bae1dSRodney W. Grimes 	 */
885fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
886628d2653SJohn Baldwin 	PROC_LOCK(p);
887628d2653SJohn Baldwin 	ps = p->p_sigacts;
8886626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
889645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
890645682fdSLuoqi Chen 
891645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
8922c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
89379065dbaSBruce Evans 	signotify(p);
89401609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
8952c42a146SMarcel Moolenaar 		/* void */;
896628d2653SJohn Baldwin 	PROC_UNLOCK(p);
897fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
8982c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
8992c42a146SMarcel Moolenaar 	return (EINTR);
9002c42a146SMarcel Moolenaar }
9012c42a146SMarcel Moolenaar 
90231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9032c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9042c42a146SMarcel Moolenaar struct osigsuspend_args {
9052c42a146SMarcel Moolenaar 	osigset_t mask;
9062c42a146SMarcel Moolenaar };
9072c42a146SMarcel Moolenaar #endif
908fb99ab88SMatthew Dillon /*
909fb99ab88SMatthew Dillon  * MPSAFE
910fb99ab88SMatthew Dillon  */
9112c42a146SMarcel Moolenaar /* ARGSUSED */
9122c42a146SMarcel Moolenaar int
913b40ce416SJulian Elischer osigsuspend(td, uap)
914b40ce416SJulian Elischer 	struct thread *td;
9152c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
9162c42a146SMarcel Moolenaar {
917b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
918645682fdSLuoqi Chen 	sigset_t mask;
919628d2653SJohn Baldwin 	register struct sigacts *ps;
9202c42a146SMarcel Moolenaar 
921fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
922628d2653SJohn Baldwin 	PROC_LOCK(p);
923628d2653SJohn Baldwin 	ps = p->p_sigacts;
9242c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
925645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
926645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
927645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
928645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
92979065dbaSBruce Evans 	signotify(p);
93001609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
931df8bae1dSRodney W. Grimes 		/* void */;
932628d2653SJohn Baldwin 	PROC_UNLOCK(p);
933fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
934df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
935df8bae1dSRodney W. Grimes 	return (EINTR);
936df8bae1dSRodney W. Grimes }
93731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
940d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
941df8bae1dSRodney W. Grimes struct osigstack_args {
942df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
943df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
944df8bae1dSRodney W. Grimes };
945d2d3e875SBruce Evans #endif
946fb99ab88SMatthew Dillon /*
947fb99ab88SMatthew Dillon  * MPSAFE
948fb99ab88SMatthew Dillon  */
949df8bae1dSRodney W. Grimes /* ARGSUSED */
95026f9a767SRodney W. Grimes int
951b40ce416SJulian Elischer osigstack(td, uap)
952b40ce416SJulian Elischer 	struct thread *td;
953df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
954df8bae1dSRodney W. Grimes {
955b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
956df8bae1dSRodney W. Grimes 	struct sigstack ss;
957fb99ab88SMatthew Dillon 	int error = 0;
958fb99ab88SMatthew Dillon 
959fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
960df8bae1dSRodney W. Grimes 
961d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
962628d2653SJohn Baldwin 		PROC_LOCK(p);
963645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
964b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
965628d2653SJohn Baldwin 		PROC_UNLOCK(p);
966d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
967d034d459SMarcel Moolenaar 		if (error)
968fb99ab88SMatthew Dillon 			goto done2;
969d034d459SMarcel Moolenaar 	}
970d034d459SMarcel Moolenaar 
971d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
972d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
973fb99ab88SMatthew Dillon 			goto done2;
974628d2653SJohn Baldwin 		PROC_LOCK(p);
975645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
976645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
977645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
978645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
979628d2653SJohn Baldwin 		PROC_UNLOCK(p);
980df8bae1dSRodney W. Grimes 	}
981fb99ab88SMatthew Dillon done2:
982fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
983fb99ab88SMatthew Dillon 	return (error);
984df8bae1dSRodney W. Grimes }
985df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
986df8bae1dSRodney W. Grimes 
987d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
988df8bae1dSRodney W. Grimes struct sigaltstack_args {
9892c42a146SMarcel Moolenaar 	stack_t	*ss;
9902c42a146SMarcel Moolenaar 	stack_t	*oss;
991df8bae1dSRodney W. Grimes };
992d2d3e875SBruce Evans #endif
993fb99ab88SMatthew Dillon /*
994fb99ab88SMatthew Dillon  * MPSAFE
995fb99ab88SMatthew Dillon  */
996df8bae1dSRodney W. Grimes /* ARGSUSED */
99726f9a767SRodney W. Grimes int
998b40ce416SJulian Elischer sigaltstack(td, uap)
999b40ce416SJulian Elischer 	struct thread *td;
1000df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1001df8bae1dSRodney W. Grimes {
10028f19eb88SIan Dowse 	stack_t ss, oss;
10038f19eb88SIan Dowse 	int error;
10048f19eb88SIan Dowse 
10058f19eb88SIan Dowse 	if (uap->ss != NULL) {
10068f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
10078f19eb88SIan Dowse 		if (error)
10088f19eb88SIan Dowse 			return (error);
10098f19eb88SIan Dowse 	}
10108f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
10118f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
10128f19eb88SIan Dowse 	if (error)
10138f19eb88SIan Dowse 		return (error);
10148f19eb88SIan Dowse 	if (uap->oss != NULL)
10158f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
10168f19eb88SIan Dowse 	return (error);
10178f19eb88SIan Dowse }
10188f19eb88SIan Dowse 
10198f19eb88SIan Dowse int
10208f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
10218f19eb88SIan Dowse {
1022b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1023fb99ab88SMatthew Dillon 	int oonstack;
1024fb99ab88SMatthew Dillon 	int error = 0;
1025fb99ab88SMatthew Dillon 
1026fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1027df8bae1dSRodney W. Grimes 
1028b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1029d034d459SMarcel Moolenaar 
10308f19eb88SIan Dowse 	if (oss != NULL) {
1031628d2653SJohn Baldwin 		PROC_LOCK(p);
10328f19eb88SIan Dowse 		*oss = p->p_sigstk;
10338f19eb88SIan Dowse 		oss->ss_flags = (p->p_flag & P_ALTSTACK)
1034d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1035628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1036df8bae1dSRodney W. Grimes 	}
1037d034d459SMarcel Moolenaar 
10388f19eb88SIan Dowse 	if (ss != NULL) {
1039fb99ab88SMatthew Dillon 		if (oonstack) {
1040fb99ab88SMatthew Dillon 			error = EPERM;
1041fb99ab88SMatthew Dillon 			goto done2;
1042fb99ab88SMatthew Dillon 		}
10438f19eb88SIan Dowse 		if ((ss->ss_flags & ~SS_DISABLE) != 0) {
1044fb99ab88SMatthew Dillon 			error = EINVAL;
1045fb99ab88SMatthew Dillon 			goto done2;
1046fb99ab88SMatthew Dillon 		}
10478f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
10488f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1049fb99ab88SMatthew Dillon 				error = ENOMEM;
1050fb99ab88SMatthew Dillon 				goto done2;
1051fb99ab88SMatthew Dillon 			}
1052628d2653SJohn Baldwin 			PROC_LOCK(p);
10538f19eb88SIan Dowse 			p->p_sigstk = *ss;
1054d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
1055628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1056628d2653SJohn Baldwin 		} else {
1057628d2653SJohn Baldwin 			PROC_LOCK(p);
1058d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
1059628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1060628d2653SJohn Baldwin 		}
1061d034d459SMarcel Moolenaar 	}
1062fb99ab88SMatthew Dillon done2:
1063fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1064fb99ab88SMatthew Dillon 	return (error);
1065df8bae1dSRodney W. Grimes }
1066df8bae1dSRodney W. Grimes 
1067d93f860cSPoul-Henning Kamp /*
1068d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1069d93f860cSPoul-Henning Kamp  * cp is calling process.
1070d93f860cSPoul-Henning Kamp  */
107137c84183SPoul-Henning Kamp static int
10729c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
10739c1ab3e0SJohn Baldwin 	register struct thread *td;
10742c42a146SMarcel Moolenaar 	int sig, pgid, all;
1075d93f860cSPoul-Henning Kamp {
1076d93f860cSPoul-Henning Kamp 	register struct proc *p;
1077d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1078d93f860cSPoul-Henning Kamp 	int nfound = 0;
1079d93f860cSPoul-Henning Kamp 
1080553629ebSJake Burkholder 	if (all) {
1081d93f860cSPoul-Henning Kamp 		/*
1082d93f860cSPoul-Henning Kamp 		 * broadcast
1083d93f860cSPoul-Henning Kamp 		 */
10841005a129SJohn Baldwin 		sx_slock(&allproc_lock);
10852e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1086628d2653SJohn Baldwin 			PROC_LOCK(p);
10879c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
10889c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1089628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1090628d2653SJohn Baldwin 				continue;
1091628d2653SJohn Baldwin 			}
1092f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1093d93f860cSPoul-Henning Kamp 				nfound++;
109433a9ed9dSJohn Baldwin 				if (sig)
10952c42a146SMarcel Moolenaar 					psignal(p, sig);
1096628d2653SJohn Baldwin 			}
109733a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1098d93f860cSPoul-Henning Kamp 		}
10991005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1100553629ebSJake Burkholder 	} else {
1101ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1102f591779bSSeigo Tanimura 		if (pgid == 0) {
1103d93f860cSPoul-Henning Kamp 			/*
1104d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1105d93f860cSPoul-Henning Kamp 			 */
11069c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1107f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1108f591779bSSeigo Tanimura 		} else {
1109d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1110f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1111ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1112d93f860cSPoul-Henning Kamp 				return (ESRCH);
1113d93f860cSPoul-Henning Kamp 			}
1114f591779bSSeigo Tanimura 		}
1115ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
11162e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1117628d2653SJohn Baldwin 			PROC_LOCK(p);
1118628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1119628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1120628d2653SJohn Baldwin 				continue;
1121628d2653SJohn Baldwin 			}
1122e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
112333a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1124628d2653SJohn Baldwin 				continue;
1125628d2653SJohn Baldwin 			}
1126f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1127d93f860cSPoul-Henning Kamp 				nfound++;
112833a9ed9dSJohn Baldwin 				if (sig)
11292c42a146SMarcel Moolenaar 					psignal(p, sig);
1130628d2653SJohn Baldwin 			}
113133a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1132d93f860cSPoul-Henning Kamp 		}
1133f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1134d93f860cSPoul-Henning Kamp 	}
1135d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1136d93f860cSPoul-Henning Kamp }
1137d93f860cSPoul-Henning Kamp 
1138d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1139df8bae1dSRodney W. Grimes struct kill_args {
1140df8bae1dSRodney W. Grimes 	int	pid;
1141df8bae1dSRodney W. Grimes 	int	signum;
1142df8bae1dSRodney W. Grimes };
1143d2d3e875SBruce Evans #endif
1144fb99ab88SMatthew Dillon /*
1145fb99ab88SMatthew Dillon  * MPSAFE
1146fb99ab88SMatthew Dillon  */
1147df8bae1dSRodney W. Grimes /* ARGSUSED */
114826f9a767SRodney W. Grimes int
1149b40ce416SJulian Elischer kill(td, uap)
1150b40ce416SJulian Elischer 	register struct thread *td;
1151df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1152df8bae1dSRodney W. Grimes {
1153df8bae1dSRodney W. Grimes 	register struct proc *p;
1154fb99ab88SMatthew Dillon 	int error = 0;
1155df8bae1dSRodney W. Grimes 
11566c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1157df8bae1dSRodney W. Grimes 		return (EINVAL);
1158fb99ab88SMatthew Dillon 
1159fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1160df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1161df8bae1dSRodney W. Grimes 		/* kill single process */
1162fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1163fb99ab88SMatthew Dillon 			error = ESRCH;
1164f44d9e24SJohn Baldwin 		} else if ((error = p_cansignal(td, p, uap->signum)) != 0) {
116533a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1166fb99ab88SMatthew Dillon 		} else {
116733a9ed9dSJohn Baldwin 			if (uap->signum)
1168df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1169628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1170fb99ab88SMatthew Dillon 			error = 0;
1171df8bae1dSRodney W. Grimes 		}
1172fb99ab88SMatthew Dillon 	} else {
1173df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1174df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
11759c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 1);
1176fb99ab88SMatthew Dillon 			break;
1177df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
11789c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 0);
1179fb99ab88SMatthew Dillon 			break;
1180df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
11819c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, -uap->pid, 0);
1182fb99ab88SMatthew Dillon 			break;
1183df8bae1dSRodney W. Grimes 		}
1184fb99ab88SMatthew Dillon 	}
1185fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1186fb99ab88SMatthew Dillon 	return(error);
1187df8bae1dSRodney W. Grimes }
1188df8bae1dSRodney W. Grimes 
1189df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1190d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1191df8bae1dSRodney W. Grimes struct okillpg_args {
1192df8bae1dSRodney W. Grimes 	int	pgid;
1193df8bae1dSRodney W. Grimes 	int	signum;
1194df8bae1dSRodney W. Grimes };
1195d2d3e875SBruce Evans #endif
1196fb99ab88SMatthew Dillon /*
1197fb99ab88SMatthew Dillon  * MPSAFE
1198fb99ab88SMatthew Dillon  */
1199df8bae1dSRodney W. Grimes /* ARGSUSED */
120026f9a767SRodney W. Grimes int
1201b40ce416SJulian Elischer okillpg(td, uap)
1202b40ce416SJulian Elischer 	struct thread *td;
1203df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1204df8bae1dSRodney W. Grimes {
1205fb99ab88SMatthew Dillon 	int error;
1206df8bae1dSRodney W. Grimes 
12076c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1208df8bae1dSRodney W. Grimes 		return (EINVAL);
1209fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
12109c1ab3e0SJohn Baldwin 	error = killpg1(td, uap->signum, uap->pgid, 0);
1211fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1212fb99ab88SMatthew Dillon 	return (error);
1213df8bae1dSRodney W. Grimes }
1214df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1215df8bae1dSRodney W. Grimes 
1216df8bae1dSRodney W. Grimes /*
1217df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1218df8bae1dSRodney W. Grimes  */
1219df8bae1dSRodney W. Grimes void
12202c42a146SMarcel Moolenaar gsignal(pgid, sig)
12212c42a146SMarcel Moolenaar 	int pgid, sig;
1222df8bae1dSRodney W. Grimes {
1223df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1224df8bae1dSRodney W. Grimes 
1225f591779bSSeigo Tanimura 	if (pgid != 0) {
1226ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1227f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1228ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1229f591779bSSeigo Tanimura 		if (pgrp != NULL) {
12302c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1231f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1232f591779bSSeigo Tanimura 		}
1233f591779bSSeigo Tanimura 	}
1234df8bae1dSRodney W. Grimes }
1235df8bae1dSRodney W. Grimes 
1236df8bae1dSRodney W. Grimes /*
1237df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1238df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1239df8bae1dSRodney W. Grimes  */
1240df8bae1dSRodney W. Grimes void
12412c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1242df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
12432c42a146SMarcel Moolenaar 	int sig, checkctty;
1244df8bae1dSRodney W. Grimes {
1245df8bae1dSRodney W. Grimes 	register struct proc *p;
1246df8bae1dSRodney W. Grimes 
1247628d2653SJohn Baldwin 	if (pgrp) {
1248f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1249628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1250628d2653SJohn Baldwin 			PROC_LOCK(p);
1251df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
12522c42a146SMarcel Moolenaar 				psignal(p, sig);
1253628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1254628d2653SJohn Baldwin 		}
1255628d2653SJohn Baldwin 	}
1256df8bae1dSRodney W. Grimes }
1257df8bae1dSRodney W. Grimes 
1258df8bae1dSRodney W. Grimes /*
1259df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1260df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1261df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1262356861dbSMatthew Dillon  *
1263356861dbSMatthew Dillon  * MPSAFE
1264df8bae1dSRodney W. Grimes  */
1265df8bae1dSRodney W. Grimes void
12662c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1267df8bae1dSRodney W. Grimes 	struct proc *p;
12682c42a146SMarcel Moolenaar 	register int sig;
12698674077aSJeffrey Hsu 	u_long code;
1270df8bae1dSRodney W. Grimes {
1271ef3dab76STim J. Robbins 	register struct sigacts *ps;
1272df8bae1dSRodney W. Grimes 
1273628d2653SJohn Baldwin 	PROC_LOCK(p);
1274ef3dab76STim J. Robbins 	ps = p->p_sigacts;
12752c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1276d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1277df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1278df8bae1dSRodney W. Grimes #ifdef KTRACE
1279374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1280374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
12812c42a146SMarcel Moolenaar 			    &p->p_sigmask, code);
1282df8bae1dSRodney W. Grimes #endif
1283a88b260aSJuli Mallett 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
1284a88b260aSJuli Mallett 						&p->p_sigmask, code);
12852c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
12862c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
12872c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
12882c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1289289ccde0SPeter Wemm 			/*
12908f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1291289ccde0SPeter Wemm 			 */
12922c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
12932c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
12942c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
12952c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
12962c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1297dedc04feSPeter Wemm 		}
12986f841fb7SMarcel Moolenaar 	} else {
12996626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
13002c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
13012c42a146SMarcel Moolenaar 		psignal(p, sig);
1302df8bae1dSRodney W. Grimes 	}
1303628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1304df8bae1dSRodney W. Grimes }
1305df8bae1dSRodney W. Grimes 
1306df8bae1dSRodney W. Grimes /*
1307df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1308df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1309df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1310df8bae1dSRodney W. Grimes  *
1311df8bae1dSRodney W. Grimes  * Exceptions:
1312df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1313df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1314df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1315df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1316df8bae1dSRodney W. Grimes  *
1317df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1318df8bae1dSRodney W. Grimes  */
1319df8bae1dSRodney W. Grimes void
13202c42a146SMarcel Moolenaar psignal(p, sig)
1321df8bae1dSRodney W. Grimes 	register struct proc *p;
13222c42a146SMarcel Moolenaar 	register int sig;
1323df8bae1dSRodney W. Grimes {
1324df8bae1dSRodney W. Grimes 	register sig_t action;
1325b40ce416SJulian Elischer 	struct thread *td;
1326e602ba25SJulian Elischer 	register int prop;
1327e602ba25SJulian Elischer 
1328df8bae1dSRodney W. Grimes 
13292899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
13302899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
13312c42a146SMarcel Moolenaar 
1332628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1333cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1334cb679c38SJonathan Lemon 
13352c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1336df8bae1dSRodney W. Grimes 	/*
13372a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
13382a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
13392a024a2bSSean Eric Fagan 	 * a chance, as well.
1340df8bae1dSRodney W. Grimes 	 */
1341b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1342df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1343b40ce416SJulian Elischer 	} else {
1344df8bae1dSRodney W. Grimes 		/*
1345df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1346df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1347df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1348df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1349df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1350df8bae1dSRodney W. Grimes 		 */
1351628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1352df8bae1dSRodney W. Grimes 			return;
13532c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1354df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
13552c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1356df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1357df8bae1dSRodney W. Grimes 		else
1358df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1359df8bae1dSRodney W. Grimes 	}
1360df8bae1dSRodney W. Grimes 
1361df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
13621d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
1363df8bae1dSRodney W. Grimes 
1364df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1365df8bae1dSRodney W. Grimes 		/*
1366df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1367df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1368df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1369df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1370df8bae1dSRodney W. Grimes 		 */
1371e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1372e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1373e602ba25SJulian Elischer 		    (action == SIG_DFL))
1374df8bae1dSRodney W. Grimes 		        return;
13751d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
13766933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1377df8bae1dSRodney W. Grimes 	}
13781d9c5696SJuli Mallett 	SIGADDSET(p->p_siglist, sig);
1379aa0fa334SJulian Elischer 	signotify(p);			/* uses schedlock */
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes 	/*
1382e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1383e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1384e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1385e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1386e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1387e602ba25SJulian Elischer 	 * We try do the per-process part here.
1388df8bae1dSRodney W. Grimes 	 */
1389e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1390e602ba25SJulian Elischer 		/*
1391e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1392e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1393e602ba25SJulian Elischer 		 */
1394e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1395e602ba25SJulian Elischer 			/*
1396e602ba25SJulian Elischer 			 * The traced process is already stopped,
1397e602ba25SJulian Elischer 			 * so no further action is necessary.
1398e602ba25SJulian Elischer 			 * No signal can restart us.
1399e602ba25SJulian Elischer 			 */
1400e602ba25SJulian Elischer 			goto out;
14011c32c37cSJohn Baldwin 		}
1402b40ce416SJulian Elischer 
1403e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1404df8bae1dSRodney W. Grimes 			/*
1405e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1406e602ba25SJulian Elischer 			 * It will die elsewhere.
1407e602ba25SJulian Elischer 			 * All threads must be restarted.
1408df8bae1dSRodney W. Grimes 			 */
1409e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1410e602ba25SJulian Elischer 			goto runfast;
1411e602ba25SJulian Elischer 		}
1412e602ba25SJulian Elischer 
1413e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1414e602ba25SJulian Elischer 			/*
1415e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
14161d9c5696SJuli Mallett 			 * process but don't leave the signal in p_siglist as
14171d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1418e602ba25SJulian Elischer 			 * continue the process and leave the signal in
14191d9c5696SJuli Mallett 			 * p_siglist.  If the process catches SIGCONT, let it
1420e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1421e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1422e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1423e602ba25SJulian Elischer 			 */
14241279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
14256933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1426e602ba25SJulian Elischer 			if (action == SIG_DFL) {
14271d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
1428e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1429e602ba25SJulian Elischer 				/*
1430e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1431e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1432e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1433e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1434e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1435e602ba25SJulian Elischer 				 * process, we need to make sure that the
1436e602ba25SJulian Elischer 				 * single thread is runnable asap.
1437e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1438e602ba25SJulian Elischer 				 */
1439e602ba25SJulian Elischer 				goto runfast;
1440e602ba25SJulian Elischer 			}
1441e602ba25SJulian Elischer 			/*
1442e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1443e602ba25SJulian Elischer 			 */
1444e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
144504774f23SJulian Elischer 			thread_unsuspend(p);
1446e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1447e602ba25SJulian Elischer 			goto out;
1448e602ba25SJulian Elischer 		}
1449e602ba25SJulian Elischer 
1450e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1451e602ba25SJulian Elischer 			/*
1452e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1453e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
145404774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1455e602ba25SJulian Elischer 			 */
14561279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
14571d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
1458e602ba25SJulian Elischer 			goto out;
1459e602ba25SJulian Elischer 		}
1460e602ba25SJulian Elischer 
1461e602ba25SJulian Elischer 		/*
1462e602ba25SJulian Elischer 		 * All other kinds of signals:
1463e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1464e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1465e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
146604774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1467e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1468e602ba25SJulian Elischer 		 */
1469e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
1470e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
147171fad9fdSJulian Elischer 			if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1472e602ba25SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
147371fad9fdSJulian Elischer 					cv_abort(td);
1474e602ba25SJulian Elischer 				else
147571fad9fdSJulian Elischer 					abortsleep(td);
1476e602ba25SJulian Elischer 			}
1477e602ba25SJulian Elischer 		}
1478e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1479df8bae1dSRodney W. Grimes 		goto out;
1480df8bae1dSRodney W. Grimes 		/*
1481e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1482e602ba25SJulian Elischer 		 * Shouldn't they abort too?
148304774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
148404774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1485df8bae1dSRodney W. Grimes 		 */
1486e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
148735c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
148835c32a76SDavid Xu 			!(prop & SA_STOP)) {
1489721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
1490721e5910SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td)
1491721e5910SJulian Elischer 				tdsignal(td, sig, action);
1492721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1493df8bae1dSRodney W. Grimes 			goto out;
149404774f23SJulian Elischer 		}
149535c32a76SDavid Xu 		if (prop & SA_STOP) {
149635c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
149735c32a76SDavid Xu 				goto out;
149835c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
149935c32a76SDavid Xu 			FOREACH_THREAD_IN_PROC(p, td) {
150071fad9fdSJulian Elischer 				if (TD_IS_SLEEPING(td) &&
150135c32a76SDavid Xu 					(td->td_flags & TDF_SINTR))
150235c32a76SDavid Xu 					thread_suspend_one(td);
150335c32a76SDavid Xu 			}
150435c32a76SDavid Xu 			if (p->p_suspcount == p->p_numthreads) {
150535c32a76SDavid Xu 				mtx_unlock_spin(&sched_lock);
150635c32a76SDavid Xu 				stop(p);
150735c32a76SDavid Xu 				p->p_xstat = sig;
15081d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
150935c32a76SDavid Xu 				PROC_LOCK(p->p_pptr);
151035c32a76SDavid Xu 				if ((p->p_pptr->p_procsig->ps_flag &
151135c32a76SDavid Xu 					PS_NOCLDSTOP) == 0) {
151235c32a76SDavid Xu 					psignal(p->p_pptr, SIGCHLD);
151335c32a76SDavid Xu 				}
151435c32a76SDavid Xu 				PROC_UNLOCK(p->p_pptr);
151535c32a76SDavid Xu 			} else {
151635c32a76SDavid Xu 				mtx_unlock_spin(&sched_lock);
151735c32a76SDavid Xu 			}
151835c32a76SDavid Xu 			goto out;
151935c32a76SDavid Xu 		}
1520721e5910SJulian Elischer 		else
1521df8bae1dSRodney W. Grimes 			goto runfast;
1522df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1523e602ba25SJulian Elischer 	} else {
1524e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
15251d9c5696SJuli Mallett 		SIGDELSET(p->p_siglist, sig);
1526e602ba25SJulian Elischer 		goto out;
1527e602ba25SJulian Elischer 	}
1528e602ba25SJulian Elischer 
1529b40ce416SJulian Elischer 	/*
1530e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1531e602ba25SJulian Elischer 	 * running threads.
1532b40ce416SJulian Elischer 	 */
1533e602ba25SJulian Elischer 
1534e602ba25SJulian Elischer runfast:
1535aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
1536e602ba25SJulian Elischer 	FOREACH_THREAD_IN_PROC(p, td)
1537e602ba25SJulian Elischer 		tdsignal(td, sig, action);
1538e602ba25SJulian Elischer 	thread_unsuspend(p);
1539e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1540e602ba25SJulian Elischer out:
1541e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1542e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1543e602ba25SJulian Elischer }
1544e602ba25SJulian Elischer 
1545e602ba25SJulian Elischer /*
1546e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1547e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1548e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1549e602ba25SJulian Elischer  */
1550e602ba25SJulian Elischer static void
1551e602ba25SJulian Elischer tdsignal(struct thread *td, int sig, sig_t action)
1552e602ba25SJulian Elischer {
1553e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1554e602ba25SJulian Elischer 	register int prop;
1555e602ba25SJulian Elischer 
1556aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1557e602ba25SJulian Elischer 	prop = sigprop(sig);
1558e602ba25SJulian Elischer 	/*
1559aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1560e602ba25SJulian Elischer 	 * killed in this lifetime.
1561e602ba25SJulian Elischer 	 */
1562e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1563e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1564e602ba25SJulian Elischer 			td->td_priority = PUSER;
1565b40ce416SJulian Elischer 		}
1566b40ce416SJulian Elischer 	}
1567e602ba25SJulian Elischer 
1568e602ba25SJulian Elischer 	/*
1569e602ba25SJulian Elischer 	 * Defer further processing for signals which are held,
1570e602ba25SJulian Elischer 	 * except that stopped processes must be continued by SIGCONT.
1571e602ba25SJulian Elischer 	 */
1572e602ba25SJulian Elischer 	if (action == SIG_HOLD) {
1573aa0fa334SJulian Elischer 		return;
1574e602ba25SJulian Elischer 	}
157571fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1576e602ba25SJulian Elischer 		/*
1577e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1578e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1579e602ba25SJulian Elischer 		 * be noticed when the process returns through
1580e602ba25SJulian Elischer 		 * trap() or syscall().
1581e602ba25SJulian Elischer 		 */
1582e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1583aa0fa334SJulian Elischer 			return;
1584e602ba25SJulian Elischer 		}
1585e602ba25SJulian Elischer 		/*
1586e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1587e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1588e602ba25SJulian Elischer 		 * for its parent.
1589e602ba25SJulian Elischer 		 */
1590e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1591e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1592aa0fa334SJulian Elischer 		} else {
1593aa0fa334SJulian Elischer 
1594df8bae1dSRodney W. Grimes 			/*
1595e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1596e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1597e602ba25SJulian Elischer 			 * be awakened.
1598df8bae1dSRodney W. Grimes 			 */
1599e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
16001d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
1601aa0fa334SJulian Elischer 				return;
1602df8bae1dSRodney W. Grimes 			}
1603df8bae1dSRodney W. Grimes 
1604aa0fa334SJulian Elischer 			/*
1605aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1606aa0fa334SJulian Elischer 			 */
1607aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
1608aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1609aa0fa334SJulian Elischer 			}
1610aa0fa334SJulian Elischer 		}
161171fad9fdSJulian Elischer 		if (td->td_flags & TDF_CVWAITQ)
161271fad9fdSJulian Elischer 			cv_abort(td);
161371fad9fdSJulian Elischer 		else
161471fad9fdSJulian Elischer 			abortsleep(td);
1615aa0fa334SJulian Elischer 	}
1616aa0fa334SJulian Elischer #ifdef SMP
1617aa0fa334SJulian Elischer 	  else {
1618df8bae1dSRodney W. Grimes 		/*
1619e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
1620df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1621df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1622df8bae1dSRodney W. Grimes 		 */
162371fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td) && td != curthread) {
1624e602ba25SJulian Elischer 			forward_signal(td);
1625aa0fa334SJulian Elischer 		}
16260ac3b636SAndrew Gallatin 	  }
16273163861cSTor Egge #endif
16286caa8a15SJohn Baldwin }
1629df8bae1dSRodney W. Grimes 
1630df8bae1dSRodney W. Grimes /*
1631df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1632df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1633df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1634df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1635df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1636628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
1637df8bae1dSRodney W. Grimes  * sequence is
1638df8bae1dSRodney W. Grimes  *
1639e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
16402c42a146SMarcel Moolenaar  *		postsig(sig);
1641df8bae1dSRodney W. Grimes  */
164226f9a767SRodney W. Grimes int
1643e602ba25SJulian Elischer issignal(td)
1644e602ba25SJulian Elischer 	struct thread *td;
1645df8bae1dSRodney W. Grimes {
1646e602ba25SJulian Elischer 	struct proc *p;
16472c42a146SMarcel Moolenaar 	sigset_t mask;
16482c42a146SMarcel Moolenaar 	register int sig, prop;
1649df8bae1dSRodney W. Grimes 
1650e602ba25SJulian Elischer 	p = td->td_proc;
1651628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
165226306795SJohn Baldwin 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &p->p_mtx.mtx_object,
165326306795SJohn Baldwin 	    "Checking for signals");
1654df8bae1dSRodney W. Grimes 	for (;;) {
16552a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
16562a024a2bSSean Eric Fagan 
16571d9c5696SJuli Mallett 		mask = p->p_siglist;
16582c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1659df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
16602c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
1661ca18d53eSChad David 		if (SIGISEMPTY(mask))		/* no signal to send */
1662df8bae1dSRodney W. Grimes 			return (0);
16631d9c5696SJuli Mallett 		sig = sig_ffs(&mask);
16642c42a146SMarcel Moolenaar 		prop = sigprop(sig);
16652a024a2bSSean Eric Fagan 
1666628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
16672a024a2bSSean Eric Fagan 
1668df8bae1dSRodney W. Grimes 		/*
1669df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1670df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1671df8bae1dSRodney W. Grimes 		 */
16722c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
16731d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
1674df8bae1dSRodney W. Grimes 			continue;
1675df8bae1dSRodney W. Grimes 		}
1676df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1677df8bae1dSRodney W. Grimes 			/*
1678d8f4f6a4SJonathan Mini 			 * If traced, always stop.
1679df8bae1dSRodney W. Grimes 			 */
16802c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1681628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1682df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1683628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
16849ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
16854d492b43SJulian Elischer 			stop(p);	/* uses schedlock too eventually */
168671fad9fdSJulian Elischer 			thread_suspend_one(td);
1687c86b6ff5SJohn Baldwin 			PROC_UNLOCK(p);
1688c86b6ff5SJohn Baldwin 			DROP_GIANT();
16892ad7d304SJohn Baldwin 			p->p_stats->p_ru.ru_nivcsw++;
1690df8bae1dSRodney W. Grimes 			mi_switch();
16919ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
169220cdcc5bSJohn Baldwin 			PICKUP_GIANT();
1693628d2653SJohn Baldwin 			PROC_LOCK(p);
1694df8bae1dSRodney W. Grimes 
1695df8bae1dSRodney W. Grimes 			/*
1696df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1697df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1698df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1699df8bae1dSRodney W. Grimes 			 */
1700df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1701df8bae1dSRodney W. Grimes 				continue;
1702df8bae1dSRodney W. Grimes 
1703df8bae1dSRodney W. Grimes 			/*
1704df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1705df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1706df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1707df8bae1dSRodney W. Grimes 			 */
17081d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
17092c42a146SMarcel Moolenaar 			sig = p->p_xstat;
17102c42a146SMarcel Moolenaar 			if (sig == 0)
1711df8bae1dSRodney W. Grimes 				continue;
1712df8bae1dSRodney W. Grimes 
1713df8bae1dSRodney W. Grimes 			/*
17141d9c5696SJuli Mallett 			 * Put the new signal into p_siglist.  If the
17151d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
1716df8bae1dSRodney W. Grimes 			 */
17171d9c5696SJuli Mallett 			SIGADDSET(p->p_siglist, sig);
17182c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1719df8bae1dSRodney W. Grimes 				continue;
17201c530be4SBruce Evans 			signotify(p);
1721df8bae1dSRodney W. Grimes 		}
1722df8bae1dSRodney W. Grimes 
1723df8bae1dSRodney W. Grimes 		/*
1724df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1725df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1726df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1727df8bae1dSRodney W. Grimes 		 */
1728d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1729df8bae1dSRodney W. Grimes 
1730d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
1731df8bae1dSRodney W. Grimes 			/*
1732df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1733df8bae1dSRodney W. Grimes 			 */
1734df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1735df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1736df8bae1dSRodney W. Grimes 				/*
1737df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1738df8bae1dSRodney W. Grimes 				 * in init? XXX
1739df8bae1dSRodney W. Grimes 				 */
1740d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
17412c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1742df8bae1dSRodney W. Grimes #endif
1743df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1744df8bae1dSRodney W. Grimes 			}
1745df8bae1dSRodney W. Grimes 			/*
1746df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1747df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1748df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1749df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1750df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1751df8bae1dSRodney W. Grimes 			 */
1752df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1753df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1754df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1755df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
1756df8bae1dSRodney W. Grimes 					break;	/* == ignore */
17572c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1758721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
1759721e5910SJulian Elischer 				if (p->p_suspcount+1 == p->p_numthreads) {
1760721e5910SJulian Elischer 					mtx_unlock_spin(&sched_lock);
1761628d2653SJohn Baldwin 					PROC_LOCK(p->p_pptr);
1762e602ba25SJulian Elischer 					if ((p->p_pptr->p_procsig->ps_flag &
1763e602ba25SJulian Elischer 				    		PS_NOCLDSTOP) == 0) {
1764df8bae1dSRodney W. Grimes 						psignal(p->p_pptr, SIGCHLD);
1765e602ba25SJulian Elischer 					}
1766628d2653SJohn Baldwin 					PROC_UNLOCK(p->p_pptr);
1767d9d6e34fSJulian Elischer 					mtx_lock_spin(&sched_lock);
1768721e5910SJulian Elischer 				}
17695b3047d5SJohn Baldwin 				stop(p);
177071fad9fdSJulian Elischer 				thread_suspend_one(td);
1771721e5910SJulian Elischer 				PROC_UNLOCK(p);
1772721e5910SJulian Elischer 				DROP_GIANT();
1773721e5910SJulian Elischer 				p->p_stats->p_ru.ru_nivcsw++;
1774721e5910SJulian Elischer 				mi_switch();
1775721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
1776721e5910SJulian Elischer 				PICKUP_GIANT();
1777721e5910SJulian Elischer 				PROC_LOCK(p);
1778df8bae1dSRodney W. Grimes 				break;
177921b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
1780df8bae1dSRodney W. Grimes 				/*
1781df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1782df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1783df8bae1dSRodney W. Grimes 				 */
1784df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1785df8bae1dSRodney W. Grimes 			} else
17862c42a146SMarcel Moolenaar 				return (sig);
1787df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1788df8bae1dSRodney W. Grimes 
1789d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
1790df8bae1dSRodney W. Grimes 			/*
1791df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1792df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1793df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1794df8bae1dSRodney W. Grimes 			 */
1795df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1796df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1797df8bae1dSRodney W. Grimes 				printf("issignal\n");
1798df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1799df8bae1dSRodney W. Grimes 
1800df8bae1dSRodney W. Grimes 		default:
1801df8bae1dSRodney W. Grimes 			/*
1802df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1803df8bae1dSRodney W. Grimes 			 * postsig() process it.
1804df8bae1dSRodney W. Grimes 			 */
18052c42a146SMarcel Moolenaar 			return (sig);
1806df8bae1dSRodney W. Grimes 		}
18071d9c5696SJuli Mallett 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1808df8bae1dSRodney W. Grimes 	}
1809df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1810df8bae1dSRodney W. Grimes }
1811df8bae1dSRodney W. Grimes 
1812df8bae1dSRodney W. Grimes /*
1813df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1814df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
18155b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
18165b3047d5SJohn Baldwin  * lock held.
1817df8bae1dSRodney W. Grimes  */
18185b3047d5SJohn Baldwin static void
1819df8bae1dSRodney W. Grimes stop(p)
1820df8bae1dSRodney W. Grimes 	register struct proc *p;
1821df8bae1dSRodney W. Grimes {
1822df8bae1dSRodney W. Grimes 
1823628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18241279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
1825df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
182601609114SAlfred Perlstein 	wakeup(p->p_pptr);
1827df8bae1dSRodney W. Grimes }
1828df8bae1dSRodney W. Grimes 
1829df8bae1dSRodney W. Grimes /*
1830df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1831df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1832df8bae1dSRodney W. Grimes  */
1833df8bae1dSRodney W. Grimes void
18342c42a146SMarcel Moolenaar postsig(sig)
18352c42a146SMarcel Moolenaar 	register int sig;
1836df8bae1dSRodney W. Grimes {
1837b40ce416SJulian Elischer 	struct thread *td = curthread;
1838b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1839628d2653SJohn Baldwin 	struct sigacts *ps;
18402c42a146SMarcel Moolenaar 	sig_t action;
18412c42a146SMarcel Moolenaar 	sigset_t returnmask;
18422c42a146SMarcel Moolenaar 	int code;
1843df8bae1dSRodney W. Grimes 
18442c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
18455526d2d9SEivind Eklund 
18462ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1847628d2653SJohn Baldwin 	ps = p->p_sigacts;
18481d9c5696SJuli Mallett 	SIGDELSET(p->p_siglist, sig);
18492c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1850df8bae1dSRodney W. Grimes #ifdef KTRACE
1851374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
1852374a15aaSJohn Baldwin 		ktrpsig(sig, action, p->p_flag & P_OLDMASK ?
18532c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1854df8bae1dSRodney W. Grimes #endif
1855628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
18562a024a2bSSean Eric Fagan 
1857df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1858df8bae1dSRodney W. Grimes 		/*
1859df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1860df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1861df8bae1dSRodney W. Grimes 		 */
1862b40ce416SJulian Elischer 		sigexit(td, sig);
1863df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1864df8bae1dSRodney W. Grimes 	} else {
1865df8bae1dSRodney W. Grimes 		/*
1866df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1867df8bae1dSRodney W. Grimes 		 */
18682c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
18695526d2d9SEivind Eklund 		    ("postsig action"));
1870df8bae1dSRodney W. Grimes 		/*
1871df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1872645682fdSLuoqi Chen 		 * occurrences of this signal.
1873df8bae1dSRodney W. Grimes 		 *
1874645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1875df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1876645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1877df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1878df8bae1dSRodney W. Grimes 		 */
1879645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
18806626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1881645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1882df8bae1dSRodney W. Grimes 		} else
1883df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
18842c42a146SMarcel Moolenaar 
18852c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
18862c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
18872c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
18882c42a146SMarcel Moolenaar 
18892c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1890289ccde0SPeter Wemm 			/*
18918f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1892289ccde0SPeter Wemm 			 */
18932c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
18942c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
18952c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
18962c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
18972c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1898dedc04feSPeter Wemm 		}
1899df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
19002c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1901df8bae1dSRodney W. Grimes 			code = 0;
1902df8bae1dSRodney W. Grimes 		} else {
19036626c604SJulian Elischer 			code = p->p_code;
19046626c604SJulian Elischer 			p->p_code = 0;
19056626c604SJulian Elischer 			p->p_sig = 0;
1906df8bae1dSRodney W. Grimes 		}
1907ac2e4153SJulian Elischer 		if (p->p_flag & P_THREADED)
190858a3c273SJeff Roberson 			thread_signal_add(curthread, sig);
190958a3c273SJeff Roberson 		else
191058a3c273SJeff Roberson 			(*p->p_sysent->sv_sendsig)(action, sig,
191158a3c273SJeff Roberson 			    &returnmask, code);
1912df8bae1dSRodney W. Grimes 	}
1913df8bae1dSRodney W. Grimes }
1914df8bae1dSRodney W. Grimes 
1915df8bae1dSRodney W. Grimes /*
1916df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1917df8bae1dSRodney W. Grimes  */
191826f9a767SRodney W. Grimes void
1919df8bae1dSRodney W. Grimes killproc(p, why)
1920df8bae1dSRodney W. Grimes 	struct proc *p;
1921df8bae1dSRodney W. Grimes 	char *why;
1922df8bae1dSRodney W. Grimes {
19239081e5e8SJohn Baldwin 
19249081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
19250384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
19260384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1927729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1928b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1929df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1930df8bae1dSRodney W. Grimes }
1931df8bae1dSRodney W. Grimes 
1932df8bae1dSRodney W. Grimes /*
1933df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1934df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1935df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1936df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1937df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1938df8bae1dSRodney W. Grimes  * does not return.
1939df8bae1dSRodney W. Grimes  */
194026f9a767SRodney W. Grimes void
1941b40ce416SJulian Elischer sigexit(td, sig)
1942b40ce416SJulian Elischer 	struct thread *td;
19432c42a146SMarcel Moolenaar 	int sig;
1944df8bae1dSRodney W. Grimes {
1945b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1946df8bae1dSRodney W. Grimes 
1947628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1948df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
19492c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
19502c42a146SMarcel Moolenaar 		p->p_sig = sig;
1951c364e17eSAndrey A. Chernov 		/*
1952c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1953c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1954c364e17eSAndrey A. Chernov 		 * these messages.)
1955c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1956c364e17eSAndrey A. Chernov 		 */
1957628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1958c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1959c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1960b40ce416SJulian Elischer 		if (coredump(td) == 0)
19612c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
196257308494SJoerg Wunsch 		if (kern_logsigexit)
196357308494SJoerg Wunsch 			log(LOG_INFO,
196457308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
19653d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
19669c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
19672c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
19682c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1969c31146a1SJohn Baldwin 	} else {
1970628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1971628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1972628d2653SJohn Baldwin 			mtx_lock(&Giant);
1973c31146a1SJohn Baldwin 	}
1974b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1975df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1976df8bae1dSRodney W. Grimes }
1977df8bae1dSRodney W. Grimes 
1978c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1979c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1980c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
1981c5edb423SSean Eric Fagan 
1982c5edb423SSean Eric Fagan /*
1983c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
1984c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
1985c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
1986c5edb423SSean Eric Fagan  *	%N	name of process ("name")
1987c5edb423SSean Eric Fagan  *	%P	process id (pid)
1988c5edb423SSean Eric Fagan  *	%U	user id (uid)
1989c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
1990c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1991c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
1992c5edb423SSean Eric Fagan  */
1993c5edb423SSean Eric Fagan 
1994fca666a1SJulian Elischer static char *
1995c5edb423SSean Eric Fagan expand_name(name, uid, pid)
19968b43b535SAlfred Perlstein 	const char *name;
19978b43b535SAlfred Perlstein 	uid_t uid;
19988b43b535SAlfred Perlstein 	pid_t pid;
19998b43b535SAlfred Perlstein {
20008b43b535SAlfred Perlstein 	const char *format, *appendstr;
2001c5edb423SSean Eric Fagan 	char *temp;
2002c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
20038b43b535SAlfred Perlstein 	size_t i, l, n;
2004c5edb423SSean Eric Fagan 
20058b43b535SAlfred Perlstein 	format = corefilename;
20068b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
20070bfe2990SEivind Eklund 	if (temp == NULL)
20088b43b535SAlfred Perlstein 		return (NULL);
2009ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2010c5edb423SSean Eric Fagan 		switch (format[i]) {
2011c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2012c5edb423SSean Eric Fagan 			i++;
2013c5edb423SSean Eric Fagan 			switch (format[i]) {
2014c5edb423SSean Eric Fagan 			case '%':
20158b43b535SAlfred Perlstein 				appendstr = "%";
2016c5edb423SSean Eric Fagan 				break;
2017c5edb423SSean Eric Fagan 			case 'N':	/* process name */
20188b43b535SAlfred Perlstein 				appendstr = name;
2019c5edb423SSean Eric Fagan 				break;
2020c5edb423SSean Eric Fagan 			case 'P':	/* process id */
20218b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
20228b43b535SAlfred Perlstein 				appendstr = buf;
2023c5edb423SSean Eric Fagan 				break;
2024c5edb423SSean Eric Fagan 			case 'U':	/* user id */
20258b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
20268b43b535SAlfred Perlstein 				appendstr = buf;
2027c5edb423SSean Eric Fagan 				break;
2028c5edb423SSean Eric Fagan 			default:
20298b43b535SAlfred Perlstein 				appendstr = "";
20308b43b535SAlfred Perlstein 			  	log(LOG_ERR,
20318b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
20328b43b535SAlfred Perlstein 				    format[i], format);
2033c5edb423SSean Eric Fagan 			}
20348b43b535SAlfred Perlstein 			l = strlen(appendstr);
20358b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
20368b43b535SAlfred Perlstein 				goto toolong;
20378b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
20388b43b535SAlfred Perlstein 			n += l;
2039c5edb423SSean Eric Fagan 			break;
2040c5edb423SSean Eric Fagan 		default:
2041c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2042c5edb423SSean Eric Fagan 		}
2043c5edb423SSean Eric Fagan 	}
20448b43b535SAlfred Perlstein 	if (format[i] != '\0')
20458b43b535SAlfred Perlstein 		goto toolong;
20468b43b535SAlfred Perlstein 	return (temp);
20478b43b535SAlfred Perlstein toolong:
20488b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
20498b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
20508b43b535SAlfred Perlstein 	free(temp, M_TEMP);
20518b43b535SAlfred Perlstein 	return (NULL);
2052c5edb423SSean Eric Fagan }
2053c5edb423SSean Eric Fagan 
2054df8bae1dSRodney W. Grimes /*
2055fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2056fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2057fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2058fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2059fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2060fca666a1SJulian Elischer  */
2061fca666a1SJulian Elischer 
2062fca666a1SJulian Elischer static int
2063b40ce416SJulian Elischer coredump(struct thread *td)
2064fca666a1SJulian Elischer {
2065b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2066fca666a1SJulian Elischer 	register struct vnode *vp;
20679c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
206806ae1e91SMatthew Dillon 	struct flock lf;
2069fca666a1SJulian Elischer 	struct nameidata nd;
2070fca666a1SJulian Elischer 	struct vattr vattr;
2071e6796b67SKirk McKusick 	int error, error1, flags;
2072f2a2857bSKirk McKusick 	struct mount *mp;
2073fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2074fca666a1SJulian Elischer 	off_t limit;
2075fca666a1SJulian Elischer 
2076628d2653SJohn Baldwin 	PROC_LOCK(p);
2077628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2078fca666a1SJulian Elischer 
2079628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2080628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2081fca666a1SJulian Elischer 		return (EFAULT);
2082628d2653SJohn Baldwin 	}
2083fca666a1SJulian Elischer 
2084fca666a1SJulian Elischer 	/*
208535a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
208635a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
208735a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
208835a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
208935a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
209035a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2091fca666a1SJulian Elischer 	 */
209235a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2093628d2653SJohn Baldwin 	if (limit == 0) {
2094628d2653SJohn Baldwin 		PROC_UNLOCK(p);
209535a2598fSSean Eric Fagan 		return 0;
2096628d2653SJohn Baldwin 	}
2097628d2653SJohn Baldwin 	PROC_UNLOCK(p);
209835a2598fSSean Eric Fagan 
2099f2a2857bSKirk McKusick restart:
21009c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2101ccdbd10cSPeter Pentchev 	if (name == NULL)
2102ccdbd10cSPeter Pentchev 		return (EINVAL);
2103b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2104e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2105e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2106fca666a1SJulian Elischer 	free(name, M_TEMP);
2107fca666a1SJulian Elischer 	if (error)
2108fca666a1SJulian Elischer 		return (error);
2109762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2110fca666a1SJulian Elischer 	vp = nd.ni_vp;
211106ae1e91SMatthew Dillon 
2112832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2113832dafadSDon Lewis 	if (vp->v_type != VREG ||
2114832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2115832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2116832dafadSDon Lewis 		error = EFAULT;
2117832dafadSDon Lewis 		goto out2;
2118832dafadSDon Lewis 	}
2119832dafadSDon Lewis 
2120b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
212106ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
212206ae1e91SMatthew Dillon 	lf.l_start = 0;
212306ae1e91SMatthew Dillon 	lf.l_len = 0;
212406ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
212506ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
212606ae1e91SMatthew Dillon 	if (error)
212706ae1e91SMatthew Dillon 		goto out2;
212806ae1e91SMatthew Dillon 
212906ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
213006ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
213106ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2132b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2133f2a2857bSKirk McKusick 			return (error);
2134f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2135f2a2857bSKirk McKusick 			return (error);
2136f2a2857bSKirk McKusick 		goto restart;
2137f2a2857bSKirk McKusick 	}
2138fca666a1SJulian Elischer 
2139fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2140fca666a1SJulian Elischer 	vattr.va_size = 0;
214188b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2142b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2143b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
214488b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2145628d2653SJohn Baldwin 	PROC_LOCK(p);
2146fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2147628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2148fca666a1SJulian Elischer 
2149fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2150b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2151fca666a1SJulian Elischer 	  ENOSYS;
2152fca666a1SJulian Elischer 
215306ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
215406ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2155f2a2857bSKirk McKusick 	vn_finished_write(mp);
215606ae1e91SMatthew Dillon out2:
2157b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2158fca666a1SJulian Elischer 	if (error == 0)
2159fca666a1SJulian Elischer 		error = error1;
2160fca666a1SJulian Elischer 	return (error);
2161fca666a1SJulian Elischer }
2162fca666a1SJulian Elischer 
2163fca666a1SJulian Elischer /*
2164df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2165df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2166df8bae1dSRodney W. Grimes  */
2167d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2168df8bae1dSRodney W. Grimes struct nosys_args {
2169df8bae1dSRodney W. Grimes 	int	dummy;
2170df8bae1dSRodney W. Grimes };
2171d2d3e875SBruce Evans #endif
2172fb99ab88SMatthew Dillon /*
2173fb99ab88SMatthew Dillon  * MPSAFE
2174fb99ab88SMatthew Dillon  */
2175df8bae1dSRodney W. Grimes /* ARGSUSED */
217626f9a767SRodney W. Grimes int
2177b40ce416SJulian Elischer nosys(td, args)
2178b40ce416SJulian Elischer 	struct thread *td;
2179df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2180df8bae1dSRodney W. Grimes {
2181b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2182b40ce416SJulian Elischer 
2183fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2184628d2653SJohn Baldwin 	PROC_LOCK(p);
2185df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2186628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2187fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2188f5216b9aSBruce Evans 	return (ENOSYS);
2189df8bae1dSRodney W. Grimes }
2190831d27a9SDon Lewis 
2191831d27a9SDon Lewis /*
219248f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2193831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2194831d27a9SDon Lewis  */
2195831d27a9SDon Lewis void
2196f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2197f1320723SAlfred Perlstein 	struct sigio **sigiop;
21982c42a146SMarcel Moolenaar 	int sig, checkctty;
2199831d27a9SDon Lewis {
2200f1320723SAlfred Perlstein 	struct sigio *sigio;
2201831d27a9SDon Lewis 
2202f1320723SAlfred Perlstein 	SIGIO_LOCK();
2203f1320723SAlfred Perlstein 	sigio = *sigiop;
2204f1320723SAlfred Perlstein 	if (sigio == NULL) {
2205f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2206f1320723SAlfred Perlstein 		return;
2207f1320723SAlfred Perlstein 	}
2208831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2209628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
22102b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
22112c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2212628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2213831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2214831d27a9SDon Lewis 		struct proc *p;
2215831d27a9SDon Lewis 
2216f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2217628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2218628d2653SJohn Baldwin 			PROC_LOCK(p);
22192b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2220831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
22212c42a146SMarcel Moolenaar 				psignal(p, sig);
2222628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2223628d2653SJohn Baldwin 		}
2224f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2225831d27a9SDon Lewis 	}
2226f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2227831d27a9SDon Lewis }
2228cb679c38SJonathan Lemon 
2229cb679c38SJonathan Lemon static int
2230cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2231cb679c38SJonathan Lemon {
2232cb679c38SJonathan Lemon 	struct proc *p = curproc;
2233cb679c38SJonathan Lemon 
2234cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2235cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2236cb679c38SJonathan Lemon 
2237628d2653SJohn Baldwin 	PROC_LOCK(p);
2238cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2239628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2240cb679c38SJonathan Lemon 
2241cb679c38SJonathan Lemon 	return (0);
2242cb679c38SJonathan Lemon }
2243cb679c38SJonathan Lemon 
2244cb679c38SJonathan Lemon static void
2245cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2246cb679c38SJonathan Lemon {
2247cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2248cb679c38SJonathan Lemon 
2249628d2653SJohn Baldwin 	PROC_LOCK(p);
2250e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2251628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2252cb679c38SJonathan Lemon }
2253cb679c38SJonathan Lemon 
2254cb679c38SJonathan Lemon /*
2255cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2256cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2257cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2258cb679c38SJonathan Lemon  * isn't worth the trouble.
2259cb679c38SJonathan Lemon  */
2260cb679c38SJonathan Lemon static int
2261cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2262cb679c38SJonathan Lemon {
2263cb679c38SJonathan Lemon 
2264cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2265cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2266cb679c38SJonathan Lemon 
2267cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2268cb679c38SJonathan Lemon 			kn->kn_data++;
2269cb679c38SJonathan Lemon 	}
2270cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2271cb679c38SJonathan Lemon }
2272