blob: 5308304993bea584105a9747d85c9abcbadcc5da [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
James Hoganca9eb492016-02-08 18:43:50 +000031#ifndef HAVE_ARCH_COPY_SIGINFO
32
33#include <linux/string.h>
34
35static inline void copy_siginfo(struct siginfo *to, struct siginfo *from)
36{
37 if (from->si_code < 0)
38 memcpy(to, from, sizeof(*to));
39 else
40 /* _sigchld is currently the largest know union member */
41 memcpy(to, from, __ARCH_SI_PREAMBLE_SIZE + sizeof(from->_sifields._sigchld));
42}
43
44#endif
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * Define some primitives to manipulate sigset_t.
48 */
49
50#ifndef __HAVE_ARCH_SIG_BITOPS
51#include <linux/bitops.h>
52
53/* We don't use <linux/bitops.h> for these because there is no need to
54 be atomic. */
55static inline void sigaddset(sigset_t *set, int _sig)
56{
57 unsigned long sig = _sig - 1;
58 if (_NSIG_WORDS == 1)
59 set->sig[0] |= 1UL << sig;
60 else
61 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
62}
63
64static inline void sigdelset(sigset_t *set, int _sig)
65{
66 unsigned long sig = _sig - 1;
67 if (_NSIG_WORDS == 1)
68 set->sig[0] &= ~(1UL << sig);
69 else
70 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
71}
72
73static inline int sigismember(sigset_t *set, int _sig)
74{
75 unsigned long sig = _sig - 1;
76 if (_NSIG_WORDS == 1)
77 return 1 & (set->sig[0] >> sig);
78 else
79 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif /* __HAVE_ARCH_SIG_BITOPS */
83
George Anzinger71fabd52006-01-08 01:02:48 -080084static inline int sigisemptyset(sigset_t *set)
85{
George Anzinger71fabd52006-01-08 01:02:48 -080086 switch (_NSIG_WORDS) {
87 case 4:
88 return (set->sig[3] | set->sig[2] |
89 set->sig[1] | set->sig[0]) == 0;
90 case 2:
91 return (set->sig[1] | set->sig[0]) == 0;
92 case 1:
93 return set->sig[0] == 0;
94 default:
Oleg Nesterov1c3bea02014-10-13 15:53:33 -070095 BUILD_BUG();
George Anzinger71fabd52006-01-08 01:02:48 -080096 return 0;
97 }
98}
99
Waiman Longc7be96a2016-12-14 15:04:10 -0800100static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
101{
102 switch (_NSIG_WORDS) {
103 case 4:
104 return (set1->sig[3] == set2->sig[3]) &&
105 (set1->sig[2] == set2->sig[2]) &&
106 (set1->sig[1] == set2->sig[1]) &&
107 (set1->sig[0] == set2->sig[0]);
108 case 2:
109 return (set1->sig[1] == set2->sig[1]) &&
110 (set1->sig[0] == set2->sig[0]);
111 case 1:
112 return set1->sig[0] == set2->sig[0];
113 }
114 return 0;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define sigmask(sig) (1UL << ((sig) - 1))
118
119#ifndef __HAVE_ARCH_SIG_SETOPS
120#include <linux/string.h>
121
122#define _SIG_SET_BINOP(name, op) \
123static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
124{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
126 \
127 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700128 case 4: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 a3 = a->sig[3]; a2 = a->sig[2]; \
130 b3 = b->sig[3]; b2 = b->sig[2]; \
131 r->sig[3] = op(a3, b3); \
132 r->sig[2] = op(a2, b2); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700133 case 2: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 a1 = a->sig[1]; b1 = b->sig[1]; \
135 r->sig[1] = op(a1, b1); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700136 case 1: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 a0 = a->sig[0]; b0 = b->sig[0]; \
138 r->sig[0] = op(a0, b0); \
139 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700140 default: \
141 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 } \
143}
144
145#define _sig_or(x,y) ((x) | (y))
146_SIG_SET_BINOP(sigorsets, _sig_or)
147
148#define _sig_and(x,y) ((x) & (y))
149_SIG_SET_BINOP(sigandsets, _sig_and)
150
Oleg Nesterov702a5072011-04-27 22:01:27 +0200151#define _sig_andn(x,y) ((x) & ~(y))
152_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154#undef _SIG_SET_BINOP
155#undef _sig_or
156#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200157#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159#define _SIG_SET_OP(name, op) \
160static inline void name(sigset_t *set) \
161{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700163 case 4: set->sig[3] = op(set->sig[3]); \
164 set->sig[2] = op(set->sig[2]); \
165 case 2: set->sig[1] = op(set->sig[1]); \
166 case 1: set->sig[0] = op(set->sig[0]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700168 default: \
169 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 } \
171}
172
173#define _sig_not(x) (~(x))
174_SIG_SET_OP(signotset, _sig_not)
175
176#undef _SIG_SET_OP
177#undef _sig_not
178
179static inline void sigemptyset(sigset_t *set)
180{
181 switch (_NSIG_WORDS) {
182 default:
183 memset(set, 0, sizeof(sigset_t));
184 break;
185 case 2: set->sig[1] = 0;
186 case 1: set->sig[0] = 0;
187 break;
188 }
189}
190
191static inline void sigfillset(sigset_t *set)
192{
193 switch (_NSIG_WORDS) {
194 default:
195 memset(set, -1, sizeof(sigset_t));
196 break;
197 case 2: set->sig[1] = -1;
198 case 1: set->sig[0] = -1;
199 break;
200 }
201}
202
203/* Some extensions for manipulating the low 32 signals in particular. */
204
205static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
206{
207 set->sig[0] |= mask;
208}
209
210static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
211{
212 set->sig[0] &= ~mask;
213}
214
215static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
216{
217 return (set->sig[0] & mask) != 0;
218}
219
220static inline void siginitset(sigset_t *set, unsigned long mask)
221{
222 set->sig[0] = mask;
223 switch (_NSIG_WORDS) {
224 default:
225 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
226 break;
227 case 2: set->sig[1] = 0;
228 case 1: ;
229 }
230}
231
232static inline void siginitsetinv(sigset_t *set, unsigned long mask)
233{
234 set->sig[0] = ~mask;
235 switch (_NSIG_WORDS) {
236 default:
237 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
238 break;
239 case 2: set->sig[1] = -1;
240 case 1: ;
241 }
242}
243
244#endif /* __HAVE_ARCH_SIG_SETOPS */
245
246static inline void init_sigpending(struct sigpending *sig)
247{
248 sigemptyset(&sig->signal);
249 INIT_LIST_HEAD(&sig->list);
250}
251
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800252extern void flush_sigqueue(struct sigpending *queue);
253
Jesper Juhle5bdd882005-05-01 08:59:13 -0700254/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
255static inline int valid_signal(unsigned long sig)
256{
257 return sig <= _NSIG ? 1 : 0;
258}
259
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200260struct timespec;
261struct pt_regs;
262
Davide Libenzifba2afa2007-05-10 22:23:13 -0700263extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700264extern int do_send_sig_info(int sig, struct siginfo *info,
265 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
267extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Oleg Nesterov943df142011-04-27 21:44:14 +0200268extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
269 const struct timespec *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270extern int sigprocmask(int, sigset_t *, sigset_t *);
Al Viro77097ae2012-04-27 13:58:59 -0400271extern void set_current_blocked(sigset_t *);
272extern void __set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200273extern int show_unhandled_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Al Viro574c4862012-11-25 22:24:19 -0500275struct sigaction {
David Howells2a148692013-03-19 14:00:53 +0000276#ifndef __ARCH_HAS_IRIX_SIGACTION
Al Viro574c4862012-11-25 22:24:19 -0500277 __sighandler_t sa_handler;
278 unsigned long sa_flags;
279#else
David Howells2a148692013-03-19 14:00:53 +0000280 unsigned int sa_flags;
Al Viro574c4862012-11-25 22:24:19 -0500281 __sighandler_t sa_handler;
282#endif
283#ifdef __ARCH_HAS_SA_RESTORER
284 __sigrestore_t sa_restorer;
285#endif
286 sigset_t sa_mask; /* mask last for extensibility */
287};
288
Al Viro92a3ce42012-11-25 21:20:05 -0500289struct k_sigaction {
290 struct sigaction sa;
291#ifdef __ARCH_HAS_KA_RESTORER
292 __sigrestore_t ka_restorer;
293#endif
294};
Al Viro495dfbf2012-12-25 19:09:45 -0500295
296#ifdef CONFIG_OLD_SIGACTION
297struct old_sigaction {
298 __sighandler_t sa_handler;
299 old_sigset_t sa_mask;
300 unsigned long sa_flags;
301 __sigrestore_t sa_restorer;
302};
303#endif
Al Viro92a3ce42012-11-25 21:20:05 -0500304
Al Viroca86b5d2012-11-07 15:09:38 -0500305struct ksignal {
306 struct k_sigaction ka;
307 siginfo_t info;
308 int sig;
309};
310
Richard Weinberger828b1f62013-10-07 15:26:57 +0200311extern int get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500312extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800313extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700314extern void kernel_sigaction(int, __sighandler_t);
315
316static inline void allow_signal(int sig)
317{
318 /*
319 * Kernel threads handle their own signals. Let the signal code
320 * know it'll be handled, so that they don't get converted to
321 * SIGKILL or just silently dropped.
322 */
323 kernel_sigaction(sig, (__force __sighandler_t)2);
324}
325
326static inline void disallow_signal(int sig)
327{
328 kernel_sigaction(sig, SIG_IGN);
329}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800331extern struct kmem_cache *sighand_cachep;
332
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200333int unhandled_signal(struct task_struct *tsk, int sig);
334
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700335/*
336 * In POSIX a signal is sent either to a specific thread (Linux task)
337 * or to the process as a whole (Linux thread group). How the signal
338 * is sent determines whether it's to one thread or the whole group,
339 * which determines which signal mask(s) are involved in blocking it
340 * from being delivered until later. When the signal is delivered,
341 * either it's caught or ignored by a user handler or it has a default
342 * effect that applies to the whole thread group (POSIX process).
343 *
344 * The possible effects an unblocked signal set to SIG_DFL can have are:
345 * ignore - Nothing Happens
346 * terminate - kill the process, i.e. all threads in the group,
347 * similar to exit_group. The group leader (only) reports
348 * WIFSIGNALED status to its parent.
349 * coredump - write a core dump file describing all threads using
350 * the same mm and then kill all those threads
351 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
352 *
353 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
354 * Other signals when not blocked and set to SIG_DFL behaves as follows.
355 * The job control signals also have other special effects.
356 *
357 * +--------------------+------------------+
358 * | POSIX signal | default action |
359 * +--------------------+------------------+
360 * | SIGHUP | terminate |
361 * | SIGINT | terminate |
362 * | SIGQUIT | coredump |
363 * | SIGILL | coredump |
364 * | SIGTRAP | coredump |
365 * | SIGABRT/SIGIOT | coredump |
366 * | SIGBUS | coredump |
367 * | SIGFPE | coredump |
368 * | SIGKILL | terminate(+) |
369 * | SIGUSR1 | terminate |
370 * | SIGSEGV | coredump |
371 * | SIGUSR2 | terminate |
372 * | SIGPIPE | terminate |
373 * | SIGALRM | terminate |
374 * | SIGTERM | terminate |
375 * | SIGCHLD | ignore |
376 * | SIGCONT | ignore(*) |
377 * | SIGSTOP | stop(*)(+) |
378 * | SIGTSTP | stop(*) |
379 * | SIGTTIN | stop(*) |
380 * | SIGTTOU | stop(*) |
381 * | SIGURG | ignore |
382 * | SIGXCPU | coredump |
383 * | SIGXFSZ | coredump |
384 * | SIGVTALRM | terminate |
385 * | SIGPROF | terminate |
386 * | SIGPOLL/SIGIO | terminate |
387 * | SIGSYS/SIGUNUSED | coredump |
388 * | SIGSTKFLT | terminate |
389 * | SIGWINCH | ignore |
390 * | SIGPWR | terminate |
391 * | SIGRTMIN-SIGRTMAX | terminate |
392 * +--------------------+------------------+
393 * | non-POSIX signal | default action |
394 * +--------------------+------------------+
395 * | SIGEMT | coredump |
396 * +--------------------+------------------+
397 *
398 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
399 * (*) Special job control effects:
400 * When SIGCONT is sent, it resumes the process (all threads in the group)
401 * from TASK_STOPPED state and also clears any pending/queued stop signals
402 * (any of those marked with "stop(*)"). This happens regardless of blocking,
403 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
404 * any pending/queued SIGCONT signals; this happens regardless of blocking,
405 * catching, or ignored the stop signal, though (except for SIGSTOP) the
406 * default action of stopping the process may happen later or never.
407 */
408
409#ifdef SIGEMT
410#define SIGEMT_MASK rt_sigmask(SIGEMT)
411#else
412#define SIGEMT_MASK 0
413#endif
414
415#if SIGRTMIN > BITS_PER_LONG
416#define rt_sigmask(sig) (1ULL << ((sig)-1))
417#else
418#define rt_sigmask(sig) sigmask(sig)
419#endif
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700420
421#define siginmask(sig, mask) \
422 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700423
424#define SIG_KERNEL_ONLY_MASK (\
425 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
426
427#define SIG_KERNEL_STOP_MASK (\
428 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
429 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
430
431#define SIG_KERNEL_COREDUMP_MASK (\
432 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
433 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
434 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
435 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
436 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
437 SIGEMT_MASK )
438
439#define SIG_KERNEL_IGNORE_MASK (\
440 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
441 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
442
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700443#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
444#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
445#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
446#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700447
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700448#define sig_user_defined(t, signr) \
449 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
450 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
451
452#define sig_fatal(t, signr) \
453 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
454 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
455
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800456void signals_init(void);
457
Al Viro5c495742012-11-18 15:29:16 -0500458int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500459int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500460
Al Virobd1c149a2013-09-01 20:35:01 +0100461#define save_altstack_ex(uss, sp) do { \
462 stack_t __user *__uss = uss; \
463 struct task_struct *t = current; \
464 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300465 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
Al Virobd1c149a2013-09-01 20:35:01 +0100466 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300467 if (t->sas_ss_flags & SS_AUTODISARM) \
468 sas_ss_reset(t); \
Al Virobd1c149a2013-09-01 20:35:01 +0100469} while (0);
470
David Howells34db8aa2013-04-12 02:29:19 +0100471#ifdef CONFIG_PROC_FS
472struct seq_file;
473extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
474#endif
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#endif /* _LINUX_SIGNAL_H */