xref: /freebsd/sys/kern/kern_sig.c (revision 537d0fb138f94640c40c7d9b12b6e20bf35a3e23)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1869a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
405591b823SEivind Eklund #include "opt_compat.h"
41aa14e9b7SMark Johnston #include "opt_gzio.h"
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>
50842ab62bSRui Paulo #include <sys/bus.h>
514a144410SRobert Watson #include <sys/capsicum.h>
52238510fcSJason Evans #include <sys/condvar.h>
53854dc8c2SJohn Baldwin #include <sys/event.h>
54854dc8c2SJohn Baldwin #include <sys/fcntl.h>
55e7228204SAlfred Perlstein #include <sys/imgact.h>
56854dc8c2SJohn Baldwin #include <sys/kernel.h>
570384fff8SJason Evans #include <sys/ktr.h>
58df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
59854dc8c2SJohn Baldwin #include <sys/lock.h>
60854dc8c2SJohn Baldwin #include <sys/malloc.h>
61854dc8c2SJohn Baldwin #include <sys/mutex.h>
62c959c237SMateusz Guzik #include <sys/refcount.h>
63854dc8c2SJohn Baldwin #include <sys/namei.h>
64854dc8c2SJohn Baldwin #include <sys/proc.h>
65cfb5f768SJonathan Anderson #include <sys/procdesc.h>
666aeb05d7STom Rhodes #include <sys/posix4.h>
67854dc8c2SJohn Baldwin #include <sys/pioctl.h>
68b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h>
69c31146a1SJohn Baldwin #include <sys/resourcevar.h>
705d217f17SJohn Birrell #include <sys/sdt.h>
7136b208e0SRobert Watson #include <sys/sbuf.h>
7244f3b092SJohn Baldwin #include <sys/sleepqueue.h>
736caa8a15SJohn Baldwin #include <sys/smp.h>
74df8bae1dSRodney W. Grimes #include <sys/stat.h>
751005a129SJohn Baldwin #include <sys/sx.h>
768f19eb88SIan Dowse #include <sys/syscallsubr.h>
77c87e2930SDavid Greenman #include <sys/sysctl.h>
78854dc8c2SJohn Baldwin #include <sys/sysent.h>
79854dc8c2SJohn Baldwin #include <sys/syslog.h>
80854dc8c2SJohn Baldwin #include <sys/sysproto.h>
8156c06c4bSDavid Xu #include <sys/timers.h>
8206ae1e91SMatthew Dillon #include <sys/unistd.h>
83854dc8c2SJohn Baldwin #include <sys/wait.h>
849104847fSDavid Xu #include <vm/vm.h>
859104847fSDavid Xu #include <vm/vm_extern.h>
869104847fSDavid Xu #include <vm/uma.h>
87df8bae1dSRodney W. Grimes 
88e7228204SAlfred Perlstein #include <sys/jail.h>
89e7228204SAlfred Perlstein 
90df8bae1dSRodney W. Grimes #include <machine/cpu.h>
91df8bae1dSRodney W. Grimes 
92bfd7575aSWayne Salamon #include <security/audit/audit.h>
93bfd7575aSWayne Salamon 
946f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
956f841fb7SMarcel Moolenaar 
965d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
9736160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__send,
9836160958SMark Johnston     "struct thread *", "struct proc *", "int");
9936160958SMark Johnston SDT_PROBE_DEFINE2(proc, , , signal__clear,
10036160958SMark Johnston     "int", "ksiginfo_t *");
10136160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__discard,
1027b77e1feSMark Johnston     "struct thread *", "struct proc *", "int");
1035d217f17SJohn Birrell 
1044d77a549SAlfred Perlstein static int	coredump(struct thread *);
105a3de221dSKonstantin Belousov static int	killpg1(struct thread *td, int sig, int pgid, int all,
106a3de221dSKonstantin Belousov 		    ksiginfo_t *ksi);
1073cf3b9f0SJohn Baldwin static int	issignal(struct thread *td);
1084d77a549SAlfred Perlstein static int	sigprop(int sig);
10994f0972bSDavid Xu static void	tdsigwakeup(struct thread *, int, sig_t, int);
1106f56cb8dSKonstantin Belousov static int	sig_suspend_threads(struct thread *, struct proc *, int);
111cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
112cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
113cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
1144093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
1159104847fSDavid Xu static void	sigqueue_start(void);
116cb679c38SJonathan Lemon 
1179104847fSDavid Xu static uma_zone_t	ksiginfo_zone = NULL;
118e76d823bSRobert Watson struct filterops sig_filtops = {
119e76d823bSRobert Watson 	.f_isfd = 0,
120e76d823bSRobert Watson 	.f_attach = filt_sigattach,
121e76d823bSRobert Watson 	.f_detach = filt_sigdetach,
122e76d823bSRobert Watson 	.f_event = filt_signal,
123e76d823bSRobert Watson };
124cb679c38SJonathan Lemon 
125fc8cca02SJohn Baldwin static int	kern_logsigexit = 1;
1263d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1273d177f46SBill Fumerola     &kern_logsigexit, 0,
1283d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
12957308494SJoerg Wunsch 
130f71a882fSDavid Xu static int	kern_forcesigexit = 1;
131f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
132f71a882fSDavid Xu     &kern_forcesigexit, 0, "Force trap signal to be handled");
133f71a882fSDavid Xu 
1346472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
1356472ac3dSEd Schouten     "POSIX real time signal");
1369104847fSDavid Xu 
1379104847fSDavid Xu static int	max_pending_per_proc = 128;
1389104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
1399104847fSDavid Xu     &max_pending_per_proc, 0, "Max pending signals per proc");
1409104847fSDavid Xu 
1419104847fSDavid Xu static int	preallocate_siginfo = 1024;
142af3b2549SHans Petter Selasky SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
1439104847fSDavid Xu     &preallocate_siginfo, 0, "Preallocated signal memory size");
1449104847fSDavid Xu 
1459104847fSDavid Xu static int	signal_overflow = 0;
146761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
1479104847fSDavid Xu     &signal_overflow, 0, "Number of signals overflew");
1489104847fSDavid Xu 
1499104847fSDavid Xu static int	signal_alloc_fail = 0;
150761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
1519104847fSDavid Xu     &signal_alloc_fail, 0, "signals failed to be allocated");
1529104847fSDavid Xu 
153f5a077c3SKonstantin Belousov static int	kern_lognosys = 0;
154f5a077c3SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
155f5a077c3SKonstantin Belousov     "Log invalid syscalls");
156f5a077c3SKonstantin Belousov 
1579104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
1589104847fSDavid Xu 
1592b87b6d4SRobert Watson /*
1602b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1612b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1622b87b6d4SRobert Watson  * in the right situations.
1632b87b6d4SRobert Watson  */
1642b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1652b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1662b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1672b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1682b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1692b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1702b87b6d4SRobert Watson 
171fc8cca02SJohn Baldwin static int	sugid_coredump;
172af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
1738b5adf9dSJoseph Koshy     &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
174c87e2930SDavid Greenman 
175b0c9d4d7SPawel Jakub Dawidek static int	capmode_coredump;
176af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
177b0c9d4d7SPawel Jakub Dawidek     &capmode_coredump, 0, "Allow processes in capability mode to dump core");
178b0c9d4d7SPawel Jakub Dawidek 
179e5a28db9SPaul Saab static int	do_coredump = 1;
180e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
181e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
182e5a28db9SPaul Saab 
1836141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
1846141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
1856141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
1866141e04aSJohn-Mark Gurney 
1870da9e11bSRui Paulo static int	coredump_devctl = 0;
188eb6368d4SRui Paulo SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
189eb6368d4SRui Paulo 	0, "Generate a devctl notification when processes coredump");
190eb6368d4SRui Paulo 
1912c42a146SMarcel Moolenaar /*
1922c42a146SMarcel Moolenaar  * Signal properties and actions.
1932c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1942c42a146SMarcel Moolenaar  * according to the following properties:
1952c42a146SMarcel Moolenaar  */
196fd50a707SBrooks Davis #define	SIGPROP_KILL		0x01	/* terminates process by default */
197fd50a707SBrooks Davis #define	SIGPROP_CORE		0x02	/* ditto and coredumps */
198fd50a707SBrooks Davis #define	SIGPROP_STOP		0x04	/* suspend process */
199fd50a707SBrooks Davis #define	SIGPROP_TTYSTOP		0x08	/* ditto, from tty */
200fd50a707SBrooks Davis #define	SIGPROP_IGNORE		0x10	/* ignore by default */
201fd50a707SBrooks Davis #define	SIGPROP_CONT		0x20	/* continue if suspended */
202fd50a707SBrooks Davis #define	SIGPROP_CANTMASK	0x40	/* non-maskable, catchable */
203df8bae1dSRodney W. Grimes 
2042c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
205ed6d876bSBrooks Davis 	[SIGHUP] =	SIGPROP_KILL,
206ed6d876bSBrooks Davis 	[SIGINT] =	SIGPROP_KILL,
207ed6d876bSBrooks Davis 	[SIGQUIT] =	SIGPROP_KILL | SIGPROP_CORE,
208ed6d876bSBrooks Davis 	[SIGILL] =	SIGPROP_KILL | SIGPROP_CORE,
209ed6d876bSBrooks Davis 	[SIGTRAP] =	SIGPROP_KILL | SIGPROP_CORE,
210ed6d876bSBrooks Davis 	[SIGABRT] =	SIGPROP_KILL | SIGPROP_CORE,
211ed6d876bSBrooks Davis 	[SIGEMT] =	SIGPROP_KILL | SIGPROP_CORE,
212ed6d876bSBrooks Davis 	[SIGFPE] =	SIGPROP_KILL | SIGPROP_CORE,
213ed6d876bSBrooks Davis 	[SIGKILL] =	SIGPROP_KILL,
214ed6d876bSBrooks Davis 	[SIGBUS] =	SIGPROP_KILL | SIGPROP_CORE,
215ed6d876bSBrooks Davis 	[SIGSEGV] =	SIGPROP_KILL | SIGPROP_CORE,
216ed6d876bSBrooks Davis 	[SIGSYS] =	SIGPROP_KILL | SIGPROP_CORE,
217ed6d876bSBrooks Davis 	[SIGPIPE] =	SIGPROP_KILL,
218ed6d876bSBrooks Davis 	[SIGALRM] =	SIGPROP_KILL,
219ed6d876bSBrooks Davis 	[SIGTERM] =	SIGPROP_KILL,
220ed6d876bSBrooks Davis 	[SIGURG] =	SIGPROP_IGNORE,
221ed6d876bSBrooks Davis 	[SIGSTOP] =	SIGPROP_STOP,
222ed6d876bSBrooks Davis 	[SIGTSTP] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
223ed6d876bSBrooks Davis 	[SIGCONT] =	SIGPROP_IGNORE | SIGPROP_CONT,
224ed6d876bSBrooks Davis 	[SIGCHLD] =	SIGPROP_IGNORE,
225ed6d876bSBrooks Davis 	[SIGTTIN] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
226ed6d876bSBrooks Davis 	[SIGTTOU] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
227ed6d876bSBrooks Davis 	[SIGIO] =	SIGPROP_IGNORE,
228ed6d876bSBrooks Davis 	[SIGXCPU] =	SIGPROP_KILL,
229ed6d876bSBrooks Davis 	[SIGXFSZ] =	SIGPROP_KILL,
230ed6d876bSBrooks Davis 	[SIGVTALRM] =	SIGPROP_KILL,
231ed6d876bSBrooks Davis 	[SIGPROF] =	SIGPROP_KILL,
232ed6d876bSBrooks Davis 	[SIGWINCH] =	SIGPROP_IGNORE,
233ed6d876bSBrooks Davis 	[SIGINFO] =	SIGPROP_IGNORE,
234ed6d876bSBrooks Davis 	[SIGUSR1] =	SIGPROP_KILL,
235ed6d876bSBrooks Davis 	[SIGUSR2] =	SIGPROP_KILL,
2362c42a146SMarcel Moolenaar };
2372c42a146SMarcel Moolenaar 
23880a8b0f3SKonstantin Belousov static void reschedule_signals(struct proc *p, sigset_t block, int flags);
2396b286ee8SKonstantin Belousov 
2409104847fSDavid Xu static void
2419104847fSDavid Xu sigqueue_start(void)
2429104847fSDavid Xu {
2439104847fSDavid Xu 	ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
2449104847fSDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2459104847fSDavid Xu 	uma_prealloc(ksiginfo_zone, preallocate_siginfo);
246b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
247b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
248b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
2499104847fSDavid Xu }
2509104847fSDavid Xu 
2515da49fcbSDavid Xu ksiginfo_t *
252ebceaf6dSDavid Xu ksiginfo_alloc(int wait)
2539104847fSDavid Xu {
254ebceaf6dSDavid Xu 	int flags;
255ebceaf6dSDavid Xu 
256ebceaf6dSDavid Xu 	flags = M_ZERO;
257ebceaf6dSDavid Xu 	if (! wait)
258ebceaf6dSDavid Xu 		flags |= M_NOWAIT;
2599104847fSDavid Xu 	if (ksiginfo_zone != NULL)
260ebceaf6dSDavid Xu 		return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
2619104847fSDavid Xu 	return (NULL);
2629104847fSDavid Xu }
2639104847fSDavid Xu 
2645da49fcbSDavid Xu void
2659104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi)
2669104847fSDavid Xu {
2679104847fSDavid Xu 	uma_zfree(ksiginfo_zone, ksi);
2689104847fSDavid Xu }
2699104847fSDavid Xu 
2705da49fcbSDavid Xu static __inline int
2715da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi)
2725da49fcbSDavid Xu {
2735da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT)) {
2745da49fcbSDavid Xu 		uma_zfree(ksiginfo_zone, ksi);
2755da49fcbSDavid Xu 		return (1);
2765da49fcbSDavid Xu 	}
2775da49fcbSDavid Xu 	return (0);
2785da49fcbSDavid Xu }
2795da49fcbSDavid Xu 
2809104847fSDavid Xu void
2819104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p)
2829104847fSDavid Xu {
2839104847fSDavid Xu 	SIGEMPTYSET(list->sq_signals);
2843dfcaad6SDavid Xu 	SIGEMPTYSET(list->sq_kill);
28582a4538fSEric Badger 	SIGEMPTYSET(list->sq_ptrace);
2869104847fSDavid Xu 	TAILQ_INIT(&list->sq_list);
2879104847fSDavid Xu 	list->sq_proc = p;
2889104847fSDavid Xu 	list->sq_flags = SQ_INIT;
2899104847fSDavid Xu }
2909104847fSDavid Xu 
2919104847fSDavid Xu /*
2929104847fSDavid Xu  * Get a signal's ksiginfo.
2939104847fSDavid Xu  * Return:
2949104847fSDavid Xu  *	0	-	signal not found
2959104847fSDavid Xu  *	others	-	signal number
2969104847fSDavid Xu  */
297a5799a4fSKonstantin Belousov static int
2989104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
2999104847fSDavid Xu {
3009104847fSDavid Xu 	struct proc *p = sq->sq_proc;
3019104847fSDavid Xu 	struct ksiginfo *ksi, *next;
3029104847fSDavid Xu 	int count = 0;
3039104847fSDavid Xu 
3049104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
3059104847fSDavid Xu 
3069104847fSDavid Xu 	if (!SIGISMEMBER(sq->sq_signals, signo))
3079104847fSDavid Xu 		return (0);
3089104847fSDavid Xu 
30982a4538fSEric Badger 	if (SIGISMEMBER(sq->sq_ptrace, signo)) {
31082a4538fSEric Badger 		count++;
31182a4538fSEric Badger 		SIGDELSET(sq->sq_ptrace, signo);
31282a4538fSEric Badger 		si->ksi_flags |= KSI_PTRACE;
31382a4538fSEric Badger 	}
3143dfcaad6SDavid Xu 	if (SIGISMEMBER(sq->sq_kill, signo)) {
3153dfcaad6SDavid Xu 		count++;
31682a4538fSEric Badger 		if (count == 1)
3173dfcaad6SDavid Xu 			SIGDELSET(sq->sq_kill, signo);
3183dfcaad6SDavid Xu 	}
3193dfcaad6SDavid Xu 
3205c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
3219104847fSDavid Xu 		if (ksi->ksi_signo == signo) {
3229104847fSDavid Xu 			if (count == 0) {
3239104847fSDavid Xu 				TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
3245da49fcbSDavid Xu 				ksi->ksi_sigq = NULL;
3259104847fSDavid Xu 				ksiginfo_copy(ksi, si);
3265da49fcbSDavid Xu 				if (ksiginfo_tryfree(ksi) && p != NULL)
3279104847fSDavid Xu 					p->p_pendingcnt--;
3289104847fSDavid Xu 			}
329016fa302SDavid Xu 			if (++count > 1)
330016fa302SDavid Xu 				break;
3319104847fSDavid Xu 		}
3329104847fSDavid Xu 	}
3339104847fSDavid Xu 
3349104847fSDavid Xu 	if (count <= 1)
3359104847fSDavid Xu 		SIGDELSET(sq->sq_signals, signo);
3369104847fSDavid Xu 	si->ksi_signo = signo;
3379104847fSDavid Xu 	return (signo);
3389104847fSDavid Xu }
3399104847fSDavid Xu 
3405da49fcbSDavid Xu void
3415da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi)
3425da49fcbSDavid Xu {
3435da49fcbSDavid Xu 	struct ksiginfo *kp;
3445da49fcbSDavid Xu 	struct proc	*p;
3455da49fcbSDavid Xu 	sigqueue_t	*sq;
3465da49fcbSDavid Xu 
347ebceaf6dSDavid Xu 	if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
3485da49fcbSDavid Xu 		return;
3495da49fcbSDavid Xu 
3505da49fcbSDavid Xu 	p = sq->sq_proc;
3515da49fcbSDavid Xu 	TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
3525da49fcbSDavid Xu 	ksi->ksi_sigq = NULL;
3535da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
3545da49fcbSDavid Xu 		p->p_pendingcnt--;
3555da49fcbSDavid Xu 
3565da49fcbSDavid Xu 	for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
3575da49fcbSDavid Xu 	     kp = TAILQ_NEXT(kp, ksi_link)) {
3585da49fcbSDavid Xu 		if (kp->ksi_signo == ksi->ksi_signo)
3595da49fcbSDavid Xu 			break;
3605da49fcbSDavid Xu 	}
36182a4538fSEric Badger 	if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
36282a4538fSEric Badger 	    !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
3635da49fcbSDavid Xu 		SIGDELSET(sq->sq_signals, ksi->ksi_signo);
3645da49fcbSDavid Xu }
3655da49fcbSDavid Xu 
366a5799a4fSKonstantin Belousov static int
3679104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
3689104847fSDavid Xu {
3699104847fSDavid Xu 	struct proc *p = sq->sq_proc;
3709104847fSDavid Xu 	struct ksiginfo *ksi;
3719104847fSDavid Xu 	int ret = 0;
3729104847fSDavid Xu 
3739104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
3749104847fSDavid Xu 
37582a4538fSEric Badger 	/*
37682a4538fSEric Badger 	 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
37782a4538fSEric Badger 	 * for these signals.
37882a4538fSEric Badger 	 */
3793dfcaad6SDavid Xu 	if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
3803dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
3819104847fSDavid Xu 		goto out_set_bit;
3823dfcaad6SDavid Xu 	}
3839104847fSDavid Xu 
3845da49fcbSDavid Xu 	/* directly insert the ksi, don't copy it */
3855da49fcbSDavid Xu 	if (si->ksi_flags & KSI_INS) {
3866a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
3876a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
3886a671a6bSKonstantin Belousov 		else
3895da49fcbSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
3905da49fcbSDavid Xu 		si->ksi_sigq = sq;
3915da49fcbSDavid Xu 		goto out_set_bit;
3925da49fcbSDavid Xu 	}
3935da49fcbSDavid Xu 
3943dfcaad6SDavid Xu 	if (__predict_false(ksiginfo_zone == NULL)) {
3953dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
3969104847fSDavid Xu 		goto out_set_bit;
3973dfcaad6SDavid Xu 	}
3989104847fSDavid Xu 
399ebceaf6dSDavid Xu 	if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
4009104847fSDavid Xu 		signal_overflow++;
4019104847fSDavid Xu 		ret = EAGAIN;
402ebceaf6dSDavid Xu 	} else if ((ksi = ksiginfo_alloc(0)) == NULL) {
4039104847fSDavid Xu 		signal_alloc_fail++;
4049104847fSDavid Xu 		ret = EAGAIN;
4059104847fSDavid Xu 	} else {
4069104847fSDavid Xu 		if (p != NULL)
4079104847fSDavid Xu 			p->p_pendingcnt++;
4089104847fSDavid Xu 		ksiginfo_copy(si, ksi);
4099104847fSDavid Xu 		ksi->ksi_signo = signo;
4106a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
4116a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
4126a671a6bSKonstantin Belousov 		else
4139104847fSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
4145da49fcbSDavid Xu 		ksi->ksi_sigq = sq;
4159104847fSDavid Xu 	}
4169104847fSDavid Xu 
41782a4538fSEric Badger 	if (ret != 0) {
41882a4538fSEric Badger 		if ((si->ksi_flags & KSI_PTRACE) != 0) {
41982a4538fSEric Badger 			SIGADDSET(sq->sq_ptrace, signo);
42082a4538fSEric Badger 			ret = 0;
42182a4538fSEric Badger 			goto out_set_bit;
42282a4538fSEric Badger 		} else if ((si->ksi_flags & KSI_TRAP) != 0 ||
423a3de221dSKonstantin Belousov 		    (si->ksi_flags & KSI_SIGQ) == 0) {
4243dfcaad6SDavid Xu 			SIGADDSET(sq->sq_kill, signo);
4259104847fSDavid Xu 			ret = 0;
4269104847fSDavid Xu 			goto out_set_bit;
4279104847fSDavid Xu 		}
4289104847fSDavid Xu 		return (ret);
42982a4538fSEric Badger 	}
4309104847fSDavid Xu 
4319104847fSDavid Xu out_set_bit:
4329104847fSDavid Xu 	SIGADDSET(sq->sq_signals, signo);
4339104847fSDavid Xu 	return (ret);
4349104847fSDavid Xu }
4359104847fSDavid Xu 
4369104847fSDavid Xu void
4379104847fSDavid Xu sigqueue_flush(sigqueue_t *sq)
4389104847fSDavid Xu {
4399104847fSDavid Xu 	struct proc *p = sq->sq_proc;
4409104847fSDavid Xu 	ksiginfo_t *ksi;
4419104847fSDavid Xu 
4429104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
4439104847fSDavid Xu 
4445da49fcbSDavid Xu 	if (p != NULL)
4455da49fcbSDavid Xu 		PROC_LOCK_ASSERT(p, MA_OWNED);
4465da49fcbSDavid Xu 
4479104847fSDavid Xu 	while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
4489104847fSDavid Xu 		TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4495da49fcbSDavid Xu 		ksi->ksi_sigq = NULL;
4505da49fcbSDavid Xu 		if (ksiginfo_tryfree(ksi) && p != NULL)
4519104847fSDavid Xu 			p->p_pendingcnt--;
4529104847fSDavid Xu 	}
4539104847fSDavid Xu 
4549104847fSDavid Xu 	SIGEMPTYSET(sq->sq_signals);
4553dfcaad6SDavid Xu 	SIGEMPTYSET(sq->sq_kill);
45682a4538fSEric Badger 	SIGEMPTYSET(sq->sq_ptrace);
4579104847fSDavid Xu }
4589104847fSDavid Xu 
459a5799a4fSKonstantin Belousov static void
460fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
4619104847fSDavid Xu {
462fc4ecc1dSDavid Xu 	sigset_t tmp;
4639104847fSDavid Xu 	struct proc *p1, *p2;
4649104847fSDavid Xu 	ksiginfo_t *ksi, *next;
4659104847fSDavid Xu 
4669104847fSDavid Xu 	KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
4679104847fSDavid Xu 	KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
4689104847fSDavid Xu 	p1 = src->sq_proc;
4699104847fSDavid Xu 	p2 = dst->sq_proc;
4709104847fSDavid Xu 	/* Move siginfo to target list */
4715c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
472fc4ecc1dSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
4739104847fSDavid Xu 			TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
4749104847fSDavid Xu 			if (p1 != NULL)
4759104847fSDavid Xu 				p1->p_pendingcnt--;
4769104847fSDavid Xu 			TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
4775da49fcbSDavid Xu 			ksi->ksi_sigq = dst;
4789104847fSDavid Xu 			if (p2 != NULL)
4799104847fSDavid Xu 				p2->p_pendingcnt++;
4809104847fSDavid Xu 		}
4819104847fSDavid Xu 	}
4829104847fSDavid Xu 
4839104847fSDavid Xu 	/* Move pending bits to target list */
4843dfcaad6SDavid Xu 	tmp = src->sq_kill;
485fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
4863dfcaad6SDavid Xu 	SIGSETOR(dst->sq_kill, tmp);
4873dfcaad6SDavid Xu 	SIGSETNAND(src->sq_kill, tmp);
4883dfcaad6SDavid Xu 
48982a4538fSEric Badger 	tmp = src->sq_ptrace;
49082a4538fSEric Badger 	SIGSETAND(tmp, *set);
49182a4538fSEric Badger 	SIGSETOR(dst->sq_ptrace, tmp);
49282a4538fSEric Badger 	SIGSETNAND(src->sq_ptrace, tmp);
49382a4538fSEric Badger 
4949104847fSDavid Xu 	tmp = src->sq_signals;
495fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
4969104847fSDavid Xu 	SIGSETOR(dst->sq_signals, tmp);
4979104847fSDavid Xu 	SIGSETNAND(src->sq_signals, tmp);
4989104847fSDavid Xu }
4999104847fSDavid Xu 
500407af02bSDavid Xu #if 0
501a5799a4fSKonstantin Belousov static void
5029104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
5039104847fSDavid Xu {
5049104847fSDavid Xu 	sigset_t set;
5059104847fSDavid Xu 
5069104847fSDavid Xu 	SIGEMPTYSET(set);
5079104847fSDavid Xu 	SIGADDSET(set, signo);
5089104847fSDavid Xu 	sigqueue_move_set(src, dst, &set);
5099104847fSDavid Xu }
510407af02bSDavid Xu #endif
5119104847fSDavid Xu 
512a5799a4fSKonstantin Belousov static void
513fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
5149104847fSDavid Xu {
5159104847fSDavid Xu 	struct proc *p = sq->sq_proc;
5169104847fSDavid Xu 	ksiginfo_t *ksi, *next;
5179104847fSDavid Xu 
5189104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
5199104847fSDavid Xu 
5209104847fSDavid Xu 	/* Remove siginfo queue */
5215c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
5229104847fSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
5239104847fSDavid Xu 			TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
5245da49fcbSDavid Xu 			ksi->ksi_sigq = NULL;
5255da49fcbSDavid Xu 			if (ksiginfo_tryfree(ksi) && p != NULL)
5269104847fSDavid Xu 				p->p_pendingcnt--;
5279104847fSDavid Xu 		}
5289104847fSDavid Xu 	}
5293dfcaad6SDavid Xu 	SIGSETNAND(sq->sq_kill, *set);
53082a4538fSEric Badger 	SIGSETNAND(sq->sq_ptrace, *set);
5319104847fSDavid Xu 	SIGSETNAND(sq->sq_signals, *set);
5329104847fSDavid Xu }
5339104847fSDavid Xu 
5349104847fSDavid Xu void
5359104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo)
5369104847fSDavid Xu {
5379104847fSDavid Xu 	sigset_t set;
5389104847fSDavid Xu 
5399104847fSDavid Xu 	SIGEMPTYSET(set);
5409104847fSDavid Xu 	SIGADDSET(set, signo);
5419104847fSDavid Xu 	sigqueue_delete_set(sq, &set);
5429104847fSDavid Xu }
5439104847fSDavid Xu 
5449104847fSDavid Xu /* Remove a set of signals for a process */
545a5799a4fSKonstantin Belousov static void
546fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
5479104847fSDavid Xu {
5489104847fSDavid Xu 	sigqueue_t worklist;
5499104847fSDavid Xu 	struct thread *td0;
5509104847fSDavid Xu 
5519104847fSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
5529104847fSDavid Xu 
5539104847fSDavid Xu 	sigqueue_init(&worklist, NULL);
5549104847fSDavid Xu 	sigqueue_move_set(&p->p_sigqueue, &worklist, set);
5559104847fSDavid Xu 
5569104847fSDavid Xu 	FOREACH_THREAD_IN_PROC(p, td0)
5579104847fSDavid Xu 		sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
5589104847fSDavid Xu 
5599104847fSDavid Xu 	sigqueue_flush(&worklist);
5609104847fSDavid Xu }
5619104847fSDavid Xu 
5629104847fSDavid Xu void
5639104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo)
5649104847fSDavid Xu {
5659104847fSDavid Xu 	sigset_t set;
5669104847fSDavid Xu 
5679104847fSDavid Xu 	SIGEMPTYSET(set);
5689104847fSDavid Xu 	SIGADDSET(set, signo);
5699104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
5709104847fSDavid Xu }
5719104847fSDavid Xu 
572a5799a4fSKonstantin Belousov static void
5739104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p)
5749104847fSDavid Xu {
5759104847fSDavid Xu 	sigset_t set;
5769104847fSDavid Xu 
5779104847fSDavid Xu 	SIGEMPTYSET(set);
5789104847fSDavid Xu 	SIGADDSET(set, SIGSTOP);
5799104847fSDavid Xu 	SIGADDSET(set, SIGTSTP);
5809104847fSDavid Xu 	SIGADDSET(set, SIGTTIN);
5819104847fSDavid Xu 	SIGADDSET(set, SIGTTOU);
5829104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
5839104847fSDavid Xu }
5849104847fSDavid Xu 
585fbbeeb6cSBruce Evans /*
5861968f37bSJohn Baldwin  * Determine signal that should be delivered to thread td, the current
5871968f37bSJohn Baldwin  * thread, 0 if none.  If there is a pending stop signal with default
588fbbeeb6cSBruce Evans  * action, the process stops in issignal().
589fbbeeb6cSBruce Evans  */
590fbbeeb6cSBruce Evans int
5913cf3b9f0SJohn Baldwin cursig(struct thread *td)
592fbbeeb6cSBruce Evans {
593c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
59490af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
595a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
5963cf3b9f0SJohn Baldwin 	return (SIGPENDING(td) ? issignal(td) : 0);
597fbbeeb6cSBruce Evans }
598fbbeeb6cSBruce Evans 
59979065dbaSBruce Evans /*
60079065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
6019104847fSDavid Xu  * mode.  This must be called whenever a signal is added to td_sigqueue or
6024093529dSJeff Roberson  * unmasked in td_sigmask.
60379065dbaSBruce Evans  */
60479065dbaSBruce Evans void
6054093529dSJeff Roberson signotify(struct thread *td)
60679065dbaSBruce Evans {
6074093529dSJeff Roberson 	struct proc *p;
6084093529dSJeff Roberson 
6094093529dSJeff Roberson 	p = td->td_proc;
61079065dbaSBruce Evans 
61179065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
6124093529dSJeff Roberson 
6138b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
614a54e85fdSJeff Roberson 		thread_lock(td);
6154093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
616a54e85fdSJeff Roberson 		thread_unlock(td);
61779065dbaSBruce Evans 	}
6188b94a061SJohn Baldwin }
6198b94a061SJohn Baldwin 
6208b94a061SJohn Baldwin int
6218b94a061SJohn Baldwin sigonstack(size_t sp)
6228b94a061SJohn Baldwin {
623a30ec4b9SDavid Xu 	struct thread *td = curthread;
6248b94a061SJohn Baldwin 
625a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
6261930e303SPoul-Henning Kamp #if defined(COMPAT_43)
627a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
628a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
629a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
6308b94a061SJohn Baldwin #else
631a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
6328b94a061SJohn Baldwin #endif
6338b94a061SJohn Baldwin 	    : 0);
6348b94a061SJohn Baldwin }
63579065dbaSBruce Evans 
6366f841fb7SMarcel Moolenaar static __inline int
6376f841fb7SMarcel Moolenaar sigprop(int sig)
6382c42a146SMarcel Moolenaar {
6396f841fb7SMarcel Moolenaar 
640ed6d876bSBrooks Davis 	if (sig > 0 && sig < nitems(sigproptbl))
641ed6d876bSBrooks Davis 		return (sigproptbl[sig]);
6422c42a146SMarcel Moolenaar 	return (0);
643df8bae1dSRodney W. Grimes }
6442c42a146SMarcel Moolenaar 
6454093529dSJeff Roberson int
6466f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
6472c42a146SMarcel Moolenaar {
6482c42a146SMarcel Moolenaar 	int i;
6492c42a146SMarcel Moolenaar 
6506f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
6512c42a146SMarcel Moolenaar 		if (set->__bits[i])
6522c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
653df8bae1dSRodney W. Grimes 	return (0);
654df8bae1dSRodney W. Grimes }
655df8bae1dSRodney W. Grimes 
656350ae563SKonstantin Belousov static bool
657ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag)
658350ae563SKonstantin Belousov {
659350ae563SKonstantin Belousov 
660c83655f3SKonstantin Belousov 	/*
661c83655f3SKonstantin Belousov 	 * SA_SIGINFO is reset when signal disposition is set to
662c83655f3SKonstantin Belousov 	 * ignore or default.  Other flags are kept according to user
663c83655f3SKonstantin Belousov 	 * settings.
664c83655f3SKonstantin Belousov 	 */
665c83655f3SKonstantin Belousov 	return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
666c83655f3SKonstantin Belousov 	    ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
667c83655f3SKonstantin Belousov 	    (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
668350ae563SKonstantin Belousov }
669350ae563SKonstantin Belousov 
6702c42a146SMarcel Moolenaar /*
6718f19eb88SIan Dowse  * kern_sigaction
6722c42a146SMarcel Moolenaar  * sigaction
67323eeeff7SPeter Wemm  * freebsd4_sigaction
6742c42a146SMarcel Moolenaar  * osigaction
6752c42a146SMarcel Moolenaar  */
6768f19eb88SIan Dowse int
677ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
678ea566832SEd Schouten     struct sigaction *oact, int flags)
679df8bae1dSRodney W. Grimes {
68090af4afaSJohn Baldwin 	struct sigacts *ps;
6818f19eb88SIan Dowse 	struct proc *p = td->td_proc;
682df8bae1dSRodney W. Grimes 
6832899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
6842c42a146SMarcel Moolenaar 		return (EINVAL);
685271ab240SKonstantin Belousov 	if (act != NULL && act->sa_handler != SIG_DFL &&
686271ab240SKonstantin Belousov 	    act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
687271ab240SKonstantin Belousov 	    SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
688271ab240SKonstantin Belousov 	    SA_NOCLDWAIT | SA_SIGINFO)) != 0)
6892d864174SKonstantin Belousov 		return (EINVAL);
6902c42a146SMarcel Moolenaar 
691628d2653SJohn Baldwin 	PROC_LOCK(p);
692628d2653SJohn Baldwin 	ps = p->p_sigacts;
69390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
6942c42a146SMarcel Moolenaar 	if (oact) {
6952c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
6962c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
6972c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
6982c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
6992c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
7002c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
7012c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
7022c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
7032c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
7042c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
70510c2b8e1SDavid E. O'Brien 		if (SIGISMEMBER(ps->ps_siginfo, sig)) {
7062c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
70710c2b8e1SDavid E. O'Brien 			oact->sa_sigaction =
70810c2b8e1SDavid E. O'Brien 			    (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
70910c2b8e1SDavid E. O'Brien 		} else
71010c2b8e1SDavid E. O'Brien 			oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
71190af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
7122c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
71390af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
7142c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
7152c42a146SMarcel Moolenaar 	}
7162c42a146SMarcel Moolenaar 	if (act) {
7172c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
718628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
71990af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
720628d2653SJohn Baldwin 			PROC_UNLOCK(p);
7212c42a146SMarcel Moolenaar 			return (EINVAL);
722628d2653SJohn Baldwin 		}
7232c42a146SMarcel Moolenaar 
724df8bae1dSRodney W. Grimes 		/*
725df8bae1dSRodney W. Grimes 		 * Change setting atomically.
726df8bae1dSRodney W. Grimes 		 */
7272c42a146SMarcel Moolenaar 
7282c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
7292c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
730350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_SIGINFO)) {
731aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
732aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
73380f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
73480f42b55SIan Dowse 		} else {
73580f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
7362c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
7372c42a146SMarcel Moolenaar 		}
738350ae563SKonstantin Belousov 		if (!sigact_flag_test(act, SA_RESTART))
7392c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
740df8bae1dSRodney W. Grimes 		else
7412c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
742350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_ONSTACK))
7432c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
744df8bae1dSRodney W. Grimes 		else
7452c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
746350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_RESETHAND))
7472c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
7481e41c1b5SSteven Wallace 		else
7492c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
750350ae563SKonstantin Belousov 		if (sigact_flag_test(act, SA_NODEFER))
7512c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
752289ccde0SPeter Wemm 		else
7532c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
7542c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
7552c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
75690af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
7576626c604SJulian Elischer 			else
75890af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
759ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
760245f17d4SJoerg Wunsch 				/*
7612c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
7622c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
7632c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
7642c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
765245f17d4SJoerg Wunsch 				 */
766245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
76790af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
7686626c604SJulian Elischer 				else
76990af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
770245f17d4SJoerg Wunsch 			} else
77190af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
772ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
77390af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
774ba1551caSIan Dowse 			else
77590af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
776df8bae1dSRodney W. Grimes 		}
777df8bae1dSRodney W. Grimes 		/*
77890af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
7792c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
78090af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
7812c42a146SMarcel Moolenaar 		 * have to restart the process.
782df8bae1dSRodney W. Grimes 		 */
7832c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
784fd50a707SBrooks Davis 		    (sigprop(sig) & SIGPROP_IGNORE &&
7852c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
7862c42a146SMarcel Moolenaar 			/* never to be seen again */
7879104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
7882c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
7892c42a146SMarcel Moolenaar 				/* easier in psignal */
79090af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
79190af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
792645682fdSLuoqi Chen 		} else {
79390af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
7942c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
79590af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
7962c42a146SMarcel Moolenaar 			else
79790af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
7982c42a146SMarcel Moolenaar 		}
79923eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
80023eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
80123eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
80223eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
80323eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
80423eeeff7SPeter Wemm 		else
80523eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
80623eeeff7SPeter Wemm #endif
807e8ebc08fSPeter Wemm #ifdef COMPAT_43
808645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
80923eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
81023eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
811645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
812645682fdSLuoqi Chen 		else
813645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
814e8ebc08fSPeter Wemm #endif
815df8bae1dSRodney W. Grimes 	}
81690af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
817628d2653SJohn Baldwin 	PROC_UNLOCK(p);
8182c42a146SMarcel Moolenaar 	return (0);
8192c42a146SMarcel Moolenaar }
8202c42a146SMarcel Moolenaar 
8212c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8222c42a146SMarcel Moolenaar struct sigaction_args {
8232c42a146SMarcel Moolenaar 	int	sig;
8242c42a146SMarcel Moolenaar 	struct	sigaction *act;
8252c42a146SMarcel Moolenaar 	struct	sigaction *oact;
8262c42a146SMarcel Moolenaar };
8272c42a146SMarcel Moolenaar #endif
8282c42a146SMarcel Moolenaar int
829e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap)
8302c42a146SMarcel Moolenaar {
8312c42a146SMarcel Moolenaar 	struct sigaction act, oact;
832e052a8b9SEd Maste 	struct sigaction *actp, *oactp;
8332c42a146SMarcel Moolenaar 	int error;
8342c42a146SMarcel Moolenaar 
8356f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
8366f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
8372c42a146SMarcel Moolenaar 	if (actp) {
8386f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
8392c42a146SMarcel Moolenaar 		if (error)
84044443757STim J. Robbins 			return (error);
8412c42a146SMarcel Moolenaar 	}
8428f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
84325d6dc06SJohn Baldwin 	if (oactp && !error)
8446f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
8452c42a146SMarcel Moolenaar 	return (error);
8462c42a146SMarcel Moolenaar }
8472c42a146SMarcel Moolenaar 
84823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
84923eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
85023eeeff7SPeter Wemm struct freebsd4_sigaction_args {
85123eeeff7SPeter Wemm 	int	sig;
85223eeeff7SPeter Wemm 	struct	sigaction *act;
85323eeeff7SPeter Wemm 	struct	sigaction *oact;
85423eeeff7SPeter Wemm };
85523eeeff7SPeter Wemm #endif
85623eeeff7SPeter Wemm int
857e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
85823eeeff7SPeter Wemm {
85923eeeff7SPeter Wemm 	struct sigaction act, oact;
860e052a8b9SEd Maste 	struct sigaction *actp, *oactp;
86123eeeff7SPeter Wemm 	int error;
86223eeeff7SPeter Wemm 
86323eeeff7SPeter Wemm 
86423eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
86523eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
86623eeeff7SPeter Wemm 	if (actp) {
86723eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
86823eeeff7SPeter Wemm 		if (error)
86944443757STim J. Robbins 			return (error);
87023eeeff7SPeter Wemm 	}
87123eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
872a14e1189SJohn Baldwin 	if (oactp && !error)
87323eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
87423eeeff7SPeter Wemm 	return (error);
87523eeeff7SPeter Wemm }
87623eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
87723eeeff7SPeter Wemm 
87831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8792c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8802c42a146SMarcel Moolenaar struct osigaction_args {
8812c42a146SMarcel Moolenaar 	int	signum;
8822c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
8832c42a146SMarcel Moolenaar 	struct	osigaction *osa;
8842c42a146SMarcel Moolenaar };
8852c42a146SMarcel Moolenaar #endif
8862c42a146SMarcel Moolenaar int
887e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap)
8882c42a146SMarcel Moolenaar {
8892c42a146SMarcel Moolenaar 	struct osigaction sa;
8902c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
891e052a8b9SEd Maste 	struct sigaction *nsap, *osap;
8922c42a146SMarcel Moolenaar 	int error;
8932c42a146SMarcel Moolenaar 
8946f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
8956f841fb7SMarcel Moolenaar 		return (EINVAL);
896fb99ab88SMatthew Dillon 
8976f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
8986f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
899fb99ab88SMatthew Dillon 
9002c42a146SMarcel Moolenaar 	if (nsap) {
9016f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
9022c42a146SMarcel Moolenaar 		if (error)
90344443757STim J. Robbins 			return (error);
9042c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
9052c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
9062c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
9072c42a146SMarcel Moolenaar 	}
90823eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
9092c42a146SMarcel Moolenaar 	if (osap && !error) {
9102c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
9112c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
9122c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
9136f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
9142c42a146SMarcel Moolenaar 	}
9152c42a146SMarcel Moolenaar 	return (error);
9162c42a146SMarcel Moolenaar }
91723eeeff7SPeter Wemm 
91873dbd3daSJohn Baldwin #if !defined(__i386__)
91923eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
92023eeeff7SPeter Wemm int
921e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap)
92223eeeff7SPeter Wemm {
92323eeeff7SPeter Wemm 
92423eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
92523eeeff7SPeter Wemm }
92623eeeff7SPeter Wemm #endif
92731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes /*
930df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
931df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
932df8bae1dSRodney W. Grimes  */
933df8bae1dSRodney W. Grimes void
934e052a8b9SEd Maste siginit(struct proc *p)
935df8bae1dSRodney W. Grimes {
9363e85b721SEd Maste 	int i;
93790af4afaSJohn Baldwin 	struct sigacts *ps;
938df8bae1dSRodney W. Grimes 
939628d2653SJohn Baldwin 	PROC_LOCK(p);
94090af4afaSJohn Baldwin 	ps = p->p_sigacts;
94190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
942350ae563SKonstantin Belousov 	for (i = 1; i <= NSIG; i++) {
943fd50a707SBrooks Davis 		if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
94490af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
945350ae563SKonstantin Belousov 		}
946350ae563SKonstantin Belousov 	}
94790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
948628d2653SJohn Baldwin 	PROC_UNLOCK(p);
949df8bae1dSRodney W. Grimes }
950df8bae1dSRodney W. Grimes 
951df8bae1dSRodney W. Grimes /*
952350ae563SKonstantin Belousov  * Reset specified signal to the default disposition.
953350ae563SKonstantin Belousov  */
954350ae563SKonstantin Belousov static void
955350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig)
956350ae563SKonstantin Belousov {
957350ae563SKonstantin Belousov 
958350ae563SKonstantin Belousov 	mtx_assert(&ps->ps_mtx, MA_OWNED);
959350ae563SKonstantin Belousov 	SIGDELSET(ps->ps_sigcatch, sig);
960fd50a707SBrooks Davis 	if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
961350ae563SKonstantin Belousov 		SIGADDSET(ps->ps_sigignore, sig);
962350ae563SKonstantin Belousov 	ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
963350ae563SKonstantin Belousov 	SIGDELSET(ps->ps_siginfo, sig);
964350ae563SKonstantin Belousov }
965350ae563SKonstantin Belousov 
966350ae563SKonstantin Belousov /*
967df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
968df8bae1dSRodney W. Grimes  */
969df8bae1dSRodney W. Grimes void
970a30ec4b9SDavid Xu execsigs(struct proc *p)
971df8bae1dSRodney W. Grimes {
972f3fe76ecSEd Schouten 	sigset_t osigignore;
973a30ec4b9SDavid Xu 	struct sigacts *ps;
974a30ec4b9SDavid Xu 	int sig;
975a30ec4b9SDavid Xu 	struct thread *td;
976df8bae1dSRodney W. Grimes 
977df8bae1dSRodney W. Grimes 	/*
978df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
9794093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
980df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
981df8bae1dSRodney W. Grimes 	 */
9829b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
983628d2653SJohn Baldwin 	ps = p->p_sigacts;
98490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
98590af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
98690af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
987350ae563SKonstantin Belousov 		sigdflt(ps, sig);
988fd50a707SBrooks Davis 		if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
9899104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
990df8bae1dSRodney W. Grimes 	}
991f3fe76ecSEd Schouten 
992f3fe76ecSEd Schouten 	/*
993f3fe76ecSEd Schouten 	 * As CloudABI processes cannot modify signal handlers, fully
994f3fe76ecSEd Schouten 	 * reset all signals to their default behavior. Do ignore
995f3fe76ecSEd Schouten 	 * SIGPIPE, as it would otherwise be impossible to recover from
996f3fe76ecSEd Schouten 	 * writes to broken pipes and sockets.
997f3fe76ecSEd Schouten 	 */
998f3fe76ecSEd Schouten 	if (SV_PROC_ABI(p) == SV_ABI_CLOUDABI) {
999f3fe76ecSEd Schouten 		osigignore = ps->ps_sigignore;
1000f3fe76ecSEd Schouten 		while (SIGNOTEMPTY(osigignore)) {
1001f3fe76ecSEd Schouten 			sig = sig_ffs(&osigignore);
1002f3fe76ecSEd Schouten 			SIGDELSET(osigignore, sig);
1003f3fe76ecSEd Schouten 			if (sig != SIGPIPE)
1004f3fe76ecSEd Schouten 				sigdflt(ps, sig);
1005f3fe76ecSEd Schouten 		}
1006f3fe76ecSEd Schouten 		SIGADDSET(ps->ps_sigignore, SIGPIPE);
1007f3fe76ecSEd Schouten 	}
1008f3fe76ecSEd Schouten 
1009df8bae1dSRodney W. Grimes 	/*
1010df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
1011df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
1012df8bae1dSRodney W. Grimes 	 */
1013469ec1ebSKonstantin Belousov 	td = curthread;
1014469ec1ebSKonstantin Belousov 	MPASS(td->td_proc == p);
1015a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
1016a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
1017a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
1018a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
101980e907a1SPeter Wemm 	/*
102080e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
102180e907a1SPeter Wemm 	 */
102290af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1023c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1024c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
102590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
1026df8bae1dSRodney W. Grimes }
1027df8bae1dSRodney W. Grimes 
1028df8bae1dSRodney W. Grimes /*
1029e77daab1SJohn Baldwin  * kern_sigprocmask()
10307c8fdcbdSMatthew Dillon  *
1031628d2653SJohn Baldwin  *	Manipulate signal mask.
1032df8bae1dSRodney W. Grimes  */
1033e77daab1SJohn Baldwin int
10346b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
103584440afbSKonstantin Belousov     int flags)
10362c42a146SMarcel Moolenaar {
10376b286ee8SKonstantin Belousov 	sigset_t new_block, oset1;
10386b286ee8SKonstantin Belousov 	struct proc *p;
10392c42a146SMarcel Moolenaar 	int error;
10402c42a146SMarcel Moolenaar 
10416b286ee8SKonstantin Belousov 	p = td->td_proc;
104270778bbaSKonstantin Belousov 	if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
104370778bbaSKonstantin Belousov 		PROC_LOCK_ASSERT(p, MA_OWNED);
104470778bbaSKonstantin Belousov 	else
10456b286ee8SKonstantin Belousov 		PROC_LOCK(p);
104670778bbaSKonstantin Belousov 	mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
104770778bbaSKonstantin Belousov 	    ? MA_OWNED : MA_NOTOWNED);
10482c42a146SMarcel Moolenaar 	if (oset != NULL)
10494093529dSJeff Roberson 		*oset = td->td_sigmask;
10502c42a146SMarcel Moolenaar 
10512c42a146SMarcel Moolenaar 	error = 0;
10522c42a146SMarcel Moolenaar 	if (set != NULL) {
10532c42a146SMarcel Moolenaar 		switch (how) {
10542c42a146SMarcel Moolenaar 		case SIG_BLOCK:
1055645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
10566b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
10574093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
10586b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
10596b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
10602c42a146SMarcel Moolenaar 			break;
10612c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
10624093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
10634093529dSJeff Roberson 			signotify(td);
1064407af02bSDavid Xu 			goto out;
10652c42a146SMarcel Moolenaar 		case SIG_SETMASK:
1066645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
10676b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
106884440afbSKonstantin Belousov 			if (flags & SIGPROCMASK_OLD)
10694093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
1070645682fdSLuoqi Chen 			else
10714093529dSJeff Roberson 				td->td_sigmask = *set;
10726b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
10736b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
10744093529dSJeff Roberson 			signotify(td);
10752c42a146SMarcel Moolenaar 			break;
10762c42a146SMarcel Moolenaar 		default:
10772c42a146SMarcel Moolenaar 			error = EINVAL;
1078407af02bSDavid Xu 			goto out;
10792c42a146SMarcel Moolenaar 		}
10806b286ee8SKonstantin Belousov 
10816b286ee8SKonstantin Belousov 		/*
10827df8f6abSKonstantin Belousov 		 * The new_block set contains signals that were not previously
10836b286ee8SKonstantin Belousov 		 * blocked, but are blocked now.
10846b286ee8SKonstantin Belousov 		 *
10856b286ee8SKonstantin Belousov 		 * In case we block any signal that was not previously blocked
10866b286ee8SKonstantin Belousov 		 * for td, and process has the signal pending, try to schedule
1087407af02bSDavid Xu 		 * signal delivery to some thread that does not block the
1088407af02bSDavid Xu 		 * signal, possibly waking it up.
10896b286ee8SKonstantin Belousov 		 */
10906b286ee8SKonstantin Belousov 		if (p->p_numthreads != 1)
109180a8b0f3SKonstantin Belousov 			reschedule_signals(p, new_block, flags);
1092407af02bSDavid Xu 	}
10936b286ee8SKonstantin Belousov 
1094407af02bSDavid Xu out:
109584440afbSKonstantin Belousov 	if (!(flags & SIGPROCMASK_PROC_LOCKED))
10966b286ee8SKonstantin Belousov 		PROC_UNLOCK(p);
10972c42a146SMarcel Moolenaar 	return (error);
10982c42a146SMarcel Moolenaar }
10992c42a146SMarcel Moolenaar 
1100d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1101df8bae1dSRodney W. Grimes struct sigprocmask_args {
1102df8bae1dSRodney W. Grimes 	int	how;
11032c42a146SMarcel Moolenaar 	const sigset_t *set;
11042c42a146SMarcel Moolenaar 	sigset_t *oset;
1105df8bae1dSRodney W. Grimes };
1106d2d3e875SBruce Evans #endif
110726f9a767SRodney W. Grimes int
1108e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1109df8bae1dSRodney W. Grimes {
11102c42a146SMarcel Moolenaar 	sigset_t set, oset;
11112c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
11122c42a146SMarcel Moolenaar 	int error;
1113df8bae1dSRodney W. Grimes 
11146f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
11156f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
11162c42a146SMarcel Moolenaar 	if (setp) {
11176f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
11182c42a146SMarcel Moolenaar 		if (error)
11192c42a146SMarcel Moolenaar 			return (error);
1120df8bae1dSRodney W. Grimes 	}
1121e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
11222c42a146SMarcel Moolenaar 	if (osetp && !error) {
11236f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
11242c42a146SMarcel Moolenaar 	}
11252c42a146SMarcel Moolenaar 	return (error);
11262c42a146SMarcel Moolenaar }
11272c42a146SMarcel Moolenaar 
112831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
11292c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
11302c42a146SMarcel Moolenaar struct osigprocmask_args {
11312c42a146SMarcel Moolenaar 	int	how;
11322c42a146SMarcel Moolenaar 	osigset_t mask;
11332c42a146SMarcel Moolenaar };
11342c42a146SMarcel Moolenaar #endif
11352c42a146SMarcel Moolenaar int
1136e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap)
11372c42a146SMarcel Moolenaar {
11382c42a146SMarcel Moolenaar 	sigset_t set, oset;
11392c42a146SMarcel Moolenaar 	int error;
11402c42a146SMarcel Moolenaar 
11412c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1142e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1143b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
1144df8bae1dSRodney W. Grimes 	return (error);
1145df8bae1dSRodney W. Grimes }
114631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1147df8bae1dSRodney W. Grimes 
114826f9a767SRodney W. Grimes int
11498451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap)
1150a447cd8bSJeff Roberson {
11519104847fSDavid Xu 	ksiginfo_t ksi;
1152a447cd8bSJeff Roberson 	sigset_t set;
1153a447cd8bSJeff Roberson 	int error;
1154a447cd8bSJeff Roberson 
1155a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
115636939a0aSDavid Xu 	if (error) {
115736939a0aSDavid Xu 		td->td_retval[0] = error;
115836939a0aSDavid Xu 		return (0);
115936939a0aSDavid Xu 	}
1160a447cd8bSJeff Roberson 
11619104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
116236939a0aSDavid Xu 	if (error) {
11638e9a54eeSKonstantin Belousov 		if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
11648e9a54eeSKonstantin Belousov 			error = ERESTART;
116536939a0aSDavid Xu 		if (error == ERESTART)
1166a447cd8bSJeff Roberson 			return (error);
116736939a0aSDavid Xu 		td->td_retval[0] = error;
116836939a0aSDavid Xu 		return (0);
116936939a0aSDavid Xu 	}
1170a447cd8bSJeff Roberson 
11719104847fSDavid Xu 	error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
117236939a0aSDavid Xu 	td->td_retval[0] = error;
117336939a0aSDavid Xu 	return (0);
1174a447cd8bSJeff Roberson }
11750c14ff0eSRobert Watson 
1176a447cd8bSJeff Roberson int
11778451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1178a447cd8bSJeff Roberson {
1179a447cd8bSJeff Roberson 	struct timespec ts;
1180a447cd8bSJeff Roberson 	struct timespec *timeout;
1181a447cd8bSJeff Roberson 	sigset_t set;
11829104847fSDavid Xu 	ksiginfo_t ksi;
1183a447cd8bSJeff Roberson 	int error;
1184a447cd8bSJeff Roberson 
1185a447cd8bSJeff Roberson 	if (uap->timeout) {
1186a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
1187a447cd8bSJeff Roberson 		if (error)
1188a447cd8bSJeff Roberson 			return (error);
1189a447cd8bSJeff Roberson 
1190a447cd8bSJeff Roberson 		timeout = &ts;
1191a447cd8bSJeff Roberson 	} else
1192a447cd8bSJeff Roberson 		timeout = NULL;
1193a447cd8bSJeff Roberson 
1194a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1195a447cd8bSJeff Roberson 	if (error)
1196a447cd8bSJeff Roberson 		return (error);
1197a447cd8bSJeff Roberson 
11989104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, timeout);
1199a447cd8bSJeff Roberson 	if (error)
1200a447cd8bSJeff Roberson 		return (error);
12019dde3bc9SDavid Xu 
1202418228dfSDavid Xu 	if (uap->info)
12039104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
12049104847fSDavid Xu 
12059104847fSDavid Xu 	if (error == 0)
12069104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1207a447cd8bSJeff Roberson 	return (error);
1208a447cd8bSJeff Roberson }
1209a447cd8bSJeff Roberson 
1210a447cd8bSJeff Roberson int
12118451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1212a447cd8bSJeff Roberson {
12139104847fSDavid Xu 	ksiginfo_t ksi;
1214a447cd8bSJeff Roberson 	sigset_t set;
1215a447cd8bSJeff Roberson 	int error;
1216a447cd8bSJeff Roberson 
1217a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1218a447cd8bSJeff Roberson 	if (error)
1219a447cd8bSJeff Roberson 		return (error);
1220a447cd8bSJeff Roberson 
12219104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
1222a447cd8bSJeff Roberson 	if (error)
1223a447cd8bSJeff Roberson 		return (error);
1224a447cd8bSJeff Roberson 
1225418228dfSDavid Xu 	if (uap->info)
12269104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
12279104847fSDavid Xu 
12289104847fSDavid Xu 	if (error == 0)
12299104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1230a447cd8bSJeff Roberson 	return (error);
1231a447cd8bSJeff Roberson }
1232a447cd8bSJeff Roberson 
123386be94fcSTycho Nightingale static void
123486be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
123586be94fcSTycho Nightingale {
123686be94fcSTycho Nightingale 	struct thread *thr;
123786be94fcSTycho Nightingale 
123886be94fcSTycho Nightingale 	FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
123986be94fcSTycho Nightingale 		if (thr == td)
124086be94fcSTycho Nightingale 			thr->td_si = *si;
124186be94fcSTycho Nightingale 		else
124286be94fcSTycho Nightingale 			thr->td_si.si_signo = 0;
124386be94fcSTycho Nightingale 	}
124486be94fcSTycho Nightingale }
124586be94fcSTycho Nightingale 
1246c6511aeaSDavid Xu int
12479104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1248a447cd8bSJeff Roberson 	struct timespec *timeout)
1249a447cd8bSJeff Roberson {
12503074d1b4SDavid Xu 	struct sigacts *ps;
1251407af02bSDavid Xu 	sigset_t saved_mask, new_block;
1252a447cd8bSJeff Roberson 	struct proc *p;
1253407af02bSDavid Xu 	int error, sig, timo, timevalid = 0;
12541089f031SDavid Xu 	struct timespec rts, ets, ts;
12551089f031SDavid Xu 	struct timeval tv;
1256a447cd8bSJeff Roberson 
1257a447cd8bSJeff Roberson 	p = td->td_proc;
1258a447cd8bSJeff Roberson 	error = 0;
12599b73d239SMatt Jacob 	ets.tv_sec = 0;
12609b73d239SMatt Jacob 	ets.tv_nsec = 0;
1261a447cd8bSJeff Roberson 
1262407af02bSDavid Xu 	if (timeout != NULL) {
12631089f031SDavid Xu 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
12641089f031SDavid Xu 			timevalid = 1;
12651089f031SDavid Xu 			getnanouptime(&rts);
12661089f031SDavid Xu 			ets = rts;
12671089f031SDavid Xu 			timespecadd(&ets, timeout);
12681089f031SDavid Xu 		}
12691089f031SDavid Xu 	}
1270407af02bSDavid Xu 	ksiginfo_init(ksi);
1271407af02bSDavid Xu 	/* Some signals can not be waited for. */
1272407af02bSDavid Xu 	SIG_CANTMASK(waitset);
1273407af02bSDavid Xu 	ps = p->p_sigacts;
1274407af02bSDavid Xu 	PROC_LOCK(p);
1275407af02bSDavid Xu 	saved_mask = td->td_sigmask;
1276407af02bSDavid Xu 	SIGSETNAND(td->td_sigmask, waitset);
1277407af02bSDavid Xu 	for (;;) {
12783074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
12793cf3b9f0SJohn Baldwin 		sig = cursig(td);
12803074d1b4SDavid Xu 		mtx_unlock(&ps->ps_mtx);
128146e47c4fSKonstantin Belousov 		KASSERT(sig >= 0, ("sig %d", sig));
1282407af02bSDavid Xu 		if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1283407af02bSDavid Xu 			if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1284407af02bSDavid Xu 			    sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1285407af02bSDavid Xu 				error = 0;
1286407af02bSDavid Xu 				break;
12873074d1b4SDavid Xu 			}
12887e0221a2SDavid Xu 		}
12897e0221a2SDavid Xu 
1290407af02bSDavid Xu 		if (error != 0)
1291407af02bSDavid Xu 			break;
12923074d1b4SDavid Xu 
1293a447cd8bSJeff Roberson 		/*
1294a447cd8bSJeff Roberson 		 * POSIX says this must be checked after looking for pending
1295a447cd8bSJeff Roberson 		 * signals.
1296a447cd8bSJeff Roberson 		 */
1297407af02bSDavid Xu 		if (timeout != NULL) {
12981089f031SDavid Xu 			if (!timevalid) {
1299a447cd8bSJeff Roberson 				error = EINVAL;
1300407af02bSDavid Xu 				break;
1301a447cd8bSJeff Roberson 			}
13021089f031SDavid Xu 			getnanouptime(&rts);
13031089f031SDavid Xu 			if (timespeccmp(&rts, &ets, >=)) {
13043074d1b4SDavid Xu 				error = EAGAIN;
1305407af02bSDavid Xu 				break;
13063074d1b4SDavid Xu 			}
13071089f031SDavid Xu 			ts = ets;
13081089f031SDavid Xu 			timespecsub(&ts, &rts);
13091089f031SDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
1310407af02bSDavid Xu 			timo = tvtohz(&tv);
1311407af02bSDavid Xu 		} else {
1312407af02bSDavid Xu 			timo = 0;
1313407af02bSDavid Xu 		}
1314a447cd8bSJeff Roberson 
1315407af02bSDavid Xu 		error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1316407af02bSDavid Xu 
1317407af02bSDavid Xu 		if (timeout != NULL) {
13181089f031SDavid Xu 			if (error == ERESTART) {
1319407af02bSDavid Xu 				/* Timeout can not be restarted. */
13203074d1b4SDavid Xu 				error = EINTR;
13211089f031SDavid Xu 			} else if (error == EAGAIN) {
1322407af02bSDavid Xu 				/* We will calculate timeout by ourself. */
13231089f031SDavid Xu 				error = 0;
13241089f031SDavid Xu 			}
13251089f031SDavid Xu 		}
1326407af02bSDavid Xu 	}
13279dde3bc9SDavid Xu 
1328407af02bSDavid Xu 	new_block = saved_mask;
1329407af02bSDavid Xu 	SIGSETNAND(new_block, td->td_sigmask);
1330407af02bSDavid Xu 	td->td_sigmask = saved_mask;
1331407af02bSDavid Xu 	/*
1332407af02bSDavid Xu 	 * Fewer signals can be delivered to us, reschedule signal
1333407af02bSDavid Xu 	 * notification.
1334407af02bSDavid Xu 	 */
1335407af02bSDavid Xu 	if (p->p_numthreads != 1)
1336407af02bSDavid Xu 		reschedule_signals(p, new_block, 0);
13375d217f17SJohn Birrell 
1338407af02bSDavid Xu 	if (error == 0) {
133936160958SMark Johnston 		SDT_PROBE2(proc, , , signal__clear, sig, ksi);
13405d217f17SJohn Birrell 
134156c06c4bSDavid Xu 		if (ksi->ksi_code == SI_TIMER)
134256c06c4bSDavid Xu 			itimer_accept(p, ksi->ksi_timerid, ksi);
13437e0221a2SDavid Xu 
13447e0221a2SDavid Xu #ifdef KTRACE
13457e0221a2SDavid Xu 		if (KTRPOINT(td, KTR_PSIG)) {
13467e0221a2SDavid Xu 			sig_t action;
13477e0221a2SDavid Xu 
13483074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
1349a447cd8bSJeff Roberson 			action = ps->ps_sigact[_SIG_IDX(sig)];
135090af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
135161009552SJilles Tjoelker 			ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
13527e0221a2SDavid Xu 		}
1353a447cd8bSJeff Roberson #endif
135486be94fcSTycho Nightingale 		if (sig == SIGKILL) {
135586be94fcSTycho Nightingale 			proc_td_siginfo_capture(td, &ksi->ksi_info);
13567e0221a2SDavid Xu 			sigexit(td, sig);
13573074d1b4SDavid Xu 		}
135886be94fcSTycho Nightingale 	}
1359a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
1360a447cd8bSJeff Roberson 	return (error);
1361a447cd8bSJeff Roberson }
1362a447cd8bSJeff Roberson 
13639104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
13649104847fSDavid Xu struct sigpending_args {
13659104847fSDavid Xu 	sigset_t	*set;
13669104847fSDavid Xu };
13679104847fSDavid Xu #endif
1368a447cd8bSJeff Roberson int
1369e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap)
1370df8bae1dSRodney W. Grimes {
1371b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13729104847fSDavid Xu 	sigset_t pending;
1373df8bae1dSRodney W. Grimes 
1374628d2653SJohn Baldwin 	PROC_LOCK(p);
13759104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
13769104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1377628d2653SJohn Baldwin 	PROC_UNLOCK(p);
13789104847fSDavid Xu 	return (copyout(&pending, uap->set, sizeof(sigset_t)));
13792c42a146SMarcel Moolenaar }
13802c42a146SMarcel Moolenaar 
138131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
13822c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
13832c42a146SMarcel Moolenaar struct osigpending_args {
13842c42a146SMarcel Moolenaar 	int	dummy;
13852c42a146SMarcel Moolenaar };
13862c42a146SMarcel Moolenaar #endif
13872c42a146SMarcel Moolenaar int
1388e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap)
13892c42a146SMarcel Moolenaar {
1390b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13919104847fSDavid Xu 	sigset_t pending;
1392b40ce416SJulian Elischer 
1393628d2653SJohn Baldwin 	PROC_LOCK(p);
13949104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
13959104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1396628d2653SJohn Baldwin 	PROC_UNLOCK(p);
13979104847fSDavid Xu 	SIG2OSIG(pending, td->td_retval[0]);
1398df8bae1dSRodney W. Grimes 	return (0);
1399df8bae1dSRodney W. Grimes }
140031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1401df8bae1dSRodney W. Grimes 
14021930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1403df8bae1dSRodney W. Grimes /*
1404df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1405df8bae1dSRodney W. Grimes  */
1406d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1407df8bae1dSRodney W. Grimes struct osigvec_args {
1408df8bae1dSRodney W. Grimes 	int	signum;
1409df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1410df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1411df8bae1dSRodney W. Grimes };
1412d2d3e875SBruce Evans #endif
1413df8bae1dSRodney W. Grimes /* ARGSUSED */
141426f9a767SRodney W. Grimes int
1415e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap)
1416df8bae1dSRodney W. Grimes {
1417df8bae1dSRodney W. Grimes 	struct sigvec vec;
14182c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
1419e052a8b9SEd Maste 	struct sigaction *nsap, *osap;
14202c42a146SMarcel Moolenaar 	int error;
1421df8bae1dSRodney W. Grimes 
14226f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
14236f841fb7SMarcel Moolenaar 		return (EINVAL);
14246f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
14256f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
14262c42a146SMarcel Moolenaar 	if (nsap) {
14276f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
14282c42a146SMarcel Moolenaar 		if (error)
1429df8bae1dSRodney W. Grimes 			return (error);
14302c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
14312c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
14322c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
14332c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1434df8bae1dSRodney W. Grimes 	}
14355edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
14362c42a146SMarcel Moolenaar 	if (osap && !error) {
14372c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
14382c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
14392c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
14402c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
14412c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
14426f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
14432c42a146SMarcel Moolenaar 	}
14442c42a146SMarcel Moolenaar 	return (error);
1445df8bae1dSRodney W. Grimes }
1446df8bae1dSRodney W. Grimes 
1447d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1448df8bae1dSRodney W. Grimes struct osigblock_args {
1449df8bae1dSRodney W. Grimes 	int	mask;
1450df8bae1dSRodney W. Grimes };
1451d2d3e875SBruce Evans #endif
145226f9a767SRodney W. Grimes int
1453e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap)
1454df8bae1dSRodney W. Grimes {
1455d6e029adSKonstantin Belousov 	sigset_t set, oset;
1456df8bae1dSRodney W. Grimes 
14572c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1458d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1459d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1460df8bae1dSRodney W. Grimes 	return (0);
1461df8bae1dSRodney W. Grimes }
1462df8bae1dSRodney W. Grimes 
1463d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1464df8bae1dSRodney W. Grimes struct osigsetmask_args {
1465df8bae1dSRodney W. Grimes 	int	mask;
1466df8bae1dSRodney W. Grimes };
1467d2d3e875SBruce Evans #endif
146826f9a767SRodney W. Grimes int
1469e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1470df8bae1dSRodney W. Grimes {
1471d6e029adSKonstantin Belousov 	sigset_t set, oset;
1472df8bae1dSRodney W. Grimes 
14732c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1474d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1475d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1476df8bae1dSRodney W. Grimes 	return (0);
1477df8bae1dSRodney W. Grimes }
14781930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1479df8bae1dSRodney W. Grimes 
1480df8bae1dSRodney W. Grimes /*
1481873fbcd7SRobert Watson  * Suspend calling thread until signal, providing mask to be set in the
1482873fbcd7SRobert Watson  * meantime.
1483df8bae1dSRodney W. Grimes  */
1484d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1485df8bae1dSRodney W. Grimes struct sigsuspend_args {
14862c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1487df8bae1dSRodney W. Grimes };
1488d2d3e875SBruce Evans #endif
1489df8bae1dSRodney W. Grimes /* ARGSUSED */
149026f9a767SRodney W. Grimes int
1491e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1492df8bae1dSRodney W. Grimes {
14932c42a146SMarcel Moolenaar 	sigset_t mask;
14942c42a146SMarcel Moolenaar 	int error;
14952c42a146SMarcel Moolenaar 
14966f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
14972c42a146SMarcel Moolenaar 	if (error)
14982c42a146SMarcel Moolenaar 		return (error);
14998f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
15008f19eb88SIan Dowse }
15018f19eb88SIan Dowse 
15028f19eb88SIan Dowse int
15038f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
15048f19eb88SIan Dowse {
15058f19eb88SIan Dowse 	struct proc *p = td->td_proc;
150684440afbSKonstantin Belousov 	int has_sig, sig;
1507df8bae1dSRodney W. Grimes 
1508df8bae1dSRodney W. Grimes 	/*
1509645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1510df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1511df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1512df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1513df8bae1dSRodney W. Grimes 	 * to indicate this.
1514df8bae1dSRodney W. Grimes 	 */
1515628d2653SJohn Baldwin 	PROC_LOCK(p);
151684440afbSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
151784440afbSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED);
15185e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
151984440afbSKonstantin Belousov 
152084440afbSKonstantin Belousov 	/*
152184440afbSKonstantin Belousov 	 * Process signals now. Otherwise, we can get spurious wakeup
152284440afbSKonstantin Belousov 	 * due to signal entered process queue, but delivered to other
152384440afbSKonstantin Belousov 	 * thread. But sigsuspend should return only on signal
152484440afbSKonstantin Belousov 	 * delivery.
152584440afbSKonstantin Belousov 	 */
1526afe1a688SKonstantin Belousov 	(p->p_sysent->sv_set_syscall_retval)(td, EINTR);
152784440afbSKonstantin Belousov 	for (has_sig = 0; !has_sig;) {
152884440afbSKonstantin Belousov 		while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
152984440afbSKonstantin Belousov 			0) == 0)
15302c42a146SMarcel Moolenaar 			/* void */;
153184440afbSKonstantin Belousov 		thread_suspend_check(0);
153284440afbSKonstantin Belousov 		mtx_lock(&p->p_sigacts->ps_mtx);
153346e47c4fSKonstantin Belousov 		while ((sig = cursig(td)) != 0) {
153446e47c4fSKonstantin Belousov 			KASSERT(sig >= 0, ("sig %d", sig));
153575c586a4SKonstantin Belousov 			has_sig += postsig(sig);
153646e47c4fSKonstantin Belousov 		}
153784440afbSKonstantin Belousov 		mtx_unlock(&p->p_sigacts->ps_mtx);
153884440afbSKonstantin Belousov 	}
1539628d2653SJohn Baldwin 	PROC_UNLOCK(p);
15402dd9ea6fSKonstantin Belousov 	td->td_errno = EINTR;
15412dd9ea6fSKonstantin Belousov 	td->td_pflags |= TDP_NERRNO;
154275c586a4SKonstantin Belousov 	return (EJUSTRETURN);
15432c42a146SMarcel Moolenaar }
15442c42a146SMarcel Moolenaar 
154531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
154697563428SAlexander Kabaev /*
154797563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
154897563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
154997563428SAlexander Kabaev  */
15502c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
15512c42a146SMarcel Moolenaar struct osigsuspend_args {
15522c42a146SMarcel Moolenaar 	osigset_t mask;
15532c42a146SMarcel Moolenaar };
15542c42a146SMarcel Moolenaar #endif
15552c42a146SMarcel Moolenaar /* ARGSUSED */
15562c42a146SMarcel Moolenaar int
1557e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap)
15582c42a146SMarcel Moolenaar {
1559645682fdSLuoqi Chen 	sigset_t mask;
15602c42a146SMarcel Moolenaar 
1561645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
156284440afbSKonstantin Belousov 	return (kern_sigsuspend(td, mask));
1563df8bae1dSRodney W. Grimes }
156431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1565df8bae1dSRodney W. Grimes 
15661930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1567d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1568df8bae1dSRodney W. Grimes struct osigstack_args {
1569df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1570df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1571df8bae1dSRodney W. Grimes };
1572d2d3e875SBruce Evans #endif
1573df8bae1dSRodney W. Grimes /* ARGSUSED */
157426f9a767SRodney W. Grimes int
1575e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap)
1576df8bae1dSRodney W. Grimes {
15775afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1578fb99ab88SMatthew Dillon 	int error = 0;
1579fb99ab88SMatthew Dillon 
1580d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
15815afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
15825afe0c99SJohn Baldwin 		if (error)
15835afe0c99SJohn Baldwin 			return (error);
1584df8bae1dSRodney W. Grimes 	}
1585a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
15865afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
15875afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1588a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1589a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1590a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1591a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
15925afe0c99SJohn Baldwin 	}
15935afe0c99SJohn Baldwin 	if (uap->oss != NULL)
15945afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
15955afe0c99SJohn Baldwin 
1596fb99ab88SMatthew Dillon 	return (error);
1597df8bae1dSRodney W. Grimes }
15981930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1599df8bae1dSRodney W. Grimes 
1600d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1601df8bae1dSRodney W. Grimes struct sigaltstack_args {
16022c42a146SMarcel Moolenaar 	stack_t	*ss;
16032c42a146SMarcel Moolenaar 	stack_t	*oss;
1604df8bae1dSRodney W. Grimes };
1605d2d3e875SBruce Evans #endif
1606df8bae1dSRodney W. Grimes /* ARGSUSED */
160726f9a767SRodney W. Grimes int
1608e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1609df8bae1dSRodney W. Grimes {
16108f19eb88SIan Dowse 	stack_t ss, oss;
16118f19eb88SIan Dowse 	int error;
16128f19eb88SIan Dowse 
16138f19eb88SIan Dowse 	if (uap->ss != NULL) {
16148f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
16158f19eb88SIan Dowse 		if (error)
16168f19eb88SIan Dowse 			return (error);
16178f19eb88SIan Dowse 	}
16188f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
16198f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
16208f19eb88SIan Dowse 	if (error)
16218f19eb88SIan Dowse 		return (error);
16228f19eb88SIan Dowse 	if (uap->oss != NULL)
16238f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
16248f19eb88SIan Dowse 	return (error);
16258f19eb88SIan Dowse }
16268f19eb88SIan Dowse 
16278f19eb88SIan Dowse int
16288f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
16298f19eb88SIan Dowse {
1630b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1631fb99ab88SMatthew Dillon 	int oonstack;
1632fb99ab88SMatthew Dillon 
1633b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1634d034d459SMarcel Moolenaar 
16358f19eb88SIan Dowse 	if (oss != NULL) {
1636a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1637a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1638d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1639df8bae1dSRodney W. Grimes 	}
1640d034d459SMarcel Moolenaar 
16418f19eb88SIan Dowse 	if (ss != NULL) {
1642a30ec4b9SDavid Xu 		if (oonstack)
1643cf60731bSJohn Baldwin 			return (EPERM);
1644a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1645cf60731bSJohn Baldwin 			return (EINVAL);
16468f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
16479104847fSDavid Xu 			if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1648cf60731bSJohn Baldwin 				return (ENOMEM);
16499104847fSDavid Xu 
1650a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1651a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1652628d2653SJohn Baldwin 		} else {
1653a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1654628d2653SJohn Baldwin 		}
1655d034d459SMarcel Moolenaar 	}
1656cf60731bSJohn Baldwin 	return (0);
1657df8bae1dSRodney W. Grimes }
1658df8bae1dSRodney W. Grimes 
1659d93f860cSPoul-Henning Kamp /*
1660d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1661d93f860cSPoul-Henning Kamp  * cp is calling process.
1662d93f860cSPoul-Henning Kamp  */
166337c84183SPoul-Henning Kamp static int
1664a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1665d93f860cSPoul-Henning Kamp {
1666a3de221dSKonstantin Belousov 	struct proc *p;
1667d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
16683d74f47bSEitan Adler 	int err;
16693d74f47bSEitan Adler 	int ret;
1670d93f860cSPoul-Henning Kamp 
16713d74f47bSEitan Adler 	ret = ESRCH;
1672553629ebSJake Burkholder 	if (all) {
1673d93f860cSPoul-Henning Kamp 		/*
1674d93f860cSPoul-Henning Kamp 		 * broadcast
1675d93f860cSPoul-Henning Kamp 		 */
16761005a129SJohn Baldwin 		sx_slock(&allproc_lock);
16774f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
1678628d2653SJohn Baldwin 			PROC_LOCK(p);
16799c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
168095f16c1eSPaul Saab 			    p == td->td_proc || p->p_state == PRS_NEW) {
1681628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1682628d2653SJohn Baldwin 				continue;
1683628d2653SJohn Baldwin 			}
16843d74f47bSEitan Adler 			err = p_cansignal(td, p, sig);
16853d74f47bSEitan Adler 			if (err == 0) {
168633a9ed9dSJohn Baldwin 				if (sig)
1687a3de221dSKonstantin Belousov 					pksignal(p, sig, ksi);
16883d74f47bSEitan Adler 				ret = err;
1689628d2653SJohn Baldwin 			}
16903d74f47bSEitan Adler 			else if (ret == ESRCH)
16913d74f47bSEitan Adler 				ret = err;
169233a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1693d93f860cSPoul-Henning Kamp 		}
16941005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1695553629ebSJake Burkholder 	} else {
1696ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1697f591779bSSeigo Tanimura 		if (pgid == 0) {
1698d93f860cSPoul-Henning Kamp 			/*
1699d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1700d93f860cSPoul-Henning Kamp 			 */
17019c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1702f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1703f591779bSSeigo Tanimura 		} else {
1704d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1705f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1706ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1707d93f860cSPoul-Henning Kamp 				return (ESRCH);
1708d93f860cSPoul-Henning Kamp 			}
1709f591779bSSeigo Tanimura 		}
1710ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
17112e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1712628d2653SJohn Baldwin 			PROC_LOCK(p);
171395f16c1eSPaul Saab 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
171495f16c1eSPaul Saab 			    p->p_state == PRS_NEW) {
1715628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1716628d2653SJohn Baldwin 				continue;
1717628d2653SJohn Baldwin 			}
17183d74f47bSEitan Adler 			err = p_cansignal(td, p, sig);
17193d74f47bSEitan Adler 			if (err == 0) {
172033a9ed9dSJohn Baldwin 				if (sig)
1721a3de221dSKonstantin Belousov 					pksignal(p, sig, ksi);
17223d74f47bSEitan Adler 				ret = err;
1723628d2653SJohn Baldwin 			}
17243d74f47bSEitan Adler 			else if (ret == ESRCH)
17253d74f47bSEitan Adler 				ret = err;
172633a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1727d93f860cSPoul-Henning Kamp 		}
1728f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1729d93f860cSPoul-Henning Kamp 	}
17303d74f47bSEitan Adler 	return (ret);
1731d93f860cSPoul-Henning Kamp }
1732d93f860cSPoul-Henning Kamp 
1733d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1734df8bae1dSRodney W. Grimes struct kill_args {
1735df8bae1dSRodney W. Grimes 	int	pid;
1736df8bae1dSRodney W. Grimes 	int	signum;
1737df8bae1dSRodney W. Grimes };
1738d2d3e875SBruce Evans #endif
1739df8bae1dSRodney W. Grimes /* ARGSUSED */
174026f9a767SRodney W. Grimes int
17418451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap)
1742df8bae1dSRodney W. Grimes {
1743a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1744a3de221dSKonstantin Belousov 	struct proc *p;
174590af4afaSJohn Baldwin 	int error;
1746df8bae1dSRodney W. Grimes 
17478890f5d0SPawel Jakub Dawidek 	/*
17488890f5d0SPawel Jakub Dawidek 	 * A process in capability mode can send signals only to himself.
17498890f5d0SPawel Jakub Dawidek 	 * The main rationale behind this is that abort(3) is implemented as
17508890f5d0SPawel Jakub Dawidek 	 * kill(getpid(), SIGABRT).
17518890f5d0SPawel Jakub Dawidek 	 */
17528890f5d0SPawel Jakub Dawidek 	if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid)
17538890f5d0SPawel Jakub Dawidek 		return (ECAPMODE);
17548890f5d0SPawel Jakub Dawidek 
175514961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->signum);
175614961ba7SRobert Watson 	AUDIT_ARG_PID(uap->pid);
17576c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1758df8bae1dSRodney W. Grimes 		return (EINVAL);
1759fb99ab88SMatthew Dillon 
1760a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
1761a3de221dSKonstantin Belousov 	ksi.ksi_signo = uap->signum;
1762a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1763a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1764a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1765a3de221dSKonstantin Belousov 
1766df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1767df8bae1dSRodney W. Grimes 		/* kill single process */
17686e1619daSMateusz Guzik 		if ((p = pfind_any(uap->pid)) == NULL)
176990af4afaSJohn Baldwin 			return (ESRCH);
177014961ba7SRobert Watson 		AUDIT_ARG_PROCESS(p);
17711a88a252SMaxim Sobolev 		error = p_cansignal(td, p, uap->signum);
177290af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1773a3de221dSKonstantin Belousov 			pksignal(p, uap->signum, &ksi);
1774628d2653SJohn Baldwin 		PROC_UNLOCK(p);
177590af4afaSJohn Baldwin 		return (error);
1776df8bae1dSRodney W. Grimes 	}
1777df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1778df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
1779a3de221dSKonstantin Belousov 		return (killpg1(td, uap->signum, 0, 1, &ksi));
1780df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
1781a3de221dSKonstantin Belousov 		return (killpg1(td, uap->signum, 0, 0, &ksi));
1782df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
1783a3de221dSKonstantin Belousov 		return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1784df8bae1dSRodney W. Grimes 	}
178590af4afaSJohn Baldwin 	/* NOTREACHED */
1786df8bae1dSRodney W. Grimes }
1787df8bae1dSRodney W. Grimes 
1788cfb5f768SJonathan Anderson int
1789e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap)
1790cfb5f768SJonathan Anderson {
1791cfb5f768SJonathan Anderson 	struct proc *p;
17927008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
1793cfb5f768SJonathan Anderson 	int error;
1794cfb5f768SJonathan Anderson 
1795cfb5f768SJonathan Anderson 	AUDIT_ARG_SIGNUM(uap->signum);
1796cfb5f768SJonathan Anderson 	AUDIT_ARG_FD(uap->fd);
1797cfb5f768SJonathan Anderson 	if ((u_int)uap->signum > _SIG_MAXSIG)
1798cfb5f768SJonathan Anderson 		return (EINVAL);
1799cfb5f768SJonathan Anderson 
18007008be5bSPawel Jakub Dawidek 	error = procdesc_find(td, uap->fd,
18017008be5bSPawel Jakub Dawidek 	    cap_rights_init(&rights, CAP_PDKILL), &p);
1802cfb5f768SJonathan Anderson 	if (error)
1803cfb5f768SJonathan Anderson 		return (error);
1804cfb5f768SJonathan Anderson 	AUDIT_ARG_PROCESS(p);
1805cfb5f768SJonathan Anderson 	error = p_cansignal(td, p, uap->signum);
1806cfb5f768SJonathan Anderson 	if (error == 0 && uap->signum)
18078451d0ddSKip Macy 		kern_psignal(p, uap->signum);
1808cfb5f768SJonathan Anderson 	PROC_UNLOCK(p);
1809cfb5f768SJonathan Anderson 	return (error);
1810cfb5f768SJonathan Anderson }
1811cfb5f768SJonathan Anderson 
18121930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1813d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1814df8bae1dSRodney W. Grimes struct okillpg_args {
1815df8bae1dSRodney W. Grimes 	int	pgid;
1816df8bae1dSRodney W. Grimes 	int	signum;
1817df8bae1dSRodney W. Grimes };
1818d2d3e875SBruce Evans #endif
1819df8bae1dSRodney W. Grimes /* ARGSUSED */
182026f9a767SRodney W. Grimes int
1821a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap)
1822df8bae1dSRodney W. Grimes {
1823a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1824df8bae1dSRodney W. Grimes 
182514961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->signum);
182614961ba7SRobert Watson 	AUDIT_ARG_PID(uap->pgid);
18276c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1828df8bae1dSRodney W. Grimes 		return (EINVAL);
18299104847fSDavid Xu 
1830a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
1831a3de221dSKonstantin Belousov 	ksi.ksi_signo = uap->signum;
1832a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1833a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1834a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1835a3de221dSKonstantin Belousov 	return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1836df8bae1dSRodney W. Grimes }
18371930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1838df8bae1dSRodney W. Grimes 
18399104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
18409104847fSDavid Xu struct sigqueue_args {
18419104847fSDavid Xu 	pid_t pid;
18429104847fSDavid Xu 	int signum;
18439104847fSDavid Xu 	/* union sigval */ void *value;
18449104847fSDavid Xu };
18459104847fSDavid Xu #endif
18469104847fSDavid Xu int
18478451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
18489104847fSDavid Xu {
1849f19351aaSBrooks Davis 	union sigval sv;
1850f19351aaSBrooks Davis 
1851f19351aaSBrooks Davis 	sv.sival_ptr = uap->value;
1852f19351aaSBrooks Davis 
1853f19351aaSBrooks Davis 	return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
1854f19351aaSBrooks Davis }
1855f19351aaSBrooks Davis 
1856f19351aaSBrooks Davis int
1857f19351aaSBrooks Davis kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
1858f19351aaSBrooks Davis {
18599104847fSDavid Xu 	ksiginfo_t ksi;
18609104847fSDavid Xu 	struct proc *p;
18619104847fSDavid Xu 	int error;
18629104847fSDavid Xu 
1863f19351aaSBrooks Davis 	if ((u_int)signum > _SIG_MAXSIG)
18649104847fSDavid Xu 		return (EINVAL);
18659104847fSDavid Xu 
18669104847fSDavid Xu 	/*
18679104847fSDavid Xu 	 * Specification says sigqueue can only send signal to
18689104847fSDavid Xu 	 * single process.
18699104847fSDavid Xu 	 */
1870f19351aaSBrooks Davis 	if (pid <= 0)
18719104847fSDavid Xu 		return (EINVAL);
18729104847fSDavid Xu 
1873*537d0fb1SMateusz Guzik 	if ((p = pfind_any(pid)) == NULL)
18749104847fSDavid Xu 		return (ESRCH);
1875f19351aaSBrooks Davis 	error = p_cansignal(td, p, signum);
1876f19351aaSBrooks Davis 	if (error == 0 && signum != 0) {
18779104847fSDavid Xu 		ksiginfo_init(&ksi);
1878a3de221dSKonstantin Belousov 		ksi.ksi_flags = KSI_SIGQ;
1879f19351aaSBrooks Davis 		ksi.ksi_signo = signum;
18809104847fSDavid Xu 		ksi.ksi_code = SI_QUEUE;
18819104847fSDavid Xu 		ksi.ksi_pid = td->td_proc->p_pid;
18829104847fSDavid Xu 		ksi.ksi_uid = td->td_ucred->cr_ruid;
1883f19351aaSBrooks Davis 		ksi.ksi_value = *value;
1884ad6eec7bSJohn Baldwin 		error = pksignal(p, ksi.ksi_signo, &ksi);
18859104847fSDavid Xu 	}
18869104847fSDavid Xu 	PROC_UNLOCK(p);
18879104847fSDavid Xu 	return (error);
18889104847fSDavid Xu }
18899104847fSDavid Xu 
1890df8bae1dSRodney W. Grimes /*
1891df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1892df8bae1dSRodney W. Grimes  */
1893df8bae1dSRodney W. Grimes void
1894a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi)
1895df8bae1dSRodney W. Grimes {
1896df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1897df8bae1dSRodney W. Grimes 
1898f591779bSSeigo Tanimura 	if (pgid != 0) {
1899ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1900f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1901ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1902f591779bSSeigo Tanimura 		if (pgrp != NULL) {
1903a3de221dSKonstantin Belousov 			pgsignal(pgrp, sig, 0, ksi);
1904f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1905f591779bSSeigo Tanimura 		}
1906f591779bSSeigo Tanimura 	}
1907df8bae1dSRodney W. Grimes }
1908df8bae1dSRodney W. Grimes 
1909df8bae1dSRodney W. Grimes /*
1910df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1911df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1912df8bae1dSRodney W. Grimes  */
1913df8bae1dSRodney W. Grimes void
1914a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1915df8bae1dSRodney W. Grimes {
1916a3de221dSKonstantin Belousov 	struct proc *p;
1917df8bae1dSRodney W. Grimes 
1918628d2653SJohn Baldwin 	if (pgrp) {
1919f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1920628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1921628d2653SJohn Baldwin 			PROC_LOCK(p);
1922e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
1923e806d352SJohn Baldwin 			    (checkctty == 0 || p->p_flag & P_CONTROLT))
1924a3de221dSKonstantin Belousov 				pksignal(p, sig, ksi);
1925628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1926628d2653SJohn Baldwin 		}
1927628d2653SJohn Baldwin 	}
1928df8bae1dSRodney W. Grimes }
1929df8bae1dSRodney W. Grimes 
1930e442f29fSKonstantin Belousov 
1931e442f29fSKonstantin Belousov /*
1932e442f29fSKonstantin Belousov  * Recalculate the signal mask and reset the signal disposition after
1933e442f29fSKonstantin Belousov  * usermode frame for delivery is formed.  Should be called after
1934e442f29fSKonstantin Belousov  * mach-specific routine, because sysent->sv_sendsig() needs correct
1935e442f29fSKonstantin Belousov  * ps_siginfo and signal mask.
1936e442f29fSKonstantin Belousov  */
1937e442f29fSKonstantin Belousov static void
1938e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps)
1939e442f29fSKonstantin Belousov {
1940e442f29fSKonstantin Belousov 	sigset_t mask;
1941e442f29fSKonstantin Belousov 
1942e442f29fSKonstantin Belousov 	mtx_assert(&ps->ps_mtx, MA_OWNED);
1943e442f29fSKonstantin Belousov 	td->td_ru.ru_nsignals++;
1944e442f29fSKonstantin Belousov 	mask = ps->ps_catchmask[_SIG_IDX(sig)];
1945e442f29fSKonstantin Belousov 	if (!SIGISMEMBER(ps->ps_signodefer, sig))
1946e442f29fSKonstantin Belousov 		SIGADDSET(mask, sig);
1947e442f29fSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1948e442f29fSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1949e442f29fSKonstantin Belousov 	if (SIGISMEMBER(ps->ps_sigreset, sig))
1950e442f29fSKonstantin Belousov 		sigdflt(ps, sig);
1951e442f29fSKonstantin Belousov }
1952e442f29fSKonstantin Belousov 
1953e442f29fSKonstantin Belousov 
1954df8bae1dSRodney W. Grimes /*
19550c14ff0eSRobert Watson  * Send a signal caused by a trap to the current thread.  If it will be
19560c14ff0eSRobert Watson  * caught immediately, deliver it with correct code.  Otherwise, post it
19570c14ff0eSRobert Watson  * normally.
1958df8bae1dSRodney W. Grimes  */
1959df8bae1dSRodney W. Grimes void
19609104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi)
1961df8bae1dSRodney W. Grimes {
19621bf4700bSJeff Roberson 	struct sigacts *ps;
19631bf4700bSJeff Roberson 	struct proc *p;
19649104847fSDavid Xu 	int sig;
19659104847fSDavid Xu 	int code;
19661bf4700bSJeff Roberson 
19671bf4700bSJeff Roberson 	p = td->td_proc;
19689104847fSDavid Xu 	sig = ksi->ksi_signo;
19699104847fSDavid Xu 	code = ksi->ksi_code;
19709104847fSDavid Xu 	KASSERT(_SIG_VALID(sig), ("invalid signal"));
19719104847fSDavid Xu 
1972628d2653SJohn Baldwin 	PROC_LOCK(p);
1973ef3dab76STim J. Robbins 	ps = p->p_sigacts;
197490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
197590af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
19764093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1977df8bae1dSRodney W. Grimes #ifdef KTRACE
1978374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1979374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
19804093529dSJeff Roberson 			    &td->td_sigmask, code);
1981df8bae1dSRodney W. Grimes #endif
19829104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
19839104847fSDavid Xu 				ksi, &td->td_sigmask);
1984e442f29fSKonstantin Belousov 		postsig_done(sig, td, ps);
198590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
19866f841fb7SMarcel Moolenaar 	} else {
1987f71a882fSDavid Xu 		/*
1988f71a882fSDavid Xu 		 * Avoid a possible infinite loop if the thread
1989f71a882fSDavid Xu 		 * masking the signal or process is ignoring the
1990f71a882fSDavid Xu 		 * signal.
1991f71a882fSDavid Xu 		 */
1992f71a882fSDavid Xu 		if (kern_forcesigexit &&
1993f71a882fSDavid Xu 		    (SIGISMEMBER(td->td_sigmask, sig) ||
1994f71a882fSDavid Xu 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1995f71a882fSDavid Xu 			SIGDELSET(td->td_sigmask, sig);
1996f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigcatch, sig);
1997f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigignore, sig);
1998f71a882fSDavid Xu 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1999f71a882fSDavid Xu 		}
200090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
20016626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
20022c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
2003ad6eec7bSJohn Baldwin 		tdsendsignal(p, td, sig, ksi);
2004df8bae1dSRodney W. Grimes 	}
2005628d2653SJohn Baldwin 	PROC_UNLOCK(p);
2006df8bae1dSRodney W. Grimes }
2007df8bae1dSRodney W. Grimes 
20084093529dSJeff Roberson static struct thread *
20094093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
20104093529dSJeff Roberson {
20113074d1b4SDavid Xu 	struct thread *td, *signal_td;
20124093529dSJeff Roberson 
20134093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20144093529dSJeff Roberson 
20154093529dSJeff Roberson 	/*
2016627451c1SDavid Xu 	 * Check if current thread can handle the signal without
201715b7a831SKonstantin Belousov 	 * switching context to another thread.
20184093529dSJeff Roberson 	 */
2019627451c1SDavid Xu 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
2020627451c1SDavid Xu 		return (curthread);
20213074d1b4SDavid Xu 	signal_td = NULL;
20223074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
20233074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
20243074d1b4SDavid Xu 			signal_td = td;
2025627451c1SDavid Xu 			break;
20263074d1b4SDavid Xu 		}
20273074d1b4SDavid Xu 	}
20283074d1b4SDavid Xu 	if (signal_td == NULL)
20293074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
20303074d1b4SDavid Xu 	return (signal_td);
20314093529dSJeff Roberson }
20324093529dSJeff Roberson 
2033df8bae1dSRodney W. Grimes /*
2034df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
2035df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
2036df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
2037df8bae1dSRodney W. Grimes  *
2038df8bae1dSRodney W. Grimes  * Exceptions:
2039df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
2040df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
2041df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
2042df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
2043df8bae1dSRodney W. Grimes  *
2044df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
20454dec0e67SRobert Watson  *
20464dec0e67SRobert Watson  * NB: This function may be entered from the debugger via the "kill" DDB
20474dec0e67SRobert Watson  * command.  There is little that can be done to mitigate the possibly messy
20484dec0e67SRobert Watson  * side effects of this unwise possibility.
2049df8bae1dSRodney W. Grimes  */
2050df8bae1dSRodney W. Grimes void
20518451d0ddSKip Macy kern_psignal(struct proc *p, int sig)
2052df8bae1dSRodney W. Grimes {
2053a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
2054a3de221dSKonstantin Belousov 
2055a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
2056a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
2057a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
2058ad6eec7bSJohn Baldwin 	(void) tdsendsignal(p, NULL, sig, &ksi);
2059a3de221dSKonstantin Belousov }
2060a3de221dSKonstantin Belousov 
2061ad6eec7bSJohn Baldwin int
2062a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2063a3de221dSKonstantin Belousov {
2064a3de221dSKonstantin Belousov 
2065ad6eec7bSJohn Baldwin 	return (tdsendsignal(p, NULL, sig, ksi));
20669104847fSDavid Xu }
20679104847fSDavid Xu 
2068cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */
20699104847fSDavid Xu int
2070cf7d9a8cSDavid Xu sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
20719104847fSDavid Xu {
2072cf7d9a8cSDavid Xu 	struct thread *td;
207341b3077aSJacques Vidrine 
20746d7b314bSDavid Xu 	if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2075cf7d9a8cSDavid Xu 		td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
20766d7b314bSDavid Xu 		if (td == NULL)
20776d7b314bSDavid Xu 			return (ESRCH);
2078cf7d9a8cSDavid Xu 		*ttd = td;
2079cf7d9a8cSDavid Xu 	} else {
2080cf7d9a8cSDavid Xu 		*ttd = NULL;
2081cf7d9a8cSDavid Xu 		PROC_LOCK(p);
20826d7b314bSDavid Xu 	}
2083cf7d9a8cSDavid Xu 	return (0);
20844093529dSJeff Roberson }
20854093529dSJeff Roberson 
2086ad6eec7bSJohn Baldwin void
2087ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig)
2088ad6eec7bSJohn Baldwin {
2089ad6eec7bSJohn Baldwin 	ksiginfo_t ksi;
2090ad6eec7bSJohn Baldwin 
2091ad6eec7bSJohn Baldwin 	ksiginfo_init(&ksi);
2092ad6eec7bSJohn Baldwin 	ksi.ksi_signo = sig;
2093ad6eec7bSJohn Baldwin 	ksi.ksi_code = SI_KERNEL;
2094ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, &ksi);
2095ad6eec7bSJohn Baldwin }
2096ad6eec7bSJohn Baldwin 
2097ad6eec7bSJohn Baldwin void
2098ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2099ad6eec7bSJohn Baldwin {
2100ad6eec7bSJohn Baldwin 
2101ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, ksi);
2102ad6eec7bSJohn Baldwin }
2103ad6eec7bSJohn Baldwin 
2104cf7d9a8cSDavid Xu int
2105ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
21064093529dSJeff Roberson {
21079104847fSDavid Xu 	sig_t action;
21089104847fSDavid Xu 	sigqueue_t *sigqueue;
21099104847fSDavid Xu 	int prop;
211090af4afaSJohn Baldwin 	struct sigacts *ps;
211194f0972bSDavid Xu 	int intrval;
21129104847fSDavid Xu 	int ret = 0;
2113da7bbd2cSJohn Baldwin 	int wakeup_swapper;
2114df8bae1dSRodney W. Grimes 
2115cf7d9a8cSDavid Xu 	MPASS(td == NULL || p == td->td_proc);
21166d7b314bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
21176d7b314bSDavid Xu 
211841b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
2119212bc4b3SDavid Xu 		panic("%s(): invalid signal %d", __func__, sig);
21204093529dSJeff Roberson 
2121212bc4b3SDavid Xu 	KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
21226d7b314bSDavid Xu 
21236d7b314bSDavid Xu 	/*
21246d7b314bSDavid Xu 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
21256d7b314bSDavid Xu 	 */
21266d7b314bSDavid Xu 	if (p->p_state == PRS_ZOMBIE) {
21276d7b314bSDavid Xu 		if (ksi && (ksi->ksi_flags & KSI_INS))
21286d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
21296d7b314bSDavid Xu 		return (ret);
21306d7b314bSDavid Xu 	}
21316d7b314bSDavid Xu 
213290af4afaSJohn Baldwin 	ps = p->p_sigacts;
21339e590ff0SKonstantin Belousov 	KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
21342c42a146SMarcel Moolenaar 	prop = sigprop(sig);
21354093529dSJeff Roberson 
21366d7b314bSDavid Xu 	if (td == NULL) {
21376d7b314bSDavid Xu 		td = sigtd(p, sig, prop);
21386d7b314bSDavid Xu 		sigqueue = &p->p_sigqueue;
2139462314b3SMateusz Guzik 	} else
21409104847fSDavid Xu 		sigqueue = &td->td_sigqueue;
21414093529dSJeff Roberson 
214236160958SMark Johnston 	SDT_PROBE3(proc, , , signal__send, td, p, sig);
21435d217f17SJohn Birrell 
2144df8bae1dSRodney W. Grimes 	/*
2145df8bae1dSRodney W. Grimes 	 * If the signal is being ignored,
2146df8bae1dSRodney W. Grimes 	 * then we forget about it immediately.
214790af4afaSJohn Baldwin 	 * (Note: we don't set SIGCONT in ps_sigignore,
2148df8bae1dSRodney W. Grimes 	 * and if it is set to SIG_IGN,
2149df8bae1dSRodney W. Grimes 	 * action will be SIG_DFL here.)
2150df8bae1dSRodney W. Grimes 	 */
215190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
21520fc32899SJohn Baldwin 	if (SIGISMEMBER(ps->ps_sigignore, sig)) {
215336160958SMark Johnston 		SDT_PROBE3(proc, , , signal__discard, td, p, sig);
21545d217f17SJohn Birrell 
215590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
21566d7b314bSDavid Xu 		if (ksi && (ksi->ksi_flags & KSI_INS))
21576d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
21589104847fSDavid Xu 		return (ret);
215990af4afaSJohn Baldwin 	}
21606675b36eSDavid Xu 	if (SIGISMEMBER(td->td_sigmask, sig))
2161df8bae1dSRodney W. Grimes 		action = SIG_HOLD;
216290af4afaSJohn Baldwin 	else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2163df8bae1dSRodney W. Grimes 		action = SIG_CATCH;
2164df8bae1dSRodney W. Grimes 	else
2165df8bae1dSRodney W. Grimes 		action = SIG_DFL;
216694f0972bSDavid Xu 	if (SIGISMEMBER(ps->ps_sigintr, sig))
216794f0972bSDavid Xu 		intrval = EINTR;
216894f0972bSDavid Xu 	else
216994f0972bSDavid Xu 		intrval = ERESTART;
217090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
2171df8bae1dSRodney W. Grimes 
2172fd50a707SBrooks Davis 	if (prop & SIGPROP_CONT)
21739104847fSDavid Xu 		sigqueue_delete_stopmask_proc(p);
2174fd50a707SBrooks Davis 	else if (prop & SIGPROP_STOP) {
2175df8bae1dSRodney W. Grimes 		/*
2176df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
2177df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
2178df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
2179df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
2180df8bae1dSRodney W. Grimes 		 */
2181fd50a707SBrooks Davis 		if ((prop & SIGPROP_TTYSTOP) &&
2182e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
21836d7b314bSDavid Xu 		    (action == SIG_DFL)) {
21846d7b314bSDavid Xu 			if (ksi && (ksi->ksi_flags & KSI_INS))
21856d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
21869104847fSDavid Xu 			return (ret);
21876d7b314bSDavid Xu 		}
21889104847fSDavid Xu 		sigqueue_delete_proc(p, SIGCONT);
2189ebceaf6dSDavid Xu 		if (p->p_flag & P_CONTINUED) {
21906933e3c1SJulian Elischer 			p->p_flag &= ~P_CONTINUED;
2191ebceaf6dSDavid Xu 			PROC_LOCK(p->p_pptr);
2192ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
2193ebceaf6dSDavid Xu 			PROC_UNLOCK(p->p_pptr);
2194ebceaf6dSDavid Xu 		}
2195df8bae1dSRodney W. Grimes 	}
21963074d1b4SDavid Xu 
21979104847fSDavid Xu 	ret = sigqueue_add(sigqueue, sig, ksi);
21989104847fSDavid Xu 	if (ret != 0)
21999104847fSDavid Xu 		return (ret);
22006d7b314bSDavid Xu 	signotify(td);
22015312b1c7SDavid Xu 	/*
22025312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
22035312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
22045312b1c7SDavid Xu 	 */
22055312b1c7SDavid Xu 	if (action == SIG_HOLD &&
2206fd50a707SBrooks Davis 	    !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
22079104847fSDavid Xu 		return (ret);
2208b4d33259SEric Badger 
2209b4d33259SEric Badger 	/* SIGKILL: Remove procfs STOPEVENTs. */
221090d75f78SAlfred Perlstein 	if (sig == SIGKILL) {
221190d75f78SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCBIC */
221290d75f78SAlfred Perlstein 		p->p_stops = 0;
221390d75f78SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCCONT */
221490d75f78SAlfred Perlstein 		p->p_step = 0;
221590d75f78SAlfred Perlstein 		wakeup(&p->p_step);
221690d75f78SAlfred Perlstein 	}
221790d75f78SAlfred Perlstein 	/*
2218e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
2219e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
2220e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
22211968f37bSJohn Baldwin 	 * times when processing needs to be done immediately, such as
2222e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
22231968f37bSJohn Baldwin 	 * We try to do the per-process part here.
2224df8bae1dSRodney W. Grimes 	 */
2225e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
22260f14f15bSJohn Baldwin 		KASSERT(!(p->p_flag & P_WEXIT),
22270f14f15bSJohn Baldwin 		    ("signal to stopped but exiting process"));
2228e602ba25SJulian Elischer 		if (sig == SIGKILL) {
2229137cf33dSDavid Xu 			/*
2230137cf33dSDavid Xu 			 * If traced process is already stopped,
2231137cf33dSDavid Xu 			 * then no further action is necessary.
2232137cf33dSDavid Xu 			 */
223383b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
223483b718ebSDavid Xu 				goto out;
2235df8bae1dSRodney W. Grimes 			/*
2236e602ba25SJulian Elischer 			 * SIGKILL sets process running.
2237e602ba25SJulian Elischer 			 * It will die elsewhere.
2238e602ba25SJulian Elischer 			 * All threads must be restarted.
2239df8bae1dSRodney W. Grimes 			 */
2240482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
2241e602ba25SJulian Elischer 			goto runfast;
2242e602ba25SJulian Elischer 		}
2243e602ba25SJulian Elischer 
2244fd50a707SBrooks Davis 		if (prop & SIGPROP_CONT) {
2245137cf33dSDavid Xu 			/*
2246137cf33dSDavid Xu 			 * If traced process is already stopped,
2247137cf33dSDavid Xu 			 * then no further action is necessary.
2248137cf33dSDavid Xu 			 */
224983b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
225083b718ebSDavid Xu 				goto out;
2251e602ba25SJulian Elischer 			/*
2252e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
22539104847fSDavid Xu 			 * process but don't leave the signal in sigqueue as
22541d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
2255e602ba25SJulian Elischer 			 * continue the process and leave the signal in
22569104847fSDavid Xu 			 * sigqueue.  If the process catches SIGCONT, let it
2257e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
2258e602ba25SJulian Elischer 			 * an event, it goes back to run state.
2259e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
2260e602ba25SJulian Elischer 			 */
22611279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
22627b4a950aSDavid Xu 			PROC_SLOCK(p);
2263ebceaf6dSDavid Xu 			if (p->p_numthreads == p->p_suspcount) {
22647b4a950aSDavid Xu 				PROC_SUNLOCK(p);
22656933e3c1SJulian Elischer 				p->p_flag |= P_CONTINUED;
2266b4490c6eSKonstantin Belousov 				p->p_xsig = SIGCONT;
22677f96995eSDavid Xu 				PROC_LOCK(p->p_pptr);
2268ebceaf6dSDavid Xu 				childproc_continued(p);
22697f96995eSDavid Xu 				PROC_UNLOCK(p->p_pptr);
22707b4a950aSDavid Xu 				PROC_SLOCK(p);
2271ebceaf6dSDavid Xu 			}
2272e602ba25SJulian Elischer 			if (action == SIG_DFL) {
2273a54e85fdSJeff Roberson 				thread_unsuspend(p);
22747b4a950aSDavid Xu 				PROC_SUNLOCK(p);
22759104847fSDavid Xu 				sigqueue_delete(sigqueue, sig);
2276a54e85fdSJeff Roberson 				goto out;
2277a54e85fdSJeff Roberson 			}
2278a54e85fdSJeff Roberson 			if (action == SIG_CATCH) {
22798460a577SJohn Birrell 				/*
22808460a577SJohn Birrell 				 * The process wants to catch it so it needs
22818460a577SJohn Birrell 				 * to run at least one thread, but which one?
22828460a577SJohn Birrell 				 */
22837b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2284e602ba25SJulian Elischer 				goto runfast;
2285e602ba25SJulian Elischer 			}
2286e602ba25SJulian Elischer 			/*
2287e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
2288e602ba25SJulian Elischer 			 */
228904774f23SJulian Elischer 			thread_unsuspend(p);
22907b4a950aSDavid Xu 			PROC_SUNLOCK(p);
2291e602ba25SJulian Elischer 			goto out;
2292e602ba25SJulian Elischer 		}
2293e602ba25SJulian Elischer 
2294fd50a707SBrooks Davis 		if (prop & SIGPROP_STOP) {
2295137cf33dSDavid Xu 			/*
2296137cf33dSDavid Xu 			 * If traced process is already stopped,
2297137cf33dSDavid Xu 			 * then no further action is necessary.
2298137cf33dSDavid Xu 			 */
229983b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
230083b718ebSDavid Xu 				goto out;
2301e602ba25SJulian Elischer 			/*
2302e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
2303e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
230404774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
2305e602ba25SJulian Elischer 			 */
23061279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
23079104847fSDavid Xu 			sigqueue_delete(sigqueue, sig);
2308e602ba25SJulian Elischer 			goto out;
2309e602ba25SJulian Elischer 		}
2310e602ba25SJulian Elischer 
2311e602ba25SJulian Elischer 		/*
2312e602ba25SJulian Elischer 		 * All other kinds of signals:
2313e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
2314e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
2315e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
231604774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
2317e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
2318e602ba25SJulian Elischer 		 */
2319da7bbd2cSJohn Baldwin 		wakeup_swapper = 0;
23207b4a950aSDavid Xu 		PROC_SLOCK(p);
2321a54e85fdSJeff Roberson 		thread_lock(td);
232244f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2323da7bbd2cSJohn Baldwin 			wakeup_swapper = sleepq_abort(td, intrval);
2324a54e85fdSJeff Roberson 		thread_unlock(td);
23257b4a950aSDavid Xu 		PROC_SUNLOCK(p);
2326da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
2327da7bbd2cSJohn Baldwin 			kick_proc0();
2328df8bae1dSRodney W. Grimes 		goto out;
2329df8bae1dSRodney W. Grimes 		/*
23309a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
23319a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
2332df8bae1dSRodney W. Grimes 		 */
2333e602ba25SJulian Elischer 	} else if (p->p_state == PRS_NORMAL) {
2334ec8297bdSDavid Xu 		if (p->p_flag & P_TRACED || action == SIG_CATCH) {
233594f0972bSDavid Xu 			tdsigwakeup(td, sig, action, intrval);
2336df8bae1dSRodney W. Grimes 			goto out;
233704774f23SJulian Elischer 		}
2338ec8297bdSDavid Xu 
2339ec8297bdSDavid Xu 		MPASS(action == SIG_DFL);
2340ec8297bdSDavid Xu 
2341fd50a707SBrooks Davis 		if (prop & SIGPROP_STOP) {
23420f14f15bSJohn Baldwin 			if (p->p_flag & (P_PPWAIT|P_WEXIT))
234335c32a76SDavid Xu 				goto out;
2344e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
2345b4490c6eSKonstantin Belousov 			p->p_xsig = sig;
23467b4a950aSDavid Xu 			PROC_SLOCK(p);
23476f56cb8dSKonstantin Belousov 			wakeup_swapper = sig_suspend_threads(td, p, 1);
23484093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
2349ebceaf6dSDavid Xu 				/*
2350ebceaf6dSDavid Xu 				 * only thread sending signal to another
2351ebceaf6dSDavid Xu 				 * process can reach here, if thread is sending
2352ebceaf6dSDavid Xu 				 * signal to its process, because thread does
2353ebceaf6dSDavid Xu 				 * not suspend itself here, p_numthreads
2354ebceaf6dSDavid Xu 				 * should never be equal to p_suspcount.
2355ebceaf6dSDavid Xu 				 */
2356ebceaf6dSDavid Xu 				thread_stopped(p);
23577b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2358b4490c6eSKonstantin Belousov 				sigqueue_delete_proc(p, p->p_xsig);
23597b4a950aSDavid Xu 			} else
23607b4a950aSDavid Xu 				PROC_SUNLOCK(p);
23616f56cb8dSKonstantin Belousov 			if (wakeup_swapper)
23626f56cb8dSKonstantin Belousov 				kick_proc0();
236335c32a76SDavid Xu 			goto out;
236435c32a76SDavid Xu 		}
2365e602ba25SJulian Elischer 	} else {
2366e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
23679104847fSDavid Xu 		sigqueue_delete(sigqueue, sig);
2368e602ba25SJulian Elischer 		goto out;
2369e602ba25SJulian Elischer 	}
2370e602ba25SJulian Elischer 
2371b40ce416SJulian Elischer 	/*
2372e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
2373e602ba25SJulian Elischer 	 * running threads.
2374b40ce416SJulian Elischer 	 */
2375e602ba25SJulian Elischer runfast:
237694f0972bSDavid Xu 	tdsigwakeup(td, sig, action, intrval);
23777b4a950aSDavid Xu 	PROC_SLOCK(p);
2378e602ba25SJulian Elischer 	thread_unsuspend(p);
23797b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2380e602ba25SJulian Elischer out:
23817b4a950aSDavid Xu 	/* If we jump here, proc slock should not be owned. */
23827b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
23839104847fSDavid Xu 	return (ret);
2384e602ba25SJulian Elischer }
2385e602ba25SJulian Elischer 
2386e602ba25SJulian Elischer /*
2387e602ba25SJulian Elischer  * The force of a signal has been directed against a single
2388e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
2389e602ba25SJulian Elischer  * out of any sleep it may be in etc.
2390e602ba25SJulian Elischer  */
2391e602ba25SJulian Elischer static void
239294f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2393e602ba25SJulian Elischer {
2394e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
23953e85b721SEd Maste 	int prop;
2396da7bbd2cSJohn Baldwin 	int wakeup_swapper;
2397e602ba25SJulian Elischer 
2398da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
23998b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2400e602ba25SJulian Elischer 	prop = sigprop(sig);
2401a4c2da15SBruce Evans 
24027b4a950aSDavid Xu 	PROC_SLOCK(p);
2403374ae2a3SJeff Roberson 	thread_lock(td);
2404e602ba25SJulian Elischer 	/*
2405aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
2406aef68c96SKonstantin Belousov 	 * killed in this lifetime.  Be careful to avoid bumping the
2407aef68c96SKonstantin Belousov 	 * priority of the idle thread, since we still allow to signal
2408aef68c96SKonstantin Belousov 	 * kernel processes.
2409e602ba25SJulian Elischer 	 */
2410fd50a707SBrooks Davis 	if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2411aef68c96SKonstantin Belousov 	    td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2412b3a4fb14SDavid Xu 		sched_prio(td, PUSER);
241380c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
2414e602ba25SJulian Elischer 		/*
2415e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
2416e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
2417e602ba25SJulian Elischer 		 * be noticed when the process returns through
2418e602ba25SJulian Elischer 		 * trap() or syscall().
2419e602ba25SJulian Elischer 		 */
242044f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
2421374ae2a3SJeff Roberson 			goto out;
2422e602ba25SJulian Elischer 		/*
2423e602ba25SJulian Elischer 		 * If SIGCONT is default (or ignored) and process is
2424e602ba25SJulian Elischer 		 * asleep, we are finished; the process should not
2425e602ba25SJulian Elischer 		 * be awakened.
2426df8bae1dSRodney W. Grimes 		 */
2427fd50a707SBrooks Davis 		if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2428a54e85fdSJeff Roberson 			thread_unlock(td);
24297b4a950aSDavid Xu 			PROC_SUNLOCK(p);
24309104847fSDavid Xu 			sigqueue_delete(&p->p_sigqueue, sig);
24314093529dSJeff Roberson 			/*
24324093529dSJeff Roberson 			 * It may be on either list in this state.
24334093529dSJeff Roberson 			 * Remove from both for now.
24344093529dSJeff Roberson 			 */
24359104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
2436aa0fa334SJulian Elischer 			return;
2437df8bae1dSRodney W. Grimes 		}
2438df8bae1dSRodney W. Grimes 
2439aa0fa334SJulian Elischer 		/*
2440a120a7a3SJohn Baldwin 		 * Don't awaken a sleeping thread for SIGSTOP if the
2441a120a7a3SJohn Baldwin 		 * STOP signal is deferred.
2442a120a7a3SJohn Baldwin 		 */
2443fd50a707SBrooks Davis 		if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
24446f56cb8dSKonstantin Belousov 		    TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2445a120a7a3SJohn Baldwin 			goto out;
2446a120a7a3SJohn Baldwin 
2447a120a7a3SJohn Baldwin 		/*
2448a4c2da15SBruce Evans 		 * Give low priority threads a better chance to run.
2449aa0fa334SJulian Elischer 		 */
2450aef68c96SKonstantin Belousov 		if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2451b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
2452ec8297bdSDavid Xu 
2453da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_abort(td, intrval);
2454a4c2da15SBruce Evans 	} else {
2455df8bae1dSRodney W. Grimes 		/*
2456a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
2457df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2458df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2459df8bae1dSRodney W. Grimes 		 */
2460a4c2da15SBruce Evans #ifdef SMP
246144f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
2462e602ba25SJulian Elischer 			forward_signal(td);
24633163861cSTor Egge #endif
24646caa8a15SJohn Baldwin 	}
2465374ae2a3SJeff Roberson out:
24667b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2467374ae2a3SJeff Roberson 	thread_unlock(td);
2468da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
2469da7bbd2cSJohn Baldwin 		kick_proc0();
2470a4c2da15SBruce Evans }
2471df8bae1dSRodney W. Grimes 
24726f56cb8dSKonstantin Belousov static int
2473d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2474d8267df7SDavid Xu {
2475d8267df7SDavid Xu 	struct thread *td2;
24766f56cb8dSKonstantin Belousov 	int wakeup_swapper;
2477d8267df7SDavid Xu 
2478d8267df7SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
24797b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
24800c46712cSMark Johnston 	MPASS(sending || td == curthread);
2481d8267df7SDavid Xu 
24826f56cb8dSKonstantin Belousov 	wakeup_swapper = 0;
2483d8267df7SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td2) {
2484a54e85fdSJeff Roberson 		thread_lock(td2);
2485b7edba77SJeff Roberson 		td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2486d8267df7SDavid Xu 		if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2487f33a947bSKonstantin Belousov 		    (td2->td_flags & TDF_SINTR)) {
2488f33a947bSKonstantin Belousov 			if (td2->td_flags & TDF_SBDRY) {
2489a120a7a3SJohn Baldwin 				/*
2490a120a7a3SJohn Baldwin 				 * Once a thread is asleep with
24916f56cb8dSKonstantin Belousov 				 * TDF_SBDRY and without TDF_SERESTART
24926f56cb8dSKonstantin Belousov 				 * or TDF_SEINTR set, it should never
2493a120a7a3SJohn Baldwin 				 * become suspended due to this check.
2494a120a7a3SJohn Baldwin 				 */
2495a120a7a3SJohn Baldwin 				KASSERT(!TD_IS_SUSPENDED(td2),
2496a120a7a3SJohn Baldwin 				    ("thread with deferred stops suspended"));
24970c46712cSMark Johnston 				if (TD_SBDRY_INTR(td2))
249846e47c4fSKonstantin Belousov 					wakeup_swapper |= sleepq_abort(td2,
249946e47c4fSKonstantin Belousov 					    TD_SBDRY_ERRNO(td2));
2500f33a947bSKonstantin Belousov 			} else if (!TD_IS_SUSPENDED(td2)) {
2501d8267df7SDavid Xu 				thread_suspend_one(td2);
2502f33a947bSKonstantin Belousov 			}
2503f33a947bSKonstantin Belousov 		} else if (!TD_IS_SUSPENDED(td2)) {
2504d8267df7SDavid Xu 			if (sending || td != td2)
2505d8267df7SDavid Xu 				td2->td_flags |= TDF_ASTPENDING;
2506d8267df7SDavid Xu #ifdef SMP
2507d8267df7SDavid Xu 			if (TD_IS_RUNNING(td2) && td2 != td)
2508d8267df7SDavid Xu 				forward_signal(td2);
2509d8267df7SDavid Xu #endif
2510d8267df7SDavid Xu 		}
2511a54e85fdSJeff Roberson 		thread_unlock(td2);
2512d8267df7SDavid Xu 	}
25136f56cb8dSKonstantin Belousov 	return (wakeup_swapper);
2514d8267df7SDavid Xu }
2515d8267df7SDavid Xu 
251682a4538fSEric Badger /*
251782a4538fSEric Badger  * Stop the process for an event deemed interesting to the debugger. If si is
251882a4538fSEric Badger  * non-NULL, this is a signal exchange; the new signal requested by the
251982a4538fSEric Badger  * debugger will be returned for handling. If si is NULL, this is some other
252082a4538fSEric Badger  * type of interesting event. The debugger may request a signal be delivered in
252182a4538fSEric Badger  * that case as well, however it will be deferred until it can be handled.
252282a4538fSEric Badger  */
2523cbf4e354SDavid Xu int
252482a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si)
25254cc9f52fSRobert Drehmel {
25264cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
252782a4538fSEric Badger 	struct thread *td2;
252882a4538fSEric Badger 	ksiginfo_t ksi;
252982a4538fSEric Badger 	int prop;
25304cc9f52fSRobert Drehmel 
253130a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
25320f14f15bSJohn Baldwin 	KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
25334cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2534aa89d8cdSJohn Baldwin 	    &p->p_mtx.lock_object, "Stopping for traced signal");
25354cc9f52fSRobert Drehmel 
2536cbf4e354SDavid Xu 	td->td_xsig = sig;
253782a4538fSEric Badger 
253882a4538fSEric Badger 	if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
253982a4538fSEric Badger 		td->td_dbgflags |= TDB_XSIG;
2540515b7a0bSJohn Baldwin 		CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2541515b7a0bSJohn Baldwin 		    td->td_tid, p->p_pid, td->td_dbgflags, sig);
25427b4a950aSDavid Xu 		PROC_SLOCK(p);
2543904c5ec4SDavid Xu 		while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
254482a4538fSEric Badger 			if (P_KILLED(p)) {
254582a4538fSEric Badger 				/*
254682a4538fSEric Badger 				 * Ensure that, if we've been PT_KILLed, the
254782a4538fSEric Badger 				 * exit status reflects that. Another thread
254882a4538fSEric Badger 				 * may also be in ptracestop(), having just
254982a4538fSEric Badger 				 * received the SIGKILL, but this thread was
255082a4538fSEric Badger 				 * unsuspended first.
255182a4538fSEric Badger 				 */
255282a4538fSEric Badger 				td->td_dbgflags &= ~TDB_XSIG;
255382a4538fSEric Badger 				td->td_xsig = SIGKILL;
255482a4538fSEric Badger 				p->p_ptevents = 0;
255582a4538fSEric Badger 				break;
255682a4538fSEric Badger 			}
25575fcfab6eSJohn Baldwin 			if (p->p_flag & P_SINGLE_EXIT &&
25585fcfab6eSJohn Baldwin 			    !(td->td_dbgflags & TDB_EXIT)) {
25595fcfab6eSJohn Baldwin 				/*
25605fcfab6eSJohn Baldwin 				 * Ignore ptrace stops except for thread exit
25615fcfab6eSJohn Baldwin 				 * events when the process exits.
25625fcfab6eSJohn Baldwin 				 */
2563904c5ec4SDavid Xu 				td->td_dbgflags &= ~TDB_XSIG;
25647b4a950aSDavid Xu 				PROC_SUNLOCK(p);
256582a4538fSEric Badger 				return (0);
2566cbf4e354SDavid Xu 			}
2567b7a25e63SKonstantin Belousov 
2568cbf4e354SDavid Xu 			/*
2569b7a25e63SKonstantin Belousov 			 * Make wait(2) work.  Ensure that right after the
2570b7a25e63SKonstantin Belousov 			 * attach, the thread which was decided to become the
2571b7a25e63SKonstantin Belousov 			 * leader of attach gets reported to the waiter.
2572b7a25e63SKonstantin Belousov 			 * Otherwise, just avoid overwriting another thread's
2573b7a25e63SKonstantin Belousov 			 * assignment to p_xthread.  If another thread has
2574b7a25e63SKonstantin Belousov 			 * already set p_xthread, the current thread will get
2575b7a25e63SKonstantin Belousov 			 * a chance to report itself upon the next iteration.
2576cbf4e354SDavid Xu 			 */
2577b7a25e63SKonstantin Belousov 			if ((td->td_dbgflags & TDB_FSTP) != 0 ||
25787f649ddaSMark Johnston 			    ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2579b7a25e63SKonstantin Belousov 			    p->p_xthread == NULL)) {
2580b4490c6eSKonstantin Belousov 				p->p_xsig = sig;
2581cbf4e354SDavid Xu 				p->p_xthread = td;
2582b7a25e63SKonstantin Belousov 				td->td_dbgflags &= ~TDB_FSTP;
2583b7a25e63SKonstantin Belousov 				p->p_flag2 &= ~P2_PTRACE_FSTP;
2584b7a25e63SKonstantin Belousov 				p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2585d8267df7SDavid Xu 				sig_suspend_threads(td, p, 0);
2586b7a25e63SKonstantin Belousov 			}
25876fa39a73SKonstantin Belousov 			if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
25886fa39a73SKonstantin Belousov 				td->td_dbgflags &= ~TDB_STOPATFORK;
25896fa39a73SKonstantin Belousov 				cv_broadcast(&p->p_dbgwait);
25906fa39a73SKonstantin Belousov 			}
2591cbf4e354SDavid Xu stopme:
25926ddcc233SKonstantin Belousov 			thread_suspend_switch(td, p);
25937ce60f60SDavid Xu 			if (p->p_xthread == td)
25947ce60f60SDavid Xu 				p->p_xthread = NULL;
25957ce60f60SDavid Xu 			if (!(p->p_flag & P_TRACED))
2596cbf4e354SDavid Xu 				break;
2597904c5ec4SDavid Xu 			if (td->td_dbgflags & TDB_SUSPEND) {
2598cbf4e354SDavid Xu 				if (p->p_flag & P_SINGLE_EXIT)
2599cbf4e354SDavid Xu 					break;
2600cbf4e354SDavid Xu 				goto stopme;
2601cbf4e354SDavid Xu 			}
2602cbf4e354SDavid Xu 		}
26037b4a950aSDavid Xu 		PROC_SUNLOCK(p);
260482a4538fSEric Badger 	}
260582a4538fSEric Badger 
260682a4538fSEric Badger 	if (si != NULL && sig == td->td_xsig) {
260782a4538fSEric Badger 		/* Parent wants us to take the original signal unchanged. */
260882a4538fSEric Badger 		si->ksi_flags |= KSI_HEAD;
260982a4538fSEric Badger 		if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
261082a4538fSEric Badger 			si->ksi_signo = 0;
261182a4538fSEric Badger 	} else if (td->td_xsig != 0) {
261282a4538fSEric Badger 		/*
261382a4538fSEric Badger 		 * If parent wants us to take a new signal, then it will leave
261482a4538fSEric Badger 		 * it in td->td_xsig; otherwise we just look for signals again.
261582a4538fSEric Badger 		 */
261682a4538fSEric Badger 		ksiginfo_init(&ksi);
261782a4538fSEric Badger 		ksi.ksi_signo = td->td_xsig;
261882a4538fSEric Badger 		ksi.ksi_flags |= KSI_PTRACE;
261982a4538fSEric Badger 		prop = sigprop(td->td_xsig);
262082a4538fSEric Badger 		td2 = sigtd(p, td->td_xsig, prop);
262182a4538fSEric Badger 		tdsendsignal(p, td2, td->td_xsig, &ksi);
262282a4538fSEric Badger 		if (td != td2)
262382a4538fSEric Badger 			return (0);
262482a4538fSEric Badger 	}
262582a4538fSEric Badger 
2626cbf4e354SDavid Xu 	return (td->td_xsig);
26274cc9f52fSRobert Drehmel }
26284cc9f52fSRobert Drehmel 
26296b286ee8SKonstantin Belousov static void
263080a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags)
26316b286ee8SKonstantin Belousov {
26326b286ee8SKonstantin Belousov 	struct sigacts *ps;
26336b286ee8SKonstantin Belousov 	struct thread *td;
2634407af02bSDavid Xu 	int sig;
26356b286ee8SKonstantin Belousov 
26366b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
263770778bbaSKonstantin Belousov 	ps = p->p_sigacts;
263870778bbaSKonstantin Belousov 	mtx_assert(&ps->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 ?
263970778bbaSKonstantin Belousov 	    MA_OWNED : MA_NOTOWNED);
2640407af02bSDavid Xu 	if (SIGISEMPTY(p->p_siglist))
2641407af02bSDavid Xu 		return;
2642407af02bSDavid Xu 	SIGSETAND(block, p->p_siglist);
2643407af02bSDavid Xu 	while ((sig = sig_ffs(&block)) != 0) {
2644407af02bSDavid Xu 		SIGDELSET(block, sig);
2645407af02bSDavid Xu 		td = sigtd(p, sig, 0);
26466b286ee8SKonstantin Belousov 		signotify(td);
264780a8b0f3SKonstantin Belousov 		if (!(flags & SIGPROCMASK_PS_LOCKED))
26486b286ee8SKonstantin Belousov 			mtx_lock(&ps->ps_mtx);
2649396a0d44SKonstantin Belousov 		if (p->p_flag & P_TRACED ||
2650396a0d44SKonstantin Belousov 		    (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2651396a0d44SKonstantin Belousov 		    !SIGISMEMBER(td->td_sigmask, sig)))
2652407af02bSDavid Xu 			tdsigwakeup(td, sig, SIG_CATCH,
2653407af02bSDavid Xu 			    (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
26546b286ee8SKonstantin Belousov 			     ERESTART));
265580a8b0f3SKonstantin Belousov 		if (!(flags & SIGPROCMASK_PS_LOCKED))
26566b286ee8SKonstantin Belousov 			mtx_unlock(&ps->ps_mtx);
26576b286ee8SKonstantin Belousov 	}
26586b286ee8SKonstantin Belousov }
26596b286ee8SKonstantin Belousov 
26606b286ee8SKonstantin Belousov void
26616b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td)
26626b286ee8SKonstantin Belousov {
26636b286ee8SKonstantin Belousov 	struct proc *p;
26646b286ee8SKonstantin Belousov 	sigset_t unblocked;
26656b286ee8SKonstantin Belousov 
26666b286ee8SKonstantin Belousov 	p = td->td_proc;
26676b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
26686b286ee8SKonstantin Belousov 
26696b286ee8SKonstantin Belousov 	sigqueue_flush(&td->td_sigqueue);
26706b286ee8SKonstantin Belousov 	if (p->p_numthreads == 1)
26716b286ee8SKonstantin Belousov 		return;
26726b286ee8SKonstantin Belousov 
26736b286ee8SKonstantin Belousov 	/*
26746b286ee8SKonstantin Belousov 	 * Since we cannot handle signals, notify signal post code
26756b286ee8SKonstantin Belousov 	 * about this by filling the sigmask.
26766b286ee8SKonstantin Belousov 	 *
26776b286ee8SKonstantin Belousov 	 * Also, if needed, wake up thread(s) that do not block the
26786b286ee8SKonstantin Belousov 	 * same signals as the exiting thread, since the thread might
26796b286ee8SKonstantin Belousov 	 * have been selected for delivery and woken up.
26806b286ee8SKonstantin Belousov 	 */
26816b286ee8SKonstantin Belousov 	SIGFILLSET(unblocked);
26826b286ee8SKonstantin Belousov 	SIGSETNAND(unblocked, td->td_sigmask);
26836b286ee8SKonstantin Belousov 	SIGFILLSET(td->td_sigmask);
268480a8b0f3SKonstantin Belousov 	reschedule_signals(p, unblocked, 0);
26856b286ee8SKonstantin Belousov 
26866b286ee8SKonstantin Belousov }
26876b286ee8SKonstantin Belousov 
26883a1e5dd8SKonstantin Belousov static int
26893a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags)
2690a120a7a3SJohn Baldwin {
2691a120a7a3SJohn Baldwin 
26923a1e5dd8SKonstantin Belousov 	MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
26933a1e5dd8SKonstantin Belousov 	    (cflags & TDF_SBDRY) != 0);
26943a1e5dd8SKonstantin Belousov 	return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2695a120a7a3SJohn Baldwin }
2696a120a7a3SJohn Baldwin 
2697a120a7a3SJohn Baldwin /*
26983a1e5dd8SKonstantin Belousov  * Defer the delivery of SIGSTOP for the current thread, according to
26993a1e5dd8SKonstantin Belousov  * the requested mode.  Returns previous flags, which must be restored
27003a1e5dd8SKonstantin Belousov  * by sigallowstop().
27013a1e5dd8SKonstantin Belousov  *
27023a1e5dd8SKonstantin Belousov  * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
27033a1e5dd8SKonstantin Belousov  * cleared by the current thread, which allow the lock-less read-only
27043a1e5dd8SKonstantin Belousov  * accesses below.
2705a120a7a3SJohn Baldwin  */
2706e3612a4cSKonstantin Belousov int
270746e47c4fSKonstantin Belousov sigdeferstop_impl(int mode)
2708a120a7a3SJohn Baldwin {
2709593efaf9SJohn Baldwin 	struct thread *td;
27103a1e5dd8SKonstantin Belousov 	int cflags, nflags;
2711a120a7a3SJohn Baldwin 
2712593efaf9SJohn Baldwin 	td = curthread;
27133a1e5dd8SKonstantin Belousov 	cflags = sigdeferstop_curr_flags(td->td_flags);
27143a1e5dd8SKonstantin Belousov 	switch (mode) {
27153a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_NOP:
27163a1e5dd8SKonstantin Belousov 		nflags = cflags;
27173a1e5dd8SKonstantin Belousov 		break;
27183a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_OFF:
27193a1e5dd8SKonstantin Belousov 		nflags = 0;
27203a1e5dd8SKonstantin Belousov 		break;
27213a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_SILENT:
27223a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
27233a1e5dd8SKonstantin Belousov 		break;
27243a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_EINTR:
27253a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
27263a1e5dd8SKonstantin Belousov 		break;
27273a1e5dd8SKonstantin Belousov 	case SIGDEFERSTOP_ERESTART:
27283a1e5dd8SKonstantin Belousov 		nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
27293a1e5dd8SKonstantin Belousov 		break;
27303a1e5dd8SKonstantin Belousov 	default:
27313a1e5dd8SKonstantin Belousov 		panic("sigdeferstop: invalid mode %x", mode);
27323a1e5dd8SKonstantin Belousov 		break;
27333a1e5dd8SKonstantin Belousov 	}
273446e47c4fSKonstantin Belousov 	if (cflags == nflags)
273546e47c4fSKonstantin Belousov 		return (SIGDEFERSTOP_VAL_NCHG);
2736a120a7a3SJohn Baldwin 	thread_lock(td);
27373a1e5dd8SKonstantin Belousov 	td->td_flags = (td->td_flags & ~cflags) | nflags;
2738a120a7a3SJohn Baldwin 	thread_unlock(td);
27393a1e5dd8SKonstantin Belousov 	return (cflags);
27403a1e5dd8SKonstantin Belousov }
27413a1e5dd8SKonstantin Belousov 
27423a1e5dd8SKonstantin Belousov /*
27433a1e5dd8SKonstantin Belousov  * Restores the STOP handling mode, typically permitting the delivery
27443a1e5dd8SKonstantin Belousov  * of SIGSTOP for the current thread.  This does not immediately
27453a1e5dd8SKonstantin Belousov  * suspend if a stop was posted.  Instead, the thread will suspend
27463a1e5dd8SKonstantin Belousov  * either via ast() or a subsequent interruptible sleep.
27473a1e5dd8SKonstantin Belousov  */
27483a1e5dd8SKonstantin Belousov void
274946e47c4fSKonstantin Belousov sigallowstop_impl(int prev)
27503a1e5dd8SKonstantin Belousov {
27513a1e5dd8SKonstantin Belousov 	struct thread *td;
27523a1e5dd8SKonstantin Belousov 	int cflags;
27533a1e5dd8SKonstantin Belousov 
275446e47c4fSKonstantin Belousov 	KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
27553a1e5dd8SKonstantin Belousov 	KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
27563a1e5dd8SKonstantin Belousov 	    ("sigallowstop: incorrect previous mode %x", prev));
27573a1e5dd8SKonstantin Belousov 	td = curthread;
27583a1e5dd8SKonstantin Belousov 	cflags = sigdeferstop_curr_flags(td->td_flags);
27593a1e5dd8SKonstantin Belousov 	if (cflags != prev) {
27603a1e5dd8SKonstantin Belousov 		thread_lock(td);
27613a1e5dd8SKonstantin Belousov 		td->td_flags = (td->td_flags & ~cflags) | prev;
27623a1e5dd8SKonstantin Belousov 		thread_unlock(td);
27633a1e5dd8SKonstantin Belousov 	}
2764a120a7a3SJohn Baldwin }
2765a120a7a3SJohn Baldwin 
2766df8bae1dSRodney W. Grimes /*
2767df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2768df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2769df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2770df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2771df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2772628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2773df8bae1dSRodney W. Grimes  * sequence is
2774df8bae1dSRodney W. Grimes  *
2775e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
27762c42a146SMarcel Moolenaar  *		postsig(sig);
2777df8bae1dSRodney W. Grimes  */
27786711f10fSJohn Baldwin static int
27793cf3b9f0SJohn Baldwin issignal(struct thread *td)
2780df8bae1dSRodney W. Grimes {
2781e602ba25SJulian Elischer 	struct proc *p;
278290af4afaSJohn Baldwin 	struct sigacts *ps;
27836b286ee8SKonstantin Belousov 	struct sigqueue *queue;
27844093529dSJeff Roberson 	sigset_t sigpending;
278586be94fcSTycho Nightingale 	ksiginfo_t ksi;
27860167b33bSKonstantin Belousov 	int prop, sig, traced;
2787df8bae1dSRodney W. Grimes 
2788e602ba25SJulian Elischer 	p = td->td_proc;
278990af4afaSJohn Baldwin 	ps = p->p_sigacts;
279090af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2791628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2792df8bae1dSRodney W. Grimes 	for (;;) {
27930167b33bSKonstantin Belousov 		traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
27942a024a2bSSean Eric Fagan 
27959104847fSDavid Xu 		sigpending = td->td_sigqueue.sq_signals;
27966b286ee8SKonstantin Belousov 		SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
27974093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
27984093529dSJeff Roberson 
27996f56cb8dSKonstantin Belousov 		if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
28006f56cb8dSKonstantin Belousov 		    (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
28014093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
28024093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2803df8bae1dSRodney W. Grimes 			return (0);
2804b7a25e63SKonstantin Belousov 		if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
2805b7a25e63SKonstantin Belousov 		    (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
2806b7a25e63SKonstantin Belousov 		    SIGISMEMBER(sigpending, SIGSTOP)) {
2807b7a25e63SKonstantin Belousov 			/*
2808b7a25e63SKonstantin Belousov 			 * If debugger just attached, always consume
2809b7a25e63SKonstantin Belousov 			 * SIGSTOP from ptrace(PT_ATTACH) first, to
2810b7a25e63SKonstantin Belousov 			 * execute the debugger attach ritual in
2811b7a25e63SKonstantin Belousov 			 * order.
2812b7a25e63SKonstantin Belousov 			 */
2813b7a25e63SKonstantin Belousov 			sig = SIGSTOP;
2814b7a25e63SKonstantin Belousov 			td->td_dbgflags |= TDB_FSTP;
2815b7a25e63SKonstantin Belousov 		} else {
28164093529dSJeff Roberson 			sig = sig_ffs(&sigpending);
2817b7a25e63SKonstantin Belousov 		}
28182a024a2bSSean Eric Fagan 
2819047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2820047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2821047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2822047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2823047aa39bSRobert Watson 		}
28242a024a2bSSean Eric Fagan 
2825df8bae1dSRodney W. Grimes 		/*
2826df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2827df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2828df8bae1dSRodney W. Grimes 		 */
282990af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
28309104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
28316b286ee8SKonstantin Belousov 			sigqueue_delete(&p->p_sigqueue, sig);
2832df8bae1dSRodney W. Grimes 			continue;
2833df8bae1dSRodney W. Grimes 		}
2834b7a25e63SKonstantin Belousov 		if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
2835df8bae1dSRodney W. Grimes 			/*
2836d8f4f6a4SJonathan Mini 			 * If traced, always stop.
28376a671a6bSKonstantin Belousov 			 * Remove old signal from queue before the stop.
28386a671a6bSKonstantin Belousov 			 * XXX shrug off debugger, it causes siginfo to
28396a671a6bSKonstantin Belousov 			 * be thrown away.
2840df8bae1dSRodney W. Grimes 			 */
28416a671a6bSKonstantin Belousov 			queue = &td->td_sigqueue;
284286be94fcSTycho Nightingale 			ksiginfo_init(&ksi);
284386be94fcSTycho Nightingale 			if (sigqueue_get(queue, sig, &ksi) == 0) {
28446a671a6bSKonstantin Belousov 				queue = &p->p_sigqueue;
284586be94fcSTycho Nightingale 				sigqueue_get(queue, sig, &ksi);
28466a671a6bSKonstantin Belousov 			}
284786be94fcSTycho Nightingale 			td->td_si = ksi.ksi_info;
28486a671a6bSKonstantin Belousov 
284990af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
285086be94fcSTycho Nightingale 			sig = ptracestop(td, sig, &ksi);
285190af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2852df8bae1dSRodney W. Grimes 
2853df8bae1dSRodney W. Grimes 			/*
2854e9445808SKonstantin Belousov 			 * Keep looking if the debugger discarded or
2855e9445808SKonstantin Belousov 			 * replaced the signal.
28568d542cb5SDavid E. O'Brien 			 */
2857e9445808SKonstantin Belousov 			if (sig == 0)
28588d542cb5SDavid E. O'Brien 				continue;
2859e9445808SKonstantin Belousov 
2860e9445808SKonstantin Belousov 			/*
2861e9445808SKonstantin Belousov 			 * If the signal became masked, re-queue it.
2862e9445808SKonstantin Belousov 			 */
2863e9445808SKonstantin Belousov 			if (SIGISMEMBER(td->td_sigmask, sig)) {
2864e9445808SKonstantin Belousov 				ksi.ksi_flags |= KSI_HEAD;
2865e9445808SKonstantin Belousov 				sigqueue_add(&p->p_sigqueue, sig, &ksi);
2866e9445808SKonstantin Belousov 				continue;
2867e9445808SKonstantin Belousov 			}
2868e9445808SKonstantin Belousov 
2869e9445808SKonstantin Belousov 			/*
2870e9445808SKonstantin Belousov 			 * If the traced bit got turned off, requeue
2871e9445808SKonstantin Belousov 			 * the signal and go back up to the top to
2872e9445808SKonstantin Belousov 			 * rescan signals.  This ensures that p_sig*
2873e9445808SKonstantin Belousov 			 * and p_sigact are consistent.
2874e9445808SKonstantin Belousov 			 */
2875e9445808SKonstantin Belousov 			if ((p->p_flag & P_TRACED) == 0) {
2876e9445808SKonstantin Belousov 				ksi.ksi_flags |= KSI_HEAD;
2877e9445808SKonstantin Belousov 				sigqueue_add(queue, sig, &ksi);
2878e9445808SKonstantin Belousov 				continue;
2879e9445808SKonstantin Belousov 			}
2880df8bae1dSRodney W. Grimes 		}
2881df8bae1dSRodney W. Grimes 
28828d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
28838d542cb5SDavid E. O'Brien 
2884df8bae1dSRodney W. Grimes 		/*
2885df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2886df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2887df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2888df8bae1dSRodney W. Grimes 		 */
2889d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2890df8bae1dSRodney W. Grimes 
2891d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2892df8bae1dSRodney W. Grimes 			/*
2893df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2894df8bae1dSRodney W. Grimes 			 */
2895df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2896df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2897df8bae1dSRodney W. Grimes 				/*
2898df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2899df8bae1dSRodney W. Grimes 				 * in init? XXX
2900df8bae1dSRodney W. Grimes 				 */
2901d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
29022c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2903df8bae1dSRodney W. Grimes #endif
2904df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2905df8bae1dSRodney W. Grimes 			}
2906df8bae1dSRodney W. Grimes 			/*
2907b38bd91fSEric Badger 			 * If there is a pending stop signal to process with
2908b38bd91fSEric Badger 			 * default action, stop here, then clear the signal.
2909b38bd91fSEric Badger 			 * Traced or exiting processes should ignore stops.
2910b38bd91fSEric Badger 			 * Additionally, a member of an orphaned process group
2911b38bd91fSEric Badger 			 * should ignore tty stops.
2912df8bae1dSRodney W. Grimes 			 */
2913fd50a707SBrooks Davis 			if (prop & SIGPROP_STOP) {
2914b38bd91fSEric Badger 				if (p->p_flag &
2915b38bd91fSEric Badger 				    (P_TRACED | P_WEXIT | P_SINGLE_EXIT) ||
2916df8bae1dSRodney W. Grimes 				    (p->p_pgrp->pg_jobc == 0 &&
2917fd50a707SBrooks Davis 				     prop & SIGPROP_TTYSTOP))
2918df8bae1dSRodney W. Grimes 					break;	/* == ignore */
291946e47c4fSKonstantin Belousov 				if (TD_SBDRY_INTR(td)) {
292046e47c4fSKonstantin Belousov 					KASSERT((td->td_flags & TDF_SBDRY) != 0,
292146e47c4fSKonstantin Belousov 					    ("lost TDF_SBDRY"));
292246e47c4fSKonstantin Belousov 					return (-1);
292346e47c4fSKonstantin Belousov 				}
292490af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
292590af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2926aa89d8cdSJohn Baldwin 				    &p->p_mtx.lock_object, "Catching SIGSTOP");
2927b7a25e63SKonstantin Belousov 				sigqueue_delete(&td->td_sigqueue, sig);
2928b7a25e63SKonstantin Belousov 				sigqueue_delete(&p->p_sigqueue, sig);
2929e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
2930b4490c6eSKonstantin Belousov 				p->p_xsig = sig;
29317b4a950aSDavid Xu 				PROC_SLOCK(p);
2932d8267df7SDavid Xu 				sig_suspend_threads(td, p, 0);
29336ddcc233SKonstantin Belousov 				thread_suspend_switch(td, p);
29347b4a950aSDavid Xu 				PROC_SUNLOCK(p);
293590af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2936b7a25e63SKonstantin Belousov 				goto next;
2937fd50a707SBrooks Davis 			} else if (prop & SIGPROP_IGNORE) {
2938df8bae1dSRodney W. Grimes 				/*
2939df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2940df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2941df8bae1dSRodney W. Grimes 				 */
2942df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2943df8bae1dSRodney W. Grimes 			} else
29442c42a146SMarcel Moolenaar 				return (sig);
2945df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2946df8bae1dSRodney W. Grimes 
2947d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2948df8bae1dSRodney W. Grimes 			/*
2949df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2950df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2951df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2952df8bae1dSRodney W. Grimes 			 */
2953fd50a707SBrooks Davis 			if ((prop & SIGPROP_CONT) == 0 &&
2954df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2955df8bae1dSRodney W. Grimes 				printf("issignal\n");
2956df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2957df8bae1dSRodney W. Grimes 
2958df8bae1dSRodney W. Grimes 		default:
2959df8bae1dSRodney W. Grimes 			/*
2960df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2961df8bae1dSRodney W. Grimes 			 * postsig() process it.
2962df8bae1dSRodney W. Grimes 			 */
29632c42a146SMarcel Moolenaar 			return (sig);
2964df8bae1dSRodney W. Grimes 		}
29659104847fSDavid Xu 		sigqueue_delete(&td->td_sigqueue, sig);	/* take the signal! */
29666b286ee8SKonstantin Belousov 		sigqueue_delete(&p->p_sigqueue, sig);
2967b7a25e63SKonstantin Belousov next:;
2968df8bae1dSRodney W. Grimes 	}
2969df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2970df8bae1dSRodney W. Grimes }
2971df8bae1dSRodney W. Grimes 
2972e574e444SDavid Xu void
2973e574e444SDavid Xu thread_stopped(struct proc *p)
2974e574e444SDavid Xu {
2975e574e444SDavid Xu 	int n;
2976e574e444SDavid Xu 
2977e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
29787b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
2979e574e444SDavid Xu 	n = p->p_suspcount;
29807c9a98f1SDavid Xu 	if (p == curproc)
2981e574e444SDavid Xu 		n++;
2982e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
29837b4a950aSDavid Xu 		PROC_SUNLOCK(p);
2984407948a5SDavid Xu 		p->p_flag &= ~P_WAITED;
2985e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
29867f96995eSDavid Xu 		childproc_stopped(p, (p->p_flag & P_TRACED) ?
2987ebceaf6dSDavid Xu 			CLD_TRAPPED : CLD_STOPPED);
2988e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
29897b4a950aSDavid Xu 		PROC_SLOCK(p);
2990e574e444SDavid Xu 	}
2991e574e444SDavid Xu }
2992e574e444SDavid Xu 
2993df8bae1dSRodney W. Grimes /*
2994df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2995df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2996df8bae1dSRodney W. Grimes  */
299775c586a4SKonstantin Belousov int
29980167b33bSKonstantin Belousov postsig(int sig)
2999df8bae1dSRodney W. Grimes {
30000167b33bSKonstantin Belousov 	struct thread *td;
30010167b33bSKonstantin Belousov 	struct proc *p;
3002628d2653SJohn Baldwin 	struct sigacts *ps;
30032c42a146SMarcel Moolenaar 	sig_t action;
30049104847fSDavid Xu 	ksiginfo_t ksi;
3005e442f29fSKonstantin Belousov 	sigset_t returnmask;
3006df8bae1dSRodney W. Grimes 
30072c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
30085526d2d9SEivind Eklund 
30090167b33bSKonstantin Belousov 	td = curthread;
30100167b33bSKonstantin Belousov 	p = td->td_proc;
30112ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3012628d2653SJohn Baldwin 	ps = p->p_sigacts;
301390af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
30145da49fcbSDavid Xu 	ksiginfo_init(&ksi);
30156b286ee8SKonstantin Belousov 	if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
30166b286ee8SKonstantin Belousov 	    sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
301775c586a4SKonstantin Belousov 		return (0);
30189104847fSDavid Xu 	ksi.ksi_signo = sig;
301956c06c4bSDavid Xu 	if (ksi.ksi_code == SI_TIMER)
302056c06c4bSDavid Xu 		itimer_accept(p, ksi.ksi_timerid, &ksi);
30212c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
3022df8bae1dSRodney W. Grimes #ifdef KTRACE
3023374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
30245e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
302561009552SJilles Tjoelker 		    &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3026df8bae1dSRodney W. Grimes #endif
30270167b33bSKonstantin Belousov 	if ((p->p_stops & S_SIG) != 0) {
3028047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
3029047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
3030047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
3031047aa39bSRobert Watson 	}
30322a024a2bSSean Eric Fagan 
30338460a577SJohn Birrell 	if (action == SIG_DFL) {
3034df8bae1dSRodney W. Grimes 		/*
3035df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
3036df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
3037df8bae1dSRodney W. Grimes 		 */
303890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
303986be94fcSTycho Nightingale 		proc_td_siginfo_capture(td, &ksi.ksi_info);
3040b40ce416SJulian Elischer 		sigexit(td, sig);
3041df8bae1dSRodney W. Grimes 		/* NOTREACHED */
3042df8bae1dSRodney W. Grimes 	} else {
3043df8bae1dSRodney W. Grimes 		/*
3044df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
3045df8bae1dSRodney W. Grimes 		 */
3046cd735d8fSKonstantin Belousov 		KASSERT(action != SIG_IGN, ("postsig action %p", action));
3047cd735d8fSKonstantin Belousov 		KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3048cd735d8fSKonstantin Belousov 		    ("postsig action: blocked sig %d", sig));
3049cd735d8fSKonstantin Belousov 
3050df8bae1dSRodney W. Grimes 		/*
3051df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
3052645682fdSLuoqi Chen 		 * occurrences of this signal.
3053df8bae1dSRodney W. Grimes 		 *
3054645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
3055df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
3056645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
3057df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
3058df8bae1dSRodney W. Grimes 		 */
30595e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
30604093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
30615e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
3062df8bae1dSRodney W. Grimes 		} else
30634093529dSJeff Roberson 			returnmask = td->td_sigmask;
30642c42a146SMarcel Moolenaar 
3065c90c9021SEd Schouten 		if (p->p_sig == sig) {
30666626c604SJulian Elischer 			p->p_code = 0;
30676626c604SJulian Elischer 			p->p_sig = 0;
3068df8bae1dSRodney W. Grimes 		}
30699104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3070e442f29fSKonstantin Belousov 		postsig_done(sig, td, ps);
3071df8bae1dSRodney W. Grimes 	}
307275c586a4SKonstantin Belousov 	return (1);
3073df8bae1dSRodney W. Grimes }
3074df8bae1dSRodney W. Grimes 
3075df8bae1dSRodney W. Grimes /*
3076df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
3077df8bae1dSRodney W. Grimes  */
307826f9a767SRodney W. Grimes void
3079e052a8b9SEd Maste killproc(struct proc *p, char *why)
3080df8bae1dSRodney W. Grimes {
30819081e5e8SJohn Baldwin 
30829081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3083c3209846SPawel Jakub Dawidek 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3084c3209846SPawel Jakub Dawidek 	    p->p_comm);
3085c3209846SPawel Jakub Dawidek 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid,
3086c3209846SPawel Jakub Dawidek 	    p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why);
30873f1c4c4fSKonstantin Belousov 	p->p_flag |= P_WKILLED;
30888451d0ddSKip Macy 	kern_psignal(p, SIGKILL);
3089df8bae1dSRodney W. Grimes }
3090df8bae1dSRodney W. Grimes 
3091df8bae1dSRodney W. Grimes /*
3092df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
3093df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
3094df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
3095df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
3096df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
3097df8bae1dSRodney W. Grimes  * does not return.
3098df8bae1dSRodney W. Grimes  */
309926f9a767SRodney W. Grimes void
3100e052a8b9SEd Maste sigexit(struct thread *td, int sig)
3101df8bae1dSRodney W. Grimes {
3102b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3103df8bae1dSRodney W. Grimes 
3104628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3105df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
3106f97c3df1SDavid Schultz 	/*
3107f97c3df1SDavid Schultz 	 * We must be single-threading to generate a core dump.  This
3108f97c3df1SDavid Schultz 	 * ensures that the registers in the core file are up-to-date.
3109f97c3df1SDavid Schultz 	 * Also, the ELF dump handler assumes that the thread list doesn't
3110f97c3df1SDavid Schultz 	 * change out from under it.
3111f97c3df1SDavid Schultz 	 *
3112f97c3df1SDavid Schultz 	 * XXX If another thread attempts to single-thread before us
3113f97c3df1SDavid Schultz 	 *     (e.g. via fork()), we won't get a dump at all.
3114f97c3df1SDavid Schultz 	 */
3115fd50a707SBrooks Davis 	if ((sigprop(sig) & SIGPROP_CORE) &&
3116fd50a707SBrooks Davis 	    thread_single(p, SINGLE_NO_EXIT) == 0) {
31172c42a146SMarcel Moolenaar 		p->p_sig = sig;
3118c364e17eSAndrey A. Chernov 		/*
3119c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
3120c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
3121c364e17eSAndrey A. Chernov 		 * these messages.)
3122c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
31234ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
3124c364e17eSAndrey A. Chernov 		 */
3125b40ce416SJulian Elischer 		if (coredump(td) == 0)
31262c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
312757308494SJoerg Wunsch 		if (kern_logsigexit)
312857308494SJoerg Wunsch 			log(LOG_INFO,
312957308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
31303d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
31319c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
31322c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
31332c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
31344ae89b95SJohn Baldwin 	} else
3135628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3136b4490c6eSKonstantin Belousov 	exit1(td, 0, sig);
3137df8bae1dSRodney W. Grimes 	/* NOTREACHED */
3138df8bae1dSRodney W. Grimes }
3139df8bae1dSRodney W. Grimes 
3140ebceaf6dSDavid Xu /*
31417f96995eSDavid Xu  * Send queued SIGCHLD to parent when child process's state
31427f96995eSDavid Xu  * is changed.
3143ebceaf6dSDavid Xu  */
31447f96995eSDavid Xu static void
31457f96995eSDavid Xu sigparent(struct proc *p, int reason, int status)
3146ebceaf6dSDavid Xu {
3147ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
3148ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3149ebceaf6dSDavid Xu 
3150ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
3151ebceaf6dSDavid Xu 		p->p_ksi->ksi_signo  = SIGCHLD;
3152ebceaf6dSDavid Xu 		p->p_ksi->ksi_code   = reason;
31537f96995eSDavid Xu 		p->p_ksi->ksi_status = status;
3154ebceaf6dSDavid Xu 		p->p_ksi->ksi_pid    = p->p_pid;
3155ebceaf6dSDavid Xu 		p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
3156ebceaf6dSDavid Xu 		if (KSI_ONQ(p->p_ksi))
3157ebceaf6dSDavid Xu 			return;
3158ebceaf6dSDavid Xu 	}
3159ad6eec7bSJohn Baldwin 	pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3160ebceaf6dSDavid Xu }
3161ebceaf6dSDavid Xu 
31627f96995eSDavid Xu static void
3163b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig)
31647f96995eSDavid Xu {
31657f96995eSDavid Xu 	struct sigacts *ps;
31667f96995eSDavid Xu 
31677f96995eSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
31687f96995eSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
31697f96995eSDavid Xu 
31707f96995eSDavid Xu 	/*
31717f96995eSDavid Xu 	 * Wake up parent sleeping in kern_wait(), also send
31727f96995eSDavid Xu 	 * SIGCHLD to parent, but SIGCHLD does not guarantee
31737f96995eSDavid Xu 	 * that parent will awake, because parent may masked
31747f96995eSDavid Xu 	 * the signal.
31757f96995eSDavid Xu 	 */
31767f96995eSDavid Xu 	p->p_pptr->p_flag |= P_STATCHILD;
31777f96995eSDavid Xu 	wakeup(p->p_pptr);
31787f96995eSDavid Xu 
31797f96995eSDavid Xu 	ps = p->p_pptr->p_sigacts;
31807f96995eSDavid Xu 	mtx_lock(&ps->ps_mtx);
31817f96995eSDavid Xu 	if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
31827f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
3183b20a9aa9SJilles Tjoelker 		sigparent(p, reason, sig);
31847f96995eSDavid Xu 	} else
31857f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
31867f96995eSDavid Xu }
31877f96995eSDavid Xu 
31887f96995eSDavid Xu void
31897f96995eSDavid Xu childproc_stopped(struct proc *p, int reason)
31907f96995eSDavid Xu {
3191b4490c6eSKonstantin Belousov 
3192b4490c6eSKonstantin Belousov 	childproc_jobstate(p, reason, p->p_xsig);
31937f96995eSDavid Xu }
31947f96995eSDavid Xu 
3195ebceaf6dSDavid Xu void
3196ebceaf6dSDavid Xu childproc_continued(struct proc *p)
3197ebceaf6dSDavid Xu {
31987f96995eSDavid Xu 	childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3199ebceaf6dSDavid Xu }
3200ebceaf6dSDavid Xu 
3201ebceaf6dSDavid Xu void
3202ebceaf6dSDavid Xu childproc_exited(struct proc *p)
3203ebceaf6dSDavid Xu {
3204b4490c6eSKonstantin Belousov 	int reason, status;
3205ebceaf6dSDavid Xu 
3206b4490c6eSKonstantin Belousov 	if (WCOREDUMP(p->p_xsig)) {
3207b4490c6eSKonstantin Belousov 		reason = CLD_DUMPED;
3208b4490c6eSKonstantin Belousov 		status = WTERMSIG(p->p_xsig);
3209b4490c6eSKonstantin Belousov 	} else if (WIFSIGNALED(p->p_xsig)) {
3210b4490c6eSKonstantin Belousov 		reason = CLD_KILLED;
3211b4490c6eSKonstantin Belousov 		status = WTERMSIG(p->p_xsig);
3212b4490c6eSKonstantin Belousov 	} else {
3213b4490c6eSKonstantin Belousov 		reason = CLD_EXITED;
3214b4490c6eSKonstantin Belousov 		status = p->p_xexit;
3215b4490c6eSKonstantin Belousov 	}
32167f96995eSDavid Xu 	/*
32177f96995eSDavid Xu 	 * XXX avoid calling wakeup(p->p_pptr), the work is
32187f96995eSDavid Xu 	 * done in exit1().
32197f96995eSDavid Xu 	 */
32207f96995eSDavid Xu 	sigparent(p, reason, status);
3221ebceaf6dSDavid Xu }
3222ebceaf6dSDavid Xu 
3223e7228204SAlfred Perlstein /*
3224e7228204SAlfred Perlstein  * We only have 1 character for the core count in the format
3225e7228204SAlfred Perlstein  * string, so the range will be 0-9
3226e7228204SAlfred Perlstein  */
3227cc37baeaSStephen J. Kiernan #define	MAX_NUM_CORE_FILES 10
3228cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES
3229cc37baeaSStephen J. Kiernan #define	NUM_CORE_FILES 5
3230cc37baeaSStephen J. Kiernan #endif
3231cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3232cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES;
3233e7228204SAlfred Perlstein 
3234e7228204SAlfred Perlstein static int
3235e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3236e7228204SAlfred Perlstein {
3237e7228204SAlfred Perlstein 	int error;
3238e7228204SAlfred Perlstein 	int new_val;
3239e7228204SAlfred Perlstein 
3240c5105012SKonstantin Belousov 	new_val = num_cores;
3241e7228204SAlfred Perlstein 	error = sysctl_handle_int(oidp, &new_val, 0, req);
3242e7228204SAlfred Perlstein 	if (error != 0 || req->newptr == NULL)
3243e7228204SAlfred Perlstein 		return (error);
3244cc37baeaSStephen J. Kiernan 	if (new_val > MAX_NUM_CORE_FILES)
3245cc37baeaSStephen J. Kiernan 		new_val = MAX_NUM_CORE_FILES;
3246e7228204SAlfred Perlstein 	if (new_val < 0)
3247e7228204SAlfred Perlstein 		new_val = 0;
3248e7228204SAlfred Perlstein 	num_cores = new_val;
3249e7228204SAlfred Perlstein 	return (0);
3250e7228204SAlfred Perlstein }
3251e7228204SAlfred Perlstein SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
3252e7228204SAlfred Perlstein 	    0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
3253e7228204SAlfred Perlstein 
3254aa14e9b7SMark Johnston #define	GZ_SUFFIX	".gz"
3255aa14e9b7SMark Johnston 
3256aa14e9b7SMark Johnston #ifdef GZIO
3257aa14e9b7SMark Johnston static int compress_user_cores = 1;
3258aa14e9b7SMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RWTUN,
325929146f1aSPawel Jakub Dawidek     &compress_user_cores, 0, "Compression of user corefiles");
3260e7228204SAlfred Perlstein 
3261aa14e9b7SMark Johnston int compress_user_cores_gzlevel = 6;
3262aa14e9b7SMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RWTUN,
3263aa14e9b7SMark Johnston     &compress_user_cores_gzlevel, 0, "Corefile gzip compression level");
3264aa14e9b7SMark Johnston #else
3265aa14e9b7SMark Johnston static int compress_user_cores = 0;
3266e7228204SAlfred Perlstein #endif
3267e7228204SAlfred Perlstein 
32685bc0ff88SMateusz Guzik /*
32695bc0ff88SMateusz Guzik  * Protect the access to corefilename[] by allproc_lock.
32705bc0ff88SMateusz Guzik  */
32715bc0ff88SMateusz Guzik #define	corefilename_lock	allproc_lock
32725bc0ff88SMateusz Guzik 
32736d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"};
3274fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
32755bc0ff88SMateusz Guzik 
32765bc0ff88SMateusz Guzik static int
32775bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
32785bc0ff88SMateusz Guzik {
32795bc0ff88SMateusz Guzik 	int error;
32805bc0ff88SMateusz Guzik 
32815bc0ff88SMateusz Guzik 	sx_xlock(&corefilename_lock);
32825bc0ff88SMateusz Guzik 	error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
32835bc0ff88SMateusz Guzik 	    req);
32845bc0ff88SMateusz Guzik 	sx_xunlock(&corefilename_lock);
32855bc0ff88SMateusz Guzik 
32865bc0ff88SMateusz Guzik 	return (error);
32875bc0ff88SMateusz Guzik }
3288fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
32895bc0ff88SMateusz Guzik     CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
32905bc0ff88SMateusz Guzik     "Process corefile name format string");
3291c5edb423SSean Eric Fagan 
3292c5edb423SSean Eric Fagan /*
3293c345faeaSPawel Jakub Dawidek  * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3294c345faeaSPawel Jakub Dawidek  * Expand the name described in corefilename, using name, uid, and pid
3295c345faeaSPawel Jakub Dawidek  * and open/create core file.
3296c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
3297c5edb423SSean Eric Fagan  *	%N	name of process ("name")
3298c5edb423SSean Eric Fagan  *	%P	process id (pid)
3299c5edb423SSean Eric Fagan  *	%U	user id (uid)
3300c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
3301c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3302c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
3303c5edb423SSean Eric Fagan  */
3304c345faeaSPawel Jakub Dawidek static int
3305c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3306c345faeaSPawel Jakub Dawidek     int compress, struct vnode **vpp, char **namep)
33078b43b535SAlfred Perlstein {
3308c345faeaSPawel Jakub Dawidek 	struct nameidata nd;
330936b208e0SRobert Watson 	struct sbuf sb;
331036b208e0SRobert Watson 	const char *format;
3311c345faeaSPawel Jakub Dawidek 	char *hostname, *name;
3312c345faeaSPawel Jakub Dawidek 	int indexpos, i, error, cmode, flags, oflags;
3313c5edb423SSean Eric Fagan 
3314b7402d82SAlfred Perlstein 	hostname = NULL;
33158b43b535SAlfred Perlstein 	format = corefilename;
3316086053a3SPawel Jakub Dawidek 	name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3317e7228204SAlfred Perlstein 	indexpos = -1;
3318c52ff611SPawel Jakub Dawidek 	(void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
33195bc0ff88SMateusz Guzik 	sx_slock(&corefilename_lock);
332029146f1aSPawel Jakub Dawidek 	for (i = 0; format[i] != '\0'; i++) {
3321c5edb423SSean Eric Fagan 		switch (format[i]) {
3322c5edb423SSean Eric Fagan 		case '%':	/* Format character */
3323c5edb423SSean Eric Fagan 			i++;
3324c5edb423SSean Eric Fagan 			switch (format[i]) {
3325c5edb423SSean Eric Fagan 			case '%':
332636b208e0SRobert Watson 				sbuf_putc(&sb, '%');
3327c5edb423SSean Eric Fagan 				break;
3328e7228204SAlfred Perlstein 			case 'H':	/* hostname */
3329b7402d82SAlfred Perlstein 				if (hostname == NULL) {
3330b7402d82SAlfred Perlstein 					hostname = malloc(MAXHOSTNAMELEN,
3331086053a3SPawel Jakub Dawidek 					    M_TEMP, M_WAITOK);
3332b7402d82SAlfred Perlstein 				}
3333e7228204SAlfred Perlstein 				getcredhostname(td->td_ucred, hostname,
3334b7402d82SAlfred Perlstein 				    MAXHOSTNAMELEN);
3335e7228204SAlfred Perlstein 				sbuf_printf(&sb, "%s", hostname);
3336e7228204SAlfred Perlstein 				break;
3337e7228204SAlfred Perlstein 			case 'I':	/* autoincrementing index */
3338e7228204SAlfred Perlstein 				sbuf_printf(&sb, "0");
3339e7228204SAlfred Perlstein 				indexpos = sbuf_len(&sb) - 1;
3340e7228204SAlfred Perlstein 				break;
3341c5edb423SSean Eric Fagan 			case 'N':	/* process name */
3342c52ff611SPawel Jakub Dawidek 				sbuf_printf(&sb, "%s", comm);
3343c5edb423SSean Eric Fagan 				break;
3344c5edb423SSean Eric Fagan 			case 'P':	/* process id */
334536b208e0SRobert Watson 				sbuf_printf(&sb, "%u", pid);
3346c5edb423SSean Eric Fagan 				break;
3347c5edb423SSean Eric Fagan 			case 'U':	/* user id */
334836b208e0SRobert Watson 				sbuf_printf(&sb, "%u", uid);
3349c5edb423SSean Eric Fagan 				break;
3350c5edb423SSean Eric Fagan 			default:
33518b43b535SAlfred Perlstein 				log(LOG_ERR,
335236b208e0SRobert Watson 				    "Unknown format character %c in "
335336b208e0SRobert Watson 				    "corename `%s'\n", format[i], format);
335429146f1aSPawel Jakub Dawidek 				break;
3355c5edb423SSean Eric Fagan 			}
3356c5edb423SSean Eric Fagan 			break;
3357c5edb423SSean Eric Fagan 		default:
335836b208e0SRobert Watson 			sbuf_putc(&sb, format[i]);
33596c08be2bSPawel Jakub Dawidek 			break;
3360c5edb423SSean Eric Fagan 		}
3361c5edb423SSean Eric Fagan 	}
33625bc0ff88SMateusz Guzik 	sx_sunlock(&corefilename_lock);
3363b7402d82SAlfred Perlstein 	free(hostname, M_TEMP);
3364f06f465dSPawel Jakub Dawidek 	if (compress)
3365e7228204SAlfred Perlstein 		sbuf_printf(&sb, GZ_SUFFIX);
33664d369413SMatthew D Fleming 	if (sbuf_error(&sb) != 0) {
336736b208e0SRobert Watson 		log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3368c52ff611SPawel Jakub Dawidek 		    "long\n", (long)pid, comm, (u_long)uid);
3369b7402d82SAlfred Perlstein 		sbuf_delete(&sb);
3370c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
3371c345faeaSPawel Jakub Dawidek 		return (ENOMEM);
3372c5edb423SSean Eric Fagan 	}
337336b208e0SRobert Watson 	sbuf_finish(&sb);
337436b208e0SRobert Watson 	sbuf_delete(&sb);
3375e7228204SAlfred Perlstein 
3376c345faeaSPawel Jakub Dawidek 	cmode = S_IRUSR | S_IWUSR;
33778ee9765aSKonstantin Belousov 	oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
33788ee9765aSKonstantin Belousov 	    (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3379c345faeaSPawel Jakub Dawidek 
3380e7228204SAlfred Perlstein 	/*
3381e7228204SAlfred Perlstein 	 * If the core format has a %I in it, then we need to check
3382e7228204SAlfred Perlstein 	 * for existing corefiles before returning a name.
3383e7228204SAlfred Perlstein 	 * To do this we iterate over 0..num_cores to find a
3384e7228204SAlfred Perlstein 	 * non-existing core file name to use.
3385e7228204SAlfred Perlstein 	 */
3386e7228204SAlfred Perlstein 	if (indexpos != -1) {
338729146f1aSPawel Jakub Dawidek 		for (i = 0; i < num_cores; i++) {
338807a8e078SPawel Jakub Dawidek 			flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
338929146f1aSPawel Jakub Dawidek 			name[indexpos] = '0' + i;
3390c345faeaSPawel Jakub Dawidek 			NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3391b0c9d4d7SPawel Jakub Dawidek 			error = vn_open_cred(&nd, &flags, cmode, oflags,
3392b0c9d4d7SPawel Jakub Dawidek 			    td->td_ucred, NULL);
3393e7228204SAlfred Perlstein 			if (error) {
339423c6445aSPawel Jakub Dawidek 				if (error == EEXIST)
3395e7228204SAlfred Perlstein 					continue;
3396e7228204SAlfred Perlstein 				log(LOG_ERR,
3397e7228204SAlfred Perlstein 				    "pid %d (%s), uid (%u):  Path `%s' failed "
3398e7228204SAlfred Perlstein 				    "on initial open test, error = %d\n",
3399c52ff611SPawel Jakub Dawidek 				    pid, comm, uid, name, error);
3400c345faeaSPawel Jakub Dawidek 			}
3401c345faeaSPawel Jakub Dawidek 			goto out;
3402c345faeaSPawel Jakub Dawidek 		}
3403c345faeaSPawel Jakub Dawidek 	}
3404c345faeaSPawel Jakub Dawidek 
3405c345faeaSPawel Jakub Dawidek 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
3406c345faeaSPawel Jakub Dawidek 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3407c345faeaSPawel Jakub Dawidek 	error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL);
3408c345faeaSPawel Jakub Dawidek out:
3409c345faeaSPawel Jakub Dawidek 	if (error) {
3410c345faeaSPawel Jakub Dawidek #ifdef AUDIT
3411c345faeaSPawel Jakub Dawidek 		audit_proc_coredump(td, name, error);
3412c345faeaSPawel Jakub Dawidek #endif
3413c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
3414c345faeaSPawel Jakub Dawidek 		return (error);
3415e7228204SAlfred Perlstein 	}
3416e7228204SAlfred Perlstein 	NDFREE(&nd, NDF_ONLY_PNBUF);
3417c345faeaSPawel Jakub Dawidek 	*vpp = nd.ni_vp;
3418c345faeaSPawel Jakub Dawidek 	*namep = name;
3419c345faeaSPawel Jakub Dawidek 	return (0);
342036b208e0SRobert Watson }
3421c5edb423SSean Eric Fagan 
3422eb6368d4SRui Paulo static int
3423eb6368d4SRui Paulo coredump_sanitise_path(const char *path)
3424eb6368d4SRui Paulo {
3425624157bbSRui Paulo 	size_t i;
3426eb6368d4SRui Paulo 
3427eb6368d4SRui Paulo 	/*
3428eb6368d4SRui Paulo 	 * Only send a subset of ASCII to devd(8) because it
3429eb6368d4SRui Paulo 	 * might pass these strings to sh -c.
3430eb6368d4SRui Paulo 	 */
3431624157bbSRui Paulo 	for (i = 0; path[i]; i++)
3432eb6368d4SRui Paulo 		if (!(isalpha(path[i]) || isdigit(path[i])) &&
3433eb6368d4SRui Paulo 		    path[i] != '/' && path[i] != '.' &&
3434eb6368d4SRui Paulo 		    path[i] != '-')
3435eb6368d4SRui Paulo 			return (0);
3436eb6368d4SRui Paulo 
3437eb6368d4SRui Paulo 	return (1);
3438eb6368d4SRui Paulo }
3439eb6368d4SRui Paulo 
3440df8bae1dSRodney W. Grimes /*
3441fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
3442fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
3443fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
3444fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
3445fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
3446fca666a1SJulian Elischer  */
3447fca666a1SJulian Elischer 
3448fca666a1SJulian Elischer static int
3449b40ce416SJulian Elischer coredump(struct thread *td)
3450fca666a1SJulian Elischer {
3451b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3452f06f465dSPawel Jakub Dawidek 	struct ucred *cred = td->td_ucred;
3453f06f465dSPawel Jakub Dawidek 	struct vnode *vp;
345406ae1e91SMatthew Dillon 	struct flock lf;
3455fca666a1SJulian Elischer 	struct vattr vattr;
3456c345faeaSPawel Jakub Dawidek 	int error, error1, locked;
3457fca666a1SJulian Elischer 	char *name;			/* name of corefile */
3458539c9eefSKonstantin Belousov 	void *rl_cookie;
3459fca666a1SJulian Elischer 	off_t limit;
34606fbc0f7dSRui Paulo 	char *data = NULL;
3461842ab62bSRui Paulo 	char *fullpath, *freepath = NULL;
3462eb6368d4SRui Paulo 	size_t len;
34636fbc0f7dSRui Paulo 	static const char comm_name[] = "comm=";
34646fbc0f7dSRui Paulo 	static const char core_name[] = "core=";
3465fca666a1SJulian Elischer 
34664ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3467f97c3df1SDavid Schultz 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3468628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
3469fca666a1SJulian Elischer 
3470677258f7SKonstantin Belousov 	if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3471677258f7SKonstantin Belousov 	    (p->p_flag2 & P2_NOTRACE) != 0) {
3472628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3473fca666a1SJulian Elischer 		return (EFAULT);
3474628d2653SJohn Baldwin 	}
3475fca666a1SJulian Elischer 
3476fca666a1SJulian Elischer 	/*
347735a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
347835a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
347935a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
348035a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
348135a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
348235a2598fSSean Eric Fagan 	 * if it is larger than the limit.
3483fca666a1SJulian Elischer 	 */
3484f6f6d240SMateusz Guzik 	limit = (off_t)lim_cur(td, RLIMIT_CORE);
3485b8fdb0d9SEdward Tomasz Napierala 	if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3486628d2653SJohn Baldwin 		PROC_UNLOCK(p);
348791d5354aSJohn Baldwin 		return (EFBIG);
348857274c51SChristian S.J. Peron 	}
3489b8fdb0d9SEdward Tomasz Napierala 	PROC_UNLOCK(p);
349035a2598fSSean Eric Fagan 
3491aa14e9b7SMark Johnston 	error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
3492aa14e9b7SMark Johnston 	    compress_user_cores, &vp, &name);
3493c345faeaSPawel Jakub Dawidek 	if (error != 0)
3494fca666a1SJulian Elischer 		return (error);
349506ae1e91SMatthew Dillon 
3496539c9eefSKonstantin Belousov 	/*
3497539c9eefSKonstantin Belousov 	 * Don't dump to non-regular files or files with links.
3498539c9eefSKonstantin Belousov 	 * Do not dump into system files.
3499539c9eefSKonstantin Belousov 	 */
3500f06f465dSPawel Jakub Dawidek 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3501539c9eefSKonstantin Belousov 	    vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0) {
350222db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
3503832dafadSDon Lewis 		error = EFAULT;
3504dacbc9dbSKonstantin Belousov 		goto out;
3505832dafadSDon Lewis 	}
3506832dafadSDon Lewis 
350722db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
3508539c9eefSKonstantin Belousov 
3509539c9eefSKonstantin Belousov 	/* Postpone other writers, including core dumps of other processes. */
3510539c9eefSKonstantin Belousov 	rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3511539c9eefSKonstantin Belousov 
351206ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
351306ae1e91SMatthew Dillon 	lf.l_start = 0;
351406ae1e91SMatthew Dillon 	lf.l_len = 0;
351506ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
3516c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
351706ae1e91SMatthew Dillon 
3518fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
3519fca666a1SJulian Elischer 	vattr.va_size = 0;
35206141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
35216141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
3522cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
35230359a12eSAttilio Rao 	VOP_SETATTR(vp, &vattr, cred);
352422db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
3525628d2653SJohn Baldwin 	PROC_LOCK(p);
3526fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
3527628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3528fca666a1SJulian Elischer 
352923c6445aSPawel Jakub Dawidek 	if (p->p_sysent->sv_coredump != NULL) {
353023c6445aSPawel Jakub Dawidek 		error = p->p_sysent->sv_coredump(td, vp, limit,
3531aa14e9b7SMark Johnston 		    compress_user_cores ? IMGACT_CORE_COMPRESS : 0);
353223c6445aSPawel Jakub Dawidek 	} else {
353323c6445aSPawel Jakub Dawidek 		error = ENOSYS;
353423c6445aSPawel Jakub Dawidek 	}
3535fca666a1SJulian Elischer 
3536c447f5b2SRobert Watson 	if (locked) {
353706ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
353806ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3539c447f5b2SRobert Watson 	}
3540539c9eefSKonstantin Belousov 	vn_rangelock_unlock(vp, rl_cookie);
3541dacbc9dbSKonstantin Belousov 
3542842ab62bSRui Paulo 	/*
3543842ab62bSRui Paulo 	 * Notify the userland helper that a process triggered a core dump.
3544842ab62bSRui Paulo 	 * This allows the helper to run an automated debugging session.
3545842ab62bSRui Paulo 	 */
3546dacbc9dbSKonstantin Belousov 	if (error != 0 || coredump_devctl == 0)
3547842ab62bSRui Paulo 		goto out;
35486fbc0f7dSRui Paulo 	len = MAXPATHLEN * 2 + sizeof(comm_name) - 1 +
35496fbc0f7dSRui Paulo 	    sizeof(' ') + sizeof(core_name) - 1;
35506fbc0f7dSRui Paulo 	data = malloc(len, M_TEMP, M_WAITOK);
3551842ab62bSRui Paulo 	if (vn_fullpath_global(td, p->p_textvp, &fullpath, &freepath) != 0)
3552842ab62bSRui Paulo 		goto out;
3553eb6368d4SRui Paulo 	if (!coredump_sanitise_path(fullpath))
3554eb6368d4SRui Paulo 		goto out;
35556fbc0f7dSRui Paulo 	snprintf(data, len, "%s%s ", comm_name, fullpath);
3556842ab62bSRui Paulo 	free(freepath, M_TEMP);
3557842ab62bSRui Paulo 	freepath = NULL;
3558624157bbSRui Paulo 	if (vn_fullpath_global(td, vp, &fullpath, &freepath) != 0)
3559842ab62bSRui Paulo 		goto out;
3560eb6368d4SRui Paulo 	if (!coredump_sanitise_path(fullpath))
3561eb6368d4SRui Paulo 		goto out;
35626fbc0f7dSRui Paulo 	strlcat(data, core_name, len);
35636fbc0f7dSRui Paulo 	strlcat(data, fullpath, len);
3564842ab62bSRui Paulo 	devctl_notify("kernel", "signal", "coredump", data);
3565842ab62bSRui Paulo out:
3566dacbc9dbSKonstantin Belousov 	error1 = vn_close(vp, FWRITE, cred, td);
3567dacbc9dbSKonstantin Belousov 	if (error == 0)
3568dacbc9dbSKonstantin Belousov 		error = error1;
356957274c51SChristian S.J. Peron #ifdef AUDIT
357057274c51SChristian S.J. Peron 	audit_proc_coredump(td, name, error);
357157274c51SChristian S.J. Peron #endif
3572842ab62bSRui Paulo 	free(freepath, M_TEMP);
35736fbc0f7dSRui Paulo 	free(data, M_TEMP);
357457274c51SChristian S.J. Peron 	free(name, M_TEMP);
3575fca666a1SJulian Elischer 	return (error);
3576fca666a1SJulian Elischer }
3577fca666a1SJulian Elischer 
3578fca666a1SJulian Elischer /*
35790c14ff0eSRobert Watson  * Nonexistent system call-- signal process (may want to handle it).  Flag
35800c14ff0eSRobert Watson  * error in case process won't see signal immediately (blocked or ignored).
3581df8bae1dSRodney W. Grimes  */
3582d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
3583df8bae1dSRodney W. Grimes struct nosys_args {
3584df8bae1dSRodney W. Grimes 	int	dummy;
3585df8bae1dSRodney W. Grimes };
3586d2d3e875SBruce Evans #endif
3587df8bae1dSRodney W. Grimes /* ARGSUSED */
358826f9a767SRodney W. Grimes int
3589e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args)
3590df8bae1dSRodney W. Grimes {
3591f5a077c3SKonstantin Belousov 	struct proc *p;
3592f5a077c3SKonstantin Belousov 
3593f5a077c3SKonstantin Belousov 	p = td->td_proc;
3594b40ce416SJulian Elischer 
3595628d2653SJohn Baldwin 	PROC_LOCK(p);
3596888aefefSKonstantin Belousov 	tdsignal(td, SIGSYS);
3597628d2653SJohn Baldwin 	PROC_UNLOCK(p);
35987ceeb35bSKonstantin Belousov 	if (kern_lognosys == 1 || kern_lognosys == 3) {
3599f5a077c3SKonstantin Belousov 		uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3600f5a077c3SKonstantin Belousov 		    td->td_sa.code);
36017ceeb35bSKonstantin Belousov 	}
36027ceeb35bSKonstantin Belousov 	if (kern_lognosys == 2 || kern_lognosys == 3) {
36037ceeb35bSKonstantin Belousov 		printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
36047ceeb35bSKonstantin Belousov 		    td->td_sa.code);
36057ceeb35bSKonstantin Belousov 	}
3606f5216b9aSBruce Evans 	return (ENOSYS);
3607df8bae1dSRodney W. Grimes }
3608831d27a9SDon Lewis 
3609831d27a9SDon Lewis /*
36100c14ff0eSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using stored
36110c14ff0eSRobert Watson  * credentials rather than those of the current process.
3612831d27a9SDon Lewis  */
3613831d27a9SDon Lewis void
3614e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty)
3615831d27a9SDon Lewis {
3616a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
3617f1320723SAlfred Perlstein 	struct sigio *sigio;
3618831d27a9SDon Lewis 
3619a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
3620a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
3621a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
3622a3de221dSKonstantin Belousov 
3623f1320723SAlfred Perlstein 	SIGIO_LOCK();
3624f1320723SAlfred Perlstein 	sigio = *sigiop;
3625f1320723SAlfred Perlstein 	if (sigio == NULL) {
3626f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
3627f1320723SAlfred Perlstein 		return;
3628f1320723SAlfred Perlstein 	}
3629831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
3630628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
36312b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
36328451d0ddSKip Macy 			kern_psignal(sigio->sio_proc, sig);
3633628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
3634831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
3635831d27a9SDon Lewis 		struct proc *p;
3636831d27a9SDon Lewis 
3637f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
3638628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3639628d2653SJohn Baldwin 			PROC_LOCK(p);
3640e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
3641e806d352SJohn Baldwin 			    CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3642831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
36438451d0ddSKip Macy 				kern_psignal(p, sig);
3644628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3645628d2653SJohn Baldwin 		}
3646f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
3647831d27a9SDon Lewis 	}
3648f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
3649831d27a9SDon Lewis }
3650cb679c38SJonathan Lemon 
3651cb679c38SJonathan Lemon static int
3652cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
3653cb679c38SJonathan Lemon {
3654cb679c38SJonathan Lemon 	struct proc *p = curproc;
3655cb679c38SJonathan Lemon 
3656cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
3657cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
3658cb679c38SJonathan Lemon 
36599e590ff0SKonstantin Belousov 	knlist_add(p->p_klist, kn, 0);
3660cb679c38SJonathan Lemon 
3661cb679c38SJonathan Lemon 	return (0);
3662cb679c38SJonathan Lemon }
3663cb679c38SJonathan Lemon 
3664cb679c38SJonathan Lemon static void
3665cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
3666cb679c38SJonathan Lemon {
3667cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
3668cb679c38SJonathan Lemon 
36699e590ff0SKonstantin Belousov 	knlist_remove(p->p_klist, kn, 0);
3670cb679c38SJonathan Lemon }
3671cb679c38SJonathan Lemon 
3672cb679c38SJonathan Lemon /*
3673cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
3674cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
3675cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
3676cb679c38SJonathan Lemon  * isn't worth the trouble.
3677cb679c38SJonathan Lemon  */
3678cb679c38SJonathan Lemon static int
3679cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
3680cb679c38SJonathan Lemon {
3681cb679c38SJonathan Lemon 
3682cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
3683cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
3684cb679c38SJonathan Lemon 
3685cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
3686cb679c38SJonathan Lemon 			kn->kn_data++;
3687cb679c38SJonathan Lemon 	}
3688cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
3689cb679c38SJonathan Lemon }
369090af4afaSJohn Baldwin 
369190af4afaSJohn Baldwin struct sigacts *
369290af4afaSJohn Baldwin sigacts_alloc(void)
369390af4afaSJohn Baldwin {
369490af4afaSJohn Baldwin 	struct sigacts *ps;
369590af4afaSJohn Baldwin 
369690af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3697ce8daaadSMateusz Guzik 	refcount_init(&ps->ps_refcnt, 1);
369890af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
369990af4afaSJohn Baldwin 	return (ps);
370090af4afaSJohn Baldwin }
370190af4afaSJohn Baldwin 
370290af4afaSJohn Baldwin void
370390af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
370490af4afaSJohn Baldwin {
370590af4afaSJohn Baldwin 
3706c959c237SMateusz Guzik 	if (refcount_release(&ps->ps_refcnt) == 0)
3707c959c237SMateusz Guzik 		return;
370890af4afaSJohn Baldwin 	mtx_destroy(&ps->ps_mtx);
370990af4afaSJohn Baldwin 	free(ps, M_SUBPROC);
371090af4afaSJohn Baldwin }
371190af4afaSJohn Baldwin 
371290af4afaSJohn Baldwin struct sigacts *
371390af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
371490af4afaSJohn Baldwin {
3715c959c237SMateusz Guzik 
3716c959c237SMateusz Guzik 	refcount_acquire(&ps->ps_refcnt);
371790af4afaSJohn Baldwin 	return (ps);
371890af4afaSJohn Baldwin }
371990af4afaSJohn Baldwin 
372090af4afaSJohn Baldwin void
372190af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
372290af4afaSJohn Baldwin {
372390af4afaSJohn Baldwin 
372490af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
372590af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
372690af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
372790af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
372890af4afaSJohn Baldwin }
372990af4afaSJohn Baldwin 
373090af4afaSJohn Baldwin int
373190af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
373290af4afaSJohn Baldwin {
373390af4afaSJohn Baldwin 
3734d00c8ea4SMateusz Guzik 	return (ps->ps_refcnt > 1);
373590af4afaSJohn Baldwin }
3736