blob: 750196fcc0a5144aebb1df7f67102502c871a990 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_SIGNAL_H
2#define _LINUX_SIGNAL_H
3
David Woodhouse7ab2feb2006-04-25 14:55:46 +01004#include <linux/list.h>
David Howells607ca462012-10-13 10:46:48 +01005#include <uapi/linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Stephen Rothwell1477fcc2011-05-20 11:11:53 +10007struct task_struct;
8
Dave Youngd33ed522010-03-10 15:23:59 -08009/* for sysctl */
10extern int print_fatal_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Real Time signals may be queued.
13 */
14
15struct sigqueue {
16 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 int flags;
18 siginfo_t info;
19 struct user_struct *user;
20};
21
22/* flags values. */
23#define SIGQUEUE_PREALLOC 1
24
25struct sigpending {
26 struct list_head list;
27 sigset_t signal;
28};
29
30/*
31 * Define some primitives to manipulate sigset_t.
32 */
33
34#ifndef __HAVE_ARCH_SIG_BITOPS
35#include <linux/bitops.h>
36
37/* We don't use <linux/bitops.h> for these because there is no need to
38 be atomic. */
39static inline void sigaddset(sigset_t *set, int _sig)
40{
41 unsigned long sig = _sig - 1;
42 if (_NSIG_WORDS == 1)
43 set->sig[0] |= 1UL << sig;
44 else
45 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
46}
47
48static inline void sigdelset(sigset_t *set, int _sig)
49{
50 unsigned long sig = _sig - 1;
51 if (_NSIG_WORDS == 1)
52 set->sig[0] &= ~(1UL << sig);
53 else
54 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
55}
56
57static inline int sigismember(sigset_t *set, int _sig)
58{
59 unsigned long sig = _sig - 1;
60 if (_NSIG_WORDS == 1)
61 return 1 & (set->sig[0] >> sig);
62 else
63 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif /* __HAVE_ARCH_SIG_BITOPS */
67
George Anzinger71fabd52006-01-08 01:02:48 -080068static inline int sigisemptyset(sigset_t *set)
69{
70 extern void _NSIG_WORDS_is_unsupported_size(void);
71 switch (_NSIG_WORDS) {
72 case 4:
73 return (set->sig[3] | set->sig[2] |
74 set->sig[1] | set->sig[0]) == 0;
75 case 2:
76 return (set->sig[1] | set->sig[0]) == 0;
77 case 1:
78 return set->sig[0] == 0;
79 default:
80 _NSIG_WORDS_is_unsupported_size();
81 return 0;
82 }
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define sigmask(sig) (1UL << ((sig) - 1))
86
87#ifndef __HAVE_ARCH_SIG_SETOPS
88#include <linux/string.h>
89
90#define _SIG_SET_BINOP(name, op) \
91static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
92{ \
93 extern void _NSIG_WORDS_is_unsupported_size(void); \
94 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
95 \
96 switch (_NSIG_WORDS) { \
97 case 4: \
98 a3 = a->sig[3]; a2 = a->sig[2]; \
99 b3 = b->sig[3]; b2 = b->sig[2]; \
100 r->sig[3] = op(a3, b3); \
101 r->sig[2] = op(a2, b2); \
102 case 2: \
103 a1 = a->sig[1]; b1 = b->sig[1]; \
104 r->sig[1] = op(a1, b1); \
105 case 1: \
106 a0 = a->sig[0]; b0 = b->sig[0]; \
107 r->sig[0] = op(a0, b0); \
108 break; \
109 default: \
110 _NSIG_WORDS_is_unsupported_size(); \
111 } \
112}
113
114#define _sig_or(x,y) ((x) | (y))
115_SIG_SET_BINOP(sigorsets, _sig_or)
116
117#define _sig_and(x,y) ((x) & (y))
118_SIG_SET_BINOP(sigandsets, _sig_and)
119
Oleg Nesterov702a5072011-04-27 22:01:27 +0200120#define _sig_andn(x,y) ((x) & ~(y))
121_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123#undef _SIG_SET_BINOP
124#undef _sig_or
125#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200126#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128#define _SIG_SET_OP(name, op) \
129static inline void name(sigset_t *set) \
130{ \
131 extern void _NSIG_WORDS_is_unsupported_size(void); \
132 \
133 switch (_NSIG_WORDS) { \
134 case 4: set->sig[3] = op(set->sig[3]); \
135 set->sig[2] = op(set->sig[2]); \
136 case 2: set->sig[1] = op(set->sig[1]); \
137 case 1: set->sig[0] = op(set->sig[0]); \
138 break; \
139 default: \
140 _NSIG_WORDS_is_unsupported_size(); \
141 } \
142}
143
144#define _sig_not(x) (~(x))
145_SIG_SET_OP(signotset, _sig_not)
146
147#undef _SIG_SET_OP
148#undef _sig_not
149
150static inline void sigemptyset(sigset_t *set)
151{
152 switch (_NSIG_WORDS) {
153 default:
154 memset(set, 0, sizeof(sigset_t));
155 break;
156 case 2: set->sig[1] = 0;
157 case 1: set->sig[0] = 0;
158 break;
159 }
160}
161
162static inline void sigfillset(sigset_t *set)
163{
164 switch (_NSIG_WORDS) {
165 default:
166 memset(set, -1, sizeof(sigset_t));
167 break;
168 case 2: set->sig[1] = -1;
169 case 1: set->sig[0] = -1;
170 break;
171 }
172}
173
174/* Some extensions for manipulating the low 32 signals in particular. */
175
176static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
177{
178 set->sig[0] |= mask;
179}
180
181static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
182{
183 set->sig[0] &= ~mask;
184}
185
186static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
187{
188 return (set->sig[0] & mask) != 0;
189}
190
191static inline void siginitset(sigset_t *set, unsigned long mask)
192{
193 set->sig[0] = mask;
194 switch (_NSIG_WORDS) {
195 default:
196 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
197 break;
198 case 2: set->sig[1] = 0;
199 case 1: ;
200 }
201}
202
203static inline void siginitsetinv(sigset_t *set, unsigned long mask)
204{
205 set->sig[0] = ~mask;
206 switch (_NSIG_WORDS) {
207 default:
208 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
209 break;
210 case 2: set->sig[1] = -1;
211 case 1: ;
212 }
213}
214
215#endif /* __HAVE_ARCH_SIG_SETOPS */
216
217static inline void init_sigpending(struct sigpending *sig)
218{
219 sigemptyset(&sig->signal);
220 INIT_LIST_HEAD(&sig->list);
221}
222
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800223extern void flush_sigqueue(struct sigpending *queue);
224
Jesper Juhle5bdd882005-05-01 08:59:13 -0700225/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
226static inline int valid_signal(unsigned long sig)
227{
228 return sig <= _NSIG ? 1 : 0;
229}
230
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200231struct timespec;
232struct pt_regs;
233
Davide Libenzifba2afa2007-05-10 22:23:13 -0700234extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700235extern int do_send_sig_info(int sig, struct siginfo *info,
236 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
238extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Oleg Nesterov943df142011-04-27 21:44:14 +0200239extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
240 const struct timespec *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241extern int sigprocmask(int, sigset_t *, sigset_t *);
Al Viro77097ae2012-04-27 13:58:59 -0400242extern void set_current_blocked(sigset_t *);
243extern void __set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200244extern int show_unhandled_signals;
Al Viro68f3f162012-05-21 21:42:32 -0400245extern int sigsuspend(sigset_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Al Viro574c4862012-11-25 22:24:19 -0500247struct sigaction {
David Howells2a148692013-03-19 14:00:53 +0000248#ifndef __ARCH_HAS_IRIX_SIGACTION
Al Viro574c4862012-11-25 22:24:19 -0500249 __sighandler_t sa_handler;
250 unsigned long sa_flags;
251#else
David Howells2a148692013-03-19 14:00:53 +0000252 unsigned int sa_flags;
Al Viro574c4862012-11-25 22:24:19 -0500253 __sighandler_t sa_handler;
254#endif
255#ifdef __ARCH_HAS_SA_RESTORER
256 __sigrestore_t sa_restorer;
257#endif
258 sigset_t sa_mask; /* mask last for extensibility */
259};
260
Al Viro92a3ce42012-11-25 21:20:05 -0500261struct k_sigaction {
262 struct sigaction sa;
263#ifdef __ARCH_HAS_KA_RESTORER
264 __sigrestore_t ka_restorer;
265#endif
266};
Al Viro495dfbf2012-12-25 19:09:45 -0500267
268#ifdef CONFIG_OLD_SIGACTION
269struct old_sigaction {
270 __sighandler_t sa_handler;
271 old_sigset_t sa_mask;
272 unsigned long sa_flags;
273 __sigrestore_t sa_restorer;
274};
275#endif
Al Viro92a3ce42012-11-25 21:20:05 -0500276
Al Viroca86b5d2012-11-07 15:09:38 -0500277struct ksignal {
278 struct k_sigaction ka;
279 siginfo_t info;
280 int sig;
281};
282
Richard Weinberger828b1f62013-10-07 15:26:57 +0200283extern int get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500284extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800285extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700286extern void kernel_sigaction(int, __sighandler_t);
287
288static inline void allow_signal(int sig)
289{
290 /*
291 * Kernel threads handle their own signals. Let the signal code
292 * know it'll be handled, so that they don't get converted to
293 * SIGKILL or just silently dropped.
294 */
295 kernel_sigaction(sig, (__force __sighandler_t)2);
296}
297
298static inline void disallow_signal(int sig)
299{
300 kernel_sigaction(sig, SIG_IGN);
301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800303extern struct kmem_cache *sighand_cachep;
304
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200305int unhandled_signal(struct task_struct *tsk, int sig);
306
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700307/*
308 * In POSIX a signal is sent either to a specific thread (Linux task)
309 * or to the process as a whole (Linux thread group). How the signal
310 * is sent determines whether it's to one thread or the whole group,
311 * which determines which signal mask(s) are involved in blocking it
312 * from being delivered until later. When the signal is delivered,
313 * either it's caught or ignored by a user handler or it has a default
314 * effect that applies to the whole thread group (POSIX process).
315 *
316 * The possible effects an unblocked signal set to SIG_DFL can have are:
317 * ignore - Nothing Happens
318 * terminate - kill the process, i.e. all threads in the group,
319 * similar to exit_group. The group leader (only) reports
320 * WIFSIGNALED status to its parent.
321 * coredump - write a core dump file describing all threads using
322 * the same mm and then kill all those threads
323 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
324 *
325 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
326 * Other signals when not blocked and set to SIG_DFL behaves as follows.
327 * The job control signals also have other special effects.
328 *
329 * +--------------------+------------------+
330 * | POSIX signal | default action |
331 * +--------------------+------------------+
332 * | SIGHUP | terminate |
333 * | SIGINT | terminate |
334 * | SIGQUIT | coredump |
335 * | SIGILL | coredump |
336 * | SIGTRAP | coredump |
337 * | SIGABRT/SIGIOT | coredump |
338 * | SIGBUS | coredump |
339 * | SIGFPE | coredump |
340 * | SIGKILL | terminate(+) |
341 * | SIGUSR1 | terminate |
342 * | SIGSEGV | coredump |
343 * | SIGUSR2 | terminate |
344 * | SIGPIPE | terminate |
345 * | SIGALRM | terminate |
346 * | SIGTERM | terminate |
347 * | SIGCHLD | ignore |
348 * | SIGCONT | ignore(*) |
349 * | SIGSTOP | stop(*)(+) |
350 * | SIGTSTP | stop(*) |
351 * | SIGTTIN | stop(*) |
352 * | SIGTTOU | stop(*) |
353 * | SIGURG | ignore |
354 * | SIGXCPU | coredump |
355 * | SIGXFSZ | coredump |
356 * | SIGVTALRM | terminate |
357 * | SIGPROF | terminate |
358 * | SIGPOLL/SIGIO | terminate |
359 * | SIGSYS/SIGUNUSED | coredump |
360 * | SIGSTKFLT | terminate |
361 * | SIGWINCH | ignore |
362 * | SIGPWR | terminate |
363 * | SIGRTMIN-SIGRTMAX | terminate |
364 * +--------------------+------------------+
365 * | non-POSIX signal | default action |
366 * +--------------------+------------------+
367 * | SIGEMT | coredump |
368 * +--------------------+------------------+
369 *
370 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
371 * (*) Special job control effects:
372 * When SIGCONT is sent, it resumes the process (all threads in the group)
373 * from TASK_STOPPED state and also clears any pending/queued stop signals
374 * (any of those marked with "stop(*)"). This happens regardless of blocking,
375 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
376 * any pending/queued SIGCONT signals; this happens regardless of blocking,
377 * catching, or ignored the stop signal, though (except for SIGSTOP) the
378 * default action of stopping the process may happen later or never.
379 */
380
381#ifdef SIGEMT
382#define SIGEMT_MASK rt_sigmask(SIGEMT)
383#else
384#define SIGEMT_MASK 0
385#endif
386
387#if SIGRTMIN > BITS_PER_LONG
388#define rt_sigmask(sig) (1ULL << ((sig)-1))
389#else
390#define rt_sigmask(sig) sigmask(sig)
391#endif
392#define siginmask(sig, mask) (rt_sigmask(sig) & (mask))
393
394#define SIG_KERNEL_ONLY_MASK (\
395 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
396
397#define SIG_KERNEL_STOP_MASK (\
398 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
399 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
400
401#define SIG_KERNEL_COREDUMP_MASK (\
402 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
403 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
404 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
405 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
406 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
407 SIGEMT_MASK )
408
409#define SIG_KERNEL_IGNORE_MASK (\
410 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
411 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
412
413#define sig_kernel_only(sig) \
414 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_ONLY_MASK))
415#define sig_kernel_coredump(sig) \
416 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_COREDUMP_MASK))
417#define sig_kernel_ignore(sig) \
418 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_IGNORE_MASK))
419#define sig_kernel_stop(sig) \
420 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_STOP_MASK))
421
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700422#define sig_user_defined(t, signr) \
423 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
424 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
425
426#define sig_fatal(t, signr) \
427 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
428 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
429
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800430void signals_init(void);
431
Al Viro5c495742012-11-18 15:29:16 -0500432int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500433int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500434
Al Virobd1c149a2013-09-01 20:35:01 +0100435#define save_altstack_ex(uss, sp) do { \
436 stack_t __user *__uss = uss; \
437 struct task_struct *t = current; \
438 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
439 put_user_ex(sas_ss_flags(sp), &__uss->ss_flags); \
440 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
441} while (0);
442
David Howells34db8aa2013-04-12 02:29:19 +0100443#ifdef CONFIG_PROC_FS
444struct seq_file;
445extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
446#endif
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#endif /* _LINUX_SIGNAL_H */