xref: /freebsd/sys/kern/kern_sig.c (revision e6feeae2f915c76275b83b7365ae966a8f8dd016)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
7df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
8df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
9df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2069a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39677b542eSDavid E. O'Brien #include <sys/cdefs.h>
40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
41677b542eSDavid E. O'Brien 
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45eb6368d4SRui Paulo #include <sys/ctype.h>
4636240ea5SDoug Rabson #include <sys/systm.h>
47df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
49df8bae1dSRodney W. Grimes #include <sys/acct.h>
504a144410SRobert Watson #include <sys/capsicum.h>
5178f57a9cSMark Johnston #include <sys/compressor.h>
52238510fcSJason Evans #include <sys/condvar.h>
53773e541eSWarner Losh #include <sys/devctl.h>
54854dc8c2SJohn Baldwin #include <sys/event.h>
55854dc8c2SJohn Baldwin #include <sys/fcntl.h>
56e7228204SAlfred Perlstein #include <sys/imgact.h>
57854dc8c2SJohn Baldwin #include <sys/kernel.h>
580384fff8SJason Evans #include <sys/ktr.h>
59df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
6091898857SMark Johnston #include <sys/limits.h>
61854dc8c2SJohn Baldwin #include <sys/lock.h>
62854dc8c2SJohn Baldwin #include <sys/malloc.h>
63854dc8c2SJohn Baldwin #include <sys/mutex.h>
64c959c237SMateusz Guzik #include <sys/refcount.h>
65854dc8c2SJohn Baldwin #include <sys/namei.h>
66854dc8c2SJohn Baldwin #include <sys/proc.h>
67cfb5f768SJonathan Anderson #include <sys/procdesc.h>
68ef401a85SKonstantin Belousov #include <sys/ptrace.h>
696aeb05d7STom Rhodes #include <sys/posix4.h>
70b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h>
71c31146a1SJohn Baldwin #include <sys/resourcevar.h>
725d217f17SJohn Birrell #include <sys/sdt.h>
7336b208e0SRobert Watson #include <sys/sbuf.h>
7444f3b092SJohn Baldwin #include <sys/sleepqueue.h>
756caa8a15SJohn Baldwin #include <sys/smp.h>
76df8bae1dSRodney W. Grimes #include <sys/stat.h>
771005a129SJohn Baldwin #include <sys/sx.h>
788f19eb88SIan Dowse #include <sys/syscallsubr.h>
79c87e2930SDavid Greenman #include <sys/sysctl.h>
80854dc8c2SJohn Baldwin #include <sys/sysent.h>
81854dc8c2SJohn Baldwin #include <sys/syslog.h>
82854dc8c2SJohn Baldwin #include <sys/sysproto.h>
8356c06c4bSDavid Xu #include <sys/timers.h>
8406ae1e91SMatthew Dillon #include <sys/unistd.h>
85854dc8c2SJohn Baldwin #include <sys/wait.h>
869104847fSDavid Xu #include <vm/vm.h>
879104847fSDavid Xu #include <vm/vm_extern.h>
889104847fSDavid Xu #include <vm/uma.h>
89df8bae1dSRodney W. Grimes 
90e7228204SAlfred Perlstein #include <sys/jail.h>
91e7228204SAlfred Perlstein 
92df8bae1dSRodney W. Grimes #include <machine/cpu.h>
93df8bae1dSRodney W. Grimes 
94bfd7575aSWayne Salamon #include <security/audit/audit.h>
95bfd7575aSWayne Salamon 
966f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
976f841fb7SMarcel Moolenaar 
985d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
9936160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__send,
10036160958SMark Johnston     "struct thread *", "struct proc *", "int");
10136160958SMark Johnston SDT_PROBE_DEFINE2(proc, , , signal__clear,
10236160958SMark Johnston     "int", "ksiginfo_t *");
10336160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__discard,
1047b77e1feSMark Johnston     "struct thread *", "struct proc *", "int");
1055d217f17SJohn Birrell 
1064d77a549SAlfred Perlstein static int	coredump(struct thread *);
107a3de221dSKonstantin Belousov static int	killpg1(struct thread *td, int sig, int pgid, int all,
108a3de221dSKonstantin Belousov 		    ksiginfo_t *ksi);
1093cf3b9f0SJohn Baldwin static int	issignal(struct thread *td);
1100bc52b0bSKonstantin Belousov static void	reschedule_signals(struct proc *p, sigset_t block, int flags);
1114d77a549SAlfred Perlstein static int	sigprop(int sig);
11294f0972bSDavid Xu static void	tdsigwakeup(struct thread *, int, sig_t, int);
113c53fec76SKonstantin Belousov static int	sig_suspend_threads(struct thread *, struct proc *);
114cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
115cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
116cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
117146fc63fSKonstantin Belousov static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
1189104847fSDavid Xu static void	sigqueue_start(void);
1194fced864SKonstantin Belousov static void	sigfastblock_setpend(struct thread *td, bool resched);
120cb679c38SJonathan Lemon 
1219104847fSDavid Xu static uma_zone_t	ksiginfo_zone = NULL;
122e76d823bSRobert Watson struct filterops sig_filtops = {
123e76d823bSRobert Watson 	.f_isfd = 0,
124e76d823bSRobert Watson 	.f_attach = filt_sigattach,
125e76d823bSRobert Watson 	.f_detach = filt_sigdetach,
126e76d823bSRobert Watson 	.f_event = filt_signal,
127e76d823bSRobert Watson };
128cb679c38SJonathan Lemon 
129fc8cca02SJohn Baldwin static int	kern_logsigexit = 1;
1303d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1313d177f46SBill Fumerola     &kern_logsigexit, 0,
1323d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
13357308494SJoerg Wunsch 
134f71a882fSDavid Xu static int	kern_forcesigexit = 1;
135f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
136f71a882fSDavid Xu     &kern_forcesigexit, 0, "Force trap signal to be handled");
137f71a882fSDavid Xu 
1387029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1396472ac3dSEd Schouten     "POSIX real time signal");
1409104847fSDavid Xu 
1419104847fSDavid Xu static int	max_pending_per_proc = 128;
1429104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
1439104847fSDavid Xu     &max_pending_per_proc, 0, "Max pending signals per proc");
1449104847fSDavid Xu 
1459104847fSDavid Xu static int	preallocate_siginfo = 1024;
146af3b2549SHans Petter Selasky SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
1479104847fSDavid Xu     &preallocate_siginfo, 0, "Preallocated signal memory size");
1489104847fSDavid Xu 
1499104847fSDavid Xu static int	signal_overflow = 0;
150761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
1519104847fSDavid Xu     &signal_overflow, 0, "Number of signals overflew");
1529104847fSDavid Xu 
1539104847fSDavid Xu static int	signal_alloc_fail = 0;
154761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
1559104847fSDavid Xu     &signal_alloc_fail, 0, "signals failed to be allocated");
1569104847fSDavid Xu 
157f5a077c3SKonstantin Belousov static int	kern_lognosys = 0;
158f5a077c3SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
159f5a077c3SKonstantin Belousov     "Log invalid syscalls");
160f5a077c3SKonstantin Belousov 
161a113b17fSKonstantin Belousov __read_frequently bool sigfastblock_fetch_always = false;
162a113b17fSKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
163a113b17fSKonstantin Belousov     &sigfastblock_fetch_always, 0,
164a113b17fSKonstantin Belousov     "Fetch sigfastblock word on each syscall entry for proper "
165a113b17fSKonstantin Belousov     "blocking semantic");
166a113b17fSKonstantin Belousov 
167bc387624SKonstantin Belousov static bool	kern_sig_discard_ign = true;
168bc387624SKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
169bc387624SKonstantin Belousov     &kern_sig_discard_ign, 0,
170bc387624SKonstantin Belousov     "Discard ignored signals on delivery, otherwise queue them to "
171bc387624SKonstantin Belousov     "the target queue");
172bc387624SKonstantin Belousov 
1739104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
1749104847fSDavid Xu 
1752b87b6d4SRobert Watson /*
1762b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1772b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1782b87b6d4SRobert Watson  * in the right situations.
1792b87b6d4SRobert Watson  */
1802b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1812b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1822b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1832b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1842b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1852b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1862b87b6d4SRobert Watson 
187fc8cca02SJohn Baldwin static int	sugid_coredump;
188af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
1898b5adf9dSJoseph Koshy     &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
190c87e2930SDavid Greenman 
191b0c9d4d7SPawel Jakub Dawidek static int	capmode_coredump;
192af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
193b0c9d4d7SPawel Jakub Dawidek     &capmode_coredump, 0, "Allow processes in capability mode to dump core");
194b0c9d4d7SPawel Jakub Dawidek 
195e5a28db9SPaul Saab static int	do_coredump = 1;
196e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
197e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
198e5a28db9SPaul Saab 
1996141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
2006141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
2016141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
2026141e04aSJohn-Mark Gurney 
2030da9e11bSRui Paulo static int	coredump_devctl = 0;
204eb6368d4SRui Paulo SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
205eb6368d4SRui Paulo 	0, "Generate a devctl notification when processes coredump");
206eb6368d4SRui Paulo 
2072c42a146SMarcel Moolenaar /*
2082c42a146SMarcel Moolenaar  * Signal properties and actions.
2092c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
2102c42a146SMarcel Moolenaar  * according to the following properties:
2112c42a146SMarcel Moolenaar  */
212fd50a707SBrooks Davis #define	SIGPROP_KILL		0x01	/* terminates process by default */
213fd50a707SBrooks Davis #define	SIGPROP_CORE		0x02	/* ditto and coredumps */
214fd50a707SBrooks Davis #define	SIGPROP_STOP		0x04	/* suspend process */
215fd50a707SBrooks Davis #define	SIGPROP_TTYSTOP		0x08	/* ditto, from tty */
216fd50a707SBrooks Davis #define	SIGPROP_IGNORE		0x10	/* ignore by default */
217fd50a707SBrooks Davis #define	SIGPROP_CONT		0x20	/* continue if suspended */
218df8bae1dSRodney W. Grimes 
2192c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
220ed6d876bSBrooks Davis 	[SIGHUP] =	SIGPROP_KILL,
221ed6d876bSBrooks Davis 	[SIGINT] =	SIGPROP_KILL,
222ed6d876bSBrooks Davis 	[SIGQUIT] =	SIGPROP_KILL | SIGPROP_CORE,
223ed6d876bSBrooks Davis 	[SIGILL] =	SIGPROP_KILL | SIGPROP_CORE,
224ed6d876bSBrooks Davis 	[SIGTRAP] =	SIGPROP_KILL | SIGPROP_CORE,
225ed6d876bSBrooks Davis 	[SIGABRT] =	SIGPROP_KILL | SIGPROP_CORE,
226ed6d876bSBrooks Davis 	[SIGEMT] =	SIGPROP_KILL | SIGPROP_CORE,
227ed6d876bSBrooks Davis 	[SIGFPE] =	SIGPROP_KILL | SIGPROP_CORE,
228ed6d876bSBrooks Davis 	[SIGKILL] =	SIGPROP_KILL,
229ed6d876bSBrooks Davis 	[SIGBUS] =	SIGPROP_KILL | SIGPROP_CORE,
230ed6d876bSBrooks Davis 	[SIGSEGV] =	SIGPROP_KILL | SIGPROP_CORE,
231ed6d876bSBrooks Davis 	[SIGSYS] =	SIGPROP_KILL | SIGPROP_CORE,
232ed6d876bSBrooks Davis 	[SIGPIPE] =	SIGPROP_KILL,
233ed6d876bSBrooks Davis 	[SIGALRM] =	SIGPROP_KILL,
234ed6d876bSBrooks Davis 	[SIGTERM] =	SIGPROP_KILL,
235ed6d876bSBrooks Davis 	[SIGURG] =	SIGPROP_IGNORE,
236ed6d876bSBrooks Davis 	[SIGSTOP] =	SIGPROP_STOP,
237ed6d876bSBrooks Davis 	[SIGTSTP] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
238ed6d876bSBrooks Davis 	[SIGCONT] =	SIGPROP_IGNORE | SIGPROP_CONT,
239ed6d876bSBrooks Davis 	[SIGCHLD] =	SIGPROP_IGNORE,
240ed6d876bSBrooks Davis 	[SIGTTIN] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
241ed6d876bSBrooks Davis 	[SIGTTOU] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
242ed6d876bSBrooks Davis 	[SIGIO] =	SIGPROP_IGNORE,
243ed6d876bSBrooks Davis 	[SIGXCPU] =	SIGPROP_KILL,
244ed6d876bSBrooks Davis 	[SIGXFSZ] =	SIGPROP_KILL,
245ed6d876bSBrooks Davis 	[SIGVTALRM] =	SIGPROP_KILL,
246ed6d876bSBrooks Davis 	[SIGPROF] =	SIGPROP_KILL,
247ed6d876bSBrooks Davis 	[SIGWINCH] =	SIGPROP_IGNORE,
248ed6d876bSBrooks Davis 	[SIGINFO] =	SIGPROP_IGNORE,
249ed6d876bSBrooks Davis 	[SIGUSR1] =	SIGPROP_KILL,
250ed6d876bSBrooks Davis 	[SIGUSR2] =	SIGPROP_KILL,
2512c42a146SMarcel Moolenaar };
2522c42a146SMarcel Moolenaar 
25381f2e906SMark Johnston #define	_SIG_FOREACH_ADVANCE(i, set) ({					\
25481f2e906SMark Johnston 	int __found;							\
25581f2e906SMark Johnston 	for (;;) {							\
25681f2e906SMark Johnston 		if (__bits != 0) {					\
25781f2e906SMark Johnston 			int __sig = ffs(__bits);			\
25881f2e906SMark Johnston 			__bits &= ~(1u << (__sig - 1));			\
25981f2e906SMark Johnston 			sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
26081f2e906SMark Johnston 			__found = 1;					\
26181f2e906SMark Johnston 			break;						\
26281f2e906SMark Johnston 		}							\
26381f2e906SMark Johnston 		if (++__i == _SIG_WORDS) {				\
26481f2e906SMark Johnston 			__found = 0;					\
26581f2e906SMark Johnston 			break;						\
26681f2e906SMark Johnston 		}							\
26781f2e906SMark Johnston 		__bits = (set)->__bits[__i];				\
26881f2e906SMark Johnston 	}								\
26981f2e906SMark Johnston 	__found != 0;							\
27081f2e906SMark Johnston })
27181f2e906SMark Johnston 
27281f2e906SMark Johnston #define	SIG_FOREACH(i, set)						\
27381f2e906SMark Johnston 	for (int32_t __i = -1, __bits = 0;				\
27481f2e906SMark Johnston 	    _SIG_FOREACH_ADVANCE(i, set); )				\
27581f2e906SMark Johnston 
2764fced864SKonstantin Belousov static sigset_t fastblock_mask;
2776b286ee8SKonstantin Belousov 
2789104847fSDavid Xu static void
279c6d31b83SKonstantin Belousov ast_sig(struct thread *td, int tda)
280c6d31b83SKonstantin Belousov {
281c6d31b83SKonstantin Belousov 	struct proc *p;
282c6d31b83SKonstantin Belousov 	int sig;
283c6d31b83SKonstantin Belousov 	bool resched_sigs;
284c6d31b83SKonstantin Belousov 
285c6d31b83SKonstantin Belousov 	p = td->td_proc;
286c6d31b83SKonstantin Belousov 
287c6d31b83SKonstantin Belousov #ifdef DIAGNOSTIC
288c6d31b83SKonstantin Belousov 	if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) |
289c6d31b83SKonstantin Belousov 	    TDAI(TDA_AST))) == 0) {
290c6d31b83SKonstantin Belousov 		PROC_LOCK(p);
291c6d31b83SKonstantin Belousov 		thread_lock(td);
292c6d31b83SKonstantin Belousov 		/*
293c6d31b83SKonstantin Belousov 		 * Note that TDA_SIG should be re-read from
294c6d31b83SKonstantin Belousov 		 * td_ast, since signal might have been delivered
295c6d31b83SKonstantin Belousov 		 * after we cleared td_flags above.  This is one of
296c6d31b83SKonstantin Belousov 		 * the reason for looping check for AST condition.
297c6d31b83SKonstantin Belousov 		 * See comment in userret() about P_PPWAIT.
298c6d31b83SKonstantin Belousov 		 */
299c6d31b83SKonstantin Belousov 		if ((p->p_flag & P_PPWAIT) == 0 &&
300c6d31b83SKonstantin Belousov 		    (td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
301c6d31b83SKonstantin Belousov 			if (SIGPENDING(td) && ((tda | td->td_ast) &
302c6d31b83SKonstantin Belousov 			    (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) {
303c6d31b83SKonstantin Belousov 				thread_unlock(td); /* fix dumps */
304c6d31b83SKonstantin Belousov 				panic(
305c6d31b83SKonstantin Belousov 				    "failed2 to set signal flags for ast p %p "
306c6d31b83SKonstantin Belousov 				    "td %p tda %#x td_ast %#x fl %#x",
307c6d31b83SKonstantin Belousov 				    p, td, tda, td->td_ast, td->td_flags);
308c6d31b83SKonstantin Belousov 			}
309c6d31b83SKonstantin Belousov 		}
310c6d31b83SKonstantin Belousov 		thread_unlock(td);
311c6d31b83SKonstantin Belousov 		PROC_UNLOCK(p);
312c6d31b83SKonstantin Belousov 	}
313c6d31b83SKonstantin Belousov #endif
314c6d31b83SKonstantin Belousov 
315c6d31b83SKonstantin Belousov 	/*
316c6d31b83SKonstantin Belousov 	 * Check for signals. Unlocked reads of p_pendingcnt or
317c6d31b83SKonstantin Belousov 	 * p_siglist might cause process-directed signal to be handled
318c6d31b83SKonstantin Belousov 	 * later.
319c6d31b83SKonstantin Belousov 	 */
320f2fd7d8bSKonstantin Belousov 	if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 ||
321c6d31b83SKonstantin Belousov 	    !SIGISEMPTY(p->p_siglist)) {
322c6d31b83SKonstantin Belousov 		sigfastblock_fetch(td);
323c6d31b83SKonstantin Belousov 		PROC_LOCK(p);
324c6d31b83SKonstantin Belousov 		mtx_lock(&p->p_sigacts->ps_mtx);
325c6d31b83SKonstantin Belousov 		while ((sig = cursig(td)) != 0) {
326c6d31b83SKonstantin Belousov 			KASSERT(sig >= 0, ("sig %d", sig));
327c6d31b83SKonstantin Belousov 			postsig(sig);
328c6d31b83SKonstantin Belousov 		}
329c6d31b83SKonstantin Belousov 		mtx_unlock(&p->p_sigacts->ps_mtx);
330c6d31b83SKonstantin Belousov 		PROC_UNLOCK(p);
331c6d31b83SKonstantin Belousov 		resched_sigs = true;
332c6d31b83SKonstantin Belousov 	} else {
333c6d31b83SKonstantin Belousov 		resched_sigs = false;
334c6d31b83SKonstantin Belousov 	}
335c6d31b83SKonstantin Belousov 
336c6d31b83SKonstantin Belousov 	/*
337c6d31b83SKonstantin Belousov 	 * Handle deferred update of the fast sigblock value, after
338c6d31b83SKonstantin Belousov 	 * the postsig() loop was performed.
339c6d31b83SKonstantin Belousov 	 */
340c6d31b83SKonstantin Belousov 	sigfastblock_setpend(td, resched_sigs);
341c6d31b83SKonstantin Belousov }
342c6d31b83SKonstantin Belousov 
343c6d31b83SKonstantin Belousov static void
344c6d31b83SKonstantin Belousov ast_sigsuspend(struct thread *td, int tda __unused)
345c6d31b83SKonstantin Belousov {
346c6d31b83SKonstantin Belousov 	MPASS((td->td_pflags & TDP_OLDMASK) != 0);
347c6d31b83SKonstantin Belousov 	td->td_pflags &= ~TDP_OLDMASK;
348c6d31b83SKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
349c6d31b83SKonstantin Belousov }
350c6d31b83SKonstantin Belousov 
351c6d31b83SKonstantin Belousov static void
3529104847fSDavid Xu sigqueue_start(void)
3539104847fSDavid Xu {
3549104847fSDavid Xu 	ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
3559104847fSDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3569104847fSDavid Xu 	uma_prealloc(ksiginfo_zone, preallocate_siginfo);
357b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
358b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
359b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
360146fc63fSKonstantin Belousov 	SIGFILLSET(fastblock_mask);
361146fc63fSKonstantin Belousov 	SIG_CANTMASK(fastblock_mask);
362c6d31b83SKonstantin Belousov 	ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig);
363c6d31b83SKonstantin Belousov 	ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP,
364c6d31b83SKonstantin Belousov 	    TDP_OLDMASK, ast_sigsuspend);
3659104847fSDavid Xu }
3669104847fSDavid Xu 
3675da49fcbSDavid Xu ksiginfo_t *
368cc29f221SKonstantin Belousov ksiginfo_alloc(int mwait)
3699104847fSDavid Xu {
370cc29f221SKonstantin Belousov 	MPASS(mwait == M_WAITOK || mwait == M_NOWAIT);
371ebceaf6dSDavid Xu 
372cc29f221SKonstantin Belousov 	if (ksiginfo_zone == NULL)
3739104847fSDavid Xu 		return (NULL);
374cc29f221SKonstantin Belousov 	return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO));
3759104847fSDavid Xu }
3769104847fSDavid Xu 
3775da49fcbSDavid Xu void
3789104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi)
3799104847fSDavid Xu {
3809104847fSDavid Xu 	uma_zfree(ksiginfo_zone, ksi);
3819104847fSDavid Xu }
3829104847fSDavid Xu 
383cdb58f9dSKonstantin Belousov static __inline bool
3845da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi)
3855da49fcbSDavid Xu {
386cdb58f9dSKonstantin Belousov 	if ((ksi->ksi_flags & KSI_EXT) == 0) {
3875da49fcbSDavid Xu 		uma_zfree(ksiginfo_zone, ksi);
388cdb58f9dSKonstantin Belousov 		return (true);
3895da49fcbSDavid Xu 	}
390cdb58f9dSKonstantin Belousov 	return (false);
3915da49fcbSDavid Xu }
3925da49fcbSDavid Xu 
3939104847fSDavid Xu void
3949104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p)
3959104847fSDavid Xu {
3969104847fSDavid Xu 	SIGEMPTYSET(list->sq_signals);
3973dfcaad6SDavid Xu 	SIGEMPTYSET(list->sq_kill);
39882a4538fSEric Badger 	SIGEMPTYSET(list->sq_ptrace);
3999104847fSDavid Xu 	TAILQ_INIT(&list->sq_list);
4009104847fSDavid Xu 	list->sq_proc = p;
4019104847fSDavid Xu 	list->sq_flags = SQ_INIT;
4029104847fSDavid Xu }
4039104847fSDavid Xu 
4049104847fSDavid Xu /*
4059104847fSDavid Xu  * Get a signal's ksiginfo.
4069104847fSDavid Xu  * Return:
4079104847fSDavid Xu  *	0	-	signal not found
4089104847fSDavid Xu  *	others	-	signal number
4099104847fSDavid Xu  */
410a5799a4fSKonstantin Belousov static int
4119104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
4129104847fSDavid Xu {
4139104847fSDavid Xu 	struct proc *p = sq->sq_proc;
4149104847fSDavid Xu 	struct ksiginfo *ksi, *next;
4159104847fSDavid Xu 	int count = 0;
4169104847fSDavid Xu 
4179104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
4189104847fSDavid Xu 
4199104847fSDavid Xu 	if (!SIGISMEMBER(sq->sq_signals, signo))
4209104847fSDavid Xu 		return (0);
4219104847fSDavid Xu 
42282a4538fSEric Badger 	if (SIGISMEMBER(sq->sq_ptrace, signo)) {
42382a4538fSEric Badger 		count++;
42482a4538fSEric Badger 		SIGDELSET(sq->sq_ptrace, signo);
42582a4538fSEric Badger 		si->ksi_flags |= KSI_PTRACE;
42682a4538fSEric Badger 	}
4273dfcaad6SDavid Xu 	if (SIGISMEMBER(sq->sq_kill, signo)) {
4283dfcaad6SDavid Xu 		count++;
42982a4538fSEric Badger 		if (count == 1)
4303dfcaad6SDavid Xu 			SIGDELSET(sq->sq_kill, signo);
4313dfcaad6SDavid Xu 	}
4323dfcaad6SDavid Xu 
4335c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
4349104847fSDavid Xu 		if (ksi->ksi_signo == signo) {
4359104847fSDavid Xu 			if (count == 0) {
4369104847fSDavid Xu 				TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4375da49fcbSDavid Xu 				ksi->ksi_sigq = NULL;
4389104847fSDavid Xu 				ksiginfo_copy(ksi, si);
4395da49fcbSDavid Xu 				if (ksiginfo_tryfree(ksi) && p != NULL)
4409104847fSDavid Xu 					p->p_pendingcnt--;
4419104847fSDavid Xu 			}
442016fa302SDavid Xu 			if (++count > 1)
443016fa302SDavid Xu 				break;
4449104847fSDavid Xu 		}
4459104847fSDavid Xu 	}
4469104847fSDavid Xu 
4479104847fSDavid Xu 	if (count <= 1)
4489104847fSDavid Xu 		SIGDELSET(sq->sq_signals, signo);
4499104847fSDavid Xu 	si->ksi_signo = signo;
4509104847fSDavid Xu 	return (signo);
4519104847fSDavid Xu }
4529104847fSDavid Xu 
4535da49fcbSDavid Xu void
4545da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi)
4555da49fcbSDavid Xu {
4565da49fcbSDavid Xu 	struct ksiginfo *kp;
4575da49fcbSDavid Xu 	struct proc	*p;
4585da49fcbSDavid Xu 	sigqueue_t	*sq;
4595da49fcbSDavid Xu 
460ebceaf6dSDavid Xu 	if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
4615da49fcbSDavid Xu 		return;
4625da49fcbSDavid Xu 
4635da49fcbSDavid Xu 	p = sq->sq_proc;
4645da49fcbSDavid Xu 	TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4655da49fcbSDavid Xu 	ksi->ksi_sigq = NULL;
4665da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
4675da49fcbSDavid Xu 		p->p_pendingcnt--;
4685da49fcbSDavid Xu 
4695da49fcbSDavid Xu 	for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
4705da49fcbSDavid Xu 	     kp = TAILQ_NEXT(kp, ksi_link)) {
4715da49fcbSDavid Xu 		if (kp->ksi_signo == ksi->ksi_signo)
4725da49fcbSDavid Xu 			break;
4735da49fcbSDavid Xu 	}
47482a4538fSEric Badger 	if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
47582a4538fSEric Badger 	    !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
4765da49fcbSDavid Xu 		SIGDELSET(sq->sq_signals, ksi->ksi_signo);
4775da49fcbSDavid Xu }
4785da49fcbSDavid Xu 
479a5799a4fSKonstantin Belousov static int
4809104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
4819104847fSDavid Xu {
4829104847fSDavid Xu 	struct proc *p = sq->sq_proc;
4839104847fSDavid Xu 	struct ksiginfo *ksi;
4849104847fSDavid Xu 	int ret = 0;
4859104847fSDavid Xu 
4869104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
4879104847fSDavid Xu 
48882a4538fSEric Badger 	/*
48982a4538fSEric Badger 	 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
49082a4538fSEric Badger 	 * for these signals.
49182a4538fSEric Badger 	 */
4923dfcaad6SDavid Xu 	if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
4933dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
4949104847fSDavid Xu 		goto out_set_bit;
4953dfcaad6SDavid Xu 	}
4969104847fSDavid Xu 
4975da49fcbSDavid Xu 	/* directly insert the ksi, don't copy it */
4985da49fcbSDavid Xu 	if (si->ksi_flags & KSI_INS) {
4996a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
5006a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
5016a671a6bSKonstantin Belousov 		else
5025da49fcbSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
5035da49fcbSDavid Xu 		si->ksi_sigq = sq;
5045da49fcbSDavid Xu 		goto out_set_bit;
5055da49fcbSDavid Xu 	}
5065da49fcbSDavid Xu 
5073dfcaad6SDavid Xu 	if (__predict_false(ksiginfo_zone == NULL)) {
5083dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
5099104847fSDavid Xu 		goto out_set_bit;
5103dfcaad6SDavid Xu 	}
5119104847fSDavid Xu 
512ebceaf6dSDavid Xu 	if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
5139104847fSDavid Xu 		signal_overflow++;
5149104847fSDavid Xu 		ret = EAGAIN;
515cc29f221SKonstantin Belousov 	} else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) {
5169104847fSDavid Xu 		signal_alloc_fail++;
5179104847fSDavid Xu 		ret = EAGAIN;
5189104847fSDavid Xu 	} else {
5199104847fSDavid Xu 		if (p != NULL)
5209104847fSDavid Xu 			p->p_pendingcnt++;
5219104847fSDavid Xu 		ksiginfo_copy(si, ksi);
5229104847fSDavid Xu 		ksi->ksi_signo = signo;
5236a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
5246a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
5256a671a6bSKonstantin Belousov 		else
5269104847fSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
5275da49fcbSDavid Xu 		ksi->ksi_sigq = sq;
5289104847fSDavid Xu 	}
5299104847fSDavid Xu 
53082a4538fSEric Badger 	if (ret != 0) {
53182a4538fSEric Badger 		if ((si->ksi_flags & KSI_PTRACE) != 0) {
53282a4538fSEric Badger 			SIGADDSET(sq->sq_ptrace, signo);
53382a4538fSEric Badger 			ret = 0;
53482a4538fSEric Badger 			goto out_set_bit;
53582a4538fSEric Badger 		} else if ((si->ksi_flags & KSI_TRAP) != 0 ||
536a3de221dSKonstantin Belousov 		    (si->ksi_flags & KSI_SIGQ) == 0) {
5373dfcaad6SDavid Xu 			SIGADDSET(sq->sq_kill, signo);
5389104847fSDavid Xu 			ret = 0;
5399104847fSDavid Xu 			goto out_set_bit;
5409104847fSDavid Xu 		}
5419104847fSDavid Xu 		return (ret);
54282a4538fSEric Badger 	}
5439104847fSDavid Xu 
5449104847fSDavid Xu out_set_bit:
5459104847fSDavid Xu 	SIGADDSET(sq->sq_signals, signo);
5469104847fSDavid Xu 	return (ret);
5479104847fSDavid Xu }
5489104847fSDavid Xu 
5499104847fSDavid Xu void
5509104847fSDavid Xu sigqueue_flush(sigqueue_t *sq)
5519104847fSDavid Xu {
5529104847fSDavid Xu 	struct proc *p = sq->sq_proc;
5539104847fSDavid Xu 	ksiginfo_t *ksi;
5549104847fSDavid Xu 
5559104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
5569104847fSDavid Xu 
5575da49fcbSDavid Xu 	if (p != NULL)
5585da49fcbSDavid Xu 		PROC_LOCK_ASSERT(p, MA_OWNED);
5595da49fcbSDavid Xu 
5609104847fSDavid Xu 	while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
5619104847fSDavid Xu 		TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
5625da49fcbSDavid Xu 		ksi->ksi_sigq = NULL;
5635da49fcbSDavid Xu 		if (ksiginfo_tryfree(ksi) && p != NULL)
5649104847fSDavid Xu 			p->p_pendingcnt--;
5659104847fSDavid Xu 	}
5669104847fSDavid Xu 
5679104847fSDavid Xu 	SIGEMPTYSET(sq->sq_signals);
5683dfcaad6SDavid Xu 	SIGEMPTYSET(sq->sq_kill);
56982a4538fSEric Badger 	SIGEMPTYSET(sq->sq_ptrace);
5709104847fSDavid Xu }
5719104847fSDavid Xu 
572a5799a4fSKonstantin Belousov static void
573fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
5749104847fSDavid Xu {
575fc4ecc1dSDavid Xu 	sigset_t tmp;
5769104847fSDavid Xu 	struct proc *p1, *p2;
5779104847fSDavid Xu 	ksiginfo_t *ksi, *next;
5789104847fSDavid Xu 
5799104847fSDavid Xu 	KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
5809104847fSDavid Xu 	KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
5819104847fSDavid Xu 	p1 = src->sq_proc;
5829104847fSDavid Xu 	p2 = dst->sq_proc;
5839104847fSDavid Xu 	/* Move siginfo to target list */
5845c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
585fc4ecc1dSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
5869104847fSDavid Xu 			TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
5879104847fSDavid Xu 			if (p1 != NULL)
5889104847fSDavid Xu 				p1->p_pendingcnt--;
5899104847fSDavid Xu 			TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
5905da49fcbSDavid Xu 			ksi->ksi_sigq = dst;
5919104847fSDavid Xu 			if (p2 != NULL)
5929104847fSDavid Xu 				p2->p_pendingcnt++;
5939104847fSDavid Xu 		}
5949104847fSDavid Xu 	}
5959104847fSDavid Xu 
5969104847fSDavid Xu 	/* Move pending bits to target list */
5973dfcaad6SDavid Xu 	tmp = src->sq_kill;
598fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
5993dfcaad6SDavid Xu 	SIGSETOR(dst->sq_kill, tmp);
6003dfcaad6SDavid Xu 	SIGSETNAND(src->sq_kill, tmp);
6013dfcaad6SDavid Xu 
60282a4538fSEric Badger 	tmp = src->sq_ptrace;
60382a4538fSEric Badger 	SIGSETAND(tmp, *set);
60482a4538fSEric Badger 	SIGSETOR(dst->sq_ptrace, tmp);
60582a4538fSEric Badger 	SIGSETNAND(src->sq_ptrace, tmp);
60682a4538fSEric Badger 
6079104847fSDavid Xu 	tmp = src->sq_signals;
608fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
6099104847fSDavid Xu 	SIGSETOR(dst->sq_signals, tmp);
6109104847fSDavid Xu 	SIGSETNAND(src->sq_signals, tmp);
6119104847fSDavid Xu }
6129104847fSDavid Xu 
613407af02bSDavid Xu #if 0
614a5799a4fSKonstantin Belousov static void
6159104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
6169104847fSDavid Xu {
6179104847fSDavid Xu 	sigset_t set;
6189104847fSDavid Xu 
6199104847fSDavid Xu 	SIGEMPTYSET(set);
6209104847fSDavid Xu 	SIGADDSET(set, signo);
6219104847fSDavid Xu 	sigqueue_move_set(src, dst, &set);
6229104847fSDavid Xu }
623407af02bSDavid Xu #endif
6249104847fSDavid Xu 
625a5799a4fSKonstantin Belousov static void
626fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
6279104847fSDavid Xu {
6289104847fSDavid Xu 	struct proc *p = sq->sq_proc;
6299104847fSDavid Xu 	ksiginfo_t *ksi, *next;
6309104847fSDavid Xu 
6319104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
6329104847fSDavid Xu 
6339104847fSDavid Xu 	/* Remove siginfo queue */
6345c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
6359104847fSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
6369104847fSDavid Xu 			TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
6375da49fcbSDavid Xu 			ksi->ksi_sigq = NULL;
6385da49fcbSDavid Xu 			if (ksiginfo_tryfree(ksi) && p != NULL)
6399104847fSDavid Xu 				p->p_pendingcnt--;
6409104847fSDavid Xu 		}
6419104847fSDavid Xu 	}
6423dfcaad6SDavid Xu 	SIGSETNAND(sq->sq_kill, *set);
64382a4538fSEric Badger 	SIGSETNAND(sq->sq_ptrace, *set);
6449104847fSDavid Xu 	SIGSETNAND(sq->sq_signals, *set);
6459104847fSDavid Xu }
6469104847fSDavid Xu 
6479104847fSDavid Xu void
6489104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo)
6499104847fSDavid Xu {
6509104847fSDavid Xu 	sigset_t set;
6519104847fSDavid Xu 
6529104847fSDavid Xu 	SIGEMPTYSET(set);
6539104847fSDavid Xu 	SIGADDSET(set, signo);
6549104847fSDavid Xu 	sigqueue_delete_set(sq, &set);
6559104847fSDavid Xu }
6569104847fSDavid Xu 
6579104847fSDavid Xu /* Remove a set of signals for a process */
658a5799a4fSKonstantin Belousov static void
659fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
6609104847fSDavid Xu {
6619104847fSDavid Xu 	sigqueue_t worklist;
6629104847fSDavid Xu 	struct thread *td0;
6639104847fSDavid Xu 
6649104847fSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
6659104847fSDavid Xu 
6669104847fSDavid Xu 	sigqueue_init(&worklist, NULL);
6679104847fSDavid Xu 	sigqueue_move_set(&p->p_sigqueue, &worklist, set);
6689104847fSDavid Xu 
6699104847fSDavid Xu 	FOREACH_THREAD_IN_PROC(p, td0)
6709104847fSDavid Xu 		sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
6719104847fSDavid Xu 
6729104847fSDavid Xu 	sigqueue_flush(&worklist);
6739104847fSDavid Xu }
6749104847fSDavid Xu 
6759104847fSDavid Xu void
6769104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo)
6779104847fSDavid Xu {
6789104847fSDavid Xu 	sigset_t set;
6799104847fSDavid Xu 
6809104847fSDavid Xu 	SIGEMPTYSET(set);
6819104847fSDavid Xu 	SIGADDSET(set, signo);
6829104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
6839104847fSDavid Xu }
6849104847fSDavid Xu 
685a5799a4fSKonstantin Belousov static void
6869104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p)
6879104847fSDavid Xu {
6889104847fSDavid Xu 	sigset_t set;
6899104847fSDavid Xu 
6909104847fSDavid Xu 	SIGEMPTYSET(set);
6919104847fSDavid Xu 	SIGADDSET(set, SIGSTOP);
6929104847fSDavid Xu 	SIGADDSET(set, SIGTSTP);
6939104847fSDavid Xu 	SIGADDSET(set, SIGTTIN);
6949104847fSDavid Xu 	SIGADDSET(set, SIGTTOU);
6959104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
6969104847fSDavid Xu }
6979104847fSDavid Xu 
698fbbeeb6cSBruce Evans /*
6991968f37bSJohn Baldwin  * Determine signal that should be delivered to thread td, the current
7001968f37bSJohn Baldwin  * thread, 0 if none.  If there is a pending stop signal with default
701fbbeeb6cSBruce Evans  * action, the process stops in issignal().
702fbbeeb6cSBruce Evans  */
703fbbeeb6cSBruce Evans int
7043cf3b9f0SJohn Baldwin cursig(struct thread *td)
705fbbeeb6cSBruce Evans {
706c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
70790af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
708a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
7093cf3b9f0SJohn Baldwin 	return (SIGPENDING(td) ? issignal(td) : 0);
710fbbeeb6cSBruce Evans }
711fbbeeb6cSBruce Evans 
71279065dbaSBruce Evans /*
71379065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
7149104847fSDavid Xu  * mode.  This must be called whenever a signal is added to td_sigqueue or
7154093529dSJeff Roberson  * unmasked in td_sigmask.
71679065dbaSBruce Evans  */
71779065dbaSBruce Evans void
7184093529dSJeff Roberson signotify(struct thread *td)
71979065dbaSBruce Evans {
7204093529dSJeff Roberson 
721ddd4d15eSMatt Macy 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
7224093529dSJeff Roberson 
723c6d31b83SKonstantin Belousov 	if (SIGPENDING(td))
724c6d31b83SKonstantin Belousov 		ast_sched(td, TDA_SIG);
7258b94a061SJohn Baldwin }
7268b94a061SJohn Baldwin 
727affd9185SKonstantin Belousov /*
728affd9185SKonstantin Belousov  * Returns 1 (true) if altstack is configured for the thread, and the
729affd9185SKonstantin Belousov  * passed stack bottom address falls into the altstack range.  Handles
730affd9185SKonstantin Belousov  * the 43 compat special case where the alt stack size is zero.
731affd9185SKonstantin Belousov  */
7328b94a061SJohn Baldwin int
7338b94a061SJohn Baldwin sigonstack(size_t sp)
7348b94a061SJohn Baldwin {
735affd9185SKonstantin Belousov 	struct thread *td;
7368b94a061SJohn Baldwin 
737affd9185SKonstantin Belousov 	td = curthread;
738affd9185SKonstantin Belousov 	if ((td->td_pflags & TDP_ALTSTACK) == 0)
739affd9185SKonstantin Belousov 		return (0);
7401930e303SPoul-Henning Kamp #if defined(COMPAT_43)
7417e097daaSKonstantin Belousov 	if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
742affd9185SKonstantin Belousov 		return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
7438b94a061SJohn Baldwin #endif
744affd9185SKonstantin Belousov 	return (sp >= (size_t)td->td_sigstk.ss_sp &&
745affd9185SKonstantin Belousov 	    sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
7468b94a061SJohn Baldwin }
74779065dbaSBruce Evans 
7486f841fb7SMarcel Moolenaar static __inline int
7496f841fb7SMarcel Moolenaar sigprop(int sig)
7502c42a146SMarcel Moolenaar {
7516f841fb7SMarcel Moolenaar 
752ed6d876bSBrooks Davis 	if (sig > 0 && sig < nitems(sigproptbl))
753ed6d876bSBrooks Davis 		return (sigproptbl[sig]);
7542c42a146SMarcel Moolenaar 	return (0);
755df8bae1dSRodney W. Grimes }
7562c42a146SMarcel Moolenaar 
757350ae563SKonstantin Belousov static bool
758ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag)
759350ae563SKonstantin Belousov {
760350ae563SKonstantin Belousov 
761c83655f3SKonstantin Belousov 	/*
762c83655f3SKonstantin Belousov 	 * SA_SIGINFO is reset when signal disposition is set to
763c83655f3SKonstantin Belousov 	 * ignore or default.  Other flags are kept according to user
764c83655f3SKonstantin Belousov 	 * settings.
765c83655f3SKonstantin Belousov 	 */
766c83655f3SKonstantin Belousov 	return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
767c83655f3SKonstantin Belousov 	    ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
768c83655f3SKonstantin Belousov 	    (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
769350ae563SKonstantin Belousov }
770350ae563SKonstantin Belousov 
7712c42a146SMarcel Moolenaar /*
7728f19eb88SIan Dowse  * kern_sigaction
7732c42a146SMarcel Moolenaar  * sigaction
77423eeeff7SPeter Wemm  * freebsd4_sigaction
7752c42a146SMarcel Moolenaar  * osigaction
7762c42a146SMarcel Moolenaar  */
7778f19eb88SIan Dowse int
778ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
779ea566832SEd Schouten     struct sigaction *oact, int flags)
780df8bae1dSRodney W. Grimes {
78190af4afaSJohn Baldwin 	struct sigacts *ps;
7828f19eb88SIan Dowse 	struct proc *p = td->td_proc;
783df8bae1dSRodney W. Grimes 
7842899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
7852c42a146SMarcel Moolenaar 		return (EINVAL);
786271ab240SKonstantin Belousov 	if (act != NULL && act->sa_handler != SIG_DFL &&
787271ab240SKonstantin Belousov 	    act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
788271ab240SKonstantin Belousov 	    SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
789271ab240SKonstantin Belousov 	    SA_NOCLDWAIT | SA_SIGINFO)) != 0)
7902d864174SKonstantin Belousov 		return (EINVAL);
7912c42a146SMarcel Moolenaar 
792628d2653SJohn Baldwin 	PROC_LOCK(p);
793628d2653SJohn Baldwin 	ps = p->p_sigacts;
79490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
7952c42a146SMarcel Moolenaar 	if (oact) {
796fb441a88SKonstantin Belousov 		memset(oact, 0, sizeof(*oact));
7972c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
7982c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
7992c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
8002c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
8012c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
8022c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
8032c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
8042c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
8052c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
80610c2b8e1SDavid E. O'Brien 		if (SIGISMEMBER(ps->ps_siginfo, sig)) {
8072c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
80810c2b8e1SDavid E. O'Brien 			oact->sa_sigaction =
80910c2b8e1SDavid E. O'Brien 			    (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
81010c2b8e1SDavid E. O'Brien 		} else
81110c2b8e1SDavid E. O'Brien 			oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
81290af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
8132c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
81490af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
8152c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
8162c42a146SMarcel Moolenaar 	}
8172c42a146SMarcel Moolenaar 	if (act) {
8182c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
819628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
82090af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
821628d2653SJohn Baldwin 			PROC_UNLOCK(p);
8222c42a146SMarcel Moolenaar 			return (EINVAL);
823628d2653SJohn Baldwin 		}
8242c42a146SMarcel Moolenaar 
825df8bae1dSRodney W. Grimes 		/*
826df8bae1dSRodney W. Grimes 		 * Change setting atomically.
827df8bae1dSRodney W. Grimes 		 */
8282c42a146SMarcel Moolenaar 
8292c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
8302c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
831350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_SIGINFO)) {
832aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
833aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
83480f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
83580f42b55SIan Dowse 		} else {
83680f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
8372c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
8382c42a146SMarcel Moolenaar 		}
839350ae563SKonstantin Belousov 		if (!sigact_flag_test(act, SA_RESTART))
8402c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
841df8bae1dSRodney W. Grimes 		else
8422c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
843350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_ONSTACK))
8442c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
845df8bae1dSRodney W. Grimes 		else
8462c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
847350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_RESETHAND))
8482c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
8491e41c1b5SSteven Wallace 		else
8502c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
851350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_NODEFER))
8522c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
853289ccde0SPeter Wemm 		else
8542c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
8552c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
8562c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
85790af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
8586626c604SJulian Elischer 			else
85990af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
860ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
861245f17d4SJoerg Wunsch 				/*
8622c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
8632c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
8642c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
8652c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
866245f17d4SJoerg Wunsch 				 */
867245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
86890af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
8696626c604SJulian Elischer 				else
87090af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
871245f17d4SJoerg Wunsch 			} else
87290af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
873ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
87490af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
875ba1551caSIan Dowse 			else
87690af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
877df8bae1dSRodney W. Grimes 		}
878df8bae1dSRodney W. Grimes 		/*
87990af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
8802c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
88190af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
8822c42a146SMarcel Moolenaar 		 * have to restart the process.
883df8bae1dSRodney W. Grimes 		 */
8842c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
885fd50a707SBrooks Davis 		    (sigprop(sig) & SIGPROP_IGNORE &&
8862c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
8872c42a146SMarcel Moolenaar 			/* never to be seen again */
8889104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
8892c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
8902c42a146SMarcel Moolenaar 				/* easier in psignal */
89190af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
89290af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
893645682fdSLuoqi Chen 		} else {
89490af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
8952c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
89690af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
8972c42a146SMarcel Moolenaar 			else
89890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
8992c42a146SMarcel Moolenaar 		}
90023eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
90123eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
90223eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
90323eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
90423eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
90523eeeff7SPeter Wemm 		else
90623eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
90723eeeff7SPeter Wemm #endif
908e8ebc08fSPeter Wemm #ifdef COMPAT_43
909645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
91023eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
91123eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
912645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
913645682fdSLuoqi Chen 		else
914645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
915e8ebc08fSPeter Wemm #endif
916df8bae1dSRodney W. Grimes 	}
91790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
918628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9192c42a146SMarcel Moolenaar 	return (0);
9202c42a146SMarcel Moolenaar }
9212c42a146SMarcel Moolenaar 
9222c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9232c42a146SMarcel Moolenaar struct sigaction_args {
9242c42a146SMarcel Moolenaar 	int	sig;
9252c42a146SMarcel Moolenaar 	struct	sigaction *act;
9262c42a146SMarcel Moolenaar 	struct	sigaction *oact;
9272c42a146SMarcel Moolenaar };
9282c42a146SMarcel Moolenaar #endif
9292c42a146SMarcel Moolenaar int
930e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap)
9312c42a146SMarcel Moolenaar {
9322c42a146SMarcel Moolenaar 	struct sigaction act, oact;
933e052a8b9SEd Maste 	struct sigaction *actp, *oactp;
9342c42a146SMarcel Moolenaar 	int error;
9352c42a146SMarcel Moolenaar 
9366f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
9376f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
9382c42a146SMarcel Moolenaar 	if (actp) {
9396f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
9402c42a146SMarcel Moolenaar 		if (error)
94144443757STim J. Robbins 			return (error);
9422c42a146SMarcel Moolenaar 	}
9438f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
94425d6dc06SJohn Baldwin 	if (oactp && !error)
9456f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
9462c42a146SMarcel Moolenaar 	return (error);
9472c42a146SMarcel Moolenaar }
9482c42a146SMarcel Moolenaar 
94923eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
95023eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
95123eeeff7SPeter Wemm struct freebsd4_sigaction_args {
95223eeeff7SPeter Wemm 	int	sig;
95323eeeff7SPeter Wemm 	struct	sigaction *act;
95423eeeff7SPeter Wemm 	struct	sigaction *oact;
95523eeeff7SPeter Wemm };
95623eeeff7SPeter Wemm #endif
95723eeeff7SPeter Wemm int
958e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
95923eeeff7SPeter Wemm {
96023eeeff7SPeter Wemm 	struct sigaction act, oact;
961e052a8b9SEd Maste 	struct sigaction *actp, *oactp;
96223eeeff7SPeter Wemm 	int error;
96323eeeff7SPeter Wemm 
96423eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
96523eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
96623eeeff7SPeter Wemm 	if (actp) {
96723eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
96823eeeff7SPeter Wemm 		if (error)
96944443757STim J. Robbins 			return (error);
97023eeeff7SPeter Wemm 	}
97123eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
972a14e1189SJohn Baldwin 	if (oactp && !error)
97323eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
97423eeeff7SPeter Wemm 	return (error);
97523eeeff7SPeter Wemm }
97623eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
97723eeeff7SPeter Wemm 
97831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
9792c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9802c42a146SMarcel Moolenaar struct osigaction_args {
9812c42a146SMarcel Moolenaar 	int	signum;
9822c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
9832c42a146SMarcel Moolenaar 	struct	osigaction *osa;
9842c42a146SMarcel Moolenaar };
9852c42a146SMarcel Moolenaar #endif
9862c42a146SMarcel Moolenaar int
987e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap)
9882c42a146SMarcel Moolenaar {
9892c42a146SMarcel Moolenaar 	struct osigaction sa;
9902c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
991e052a8b9SEd Maste 	struct sigaction *nsap, *osap;
9922c42a146SMarcel Moolenaar 	int error;
9932c42a146SMarcel Moolenaar 
9946f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
9956f841fb7SMarcel Moolenaar 		return (EINVAL);
996fb99ab88SMatthew Dillon 
9976f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
9986f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
999fb99ab88SMatthew Dillon 
10002c42a146SMarcel Moolenaar 	if (nsap) {
10016f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
10022c42a146SMarcel Moolenaar 		if (error)
100344443757STim J. Robbins 			return (error);
10042c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
10052c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
10062c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
10072c42a146SMarcel Moolenaar 	}
100823eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10092c42a146SMarcel Moolenaar 	if (osap && !error) {
10102c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
10112c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
10122c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
10136f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
10142c42a146SMarcel Moolenaar 	}
10152c42a146SMarcel Moolenaar 	return (error);
10162c42a146SMarcel Moolenaar }
101723eeeff7SPeter Wemm 
101873dbd3daSJohn Baldwin #if !defined(__i386__)
101923eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
102023eeeff7SPeter Wemm int
1021e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap)
102223eeeff7SPeter Wemm {
102323eeeff7SPeter Wemm 
102423eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
102523eeeff7SPeter Wemm }
102623eeeff7SPeter Wemm #endif
102731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1028df8bae1dSRodney W. Grimes 
1029df8bae1dSRodney W. Grimes /*
1030df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
1031df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
1032df8bae1dSRodney W. Grimes  */
1033df8bae1dSRodney W. Grimes void
1034e052a8b9SEd Maste siginit(struct proc *p)
1035df8bae1dSRodney W. Grimes {
10363e85b721SEd Maste 	int i;
103790af4afaSJohn Baldwin 	struct sigacts *ps;
1038df8bae1dSRodney W. Grimes 
1039628d2653SJohn Baldwin 	PROC_LOCK(p);
104090af4afaSJohn Baldwin 	ps = p->p_sigacts;
104190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
1042350ae563SKonstantin Belousov 	for (i = 1; i <= NSIG; i++) {
1043fd50a707SBrooks Davis 		if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
104490af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
1045350ae563SKonstantin Belousov 		}
1046350ae563SKonstantin Belousov 	}
104790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
1048628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1049df8bae1dSRodney W. Grimes }
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes /*
1052350ae563SKonstantin Belousov  * Reset specified signal to the default disposition.
1053350ae563SKonstantin Belousov  */
1054350ae563SKonstantin Belousov static void
1055350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig)
1056350ae563SKonstantin Belousov {
1057350ae563SKonstantin Belousov 
1058350ae563SKonstantin Belousov 	mtx_assert(&ps->ps_mtx, MA_OWNED);
1059350ae563SKonstantin Belousov 	SIGDELSET(ps->ps_sigcatch, sig);
1060fd50a707SBrooks Davis 	if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
1061350ae563SKonstantin Belousov 		SIGADDSET(ps->ps_sigignore, sig);
1062350ae563SKonstantin Belousov 	ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1063350ae563SKonstantin Belousov 	SIGDELSET(ps->ps_siginfo, sig);
1064350ae563SKonstantin Belousov }
1065350ae563SKonstantin Belousov 
1066350ae563SKonstantin Belousov /*
1067df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
1068df8bae1dSRodney W. Grimes  */
1069df8bae1dSRodney W. Grimes void
1070a30ec4b9SDavid Xu execsigs(struct proc *p)
1071df8bae1dSRodney W. Grimes {
1072a30ec4b9SDavid Xu 	struct sigacts *ps;
1073a30ec4b9SDavid Xu 	struct thread *td;
1074df8bae1dSRodney W. Grimes 
1075df8bae1dSRodney W. Grimes 	/*
1076df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
10774093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
1078df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
1079df8bae1dSRodney W. Grimes 	 */
10809b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1081628d2653SJohn Baldwin 	ps = p->p_sigacts;
108290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
1083079c5b9eSKyle Evans 	sig_drop_caught(p);
1084f3fe76ecSEd Schouten 
1085f3fe76ecSEd Schouten 	/*
1086df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
1087df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
1088df8bae1dSRodney W. Grimes 	 */
1089469ec1ebSKonstantin Belousov 	td = curthread;
1090469ec1ebSKonstantin Belousov 	MPASS(td->td_proc == p);
1091a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
1092a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
1093a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
1094a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
109580e907a1SPeter Wemm 	/*
109680e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
109780e907a1SPeter Wemm 	 */
109890af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1099c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1100c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
110190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
1102df8bae1dSRodney W. Grimes }
1103df8bae1dSRodney W. Grimes 
1104df8bae1dSRodney W. Grimes /*
1105e77daab1SJohn Baldwin  * kern_sigprocmask()
11067c8fdcbdSMatthew Dillon  *
1107628d2653SJohn Baldwin  *	Manipulate signal mask.
1108df8bae1dSRodney W. Grimes  */
1109e77daab1SJohn Baldwin int
11106b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
111184440afbSKonstantin Belousov     int flags)
11122c42a146SMarcel Moolenaar {
11136b286ee8SKonstantin Belousov 	sigset_t new_block, oset1;
11146b286ee8SKonstantin Belousov 	struct proc *p;
11152c42a146SMarcel Moolenaar 	int error;
11162c42a146SMarcel Moolenaar 
11176b286ee8SKonstantin Belousov 	p = td->td_proc;
111870778bbaSKonstantin Belousov 	if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
111970778bbaSKonstantin Belousov 		PROC_LOCK_ASSERT(p, MA_OWNED);
112070778bbaSKonstantin Belousov 	else
11216b286ee8SKonstantin Belousov 		PROC_LOCK(p);
112270778bbaSKonstantin Belousov 	mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
112370778bbaSKonstantin Belousov 	    ? MA_OWNED : MA_NOTOWNED);
11242c42a146SMarcel Moolenaar 	if (oset != NULL)
11254093529dSJeff Roberson 		*oset = td->td_sigmask;
11262c42a146SMarcel Moolenaar 
11272c42a146SMarcel Moolenaar 	error = 0;
11282c42a146SMarcel Moolenaar 	if (set != NULL) {
11292c42a146SMarcel Moolenaar 		switch (how) {
11302c42a146SMarcel Moolenaar 		case SIG_BLOCK:
1131645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
11326b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
11334093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
11346b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
11356b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
11362c42a146SMarcel Moolenaar 			break;
11372c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
11384093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
11394093529dSJeff Roberson 			signotify(td);
1140407af02bSDavid Xu 			goto out;
11412c42a146SMarcel Moolenaar 		case SIG_SETMASK:
1142645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
11436b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
114484440afbSKonstantin Belousov 			if (flags & SIGPROCMASK_OLD)
11454093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
1146645682fdSLuoqi Chen 			else
11474093529dSJeff Roberson 				td->td_sigmask = *set;
11486b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
11496b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
11504093529dSJeff Roberson 			signotify(td);
11512c42a146SMarcel Moolenaar 			break;
11522c42a146SMarcel Moolenaar 		default:
11532c42a146SMarcel Moolenaar 			error = EINVAL;
1154407af02bSDavid Xu 			goto out;
11552c42a146SMarcel Moolenaar 		}
11566b286ee8SKonstantin Belousov 
11576b286ee8SKonstantin Belousov 		/*
11587df8f6abSKonstantin Belousov 		 * The new_block set contains signals that were not previously
11596b286ee8SKonstantin Belousov 		 * blocked, but are blocked now.
11606b286ee8SKonstantin Belousov 		 *
11616b286ee8SKonstantin Belousov 		 * In case we block any signal that was not previously blocked
11626b286ee8SKonstantin Belousov 		 * for td, and process has the signal pending, try to schedule
1163407af02bSDavid Xu 		 * signal delivery to some thread that does not block the
1164407af02bSDavid Xu 		 * signal, possibly waking it up.
11656b286ee8SKonstantin Belousov 		 */
11666b286ee8SKonstantin Belousov 		if (p->p_numthreads != 1)
116780a8b0f3SKonstantin Belousov 			reschedule_signals(p, new_block, flags);
1168407af02bSDavid Xu 	}
11696b286ee8SKonstantin Belousov 
1170407af02bSDavid Xu out:
117184440afbSKonstantin Belousov 	if (!(flags & SIGPROCMASK_PROC_LOCKED))
11726b286ee8SKonstantin Belousov 		PROC_UNLOCK(p);
11732c42a146SMarcel Moolenaar 	return (error);
11742c42a146SMarcel Moolenaar }
11752c42a146SMarcel Moolenaar 
1176d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1177df8bae1dSRodney W. Grimes struct sigprocmask_args {
1178df8bae1dSRodney W. Grimes 	int	how;
11792c42a146SMarcel Moolenaar 	const sigset_t *set;
11802c42a146SMarcel Moolenaar 	sigset_t *oset;
1181df8bae1dSRodney W. Grimes };
1182d2d3e875SBruce Evans #endif
118326f9a767SRodney W. Grimes int
1184e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1185df8bae1dSRodney W. Grimes {
11862c42a146SMarcel Moolenaar 	sigset_t set, oset;
11872c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
11882c42a146SMarcel Moolenaar 	int error;
1189df8bae1dSRodney W. Grimes 
11906f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
11916f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
11922c42a146SMarcel Moolenaar 	if (setp) {
11936f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
11942c42a146SMarcel Moolenaar 		if (error)
11952c42a146SMarcel Moolenaar 			return (error);
1196df8bae1dSRodney W. Grimes 	}
1197e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
11982c42a146SMarcel Moolenaar 	if (osetp && !error) {
11996f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
12002c42a146SMarcel Moolenaar 	}
12012c42a146SMarcel Moolenaar 	return (error);
12022c42a146SMarcel Moolenaar }
12032c42a146SMarcel Moolenaar 
120431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
12052c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
12062c42a146SMarcel Moolenaar struct osigprocmask_args {
12072c42a146SMarcel Moolenaar 	int	how;
12082c42a146SMarcel Moolenaar 	osigset_t mask;
12092c42a146SMarcel Moolenaar };
12102c42a146SMarcel Moolenaar #endif
12112c42a146SMarcel Moolenaar int
1212e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap)
12132c42a146SMarcel Moolenaar {
12142c42a146SMarcel Moolenaar 	sigset_t set, oset;
12152c42a146SMarcel Moolenaar 	int error;
12162c42a146SMarcel Moolenaar 
12172c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1218e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1219b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
1220df8bae1dSRodney W. Grimes 	return (error);
1221df8bae1dSRodney W. Grimes }
122231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1223df8bae1dSRodney W. Grimes 
122426f9a767SRodney W. Grimes int
12258451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap)
1226a447cd8bSJeff Roberson {
12279104847fSDavid Xu 	ksiginfo_t ksi;
1228a447cd8bSJeff Roberson 	sigset_t set;
1229a447cd8bSJeff Roberson 	int error;
1230a447cd8bSJeff Roberson 
1231a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
123236939a0aSDavid Xu 	if (error) {
123336939a0aSDavid Xu 		td->td_retval[0] = error;
123436939a0aSDavid Xu 		return (0);
123536939a0aSDavid Xu 	}
1236a447cd8bSJeff Roberson 
12379104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
123836939a0aSDavid Xu 	if (error) {
1239acced8b0SKonstantin Belousov 		/*
1240acced8b0SKonstantin Belousov 		 * sigwait() function shall not return EINTR, but
1241acced8b0SKonstantin Belousov 		 * the syscall does.  Non-ancient libc provides the
1242acced8b0SKonstantin Belousov 		 * wrapper which hides EINTR.  Otherwise, EINTR return
1243acced8b0SKonstantin Belousov 		 * is used by libthr to handle required cancellation
1244acced8b0SKonstantin Belousov 		 * point in the sigwait().
1245acced8b0SKonstantin Belousov 		 */
12468e9a54eeSKonstantin Belousov 		if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1247afb36e28SKonstantin Belousov 			return (ERESTART);
124836939a0aSDavid Xu 		td->td_retval[0] = error;
124936939a0aSDavid Xu 		return (0);
125036939a0aSDavid Xu 	}
1251a447cd8bSJeff Roberson 
12529104847fSDavid Xu 	error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
125336939a0aSDavid Xu 	td->td_retval[0] = error;
125436939a0aSDavid Xu 	return (0);
1255a447cd8bSJeff Roberson }
12560c14ff0eSRobert Watson 
1257a447cd8bSJeff Roberson int
12588451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1259a447cd8bSJeff Roberson {
1260a447cd8bSJeff Roberson 	struct timespec ts;
1261a447cd8bSJeff Roberson 	struct timespec *timeout;
1262a447cd8bSJeff Roberson 	sigset_t set;
12639104847fSDavid Xu 	ksiginfo_t ksi;
1264a447cd8bSJeff Roberson 	int error;
1265a447cd8bSJeff Roberson 
1266a447cd8bSJeff Roberson 	if (uap->timeout) {
1267a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
1268a447cd8bSJeff Roberson 		if (error)
1269a447cd8bSJeff Roberson 			return (error);
1270a447cd8bSJeff Roberson 
1271a447cd8bSJeff Roberson 		timeout = &ts;
1272a447cd8bSJeff Roberson 	} else
1273a447cd8bSJeff Roberson 		timeout = NULL;
1274a447cd8bSJeff Roberson 
1275a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1276a447cd8bSJeff Roberson 	if (error)
1277a447cd8bSJeff Roberson 		return (error);
1278a447cd8bSJeff Roberson 
12799104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, timeout);
1280a447cd8bSJeff Roberson 	if (error)
1281a447cd8bSJeff Roberson 		return (error);
12829dde3bc9SDavid Xu 
1283418228dfSDavid Xu 	if (uap->info)
12849104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
12859104847fSDavid Xu 
12869104847fSDavid Xu 	if (error == 0)
12879104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1288a447cd8bSJeff Roberson 	return (error);
1289a447cd8bSJeff Roberson }
1290a447cd8bSJeff Roberson 
1291a447cd8bSJeff Roberson int
12928451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1293a447cd8bSJeff Roberson {
12949104847fSDavid Xu 	ksiginfo_t ksi;
1295a447cd8bSJeff Roberson 	sigset_t set;
1296a447cd8bSJeff Roberson 	int error;
1297a447cd8bSJeff Roberson 
1298a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1299a447cd8bSJeff Roberson 	if (error)
1300a447cd8bSJeff Roberson 		return (error);
1301a447cd8bSJeff Roberson 
13029104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
1303a447cd8bSJeff Roberson 	if (error)
1304a447cd8bSJeff Roberson 		return (error);
1305a447cd8bSJeff Roberson 
1306418228dfSDavid Xu 	if (uap->info)
13079104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
13089104847fSDavid Xu 
13099104847fSDavid Xu 	if (error == 0)
13109104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1311a447cd8bSJeff Roberson 	return (error);
1312a447cd8bSJeff Roberson }
1313a447cd8bSJeff Roberson 
131486be94fcSTycho Nightingale static void
131586be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
131686be94fcSTycho Nightingale {
131786be94fcSTycho Nightingale 	struct thread *thr;
131886be94fcSTycho Nightingale 
131986be94fcSTycho Nightingale 	FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
132086be94fcSTycho Nightingale 		if (thr == td)
132186be94fcSTycho Nightingale 			thr->td_si = *si;
132286be94fcSTycho Nightingale 		else
132386be94fcSTycho Nightingale 			thr->td_si.si_signo = 0;
132486be94fcSTycho Nightingale 	}
132586be94fcSTycho Nightingale }
132686be94fcSTycho Nightingale 
1327c6511aeaSDavid Xu int
13289104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1329a447cd8bSJeff Roberson 	struct timespec *timeout)
1330a447cd8bSJeff Roberson {
13313074d1b4SDavid Xu 	struct sigacts *ps;
1332407af02bSDavid Xu 	sigset_t saved_mask, new_block;
1333a447cd8bSJeff Roberson 	struct proc *p;
13344a700f3cSDmitry Chagin 	int error, sig, timevalid = 0;
13354a700f3cSDmitry Chagin 	sbintime_t sbt, precision, tsbt;
13364a700f3cSDmitry Chagin 	struct timespec ts;
1337ef401a85SKonstantin Belousov 	bool traced;
1338a447cd8bSJeff Roberson 
1339a447cd8bSJeff Roberson 	p = td->td_proc;
1340a447cd8bSJeff Roberson 	error = 0;
1341ef401a85SKonstantin Belousov 	traced = false;
1342a447cd8bSJeff Roberson 
1343dbec10e0SJonathan T. Looney 	/* Ensure the sigfastblock value is up to date. */
1344dbec10e0SJonathan T. Looney 	sigfastblock_fetch(td);
1345dbec10e0SJonathan T. Looney 
1346407af02bSDavid Xu 	if (timeout != NULL) {
13471089f031SDavid Xu 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
13481089f031SDavid Xu 			timevalid = 1;
13494a700f3cSDmitry Chagin 			ts = *timeout;
13504a700f3cSDmitry Chagin 			if (ts.tv_sec < INT32_MAX / 2) {
13514a700f3cSDmitry Chagin 				tsbt = tstosbt(ts);
13524a700f3cSDmitry Chagin 				precision = tsbt;
13534a700f3cSDmitry Chagin 				precision >>= tc_precexp;
13544a700f3cSDmitry Chagin 				if (TIMESEL(&sbt, tsbt))
13554a700f3cSDmitry Chagin 					sbt += tc_tick_sbt;
13564a700f3cSDmitry Chagin 				sbt += tsbt;
13574a700f3cSDmitry Chagin 			} else
13584a700f3cSDmitry Chagin 				precision = sbt = 0;
13591089f031SDavid Xu 		}
13604a700f3cSDmitry Chagin 	} else
13614a700f3cSDmitry Chagin 		precision = sbt = 0;
1362407af02bSDavid Xu 	ksiginfo_init(ksi);
1363407af02bSDavid Xu 	/* Some signals can not be waited for. */
1364407af02bSDavid Xu 	SIG_CANTMASK(waitset);
1365407af02bSDavid Xu 	ps = p->p_sigacts;
1366407af02bSDavid Xu 	PROC_LOCK(p);
1367407af02bSDavid Xu 	saved_mask = td->td_sigmask;
1368407af02bSDavid Xu 	SIGSETNAND(td->td_sigmask, waitset);
1369bc387624SKonstantin Belousov 	if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1370b599982bSKonstantin Belousov 	    !kern_sig_discard_ign) {
1371b599982bSKonstantin Belousov 		thread_lock(td);
1372b599982bSKonstantin Belousov 		td->td_flags |= TDF_SIGWAIT;
1373b599982bSKonstantin Belousov 		thread_unlock(td);
1374b599982bSKonstantin Belousov 	}
1375407af02bSDavid Xu 	for (;;) {
13763074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
13773cf3b9f0SJohn Baldwin 		sig = cursig(td);
13783074d1b4SDavid Xu 		mtx_unlock(&ps->ps_mtx);
137946e47c4fSKonstantin Belousov 		KASSERT(sig >= 0, ("sig %d", sig));
1380407af02bSDavid Xu 		if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1381407af02bSDavid Xu 			if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1382407af02bSDavid Xu 			    sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1383407af02bSDavid Xu 				error = 0;
1384407af02bSDavid Xu 				break;
13853074d1b4SDavid Xu 			}
13867e0221a2SDavid Xu 		}
13877e0221a2SDavid Xu 
1388407af02bSDavid Xu 		if (error != 0)
1389407af02bSDavid Xu 			break;
13903074d1b4SDavid Xu 
1391a447cd8bSJeff Roberson 		/*
1392a447cd8bSJeff Roberson 		 * POSIX says this must be checked after looking for pending
1393a447cd8bSJeff Roberson 		 * signals.
1394a447cd8bSJeff Roberson 		 */
13954a700f3cSDmitry Chagin 		if (timeout != NULL && !timevalid) {
1396a447cd8bSJeff Roberson 			error = EINVAL;
1397407af02bSDavid Xu 			break;
1398a447cd8bSJeff Roberson 		}
1399a447cd8bSJeff Roberson 
1400ef401a85SKonstantin Belousov 		if (traced) {
1401ef401a85SKonstantin Belousov 			error = EINTR;
1402ef401a85SKonstantin Belousov 			break;
1403ef401a85SKonstantin Belousov 		}
1404ef401a85SKonstantin Belousov 
14054a700f3cSDmitry Chagin 		error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
14064a700f3cSDmitry Chagin 		    "sigwait", sbt, precision, C_ABSOLUTE);
1407407af02bSDavid Xu 
1408afb36e28SKonstantin Belousov 		/* The syscalls can not be restarted. */
1409afb36e28SKonstantin Belousov 		if (error == ERESTART)
14103074d1b4SDavid Xu 			error = EINTR;
1411afb36e28SKonstantin Belousov 
1412ef401a85SKonstantin Belousov 		/*
1413ef401a85SKonstantin Belousov 		 * If PTRACE_SCE or PTRACE_SCX were set after
1414ef401a85SKonstantin Belousov 		 * userspace entered the syscall, return spurious
1415ef401a85SKonstantin Belousov 		 * EINTR after wait was done.  Only do this as last
1416ef401a85SKonstantin Belousov 		 * resort after rechecking for possible queued signals
1417ef401a85SKonstantin Belousov 		 * and expired timeouts.
1418ef401a85SKonstantin Belousov 		 */
1419ef401a85SKonstantin Belousov 		if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1420ef401a85SKonstantin Belousov 			traced = true;
1421407af02bSDavid Xu 	}
1422b599982bSKonstantin Belousov 	thread_lock(td);
1423b599982bSKonstantin Belousov 	td->td_flags &= ~TDF_SIGWAIT;
1424b599982bSKonstantin Belousov 	thread_unlock(td);
14259dde3bc9SDavid Xu 
1426407af02bSDavid Xu 	new_block = saved_mask;
1427407af02bSDavid Xu 	SIGSETNAND(new_block, td->td_sigmask);
1428407af02bSDavid Xu 	td->td_sigmask = saved_mask;
1429407af02bSDavid Xu 	/*
1430407af02bSDavid Xu 	 * Fewer signals can be delivered to us, reschedule signal
1431407af02bSDavid Xu 	 * notification.
1432407af02bSDavid Xu 	 */
1433407af02bSDavid Xu 	if (p->p_numthreads != 1)
1434407af02bSDavid Xu 		reschedule_signals(p, new_block, 0);
14355d217f17SJohn Birrell 
1436407af02bSDavid Xu 	if (error == 0) {
143736160958SMark Johnston 		SDT_PROBE2(proc, , , signal__clear, sig, ksi);
14385d217f17SJohn Birrell 
143956c06c4bSDavid Xu 		if (ksi->ksi_code == SI_TIMER)
144056c06c4bSDavid Xu 			itimer_accept(p, ksi->ksi_timerid, ksi);
14417e0221a2SDavid Xu 
14427e0221a2SDavid Xu #ifdef KTRACE
14437e0221a2SDavid Xu 		if (KTRPOINT(td, KTR_PSIG)) {
14447e0221a2SDavid Xu 			sig_t action;
14457e0221a2SDavid Xu 
14463074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
1447a447cd8bSJeff Roberson 			action = ps->ps_sigact[_SIG_IDX(sig)];
144890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
144961009552SJilles Tjoelker 			ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
14507e0221a2SDavid Xu 		}
1451a447cd8bSJeff Roberson #endif
145286be94fcSTycho Nightingale 		if (sig == SIGKILL) {
145386be94fcSTycho Nightingale 			proc_td_siginfo_capture(td, &ksi->ksi_info);
14547e0221a2SDavid Xu 			sigexit(td, sig);
14553074d1b4SDavid Xu 		}
145686be94fcSTycho Nightingale 	}
1457a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
1458a447cd8bSJeff Roberson 	return (error);
1459a447cd8bSJeff Roberson }
1460a447cd8bSJeff Roberson 
14619104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
14629104847fSDavid Xu struct sigpending_args {
14639104847fSDavid Xu 	sigset_t	*set;
14649104847fSDavid Xu };
14659104847fSDavid Xu #endif
1466a447cd8bSJeff Roberson int
1467e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap)
1468df8bae1dSRodney W. Grimes {
1469b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
14709104847fSDavid Xu 	sigset_t pending;
1471df8bae1dSRodney W. Grimes 
1472628d2653SJohn Baldwin 	PROC_LOCK(p);
14739104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
14749104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1475628d2653SJohn Baldwin 	PROC_UNLOCK(p);
14769104847fSDavid Xu 	return (copyout(&pending, uap->set, sizeof(sigset_t)));
14772c42a146SMarcel Moolenaar }
14782c42a146SMarcel Moolenaar 
147931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
14802c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
14812c42a146SMarcel Moolenaar struct osigpending_args {
14822c42a146SMarcel Moolenaar 	int	dummy;
14832c42a146SMarcel Moolenaar };
14842c42a146SMarcel Moolenaar #endif
14852c42a146SMarcel Moolenaar int
1486e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap)
14872c42a146SMarcel Moolenaar {
1488b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
14899104847fSDavid Xu 	sigset_t pending;
1490b40ce416SJulian Elischer 
1491628d2653SJohn Baldwin 	PROC_LOCK(p);
14929104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
14939104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1494628d2653SJohn Baldwin 	PROC_UNLOCK(p);
14959104847fSDavid Xu 	SIG2OSIG(pending, td->td_retval[0]);
1496df8bae1dSRodney W. Grimes 	return (0);
1497df8bae1dSRodney W. Grimes }
149831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1499df8bae1dSRodney W. Grimes 
15001930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1501df8bae1dSRodney W. Grimes /*
1502df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1503df8bae1dSRodney W. Grimes  */
1504d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1505df8bae1dSRodney W. Grimes struct osigvec_args {
1506df8bae1dSRodney W. Grimes 	int	signum;
1507df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1508df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1509df8bae1dSRodney W. Grimes };
1510d2d3e875SBruce Evans #endif
1511df8bae1dSRodney W. Grimes /* ARGSUSED */
151226f9a767SRodney W. Grimes int
1513e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap)
1514df8bae1dSRodney W. Grimes {
1515df8bae1dSRodney W. Grimes 	struct sigvec vec;
15162c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
1517e052a8b9SEd Maste 	struct sigaction *nsap, *osap;
15182c42a146SMarcel Moolenaar 	int error;
1519df8bae1dSRodney W. Grimes 
15206f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
15216f841fb7SMarcel Moolenaar 		return (EINVAL);
15226f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
15236f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
15242c42a146SMarcel Moolenaar 	if (nsap) {
15256f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
15262c42a146SMarcel Moolenaar 		if (error)
1527df8bae1dSRodney W. Grimes 			return (error);
15282c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
15292c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
15302c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
15312c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1532df8bae1dSRodney W. Grimes 	}
15335edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
15342c42a146SMarcel Moolenaar 	if (osap && !error) {
15352c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
15362c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
15372c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
15382c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
15392c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
15406f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
15412c42a146SMarcel Moolenaar 	}
15422c42a146SMarcel Moolenaar 	return (error);
1543df8bae1dSRodney W. Grimes }
1544df8bae1dSRodney W. Grimes 
1545d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1546df8bae1dSRodney W. Grimes struct osigblock_args {
1547df8bae1dSRodney W. Grimes 	int	mask;
1548df8bae1dSRodney W. Grimes };
1549d2d3e875SBruce Evans #endif
155026f9a767SRodney W. Grimes int
1551e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap)
1552df8bae1dSRodney W. Grimes {
1553d6e029adSKonstantin Belousov 	sigset_t set, oset;
1554df8bae1dSRodney W. Grimes 
15552c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1556d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1557d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1558df8bae1dSRodney W. Grimes 	return (0);
1559df8bae1dSRodney W. Grimes }
1560df8bae1dSRodney W. Grimes 
1561d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1562df8bae1dSRodney W. Grimes struct osigsetmask_args {
1563df8bae1dSRodney W. Grimes 	int	mask;
1564df8bae1dSRodney W. Grimes };
1565d2d3e875SBruce Evans #endif
156626f9a767SRodney W. Grimes int
1567e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1568df8bae1dSRodney W. Grimes {
1569d6e029adSKonstantin Belousov 	sigset_t set, oset;
1570df8bae1dSRodney W. Grimes 
15712c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1572d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1573d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1574df8bae1dSRodney W. Grimes 	return (0);
1575df8bae1dSRodney W. Grimes }
15761930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1577df8bae1dSRodney W. Grimes 
1578df8bae1dSRodney W. Grimes /*
1579873fbcd7SRobert Watson  * Suspend calling thread until signal, providing mask to be set in the
1580873fbcd7SRobert Watson  * meantime.
1581df8bae1dSRodney W. Grimes  */
1582d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1583df8bae1dSRodney W. Grimes struct sigsuspend_args {
15842c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1585df8bae1dSRodney W. Grimes };
1586d2d3e875SBruce Evans #endif
1587df8bae1dSRodney W. Grimes /* ARGSUSED */
158826f9a767SRodney W. Grimes int
1589e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1590df8bae1dSRodney W. Grimes {
15912c42a146SMarcel Moolenaar 	sigset_t mask;
15922c42a146SMarcel Moolenaar 	int error;
15932c42a146SMarcel Moolenaar 
15946f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
15952c42a146SMarcel Moolenaar 	if (error)
15962c42a146SMarcel Moolenaar 		return (error);
15978f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
15988f19eb88SIan Dowse }
15998f19eb88SIan Dowse 
16008f19eb88SIan Dowse int
16018f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
16028f19eb88SIan Dowse {
16038f19eb88SIan Dowse 	struct proc *p = td->td_proc;
160484440afbSKonstantin Belousov 	int has_sig, sig;
1605df8bae1dSRodney W. Grimes 
1606dbec10e0SJonathan T. Looney 	/* Ensure the sigfastblock value is up to date. */
1607dbec10e0SJonathan T. Looney 	sigfastblock_fetch(td);
1608dbec10e0SJonathan T. Looney 
1609df8bae1dSRodney W. Grimes 	/*
1610645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1611df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1612df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1613df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1614df8bae1dSRodney W. Grimes 	 * to indicate this.
1615df8bae1dSRodney W. Grimes 	 */
1616628d2653SJohn Baldwin 	PROC_LOCK(p);
161784440afbSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
161884440afbSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED);
16195e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1620c6d31b83SKonstantin Belousov 	ast_sched(td, TDA_SIGSUSPEND);
162184440afbSKonstantin Belousov 
162284440afbSKonstantin Belousov 	/*
162384440afbSKonstantin Belousov 	 * Process signals now. Otherwise, we can get spurious wakeup
162484440afbSKonstantin Belousov 	 * due to signal entered process queue, but delivered to other
162584440afbSKonstantin Belousov 	 * thread. But sigsuspend should return only on signal
162684440afbSKonstantin Belousov 	 * delivery.
162784440afbSKonstantin Belousov 	 */
1628afe1a688SKonstantin Belousov 	(p->p_sysent->sv_set_syscall_retval)(td, EINTR);
162984440afbSKonstantin Belousov 	for (has_sig = 0; !has_sig;) {
163084440afbSKonstantin Belousov 		while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
163184440afbSKonstantin Belousov 			0) == 0)
16322c42a146SMarcel Moolenaar 			/* void */;
163384440afbSKonstantin Belousov 		thread_suspend_check(0);
163484440afbSKonstantin Belousov 		mtx_lock(&p->p_sigacts->ps_mtx);
163546e47c4fSKonstantin Belousov 		while ((sig = cursig(td)) != 0) {
163646e47c4fSKonstantin Belousov 			KASSERT(sig >= 0, ("sig %d", sig));
163775c586a4SKonstantin Belousov 			has_sig += postsig(sig);
163846e47c4fSKonstantin Belousov 		}
163984440afbSKonstantin Belousov 		mtx_unlock(&p->p_sigacts->ps_mtx);
1640ef401a85SKonstantin Belousov 
1641ef401a85SKonstantin Belousov 		/*
1642ef401a85SKonstantin Belousov 		 * If PTRACE_SCE or PTRACE_SCX were set after
1643ef401a85SKonstantin Belousov 		 * userspace entered the syscall, return spurious
1644ef401a85SKonstantin Belousov 		 * EINTR.
1645ef401a85SKonstantin Belousov 		 */
1646ef401a85SKonstantin Belousov 		if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1647ef401a85SKonstantin Belousov 			has_sig += 1;
164884440afbSKonstantin Belousov 	}
1649628d2653SJohn Baldwin 	PROC_UNLOCK(p);
16502dd9ea6fSKonstantin Belousov 	td->td_errno = EINTR;
16512dd9ea6fSKonstantin Belousov 	td->td_pflags |= TDP_NERRNO;
165275c586a4SKonstantin Belousov 	return (EJUSTRETURN);
16532c42a146SMarcel Moolenaar }
16542c42a146SMarcel Moolenaar 
165531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
165697563428SAlexander Kabaev /*
165797563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
165897563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
165997563428SAlexander Kabaev  */
16602c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
16612c42a146SMarcel Moolenaar struct osigsuspend_args {
16622c42a146SMarcel Moolenaar 	osigset_t mask;
16632c42a146SMarcel Moolenaar };
16642c42a146SMarcel Moolenaar #endif
16652c42a146SMarcel Moolenaar /* ARGSUSED */
16662c42a146SMarcel Moolenaar int
1667e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap)
16682c42a146SMarcel Moolenaar {
1669645682fdSLuoqi Chen 	sigset_t mask;
16702c42a146SMarcel Moolenaar 
1671645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
167284440afbSKonstantin Belousov 	return (kern_sigsuspend(td, mask));
1673df8bae1dSRodney W. Grimes }
167431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1675df8bae1dSRodney W. Grimes 
16761930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1677d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1678df8bae1dSRodney W. Grimes struct osigstack_args {
1679df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1680df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1681df8bae1dSRodney W. Grimes };
1682d2d3e875SBruce Evans #endif
1683df8bae1dSRodney W. Grimes /* ARGSUSED */
168426f9a767SRodney W. Grimes int
1685e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap)
1686df8bae1dSRodney W. Grimes {
16875afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1688fb99ab88SMatthew Dillon 	int error = 0;
1689fb99ab88SMatthew Dillon 
1690d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
16915afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
16925afe0c99SJohn Baldwin 		if (error)
16935afe0c99SJohn Baldwin 			return (error);
1694df8bae1dSRodney W. Grimes 	}
1695a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
16965afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
16975afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1698a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1699a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1700a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1701a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
17025afe0c99SJohn Baldwin 	}
17035afe0c99SJohn Baldwin 	if (uap->oss != NULL)
17045afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
17055afe0c99SJohn Baldwin 
1706fb99ab88SMatthew Dillon 	return (error);
1707df8bae1dSRodney W. Grimes }
17081930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1709df8bae1dSRodney W. Grimes 
1710d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1711df8bae1dSRodney W. Grimes struct sigaltstack_args {
17122c42a146SMarcel Moolenaar 	stack_t	*ss;
17132c42a146SMarcel Moolenaar 	stack_t	*oss;
1714df8bae1dSRodney W. Grimes };
1715d2d3e875SBruce Evans #endif
1716df8bae1dSRodney W. Grimes /* ARGSUSED */
171726f9a767SRodney W. Grimes int
1718e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1719df8bae1dSRodney W. Grimes {
17208f19eb88SIan Dowse 	stack_t ss, oss;
17218f19eb88SIan Dowse 	int error;
17228f19eb88SIan Dowse 
17238f19eb88SIan Dowse 	if (uap->ss != NULL) {
17248f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
17258f19eb88SIan Dowse 		if (error)
17268f19eb88SIan Dowse 			return (error);
17278f19eb88SIan Dowse 	}
17288f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
17298f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
17308f19eb88SIan Dowse 	if (error)
17318f19eb88SIan Dowse 		return (error);
17328f19eb88SIan Dowse 	if (uap->oss != NULL)
17338f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
17348f19eb88SIan Dowse 	return (error);
17358f19eb88SIan Dowse }
17368f19eb88SIan Dowse 
17378f19eb88SIan Dowse int
17388f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
17398f19eb88SIan Dowse {
1740b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1741fb99ab88SMatthew Dillon 	int oonstack;
1742fb99ab88SMatthew Dillon 
1743b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1744d034d459SMarcel Moolenaar 
17458f19eb88SIan Dowse 	if (oss != NULL) {
1746a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1747a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1748d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1749df8bae1dSRodney W. Grimes 	}
1750d034d459SMarcel Moolenaar 
17518f19eb88SIan Dowse 	if (ss != NULL) {
1752a30ec4b9SDavid Xu 		if (oonstack)
1753cf60731bSJohn Baldwin 			return (EPERM);
1754a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1755cf60731bSJohn Baldwin 			return (EINVAL);
17568f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
17579104847fSDavid Xu 			if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1758cf60731bSJohn Baldwin 				return (ENOMEM);
17599104847fSDavid Xu 
1760a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1761a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1762628d2653SJohn Baldwin 		} else {
1763a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1764628d2653SJohn Baldwin 		}
1765d034d459SMarcel Moolenaar 	}
1766cf60731bSJohn Baldwin 	return (0);
1767df8bae1dSRodney W. Grimes }
1768df8bae1dSRodney W. Grimes 
17690cc9fb75SKonstantin Belousov struct killpg1_ctx {
17700cc9fb75SKonstantin Belousov 	struct thread *td;
17710cc9fb75SKonstantin Belousov 	ksiginfo_t *ksi;
17720cc9fb75SKonstantin Belousov 	int sig;
17730cc9fb75SKonstantin Belousov 	bool sent;
17740cc9fb75SKonstantin Belousov 	bool found;
17750cc9fb75SKonstantin Belousov 	int ret;
17760cc9fb75SKonstantin Belousov };
17770cc9fb75SKonstantin Belousov 
17780cc9fb75SKonstantin Belousov static void
177969413598SMateusz Guzik killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg)
17800cc9fb75SKonstantin Belousov {
17810cc9fb75SKonstantin Belousov 	int err;
17820cc9fb75SKonstantin Belousov 
17830cc9fb75SKonstantin Belousov 	err = p_cansignal(arg->td, p, arg->sig);
17840cc9fb75SKonstantin Belousov 	if (err == 0 && arg->sig != 0)
17850cc9fb75SKonstantin Belousov 		pksignal(p, arg->sig, arg->ksi);
17860cc9fb75SKonstantin Belousov 	if (err != ESRCH)
17870cc9fb75SKonstantin Belousov 		arg->found = true;
17880cc9fb75SKonstantin Belousov 	if (err == 0)
17890cc9fb75SKonstantin Belousov 		arg->sent = true;
17900cc9fb75SKonstantin Belousov 	else if (arg->ret == 0 && err != ESRCH && err != EPERM)
17910cc9fb75SKonstantin Belousov 		arg->ret = err;
17920cc9fb75SKonstantin Belousov }
17930cc9fb75SKonstantin Belousov 
179469413598SMateusz Guzik static void
179569413598SMateusz Guzik killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
179669413598SMateusz Guzik {
179769413598SMateusz Guzik 
179869413598SMateusz Guzik 	if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
179969413598SMateusz Guzik 	    (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
180069413598SMateusz Guzik 		return;
180169413598SMateusz Guzik 
180269413598SMateusz Guzik 	PROC_LOCK(p);
180369413598SMateusz Guzik 	killpg1_sendsig_locked(p, arg);
180469413598SMateusz Guzik 	PROC_UNLOCK(p);
180569413598SMateusz Guzik }
180669413598SMateusz Guzik 
180769413598SMateusz Guzik static void
180869413598SMateusz Guzik kill_processes_prison_cb(struct proc *p, void *arg)
180969413598SMateusz Guzik {
181069413598SMateusz Guzik 	struct killpg1_ctx *ctx = arg;
181169413598SMateusz Guzik 
181269413598SMateusz Guzik 	if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
181369413598SMateusz Guzik 	    (p == ctx->td->td_proc) || p->p_state == PRS_NEW)
181469413598SMateusz Guzik 		return;
181569413598SMateusz Guzik 
181669413598SMateusz Guzik 	killpg1_sendsig_locked(p, ctx);
181769413598SMateusz Guzik }
181869413598SMateusz Guzik 
1819d93f860cSPoul-Henning Kamp /*
1820d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1821d93f860cSPoul-Henning Kamp  * cp is calling process.
1822d93f860cSPoul-Henning Kamp  */
182337c84183SPoul-Henning Kamp static int
1824a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1825d93f860cSPoul-Henning Kamp {
1826a3de221dSKonstantin Belousov 	struct proc *p;
1827d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
18280cc9fb75SKonstantin Belousov 	struct killpg1_ctx arg;
1829d93f860cSPoul-Henning Kamp 
18300cc9fb75SKonstantin Belousov 	arg.td = td;
18310cc9fb75SKonstantin Belousov 	arg.ksi = ksi;
18320cc9fb75SKonstantin Belousov 	arg.sig = sig;
18330cc9fb75SKonstantin Belousov 	arg.sent = false;
18340cc9fb75SKonstantin Belousov 	arg.found = false;
18350cc9fb75SKonstantin Belousov 	arg.ret = 0;
1836553629ebSJake Burkholder 	if (all) {
1837d93f860cSPoul-Henning Kamp 		/*
1838d93f860cSPoul-Henning Kamp 		 * broadcast
1839d93f860cSPoul-Henning Kamp 		 */
184069413598SMateusz Guzik 		prison_proc_iterate(td->td_ucred->cr_prison,
184169413598SMateusz Guzik 		    kill_processes_prison_cb, &arg);
1842553629ebSJake Burkholder 	} else {
1843ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1844f591779bSSeigo Tanimura 		if (pgid == 0) {
1845d93f860cSPoul-Henning Kamp 			/*
1846d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1847d93f860cSPoul-Henning Kamp 			 */
18489c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1849f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1850f591779bSSeigo Tanimura 		} else {
1851d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1852f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1853ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1854d93f860cSPoul-Henning Kamp 				return (ESRCH);
1855d93f860cSPoul-Henning Kamp 			}
1856f591779bSSeigo Tanimura 		}
1857ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
18582e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
18590cc9fb75SKonstantin Belousov 			killpg1_sendsig(p, false, &arg);
1860d93f860cSPoul-Henning Kamp 		}
1861f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1862d93f860cSPoul-Henning Kamp 	}
18630cc9fb75SKonstantin Belousov 	MPASS(arg.ret != 0 || arg.found || !arg.sent);
18640cc9fb75SKonstantin Belousov 	if (arg.ret == 0 && !arg.sent)
18650cc9fb75SKonstantin Belousov 		arg.ret = arg.found ? EPERM : ESRCH;
18660cc9fb75SKonstantin Belousov 	return (arg.ret);
1867d93f860cSPoul-Henning Kamp }
1868d93f860cSPoul-Henning Kamp 
1869d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1870df8bae1dSRodney W. Grimes struct kill_args {
1871df8bae1dSRodney W. Grimes 	int	pid;
1872df8bae1dSRodney W. Grimes 	int	signum;
1873df8bae1dSRodney W. Grimes };
1874d2d3e875SBruce Evans #endif
1875df8bae1dSRodney W. Grimes /* ARGSUSED */
187626f9a767SRodney W. Grimes int
18778451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap)
1878df8bae1dSRodney W. Grimes {
187934ad5ac2SEdward Tomasz Napierala 
188034ad5ac2SEdward Tomasz Napierala 	return (kern_kill(td, uap->pid, uap->signum));
188134ad5ac2SEdward Tomasz Napierala }
188234ad5ac2SEdward Tomasz Napierala 
188334ad5ac2SEdward Tomasz Napierala int
188434ad5ac2SEdward Tomasz Napierala kern_kill(struct thread *td, pid_t pid, int signum)
188534ad5ac2SEdward Tomasz Napierala {
1886a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1887a3de221dSKonstantin Belousov 	struct proc *p;
188890af4afaSJohn Baldwin 	int error;
1889df8bae1dSRodney W. Grimes 
18908890f5d0SPawel Jakub Dawidek 	/*
18918890f5d0SPawel Jakub Dawidek 	 * A process in capability mode can send signals only to himself.
18928890f5d0SPawel Jakub Dawidek 	 * The main rationale behind this is that abort(3) is implemented as
18938890f5d0SPawel Jakub Dawidek 	 * kill(getpid(), SIGABRT).
18948890f5d0SPawel Jakub Dawidek 	 */
189534ad5ac2SEdward Tomasz Napierala 	if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
18968890f5d0SPawel Jakub Dawidek 		return (ECAPMODE);
18978890f5d0SPawel Jakub Dawidek 
189834ad5ac2SEdward Tomasz Napierala 	AUDIT_ARG_SIGNUM(signum);
189934ad5ac2SEdward Tomasz Napierala 	AUDIT_ARG_PID(pid);
190034ad5ac2SEdward Tomasz Napierala 	if ((u_int)signum > _SIG_MAXSIG)
1901df8bae1dSRodney W. Grimes 		return (EINVAL);
1902fb99ab88SMatthew Dillon 
1903a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
190434ad5ac2SEdward Tomasz Napierala 	ksi.ksi_signo = signum;
1905a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1906a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1907a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1908a3de221dSKonstantin Belousov 
190934ad5ac2SEdward Tomasz Napierala 	if (pid > 0) {
1910df8bae1dSRodney W. Grimes 		/* kill single process */
191134ad5ac2SEdward Tomasz Napierala 		if ((p = pfind_any(pid)) == NULL)
191290af4afaSJohn Baldwin 			return (ESRCH);
191314961ba7SRobert Watson 		AUDIT_ARG_PROCESS(p);
191434ad5ac2SEdward Tomasz Napierala 		error = p_cansignal(td, p, signum);
191534ad5ac2SEdward Tomasz Napierala 		if (error == 0 && signum)
191634ad5ac2SEdward Tomasz Napierala 			pksignal(p, signum, &ksi);
1917628d2653SJohn Baldwin 		PROC_UNLOCK(p);
191890af4afaSJohn Baldwin 		return (error);
1919df8bae1dSRodney W. Grimes 	}
192034ad5ac2SEdward Tomasz Napierala 	switch (pid) {
1921df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
192234ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, 0, 1, &ksi));
1923df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
192434ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, 0, 0, &ksi));
1925df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
192634ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, -pid, 0, &ksi));
1927df8bae1dSRodney W. Grimes 	}
192890af4afaSJohn Baldwin 	/* NOTREACHED */
1929df8bae1dSRodney W. Grimes }
1930df8bae1dSRodney W. Grimes 
1931cfb5f768SJonathan Anderson int
1932e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap)
1933cfb5f768SJonathan Anderson {
1934cfb5f768SJonathan Anderson 	struct proc *p;
1935cfb5f768SJonathan Anderson 	int error;
1936cfb5f768SJonathan Anderson 
1937cfb5f768SJonathan Anderson 	AUDIT_ARG_SIGNUM(uap->signum);
1938cfb5f768SJonathan Anderson 	AUDIT_ARG_FD(uap->fd);
1939cfb5f768SJonathan Anderson 	if ((u_int)uap->signum > _SIG_MAXSIG)
1940cfb5f768SJonathan Anderson 		return (EINVAL);
1941cfb5f768SJonathan Anderson 
1942cbd92ce6SMatt Macy 	error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1943cfb5f768SJonathan Anderson 	if (error)
1944cfb5f768SJonathan Anderson 		return (error);
1945cfb5f768SJonathan Anderson 	AUDIT_ARG_PROCESS(p);
1946cfb5f768SJonathan Anderson 	error = p_cansignal(td, p, uap->signum);
1947cfb5f768SJonathan Anderson 	if (error == 0 && uap->signum)
19488451d0ddSKip Macy 		kern_psignal(p, uap->signum);
1949cfb5f768SJonathan Anderson 	PROC_UNLOCK(p);
1950cfb5f768SJonathan Anderson 	return (error);
1951cfb5f768SJonathan Anderson }
1952cfb5f768SJonathan Anderson 
19531930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1954d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1955df8bae1dSRodney W. Grimes struct okillpg_args {
1956df8bae1dSRodney W. Grimes 	int	pgid;
1957df8bae1dSRodney W. Grimes 	int	signum;
1958df8bae1dSRodney W. Grimes };
1959d2d3e875SBruce Evans #endif
1960df8bae1dSRodney W. Grimes /* ARGSUSED */
196126f9a767SRodney W. Grimes int
1962a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap)
1963df8bae1dSRodney W. Grimes {
1964a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1965df8bae1dSRodney W. Grimes 
196614961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->signum);
196714961ba7SRobert Watson 	AUDIT_ARG_PID(uap->pgid);
19686c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1969df8bae1dSRodney W. Grimes 		return (EINVAL);
19709104847fSDavid Xu 
1971a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
1972a3de221dSKonstantin Belousov 	ksi.ksi_signo = uap->signum;
1973a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1974a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1975a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1976a3de221dSKonstantin Belousov 	return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1977df8bae1dSRodney W. Grimes }
19781930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1979df8bae1dSRodney W. Grimes 
19809104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
19819104847fSDavid Xu struct sigqueue_args {
19829104847fSDavid Xu 	pid_t pid;
19839104847fSDavid Xu 	int signum;
19849104847fSDavid Xu 	/* union sigval */ void *value;
19859104847fSDavid Xu };
19869104847fSDavid Xu #endif
19879104847fSDavid Xu int
19888451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
19899104847fSDavid Xu {
1990f19351aaSBrooks Davis 	union sigval sv;
1991f19351aaSBrooks Davis 
1992f19351aaSBrooks Davis 	sv.sival_ptr = uap->value;
1993f19351aaSBrooks Davis 
1994f19351aaSBrooks Davis 	return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
1995f19351aaSBrooks Davis }
1996f19351aaSBrooks Davis 
1997f19351aaSBrooks Davis int
1998f19351aaSBrooks Davis kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
1999f19351aaSBrooks Davis {
20009104847fSDavid Xu 	ksiginfo_t ksi;
20019104847fSDavid Xu 	struct proc *p;
20029104847fSDavid Xu 	int error;
20039104847fSDavid Xu 
2004f19351aaSBrooks Davis 	if ((u_int)signum > _SIG_MAXSIG)
20059104847fSDavid Xu 		return (EINVAL);
20069104847fSDavid Xu 
20079104847fSDavid Xu 	/*
20089104847fSDavid Xu 	 * Specification says sigqueue can only send signal to
20099104847fSDavid Xu 	 * single process.
20109104847fSDavid Xu 	 */
2011f19351aaSBrooks Davis 	if (pid <= 0)
20129104847fSDavid Xu 		return (EINVAL);
20139104847fSDavid Xu 
2014537d0fb1SMateusz Guzik 	if ((p = pfind_any(pid)) == NULL)
20159104847fSDavid Xu 		return (ESRCH);
2016f19351aaSBrooks Davis 	error = p_cansignal(td, p, signum);
2017f19351aaSBrooks Davis 	if (error == 0 && signum != 0) {
20189104847fSDavid Xu 		ksiginfo_init(&ksi);
2019a3de221dSKonstantin Belousov 		ksi.ksi_flags = KSI_SIGQ;
2020f19351aaSBrooks Davis 		ksi.ksi_signo = signum;
20219104847fSDavid Xu 		ksi.ksi_code = SI_QUEUE;
20229104847fSDavid Xu 		ksi.ksi_pid = td->td_proc->p_pid;
20239104847fSDavid Xu 		ksi.ksi_uid = td->td_ucred->cr_ruid;
2024f19351aaSBrooks Davis 		ksi.ksi_value = *value;
2025ad6eec7bSJohn Baldwin 		error = pksignal(p, ksi.ksi_signo, &ksi);
20269104847fSDavid Xu 	}
20279104847fSDavid Xu 	PROC_UNLOCK(p);
20289104847fSDavid Xu 	return (error);
20299104847fSDavid Xu }
20309104847fSDavid Xu 
2031df8bae1dSRodney W. Grimes /*
2032df8bae1dSRodney W. Grimes  * Send a signal to a process group.
2033df8bae1dSRodney W. Grimes  */
2034df8bae1dSRodney W. Grimes void
2035a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi)
2036df8bae1dSRodney W. Grimes {
2037df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
2038df8bae1dSRodney W. Grimes 
2039f591779bSSeigo Tanimura 	if (pgid != 0) {
2040ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
2041f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
2042ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
2043f591779bSSeigo Tanimura 		if (pgrp != NULL) {
2044a3de221dSKonstantin Belousov 			pgsignal(pgrp, sig, 0, ksi);
2045f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
2046f591779bSSeigo Tanimura 		}
2047f591779bSSeigo Tanimura 	}
2048df8bae1dSRodney W. Grimes }
2049df8bae1dSRodney W. Grimes 
2050df8bae1dSRodney W. Grimes /*
2051df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
2052df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
2053df8bae1dSRodney W. Grimes  */
2054df8bae1dSRodney W. Grimes void
2055a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2056df8bae1dSRodney W. Grimes {
2057a3de221dSKonstantin Belousov 	struct proc *p;
2058df8bae1dSRodney W. Grimes 
2059628d2653SJohn Baldwin 	if (pgrp) {
2060f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2061628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2062628d2653SJohn Baldwin 			PROC_LOCK(p);
2063e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
2064e806d352SJohn Baldwin 			    (checkctty == 0 || p->p_flag & P_CONTROLT))
2065a3de221dSKonstantin Belousov 				pksignal(p, sig, ksi);
2066628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2067628d2653SJohn Baldwin 		}
2068628d2653SJohn Baldwin 	}
2069df8bae1dSRodney W. Grimes }
2070df8bae1dSRodney W. Grimes 
2071e442f29fSKonstantin Belousov /*
2072e442f29fSKonstantin Belousov  * Recalculate the signal mask and reset the signal disposition after
2073e442f29fSKonstantin Belousov  * usermode frame for delivery is formed.  Should be called after
2074e442f29fSKonstantin Belousov  * mach-specific routine, because sysent->sv_sendsig() needs correct
2075e442f29fSKonstantin Belousov  * ps_siginfo and signal mask.
2076e442f29fSKonstantin Belousov  */
2077e442f29fSKonstantin Belousov static void
2078e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps)
2079e442f29fSKonstantin Belousov {
2080e442f29fSKonstantin Belousov 	sigset_t mask;
2081e442f29fSKonstantin Belousov 
2082e442f29fSKonstantin Belousov 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2083e442f29fSKonstantin Belousov 	td->td_ru.ru_nsignals++;
2084e442f29fSKonstantin Belousov 	mask = ps->ps_catchmask[_SIG_IDX(sig)];
2085e442f29fSKonstantin Belousov 	if (!SIGISMEMBER(ps->ps_signodefer, sig))
2086e442f29fSKonstantin Belousov 		SIGADDSET(mask, sig);
2087e442f29fSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2088e442f29fSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2089e442f29fSKonstantin Belousov 	if (SIGISMEMBER(ps->ps_sigreset, sig))
2090e442f29fSKonstantin Belousov 		sigdflt(ps, sig);
2091e442f29fSKonstantin Belousov }
2092e442f29fSKonstantin Belousov 
2093df8bae1dSRodney W. Grimes /*
20940c14ff0eSRobert Watson  * Send a signal caused by a trap to the current thread.  If it will be
20950c14ff0eSRobert Watson  * caught immediately, deliver it with correct code.  Otherwise, post it
20960c14ff0eSRobert Watson  * normally.
2097df8bae1dSRodney W. Grimes  */
2098df8bae1dSRodney W. Grimes void
20999104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi)
2100df8bae1dSRodney W. Grimes {
21011bf4700bSJeff Roberson 	struct sigacts *ps;
21021bf4700bSJeff Roberson 	struct proc *p;
2103146fc63fSKonstantin Belousov 	sigset_t sigmask;
2104c5c981d4SMateusz Guzik 	int sig;
21051bf4700bSJeff Roberson 
21061bf4700bSJeff Roberson 	p = td->td_proc;
21079104847fSDavid Xu 	sig = ksi->ksi_signo;
21089104847fSDavid Xu 	KASSERT(_SIG_VALID(sig), ("invalid signal"));
21099104847fSDavid Xu 
2110a113b17fSKonstantin Belousov 	sigfastblock_fetch(td);
2111628d2653SJohn Baldwin 	PROC_LOCK(p);
2112ef3dab76STim J. Robbins 	ps = p->p_sigacts;
211390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2114146fc63fSKonstantin Belousov 	sigmask = td->td_sigmask;
2115146fc63fSKonstantin Belousov 	if (td->td_sigblock_val != 0)
2116146fc63fSKonstantin Belousov 		SIGSETOR(sigmask, fastblock_mask);
211790af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2118146fc63fSKonstantin Belousov 	    !SIGISMEMBER(sigmask, sig)) {
2119df8bae1dSRodney W. Grimes #ifdef KTRACE
2120374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
2121374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2122c5c981d4SMateusz Guzik 			    &td->td_sigmask, ksi->ksi_code);
2123df8bae1dSRodney W. Grimes #endif
21249104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
21259104847fSDavid Xu 		    ksi, &td->td_sigmask);
2126e442f29fSKonstantin Belousov 		postsig_done(sig, td, ps);
212790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
21286f841fb7SMarcel Moolenaar 	} else {
2129f71a882fSDavid Xu 		/*
2130f71a882fSDavid Xu 		 * Avoid a possible infinite loop if the thread
2131f71a882fSDavid Xu 		 * masking the signal or process is ignoring the
2132f71a882fSDavid Xu 		 * signal.
2133f71a882fSDavid Xu 		 */
2134146fc63fSKonstantin Belousov 		if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2135f71a882fSDavid Xu 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2136f71a882fSDavid Xu 			SIGDELSET(td->td_sigmask, sig);
2137f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigcatch, sig);
2138f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigignore, sig);
2139f71a882fSDavid Xu 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2140146fc63fSKonstantin Belousov 			td->td_pflags &= ~TDP_SIGFASTBLOCK;
2141146fc63fSKonstantin Belousov 			td->td_sigblock_val = 0;
2142f71a882fSDavid Xu 		}
214390af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
21442c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
2145ad6eec7bSJohn Baldwin 		tdsendsignal(p, td, sig, ksi);
2146df8bae1dSRodney W. Grimes 	}
2147628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2148df8bae1dSRodney W. Grimes }
2149df8bae1dSRodney W. Grimes 
21504093529dSJeff Roberson static struct thread *
2151146fc63fSKonstantin Belousov sigtd(struct proc *p, int sig, bool fast_sigblock)
21524093529dSJeff Roberson {
21533074d1b4SDavid Xu 	struct thread *td, *signal_td;
21544093529dSJeff Roberson 
21554093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
2156146fc63fSKonstantin Belousov 	MPASS(!fast_sigblock || p == curproc);
21574093529dSJeff Roberson 
21584093529dSJeff Roberson 	/*
2159627451c1SDavid Xu 	 * Check if current thread can handle the signal without
216015b7a831SKonstantin Belousov 	 * switching context to another thread.
21614093529dSJeff Roberson 	 */
2162146fc63fSKonstantin Belousov 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2163146fc63fSKonstantin Belousov 	    (!fast_sigblock || curthread->td_sigblock_val == 0))
2164627451c1SDavid Xu 		return (curthread);
21653074d1b4SDavid Xu 	signal_td = NULL;
21663074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
2167146fc63fSKonstantin Belousov 		if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2168146fc63fSKonstantin Belousov 		    td != curthread || td->td_sigblock_val == 0)) {
21693074d1b4SDavid Xu 			signal_td = td;
2170627451c1SDavid Xu 			break;
21713074d1b4SDavid Xu 		}
21723074d1b4SDavid Xu 	}
21733074d1b4SDavid Xu 	if (signal_td == NULL)
21743074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
21753074d1b4SDavid Xu 	return (signal_td);
21764093529dSJeff Roberson }
21774093529dSJeff Roberson 
2178df8bae1dSRodney W. Grimes /*
2179df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
2180df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
2181df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
2182df8bae1dSRodney W. Grimes  *
2183df8bae1dSRodney W. Grimes  * Exceptions:
2184df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
2185df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
2186df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
2187df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
2188df8bae1dSRodney W. Grimes  *
2189df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
21904dec0e67SRobert Watson  *
21914dec0e67SRobert Watson  * NB: This function may be entered from the debugger via the "kill" DDB
21924dec0e67SRobert Watson  * command.  There is little that can be done to mitigate the possibly messy
21934dec0e67SRobert Watson  * side effects of this unwise possibility.
2194df8bae1dSRodney W. Grimes  */
2195df8bae1dSRodney W. Grimes void
21968451d0ddSKip Macy kern_psignal(struct proc *p, int sig)
2197df8bae1dSRodney W. Grimes {
2198a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
2199a3de221dSKonstantin Belousov 
2200a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
2201a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
2202a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
2203ad6eec7bSJohn Baldwin 	(void) tdsendsignal(p, NULL, sig, &ksi);
2204a3de221dSKonstantin Belousov }
2205a3de221dSKonstantin Belousov 
2206ad6eec7bSJohn Baldwin int
2207a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2208a3de221dSKonstantin Belousov {
2209a3de221dSKonstantin Belousov 
2210ad6eec7bSJohn Baldwin 	return (tdsendsignal(p, NULL, sig, ksi));
22119104847fSDavid Xu }
22129104847fSDavid Xu 
2213cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */
22149104847fSDavid Xu int
2215cf7d9a8cSDavid Xu sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
22169104847fSDavid Xu {
2217cf7d9a8cSDavid Xu 	struct thread *td;
221841b3077aSJacques Vidrine 
22196d7b314bSDavid Xu 	if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2220cf7d9a8cSDavid Xu 		td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
22216d7b314bSDavid Xu 		if (td == NULL)
22226d7b314bSDavid Xu 			return (ESRCH);
2223cf7d9a8cSDavid Xu 		*ttd = td;
2224cf7d9a8cSDavid Xu 	} else {
2225cf7d9a8cSDavid Xu 		*ttd = NULL;
2226cf7d9a8cSDavid Xu 		PROC_LOCK(p);
22276d7b314bSDavid Xu 	}
2228cf7d9a8cSDavid Xu 	return (0);
22294093529dSJeff Roberson }
22304093529dSJeff Roberson 
2231ad6eec7bSJohn Baldwin void
2232ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig)
2233ad6eec7bSJohn Baldwin {
2234ad6eec7bSJohn Baldwin 	ksiginfo_t ksi;
2235ad6eec7bSJohn Baldwin 
2236ad6eec7bSJohn Baldwin 	ksiginfo_init(&ksi);
2237ad6eec7bSJohn Baldwin 	ksi.ksi_signo = sig;
2238ad6eec7bSJohn Baldwin 	ksi.ksi_code = SI_KERNEL;
2239ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, &ksi);
2240ad6eec7bSJohn Baldwin }
2241ad6eec7bSJohn Baldwin 
2242ad6eec7bSJohn Baldwin void
2243ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2244ad6eec7bSJohn Baldwin {
2245ad6eec7bSJohn Baldwin 
2246ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, ksi);
2247ad6eec7bSJohn Baldwin }
2248ad6eec7bSJohn Baldwin 
22499b86d3e5SKonstantin Belousov static int
22509b86d3e5SKonstantin Belousov sig_sleepq_abort(struct thread *td, int intrval)
22519b86d3e5SKonstantin Belousov {
22529b86d3e5SKonstantin Belousov 	THREAD_LOCK_ASSERT(td, MA_OWNED);
22539b86d3e5SKonstantin Belousov 
22549b86d3e5SKonstantin Belousov 	if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
22559b86d3e5SKonstantin Belousov 		thread_unlock(td);
22569b86d3e5SKonstantin Belousov 		return (0);
22579b86d3e5SKonstantin Belousov 	}
22589b86d3e5SKonstantin Belousov 	return (sleepq_abort(td, intrval));
22599b86d3e5SKonstantin Belousov }
22609b86d3e5SKonstantin Belousov 
2261cf7d9a8cSDavid Xu int
2262ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
22634093529dSJeff Roberson {
22649104847fSDavid Xu 	sig_t action;
22659104847fSDavid Xu 	sigqueue_t *sigqueue;
22669104847fSDavid Xu 	int prop;
226790af4afaSJohn Baldwin 	struct sigacts *ps;
226894f0972bSDavid Xu 	int intrval;
22699104847fSDavid Xu 	int ret = 0;
2270da7bbd2cSJohn Baldwin 	int wakeup_swapper;
2271df8bae1dSRodney W. Grimes 
2272cf7d9a8cSDavid Xu 	MPASS(td == NULL || p == td->td_proc);
22736d7b314bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
22746d7b314bSDavid Xu 
227541b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
2276212bc4b3SDavid Xu 		panic("%s(): invalid signal %d", __func__, sig);
22774093529dSJeff Roberson 
2278212bc4b3SDavid Xu 	KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
22796d7b314bSDavid Xu 
22806d7b314bSDavid Xu 	/*
22816d7b314bSDavid Xu 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
22826d7b314bSDavid Xu 	 */
22836d7b314bSDavid Xu 	if (p->p_state == PRS_ZOMBIE) {
22840a4f2ac3SKonstantin Belousov 		if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
22856d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
22866d7b314bSDavid Xu 		return (ret);
22876d7b314bSDavid Xu 	}
22886d7b314bSDavid Xu 
228990af4afaSJohn Baldwin 	ps = p->p_sigacts;
22909e590ff0SKonstantin Belousov 	KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
22912c42a146SMarcel Moolenaar 	prop = sigprop(sig);
22924093529dSJeff Roberson 
22936d7b314bSDavid Xu 	if (td == NULL) {
2294146fc63fSKonstantin Belousov 		td = sigtd(p, sig, false);
22956d7b314bSDavid Xu 		sigqueue = &p->p_sigqueue;
2296462314b3SMateusz Guzik 	} else
22979104847fSDavid Xu 		sigqueue = &td->td_sigqueue;
22984093529dSJeff Roberson 
229936160958SMark Johnston 	SDT_PROBE3(proc, , , signal__send, td, p, sig);
23005d217f17SJohn Birrell 
2301df8bae1dSRodney W. Grimes 	/*
2302bc387624SKonstantin Belousov 	 * If the signal is being ignored, then we forget about it
2303bc387624SKonstantin Belousov 	 * immediately, except when the target process executes
2304bc387624SKonstantin Belousov 	 * sigwait().  (Note: we don't set SIGCONT in ps_sigignore,
2305bc387624SKonstantin Belousov 	 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2306df8bae1dSRodney W. Grimes 	 */
230790af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
23080fc32899SJohn Baldwin 	if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2309bc387624SKonstantin Belousov 		if (kern_sig_discard_ign &&
2310bc387624SKonstantin Belousov 		    (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
231136160958SMark Johnston 			SDT_PROBE3(proc, , , signal__discard, td, p, sig);
23125d217f17SJohn Birrell 
231390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
23140a4f2ac3SKonstantin Belousov 			if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
23156d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
23169104847fSDavid Xu 			return (ret);
2317bc387624SKonstantin Belousov 		} else {
2318bc387624SKonstantin Belousov 			action = SIG_CATCH;
2319f17eb93dSKonstantin Belousov 			intrval = 0;
232090af4afaSJohn Baldwin 		}
2321f17eb93dSKonstantin Belousov 	} else {
2322f17eb93dSKonstantin Belousov 		if (SIGISMEMBER(td->td_sigmask, sig))
2323df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
232490af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2325df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
2326df8bae1dSRodney W. Grimes 		else
2327df8bae1dSRodney W. Grimes 			action = SIG_DFL;
232894f0972bSDavid Xu 		if (SIGISMEMBER(ps->ps_sigintr, sig))
232994f0972bSDavid Xu 			intrval = EINTR;
233094f0972bSDavid Xu 		else
233194f0972bSDavid Xu 			intrval = ERESTART;
2332f17eb93dSKonstantin Belousov 	}
233390af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
2334df8bae1dSRodney W. Grimes 
2335fd50a707SBrooks Davis 	if (prop & SIGPROP_CONT)
23369104847fSDavid Xu 		sigqueue_delete_stopmask_proc(p);
2337fd50a707SBrooks Davis 	else if (prop & SIGPROP_STOP) {
2338df8bae1dSRodney W. Grimes 		/*
2339df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
2340df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
2341df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
2342df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
2343df8bae1dSRodney W. Grimes 		 */
2344993a1699SKonstantin Belousov 		if ((prop & SIGPROP_TTYSTOP) != 0 &&
23455844bd05SKonstantin Belousov 		    (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2346993a1699SKonstantin Belousov 		    action == SIG_DFL) {
23470a4f2ac3SKonstantin Belousov 			if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
23486d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
23499104847fSDavid Xu 			return (ret);
23506d7b314bSDavid Xu 		}
23519104847fSDavid Xu 		sigqueue_delete_proc(p, SIGCONT);
2352ebceaf6dSDavid Xu 		if (p->p_flag & P_CONTINUED) {
23536933e3c1SJulian Elischer 			p->p_flag &= ~P_CONTINUED;
2354ebceaf6dSDavid Xu 			PROC_LOCK(p->p_pptr);
2355ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
2356ebceaf6dSDavid Xu 			PROC_UNLOCK(p->p_pptr);
2357ebceaf6dSDavid Xu 		}
2358df8bae1dSRodney W. Grimes 	}
23593074d1b4SDavid Xu 
23609104847fSDavid Xu 	ret = sigqueue_add(sigqueue, sig, ksi);
23619104847fSDavid Xu 	if (ret != 0)
23629104847fSDavid Xu 		return (ret);
23636d7b314bSDavid Xu 	signotify(td);
23645312b1c7SDavid Xu 	/*
23655312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
23665312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
23675312b1c7SDavid Xu 	 */
23685312b1c7SDavid Xu 	if (action == SIG_HOLD &&
2369fd50a707SBrooks Davis 	    !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
23709104847fSDavid Xu 		return (ret);
2371b4d33259SEric Badger 
237261a74c5cSJeff Roberson 	wakeup_swapper = 0;
237361a74c5cSJeff Roberson 
237490d75f78SAlfred Perlstein 	/*
2375e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
2376e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
2377e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
23781968f37bSJohn Baldwin 	 * times when processing needs to be done immediately, such as
2379e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
23801968f37bSJohn Baldwin 	 * We try to do the per-process part here.
2381df8bae1dSRodney W. Grimes 	 */
2382e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
23830f14f15bSJohn Baldwin 		KASSERT(!(p->p_flag & P_WEXIT),
23840f14f15bSJohn Baldwin 		    ("signal to stopped but exiting process"));
2385e602ba25SJulian Elischer 		if (sig == SIGKILL) {
2386137cf33dSDavid Xu 			/*
2387137cf33dSDavid Xu 			 * If traced process is already stopped,
2388137cf33dSDavid Xu 			 * then no further action is necessary.
2389137cf33dSDavid Xu 			 */
239083b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
239183b718ebSDavid Xu 				goto out;
2392df8bae1dSRodney W. Grimes 			/*
2393e602ba25SJulian Elischer 			 * SIGKILL sets process running.
2394e602ba25SJulian Elischer 			 * It will die elsewhere.
2395e602ba25SJulian Elischer 			 * All threads must be restarted.
2396df8bae1dSRodney W. Grimes 			 */
2397482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
2398e602ba25SJulian Elischer 			goto runfast;
2399e602ba25SJulian Elischer 		}
2400e602ba25SJulian Elischer 
2401fd50a707SBrooks Davis 		if (prop & SIGPROP_CONT) {
2402137cf33dSDavid Xu 			/*
2403137cf33dSDavid Xu 			 * If traced process is already stopped,
2404137cf33dSDavid Xu 			 * then no further action is necessary.
2405137cf33dSDavid Xu 			 */
240683b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
240783b718ebSDavid Xu 				goto out;
2408e602ba25SJulian Elischer 			/*
2409e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
24109104847fSDavid Xu 			 * process but don't leave the signal in sigqueue as
24111d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
2412e602ba25SJulian Elischer 			 * continue the process and leave the signal in
24139104847fSDavid Xu 			 * sigqueue.  If the process catches SIGCONT, let it
2414e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
2415e602ba25SJulian Elischer 			 * an event, it goes back to run state.
2416e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
2417e602ba25SJulian Elischer 			 */
24181279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
24197b4a950aSDavid Xu 			PROC_SLOCK(p);
2420ebceaf6dSDavid Xu 			if (p->p_numthreads == p->p_suspcount) {
24217b4a950aSDavid Xu 				PROC_SUNLOCK(p);
24226933e3c1SJulian Elischer 				p->p_flag |= P_CONTINUED;
2423b4490c6eSKonstantin Belousov 				p->p_xsig = SIGCONT;
24247f96995eSDavid Xu 				PROC_LOCK(p->p_pptr);
2425ebceaf6dSDavid Xu 				childproc_continued(p);
24267f96995eSDavid Xu 				PROC_UNLOCK(p->p_pptr);
24277b4a950aSDavid Xu 				PROC_SLOCK(p);
2428ebceaf6dSDavid Xu 			}
2429e602ba25SJulian Elischer 			if (action == SIG_DFL) {
2430a54e85fdSJeff Roberson 				thread_unsuspend(p);
24317b4a950aSDavid Xu 				PROC_SUNLOCK(p);
24329104847fSDavid Xu 				sigqueue_delete(sigqueue, sig);
2433dc47fdf1SKonstantin Belousov 				goto out_cont;
2434a54e85fdSJeff Roberson 			}
2435a54e85fdSJeff Roberson 			if (action == SIG_CATCH) {
24368460a577SJohn Birrell 				/*
24378460a577SJohn Birrell 				 * The process wants to catch it so it needs
24388460a577SJohn Birrell 				 * to run at least one thread, but which one?
24398460a577SJohn Birrell 				 */
24407b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2441e602ba25SJulian Elischer 				goto runfast;
2442e602ba25SJulian Elischer 			}
2443e602ba25SJulian Elischer 			/*
2444e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
2445e602ba25SJulian Elischer 			 */
244604774f23SJulian Elischer 			thread_unsuspend(p);
24477b4a950aSDavid Xu 			PROC_SUNLOCK(p);
2448dc47fdf1SKonstantin Belousov 			goto out_cont;
2449e602ba25SJulian Elischer 		}
2450e602ba25SJulian Elischer 
2451fd50a707SBrooks Davis 		if (prop & SIGPROP_STOP) {
2452137cf33dSDavid Xu 			/*
2453137cf33dSDavid Xu 			 * If traced process is already stopped,
2454137cf33dSDavid Xu 			 * then no further action is necessary.
2455137cf33dSDavid Xu 			 */
245683b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
245783b718ebSDavid Xu 				goto out;
2458e602ba25SJulian Elischer 			/*
2459e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
2460e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
246104774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
2462e602ba25SJulian Elischer 			 */
24631279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
24649104847fSDavid Xu 			sigqueue_delete(sigqueue, sig);
2465e602ba25SJulian Elischer 			goto out;
2466e602ba25SJulian Elischer 		}
2467e602ba25SJulian Elischer 
2468e602ba25SJulian Elischer 		/*
2469e602ba25SJulian Elischer 		 * All other kinds of signals:
2470e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
2471e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
2472e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
247304774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
2474e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
2475e602ba25SJulian Elischer 		 */
24767b4a950aSDavid Xu 		PROC_SLOCK(p);
2477a54e85fdSJeff Roberson 		thread_lock(td);
247861a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td))
24799b86d3e5SKonstantin Belousov 			wakeup_swapper = sig_sleepq_abort(td, intrval);
248061a74c5cSJeff Roberson 		else
2481a54e85fdSJeff Roberson 			thread_unlock(td);
24827b4a950aSDavid Xu 		PROC_SUNLOCK(p);
2483df8bae1dSRodney W. Grimes 		goto out;
2484df8bae1dSRodney W. Grimes 		/*
24859a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
24869a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
2487df8bae1dSRodney W. Grimes 		 */
2488e602ba25SJulian Elischer 	} else if (p->p_state == PRS_NORMAL) {
2489ec8297bdSDavid Xu 		if (p->p_flag & P_TRACED || action == SIG_CATCH) {
249094f0972bSDavid Xu 			tdsigwakeup(td, sig, action, intrval);
2491df8bae1dSRodney W. Grimes 			goto out;
249204774f23SJulian Elischer 		}
2493ec8297bdSDavid Xu 
2494ec8297bdSDavid Xu 		MPASS(action == SIG_DFL);
2495ec8297bdSDavid Xu 
2496fd50a707SBrooks Davis 		if (prop & SIGPROP_STOP) {
24970f14f15bSJohn Baldwin 			if (p->p_flag & (P_PPWAIT|P_WEXIT))
249835c32a76SDavid Xu 				goto out;
2499e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
2500b4490c6eSKonstantin Belousov 			p->p_xsig = sig;
25017b4a950aSDavid Xu 			PROC_SLOCK(p);
2502c53fec76SKonstantin Belousov 			wakeup_swapper = sig_suspend_threads(td, p);
25034093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
2504ebceaf6dSDavid Xu 				/*
2505ebceaf6dSDavid Xu 				 * only thread sending signal to another
2506ebceaf6dSDavid Xu 				 * process can reach here, if thread is sending
2507ebceaf6dSDavid Xu 				 * signal to its process, because thread does
2508ebceaf6dSDavid Xu 				 * not suspend itself here, p_numthreads
2509ebceaf6dSDavid Xu 				 * should never be equal to p_suspcount.
2510ebceaf6dSDavid Xu 				 */
2511ebceaf6dSDavid Xu 				thread_stopped(p);
25127b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2513b4490c6eSKonstantin Belousov 				sigqueue_delete_proc(p, p->p_xsig);
25147b4a950aSDavid Xu 			} else
25157b4a950aSDavid Xu 				PROC_SUNLOCK(p);
251635c32a76SDavid Xu 			goto out;
251735c32a76SDavid Xu 		}
2518e602ba25SJulian Elischer 	} else {
2519e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
25209104847fSDavid Xu 		sigqueue_delete(sigqueue, sig);
2521e602ba25SJulian Elischer 		goto out;
2522e602ba25SJulian Elischer 	}
2523e602ba25SJulian Elischer 
2524b40ce416SJulian Elischer 	/*
2525e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
2526e602ba25SJulian Elischer 	 * running threads.
2527b40ce416SJulian Elischer 	 */
2528e602ba25SJulian Elischer runfast:
252994f0972bSDavid Xu 	tdsigwakeup(td, sig, action, intrval);
25307b4a950aSDavid Xu 	PROC_SLOCK(p);
2531e602ba25SJulian Elischer 	thread_unsuspend(p);
25327b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2533dc47fdf1SKonstantin Belousov out_cont:
2534dc47fdf1SKonstantin Belousov 	itimer_proc_continue(p);
25352fd1ffefSKonstantin Belousov 	kqtimer_proc_continue(p);
2536e602ba25SJulian Elischer out:
25377b4a950aSDavid Xu 	/* If we jump here, proc slock should not be owned. */
25387b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
253961a74c5cSJeff Roberson 	if (wakeup_swapper)
254061a74c5cSJeff Roberson 		kick_proc0();
254161a74c5cSJeff Roberson 
25429104847fSDavid Xu 	return (ret);
2543e602ba25SJulian Elischer }
2544e602ba25SJulian Elischer 
2545e602ba25SJulian Elischer /*
2546e602ba25SJulian Elischer  * The force of a signal has been directed against a single
2547e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
2548e602ba25SJulian Elischer  * out of any sleep it may be in etc.
2549e602ba25SJulian Elischer  */
2550e602ba25SJulian Elischer static void
255194f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2552e602ba25SJulian Elischer {
2553e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
255461a74c5cSJeff Roberson 	int prop, wakeup_swapper;
2555e602ba25SJulian Elischer 
25568b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2557e602ba25SJulian Elischer 	prop = sigprop(sig);
2558a4c2da15SBruce Evans 
25597b4a950aSDavid Xu 	PROC_SLOCK(p);
2560374ae2a3SJeff Roberson 	thread_lock(td);
2561e602ba25SJulian Elischer 	/*
2562aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
2563aef68c96SKonstantin Belousov 	 * killed in this lifetime.  Be careful to avoid bumping the
2564aef68c96SKonstantin Belousov 	 * priority of the idle thread, since we still allow to signal
2565aef68c96SKonstantin Belousov 	 * kernel processes.
2566e602ba25SJulian Elischer 	 */
2567fd50a707SBrooks Davis 	if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2568aef68c96SKonstantin Belousov 	    td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2569b3a4fb14SDavid Xu 		sched_prio(td, PUSER);
257080c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
2571e602ba25SJulian Elischer 		/*
2572e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
2573e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
2574e602ba25SJulian Elischer 		 * be noticed when the process returns through
2575e602ba25SJulian Elischer 		 * trap() or syscall().
2576e602ba25SJulian Elischer 		 */
257744f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
2578374ae2a3SJeff Roberson 			goto out;
2579e602ba25SJulian Elischer 		/*
2580e602ba25SJulian Elischer 		 * If SIGCONT is default (or ignored) and process is
2581e602ba25SJulian Elischer 		 * asleep, we are finished; the process should not
2582e602ba25SJulian Elischer 		 * be awakened.
2583df8bae1dSRodney W. Grimes 		 */
2584fd50a707SBrooks Davis 		if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2585a54e85fdSJeff Roberson 			thread_unlock(td);
25867b4a950aSDavid Xu 			PROC_SUNLOCK(p);
25879104847fSDavid Xu 			sigqueue_delete(&p->p_sigqueue, sig);
25884093529dSJeff Roberson 			/*
25894093529dSJeff Roberson 			 * It may be on either list in this state.
25904093529dSJeff Roberson 			 * Remove from both for now.
25914093529dSJeff Roberson 			 */
25929104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
2593aa0fa334SJulian Elischer 			return;
2594df8bae1dSRodney W. Grimes 		}
2595df8bae1dSRodney W. Grimes 
2596aa0fa334SJulian Elischer 		/*
2597a120a7a3SJohn Baldwin 		 * Don't awaken a sleeping thread for SIGSTOP if the
2598a120a7a3SJohn Baldwin 		 * STOP signal is deferred.
2599a120a7a3SJohn Baldwin 		 */
2600fd50a707SBrooks Davis 		if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
26016f56cb8dSKonstantin Belousov 		    TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2602a120a7a3SJohn Baldwin 			goto out;
2603a120a7a3SJohn Baldwin 
2604a120a7a3SJohn Baldwin 		/*
2605a4c2da15SBruce Evans 		 * Give low priority threads a better chance to run.
2606aa0fa334SJulian Elischer 		 */
2607aef68c96SKonstantin Belousov 		if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2608b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
2609ec8297bdSDavid Xu 
26109b86d3e5SKonstantin Belousov 		wakeup_swapper = sig_sleepq_abort(td, intrval);
261161a74c5cSJeff Roberson 		PROC_SUNLOCK(p);
261261a74c5cSJeff Roberson 		if (wakeup_swapper)
261361a74c5cSJeff Roberson 			kick_proc0();
261461a74c5cSJeff Roberson 		return;
261561a74c5cSJeff Roberson 	}
261661a74c5cSJeff Roberson 
2617df8bae1dSRodney W. Grimes 	/*
2618a4c2da15SBruce Evans 	 * Other states do nothing with the signal immediately,
2619df8bae1dSRodney W. Grimes 	 * other than kicking ourselves if we are running.
2620df8bae1dSRodney W. Grimes 	 * It will either never be noticed, or noticed very soon.
2621df8bae1dSRodney W. Grimes 	 */
2622a4c2da15SBruce Evans #ifdef SMP
262344f3b092SJohn Baldwin 	if (TD_IS_RUNNING(td) && td != curthread)
2624e602ba25SJulian Elischer 		forward_signal(td);
26253163861cSTor Egge #endif
262661a74c5cSJeff Roberson 
2627374ae2a3SJeff Roberson out:
26287b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2629374ae2a3SJeff Roberson 	thread_unlock(td);
2630a4c2da15SBruce Evans }
2631df8bae1dSRodney W. Grimes 
263287a64872SKonstantin Belousov static void
263387a64872SKonstantin Belousov ptrace_coredump(struct thread *td)
263487a64872SKonstantin Belousov {
263587a64872SKonstantin Belousov 	struct proc *p;
263687a64872SKonstantin Belousov 	struct thr_coredump_req *tcq;
263787a64872SKonstantin Belousov 	void *rl_cookie;
263887a64872SKonstantin Belousov 
263987a64872SKonstantin Belousov 	MPASS(td == curthread);
264087a64872SKonstantin Belousov 	p = td->td_proc;
264187a64872SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
2642*e6feeae2SKonstantin Belousov 	if ((td->td_dbgflags & TDB_COREDUMPREQ) == 0)
264387a64872SKonstantin Belousov 		return;
264487a64872SKonstantin Belousov 	KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
264587a64872SKonstantin Belousov 
2646*e6feeae2SKonstantin Belousov 	tcq = td->td_remotereq;
2647*e6feeae2SKonstantin Belousov 	KASSERT(tcq != NULL, ("td_remotereq is NULL"));
264887a64872SKonstantin Belousov 
264987a64872SKonstantin Belousov 	if (p->p_sysent->sv_coredump == NULL) {
265087a64872SKonstantin Belousov 		tcq->tc_error = ENOSYS;
265187a64872SKonstantin Belousov 		goto wake;
265287a64872SKonstantin Belousov 	}
265387a64872SKonstantin Belousov 
265487a64872SKonstantin Belousov 	PROC_UNLOCK(p);
265587a64872SKonstantin Belousov 	rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
265687a64872SKonstantin Belousov 
265787a64872SKonstantin Belousov 	tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
265887a64872SKonstantin Belousov 	    tcq->tc_limit, tcq->tc_flags);
265987a64872SKonstantin Belousov 
266087a64872SKonstantin Belousov 	vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
266187a64872SKonstantin Belousov 	PROC_LOCK(p);
266287a64872SKonstantin Belousov wake:
2663*e6feeae2SKonstantin Belousov 	td->td_dbgflags &= ~TDB_COREDUMPREQ;
2664*e6feeae2SKonstantin Belousov 	td->td_remotereq = NULL;
266587a64872SKonstantin Belousov 	wakeup(p);
266687a64872SKonstantin Belousov }
266787a64872SKonstantin Belousov 
26686f56cb8dSKonstantin Belousov static int
2669c53fec76SKonstantin Belousov sig_suspend_threads(struct thread *td, struct proc *p)
2670d8267df7SDavid Xu {
2671d8267df7SDavid Xu 	struct thread *td2;
26726f56cb8dSKonstantin Belousov 	int wakeup_swapper;
2673d8267df7SDavid Xu 
2674d8267df7SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
26757b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
2676d8267df7SDavid Xu 
26776f56cb8dSKonstantin Belousov 	wakeup_swapper = 0;
2678d8267df7SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td2) {
2679a54e85fdSJeff Roberson 		thread_lock(td2);
2680c6d31b83SKonstantin Belousov 		ast_sched_locked(td2, TDA_SUSPEND);
2681d8267df7SDavid Xu 		if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2682f33a947bSKonstantin Belousov 		    (td2->td_flags & TDF_SINTR)) {
2683f33a947bSKonstantin Belousov 			if (td2->td_flags & TDF_SBDRY) {
2684a120a7a3SJohn Baldwin 				/*
2685a120a7a3SJohn Baldwin 				 * Once a thread is asleep with
26866f56cb8dSKonstantin Belousov 				 * TDF_SBDRY and without TDF_SERESTART
26876f56cb8dSKonstantin Belousov 				 * or TDF_SEINTR set, it should never
2688a120a7a3SJohn Baldwin 				 * become suspended due to this check.
2689a120a7a3SJohn Baldwin 				 */
2690a120a7a3SJohn Baldwin 				KASSERT(!TD_IS_SUSPENDED(td2),
2691a120a7a3SJohn Baldwin 				    ("thread with deferred stops suspended"));
269261a74c5cSJeff Roberson 				if (TD_SBDRY_INTR(td2)) {
269346e47c4fSKonstantin Belousov 					wakeup_swapper |= sleepq_abort(td2,
269446e47c4fSKonstantin Belousov 					    TD_SBDRY_ERRNO(td2));
269561a74c5cSJeff Roberson 					continue;
2696f33a947bSKonstantin Belousov 				}
269761a74c5cSJeff Roberson 			} else if (!TD_IS_SUSPENDED(td2))
269861a74c5cSJeff Roberson 				thread_suspend_one(td2);
2699f33a947bSKonstantin Belousov 		} else if (!TD_IS_SUSPENDED(td2)) {
2700d8267df7SDavid Xu #ifdef SMP
2701d8267df7SDavid Xu 			if (TD_IS_RUNNING(td2) && td2 != td)
2702d8267df7SDavid Xu 				forward_signal(td2);
2703d8267df7SDavid Xu #endif
2704d8267df7SDavid Xu 		}
2705a54e85fdSJeff Roberson 		thread_unlock(td2);
2706d8267df7SDavid Xu 	}
27076f56cb8dSKonstantin Belousov 	return (wakeup_swapper);
2708d8267df7SDavid Xu }
2709d8267df7SDavid Xu 
271082a4538fSEric Badger /*
271182a4538fSEric Badger  * Stop the process for an event deemed interesting to the debugger. If si is
271282a4538fSEric Badger  * non-NULL, this is a signal exchange; the new signal requested by the
271382a4538fSEric Badger  * debugger will be returned for handling. If si is NULL, this is some other
271482a4538fSEric Badger  * type of interesting event. The debugger may request a signal be delivered in
271582a4538fSEric Badger  * that case as well, however it will be deferred until it can be handled.
271682a4538fSEric Badger  */
2717cbf4e354SDavid Xu int
271882a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si)
27194cc9f52fSRobert Drehmel {
27204cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
272182a4538fSEric Badger 	struct thread *td2;
272282a4538fSEric Badger 	ksiginfo_t ksi;
27234cc9f52fSRobert Drehmel 
272430a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
27250f14f15bSJohn Baldwin 	KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
27264cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2727aa89d8cdSJohn Baldwin 	    &p->p_mtx.lock_object, "Stopping for traced signal");
27284cc9f52fSRobert Drehmel 
2729cbf4e354SDavid Xu 	td->td_xsig = sig;
273082a4538fSEric Badger 
273182a4538fSEric Badger 	if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
273282a4538fSEric Badger 		td->td_dbgflags |= TDB_XSIG;
2733515b7a0bSJohn Baldwin 		CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2734515b7a0bSJohn Baldwin 		    td->td_tid, p->p_pid, td->td_dbgflags, sig);
27357b4a950aSDavid Xu 		PROC_SLOCK(p);
2736904c5ec4SDavid Xu 		while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
273782a4538fSEric Badger 			if (P_KILLED(p)) {
273882a4538fSEric Badger 				/*
273982a4538fSEric Badger 				 * Ensure that, if we've been PT_KILLed, the
274082a4538fSEric Badger 				 * exit status reflects that. Another thread
274182a4538fSEric Badger 				 * may also be in ptracestop(), having just
274282a4538fSEric Badger 				 * received the SIGKILL, but this thread was
274382a4538fSEric Badger 				 * unsuspended first.
274482a4538fSEric Badger 				 */
274582a4538fSEric Badger 				td->td_dbgflags &= ~TDB_XSIG;
274682a4538fSEric Badger 				td->td_xsig = SIGKILL;
274782a4538fSEric Badger 				p->p_ptevents = 0;
274882a4538fSEric Badger 				break;
274982a4538fSEric Badger 			}
27505fcfab6eSJohn Baldwin 			if (p->p_flag & P_SINGLE_EXIT &&
27515fcfab6eSJohn Baldwin 			    !(td->td_dbgflags & TDB_EXIT)) {
27525fcfab6eSJohn Baldwin 				/*
27535fcfab6eSJohn Baldwin 				 * Ignore ptrace stops except for thread exit
27545fcfab6eSJohn Baldwin 				 * events when the process exits.
27555fcfab6eSJohn Baldwin 				 */
2756904c5ec4SDavid Xu 				td->td_dbgflags &= ~TDB_XSIG;
27577b4a950aSDavid Xu 				PROC_SUNLOCK(p);
275882a4538fSEric Badger 				return (0);
2759cbf4e354SDavid Xu 			}
2760b7a25e63SKonstantin Belousov 
2761cbf4e354SDavid Xu 			/*
2762b7a25e63SKonstantin Belousov 			 * Make wait(2) work.  Ensure that right after the
2763b7a25e63SKonstantin Belousov 			 * attach, the thread which was decided to become the
2764b7a25e63SKonstantin Belousov 			 * leader of attach gets reported to the waiter.
2765b7a25e63SKonstantin Belousov 			 * Otherwise, just avoid overwriting another thread's
2766b7a25e63SKonstantin Belousov 			 * assignment to p_xthread.  If another thread has
2767b7a25e63SKonstantin Belousov 			 * already set p_xthread, the current thread will get
2768b7a25e63SKonstantin Belousov 			 * a chance to report itself upon the next iteration.
2769cbf4e354SDavid Xu 			 */
2770b7a25e63SKonstantin Belousov 			if ((td->td_dbgflags & TDB_FSTP) != 0 ||
27717f649ddaSMark Johnston 			    ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2772b7a25e63SKonstantin Belousov 			    p->p_xthread == NULL)) {
2773b4490c6eSKonstantin Belousov 				p->p_xsig = sig;
2774cbf4e354SDavid Xu 				p->p_xthread = td;
2775ab74c843SKonstantin Belousov 
2776ab74c843SKonstantin Belousov 				/*
2777ab74c843SKonstantin Belousov 				 * If we are on sleepqueue already,
2778ab74c843SKonstantin Belousov 				 * let sleepqueue code decide if it
2779ab74c843SKonstantin Belousov 				 * needs to go sleep after attach.
2780ab74c843SKonstantin Belousov 				 */
2781ab74c843SKonstantin Belousov 				if (td->td_wchan == NULL)
2782b7a25e63SKonstantin Belousov 					td->td_dbgflags &= ~TDB_FSTP;
2783ab74c843SKonstantin Belousov 
2784b7a25e63SKonstantin Belousov 				p->p_flag2 &= ~P2_PTRACE_FSTP;
2785b7a25e63SKonstantin Belousov 				p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2786c53fec76SKonstantin Belousov 				sig_suspend_threads(td, p);
2787b7a25e63SKonstantin Belousov 			}
27886fa39a73SKonstantin Belousov 			if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
27896fa39a73SKonstantin Belousov 				td->td_dbgflags &= ~TDB_STOPATFORK;
27906fa39a73SKonstantin Belousov 			}
2791cbf4e354SDavid Xu stopme:
279268d311b6SKonstantin Belousov 			td->td_dbgflags |= TDB_SSWITCH;
27936ddcc233SKonstantin Belousov 			thread_suspend_switch(td, p);
279468d311b6SKonstantin Belousov 			td->td_dbgflags &= ~TDB_SSWITCH;
2795*e6feeae2SKonstantin Belousov 			if ((td->td_dbgflags & TDB_COREDUMPREQ) != 0) {
279687a64872SKonstantin Belousov 				PROC_SUNLOCK(p);
279787a64872SKonstantin Belousov 				ptrace_coredump(td);
279887a64872SKonstantin Belousov 				PROC_SLOCK(p);
279987a64872SKonstantin Belousov 				goto stopme;
280087a64872SKonstantin Belousov 			}
28017ce60f60SDavid Xu 			if (p->p_xthread == td)
28027ce60f60SDavid Xu 				p->p_xthread = NULL;
28037ce60f60SDavid Xu 			if (!(p->p_flag & P_TRACED))
2804cbf4e354SDavid Xu 				break;
2805904c5ec4SDavid Xu 			if (td->td_dbgflags & TDB_SUSPEND) {
2806cbf4e354SDavid Xu 				if (p->p_flag & P_SINGLE_EXIT)
2807cbf4e354SDavid Xu 					break;
2808cbf4e354SDavid Xu 				goto stopme;
2809cbf4e354SDavid Xu 			}
2810cbf4e354SDavid Xu 		}
28117b4a950aSDavid Xu 		PROC_SUNLOCK(p);
281282a4538fSEric Badger 	}
281382a4538fSEric Badger 
281482a4538fSEric Badger 	if (si != NULL && sig == td->td_xsig) {
281582a4538fSEric Badger 		/* Parent wants us to take the original signal unchanged. */
281682a4538fSEric Badger 		si->ksi_flags |= KSI_HEAD;
281782a4538fSEric Badger 		if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
281882a4538fSEric Badger 			si->ksi_signo = 0;
281982a4538fSEric Badger 	} else if (td->td_xsig != 0) {
282082a4538fSEric Badger 		/*
282182a4538fSEric Badger 		 * If parent wants us to take a new signal, then it will leave
282282a4538fSEric Badger 		 * it in td->td_xsig; otherwise we just look for signals again.
282382a4538fSEric Badger 		 */
282482a4538fSEric Badger 		ksiginfo_init(&ksi);
282582a4538fSEric Badger 		ksi.ksi_signo = td->td_xsig;
282682a4538fSEric Badger 		ksi.ksi_flags |= KSI_PTRACE;
2827146fc63fSKonstantin Belousov 		td2 = sigtd(p, td->td_xsig, false);
282882a4538fSEric Badger 		tdsendsignal(p, td2, td->td_xsig, &ksi);
282982a4538fSEric Badger 		if (td != td2)
283082a4538fSEric Badger 			return (0);
283182a4538fSEric Badger 	}
283282a4538fSEric Badger 
2833cbf4e354SDavid Xu 	return (td->td_xsig);
28344cc9f52fSRobert Drehmel }
28354cc9f52fSRobert Drehmel 
28360bc52b0bSKonstantin Belousov static void
283780a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags)
28386b286ee8SKonstantin Belousov {
28396b286ee8SKonstantin Belousov 	struct sigacts *ps;
28406b286ee8SKonstantin Belousov 	struct thread *td;
2841407af02bSDavid Xu 	int sig;
2842146fc63fSKonstantin Belousov 	bool fastblk, pslocked;
28436b286ee8SKonstantin Belousov 
28446b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
284570778bbaSKonstantin Belousov 	ps = p->p_sigacts;
2846146fc63fSKonstantin Belousov 	pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2847146fc63fSKonstantin Belousov 	mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2848407af02bSDavid Xu 	if (SIGISEMPTY(p->p_siglist))
2849407af02bSDavid Xu 		return;
2850407af02bSDavid Xu 	SIGSETAND(block, p->p_siglist);
2851146fc63fSKonstantin Belousov 	fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
285281f2e906SMark Johnston 	SIG_FOREACH(sig, &block) {
2853146fc63fSKonstantin Belousov 		td = sigtd(p, sig, fastblk);
2854146fc63fSKonstantin Belousov 
2855146fc63fSKonstantin Belousov 		/*
2856146fc63fSKonstantin Belousov 		 * If sigtd() selected us despite sigfastblock is
2857146fc63fSKonstantin Belousov 		 * blocking, do not activate AST or wake us, to avoid
2858146fc63fSKonstantin Belousov 		 * loop in AST handler.
2859146fc63fSKonstantin Belousov 		 */
2860146fc63fSKonstantin Belousov 		if (fastblk && td == curthread)
2861146fc63fSKonstantin Belousov 			continue;
2862146fc63fSKonstantin Belousov 
28636b286ee8SKonstantin Belousov 		signotify(td);
2864146fc63fSKonstantin Belousov 		if (!pslocked)
28656b286ee8SKonstantin Belousov 			mtx_lock(&ps->ps_mtx);
2866396a0d44SKonstantin Belousov 		if (p->p_flag & P_TRACED ||
2867396a0d44SKonstantin Belousov 		    (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2868146fc63fSKonstantin Belousov 		    !SIGISMEMBER(td->td_sigmask, sig))) {
2869407af02bSDavid Xu 			tdsigwakeup(td, sig, SIG_CATCH,
2870407af02bSDavid Xu 			    (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
28716b286ee8SKonstantin Belousov 			    ERESTART));
2872146fc63fSKonstantin Belousov 		}
2873146fc63fSKonstantin Belousov 		if (!pslocked)
28746b286ee8SKonstantin Belousov 			mtx_unlock(&ps->ps_mtx);
28756b286ee8SKonstantin Belousov 	}
28766b286ee8SKonstantin Belousov }
28776b286ee8SKonstantin Belousov 
28786b286ee8SKonstantin Belousov void
28796b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td)
28806b286ee8SKonstantin Belousov {
28816b286ee8SKonstantin Belousov 	struct proc *p;
28826b286ee8SKonstantin Belousov 	sigset_t unblocked;
28836b286ee8SKonstantin Belousov 
28846b286ee8SKonstantin Belousov 	p = td->td_proc;
28856b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
28866b286ee8SKonstantin Belousov 
28876b286ee8SKonstantin Belousov 	sigqueue_flush(&td->td_sigqueue);
28886b286ee8SKonstantin Belousov 	if (p->p_numthreads == 1)
28896b286ee8SKonstantin Belousov 		return;
28906b286ee8SKonstantin Belousov 
28916b286ee8SKonstantin Belousov 	/*
28926b286ee8SKonstantin Belousov 	 * Since we cannot handle signals, notify signal post code
28936b286ee8SKonstantin Belousov 	 * about this by filling the sigmask.
28946b286ee8SKonstantin Belousov 	 *
28956b286ee8SKonstantin Belousov 	 * Also, if needed, wake up thread(s) that do not block the
28966b286ee8SKonstantin Belousov 	 * same signals as the exiting thread, since the thread might
28976b286ee8SKonstantin Belousov 	 * have been selected for delivery and woken up.
28986b286ee8SKonstantin Belousov 	 */
28996b286ee8SKonstantin Belousov 	SIGFILLSET(unblocked);
29006b286ee8SKonstantin Belousov 	SIGSETNAND(unblocked, td->td_sigmask);
29016b286ee8SKonstantin Belousov 	SIGFILLSET(td->td_sigmask);
290280a8b0f3SKonstantin Belousov 	reschedule_signals(p, unblocked, 0);
29036b286ee8SKonstantin Belousov 
29046b286ee8SKonstantin Belousov }
29056b286ee8SKonstantin Belousov 
29063a1e5dd8SKonstantin Belousov static int
29073a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags)
2908a120a7a3SJohn Baldwin {
2909a120a7a3SJohn Baldwin 
29103a1e5dd8SKonstantin Belousov 	MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
29113a1e5dd8SKonstantin Belousov 	    (cflags & TDF_SBDRY) != 0);
29123a1e5dd8SKonstantin Belousov 	return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2913a120a7a3SJohn Baldwin }
2914a120a7a3SJohn Baldwin 
2915a120a7a3SJohn Baldwin /*
29163a1e5dd8SKonstantin Belousov  * Defer the delivery of SIGSTOP for the current thread, according to
29173a1e5dd8SKonstantin Belousov  * the requested mode.  Returns previous flags, which must be restored
29183a1e5dd8SKonstantin Belousov  * by sigallowstop().
29193a1e5dd8SKonstantin Belousov  *
29203a1e5dd8SKonstantin Belousov  * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
29213a1e5dd8SKonstantin Belousov  * cleared by the current thread, which allow the lock-less read-only
29223a1e5dd8SKonstantin Belousov  * accesses below.
2923a120a7a3SJohn Baldwin  */
2924e3612a4cSKonstantin Belousov int
292546e47c4fSKonstantin Belousov sigdeferstop_impl(int mode)
2926a120a7a3SJohn Baldwin {
2927593efaf9SJohn Baldwin 	struct thread *td;
29283a1e5dd8SKonstantin Belousov 	int cflags, nflags;
2929a120a7a3SJohn Baldwin 
2930593efaf9SJohn Baldwin 	td = curthread;
29313a1e5dd8SKonstantin Belousov 	cflags = sigdeferstop_curr_flags(td->td_flags);
29323a1e5dd8SKonstantin Belousov 	switch (mode) {
29333a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_NOP:
29343a1e5dd8SKonstantin Belousov 		nflags = cflags;
29353a1e5dd8SKonstantin Belousov 		break;
29363a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_OFF:
29373a1e5dd8SKonstantin Belousov 		nflags = 0;
29383a1e5dd8SKonstantin Belousov 		break;
29393a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_SILENT:
29403a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
29413a1e5dd8SKonstantin Belousov 		break;
29423a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_EINTR:
29433a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
29443a1e5dd8SKonstantin Belousov 		break;
29453a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_ERESTART:
29463a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
29473a1e5dd8SKonstantin Belousov 		break;
29483a1e5dd8SKonstantin Belousov 	default:
29493a1e5dd8SKonstantin Belousov 		panic("sigdeferstop: invalid mode %x", mode);
29503a1e5dd8SKonstantin Belousov 		break;
29513a1e5dd8SKonstantin Belousov 	}
295246e47c4fSKonstantin Belousov 	if (cflags == nflags)
295346e47c4fSKonstantin Belousov 		return (SIGDEFERSTOP_VAL_NCHG);
2954a120a7a3SJohn Baldwin 	thread_lock(td);
29553a1e5dd8SKonstantin Belousov 	td->td_flags = (td->td_flags & ~cflags) | nflags;
2956a120a7a3SJohn Baldwin 	thread_unlock(td);
29573a1e5dd8SKonstantin Belousov 	return (cflags);
29583a1e5dd8SKonstantin Belousov }
29593a1e5dd8SKonstantin Belousov 
29603a1e5dd8SKonstantin Belousov /*
29613a1e5dd8SKonstantin Belousov  * Restores the STOP handling mode, typically permitting the delivery
29623a1e5dd8SKonstantin Belousov  * of SIGSTOP for the current thread.  This does not immediately
29633a1e5dd8SKonstantin Belousov  * suspend if a stop was posted.  Instead, the thread will suspend
29643a1e5dd8SKonstantin Belousov  * either via ast() or a subsequent interruptible sleep.
29653a1e5dd8SKonstantin Belousov  */
29663a1e5dd8SKonstantin Belousov void
296746e47c4fSKonstantin Belousov sigallowstop_impl(int prev)
29683a1e5dd8SKonstantin Belousov {
29693a1e5dd8SKonstantin Belousov 	struct thread *td;
29703a1e5dd8SKonstantin Belousov 	int cflags;
29713a1e5dd8SKonstantin Belousov 
297246e47c4fSKonstantin Belousov 	KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
29733a1e5dd8SKonstantin Belousov 	KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
29743a1e5dd8SKonstantin Belousov 	    ("sigallowstop: incorrect previous mode %x", prev));
29753a1e5dd8SKonstantin Belousov 	td = curthread;
29763a1e5dd8SKonstantin Belousov 	cflags = sigdeferstop_curr_flags(td->td_flags);
29773a1e5dd8SKonstantin Belousov 	if (cflags != prev) {
29783a1e5dd8SKonstantin Belousov 		thread_lock(td);
29793a1e5dd8SKonstantin Belousov 		td->td_flags = (td->td_flags & ~cflags) | prev;
29803a1e5dd8SKonstantin Belousov 		thread_unlock(td);
29813a1e5dd8SKonstantin Belousov 	}
2982a120a7a3SJohn Baldwin }
2983a120a7a3SJohn Baldwin 
298481f2e906SMark Johnston enum sigstatus {
298581f2e906SMark Johnston 	SIGSTATUS_HANDLE,
298681f2e906SMark Johnston 	SIGSTATUS_HANDLED,
298781f2e906SMark Johnston 	SIGSTATUS_IGNORE,
298881f2e906SMark Johnston 	SIGSTATUS_SBDRY_STOP,
298981f2e906SMark Johnston };
299081f2e906SMark Johnston 
299181f2e906SMark Johnston /*
299281f2e906SMark Johnston  * The thread has signal "sig" pending.  Figure out what to do with it:
299381f2e906SMark Johnston  *
299481f2e906SMark Johnston  * _HANDLE     -> the caller should handle the signal
299581f2e906SMark Johnston  * _HANDLED    -> handled internally, reload pending signal set
299681f2e906SMark Johnston  * _IGNORE     -> ignored, remove from the set of pending signals and try the
299781f2e906SMark Johnston  *                next pending signal
299881f2e906SMark Johnston  * _SBDRY_STOP -> the signal should stop the thread but this is not
299981f2e906SMark Johnston  *                permitted in the current context
300081f2e906SMark Johnston  */
300181f2e906SMark Johnston static enum sigstatus
300281f2e906SMark Johnston sigprocess(struct thread *td, int sig)
300381f2e906SMark Johnston {
300481f2e906SMark Johnston 	struct proc *p;
300581f2e906SMark Johnston 	struct sigacts *ps;
300681f2e906SMark Johnston 	struct sigqueue *queue;
300781f2e906SMark Johnston 	ksiginfo_t ksi;
300881f2e906SMark Johnston 	int prop;
300981f2e906SMark Johnston 
301081f2e906SMark Johnston 	KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
301181f2e906SMark Johnston 
301281f2e906SMark Johnston 	p = td->td_proc;
301381f2e906SMark Johnston 	ps = p->p_sigacts;
301481f2e906SMark Johnston 	mtx_assert(&ps->ps_mtx, MA_OWNED);
301581f2e906SMark Johnston 	PROC_LOCK_ASSERT(p, MA_OWNED);
301681f2e906SMark Johnston 
301781f2e906SMark Johnston 	/*
301881f2e906SMark Johnston 	 * We should allow pending but ignored signals below
301902a2aacbSKonstantin Belousov 	 * if there is sigwait() active, or P_TRACED was
302081f2e906SMark Johnston 	 * on when they were posted.
302181f2e906SMark Johnston 	 */
302281f2e906SMark Johnston 	if (SIGISMEMBER(ps->ps_sigignore, sig) &&
302381f2e906SMark Johnston 	    (p->p_flag & P_TRACED) == 0 &&
302481f2e906SMark Johnston 	    (td->td_flags & TDF_SIGWAIT) == 0) {
302581f2e906SMark Johnston 		return (SIGSTATUS_IGNORE);
302681f2e906SMark Johnston 	}
302781f2e906SMark Johnston 
302802a2aacbSKonstantin Belousov 	/*
302902a2aacbSKonstantin Belousov 	 * If the process is going to single-thread mode to prepare
303002a2aacbSKonstantin Belousov 	 * for exit, there is no sense in delivering any signal
303102a2aacbSKonstantin Belousov 	 * to usermode.  Another important consequence is that
303202a2aacbSKonstantin Belousov 	 * msleep(..., PCATCH, ...) now is only interruptible by a
303302a2aacbSKonstantin Belousov 	 * suspend request.
303402a2aacbSKonstantin Belousov 	 */
303502a2aacbSKonstantin Belousov 	if ((p->p_flag2 & P2_WEXIT) != 0)
303602a2aacbSKonstantin Belousov 		return (SIGSTATUS_IGNORE);
303702a2aacbSKonstantin Belousov 
303881f2e906SMark Johnston 	if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
303981f2e906SMark Johnston 		/*
304081f2e906SMark Johnston 		 * If traced, always stop.
304181f2e906SMark Johnston 		 * Remove old signal from queue before the stop.
304281f2e906SMark Johnston 		 * XXX shrug off debugger, it causes siginfo to
304381f2e906SMark Johnston 		 * be thrown away.
304481f2e906SMark Johnston 		 */
304581f2e906SMark Johnston 		queue = &td->td_sigqueue;
304681f2e906SMark Johnston 		ksiginfo_init(&ksi);
304781f2e906SMark Johnston 		if (sigqueue_get(queue, sig, &ksi) == 0) {
304881f2e906SMark Johnston 			queue = &p->p_sigqueue;
304981f2e906SMark Johnston 			sigqueue_get(queue, sig, &ksi);
305081f2e906SMark Johnston 		}
305181f2e906SMark Johnston 		td->td_si = ksi.ksi_info;
305281f2e906SMark Johnston 
305381f2e906SMark Johnston 		mtx_unlock(&ps->ps_mtx);
305481f2e906SMark Johnston 		sig = ptracestop(td, sig, &ksi);
305581f2e906SMark Johnston 		mtx_lock(&ps->ps_mtx);
305681f2e906SMark Johnston 
305781f2e906SMark Johnston 		td->td_si.si_signo = 0;
305881f2e906SMark Johnston 
305981f2e906SMark Johnston 		/*
306081f2e906SMark Johnston 		 * Keep looking if the debugger discarded or
306181f2e906SMark Johnston 		 * replaced the signal.
306281f2e906SMark Johnston 		 */
306381f2e906SMark Johnston 		if (sig == 0)
306481f2e906SMark Johnston 			return (SIGSTATUS_HANDLED);
306581f2e906SMark Johnston 
306681f2e906SMark Johnston 		/*
306781f2e906SMark Johnston 		 * If the signal became masked, re-queue it.
306881f2e906SMark Johnston 		 */
306981f2e906SMark Johnston 		if (SIGISMEMBER(td->td_sigmask, sig)) {
307081f2e906SMark Johnston 			ksi.ksi_flags |= KSI_HEAD;
307181f2e906SMark Johnston 			sigqueue_add(&p->p_sigqueue, sig, &ksi);
307281f2e906SMark Johnston 			return (SIGSTATUS_HANDLED);
307381f2e906SMark Johnston 		}
307481f2e906SMark Johnston 
307581f2e906SMark Johnston 		/*
307681f2e906SMark Johnston 		 * If the traced bit got turned off, requeue the signal and
307781f2e906SMark Johnston 		 * reload the set of pending signals.  This ensures that p_sig*
307881f2e906SMark Johnston 		 * and p_sigact are consistent.
307981f2e906SMark Johnston 		 */
308081f2e906SMark Johnston 		if ((p->p_flag & P_TRACED) == 0) {
3081a24afbb4SKonstantin Belousov 			if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
308281f2e906SMark Johnston 				ksi.ksi_flags |= KSI_HEAD;
308381f2e906SMark Johnston 				sigqueue_add(queue, sig, &ksi);
3084a24afbb4SKonstantin Belousov 			}
308581f2e906SMark Johnston 			return (SIGSTATUS_HANDLED);
308681f2e906SMark Johnston 		}
308781f2e906SMark Johnston 	}
308881f2e906SMark Johnston 
308981f2e906SMark Johnston 	/*
309081f2e906SMark Johnston 	 * Decide whether the signal should be returned.
309181f2e906SMark Johnston 	 * Return the signal's number, or fall through
309281f2e906SMark Johnston 	 * to clear it from the pending mask.
309381f2e906SMark Johnston 	 */
309481f2e906SMark Johnston 	switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
309581f2e906SMark Johnston 	case (intptr_t)SIG_DFL:
309681f2e906SMark Johnston 		/*
309781f2e906SMark Johnston 		 * Don't take default actions on system processes.
309881f2e906SMark Johnston 		 */
309981f2e906SMark Johnston 		if (p->p_pid <= 1) {
310081f2e906SMark Johnston #ifdef DIAGNOSTIC
310181f2e906SMark Johnston 			/*
310281f2e906SMark Johnston 			 * Are you sure you want to ignore SIGSEGV
310381f2e906SMark Johnston 			 * in init? XXX
310481f2e906SMark Johnston 			 */
310581f2e906SMark Johnston 			printf("Process (pid %lu) got signal %d\n",
310681f2e906SMark Johnston 				(u_long)p->p_pid, sig);
310781f2e906SMark Johnston #endif
310881f2e906SMark Johnston 			return (SIGSTATUS_IGNORE);
310981f2e906SMark Johnston 		}
311081f2e906SMark Johnston 
311181f2e906SMark Johnston 		/*
311281f2e906SMark Johnston 		 * If there is a pending stop signal to process with
311381f2e906SMark Johnston 		 * default action, stop here, then clear the signal.
311481f2e906SMark Johnston 		 * Traced or exiting processes should ignore stops.
311581f2e906SMark Johnston 		 * Additionally, a member of an orphaned process group
311681f2e906SMark Johnston 		 * should ignore tty stops.
311781f2e906SMark Johnston 		 */
311881f2e906SMark Johnston 		prop = sigprop(sig);
311981f2e906SMark Johnston 		if (prop & SIGPROP_STOP) {
312081f2e906SMark Johnston 			mtx_unlock(&ps->ps_mtx);
312181f2e906SMark Johnston 			if ((p->p_flag & (P_TRACED | P_WEXIT |
312281f2e906SMark Johnston 			    P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
312381f2e906SMark Johnston 			    pg_flags & PGRP_ORPHANED) != 0 &&
312481f2e906SMark Johnston 			    (prop & SIGPROP_TTYSTOP) != 0)) {
312581f2e906SMark Johnston 				mtx_lock(&ps->ps_mtx);
312681f2e906SMark Johnston 				return (SIGSTATUS_IGNORE);
312781f2e906SMark Johnston 			}
312881f2e906SMark Johnston 			if (TD_SBDRY_INTR(td)) {
312981f2e906SMark Johnston 				KASSERT((td->td_flags & TDF_SBDRY) != 0,
313081f2e906SMark Johnston 				    ("lost TDF_SBDRY"));
313181f2e906SMark Johnston 				mtx_lock(&ps->ps_mtx);
313281f2e906SMark Johnston 				return (SIGSTATUS_SBDRY_STOP);
313381f2e906SMark Johnston 			}
313481f2e906SMark Johnston 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
313581f2e906SMark Johnston 			    &p->p_mtx.lock_object, "Catching SIGSTOP");
313681f2e906SMark Johnston 			sigqueue_delete(&td->td_sigqueue, sig);
313781f2e906SMark Johnston 			sigqueue_delete(&p->p_sigqueue, sig);
313881f2e906SMark Johnston 			p->p_flag |= P_STOPPED_SIG;
313981f2e906SMark Johnston 			p->p_xsig = sig;
314081f2e906SMark Johnston 			PROC_SLOCK(p);
3141c53fec76SKonstantin Belousov 			sig_suspend_threads(td, p);
314281f2e906SMark Johnston 			thread_suspend_switch(td, p);
314381f2e906SMark Johnston 			PROC_SUNLOCK(p);
314481f2e906SMark Johnston 			mtx_lock(&ps->ps_mtx);
314581f2e906SMark Johnston 			return (SIGSTATUS_HANDLED);
314681f2e906SMark Johnston 		} else if ((prop & SIGPROP_IGNORE) != 0 &&
314781f2e906SMark Johnston 		    (td->td_flags & TDF_SIGWAIT) == 0) {
314881f2e906SMark Johnston 			/*
314981f2e906SMark Johnston 			 * Default action is to ignore; drop it if
315081f2e906SMark Johnston 			 * not in kern_sigtimedwait().
315181f2e906SMark Johnston 			 */
315281f2e906SMark Johnston 			return (SIGSTATUS_IGNORE);
315381f2e906SMark Johnston 		} else {
315481f2e906SMark Johnston 			return (SIGSTATUS_HANDLE);
315581f2e906SMark Johnston 		}
315681f2e906SMark Johnston 
315781f2e906SMark Johnston 	case (intptr_t)SIG_IGN:
315881f2e906SMark Johnston 		if ((td->td_flags & TDF_SIGWAIT) == 0)
315981f2e906SMark Johnston 			return (SIGSTATUS_IGNORE);
316081f2e906SMark Johnston 		else
316181f2e906SMark Johnston 			return (SIGSTATUS_HANDLE);
316281f2e906SMark Johnston 
316381f2e906SMark Johnston 	default:
316481f2e906SMark Johnston 		/*
316581f2e906SMark Johnston 		 * This signal has an action, let postsig() process it.
316681f2e906SMark Johnston 		 */
316781f2e906SMark Johnston 		return (SIGSTATUS_HANDLE);
316881f2e906SMark Johnston 	}
316981f2e906SMark Johnston }
317081f2e906SMark Johnston 
3171df8bae1dSRodney W. Grimes /*
3172df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
3173df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
3174df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
3175df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
317681f2e906SMark Johnston  * a syscall or trap (though this can usually be done without calling
317781f2e906SMark Johnston  * issignal by checking the pending signal masks in cursig.) The normal call
3178df8bae1dSRodney W. Grimes  * sequence is
3179df8bae1dSRodney W. Grimes  *
3180e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
31812c42a146SMarcel Moolenaar  *		postsig(sig);
3182df8bae1dSRodney W. Grimes  */
31836711f10fSJohn Baldwin static int
31843cf3b9f0SJohn Baldwin issignal(struct thread *td)
3185df8bae1dSRodney W. Grimes {
3186e602ba25SJulian Elischer 	struct proc *p;
31874093529dSJeff Roberson 	sigset_t sigpending;
318881f2e906SMark Johnston 	int sig;
3189df8bae1dSRodney W. Grimes 
3190e602ba25SJulian Elischer 	p = td->td_proc;
3191628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
319281f2e906SMark Johnston 
3193df8bae1dSRodney W. Grimes 	for (;;) {
31949104847fSDavid Xu 		sigpending = td->td_sigqueue.sq_signals;
31956b286ee8SKonstantin Belousov 		SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
31964093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
31974093529dSJeff Roberson 
31986f56cb8dSKonstantin Belousov 		if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
31996f56cb8dSKonstantin Belousov 		    (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
32004093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
32014093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
3202df8bae1dSRodney W. Grimes 			return (0);
3203146fc63fSKonstantin Belousov 
3204146fc63fSKonstantin Belousov 		/*
3205146fc63fSKonstantin Belousov 		 * Do fast sigblock if requested by usermode.  Since
3206146fc63fSKonstantin Belousov 		 * we do know that there was a signal pending at this
3207146fc63fSKonstantin Belousov 		 * point, set the FAST_SIGBLOCK_PEND as indicator for
3208146fc63fSKonstantin Belousov 		 * usermode to perform a dummy call to
3209146fc63fSKonstantin Belousov 		 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3210146fc63fSKonstantin Belousov 		 * delivery of postponed pending signal.
3211146fc63fSKonstantin Belousov 		 */
3212146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3213146fc63fSKonstantin Belousov 			if (td->td_sigblock_val != 0)
3214146fc63fSKonstantin Belousov 				SIGSETNAND(sigpending, fastblock_mask);
3215146fc63fSKonstantin Belousov 			if (SIGISEMPTY(sigpending)) {
3216146fc63fSKonstantin Belousov 				td->td_pflags |= TDP_SIGFASTPENDING;
3217146fc63fSKonstantin Belousov 				return (0);
3218146fc63fSKonstantin Belousov 			}
3219146fc63fSKonstantin Belousov 		}
3220146fc63fSKonstantin Belousov 
3221b7a25e63SKonstantin Belousov 		if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3222b7a25e63SKonstantin Belousov 		    (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3223b7a25e63SKonstantin Belousov 		    SIGISMEMBER(sigpending, SIGSTOP)) {
3224b7a25e63SKonstantin Belousov 			/*
3225b7a25e63SKonstantin Belousov 			 * If debugger just attached, always consume
3226b7a25e63SKonstantin Belousov 			 * SIGSTOP from ptrace(PT_ATTACH) first, to
3227b7a25e63SKonstantin Belousov 			 * execute the debugger attach ritual in
3228b7a25e63SKonstantin Belousov 			 * order.
3229b7a25e63SKonstantin Belousov 			 */
3230b7a25e63SKonstantin Belousov 			td->td_dbgflags |= TDB_FSTP;
323181f2e906SMark Johnston 			SIGEMPTYSET(sigpending);
323281f2e906SMark Johnston 			SIGADDSET(sigpending, SIGSTOP);
3233b7a25e63SKonstantin Belousov 		}
32342a024a2bSSean Eric Fagan 
323581f2e906SMark Johnston 		SIG_FOREACH(sig, &sigpending) {
323681f2e906SMark Johnston 			switch (sigprocess(td, sig)) {
323781f2e906SMark Johnston 			case SIGSTATUS_HANDLE:
323881f2e906SMark Johnston 				return (sig);
323981f2e906SMark Johnston 			case SIGSTATUS_HANDLED:
324081f2e906SMark Johnston 				goto next;
324181f2e906SMark Johnston 			case SIGSTATUS_IGNORE:
32429104847fSDavid Xu 				sigqueue_delete(&td->td_sigqueue, sig);
32436b286ee8SKonstantin Belousov 				sigqueue_delete(&p->p_sigqueue, sig);
324481f2e906SMark Johnston 				break;
324581f2e906SMark Johnston 			case SIGSTATUS_SBDRY_STOP:
324646e47c4fSKonstantin Belousov 				return (-1);
324746e47c4fSKonstantin Belousov 			}
3248df8bae1dSRodney W. Grimes 		}
3249b7a25e63SKonstantin Belousov next:;
3250df8bae1dSRodney W. Grimes 	}
3251df8bae1dSRodney W. Grimes }
3252df8bae1dSRodney W. Grimes 
3253e574e444SDavid Xu void
3254e574e444SDavid Xu thread_stopped(struct proc *p)
3255e574e444SDavid Xu {
3256e574e444SDavid Xu 	int n;
3257e574e444SDavid Xu 
3258e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
32597b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
3260e574e444SDavid Xu 	n = p->p_suspcount;
32617c9a98f1SDavid Xu 	if (p == curproc)
3262e574e444SDavid Xu 		n++;
3263e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
32647b4a950aSDavid Xu 		PROC_SUNLOCK(p);
3265407948a5SDavid Xu 		p->p_flag &= ~P_WAITED;
3266e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
32677f96995eSDavid Xu 		childproc_stopped(p, (p->p_flag & P_TRACED) ?
3268ebceaf6dSDavid Xu 			CLD_TRAPPED : CLD_STOPPED);
3269e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
32707b4a950aSDavid Xu 		PROC_SLOCK(p);
3271e574e444SDavid Xu 	}
3272e574e444SDavid Xu }
3273e574e444SDavid Xu 
3274df8bae1dSRodney W. Grimes /*
3275df8bae1dSRodney W. Grimes  * Take the action for the specified signal
3276df8bae1dSRodney W. Grimes  * from the current set of pending signals.
3277df8bae1dSRodney W. Grimes  */
327875c586a4SKonstantin Belousov int
32790167b33bSKonstantin Belousov postsig(int sig)
3280df8bae1dSRodney W. Grimes {
32810167b33bSKonstantin Belousov 	struct thread *td;
32820167b33bSKonstantin Belousov 	struct proc *p;
3283628d2653SJohn Baldwin 	struct sigacts *ps;
32842c42a146SMarcel Moolenaar 	sig_t action;
32859104847fSDavid Xu 	ksiginfo_t ksi;
3286e442f29fSKonstantin Belousov 	sigset_t returnmask;
3287df8bae1dSRodney W. Grimes 
32882c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
32895526d2d9SEivind Eklund 
32900167b33bSKonstantin Belousov 	td = curthread;
32910167b33bSKonstantin Belousov 	p = td->td_proc;
32922ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3293628d2653SJohn Baldwin 	ps = p->p_sigacts;
329490af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
32955da49fcbSDavid Xu 	ksiginfo_init(&ksi);
32966b286ee8SKonstantin Belousov 	if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
32976b286ee8SKonstantin Belousov 	    sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
329875c586a4SKonstantin Belousov 		return (0);
32999104847fSDavid Xu 	ksi.ksi_signo = sig;
330056c06c4bSDavid Xu 	if (ksi.ksi_code == SI_TIMER)
330156c06c4bSDavid Xu 		itimer_accept(p, ksi.ksi_timerid, &ksi);
33022c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
3303df8bae1dSRodney W. Grimes #ifdef KTRACE
3304374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
33055e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
330661009552SJilles Tjoelker 		    &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3307df8bae1dSRodney W. Grimes #endif
33082a024a2bSSean Eric Fagan 
33098460a577SJohn Birrell 	if (action == SIG_DFL) {
3310df8bae1dSRodney W. Grimes 		/*
3311df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
3312df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
3313df8bae1dSRodney W. Grimes 		 */
331490af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
331586be94fcSTycho Nightingale 		proc_td_siginfo_capture(td, &ksi.ksi_info);
3316b40ce416SJulian Elischer 		sigexit(td, sig);
3317df8bae1dSRodney W. Grimes 		/* NOTREACHED */
3318df8bae1dSRodney W. Grimes 	} else {
3319df8bae1dSRodney W. Grimes 		/*
3320df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
3321df8bae1dSRodney W. Grimes 		 */
3322cd735d8fSKonstantin Belousov 		KASSERT(action != SIG_IGN, ("postsig action %p", action));
3323cd735d8fSKonstantin Belousov 		KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3324cd735d8fSKonstantin Belousov 		    ("postsig action: blocked sig %d", sig));
3325cd735d8fSKonstantin Belousov 
3326df8bae1dSRodney W. Grimes 		/*
3327df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
3328645682fdSLuoqi Chen 		 * occurrences of this signal.
3329df8bae1dSRodney W. Grimes 		 *
3330645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
3331df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
3332645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
3333df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
3334df8bae1dSRodney W. Grimes 		 */
33355e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
33364093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
33375e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
3338df8bae1dSRodney W. Grimes 		} else
33394093529dSJeff Roberson 			returnmask = td->td_sigmask;
33402c42a146SMarcel Moolenaar 
3341c90c9021SEd Schouten 		if (p->p_sig == sig) {
33426626c604SJulian Elischer 			p->p_sig = 0;
3343df8bae1dSRodney W. Grimes 		}
33449104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3345e442f29fSKonstantin Belousov 		postsig_done(sig, td, ps);
3346df8bae1dSRodney W. Grimes 	}
334775c586a4SKonstantin Belousov 	return (1);
3348df8bae1dSRodney W. Grimes }
3349df8bae1dSRodney W. Grimes 
33500c82fb26SKonstantin Belousov int
33510c82fb26SKonstantin Belousov sig_ast_checksusp(struct thread *td)
33520c82fb26SKonstantin Belousov {
33533d277851SKonstantin Belousov 	struct proc *p __diagused;
33540c82fb26SKonstantin Belousov 	int ret;
33550c82fb26SKonstantin Belousov 
33560c82fb26SKonstantin Belousov 	p = td->td_proc;
33570c82fb26SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
33580c82fb26SKonstantin Belousov 
3359c6d31b83SKonstantin Belousov 	if (!td_ast_pending(td, TDA_SUSPEND))
33600c82fb26SKonstantin Belousov 		return (0);
33610c82fb26SKonstantin Belousov 
33620c82fb26SKonstantin Belousov 	ret = thread_suspend_check(1);
33630c82fb26SKonstantin Belousov 	MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
33640c82fb26SKonstantin Belousov 	return (ret);
33650c82fb26SKonstantin Belousov }
33660c82fb26SKonstantin Belousov 
33670c82fb26SKonstantin Belousov int
33680c82fb26SKonstantin Belousov sig_ast_needsigchk(struct thread *td)
33690c82fb26SKonstantin Belousov {
33700c82fb26SKonstantin Belousov 	struct proc *p;
33710c82fb26SKonstantin Belousov 	struct sigacts *ps;
33720c82fb26SKonstantin Belousov 	int ret, sig;
33730c82fb26SKonstantin Belousov 
33740c82fb26SKonstantin Belousov 	p = td->td_proc;
33750c82fb26SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
33760c82fb26SKonstantin Belousov 
3377c6d31b83SKonstantin Belousov 	if (!td_ast_pending(td, TDA_SIG))
33780c82fb26SKonstantin Belousov 		return (0);
33790c82fb26SKonstantin Belousov 
33800c82fb26SKonstantin Belousov 	ps = p->p_sigacts;
33810c82fb26SKonstantin Belousov 	mtx_lock(&ps->ps_mtx);
33820c82fb26SKonstantin Belousov 	sig = cursig(td);
33830c82fb26SKonstantin Belousov 	if (sig == -1) {
33840c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
33850c82fb26SKonstantin Belousov 		KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
33860c82fb26SKonstantin Belousov 		KASSERT(TD_SBDRY_INTR(td),
33870c82fb26SKonstantin Belousov 		    ("lost TDF_SERESTART of TDF_SEINTR"));
33880c82fb26SKonstantin Belousov 		KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
33890c82fb26SKonstantin Belousov 		    (TDF_SEINTR | TDF_SERESTART),
33900c82fb26SKonstantin Belousov 		    ("both TDF_SEINTR and TDF_SERESTART"));
33910c82fb26SKonstantin Belousov 		ret = TD_SBDRY_ERRNO(td);
33920c82fb26SKonstantin Belousov 	} else if (sig != 0) {
33930c82fb26SKonstantin Belousov 		ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
33940c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
33950c82fb26SKonstantin Belousov 	} else {
33960c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
33970c82fb26SKonstantin Belousov 		ret = 0;
33980c82fb26SKonstantin Belousov 	}
33990c82fb26SKonstantin Belousov 
34000c82fb26SKonstantin Belousov 	/*
34010c82fb26SKonstantin Belousov 	 * Do not go into sleep if this thread was the ptrace(2)
34020c82fb26SKonstantin Belousov 	 * attach leader.  cursig() consumed SIGSTOP from PT_ATTACH,
34030c82fb26SKonstantin Belousov 	 * but we usually act on the signal by interrupting sleep, and
34040c82fb26SKonstantin Belousov 	 * should do that here as well.
34050c82fb26SKonstantin Belousov 	 */
34060c82fb26SKonstantin Belousov 	if ((td->td_dbgflags & TDB_FSTP) != 0) {
34070c82fb26SKonstantin Belousov 		if (ret == 0)
34080c82fb26SKonstantin Belousov 			ret = EINTR;
34090c82fb26SKonstantin Belousov 		td->td_dbgflags &= ~TDB_FSTP;
34100c82fb26SKonstantin Belousov 	}
34110c82fb26SKonstantin Belousov 
34120c82fb26SKonstantin Belousov 	return (ret);
34130c82fb26SKonstantin Belousov }
34140c82fb26SKonstantin Belousov 
34150400be45SKonstantin Belousov int
34160400be45SKonstantin Belousov sig_intr(void)
34170400be45SKonstantin Belousov {
34180400be45SKonstantin Belousov 	struct thread *td;
34190400be45SKonstantin Belousov 	struct proc *p;
34200400be45SKonstantin Belousov 	int ret;
34210400be45SKonstantin Belousov 
34220400be45SKonstantin Belousov 	td = curthread;
3423c6d31b83SKonstantin Belousov 	if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3424203dda8aSKonstantin Belousov 		return (0);
3425203dda8aSKonstantin Belousov 
34260400be45SKonstantin Belousov 	p = td->td_proc;
34270400be45SKonstantin Belousov 
34280400be45SKonstantin Belousov 	PROC_LOCK(p);
34290400be45SKonstantin Belousov 	ret = sig_ast_checksusp(td);
34300400be45SKonstantin Belousov 	if (ret == 0)
34310400be45SKonstantin Belousov 		ret = sig_ast_needsigchk(td);
34320400be45SKonstantin Belousov 	PROC_UNLOCK(p);
34330400be45SKonstantin Belousov 	return (ret);
34340400be45SKonstantin Belousov }
34350400be45SKonstantin Belousov 
3436244ab566SKonstantin Belousov bool
3437244ab566SKonstantin Belousov curproc_sigkilled(void)
3438244ab566SKonstantin Belousov {
3439244ab566SKonstantin Belousov 	struct thread *td;
3440244ab566SKonstantin Belousov 	struct proc *p;
3441244ab566SKonstantin Belousov 	struct sigacts *ps;
3442244ab566SKonstantin Belousov 	bool res;
3443244ab566SKonstantin Belousov 
3444244ab566SKonstantin Belousov 	td = curthread;
3445c6d31b83SKonstantin Belousov 	if (!td_ast_pending(td, TDA_SIG))
3446244ab566SKonstantin Belousov 		return (false);
3447244ab566SKonstantin Belousov 
3448244ab566SKonstantin Belousov 	p = td->td_proc;
3449244ab566SKonstantin Belousov 	PROC_LOCK(p);
3450244ab566SKonstantin Belousov 	ps = p->p_sigacts;
3451244ab566SKonstantin Belousov 	mtx_lock(&ps->ps_mtx);
3452244ab566SKonstantin Belousov 	res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3453244ab566SKonstantin Belousov 	    SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3454244ab566SKonstantin Belousov 	mtx_unlock(&ps->ps_mtx);
3455244ab566SKonstantin Belousov 	PROC_UNLOCK(p);
3456244ab566SKonstantin Belousov 	return (res);
3457244ab566SKonstantin Belousov }
3458244ab566SKonstantin Belousov 
3459a70e9a13SKonstantin Belousov void
3460a70e9a13SKonstantin Belousov proc_wkilled(struct proc *p)
3461a70e9a13SKonstantin Belousov {
3462a70e9a13SKonstantin Belousov 
3463a70e9a13SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
3464a70e9a13SKonstantin Belousov 	if ((p->p_flag & P_WKILLED) == 0) {
3465a70e9a13SKonstantin Belousov 		p->p_flag |= P_WKILLED;
3466a70e9a13SKonstantin Belousov 		/*
3467a70e9a13SKonstantin Belousov 		 * Notify swapper that there is a process to swap in.
3468a70e9a13SKonstantin Belousov 		 * The notification is racy, at worst it would take 10
3469a70e9a13SKonstantin Belousov 		 * seconds for the swapper process to notice.
3470a70e9a13SKonstantin Belousov 		 */
3471a70e9a13SKonstantin Belousov 		if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3472a70e9a13SKonstantin Belousov 			wakeup(&proc0);
3473a70e9a13SKonstantin Belousov 	}
3474a70e9a13SKonstantin Belousov }
3475a70e9a13SKonstantin Belousov 
3476df8bae1dSRodney W. Grimes /*
3477df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
3478df8bae1dSRodney W. Grimes  */
347926f9a767SRodney W. Grimes void
3480fe20aaecSRyan Libby killproc(struct proc *p, const char *why)
3481df8bae1dSRodney W. Grimes {
34829081e5e8SJohn Baldwin 
34839081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3484c3209846SPawel Jakub Dawidek 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3485c3209846SPawel Jakub Dawidek 	    p->p_comm);
3486af8beccaSKristof Provost 	log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
34874ae4822dSKristof Provost 	    p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3488de2d0d29SKristof Provost 	    p->p_ucred->cr_uid, why);
3489a70e9a13SKonstantin Belousov 	proc_wkilled(p);
34908451d0ddSKip Macy 	kern_psignal(p, SIGKILL);
3491df8bae1dSRodney W. Grimes }
3492df8bae1dSRodney W. Grimes 
3493df8bae1dSRodney W. Grimes /*
3494df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
3495df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
3496df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
3497df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
3498df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
3499df8bae1dSRodney W. Grimes  * does not return.
3500df8bae1dSRodney W. Grimes  */
350126f9a767SRodney W. Grimes void
3502e052a8b9SEd Maste sigexit(struct thread *td, int sig)
3503df8bae1dSRodney W. Grimes {
3504b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3505df8bae1dSRodney W. Grimes 
3506628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
35074493a13eSKonstantin Belousov 	proc_set_p2_wexit(p);
35084493a13eSKonstantin Belousov 
3509df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
3510f97c3df1SDavid Schultz 	/*
3511f97c3df1SDavid Schultz 	 * We must be single-threading to generate a core dump.  This
3512f97c3df1SDavid Schultz 	 * ensures that the registers in the core file are up-to-date.
3513f97c3df1SDavid Schultz 	 * Also, the ELF dump handler assumes that the thread list doesn't
3514f97c3df1SDavid Schultz 	 * change out from under it.
3515f97c3df1SDavid Schultz 	 *
3516f97c3df1SDavid Schultz 	 * XXX If another thread attempts to single-thread before us
3517f97c3df1SDavid Schultz 	 *     (e.g. via fork()), we won't get a dump at all.
3518f97c3df1SDavid Schultz 	 */
3519fd50a707SBrooks Davis 	if ((sigprop(sig) & SIGPROP_CORE) &&
3520fd50a707SBrooks Davis 	    thread_single(p, SINGLE_NO_EXIT) == 0) {
35212c42a146SMarcel Moolenaar 		p->p_sig = sig;
3522c364e17eSAndrey A. Chernov 		/*
3523c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
3524c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
3525c364e17eSAndrey A. Chernov 		 * these messages.)
3526c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
35274ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
3528c364e17eSAndrey A. Chernov 		 */
3529b40ce416SJulian Elischer 		if (coredump(td) == 0)
35302c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
353157308494SJoerg Wunsch 		if (kern_logsigexit)
353257308494SJoerg Wunsch 			log(LOG_INFO,
3533af8beccaSKristof Provost 			    "pid %d (%s), jid %d, uid %d: exited on "
35344ae4822dSKristof Provost 			    "signal %d%s\n", p->p_pid, p->p_comm,
35354ae4822dSKristof Provost 			    p->p_ucred->cr_prison->pr_id,
3536de2d0d29SKristof Provost 			    td->td_ucred->cr_uid,
35372c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
35382c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
35394ae89b95SJohn Baldwin 	} else
3540628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3541b4490c6eSKonstantin Belousov 	exit1(td, 0, sig);
3542df8bae1dSRodney W. Grimes 	/* NOTREACHED */
3543df8bae1dSRodney W. Grimes }
3544df8bae1dSRodney W. Grimes 
3545ebceaf6dSDavid Xu /*
35467f96995eSDavid Xu  * Send queued SIGCHLD to parent when child process's state
35477f96995eSDavid Xu  * is changed.
3548ebceaf6dSDavid Xu  */
35497f96995eSDavid Xu static void
35507f96995eSDavid Xu sigparent(struct proc *p, int reason, int status)
3551ebceaf6dSDavid Xu {
3552ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
3553ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3554ebceaf6dSDavid Xu 
3555ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
3556ebceaf6dSDavid Xu 		p->p_ksi->ksi_signo  = SIGCHLD;
3557ebceaf6dSDavid Xu 		p->p_ksi->ksi_code   = reason;
35587f96995eSDavid Xu 		p->p_ksi->ksi_status = status;
3559ebceaf6dSDavid Xu 		p->p_ksi->ksi_pid    = p->p_pid;
3560ebceaf6dSDavid Xu 		p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
3561ebceaf6dSDavid Xu 		if (KSI_ONQ(p->p_ksi))
3562ebceaf6dSDavid Xu 			return;
3563ebceaf6dSDavid Xu 	}
3564ad6eec7bSJohn Baldwin 	pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3565ebceaf6dSDavid Xu }
3566ebceaf6dSDavid Xu 
35677f96995eSDavid Xu static void
3568b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig)
35697f96995eSDavid Xu {
35707f96995eSDavid Xu 	struct sigacts *ps;
35717f96995eSDavid Xu 
35727f96995eSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
35737f96995eSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
35747f96995eSDavid Xu 
35757f96995eSDavid Xu 	/*
35767f96995eSDavid Xu 	 * Wake up parent sleeping in kern_wait(), also send
35777f96995eSDavid Xu 	 * SIGCHLD to parent, but SIGCHLD does not guarantee
35787f96995eSDavid Xu 	 * that parent will awake, because parent may masked
35797f96995eSDavid Xu 	 * the signal.
35807f96995eSDavid Xu 	 */
35817f96995eSDavid Xu 	p->p_pptr->p_flag |= P_STATCHILD;
35827f96995eSDavid Xu 	wakeup(p->p_pptr);
35837f96995eSDavid Xu 
35847f96995eSDavid Xu 	ps = p->p_pptr->p_sigacts;
35857f96995eSDavid Xu 	mtx_lock(&ps->ps_mtx);
35867f96995eSDavid Xu 	if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
35877f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
3588b20a9aa9SJilles Tjoelker 		sigparent(p, reason, sig);
35897f96995eSDavid Xu 	} else
35907f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
35917f96995eSDavid Xu }
35927f96995eSDavid Xu 
35937f96995eSDavid Xu void
35947f96995eSDavid Xu childproc_stopped(struct proc *p, int reason)
35957f96995eSDavid Xu {
3596b4490c6eSKonstantin Belousov 
3597b4490c6eSKonstantin Belousov 	childproc_jobstate(p, reason, p->p_xsig);
35987f96995eSDavid Xu }
35997f96995eSDavid Xu 
3600ebceaf6dSDavid Xu void
3601ebceaf6dSDavid Xu childproc_continued(struct proc *p)
3602ebceaf6dSDavid Xu {
36037f96995eSDavid Xu 	childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3604ebceaf6dSDavid Xu }
3605ebceaf6dSDavid Xu 
3606ebceaf6dSDavid Xu void
3607ebceaf6dSDavid Xu childproc_exited(struct proc *p)
3608ebceaf6dSDavid Xu {
3609b4490c6eSKonstantin Belousov 	int reason, status;
3610ebceaf6dSDavid Xu 
3611b4490c6eSKonstantin Belousov 	if (WCOREDUMP(p->p_xsig)) {
3612b4490c6eSKonstantin Belousov 		reason = CLD_DUMPED;
3613b4490c6eSKonstantin Belousov 		status = WTERMSIG(p->p_xsig);
3614b4490c6eSKonstantin Belousov 	} else if (WIFSIGNALED(p->p_xsig)) {
3615b4490c6eSKonstantin Belousov 		reason = CLD_KILLED;
3616b4490c6eSKonstantin Belousov 		status = WTERMSIG(p->p_xsig);
3617b4490c6eSKonstantin Belousov 	} else {
3618b4490c6eSKonstantin Belousov 		reason = CLD_EXITED;
3619b4490c6eSKonstantin Belousov 		status = p->p_xexit;
3620b4490c6eSKonstantin Belousov 	}
36217f96995eSDavid Xu 	/*
36227f96995eSDavid Xu 	 * XXX avoid calling wakeup(p->p_pptr), the work is
36237f96995eSDavid Xu 	 * done in exit1().
36247f96995eSDavid Xu 	 */
36257f96995eSDavid Xu 	sigparent(p, reason, status);
3626ebceaf6dSDavid Xu }
3627ebceaf6dSDavid Xu 
3628f1fe1e02SMariusz Zaborski #define	MAX_NUM_CORE_FILES 100000
3629cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES
3630cc37baeaSStephen J. Kiernan #define	NUM_CORE_FILES 5
3631cc37baeaSStephen J. Kiernan #endif
3632cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3633cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES;
3634e7228204SAlfred Perlstein 
3635e7228204SAlfred Perlstein static int
3636e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3637e7228204SAlfred Perlstein {
3638e7228204SAlfred Perlstein 	int error;
3639e7228204SAlfred Perlstein 	int new_val;
3640e7228204SAlfred Perlstein 
3641c5105012SKonstantin Belousov 	new_val = num_cores;
3642e7228204SAlfred Perlstein 	error = sysctl_handle_int(oidp, &new_val, 0, req);
3643e7228204SAlfred Perlstein 	if (error != 0 || req->newptr == NULL)
3644e7228204SAlfred Perlstein 		return (error);
3645cc37baeaSStephen J. Kiernan 	if (new_val > MAX_NUM_CORE_FILES)
3646cc37baeaSStephen J. Kiernan 		new_val = MAX_NUM_CORE_FILES;
3647e7228204SAlfred Perlstein 	if (new_val < 0)
3648e7228204SAlfred Perlstein 		new_val = 0;
3649e7228204SAlfred Perlstein 	num_cores = new_val;
3650e7228204SAlfred Perlstein 	return (0);
3651e7228204SAlfred Perlstein }
36527029da5cSPawel Biernacki SYSCTL_PROC(_debug, OID_AUTO, ncores,
3653fe27f1dbSAlexander Motin     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
36547029da5cSPawel Biernacki     sysctl_debug_num_cores_check, "I",
36556cad1a5dSMariusz Zaborski     "Maximum number of generated process corefiles while using index format");
3656e7228204SAlfred Perlstein 
36576026dcd7SMark Johnston #define	GZIP_SUFFIX	".gz"
36586026dcd7SMark Johnston #define	ZSTD_SUFFIX	".zst"
3659aa14e9b7SMark Johnston 
366078f57a9cSMark Johnston int compress_user_cores = 0;
3661e7228204SAlfred Perlstein 
366278f57a9cSMark Johnston static int
366378f57a9cSMark Johnston sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
366478f57a9cSMark Johnston {
366578f57a9cSMark Johnston 	int error, val;
366678f57a9cSMark Johnston 
366778f57a9cSMark Johnston 	val = compress_user_cores;
366878f57a9cSMark Johnston 	error = sysctl_handle_int(oidp, &val, 0, req);
366978f57a9cSMark Johnston 	if (error != 0 || req->newptr == NULL)
367078f57a9cSMark Johnston 		return (error);
367178f57a9cSMark Johnston 	if (val != 0 && !compressor_avail(val))
367278f57a9cSMark Johnston 		return (EINVAL);
367378f57a9cSMark Johnston 	compress_user_cores = val;
367478f57a9cSMark Johnston 	return (error);
367578f57a9cSMark Johnston }
36767029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
36777029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
36787029da5cSPawel Biernacki     sysctl_compress_user_cores, "I",
36796026dcd7SMark Johnston     "Enable compression of user corefiles ("
36806026dcd7SMark Johnston     __XSTRING(COMPRESS_GZIP) " = gzip, "
36816026dcd7SMark Johnston     __XSTRING(COMPRESS_ZSTD) " = zstd)");
368278f57a9cSMark Johnston 
368378f57a9cSMark Johnston int compress_user_cores_level = 6;
368478f57a9cSMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
368578f57a9cSMark Johnston     &compress_user_cores_level, 0,
368678f57a9cSMark Johnston     "Corefile compression level");
3687e7228204SAlfred Perlstein 
36885bc0ff88SMateusz Guzik /*
36895bc0ff88SMateusz Guzik  * Protect the access to corefilename[] by allproc_lock.
36905bc0ff88SMateusz Guzik  */
36915bc0ff88SMateusz Guzik #define	corefilename_lock	allproc_lock
36925bc0ff88SMateusz Guzik 
36936d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"};
3694fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
36955bc0ff88SMateusz Guzik 
36965bc0ff88SMateusz Guzik static int
36975bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
36985bc0ff88SMateusz Guzik {
36995bc0ff88SMateusz Guzik 	int error;
37005bc0ff88SMateusz Guzik 
37015bc0ff88SMateusz Guzik 	sx_xlock(&corefilename_lock);
37025bc0ff88SMateusz Guzik 	error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
37035bc0ff88SMateusz Guzik 	    req);
37045bc0ff88SMateusz Guzik 	sx_xunlock(&corefilename_lock);
37055bc0ff88SMateusz Guzik 
37065bc0ff88SMateusz Guzik 	return (error);
37075bc0ff88SMateusz Guzik }
3708fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
37095bc0ff88SMateusz Guzik     CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
37105bc0ff88SMateusz Guzik     "Process corefile name format string");
3711c5edb423SSean Eric Fagan 
37120dea6e3cSMariusz Zaborski static void
37130dea6e3cSMariusz Zaborski vnode_close_locked(struct thread *td, struct vnode *vp)
37140dea6e3cSMariusz Zaborski {
37150dea6e3cSMariusz Zaborski 
3716b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
37170dea6e3cSMariusz Zaborski 	vn_close(vp, FWRITE, td->td_ucred, td);
37180dea6e3cSMariusz Zaborski }
37190dea6e3cSMariusz Zaborski 
37200dea6e3cSMariusz Zaborski /*
37210dea6e3cSMariusz Zaborski  * If the core format has a %I in it, then we need to check
37220dea6e3cSMariusz Zaborski  * for existing corefiles before defining a name.
3723f1fe1e02SMariusz Zaborski  * To do this we iterate over 0..ncores to find a
37240dea6e3cSMariusz Zaborski  * non-existing core file name to use. If all core files are
37250dea6e3cSMariusz Zaborski  * already used we choose the oldest one.
37260dea6e3cSMariusz Zaborski  */
37270dea6e3cSMariusz Zaborski static int
37280dea6e3cSMariusz Zaborski corefile_open_last(struct thread *td, char *name, int indexpos,
3729f1fe1e02SMariusz Zaborski     int indexlen, int ncores, struct vnode **vpp)
37300dea6e3cSMariusz Zaborski {
37310dea6e3cSMariusz Zaborski 	struct vnode *oldvp, *nextvp, *vp;
37320dea6e3cSMariusz Zaborski 	struct vattr vattr;
37330dea6e3cSMariusz Zaborski 	struct nameidata nd;
37340dea6e3cSMariusz Zaborski 	int error, i, flags, oflags, cmode;
3735f1fe1e02SMariusz Zaborski 	char ch;
37360dea6e3cSMariusz Zaborski 	struct timespec lasttime;
37370dea6e3cSMariusz Zaborski 
37380dea6e3cSMariusz Zaborski 	nextvp = oldvp = NULL;
37390dea6e3cSMariusz Zaborski 	cmode = S_IRUSR | S_IWUSR;
37400dea6e3cSMariusz Zaborski 	oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
37410dea6e3cSMariusz Zaborski 	    (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
37420dea6e3cSMariusz Zaborski 
3743f1fe1e02SMariusz Zaborski 	for (i = 0; i < ncores; i++) {
37440dea6e3cSMariusz Zaborski 		flags = O_CREAT | FWRITE | O_NOFOLLOW;
3745f1fe1e02SMariusz Zaborski 
3746f1fe1e02SMariusz Zaborski 		ch = name[indexpos + indexlen];
3747f1fe1e02SMariusz Zaborski 		(void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3748f1fe1e02SMariusz Zaborski 		    i);
3749f1fe1e02SMariusz Zaborski 		name[indexpos + indexlen] = ch;
37500dea6e3cSMariusz Zaborski 
37517e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
37520dea6e3cSMariusz Zaborski 		error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
37530dea6e3cSMariusz Zaborski 		    NULL);
37540dea6e3cSMariusz Zaborski 		if (error != 0)
37550dea6e3cSMariusz Zaborski 			break;
37560dea6e3cSMariusz Zaborski 
37570dea6e3cSMariusz Zaborski 		vp = nd.ni_vp;
3758bb92cd7bSMateusz Guzik 		NDFREE_PNBUF(&nd);
37590dea6e3cSMariusz Zaborski 		if ((flags & O_CREAT) == O_CREAT) {
37600dea6e3cSMariusz Zaborski 			nextvp = vp;
37610dea6e3cSMariusz Zaborski 			break;
37620dea6e3cSMariusz Zaborski 		}
37630dea6e3cSMariusz Zaborski 
37640dea6e3cSMariusz Zaborski 		error = VOP_GETATTR(vp, &vattr, td->td_ucred);
37650dea6e3cSMariusz Zaborski 		if (error != 0) {
37660dea6e3cSMariusz Zaborski 			vnode_close_locked(td, vp);
37670dea6e3cSMariusz Zaborski 			break;
37680dea6e3cSMariusz Zaborski 		}
37690dea6e3cSMariusz Zaborski 
37700dea6e3cSMariusz Zaborski 		if (oldvp == NULL ||
37710dea6e3cSMariusz Zaborski 		    lasttime.tv_sec > vattr.va_mtime.tv_sec ||
37720dea6e3cSMariusz Zaborski 		    (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
37730dea6e3cSMariusz Zaborski 		    lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
37740dea6e3cSMariusz Zaborski 			if (oldvp != NULL)
3775e298466eSAndriy Gapon 				vn_close(oldvp, FWRITE, td->td_ucred, td);
37760dea6e3cSMariusz Zaborski 			oldvp = vp;
3777e298466eSAndriy Gapon 			VOP_UNLOCK(oldvp);
37780dea6e3cSMariusz Zaborski 			lasttime = vattr.va_mtime;
37790dea6e3cSMariusz Zaborski 		} else {
37800dea6e3cSMariusz Zaborski 			vnode_close_locked(td, vp);
37810dea6e3cSMariusz Zaborski 		}
37820dea6e3cSMariusz Zaborski 	}
37830dea6e3cSMariusz Zaborski 
37840dea6e3cSMariusz Zaborski 	if (oldvp != NULL) {
378589f2ab06SKonstantin Belousov 		if (nextvp == NULL) {
378689f2ab06SKonstantin Belousov 			if ((td->td_proc->p_flag & P_SUGID) != 0) {
378789f2ab06SKonstantin Belousov 				error = EFAULT;
3788e298466eSAndriy Gapon 				vn_close(oldvp, FWRITE, td->td_ucred, td);
378989f2ab06SKonstantin Belousov 			} else {
379089f2ab06SKonstantin Belousov 				nextvp = oldvp;
3791e298466eSAndriy Gapon 				error = vn_lock(nextvp, LK_EXCLUSIVE);
3792e298466eSAndriy Gapon 				if (error != 0) {
3793e298466eSAndriy Gapon 					vn_close(nextvp, FWRITE, td->td_ucred,
3794e298466eSAndriy Gapon 					    td);
3795e298466eSAndriy Gapon 					nextvp = NULL;
3796e298466eSAndriy Gapon 				}
379789f2ab06SKonstantin Belousov 			}
379889f2ab06SKonstantin Belousov 		} else {
3799e298466eSAndriy Gapon 			vn_close(oldvp, FWRITE, td->td_ucred, td);
380089f2ab06SKonstantin Belousov 		}
38010dea6e3cSMariusz Zaborski 	}
38020dea6e3cSMariusz Zaborski 	if (error != 0) {
38030dea6e3cSMariusz Zaborski 		if (nextvp != NULL)
38040dea6e3cSMariusz Zaborski 			vnode_close_locked(td, oldvp);
38050dea6e3cSMariusz Zaborski 	} else {
38060dea6e3cSMariusz Zaborski 		*vpp = nextvp;
38070dea6e3cSMariusz Zaborski 	}
38080dea6e3cSMariusz Zaborski 
38090dea6e3cSMariusz Zaborski 	return (error);
38100dea6e3cSMariusz Zaborski }
38110dea6e3cSMariusz Zaborski 
3812c5edb423SSean Eric Fagan /*
3813c345faeaSPawel Jakub Dawidek  * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3814c345faeaSPawel Jakub Dawidek  * Expand the name described in corefilename, using name, uid, and pid
3815c345faeaSPawel Jakub Dawidek  * and open/create core file.
3816c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
3817c5edb423SSean Eric Fagan  *	%N	name of process ("name")
3818c5edb423SSean Eric Fagan  *	%P	process id (pid)
3819c5edb423SSean Eric Fagan  *	%U	user id (uid)
3820c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
3821c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3822c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
3823c5edb423SSean Eric Fagan  */
3824c345faeaSPawel Jakub Dawidek static int
3825c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
38269d3ecb7eSEric van Gyzen     int compress, int signum, struct vnode **vpp, char **namep)
38278b43b535SAlfred Perlstein {
382836b208e0SRobert Watson 	struct sbuf sb;
38290dea6e3cSMariusz Zaborski 	struct nameidata nd;
383036b208e0SRobert Watson 	const char *format;
3831c345faeaSPawel Jakub Dawidek 	char *hostname, *name;
3832f1fe1e02SMariusz Zaborski 	int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3833c5edb423SSean Eric Fagan 
3834b7402d82SAlfred Perlstein 	hostname = NULL;
38358b43b535SAlfred Perlstein 	format = corefilename;
3836086053a3SPawel Jakub Dawidek 	name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3837f1fe1e02SMariusz Zaborski 	indexlen = 0;
3838e7228204SAlfred Perlstein 	indexpos = -1;
3839f1fe1e02SMariusz Zaborski 	ncores = num_cores;
3840c52ff611SPawel Jakub Dawidek 	(void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
38415bc0ff88SMateusz Guzik 	sx_slock(&corefilename_lock);
384229146f1aSPawel Jakub Dawidek 	for (i = 0; format[i] != '\0'; i++) {
3843c5edb423SSean Eric Fagan 		switch (format[i]) {
3844c5edb423SSean Eric Fagan 		case '%':	/* Format character */
3845c5edb423SSean Eric Fagan 			i++;
3846c5edb423SSean Eric Fagan 			switch (format[i]) {
3847c5edb423SSean Eric Fagan 			case '%':
384836b208e0SRobert Watson 				sbuf_putc(&sb, '%');
3849c5edb423SSean Eric Fagan 				break;
3850e7228204SAlfred Perlstein 			case 'H':	/* hostname */
3851b7402d82SAlfred Perlstein 				if (hostname == NULL) {
3852b7402d82SAlfred Perlstein 					hostname = malloc(MAXHOSTNAMELEN,
3853086053a3SPawel Jakub Dawidek 					    M_TEMP, M_WAITOK);
3854b7402d82SAlfred Perlstein 				}
3855e7228204SAlfred Perlstein 				getcredhostname(td->td_ucred, hostname,
3856b7402d82SAlfred Perlstein 				    MAXHOSTNAMELEN);
3857e7228204SAlfred Perlstein 				sbuf_printf(&sb, "%s", hostname);
3858e7228204SAlfred Perlstein 				break;
3859e7228204SAlfred Perlstein 			case 'I':	/* autoincrementing index */
3860f1fe1e02SMariusz Zaborski 				if (indexpos != -1) {
3861f1fe1e02SMariusz Zaborski 					sbuf_printf(&sb, "%%I");
3862f1fe1e02SMariusz Zaborski 					break;
3863f1fe1e02SMariusz Zaborski 				}
3864f1fe1e02SMariusz Zaborski 
3865f1fe1e02SMariusz Zaborski 				indexpos = sbuf_len(&sb);
3866f1fe1e02SMariusz Zaborski 				sbuf_printf(&sb, "%u", ncores - 1);
3867f1fe1e02SMariusz Zaborski 				indexlen = sbuf_len(&sb) - indexpos;
3868e7228204SAlfred Perlstein 				break;
3869c5edb423SSean Eric Fagan 			case 'N':	/* process name */
3870c52ff611SPawel Jakub Dawidek 				sbuf_printf(&sb, "%s", comm);
3871c5edb423SSean Eric Fagan 				break;
3872c5edb423SSean Eric Fagan 			case 'P':	/* process id */
387336b208e0SRobert Watson 				sbuf_printf(&sb, "%u", pid);
3874c5edb423SSean Eric Fagan 				break;
38759d3ecb7eSEric van Gyzen 			case 'S':	/* signal number */
38769d3ecb7eSEric van Gyzen 				sbuf_printf(&sb, "%i", signum);
38779d3ecb7eSEric van Gyzen 				break;
3878c5edb423SSean Eric Fagan 			case 'U':	/* user id */
387936b208e0SRobert Watson 				sbuf_printf(&sb, "%u", uid);
3880c5edb423SSean Eric Fagan 				break;
3881c5edb423SSean Eric Fagan 			default:
38828b43b535SAlfred Perlstein 				log(LOG_ERR,
388336b208e0SRobert Watson 				    "Unknown format character %c in "
388436b208e0SRobert Watson 				    "corename `%s'\n", format[i], format);
388529146f1aSPawel Jakub Dawidek 				break;
3886c5edb423SSean Eric Fagan 			}
3887c5edb423SSean Eric Fagan 			break;
3888c5edb423SSean Eric Fagan 		default:
388936b208e0SRobert Watson 			sbuf_putc(&sb, format[i]);
38906c08be2bSPawel Jakub Dawidek 			break;
3891c5edb423SSean Eric Fagan 		}
3892c5edb423SSean Eric Fagan 	}
38935bc0ff88SMateusz Guzik 	sx_sunlock(&corefilename_lock);
3894b7402d82SAlfred Perlstein 	free(hostname, M_TEMP);
389578f57a9cSMark Johnston 	if (compress == COMPRESS_GZIP)
38966026dcd7SMark Johnston 		sbuf_printf(&sb, GZIP_SUFFIX);
38976026dcd7SMark Johnston 	else if (compress == COMPRESS_ZSTD)
38986026dcd7SMark Johnston 		sbuf_printf(&sb, ZSTD_SUFFIX);
38994d369413SMatthew D Fleming 	if (sbuf_error(&sb) != 0) {
390036b208e0SRobert Watson 		log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3901c52ff611SPawel Jakub Dawidek 		    "long\n", (long)pid, comm, (u_long)uid);
3902b7402d82SAlfred Perlstein 		sbuf_delete(&sb);
3903c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
3904c345faeaSPawel Jakub Dawidek 		return (ENOMEM);
3905c5edb423SSean Eric Fagan 	}
390636b208e0SRobert Watson 	sbuf_finish(&sb);
390736b208e0SRobert Watson 	sbuf_delete(&sb);
3908e7228204SAlfred Perlstein 
3909e7228204SAlfred Perlstein 	if (indexpos != -1) {
3910f1fe1e02SMariusz Zaborski 		error = corefile_open_last(td, name, indexpos, indexlen, ncores,
3911f1fe1e02SMariusz Zaborski 		    vpp);
39120dea6e3cSMariusz Zaborski 		if (error != 0) {
3913e7228204SAlfred Perlstein 			log(LOG_ERR,
3914e7228204SAlfred Perlstein 			    "pid %d (%s), uid (%u):  Path `%s' failed "
3915e7228204SAlfred Perlstein 			    "on initial open test, error = %d\n",
3916c52ff611SPawel Jakub Dawidek 			    pid, comm, uid, name, error);
3917c345faeaSPawel Jakub Dawidek 		}
39180dea6e3cSMariusz Zaborski 	} else {
39190dea6e3cSMariusz Zaborski 		cmode = S_IRUSR | S_IWUSR;
39200dea6e3cSMariusz Zaborski 		oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
39210dea6e3cSMariusz Zaborski 		    (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
39220dea6e3cSMariusz Zaborski 		flags = O_CREAT | FWRITE | O_NOFOLLOW;
392389f2ab06SKonstantin Belousov 		if ((td->td_proc->p_flag & P_SUGID) != 0)
392489f2ab06SKonstantin Belousov 			flags |= O_EXCL;
39250dea6e3cSMariusz Zaborski 
39267e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
39270dea6e3cSMariusz Zaborski 		error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
39280dea6e3cSMariusz Zaborski 		    NULL);
39290dea6e3cSMariusz Zaborski 		if (error == 0) {
39300dea6e3cSMariusz Zaborski 			*vpp = nd.ni_vp;
3931bb92cd7bSMateusz Guzik 			NDFREE_PNBUF(&nd);
3932c345faeaSPawel Jakub Dawidek 		}
3933c345faeaSPawel Jakub Dawidek 	}
3934c345faeaSPawel Jakub Dawidek 
39350dea6e3cSMariusz Zaborski 	if (error != 0) {
3936c345faeaSPawel Jakub Dawidek #ifdef AUDIT
3937c345faeaSPawel Jakub Dawidek 		audit_proc_coredump(td, name, error);
3938c345faeaSPawel Jakub Dawidek #endif
3939c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
3940c345faeaSPawel Jakub Dawidek 		return (error);
3941e7228204SAlfred Perlstein 	}
3942c345faeaSPawel Jakub Dawidek 	*namep = name;
3943c345faeaSPawel Jakub Dawidek 	return (0);
394436b208e0SRobert Watson }
3945c5edb423SSean Eric Fagan 
3946df8bae1dSRodney W. Grimes /*
3947fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
3948fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
3949fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
3950fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
3951fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
3952fca666a1SJulian Elischer  */
3953fca666a1SJulian Elischer 
3954fca666a1SJulian Elischer static int
3955b40ce416SJulian Elischer coredump(struct thread *td)
3956fca666a1SJulian Elischer {
3957b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3958f06f465dSPawel Jakub Dawidek 	struct ucred *cred = td->td_ucred;
3959f06f465dSPawel Jakub Dawidek 	struct vnode *vp;
396006ae1e91SMatthew Dillon 	struct flock lf;
3961fca666a1SJulian Elischer 	struct vattr vattr;
39627739d927SMateusz Guzik 	size_t fullpathsize;
3963c345faeaSPawel Jakub Dawidek 	int error, error1, locked;
3964fca666a1SJulian Elischer 	char *name;			/* name of corefile */
3965539c9eefSKonstantin Belousov 	void *rl_cookie;
3966fca666a1SJulian Elischer 	off_t limit;
3967842ab62bSRui Paulo 	char *fullpath, *freepath = NULL;
3968349fcda4SWarner Losh 	struct sbuf *sb;
3969fca666a1SJulian Elischer 
39704ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3971f97c3df1SDavid Schultz 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3972fca666a1SJulian Elischer 
3973677258f7SKonstantin Belousov 	if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3974677258f7SKonstantin Belousov 	    (p->p_flag2 & P2_NOTRACE) != 0) {
3975628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3976fca666a1SJulian Elischer 		return (EFAULT);
3977628d2653SJohn Baldwin 	}
3978fca666a1SJulian Elischer 
3979fca666a1SJulian Elischer 	/*
398035a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
398135a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
398235a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
398335a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
398435a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
398535a2598fSSean Eric Fagan 	 * if it is larger than the limit.
3986fca666a1SJulian Elischer 	 */
3987f6f6d240SMateusz Guzik 	limit = (off_t)lim_cur(td, RLIMIT_CORE);
3988b8fdb0d9SEdward Tomasz Napierala 	if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3989628d2653SJohn Baldwin 		PROC_UNLOCK(p);
399091d5354aSJohn Baldwin 		return (EFBIG);
399157274c51SChristian S.J. Peron 	}
3992b8fdb0d9SEdward Tomasz Napierala 	PROC_UNLOCK(p);
399335a2598fSSean Eric Fagan 
3994aa14e9b7SMark Johnston 	error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
39959d3ecb7eSEric van Gyzen 	    compress_user_cores, p->p_sig, &vp, &name);
3996c345faeaSPawel Jakub Dawidek 	if (error != 0)
3997fca666a1SJulian Elischer 		return (error);
399806ae1e91SMatthew Dillon 
3999539c9eefSKonstantin Belousov 	/*
4000539c9eefSKonstantin Belousov 	 * Don't dump to non-regular files or files with links.
400189f2ab06SKonstantin Belousov 	 * Do not dump into system files. Effective user must own the corefile.
4002539c9eefSKonstantin Belousov 	 */
4003f06f465dSPawel Jakub Dawidek 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
40047a29e0bfSKonstantin Belousov 	    vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
400589f2ab06SKonstantin Belousov 	    vattr.va_uid != cred->cr_uid) {
4006b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
4007832dafadSDon Lewis 		error = EFAULT;
4008dacbc9dbSKonstantin Belousov 		goto out;
4009832dafadSDon Lewis 	}
4010832dafadSDon Lewis 
4011b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
4012539c9eefSKonstantin Belousov 
4013539c9eefSKonstantin Belousov 	/* Postpone other writers, including core dumps of other processes. */
4014539c9eefSKonstantin Belousov 	rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
4015539c9eefSKonstantin Belousov 
401606ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
401706ae1e91SMatthew Dillon 	lf.l_start = 0;
401806ae1e91SMatthew Dillon 	lf.l_len = 0;
401906ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
4020c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
402106ae1e91SMatthew Dillon 
4022fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
4023fca666a1SJulian Elischer 	vattr.va_size = 0;
40246141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
40256141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
4026cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
40270359a12eSAttilio Rao 	VOP_SETATTR(vp, &vattr, cred);
4028b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
4029628d2653SJohn Baldwin 	PROC_LOCK(p);
4030fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
4031628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4032fca666a1SJulian Elischer 
403323c6445aSPawel Jakub Dawidek 	if (p->p_sysent->sv_coredump != NULL) {
403478f57a9cSMark Johnston 		error = p->p_sysent->sv_coredump(td, vp, limit, 0);
403523c6445aSPawel Jakub Dawidek 	} else {
403623c6445aSPawel Jakub Dawidek 		error = ENOSYS;
403723c6445aSPawel Jakub Dawidek 	}
4038fca666a1SJulian Elischer 
4039c447f5b2SRobert Watson 	if (locked) {
404006ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
404106ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
4042c447f5b2SRobert Watson 	}
4043539c9eefSKonstantin Belousov 	vn_rangelock_unlock(vp, rl_cookie);
4044dacbc9dbSKonstantin Belousov 
4045842ab62bSRui Paulo 	/*
4046842ab62bSRui Paulo 	 * Notify the userland helper that a process triggered a core dump.
4047842ab62bSRui Paulo 	 * This allows the helper to run an automated debugging session.
4048842ab62bSRui Paulo 	 */
4049dacbc9dbSKonstantin Belousov 	if (error != 0 || coredump_devctl == 0)
4050842ab62bSRui Paulo 		goto out;
4051349fcda4SWarner Losh 	sb = sbuf_new_auto();
4052feabaaf9SMateusz Guzik 	if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
4053349fcda4SWarner Losh 		goto out2;
4054349fcda4SWarner Losh 	sbuf_printf(sb, "comm=\"");
4055349fcda4SWarner Losh 	devctl_safe_quote_sb(sb, fullpath);
4056842ab62bSRui Paulo 	free(freepath, M_TEMP);
4057349fcda4SWarner Losh 	sbuf_printf(sb, "\" core=\"");
4058349fcda4SWarner Losh 
4059349fcda4SWarner Losh 	/*
4060349fcda4SWarner Losh 	 * We can't lookup core file vp directly. When we're replacing a core, and
4061349fcda4SWarner Losh 	 * other random times, we flush the name cache, so it will fail. Instead,
4062349fcda4SWarner Losh 	 * if the path of the core is relative, add the current dir in front if it.
4063349fcda4SWarner Losh 	 */
4064349fcda4SWarner Losh 	if (name[0] != '/') {
40657739d927SMateusz Guzik 		fullpathsize = MAXPATHLEN;
40667739d927SMateusz Guzik 		freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
4067feabaaf9SMateusz Guzik 		if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
40687739d927SMateusz Guzik 			free(freepath, M_TEMP);
4069349fcda4SWarner Losh 			goto out2;
4070349fcda4SWarner Losh 		}
4071349fcda4SWarner Losh 		devctl_safe_quote_sb(sb, fullpath);
40727739d927SMateusz Guzik 		free(freepath, M_TEMP);
4073349fcda4SWarner Losh 		sbuf_putc(sb, '/');
4074349fcda4SWarner Losh 	}
4075349fcda4SWarner Losh 	devctl_safe_quote_sb(sb, name);
4076349fcda4SWarner Losh 	sbuf_printf(sb, "\"");
4077349fcda4SWarner Losh 	if (sbuf_finish(sb) == 0)
4078349fcda4SWarner Losh 		devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
4079349fcda4SWarner Losh out2:
4080349fcda4SWarner Losh 	sbuf_delete(sb);
4081842ab62bSRui Paulo out:
4082dacbc9dbSKonstantin Belousov 	error1 = vn_close(vp, FWRITE, cred, td);
4083dacbc9dbSKonstantin Belousov 	if (error == 0)
4084dacbc9dbSKonstantin Belousov 		error = error1;
408557274c51SChristian S.J. Peron #ifdef AUDIT
408657274c51SChristian S.J. Peron 	audit_proc_coredump(td, name, error);
408757274c51SChristian S.J. Peron #endif
408857274c51SChristian S.J. Peron 	free(name, M_TEMP);
4089fca666a1SJulian Elischer 	return (error);
4090fca666a1SJulian Elischer }
4091fca666a1SJulian Elischer 
4092fca666a1SJulian Elischer /*
40930c14ff0eSRobert Watson  * Nonexistent system call-- signal process (may want to handle it).  Flag
40940c14ff0eSRobert Watson  * error in case process won't see signal immediately (blocked or ignored).
4095df8bae1dSRodney W. Grimes  */
4096d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
4097df8bae1dSRodney W. Grimes struct nosys_args {
4098df8bae1dSRodney W. Grimes 	int	dummy;
4099df8bae1dSRodney W. Grimes };
4100d2d3e875SBruce Evans #endif
4101df8bae1dSRodney W. Grimes /* ARGSUSED */
410226f9a767SRodney W. Grimes int
4103e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args)
4104df8bae1dSRodney W. Grimes {
4105f5a077c3SKonstantin Belousov 	struct proc *p;
4106f5a077c3SKonstantin Belousov 
4107f5a077c3SKonstantin Belousov 	p = td->td_proc;
4108b40ce416SJulian Elischer 
4109628d2653SJohn Baldwin 	PROC_LOCK(p);
4110888aefefSKonstantin Belousov 	tdsignal(td, SIGSYS);
4111628d2653SJohn Baldwin 	PROC_UNLOCK(p);
41127ceeb35bSKonstantin Belousov 	if (kern_lognosys == 1 || kern_lognosys == 3) {
4113f5a077c3SKonstantin Belousov 		uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4114f5a077c3SKonstantin Belousov 		    td->td_sa.code);
41157ceeb35bSKonstantin Belousov 	}
411618f917a9SBrooks Davis 	if (kern_lognosys == 2 || kern_lognosys == 3 ||
411718f917a9SBrooks Davis 	    (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
41187ceeb35bSKonstantin Belousov 		printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
41197ceeb35bSKonstantin Belousov 		    td->td_sa.code);
41207ceeb35bSKonstantin Belousov 	}
4121f5216b9aSBruce Evans 	return (ENOSYS);
4122df8bae1dSRodney W. Grimes }
4123831d27a9SDon Lewis 
4124831d27a9SDon Lewis /*
41250c14ff0eSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using stored
41260c14ff0eSRobert Watson  * credentials rather than those of the current process.
4127831d27a9SDon Lewis  */
4128831d27a9SDon Lewis void
4129e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty)
4130831d27a9SDon Lewis {
4131a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
4132f1320723SAlfred Perlstein 	struct sigio *sigio;
4133831d27a9SDon Lewis 
4134a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
4135a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
4136a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
4137a3de221dSKonstantin Belousov 
4138f1320723SAlfred Perlstein 	SIGIO_LOCK();
4139f1320723SAlfred Perlstein 	sigio = *sigiop;
4140f1320723SAlfred Perlstein 	if (sigio == NULL) {
4141f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
4142f1320723SAlfred Perlstein 		return;
4143f1320723SAlfred Perlstein 	}
4144831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
4145628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
41462b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
41478451d0ddSKip Macy 			kern_psignal(sigio->sio_proc, sig);
4148628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
4149831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
4150831d27a9SDon Lewis 		struct proc *p;
4151831d27a9SDon Lewis 
4152f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
4153628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4154628d2653SJohn Baldwin 			PROC_LOCK(p);
4155e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
4156e806d352SJohn Baldwin 			    CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4157831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
41588451d0ddSKip Macy 				kern_psignal(p, sig);
4159628d2653SJohn Baldwin 			PROC_UNLOCK(p);
4160628d2653SJohn Baldwin 		}
4161f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
4162831d27a9SDon Lewis 	}
4163f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
4164831d27a9SDon Lewis }
4165cb679c38SJonathan Lemon 
4166cb679c38SJonathan Lemon static int
4167cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
4168cb679c38SJonathan Lemon {
4169cb679c38SJonathan Lemon 	struct proc *p = curproc;
4170cb679c38SJonathan Lemon 
4171cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
4172cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
4173cb679c38SJonathan Lemon 
41749e590ff0SKonstantin Belousov 	knlist_add(p->p_klist, kn, 0);
4175cb679c38SJonathan Lemon 
4176cb679c38SJonathan Lemon 	return (0);
4177cb679c38SJonathan Lemon }
4178cb679c38SJonathan Lemon 
4179cb679c38SJonathan Lemon static void
4180cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
4181cb679c38SJonathan Lemon {
4182cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
4183cb679c38SJonathan Lemon 
41849e590ff0SKonstantin Belousov 	knlist_remove(p->p_klist, kn, 0);
4185cb679c38SJonathan Lemon }
4186cb679c38SJonathan Lemon 
4187cb679c38SJonathan Lemon /*
4188cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
4189cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
4190cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
4191cb679c38SJonathan Lemon  * isn't worth the trouble.
4192cb679c38SJonathan Lemon  */
4193cb679c38SJonathan Lemon static int
4194cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
4195cb679c38SJonathan Lemon {
4196cb679c38SJonathan Lemon 
4197cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
4198cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
4199cb679c38SJonathan Lemon 
4200cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
4201cb679c38SJonathan Lemon 			kn->kn_data++;
4202cb679c38SJonathan Lemon 	}
4203cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
4204cb679c38SJonathan Lemon }
420590af4afaSJohn Baldwin 
420690af4afaSJohn Baldwin struct sigacts *
420790af4afaSJohn Baldwin sigacts_alloc(void)
420890af4afaSJohn Baldwin {
420990af4afaSJohn Baldwin 	struct sigacts *ps;
421090af4afaSJohn Baldwin 
421190af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4212ce8daaadSMateusz Guzik 	refcount_init(&ps->ps_refcnt, 1);
421390af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
421490af4afaSJohn Baldwin 	return (ps);
421590af4afaSJohn Baldwin }
421690af4afaSJohn Baldwin 
421790af4afaSJohn Baldwin void
421890af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
421990af4afaSJohn Baldwin {
422090af4afaSJohn Baldwin 
4221c959c237SMateusz Guzik 	if (refcount_release(&ps->ps_refcnt) == 0)
4222c959c237SMateusz Guzik 		return;
422390af4afaSJohn Baldwin 	mtx_destroy(&ps->ps_mtx);
422490af4afaSJohn Baldwin 	free(ps, M_SUBPROC);
422590af4afaSJohn Baldwin }
422690af4afaSJohn Baldwin 
422790af4afaSJohn Baldwin struct sigacts *
422890af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
422990af4afaSJohn Baldwin {
4230c959c237SMateusz Guzik 
4231c959c237SMateusz Guzik 	refcount_acquire(&ps->ps_refcnt);
423290af4afaSJohn Baldwin 	return (ps);
423390af4afaSJohn Baldwin }
423490af4afaSJohn Baldwin 
423590af4afaSJohn Baldwin void
423690af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
423790af4afaSJohn Baldwin {
423890af4afaSJohn Baldwin 
423990af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
424090af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
424190af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
424290af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
424390af4afaSJohn Baldwin }
424490af4afaSJohn Baldwin 
424590af4afaSJohn Baldwin int
424690af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
424790af4afaSJohn Baldwin {
424890af4afaSJohn Baldwin 
4249d00c8ea4SMateusz Guzik 	return (ps->ps_refcnt > 1);
425090af4afaSJohn Baldwin }
4251079c5b9eSKyle Evans 
4252079c5b9eSKyle Evans void
4253079c5b9eSKyle Evans sig_drop_caught(struct proc *p)
4254079c5b9eSKyle Evans {
4255079c5b9eSKyle Evans 	int sig;
4256079c5b9eSKyle Evans 	struct sigacts *ps;
4257079c5b9eSKyle Evans 
4258079c5b9eSKyle Evans 	ps = p->p_sigacts;
4259079c5b9eSKyle Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
4260079c5b9eSKyle Evans 	mtx_assert(&ps->ps_mtx, MA_OWNED);
426181f2e906SMark Johnston 	SIG_FOREACH(sig, &ps->ps_sigcatch) {
4262079c5b9eSKyle Evans 		sigdflt(ps, sig);
4263079c5b9eSKyle Evans 		if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4264079c5b9eSKyle Evans 			sigqueue_delete_proc(p, sig);
4265079c5b9eSKyle Evans 	}
4266079c5b9eSKyle Evans }
4267146fc63fSKonstantin Belousov 
4268a113b17fSKonstantin Belousov static void
4269a113b17fSKonstantin Belousov sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4270a113b17fSKonstantin Belousov {
4271a113b17fSKonstantin Belousov 	ksiginfo_t ksi;
4272a113b17fSKonstantin Belousov 
4273a113b17fSKonstantin Belousov 	/*
4274a113b17fSKonstantin Belousov 	 * Prevent further fetches and SIGSEGVs, allowing thread to
4275a113b17fSKonstantin Belousov 	 * issue syscalls despite corruption.
4276a113b17fSKonstantin Belousov 	 */
4277a113b17fSKonstantin Belousov 	sigfastblock_clear(td);
4278a113b17fSKonstantin Belousov 
4279a113b17fSKonstantin Belousov 	if (!sendsig)
4280a113b17fSKonstantin Belousov 		return;
4281a113b17fSKonstantin Belousov 	ksiginfo_init_trap(&ksi);
4282a113b17fSKonstantin Belousov 	ksi.ksi_signo = SIGSEGV;
4283a113b17fSKonstantin Belousov 	ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4284a113b17fSKonstantin Belousov 	ksi.ksi_addr = td->td_sigblock_ptr;
4285a113b17fSKonstantin Belousov 	trapsignal(td, &ksi);
4286a113b17fSKonstantin Belousov }
4287a113b17fSKonstantin Belousov 
4288a113b17fSKonstantin Belousov static bool
4289a113b17fSKonstantin Belousov sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4290a113b17fSKonstantin Belousov {
4291a113b17fSKonstantin Belousov 	uint32_t res;
4292a113b17fSKonstantin Belousov 
4293a113b17fSKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4294a113b17fSKonstantin Belousov 		return (true);
4295a113b17fSKonstantin Belousov 	if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4296a113b17fSKonstantin Belousov 		sigfastblock_failed(td, sendsig, false);
4297a113b17fSKonstantin Belousov 		return (false);
4298a113b17fSKonstantin Belousov 	}
4299a113b17fSKonstantin Belousov 	*valp = res;
4300a113b17fSKonstantin Belousov 	td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4301a113b17fSKonstantin Belousov 	return (true);
4302a113b17fSKonstantin Belousov }
4303a113b17fSKonstantin Belousov 
4304fb3c434bSKonstantin Belousov static void
4305fb3c434bSKonstantin Belousov sigfastblock_resched(struct thread *td, bool resched)
4306fb3c434bSKonstantin Belousov {
4307fb3c434bSKonstantin Belousov 	struct proc *p;
4308fb3c434bSKonstantin Belousov 
4309fb3c434bSKonstantin Belousov 	if (resched) {
4310fb3c434bSKonstantin Belousov 		p = td->td_proc;
4311fb3c434bSKonstantin Belousov 		PROC_LOCK(p);
4312fb3c434bSKonstantin Belousov 		reschedule_signals(p, td->td_sigmask, 0);
4313fb3c434bSKonstantin Belousov 		PROC_UNLOCK(p);
4314fb3c434bSKonstantin Belousov 	}
4315c6d31b83SKonstantin Belousov 	ast_sched(td, TDA_SIG);
4316fb3c434bSKonstantin Belousov }
4317fb3c434bSKonstantin Belousov 
4318146fc63fSKonstantin Belousov int
4319146fc63fSKonstantin Belousov sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4320146fc63fSKonstantin Belousov {
4321146fc63fSKonstantin Belousov 	struct proc *p;
4322146fc63fSKonstantin Belousov 	int error, res;
4323146fc63fSKonstantin Belousov 	uint32_t oldval;
4324146fc63fSKonstantin Belousov 
4325146fc63fSKonstantin Belousov 	error = 0;
4326a113b17fSKonstantin Belousov 	p = td->td_proc;
4327146fc63fSKonstantin Belousov 	switch (uap->cmd) {
4328146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_SETPTR:
4329146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4330146fc63fSKonstantin Belousov 			error = EBUSY;
4331146fc63fSKonstantin Belousov 			break;
4332146fc63fSKonstantin Belousov 		}
4333146fc63fSKonstantin Belousov 		if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4334146fc63fSKonstantin Belousov 			error = EINVAL;
4335146fc63fSKonstantin Belousov 			break;
4336146fc63fSKonstantin Belousov 		}
4337146fc63fSKonstantin Belousov 		td->td_pflags |= TDP_SIGFASTBLOCK;
4338146fc63fSKonstantin Belousov 		td->td_sigblock_ptr = uap->ptr;
4339146fc63fSKonstantin Belousov 		break;
4340146fc63fSKonstantin Belousov 
4341146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_UNBLOCK:
4342a113b17fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4343146fc63fSKonstantin Belousov 			error = EINVAL;
4344146fc63fSKonstantin Belousov 			break;
4345146fc63fSKonstantin Belousov 		}
4346a113b17fSKonstantin Belousov 
4347a113b17fSKonstantin Belousov 		for (;;) {
4348a113b17fSKonstantin Belousov 			res = casueword32(td->td_sigblock_ptr,
4349a113b17fSKonstantin Belousov 			    SIGFASTBLOCK_PEND, &oldval, 0);
4350146fc63fSKonstantin Belousov 			if (res == -1) {
4351146fc63fSKonstantin Belousov 				error = EFAULT;
4352a113b17fSKonstantin Belousov 				sigfastblock_failed(td, false, true);
4353146fc63fSKonstantin Belousov 				break;
4354146fc63fSKonstantin Belousov 			}
4355a113b17fSKonstantin Belousov 			if (res == 0)
4356a113b17fSKonstantin Belousov 				break;
4357a113b17fSKonstantin Belousov 			MPASS(res == 1);
4358146fc63fSKonstantin Belousov 			if (oldval != SIGFASTBLOCK_PEND) {
4359146fc63fSKonstantin Belousov 				error = EBUSY;
4360146fc63fSKonstantin Belousov 				break;
4361146fc63fSKonstantin Belousov 			}
4362146fc63fSKonstantin Belousov 			error = thread_check_susp(td, false);
4363146fc63fSKonstantin Belousov 			if (error != 0)
4364146fc63fSKonstantin Belousov 				break;
4365146fc63fSKonstantin Belousov 		}
4366a113b17fSKonstantin Belousov 		if (error != 0)
4367a113b17fSKonstantin Belousov 			break;
4368a113b17fSKonstantin Belousov 
4369a113b17fSKonstantin Belousov 		/*
4370a113b17fSKonstantin Belousov 		 * td_sigblock_val is cleared there, but not on a
4371a113b17fSKonstantin Belousov 		 * syscall exit.  The end effect is that a single
4372a113b17fSKonstantin Belousov 		 * interruptible sleep, while user sigblock word is
4373a113b17fSKonstantin Belousov 		 * set, might return EINTR or ERESTART to usermode
4374a113b17fSKonstantin Belousov 		 * without delivering signal.  All further sleeps,
4375a113b17fSKonstantin Belousov 		 * until userspace clears the word and does
4376a113b17fSKonstantin Belousov 		 * sigfastblock(UNBLOCK), observe current word and no
4377a113b17fSKonstantin Belousov 		 * longer get interrupted.  It is slight
4378a113b17fSKonstantin Belousov 		 * non-conformance, with alternative to have read the
4379a113b17fSKonstantin Belousov 		 * sigblock word on each syscall entry.
4380a113b17fSKonstantin Belousov 		 */
4381146fc63fSKonstantin Belousov 		td->td_sigblock_val = 0;
4382146fc63fSKonstantin Belousov 
4383146fc63fSKonstantin Belousov 		/*
4384146fc63fSKonstantin Belousov 		 * Rely on normal ast mechanism to deliver pending
4385146fc63fSKonstantin Belousov 		 * signals to current thread.  But notify others about
4386146fc63fSKonstantin Belousov 		 * fake unblock.
4387146fc63fSKonstantin Belousov 		 */
4388fb3c434bSKonstantin Belousov 		sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4389fb3c434bSKonstantin Belousov 
4390146fc63fSKonstantin Belousov 		break;
4391146fc63fSKonstantin Belousov 
4392146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_UNSETPTR:
4393146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4394146fc63fSKonstantin Belousov 			error = EINVAL;
4395146fc63fSKonstantin Belousov 			break;
4396146fc63fSKonstantin Belousov 		}
4397a113b17fSKonstantin Belousov 		if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4398146fc63fSKonstantin Belousov 			error = EFAULT;
4399146fc63fSKonstantin Belousov 			break;
4400146fc63fSKonstantin Belousov 		}
4401146fc63fSKonstantin Belousov 		if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4402146fc63fSKonstantin Belousov 			error = EBUSY;
4403146fc63fSKonstantin Belousov 			break;
4404146fc63fSKonstantin Belousov 		}
4405a113b17fSKonstantin Belousov 		sigfastblock_clear(td);
4406146fc63fSKonstantin Belousov 		break;
4407146fc63fSKonstantin Belousov 
4408146fc63fSKonstantin Belousov 	default:
4409146fc63fSKonstantin Belousov 		error = EINVAL;
4410146fc63fSKonstantin Belousov 		break;
4411146fc63fSKonstantin Belousov 	}
4412146fc63fSKonstantin Belousov 	return (error);
4413146fc63fSKonstantin Belousov }
4414146fc63fSKonstantin Belousov 
4415146fc63fSKonstantin Belousov void
4416a113b17fSKonstantin Belousov sigfastblock_clear(struct thread *td)
4417146fc63fSKonstantin Belousov {
4418a113b17fSKonstantin Belousov 	bool resched;
4419146fc63fSKonstantin Belousov 
4420146fc63fSKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4421146fc63fSKonstantin Belousov 		return;
4422a113b17fSKonstantin Belousov 	td->td_sigblock_val = 0;
442300ebd809SKonstantin Belousov 	resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
442400ebd809SKonstantin Belousov 	    SIGPENDING(td);
4425a113b17fSKonstantin Belousov 	td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4426fb3c434bSKonstantin Belousov 	sigfastblock_resched(td, resched);
4427146fc63fSKonstantin Belousov }
4428146fc63fSKonstantin Belousov 
4429146fc63fSKonstantin Belousov void
4430a113b17fSKonstantin Belousov sigfastblock_fetch(struct thread *td)
4431146fc63fSKonstantin Belousov {
4432a113b17fSKonstantin Belousov 	uint32_t val;
4433146fc63fSKonstantin Belousov 
4434a113b17fSKonstantin Belousov 	(void)sigfastblock_fetch_sig(td, true, &val);
4435a113b17fSKonstantin Belousov }
4436146fc63fSKonstantin Belousov 
44370bc52b0bSKonstantin Belousov static void
44380bc52b0bSKonstantin Belousov sigfastblock_setpend1(struct thread *td)
4439a113b17fSKonstantin Belousov {
4440a113b17fSKonstantin Belousov 	int res;
4441a113b17fSKonstantin Belousov 	uint32_t oldval;
4442a113b17fSKonstantin Belousov 
4443513320c0SKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4444a113b17fSKonstantin Belousov 		return;
4445a113b17fSKonstantin Belousov 	res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4446a113b17fSKonstantin Belousov 	if (res == -1) {
4447a113b17fSKonstantin Belousov 		sigfastblock_failed(td, true, false);
4448a113b17fSKonstantin Belousov 		return;
4449a113b17fSKonstantin Belousov 	}
4450a113b17fSKonstantin Belousov 	for (;;) {
4451a113b17fSKonstantin Belousov 		res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4452a113b17fSKonstantin Belousov 		    oldval | SIGFASTBLOCK_PEND);
4453a113b17fSKonstantin Belousov 		if (res == -1) {
4454a113b17fSKonstantin Belousov 			sigfastblock_failed(td, true, true);
4455a113b17fSKonstantin Belousov 			return;
4456a113b17fSKonstantin Belousov 		}
4457a113b17fSKonstantin Belousov 		if (res == 0) {
4458a113b17fSKonstantin Belousov 			td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4459a113b17fSKonstantin Belousov 			td->td_pflags &= ~TDP_SIGFASTPENDING;
4460a113b17fSKonstantin Belousov 			break;
4461a113b17fSKonstantin Belousov 		}
4462a113b17fSKonstantin Belousov 		MPASS(res == 1);
4463a113b17fSKonstantin Belousov 		if (thread_check_susp(td, false) != 0)
4464a113b17fSKonstantin Belousov 			break;
4465a113b17fSKonstantin Belousov 	}
4466146fc63fSKonstantin Belousov }
44670bc52b0bSKonstantin Belousov 
44684fced864SKonstantin Belousov static void
44690bc52b0bSKonstantin Belousov sigfastblock_setpend(struct thread *td, bool resched)
44700bc52b0bSKonstantin Belousov {
44710bc52b0bSKonstantin Belousov 	struct proc *p;
44720bc52b0bSKonstantin Belousov 
44730bc52b0bSKonstantin Belousov 	sigfastblock_setpend1(td);
44740bc52b0bSKonstantin Belousov 	if (resched) {
44750bc52b0bSKonstantin Belousov 		p = td->td_proc;
44760bc52b0bSKonstantin Belousov 		PROC_LOCK(p);
44770bc52b0bSKonstantin Belousov 		reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);
44780bc52b0bSKonstantin Belousov 		PROC_UNLOCK(p);
44790bc52b0bSKonstantin Belousov 	}
44800bc52b0bSKonstantin Belousov }
4481