xref: /freebsd/sys/kern/kern_sig.c (revision 23eeeff7bee66bd81fa2d7ec9d648339bd0d041f)
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(COMPAT_FREEBSD4) && !defined(NO_COMPAT_FREEBSD4)
7723eeeff7SPeter Wemm #error "You *really* want COMPAT_FREEBSD4 on -current for a while"
7823eeeff7SPeter Wemm #endif
7923eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
8023eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
8123eeeff7SPeter Wemm #endif
8223eeeff7SPeter Wemm 
836f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
846f841fb7SMarcel Moolenaar 
854d77a549SAlfred Perlstein static int	coredump(struct thread *);
864d77a549SAlfred Perlstein static int	do_sigprocmask(struct proc *p, int how, sigset_t *set,
874d77a549SAlfred Perlstein 			sigset_t *oset, int old);
884d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
899c1ab3e0SJohn Baldwin static int	killpg1(struct thread *td, int sig, int pgid, int all);
904d77a549SAlfred Perlstein static int	sig_ffs(sigset_t *set);
914d77a549SAlfred Perlstein static int	sigprop(int sig);
924d77a549SAlfred Perlstein static void	stop(struct proc *);
93e602ba25SJulian Elischer static void	tdsignal(struct thread *td, int sig, sig_t action);
94cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
95cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
96cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
97cb679c38SJonathan Lemon 
98cb679c38SJonathan Lemon struct filterops sig_filtops =
99cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
100cb679c38SJonathan Lemon 
10157308494SJoerg Wunsch static int	kern_logsigexit = 1;
1023d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1033d177f46SBill Fumerola     &kern_logsigexit, 0,
1043d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
10557308494SJoerg Wunsch 
1062b87b6d4SRobert Watson /*
1072b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1082b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1092b87b6d4SRobert Watson  * in the right situations.
1102b87b6d4SRobert Watson  */
1112b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1122b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1132b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1142b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1152b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1162b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1172b87b6d4SRobert Watson 
11822d4b0fbSJohn Polstra int sugid_coredump;
1193d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1203d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
121c87e2930SDavid Greenman 
122e5a28db9SPaul Saab static int	do_coredump = 1;
123e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
124e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
125e5a28db9SPaul Saab 
1262c42a146SMarcel Moolenaar /*
1272c42a146SMarcel Moolenaar  * Signal properties and actions.
1282c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1292c42a146SMarcel Moolenaar  * according to the following properties:
1302c42a146SMarcel Moolenaar  */
1312c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1322c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1332c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1342c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1352c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1362c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1372c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
138df8bae1dSRodney W. Grimes 
1392c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1402c42a146SMarcel Moolenaar         SA_KILL,                /* SIGHUP */
1412c42a146SMarcel Moolenaar         SA_KILL,                /* SIGINT */
1422c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGQUIT */
1432c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGILL */
1442c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGTRAP */
1452c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGABRT */
1462c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGEMT */
1472c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGFPE */
1482c42a146SMarcel Moolenaar         SA_KILL,                /* SIGKILL */
1492c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGBUS */
1502c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSEGV */
1512c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,        /* SIGSYS */
1522c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPIPE */
1532c42a146SMarcel Moolenaar         SA_KILL,                /* SIGALRM */
1542c42a146SMarcel Moolenaar         SA_KILL,                /* SIGTERM */
1552c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGURG */
1562c42a146SMarcel Moolenaar         SA_STOP,                /* SIGSTOP */
1572c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
1582c42a146SMarcel Moolenaar         SA_IGNORE|SA_CONT,      /* SIGCONT */
1592c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGCHLD */
1602c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
1612c42a146SMarcel Moolenaar         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
1622c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGIO */
1632c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXCPU */
1642c42a146SMarcel Moolenaar         SA_KILL,                /* SIGXFSZ */
1652c42a146SMarcel Moolenaar         SA_KILL,                /* SIGVTALRM */
1662c42a146SMarcel Moolenaar         SA_KILL,                /* SIGPROF */
1672c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGWINCH  */
1682c42a146SMarcel Moolenaar         SA_IGNORE,              /* SIGINFO */
1692c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR1 */
1702c42a146SMarcel Moolenaar         SA_KILL,                /* SIGUSR2 */
1712c42a146SMarcel Moolenaar };
1722c42a146SMarcel Moolenaar 
173fbbeeb6cSBruce Evans /*
174fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
175fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
176fbbeeb6cSBruce Evans  * action, the process stops in issignal().
177e602ba25SJulian Elischer  * XXXKSE   the check for a pending stop is not done under KSE
178fbbeeb6cSBruce Evans  *
17933510ef1SBruce Evans  * MP SAFE.
180fbbeeb6cSBruce Evans  */
181fbbeeb6cSBruce Evans int
182e602ba25SJulian Elischer cursig(struct thread *td)
183fbbeeb6cSBruce Evans {
184e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
185fbbeeb6cSBruce Evans 
1862ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
187179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
1881d9c5696SJuli Mallett 	return (SIGPENDING(p) ? issignal(td) : 0);
189fbbeeb6cSBruce Evans }
190fbbeeb6cSBruce Evans 
19179065dbaSBruce Evans /*
19279065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
1931d9c5696SJuli Mallett  * mode.  This must be called whenever a signal is added to p_siglist or
19479065dbaSBruce Evans  * unmasked in p_sigmask.
19579065dbaSBruce Evans  */
19679065dbaSBruce Evans void
19779065dbaSBruce Evans signotify(struct proc *p)
19879065dbaSBruce Evans {
1994f0db5e0SJulian Elischer 	struct kse *ke;
2004f0db5e0SJulian Elischer 	struct ksegrp *kg;
20179065dbaSBruce Evans 
20279065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
20379065dbaSBruce Evans 	mtx_lock_spin(&sched_lock);
2041d9c5696SJuli Mallett 	if (SIGPENDING(p)) {
20579065dbaSBruce Evans 		p->p_sflag |= PS_NEEDSIGCHK;
2064f0db5e0SJulian Elischer 		/* XXXKSE for now punish all KSEs */
2074f0db5e0SJulian Elischer 		FOREACH_KSEGRP_IN_PROC(p, kg) {
2084f0db5e0SJulian Elischer 			FOREACH_KSE_IN_GROUP(kg, ke) {
2094f0db5e0SJulian Elischer 				ke->ke_flags |= KEF_ASTPENDING;
2104f0db5e0SJulian Elischer 			}
2114f0db5e0SJulian Elischer 		}
21279065dbaSBruce Evans 	}
21379065dbaSBruce Evans 	mtx_unlock_spin(&sched_lock);
21479065dbaSBruce Evans }
21579065dbaSBruce Evans 
2166f841fb7SMarcel Moolenaar static __inline int
2176f841fb7SMarcel Moolenaar sigprop(int sig)
2182c42a146SMarcel Moolenaar {
2196f841fb7SMarcel Moolenaar 
2202c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
2212c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
2222c42a146SMarcel Moolenaar 	return (0);
223df8bae1dSRodney W. Grimes }
2242c42a146SMarcel Moolenaar 
2256f841fb7SMarcel Moolenaar static __inline int
2266f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
2272c42a146SMarcel Moolenaar {
2282c42a146SMarcel Moolenaar 	int i;
2292c42a146SMarcel Moolenaar 
2306f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
2312c42a146SMarcel Moolenaar 		if (set->__bits[i])
2322c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
233df8bae1dSRodney W. Grimes 	return (0);
234df8bae1dSRodney W. Grimes }
235df8bae1dSRodney W. Grimes 
2362c42a146SMarcel Moolenaar /*
2378f19eb88SIan Dowse  * kern_sigaction
2382c42a146SMarcel Moolenaar  * sigaction
23923eeeff7SPeter Wemm  * freebsd4_sigaction
2402c42a146SMarcel Moolenaar  * osigaction
2412c42a146SMarcel Moolenaar  */
2428f19eb88SIan Dowse int
24323eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
2448f19eb88SIan Dowse 	struct thread *td;
2452c42a146SMarcel Moolenaar 	register int sig;
2462c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
24723eeeff7SPeter Wemm 	int flags;
248df8bae1dSRodney W. Grimes {
249628d2653SJohn Baldwin 	register struct sigacts *ps;
2508f19eb88SIan Dowse 	struct proc *p = td->td_proc;
251df8bae1dSRodney W. Grimes 
2522899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
2532c42a146SMarcel Moolenaar 		return (EINVAL);
2542c42a146SMarcel Moolenaar 
255628d2653SJohn Baldwin 	PROC_LOCK(p);
256628d2653SJohn Baldwin 	ps = p->p_sigacts;
2572c42a146SMarcel Moolenaar 	if (oact) {
2582c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
2592c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
2602c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
2612c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
2622c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
2632c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
2642c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
2652c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
2662c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
2672c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
2682c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
2692c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
2702c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
271645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
2722c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
273645682fdSLuoqi Chen 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
2742c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
2752c42a146SMarcel Moolenaar 	}
2762c42a146SMarcel Moolenaar 	if (act) {
2772c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
278628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
279628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2802c42a146SMarcel Moolenaar 			return (EINVAL);
281628d2653SJohn Baldwin 		}
2822c42a146SMarcel Moolenaar 
283df8bae1dSRodney W. Grimes 		/*
284df8bae1dSRodney W. Grimes 		 * Change setting atomically.
285df8bae1dSRodney W. Grimes 		 */
2862c42a146SMarcel Moolenaar 
2872c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
2882c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
2892c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
290aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
291aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
29280f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
29380f42b55SIan Dowse 		} else {
29480f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
2952c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
2962c42a146SMarcel Moolenaar 		}
2972c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
2982c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
299df8bae1dSRodney W. Grimes 		else
3002c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
3012c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
3022c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
303df8bae1dSRodney W. Grimes 		else
3042c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
3052c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
3062c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
3071e41c1b5SSteven Wallace 		else
3082c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
3092c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
3102c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
311289ccde0SPeter Wemm 		else
3122c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
313df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
3142c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_USERTRAMP)
3152c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_usertramp, sig);
316df8bae1dSRodney W. Grimes 		else
3178c3d74f4SBruce Evans 			SIGDELSET(ps->ps_usertramp, sig);
318df8bae1dSRodney W. Grimes #endif
3192c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
3202c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
321645682fdSLuoqi Chen 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
3226626c604SJulian Elischer 			else
323645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
324ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
325245f17d4SJoerg Wunsch 				/*
3262c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
3272c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
3282c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
3292c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
330245f17d4SJoerg Wunsch 				 */
331245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
332645682fdSLuoqi Chen 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
3336626c604SJulian Elischer 				else
334645682fdSLuoqi Chen 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
335245f17d4SJoerg Wunsch 			} else
336645682fdSLuoqi Chen 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
337ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
338ba1551caSIan Dowse 				p->p_procsig->ps_flag |= PS_CLDSIGIGN;
339ba1551caSIan Dowse 			else
340ba1551caSIan Dowse 				p->p_procsig->ps_flag &= ~PS_CLDSIGIGN;
341df8bae1dSRodney W. Grimes 		}
342df8bae1dSRodney W. Grimes 		/*
343df8bae1dSRodney W. Grimes 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
3442c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
3452c42a146SMarcel Moolenaar 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
3462c42a146SMarcel Moolenaar 		 * have to restart the process.
347df8bae1dSRodney W. Grimes 		 */
3482c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
3492c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
3502c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
3512c42a146SMarcel Moolenaar 			/* never to be seen again */
3521d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
3532c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
3542c42a146SMarcel Moolenaar 				/* easier in psignal */
3552c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
3562c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
357645682fdSLuoqi Chen 		} else {
3582c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigignore, sig);
3592c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
3602c42a146SMarcel Moolenaar 				SIGDELSET(p->p_sigcatch, sig);
3612c42a146SMarcel Moolenaar 			else
3622c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigcatch, sig);
3632c42a146SMarcel Moolenaar 		}
36423eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
36523eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
36623eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
36723eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
36823eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
36923eeeff7SPeter Wemm 		else
37023eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
37123eeeff7SPeter Wemm #endif
372e8ebc08fSPeter Wemm #ifdef COMPAT_43
373645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
37423eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
37523eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
376645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
377645682fdSLuoqi Chen 		else
378645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
379e8ebc08fSPeter Wemm #endif
380df8bae1dSRodney W. Grimes 	}
381628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3822c42a146SMarcel Moolenaar 	return (0);
3832c42a146SMarcel Moolenaar }
3842c42a146SMarcel Moolenaar 
3852c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
3862c42a146SMarcel Moolenaar struct sigaction_args {
3872c42a146SMarcel Moolenaar 	int	sig;
3882c42a146SMarcel Moolenaar 	struct	sigaction *act;
3892c42a146SMarcel Moolenaar 	struct	sigaction *oact;
3902c42a146SMarcel Moolenaar };
3912c42a146SMarcel Moolenaar #endif
392fb99ab88SMatthew Dillon /*
393fb99ab88SMatthew Dillon  * MPSAFE
394fb99ab88SMatthew Dillon  */
3952c42a146SMarcel Moolenaar /* ARGSUSED */
3962c42a146SMarcel Moolenaar int
397b40ce416SJulian Elischer sigaction(td, uap)
398b40ce416SJulian Elischer 	struct thread *td;
3992c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
4002c42a146SMarcel Moolenaar {
4012c42a146SMarcel Moolenaar 	struct sigaction act, oact;
4022c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
4032c42a146SMarcel Moolenaar 	int error;
4042c42a146SMarcel Moolenaar 
405fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
406fb99ab88SMatthew Dillon 
4076f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
4086f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
4092c42a146SMarcel Moolenaar 	if (actp) {
4106f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
4112c42a146SMarcel Moolenaar 		if (error)
412fb99ab88SMatthew Dillon 			goto done2;
4132c42a146SMarcel Moolenaar 	}
4148f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
4152c42a146SMarcel Moolenaar 	if (oactp && !error) {
4166f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
4172c42a146SMarcel Moolenaar 	}
418fb99ab88SMatthew Dillon done2:
419fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
4202c42a146SMarcel Moolenaar 	return (error);
4212c42a146SMarcel Moolenaar }
4222c42a146SMarcel Moolenaar 
42323eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
42423eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
42523eeeff7SPeter Wemm struct freebsd4_sigaction_args {
42623eeeff7SPeter Wemm 	int	sig;
42723eeeff7SPeter Wemm 	struct	sigaction *act;
42823eeeff7SPeter Wemm 	struct	sigaction *oact;
42923eeeff7SPeter Wemm };
43023eeeff7SPeter Wemm #endif
43123eeeff7SPeter Wemm /*
43223eeeff7SPeter Wemm  * MPSAFE
43323eeeff7SPeter Wemm  */
43423eeeff7SPeter Wemm /* ARGSUSED */
43523eeeff7SPeter Wemm int
43623eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
43723eeeff7SPeter Wemm 	struct thread *td;
43823eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
43923eeeff7SPeter Wemm {
44023eeeff7SPeter Wemm 	struct sigaction act, oact;
44123eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
44223eeeff7SPeter Wemm 	int error;
44323eeeff7SPeter Wemm 
44423eeeff7SPeter Wemm 	mtx_lock(&Giant);
44523eeeff7SPeter Wemm 
44623eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
44723eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
44823eeeff7SPeter Wemm 	if (actp) {
44923eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
45023eeeff7SPeter Wemm 		if (error)
45123eeeff7SPeter Wemm 			goto done2;
45223eeeff7SPeter Wemm 	}
45323eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
45423eeeff7SPeter Wemm 	if (oactp && !error) {
45523eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
45623eeeff7SPeter Wemm 	}
45723eeeff7SPeter Wemm done2:
45823eeeff7SPeter Wemm 	mtx_unlock(&Giant);
45923eeeff7SPeter Wemm 	return (error);
46023eeeff7SPeter Wemm }
46123eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
46223eeeff7SPeter Wemm 
46331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
4642c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
4652c42a146SMarcel Moolenaar struct osigaction_args {
4662c42a146SMarcel Moolenaar 	int	signum;
4672c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
4682c42a146SMarcel Moolenaar 	struct	osigaction *osa;
4692c42a146SMarcel Moolenaar };
4702c42a146SMarcel Moolenaar #endif
471fb99ab88SMatthew Dillon /*
472fb99ab88SMatthew Dillon  * MPSAFE
473fb99ab88SMatthew Dillon  */
4742c42a146SMarcel Moolenaar /* ARGSUSED */
4752c42a146SMarcel Moolenaar int
476b40ce416SJulian Elischer osigaction(td, uap)
477b40ce416SJulian Elischer 	struct thread *td;
4782c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
4792c42a146SMarcel Moolenaar {
4802c42a146SMarcel Moolenaar 	struct osigaction sa;
4812c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
4822c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
4832c42a146SMarcel Moolenaar 	int error;
4842c42a146SMarcel Moolenaar 
4856f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
4866f841fb7SMarcel Moolenaar 		return (EINVAL);
487fb99ab88SMatthew Dillon 
4886f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
4896f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
490fb99ab88SMatthew Dillon 
491fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
492fb99ab88SMatthew Dillon 
4932c42a146SMarcel Moolenaar 	if (nsap) {
4946f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
4952c42a146SMarcel Moolenaar 		if (error)
496fb99ab88SMatthew Dillon 			goto done2;
4972c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
4982c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
4992c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
5002c42a146SMarcel Moolenaar 	}
50123eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
5022c42a146SMarcel Moolenaar 	if (osap && !error) {
5032c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
5042c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
5052c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
5066f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
5072c42a146SMarcel Moolenaar 	}
508fb99ab88SMatthew Dillon done2:
509fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
5102c42a146SMarcel Moolenaar 	return (error);
5112c42a146SMarcel Moolenaar }
51223eeeff7SPeter Wemm 
51323eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
51423eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
51523eeeff7SPeter Wemm int
51623eeeff7SPeter Wemm osigreturn(td, uap)
51723eeeff7SPeter Wemm 	struct thread *td;
51823eeeff7SPeter Wemm 	struct osigreturn_args *uap;
51923eeeff7SPeter Wemm {
52023eeeff7SPeter Wemm 
52123eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
52223eeeff7SPeter Wemm }
52323eeeff7SPeter Wemm #endif
52431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
525df8bae1dSRodney W. Grimes 
526df8bae1dSRodney W. Grimes /*
527df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
528df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
529df8bae1dSRodney W. Grimes  */
530df8bae1dSRodney W. Grimes void
531df8bae1dSRodney W. Grimes siginit(p)
532df8bae1dSRodney W. Grimes 	struct proc *p;
533df8bae1dSRodney W. Grimes {
534df8bae1dSRodney W. Grimes 	register int i;
535df8bae1dSRodney W. Grimes 
536628d2653SJohn Baldwin 	PROC_LOCK(p);
5372c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
5382c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
5392c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigignore, i);
540628d2653SJohn Baldwin 	PROC_UNLOCK(p);
541df8bae1dSRodney W. Grimes }
542df8bae1dSRodney W. Grimes 
543df8bae1dSRodney W. Grimes /*
544df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
545df8bae1dSRodney W. Grimes  */
546df8bae1dSRodney W. Grimes void
547df8bae1dSRodney W. Grimes execsigs(p)
548df8bae1dSRodney W. Grimes 	register struct proc *p;
549df8bae1dSRodney W. Grimes {
550628d2653SJohn Baldwin 	register struct sigacts *ps;
5512c42a146SMarcel Moolenaar 	register int sig;
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 	/*
554df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
555df8bae1dSRodney W. Grimes 	 * through p_sigmask (unless they were caught,
556df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
557df8bae1dSRodney W. Grimes 	 */
5589b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
559628d2653SJohn Baldwin 	ps = p->p_sigacts;
5602c42a146SMarcel Moolenaar 	while (SIGNOTEMPTY(p->p_sigcatch)) {
5612c42a146SMarcel Moolenaar 		sig = sig_ffs(&p->p_sigcatch);
5622c42a146SMarcel Moolenaar 		SIGDELSET(p->p_sigcatch, sig);
5632c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
5642c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
5652c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
5661d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
567df8bae1dSRodney W. Grimes 		}
5682c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
569df8bae1dSRodney W. Grimes 	}
570df8bae1dSRodney W. Grimes 	/*
571df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
572df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
573df8bae1dSRodney W. Grimes 	 */
574645682fdSLuoqi Chen 	p->p_sigstk.ss_flags = SS_DISABLE;
575645682fdSLuoqi Chen 	p->p_sigstk.ss_size = 0;
576645682fdSLuoqi Chen 	p->p_sigstk.ss_sp = 0;
5773b26be6aSAkinori MUSHA 	p->p_flag &= ~P_ALTSTACK;
57880e907a1SPeter Wemm 	/*
57980e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
58080e907a1SPeter Wemm 	 */
581ba1551caSIan Dowse 	p->p_procsig->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
582c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
583c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
584df8bae1dSRodney W. Grimes }
585df8bae1dSRodney W. Grimes 
586df8bae1dSRodney W. Grimes /*
587628d2653SJohn Baldwin  * do_sigprocmask()
5887c8fdcbdSMatthew Dillon  *
589628d2653SJohn Baldwin  *	Manipulate signal mask.
590df8bae1dSRodney W. Grimes  */
5912c42a146SMarcel Moolenaar static int
592645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old)
5932c42a146SMarcel Moolenaar 	struct proc *p;
5942c42a146SMarcel Moolenaar 	int how;
5952c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
596645682fdSLuoqi Chen 	int old;
5972c42a146SMarcel Moolenaar {
5982c42a146SMarcel Moolenaar 	int error;
5992c42a146SMarcel Moolenaar 
600628d2653SJohn Baldwin 	PROC_LOCK(p);
6012c42a146SMarcel Moolenaar 	if (oset != NULL)
6022c42a146SMarcel Moolenaar 		*oset = p->p_sigmask;
6032c42a146SMarcel Moolenaar 
6042c42a146SMarcel Moolenaar 	error = 0;
6052c42a146SMarcel Moolenaar 	if (set != NULL) {
6062c42a146SMarcel Moolenaar 		switch (how) {
6072c42a146SMarcel Moolenaar 		case SIG_BLOCK:
608645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
6092c42a146SMarcel Moolenaar 			SIGSETOR(p->p_sigmask, *set);
6102c42a146SMarcel Moolenaar 			break;
6112c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
6122c42a146SMarcel Moolenaar 			SIGSETNAND(p->p_sigmask, *set);
61379065dbaSBruce Evans 			signotify(p);
6142c42a146SMarcel Moolenaar 			break;
6152c42a146SMarcel Moolenaar 		case SIG_SETMASK:
616645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
617645682fdSLuoqi Chen 			if (old)
618645682fdSLuoqi Chen 				SIGSETLO(p->p_sigmask, *set);
619645682fdSLuoqi Chen 			else
6202c42a146SMarcel Moolenaar 				p->p_sigmask = *set;
62179065dbaSBruce Evans 			signotify(p);
6222c42a146SMarcel Moolenaar 			break;
6232c42a146SMarcel Moolenaar 		default:
6242c42a146SMarcel Moolenaar 			error = EINVAL;
6252c42a146SMarcel Moolenaar 			break;
6262c42a146SMarcel Moolenaar 		}
6272c42a146SMarcel Moolenaar 	}
628628d2653SJohn Baldwin 	PROC_UNLOCK(p);
6292c42a146SMarcel Moolenaar 	return (error);
6302c42a146SMarcel Moolenaar }
6312c42a146SMarcel Moolenaar 
6327c8fdcbdSMatthew Dillon /*
633b40ce416SJulian Elischer  * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
6347c8fdcbdSMatthew Dillon  */
6357c8fdcbdSMatthew Dillon 
636d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
637df8bae1dSRodney W. Grimes struct sigprocmask_args {
638df8bae1dSRodney W. Grimes 	int	how;
6392c42a146SMarcel Moolenaar 	const sigset_t *set;
6402c42a146SMarcel Moolenaar 	sigset_t *oset;
641df8bae1dSRodney W. Grimes };
642d2d3e875SBruce Evans #endif
64326f9a767SRodney W. Grimes int
644b40ce416SJulian Elischer sigprocmask(td, uap)
645b40ce416SJulian Elischer 	register struct thread *td;
646df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
647df8bae1dSRodney W. Grimes {
648b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
6492c42a146SMarcel Moolenaar 	sigset_t set, oset;
6502c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
6512c42a146SMarcel Moolenaar 	int error;
652df8bae1dSRodney W. Grimes 
6536f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
6546f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
6552c42a146SMarcel Moolenaar 	if (setp) {
6566f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
6572c42a146SMarcel Moolenaar 		if (error)
6582c42a146SMarcel Moolenaar 			return (error);
659df8bae1dSRodney W. Grimes 	}
660645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
6612c42a146SMarcel Moolenaar 	if (osetp && !error) {
6626f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
6632c42a146SMarcel Moolenaar 	}
6642c42a146SMarcel Moolenaar 	return (error);
6652c42a146SMarcel Moolenaar }
6662c42a146SMarcel Moolenaar 
66731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
6687c8fdcbdSMatthew Dillon /*
6697c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
6707c8fdcbdSMatthew Dillon  */
6712c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
6722c42a146SMarcel Moolenaar struct osigprocmask_args {
6732c42a146SMarcel Moolenaar 	int	how;
6742c42a146SMarcel Moolenaar 	osigset_t mask;
6752c42a146SMarcel Moolenaar };
6762c42a146SMarcel Moolenaar #endif
6772c42a146SMarcel Moolenaar int
678b40ce416SJulian Elischer osigprocmask(td, uap)
679b40ce416SJulian Elischer 	register struct thread *td;
6802c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
6812c42a146SMarcel Moolenaar {
682b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
6832c42a146SMarcel Moolenaar 	sigset_t set, oset;
6842c42a146SMarcel Moolenaar 	int error;
6852c42a146SMarcel Moolenaar 
6862c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
687645682fdSLuoqi Chen 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
688b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
689df8bae1dSRodney W. Grimes 	return (error);
690df8bae1dSRodney W. Grimes }
69131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
692df8bae1dSRodney W. Grimes 
693d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
694df8bae1dSRodney W. Grimes struct sigpending_args {
6952c42a146SMarcel Moolenaar 	sigset_t	*set;
696df8bae1dSRodney W. Grimes };
697d2d3e875SBruce Evans #endif
698fb99ab88SMatthew Dillon /*
699fb99ab88SMatthew Dillon  * MPSAFE
700fb99ab88SMatthew Dillon  */
701df8bae1dSRodney W. Grimes /* ARGSUSED */
70226f9a767SRodney W. Grimes int
703b40ce416SJulian Elischer sigpending(td, uap)
704b40ce416SJulian Elischer 	struct thread *td;
705df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
706df8bae1dSRodney W. Grimes {
707b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
708628d2653SJohn Baldwin 	sigset_t siglist;
709fb99ab88SMatthew Dillon 	int error;
710df8bae1dSRodney W. Grimes 
711fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
712628d2653SJohn Baldwin 	PROC_LOCK(p);
7131d9c5696SJuli Mallett 	siglist = p->p_siglist;
714628d2653SJohn Baldwin 	PROC_UNLOCK(p);
715fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
716fb99ab88SMatthew Dillon 	error = copyout(&siglist, uap->set, sizeof(sigset_t));
717fb99ab88SMatthew Dillon 	return(error);
7182c42a146SMarcel Moolenaar }
7192c42a146SMarcel Moolenaar 
72031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
7212c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7222c42a146SMarcel Moolenaar struct osigpending_args {
7232c42a146SMarcel Moolenaar 	int	dummy;
7242c42a146SMarcel Moolenaar };
7252c42a146SMarcel Moolenaar #endif
726fb99ab88SMatthew Dillon /*
727fb99ab88SMatthew Dillon  * MPSAFE
728fb99ab88SMatthew Dillon  */
7292c42a146SMarcel Moolenaar /* ARGSUSED */
7302c42a146SMarcel Moolenaar int
731b40ce416SJulian Elischer osigpending(td, uap)
732b40ce416SJulian Elischer 	struct thread *td;
7332c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
7342c42a146SMarcel Moolenaar {
735b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
736b40ce416SJulian Elischer 
737fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
738628d2653SJohn Baldwin 	PROC_LOCK(p);
7391d9c5696SJuli Mallett 	SIG2OSIG(p->p_siglist, td->td_retval[0]);
740628d2653SJohn Baldwin 	PROC_UNLOCK(p);
741fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
742df8bae1dSRodney W. Grimes 	return (0);
743df8bae1dSRodney W. Grimes }
74431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
745df8bae1dSRodney W. Grimes 
746df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
747df8bae1dSRodney W. Grimes /*
748df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
749df8bae1dSRodney W. Grimes  */
750d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
751df8bae1dSRodney W. Grimes struct osigvec_args {
752df8bae1dSRodney W. Grimes 	int	signum;
753df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
754df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
755df8bae1dSRodney W. Grimes };
756d2d3e875SBruce Evans #endif
757fb99ab88SMatthew Dillon /*
758fb99ab88SMatthew Dillon  * MPSAFE
759fb99ab88SMatthew Dillon  */
760df8bae1dSRodney W. Grimes /* ARGSUSED */
76126f9a767SRodney W. Grimes int
762b40ce416SJulian Elischer osigvec(td, uap)
763b40ce416SJulian Elischer 	struct thread *td;
764df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
765df8bae1dSRodney W. Grimes {
766df8bae1dSRodney W. Grimes 	struct sigvec vec;
7672c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
7682c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
7692c42a146SMarcel Moolenaar 	int error;
770df8bae1dSRodney W. Grimes 
7716f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
7726f841fb7SMarcel Moolenaar 		return (EINVAL);
7736f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
7746f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
7752c42a146SMarcel Moolenaar 	if (nsap) {
7766f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
7772c42a146SMarcel Moolenaar 		if (error)
778df8bae1dSRodney W. Grimes 			return (error);
7792c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
7802c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
7812c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
7822c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
783df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS
7842c42a146SMarcel Moolenaar 		nsap->sa_flags |= SA_USERTRAMP;
785df8bae1dSRodney W. Grimes #endif
786df8bae1dSRodney W. Grimes 	}
787fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
7888f19eb88SIan Dowse 	error = kern_sigaction(td, uap->signum, nsap, osap, 1);
789fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
7902c42a146SMarcel Moolenaar 	if (osap && !error) {
7912c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
7922c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
7932c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
7942c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
7952c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
7962c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS
7972c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDSTOP;
7982c42a146SMarcel Moolenaar #endif
7996f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
8002c42a146SMarcel Moolenaar 	}
8012c42a146SMarcel Moolenaar 	return (error);
802df8bae1dSRodney W. Grimes }
803df8bae1dSRodney W. Grimes 
804d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
805df8bae1dSRodney W. Grimes struct osigblock_args {
806df8bae1dSRodney W. Grimes 	int	mask;
807df8bae1dSRodney W. Grimes };
808d2d3e875SBruce Evans #endif
809fb99ab88SMatthew Dillon /*
810fb99ab88SMatthew Dillon  * MPSAFE
811fb99ab88SMatthew Dillon  */
81226f9a767SRodney W. Grimes int
813b40ce416SJulian Elischer osigblock(td, uap)
814b40ce416SJulian Elischer 	register struct thread *td;
815df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
816df8bae1dSRodney W. Grimes {
817b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
8182c42a146SMarcel Moolenaar 	sigset_t set;
819df8bae1dSRodney W. Grimes 
8202c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
8212c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
822fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
823628d2653SJohn Baldwin 	PROC_LOCK(p);
824b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
8252c42a146SMarcel Moolenaar 	SIGSETOR(p->p_sigmask, set);
826628d2653SJohn Baldwin 	PROC_UNLOCK(p);
827fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
828df8bae1dSRodney W. Grimes 	return (0);
829df8bae1dSRodney W. Grimes }
830df8bae1dSRodney W. Grimes 
831d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
832df8bae1dSRodney W. Grimes struct osigsetmask_args {
833df8bae1dSRodney W. Grimes 	int	mask;
834df8bae1dSRodney W. Grimes };
835d2d3e875SBruce Evans #endif
836fb99ab88SMatthew Dillon /*
837fb99ab88SMatthew Dillon  * MPSAFE
838fb99ab88SMatthew Dillon  */
83926f9a767SRodney W. Grimes int
840b40ce416SJulian Elischer osigsetmask(td, uap)
841b40ce416SJulian Elischer 	struct thread *td;
842df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
843df8bae1dSRodney W. Grimes {
844b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
8452c42a146SMarcel Moolenaar 	sigset_t set;
846df8bae1dSRodney W. Grimes 
8472c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
8482c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
849fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
850628d2653SJohn Baldwin 	PROC_LOCK(p);
851b40ce416SJulian Elischer 	SIG2OSIG(p->p_sigmask, td->td_retval[0]);
852645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, set);
85379065dbaSBruce Evans 	signotify(p);
854628d2653SJohn Baldwin 	PROC_UNLOCK(p);
855fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
856df8bae1dSRodney W. Grimes 	return (0);
857df8bae1dSRodney W. Grimes }
858df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
859df8bae1dSRodney W. Grimes 
860df8bae1dSRodney W. Grimes /*
861df8bae1dSRodney W. Grimes  * Suspend process until signal, providing mask to be set
862df8bae1dSRodney W. Grimes  * in the meantime.  Note nonstandard calling convention:
863df8bae1dSRodney W. Grimes  * libc stub passes mask, not pointer, to save a copyin.
864b40ce416SJulian Elischer  ***** XXXKSE this doesn't make sense under KSE.
865b40ce416SJulian Elischer  ***** Do we suspend the thread or all threads in the process?
866b40ce416SJulian Elischer  ***** How do we suspend threads running NOW on another processor?
867df8bae1dSRodney W. Grimes  */
868d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
869df8bae1dSRodney W. Grimes struct sigsuspend_args {
8702c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
871df8bae1dSRodney W. Grimes };
872d2d3e875SBruce Evans #endif
873fb99ab88SMatthew Dillon /*
874fb99ab88SMatthew Dillon  * MPSAFE
875fb99ab88SMatthew Dillon  */
876df8bae1dSRodney W. Grimes /* ARGSUSED */
87726f9a767SRodney W. Grimes int
878b40ce416SJulian Elischer sigsuspend(td, uap)
879b40ce416SJulian Elischer 	struct thread *td;
880df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
881df8bae1dSRodney W. Grimes {
8822c42a146SMarcel Moolenaar 	sigset_t mask;
8832c42a146SMarcel Moolenaar 	int error;
8842c42a146SMarcel Moolenaar 
8856f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
8862c42a146SMarcel Moolenaar 	if (error)
8872c42a146SMarcel Moolenaar 		return (error);
8888f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
8898f19eb88SIan Dowse }
8908f19eb88SIan Dowse 
8918f19eb88SIan Dowse int
8928f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
8938f19eb88SIan Dowse {
8948f19eb88SIan Dowse 	struct proc *p = td->td_proc;
8958f19eb88SIan Dowse 	register struct sigacts *ps;
896df8bae1dSRodney W. Grimes 
897df8bae1dSRodney W. Grimes 	/*
898645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
899df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
900df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
901df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
902df8bae1dSRodney W. Grimes 	 * to indicate this.
903df8bae1dSRodney W. Grimes 	 */
904fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
905628d2653SJohn Baldwin 	PROC_LOCK(p);
906628d2653SJohn Baldwin 	ps = p->p_sigacts;
9076626c604SJulian Elischer 	p->p_oldsigmask = p->p_sigmask;
908645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
909645682fdSLuoqi Chen 
910645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
9112c42a146SMarcel Moolenaar 	p->p_sigmask = mask;
91279065dbaSBruce Evans 	signotify(p);
91301609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
9142c42a146SMarcel Moolenaar 		/* void */;
915628d2653SJohn Baldwin 	PROC_UNLOCK(p);
916fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
9172c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
9182c42a146SMarcel Moolenaar 	return (EINTR);
9192c42a146SMarcel Moolenaar }
9202c42a146SMarcel Moolenaar 
92131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9222c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9232c42a146SMarcel Moolenaar struct osigsuspend_args {
9242c42a146SMarcel Moolenaar 	osigset_t mask;
9252c42a146SMarcel Moolenaar };
9262c42a146SMarcel Moolenaar #endif
927fb99ab88SMatthew Dillon /*
928fb99ab88SMatthew Dillon  * MPSAFE
929fb99ab88SMatthew Dillon  */
9302c42a146SMarcel Moolenaar /* ARGSUSED */
9312c42a146SMarcel Moolenaar int
932b40ce416SJulian Elischer osigsuspend(td, uap)
933b40ce416SJulian Elischer 	struct thread *td;
9342c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
9352c42a146SMarcel Moolenaar {
936b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
937645682fdSLuoqi Chen 	sigset_t mask;
938628d2653SJohn Baldwin 	register struct sigacts *ps;
9392c42a146SMarcel Moolenaar 
940fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
941628d2653SJohn Baldwin 	PROC_LOCK(p);
942628d2653SJohn Baldwin 	ps = p->p_sigacts;
9432c42a146SMarcel Moolenaar 	p->p_oldsigmask = p->p_sigmask;
944645682fdSLuoqi Chen 	p->p_flag |= P_OLDMASK;
945645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
946645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
947645682fdSLuoqi Chen 	SIGSETLO(p->p_sigmask, mask);
94879065dbaSBruce Evans 	signotify(p);
94901609114SAlfred Perlstein 	while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
950df8bae1dSRodney W. Grimes 		/* void */;
951628d2653SJohn Baldwin 	PROC_UNLOCK(p);
952fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
953df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
954df8bae1dSRodney W. Grimes 	return (EINTR);
955df8bae1dSRodney W. Grimes }
95631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
957df8bae1dSRodney W. Grimes 
958df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
959d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
960df8bae1dSRodney W. Grimes struct osigstack_args {
961df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
962df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
963df8bae1dSRodney W. Grimes };
964d2d3e875SBruce Evans #endif
965fb99ab88SMatthew Dillon /*
966fb99ab88SMatthew Dillon  * MPSAFE
967fb99ab88SMatthew Dillon  */
968df8bae1dSRodney W. Grimes /* ARGSUSED */
96926f9a767SRodney W. Grimes int
970b40ce416SJulian Elischer osigstack(td, uap)
971b40ce416SJulian Elischer 	struct thread *td;
972df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
973df8bae1dSRodney W. Grimes {
974b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
975df8bae1dSRodney W. Grimes 	struct sigstack ss;
976fb99ab88SMatthew Dillon 	int error = 0;
977fb99ab88SMatthew Dillon 
978fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
979df8bae1dSRodney W. Grimes 
980d034d459SMarcel Moolenaar 	if (uap->oss != NULL) {
981628d2653SJohn Baldwin 		PROC_LOCK(p);
982645682fdSLuoqi Chen 		ss.ss_sp = p->p_sigstk.ss_sp;
983b40ce416SJulian Elischer 		ss.ss_onstack = sigonstack(cpu_getstack(td));
984628d2653SJohn Baldwin 		PROC_UNLOCK(p);
985d034d459SMarcel Moolenaar 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
986d034d459SMarcel Moolenaar 		if (error)
987fb99ab88SMatthew Dillon 			goto done2;
988d034d459SMarcel Moolenaar 	}
989d034d459SMarcel Moolenaar 
990d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
991d034d459SMarcel Moolenaar 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
992fb99ab88SMatthew Dillon 			goto done2;
993628d2653SJohn Baldwin 		PROC_LOCK(p);
994645682fdSLuoqi Chen 		p->p_sigstk.ss_sp = ss.ss_sp;
995645682fdSLuoqi Chen 		p->p_sigstk.ss_size = 0;
996645682fdSLuoqi Chen 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
997645682fdSLuoqi Chen 		p->p_flag |= P_ALTSTACK;
998628d2653SJohn Baldwin 		PROC_UNLOCK(p);
999df8bae1dSRodney W. Grimes 	}
1000fb99ab88SMatthew Dillon done2:
1001fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1002fb99ab88SMatthew Dillon 	return (error);
1003df8bae1dSRodney W. Grimes }
1004df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1005df8bae1dSRodney W. Grimes 
1006d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1007df8bae1dSRodney W. Grimes struct sigaltstack_args {
10082c42a146SMarcel Moolenaar 	stack_t	*ss;
10092c42a146SMarcel Moolenaar 	stack_t	*oss;
1010df8bae1dSRodney W. Grimes };
1011d2d3e875SBruce Evans #endif
1012fb99ab88SMatthew Dillon /*
1013fb99ab88SMatthew Dillon  * MPSAFE
1014fb99ab88SMatthew Dillon  */
1015df8bae1dSRodney W. Grimes /* ARGSUSED */
101626f9a767SRodney W. Grimes int
1017b40ce416SJulian Elischer sigaltstack(td, uap)
1018b40ce416SJulian Elischer 	struct thread *td;
1019df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1020df8bae1dSRodney W. Grimes {
10218f19eb88SIan Dowse 	stack_t ss, oss;
10228f19eb88SIan Dowse 	int error;
10238f19eb88SIan Dowse 
10248f19eb88SIan Dowse 	if (uap->ss != NULL) {
10258f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
10268f19eb88SIan Dowse 		if (error)
10278f19eb88SIan Dowse 			return (error);
10288f19eb88SIan Dowse 	}
10298f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
10308f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
10318f19eb88SIan Dowse 	if (error)
10328f19eb88SIan Dowse 		return (error);
10338f19eb88SIan Dowse 	if (uap->oss != NULL)
10348f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
10358f19eb88SIan Dowse 	return (error);
10368f19eb88SIan Dowse }
10378f19eb88SIan Dowse 
10388f19eb88SIan Dowse int
10398f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
10408f19eb88SIan Dowse {
1041b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1042fb99ab88SMatthew Dillon 	int oonstack;
1043fb99ab88SMatthew Dillon 	int error = 0;
1044fb99ab88SMatthew Dillon 
1045fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1046df8bae1dSRodney W. Grimes 
1047b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1048d034d459SMarcel Moolenaar 
10498f19eb88SIan Dowse 	if (oss != NULL) {
1050628d2653SJohn Baldwin 		PROC_LOCK(p);
10518f19eb88SIan Dowse 		*oss = p->p_sigstk;
10528f19eb88SIan Dowse 		oss->ss_flags = (p->p_flag & P_ALTSTACK)
1053d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1054628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1055df8bae1dSRodney W. Grimes 	}
1056d034d459SMarcel Moolenaar 
10578f19eb88SIan Dowse 	if (ss != NULL) {
1058fb99ab88SMatthew Dillon 		if (oonstack) {
1059fb99ab88SMatthew Dillon 			error = EPERM;
1060fb99ab88SMatthew Dillon 			goto done2;
1061fb99ab88SMatthew Dillon 		}
10628f19eb88SIan Dowse 		if ((ss->ss_flags & ~SS_DISABLE) != 0) {
1063fb99ab88SMatthew Dillon 			error = EINVAL;
1064fb99ab88SMatthew Dillon 			goto done2;
1065fb99ab88SMatthew Dillon 		}
10668f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
10678f19eb88SIan Dowse 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1068fb99ab88SMatthew Dillon 				error = ENOMEM;
1069fb99ab88SMatthew Dillon 				goto done2;
1070fb99ab88SMatthew Dillon 			}
1071628d2653SJohn Baldwin 			PROC_LOCK(p);
10728f19eb88SIan Dowse 			p->p_sigstk = *ss;
1073d034d459SMarcel Moolenaar 			p->p_flag |= P_ALTSTACK;
1074628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1075628d2653SJohn Baldwin 		} else {
1076628d2653SJohn Baldwin 			PROC_LOCK(p);
1077d034d459SMarcel Moolenaar 			p->p_flag &= ~P_ALTSTACK;
1078628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1079628d2653SJohn Baldwin 		}
1080d034d459SMarcel Moolenaar 	}
1081fb99ab88SMatthew Dillon done2:
1082fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1083fb99ab88SMatthew Dillon 	return (error);
1084df8bae1dSRodney W. Grimes }
1085df8bae1dSRodney W. Grimes 
1086d93f860cSPoul-Henning Kamp /*
1087d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1088d93f860cSPoul-Henning Kamp  * cp is calling process.
1089d93f860cSPoul-Henning Kamp  */
109037c84183SPoul-Henning Kamp static int
10919c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all)
10929c1ab3e0SJohn Baldwin 	register struct thread *td;
10932c42a146SMarcel Moolenaar 	int sig, pgid, all;
1094d93f860cSPoul-Henning Kamp {
1095d93f860cSPoul-Henning Kamp 	register struct proc *p;
1096d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1097d93f860cSPoul-Henning Kamp 	int nfound = 0;
1098d93f860cSPoul-Henning Kamp 
1099553629ebSJake Burkholder 	if (all) {
1100d93f860cSPoul-Henning Kamp 		/*
1101d93f860cSPoul-Henning Kamp 		 * broadcast
1102d93f860cSPoul-Henning Kamp 		 */
11031005a129SJohn Baldwin 		sx_slock(&allproc_lock);
11042e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1105628d2653SJohn Baldwin 			PROC_LOCK(p);
11069c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
11079c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1108628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1109628d2653SJohn Baldwin 				continue;
1110628d2653SJohn Baldwin 			}
1111f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1112d93f860cSPoul-Henning Kamp 				nfound++;
111333a9ed9dSJohn Baldwin 				if (sig)
11142c42a146SMarcel Moolenaar 					psignal(p, sig);
1115628d2653SJohn Baldwin 			}
111633a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1117d93f860cSPoul-Henning Kamp 		}
11181005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1119553629ebSJake Burkholder 	} else {
1120ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1121f591779bSSeigo Tanimura 		if (pgid == 0) {
1122d93f860cSPoul-Henning Kamp 			/*
1123d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1124d93f860cSPoul-Henning Kamp 			 */
11259c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1126f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1127f591779bSSeigo Tanimura 		} else {
1128d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1129f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1130ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1131d93f860cSPoul-Henning Kamp 				return (ESRCH);
1132d93f860cSPoul-Henning Kamp 			}
1133f591779bSSeigo Tanimura 		}
1134ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
11352e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1136628d2653SJohn Baldwin 			PROC_LOCK(p);
1137628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1138628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1139628d2653SJohn Baldwin 				continue;
1140628d2653SJohn Baldwin 			}
1141e602ba25SJulian Elischer 			if (p->p_state == PRS_ZOMBIE) {
114233a9ed9dSJohn Baldwin 				PROC_UNLOCK(p);
1143628d2653SJohn Baldwin 				continue;
1144628d2653SJohn Baldwin 			}
1145f44d9e24SJohn Baldwin 			if (p_cansignal(td, p, sig) == 0) {
1146d93f860cSPoul-Henning Kamp 				nfound++;
114733a9ed9dSJohn Baldwin 				if (sig)
11482c42a146SMarcel Moolenaar 					psignal(p, sig);
1149628d2653SJohn Baldwin 			}
115033a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1151d93f860cSPoul-Henning Kamp 		}
1152f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1153d93f860cSPoul-Henning Kamp 	}
1154d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1155d93f860cSPoul-Henning Kamp }
1156d93f860cSPoul-Henning Kamp 
1157d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1158df8bae1dSRodney W. Grimes struct kill_args {
1159df8bae1dSRodney W. Grimes 	int	pid;
1160df8bae1dSRodney W. Grimes 	int	signum;
1161df8bae1dSRodney W. Grimes };
1162d2d3e875SBruce Evans #endif
1163fb99ab88SMatthew Dillon /*
1164fb99ab88SMatthew Dillon  * MPSAFE
1165fb99ab88SMatthew Dillon  */
1166df8bae1dSRodney W. Grimes /* ARGSUSED */
116726f9a767SRodney W. Grimes int
1168b40ce416SJulian Elischer kill(td, uap)
1169b40ce416SJulian Elischer 	register struct thread *td;
1170df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1171df8bae1dSRodney W. Grimes {
1172df8bae1dSRodney W. Grimes 	register struct proc *p;
1173fb99ab88SMatthew Dillon 	int error = 0;
1174df8bae1dSRodney W. Grimes 
11756c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1176df8bae1dSRodney W. Grimes 		return (EINVAL);
1177fb99ab88SMatthew Dillon 
1178fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1179df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1180df8bae1dSRodney W. Grimes 		/* kill single process */
1181fb99ab88SMatthew Dillon 		if ((p = pfind(uap->pid)) == NULL) {
1182fb99ab88SMatthew Dillon 			error = ESRCH;
1183f44d9e24SJohn Baldwin 		} else if ((error = p_cansignal(td, p, uap->signum)) != 0) {
118433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1185fb99ab88SMatthew Dillon 		} else {
118633a9ed9dSJohn Baldwin 			if (uap->signum)
1187df8bae1dSRodney W. Grimes 				psignal(p, uap->signum);
1188628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1189fb99ab88SMatthew Dillon 			error = 0;
1190df8bae1dSRodney W. Grimes 		}
1191fb99ab88SMatthew Dillon 	} else {
1192df8bae1dSRodney W. Grimes 		switch (uap->pid) {
1193df8bae1dSRodney W. Grimes 		case -1:		/* broadcast signal */
11949c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 1);
1195fb99ab88SMatthew Dillon 			break;
1196df8bae1dSRodney W. Grimes 		case 0:			/* signal own process group */
11979c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, 0, 0);
1198fb99ab88SMatthew Dillon 			break;
1199df8bae1dSRodney W. Grimes 		default:		/* negative explicit process group */
12009c1ab3e0SJohn Baldwin 			error = killpg1(td, uap->signum, -uap->pid, 0);
1201fb99ab88SMatthew Dillon 			break;
1202df8bae1dSRodney W. Grimes 		}
1203fb99ab88SMatthew Dillon 	}
1204fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1205fb99ab88SMatthew Dillon 	return(error);
1206df8bae1dSRodney W. Grimes }
1207df8bae1dSRodney W. Grimes 
1208df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1209d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1210df8bae1dSRodney W. Grimes struct okillpg_args {
1211df8bae1dSRodney W. Grimes 	int	pgid;
1212df8bae1dSRodney W. Grimes 	int	signum;
1213df8bae1dSRodney W. Grimes };
1214d2d3e875SBruce Evans #endif
1215fb99ab88SMatthew Dillon /*
1216fb99ab88SMatthew Dillon  * MPSAFE
1217fb99ab88SMatthew Dillon  */
1218df8bae1dSRodney W. Grimes /* ARGSUSED */
121926f9a767SRodney W. Grimes int
1220b40ce416SJulian Elischer okillpg(td, uap)
1221b40ce416SJulian Elischer 	struct thread *td;
1222df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1223df8bae1dSRodney W. Grimes {
1224fb99ab88SMatthew Dillon 	int error;
1225df8bae1dSRodney W. Grimes 
12266c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1227df8bae1dSRodney W. Grimes 		return (EINVAL);
1228fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
12299c1ab3e0SJohn Baldwin 	error = killpg1(td, uap->signum, uap->pgid, 0);
1230fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
1231fb99ab88SMatthew Dillon 	return (error);
1232df8bae1dSRodney W. Grimes }
1233df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
1234df8bae1dSRodney W. Grimes 
1235df8bae1dSRodney W. Grimes /*
1236df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1237df8bae1dSRodney W. Grimes  */
1238df8bae1dSRodney W. Grimes void
12392c42a146SMarcel Moolenaar gsignal(pgid, sig)
12402c42a146SMarcel Moolenaar 	int pgid, sig;
1241df8bae1dSRodney W. Grimes {
1242df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1243df8bae1dSRodney W. Grimes 
1244f591779bSSeigo Tanimura 	if (pgid != 0) {
1245ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1246f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1247ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1248f591779bSSeigo Tanimura 		if (pgrp != NULL) {
12492c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1250f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1251f591779bSSeigo Tanimura 		}
1252f591779bSSeigo Tanimura 	}
1253df8bae1dSRodney W. Grimes }
1254df8bae1dSRodney W. Grimes 
1255df8bae1dSRodney W. Grimes /*
1256df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1257df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1258df8bae1dSRodney W. Grimes  */
1259df8bae1dSRodney W. Grimes void
12602c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1261df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
12622c42a146SMarcel Moolenaar 	int sig, checkctty;
1263df8bae1dSRodney W. Grimes {
1264df8bae1dSRodney W. Grimes 	register struct proc *p;
1265df8bae1dSRodney W. Grimes 
1266628d2653SJohn Baldwin 	if (pgrp) {
1267f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1268628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1269628d2653SJohn Baldwin 			PROC_LOCK(p);
1270df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
12712c42a146SMarcel Moolenaar 				psignal(p, sig);
1272628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1273628d2653SJohn Baldwin 		}
1274628d2653SJohn Baldwin 	}
1275df8bae1dSRodney W. Grimes }
1276df8bae1dSRodney W. Grimes 
1277df8bae1dSRodney W. Grimes /*
1278df8bae1dSRodney W. Grimes  * Send a signal caused by a trap to the current process.
1279df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1280df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1281356861dbSMatthew Dillon  *
1282356861dbSMatthew Dillon  * MPSAFE
1283df8bae1dSRodney W. Grimes  */
1284df8bae1dSRodney W. Grimes void
12852c42a146SMarcel Moolenaar trapsignal(p, sig, code)
1286df8bae1dSRodney W. Grimes 	struct proc *p;
12872c42a146SMarcel Moolenaar 	register int sig;
12888674077aSJeffrey Hsu 	u_long code;
1289df8bae1dSRodney W. Grimes {
1290df8bae1dSRodney W. Grimes 	register struct sigacts *ps = p->p_sigacts;
1291df8bae1dSRodney W. Grimes 
1292628d2653SJohn Baldwin 	PROC_LOCK(p);
12932c42a146SMarcel Moolenaar 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1294d96cfeaeSMarcel Moolenaar 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1295df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1296df8bae1dSRodney W. Grimes #ifdef KTRACE
1297374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1298374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
12992c42a146SMarcel Moolenaar 			    &p->p_sigmask, code);
1300df8bae1dSRodney W. Grimes #endif
1301a88b260aSJuli Mallett 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
1302a88b260aSJuli Mallett 						&p->p_sigmask, code);
13032c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
13042c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
13052c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
13062c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1307289ccde0SPeter Wemm 			/*
13088f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1309289ccde0SPeter Wemm 			 */
13102c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
13112c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
13122c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
13132c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
13142c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1315dedc04feSPeter Wemm 		}
13166f841fb7SMarcel Moolenaar 	} else {
13176626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
13182c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
13192c42a146SMarcel Moolenaar 		psignal(p, sig);
1320df8bae1dSRodney W. Grimes 	}
1321628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1322df8bae1dSRodney W. Grimes }
1323df8bae1dSRodney W. Grimes 
1324df8bae1dSRodney W. Grimes /*
1325df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1326df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1327df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1328df8bae1dSRodney W. Grimes  *
1329df8bae1dSRodney W. Grimes  * Exceptions:
1330df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1331df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1332df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1333df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1334df8bae1dSRodney W. Grimes  *
1335df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
1336df8bae1dSRodney W. Grimes  */
1337df8bae1dSRodney W. Grimes void
13382c42a146SMarcel Moolenaar psignal(p, sig)
1339df8bae1dSRodney W. Grimes 	register struct proc *p;
13402c42a146SMarcel Moolenaar 	register int sig;
1341df8bae1dSRodney W. Grimes {
1342df8bae1dSRodney W. Grimes 	register sig_t action;
1343b40ce416SJulian Elischer 	struct thread *td;
1344e602ba25SJulian Elischer 	register int prop;
1345e602ba25SJulian Elischer 
1346df8bae1dSRodney W. Grimes 
13472899d606SDag-Erling Smørgrav 	KASSERT(_SIG_VALID(sig),
13482899d606SDag-Erling Smørgrav 	    ("psignal(): invalid signal %d\n", sig));
13492c42a146SMarcel Moolenaar 
1350628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1351cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1352cb679c38SJonathan Lemon 
13532c42a146SMarcel Moolenaar 	prop = sigprop(sig);
1354df8bae1dSRodney W. Grimes 	/*
13552a024a2bSSean Eric Fagan 	 * If proc is traced, always give parent a chance;
13562a024a2bSSean Eric Fagan 	 * if signal event is tracked by procfs, give *that*
13572a024a2bSSean Eric Fagan 	 * a chance, as well.
1358df8bae1dSRodney W. Grimes 	 */
1359b40ce416SJulian Elischer 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1360df8bae1dSRodney W. Grimes 		action = SIG_DFL;
1361b40ce416SJulian Elischer 	} else {
1362df8bae1dSRodney W. Grimes 		/*
1363df8bae1dSRodney W. Grimes 		 * If the signal is being ignored,
1364df8bae1dSRodney W. Grimes 		 * then we forget about it immediately.
1365df8bae1dSRodney W. Grimes 		 * (Note: we don't set SIGCONT in p_sigignore,
1366df8bae1dSRodney W. Grimes 		 * and if it is set to SIG_IGN,
1367df8bae1dSRodney W. Grimes 		 * action will be SIG_DFL here.)
1368df8bae1dSRodney W. Grimes 		 */
1369628d2653SJohn Baldwin 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1370df8bae1dSRodney W. Grimes 			return;
13712c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigmask, sig))
1372df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
13732c42a146SMarcel Moolenaar 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1374df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
1375df8bae1dSRodney W. Grimes 		else
1376df8bae1dSRodney W. Grimes 			action = SIG_DFL;
1377df8bae1dSRodney W. Grimes 	}
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes 	if (prop & SA_CONT)
13801d9c5696SJuli Mallett 		SIG_STOPSIGMASK(p->p_siglist);
1381df8bae1dSRodney W. Grimes 
1382df8bae1dSRodney W. Grimes 	if (prop & SA_STOP) {
1383df8bae1dSRodney W. Grimes 		/*
1384df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
1385df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
1386df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
1387df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
1388df8bae1dSRodney W. Grimes 		 */
1389e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
1390e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
1391e602ba25SJulian Elischer 		    (action == SIG_DFL))
1392df8bae1dSRodney W. Grimes 		        return;
13931d9c5696SJuli Mallett 		SIG_CONTSIGMASK(p->p_siglist);
13946933e3c1SJulian Elischer 		p->p_flag &= ~P_CONTINUED;
1395df8bae1dSRodney W. Grimes 	}
13961d9c5696SJuli Mallett 	SIGADDSET(p->p_siglist, sig);
1397aa0fa334SJulian Elischer 	signotify(p);			/* uses schedlock */
1398df8bae1dSRodney W. Grimes 
1399df8bae1dSRodney W. Grimes 	/*
1400e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
1401e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
1402e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
1403e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
1404e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
1405e602ba25SJulian Elischer 	 * We try do the per-process part here.
1406df8bae1dSRodney W. Grimes 	 */
1407e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
1408e602ba25SJulian Elischer 		/*
1409e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
1410e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
1411e602ba25SJulian Elischer 		 */
1412e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1413e602ba25SJulian Elischer 			/*
1414e602ba25SJulian Elischer 			 * The traced process is already stopped,
1415e602ba25SJulian Elischer 			 * so no further action is necessary.
1416e602ba25SJulian Elischer 			 * No signal can restart us.
1417e602ba25SJulian Elischer 			 */
1418e602ba25SJulian Elischer 			goto out;
14191c32c37cSJohn Baldwin 		}
1420b40ce416SJulian Elischer 
1421e602ba25SJulian Elischer 		if (sig == SIGKILL) {
1422df8bae1dSRodney W. Grimes 			/*
1423e602ba25SJulian Elischer 			 * SIGKILL sets process running.
1424e602ba25SJulian Elischer 			 * It will die elsewhere.
1425e602ba25SJulian Elischer 			 * All threads must be restarted.
1426df8bae1dSRodney W. Grimes 			 */
1427e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED;
1428e602ba25SJulian Elischer 			goto runfast;
1429e602ba25SJulian Elischer 		}
1430e602ba25SJulian Elischer 
1431e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1432e602ba25SJulian Elischer 			/*
1433e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
14341d9c5696SJuli Mallett 			 * process but don't leave the signal in p_siglist as
14351d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
1436e602ba25SJulian Elischer 			 * continue the process and leave the signal in
14371d9c5696SJuli Mallett 			 * p_siglist.  If the process catches SIGCONT, let it
1438e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
1439e602ba25SJulian Elischer 			 * an event, it goes back to run state.
1440e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
1441e602ba25SJulian Elischer 			 */
14421279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
14436933e3c1SJulian Elischer 			p->p_flag |= P_CONTINUED;
1444e602ba25SJulian Elischer 			if (action == SIG_DFL) {
14451d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
1446e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
1447e602ba25SJulian Elischer 				/*
1448e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
1449e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
1450e602ba25SJulian Elischer 				 * It would seem that the answer would be to
1451e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
1452e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
1453e602ba25SJulian Elischer 				 * process, we need to make sure that the
1454e602ba25SJulian Elischer 				 * single thread is runnable asap.
1455e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
1456e602ba25SJulian Elischer 				 */
1457e602ba25SJulian Elischer 				goto runfast;
1458e602ba25SJulian Elischer 			}
1459e602ba25SJulian Elischer 			/*
1460e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
1461e602ba25SJulian Elischer 			 */
1462e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
146304774f23SJulian Elischer 			thread_unsuspend(p);
1464e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1465e602ba25SJulian Elischer 			goto out;
1466e602ba25SJulian Elischer 		}
1467e602ba25SJulian Elischer 
1468e602ba25SJulian Elischer 		if (prop & SA_STOP) {
1469e602ba25SJulian Elischer 			/*
1470e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
1471e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
147204774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
1473e602ba25SJulian Elischer 			 */
14741279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
14751d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
1476e602ba25SJulian Elischer 			goto out;
1477e602ba25SJulian Elischer 		}
1478e602ba25SJulian Elischer 
1479e602ba25SJulian Elischer 		/*
1480e602ba25SJulian Elischer 		 * All other kinds of signals:
1481e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
1482e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
1483e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
148404774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
1485e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
1486e602ba25SJulian Elischer 		 */
1487e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
1488e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
148971fad9fdSJulian Elischer 			if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1490e602ba25SJulian Elischer 				if (td->td_flags & TDF_CVWAITQ)
149171fad9fdSJulian Elischer 					cv_abort(td);
1492e602ba25SJulian Elischer 				else
149371fad9fdSJulian Elischer 					abortsleep(td);
1494e602ba25SJulian Elischer 			}
1495e602ba25SJulian Elischer 		}
1496e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
1497df8bae1dSRodney W. Grimes 		goto out;
1498df8bae1dSRodney W. Grimes 		/*
1499e602ba25SJulian Elischer 		 * XXXKSE  What about threads that are waiting on mutexes?
1500e602ba25SJulian Elischer 		 * Shouldn't they abort too?
150104774f23SJulian Elischer 		 * No, hopefully mutexes are short lived.. They'll
150204774f23SJulian Elischer 		 * eventually hit thread_suspend_check().
1503df8bae1dSRodney W. Grimes 		 */
1504e602ba25SJulian Elischer 	}  else if (p->p_state == PRS_NORMAL) {
1505e602ba25SJulian Elischer 		if (prop & SA_CONT) {
1506df8bae1dSRodney W. Grimes 			/*
1507e602ba25SJulian Elischer 			 * Already active, don't need to start again.
1508df8bae1dSRodney W. Grimes 			 */
15091d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
1510df8bae1dSRodney W. Grimes 			goto out;
1511df8bae1dSRodney W. Grimes 		}
151235c32a76SDavid Xu 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
151335c32a76SDavid Xu 			!(prop & SA_STOP)) {
1514721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
1515721e5910SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td)
1516721e5910SJulian Elischer 				tdsignal(td, sig, action);
1517721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
1518df8bae1dSRodney W. Grimes 			goto out;
151904774f23SJulian Elischer 		}
152035c32a76SDavid Xu 		if (prop & SA_STOP) {
152135c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
152235c32a76SDavid Xu 				goto out;
152335c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
152435c32a76SDavid Xu 			FOREACH_THREAD_IN_PROC(p, td) {
152571fad9fdSJulian Elischer 				if (TD_IS_SLEEPING(td) &&
152635c32a76SDavid Xu 					(td->td_flags & TDF_SINTR))
152735c32a76SDavid Xu 					thread_suspend_one(td);
152835c32a76SDavid Xu 			}
152935c32a76SDavid Xu 			if (p->p_suspcount == p->p_numthreads) {
153035c32a76SDavid Xu 				mtx_unlock_spin(&sched_lock);
153135c32a76SDavid Xu 				stop(p);
153235c32a76SDavid Xu 				p->p_xstat = sig;
15331d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
153435c32a76SDavid Xu 				PROC_LOCK(p->p_pptr);
153535c32a76SDavid Xu 				if ((p->p_pptr->p_procsig->ps_flag &
153635c32a76SDavid Xu 					PS_NOCLDSTOP) == 0) {
153735c32a76SDavid Xu 					psignal(p->p_pptr, SIGCHLD);
153835c32a76SDavid Xu 				}
153935c32a76SDavid Xu 				PROC_UNLOCK(p->p_pptr);
154035c32a76SDavid Xu 			} else {
154135c32a76SDavid Xu 				mtx_unlock_spin(&sched_lock);
154235c32a76SDavid Xu 			}
154335c32a76SDavid Xu 			goto out;
154435c32a76SDavid Xu 		}
1545721e5910SJulian Elischer 		else
1546df8bae1dSRodney W. Grimes 			goto runfast;
1547df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1548e602ba25SJulian Elischer 	} else {
1549e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
15501d9c5696SJuli Mallett 		SIGDELSET(p->p_siglist, sig);
1551e602ba25SJulian Elischer 		goto out;
1552e602ba25SJulian Elischer 	}
1553e602ba25SJulian Elischer 
1554b40ce416SJulian Elischer 	/*
1555e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
1556e602ba25SJulian Elischer 	 * running threads.
1557b40ce416SJulian Elischer 	 */
1558e602ba25SJulian Elischer 
1559e602ba25SJulian Elischer runfast:
1560aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
1561e602ba25SJulian Elischer 	FOREACH_THREAD_IN_PROC(p, td)
1562e602ba25SJulian Elischer 		tdsignal(td, sig, action);
1563e602ba25SJulian Elischer 	thread_unsuspend(p);
1564e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
1565e602ba25SJulian Elischer out:
1566e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
1567e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
1568e602ba25SJulian Elischer }
1569e602ba25SJulian Elischer 
1570e602ba25SJulian Elischer /*
1571e602ba25SJulian Elischer  * The force of a signal has been directed against a single
1572e602ba25SJulian Elischer  * thread. We need to see what we can do about knocking it
1573e602ba25SJulian Elischer  * out of any sleep it may be in etc.
1574e602ba25SJulian Elischer  */
1575e602ba25SJulian Elischer static void
1576e602ba25SJulian Elischer tdsignal(struct thread *td, int sig, sig_t action)
1577e602ba25SJulian Elischer {
1578e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
1579e602ba25SJulian Elischer 	register int prop;
1580e602ba25SJulian Elischer 
1581aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
1582e602ba25SJulian Elischer 	prop = sigprop(sig);
1583e602ba25SJulian Elischer 	/*
1584aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
1585e602ba25SJulian Elischer 	 * killed in this lifetime.
1586e602ba25SJulian Elischer 	 */
1587e602ba25SJulian Elischer 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1588e602ba25SJulian Elischer 		if (td->td_priority > PUSER) {
1589e602ba25SJulian Elischer 			td->td_priority = PUSER;
1590b40ce416SJulian Elischer 		}
1591b40ce416SJulian Elischer 	}
1592e602ba25SJulian Elischer 
1593e602ba25SJulian Elischer 	/*
1594e602ba25SJulian Elischer 	 * Defer further processing for signals which are held,
1595e602ba25SJulian Elischer 	 * except that stopped processes must be continued by SIGCONT.
1596e602ba25SJulian Elischer 	 */
1597e602ba25SJulian Elischer 	if (action == SIG_HOLD) {
1598aa0fa334SJulian Elischer 		return;
1599e602ba25SJulian Elischer 	}
160071fad9fdSJulian Elischer 	if (TD_IS_SLEEPING(td)) {
1601e602ba25SJulian Elischer 		/*
1602e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
1603e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
1604e602ba25SJulian Elischer 		 * be noticed when the process returns through
1605e602ba25SJulian Elischer 		 * trap() or syscall().
1606e602ba25SJulian Elischer 		 */
1607e602ba25SJulian Elischer 		if ((td->td_flags & TDF_SINTR) == 0) {
1608aa0fa334SJulian Elischer 			return;
1609e602ba25SJulian Elischer 		}
1610e602ba25SJulian Elischer 		/*
1611e602ba25SJulian Elischer 		 * Process is sleeping and traced.  Make it runnable
1612e602ba25SJulian Elischer 		 * so it can discover the signal in issignal() and stop
1613e602ba25SJulian Elischer 		 * for its parent.
1614e602ba25SJulian Elischer 		 */
1615e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
1616e602ba25SJulian Elischer 			p->p_flag &= ~P_STOPPED_TRACE;
1617aa0fa334SJulian Elischer 		} else {
1618aa0fa334SJulian Elischer 
1619df8bae1dSRodney W. Grimes 			/*
1620e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored) and process is
1621e602ba25SJulian Elischer 			 * asleep, we are finished; the process should not
1622e602ba25SJulian Elischer 			 * be awakened.
1623df8bae1dSRodney W. Grimes 			 */
1624e602ba25SJulian Elischer 			if ((prop & SA_CONT) && action == SIG_DFL) {
16251d9c5696SJuli Mallett 				SIGDELSET(p->p_siglist, sig);
1626aa0fa334SJulian Elischer 				return;
1627df8bae1dSRodney W. Grimes 			}
1628df8bae1dSRodney W. Grimes 
1629aa0fa334SJulian Elischer 			/*
1630aa0fa334SJulian Elischer 			 * Raise priority to at least PUSER.
1631aa0fa334SJulian Elischer 			 */
1632aa0fa334SJulian Elischer 			if (td->td_priority > PUSER) {
1633aa0fa334SJulian Elischer 				td->td_priority = PUSER;
1634aa0fa334SJulian Elischer 			}
1635aa0fa334SJulian Elischer 		}
163671fad9fdSJulian Elischer 		if (td->td_flags & TDF_CVWAITQ)
163771fad9fdSJulian Elischer 			cv_abort(td);
163871fad9fdSJulian Elischer 		else
163971fad9fdSJulian Elischer 			abortsleep(td);
1640aa0fa334SJulian Elischer 	}
1641aa0fa334SJulian Elischer #ifdef SMP
1642aa0fa334SJulian Elischer 	  else {
1643df8bae1dSRodney W. Grimes 		/*
1644e602ba25SJulian Elischer 		 * Other states do nothing with the signal immediatly,
1645df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
1646df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
1647df8bae1dSRodney W. Grimes 		 */
164871fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td) && td != curthread) {
1649e602ba25SJulian Elischer 			forward_signal(td);
1650aa0fa334SJulian Elischer 		}
16510ac3b636SAndrew Gallatin 	  }
16523163861cSTor Egge #endif
16536caa8a15SJohn Baldwin }
1654df8bae1dSRodney W. Grimes 
1655df8bae1dSRodney W. Grimes /*
1656df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
1657df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
1658df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
1659df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
1660df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
1661628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
1662df8bae1dSRodney W. Grimes  * sequence is
1663df8bae1dSRodney W. Grimes  *
1664e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
16652c42a146SMarcel Moolenaar  *		postsig(sig);
1666df8bae1dSRodney W. Grimes  */
166726f9a767SRodney W. Grimes int
1668e602ba25SJulian Elischer issignal(td)
1669e602ba25SJulian Elischer 	struct thread *td;
1670df8bae1dSRodney W. Grimes {
1671e602ba25SJulian Elischer 	struct proc *p;
16722c42a146SMarcel Moolenaar 	sigset_t mask;
16732c42a146SMarcel Moolenaar 	register int sig, prop;
1674df8bae1dSRodney W. Grimes 
1675e602ba25SJulian Elischer 	p = td->td_proc;
1676628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1677b39f3284SJulian Elischer 	WITNESS_SLEEP(1, &p->p_mtx.mtx_object);
1678df8bae1dSRodney W. Grimes 	for (;;) {
16792a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
16802a024a2bSSean Eric Fagan 
16811d9c5696SJuli Mallett 		mask = p->p_siglist;
16822c42a146SMarcel Moolenaar 		SIGSETNAND(mask, p->p_sigmask);
1683df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
16842c42a146SMarcel Moolenaar 			SIG_STOPSIGMASK(mask);
1685ca18d53eSChad David 		if (SIGISEMPTY(mask))		/* no signal to send */
1686df8bae1dSRodney W. Grimes 			return (0);
16871d9c5696SJuli Mallett 		sig = sig_ffs(&mask);
16882c42a146SMarcel Moolenaar 		prop = sigprop(sig);
16892a024a2bSSean Eric Fagan 
1690628d2653SJohn Baldwin 		_STOPEVENT(p, S_SIG, sig);
16912a024a2bSSean Eric Fagan 
1692df8bae1dSRodney W. Grimes 		/*
1693df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
1694df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
1695df8bae1dSRodney W. Grimes 		 */
16962c42a146SMarcel Moolenaar 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
16971d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);
1698df8bae1dSRodney W. Grimes 			continue;
1699df8bae1dSRodney W. Grimes 		}
1700df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1701df8bae1dSRodney W. Grimes 			/*
1702d8f4f6a4SJonathan Mini 			 * If traced, always stop.
1703df8bae1dSRodney W. Grimes 			 */
17042c42a146SMarcel Moolenaar 			p->p_xstat = sig;
1705628d2653SJohn Baldwin 			PROC_LOCK(p->p_pptr);
1706df8bae1dSRodney W. Grimes 			psignal(p->p_pptr, SIGCHLD);
1707628d2653SJohn Baldwin 			PROC_UNLOCK(p->p_pptr);
17089ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
17094d492b43SJulian Elischer 			stop(p);	/* uses schedlock too eventually */
171071fad9fdSJulian Elischer 			thread_suspend_one(td);
1711c86b6ff5SJohn Baldwin 			PROC_UNLOCK(p);
1712c86b6ff5SJohn Baldwin 			DROP_GIANT();
17132ad7d304SJohn Baldwin 			p->p_stats->p_ru.ru_nivcsw++;
1714df8bae1dSRodney W. Grimes 			mi_switch();
17159ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
171620cdcc5bSJohn Baldwin 			PICKUP_GIANT();
1717628d2653SJohn Baldwin 			PROC_LOCK(p);
1718df8bae1dSRodney W. Grimes 
1719df8bae1dSRodney W. Grimes 			/*
1720df8bae1dSRodney W. Grimes 			 * If the traced bit got turned off, go back up
1721df8bae1dSRodney W. Grimes 			 * to the top to rescan signals.  This ensures
1722df8bae1dSRodney W. Grimes 			 * that p_sig* and ps_sigact are consistent.
1723df8bae1dSRodney W. Grimes 			 */
1724df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_TRACED) == 0)
1725df8bae1dSRodney W. Grimes 				continue;
1726df8bae1dSRodney W. Grimes 
1727df8bae1dSRodney W. Grimes 			/*
1728df8bae1dSRodney W. Grimes 			 * If parent wants us to take the signal,
1729df8bae1dSRodney W. Grimes 			 * then it will leave it in p->p_xstat;
1730df8bae1dSRodney W. Grimes 			 * otherwise we just look for signals again.
1731df8bae1dSRodney W. Grimes 			 */
17321d9c5696SJuli Mallett 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
17332c42a146SMarcel Moolenaar 			sig = p->p_xstat;
17342c42a146SMarcel Moolenaar 			if (sig == 0)
1735df8bae1dSRodney W. Grimes 				continue;
1736df8bae1dSRodney W. Grimes 
1737df8bae1dSRodney W. Grimes 			/*
17381d9c5696SJuli Mallett 			 * Put the new signal into p_siglist.  If the
17391d9c5696SJuli Mallett 			 * signal is being masked, look for other signals.
1740df8bae1dSRodney W. Grimes 			 */
17411d9c5696SJuli Mallett 			SIGADDSET(p->p_siglist, sig);
17422c42a146SMarcel Moolenaar 			if (SIGISMEMBER(p->p_sigmask, sig))
1743df8bae1dSRodney W. Grimes 				continue;
17441c530be4SBruce Evans 			signotify(p);
1745df8bae1dSRodney W. Grimes 		}
1746df8bae1dSRodney W. Grimes 
1747df8bae1dSRodney W. Grimes 		/*
1748df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
1749df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
1750df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
1751df8bae1dSRodney W. Grimes 		 */
17522c42a146SMarcel Moolenaar 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1753df8bae1dSRodney W. Grimes 
17540b53fbe8SBruce Evans 		case (int)SIG_DFL:
1755df8bae1dSRodney W. Grimes 			/*
1756df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
1757df8bae1dSRodney W. Grimes 			 */
1758df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
1759df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1760df8bae1dSRodney W. Grimes 				/*
1761df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
1762df8bae1dSRodney W. Grimes 				 * in init? XXX
1763df8bae1dSRodney W. Grimes 				 */
1764d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
17652c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
1766df8bae1dSRodney W. Grimes #endif
1767df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1768df8bae1dSRodney W. Grimes 			}
1769df8bae1dSRodney W. Grimes 			/*
1770df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
1771df8bae1dSRodney W. Grimes 			 * with default action, stop here,
1772df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
1773df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
1774df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
1775df8bae1dSRodney W. Grimes 			 */
1776df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
1777df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
1778df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
1779df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
1780df8bae1dSRodney W. Grimes 					break;	/* == ignore */
17812c42a146SMarcel Moolenaar 				p->p_xstat = sig;
1782721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
1783721e5910SJulian Elischer 				if (p->p_suspcount+1 == p->p_numthreads) {
1784721e5910SJulian Elischer 					mtx_unlock_spin(&sched_lock);
1785628d2653SJohn Baldwin 					PROC_LOCK(p->p_pptr);
1786e602ba25SJulian Elischer 					if ((p->p_pptr->p_procsig->ps_flag &
1787e602ba25SJulian Elischer 				    		PS_NOCLDSTOP) == 0) {
1788df8bae1dSRodney W. Grimes 						psignal(p->p_pptr, SIGCHLD);
1789e602ba25SJulian Elischer 					}
1790628d2653SJohn Baldwin 					PROC_UNLOCK(p->p_pptr);
1791d9d6e34fSJulian Elischer 					mtx_lock_spin(&sched_lock);
1792721e5910SJulian Elischer 				}
17935b3047d5SJohn Baldwin 				stop(p);
179471fad9fdSJulian Elischer 				thread_suspend_one(td);
1795721e5910SJulian Elischer 				PROC_UNLOCK(p);
1796721e5910SJulian Elischer 				DROP_GIANT();
1797721e5910SJulian Elischer 				p->p_stats->p_ru.ru_nivcsw++;
1798721e5910SJulian Elischer 				mi_switch();
1799721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
1800721e5910SJulian Elischer 				PICKUP_GIANT();
1801721e5910SJulian Elischer 				PROC_LOCK(p);
1802df8bae1dSRodney W. Grimes 				break;
180321b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
1804df8bae1dSRodney W. Grimes 				/*
1805df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
1806df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
1807df8bae1dSRodney W. Grimes 				 */
1808df8bae1dSRodney W. Grimes 				break;		/* == ignore */
1809df8bae1dSRodney W. Grimes 			} else
18102c42a146SMarcel Moolenaar 				return (sig);
1811df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
1812df8bae1dSRodney W. Grimes 
18130b53fbe8SBruce Evans 		case (int)SIG_IGN:
1814df8bae1dSRodney W. Grimes 			/*
1815df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
1816df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
1817df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
1818df8bae1dSRodney W. Grimes 			 */
1819df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
1820df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
1821df8bae1dSRodney W. Grimes 				printf("issignal\n");
1822df8bae1dSRodney W. Grimes 			break;		/* == ignore */
1823df8bae1dSRodney W. Grimes 
1824df8bae1dSRodney W. Grimes 		default:
1825df8bae1dSRodney W. Grimes 			/*
1826df8bae1dSRodney W. Grimes 			 * This signal has an action, let
1827df8bae1dSRodney W. Grimes 			 * postsig() process it.
1828df8bae1dSRodney W. Grimes 			 */
18292c42a146SMarcel Moolenaar 			return (sig);
1830df8bae1dSRodney W. Grimes 		}
18311d9c5696SJuli Mallett 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1832df8bae1dSRodney W. Grimes 	}
1833df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1834df8bae1dSRodney W. Grimes }
1835df8bae1dSRodney W. Grimes 
1836df8bae1dSRodney W. Grimes /*
1837df8bae1dSRodney W. Grimes  * Put the argument process into the stopped state and notify the parent
1838df8bae1dSRodney W. Grimes  * via wakeup.  Signals are handled elsewhere.  The process must not be
18395b3047d5SJohn Baldwin  * on the run queue.  Must be called with the proc p locked and the scheduler
18405b3047d5SJohn Baldwin  * lock held.
1841df8bae1dSRodney W. Grimes  */
18425b3047d5SJohn Baldwin static void
1843df8bae1dSRodney W. Grimes stop(p)
1844df8bae1dSRodney W. Grimes 	register struct proc *p;
1845df8bae1dSRodney W. Grimes {
1846df8bae1dSRodney W. Grimes 
1847628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
18481279572aSDavid Xu 	p->p_flag |= P_STOPPED_SIG;
1849df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_WAITED;
185001609114SAlfred Perlstein 	wakeup(p->p_pptr);
1851df8bae1dSRodney W. Grimes }
1852df8bae1dSRodney W. Grimes 
1853df8bae1dSRodney W. Grimes /*
1854df8bae1dSRodney W. Grimes  * Take the action for the specified signal
1855df8bae1dSRodney W. Grimes  * from the current set of pending signals.
1856df8bae1dSRodney W. Grimes  */
1857df8bae1dSRodney W. Grimes void
18582c42a146SMarcel Moolenaar postsig(sig)
18592c42a146SMarcel Moolenaar 	register int sig;
1860df8bae1dSRodney W. Grimes {
1861b40ce416SJulian Elischer 	struct thread *td = curthread;
1862b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
1863628d2653SJohn Baldwin 	struct sigacts *ps;
18642c42a146SMarcel Moolenaar 	sig_t action;
18652c42a146SMarcel Moolenaar 	sigset_t returnmask;
18662c42a146SMarcel Moolenaar 	int code;
1867df8bae1dSRodney W. Grimes 
18682c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
18695526d2d9SEivind Eklund 
18702ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1871628d2653SJohn Baldwin 	ps = p->p_sigacts;
18721d9c5696SJuli Mallett 	SIGDELSET(p->p_siglist, sig);
18732c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
1874df8bae1dSRodney W. Grimes #ifdef KTRACE
1875374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
1876374a15aaSJohn Baldwin 		ktrpsig(sig, action, p->p_flag & P_OLDMASK ?
18772c42a146SMarcel Moolenaar 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1878df8bae1dSRodney W. Grimes #endif
1879628d2653SJohn Baldwin 	_STOPEVENT(p, S_SIG, sig);
18802a024a2bSSean Eric Fagan 
1881df8bae1dSRodney W. Grimes 	if (action == SIG_DFL) {
1882df8bae1dSRodney W. Grimes 		/*
1883df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
1884df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
1885df8bae1dSRodney W. Grimes 		 */
1886b40ce416SJulian Elischer 		sigexit(td, sig);
1887df8bae1dSRodney W. Grimes 		/* NOTREACHED */
1888df8bae1dSRodney W. Grimes 	} else {
1889df8bae1dSRodney W. Grimes 		/*
1890df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
1891df8bae1dSRodney W. Grimes 		 */
18922c42a146SMarcel Moolenaar 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
18935526d2d9SEivind Eklund 		    ("postsig action"));
1894df8bae1dSRodney W. Grimes 		/*
1895df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
1896645682fdSLuoqi Chen 		 * occurrences of this signal.
1897df8bae1dSRodney W. Grimes 		 *
1898645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
1899df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
1900645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
1901df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
1902df8bae1dSRodney W. Grimes 		 */
1903645682fdSLuoqi Chen 		if (p->p_flag & P_OLDMASK) {
19046626c604SJulian Elischer 			returnmask = p->p_oldsigmask;
1905645682fdSLuoqi Chen 			p->p_flag &= ~P_OLDMASK;
1906df8bae1dSRodney W. Grimes 		} else
1907df8bae1dSRodney W. Grimes 			returnmask = p->p_sigmask;
19082c42a146SMarcel Moolenaar 
19092c42a146SMarcel Moolenaar 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
19102c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
19112c42a146SMarcel Moolenaar 			SIGADDSET(p->p_sigmask, sig);
19122c42a146SMarcel Moolenaar 
19132c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1914289ccde0SPeter Wemm 			/*
19158f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1916289ccde0SPeter Wemm 			 */
19172c42a146SMarcel Moolenaar 			SIGDELSET(p->p_sigcatch, sig);
19182c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
19192c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
19202c42a146SMarcel Moolenaar 				SIGADDSET(p->p_sigignore, sig);
19212c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1922dedc04feSPeter Wemm 		}
1923df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
19242c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
1925df8bae1dSRodney W. Grimes 			code = 0;
1926df8bae1dSRodney W. Grimes 		} else {
19276626c604SJulian Elischer 			code = p->p_code;
19286626c604SJulian Elischer 			p->p_code = 0;
19296626c604SJulian Elischer 			p->p_sig = 0;
1930df8bae1dSRodney W. Grimes 		}
1931c76e33b6SJonathan Mini 		if (p->p_flag & P_KSES)
1932c76e33b6SJonathan Mini 			if (signal_upcall(p, sig))
1933c76e33b6SJonathan Mini 				return;
1934a88b260aSJuli Mallett 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1935df8bae1dSRodney W. Grimes 	}
1936df8bae1dSRodney W. Grimes }
1937df8bae1dSRodney W. Grimes 
1938df8bae1dSRodney W. Grimes /*
1939df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
1940df8bae1dSRodney W. Grimes  */
194126f9a767SRodney W. Grimes void
1942df8bae1dSRodney W. Grimes killproc(p, why)
1943df8bae1dSRodney W. Grimes 	struct proc *p;
1944df8bae1dSRodney W. Grimes 	char *why;
1945df8bae1dSRodney W. Grimes {
19469081e5e8SJohn Baldwin 
19479081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
19480384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
19490384fff8SJason Evans 		p, p->p_pid, p->p_comm);
1950729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1951b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1952df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
1953df8bae1dSRodney W. Grimes }
1954df8bae1dSRodney W. Grimes 
1955df8bae1dSRodney W. Grimes /*
1956df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
1957df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
1958df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
1959df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
1960df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
1961df8bae1dSRodney W. Grimes  * does not return.
1962df8bae1dSRodney W. Grimes  */
196326f9a767SRodney W. Grimes void
1964b40ce416SJulian Elischer sigexit(td, sig)
1965b40ce416SJulian Elischer 	struct thread *td;
19662c42a146SMarcel Moolenaar 	int sig;
1967df8bae1dSRodney W. Grimes {
1968b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1969df8bae1dSRodney W. Grimes 
1970628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1971df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
19722c42a146SMarcel Moolenaar 	if (sigprop(sig) & SA_CORE) {
19732c42a146SMarcel Moolenaar 		p->p_sig = sig;
1974c364e17eSAndrey A. Chernov 		/*
1975c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
1976c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
1977c364e17eSAndrey A. Chernov 		 * these messages.)
1978c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
1979c364e17eSAndrey A. Chernov 		 */
1980628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1981c31146a1SJohn Baldwin 		if (!mtx_owned(&Giant))
1982c31146a1SJohn Baldwin 			mtx_lock(&Giant);
1983b40ce416SJulian Elischer 		if (coredump(td) == 0)
19842c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
198557308494SJoerg Wunsch 		if (kern_logsigexit)
198657308494SJoerg Wunsch 			log(LOG_INFO,
198757308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
19883d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
19899c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
19902c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
19912c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
1992c31146a1SJohn Baldwin 	} else {
1993628d2653SJohn Baldwin 		PROC_UNLOCK(p);
1994628d2653SJohn Baldwin 		if (!mtx_owned(&Giant))
1995628d2653SJohn Baldwin 			mtx_lock(&Giant);
1996c31146a1SJohn Baldwin 	}
1997b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
1998df8bae1dSRodney W. Grimes 	/* NOTREACHED */
1999df8bae1dSRodney W. Grimes }
2000df8bae1dSRodney W. Grimes 
2001c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2002c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2003c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2004c5edb423SSean Eric Fagan 
2005c5edb423SSean Eric Fagan /*
2006c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2007c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2008c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2009c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2010c5edb423SSean Eric Fagan  *	%P	process id (pid)
2011c5edb423SSean Eric Fagan  *	%U	user id (uid)
2012c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2013c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2014c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2015c5edb423SSean Eric Fagan  */
2016c5edb423SSean Eric Fagan 
2017fca666a1SJulian Elischer static char *
2018c5edb423SSean Eric Fagan expand_name(name, uid, pid)
20198b43b535SAlfred Perlstein 	const char *name;
20208b43b535SAlfred Perlstein 	uid_t uid;
20218b43b535SAlfred Perlstein 	pid_t pid;
20228b43b535SAlfred Perlstein {
20238b43b535SAlfred Perlstein 	const char *format, *appendstr;
2024c5edb423SSean Eric Fagan 	char *temp;
2025c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
20268b43b535SAlfred Perlstein 	size_t i, l, n;
2027c5edb423SSean Eric Fagan 
20288b43b535SAlfred Perlstein 	format = corefilename;
20298b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
20300bfe2990SEivind Eklund 	if (temp == NULL)
20318b43b535SAlfred Perlstein 		return (NULL);
2032ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2033c5edb423SSean Eric Fagan 		switch (format[i]) {
2034c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2035c5edb423SSean Eric Fagan 			i++;
2036c5edb423SSean Eric Fagan 			switch (format[i]) {
2037c5edb423SSean Eric Fagan 			case '%':
20388b43b535SAlfred Perlstein 				appendstr = "%";
2039c5edb423SSean Eric Fagan 				break;
2040c5edb423SSean Eric Fagan 			case 'N':	/* process name */
20418b43b535SAlfred Perlstein 				appendstr = name;
2042c5edb423SSean Eric Fagan 				break;
2043c5edb423SSean Eric Fagan 			case 'P':	/* process id */
20448b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
20458b43b535SAlfred Perlstein 				appendstr = buf;
2046c5edb423SSean Eric Fagan 				break;
2047c5edb423SSean Eric Fagan 			case 'U':	/* user id */
20488b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
20498b43b535SAlfred Perlstein 				appendstr = buf;
2050c5edb423SSean Eric Fagan 				break;
2051c5edb423SSean Eric Fagan 			default:
20528b43b535SAlfred Perlstein 				appendstr = "";
20538b43b535SAlfred Perlstein 			  	log(LOG_ERR,
20548b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
20558b43b535SAlfred Perlstein 				    format[i], format);
2056c5edb423SSean Eric Fagan 			}
20578b43b535SAlfred Perlstein 			l = strlen(appendstr);
20588b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
20598b43b535SAlfred Perlstein 				goto toolong;
20608b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
20618b43b535SAlfred Perlstein 			n += l;
2062c5edb423SSean Eric Fagan 			break;
2063c5edb423SSean Eric Fagan 		default:
2064c5edb423SSean Eric Fagan 			temp[n++] = format[i];
2065c5edb423SSean Eric Fagan 		}
2066c5edb423SSean Eric Fagan 	}
20678b43b535SAlfred Perlstein 	if (format[i] != '\0')
20688b43b535SAlfred Perlstein 		goto toolong;
20698b43b535SAlfred Perlstein 	return (temp);
20708b43b535SAlfred Perlstein toolong:
20718b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
20728b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
20738b43b535SAlfred Perlstein 	free(temp, M_TEMP);
20748b43b535SAlfred Perlstein 	return (NULL);
2075c5edb423SSean Eric Fagan }
2076c5edb423SSean Eric Fagan 
2077df8bae1dSRodney W. Grimes /*
2078fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
2079fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
2080fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
2081fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
2082fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
2083fca666a1SJulian Elischer  */
2084fca666a1SJulian Elischer 
2085fca666a1SJulian Elischer static int
2086b40ce416SJulian Elischer coredump(struct thread *td)
2087fca666a1SJulian Elischer {
2088b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2089fca666a1SJulian Elischer 	register struct vnode *vp;
20909c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
209106ae1e91SMatthew Dillon 	struct flock lf;
2092fca666a1SJulian Elischer 	struct nameidata nd;
2093fca666a1SJulian Elischer 	struct vattr vattr;
2094e6796b67SKirk McKusick 	int error, error1, flags;
2095f2a2857bSKirk McKusick 	struct mount *mp;
2096fca666a1SJulian Elischer 	char *name;			/* name of corefile */
2097fca666a1SJulian Elischer 	off_t limit;
2098fca666a1SJulian Elischer 
2099628d2653SJohn Baldwin 	PROC_LOCK(p);
2100628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
2101fca666a1SJulian Elischer 
2102628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2103628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2104fca666a1SJulian Elischer 		return (EFAULT);
2105628d2653SJohn Baldwin 	}
2106fca666a1SJulian Elischer 
2107fca666a1SJulian Elischer 	/*
210835a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
210935a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
211035a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
211135a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
211235a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
211335a2598fSSean Eric Fagan 	 * if it is larger than the limit.
2114fca666a1SJulian Elischer 	 */
211535a2598fSSean Eric Fagan 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2116628d2653SJohn Baldwin 	if (limit == 0) {
2117628d2653SJohn Baldwin 		PROC_UNLOCK(p);
211835a2598fSSean Eric Fagan 		return 0;
2119628d2653SJohn Baldwin 	}
2120628d2653SJohn Baldwin 	PROC_UNLOCK(p);
212135a2598fSSean Eric Fagan 
2122f2a2857bSKirk McKusick restart:
21239c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2124ccdbd10cSPeter Pentchev 	if (name == NULL)
2125ccdbd10cSPeter Pentchev 		return (EINVAL);
2126b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2127e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2128e6796b67SKirk McKusick 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2129fca666a1SJulian Elischer 	free(name, M_TEMP);
2130fca666a1SJulian Elischer 	if (error)
2131fca666a1SJulian Elischer 		return (error);
2132762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
2133fca666a1SJulian Elischer 	vp = nd.ni_vp;
213406ae1e91SMatthew Dillon 
2135832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
2136832dafadSDon Lewis 	if (vp->v_type != VREG ||
2137832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2138832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
2139832dafadSDon Lewis 		error = EFAULT;
2140832dafadSDon Lewis 		goto out2;
2141832dafadSDon Lewis 	}
2142832dafadSDon Lewis 
2143b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
214406ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
214506ae1e91SMatthew Dillon 	lf.l_start = 0;
214606ae1e91SMatthew Dillon 	lf.l_len = 0;
214706ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
214806ae1e91SMatthew Dillon 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
214906ae1e91SMatthew Dillon 	if (error)
215006ae1e91SMatthew Dillon 		goto out2;
215106ae1e91SMatthew Dillon 
215206ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
215306ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
215406ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2155b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2156f2a2857bSKirk McKusick 			return (error);
2157f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2158f2a2857bSKirk McKusick 			return (error);
2159f2a2857bSKirk McKusick 		goto restart;
2160f2a2857bSKirk McKusick 	}
2161fca666a1SJulian Elischer 
2162fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
2163fca666a1SJulian Elischer 	vattr.va_size = 0;
216488b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2165b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2166b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
216788b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
2168628d2653SJohn Baldwin 	PROC_LOCK(p);
2169fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
2170628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2171fca666a1SJulian Elischer 
2172fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
2173b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
2174fca666a1SJulian Elischer 	  ENOSYS;
2175fca666a1SJulian Elischer 
217606ae1e91SMatthew Dillon 	lf.l_type = F_UNLCK;
217706ae1e91SMatthew Dillon 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2178f2a2857bSKirk McKusick 	vn_finished_write(mp);
217906ae1e91SMatthew Dillon out2:
2180b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
2181fca666a1SJulian Elischer 	if (error == 0)
2182fca666a1SJulian Elischer 		error = error1;
2183fca666a1SJulian Elischer 	return (error);
2184fca666a1SJulian Elischer }
2185fca666a1SJulian Elischer 
2186fca666a1SJulian Elischer /*
2187df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
2188df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
2189df8bae1dSRodney W. Grimes  */
2190d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2191df8bae1dSRodney W. Grimes struct nosys_args {
2192df8bae1dSRodney W. Grimes 	int	dummy;
2193df8bae1dSRodney W. Grimes };
2194d2d3e875SBruce Evans #endif
2195fb99ab88SMatthew Dillon /*
2196fb99ab88SMatthew Dillon  * MPSAFE
2197fb99ab88SMatthew Dillon  */
2198df8bae1dSRodney W. Grimes /* ARGSUSED */
219926f9a767SRodney W. Grimes int
2200b40ce416SJulian Elischer nosys(td, args)
2201b40ce416SJulian Elischer 	struct thread *td;
2202df8bae1dSRodney W. Grimes 	struct nosys_args *args;
2203df8bae1dSRodney W. Grimes {
2204b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2205b40ce416SJulian Elischer 
2206fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
2207628d2653SJohn Baldwin 	PROC_LOCK(p);
2208df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
2209628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2210fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
2211f5216b9aSBruce Evans 	return (ENOSYS);
2212df8bae1dSRodney W. Grimes }
2213831d27a9SDon Lewis 
2214831d27a9SDon Lewis /*
221548f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
2216831d27a9SDon Lewis  * stored credentials rather than those of the current process.
2217831d27a9SDon Lewis  */
2218831d27a9SDon Lewis void
2219f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
2220f1320723SAlfred Perlstein 	struct sigio **sigiop;
22212c42a146SMarcel Moolenaar 	int sig, checkctty;
2222831d27a9SDon Lewis {
2223f1320723SAlfred Perlstein 	struct sigio *sigio;
2224831d27a9SDon Lewis 
2225f1320723SAlfred Perlstein 	SIGIO_LOCK();
2226f1320723SAlfred Perlstein 	sigio = *sigiop;
2227f1320723SAlfred Perlstein 	if (sigio == NULL) {
2228f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
2229f1320723SAlfred Perlstein 		return;
2230f1320723SAlfred Perlstein 	}
2231831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
2232628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
22332b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
22342c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
2235628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
2236831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
2237831d27a9SDon Lewis 		struct proc *p;
2238831d27a9SDon Lewis 
2239f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
2240628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2241628d2653SJohn Baldwin 			PROC_LOCK(p);
22422b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2243831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
22442c42a146SMarcel Moolenaar 				psignal(p, sig);
2245628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2246628d2653SJohn Baldwin 		}
2247f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
2248831d27a9SDon Lewis 	}
2249f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
2250831d27a9SDon Lewis }
2251cb679c38SJonathan Lemon 
2252cb679c38SJonathan Lemon static int
2253cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
2254cb679c38SJonathan Lemon {
2255cb679c38SJonathan Lemon 	struct proc *p = curproc;
2256cb679c38SJonathan Lemon 
2257cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
2258cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2259cb679c38SJonathan Lemon 
2260628d2653SJohn Baldwin 	PROC_LOCK(p);
2261cb679c38SJonathan Lemon 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2262628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2263cb679c38SJonathan Lemon 
2264cb679c38SJonathan Lemon 	return (0);
2265cb679c38SJonathan Lemon }
2266cb679c38SJonathan Lemon 
2267cb679c38SJonathan Lemon static void
2268cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
2269cb679c38SJonathan Lemon {
2270cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
2271cb679c38SJonathan Lemon 
2272628d2653SJohn Baldwin 	PROC_LOCK(p);
2273e3975643SJake Burkholder 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2274628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2275cb679c38SJonathan Lemon }
2276cb679c38SJonathan Lemon 
2277cb679c38SJonathan Lemon /*
2278cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
2279cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
2280cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
2281cb679c38SJonathan Lemon  * isn't worth the trouble.
2282cb679c38SJonathan Lemon  */
2283cb679c38SJonathan Lemon static int
2284cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
2285cb679c38SJonathan Lemon {
2286cb679c38SJonathan Lemon 
2287cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
2288cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
2289cb679c38SJonathan Lemon 
2290cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
2291cb679c38SJonathan Lemon 			kn->kn_data++;
2292cb679c38SJonathan Lemon 	}
2293cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
2294cb679c38SJonathan Lemon }
2295