xref: /freebsd/sys/kern/kern_sig.c (revision 9b86d3e5de1997ed8d3153d4530bcb981e6602b9)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
7df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
8df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
9df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2069a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39677b542eSDavid E. O'Brien #include <sys/cdefs.h>
40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
41677b542eSDavid E. O'Brien 
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45eb6368d4SRui Paulo #include <sys/ctype.h>
4636240ea5SDoug Rabson #include <sys/systm.h>
47df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
49df8bae1dSRodney W. Grimes #include <sys/acct.h>
504a144410SRobert Watson #include <sys/capsicum.h>
5178f57a9cSMark Johnston #include <sys/compressor.h>
52238510fcSJason Evans #include <sys/condvar.h>
53773e541eSWarner Losh #include <sys/devctl.h>
54854dc8c2SJohn Baldwin #include <sys/event.h>
55854dc8c2SJohn Baldwin #include <sys/fcntl.h>
56e7228204SAlfred Perlstein #include <sys/imgact.h>
57854dc8c2SJohn Baldwin #include <sys/kernel.h>
580384fff8SJason Evans #include <sys/ktr.h>
59df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
6091898857SMark Johnston #include <sys/limits.h>
61854dc8c2SJohn Baldwin #include <sys/lock.h>
62854dc8c2SJohn Baldwin #include <sys/malloc.h>
63854dc8c2SJohn Baldwin #include <sys/mutex.h>
64c959c237SMateusz Guzik #include <sys/refcount.h>
65854dc8c2SJohn Baldwin #include <sys/namei.h>
66854dc8c2SJohn Baldwin #include <sys/proc.h>
67cfb5f768SJonathan Anderson #include <sys/procdesc.h>
68ef401a85SKonstantin Belousov #include <sys/ptrace.h>
696aeb05d7STom Rhodes #include <sys/posix4.h>
70b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h>
71c31146a1SJohn Baldwin #include <sys/resourcevar.h>
725d217f17SJohn Birrell #include <sys/sdt.h>
7336b208e0SRobert Watson #include <sys/sbuf.h>
7444f3b092SJohn Baldwin #include <sys/sleepqueue.h>
756caa8a15SJohn Baldwin #include <sys/smp.h>
76df8bae1dSRodney W. Grimes #include <sys/stat.h>
771005a129SJohn Baldwin #include <sys/sx.h>
788f19eb88SIan Dowse #include <sys/syscallsubr.h>
79c87e2930SDavid Greenman #include <sys/sysctl.h>
80854dc8c2SJohn Baldwin #include <sys/sysent.h>
81854dc8c2SJohn Baldwin #include <sys/syslog.h>
82854dc8c2SJohn Baldwin #include <sys/sysproto.h>
8356c06c4bSDavid Xu #include <sys/timers.h>
8406ae1e91SMatthew Dillon #include <sys/unistd.h>
85854dc8c2SJohn Baldwin #include <sys/wait.h>
869104847fSDavid Xu #include <vm/vm.h>
879104847fSDavid Xu #include <vm/vm_extern.h>
889104847fSDavid Xu #include <vm/uma.h>
89df8bae1dSRodney W. Grimes 
90e7228204SAlfred Perlstein #include <sys/jail.h>
91e7228204SAlfred Perlstein 
92df8bae1dSRodney W. Grimes #include <machine/cpu.h>
93df8bae1dSRodney W. Grimes 
94bfd7575aSWayne Salamon #include <security/audit/audit.h>
95bfd7575aSWayne Salamon 
966f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
976f841fb7SMarcel Moolenaar 
985d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
9936160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__send,
10036160958SMark Johnston     "struct thread *", "struct proc *", "int");
10136160958SMark Johnston SDT_PROBE_DEFINE2(proc, , , signal__clear,
10236160958SMark Johnston     "int", "ksiginfo_t *");
10336160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__discard,
1047b77e1feSMark Johnston     "struct thread *", "struct proc *", "int");
1055d217f17SJohn Birrell 
1064d77a549SAlfred Perlstein static int	coredump(struct thread *);
107a3de221dSKonstantin Belousov static int	killpg1(struct thread *td, int sig, int pgid, int all,
108a3de221dSKonstantin Belousov 		    ksiginfo_t *ksi);
1093cf3b9f0SJohn Baldwin static int	issignal(struct thread *td);
1100bc52b0bSKonstantin Belousov static void	reschedule_signals(struct proc *p, sigset_t block, int flags);
1114d77a549SAlfred Perlstein static int	sigprop(int sig);
11294f0972bSDavid Xu static void	tdsigwakeup(struct thread *, int, sig_t, int);
1136f56cb8dSKonstantin Belousov static int	sig_suspend_threads(struct thread *, struct proc *, int);
114cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
115cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
116cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
117146fc63fSKonstantin Belousov static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
1189104847fSDavid Xu static void	sigqueue_start(void);
119cb679c38SJonathan Lemon 
1209104847fSDavid Xu static uma_zone_t	ksiginfo_zone = NULL;
121e76d823bSRobert Watson struct filterops sig_filtops = {
122e76d823bSRobert Watson 	.f_isfd = 0,
123e76d823bSRobert Watson 	.f_attach = filt_sigattach,
124e76d823bSRobert Watson 	.f_detach = filt_sigdetach,
125e76d823bSRobert Watson 	.f_event = filt_signal,
126e76d823bSRobert Watson };
127cb679c38SJonathan Lemon 
128fc8cca02SJohn Baldwin static int	kern_logsigexit = 1;
1293d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1303d177f46SBill Fumerola     &kern_logsigexit, 0,
1313d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
13257308494SJoerg Wunsch 
133f71a882fSDavid Xu static int	kern_forcesigexit = 1;
134f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
135f71a882fSDavid Xu     &kern_forcesigexit, 0, "Force trap signal to be handled");
136f71a882fSDavid Xu 
1377029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1386472ac3dSEd Schouten     "POSIX real time signal");
1399104847fSDavid Xu 
1409104847fSDavid Xu static int	max_pending_per_proc = 128;
1419104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
1429104847fSDavid Xu     &max_pending_per_proc, 0, "Max pending signals per proc");
1439104847fSDavid Xu 
1449104847fSDavid Xu static int	preallocate_siginfo = 1024;
145af3b2549SHans Petter Selasky SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
1469104847fSDavid Xu     &preallocate_siginfo, 0, "Preallocated signal memory size");
1479104847fSDavid Xu 
1489104847fSDavid Xu static int	signal_overflow = 0;
149761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
1509104847fSDavid Xu     &signal_overflow, 0, "Number of signals overflew");
1519104847fSDavid Xu 
1529104847fSDavid Xu static int	signal_alloc_fail = 0;
153761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
1549104847fSDavid Xu     &signal_alloc_fail, 0, "signals failed to be allocated");
1559104847fSDavid Xu 
156f5a077c3SKonstantin Belousov static int	kern_lognosys = 0;
157f5a077c3SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
158f5a077c3SKonstantin Belousov     "Log invalid syscalls");
159f5a077c3SKonstantin Belousov 
160a113b17fSKonstantin Belousov __read_frequently bool sigfastblock_fetch_always = false;
161a113b17fSKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
162a113b17fSKonstantin Belousov     &sigfastblock_fetch_always, 0,
163a113b17fSKonstantin Belousov     "Fetch sigfastblock word on each syscall entry for proper "
164a113b17fSKonstantin Belousov     "blocking semantic");
165a113b17fSKonstantin Belousov 
166bc387624SKonstantin Belousov static bool	kern_sig_discard_ign = true;
167bc387624SKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
168bc387624SKonstantin Belousov     &kern_sig_discard_ign, 0,
169bc387624SKonstantin Belousov     "Discard ignored signals on delivery, otherwise queue them to "
170bc387624SKonstantin Belousov     "the target queue");
171bc387624SKonstantin Belousov 
1729104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
1739104847fSDavid Xu 
1742b87b6d4SRobert Watson /*
1752b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1762b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1772b87b6d4SRobert Watson  * in the right situations.
1782b87b6d4SRobert Watson  */
1792b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1802b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1812b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1822b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1832b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1842b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1852b87b6d4SRobert Watson 
186fc8cca02SJohn Baldwin static int	sugid_coredump;
187af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
1888b5adf9dSJoseph Koshy     &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
189c87e2930SDavid Greenman 
190b0c9d4d7SPawel Jakub Dawidek static int	capmode_coredump;
191af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
192b0c9d4d7SPawel Jakub Dawidek     &capmode_coredump, 0, "Allow processes in capability mode to dump core");
193b0c9d4d7SPawel Jakub Dawidek 
194e5a28db9SPaul Saab static int	do_coredump = 1;
195e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
196e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
197e5a28db9SPaul Saab 
1986141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
1996141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
2006141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
2016141e04aSJohn-Mark Gurney 
2020da9e11bSRui Paulo static int	coredump_devctl = 0;
203eb6368d4SRui Paulo SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
204eb6368d4SRui Paulo 	0, "Generate a devctl notification when processes coredump");
205eb6368d4SRui Paulo 
2062c42a146SMarcel Moolenaar /*
2072c42a146SMarcel Moolenaar  * Signal properties and actions.
2082c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
2092c42a146SMarcel Moolenaar  * according to the following properties:
2102c42a146SMarcel Moolenaar  */
211fd50a707SBrooks Davis #define	SIGPROP_KILL		0x01	/* terminates process by default */
212fd50a707SBrooks Davis #define	SIGPROP_CORE		0x02	/* ditto and coredumps */
213fd50a707SBrooks Davis #define	SIGPROP_STOP		0x04	/* suspend process */
214fd50a707SBrooks Davis #define	SIGPROP_TTYSTOP		0x08	/* ditto, from tty */
215fd50a707SBrooks Davis #define	SIGPROP_IGNORE		0x10	/* ignore by default */
216fd50a707SBrooks Davis #define	SIGPROP_CONT		0x20	/* continue if suspended */
217fd50a707SBrooks Davis #define	SIGPROP_CANTMASK	0x40	/* non-maskable, catchable */
218df8bae1dSRodney W. Grimes 
2192c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
220ed6d876bSBrooks Davis 	[SIGHUP] =	SIGPROP_KILL,
221ed6d876bSBrooks Davis 	[SIGINT] =	SIGPROP_KILL,
222ed6d876bSBrooks Davis 	[SIGQUIT] =	SIGPROP_KILL | SIGPROP_CORE,
223ed6d876bSBrooks Davis 	[SIGILL] =	SIGPROP_KILL | SIGPROP_CORE,
224ed6d876bSBrooks Davis 	[SIGTRAP] =	SIGPROP_KILL | SIGPROP_CORE,
225ed6d876bSBrooks Davis 	[SIGABRT] =	SIGPROP_KILL | SIGPROP_CORE,
226ed6d876bSBrooks Davis 	[SIGEMT] =	SIGPROP_KILL | SIGPROP_CORE,
227ed6d876bSBrooks Davis 	[SIGFPE] =	SIGPROP_KILL | SIGPROP_CORE,
228ed6d876bSBrooks Davis 	[SIGKILL] =	SIGPROP_KILL,
229ed6d876bSBrooks Davis 	[SIGBUS] =	SIGPROP_KILL | SIGPROP_CORE,
230ed6d876bSBrooks Davis 	[SIGSEGV] =	SIGPROP_KILL | SIGPROP_CORE,
231ed6d876bSBrooks Davis 	[SIGSYS] =	SIGPROP_KILL | SIGPROP_CORE,
232ed6d876bSBrooks Davis 	[SIGPIPE] =	SIGPROP_KILL,
233ed6d876bSBrooks Davis 	[SIGALRM] =	SIGPROP_KILL,
234ed6d876bSBrooks Davis 	[SIGTERM] =	SIGPROP_KILL,
235ed6d876bSBrooks Davis 	[SIGURG] =	SIGPROP_IGNORE,
236ed6d876bSBrooks Davis 	[SIGSTOP] =	SIGPROP_STOP,
237ed6d876bSBrooks Davis 	[SIGTSTP] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
238ed6d876bSBrooks Davis 	[SIGCONT] =	SIGPROP_IGNORE | SIGPROP_CONT,
239ed6d876bSBrooks Davis 	[SIGCHLD] =	SIGPROP_IGNORE,
240ed6d876bSBrooks Davis 	[SIGTTIN] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
241ed6d876bSBrooks Davis 	[SIGTTOU] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
242ed6d876bSBrooks Davis 	[SIGIO] =	SIGPROP_IGNORE,
243ed6d876bSBrooks Davis 	[SIGXCPU] =	SIGPROP_KILL,
244ed6d876bSBrooks Davis 	[SIGXFSZ] =	SIGPROP_KILL,
245ed6d876bSBrooks Davis 	[SIGVTALRM] =	SIGPROP_KILL,
246ed6d876bSBrooks Davis 	[SIGPROF] =	SIGPROP_KILL,
247ed6d876bSBrooks Davis 	[SIGWINCH] =	SIGPROP_IGNORE,
248ed6d876bSBrooks Davis 	[SIGINFO] =	SIGPROP_IGNORE,
249ed6d876bSBrooks Davis 	[SIGUSR1] =	SIGPROP_KILL,
250ed6d876bSBrooks Davis 	[SIGUSR2] =	SIGPROP_KILL,
2512c42a146SMarcel Moolenaar };
2522c42a146SMarcel Moolenaar 
253146fc63fSKonstantin Belousov sigset_t fastblock_mask;
2546b286ee8SKonstantin Belousov 
2559104847fSDavid Xu static void
2569104847fSDavid Xu sigqueue_start(void)
2579104847fSDavid Xu {
2589104847fSDavid Xu 	ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
2599104847fSDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2609104847fSDavid Xu 	uma_prealloc(ksiginfo_zone, preallocate_siginfo);
261b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
262b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
263b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
264146fc63fSKonstantin Belousov 	SIGFILLSET(fastblock_mask);
265146fc63fSKonstantin Belousov 	SIG_CANTMASK(fastblock_mask);
2669104847fSDavid Xu }
2679104847fSDavid Xu 
2685da49fcbSDavid Xu ksiginfo_t *
269ebceaf6dSDavid Xu ksiginfo_alloc(int wait)
2709104847fSDavid Xu {
271ebceaf6dSDavid Xu 	int flags;
272ebceaf6dSDavid Xu 
273ebceaf6dSDavid Xu 	flags = M_ZERO;
274ebceaf6dSDavid Xu 	if (! wait)
275ebceaf6dSDavid Xu 		flags |= M_NOWAIT;
2769104847fSDavid Xu 	if (ksiginfo_zone != NULL)
277ebceaf6dSDavid Xu 		return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
2789104847fSDavid Xu 	return (NULL);
2799104847fSDavid Xu }
2809104847fSDavid Xu 
2815da49fcbSDavid Xu void
2829104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi)
2839104847fSDavid Xu {
2849104847fSDavid Xu 	uma_zfree(ksiginfo_zone, ksi);
2859104847fSDavid Xu }
2869104847fSDavid Xu 
2875da49fcbSDavid Xu static __inline int
2885da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi)
2895da49fcbSDavid Xu {
2905da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT)) {
2915da49fcbSDavid Xu 		uma_zfree(ksiginfo_zone, ksi);
2925da49fcbSDavid Xu 		return (1);
2935da49fcbSDavid Xu 	}
2945da49fcbSDavid Xu 	return (0);
2955da49fcbSDavid Xu }
2965da49fcbSDavid Xu 
2979104847fSDavid Xu void
2989104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p)
2999104847fSDavid Xu {
3009104847fSDavid Xu 	SIGEMPTYSET(list->sq_signals);
3013dfcaad6SDavid Xu 	SIGEMPTYSET(list->sq_kill);
30282a4538fSEric Badger 	SIGEMPTYSET(list->sq_ptrace);
3039104847fSDavid Xu 	TAILQ_INIT(&list->sq_list);
3049104847fSDavid Xu 	list->sq_proc = p;
3059104847fSDavid Xu 	list->sq_flags = SQ_INIT;
3069104847fSDavid Xu }
3079104847fSDavid Xu 
3089104847fSDavid Xu /*
3099104847fSDavid Xu  * Get a signal's ksiginfo.
3109104847fSDavid Xu  * Return:
3119104847fSDavid Xu  *	0	-	signal not found
3129104847fSDavid Xu  *	others	-	signal number
3139104847fSDavid Xu  */
314a5799a4fSKonstantin Belousov static int
3159104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
3169104847fSDavid Xu {
3179104847fSDavid Xu 	struct proc *p = sq->sq_proc;
3189104847fSDavid Xu 	struct ksiginfo *ksi, *next;
3199104847fSDavid Xu 	int count = 0;
3209104847fSDavid Xu 
3219104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
3229104847fSDavid Xu 
3239104847fSDavid Xu 	if (!SIGISMEMBER(sq->sq_signals, signo))
3249104847fSDavid Xu 		return (0);
3259104847fSDavid Xu 
32682a4538fSEric Badger 	if (SIGISMEMBER(sq->sq_ptrace, signo)) {
32782a4538fSEric Badger 		count++;
32882a4538fSEric Badger 		SIGDELSET(sq->sq_ptrace, signo);
32982a4538fSEric Badger 		si->ksi_flags |= KSI_PTRACE;
33082a4538fSEric Badger 	}
3313dfcaad6SDavid Xu 	if (SIGISMEMBER(sq->sq_kill, signo)) {
3323dfcaad6SDavid Xu 		count++;
33382a4538fSEric Badger 		if (count == 1)
3343dfcaad6SDavid Xu 			SIGDELSET(sq->sq_kill, signo);
3353dfcaad6SDavid Xu 	}
3363dfcaad6SDavid Xu 
3375c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
3389104847fSDavid Xu 		if (ksi->ksi_signo == signo) {
3399104847fSDavid Xu 			if (count == 0) {
3409104847fSDavid Xu 				TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
3415da49fcbSDavid Xu 				ksi->ksi_sigq = NULL;
3429104847fSDavid Xu 				ksiginfo_copy(ksi, si);
3435da49fcbSDavid Xu 				if (ksiginfo_tryfree(ksi) && p != NULL)
3449104847fSDavid Xu 					p->p_pendingcnt--;
3459104847fSDavid Xu 			}
346016fa302SDavid Xu 			if (++count > 1)
347016fa302SDavid Xu 				break;
3489104847fSDavid Xu 		}
3499104847fSDavid Xu 	}
3509104847fSDavid Xu 
3519104847fSDavid Xu 	if (count <= 1)
3529104847fSDavid Xu 		SIGDELSET(sq->sq_signals, signo);
3539104847fSDavid Xu 	si->ksi_signo = signo;
3549104847fSDavid Xu 	return (signo);
3559104847fSDavid Xu }
3569104847fSDavid Xu 
3575da49fcbSDavid Xu void
3585da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi)
3595da49fcbSDavid Xu {
3605da49fcbSDavid Xu 	struct ksiginfo *kp;
3615da49fcbSDavid Xu 	struct proc	*p;
3625da49fcbSDavid Xu 	sigqueue_t	*sq;
3635da49fcbSDavid Xu 
364ebceaf6dSDavid Xu 	if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
3655da49fcbSDavid Xu 		return;
3665da49fcbSDavid Xu 
3675da49fcbSDavid Xu 	p = sq->sq_proc;
3685da49fcbSDavid Xu 	TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
3695da49fcbSDavid Xu 	ksi->ksi_sigq = NULL;
3705da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
3715da49fcbSDavid Xu 		p->p_pendingcnt--;
3725da49fcbSDavid Xu 
3735da49fcbSDavid Xu 	for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
3745da49fcbSDavid Xu 	     kp = TAILQ_NEXT(kp, ksi_link)) {
3755da49fcbSDavid Xu 		if (kp->ksi_signo == ksi->ksi_signo)
3765da49fcbSDavid Xu 			break;
3775da49fcbSDavid Xu 	}
37882a4538fSEric Badger 	if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
37982a4538fSEric Badger 	    !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
3805da49fcbSDavid Xu 		SIGDELSET(sq->sq_signals, ksi->ksi_signo);
3815da49fcbSDavid Xu }
3825da49fcbSDavid Xu 
383a5799a4fSKonstantin Belousov static int
3849104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
3859104847fSDavid Xu {
3869104847fSDavid Xu 	struct proc *p = sq->sq_proc;
3879104847fSDavid Xu 	struct ksiginfo *ksi;
3889104847fSDavid Xu 	int ret = 0;
3899104847fSDavid Xu 
3909104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
3919104847fSDavid Xu 
39282a4538fSEric Badger 	/*
39382a4538fSEric Badger 	 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
39482a4538fSEric Badger 	 * for these signals.
39582a4538fSEric Badger 	 */
3963dfcaad6SDavid Xu 	if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
3973dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
3989104847fSDavid Xu 		goto out_set_bit;
3993dfcaad6SDavid Xu 	}
4009104847fSDavid Xu 
4015da49fcbSDavid Xu 	/* directly insert the ksi, don't copy it */
4025da49fcbSDavid Xu 	if (si->ksi_flags & KSI_INS) {
4036a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
4046a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
4056a671a6bSKonstantin Belousov 		else
4065da49fcbSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
4075da49fcbSDavid Xu 		si->ksi_sigq = sq;
4085da49fcbSDavid Xu 		goto out_set_bit;
4095da49fcbSDavid Xu 	}
4105da49fcbSDavid Xu 
4113dfcaad6SDavid Xu 	if (__predict_false(ksiginfo_zone == NULL)) {
4123dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
4139104847fSDavid Xu 		goto out_set_bit;
4143dfcaad6SDavid Xu 	}
4159104847fSDavid Xu 
416ebceaf6dSDavid Xu 	if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
4179104847fSDavid Xu 		signal_overflow++;
4189104847fSDavid Xu 		ret = EAGAIN;
419ebceaf6dSDavid Xu 	} else if ((ksi = ksiginfo_alloc(0)) == NULL) {
4209104847fSDavid Xu 		signal_alloc_fail++;
4219104847fSDavid Xu 		ret = EAGAIN;
4229104847fSDavid Xu 	} else {
4239104847fSDavid Xu 		if (p != NULL)
4249104847fSDavid Xu 			p->p_pendingcnt++;
4259104847fSDavid Xu 		ksiginfo_copy(si, ksi);
4269104847fSDavid Xu 		ksi->ksi_signo = signo;
4276a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
4286a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
4296a671a6bSKonstantin Belousov 		else
4309104847fSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
4315da49fcbSDavid Xu 		ksi->ksi_sigq = sq;
4329104847fSDavid Xu 	}
4339104847fSDavid Xu 
43482a4538fSEric Badger 	if (ret != 0) {
43582a4538fSEric Badger 		if ((si->ksi_flags & KSI_PTRACE) != 0) {
43682a4538fSEric Badger 			SIGADDSET(sq->sq_ptrace, signo);
43782a4538fSEric Badger 			ret = 0;
43882a4538fSEric Badger 			goto out_set_bit;
43982a4538fSEric Badger 		} else if ((si->ksi_flags & KSI_TRAP) != 0 ||
440a3de221dSKonstantin Belousov 		    (si->ksi_flags & KSI_SIGQ) == 0) {
4413dfcaad6SDavid Xu 			SIGADDSET(sq->sq_kill, signo);
4429104847fSDavid Xu 			ret = 0;
4439104847fSDavid Xu 			goto out_set_bit;
4449104847fSDavid Xu 		}
4459104847fSDavid Xu 		return (ret);
44682a4538fSEric Badger 	}
4479104847fSDavid Xu 
4489104847fSDavid Xu out_set_bit:
4499104847fSDavid Xu 	SIGADDSET(sq->sq_signals, signo);
4509104847fSDavid Xu 	return (ret);
4519104847fSDavid Xu }
4529104847fSDavid Xu 
4539104847fSDavid Xu void
4549104847fSDavid Xu sigqueue_flush(sigqueue_t *sq)
4559104847fSDavid Xu {
4569104847fSDavid Xu 	struct proc *p = sq->sq_proc;
4579104847fSDavid Xu 	ksiginfo_t *ksi;
4589104847fSDavid Xu 
4599104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
4609104847fSDavid Xu 
4615da49fcbSDavid Xu 	if (p != NULL)
4625da49fcbSDavid Xu 		PROC_LOCK_ASSERT(p, MA_OWNED);
4635da49fcbSDavid Xu 
4649104847fSDavid Xu 	while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
4659104847fSDavid Xu 		TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4665da49fcbSDavid Xu 		ksi->ksi_sigq = NULL;
4675da49fcbSDavid Xu 		if (ksiginfo_tryfree(ksi) && p != NULL)
4689104847fSDavid Xu 			p->p_pendingcnt--;
4699104847fSDavid Xu 	}
4709104847fSDavid Xu 
4719104847fSDavid Xu 	SIGEMPTYSET(sq->sq_signals);
4723dfcaad6SDavid Xu 	SIGEMPTYSET(sq->sq_kill);
47382a4538fSEric Badger 	SIGEMPTYSET(sq->sq_ptrace);
4749104847fSDavid Xu }
4759104847fSDavid Xu 
476a5799a4fSKonstantin Belousov static void
477fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
4789104847fSDavid Xu {
479fc4ecc1dSDavid Xu 	sigset_t tmp;
4809104847fSDavid Xu 	struct proc *p1, *p2;
4819104847fSDavid Xu 	ksiginfo_t *ksi, *next;
4829104847fSDavid Xu 
4839104847fSDavid Xu 	KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
4849104847fSDavid Xu 	KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
4859104847fSDavid Xu 	p1 = src->sq_proc;
4869104847fSDavid Xu 	p2 = dst->sq_proc;
4879104847fSDavid Xu 	/* Move siginfo to target list */
4885c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
489fc4ecc1dSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
4909104847fSDavid Xu 			TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
4919104847fSDavid Xu 			if (p1 != NULL)
4929104847fSDavid Xu 				p1->p_pendingcnt--;
4939104847fSDavid Xu 			TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
4945da49fcbSDavid Xu 			ksi->ksi_sigq = dst;
4959104847fSDavid Xu 			if (p2 != NULL)
4969104847fSDavid Xu 				p2->p_pendingcnt++;
4979104847fSDavid Xu 		}
4989104847fSDavid Xu 	}
4999104847fSDavid Xu 
5009104847fSDavid Xu 	/* Move pending bits to target list */
5013dfcaad6SDavid Xu 	tmp = src->sq_kill;
502fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
5033dfcaad6SDavid Xu 	SIGSETOR(dst->sq_kill, tmp);
5043dfcaad6SDavid Xu 	SIGSETNAND(src->sq_kill, tmp);
5053dfcaad6SDavid Xu 
50682a4538fSEric Badger 	tmp = src->sq_ptrace;
50782a4538fSEric Badger 	SIGSETAND(tmp, *set);
50882a4538fSEric Badger 	SIGSETOR(dst->sq_ptrace, tmp);
50982a4538fSEric Badger 	SIGSETNAND(src->sq_ptrace, tmp);
51082a4538fSEric Badger 
5119104847fSDavid Xu 	tmp = src->sq_signals;
512fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
5139104847fSDavid Xu 	SIGSETOR(dst->sq_signals, tmp);
5149104847fSDavid Xu 	SIGSETNAND(src->sq_signals, tmp);
5159104847fSDavid Xu }
5169104847fSDavid Xu 
517407af02bSDavid Xu #if 0
518a5799a4fSKonstantin Belousov static void
5199104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
5209104847fSDavid Xu {
5219104847fSDavid Xu 	sigset_t set;
5229104847fSDavid Xu 
5239104847fSDavid Xu 	SIGEMPTYSET(set);
5249104847fSDavid Xu 	SIGADDSET(set, signo);
5259104847fSDavid Xu 	sigqueue_move_set(src, dst, &set);
5269104847fSDavid Xu }
527407af02bSDavid Xu #endif
5289104847fSDavid Xu 
529a5799a4fSKonstantin Belousov static void
530fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
5319104847fSDavid Xu {
5329104847fSDavid Xu 	struct proc *p = sq->sq_proc;
5339104847fSDavid Xu 	ksiginfo_t *ksi, *next;
5349104847fSDavid Xu 
5359104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
5369104847fSDavid Xu 
5379104847fSDavid Xu 	/* Remove siginfo queue */
5385c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
5399104847fSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
5409104847fSDavid Xu 			TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
5415da49fcbSDavid Xu 			ksi->ksi_sigq = NULL;
5425da49fcbSDavid Xu 			if (ksiginfo_tryfree(ksi) && p != NULL)
5439104847fSDavid Xu 				p->p_pendingcnt--;
5449104847fSDavid Xu 		}
5459104847fSDavid Xu 	}
5463dfcaad6SDavid Xu 	SIGSETNAND(sq->sq_kill, *set);
54782a4538fSEric Badger 	SIGSETNAND(sq->sq_ptrace, *set);
5489104847fSDavid Xu 	SIGSETNAND(sq->sq_signals, *set);
5499104847fSDavid Xu }
5509104847fSDavid Xu 
5519104847fSDavid Xu void
5529104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo)
5539104847fSDavid Xu {
5549104847fSDavid Xu 	sigset_t set;
5559104847fSDavid Xu 
5569104847fSDavid Xu 	SIGEMPTYSET(set);
5579104847fSDavid Xu 	SIGADDSET(set, signo);
5589104847fSDavid Xu 	sigqueue_delete_set(sq, &set);
5599104847fSDavid Xu }
5609104847fSDavid Xu 
5619104847fSDavid Xu /* Remove a set of signals for a process */
562a5799a4fSKonstantin Belousov static void
563fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
5649104847fSDavid Xu {
5659104847fSDavid Xu 	sigqueue_t worklist;
5669104847fSDavid Xu 	struct thread *td0;
5679104847fSDavid Xu 
5689104847fSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
5699104847fSDavid Xu 
5709104847fSDavid Xu 	sigqueue_init(&worklist, NULL);
5719104847fSDavid Xu 	sigqueue_move_set(&p->p_sigqueue, &worklist, set);
5729104847fSDavid Xu 
5739104847fSDavid Xu 	FOREACH_THREAD_IN_PROC(p, td0)
5749104847fSDavid Xu 		sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
5759104847fSDavid Xu 
5769104847fSDavid Xu 	sigqueue_flush(&worklist);
5779104847fSDavid Xu }
5789104847fSDavid Xu 
5799104847fSDavid Xu void
5809104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo)
5819104847fSDavid Xu {
5829104847fSDavid Xu 	sigset_t set;
5839104847fSDavid Xu 
5849104847fSDavid Xu 	SIGEMPTYSET(set);
5859104847fSDavid Xu 	SIGADDSET(set, signo);
5869104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
5879104847fSDavid Xu }
5889104847fSDavid Xu 
589a5799a4fSKonstantin Belousov static void
5909104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p)
5919104847fSDavid Xu {
5929104847fSDavid Xu 	sigset_t set;
5939104847fSDavid Xu 
5949104847fSDavid Xu 	SIGEMPTYSET(set);
5959104847fSDavid Xu 	SIGADDSET(set, SIGSTOP);
5969104847fSDavid Xu 	SIGADDSET(set, SIGTSTP);
5979104847fSDavid Xu 	SIGADDSET(set, SIGTTIN);
5989104847fSDavid Xu 	SIGADDSET(set, SIGTTOU);
5999104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
6009104847fSDavid Xu }
6019104847fSDavid Xu 
602fbbeeb6cSBruce Evans /*
6031968f37bSJohn Baldwin  * Determine signal that should be delivered to thread td, the current
6041968f37bSJohn Baldwin  * thread, 0 if none.  If there is a pending stop signal with default
605fbbeeb6cSBruce Evans  * action, the process stops in issignal().
606fbbeeb6cSBruce Evans  */
607fbbeeb6cSBruce Evans int
6083cf3b9f0SJohn Baldwin cursig(struct thread *td)
609fbbeeb6cSBruce Evans {
610c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
61190af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
612a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
6133cf3b9f0SJohn Baldwin 	return (SIGPENDING(td) ? issignal(td) : 0);
614fbbeeb6cSBruce Evans }
615fbbeeb6cSBruce Evans 
61679065dbaSBruce Evans /*
61779065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
6189104847fSDavid Xu  * mode.  This must be called whenever a signal is added to td_sigqueue or
6194093529dSJeff Roberson  * unmasked in td_sigmask.
62079065dbaSBruce Evans  */
62179065dbaSBruce Evans void
6224093529dSJeff Roberson signotify(struct thread *td)
62379065dbaSBruce Evans {
6244093529dSJeff Roberson 
625ddd4d15eSMatt Macy 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
6264093529dSJeff Roberson 
6278b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
628a54e85fdSJeff Roberson 		thread_lock(td);
6294093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
630a54e85fdSJeff Roberson 		thread_unlock(td);
63179065dbaSBruce Evans 	}
6328b94a061SJohn Baldwin }
6338b94a061SJohn Baldwin 
634affd9185SKonstantin Belousov /*
635affd9185SKonstantin Belousov  * Returns 1 (true) if altstack is configured for the thread, and the
636affd9185SKonstantin Belousov  * passed stack bottom address falls into the altstack range.  Handles
637affd9185SKonstantin Belousov  * the 43 compat special case where the alt stack size is zero.
638affd9185SKonstantin Belousov  */
6398b94a061SJohn Baldwin int
6408b94a061SJohn Baldwin sigonstack(size_t sp)
6418b94a061SJohn Baldwin {
642affd9185SKonstantin Belousov 	struct thread *td;
6438b94a061SJohn Baldwin 
644affd9185SKonstantin Belousov 	td = curthread;
645affd9185SKonstantin Belousov 	if ((td->td_pflags & TDP_ALTSTACK) == 0)
646affd9185SKonstantin Belousov 		return (0);
6471930e303SPoul-Henning Kamp #if defined(COMPAT_43)
6487e097daaSKonstantin Belousov 	if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
649affd9185SKonstantin Belousov 		return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
6508b94a061SJohn Baldwin #endif
651affd9185SKonstantin Belousov 	return (sp >= (size_t)td->td_sigstk.ss_sp &&
652affd9185SKonstantin Belousov 	    sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
6538b94a061SJohn Baldwin }
65479065dbaSBruce Evans 
6556f841fb7SMarcel Moolenaar static __inline int
6566f841fb7SMarcel Moolenaar sigprop(int sig)
6572c42a146SMarcel Moolenaar {
6586f841fb7SMarcel Moolenaar 
659ed6d876bSBrooks Davis 	if (sig > 0 && sig < nitems(sigproptbl))
660ed6d876bSBrooks Davis 		return (sigproptbl[sig]);
6612c42a146SMarcel Moolenaar 	return (0);
662df8bae1dSRodney W. Grimes }
6632c42a146SMarcel Moolenaar 
6644093529dSJeff Roberson int
6656f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
6662c42a146SMarcel Moolenaar {
6672c42a146SMarcel Moolenaar 	int i;
6682c42a146SMarcel Moolenaar 
6696f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
6702c42a146SMarcel Moolenaar 		if (set->__bits[i])
6712c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
672df8bae1dSRodney W. Grimes 	return (0);
673df8bae1dSRodney W. Grimes }
674df8bae1dSRodney W. Grimes 
675350ae563SKonstantin Belousov static bool
676ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag)
677350ae563SKonstantin Belousov {
678350ae563SKonstantin Belousov 
679c83655f3SKonstantin Belousov 	/*
680c83655f3SKonstantin Belousov 	 * SA_SIGINFO is reset when signal disposition is set to
681c83655f3SKonstantin Belousov 	 * ignore or default.  Other flags are kept according to user
682c83655f3SKonstantin Belousov 	 * settings.
683c83655f3SKonstantin Belousov 	 */
684c83655f3SKonstantin Belousov 	return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
685c83655f3SKonstantin Belousov 	    ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
686c83655f3SKonstantin Belousov 	    (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
687350ae563SKonstantin Belousov }
688350ae563SKonstantin Belousov 
6892c42a146SMarcel Moolenaar /*
6908f19eb88SIan Dowse  * kern_sigaction
6912c42a146SMarcel Moolenaar  * sigaction
69223eeeff7SPeter Wemm  * freebsd4_sigaction
6932c42a146SMarcel Moolenaar  * osigaction
6942c42a146SMarcel Moolenaar  */
6958f19eb88SIan Dowse int
696ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
697ea566832SEd Schouten     struct sigaction *oact, int flags)
698df8bae1dSRodney W. Grimes {
69990af4afaSJohn Baldwin 	struct sigacts *ps;
7008f19eb88SIan Dowse 	struct proc *p = td->td_proc;
701df8bae1dSRodney W. Grimes 
7022899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
7032c42a146SMarcel Moolenaar 		return (EINVAL);
704271ab240SKonstantin Belousov 	if (act != NULL && act->sa_handler != SIG_DFL &&
705271ab240SKonstantin Belousov 	    act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
706271ab240SKonstantin Belousov 	    SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
707271ab240SKonstantin Belousov 	    SA_NOCLDWAIT | SA_SIGINFO)) != 0)
7082d864174SKonstantin Belousov 		return (EINVAL);
7092c42a146SMarcel Moolenaar 
710628d2653SJohn Baldwin 	PROC_LOCK(p);
711628d2653SJohn Baldwin 	ps = p->p_sigacts;
71290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
7132c42a146SMarcel Moolenaar 	if (oact) {
714fb441a88SKonstantin Belousov 		memset(oact, 0, sizeof(*oact));
7152c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
7162c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
7172c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
7182c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
7192c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
7202c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
7212c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
7222c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
7232c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
72410c2b8e1SDavid E. O'Brien 		if (SIGISMEMBER(ps->ps_siginfo, sig)) {
7252c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
72610c2b8e1SDavid E. O'Brien 			oact->sa_sigaction =
72710c2b8e1SDavid E. O'Brien 			    (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
72810c2b8e1SDavid E. O'Brien 		} else
72910c2b8e1SDavid E. O'Brien 			oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
73090af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
7312c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
73290af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
7332c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
7342c42a146SMarcel Moolenaar 	}
7352c42a146SMarcel Moolenaar 	if (act) {
7362c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
737628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
73890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
739628d2653SJohn Baldwin 			PROC_UNLOCK(p);
7402c42a146SMarcel Moolenaar 			return (EINVAL);
741628d2653SJohn Baldwin 		}
7422c42a146SMarcel Moolenaar 
743df8bae1dSRodney W. Grimes 		/*
744df8bae1dSRodney W. Grimes 		 * Change setting atomically.
745df8bae1dSRodney W. Grimes 		 */
7462c42a146SMarcel Moolenaar 
7472c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
7482c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
749350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_SIGINFO)) {
750aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
751aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
75280f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
75380f42b55SIan Dowse 		} else {
75480f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
7552c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
7562c42a146SMarcel Moolenaar 		}
757350ae563SKonstantin Belousov 		if (!sigact_flag_test(act, SA_RESTART))
7582c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
759df8bae1dSRodney W. Grimes 		else
7602c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
761350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_ONSTACK))
7622c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
763df8bae1dSRodney W. Grimes 		else
7642c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
765350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_RESETHAND))
7662c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
7671e41c1b5SSteven Wallace 		else
7682c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
769350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_NODEFER))
7702c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
771289ccde0SPeter Wemm 		else
7722c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
7732c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
7742c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
77590af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
7766626c604SJulian Elischer 			else
77790af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
778ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
779245f17d4SJoerg Wunsch 				/*
7802c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
7812c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
7822c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
7832c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
784245f17d4SJoerg Wunsch 				 */
785245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
78690af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
7876626c604SJulian Elischer 				else
78890af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
789245f17d4SJoerg Wunsch 			} else
79090af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
791ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
79290af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
793ba1551caSIan Dowse 			else
79490af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
795df8bae1dSRodney W. Grimes 		}
796df8bae1dSRodney W. Grimes 		/*
79790af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
7982c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
79990af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
8002c42a146SMarcel Moolenaar 		 * have to restart the process.
801df8bae1dSRodney W. Grimes 		 */
8022c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
803fd50a707SBrooks Davis 		    (sigprop(sig) & SIGPROP_IGNORE &&
8042c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
8052c42a146SMarcel Moolenaar 			/* never to be seen again */
8069104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
8072c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
8082c42a146SMarcel Moolenaar 				/* easier in psignal */
80990af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
81090af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
811645682fdSLuoqi Chen 		} else {
81290af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
8132c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
81490af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
8152c42a146SMarcel Moolenaar 			else
81690af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
8172c42a146SMarcel Moolenaar 		}
81823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
81923eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
82023eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
82123eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
82223eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
82323eeeff7SPeter Wemm 		else
82423eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
82523eeeff7SPeter Wemm #endif
826e8ebc08fSPeter Wemm #ifdef COMPAT_43
827645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
82823eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
82923eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
830645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
831645682fdSLuoqi Chen 		else
832645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
833e8ebc08fSPeter Wemm #endif
834df8bae1dSRodney W. Grimes 	}
83590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
836628d2653SJohn Baldwin 	PROC_UNLOCK(p);
8372c42a146SMarcel Moolenaar 	return (0);
8382c42a146SMarcel Moolenaar }
8392c42a146SMarcel Moolenaar 
8402c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8412c42a146SMarcel Moolenaar struct sigaction_args {
8422c42a146SMarcel Moolenaar 	int	sig;
8432c42a146SMarcel Moolenaar 	struct	sigaction *act;
8442c42a146SMarcel Moolenaar 	struct	sigaction *oact;
8452c42a146SMarcel Moolenaar };
8462c42a146SMarcel Moolenaar #endif
8472c42a146SMarcel Moolenaar int
848e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap)
8492c42a146SMarcel Moolenaar {
8502c42a146SMarcel Moolenaar 	struct sigaction act, oact;
851e052a8b9SEd Maste 	struct sigaction *actp, *oactp;
8522c42a146SMarcel Moolenaar 	int error;
8532c42a146SMarcel Moolenaar 
8546f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
8556f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
8562c42a146SMarcel Moolenaar 	if (actp) {
8576f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
8582c42a146SMarcel Moolenaar 		if (error)
85944443757STim J. Robbins 			return (error);
8602c42a146SMarcel Moolenaar 	}
8618f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
86225d6dc06SJohn Baldwin 	if (oactp && !error)
8636f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
8642c42a146SMarcel Moolenaar 	return (error);
8652c42a146SMarcel Moolenaar }
8662c42a146SMarcel Moolenaar 
86723eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
86823eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
86923eeeff7SPeter Wemm struct freebsd4_sigaction_args {
87023eeeff7SPeter Wemm 	int	sig;
87123eeeff7SPeter Wemm 	struct	sigaction *act;
87223eeeff7SPeter Wemm 	struct	sigaction *oact;
87323eeeff7SPeter Wemm };
87423eeeff7SPeter Wemm #endif
87523eeeff7SPeter Wemm int
876e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
87723eeeff7SPeter Wemm {
87823eeeff7SPeter Wemm 	struct sigaction act, oact;
879e052a8b9SEd Maste 	struct sigaction *actp, *oactp;
88023eeeff7SPeter Wemm 	int error;
88123eeeff7SPeter Wemm 
88223eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
88323eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
88423eeeff7SPeter Wemm 	if (actp) {
88523eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
88623eeeff7SPeter Wemm 		if (error)
88744443757STim J. Robbins 			return (error);
88823eeeff7SPeter Wemm 	}
88923eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
890a14e1189SJohn Baldwin 	if (oactp && !error)
89123eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
89223eeeff7SPeter Wemm 	return (error);
89323eeeff7SPeter Wemm }
89423eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
89523eeeff7SPeter Wemm 
89631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8972c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8982c42a146SMarcel Moolenaar struct osigaction_args {
8992c42a146SMarcel Moolenaar 	int	signum;
9002c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
9012c42a146SMarcel Moolenaar 	struct	osigaction *osa;
9022c42a146SMarcel Moolenaar };
9032c42a146SMarcel Moolenaar #endif
9042c42a146SMarcel Moolenaar int
905e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap)
9062c42a146SMarcel Moolenaar {
9072c42a146SMarcel Moolenaar 	struct osigaction sa;
9082c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
909e052a8b9SEd Maste 	struct sigaction *nsap, *osap;
9102c42a146SMarcel Moolenaar 	int error;
9112c42a146SMarcel Moolenaar 
9126f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
9136f841fb7SMarcel Moolenaar 		return (EINVAL);
914fb99ab88SMatthew Dillon 
9156f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
9166f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
917fb99ab88SMatthew Dillon 
9182c42a146SMarcel Moolenaar 	if (nsap) {
9196f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
9202c42a146SMarcel Moolenaar 		if (error)
92144443757STim J. Robbins 			return (error);
9222c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
9232c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
9242c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
9252c42a146SMarcel Moolenaar 	}
92623eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
9272c42a146SMarcel Moolenaar 	if (osap && !error) {
9282c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
9292c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
9302c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
9316f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
9322c42a146SMarcel Moolenaar 	}
9332c42a146SMarcel Moolenaar 	return (error);
9342c42a146SMarcel Moolenaar }
93523eeeff7SPeter Wemm 
93673dbd3daSJohn Baldwin #if !defined(__i386__)
93723eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
93823eeeff7SPeter Wemm int
939e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap)
94023eeeff7SPeter Wemm {
94123eeeff7SPeter Wemm 
94223eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
94323eeeff7SPeter Wemm }
94423eeeff7SPeter Wemm #endif
94531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
946df8bae1dSRodney W. Grimes 
947df8bae1dSRodney W. Grimes /*
948df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
949df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
950df8bae1dSRodney W. Grimes  */
951df8bae1dSRodney W. Grimes void
952e052a8b9SEd Maste siginit(struct proc *p)
953df8bae1dSRodney W. Grimes {
9543e85b721SEd Maste 	int i;
95590af4afaSJohn Baldwin 	struct sigacts *ps;
956df8bae1dSRodney W. Grimes 
957628d2653SJohn Baldwin 	PROC_LOCK(p);
95890af4afaSJohn Baldwin 	ps = p->p_sigacts;
95990af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
960350ae563SKonstantin Belousov 	for (i = 1; i <= NSIG; i++) {
961fd50a707SBrooks Davis 		if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
96290af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
963350ae563SKonstantin Belousov 		}
964350ae563SKonstantin Belousov 	}
96590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
966628d2653SJohn Baldwin 	PROC_UNLOCK(p);
967df8bae1dSRodney W. Grimes }
968df8bae1dSRodney W. Grimes 
969df8bae1dSRodney W. Grimes /*
970350ae563SKonstantin Belousov  * Reset specified signal to the default disposition.
971350ae563SKonstantin Belousov  */
972350ae563SKonstantin Belousov static void
973350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig)
974350ae563SKonstantin Belousov {
975350ae563SKonstantin Belousov 
976350ae563SKonstantin Belousov 	mtx_assert(&ps->ps_mtx, MA_OWNED);
977350ae563SKonstantin Belousov 	SIGDELSET(ps->ps_sigcatch, sig);
978fd50a707SBrooks Davis 	if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
979350ae563SKonstantin Belousov 		SIGADDSET(ps->ps_sigignore, sig);
980350ae563SKonstantin Belousov 	ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
981350ae563SKonstantin Belousov 	SIGDELSET(ps->ps_siginfo, sig);
982350ae563SKonstantin Belousov }
983350ae563SKonstantin Belousov 
984350ae563SKonstantin Belousov /*
985df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
986df8bae1dSRodney W. Grimes  */
987df8bae1dSRodney W. Grimes void
988a30ec4b9SDavid Xu execsigs(struct proc *p)
989df8bae1dSRodney W. Grimes {
990a30ec4b9SDavid Xu 	struct sigacts *ps;
991a30ec4b9SDavid Xu 	struct thread *td;
992df8bae1dSRodney W. Grimes 
993df8bae1dSRodney W. Grimes 	/*
994df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
9954093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
996df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
997df8bae1dSRodney W. Grimes 	 */
9989b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
999628d2653SJohn Baldwin 	ps = p->p_sigacts;
100090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
1001079c5b9eSKyle Evans 	sig_drop_caught(p);
1002f3fe76ecSEd Schouten 
1003f3fe76ecSEd Schouten 	/*
1004df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
1005df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
1006df8bae1dSRodney W. Grimes 	 */
1007469ec1ebSKonstantin Belousov 	td = curthread;
1008469ec1ebSKonstantin Belousov 	MPASS(td->td_proc == p);
1009a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
1010a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
1011a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
1012a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
101380e907a1SPeter Wemm 	/*
101480e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
101580e907a1SPeter Wemm 	 */
101690af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1017c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1018c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
101990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
1020df8bae1dSRodney W. Grimes }
1021df8bae1dSRodney W. Grimes 
1022df8bae1dSRodney W. Grimes /*
1023e77daab1SJohn Baldwin  * kern_sigprocmask()
10247c8fdcbdSMatthew Dillon  *
1025628d2653SJohn Baldwin  *	Manipulate signal mask.
1026df8bae1dSRodney W. Grimes  */
1027e77daab1SJohn Baldwin int
10286b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
102984440afbSKonstantin Belousov     int flags)
10302c42a146SMarcel Moolenaar {
10316b286ee8SKonstantin Belousov 	sigset_t new_block, oset1;
10326b286ee8SKonstantin Belousov 	struct proc *p;
10332c42a146SMarcel Moolenaar 	int error;
10342c42a146SMarcel Moolenaar 
10356b286ee8SKonstantin Belousov 	p = td->td_proc;
103670778bbaSKonstantin Belousov 	if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
103770778bbaSKonstantin Belousov 		PROC_LOCK_ASSERT(p, MA_OWNED);
103870778bbaSKonstantin Belousov 	else
10396b286ee8SKonstantin Belousov 		PROC_LOCK(p);
104070778bbaSKonstantin Belousov 	mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
104170778bbaSKonstantin Belousov 	    ? MA_OWNED : MA_NOTOWNED);
10422c42a146SMarcel Moolenaar 	if (oset != NULL)
10434093529dSJeff Roberson 		*oset = td->td_sigmask;
10442c42a146SMarcel Moolenaar 
10452c42a146SMarcel Moolenaar 	error = 0;
10462c42a146SMarcel Moolenaar 	if (set != NULL) {
10472c42a146SMarcel Moolenaar 		switch (how) {
10482c42a146SMarcel Moolenaar 		case SIG_BLOCK:
1049645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
10506b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
10514093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
10526b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
10536b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
10542c42a146SMarcel Moolenaar 			break;
10552c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
10564093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
10574093529dSJeff Roberson 			signotify(td);
1058407af02bSDavid Xu 			goto out;
10592c42a146SMarcel Moolenaar 		case SIG_SETMASK:
1060645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
10616b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
106284440afbSKonstantin Belousov 			if (flags & SIGPROCMASK_OLD)
10634093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
1064645682fdSLuoqi Chen 			else
10654093529dSJeff Roberson 				td->td_sigmask = *set;
10666b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
10676b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
10684093529dSJeff Roberson 			signotify(td);
10692c42a146SMarcel Moolenaar 			break;
10702c42a146SMarcel Moolenaar 		default:
10712c42a146SMarcel Moolenaar 			error = EINVAL;
1072407af02bSDavid Xu 			goto out;
10732c42a146SMarcel Moolenaar 		}
10746b286ee8SKonstantin Belousov 
10756b286ee8SKonstantin Belousov 		/*
10767df8f6abSKonstantin Belousov 		 * The new_block set contains signals that were not previously
10776b286ee8SKonstantin Belousov 		 * blocked, but are blocked now.
10786b286ee8SKonstantin Belousov 		 *
10796b286ee8SKonstantin Belousov 		 * In case we block any signal that was not previously blocked
10806b286ee8SKonstantin Belousov 		 * for td, and process has the signal pending, try to schedule
1081407af02bSDavid Xu 		 * signal delivery to some thread that does not block the
1082407af02bSDavid Xu 		 * signal, possibly waking it up.
10836b286ee8SKonstantin Belousov 		 */
10846b286ee8SKonstantin Belousov 		if (p->p_numthreads != 1)
108580a8b0f3SKonstantin Belousov 			reschedule_signals(p, new_block, flags);
1086407af02bSDavid Xu 	}
10876b286ee8SKonstantin Belousov 
1088407af02bSDavid Xu out:
108984440afbSKonstantin Belousov 	if (!(flags & SIGPROCMASK_PROC_LOCKED))
10906b286ee8SKonstantin Belousov 		PROC_UNLOCK(p);
10912c42a146SMarcel Moolenaar 	return (error);
10922c42a146SMarcel Moolenaar }
10932c42a146SMarcel Moolenaar 
1094d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1095df8bae1dSRodney W. Grimes struct sigprocmask_args {
1096df8bae1dSRodney W. Grimes 	int	how;
10972c42a146SMarcel Moolenaar 	const sigset_t *set;
10982c42a146SMarcel Moolenaar 	sigset_t *oset;
1099df8bae1dSRodney W. Grimes };
1100d2d3e875SBruce Evans #endif
110126f9a767SRodney W. Grimes int
1102e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1103df8bae1dSRodney W. Grimes {
11042c42a146SMarcel Moolenaar 	sigset_t set, oset;
11052c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
11062c42a146SMarcel Moolenaar 	int error;
1107df8bae1dSRodney W. Grimes 
11086f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
11096f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
11102c42a146SMarcel Moolenaar 	if (setp) {
11116f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
11122c42a146SMarcel Moolenaar 		if (error)
11132c42a146SMarcel Moolenaar 			return (error);
1114df8bae1dSRodney W. Grimes 	}
1115e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
11162c42a146SMarcel Moolenaar 	if (osetp && !error) {
11176f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
11182c42a146SMarcel Moolenaar 	}
11192c42a146SMarcel Moolenaar 	return (error);
11202c42a146SMarcel Moolenaar }
11212c42a146SMarcel Moolenaar 
112231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
11232c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11242c42a146SMarcel Moolenaar struct osigprocmask_args {
11252c42a146SMarcel Moolenaar 	int	how;
11262c42a146SMarcel Moolenaar 	osigset_t mask;
11272c42a146SMarcel Moolenaar };
11282c42a146SMarcel Moolenaar #endif
11292c42a146SMarcel Moolenaar int
1130e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap)
11312c42a146SMarcel Moolenaar {
11322c42a146SMarcel Moolenaar 	sigset_t set, oset;
11332c42a146SMarcel Moolenaar 	int error;
11342c42a146SMarcel Moolenaar 
11352c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1136e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1137b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
1138df8bae1dSRodney W. Grimes 	return (error);
1139df8bae1dSRodney W. Grimes }
114031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1141df8bae1dSRodney W. Grimes 
114226f9a767SRodney W. Grimes int
11438451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap)
1144a447cd8bSJeff Roberson {
11459104847fSDavid Xu 	ksiginfo_t ksi;
1146a447cd8bSJeff Roberson 	sigset_t set;
1147a447cd8bSJeff Roberson 	int error;
1148a447cd8bSJeff Roberson 
1149a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
115036939a0aSDavid Xu 	if (error) {
115136939a0aSDavid Xu 		td->td_retval[0] = error;
115236939a0aSDavid Xu 		return (0);
115336939a0aSDavid Xu 	}
1154a447cd8bSJeff Roberson 
11559104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
115636939a0aSDavid Xu 	if (error) {
1157acced8b0SKonstantin Belousov 		/*
1158acced8b0SKonstantin Belousov 		 * sigwait() function shall not return EINTR, but
1159acced8b0SKonstantin Belousov 		 * the syscall does.  Non-ancient libc provides the
1160acced8b0SKonstantin Belousov 		 * wrapper which hides EINTR.  Otherwise, EINTR return
1161acced8b0SKonstantin Belousov 		 * is used by libthr to handle required cancellation
1162acced8b0SKonstantin Belousov 		 * point in the sigwait().
1163acced8b0SKonstantin Belousov 		 */
11648e9a54eeSKonstantin Belousov 		if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1165afb36e28SKonstantin Belousov 			return (ERESTART);
116636939a0aSDavid Xu 		td->td_retval[0] = error;
116736939a0aSDavid Xu 		return (0);
116836939a0aSDavid Xu 	}
1169a447cd8bSJeff Roberson 
11709104847fSDavid Xu 	error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
117136939a0aSDavid Xu 	td->td_retval[0] = error;
117236939a0aSDavid Xu 	return (0);
1173a447cd8bSJeff Roberson }
11740c14ff0eSRobert Watson 
1175a447cd8bSJeff Roberson int
11768451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1177a447cd8bSJeff Roberson {
1178a447cd8bSJeff Roberson 	struct timespec ts;
1179a447cd8bSJeff Roberson 	struct timespec *timeout;
1180a447cd8bSJeff Roberson 	sigset_t set;
11819104847fSDavid Xu 	ksiginfo_t ksi;
1182a447cd8bSJeff Roberson 	int error;
1183a447cd8bSJeff Roberson 
1184a447cd8bSJeff Roberson 	if (uap->timeout) {
1185a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
1186a447cd8bSJeff Roberson 		if (error)
1187a447cd8bSJeff Roberson 			return (error);
1188a447cd8bSJeff Roberson 
1189a447cd8bSJeff Roberson 		timeout = &ts;
1190a447cd8bSJeff Roberson 	} else
1191a447cd8bSJeff Roberson 		timeout = NULL;
1192a447cd8bSJeff Roberson 
1193a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1194a447cd8bSJeff Roberson 	if (error)
1195a447cd8bSJeff Roberson 		return (error);
1196a447cd8bSJeff Roberson 
11979104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, timeout);
1198a447cd8bSJeff Roberson 	if (error)
1199a447cd8bSJeff Roberson 		return (error);
12009dde3bc9SDavid Xu 
1201418228dfSDavid Xu 	if (uap->info)
12029104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
12039104847fSDavid Xu 
12049104847fSDavid Xu 	if (error == 0)
12059104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1206a447cd8bSJeff Roberson 	return (error);
1207a447cd8bSJeff Roberson }
1208a447cd8bSJeff Roberson 
1209a447cd8bSJeff Roberson int
12108451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1211a447cd8bSJeff Roberson {
12129104847fSDavid Xu 	ksiginfo_t ksi;
1213a447cd8bSJeff Roberson 	sigset_t set;
1214a447cd8bSJeff Roberson 	int error;
1215a447cd8bSJeff Roberson 
1216a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1217a447cd8bSJeff Roberson 	if (error)
1218a447cd8bSJeff Roberson 		return (error);
1219a447cd8bSJeff Roberson 
12209104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
1221a447cd8bSJeff Roberson 	if (error)
1222a447cd8bSJeff Roberson 		return (error);
1223a447cd8bSJeff Roberson 
1224418228dfSDavid Xu 	if (uap->info)
12259104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
12269104847fSDavid Xu 
12279104847fSDavid Xu 	if (error == 0)
12289104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1229a447cd8bSJeff Roberson 	return (error);
1230a447cd8bSJeff Roberson }
1231a447cd8bSJeff Roberson 
123286be94fcSTycho Nightingale static void
123386be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
123486be94fcSTycho Nightingale {
123586be94fcSTycho Nightingale 	struct thread *thr;
123686be94fcSTycho Nightingale 
123786be94fcSTycho Nightingale 	FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
123886be94fcSTycho Nightingale 		if (thr == td)
123986be94fcSTycho Nightingale 			thr->td_si = *si;
124086be94fcSTycho Nightingale 		else
124186be94fcSTycho Nightingale 			thr->td_si.si_signo = 0;
124286be94fcSTycho Nightingale 	}
124386be94fcSTycho Nightingale }
124486be94fcSTycho Nightingale 
1245c6511aeaSDavid Xu int
12469104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1247a447cd8bSJeff Roberson 	struct timespec *timeout)
1248a447cd8bSJeff Roberson {
12493074d1b4SDavid Xu 	struct sigacts *ps;
1250407af02bSDavid Xu 	sigset_t saved_mask, new_block;
1251a447cd8bSJeff Roberson 	struct proc *p;
1252407af02bSDavid Xu 	int error, sig, timo, timevalid = 0;
12531089f031SDavid Xu 	struct timespec rts, ets, ts;
12541089f031SDavid Xu 	struct timeval tv;
1255ef401a85SKonstantin Belousov 	bool traced;
1256a447cd8bSJeff Roberson 
1257a447cd8bSJeff Roberson 	p = td->td_proc;
1258a447cd8bSJeff Roberson 	error = 0;
12599b73d239SMatt Jacob 	ets.tv_sec = 0;
12609b73d239SMatt Jacob 	ets.tv_nsec = 0;
1261ef401a85SKonstantin Belousov 	traced = false;
1262a447cd8bSJeff Roberson 
1263dbec10e0SJonathan T. Looney 	/* Ensure the sigfastblock value is up to date. */
1264dbec10e0SJonathan T. Looney 	sigfastblock_fetch(td);
1265dbec10e0SJonathan T. Looney 
1266407af02bSDavid Xu 	if (timeout != NULL) {
12671089f031SDavid Xu 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
12681089f031SDavid Xu 			timevalid = 1;
12691089f031SDavid Xu 			getnanouptime(&rts);
12706040822cSAlan Somers 			timespecadd(&rts, timeout, &ets);
12711089f031SDavid Xu 		}
12721089f031SDavid Xu 	}
1273407af02bSDavid Xu 	ksiginfo_init(ksi);
1274407af02bSDavid Xu 	/* Some signals can not be waited for. */
1275407af02bSDavid Xu 	SIG_CANTMASK(waitset);
1276407af02bSDavid Xu 	ps = p->p_sigacts;
1277407af02bSDavid Xu 	PROC_LOCK(p);
1278407af02bSDavid Xu 	saved_mask = td->td_sigmask;
1279407af02bSDavid Xu 	SIGSETNAND(td->td_sigmask, waitset);
1280bc387624SKonstantin Belousov 	if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1281b599982bSKonstantin Belousov 	    !kern_sig_discard_ign) {
1282b599982bSKonstantin Belousov 		thread_lock(td);
1283b599982bSKonstantin Belousov 		td->td_flags |= TDF_SIGWAIT;
1284b599982bSKonstantin Belousov 		thread_unlock(td);
1285b599982bSKonstantin Belousov 	}
1286407af02bSDavid Xu 	for (;;) {
12873074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
12883cf3b9f0SJohn Baldwin 		sig = cursig(td);
12893074d1b4SDavid Xu 		mtx_unlock(&ps->ps_mtx);
129046e47c4fSKonstantin Belousov 		KASSERT(sig >= 0, ("sig %d", sig));
1291407af02bSDavid Xu 		if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1292407af02bSDavid Xu 			if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1293407af02bSDavid Xu 			    sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1294407af02bSDavid Xu 				error = 0;
1295407af02bSDavid Xu 				break;
12963074d1b4SDavid Xu 			}
12977e0221a2SDavid Xu 		}
12987e0221a2SDavid Xu 
1299407af02bSDavid Xu 		if (error != 0)
1300407af02bSDavid Xu 			break;
13013074d1b4SDavid Xu 
1302a447cd8bSJeff Roberson 		/*
1303a447cd8bSJeff Roberson 		 * POSIX says this must be checked after looking for pending
1304a447cd8bSJeff Roberson 		 * signals.
1305a447cd8bSJeff Roberson 		 */
1306407af02bSDavid Xu 		if (timeout != NULL) {
13071089f031SDavid Xu 			if (!timevalid) {
1308a447cd8bSJeff Roberson 				error = EINVAL;
1309407af02bSDavid Xu 				break;
1310a447cd8bSJeff Roberson 			}
13111089f031SDavid Xu 			getnanouptime(&rts);
13121089f031SDavid Xu 			if (timespeccmp(&rts, &ets, >=)) {
13133074d1b4SDavid Xu 				error = EAGAIN;
1314407af02bSDavid Xu 				break;
13153074d1b4SDavid Xu 			}
13166040822cSAlan Somers 			timespecsub(&ets, &rts, &ts);
13171089f031SDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
1318407af02bSDavid Xu 			timo = tvtohz(&tv);
1319407af02bSDavid Xu 		} else {
1320407af02bSDavid Xu 			timo = 0;
1321407af02bSDavid Xu 		}
1322a447cd8bSJeff Roberson 
1323ef401a85SKonstantin Belousov 		if (traced) {
1324ef401a85SKonstantin Belousov 			error = EINTR;
1325ef401a85SKonstantin Belousov 			break;
1326ef401a85SKonstantin Belousov 		}
1327ef401a85SKonstantin Belousov 
1328c4feb1abSMark Johnston 		error = msleep(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1329c4feb1abSMark Johnston 		    "sigwait", timo);
1330407af02bSDavid Xu 
1331afb36e28SKonstantin Belousov 		/* The syscalls can not be restarted. */
1332afb36e28SKonstantin Belousov 		if (error == ERESTART)
13333074d1b4SDavid Xu 			error = EINTR;
1334afb36e28SKonstantin Belousov 
1335407af02bSDavid Xu 		/* We will calculate timeout by ourself. */
1336afb36e28SKonstantin Belousov 		if (timeout != NULL && error == EAGAIN)
13371089f031SDavid Xu 			error = 0;
1338ef401a85SKonstantin Belousov 
1339ef401a85SKonstantin Belousov 		/*
1340ef401a85SKonstantin Belousov 		 * If PTRACE_SCE or PTRACE_SCX were set after
1341ef401a85SKonstantin Belousov 		 * userspace entered the syscall, return spurious
1342ef401a85SKonstantin Belousov 		 * EINTR after wait was done.  Only do this as last
1343ef401a85SKonstantin Belousov 		 * resort after rechecking for possible queued signals
1344ef401a85SKonstantin Belousov 		 * and expired timeouts.
1345ef401a85SKonstantin Belousov 		 */
1346ef401a85SKonstantin Belousov 		if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1347ef401a85SKonstantin Belousov 			traced = true;
1348407af02bSDavid Xu 	}
1349b599982bSKonstantin Belousov 	thread_lock(td);
1350b599982bSKonstantin Belousov 	td->td_flags &= ~TDF_SIGWAIT;
1351b599982bSKonstantin Belousov 	thread_unlock(td);
13529dde3bc9SDavid Xu 
1353407af02bSDavid Xu 	new_block = saved_mask;
1354407af02bSDavid Xu 	SIGSETNAND(new_block, td->td_sigmask);
1355407af02bSDavid Xu 	td->td_sigmask = saved_mask;
1356407af02bSDavid Xu 	/*
1357407af02bSDavid Xu 	 * Fewer signals can be delivered to us, reschedule signal
1358407af02bSDavid Xu 	 * notification.
1359407af02bSDavid Xu 	 */
1360407af02bSDavid Xu 	if (p->p_numthreads != 1)
1361407af02bSDavid Xu 		reschedule_signals(p, new_block, 0);
13625d217f17SJohn Birrell 
1363407af02bSDavid Xu 	if (error == 0) {
136436160958SMark Johnston 		SDT_PROBE2(proc, , , signal__clear, sig, ksi);
13655d217f17SJohn Birrell 
136656c06c4bSDavid Xu 		if (ksi->ksi_code == SI_TIMER)
136756c06c4bSDavid Xu 			itimer_accept(p, ksi->ksi_timerid, ksi);
13687e0221a2SDavid Xu 
13697e0221a2SDavid Xu #ifdef KTRACE
13707e0221a2SDavid Xu 		if (KTRPOINT(td, KTR_PSIG)) {
13717e0221a2SDavid Xu 			sig_t action;
13727e0221a2SDavid Xu 
13733074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
1374a447cd8bSJeff Roberson 			action = ps->ps_sigact[_SIG_IDX(sig)];
137590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
137661009552SJilles Tjoelker 			ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
13777e0221a2SDavid Xu 		}
1378a447cd8bSJeff Roberson #endif
137986be94fcSTycho Nightingale 		if (sig == SIGKILL) {
138086be94fcSTycho Nightingale 			proc_td_siginfo_capture(td, &ksi->ksi_info);
13817e0221a2SDavid Xu 			sigexit(td, sig);
13823074d1b4SDavid Xu 		}
138386be94fcSTycho Nightingale 	}
1384a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
1385a447cd8bSJeff Roberson 	return (error);
1386a447cd8bSJeff Roberson }
1387a447cd8bSJeff Roberson 
13889104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
13899104847fSDavid Xu struct sigpending_args {
13909104847fSDavid Xu 	sigset_t	*set;
13919104847fSDavid Xu };
13929104847fSDavid Xu #endif
1393a447cd8bSJeff Roberson int
1394e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap)
1395df8bae1dSRodney W. Grimes {
1396b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13979104847fSDavid Xu 	sigset_t pending;
1398df8bae1dSRodney W. Grimes 
1399628d2653SJohn Baldwin 	PROC_LOCK(p);
14009104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
14019104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1402628d2653SJohn Baldwin 	PROC_UNLOCK(p);
14039104847fSDavid Xu 	return (copyout(&pending, uap->set, sizeof(sigset_t)));
14042c42a146SMarcel Moolenaar }
14052c42a146SMarcel Moolenaar 
140631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
14072c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
14082c42a146SMarcel Moolenaar struct osigpending_args {
14092c42a146SMarcel Moolenaar 	int	dummy;
14102c42a146SMarcel Moolenaar };
14112c42a146SMarcel Moolenaar #endif
14122c42a146SMarcel Moolenaar int
1413e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap)
14142c42a146SMarcel Moolenaar {
1415b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
14169104847fSDavid Xu 	sigset_t pending;
1417b40ce416SJulian Elischer 
1418628d2653SJohn Baldwin 	PROC_LOCK(p);
14199104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
14209104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1421628d2653SJohn Baldwin 	PROC_UNLOCK(p);
14229104847fSDavid Xu 	SIG2OSIG(pending, td->td_retval[0]);
1423df8bae1dSRodney W. Grimes 	return (0);
1424df8bae1dSRodney W. Grimes }
142531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1426df8bae1dSRodney W. Grimes 
14271930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1428df8bae1dSRodney W. Grimes /*
1429df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1430df8bae1dSRodney W. Grimes  */
1431d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1432df8bae1dSRodney W. Grimes struct osigvec_args {
1433df8bae1dSRodney W. Grimes 	int	signum;
1434df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1435df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1436df8bae1dSRodney W. Grimes };
1437d2d3e875SBruce Evans #endif
1438df8bae1dSRodney W. Grimes /* ARGSUSED */
143926f9a767SRodney W. Grimes int
1440e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap)
1441df8bae1dSRodney W. Grimes {
1442df8bae1dSRodney W. Grimes 	struct sigvec vec;
14432c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
1444e052a8b9SEd Maste 	struct sigaction *nsap, *osap;
14452c42a146SMarcel Moolenaar 	int error;
1446df8bae1dSRodney W. Grimes 
14476f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
14486f841fb7SMarcel Moolenaar 		return (EINVAL);
14496f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
14506f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
14512c42a146SMarcel Moolenaar 	if (nsap) {
14526f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
14532c42a146SMarcel Moolenaar 		if (error)
1454df8bae1dSRodney W. Grimes 			return (error);
14552c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
14562c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
14572c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
14582c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1459df8bae1dSRodney W. Grimes 	}
14605edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
14612c42a146SMarcel Moolenaar 	if (osap && !error) {
14622c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
14632c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
14642c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
14652c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
14662c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
14676f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
14682c42a146SMarcel Moolenaar 	}
14692c42a146SMarcel Moolenaar 	return (error);
1470df8bae1dSRodney W. Grimes }
1471df8bae1dSRodney W. Grimes 
1472d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1473df8bae1dSRodney W. Grimes struct osigblock_args {
1474df8bae1dSRodney W. Grimes 	int	mask;
1475df8bae1dSRodney W. Grimes };
1476d2d3e875SBruce Evans #endif
147726f9a767SRodney W. Grimes int
1478e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap)
1479df8bae1dSRodney W. Grimes {
1480d6e029adSKonstantin Belousov 	sigset_t set, oset;
1481df8bae1dSRodney W. Grimes 
14822c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1483d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1484d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1485df8bae1dSRodney W. Grimes 	return (0);
1486df8bae1dSRodney W. Grimes }
1487df8bae1dSRodney W. Grimes 
1488d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1489df8bae1dSRodney W. Grimes struct osigsetmask_args {
1490df8bae1dSRodney W. Grimes 	int	mask;
1491df8bae1dSRodney W. Grimes };
1492d2d3e875SBruce Evans #endif
149326f9a767SRodney W. Grimes int
1494e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1495df8bae1dSRodney W. Grimes {
1496d6e029adSKonstantin Belousov 	sigset_t set, oset;
1497df8bae1dSRodney W. Grimes 
14982c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1499d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1500d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1501df8bae1dSRodney W. Grimes 	return (0);
1502df8bae1dSRodney W. Grimes }
15031930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1504df8bae1dSRodney W. Grimes 
1505df8bae1dSRodney W. Grimes /*
1506873fbcd7SRobert Watson  * Suspend calling thread until signal, providing mask to be set in the
1507873fbcd7SRobert Watson  * meantime.
1508df8bae1dSRodney W. Grimes  */
1509d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1510df8bae1dSRodney W. Grimes struct sigsuspend_args {
15112c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1512df8bae1dSRodney W. Grimes };
1513d2d3e875SBruce Evans #endif
1514df8bae1dSRodney W. Grimes /* ARGSUSED */
151526f9a767SRodney W. Grimes int
1516e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1517df8bae1dSRodney W. Grimes {
15182c42a146SMarcel Moolenaar 	sigset_t mask;
15192c42a146SMarcel Moolenaar 	int error;
15202c42a146SMarcel Moolenaar 
15216f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
15222c42a146SMarcel Moolenaar 	if (error)
15232c42a146SMarcel Moolenaar 		return (error);
15248f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
15258f19eb88SIan Dowse }
15268f19eb88SIan Dowse 
15278f19eb88SIan Dowse int
15288f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
15298f19eb88SIan Dowse {
15308f19eb88SIan Dowse 	struct proc *p = td->td_proc;
153184440afbSKonstantin Belousov 	int has_sig, sig;
1532df8bae1dSRodney W. Grimes 
1533dbec10e0SJonathan T. Looney 	/* Ensure the sigfastblock value is up to date. */
1534dbec10e0SJonathan T. Looney 	sigfastblock_fetch(td);
1535dbec10e0SJonathan T. Looney 
1536df8bae1dSRodney W. Grimes 	/*
1537645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1538df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1539df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1540df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1541df8bae1dSRodney W. Grimes 	 * to indicate this.
1542df8bae1dSRodney W. Grimes 	 */
1543628d2653SJohn Baldwin 	PROC_LOCK(p);
154484440afbSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
154584440afbSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED);
15465e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
154784440afbSKonstantin Belousov 
154884440afbSKonstantin Belousov 	/*
154984440afbSKonstantin Belousov 	 * Process signals now. Otherwise, we can get spurious wakeup
155084440afbSKonstantin Belousov 	 * due to signal entered process queue, but delivered to other
155184440afbSKonstantin Belousov 	 * thread. But sigsuspend should return only on signal
155284440afbSKonstantin Belousov 	 * delivery.
155384440afbSKonstantin Belousov 	 */
1554afe1a688SKonstantin Belousov 	(p->p_sysent->sv_set_syscall_retval)(td, EINTR);
155584440afbSKonstantin Belousov 	for (has_sig = 0; !has_sig;) {
155684440afbSKonstantin Belousov 		while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
155784440afbSKonstantin Belousov 			0) == 0)
15582c42a146SMarcel Moolenaar 			/* void */;
155984440afbSKonstantin Belousov 		thread_suspend_check(0);
156084440afbSKonstantin Belousov 		mtx_lock(&p->p_sigacts->ps_mtx);
156146e47c4fSKonstantin Belousov 		while ((sig = cursig(td)) != 0) {
156246e47c4fSKonstantin Belousov 			KASSERT(sig >= 0, ("sig %d", sig));
156375c586a4SKonstantin Belousov 			has_sig += postsig(sig);
156446e47c4fSKonstantin Belousov 		}
156584440afbSKonstantin Belousov 		mtx_unlock(&p->p_sigacts->ps_mtx);
1566ef401a85SKonstantin Belousov 
1567ef401a85SKonstantin Belousov 		/*
1568ef401a85SKonstantin Belousov 		 * If PTRACE_SCE or PTRACE_SCX were set after
1569ef401a85SKonstantin Belousov 		 * userspace entered the syscall, return spurious
1570ef401a85SKonstantin Belousov 		 * EINTR.
1571ef401a85SKonstantin Belousov 		 */
1572ef401a85SKonstantin Belousov 		if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1573ef401a85SKonstantin Belousov 			has_sig += 1;
157484440afbSKonstantin Belousov 	}
1575628d2653SJohn Baldwin 	PROC_UNLOCK(p);
15762dd9ea6fSKonstantin Belousov 	td->td_errno = EINTR;
15772dd9ea6fSKonstantin Belousov 	td->td_pflags |= TDP_NERRNO;
157875c586a4SKonstantin Belousov 	return (EJUSTRETURN);
15792c42a146SMarcel Moolenaar }
15802c42a146SMarcel Moolenaar 
158131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
158297563428SAlexander Kabaev /*
158397563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
158497563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
158597563428SAlexander Kabaev  */
15862c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
15872c42a146SMarcel Moolenaar struct osigsuspend_args {
15882c42a146SMarcel Moolenaar 	osigset_t mask;
15892c42a146SMarcel Moolenaar };
15902c42a146SMarcel Moolenaar #endif
15912c42a146SMarcel Moolenaar /* ARGSUSED */
15922c42a146SMarcel Moolenaar int
1593e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap)
15942c42a146SMarcel Moolenaar {
1595645682fdSLuoqi Chen 	sigset_t mask;
15962c42a146SMarcel Moolenaar 
1597645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
159884440afbSKonstantin Belousov 	return (kern_sigsuspend(td, mask));
1599df8bae1dSRodney W. Grimes }
160031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1601df8bae1dSRodney W. Grimes 
16021930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1603d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1604df8bae1dSRodney W. Grimes struct osigstack_args {
1605df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1606df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1607df8bae1dSRodney W. Grimes };
1608d2d3e875SBruce Evans #endif
1609df8bae1dSRodney W. Grimes /* ARGSUSED */
161026f9a767SRodney W. Grimes int
1611e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap)
1612df8bae1dSRodney W. Grimes {
16135afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1614fb99ab88SMatthew Dillon 	int error = 0;
1615fb99ab88SMatthew Dillon 
1616d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
16175afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
16185afe0c99SJohn Baldwin 		if (error)
16195afe0c99SJohn Baldwin 			return (error);
1620df8bae1dSRodney W. Grimes 	}
1621a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
16225afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
16235afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1624a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1625a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1626a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1627a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
16285afe0c99SJohn Baldwin 	}
16295afe0c99SJohn Baldwin 	if (uap->oss != NULL)
16305afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
16315afe0c99SJohn Baldwin 
1632fb99ab88SMatthew Dillon 	return (error);
1633df8bae1dSRodney W. Grimes }
16341930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1635df8bae1dSRodney W. Grimes 
1636d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1637df8bae1dSRodney W. Grimes struct sigaltstack_args {
16382c42a146SMarcel Moolenaar 	stack_t	*ss;
16392c42a146SMarcel Moolenaar 	stack_t	*oss;
1640df8bae1dSRodney W. Grimes };
1641d2d3e875SBruce Evans #endif
1642df8bae1dSRodney W. Grimes /* ARGSUSED */
164326f9a767SRodney W. Grimes int
1644e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1645df8bae1dSRodney W. Grimes {
16468f19eb88SIan Dowse 	stack_t ss, oss;
16478f19eb88SIan Dowse 	int error;
16488f19eb88SIan Dowse 
16498f19eb88SIan Dowse 	if (uap->ss != NULL) {
16508f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
16518f19eb88SIan Dowse 		if (error)
16528f19eb88SIan Dowse 			return (error);
16538f19eb88SIan Dowse 	}
16548f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
16558f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
16568f19eb88SIan Dowse 	if (error)
16578f19eb88SIan Dowse 		return (error);
16588f19eb88SIan Dowse 	if (uap->oss != NULL)
16598f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
16608f19eb88SIan Dowse 	return (error);
16618f19eb88SIan Dowse }
16628f19eb88SIan Dowse 
16638f19eb88SIan Dowse int
16648f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
16658f19eb88SIan Dowse {
1666b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1667fb99ab88SMatthew Dillon 	int oonstack;
1668fb99ab88SMatthew Dillon 
1669b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1670d034d459SMarcel Moolenaar 
16718f19eb88SIan Dowse 	if (oss != NULL) {
1672a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1673a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1674d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1675df8bae1dSRodney W. Grimes 	}
1676d034d459SMarcel Moolenaar 
16778f19eb88SIan Dowse 	if (ss != NULL) {
1678a30ec4b9SDavid Xu 		if (oonstack)
1679cf60731bSJohn Baldwin 			return (EPERM);
1680a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1681cf60731bSJohn Baldwin 			return (EINVAL);
16828f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
16839104847fSDavid Xu 			if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1684cf60731bSJohn Baldwin 				return (ENOMEM);
16859104847fSDavid Xu 
1686a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1687a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1688628d2653SJohn Baldwin 		} else {
1689a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1690628d2653SJohn Baldwin 		}
1691d034d459SMarcel Moolenaar 	}
1692cf60731bSJohn Baldwin 	return (0);
1693df8bae1dSRodney W. Grimes }
1694df8bae1dSRodney W. Grimes 
16950cc9fb75SKonstantin Belousov struct killpg1_ctx {
16960cc9fb75SKonstantin Belousov 	struct thread *td;
16970cc9fb75SKonstantin Belousov 	ksiginfo_t *ksi;
16980cc9fb75SKonstantin Belousov 	int sig;
16990cc9fb75SKonstantin Belousov 	bool sent;
17000cc9fb75SKonstantin Belousov 	bool found;
17010cc9fb75SKonstantin Belousov 	int ret;
17020cc9fb75SKonstantin Belousov };
17030cc9fb75SKonstantin Belousov 
17040cc9fb75SKonstantin Belousov static void
17050cc9fb75SKonstantin Belousov killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
17060cc9fb75SKonstantin Belousov {
17070cc9fb75SKonstantin Belousov 	int err;
17080cc9fb75SKonstantin Belousov 
17090cc9fb75SKonstantin Belousov 	if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
17100cc9fb75SKonstantin Belousov 	    (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
17110cc9fb75SKonstantin Belousov 		return;
17120cc9fb75SKonstantin Belousov 	PROC_LOCK(p);
17130cc9fb75SKonstantin Belousov 	err = p_cansignal(arg->td, p, arg->sig);
17140cc9fb75SKonstantin Belousov 	if (err == 0 && arg->sig != 0)
17150cc9fb75SKonstantin Belousov 		pksignal(p, arg->sig, arg->ksi);
17160cc9fb75SKonstantin Belousov 	PROC_UNLOCK(p);
17170cc9fb75SKonstantin Belousov 	if (err != ESRCH)
17180cc9fb75SKonstantin Belousov 		arg->found = true;
17190cc9fb75SKonstantin Belousov 	if (err == 0)
17200cc9fb75SKonstantin Belousov 		arg->sent = true;
17210cc9fb75SKonstantin Belousov 	else if (arg->ret == 0 && err != ESRCH && err != EPERM)
17220cc9fb75SKonstantin Belousov 		arg->ret = err;
17230cc9fb75SKonstantin Belousov }
17240cc9fb75SKonstantin Belousov 
1725d93f860cSPoul-Henning Kamp /*
1726d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1727d93f860cSPoul-Henning Kamp  * cp is calling process.
1728d93f860cSPoul-Henning Kamp  */
172937c84183SPoul-Henning Kamp static int
1730a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1731d93f860cSPoul-Henning Kamp {
1732a3de221dSKonstantin Belousov 	struct proc *p;
1733d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
17340cc9fb75SKonstantin Belousov 	struct killpg1_ctx arg;
1735d93f860cSPoul-Henning Kamp 
17360cc9fb75SKonstantin Belousov 	arg.td = td;
17370cc9fb75SKonstantin Belousov 	arg.ksi = ksi;
17380cc9fb75SKonstantin Belousov 	arg.sig = sig;
17390cc9fb75SKonstantin Belousov 	arg.sent = false;
17400cc9fb75SKonstantin Belousov 	arg.found = false;
17410cc9fb75SKonstantin Belousov 	arg.ret = 0;
1742553629ebSJake Burkholder 	if (all) {
1743d93f860cSPoul-Henning Kamp 		/*
1744d93f860cSPoul-Henning Kamp 		 * broadcast
1745d93f860cSPoul-Henning Kamp 		 */
17461005a129SJohn Baldwin 		sx_slock(&allproc_lock);
17474f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
17480cc9fb75SKonstantin Belousov 			killpg1_sendsig(p, true, &arg);
1749d93f860cSPoul-Henning Kamp 		}
17501005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1751553629ebSJake Burkholder 	} else {
1752ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1753f591779bSSeigo Tanimura 		if (pgid == 0) {
1754d93f860cSPoul-Henning Kamp 			/*
1755d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1756d93f860cSPoul-Henning Kamp 			 */
17579c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1758f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1759f591779bSSeigo Tanimura 		} else {
1760d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1761f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1762ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1763d93f860cSPoul-Henning Kamp 				return (ESRCH);
1764d93f860cSPoul-Henning Kamp 			}
1765f591779bSSeigo Tanimura 		}
1766ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
17672e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
17680cc9fb75SKonstantin Belousov 			killpg1_sendsig(p, false, &arg);
1769d93f860cSPoul-Henning Kamp 		}
1770f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1771d93f860cSPoul-Henning Kamp 	}
17720cc9fb75SKonstantin Belousov 	MPASS(arg.ret != 0 || arg.found || !arg.sent);
17730cc9fb75SKonstantin Belousov 	if (arg.ret == 0 && !arg.sent)
17740cc9fb75SKonstantin Belousov 		arg.ret = arg.found ? EPERM : ESRCH;
17750cc9fb75SKonstantin Belousov 	return (arg.ret);
1776d93f860cSPoul-Henning Kamp }
1777d93f860cSPoul-Henning Kamp 
1778d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1779df8bae1dSRodney W. Grimes struct kill_args {
1780df8bae1dSRodney W. Grimes 	int	pid;
1781df8bae1dSRodney W. Grimes 	int	signum;
1782df8bae1dSRodney W. Grimes };
1783d2d3e875SBruce Evans #endif
1784df8bae1dSRodney W. Grimes /* ARGSUSED */
178526f9a767SRodney W. Grimes int
17868451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap)
1787df8bae1dSRodney W. Grimes {
178834ad5ac2SEdward Tomasz Napierala 
178934ad5ac2SEdward Tomasz Napierala 	return (kern_kill(td, uap->pid, uap->signum));
179034ad5ac2SEdward Tomasz Napierala }
179134ad5ac2SEdward Tomasz Napierala 
179234ad5ac2SEdward Tomasz Napierala int
179334ad5ac2SEdward Tomasz Napierala kern_kill(struct thread *td, pid_t pid, int signum)
179434ad5ac2SEdward Tomasz Napierala {
1795a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1796a3de221dSKonstantin Belousov 	struct proc *p;
179790af4afaSJohn Baldwin 	int error;
1798df8bae1dSRodney W. Grimes 
17998890f5d0SPawel Jakub Dawidek 	/*
18008890f5d0SPawel Jakub Dawidek 	 * A process in capability mode can send signals only to himself.
18018890f5d0SPawel Jakub Dawidek 	 * The main rationale behind this is that abort(3) is implemented as
18028890f5d0SPawel Jakub Dawidek 	 * kill(getpid(), SIGABRT).
18038890f5d0SPawel Jakub Dawidek 	 */
180434ad5ac2SEdward Tomasz Napierala 	if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
18058890f5d0SPawel Jakub Dawidek 		return (ECAPMODE);
18068890f5d0SPawel Jakub Dawidek 
180734ad5ac2SEdward Tomasz Napierala 	AUDIT_ARG_SIGNUM(signum);
180834ad5ac2SEdward Tomasz Napierala 	AUDIT_ARG_PID(pid);
180934ad5ac2SEdward Tomasz Napierala 	if ((u_int)signum > _SIG_MAXSIG)
1810df8bae1dSRodney W. Grimes 		return (EINVAL);
1811fb99ab88SMatthew Dillon 
1812a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
181334ad5ac2SEdward Tomasz Napierala 	ksi.ksi_signo = signum;
1814a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1815a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1816a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1817a3de221dSKonstantin Belousov 
181834ad5ac2SEdward Tomasz Napierala 	if (pid > 0) {
1819df8bae1dSRodney W. Grimes 		/* kill single process */
182034ad5ac2SEdward Tomasz Napierala 		if ((p = pfind_any(pid)) == NULL)
182190af4afaSJohn Baldwin 			return (ESRCH);
182214961ba7SRobert Watson 		AUDIT_ARG_PROCESS(p);
182334ad5ac2SEdward Tomasz Napierala 		error = p_cansignal(td, p, signum);
182434ad5ac2SEdward Tomasz Napierala 		if (error == 0 && signum)
182534ad5ac2SEdward Tomasz Napierala 			pksignal(p, signum, &ksi);
1826628d2653SJohn Baldwin 		PROC_UNLOCK(p);
182790af4afaSJohn Baldwin 		return (error);
1828df8bae1dSRodney W. Grimes 	}
182934ad5ac2SEdward Tomasz Napierala 	switch (pid) {
1830df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
183134ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, 0, 1, &ksi));
1832df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
183334ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, 0, 0, &ksi));
1834df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
183534ad5ac2SEdward Tomasz Napierala 		return (killpg1(td, signum, -pid, 0, &ksi));
1836df8bae1dSRodney W. Grimes 	}
183790af4afaSJohn Baldwin 	/* NOTREACHED */
1838df8bae1dSRodney W. Grimes }
1839df8bae1dSRodney W. Grimes 
1840cfb5f768SJonathan Anderson int
1841e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap)
1842cfb5f768SJonathan Anderson {
1843cfb5f768SJonathan Anderson 	struct proc *p;
1844cfb5f768SJonathan Anderson 	int error;
1845cfb5f768SJonathan Anderson 
1846cfb5f768SJonathan Anderson 	AUDIT_ARG_SIGNUM(uap->signum);
1847cfb5f768SJonathan Anderson 	AUDIT_ARG_FD(uap->fd);
1848cfb5f768SJonathan Anderson 	if ((u_int)uap->signum > _SIG_MAXSIG)
1849cfb5f768SJonathan Anderson 		return (EINVAL);
1850cfb5f768SJonathan Anderson 
1851cbd92ce6SMatt Macy 	error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1852cfb5f768SJonathan Anderson 	if (error)
1853cfb5f768SJonathan Anderson 		return (error);
1854cfb5f768SJonathan Anderson 	AUDIT_ARG_PROCESS(p);
1855cfb5f768SJonathan Anderson 	error = p_cansignal(td, p, uap->signum);
1856cfb5f768SJonathan Anderson 	if (error == 0 && uap->signum)
18578451d0ddSKip Macy 		kern_psignal(p, uap->signum);
1858cfb5f768SJonathan Anderson 	PROC_UNLOCK(p);
1859cfb5f768SJonathan Anderson 	return (error);
1860cfb5f768SJonathan Anderson }
1861cfb5f768SJonathan Anderson 
18621930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1863d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1864df8bae1dSRodney W. Grimes struct okillpg_args {
1865df8bae1dSRodney W. Grimes 	int	pgid;
1866df8bae1dSRodney W. Grimes 	int	signum;
1867df8bae1dSRodney W. Grimes };
1868d2d3e875SBruce Evans #endif
1869df8bae1dSRodney W. Grimes /* ARGSUSED */
187026f9a767SRodney W. Grimes int
1871a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap)
1872df8bae1dSRodney W. Grimes {
1873a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1874df8bae1dSRodney W. Grimes 
187514961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->signum);
187614961ba7SRobert Watson 	AUDIT_ARG_PID(uap->pgid);
18776c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1878df8bae1dSRodney W. Grimes 		return (EINVAL);
18799104847fSDavid Xu 
1880a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
1881a3de221dSKonstantin Belousov 	ksi.ksi_signo = uap->signum;
1882a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1883a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1884a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1885a3de221dSKonstantin Belousov 	return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1886df8bae1dSRodney W. Grimes }
18871930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1888df8bae1dSRodney W. Grimes 
18899104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
18909104847fSDavid Xu struct sigqueue_args {
18919104847fSDavid Xu 	pid_t pid;
18929104847fSDavid Xu 	int signum;
18939104847fSDavid Xu 	/* union sigval */ void *value;
18949104847fSDavid Xu };
18959104847fSDavid Xu #endif
18969104847fSDavid Xu int
18978451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
18989104847fSDavid Xu {
1899f19351aaSBrooks Davis 	union sigval sv;
1900f19351aaSBrooks Davis 
1901f19351aaSBrooks Davis 	sv.sival_ptr = uap->value;
1902f19351aaSBrooks Davis 
1903f19351aaSBrooks Davis 	return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
1904f19351aaSBrooks Davis }
1905f19351aaSBrooks Davis 
1906f19351aaSBrooks Davis int
1907f19351aaSBrooks Davis kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
1908f19351aaSBrooks Davis {
19099104847fSDavid Xu 	ksiginfo_t ksi;
19109104847fSDavid Xu 	struct proc *p;
19119104847fSDavid Xu 	int error;
19129104847fSDavid Xu 
1913f19351aaSBrooks Davis 	if ((u_int)signum > _SIG_MAXSIG)
19149104847fSDavid Xu 		return (EINVAL);
19159104847fSDavid Xu 
19169104847fSDavid Xu 	/*
19179104847fSDavid Xu 	 * Specification says sigqueue can only send signal to
19189104847fSDavid Xu 	 * single process.
19199104847fSDavid Xu 	 */
1920f19351aaSBrooks Davis 	if (pid <= 0)
19219104847fSDavid Xu 		return (EINVAL);
19229104847fSDavid Xu 
1923537d0fb1SMateusz Guzik 	if ((p = pfind_any(pid)) == NULL)
19249104847fSDavid Xu 		return (ESRCH);
1925f19351aaSBrooks Davis 	error = p_cansignal(td, p, signum);
1926f19351aaSBrooks Davis 	if (error == 0 && signum != 0) {
19279104847fSDavid Xu 		ksiginfo_init(&ksi);
1928a3de221dSKonstantin Belousov 		ksi.ksi_flags = KSI_SIGQ;
1929f19351aaSBrooks Davis 		ksi.ksi_signo = signum;
19309104847fSDavid Xu 		ksi.ksi_code = SI_QUEUE;
19319104847fSDavid Xu 		ksi.ksi_pid = td->td_proc->p_pid;
19329104847fSDavid Xu 		ksi.ksi_uid = td->td_ucred->cr_ruid;
1933f19351aaSBrooks Davis 		ksi.ksi_value = *value;
1934ad6eec7bSJohn Baldwin 		error = pksignal(p, ksi.ksi_signo, &ksi);
19359104847fSDavid Xu 	}
19369104847fSDavid Xu 	PROC_UNLOCK(p);
19379104847fSDavid Xu 	return (error);
19389104847fSDavid Xu }
19399104847fSDavid Xu 
1940df8bae1dSRodney W. Grimes /*
1941df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1942df8bae1dSRodney W. Grimes  */
1943df8bae1dSRodney W. Grimes void
1944a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi)
1945df8bae1dSRodney W. Grimes {
1946df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1947df8bae1dSRodney W. Grimes 
1948f591779bSSeigo Tanimura 	if (pgid != 0) {
1949ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1950f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1951ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1952f591779bSSeigo Tanimura 		if (pgrp != NULL) {
1953a3de221dSKonstantin Belousov 			pgsignal(pgrp, sig, 0, ksi);
1954f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1955f591779bSSeigo Tanimura 		}
1956f591779bSSeigo Tanimura 	}
1957df8bae1dSRodney W. Grimes }
1958df8bae1dSRodney W. Grimes 
1959df8bae1dSRodney W. Grimes /*
1960df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1961df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1962df8bae1dSRodney W. Grimes  */
1963df8bae1dSRodney W. Grimes void
1964a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1965df8bae1dSRodney W. Grimes {
1966a3de221dSKonstantin Belousov 	struct proc *p;
1967df8bae1dSRodney W. Grimes 
1968628d2653SJohn Baldwin 	if (pgrp) {
1969f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1970628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1971628d2653SJohn Baldwin 			PROC_LOCK(p);
1972e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
1973e806d352SJohn Baldwin 			    (checkctty == 0 || p->p_flag & P_CONTROLT))
1974a3de221dSKonstantin Belousov 				pksignal(p, sig, ksi);
1975628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1976628d2653SJohn Baldwin 		}
1977628d2653SJohn Baldwin 	}
1978df8bae1dSRodney W. Grimes }
1979df8bae1dSRodney W. Grimes 
1980e442f29fSKonstantin Belousov /*
1981e442f29fSKonstantin Belousov  * Recalculate the signal mask and reset the signal disposition after
1982e442f29fSKonstantin Belousov  * usermode frame for delivery is formed.  Should be called after
1983e442f29fSKonstantin Belousov  * mach-specific routine, because sysent->sv_sendsig() needs correct
1984e442f29fSKonstantin Belousov  * ps_siginfo and signal mask.
1985e442f29fSKonstantin Belousov  */
1986e442f29fSKonstantin Belousov static void
1987e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps)
1988e442f29fSKonstantin Belousov {
1989e442f29fSKonstantin Belousov 	sigset_t mask;
1990e442f29fSKonstantin Belousov 
1991e442f29fSKonstantin Belousov 	mtx_assert(&ps->ps_mtx, MA_OWNED);
1992e442f29fSKonstantin Belousov 	td->td_ru.ru_nsignals++;
1993e442f29fSKonstantin Belousov 	mask = ps->ps_catchmask[_SIG_IDX(sig)];
1994e442f29fSKonstantin Belousov 	if (!SIGISMEMBER(ps->ps_signodefer, sig))
1995e442f29fSKonstantin Belousov 		SIGADDSET(mask, sig);
1996e442f29fSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1997e442f29fSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1998e442f29fSKonstantin Belousov 	if (SIGISMEMBER(ps->ps_sigreset, sig))
1999e442f29fSKonstantin Belousov 		sigdflt(ps, sig);
2000e442f29fSKonstantin Belousov }
2001e442f29fSKonstantin Belousov 
2002df8bae1dSRodney W. Grimes /*
20030c14ff0eSRobert Watson  * Send a signal caused by a trap to the current thread.  If it will be
20040c14ff0eSRobert Watson  * caught immediately, deliver it with correct code.  Otherwise, post it
20050c14ff0eSRobert Watson  * normally.
2006df8bae1dSRodney W. Grimes  */
2007df8bae1dSRodney W. Grimes void
20089104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi)
2009df8bae1dSRodney W. Grimes {
20101bf4700bSJeff Roberson 	struct sigacts *ps;
20111bf4700bSJeff Roberson 	struct proc *p;
2012146fc63fSKonstantin Belousov 	sigset_t sigmask;
2013146fc63fSKonstantin Belousov 	int code, sig;
20141bf4700bSJeff Roberson 
20151bf4700bSJeff Roberson 	p = td->td_proc;
20169104847fSDavid Xu 	sig = ksi->ksi_signo;
20179104847fSDavid Xu 	code = ksi->ksi_code;
20189104847fSDavid Xu 	KASSERT(_SIG_VALID(sig), ("invalid signal"));
20199104847fSDavid Xu 
2020a113b17fSKonstantin Belousov 	sigfastblock_fetch(td);
2021628d2653SJohn Baldwin 	PROC_LOCK(p);
2022ef3dab76STim J. Robbins 	ps = p->p_sigacts;
202390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
2024146fc63fSKonstantin Belousov 	sigmask = td->td_sigmask;
2025146fc63fSKonstantin Belousov 	if (td->td_sigblock_val != 0)
2026146fc63fSKonstantin Belousov 		SIGSETOR(sigmask, fastblock_mask);
202790af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2028146fc63fSKonstantin Belousov 	    !SIGISMEMBER(sigmask, sig)) {
2029df8bae1dSRodney W. Grimes #ifdef KTRACE
2030374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
2031374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
20324093529dSJeff Roberson 			    &td->td_sigmask, code);
2033df8bae1dSRodney W. Grimes #endif
20349104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
20359104847fSDavid Xu 				ksi, &td->td_sigmask);
2036e442f29fSKonstantin Belousov 		postsig_done(sig, td, ps);
203790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
20386f841fb7SMarcel Moolenaar 	} else {
2039f71a882fSDavid Xu 		/*
2040f71a882fSDavid Xu 		 * Avoid a possible infinite loop if the thread
2041f71a882fSDavid Xu 		 * masking the signal or process is ignoring the
2042f71a882fSDavid Xu 		 * signal.
2043f71a882fSDavid Xu 		 */
2044146fc63fSKonstantin Belousov 		if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2045f71a882fSDavid Xu 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2046f71a882fSDavid Xu 			SIGDELSET(td->td_sigmask, sig);
2047f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigcatch, sig);
2048f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigignore, sig);
2049f71a882fSDavid Xu 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2050146fc63fSKonstantin Belousov 			td->td_pflags &= ~TDP_SIGFASTBLOCK;
2051146fc63fSKonstantin Belousov 			td->td_sigblock_val = 0;
2052f71a882fSDavid Xu 		}
205390af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
20542c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
2055ad6eec7bSJohn Baldwin 		tdsendsignal(p, td, sig, ksi);
2056df8bae1dSRodney W. Grimes 	}
2057628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2058df8bae1dSRodney W. Grimes }
2059df8bae1dSRodney W. Grimes 
20604093529dSJeff Roberson static struct thread *
2061146fc63fSKonstantin Belousov sigtd(struct proc *p, int sig, bool fast_sigblock)
20624093529dSJeff Roberson {
20633074d1b4SDavid Xu 	struct thread *td, *signal_td;
20644093529dSJeff Roberson 
20654093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
2066146fc63fSKonstantin Belousov 	MPASS(!fast_sigblock || p == curproc);
20674093529dSJeff Roberson 
20684093529dSJeff Roberson 	/*
2069627451c1SDavid Xu 	 * Check if current thread can handle the signal without
207015b7a831SKonstantin Belousov 	 * switching context to another thread.
20714093529dSJeff Roberson 	 */
2072146fc63fSKonstantin Belousov 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2073146fc63fSKonstantin Belousov 	    (!fast_sigblock || curthread->td_sigblock_val == 0))
2074627451c1SDavid Xu 		return (curthread);
20753074d1b4SDavid Xu 	signal_td = NULL;
20763074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
2077146fc63fSKonstantin Belousov 		if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2078146fc63fSKonstantin Belousov 		    td != curthread || td->td_sigblock_val == 0)) {
20793074d1b4SDavid Xu 			signal_td = td;
2080627451c1SDavid Xu 			break;
20813074d1b4SDavid Xu 		}
20823074d1b4SDavid Xu 	}
20833074d1b4SDavid Xu 	if (signal_td == NULL)
20843074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
20853074d1b4SDavid Xu 	return (signal_td);
20864093529dSJeff Roberson }
20874093529dSJeff Roberson 
2088df8bae1dSRodney W. Grimes /*
2089df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
2090df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
2091df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
2092df8bae1dSRodney W. Grimes  *
2093df8bae1dSRodney W. Grimes  * Exceptions:
2094df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
2095df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
2096df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
2097df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
2098df8bae1dSRodney W. Grimes  *
2099df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
21004dec0e67SRobert Watson  *
21014dec0e67SRobert Watson  * NB: This function may be entered from the debugger via the "kill" DDB
21024dec0e67SRobert Watson  * command.  There is little that can be done to mitigate the possibly messy
21034dec0e67SRobert Watson  * side effects of this unwise possibility.
2104df8bae1dSRodney W. Grimes  */
2105df8bae1dSRodney W. Grimes void
21068451d0ddSKip Macy kern_psignal(struct proc *p, int sig)
2107df8bae1dSRodney W. Grimes {
2108a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
2109a3de221dSKonstantin Belousov 
2110a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
2111a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
2112a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
2113ad6eec7bSJohn Baldwin 	(void) tdsendsignal(p, NULL, sig, &ksi);
2114a3de221dSKonstantin Belousov }
2115a3de221dSKonstantin Belousov 
2116ad6eec7bSJohn Baldwin int
2117a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2118a3de221dSKonstantin Belousov {
2119a3de221dSKonstantin Belousov 
2120ad6eec7bSJohn Baldwin 	return (tdsendsignal(p, NULL, sig, ksi));
21219104847fSDavid Xu }
21229104847fSDavid Xu 
2123cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */
21249104847fSDavid Xu int
2125cf7d9a8cSDavid Xu sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
21269104847fSDavid Xu {
2127cf7d9a8cSDavid Xu 	struct thread *td;
212841b3077aSJacques Vidrine 
21296d7b314bSDavid Xu 	if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2130cf7d9a8cSDavid Xu 		td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
21316d7b314bSDavid Xu 		if (td == NULL)
21326d7b314bSDavid Xu 			return (ESRCH);
2133cf7d9a8cSDavid Xu 		*ttd = td;
2134cf7d9a8cSDavid Xu 	} else {
2135cf7d9a8cSDavid Xu 		*ttd = NULL;
2136cf7d9a8cSDavid Xu 		PROC_LOCK(p);
21376d7b314bSDavid Xu 	}
2138cf7d9a8cSDavid Xu 	return (0);
21394093529dSJeff Roberson }
21404093529dSJeff Roberson 
2141ad6eec7bSJohn Baldwin void
2142ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig)
2143ad6eec7bSJohn Baldwin {
2144ad6eec7bSJohn Baldwin 	ksiginfo_t ksi;
2145ad6eec7bSJohn Baldwin 
2146ad6eec7bSJohn Baldwin 	ksiginfo_init(&ksi);
2147ad6eec7bSJohn Baldwin 	ksi.ksi_signo = sig;
2148ad6eec7bSJohn Baldwin 	ksi.ksi_code = SI_KERNEL;
2149ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, &ksi);
2150ad6eec7bSJohn Baldwin }
2151ad6eec7bSJohn Baldwin 
2152ad6eec7bSJohn Baldwin void
2153ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2154ad6eec7bSJohn Baldwin {
2155ad6eec7bSJohn Baldwin 
2156ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, ksi);
2157ad6eec7bSJohn Baldwin }
2158ad6eec7bSJohn Baldwin 
2159*9b86d3e5SKonstantin Belousov static int
2160*9b86d3e5SKonstantin Belousov sig_sleepq_abort(struct thread *td, int intrval)
2161*9b86d3e5SKonstantin Belousov {
2162*9b86d3e5SKonstantin Belousov 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2163*9b86d3e5SKonstantin Belousov 
2164*9b86d3e5SKonstantin Belousov 	if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
2165*9b86d3e5SKonstantin Belousov 		thread_unlock(td);
2166*9b86d3e5SKonstantin Belousov 		return (0);
2167*9b86d3e5SKonstantin Belousov 	}
2168*9b86d3e5SKonstantin Belousov 	return (sleepq_abort(td, intrval));
2169*9b86d3e5SKonstantin Belousov }
2170*9b86d3e5SKonstantin Belousov 
2171cf7d9a8cSDavid Xu int
2172ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
21734093529dSJeff Roberson {
21749104847fSDavid Xu 	sig_t action;
21759104847fSDavid Xu 	sigqueue_t *sigqueue;
21769104847fSDavid Xu 	int prop;
217790af4afaSJohn Baldwin 	struct sigacts *ps;
217894f0972bSDavid Xu 	int intrval;
21799104847fSDavid Xu 	int ret = 0;
2180da7bbd2cSJohn Baldwin 	int wakeup_swapper;
2181df8bae1dSRodney W. Grimes 
2182cf7d9a8cSDavid Xu 	MPASS(td == NULL || p == td->td_proc);
21836d7b314bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
21846d7b314bSDavid Xu 
218541b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
2186212bc4b3SDavid Xu 		panic("%s(): invalid signal %d", __func__, sig);
21874093529dSJeff Roberson 
2188212bc4b3SDavid Xu 	KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
21896d7b314bSDavid Xu 
21906d7b314bSDavid Xu 	/*
21916d7b314bSDavid Xu 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
21926d7b314bSDavid Xu 	 */
21936d7b314bSDavid Xu 	if (p->p_state == PRS_ZOMBIE) {
21946d7b314bSDavid Xu 		if (ksi && (ksi->ksi_flags & KSI_INS))
21956d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
21966d7b314bSDavid Xu 		return (ret);
21976d7b314bSDavid Xu 	}
21986d7b314bSDavid Xu 
219990af4afaSJohn Baldwin 	ps = p->p_sigacts;
22009e590ff0SKonstantin Belousov 	KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
22012c42a146SMarcel Moolenaar 	prop = sigprop(sig);
22024093529dSJeff Roberson 
22036d7b314bSDavid Xu 	if (td == NULL) {
2204146fc63fSKonstantin Belousov 		td = sigtd(p, sig, false);
22056d7b314bSDavid Xu 		sigqueue = &p->p_sigqueue;
2206462314b3SMateusz Guzik 	} else
22079104847fSDavid Xu 		sigqueue = &td->td_sigqueue;
22084093529dSJeff Roberson 
220936160958SMark Johnston 	SDT_PROBE3(proc, , , signal__send, td, p, sig);
22105d217f17SJohn Birrell 
2211df8bae1dSRodney W. Grimes 	/*
2212bc387624SKonstantin Belousov 	 * If the signal is being ignored, then we forget about it
2213bc387624SKonstantin Belousov 	 * immediately, except when the target process executes
2214bc387624SKonstantin Belousov 	 * sigwait().  (Note: we don't set SIGCONT in ps_sigignore,
2215bc387624SKonstantin Belousov 	 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2216df8bae1dSRodney W. Grimes 	 */
221790af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
22180fc32899SJohn Baldwin 	if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2219bc387624SKonstantin Belousov 		if (kern_sig_discard_ign &&
2220bc387624SKonstantin Belousov 		    (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
222136160958SMark Johnston 			SDT_PROBE3(proc, , , signal__discard, td, p, sig);
22225d217f17SJohn Birrell 
222390af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
22246d7b314bSDavid Xu 			if (ksi && (ksi->ksi_flags & KSI_INS))
22256d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
22269104847fSDavid Xu 			return (ret);
2227bc387624SKonstantin Belousov 		} else {
2228bc387624SKonstantin Belousov 			action = SIG_CATCH;
2229f17eb93dSKonstantin Belousov 			intrval = 0;
223090af4afaSJohn Baldwin 		}
2231f17eb93dSKonstantin Belousov 	} else {
2232f17eb93dSKonstantin Belousov 		if (SIGISMEMBER(td->td_sigmask, sig))
2233df8bae1dSRodney W. Grimes 			action = SIG_HOLD;
223490af4afaSJohn Baldwin 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2235df8bae1dSRodney W. Grimes 			action = SIG_CATCH;
2236df8bae1dSRodney W. Grimes 		else
2237df8bae1dSRodney W. Grimes 			action = SIG_DFL;
223894f0972bSDavid Xu 		if (SIGISMEMBER(ps->ps_sigintr, sig))
223994f0972bSDavid Xu 			intrval = EINTR;
224094f0972bSDavid Xu 		else
224194f0972bSDavid Xu 			intrval = ERESTART;
2242f17eb93dSKonstantin Belousov 	}
224390af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
2244df8bae1dSRodney W. Grimes 
2245fd50a707SBrooks Davis 	if (prop & SIGPROP_CONT)
22469104847fSDavid Xu 		sigqueue_delete_stopmask_proc(p);
2247fd50a707SBrooks Davis 	else if (prop & SIGPROP_STOP) {
2248df8bae1dSRodney W. Grimes 		/*
2249df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
2250df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
2251df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
2252df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
2253df8bae1dSRodney W. Grimes 		 */
2254993a1699SKonstantin Belousov 		if ((prop & SIGPROP_TTYSTOP) != 0 &&
22555844bd05SKonstantin Belousov 		    (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2256993a1699SKonstantin Belousov 		    action == SIG_DFL) {
22576d7b314bSDavid Xu 			if (ksi && (ksi->ksi_flags & KSI_INS))
22586d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
22599104847fSDavid Xu 			return (ret);
22606d7b314bSDavid Xu 		}
22619104847fSDavid Xu 		sigqueue_delete_proc(p, SIGCONT);
2262ebceaf6dSDavid Xu 		if (p->p_flag & P_CONTINUED) {
22636933e3c1SJulian Elischer 			p->p_flag &= ~P_CONTINUED;
2264ebceaf6dSDavid Xu 			PROC_LOCK(p->p_pptr);
2265ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
2266ebceaf6dSDavid Xu 			PROC_UNLOCK(p->p_pptr);
2267ebceaf6dSDavid Xu 		}
2268df8bae1dSRodney W. Grimes 	}
22693074d1b4SDavid Xu 
22709104847fSDavid Xu 	ret = sigqueue_add(sigqueue, sig, ksi);
22719104847fSDavid Xu 	if (ret != 0)
22729104847fSDavid Xu 		return (ret);
22736d7b314bSDavid Xu 	signotify(td);
22745312b1c7SDavid Xu 	/*
22755312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
22765312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
22775312b1c7SDavid Xu 	 */
22785312b1c7SDavid Xu 	if (action == SIG_HOLD &&
2279fd50a707SBrooks Davis 	    !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
22809104847fSDavid Xu 		return (ret);
2281b4d33259SEric Badger 
228261a74c5cSJeff Roberson 	wakeup_swapper = 0;
228361a74c5cSJeff Roberson 
228490d75f78SAlfred Perlstein 	/*
2285e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
2286e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
2287e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
22881968f37bSJohn Baldwin 	 * times when processing needs to be done immediately, such as
2289e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
22901968f37bSJohn Baldwin 	 * We try to do the per-process part here.
2291df8bae1dSRodney W. Grimes 	 */
2292e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
22930f14f15bSJohn Baldwin 		KASSERT(!(p->p_flag & P_WEXIT),
22940f14f15bSJohn Baldwin 		    ("signal to stopped but exiting process"));
2295e602ba25SJulian Elischer 		if (sig == SIGKILL) {
2296137cf33dSDavid Xu 			/*
2297137cf33dSDavid Xu 			 * If traced process is already stopped,
2298137cf33dSDavid Xu 			 * then no further action is necessary.
2299137cf33dSDavid Xu 			 */
230083b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
230183b718ebSDavid Xu 				goto out;
2302df8bae1dSRodney W. Grimes 			/*
2303e602ba25SJulian Elischer 			 * SIGKILL sets process running.
2304e602ba25SJulian Elischer 			 * It will die elsewhere.
2305e602ba25SJulian Elischer 			 * All threads must be restarted.
2306df8bae1dSRodney W. Grimes 			 */
2307482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
2308e602ba25SJulian Elischer 			goto runfast;
2309e602ba25SJulian Elischer 		}
2310e602ba25SJulian Elischer 
2311fd50a707SBrooks Davis 		if (prop & SIGPROP_CONT) {
2312137cf33dSDavid Xu 			/*
2313137cf33dSDavid Xu 			 * If traced process is already stopped,
2314137cf33dSDavid Xu 			 * then no further action is necessary.
2315137cf33dSDavid Xu 			 */
231683b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
231783b718ebSDavid Xu 				goto out;
2318e602ba25SJulian Elischer 			/*
2319e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
23209104847fSDavid Xu 			 * process but don't leave the signal in sigqueue as
23211d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
2322e602ba25SJulian Elischer 			 * continue the process and leave the signal in
23239104847fSDavid Xu 			 * sigqueue.  If the process catches SIGCONT, let it
2324e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
2325e602ba25SJulian Elischer 			 * an event, it goes back to run state.
2326e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
2327e602ba25SJulian Elischer 			 */
23281279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
23297b4a950aSDavid Xu 			PROC_SLOCK(p);
2330ebceaf6dSDavid Xu 			if (p->p_numthreads == p->p_suspcount) {
23317b4a950aSDavid Xu 				PROC_SUNLOCK(p);
23326933e3c1SJulian Elischer 				p->p_flag |= P_CONTINUED;
2333b4490c6eSKonstantin Belousov 				p->p_xsig = SIGCONT;
23347f96995eSDavid Xu 				PROC_LOCK(p->p_pptr);
2335ebceaf6dSDavid Xu 				childproc_continued(p);
23367f96995eSDavid Xu 				PROC_UNLOCK(p->p_pptr);
23377b4a950aSDavid Xu 				PROC_SLOCK(p);
2338ebceaf6dSDavid Xu 			}
2339e602ba25SJulian Elischer 			if (action == SIG_DFL) {
2340a54e85fdSJeff Roberson 				thread_unsuspend(p);
23417b4a950aSDavid Xu 				PROC_SUNLOCK(p);
23429104847fSDavid Xu 				sigqueue_delete(sigqueue, sig);
2343dc47fdf1SKonstantin Belousov 				goto out_cont;
2344a54e85fdSJeff Roberson 			}
2345a54e85fdSJeff Roberson 			if (action == SIG_CATCH) {
23468460a577SJohn Birrell 				/*
23478460a577SJohn Birrell 				 * The process wants to catch it so it needs
23488460a577SJohn Birrell 				 * to run at least one thread, but which one?
23498460a577SJohn Birrell 				 */
23507b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2351e602ba25SJulian Elischer 				goto runfast;
2352e602ba25SJulian Elischer 			}
2353e602ba25SJulian Elischer 			/*
2354e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
2355e602ba25SJulian Elischer 			 */
235604774f23SJulian Elischer 			thread_unsuspend(p);
23577b4a950aSDavid Xu 			PROC_SUNLOCK(p);
2358dc47fdf1SKonstantin Belousov 			goto out_cont;
2359e602ba25SJulian Elischer 		}
2360e602ba25SJulian Elischer 
2361fd50a707SBrooks Davis 		if (prop & SIGPROP_STOP) {
2362137cf33dSDavid Xu 			/*
2363137cf33dSDavid Xu 			 * If traced process is already stopped,
2364137cf33dSDavid Xu 			 * then no further action is necessary.
2365137cf33dSDavid Xu 			 */
236683b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
236783b718ebSDavid Xu 				goto out;
2368e602ba25SJulian Elischer 			/*
2369e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
2370e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
237104774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
2372e602ba25SJulian Elischer 			 */
23731279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
23749104847fSDavid Xu 			sigqueue_delete(sigqueue, sig);
2375e602ba25SJulian Elischer 			goto out;
2376e602ba25SJulian Elischer 		}
2377e602ba25SJulian Elischer 
2378e602ba25SJulian Elischer 		/*
2379e602ba25SJulian Elischer 		 * All other kinds of signals:
2380e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
2381e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
2382e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
238304774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
2384e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
2385e602ba25SJulian Elischer 		 */
23867b4a950aSDavid Xu 		PROC_SLOCK(p);
2387a54e85fdSJeff Roberson 		thread_lock(td);
238861a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td))
2389*9b86d3e5SKonstantin Belousov 			wakeup_swapper = sig_sleepq_abort(td, intrval);
239061a74c5cSJeff Roberson 		else
2391a54e85fdSJeff Roberson 			thread_unlock(td);
23927b4a950aSDavid Xu 		PROC_SUNLOCK(p);
2393df8bae1dSRodney W. Grimes 		goto out;
2394df8bae1dSRodney W. Grimes 		/*
23959a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
23969a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
2397df8bae1dSRodney W. Grimes 		 */
2398e602ba25SJulian Elischer 	} else if (p->p_state == PRS_NORMAL) {
2399ec8297bdSDavid Xu 		if (p->p_flag & P_TRACED || action == SIG_CATCH) {
240094f0972bSDavid Xu 			tdsigwakeup(td, sig, action, intrval);
2401df8bae1dSRodney W. Grimes 			goto out;
240204774f23SJulian Elischer 		}
2403ec8297bdSDavid Xu 
2404ec8297bdSDavid Xu 		MPASS(action == SIG_DFL);
2405ec8297bdSDavid Xu 
2406fd50a707SBrooks Davis 		if (prop & SIGPROP_STOP) {
24070f14f15bSJohn Baldwin 			if (p->p_flag & (P_PPWAIT|P_WEXIT))
240835c32a76SDavid Xu 				goto out;
2409e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
2410b4490c6eSKonstantin Belousov 			p->p_xsig = sig;
24117b4a950aSDavid Xu 			PROC_SLOCK(p);
24126f56cb8dSKonstantin Belousov 			wakeup_swapper = sig_suspend_threads(td, p, 1);
24134093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
2414ebceaf6dSDavid Xu 				/*
2415ebceaf6dSDavid Xu 				 * only thread sending signal to another
2416ebceaf6dSDavid Xu 				 * process can reach here, if thread is sending
2417ebceaf6dSDavid Xu 				 * signal to its process, because thread does
2418ebceaf6dSDavid Xu 				 * not suspend itself here, p_numthreads
2419ebceaf6dSDavid Xu 				 * should never be equal to p_suspcount.
2420ebceaf6dSDavid Xu 				 */
2421ebceaf6dSDavid Xu 				thread_stopped(p);
24227b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2423b4490c6eSKonstantin Belousov 				sigqueue_delete_proc(p, p->p_xsig);
24247b4a950aSDavid Xu 			} else
24257b4a950aSDavid Xu 				PROC_SUNLOCK(p);
242635c32a76SDavid Xu 			goto out;
242735c32a76SDavid Xu 		}
2428e602ba25SJulian Elischer 	} else {
2429e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
24309104847fSDavid Xu 		sigqueue_delete(sigqueue, sig);
2431e602ba25SJulian Elischer 		goto out;
2432e602ba25SJulian Elischer 	}
2433e602ba25SJulian Elischer 
2434b40ce416SJulian Elischer 	/*
2435e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
2436e602ba25SJulian Elischer 	 * running threads.
2437b40ce416SJulian Elischer 	 */
2438e602ba25SJulian Elischer runfast:
243994f0972bSDavid Xu 	tdsigwakeup(td, sig, action, intrval);
24407b4a950aSDavid Xu 	PROC_SLOCK(p);
2441e602ba25SJulian Elischer 	thread_unsuspend(p);
24427b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2443dc47fdf1SKonstantin Belousov out_cont:
2444dc47fdf1SKonstantin Belousov 	itimer_proc_continue(p);
24452fd1ffefSKonstantin Belousov 	kqtimer_proc_continue(p);
2446e602ba25SJulian Elischer out:
24477b4a950aSDavid Xu 	/* If we jump here, proc slock should not be owned. */
24487b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
244961a74c5cSJeff Roberson 	if (wakeup_swapper)
245061a74c5cSJeff Roberson 		kick_proc0();
245161a74c5cSJeff Roberson 
24529104847fSDavid Xu 	return (ret);
2453e602ba25SJulian Elischer }
2454e602ba25SJulian Elischer 
2455e602ba25SJulian Elischer /*
2456e602ba25SJulian Elischer  * The force of a signal has been directed against a single
2457e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
2458e602ba25SJulian Elischer  * out of any sleep it may be in etc.
2459e602ba25SJulian Elischer  */
2460e602ba25SJulian Elischer static void
246194f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2462e602ba25SJulian Elischer {
2463e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
246461a74c5cSJeff Roberson 	int prop, wakeup_swapper;
2465e602ba25SJulian Elischer 
24668b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2467e602ba25SJulian Elischer 	prop = sigprop(sig);
2468a4c2da15SBruce Evans 
24697b4a950aSDavid Xu 	PROC_SLOCK(p);
2470374ae2a3SJeff Roberson 	thread_lock(td);
2471e602ba25SJulian Elischer 	/*
2472aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
2473aef68c96SKonstantin Belousov 	 * killed in this lifetime.  Be careful to avoid bumping the
2474aef68c96SKonstantin Belousov 	 * priority of the idle thread, since we still allow to signal
2475aef68c96SKonstantin Belousov 	 * kernel processes.
2476e602ba25SJulian Elischer 	 */
2477fd50a707SBrooks Davis 	if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2478aef68c96SKonstantin Belousov 	    td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2479b3a4fb14SDavid Xu 		sched_prio(td, PUSER);
248080c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
2481e602ba25SJulian Elischer 		/*
2482e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
2483e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
2484e602ba25SJulian Elischer 		 * be noticed when the process returns through
2485e602ba25SJulian Elischer 		 * trap() or syscall().
2486e602ba25SJulian Elischer 		 */
248744f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
2488374ae2a3SJeff Roberson 			goto out;
2489e602ba25SJulian Elischer 		/*
2490e602ba25SJulian Elischer 		 * If SIGCONT is default (or ignored) and process is
2491e602ba25SJulian Elischer 		 * asleep, we are finished; the process should not
2492e602ba25SJulian Elischer 		 * be awakened.
2493df8bae1dSRodney W. Grimes 		 */
2494fd50a707SBrooks Davis 		if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2495a54e85fdSJeff Roberson 			thread_unlock(td);
24967b4a950aSDavid Xu 			PROC_SUNLOCK(p);
24979104847fSDavid Xu 			sigqueue_delete(&p->p_sigqueue, sig);
24984093529dSJeff Roberson 			/*
24994093529dSJeff Roberson 			 * It may be on either list in this state.
25004093529dSJeff Roberson 			 * Remove from both for now.
25014093529dSJeff Roberson 			 */
25029104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
2503aa0fa334SJulian Elischer 			return;
2504df8bae1dSRodney W. Grimes 		}
2505df8bae1dSRodney W. Grimes 
2506aa0fa334SJulian Elischer 		/*
2507a120a7a3SJohn Baldwin 		 * Don't awaken a sleeping thread for SIGSTOP if the
2508a120a7a3SJohn Baldwin 		 * STOP signal is deferred.
2509a120a7a3SJohn Baldwin 		 */
2510fd50a707SBrooks Davis 		if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
25116f56cb8dSKonstantin Belousov 		    TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2512a120a7a3SJohn Baldwin 			goto out;
2513a120a7a3SJohn Baldwin 
2514a120a7a3SJohn Baldwin 		/*
2515a4c2da15SBruce Evans 		 * Give low priority threads a better chance to run.
2516aa0fa334SJulian Elischer 		 */
2517aef68c96SKonstantin Belousov 		if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2518b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
2519ec8297bdSDavid Xu 
2520*9b86d3e5SKonstantin Belousov 		wakeup_swapper = sig_sleepq_abort(td, intrval);
252161a74c5cSJeff Roberson 		PROC_SUNLOCK(p);
252261a74c5cSJeff Roberson 		if (wakeup_swapper)
252361a74c5cSJeff Roberson 			kick_proc0();
252461a74c5cSJeff Roberson 		return;
252561a74c5cSJeff Roberson 	}
252661a74c5cSJeff Roberson 
2527df8bae1dSRodney W. Grimes 	/*
2528a4c2da15SBruce Evans 	 * Other states do nothing with the signal immediately,
2529df8bae1dSRodney W. Grimes 	 * other than kicking ourselves if we are running.
2530df8bae1dSRodney W. Grimes 	 * It will either never be noticed, or noticed very soon.
2531df8bae1dSRodney W. Grimes 	 */
2532a4c2da15SBruce Evans #ifdef SMP
253344f3b092SJohn Baldwin 	if (TD_IS_RUNNING(td) && td != curthread)
2534e602ba25SJulian Elischer 		forward_signal(td);
25353163861cSTor Egge #endif
253661a74c5cSJeff Roberson 
2537374ae2a3SJeff Roberson out:
25387b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2539374ae2a3SJeff Roberson 	thread_unlock(td);
2540a4c2da15SBruce Evans }
2541df8bae1dSRodney W. Grimes 
254287a64872SKonstantin Belousov static void
254387a64872SKonstantin Belousov ptrace_coredump(struct thread *td)
254487a64872SKonstantin Belousov {
254587a64872SKonstantin Belousov 	struct proc *p;
254687a64872SKonstantin Belousov 	struct thr_coredump_req *tcq;
254787a64872SKonstantin Belousov 	void *rl_cookie;
254887a64872SKonstantin Belousov 
254987a64872SKonstantin Belousov 	MPASS(td == curthread);
255087a64872SKonstantin Belousov 	p = td->td_proc;
255187a64872SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
255287a64872SKonstantin Belousov 	if ((td->td_dbgflags & TDB_COREDUMPRQ) == 0)
255387a64872SKonstantin Belousov 		return;
255487a64872SKonstantin Belousov 	KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
255587a64872SKonstantin Belousov 
255687a64872SKonstantin Belousov 	tcq = td->td_coredump;
255787a64872SKonstantin Belousov 	KASSERT(tcq != NULL, ("td_coredump is NULL"));
255887a64872SKonstantin Belousov 
255987a64872SKonstantin Belousov 	if (p->p_sysent->sv_coredump == NULL) {
256087a64872SKonstantin Belousov 		tcq->tc_error = ENOSYS;
256187a64872SKonstantin Belousov 		goto wake;
256287a64872SKonstantin Belousov 	}
256387a64872SKonstantin Belousov 
256487a64872SKonstantin Belousov 	PROC_UNLOCK(p);
256587a64872SKonstantin Belousov 	rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
256687a64872SKonstantin Belousov 
256787a64872SKonstantin Belousov 	tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
256887a64872SKonstantin Belousov 	    tcq->tc_limit, tcq->tc_flags);
256987a64872SKonstantin Belousov 
257087a64872SKonstantin Belousov 	vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
257187a64872SKonstantin Belousov 	PROC_LOCK(p);
257287a64872SKonstantin Belousov wake:
257387a64872SKonstantin Belousov 	td->td_dbgflags &= ~TDB_COREDUMPRQ;
257487a64872SKonstantin Belousov 	td->td_coredump = NULL;
257587a64872SKonstantin Belousov 	wakeup(p);
257687a64872SKonstantin Belousov }
257787a64872SKonstantin Belousov 
25786f56cb8dSKonstantin Belousov static int
2579d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2580d8267df7SDavid Xu {
2581d8267df7SDavid Xu 	struct thread *td2;
25826f56cb8dSKonstantin Belousov 	int wakeup_swapper;
2583d8267df7SDavid Xu 
2584d8267df7SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
25857b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
25860c46712cSMark Johnston 	MPASS(sending || td == curthread);
2587d8267df7SDavid Xu 
25886f56cb8dSKonstantin Belousov 	wakeup_swapper = 0;
2589d8267df7SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td2) {
2590a54e85fdSJeff Roberson 		thread_lock(td2);
2591b7edba77SJeff Roberson 		td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2592d8267df7SDavid Xu 		if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2593f33a947bSKonstantin Belousov 		    (td2->td_flags & TDF_SINTR)) {
2594f33a947bSKonstantin Belousov 			if (td2->td_flags & TDF_SBDRY) {
2595a120a7a3SJohn Baldwin 				/*
2596a120a7a3SJohn Baldwin 				 * Once a thread is asleep with
25976f56cb8dSKonstantin Belousov 				 * TDF_SBDRY and without TDF_SERESTART
25986f56cb8dSKonstantin Belousov 				 * or TDF_SEINTR set, it should never
2599a120a7a3SJohn Baldwin 				 * become suspended due to this check.
2600a120a7a3SJohn Baldwin 				 */
2601a120a7a3SJohn Baldwin 				KASSERT(!TD_IS_SUSPENDED(td2),
2602a120a7a3SJohn Baldwin 				    ("thread with deferred stops suspended"));
260361a74c5cSJeff Roberson 				if (TD_SBDRY_INTR(td2)) {
260446e47c4fSKonstantin Belousov 					wakeup_swapper |= sleepq_abort(td2,
260546e47c4fSKonstantin Belousov 					    TD_SBDRY_ERRNO(td2));
260661a74c5cSJeff Roberson 					continue;
2607f33a947bSKonstantin Belousov 				}
260861a74c5cSJeff Roberson 			} else if (!TD_IS_SUSPENDED(td2))
260961a74c5cSJeff Roberson 				thread_suspend_one(td2);
2610f33a947bSKonstantin Belousov 		} else if (!TD_IS_SUSPENDED(td2)) {
2611d8267df7SDavid Xu 			if (sending || td != td2)
2612d8267df7SDavid Xu 				td2->td_flags |= TDF_ASTPENDING;
2613d8267df7SDavid Xu #ifdef SMP
2614d8267df7SDavid Xu 			if (TD_IS_RUNNING(td2) && td2 != td)
2615d8267df7SDavid Xu 				forward_signal(td2);
2616d8267df7SDavid Xu #endif
2617d8267df7SDavid Xu 		}
2618a54e85fdSJeff Roberson 		thread_unlock(td2);
2619d8267df7SDavid Xu 	}
26206f56cb8dSKonstantin Belousov 	return (wakeup_swapper);
2621d8267df7SDavid Xu }
2622d8267df7SDavid Xu 
262382a4538fSEric Badger /*
262482a4538fSEric Badger  * Stop the process for an event deemed interesting to the debugger. If si is
262582a4538fSEric Badger  * non-NULL, this is a signal exchange; the new signal requested by the
262682a4538fSEric Badger  * debugger will be returned for handling. If si is NULL, this is some other
262782a4538fSEric Badger  * type of interesting event. The debugger may request a signal be delivered in
262882a4538fSEric Badger  * that case as well, however it will be deferred until it can be handled.
262982a4538fSEric Badger  */
2630cbf4e354SDavid Xu int
263182a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si)
26324cc9f52fSRobert Drehmel {
26334cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
263482a4538fSEric Badger 	struct thread *td2;
263582a4538fSEric Badger 	ksiginfo_t ksi;
26364cc9f52fSRobert Drehmel 
263730a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
26380f14f15bSJohn Baldwin 	KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
26394cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2640aa89d8cdSJohn Baldwin 	    &p->p_mtx.lock_object, "Stopping for traced signal");
26414cc9f52fSRobert Drehmel 
2642cbf4e354SDavid Xu 	td->td_xsig = sig;
264382a4538fSEric Badger 
264482a4538fSEric Badger 	if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
264582a4538fSEric Badger 		td->td_dbgflags |= TDB_XSIG;
2646515b7a0bSJohn Baldwin 		CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2647515b7a0bSJohn Baldwin 		    td->td_tid, p->p_pid, td->td_dbgflags, sig);
26487b4a950aSDavid Xu 		PROC_SLOCK(p);
2649904c5ec4SDavid Xu 		while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
265082a4538fSEric Badger 			if (P_KILLED(p)) {
265182a4538fSEric Badger 				/*
265282a4538fSEric Badger 				 * Ensure that, if we've been PT_KILLed, the
265382a4538fSEric Badger 				 * exit status reflects that. Another thread
265482a4538fSEric Badger 				 * may also be in ptracestop(), having just
265582a4538fSEric Badger 				 * received the SIGKILL, but this thread was
265682a4538fSEric Badger 				 * unsuspended first.
265782a4538fSEric Badger 				 */
265882a4538fSEric Badger 				td->td_dbgflags &= ~TDB_XSIG;
265982a4538fSEric Badger 				td->td_xsig = SIGKILL;
266082a4538fSEric Badger 				p->p_ptevents = 0;
266182a4538fSEric Badger 				break;
266282a4538fSEric Badger 			}
26635fcfab6eSJohn Baldwin 			if (p->p_flag & P_SINGLE_EXIT &&
26645fcfab6eSJohn Baldwin 			    !(td->td_dbgflags & TDB_EXIT)) {
26655fcfab6eSJohn Baldwin 				/*
26665fcfab6eSJohn Baldwin 				 * Ignore ptrace stops except for thread exit
26675fcfab6eSJohn Baldwin 				 * events when the process exits.
26685fcfab6eSJohn Baldwin 				 */
2669904c5ec4SDavid Xu 				td->td_dbgflags &= ~TDB_XSIG;
26707b4a950aSDavid Xu 				PROC_SUNLOCK(p);
267182a4538fSEric Badger 				return (0);
2672cbf4e354SDavid Xu 			}
2673b7a25e63SKonstantin Belousov 
2674cbf4e354SDavid Xu 			/*
2675b7a25e63SKonstantin Belousov 			 * Make wait(2) work.  Ensure that right after the
2676b7a25e63SKonstantin Belousov 			 * attach, the thread which was decided to become the
2677b7a25e63SKonstantin Belousov 			 * leader of attach gets reported to the waiter.
2678b7a25e63SKonstantin Belousov 			 * Otherwise, just avoid overwriting another thread's
2679b7a25e63SKonstantin Belousov 			 * assignment to p_xthread.  If another thread has
2680b7a25e63SKonstantin Belousov 			 * already set p_xthread, the current thread will get
2681b7a25e63SKonstantin Belousov 			 * a chance to report itself upon the next iteration.
2682cbf4e354SDavid Xu 			 */
2683b7a25e63SKonstantin Belousov 			if ((td->td_dbgflags & TDB_FSTP) != 0 ||
26847f649ddaSMark Johnston 			    ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2685b7a25e63SKonstantin Belousov 			    p->p_xthread == NULL)) {
2686b4490c6eSKonstantin Belousov 				p->p_xsig = sig;
2687cbf4e354SDavid Xu 				p->p_xthread = td;
2688ab74c843SKonstantin Belousov 
2689ab74c843SKonstantin Belousov 				/*
2690ab74c843SKonstantin Belousov 				 * If we are on sleepqueue already,
2691ab74c843SKonstantin Belousov 				 * let sleepqueue code decide if it
2692ab74c843SKonstantin Belousov 				 * needs to go sleep after attach.
2693ab74c843SKonstantin Belousov 				 */
2694ab74c843SKonstantin Belousov 				if (td->td_wchan == NULL)
2695b7a25e63SKonstantin Belousov 					td->td_dbgflags &= ~TDB_FSTP;
2696ab74c843SKonstantin Belousov 
2697b7a25e63SKonstantin Belousov 				p->p_flag2 &= ~P2_PTRACE_FSTP;
2698b7a25e63SKonstantin Belousov 				p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2699d8267df7SDavid Xu 				sig_suspend_threads(td, p, 0);
2700b7a25e63SKonstantin Belousov 			}
27016fa39a73SKonstantin Belousov 			if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
27026fa39a73SKonstantin Belousov 				td->td_dbgflags &= ~TDB_STOPATFORK;
27036fa39a73SKonstantin Belousov 			}
2704cbf4e354SDavid Xu stopme:
270568d311b6SKonstantin Belousov 			td->td_dbgflags |= TDB_SSWITCH;
27066ddcc233SKonstantin Belousov 			thread_suspend_switch(td, p);
270768d311b6SKonstantin Belousov 			td->td_dbgflags &= ~TDB_SSWITCH;
270887a64872SKonstantin Belousov 			if ((td->td_dbgflags & TDB_COREDUMPRQ) != 0) {
270987a64872SKonstantin Belousov 				PROC_SUNLOCK(p);
271087a64872SKonstantin Belousov 				ptrace_coredump(td);
271187a64872SKonstantin Belousov 				PROC_SLOCK(p);
271287a64872SKonstantin Belousov 				goto stopme;
271387a64872SKonstantin Belousov 			}
27147ce60f60SDavid Xu 			if (p->p_xthread == td)
27157ce60f60SDavid Xu 				p->p_xthread = NULL;
27167ce60f60SDavid Xu 			if (!(p->p_flag & P_TRACED))
2717cbf4e354SDavid Xu 				break;
2718904c5ec4SDavid Xu 			if (td->td_dbgflags & TDB_SUSPEND) {
2719cbf4e354SDavid Xu 				if (p->p_flag & P_SINGLE_EXIT)
2720cbf4e354SDavid Xu 					break;
2721cbf4e354SDavid Xu 				goto stopme;
2722cbf4e354SDavid Xu 			}
2723cbf4e354SDavid Xu 		}
27247b4a950aSDavid Xu 		PROC_SUNLOCK(p);
272582a4538fSEric Badger 	}
272682a4538fSEric Badger 
272782a4538fSEric Badger 	if (si != NULL && sig == td->td_xsig) {
272882a4538fSEric Badger 		/* Parent wants us to take the original signal unchanged. */
272982a4538fSEric Badger 		si->ksi_flags |= KSI_HEAD;
273082a4538fSEric Badger 		if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
273182a4538fSEric Badger 			si->ksi_signo = 0;
273282a4538fSEric Badger 	} else if (td->td_xsig != 0) {
273382a4538fSEric Badger 		/*
273482a4538fSEric Badger 		 * If parent wants us to take a new signal, then it will leave
273582a4538fSEric Badger 		 * it in td->td_xsig; otherwise we just look for signals again.
273682a4538fSEric Badger 		 */
273782a4538fSEric Badger 		ksiginfo_init(&ksi);
273882a4538fSEric Badger 		ksi.ksi_signo = td->td_xsig;
273982a4538fSEric Badger 		ksi.ksi_flags |= KSI_PTRACE;
2740146fc63fSKonstantin Belousov 		td2 = sigtd(p, td->td_xsig, false);
274182a4538fSEric Badger 		tdsendsignal(p, td2, td->td_xsig, &ksi);
274282a4538fSEric Badger 		if (td != td2)
274382a4538fSEric Badger 			return (0);
274482a4538fSEric Badger 	}
274582a4538fSEric Badger 
2746cbf4e354SDavid Xu 	return (td->td_xsig);
27474cc9f52fSRobert Drehmel }
27484cc9f52fSRobert Drehmel 
27490bc52b0bSKonstantin Belousov static void
275080a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags)
27516b286ee8SKonstantin Belousov {
27526b286ee8SKonstantin Belousov 	struct sigacts *ps;
27536b286ee8SKonstantin Belousov 	struct thread *td;
2754407af02bSDavid Xu 	int sig;
2755146fc63fSKonstantin Belousov 	bool fastblk, pslocked;
27566b286ee8SKonstantin Belousov 
27576b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
275870778bbaSKonstantin Belousov 	ps = p->p_sigacts;
2759146fc63fSKonstantin Belousov 	pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2760146fc63fSKonstantin Belousov 	mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2761407af02bSDavid Xu 	if (SIGISEMPTY(p->p_siglist))
2762407af02bSDavid Xu 		return;
2763407af02bSDavid Xu 	SIGSETAND(block, p->p_siglist);
2764146fc63fSKonstantin Belousov 	fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2765407af02bSDavid Xu 	while ((sig = sig_ffs(&block)) != 0) {
2766407af02bSDavid Xu 		SIGDELSET(block, sig);
2767146fc63fSKonstantin Belousov 		td = sigtd(p, sig, fastblk);
2768146fc63fSKonstantin Belousov 
2769146fc63fSKonstantin Belousov 		/*
2770146fc63fSKonstantin Belousov 		 * If sigtd() selected us despite sigfastblock is
2771146fc63fSKonstantin Belousov 		 * blocking, do not activate AST or wake us, to avoid
2772146fc63fSKonstantin Belousov 		 * loop in AST handler.
2773146fc63fSKonstantin Belousov 		 */
2774146fc63fSKonstantin Belousov 		if (fastblk && td == curthread)
2775146fc63fSKonstantin Belousov 			continue;
2776146fc63fSKonstantin Belousov 
27776b286ee8SKonstantin Belousov 		signotify(td);
2778146fc63fSKonstantin Belousov 		if (!pslocked)
27796b286ee8SKonstantin Belousov 			mtx_lock(&ps->ps_mtx);
2780396a0d44SKonstantin Belousov 		if (p->p_flag & P_TRACED ||
2781396a0d44SKonstantin Belousov 		    (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2782146fc63fSKonstantin Belousov 		    !SIGISMEMBER(td->td_sigmask, sig))) {
2783407af02bSDavid Xu 			tdsigwakeup(td, sig, SIG_CATCH,
2784407af02bSDavid Xu 			    (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
27856b286ee8SKonstantin Belousov 			    ERESTART));
2786146fc63fSKonstantin Belousov 		}
2787146fc63fSKonstantin Belousov 		if (!pslocked)
27886b286ee8SKonstantin Belousov 			mtx_unlock(&ps->ps_mtx);
27896b286ee8SKonstantin Belousov 	}
27906b286ee8SKonstantin Belousov }
27916b286ee8SKonstantin Belousov 
27926b286ee8SKonstantin Belousov void
27936b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td)
27946b286ee8SKonstantin Belousov {
27956b286ee8SKonstantin Belousov 	struct proc *p;
27966b286ee8SKonstantin Belousov 	sigset_t unblocked;
27976b286ee8SKonstantin Belousov 
27986b286ee8SKonstantin Belousov 	p = td->td_proc;
27996b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
28006b286ee8SKonstantin Belousov 
28016b286ee8SKonstantin Belousov 	sigqueue_flush(&td->td_sigqueue);
28026b286ee8SKonstantin Belousov 	if (p->p_numthreads == 1)
28036b286ee8SKonstantin Belousov 		return;
28046b286ee8SKonstantin Belousov 
28056b286ee8SKonstantin Belousov 	/*
28066b286ee8SKonstantin Belousov 	 * Since we cannot handle signals, notify signal post code
28076b286ee8SKonstantin Belousov 	 * about this by filling the sigmask.
28086b286ee8SKonstantin Belousov 	 *
28096b286ee8SKonstantin Belousov 	 * Also, if needed, wake up thread(s) that do not block the
28106b286ee8SKonstantin Belousov 	 * same signals as the exiting thread, since the thread might
28116b286ee8SKonstantin Belousov 	 * have been selected for delivery and woken up.
28126b286ee8SKonstantin Belousov 	 */
28136b286ee8SKonstantin Belousov 	SIGFILLSET(unblocked);
28146b286ee8SKonstantin Belousov 	SIGSETNAND(unblocked, td->td_sigmask);
28156b286ee8SKonstantin Belousov 	SIGFILLSET(td->td_sigmask);
281680a8b0f3SKonstantin Belousov 	reschedule_signals(p, unblocked, 0);
28176b286ee8SKonstantin Belousov 
28186b286ee8SKonstantin Belousov }
28196b286ee8SKonstantin Belousov 
28203a1e5dd8SKonstantin Belousov static int
28213a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags)
2822a120a7a3SJohn Baldwin {
2823a120a7a3SJohn Baldwin 
28243a1e5dd8SKonstantin Belousov 	MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
28253a1e5dd8SKonstantin Belousov 	    (cflags & TDF_SBDRY) != 0);
28263a1e5dd8SKonstantin Belousov 	return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2827a120a7a3SJohn Baldwin }
2828a120a7a3SJohn Baldwin 
2829a120a7a3SJohn Baldwin /*
28303a1e5dd8SKonstantin Belousov  * Defer the delivery of SIGSTOP for the current thread, according to
28313a1e5dd8SKonstantin Belousov  * the requested mode.  Returns previous flags, which must be restored
28323a1e5dd8SKonstantin Belousov  * by sigallowstop().
28333a1e5dd8SKonstantin Belousov  *
28343a1e5dd8SKonstantin Belousov  * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
28353a1e5dd8SKonstantin Belousov  * cleared by the current thread, which allow the lock-less read-only
28363a1e5dd8SKonstantin Belousov  * accesses below.
2837a120a7a3SJohn Baldwin  */
2838e3612a4cSKonstantin Belousov int
283946e47c4fSKonstantin Belousov sigdeferstop_impl(int mode)
2840a120a7a3SJohn Baldwin {
2841593efaf9SJohn Baldwin 	struct thread *td;
28423a1e5dd8SKonstantin Belousov 	int cflags, nflags;
2843a120a7a3SJohn Baldwin 
2844593efaf9SJohn Baldwin 	td = curthread;
28453a1e5dd8SKonstantin Belousov 	cflags = sigdeferstop_curr_flags(td->td_flags);
28463a1e5dd8SKonstantin Belousov 	switch (mode) {
28473a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_NOP:
28483a1e5dd8SKonstantin Belousov 		nflags = cflags;
28493a1e5dd8SKonstantin Belousov 		break;
28503a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_OFF:
28513a1e5dd8SKonstantin Belousov 		nflags = 0;
28523a1e5dd8SKonstantin Belousov 		break;
28533a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_SILENT:
28543a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
28553a1e5dd8SKonstantin Belousov 		break;
28563a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_EINTR:
28573a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
28583a1e5dd8SKonstantin Belousov 		break;
28593a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_ERESTART:
28603a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
28613a1e5dd8SKonstantin Belousov 		break;
28623a1e5dd8SKonstantin Belousov 	default:
28633a1e5dd8SKonstantin Belousov 		panic("sigdeferstop: invalid mode %x", mode);
28643a1e5dd8SKonstantin Belousov 		break;
28653a1e5dd8SKonstantin Belousov 	}
286646e47c4fSKonstantin Belousov 	if (cflags == nflags)
286746e47c4fSKonstantin Belousov 		return (SIGDEFERSTOP_VAL_NCHG);
2868a120a7a3SJohn Baldwin 	thread_lock(td);
28693a1e5dd8SKonstantin Belousov 	td->td_flags = (td->td_flags & ~cflags) | nflags;
2870a120a7a3SJohn Baldwin 	thread_unlock(td);
28713a1e5dd8SKonstantin Belousov 	return (cflags);
28723a1e5dd8SKonstantin Belousov }
28733a1e5dd8SKonstantin Belousov 
28743a1e5dd8SKonstantin Belousov /*
28753a1e5dd8SKonstantin Belousov  * Restores the STOP handling mode, typically permitting the delivery
28763a1e5dd8SKonstantin Belousov  * of SIGSTOP for the current thread.  This does not immediately
28773a1e5dd8SKonstantin Belousov  * suspend if a stop was posted.  Instead, the thread will suspend
28783a1e5dd8SKonstantin Belousov  * either via ast() or a subsequent interruptible sleep.
28793a1e5dd8SKonstantin Belousov  */
28803a1e5dd8SKonstantin Belousov void
288146e47c4fSKonstantin Belousov sigallowstop_impl(int prev)
28823a1e5dd8SKonstantin Belousov {
28833a1e5dd8SKonstantin Belousov 	struct thread *td;
28843a1e5dd8SKonstantin Belousov 	int cflags;
28853a1e5dd8SKonstantin Belousov 
288646e47c4fSKonstantin Belousov 	KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
28873a1e5dd8SKonstantin Belousov 	KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
28883a1e5dd8SKonstantin Belousov 	    ("sigallowstop: incorrect previous mode %x", prev));
28893a1e5dd8SKonstantin Belousov 	td = curthread;
28903a1e5dd8SKonstantin Belousov 	cflags = sigdeferstop_curr_flags(td->td_flags);
28913a1e5dd8SKonstantin Belousov 	if (cflags != prev) {
28923a1e5dd8SKonstantin Belousov 		thread_lock(td);
28933a1e5dd8SKonstantin Belousov 		td->td_flags = (td->td_flags & ~cflags) | prev;
28943a1e5dd8SKonstantin Belousov 		thread_unlock(td);
28953a1e5dd8SKonstantin Belousov 	}
2896a120a7a3SJohn Baldwin }
2897a120a7a3SJohn Baldwin 
2898df8bae1dSRodney W. Grimes /*
2899df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2900df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2901df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2902df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2903df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2904628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2905df8bae1dSRodney W. Grimes  * sequence is
2906df8bae1dSRodney W. Grimes  *
2907e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
29082c42a146SMarcel Moolenaar  *		postsig(sig);
2909df8bae1dSRodney W. Grimes  */
29106711f10fSJohn Baldwin static int
29113cf3b9f0SJohn Baldwin issignal(struct thread *td)
2912df8bae1dSRodney W. Grimes {
2913e602ba25SJulian Elischer 	struct proc *p;
291490af4afaSJohn Baldwin 	struct sigacts *ps;
29156b286ee8SKonstantin Belousov 	struct sigqueue *queue;
29164093529dSJeff Roberson 	sigset_t sigpending;
291786be94fcSTycho Nightingale 	ksiginfo_t ksi;
291859838c1aSJohn Baldwin 	int prop, sig;
2919df8bae1dSRodney W. Grimes 
2920e602ba25SJulian Elischer 	p = td->td_proc;
292190af4afaSJohn Baldwin 	ps = p->p_sigacts;
292290af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2923628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2924df8bae1dSRodney W. Grimes 	for (;;) {
29259104847fSDavid Xu 		sigpending = td->td_sigqueue.sq_signals;
29266b286ee8SKonstantin Belousov 		SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
29274093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
29284093529dSJeff Roberson 
29296f56cb8dSKonstantin Belousov 		if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
29306f56cb8dSKonstantin Belousov 		    (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
29314093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
29324093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2933df8bae1dSRodney W. Grimes 			return (0);
2934146fc63fSKonstantin Belousov 
2935146fc63fSKonstantin Belousov 		/*
2936146fc63fSKonstantin Belousov 		 * Do fast sigblock if requested by usermode.  Since
2937146fc63fSKonstantin Belousov 		 * we do know that there was a signal pending at this
2938146fc63fSKonstantin Belousov 		 * point, set the FAST_SIGBLOCK_PEND as indicator for
2939146fc63fSKonstantin Belousov 		 * usermode to perform a dummy call to
2940146fc63fSKonstantin Belousov 		 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
2941146fc63fSKonstantin Belousov 		 * delivery of postponed pending signal.
2942146fc63fSKonstantin Belousov 		 */
2943146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
2944146fc63fSKonstantin Belousov 			if (td->td_sigblock_val != 0)
2945146fc63fSKonstantin Belousov 				SIGSETNAND(sigpending, fastblock_mask);
2946146fc63fSKonstantin Belousov 			if (SIGISEMPTY(sigpending)) {
2947146fc63fSKonstantin Belousov 				td->td_pflags |= TDP_SIGFASTPENDING;
2948146fc63fSKonstantin Belousov 				return (0);
2949146fc63fSKonstantin Belousov 			}
2950146fc63fSKonstantin Belousov 		}
2951146fc63fSKonstantin Belousov 
2952b7a25e63SKonstantin Belousov 		if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
2953b7a25e63SKonstantin Belousov 		    (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
2954b7a25e63SKonstantin Belousov 		    SIGISMEMBER(sigpending, SIGSTOP)) {
2955b7a25e63SKonstantin Belousov 			/*
2956b7a25e63SKonstantin Belousov 			 * If debugger just attached, always consume
2957b7a25e63SKonstantin Belousov 			 * SIGSTOP from ptrace(PT_ATTACH) first, to
2958b7a25e63SKonstantin Belousov 			 * execute the debugger attach ritual in
2959b7a25e63SKonstantin Belousov 			 * order.
2960b7a25e63SKonstantin Belousov 			 */
2961b7a25e63SKonstantin Belousov 			sig = SIGSTOP;
2962b7a25e63SKonstantin Belousov 			td->td_dbgflags |= TDB_FSTP;
2963b7a25e63SKonstantin Belousov 		} else {
29644093529dSJeff Roberson 			sig = sig_ffs(&sigpending);
2965b7a25e63SKonstantin Belousov 		}
29662a024a2bSSean Eric Fagan 
2967df8bae1dSRodney W. Grimes 		/*
2968bc387624SKonstantin Belousov 		 * We should allow pending but ignored signals below
2969bc387624SKonstantin Belousov 		 * only if there is sigwait() active, or P_TRACED was
2970bc387624SKonstantin Belousov 		 * on when they were posted.
2971df8bae1dSRodney W. Grimes 		 */
297259838c1aSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) &&
2973bc387624SKonstantin Belousov 		    (p->p_flag & P_TRACED) == 0 &&
2974b599982bSKonstantin Belousov 		    (td->td_flags & TDF_SIGWAIT) == 0) {
29759104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
29766b286ee8SKonstantin Belousov 			sigqueue_delete(&p->p_sigqueue, sig);
2977df8bae1dSRodney W. Grimes 			continue;
2978df8bae1dSRodney W. Grimes 		}
2979b7a25e63SKonstantin Belousov 		if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
2980df8bae1dSRodney W. Grimes 			/*
2981d8f4f6a4SJonathan Mini 			 * If traced, always stop.
29826a671a6bSKonstantin Belousov 			 * Remove old signal from queue before the stop.
29836a671a6bSKonstantin Belousov 			 * XXX shrug off debugger, it causes siginfo to
29846a671a6bSKonstantin Belousov 			 * be thrown away.
2985df8bae1dSRodney W. Grimes 			 */
29866a671a6bSKonstantin Belousov 			queue = &td->td_sigqueue;
298786be94fcSTycho Nightingale 			ksiginfo_init(&ksi);
298886be94fcSTycho Nightingale 			if (sigqueue_get(queue, sig, &ksi) == 0) {
29896a671a6bSKonstantin Belousov 				queue = &p->p_sigqueue;
299086be94fcSTycho Nightingale 				sigqueue_get(queue, sig, &ksi);
29916a671a6bSKonstantin Belousov 			}
299286be94fcSTycho Nightingale 			td->td_si = ksi.ksi_info;
29936a671a6bSKonstantin Belousov 
299490af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
299586be94fcSTycho Nightingale 			sig = ptracestop(td, sig, &ksi);
299690af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2997df8bae1dSRodney W. Grimes 
2998c5786670SJohn Baldwin 			td->td_si.si_signo = 0;
2999c5786670SJohn Baldwin 
3000df8bae1dSRodney W. Grimes 			/*
3001e9445808SKonstantin Belousov 			 * Keep looking if the debugger discarded or
3002e9445808SKonstantin Belousov 			 * replaced the signal.
30038d542cb5SDavid E. O'Brien 			 */
3004e9445808SKonstantin Belousov 			if (sig == 0)
30058d542cb5SDavid E. O'Brien 				continue;
3006e9445808SKonstantin Belousov 
3007e9445808SKonstantin Belousov 			/*
3008e9445808SKonstantin Belousov 			 * If the signal became masked, re-queue it.
3009e9445808SKonstantin Belousov 			 */
3010e9445808SKonstantin Belousov 			if (SIGISMEMBER(td->td_sigmask, sig)) {
3011e9445808SKonstantin Belousov 				ksi.ksi_flags |= KSI_HEAD;
3012e9445808SKonstantin Belousov 				sigqueue_add(&p->p_sigqueue, sig, &ksi);
3013e9445808SKonstantin Belousov 				continue;
3014e9445808SKonstantin Belousov 			}
3015e9445808SKonstantin Belousov 
3016e9445808SKonstantin Belousov 			/*
3017e9445808SKonstantin Belousov 			 * If the traced bit got turned off, requeue
3018e9445808SKonstantin Belousov 			 * the signal and go back up to the top to
3019e9445808SKonstantin Belousov 			 * rescan signals.  This ensures that p_sig*
3020e9445808SKonstantin Belousov 			 * and p_sigact are consistent.
3021e9445808SKonstantin Belousov 			 */
3022e9445808SKonstantin Belousov 			if ((p->p_flag & P_TRACED) == 0) {
3023e9445808SKonstantin Belousov 				ksi.ksi_flags |= KSI_HEAD;
3024e9445808SKonstantin Belousov 				sigqueue_add(queue, sig, &ksi);
3025e9445808SKonstantin Belousov 				continue;
3026e9445808SKonstantin Belousov 			}
3027df8bae1dSRodney W. Grimes 		}
3028df8bae1dSRodney W. Grimes 
30298d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
30308d542cb5SDavid E. O'Brien 
3031df8bae1dSRodney W. Grimes 		/*
3032df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
3033df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
3034df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
3035df8bae1dSRodney W. Grimes 		 */
3036d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3037d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
3038df8bae1dSRodney W. Grimes 			/*
3039df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
3040df8bae1dSRodney W. Grimes 			 */
3041df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
3042df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
3043df8bae1dSRodney W. Grimes 				/*
3044df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
3045df8bae1dSRodney W. Grimes 				 * in init? XXX
3046df8bae1dSRodney W. Grimes 				 */
3047d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
30482c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
3049df8bae1dSRodney W. Grimes #endif
3050df8bae1dSRodney W. Grimes 				break;		/* == ignore */
3051df8bae1dSRodney W. Grimes 			}
3052df8bae1dSRodney W. Grimes 			/*
3053b38bd91fSEric Badger 			 * If there is a pending stop signal to process with
3054b38bd91fSEric Badger 			 * default action, stop here, then clear the signal.
3055b38bd91fSEric Badger 			 * Traced or exiting processes should ignore stops.
3056b38bd91fSEric Badger 			 * Additionally, a member of an orphaned process group
3057b38bd91fSEric Badger 			 * should ignore tty stops.
3058df8bae1dSRodney W. Grimes 			 */
3059fd50a707SBrooks Davis 			if (prop & SIGPROP_STOP) {
3060e0d83cd3SKonstantin Belousov 				mtx_unlock(&ps->ps_mtx);
3061993a1699SKonstantin Belousov 				if ((p->p_flag & (P_TRACED | P_WEXIT |
30625844bd05SKonstantin Belousov 				    P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
30635844bd05SKonstantin Belousov 				    pg_flags & PGRP_ORPHANED) != 0 &&
3064e0d83cd3SKonstantin Belousov 				    (prop & SIGPROP_TTYSTOP) != 0)) {
3065e0d83cd3SKonstantin Belousov 					mtx_lock(&ps->ps_mtx);
3066df8bae1dSRodney W. Grimes 					break;	/* == ignore */
3067e0d83cd3SKonstantin Belousov 				}
306846e47c4fSKonstantin Belousov 				if (TD_SBDRY_INTR(td)) {
306946e47c4fSKonstantin Belousov 					KASSERT((td->td_flags & TDF_SBDRY) != 0,
307046e47c4fSKonstantin Belousov 					    ("lost TDF_SBDRY"));
3071e0d83cd3SKonstantin Belousov 					mtx_lock(&ps->ps_mtx);
307246e47c4fSKonstantin Belousov 					return (-1);
307346e47c4fSKonstantin Belousov 				}
307490af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3075aa89d8cdSJohn Baldwin 				    &p->p_mtx.lock_object, "Catching SIGSTOP");
3076b7a25e63SKonstantin Belousov 				sigqueue_delete(&td->td_sigqueue, sig);
3077b7a25e63SKonstantin Belousov 				sigqueue_delete(&p->p_sigqueue, sig);
3078e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
3079b4490c6eSKonstantin Belousov 				p->p_xsig = sig;
30807b4a950aSDavid Xu 				PROC_SLOCK(p);
3081d8267df7SDavid Xu 				sig_suspend_threads(td, p, 0);
30826ddcc233SKonstantin Belousov 				thread_suspend_switch(td, p);
30837b4a950aSDavid Xu 				PROC_SUNLOCK(p);
308490af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
3085b7a25e63SKonstantin Belousov 				goto next;
3086bc387624SKonstantin Belousov 			} else if ((prop & SIGPROP_IGNORE) != 0 &&
3087b599982bSKonstantin Belousov 			    (td->td_flags & TDF_SIGWAIT) == 0) {
3088df8bae1dSRodney W. Grimes 				/*
3089bc387624SKonstantin Belousov 				 * Default action is to ignore; drop it if
3090bc387624SKonstantin Belousov 				 * not in kern_sigtimedwait().
3091df8bae1dSRodney W. Grimes 				 */
3092df8bae1dSRodney W. Grimes 				break;		/* == ignore */
3093df8bae1dSRodney W. Grimes 			} else
30942c42a146SMarcel Moolenaar 				return (sig);
3095df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
3096df8bae1dSRodney W. Grimes 
3097d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
3098b599982bSKonstantin Belousov 			if ((td->td_flags & TDF_SIGWAIT) == 0)
3099df8bae1dSRodney W. Grimes 				break;		/* == ignore */
3100bc387624SKonstantin Belousov 			else
3101bc387624SKonstantin Belousov 				return (sig);
3102df8bae1dSRodney W. Grimes 
3103df8bae1dSRodney W. Grimes 		default:
3104df8bae1dSRodney W. Grimes 			/*
3105df8bae1dSRodney W. Grimes 			 * This signal has an action, let
3106df8bae1dSRodney W. Grimes 			 * postsig() process it.
3107df8bae1dSRodney W. Grimes 			 */
31082c42a146SMarcel Moolenaar 			return (sig);
3109df8bae1dSRodney W. Grimes 		}
31109104847fSDavid Xu 		sigqueue_delete(&td->td_sigqueue, sig);	/* take the signal! */
31116b286ee8SKonstantin Belousov 		sigqueue_delete(&p->p_sigqueue, sig);
3112b7a25e63SKonstantin Belousov next:;
3113df8bae1dSRodney W. Grimes 	}
3114df8bae1dSRodney W. Grimes 	/* NOTREACHED */
3115df8bae1dSRodney W. Grimes }
3116df8bae1dSRodney W. Grimes 
3117e574e444SDavid Xu void
3118e574e444SDavid Xu thread_stopped(struct proc *p)
3119e574e444SDavid Xu {
3120e574e444SDavid Xu 	int n;
3121e574e444SDavid Xu 
3122e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
31237b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
3124e574e444SDavid Xu 	n = p->p_suspcount;
31257c9a98f1SDavid Xu 	if (p == curproc)
3126e574e444SDavid Xu 		n++;
3127e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
31287b4a950aSDavid Xu 		PROC_SUNLOCK(p);
3129407948a5SDavid Xu 		p->p_flag &= ~P_WAITED;
3130e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
31317f96995eSDavid Xu 		childproc_stopped(p, (p->p_flag & P_TRACED) ?
3132ebceaf6dSDavid Xu 			CLD_TRAPPED : CLD_STOPPED);
3133e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
31347b4a950aSDavid Xu 		PROC_SLOCK(p);
3135e574e444SDavid Xu 	}
3136e574e444SDavid Xu }
3137e574e444SDavid Xu 
3138df8bae1dSRodney W. Grimes /*
3139df8bae1dSRodney W. Grimes  * Take the action for the specified signal
3140df8bae1dSRodney W. Grimes  * from the current set of pending signals.
3141df8bae1dSRodney W. Grimes  */
314275c586a4SKonstantin Belousov int
31430167b33bSKonstantin Belousov postsig(int sig)
3144df8bae1dSRodney W. Grimes {
31450167b33bSKonstantin Belousov 	struct thread *td;
31460167b33bSKonstantin Belousov 	struct proc *p;
3147628d2653SJohn Baldwin 	struct sigacts *ps;
31482c42a146SMarcel Moolenaar 	sig_t action;
31499104847fSDavid Xu 	ksiginfo_t ksi;
3150e442f29fSKonstantin Belousov 	sigset_t returnmask;
3151df8bae1dSRodney W. Grimes 
31522c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
31535526d2d9SEivind Eklund 
31540167b33bSKonstantin Belousov 	td = curthread;
31550167b33bSKonstantin Belousov 	p = td->td_proc;
31562ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3157628d2653SJohn Baldwin 	ps = p->p_sigacts;
315890af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
31595da49fcbSDavid Xu 	ksiginfo_init(&ksi);
31606b286ee8SKonstantin Belousov 	if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
31616b286ee8SKonstantin Belousov 	    sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
316275c586a4SKonstantin Belousov 		return (0);
31639104847fSDavid Xu 	ksi.ksi_signo = sig;
316456c06c4bSDavid Xu 	if (ksi.ksi_code == SI_TIMER)
316556c06c4bSDavid Xu 		itimer_accept(p, ksi.ksi_timerid, &ksi);
31662c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
3167df8bae1dSRodney W. Grimes #ifdef KTRACE
3168374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
31695e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
317061009552SJilles Tjoelker 		    &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3171df8bae1dSRodney W. Grimes #endif
31722a024a2bSSean Eric Fagan 
31738460a577SJohn Birrell 	if (action == SIG_DFL) {
3174df8bae1dSRodney W. Grimes 		/*
3175df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
3176df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
3177df8bae1dSRodney W. Grimes 		 */
317890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
317986be94fcSTycho Nightingale 		proc_td_siginfo_capture(td, &ksi.ksi_info);
3180b40ce416SJulian Elischer 		sigexit(td, sig);
3181df8bae1dSRodney W. Grimes 		/* NOTREACHED */
3182df8bae1dSRodney W. Grimes 	} else {
3183df8bae1dSRodney W. Grimes 		/*
3184df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
3185df8bae1dSRodney W. Grimes 		 */
3186cd735d8fSKonstantin Belousov 		KASSERT(action != SIG_IGN, ("postsig action %p", action));
3187cd735d8fSKonstantin Belousov 		KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3188cd735d8fSKonstantin Belousov 		    ("postsig action: blocked sig %d", sig));
3189cd735d8fSKonstantin Belousov 
3190df8bae1dSRodney W. Grimes 		/*
3191df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
3192645682fdSLuoqi Chen 		 * occurrences of this signal.
3193df8bae1dSRodney W. Grimes 		 *
3194645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
3195df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
3196645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
3197df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
3198df8bae1dSRodney W. Grimes 		 */
31995e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
32004093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
32015e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
3202df8bae1dSRodney W. Grimes 		} else
32034093529dSJeff Roberson 			returnmask = td->td_sigmask;
32042c42a146SMarcel Moolenaar 
3205c90c9021SEd Schouten 		if (p->p_sig == sig) {
32066626c604SJulian Elischer 			p->p_sig = 0;
3207df8bae1dSRodney W. Grimes 		}
32089104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3209e442f29fSKonstantin Belousov 		postsig_done(sig, td, ps);
3210df8bae1dSRodney W. Grimes 	}
321175c586a4SKonstantin Belousov 	return (1);
3212df8bae1dSRodney W. Grimes }
3213df8bae1dSRodney W. Grimes 
32140c82fb26SKonstantin Belousov int
32150c82fb26SKonstantin Belousov sig_ast_checksusp(struct thread *td)
32160c82fb26SKonstantin Belousov {
32170c82fb26SKonstantin Belousov 	struct proc *p;
32180c82fb26SKonstantin Belousov 	int ret;
32190c82fb26SKonstantin Belousov 
32200c82fb26SKonstantin Belousov 	p = td->td_proc;
32210c82fb26SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
32220c82fb26SKonstantin Belousov 
32230c82fb26SKonstantin Belousov 	if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
32240c82fb26SKonstantin Belousov 		return (0);
32250c82fb26SKonstantin Belousov 
32260c82fb26SKonstantin Belousov 	ret = thread_suspend_check(1);
32270c82fb26SKonstantin Belousov 	MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
32280c82fb26SKonstantin Belousov 	return (ret);
32290c82fb26SKonstantin Belousov }
32300c82fb26SKonstantin Belousov 
32310c82fb26SKonstantin Belousov int
32320c82fb26SKonstantin Belousov sig_ast_needsigchk(struct thread *td)
32330c82fb26SKonstantin Belousov {
32340c82fb26SKonstantin Belousov 	struct proc *p;
32350c82fb26SKonstantin Belousov 	struct sigacts *ps;
32360c82fb26SKonstantin Belousov 	int ret, sig;
32370c82fb26SKonstantin Belousov 
32380c82fb26SKonstantin Belousov 	p = td->td_proc;
32390c82fb26SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
32400c82fb26SKonstantin Belousov 
32410c82fb26SKonstantin Belousov 	if ((td->td_flags & TDF_NEEDSIGCHK) == 0)
32420c82fb26SKonstantin Belousov 		return (0);
32430c82fb26SKonstantin Belousov 
32440c82fb26SKonstantin Belousov 	ps = p->p_sigacts;
32450c82fb26SKonstantin Belousov 	mtx_lock(&ps->ps_mtx);
32460c82fb26SKonstantin Belousov 	sig = cursig(td);
32470c82fb26SKonstantin Belousov 	if (sig == -1) {
32480c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
32490c82fb26SKonstantin Belousov 		KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
32500c82fb26SKonstantin Belousov 		KASSERT(TD_SBDRY_INTR(td),
32510c82fb26SKonstantin Belousov 		    ("lost TDF_SERESTART of TDF_SEINTR"));
32520c82fb26SKonstantin Belousov 		KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
32530c82fb26SKonstantin Belousov 		    (TDF_SEINTR | TDF_SERESTART),
32540c82fb26SKonstantin Belousov 		    ("both TDF_SEINTR and TDF_SERESTART"));
32550c82fb26SKonstantin Belousov 		ret = TD_SBDRY_ERRNO(td);
32560c82fb26SKonstantin Belousov 	} else if (sig != 0) {
32570c82fb26SKonstantin Belousov 		ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
32580c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
32590c82fb26SKonstantin Belousov 	} else {
32600c82fb26SKonstantin Belousov 		mtx_unlock(&ps->ps_mtx);
32610c82fb26SKonstantin Belousov 		ret = 0;
32620c82fb26SKonstantin Belousov 	}
32630c82fb26SKonstantin Belousov 
32640c82fb26SKonstantin Belousov 	/*
32650c82fb26SKonstantin Belousov 	 * Do not go into sleep if this thread was the ptrace(2)
32660c82fb26SKonstantin Belousov 	 * attach leader.  cursig() consumed SIGSTOP from PT_ATTACH,
32670c82fb26SKonstantin Belousov 	 * but we usually act on the signal by interrupting sleep, and
32680c82fb26SKonstantin Belousov 	 * should do that here as well.
32690c82fb26SKonstantin Belousov 	 */
32700c82fb26SKonstantin Belousov 	if ((td->td_dbgflags & TDB_FSTP) != 0) {
32710c82fb26SKonstantin Belousov 		if (ret == 0)
32720c82fb26SKonstantin Belousov 			ret = EINTR;
32730c82fb26SKonstantin Belousov 		td->td_dbgflags &= ~TDB_FSTP;
32740c82fb26SKonstantin Belousov 	}
32750c82fb26SKonstantin Belousov 
32760c82fb26SKonstantin Belousov 	return (ret);
32770c82fb26SKonstantin Belousov }
32780c82fb26SKonstantin Belousov 
32790400be45SKonstantin Belousov int
32800400be45SKonstantin Belousov sig_intr(void)
32810400be45SKonstantin Belousov {
32820400be45SKonstantin Belousov 	struct thread *td;
32830400be45SKonstantin Belousov 	struct proc *p;
32840400be45SKonstantin Belousov 	int ret;
32850400be45SKonstantin Belousov 
32860400be45SKonstantin Belousov 	td = curthread;
3287203dda8aSKonstantin Belousov 	if ((td->td_flags & (TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK)) == 0)
3288203dda8aSKonstantin Belousov 		return (0);
3289203dda8aSKonstantin Belousov 
32900400be45SKonstantin Belousov 	p = td->td_proc;
32910400be45SKonstantin Belousov 
32920400be45SKonstantin Belousov 	PROC_LOCK(p);
32930400be45SKonstantin Belousov 	ret = sig_ast_checksusp(td);
32940400be45SKonstantin Belousov 	if (ret == 0)
32950400be45SKonstantin Belousov 		ret = sig_ast_needsigchk(td);
32960400be45SKonstantin Belousov 	PROC_UNLOCK(p);
32970400be45SKonstantin Belousov 	return (ret);
32980400be45SKonstantin Belousov }
32990400be45SKonstantin Belousov 
3300a70e9a13SKonstantin Belousov void
3301a70e9a13SKonstantin Belousov proc_wkilled(struct proc *p)
3302a70e9a13SKonstantin Belousov {
3303a70e9a13SKonstantin Belousov 
3304a70e9a13SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
3305a70e9a13SKonstantin Belousov 	if ((p->p_flag & P_WKILLED) == 0) {
3306a70e9a13SKonstantin Belousov 		p->p_flag |= P_WKILLED;
3307a70e9a13SKonstantin Belousov 		/*
3308a70e9a13SKonstantin Belousov 		 * Notify swapper that there is a process to swap in.
3309a70e9a13SKonstantin Belousov 		 * The notification is racy, at worst it would take 10
3310a70e9a13SKonstantin Belousov 		 * seconds for the swapper process to notice.
3311a70e9a13SKonstantin Belousov 		 */
3312a70e9a13SKonstantin Belousov 		if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3313a70e9a13SKonstantin Belousov 			wakeup(&proc0);
3314a70e9a13SKonstantin Belousov 	}
3315a70e9a13SKonstantin Belousov }
3316a70e9a13SKonstantin Belousov 
3317df8bae1dSRodney W. Grimes /*
3318df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
3319df8bae1dSRodney W. Grimes  */
332026f9a767SRodney W. Grimes void
3321fe20aaecSRyan Libby killproc(struct proc *p, const char *why)
3322df8bae1dSRodney W. Grimes {
33239081e5e8SJohn Baldwin 
33249081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3325c3209846SPawel Jakub Dawidek 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3326c3209846SPawel Jakub Dawidek 	    p->p_comm);
3327af8beccaSKristof Provost 	log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
33284ae4822dSKristof Provost 	    p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3329de2d0d29SKristof Provost 	    p->p_ucred->cr_uid, why);
3330a70e9a13SKonstantin Belousov 	proc_wkilled(p);
33318451d0ddSKip Macy 	kern_psignal(p, SIGKILL);
3332df8bae1dSRodney W. Grimes }
3333df8bae1dSRodney W. Grimes 
3334df8bae1dSRodney W. Grimes /*
3335df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
3336df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
3337df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
3338df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
3339df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
3340df8bae1dSRodney W. Grimes  * does not return.
3341df8bae1dSRodney W. Grimes  */
334226f9a767SRodney W. Grimes void
3343e052a8b9SEd Maste sigexit(struct thread *td, int sig)
3344df8bae1dSRodney W. Grimes {
3345b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3346df8bae1dSRodney W. Grimes 
3347628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3348df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
3349f97c3df1SDavid Schultz 	/*
3350f97c3df1SDavid Schultz 	 * We must be single-threading to generate a core dump.  This
3351f97c3df1SDavid Schultz 	 * ensures that the registers in the core file are up-to-date.
3352f97c3df1SDavid Schultz 	 * Also, the ELF dump handler assumes that the thread list doesn't
3353f97c3df1SDavid Schultz 	 * change out from under it.
3354f97c3df1SDavid Schultz 	 *
3355f97c3df1SDavid Schultz 	 * XXX If another thread attempts to single-thread before us
3356f97c3df1SDavid Schultz 	 *     (e.g. via fork()), we won't get a dump at all.
3357f97c3df1SDavid Schultz 	 */
3358fd50a707SBrooks Davis 	if ((sigprop(sig) & SIGPROP_CORE) &&
3359fd50a707SBrooks Davis 	    thread_single(p, SINGLE_NO_EXIT) == 0) {
33602c42a146SMarcel Moolenaar 		p->p_sig = sig;
3361c364e17eSAndrey A. Chernov 		/*
3362c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
3363c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
3364c364e17eSAndrey A. Chernov 		 * these messages.)
3365c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
33664ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
3367c364e17eSAndrey A. Chernov 		 */
3368b40ce416SJulian Elischer 		if (coredump(td) == 0)
33692c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
337057308494SJoerg Wunsch 		if (kern_logsigexit)
337157308494SJoerg Wunsch 			log(LOG_INFO,
3372af8beccaSKristof Provost 			    "pid %d (%s), jid %d, uid %d: exited on "
33734ae4822dSKristof Provost 			    "signal %d%s\n", p->p_pid, p->p_comm,
33744ae4822dSKristof Provost 			    p->p_ucred->cr_prison->pr_id,
3375de2d0d29SKristof Provost 			    td->td_ucred->cr_uid,
33762c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
33772c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
33784ae89b95SJohn Baldwin 	} else
3379628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3380b4490c6eSKonstantin Belousov 	exit1(td, 0, sig);
3381df8bae1dSRodney W. Grimes 	/* NOTREACHED */
3382df8bae1dSRodney W. Grimes }
3383df8bae1dSRodney W. Grimes 
3384ebceaf6dSDavid Xu /*
33857f96995eSDavid Xu  * Send queued SIGCHLD to parent when child process's state
33867f96995eSDavid Xu  * is changed.
3387ebceaf6dSDavid Xu  */
33887f96995eSDavid Xu static void
33897f96995eSDavid Xu sigparent(struct proc *p, int reason, int status)
3390ebceaf6dSDavid Xu {
3391ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
3392ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3393ebceaf6dSDavid Xu 
3394ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
3395ebceaf6dSDavid Xu 		p->p_ksi->ksi_signo  = SIGCHLD;
3396ebceaf6dSDavid Xu 		p->p_ksi->ksi_code   = reason;
33977f96995eSDavid Xu 		p->p_ksi->ksi_status = status;
3398ebceaf6dSDavid Xu 		p->p_ksi->ksi_pid    = p->p_pid;
3399ebceaf6dSDavid Xu 		p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
3400ebceaf6dSDavid Xu 		if (KSI_ONQ(p->p_ksi))
3401ebceaf6dSDavid Xu 			return;
3402ebceaf6dSDavid Xu 	}
3403ad6eec7bSJohn Baldwin 	pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3404ebceaf6dSDavid Xu }
3405ebceaf6dSDavid Xu 
34067f96995eSDavid Xu static void
3407b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig)
34087f96995eSDavid Xu {
34097f96995eSDavid Xu 	struct sigacts *ps;
34107f96995eSDavid Xu 
34117f96995eSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
34127f96995eSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
34137f96995eSDavid Xu 
34147f96995eSDavid Xu 	/*
34157f96995eSDavid Xu 	 * Wake up parent sleeping in kern_wait(), also send
34167f96995eSDavid Xu 	 * SIGCHLD to parent, but SIGCHLD does not guarantee
34177f96995eSDavid Xu 	 * that parent will awake, because parent may masked
34187f96995eSDavid Xu 	 * the signal.
34197f96995eSDavid Xu 	 */
34207f96995eSDavid Xu 	p->p_pptr->p_flag |= P_STATCHILD;
34217f96995eSDavid Xu 	wakeup(p->p_pptr);
34227f96995eSDavid Xu 
34237f96995eSDavid Xu 	ps = p->p_pptr->p_sigacts;
34247f96995eSDavid Xu 	mtx_lock(&ps->ps_mtx);
34257f96995eSDavid Xu 	if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
34267f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
3427b20a9aa9SJilles Tjoelker 		sigparent(p, reason, sig);
34287f96995eSDavid Xu 	} else
34297f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
34307f96995eSDavid Xu }
34317f96995eSDavid Xu 
34327f96995eSDavid Xu void
34337f96995eSDavid Xu childproc_stopped(struct proc *p, int reason)
34347f96995eSDavid Xu {
3435b4490c6eSKonstantin Belousov 
3436b4490c6eSKonstantin Belousov 	childproc_jobstate(p, reason, p->p_xsig);
34377f96995eSDavid Xu }
34387f96995eSDavid Xu 
3439ebceaf6dSDavid Xu void
3440ebceaf6dSDavid Xu childproc_continued(struct proc *p)
3441ebceaf6dSDavid Xu {
34427f96995eSDavid Xu 	childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3443ebceaf6dSDavid Xu }
3444ebceaf6dSDavid Xu 
3445ebceaf6dSDavid Xu void
3446ebceaf6dSDavid Xu childproc_exited(struct proc *p)
3447ebceaf6dSDavid Xu {
3448b4490c6eSKonstantin Belousov 	int reason, status;
3449ebceaf6dSDavid Xu 
3450b4490c6eSKonstantin Belousov 	if (WCOREDUMP(p->p_xsig)) {
3451b4490c6eSKonstantin Belousov 		reason = CLD_DUMPED;
3452b4490c6eSKonstantin Belousov 		status = WTERMSIG(p->p_xsig);
3453b4490c6eSKonstantin Belousov 	} else if (WIFSIGNALED(p->p_xsig)) {
3454b4490c6eSKonstantin Belousov 		reason = CLD_KILLED;
3455b4490c6eSKonstantin Belousov 		status = WTERMSIG(p->p_xsig);
3456b4490c6eSKonstantin Belousov 	} else {
3457b4490c6eSKonstantin Belousov 		reason = CLD_EXITED;
3458b4490c6eSKonstantin Belousov 		status = p->p_xexit;
3459b4490c6eSKonstantin Belousov 	}
34607f96995eSDavid Xu 	/*
34617f96995eSDavid Xu 	 * XXX avoid calling wakeup(p->p_pptr), the work is
34627f96995eSDavid Xu 	 * done in exit1().
34637f96995eSDavid Xu 	 */
34647f96995eSDavid Xu 	sigparent(p, reason, status);
3465ebceaf6dSDavid Xu }
3466ebceaf6dSDavid Xu 
3467f1fe1e02SMariusz Zaborski #define	MAX_NUM_CORE_FILES 100000
3468cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES
3469cc37baeaSStephen J. Kiernan #define	NUM_CORE_FILES 5
3470cc37baeaSStephen J. Kiernan #endif
3471cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3472cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES;
3473e7228204SAlfred Perlstein 
3474e7228204SAlfred Perlstein static int
3475e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3476e7228204SAlfred Perlstein {
3477e7228204SAlfred Perlstein 	int error;
3478e7228204SAlfred Perlstein 	int new_val;
3479e7228204SAlfred Perlstein 
3480c5105012SKonstantin Belousov 	new_val = num_cores;
3481e7228204SAlfred Perlstein 	error = sysctl_handle_int(oidp, &new_val, 0, req);
3482e7228204SAlfred Perlstein 	if (error != 0 || req->newptr == NULL)
3483e7228204SAlfred Perlstein 		return (error);
3484cc37baeaSStephen J. Kiernan 	if (new_val > MAX_NUM_CORE_FILES)
3485cc37baeaSStephen J. Kiernan 		new_val = MAX_NUM_CORE_FILES;
3486e7228204SAlfred Perlstein 	if (new_val < 0)
3487e7228204SAlfred Perlstein 		new_val = 0;
3488e7228204SAlfred Perlstein 	num_cores = new_val;
3489e7228204SAlfred Perlstein 	return (0);
3490e7228204SAlfred Perlstein }
34917029da5cSPawel Biernacki SYSCTL_PROC(_debug, OID_AUTO, ncores,
34927029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof(int),
34937029da5cSPawel Biernacki     sysctl_debug_num_cores_check, "I",
34946cad1a5dSMariusz Zaborski     "Maximum number of generated process corefiles while using index format");
3495e7228204SAlfred Perlstein 
34966026dcd7SMark Johnston #define	GZIP_SUFFIX	".gz"
34976026dcd7SMark Johnston #define	ZSTD_SUFFIX	".zst"
3498aa14e9b7SMark Johnston 
349978f57a9cSMark Johnston int compress_user_cores = 0;
3500e7228204SAlfred Perlstein 
350178f57a9cSMark Johnston static int
350278f57a9cSMark Johnston sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
350378f57a9cSMark Johnston {
350478f57a9cSMark Johnston 	int error, val;
350578f57a9cSMark Johnston 
350678f57a9cSMark Johnston 	val = compress_user_cores;
350778f57a9cSMark Johnston 	error = sysctl_handle_int(oidp, &val, 0, req);
350878f57a9cSMark Johnston 	if (error != 0 || req->newptr == NULL)
350978f57a9cSMark Johnston 		return (error);
351078f57a9cSMark Johnston 	if (val != 0 && !compressor_avail(val))
351178f57a9cSMark Johnston 		return (EINVAL);
351278f57a9cSMark Johnston 	compress_user_cores = val;
351378f57a9cSMark Johnston 	return (error);
351478f57a9cSMark Johnston }
35157029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
35167029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
35177029da5cSPawel Biernacki     sysctl_compress_user_cores, "I",
35186026dcd7SMark Johnston     "Enable compression of user corefiles ("
35196026dcd7SMark Johnston     __XSTRING(COMPRESS_GZIP) " = gzip, "
35206026dcd7SMark Johnston     __XSTRING(COMPRESS_ZSTD) " = zstd)");
352178f57a9cSMark Johnston 
352278f57a9cSMark Johnston int compress_user_cores_level = 6;
352378f57a9cSMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
352478f57a9cSMark Johnston     &compress_user_cores_level, 0,
352578f57a9cSMark Johnston     "Corefile compression level");
3526e7228204SAlfred Perlstein 
35275bc0ff88SMateusz Guzik /*
35285bc0ff88SMateusz Guzik  * Protect the access to corefilename[] by allproc_lock.
35295bc0ff88SMateusz Guzik  */
35305bc0ff88SMateusz Guzik #define	corefilename_lock	allproc_lock
35315bc0ff88SMateusz Guzik 
35326d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"};
3533fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
35345bc0ff88SMateusz Guzik 
35355bc0ff88SMateusz Guzik static int
35365bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
35375bc0ff88SMateusz Guzik {
35385bc0ff88SMateusz Guzik 	int error;
35395bc0ff88SMateusz Guzik 
35405bc0ff88SMateusz Guzik 	sx_xlock(&corefilename_lock);
35415bc0ff88SMateusz Guzik 	error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
35425bc0ff88SMateusz Guzik 	    req);
35435bc0ff88SMateusz Guzik 	sx_xunlock(&corefilename_lock);
35445bc0ff88SMateusz Guzik 
35455bc0ff88SMateusz Guzik 	return (error);
35465bc0ff88SMateusz Guzik }
3547fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
35485bc0ff88SMateusz Guzik     CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
35495bc0ff88SMateusz Guzik     "Process corefile name format string");
3550c5edb423SSean Eric Fagan 
35510dea6e3cSMariusz Zaborski static void
35520dea6e3cSMariusz Zaborski vnode_close_locked(struct thread *td, struct vnode *vp)
35530dea6e3cSMariusz Zaborski {
35540dea6e3cSMariusz Zaborski 
3555b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
35560dea6e3cSMariusz Zaborski 	vn_close(vp, FWRITE, td->td_ucred, td);
35570dea6e3cSMariusz Zaborski }
35580dea6e3cSMariusz Zaborski 
35590dea6e3cSMariusz Zaborski /*
35600dea6e3cSMariusz Zaborski  * If the core format has a %I in it, then we need to check
35610dea6e3cSMariusz Zaborski  * for existing corefiles before defining a name.
3562f1fe1e02SMariusz Zaborski  * To do this we iterate over 0..ncores to find a
35630dea6e3cSMariusz Zaborski  * non-existing core file name to use. If all core files are
35640dea6e3cSMariusz Zaborski  * already used we choose the oldest one.
35650dea6e3cSMariusz Zaborski  */
35660dea6e3cSMariusz Zaborski static int
35670dea6e3cSMariusz Zaborski corefile_open_last(struct thread *td, char *name, int indexpos,
3568f1fe1e02SMariusz Zaborski     int indexlen, int ncores, struct vnode **vpp)
35690dea6e3cSMariusz Zaborski {
35700dea6e3cSMariusz Zaborski 	struct vnode *oldvp, *nextvp, *vp;
35710dea6e3cSMariusz Zaborski 	struct vattr vattr;
35720dea6e3cSMariusz Zaborski 	struct nameidata nd;
35730dea6e3cSMariusz Zaborski 	int error, i, flags, oflags, cmode;
3574f1fe1e02SMariusz Zaborski 	char ch;
35750dea6e3cSMariusz Zaborski 	struct timespec lasttime;
35760dea6e3cSMariusz Zaborski 
35770dea6e3cSMariusz Zaborski 	nextvp = oldvp = NULL;
35780dea6e3cSMariusz Zaborski 	cmode = S_IRUSR | S_IWUSR;
35790dea6e3cSMariusz Zaborski 	oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
35800dea6e3cSMariusz Zaborski 	    (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
35810dea6e3cSMariusz Zaborski 
3582f1fe1e02SMariusz Zaborski 	for (i = 0; i < ncores; i++) {
35830dea6e3cSMariusz Zaborski 		flags = O_CREAT | FWRITE | O_NOFOLLOW;
3584f1fe1e02SMariusz Zaborski 
3585f1fe1e02SMariusz Zaborski 		ch = name[indexpos + indexlen];
3586f1fe1e02SMariusz Zaborski 		(void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3587f1fe1e02SMariusz Zaborski 		    i);
3588f1fe1e02SMariusz Zaborski 		name[indexpos + indexlen] = ch;
35890dea6e3cSMariusz Zaborski 
35900dea6e3cSMariusz Zaborski 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
35910dea6e3cSMariusz Zaborski 		error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
35920dea6e3cSMariusz Zaborski 		    NULL);
35930dea6e3cSMariusz Zaborski 		if (error != 0)
35940dea6e3cSMariusz Zaborski 			break;
35950dea6e3cSMariusz Zaborski 
35960dea6e3cSMariusz Zaborski 		vp = nd.ni_vp;
35970dea6e3cSMariusz Zaborski 		NDFREE(&nd, NDF_ONLY_PNBUF);
35980dea6e3cSMariusz Zaborski 		if ((flags & O_CREAT) == O_CREAT) {
35990dea6e3cSMariusz Zaborski 			nextvp = vp;
36000dea6e3cSMariusz Zaborski 			break;
36010dea6e3cSMariusz Zaborski 		}
36020dea6e3cSMariusz Zaborski 
36030dea6e3cSMariusz Zaborski 		error = VOP_GETATTR(vp, &vattr, td->td_ucred);
36040dea6e3cSMariusz Zaborski 		if (error != 0) {
36050dea6e3cSMariusz Zaborski 			vnode_close_locked(td, vp);
36060dea6e3cSMariusz Zaborski 			break;
36070dea6e3cSMariusz Zaborski 		}
36080dea6e3cSMariusz Zaborski 
36090dea6e3cSMariusz Zaborski 		if (oldvp == NULL ||
36100dea6e3cSMariusz Zaborski 		    lasttime.tv_sec > vattr.va_mtime.tv_sec ||
36110dea6e3cSMariusz Zaborski 		    (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
36120dea6e3cSMariusz Zaborski 		    lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
36130dea6e3cSMariusz Zaborski 			if (oldvp != NULL)
3614e298466eSAndriy Gapon 				vn_close(oldvp, FWRITE, td->td_ucred, td);
36150dea6e3cSMariusz Zaborski 			oldvp = vp;
3616e298466eSAndriy Gapon 			VOP_UNLOCK(oldvp);
36170dea6e3cSMariusz Zaborski 			lasttime = vattr.va_mtime;
36180dea6e3cSMariusz Zaborski 		} else {
36190dea6e3cSMariusz Zaborski 			vnode_close_locked(td, vp);
36200dea6e3cSMariusz Zaborski 		}
36210dea6e3cSMariusz Zaborski 	}
36220dea6e3cSMariusz Zaborski 
36230dea6e3cSMariusz Zaborski 	if (oldvp != NULL) {
362489f2ab06SKonstantin Belousov 		if (nextvp == NULL) {
362589f2ab06SKonstantin Belousov 			if ((td->td_proc->p_flag & P_SUGID) != 0) {
362689f2ab06SKonstantin Belousov 				error = EFAULT;
3627e298466eSAndriy Gapon 				vn_close(oldvp, FWRITE, td->td_ucred, td);
362889f2ab06SKonstantin Belousov 			} else {
362989f2ab06SKonstantin Belousov 				nextvp = oldvp;
3630e298466eSAndriy Gapon 				error = vn_lock(nextvp, LK_EXCLUSIVE);
3631e298466eSAndriy Gapon 				if (error != 0) {
3632e298466eSAndriy Gapon 					vn_close(nextvp, FWRITE, td->td_ucred,
3633e298466eSAndriy Gapon 					    td);
3634e298466eSAndriy Gapon 					nextvp = NULL;
3635e298466eSAndriy Gapon 				}
363689f2ab06SKonstantin Belousov 			}
363789f2ab06SKonstantin Belousov 		} else {
3638e298466eSAndriy Gapon 			vn_close(oldvp, FWRITE, td->td_ucred, td);
363989f2ab06SKonstantin Belousov 		}
36400dea6e3cSMariusz Zaborski 	}
36410dea6e3cSMariusz Zaborski 	if (error != 0) {
36420dea6e3cSMariusz Zaborski 		if (nextvp != NULL)
36430dea6e3cSMariusz Zaborski 			vnode_close_locked(td, oldvp);
36440dea6e3cSMariusz Zaborski 	} else {
36450dea6e3cSMariusz Zaborski 		*vpp = nextvp;
36460dea6e3cSMariusz Zaborski 	}
36470dea6e3cSMariusz Zaborski 
36480dea6e3cSMariusz Zaborski 	return (error);
36490dea6e3cSMariusz Zaborski }
36500dea6e3cSMariusz Zaborski 
3651c5edb423SSean Eric Fagan /*
3652c345faeaSPawel Jakub Dawidek  * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3653c345faeaSPawel Jakub Dawidek  * Expand the name described in corefilename, using name, uid, and pid
3654c345faeaSPawel Jakub Dawidek  * and open/create core file.
3655c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
3656c5edb423SSean Eric Fagan  *	%N	name of process ("name")
3657c5edb423SSean Eric Fagan  *	%P	process id (pid)
3658c5edb423SSean Eric Fagan  *	%U	user id (uid)
3659c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
3660c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3661c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
3662c5edb423SSean Eric Fagan  */
3663c345faeaSPawel Jakub Dawidek static int
3664c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
36659d3ecb7eSEric van Gyzen     int compress, int signum, struct vnode **vpp, char **namep)
36668b43b535SAlfred Perlstein {
366736b208e0SRobert Watson 	struct sbuf sb;
36680dea6e3cSMariusz Zaborski 	struct nameidata nd;
366936b208e0SRobert Watson 	const char *format;
3670c345faeaSPawel Jakub Dawidek 	char *hostname, *name;
3671f1fe1e02SMariusz Zaborski 	int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3672c5edb423SSean Eric Fagan 
3673b7402d82SAlfred Perlstein 	hostname = NULL;
36748b43b535SAlfred Perlstein 	format = corefilename;
3675086053a3SPawel Jakub Dawidek 	name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3676f1fe1e02SMariusz Zaborski 	indexlen = 0;
3677e7228204SAlfred Perlstein 	indexpos = -1;
3678f1fe1e02SMariusz Zaborski 	ncores = num_cores;
3679c52ff611SPawel Jakub Dawidek 	(void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
36805bc0ff88SMateusz Guzik 	sx_slock(&corefilename_lock);
368129146f1aSPawel Jakub Dawidek 	for (i = 0; format[i] != '\0'; i++) {
3682c5edb423SSean Eric Fagan 		switch (format[i]) {
3683c5edb423SSean Eric Fagan 		case '%':	/* Format character */
3684c5edb423SSean Eric Fagan 			i++;
3685c5edb423SSean Eric Fagan 			switch (format[i]) {
3686c5edb423SSean Eric Fagan 			case '%':
368736b208e0SRobert Watson 				sbuf_putc(&sb, '%');
3688c5edb423SSean Eric Fagan 				break;
3689e7228204SAlfred Perlstein 			case 'H':	/* hostname */
3690b7402d82SAlfred Perlstein 				if (hostname == NULL) {
3691b7402d82SAlfred Perlstein 					hostname = malloc(MAXHOSTNAMELEN,
3692086053a3SPawel Jakub Dawidek 					    M_TEMP, M_WAITOK);
3693b7402d82SAlfred Perlstein 				}
3694e7228204SAlfred Perlstein 				getcredhostname(td->td_ucred, hostname,
3695b7402d82SAlfred Perlstein 				    MAXHOSTNAMELEN);
3696e7228204SAlfred Perlstein 				sbuf_printf(&sb, "%s", hostname);
3697e7228204SAlfred Perlstein 				break;
3698e7228204SAlfred Perlstein 			case 'I':	/* autoincrementing index */
3699f1fe1e02SMariusz Zaborski 				if (indexpos != -1) {
3700f1fe1e02SMariusz Zaborski 					sbuf_printf(&sb, "%%I");
3701f1fe1e02SMariusz Zaborski 					break;
3702f1fe1e02SMariusz Zaborski 				}
3703f1fe1e02SMariusz Zaborski 
3704f1fe1e02SMariusz Zaborski 				indexpos = sbuf_len(&sb);
3705f1fe1e02SMariusz Zaborski 				sbuf_printf(&sb, "%u", ncores - 1);
3706f1fe1e02SMariusz Zaborski 				indexlen = sbuf_len(&sb) - indexpos;
3707e7228204SAlfred Perlstein 				break;
3708c5edb423SSean Eric Fagan 			case 'N':	/* process name */
3709c52ff611SPawel Jakub Dawidek 				sbuf_printf(&sb, "%s", comm);
3710c5edb423SSean Eric Fagan 				break;
3711c5edb423SSean Eric Fagan 			case 'P':	/* process id */
371236b208e0SRobert Watson 				sbuf_printf(&sb, "%u", pid);
3713c5edb423SSean Eric Fagan 				break;
37149d3ecb7eSEric van Gyzen 			case 'S':	/* signal number */
37159d3ecb7eSEric van Gyzen 				sbuf_printf(&sb, "%i", signum);
37169d3ecb7eSEric van Gyzen 				break;
3717c5edb423SSean Eric Fagan 			case 'U':	/* user id */
371836b208e0SRobert Watson 				sbuf_printf(&sb, "%u", uid);
3719c5edb423SSean Eric Fagan 				break;
3720c5edb423SSean Eric Fagan 			default:
37218b43b535SAlfred Perlstein 				log(LOG_ERR,
372236b208e0SRobert Watson 				    "Unknown format character %c in "
372336b208e0SRobert Watson 				    "corename `%s'\n", format[i], format);
372429146f1aSPawel Jakub Dawidek 				break;
3725c5edb423SSean Eric Fagan 			}
3726c5edb423SSean Eric Fagan 			break;
3727c5edb423SSean Eric Fagan 		default:
372836b208e0SRobert Watson 			sbuf_putc(&sb, format[i]);
37296c08be2bSPawel Jakub Dawidek 			break;
3730c5edb423SSean Eric Fagan 		}
3731c5edb423SSean Eric Fagan 	}
37325bc0ff88SMateusz Guzik 	sx_sunlock(&corefilename_lock);
3733b7402d82SAlfred Perlstein 	free(hostname, M_TEMP);
373478f57a9cSMark Johnston 	if (compress == COMPRESS_GZIP)
37356026dcd7SMark Johnston 		sbuf_printf(&sb, GZIP_SUFFIX);
37366026dcd7SMark Johnston 	else if (compress == COMPRESS_ZSTD)
37376026dcd7SMark Johnston 		sbuf_printf(&sb, ZSTD_SUFFIX);
37384d369413SMatthew D Fleming 	if (sbuf_error(&sb) != 0) {
373936b208e0SRobert Watson 		log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3740c52ff611SPawel Jakub Dawidek 		    "long\n", (long)pid, comm, (u_long)uid);
3741b7402d82SAlfred Perlstein 		sbuf_delete(&sb);
3742c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
3743c345faeaSPawel Jakub Dawidek 		return (ENOMEM);
3744c5edb423SSean Eric Fagan 	}
374536b208e0SRobert Watson 	sbuf_finish(&sb);
374636b208e0SRobert Watson 	sbuf_delete(&sb);
3747e7228204SAlfred Perlstein 
3748e7228204SAlfred Perlstein 	if (indexpos != -1) {
3749f1fe1e02SMariusz Zaborski 		error = corefile_open_last(td, name, indexpos, indexlen, ncores,
3750f1fe1e02SMariusz Zaborski 		    vpp);
37510dea6e3cSMariusz Zaborski 		if (error != 0) {
3752e7228204SAlfred Perlstein 			log(LOG_ERR,
3753e7228204SAlfred Perlstein 			    "pid %d (%s), uid (%u):  Path `%s' failed "
3754e7228204SAlfred Perlstein 			    "on initial open test, error = %d\n",
3755c52ff611SPawel Jakub Dawidek 			    pid, comm, uid, name, error);
3756c345faeaSPawel Jakub Dawidek 		}
37570dea6e3cSMariusz Zaborski 	} else {
37580dea6e3cSMariusz Zaborski 		cmode = S_IRUSR | S_IWUSR;
37590dea6e3cSMariusz Zaborski 		oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
37600dea6e3cSMariusz Zaborski 		    (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
37610dea6e3cSMariusz Zaborski 		flags = O_CREAT | FWRITE | O_NOFOLLOW;
376289f2ab06SKonstantin Belousov 		if ((td->td_proc->p_flag & P_SUGID) != 0)
376389f2ab06SKonstantin Belousov 			flags |= O_EXCL;
37640dea6e3cSMariusz Zaborski 
37650dea6e3cSMariusz Zaborski 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
37660dea6e3cSMariusz Zaborski 		error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
37670dea6e3cSMariusz Zaborski 		    NULL);
37680dea6e3cSMariusz Zaborski 		if (error == 0) {
37690dea6e3cSMariusz Zaborski 			*vpp = nd.ni_vp;
37700dea6e3cSMariusz Zaborski 			NDFREE(&nd, NDF_ONLY_PNBUF);
3771c345faeaSPawel Jakub Dawidek 		}
3772c345faeaSPawel Jakub Dawidek 	}
3773c345faeaSPawel Jakub Dawidek 
37740dea6e3cSMariusz Zaborski 	if (error != 0) {
3775c345faeaSPawel Jakub Dawidek #ifdef AUDIT
3776c345faeaSPawel Jakub Dawidek 		audit_proc_coredump(td, name, error);
3777c345faeaSPawel Jakub Dawidek #endif
3778c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
3779c345faeaSPawel Jakub Dawidek 		return (error);
3780e7228204SAlfred Perlstein 	}
3781c345faeaSPawel Jakub Dawidek 	*namep = name;
3782c345faeaSPawel Jakub Dawidek 	return (0);
378336b208e0SRobert Watson }
3784c5edb423SSean Eric Fagan 
3785df8bae1dSRodney W. Grimes /*
3786fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
3787fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
3788fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
3789fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
3790fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
3791fca666a1SJulian Elischer  */
3792fca666a1SJulian Elischer 
3793fca666a1SJulian Elischer static int
3794b40ce416SJulian Elischer coredump(struct thread *td)
3795fca666a1SJulian Elischer {
3796b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3797f06f465dSPawel Jakub Dawidek 	struct ucred *cred = td->td_ucred;
3798f06f465dSPawel Jakub Dawidek 	struct vnode *vp;
379906ae1e91SMatthew Dillon 	struct flock lf;
3800fca666a1SJulian Elischer 	struct vattr vattr;
38017739d927SMateusz Guzik 	size_t fullpathsize;
3802c345faeaSPawel Jakub Dawidek 	int error, error1, locked;
3803fca666a1SJulian Elischer 	char *name;			/* name of corefile */
3804539c9eefSKonstantin Belousov 	void *rl_cookie;
3805fca666a1SJulian Elischer 	off_t limit;
3806842ab62bSRui Paulo 	char *fullpath, *freepath = NULL;
3807349fcda4SWarner Losh 	struct sbuf *sb;
3808fca666a1SJulian Elischer 
38094ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3810f97c3df1SDavid Schultz 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3811fca666a1SJulian Elischer 
3812677258f7SKonstantin Belousov 	if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3813677258f7SKonstantin Belousov 	    (p->p_flag2 & P2_NOTRACE) != 0) {
3814628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3815fca666a1SJulian Elischer 		return (EFAULT);
3816628d2653SJohn Baldwin 	}
3817fca666a1SJulian Elischer 
3818fca666a1SJulian Elischer 	/*
381935a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
382035a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
382135a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
382235a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
382335a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
382435a2598fSSean Eric Fagan 	 * if it is larger than the limit.
3825fca666a1SJulian Elischer 	 */
3826f6f6d240SMateusz Guzik 	limit = (off_t)lim_cur(td, RLIMIT_CORE);
3827b8fdb0d9SEdward Tomasz Napierala 	if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3828628d2653SJohn Baldwin 		PROC_UNLOCK(p);
382991d5354aSJohn Baldwin 		return (EFBIG);
383057274c51SChristian S.J. Peron 	}
3831b8fdb0d9SEdward Tomasz Napierala 	PROC_UNLOCK(p);
383235a2598fSSean Eric Fagan 
3833aa14e9b7SMark Johnston 	error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
38349d3ecb7eSEric van Gyzen 	    compress_user_cores, p->p_sig, &vp, &name);
3835c345faeaSPawel Jakub Dawidek 	if (error != 0)
3836fca666a1SJulian Elischer 		return (error);
383706ae1e91SMatthew Dillon 
3838539c9eefSKonstantin Belousov 	/*
3839539c9eefSKonstantin Belousov 	 * Don't dump to non-regular files or files with links.
384089f2ab06SKonstantin Belousov 	 * Do not dump into system files. Effective user must own the corefile.
3841539c9eefSKonstantin Belousov 	 */
3842f06f465dSPawel Jakub Dawidek 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
38437a29e0bfSKonstantin Belousov 	    vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
384489f2ab06SKonstantin Belousov 	    vattr.va_uid != cred->cr_uid) {
3845b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
3846832dafadSDon Lewis 		error = EFAULT;
3847dacbc9dbSKonstantin Belousov 		goto out;
3848832dafadSDon Lewis 	}
3849832dafadSDon Lewis 
3850b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
3851539c9eefSKonstantin Belousov 
3852539c9eefSKonstantin Belousov 	/* Postpone other writers, including core dumps of other processes. */
3853539c9eefSKonstantin Belousov 	rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3854539c9eefSKonstantin Belousov 
385506ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
385606ae1e91SMatthew Dillon 	lf.l_start = 0;
385706ae1e91SMatthew Dillon 	lf.l_len = 0;
385806ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
3859c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
386006ae1e91SMatthew Dillon 
3861fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
3862fca666a1SJulian Elischer 	vattr.va_size = 0;
38636141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
38646141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
3865cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
38660359a12eSAttilio Rao 	VOP_SETATTR(vp, &vattr, cred);
3867b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
3868628d2653SJohn Baldwin 	PROC_LOCK(p);
3869fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
3870628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3871fca666a1SJulian Elischer 
387223c6445aSPawel Jakub Dawidek 	if (p->p_sysent->sv_coredump != NULL) {
387378f57a9cSMark Johnston 		error = p->p_sysent->sv_coredump(td, vp, limit, 0);
387423c6445aSPawel Jakub Dawidek 	} else {
387523c6445aSPawel Jakub Dawidek 		error = ENOSYS;
387623c6445aSPawel Jakub Dawidek 	}
3877fca666a1SJulian Elischer 
3878c447f5b2SRobert Watson 	if (locked) {
387906ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
388006ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3881c447f5b2SRobert Watson 	}
3882539c9eefSKonstantin Belousov 	vn_rangelock_unlock(vp, rl_cookie);
3883dacbc9dbSKonstantin Belousov 
3884842ab62bSRui Paulo 	/*
3885842ab62bSRui Paulo 	 * Notify the userland helper that a process triggered a core dump.
3886842ab62bSRui Paulo 	 * This allows the helper to run an automated debugging session.
3887842ab62bSRui Paulo 	 */
3888dacbc9dbSKonstantin Belousov 	if (error != 0 || coredump_devctl == 0)
3889842ab62bSRui Paulo 		goto out;
3890349fcda4SWarner Losh 	sb = sbuf_new_auto();
3891feabaaf9SMateusz Guzik 	if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
3892349fcda4SWarner Losh 		goto out2;
3893349fcda4SWarner Losh 	sbuf_printf(sb, "comm=\"");
3894349fcda4SWarner Losh 	devctl_safe_quote_sb(sb, fullpath);
3895842ab62bSRui Paulo 	free(freepath, M_TEMP);
3896349fcda4SWarner Losh 	sbuf_printf(sb, "\" core=\"");
3897349fcda4SWarner Losh 
3898349fcda4SWarner Losh 	/*
3899349fcda4SWarner Losh 	 * We can't lookup core file vp directly. When we're replacing a core, and
3900349fcda4SWarner Losh 	 * other random times, we flush the name cache, so it will fail. Instead,
3901349fcda4SWarner Losh 	 * if the path of the core is relative, add the current dir in front if it.
3902349fcda4SWarner Losh 	 */
3903349fcda4SWarner Losh 	if (name[0] != '/') {
39047739d927SMateusz Guzik 		fullpathsize = MAXPATHLEN;
39057739d927SMateusz Guzik 		freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
3906feabaaf9SMateusz Guzik 		if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
39077739d927SMateusz Guzik 			free(freepath, M_TEMP);
3908349fcda4SWarner Losh 			goto out2;
3909349fcda4SWarner Losh 		}
3910349fcda4SWarner Losh 		devctl_safe_quote_sb(sb, fullpath);
39117739d927SMateusz Guzik 		free(freepath, M_TEMP);
3912349fcda4SWarner Losh 		sbuf_putc(sb, '/');
3913349fcda4SWarner Losh 	}
3914349fcda4SWarner Losh 	devctl_safe_quote_sb(sb, name);
3915349fcda4SWarner Losh 	sbuf_printf(sb, "\"");
3916349fcda4SWarner Losh 	if (sbuf_finish(sb) == 0)
3917349fcda4SWarner Losh 		devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
3918349fcda4SWarner Losh out2:
3919349fcda4SWarner Losh 	sbuf_delete(sb);
3920842ab62bSRui Paulo out:
3921dacbc9dbSKonstantin Belousov 	error1 = vn_close(vp, FWRITE, cred, td);
3922dacbc9dbSKonstantin Belousov 	if (error == 0)
3923dacbc9dbSKonstantin Belousov 		error = error1;
392457274c51SChristian S.J. Peron #ifdef AUDIT
392557274c51SChristian S.J. Peron 	audit_proc_coredump(td, name, error);
392657274c51SChristian S.J. Peron #endif
392757274c51SChristian S.J. Peron 	free(name, M_TEMP);
3928fca666a1SJulian Elischer 	return (error);
3929fca666a1SJulian Elischer }
3930fca666a1SJulian Elischer 
3931fca666a1SJulian Elischer /*
39320c14ff0eSRobert Watson  * Nonexistent system call-- signal process (may want to handle it).  Flag
39330c14ff0eSRobert Watson  * error in case process won't see signal immediately (blocked or ignored).
3934df8bae1dSRodney W. Grimes  */
3935d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
3936df8bae1dSRodney W. Grimes struct nosys_args {
3937df8bae1dSRodney W. Grimes 	int	dummy;
3938df8bae1dSRodney W. Grimes };
3939d2d3e875SBruce Evans #endif
3940df8bae1dSRodney W. Grimes /* ARGSUSED */
394126f9a767SRodney W. Grimes int
3942e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args)
3943df8bae1dSRodney W. Grimes {
3944f5a077c3SKonstantin Belousov 	struct proc *p;
3945f5a077c3SKonstantin Belousov 
3946f5a077c3SKonstantin Belousov 	p = td->td_proc;
3947b40ce416SJulian Elischer 
3948628d2653SJohn Baldwin 	PROC_LOCK(p);
3949888aefefSKonstantin Belousov 	tdsignal(td, SIGSYS);
3950628d2653SJohn Baldwin 	PROC_UNLOCK(p);
39517ceeb35bSKonstantin Belousov 	if (kern_lognosys == 1 || kern_lognosys == 3) {
3952f5a077c3SKonstantin Belousov 		uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3953f5a077c3SKonstantin Belousov 		    td->td_sa.code);
39547ceeb35bSKonstantin Belousov 	}
395518f917a9SBrooks Davis 	if (kern_lognosys == 2 || kern_lognosys == 3 ||
395618f917a9SBrooks Davis 	    (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
39577ceeb35bSKonstantin Belousov 		printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
39587ceeb35bSKonstantin Belousov 		    td->td_sa.code);
39597ceeb35bSKonstantin Belousov 	}
3960f5216b9aSBruce Evans 	return (ENOSYS);
3961df8bae1dSRodney W. Grimes }
3962831d27a9SDon Lewis 
3963831d27a9SDon Lewis /*
39640c14ff0eSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using stored
39650c14ff0eSRobert Watson  * credentials rather than those of the current process.
3966831d27a9SDon Lewis  */
3967831d27a9SDon Lewis void
3968e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty)
3969831d27a9SDon Lewis {
3970a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
3971f1320723SAlfred Perlstein 	struct sigio *sigio;
3972831d27a9SDon Lewis 
3973a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
3974a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
3975a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
3976a3de221dSKonstantin Belousov 
3977f1320723SAlfred Perlstein 	SIGIO_LOCK();
3978f1320723SAlfred Perlstein 	sigio = *sigiop;
3979f1320723SAlfred Perlstein 	if (sigio == NULL) {
3980f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
3981f1320723SAlfred Perlstein 		return;
3982f1320723SAlfred Perlstein 	}
3983831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
3984628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
39852b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
39868451d0ddSKip Macy 			kern_psignal(sigio->sio_proc, sig);
3987628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
3988831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
3989831d27a9SDon Lewis 		struct proc *p;
3990831d27a9SDon Lewis 
3991f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
3992628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3993628d2653SJohn Baldwin 			PROC_LOCK(p);
3994e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
3995e806d352SJohn Baldwin 			    CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3996831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
39978451d0ddSKip Macy 				kern_psignal(p, sig);
3998628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3999628d2653SJohn Baldwin 		}
4000f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
4001831d27a9SDon Lewis 	}
4002f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
4003831d27a9SDon Lewis }
4004cb679c38SJonathan Lemon 
4005cb679c38SJonathan Lemon static int
4006cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
4007cb679c38SJonathan Lemon {
4008cb679c38SJonathan Lemon 	struct proc *p = curproc;
4009cb679c38SJonathan Lemon 
4010cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
4011cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
4012cb679c38SJonathan Lemon 
40139e590ff0SKonstantin Belousov 	knlist_add(p->p_klist, kn, 0);
4014cb679c38SJonathan Lemon 
4015cb679c38SJonathan Lemon 	return (0);
4016cb679c38SJonathan Lemon }
4017cb679c38SJonathan Lemon 
4018cb679c38SJonathan Lemon static void
4019cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
4020cb679c38SJonathan Lemon {
4021cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
4022cb679c38SJonathan Lemon 
40239e590ff0SKonstantin Belousov 	knlist_remove(p->p_klist, kn, 0);
4024cb679c38SJonathan Lemon }
4025cb679c38SJonathan Lemon 
4026cb679c38SJonathan Lemon /*
4027cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
4028cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
4029cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
4030cb679c38SJonathan Lemon  * isn't worth the trouble.
4031cb679c38SJonathan Lemon  */
4032cb679c38SJonathan Lemon static int
4033cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
4034cb679c38SJonathan Lemon {
4035cb679c38SJonathan Lemon 
4036cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
4037cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
4038cb679c38SJonathan Lemon 
4039cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
4040cb679c38SJonathan Lemon 			kn->kn_data++;
4041cb679c38SJonathan Lemon 	}
4042cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
4043cb679c38SJonathan Lemon }
404490af4afaSJohn Baldwin 
404590af4afaSJohn Baldwin struct sigacts *
404690af4afaSJohn Baldwin sigacts_alloc(void)
404790af4afaSJohn Baldwin {
404890af4afaSJohn Baldwin 	struct sigacts *ps;
404990af4afaSJohn Baldwin 
405090af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4051ce8daaadSMateusz Guzik 	refcount_init(&ps->ps_refcnt, 1);
405290af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
405390af4afaSJohn Baldwin 	return (ps);
405490af4afaSJohn Baldwin }
405590af4afaSJohn Baldwin 
405690af4afaSJohn Baldwin void
405790af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
405890af4afaSJohn Baldwin {
405990af4afaSJohn Baldwin 
4060c959c237SMateusz Guzik 	if (refcount_release(&ps->ps_refcnt) == 0)
4061c959c237SMateusz Guzik 		return;
406290af4afaSJohn Baldwin 	mtx_destroy(&ps->ps_mtx);
406390af4afaSJohn Baldwin 	free(ps, M_SUBPROC);
406490af4afaSJohn Baldwin }
406590af4afaSJohn Baldwin 
406690af4afaSJohn Baldwin struct sigacts *
406790af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
406890af4afaSJohn Baldwin {
4069c959c237SMateusz Guzik 
4070c959c237SMateusz Guzik 	refcount_acquire(&ps->ps_refcnt);
407190af4afaSJohn Baldwin 	return (ps);
407290af4afaSJohn Baldwin }
407390af4afaSJohn Baldwin 
407490af4afaSJohn Baldwin void
407590af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
407690af4afaSJohn Baldwin {
407790af4afaSJohn Baldwin 
407890af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
407990af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
408090af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
408190af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
408290af4afaSJohn Baldwin }
408390af4afaSJohn Baldwin 
408490af4afaSJohn Baldwin int
408590af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
408690af4afaSJohn Baldwin {
408790af4afaSJohn Baldwin 
4088d00c8ea4SMateusz Guzik 	return (ps->ps_refcnt > 1);
408990af4afaSJohn Baldwin }
4090079c5b9eSKyle Evans 
4091079c5b9eSKyle Evans void
4092079c5b9eSKyle Evans sig_drop_caught(struct proc *p)
4093079c5b9eSKyle Evans {
4094079c5b9eSKyle Evans 	int sig;
4095079c5b9eSKyle Evans 	struct sigacts *ps;
4096079c5b9eSKyle Evans 
4097079c5b9eSKyle Evans 	ps = p->p_sigacts;
4098079c5b9eSKyle Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
4099079c5b9eSKyle Evans 	mtx_assert(&ps->ps_mtx, MA_OWNED);
4100079c5b9eSKyle Evans 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
4101079c5b9eSKyle Evans 		sig = sig_ffs(&ps->ps_sigcatch);
4102079c5b9eSKyle Evans 		sigdflt(ps, sig);
4103079c5b9eSKyle Evans 		if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4104079c5b9eSKyle Evans 			sigqueue_delete_proc(p, sig);
4105079c5b9eSKyle Evans 	}
4106079c5b9eSKyle Evans }
4107146fc63fSKonstantin Belousov 
4108a113b17fSKonstantin Belousov static void
4109a113b17fSKonstantin Belousov sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4110a113b17fSKonstantin Belousov {
4111a113b17fSKonstantin Belousov 	ksiginfo_t ksi;
4112a113b17fSKonstantin Belousov 
4113a113b17fSKonstantin Belousov 	/*
4114a113b17fSKonstantin Belousov 	 * Prevent further fetches and SIGSEGVs, allowing thread to
4115a113b17fSKonstantin Belousov 	 * issue syscalls despite corruption.
4116a113b17fSKonstantin Belousov 	 */
4117a113b17fSKonstantin Belousov 	sigfastblock_clear(td);
4118a113b17fSKonstantin Belousov 
4119a113b17fSKonstantin Belousov 	if (!sendsig)
4120a113b17fSKonstantin Belousov 		return;
4121a113b17fSKonstantin Belousov 	ksiginfo_init_trap(&ksi);
4122a113b17fSKonstantin Belousov 	ksi.ksi_signo = SIGSEGV;
4123a113b17fSKonstantin Belousov 	ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4124a113b17fSKonstantin Belousov 	ksi.ksi_addr = td->td_sigblock_ptr;
4125a113b17fSKonstantin Belousov 	trapsignal(td, &ksi);
4126a113b17fSKonstantin Belousov }
4127a113b17fSKonstantin Belousov 
4128a113b17fSKonstantin Belousov static bool
4129a113b17fSKonstantin Belousov sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4130a113b17fSKonstantin Belousov {
4131a113b17fSKonstantin Belousov 	uint32_t res;
4132a113b17fSKonstantin Belousov 
4133a113b17fSKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4134a113b17fSKonstantin Belousov 		return (true);
4135a113b17fSKonstantin Belousov 	if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4136a113b17fSKonstantin Belousov 		sigfastblock_failed(td, sendsig, false);
4137a113b17fSKonstantin Belousov 		return (false);
4138a113b17fSKonstantin Belousov 	}
4139a113b17fSKonstantin Belousov 	*valp = res;
4140a113b17fSKonstantin Belousov 	td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4141a113b17fSKonstantin Belousov 	return (true);
4142a113b17fSKonstantin Belousov }
4143a113b17fSKonstantin Belousov 
4144fb3c434bSKonstantin Belousov static void
4145fb3c434bSKonstantin Belousov sigfastblock_resched(struct thread *td, bool resched)
4146fb3c434bSKonstantin Belousov {
4147fb3c434bSKonstantin Belousov 	struct proc *p;
4148fb3c434bSKonstantin Belousov 
4149fb3c434bSKonstantin Belousov 	if (resched) {
4150fb3c434bSKonstantin Belousov 		p = td->td_proc;
4151fb3c434bSKonstantin Belousov 		PROC_LOCK(p);
4152fb3c434bSKonstantin Belousov 		reschedule_signals(p, td->td_sigmask, 0);
4153fb3c434bSKonstantin Belousov 		PROC_UNLOCK(p);
4154fb3c434bSKonstantin Belousov 	}
4155fb3c434bSKonstantin Belousov 	thread_lock(td);
4156fb3c434bSKonstantin Belousov 	td->td_flags |= TDF_ASTPENDING | TDF_NEEDSIGCHK;
4157fb3c434bSKonstantin Belousov 	thread_unlock(td);
4158fb3c434bSKonstantin Belousov }
4159fb3c434bSKonstantin Belousov 
4160146fc63fSKonstantin Belousov int
4161146fc63fSKonstantin Belousov sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4162146fc63fSKonstantin Belousov {
4163146fc63fSKonstantin Belousov 	struct proc *p;
4164146fc63fSKonstantin Belousov 	int error, res;
4165146fc63fSKonstantin Belousov 	uint32_t oldval;
4166146fc63fSKonstantin Belousov 
4167146fc63fSKonstantin Belousov 	error = 0;
4168a113b17fSKonstantin Belousov 	p = td->td_proc;
4169146fc63fSKonstantin Belousov 	switch (uap->cmd) {
4170146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_SETPTR:
4171146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4172146fc63fSKonstantin Belousov 			error = EBUSY;
4173146fc63fSKonstantin Belousov 			break;
4174146fc63fSKonstantin Belousov 		}
4175146fc63fSKonstantin Belousov 		if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4176146fc63fSKonstantin Belousov 			error = EINVAL;
4177146fc63fSKonstantin Belousov 			break;
4178146fc63fSKonstantin Belousov 		}
4179146fc63fSKonstantin Belousov 		td->td_pflags |= TDP_SIGFASTBLOCK;
4180146fc63fSKonstantin Belousov 		td->td_sigblock_ptr = uap->ptr;
4181146fc63fSKonstantin Belousov 		break;
4182146fc63fSKonstantin Belousov 
4183146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_UNBLOCK:
4184a113b17fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4185146fc63fSKonstantin Belousov 			error = EINVAL;
4186146fc63fSKonstantin Belousov 			break;
4187146fc63fSKonstantin Belousov 		}
4188a113b17fSKonstantin Belousov 
4189a113b17fSKonstantin Belousov 		for (;;) {
4190a113b17fSKonstantin Belousov 			res = casueword32(td->td_sigblock_ptr,
4191a113b17fSKonstantin Belousov 			    SIGFASTBLOCK_PEND, &oldval, 0);
4192146fc63fSKonstantin Belousov 			if (res == -1) {
4193146fc63fSKonstantin Belousov 				error = EFAULT;
4194a113b17fSKonstantin Belousov 				sigfastblock_failed(td, false, true);
4195146fc63fSKonstantin Belousov 				break;
4196146fc63fSKonstantin Belousov 			}
4197a113b17fSKonstantin Belousov 			if (res == 0)
4198a113b17fSKonstantin Belousov 				break;
4199a113b17fSKonstantin Belousov 			MPASS(res == 1);
4200146fc63fSKonstantin Belousov 			if (oldval != SIGFASTBLOCK_PEND) {
4201146fc63fSKonstantin Belousov 				error = EBUSY;
4202146fc63fSKonstantin Belousov 				break;
4203146fc63fSKonstantin Belousov 			}
4204146fc63fSKonstantin Belousov 			error = thread_check_susp(td, false);
4205146fc63fSKonstantin Belousov 			if (error != 0)
4206146fc63fSKonstantin Belousov 				break;
4207146fc63fSKonstantin Belousov 		}
4208a113b17fSKonstantin Belousov 		if (error != 0)
4209a113b17fSKonstantin Belousov 			break;
4210a113b17fSKonstantin Belousov 
4211a113b17fSKonstantin Belousov 		/*
4212a113b17fSKonstantin Belousov 		 * td_sigblock_val is cleared there, but not on a
4213a113b17fSKonstantin Belousov 		 * syscall exit.  The end effect is that a single
4214a113b17fSKonstantin Belousov 		 * interruptible sleep, while user sigblock word is
4215a113b17fSKonstantin Belousov 		 * set, might return EINTR or ERESTART to usermode
4216a113b17fSKonstantin Belousov 		 * without delivering signal.  All further sleeps,
4217a113b17fSKonstantin Belousov 		 * until userspace clears the word and does
4218a113b17fSKonstantin Belousov 		 * sigfastblock(UNBLOCK), observe current word and no
4219a113b17fSKonstantin Belousov 		 * longer get interrupted.  It is slight
4220a113b17fSKonstantin Belousov 		 * non-conformance, with alternative to have read the
4221a113b17fSKonstantin Belousov 		 * sigblock word on each syscall entry.
4222a113b17fSKonstantin Belousov 		 */
4223146fc63fSKonstantin Belousov 		td->td_sigblock_val = 0;
4224146fc63fSKonstantin Belousov 
4225146fc63fSKonstantin Belousov 		/*
4226146fc63fSKonstantin Belousov 		 * Rely on normal ast mechanism to deliver pending
4227146fc63fSKonstantin Belousov 		 * signals to current thread.  But notify others about
4228146fc63fSKonstantin Belousov 		 * fake unblock.
4229146fc63fSKonstantin Belousov 		 */
4230fb3c434bSKonstantin Belousov 		sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4231fb3c434bSKonstantin Belousov 
4232146fc63fSKonstantin Belousov 		break;
4233146fc63fSKonstantin Belousov 
4234146fc63fSKonstantin Belousov 	case SIGFASTBLOCK_UNSETPTR:
4235146fc63fSKonstantin Belousov 		if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4236146fc63fSKonstantin Belousov 			error = EINVAL;
4237146fc63fSKonstantin Belousov 			break;
4238146fc63fSKonstantin Belousov 		}
4239a113b17fSKonstantin Belousov 		if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4240146fc63fSKonstantin Belousov 			error = EFAULT;
4241146fc63fSKonstantin Belousov 			break;
4242146fc63fSKonstantin Belousov 		}
4243146fc63fSKonstantin Belousov 		if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4244146fc63fSKonstantin Belousov 			error = EBUSY;
4245146fc63fSKonstantin Belousov 			break;
4246146fc63fSKonstantin Belousov 		}
4247a113b17fSKonstantin Belousov 		sigfastblock_clear(td);
4248146fc63fSKonstantin Belousov 		break;
4249146fc63fSKonstantin Belousov 
4250146fc63fSKonstantin Belousov 	default:
4251146fc63fSKonstantin Belousov 		error = EINVAL;
4252146fc63fSKonstantin Belousov 		break;
4253146fc63fSKonstantin Belousov 	}
4254146fc63fSKonstantin Belousov 	return (error);
4255146fc63fSKonstantin Belousov }
4256146fc63fSKonstantin Belousov 
4257146fc63fSKonstantin Belousov void
4258a113b17fSKonstantin Belousov sigfastblock_clear(struct thread *td)
4259146fc63fSKonstantin Belousov {
4260a113b17fSKonstantin Belousov 	bool resched;
4261146fc63fSKonstantin Belousov 
4262146fc63fSKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4263146fc63fSKonstantin Belousov 		return;
4264a113b17fSKonstantin Belousov 	td->td_sigblock_val = 0;
426500ebd809SKonstantin Belousov 	resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
426600ebd809SKonstantin Belousov 	    SIGPENDING(td);
4267a113b17fSKonstantin Belousov 	td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4268fb3c434bSKonstantin Belousov 	sigfastblock_resched(td, resched);
4269146fc63fSKonstantin Belousov }
4270146fc63fSKonstantin Belousov 
4271146fc63fSKonstantin Belousov void
4272a113b17fSKonstantin Belousov sigfastblock_fetch(struct thread *td)
4273146fc63fSKonstantin Belousov {
4274a113b17fSKonstantin Belousov 	uint32_t val;
4275146fc63fSKonstantin Belousov 
4276a113b17fSKonstantin Belousov 	(void)sigfastblock_fetch_sig(td, true, &val);
4277a113b17fSKonstantin Belousov }
4278146fc63fSKonstantin Belousov 
42790bc52b0bSKonstantin Belousov static void
42800bc52b0bSKonstantin Belousov sigfastblock_setpend1(struct thread *td)
4281a113b17fSKonstantin Belousov {
4282a113b17fSKonstantin Belousov 	int res;
4283a113b17fSKonstantin Belousov 	uint32_t oldval;
4284a113b17fSKonstantin Belousov 
4285513320c0SKonstantin Belousov 	if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4286a113b17fSKonstantin Belousov 		return;
4287a113b17fSKonstantin Belousov 	res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4288a113b17fSKonstantin Belousov 	if (res == -1) {
4289a113b17fSKonstantin Belousov 		sigfastblock_failed(td, true, false);
4290a113b17fSKonstantin Belousov 		return;
4291a113b17fSKonstantin Belousov 	}
4292a113b17fSKonstantin Belousov 	for (;;) {
4293a113b17fSKonstantin Belousov 		res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4294a113b17fSKonstantin Belousov 		    oldval | SIGFASTBLOCK_PEND);
4295a113b17fSKonstantin Belousov 		if (res == -1) {
4296a113b17fSKonstantin Belousov 			sigfastblock_failed(td, true, true);
4297a113b17fSKonstantin Belousov 			return;
4298a113b17fSKonstantin Belousov 		}
4299a113b17fSKonstantin Belousov 		if (res == 0) {
4300a113b17fSKonstantin Belousov 			td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4301a113b17fSKonstantin Belousov 			td->td_pflags &= ~TDP_SIGFASTPENDING;
4302a113b17fSKonstantin Belousov 			break;
4303a113b17fSKonstantin Belousov 		}
4304a113b17fSKonstantin Belousov 		MPASS(res == 1);
4305a113b17fSKonstantin Belousov 		if (thread_check_susp(td, false) != 0)
4306a113b17fSKonstantin Belousov 			break;
4307a113b17fSKonstantin Belousov 	}
4308146fc63fSKonstantin Belousov }
43090bc52b0bSKonstantin Belousov 
43100bc52b0bSKonstantin Belousov void
43110bc52b0bSKonstantin Belousov sigfastblock_setpend(struct thread *td, bool resched)
43120bc52b0bSKonstantin Belousov {
43130bc52b0bSKonstantin Belousov 	struct proc *p;
43140bc52b0bSKonstantin Belousov 
43150bc52b0bSKonstantin Belousov 	sigfastblock_setpend1(td);
43160bc52b0bSKonstantin Belousov 	if (resched) {
43170bc52b0bSKonstantin Belousov 		p = td->td_proc;
43180bc52b0bSKonstantin Belousov 		PROC_LOCK(p);
43190bc52b0bSKonstantin Belousov 		reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);
43200bc52b0bSKonstantin Belousov 		PROC_UNLOCK(p);
43210bc52b0bSKonstantin Belousov 	}
43220bc52b0bSKonstantin Belousov }
4323