xref: /freebsd/sys/kern/kern_sig.c (revision 7b77e1fe0f482838f8e0940ce57af648f8a6643d)
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.
18df8bae1dSRodney W. Grimes  * 4. 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"
415d217f17SJohn Birrell #include "opt_kdtrace.h"
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43e7228204SAlfred Perlstein #include "opt_core.h"
44cfb5f768SJonathan Anderson #include "opt_procdesc.h"
45db6a20e2SGarrett Wollman 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
4736240ea5SDoug Rabson #include <sys/systm.h>
48df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/vnode.h>
50df8bae1dSRodney W. Grimes #include <sys/acct.h>
51cfb5f768SJonathan Anderson #include <sys/capability.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>
62854dc8c2SJohn Baldwin #include <sys/namei.h>
63854dc8c2SJohn Baldwin #include <sys/proc.h>
64cfb5f768SJonathan Anderson #include <sys/procdesc.h>
656aeb05d7STom Rhodes #include <sys/posix4.h>
66854dc8c2SJohn Baldwin #include <sys/pioctl.h>
67b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h>
68c31146a1SJohn Baldwin #include <sys/resourcevar.h>
695d217f17SJohn Birrell #include <sys/sdt.h>
7036b208e0SRobert Watson #include <sys/sbuf.h>
7144f3b092SJohn Baldwin #include <sys/sleepqueue.h>
726caa8a15SJohn Baldwin #include <sys/smp.h>
73df8bae1dSRodney W. Grimes #include <sys/stat.h>
741005a129SJohn Baldwin #include <sys/sx.h>
758f19eb88SIan Dowse #include <sys/syscallsubr.h>
76c87e2930SDavid Greenman #include <sys/sysctl.h>
77854dc8c2SJohn Baldwin #include <sys/sysent.h>
78854dc8c2SJohn Baldwin #include <sys/syslog.h>
79854dc8c2SJohn Baldwin #include <sys/sysproto.h>
8056c06c4bSDavid Xu #include <sys/timers.h>
8106ae1e91SMatthew Dillon #include <sys/unistd.h>
82854dc8c2SJohn Baldwin #include <sys/wait.h>
839104847fSDavid Xu #include <vm/vm.h>
849104847fSDavid Xu #include <vm/vm_extern.h>
859104847fSDavid Xu #include <vm/uma.h>
86df8bae1dSRodney W. Grimes 
87e7228204SAlfred Perlstein #include <sys/jail.h>
88e7228204SAlfred Perlstein 
89df8bae1dSRodney W. Grimes #include <machine/cpu.h>
90df8bae1dSRodney W. Grimes 
91bfd7575aSWayne Salamon #include <security/audit/audit.h>
92bfd7575aSWayne Salamon 
936f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
946f841fb7SMarcel Moolenaar 
955d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
96*7b77e1feSMark Johnston SDT_PROBE_DEFINE3(proc, kernel, , signal_send, signal-send, "struct thread *",
97*7b77e1feSMark Johnston     "struct proc *", "int");
98*7b77e1feSMark Johnston SDT_PROBE_DEFINE2(proc, kernel, , signal_clear, signal-clear, "int",
99*7b77e1feSMark Johnston     "ksiginfo_t *");
100*7b77e1feSMark Johnston SDT_PROBE_DEFINE3(proc, kernel, , signal_discard, signal-discard,
101*7b77e1feSMark Johnston     "struct thread *", "struct proc *", "int");
1025d217f17SJohn Birrell 
1034d77a549SAlfred Perlstein static int	coredump(struct thread *);
104a3de221dSKonstantin Belousov static int	killpg1(struct thread *td, int sig, int pgid, int all,
105a3de221dSKonstantin Belousov 		    ksiginfo_t *ksi);
1063cf3b9f0SJohn Baldwin static int	issignal(struct thread *td);
1074d77a549SAlfred Perlstein static int	sigprop(int sig);
10894f0972bSDavid Xu static void	tdsigwakeup(struct thread *, int, sig_t, int);
109d8267df7SDavid Xu static void	sig_suspend_threads(struct thread *, struct proc *, int);
110cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
111cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
112cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
1134093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
1149104847fSDavid Xu static void	sigqueue_start(void);
115cb679c38SJonathan Lemon 
1169104847fSDavid Xu static uma_zone_t	ksiginfo_zone = NULL;
117e76d823bSRobert Watson struct filterops sig_filtops = {
118e76d823bSRobert Watson 	.f_isfd = 0,
119e76d823bSRobert Watson 	.f_attach = filt_sigattach,
120e76d823bSRobert Watson 	.f_detach = filt_sigdetach,
121e76d823bSRobert Watson 	.f_event = filt_signal,
122e76d823bSRobert Watson };
123cb679c38SJonathan Lemon 
124fc8cca02SJohn Baldwin static int	kern_logsigexit = 1;
1253d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1263d177f46SBill Fumerola     &kern_logsigexit, 0,
1273d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
12857308494SJoerg Wunsch 
129f71a882fSDavid Xu static int	kern_forcesigexit = 1;
130f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
131f71a882fSDavid Xu     &kern_forcesigexit, 0, "Force trap signal to be handled");
132f71a882fSDavid Xu 
1336472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
1346472ac3dSEd Schouten     "POSIX real time signal");
1359104847fSDavid Xu 
1369104847fSDavid Xu static int	max_pending_per_proc = 128;
1379104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
1389104847fSDavid Xu     &max_pending_per_proc, 0, "Max pending signals per proc");
1399104847fSDavid Xu 
1409104847fSDavid Xu static int	preallocate_siginfo = 1024;
1419104847fSDavid Xu TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
1429104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
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 
1539104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
1549104847fSDavid Xu 
1552b87b6d4SRobert Watson /*
1562b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1572b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1582b87b6d4SRobert Watson  * in the right situations.
1592b87b6d4SRobert Watson  */
1602b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1612b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1622b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1632b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1642b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1652b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1662b87b6d4SRobert Watson 
167fc8cca02SJohn Baldwin static int	sugid_coredump;
168b62d05fcSPawel Jakub Dawidek TUNABLE_INT("kern.sugid_coredump", &sugid_coredump);
1693d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1708b5adf9dSJoseph Koshy     &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
171c87e2930SDavid Greenman 
172b0c9d4d7SPawel Jakub Dawidek static int	capmode_coredump;
173b0c9d4d7SPawel Jakub Dawidek TUNABLE_INT("kern.capmode_coredump", &capmode_coredump);
174b0c9d4d7SPawel Jakub Dawidek SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RW,
175b0c9d4d7SPawel Jakub Dawidek     &capmode_coredump, 0, "Allow processes in capability mode to dump core");
176b0c9d4d7SPawel Jakub Dawidek 
177e5a28db9SPaul Saab static int	do_coredump = 1;
178e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
179e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
180e5a28db9SPaul Saab 
1816141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
1826141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
1836141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
1846141e04aSJohn-Mark Gurney 
1852c42a146SMarcel Moolenaar /*
1862c42a146SMarcel Moolenaar  * Signal properties and actions.
1872c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1882c42a146SMarcel Moolenaar  * according to the following properties:
1892c42a146SMarcel Moolenaar  */
1902c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1912c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1922c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1932c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1942c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1952c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1962c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
197df8bae1dSRodney W. Grimes 
1982c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
1998a8be776SJilles Tjoelker 	SA_KILL,			/* SIGHUP */
2008a8be776SJilles Tjoelker 	SA_KILL,			/* SIGINT */
2018a8be776SJilles Tjoelker 	SA_KILL|SA_CORE,		/* SIGQUIT */
2022c42a146SMarcel Moolenaar 	SA_KILL|SA_CORE,		/* SIGILL */
2032c42a146SMarcel Moolenaar 	SA_KILL|SA_CORE,		/* SIGTRAP */
2042c42a146SMarcel Moolenaar 	SA_KILL|SA_CORE,		/* SIGABRT */
2058a8be776SJilles Tjoelker 	SA_KILL|SA_CORE,		/* SIGEMT */
2062c42a146SMarcel Moolenaar 	SA_KILL|SA_CORE,		/* SIGFPE */
2078a8be776SJilles Tjoelker 	SA_KILL,			/* SIGKILL */
2082c42a146SMarcel Moolenaar 	SA_KILL|SA_CORE,		/* SIGBUS */
2092c42a146SMarcel Moolenaar 	SA_KILL|SA_CORE,		/* SIGSEGV */
2102c42a146SMarcel Moolenaar 	SA_KILL|SA_CORE,		/* SIGSYS */
2118a8be776SJilles Tjoelker 	SA_KILL,			/* SIGPIPE */
2128a8be776SJilles Tjoelker 	SA_KILL,			/* SIGALRM */
2138a8be776SJilles Tjoelker 	SA_KILL,			/* SIGTERM */
2148a8be776SJilles Tjoelker 	SA_IGNORE,			/* SIGURG */
2158a8be776SJilles Tjoelker 	SA_STOP,			/* SIGSTOP */
2168a8be776SJilles Tjoelker 	SA_STOP|SA_TTYSTOP,		/* SIGTSTP */
2178a8be776SJilles Tjoelker 	SA_IGNORE|SA_CONT,		/* SIGCONT */
2188a8be776SJilles Tjoelker 	SA_IGNORE,			/* SIGCHLD */
2198a8be776SJilles Tjoelker 	SA_STOP|SA_TTYSTOP,		/* SIGTTIN */
2208a8be776SJilles Tjoelker 	SA_STOP|SA_TTYSTOP,		/* SIGTTOU */
2218a8be776SJilles Tjoelker 	SA_IGNORE,			/* SIGIO */
2222c42a146SMarcel Moolenaar 	SA_KILL,			/* SIGXCPU */
2232c42a146SMarcel Moolenaar 	SA_KILL,			/* SIGXFSZ */
2248a8be776SJilles Tjoelker 	SA_KILL,			/* SIGVTALRM */
2258a8be776SJilles Tjoelker 	SA_KILL,			/* SIGPROF */
2268a8be776SJilles Tjoelker 	SA_IGNORE,			/* SIGWINCH  */
2278a8be776SJilles Tjoelker 	SA_IGNORE,			/* SIGINFO */
2288a8be776SJilles Tjoelker 	SA_KILL,			/* SIGUSR1 */
2298a8be776SJilles Tjoelker 	SA_KILL,			/* SIGUSR2 */
2302c42a146SMarcel Moolenaar };
2312c42a146SMarcel Moolenaar 
23280a8b0f3SKonstantin Belousov static void reschedule_signals(struct proc *p, sigset_t block, int flags);
2336b286ee8SKonstantin Belousov 
2349104847fSDavid Xu static void
2359104847fSDavid Xu sigqueue_start(void)
2369104847fSDavid Xu {
2379104847fSDavid Xu 	ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
2389104847fSDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2399104847fSDavid Xu 	uma_prealloc(ksiginfo_zone, preallocate_siginfo);
240b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
241b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
242b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
2439104847fSDavid Xu }
2449104847fSDavid Xu 
2455da49fcbSDavid Xu ksiginfo_t *
246ebceaf6dSDavid Xu ksiginfo_alloc(int wait)
2479104847fSDavid Xu {
248ebceaf6dSDavid Xu 	int flags;
249ebceaf6dSDavid Xu 
250ebceaf6dSDavid Xu 	flags = M_ZERO;
251ebceaf6dSDavid Xu 	if (! wait)
252ebceaf6dSDavid Xu 		flags |= M_NOWAIT;
2539104847fSDavid Xu 	if (ksiginfo_zone != NULL)
254ebceaf6dSDavid Xu 		return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
2559104847fSDavid Xu 	return (NULL);
2569104847fSDavid Xu }
2579104847fSDavid Xu 
2585da49fcbSDavid Xu void
2599104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi)
2609104847fSDavid Xu {
2619104847fSDavid Xu 	uma_zfree(ksiginfo_zone, ksi);
2629104847fSDavid Xu }
2639104847fSDavid Xu 
2645da49fcbSDavid Xu static __inline int
2655da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi)
2665da49fcbSDavid Xu {
2675da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT)) {
2685da49fcbSDavid Xu 		uma_zfree(ksiginfo_zone, ksi);
2695da49fcbSDavid Xu 		return (1);
2705da49fcbSDavid Xu 	}
2715da49fcbSDavid Xu 	return (0);
2725da49fcbSDavid Xu }
2735da49fcbSDavid Xu 
2749104847fSDavid Xu void
2759104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p)
2769104847fSDavid Xu {
2779104847fSDavid Xu 	SIGEMPTYSET(list->sq_signals);
2783dfcaad6SDavid Xu 	SIGEMPTYSET(list->sq_kill);
2799104847fSDavid Xu 	TAILQ_INIT(&list->sq_list);
2809104847fSDavid Xu 	list->sq_proc = p;
2819104847fSDavid Xu 	list->sq_flags = SQ_INIT;
2829104847fSDavid Xu }
2839104847fSDavid Xu 
2849104847fSDavid Xu /*
2859104847fSDavid Xu  * Get a signal's ksiginfo.
2869104847fSDavid Xu  * Return:
2879104847fSDavid Xu  *	0	-	signal not found
2889104847fSDavid Xu  *	others	-	signal number
2899104847fSDavid Xu  */
290a5799a4fSKonstantin Belousov static int
2919104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
2929104847fSDavid Xu {
2939104847fSDavid Xu 	struct proc *p = sq->sq_proc;
2949104847fSDavid Xu 	struct ksiginfo *ksi, *next;
2959104847fSDavid Xu 	int count = 0;
2969104847fSDavid Xu 
2979104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
2989104847fSDavid Xu 
2999104847fSDavid Xu 	if (!SIGISMEMBER(sq->sq_signals, signo))
3009104847fSDavid Xu 		return (0);
3019104847fSDavid Xu 
3023dfcaad6SDavid Xu 	if (SIGISMEMBER(sq->sq_kill, signo)) {
3033dfcaad6SDavid Xu 		count++;
3043dfcaad6SDavid Xu 		SIGDELSET(sq->sq_kill, signo);
3053dfcaad6SDavid Xu 	}
3063dfcaad6SDavid Xu 
3075c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
3089104847fSDavid Xu 		if (ksi->ksi_signo == signo) {
3099104847fSDavid Xu 			if (count == 0) {
3109104847fSDavid Xu 				TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
3115da49fcbSDavid Xu 				ksi->ksi_sigq = NULL;
3129104847fSDavid Xu 				ksiginfo_copy(ksi, si);
3135da49fcbSDavid Xu 				if (ksiginfo_tryfree(ksi) && p != NULL)
3149104847fSDavid Xu 					p->p_pendingcnt--;
3159104847fSDavid Xu 			}
316016fa302SDavid Xu 			if (++count > 1)
317016fa302SDavid Xu 				break;
3189104847fSDavid Xu 		}
3199104847fSDavid Xu 	}
3209104847fSDavid Xu 
3219104847fSDavid Xu 	if (count <= 1)
3229104847fSDavid Xu 		SIGDELSET(sq->sq_signals, signo);
3239104847fSDavid Xu 	si->ksi_signo = signo;
3249104847fSDavid Xu 	return (signo);
3259104847fSDavid Xu }
3269104847fSDavid Xu 
3275da49fcbSDavid Xu void
3285da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi)
3295da49fcbSDavid Xu {
3305da49fcbSDavid Xu 	struct ksiginfo *kp;
3315da49fcbSDavid Xu 	struct proc	*p;
3325da49fcbSDavid Xu 	sigqueue_t	*sq;
3335da49fcbSDavid Xu 
334ebceaf6dSDavid Xu 	if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
3355da49fcbSDavid Xu 		return;
3365da49fcbSDavid Xu 
3375da49fcbSDavid Xu 	p = sq->sq_proc;
3385da49fcbSDavid Xu 	TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
3395da49fcbSDavid Xu 	ksi->ksi_sigq = NULL;
3405da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
3415da49fcbSDavid Xu 		p->p_pendingcnt--;
3425da49fcbSDavid Xu 
3435da49fcbSDavid Xu 	for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
3445da49fcbSDavid Xu 	     kp = TAILQ_NEXT(kp, ksi_link)) {
3455da49fcbSDavid Xu 		if (kp->ksi_signo == ksi->ksi_signo)
3465da49fcbSDavid Xu 			break;
3475da49fcbSDavid Xu 	}
3483dfcaad6SDavid Xu 	if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
3495da49fcbSDavid Xu 		SIGDELSET(sq->sq_signals, ksi->ksi_signo);
3505da49fcbSDavid Xu }
3515da49fcbSDavid Xu 
352a5799a4fSKonstantin Belousov static int
3539104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
3549104847fSDavid Xu {
3559104847fSDavid Xu 	struct proc *p = sq->sq_proc;
3569104847fSDavid Xu 	struct ksiginfo *ksi;
3579104847fSDavid Xu 	int ret = 0;
3589104847fSDavid Xu 
3599104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
3609104847fSDavid Xu 
3613dfcaad6SDavid Xu 	if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
3623dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
3639104847fSDavid Xu 		goto out_set_bit;
3643dfcaad6SDavid Xu 	}
3659104847fSDavid Xu 
3665da49fcbSDavid Xu 	/* directly insert the ksi, don't copy it */
3675da49fcbSDavid Xu 	if (si->ksi_flags & KSI_INS) {
3686a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
3696a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
3706a671a6bSKonstantin Belousov 		else
3715da49fcbSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
3725da49fcbSDavid Xu 		si->ksi_sigq = sq;
3735da49fcbSDavid Xu 		goto out_set_bit;
3745da49fcbSDavid Xu 	}
3755da49fcbSDavid Xu 
3763dfcaad6SDavid Xu 	if (__predict_false(ksiginfo_zone == NULL)) {
3773dfcaad6SDavid Xu 		SIGADDSET(sq->sq_kill, signo);
3789104847fSDavid Xu 		goto out_set_bit;
3793dfcaad6SDavid Xu 	}
3809104847fSDavid Xu 
381ebceaf6dSDavid Xu 	if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
3829104847fSDavid Xu 		signal_overflow++;
3839104847fSDavid Xu 		ret = EAGAIN;
384ebceaf6dSDavid Xu 	} else if ((ksi = ksiginfo_alloc(0)) == NULL) {
3859104847fSDavid Xu 		signal_alloc_fail++;
3869104847fSDavid Xu 		ret = EAGAIN;
3879104847fSDavid Xu 	} else {
3889104847fSDavid Xu 		if (p != NULL)
3899104847fSDavid Xu 			p->p_pendingcnt++;
3909104847fSDavid Xu 		ksiginfo_copy(si, ksi);
3919104847fSDavid Xu 		ksi->ksi_signo = signo;
3926a671a6bSKonstantin Belousov 		if (si->ksi_flags & KSI_HEAD)
3936a671a6bSKonstantin Belousov 			TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
3946a671a6bSKonstantin Belousov 		else
3959104847fSDavid Xu 			TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
3965da49fcbSDavid Xu 		ksi->ksi_sigq = sq;
3979104847fSDavid Xu 	}
3989104847fSDavid Xu 
399a3de221dSKonstantin Belousov 	if ((si->ksi_flags & KSI_TRAP) != 0 ||
400a3de221dSKonstantin Belousov 	    (si->ksi_flags & KSI_SIGQ) == 0) {
4013dfcaad6SDavid Xu 		if (ret != 0)
4023dfcaad6SDavid Xu 			SIGADDSET(sq->sq_kill, signo);
4039104847fSDavid Xu 		ret = 0;
4049104847fSDavid Xu 		goto out_set_bit;
4059104847fSDavid Xu 	}
4069104847fSDavid Xu 
4079104847fSDavid Xu 	if (ret != 0)
4089104847fSDavid Xu 		return (ret);
4099104847fSDavid Xu 
4109104847fSDavid Xu out_set_bit:
4119104847fSDavid Xu 	SIGADDSET(sq->sq_signals, signo);
4129104847fSDavid Xu 	return (ret);
4139104847fSDavid Xu }
4149104847fSDavid Xu 
4159104847fSDavid Xu void
4169104847fSDavid Xu sigqueue_flush(sigqueue_t *sq)
4179104847fSDavid Xu {
4189104847fSDavid Xu 	struct proc *p = sq->sq_proc;
4199104847fSDavid Xu 	ksiginfo_t *ksi;
4209104847fSDavid Xu 
4219104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
4229104847fSDavid Xu 
4235da49fcbSDavid Xu 	if (p != NULL)
4245da49fcbSDavid Xu 		PROC_LOCK_ASSERT(p, MA_OWNED);
4255da49fcbSDavid Xu 
4269104847fSDavid Xu 	while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
4279104847fSDavid Xu 		TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4285da49fcbSDavid Xu 		ksi->ksi_sigq = NULL;
4295da49fcbSDavid Xu 		if (ksiginfo_tryfree(ksi) && p != NULL)
4309104847fSDavid Xu 			p->p_pendingcnt--;
4319104847fSDavid Xu 	}
4329104847fSDavid Xu 
4339104847fSDavid Xu 	SIGEMPTYSET(sq->sq_signals);
4343dfcaad6SDavid Xu 	SIGEMPTYSET(sq->sq_kill);
4359104847fSDavid Xu }
4369104847fSDavid Xu 
437a5799a4fSKonstantin Belousov static void
438fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
4399104847fSDavid Xu {
440fc4ecc1dSDavid Xu 	sigset_t tmp;
4419104847fSDavid Xu 	struct proc *p1, *p2;
4429104847fSDavid Xu 	ksiginfo_t *ksi, *next;
4439104847fSDavid Xu 
4449104847fSDavid Xu 	KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
4459104847fSDavid Xu 	KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
4469104847fSDavid Xu 	p1 = src->sq_proc;
4479104847fSDavid Xu 	p2 = dst->sq_proc;
4489104847fSDavid Xu 	/* Move siginfo to target list */
4495c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
450fc4ecc1dSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
4519104847fSDavid Xu 			TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
4529104847fSDavid Xu 			if (p1 != NULL)
4539104847fSDavid Xu 				p1->p_pendingcnt--;
4549104847fSDavid Xu 			TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
4555da49fcbSDavid Xu 			ksi->ksi_sigq = dst;
4569104847fSDavid Xu 			if (p2 != NULL)
4579104847fSDavid Xu 				p2->p_pendingcnt++;
4589104847fSDavid Xu 		}
4599104847fSDavid Xu 	}
4609104847fSDavid Xu 
4619104847fSDavid Xu 	/* Move pending bits to target list */
4623dfcaad6SDavid Xu 	tmp = src->sq_kill;
463fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
4643dfcaad6SDavid Xu 	SIGSETOR(dst->sq_kill, tmp);
4653dfcaad6SDavid Xu 	SIGSETNAND(src->sq_kill, tmp);
4663dfcaad6SDavid Xu 
4679104847fSDavid Xu 	tmp = src->sq_signals;
468fc4ecc1dSDavid Xu 	SIGSETAND(tmp, *set);
4699104847fSDavid Xu 	SIGSETOR(dst->sq_signals, tmp);
4709104847fSDavid Xu 	SIGSETNAND(src->sq_signals, tmp);
4719104847fSDavid Xu }
4729104847fSDavid Xu 
473407af02bSDavid Xu #if 0
474a5799a4fSKonstantin Belousov static void
4759104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
4769104847fSDavid Xu {
4779104847fSDavid Xu 	sigset_t set;
4789104847fSDavid Xu 
4799104847fSDavid Xu 	SIGEMPTYSET(set);
4809104847fSDavid Xu 	SIGADDSET(set, signo);
4819104847fSDavid Xu 	sigqueue_move_set(src, dst, &set);
4829104847fSDavid Xu }
483407af02bSDavid Xu #endif
4849104847fSDavid Xu 
485a5799a4fSKonstantin Belousov static void
486fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
4879104847fSDavid Xu {
4889104847fSDavid Xu 	struct proc *p = sq->sq_proc;
4899104847fSDavid Xu 	ksiginfo_t *ksi, *next;
4909104847fSDavid Xu 
4919104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
4929104847fSDavid Xu 
4939104847fSDavid Xu 	/* Remove siginfo queue */
4945c28a8d4SDavid Xu 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
4959104847fSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
4969104847fSDavid Xu 			TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4975da49fcbSDavid Xu 			ksi->ksi_sigq = NULL;
4985da49fcbSDavid Xu 			if (ksiginfo_tryfree(ksi) && p != NULL)
4999104847fSDavid Xu 				p->p_pendingcnt--;
5009104847fSDavid Xu 		}
5019104847fSDavid Xu 	}
5023dfcaad6SDavid Xu 	SIGSETNAND(sq->sq_kill, *set);
5039104847fSDavid Xu 	SIGSETNAND(sq->sq_signals, *set);
5049104847fSDavid Xu }
5059104847fSDavid Xu 
5069104847fSDavid Xu void
5079104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo)
5089104847fSDavid Xu {
5099104847fSDavid Xu 	sigset_t set;
5109104847fSDavid Xu 
5119104847fSDavid Xu 	SIGEMPTYSET(set);
5129104847fSDavid Xu 	SIGADDSET(set, signo);
5139104847fSDavid Xu 	sigqueue_delete_set(sq, &set);
5149104847fSDavid Xu }
5159104847fSDavid Xu 
5169104847fSDavid Xu /* Remove a set of signals for a process */
517a5799a4fSKonstantin Belousov static void
518fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
5199104847fSDavid Xu {
5209104847fSDavid Xu 	sigqueue_t worklist;
5219104847fSDavid Xu 	struct thread *td0;
5229104847fSDavid Xu 
5239104847fSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
5249104847fSDavid Xu 
5259104847fSDavid Xu 	sigqueue_init(&worklist, NULL);
5269104847fSDavid Xu 	sigqueue_move_set(&p->p_sigqueue, &worklist, set);
5279104847fSDavid Xu 
5289104847fSDavid Xu 	FOREACH_THREAD_IN_PROC(p, td0)
5299104847fSDavid Xu 		sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
5309104847fSDavid Xu 
5319104847fSDavid Xu 	sigqueue_flush(&worklist);
5329104847fSDavid Xu }
5339104847fSDavid Xu 
5349104847fSDavid Xu void
5359104847fSDavid Xu sigqueue_delete_proc(struct proc *p, 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_proc(p, &set);
5429104847fSDavid Xu }
5439104847fSDavid Xu 
544a5799a4fSKonstantin Belousov static void
5459104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p)
5469104847fSDavid Xu {
5479104847fSDavid Xu 	sigset_t set;
5489104847fSDavid Xu 
5499104847fSDavid Xu 	SIGEMPTYSET(set);
5509104847fSDavid Xu 	SIGADDSET(set, SIGSTOP);
5519104847fSDavid Xu 	SIGADDSET(set, SIGTSTP);
5529104847fSDavid Xu 	SIGADDSET(set, SIGTTIN);
5539104847fSDavid Xu 	SIGADDSET(set, SIGTTOU);
5549104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
5559104847fSDavid Xu }
5569104847fSDavid Xu 
557fbbeeb6cSBruce Evans /*
5581968f37bSJohn Baldwin  * Determine signal that should be delivered to thread td, the current
5591968f37bSJohn Baldwin  * thread, 0 if none.  If there is a pending stop signal with default
560fbbeeb6cSBruce Evans  * action, the process stops in issignal().
561fbbeeb6cSBruce Evans  */
562fbbeeb6cSBruce Evans int
5633cf3b9f0SJohn Baldwin cursig(struct thread *td)
564fbbeeb6cSBruce Evans {
565c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
56690af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
567a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
5683cf3b9f0SJohn Baldwin 	return (SIGPENDING(td) ? issignal(td) : 0);
569fbbeeb6cSBruce Evans }
570fbbeeb6cSBruce Evans 
57179065dbaSBruce Evans /*
57279065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
5739104847fSDavid Xu  * mode.  This must be called whenever a signal is added to td_sigqueue or
5744093529dSJeff Roberson  * unmasked in td_sigmask.
57579065dbaSBruce Evans  */
57679065dbaSBruce Evans void
5774093529dSJeff Roberson signotify(struct thread *td)
57879065dbaSBruce Evans {
5794093529dSJeff Roberson 	struct proc *p;
5804093529dSJeff Roberson 
5814093529dSJeff Roberson 	p = td->td_proc;
58279065dbaSBruce Evans 
58379065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
5844093529dSJeff Roberson 
5858b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
586a54e85fdSJeff Roberson 		thread_lock(td);
5874093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
588a54e85fdSJeff Roberson 		thread_unlock(td);
58979065dbaSBruce Evans 	}
5908b94a061SJohn Baldwin }
5918b94a061SJohn Baldwin 
5928b94a061SJohn Baldwin int
5938b94a061SJohn Baldwin sigonstack(size_t sp)
5948b94a061SJohn Baldwin {
595a30ec4b9SDavid Xu 	struct thread *td = curthread;
5968b94a061SJohn Baldwin 
597a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
5981930e303SPoul-Henning Kamp #if defined(COMPAT_43)
599a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
600a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
601a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
6028b94a061SJohn Baldwin #else
603a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
6048b94a061SJohn Baldwin #endif
6058b94a061SJohn Baldwin 	    : 0);
6068b94a061SJohn Baldwin }
60779065dbaSBruce Evans 
6086f841fb7SMarcel Moolenaar static __inline int
6096f841fb7SMarcel Moolenaar sigprop(int sig)
6102c42a146SMarcel Moolenaar {
6116f841fb7SMarcel Moolenaar 
6122c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
6132c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
6142c42a146SMarcel Moolenaar 	return (0);
615df8bae1dSRodney W. Grimes }
6162c42a146SMarcel Moolenaar 
6174093529dSJeff Roberson int
6186f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
6192c42a146SMarcel Moolenaar {
6202c42a146SMarcel Moolenaar 	int i;
6212c42a146SMarcel Moolenaar 
6226f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
6232c42a146SMarcel Moolenaar 		if (set->__bits[i])
6242c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
625df8bae1dSRodney W. Grimes 	return (0);
626df8bae1dSRodney W. Grimes }
627df8bae1dSRodney W. Grimes 
6282c42a146SMarcel Moolenaar /*
6298f19eb88SIan Dowse  * kern_sigaction
6302c42a146SMarcel Moolenaar  * sigaction
63123eeeff7SPeter Wemm  * freebsd4_sigaction
6322c42a146SMarcel Moolenaar  * osigaction
6332c42a146SMarcel Moolenaar  */
6348f19eb88SIan Dowse int
63523eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
6368f19eb88SIan Dowse 	struct thread *td;
6372c42a146SMarcel Moolenaar 	register int sig;
6382c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
63923eeeff7SPeter Wemm 	int flags;
640df8bae1dSRodney W. Grimes {
64190af4afaSJohn Baldwin 	struct sigacts *ps;
6428f19eb88SIan Dowse 	struct proc *p = td->td_proc;
643df8bae1dSRodney W. Grimes 
6442899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
6452c42a146SMarcel Moolenaar 		return (EINVAL);
6462c42a146SMarcel Moolenaar 
647628d2653SJohn Baldwin 	PROC_LOCK(p);
648628d2653SJohn Baldwin 	ps = p->p_sigacts;
64990af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
6502c42a146SMarcel Moolenaar 	if (oact) {
6512c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
6522c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
6532c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
6542c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
6552c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
6562c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
6572c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
6582c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
6592c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
6602c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
66110c2b8e1SDavid E. O'Brien 		if (SIGISMEMBER(ps->ps_siginfo, sig)) {
6622c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
66310c2b8e1SDavid E. O'Brien 			oact->sa_sigaction =
66410c2b8e1SDavid E. O'Brien 			    (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
66510c2b8e1SDavid E. O'Brien 		} else
66610c2b8e1SDavid E. O'Brien 			oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
66790af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
6682c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
66990af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
6702c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
6712c42a146SMarcel Moolenaar 	}
6722c42a146SMarcel Moolenaar 	if (act) {
6732c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
674628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
67590af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
676628d2653SJohn Baldwin 			PROC_UNLOCK(p);
6772c42a146SMarcel Moolenaar 			return (EINVAL);
678628d2653SJohn Baldwin 		}
6792c42a146SMarcel Moolenaar 
680df8bae1dSRodney W. Grimes 		/*
681df8bae1dSRodney W. Grimes 		 * Change setting atomically.
682df8bae1dSRodney W. Grimes 		 */
6832c42a146SMarcel Moolenaar 
6842c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
6852c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
6862c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
687aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
688aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
68980f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
69080f42b55SIan Dowse 		} else {
69180f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
6922c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
6932c42a146SMarcel Moolenaar 		}
6942c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
6952c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
696df8bae1dSRodney W. Grimes 		else
6972c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
6982c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
6992c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
700df8bae1dSRodney W. Grimes 		else
7012c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
7022c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
7032c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
7041e41c1b5SSteven Wallace 		else
7052c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
7062c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
7072c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
708289ccde0SPeter Wemm 		else
7092c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
7102c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
7112c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
71290af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
7136626c604SJulian Elischer 			else
71490af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
715ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
716245f17d4SJoerg Wunsch 				/*
7172c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
7182c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
7192c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
7202c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
721245f17d4SJoerg Wunsch 				 */
722245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
72390af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
7246626c604SJulian Elischer 				else
72590af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
726245f17d4SJoerg Wunsch 			} else
72790af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
728ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
72990af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
730ba1551caSIan Dowse 			else
73190af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
732df8bae1dSRodney W. Grimes 		}
733df8bae1dSRodney W. Grimes 		/*
73490af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
7352c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
73690af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
7372c42a146SMarcel Moolenaar 		 * have to restart the process.
738df8bae1dSRodney W. Grimes 		 */
7392c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
7402c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
7412c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
7422c42a146SMarcel Moolenaar 			/* never to be seen again */
7439104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
7442c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
7452c42a146SMarcel Moolenaar 				/* easier in psignal */
74690af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
74790af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
748645682fdSLuoqi Chen 		} else {
74990af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
7502c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
75190af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
7522c42a146SMarcel Moolenaar 			else
75390af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
7542c42a146SMarcel Moolenaar 		}
75523eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
75623eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
75723eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
75823eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
75923eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
76023eeeff7SPeter Wemm 		else
76123eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
76223eeeff7SPeter Wemm #endif
763e8ebc08fSPeter Wemm #ifdef COMPAT_43
764645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
76523eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
76623eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
767645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
768645682fdSLuoqi Chen 		else
769645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
770e8ebc08fSPeter Wemm #endif
771df8bae1dSRodney W. Grimes 	}
77290af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
773628d2653SJohn Baldwin 	PROC_UNLOCK(p);
7742c42a146SMarcel Moolenaar 	return (0);
7752c42a146SMarcel Moolenaar }
7762c42a146SMarcel Moolenaar 
7772c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7782c42a146SMarcel Moolenaar struct sigaction_args {
7792c42a146SMarcel Moolenaar 	int	sig;
7802c42a146SMarcel Moolenaar 	struct	sigaction *act;
7812c42a146SMarcel Moolenaar 	struct	sigaction *oact;
7822c42a146SMarcel Moolenaar };
7832c42a146SMarcel Moolenaar #endif
7842c42a146SMarcel Moolenaar int
7858451d0ddSKip Macy sys_sigaction(td, uap)
786b40ce416SJulian Elischer 	struct thread *td;
7872c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
7882c42a146SMarcel Moolenaar {
7892c42a146SMarcel Moolenaar 	struct sigaction act, oact;
7902c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
7912c42a146SMarcel Moolenaar 	int error;
7922c42a146SMarcel Moolenaar 
7936f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
7946f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
7952c42a146SMarcel Moolenaar 	if (actp) {
7966f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
7972c42a146SMarcel Moolenaar 		if (error)
79844443757STim J. Robbins 			return (error);
7992c42a146SMarcel Moolenaar 	}
8008f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
80125d6dc06SJohn Baldwin 	if (oactp && !error)
8026f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
8032c42a146SMarcel Moolenaar 	return (error);
8042c42a146SMarcel Moolenaar }
8052c42a146SMarcel Moolenaar 
80623eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
80723eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
80823eeeff7SPeter Wemm struct freebsd4_sigaction_args {
80923eeeff7SPeter Wemm 	int	sig;
81023eeeff7SPeter Wemm 	struct	sigaction *act;
81123eeeff7SPeter Wemm 	struct	sigaction *oact;
81223eeeff7SPeter Wemm };
81323eeeff7SPeter Wemm #endif
81423eeeff7SPeter Wemm int
81523eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
81623eeeff7SPeter Wemm 	struct thread *td;
81723eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
81823eeeff7SPeter Wemm {
81923eeeff7SPeter Wemm 	struct sigaction act, oact;
82023eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
82123eeeff7SPeter Wemm 	int error;
82223eeeff7SPeter Wemm 
82323eeeff7SPeter Wemm 
82423eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
82523eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
82623eeeff7SPeter Wemm 	if (actp) {
82723eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
82823eeeff7SPeter Wemm 		if (error)
82944443757STim J. Robbins 			return (error);
83023eeeff7SPeter Wemm 	}
83123eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
832a14e1189SJohn Baldwin 	if (oactp && !error)
83323eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
83423eeeff7SPeter Wemm 	return (error);
83523eeeff7SPeter Wemm }
83623eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
83723eeeff7SPeter Wemm 
83831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8392c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8402c42a146SMarcel Moolenaar struct osigaction_args {
8412c42a146SMarcel Moolenaar 	int	signum;
8422c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
8432c42a146SMarcel Moolenaar 	struct	osigaction *osa;
8442c42a146SMarcel Moolenaar };
8452c42a146SMarcel Moolenaar #endif
8462c42a146SMarcel Moolenaar int
847b40ce416SJulian Elischer osigaction(td, uap)
848b40ce416SJulian Elischer 	struct thread *td;
8492c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
8502c42a146SMarcel Moolenaar {
8512c42a146SMarcel Moolenaar 	struct osigaction sa;
8522c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
8532c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
8542c42a146SMarcel Moolenaar 	int error;
8552c42a146SMarcel Moolenaar 
8566f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
8576f841fb7SMarcel Moolenaar 		return (EINVAL);
858fb99ab88SMatthew Dillon 
8596f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
8606f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
861fb99ab88SMatthew Dillon 
8622c42a146SMarcel Moolenaar 	if (nsap) {
8636f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
8642c42a146SMarcel Moolenaar 		if (error)
86544443757STim J. Robbins 			return (error);
8662c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
8672c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
8682c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
8692c42a146SMarcel Moolenaar 	}
87023eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
8712c42a146SMarcel Moolenaar 	if (osap && !error) {
8722c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
8732c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
8742c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
8756f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
8762c42a146SMarcel Moolenaar 	}
8772c42a146SMarcel Moolenaar 	return (error);
8782c42a146SMarcel Moolenaar }
87923eeeff7SPeter Wemm 
88073dbd3daSJohn Baldwin #if !defined(__i386__)
88123eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
88223eeeff7SPeter Wemm int
88323eeeff7SPeter Wemm osigreturn(td, uap)
88423eeeff7SPeter Wemm 	struct thread *td;
88523eeeff7SPeter Wemm 	struct osigreturn_args *uap;
88623eeeff7SPeter Wemm {
88723eeeff7SPeter Wemm 
88823eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
88923eeeff7SPeter Wemm }
89023eeeff7SPeter Wemm #endif
89131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
892df8bae1dSRodney W. Grimes 
893df8bae1dSRodney W. Grimes /*
894df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
895df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
896df8bae1dSRodney W. Grimes  */
897df8bae1dSRodney W. Grimes void
898df8bae1dSRodney W. Grimes siginit(p)
899df8bae1dSRodney W. Grimes 	struct proc *p;
900df8bae1dSRodney W. Grimes {
901df8bae1dSRodney W. Grimes 	register int i;
90290af4afaSJohn Baldwin 	struct sigacts *ps;
903df8bae1dSRodney W. Grimes 
904628d2653SJohn Baldwin 	PROC_LOCK(p);
90590af4afaSJohn Baldwin 	ps = p->p_sigacts;
90690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
9072c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
9082c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
90990af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
91090af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
911628d2653SJohn Baldwin 	PROC_UNLOCK(p);
912df8bae1dSRodney W. Grimes }
913df8bae1dSRodney W. Grimes 
914df8bae1dSRodney W. Grimes /*
915df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
916df8bae1dSRodney W. Grimes  */
917df8bae1dSRodney W. Grimes void
918a30ec4b9SDavid Xu execsigs(struct proc *p)
919df8bae1dSRodney W. Grimes {
920a30ec4b9SDavid Xu 	struct sigacts *ps;
921a30ec4b9SDavid Xu 	int sig;
922a30ec4b9SDavid Xu 	struct thread *td;
923df8bae1dSRodney W. Grimes 
924df8bae1dSRodney W. Grimes 	/*
925df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
9264093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
927df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
928df8bae1dSRodney W. Grimes 	 */
9299b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
930a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
931628d2653SJohn Baldwin 	ps = p->p_sigacts;
93290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
93390af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
93490af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
93590af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
9362c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
9372c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
93890af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
9399104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
940df8bae1dSRodney W. Grimes 		}
9412c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
942df8bae1dSRodney W. Grimes 	}
943df8bae1dSRodney W. Grimes 	/*
944df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
945df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
946df8bae1dSRodney W. Grimes 	 */
947a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
948a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
949a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
950a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
95180e907a1SPeter Wemm 	/*
95280e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
95380e907a1SPeter Wemm 	 */
95490af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
955c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
956c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
95790af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
958df8bae1dSRodney W. Grimes }
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes /*
961e77daab1SJohn Baldwin  * kern_sigprocmask()
9627c8fdcbdSMatthew Dillon  *
963628d2653SJohn Baldwin  *	Manipulate signal mask.
964df8bae1dSRodney W. Grimes  */
965e77daab1SJohn Baldwin int
9666b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
96784440afbSKonstantin Belousov     int flags)
9682c42a146SMarcel Moolenaar {
9696b286ee8SKonstantin Belousov 	sigset_t new_block, oset1;
9706b286ee8SKonstantin Belousov 	struct proc *p;
9712c42a146SMarcel Moolenaar 	int error;
9722c42a146SMarcel Moolenaar 
9736b286ee8SKonstantin Belousov 	p = td->td_proc;
97484440afbSKonstantin Belousov 	if (!(flags & SIGPROCMASK_PROC_LOCKED))
9756b286ee8SKonstantin Belousov 		PROC_LOCK(p);
9762c42a146SMarcel Moolenaar 	if (oset != NULL)
9774093529dSJeff Roberson 		*oset = td->td_sigmask;
9782c42a146SMarcel Moolenaar 
9792c42a146SMarcel Moolenaar 	error = 0;
9802c42a146SMarcel Moolenaar 	if (set != NULL) {
9812c42a146SMarcel Moolenaar 		switch (how) {
9822c42a146SMarcel Moolenaar 		case SIG_BLOCK:
983645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
9846b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
9854093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
9866b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
9876b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
9882c42a146SMarcel Moolenaar 			break;
9892c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
9904093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
9914093529dSJeff Roberson 			signotify(td);
992407af02bSDavid Xu 			goto out;
9932c42a146SMarcel Moolenaar 		case SIG_SETMASK:
994645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
9956b286ee8SKonstantin Belousov 			oset1 = td->td_sigmask;
99684440afbSKonstantin Belousov 			if (flags & SIGPROCMASK_OLD)
9974093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
998645682fdSLuoqi Chen 			else
9994093529dSJeff Roberson 				td->td_sigmask = *set;
10006b286ee8SKonstantin Belousov 			new_block = td->td_sigmask;
10016b286ee8SKonstantin Belousov 			SIGSETNAND(new_block, oset1);
10024093529dSJeff Roberson 			signotify(td);
10032c42a146SMarcel Moolenaar 			break;
10042c42a146SMarcel Moolenaar 		default:
10052c42a146SMarcel Moolenaar 			error = EINVAL;
1006407af02bSDavid Xu 			goto out;
10072c42a146SMarcel Moolenaar 		}
10086b286ee8SKonstantin Belousov 
10096b286ee8SKonstantin Belousov 		/*
10107df8f6abSKonstantin Belousov 		 * The new_block set contains signals that were not previously
10116b286ee8SKonstantin Belousov 		 * blocked, but are blocked now.
10126b286ee8SKonstantin Belousov 		 *
10136b286ee8SKonstantin Belousov 		 * In case we block any signal that was not previously blocked
10146b286ee8SKonstantin Belousov 		 * for td, and process has the signal pending, try to schedule
1015407af02bSDavid Xu 		 * signal delivery to some thread that does not block the
1016407af02bSDavid Xu 		 * signal, possibly waking it up.
10176b286ee8SKonstantin Belousov 		 */
10186b286ee8SKonstantin Belousov 		if (p->p_numthreads != 1)
101980a8b0f3SKonstantin Belousov 			reschedule_signals(p, new_block, flags);
1020407af02bSDavid Xu 	}
10216b286ee8SKonstantin Belousov 
1022407af02bSDavid Xu out:
102384440afbSKonstantin Belousov 	if (!(flags & SIGPROCMASK_PROC_LOCKED))
10246b286ee8SKonstantin Belousov 		PROC_UNLOCK(p);
10252c42a146SMarcel Moolenaar 	return (error);
10262c42a146SMarcel Moolenaar }
10272c42a146SMarcel Moolenaar 
1028d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1029df8bae1dSRodney W. Grimes struct sigprocmask_args {
1030df8bae1dSRodney W. Grimes 	int	how;
10312c42a146SMarcel Moolenaar 	const sigset_t *set;
10322c42a146SMarcel Moolenaar 	sigset_t *oset;
1033df8bae1dSRodney W. Grimes };
1034d2d3e875SBruce Evans #endif
103526f9a767SRodney W. Grimes int
10368451d0ddSKip Macy sys_sigprocmask(td, uap)
1037b40ce416SJulian Elischer 	register struct thread *td;
1038df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
1039df8bae1dSRodney W. Grimes {
10402c42a146SMarcel Moolenaar 	sigset_t set, oset;
10412c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
10422c42a146SMarcel Moolenaar 	int error;
1043df8bae1dSRodney W. Grimes 
10446f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
10456f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
10462c42a146SMarcel Moolenaar 	if (setp) {
10476f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
10482c42a146SMarcel Moolenaar 		if (error)
10492c42a146SMarcel Moolenaar 			return (error);
1050df8bae1dSRodney W. Grimes 	}
1051e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
10522c42a146SMarcel Moolenaar 	if (osetp && !error) {
10536f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
10542c42a146SMarcel Moolenaar 	}
10552c42a146SMarcel Moolenaar 	return (error);
10562c42a146SMarcel Moolenaar }
10572c42a146SMarcel Moolenaar 
105831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
10592c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
10602c42a146SMarcel Moolenaar struct osigprocmask_args {
10612c42a146SMarcel Moolenaar 	int	how;
10622c42a146SMarcel Moolenaar 	osigset_t mask;
10632c42a146SMarcel Moolenaar };
10642c42a146SMarcel Moolenaar #endif
10652c42a146SMarcel Moolenaar int
1066b40ce416SJulian Elischer osigprocmask(td, uap)
1067b40ce416SJulian Elischer 	register struct thread *td;
10682c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
10692c42a146SMarcel Moolenaar {
10702c42a146SMarcel Moolenaar 	sigset_t set, oset;
10712c42a146SMarcel Moolenaar 	int error;
10722c42a146SMarcel Moolenaar 
10732c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1074e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1075b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
1076df8bae1dSRodney W. Grimes 	return (error);
1077df8bae1dSRodney W. Grimes }
107831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1079df8bae1dSRodney W. Grimes 
108026f9a767SRodney W. Grimes int
10818451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap)
1082a447cd8bSJeff Roberson {
10839104847fSDavid Xu 	ksiginfo_t ksi;
1084a447cd8bSJeff Roberson 	sigset_t set;
1085a447cd8bSJeff Roberson 	int error;
1086a447cd8bSJeff Roberson 
1087a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
108836939a0aSDavid Xu 	if (error) {
108936939a0aSDavid Xu 		td->td_retval[0] = error;
109036939a0aSDavid Xu 		return (0);
109136939a0aSDavid Xu 	}
1092a447cd8bSJeff Roberson 
10939104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
109436939a0aSDavid Xu 	if (error) {
10958e9a54eeSKonstantin Belousov 		if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
10968e9a54eeSKonstantin Belousov 			error = ERESTART;
109736939a0aSDavid Xu 		if (error == ERESTART)
1098a447cd8bSJeff Roberson 			return (error);
109936939a0aSDavid Xu 		td->td_retval[0] = error;
110036939a0aSDavid Xu 		return (0);
110136939a0aSDavid Xu 	}
1102a447cd8bSJeff Roberson 
11039104847fSDavid Xu 	error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
110436939a0aSDavid Xu 	td->td_retval[0] = error;
110536939a0aSDavid Xu 	return (0);
1106a447cd8bSJeff Roberson }
11070c14ff0eSRobert Watson 
1108a447cd8bSJeff Roberson int
11098451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1110a447cd8bSJeff Roberson {
1111a447cd8bSJeff Roberson 	struct timespec ts;
1112a447cd8bSJeff Roberson 	struct timespec *timeout;
1113a447cd8bSJeff Roberson 	sigset_t set;
11149104847fSDavid Xu 	ksiginfo_t ksi;
1115a447cd8bSJeff Roberson 	int error;
1116a447cd8bSJeff Roberson 
1117a447cd8bSJeff Roberson 	if (uap->timeout) {
1118a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
1119a447cd8bSJeff Roberson 		if (error)
1120a447cd8bSJeff Roberson 			return (error);
1121a447cd8bSJeff Roberson 
1122a447cd8bSJeff Roberson 		timeout = &ts;
1123a447cd8bSJeff Roberson 	} else
1124a447cd8bSJeff Roberson 		timeout = NULL;
1125a447cd8bSJeff Roberson 
1126a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1127a447cd8bSJeff Roberson 	if (error)
1128a447cd8bSJeff Roberson 		return (error);
1129a447cd8bSJeff Roberson 
11309104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, timeout);
1131a447cd8bSJeff Roberson 	if (error)
1132a447cd8bSJeff Roberson 		return (error);
11339dde3bc9SDavid Xu 
1134418228dfSDavid Xu 	if (uap->info)
11359104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
11369104847fSDavid Xu 
11379104847fSDavid Xu 	if (error == 0)
11389104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1139a447cd8bSJeff Roberson 	return (error);
1140a447cd8bSJeff Roberson }
1141a447cd8bSJeff Roberson 
1142a447cd8bSJeff Roberson int
11438451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1144a447cd8bSJeff Roberson {
11459104847fSDavid Xu 	ksiginfo_t ksi;
1146a447cd8bSJeff Roberson 	sigset_t set;
1147a447cd8bSJeff Roberson 	int error;
1148a447cd8bSJeff Roberson 
1149a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1150a447cd8bSJeff Roberson 	if (error)
1151a447cd8bSJeff Roberson 		return (error);
1152a447cd8bSJeff Roberson 
11539104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
1154a447cd8bSJeff Roberson 	if (error)
1155a447cd8bSJeff Roberson 		return (error);
1156a447cd8bSJeff Roberson 
1157418228dfSDavid Xu 	if (uap->info)
11589104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
11599104847fSDavid Xu 
11609104847fSDavid Xu 	if (error == 0)
11619104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1162a447cd8bSJeff Roberson 	return (error);
1163a447cd8bSJeff Roberson }
1164a447cd8bSJeff Roberson 
1165c6511aeaSDavid Xu int
11669104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1167a447cd8bSJeff Roberson 	struct timespec *timeout)
1168a447cd8bSJeff Roberson {
11693074d1b4SDavid Xu 	struct sigacts *ps;
1170407af02bSDavid Xu 	sigset_t saved_mask, new_block;
1171a447cd8bSJeff Roberson 	struct proc *p;
1172407af02bSDavid Xu 	int error, sig, timo, timevalid = 0;
11731089f031SDavid Xu 	struct timespec rts, ets, ts;
11741089f031SDavid Xu 	struct timeval tv;
1175a447cd8bSJeff Roberson 
1176a447cd8bSJeff Roberson 	p = td->td_proc;
1177a447cd8bSJeff Roberson 	error = 0;
11789b73d239SMatt Jacob 	ets.tv_sec = 0;
11799b73d239SMatt Jacob 	ets.tv_nsec = 0;
1180a447cd8bSJeff Roberson 
1181407af02bSDavid Xu 	if (timeout != NULL) {
11821089f031SDavid Xu 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
11831089f031SDavid Xu 			timevalid = 1;
11841089f031SDavid Xu 			getnanouptime(&rts);
11851089f031SDavid Xu 			ets = rts;
11861089f031SDavid Xu 			timespecadd(&ets, timeout);
11871089f031SDavid Xu 		}
11881089f031SDavid Xu 	}
1189407af02bSDavid Xu 	ksiginfo_init(ksi);
1190407af02bSDavid Xu 	/* Some signals can not be waited for. */
1191407af02bSDavid Xu 	SIG_CANTMASK(waitset);
1192407af02bSDavid Xu 	ps = p->p_sigacts;
1193407af02bSDavid Xu 	PROC_LOCK(p);
1194407af02bSDavid Xu 	saved_mask = td->td_sigmask;
1195407af02bSDavid Xu 	SIGSETNAND(td->td_sigmask, waitset);
1196407af02bSDavid Xu 	for (;;) {
11973074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
11983cf3b9f0SJohn Baldwin 		sig = cursig(td);
11993074d1b4SDavid Xu 		mtx_unlock(&ps->ps_mtx);
1200407af02bSDavid Xu 		if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1201407af02bSDavid Xu 			if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1202407af02bSDavid Xu 			    sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1203407af02bSDavid Xu 				error = 0;
1204407af02bSDavid Xu 				break;
12053074d1b4SDavid Xu 			}
12067e0221a2SDavid Xu 		}
12077e0221a2SDavid Xu 
1208407af02bSDavid Xu 		if (error != 0)
1209407af02bSDavid Xu 			break;
12103074d1b4SDavid Xu 
1211a447cd8bSJeff Roberson 		/*
1212a447cd8bSJeff Roberson 		 * POSIX says this must be checked after looking for pending
1213a447cd8bSJeff Roberson 		 * signals.
1214a447cd8bSJeff Roberson 		 */
1215407af02bSDavid Xu 		if (timeout != NULL) {
12161089f031SDavid Xu 			if (!timevalid) {
1217a447cd8bSJeff Roberson 				error = EINVAL;
1218407af02bSDavid Xu 				break;
1219a447cd8bSJeff Roberson 			}
12201089f031SDavid Xu 			getnanouptime(&rts);
12211089f031SDavid Xu 			if (timespeccmp(&rts, &ets, >=)) {
12223074d1b4SDavid Xu 				error = EAGAIN;
1223407af02bSDavid Xu 				break;
12243074d1b4SDavid Xu 			}
12251089f031SDavid Xu 			ts = ets;
12261089f031SDavid Xu 			timespecsub(&ts, &rts);
12271089f031SDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
1228407af02bSDavid Xu 			timo = tvtohz(&tv);
1229407af02bSDavid Xu 		} else {
1230407af02bSDavid Xu 			timo = 0;
1231407af02bSDavid Xu 		}
1232a447cd8bSJeff Roberson 
1233407af02bSDavid Xu 		error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1234407af02bSDavid Xu 
1235407af02bSDavid Xu 		if (timeout != NULL) {
12361089f031SDavid Xu 			if (error == ERESTART) {
1237407af02bSDavid Xu 				/* Timeout can not be restarted. */
12383074d1b4SDavid Xu 				error = EINTR;
12391089f031SDavid Xu 			} else if (error == EAGAIN) {
1240407af02bSDavid Xu 				/* We will calculate timeout by ourself. */
12411089f031SDavid Xu 				error = 0;
12421089f031SDavid Xu 			}
12431089f031SDavid Xu 		}
1244407af02bSDavid Xu 	}
12459dde3bc9SDavid Xu 
1246407af02bSDavid Xu 	new_block = saved_mask;
1247407af02bSDavid Xu 	SIGSETNAND(new_block, td->td_sigmask);
1248407af02bSDavid Xu 	td->td_sigmask = saved_mask;
1249407af02bSDavid Xu 	/*
1250407af02bSDavid Xu 	 * Fewer signals can be delivered to us, reschedule signal
1251407af02bSDavid Xu 	 * notification.
1252407af02bSDavid Xu 	 */
1253407af02bSDavid Xu 	if (p->p_numthreads != 1)
1254407af02bSDavid Xu 		reschedule_signals(p, new_block, 0);
12555d217f17SJohn Birrell 
1256407af02bSDavid Xu 	if (error == 0) {
12575d217f17SJohn Birrell 		SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
12585d217f17SJohn Birrell 
125956c06c4bSDavid Xu 		if (ksi->ksi_code == SI_TIMER)
126056c06c4bSDavid Xu 			itimer_accept(p, ksi->ksi_timerid, ksi);
12617e0221a2SDavid Xu 
12627e0221a2SDavid Xu #ifdef KTRACE
12637e0221a2SDavid Xu 		if (KTRPOINT(td, KTR_PSIG)) {
12647e0221a2SDavid Xu 			sig_t action;
12657e0221a2SDavid Xu 
12663074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
1267a447cd8bSJeff Roberson 			action = ps->ps_sigact[_SIG_IDX(sig)];
126890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
126961009552SJilles Tjoelker 			ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
12707e0221a2SDavid Xu 		}
1271a447cd8bSJeff Roberson #endif
12727b8d5e48SDavid Xu 		if (sig == SIGKILL)
12737e0221a2SDavid Xu 			sigexit(td, sig);
12743074d1b4SDavid Xu 	}
1275a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
1276a447cd8bSJeff Roberson 	return (error);
1277a447cd8bSJeff Roberson }
1278a447cd8bSJeff Roberson 
12799104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
12809104847fSDavid Xu struct sigpending_args {
12819104847fSDavid Xu 	sigset_t	*set;
12829104847fSDavid Xu };
12839104847fSDavid Xu #endif
1284a447cd8bSJeff Roberson int
12858451d0ddSKip Macy sys_sigpending(td, uap)
1286b40ce416SJulian Elischer 	struct thread *td;
1287df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
1288df8bae1dSRodney W. Grimes {
1289b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
12909104847fSDavid Xu 	sigset_t pending;
1291df8bae1dSRodney W. Grimes 
1292628d2653SJohn Baldwin 	PROC_LOCK(p);
12939104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
12949104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1295628d2653SJohn Baldwin 	PROC_UNLOCK(p);
12969104847fSDavid Xu 	return (copyout(&pending, uap->set, sizeof(sigset_t)));
12972c42a146SMarcel Moolenaar }
12982c42a146SMarcel Moolenaar 
129931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
13002c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
13012c42a146SMarcel Moolenaar struct osigpending_args {
13022c42a146SMarcel Moolenaar 	int	dummy;
13032c42a146SMarcel Moolenaar };
13042c42a146SMarcel Moolenaar #endif
13052c42a146SMarcel Moolenaar int
1306b40ce416SJulian Elischer osigpending(td, uap)
1307b40ce416SJulian Elischer 	struct thread *td;
13082c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
13092c42a146SMarcel Moolenaar {
1310b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13119104847fSDavid Xu 	sigset_t pending;
1312b40ce416SJulian Elischer 
1313628d2653SJohn Baldwin 	PROC_LOCK(p);
13149104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
13159104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1316628d2653SJohn Baldwin 	PROC_UNLOCK(p);
13179104847fSDavid Xu 	SIG2OSIG(pending, td->td_retval[0]);
1318df8bae1dSRodney W. Grimes 	return (0);
1319df8bae1dSRodney W. Grimes }
132031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1321df8bae1dSRodney W. Grimes 
13221930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1323df8bae1dSRodney W. Grimes /*
1324df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1325df8bae1dSRodney W. Grimes  */
1326d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1327df8bae1dSRodney W. Grimes struct osigvec_args {
1328df8bae1dSRodney W. Grimes 	int	signum;
1329df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1330df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1331df8bae1dSRodney W. Grimes };
1332d2d3e875SBruce Evans #endif
1333df8bae1dSRodney W. Grimes /* ARGSUSED */
133426f9a767SRodney W. Grimes int
1335b40ce416SJulian Elischer osigvec(td, uap)
1336b40ce416SJulian Elischer 	struct thread *td;
1337df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1338df8bae1dSRodney W. Grimes {
1339df8bae1dSRodney W. Grimes 	struct sigvec vec;
13402c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
13412c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
13422c42a146SMarcel Moolenaar 	int error;
1343df8bae1dSRodney W. Grimes 
13446f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
13456f841fb7SMarcel Moolenaar 		return (EINVAL);
13466f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
13476f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
13482c42a146SMarcel Moolenaar 	if (nsap) {
13496f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
13502c42a146SMarcel Moolenaar 		if (error)
1351df8bae1dSRodney W. Grimes 			return (error);
13522c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
13532c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
13542c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
13552c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1356df8bae1dSRodney W. Grimes 	}
13575edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
13582c42a146SMarcel Moolenaar 	if (osap && !error) {
13592c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
13602c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
13612c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
13622c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
13632c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
13646f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
13652c42a146SMarcel Moolenaar 	}
13662c42a146SMarcel Moolenaar 	return (error);
1367df8bae1dSRodney W. Grimes }
1368df8bae1dSRodney W. Grimes 
1369d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1370df8bae1dSRodney W. Grimes struct osigblock_args {
1371df8bae1dSRodney W. Grimes 	int	mask;
1372df8bae1dSRodney W. Grimes };
1373d2d3e875SBruce Evans #endif
137426f9a767SRodney W. Grimes int
1375b40ce416SJulian Elischer osigblock(td, uap)
1376b40ce416SJulian Elischer 	register struct thread *td;
1377df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1378df8bae1dSRodney W. Grimes {
1379d6e029adSKonstantin Belousov 	sigset_t set, oset;
1380df8bae1dSRodney W. Grimes 
13812c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1382d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1383d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1384df8bae1dSRodney W. Grimes 	return (0);
1385df8bae1dSRodney W. Grimes }
1386df8bae1dSRodney W. Grimes 
1387d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1388df8bae1dSRodney W. Grimes struct osigsetmask_args {
1389df8bae1dSRodney W. Grimes 	int	mask;
1390df8bae1dSRodney W. Grimes };
1391d2d3e875SBruce Evans #endif
139226f9a767SRodney W. Grimes int
1393b40ce416SJulian Elischer osigsetmask(td, uap)
1394b40ce416SJulian Elischer 	struct thread *td;
1395df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1396df8bae1dSRodney W. Grimes {
1397d6e029adSKonstantin Belousov 	sigset_t set, oset;
1398df8bae1dSRodney W. Grimes 
13992c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1400d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1401d6e029adSKonstantin Belousov 	SIG2OSIG(oset, td->td_retval[0]);
1402df8bae1dSRodney W. Grimes 	return (0);
1403df8bae1dSRodney W. Grimes }
14041930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1405df8bae1dSRodney W. Grimes 
1406df8bae1dSRodney W. Grimes /*
1407873fbcd7SRobert Watson  * Suspend calling thread until signal, providing mask to be set in the
1408873fbcd7SRobert Watson  * meantime.
1409df8bae1dSRodney W. Grimes  */
1410d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1411df8bae1dSRodney W. Grimes struct sigsuspend_args {
14122c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1413df8bae1dSRodney W. Grimes };
1414d2d3e875SBruce Evans #endif
1415df8bae1dSRodney W. Grimes /* ARGSUSED */
141626f9a767SRodney W. Grimes int
14178451d0ddSKip Macy sys_sigsuspend(td, uap)
1418b40ce416SJulian Elischer 	struct thread *td;
1419df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1420df8bae1dSRodney W. Grimes {
14212c42a146SMarcel Moolenaar 	sigset_t mask;
14222c42a146SMarcel Moolenaar 	int error;
14232c42a146SMarcel Moolenaar 
14246f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
14252c42a146SMarcel Moolenaar 	if (error)
14262c42a146SMarcel Moolenaar 		return (error);
14278f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
14288f19eb88SIan Dowse }
14298f19eb88SIan Dowse 
14308f19eb88SIan Dowse int
14318f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
14328f19eb88SIan Dowse {
14338f19eb88SIan Dowse 	struct proc *p = td->td_proc;
143484440afbSKonstantin Belousov 	int has_sig, sig;
1435df8bae1dSRodney W. Grimes 
1436df8bae1dSRodney W. Grimes 	/*
1437645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1438df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1439df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1440df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1441df8bae1dSRodney W. Grimes 	 * to indicate this.
1442df8bae1dSRodney W. Grimes 	 */
1443628d2653SJohn Baldwin 	PROC_LOCK(p);
144484440afbSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
144584440afbSKonstantin Belousov 	    SIGPROCMASK_PROC_LOCKED);
14465e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
144784440afbSKonstantin Belousov 
144884440afbSKonstantin Belousov 	/*
144984440afbSKonstantin Belousov 	 * Process signals now. Otherwise, we can get spurious wakeup
145084440afbSKonstantin Belousov 	 * due to signal entered process queue, but delivered to other
145184440afbSKonstantin Belousov 	 * thread. But sigsuspend should return only on signal
145284440afbSKonstantin Belousov 	 * delivery.
145384440afbSKonstantin Belousov 	 */
1454afe1a688SKonstantin Belousov 	(p->p_sysent->sv_set_syscall_retval)(td, EINTR);
145584440afbSKonstantin Belousov 	for (has_sig = 0; !has_sig;) {
145684440afbSKonstantin Belousov 		while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
145784440afbSKonstantin Belousov 			0) == 0)
14582c42a146SMarcel Moolenaar 			/* void */;
145984440afbSKonstantin Belousov 		thread_suspend_check(0);
146084440afbSKonstantin Belousov 		mtx_lock(&p->p_sigacts->ps_mtx);
14613cf3b9f0SJohn Baldwin 		while ((sig = cursig(td)) != 0)
146275c586a4SKonstantin Belousov 			has_sig += postsig(sig);
146384440afbSKonstantin Belousov 		mtx_unlock(&p->p_sigacts->ps_mtx);
146484440afbSKonstantin Belousov 	}
1465628d2653SJohn Baldwin 	PROC_UNLOCK(p);
14662dd9ea6fSKonstantin Belousov 	td->td_errno = EINTR;
14672dd9ea6fSKonstantin Belousov 	td->td_pflags |= TDP_NERRNO;
146875c586a4SKonstantin Belousov 	return (EJUSTRETURN);
14692c42a146SMarcel Moolenaar }
14702c42a146SMarcel Moolenaar 
147131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
147297563428SAlexander Kabaev /*
147397563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
147497563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
147597563428SAlexander Kabaev  */
14762c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
14772c42a146SMarcel Moolenaar struct osigsuspend_args {
14782c42a146SMarcel Moolenaar 	osigset_t mask;
14792c42a146SMarcel Moolenaar };
14802c42a146SMarcel Moolenaar #endif
14812c42a146SMarcel Moolenaar /* ARGSUSED */
14822c42a146SMarcel Moolenaar int
1483b40ce416SJulian Elischer osigsuspend(td, uap)
1484b40ce416SJulian Elischer 	struct thread *td;
14852c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
14862c42a146SMarcel Moolenaar {
1487645682fdSLuoqi Chen 	sigset_t mask;
14882c42a146SMarcel Moolenaar 
1489645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
149084440afbSKonstantin Belousov 	return (kern_sigsuspend(td, mask));
1491df8bae1dSRodney W. Grimes }
149231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1493df8bae1dSRodney W. Grimes 
14941930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1495d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1496df8bae1dSRodney W. Grimes struct osigstack_args {
1497df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1498df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1499df8bae1dSRodney W. Grimes };
1500d2d3e875SBruce Evans #endif
1501df8bae1dSRodney W. Grimes /* ARGSUSED */
150226f9a767SRodney W. Grimes int
1503b40ce416SJulian Elischer osigstack(td, uap)
1504b40ce416SJulian Elischer 	struct thread *td;
1505df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1506df8bae1dSRodney W. Grimes {
15075afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1508fb99ab88SMatthew Dillon 	int error = 0;
1509fb99ab88SMatthew Dillon 
1510d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
15115afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
15125afe0c99SJohn Baldwin 		if (error)
15135afe0c99SJohn Baldwin 			return (error);
1514df8bae1dSRodney W. Grimes 	}
1515a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
15165afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
15175afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1518a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1519a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1520a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1521a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
15225afe0c99SJohn Baldwin 	}
15235afe0c99SJohn Baldwin 	if (uap->oss != NULL)
15245afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
15255afe0c99SJohn Baldwin 
1526fb99ab88SMatthew Dillon 	return (error);
1527df8bae1dSRodney W. Grimes }
15281930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1529df8bae1dSRodney W. Grimes 
1530d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1531df8bae1dSRodney W. Grimes struct sigaltstack_args {
15322c42a146SMarcel Moolenaar 	stack_t	*ss;
15332c42a146SMarcel Moolenaar 	stack_t	*oss;
1534df8bae1dSRodney W. Grimes };
1535d2d3e875SBruce Evans #endif
1536df8bae1dSRodney W. Grimes /* ARGSUSED */
153726f9a767SRodney W. Grimes int
15388451d0ddSKip Macy sys_sigaltstack(td, uap)
1539b40ce416SJulian Elischer 	struct thread *td;
1540df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1541df8bae1dSRodney W. Grimes {
15428f19eb88SIan Dowse 	stack_t ss, oss;
15438f19eb88SIan Dowse 	int error;
15448f19eb88SIan Dowse 
15458f19eb88SIan Dowse 	if (uap->ss != NULL) {
15468f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
15478f19eb88SIan Dowse 		if (error)
15488f19eb88SIan Dowse 			return (error);
15498f19eb88SIan Dowse 	}
15508f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
15518f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
15528f19eb88SIan Dowse 	if (error)
15538f19eb88SIan Dowse 		return (error);
15548f19eb88SIan Dowse 	if (uap->oss != NULL)
15558f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
15568f19eb88SIan Dowse 	return (error);
15578f19eb88SIan Dowse }
15588f19eb88SIan Dowse 
15598f19eb88SIan Dowse int
15608f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
15618f19eb88SIan Dowse {
1562b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1563fb99ab88SMatthew Dillon 	int oonstack;
1564fb99ab88SMatthew Dillon 
1565b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1566d034d459SMarcel Moolenaar 
15678f19eb88SIan Dowse 	if (oss != NULL) {
1568a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1569a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1570d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1571df8bae1dSRodney W. Grimes 	}
1572d034d459SMarcel Moolenaar 
15738f19eb88SIan Dowse 	if (ss != NULL) {
1574a30ec4b9SDavid Xu 		if (oonstack)
1575cf60731bSJohn Baldwin 			return (EPERM);
1576a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1577cf60731bSJohn Baldwin 			return (EINVAL);
15788f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
15799104847fSDavid Xu 			if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1580cf60731bSJohn Baldwin 				return (ENOMEM);
15819104847fSDavid Xu 
1582a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1583a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1584628d2653SJohn Baldwin 		} else {
1585a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1586628d2653SJohn Baldwin 		}
1587d034d459SMarcel Moolenaar 	}
1588cf60731bSJohn Baldwin 	return (0);
1589df8bae1dSRodney W. Grimes }
1590df8bae1dSRodney W. Grimes 
1591d93f860cSPoul-Henning Kamp /*
1592d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1593d93f860cSPoul-Henning Kamp  * cp is calling process.
1594d93f860cSPoul-Henning Kamp  */
159537c84183SPoul-Henning Kamp static int
1596a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1597d93f860cSPoul-Henning Kamp {
1598a3de221dSKonstantin Belousov 	struct proc *p;
1599d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
16003d74f47bSEitan Adler 	int err;
16013d74f47bSEitan Adler 	int ret;
1602d93f860cSPoul-Henning Kamp 
16033d74f47bSEitan Adler 	ret = ESRCH;
1604553629ebSJake Burkholder 	if (all) {
1605d93f860cSPoul-Henning Kamp 		/*
1606d93f860cSPoul-Henning Kamp 		 * broadcast
1607d93f860cSPoul-Henning Kamp 		 */
16081005a129SJohn Baldwin 		sx_slock(&allproc_lock);
16094f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
1610628d2653SJohn Baldwin 			PROC_LOCK(p);
16119c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
161295f16c1eSPaul Saab 			    p == td->td_proc || p->p_state == PRS_NEW) {
1613628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1614628d2653SJohn Baldwin 				continue;
1615628d2653SJohn Baldwin 			}
16163d74f47bSEitan Adler 			err = p_cansignal(td, p, sig);
16173d74f47bSEitan Adler 			if (err == 0) {
161833a9ed9dSJohn Baldwin 				if (sig)
1619a3de221dSKonstantin Belousov 					pksignal(p, sig, ksi);
16203d74f47bSEitan Adler 				ret = err;
1621628d2653SJohn Baldwin 			}
16223d74f47bSEitan Adler 			else if (ret == ESRCH)
16233d74f47bSEitan Adler 				ret = err;
162433a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1625d93f860cSPoul-Henning Kamp 		}
16261005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1627553629ebSJake Burkholder 	} else {
1628ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1629f591779bSSeigo Tanimura 		if (pgid == 0) {
1630d93f860cSPoul-Henning Kamp 			/*
1631d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1632d93f860cSPoul-Henning Kamp 			 */
16339c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1634f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1635f591779bSSeigo Tanimura 		} else {
1636d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1637f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1638ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1639d93f860cSPoul-Henning Kamp 				return (ESRCH);
1640d93f860cSPoul-Henning Kamp 			}
1641f591779bSSeigo Tanimura 		}
1642ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
16432e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1644628d2653SJohn Baldwin 			PROC_LOCK(p);
164595f16c1eSPaul Saab 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
164695f16c1eSPaul Saab 			    p->p_state == PRS_NEW) {
1647628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1648628d2653SJohn Baldwin 				continue;
1649628d2653SJohn Baldwin 			}
16503d74f47bSEitan Adler 			err = p_cansignal(td, p, sig);
16513d74f47bSEitan Adler 			if (err == 0) {
165233a9ed9dSJohn Baldwin 				if (sig)
1653a3de221dSKonstantin Belousov 					pksignal(p, sig, ksi);
16543d74f47bSEitan Adler 				ret = err;
1655628d2653SJohn Baldwin 			}
16563d74f47bSEitan Adler 			else if (ret == ESRCH)
16573d74f47bSEitan Adler 				ret = err;
165833a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1659d93f860cSPoul-Henning Kamp 		}
1660f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1661d93f860cSPoul-Henning Kamp 	}
16623d74f47bSEitan Adler 	return (ret);
1663d93f860cSPoul-Henning Kamp }
1664d93f860cSPoul-Henning Kamp 
1665d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1666df8bae1dSRodney W. Grimes struct kill_args {
1667df8bae1dSRodney W. Grimes 	int	pid;
1668df8bae1dSRodney W. Grimes 	int	signum;
1669df8bae1dSRodney W. Grimes };
1670d2d3e875SBruce Evans #endif
1671df8bae1dSRodney W. Grimes /* ARGSUSED */
167226f9a767SRodney W. Grimes int
16738451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap)
1674df8bae1dSRodney W. Grimes {
1675a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1676a3de221dSKonstantin Belousov 	struct proc *p;
167790af4afaSJohn Baldwin 	int error;
1678df8bae1dSRodney W. Grimes 
16798890f5d0SPawel Jakub Dawidek 	/*
16808890f5d0SPawel Jakub Dawidek 	 * A process in capability mode can send signals only to himself.
16818890f5d0SPawel Jakub Dawidek 	 * The main rationale behind this is that abort(3) is implemented as
16828890f5d0SPawel Jakub Dawidek 	 * kill(getpid(), SIGABRT).
16838890f5d0SPawel Jakub Dawidek 	 */
16848890f5d0SPawel Jakub Dawidek 	if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid)
16858890f5d0SPawel Jakub Dawidek 		return (ECAPMODE);
16868890f5d0SPawel Jakub Dawidek 
168714961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->signum);
168814961ba7SRobert Watson 	AUDIT_ARG_PID(uap->pid);
16896c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1690df8bae1dSRodney W. Grimes 		return (EINVAL);
1691fb99ab88SMatthew Dillon 
1692a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
1693a3de221dSKonstantin Belousov 	ksi.ksi_signo = uap->signum;
1694a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1695a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1696a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1697a3de221dSKonstantin Belousov 
1698df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1699df8bae1dSRodney W. Grimes 		/* kill single process */
17000b011ea3SPawel Jakub Dawidek 		if ((p = pfind(uap->pid)) == NULL) {
170124b2151fSPawel Jakub Dawidek 			if ((p = zpfind(uap->pid)) == NULL)
170290af4afaSJohn Baldwin 				return (ESRCH);
17030b011ea3SPawel Jakub Dawidek 		}
170414961ba7SRobert Watson 		AUDIT_ARG_PROCESS(p);
17051a88a252SMaxim Sobolev 		error = p_cansignal(td, p, uap->signum);
170690af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1707a3de221dSKonstantin Belousov 			pksignal(p, uap->signum, &ksi);
1708628d2653SJohn Baldwin 		PROC_UNLOCK(p);
170990af4afaSJohn Baldwin 		return (error);
1710df8bae1dSRodney W. Grimes 	}
1711df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1712df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
1713a3de221dSKonstantin Belousov 		return (killpg1(td, uap->signum, 0, 1, &ksi));
1714df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
1715a3de221dSKonstantin Belousov 		return (killpg1(td, uap->signum, 0, 0, &ksi));
1716df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
1717a3de221dSKonstantin Belousov 		return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1718df8bae1dSRodney W. Grimes 	}
171990af4afaSJohn Baldwin 	/* NOTREACHED */
1720df8bae1dSRodney W. Grimes }
1721df8bae1dSRodney W. Grimes 
1722cfb5f768SJonathan Anderson int
17238451d0ddSKip Macy sys_pdkill(td, uap)
1724cfb5f768SJonathan Anderson 	struct thread *td;
1725cfb5f768SJonathan Anderson 	struct pdkill_args *uap;
1726cfb5f768SJonathan Anderson {
1727cfb5f768SJonathan Anderson #ifdef PROCDESC
1728cfb5f768SJonathan Anderson 	struct proc *p;
1729cfb5f768SJonathan Anderson 	int error;
1730cfb5f768SJonathan Anderson 
1731cfb5f768SJonathan Anderson 	AUDIT_ARG_SIGNUM(uap->signum);
1732cfb5f768SJonathan Anderson 	AUDIT_ARG_FD(uap->fd);
1733cfb5f768SJonathan Anderson 	if ((u_int)uap->signum > _SIG_MAXSIG)
1734cfb5f768SJonathan Anderson 		return (EINVAL);
1735cfb5f768SJonathan Anderson 
1736cfb5f768SJonathan Anderson 	error = procdesc_find(td, uap->fd, CAP_PDKILL, &p);
1737cfb5f768SJonathan Anderson 	if (error)
1738cfb5f768SJonathan Anderson 		return (error);
1739cfb5f768SJonathan Anderson 	AUDIT_ARG_PROCESS(p);
1740cfb5f768SJonathan Anderson 	error = p_cansignal(td, p, uap->signum);
1741cfb5f768SJonathan Anderson 	if (error == 0 && uap->signum)
17428451d0ddSKip Macy 		kern_psignal(p, uap->signum);
1743cfb5f768SJonathan Anderson 	PROC_UNLOCK(p);
1744cfb5f768SJonathan Anderson 	return (error);
1745cfb5f768SJonathan Anderson #else
1746cfb5f768SJonathan Anderson 	return (ENOSYS);
1747cfb5f768SJonathan Anderson #endif
1748cfb5f768SJonathan Anderson }
1749cfb5f768SJonathan Anderson 
17501930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1751d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1752df8bae1dSRodney W. Grimes struct okillpg_args {
1753df8bae1dSRodney W. Grimes 	int	pgid;
1754df8bae1dSRodney W. Grimes 	int	signum;
1755df8bae1dSRodney W. Grimes };
1756d2d3e875SBruce Evans #endif
1757df8bae1dSRodney W. Grimes /* ARGSUSED */
175826f9a767SRodney W. Grimes int
1759a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap)
1760df8bae1dSRodney W. Grimes {
1761a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1762df8bae1dSRodney W. Grimes 
176314961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->signum);
176414961ba7SRobert Watson 	AUDIT_ARG_PID(uap->pgid);
17656c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1766df8bae1dSRodney W. Grimes 		return (EINVAL);
17679104847fSDavid Xu 
1768a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
1769a3de221dSKonstantin Belousov 	ksi.ksi_signo = uap->signum;
1770a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_USER;
1771a3de221dSKonstantin Belousov 	ksi.ksi_pid = td->td_proc->p_pid;
1772a3de221dSKonstantin Belousov 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1773a3de221dSKonstantin Belousov 	return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1774df8bae1dSRodney W. Grimes }
17751930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1776df8bae1dSRodney W. Grimes 
17779104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
17789104847fSDavid Xu struct sigqueue_args {
17799104847fSDavid Xu 	pid_t pid;
17809104847fSDavid Xu 	int signum;
17819104847fSDavid Xu 	/* union sigval */ void *value;
17829104847fSDavid Xu };
17839104847fSDavid Xu #endif
17849104847fSDavid Xu int
17858451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
17869104847fSDavid Xu {
17879104847fSDavid Xu 	ksiginfo_t ksi;
17889104847fSDavid Xu 	struct proc *p;
17899104847fSDavid Xu 	int error;
17909104847fSDavid Xu 
17919104847fSDavid Xu 	if ((u_int)uap->signum > _SIG_MAXSIG)
17929104847fSDavid Xu 		return (EINVAL);
17939104847fSDavid Xu 
17949104847fSDavid Xu 	/*
17959104847fSDavid Xu 	 * Specification says sigqueue can only send signal to
17969104847fSDavid Xu 	 * single process.
17979104847fSDavid Xu 	 */
17989104847fSDavid Xu 	if (uap->pid <= 0)
17999104847fSDavid Xu 		return (EINVAL);
18009104847fSDavid Xu 
18019104847fSDavid Xu 	if ((p = pfind(uap->pid)) == NULL) {
18029104847fSDavid Xu 		if ((p = zpfind(uap->pid)) == NULL)
18039104847fSDavid Xu 			return (ESRCH);
18049104847fSDavid Xu 	}
18059104847fSDavid Xu 	error = p_cansignal(td, p, uap->signum);
18069104847fSDavid Xu 	if (error == 0 && uap->signum != 0) {
18079104847fSDavid Xu 		ksiginfo_init(&ksi);
1808a3de221dSKonstantin Belousov 		ksi.ksi_flags = KSI_SIGQ;
18099104847fSDavid Xu 		ksi.ksi_signo = uap->signum;
18109104847fSDavid Xu 		ksi.ksi_code = SI_QUEUE;
18119104847fSDavid Xu 		ksi.ksi_pid = td->td_proc->p_pid;
18129104847fSDavid Xu 		ksi.ksi_uid = td->td_ucred->cr_ruid;
18138f0371f1SDavid Xu 		ksi.ksi_value.sival_ptr = uap->value;
1814ad6eec7bSJohn Baldwin 		error = pksignal(p, ksi.ksi_signo, &ksi);
18159104847fSDavid Xu 	}
18169104847fSDavid Xu 	PROC_UNLOCK(p);
18179104847fSDavid Xu 	return (error);
18189104847fSDavid Xu }
18199104847fSDavid Xu 
1820df8bae1dSRodney W. Grimes /*
1821df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1822df8bae1dSRodney W. Grimes  */
1823df8bae1dSRodney W. Grimes void
1824a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi)
1825df8bae1dSRodney W. Grimes {
1826df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1827df8bae1dSRodney W. Grimes 
1828f591779bSSeigo Tanimura 	if (pgid != 0) {
1829ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1830f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1831ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1832f591779bSSeigo Tanimura 		if (pgrp != NULL) {
1833a3de221dSKonstantin Belousov 			pgsignal(pgrp, sig, 0, ksi);
1834f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1835f591779bSSeigo Tanimura 		}
1836f591779bSSeigo Tanimura 	}
1837df8bae1dSRodney W. Grimes }
1838df8bae1dSRodney W. Grimes 
1839df8bae1dSRodney W. Grimes /*
1840df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1841df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1842df8bae1dSRodney W. Grimes  */
1843df8bae1dSRodney W. Grimes void
1844a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1845df8bae1dSRodney W. Grimes {
1846a3de221dSKonstantin Belousov 	struct proc *p;
1847df8bae1dSRodney W. Grimes 
1848628d2653SJohn Baldwin 	if (pgrp) {
1849f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1850628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1851628d2653SJohn Baldwin 			PROC_LOCK(p);
1852e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
1853e806d352SJohn Baldwin 			    (checkctty == 0 || p->p_flag & P_CONTROLT))
1854a3de221dSKonstantin Belousov 				pksignal(p, sig, ksi);
1855628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1856628d2653SJohn Baldwin 		}
1857628d2653SJohn Baldwin 	}
1858df8bae1dSRodney W. Grimes }
1859df8bae1dSRodney W. Grimes 
1860df8bae1dSRodney W. Grimes /*
18610c14ff0eSRobert Watson  * Send a signal caused by a trap to the current thread.  If it will be
18620c14ff0eSRobert Watson  * caught immediately, deliver it with correct code.  Otherwise, post it
18630c14ff0eSRobert Watson  * normally.
1864df8bae1dSRodney W. Grimes  */
1865df8bae1dSRodney W. Grimes void
18669104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi)
1867df8bae1dSRodney W. Grimes {
18681bf4700bSJeff Roberson 	struct sigacts *ps;
1869d6e029adSKonstantin Belousov 	sigset_t mask;
18701bf4700bSJeff Roberson 	struct proc *p;
18719104847fSDavid Xu 	int sig;
18729104847fSDavid Xu 	int code;
18731bf4700bSJeff Roberson 
18741bf4700bSJeff Roberson 	p = td->td_proc;
18759104847fSDavid Xu 	sig = ksi->ksi_signo;
18769104847fSDavid Xu 	code = ksi->ksi_code;
18779104847fSDavid Xu 	KASSERT(_SIG_VALID(sig), ("invalid signal"));
18789104847fSDavid Xu 
1879628d2653SJohn Baldwin 	PROC_LOCK(p);
1880ef3dab76STim J. Robbins 	ps = p->p_sigacts;
188190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
188290af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
18834093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
18841c4bcd05SJeff Roberson 		td->td_ru.ru_nsignals++;
1885df8bae1dSRodney W. Grimes #ifdef KTRACE
1886374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1887374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
18884093529dSJeff Roberson 			    &td->td_sigmask, code);
1889df8bae1dSRodney W. Grimes #endif
18909104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
18919104847fSDavid Xu 				ksi, &td->td_sigmask);
189280a8b0f3SKonstantin Belousov 		mask = ps->ps_catchmask[_SIG_IDX(sig)];
189380a8b0f3SKonstantin Belousov 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
1894d6e029adSKonstantin Belousov 			SIGADDSET(mask, sig);
189580845402SKonstantin Belousov 		kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
189680a8b0f3SKonstantin Belousov 		    SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
18972c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1898289ccde0SPeter Wemm 			/*
18998f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1900289ccde0SPeter Wemm 			 */
190190af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
19022c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
19032c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
190490af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
19052c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1906dedc04feSPeter Wemm 		}
190790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
19086f841fb7SMarcel Moolenaar 	} else {
1909f71a882fSDavid Xu 		/*
1910f71a882fSDavid Xu 		 * Avoid a possible infinite loop if the thread
1911f71a882fSDavid Xu 		 * masking the signal or process is ignoring the
1912f71a882fSDavid Xu 		 * signal.
1913f71a882fSDavid Xu 		 */
1914f71a882fSDavid Xu 		if (kern_forcesigexit &&
1915f71a882fSDavid Xu 		    (SIGISMEMBER(td->td_sigmask, sig) ||
1916f71a882fSDavid Xu 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1917f71a882fSDavid Xu 			SIGDELSET(td->td_sigmask, sig);
1918f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigcatch, sig);
1919f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigignore, sig);
1920f71a882fSDavid Xu 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1921f71a882fSDavid Xu 		}
192290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
19236626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
19242c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
1925ad6eec7bSJohn Baldwin 		tdsendsignal(p, td, sig, ksi);
1926df8bae1dSRodney W. Grimes 	}
1927628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1928df8bae1dSRodney W. Grimes }
1929df8bae1dSRodney W. Grimes 
19304093529dSJeff Roberson static struct thread *
19314093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
19324093529dSJeff Roberson {
19333074d1b4SDavid Xu 	struct thread *td, *signal_td;
19344093529dSJeff Roberson 
19354093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
19364093529dSJeff Roberson 
19374093529dSJeff Roberson 	/*
1938627451c1SDavid Xu 	 * Check if current thread can handle the signal without
193915b7a831SKonstantin Belousov 	 * switching context to another thread.
19404093529dSJeff Roberson 	 */
1941627451c1SDavid Xu 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1942627451c1SDavid Xu 		return (curthread);
19433074d1b4SDavid Xu 	signal_td = NULL;
19443074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
19453074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
19463074d1b4SDavid Xu 			signal_td = td;
1947627451c1SDavid Xu 			break;
19483074d1b4SDavid Xu 		}
19493074d1b4SDavid Xu 	}
19503074d1b4SDavid Xu 	if (signal_td == NULL)
19513074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
19523074d1b4SDavid Xu 	return (signal_td);
19534093529dSJeff Roberson }
19544093529dSJeff Roberson 
1955df8bae1dSRodney W. Grimes /*
1956df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1957df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1958df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1959df8bae1dSRodney W. Grimes  *
1960df8bae1dSRodney W. Grimes  * Exceptions:
1961df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1962df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1963df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1964df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1965df8bae1dSRodney W. Grimes  *
1966df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
19674dec0e67SRobert Watson  *
19684dec0e67SRobert Watson  * NB: This function may be entered from the debugger via the "kill" DDB
19694dec0e67SRobert Watson  * command.  There is little that can be done to mitigate the possibly messy
19704dec0e67SRobert Watson  * side effects of this unwise possibility.
1971df8bae1dSRodney W. Grimes  */
1972df8bae1dSRodney W. Grimes void
19738451d0ddSKip Macy kern_psignal(struct proc *p, int sig)
1974df8bae1dSRodney W. Grimes {
1975a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
1976a3de221dSKonstantin Belousov 
1977a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
1978a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
1979a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
1980ad6eec7bSJohn Baldwin 	(void) tdsendsignal(p, NULL, sig, &ksi);
1981a3de221dSKonstantin Belousov }
1982a3de221dSKonstantin Belousov 
1983ad6eec7bSJohn Baldwin int
1984a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1985a3de221dSKonstantin Belousov {
1986a3de221dSKonstantin Belousov 
1987ad6eec7bSJohn Baldwin 	return (tdsendsignal(p, NULL, sig, ksi));
19889104847fSDavid Xu }
19899104847fSDavid Xu 
1990cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */
19919104847fSDavid Xu int
1992cf7d9a8cSDavid Xu sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
19939104847fSDavid Xu {
1994cf7d9a8cSDavid Xu 	struct thread *td;
199541b3077aSJacques Vidrine 
19966d7b314bSDavid Xu 	if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1997cf7d9a8cSDavid Xu 		td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
19986d7b314bSDavid Xu 		if (td == NULL)
19996d7b314bSDavid Xu 			return (ESRCH);
2000cf7d9a8cSDavid Xu 		*ttd = td;
2001cf7d9a8cSDavid Xu 	} else {
2002cf7d9a8cSDavid Xu 		*ttd = NULL;
2003cf7d9a8cSDavid Xu 		PROC_LOCK(p);
20046d7b314bSDavid Xu 	}
2005cf7d9a8cSDavid Xu 	return (0);
20064093529dSJeff Roberson }
20074093529dSJeff Roberson 
2008ad6eec7bSJohn Baldwin void
2009ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig)
2010ad6eec7bSJohn Baldwin {
2011ad6eec7bSJohn Baldwin 	ksiginfo_t ksi;
2012ad6eec7bSJohn Baldwin 
2013ad6eec7bSJohn Baldwin 	ksiginfo_init(&ksi);
2014ad6eec7bSJohn Baldwin 	ksi.ksi_signo = sig;
2015ad6eec7bSJohn Baldwin 	ksi.ksi_code = SI_KERNEL;
2016ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, &ksi);
2017ad6eec7bSJohn Baldwin }
2018ad6eec7bSJohn Baldwin 
2019ad6eec7bSJohn Baldwin void
2020ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2021ad6eec7bSJohn Baldwin {
2022ad6eec7bSJohn Baldwin 
2023ad6eec7bSJohn Baldwin 	(void) tdsendsignal(td->td_proc, td, sig, ksi);
2024ad6eec7bSJohn Baldwin }
2025ad6eec7bSJohn Baldwin 
2026cf7d9a8cSDavid Xu int
2027ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
20284093529dSJeff Roberson {
20299104847fSDavid Xu 	sig_t action;
20309104847fSDavid Xu 	sigqueue_t *sigqueue;
20319104847fSDavid Xu 	int prop;
203290af4afaSJohn Baldwin 	struct sigacts *ps;
203394f0972bSDavid Xu 	int intrval;
20349104847fSDavid Xu 	int ret = 0;
2035da7bbd2cSJohn Baldwin 	int wakeup_swapper;
2036df8bae1dSRodney W. Grimes 
2037cf7d9a8cSDavid Xu 	MPASS(td == NULL || p == td->td_proc);
20386d7b314bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
20396d7b314bSDavid Xu 
204041b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
2041212bc4b3SDavid Xu 		panic("%s(): invalid signal %d", __func__, sig);
20424093529dSJeff Roberson 
2043212bc4b3SDavid Xu 	KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
20446d7b314bSDavid Xu 
20456d7b314bSDavid Xu 	/*
20466d7b314bSDavid Xu 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
20476d7b314bSDavid Xu 	 */
20486d7b314bSDavid Xu 	if (p->p_state == PRS_ZOMBIE) {
20496d7b314bSDavid Xu 		if (ksi && (ksi->ksi_flags & KSI_INS))
20506d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
20516d7b314bSDavid Xu 		return (ret);
20526d7b314bSDavid Xu 	}
20536d7b314bSDavid Xu 
205490af4afaSJohn Baldwin 	ps = p->p_sigacts;
2055ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
20562c42a146SMarcel Moolenaar 	prop = sigprop(sig);
20574093529dSJeff Roberson 
20586d7b314bSDavid Xu 	if (td == NULL) {
20596d7b314bSDavid Xu 		td = sigtd(p, sig, prop);
20606d7b314bSDavid Xu 		sigqueue = &p->p_sigqueue;
2061462314b3SMateusz Guzik 	} else
20629104847fSDavid Xu 		sigqueue = &td->td_sigqueue;
20634093529dSJeff Roberson 
20645d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
20655d217f17SJohn Birrell 
2066df8bae1dSRodney W. Grimes 	/*
2067df8bae1dSRodney W. Grimes 	 * If the signal is being ignored,
2068df8bae1dSRodney W. Grimes 	 * then we forget about it immediately.
206990af4afaSJohn Baldwin 	 * (Note: we don't set SIGCONT in ps_sigignore,
2070df8bae1dSRodney W. Grimes 	 * and if it is set to SIG_IGN,
2071df8bae1dSRodney W. Grimes 	 * action will be SIG_DFL here.)
2072df8bae1dSRodney W. Grimes 	 */
207390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
20740fc32899SJohn Baldwin 	if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2075c241c5e4SSergey Kandaurov 		SDT_PROBE(proc, kernel, , signal_discard, td, p, sig, 0, 0 );
20765d217f17SJohn Birrell 
207790af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
20786d7b314bSDavid Xu 		if (ksi && (ksi->ksi_flags & KSI_INS))
20796d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
20809104847fSDavid Xu 		return (ret);
208190af4afaSJohn Baldwin 	}
20826675b36eSDavid Xu 	if (SIGISMEMBER(td->td_sigmask, sig))
2083df8bae1dSRodney W. Grimes 		action = SIG_HOLD;
208490af4afaSJohn Baldwin 	else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2085df8bae1dSRodney W. Grimes 		action = SIG_CATCH;
2086df8bae1dSRodney W. Grimes 	else
2087df8bae1dSRodney W. Grimes 		action = SIG_DFL;
208894f0972bSDavid Xu 	if (SIGISMEMBER(ps->ps_sigintr, sig))
208994f0972bSDavid Xu 		intrval = EINTR;
209094f0972bSDavid Xu 	else
209194f0972bSDavid Xu 		intrval = ERESTART;
209290af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
2093df8bae1dSRodney W. Grimes 
20949104847fSDavid Xu 	if (prop & SA_CONT)
20959104847fSDavid Xu 		sigqueue_delete_stopmask_proc(p);
20969104847fSDavid Xu 	else if (prop & SA_STOP) {
2097df8bae1dSRodney W. Grimes 		/*
2098df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
2099df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
2100df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
2101df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
2102df8bae1dSRodney W. Grimes 		 */
2103e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
2104e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
21056d7b314bSDavid Xu 		    (action == SIG_DFL)) {
21066d7b314bSDavid Xu 			if (ksi && (ksi->ksi_flags & KSI_INS))
21076d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
21089104847fSDavid Xu 			return (ret);
21096d7b314bSDavid Xu 		}
21109104847fSDavid Xu 		sigqueue_delete_proc(p, SIGCONT);
2111ebceaf6dSDavid Xu 		if (p->p_flag & P_CONTINUED) {
21126933e3c1SJulian Elischer 			p->p_flag &= ~P_CONTINUED;
2113ebceaf6dSDavid Xu 			PROC_LOCK(p->p_pptr);
2114ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
2115ebceaf6dSDavid Xu 			PROC_UNLOCK(p->p_pptr);
2116ebceaf6dSDavid Xu 		}
2117df8bae1dSRodney W. Grimes 	}
21183074d1b4SDavid Xu 
21199104847fSDavid Xu 	ret = sigqueue_add(sigqueue, sig, ksi);
21209104847fSDavid Xu 	if (ret != 0)
21219104847fSDavid Xu 		return (ret);
21226d7b314bSDavid Xu 	signotify(td);
21235312b1c7SDavid Xu 	/*
21245312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
21255312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
21265312b1c7SDavid Xu 	 */
21275312b1c7SDavid Xu 	if (action == SIG_HOLD &&
21285312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
21299104847fSDavid Xu 		return (ret);
2130df8bae1dSRodney W. Grimes 	/*
213190d75f78SAlfred Perlstein 	 * SIGKILL: Remove procfs STOPEVENTs.
213290d75f78SAlfred Perlstein 	 */
213390d75f78SAlfred Perlstein 	if (sig == SIGKILL) {
213490d75f78SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCBIC */
213590d75f78SAlfred Perlstein 		p->p_stops = 0;
213690d75f78SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCCONT */
213790d75f78SAlfred Perlstein 		p->p_step = 0;
213890d75f78SAlfred Perlstein 		wakeup(&p->p_step);
213990d75f78SAlfred Perlstein 	}
214090d75f78SAlfred Perlstein 	/*
2141e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
2142e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
2143e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
21441968f37bSJohn Baldwin 	 * times when processing needs to be done immediately, such as
2145e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
21461968f37bSJohn Baldwin 	 * We try to do the per-process part here.
2147df8bae1dSRodney W. Grimes 	 */
2148e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
21490f14f15bSJohn Baldwin 		KASSERT(!(p->p_flag & P_WEXIT),
21500f14f15bSJohn Baldwin 		    ("signal to stopped but exiting process"));
2151e602ba25SJulian Elischer 		if (sig == SIGKILL) {
2152137cf33dSDavid Xu 			/*
2153137cf33dSDavid Xu 			 * If traced process is already stopped,
2154137cf33dSDavid Xu 			 * then no further action is necessary.
2155137cf33dSDavid Xu 			 */
215683b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
215783b718ebSDavid Xu 				goto out;
2158df8bae1dSRodney W. Grimes 			/*
2159e602ba25SJulian Elischer 			 * SIGKILL sets process running.
2160e602ba25SJulian Elischer 			 * It will die elsewhere.
2161e602ba25SJulian Elischer 			 * All threads must be restarted.
2162df8bae1dSRodney W. Grimes 			 */
2163482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
2164e602ba25SJulian Elischer 			goto runfast;
2165e602ba25SJulian Elischer 		}
2166e602ba25SJulian Elischer 
2167e602ba25SJulian Elischer 		if (prop & SA_CONT) {
2168137cf33dSDavid Xu 			/*
2169137cf33dSDavid Xu 			 * If traced process is already stopped,
2170137cf33dSDavid Xu 			 * then no further action is necessary.
2171137cf33dSDavid Xu 			 */
217283b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
217383b718ebSDavid Xu 				goto out;
2174e602ba25SJulian Elischer 			/*
2175e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
21769104847fSDavid Xu 			 * process but don't leave the signal in sigqueue as
21771d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
2178e602ba25SJulian Elischer 			 * continue the process and leave the signal in
21799104847fSDavid Xu 			 * sigqueue.  If the process catches SIGCONT, let it
2180e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
2181e602ba25SJulian Elischer 			 * an event, it goes back to run state.
2182e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
2183e602ba25SJulian Elischer 			 */
21841279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
21857b4a950aSDavid Xu 			PROC_SLOCK(p);
2186ebceaf6dSDavid Xu 			if (p->p_numthreads == p->p_suspcount) {
21877b4a950aSDavid Xu 				PROC_SUNLOCK(p);
21886933e3c1SJulian Elischer 				p->p_flag |= P_CONTINUED;
21897f96995eSDavid Xu 				p->p_xstat = SIGCONT;
21907f96995eSDavid Xu 				PROC_LOCK(p->p_pptr);
2191ebceaf6dSDavid Xu 				childproc_continued(p);
21927f96995eSDavid Xu 				PROC_UNLOCK(p->p_pptr);
21937b4a950aSDavid Xu 				PROC_SLOCK(p);
2194ebceaf6dSDavid Xu 			}
2195e602ba25SJulian Elischer 			if (action == SIG_DFL) {
2196a54e85fdSJeff Roberson 				thread_unsuspend(p);
21977b4a950aSDavid Xu 				PROC_SUNLOCK(p);
21989104847fSDavid Xu 				sigqueue_delete(sigqueue, sig);
2199a54e85fdSJeff Roberson 				goto out;
2200a54e85fdSJeff Roberson 			}
2201a54e85fdSJeff Roberson 			if (action == SIG_CATCH) {
22028460a577SJohn Birrell 				/*
22038460a577SJohn Birrell 				 * The process wants to catch it so it needs
22048460a577SJohn Birrell 				 * to run at least one thread, but which one?
22058460a577SJohn Birrell 				 */
22067b4a950aSDavid Xu 				PROC_SUNLOCK(p);
2207e602ba25SJulian Elischer 				goto runfast;
2208e602ba25SJulian Elischer 			}
2209e602ba25SJulian Elischer 			/*
2210e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
2211e602ba25SJulian Elischer 			 */
221204774f23SJulian Elischer 			thread_unsuspend(p);
22137b4a950aSDavid Xu 			PROC_SUNLOCK(p);
2214e602ba25SJulian Elischer 			goto out;
2215e602ba25SJulian Elischer 		}
2216e602ba25SJulian Elischer 
2217e602ba25SJulian Elischer 		if (prop & SA_STOP) {
2218137cf33dSDavid Xu 			/*
2219137cf33dSDavid Xu 			 * If traced process is already stopped,
2220137cf33dSDavid Xu 			 * then no further action is necessary.
2221137cf33dSDavid Xu 			 */
222283b718ebSDavid Xu 			if (p->p_flag & P_TRACED)
222383b718ebSDavid Xu 				goto out;
2224e602ba25SJulian Elischer 			/*
2225e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
2226e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
222704774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
2228e602ba25SJulian Elischer 			 */
22291279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
22309104847fSDavid Xu 			sigqueue_delete(sigqueue, sig);
2231e602ba25SJulian Elischer 			goto out;
2232e602ba25SJulian Elischer 		}
2233e602ba25SJulian Elischer 
2234e602ba25SJulian Elischer 		/*
2235e602ba25SJulian Elischer 		 * All other kinds of signals:
2236e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
2237e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
2238e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
223904774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
2240e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
2241e602ba25SJulian Elischer 		 */
2242da7bbd2cSJohn Baldwin 		wakeup_swapper = 0;
22437b4a950aSDavid Xu 		PROC_SLOCK(p);
2244a54e85fdSJeff Roberson 		thread_lock(td);
224544f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2246da7bbd2cSJohn Baldwin 			wakeup_swapper = sleepq_abort(td, intrval);
2247a54e85fdSJeff Roberson 		thread_unlock(td);
22487b4a950aSDavid Xu 		PROC_SUNLOCK(p);
2249da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
2250da7bbd2cSJohn Baldwin 			kick_proc0();
2251df8bae1dSRodney W. Grimes 		goto out;
2252df8bae1dSRodney W. Grimes 		/*
22539a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
22549a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
2255df8bae1dSRodney W. Grimes 		 */
2256e602ba25SJulian Elischer 	} else if (p->p_state == PRS_NORMAL) {
2257ec8297bdSDavid Xu 		if (p->p_flag & P_TRACED || action == SIG_CATCH) {
225894f0972bSDavid Xu 			tdsigwakeup(td, sig, action, intrval);
2259df8bae1dSRodney W. Grimes 			goto out;
226004774f23SJulian Elischer 		}
2261ec8297bdSDavid Xu 
2262ec8297bdSDavid Xu 		MPASS(action == SIG_DFL);
2263ec8297bdSDavid Xu 
226435c32a76SDavid Xu 		if (prop & SA_STOP) {
22650f14f15bSJohn Baldwin 			if (p->p_flag & (P_PPWAIT|P_WEXIT))
226635c32a76SDavid Xu 				goto out;
2267e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
2268e574e444SDavid Xu 			p->p_xstat = sig;
22697b4a950aSDavid Xu 			PROC_SLOCK(p);
2270d8267df7SDavid Xu 			sig_suspend_threads(td, p, 1);
22714093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
2272ebceaf6dSDavid Xu 				/*
2273ebceaf6dSDavid Xu 				 * only thread sending signal to another
2274ebceaf6dSDavid Xu 				 * process can reach here, if thread is sending
2275ebceaf6dSDavid Xu 				 * signal to its process, because thread does
2276ebceaf6dSDavid Xu 				 * not suspend itself here, p_numthreads
2277ebceaf6dSDavid Xu 				 * should never be equal to p_suspcount.
2278ebceaf6dSDavid Xu 				 */
2279ebceaf6dSDavid Xu 				thread_stopped(p);
22807b4a950aSDavid Xu 				PROC_SUNLOCK(p);
22819104847fSDavid Xu 				sigqueue_delete_proc(p, p->p_xstat);
22827b4a950aSDavid Xu 			} else
22837b4a950aSDavid Xu 				PROC_SUNLOCK(p);
228435c32a76SDavid Xu 			goto out;
228535c32a76SDavid Xu 		}
2286e602ba25SJulian Elischer 	} else {
2287e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
22889104847fSDavid Xu 		sigqueue_delete(sigqueue, sig);
2289e602ba25SJulian Elischer 		goto out;
2290e602ba25SJulian Elischer 	}
2291e602ba25SJulian Elischer 
2292b40ce416SJulian Elischer 	/*
2293e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
2294e602ba25SJulian Elischer 	 * running threads.
2295b40ce416SJulian Elischer 	 */
2296e602ba25SJulian Elischer runfast:
229794f0972bSDavid Xu 	tdsigwakeup(td, sig, action, intrval);
22987b4a950aSDavid Xu 	PROC_SLOCK(p);
2299e602ba25SJulian Elischer 	thread_unsuspend(p);
23007b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2301e602ba25SJulian Elischer out:
23027b4a950aSDavid Xu 	/* If we jump here, proc slock should not be owned. */
23037b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
23049104847fSDavid Xu 	return (ret);
2305e602ba25SJulian Elischer }
2306e602ba25SJulian Elischer 
2307e602ba25SJulian Elischer /*
2308e602ba25SJulian Elischer  * The force of a signal has been directed against a single
2309e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
2310e602ba25SJulian Elischer  * out of any sleep it may be in etc.
2311e602ba25SJulian Elischer  */
2312e602ba25SJulian Elischer static void
231394f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2314e602ba25SJulian Elischer {
2315e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
2316e602ba25SJulian Elischer 	register int prop;
2317da7bbd2cSJohn Baldwin 	int wakeup_swapper;
2318e602ba25SJulian Elischer 
2319da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
23208b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2321e602ba25SJulian Elischer 	prop = sigprop(sig);
2322a4c2da15SBruce Evans 
23237b4a950aSDavid Xu 	PROC_SLOCK(p);
2324374ae2a3SJeff Roberson 	thread_lock(td);
2325e602ba25SJulian Elischer 	/*
2326aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
2327e602ba25SJulian Elischer 	 * killed in this lifetime.
2328e602ba25SJulian Elischer 	 */
23296eeb364bSJeff Roberson 	if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2330b3a4fb14SDavid Xu 		sched_prio(td, PUSER);
233180c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
2332e602ba25SJulian Elischer 		/*
2333e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
2334e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
2335e602ba25SJulian Elischer 		 * be noticed when the process returns through
2336e602ba25SJulian Elischer 		 * trap() or syscall().
2337e602ba25SJulian Elischer 		 */
233844f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
2339374ae2a3SJeff Roberson 			goto out;
2340e602ba25SJulian Elischer 		/*
2341e602ba25SJulian Elischer 		 * If SIGCONT is default (or ignored) and process is
2342e602ba25SJulian Elischer 		 * asleep, we are finished; the process should not
2343e602ba25SJulian Elischer 		 * be awakened.
2344df8bae1dSRodney W. Grimes 		 */
2345e602ba25SJulian Elischer 		if ((prop & SA_CONT) && action == SIG_DFL) {
2346a54e85fdSJeff Roberson 			thread_unlock(td);
23477b4a950aSDavid Xu 			PROC_SUNLOCK(p);
23489104847fSDavid Xu 			sigqueue_delete(&p->p_sigqueue, sig);
23494093529dSJeff Roberson 			/*
23504093529dSJeff Roberson 			 * It may be on either list in this state.
23514093529dSJeff Roberson 			 * Remove from both for now.
23524093529dSJeff Roberson 			 */
23539104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
2354aa0fa334SJulian Elischer 			return;
2355df8bae1dSRodney W. Grimes 		}
2356df8bae1dSRodney W. Grimes 
2357aa0fa334SJulian Elischer 		/*
2358a120a7a3SJohn Baldwin 		 * Don't awaken a sleeping thread for SIGSTOP if the
2359a120a7a3SJohn Baldwin 		 * STOP signal is deferred.
2360a120a7a3SJohn Baldwin 		 */
2361a120a7a3SJohn Baldwin 		if ((prop & SA_STOP) && (td->td_flags & TDF_SBDRY))
2362a120a7a3SJohn Baldwin 			goto out;
2363a120a7a3SJohn Baldwin 
2364a120a7a3SJohn Baldwin 		/*
2365a4c2da15SBruce Evans 		 * Give low priority threads a better chance to run.
2366aa0fa334SJulian Elischer 		 */
236744f3b092SJohn Baldwin 		if (td->td_priority > PUSER)
2368b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
2369ec8297bdSDavid Xu 
2370da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_abort(td, intrval);
2371a4c2da15SBruce Evans 	} else {
2372df8bae1dSRodney W. Grimes 		/*
2373a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
2374df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2375df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2376df8bae1dSRodney W. Grimes 		 */
2377a4c2da15SBruce Evans #ifdef SMP
237844f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
2379e602ba25SJulian Elischer 			forward_signal(td);
23803163861cSTor Egge #endif
23816caa8a15SJohn Baldwin 	}
2382374ae2a3SJeff Roberson out:
23837b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2384374ae2a3SJeff Roberson 	thread_unlock(td);
2385da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
2386da7bbd2cSJohn Baldwin 		kick_proc0();
2387a4c2da15SBruce Evans }
2388df8bae1dSRodney W. Grimes 
2389d8267df7SDavid Xu static void
2390d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2391d8267df7SDavid Xu {
2392d8267df7SDavid Xu 	struct thread *td2;
2393d8267df7SDavid Xu 
2394d8267df7SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
23957b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
2396d8267df7SDavid Xu 
2397d8267df7SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td2) {
2398a54e85fdSJeff Roberson 		thread_lock(td2);
2399b7edba77SJeff Roberson 		td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2400d8267df7SDavid Xu 		if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2401f33a947bSKonstantin Belousov 		    (td2->td_flags & TDF_SINTR)) {
2402f33a947bSKonstantin Belousov 			if (td2->td_flags & TDF_SBDRY) {
2403a120a7a3SJohn Baldwin 				/*
2404a120a7a3SJohn Baldwin 				 * Once a thread is asleep with
2405a120a7a3SJohn Baldwin 				 * TDF_SBDRY set, it should never
2406a120a7a3SJohn Baldwin 				 * become suspended due to this check.
2407a120a7a3SJohn Baldwin 				 */
2408a120a7a3SJohn Baldwin 				KASSERT(!TD_IS_SUSPENDED(td2),
2409a120a7a3SJohn Baldwin 				    ("thread with deferred stops suspended"));
2410f33a947bSKonstantin Belousov 			} else if (!TD_IS_SUSPENDED(td2)) {
2411d8267df7SDavid Xu 				thread_suspend_one(td2);
2412f33a947bSKonstantin Belousov 			}
2413f33a947bSKonstantin Belousov 		} else if (!TD_IS_SUSPENDED(td2)) {
2414d8267df7SDavid Xu 			if (sending || td != td2)
2415d8267df7SDavid Xu 				td2->td_flags |= TDF_ASTPENDING;
2416d8267df7SDavid Xu #ifdef SMP
2417d8267df7SDavid Xu 			if (TD_IS_RUNNING(td2) && td2 != td)
2418d8267df7SDavid Xu 				forward_signal(td2);
2419d8267df7SDavid Xu #endif
2420d8267df7SDavid Xu 		}
2421a54e85fdSJeff Roberson 		thread_unlock(td2);
2422d8267df7SDavid Xu 	}
2423d8267df7SDavid Xu }
2424d8267df7SDavid Xu 
2425cbf4e354SDavid Xu int
24264cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
24274cc9f52fSRobert Drehmel {
24284cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
24294cc9f52fSRobert Drehmel 
243030a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
24310f14f15bSJohn Baldwin 	KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
24324cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2433aa89d8cdSJohn Baldwin 	    &p->p_mtx.lock_object, "Stopping for traced signal");
24344cc9f52fSRobert Drehmel 
2435904c5ec4SDavid Xu 	td->td_dbgflags |= TDB_XSIG;
2436cbf4e354SDavid Xu 	td->td_xsig = sig;
24377b4a950aSDavid Xu 	PROC_SLOCK(p);
2438904c5ec4SDavid Xu 	while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2439cbf4e354SDavid Xu 		if (p->p_flag & P_SINGLE_EXIT) {
2440904c5ec4SDavid Xu 			td->td_dbgflags &= ~TDB_XSIG;
24417b4a950aSDavid Xu 			PROC_SUNLOCK(p);
2442cbf4e354SDavid Xu 			return (sig);
2443cbf4e354SDavid Xu 		}
2444cbf4e354SDavid Xu 		/*
2445cbf4e354SDavid Xu 		 * Just make wait() to work, the last stopped thread
2446cbf4e354SDavid Xu 		 * will win.
2447cbf4e354SDavid Xu 		 */
2448cbf4e354SDavid Xu 		p->p_xstat = sig;
2449cbf4e354SDavid Xu 		p->p_xthread = td;
2450cbf4e354SDavid Xu 		p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2451d8267df7SDavid Xu 		sig_suspend_threads(td, p, 0);
24526fa39a73SKonstantin Belousov 		if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
24536fa39a73SKonstantin Belousov 			td->td_dbgflags &= ~TDB_STOPATFORK;
24546fa39a73SKonstantin Belousov 			cv_broadcast(&p->p_dbgwait);
24556fa39a73SKonstantin Belousov 		}
2456cbf4e354SDavid Xu stopme:
2457a54e85fdSJeff Roberson 		thread_suspend_switch(td);
24587ce60f60SDavid Xu 		if (p->p_xthread == td)
24597ce60f60SDavid Xu 			p->p_xthread = NULL;
24607ce60f60SDavid Xu 		if (!(p->p_flag & P_TRACED))
2461cbf4e354SDavid Xu 			break;
2462904c5ec4SDavid Xu 		if (td->td_dbgflags & TDB_SUSPEND) {
2463cbf4e354SDavid Xu 			if (p->p_flag & P_SINGLE_EXIT)
2464cbf4e354SDavid Xu 				break;
2465cbf4e354SDavid Xu 			goto stopme;
2466cbf4e354SDavid Xu 		}
2467cbf4e354SDavid Xu 	}
24687b4a950aSDavid Xu 	PROC_SUNLOCK(p);
2469cbf4e354SDavid Xu 	return (td->td_xsig);
24704cc9f52fSRobert Drehmel }
24714cc9f52fSRobert Drehmel 
24726b286ee8SKonstantin Belousov static void
247380a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags)
24746b286ee8SKonstantin Belousov {
24756b286ee8SKonstantin Belousov 	struct sigacts *ps;
24766b286ee8SKonstantin Belousov 	struct thread *td;
2477407af02bSDavid Xu 	int sig;
24786b286ee8SKonstantin Belousov 
24796b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
2480407af02bSDavid Xu 	if (SIGISEMPTY(p->p_siglist))
2481407af02bSDavid Xu 		return;
24826b286ee8SKonstantin Belousov 	ps = p->p_sigacts;
2483407af02bSDavid Xu 	SIGSETAND(block, p->p_siglist);
2484407af02bSDavid Xu 	while ((sig = sig_ffs(&block)) != 0) {
2485407af02bSDavid Xu 		SIGDELSET(block, sig);
2486407af02bSDavid Xu 		td = sigtd(p, sig, 0);
24876b286ee8SKonstantin Belousov 		signotify(td);
248880a8b0f3SKonstantin Belousov 		if (!(flags & SIGPROCMASK_PS_LOCKED))
24896b286ee8SKonstantin Belousov 			mtx_lock(&ps->ps_mtx);
2490407af02bSDavid Xu 		if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2491407af02bSDavid Xu 			tdsigwakeup(td, sig, SIG_CATCH,
2492407af02bSDavid Xu 			    (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
24936b286ee8SKonstantin Belousov 			     ERESTART));
249480a8b0f3SKonstantin Belousov 		if (!(flags & SIGPROCMASK_PS_LOCKED))
24956b286ee8SKonstantin Belousov 			mtx_unlock(&ps->ps_mtx);
24966b286ee8SKonstantin Belousov 	}
24976b286ee8SKonstantin Belousov }
24986b286ee8SKonstantin Belousov 
24996b286ee8SKonstantin Belousov void
25006b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td)
25016b286ee8SKonstantin Belousov {
25026b286ee8SKonstantin Belousov 	struct proc *p;
25036b286ee8SKonstantin Belousov 	sigset_t unblocked;
25046b286ee8SKonstantin Belousov 
25056b286ee8SKonstantin Belousov 	p = td->td_proc;
25066b286ee8SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
25076b286ee8SKonstantin Belousov 
25086b286ee8SKonstantin Belousov 	sigqueue_flush(&td->td_sigqueue);
25096b286ee8SKonstantin Belousov 	if (p->p_numthreads == 1)
25106b286ee8SKonstantin Belousov 		return;
25116b286ee8SKonstantin Belousov 
25126b286ee8SKonstantin Belousov 	/*
25136b286ee8SKonstantin Belousov 	 * Since we cannot handle signals, notify signal post code
25146b286ee8SKonstantin Belousov 	 * about this by filling the sigmask.
25156b286ee8SKonstantin Belousov 	 *
25166b286ee8SKonstantin Belousov 	 * Also, if needed, wake up thread(s) that do not block the
25176b286ee8SKonstantin Belousov 	 * same signals as the exiting thread, since the thread might
25186b286ee8SKonstantin Belousov 	 * have been selected for delivery and woken up.
25196b286ee8SKonstantin Belousov 	 */
25206b286ee8SKonstantin Belousov 	SIGFILLSET(unblocked);
25216b286ee8SKonstantin Belousov 	SIGSETNAND(unblocked, td->td_sigmask);
25226b286ee8SKonstantin Belousov 	SIGFILLSET(td->td_sigmask);
252380a8b0f3SKonstantin Belousov 	reschedule_signals(p, unblocked, 0);
25246b286ee8SKonstantin Belousov 
25256b286ee8SKonstantin Belousov }
25266b286ee8SKonstantin Belousov 
2527593efaf9SJohn Baldwin /*
2528593efaf9SJohn Baldwin  * Defer the delivery of SIGSTOP for the current thread.  Returns true
2529593efaf9SJohn Baldwin  * if stops were deferred and false if they were already deferred.
2530593efaf9SJohn Baldwin  */
2531593efaf9SJohn Baldwin int
2532593efaf9SJohn Baldwin sigdeferstop(void)
2533a120a7a3SJohn Baldwin {
2534593efaf9SJohn Baldwin 	struct thread *td;
2535a120a7a3SJohn Baldwin 
2536593efaf9SJohn Baldwin 	td = curthread;
2537593efaf9SJohn Baldwin 	if (td->td_flags & TDF_SBDRY)
2538593efaf9SJohn Baldwin 		return (0);
2539a120a7a3SJohn Baldwin 	thread_lock(td);
2540a120a7a3SJohn Baldwin 	td->td_flags |= TDF_SBDRY;
2541a120a7a3SJohn Baldwin 	thread_unlock(td);
2542593efaf9SJohn Baldwin 	return (1);
2543a120a7a3SJohn Baldwin }
2544a120a7a3SJohn Baldwin 
2545a120a7a3SJohn Baldwin /*
2546a120a7a3SJohn Baldwin  * Permit the delivery of SIGSTOP for the current thread.  This does
2547a120a7a3SJohn Baldwin  * not immediately suspend if a stop was posted.  Instead, the thread
2548a120a7a3SJohn Baldwin  * will suspend either via ast() or a subsequent interruptible sleep.
2549a120a7a3SJohn Baldwin  */
2550a120a7a3SJohn Baldwin void
2551593efaf9SJohn Baldwin sigallowstop()
2552a120a7a3SJohn Baldwin {
2553593efaf9SJohn Baldwin 	struct thread *td;
2554a120a7a3SJohn Baldwin 
2555593efaf9SJohn Baldwin 	td = curthread;
2556a120a7a3SJohn Baldwin 	thread_lock(td);
2557a120a7a3SJohn Baldwin 	td->td_flags &= ~TDF_SBDRY;
2558a120a7a3SJohn Baldwin 	thread_unlock(td);
2559a120a7a3SJohn Baldwin }
2560a120a7a3SJohn Baldwin 
2561df8bae1dSRodney W. Grimes /*
2562df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2563df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2564df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2565df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2566df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2567628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2568df8bae1dSRodney W. Grimes  * sequence is
2569df8bae1dSRodney W. Grimes  *
2570e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
25712c42a146SMarcel Moolenaar  *		postsig(sig);
2572df8bae1dSRodney W. Grimes  */
25736711f10fSJohn Baldwin static int
25743cf3b9f0SJohn Baldwin issignal(struct thread *td)
2575df8bae1dSRodney W. Grimes {
2576e602ba25SJulian Elischer 	struct proc *p;
257790af4afaSJohn Baldwin 	struct sigacts *ps;
25786b286ee8SKonstantin Belousov 	struct sigqueue *queue;
25794093529dSJeff Roberson 	sigset_t sigpending;
25804f17d481SKonstantin Belousov 	int sig, prop, newsig;
2581df8bae1dSRodney W. Grimes 
2582e602ba25SJulian Elischer 	p = td->td_proc;
258390af4afaSJohn Baldwin 	ps = p->p_sigacts;
258490af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2585628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2586df8bae1dSRodney W. Grimes 	for (;;) {
25872a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
25882a024a2bSSean Eric Fagan 
25899104847fSDavid Xu 		sigpending = td->td_sigqueue.sq_signals;
25906b286ee8SKonstantin Belousov 		SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
25914093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
25924093529dSJeff Roberson 
2593a120a7a3SJohn Baldwin 		if (p->p_flag & P_PPWAIT || td->td_flags & TDF_SBDRY)
25944093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
25954093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2596df8bae1dSRodney W. Grimes 			return (0);
25974093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
25982a024a2bSSean Eric Fagan 
2599047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2600047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2601047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2602047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2603047aa39bSRobert Watson 		}
26042a024a2bSSean Eric Fagan 
2605df8bae1dSRodney W. Grimes 		/*
2606df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2607df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2608df8bae1dSRodney W. Grimes 		 */
260990af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
26109104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
26116b286ee8SKonstantin Belousov 			sigqueue_delete(&p->p_sigqueue, sig);
2612df8bae1dSRodney W. Grimes 			continue;
2613df8bae1dSRodney W. Grimes 		}
2614888d4d4fSKonstantin Belousov 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPTRACE) == 0) {
2615df8bae1dSRodney W. Grimes 			/*
2616d8f4f6a4SJonathan Mini 			 * If traced, always stop.
26176a671a6bSKonstantin Belousov 			 * Remove old signal from queue before the stop.
26186a671a6bSKonstantin Belousov 			 * XXX shrug off debugger, it causes siginfo to
26196a671a6bSKonstantin Belousov 			 * be thrown away.
2620df8bae1dSRodney W. Grimes 			 */
26216a671a6bSKonstantin Belousov 			queue = &td->td_sigqueue;
26228a260079SKonstantin Belousov 			td->td_dbgksi.ksi_signo = 0;
26238a260079SKonstantin Belousov 			if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
26246a671a6bSKonstantin Belousov 				queue = &p->p_sigqueue;
26258a260079SKonstantin Belousov 				sigqueue_get(queue, sig, &td->td_dbgksi);
26266a671a6bSKonstantin Belousov 			}
26276a671a6bSKonstantin Belousov 
262890af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2629cbf4e354SDavid Xu 			newsig = ptracestop(td, sig);
263090af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2631df8bae1dSRodney W. Grimes 
26329104847fSDavid Xu 			if (sig != newsig) {
26339104847fSDavid Xu 
2634df8bae1dSRodney W. Grimes 				/*
2635df8bae1dSRodney W. Grimes 				 * If parent wants us to take the signal,
2636df8bae1dSRodney W. Grimes 				 * then it will leave it in p->p_xstat;
2637df8bae1dSRodney W. Grimes 				 * otherwise we just look for signals again.
2638df8bae1dSRodney W. Grimes 				*/
2639cbf4e354SDavid Xu 				if (newsig == 0)
2640df8bae1dSRodney W. Grimes 					continue;
2641cbf4e354SDavid Xu 				sig = newsig;
26429104847fSDavid Xu 
26439104847fSDavid Xu 				/*
26449104847fSDavid Xu 				 * Put the new signal into td_sigqueue. If the
264523c6445aSPawel Jakub Dawidek 				 * signal is being masked, look for other
264623c6445aSPawel Jakub Dawidek 				 * signals.
26479104847fSDavid Xu 				 */
26486a671a6bSKonstantin Belousov 				sigqueue_add(queue, sig, NULL);
26499104847fSDavid Xu 				if (SIGISMEMBER(td->td_sigmask, sig))
26509104847fSDavid Xu 					continue;
26519104847fSDavid Xu 				signotify(td);
26526a671a6bSKonstantin Belousov 			} else {
26538a260079SKonstantin Belousov 				if (td->td_dbgksi.ksi_signo != 0) {
26548a260079SKonstantin Belousov 					td->td_dbgksi.ksi_flags |= KSI_HEAD;
26556a671a6bSKonstantin Belousov 					if (sigqueue_add(&td->td_sigqueue, sig,
26568a260079SKonstantin Belousov 					    &td->td_dbgksi) != 0)
26578a260079SKonstantin Belousov 						td->td_dbgksi.ksi_signo = 0;
26586a671a6bSKonstantin Belousov 				}
26598a260079SKonstantin Belousov 				if (td->td_dbgksi.ksi_signo == 0)
26606a671a6bSKonstantin Belousov 					sigqueue_add(&td->td_sigqueue, sig,
26616a671a6bSKonstantin Belousov 					    NULL);
26629104847fSDavid Xu 			}
26639104847fSDavid Xu 
2664df8bae1dSRodney W. Grimes 			/*
26658d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
26668d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
26678d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
26688d542cb5SDavid E. O'Brien 			 */
26698d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
26708d542cb5SDavid E. O'Brien 				continue;
2671df8bae1dSRodney W. Grimes 		}
2672df8bae1dSRodney W. Grimes 
26738d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
26748d542cb5SDavid E. O'Brien 
2675df8bae1dSRodney W. Grimes 		/*
2676df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2677df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2678df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2679df8bae1dSRodney W. Grimes 		 */
2680d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2681df8bae1dSRodney W. Grimes 
2682d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2683df8bae1dSRodney W. Grimes 			/*
2684df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2685df8bae1dSRodney W. Grimes 			 */
2686df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2687df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2688df8bae1dSRodney W. Grimes 				/*
2689df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2690df8bae1dSRodney W. Grimes 				 * in init? XXX
2691df8bae1dSRodney W. Grimes 				 */
2692d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
26932c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2694df8bae1dSRodney W. Grimes #endif
2695df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2696df8bae1dSRodney W. Grimes 			}
2697df8bae1dSRodney W. Grimes 			/*
2698df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2699df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2700df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2701df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2702df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2703df8bae1dSRodney W. Grimes 			 */
2704df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
27050f14f15bSJohn Baldwin 				if (p->p_flag & (P_TRACED|P_WEXIT) ||
2706df8bae1dSRodney W. Grimes 				    (p->p_pgrp->pg_jobc == 0 &&
2707df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2708df8bae1dSRodney W. Grimes 					break;	/* == ignore */
270990af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
271090af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2711aa89d8cdSJohn Baldwin 				    &p->p_mtx.lock_object, "Catching SIGSTOP");
2712e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
27132c42a146SMarcel Moolenaar 				p->p_xstat = sig;
27147b4a950aSDavid Xu 				PROC_SLOCK(p);
2715d8267df7SDavid Xu 				sig_suspend_threads(td, p, 0);
2716a54e85fdSJeff Roberson 				thread_suspend_switch(td);
27177b4a950aSDavid Xu 				PROC_SUNLOCK(p);
271890af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2719df8bae1dSRodney W. Grimes 				break;
272021b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2721df8bae1dSRodney W. Grimes 				/*
2722df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2723df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2724df8bae1dSRodney W. Grimes 				 */
2725df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2726df8bae1dSRodney W. Grimes 			} else
27272c42a146SMarcel Moolenaar 				return (sig);
2728df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2729df8bae1dSRodney W. Grimes 
2730d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2731df8bae1dSRodney W. Grimes 			/*
2732df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2733df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2734df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2735df8bae1dSRodney W. Grimes 			 */
2736df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2737df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2738df8bae1dSRodney W. Grimes 				printf("issignal\n");
2739df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2740df8bae1dSRodney W. Grimes 
2741df8bae1dSRodney W. Grimes 		default:
2742df8bae1dSRodney W. Grimes 			/*
2743df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2744df8bae1dSRodney W. Grimes 			 * postsig() process it.
2745df8bae1dSRodney W. Grimes 			 */
27462c42a146SMarcel Moolenaar 			return (sig);
2747df8bae1dSRodney W. Grimes 		}
27489104847fSDavid Xu 		sigqueue_delete(&td->td_sigqueue, sig);	/* take the signal! */
27496b286ee8SKonstantin Belousov 		sigqueue_delete(&p->p_sigqueue, sig);
2750df8bae1dSRodney W. Grimes 	}
2751df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2752df8bae1dSRodney W. Grimes }
2753df8bae1dSRodney W. Grimes 
2754e574e444SDavid Xu void
2755e574e444SDavid Xu thread_stopped(struct proc *p)
2756e574e444SDavid Xu {
2757e574e444SDavid Xu 	int n;
2758e574e444SDavid Xu 
2759e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
27607b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
2761e574e444SDavid Xu 	n = p->p_suspcount;
27627c9a98f1SDavid Xu 	if (p == curproc)
2763e574e444SDavid Xu 		n++;
2764e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
27657b4a950aSDavid Xu 		PROC_SUNLOCK(p);
2766407948a5SDavid Xu 		p->p_flag &= ~P_WAITED;
2767e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
27687f96995eSDavid Xu 		childproc_stopped(p, (p->p_flag & P_TRACED) ?
2769ebceaf6dSDavid Xu 			CLD_TRAPPED : CLD_STOPPED);
2770e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
27717b4a950aSDavid Xu 		PROC_SLOCK(p);
2772e574e444SDavid Xu 	}
2773e574e444SDavid Xu }
2774e574e444SDavid Xu 
2775df8bae1dSRodney W. Grimes /*
2776df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2777df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2778df8bae1dSRodney W. Grimes  */
277975c586a4SKonstantin Belousov int
27802c42a146SMarcel Moolenaar postsig(sig)
27812c42a146SMarcel Moolenaar 	register int sig;
2782df8bae1dSRodney W. Grimes {
2783b40ce416SJulian Elischer 	struct thread *td = curthread;
2784b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2785628d2653SJohn Baldwin 	struct sigacts *ps;
27862c42a146SMarcel Moolenaar 	sig_t action;
27879104847fSDavid Xu 	ksiginfo_t ksi;
2788d6e029adSKonstantin Belousov 	sigset_t returnmask, mask;
2789df8bae1dSRodney W. Grimes 
27902c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
27915526d2d9SEivind Eklund 
27922ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2793628d2653SJohn Baldwin 	ps = p->p_sigacts;
279490af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
27955da49fcbSDavid Xu 	ksiginfo_init(&ksi);
27966b286ee8SKonstantin Belousov 	if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
27976b286ee8SKonstantin Belousov 	    sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
279875c586a4SKonstantin Belousov 		return (0);
27999104847fSDavid Xu 	ksi.ksi_signo = sig;
280056c06c4bSDavid Xu 	if (ksi.ksi_code == SI_TIMER)
280156c06c4bSDavid Xu 		itimer_accept(p, ksi.ksi_timerid, &ksi);
28022c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2803df8bae1dSRodney W. Grimes #ifdef KTRACE
2804374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
28055e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
280661009552SJilles Tjoelker 		    &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2807df8bae1dSRodney W. Grimes #endif
2808047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2809047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2810047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2811047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2812047aa39bSRobert Watson 	}
28132a024a2bSSean Eric Fagan 
28148460a577SJohn Birrell 	if (action == SIG_DFL) {
2815df8bae1dSRodney W. Grimes 		/*
2816df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2817df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2818df8bae1dSRodney W. Grimes 		 */
281990af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2820b40ce416SJulian Elischer 		sigexit(td, sig);
2821df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2822df8bae1dSRodney W. Grimes 	} else {
2823df8bae1dSRodney W. Grimes 		/*
2824df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2825df8bae1dSRodney W. Grimes 		 */
28264093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
28275526d2d9SEivind Eklund 		    ("postsig action"));
2828df8bae1dSRodney W. Grimes 		/*
2829df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2830645682fdSLuoqi Chen 		 * occurrences of this signal.
2831df8bae1dSRodney W. Grimes 		 *
2832645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2833df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2834645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2835df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2836df8bae1dSRodney W. Grimes 		 */
28375e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
28384093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
28395e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2840df8bae1dSRodney W. Grimes 		} else
28414093529dSJeff Roberson 			returnmask = td->td_sigmask;
28422c42a146SMarcel Moolenaar 
284380a8b0f3SKonstantin Belousov 		mask = ps->ps_catchmask[_SIG_IDX(sig)];
284480a8b0f3SKonstantin Belousov 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
2845d6e029adSKonstantin Belousov 			SIGADDSET(mask, sig);
2846d6e029adSKonstantin Belousov 		kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
284780a8b0f3SKonstantin Belousov 		    SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
28482c42a146SMarcel Moolenaar 
28492c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2850289ccde0SPeter Wemm 			/*
28518f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2852289ccde0SPeter Wemm 			 */
285390af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
28542c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
28552c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
285690af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
28572c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2858dedc04feSPeter Wemm 		}
28591c4bcd05SJeff Roberson 		td->td_ru.ru_nsignals++;
2860c90c9021SEd Schouten 		if (p->p_sig == sig) {
28616626c604SJulian Elischer 			p->p_code = 0;
28626626c604SJulian Elischer 			p->p_sig = 0;
2863df8bae1dSRodney W. Grimes 		}
28649104847fSDavid Xu 		(*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2865df8bae1dSRodney W. Grimes 	}
286675c586a4SKonstantin Belousov 	return (1);
2867df8bae1dSRodney W. Grimes }
2868df8bae1dSRodney W. Grimes 
2869df8bae1dSRodney W. Grimes /*
2870df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2871df8bae1dSRodney W. Grimes  */
287226f9a767SRodney W. Grimes void
2873df8bae1dSRodney W. Grimes killproc(p, why)
2874df8bae1dSRodney W. Grimes 	struct proc *p;
2875df8bae1dSRodney W. Grimes 	char *why;
2876df8bae1dSRodney W. Grimes {
28779081e5e8SJohn Baldwin 
28789081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2879c3209846SPawel Jakub Dawidek 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
2880c3209846SPawel Jakub Dawidek 	    p->p_comm);
2881c3209846SPawel Jakub Dawidek 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid,
2882c3209846SPawel Jakub Dawidek 	    p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why);
28833f1c4c4fSKonstantin Belousov 	p->p_flag |= P_WKILLED;
28848451d0ddSKip Macy 	kern_psignal(p, SIGKILL);
2885df8bae1dSRodney W. Grimes }
2886df8bae1dSRodney W. Grimes 
2887df8bae1dSRodney W. Grimes /*
2888df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2889df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2890df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2891df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2892df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2893df8bae1dSRodney W. Grimes  * does not return.
2894df8bae1dSRodney W. Grimes  */
289526f9a767SRodney W. Grimes void
2896b40ce416SJulian Elischer sigexit(td, sig)
2897b40ce416SJulian Elischer 	struct thread *td;
28982c42a146SMarcel Moolenaar 	int sig;
2899df8bae1dSRodney W. Grimes {
2900b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2901df8bae1dSRodney W. Grimes 
2902628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2903df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
2904f97c3df1SDavid Schultz 	/*
2905f97c3df1SDavid Schultz 	 * We must be single-threading to generate a core dump.  This
2906f97c3df1SDavid Schultz 	 * ensures that the registers in the core file are up-to-date.
2907f97c3df1SDavid Schultz 	 * Also, the ELF dump handler assumes that the thread list doesn't
2908f97c3df1SDavid Schultz 	 * change out from under it.
2909f97c3df1SDavid Schultz 	 *
2910f97c3df1SDavid Schultz 	 * XXX If another thread attempts to single-thread before us
2911f97c3df1SDavid Schultz 	 *     (e.g. via fork()), we won't get a dump at all.
2912f97c3df1SDavid Schultz 	 */
2913f97c3df1SDavid Schultz 	if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
29142c42a146SMarcel Moolenaar 		p->p_sig = sig;
2915c364e17eSAndrey A. Chernov 		/*
2916c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2917c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2918c364e17eSAndrey A. Chernov 		 * these messages.)
2919c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
29204ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2921c364e17eSAndrey A. Chernov 		 */
2922b40ce416SJulian Elischer 		if (coredump(td) == 0)
29232c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
292457308494SJoerg Wunsch 		if (kern_logsigexit)
292557308494SJoerg Wunsch 			log(LOG_INFO,
292657308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
29273d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
29289c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
29292c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
29302c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
29314ae89b95SJohn Baldwin 	} else
2932628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2933b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2934df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2935df8bae1dSRodney W. Grimes }
2936df8bae1dSRodney W. Grimes 
2937ebceaf6dSDavid Xu /*
29387f96995eSDavid Xu  * Send queued SIGCHLD to parent when child process's state
29397f96995eSDavid Xu  * is changed.
2940ebceaf6dSDavid Xu  */
29417f96995eSDavid Xu static void
29427f96995eSDavid Xu sigparent(struct proc *p, int reason, int status)
2943ebceaf6dSDavid Xu {
2944ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2945ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2946ebceaf6dSDavid Xu 
2947ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
2948ebceaf6dSDavid Xu 		p->p_ksi->ksi_signo  = SIGCHLD;
2949ebceaf6dSDavid Xu 		p->p_ksi->ksi_code   = reason;
29507f96995eSDavid Xu 		p->p_ksi->ksi_status = status;
2951ebceaf6dSDavid Xu 		p->p_ksi->ksi_pid    = p->p_pid;
2952ebceaf6dSDavid Xu 		p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
2953ebceaf6dSDavid Xu 		if (KSI_ONQ(p->p_ksi))
2954ebceaf6dSDavid Xu 			return;
2955ebceaf6dSDavid Xu 	}
2956ad6eec7bSJohn Baldwin 	pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
2957ebceaf6dSDavid Xu }
2958ebceaf6dSDavid Xu 
29597f96995eSDavid Xu static void
29607f96995eSDavid Xu childproc_jobstate(struct proc *p, int reason, int status)
29617f96995eSDavid Xu {
29627f96995eSDavid Xu 	struct sigacts *ps;
29637f96995eSDavid Xu 
29647f96995eSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
29657f96995eSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
29667f96995eSDavid Xu 
29677f96995eSDavid Xu 	/*
29687f96995eSDavid Xu 	 * Wake up parent sleeping in kern_wait(), also send
29697f96995eSDavid Xu 	 * SIGCHLD to parent, but SIGCHLD does not guarantee
29707f96995eSDavid Xu 	 * that parent will awake, because parent may masked
29717f96995eSDavid Xu 	 * the signal.
29727f96995eSDavid Xu 	 */
29737f96995eSDavid Xu 	p->p_pptr->p_flag |= P_STATCHILD;
29747f96995eSDavid Xu 	wakeup(p->p_pptr);
29757f96995eSDavid Xu 
29767f96995eSDavid Xu 	ps = p->p_pptr->p_sigacts;
29777f96995eSDavid Xu 	mtx_lock(&ps->ps_mtx);
29787f96995eSDavid Xu 	if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
29797f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
29807f96995eSDavid Xu 		sigparent(p, reason, status);
29817f96995eSDavid Xu 	} else
29827f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
29837f96995eSDavid Xu }
29847f96995eSDavid Xu 
29857f96995eSDavid Xu void
29867f96995eSDavid Xu childproc_stopped(struct proc *p, int reason)
29877f96995eSDavid Xu {
29887f96995eSDavid Xu 	childproc_jobstate(p, reason, p->p_xstat);
29897f96995eSDavid Xu }
29907f96995eSDavid Xu 
2991ebceaf6dSDavid Xu void
2992ebceaf6dSDavid Xu childproc_continued(struct proc *p)
2993ebceaf6dSDavid Xu {
29947f96995eSDavid Xu 	childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2995ebceaf6dSDavid Xu }
2996ebceaf6dSDavid Xu 
2997ebceaf6dSDavid Xu void
2998ebceaf6dSDavid Xu childproc_exited(struct proc *p)
2999ebceaf6dSDavid Xu {
3000ebceaf6dSDavid Xu 	int reason;
3001f4d85223SDavid Xu 	int status = p->p_xstat; /* convert to int */
3002ebceaf6dSDavid Xu 
3003ebceaf6dSDavid Xu 	reason = CLD_EXITED;
3004f4d85223SDavid Xu 	if (WCOREDUMP(status))
3005ebceaf6dSDavid Xu 		reason = CLD_DUMPED;
3006f4d85223SDavid Xu 	else if (WIFSIGNALED(status))
3007ebceaf6dSDavid Xu 		reason = CLD_KILLED;
30087f96995eSDavid Xu 	/*
30097f96995eSDavid Xu 	 * XXX avoid calling wakeup(p->p_pptr), the work is
30107f96995eSDavid Xu 	 * done in exit1().
30117f96995eSDavid Xu 	 */
30127f96995eSDavid Xu 	sigparent(p, reason, status);
3013ebceaf6dSDavid Xu }
3014ebceaf6dSDavid Xu 
3015e7228204SAlfred Perlstein /*
3016e7228204SAlfred Perlstein  * We only have 1 character for the core count in the format
3017e7228204SAlfred Perlstein  * string, so the range will be 0-9
3018e7228204SAlfred Perlstein  */
3019e7228204SAlfred Perlstein #define MAX_NUM_CORES 10
3020e7228204SAlfred Perlstein static int num_cores = 5;
3021e7228204SAlfred Perlstein 
3022e7228204SAlfred Perlstein static int
3023e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3024e7228204SAlfred Perlstein {
3025e7228204SAlfred Perlstein 	int error;
3026e7228204SAlfred Perlstein 	int new_val;
3027e7228204SAlfred Perlstein 
3028c5105012SKonstantin Belousov 	new_val = num_cores;
3029e7228204SAlfred Perlstein 	error = sysctl_handle_int(oidp, &new_val, 0, req);
3030e7228204SAlfred Perlstein 	if (error != 0 || req->newptr == NULL)
3031e7228204SAlfred Perlstein 		return (error);
3032e7228204SAlfred Perlstein 	if (new_val > MAX_NUM_CORES)
3033e7228204SAlfred Perlstein 		new_val = MAX_NUM_CORES;
3034e7228204SAlfred Perlstein 	if (new_val < 0)
3035e7228204SAlfred Perlstein 		new_val = 0;
3036e7228204SAlfred Perlstein 	num_cores = new_val;
3037e7228204SAlfred Perlstein 	return (0);
3038e7228204SAlfred Perlstein }
3039e7228204SAlfred Perlstein SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
3040e7228204SAlfred Perlstein 	    0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
3041e7228204SAlfred Perlstein 
3042e7228204SAlfred Perlstein #if defined(COMPRESS_USER_CORES)
3043e7228204SAlfred Perlstein int compress_user_cores = 1;
3044e7228204SAlfred Perlstein SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW,
304529146f1aSPawel Jakub Dawidek     &compress_user_cores, 0, "Compression of user corefiles");
3046e7228204SAlfred Perlstein 
3047e7228204SAlfred Perlstein int compress_user_cores_gzlevel = -1; /* default level */
3048e7228204SAlfred Perlstein SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW,
304929146f1aSPawel Jakub Dawidek     &compress_user_cores_gzlevel, -1, "Corefile gzip compression level");
3050e7228204SAlfred Perlstein 
3051e7228204SAlfred Perlstein #define GZ_SUFFIX	".gz"
3052e7228204SAlfred Perlstein #define GZ_SUFFIX_LEN	3
3053e7228204SAlfred Perlstein #endif
3054e7228204SAlfred Perlstein 
30556d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"};
3056b62d05fcSPawel Jakub Dawidek TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3057c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
305829146f1aSPawel Jakub Dawidek     sizeof(corefilename), "Process corefile name format string");
3059c5edb423SSean Eric Fagan 
3060c5edb423SSean Eric Fagan /*
3061c345faeaSPawel Jakub Dawidek  * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3062c345faeaSPawel Jakub Dawidek  * Expand the name described in corefilename, using name, uid, and pid
3063c345faeaSPawel Jakub Dawidek  * and open/create core file.
3064c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
3065c5edb423SSean Eric Fagan  *	%N	name of process ("name")
3066c5edb423SSean Eric Fagan  *	%P	process id (pid)
3067c5edb423SSean Eric Fagan  *	%U	user id (uid)
3068c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
3069c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3070c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
3071c5edb423SSean Eric Fagan  */
3072c345faeaSPawel Jakub Dawidek static int
3073c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3074c345faeaSPawel Jakub Dawidek     int compress, struct vnode **vpp, char **namep)
30758b43b535SAlfred Perlstein {
3076c345faeaSPawel Jakub Dawidek 	struct nameidata nd;
307736b208e0SRobert Watson 	struct sbuf sb;
307836b208e0SRobert Watson 	const char *format;
3079c345faeaSPawel Jakub Dawidek 	char *hostname, *name;
3080c345faeaSPawel Jakub Dawidek 	int indexpos, i, error, cmode, flags, oflags;
3081c5edb423SSean Eric Fagan 
3082b7402d82SAlfred Perlstein 	hostname = NULL;
30838b43b535SAlfred Perlstein 	format = corefilename;
3084086053a3SPawel Jakub Dawidek 	name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3085e7228204SAlfred Perlstein 	indexpos = -1;
3086c52ff611SPawel Jakub Dawidek 	(void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
308729146f1aSPawel Jakub Dawidek 	for (i = 0; format[i] != '\0'; i++) {
3088c5edb423SSean Eric Fagan 		switch (format[i]) {
3089c5edb423SSean Eric Fagan 		case '%':	/* Format character */
3090c5edb423SSean Eric Fagan 			i++;
3091c5edb423SSean Eric Fagan 			switch (format[i]) {
3092c5edb423SSean Eric Fagan 			case '%':
309336b208e0SRobert Watson 				sbuf_putc(&sb, '%');
3094c5edb423SSean Eric Fagan 				break;
3095e7228204SAlfred Perlstein 			case 'H':	/* hostname */
3096b7402d82SAlfred Perlstein 				if (hostname == NULL) {
3097b7402d82SAlfred Perlstein 					hostname = malloc(MAXHOSTNAMELEN,
3098086053a3SPawel Jakub Dawidek 					    M_TEMP, M_WAITOK);
3099b7402d82SAlfred Perlstein 				}
3100e7228204SAlfred Perlstein 				getcredhostname(td->td_ucred, hostname,
3101b7402d82SAlfred Perlstein 				    MAXHOSTNAMELEN);
3102e7228204SAlfred Perlstein 				sbuf_printf(&sb, "%s", hostname);
3103e7228204SAlfred Perlstein 				break;
3104e7228204SAlfred Perlstein 			case 'I':	/* autoincrementing index */
3105e7228204SAlfred Perlstein 				sbuf_printf(&sb, "0");
3106e7228204SAlfred Perlstein 				indexpos = sbuf_len(&sb) - 1;
3107e7228204SAlfred Perlstein 				break;
3108c5edb423SSean Eric Fagan 			case 'N':	/* process name */
3109c52ff611SPawel Jakub Dawidek 				sbuf_printf(&sb, "%s", comm);
3110c5edb423SSean Eric Fagan 				break;
3111c5edb423SSean Eric Fagan 			case 'P':	/* process id */
311236b208e0SRobert Watson 				sbuf_printf(&sb, "%u", pid);
3113c5edb423SSean Eric Fagan 				break;
3114c5edb423SSean Eric Fagan 			case 'U':	/* user id */
311536b208e0SRobert Watson 				sbuf_printf(&sb, "%u", uid);
3116c5edb423SSean Eric Fagan 				break;
3117c5edb423SSean Eric Fagan 			default:
31188b43b535SAlfred Perlstein 				log(LOG_ERR,
311936b208e0SRobert Watson 				    "Unknown format character %c in "
312036b208e0SRobert Watson 				    "corename `%s'\n", format[i], format);
312129146f1aSPawel Jakub Dawidek 				break;
3122c5edb423SSean Eric Fagan 			}
3123c5edb423SSean Eric Fagan 			break;
3124c5edb423SSean Eric Fagan 		default:
312536b208e0SRobert Watson 			sbuf_putc(&sb, format[i]);
31266c08be2bSPawel Jakub Dawidek 			break;
3127c5edb423SSean Eric Fagan 		}
3128c5edb423SSean Eric Fagan 	}
3129b7402d82SAlfred Perlstein 	free(hostname, M_TEMP);
3130e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
3131f06f465dSPawel Jakub Dawidek 	if (compress)
3132e7228204SAlfred Perlstein 		sbuf_printf(&sb, GZ_SUFFIX);
3133e7228204SAlfred Perlstein #endif
31344d369413SMatthew D Fleming 	if (sbuf_error(&sb) != 0) {
313536b208e0SRobert Watson 		log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3136c52ff611SPawel Jakub Dawidek 		    "long\n", (long)pid, comm, (u_long)uid);
3137b7402d82SAlfred Perlstein 		sbuf_delete(&sb);
3138c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
3139c345faeaSPawel Jakub Dawidek 		return (ENOMEM);
3140c5edb423SSean Eric Fagan 	}
314136b208e0SRobert Watson 	sbuf_finish(&sb);
314236b208e0SRobert Watson 	sbuf_delete(&sb);
3143e7228204SAlfred Perlstein 
3144c345faeaSPawel Jakub Dawidek 	cmode = S_IRUSR | S_IWUSR;
3145c345faeaSPawel Jakub Dawidek 	oflags = VN_OPEN_NOAUDIT | (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3146c345faeaSPawel Jakub Dawidek 
3147e7228204SAlfred Perlstein 	/*
3148e7228204SAlfred Perlstein 	 * If the core format has a %I in it, then we need to check
3149e7228204SAlfred Perlstein 	 * for existing corefiles before returning a name.
3150e7228204SAlfred Perlstein 	 * To do this we iterate over 0..num_cores to find a
3151e7228204SAlfred Perlstein 	 * non-existing core file name to use.
3152e7228204SAlfred Perlstein 	 */
3153e7228204SAlfred Perlstein 	if (indexpos != -1) {
315429146f1aSPawel Jakub Dawidek 		for (i = 0; i < num_cores; i++) {
315507a8e078SPawel Jakub Dawidek 			flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
315629146f1aSPawel Jakub Dawidek 			name[indexpos] = '0' + i;
3157c345faeaSPawel Jakub Dawidek 			NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3158b0c9d4d7SPawel Jakub Dawidek 			error = vn_open_cred(&nd, &flags, cmode, oflags,
3159b0c9d4d7SPawel Jakub Dawidek 			    td->td_ucred, NULL);
3160e7228204SAlfred Perlstein 			if (error) {
316123c6445aSPawel Jakub Dawidek 				if (error == EEXIST)
3162e7228204SAlfred Perlstein 					continue;
3163e7228204SAlfred Perlstein 				log(LOG_ERR,
3164e7228204SAlfred Perlstein 				    "pid %d (%s), uid (%u):  Path `%s' failed "
3165e7228204SAlfred Perlstein 				    "on initial open test, error = %d\n",
3166c52ff611SPawel Jakub Dawidek 				    pid, comm, uid, name, error);
3167c345faeaSPawel Jakub Dawidek 			}
3168c345faeaSPawel Jakub Dawidek 			goto out;
3169c345faeaSPawel Jakub Dawidek 		}
3170c345faeaSPawel Jakub Dawidek 	}
3171c345faeaSPawel Jakub Dawidek 
3172c345faeaSPawel Jakub Dawidek 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
3173c345faeaSPawel Jakub Dawidek 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3174c345faeaSPawel Jakub Dawidek 	error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL);
3175c345faeaSPawel Jakub Dawidek out:
3176c345faeaSPawel Jakub Dawidek 	if (error) {
3177c345faeaSPawel Jakub Dawidek #ifdef AUDIT
3178c345faeaSPawel Jakub Dawidek 		audit_proc_coredump(td, name, error);
3179c345faeaSPawel Jakub Dawidek #endif
3180c52ff611SPawel Jakub Dawidek 		free(name, M_TEMP);
3181c345faeaSPawel Jakub Dawidek 		return (error);
3182e7228204SAlfred Perlstein 	}
3183e7228204SAlfred Perlstein 	NDFREE(&nd, NDF_ONLY_PNBUF);
3184c345faeaSPawel Jakub Dawidek 	*vpp = nd.ni_vp;
3185c345faeaSPawel Jakub Dawidek 	*namep = name;
3186c345faeaSPawel Jakub Dawidek 	return (0);
318736b208e0SRobert Watson }
3188c5edb423SSean Eric Fagan 
3189df8bae1dSRodney W. Grimes /*
3190fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
3191fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
3192fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
3193fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
3194fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
3195fca666a1SJulian Elischer  */
3196fca666a1SJulian Elischer 
3197fca666a1SJulian Elischer static int
3198b40ce416SJulian Elischer coredump(struct thread *td)
3199fca666a1SJulian Elischer {
3200b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3201f06f465dSPawel Jakub Dawidek 	struct ucred *cred = td->td_ucred;
3202f06f465dSPawel Jakub Dawidek 	struct vnode *vp;
320306ae1e91SMatthew Dillon 	struct flock lf;
3204fca666a1SJulian Elischer 	struct vattr vattr;
3205c345faeaSPawel Jakub Dawidek 	int error, error1, locked;
3206f2a2857bSKirk McKusick 	struct mount *mp;
3207fca666a1SJulian Elischer 	char *name;			/* name of corefile */
3208fca666a1SJulian Elischer 	off_t limit;
3209e7228204SAlfred Perlstein 	int compress;
3210fca666a1SJulian Elischer 
3211e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
3212e7228204SAlfred Perlstein 	compress = compress_user_cores;
3213e7228204SAlfred Perlstein #else
3214e7228204SAlfred Perlstein 	compress = 0;
3215e7228204SAlfred Perlstein #endif
32164ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3217f97c3df1SDavid Schultz 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3218628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
3219fca666a1SJulian Elischer 
32207e73ee85SPawel Jakub Dawidek 	if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0)) {
3221628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3222fca666a1SJulian Elischer 		return (EFAULT);
3223628d2653SJohn Baldwin 	}
3224fca666a1SJulian Elischer 
3225fca666a1SJulian Elischer 	/*
322635a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
322735a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
322835a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
322935a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
323035a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
323135a2598fSSean Eric Fagan 	 * if it is larger than the limit.
3232fca666a1SJulian Elischer 	 */
323391d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
3234b8fdb0d9SEdward Tomasz Napierala 	if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3235628d2653SJohn Baldwin 		PROC_UNLOCK(p);
323691d5354aSJohn Baldwin 		return (EFBIG);
323757274c51SChristian S.J. Peron 	}
3238b8fdb0d9SEdward Tomasz Napierala 	PROC_UNLOCK(p);
323935a2598fSSean Eric Fagan 
3240f2a2857bSKirk McKusick restart:
3241c345faeaSPawel Jakub Dawidek 	error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, compress,
3242c345faeaSPawel Jakub Dawidek 	    &vp, &name);
3243c345faeaSPawel Jakub Dawidek 	if (error != 0)
3244fca666a1SJulian Elischer 		return (error);
324506ae1e91SMatthew Dillon 
3246832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
3247f06f465dSPawel Jakub Dawidek 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3248f06f465dSPawel Jakub Dawidek 	    vattr.va_nlink != 1) {
324922db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
3250832dafadSDon Lewis 		error = EFAULT;
325133f19beeSJohn Baldwin 		goto close;
3252832dafadSDon Lewis 	}
3253832dafadSDon Lewis 
325422db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
325506ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
325606ae1e91SMatthew Dillon 	lf.l_start = 0;
325706ae1e91SMatthew Dillon 	lf.l_len = 0;
325806ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
3259c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
326006ae1e91SMatthew Dillon 
326106ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
326206ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
326336bbf86bSRobert Watson 		if (locked)
326406ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3265b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
326633f19beeSJohn Baldwin 			goto out;
3267f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
326833f19beeSJohn Baldwin 			goto out;
3269b9ce4f67SGleb Smirnoff 		free(name, M_TEMP);
3270f2a2857bSKirk McKusick 		goto restart;
3271f2a2857bSKirk McKusick 	}
3272fca666a1SJulian Elischer 
3273fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
3274fca666a1SJulian Elischer 	vattr.va_size = 0;
32756141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
32766141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
3277cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
32780359a12eSAttilio Rao 	VOP_SETATTR(vp, &vattr, cred);
327922db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
328011991ab4STor Egge 	vn_finished_write(mp);
3281628d2653SJohn Baldwin 	PROC_LOCK(p);
3282fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
3283628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3284fca666a1SJulian Elischer 
328523c6445aSPawel Jakub Dawidek 	if (p->p_sysent->sv_coredump != NULL) {
328623c6445aSPawel Jakub Dawidek 		error = p->p_sysent->sv_coredump(td, vp, limit,
328723c6445aSPawel Jakub Dawidek 		    compress ? IMGACT_CORE_COMPRESS : 0);
328823c6445aSPawel Jakub Dawidek 	} else {
328923c6445aSPawel Jakub Dawidek 		error = ENOSYS;
329023c6445aSPawel Jakub Dawidek 	}
3291fca666a1SJulian Elischer 
3292c447f5b2SRobert Watson 	if (locked) {
329306ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
329406ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3295c447f5b2SRobert Watson 	}
329633f19beeSJohn Baldwin close:
3297b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
3298fca666a1SJulian Elischer 	if (error == 0)
3299fca666a1SJulian Elischer 		error = error1;
330033f19beeSJohn Baldwin out:
330157274c51SChristian S.J. Peron #ifdef AUDIT
330257274c51SChristian S.J. Peron 	audit_proc_coredump(td, name, error);
330357274c51SChristian S.J. Peron #endif
330457274c51SChristian S.J. Peron 	free(name, M_TEMP);
3305fca666a1SJulian Elischer 	return (error);
3306fca666a1SJulian Elischer }
3307fca666a1SJulian Elischer 
3308fca666a1SJulian Elischer /*
33090c14ff0eSRobert Watson  * Nonexistent system call-- signal process (may want to handle it).  Flag
33100c14ff0eSRobert Watson  * error in case process won't see signal immediately (blocked or ignored).
3311df8bae1dSRodney W. Grimes  */
3312d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
3313df8bae1dSRodney W. Grimes struct nosys_args {
3314df8bae1dSRodney W. Grimes 	int	dummy;
3315df8bae1dSRodney W. Grimes };
3316d2d3e875SBruce Evans #endif
3317df8bae1dSRodney W. Grimes /* ARGSUSED */
331826f9a767SRodney W. Grimes int
3319b40ce416SJulian Elischer nosys(td, args)
3320b40ce416SJulian Elischer 	struct thread *td;
3321df8bae1dSRodney W. Grimes 	struct nosys_args *args;
3322df8bae1dSRodney W. Grimes {
3323b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3324b40ce416SJulian Elischer 
3325628d2653SJohn Baldwin 	PROC_LOCK(p);
3326888aefefSKonstantin Belousov 	tdsignal(td, SIGSYS);
3327628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3328f5216b9aSBruce Evans 	return (ENOSYS);
3329df8bae1dSRodney W. Grimes }
3330831d27a9SDon Lewis 
3331831d27a9SDon Lewis /*
33320c14ff0eSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using stored
33330c14ff0eSRobert Watson  * credentials rather than those of the current process.
3334831d27a9SDon Lewis  */
3335831d27a9SDon Lewis void
3336f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
3337f1320723SAlfred Perlstein 	struct sigio **sigiop;
33382c42a146SMarcel Moolenaar 	int sig, checkctty;
3339831d27a9SDon Lewis {
3340a3de221dSKonstantin Belousov 	ksiginfo_t ksi;
3341f1320723SAlfred Perlstein 	struct sigio *sigio;
3342831d27a9SDon Lewis 
3343a3de221dSKonstantin Belousov 	ksiginfo_init(&ksi);
3344a3de221dSKonstantin Belousov 	ksi.ksi_signo = sig;
3345a3de221dSKonstantin Belousov 	ksi.ksi_code = SI_KERNEL;
3346a3de221dSKonstantin Belousov 
3347f1320723SAlfred Perlstein 	SIGIO_LOCK();
3348f1320723SAlfred Perlstein 	sigio = *sigiop;
3349f1320723SAlfred Perlstein 	if (sigio == NULL) {
3350f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
3351f1320723SAlfred Perlstein 		return;
3352f1320723SAlfred Perlstein 	}
3353831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
3354628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
33552b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
33568451d0ddSKip Macy 			kern_psignal(sigio->sio_proc, sig);
3357628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
3358831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
3359831d27a9SDon Lewis 		struct proc *p;
3360831d27a9SDon Lewis 
3361f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
3362628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3363628d2653SJohn Baldwin 			PROC_LOCK(p);
3364e806d352SJohn Baldwin 			if (p->p_state == PRS_NORMAL &&
3365e806d352SJohn Baldwin 			    CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3366831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
33678451d0ddSKip Macy 				kern_psignal(p, sig);
3368628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3369628d2653SJohn Baldwin 		}
3370f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
3371831d27a9SDon Lewis 	}
3372f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
3373831d27a9SDon Lewis }
3374cb679c38SJonathan Lemon 
3375cb679c38SJonathan Lemon static int
3376cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
3377cb679c38SJonathan Lemon {
3378cb679c38SJonathan Lemon 	struct proc *p = curproc;
3379cb679c38SJonathan Lemon 
3380cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
3381cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
3382cb679c38SJonathan Lemon 
3383ad3b9257SJohn-Mark Gurney 	knlist_add(&p->p_klist, kn, 0);
3384cb679c38SJonathan Lemon 
3385cb679c38SJonathan Lemon 	return (0);
3386cb679c38SJonathan Lemon }
3387cb679c38SJonathan Lemon 
3388cb679c38SJonathan Lemon static void
3389cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
3390cb679c38SJonathan Lemon {
3391cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
3392cb679c38SJonathan Lemon 
3393ad3b9257SJohn-Mark Gurney 	knlist_remove(&p->p_klist, kn, 0);
3394cb679c38SJonathan Lemon }
3395cb679c38SJonathan Lemon 
3396cb679c38SJonathan Lemon /*
3397cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
3398cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
3399cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
3400cb679c38SJonathan Lemon  * isn't worth the trouble.
3401cb679c38SJonathan Lemon  */
3402cb679c38SJonathan Lemon static int
3403cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
3404cb679c38SJonathan Lemon {
3405cb679c38SJonathan Lemon 
3406cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
3407cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
3408cb679c38SJonathan Lemon 
3409cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
3410cb679c38SJonathan Lemon 			kn->kn_data++;
3411cb679c38SJonathan Lemon 	}
3412cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
3413cb679c38SJonathan Lemon }
341490af4afaSJohn Baldwin 
341590af4afaSJohn Baldwin struct sigacts *
341690af4afaSJohn Baldwin sigacts_alloc(void)
341790af4afaSJohn Baldwin {
341890af4afaSJohn Baldwin 	struct sigacts *ps;
341990af4afaSJohn Baldwin 
342090af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
342190af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
342290af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
342390af4afaSJohn Baldwin 	return (ps);
342490af4afaSJohn Baldwin }
342590af4afaSJohn Baldwin 
342690af4afaSJohn Baldwin void
342790af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
342890af4afaSJohn Baldwin {
342990af4afaSJohn Baldwin 
343090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
343190af4afaSJohn Baldwin 	ps->ps_refcnt--;
343290af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
343390af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
343490af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
343590af4afaSJohn Baldwin 	} else
343690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
343790af4afaSJohn Baldwin }
343890af4afaSJohn Baldwin 
343990af4afaSJohn Baldwin struct sigacts *
344090af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
344190af4afaSJohn Baldwin {
344290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
344390af4afaSJohn Baldwin 	ps->ps_refcnt++;
344490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
344590af4afaSJohn Baldwin 	return (ps);
344690af4afaSJohn Baldwin }
344790af4afaSJohn Baldwin 
344890af4afaSJohn Baldwin void
344990af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
345090af4afaSJohn Baldwin {
345190af4afaSJohn Baldwin 
345290af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
345390af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
345490af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
345590af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
345690af4afaSJohn Baldwin }
345790af4afaSJohn Baldwin 
345890af4afaSJohn Baldwin int
345990af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
346090af4afaSJohn Baldwin {
346190af4afaSJohn Baldwin 	int shared;
346290af4afaSJohn Baldwin 
346390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
346490af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
346590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
346690af4afaSJohn Baldwin 	return (shared);
346790af4afaSJohn Baldwin }
3468