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$
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 */
236199355Skib#define	KSI_SIGQ	0x08	/* Generated by sigqueue, might ret EGAIN. */
237202692Skib#define	KSI_HEAD	0x10	/* Insert into head, not tail. */
238199355Skib#define	KSI_COPYMASK	(KSI_TRAP|KSI_SIGQ)
239151307Sdavidxu
240151575Sdavidxu#define	KSI_ONQ(ksi)	((ksi)->ksi_sigq != NULL)
241151575Sdavidxu
242151316Sdavidxutypedef struct sigqueue {
243156213Sdavidxu	sigset_t	sq_signals;	/* All pending signals. */
244156213Sdavidxu	sigset_t	sq_kill;	/* Legacy depth 1 queue. */
245156213Sdavidxu	TAILQ_HEAD(, ksiginfo)	sq_list;/* Queued signal info. */
246151316Sdavidxu	struct proc	*sq_proc;
247151316Sdavidxu	int		sq_flags;
248151316Sdavidxu} sigqueue_t;
249151316Sdavidxu
250151316Sdavidxu/* Flags for ksi_flags */
251151316Sdavidxu#define	SQ_INIT	0x01
252151316Sdavidxu
25355205Speter#ifdef _KERNEL
25451791Smarcel
25593786Sbde/* Return nonzero if process p has an unmasked pending signal. */
256112888Sjeff#define	SIGPENDING(td)							\
257197963Skib	((!SIGISEMPTY((td)->td_siglist) &&				\
258197963Skib	    !sigsetmasked(&(td)->td_siglist, &(td)->td_sigmask)) ||	\
259197963Skib	 (!SIGISEMPTY((td)->td_proc->p_siglist) &&			\
260197963Skib	    !sigsetmasked(&(td)->td_proc->p_siglist, &(td)->td_sigmask)))
26193786Sbde/*
26293786Sbde * Return the value of the pseudo-expression ((*set & ~*mask) != 0).  This
26393786Sbde * is an optimized version of SIGISEMPTY() on a temporary variable
26493786Sbde * containing SIGSETNAND(*set, *mask).
26593786Sbde */
26693786Sbdestatic __inline int
26793786Sbdesigsetmasked(sigset_t *set, sigset_t *mask)
26893786Sbde{
26993786Sbde	int i;
27093786Sbde
27193786Sbde	for (i = 0; i < _SIG_WORDS; i++) {
27293786Sbde		if (set->__bits[i] & ~mask->__bits[i])
27393786Sbde			return (0);
27493786Sbde	}
27593786Sbde	return (1);
27693786Sbde}
27793786Sbde
278209819Sjhb#define	ksiginfo_init(ksi)			\
279151307Sdavidxudo {						\
280151307Sdavidxu	bzero(ksi, sizeof(ksiginfo_t));		\
281151307Sdavidxu} while(0)
282151307Sdavidxu
283209819Sjhb#define	ksiginfo_init_trap(ksi)			\
284151307Sdavidxudo {						\
285151307Sdavidxu	ksiginfo_t *kp = ksi;			\
286151307Sdavidxu	bzero(kp, sizeof(ksiginfo_t));		\
287151307Sdavidxu	kp->ksi_flags |= KSI_TRAP;		\
288151307Sdavidxu} while(0)
289151307Sdavidxu
290151307Sdavidxustatic __inline void
291151307Sdavidxuksiginfo_copy(ksiginfo_t *src, ksiginfo_t *dst)
292151307Sdavidxu{
293151307Sdavidxu	(dst)->ksi_info = src->ksi_info;
294151575Sdavidxu	(dst)->ksi_flags = (src->ksi_flags & KSI_COPYMASK);
295151307Sdavidxu}
296151307Sdavidxu
297213642Sdavidxustatic __inline void
298213642Sdavidxuksiginfo_set_sigev(ksiginfo_t *dst, struct sigevent *sigev)
299213642Sdavidxu{
300213642Sdavidxu	dst->ksi_signo = sigev->sigev_signo;
301213642Sdavidxu	dst->ksi_value = sigev->sigev_value;
302213642Sdavidxu}
303213642Sdavidxu
30433777Sbdestruct pgrp;
30534924Sbdestruct proc;
30641086Struckmanstruct sigio;
307209819Sjhbstruct thread;
30833777Sbde
3091541Srgrimes/*
31095759Stanimura * Lock the pointers for a sigio object in the underlying objects of
31195759Stanimura * a file descriptor.
31295759Stanimura */
313209819Sjhb#define	SIGIO_LOCK()	mtx_lock(&sigio_lock)
314209819Sjhb#define	SIGIO_TRYLOCK()	mtx_trylock(&sigio_lock)
315209819Sjhb#define	SIGIO_UNLOCK()	mtx_unlock(&sigio_lock)
316209819Sjhb#define	SIGIO_LOCKED()	mtx_owned(&sigio_lock)
317209819Sjhb#define	SIGIO_ASSERT(type)	mtx_assert(&sigio_lock, type)
31895759Stanimura
319209819Sjhbextern struct mtx	sigio_lock;
320209819Sjhb
321209819Sjhb/* Flags for kern_sigprocmask(). */
322198506Skib#define	SIGPROCMASK_OLD		0x0001
323198506Skib#define	SIGPROCMASK_PROC_LOCKED	0x0002
324198670Skib#define	SIGPROCMASK_PS_LOCKED	0x0004
325198506Skib
326248470Sjhbint	cursig(struct thread *td);
327247116Sjhbint	sigdeferstop(void);
328247116Sjhbvoid	sigallowstop(void);
32992719Salfredvoid	execsigs(struct proc *p);
330199355Skibvoid	gsignal(int pgid, int sig, ksiginfo_t *ksi);
33192719Salfredvoid	killproc(struct proc *p, char *why);
332209819Sjhbksiginfo_t * ksiginfo_alloc(int wait);
333209819Sjhbvoid	ksiginfo_free(ksiginfo_t *ksi);
334209592Sjhbint	pksignal(struct proc *p, int sig, ksiginfo_t *ksi);
335209819Sjhbvoid	pgsigio(struct sigio **sigiop, int sig, int checkctty);
336199355Skibvoid	pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi);
337199136Skibint	postsig(int sig);
338225617Skmacyvoid	kern_psignal(struct proc *p, int sig);
339209596Sjhbint	ptracestop(struct thread *td, int sig);
340209819Sjhbvoid	sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *retmask);
341114983Sjhbstruct sigacts *sigacts_alloc(void);
342114983Sjhbvoid	sigacts_copy(struct sigacts *dest, struct sigacts *src);
343114983Sjhbvoid	sigacts_free(struct sigacts *ps);
344114983Sjhbstruct sigacts *sigacts_hold(struct sigacts *ps);
345114983Sjhbint	sigacts_shared(struct sigacts *ps);
346209819Sjhbvoid	sigexit(struct thread *td, int sig) __dead2;
347213642Sdavidxuint	sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **);
348114325Sjhbint	sig_ffs(sigset_t *set);
34992719Salfredvoid	siginit(struct proc *p);
350112888Sjeffvoid	signotify(struct thread *td);
351209596Sjhbvoid	sigqueue_delete(struct sigqueue *queue, int sig);
352209596Sjhbvoid	sigqueue_delete_proc(struct proc *p, int sig);
353209596Sjhbvoid	sigqueue_flush(struct sigqueue *queue);
354209596Sjhbvoid	sigqueue_init(struct sigqueue *queue, struct proc *p);
355209596Sjhbvoid	sigqueue_take(ksiginfo_t *ksi);
356209596Sjhbvoid	tdksignal(struct thread *td, int sig, ksiginfo_t *ksi);
357213642Sdavidxuint	tdsendsignal(struct proc *p, struct thread *td, int sig,
358213642Sdavidxu	   ksiginfo_t *ksi);
359197963Skibvoid	tdsigcleanup(struct thread *td);
360209592Sjhbvoid	tdsignal(struct thread *td, int sig);
361209819Sjhbvoid	trapsignal(struct thread *td, ksiginfo_t *ksi);
362209613Sjhb
36365990Sbde#endif /* _KERNEL */
36451791Smarcel
36565990Sbde#endif /* !_SYS_SIGNALVAR_H_ */
366