xref: /freebsd/sys/kern/kern_sig.c (revision fd5bc306ff3d8f908f36703d6ab714322f9f3c75)
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 
37140ceb5dSKonstantin Belousov #include "opt_capsicum.h"
38db6a20e2SGarrett Wollman #include "opt_ktrace.h"
39db6a20e2SGarrett Wollman 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41140ceb5dSKonstantin Belousov #include <sys/capsicum.h>
42eb6368d4SRui Paulo #include <sys/ctype.h>
4336240ea5SDoug Rabson #include <sys/systm.h>
44df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
45df8bae1dSRodney W. Grimes #include <sys/vnode.h>
46df8bae1dSRodney W. Grimes #include <sys/acct.h>
474a144410SRobert Watson #include <sys/capsicum.h>
4878f57a9cSMark Johnston #include <sys/compressor.h>
49238510fcSJason Evans #include <sys/condvar.h>
50773e541eSWarner Losh #include <sys/devctl.h>
51854dc8c2SJohn Baldwin #include <sys/event.h>
52854dc8c2SJohn Baldwin #include <sys/fcntl.h>
53e7228204SAlfred Perlstein #include <sys/imgact.h>
54437e1e37SBrooks Davis #include <sys/jail.h>
55854dc8c2SJohn Baldwin #include <sys/kernel.h>
560384fff8SJason Evans #include <sys/ktr.h>
57df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
5891898857SMark Johnston #include <sys/limits.h>
59854dc8c2SJohn Baldwin #include <sys/lock.h>
60854dc8c2SJohn Baldwin #include <sys/malloc.h>
61854dc8c2SJohn Baldwin #include <sys/mutex.h>
62c959c237SMateusz Guzik #include <sys/refcount.h>
63854dc8c2SJohn Baldwin #include <sys/namei.h>
64854dc8c2SJohn Baldwin #include <sys/proc.h>
65cfb5f768SJonathan Anderson #include <sys/procdesc.h>
66ef401a85SKonstantin Belousov #include <sys/ptrace.h>
676aeb05d7STom Rhodes #include <sys/posix4.h>
68b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h>
69c31146a1SJohn Baldwin #include <sys/resourcevar.h>
705d217f17SJohn Birrell #include <sys/sdt.h>
7136b208e0SRobert Watson #include <sys/sbuf.h>
7244f3b092SJohn Baldwin #include <sys/sleepqueue.h>
736caa8a15SJohn Baldwin #include <sys/smp.h>
74df8bae1dSRodney W. Grimes #include <sys/stat.h>
751005a129SJohn Baldwin #include <sys/sx.h>
76140ceb5dSKonstantin Belousov #include <sys/syscall.h>
778f19eb88SIan Dowse #include <sys/syscallsubr.h>
78c87e2930SDavid Greenman #include <sys/sysctl.h>
79854dc8c2SJohn Baldwin #include <sys/sysent.h>
80854dc8c2SJohn Baldwin #include <sys/syslog.h>
81854dc8c2SJohn Baldwin #include <sys/sysproto.h>
8256c06c4bSDavid Xu #include <sys/timers.h>
8306ae1e91SMatthew Dillon #include <sys/unistd.h>
84140ceb5dSKonstantin Belousov #include <sys/vmmeter.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 
90df8bae1dSRodney W. Grimes #include <machine/cpu.h>
91df8bae1dSRodney W. Grimes 
92bfd7575aSWayne Salamon #include <security/audit/audit.h>
93bfd7575aSWayne Salamon 
946f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
956f841fb7SMarcel Moolenaar 
965d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
9736160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__send,
9836160958SMark Johnston     "struct thread *", "struct proc *", "int");
9936160958SMark Johnston SDT_PROBE_DEFINE2(proc, , , signal__clear,
10036160958SMark Johnston     "int", "ksiginfo_t *");
10136160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__discard,
1027b77e1feSMark Johnston     "struct thread *", "struct proc *", "int");
1035d217f17SJohn Birrell 
1044d77a549SAlfred Perlstein static int	coredump(struct thread *);
105a3de221dSKonstantin Belousov static int	killpg1(struct thread *td, int sig, int pgid, int all,
106a3de221dSKonstantin Belousov 		    ksiginfo_t *ksi);
1073cf3b9f0SJohn Baldwin static int	issignal(struct thread *td);
1080bc52b0bSKonstantin Belousov static void	reschedule_signals(struct proc *p, sigset_t block, int flags);
1094d77a549SAlfred Perlstein static int	sigprop(int sig);
11094f0972bSDavid Xu static void	tdsigwakeup(struct thread *, int, sig_t, int);
111d4c4ca85SMark Johnston static void	sig_suspend_threads(struct thread *, struct proc *);
112cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
113cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
114cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
115146fc63fSKonstantin Belousov static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
1169104847fSDavid Xu static void	sigqueue_start(void);
1174fced864SKonstantin Belousov static void	sigfastblock_setpend(struct thread *td, bool resched);
118cb679c38SJonathan Lemon 
1199104847fSDavid Xu static uma_zone_t	ksiginfo_zone = NULL;
120ef9ffb85SMark Johnston const struct filterops sig_filtops = {
121e76d823bSRobert Watson 	.f_isfd = 0,
122e76d823bSRobert Watson 	.f_attach = filt_sigattach,
123e76d823bSRobert Watson 	.f_detach = filt_sigdetach,
124e76d823bSRobert Watson 	.f_event = filt_signal,
125e76d823bSRobert Watson };
126cb679c38SJonathan Lemon 
127fc8cca02SJohn Baldwin static int	kern_logsigexit = 1;
1283d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1293d177f46SBill Fumerola     &kern_logsigexit, 0,
1303d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
13157308494SJoerg Wunsch 
132f71a882fSDavid Xu static int	kern_forcesigexit = 1;
133f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
134f71a882fSDavid Xu     &kern_forcesigexit, 0, "Force trap signal to be handled");
135f71a882fSDavid Xu 
1367029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1376472ac3dSEd Schouten     "POSIX real time signal");
1389104847fSDavid Xu 
1399104847fSDavid Xu static int	max_pending_per_proc = 128;
1409104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
1419104847fSDavid Xu     &max_pending_per_proc, 0, "Max pending signals per proc");
1429104847fSDavid Xu 
1439104847fSDavid Xu static int	preallocate_siginfo = 1024;
144af3b2549SHans Petter Selasky SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
1459104847fSDavid Xu     &preallocate_siginfo, 0, "Preallocated signal memory size");
1469104847fSDavid Xu 
1479104847fSDavid Xu static int	signal_overflow = 0;
148761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
1499104847fSDavid Xu     &signal_overflow, 0, "Number of signals overflew");
1509104847fSDavid Xu 
1519104847fSDavid Xu static int	signal_alloc_fail = 0;
152761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
1539104847fSDavid Xu     &signal_alloc_fail, 0, "signals failed to be allocated");
1549104847fSDavid Xu 
155f5a077c3SKonstantin Belousov static int	kern_lognosys = 0;
156f5a077c3SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
157f5a077c3SKonstantin Belousov     "Log invalid syscalls");
158f5a077c3SKonstantin Belousov 
1595804a162SKonstantin Belousov static int	kern_signosys = 1;
1605804a162SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, signosys, CTLFLAG_RWTUN, &kern_signosys, 0,
1615804a162SKonstantin Belousov     "Send SIGSYS on return from invalid syscall");
1625804a162SKonstantin Belousov 
163a113b17fSKonstantin Belousov __read_frequently bool sigfastblock_fetch_always = false;
164a113b17fSKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
165a113b17fSKonstantin Belousov     &sigfastblock_fetch_always, 0,
166a113b17fSKonstantin Belousov     "Fetch sigfastblock word on each syscall entry for proper "
167a113b17fSKonstantin Belousov     "blocking semantic");
168a113b17fSKonstantin Belousov 
169bc387624SKonstantin Belousov static bool	kern_sig_discard_ign = true;
170bc387624SKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
171bc387624SKonstantin Belousov     &kern_sig_discard_ign, 0,
172bc387624SKonstantin Belousov     "Discard ignored signals on delivery, otherwise queue them to "
173bc387624SKonstantin Belousov     "the target queue");
174bc387624SKonstantin Belousov 
1759104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
1769104847fSDavid Xu 
1772b87b6d4SRobert Watson /*
1782b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1792b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1802b87b6d4SRobert Watson  * in the right situations.
1812b87b6d4SRobert Watson  */
1822b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1832b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1842b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1852b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1862b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1872b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1882b87b6d4SRobert Watson 
189fc8cca02SJohn Baldwin static int	sugid_coredump;
190af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
1918b5adf9dSJoseph Koshy     &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
192c87e2930SDavid Greenman 
193b0c9d4d7SPawel Jakub Dawidek static int	capmode_coredump;
194af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
195b0c9d4d7SPawel Jakub Dawidek     &capmode_coredump, 0, "Allow processes in capability mode to dump core");
196b0c9d4d7SPawel Jakub Dawidek 
197e5a28db9SPaul Saab static int	do_coredump = 1;
198e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
199e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
200e5a28db9SPaul Saab 
2016141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
2026141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
2036141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
2046141e04aSJohn-Mark Gurney 
2050da9e11bSRui Paulo static int	coredump_devctl = 0;
206eb6368d4SRui Paulo SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
207eb6368d4SRui Paulo 	0, "Generate a devctl notification when processes coredump");
208eb6368d4SRui Paulo 
2092c42a146SMarcel Moolenaar /*
2102c42a146SMarcel Moolenaar  * Signal properties and actions.
2112c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
2122c42a146SMarcel Moolenaar  * according to the following properties:
2132c42a146SMarcel Moolenaar  */
214fd50a707SBrooks Davis #define	SIGPROP_KILL		0x01	/* terminates process by default */
215fd50a707SBrooks Davis #define	SIGPROP_CORE		0x02	/* ditto and coredumps */
216fd50a707SBrooks Davis #define	SIGPROP_STOP		0x04	/* suspend process */
217fd50a707SBrooks Davis #define	SIGPROP_TTYSTOP		0x08	/* ditto, from tty */
218fd50a707SBrooks Davis #define	SIGPROP_IGNORE		0x10	/* ignore by default */
219fd50a707SBrooks Davis #define	SIGPROP_CONT		0x20	/* continue if suspended */
220df8bae1dSRodney W. Grimes 
2210d3f1b4fSMark Johnston static const int sigproptbl[NSIG] = {
222ed6d876bSBrooks Davis 	[SIGHUP] =	SIGPROP_KILL,
223ed6d876bSBrooks Davis 	[SIGINT] =	SIGPROP_KILL,
224ed6d876bSBrooks Davis 	[SIGQUIT] =	SIGPROP_KILL | SIGPROP_CORE,
225ed6d876bSBrooks Davis 	[SIGILL] =	SIGPROP_KILL | SIGPROP_CORE,
226ed6d876bSBrooks Davis 	[SIGTRAP] =	SIGPROP_KILL | SIGPROP_CORE,
227ed6d876bSBrooks Davis 	[SIGABRT] =	SIGPROP_KILL | SIGPROP_CORE,
228ed6d876bSBrooks Davis 	[SIGEMT] =	SIGPROP_KILL | SIGPROP_CORE,
229ed6d876bSBrooks Davis 	[SIGFPE] =	SIGPROP_KILL | SIGPROP_CORE,
230ed6d876bSBrooks Davis 	[SIGKILL] =	SIGPROP_KILL,
231ed6d876bSBrooks Davis 	[SIGBUS] =	SIGPROP_KILL | SIGPROP_CORE,
232ed6d876bSBrooks Davis 	[SIGSEGV] =	SIGPROP_KILL | SIGPROP_CORE,
233ed6d876bSBrooks Davis 	[SIGSYS] =	SIGPROP_KILL | SIGPROP_CORE,
234ed6d876bSBrooks Davis 	[SIGPIPE] =	SIGPROP_KILL,
235ed6d876bSBrooks Davis 	[SIGALRM] =	SIGPROP_KILL,
236ed6d876bSBrooks Davis 	[SIGTERM] =	SIGPROP_KILL,
237ed6d876bSBrooks Davis 	[SIGURG] =	SIGPROP_IGNORE,
238ed6d876bSBrooks Davis 	[SIGSTOP] =	SIGPROP_STOP,
239ed6d876bSBrooks Davis 	[SIGTSTP] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
240ed6d876bSBrooks Davis 	[SIGCONT] =	SIGPROP_IGNORE | SIGPROP_CONT,
241ed6d876bSBrooks Davis 	[SIGCHLD] =	SIGPROP_IGNORE,
242ed6d876bSBrooks Davis 	[SIGTTIN] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
243ed6d876bSBrooks Davis 	[SIGTTOU] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
244ed6d876bSBrooks Davis 	[SIGIO] =	SIGPROP_IGNORE,
245ed6d876bSBrooks Davis 	[SIGXCPU] =	SIGPROP_KILL,
246ed6d876bSBrooks Davis 	[SIGXFSZ] =	SIGPROP_KILL,
247ed6d876bSBrooks Davis 	[SIGVTALRM] =	SIGPROP_KILL,
248ed6d876bSBrooks Davis 	[SIGPROF] =	SIGPROP_KILL,
249ed6d876bSBrooks Davis 	[SIGWINCH] =	SIGPROP_IGNORE,
250ed6d876bSBrooks Davis 	[SIGINFO] =	SIGPROP_IGNORE,
251ed6d876bSBrooks Davis 	[SIGUSR1] =	SIGPROP_KILL,
252ed6d876bSBrooks Davis 	[SIGUSR2] =	SIGPROP_KILL,
2532c42a146SMarcel Moolenaar };
2542c42a146SMarcel Moolenaar 
25581f2e906SMark Johnston #define	_SIG_FOREACH_ADVANCE(i, set) ({					\
25681f2e906SMark Johnston 	int __found;							\
25781f2e906SMark Johnston 	for (;;) {							\
25881f2e906SMark Johnston 		if (__bits != 0) {					\
25981f2e906SMark Johnston 			int __sig = ffs(__bits);			\
26081f2e906SMark Johnston 			__bits &= ~(1u << (__sig - 1));			\
26181f2e906SMark Johnston 			sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
26281f2e906SMark Johnston 			__found = 1;					\
26381f2e906SMark Johnston 			break;						\
26481f2e906SMark Johnston 		}							\
26581f2e906SMark Johnston 		if (++__i == _SIG_WORDS) {				\
26681f2e906SMark Johnston 			__found = 0;					\
26781f2e906SMark Johnston 			break;						\
26881f2e906SMark Johnston 		}							\
26981f2e906SMark Johnston 		__bits = (set)->__bits[__i];				\
27081f2e906SMark Johnston 	}								\
27181f2e906SMark Johnston 	__found != 0;							\
27281f2e906SMark Johnston })
27381f2e906SMark Johnston 
27481f2e906SMark Johnston #define	SIG_FOREACH(i, set)						\
27581f2e906SMark Johnston 	for (int32_t __i = -1, __bits = 0;				\
27681f2e906SMark Johnston 	    _SIG_FOREACH_ADVANCE(i, set); )				\
27781f2e906SMark Johnston 
2784fced864SKonstantin Belousov static sigset_t fastblock_mask;
2796b286ee8SKonstantin Belousov 
2809104847fSDavid Xu static void
ast_sig(struct thread * td,int tda)281c6d31b83SKonstantin Belousov ast_sig(struct thread *td, int tda)
282c6d31b83SKonstantin Belousov {
283c6d31b83SKonstantin Belousov 	struct proc *p;
284f0592b3cSKonstantin Belousov 	int old_boundary, sig;
285c6d31b83SKonstantin Belousov 	bool resched_sigs;
286c6d31b83SKonstantin Belousov 
287c6d31b83SKonstantin Belousov 	p = td->td_proc;
288c6d31b83SKonstantin Belousov 
289c6d31b83SKonstantin Belousov #ifdef DIAGNOSTIC
290c6d31b83SKonstantin Belousov 	if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) |
291c6d31b83SKonstantin Belousov 	    TDAI(TDA_AST))) == 0) {
292c6d31b83SKonstantin Belousov 		PROC_LOCK(p);
293c6d31b83SKonstantin Belousov 		thread_lock(td);
294c6d31b83SKonstantin Belousov 		/*
295c6d31b83SKonstantin Belousov 		 * Note that TDA_SIG should be re-read from
296c6d31b83SKonstantin Belousov 		 * td_ast, since signal might have been delivered
297c6d31b83SKonstantin Belousov 		 * after we cleared td_flags above.  This is one of
298c6d31b83SKonstantin Belousov 		 * the reason for looping check for AST condition.
299c6d31b83SKonstantin Belousov 		 * See comment in userret() about P_PPWAIT.
300c6d31b83SKonstantin Belousov 		 */
301c6d31b83SKonstantin Belousov 		if ((p->p_flag & P_PPWAIT) == 0 &&
302c6d31b83SKonstantin Belousov 		    (td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
303c6d31b83SKonstantin Belousov 			if (SIGPENDING(td) && ((tda | td->td_ast) &
304c6d31b83SKonstantin Belousov 			    (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) {
305c6d31b83SKonstantin Belousov 				thread_unlock(td); /* fix dumps */
306c6d31b83SKonstantin Belousov 				panic(
307c6d31b83SKonstantin Belousov 				    "failed2 to set signal flags for ast p %p "
308c6d31b83SKonstantin Belousov 				    "td %p tda %#x td_ast %#x fl %#x",
309c6d31b83SKonstantin Belousov 				    p, td, tda, td->td_ast, td->td_flags);
310c6d31b83SKonstantin Belousov 			}
311c6d31b83SKonstantin Belousov 		}
312c6d31b83SKonstantin Belousov 		thread_unlock(td);
313c6d31b83SKonstantin Belousov 		PROC_UNLOCK(p);
314c6d31b83SKonstantin Belousov 	}
315c6d31b83SKonstantin Belousov #endif
316c6d31b83SKonstantin Belousov 
317c6d31b83SKonstantin Belousov 	/*
318c6d31b83SKonstantin Belousov 	 * Check for signals. Unlocked reads of p_pendingcnt or
319c6d31b83SKonstantin Belousov 	 * p_siglist might cause process-directed signal to be handled
320c6d31b83SKonstantin Belousov 	 * later.
321c6d31b83SKonstantin Belousov 	 */
322f2fd7d8bSKonstantin Belousov 	if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 ||
323c6d31b83SKonstantin Belousov 	    !SIGISEMPTY(p->p_siglist)) {
324c6d31b83SKonstantin Belousov 		sigfastblock_fetch(td);
325c6d31b83SKonstantin Belousov 		PROC_LOCK(p);
326f0592b3cSKonstantin Belousov 		old_boundary = ~TDB_BOUNDARY | (td->td_dbgflags & TDB_BOUNDARY);
327f0592b3cSKonstantin Belousov 		td->td_dbgflags |= TDB_BOUNDARY;
328c6d31b83SKonstantin Belousov 		mtx_lock(&p->p_sigacts->ps_mtx);
329c6d31b83SKonstantin Belousov 		while ((sig = cursig(td)) != 0) {
330c6d31b83SKonstantin Belousov 			KASSERT(sig >= 0, ("sig %d", sig));
331c6d31b83SKonstantin Belousov 			postsig(sig);
332c6d31b83SKonstantin Belousov 		}
333c6d31b83SKonstantin Belousov 		mtx_unlock(&p->p_sigacts->ps_mtx);
334f0592b3cSKonstantin Belousov 		td->td_dbgflags &= old_boundary;
335c6d31b83SKonstantin Belousov 		PROC_UNLOCK(p);
336c6d31b83SKonstantin Belousov 		resched_sigs = true;
337c6d31b83SKonstantin Belousov 	} else {
338c6d31b83SKonstantin Belousov 		resched_sigs = false;
339c6d31b83SKonstantin Belousov 	}
340c6d31b83SKonstantin Belousov 
341c6d31b83SKonstantin Belousov 	/*
342c6d31b83SKonstantin Belousov 	 * Handle deferred update of the fast sigblock value, after
343c6d31b83SKonstantin Belousov 	 * the postsig() loop was performed.
344c6d31b83SKonstantin Belousov 	 */
345c6d31b83SKonstantin Belousov 	sigfastblock_setpend(td, resched_sigs);
346*fd5bc306SKonstantin Belousov 
347*fd5bc306SKonstantin Belousov 	/*
348*fd5bc306SKonstantin Belousov 	 * Clear td_sa.code: signal to ptrace that syscall arguments
349*fd5bc306SKonstantin Belousov 	 * are unavailable after this point. This AST handler is the
350*fd5bc306SKonstantin Belousov 	 * last chance for ptracestop() to signal the tracer before
351*fd5bc306SKonstantin Belousov 	 * the tracee returns to userspace.
352*fd5bc306SKonstantin Belousov 	 */
353*fd5bc306SKonstantin Belousov 	td->td_sa.code = 0;
354c6d31b83SKonstantin Belousov }
355c6d31b83SKonstantin Belousov 
356c6d31b83SKonstantin Belousov static void
ast_sigsuspend(struct thread * td,int tda __unused)357c6d31b83SKonstantin Belousov ast_sigsuspend(struct thread *td, int tda __unused)
358c6d31b83SKonstantin Belousov {
359c6d31b83SKonstantin Belousov 	MPASS((td->td_pflags & TDP_OLDMASK) != 0);
360c6d31b83SKonstantin Belousov 	td->td_pflags &= ~TDP_OLDMASK;
361c6d31b83SKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
362c6d31b83SKonstantin Belousov }
363c6d31b83SKonstantin Belousov 
364c6d31b83SKonstantin Belousov static void
sigqueue_start(void)3659104847fSDavid Xu sigqueue_start(void)
3669104847fSDavid Xu {
3679104847fSDavid Xu 	ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
3689104847fSDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3699104847fSDavid Xu 	uma_prealloc(ksiginfo_zone, preallocate_siginfo);
370b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
371b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
372b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
373146fc63fSKonstantin Belousov 	SIGFILLSET(fastblock_mask);
374146fc63fSKonstantin Belousov 	SIG_CANTMASK(fastblock_mask);
375c6d31b83SKonstantin Belousov 	ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig);
376cab31f56SKyle Evans 
377cab31f56SKyle Evans 	/*
378cab31f56SKyle Evans 	 * TDA_PSELECT is for the case where the signal mask should be restored
379cab31f56SKyle Evans 	 * before delivering any signals so that we do not deliver any that are
380cab31f56SKyle Evans 	 * blocked by the normal thread mask.  It is mutually exclusive with
381cab31f56SKyle Evans 	 * TDA_SIGSUSPEND, which should be used if we *do* want to deliver
382cab31f56SKyle Evans 	 * signals that are normally blocked, e.g., if it interrupted our sleep.
383cab31f56SKyle Evans 	 */
384cab31f56SKyle Evans 	ast_register(TDA_PSELECT, ASTR_ASTF_REQUIRED | ASTR_TDP,
385cab31f56SKyle Evans 	    TDP_OLDMASK, ast_sigsuspend);
386c6d31b83SKonstantin Belousov 	ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP,
387c6d31b83SKonstantin Belousov 	    TDP_OLDMASK, ast_sigsuspend);
3889104847fSDavid Xu }
3899104847fSDavid Xu 
3905da49fcbSDavid Xu ksiginfo_t *
ksiginfo_alloc(int mwait)391cc29f221SKonstantin Belousov ksiginfo_alloc(int mwait)
3929104847fSDavid Xu {
393cc29f221SKonstantin Belousov 	MPASS(mwait == M_WAITOK || mwait == M_NOWAIT);
394ebceaf6dSDavid Xu 
395cc29f221SKonstantin Belousov 	if (ksiginfo_zone == NULL)
3969104847fSDavid Xu 		return (NULL);
397cc29f221SKonstantin Belousov 	return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO));
3989104847fSDavid Xu }
3999104847fSDavid Xu 
4005da49fcbSDavid Xu void
ksiginfo_free(ksiginfo_t * ksi)4019104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi)
4029104847fSDavid Xu {
4039104847fSDavid Xu 	uma_zfree(ksiginfo_zone, ksi);
4049104847fSDavid Xu }
4059104847fSDavid Xu 
406cdb58f9dSKonstantin Belousov static __inline bool
ksiginfo_tryfree(ksiginfo_t * ksi)4075da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi)
4085da49fcbSDavid Xu {
409cdb58f9dSKonstantin Belousov 	if ((ksi->ksi_flags & KSI_EXT) == 0) {
4105da49fcbSDavid Xu 		uma_zfree(ksiginfo_zone, ksi);
411cdb58f9dSKonstantin Belousov 		return (true);
4125da49fcbSDavid Xu 	}
413cdb58f9dSKonstantin Belousov 	return (false);
4145da49fcbSDavid Xu }
4155da49fcbSDavid Xu 
4169104847fSDavid Xu void
sigqueue_init(sigqueue_t * list,struct proc * p)4179104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p)
4189104847fSDavid Xu {
4199104847fSDavid Xu 	SIGEMPTYSET(list->sq_signals);
4203dfcaad6SDavid Xu 	SIGEMPTYSET(list->sq_kill);
42182a4538fSEric Badger 	SIGEMPTYSET(list->sq_ptrace);
4229104847fSDavid Xu 	TAILQ_INIT(&list->sq_list);
4239104847fSDavid Xu 	list->sq_proc = p;
4249104847fSDavid Xu 	list->sq_flags = SQ_INIT;
4259104847fSDavid Xu }
4269104847fSDavid Xu 
4279104847fSDavid Xu /*
4289104847fSDavid Xu  * Get a signal's ksiginfo.
4299104847fSDavid Xu  * Return:
4309104847fSDavid Xu  *	0	-	signal not found
4319104847fSDavid Xu  *	others	-	signal number
4329104847fSDavid Xu  */
433a5799a4fSKonstantin Belousov static int
sigqueue_get(sigqueue_t * sq,int signo,ksiginfo_t * si)4349104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
4359104847fSDavid Xu {
4369104847fSDavid Xu 	struct proc *p = sq->sq_proc;
4379104847fSDavid Xu 	struct ksiginfo *ksi, *next;
4389104847fSDavid Xu 	int count = 0;
4399104847fSDavid Xu 
4409104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
4419104847fSDavid Xu 
4429104847fSDavid Xu 	if (!SIGISMEMBER(sq->sq_signals, signo))
4439104847fSDavid Xu 		return (0);
4449104847fSDavid Xu 
44582a4538fSEric Badger 	if (SIGISMEMBER(sq->sq_ptrace, signo)) {
44682a4538fSEric Badger 		count++;
44782a4538fSEric Badger 		SIGDELSET(sq->sq_ptrace, signo);
44882a4538fSEric Badger 		si->ksi_flags |= KSI_PTRACE;
44982a4538fSEric Badger 	}
4503dfcaad6SDavid Xu 	if (SIGISMEMBER(sq->sq_kill, signo)) {
4513dfcaad6SDavid Xu 		count++;
45282a4538fSEric Badger 		if (count == 1)
4533dfcaad6SDavid Xu 			SIGDELSET(sq->sq_kill, signo);
4543dfcaad6SDavid Xu 	}
4553dfcaad6SDavid Xu 
4565c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
4579104847fSDavid Xu 		if (ksi->ksi_signo == signo) {
4589104847fSDavid Xu 			if (count == 0) {
4599104847fSDavid Xu 				TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4605da49fcbSDavid Xu 				ksi->ksi_sigq = NULL;
4619104847fSDavid Xu 				ksiginfo_copy(ksi, si);
4625da49fcbSDavid Xu 				if (ksiginfo_tryfree(ksi) && p != NULL)
4639104847fSDavid Xu 					p->p_pendingcnt--;
4649104847fSDavid Xu 			}
465016fa302SDavid Xu 			if (++count > 1)
466016fa302SDavid Xu 				break;
4679104847fSDavid Xu 		}
4689104847fSDavid Xu 	}
4699104847fSDavid Xu 
4709104847fSDavid Xu 	if (count <= 1)
4719104847fSDavid Xu 		SIGDELSET(sq->sq_signals, signo);
4729104847fSDavid Xu 	si->ksi_signo = signo;
4739104847fSDavid Xu 	return (signo);
4749104847fSDavid Xu }
4759104847fSDavid Xu 
4765da49fcbSDavid Xu void
sigqueue_take(ksiginfo_t * ksi)4775da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi)
4785da49fcbSDavid Xu {
4795da49fcbSDavid Xu 	struct ksiginfo *kp;
4805da49fcbSDavid Xu 	struct proc	*p;
4815da49fcbSDavid Xu 	sigqueue_t	*sq;
4825da49fcbSDavid Xu 
483ebceaf6dSDavid Xu 	if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
4845da49fcbSDavid Xu 		return;
4855da49fcbSDavid Xu 
4865da49fcbSDavid Xu 	p = sq->sq_proc;
4875da49fcbSDavid Xu 	TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4885da49fcbSDavid Xu 	ksi->ksi_sigq = NULL;
4895da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
4905da49fcbSDavid Xu 		p->p_pendingcnt--;
4915da49fcbSDavid Xu 
4925da49fcbSDavid Xu 	for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
4935da49fcbSDavid Xu 	     kp = TAILQ_NEXT(kp, ksi_link)) {
4945da49fcbSDavid Xu 		if (kp->ksi_signo == ksi->ksi_signo)
4955da49fcbSDavid Xu 			break;
4965da49fcbSDavid Xu 	}
49782a4538fSEric Badger 	if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
49882a4538fSEric Badger 	    !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
4995da49fcbSDavid Xu 		SIGDELSET(sq->sq_signals, ksi->ksi_signo);
5005da49fcbSDavid Xu }
5015da49fcbSDavid Xu 
502a5799a4fSKonstantin Belousov static int
sigqueue_add(sigqueue_t * sq,int signo,ksiginfo_t * si)5039104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
5049104847fSDavid Xu {
5059104847fSDavid Xu 	struct proc *p = sq->sq_proc;
5069104847fSDavid Xu 	struct ksiginfo *ksi;
5079104847fSDavid Xu 	int ret = 0;
5089104847fSDavid Xu 
5099104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
5109104847fSDavid Xu 
51182a4538fSEric Badger 	/*
51282a4538fSEric Badger 	 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
51382a4538fSEric Badger 	 * for these signals.
51482a4538fSEric Badger 	 */
5153dfcaad6SDavid Xu 	if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
5163dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
5179104847fSDavid Xu 		goto out_set_bit;
5183dfcaad6SDavid Xu 	}
5199104847fSDavid Xu 
5205da49fcbSDavid Xu 	/* directly insert the ksi, don't copy it */
5215da49fcbSDavid Xu 	if (si->ksi_flags & KSI_INS) {
5226a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
5236a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
5246a671a6bSKonstantin Belousov 		else
5255da49fcbSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
5265da49fcbSDavid Xu 		si->ksi_sigq = sq;
5275da49fcbSDavid Xu 		goto out_set_bit;
5285da49fcbSDavid Xu 	}
5295da49fcbSDavid Xu 
5303dfcaad6SDavid Xu 	if (__predict_false(ksiginfo_zone == NULL)) {
5313dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
5329104847fSDavid Xu 		goto out_set_bit;
5333dfcaad6SDavid Xu 	}
5349104847fSDavid Xu 
535ebceaf6dSDavid Xu 	if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
5369104847fSDavid Xu 		signal_overflow++;
5379104847fSDavid Xu 		ret = EAGAIN;
538cc29f221SKonstantin Belousov 	} else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) {
5399104847fSDavid Xu 		signal_alloc_fail++;
5409104847fSDavid Xu 		ret = EAGAIN;
5419104847fSDavid Xu 	} else {
5429104847fSDavid Xu 		if (p != NULL)
5439104847fSDavid Xu 			p->p_pendingcnt++;
5449104847fSDavid Xu 		ksiginfo_copy(si, ksi);
5459104847fSDavid Xu 		ksi->ksi_signo = signo;
5466a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
5476a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
5486a671a6bSKonstantin Belousov 		else
5499104847fSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
5505da49fcbSDavid Xu 		ksi->ksi_sigq = sq;
5519104847fSDavid Xu 	}
5529104847fSDavid Xu 
55382a4538fSEric Badger 	if (ret != 0) {
55482a4538fSEric Badger 		if ((si->ksi_flags & KSI_PTRACE) != 0) {
55582a4538fSEric Badger 			SIGADDSET(sq->sq_ptrace, signo);
55682a4538fSEric Badger 			ret = 0;
55782a4538fSEric Badger 			goto out_set_bit;
55882a4538fSEric Badger 		} else if ((si->ksi_flags & KSI_TRAP) != 0 ||
559a3de221dSKonstantin Belousov 		    (si->ksi_flags & KSI_SIGQ) == 0) {
5603dfcaad6SDavid Xu 			SIGADDSET(sq->sq_kill, signo);
5619104847fSDavid Xu 			ret = 0;
5629104847fSDavid Xu 			goto out_set_bit;
5639104847fSDavid Xu 		}
5649104847fSDavid Xu 		return (ret);
56582a4538fSEric Badger 	}
5669104847fSDavid Xu 
5679104847fSDavid Xu out_set_bit:
5689104847fSDavid Xu 	SIGADDSET(sq->sq_signals, signo);
5699104847fSDavid Xu 	return (ret);
5709104847fSDavid Xu }
5719104847fSDavid Xu 
5729104847fSDavid Xu void
sigqueue_flush(sigqueue_t * sq)5739104847fSDavid Xu sigqueue_flush(sigqueue_t *sq)
5749104847fSDavid Xu {
5759104847fSDavid Xu 	struct proc *p = sq->sq_proc;
5769104847fSDavid Xu 	ksiginfo_t *ksi;
5779104847fSDavid Xu 
5789104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
5799104847fSDavid Xu 
5805da49fcbSDavid Xu 	if (p != NULL)
5815da49fcbSDavid Xu 		PROC_LOCK_ASSERT(p, MA_OWNED);
5825da49fcbSDavid Xu 
5839104847fSDavid Xu 	while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
5849104847fSDavid Xu 		TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
5855da49fcbSDavid Xu 		ksi->ksi_sigq = NULL;
5865da49fcbSDavid Xu 		if (ksiginfo_tryfree(ksi) && p != NULL)
5879104847fSDavid Xu 			p->p_pendingcnt--;
5889104847fSDavid Xu 	}
5899104847fSDavid Xu 
5909104847fSDavid Xu 	SIGEMPTYSET(sq->sq_signals);
5913dfcaad6SDavid Xu 	SIGEMPTYSET(sq->sq_kill);
59282a4538fSEric Badger 	SIGEMPTYSET(sq->sq_ptrace);
5939104847fSDavid Xu }
5949104847fSDavid Xu 
595a5799a4fSKonstantin Belousov static void
sigqueue_move_set(sigqueue_t * src,sigqueue_t * dst,const sigset_t * set)596fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
5979104847fSDavid Xu {
598fc4ecc1dSDavid Xu 	sigset_t tmp;
5999104847fSDavid Xu 	struct proc *p1, *p2;
6009104847fSDavid Xu 	ksiginfo_t *ksi, *next;
6019104847fSDavid Xu 
6029104847fSDavid Xu 	KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
6039104847fSDavid Xu 	KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
6049104847fSDavid Xu 	p1 = src->sq_proc;
6059104847fSDavid Xu 	p2 = dst->sq_proc;
6069104847fSDavid Xu 	/* Move siginfo to target list */
6075c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
608fc4ecc1dSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
6099104847fSDavid Xu 			TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
6109104847fSDavid Xu 			if (p1 != NULL)
6119104847fSDavid Xu 				p1->p_pendingcnt--;
6129104847fSDavid Xu 			TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
6135da49fcbSDavid Xu 			ksi->ksi_sigq = dst;
6149104847fSDavid Xu 			if (p2 != NULL)
6159104847fSDavid Xu 				p2->p_pendingcnt++;
6169104847fSDavid Xu 		}
6179104847fSDavid Xu 	}
6189104847fSDavid Xu 
6199104847fSDavid Xu 	/* Move pending bits to target list */
6203dfcaad6SDavid Xu 	tmp = src->sq_kill;
621fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
6223dfcaad6SDavid Xu 	SIGSETOR(dst->sq_kill, tmp);
6233dfcaad6SDavid Xu 	SIGSETNAND(src->sq_kill, tmp);
6243dfcaad6SDavid Xu 
62582a4538fSEric Badger 	tmp = src->sq_ptrace;
62682a4538fSEric Badger 	SIGSETAND(tmp, *set);
62782a4538fSEric Badger 	SIGSETOR(dst->sq_ptrace, tmp);
62882a4538fSEric Badger 	SIGSETNAND(src->sq_ptrace, tmp);
62982a4538fSEric Badger 
6309104847fSDavid Xu 	tmp = src->sq_signals;
631fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
6329104847fSDavid Xu 	SIGSETOR(dst->sq_signals, tmp);
6339104847fSDavid Xu 	SIGSETNAND(src->sq_signals, tmp);
6349104847fSDavid Xu }
6359104847fSDavid Xu 
636407af02bSDavid Xu #if 0
637a5799a4fSKonstantin Belousov static void
6389104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
6399104847fSDavid Xu {
6409104847fSDavid Xu 	sigset_t set;
6419104847fSDavid Xu 
6429104847fSDavid Xu 	SIGEMPTYSET(set);
6439104847fSDavid Xu 	SIGADDSET(set, signo);
6449104847fSDavid Xu 	sigqueue_move_set(src, dst, &set);
6459104847fSDavid Xu }
646407af02bSDavid Xu #endif
6479104847fSDavid Xu 
648a5799a4fSKonstantin Belousov static void
sigqueue_delete_set(sigqueue_t * sq,const sigset_t * set)649fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
6509104847fSDavid Xu {
6519104847fSDavid Xu 	struct proc *p = sq->sq_proc;
6529104847fSDavid Xu 	ksiginfo_t *ksi, *next;
6539104847fSDavid Xu 
6549104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
6559104847fSDavid Xu 
6569104847fSDavid Xu 	/* Remove siginfo queue */
6575c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
6589104847fSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
6599104847fSDavid Xu 			TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
6605da49fcbSDavid Xu 			ksi->ksi_sigq = NULL;
6615da49fcbSDavid Xu 			if (ksiginfo_tryfree(ksi) && p != NULL)
6629104847fSDavid Xu 				p->p_pendingcnt--;
6639104847fSDavid Xu 		}
6649104847fSDavid Xu 	}
6653dfcaad6SDavid Xu 	SIGSETNAND(sq->sq_kill, *set);
66682a4538fSEric Badger 	SIGSETNAND(sq->sq_ptrace, *set);
6679104847fSDavid Xu 	SIGSETNAND(sq->sq_signals, *set);
6689104847fSDavid Xu }
6699104847fSDavid Xu 
6709104847fSDavid Xu void
sigqueue_delete(sigqueue_t * sq,int signo)6719104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo)
6729104847fSDavid Xu {
6739104847fSDavid Xu 	sigset_t set;
6749104847fSDavid Xu 
6759104847fSDavid Xu 	SIGEMPTYSET(set);
6769104847fSDavid Xu 	SIGADDSET(set, signo);
6779104847fSDavid Xu 	sigqueue_delete_set(sq, &set);
6789104847fSDavid Xu }
6799104847fSDavid Xu 
6809104847fSDavid Xu /* Remove a set of signals for a process */
681a5799a4fSKonstantin Belousov static void
sigqueue_delete_set_proc(struct proc * p,const sigset_t * set)682fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
6839104847fSDavid Xu {
6849104847fSDavid Xu 	sigqueue_t worklist;
6859104847fSDavid Xu 	struct thread *td0;
6869104847fSDavid Xu 
6879104847fSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
6889104847fSDavid Xu 
689aaa92413SKonstantin Belousov 	sigqueue_init(&worklist, NULL);
6909104847fSDavid Xu 	sigqueue_move_set(&p->p_sigqueue, &worklist, set);
6919104847fSDavid Xu 
6929104847fSDavid Xu 	FOREACH_THREAD_IN_PROC(p, td0)
6939104847fSDavid Xu 		sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
6949104847fSDavid Xu 
6959104847fSDavid Xu 	sigqueue_flush(&worklist);
6969104847fSDavid Xu }
6979104847fSDavid Xu 
6989104847fSDavid Xu void
sigqueue_delete_proc(struct proc * p,int signo)6999104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo)
7009104847fSDavid Xu {
7019104847fSDavid Xu 	sigset_t set;
7029104847fSDavid Xu 
7039104847fSDavid Xu 	SIGEMPTYSET(set);
7049104847fSDavid Xu 	SIGADDSET(set, signo);
7059104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
7069104847fSDavid Xu }
7079104847fSDavid Xu 
708a5799a4fSKonstantin Belousov static void
sigqueue_delete_stopmask_proc(struct proc * p)7099104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p)
7109104847fSDavid Xu {
7119104847fSDavid Xu 	sigset_t set;
7129104847fSDavid Xu 
7139104847fSDavid Xu 	SIGEMPTYSET(set);
7149104847fSDavid Xu 	SIGADDSET(set, SIGSTOP);
7159104847fSDavid Xu 	SIGADDSET(set, SIGTSTP);
7169104847fSDavid Xu 	SIGADDSET(set, SIGTTIN);
7179104847fSDavid Xu 	SIGADDSET(set, SIGTTOU);
7189104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
7199104847fSDavid Xu }
7209104847fSDavid Xu 
721fbbeeb6cSBruce Evans /*
7221968f37bSJohn Baldwin  * Determine signal that should be delivered to thread td, the current
7231968f37bSJohn Baldwin  * thread, 0 if none.  If there is a pending stop signal with default
724fbbeeb6cSBruce Evans  * action, the process stops in issignal().
725fbbeeb6cSBruce Evans  */
726fbbeeb6cSBruce Evans int
cursig(struct thread * td)7273cf3b9f0SJohn Baldwin cursig(struct thread *td)
728fbbeeb6cSBruce Evans {
729c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
73090af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
731a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
7323cf3b9f0SJohn Baldwin 	return (SIGPENDING(td) ? issignal(td) : 0);
733fbbeeb6cSBruce Evans }
734fbbeeb6cSBruce Evans 
73579065dbaSBruce Evans /*
73679065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
7379104847fSDavid Xu  * mode.  This must be called whenever a signal is added to td_sigqueue or
7384093529dSJeff Roberson  * unmasked in td_sigmask.
73979065dbaSBruce Evans  */
74079065dbaSBruce Evans void
signotify(struct thread * td)7414093529dSJeff Roberson signotify(struct thread *td)
74279065dbaSBruce Evans {
7434093529dSJeff Roberson 
744ddd4d15eSMatt Macy 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
7454093529dSJeff Roberson 
746c6d31b83SKonstantin Belousov 	if (SIGPENDING(td))
747c6d31b83SKonstantin Belousov 		ast_sched(td, TDA_SIG);
7488b94a061SJohn Baldwin }
7498b94a061SJohn Baldwin 
750affd9185SKonstantin Belousov /*
751affd9185SKonstantin Belousov  * Returns 1 (true) if altstack is configured for the thread, and the
752affd9185SKonstantin Belousov  * passed stack bottom address falls into the altstack range.  Handles
753affd9185SKonstantin Belousov  * the 43 compat special case where the alt stack size is zero.
754affd9185SKonstantin Belousov  */
7558b94a061SJohn Baldwin int
sigonstack(size_t sp)7568b94a061SJohn Baldwin sigonstack(size_t sp)
7578b94a061SJohn Baldwin {
758affd9185SKonstantin Belousov 	struct thread *td;
7598b94a061SJohn Baldwin 
760affd9185SKonstantin Belousov 	td = curthread;
761affd9185SKonstantin Belousov 	if ((td->td_pflags & TDP_ALTSTACK) == 0)
762affd9185SKonstantin Belousov 		return (0);
7631930e303SPoul-Henning Kamp #if defined(COMPAT_43)
7647e097daaSKonstantin Belousov 	if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
765affd9185SKonstantin Belousov 		return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
7668b94a061SJohn Baldwin #endif
767affd9185SKonstantin Belousov 	return (sp >= (size_t)td->td_sigstk.ss_sp &&
768affd9185SKonstantin Belousov 	    sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
7698b94a061SJohn Baldwin }
77079065dbaSBruce Evans 
7716f841fb7SMarcel Moolenaar static __inline int
sigprop(int sig)7726f841fb7SMarcel Moolenaar sigprop(int sig)
7732c42a146SMarcel Moolenaar {
7746f841fb7SMarcel Moolenaar 
775ed6d876bSBrooks Davis 	if (sig > 0 && sig < nitems(sigproptbl))
776ed6d876bSBrooks Davis 		return (sigproptbl[sig]);
7772c42a146SMarcel Moolenaar 	return (0);
778df8bae1dSRodney W. Grimes }
7792c42a146SMarcel Moolenaar 
780350ae563SKonstantin Belousov static bool
sigact_flag_test(const struct sigaction * act,int flag)781ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag)
782350ae563SKonstantin Belousov {
783350ae563SKonstantin Belousov 
784c83655f3SKonstantin Belousov 	/*
785c83655f3SKonstantin Belousov 	 * SA_SIGINFO is reset when signal disposition is set to
786c83655f3SKonstantin Belousov 	 * ignore or default.  Other flags are kept according to user
787c83655f3SKonstantin Belousov 	 * settings.
788c83655f3SKonstantin Belousov 	 */
789c83655f3SKonstantin Belousov 	return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
790c83655f3SKonstantin Belousov 	    ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
791c83655f3SKonstantin Belousov 	    (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
792350ae563SKonstantin Belousov }
793350ae563SKonstantin Belousov 
7942c42a146SMarcel Moolenaar /*
7958f19eb88SIan Dowse  * kern_sigaction
7962c42a146SMarcel Moolenaar  * sigaction
79723eeeff7SPeter Wemm  * freebsd4_sigaction
7982c42a146SMarcel Moolenaar  * osigaction
7992c42a146SMarcel Moolenaar  */
8008f19eb88SIan Dowse int
kern_sigaction(struct thread * td,int sig,const struct sigaction * act,struct sigaction * oact,int flags)801ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
802ea566832SEd Schouten     struct sigaction *oact, int flags)
803df8bae1dSRodney W. Grimes {
80490af4afaSJohn Baldwin 	struct sigacts *ps;
8058f19eb88SIan Dowse 	struct proc *p = td->td_proc;
806df8bae1dSRodney W. Grimes 
8072899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
8082c42a146SMarcel Moolenaar 		return (EINVAL);
809271ab240SKonstantin Belousov 	if (act != NULL && act->sa_handler != SIG_DFL &&
810271ab240SKonstantin Belousov 	    act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
811271ab240SKonstantin Belousov 	    SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
812271ab240SKonstantin Belousov 	    SA_NOCLDWAIT | SA_SIGINFO)) != 0)
8132d864174SKonstantin Belousov 		return (EINVAL);
8142c42a146SMarcel Moolenaar 
815628d2653SJohn Baldwin 	PROC_LOCK(p);
816628d2653SJohn Baldwin 	ps = p->p_sigacts;
81790af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
8182c42a146SMarcel Moolenaar 	if (oact) {
819fb441a88SKonstantin Belousov 		memset(oact, 0, sizeof(*oact));
8202c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
8212c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
8222c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
8232c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
8242c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
8252c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
8262c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
8272c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
8282c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
82910c2b8e1SDavid E. O'Brien 		if (SIGISMEMBER(ps->ps_siginfo, sig)) {
8302c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
83110c2b8e1SDavid E. O'Brien 			oact->sa_sigaction =
83210c2b8e1SDavid E. O'Brien 			    (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
83310c2b8e1SDavid E. O'Brien 		} else
83410c2b8e1SDavid E. O'Brien 			oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
83590af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
8362c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
83790af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
8382c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
8392c42a146SMarcel Moolenaar 	}
8402c42a146SMarcel Moolenaar 	if (act) {
8412c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
842628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
84390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
844628d2653SJohn Baldwin 			PROC_UNLOCK(p);
8452c42a146SMarcel Moolenaar 			return (EINVAL);
846628d2653SJohn Baldwin 		}
8472c42a146SMarcel Moolenaar 
848df8bae1dSRodney W. Grimes 		/*
849df8bae1dSRodney W. Grimes 		 * Change setting atomically.
850df8bae1dSRodney W. Grimes 		 */
8512c42a146SMarcel Moolenaar 
8522c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
8532c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
854350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_SIGINFO)) {
855aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
856aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
85780f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
85880f42b55SIan Dowse 		} else {
85980f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
8602c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
8612c42a146SMarcel Moolenaar 		}
862350ae563SKonstantin Belousov 		if (!sigact_flag_test(act, SA_RESTART))
8632c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
864df8bae1dSRodney W. Grimes 		else
8652c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
866350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_ONSTACK))
8672c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
868df8bae1dSRodney W. Grimes 		else
8692c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
870350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_RESETHAND))
8712c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
8721e41c1b5SSteven Wallace 		else
8732c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
874350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_NODEFER))
8752c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
876289ccde0SPeter Wemm 		else
8772c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
8782c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
8792c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
88090af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
8816626c604SJulian Elischer 			else
88290af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
883ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
884245f17d4SJoerg Wunsch 				/*
8852c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
8862c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
8872c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
8882c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
889245f17d4SJoerg Wunsch 				 */
890245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
89190af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
8926626c604SJulian Elischer 				else
89390af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
894245f17d4SJoerg Wunsch 			} else
89590af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
896ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
89790af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
898ba1551caSIan Dowse 			else
89990af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
900df8bae1dSRodney W. Grimes 		}
901df8bae1dSRodney W. Grimes 		/*
90290af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
9032c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
90490af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
9052c42a146SMarcel Moolenaar 		 * have to restart the process.
906df8bae1dSRodney W. Grimes 		 */
9072c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
908fd50a707SBrooks Davis 		    (sigprop(sig) & SIGPROP_IGNORE &&
9092c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
9102c42a146SMarcel Moolenaar 			/* never to be seen again */
9119104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
9122c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
9132c42a146SMarcel Moolenaar 				/* easier in psignal */
91490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
91590af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
916645682fdSLuoqi Chen 		} else {
91790af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
9182c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
91990af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
9202c42a146SMarcel Moolenaar 			else
92190af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
9222c42a146SMarcel Moolenaar 		}
92323eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
92423eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
92523eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
92623eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
92723eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
92823eeeff7SPeter Wemm 		else
92923eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
93023eeeff7SPeter Wemm #endif
931e8ebc08fSPeter Wemm #ifdef COMPAT_43
932645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
93323eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
93423eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
935645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
936645682fdSLuoqi Chen 		else
937645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
938e8ebc08fSPeter Wemm #endif
939df8bae1dSRodney W. Grimes 	}
94090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
941628d2653SJohn Baldwin 	PROC_UNLOCK(p);
9422c42a146SMarcel Moolenaar 	return (0);
9432c42a146SMarcel Moolenaar }
9442c42a146SMarcel Moolenaar 
9452c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
9462c42a146SMarcel Moolenaar struct sigaction_args {
9472c42a146SMarcel Moolenaar 	int	sig;
9482c42a146SMarcel Moolenaar 	struct	sigaction *act;
9492c42a146SMarcel Moolenaar 	struct	sigaction *oact;
9502c42a146SMarcel Moolenaar };
9512c42a146SMarcel Moolenaar #endif
9522c42a146SMarcel Moolenaar int
sys_sigaction(struct thread * td,struct sigaction_args * uap)953e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap)
9542c42a146SMarcel Moolenaar {
9552c42a146SMarcel Moolenaar 	struct sigaction act, oact;
956e052a8b9SEd Maste 	struct sigaction *actp, *oactp;
9572c42a146SMarcel Moolenaar 	int error;
9582c42a146SMarcel Moolenaar 
9596f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
9606f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
9612c42a146SMarcel Moolenaar 	if (actp) {
9626f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
9632c42a146SMarcel Moolenaar 		if (error)
96444443757STim J. Robbins 			return (error);
9652c42a146SMarcel Moolenaar 	}
9668f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
96725d6dc06SJohn Baldwin 	if (oactp && !error)
9686f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
9692c42a146SMarcel Moolenaar 	return (error);
9702c42a146SMarcel Moolenaar }
9712c42a146SMarcel Moolenaar 
97223eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
97323eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
97423eeeff7SPeter Wemm struct freebsd4_sigaction_args {
97523eeeff7SPeter Wemm 	int	sig;
97623eeeff7SPeter Wemm 	struct	sigaction *act;
97723eeeff7SPeter Wemm 	struct	sigaction *oact;
97823eeeff7SPeter Wemm };
97923eeeff7SPeter Wemm #endif
98023eeeff7SPeter Wemm int
freebsd4_sigaction(struct thread * td,struct freebsd4_sigaction_args * uap)981e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
98223eeeff7SPeter Wemm {
98323eeeff7SPeter Wemm 	struct sigaction act, oact;
984e052a8b9SEd Maste 	struct sigaction *actp, *oactp;
98523eeeff7SPeter Wemm 	int error;
98623eeeff7SPeter Wemm 
98723eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
98823eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
98923eeeff7SPeter Wemm 	if (actp) {
99023eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
99123eeeff7SPeter Wemm 		if (error)
99244443757STim J. Robbins 			return (error);
99323eeeff7SPeter Wemm 	}
99423eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
995a14e1189SJohn Baldwin 	if (oactp && !error)
99623eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
99723eeeff7SPeter Wemm 	return (error);
99823eeeff7SPeter Wemm }
99923eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
100023eeeff7SPeter Wemm 
100131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
10022c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
10032c42a146SMarcel Moolenaar struct osigaction_args {
10042c42a146SMarcel Moolenaar 	int	signum;
10052c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
10062c42a146SMarcel Moolenaar 	struct	osigaction *osa;
10072c42a146SMarcel Moolenaar };
10082c42a146SMarcel Moolenaar #endif
10092c42a146SMarcel Moolenaar int
osigaction(struct thread * td,struct osigaction_args * uap)1010e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap)
10112c42a146SMarcel Moolenaar {
10122c42a146SMarcel Moolenaar 	struct osigaction sa;
10132c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
1014e052a8b9SEd Maste 	struct sigaction *nsap, *osap;
10152c42a146SMarcel Moolenaar 	int error;
10162c42a146SMarcel Moolenaar 
10176f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
10186f841fb7SMarcel Moolenaar 		return (EINVAL);
1019fb99ab88SMatthew Dillon 
10206f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
10216f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
1022fb99ab88SMatthew Dillon 
10232c42a146SMarcel Moolenaar 	if (nsap) {
10246f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
10252c42a146SMarcel Moolenaar 		if (error)
102644443757STim J. Robbins 			return (error);
10272c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
10282c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
10292c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
10302c42a146SMarcel Moolenaar 	}
103123eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
10322c42a146SMarcel Moolenaar 	if (osap && !error) {
10332c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
10342c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
10352c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
10366f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
10372c42a146SMarcel Moolenaar 	}
10382c42a146SMarcel Moolenaar 	return (error);
10392c42a146SMarcel Moolenaar }
104023eeeff7SPeter Wemm 
104173dbd3daSJohn Baldwin #if !defined(__i386__)
104223eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
104323eeeff7SPeter Wemm int
osigreturn(struct thread * td,struct osigreturn_args * uap)1044e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap)
104523eeeff7SPeter Wemm {
104623eeeff7SPeter Wemm 
104723eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
104823eeeff7SPeter Wemm }
104923eeeff7SPeter Wemm #endif
105031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1051df8bae1dSRodney W. Grimes 
1052df8bae1dSRodney W. Grimes /*
1053df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
1054df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
1055df8bae1dSRodney W. Grimes  */
1056df8bae1dSRodney W. Grimes void
siginit(struct proc * p)1057e052a8b9SEd Maste siginit(struct proc *p)
1058df8bae1dSRodney W. Grimes {
10593e85b721SEd Maste 	int i;
106090af4afaSJohn Baldwin 	struct sigacts *ps;
1061df8bae1dSRodney W. Grimes 
1062628d2653SJohn Baldwin 	PROC_LOCK(p);
106390af4afaSJohn Baldwin 	ps = p->p_sigacts;
106490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
1065350ae563SKonstantin Belousov 	for (i = 1; i <= NSIG; i++) {
1066fd50a707SBrooks Davis 		if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
106790af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
1068350ae563SKonstantin Belousov 		}
1069350ae563SKonstantin Belousov 	}
107090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
1071628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1072df8bae1dSRodney W. Grimes }
1073df8bae1dSRodney W. Grimes 
1074df8bae1dSRodney W. Grimes /*
1075350ae563SKonstantin Belousov  * Reset specified signal to the default disposition.
1076350ae563SKonstantin Belousov  */
1077350ae563SKonstantin Belousov static void
sigdflt(struct sigacts * ps,int sig)1078350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig)
1079350ae563SKonstantin Belousov {
1080350ae563SKonstantin Belousov 
1081350ae563SKonstantin Belousov 	mtx_assert(&ps->ps_mtx, MA_OWNED);
1082350ae563SKonstantin Belousov 	SIGDELSET(ps->ps_sigcatch, sig);
1083fd50a707SBrooks Davis 	if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
1084350ae563SKonstantin Belousov 		SIGADDSET(ps->ps_sigignore, sig);
1085350ae563SKonstantin Belousov 	ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1086350ae563SKonstantin Belousov 	SIGDELSET(ps->ps_siginfo, sig);
1087350ae563SKonstantin Belousov }
1088350ae563SKonstantin Belousov 
1089350ae563SKonstantin Belousov /*
1090df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
1091df8bae1dSRodney W. Grimes  */
1092df8bae1dSRodney W. Grimes void
execsigs(struct proc * p)1093a30ec4b9SDavid Xu execsigs(struct proc *p)
1094df8bae1dSRodney W. Grimes {
1095a30ec4b9SDavid Xu 	struct sigacts *ps;
1096a30ec4b9SDavid Xu 	struct thread *td;
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes 	/*
1099df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
11004093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
1101df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
1102df8bae1dSRodney W. Grimes 	 */
11039b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1104628d2653SJohn Baldwin 	ps = p->p_sigacts;
110590af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
1106079c5b9eSKyle Evans 	sig_drop_caught(p);
1107f3fe76ecSEd Schouten 
1108f3fe76ecSEd Schouten 	/*
1109df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
1110df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
1111df8bae1dSRodney W. Grimes 	 */
1112469ec1ebSKonstantin Belousov 	td = curthread;
1113469ec1ebSKonstantin Belousov 	MPASS(td->td_proc == p);
1114a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
1115a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
1116a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
1117a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
111880e907a1SPeter Wemm 	/*
111980e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
112080e907a1SPeter Wemm 	 */
112190af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1122c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1123c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
112490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
1125df8bae1dSRodney W. Grimes }
1126df8bae1dSRodney W. Grimes 
1127df8bae1dSRodney W. Grimes /*
1128e77daab1SJohn Baldwin  * kern_sigprocmask()
11297c8fdcbdSMatthew Dillon  *
1130628d2653SJohn Baldwin  *	Manipulate signal mask.
1131df8bae1dSRodney W. Grimes  */
1132e77daab1SJohn Baldwin int
kern_sigprocmask(struct thread * td,int how,sigset_t * set,sigset_t * oset,int flags)11336b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
113484440afbSKonstantin Belousov     int flags)
11352c42a146SMarcel Moolenaar {
11366b286ee8SKonstantin Belousov 	sigset_t new_block, oset1;
11376b286ee8SKonstantin Belousov 	struct proc *p;
11382c42a146SMarcel Moolenaar 	int error;
11392c42a146SMarcel Moolenaar 
11406b286ee8SKonstantin Belousov 	p = td->td_proc;
114170778bbaSKonstantin Belousov 	if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
114270778bbaSKonstantin Belousov 		PROC_LOCK_ASSERT(p, MA_OWNED);
114370778bbaSKonstantin Belousov 	else
11446b286ee8SKonstantin Belousov 		PROC_LOCK(p);
114570778bbaSKonstantin Belousov 	mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
114670778bbaSKonstantin Belousov 	    ? MA_OWNED : MA_NOTOWNED);
11472c42a146SMarcel Moolenaar 	if (oset != NULL)
11484093529dSJeff Roberson 		*oset = td->td_sigmask;
11492c42a146SMarcel Moolenaar 
11502c42a146SMarcel Moolenaar 	error = 0;
11512c42a146SMarcel Moolenaar 	if (set != NULL) {
11522c42a146SMarcel Moolenaar 		switch (how) {
11532c42a146SMarcel Moolenaar 		case SIG_BLOCK:
1154645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
11556b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
11564093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
11576b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
11586b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
11592c42a146SMarcel Moolenaar 			break;
11602c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
11614093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
11624093529dSJeff Roberson 			signotify(td);
1163407af02bSDavid Xu 			goto out;
11642c42a146SMarcel Moolenaar 		case SIG_SETMASK:
1165645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
11666b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
116784440afbSKonstantin Belousov 			if (flags & SIGPROCMASK_OLD)
11684093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
1169645682fdSLuoqi Chen 			else
11704093529dSJeff Roberson 				td->td_sigmask = *set;
11716b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
11726b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
11734093529dSJeff Roberson 			signotify(td);
11742c42a146SMarcel Moolenaar 			break;
11752c42a146SMarcel Moolenaar 		default:
11762c42a146SMarcel Moolenaar 			error = EINVAL;
1177407af02bSDavid Xu 			goto out;
11782c42a146SMarcel Moolenaar 		}
11796b286ee8SKonstantin Belousov 
11806b286ee8SKonstantin Belousov 		/*
11817df8f6abSKonstantin Belousov 		 * The new_block set contains signals that were not previously
11826b286ee8SKonstantin Belousov 		 * blocked, but are blocked now.
11836b286ee8SKonstantin Belousov 		 *
11846b286ee8SKonstantin Belousov 		 * In case we block any signal that was not previously blocked
11856b286ee8SKonstantin Belousov 		 * for td, and process has the signal pending, try to schedule
1186407af02bSDavid Xu 		 * signal delivery to some thread that does not block the
1187407af02bSDavid Xu 		 * signal, possibly waking it up.
11886b286ee8SKonstantin Belousov 		 */
11896b286ee8SKonstantin Belousov 		if (p->p_numthreads != 1)
119080a8b0f3SKonstantin Belousov 			reschedule_signals(p, new_block, flags);
1191407af02bSDavid Xu 	}
11926b286ee8SKonstantin Belousov 
1193407af02bSDavid Xu out:
119484440afbSKonstantin Belousov 	if (!(flags & SIGPROCMASK_PROC_LOCKED))
11956b286ee8SKonstantin Belousov 		PROC_UNLOCK(p);
11962c42a146SMarcel Moolenaar 	return (error);
11972c42a146SMarcel Moolenaar }
11982c42a146SMarcel Moolenaar 
1199d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1200df8bae1dSRodney W. Grimes struct sigprocmask_args {
1201df8bae1dSRodney W. Grimes 	int	how;
12022c42a146SMarcel Moolenaar 	const sigset_t *set;
12032c42a146SMarcel Moolenaar 	sigset_t *oset;
1204df8bae1dSRodney W. Grimes };
1205d2d3e875SBruce Evans #endif
120626f9a767SRodney W. Grimes int
sys_sigprocmask(struct thread * td,struct sigprocmask_args * uap)1207e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1208df8bae1dSRodney W. Grimes {
12092c42a146SMarcel Moolenaar 	sigset_t set, oset;
12102c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
12112c42a146SMarcel Moolenaar 	int error;
1212df8bae1dSRodney W. Grimes 
12136f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
12146f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
12152c42a146SMarcel Moolenaar 	if (setp) {
12166f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
12172c42a146SMarcel Moolenaar 		if (error)
12182c42a146SMarcel Moolenaar 			return (error);
1219df8bae1dSRodney W. Grimes 	}
1220e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
12212c42a146SMarcel Moolenaar 	if (osetp && !error) {
12226f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
12232c42a146SMarcel Moolenaar 	}
12242c42a146SMarcel Moolenaar 	return (error);
12252c42a146SMarcel Moolenaar }
12262c42a146SMarcel Moolenaar 
122731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
12282c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
12292c42a146SMarcel Moolenaar struct osigprocmask_args {
12302c42a146SMarcel Moolenaar 	int	how;
12312c42a146SMarcel Moolenaar 	osigset_t mask;
12322c42a146SMarcel Moolenaar };
12332c42a146SMarcel Moolenaar #endif
12342c42a146SMarcel Moolenaar int
osigprocmask(struct thread * td,struct osigprocmask_args * uap)1235e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap)
12362c42a146SMarcel Moolenaar {
12372c42a146SMarcel Moolenaar 	sigset_t set, oset;
12382c42a146SMarcel Moolenaar 	int error;
12392c42a146SMarcel Moolenaar 
12402c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1241e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1242b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
1243df8bae1dSRodney W. Grimes 	return (error);
1244df8bae1dSRodney W. Grimes }
124531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1246df8bae1dSRodney W. Grimes 
124726f9a767SRodney W. Grimes int
sys_sigwait(struct thread * td,struct sigwait_args * uap)12488451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap)
1249a447cd8bSJeff Roberson {
12509104847fSDavid Xu 	ksiginfo_t ksi;
1251a447cd8bSJeff Roberson 	sigset_t set;
1252a447cd8bSJeff Roberson 	int error;
1253a447cd8bSJeff Roberson 
1254a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
125536939a0aSDavid Xu 	if (error) {
125636939a0aSDavid Xu 		td->td_retval[0] = error;
125736939a0aSDavid Xu 		return (0);
125836939a0aSDavid Xu 	}
1259a447cd8bSJeff Roberson 
12609104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
126136939a0aSDavid Xu 	if (error) {
1262acced8b0SKonstantin Belousov 		/*
1263acced8b0SKonstantin Belousov 		 * sigwait() function shall not return EINTR, but
1264acced8b0SKonstantin Belousov 		 * the syscall does.  Non-ancient libc provides the
1265acced8b0SKonstantin Belousov 		 * wrapper which hides EINTR.  Otherwise, EINTR return
1266acced8b0SKonstantin Belousov 		 * is used by libthr to handle required cancellation
1267acced8b0SKonstantin Belousov 		 * point in the sigwait().
1268acced8b0SKonstantin Belousov 		 */
12698e9a54eeSKonstantin Belousov 		if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1270afb36e28SKonstantin Belousov 			return (ERESTART);
127136939a0aSDavid Xu 		td->td_retval[0] = error;
127236939a0aSDavid Xu 		return (0);
127336939a0aSDavid Xu 	}
1274a447cd8bSJeff Roberson 
12759104847fSDavid Xu 	error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
127636939a0aSDavid Xu 	td->td_retval[0] = error;
127736939a0aSDavid Xu 	return (0);
1278a447cd8bSJeff Roberson }
12790c14ff0eSRobert Watson 
1280a447cd8bSJeff Roberson int
sys_sigtimedwait(struct thread * td,struct sigtimedwait_args * uap)12818451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1282a447cd8bSJeff Roberson {
1283a447cd8bSJeff Roberson 	struct timespec ts;
1284a447cd8bSJeff Roberson 	struct timespec *timeout;
1285a447cd8bSJeff Roberson 	sigset_t set;
12869104847fSDavid Xu 	ksiginfo_t ksi;
1287a447cd8bSJeff Roberson 	int error;
1288a447cd8bSJeff Roberson 
1289a447cd8bSJeff Roberson 	if (uap->timeout) {
1290a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
1291a447cd8bSJeff Roberson 		if (error)
1292a447cd8bSJeff Roberson 			return (error);
1293a447cd8bSJeff Roberson 
1294a447cd8bSJeff Roberson 		timeout = &ts;
1295a447cd8bSJeff Roberson 	} else
1296a447cd8bSJeff Roberson 		timeout = NULL;
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, timeout);
1303a447cd8bSJeff Roberson 	if (error)
1304a447cd8bSJeff Roberson 		return (error);
13059dde3bc9SDavid Xu 
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 
1314a447cd8bSJeff Roberson int
sys_sigwaitinfo(struct thread * td,struct sigwaitinfo_args * uap)13158451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1316a447cd8bSJeff Roberson {
13179104847fSDavid Xu 	ksiginfo_t ksi;
1318a447cd8bSJeff Roberson 	sigset_t set;
1319a447cd8bSJeff Roberson 	int error;
1320a447cd8bSJeff Roberson 
1321a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1322a447cd8bSJeff Roberson 	if (error)
1323a447cd8bSJeff Roberson 		return (error);
1324a447cd8bSJeff Roberson 
13259104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
1326a447cd8bSJeff Roberson 	if (error)
1327a447cd8bSJeff Roberson 		return (error);
1328a447cd8bSJeff Roberson 
1329418228dfSDavid Xu 	if (uap->info)
13309104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
13319104847fSDavid Xu 
13329104847fSDavid Xu 	if (error == 0)
13339104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1334a447cd8bSJeff Roberson 	return (error);
1335a447cd8bSJeff Roberson }
1336a447cd8bSJeff Roberson 
133786be94fcSTycho Nightingale static void
proc_td_siginfo_capture(struct thread * td,siginfo_t * si)133886be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
133986be94fcSTycho Nightingale {
134086be94fcSTycho Nightingale 	struct thread *thr;
134186be94fcSTycho Nightingale 
134286be94fcSTycho Nightingale 	FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
134386be94fcSTycho Nightingale 		if (thr == td)
134486be94fcSTycho Nightingale 			thr->td_si = *si;
134586be94fcSTycho Nightingale 		else
134686be94fcSTycho Nightingale 			thr->td_si.si_signo = 0;
134786be94fcSTycho Nightingale 	}
134886be94fcSTycho Nightingale }
134986be94fcSTycho Nightingale 
1350c6511aeaSDavid Xu int
kern_sigtimedwait(struct thread * td,sigset_t waitset,ksiginfo_t * ksi,struct timespec * timeout)13519104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1352a447cd8bSJeff Roberson 	struct timespec *timeout)
1353a447cd8bSJeff Roberson {
13543074d1b4SDavid Xu 	struct sigacts *ps;
1355407af02bSDavid Xu 	sigset_t saved_mask, new_block;
1356a447cd8bSJeff Roberson 	struct proc *p;
13574a700f3cSDmitry Chagin 	int error, sig, timevalid = 0;
13584a700f3cSDmitry Chagin 	sbintime_t sbt, precision, tsbt;
13594a700f3cSDmitry Chagin 	struct timespec ts;
1360ef401a85SKonstantin Belousov 	bool traced;
1361a447cd8bSJeff Roberson 
1362a447cd8bSJeff Roberson 	p = td->td_proc;
1363a447cd8bSJeff Roberson 	error = 0;
1364ef401a85SKonstantin Belousov 	traced = false;
1365a447cd8bSJeff Roberson 
1366dbec10e0SJonathan T. Looney 	/* Ensure the sigfastblock value is up to date. */
1367dbec10e0SJonathan T. Looney 	sigfastblock_fetch(td);
1368dbec10e0SJonathan T. Looney 
1369407af02bSDavid Xu 	if (timeout != NULL) {
13701089f031SDavid Xu 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
13711089f031SDavid Xu 			timevalid = 1;
13724a700f3cSDmitry Chagin 			ts = *timeout;
13734a700f3cSDmitry Chagin 			if (ts.tv_sec < INT32_MAX / 2) {
13744a700f3cSDmitry Chagin 				tsbt = tstosbt(ts);
13754a700f3cSDmitry Chagin 				precision = tsbt;
13764a700f3cSDmitry Chagin 				precision >>= tc_precexp;
13774a700f3cSDmitry Chagin 				if (TIMESEL(&sbt, tsbt))
13784a700f3cSDmitry Chagin 					sbt += tc_tick_sbt;
13794a700f3cSDmitry Chagin 				sbt += tsbt;
13804a700f3cSDmitry Chagin 			} else
13814a700f3cSDmitry Chagin 				precision = sbt = 0;
13821089f031SDavid Xu 		}
13834a700f3cSDmitry Chagin 	} else
13844a700f3cSDmitry Chagin 		precision = sbt = 0;
1385407af02bSDavid Xu 	ksiginfo_init(ksi);
1386407af02bSDavid Xu 	/* Some signals can not be waited for. */
1387407af02bSDavid Xu 	SIG_CANTMASK(waitset);
1388407af02bSDavid Xu 	ps = p->p_sigacts;
1389407af02bSDavid Xu 	PROC_LOCK(p);
1390407af02bSDavid Xu 	saved_mask = td->td_sigmask;
1391407af02bSDavid Xu 	SIGSETNAND(td->td_sigmask, waitset);
1392bc387624SKonstantin Belousov 	if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1393b599982bSKonstantin Belousov 	    !kern_sig_discard_ign) {
1394b599982bSKonstantin Belousov 		thread_lock(td);
1395b599982bSKonstantin Belousov 		td->td_flags |= TDF_SIGWAIT;
1396b599982bSKonstantin Belousov 		thread_unlock(td);
1397b599982bSKonstantin Belousov 	}
1398407af02bSDavid Xu 	for (;;) {
13993074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
14003cf3b9f0SJohn Baldwin 		sig = cursig(td);
14013074d1b4SDavid Xu 		mtx_unlock(&ps->ps_mtx);
140246e47c4fSKonstantin Belousov 		KASSERT(sig >= 0, ("sig %d", sig));
1403407af02bSDavid Xu 		if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1404407af02bSDavid Xu 			if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1405407af02bSDavid Xu 			    sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1406407af02bSDavid Xu 				error = 0;
1407407af02bSDavid Xu 				break;
14083074d1b4SDavid Xu 			}
14097e0221a2SDavid Xu 		}
14107e0221a2SDavid Xu 
1411407af02bSDavid Xu 		if (error != 0)
1412407af02bSDavid Xu 			break;
14133074d1b4SDavid Xu 
1414a447cd8bSJeff Roberson 		/*
1415a447cd8bSJeff Roberson 		 * POSIX says this must be checked after looking for pending
1416a447cd8bSJeff Roberson 		 * signals.
1417a447cd8bSJeff Roberson 		 */
14184a700f3cSDmitry Chagin 		if (timeout != NULL && !timevalid) {
1419a447cd8bSJeff Roberson 			error = EINVAL;
1420407af02bSDavid Xu 			break;
1421a447cd8bSJeff Roberson 		}
1422a447cd8bSJeff Roberson 
1423ef401a85SKonstantin Belousov 		if (traced) {
1424ef401a85SKonstantin Belousov 			error = EINTR;
1425ef401a85SKonstantin Belousov 			break;
1426ef401a85SKonstantin Belousov 		}
1427ef401a85SKonstantin Belousov 
14284a700f3cSDmitry Chagin 		error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
14294a700f3cSDmitry Chagin 		    "sigwait", sbt, precision, C_ABSOLUTE);
1430407af02bSDavid Xu 
1431afb36e28SKonstantin Belousov 		/* The syscalls can not be restarted. */
1432afb36e28SKonstantin Belousov 		if (error == ERESTART)
14333074d1b4SDavid Xu 			error = EINTR;
1434afb36e28SKonstantin Belousov 
1435ef401a85SKonstantin Belousov 		/*
1436ef401a85SKonstantin Belousov 		 * If PTRACE_SCE or PTRACE_SCX were set after
1437ef401a85SKonstantin Belousov 		 * userspace entered the syscall, return spurious
1438ef401a85SKonstantin Belousov 		 * EINTR after wait was done.  Only do this as last
1439ef401a85SKonstantin Belousov 		 * resort after rechecking for possible queued signals
1440ef401a85SKonstantin Belousov 		 * and expired timeouts.
1441ef401a85SKonstantin Belousov 		 */
1442ef401a85SKonstantin Belousov 		if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1443ef401a85SKonstantin Belousov 			traced = true;
1444407af02bSDavid Xu 	}
1445b599982bSKonstantin Belousov 	thread_lock(td);
1446b599982bSKonstantin Belousov 	td->td_flags &= ~TDF_SIGWAIT;
1447b599982bSKonstantin Belousov 	thread_unlock(td);
14489dde3bc9SDavid Xu 
1449407af02bSDavid Xu 	new_block = saved_mask;
1450407af02bSDavid Xu 	SIGSETNAND(new_block, td->td_sigmask);
1451407af02bSDavid Xu 	td->td_sigmask = saved_mask;
1452407af02bSDavid Xu 	/*
1453407af02bSDavid Xu 	 * Fewer signals can be delivered to us, reschedule signal
1454407af02bSDavid Xu 	 * notification.
1455407af02bSDavid Xu 	 */
1456407af02bSDavid Xu 	if (p->p_numthreads != 1)
1457407af02bSDavid Xu 		reschedule_signals(p, new_block, 0);
14585d217f17SJohn Birrell 
1459407af02bSDavid Xu 	if (error == 0) {
146036160958SMark Johnston 		SDT_PROBE2(proc, , , signal__clear, sig, ksi);
14615d217f17SJohn Birrell 
146256c06c4bSDavid Xu 		if (ksi->ksi_code == SI_TIMER)
146356c06c4bSDavid Xu 			itimer_accept(p, ksi->ksi_timerid, ksi);
14647e0221a2SDavid Xu 
14657e0221a2SDavid Xu #ifdef KTRACE
14667e0221a2SDavid Xu 		if (KTRPOINT(td, KTR_PSIG)) {
14677e0221a2SDavid Xu 			sig_t action;
14687e0221a2SDavid Xu 
14693074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
1470a447cd8bSJeff Roberson 			action = ps->ps_sigact[_SIG_IDX(sig)];
147190af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
147261009552SJilles Tjoelker 			ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
14737e0221a2SDavid Xu 		}
1474a447cd8bSJeff Roberson #endif
147586be94fcSTycho Nightingale 		if (sig == SIGKILL) {
147686be94fcSTycho Nightingale 			proc_td_siginfo_capture(td, &ksi->ksi_info);
1477aaa92413SKonstantin Belousov 			sigexit(td, sig);
14783074d1b4SDavid Xu 		}
147986be94fcSTycho Nightingale 	}
1480a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
1481a447cd8bSJeff Roberson 	return (error);
1482a447cd8bSJeff Roberson }
1483a447cd8bSJeff Roberson 
14849104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
14859104847fSDavid Xu struct sigpending_args {
14869104847fSDavid Xu 	sigset_t	*set;
14879104847fSDavid Xu };
14889104847fSDavid Xu #endif
1489a447cd8bSJeff Roberson int
sys_sigpending(struct thread * td,struct sigpending_args * uap)1490e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap)
1491df8bae1dSRodney W. Grimes {
1492b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
14939104847fSDavid Xu 	sigset_t pending;
1494df8bae1dSRodney W. Grimes 
1495628d2653SJohn Baldwin 	PROC_LOCK(p);
14969104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
14979104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1498628d2653SJohn Baldwin 	PROC_UNLOCK(p);
14999104847fSDavid Xu 	return (copyout(&pending, uap->set, sizeof(sigset_t)));
15002c42a146SMarcel Moolenaar }
15012c42a146SMarcel Moolenaar 
150231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
15032c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
15042c42a146SMarcel Moolenaar struct osigpending_args {
15052c42a146SMarcel Moolenaar 	int	dummy;
15062c42a146SMarcel Moolenaar };
15072c42a146SMarcel Moolenaar #endif
15082c42a146SMarcel Moolenaar int
osigpending(struct thread * td,struct osigpending_args * uap)1509e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap)
15102c42a146SMarcel Moolenaar {
1511b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
15129104847fSDavid Xu 	sigset_t pending;
1513b40ce416SJulian Elischer 
1514628d2653SJohn Baldwin 	PROC_LOCK(p);
15159104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
15169104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1517628d2653SJohn Baldwin 	PROC_UNLOCK(p);
15189104847fSDavid Xu 	SIG2OSIG(pending, td->td_retval[0]);
1519df8bae1dSRodney W. Grimes 	return (0);
1520df8bae1dSRodney W. Grimes }
152131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1522df8bae1dSRodney W. Grimes 
15231930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1524df8bae1dSRodney W. Grimes /*
1525df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1526df8bae1dSRodney W. Grimes  */
1527d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1528df8bae1dSRodney W. Grimes struct osigvec_args {
1529df8bae1dSRodney W. Grimes 	int	signum;
1530df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1531df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1532df8bae1dSRodney W. Grimes };
1533d2d3e875SBruce Evans #endif
1534df8bae1dSRodney W. Grimes /* ARGSUSED */
153526f9a767SRodney W. Grimes int
osigvec(struct thread * td,struct osigvec_args * uap)1536e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap)
1537df8bae1dSRodney W. Grimes {
1538df8bae1dSRodney W. Grimes 	struct sigvec vec;
15392c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
1540e052a8b9SEd Maste 	struct sigaction *nsap, *osap;
15412c42a146SMarcel Moolenaar 	int error;
1542df8bae1dSRodney W. Grimes 
15436f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
15446f841fb7SMarcel Moolenaar 		return (EINVAL);
15456f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
15466f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
15472c42a146SMarcel Moolenaar 	if (nsap) {
15486f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
15492c42a146SMarcel Moolenaar 		if (error)
1550df8bae1dSRodney W. Grimes 			return (error);
15512c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
15522c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
15532c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
15542c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1555df8bae1dSRodney W. Grimes 	}
15565edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
15572c42a146SMarcel Moolenaar 	if (osap && !error) {
15582c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
15592c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
15602c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
15612c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
15622c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
15636f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
15642c42a146SMarcel Moolenaar 	}
15652c42a146SMarcel Moolenaar 	return (error);
1566df8bae1dSRodney W. Grimes }
1567df8bae1dSRodney W. Grimes 
1568d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1569df8bae1dSRodney W. Grimes struct osigblock_args {
1570df8bae1dSRodney W. Grimes 	int	mask;
1571df8bae1dSRodney W. Grimes };
1572d2d3e875SBruce Evans #endif
157326f9a767SRodney W. Grimes int
osigblock(struct thread * td,struct osigblock_args * uap)1574e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap)
1575df8bae1dSRodney W. Grimes {
1576d6e029adSKonstantin Belousov 	sigset_t set, oset;
1577df8bae1dSRodney W. Grimes 
15782c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1579d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1580d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1581df8bae1dSRodney W. Grimes 	return (0);
1582df8bae1dSRodney W. Grimes }
1583df8bae1dSRodney W. Grimes 
1584d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1585df8bae1dSRodney W. Grimes struct osigsetmask_args {
1586df8bae1dSRodney W. Grimes 	int	mask;
1587df8bae1dSRodney W. Grimes };
1588d2d3e875SBruce Evans #endif
158926f9a767SRodney W. Grimes int
osigsetmask(struct thread * td,struct osigsetmask_args * uap)1590e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1591df8bae1dSRodney W. Grimes {
1592d6e029adSKonstantin Belousov 	sigset_t set, oset;
1593df8bae1dSRodney W. Grimes 
15942c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1595d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1596d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1597df8bae1dSRodney W. Grimes 	return (0);
1598df8bae1dSRodney W. Grimes }
15991930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1600df8bae1dSRodney W. Grimes 
1601df8bae1dSRodney W. Grimes /*
1602873fbcd7SRobert Watson  * Suspend calling thread until signal, providing mask to be set in the
1603873fbcd7SRobert Watson  * meantime.
1604df8bae1dSRodney W. Grimes  */
1605d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1606df8bae1dSRodney W. Grimes struct sigsuspend_args {
16072c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1608df8bae1dSRodney W. Grimes };
1609d2d3e875SBruce Evans #endif
1610df8bae1dSRodney W. Grimes /* ARGSUSED */
161126f9a767SRodney W. Grimes int
sys_sigsuspend(struct thread * td,struct sigsuspend_args * uap)1612e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1613df8bae1dSRodney W. Grimes {
16142c42a146SMarcel Moolenaar 	sigset_t mask;
16152c42a146SMarcel Moolenaar 	int error;
16162c42a146SMarcel Moolenaar 
16176f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
16182c42a146SMarcel Moolenaar 	if (error)
16192c42a146SMarcel Moolenaar 		return (error);
16208f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
16218f19eb88SIan Dowse }
16228f19eb88SIan Dowse 
16238f19eb88SIan Dowse int
kern_sigsuspend(struct thread * td,sigset_t mask)16248f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
16258f19eb88SIan Dowse {
16268f19eb88SIan Dowse 	struct proc *p = td->td_proc;
162784440afbSKonstantin Belousov 	int has_sig, sig;
1628df8bae1dSRodney W. Grimes 
1629dbec10e0SJonathan T. Looney 	/* Ensure the sigfastblock value is up to date. */
1630dbec10e0SJonathan T. Looney 	sigfastblock_fetch(td);
1631dbec10e0SJonathan T. Looney 
1632df8bae1dSRodney W. Grimes 	/*
1633645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1634df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1635df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1636df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1637df8bae1dSRodney W. Grimes 	 * to indicate this.
1638df8bae1dSRodney W. Grimes 	 */
1639628d2653SJohn Baldwin 	PROC_LOCK(p);
164084440afbSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
164184440afbSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED);
16425e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1643c6d31b83SKonstantin Belousov 	ast_sched(td, TDA_SIGSUSPEND);
164484440afbSKonstantin Belousov 
164584440afbSKonstantin Belousov 	/*
164684440afbSKonstantin Belousov 	 * Process signals now. Otherwise, we can get spurious wakeup
164784440afbSKonstantin Belousov 	 * due to signal entered process queue, but delivered to other
164884440afbSKonstantin Belousov 	 * thread. But sigsuspend should return only on signal
164984440afbSKonstantin Belousov 	 * delivery.
165084440afbSKonstantin Belousov 	 */
1651afe1a688SKonstantin Belousov 	(p->p_sysent->sv_set_syscall_retval)(td, EINTR);
165284440afbSKonstantin Belousov 	for (has_sig = 0; !has_sig;) {
165384440afbSKonstantin Belousov 		while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
165484440afbSKonstantin Belousov 			0) == 0)
16552c42a146SMarcel Moolenaar 			/* void */;
165684440afbSKonstantin Belousov 		thread_suspend_check(0);
165784440afbSKonstantin Belousov 		mtx_lock(&p->p_sigacts->ps_mtx);
165846e47c4fSKonstantin Belousov 		while ((sig = cursig(td)) != 0) {
165946e47c4fSKonstantin Belousov 			KASSERT(sig >= 0, ("sig %d", sig));
166075c586a4SKonstantin Belousov 			has_sig += postsig(sig);
166146e47c4fSKonstantin Belousov 		}
166284440afbSKonstantin Belousov 		mtx_unlock(&p->p_sigacts->ps_mtx);
1663ef401a85SKonstantin Belousov 
1664ef401a85SKonstantin Belousov 		/*
1665ef401a85SKonstantin Belousov 		 * If PTRACE_SCE or PTRACE_SCX were set after
1666ef401a85SKonstantin Belousov 		 * userspace entered the syscall, return spurious
1667ef401a85SKonstantin Belousov 		 * EINTR.
1668ef401a85SKonstantin Belousov 		 */
1669ef401a85SKonstantin Belousov 		if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1670ef401a85SKonstantin Belousov 			has_sig += 1;
167184440afbSKonstantin Belousov 	}
1672628d2653SJohn Baldwin 	PROC_UNLOCK(p);
16732dd9ea6fSKonstantin Belousov 	td->td_errno = EINTR;
16742dd9ea6fSKonstantin Belousov 	td->td_pflags |= TDP_NERRNO;
167575c586a4SKonstantin Belousov 	return (EJUSTRETURN);
16762c42a146SMarcel Moolenaar }
16772c42a146SMarcel Moolenaar 
167831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
167997563428SAlexander Kabaev /*
168097563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
168197563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
168297563428SAlexander Kabaev  */
16832c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
16842c42a146SMarcel Moolenaar struct osigsuspend_args {
16852c42a146SMarcel Moolenaar 	osigset_t mask;
16862c42a146SMarcel Moolenaar };
16872c42a146SMarcel Moolenaar #endif
16882c42a146SMarcel Moolenaar /* ARGSUSED */
16892c42a146SMarcel Moolenaar int
osigsuspend(struct thread * td,struct osigsuspend_args * uap)1690e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap)
16912c42a146SMarcel Moolenaar {
1692645682fdSLuoqi Chen 	sigset_t mask;
16932c42a146SMarcel Moolenaar 
1694645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
169584440afbSKonstantin Belousov 	return (kern_sigsuspend(td, mask));
1696df8bae1dSRodney W. Grimes }
169731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1698df8bae1dSRodney W. Grimes 
16991930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1700d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1701df8bae1dSRodney W. Grimes struct osigstack_args {
1702df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1703df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1704df8bae1dSRodney W. Grimes };
1705d2d3e875SBruce Evans #endif
1706df8bae1dSRodney W. Grimes /* ARGSUSED */
170726f9a767SRodney W. Grimes int
osigstack(struct thread * td,struct osigstack_args * uap)1708e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap)
1709df8bae1dSRodney W. Grimes {
17105afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1711fb99ab88SMatthew Dillon 	int error = 0;
1712fb99ab88SMatthew Dillon 
1713d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
17145afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
17155afe0c99SJohn Baldwin 		if (error)
17165afe0c99SJohn Baldwin 			return (error);
1717df8bae1dSRodney W. Grimes 	}
1718a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
17195afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
17205afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1721a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1722a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1723a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1724a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
17255afe0c99SJohn Baldwin 	}
17265afe0c99SJohn Baldwin 	if (uap->oss != NULL)
17275afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
17285afe0c99SJohn Baldwin 
1729fb99ab88SMatthew Dillon 	return (error);
1730df8bae1dSRodney W. Grimes }
17311930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1732df8bae1dSRodney W. Grimes 
1733d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1734df8bae1dSRodney W. Grimes struct sigaltstack_args {
17352c42a146SMarcel Moolenaar 	stack_t	*ss;
17362c42a146SMarcel Moolenaar 	stack_t	*oss;
1737df8bae1dSRodney W. Grimes };
1738d2d3e875SBruce Evans #endif
1739df8bae1dSRodney W. Grimes /* ARGSUSED */
174026f9a767SRodney W. Grimes int
sys_sigaltstack(struct thread * td,struct sigaltstack_args * uap)1741e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1742df8bae1dSRodney W. Grimes {
17438f19eb88SIan Dowse 	stack_t ss, oss;
17448f19eb88SIan Dowse 	int error;
17458f19eb88SIan Dowse 
17468f19eb88SIan Dowse 	if (uap->ss != NULL) {
17478f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
17488f19eb88SIan Dowse 		if (error)
17498f19eb88SIan Dowse 			return (error);
17508f19eb88SIan Dowse 	}
17518f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
17528f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
17538f19eb88SIan Dowse 	if (error)
17548f19eb88SIan Dowse 		return (error);
17558f19eb88SIan Dowse 	if (uap->oss != NULL)
17568f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
17578f19eb88SIan Dowse 	return (error);
17588f19eb88SIan Dowse }
17598f19eb88SIan Dowse 
17608f19eb88SIan Dowse int
kern_sigaltstack(struct thread * td,stack_t * ss,stack_t * oss)17618f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
17628f19eb88SIan Dowse {
1763b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1764fb99ab88SMatthew Dillon 	int oonstack;
1765fb99ab88SMatthew Dillon 
1766b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1767d034d459SMarcel Moolenaar 
17688f19eb88SIan Dowse 	if (oss != NULL) {
1769a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1770a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1771d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1772df8bae1dSRodney W. Grimes 	}
1773d034d459SMarcel Moolenaar 
17748f19eb88SIan Dowse 	if (ss != NULL) {
1775a30ec4b9SDavid Xu 		if (oonstack)
1776cf60731bSJohn Baldwin 			return (EPERM);
1777a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1778cf60731bSJohn Baldwin 			return (EINVAL);
17798f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
17809104847fSDavid Xu 			if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1781cf60731bSJohn Baldwin 				return (ENOMEM);
17829104847fSDavid Xu 
1783a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1784a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1785628d2653SJohn Baldwin 		} else {
1786a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1787628d2653SJohn Baldwin 		}
1788d034d459SMarcel Moolenaar 	}
1789cf60731bSJohn Baldwin 	return (0);
1790df8bae1dSRodney W. Grimes }
1791df8bae1dSRodney W. Grimes 
17920cc9fb75SKonstantin Belousov struct killpg1_ctx {
17930cc9fb75SKonstantin Belousov 	struct thread *td;
17940cc9fb75SKonstantin Belousov 	ksiginfo_t *ksi;
17950cc9fb75SKonstantin Belousov 	int sig;
17960cc9fb75SKonstantin Belousov 	bool sent;
17970cc9fb75SKonstantin Belousov 	bool found;
17980cc9fb75SKonstantin Belousov 	int ret;
17990cc9fb75SKonstantin Belousov };
18000cc9fb75SKonstantin Belousov 
18010cc9fb75SKonstantin Belousov static void
killpg1_sendsig_locked(struct proc * p,struct killpg1_ctx * arg)180269413598SMateusz Guzik killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg)
18030cc9fb75SKonstantin Belousov {
18040cc9fb75SKonstantin Belousov 	int err;
18050cc9fb75SKonstantin Belousov 
18060cc9fb75SKonstantin Belousov 	err = p_cansignal(arg->td, p, arg->sig);
18070cc9fb75SKonstantin Belousov 	if (err == 0 && arg->sig != 0)
18080cc9fb75SKonstantin Belousov 		pksignal(p, arg->sig, arg->ksi);
18090cc9fb75SKonstantin Belousov 	if (err != ESRCH)
18100cc9fb75SKonstantin Belousov 		arg->found = true;
18110cc9fb75SKonstantin Belousov 	if (err == 0)
18120cc9fb75SKonstantin Belousov 		arg->sent = true;
18130cc9fb75SKonstantin Belousov 	else if (arg->ret == 0 && err != ESRCH && err != EPERM)
18140cc9fb75SKonstantin Belousov 		arg->ret = err;
18150cc9fb75SKonstantin Belousov }
18160cc9fb75SKonstantin Belousov 
181769413598SMateusz Guzik static void
killpg1_sendsig(struct proc * p,bool notself,struct killpg1_ctx * arg)181869413598SMateusz Guzik killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
181969413598SMateusz Guzik {
182069413598SMateusz Guzik 
182169413598SMateusz Guzik 	if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
182269413598SMateusz Guzik 	    (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
182369413598SMateusz Guzik 		return;
182469413598SMateusz Guzik 
182569413598SMateusz Guzik 	PROC_LOCK(p);
182669413598SMateusz Guzik 	killpg1_sendsig_locked(p, arg);
182769413598SMateusz Guzik 	PROC_UNLOCK(p);
182869413598SMateusz Guzik }
182969413598SMateusz Guzik 
183069413598SMateusz Guzik static void
kill_processes_prison_cb(struct proc * p,void * arg)183169413598SMateusz Guzik kill_processes_prison_cb(struct proc *p, void *arg)
183269413598SMateusz Guzik {
183369413598SMateusz Guzik 	struct killpg1_ctx *ctx = arg;
183469413598SMateusz Guzik 
183569413598SMateusz Guzik 	if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
183669413598SMateusz Guzik 	    (p == ctx->td->td_proc) || p->p_state == PRS_NEW)
183769413598SMateusz Guzik 		return;
183869413598SMateusz Guzik 
183969413598SMateusz Guzik 	killpg1_sendsig_locked(p, ctx);
184069413598SMateusz Guzik }
184169413598SMateusz Guzik 
1842d93f860cSPoul-Henning Kamp /*
1843d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
18444b59d172SKonstantin Belousov  * td is the calling thread, as usual.
1845d93f860cSPoul-Henning Kamp  */
184637c84183SPoul-Henning Kamp static int
killpg1(struct thread * td,int sig,int pgid,int all,ksiginfo_t * ksi)1847a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1848d93f860cSPoul-Henning Kamp {
1849a3de221dSKonstantin Belousov 	struct proc *p;
1850d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
18510cc9fb75SKonstantin Belousov 	struct killpg1_ctx arg;
1852d93f860cSPoul-Henning Kamp 
18530cc9fb75SKonstantin Belousov 	arg.td = td;
18540cc9fb75SKonstantin Belousov 	arg.ksi = ksi;
18550cc9fb75SKonstantin Belousov 	arg.sig = sig;
18560cc9fb75SKonstantin Belousov 	arg.sent = false;
18570cc9fb75SKonstantin Belousov 	arg.found = false;
18580cc9fb75SKonstantin Belousov 	arg.ret = 0;
1859553629ebSJake Burkholder 	if (all) {
1860d93f860cSPoul-Henning Kamp 		/*
1861d93f860cSPoul-Henning Kamp 		 * broadcast
1862d93f860cSPoul-Henning Kamp 		 */
186369413598SMateusz Guzik 		prison_proc_iterate(td->td_ucred->cr_prison,
186469413598SMateusz Guzik 		    kill_processes_prison_cb, &arg);
1865553629ebSJake Burkholder 	} else {
18663360b485SKonstantin Belousov again:
1867ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1868f591779bSSeigo Tanimura 		if (pgid == 0) {
1869d93f860cSPoul-Henning Kamp 			/*
1870d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1871d93f860cSPoul-Henning Kamp 			 */
18729c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1873f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1874f591779bSSeigo Tanimura 		} else {
1875d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1876f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1877ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1878d93f860cSPoul-Henning Kamp 				return (ESRCH);
1879d93f860cSPoul-Henning Kamp 			}
1880f591779bSSeigo Tanimura 		}
1881ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
18823360b485SKonstantin Belousov 		if (!sx_try_xlock(&pgrp->pg_killsx)) {
18833360b485SKonstantin Belousov 			PGRP_UNLOCK(pgrp);
18843360b485SKonstantin Belousov 			sx_xlock(&pgrp->pg_killsx);
18853360b485SKonstantin Belousov 			sx_xunlock(&pgrp->pg_killsx);
18863360b485SKonstantin Belousov 			goto again;
18873360b485SKonstantin Belousov 		}
18882e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
18890cc9fb75SKonstantin Belousov 			killpg1_sendsig(p, false, &arg);
1890d93f860cSPoul-Henning Kamp 		}
1891f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
18923360b485SKonstantin Belousov 		sx_xunlock(&pgrp->pg_killsx);
1893d93f860cSPoul-Henning Kamp 	}
18940cc9fb75SKonstantin Belousov 	MPASS(arg.ret != 0 || arg.found || !arg.sent);
18950cc9fb75SKonstantin Belousov 	if (arg.ret == 0 && !arg.sent)
18960cc9fb75SKonstantin Belousov 		arg.ret = arg.found ? EPERM : ESRCH;
18970cc9fb75SKonstantin Belousov 	return (arg.ret);
1898d93f860cSPoul-Henning Kamp }
1899d93f860cSPoul-Henning Kamp 
1900d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1901df8bae1dSRodney W. Grimes struct kill_args {
1902df8bae1dSRodney W. Grimes 	int	pid;
1903df8bae1dSRodney W. Grimes 	int	signum;
1904df8bae1dSRodney W. Grimes };
1905d2d3e875SBruce Evans #endif
1906df8bae1dSRodney W. Grimes /* ARGSUSED */
190726f9a767SRodney W. Grimes int
sys_kill(struct thread * td,struct kill_args * uap)19088451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap)
1909df8bae1dSRodney W. Grimes {
191034ad5ac2SEdward Tomasz Napierala 
191134ad5ac2SEdward Tomasz Napierala 	return (kern_kill(td, uap->pid, uap->signum));
191234ad5ac2SEdward Tomasz Napierala }
191334ad5ac2SEdward Tomasz Napierala 
191434ad5ac2SEdward Tomasz Napierala int
kern_kill(struct thread * td,pid_t pid,int signum)191534ad5ac2SEdward Tomasz Napierala kern_kill(struct thread *td, pid_t pid, int signum)
191634ad5ac2SEdward Tomasz Napierala {
1917a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1918a3de221dSKonstantin Belousov 	struct proc *p;
191990af4afaSJohn Baldwin 	int error;
1920df8bae1dSRodney W. Grimes 
19218890f5d0SPawel Jakub Dawidek 	/*
19228890f5d0SPawel Jakub Dawidek 	 * A process in capability mode can send signals only to himself.
19238890f5d0SPawel Jakub Dawidek 	 * The main rationale behind this is that abort(3) is implemented as
19248890f5d0SPawel Jakub Dawidek 	 * kill(getpid(), SIGABRT).
19258890f5d0SPawel Jakub Dawidek 	 */
19266a4616a5SJake Freeland 	if (pid != td->td_proc->p_pid) {
19276a4616a5SJake Freeland 		if (CAP_TRACING(td))
19286a4616a5SJake Freeland 			ktrcapfail(CAPFAIL_SIGNAL, &signum);
19296a4616a5SJake Freeland 		if (IN_CAPABILITY_MODE(td))
19308890f5d0SPawel Jakub Dawidek 			return (ECAPMODE);
19316a4616a5SJake Freeland 	}
19328890f5d0SPawel Jakub Dawidek 
193334ad5ac2SEdward Tomasz Napierala 	AUDIT_ARG_SIGNUM(signum);
193434ad5ac2SEdward Tomasz Napierala 	AUDIT_ARG_PID(pid);
193534ad5ac2SEdward Tomasz Napierala 	if ((u_int)signum > _SIG_MAXSIG)
1936df8bae1dSRodney W. Grimes 		return (EINVAL);
1937fb99ab88SMatthew Dillon 
1938a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
193934ad5ac2SEdward Tomasz Napierala 	ksi.ksi_signo = signum;
1940a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1941a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1942a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1943a3de221dSKonstantin Belousov 
194434ad5ac2SEdward Tomasz Napierala 	if (pid > 0) {
1945df8bae1dSRodney W. Grimes 		/* kill single process */
194634ad5ac2SEdward Tomasz Napierala 		if ((p = pfind_any(pid)) == NULL)
194790af4afaSJohn Baldwin 			return (ESRCH);
194814961ba7SRobert Watson 		AUDIT_ARG_PROCESS(p);
194934ad5ac2SEdward Tomasz Napierala 		error = p_cansignal(td, p, signum);
195034ad5ac2SEdward Tomasz Napierala 		if (error == 0 && signum)
195134ad5ac2SEdward Tomasz Napierala 			pksignal(p, signum, &ksi);
1952628d2653SJohn Baldwin 		PROC_UNLOCK(p);
195390af4afaSJohn Baldwin 		return (error);
1954df8bae1dSRodney W. Grimes 	}
195534ad5ac2SEdward Tomasz Napierala 	switch (pid) {
1956df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
195734ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, 0, 1, &ksi));
1958df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
195934ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, 0, 0, &ksi));
1960df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
196134ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, -pid, 0, &ksi));
1962df8bae1dSRodney W. Grimes 	}
196390af4afaSJohn Baldwin 	/* NOTREACHED */
1964df8bae1dSRodney W. Grimes }
1965df8bae1dSRodney W. Grimes 
1966cfb5f768SJonathan Anderson int
sys_pdkill(struct thread * td,struct pdkill_args * uap)1967e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap)
1968cfb5f768SJonathan Anderson {
1969cfb5f768SJonathan Anderson 	struct proc *p;
1970cfb5f768SJonathan Anderson 	int error;
1971cfb5f768SJonathan Anderson 
1972cfb5f768SJonathan Anderson 	AUDIT_ARG_SIGNUM(uap->signum);
1973cfb5f768SJonathan Anderson 	AUDIT_ARG_FD(uap->fd);
1974cfb5f768SJonathan Anderson 	if ((u_int)uap->signum > _SIG_MAXSIG)
1975cfb5f768SJonathan Anderson 		return (EINVAL);
1976cfb5f768SJonathan Anderson 
1977cbd92ce6SMatt Macy 	error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1978cfb5f768SJonathan Anderson 	if (error)
1979cfb5f768SJonathan Anderson 		return (error);
1980cfb5f768SJonathan Anderson 	AUDIT_ARG_PROCESS(p);
1981cfb5f768SJonathan Anderson 	error = p_cansignal(td, p, uap->signum);
1982cfb5f768SJonathan Anderson 	if (error == 0 && uap->signum)
19838451d0ddSKip Macy 		kern_psignal(p, uap->signum);
1984cfb5f768SJonathan Anderson 	PROC_UNLOCK(p);
1985cfb5f768SJonathan Anderson 	return (error);
1986cfb5f768SJonathan Anderson }
1987cfb5f768SJonathan Anderson 
19881930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1989d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1990df8bae1dSRodney W. Grimes struct okillpg_args {
1991df8bae1dSRodney W. Grimes 	int	pgid;
1992df8bae1dSRodney W. Grimes 	int	signum;
1993df8bae1dSRodney W. Grimes };
1994d2d3e875SBruce Evans #endif
1995df8bae1dSRodney W. Grimes /* ARGSUSED */
199626f9a767SRodney W. Grimes int
okillpg(struct thread * td,struct okillpg_args * uap)1997a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap)
1998df8bae1dSRodney W. Grimes {
1999a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
2000df8bae1dSRodney W. Grimes 
200114961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->signum);
200214961ba7SRobert Watson 	AUDIT_ARG_PID(uap->pgid);
20036c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
2004df8bae1dSRodney W. Grimes 		return (EINVAL);
20059104847fSDavid Xu 
2006a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
2007a3de221dSKonstantin Belousov 	ksi.ksi_signo = uap->signum;
2008a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
2009a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
2010a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
2011a3de221dSKonstantin Belousov 	return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
2012df8bae1dSRodney W. Grimes }
20131930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
2014df8bae1dSRodney W. Grimes 
20159104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
20169104847fSDavid Xu struct sigqueue_args {
20179104847fSDavid Xu 	pid_t pid;
20189104847fSDavid Xu 	int signum;
20199104847fSDavid Xu 	/* union sigval */ void *value;
20209104847fSDavid Xu };
20219104847fSDavid Xu #endif
20229104847fSDavid Xu int
sys_sigqueue(struct thread * td,struct sigqueue_args * uap)20238451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
20249104847fSDavid Xu {
2025f19351aaSBrooks Davis 	union sigval sv;
2026f19351aaSBrooks Davis 
2027f19351aaSBrooks Davis 	sv.sival_ptr = uap->value;
2028f19351aaSBrooks Davis 
2029f19351aaSBrooks Davis 	return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2030f19351aaSBrooks Davis }
2031f19351aaSBrooks Davis 
2032f19351aaSBrooks Davis int
kern_sigqueue(struct thread * td,pid_t pid,int signumf,union sigval * value)203353186bc1SKonstantin Belousov kern_sigqueue(struct thread *td, pid_t pid, int signumf, union sigval *value)
2034f19351aaSBrooks Davis {
20359104847fSDavid Xu 	ksiginfo_t ksi;
20369104847fSDavid Xu 	struct proc *p;
203753186bc1SKonstantin Belousov 	struct thread *td2;
203853186bc1SKonstantin Belousov 	u_int signum;
20399104847fSDavid Xu 	int error;
20409104847fSDavid Xu 
204153186bc1SKonstantin Belousov 	signum = signumf & ~__SIGQUEUE_TID;
204253186bc1SKonstantin Belousov 	if (signum > _SIG_MAXSIG)
20439104847fSDavid Xu 		return (EINVAL);
20449104847fSDavid Xu 
20459104847fSDavid Xu 	/*
20469104847fSDavid Xu 	 * Specification says sigqueue can only send signal to
20479104847fSDavid Xu 	 * single process.
20489104847fSDavid Xu 	 */
2049f19351aaSBrooks Davis 	if (pid <= 0)
20509104847fSDavid Xu 		return (EINVAL);
20519104847fSDavid Xu 
205253186bc1SKonstantin Belousov 	if ((signumf & __SIGQUEUE_TID) == 0) {
2053537d0fb1SMateusz Guzik 		if ((p = pfind_any(pid)) == NULL)
20549104847fSDavid Xu 			return (ESRCH);
205553186bc1SKonstantin Belousov 		td2 = NULL;
205653186bc1SKonstantin Belousov 	} else {
205753186bc1SKonstantin Belousov 		p = td->td_proc;
205853186bc1SKonstantin Belousov 		td2 = tdfind((lwpid_t)pid, p->p_pid);
205953186bc1SKonstantin Belousov 		if (td2 == NULL)
206053186bc1SKonstantin Belousov 			return (ESRCH);
206153186bc1SKonstantin Belousov 	}
206253186bc1SKonstantin Belousov 
2063f19351aaSBrooks Davis 	error = p_cansignal(td, p, signum);
2064f19351aaSBrooks Davis 	if (error == 0 && signum != 0) {
20659104847fSDavid Xu 		ksiginfo_init(&ksi);
2066a3de221dSKonstantin Belousov 		ksi.ksi_flags = KSI_SIGQ;
2067f19351aaSBrooks Davis 		ksi.ksi_signo = signum;
20689104847fSDavid Xu 		ksi.ksi_code = SI_QUEUE;
20699104847fSDavid Xu 		ksi.ksi_pid = td->td_proc->p_pid;
20709104847fSDavid Xu 		ksi.ksi_uid = td->td_ucred->cr_ruid;
2071f19351aaSBrooks Davis 		ksi.ksi_value = *value;
207253186bc1SKonstantin Belousov 		error = tdsendsignal(p, td2, ksi.ksi_signo, &ksi);
20739104847fSDavid Xu 	}
20749104847fSDavid Xu 	PROC_UNLOCK(p);
20759104847fSDavid Xu 	return (error);
20769104847fSDavid Xu }
20779104847fSDavid Xu 
2078df8bae1dSRodney W. Grimes /*
2079df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
2080df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
2081df8bae1dSRodney W. Grimes  */
2082df8bae1dSRodney W. Grimes void
pgsignal(struct pgrp * pgrp,int sig,int checkctty,ksiginfo_t * ksi)2083a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2084df8bae1dSRodney W. Grimes {
2085a3de221dSKonstantin Belousov 	struct proc *p;
2086df8bae1dSRodney W. Grimes 
2087628d2653SJohn Baldwin 	if (pgrp) {
2088f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2089628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2090628d2653SJohn Baldwin 			PROC_LOCK(p);
2091e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
2092e806d352SJohn Baldwin 			    (checkctty == 0 || p->p_flag & P_CONTROLT))
2093a3de221dSKonstantin Belousov 				pksignal(p, sig, ksi);
2094628d2653SJohn Baldwin 			PROC_UNLOCK(p);
2095628d2653SJohn Baldwin 		}
2096628d2653SJohn Baldwin 	}
2097df8bae1dSRodney W. Grimes }
2098df8bae1dSRodney W. Grimes 
2099e442f29fSKonstantin Belousov /*
2100e442f29fSKonstantin Belousov  * Recalculate the signal mask and reset the signal disposition after
2101e442f29fSKonstantin Belousov  * usermode frame for delivery is formed.  Should be called after
2102e442f29fSKonstantin Belousov  * mach-specific routine, because sysent->sv_sendsig() needs correct
2103e442f29fSKonstantin Belousov  * ps_siginfo and signal mask.
2104e442f29fSKonstantin Belousov  */
2105e442f29fSKonstantin Belousov static void
postsig_done(int sig,struct thread * td,struct sigacts * ps)2106e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps)
2107e442f29fSKonstantin Belousov {
2108e442f29fSKonstantin Belousov 	sigset_t mask;
2109e442f29fSKonstantin Belousov 
2110e442f29fSKonstantin Belousov 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2111e442f29fSKonstantin Belousov 	td->td_ru.ru_nsignals++;
2112e442f29fSKonstantin Belousov 	mask = ps->ps_catchmask[_SIG_IDX(sig)];
2113e442f29fSKonstantin Belousov 	if (!SIGISMEMBER(ps->ps_signodefer, sig))
2114e442f29fSKonstantin Belousov 		SIGADDSET(mask, sig);
2115e442f29fSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2116e442f29fSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2117e442f29fSKonstantin Belousov 	if (SIGISMEMBER(ps->ps_sigreset, sig))
2118e442f29fSKonstantin Belousov 		sigdflt(ps, sig);
2119e442f29fSKonstantin Belousov }
2120e442f29fSKonstantin Belousov 
2121df8bae1dSRodney W. Grimes /*
21220c14ff0eSRobert Watson  * Send a signal caused by a trap to the current thread.  If it will be
21230c14ff0eSRobert Watson  * caught immediately, deliver it with correct code.  Otherwise, post it
21240c14ff0eSRobert Watson  * normally.
2125df8bae1dSRodney W. Grimes  */
2126df8bae1dSRodney W. Grimes void
trapsignal(struct thread * td,ksiginfo_t * ksi)21279104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi)
2128df8bae1dSRodney W. Grimes {
21291bf4700bSJeff Roberson 	struct sigacts *ps;
21301bf4700bSJeff Roberson 	struct proc *p;
2131146fc63fSKonstantin Belousov 	sigset_t sigmask;
2132c5c981d4SMateusz Guzik 	int sig;
21331bf4700bSJeff Roberson 
21341bf4700bSJeff Roberson 	p = td->td_proc;
21359104847fSDavid Xu 	sig = ksi->ksi_signo;
21369104847fSDavid Xu 	KASSERT(_SIG_VALID(sig), ("invalid signal"));
21379104847fSDavid Xu 
2138a113b17fSKonstantin Belousov 	sigfastblock_fetch(td);
2139628d2653SJohn Baldwin 	PROC_LOCK(p);
2140ef3dab76STim J. Robbins 	ps = p->p_sigacts;
214190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2142146fc63fSKonstantin Belousov 	sigmask = td->td_sigmask;
2143146fc63fSKonstantin Belousov 	if (td->td_sigblock_val != 0)
2144146fc63fSKonstantin Belousov 		SIGSETOR(sigmask, fastblock_mask);
214590af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2146146fc63fSKonstantin Belousov 	    !SIGISMEMBER(sigmask, sig)) {
2147df8bae1dSRodney W. Grimes #ifdef KTRACE
2148374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
2149374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2150c5c981d4SMateusz Guzik 			    &td->td_sigmask, ksi->ksi_code);
2151df8bae1dSRodney W. Grimes #endif
21529104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
21539104847fSDavid Xu 		    ksi, &td->td_sigmask);
2154e442f29fSKonstantin Belousov 		postsig_done(sig, td, ps);
215590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
21566f841fb7SMarcel Moolenaar 	} else {
2157f71a882fSDavid Xu 		/*
2158f71a882fSDavid Xu 		 * Avoid a possible infinite loop if the thread
2159f71a882fSDavid Xu 		 * masking the signal or process is ignoring the
2160f71a882fSDavid Xu 		 * signal.
2161f71a882fSDavid Xu 		 */
2162146fc63fSKonstantin Belousov 		if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2163f71a882fSDavid Xu 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2164f71a882fSDavid Xu 			SIGDELSET(td->td_sigmask, sig);
2165f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigcatch, sig);
2166f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigignore, sig);
2167f71a882fSDavid Xu 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2168146fc63fSKonstantin Belousov 			td->td_pflags &= ~TDP_SIGFASTBLOCK;
2169146fc63fSKonstantin Belousov 			td->td_sigblock_val = 0;
2170f71a882fSDavid Xu 		}
217190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
21722c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
2173ad6eec7bSJohn Baldwin 		tdsendsignal(p, td, sig, ksi);
2174df8bae1dSRodney W. Grimes 	}
2175628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2176df8bae1dSRodney W. Grimes }
2177df8bae1dSRodney W. Grimes 
21784093529dSJeff Roberson static struct thread *
sigtd(struct proc * p,int sig,bool fast_sigblock)2179146fc63fSKonstantin Belousov sigtd(struct proc *p, int sig, bool fast_sigblock)
21804093529dSJeff Roberson {
21813074d1b4SDavid Xu 	struct thread *td, *signal_td;
21824093529dSJeff Roberson 
21834093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
2184146fc63fSKonstantin Belousov 	MPASS(!fast_sigblock || p == curproc);
21854093529dSJeff Roberson 
21864093529dSJeff Roberson 	/*
2187627451c1SDavid Xu 	 * Check if current thread can handle the signal without
218815b7a831SKonstantin Belousov 	 * switching context to another thread.
21894093529dSJeff Roberson 	 */
2190146fc63fSKonstantin Belousov 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2191146fc63fSKonstantin Belousov 	    (!fast_sigblock || curthread->td_sigblock_val == 0))
2192627451c1SDavid Xu 		return (curthread);
2193dfe17248SKonstantin Belousov 
2194dfe17248SKonstantin Belousov 	/* Find a non-stopped thread that does not mask the signal. */
21953074d1b4SDavid Xu 	signal_td = NULL;
21963074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
2197146fc63fSKonstantin Belousov 		if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2198dfe17248SKonstantin Belousov 		    td != curthread || td->td_sigblock_val == 0) &&
2199dfe17248SKonstantin Belousov 		    (td->td_flags & TDF_BOUNDARY) == 0) {
22003074d1b4SDavid Xu 			signal_td = td;
2201627451c1SDavid Xu 			break;
22023074d1b4SDavid Xu 		}
22033074d1b4SDavid Xu 	}
2204dfe17248SKonstantin Belousov 	/* Select random (first) thread if no better match was found. */
22053074d1b4SDavid Xu 	if (signal_td == NULL)
22063074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
22073074d1b4SDavid Xu 	return (signal_td);
22084093529dSJeff Roberson }
22094093529dSJeff Roberson 
2210df8bae1dSRodney W. Grimes /*
2211df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
2212df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
2213df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
2214df8bae1dSRodney W. Grimes  *
2215df8bae1dSRodney W. Grimes  * Exceptions:
2216df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
2217df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
2218df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
2219df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
2220df8bae1dSRodney W. Grimes  *
2221df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
22224dec0e67SRobert Watson  *
22234dec0e67SRobert Watson  * NB: This function may be entered from the debugger via the "kill" DDB
22244dec0e67SRobert Watson  * command.  There is little that can be done to mitigate the possibly messy
22254dec0e67SRobert Watson  * side effects of this unwise possibility.
2226df8bae1dSRodney W. Grimes  */
2227df8bae1dSRodney W. Grimes void
kern_psignal(struct proc * p,int sig)22288451d0ddSKip Macy kern_psignal(struct proc *p, int sig)
2229df8bae1dSRodney W. Grimes {
2230a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
2231a3de221dSKonstantin Belousov 
2232a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
2233a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
2234a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
2235ad6eec7bSJohn Baldwin 	(void) tdsendsignal(p, NULL, sig, &ksi);
2236a3de221dSKonstantin Belousov }
2237a3de221dSKonstantin Belousov 
2238ad6eec7bSJohn Baldwin int
pksignal(struct proc * p,int sig,ksiginfo_t * ksi)2239a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2240a3de221dSKonstantin Belousov {
2241a3de221dSKonstantin Belousov 
2242ad6eec7bSJohn Baldwin 	return (tdsendsignal(p, NULL, sig, ksi));
22439104847fSDavid Xu }
22449104847fSDavid Xu 
2245cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */
22469104847fSDavid Xu int
sigev_findtd(struct proc * p,struct sigevent * sigev,struct thread ** ttd)2247cf7d9a8cSDavid Xu sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
22489104847fSDavid Xu {
2249cf7d9a8cSDavid Xu 	struct thread *td;
225041b3077aSJacques Vidrine 
22516d7b314bSDavid Xu 	if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2252cf7d9a8cSDavid Xu 		td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
22536d7b314bSDavid Xu 		if (td == NULL)
22546d7b314bSDavid Xu 			return (ESRCH);
2255cf7d9a8cSDavid Xu 		*ttd = td;
2256cf7d9a8cSDavid Xu 	} else {
2257cf7d9a8cSDavid Xu 		*ttd = NULL;
2258cf7d9a8cSDavid Xu 		PROC_LOCK(p);
22596d7b314bSDavid Xu 	}
2260cf7d9a8cSDavid Xu 	return (0);
22614093529dSJeff Roberson }
22624093529dSJeff Roberson 
2263ad6eec7bSJohn Baldwin void
tdsignal(struct thread * td,int sig)2264ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig)
2265ad6eec7bSJohn Baldwin {
2266ad6eec7bSJohn Baldwin 	ksiginfo_t ksi;
2267ad6eec7bSJohn Baldwin 
2268ad6eec7bSJohn Baldwin 	ksiginfo_init(&ksi);
2269ad6eec7bSJohn Baldwin 	ksi.ksi_signo = sig;
2270ad6eec7bSJohn Baldwin 	ksi.ksi_code = SI_KERNEL;
2271ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, &ksi);
2272ad6eec7bSJohn Baldwin }
2273ad6eec7bSJohn Baldwin 
2274ad6eec7bSJohn Baldwin void
tdksignal(struct thread * td,int sig,ksiginfo_t * ksi)2275ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2276ad6eec7bSJohn Baldwin {
2277ad6eec7bSJohn Baldwin 
2278ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, ksi);
2279ad6eec7bSJohn Baldwin }
2280ad6eec7bSJohn Baldwin 
2281d4c4ca85SMark Johnston static void
sig_sleepq_abort(struct thread * td,int intrval)22829b86d3e5SKonstantin Belousov sig_sleepq_abort(struct thread *td, int intrval)
22839b86d3e5SKonstantin Belousov {
22849b86d3e5SKonstantin Belousov 	THREAD_LOCK_ASSERT(td, MA_OWNED);
22859b86d3e5SKonstantin Belousov 
2286d4c4ca85SMark Johnston 	if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0)
22879b86d3e5SKonstantin Belousov 		thread_unlock(td);
2288d4c4ca85SMark Johnston 	else
2289d4c4ca85SMark Johnston 		sleepq_abort(td, intrval);
22909b86d3e5SKonstantin Belousov }
22919b86d3e5SKonstantin Belousov 
2292cf7d9a8cSDavid Xu int
tdsendsignal(struct proc * p,struct thread * td,int sig,ksiginfo_t * ksi)2293ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
22944093529dSJeff Roberson {
22959104847fSDavid Xu 	sig_t action;
22969104847fSDavid Xu 	sigqueue_t *sigqueue;
229790af4afaSJohn Baldwin 	struct sigacts *ps;
2298d4c4ca85SMark Johnston 	int intrval, prop, ret;
2299df8bae1dSRodney W. Grimes 
2300cf7d9a8cSDavid Xu 	MPASS(td == NULL || p == td->td_proc);
23016d7b314bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
23026d7b314bSDavid Xu 
230341b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
2304212bc4b3SDavid Xu 		panic("%s(): invalid signal %d", __func__, sig);
23054093529dSJeff Roberson 
2306212bc4b3SDavid Xu 	KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
23076d7b314bSDavid Xu 
23086d7b314bSDavid Xu 	/*
23096d7b314bSDavid Xu 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
23106d7b314bSDavid Xu 	 */
23116d7b314bSDavid Xu 	if (p->p_state == PRS_ZOMBIE) {
23120a4f2ac3SKonstantin Belousov 		if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
23136d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
2314d4c4ca85SMark Johnston 		return (0);
23156d7b314bSDavid Xu 	}
23166d7b314bSDavid Xu 
231790af4afaSJohn Baldwin 	ps = p->p_sigacts;
23189e590ff0SKonstantin Belousov 	KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
23192c42a146SMarcel Moolenaar 	prop = sigprop(sig);
23204093529dSJeff Roberson 
23216d7b314bSDavid Xu 	if (td == NULL) {
2322146fc63fSKonstantin Belousov 		td = sigtd(p, sig, false);
23236d7b314bSDavid Xu 		sigqueue = &p->p_sigqueue;
2324462314b3SMateusz Guzik 	} else
23259104847fSDavid Xu 		sigqueue = &td->td_sigqueue;
23264093529dSJeff Roberson 
232736160958SMark Johnston 	SDT_PROBE3(proc, , , signal__send, td, p, sig);
23285d217f17SJohn Birrell 
2329df8bae1dSRodney W. Grimes 	/*
2330bc387624SKonstantin Belousov 	 * If the signal is being ignored, then we forget about it
2331bc387624SKonstantin Belousov 	 * immediately, except when the target process executes
2332bc387624SKonstantin Belousov 	 * sigwait().  (Note: we don't set SIGCONT in ps_sigignore,
2333bc387624SKonstantin Belousov 	 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2334df8bae1dSRodney W. Grimes 	 */
233590af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
23360fc32899SJohn Baldwin 	if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2337bc387624SKonstantin Belousov 		if (kern_sig_discard_ign &&
2338bc387624SKonstantin Belousov 		    (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
233936160958SMark Johnston 			SDT_PROBE3(proc, , , signal__discard, td, p, sig);
23405d217f17SJohn Birrell 
234190af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
23420a4f2ac3SKonstantin Belousov 			if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
23436d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
2344d4c4ca85SMark Johnston 			return (0);
2345bc387624SKonstantin Belousov 		} else {
2346bc387624SKonstantin Belousov 			action = SIG_CATCH;
2347f17eb93dSKonstantin Belousov 			intrval = 0;
234890af4afaSJohn Baldwin 		}
2349f17eb93dSKonstantin Belousov 	} else {
2350f17eb93dSKonstantin Belousov 		if (SIGISMEMBER(td->td_sigmask, sig))
2351df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
235290af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2353df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
2354df8bae1dSRodney W. Grimes 		else
2355df8bae1dSRodney W. Grimes 			action = SIG_DFL;
235694f0972bSDavid Xu 		if (SIGISMEMBER(ps->ps_sigintr, sig))
235794f0972bSDavid Xu 			intrval = EINTR;
235894f0972bSDavid Xu 		else
235994f0972bSDavid Xu 			intrval = ERESTART;
2360f17eb93dSKonstantin Belousov 	}
236190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
2362df8bae1dSRodney W. Grimes 
2363fd50a707SBrooks Davis 	if (prop & SIGPROP_CONT)
23649104847fSDavid Xu 		sigqueue_delete_stopmask_proc(p);
2365fd50a707SBrooks Davis 	else if (prop & SIGPROP_STOP) {
2366df8bae1dSRodney W. Grimes 		/*
2367df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
2368df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
2369df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
2370df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
2371df8bae1dSRodney W. Grimes 		 */
2372993a1699SKonstantin Belousov 		if ((prop & SIGPROP_TTYSTOP) != 0 &&
23735844bd05SKonstantin Belousov 		    (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2374993a1699SKonstantin Belousov 		    action == SIG_DFL) {
23750a4f2ac3SKonstantin Belousov 			if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
23766d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
2377d4c4ca85SMark Johnston 			return (0);
23786d7b314bSDavid Xu 		}
23799104847fSDavid Xu 		sigqueue_delete_proc(p, SIGCONT);
2380ebceaf6dSDavid Xu 		if (p->p_flag & P_CONTINUED) {
23816933e3c1SJulian Elischer 			p->p_flag &= ~P_CONTINUED;
2382ebceaf6dSDavid Xu 			PROC_LOCK(p->p_pptr);
2383ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
2384ebceaf6dSDavid Xu 			PROC_UNLOCK(p->p_pptr);
2385ebceaf6dSDavid Xu 		}
2386df8bae1dSRodney W. Grimes 	}
23873074d1b4SDavid Xu 
23889104847fSDavid Xu 	ret = sigqueue_add(sigqueue, sig, ksi);
23899104847fSDavid Xu 	if (ret != 0)
23909104847fSDavid Xu 		return (ret);
23916d7b314bSDavid Xu 	signotify(td);
23925312b1c7SDavid Xu 	/*
23935312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
23945312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
23955312b1c7SDavid Xu 	 */
23965312b1c7SDavid Xu 	if (action == SIG_HOLD &&
2397fd50a707SBrooks Davis 	    !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2398d4c4ca85SMark Johnston 		return (0);
239961a74c5cSJeff Roberson 
240090d75f78SAlfred Perlstein 	/*
2401e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
2402e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
2403e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
24041968f37bSJohn Baldwin 	 * times when processing needs to be done immediately, such as
2405e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
24061968f37bSJohn Baldwin 	 * We try to do the per-process part here.
2407df8bae1dSRodney W. Grimes 	 */
2408e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
24090f14f15bSJohn Baldwin 		KASSERT(!(p->p_flag & P_WEXIT),
24100f14f15bSJohn Baldwin 		    ("signal to stopped but exiting process"));
2411e602ba25SJulian Elischer 		if (sig == SIGKILL) {
2412137cf33dSDavid Xu 			/*
2413137cf33dSDavid Xu 			 * If traced process is already stopped,
2414137cf33dSDavid Xu 			 * then no further action is necessary.
2415137cf33dSDavid Xu 			 */
241683b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
2417d4c4ca85SMark Johnston 				return (0);
2418df8bae1dSRodney W. Grimes 			/*
2419e602ba25SJulian Elischer 			 * SIGKILL sets process running.
2420e602ba25SJulian Elischer 			 * It will die elsewhere.
2421e602ba25SJulian Elischer 			 * All threads must be restarted.
2422df8bae1dSRodney W. Grimes 			 */
2423482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
2424e602ba25SJulian Elischer 			goto runfast;
2425e602ba25SJulian Elischer 		}
2426e602ba25SJulian Elischer 
2427fd50a707SBrooks Davis 		if (prop & SIGPROP_CONT) {
2428137cf33dSDavid Xu 			/*
2429137cf33dSDavid Xu 			 * If traced process is already stopped,
2430137cf33dSDavid Xu 			 * then no further action is necessary.
2431137cf33dSDavid Xu 			 */
243283b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
2433d4c4ca85SMark Johnston 				return (0);
2434e602ba25SJulian Elischer 			/*
2435e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
24369104847fSDavid Xu 			 * process but don't leave the signal in sigqueue as
24371d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
2438e602ba25SJulian Elischer 			 * continue the process and leave the signal in
24399104847fSDavid Xu 			 * sigqueue.  If the process catches SIGCONT, let it
2440e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
2441e602ba25SJulian Elischer 			 * an event, it goes back to run state.
2442e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
2443e602ba25SJulian Elischer 			 */
24441279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
24457b4a950aSDavid Xu 			PROC_SLOCK(p);
2446ebceaf6dSDavid Xu 			if (p->p_numthreads == p->p_suspcount) {
24477b4a950aSDavid Xu 				PROC_SUNLOCK(p);
24486933e3c1SJulian Elischer 				p->p_flag |= P_CONTINUED;
2449b4490c6eSKonstantin Belousov 				p->p_xsig = SIGCONT;
24507f96995eSDavid Xu 				PROC_LOCK(p->p_pptr);
2451ebceaf6dSDavid Xu 				childproc_continued(p);
24527f96995eSDavid Xu 				PROC_UNLOCK(p->p_pptr);
24537b4a950aSDavid Xu 				PROC_SLOCK(p);
2454ebceaf6dSDavid Xu 			}
2455e602ba25SJulian Elischer 			if (action == SIG_DFL) {
2456a54e85fdSJeff Roberson 				thread_unsuspend(p);
24577b4a950aSDavid Xu 				PROC_SUNLOCK(p);
24589104847fSDavid Xu 				sigqueue_delete(sigqueue, sig);
2459dc47fdf1SKonstantin Belousov 				goto out_cont;
2460a54e85fdSJeff Roberson 			}
2461a54e85fdSJeff Roberson 			if (action == SIG_CATCH) {
24628460a577SJohn Birrell 				/*
24638460a577SJohn Birrell 				 * The process wants to catch it so it needs
24648460a577SJohn Birrell 				 * to run at least one thread, but which one?
24658460a577SJohn Birrell 				 */
24667b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2467e602ba25SJulian Elischer 				goto runfast;
2468e602ba25SJulian Elischer 			}
2469e602ba25SJulian Elischer 			/*
2470e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
2471e602ba25SJulian Elischer 			 */
247204774f23SJulian Elischer 			thread_unsuspend(p);
24737b4a950aSDavid Xu 			PROC_SUNLOCK(p);
2474dc47fdf1SKonstantin Belousov 			goto out_cont;
2475e602ba25SJulian Elischer 		}
2476e602ba25SJulian Elischer 
2477fd50a707SBrooks Davis 		if (prop & SIGPROP_STOP) {
2478137cf33dSDavid Xu 			/*
2479137cf33dSDavid Xu 			 * If traced process is already stopped,
2480137cf33dSDavid Xu 			 * then no further action is necessary.
2481137cf33dSDavid Xu 			 */
248283b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
2483d4c4ca85SMark Johnston 				return (0);
2484e602ba25SJulian Elischer 			/*
2485e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
2486e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
248704774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
2488e602ba25SJulian Elischer 			 */
24891279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
24909104847fSDavid Xu 			sigqueue_delete(sigqueue, sig);
2491d4c4ca85SMark Johnston 			return (0);
2492e602ba25SJulian Elischer 		}
2493e602ba25SJulian Elischer 
2494e602ba25SJulian Elischer 		/*
2495e602ba25SJulian Elischer 		 * All other kinds of signals:
2496e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
2497e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
2498e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
249904774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
2500e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
2501e602ba25SJulian Elischer 		 */
25027b4a950aSDavid Xu 		PROC_SLOCK(p);
2503a54e85fdSJeff Roberson 		thread_lock(td);
250461a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td))
2505d4c4ca85SMark Johnston 			sig_sleepq_abort(td, intrval);
250661a74c5cSJeff Roberson 		else
2507a54e85fdSJeff Roberson 			thread_unlock(td);
25087b4a950aSDavid Xu 		PROC_SUNLOCK(p);
2509d4c4ca85SMark Johnston 		return (0);
2510df8bae1dSRodney W. Grimes 		/*
25119a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
25129a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
2513df8bae1dSRodney W. Grimes 		 */
2514e602ba25SJulian Elischer 	} else if (p->p_state == PRS_NORMAL) {
2515ec8297bdSDavid Xu 		if (p->p_flag & P_TRACED || action == SIG_CATCH) {
251694f0972bSDavid Xu 			tdsigwakeup(td, sig, action, intrval);
2517d4c4ca85SMark Johnston 			return (0);
251804774f23SJulian Elischer 		}
2519ec8297bdSDavid Xu 
2520ec8297bdSDavid Xu 		MPASS(action == SIG_DFL);
2521ec8297bdSDavid Xu 
2522fd50a707SBrooks Davis 		if (prop & SIGPROP_STOP) {
25230f14f15bSJohn Baldwin 			if (p->p_flag & (P_PPWAIT|P_WEXIT))
2524d4c4ca85SMark Johnston 				return (0);
2525e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
2526b4490c6eSKonstantin Belousov 			p->p_xsig = sig;
25277b4a950aSDavid Xu 			PROC_SLOCK(p);
2528d4c4ca85SMark Johnston 			sig_suspend_threads(td, p);
25294093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
2530ebceaf6dSDavid Xu 				/*
2531ebceaf6dSDavid Xu 				 * only thread sending signal to another
2532ebceaf6dSDavid Xu 				 * process can reach here, if thread is sending
2533ebceaf6dSDavid Xu 				 * signal to its process, because thread does
2534ebceaf6dSDavid Xu 				 * not suspend itself here, p_numthreads
2535ebceaf6dSDavid Xu 				 * should never be equal to p_suspcount.
2536ebceaf6dSDavid Xu 				 */
2537ebceaf6dSDavid Xu 				thread_stopped(p);
25387b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2539b4490c6eSKonstantin Belousov 				sigqueue_delete_proc(p, p->p_xsig);
25407b4a950aSDavid Xu 			} else
25417b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2542d4c4ca85SMark Johnston 			return (0);
254335c32a76SDavid Xu 		}
2544e602ba25SJulian Elischer 	} else {
2545e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
25469104847fSDavid Xu 		sigqueue_delete(sigqueue, sig);
2547d4c4ca85SMark Johnston 		return (0);
2548e602ba25SJulian Elischer 	}
2549e602ba25SJulian Elischer 
2550b40ce416SJulian Elischer 	/*
2551e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
2552e602ba25SJulian Elischer 	 * running threads.
2553b40ce416SJulian Elischer 	 */
2554e602ba25SJulian Elischer runfast:
255594f0972bSDavid Xu 	tdsigwakeup(td, sig, action, intrval);
25567b4a950aSDavid Xu 	PROC_SLOCK(p);
2557e602ba25SJulian Elischer 	thread_unsuspend(p);
25587b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2559dc47fdf1SKonstantin Belousov out_cont:
2560dc47fdf1SKonstantin Belousov 	itimer_proc_continue(p);
25612fd1ffefSKonstantin Belousov 	kqtimer_proc_continue(p);
256261a74c5cSJeff Roberson 
2563d4c4ca85SMark Johnston 	return (0);
2564e602ba25SJulian Elischer }
2565e602ba25SJulian Elischer 
2566e602ba25SJulian Elischer /*
2567e602ba25SJulian Elischer  * The force of a signal has been directed against a single
2568e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
2569e602ba25SJulian Elischer  * out of any sleep it may be in etc.
2570e602ba25SJulian Elischer  */
2571e602ba25SJulian Elischer static void
tdsigwakeup(struct thread * td,int sig,sig_t action,int intrval)257294f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2573e602ba25SJulian Elischer {
2574e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
2575d4c4ca85SMark Johnston 	int prop;
2576e602ba25SJulian Elischer 
25778b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2578e602ba25SJulian Elischer 	prop = sigprop(sig);
2579a4c2da15SBruce Evans 
25807b4a950aSDavid Xu 	PROC_SLOCK(p);
2581374ae2a3SJeff Roberson 	thread_lock(td);
2582e602ba25SJulian Elischer 	/*
2583aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
2584aef68c96SKonstantin Belousov 	 * killed in this lifetime.  Be careful to avoid bumping the
2585aef68c96SKonstantin Belousov 	 * priority of the idle thread, since we still allow to signal
2586aef68c96SKonstantin Belousov 	 * kernel processes.
2587e602ba25SJulian Elischer 	 */
2588fd50a707SBrooks Davis 	if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2589aef68c96SKonstantin Belousov 	    td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2590b3a4fb14SDavid Xu 		sched_prio(td, PUSER);
259180c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
2592e602ba25SJulian Elischer 		/*
2593e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
2594e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
2595e602ba25SJulian Elischer 		 * be noticed when the process returns through
2596e602ba25SJulian Elischer 		 * trap() or syscall().
2597e602ba25SJulian Elischer 		 */
259844f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
2599374ae2a3SJeff Roberson 			goto out;
2600e602ba25SJulian Elischer 		/*
2601e602ba25SJulian Elischer 		 * If SIGCONT is default (or ignored) and process is
2602e602ba25SJulian Elischer 		 * asleep, we are finished; the process should not
2603e602ba25SJulian Elischer 		 * be awakened.
2604df8bae1dSRodney W. Grimes 		 */
2605fd50a707SBrooks Davis 		if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2606a54e85fdSJeff Roberson 			thread_unlock(td);
26077b4a950aSDavid Xu 			PROC_SUNLOCK(p);
26089104847fSDavid Xu 			sigqueue_delete(&p->p_sigqueue, sig);
26094093529dSJeff Roberson 			/*
26104093529dSJeff Roberson 			 * It may be on either list in this state.
26114093529dSJeff Roberson 			 * Remove from both for now.
26124093529dSJeff Roberson 			 */
26139104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
2614aa0fa334SJulian Elischer 			return;
2615df8bae1dSRodney W. Grimes 		}
2616df8bae1dSRodney W. Grimes 
2617aa0fa334SJulian Elischer 		/*
2618a120a7a3SJohn Baldwin 		 * Don't awaken a sleeping thread for SIGSTOP if the
2619a120a7a3SJohn Baldwin 		 * STOP signal is deferred.
2620a120a7a3SJohn Baldwin 		 */
2621fd50a707SBrooks Davis 		if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
26226f56cb8dSKonstantin Belousov 		    TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2623a120a7a3SJohn Baldwin 			goto out;
2624a120a7a3SJohn Baldwin 
2625a120a7a3SJohn Baldwin 		/*
2626a4c2da15SBruce Evans 		 * Give low priority threads a better chance to run.
2627aa0fa334SJulian Elischer 		 */
2628aef68c96SKonstantin Belousov 		if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2629b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
2630ec8297bdSDavid Xu 
2631d4c4ca85SMark Johnston 		sig_sleepq_abort(td, intrval);
263261a74c5cSJeff Roberson 		PROC_SUNLOCK(p);
263361a74c5cSJeff Roberson 		return;
263461a74c5cSJeff Roberson 	}
263561a74c5cSJeff Roberson 
2636df8bae1dSRodney W. Grimes 	/*
2637a4c2da15SBruce Evans 	 * Other states do nothing with the signal immediately,
2638df8bae1dSRodney W. Grimes 	 * other than kicking ourselves if we are running.
2639df8bae1dSRodney W. Grimes 	 * It will either never be noticed, or noticed very soon.
2640df8bae1dSRodney W. Grimes 	 */
2641a4c2da15SBruce Evans #ifdef SMP
264244f3b092SJohn Baldwin 	if (TD_IS_RUNNING(td) && td != curthread)
2643e602ba25SJulian Elischer 		forward_signal(td);
26443163861cSTor Egge #endif
264561a74c5cSJeff Roberson 
2646374ae2a3SJeff Roberson out:
26477b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2648374ae2a3SJeff Roberson 	thread_unlock(td);
2649a4c2da15SBruce Evans }
2650df8bae1dSRodney W. Grimes 
265187a64872SKonstantin Belousov static void
ptrace_coredumpreq(struct thread * td,struct proc * p,struct thr_coredump_req * tcq)2652140ceb5dSKonstantin Belousov ptrace_coredumpreq(struct thread *td, struct proc *p,
2653140ceb5dSKonstantin Belousov     struct thr_coredump_req *tcq)
2654140ceb5dSKonstantin Belousov {
2655140ceb5dSKonstantin Belousov 	void *rl_cookie;
2656140ceb5dSKonstantin Belousov 
2657140ceb5dSKonstantin Belousov 	if (p->p_sysent->sv_coredump == NULL) {
2658140ceb5dSKonstantin Belousov 		tcq->tc_error = ENOSYS;
2659140ceb5dSKonstantin Belousov 		return;
2660140ceb5dSKonstantin Belousov 	}
2661140ceb5dSKonstantin Belousov 
2662140ceb5dSKonstantin Belousov 	rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2663140ceb5dSKonstantin Belousov 	tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
2664140ceb5dSKonstantin Belousov 	    tcq->tc_limit, tcq->tc_flags);
2665140ceb5dSKonstantin Belousov 	vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2666140ceb5dSKonstantin Belousov }
2667140ceb5dSKonstantin Belousov 
2668140ceb5dSKonstantin Belousov static void
ptrace_syscallreq(struct thread * td,struct proc * p,struct thr_syscall_req * tsr)2669140ceb5dSKonstantin Belousov ptrace_syscallreq(struct thread *td, struct proc *p,
2670140ceb5dSKonstantin Belousov     struct thr_syscall_req *tsr)
2671140ceb5dSKonstantin Belousov {
2672140ceb5dSKonstantin Belousov 	struct sysentvec *sv;
2673140ceb5dSKonstantin Belousov 	struct sysent *se;
2674140ceb5dSKonstantin Belousov 	register_t rv_saved[2];
2675140ceb5dSKonstantin Belousov 	int error, nerror;
2676140ceb5dSKonstantin Belousov 	int sc;
2677140ceb5dSKonstantin Belousov 	bool audited, sy_thr_static;
2678140ceb5dSKonstantin Belousov 
2679140ceb5dSKonstantin Belousov 	sv = p->p_sysent;
2680140ceb5dSKonstantin Belousov 	if (sv->sv_table == NULL || sv->sv_size < tsr->ts_sa.code) {
2681140ceb5dSKonstantin Belousov 		tsr->ts_ret.sr_error = ENOSYS;
2682140ceb5dSKonstantin Belousov 		return;
2683140ceb5dSKonstantin Belousov 	}
2684140ceb5dSKonstantin Belousov 
2685140ceb5dSKonstantin Belousov 	sc = tsr->ts_sa.code;
2686140ceb5dSKonstantin Belousov 	if (sc == SYS_syscall || sc == SYS___syscall) {
2687974be51bSKonstantin Belousov 		sc = tsr->ts_sa.args[0];
2688140ceb5dSKonstantin Belousov 		memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1],
2689140ceb5dSKonstantin Belousov 		    sizeof(register_t) * (tsr->ts_nargs - 1));
2690140ceb5dSKonstantin Belousov 	}
2691140ceb5dSKonstantin Belousov 
2692140ceb5dSKonstantin Belousov 	tsr->ts_sa.callp = se = &sv->sv_table[sc];
2693140ceb5dSKonstantin Belousov 
2694140ceb5dSKonstantin Belousov 	VM_CNT_INC(v_syscall);
2695140ceb5dSKonstantin Belousov 	td->td_pticks = 0;
2696140ceb5dSKonstantin Belousov 	if (__predict_false(td->td_cowgen != atomic_load_int(
2697140ceb5dSKonstantin Belousov 	    &td->td_proc->p_cowgen)))
2698140ceb5dSKonstantin Belousov 		thread_cow_update(td);
2699140ceb5dSKonstantin Belousov 
270005296a0fSJake Freeland 	td->td_sa = tsr->ts_sa;
270105296a0fSJake Freeland 
2702140ceb5dSKonstantin Belousov #ifdef CAPABILITY_MODE
270305296a0fSJake Freeland 	if ((se->sy_flags & SYF_CAPENABLED) == 0) {
270405296a0fSJake Freeland 		if (CAP_TRACING(td))
270505296a0fSJake Freeland 			ktrcapfail(CAPFAIL_SYSCALL, NULL);
270605296a0fSJake Freeland 		if (IN_CAPABILITY_MODE(td)) {
2707140ceb5dSKonstantin Belousov 			tsr->ts_ret.sr_error = ECAPMODE;
2708140ceb5dSKonstantin Belousov 			return;
2709140ceb5dSKonstantin Belousov 		}
271005296a0fSJake Freeland 	}
2711140ceb5dSKonstantin Belousov #endif
2712140ceb5dSKonstantin Belousov 
2713140ceb5dSKonstantin Belousov 	sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2714974be51bSKonstantin Belousov 	audited = AUDIT_SYSCALL_ENTER(sc, td) != 0;
2715140ceb5dSKonstantin Belousov 
2716140ceb5dSKonstantin Belousov 	if (!sy_thr_static) {
271739024a89SKonstantin Belousov 		error = syscall_thread_enter(td, &se);
271839024a89SKonstantin Belousov 		sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2719140ceb5dSKonstantin Belousov 		if (error != 0) {
2720140ceb5dSKonstantin Belousov 			tsr->ts_ret.sr_error = error;
2721140ceb5dSKonstantin Belousov 			return;
2722140ceb5dSKonstantin Belousov 		}
2723140ceb5dSKonstantin Belousov 	}
2724140ceb5dSKonstantin Belousov 
2725140ceb5dSKonstantin Belousov 	rv_saved[0] = td->td_retval[0];
2726140ceb5dSKonstantin Belousov 	rv_saved[1] = td->td_retval[1];
2727140ceb5dSKonstantin Belousov 	nerror = td->td_errno;
2728140ceb5dSKonstantin Belousov 	td->td_retval[0] = 0;
2729140ceb5dSKonstantin Belousov 	td->td_retval[1] = 0;
2730140ceb5dSKonstantin Belousov 
2731140ceb5dSKonstantin Belousov #ifdef KDTRACE_HOOKS
2732140ceb5dSKonstantin Belousov 	if (se->sy_entry != 0)
2733140ceb5dSKonstantin Belousov 		(*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0);
2734140ceb5dSKonstantin Belousov #endif
2735140ceb5dSKonstantin Belousov 	tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args);
2736140ceb5dSKonstantin Belousov #ifdef KDTRACE_HOOKS
2737140ceb5dSKonstantin Belousov 	if (se->sy_return != 0)
2738140ceb5dSKonstantin Belousov 		(*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN,
2739974be51bSKonstantin Belousov 		    tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]);
2740140ceb5dSKonstantin Belousov #endif
2741140ceb5dSKonstantin Belousov 
2742140ceb5dSKonstantin Belousov 	tsr->ts_ret.sr_retval[0] = td->td_retval[0];
2743140ceb5dSKonstantin Belousov 	tsr->ts_ret.sr_retval[1] = td->td_retval[1];
2744140ceb5dSKonstantin Belousov 	td->td_retval[0] = rv_saved[0];
2745140ceb5dSKonstantin Belousov 	td->td_retval[1] = rv_saved[1];
2746140ceb5dSKonstantin Belousov 	td->td_errno = nerror;
2747140ceb5dSKonstantin Belousov 
2748140ceb5dSKonstantin Belousov 	if (audited)
2749140ceb5dSKonstantin Belousov 		AUDIT_SYSCALL_EXIT(error, td);
2750140ceb5dSKonstantin Belousov 	if (!sy_thr_static)
2751140ceb5dSKonstantin Belousov 		syscall_thread_exit(td, se);
2752140ceb5dSKonstantin Belousov }
2753140ceb5dSKonstantin Belousov 
2754140ceb5dSKonstantin Belousov static void
ptrace_remotereq(struct thread * td,int flag)2755140ceb5dSKonstantin Belousov ptrace_remotereq(struct thread *td, int flag)
275687a64872SKonstantin Belousov {
275787a64872SKonstantin Belousov 	struct proc *p;
275887a64872SKonstantin Belousov 
275987a64872SKonstantin Belousov 	MPASS(td == curthread);
276087a64872SKonstantin Belousov 	p = td->td_proc;
276187a64872SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
2762140ceb5dSKonstantin Belousov 	if ((td->td_dbgflags & flag) == 0)
276387a64872SKonstantin Belousov 		return;
276487a64872SKonstantin Belousov 	KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2765140ceb5dSKonstantin Belousov 	KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL"));
276687a64872SKonstantin Belousov 
276787a64872SKonstantin Belousov 	PROC_UNLOCK(p);
2768140ceb5dSKonstantin Belousov 	switch (flag) {
2769140ceb5dSKonstantin Belousov 	case TDB_COREDUMPREQ:
2770140ceb5dSKonstantin Belousov 		ptrace_coredumpreq(td, p, td->td_remotereq);
2771140ceb5dSKonstantin Belousov 		break;
2772140ceb5dSKonstantin Belousov 	case TDB_SCREMOTEREQ:
2773140ceb5dSKonstantin Belousov 		ptrace_syscallreq(td, p, td->td_remotereq);
2774140ceb5dSKonstantin Belousov 		break;
2775140ceb5dSKonstantin Belousov 	default:
2776140ceb5dSKonstantin Belousov 		__unreachable();
2777140ceb5dSKonstantin Belousov 	}
277887a64872SKonstantin Belousov 	PROC_LOCK(p);
2779140ceb5dSKonstantin Belousov 
2780140ceb5dSKonstantin Belousov 	MPASS((td->td_dbgflags & flag) != 0);
2781140ceb5dSKonstantin Belousov 	td->td_dbgflags &= ~flag;
2782e6feeae2SKonstantin Belousov 	td->td_remotereq = NULL;
278387a64872SKonstantin Belousov 	wakeup(p);
278487a64872SKonstantin Belousov }
278587a64872SKonstantin Belousov 
2786d4c4ca85SMark Johnston static void
sig_suspend_threads(struct thread * td,struct proc * p)2787c53fec76SKonstantin Belousov sig_suspend_threads(struct thread *td, struct proc *p)
2788d8267df7SDavid Xu {
2789d8267df7SDavid Xu 	struct thread *td2;
2790d8267df7SDavid Xu 
2791d8267df7SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
27927b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
2793d8267df7SDavid Xu 
2794d8267df7SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td2) {
2795a54e85fdSJeff Roberson 		thread_lock(td2);
2796c6d31b83SKonstantin Belousov 		ast_sched_locked(td2, TDA_SUSPEND);
2797e24a6552SMark Johnston 		if (TD_IS_SLEEPING(td2) && (td2->td_flags & TDF_SINTR) != 0) {
2798f33a947bSKonstantin Belousov 			if (td2->td_flags & TDF_SBDRY) {
2799a120a7a3SJohn Baldwin 				/*
2800a120a7a3SJohn Baldwin 				 * Once a thread is asleep with
28016f56cb8dSKonstantin Belousov 				 * TDF_SBDRY and without TDF_SERESTART
28026f56cb8dSKonstantin Belousov 				 * or TDF_SEINTR set, it should never
2803a120a7a3SJohn Baldwin 				 * become suspended due to this check.
2804a120a7a3SJohn Baldwin 				 */
2805a120a7a3SJohn Baldwin 				KASSERT(!TD_IS_SUSPENDED(td2),
2806a120a7a3SJohn Baldwin 				    ("thread with deferred stops suspended"));
280761a74c5cSJeff Roberson 				if (TD_SBDRY_INTR(td2)) {
2808d4c4ca85SMark Johnston 					sleepq_abort(td2, TD_SBDRY_ERRNO(td2));
280961a74c5cSJeff Roberson 					continue;
2810f33a947bSKonstantin Belousov 				}
281161a74c5cSJeff Roberson 			} else if (!TD_IS_SUSPENDED(td2))
281261a74c5cSJeff Roberson 				thread_suspend_one(td2);
2813f33a947bSKonstantin Belousov 		} else if (!TD_IS_SUSPENDED(td2)) {
2814d8267df7SDavid Xu #ifdef SMP
2815d8267df7SDavid Xu 			if (TD_IS_RUNNING(td2) && td2 != td)
2816d8267df7SDavid Xu 				forward_signal(td2);
2817d8267df7SDavid Xu #endif
2818d8267df7SDavid Xu 		}
2819a54e85fdSJeff Roberson 		thread_unlock(td2);
2820d8267df7SDavid Xu 	}
2821d8267df7SDavid Xu }
2822d8267df7SDavid Xu 
282382a4538fSEric Badger /*
282482a4538fSEric Badger  * Stop the process for an event deemed interesting to the debugger. If si is
282582a4538fSEric Badger  * non-NULL, this is a signal exchange; the new signal requested by the
282682a4538fSEric Badger  * debugger will be returned for handling. If si is NULL, this is some other
282782a4538fSEric Badger  * type of interesting event. The debugger may request a signal be delivered in
282882a4538fSEric Badger  * that case as well, however it will be deferred until it can be handled.
282982a4538fSEric Badger  */
2830cbf4e354SDavid Xu int
ptracestop(struct thread * td,int sig,ksiginfo_t * si)283182a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si)
28324cc9f52fSRobert Drehmel {
28334cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
283482a4538fSEric Badger 	struct thread *td2;
283582a4538fSEric Badger 	ksiginfo_t ksi;
28364cc9f52fSRobert Drehmel 
283730a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
28380f14f15bSJohn Baldwin 	KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
28394cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2840aa89d8cdSJohn Baldwin 	    &p->p_mtx.lock_object, "Stopping for traced signal");
28414cc9f52fSRobert Drehmel 
2842cbf4e354SDavid Xu 	td->td_xsig = sig;
284382a4538fSEric Badger 
284482a4538fSEric Badger 	if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
284582a4538fSEric Badger 		td->td_dbgflags |= TDB_XSIG;
2846515b7a0bSJohn Baldwin 		CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2847515b7a0bSJohn Baldwin 		    td->td_tid, p->p_pid, td->td_dbgflags, sig);
28487b4a950aSDavid Xu 		PROC_SLOCK(p);
2849904c5ec4SDavid Xu 		while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
285082a4538fSEric Badger 			if (P_KILLED(p)) {
285182a4538fSEric Badger 				/*
285282a4538fSEric Badger 				 * Ensure that, if we've been PT_KILLed, the
285382a4538fSEric Badger 				 * exit status reflects that. Another thread
285482a4538fSEric Badger 				 * may also be in ptracestop(), having just
285582a4538fSEric Badger 				 * received the SIGKILL, but this thread was
285682a4538fSEric Badger 				 * unsuspended first.
285782a4538fSEric Badger 				 */
285882a4538fSEric Badger 				td->td_dbgflags &= ~TDB_XSIG;
285982a4538fSEric Badger 				td->td_xsig = SIGKILL;
286082a4538fSEric Badger 				p->p_ptevents = 0;
286182a4538fSEric Badger 				break;
286282a4538fSEric Badger 			}
28635fcfab6eSJohn Baldwin 			if (p->p_flag & P_SINGLE_EXIT &&
28645fcfab6eSJohn Baldwin 			    !(td->td_dbgflags & TDB_EXIT)) {
28655fcfab6eSJohn Baldwin 				/*
28665fcfab6eSJohn Baldwin 				 * Ignore ptrace stops except for thread exit
28675fcfab6eSJohn Baldwin 				 * events when the process exits.
28685fcfab6eSJohn Baldwin 				 */
2869904c5ec4SDavid Xu 				td->td_dbgflags &= ~TDB_XSIG;
28707b4a950aSDavid Xu 				PROC_SUNLOCK(p);
287182a4538fSEric Badger 				return (0);
2872cbf4e354SDavid Xu 			}
2873b7a25e63SKonstantin Belousov 
2874cbf4e354SDavid Xu 			/*
2875b7a25e63SKonstantin Belousov 			 * Make wait(2) work.  Ensure that right after the
2876b7a25e63SKonstantin Belousov 			 * attach, the thread which was decided to become the
2877b7a25e63SKonstantin Belousov 			 * leader of attach gets reported to the waiter.
2878b7a25e63SKonstantin Belousov 			 * Otherwise, just avoid overwriting another thread's
2879b7a25e63SKonstantin Belousov 			 * assignment to p_xthread.  If another thread has
2880b7a25e63SKonstantin Belousov 			 * already set p_xthread, the current thread will get
2881b7a25e63SKonstantin Belousov 			 * a chance to report itself upon the next iteration.
2882cbf4e354SDavid Xu 			 */
2883b7a25e63SKonstantin Belousov 			if ((td->td_dbgflags & TDB_FSTP) != 0 ||
28847f649ddaSMark Johnston 			    ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2885b7a25e63SKonstantin Belousov 			    p->p_xthread == NULL)) {
2886b4490c6eSKonstantin Belousov 				p->p_xsig = sig;
2887cbf4e354SDavid Xu 				p->p_xthread = td;
2888ab74c843SKonstantin Belousov 
2889ab74c843SKonstantin Belousov 				/*
2890ab74c843SKonstantin Belousov 				 * If we are on sleepqueue already,
2891ab74c843SKonstantin Belousov 				 * let sleepqueue code decide if it
2892ab74c843SKonstantin Belousov 				 * needs to go sleep after attach.
2893ab74c843SKonstantin Belousov 				 */
2894ab74c843SKonstantin Belousov 				if (td->td_wchan == NULL)
2895b7a25e63SKonstantin Belousov 					td->td_dbgflags &= ~TDB_FSTP;
2896ab74c843SKonstantin Belousov 
2897b7a25e63SKonstantin Belousov 				p->p_flag2 &= ~P2_PTRACE_FSTP;
2898b7a25e63SKonstantin Belousov 				p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2899c53fec76SKonstantin Belousov 				sig_suspend_threads(td, p);
2900b7a25e63SKonstantin Belousov 			}
29016fa39a73SKonstantin Belousov 			if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
29026fa39a73SKonstantin Belousov 				td->td_dbgflags &= ~TDB_STOPATFORK;
29036fa39a73SKonstantin Belousov 			}
2904cbf4e354SDavid Xu stopme:
290568d311b6SKonstantin Belousov 			td->td_dbgflags |= TDB_SSWITCH;
29066ddcc233SKonstantin Belousov 			thread_suspend_switch(td, p);
290768d311b6SKonstantin Belousov 			td->td_dbgflags &= ~TDB_SSWITCH;
2908140ceb5dSKonstantin Belousov 			if ((td->td_dbgflags & (TDB_COREDUMPREQ |
2909140ceb5dSKonstantin Belousov 			    TDB_SCREMOTEREQ)) != 0) {
2910140ceb5dSKonstantin Belousov 				MPASS((td->td_dbgflags & (TDB_COREDUMPREQ |
2911140ceb5dSKonstantin Belousov 				    TDB_SCREMOTEREQ)) !=
2912140ceb5dSKonstantin Belousov 				    (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
291387a64872SKonstantin Belousov 				PROC_SUNLOCK(p);
2914140ceb5dSKonstantin Belousov 				ptrace_remotereq(td, td->td_dbgflags &
2915140ceb5dSKonstantin Belousov 				    (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
291687a64872SKonstantin Belousov 				PROC_SLOCK(p);
291787a64872SKonstantin Belousov 				goto stopme;
291887a64872SKonstantin Belousov 			}
29197ce60f60SDavid Xu 			if (p->p_xthread == td)
29207ce60f60SDavid Xu 				p->p_xthread = NULL;
29217ce60f60SDavid Xu 			if (!(p->p_flag & P_TRACED))
2922cbf4e354SDavid Xu 				break;
2923904c5ec4SDavid Xu 			if (td->td_dbgflags & TDB_SUSPEND) {
2924cbf4e354SDavid Xu 				if (p->p_flag & P_SINGLE_EXIT)
2925cbf4e354SDavid Xu 					break;
2926cbf4e354SDavid Xu 				goto stopme;
2927cbf4e354SDavid Xu 			}
2928cbf4e354SDavid Xu 		}
29297b4a950aSDavid Xu 		PROC_SUNLOCK(p);
293082a4538fSEric Badger 	}
293182a4538fSEric Badger 
293282a4538fSEric Badger 	if (si != NULL && sig == td->td_xsig) {
293382a4538fSEric Badger 		/* Parent wants us to take the original signal unchanged. */
293482a4538fSEric Badger 		si->ksi_flags |= KSI_HEAD;
293582a4538fSEric Badger 		if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
293682a4538fSEric Badger 			si->ksi_signo = 0;
293782a4538fSEric Badger 	} else if (td->td_xsig != 0) {
293882a4538fSEric Badger 		/*
293982a4538fSEric Badger 		 * If parent wants us to take a new signal, then it will leave
294082a4538fSEric Badger 		 * it in td->td_xsig; otherwise we just look for signals again.
294182a4538fSEric Badger 		 */
294282a4538fSEric Badger 		ksiginfo_init(&ksi);
294382a4538fSEric Badger 		ksi.ksi_signo = td->td_xsig;
294482a4538fSEric Badger 		ksi.ksi_flags |= KSI_PTRACE;
2945146fc63fSKonstantin Belousov 		td2 = sigtd(p, td->td_xsig, false);
294682a4538fSEric Badger 		tdsendsignal(p, td2, td->td_xsig, &ksi);
294782a4538fSEric Badger 		if (td != td2)
294882a4538fSEric Badger 			return (0);
294982a4538fSEric Badger 	}
295082a4538fSEric Badger 
2951cbf4e354SDavid Xu 	return (td->td_xsig);
29524cc9f52fSRobert Drehmel }
29534cc9f52fSRobert Drehmel 
29540bc52b0bSKonstantin Belousov static void
reschedule_signals(struct proc * p,sigset_t block,int flags)295580a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags)
29566b286ee8SKonstantin Belousov {
29576b286ee8SKonstantin Belousov 	struct sigacts *ps;
29586b286ee8SKonstantin Belousov 	struct thread *td;
2959407af02bSDavid Xu 	int sig;
2960146fc63fSKonstantin Belousov 	bool fastblk, pslocked;
29616b286ee8SKonstantin Belousov 
29626b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
296370778bbaSKonstantin Belousov 	ps = p->p_sigacts;
2964146fc63fSKonstantin Belousov 	pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2965146fc63fSKonstantin Belousov 	mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2966407af02bSDavid Xu 	if (SIGISEMPTY(p->p_siglist))
2967407af02bSDavid Xu 		return;
2968407af02bSDavid Xu 	SIGSETAND(block, p->p_siglist);
2969146fc63fSKonstantin Belousov 	fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
297081f2e906SMark Johnston 	SIG_FOREACH(sig, &block) {
2971146fc63fSKonstantin Belousov 		td = sigtd(p, sig, fastblk);
2972146fc63fSKonstantin Belousov 
2973146fc63fSKonstantin Belousov 		/*
2974146fc63fSKonstantin Belousov 		 * If sigtd() selected us despite sigfastblock is
2975146fc63fSKonstantin Belousov 		 * blocking, do not activate AST or wake us, to avoid
2976146fc63fSKonstantin Belousov 		 * loop in AST handler.
2977146fc63fSKonstantin Belousov 		 */
2978146fc63fSKonstantin Belousov 		if (fastblk && td == curthread)
2979146fc63fSKonstantin Belousov 			continue;
2980146fc63fSKonstantin Belousov 
29816b286ee8SKonstantin Belousov 		signotify(td);
2982146fc63fSKonstantin Belousov 		if (!pslocked)
29836b286ee8SKonstantin Belousov 			mtx_lock(&ps->ps_mtx);
2984396a0d44SKonstantin Belousov 		if (p->p_flag & P_TRACED ||
2985396a0d44SKonstantin Belousov 		    (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2986146fc63fSKonstantin Belousov 		    !SIGISMEMBER(td->td_sigmask, sig))) {
2987407af02bSDavid Xu 			tdsigwakeup(td, sig, SIG_CATCH,
2988407af02bSDavid Xu 			    (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
29896b286ee8SKonstantin Belousov 			    ERESTART));
2990146fc63fSKonstantin Belousov 		}
2991146fc63fSKonstantin Belousov 		if (!pslocked)
29926b286ee8SKonstantin Belousov 			mtx_unlock(&ps->ps_mtx);
29936b286ee8SKonstantin Belousov 	}
29946b286ee8SKonstantin Belousov }
29956b286ee8SKonstantin Belousov 
29966b286ee8SKonstantin Belousov void
tdsigcleanup(struct thread * td)29976b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td)
29986b286ee8SKonstantin Belousov {
29996b286ee8SKonstantin Belousov 	struct proc *p;
30006b286ee8SKonstantin Belousov 	sigset_t unblocked;
30016b286ee8SKonstantin Belousov 
30026b286ee8SKonstantin Belousov 	p = td->td_proc;
30036b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
30046b286ee8SKonstantin Belousov 
30056b286ee8SKonstantin Belousov 	sigqueue_flush(&td->td_sigqueue);
30066b286ee8SKonstantin Belousov 	if (p->p_numthreads == 1)
30076b286ee8SKonstantin Belousov 		return;
30086b286ee8SKonstantin Belousov 
30096b286ee8SKonstantin Belousov 	/*
30106b286ee8SKonstantin Belousov 	 * Since we cannot handle signals, notify signal post code
30116b286ee8SKonstantin Belousov 	 * about this by filling the sigmask.
30126b286ee8SKonstantin Belousov 	 *
30136b286ee8SKonstantin Belousov 	 * Also, if needed, wake up thread(s) that do not block the
30146b286ee8SKonstantin Belousov 	 * same signals as the exiting thread, since the thread might
30156b286ee8SKonstantin Belousov 	 * have been selected for delivery and woken up.
30166b286ee8SKonstantin Belousov 	 */
30176b286ee8SKonstantin Belousov 	SIGFILLSET(unblocked);
30186b286ee8SKonstantin Belousov 	SIGSETNAND(unblocked, td->td_sigmask);
30196b286ee8SKonstantin Belousov 	SIGFILLSET(td->td_sigmask);
302080a8b0f3SKonstantin Belousov 	reschedule_signals(p, unblocked, 0);
30216b286ee8SKonstantin Belousov 
30226b286ee8SKonstantin Belousov }
30236b286ee8SKonstantin Belousov 
30243a1e5dd8SKonstantin Belousov static int
sigdeferstop_curr_flags(int cflags)30253a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags)
3026a120a7a3SJohn Baldwin {
3027a120a7a3SJohn Baldwin 
30283a1e5dd8SKonstantin Belousov 	MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
30293a1e5dd8SKonstantin Belousov 	    (cflags & TDF_SBDRY) != 0);
30303a1e5dd8SKonstantin Belousov 	return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
3031a120a7a3SJohn Baldwin }
3032a120a7a3SJohn Baldwin 
3033a120a7a3SJohn Baldwin /*
30343a1e5dd8SKonstantin Belousov  * Defer the delivery of SIGSTOP for the current thread, according to
30353a1e5dd8SKonstantin Belousov  * the requested mode.  Returns previous flags, which must be restored
30363a1e5dd8SKonstantin Belousov  * by sigallowstop().
30373a1e5dd8SKonstantin Belousov  *
30383a1e5dd8SKonstantin Belousov  * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
30393a1e5dd8SKonstantin Belousov  * cleared by the current thread, which allow the lock-less read-only
30403a1e5dd8SKonstantin Belousov  * accesses below.
3041a120a7a3SJohn Baldwin  */
3042e3612a4cSKonstantin Belousov int
sigdeferstop_impl(int mode)304346e47c4fSKonstantin Belousov sigdeferstop_impl(int mode)
3044a120a7a3SJohn Baldwin {
3045593efaf9SJohn Baldwin 	struct thread *td;
30463a1e5dd8SKonstantin Belousov 	int cflags, nflags;
3047a120a7a3SJohn Baldwin 
3048593efaf9SJohn Baldwin 	td = curthread;
30493a1e5dd8SKonstantin Belousov 	cflags = sigdeferstop_curr_flags(td->td_flags);
30503a1e5dd8SKonstantin Belousov 	switch (mode) {
30513a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_NOP:
30523a1e5dd8SKonstantin Belousov 		nflags = cflags;
30533a1e5dd8SKonstantin Belousov 		break;
30543a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_OFF:
30553a1e5dd8SKonstantin Belousov 		nflags = 0;
30563a1e5dd8SKonstantin Belousov 		break;
30573a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_SILENT:
30583a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
30593a1e5dd8SKonstantin Belousov 		break;
30603a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_EINTR:
30613a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
30623a1e5dd8SKonstantin Belousov 		break;
30633a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_ERESTART:
30643a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
30653a1e5dd8SKonstantin Belousov 		break;
30663a1e5dd8SKonstantin Belousov 	default:
30673a1e5dd8SKonstantin Belousov 		panic("sigdeferstop: invalid mode %x", mode);
30683a1e5dd8SKonstantin Belousov 		break;
30693a1e5dd8SKonstantin Belousov 	}
307046e47c4fSKonstantin Belousov 	if (cflags == nflags)
307146e47c4fSKonstantin Belousov 		return (SIGDEFERSTOP_VAL_NCHG);
3072a120a7a3SJohn Baldwin 	thread_lock(td);
30733a1e5dd8SKonstantin Belousov 	td->td_flags = (td->td_flags & ~cflags) | nflags;
3074a120a7a3SJohn Baldwin 	thread_unlock(td);
30753a1e5dd8SKonstantin Belousov 	return (cflags);
30763a1e5dd8SKonstantin Belousov }
30773a1e5dd8SKonstantin Belousov 
30783a1e5dd8SKonstantin Belousov /*
30793a1e5dd8SKonstantin Belousov  * Restores the STOP handling mode, typically permitting the delivery
30803a1e5dd8SKonstantin Belousov  * of SIGSTOP for the current thread.  This does not immediately
30813a1e5dd8SKonstantin Belousov  * suspend if a stop was posted.  Instead, the thread will suspend
30823a1e5dd8SKonstantin Belousov  * either via ast() or a subsequent interruptible sleep.
30833a1e5dd8SKonstantin Belousov  */
30843a1e5dd8SKonstantin Belousov void
sigallowstop_impl(int prev)308546e47c4fSKonstantin Belousov sigallowstop_impl(int prev)
30863a1e5dd8SKonstantin Belousov {
30873a1e5dd8SKonstantin Belousov 	struct thread *td;
30883a1e5dd8SKonstantin Belousov 	int cflags;
30893a1e5dd8SKonstantin Belousov 
309046e47c4fSKonstantin Belousov 	KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
30913a1e5dd8SKonstantin Belousov 	KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
30923a1e5dd8SKonstantin Belousov 	    ("sigallowstop: incorrect previous mode %x", prev));
30933a1e5dd8SKonstantin Belousov 	td = curthread;
30943a1e5dd8SKonstantin Belousov 	cflags = sigdeferstop_curr_flags(td->td_flags);
30953a1e5dd8SKonstantin Belousov 	if (cflags != prev) {
30963a1e5dd8SKonstantin Belousov 		thread_lock(td);
30973a1e5dd8SKonstantin Belousov 		td->td_flags = (td->td_flags & ~cflags) | prev;
30983a1e5dd8SKonstantin Belousov 		thread_unlock(td);
30993a1e5dd8SKonstantin Belousov 	}
3100a120a7a3SJohn Baldwin }
3101a120a7a3SJohn Baldwin 
310281f2e906SMark Johnston enum sigstatus {
310381f2e906SMark Johnston 	SIGSTATUS_HANDLE,
310481f2e906SMark Johnston 	SIGSTATUS_HANDLED,
310581f2e906SMark Johnston 	SIGSTATUS_IGNORE,
310681f2e906SMark Johnston 	SIGSTATUS_SBDRY_STOP,
310781f2e906SMark Johnston };
310881f2e906SMark Johnston 
310981f2e906SMark Johnston /*
311081f2e906SMark Johnston  * The thread has signal "sig" pending.  Figure out what to do with it:
311181f2e906SMark Johnston  *
311281f2e906SMark Johnston  * _HANDLE     -> the caller should handle the signal
311381f2e906SMark Johnston  * _HANDLED    -> handled internally, reload pending signal set
311481f2e906SMark Johnston  * _IGNORE     -> ignored, remove from the set of pending signals and try the
311581f2e906SMark Johnston  *                next pending signal
311681f2e906SMark Johnston  * _SBDRY_STOP -> the signal should stop the thread but this is not
311781f2e906SMark Johnston  *                permitted in the current context
311881f2e906SMark Johnston  */
311981f2e906SMark Johnston static enum sigstatus
sigprocess(struct thread * td,int sig)312081f2e906SMark Johnston sigprocess(struct thread *td, int sig)
312181f2e906SMark Johnston {
312281f2e906SMark Johnston 	struct proc *p;
312381f2e906SMark Johnston 	struct sigacts *ps;
312481f2e906SMark Johnston 	struct sigqueue *queue;
312581f2e906SMark Johnston 	ksiginfo_t ksi;
312681f2e906SMark Johnston 	int prop;
312781f2e906SMark Johnston 
312881f2e906SMark Johnston 	KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
312981f2e906SMark Johnston 
313081f2e906SMark Johnston 	p = td->td_proc;
313181f2e906SMark Johnston 	ps = p->p_sigacts;
313281f2e906SMark Johnston 	mtx_assert(&ps->ps_mtx, MA_OWNED);
313381f2e906SMark Johnston 	PROC_LOCK_ASSERT(p, MA_OWNED);
313481f2e906SMark Johnston 
313581f2e906SMark Johnston 	/*
313681f2e906SMark Johnston 	 * We should allow pending but ignored signals below
313702a2aacbSKonstantin Belousov 	 * if there is sigwait() active, or P_TRACED was
313881f2e906SMark Johnston 	 * on when they were posted.
313981f2e906SMark Johnston 	 */
314081f2e906SMark Johnston 	if (SIGISMEMBER(ps->ps_sigignore, sig) &&
314181f2e906SMark Johnston 	    (p->p_flag & P_TRACED) == 0 &&
314281f2e906SMark Johnston 	    (td->td_flags & TDF_SIGWAIT) == 0) {
314381f2e906SMark Johnston 		return (SIGSTATUS_IGNORE);
314481f2e906SMark Johnston 	}
314581f2e906SMark Johnston 
314602a2aacbSKonstantin Belousov 	/*
314702a2aacbSKonstantin Belousov 	 * If the process is going to single-thread mode to prepare
314802a2aacbSKonstantin Belousov 	 * for exit, there is no sense in delivering any signal
314902a2aacbSKonstantin Belousov 	 * to usermode.  Another important consequence is that
315002a2aacbSKonstantin Belousov 	 * msleep(..., PCATCH, ...) now is only interruptible by a
315102a2aacbSKonstantin Belousov 	 * suspend request.
315202a2aacbSKonstantin Belousov 	 */
315302a2aacbSKonstantin Belousov 	if ((p->p_flag2 & P2_WEXIT) != 0)
315402a2aacbSKonstantin Belousov 		return (SIGSTATUS_IGNORE);
315502a2aacbSKonstantin Belousov 
315681f2e906SMark Johnston 	if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
315781f2e906SMark Johnston 		/*
315881f2e906SMark Johnston 		 * If traced, always stop.
315981f2e906SMark Johnston 		 * Remove old signal from queue before the stop.
316081f2e906SMark Johnston 		 * XXX shrug off debugger, it causes siginfo to
316181f2e906SMark Johnston 		 * be thrown away.
316281f2e906SMark Johnston 		 */
316381f2e906SMark Johnston 		queue = &td->td_sigqueue;
316481f2e906SMark Johnston 		ksiginfo_init(&ksi);
316581f2e906SMark Johnston 		if (sigqueue_get(queue, sig, &ksi) == 0) {
316681f2e906SMark Johnston 			queue = &p->p_sigqueue;
316781f2e906SMark Johnston 			sigqueue_get(queue, sig, &ksi);
316881f2e906SMark Johnston 		}
316981f2e906SMark Johnston 		td->td_si = ksi.ksi_info;
317081f2e906SMark Johnston 
317181f2e906SMark Johnston 		mtx_unlock(&ps->ps_mtx);
317281f2e906SMark Johnston 		sig = ptracestop(td, sig, &ksi);
317381f2e906SMark Johnston 		mtx_lock(&ps->ps_mtx);
317481f2e906SMark Johnston 
317581f2e906SMark Johnston 		td->td_si.si_signo = 0;
317681f2e906SMark Johnston 
317781f2e906SMark Johnston 		/*
317881f2e906SMark Johnston 		 * Keep looking if the debugger discarded or
317981f2e906SMark Johnston 		 * replaced the signal.
318081f2e906SMark Johnston 		 */
318181f2e906SMark Johnston 		if (sig == 0)
318281f2e906SMark Johnston 			return (SIGSTATUS_HANDLED);
318381f2e906SMark Johnston 
318481f2e906SMark Johnston 		/*
318581f2e906SMark Johnston 		 * If the signal became masked, re-queue it.
318681f2e906SMark Johnston 		 */
318781f2e906SMark Johnston 		if (SIGISMEMBER(td->td_sigmask, sig)) {
318881f2e906SMark Johnston 			ksi.ksi_flags |= KSI_HEAD;
318981f2e906SMark Johnston 			sigqueue_add(&p->p_sigqueue, sig, &ksi);
319081f2e906SMark Johnston 			return (SIGSTATUS_HANDLED);
319181f2e906SMark Johnston 		}
319281f2e906SMark Johnston 
319381f2e906SMark Johnston 		/*
319481f2e906SMark Johnston 		 * If the traced bit got turned off, requeue the signal and
319581f2e906SMark Johnston 		 * reload the set of pending signals.  This ensures that p_sig*
319681f2e906SMark Johnston 		 * and p_sigact are consistent.
319781f2e906SMark Johnston 		 */
319881f2e906SMark Johnston 		if ((p->p_flag & P_TRACED) == 0) {
3199a24afbb4SKonstantin Belousov 			if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
320081f2e906SMark Johnston 				ksi.ksi_flags |= KSI_HEAD;
320181f2e906SMark Johnston 				sigqueue_add(queue, sig, &ksi);
3202a24afbb4SKonstantin Belousov 			}
320381f2e906SMark Johnston 			return (SIGSTATUS_HANDLED);
320481f2e906SMark Johnston 		}
320581f2e906SMark Johnston 	}
320681f2e906SMark Johnston 
320781f2e906SMark Johnston 	/*
320881f2e906SMark Johnston 	 * Decide whether the signal should be returned.
320981f2e906SMark Johnston 	 * Return the signal's number, or fall through
321081f2e906SMark Johnston 	 * to clear it from the pending mask.
321181f2e906SMark Johnston 	 */
321281f2e906SMark Johnston 	switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
321381f2e906SMark Johnston 	case (intptr_t)SIG_DFL:
321481f2e906SMark Johnston 		/*
321581f2e906SMark Johnston 		 * Don't take default actions on system processes.
321681f2e906SMark Johnston 		 */
321781f2e906SMark Johnston 		if (p->p_pid <= 1) {
321881f2e906SMark Johnston #ifdef DIAGNOSTIC
321981f2e906SMark Johnston 			/*
322081f2e906SMark Johnston 			 * Are you sure you want to ignore SIGSEGV
322181f2e906SMark Johnston 			 * in init? XXX
322281f2e906SMark Johnston 			 */
322381f2e906SMark Johnston 			printf("Process (pid %lu) got signal %d\n",
322481f2e906SMark Johnston 				(u_long)p->p_pid, sig);
322581f2e906SMark Johnston #endif
322681f2e906SMark Johnston 			return (SIGSTATUS_IGNORE);
322781f2e906SMark Johnston 		}
322881f2e906SMark Johnston 
322981f2e906SMark Johnston 		/*
323081f2e906SMark Johnston 		 * If there is a pending stop signal to process with
323181f2e906SMark Johnston 		 * default action, stop here, then clear the signal.
323281f2e906SMark Johnston 		 * Traced or exiting processes should ignore stops.
323381f2e906SMark Johnston 		 * Additionally, a member of an orphaned process group
323481f2e906SMark Johnston 		 * should ignore tty stops.
323581f2e906SMark Johnston 		 */
323681f2e906SMark Johnston 		prop = sigprop(sig);
323781f2e906SMark Johnston 		if (prop & SIGPROP_STOP) {
323881f2e906SMark Johnston 			mtx_unlock(&ps->ps_mtx);
323981f2e906SMark Johnston 			if ((p->p_flag & (P_TRACED | P_WEXIT |
324081f2e906SMark Johnston 			    P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
324181f2e906SMark Johnston 			    pg_flags & PGRP_ORPHANED) != 0 &&
324281f2e906SMark Johnston 			    (prop & SIGPROP_TTYSTOP) != 0)) {
324381f2e906SMark Johnston 				mtx_lock(&ps->ps_mtx);
324481f2e906SMark Johnston 				return (SIGSTATUS_IGNORE);
324581f2e906SMark Johnston 			}
324681f2e906SMark Johnston 			if (TD_SBDRY_INTR(td)) {
324781f2e906SMark Johnston 				KASSERT((td->td_flags & TDF_SBDRY) != 0,
324881f2e906SMark Johnston 				    ("lost TDF_SBDRY"));
324981f2e906SMark Johnston 				mtx_lock(&ps->ps_mtx);
325081f2e906SMark Johnston 				return (SIGSTATUS_SBDRY_STOP);
325181f2e906SMark Johnston 			}
325281f2e906SMark Johnston 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
325381f2e906SMark Johnston 			    &p->p_mtx.lock_object, "Catching SIGSTOP");
325481f2e906SMark Johnston 			sigqueue_delete(&td->td_sigqueue, sig);
325581f2e906SMark Johnston 			sigqueue_delete(&p->p_sigqueue, sig);
325681f2e906SMark Johnston 			p->p_flag |= P_STOPPED_SIG;
325781f2e906SMark Johnston 			p->p_xsig = sig;
325881f2e906SMark Johnston 			PROC_SLOCK(p);
3259c53fec76SKonstantin Belousov 			sig_suspend_threads(td, p);
326081f2e906SMark Johnston 			thread_suspend_switch(td, p);
326181f2e906SMark Johnston 			PROC_SUNLOCK(p);
326281f2e906SMark Johnston 			mtx_lock(&ps->ps_mtx);
326381f2e906SMark Johnston 			return (SIGSTATUS_HANDLED);
326481f2e906SMark Johnston 		} else if ((prop & SIGPROP_IGNORE) != 0 &&
326581f2e906SMark Johnston 		    (td->td_flags & TDF_SIGWAIT) == 0) {
326681f2e906SMark Johnston 			/*
326781f2e906SMark Johnston 			 * Default action is to ignore; drop it if
326881f2e906SMark Johnston 			 * not in kern_sigtimedwait().
326981f2e906SMark Johnston 			 */
327081f2e906SMark Johnston 			return (SIGSTATUS_IGNORE);
327181f2e906SMark Johnston 		} else {
327281f2e906SMark Johnston 			return (SIGSTATUS_HANDLE);
327381f2e906SMark Johnston 		}
327481f2e906SMark Johnston 
327581f2e906SMark Johnston 	case (intptr_t)SIG_IGN:
327681f2e906SMark Johnston 		if ((td->td_flags & TDF_SIGWAIT) == 0)
327781f2e906SMark Johnston 			return (SIGSTATUS_IGNORE);
327881f2e906SMark Johnston 		else
327981f2e906SMark Johnston 			return (SIGSTATUS_HANDLE);
328081f2e906SMark Johnston 
328181f2e906SMark Johnston 	default:
328281f2e906SMark Johnston 		/*
328381f2e906SMark Johnston 		 * This signal has an action, let postsig() process it.
328481f2e906SMark Johnston 		 */
328581f2e906SMark Johnston 		return (SIGSTATUS_HANDLE);
328681f2e906SMark Johnston 	}
328781f2e906SMark Johnston }
328881f2e906SMark Johnston 
3289df8bae1dSRodney W. Grimes /*
3290df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
3291df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
3292df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
3293df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
329481f2e906SMark Johnston  * a syscall or trap (though this can usually be done without calling
329581f2e906SMark Johnston  * issignal by checking the pending signal masks in cursig.) The normal call
3296df8bae1dSRodney W. Grimes  * sequence is
3297df8bae1dSRodney W. Grimes  *
3298e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
32992c42a146SMarcel Moolenaar  *		postsig(sig);
3300df8bae1dSRodney W. Grimes  */
33016711f10fSJohn Baldwin static int
issignal(struct thread * td)33023cf3b9f0SJohn Baldwin issignal(struct thread *td)
3303df8bae1dSRodney W. Grimes {
3304e602ba25SJulian Elischer 	struct proc *p;
33054093529dSJeff Roberson 	sigset_t sigpending;
330681f2e906SMark Johnston 	int sig;
3307df8bae1dSRodney W. Grimes 
3308e602ba25SJulian Elischer 	p = td->td_proc;
3309628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
331081f2e906SMark Johnston 
3311df8bae1dSRodney W. Grimes 	for (;;) {
33129104847fSDavid Xu 		sigpending = td->td_sigqueue.sq_signals;
33136b286ee8SKonstantin Belousov 		SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
33144093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
33154093529dSJeff Roberson 
33166f56cb8dSKonstantin Belousov 		if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
33176f56cb8dSKonstantin Belousov 		    (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
33184093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
33194093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
3320df8bae1dSRodney W. Grimes 			return (0);
3321146fc63fSKonstantin Belousov 
3322146fc63fSKonstantin Belousov 		/*
3323146fc63fSKonstantin Belousov 		 * Do fast sigblock if requested by usermode.  Since
3324146fc63fSKonstantin Belousov 		 * we do know that there was a signal pending at this
3325146fc63fSKonstantin Belousov 		 * point, set the FAST_SIGBLOCK_PEND as indicator for
3326146fc63fSKonstantin Belousov 		 * usermode to perform a dummy call to
3327146fc63fSKonstantin Belousov 		 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3328146fc63fSKonstantin Belousov 		 * delivery of postponed pending signal.
3329146fc63fSKonstantin Belousov 		 */
3330146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3331146fc63fSKonstantin Belousov 			if (td->td_sigblock_val != 0)
3332146fc63fSKonstantin Belousov 				SIGSETNAND(sigpending, fastblock_mask);
3333146fc63fSKonstantin Belousov 			if (SIGISEMPTY(sigpending)) {
3334146fc63fSKonstantin Belousov 				td->td_pflags |= TDP_SIGFASTPENDING;
3335146fc63fSKonstantin Belousov 				return (0);
3336146fc63fSKonstantin Belousov 			}
3337146fc63fSKonstantin Belousov 		}
3338146fc63fSKonstantin Belousov 
3339b7a25e63SKonstantin Belousov 		if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3340b7a25e63SKonstantin Belousov 		    (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3341b7a25e63SKonstantin Belousov 		    SIGISMEMBER(sigpending, SIGSTOP)) {
3342b7a25e63SKonstantin Belousov 			/*
3343b7a25e63SKonstantin Belousov 			 * If debugger just attached, always consume
3344b7a25e63SKonstantin Belousov 			 * SIGSTOP from ptrace(PT_ATTACH) first, to
3345b7a25e63SKonstantin Belousov 			 * execute the debugger attach ritual in
3346b7a25e63SKonstantin Belousov 			 * order.
3347b7a25e63SKonstantin Belousov 			 */
3348b7a25e63SKonstantin Belousov 			td->td_dbgflags |= TDB_FSTP;
334981f2e906SMark Johnston 			SIGEMPTYSET(sigpending);
335081f2e906SMark Johnston 			SIGADDSET(sigpending, SIGSTOP);
3351b7a25e63SKonstantin Belousov 		}
33522a024a2bSSean Eric Fagan 
335381f2e906SMark Johnston 		SIG_FOREACH(sig, &sigpending) {
335481f2e906SMark Johnston 			switch (sigprocess(td, sig)) {
335581f2e906SMark Johnston 			case SIGSTATUS_HANDLE:
335681f2e906SMark Johnston 				return (sig);
335781f2e906SMark Johnston 			case SIGSTATUS_HANDLED:
335881f2e906SMark Johnston 				goto next;
335981f2e906SMark Johnston 			case SIGSTATUS_IGNORE:
33609104847fSDavid Xu 				sigqueue_delete(&td->td_sigqueue, sig);
33616b286ee8SKonstantin Belousov 				sigqueue_delete(&p->p_sigqueue, sig);
336281f2e906SMark Johnston 				break;
336381f2e906SMark Johnston 			case SIGSTATUS_SBDRY_STOP:
336446e47c4fSKonstantin Belousov 				return (-1);
336546e47c4fSKonstantin Belousov 			}
3366df8bae1dSRodney W. Grimes 		}
3367b7a25e63SKonstantin Belousov next:;
3368df8bae1dSRodney W. Grimes 	}
3369df8bae1dSRodney W. Grimes }
3370df8bae1dSRodney W. Grimes 
3371e574e444SDavid Xu void
thread_stopped(struct proc * p)3372e574e444SDavid Xu thread_stopped(struct proc *p)
3373e574e444SDavid Xu {
3374e574e444SDavid Xu 	int n;
3375e574e444SDavid Xu 
3376e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
33777b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
3378e574e444SDavid Xu 	n = p->p_suspcount;
33797c9a98f1SDavid Xu 	if (p == curproc)
3380e574e444SDavid Xu 		n++;
3381e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
33827b4a950aSDavid Xu 		PROC_SUNLOCK(p);
3383407948a5SDavid Xu 		p->p_flag &= ~P_WAITED;
3384e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
33857f96995eSDavid Xu 		childproc_stopped(p, (p->p_flag & P_TRACED) ?
3386ebceaf6dSDavid Xu 			CLD_TRAPPED : CLD_STOPPED);
3387e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
33887b4a950aSDavid Xu 		PROC_SLOCK(p);
3389e574e444SDavid Xu 	}
3390e574e444SDavid Xu }
3391e574e444SDavid Xu 
3392df8bae1dSRodney W. Grimes /*
3393df8bae1dSRodney W. Grimes  * Take the action for the specified signal
3394df8bae1dSRodney W. Grimes  * from the current set of pending signals.
3395df8bae1dSRodney W. Grimes  */
339675c586a4SKonstantin Belousov int
postsig(int sig)33970167b33bSKonstantin Belousov postsig(int sig)
3398df8bae1dSRodney W. Grimes {
33990167b33bSKonstantin Belousov 	struct thread *td;
34000167b33bSKonstantin Belousov 	struct proc *p;
3401628d2653SJohn Baldwin 	struct sigacts *ps;
34022c42a146SMarcel Moolenaar 	sig_t action;
34039104847fSDavid Xu 	ksiginfo_t ksi;
3404e442f29fSKonstantin Belousov 	sigset_t returnmask;
3405df8bae1dSRodney W. Grimes 
34062c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
34075526d2d9SEivind Eklund 
34080167b33bSKonstantin Belousov 	td = curthread;
34090167b33bSKonstantin Belousov 	p = td->td_proc;
34102ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3411628d2653SJohn Baldwin 	ps = p->p_sigacts;
341290af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
34135da49fcbSDavid Xu 	ksiginfo_init(&ksi);
34146b286ee8SKonstantin Belousov 	if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
34156b286ee8SKonstantin Belousov 	    sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
341675c586a4SKonstantin Belousov 		return (0);
34179104847fSDavid Xu 	ksi.ksi_signo = sig;
341856c06c4bSDavid Xu 	if (ksi.ksi_code == SI_TIMER)
341956c06c4bSDavid Xu 		itimer_accept(p, ksi.ksi_timerid, &ksi);
34202c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
3421df8bae1dSRodney W. Grimes #ifdef KTRACE
3422374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
34235e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
342461009552SJilles Tjoelker 		    &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3425df8bae1dSRodney W. Grimes #endif
34262a024a2bSSean Eric Fagan 
34278460a577SJohn Birrell 	if (action == SIG_DFL) {
3428df8bae1dSRodney W. Grimes 		/*
3429df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
3430df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
3431df8bae1dSRodney W. Grimes 		 */
343290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
343386be94fcSTycho Nightingale 		proc_td_siginfo_capture(td, &ksi.ksi_info);
3434aaa92413SKonstantin Belousov 		sigexit(td, sig);
3435df8bae1dSRodney W. Grimes 		/* NOTREACHED */
3436df8bae1dSRodney W. Grimes 	} else {
3437df8bae1dSRodney W. Grimes 		/*
3438df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
3439df8bae1dSRodney W. Grimes 		 */
3440cd735d8fSKonstantin Belousov 		KASSERT(action != SIG_IGN, ("postsig action %p", action));
3441cd735d8fSKonstantin Belousov 		KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3442cd735d8fSKonstantin Belousov 		    ("postsig action: blocked sig %d", sig));
3443cd735d8fSKonstantin Belousov 
3444df8bae1dSRodney W. Grimes 		/*
3445df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
3446645682fdSLuoqi Chen 		 * occurrences of this signal.
3447df8bae1dSRodney W. Grimes 		 *
3448645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
3449df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
3450645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
3451df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
3452df8bae1dSRodney W. Grimes 		 */
34535e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
34544093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
34555e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
3456df8bae1dSRodney W. Grimes 		} else
34574093529dSJeff Roberson 			returnmask = td->td_sigmask;
34582c42a146SMarcel Moolenaar 
3459c90c9021SEd Schouten 		if (p->p_sig == sig) {
34606626c604SJulian Elischer 			p->p_sig = 0;
3461df8bae1dSRodney W. Grimes 		}
34629104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3463e442f29fSKonstantin Belousov 		postsig_done(sig, td, ps);
3464df8bae1dSRodney W. Grimes 	}
346575c586a4SKonstantin Belousov 	return (1);
3466df8bae1dSRodney W. Grimes }
3467df8bae1dSRodney W. Grimes 
34680c82fb26SKonstantin Belousov int
sig_ast_checksusp(struct thread * td)34690c82fb26SKonstantin Belousov sig_ast_checksusp(struct thread *td)
34700c82fb26SKonstantin Belousov {
34713d277851SKonstantin Belousov 	struct proc *p __diagused;
34720c82fb26SKonstantin Belousov 	int ret;
34730c82fb26SKonstantin Belousov 
34740c82fb26SKonstantin Belousov 	p = td->td_proc;
34750c82fb26SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
34760c82fb26SKonstantin Belousov 
3477c6d31b83SKonstantin Belousov 	if (!td_ast_pending(td, TDA_SUSPEND))
34780c82fb26SKonstantin Belousov 		return (0);
34790c82fb26SKonstantin Belousov 
34800c82fb26SKonstantin Belousov 	ret = thread_suspend_check(1);
34810c82fb26SKonstantin Belousov 	MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
34820c82fb26SKonstantin Belousov 	return (ret);
34830c82fb26SKonstantin Belousov }
34840c82fb26SKonstantin Belousov 
34850c82fb26SKonstantin Belousov int
sig_ast_needsigchk(struct thread * td)34860c82fb26SKonstantin Belousov sig_ast_needsigchk(struct thread *td)
34870c82fb26SKonstantin Belousov {
34880c82fb26SKonstantin Belousov 	struct proc *p;
34890c82fb26SKonstantin Belousov 	struct sigacts *ps;
34900c82fb26SKonstantin Belousov 	int ret, sig;
34910c82fb26SKonstantin Belousov 
34920c82fb26SKonstantin Belousov 	p = td->td_proc;
34930c82fb26SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
34940c82fb26SKonstantin Belousov 
3495c6d31b83SKonstantin Belousov 	if (!td_ast_pending(td, TDA_SIG))
34960c82fb26SKonstantin Belousov 		return (0);
34970c82fb26SKonstantin Belousov 
34980c82fb26SKonstantin Belousov 	ps = p->p_sigacts;
34990c82fb26SKonstantin Belousov 	mtx_lock(&ps->ps_mtx);
35000c82fb26SKonstantin Belousov 	sig = cursig(td);
35010c82fb26SKonstantin Belousov 	if (sig == -1) {
35020c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
35030c82fb26SKonstantin Belousov 		KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
35040c82fb26SKonstantin Belousov 		KASSERT(TD_SBDRY_INTR(td),
35050c82fb26SKonstantin Belousov 		    ("lost TDF_SERESTART of TDF_SEINTR"));
35060c82fb26SKonstantin Belousov 		KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
35070c82fb26SKonstantin Belousov 		    (TDF_SEINTR | TDF_SERESTART),
35080c82fb26SKonstantin Belousov 		    ("both TDF_SEINTR and TDF_SERESTART"));
35090c82fb26SKonstantin Belousov 		ret = TD_SBDRY_ERRNO(td);
35100c82fb26SKonstantin Belousov 	} else if (sig != 0) {
35110c82fb26SKonstantin Belousov 		ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
35120c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
35130c82fb26SKonstantin Belousov 	} else {
35140c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
35150c82fb26SKonstantin Belousov 		ret = 0;
35160c82fb26SKonstantin Belousov 	}
35170c82fb26SKonstantin Belousov 
35180c82fb26SKonstantin Belousov 	/*
35190c82fb26SKonstantin Belousov 	 * Do not go into sleep if this thread was the ptrace(2)
35200c82fb26SKonstantin Belousov 	 * attach leader.  cursig() consumed SIGSTOP from PT_ATTACH,
35210c82fb26SKonstantin Belousov 	 * but we usually act on the signal by interrupting sleep, and
35220c82fb26SKonstantin Belousov 	 * should do that here as well.
35230c82fb26SKonstantin Belousov 	 */
35240c82fb26SKonstantin Belousov 	if ((td->td_dbgflags & TDB_FSTP) != 0) {
35250c82fb26SKonstantin Belousov 		if (ret == 0)
35260c82fb26SKonstantin Belousov 			ret = EINTR;
35270c82fb26SKonstantin Belousov 		td->td_dbgflags &= ~TDB_FSTP;
35280c82fb26SKonstantin Belousov 	}
35290c82fb26SKonstantin Belousov 
35300c82fb26SKonstantin Belousov 	return (ret);
35310c82fb26SKonstantin Belousov }
35320c82fb26SKonstantin Belousov 
35330400be45SKonstantin Belousov int
sig_intr(void)35340400be45SKonstantin Belousov sig_intr(void)
35350400be45SKonstantin Belousov {
35360400be45SKonstantin Belousov 	struct thread *td;
35370400be45SKonstantin Belousov 	struct proc *p;
35380400be45SKonstantin Belousov 	int ret;
35390400be45SKonstantin Belousov 
35400400be45SKonstantin Belousov 	td = curthread;
3541c6d31b83SKonstantin Belousov 	if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3542203dda8aSKonstantin Belousov 		return (0);
3543203dda8aSKonstantin Belousov 
35440400be45SKonstantin Belousov 	p = td->td_proc;
35450400be45SKonstantin Belousov 
35460400be45SKonstantin Belousov 	PROC_LOCK(p);
35470400be45SKonstantin Belousov 	ret = sig_ast_checksusp(td);
35480400be45SKonstantin Belousov 	if (ret == 0)
35490400be45SKonstantin Belousov 		ret = sig_ast_needsigchk(td);
35500400be45SKonstantin Belousov 	PROC_UNLOCK(p);
35510400be45SKonstantin Belousov 	return (ret);
35520400be45SKonstantin Belousov }
35530400be45SKonstantin Belousov 
3554244ab566SKonstantin Belousov bool
curproc_sigkilled(void)3555244ab566SKonstantin Belousov curproc_sigkilled(void)
3556244ab566SKonstantin Belousov {
3557244ab566SKonstantin Belousov 	struct thread *td;
3558244ab566SKonstantin Belousov 	struct proc *p;
3559244ab566SKonstantin Belousov 	struct sigacts *ps;
3560244ab566SKonstantin Belousov 	bool res;
3561244ab566SKonstantin Belousov 
3562244ab566SKonstantin Belousov 	td = curthread;
3563c6d31b83SKonstantin Belousov 	if (!td_ast_pending(td, TDA_SIG))
3564244ab566SKonstantin Belousov 		return (false);
3565244ab566SKonstantin Belousov 
3566244ab566SKonstantin Belousov 	p = td->td_proc;
3567244ab566SKonstantin Belousov 	PROC_LOCK(p);
3568244ab566SKonstantin Belousov 	ps = p->p_sigacts;
3569244ab566SKonstantin Belousov 	mtx_lock(&ps->ps_mtx);
3570244ab566SKonstantin Belousov 	res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3571244ab566SKonstantin Belousov 	    SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3572244ab566SKonstantin Belousov 	mtx_unlock(&ps->ps_mtx);
3573244ab566SKonstantin Belousov 	PROC_UNLOCK(p);
3574244ab566SKonstantin Belousov 	return (res);
3575244ab566SKonstantin Belousov }
3576244ab566SKonstantin Belousov 
3577a70e9a13SKonstantin Belousov void
proc_wkilled(struct proc * p)3578a70e9a13SKonstantin Belousov proc_wkilled(struct proc *p)
3579a70e9a13SKonstantin Belousov {
3580a70e9a13SKonstantin Belousov 
3581a70e9a13SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
3582e24a6552SMark Johnston 	if ((p->p_flag & P_WKILLED) == 0)
3583a70e9a13SKonstantin Belousov 		p->p_flag |= P_WKILLED;
3584a70e9a13SKonstantin Belousov }
3585a70e9a13SKonstantin Belousov 
3586df8bae1dSRodney W. Grimes /*
3587df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
3588df8bae1dSRodney W. Grimes  */
358926f9a767SRodney W. Grimes void
killproc(struct proc * p,const char * why)3590fe20aaecSRyan Libby killproc(struct proc *p, const char *why)
3591df8bae1dSRodney W. Grimes {
35929081e5e8SJohn Baldwin 
35939081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3594c3209846SPawel Jakub Dawidek 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3595c3209846SPawel Jakub Dawidek 	    p->p_comm);
3596af8beccaSKristof Provost 	log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
35974ae4822dSKristof Provost 	    p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3598de2d0d29SKristof Provost 	    p->p_ucred->cr_uid, why);
3599a70e9a13SKonstantin Belousov 	proc_wkilled(p);
36008451d0ddSKip Macy 	kern_psignal(p, SIGKILL);
3601df8bae1dSRodney W. Grimes }
3602df8bae1dSRodney W. Grimes 
3603df8bae1dSRodney W. Grimes /*
3604df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
3605df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
3606df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
3607df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
3608df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
3609df8bae1dSRodney W. Grimes  * does not return.
3610df8bae1dSRodney W. Grimes  */
3611aaa92413SKonstantin Belousov void
sigexit(struct thread * td,int sig)3612aaa92413SKonstantin Belousov sigexit(struct thread *td, int sig)
3613df8bae1dSRodney W. Grimes {
3614b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
36156edbe561SEd Maste 	const char *coreinfo;
36166edbe561SEd Maste 	int rv;
3617dabf006aSKyle Evans 	bool logexit;
3618df8bae1dSRodney W. Grimes 
3619628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
36204493a13eSKonstantin Belousov 	proc_set_p2_wexit(p);
36214493a13eSKonstantin Belousov 
3622df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
3623dabf006aSKyle Evans 	if ((p->p_flag2 & P2_LOGSIGEXIT_CTL) == 0)
3624dabf006aSKyle Evans 		logexit = kern_logsigexit != 0;
3625dabf006aSKyle Evans 	else
3626dabf006aSKyle Evans 		logexit = (p->p_flag2 & P2_LOGSIGEXIT_ENABLE) != 0;
3627dabf006aSKyle Evans 
3628f97c3df1SDavid Schultz 	/*
3629f97c3df1SDavid Schultz 	 * We must be single-threading to generate a core dump.  This
3630f97c3df1SDavid Schultz 	 * ensures that the registers in the core file are up-to-date.
3631f97c3df1SDavid Schultz 	 * Also, the ELF dump handler assumes that the thread list doesn't
3632f97c3df1SDavid Schultz 	 * change out from under it.
3633f97c3df1SDavid Schultz 	 *
3634f97c3df1SDavid Schultz 	 * XXX If another thread attempts to single-thread before us
3635f97c3df1SDavid Schultz 	 *     (e.g. via fork()), we won't get a dump at all.
3636f97c3df1SDavid Schultz 	 */
3637fd50a707SBrooks Davis 	if ((sigprop(sig) & SIGPROP_CORE) &&
3638fd50a707SBrooks Davis 	    thread_single(p, SINGLE_NO_EXIT) == 0) {
36392c42a146SMarcel Moolenaar 		p->p_sig = sig;
3640c364e17eSAndrey A. Chernov 		/*
3641c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
3642c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
3643c364e17eSAndrey A. Chernov 		 * these messages.)
3644c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
36454ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
3646c364e17eSAndrey A. Chernov 		 */
36476edbe561SEd Maste 		rv = coredump(td);
36486edbe561SEd Maste 		switch (rv) {
36496edbe561SEd Maste 		case 0:
36502c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
36516edbe561SEd Maste 			coreinfo = " (core dumped)";
36526edbe561SEd Maste 			break;
36536edbe561SEd Maste 		case EFAULT:
36546edbe561SEd Maste 			coreinfo = " (no core dump - bad address)";
36556edbe561SEd Maste 			break;
36566edbe561SEd Maste 		case EINVAL:
36576edbe561SEd Maste 			coreinfo = " (no core dump - invalid argument)";
36586edbe561SEd Maste 			break;
36596edbe561SEd Maste 		case EFBIG:
36606edbe561SEd Maste 			coreinfo = " (no core dump - too large)";
36616edbe561SEd Maste 			break;
36626edbe561SEd Maste 		default:
36636edbe561SEd Maste 			coreinfo = " (no core dump - other error)";
36641a6238d1SEd Maste 			break;
36656edbe561SEd Maste 		}
3666dabf006aSKyle Evans 		if (logexit)
366757308494SJoerg Wunsch 			log(LOG_INFO,
3668af8beccaSKristof Provost 			    "pid %d (%s), jid %d, uid %d: exited on "
36694ae4822dSKristof Provost 			    "signal %d%s\n", p->p_pid, p->p_comm,
36704ae4822dSKristof Provost 			    p->p_ucred->cr_prison->pr_id,
3671de2d0d29SKristof Provost 			    td->td_ucred->cr_uid,
36726edbe561SEd Maste 			    sig &~ WCOREFLAG, coreinfo);
36734ae89b95SJohn Baldwin 	} else
3674628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3675aaa92413SKonstantin Belousov 	exit1(td, 0, sig);
3676df8bae1dSRodney W. Grimes 	/* NOTREACHED */
3677df8bae1dSRodney W. Grimes }
3678df8bae1dSRodney W. Grimes 
3679ebceaf6dSDavid Xu /*
36807f96995eSDavid Xu  * Send queued SIGCHLD to parent when child process's state
36817f96995eSDavid Xu  * is changed.
3682ebceaf6dSDavid Xu  */
36837f96995eSDavid Xu static void
sigparent(struct proc * p,int reason,int status)36847f96995eSDavid Xu sigparent(struct proc *p, int reason, int status)
3685ebceaf6dSDavid Xu {
3686ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
3687ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3688ebceaf6dSDavid Xu 
3689ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
3690ebceaf6dSDavid Xu 		p->p_ksi->ksi_signo  = SIGCHLD;
3691ebceaf6dSDavid Xu 		p->p_ksi->ksi_code   = reason;
36927f96995eSDavid Xu 		p->p_ksi->ksi_status = status;
3693ebceaf6dSDavid Xu 		p->p_ksi->ksi_pid    = p->p_pid;
3694ebceaf6dSDavid Xu 		p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
3695ebceaf6dSDavid Xu 		if (KSI_ONQ(p->p_ksi))
3696ebceaf6dSDavid Xu 			return;
3697ebceaf6dSDavid Xu 	}
3698ad6eec7bSJohn Baldwin 	pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3699ebceaf6dSDavid Xu }
3700ebceaf6dSDavid Xu 
37017f96995eSDavid Xu static void
childproc_jobstate(struct proc * p,int reason,int sig)3702b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig)
37037f96995eSDavid Xu {
37047f96995eSDavid Xu 	struct sigacts *ps;
37057f96995eSDavid Xu 
37067f96995eSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
37077f96995eSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
37087f96995eSDavid Xu 
37097f96995eSDavid Xu 	/*
37107f96995eSDavid Xu 	 * Wake up parent sleeping in kern_wait(), also send
37117f96995eSDavid Xu 	 * SIGCHLD to parent, but SIGCHLD does not guarantee
37127f96995eSDavid Xu 	 * that parent will awake, because parent may masked
37137f96995eSDavid Xu 	 * the signal.
37147f96995eSDavid Xu 	 */
37157f96995eSDavid Xu 	p->p_pptr->p_flag |= P_STATCHILD;
37167f96995eSDavid Xu 	wakeup(p->p_pptr);
37177f96995eSDavid Xu 
37187f96995eSDavid Xu 	ps = p->p_pptr->p_sigacts;
37197f96995eSDavid Xu 	mtx_lock(&ps->ps_mtx);
37207f96995eSDavid Xu 	if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
37217f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
3722b20a9aa9SJilles Tjoelker 		sigparent(p, reason, sig);
37237f96995eSDavid Xu 	} else
37247f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
37257f96995eSDavid Xu }
37267f96995eSDavid Xu 
37277f96995eSDavid Xu void
childproc_stopped(struct proc * p,int reason)37287f96995eSDavid Xu childproc_stopped(struct proc *p, int reason)
37297f96995eSDavid Xu {
3730b4490c6eSKonstantin Belousov 
3731b4490c6eSKonstantin Belousov 	childproc_jobstate(p, reason, p->p_xsig);
37327f96995eSDavid Xu }
37337f96995eSDavid Xu 
3734ebceaf6dSDavid Xu void
childproc_continued(struct proc * p)3735ebceaf6dSDavid Xu childproc_continued(struct proc *p)
3736ebceaf6dSDavid Xu {
37377f96995eSDavid Xu 	childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3738ebceaf6dSDavid Xu }
3739ebceaf6dSDavid Xu 
3740ebceaf6dSDavid Xu void
childproc_exited(struct proc * p)3741ebceaf6dSDavid Xu childproc_exited(struct proc *p)
3742ebceaf6dSDavid Xu {
3743b4490c6eSKonstantin Belousov 	int reason, status;
3744ebceaf6dSDavid Xu 
3745b4490c6eSKonstantin Belousov 	if (WCOREDUMP(p->p_xsig)) {
3746b4490c6eSKonstantin Belousov 		reason = CLD_DUMPED;
3747b4490c6eSKonstantin Belousov 		status = WTERMSIG(p->p_xsig);
3748b4490c6eSKonstantin Belousov 	} else if (WIFSIGNALED(p->p_xsig)) {
3749b4490c6eSKonstantin Belousov 		reason = CLD_KILLED;
3750b4490c6eSKonstantin Belousov 		status = WTERMSIG(p->p_xsig);
3751b4490c6eSKonstantin Belousov 	} else {
3752b4490c6eSKonstantin Belousov 		reason = CLD_EXITED;
3753b4490c6eSKonstantin Belousov 		status = p->p_xexit;
3754b4490c6eSKonstantin Belousov 	}
37557f96995eSDavid Xu 	/*
37567f96995eSDavid Xu 	 * XXX avoid calling wakeup(p->p_pptr), the work is
37577f96995eSDavid Xu 	 * done in exit1().
37587f96995eSDavid Xu 	 */
37597f96995eSDavid Xu 	sigparent(p, reason, status);
3760ebceaf6dSDavid Xu }
3761ebceaf6dSDavid Xu 
3762f1fe1e02SMariusz Zaborski #define	MAX_NUM_CORE_FILES 100000
3763cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES
3764cc37baeaSStephen J. Kiernan #define	NUM_CORE_FILES 5
3765cc37baeaSStephen J. Kiernan #endif
3766cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3767cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES;
3768e7228204SAlfred Perlstein 
3769e7228204SAlfred Perlstein static int
sysctl_debug_num_cores_check(SYSCTL_HANDLER_ARGS)3770e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3771e7228204SAlfred Perlstein {
3772e7228204SAlfred Perlstein 	int error;
3773e7228204SAlfred Perlstein 	int new_val;
3774e7228204SAlfred Perlstein 
3775c5105012SKonstantin Belousov 	new_val = num_cores;
3776e7228204SAlfred Perlstein 	error = sysctl_handle_int(oidp, &new_val, 0, req);
3777e7228204SAlfred Perlstein 	if (error != 0 || req->newptr == NULL)
3778e7228204SAlfred Perlstein 		return (error);
3779cc37baeaSStephen J. Kiernan 	if (new_val > MAX_NUM_CORE_FILES)
3780cc37baeaSStephen J. Kiernan 		new_val = MAX_NUM_CORE_FILES;
3781e7228204SAlfred Perlstein 	if (new_val < 0)
3782e7228204SAlfred Perlstein 		new_val = 0;
3783e7228204SAlfred Perlstein 	num_cores = new_val;
3784e7228204SAlfred Perlstein 	return (0);
3785e7228204SAlfred Perlstein }
37867029da5cSPawel Biernacki SYSCTL_PROC(_debug, OID_AUTO, ncores,
3787fe27f1dbSAlexander Motin     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
37887029da5cSPawel Biernacki     sysctl_debug_num_cores_check, "I",
37896cad1a5dSMariusz Zaborski     "Maximum number of generated process corefiles while using index format");
3790e7228204SAlfred Perlstein 
37916026dcd7SMark Johnston #define	GZIP_SUFFIX	".gz"
37926026dcd7SMark Johnston #define	ZSTD_SUFFIX	".zst"
3793aa14e9b7SMark Johnston 
379478f57a9cSMark Johnston int compress_user_cores = 0;
3795e7228204SAlfred Perlstein 
379678f57a9cSMark Johnston static int
sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)379778f57a9cSMark Johnston sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
379878f57a9cSMark Johnston {
379978f57a9cSMark Johnston 	int error, val;
380078f57a9cSMark Johnston 
380178f57a9cSMark Johnston 	val = compress_user_cores;
380278f57a9cSMark Johnston 	error = sysctl_handle_int(oidp, &val, 0, req);
380378f57a9cSMark Johnston 	if (error != 0 || req->newptr == NULL)
380478f57a9cSMark Johnston 		return (error);
380578f57a9cSMark Johnston 	if (val != 0 && !compressor_avail(val))
380678f57a9cSMark Johnston 		return (EINVAL);
380778f57a9cSMark Johnston 	compress_user_cores = val;
380878f57a9cSMark Johnston 	return (error);
380978f57a9cSMark Johnston }
38107029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
38117029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
38127029da5cSPawel Biernacki     sysctl_compress_user_cores, "I",
38136026dcd7SMark Johnston     "Enable compression of user corefiles ("
38146026dcd7SMark Johnston     __XSTRING(COMPRESS_GZIP) " = gzip, "
38156026dcd7SMark Johnston     __XSTRING(COMPRESS_ZSTD) " = zstd)");
381678f57a9cSMark Johnston 
381778f57a9cSMark Johnston int compress_user_cores_level = 6;
381878f57a9cSMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
381978f57a9cSMark Johnston     &compress_user_cores_level, 0,
382078f57a9cSMark Johnston     "Corefile compression level");
3821e7228204SAlfred Perlstein 
38225bc0ff88SMateusz Guzik /*
38235bc0ff88SMateusz Guzik  * Protect the access to corefilename[] by allproc_lock.
38245bc0ff88SMateusz Guzik  */
38255bc0ff88SMateusz Guzik #define	corefilename_lock	allproc_lock
38265bc0ff88SMateusz Guzik 
38276d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"};
3828fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
38295bc0ff88SMateusz Guzik 
38305bc0ff88SMateusz Guzik static int
sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)38315bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
38325bc0ff88SMateusz Guzik {
38335bc0ff88SMateusz Guzik 	int error;
38345bc0ff88SMateusz Guzik 
38355bc0ff88SMateusz Guzik 	sx_xlock(&corefilename_lock);
38365bc0ff88SMateusz Guzik 	error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
38375bc0ff88SMateusz Guzik 	    req);
38385bc0ff88SMateusz Guzik 	sx_xunlock(&corefilename_lock);
38395bc0ff88SMateusz Guzik 
38405bc0ff88SMateusz Guzik 	return (error);
38415bc0ff88SMateusz Guzik }
3842fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
38435bc0ff88SMateusz Guzik     CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
38445bc0ff88SMateusz Guzik     "Process corefile name format string");
3845c5edb423SSean Eric Fagan 
38460dea6e3cSMariusz Zaborski static void
vnode_close_locked(struct thread * td,struct vnode * vp)38470dea6e3cSMariusz Zaborski vnode_close_locked(struct thread *td, struct vnode *vp)
38480dea6e3cSMariusz Zaborski {
38490dea6e3cSMariusz Zaborski 
3850b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
38510dea6e3cSMariusz Zaborski 	vn_close(vp, FWRITE, td->td_ucred, td);
38520dea6e3cSMariusz Zaborski }
38530dea6e3cSMariusz Zaborski 
38540dea6e3cSMariusz Zaborski /*
38550dea6e3cSMariusz Zaborski  * If the core format has a %I in it, then we need to check
38560dea6e3cSMariusz Zaborski  * for existing corefiles before defining a name.
3857f1fe1e02SMariusz Zaborski  * To do this we iterate over 0..ncores to find a
38580dea6e3cSMariusz Zaborski  * non-existing core file name to use. If all core files are
38590dea6e3cSMariusz Zaborski  * already used we choose the oldest one.
38600dea6e3cSMariusz Zaborski  */
38610dea6e3cSMariusz Zaborski static int
corefile_open_last(struct thread * td,char * name,int indexpos,int indexlen,int ncores,struct vnode ** vpp)38620dea6e3cSMariusz Zaborski corefile_open_last(struct thread *td, char *name, int indexpos,
3863f1fe1e02SMariusz Zaborski     int indexlen, int ncores, struct vnode **vpp)
38640dea6e3cSMariusz Zaborski {
38650dea6e3cSMariusz Zaborski 	struct vnode *oldvp, *nextvp, *vp;
38660dea6e3cSMariusz Zaborski 	struct vattr vattr;
38670dea6e3cSMariusz Zaborski 	struct nameidata nd;
38680dea6e3cSMariusz Zaborski 	int error, i, flags, oflags, cmode;
3869f1fe1e02SMariusz Zaborski 	char ch;
38700dea6e3cSMariusz Zaborski 	struct timespec lasttime;
38710dea6e3cSMariusz Zaborski 
38720dea6e3cSMariusz Zaborski 	nextvp = oldvp = NULL;
38730dea6e3cSMariusz Zaborski 	cmode = S_IRUSR | S_IWUSR;
38740dea6e3cSMariusz Zaborski 	oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
38750dea6e3cSMariusz Zaborski 	    (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
38760dea6e3cSMariusz Zaborski 
3877f1fe1e02SMariusz Zaborski 	for (i = 0; i < ncores; i++) {
38780dea6e3cSMariusz Zaborski 		flags = O_CREAT | FWRITE | O_NOFOLLOW;
3879f1fe1e02SMariusz Zaborski 
3880f1fe1e02SMariusz Zaborski 		ch = name[indexpos + indexlen];
3881f1fe1e02SMariusz Zaborski 		(void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3882f1fe1e02SMariusz Zaborski 		    i);
3883f1fe1e02SMariusz Zaborski 		name[indexpos + indexlen] = ch;
38840dea6e3cSMariusz Zaborski 
38857e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
38860dea6e3cSMariusz Zaborski 		error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
38870dea6e3cSMariusz Zaborski 		    NULL);
38880dea6e3cSMariusz Zaborski 		if (error != 0)
38890dea6e3cSMariusz Zaborski 			break;
38900dea6e3cSMariusz Zaborski 
38910dea6e3cSMariusz Zaborski 		vp = nd.ni_vp;
3892bb92cd7bSMateusz Guzik 		NDFREE_PNBUF(&nd);
38930dea6e3cSMariusz Zaborski 		if ((flags & O_CREAT) == O_CREAT) {
38940dea6e3cSMariusz Zaborski 			nextvp = vp;
38950dea6e3cSMariusz Zaborski 			break;
38960dea6e3cSMariusz Zaborski 		}
38970dea6e3cSMariusz Zaborski 
38980dea6e3cSMariusz Zaborski 		error = VOP_GETATTR(vp, &vattr, td->td_ucred);
38990dea6e3cSMariusz Zaborski 		if (error != 0) {
39000dea6e3cSMariusz Zaborski 			vnode_close_locked(td, vp);
39010dea6e3cSMariusz Zaborski 			break;
39020dea6e3cSMariusz Zaborski 		}
39030dea6e3cSMariusz Zaborski 
39040dea6e3cSMariusz Zaborski 		if (oldvp == NULL ||
39050dea6e3cSMariusz Zaborski 		    lasttime.tv_sec > vattr.va_mtime.tv_sec ||
39060dea6e3cSMariusz Zaborski 		    (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
39070dea6e3cSMariusz Zaborski 		    lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
39080dea6e3cSMariusz Zaborski 			if (oldvp != NULL)
3909e298466eSAndriy Gapon 				vn_close(oldvp, FWRITE, td->td_ucred, td);
39100dea6e3cSMariusz Zaborski 			oldvp = vp;
3911e298466eSAndriy Gapon 			VOP_UNLOCK(oldvp);
39120dea6e3cSMariusz Zaborski 			lasttime = vattr.va_mtime;
39130dea6e3cSMariusz Zaborski 		} else {
39140dea6e3cSMariusz Zaborski 			vnode_close_locked(td, vp);
39150dea6e3cSMariusz Zaborski 		}
39160dea6e3cSMariusz Zaborski 	}
39170dea6e3cSMariusz Zaborski 
39180dea6e3cSMariusz Zaborski 	if (oldvp != NULL) {
391989f2ab06SKonstantin Belousov 		if (nextvp == NULL) {
392089f2ab06SKonstantin Belousov 			if ((td->td_proc->p_flag & P_SUGID) != 0) {
392189f2ab06SKonstantin Belousov 				error = EFAULT;
3922e298466eSAndriy Gapon 				vn_close(oldvp, FWRITE, td->td_ucred, td);
392389f2ab06SKonstantin Belousov 			} else {
392489f2ab06SKonstantin Belousov 				nextvp = oldvp;
3925e298466eSAndriy Gapon 				error = vn_lock(nextvp, LK_EXCLUSIVE);
3926e298466eSAndriy Gapon 				if (error != 0) {
3927e298466eSAndriy Gapon 					vn_close(nextvp, FWRITE, td->td_ucred,
3928e298466eSAndriy Gapon 					    td);
3929e298466eSAndriy Gapon 					nextvp = NULL;
3930e298466eSAndriy Gapon 				}
393189f2ab06SKonstantin Belousov 			}
393289f2ab06SKonstantin Belousov 		} else {
3933e298466eSAndriy Gapon 			vn_close(oldvp, FWRITE, td->td_ucred, td);
393489f2ab06SKonstantin Belousov 		}
39350dea6e3cSMariusz Zaborski 	}
39360dea6e3cSMariusz Zaborski 	if (error != 0) {
39370dea6e3cSMariusz Zaborski 		if (nextvp != NULL)
39380dea6e3cSMariusz Zaborski 			vnode_close_locked(td, oldvp);
39390dea6e3cSMariusz Zaborski 	} else {
39400dea6e3cSMariusz Zaborski 		*vpp = nextvp;
39410dea6e3cSMariusz Zaborski 	}
39420dea6e3cSMariusz Zaborski 
39430dea6e3cSMariusz Zaborski 	return (error);
39440dea6e3cSMariusz Zaborski }
39450dea6e3cSMariusz Zaborski 
3946c5edb423SSean Eric Fagan /*
3947c345faeaSPawel Jakub Dawidek  * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3948c345faeaSPawel Jakub Dawidek  * Expand the name described in corefilename, using name, uid, and pid
3949c345faeaSPawel Jakub Dawidek  * and open/create core file.
3950c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
3951c5edb423SSean Eric Fagan  *	%N	name of process ("name")
3952c5edb423SSean Eric Fagan  *	%P	process id (pid)
3953c5edb423SSean Eric Fagan  *	%U	user id (uid)
3954c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
3955c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3956c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
3957c5edb423SSean Eric Fagan  */
3958c345faeaSPawel Jakub Dawidek static int
corefile_open(const char * comm,uid_t uid,pid_t pid,struct thread * td,int compress,int signum,struct vnode ** vpp,char ** namep)3959c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
39609d3ecb7eSEric van Gyzen     int compress, int signum, struct vnode **vpp, char **namep)
39618b43b535SAlfred Perlstein {
396236b208e0SRobert Watson 	struct sbuf sb;
39630dea6e3cSMariusz Zaborski 	struct nameidata nd;
396436b208e0SRobert Watson 	const char *format;
3965c345faeaSPawel Jakub Dawidek 	char *hostname, *name;
3966f1fe1e02SMariusz Zaborski 	int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3967c5edb423SSean Eric Fagan 
3968b7402d82SAlfred Perlstein 	hostname = NULL;
39698b43b535SAlfred Perlstein 	format = corefilename;
3970086053a3SPawel Jakub Dawidek 	name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3971f1fe1e02SMariusz Zaborski 	indexlen = 0;
3972e7228204SAlfred Perlstein 	indexpos = -1;
3973f1fe1e02SMariusz Zaborski 	ncores = num_cores;
3974c52ff611SPawel Jakub Dawidek 	(void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
39755bc0ff88SMateusz Guzik 	sx_slock(&corefilename_lock);
397629146f1aSPawel Jakub Dawidek 	for (i = 0; format[i] != '\0'; i++) {
3977c5edb423SSean Eric Fagan 		switch (format[i]) {
3978c5edb423SSean Eric Fagan 		case '%':	/* Format character */
3979c5edb423SSean Eric Fagan 			i++;
3980c5edb423SSean Eric Fagan 			switch (format[i]) {
3981c5edb423SSean Eric Fagan 			case '%':
398236b208e0SRobert Watson 				sbuf_putc(&sb, '%');
3983c5edb423SSean Eric Fagan 				break;
3984e7228204SAlfred Perlstein 			case 'H':	/* hostname */
3985b7402d82SAlfred Perlstein 				if (hostname == NULL) {
3986b7402d82SAlfred Perlstein 					hostname = malloc(MAXHOSTNAMELEN,
3987086053a3SPawel Jakub Dawidek 					    M_TEMP, M_WAITOK);
3988b7402d82SAlfred Perlstein 				}
3989e7228204SAlfred Perlstein 				getcredhostname(td->td_ucred, hostname,
3990b7402d82SAlfred Perlstein 				    MAXHOSTNAMELEN);
39910a713948SAlexander Motin 				sbuf_cat(&sb, hostname);
3992e7228204SAlfred Perlstein 				break;
3993e7228204SAlfred Perlstein 			case 'I':	/* autoincrementing index */
3994f1fe1e02SMariusz Zaborski 				if (indexpos != -1) {
3995f1fe1e02SMariusz Zaborski 					sbuf_printf(&sb, "%%I");
3996f1fe1e02SMariusz Zaborski 					break;
3997f1fe1e02SMariusz Zaborski 				}
3998f1fe1e02SMariusz Zaborski 
3999f1fe1e02SMariusz Zaborski 				indexpos = sbuf_len(&sb);
4000f1fe1e02SMariusz Zaborski 				sbuf_printf(&sb, "%u", ncores - 1);
4001f1fe1e02SMariusz Zaborski 				indexlen = sbuf_len(&sb) - indexpos;
4002e7228204SAlfred Perlstein 				break;
4003c5edb423SSean Eric Fagan 			case 'N':	/* process name */
4004c52ff611SPawel Jakub Dawidek 				sbuf_printf(&sb, "%s", comm);
4005c5edb423SSean Eric Fagan 				break;
4006c5edb423SSean Eric Fagan 			case 'P':	/* process id */
400736b208e0SRobert Watson 				sbuf_printf(&sb, "%u", pid);
4008c5edb423SSean Eric Fagan 				break;
40099d3ecb7eSEric van Gyzen 			case 'S':	/* signal number */
40109d3ecb7eSEric van Gyzen 				sbuf_printf(&sb, "%i", signum);
40119d3ecb7eSEric van Gyzen 				break;
4012c5edb423SSean Eric Fagan 			case 'U':	/* user id */
401336b208e0SRobert Watson 				sbuf_printf(&sb, "%u", uid);
4014c5edb423SSean Eric Fagan 				break;
4015c5edb423SSean Eric Fagan 			default:
40168b43b535SAlfred Perlstein 				log(LOG_ERR,
401736b208e0SRobert Watson 				    "Unknown format character %c in "
401836b208e0SRobert Watson 				    "corename `%s'\n", format[i], format);
401929146f1aSPawel Jakub Dawidek 				break;
4020c5edb423SSean Eric Fagan 			}
4021c5edb423SSean Eric Fagan 			break;
4022c5edb423SSean Eric Fagan 		default:
402336b208e0SRobert Watson 			sbuf_putc(&sb, format[i]);
40246c08be2bSPawel Jakub Dawidek 			break;
4025c5edb423SSean Eric Fagan 		}
4026c5edb423SSean Eric Fagan 	}
40275bc0ff88SMateusz Guzik 	sx_sunlock(&corefilename_lock);
4028b7402d82SAlfred Perlstein 	free(hostname, M_TEMP);
402978f57a9cSMark Johnston 	if (compress == COMPRESS_GZIP)
40300a713948SAlexander Motin 		sbuf_cat(&sb, GZIP_SUFFIX);
40316026dcd7SMark Johnston 	else if (compress == COMPRESS_ZSTD)
40320a713948SAlexander Motin 		sbuf_cat(&sb, ZSTD_SUFFIX);
40334d369413SMatthew D Fleming 	if (sbuf_error(&sb) != 0) {
403436b208e0SRobert Watson 		log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
4035c52ff611SPawel Jakub Dawidek 		    "long\n", (long)pid, comm, (u_long)uid);
4036b7402d82SAlfred Perlstein 		sbuf_delete(&sb);
4037c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
4038c345faeaSPawel Jakub Dawidek 		return (ENOMEM);
4039c5edb423SSean Eric Fagan 	}
404036b208e0SRobert Watson 	sbuf_finish(&sb);
404136b208e0SRobert Watson 	sbuf_delete(&sb);
4042e7228204SAlfred Perlstein 
4043e7228204SAlfred Perlstein 	if (indexpos != -1) {
4044f1fe1e02SMariusz Zaborski 		error = corefile_open_last(td, name, indexpos, indexlen, ncores,
4045f1fe1e02SMariusz Zaborski 		    vpp);
40460dea6e3cSMariusz Zaborski 		if (error != 0) {
4047e7228204SAlfred Perlstein 			log(LOG_ERR,
4048e7228204SAlfred Perlstein 			    "pid %d (%s), uid (%u):  Path `%s' failed "
4049e7228204SAlfred Perlstein 			    "on initial open test, error = %d\n",
4050c52ff611SPawel Jakub Dawidek 			    pid, comm, uid, name, error);
4051c345faeaSPawel Jakub Dawidek 		}
40520dea6e3cSMariusz Zaborski 	} else {
40530dea6e3cSMariusz Zaborski 		cmode = S_IRUSR | S_IWUSR;
40540dea6e3cSMariusz Zaborski 		oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
40550dea6e3cSMariusz Zaborski 		    (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
40560dea6e3cSMariusz Zaborski 		flags = O_CREAT | FWRITE | O_NOFOLLOW;
405789f2ab06SKonstantin Belousov 		if ((td->td_proc->p_flag & P_SUGID) != 0)
405889f2ab06SKonstantin Belousov 			flags |= O_EXCL;
40590dea6e3cSMariusz Zaborski 
40607e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
40610dea6e3cSMariusz Zaborski 		error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
40620dea6e3cSMariusz Zaborski 		    NULL);
40630dea6e3cSMariusz Zaborski 		if (error == 0) {
40640dea6e3cSMariusz Zaborski 			*vpp = nd.ni_vp;
4065bb92cd7bSMateusz Guzik 			NDFREE_PNBUF(&nd);
4066c345faeaSPawel Jakub Dawidek 		}
4067c345faeaSPawel Jakub Dawidek 	}
4068c345faeaSPawel Jakub Dawidek 
40690dea6e3cSMariusz Zaborski 	if (error != 0) {
4070c345faeaSPawel Jakub Dawidek #ifdef AUDIT
4071c345faeaSPawel Jakub Dawidek 		audit_proc_coredump(td, name, error);
4072c345faeaSPawel Jakub Dawidek #endif
4073c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
4074c345faeaSPawel Jakub Dawidek 		return (error);
4075e7228204SAlfred Perlstein 	}
4076c345faeaSPawel Jakub Dawidek 	*namep = name;
4077c345faeaSPawel Jakub Dawidek 	return (0);
407836b208e0SRobert Watson }
4079c5edb423SSean Eric Fagan 
4080df8bae1dSRodney W. Grimes /*
4081fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
4082fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
4083fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
4084fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
4085fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
4086fca666a1SJulian Elischer  */
4087fca666a1SJulian Elischer 
4088fca666a1SJulian Elischer static int
coredump(struct thread * td)4089b40ce416SJulian Elischer coredump(struct thread *td)
4090fca666a1SJulian Elischer {
4091b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
4092f06f465dSPawel Jakub Dawidek 	struct ucred *cred = td->td_ucred;
4093f06f465dSPawel Jakub Dawidek 	struct vnode *vp;
409406ae1e91SMatthew Dillon 	struct flock lf;
4095fca666a1SJulian Elischer 	struct vattr vattr;
40967739d927SMateusz Guzik 	size_t fullpathsize;
4097c345faeaSPawel Jakub Dawidek 	int error, error1, locked;
4098fca666a1SJulian Elischer 	char *name;			/* name of corefile */
4099539c9eefSKonstantin Belousov 	void *rl_cookie;
4100fca666a1SJulian Elischer 	off_t limit;
4101842ab62bSRui Paulo 	char *fullpath, *freepath = NULL;
4102349fcda4SWarner Losh 	struct sbuf *sb;
4103fca666a1SJulian Elischer 
41044ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
4105f97c3df1SDavid Schultz 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
4106fca666a1SJulian Elischer 
4107677258f7SKonstantin Belousov 	if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
4108677258f7SKonstantin Belousov 	    (p->p_flag2 & P2_NOTRACE) != 0) {
4109628d2653SJohn Baldwin 		PROC_UNLOCK(p);
4110fca666a1SJulian Elischer 		return (EFAULT);
4111628d2653SJohn Baldwin 	}
4112fca666a1SJulian Elischer 
4113fca666a1SJulian Elischer 	/*
411435a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
411535a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
411635a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
411735a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
411835a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
411935a2598fSSean Eric Fagan 	 * if it is larger than the limit.
4120fca666a1SJulian Elischer 	 */
4121f6f6d240SMateusz Guzik 	limit = (off_t)lim_cur(td, RLIMIT_CORE);
4122b8fdb0d9SEdward Tomasz Napierala 	if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
4123628d2653SJohn Baldwin 		PROC_UNLOCK(p);
412491d5354aSJohn Baldwin 		return (EFBIG);
412557274c51SChristian S.J. Peron 	}
4126b8fdb0d9SEdward Tomasz Napierala 	PROC_UNLOCK(p);
412735a2598fSSean Eric Fagan 
4128aa14e9b7SMark Johnston 	error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
41299d3ecb7eSEric van Gyzen 	    compress_user_cores, p->p_sig, &vp, &name);
4130c345faeaSPawel Jakub Dawidek 	if (error != 0)
4131fca666a1SJulian Elischer 		return (error);
413206ae1e91SMatthew Dillon 
4133539c9eefSKonstantin Belousov 	/*
4134539c9eefSKonstantin Belousov 	 * Don't dump to non-regular files or files with links.
413589f2ab06SKonstantin Belousov 	 * Do not dump into system files. Effective user must own the corefile.
4136539c9eefSKonstantin Belousov 	 */
4137f06f465dSPawel Jakub Dawidek 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
41387a29e0bfSKonstantin Belousov 	    vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
413989f2ab06SKonstantin Belousov 	    vattr.va_uid != cred->cr_uid) {
4140b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
4141832dafadSDon Lewis 		error = EFAULT;
4142dacbc9dbSKonstantin Belousov 		goto out;
4143832dafadSDon Lewis 	}
4144832dafadSDon Lewis 
4145b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
4146539c9eefSKonstantin Belousov 
4147539c9eefSKonstantin Belousov 	/* Postpone other writers, including core dumps of other processes. */
4148539c9eefSKonstantin Belousov 	rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
4149539c9eefSKonstantin Belousov 
415006ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
415106ae1e91SMatthew Dillon 	lf.l_start = 0;
415206ae1e91SMatthew Dillon 	lf.l_len = 0;
415306ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
4154c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
415506ae1e91SMatthew Dillon 
4156fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
4157fca666a1SJulian Elischer 	vattr.va_size = 0;
41586141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
41596141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
4160cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
41610359a12eSAttilio Rao 	VOP_SETATTR(vp, &vattr, cred);
4162b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
4163628d2653SJohn Baldwin 	PROC_LOCK(p);
4164fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
4165628d2653SJohn Baldwin 	PROC_UNLOCK(p);
4166fca666a1SJulian Elischer 
416723c6445aSPawel Jakub Dawidek 	if (p->p_sysent->sv_coredump != NULL) {
416878f57a9cSMark Johnston 		error = p->p_sysent->sv_coredump(td, vp, limit, 0);
416923c6445aSPawel Jakub Dawidek 	} else {
417023c6445aSPawel Jakub Dawidek 		error = ENOSYS;
417123c6445aSPawel Jakub Dawidek 	}
4172fca666a1SJulian Elischer 
4173c447f5b2SRobert Watson 	if (locked) {
417406ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
417506ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
4176c447f5b2SRobert Watson 	}
4177539c9eefSKonstantin Belousov 	vn_rangelock_unlock(vp, rl_cookie);
4178dacbc9dbSKonstantin Belousov 
4179842ab62bSRui Paulo 	/*
4180842ab62bSRui Paulo 	 * Notify the userland helper that a process triggered a core dump.
4181842ab62bSRui Paulo 	 * This allows the helper to run an automated debugging session.
4182842ab62bSRui Paulo 	 */
4183dacbc9dbSKonstantin Belousov 	if (error != 0 || coredump_devctl == 0)
4184842ab62bSRui Paulo 		goto out;
4185349fcda4SWarner Losh 	sb = sbuf_new_auto();
4186feabaaf9SMateusz Guzik 	if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
4187349fcda4SWarner Losh 		goto out2;
41880a713948SAlexander Motin 	sbuf_cat(sb, "comm=\"");
4189349fcda4SWarner Losh 	devctl_safe_quote_sb(sb, fullpath);
4190842ab62bSRui Paulo 	free(freepath, M_TEMP);
41910a713948SAlexander Motin 	sbuf_cat(sb, "\" core=\"");
4192349fcda4SWarner Losh 
4193349fcda4SWarner Losh 	/*
4194349fcda4SWarner Losh 	 * We can't lookup core file vp directly. When we're replacing a core, and
4195349fcda4SWarner Losh 	 * other random times, we flush the name cache, so it will fail. Instead,
4196349fcda4SWarner Losh 	 * if the path of the core is relative, add the current dir in front if it.
4197349fcda4SWarner Losh 	 */
4198349fcda4SWarner Losh 	if (name[0] != '/') {
41997739d927SMateusz Guzik 		fullpathsize = MAXPATHLEN;
42007739d927SMateusz Guzik 		freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
4201feabaaf9SMateusz Guzik 		if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
42027739d927SMateusz Guzik 			free(freepath, M_TEMP);
4203349fcda4SWarner Losh 			goto out2;
4204349fcda4SWarner Losh 		}
4205349fcda4SWarner Losh 		devctl_safe_quote_sb(sb, fullpath);
42067739d927SMateusz Guzik 		free(freepath, M_TEMP);
4207349fcda4SWarner Losh 		sbuf_putc(sb, '/');
4208349fcda4SWarner Losh 	}
4209349fcda4SWarner Losh 	devctl_safe_quote_sb(sb, name);
42100a713948SAlexander Motin 	sbuf_putc(sb, '"');
4211349fcda4SWarner Losh 	if (sbuf_finish(sb) == 0)
4212349fcda4SWarner Losh 		devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
4213349fcda4SWarner Losh out2:
4214349fcda4SWarner Losh 	sbuf_delete(sb);
4215842ab62bSRui Paulo out:
4216dacbc9dbSKonstantin Belousov 	error1 = vn_close(vp, FWRITE, cred, td);
4217dacbc9dbSKonstantin Belousov 	if (error == 0)
4218dacbc9dbSKonstantin Belousov 		error = error1;
421957274c51SChristian S.J. Peron #ifdef AUDIT
422057274c51SChristian S.J. Peron 	audit_proc_coredump(td, name, error);
422157274c51SChristian S.J. Peron #endif
422257274c51SChristian S.J. Peron 	free(name, M_TEMP);
4223fca666a1SJulian Elischer 	return (error);
4224fca666a1SJulian Elischer }
4225fca666a1SJulian Elischer 
4226fca666a1SJulian Elischer /*
42270c14ff0eSRobert Watson  * Nonexistent system call-- signal process (may want to handle it).  Flag
42280c14ff0eSRobert Watson  * error in case process won't see signal immediately (blocked or ignored).
4229df8bae1dSRodney W. Grimes  */
4230d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
4231df8bae1dSRodney W. Grimes struct nosys_args {
4232df8bae1dSRodney W. Grimes 	int	dummy;
4233df8bae1dSRodney W. Grimes };
4234d2d3e875SBruce Evans #endif
4235df8bae1dSRodney W. Grimes /* ARGSUSED */
423626f9a767SRodney W. Grimes int
nosys(struct thread * td,struct nosys_args * args)4237e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args)
4238df8bae1dSRodney W. Grimes {
4239f5a077c3SKonstantin Belousov 	struct proc *p;
4240f5a077c3SKonstantin Belousov 
4241f5a077c3SKonstantin Belousov 	p = td->td_proc;
4242b40ce416SJulian Elischer 
42435804a162SKonstantin Belousov 	if (SV_PROC_FLAG(p, SV_SIGSYS) != 0 && kern_signosys) {
4244628d2653SJohn Baldwin 		PROC_LOCK(p);
4245888aefefSKonstantin Belousov 		tdsignal(td, SIGSYS);
4246628d2653SJohn Baldwin 		PROC_UNLOCK(p);
4247b82b4ae7SKonstantin Belousov 	}
42487ceeb35bSKonstantin Belousov 	if (kern_lognosys == 1 || kern_lognosys == 3) {
4249f5a077c3SKonstantin Belousov 		uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4250f5a077c3SKonstantin Belousov 		    td->td_sa.code);
42517ceeb35bSKonstantin Belousov 	}
425218f917a9SBrooks Davis 	if (kern_lognosys == 2 || kern_lognosys == 3 ||
425318f917a9SBrooks Davis 	    (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
42547ceeb35bSKonstantin Belousov 		printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
42557ceeb35bSKonstantin Belousov 		    td->td_sa.code);
42567ceeb35bSKonstantin Belousov 	}
4257f5216b9aSBruce Evans 	return (ENOSYS);
4258df8bae1dSRodney W. Grimes }
4259831d27a9SDon Lewis 
4260831d27a9SDon Lewis /*
42610c14ff0eSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using stored
42620c14ff0eSRobert Watson  * credentials rather than those of the current process.
4263831d27a9SDon Lewis  */
4264831d27a9SDon Lewis void
pgsigio(struct sigio ** sigiop,int sig,int checkctty)4265e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty)
4266831d27a9SDon Lewis {
4267a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
4268f1320723SAlfred Perlstein 	struct sigio *sigio;
4269831d27a9SDon Lewis 
4270a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
4271a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
4272a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
4273a3de221dSKonstantin Belousov 
4274f1320723SAlfred Perlstein 	SIGIO_LOCK();
4275f1320723SAlfred Perlstein 	sigio = *sigiop;
4276f1320723SAlfred Perlstein 	if (sigio == NULL) {
4277f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
4278f1320723SAlfred Perlstein 		return;
4279f1320723SAlfred Perlstein 	}
4280831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
4281628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
42822b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
42838451d0ddSKip Macy 			kern_psignal(sigio->sio_proc, sig);
4284628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
4285831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
4286831d27a9SDon Lewis 		struct proc *p;
4287831d27a9SDon Lewis 
4288f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
4289628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4290628d2653SJohn Baldwin 			PROC_LOCK(p);
4291e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
4292e806d352SJohn Baldwin 			    CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4293831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
42948451d0ddSKip Macy 				kern_psignal(p, sig);
4295628d2653SJohn Baldwin 			PROC_UNLOCK(p);
4296628d2653SJohn Baldwin 		}
4297f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
4298831d27a9SDon Lewis 	}
4299f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
4300831d27a9SDon Lewis }
4301cb679c38SJonathan Lemon 
4302cb679c38SJonathan Lemon static int
filt_sigattach(struct knote * kn)4303cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
4304cb679c38SJonathan Lemon {
4305cb679c38SJonathan Lemon 	struct proc *p = curproc;
4306cb679c38SJonathan Lemon 
4307cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
4308cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
4309cb679c38SJonathan Lemon 
43109e590ff0SKonstantin Belousov 	knlist_add(p->p_klist, kn, 0);
4311cb679c38SJonathan Lemon 
4312cb679c38SJonathan Lemon 	return (0);
4313cb679c38SJonathan Lemon }
4314cb679c38SJonathan Lemon 
4315cb679c38SJonathan Lemon static void
filt_sigdetach(struct knote * kn)4316cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
4317cb679c38SJonathan Lemon {
4318ed410b78SKonstantin Belousov 	knlist_remove(kn->kn_knlist, kn, 0);
4319cb679c38SJonathan Lemon }
4320cb679c38SJonathan Lemon 
4321cb679c38SJonathan Lemon /*
4322cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
4323cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
4324cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
4325cb679c38SJonathan Lemon  * isn't worth the trouble.
4326cb679c38SJonathan Lemon  */
4327cb679c38SJonathan Lemon static int
filt_signal(struct knote * kn,long hint)4328cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
4329cb679c38SJonathan Lemon {
4330cb679c38SJonathan Lemon 
4331cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
4332cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
4333cb679c38SJonathan Lemon 
4334cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
4335cb679c38SJonathan Lemon 			kn->kn_data++;
4336cb679c38SJonathan Lemon 	}
4337cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
4338cb679c38SJonathan Lemon }
433990af4afaSJohn Baldwin 
434090af4afaSJohn Baldwin struct sigacts *
sigacts_alloc(void)434190af4afaSJohn Baldwin sigacts_alloc(void)
434290af4afaSJohn Baldwin {
434390af4afaSJohn Baldwin 	struct sigacts *ps;
434490af4afaSJohn Baldwin 
434590af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4346ce8daaadSMateusz Guzik 	refcount_init(&ps->ps_refcnt, 1);
434790af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
434890af4afaSJohn Baldwin 	return (ps);
434990af4afaSJohn Baldwin }
435090af4afaSJohn Baldwin 
435190af4afaSJohn Baldwin void
sigacts_free(struct sigacts * ps)435290af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
435390af4afaSJohn Baldwin {
435490af4afaSJohn Baldwin 
4355c959c237SMateusz Guzik 	if (refcount_release(&ps->ps_refcnt) == 0)
4356c959c237SMateusz Guzik 		return;
435790af4afaSJohn Baldwin 	mtx_destroy(&ps->ps_mtx);
435890af4afaSJohn Baldwin 	free(ps, M_SUBPROC);
435990af4afaSJohn Baldwin }
436090af4afaSJohn Baldwin 
436190af4afaSJohn Baldwin struct sigacts *
sigacts_hold(struct sigacts * ps)436290af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
436390af4afaSJohn Baldwin {
4364c959c237SMateusz Guzik 
4365c959c237SMateusz Guzik 	refcount_acquire(&ps->ps_refcnt);
436690af4afaSJohn Baldwin 	return (ps);
436790af4afaSJohn Baldwin }
436890af4afaSJohn Baldwin 
436990af4afaSJohn Baldwin void
sigacts_copy(struct sigacts * dest,struct sigacts * src)437090af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
437190af4afaSJohn Baldwin {
437290af4afaSJohn Baldwin 
437390af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
437490af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
437590af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
437690af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
437790af4afaSJohn Baldwin }
437890af4afaSJohn Baldwin 
437990af4afaSJohn Baldwin int
sigacts_shared(struct sigacts * ps)438090af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
438190af4afaSJohn Baldwin {
438290af4afaSJohn Baldwin 
4383d00c8ea4SMateusz Guzik 	return (ps->ps_refcnt > 1);
438490af4afaSJohn Baldwin }
4385079c5b9eSKyle Evans 
4386079c5b9eSKyle Evans void
sig_drop_caught(struct proc * p)4387079c5b9eSKyle Evans sig_drop_caught(struct proc *p)
4388079c5b9eSKyle Evans {
4389079c5b9eSKyle Evans 	int sig;
4390079c5b9eSKyle Evans 	struct sigacts *ps;
4391079c5b9eSKyle Evans 
4392079c5b9eSKyle Evans 	ps = p->p_sigacts;
4393079c5b9eSKyle Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
4394079c5b9eSKyle Evans 	mtx_assert(&ps->ps_mtx, MA_OWNED);
439581f2e906SMark Johnston 	SIG_FOREACH(sig, &ps->ps_sigcatch) {
4396079c5b9eSKyle Evans 		sigdflt(ps, sig);
4397079c5b9eSKyle Evans 		if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4398079c5b9eSKyle Evans 			sigqueue_delete_proc(p, sig);
4399079c5b9eSKyle Evans 	}
4400079c5b9eSKyle Evans }
4401146fc63fSKonstantin Belousov 
4402a113b17fSKonstantin Belousov static void
sigfastblock_failed(struct thread * td,bool sendsig,bool write)4403a113b17fSKonstantin Belousov sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4404a113b17fSKonstantin Belousov {
4405a113b17fSKonstantin Belousov 	ksiginfo_t ksi;
4406a113b17fSKonstantin Belousov 
4407a113b17fSKonstantin Belousov 	/*
4408a113b17fSKonstantin Belousov 	 * Prevent further fetches and SIGSEGVs, allowing thread to
4409a113b17fSKonstantin Belousov 	 * issue syscalls despite corruption.
4410a113b17fSKonstantin Belousov 	 */
4411a113b17fSKonstantin Belousov 	sigfastblock_clear(td);
4412a113b17fSKonstantin Belousov 
4413a113b17fSKonstantin Belousov 	if (!sendsig)
4414a113b17fSKonstantin Belousov 		return;
4415a113b17fSKonstantin Belousov 	ksiginfo_init_trap(&ksi);
4416a113b17fSKonstantin Belousov 	ksi.ksi_signo = SIGSEGV;
4417a113b17fSKonstantin Belousov 	ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4418a113b17fSKonstantin Belousov 	ksi.ksi_addr = td->td_sigblock_ptr;
4419a113b17fSKonstantin Belousov 	trapsignal(td, &ksi);
4420a113b17fSKonstantin Belousov }
4421a113b17fSKonstantin Belousov 
4422a113b17fSKonstantin Belousov static bool
sigfastblock_fetch_sig(struct thread * td,bool sendsig,uint32_t * valp)4423a113b17fSKonstantin Belousov sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4424a113b17fSKonstantin Belousov {
4425a113b17fSKonstantin Belousov 	uint32_t res;
4426a113b17fSKonstantin Belousov 
4427a113b17fSKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4428a113b17fSKonstantin Belousov 		return (true);
4429a113b17fSKonstantin Belousov 	if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4430a113b17fSKonstantin Belousov 		sigfastblock_failed(td, sendsig, false);
4431a113b17fSKonstantin Belousov 		return (false);
4432a113b17fSKonstantin Belousov 	}
4433a113b17fSKonstantin Belousov 	*valp = res;
4434a113b17fSKonstantin Belousov 	td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4435a113b17fSKonstantin Belousov 	return (true);
4436a113b17fSKonstantin Belousov }
4437a113b17fSKonstantin Belousov 
4438fb3c434bSKonstantin Belousov static void
sigfastblock_resched(struct thread * td,bool resched)4439fb3c434bSKonstantin Belousov sigfastblock_resched(struct thread *td, bool resched)
4440fb3c434bSKonstantin Belousov {
4441fb3c434bSKonstantin Belousov 	struct proc *p;
4442fb3c434bSKonstantin Belousov 
4443fb3c434bSKonstantin Belousov 	if (resched) {
4444fb3c434bSKonstantin Belousov 		p = td->td_proc;
4445fb3c434bSKonstantin Belousov 		PROC_LOCK(p);
4446fb3c434bSKonstantin Belousov 		reschedule_signals(p, td->td_sigmask, 0);
4447fb3c434bSKonstantin Belousov 		PROC_UNLOCK(p);
4448fb3c434bSKonstantin Belousov 	}
4449c6d31b83SKonstantin Belousov 	ast_sched(td, TDA_SIG);
4450fb3c434bSKonstantin Belousov }
4451fb3c434bSKonstantin Belousov 
4452146fc63fSKonstantin Belousov int
sys_sigfastblock(struct thread * td,struct sigfastblock_args * uap)4453146fc63fSKonstantin Belousov sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4454146fc63fSKonstantin Belousov {
4455146fc63fSKonstantin Belousov 	struct proc *p;
4456146fc63fSKonstantin Belousov 	int error, res;
4457146fc63fSKonstantin Belousov 	uint32_t oldval;
4458146fc63fSKonstantin Belousov 
4459146fc63fSKonstantin Belousov 	error = 0;
4460a113b17fSKonstantin Belousov 	p = td->td_proc;
4461146fc63fSKonstantin Belousov 	switch (uap->cmd) {
4462146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_SETPTR:
4463146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4464146fc63fSKonstantin Belousov 			error = EBUSY;
4465146fc63fSKonstantin Belousov 			break;
4466146fc63fSKonstantin Belousov 		}
4467146fc63fSKonstantin Belousov 		if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4468146fc63fSKonstantin Belousov 			error = EINVAL;
4469146fc63fSKonstantin Belousov 			break;
4470146fc63fSKonstantin Belousov 		}
4471146fc63fSKonstantin Belousov 		td->td_pflags |= TDP_SIGFASTBLOCK;
4472146fc63fSKonstantin Belousov 		td->td_sigblock_ptr = uap->ptr;
4473146fc63fSKonstantin Belousov 		break;
4474146fc63fSKonstantin Belousov 
4475146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_UNBLOCK:
4476a113b17fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4477146fc63fSKonstantin Belousov 			error = EINVAL;
4478146fc63fSKonstantin Belousov 			break;
4479146fc63fSKonstantin Belousov 		}
4480a113b17fSKonstantin Belousov 
4481a113b17fSKonstantin Belousov 		for (;;) {
4482a113b17fSKonstantin Belousov 			res = casueword32(td->td_sigblock_ptr,
4483a113b17fSKonstantin Belousov 			    SIGFASTBLOCK_PEND, &oldval, 0);
4484146fc63fSKonstantin Belousov 			if (res == -1) {
4485146fc63fSKonstantin Belousov 				error = EFAULT;
4486a113b17fSKonstantin Belousov 				sigfastblock_failed(td, false, true);
4487146fc63fSKonstantin Belousov 				break;
4488146fc63fSKonstantin Belousov 			}
4489a113b17fSKonstantin Belousov 			if (res == 0)
4490a113b17fSKonstantin Belousov 				break;
4491a113b17fSKonstantin Belousov 			MPASS(res == 1);
4492146fc63fSKonstantin Belousov 			if (oldval != SIGFASTBLOCK_PEND) {
4493146fc63fSKonstantin Belousov 				error = EBUSY;
4494146fc63fSKonstantin Belousov 				break;
4495146fc63fSKonstantin Belousov 			}
4496146fc63fSKonstantin Belousov 			error = thread_check_susp(td, false);
4497146fc63fSKonstantin Belousov 			if (error != 0)
4498146fc63fSKonstantin Belousov 				break;
4499146fc63fSKonstantin Belousov 		}
4500a113b17fSKonstantin Belousov 		if (error != 0)
4501a113b17fSKonstantin Belousov 			break;
4502a113b17fSKonstantin Belousov 
4503a113b17fSKonstantin Belousov 		/*
4504a113b17fSKonstantin Belousov 		 * td_sigblock_val is cleared there, but not on a
4505a113b17fSKonstantin Belousov 		 * syscall exit.  The end effect is that a single
4506a113b17fSKonstantin Belousov 		 * interruptible sleep, while user sigblock word is
4507a113b17fSKonstantin Belousov 		 * set, might return EINTR or ERESTART to usermode
4508a113b17fSKonstantin Belousov 		 * without delivering signal.  All further sleeps,
4509a113b17fSKonstantin Belousov 		 * until userspace clears the word and does
4510a113b17fSKonstantin Belousov 		 * sigfastblock(UNBLOCK), observe current word and no
4511a113b17fSKonstantin Belousov 		 * longer get interrupted.  It is slight
4512a113b17fSKonstantin Belousov 		 * non-conformance, with alternative to have read the
4513a113b17fSKonstantin Belousov 		 * sigblock word on each syscall entry.
4514a113b17fSKonstantin Belousov 		 */
4515146fc63fSKonstantin Belousov 		td->td_sigblock_val = 0;
4516146fc63fSKonstantin Belousov 
4517146fc63fSKonstantin Belousov 		/*
4518146fc63fSKonstantin Belousov 		 * Rely on normal ast mechanism to deliver pending
4519146fc63fSKonstantin Belousov 		 * signals to current thread.  But notify others about
4520146fc63fSKonstantin Belousov 		 * fake unblock.
4521146fc63fSKonstantin Belousov 		 */
4522fb3c434bSKonstantin Belousov 		sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4523fb3c434bSKonstantin Belousov 
4524146fc63fSKonstantin Belousov 		break;
4525146fc63fSKonstantin Belousov 
4526146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_UNSETPTR:
4527146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4528146fc63fSKonstantin Belousov 			error = EINVAL;
4529146fc63fSKonstantin Belousov 			break;
4530146fc63fSKonstantin Belousov 		}
4531a113b17fSKonstantin Belousov 		if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4532146fc63fSKonstantin Belousov 			error = EFAULT;
4533146fc63fSKonstantin Belousov 			break;
4534146fc63fSKonstantin Belousov 		}
4535146fc63fSKonstantin Belousov 		if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4536146fc63fSKonstantin Belousov 			error = EBUSY;
4537146fc63fSKonstantin Belousov 			break;
4538146fc63fSKonstantin Belousov 		}
4539a113b17fSKonstantin Belousov 		sigfastblock_clear(td);
4540146fc63fSKonstantin Belousov 		break;
4541146fc63fSKonstantin Belousov 
4542146fc63fSKonstantin Belousov 	default:
4543146fc63fSKonstantin Belousov 		error = EINVAL;
4544146fc63fSKonstantin Belousov 		break;
4545146fc63fSKonstantin Belousov 	}
4546146fc63fSKonstantin Belousov 	return (error);
4547146fc63fSKonstantin Belousov }
4548146fc63fSKonstantin Belousov 
4549146fc63fSKonstantin Belousov void
sigfastblock_clear(struct thread * td)4550a113b17fSKonstantin Belousov sigfastblock_clear(struct thread *td)
4551146fc63fSKonstantin Belousov {
4552a113b17fSKonstantin Belousov 	bool resched;
4553146fc63fSKonstantin Belousov 
4554146fc63fSKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4555146fc63fSKonstantin Belousov 		return;
4556a113b17fSKonstantin Belousov 	td->td_sigblock_val = 0;
455700ebd809SKonstantin Belousov 	resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
455800ebd809SKonstantin Belousov 	    SIGPENDING(td);
4559a113b17fSKonstantin Belousov 	td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4560fb3c434bSKonstantin Belousov 	sigfastblock_resched(td, resched);
4561146fc63fSKonstantin Belousov }
4562146fc63fSKonstantin Belousov 
4563146fc63fSKonstantin Belousov void
sigfastblock_fetch(struct thread * td)4564a113b17fSKonstantin Belousov sigfastblock_fetch(struct thread *td)
4565146fc63fSKonstantin Belousov {
4566a113b17fSKonstantin Belousov 	uint32_t val;
4567146fc63fSKonstantin Belousov 
4568a113b17fSKonstantin Belousov 	(void)sigfastblock_fetch_sig(td, true, &val);
4569a113b17fSKonstantin Belousov }
4570146fc63fSKonstantin Belousov 
45710bc52b0bSKonstantin Belousov static void
sigfastblock_setpend1(struct thread * td)45720bc52b0bSKonstantin Belousov sigfastblock_setpend1(struct thread *td)
4573a113b17fSKonstantin Belousov {
4574a113b17fSKonstantin Belousov 	int res;
4575a113b17fSKonstantin Belousov 	uint32_t oldval;
4576a113b17fSKonstantin Belousov 
4577513320c0SKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4578a113b17fSKonstantin Belousov 		return;
4579a113b17fSKonstantin Belousov 	res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4580a113b17fSKonstantin Belousov 	if (res == -1) {
4581a113b17fSKonstantin Belousov 		sigfastblock_failed(td, true, false);
4582a113b17fSKonstantin Belousov 		return;
4583a113b17fSKonstantin Belousov 	}
4584a113b17fSKonstantin Belousov 	for (;;) {
4585a113b17fSKonstantin Belousov 		res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4586a113b17fSKonstantin Belousov 		    oldval | SIGFASTBLOCK_PEND);
4587a113b17fSKonstantin Belousov 		if (res == -1) {
4588a113b17fSKonstantin Belousov 			sigfastblock_failed(td, true, true);
4589a113b17fSKonstantin Belousov 			return;
4590a113b17fSKonstantin Belousov 		}
4591a113b17fSKonstantin Belousov 		if (res == 0) {
4592a113b17fSKonstantin Belousov 			td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4593a113b17fSKonstantin Belousov 			td->td_pflags &= ~TDP_SIGFASTPENDING;
4594a113b17fSKonstantin Belousov 			break;
4595a113b17fSKonstantin Belousov 		}
4596a113b17fSKonstantin Belousov 		MPASS(res == 1);
4597a113b17fSKonstantin Belousov 		if (thread_check_susp(td, false) != 0)
4598a113b17fSKonstantin Belousov 			break;
4599a113b17fSKonstantin Belousov 	}
4600146fc63fSKonstantin Belousov }
46010bc52b0bSKonstantin Belousov 
46024fced864SKonstantin Belousov static void
sigfastblock_setpend(struct thread * td,bool resched)46030bc52b0bSKonstantin Belousov sigfastblock_setpend(struct thread *td, bool resched)
46040bc52b0bSKonstantin Belousov {
46050bc52b0bSKonstantin Belousov 	struct proc *p;
46060bc52b0bSKonstantin Belousov 
46070bc52b0bSKonstantin Belousov 	sigfastblock_setpend1(td);
46080bc52b0bSKonstantin Belousov 	if (resched) {
46090bc52b0bSKonstantin Belousov 		p = td->td_proc;
46100bc52b0bSKonstantin Belousov 		PROC_LOCK(p);
46110bc52b0bSKonstantin Belousov 		reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);
46120bc52b0bSKonstantin Belousov 		PROC_UNLOCK(p);
46130bc52b0bSKonstantin Belousov 	}
46140bc52b0bSKonstantin Belousov }
4615