]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/signalvar.h
ping: use the monotonic clock to measure durations
[FreeBSD/FreeBSD.git] / sys / sys / signalvar.h
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      @(#)signalvar.h 8.6 (Berkeley) 2/19/95
32  * $FreeBSD$
33  */
34
35 #ifndef _SYS_SIGNALVAR_H_
36 #define _SYS_SIGNALVAR_H_
37
38 #include <sys/queue.h>
39 #include <sys/_lock.h>
40 #include <sys/_mutex.h>
41 #include <sys/signal.h>
42
43 /*
44  * Kernel signal definitions and data structures.
45  */
46
47 /*
48  * Logical process signal actions and state, needed only within the process
49  * The mapping between sigacts and proc structures is 1:1 except for rfork()
50  * processes masquerading as threads which use one structure for the whole
51  * group.  All members are locked by the included mutex.  The reference count
52  * and mutex must be last for the bcopy in sigacts_copy() to work.
53  */
54 struct sigacts {
55         sig_t   ps_sigact[_SIG_MAXSIG]; /* Disposition of signals. */
56         sigset_t ps_catchmask[_SIG_MAXSIG];     /* Signals to be blocked. */
57         sigset_t ps_sigonstack;         /* Signals to take on sigstack. */
58         sigset_t ps_sigintr;            /* Signals that interrupt syscalls. */
59         sigset_t ps_sigreset;           /* Signals that reset when caught. */
60         sigset_t ps_signodefer;         /* Signals not masked while handled. */
61         sigset_t ps_siginfo;            /* Signals that want SA_SIGINFO args. */
62         sigset_t ps_sigignore;          /* Signals being ignored. */
63         sigset_t ps_sigcatch;           /* Signals being caught by user. */
64         sigset_t ps_freebsd4;           /* Signals using freebsd4 ucontext. */
65         sigset_t ps_osigset;            /* Signals using <= 3.x osigset_t. */
66         sigset_t ps_usertramp;          /* SunOS compat; libc sigtramp. XXX */
67         int     ps_flag;
68         u_int   ps_refcnt;
69         struct mtx ps_mtx;
70 };
71
72 #define PS_NOCLDWAIT    0x0001  /* No zombies if child dies */
73 #define PS_NOCLDSTOP    0x0002  /* No SIGCHLD when children stop. */
74 #define PS_CLDSIGIGN    0x0004  /* The SIGCHLD handler is SIG_IGN. */
75
76 #ifdef _KERNEL
77
78 #ifdef COMPAT_43
79 typedef struct {
80         struct osigcontext si_sc;
81         int             si_signo;
82         int             si_code;
83         union sigval    si_value;
84 } osiginfo_t;
85
86 struct osigaction {
87         union {
88                 void    (*__sa_handler)(int);
89                 void    (*__sa_sigaction)(int, osiginfo_t *, void *);
90         } __sigaction_u;                /* signal handler */
91         osigset_t       sa_mask;        /* signal mask to apply */
92         int             sa_flags;       /* see signal options below */
93 };
94
95 typedef void __osiginfohandler_t(int, osiginfo_t *, void *);
96 #endif /* COMPAT_43 */
97
98 /* additional signal action values, used only temporarily/internally */
99 #define SIG_CATCH       ((__sighandler_t *)2)
100 /* #define SIG_HOLD        ((__sighandler_t *)3) See signal.h */
101
102 /*
103  * get signal action for process and signal; currently only for current process
104  */
105 #define SIGACTION(p, sig)       (p->p_sigacts->ps_sigact[_SIG_IDX(sig)])
106
107 #endif /* _KERNEL */
108
109 /*
110  * sigset_t manipulation macros.
111  */
112 #define SIGADDSET(set, signo)                                           \
113         ((set).__bits[_SIG_WORD(signo)] |= _SIG_BIT(signo))
114
115 #define SIGDELSET(set, signo)                                           \
116         ((set).__bits[_SIG_WORD(signo)] &= ~_SIG_BIT(signo))
117
118 #define SIGEMPTYSET(set)                                                \
119         do {                                                            \
120                 int __i;                                                \
121                 for (__i = 0; __i < _SIG_WORDS; __i++)                  \
122                         (set).__bits[__i] = 0;                          \
123         } while (0)
124
125 #define SIGFILLSET(set)                                                 \
126         do {                                                            \
127                 int __i;                                                \
128                 for (__i = 0; __i < _SIG_WORDS; __i++)                  \
129                         (set).__bits[__i] = ~0U;                        \
130         } while (0)
131
132 #define SIGISMEMBER(set, signo)                                         \
133         ((set).__bits[_SIG_WORD(signo)] & _SIG_BIT(signo))
134
135 #define SIGISEMPTY(set)         (__sigisempty(&(set)))
136 #define SIGNOTEMPTY(set)        (!__sigisempty(&(set)))
137
138 #define SIGSETEQ(set1, set2)    (__sigseteq(&(set1), &(set2)))
139 #define SIGSETNEQ(set1, set2)   (!__sigseteq(&(set1), &(set2)))
140
141 #define SIGSETOR(set1, set2)                                            \
142         do {                                                            \
143                 int __i;                                                \
144                 for (__i = 0; __i < _SIG_WORDS; __i++)                  \
145                         (set1).__bits[__i] |= (set2).__bits[__i];       \
146         } while (0)
147
148 #define SIGSETAND(set1, set2)                                           \
149         do {                                                            \
150                 int __i;                                                \
151                 for (__i = 0; __i < _SIG_WORDS; __i++)                  \
152                         (set1).__bits[__i] &= (set2).__bits[__i];       \
153         } while (0)
154
155 #define SIGSETNAND(set1, set2)                                          \
156         do {                                                            \
157                 int __i;                                                \
158                 for (__i = 0; __i < _SIG_WORDS; __i++)                  \
159                         (set1).__bits[__i] &= ~(set2).__bits[__i];      \
160         } while (0)
161
162 #define SIGSETLO(set1, set2)    ((set1).__bits[0] = (set2).__bits[0])
163 #define SIGSETOLD(set, oset)    ((set).__bits[0] = (oset))
164
165 #define SIG_CANTMASK(set)                                               \
166         SIGDELSET(set, SIGKILL), SIGDELSET(set, SIGSTOP)
167
168 #define SIG_STOPSIGMASK(set)                                            \
169         SIGDELSET(set, SIGSTOP), SIGDELSET(set, SIGTSTP),               \
170         SIGDELSET(set, SIGTTIN), SIGDELSET(set, SIGTTOU)
171
172 #define SIG_CONTSIGMASK(set)                                            \
173         SIGDELSET(set, SIGCONT)
174
175 #define sigcantmask     (sigmask(SIGKILL) | sigmask(SIGSTOP))
176
177 #define SIG2OSIG(sig, osig)     (osig = (sig).__bits[0])
178 #define OSIG2SIG(osig, sig)     SIGEMPTYSET(sig); (sig).__bits[0] = osig
179
180 static __inline int
181 __sigisempty(sigset_t *set)
182 {
183         int i;
184
185         for (i = 0; i < _SIG_WORDS; i++) {
186                 if (set->__bits[i])
187                         return (0);
188         }
189         return (1);
190 }
191
192 static __inline int
193 __sigseteq(sigset_t *set1, sigset_t *set2)
194 {
195         int i;
196
197         for (i = 0; i < _SIG_WORDS; i++) {
198                 if (set1->__bits[i] != set2->__bits[i])
199                         return (0);
200         }
201         return (1);
202 }
203
204 #ifdef COMPAT_FREEBSD6
205 struct osigevent {
206         int     sigev_notify;           /* Notification type */
207         union {
208                 int     __sigev_signo;  /* Signal number */
209                 int     __sigev_notify_kqueue;
210         } __sigev_u;
211         union sigval sigev_value;       /* Signal value */
212 };
213 #endif
214
215 typedef struct ksiginfo {
216         TAILQ_ENTRY(ksiginfo)   ksi_link;
217         siginfo_t               ksi_info;
218         int                     ksi_flags;
219         struct sigqueue         *ksi_sigq;
220 } ksiginfo_t;
221
222 #define ksi_signo       ksi_info.si_signo
223 #define ksi_errno       ksi_info.si_errno
224 #define ksi_code        ksi_info.si_code
225 #define ksi_pid         ksi_info.si_pid
226 #define ksi_uid         ksi_info.si_uid
227 #define ksi_status      ksi_info.si_status
228 #define ksi_addr        ksi_info.si_addr
229 #define ksi_value       ksi_info.si_value
230 #define ksi_band        ksi_info.si_band
231 #define ksi_trapno      ksi_info.si_trapno
232 #define ksi_overrun     ksi_info.si_overrun
233 #define ksi_timerid     ksi_info.si_timerid
234 #define ksi_mqd         ksi_info.si_mqd
235
236 /* bits for ksi_flags */
237 #define KSI_TRAP        0x01    /* Generated by trap. */
238 #define KSI_EXT         0x02    /* Externally managed ksi. */
239 #define KSI_INS         0x04    /* Directly insert ksi, not the copy */
240 #define KSI_SIGQ        0x08    /* Generated by sigqueue, might ret EAGAIN. */
241 #define KSI_HEAD        0x10    /* Insert into head, not tail. */
242 #define KSI_PTRACE      0x20    /* Generated by ptrace. */
243 #define KSI_COPYMASK    (KSI_TRAP | KSI_SIGQ | KSI_PTRACE)
244
245 #define KSI_ONQ(ksi)    ((ksi)->ksi_sigq != NULL)
246
247 typedef struct sigqueue {
248         sigset_t        sq_signals;     /* All pending signals. */
249         sigset_t        sq_kill;        /* Legacy depth 1 queue. */
250         sigset_t        sq_ptrace;      /* Depth 1 queue for ptrace(2). */
251         TAILQ_HEAD(, ksiginfo)  sq_list;/* Queued signal info. */
252         struct proc     *sq_proc;
253         int             sq_flags;
254 } sigqueue_t;
255
256 /* Flags for ksi_flags */
257 #define SQ_INIT 0x01
258
259 #ifdef _KERNEL
260
261 /* Return nonzero if process p has an unmasked pending signal. */
262 #define SIGPENDING(td)                                                  \
263         ((!SIGISEMPTY((td)->td_siglist) &&                              \
264             !sigsetmasked(&(td)->td_siglist, &(td)->td_sigmask)) ||     \
265          (!SIGISEMPTY((td)->td_proc->p_siglist) &&                      \
266             !sigsetmasked(&(td)->td_proc->p_siglist, &(td)->td_sigmask)))
267 /*
268  * Return the value of the pseudo-expression ((*set & ~*mask) != 0).  This
269  * is an optimized version of SIGISEMPTY() on a temporary variable
270  * containing SIGSETNAND(*set, *mask).
271  */
272 static __inline int
273 sigsetmasked(sigset_t *set, sigset_t *mask)
274 {
275         int i;
276
277         for (i = 0; i < _SIG_WORDS; i++) {
278                 if (set->__bits[i] & ~mask->__bits[i])
279                         return (0);
280         }
281         return (1);
282 }
283
284 #define ksiginfo_init(ksi)                      \
285 do {                                            \
286         bzero(ksi, sizeof(ksiginfo_t));         \
287 } while(0)
288
289 #define ksiginfo_init_trap(ksi)                 \
290 do {                                            \
291         ksiginfo_t *kp = ksi;                   \
292         bzero(kp, sizeof(ksiginfo_t));          \
293         kp->ksi_flags |= KSI_TRAP;              \
294 } while(0)
295
296 static __inline void
297 ksiginfo_copy(ksiginfo_t *src, ksiginfo_t *dst)
298 {
299         (dst)->ksi_info = src->ksi_info;
300         (dst)->ksi_flags = (src->ksi_flags & KSI_COPYMASK);
301 }
302
303 static __inline void
304 ksiginfo_set_sigev(ksiginfo_t *dst, struct sigevent *sigev)
305 {
306         dst->ksi_signo = sigev->sigev_signo;
307         dst->ksi_value = sigev->sigev_value;
308 }
309
310 struct pgrp;
311 struct proc;
312 struct sigio;
313 struct thread;
314
315 /*
316  * Lock the pointers for a sigio object in the underlying objects of
317  * a file descriptor.
318  */
319 #define SIGIO_LOCK()    mtx_lock(&sigio_lock)
320 #define SIGIO_TRYLOCK() mtx_trylock(&sigio_lock)
321 #define SIGIO_UNLOCK()  mtx_unlock(&sigio_lock)
322 #define SIGIO_LOCKED()  mtx_owned(&sigio_lock)
323 #define SIGIO_ASSERT(type)      mtx_assert(&sigio_lock, type)
324
325 extern struct mtx       sigio_lock;
326
327 /* Flags for kern_sigprocmask(). */
328 #define SIGPROCMASK_OLD         0x0001
329 #define SIGPROCMASK_PROC_LOCKED 0x0002
330 #define SIGPROCMASK_PS_LOCKED   0x0004
331
332 /*
333  * Modes for sigdeferstop().  Manages behaviour of
334  * thread_suspend_check() in the region delimited by
335  * sigdeferstop()/sigallowstop().  Must be restored to
336  * SIGDEFERSTOP_OFF before returning to userspace.
337  */
338 #define SIGDEFERSTOP_NOP        0 /* continue doing whatever is done now */
339 #define SIGDEFERSTOP_OFF        1 /* stop ignoring STOPs */
340 #define SIGDEFERSTOP_SILENT     2 /* silently ignore STOPs */
341 #define SIGDEFERSTOP_EINTR      3 /* ignore STOPs, return EINTR */
342 #define SIGDEFERSTOP_ERESTART   4 /* ignore STOPs, return ERESTART */
343
344 #define SIGDEFERSTOP_VAL_NCHG   (-1) /* placeholder indicating no state change */
345 int     sigdeferstop_impl(int mode);
346 void    sigallowstop_impl(int prev);
347
348 static inline int
349 sigdeferstop(int mode)
350 {
351
352         if (__predict_true(mode == SIGDEFERSTOP_NOP))
353                 return (SIGDEFERSTOP_VAL_NCHG);
354         return (sigdeferstop_impl(mode));
355 }
356
357 static inline void
358 sigallowstop(int prev)
359 {
360
361         if (__predict_true(prev == SIGDEFERSTOP_VAL_NCHG))
362                 return;
363         sigallowstop_impl(prev);
364 }
365
366 int     cursig(struct thread *td);
367 void    execsigs(struct proc *p);
368 void    gsignal(int pgid, int sig, ksiginfo_t *ksi);
369 void    killproc(struct proc *p, char *why);
370 ksiginfo_t * ksiginfo_alloc(int wait);
371 void    ksiginfo_free(ksiginfo_t *ksi);
372 int     pksignal(struct proc *p, int sig, ksiginfo_t *ksi);
373 void    pgsigio(struct sigio **sigiop, int sig, int checkctty);
374 void    pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi);
375 int     postsig(int sig);
376 void    kern_psignal(struct proc *p, int sig);
377 int     ptracestop(struct thread *td, int sig, ksiginfo_t *si);
378 void    sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *retmask);
379 struct sigacts *sigacts_alloc(void);
380 void    sigacts_copy(struct sigacts *dest, struct sigacts *src);
381 void    sigacts_free(struct sigacts *ps);
382 struct sigacts *sigacts_hold(struct sigacts *ps);
383 int     sigacts_shared(struct sigacts *ps);
384 void    sigexit(struct thread *td, int sig) __dead2;
385 int     sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **);
386 int     sig_ffs(sigset_t *set);
387 void    siginit(struct proc *p);
388 void    signotify(struct thread *td);
389 void    sigqueue_delete(struct sigqueue *queue, int sig);
390 void    sigqueue_delete_proc(struct proc *p, int sig);
391 void    sigqueue_flush(struct sigqueue *queue);
392 void    sigqueue_init(struct sigqueue *queue, struct proc *p);
393 void    sigqueue_take(ksiginfo_t *ksi);
394 void    tdksignal(struct thread *td, int sig, ksiginfo_t *ksi);
395 int     tdsendsignal(struct proc *p, struct thread *td, int sig,
396            ksiginfo_t *ksi);
397 void    tdsigcleanup(struct thread *td);
398 void    tdsignal(struct thread *td, int sig);
399 void    trapsignal(struct thread *td, ksiginfo_t *ksi);
400
401 #endif /* _KERNEL */
402
403 #endif /* !_SYS_SIGNALVAR_H_ */