blob: a44e7f0622384208aa342970b05e52ac47db4312 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_SIGNAL_H
2#define _LINUX_SIGNAL_H
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <asm/signal.h>
5#include <asm/siginfo.h>
6
7#ifdef __KERNEL__
David Woodhouse7ab2feb2006-04-25 14:55:46 +01008#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Dave Youngd33ed522010-03-10 15:23:59 -080010/* for sysctl */
11extern int print_fatal_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Real Time signals may be queued.
14 */
15
16struct sigqueue {
17 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 int flags;
19 siginfo_t info;
20 struct user_struct *user;
21};
22
23/* flags values. */
24#define SIGQUEUE_PREALLOC 1
25
26struct sigpending {
27 struct list_head list;
28 sigset_t signal;
29};
30
31/*
32 * Define some primitives to manipulate sigset_t.
33 */
34
35#ifndef __HAVE_ARCH_SIG_BITOPS
36#include <linux/bitops.h>
37
38/* We don't use <linux/bitops.h> for these because there is no need to
39 be atomic. */
40static inline void sigaddset(sigset_t *set, int _sig)
41{
42 unsigned long sig = _sig - 1;
43 if (_NSIG_WORDS == 1)
44 set->sig[0] |= 1UL << sig;
45 else
46 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
47}
48
49static inline void sigdelset(sigset_t *set, int _sig)
50{
51 unsigned long sig = _sig - 1;
52 if (_NSIG_WORDS == 1)
53 set->sig[0] &= ~(1UL << sig);
54 else
55 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
56}
57
58static inline int sigismember(sigset_t *set, int _sig)
59{
60 unsigned long sig = _sig - 1;
61 if (_NSIG_WORDS == 1)
62 return 1 & (set->sig[0] >> sig);
63 else
64 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
65}
66
67static inline int sigfindinword(unsigned long word)
68{
69 return ffz(~word);
70}
71
72#endif /* __HAVE_ARCH_SIG_BITOPS */
73
George Anzinger71fabd52006-01-08 01:02:48 -080074static inline int sigisemptyset(sigset_t *set)
75{
76 extern void _NSIG_WORDS_is_unsupported_size(void);
77 switch (_NSIG_WORDS) {
78 case 4:
79 return (set->sig[3] | set->sig[2] |
80 set->sig[1] | set->sig[0]) == 0;
81 case 2:
82 return (set->sig[1] | set->sig[0]) == 0;
83 case 1:
84 return set->sig[0] == 0;
85 default:
86 _NSIG_WORDS_is_unsupported_size();
87 return 0;
88 }
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define sigmask(sig) (1UL << ((sig) - 1))
92
93#ifndef __HAVE_ARCH_SIG_SETOPS
94#include <linux/string.h>
95
96#define _SIG_SET_BINOP(name, op) \
97static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
98{ \
99 extern void _NSIG_WORDS_is_unsupported_size(void); \
100 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
101 \
102 switch (_NSIG_WORDS) { \
103 case 4: \
104 a3 = a->sig[3]; a2 = a->sig[2]; \
105 b3 = b->sig[3]; b2 = b->sig[2]; \
106 r->sig[3] = op(a3, b3); \
107 r->sig[2] = op(a2, b2); \
108 case 2: \
109 a1 = a->sig[1]; b1 = b->sig[1]; \
110 r->sig[1] = op(a1, b1); \
111 case 1: \
112 a0 = a->sig[0]; b0 = b->sig[0]; \
113 r->sig[0] = op(a0, b0); \
114 break; \
115 default: \
116 _NSIG_WORDS_is_unsupported_size(); \
117 } \
118}
119
120#define _sig_or(x,y) ((x) | (y))
121_SIG_SET_BINOP(sigorsets, _sig_or)
122
123#define _sig_and(x,y) ((x) & (y))
124_SIG_SET_BINOP(sigandsets, _sig_and)
125
Oleg Nesterov702a5072011-04-27 22:01:27 +0200126#define _sig_andn(x,y) ((x) & ~(y))
127_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129#undef _SIG_SET_BINOP
130#undef _sig_or
131#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200132#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134#define _SIG_SET_OP(name, op) \
135static inline void name(sigset_t *set) \
136{ \
137 extern void _NSIG_WORDS_is_unsupported_size(void); \
138 \
139 switch (_NSIG_WORDS) { \
140 case 4: set->sig[3] = op(set->sig[3]); \
141 set->sig[2] = op(set->sig[2]); \
142 case 2: set->sig[1] = op(set->sig[1]); \
143 case 1: set->sig[0] = op(set->sig[0]); \
144 break; \
145 default: \
146 _NSIG_WORDS_is_unsupported_size(); \
147 } \
148}
149
150#define _sig_not(x) (~(x))
151_SIG_SET_OP(signotset, _sig_not)
152
153#undef _SIG_SET_OP
154#undef _sig_not
155
156static inline void sigemptyset(sigset_t *set)
157{
158 switch (_NSIG_WORDS) {
159 default:
160 memset(set, 0, sizeof(sigset_t));
161 break;
162 case 2: set->sig[1] = 0;
163 case 1: set->sig[0] = 0;
164 break;
165 }
166}
167
168static inline void sigfillset(sigset_t *set)
169{
170 switch (_NSIG_WORDS) {
171 default:
172 memset(set, -1, sizeof(sigset_t));
173 break;
174 case 2: set->sig[1] = -1;
175 case 1: set->sig[0] = -1;
176 break;
177 }
178}
179
180/* Some extensions for manipulating the low 32 signals in particular. */
181
182static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
183{
184 set->sig[0] |= mask;
185}
186
187static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
188{
189 set->sig[0] &= ~mask;
190}
191
192static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
193{
194 return (set->sig[0] & mask) != 0;
195}
196
197static inline void siginitset(sigset_t *set, unsigned long mask)
198{
199 set->sig[0] = mask;
200 switch (_NSIG_WORDS) {
201 default:
202 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
203 break;
204 case 2: set->sig[1] = 0;
205 case 1: ;
206 }
207}
208
209static inline void siginitsetinv(sigset_t *set, unsigned long mask)
210{
211 set->sig[0] = ~mask;
212 switch (_NSIG_WORDS) {
213 default:
214 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
215 break;
216 case 2: set->sig[1] = -1;
217 case 1: ;
218 }
219}
220
221#endif /* __HAVE_ARCH_SIG_SETOPS */
222
223static inline void init_sigpending(struct sigpending *sig)
224{
225 sigemptyset(&sig->signal);
226 INIT_LIST_HEAD(&sig->list);
227}
228
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800229extern void flush_sigqueue(struct sigpending *queue);
230
Jesper Juhle5bdd882005-05-01 08:59:13 -0700231/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
232static inline int valid_signal(unsigned long sig)
233{
234 return sig <= _NSIG ? 1 : 0;
235}
236
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200237struct timespec;
238struct pt_regs;
239
Davide Libenzifba2afa2007-05-10 22:23:13 -0700240extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700241extern int do_send_sig_info(int sig, struct siginfo *info,
242 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
244extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Thomas Gleixner62ab4502009-04-04 21:01:06 +0000245extern long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig,
246 siginfo_t *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247extern long do_sigpending(void __user *, unsigned long);
Oleg Nesterov943df142011-04-27 21:44:14 +0200248extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
249 const struct timespec *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250extern int sigprocmask(int, sigset_t *, sigset_t *);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +0200251extern void set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200252extern int show_unhandled_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254extern int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka, struct pt_regs *regs, void *cookie);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800255extern void exit_signals(struct task_struct *tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800257extern struct kmem_cache *sighand_cachep;
258
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200259int unhandled_signal(struct task_struct *tsk, int sig);
260
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700261/*
262 * In POSIX a signal is sent either to a specific thread (Linux task)
263 * or to the process as a whole (Linux thread group). How the signal
264 * is sent determines whether it's to one thread or the whole group,
265 * which determines which signal mask(s) are involved in blocking it
266 * from being delivered until later. When the signal is delivered,
267 * either it's caught or ignored by a user handler or it has a default
268 * effect that applies to the whole thread group (POSIX process).
269 *
270 * The possible effects an unblocked signal set to SIG_DFL can have are:
271 * ignore - Nothing Happens
272 * terminate - kill the process, i.e. all threads in the group,
273 * similar to exit_group. The group leader (only) reports
274 * WIFSIGNALED status to its parent.
275 * coredump - write a core dump file describing all threads using
276 * the same mm and then kill all those threads
277 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
278 *
279 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
280 * Other signals when not blocked and set to SIG_DFL behaves as follows.
281 * The job control signals also have other special effects.
282 *
283 * +--------------------+------------------+
284 * | POSIX signal | default action |
285 * +--------------------+------------------+
286 * | SIGHUP | terminate |
287 * | SIGINT | terminate |
288 * | SIGQUIT | coredump |
289 * | SIGILL | coredump |
290 * | SIGTRAP | coredump |
291 * | SIGABRT/SIGIOT | coredump |
292 * | SIGBUS | coredump |
293 * | SIGFPE | coredump |
294 * | SIGKILL | terminate(+) |
295 * | SIGUSR1 | terminate |
296 * | SIGSEGV | coredump |
297 * | SIGUSR2 | terminate |
298 * | SIGPIPE | terminate |
299 * | SIGALRM | terminate |
300 * | SIGTERM | terminate |
301 * | SIGCHLD | ignore |
302 * | SIGCONT | ignore(*) |
303 * | SIGSTOP | stop(*)(+) |
304 * | SIGTSTP | stop(*) |
305 * | SIGTTIN | stop(*) |
306 * | SIGTTOU | stop(*) |
307 * | SIGURG | ignore |
308 * | SIGXCPU | coredump |
309 * | SIGXFSZ | coredump |
310 * | SIGVTALRM | terminate |
311 * | SIGPROF | terminate |
312 * | SIGPOLL/SIGIO | terminate |
313 * | SIGSYS/SIGUNUSED | coredump |
314 * | SIGSTKFLT | terminate |
315 * | SIGWINCH | ignore |
316 * | SIGPWR | terminate |
317 * | SIGRTMIN-SIGRTMAX | terminate |
318 * +--------------------+------------------+
319 * | non-POSIX signal | default action |
320 * +--------------------+------------------+
321 * | SIGEMT | coredump |
322 * +--------------------+------------------+
323 *
324 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
325 * (*) Special job control effects:
326 * When SIGCONT is sent, it resumes the process (all threads in the group)
327 * from TASK_STOPPED state and also clears any pending/queued stop signals
328 * (any of those marked with "stop(*)"). This happens regardless of blocking,
329 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
330 * any pending/queued SIGCONT signals; this happens regardless of blocking,
331 * catching, or ignored the stop signal, though (except for SIGSTOP) the
332 * default action of stopping the process may happen later or never.
333 */
334
335#ifdef SIGEMT
336#define SIGEMT_MASK rt_sigmask(SIGEMT)
337#else
338#define SIGEMT_MASK 0
339#endif
340
341#if SIGRTMIN > BITS_PER_LONG
342#define rt_sigmask(sig) (1ULL << ((sig)-1))
343#else
344#define rt_sigmask(sig) sigmask(sig)
345#endif
346#define siginmask(sig, mask) (rt_sigmask(sig) & (mask))
347
348#define SIG_KERNEL_ONLY_MASK (\
349 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
350
351#define SIG_KERNEL_STOP_MASK (\
352 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
353 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
354
355#define SIG_KERNEL_COREDUMP_MASK (\
356 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
357 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
358 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
359 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
360 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
361 SIGEMT_MASK )
362
363#define SIG_KERNEL_IGNORE_MASK (\
364 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
365 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
366
367#define sig_kernel_only(sig) \
368 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_ONLY_MASK))
369#define sig_kernel_coredump(sig) \
370 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_COREDUMP_MASK))
371#define sig_kernel_ignore(sig) \
372 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_IGNORE_MASK))
373#define sig_kernel_stop(sig) \
374 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_STOP_MASK))
375
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700376#define sig_user_defined(t, signr) \
377 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
378 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
379
380#define sig_fatal(t, signr) \
381 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
382 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
383
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800384void signals_init(void);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#endif /* __KERNEL__ */
387
388#endif /* _LINUX_SIGNAL_H */