xref: /freebsd/sys/kern/kern_sig.c (revision bfd7575a39f40753599090e390d1e97c39b49ea0)
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"
41db6a20e2SGarrett Wollman #include "opt_ktrace.h"
42db6a20e2SGarrett Wollman 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
4436240ea5SDoug Rabson #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
46df8bae1dSRodney W. Grimes #include <sys/vnode.h>
47df8bae1dSRodney W. Grimes #include <sys/acct.h>
48238510fcSJason Evans #include <sys/condvar.h>
49854dc8c2SJohn Baldwin #include <sys/event.h>
50854dc8c2SJohn Baldwin #include <sys/fcntl.h>
51854dc8c2SJohn Baldwin #include <sys/kernel.h>
529dde3bc9SDavid Xu #include <sys/kse.h>
530384fff8SJason Evans #include <sys/ktr.h>
54df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
55854dc8c2SJohn Baldwin #include <sys/lock.h>
56854dc8c2SJohn Baldwin #include <sys/malloc.h>
57854dc8c2SJohn Baldwin #include <sys/mutex.h>
58854dc8c2SJohn Baldwin #include <sys/namei.h>
59854dc8c2SJohn Baldwin #include <sys/proc.h>
60854dc8c2SJohn Baldwin #include <sys/pioctl.h>
61c31146a1SJohn Baldwin #include <sys/resourcevar.h>
62b3a4fb14SDavid Xu #include <sys/sched.h>
6344f3b092SJohn Baldwin #include <sys/sleepqueue.h>
646caa8a15SJohn Baldwin #include <sys/smp.h>
65df8bae1dSRodney W. Grimes #include <sys/stat.h>
661005a129SJohn Baldwin #include <sys/sx.h>
678f19eb88SIan Dowse #include <sys/syscallsubr.h>
68c87e2930SDavid Greenman #include <sys/sysctl.h>
69854dc8c2SJohn Baldwin #include <sys/sysent.h>
70854dc8c2SJohn Baldwin #include <sys/syslog.h>
71854dc8c2SJohn Baldwin #include <sys/sysproto.h>
7256c06c4bSDavid Xu #include <sys/timers.h>
7306ae1e91SMatthew Dillon #include <sys/unistd.h>
74854dc8c2SJohn Baldwin #include <sys/wait.h>
759104847fSDavid Xu #include <vm/vm.h>
769104847fSDavid Xu #include <vm/vm_extern.h>
779104847fSDavid Xu #include <vm/uma.h>
78df8bae1dSRodney W. Grimes 
79b51d237aSDavid Xu #include <posix4/posix4.h>
80df8bae1dSRodney W. Grimes #include <machine/cpu.h>
81df8bae1dSRodney W. Grimes 
82bfd7575aSWayne Salamon #include <security/audit/audit.h>
83bfd7575aSWayne Salamon 
8423eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43)
8523eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
8623eeeff7SPeter Wemm #endif
8723eeeff7SPeter Wemm 
886f841fb7SMarcel Moolenaar #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
896f841fb7SMarcel Moolenaar 
904d77a549SAlfred Perlstein static int	coredump(struct thread *);
914d77a549SAlfred Perlstein static char	*expand_name(const char *, uid_t, pid_t);
921a88a252SMaxim Sobolev static int	killpg1(struct thread *td, int sig, int pgid, int all);
936711f10fSJohn Baldwin static int	issignal(struct thread *p);
944d77a549SAlfred Perlstein static int	sigprop(int sig);
95d8267df7SDavid Xu static void	tdsigwakeup(struct thread *, int, sig_t);
96d8267df7SDavid Xu static void	sig_suspend_threads(struct thread *, struct proc *, int);
97cb679c38SJonathan Lemon static int	filt_sigattach(struct knote *kn);
98cb679c38SJonathan Lemon static void	filt_sigdetach(struct knote *kn);
99cb679c38SJonathan Lemon static int	filt_signal(struct knote *kn, long hint);
1004093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop);
1019104847fSDavid Xu static int	kern_sigtimedwait(struct thread *, sigset_t,
1029104847fSDavid Xu 			ksiginfo_t *, struct timespec *);
1036d7b314bSDavid Xu static int	do_tdsignal(struct proc *, struct thread *, int, ksiginfo_t *);
1049104847fSDavid Xu static void	sigqueue_start(void);
105cb679c38SJonathan Lemon 
1069104847fSDavid Xu static uma_zone_t	ksiginfo_zone = NULL;
107cb679c38SJonathan Lemon struct filterops sig_filtops =
108cb679c38SJonathan Lemon 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
109cb679c38SJonathan Lemon 
11057308494SJoerg Wunsch static int	kern_logsigexit = 1;
1113d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
1123d177f46SBill Fumerola     &kern_logsigexit, 0,
1133d177f46SBill Fumerola     "Log processes quitting on abnormal signals to syslog(3)");
11457308494SJoerg Wunsch 
115f71a882fSDavid Xu static int	kern_forcesigexit = 1;
116f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
117f71a882fSDavid Xu     &kern_forcesigexit, 0, "Force trap signal to be handled");
118f71a882fSDavid Xu 
1199104847fSDavid Xu SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
1209104847fSDavid Xu 
1219104847fSDavid Xu static int	max_pending_per_proc = 128;
1229104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
1239104847fSDavid Xu     &max_pending_per_proc, 0, "Max pending signals per proc");
1249104847fSDavid Xu 
1259104847fSDavid Xu static int	preallocate_siginfo = 1024;
1269104847fSDavid Xu TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
1279104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
1289104847fSDavid Xu     &preallocate_siginfo, 0, "Preallocated signal memory size");
1299104847fSDavid Xu 
1309104847fSDavid Xu static int	signal_overflow = 0;
131761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
1329104847fSDavid Xu     &signal_overflow, 0, "Number of signals overflew");
1339104847fSDavid Xu 
1349104847fSDavid Xu static int	signal_alloc_fail = 0;
135761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
1369104847fSDavid Xu     &signal_alloc_fail, 0, "signals failed to be allocated");
1379104847fSDavid Xu 
1389104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
1399104847fSDavid Xu 
1402b87b6d4SRobert Watson /*
1412b87b6d4SRobert Watson  * Policy -- Can ucred cr1 send SIGIO to process cr2?
1422b87b6d4SRobert Watson  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
1432b87b6d4SRobert Watson  * in the right situations.
1442b87b6d4SRobert Watson  */
1452b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \
1462b87b6d4SRobert Watson 	((cr1)->cr_uid == 0 || \
1472b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
1482b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
1492b87b6d4SRobert Watson 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
1502b87b6d4SRobert Watson 	    (cr1)->cr_uid == (cr2)->cr_uid)
1512b87b6d4SRobert Watson 
15222d4b0fbSJohn Polstra int sugid_coredump;
1533d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
1543d177f46SBill Fumerola     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
155c87e2930SDavid Greenman 
156e5a28db9SPaul Saab static int	do_coredump = 1;
157e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
158e5a28db9SPaul Saab 	&do_coredump, 0, "Enable/Disable coredumps");
159e5a28db9SPaul Saab 
1606141e04aSJohn-Mark Gurney static int	set_core_nodump_flag = 0;
1616141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
1626141e04aSJohn-Mark Gurney 	0, "Enable setting the NODUMP flag on coredump files");
1636141e04aSJohn-Mark Gurney 
1642c42a146SMarcel Moolenaar /*
1652c42a146SMarcel Moolenaar  * Signal properties and actions.
1662c42a146SMarcel Moolenaar  * The array below categorizes the signals and their default actions
1672c42a146SMarcel Moolenaar  * according to the following properties:
1682c42a146SMarcel Moolenaar  */
1692c42a146SMarcel Moolenaar #define	SA_KILL		0x01		/* terminates process by default */
1702c42a146SMarcel Moolenaar #define	SA_CORE		0x02		/* ditto and coredumps */
1712c42a146SMarcel Moolenaar #define	SA_STOP		0x04		/* suspend process */
1722c42a146SMarcel Moolenaar #define	SA_TTYSTOP	0x08		/* ditto, from tty */
1732c42a146SMarcel Moolenaar #define	SA_IGNORE	0x10		/* ignore by default */
1742c42a146SMarcel Moolenaar #define	SA_CONT		0x20		/* continue if suspended */
1752c42a146SMarcel Moolenaar #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
176da33176fSJeff Roberson #define	SA_PROC		0x80		/* deliverable to any thread */
177df8bae1dSRodney W. Grimes 
1782c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = {
179da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGHUP */
180da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGINT */
181da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
1822c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGILL */
1832c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGTRAP */
1842c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGABRT */
185da33176fSJeff Roberson         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
1862c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGFPE */
187da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGKILL */
1882c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGBUS */
1892c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSEGV */
1902c42a146SMarcel Moolenaar         SA_KILL|SA_CORE,		/* SIGSYS */
191da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPIPE */
192da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGALRM */
193da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGTERM */
194da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGURG */
195da33176fSJeff Roberson         SA_STOP|SA_PROC,		/* SIGSTOP */
196da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
197da33176fSJeff Roberson         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
198da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGCHLD */
199da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
200da33176fSJeff Roberson         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
201da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGIO */
2022c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXCPU */
2032c42a146SMarcel Moolenaar         SA_KILL,			/* SIGXFSZ */
204da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGVTALRM */
205da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGPROF */
206da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
207da33176fSJeff Roberson         SA_IGNORE|SA_PROC,		/* SIGINFO */
208da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR1 */
209da33176fSJeff Roberson         SA_KILL|SA_PROC,		/* SIGUSR2 */
2102c42a146SMarcel Moolenaar };
2112c42a146SMarcel Moolenaar 
2129104847fSDavid Xu static void
2139104847fSDavid Xu sigqueue_start(void)
2149104847fSDavid Xu {
2159104847fSDavid Xu 	ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
2169104847fSDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2179104847fSDavid Xu 	uma_prealloc(ksiginfo_zone, preallocate_siginfo);
218b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
219b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
220b51d237aSDavid Xu 	p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
2219104847fSDavid Xu }
2229104847fSDavid Xu 
2235da49fcbSDavid Xu ksiginfo_t *
224ebceaf6dSDavid Xu ksiginfo_alloc(int wait)
2259104847fSDavid Xu {
226ebceaf6dSDavid Xu 	int flags;
227ebceaf6dSDavid Xu 
228ebceaf6dSDavid Xu 	flags = M_ZERO;
229ebceaf6dSDavid Xu 	if (! wait)
230ebceaf6dSDavid Xu 		flags |= M_NOWAIT;
2319104847fSDavid Xu 	if (ksiginfo_zone != NULL)
232ebceaf6dSDavid Xu 		return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
2339104847fSDavid Xu 	return (NULL);
2349104847fSDavid Xu }
2359104847fSDavid Xu 
2365da49fcbSDavid Xu void
2379104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi)
2389104847fSDavid Xu {
2399104847fSDavid Xu 	uma_zfree(ksiginfo_zone, ksi);
2409104847fSDavid Xu }
2419104847fSDavid Xu 
2425da49fcbSDavid Xu static __inline int
2435da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi)
2445da49fcbSDavid Xu {
2455da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT)) {
2465da49fcbSDavid Xu 		uma_zfree(ksiginfo_zone, ksi);
2475da49fcbSDavid Xu 		return (1);
2485da49fcbSDavid Xu 	}
2495da49fcbSDavid Xu 	return (0);
2505da49fcbSDavid Xu }
2515da49fcbSDavid Xu 
2529104847fSDavid Xu void
2539104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p)
2549104847fSDavid Xu {
2559104847fSDavid Xu 	SIGEMPTYSET(list->sq_signals);
2569104847fSDavid Xu 	TAILQ_INIT(&list->sq_list);
2579104847fSDavid Xu 	list->sq_proc = p;
2589104847fSDavid Xu 	list->sq_flags = SQ_INIT;
2599104847fSDavid Xu }
2609104847fSDavid Xu 
2619104847fSDavid Xu /*
2629104847fSDavid Xu  * Get a signal's ksiginfo.
2639104847fSDavid Xu  * Return:
2649104847fSDavid Xu  * 	0	-	signal not found
2659104847fSDavid Xu  *	others	-	signal number
2669104847fSDavid Xu  */
2679104847fSDavid Xu int
2689104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
2699104847fSDavid Xu {
2709104847fSDavid Xu 	struct proc *p = sq->sq_proc;
2719104847fSDavid Xu 	struct ksiginfo *ksi, *next;
2729104847fSDavid Xu 	int count = 0;
2739104847fSDavid Xu 
2749104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
2759104847fSDavid Xu 
2769104847fSDavid Xu 	if (!SIGISMEMBER(sq->sq_signals, signo))
2779104847fSDavid Xu 		return (0);
2789104847fSDavid Xu 
2799104847fSDavid Xu 	for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) {
2809104847fSDavid Xu 		next = TAILQ_NEXT(ksi, ksi_link);
2819104847fSDavid Xu 		if (ksi->ksi_signo == signo) {
2829104847fSDavid Xu 			if (count == 0) {
2839104847fSDavid Xu 				TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
2845da49fcbSDavid Xu 				ksi->ksi_sigq = NULL;
2859104847fSDavid Xu 				ksiginfo_copy(ksi, si);
2865da49fcbSDavid Xu 				if (ksiginfo_tryfree(ksi) && p != NULL)
2879104847fSDavid Xu 					p->p_pendingcnt--;
2889104847fSDavid Xu 			}
2899104847fSDavid Xu 			count++;
2909104847fSDavid Xu 		}
2919104847fSDavid Xu 	}
2929104847fSDavid Xu 
2939104847fSDavid Xu 	if (count <= 1)
2949104847fSDavid Xu 		SIGDELSET(sq->sq_signals, signo);
2959104847fSDavid Xu 	si->ksi_signo = signo;
2969104847fSDavid Xu 	return (signo);
2979104847fSDavid Xu }
2989104847fSDavid Xu 
2995da49fcbSDavid Xu void
3005da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi)
3015da49fcbSDavid Xu {
3025da49fcbSDavid Xu 	struct ksiginfo *kp;
3035da49fcbSDavid Xu 	struct proc	*p;
3045da49fcbSDavid Xu 	sigqueue_t	*sq;
3055da49fcbSDavid Xu 
306ebceaf6dSDavid Xu 	if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
3075da49fcbSDavid Xu 		return;
3085da49fcbSDavid Xu 
3095da49fcbSDavid Xu 	p = sq->sq_proc;
3105da49fcbSDavid Xu 	TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
3115da49fcbSDavid Xu 	ksi->ksi_sigq = NULL;
3125da49fcbSDavid Xu 	if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
3135da49fcbSDavid Xu 		p->p_pendingcnt--;
3145da49fcbSDavid Xu 
3155da49fcbSDavid Xu 	for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
3165da49fcbSDavid Xu 	     kp = TAILQ_NEXT(kp, ksi_link)) {
3175da49fcbSDavid Xu 		if (kp->ksi_signo == ksi->ksi_signo)
3185da49fcbSDavid Xu 			break;
3195da49fcbSDavid Xu 	}
3205da49fcbSDavid Xu 	if (kp == NULL)
3215da49fcbSDavid Xu 		SIGDELSET(sq->sq_signals, ksi->ksi_signo);
3225da49fcbSDavid Xu }
3235da49fcbSDavid Xu 
3249104847fSDavid Xu int
3259104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
3269104847fSDavid Xu {
3279104847fSDavid Xu 	struct proc *p = sq->sq_proc;
3289104847fSDavid Xu 	struct ksiginfo *ksi;
3299104847fSDavid Xu 	int ret = 0;
3309104847fSDavid Xu 
3319104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
3329104847fSDavid Xu 
3339104847fSDavid Xu 	if (signo == SIGKILL || signo == SIGSTOP || si == NULL)
3349104847fSDavid Xu 		goto out_set_bit;
3359104847fSDavid Xu 
3365da49fcbSDavid Xu 	/* directly insert the ksi, don't copy it */
3375da49fcbSDavid Xu 	if (si->ksi_flags & KSI_INS) {
3385da49fcbSDavid Xu 		TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
3395da49fcbSDavid Xu 		si->ksi_sigq = sq;
3405da49fcbSDavid Xu 		goto out_set_bit;
3415da49fcbSDavid Xu 	}
3425da49fcbSDavid Xu 
3439104847fSDavid Xu 	if (__predict_false(ksiginfo_zone == NULL))
3449104847fSDavid Xu 		goto out_set_bit;
3459104847fSDavid Xu 
346ebceaf6dSDavid Xu 	if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
3479104847fSDavid Xu 		signal_overflow++;
3489104847fSDavid Xu 		ret = EAGAIN;
349ebceaf6dSDavid Xu 	} else if ((ksi = ksiginfo_alloc(0)) == NULL) {
3509104847fSDavid Xu 		signal_alloc_fail++;
3519104847fSDavid Xu 		ret = EAGAIN;
3529104847fSDavid Xu 	} else {
3539104847fSDavid Xu 		if (p != NULL)
3549104847fSDavid Xu 			p->p_pendingcnt++;
3559104847fSDavid Xu 		ksiginfo_copy(si, ksi);
3569104847fSDavid Xu 		ksi->ksi_signo = signo;
3579104847fSDavid Xu 		TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
3585da49fcbSDavid Xu 		ksi->ksi_sigq = sq;
3599104847fSDavid Xu 	}
3609104847fSDavid Xu 
3619104847fSDavid Xu 	if ((si->ksi_flags & KSI_TRAP) != 0) {
3629104847fSDavid Xu 		ret = 0;
3639104847fSDavid Xu 		goto out_set_bit;
3649104847fSDavid Xu 	}
3659104847fSDavid Xu 
3669104847fSDavid Xu 	if (ret != 0)
3679104847fSDavid Xu 		return (ret);
3689104847fSDavid Xu 
3699104847fSDavid Xu out_set_bit:
3709104847fSDavid Xu 	SIGADDSET(sq->sq_signals, signo);
3719104847fSDavid Xu 	return (ret);
3729104847fSDavid Xu }
3739104847fSDavid Xu 
3749104847fSDavid Xu void
3759104847fSDavid Xu sigqueue_flush(sigqueue_t *sq)
3769104847fSDavid Xu {
3779104847fSDavid Xu 	struct proc *p = sq->sq_proc;
3789104847fSDavid Xu 	ksiginfo_t *ksi;
3799104847fSDavid Xu 
3809104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
3819104847fSDavid Xu 
3825da49fcbSDavid Xu 	if (p != NULL)
3835da49fcbSDavid Xu 		PROC_LOCK_ASSERT(p, MA_OWNED);
3845da49fcbSDavid Xu 
3859104847fSDavid Xu 	while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
3869104847fSDavid Xu 		TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
3875da49fcbSDavid Xu 		ksi->ksi_sigq = NULL;
3885da49fcbSDavid Xu 		if (ksiginfo_tryfree(ksi) && p != NULL)
3899104847fSDavid Xu 			p->p_pendingcnt--;
3909104847fSDavid Xu 	}
3919104847fSDavid Xu 
3929104847fSDavid Xu 	SIGEMPTYSET(sq->sq_signals);
3939104847fSDavid Xu }
3949104847fSDavid Xu 
3959104847fSDavid Xu void
3969104847fSDavid Xu sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
3979104847fSDavid Xu {
3989104847fSDavid Xu 	ksiginfo_t *ksi;
3999104847fSDavid Xu 
4009104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
4019104847fSDavid Xu 
4029104847fSDavid Xu 	TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
4039104847fSDavid Xu 		SIGADDSET(*set, ksi->ksi_signo);
4049104847fSDavid Xu }
4059104847fSDavid Xu 
4069104847fSDavid Xu void
4079104847fSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
4089104847fSDavid Xu {
4099104847fSDavid Xu 	sigset_t tmp, set;
4109104847fSDavid Xu 	struct proc *p1, *p2;
4119104847fSDavid Xu 	ksiginfo_t *ksi, *next;
4129104847fSDavid Xu 
4139104847fSDavid Xu 	KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
4149104847fSDavid Xu 	KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
4159104847fSDavid Xu 	/*
4169104847fSDavid Xu 	 * make a copy, this allows setp to point to src or dst
4179104847fSDavid Xu 	 * sq_signals without trouble.
4189104847fSDavid Xu 	 */
4199104847fSDavid Xu 	set = *setp;
4209104847fSDavid Xu 	p1 = src->sq_proc;
4219104847fSDavid Xu 	p2 = dst->sq_proc;
4229104847fSDavid Xu 	/* Move siginfo to target list */
4239104847fSDavid Xu 	for (ksi = TAILQ_FIRST(&src->sq_list); ksi != NULL; ksi = next) {
4249104847fSDavid Xu 		next = TAILQ_NEXT(ksi, ksi_link);
4259104847fSDavid Xu 		if (SIGISMEMBER(set, ksi->ksi_signo)) {
4269104847fSDavid Xu 			TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
4279104847fSDavid Xu 			if (p1 != NULL)
4289104847fSDavid Xu 				p1->p_pendingcnt--;
4299104847fSDavid Xu 			TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
4305da49fcbSDavid Xu 			ksi->ksi_sigq = dst;
4319104847fSDavid Xu 			if (p2 != NULL)
4329104847fSDavid Xu 				p2->p_pendingcnt++;
4339104847fSDavid Xu 		}
4349104847fSDavid Xu 	}
4359104847fSDavid Xu 
4369104847fSDavid Xu 	/* Move pending bits to target list */
4379104847fSDavid Xu 	tmp = src->sq_signals;
4389104847fSDavid Xu 	SIGSETAND(tmp, set);
4399104847fSDavid Xu 	SIGSETOR(dst->sq_signals, tmp);
4409104847fSDavid Xu 	SIGSETNAND(src->sq_signals, tmp);
4419104847fSDavid Xu 
4429104847fSDavid Xu 	/* Finally, rescan src queue and set pending bits for it */
4439104847fSDavid Xu 	sigqueue_collect_set(src, &src->sq_signals);
4449104847fSDavid Xu }
4459104847fSDavid Xu 
4469104847fSDavid Xu void
4479104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
4489104847fSDavid Xu {
4499104847fSDavid Xu 	sigset_t set;
4509104847fSDavid Xu 
4519104847fSDavid Xu 	SIGEMPTYSET(set);
4529104847fSDavid Xu 	SIGADDSET(set, signo);
4539104847fSDavid Xu 	sigqueue_move_set(src, dst, &set);
4549104847fSDavid Xu }
4559104847fSDavid Xu 
4569104847fSDavid Xu void
4579104847fSDavid Xu sigqueue_delete_set(sigqueue_t *sq, sigset_t *set)
4589104847fSDavid Xu {
4599104847fSDavid Xu 	struct proc *p = sq->sq_proc;
4609104847fSDavid Xu 	ksiginfo_t *ksi, *next;
4619104847fSDavid Xu 
4629104847fSDavid Xu 	KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
4639104847fSDavid Xu 
4649104847fSDavid Xu 	/* Remove siginfo queue */
4659104847fSDavid Xu 	for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) {
4669104847fSDavid Xu 		next = TAILQ_NEXT(ksi, ksi_link);
4679104847fSDavid Xu 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
4689104847fSDavid Xu 			TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
4695da49fcbSDavid Xu 			ksi->ksi_sigq = NULL;
4705da49fcbSDavid Xu 			if (ksiginfo_tryfree(ksi) && p != NULL)
4719104847fSDavid Xu 				p->p_pendingcnt--;
4729104847fSDavid Xu 		}
4739104847fSDavid Xu 	}
4749104847fSDavid Xu 	SIGSETNAND(sq->sq_signals, *set);
4759104847fSDavid Xu 	/* Finally, rescan queue and set pending bits for it */
4769104847fSDavid Xu 	sigqueue_collect_set(sq, &sq->sq_signals);
4779104847fSDavid Xu }
4789104847fSDavid Xu 
4799104847fSDavid Xu void
4809104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo)
4819104847fSDavid Xu {
4829104847fSDavid Xu 	sigset_t set;
4839104847fSDavid Xu 
4849104847fSDavid Xu 	SIGEMPTYSET(set);
4859104847fSDavid Xu 	SIGADDSET(set, signo);
4869104847fSDavid Xu 	sigqueue_delete_set(sq, &set);
4879104847fSDavid Xu }
4889104847fSDavid Xu 
4899104847fSDavid Xu /* Remove a set of signals for a process */
4909104847fSDavid Xu void
4919104847fSDavid Xu sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
4929104847fSDavid Xu {
4939104847fSDavid Xu 	sigqueue_t worklist;
4949104847fSDavid Xu 	struct thread *td0;
4959104847fSDavid Xu 
4969104847fSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
4979104847fSDavid Xu 
4989104847fSDavid Xu 	sigqueue_init(&worklist, NULL);
4999104847fSDavid Xu 	sigqueue_move_set(&p->p_sigqueue, &worklist, set);
5009104847fSDavid Xu 
5019104847fSDavid Xu 	mtx_lock_spin(&sched_lock);
5029104847fSDavid Xu 	FOREACH_THREAD_IN_PROC(p, td0)
5039104847fSDavid Xu 		sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
5049104847fSDavid Xu 	mtx_unlock_spin(&sched_lock);
5059104847fSDavid Xu 
5069104847fSDavid Xu 	sigqueue_flush(&worklist);
5079104847fSDavid Xu }
5089104847fSDavid Xu 
5099104847fSDavid Xu void
5109104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo)
5119104847fSDavid Xu {
5129104847fSDavid Xu 	sigset_t set;
5139104847fSDavid Xu 
5149104847fSDavid Xu 	SIGEMPTYSET(set);
5159104847fSDavid Xu 	SIGADDSET(set, signo);
5169104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
5179104847fSDavid Xu }
5189104847fSDavid Xu 
5199104847fSDavid Xu void
5209104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p)
5219104847fSDavid Xu {
5229104847fSDavid Xu 	sigset_t set;
5239104847fSDavid Xu 
5249104847fSDavid Xu 	SIGEMPTYSET(set);
5259104847fSDavid Xu 	SIGADDSET(set, SIGSTOP);
5269104847fSDavid Xu 	SIGADDSET(set, SIGTSTP);
5279104847fSDavid Xu 	SIGADDSET(set, SIGTTIN);
5289104847fSDavid Xu 	SIGADDSET(set, SIGTTOU);
5299104847fSDavid Xu 	sigqueue_delete_set_proc(p, &set);
5309104847fSDavid Xu }
5319104847fSDavid Xu 
532fbbeeb6cSBruce Evans /*
533fbbeeb6cSBruce Evans  * Determine signal that should be delivered to process p, the current
534fbbeeb6cSBruce Evans  * process, 0 if none.  If there is a pending stop signal with default
535fbbeeb6cSBruce Evans  * action, the process stops in issignal().
536fbbeeb6cSBruce Evans  *
53733510ef1SBruce Evans  * MP SAFE.
538fbbeeb6cSBruce Evans  */
539fbbeeb6cSBruce Evans int
540e602ba25SJulian Elischer cursig(struct thread *td)
541fbbeeb6cSBruce Evans {
542c9dfa2e0SJeff Roberson 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
54390af4afaSJohn Baldwin 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
544179235b3SBruce Evans 	mtx_assert(&sched_lock, MA_NOTOWNED);
5454093529dSJeff Roberson 	return (SIGPENDING(td) ? issignal(td) : 0);
546fbbeeb6cSBruce Evans }
547fbbeeb6cSBruce Evans 
54879065dbaSBruce Evans /*
54979065dbaSBruce Evans  * Arrange for ast() to handle unmasked pending signals on return to user
5509104847fSDavid Xu  * mode.  This must be called whenever a signal is added to td_sigqueue or
5514093529dSJeff Roberson  * unmasked in td_sigmask.
55279065dbaSBruce Evans  */
55379065dbaSBruce Evans void
5544093529dSJeff Roberson signotify(struct thread *td)
55579065dbaSBruce Evans {
5564093529dSJeff Roberson 	struct proc *p;
5579dde3bc9SDavid Xu 	sigset_t set, saved;
5584093529dSJeff Roberson 
5594093529dSJeff Roberson 	p = td->td_proc;
56079065dbaSBruce Evans 
56179065dbaSBruce Evans 	PROC_LOCK_ASSERT(p, MA_OWNED);
5624093529dSJeff Roberson 
5634093529dSJeff Roberson 	/*
5644093529dSJeff Roberson 	 * If our mask changed we may have to move signal that were
5659104847fSDavid Xu 	 * previously masked by all threads to our sigqueue.
5664093529dSJeff Roberson 	 */
5679104847fSDavid Xu 	set = p->p_sigqueue.sq_signals;
5689dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
5699104847fSDavid Xu 		saved = p->p_sigqueue.sq_signals;
5704093529dSJeff Roberson 	SIGSETNAND(set, td->td_sigmask);
5719104847fSDavid Xu 	if (! SIGISEMPTY(set))
5729104847fSDavid Xu 		sigqueue_move_set(&p->p_sigqueue, &td->td_sigqueue, &set);
5738b94a061SJohn Baldwin 	if (SIGPENDING(td)) {
57479065dbaSBruce Evans 		mtx_lock_spin(&sched_lock);
5754093529dSJeff Roberson 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
57679065dbaSBruce Evans 		mtx_unlock_spin(&sched_lock);
57779065dbaSBruce Evans 	}
5789dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
5799104847fSDavid Xu 		if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
5809dde3bc9SDavid Xu 			/* pending set changed */
5819dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
5829dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
5839dde3bc9SDavid Xu 		}
5849dde3bc9SDavid Xu 	}
5858b94a061SJohn Baldwin }
5868b94a061SJohn Baldwin 
5878b94a061SJohn Baldwin int
5888b94a061SJohn Baldwin sigonstack(size_t sp)
5898b94a061SJohn Baldwin {
590a30ec4b9SDavid Xu 	struct thread *td = curthread;
5918b94a061SJohn Baldwin 
592a30ec4b9SDavid Xu 	return ((td->td_pflags & TDP_ALTSTACK) ?
5931930e303SPoul-Henning Kamp #if defined(COMPAT_43)
594a30ec4b9SDavid Xu 	    ((td->td_sigstk.ss_size == 0) ?
595a30ec4b9SDavid Xu 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
596a30ec4b9SDavid Xu 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
5978b94a061SJohn Baldwin #else
598a30ec4b9SDavid Xu 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
5998b94a061SJohn Baldwin #endif
6008b94a061SJohn Baldwin 	    : 0);
6018b94a061SJohn Baldwin }
60279065dbaSBruce Evans 
6036f841fb7SMarcel Moolenaar static __inline int
6046f841fb7SMarcel Moolenaar sigprop(int sig)
6052c42a146SMarcel Moolenaar {
6066f841fb7SMarcel Moolenaar 
6072c42a146SMarcel Moolenaar 	if (sig > 0 && sig < NSIG)
6082c42a146SMarcel Moolenaar 		return (sigproptbl[_SIG_IDX(sig)]);
6092c42a146SMarcel Moolenaar 	return (0);
610df8bae1dSRodney W. Grimes }
6112c42a146SMarcel Moolenaar 
6124093529dSJeff Roberson int
6136f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set)
6142c42a146SMarcel Moolenaar {
6152c42a146SMarcel Moolenaar 	int i;
6162c42a146SMarcel Moolenaar 
6176f841fb7SMarcel Moolenaar 	for (i = 0; i < _SIG_WORDS; i++)
6182c42a146SMarcel Moolenaar 		if (set->__bits[i])
6192c42a146SMarcel Moolenaar 			return (ffs(set->__bits[i]) + (i * 32));
620df8bae1dSRodney W. Grimes 	return (0);
621df8bae1dSRodney W. Grimes }
622df8bae1dSRodney W. Grimes 
6232c42a146SMarcel Moolenaar /*
6248f19eb88SIan Dowse  * kern_sigaction
6252c42a146SMarcel Moolenaar  * sigaction
62623eeeff7SPeter Wemm  * freebsd4_sigaction
6272c42a146SMarcel Moolenaar  * osigaction
62825d6dc06SJohn Baldwin  *
62925d6dc06SJohn Baldwin  * MPSAFE
6302c42a146SMarcel Moolenaar  */
6318f19eb88SIan Dowse int
63223eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags)
6338f19eb88SIan Dowse 	struct thread *td;
6342c42a146SMarcel Moolenaar 	register int sig;
6352c42a146SMarcel Moolenaar 	struct sigaction *act, *oact;
63623eeeff7SPeter Wemm 	int flags;
637df8bae1dSRodney W. Grimes {
63890af4afaSJohn Baldwin 	struct sigacts *ps;
6398f19eb88SIan Dowse 	struct proc *p = td->td_proc;
640df8bae1dSRodney W. Grimes 
6412899d606SDag-Erling Smørgrav 	if (!_SIG_VALID(sig))
6422c42a146SMarcel Moolenaar 		return (EINVAL);
6432c42a146SMarcel Moolenaar 
644628d2653SJohn Baldwin 	PROC_LOCK(p);
645628d2653SJohn Baldwin 	ps = p->p_sigacts;
64690af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
6472c42a146SMarcel Moolenaar 	if (oact) {
6482c42a146SMarcel Moolenaar 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
6492c42a146SMarcel Moolenaar 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
6502c42a146SMarcel Moolenaar 		oact->sa_flags = 0;
6512c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
6522c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_ONSTACK;
6532c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
6542c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESTART;
6552c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig))
6562c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_RESETHAND;
6572c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_signodefer, sig))
6582c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NODEFER;
6592c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_siginfo, sig))
6602c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_SIGINFO;
66190af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
6622c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDSTOP;
66390af4afaSJohn Baldwin 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
6642c42a146SMarcel Moolenaar 			oact->sa_flags |= SA_NOCLDWAIT;
6652c42a146SMarcel Moolenaar 	}
6662c42a146SMarcel Moolenaar 	if (act) {
6672c42a146SMarcel Moolenaar 		if ((sig == SIGKILL || sig == SIGSTOP) &&
668628d2653SJohn Baldwin 		    act->sa_handler != SIG_DFL) {
66990af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
670628d2653SJohn Baldwin 			PROC_UNLOCK(p);
6712c42a146SMarcel Moolenaar 			return (EINVAL);
672628d2653SJohn Baldwin 		}
6732c42a146SMarcel Moolenaar 
674df8bae1dSRodney W. Grimes 		/*
675df8bae1dSRodney W. Grimes 		 * Change setting atomically.
676df8bae1dSRodney W. Grimes 		 */
6772c42a146SMarcel Moolenaar 
6782c42a146SMarcel Moolenaar 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
6792c42a146SMarcel Moolenaar 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
6802c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_SIGINFO) {
681aa7a4daeSPeter Wemm 			ps->ps_sigact[_SIG_IDX(sig)] =
682aa7a4daeSPeter Wemm 			    (__sighandler_t *)act->sa_sigaction;
68380f42b55SIan Dowse 			SIGADDSET(ps->ps_siginfo, sig);
68480f42b55SIan Dowse 		} else {
68580f42b55SIan Dowse 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
6862c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_siginfo, sig);
6872c42a146SMarcel Moolenaar 		}
6882c42a146SMarcel Moolenaar 		if (!(act->sa_flags & SA_RESTART))
6892c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigintr, sig);
690df8bae1dSRodney W. Grimes 		else
6912c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigintr, sig);
6922c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_ONSTACK)
6932c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigonstack, sig);
694df8bae1dSRodney W. Grimes 		else
6952c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigonstack, sig);
6962c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_RESETHAND)
6972c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_sigreset, sig);
6981e41c1b5SSteven Wallace 		else
6992c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_sigreset, sig);
7002c42a146SMarcel Moolenaar 		if (act->sa_flags & SA_NODEFER)
7012c42a146SMarcel Moolenaar 			SIGADDSET(ps->ps_signodefer, sig);
702289ccde0SPeter Wemm 		else
7032c42a146SMarcel Moolenaar 			SIGDELSET(ps->ps_signodefer, sig);
7042c42a146SMarcel Moolenaar 		if (sig == SIGCHLD) {
7052c42a146SMarcel Moolenaar 			if (act->sa_flags & SA_NOCLDSTOP)
70690af4afaSJohn Baldwin 				ps->ps_flag |= PS_NOCLDSTOP;
7076626c604SJulian Elischer 			else
70890af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDSTOP;
709ba1551caSIan Dowse 			if (act->sa_flags & SA_NOCLDWAIT) {
710245f17d4SJoerg Wunsch 				/*
7112c42a146SMarcel Moolenaar 				 * Paranoia: since SA_NOCLDWAIT is implemented
7122c42a146SMarcel Moolenaar 				 * by reparenting the dying child to PID 1 (and
7132c42a146SMarcel Moolenaar 				 * trust it to reap the zombie), PID 1 itself
7142c42a146SMarcel Moolenaar 				 * is forbidden to set SA_NOCLDWAIT.
715245f17d4SJoerg Wunsch 				 */
716245f17d4SJoerg Wunsch 				if (p->p_pid == 1)
71790af4afaSJohn Baldwin 					ps->ps_flag &= ~PS_NOCLDWAIT;
7186626c604SJulian Elischer 				else
71990af4afaSJohn Baldwin 					ps->ps_flag |= PS_NOCLDWAIT;
720245f17d4SJoerg Wunsch 			} else
72190af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_NOCLDWAIT;
722ba1551caSIan Dowse 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
72390af4afaSJohn Baldwin 				ps->ps_flag |= PS_CLDSIGIGN;
724ba1551caSIan Dowse 			else
72590af4afaSJohn Baldwin 				ps->ps_flag &= ~PS_CLDSIGIGN;
726df8bae1dSRodney W. Grimes 		}
727df8bae1dSRodney W. Grimes 		/*
72890af4afaSJohn Baldwin 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
7292c42a146SMarcel Moolenaar 		 * and for signals set to SIG_DFL where the default is to
73090af4afaSJohn Baldwin 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
7312c42a146SMarcel Moolenaar 		 * have to restart the process.
732df8bae1dSRodney W. Grimes 		 */
7332c42a146SMarcel Moolenaar 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
7342c42a146SMarcel Moolenaar 		    (sigprop(sig) & SA_IGNORE &&
7352c42a146SMarcel Moolenaar 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
7369dde3bc9SDavid Xu 			if ((p->p_flag & P_SA) &&
7379104847fSDavid Xu 			     SIGISMEMBER(p->p_sigqueue.sq_signals, sig)) {
7389dde3bc9SDavid Xu 				p->p_flag |= P_SIGEVENT;
7399dde3bc9SDavid Xu 				wakeup(&p->p_siglist);
7409dde3bc9SDavid Xu 			}
7412c42a146SMarcel Moolenaar 			/* never to be seen again */
7429104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
7432c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
7442c42a146SMarcel Moolenaar 				/* easier in psignal */
74590af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
74690af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
747645682fdSLuoqi Chen 		} else {
74890af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigignore, sig);
7492c42a146SMarcel Moolenaar 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
75090af4afaSJohn Baldwin 				SIGDELSET(ps->ps_sigcatch, sig);
7512c42a146SMarcel Moolenaar 			else
75290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigcatch, sig);
7532c42a146SMarcel Moolenaar 		}
75423eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
75523eeeff7SPeter Wemm 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
75623eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
75723eeeff7SPeter Wemm 		    (flags & KSA_FREEBSD4) == 0)
75823eeeff7SPeter Wemm 			SIGDELSET(ps->ps_freebsd4, sig);
75923eeeff7SPeter Wemm 		else
76023eeeff7SPeter Wemm 			SIGADDSET(ps->ps_freebsd4, sig);
76123eeeff7SPeter Wemm #endif
762e8ebc08fSPeter Wemm #ifdef COMPAT_43
763645682fdSLuoqi Chen 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
76423eeeff7SPeter Wemm 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
76523eeeff7SPeter Wemm 		    (flags & KSA_OSIGSET) == 0)
766645682fdSLuoqi Chen 			SIGDELSET(ps->ps_osigset, sig);
767645682fdSLuoqi Chen 		else
768645682fdSLuoqi Chen 			SIGADDSET(ps->ps_osigset, sig);
769e8ebc08fSPeter Wemm #endif
770df8bae1dSRodney W. Grimes 	}
77190af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
772628d2653SJohn Baldwin 	PROC_UNLOCK(p);
7732c42a146SMarcel Moolenaar 	return (0);
7742c42a146SMarcel Moolenaar }
7752c42a146SMarcel Moolenaar 
7762c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
7772c42a146SMarcel Moolenaar struct sigaction_args {
7782c42a146SMarcel Moolenaar 	int	sig;
7792c42a146SMarcel Moolenaar 	struct	sigaction *act;
7802c42a146SMarcel Moolenaar 	struct	sigaction *oact;
7812c42a146SMarcel Moolenaar };
7822c42a146SMarcel Moolenaar #endif
783fb99ab88SMatthew Dillon /*
784fb99ab88SMatthew Dillon  * MPSAFE
785fb99ab88SMatthew Dillon  */
7862c42a146SMarcel Moolenaar int
787b40ce416SJulian Elischer sigaction(td, uap)
788b40ce416SJulian Elischer 	struct thread *td;
7892c42a146SMarcel Moolenaar 	register struct sigaction_args *uap;
7902c42a146SMarcel Moolenaar {
7912c42a146SMarcel Moolenaar 	struct sigaction act, oact;
7922c42a146SMarcel Moolenaar 	register struct sigaction *actp, *oactp;
7932c42a146SMarcel Moolenaar 	int error;
7942c42a146SMarcel Moolenaar 
7956f841fb7SMarcel Moolenaar 	actp = (uap->act != NULL) ? &act : NULL;
7966f841fb7SMarcel Moolenaar 	oactp = (uap->oact != NULL) ? &oact : NULL;
7972c42a146SMarcel Moolenaar 	if (actp) {
7986f841fb7SMarcel Moolenaar 		error = copyin(uap->act, actp, sizeof(act));
7992c42a146SMarcel Moolenaar 		if (error)
80044443757STim J. Robbins 			return (error);
8012c42a146SMarcel Moolenaar 	}
8028f19eb88SIan Dowse 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
80325d6dc06SJohn Baldwin 	if (oactp && !error)
8046f841fb7SMarcel Moolenaar 		error = copyout(oactp, uap->oact, sizeof(oact));
8052c42a146SMarcel Moolenaar 	return (error);
8062c42a146SMarcel Moolenaar }
8072c42a146SMarcel Moolenaar 
80823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4
80923eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_
81023eeeff7SPeter Wemm struct freebsd4_sigaction_args {
81123eeeff7SPeter Wemm 	int	sig;
81223eeeff7SPeter Wemm 	struct	sigaction *act;
81323eeeff7SPeter Wemm 	struct	sigaction *oact;
81423eeeff7SPeter Wemm };
81523eeeff7SPeter Wemm #endif
81623eeeff7SPeter Wemm /*
81723eeeff7SPeter Wemm  * MPSAFE
81823eeeff7SPeter Wemm  */
81923eeeff7SPeter Wemm int
82023eeeff7SPeter Wemm freebsd4_sigaction(td, uap)
82123eeeff7SPeter Wemm 	struct thread *td;
82223eeeff7SPeter Wemm 	register struct freebsd4_sigaction_args *uap;
82323eeeff7SPeter Wemm {
82423eeeff7SPeter Wemm 	struct sigaction act, oact;
82523eeeff7SPeter Wemm 	register struct sigaction *actp, *oactp;
82623eeeff7SPeter Wemm 	int error;
82723eeeff7SPeter Wemm 
82823eeeff7SPeter Wemm 
82923eeeff7SPeter Wemm 	actp = (uap->act != NULL) ? &act : NULL;
83023eeeff7SPeter Wemm 	oactp = (uap->oact != NULL) ? &oact : NULL;
83123eeeff7SPeter Wemm 	if (actp) {
83223eeeff7SPeter Wemm 		error = copyin(uap->act, actp, sizeof(act));
83323eeeff7SPeter Wemm 		if (error)
83444443757STim J. Robbins 			return (error);
83523eeeff7SPeter Wemm 	}
83623eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
837a14e1189SJohn Baldwin 	if (oactp && !error)
83823eeeff7SPeter Wemm 		error = copyout(oactp, uap->oact, sizeof(oact));
83923eeeff7SPeter Wemm 	return (error);
84023eeeff7SPeter Wemm }
84123eeeff7SPeter Wemm #endif	/* COMAPT_FREEBSD4 */
84223eeeff7SPeter Wemm 
84331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
8442c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
8452c42a146SMarcel Moolenaar struct osigaction_args {
8462c42a146SMarcel Moolenaar 	int	signum;
8472c42a146SMarcel Moolenaar 	struct	osigaction *nsa;
8482c42a146SMarcel Moolenaar 	struct	osigaction *osa;
8492c42a146SMarcel Moolenaar };
8502c42a146SMarcel Moolenaar #endif
851fb99ab88SMatthew Dillon /*
852fb99ab88SMatthew Dillon  * MPSAFE
853fb99ab88SMatthew Dillon  */
8542c42a146SMarcel Moolenaar int
855b40ce416SJulian Elischer osigaction(td, uap)
856b40ce416SJulian Elischer 	struct thread *td;
8572c42a146SMarcel Moolenaar 	register struct osigaction_args *uap;
8582c42a146SMarcel Moolenaar {
8592c42a146SMarcel Moolenaar 	struct osigaction sa;
8602c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
8612c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
8622c42a146SMarcel Moolenaar 	int error;
8632c42a146SMarcel Moolenaar 
8646f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
8656f841fb7SMarcel Moolenaar 		return (EINVAL);
866fb99ab88SMatthew Dillon 
8676f841fb7SMarcel Moolenaar 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
8686f841fb7SMarcel Moolenaar 	osap = (uap->osa != NULL) ? &osa : NULL;
869fb99ab88SMatthew Dillon 
8702c42a146SMarcel Moolenaar 	if (nsap) {
8716f841fb7SMarcel Moolenaar 		error = copyin(uap->nsa, &sa, sizeof(sa));
8722c42a146SMarcel Moolenaar 		if (error)
87344443757STim J. Robbins 			return (error);
8742c42a146SMarcel Moolenaar 		nsap->sa_handler = sa.sa_handler;
8752c42a146SMarcel Moolenaar 		nsap->sa_flags = sa.sa_flags;
8762c42a146SMarcel Moolenaar 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
8772c42a146SMarcel Moolenaar 	}
87823eeeff7SPeter Wemm 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
8792c42a146SMarcel Moolenaar 	if (osap && !error) {
8802c42a146SMarcel Moolenaar 		sa.sa_handler = osap->sa_handler;
8812c42a146SMarcel Moolenaar 		sa.sa_flags = osap->sa_flags;
8822c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
8836f841fb7SMarcel Moolenaar 		error = copyout(&sa, uap->osa, sizeof(sa));
8842c42a146SMarcel Moolenaar 	}
8852c42a146SMarcel Moolenaar 	return (error);
8862c42a146SMarcel Moolenaar }
88723eeeff7SPeter Wemm 
88823eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__)
88923eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */
89023eeeff7SPeter Wemm int
89123eeeff7SPeter Wemm osigreturn(td, uap)
89223eeeff7SPeter Wemm 	struct thread *td;
89323eeeff7SPeter Wemm 	struct osigreturn_args *uap;
89423eeeff7SPeter Wemm {
89523eeeff7SPeter Wemm 
89623eeeff7SPeter Wemm 	return (nosys(td, (struct nosys_args *)uap));
89723eeeff7SPeter Wemm }
89823eeeff7SPeter Wemm #endif
89931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
900df8bae1dSRodney W. Grimes 
901df8bae1dSRodney W. Grimes /*
902df8bae1dSRodney W. Grimes  * Initialize signal state for process 0;
903df8bae1dSRodney W. Grimes  * set to ignore signals that are ignored by default.
904df8bae1dSRodney W. Grimes  */
905df8bae1dSRodney W. Grimes void
906df8bae1dSRodney W. Grimes siginit(p)
907df8bae1dSRodney W. Grimes 	struct proc *p;
908df8bae1dSRodney W. Grimes {
909df8bae1dSRodney W. Grimes 	register int i;
91090af4afaSJohn Baldwin 	struct sigacts *ps;
911df8bae1dSRodney W. Grimes 
912628d2653SJohn Baldwin 	PROC_LOCK(p);
91390af4afaSJohn Baldwin 	ps = p->p_sigacts;
91490af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
9152c42a146SMarcel Moolenaar 	for (i = 1; i <= NSIG; i++)
9162c42a146SMarcel Moolenaar 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
91790af4afaSJohn Baldwin 			SIGADDSET(ps->ps_sigignore, i);
91890af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
919628d2653SJohn Baldwin 	PROC_UNLOCK(p);
920df8bae1dSRodney W. Grimes }
921df8bae1dSRodney W. Grimes 
922df8bae1dSRodney W. Grimes /*
923df8bae1dSRodney W. Grimes  * Reset signals for an exec of the specified process.
924df8bae1dSRodney W. Grimes  */
925df8bae1dSRodney W. Grimes void
926a30ec4b9SDavid Xu execsigs(struct proc *p)
927df8bae1dSRodney W. Grimes {
928a30ec4b9SDavid Xu 	struct sigacts *ps;
929a30ec4b9SDavid Xu 	int sig;
930a30ec4b9SDavid Xu 	struct thread *td;
931df8bae1dSRodney W. Grimes 
932df8bae1dSRodney W. Grimes 	/*
933df8bae1dSRodney W. Grimes 	 * Reset caught signals.  Held signals remain held
9344093529dSJeff Roberson 	 * through td_sigmask (unless they were caught,
935df8bae1dSRodney W. Grimes 	 * and are now ignored by default).
936df8bae1dSRodney W. Grimes 	 */
9379b3b1c5fSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
938a30ec4b9SDavid Xu 	td = FIRST_THREAD_IN_PROC(p);
939628d2653SJohn Baldwin 	ps = p->p_sigacts;
94090af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
94190af4afaSJohn Baldwin 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
94290af4afaSJohn Baldwin 		sig = sig_ffs(&ps->ps_sigcatch);
94390af4afaSJohn Baldwin 		SIGDELSET(ps->ps_sigcatch, sig);
9442c42a146SMarcel Moolenaar 		if (sigprop(sig) & SA_IGNORE) {
9452c42a146SMarcel Moolenaar 			if (sig != SIGCONT)
94690af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
9479104847fSDavid Xu 			sigqueue_delete_proc(p, sig);
948df8bae1dSRodney W. Grimes 		}
9492c42a146SMarcel Moolenaar 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
950df8bae1dSRodney W. Grimes 	}
951df8bae1dSRodney W. Grimes 	/*
952df8bae1dSRodney W. Grimes 	 * Reset stack state to the user stack.
953df8bae1dSRodney W. Grimes 	 * Clear set of signals caught on the signal stack.
954df8bae1dSRodney W. Grimes 	 */
955a30ec4b9SDavid Xu 	td->td_sigstk.ss_flags = SS_DISABLE;
956a30ec4b9SDavid Xu 	td->td_sigstk.ss_size = 0;
957a30ec4b9SDavid Xu 	td->td_sigstk.ss_sp = 0;
958a30ec4b9SDavid Xu 	td->td_pflags &= ~TDP_ALTSTACK;
95980e907a1SPeter Wemm 	/*
96080e907a1SPeter Wemm 	 * Reset no zombies if child dies flag as Solaris does.
96180e907a1SPeter Wemm 	 */
96290af4afaSJohn Baldwin 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
963c7fd62daSDavid Malone 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
964c7fd62daSDavid Malone 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
96590af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
966df8bae1dSRodney W. Grimes }
967df8bae1dSRodney W. Grimes 
968df8bae1dSRodney W. Grimes /*
969e77daab1SJohn Baldwin  * kern_sigprocmask()
9707c8fdcbdSMatthew Dillon  *
971628d2653SJohn Baldwin  *	Manipulate signal mask.
972df8bae1dSRodney W. Grimes  */
973e77daab1SJohn Baldwin int
974e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old)
9754093529dSJeff Roberson 	struct thread *td;
9762c42a146SMarcel Moolenaar 	int how;
9772c42a146SMarcel Moolenaar 	sigset_t *set, *oset;
978645682fdSLuoqi Chen 	int old;
9792c42a146SMarcel Moolenaar {
9802c42a146SMarcel Moolenaar 	int error;
9812c42a146SMarcel Moolenaar 
9824093529dSJeff Roberson 	PROC_LOCK(td->td_proc);
9832c42a146SMarcel Moolenaar 	if (oset != NULL)
9844093529dSJeff Roberson 		*oset = td->td_sigmask;
9852c42a146SMarcel Moolenaar 
9862c42a146SMarcel Moolenaar 	error = 0;
9872c42a146SMarcel Moolenaar 	if (set != NULL) {
9882c42a146SMarcel Moolenaar 		switch (how) {
9892c42a146SMarcel Moolenaar 		case SIG_BLOCK:
990645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
9914093529dSJeff Roberson 			SIGSETOR(td->td_sigmask, *set);
9922c42a146SMarcel Moolenaar 			break;
9932c42a146SMarcel Moolenaar 		case SIG_UNBLOCK:
9944093529dSJeff Roberson 			SIGSETNAND(td->td_sigmask, *set);
9954093529dSJeff Roberson 			signotify(td);
9962c42a146SMarcel Moolenaar 			break;
9972c42a146SMarcel Moolenaar 		case SIG_SETMASK:
998645682fdSLuoqi Chen 			SIG_CANTMASK(*set);
999645682fdSLuoqi Chen 			if (old)
10004093529dSJeff Roberson 				SIGSETLO(td->td_sigmask, *set);
1001645682fdSLuoqi Chen 			else
10024093529dSJeff Roberson 				td->td_sigmask = *set;
10034093529dSJeff Roberson 			signotify(td);
10042c42a146SMarcel Moolenaar 			break;
10052c42a146SMarcel Moolenaar 		default:
10062c42a146SMarcel Moolenaar 			error = EINVAL;
10072c42a146SMarcel Moolenaar 			break;
10082c42a146SMarcel Moolenaar 		}
10092c42a146SMarcel Moolenaar 	}
10104093529dSJeff Roberson 	PROC_UNLOCK(td->td_proc);
10112c42a146SMarcel Moolenaar 	return (error);
10122c42a146SMarcel Moolenaar }
10132c42a146SMarcel Moolenaar 
10147c8fdcbdSMatthew Dillon /*
1015e77daab1SJohn Baldwin  * sigprocmask() - MP SAFE
10167c8fdcbdSMatthew Dillon  */
10177c8fdcbdSMatthew Dillon 
1018d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1019df8bae1dSRodney W. Grimes struct sigprocmask_args {
1020df8bae1dSRodney W. Grimes 	int	how;
10212c42a146SMarcel Moolenaar 	const sigset_t *set;
10222c42a146SMarcel Moolenaar 	sigset_t *oset;
1023df8bae1dSRodney W. Grimes };
1024d2d3e875SBruce Evans #endif
102526f9a767SRodney W. Grimes int
1026b40ce416SJulian Elischer sigprocmask(td, uap)
1027b40ce416SJulian Elischer 	register struct thread *td;
1028df8bae1dSRodney W. Grimes 	struct sigprocmask_args *uap;
1029df8bae1dSRodney W. Grimes {
10302c42a146SMarcel Moolenaar 	sigset_t set, oset;
10312c42a146SMarcel Moolenaar 	sigset_t *setp, *osetp;
10322c42a146SMarcel Moolenaar 	int error;
1033df8bae1dSRodney W. Grimes 
10346f841fb7SMarcel Moolenaar 	setp = (uap->set != NULL) ? &set : NULL;
10356f841fb7SMarcel Moolenaar 	osetp = (uap->oset != NULL) ? &oset : NULL;
10362c42a146SMarcel Moolenaar 	if (setp) {
10376f841fb7SMarcel Moolenaar 		error = copyin(uap->set, setp, sizeof(set));
10382c42a146SMarcel Moolenaar 		if (error)
10392c42a146SMarcel Moolenaar 			return (error);
1040df8bae1dSRodney W. Grimes 	}
1041e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
10422c42a146SMarcel Moolenaar 	if (osetp && !error) {
10436f841fb7SMarcel Moolenaar 		error = copyout(osetp, uap->oset, sizeof(oset));
10442c42a146SMarcel Moolenaar 	}
10452c42a146SMarcel Moolenaar 	return (error);
10462c42a146SMarcel Moolenaar }
10472c42a146SMarcel Moolenaar 
104831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
10497c8fdcbdSMatthew Dillon /*
10507c8fdcbdSMatthew Dillon  * osigprocmask() - MP SAFE
10517c8fdcbdSMatthew Dillon  */
10522c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
10532c42a146SMarcel Moolenaar struct osigprocmask_args {
10542c42a146SMarcel Moolenaar 	int	how;
10552c42a146SMarcel Moolenaar 	osigset_t mask;
10562c42a146SMarcel Moolenaar };
10572c42a146SMarcel Moolenaar #endif
10582c42a146SMarcel Moolenaar int
1059b40ce416SJulian Elischer osigprocmask(td, uap)
1060b40ce416SJulian Elischer 	register struct thread *td;
10612c42a146SMarcel Moolenaar 	struct osigprocmask_args *uap;
10622c42a146SMarcel Moolenaar {
10632c42a146SMarcel Moolenaar 	sigset_t set, oset;
10642c42a146SMarcel Moolenaar 	int error;
10652c42a146SMarcel Moolenaar 
10662c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
1067e77daab1SJohn Baldwin 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1068b40ce416SJulian Elischer 	SIG2OSIG(oset, td->td_retval[0]);
1069df8bae1dSRodney W. Grimes 	return (error);
1070df8bae1dSRodney W. Grimes }
107131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1072df8bae1dSRodney W. Grimes 
1073fb99ab88SMatthew Dillon /*
1074fb99ab88SMatthew Dillon  * MPSAFE
1075fb99ab88SMatthew Dillon  */
107626f9a767SRodney W. Grimes int
1077a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap)
1078a447cd8bSJeff Roberson {
10799104847fSDavid Xu 	ksiginfo_t ksi;
1080a447cd8bSJeff Roberson 	sigset_t set;
1081a447cd8bSJeff Roberson 	int error;
1082a447cd8bSJeff Roberson 
1083a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
108436939a0aSDavid Xu 	if (error) {
108536939a0aSDavid Xu 		td->td_retval[0] = error;
108636939a0aSDavid Xu 		return (0);
108736939a0aSDavid Xu 	}
1088a447cd8bSJeff Roberson 
10899104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
109036939a0aSDavid Xu 	if (error) {
109136939a0aSDavid Xu 		if (error == ERESTART)
1092a447cd8bSJeff Roberson 			return (error);
109336939a0aSDavid Xu 		td->td_retval[0] = error;
109436939a0aSDavid Xu 		return (0);
109536939a0aSDavid Xu 	}
1096a447cd8bSJeff Roberson 
10979104847fSDavid Xu 	error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
109836939a0aSDavid Xu 	td->td_retval[0] = error;
109936939a0aSDavid Xu 	return (0);
1100a447cd8bSJeff Roberson }
1101a447cd8bSJeff Roberson /*
1102a447cd8bSJeff Roberson  * MPSAFE
1103a447cd8bSJeff Roberson  */
1104a447cd8bSJeff Roberson int
1105a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1106a447cd8bSJeff Roberson {
1107a447cd8bSJeff Roberson 	struct timespec ts;
1108a447cd8bSJeff Roberson 	struct timespec *timeout;
1109a447cd8bSJeff Roberson 	sigset_t set;
11109104847fSDavid Xu 	ksiginfo_t ksi;
1111a447cd8bSJeff Roberson 	int error;
1112a447cd8bSJeff Roberson 
1113a447cd8bSJeff Roberson 	if (uap->timeout) {
1114a447cd8bSJeff Roberson 		error = copyin(uap->timeout, &ts, sizeof(ts));
1115a447cd8bSJeff Roberson 		if (error)
1116a447cd8bSJeff Roberson 			return (error);
1117a447cd8bSJeff Roberson 
1118a447cd8bSJeff Roberson 		timeout = &ts;
1119a447cd8bSJeff Roberson 	} else
1120a447cd8bSJeff Roberson 		timeout = NULL;
1121a447cd8bSJeff Roberson 
1122a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1123a447cd8bSJeff Roberson 	if (error)
1124a447cd8bSJeff Roberson 		return (error);
1125a447cd8bSJeff Roberson 
11269104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, timeout);
1127a447cd8bSJeff Roberson 	if (error)
1128a447cd8bSJeff Roberson 		return (error);
11299dde3bc9SDavid Xu 
1130418228dfSDavid Xu 	if (uap->info)
11319104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
11329104847fSDavid Xu 
11339104847fSDavid Xu 	if (error == 0)
11349104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1135a447cd8bSJeff Roberson 	return (error);
1136a447cd8bSJeff Roberson }
1137a447cd8bSJeff Roberson 
1138a447cd8bSJeff Roberson /*
1139a447cd8bSJeff Roberson  * MPSAFE
1140a447cd8bSJeff Roberson  */
1141a447cd8bSJeff Roberson int
1142a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1143a447cd8bSJeff Roberson {
11449104847fSDavid Xu 	ksiginfo_t ksi;
1145a447cd8bSJeff Roberson 	sigset_t set;
1146a447cd8bSJeff Roberson 	int error;
1147a447cd8bSJeff Roberson 
1148a447cd8bSJeff Roberson 	error = copyin(uap->set, &set, sizeof(set));
1149a447cd8bSJeff Roberson 	if (error)
1150a447cd8bSJeff Roberson 		return (error);
1151a447cd8bSJeff Roberson 
11529104847fSDavid Xu 	error = kern_sigtimedwait(td, set, &ksi, NULL);
1153a447cd8bSJeff Roberson 	if (error)
1154a447cd8bSJeff Roberson 		return (error);
1155a447cd8bSJeff Roberson 
1156418228dfSDavid Xu 	if (uap->info)
11579104847fSDavid Xu 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
11589104847fSDavid Xu 
11599104847fSDavid Xu 	if (error == 0)
11609104847fSDavid Xu 		td->td_retval[0] = ksi.ksi_signo;
1161a447cd8bSJeff Roberson 	return (error);
1162a447cd8bSJeff Roberson }
1163a447cd8bSJeff Roberson 
1164a447cd8bSJeff Roberson static int
11659104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1166a447cd8bSJeff Roberson 	struct timespec *timeout)
1167a447cd8bSJeff Roberson {
11683074d1b4SDavid Xu 	struct sigacts *ps;
11696675b36eSDavid Xu 	sigset_t savedmask;
1170a447cd8bSJeff Roberson 	struct proc *p;
11711089f031SDavid Xu 	int error, sig, hz, i, timevalid = 0;
11721089f031SDavid Xu 	struct timespec rts, ets, ts;
11731089f031SDavid Xu 	struct timeval tv;
1174a447cd8bSJeff Roberson 
1175a447cd8bSJeff Roberson 	p = td->td_proc;
1176a447cd8bSJeff Roberson 	error = 0;
1177a447cd8bSJeff Roberson 	sig = 0;
11783074d1b4SDavid Xu 	SIG_CANTMASK(waitset);
1179a447cd8bSJeff Roberson 
1180a447cd8bSJeff Roberson 	PROC_LOCK(p);
1181a447cd8bSJeff Roberson 	ps = p->p_sigacts;
11823074d1b4SDavid Xu 	savedmask = td->td_sigmask;
11831089f031SDavid Xu 	if (timeout) {
11841089f031SDavid Xu 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
11851089f031SDavid Xu 			timevalid = 1;
11861089f031SDavid Xu 			getnanouptime(&rts);
11871089f031SDavid Xu 		 	ets = rts;
11881089f031SDavid Xu 			timespecadd(&ets, timeout);
11891089f031SDavid Xu 		}
11901089f031SDavid Xu 	}
11913074d1b4SDavid Xu 
11923074d1b4SDavid Xu again:
11933074d1b4SDavid Xu 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
11943074d1b4SDavid Xu 		if (!SIGISMEMBER(waitset, i))
11953074d1b4SDavid Xu 			continue;
11969104847fSDavid Xu 		if (SIGISMEMBER(td->td_sigqueue.sq_signals, i)) {
1197418228dfSDavid Xu 			SIGFILLSET(td->td_sigmask);
1198418228dfSDavid Xu 			SIG_CANTMASK(td->td_sigmask);
11993074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
120090af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
1201a447cd8bSJeff Roberson 			sig = cursig(td);
12023074d1b4SDavid Xu 			i = 0;
12033074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
12049104847fSDavid Xu 		} else if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) {
12053074d1b4SDavid Xu 			if (p->p_flag & P_SA) {
12063074d1b4SDavid Xu 				p->p_flag |= P_SIGEVENT;
12073074d1b4SDavid Xu 				wakeup(&p->p_siglist);
12083074d1b4SDavid Xu 			}
12099104847fSDavid Xu 			sigqueue_move(&p->p_sigqueue, &td->td_sigqueue, i);
12103074d1b4SDavid Xu 			SIGFILLSET(td->td_sigmask);
12113074d1b4SDavid Xu 			SIG_CANTMASK(td->td_sigmask);
12123074d1b4SDavid Xu 			SIGDELSET(td->td_sigmask, i);
12133074d1b4SDavid Xu 			mtx_lock(&ps->ps_mtx);
12143074d1b4SDavid Xu 			sig = cursig(td);
12153074d1b4SDavid Xu 			i = 0;
12163074d1b4SDavid Xu 			mtx_unlock(&ps->ps_mtx);
12173074d1b4SDavid Xu 		}
12186675b36eSDavid Xu 		if (sig)
1219a447cd8bSJeff Roberson 			goto out;
12203074d1b4SDavid Xu 	}
12213074d1b4SDavid Xu 	if (error)
12223074d1b4SDavid Xu 		goto out;
12233074d1b4SDavid Xu 
1224a447cd8bSJeff Roberson 	/*
1225a447cd8bSJeff Roberson 	 * POSIX says this must be checked after looking for pending
1226a447cd8bSJeff Roberson 	 * signals.
1227a447cd8bSJeff Roberson 	 */
1228a447cd8bSJeff Roberson 	if (timeout) {
12291089f031SDavid Xu 		if (!timevalid) {
1230a447cd8bSJeff Roberson 			error = EINVAL;
1231a447cd8bSJeff Roberson 			goto out;
1232a447cd8bSJeff Roberson 		}
12331089f031SDavid Xu 		getnanouptime(&rts);
12341089f031SDavid Xu 		if (timespeccmp(&rts, &ets, >=)) {
12353074d1b4SDavid Xu 			error = EAGAIN;
12363074d1b4SDavid Xu 			goto out;
12373074d1b4SDavid Xu 		}
12381089f031SDavid Xu 		ts = ets;
12391089f031SDavid Xu 		timespecsub(&ts, &rts);
12401089f031SDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
1241a447cd8bSJeff Roberson 		hz = tvtohz(&tv);
1242a447cd8bSJeff Roberson 	} else
1243a447cd8bSJeff Roberson 		hz = 0;
1244a447cd8bSJeff Roberson 
12456675b36eSDavid Xu 	td->td_sigmask = savedmask;
12466675b36eSDavid Xu 	SIGSETNAND(td->td_sigmask, waitset);
12476675b36eSDavid Xu 	signotify(td);
124886b5e563SDag-Erling Smørgrav 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
12491089f031SDavid Xu 	if (timeout) {
12501089f031SDavid Xu 		if (error == ERESTART) {
12511089f031SDavid Xu 			/* timeout can not be restarted. */
12523074d1b4SDavid Xu 			error = EINTR;
12531089f031SDavid Xu 		} else if (error == EAGAIN) {
12541089f031SDavid Xu 			/* will calculate timeout by ourself. */
12551089f031SDavid Xu 			error = 0;
12561089f031SDavid Xu 		}
12571089f031SDavid Xu 	}
12583074d1b4SDavid Xu 	goto again;
12599dde3bc9SDavid Xu 
1260a447cd8bSJeff Roberson out:
1261a447cd8bSJeff Roberson 	if (sig) {
1262a447cd8bSJeff Roberson 		sig_t action;
1263a447cd8bSJeff Roberson 
12645da49fcbSDavid Xu 		ksiginfo_init(ksi);
12659104847fSDavid Xu 		sigqueue_get(&td->td_sigqueue, sig, ksi);
12669104847fSDavid Xu 		ksi->ksi_signo = sig;
126756c06c4bSDavid Xu 		if (ksi->ksi_code == SI_TIMER)
126856c06c4bSDavid Xu 			itimer_accept(p, ksi->ksi_timerid, ksi);
12693074d1b4SDavid Xu 		error = 0;
12703074d1b4SDavid Xu 		mtx_lock(&ps->ps_mtx);
1271a447cd8bSJeff Roberson 		action = ps->ps_sigact[_SIG_IDX(sig)];
127290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
1273a447cd8bSJeff Roberson #ifdef KTRACE
1274a447cd8bSJeff Roberson 		if (KTRPOINT(td, KTR_PSIG))
12753074d1b4SDavid Xu 			ktrpsig(sig, action, &td->td_sigmask, 0);
1276a447cd8bSJeff Roberson #endif
1277a447cd8bSJeff Roberson 		_STOPEVENT(p, S_SIG, sig);
1278a447cd8bSJeff Roberson 
12793074d1b4SDavid Xu 	}
12809104847fSDavid Xu 	td->td_sigmask = savedmask;
12819104847fSDavid Xu 	signotify(td);
1282a447cd8bSJeff Roberson 	PROC_UNLOCK(p);
1283a447cd8bSJeff Roberson 	return (error);
1284a447cd8bSJeff Roberson }
1285a447cd8bSJeff Roberson 
12869104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
12879104847fSDavid Xu struct sigpending_args {
12889104847fSDavid Xu 	sigset_t	*set;
12899104847fSDavid Xu };
12909104847fSDavid Xu #endif
1291a447cd8bSJeff Roberson /*
1292a447cd8bSJeff Roberson  * MPSAFE
1293a447cd8bSJeff Roberson  */
1294a447cd8bSJeff Roberson int
1295b40ce416SJulian Elischer sigpending(td, uap)
1296b40ce416SJulian Elischer 	struct thread *td;
1297df8bae1dSRodney W. Grimes 	struct sigpending_args *uap;
1298df8bae1dSRodney W. Grimes {
1299b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13009104847fSDavid Xu 	sigset_t pending;
1301df8bae1dSRodney W. Grimes 
1302628d2653SJohn Baldwin 	PROC_LOCK(p);
13039104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
13049104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1305628d2653SJohn Baldwin 	PROC_UNLOCK(p);
13069104847fSDavid Xu 	return (copyout(&pending, uap->set, sizeof(sigset_t)));
13072c42a146SMarcel Moolenaar }
13082c42a146SMarcel Moolenaar 
130931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
13102c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
13112c42a146SMarcel Moolenaar struct osigpending_args {
13122c42a146SMarcel Moolenaar 	int	dummy;
13132c42a146SMarcel Moolenaar };
13142c42a146SMarcel Moolenaar #endif
1315fb99ab88SMatthew Dillon /*
1316fb99ab88SMatthew Dillon  * MPSAFE
1317fb99ab88SMatthew Dillon  */
13182c42a146SMarcel Moolenaar int
1319b40ce416SJulian Elischer osigpending(td, uap)
1320b40ce416SJulian Elischer 	struct thread *td;
13212c42a146SMarcel Moolenaar 	struct osigpending_args *uap;
13222c42a146SMarcel Moolenaar {
1323b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13249104847fSDavid Xu 	sigset_t pending;
1325b40ce416SJulian Elischer 
1326628d2653SJohn Baldwin 	PROC_LOCK(p);
13279104847fSDavid Xu 	pending = p->p_sigqueue.sq_signals;
13289104847fSDavid Xu 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1329628d2653SJohn Baldwin 	PROC_UNLOCK(p);
13309104847fSDavid Xu 	SIG2OSIG(pending, td->td_retval[0]);
1331df8bae1dSRodney W. Grimes 	return (0);
1332df8bae1dSRodney W. Grimes }
133331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1334df8bae1dSRodney W. Grimes 
13351930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1336df8bae1dSRodney W. Grimes /*
1337df8bae1dSRodney W. Grimes  * Generalized interface signal handler, 4.3-compatible.
1338df8bae1dSRodney W. Grimes  */
1339d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1340df8bae1dSRodney W. Grimes struct osigvec_args {
1341df8bae1dSRodney W. Grimes 	int	signum;
1342df8bae1dSRodney W. Grimes 	struct	sigvec *nsv;
1343df8bae1dSRodney W. Grimes 	struct	sigvec *osv;
1344df8bae1dSRodney W. Grimes };
1345d2d3e875SBruce Evans #endif
1346fb99ab88SMatthew Dillon /*
1347fb99ab88SMatthew Dillon  * MPSAFE
1348fb99ab88SMatthew Dillon  */
1349df8bae1dSRodney W. Grimes /* ARGSUSED */
135026f9a767SRodney W. Grimes int
1351b40ce416SJulian Elischer osigvec(td, uap)
1352b40ce416SJulian Elischer 	struct thread *td;
1353df8bae1dSRodney W. Grimes 	register struct osigvec_args *uap;
1354df8bae1dSRodney W. Grimes {
1355df8bae1dSRodney W. Grimes 	struct sigvec vec;
13562c42a146SMarcel Moolenaar 	struct sigaction nsa, osa;
13572c42a146SMarcel Moolenaar 	register struct sigaction *nsap, *osap;
13582c42a146SMarcel Moolenaar 	int error;
1359df8bae1dSRodney W. Grimes 
13606f841fb7SMarcel Moolenaar 	if (uap->signum <= 0 || uap->signum >= ONSIG)
13616f841fb7SMarcel Moolenaar 		return (EINVAL);
13626f841fb7SMarcel Moolenaar 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
13636f841fb7SMarcel Moolenaar 	osap = (uap->osv != NULL) ? &osa : NULL;
13642c42a146SMarcel Moolenaar 	if (nsap) {
13656f841fb7SMarcel Moolenaar 		error = copyin(uap->nsv, &vec, sizeof(vec));
13662c42a146SMarcel Moolenaar 		if (error)
1367df8bae1dSRodney W. Grimes 			return (error);
13682c42a146SMarcel Moolenaar 		nsap->sa_handler = vec.sv_handler;
13692c42a146SMarcel Moolenaar 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
13702c42a146SMarcel Moolenaar 		nsap->sa_flags = vec.sv_flags;
13712c42a146SMarcel Moolenaar 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1372df8bae1dSRodney W. Grimes 	}
13735edadff9SJohn Baldwin 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
13742c42a146SMarcel Moolenaar 	if (osap && !error) {
13752c42a146SMarcel Moolenaar 		vec.sv_handler = osap->sa_handler;
13762c42a146SMarcel Moolenaar 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
13772c42a146SMarcel Moolenaar 		vec.sv_flags = osap->sa_flags;
13782c42a146SMarcel Moolenaar 		vec.sv_flags &= ~SA_NOCLDWAIT;
13792c42a146SMarcel Moolenaar 		vec.sv_flags ^= SA_RESTART;
13806f841fb7SMarcel Moolenaar 		error = copyout(&vec, uap->osv, sizeof(vec));
13812c42a146SMarcel Moolenaar 	}
13822c42a146SMarcel Moolenaar 	return (error);
1383df8bae1dSRodney W. Grimes }
1384df8bae1dSRodney W. Grimes 
1385d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1386df8bae1dSRodney W. Grimes struct osigblock_args {
1387df8bae1dSRodney W. Grimes 	int	mask;
1388df8bae1dSRodney W. Grimes };
1389d2d3e875SBruce Evans #endif
1390fb99ab88SMatthew Dillon /*
1391fb99ab88SMatthew Dillon  * MPSAFE
1392fb99ab88SMatthew Dillon  */
139326f9a767SRodney W. Grimes int
1394b40ce416SJulian Elischer osigblock(td, uap)
1395b40ce416SJulian Elischer 	register struct thread *td;
1396df8bae1dSRodney W. Grimes 	struct osigblock_args *uap;
1397df8bae1dSRodney W. Grimes {
1398b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13992c42a146SMarcel Moolenaar 	sigset_t set;
1400df8bae1dSRodney W. Grimes 
14012c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
14022c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1403628d2653SJohn Baldwin 	PROC_LOCK(p);
14044093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
14054093529dSJeff Roberson 	SIGSETOR(td->td_sigmask, set);
1406628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1407df8bae1dSRodney W. Grimes 	return (0);
1408df8bae1dSRodney W. Grimes }
1409df8bae1dSRodney W. Grimes 
1410d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1411df8bae1dSRodney W. Grimes struct osigsetmask_args {
1412df8bae1dSRodney W. Grimes 	int	mask;
1413df8bae1dSRodney W. Grimes };
1414d2d3e875SBruce Evans #endif
1415fb99ab88SMatthew Dillon /*
1416fb99ab88SMatthew Dillon  * MPSAFE
1417fb99ab88SMatthew Dillon  */
141826f9a767SRodney W. Grimes int
1419b40ce416SJulian Elischer osigsetmask(td, uap)
1420b40ce416SJulian Elischer 	struct thread *td;
1421df8bae1dSRodney W. Grimes 	struct osigsetmask_args *uap;
1422df8bae1dSRodney W. Grimes {
1423b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
14242c42a146SMarcel Moolenaar 	sigset_t set;
1425df8bae1dSRodney W. Grimes 
14262c42a146SMarcel Moolenaar 	OSIG2SIG(uap->mask, set);
14272c42a146SMarcel Moolenaar 	SIG_CANTMASK(set);
1428628d2653SJohn Baldwin 	PROC_LOCK(p);
14294093529dSJeff Roberson 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
14304093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, set);
14314093529dSJeff Roberson 	signotify(td);
1432628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1433df8bae1dSRodney W. Grimes 	return (0);
1434df8bae1dSRodney W. Grimes }
14351930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1436df8bae1dSRodney W. Grimes 
1437df8bae1dSRodney W. Grimes /*
14389104847fSDavid Xu  * Suspend calling thread until signal, providing mask to be set
143997563428SAlexander Kabaev  * in the meantime.
1440df8bae1dSRodney W. Grimes  */
1441d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1442df8bae1dSRodney W. Grimes struct sigsuspend_args {
14432c42a146SMarcel Moolenaar 	const sigset_t *sigmask;
1444df8bae1dSRodney W. Grimes };
1445d2d3e875SBruce Evans #endif
1446fb99ab88SMatthew Dillon /*
1447fb99ab88SMatthew Dillon  * MPSAFE
1448fb99ab88SMatthew Dillon  */
1449df8bae1dSRodney W. Grimes /* ARGSUSED */
145026f9a767SRodney W. Grimes int
1451b40ce416SJulian Elischer sigsuspend(td, uap)
1452b40ce416SJulian Elischer 	struct thread *td;
1453df8bae1dSRodney W. Grimes 	struct sigsuspend_args *uap;
1454df8bae1dSRodney W. Grimes {
14552c42a146SMarcel Moolenaar 	sigset_t mask;
14562c42a146SMarcel Moolenaar 	int error;
14572c42a146SMarcel Moolenaar 
14586f841fb7SMarcel Moolenaar 	error = copyin(uap->sigmask, &mask, sizeof(mask));
14592c42a146SMarcel Moolenaar 	if (error)
14602c42a146SMarcel Moolenaar 		return (error);
14618f19eb88SIan Dowse 	return (kern_sigsuspend(td, mask));
14628f19eb88SIan Dowse }
14638f19eb88SIan Dowse 
14648f19eb88SIan Dowse int
14658f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask)
14668f19eb88SIan Dowse {
14678f19eb88SIan Dowse 	struct proc *p = td->td_proc;
1468df8bae1dSRodney W. Grimes 
1469df8bae1dSRodney W. Grimes 	/*
1470645682fdSLuoqi Chen 	 * When returning from sigsuspend, we want
1471df8bae1dSRodney W. Grimes 	 * the old mask to be restored after the
1472df8bae1dSRodney W. Grimes 	 * signal handler has finished.  Thus, we
1473df8bae1dSRodney W. Grimes 	 * save it here and mark the sigacts structure
1474df8bae1dSRodney W. Grimes 	 * to indicate this.
1475df8bae1dSRodney W. Grimes 	 */
1476628d2653SJohn Baldwin 	PROC_LOCK(p);
14774093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
14785e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1479645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
14804093529dSJeff Roberson 	td->td_sigmask = mask;
14814093529dSJeff Roberson 	signotify(td);
148286b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
14832c42a146SMarcel Moolenaar 		/* void */;
1484628d2653SJohn Baldwin 	PROC_UNLOCK(p);
14852c42a146SMarcel Moolenaar 	/* always return EINTR rather than ERESTART... */
14862c42a146SMarcel Moolenaar 	return (EINTR);
14872c42a146SMarcel Moolenaar }
14882c42a146SMarcel Moolenaar 
148931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
149097563428SAlexander Kabaev /*
149197563428SAlexander Kabaev  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
149297563428SAlexander Kabaev  * convention: libc stub passes mask, not pointer, to save a copyin.
149397563428SAlexander Kabaev  */
14942c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_
14952c42a146SMarcel Moolenaar struct osigsuspend_args {
14962c42a146SMarcel Moolenaar 	osigset_t mask;
14972c42a146SMarcel Moolenaar };
14982c42a146SMarcel Moolenaar #endif
1499fb99ab88SMatthew Dillon /*
1500fb99ab88SMatthew Dillon  * MPSAFE
1501fb99ab88SMatthew Dillon  */
15022c42a146SMarcel Moolenaar /* ARGSUSED */
15032c42a146SMarcel Moolenaar int
1504b40ce416SJulian Elischer osigsuspend(td, uap)
1505b40ce416SJulian Elischer 	struct thread *td;
15062c42a146SMarcel Moolenaar 	struct osigsuspend_args *uap;
15072c42a146SMarcel Moolenaar {
1508b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1509645682fdSLuoqi Chen 	sigset_t mask;
15102c42a146SMarcel Moolenaar 
1511628d2653SJohn Baldwin 	PROC_LOCK(p);
15124093529dSJeff Roberson 	td->td_oldsigmask = td->td_sigmask;
15135e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_OLDMASK;
1514645682fdSLuoqi Chen 	OSIG2SIG(uap->mask, mask);
1515645682fdSLuoqi Chen 	SIG_CANTMASK(mask);
15164093529dSJeff Roberson 	SIGSETLO(td->td_sigmask, mask);
15174093529dSJeff Roberson 	signotify(td);
151886b5e563SDag-Erling Smørgrav 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1519df8bae1dSRodney W. Grimes 		/* void */;
1520628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1521df8bae1dSRodney W. Grimes 	/* always return EINTR rather than ERESTART... */
1522df8bae1dSRodney W. Grimes 	return (EINTR);
1523df8bae1dSRodney W. Grimes }
152431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */
1525df8bae1dSRodney W. Grimes 
15261930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1527d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1528df8bae1dSRodney W. Grimes struct osigstack_args {
1529df8bae1dSRodney W. Grimes 	struct	sigstack *nss;
1530df8bae1dSRodney W. Grimes 	struct	sigstack *oss;
1531df8bae1dSRodney W. Grimes };
1532d2d3e875SBruce Evans #endif
1533fb99ab88SMatthew Dillon /*
1534fb99ab88SMatthew Dillon  * MPSAFE
1535fb99ab88SMatthew Dillon  */
1536df8bae1dSRodney W. Grimes /* ARGSUSED */
153726f9a767SRodney W. Grimes int
1538b40ce416SJulian Elischer osigstack(td, uap)
1539b40ce416SJulian Elischer 	struct thread *td;
1540df8bae1dSRodney W. Grimes 	register struct osigstack_args *uap;
1541df8bae1dSRodney W. Grimes {
15425afe0c99SJohn Baldwin 	struct sigstack nss, oss;
1543fb99ab88SMatthew Dillon 	int error = 0;
1544fb99ab88SMatthew Dillon 
1545d034d459SMarcel Moolenaar 	if (uap->nss != NULL) {
15465afe0c99SJohn Baldwin 		error = copyin(uap->nss, &nss, sizeof(nss));
15475afe0c99SJohn Baldwin 		if (error)
15485afe0c99SJohn Baldwin 			return (error);
1549df8bae1dSRodney W. Grimes 	}
1550a30ec4b9SDavid Xu 	oss.ss_sp = td->td_sigstk.ss_sp;
15515afe0c99SJohn Baldwin 	oss.ss_onstack = sigonstack(cpu_getstack(td));
15525afe0c99SJohn Baldwin 	if (uap->nss != NULL) {
1553a30ec4b9SDavid Xu 		td->td_sigstk.ss_sp = nss.ss_sp;
1554a30ec4b9SDavid Xu 		td->td_sigstk.ss_size = 0;
1555a30ec4b9SDavid Xu 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1556a30ec4b9SDavid Xu 		td->td_pflags |= TDP_ALTSTACK;
15575afe0c99SJohn Baldwin 	}
15585afe0c99SJohn Baldwin 	if (uap->oss != NULL)
15595afe0c99SJohn Baldwin 		error = copyout(&oss, uap->oss, sizeof(oss));
15605afe0c99SJohn Baldwin 
1561fb99ab88SMatthew Dillon 	return (error);
1562df8bae1dSRodney W. Grimes }
15631930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1564df8bae1dSRodney W. Grimes 
1565d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1566df8bae1dSRodney W. Grimes struct sigaltstack_args {
15672c42a146SMarcel Moolenaar 	stack_t	*ss;
15682c42a146SMarcel Moolenaar 	stack_t	*oss;
1569df8bae1dSRodney W. Grimes };
1570d2d3e875SBruce Evans #endif
1571fb99ab88SMatthew Dillon /*
1572fb99ab88SMatthew Dillon  * MPSAFE
1573fb99ab88SMatthew Dillon  */
1574df8bae1dSRodney W. Grimes /* ARGSUSED */
157526f9a767SRodney W. Grimes int
1576b40ce416SJulian Elischer sigaltstack(td, uap)
1577b40ce416SJulian Elischer 	struct thread *td;
1578df8bae1dSRodney W. Grimes 	register struct sigaltstack_args *uap;
1579df8bae1dSRodney W. Grimes {
15808f19eb88SIan Dowse 	stack_t ss, oss;
15818f19eb88SIan Dowse 	int error;
15828f19eb88SIan Dowse 
15838f19eb88SIan Dowse 	if (uap->ss != NULL) {
15848f19eb88SIan Dowse 		error = copyin(uap->ss, &ss, sizeof(ss));
15858f19eb88SIan Dowse 		if (error)
15868f19eb88SIan Dowse 			return (error);
15878f19eb88SIan Dowse 	}
15888f19eb88SIan Dowse 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
15898f19eb88SIan Dowse 	    (uap->oss != NULL) ? &oss : NULL);
15908f19eb88SIan Dowse 	if (error)
15918f19eb88SIan Dowse 		return (error);
15928f19eb88SIan Dowse 	if (uap->oss != NULL)
15938f19eb88SIan Dowse 		error = copyout(&oss, uap->oss, sizeof(stack_t));
15948f19eb88SIan Dowse 	return (error);
15958f19eb88SIan Dowse }
15968f19eb88SIan Dowse 
15978f19eb88SIan Dowse int
15988f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
15998f19eb88SIan Dowse {
1600b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1601fb99ab88SMatthew Dillon 	int oonstack;
1602fb99ab88SMatthew Dillon 
1603b40ce416SJulian Elischer 	oonstack = sigonstack(cpu_getstack(td));
1604d034d459SMarcel Moolenaar 
16058f19eb88SIan Dowse 	if (oss != NULL) {
1606a30ec4b9SDavid Xu 		*oss = td->td_sigstk;
1607a30ec4b9SDavid Xu 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1608d034d459SMarcel Moolenaar 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1609df8bae1dSRodney W. Grimes 	}
1610d034d459SMarcel Moolenaar 
16118f19eb88SIan Dowse 	if (ss != NULL) {
1612a30ec4b9SDavid Xu 		if (oonstack)
1613cf60731bSJohn Baldwin 			return (EPERM);
1614a30ec4b9SDavid Xu 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1615cf60731bSJohn Baldwin 			return (EINVAL);
16168f19eb88SIan Dowse 		if (!(ss->ss_flags & SS_DISABLE)) {
16179104847fSDavid Xu 			if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1618cf60731bSJohn Baldwin 				return (ENOMEM);
16199104847fSDavid Xu 
1620a30ec4b9SDavid Xu 			td->td_sigstk = *ss;
1621a30ec4b9SDavid Xu 			td->td_pflags |= TDP_ALTSTACK;
1622628d2653SJohn Baldwin 		} else {
1623a30ec4b9SDavid Xu 			td->td_pflags &= ~TDP_ALTSTACK;
1624628d2653SJohn Baldwin 		}
1625d034d459SMarcel Moolenaar 	}
1626cf60731bSJohn Baldwin 	return (0);
1627df8bae1dSRodney W. Grimes }
1628df8bae1dSRodney W. Grimes 
1629d93f860cSPoul-Henning Kamp /*
1630d93f860cSPoul-Henning Kamp  * Common code for kill process group/broadcast kill.
1631d93f860cSPoul-Henning Kamp  * cp is calling process.
1632d93f860cSPoul-Henning Kamp  */
163337c84183SPoul-Henning Kamp static int
16341a88a252SMaxim Sobolev killpg1(td, sig, pgid, all)
16359c1ab3e0SJohn Baldwin 	register struct thread *td;
16361a88a252SMaxim Sobolev 	int sig, pgid, all;
1637d93f860cSPoul-Henning Kamp {
1638d93f860cSPoul-Henning Kamp 	register struct proc *p;
1639d93f860cSPoul-Henning Kamp 	struct pgrp *pgrp;
1640d93f860cSPoul-Henning Kamp 	int nfound = 0;
1641d93f860cSPoul-Henning Kamp 
1642553629ebSJake Burkholder 	if (all) {
1643d93f860cSPoul-Henning Kamp 		/*
1644d93f860cSPoul-Henning Kamp 		 * broadcast
1645d93f860cSPoul-Henning Kamp 		 */
16461005a129SJohn Baldwin 		sx_slock(&allproc_lock);
16472e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1648628d2653SJohn Baldwin 			PROC_LOCK(p);
16499c1ab3e0SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
16509c1ab3e0SJohn Baldwin 			    p == td->td_proc) {
1651628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1652628d2653SJohn Baldwin 				continue;
1653628d2653SJohn Baldwin 			}
16541a88a252SMaxim Sobolev 			if (p_cansignal(td, p, sig) == 0) {
1655d93f860cSPoul-Henning Kamp 				nfound++;
165633a9ed9dSJohn Baldwin 				if (sig)
16572c42a146SMarcel Moolenaar 					psignal(p, sig);
1658628d2653SJohn Baldwin 			}
165933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1660d93f860cSPoul-Henning Kamp 		}
16611005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1662553629ebSJake Burkholder 	} else {
1663ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1664f591779bSSeigo Tanimura 		if (pgid == 0) {
1665d93f860cSPoul-Henning Kamp 			/*
1666d93f860cSPoul-Henning Kamp 			 * zero pgid means send to my process group.
1667d93f860cSPoul-Henning Kamp 			 */
16689c1ab3e0SJohn Baldwin 			pgrp = td->td_proc->p_pgrp;
1669f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
1670f591779bSSeigo Tanimura 		} else {
1671d93f860cSPoul-Henning Kamp 			pgrp = pgfind(pgid);
1672f591779bSSeigo Tanimura 			if (pgrp == NULL) {
1673ba626c1dSJohn Baldwin 				sx_sunlock(&proctree_lock);
1674d93f860cSPoul-Henning Kamp 				return (ESRCH);
1675d93f860cSPoul-Henning Kamp 			}
1676f591779bSSeigo Tanimura 		}
1677ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
16782e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1679628d2653SJohn Baldwin 			PROC_LOCK(p);
1680628d2653SJohn Baldwin 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1681628d2653SJohn Baldwin 				PROC_UNLOCK(p);
1682628d2653SJohn Baldwin 				continue;
1683628d2653SJohn Baldwin 			}
16841a88a252SMaxim Sobolev 			if (p_cansignal(td, p, sig) == 0) {
1685d93f860cSPoul-Henning Kamp 				nfound++;
168633a9ed9dSJohn Baldwin 				if (sig)
16872c42a146SMarcel Moolenaar 					psignal(p, sig);
1688628d2653SJohn Baldwin 			}
168933a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1690d93f860cSPoul-Henning Kamp 		}
1691f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
1692d93f860cSPoul-Henning Kamp 	}
1693d93f860cSPoul-Henning Kamp 	return (nfound ? 0 : ESRCH);
1694d93f860cSPoul-Henning Kamp }
1695d93f860cSPoul-Henning Kamp 
1696d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1697df8bae1dSRodney W. Grimes struct kill_args {
1698df8bae1dSRodney W. Grimes 	int	pid;
1699df8bae1dSRodney W. Grimes 	int	signum;
1700df8bae1dSRodney W. Grimes };
1701d2d3e875SBruce Evans #endif
1702fb99ab88SMatthew Dillon /*
1703fb99ab88SMatthew Dillon  * MPSAFE
1704fb99ab88SMatthew Dillon  */
1705df8bae1dSRodney W. Grimes /* ARGSUSED */
170626f9a767SRodney W. Grimes int
1707b40ce416SJulian Elischer kill(td, uap)
1708b40ce416SJulian Elischer 	register struct thread *td;
1709df8bae1dSRodney W. Grimes 	register struct kill_args *uap;
1710df8bae1dSRodney W. Grimes {
1711df8bae1dSRodney W. Grimes 	register struct proc *p;
171290af4afaSJohn Baldwin 	int error;
1713df8bae1dSRodney W. Grimes 
1714bfd7575aSWayne Salamon 	AUDIT_ARG(signum, uap->signum);
17156c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1716df8bae1dSRodney W. Grimes 		return (EINVAL);
1717fb99ab88SMatthew Dillon 
1718df8bae1dSRodney W. Grimes 	if (uap->pid > 0) {
1719df8bae1dSRodney W. Grimes 		/* kill single process */
17200b011ea3SPawel Jakub Dawidek 		if ((p = pfind(uap->pid)) == NULL) {
172124b2151fSPawel Jakub Dawidek 			if ((p = zpfind(uap->pid)) == NULL)
172290af4afaSJohn Baldwin 				return (ESRCH);
17230b011ea3SPawel Jakub Dawidek 		}
1724bfd7575aSWayne Salamon 		AUDIT_ARG(process, p);
17251a88a252SMaxim Sobolev 		error = p_cansignal(td, p, uap->signum);
172690af4afaSJohn Baldwin 		if (error == 0 && uap->signum)
1727df8bae1dSRodney W. Grimes 			psignal(p, uap->signum);
1728628d2653SJohn Baldwin 		PROC_UNLOCK(p);
172990af4afaSJohn Baldwin 		return (error);
1730df8bae1dSRodney W. Grimes 	}
1731bfd7575aSWayne Salamon 	AUDIT_ARG(pid, uap->pid);
1732df8bae1dSRodney W. Grimes 	switch (uap->pid) {
1733df8bae1dSRodney W. Grimes 	case -1:		/* broadcast signal */
17341a88a252SMaxim Sobolev 		return (killpg1(td, uap->signum, 0, 1));
1735df8bae1dSRodney W. Grimes 	case 0:			/* signal own process group */
17361a88a252SMaxim Sobolev 		return (killpg1(td, uap->signum, 0, 0));
1737df8bae1dSRodney W. Grimes 	default:		/* negative explicit process group */
17381a88a252SMaxim Sobolev 		return (killpg1(td, uap->signum, -uap->pid, 0));
1739df8bae1dSRodney W. Grimes 	}
174090af4afaSJohn Baldwin 	/* NOTREACHED */
1741df8bae1dSRodney W. Grimes }
1742df8bae1dSRodney W. Grimes 
17431930e303SPoul-Henning Kamp #if defined(COMPAT_43)
1744d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1745df8bae1dSRodney W. Grimes struct okillpg_args {
1746df8bae1dSRodney W. Grimes 	int	pgid;
1747df8bae1dSRodney W. Grimes 	int	signum;
1748df8bae1dSRodney W. Grimes };
1749d2d3e875SBruce Evans #endif
1750fb99ab88SMatthew Dillon /*
1751fb99ab88SMatthew Dillon  * MPSAFE
1752fb99ab88SMatthew Dillon  */
1753df8bae1dSRodney W. Grimes /* ARGSUSED */
175426f9a767SRodney W. Grimes int
1755b40ce416SJulian Elischer okillpg(td, uap)
1756b40ce416SJulian Elischer 	struct thread *td;
1757df8bae1dSRodney W. Grimes 	register struct okillpg_args *uap;
1758df8bae1dSRodney W. Grimes {
1759df8bae1dSRodney W. Grimes 
1760bfd7575aSWayne Salamon 	AUDIT_ARG(signum, uap->signum);
1761bfd7575aSWayne Salamon 	AUDIT_ARG(pid, uap->pgid);
17626c1534a7SPeter Wemm 	if ((u_int)uap->signum > _SIG_MAXSIG)
1763df8bae1dSRodney W. Grimes 		return (EINVAL);
17649104847fSDavid Xu 
17651a88a252SMaxim Sobolev 	return (killpg1(td, uap->signum, uap->pgid, 0));
1766df8bae1dSRodney W. Grimes }
17671930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
1768df8bae1dSRodney W. Grimes 
17699104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_
17709104847fSDavid Xu struct sigqueue_args {
17719104847fSDavid Xu 	pid_t pid;
17729104847fSDavid Xu 	int signum;
17739104847fSDavid Xu 	/* union sigval */ void *value;
17749104847fSDavid Xu };
17759104847fSDavid Xu #endif
17769104847fSDavid Xu 
17779104847fSDavid Xu int
17789104847fSDavid Xu sigqueue(struct thread *td, struct sigqueue_args *uap)
17799104847fSDavid Xu {
17809104847fSDavid Xu 	ksiginfo_t ksi;
17819104847fSDavid Xu 	struct proc *p;
17829104847fSDavid Xu 	int error;
17839104847fSDavid Xu 
17849104847fSDavid Xu 	if ((u_int)uap->signum > _SIG_MAXSIG)
17859104847fSDavid Xu 		return (EINVAL);
17869104847fSDavid Xu 
17879104847fSDavid Xu 	/*
17889104847fSDavid Xu 	 * Specification says sigqueue can only send signal to
17899104847fSDavid Xu 	 * single process.
17909104847fSDavid Xu 	 */
17919104847fSDavid Xu 	if (uap->pid <= 0)
17929104847fSDavid Xu 		return (EINVAL);
17939104847fSDavid Xu 
17949104847fSDavid Xu 	if ((p = pfind(uap->pid)) == NULL) {
17959104847fSDavid Xu 		if ((p = zpfind(uap->pid)) == NULL)
17969104847fSDavid Xu 			return (ESRCH);
17979104847fSDavid Xu 	}
17989104847fSDavid Xu 	error = p_cansignal(td, p, uap->signum);
17999104847fSDavid Xu 	if (error == 0 && uap->signum != 0) {
18009104847fSDavid Xu 		ksiginfo_init(&ksi);
18019104847fSDavid Xu 		ksi.ksi_signo = uap->signum;
18029104847fSDavid Xu 		ksi.ksi_code = SI_QUEUE;
18039104847fSDavid Xu 		ksi.ksi_pid = td->td_proc->p_pid;
18049104847fSDavid Xu 		ksi.ksi_uid = td->td_ucred->cr_ruid;
18058f0371f1SDavid Xu 		ksi.ksi_value.sival_ptr = uap->value;
18066d7b314bSDavid Xu 		error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
18079104847fSDavid Xu 	}
18089104847fSDavid Xu 	PROC_UNLOCK(p);
18099104847fSDavid Xu 	return (error);
18109104847fSDavid Xu }
18119104847fSDavid Xu 
1812df8bae1dSRodney W. Grimes /*
1813df8bae1dSRodney W. Grimes  * Send a signal to a process group.
1814df8bae1dSRodney W. Grimes  */
1815df8bae1dSRodney W. Grimes void
18162c42a146SMarcel Moolenaar gsignal(pgid, sig)
18172c42a146SMarcel Moolenaar 	int pgid, sig;
1818df8bae1dSRodney W. Grimes {
1819df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
1820df8bae1dSRodney W. Grimes 
1821f591779bSSeigo Tanimura 	if (pgid != 0) {
1822ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
1823f591779bSSeigo Tanimura 		pgrp = pgfind(pgid);
1824ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
1825f591779bSSeigo Tanimura 		if (pgrp != NULL) {
18262c42a146SMarcel Moolenaar 			pgsignal(pgrp, sig, 0);
1827f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
1828f591779bSSeigo Tanimura 		}
1829f591779bSSeigo Tanimura 	}
1830df8bae1dSRodney W. Grimes }
1831df8bae1dSRodney W. Grimes 
1832df8bae1dSRodney W. Grimes /*
1833df8bae1dSRodney W. Grimes  * Send a signal to a process group.  If checktty is 1,
1834df8bae1dSRodney W. Grimes  * limit to members which have a controlling terminal.
1835df8bae1dSRodney W. Grimes  */
1836df8bae1dSRodney W. Grimes void
18372c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty)
1838df8bae1dSRodney W. Grimes 	struct pgrp *pgrp;
18392c42a146SMarcel Moolenaar 	int sig, checkctty;
1840df8bae1dSRodney W. Grimes {
1841df8bae1dSRodney W. Grimes 	register struct proc *p;
1842df8bae1dSRodney W. Grimes 
1843628d2653SJohn Baldwin 	if (pgrp) {
1844f591779bSSeigo Tanimura 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1845628d2653SJohn Baldwin 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1846628d2653SJohn Baldwin 			PROC_LOCK(p);
1847df8bae1dSRodney W. Grimes 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
18482c42a146SMarcel Moolenaar 				psignal(p, sig);
1849628d2653SJohn Baldwin 			PROC_UNLOCK(p);
1850628d2653SJohn Baldwin 		}
1851628d2653SJohn Baldwin 	}
1852df8bae1dSRodney W. Grimes }
1853df8bae1dSRodney W. Grimes 
1854df8bae1dSRodney W. Grimes /*
18551bf4700bSJeff Roberson  * Send a signal caused by a trap to the current thread.
1856df8bae1dSRodney W. Grimes  * If it will be caught immediately, deliver it with correct code.
1857df8bae1dSRodney W. Grimes  * Otherwise, post it normally.
1858356861dbSMatthew Dillon  *
1859356861dbSMatthew Dillon  * MPSAFE
1860df8bae1dSRodney W. Grimes  */
1861df8bae1dSRodney W. Grimes void
18629104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi)
1863df8bae1dSRodney W. Grimes {
18641bf4700bSJeff Roberson 	struct sigacts *ps;
18651bf4700bSJeff Roberson 	struct proc *p;
18669dde3bc9SDavid Xu 	int error;
18679104847fSDavid Xu 	int sig;
18689104847fSDavid Xu 	int code;
18691bf4700bSJeff Roberson 
18701bf4700bSJeff Roberson 	p = td->td_proc;
18719104847fSDavid Xu 	sig = ksi->ksi_signo;
18729104847fSDavid Xu 	code = ksi->ksi_code;
18739104847fSDavid Xu 	KASSERT(_SIG_VALID(sig), ("invalid signal"));
18749104847fSDavid Xu 
1875aa0aa7a1STim J. Robbins 	if (td->td_pflags & TDP_SA) {
18767eeaaf9bSDavid Xu 		if (td->td_mailbox == NULL)
18775995adc2SJulian Elischer 			thread_user_enter(td);
1878628d2653SJohn Baldwin 		PROC_LOCK(p);
18799dde3bc9SDavid Xu 		SIGDELSET(td->td_sigmask, sig);
18809dde3bc9SDavid Xu 		mtx_lock_spin(&sched_lock);
18819dde3bc9SDavid Xu 		/*
18829dde3bc9SDavid Xu 		 * Force scheduling an upcall, so UTS has chance to
18839dde3bc9SDavid Xu 		 * process the signal before thread runs again in
18849dde3bc9SDavid Xu 		 * userland.
18859dde3bc9SDavid Xu 		 */
18869dde3bc9SDavid Xu 		if (td->td_upcall)
18879dde3bc9SDavid Xu 			td->td_upcall->ku_flags |= KUF_DOUPCALL;
18889dde3bc9SDavid Xu 		mtx_unlock_spin(&sched_lock);
1889432b45deSDavid Xu 	} else {
18909dde3bc9SDavid Xu 		PROC_LOCK(p);
18919dde3bc9SDavid Xu 	}
1892ef3dab76STim J. Robbins 	ps = p->p_sigacts;
189390af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
189490af4afaSJohn Baldwin 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
18954093529dSJeff Roberson 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1896df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
1897df8bae1dSRodney W. Grimes #ifdef KTRACE
1898374a15aaSJohn Baldwin 		if (KTRPOINT(curthread, KTR_PSIG))
1899374a15aaSJohn Baldwin 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
19004093529dSJeff Roberson 			    &td->td_sigmask, code);
1901df8bae1dSRodney W. Grimes #endif
1902aa0aa7a1STim J. Robbins 		if (!(td->td_pflags & TDP_SA))
19039104847fSDavid Xu 			(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
19049104847fSDavid Xu 				ksi, &td->td_sigmask);
1905cbf4e354SDavid Xu 		else if (td->td_mailbox == NULL) {
1906cbf4e354SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1907cbf4e354SDavid Xu 			/* UTS caused a sync signal */
1908cbf4e354SDavid Xu 			p->p_code = code;	/* XXX for core dump/debugger */
1909cbf4e354SDavid Xu 			p->p_sig = sig;		/* XXX to verify code */
1910cbf4e354SDavid Xu 			sigexit(td, sig);
1911cbf4e354SDavid Xu 		} else {
19129dde3bc9SDavid Xu 			mtx_unlock(&ps->ps_mtx);
1913cbf4e354SDavid Xu 			SIGADDSET(td->td_sigmask, sig);
19149dde3bc9SDavid Xu 			PROC_UNLOCK(p);
19159104847fSDavid Xu 			error = copyout(&ksi->ksi_info, &td->td_mailbox->tm_syncsig,
19169104847fSDavid Xu 			    sizeof(siginfo_t));
19179dde3bc9SDavid Xu 			PROC_LOCK(p);
1918432b45deSDavid Xu 			/* UTS memory corrupted */
19199dde3bc9SDavid Xu 			if (error)
1920cbf4e354SDavid Xu 				sigexit(td, SIGSEGV);
19219dde3bc9SDavid Xu 			mtx_lock(&ps->ps_mtx);
19229dde3bc9SDavid Xu 		}
19234093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
19242c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
19254093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
19262c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1927289ccde0SPeter Wemm 			/*
19288f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
1929289ccde0SPeter Wemm 			 */
193090af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
19312c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
19322c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
193390af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
19342c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1935dedc04feSPeter Wemm 		}
193690af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
19376f841fb7SMarcel Moolenaar 	} else {
1938f71a882fSDavid Xu 		/*
1939f71a882fSDavid Xu 		 * Avoid a possible infinite loop if the thread
1940f71a882fSDavid Xu 		 * masking the signal or process is ignoring the
1941f71a882fSDavid Xu 		 * signal.
1942f71a882fSDavid Xu 		 */
1943f71a882fSDavid Xu 		if (kern_forcesigexit &&
1944f71a882fSDavid Xu 		    (SIGISMEMBER(td->td_sigmask, sig) ||
1945f71a882fSDavid Xu 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1946f71a882fSDavid Xu 			SIGDELSET(td->td_sigmask, sig);
1947f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigcatch, sig);
1948f71a882fSDavid Xu 			SIGDELSET(ps->ps_sigignore, sig);
1949f71a882fSDavid Xu 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1950f71a882fSDavid Xu 		}
195190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
19526626c604SJulian Elischer 		p->p_code = code;	/* XXX for core dump/debugger */
19532c42a146SMarcel Moolenaar 		p->p_sig = sig;		/* XXX to verify code */
19546d7b314bSDavid Xu 		tdsignal(p, td, sig, ksi);
1955df8bae1dSRodney W. Grimes 	}
1956628d2653SJohn Baldwin 	PROC_UNLOCK(p);
1957df8bae1dSRodney W. Grimes }
1958df8bae1dSRodney W. Grimes 
19594093529dSJeff Roberson static struct thread *
19604093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop)
19614093529dSJeff Roberson {
19623074d1b4SDavid Xu 	struct thread *td, *signal_td;
19634093529dSJeff Roberson 
19644093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
19654093529dSJeff Roberson 
19664093529dSJeff Roberson 	/*
1967627451c1SDavid Xu 	 * Check if current thread can handle the signal without
1968627451c1SDavid Xu 	 * switching conetxt to another thread.
19694093529dSJeff Roberson 	 */
1970627451c1SDavid Xu 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1971627451c1SDavid Xu 		return (curthread);
19723074d1b4SDavid Xu 	signal_td = NULL;
1973a9a48d68SDavid Xu 	mtx_lock_spin(&sched_lock);
19743074d1b4SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
19753074d1b4SDavid Xu 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
19763074d1b4SDavid Xu 			signal_td = td;
1977627451c1SDavid Xu 			break;
19783074d1b4SDavid Xu 		}
19793074d1b4SDavid Xu 	}
19803074d1b4SDavid Xu 	if (signal_td == NULL)
19813074d1b4SDavid Xu 		signal_td = FIRST_THREAD_IN_PROC(p);
1982a9a48d68SDavid Xu 	mtx_unlock_spin(&sched_lock);
19833074d1b4SDavid Xu 	return (signal_td);
19844093529dSJeff Roberson }
19854093529dSJeff Roberson 
1986df8bae1dSRodney W. Grimes /*
1987df8bae1dSRodney W. Grimes  * Send the signal to the process.  If the signal has an action, the action
1988df8bae1dSRodney W. Grimes  * is usually performed by the target process rather than the caller; we add
1989df8bae1dSRodney W. Grimes  * the signal to the set of pending signals for the process.
1990df8bae1dSRodney W. Grimes  *
1991df8bae1dSRodney W. Grimes  * Exceptions:
1992df8bae1dSRodney W. Grimes  *   o When a stop signal is sent to a sleeping process that takes the
1993df8bae1dSRodney W. Grimes  *     default action, the process is stopped without awakening it.
1994df8bae1dSRodney W. Grimes  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1995df8bae1dSRodney W. Grimes  *     regardless of the signal action (eg, blocked or ignored).
1996df8bae1dSRodney W. Grimes  *
1997df8bae1dSRodney W. Grimes  * Other ignored signals are discarded immediately.
199890af4afaSJohn Baldwin  *
199990af4afaSJohn Baldwin  * MPSAFE
2000df8bae1dSRodney W. Grimes  */
2001df8bae1dSRodney W. Grimes void
20024093529dSJeff Roberson psignal(struct proc *p, int sig)
2003df8bae1dSRodney W. Grimes {
20046d7b314bSDavid Xu 	(void) tdsignal(p, NULL, sig, NULL);
20059104847fSDavid Xu }
20069104847fSDavid Xu 
20079104847fSDavid Xu int
20086d7b314bSDavid Xu psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
20099104847fSDavid Xu {
20106d7b314bSDavid Xu 	struct thread *td = NULL;
201141b3077aSJacques Vidrine 
20124093529dSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
20136d7b314bSDavid Xu 
20146d7b314bSDavid Xu 	KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));
20154093529dSJeff Roberson 
20164093529dSJeff Roberson 	/*
20176d7b314bSDavid Xu 	 * ksi_code and other fields should be set before
20186d7b314bSDavid Xu 	 * calling this function.
20194093529dSJeff Roberson 	 */
20206d7b314bSDavid Xu 	ksi->ksi_signo = sigev->sigev_signo;
20216d7b314bSDavid Xu 	ksi->ksi_value = sigev->sigev_value;
20226d7b314bSDavid Xu 	if (sigev->sigev_notify == SIGEV_THREAD_ID) {
20236d7b314bSDavid Xu 		td = thread_find(p, sigev->sigev_notify_thread_id);
20246d7b314bSDavid Xu 		if (td == NULL)
20256d7b314bSDavid Xu 			return (ESRCH);
20266d7b314bSDavid Xu 	}
20276d7b314bSDavid Xu 	return (tdsignal(p, td, ksi->ksi_signo, ksi));
20284093529dSJeff Roberson }
20294093529dSJeff Roberson 
203090af4afaSJohn Baldwin /*
203190af4afaSJohn Baldwin  * MPSAFE
203290af4afaSJohn Baldwin  */
20339104847fSDavid Xu int
20346d7b314bSDavid Xu tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
20354093529dSJeff Roberson {
20369dde3bc9SDavid Xu 	sigset_t saved;
20379104847fSDavid Xu 	int ret;
20389dde3bc9SDavid Xu 
20399dde3bc9SDavid Xu 	if (p->p_flag & P_SA)
20409104847fSDavid Xu 		saved = p->p_sigqueue.sq_signals;
20416d7b314bSDavid Xu 	ret = do_tdsignal(p, td, sig, ksi);
20429dde3bc9SDavid Xu 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
20439104847fSDavid Xu 		if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
20449dde3bc9SDavid Xu 			/* pending set changed */
20459dde3bc9SDavid Xu 			p->p_flag |= P_SIGEVENT;
20469dde3bc9SDavid Xu 			wakeup(&p->p_siglist);
20479dde3bc9SDavid Xu 		}
20489dde3bc9SDavid Xu 	}
20499104847fSDavid Xu 	return (ret);
20509dde3bc9SDavid Xu }
20519dde3bc9SDavid Xu 
20529104847fSDavid Xu static int
20536d7b314bSDavid Xu do_tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
20549dde3bc9SDavid Xu {
20559104847fSDavid Xu 	sig_t action;
20569104847fSDavid Xu 	sigqueue_t *sigqueue;
20579104847fSDavid Xu 	int prop;
205890af4afaSJohn Baldwin 	struct sigacts *ps;
20599104847fSDavid Xu 	int ret = 0;
2060df8bae1dSRodney W. Grimes 
20616d7b314bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
20626d7b314bSDavid Xu 
206341b3077aSJacques Vidrine 	if (!_SIG_VALID(sig))
206441b3077aSJacques Vidrine 		panic("do_tdsignal(): invalid signal");
20654093529dSJeff Roberson 
20666d7b314bSDavid Xu 	KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("do_tdsignal: ksi on queue"));
20676d7b314bSDavid Xu 
20686d7b314bSDavid Xu 	/*
20696d7b314bSDavid Xu 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
20706d7b314bSDavid Xu 	 */
20716d7b314bSDavid Xu 	if (p->p_state == PRS_ZOMBIE) {
20726d7b314bSDavid Xu 		if (ksi && (ksi->ksi_flags & KSI_INS))
20736d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
20746d7b314bSDavid Xu 		return (ret);
20756d7b314bSDavid Xu 	}
20766d7b314bSDavid Xu 
207790af4afaSJohn Baldwin 	ps = p->p_sigacts;
2078ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
20792c42a146SMarcel Moolenaar 	prop = sigprop(sig);
20804093529dSJeff Roberson 
20814093529dSJeff Roberson 	/*
208214b5ae1aSMike Makonnen 	 * If the signal is blocked and not destined for this thread, then
208314b5ae1aSMike Makonnen 	 * assign it to the process so that we can find it later in the first
208414b5ae1aSMike Makonnen 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
20854093529dSJeff Roberson 	 */
20866d7b314bSDavid Xu 	if (td == NULL) {
20876d7b314bSDavid Xu 		td = sigtd(p, sig, prop);
20886d7b314bSDavid Xu 		if (SIGISMEMBER(td->td_sigmask, sig))
20896d7b314bSDavid Xu 			sigqueue = &p->p_sigqueue;
20906d7b314bSDavid Xu 		else
20919104847fSDavid Xu 			sigqueue = &td->td_sigqueue;
20923074d1b4SDavid Xu 	} else {
20936d7b314bSDavid Xu 		KASSERT(td->td_proc == p, ("invalid thread"));
20949104847fSDavid Xu 		sigqueue = &td->td_sigqueue;
20953074d1b4SDavid Xu 	}
20964093529dSJeff Roberson 
2097df8bae1dSRodney W. Grimes 	/*
2098df8bae1dSRodney W. Grimes 	 * If the signal is being ignored,
20999104847fSDavid Xu 	 * or process is exiting or thread is exiting,
2100df8bae1dSRodney W. Grimes 	 * then we forget about it immediately.
210190af4afaSJohn Baldwin 	 * (Note: we don't set SIGCONT in ps_sigignore,
2102df8bae1dSRodney W. Grimes 	 * and if it is set to SIG_IGN,
2103df8bae1dSRodney W. Grimes 	 * action will be SIG_DFL here.)
2104df8bae1dSRodney W. Grimes 	 */
210590af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
210690af4afaSJohn Baldwin 	if (SIGISMEMBER(ps->ps_sigignore, sig) ||
210790af4afaSJohn Baldwin 	    (p->p_flag & P_WEXIT)) {
210890af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
21096d7b314bSDavid Xu 		if (ksi && (ksi->ksi_flags & KSI_INS))
21106d7b314bSDavid Xu 			ksiginfo_tryfree(ksi);
21119104847fSDavid Xu 		return (ret);
211290af4afaSJohn Baldwin 	}
21136675b36eSDavid Xu 	if (SIGISMEMBER(td->td_sigmask, sig))
2114df8bae1dSRodney W. Grimes 		action = SIG_HOLD;
211590af4afaSJohn Baldwin 	else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2116df8bae1dSRodney W. Grimes 		action = SIG_CATCH;
2117df8bae1dSRodney W. Grimes 	else
2118df8bae1dSRodney W. Grimes 		action = SIG_DFL;
211990af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
2120df8bae1dSRodney W. Grimes 
21219104847fSDavid Xu 	if (prop & SA_CONT)
21229104847fSDavid Xu 		sigqueue_delete_stopmask_proc(p);
21239104847fSDavid Xu 	else if (prop & SA_STOP) {
2124df8bae1dSRodney W. Grimes 		/*
2125df8bae1dSRodney W. Grimes 		 * If sending a tty stop signal to a member of an orphaned
2126df8bae1dSRodney W. Grimes 		 * process group, discard the signal here if the action
2127df8bae1dSRodney W. Grimes 		 * is default; don't stop the process below if sleeping,
2128df8bae1dSRodney W. Grimes 		 * and don't clear any pending SIGCONT.
2129df8bae1dSRodney W. Grimes 		 */
2130e602ba25SJulian Elischer 		if ((prop & SA_TTYSTOP) &&
2131e602ba25SJulian Elischer 		    (p->p_pgrp->pg_jobc == 0) &&
21326d7b314bSDavid Xu 		    (action == SIG_DFL)) {
21336d7b314bSDavid Xu 			if (ksi && (ksi->ksi_flags & KSI_INS))
21346d7b314bSDavid Xu 				ksiginfo_tryfree(ksi);
21359104847fSDavid Xu 			return (ret);
21366d7b314bSDavid Xu 		}
21379104847fSDavid Xu 		sigqueue_delete_proc(p, SIGCONT);
2138ebceaf6dSDavid Xu 		if (p->p_flag & P_CONTINUED) {
21396933e3c1SJulian Elischer 			p->p_flag &= ~P_CONTINUED;
2140ebceaf6dSDavid Xu 			PROC_LOCK(p->p_pptr);
2141ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
2142ebceaf6dSDavid Xu 			PROC_UNLOCK(p->p_pptr);
2143ebceaf6dSDavid Xu 		}
2144df8bae1dSRodney W. Grimes 	}
21453074d1b4SDavid Xu 
21469104847fSDavid Xu 	ret = sigqueue_add(sigqueue, sig, ksi);
21479104847fSDavid Xu 	if (ret != 0)
21489104847fSDavid Xu 		return (ret);
21496d7b314bSDavid Xu 	signotify(td);
21505312b1c7SDavid Xu 	/*
21515312b1c7SDavid Xu 	 * Defer further processing for signals which are held,
21525312b1c7SDavid Xu 	 * except that stopped processes must be continued by SIGCONT.
21535312b1c7SDavid Xu 	 */
21545312b1c7SDavid Xu 	if (action == SIG_HOLD &&
21555312b1c7SDavid Xu 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
21569104847fSDavid Xu 		return (ret);
2157df8bae1dSRodney W. Grimes 	/*
215890d75f78SAlfred Perlstein 	 * SIGKILL: Remove procfs STOPEVENTs.
215990d75f78SAlfred Perlstein 	 */
216090d75f78SAlfred Perlstein 	if (sig == SIGKILL) {
216190d75f78SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCBIC */
216290d75f78SAlfred Perlstein 		p->p_stops = 0;
216390d75f78SAlfred Perlstein 		/* from procfs_ioctl.c: PIOCCONT */
216490d75f78SAlfred Perlstein 		p->p_step = 0;
216590d75f78SAlfred Perlstein 		wakeup(&p->p_step);
216690d75f78SAlfred Perlstein 	}
216790d75f78SAlfred Perlstein 	/*
2168e602ba25SJulian Elischer 	 * Some signals have a process-wide effect and a per-thread
2169e602ba25SJulian Elischer 	 * component.  Most processing occurs when the process next
2170e602ba25SJulian Elischer 	 * tries to cross the user boundary, however there are some
2171e602ba25SJulian Elischer 	 * times when processing needs to be done immediatly, such as
2172e602ba25SJulian Elischer 	 * waking up threads so that they can cross the user boundary.
2173e602ba25SJulian Elischer 	 * We try do the per-process part here.
2174df8bae1dSRodney W. Grimes 	 */
2175e602ba25SJulian Elischer 	if (P_SHOULDSTOP(p)) {
2176e602ba25SJulian Elischer 		/*
2177e602ba25SJulian Elischer 		 * The process is in stopped mode. All the threads should be
2178e602ba25SJulian Elischer 		 * either winding down or already on the suspended queue.
2179e602ba25SJulian Elischer 		 */
2180e602ba25SJulian Elischer 		if (p->p_flag & P_TRACED) {
2181e602ba25SJulian Elischer 			/*
2182e602ba25SJulian Elischer 			 * The traced process is already stopped,
2183e602ba25SJulian Elischer 			 * so no further action is necessary.
2184e602ba25SJulian Elischer 			 * No signal can restart us.
2185e602ba25SJulian Elischer 			 */
2186e602ba25SJulian Elischer 			goto out;
21871c32c37cSJohn Baldwin 		}
2188b40ce416SJulian Elischer 
2189e602ba25SJulian Elischer 		if (sig == SIGKILL) {
2190df8bae1dSRodney W. Grimes 			/*
2191e602ba25SJulian Elischer 			 * SIGKILL sets process running.
2192e602ba25SJulian Elischer 			 * It will die elsewhere.
2193e602ba25SJulian Elischer 			 * All threads must be restarted.
2194df8bae1dSRodney W. Grimes 			 */
2195482d099cSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
2196e602ba25SJulian Elischer 			goto runfast;
2197e602ba25SJulian Elischer 		}
2198e602ba25SJulian Elischer 
2199e602ba25SJulian Elischer 		if (prop & SA_CONT) {
2200e602ba25SJulian Elischer 			/*
2201e602ba25SJulian Elischer 			 * If SIGCONT is default (or ignored), we continue the
22029104847fSDavid Xu 			 * process but don't leave the signal in sigqueue as
22031d9c5696SJuli Mallett 			 * it has no further action.  If SIGCONT is held, we
2204e602ba25SJulian Elischer 			 * continue the process and leave the signal in
22059104847fSDavid Xu 			 * sigqueue.  If the process catches SIGCONT, let it
2206e602ba25SJulian Elischer 			 * handle the signal itself.  If it isn't waiting on
2207e602ba25SJulian Elischer 			 * an event, it goes back to run state.
2208e602ba25SJulian Elischer 			 * Otherwise, process goes back to sleep state.
2209e602ba25SJulian Elischer 			 */
22101279572aSDavid Xu 			p->p_flag &= ~P_STOPPED_SIG;
2211ebceaf6dSDavid Xu 			if (p->p_numthreads == p->p_suspcount) {
22126933e3c1SJulian Elischer 				p->p_flag |= P_CONTINUED;
22137f96995eSDavid Xu 				p->p_xstat = SIGCONT;
22147f96995eSDavid Xu 				PROC_LOCK(p->p_pptr);
2215ebceaf6dSDavid Xu 				childproc_continued(p);
22167f96995eSDavid Xu 				PROC_UNLOCK(p->p_pptr);
2217ebceaf6dSDavid Xu 			}
2218e602ba25SJulian Elischer 			if (action == SIG_DFL) {
22199104847fSDavid Xu 				sigqueue_delete(sigqueue, sig);
2220e602ba25SJulian Elischer 			} else if (action == SIG_CATCH) {
2221e602ba25SJulian Elischer 				/*
2222e602ba25SJulian Elischer 				 * The process wants to catch it so it needs
2223e602ba25SJulian Elischer 				 * to run at least one thread, but which one?
2224e602ba25SJulian Elischer 				 * It would seem that the answer would be to
2225e602ba25SJulian Elischer 				 * run an upcall in the next KSE to run, and
2226e602ba25SJulian Elischer 				 * deliver the signal that way. In a NON KSE
2227e602ba25SJulian Elischer 				 * process, we need to make sure that the
2228e602ba25SJulian Elischer 				 * single thread is runnable asap.
2229e602ba25SJulian Elischer 				 * XXXKSE for now however, make them all run.
2230e602ba25SJulian Elischer 				 */
2231e602ba25SJulian Elischer 				goto runfast;
2232e602ba25SJulian Elischer 			}
2233e602ba25SJulian Elischer 			/*
2234e602ba25SJulian Elischer 			 * The signal is not ignored or caught.
2235e602ba25SJulian Elischer 			 */
2236e602ba25SJulian Elischer 			mtx_lock_spin(&sched_lock);
223704774f23SJulian Elischer 			thread_unsuspend(p);
2238e602ba25SJulian Elischer 			mtx_unlock_spin(&sched_lock);
2239e602ba25SJulian Elischer 			goto out;
2240e602ba25SJulian Elischer 		}
2241e602ba25SJulian Elischer 
2242e602ba25SJulian Elischer 		if (prop & SA_STOP) {
2243e602ba25SJulian Elischer 			/*
2244e602ba25SJulian Elischer 			 * Already stopped, don't need to stop again
2245e602ba25SJulian Elischer 			 * (If we did the shell could get confused).
224604774f23SJulian Elischer 			 * Just make sure the signal STOP bit set.
2247e602ba25SJulian Elischer 			 */
22481279572aSDavid Xu 			p->p_flag |= P_STOPPED_SIG;
22499104847fSDavid Xu 			sigqueue_delete(sigqueue, sig);
2250e602ba25SJulian Elischer 			goto out;
2251e602ba25SJulian Elischer 		}
2252e602ba25SJulian Elischer 
2253e602ba25SJulian Elischer 		/*
2254e602ba25SJulian Elischer 		 * All other kinds of signals:
2255e602ba25SJulian Elischer 		 * If a thread is sleeping interruptibly, simulate a
2256e602ba25SJulian Elischer 		 * wakeup so that when it is continued it will be made
2257e602ba25SJulian Elischer 		 * runnable and can look at the signal.  However, don't make
225804774f23SJulian Elischer 		 * the PROCESS runnable, leave it stopped.
2259e602ba25SJulian Elischer 		 * It may run a bit until it hits a thread_suspend_check().
2260e602ba25SJulian Elischer 		 */
2261e602ba25SJulian Elischer 		mtx_lock_spin(&sched_lock);
226244f3b092SJohn Baldwin 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
226344f3b092SJohn Baldwin 			sleepq_abort(td);
2264e602ba25SJulian Elischer 		mtx_unlock_spin(&sched_lock);
2265df8bae1dSRodney W. Grimes 		goto out;
2266df8bae1dSRodney W. Grimes 		/*
22679a6a4cb5SPeter Wemm 		 * Mutexes are short lived. Threads waiting on them will
22689a6a4cb5SPeter Wemm 		 * hit thread_suspend_check() soon.
2269df8bae1dSRodney W. Grimes 		 */
2270e602ba25SJulian Elischer 	} else if (p->p_state == PRS_NORMAL) {
2271ec8297bdSDavid Xu 		if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2272721e5910SJulian Elischer 			mtx_lock_spin(&sched_lock);
22734093529dSJeff Roberson 			tdsigwakeup(td, sig, action);
2274721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
2275df8bae1dSRodney W. Grimes 			goto out;
227604774f23SJulian Elischer 		}
2277ec8297bdSDavid Xu 
2278ec8297bdSDavid Xu 		MPASS(action == SIG_DFL);
2279ec8297bdSDavid Xu 
228035c32a76SDavid Xu 		if (prop & SA_STOP) {
228135c32a76SDavid Xu 			if (p->p_flag & P_PPWAIT)
228235c32a76SDavid Xu 				goto out;
2283e574e444SDavid Xu 			p->p_flag |= P_STOPPED_SIG;
2284e574e444SDavid Xu 			p->p_xstat = sig;
228535c32a76SDavid Xu 			mtx_lock_spin(&sched_lock);
2286d8267df7SDavid Xu 			sig_suspend_threads(td, p, 1);
22874093529dSJeff Roberson 			if (p->p_numthreads == p->p_suspcount) {
2288ebceaf6dSDavid Xu 				/*
2289ebceaf6dSDavid Xu 				 * only thread sending signal to another
2290ebceaf6dSDavid Xu 				 * process can reach here, if thread is sending
2291ebceaf6dSDavid Xu 				 * signal to its process, because thread does
2292ebceaf6dSDavid Xu 				 * not suspend itself here, p_numthreads
2293ebceaf6dSDavid Xu 				 * should never be equal to p_suspcount.
2294ebceaf6dSDavid Xu 				 */
2295ebceaf6dSDavid Xu 				thread_stopped(p);
22969104847fSDavid Xu 				mtx_unlock_spin(&sched_lock);
22979104847fSDavid Xu 				sigqueue_delete_proc(p, p->p_xstat);
22989104847fSDavid Xu 			} else
229935c32a76SDavid Xu 				mtx_unlock_spin(&sched_lock);
230035c32a76SDavid Xu 			goto out;
230135c32a76SDavid Xu 		}
2302721e5910SJulian Elischer 		else
2303df8bae1dSRodney W. Grimes 			goto runfast;
2304df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2305e602ba25SJulian Elischer 	} else {
2306e602ba25SJulian Elischer 		/* Not in "NORMAL" state. discard the signal. */
23079104847fSDavid Xu 		sigqueue_delete(sigqueue, sig);
2308e602ba25SJulian Elischer 		goto out;
2309e602ba25SJulian Elischer 	}
2310e602ba25SJulian Elischer 
2311b40ce416SJulian Elischer 	/*
2312e602ba25SJulian Elischer 	 * The process is not stopped so we need to apply the signal to all the
2313e602ba25SJulian Elischer 	 * running threads.
2314b40ce416SJulian Elischer 	 */
2315e602ba25SJulian Elischer 
2316e602ba25SJulian Elischer runfast:
2317aa0fa334SJulian Elischer 	mtx_lock_spin(&sched_lock);
23184093529dSJeff Roberson 	tdsigwakeup(td, sig, action);
2319e602ba25SJulian Elischer 	thread_unsuspend(p);
2320e602ba25SJulian Elischer 	mtx_unlock_spin(&sched_lock);
2321e602ba25SJulian Elischer out:
2322e602ba25SJulian Elischer 	/* If we jump here, sched_lock should not be owned. */
2323e602ba25SJulian Elischer 	mtx_assert(&sched_lock, MA_NOTOWNED);
23249104847fSDavid Xu 	return (ret);
2325e602ba25SJulian Elischer }
2326e602ba25SJulian Elischer 
2327e602ba25SJulian Elischer /*
2328e602ba25SJulian Elischer  * The force of a signal has been directed against a single
2329e602ba25SJulian Elischer  * thread.  We need to see what we can do about knocking it
2330e602ba25SJulian Elischer  * out of any sleep it may be in etc.
2331e602ba25SJulian Elischer  */
2332e602ba25SJulian Elischer static void
23334093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action)
2334e602ba25SJulian Elischer {
2335e602ba25SJulian Elischer 	struct proc *p = td->td_proc;
2336e602ba25SJulian Elischer 	register int prop;
2337e602ba25SJulian Elischer 
23388b94a061SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2339aa0fa334SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
2340e602ba25SJulian Elischer 	prop = sigprop(sig);
2341a4c2da15SBruce Evans 
2342e602ba25SJulian Elischer 	/*
2343aa0fa334SJulian Elischer 	 * Bring the priority of a thread up if we want it to get
2344e602ba25SJulian Elischer 	 * killed in this lifetime.
2345e602ba25SJulian Elischer 	 */
2346a4c2da15SBruce Evans 	if (action == SIG_DFL && (prop & SA_KILL)) {
23473ef6ac33SJeff Roberson 		if (p->p_nice > 0)
23483ef6ac33SJeff Roberson 			sched_nice(td->td_proc, 0);
2349a4c2da15SBruce Evans 		if (td->td_priority > PUSER)
2350b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
2351b40ce416SJulian Elischer 	}
2352a4c2da15SBruce Evans 
235380c4433cSJohn Baldwin 	if (TD_ON_SLEEPQ(td)) {
2354e602ba25SJulian Elischer 		/*
2355e602ba25SJulian Elischer 		 * If thread is sleeping uninterruptibly
2356e602ba25SJulian Elischer 		 * we can't interrupt the sleep... the signal will
2357e602ba25SJulian Elischer 		 * be noticed when the process returns through
2358e602ba25SJulian Elischer 		 * trap() or syscall().
2359e602ba25SJulian Elischer 		 */
236044f3b092SJohn Baldwin 		if ((td->td_flags & TDF_SINTR) == 0)
2361aa0fa334SJulian Elischer 			return;
2362e602ba25SJulian Elischer 		/*
2363e602ba25SJulian Elischer 		 * If SIGCONT is default (or ignored) and process is
2364e602ba25SJulian Elischer 		 * asleep, we are finished; the process should not
2365e602ba25SJulian Elischer 		 * be awakened.
2366df8bae1dSRodney W. Grimes 		 */
2367e602ba25SJulian Elischer 		if ((prop & SA_CONT) && action == SIG_DFL) {
23689104847fSDavid Xu 			mtx_unlock_spin(&sched_lock);
23699104847fSDavid Xu 			sigqueue_delete(&p->p_sigqueue, sig);
23704093529dSJeff Roberson 			/*
23714093529dSJeff Roberson 			 * It may be on either list in this state.
23724093529dSJeff Roberson 			 * Remove from both for now.
23734093529dSJeff Roberson 			 */
23749104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
23759104847fSDavid Xu 			mtx_lock_spin(&sched_lock);
2376aa0fa334SJulian Elischer 			return;
2377df8bae1dSRodney W. Grimes 		}
2378df8bae1dSRodney W. Grimes 
2379aa0fa334SJulian Elischer 		/*
2380a4c2da15SBruce Evans 		 * Give low priority threads a better chance to run.
2381aa0fa334SJulian Elischer 		 */
238244f3b092SJohn Baldwin 		if (td->td_priority > PUSER)
2383b3a4fb14SDavid Xu 			sched_prio(td, PUSER);
2384ec8297bdSDavid Xu 
238544f3b092SJohn Baldwin 		sleepq_abort(td);
2386a4c2da15SBruce Evans 	} else {
2387df8bae1dSRodney W. Grimes 		/*
2388a4c2da15SBruce Evans 		 * Other states do nothing with the signal immediately,
2389df8bae1dSRodney W. Grimes 		 * other than kicking ourselves if we are running.
2390df8bae1dSRodney W. Grimes 		 * It will either never be noticed, or noticed very soon.
2391df8bae1dSRodney W. Grimes 		 */
2392a4c2da15SBruce Evans #ifdef SMP
239344f3b092SJohn Baldwin 		if (TD_IS_RUNNING(td) && td != curthread)
2394e602ba25SJulian Elischer 			forward_signal(td);
23953163861cSTor Egge #endif
23966caa8a15SJohn Baldwin 	}
2397a4c2da15SBruce Evans }
2398df8bae1dSRodney W. Grimes 
2399d8267df7SDavid Xu static void
2400d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2401d8267df7SDavid Xu {
2402d8267df7SDavid Xu 	struct thread *td2;
2403d8267df7SDavid Xu 
2404d8267df7SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2405d8267df7SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2406d8267df7SDavid Xu 
2407d8267df7SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td2) {
2408d8267df7SDavid Xu 		if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2409d8267df7SDavid Xu 		    (td2->td_flags & TDF_SINTR) &&
2410d8267df7SDavid Xu 		    !TD_IS_SUSPENDED(td2)) {
2411d8267df7SDavid Xu 			thread_suspend_one(td2);
2412d8267df7SDavid Xu 		} else {
2413d8267df7SDavid Xu 			if (sending || td != td2)
2414d8267df7SDavid Xu 				td2->td_flags |= TDF_ASTPENDING;
2415d8267df7SDavid Xu #ifdef SMP
2416d8267df7SDavid Xu 			if (TD_IS_RUNNING(td2) && td2 != td)
2417d8267df7SDavid Xu 				forward_signal(td2);
2418d8267df7SDavid Xu #endif
2419d8267df7SDavid Xu 		}
2420d8267df7SDavid Xu 	}
2421d8267df7SDavid Xu }
2422d8267df7SDavid Xu 
2423cbf4e354SDavid Xu int
24244cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig)
24254cc9f52fSRobert Drehmel {
24264cc9f52fSRobert Drehmel 	struct proc *p = td->td_proc;
24274cc9f52fSRobert Drehmel 
242830a9f26dSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
24294cc9f52fSRobert Drehmel 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
24304cc9f52fSRobert Drehmel 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
24314cc9f52fSRobert Drehmel 
24324cc9f52fSRobert Drehmel 	mtx_lock_spin(&sched_lock);
2433cbf4e354SDavid Xu 	td->td_flags |= TDF_XSIG;
2434cbf4e354SDavid Xu 	mtx_unlock_spin(&sched_lock);
2435cbf4e354SDavid Xu 	td->td_xsig = sig;
2436cbf4e354SDavid Xu 	while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2437cbf4e354SDavid Xu 		if (p->p_flag & P_SINGLE_EXIT) {
2438cbf4e354SDavid Xu 			mtx_lock_spin(&sched_lock);
2439cbf4e354SDavid Xu 			td->td_flags &= ~TDF_XSIG;
2440cbf4e354SDavid Xu 			mtx_unlock_spin(&sched_lock);
2441cbf4e354SDavid Xu 			return (sig);
2442cbf4e354SDavid Xu 		}
2443cbf4e354SDavid Xu 		/*
2444cbf4e354SDavid Xu 		 * Just make wait() to work, the last stopped thread
2445cbf4e354SDavid Xu 		 * will win.
2446cbf4e354SDavid Xu 		 */
2447cbf4e354SDavid Xu 		p->p_xstat = sig;
2448cbf4e354SDavid Xu 		p->p_xthread = td;
2449cbf4e354SDavid Xu 		p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2450cbf4e354SDavid Xu 		mtx_lock_spin(&sched_lock);
2451d8267df7SDavid Xu 		sig_suspend_threads(td, p, 0);
2452cbf4e354SDavid Xu stopme:
2453cbf4e354SDavid Xu 		thread_stopped(p);
24544cc9f52fSRobert Drehmel 		thread_suspend_one(td);
24554cc9f52fSRobert Drehmel 		PROC_UNLOCK(p);
24564cc9f52fSRobert Drehmel 		DROP_GIANT();
2457cbf4e354SDavid Xu 		mi_switch(SW_VOL, NULL);
24584cc9f52fSRobert Drehmel 		mtx_unlock_spin(&sched_lock);
24594cc9f52fSRobert Drehmel 		PICKUP_GIANT();
2460cbf4e354SDavid Xu 		PROC_LOCK(p);
2461cbf4e354SDavid Xu 		if (!(p->p_flag & P_TRACED))
2462cbf4e354SDavid Xu 			break;
2463cbf4e354SDavid Xu 		if (td->td_flags & TDF_DBSUSPEND) {
2464cbf4e354SDavid Xu 			if (p->p_flag & P_SINGLE_EXIT)
2465cbf4e354SDavid Xu 				break;
2466cbf4e354SDavid Xu 			mtx_lock_spin(&sched_lock);
2467cbf4e354SDavid Xu 			goto stopme;
2468cbf4e354SDavid Xu 		}
2469cbf4e354SDavid Xu 	}
2470cbf4e354SDavid Xu 	return (td->td_xsig);
24714cc9f52fSRobert Drehmel }
24724cc9f52fSRobert Drehmel 
2473df8bae1dSRodney W. Grimes /*
2474df8bae1dSRodney W. Grimes  * If the current process has received a signal (should be caught or cause
2475df8bae1dSRodney W. Grimes  * termination, should interrupt current syscall), return the signal number.
2476df8bae1dSRodney W. Grimes  * Stop signals with default action are processed immediately, then cleared;
2477df8bae1dSRodney W. Grimes  * they aren't returned.  This is checked after each entry to the system for
2478df8bae1dSRodney W. Grimes  * a syscall or trap (though this can usually be done without calling issignal
2479628855e7SJulian Elischer  * by checking the pending signal masks in cursig.) The normal call
2480df8bae1dSRodney W. Grimes  * sequence is
2481df8bae1dSRodney W. Grimes  *
2482e602ba25SJulian Elischer  *	while (sig = cursig(curthread))
24832c42a146SMarcel Moolenaar  *		postsig(sig);
2484df8bae1dSRodney W. Grimes  */
24856711f10fSJohn Baldwin static int
2486e602ba25SJulian Elischer issignal(td)
2487e602ba25SJulian Elischer 	struct thread *td;
2488df8bae1dSRodney W. Grimes {
2489e602ba25SJulian Elischer 	struct proc *p;
249090af4afaSJohn Baldwin 	struct sigacts *ps;
24914093529dSJeff Roberson 	sigset_t sigpending;
2492cbf4e354SDavid Xu 	int sig, prop, newsig;
2493df8bae1dSRodney W. Grimes 
2494e602ba25SJulian Elischer 	p = td->td_proc;
249590af4afaSJohn Baldwin 	ps = p->p_sigacts;
249690af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2497628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2498df8bae1dSRodney W. Grimes 	for (;;) {
24992a024a2bSSean Eric Fagan 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
25002a024a2bSSean Eric Fagan 
25019104847fSDavid Xu 		sigpending = td->td_sigqueue.sq_signals;
25024093529dSJeff Roberson 		SIGSETNAND(sigpending, td->td_sigmask);
25034093529dSJeff Roberson 
2504df8bae1dSRodney W. Grimes 		if (p->p_flag & P_PPWAIT)
25054093529dSJeff Roberson 			SIG_STOPSIGMASK(sigpending);
25064093529dSJeff Roberson 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2507df8bae1dSRodney W. Grimes 			return (0);
25084093529dSJeff Roberson 		sig = sig_ffs(&sigpending);
25092a024a2bSSean Eric Fagan 
2510047aa39bSRobert Watson 		if (p->p_stops & S_SIG) {
2511047aa39bSRobert Watson 			mtx_unlock(&ps->ps_mtx);
2512047aa39bSRobert Watson 			stopevent(p, S_SIG, sig);
2513047aa39bSRobert Watson 			mtx_lock(&ps->ps_mtx);
2514047aa39bSRobert Watson 		}
25152a024a2bSSean Eric Fagan 
2516df8bae1dSRodney W. Grimes 		/*
2517df8bae1dSRodney W. Grimes 		 * We should see pending but ignored signals
2518df8bae1dSRodney W. Grimes 		 * only if P_TRACED was on when they were posted.
2519df8bae1dSRodney W. Grimes 		 */
252090af4afaSJohn Baldwin 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
25219104847fSDavid Xu 			sigqueue_delete(&td->td_sigqueue, sig);
2522cbf4e354SDavid Xu 			if (td->td_pflags & TDP_SA)
2523cbf4e354SDavid Xu 				SIGADDSET(td->td_sigmask, sig);
2524df8bae1dSRodney W. Grimes 			continue;
2525df8bae1dSRodney W. Grimes 		}
2526df8bae1dSRodney W. Grimes 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2527df8bae1dSRodney W. Grimes 			/*
2528d8f4f6a4SJonathan Mini 			 * If traced, always stop.
2529df8bae1dSRodney W. Grimes 			 */
253090af4afaSJohn Baldwin 			mtx_unlock(&ps->ps_mtx);
2531cbf4e354SDavid Xu 			newsig = ptracestop(td, sig);
253290af4afaSJohn Baldwin 			mtx_lock(&ps->ps_mtx);
2533df8bae1dSRodney W. Grimes 
25349104847fSDavid Xu 			if (td->td_pflags & TDP_SA)
25359104847fSDavid Xu 				SIGADDSET(td->td_sigmask, sig);
25369104847fSDavid Xu 
25379104847fSDavid Xu 			if (sig != newsig) {
2538413cf3bbSDavid Xu 				ksiginfo_t ksi;
25399104847fSDavid Xu 				/*
25409104847fSDavid Xu 				 * clear old signal.
25419104847fSDavid Xu 				 * XXX shrug off debugger, it causes siginfo to
25429104847fSDavid Xu 				 * be thrown away.
25439104847fSDavid Xu 				 */
2544413cf3bbSDavid Xu 				sigqueue_get(&td->td_sigqueue, sig, &ksi);
25459104847fSDavid Xu 
2546df8bae1dSRodney W. Grimes 				/*
2547df8bae1dSRodney W. Grimes 				 * If parent wants us to take the signal,
2548df8bae1dSRodney W. Grimes 				 * then it will leave it in p->p_xstat;
2549df8bae1dSRodney W. Grimes 				 * otherwise we just look for signals again.
2550df8bae1dSRodney W. Grimes 			 	*/
2551cbf4e354SDavid Xu 				if (newsig == 0)
2552df8bae1dSRodney W. Grimes 					continue;
2553cbf4e354SDavid Xu 				sig = newsig;
25549104847fSDavid Xu 
25559104847fSDavid Xu 				/*
25569104847fSDavid Xu 				 * Put the new signal into td_sigqueue. If the
25579104847fSDavid Xu 				 * signal is being masked, look for other signals.
25589104847fSDavid Xu 				 */
25599104847fSDavid Xu 				SIGADDSET(td->td_sigqueue.sq_signals, sig);
25609104847fSDavid Xu 				if (td->td_pflags & TDP_SA)
25619104847fSDavid Xu 					SIGDELSET(td->td_sigmask, sig);
25629104847fSDavid Xu 				if (SIGISMEMBER(td->td_sigmask, sig))
25639104847fSDavid Xu 					continue;
25649104847fSDavid Xu 				signotify(td);
25659104847fSDavid Xu 			}
25669104847fSDavid Xu 
2567df8bae1dSRodney W. Grimes 			/*
25688d542cb5SDavid E. O'Brien 			 * If the traced bit got turned off, go back up
25698d542cb5SDavid E. O'Brien 			 * to the top to rescan signals.  This ensures
25708d542cb5SDavid E. O'Brien 			 * that p_sig* and p_sigact are consistent.
25718d542cb5SDavid E. O'Brien 			 */
25728d542cb5SDavid E. O'Brien 			if ((p->p_flag & P_TRACED) == 0)
25738d542cb5SDavid E. O'Brien 				continue;
2574df8bae1dSRodney W. Grimes 		}
2575df8bae1dSRodney W. Grimes 
25768d542cb5SDavid E. O'Brien 		prop = sigprop(sig);
25778d542cb5SDavid E. O'Brien 
2578df8bae1dSRodney W. Grimes 		/*
2579df8bae1dSRodney W. Grimes 		 * Decide whether the signal should be returned.
2580df8bae1dSRodney W. Grimes 		 * Return the signal's number, or fall through
2581df8bae1dSRodney W. Grimes 		 * to clear it from the pending mask.
2582df8bae1dSRodney W. Grimes 		 */
2583d321df47SPoul-Henning Kamp 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2584df8bae1dSRodney W. Grimes 
2585d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_DFL:
2586df8bae1dSRodney W. Grimes 			/*
2587df8bae1dSRodney W. Grimes 			 * Don't take default actions on system processes.
2588df8bae1dSRodney W. Grimes 			 */
2589df8bae1dSRodney W. Grimes 			if (p->p_pid <= 1) {
2590df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
2591df8bae1dSRodney W. Grimes 				/*
2592df8bae1dSRodney W. Grimes 				 * Are you sure you want to ignore SIGSEGV
2593df8bae1dSRodney W. Grimes 				 * in init? XXX
2594df8bae1dSRodney W. Grimes 				 */
2595d93f860cSPoul-Henning Kamp 				printf("Process (pid %lu) got signal %d\n",
25962c42a146SMarcel Moolenaar 					(u_long)p->p_pid, sig);
2597df8bae1dSRodney W. Grimes #endif
2598df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2599df8bae1dSRodney W. Grimes 			}
2600df8bae1dSRodney W. Grimes 			/*
2601df8bae1dSRodney W. Grimes 			 * If there is a pending stop signal to process
2602df8bae1dSRodney W. Grimes 			 * with default action, stop here,
2603df8bae1dSRodney W. Grimes 			 * then clear the signal.  However,
2604df8bae1dSRodney W. Grimes 			 * if process is member of an orphaned
2605df8bae1dSRodney W. Grimes 			 * process group, ignore tty stop signals.
2606df8bae1dSRodney W. Grimes 			 */
2607df8bae1dSRodney W. Grimes 			if (prop & SA_STOP) {
2608df8bae1dSRodney W. Grimes 				if (p->p_flag & P_TRACED ||
2609df8bae1dSRodney W. Grimes 		    		    (p->p_pgrp->pg_jobc == 0 &&
2610df8bae1dSRodney W. Grimes 				     prop & SA_TTYSTOP))
2611df8bae1dSRodney W. Grimes 					break;	/* == ignore */
261290af4afaSJohn Baldwin 				mtx_unlock(&ps->ps_mtx);
261390af4afaSJohn Baldwin 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
261490af4afaSJohn Baldwin 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2615e574e444SDavid Xu 				p->p_flag |= P_STOPPED_SIG;
26162c42a146SMarcel Moolenaar 				p->p_xstat = sig;
2617721e5910SJulian Elischer 				mtx_lock_spin(&sched_lock);
2618d8267df7SDavid Xu 				sig_suspend_threads(td, p, 0);
2619e574e444SDavid Xu 				thread_stopped(p);
262071fad9fdSJulian Elischer 				thread_suspend_one(td);
2621721e5910SJulian Elischer 				PROC_UNLOCK(p);
2622721e5910SJulian Elischer 				DROP_GIANT();
2623bf0acc27SJohn Baldwin 				mi_switch(SW_INVOL, NULL);
2624721e5910SJulian Elischer 				mtx_unlock_spin(&sched_lock);
2625721e5910SJulian Elischer 				PICKUP_GIANT();
2626721e5910SJulian Elischer 				PROC_LOCK(p);
262790af4afaSJohn Baldwin 				mtx_lock(&ps->ps_mtx);
2628df8bae1dSRodney W. Grimes 				break;
262921b68415SDavid E. O'Brien 			} else if (prop & SA_IGNORE) {
2630df8bae1dSRodney W. Grimes 				/*
2631df8bae1dSRodney W. Grimes 				 * Except for SIGCONT, shouldn't get here.
2632df8bae1dSRodney W. Grimes 				 * Default action is to ignore; drop it.
2633df8bae1dSRodney W. Grimes 				 */
2634df8bae1dSRodney W. Grimes 				break;		/* == ignore */
2635df8bae1dSRodney W. Grimes 			} else
26362c42a146SMarcel Moolenaar 				return (sig);
2637df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
2638df8bae1dSRodney W. Grimes 
2639d321df47SPoul-Henning Kamp 		case (intptr_t)SIG_IGN:
2640df8bae1dSRodney W. Grimes 			/*
2641df8bae1dSRodney W. Grimes 			 * Masking above should prevent us ever trying
2642df8bae1dSRodney W. Grimes 			 * to take action on an ignored signal other
2643df8bae1dSRodney W. Grimes 			 * than SIGCONT, unless process is traced.
2644df8bae1dSRodney W. Grimes 			 */
2645df8bae1dSRodney W. Grimes 			if ((prop & SA_CONT) == 0 &&
2646df8bae1dSRodney W. Grimes 			    (p->p_flag & P_TRACED) == 0)
2647df8bae1dSRodney W. Grimes 				printf("issignal\n");
2648df8bae1dSRodney W. Grimes 			break;		/* == ignore */
2649df8bae1dSRodney W. Grimes 
2650df8bae1dSRodney W. Grimes 		default:
2651df8bae1dSRodney W. Grimes 			/*
2652df8bae1dSRodney W. Grimes 			 * This signal has an action, let
2653df8bae1dSRodney W. Grimes 			 * postsig() process it.
2654df8bae1dSRodney W. Grimes 			 */
26552c42a146SMarcel Moolenaar 			return (sig);
2656df8bae1dSRodney W. Grimes 		}
26579104847fSDavid Xu 		sigqueue_delete(&td->td_sigqueue, sig);		/* take the signal! */
2658df8bae1dSRodney W. Grimes 	}
2659df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2660df8bae1dSRodney W. Grimes }
2661df8bae1dSRodney W. Grimes 
2662df8bae1dSRodney W. Grimes /*
266390af4afaSJohn Baldwin  * MPSAFE
266490af4afaSJohn Baldwin  */
2665e574e444SDavid Xu void
2666e574e444SDavid Xu thread_stopped(struct proc *p)
2667e574e444SDavid Xu {
2668e574e444SDavid Xu 	struct proc *p1 = curthread->td_proc;
2669e574e444SDavid Xu 	int n;
2670e574e444SDavid Xu 
2671e574e444SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2672e574e444SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
2673e574e444SDavid Xu 	n = p->p_suspcount;
2674e574e444SDavid Xu 	if (p == p1)
2675e574e444SDavid Xu 		n++;
2676e574e444SDavid Xu 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2677e574e444SDavid Xu 		mtx_unlock_spin(&sched_lock);
2678407948a5SDavid Xu 		p->p_flag &= ~P_WAITED;
2679e574e444SDavid Xu 		PROC_LOCK(p->p_pptr);
26807f96995eSDavid Xu 		childproc_stopped(p, (p->p_flag & P_TRACED) ?
2681ebceaf6dSDavid Xu 			CLD_TRAPPED : CLD_STOPPED);
2682e574e444SDavid Xu 		PROC_UNLOCK(p->p_pptr);
2683e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
2684e574e444SDavid Xu 	}
2685e574e444SDavid Xu }
2686e574e444SDavid Xu 
2687df8bae1dSRodney W. Grimes /*
2688df8bae1dSRodney W. Grimes  * Take the action for the specified signal
2689df8bae1dSRodney W. Grimes  * from the current set of pending signals.
2690df8bae1dSRodney W. Grimes  */
2691df8bae1dSRodney W. Grimes void
26922c42a146SMarcel Moolenaar postsig(sig)
26932c42a146SMarcel Moolenaar 	register int sig;
2694df8bae1dSRodney W. Grimes {
2695b40ce416SJulian Elischer 	struct thread *td = curthread;
2696b40ce416SJulian Elischer 	register struct proc *p = td->td_proc;
2697628d2653SJohn Baldwin 	struct sigacts *ps;
26982c42a146SMarcel Moolenaar 	sig_t action;
26999104847fSDavid Xu 	ksiginfo_t ksi;
27002c42a146SMarcel Moolenaar 	sigset_t returnmask;
27012c42a146SMarcel Moolenaar 	int code;
2702df8bae1dSRodney W. Grimes 
27032c42a146SMarcel Moolenaar 	KASSERT(sig != 0, ("postsig"));
27045526d2d9SEivind Eklund 
27052ad7d304SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2706628d2653SJohn Baldwin 	ps = p->p_sigacts;
270790af4afaSJohn Baldwin 	mtx_assert(&ps->ps_mtx, MA_OWNED);
27085da49fcbSDavid Xu 	ksiginfo_init(&ksi);
27099104847fSDavid Xu 	sigqueue_get(&td->td_sigqueue, sig, &ksi);
27109104847fSDavid Xu 	ksi.ksi_signo = sig;
271156c06c4bSDavid Xu 	if (ksi.ksi_code == SI_TIMER)
271256c06c4bSDavid Xu 		itimer_accept(p, ksi.ksi_timerid, &ksi);
27132c42a146SMarcel Moolenaar 	action = ps->ps_sigact[_SIG_IDX(sig)];
2714df8bae1dSRodney W. Grimes #ifdef KTRACE
2715374a15aaSJohn Baldwin 	if (KTRPOINT(td, KTR_PSIG))
27165e26dcb5SJohn Baldwin 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
27174093529dSJeff Roberson 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2718df8bae1dSRodney W. Grimes #endif
2719047aa39bSRobert Watson 	if (p->p_stops & S_SIG) {
2720047aa39bSRobert Watson 		mtx_unlock(&ps->ps_mtx);
2721047aa39bSRobert Watson 		stopevent(p, S_SIG, sig);
2722047aa39bSRobert Watson 		mtx_lock(&ps->ps_mtx);
2723047aa39bSRobert Watson 	}
27242a024a2bSSean Eric Fagan 
2725cbf4e354SDavid Xu 	if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2726df8bae1dSRodney W. Grimes 		/*
2727df8bae1dSRodney W. Grimes 		 * Default action, where the default is to kill
2728df8bae1dSRodney W. Grimes 		 * the process.  (Other cases were ignored above.)
2729df8bae1dSRodney W. Grimes 		 */
273090af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
2731b40ce416SJulian Elischer 		sigexit(td, sig);
2732df8bae1dSRodney W. Grimes 		/* NOTREACHED */
2733df8bae1dSRodney W. Grimes 	} else {
2734cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA) {
2735432b45deSDavid Xu 			if (sig == SIGKILL) {
2736432b45deSDavid Xu 				mtx_unlock(&ps->ps_mtx);
2737432b45deSDavid Xu 				sigexit(td, sig);
2738432b45deSDavid Xu 			}
2739432b45deSDavid Xu 		}
2740432b45deSDavid Xu 
2741df8bae1dSRodney W. Grimes 		/*
2742df8bae1dSRodney W. Grimes 		 * If we get here, the signal must be caught.
2743df8bae1dSRodney W. Grimes 		 */
27444093529dSJeff Roberson 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
27455526d2d9SEivind Eklund 		    ("postsig action"));
2746df8bae1dSRodney W. Grimes 		/*
2747df8bae1dSRodney W. Grimes 		 * Set the new mask value and also defer further
2748645682fdSLuoqi Chen 		 * occurrences of this signal.
2749df8bae1dSRodney W. Grimes 		 *
2750645682fdSLuoqi Chen 		 * Special case: user has done a sigsuspend.  Here the
2751df8bae1dSRodney W. Grimes 		 * current mask is not of interest, but rather the
2752645682fdSLuoqi Chen 		 * mask from before the sigsuspend is what we want
2753df8bae1dSRodney W. Grimes 		 * restored after the signal processing is completed.
2754df8bae1dSRodney W. Grimes 		 */
27555e26dcb5SJohn Baldwin 		if (td->td_pflags & TDP_OLDMASK) {
27564093529dSJeff Roberson 			returnmask = td->td_oldsigmask;
27575e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_OLDMASK;
2758df8bae1dSRodney W. Grimes 		} else
27594093529dSJeff Roberson 			returnmask = td->td_sigmask;
27602c42a146SMarcel Moolenaar 
27614093529dSJeff Roberson 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
27622c42a146SMarcel Moolenaar 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
27634093529dSJeff Roberson 			SIGADDSET(td->td_sigmask, sig);
27642c42a146SMarcel Moolenaar 
27652c42a146SMarcel Moolenaar 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2766289ccde0SPeter Wemm 			/*
27678f19eb88SIan Dowse 			 * See kern_sigaction() for origin of this code.
2768289ccde0SPeter Wemm 			 */
276990af4afaSJohn Baldwin 			SIGDELSET(ps->ps_sigcatch, sig);
27702c42a146SMarcel Moolenaar 			if (sig != SIGCONT &&
27712c42a146SMarcel Moolenaar 			    sigprop(sig) & SA_IGNORE)
277290af4afaSJohn Baldwin 				SIGADDSET(ps->ps_sigignore, sig);
27732c42a146SMarcel Moolenaar 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2774dedc04feSPeter Wemm 		}
2775df8bae1dSRodney W. Grimes 		p->p_stats->p_ru.ru_nsignals++;
27762c42a146SMarcel Moolenaar 		if (p->p_sig != sig) {
2777df8bae1dSRodney W. Grimes 			code = 0;
2778df8bae1dSRodney W. Grimes 		} else {
27796626c604SJulian Elischer 			code = p->p_code;
27806626c604SJulian Elischer 			p->p_code = 0;
27816626c604SJulian Elischer 			p->p_sig = 0;
2782df8bae1dSRodney W. Grimes 		}
2783cbf4e354SDavid Xu 		if (td->td_pflags & TDP_SA)
27849104847fSDavid Xu 			thread_signal_add(curthread, &ksi);
278558a3c273SJeff Roberson 		else
27869104847fSDavid Xu 			(*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2787df8bae1dSRodney W. Grimes 	}
2788df8bae1dSRodney W. Grimes }
2789df8bae1dSRodney W. Grimes 
2790df8bae1dSRodney W. Grimes /*
2791df8bae1dSRodney W. Grimes  * Kill the current process for stated reason.
2792df8bae1dSRodney W. Grimes  */
279326f9a767SRodney W. Grimes void
2794df8bae1dSRodney W. Grimes killproc(p, why)
2795df8bae1dSRodney W. Grimes 	struct proc *p;
2796df8bae1dSRodney W. Grimes 	char *why;
2797df8bae1dSRodney W. Grimes {
27989081e5e8SJohn Baldwin 
27999081e5e8SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
28000384fff8SJason Evans 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
28010384fff8SJason Evans 		p, p->p_pid, p->p_comm);
2802729b1e51SDavid Greenman 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2803b1fc0ec1SRobert Watson 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2804df8bae1dSRodney W. Grimes 	psignal(p, SIGKILL);
2805df8bae1dSRodney W. Grimes }
2806df8bae1dSRodney W. Grimes 
2807df8bae1dSRodney W. Grimes /*
2808df8bae1dSRodney W. Grimes  * Force the current process to exit with the specified signal, dumping core
2809df8bae1dSRodney W. Grimes  * if appropriate.  We bypass the normal tests for masked and caught signals,
2810df8bae1dSRodney W. Grimes  * allowing unrecoverable failures to terminate the process without changing
2811df8bae1dSRodney W. Grimes  * signal state.  Mark the accounting record with the signal termination.
2812df8bae1dSRodney W. Grimes  * If dumping core, save the signal number for the debugger.  Calls exit and
2813df8bae1dSRodney W. Grimes  * does not return.
281490af4afaSJohn Baldwin  *
281590af4afaSJohn Baldwin  * MPSAFE
2816df8bae1dSRodney W. Grimes  */
281726f9a767SRodney W. Grimes void
2818b40ce416SJulian Elischer sigexit(td, sig)
2819b40ce416SJulian Elischer 	struct thread *td;
28202c42a146SMarcel Moolenaar 	int sig;
2821df8bae1dSRodney W. Grimes {
2822b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2823df8bae1dSRodney W. Grimes 
2824628d2653SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
2825df8bae1dSRodney W. Grimes 	p->p_acflag |= AXSIG;
2826f97c3df1SDavid Schultz 	/*
2827f97c3df1SDavid Schultz 	 * We must be single-threading to generate a core dump.  This
2828f97c3df1SDavid Schultz 	 * ensures that the registers in the core file are up-to-date.
2829f97c3df1SDavid Schultz 	 * Also, the ELF dump handler assumes that the thread list doesn't
2830f97c3df1SDavid Schultz 	 * change out from under it.
2831f97c3df1SDavid Schultz 	 *
2832f97c3df1SDavid Schultz 	 * XXX If another thread attempts to single-thread before us
2833f97c3df1SDavid Schultz 	 *     (e.g. via fork()), we won't get a dump at all.
2834f97c3df1SDavid Schultz 	 */
2835f97c3df1SDavid Schultz 	if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
28362c42a146SMarcel Moolenaar 		p->p_sig = sig;
2837c364e17eSAndrey A. Chernov 		/*
2838c364e17eSAndrey A. Chernov 		 * Log signals which would cause core dumps
2839c364e17eSAndrey A. Chernov 		 * (Log as LOG_INFO to appease those who don't want
2840c364e17eSAndrey A. Chernov 		 * these messages.)
2841c364e17eSAndrey A. Chernov 		 * XXX : Todo, as well as euid, write out ruid too
28424ae89b95SJohn Baldwin 		 * Note that coredump() drops proc lock.
2843c364e17eSAndrey A. Chernov 		 */
2844b40ce416SJulian Elischer 		if (coredump(td) == 0)
28452c42a146SMarcel Moolenaar 			sig |= WCOREFLAG;
284657308494SJoerg Wunsch 		if (kern_logsigexit)
284757308494SJoerg Wunsch 			log(LOG_INFO,
284857308494SJoerg Wunsch 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
28493d1b21c6SAndrey A. Chernov 			    p->p_pid, p->p_comm,
28509c1ab3e0SJohn Baldwin 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
28512c42a146SMarcel Moolenaar 			    sig &~ WCOREFLAG,
28522c42a146SMarcel Moolenaar 			    sig & WCOREFLAG ? " (core dumped)" : "");
28534ae89b95SJohn Baldwin 	} else
2854628d2653SJohn Baldwin 		PROC_UNLOCK(p);
2855b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(0, sig));
2856df8bae1dSRodney W. Grimes 	/* NOTREACHED */
2857df8bae1dSRodney W. Grimes }
2858df8bae1dSRodney W. Grimes 
2859ebceaf6dSDavid Xu /*
28607f96995eSDavid Xu  * Send queued SIGCHLD to parent when child process's state
28617f96995eSDavid Xu  * is changed.
2862ebceaf6dSDavid Xu  */
28637f96995eSDavid Xu static void
28647f96995eSDavid Xu sigparent(struct proc *p, int reason, int status)
2865ebceaf6dSDavid Xu {
2866ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
2867ebceaf6dSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2868ebceaf6dSDavid Xu 
2869ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
2870ebceaf6dSDavid Xu 		p->p_ksi->ksi_signo  = SIGCHLD;
2871ebceaf6dSDavid Xu 		p->p_ksi->ksi_code   = reason;
28727f96995eSDavid Xu 		p->p_ksi->ksi_status = status;
2873ebceaf6dSDavid Xu 		p->p_ksi->ksi_pid    = p->p_pid;
2874ebceaf6dSDavid Xu 		p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
2875ebceaf6dSDavid Xu 		if (KSI_ONQ(p->p_ksi))
2876ebceaf6dSDavid Xu 			return;
2877ebceaf6dSDavid Xu 	}
2878ebceaf6dSDavid Xu 	tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2879ebceaf6dSDavid Xu }
2880ebceaf6dSDavid Xu 
28817f96995eSDavid Xu static void
28827f96995eSDavid Xu childproc_jobstate(struct proc *p, int reason, int status)
28837f96995eSDavid Xu {
28847f96995eSDavid Xu 	struct sigacts *ps;
28857f96995eSDavid Xu 
28867f96995eSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
28877f96995eSDavid Xu 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
28887f96995eSDavid Xu 
28897f96995eSDavid Xu 	/*
28907f96995eSDavid Xu 	 * Wake up parent sleeping in kern_wait(), also send
28917f96995eSDavid Xu 	 * SIGCHLD to parent, but SIGCHLD does not guarantee
28927f96995eSDavid Xu 	 * that parent will awake, because parent may masked
28937f96995eSDavid Xu 	 * the signal.
28947f96995eSDavid Xu 	 */
28957f96995eSDavid Xu 	p->p_pptr->p_flag |= P_STATCHILD;
28967f96995eSDavid Xu 	wakeup(p->p_pptr);
28977f96995eSDavid Xu 
28987f96995eSDavid Xu 	ps = p->p_pptr->p_sigacts;
28997f96995eSDavid Xu 	mtx_lock(&ps->ps_mtx);
29007f96995eSDavid Xu 	if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
29017f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
29027f96995eSDavid Xu 		sigparent(p, reason, status);
29037f96995eSDavid Xu 	} else
29047f96995eSDavid Xu 		mtx_unlock(&ps->ps_mtx);
29057f96995eSDavid Xu }
29067f96995eSDavid Xu 
29077f96995eSDavid Xu void
29087f96995eSDavid Xu childproc_stopped(struct proc *p, int reason)
29097f96995eSDavid Xu {
29107f96995eSDavid Xu 	childproc_jobstate(p, reason, p->p_xstat);
29117f96995eSDavid Xu }
29127f96995eSDavid Xu 
2913ebceaf6dSDavid Xu void
2914ebceaf6dSDavid Xu childproc_continued(struct proc *p)
2915ebceaf6dSDavid Xu {
29167f96995eSDavid Xu 	childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2917ebceaf6dSDavid Xu }
2918ebceaf6dSDavid Xu 
2919ebceaf6dSDavid Xu void
2920ebceaf6dSDavid Xu childproc_exited(struct proc *p)
2921ebceaf6dSDavid Xu {
2922ebceaf6dSDavid Xu 	int reason;
2923f4d85223SDavid Xu 	int status = p->p_xstat; /* convert to int */
2924ebceaf6dSDavid Xu 
2925ebceaf6dSDavid Xu 	reason = CLD_EXITED;
2926f4d85223SDavid Xu 	if (WCOREDUMP(status))
2927ebceaf6dSDavid Xu 		reason = CLD_DUMPED;
2928f4d85223SDavid Xu 	else if (WIFSIGNALED(status))
2929ebceaf6dSDavid Xu 		reason = CLD_KILLED;
29307f96995eSDavid Xu 	/*
29317f96995eSDavid Xu 	 * XXX avoid calling wakeup(p->p_pptr), the work is
29327f96995eSDavid Xu 	 * done in exit1().
29337f96995eSDavid Xu 	 */
29347f96995eSDavid Xu 	sigparent(p, reason, status);
2935ebceaf6dSDavid Xu }
2936ebceaf6dSDavid Xu 
29376d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"};
2938c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2939c5edb423SSean Eric Fagan 	      sizeof(corefilename), "process corefile name format string");
2940c5edb423SSean Eric Fagan 
2941c5edb423SSean Eric Fagan /*
2942c5edb423SSean Eric Fagan  * expand_name(name, uid, pid)
2943c5edb423SSean Eric Fagan  * Expand the name described in corefilename, using name, uid, and pid.
2944c5edb423SSean Eric Fagan  * corefilename is a printf-like string, with three format specifiers:
2945c5edb423SSean Eric Fagan  *	%N	name of process ("name")
2946c5edb423SSean Eric Fagan  *	%P	process id (pid)
2947c5edb423SSean Eric Fagan  *	%U	user id (uid)
2948c5edb423SSean Eric Fagan  * For example, "%N.core" is the default; they can be disabled completely
2949c5edb423SSean Eric Fagan  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2950c5edb423SSean Eric Fagan  * This is controlled by the sysctl variable kern.corefile (see above).
2951c5edb423SSean Eric Fagan  */
2952c5edb423SSean Eric Fagan 
2953fca666a1SJulian Elischer static char *
2954c5edb423SSean Eric Fagan expand_name(name, uid, pid)
29558b43b535SAlfred Perlstein 	const char *name;
29568b43b535SAlfred Perlstein 	uid_t uid;
29578b43b535SAlfred Perlstein 	pid_t pid;
29588b43b535SAlfred Perlstein {
29598b43b535SAlfred Perlstein 	const char *format, *appendstr;
2960c5edb423SSean Eric Fagan 	char *temp;
2961c5edb423SSean Eric Fagan 	char buf[11];		/* Buffer for pid/uid -- max 4B */
29628b43b535SAlfred Perlstein 	size_t i, l, n;
2963c5edb423SSean Eric Fagan 
29648b43b535SAlfred Perlstein 	format = corefilename;
29658b43b535SAlfred Perlstein 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
29660bfe2990SEivind Eklund 	if (temp == NULL)
29678b43b535SAlfred Perlstein 		return (NULL);
2968ce38ca0fSAlfred Perlstein 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2969c5edb423SSean Eric Fagan 		switch (format[i]) {
2970c5edb423SSean Eric Fagan 		case '%':	/* Format character */
2971c5edb423SSean Eric Fagan 			i++;
2972c5edb423SSean Eric Fagan 			switch (format[i]) {
2973c5edb423SSean Eric Fagan 			case '%':
29748b43b535SAlfred Perlstein 				appendstr = "%";
2975c5edb423SSean Eric Fagan 				break;
2976c5edb423SSean Eric Fagan 			case 'N':	/* process name */
29778b43b535SAlfred Perlstein 				appendstr = name;
2978c5edb423SSean Eric Fagan 				break;
2979c5edb423SSean Eric Fagan 			case 'P':	/* process id */
29808b43b535SAlfred Perlstein 				sprintf(buf, "%u", pid);
29818b43b535SAlfred Perlstein 				appendstr = buf;
2982c5edb423SSean Eric Fagan 				break;
2983c5edb423SSean Eric Fagan 			case 'U':	/* user id */
29848b43b535SAlfred Perlstein 				sprintf(buf, "%u", uid);
29858b43b535SAlfred Perlstein 				appendstr = buf;
2986c5edb423SSean Eric Fagan 				break;
2987c5edb423SSean Eric Fagan 			default:
29888b43b535SAlfred Perlstein 				appendstr = "";
29898b43b535SAlfred Perlstein 			  	log(LOG_ERR,
29908b43b535SAlfred Perlstein 				    "Unknown format character %c in `%s'\n",
29918b43b535SAlfred Perlstein 				    format[i], format);
2992c5edb423SSean Eric Fagan 			}
29938b43b535SAlfred Perlstein 			l = strlen(appendstr);
29948b43b535SAlfred Perlstein 			if ((n + l) >= MAXPATHLEN)
29958b43b535SAlfred Perlstein 				goto toolong;
29968b43b535SAlfred Perlstein 			memcpy(temp + n, appendstr, l);
29978b43b535SAlfred Perlstein 			n += l;
2998c5edb423SSean Eric Fagan 			break;
2999c5edb423SSean Eric Fagan 		default:
3000c5edb423SSean Eric Fagan 			temp[n++] = format[i];
3001c5edb423SSean Eric Fagan 		}
3002c5edb423SSean Eric Fagan 	}
30038b43b535SAlfred Perlstein 	if (format[i] != '\0')
30048b43b535SAlfred Perlstein 		goto toolong;
30058b43b535SAlfred Perlstein 	return (temp);
30068b43b535SAlfred Perlstein toolong:
30078b43b535SAlfred Perlstein 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
30088b43b535SAlfred Perlstein 	    (long)pid, name, (u_long)uid);
30098b43b535SAlfred Perlstein 	free(temp, M_TEMP);
30108b43b535SAlfred Perlstein 	return (NULL);
3011c5edb423SSean Eric Fagan }
3012c5edb423SSean Eric Fagan 
3013df8bae1dSRodney W. Grimes /*
3014fca666a1SJulian Elischer  * Dump a process' core.  The main routine does some
3015fca666a1SJulian Elischer  * policy checking, and creates the name of the coredump;
3016fca666a1SJulian Elischer  * then it passes on a vnode and a size limit to the process-specific
3017fca666a1SJulian Elischer  * coredump routine if there is one; if there _is not_ one, it returns
3018fca666a1SJulian Elischer  * ENOSYS; otherwise it returns the error from the process-specific routine.
3019fca666a1SJulian Elischer  */
3020fca666a1SJulian Elischer 
3021fca666a1SJulian Elischer static int
3022b40ce416SJulian Elischer coredump(struct thread *td)
3023fca666a1SJulian Elischer {
3024b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3025fca666a1SJulian Elischer 	register struct vnode *vp;
30269c1ab3e0SJohn Baldwin 	register struct ucred *cred = td->td_ucred;
302706ae1e91SMatthew Dillon 	struct flock lf;
3028fca666a1SJulian Elischer 	struct nameidata nd;
3029fca666a1SJulian Elischer 	struct vattr vattr;
3030c447f5b2SRobert Watson 	int error, error1, flags, locked;
3031f2a2857bSKirk McKusick 	struct mount *mp;
3032fca666a1SJulian Elischer 	char *name;			/* name of corefile */
3033fca666a1SJulian Elischer 	off_t limit;
3034fca666a1SJulian Elischer 
30354ae89b95SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3036f97c3df1SDavid Schultz 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3037628d2653SJohn Baldwin 	_STOPEVENT(p, S_CORE, 0);
3038fca666a1SJulian Elischer 
3039628d2653SJohn Baldwin 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3040628d2653SJohn Baldwin 		PROC_UNLOCK(p);
3041fca666a1SJulian Elischer 		return (EFAULT);
3042628d2653SJohn Baldwin 	}
3043fca666a1SJulian Elischer 
3044fca666a1SJulian Elischer 	/*
304535a2598fSSean Eric Fagan 	 * Note that the bulk of limit checking is done after
304635a2598fSSean Eric Fagan 	 * the corefile is created.  The exception is if the limit
304735a2598fSSean Eric Fagan 	 * for corefiles is 0, in which case we don't bother
304835a2598fSSean Eric Fagan 	 * creating the corefile at all.  This layout means that
304935a2598fSSean Eric Fagan 	 * a corefile is truncated instead of not being created,
305035a2598fSSean Eric Fagan 	 * if it is larger than the limit.
3051fca666a1SJulian Elischer 	 */
305291d5354aSJohn Baldwin 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
3053628d2653SJohn Baldwin 	PROC_UNLOCK(p);
305491d5354aSJohn Baldwin 	if (limit == 0)
305591d5354aSJohn Baldwin 		return (EFBIG);
305635a2598fSSean Eric Fagan 
30574ae89b95SJohn Baldwin 	mtx_lock(&Giant);
3058f2a2857bSKirk McKusick restart:
30599c1ab3e0SJohn Baldwin 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
30604ae89b95SJohn Baldwin 	if (name == NULL) {
30614ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
3062ccdbd10cSPeter Pentchev 		return (EINVAL);
30634ae89b95SJohn Baldwin 	}
3064b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
3065e6796b67SKirk McKusick 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
30667c89f162SPoul-Henning Kamp 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
3067fca666a1SJulian Elischer 	free(name, M_TEMP);
30684ae89b95SJohn Baldwin 	if (error) {
30694ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
3070fca666a1SJulian Elischer 		return (error);
30714ae89b95SJohn Baldwin 	}
3072762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
3073fca666a1SJulian Elischer 	vp = nd.ni_vp;
307406ae1e91SMatthew Dillon 
3075832dafadSDon Lewis 	/* Don't dump to non-regular files or files with links. */
3076832dafadSDon Lewis 	if (vp->v_type != VREG ||
3077832dafadSDon Lewis 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
3078832dafadSDon Lewis 		VOP_UNLOCK(vp, 0, td);
3079832dafadSDon Lewis 		error = EFAULT;
30804ae89b95SJohn Baldwin 		goto out;
3081832dafadSDon Lewis 	}
3082832dafadSDon Lewis 
3083b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
308406ae1e91SMatthew Dillon 	lf.l_whence = SEEK_SET;
308506ae1e91SMatthew Dillon 	lf.l_start = 0;
308606ae1e91SMatthew Dillon 	lf.l_len = 0;
308706ae1e91SMatthew Dillon 	lf.l_type = F_WRLCK;
3088c447f5b2SRobert Watson 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
308906ae1e91SMatthew Dillon 
309006ae1e91SMatthew Dillon 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
309106ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
309236bbf86bSRobert Watson 		if (locked)
309306ae1e91SMatthew Dillon 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3094b40ce416SJulian Elischer 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3095f2a2857bSKirk McKusick 			return (error);
3096f2a2857bSKirk McKusick 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3097f2a2857bSKirk McKusick 			return (error);
3098f2a2857bSKirk McKusick 		goto restart;
3099f2a2857bSKirk McKusick 	}
3100fca666a1SJulian Elischer 
3101fca666a1SJulian Elischer 	VATTR_NULL(&vattr);
3102fca666a1SJulian Elischer 	vattr.va_size = 0;
31036141e04aSJohn-Mark Gurney 	if (set_core_nodump_flag)
31046141e04aSJohn-Mark Gurney 		vattr.va_flags = UF_NODUMP;
310588b1d98fSPaul Saab 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3106b40ce416SJulian Elischer 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
3107b40ce416SJulian Elischer 	VOP_SETATTR(vp, &vattr, cred, td);
310888b1d98fSPaul Saab 	VOP_UNLOCK(vp, 0, td);
3109628d2653SJohn Baldwin 	PROC_LOCK(p);
3110fca666a1SJulian Elischer 	p->p_acflag |= ACORE;
3111628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3112fca666a1SJulian Elischer 
3113fca666a1SJulian Elischer 	error = p->p_sysent->sv_coredump ?
3114b40ce416SJulian Elischer 	  p->p_sysent->sv_coredump(td, vp, limit) :
3115fca666a1SJulian Elischer 	  ENOSYS;
3116fca666a1SJulian Elischer 
3117c447f5b2SRobert Watson 	if (locked) {
311806ae1e91SMatthew Dillon 		lf.l_type = F_UNLCK;
311906ae1e91SMatthew Dillon 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3120c447f5b2SRobert Watson 	}
3121f2a2857bSKirk McKusick 	vn_finished_write(mp);
31224ae89b95SJohn Baldwin out:
3123b40ce416SJulian Elischer 	error1 = vn_close(vp, FWRITE, cred, td);
31244ae89b95SJohn Baldwin 	mtx_unlock(&Giant);
3125fca666a1SJulian Elischer 	if (error == 0)
3126fca666a1SJulian Elischer 		error = error1;
3127fca666a1SJulian Elischer 	return (error);
3128fca666a1SJulian Elischer }
3129fca666a1SJulian Elischer 
3130fca666a1SJulian Elischer /*
3131df8bae1dSRodney W. Grimes  * Nonexistent system call-- signal process (may want to handle it).
3132df8bae1dSRodney W. Grimes  * Flag error in case process won't see signal immediately (blocked or ignored).
3133df8bae1dSRodney W. Grimes  */
3134d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
3135df8bae1dSRodney W. Grimes struct nosys_args {
3136df8bae1dSRodney W. Grimes 	int	dummy;
3137df8bae1dSRodney W. Grimes };
3138d2d3e875SBruce Evans #endif
3139fb99ab88SMatthew Dillon /*
3140fb99ab88SMatthew Dillon  * MPSAFE
3141fb99ab88SMatthew Dillon  */
3142df8bae1dSRodney W. Grimes /* ARGSUSED */
314326f9a767SRodney W. Grimes int
3144b40ce416SJulian Elischer nosys(td, args)
3145b40ce416SJulian Elischer 	struct thread *td;
3146df8bae1dSRodney W. Grimes 	struct nosys_args *args;
3147df8bae1dSRodney W. Grimes {
3148b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
3149b40ce416SJulian Elischer 
3150628d2653SJohn Baldwin 	PROC_LOCK(p);
3151df8bae1dSRodney W. Grimes 	psignal(p, SIGSYS);
3152628d2653SJohn Baldwin 	PROC_UNLOCK(p);
3153f5216b9aSBruce Evans 	return (ENOSYS);
3154df8bae1dSRodney W. Grimes }
3155831d27a9SDon Lewis 
3156831d27a9SDon Lewis /*
315748f1ba5bSRobert Watson  * Send a SIGIO or SIGURG signal to a process or process group using
3158831d27a9SDon Lewis  * stored credentials rather than those of the current process.
3159831d27a9SDon Lewis  */
3160831d27a9SDon Lewis void
3161f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty)
3162f1320723SAlfred Perlstein 	struct sigio **sigiop;
31632c42a146SMarcel Moolenaar 	int sig, checkctty;
3164831d27a9SDon Lewis {
3165f1320723SAlfred Perlstein 	struct sigio *sigio;
3166831d27a9SDon Lewis 
3167f1320723SAlfred Perlstein 	SIGIO_LOCK();
3168f1320723SAlfred Perlstein 	sigio = *sigiop;
3169f1320723SAlfred Perlstein 	if (sigio == NULL) {
3170f1320723SAlfred Perlstein 		SIGIO_UNLOCK();
3171f1320723SAlfred Perlstein 		return;
3172f1320723SAlfred Perlstein 	}
3173831d27a9SDon Lewis 	if (sigio->sio_pgid > 0) {
3174628d2653SJohn Baldwin 		PROC_LOCK(sigio->sio_proc);
31752b87b6d4SRobert Watson 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
31762c42a146SMarcel Moolenaar 			psignal(sigio->sio_proc, sig);
3177628d2653SJohn Baldwin 		PROC_UNLOCK(sigio->sio_proc);
3178831d27a9SDon Lewis 	} else if (sigio->sio_pgid < 0) {
3179831d27a9SDon Lewis 		struct proc *p;
3180831d27a9SDon Lewis 
3181f591779bSSeigo Tanimura 		PGRP_LOCK(sigio->sio_pgrp);
3182628d2653SJohn Baldwin 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3183628d2653SJohn Baldwin 			PROC_LOCK(p);
31842b87b6d4SRobert Watson 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3185831d27a9SDon Lewis 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
31862c42a146SMarcel Moolenaar 				psignal(p, sig);
3187628d2653SJohn Baldwin 			PROC_UNLOCK(p);
3188628d2653SJohn Baldwin 		}
3189f591779bSSeigo Tanimura 		PGRP_UNLOCK(sigio->sio_pgrp);
3190831d27a9SDon Lewis 	}
3191f1320723SAlfred Perlstein 	SIGIO_UNLOCK();
3192831d27a9SDon Lewis }
3193cb679c38SJonathan Lemon 
3194cb679c38SJonathan Lemon static int
3195cb679c38SJonathan Lemon filt_sigattach(struct knote *kn)
3196cb679c38SJonathan Lemon {
3197cb679c38SJonathan Lemon 	struct proc *p = curproc;
3198cb679c38SJonathan Lemon 
3199cb679c38SJonathan Lemon 	kn->kn_ptr.p_proc = p;
3200cb679c38SJonathan Lemon 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
3201cb679c38SJonathan Lemon 
3202ad3b9257SJohn-Mark Gurney 	knlist_add(&p->p_klist, kn, 0);
3203cb679c38SJonathan Lemon 
3204cb679c38SJonathan Lemon 	return (0);
3205cb679c38SJonathan Lemon }
3206cb679c38SJonathan Lemon 
3207cb679c38SJonathan Lemon static void
3208cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn)
3209cb679c38SJonathan Lemon {
3210cb679c38SJonathan Lemon 	struct proc *p = kn->kn_ptr.p_proc;
3211cb679c38SJonathan Lemon 
3212ad3b9257SJohn-Mark Gurney 	knlist_remove(&p->p_klist, kn, 0);
3213cb679c38SJonathan Lemon }
3214cb679c38SJonathan Lemon 
3215cb679c38SJonathan Lemon /*
3216cb679c38SJonathan Lemon  * signal knotes are shared with proc knotes, so we apply a mask to
3217cb679c38SJonathan Lemon  * the hint in order to differentiate them from process hints.  This
3218cb679c38SJonathan Lemon  * could be avoided by using a signal-specific knote list, but probably
3219cb679c38SJonathan Lemon  * isn't worth the trouble.
3220cb679c38SJonathan Lemon  */
3221cb679c38SJonathan Lemon static int
3222cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint)
3223cb679c38SJonathan Lemon {
3224cb679c38SJonathan Lemon 
3225cb679c38SJonathan Lemon 	if (hint & NOTE_SIGNAL) {
3226cb679c38SJonathan Lemon 		hint &= ~NOTE_SIGNAL;
3227cb679c38SJonathan Lemon 
3228cb679c38SJonathan Lemon 		if (kn->kn_id == hint)
3229cb679c38SJonathan Lemon 			kn->kn_data++;
3230cb679c38SJonathan Lemon 	}
3231cb679c38SJonathan Lemon 	return (kn->kn_data != 0);
3232cb679c38SJonathan Lemon }
323390af4afaSJohn Baldwin 
323490af4afaSJohn Baldwin struct sigacts *
323590af4afaSJohn Baldwin sigacts_alloc(void)
323690af4afaSJohn Baldwin {
323790af4afaSJohn Baldwin 	struct sigacts *ps;
323890af4afaSJohn Baldwin 
323990af4afaSJohn Baldwin 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
324090af4afaSJohn Baldwin 	ps->ps_refcnt = 1;
324190af4afaSJohn Baldwin 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
324290af4afaSJohn Baldwin 	return (ps);
324390af4afaSJohn Baldwin }
324490af4afaSJohn Baldwin 
324590af4afaSJohn Baldwin void
324690af4afaSJohn Baldwin sigacts_free(struct sigacts *ps)
324790af4afaSJohn Baldwin {
324890af4afaSJohn Baldwin 
324990af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
325090af4afaSJohn Baldwin 	ps->ps_refcnt--;
325190af4afaSJohn Baldwin 	if (ps->ps_refcnt == 0) {
325290af4afaSJohn Baldwin 		mtx_destroy(&ps->ps_mtx);
325390af4afaSJohn Baldwin 		free(ps, M_SUBPROC);
325490af4afaSJohn Baldwin 	} else
325590af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
325690af4afaSJohn Baldwin }
325790af4afaSJohn Baldwin 
325890af4afaSJohn Baldwin struct sigacts *
325990af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps)
326090af4afaSJohn Baldwin {
326190af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
326290af4afaSJohn Baldwin 	ps->ps_refcnt++;
326390af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
326490af4afaSJohn Baldwin 	return (ps);
326590af4afaSJohn Baldwin }
326690af4afaSJohn Baldwin 
326790af4afaSJohn Baldwin void
326890af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src)
326990af4afaSJohn Baldwin {
327090af4afaSJohn Baldwin 
327190af4afaSJohn Baldwin 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
327290af4afaSJohn Baldwin 	mtx_lock(&src->ps_mtx);
327390af4afaSJohn Baldwin 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
327490af4afaSJohn Baldwin 	mtx_unlock(&src->ps_mtx);
327590af4afaSJohn Baldwin }
327690af4afaSJohn Baldwin 
327790af4afaSJohn Baldwin int
327890af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps)
327990af4afaSJohn Baldwin {
328090af4afaSJohn Baldwin 	int shared;
328190af4afaSJohn Baldwin 
328290af4afaSJohn Baldwin 	mtx_lock(&ps->ps_mtx);
328390af4afaSJohn Baldwin 	shared = ps->ps_refcnt > 1;
328490af4afaSJohn Baldwin 	mtx_unlock(&ps->ps_mtx);
328590af4afaSJohn Baldwin 	return (shared);
328690af4afaSJohn Baldwin }
3287