blob: 92557bbce7e7b9a3d9c6c6ed4c4d4f54ff7ff9e5 [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>
Oleg Nesterov1c3bea02014-10-13 15:53:33 -07005#include <linux/bug.h>
David Howells607ca462012-10-13 10:46:48 +01006#include <uapi/linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Stephen Rothwell1477fcc2011-05-20 11:11:53 +10008struct task_struct;
9
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif /* __HAVE_ARCH_SIG_BITOPS */
68
George Anzinger71fabd52006-01-08 01:02:48 -080069static inline int sigisemptyset(sigset_t *set)
70{
George Anzinger71fabd52006-01-08 01:02:48 -080071 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:
Oleg Nesterov1c3bea02014-10-13 15:53:33 -070080 BUILD_BUG();
George Anzinger71fabd52006-01-08 01:02:48 -080081 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{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
94 \
95 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -070096 case 4: \
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 a3 = a->sig[3]; a2 = a->sig[2]; \
98 b3 = b->sig[3]; b2 = b->sig[2]; \
99 r->sig[3] = op(a3, b3); \
100 r->sig[2] = op(a2, b2); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700101 case 2: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 a1 = a->sig[1]; b1 = b->sig[1]; \
103 r->sig[1] = op(a1, b1); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700104 case 1: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 a0 = a->sig[0]; b0 = b->sig[0]; \
106 r->sig[0] = op(a0, b0); \
107 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700108 default: \
109 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 } \
111}
112
113#define _sig_or(x,y) ((x) | (y))
114_SIG_SET_BINOP(sigorsets, _sig_or)
115
116#define _sig_and(x,y) ((x) & (y))
117_SIG_SET_BINOP(sigandsets, _sig_and)
118
Oleg Nesterov702a5072011-04-27 22:01:27 +0200119#define _sig_andn(x,y) ((x) & ~(y))
120_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122#undef _SIG_SET_BINOP
123#undef _sig_or
124#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200125#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127#define _SIG_SET_OP(name, op) \
128static inline void name(sigset_t *set) \
129{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700131 case 4: set->sig[3] = op(set->sig[3]); \
132 set->sig[2] = op(set->sig[2]); \
133 case 2: set->sig[1] = op(set->sig[1]); \
134 case 1: set->sig[0] = op(set->sig[0]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700136 default: \
137 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 } \
139}
140
141#define _sig_not(x) (~(x))
142_SIG_SET_OP(signotset, _sig_not)
143
144#undef _SIG_SET_OP
145#undef _sig_not
146
147static inline void sigemptyset(sigset_t *set)
148{
149 switch (_NSIG_WORDS) {
150 default:
151 memset(set, 0, sizeof(sigset_t));
152 break;
153 case 2: set->sig[1] = 0;
154 case 1: set->sig[0] = 0;
155 break;
156 }
157}
158
159static inline void sigfillset(sigset_t *set)
160{
161 switch (_NSIG_WORDS) {
162 default:
163 memset(set, -1, sizeof(sigset_t));
164 break;
165 case 2: set->sig[1] = -1;
166 case 1: set->sig[0] = -1;
167 break;
168 }
169}
170
171/* Some extensions for manipulating the low 32 signals in particular. */
172
173static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
174{
175 set->sig[0] |= mask;
176}
177
178static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
179{
180 set->sig[0] &= ~mask;
181}
182
183static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
184{
185 return (set->sig[0] & mask) != 0;
186}
187
188static inline void siginitset(sigset_t *set, unsigned long mask)
189{
190 set->sig[0] = mask;
191 switch (_NSIG_WORDS) {
192 default:
193 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
194 break;
195 case 2: set->sig[1] = 0;
196 case 1: ;
197 }
198}
199
200static inline void siginitsetinv(sigset_t *set, unsigned long mask)
201{
202 set->sig[0] = ~mask;
203 switch (_NSIG_WORDS) {
204 default:
205 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
206 break;
207 case 2: set->sig[1] = -1;
208 case 1: ;
209 }
210}
211
212#endif /* __HAVE_ARCH_SIG_SETOPS */
213
214static inline void init_sigpending(struct sigpending *sig)
215{
216 sigemptyset(&sig->signal);
217 INIT_LIST_HEAD(&sig->list);
218}
219
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800220extern void flush_sigqueue(struct sigpending *queue);
221
Jesper Juhle5bdd882005-05-01 08:59:13 -0700222/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
223static inline int valid_signal(unsigned long sig)
224{
225 return sig <= _NSIG ? 1 : 0;
226}
227
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200228struct timespec;
229struct pt_regs;
230
Davide Libenzifba2afa2007-05-10 22:23:13 -0700231extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700232extern int do_send_sig_info(int sig, struct siginfo *info,
233 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
235extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Oleg Nesterov943df142011-04-27 21:44:14 +0200236extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
237 const struct timespec *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238extern int sigprocmask(int, sigset_t *, sigset_t *);
Al Viro77097ae2012-04-27 13:58:59 -0400239extern void set_current_blocked(sigset_t *);
240extern void __set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200241extern int show_unhandled_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Al Viro574c4862012-11-25 22:24:19 -0500243struct sigaction {
David Howells2a148692013-03-19 14:00:53 +0000244#ifndef __ARCH_HAS_IRIX_SIGACTION
Al Viro574c4862012-11-25 22:24:19 -0500245 __sighandler_t sa_handler;
246 unsigned long sa_flags;
247#else
David Howells2a148692013-03-19 14:00:53 +0000248 unsigned int sa_flags;
Al Viro574c4862012-11-25 22:24:19 -0500249 __sighandler_t sa_handler;
250#endif
251#ifdef __ARCH_HAS_SA_RESTORER
252 __sigrestore_t sa_restorer;
253#endif
254 sigset_t sa_mask; /* mask last for extensibility */
255};
256
Al Viro92a3ce42012-11-25 21:20:05 -0500257struct k_sigaction {
258 struct sigaction sa;
259#ifdef __ARCH_HAS_KA_RESTORER
260 __sigrestore_t ka_restorer;
261#endif
262};
Al Viro495dfbf2012-12-25 19:09:45 -0500263
264#ifdef CONFIG_OLD_SIGACTION
265struct old_sigaction {
266 __sighandler_t sa_handler;
267 old_sigset_t sa_mask;
268 unsigned long sa_flags;
269 __sigrestore_t sa_restorer;
270};
271#endif
Al Viro92a3ce42012-11-25 21:20:05 -0500272
Al Viroca86b5d2012-11-07 15:09:38 -0500273struct ksignal {
274 struct k_sigaction ka;
275 siginfo_t info;
276 int sig;
277};
278
Richard Weinberger828b1f62013-10-07 15:26:57 +0200279extern int get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500280extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800281extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700282extern void kernel_sigaction(int, __sighandler_t);
283
284static inline void allow_signal(int sig)
285{
286 /*
287 * Kernel threads handle their own signals. Let the signal code
288 * know it'll be handled, so that they don't get converted to
289 * SIGKILL or just silently dropped.
290 */
291 kernel_sigaction(sig, (__force __sighandler_t)2);
292}
293
294static inline void disallow_signal(int sig)
295{
296 kernel_sigaction(sig, SIG_IGN);
297}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800299extern struct kmem_cache *sighand_cachep;
300
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200301int unhandled_signal(struct task_struct *tsk, int sig);
302
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700303/*
304 * In POSIX a signal is sent either to a specific thread (Linux task)
305 * or to the process as a whole (Linux thread group). How the signal
306 * is sent determines whether it's to one thread or the whole group,
307 * which determines which signal mask(s) are involved in blocking it
308 * from being delivered until later. When the signal is delivered,
309 * either it's caught or ignored by a user handler or it has a default
310 * effect that applies to the whole thread group (POSIX process).
311 *
312 * The possible effects an unblocked signal set to SIG_DFL can have are:
313 * ignore - Nothing Happens
314 * terminate - kill the process, i.e. all threads in the group,
315 * similar to exit_group. The group leader (only) reports
316 * WIFSIGNALED status to its parent.
317 * coredump - write a core dump file describing all threads using
318 * the same mm and then kill all those threads
319 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
320 *
321 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
322 * Other signals when not blocked and set to SIG_DFL behaves as follows.
323 * The job control signals also have other special effects.
324 *
325 * +--------------------+------------------+
326 * | POSIX signal | default action |
327 * +--------------------+------------------+
328 * | SIGHUP | terminate |
329 * | SIGINT | terminate |
330 * | SIGQUIT | coredump |
331 * | SIGILL | coredump |
332 * | SIGTRAP | coredump |
333 * | SIGABRT/SIGIOT | coredump |
334 * | SIGBUS | coredump |
335 * | SIGFPE | coredump |
336 * | SIGKILL | terminate(+) |
337 * | SIGUSR1 | terminate |
338 * | SIGSEGV | coredump |
339 * | SIGUSR2 | terminate |
340 * | SIGPIPE | terminate |
341 * | SIGALRM | terminate |
342 * | SIGTERM | terminate |
343 * | SIGCHLD | ignore |
344 * | SIGCONT | ignore(*) |
345 * | SIGSTOP | stop(*)(+) |
346 * | SIGTSTP | stop(*) |
347 * | SIGTTIN | stop(*) |
348 * | SIGTTOU | stop(*) |
349 * | SIGURG | ignore |
350 * | SIGXCPU | coredump |
351 * | SIGXFSZ | coredump |
352 * | SIGVTALRM | terminate |
353 * | SIGPROF | terminate |
354 * | SIGPOLL/SIGIO | terminate |
355 * | SIGSYS/SIGUNUSED | coredump |
356 * | SIGSTKFLT | terminate |
357 * | SIGWINCH | ignore |
358 * | SIGPWR | terminate |
359 * | SIGRTMIN-SIGRTMAX | terminate |
360 * +--------------------+------------------+
361 * | non-POSIX signal | default action |
362 * +--------------------+------------------+
363 * | SIGEMT | coredump |
364 * +--------------------+------------------+
365 *
366 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
367 * (*) Special job control effects:
368 * When SIGCONT is sent, it resumes the process (all threads in the group)
369 * from TASK_STOPPED state and also clears any pending/queued stop signals
370 * (any of those marked with "stop(*)"). This happens regardless of blocking,
371 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
372 * any pending/queued SIGCONT signals; this happens regardless of blocking,
373 * catching, or ignored the stop signal, though (except for SIGSTOP) the
374 * default action of stopping the process may happen later or never.
375 */
376
377#ifdef SIGEMT
378#define SIGEMT_MASK rt_sigmask(SIGEMT)
379#else
380#define SIGEMT_MASK 0
381#endif
382
383#if SIGRTMIN > BITS_PER_LONG
384#define rt_sigmask(sig) (1ULL << ((sig)-1))
385#else
386#define rt_sigmask(sig) sigmask(sig)
387#endif
388#define siginmask(sig, mask) (rt_sigmask(sig) & (mask))
389
390#define SIG_KERNEL_ONLY_MASK (\
391 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
392
393#define SIG_KERNEL_STOP_MASK (\
394 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
395 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
396
397#define SIG_KERNEL_COREDUMP_MASK (\
398 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
399 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
400 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
401 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
402 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
403 SIGEMT_MASK )
404
405#define SIG_KERNEL_IGNORE_MASK (\
406 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
407 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
408
409#define sig_kernel_only(sig) \
410 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_ONLY_MASK))
411#define sig_kernel_coredump(sig) \
412 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_COREDUMP_MASK))
413#define sig_kernel_ignore(sig) \
414 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_IGNORE_MASK))
415#define sig_kernel_stop(sig) \
416 (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_STOP_MASK))
417
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700418#define sig_user_defined(t, signr) \
419 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
420 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
421
422#define sig_fatal(t, signr) \
423 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
424 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
425
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800426void signals_init(void);
427
Al Viro5c495742012-11-18 15:29:16 -0500428int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500429int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500430
Al Virobd1c149a2013-09-01 20:35:01 +0100431#define save_altstack_ex(uss, sp) do { \
432 stack_t __user *__uss = uss; \
433 struct task_struct *t = current; \
434 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
435 put_user_ex(sas_ss_flags(sp), &__uss->ss_flags); \
436 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
437} while (0);
438
David Howells34db8aa2013-04-12 02:29:19 +0100439#ifdef CONFIG_PROC_FS
440struct seq_file;
441extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
442#endif
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif /* _LINUX_SIGNAL_H */