1139825Simp/*-
21541Srgrimes * Copyright (c) 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2914502Shsu *	@(#)signalvar.h	8.6 (Berkeley) 2/19/95
3050477Speter * $FreeBSD: stable/10/sys/sys/signalvar.h 315949 2017-03-25 13:33:23Z badger $
311541Srgrimes */
321541Srgrimes
3365990Sbde#ifndef _SYS_SIGNALVAR_H_
341541Srgrimes#define	_SYS_SIGNALVAR_H_
351541Srgrimes
36114995Sjhb#include <sys/queue.h>
37114983Sjhb#include <sys/_lock.h>
38114983Sjhb#include <sys/_mutex.h>
3934924Sbde#include <sys/signal.h>
4034924Sbde
411541Srgrimes/*
42209819Sjhb * Kernel signal definitions and data structures.
431541Srgrimes */
441541Srgrimes
451541Srgrimes/*
46114983Sjhb * Logical process signal actions and state, needed only within the process
47114983Sjhb * The mapping between sigacts and proc structures is 1:1 except for rfork()
48114983Sjhb * processes masquerading as threads which use one structure for the whole
49114983Sjhb * group.  All members are locked by the included mutex.  The reference count
50114983Sjhb * and mutex must be last for the bcopy in sigacts_copy() to work.
511541Srgrimes */
5283045Sobrienstruct sigacts {
53114325Sjhb	sig_t	ps_sigact[_SIG_MAXSIG];	/* Disposition of signals. */
54114325Sjhb	sigset_t ps_catchmask[_SIG_MAXSIG];	/* Signals to be blocked. */
55114325Sjhb	sigset_t ps_sigonstack;		/* Signals to take on sigstack. */
56114325Sjhb	sigset_t ps_sigintr;		/* Signals that interrupt syscalls. */
57114325Sjhb	sigset_t ps_sigreset;		/* Signals that reset when caught. */
58114325Sjhb	sigset_t ps_signodefer;		/* Signals not masked while handled. */
59114325Sjhb	sigset_t ps_siginfo;		/* Signals that want SA_SIGINFO args. */
60114983Sjhb	sigset_t ps_sigignore;		/* Signals being ignored. */
61114983Sjhb	sigset_t ps_sigcatch;		/* Signals being caught by user. */
62209819Sjhb	sigset_t ps_freebsd4;		/* Signals using freebsd4 ucontext. */
63114325Sjhb	sigset_t ps_osigset;		/* Signals using <= 3.x osigset_t. */
64114983Sjhb	sigset_t ps_usertramp;		/* SunOS compat; libc sigtramp. XXX */
65114983Sjhb	int	ps_flag;
66270092Smjg	u_int	ps_refcnt;
67114983Sjhb	struct mtx ps_mtx;
681541Srgrimes};
691541Srgrimes
70114983Sjhb#define	PS_NOCLDWAIT	0x0001	/* No zombies if child dies */
71114983Sjhb#define	PS_NOCLDSTOP	0x0002	/* No SIGCHLD when children stop. */
72114983Sjhb#define	PS_CLDSIGIGN	0x0004	/* The SIGCHLD handler is SIG_IGN. */
73114983Sjhb
74209819Sjhb#ifdef _KERNEL
75209819Sjhb
76209819Sjhb#ifdef COMPAT_43
7751791Smarceltypedef struct {
7851791Smarcel	struct osigcontext si_sc;
7951791Smarcel	int		si_signo;
8051791Smarcel	int		si_code;
8151791Smarcel	union sigval	si_value;
8251791Smarcel} osiginfo_t;
831541Srgrimes
8483045Sobrienstruct osigaction {
8551791Smarcel	union {
8692719Salfred		void    (*__sa_handler)(int);
8792719Salfred		void    (*__sa_sigaction)(int, osiginfo_t *, void *);
8851791Smarcel	} __sigaction_u;		/* signal handler */
8951791Smarcel	osigset_t	sa_mask;	/* signal mask to apply */
9051791Smarcel	int		sa_flags;	/* see signal options below */
9151791Smarcel};
9251791Smarcel
9392719Salfredtypedef void __osiginfohandler_t(int, osiginfo_t *, void *);
94209819Sjhb#endif /* COMPAT_43 */
9551791Smarcel
9652160Smarcel/* additional signal action values, used only temporarily/internally */
9752160Smarcel#define	SIG_CATCH	((__sighandler_t *)2)
98199827Skib/* #define SIG_HOLD        ((__sighandler_t *)3) See signal.h */
991541Srgrimes
1001541Srgrimes/*
10151791Smarcel * get signal action for process and signal; currently only for current process
1021541Srgrimes */
103209819Sjhb#define	SIGACTION(p, sig)	(p->p_sigacts->ps_sigact[_SIG_IDX(sig)])
1041541Srgrimes
105209819Sjhb#endif /* _KERNEL */
106209819Sjhb
1071541Srgrimes/*
108209819Sjhb * sigset_t manipulation macros.
1091541Srgrimes */
110209819Sjhb#define	SIGADDSET(set, signo)						\
111105776Smarkm	((set).__bits[_SIG_WORD(signo)] |= _SIG_BIT(signo))
1121541Srgrimes
113209819Sjhb#define	SIGDELSET(set, signo)						\
114105776Smarkm	((set).__bits[_SIG_WORD(signo)] &= ~_SIG_BIT(signo))
1151541Srgrimes
116209819Sjhb#define	SIGEMPTYSET(set)						\
11751791Smarcel	do {								\
11851791Smarcel		int __i;						\
11951791Smarcel		for (__i = 0; __i < _SIG_WORDS; __i++)			\
12051791Smarcel			(set).__bits[__i] = 0;				\
12151791Smarcel	} while (0)
1221541Srgrimes
123209819Sjhb#define	SIGFILLSET(set)							\
12451791Smarcel	do {								\
12551791Smarcel		int __i;						\
12651791Smarcel		for (__i = 0; __i < _SIG_WORDS; __i++)			\
12765990Sbde			(set).__bits[__i] = ~0U;			\
12851791Smarcel	} while (0)
1291541Srgrimes
130209819Sjhb#define	SIGISMEMBER(set, signo)						\
13151791Smarcel	((set).__bits[_SIG_WORD(signo)] & _SIG_BIT(signo))
1321541Srgrimes
133209819Sjhb#define	SIGISEMPTY(set)		(__sigisempty(&(set)))
134209819Sjhb#define	SIGNOTEMPTY(set)	(!__sigisempty(&(set)))
13551791Smarcel
136209819Sjhb#define	SIGSETEQ(set1, set2)	(__sigseteq(&(set1), &(set2)))
137209819Sjhb#define	SIGSETNEQ(set1, set2)	(!__sigseteq(&(set1), &(set2)))
13851791Smarcel
139209819Sjhb#define	SIGSETOR(set1, set2)						\
14051791Smarcel	do {								\
14151791Smarcel		int __i;						\
14251791Smarcel		for (__i = 0; __i < _SIG_WORDS; __i++)			\
14351791Smarcel			(set1).__bits[__i] |= (set2).__bits[__i];	\
14451791Smarcel	} while (0)
14551791Smarcel
146209819Sjhb#define	SIGSETAND(set1, set2)						\
14751791Smarcel	do {								\
14851791Smarcel		int __i;						\
14951791Smarcel		for (__i = 0; __i < _SIG_WORDS; __i++)			\
15051791Smarcel			(set1).__bits[__i] &= (set2).__bits[__i];	\
15151791Smarcel	} while (0)
15251791Smarcel
153209819Sjhb#define	SIGSETNAND(set1, set2)						\
15451791Smarcel	do {								\
15551791Smarcel		int __i;						\
15651791Smarcel		for (__i = 0; __i < _SIG_WORDS; __i++)			\
15751791Smarcel			(set1).__bits[__i] &= ~(set2).__bits[__i];	\
15851791Smarcel	} while (0)
15951791Smarcel
160209819Sjhb#define	SIGSETLO(set1, set2)	((set1).__bits[0] = (set2).__bits[0])
161209819Sjhb#define	SIGSETOLD(set, oset)	((set).__bits[0] = (oset))
16252140Sluoqi
163209819Sjhb#define	SIG_CANTMASK(set)						\
16451791Smarcel	SIGDELSET(set, SIGKILL), SIGDELSET(set, SIGSTOP)
16551791Smarcel
166209819Sjhb#define	SIG_STOPSIGMASK(set)						\
16751791Smarcel	SIGDELSET(set, SIGSTOP), SIGDELSET(set, SIGTSTP),		\
16851791Smarcel	SIGDELSET(set, SIGTTIN), SIGDELSET(set, SIGTTOU)
16951791Smarcel
170209819Sjhb#define	SIG_CONTSIGMASK(set)						\
17151791Smarcel	SIGDELSET(set, SIGCONT)
17251791Smarcel
173209819Sjhb#define	sigcantmask	(sigmask(SIGKILL) | sigmask(SIGSTOP))
17451791Smarcel
175209819Sjhb#define	SIG2OSIG(sig, osig)	(osig = (sig).__bits[0])
176209819Sjhb#define	OSIG2SIG(osig, sig)	SIGEMPTYSET(sig); (sig).__bits[0] = osig
17751791Smarcel
17855584Sjasonestatic __inline int
17952160Smarcel__sigisempty(sigset_t *set)
18051791Smarcel{
18151791Smarcel	int i;
18251791Smarcel
18351791Smarcel	for (i = 0; i < _SIG_WORDS; i++) {
18451791Smarcel		if (set->__bits[i])
18551791Smarcel			return (0);
18651791Smarcel	}
18751791Smarcel	return (1);
18851791Smarcel}
18951791Smarcel
19055584Sjasonestatic __inline int
19152160Smarcel__sigseteq(sigset_t *set1, sigset_t *set2)
19251791Smarcel{
19351791Smarcel	int i;
19451791Smarcel
19551791Smarcel	for (i = 0; i < _SIG_WORDS; i++) {
19651791Smarcel		if (set1->__bits[i] != set2->__bits[i])
19751791Smarcel			return (0);
19851791Smarcel	}
19951791Smarcel	return (1);
20051791Smarcel}
20151791Smarcel
202151867Sdavidxustruct osigevent {
203151867Sdavidxu	int	sigev_notify;		/* Notification type */
204151867Sdavidxu	union {
205151867Sdavidxu		int	__sigev_signo;	/* Signal number */
206151867Sdavidxu		int	__sigev_notify_kqueue;
207151867Sdavidxu	} __sigev_u;
208151867Sdavidxu	union sigval sigev_value;	/* Signal value */
209151867Sdavidxu};
210151867Sdavidxu
211151307Sdavidxutypedef struct ksiginfo {
212151307Sdavidxu	TAILQ_ENTRY(ksiginfo)	ksi_link;
213151307Sdavidxu	siginfo_t		ksi_info;
214151307Sdavidxu	int			ksi_flags;
215151575Sdavidxu	struct sigqueue		*ksi_sigq;
216151307Sdavidxu} ksiginfo_t;
217151307Sdavidxu
218209819Sjhb#define	ksi_signo	ksi_info.si_signo
219209819Sjhb#define	ksi_errno	ksi_info.si_errno
220209819Sjhb#define	ksi_code	ksi_info.si_code
221209819Sjhb#define	ksi_pid		ksi_info.si_pid
222209819Sjhb#define	ksi_uid		ksi_info.si_uid
223209819Sjhb#define	ksi_status      ksi_info.si_status
224209819Sjhb#define	ksi_addr        ksi_info.si_addr
225209819Sjhb#define	ksi_value	ksi_info.si_value
226209819Sjhb#define	ksi_band	ksi_info.si_band
227209819Sjhb#define	ksi_trapno	ksi_info.si_trapno
228209819Sjhb#define	ksi_overrun	ksi_info.si_overrun
229209819Sjhb#define	ksi_timerid	ksi_info.si_timerid
230209819Sjhb#define	ksi_mqd		ksi_info.si_mqd
231151307Sdavidxu
232151307Sdavidxu/* bits for ksi_flags */
233209819Sjhb#define	KSI_TRAP	0x01	/* Generated by trap. */
234151575Sdavidxu#define	KSI_EXT		0x02	/* Externally managed ksi. */
235209819Sjhb#define	KSI_INS		0x04	/* Directly insert ksi, not the copy */
236315145Skib#define	KSI_SIGQ	0x08	/* Generated by sigqueue, might ret EAGAIN. */
237202692Skib#define	KSI_HEAD	0x10	/* Insert into head, not tail. */
238315949Sbadger#define	KSI_PTRACE	0x20	/* Generated by ptrace. */
239315949Sbadger#define	KSI_COPYMASK	(KSI_TRAP | KSI_SIGQ | KSI_PTRACE)
240151307Sdavidxu
241151575Sdavidxu#define	KSI_ONQ(ksi)	((ksi)->ksi_sigq != NULL)
242151575Sdavidxu
243151316Sdavidxutypedef struct sigqueue {
244156213Sdavidxu	sigset_t	sq_signals;	/* All pending signals. */
245156213Sdavidxu	sigset_t	sq_kill;	/* Legacy depth 1 queue. */
246315949Sbadger	sigset_t	sq_ptrace;	/* Depth 1 queue for ptrace(2). */
247156213Sdavidxu	TAILQ_HEAD(, ksiginfo)	sq_list;/* Queued signal info. */
248151316Sdavidxu	struct proc	*sq_proc;
249151316Sdavidxu	int		sq_flags;
250151316Sdavidxu} sigqueue_t;
251151316Sdavidxu
252151316Sdavidxu/* Flags for ksi_flags */
253151316Sdavidxu#define	SQ_INIT	0x01
254151316Sdavidxu
25555205Speter#ifdef _KERNEL
25651791Smarcel
25793786Sbde/* Return nonzero if process p has an unmasked pending signal. */
258112888Sjeff#define	SIGPENDING(td)							\
259197963Skib	((!SIGISEMPTY((td)->td_siglist) &&				\
260197963Skib	    !sigsetmasked(&(td)->td_siglist, &(td)->td_sigmask)) ||	\
261197963Skib	 (!SIGISEMPTY((td)->td_proc->p_siglist) &&			\
262197963Skib	    !sigsetmasked(&(td)->td_proc->p_siglist, &(td)->td_sigmask)))
26393786Sbde/*
26493786Sbde * Return the value of the pseudo-expression ((*set & ~*mask) != 0).  This
26593786Sbde * is an optimized version of SIGISEMPTY() on a temporary variable
26693786Sbde * containing SIGSETNAND(*set, *mask).
26793786Sbde */
26893786Sbdestatic __inline int
26993786Sbdesigsetmasked(sigset_t *set, sigset_t *mask)
27093786Sbde{
27193786Sbde	int i;
27293786Sbde
27393786Sbde	for (i = 0; i < _SIG_WORDS; i++) {
27493786Sbde		if (set->__bits[i] & ~mask->__bits[i])
27593786Sbde			return (0);
27693786Sbde	}
27793786Sbde	return (1);
27893786Sbde}
27993786Sbde
280209819Sjhb#define	ksiginfo_init(ksi)			\
281151307Sdavidxudo {						\
282151307Sdavidxu	bzero(ksi, sizeof(ksiginfo_t));		\
283151307Sdavidxu} while(0)
284151307Sdavidxu
285209819Sjhb#define	ksiginfo_init_trap(ksi)			\
286151307Sdavidxudo {						\
287151307Sdavidxu	ksiginfo_t *kp = ksi;			\
288151307Sdavidxu	bzero(kp, sizeof(ksiginfo_t));		\
289151307Sdavidxu	kp->ksi_flags |= KSI_TRAP;		\
290151307Sdavidxu} while(0)
291151307Sdavidxu
292151307Sdavidxustatic __inline void
293151307Sdavidxuksiginfo_copy(ksiginfo_t *src, ksiginfo_t *dst)
294151307Sdavidxu{
295151307Sdavidxu	(dst)->ksi_info = src->ksi_info;
296151575Sdavidxu	(dst)->ksi_flags = (src->ksi_flags & KSI_COPYMASK);
297151307Sdavidxu}
298151307Sdavidxu
299213642Sdavidxustatic __inline void
300213642Sdavidxuksiginfo_set_sigev(ksiginfo_t *dst, struct sigevent *sigev)
301213642Sdavidxu{
302213642Sdavidxu	dst->ksi_signo = sigev->sigev_signo;
303213642Sdavidxu	dst->ksi_value = sigev->sigev_value;
304213642Sdavidxu}
305213642Sdavidxu
30633777Sbdestruct pgrp;
30734924Sbdestruct proc;
30841086Struckmanstruct sigio;
309209819Sjhbstruct thread;
31033777Sbde
3111541Srgrimes/*
31295759Stanimura * Lock the pointers for a sigio object in the underlying objects of
31395759Stanimura * a file descriptor.
31495759Stanimura */
315209819Sjhb#define	SIGIO_LOCK()	mtx_lock(&sigio_lock)
316209819Sjhb#define	SIGIO_TRYLOCK()	mtx_trylock(&sigio_lock)
317209819Sjhb#define	SIGIO_UNLOCK()	mtx_unlock(&sigio_lock)
318209819Sjhb#define	SIGIO_LOCKED()	mtx_owned(&sigio_lock)
319209819Sjhb#define	SIGIO_ASSERT(type)	mtx_assert(&sigio_lock, type)
32095759Stanimura
321209819Sjhbextern struct mtx	sigio_lock;
322209819Sjhb
323209819Sjhb/* Flags for kern_sigprocmask(). */
324198506Skib#define	SIGPROCMASK_OLD		0x0001
325198506Skib#define	SIGPROCMASK_PROC_LOCKED	0x0002
326198670Skib#define	SIGPROCMASK_PS_LOCKED	0x0004
327198506Skib
328248470Sjhbint	cursig(struct thread *td);
329247116Sjhbint	sigdeferstop(void);
330277697Skibint	sigallowstop(void);
33192719Salfredvoid	execsigs(struct proc *p);
332199355Skibvoid	gsignal(int pgid, int sig, ksiginfo_t *ksi);
33392719Salfredvoid	killproc(struct proc *p, char *why);
334209819Sjhbksiginfo_t * ksiginfo_alloc(int wait);
335209819Sjhbvoid	ksiginfo_free(ksiginfo_t *ksi);
336209592Sjhbint	pksignal(struct proc *p, int sig, ksiginfo_t *ksi);
337209819Sjhbvoid	pgsigio(struct sigio **sigiop, int sig, int checkctty);
338199355Skibvoid	pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi);
339199136Skibint	postsig(int sig);
340225617Skmacyvoid	kern_psignal(struct proc *p, int sig);
341315949Sbadgerint	ptracestop(struct thread *td, int sig, ksiginfo_t *si);
342209819Sjhbvoid	sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *retmask);
343114983Sjhbstruct sigacts *sigacts_alloc(void);
344114983Sjhbvoid	sigacts_copy(struct sigacts *dest, struct sigacts *src);
345114983Sjhbvoid	sigacts_free(struct sigacts *ps);
346114983Sjhbstruct sigacts *sigacts_hold(struct sigacts *ps);
347114983Sjhbint	sigacts_shared(struct sigacts *ps);
348209819Sjhbvoid	sigexit(struct thread *td, int sig) __dead2;
349213642Sdavidxuint	sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **);
350114325Sjhbint	sig_ffs(sigset_t *set);
35192719Salfredvoid	siginit(struct proc *p);
352112888Sjeffvoid	signotify(struct thread *td);
353209596Sjhbvoid	sigqueue_delete(struct sigqueue *queue, int sig);
354209596Sjhbvoid	sigqueue_delete_proc(struct proc *p, int sig);
355209596Sjhbvoid	sigqueue_flush(struct sigqueue *queue);
356209596Sjhbvoid	sigqueue_init(struct sigqueue *queue, struct proc *p);
357209596Sjhbvoid	sigqueue_take(ksiginfo_t *ksi);
358209596Sjhbvoid	tdksignal(struct thread *td, int sig, ksiginfo_t *ksi);
359213642Sdavidxuint	tdsendsignal(struct proc *p, struct thread *td, int sig,
360213642Sdavidxu	   ksiginfo_t *ksi);
361197963Skibvoid	tdsigcleanup(struct thread *td);
362209592Sjhbvoid	tdsignal(struct thread *td, int sig);
363209819Sjhbvoid	trapsignal(struct thread *td, ksiginfo_t *ksi);
364209613Sjhb
36565990Sbde#endif /* _KERNEL */
36651791Smarcel
36765990Sbde#endif /* !_SYS_SIGNALVAR_H_ */
368