blob: d1c2b05a7a556c69f1f1d564427054e7eadc7b29 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_SIGNAL_H
2#define _LINUX_SIGNAL_H
3
Oleg Nesterov1c3bea02014-10-13 15:53:33 -07004#include <linux/bug.h>
Ingo Molnare6d930b2017-02-03 23:43:50 +01005#include <linux/signal_types.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
James Hoganca9eb492016-02-08 18:43:50 +000030#ifndef HAVE_ARCH_COPY_SIGINFO
31
32#include <linux/string.h>
33
34static inline void copy_siginfo(struct siginfo *to, struct siginfo *from)
35{
36 if (from->si_code < 0)
37 memcpy(to, from, sizeof(*to));
38 else
39 /* _sigchld is currently the largest know union member */
40 memcpy(to, from, __ARCH_SI_PREAMBLE_SIZE + sizeof(from->_sifields._sigchld));
41}
42
43#endif
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Define some primitives to manipulate sigset_t.
47 */
48
49#ifndef __HAVE_ARCH_SIG_BITOPS
50#include <linux/bitops.h>
51
52/* We don't use <linux/bitops.h> for these because there is no need to
53 be atomic. */
54static inline void sigaddset(sigset_t *set, int _sig)
55{
56 unsigned long sig = _sig - 1;
57 if (_NSIG_WORDS == 1)
58 set->sig[0] |= 1UL << sig;
59 else
60 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
61}
62
63static inline void sigdelset(sigset_t *set, int _sig)
64{
65 unsigned long sig = _sig - 1;
66 if (_NSIG_WORDS == 1)
67 set->sig[0] &= ~(1UL << sig);
68 else
69 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
70}
71
72static inline int sigismember(sigset_t *set, int _sig)
73{
74 unsigned long sig = _sig - 1;
75 if (_NSIG_WORDS == 1)
76 return 1 & (set->sig[0] >> sig);
77 else
78 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif /* __HAVE_ARCH_SIG_BITOPS */
82
George Anzinger71fabd5e2006-01-08 01:02:48 -080083static inline int sigisemptyset(sigset_t *set)
84{
George Anzinger71fabd5e2006-01-08 01:02:48 -080085 switch (_NSIG_WORDS) {
86 case 4:
87 return (set->sig[3] | set->sig[2] |
88 set->sig[1] | set->sig[0]) == 0;
89 case 2:
90 return (set->sig[1] | set->sig[0]) == 0;
91 case 1:
92 return set->sig[0] == 0;
93 default:
Oleg Nesterov1c3bea02014-10-13 15:53:33 -070094 BUILD_BUG();
George Anzinger71fabd5e2006-01-08 01:02:48 -080095 return 0;
96 }
97}
98
Waiman Longc7be96a2016-12-14 15:04:10 -080099static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
100{
101 switch (_NSIG_WORDS) {
102 case 4:
103 return (set1->sig[3] == set2->sig[3]) &&
104 (set1->sig[2] == set2->sig[2]) &&
105 (set1->sig[1] == set2->sig[1]) &&
106 (set1->sig[0] == set2->sig[0]);
107 case 2:
108 return (set1->sig[1] == set2->sig[1]) &&
109 (set1->sig[0] == set2->sig[0]);
110 case 1:
111 return set1->sig[0] == set2->sig[0];
112 }
113 return 0;
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define sigmask(sig) (1UL << ((sig) - 1))
117
118#ifndef __HAVE_ARCH_SIG_SETOPS
119#include <linux/string.h>
120
121#define _SIG_SET_BINOP(name, op) \
122static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
123{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
125 \
126 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700127 case 4: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 a3 = a->sig[3]; a2 = a->sig[2]; \
129 b3 = b->sig[3]; b2 = b->sig[2]; \
130 r->sig[3] = op(a3, b3); \
131 r->sig[2] = op(a2, b2); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700132 case 2: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 a1 = a->sig[1]; b1 = b->sig[1]; \
134 r->sig[1] = op(a1, b1); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700135 case 1: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 a0 = a->sig[0]; b0 = b->sig[0]; \
137 r->sig[0] = op(a0, b0); \
138 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700139 default: \
140 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 } \
142}
143
144#define _sig_or(x,y) ((x) | (y))
145_SIG_SET_BINOP(sigorsets, _sig_or)
146
147#define _sig_and(x,y) ((x) & (y))
148_SIG_SET_BINOP(sigandsets, _sig_and)
149
Oleg Nesterov702a5072011-04-27 22:01:27 +0200150#define _sig_andn(x,y) ((x) & ~(y))
151_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#undef _SIG_SET_BINOP
154#undef _sig_or
155#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200156#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158#define _SIG_SET_OP(name, op) \
159static inline void name(sigset_t *set) \
160{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700162 case 4: set->sig[3] = op(set->sig[3]); \
163 set->sig[2] = op(set->sig[2]); \
164 case 2: set->sig[1] = op(set->sig[1]); \
165 case 1: set->sig[0] = op(set->sig[0]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700167 default: \
168 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 } \
170}
171
172#define _sig_not(x) (~(x))
173_SIG_SET_OP(signotset, _sig_not)
174
175#undef _SIG_SET_OP
176#undef _sig_not
177
178static inline void sigemptyset(sigset_t *set)
179{
180 switch (_NSIG_WORDS) {
181 default:
182 memset(set, 0, sizeof(sigset_t));
183 break;
184 case 2: set->sig[1] = 0;
185 case 1: set->sig[0] = 0;
186 break;
187 }
188}
189
190static inline void sigfillset(sigset_t *set)
191{
192 switch (_NSIG_WORDS) {
193 default:
194 memset(set, -1, sizeof(sigset_t));
195 break;
196 case 2: set->sig[1] = -1;
197 case 1: set->sig[0] = -1;
198 break;
199 }
200}
201
202/* Some extensions for manipulating the low 32 signals in particular. */
203
204static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
205{
206 set->sig[0] |= mask;
207}
208
209static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
210{
211 set->sig[0] &= ~mask;
212}
213
214static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
215{
216 return (set->sig[0] & mask) != 0;
217}
218
219static inline void siginitset(sigset_t *set, unsigned long mask)
220{
221 set->sig[0] = mask;
222 switch (_NSIG_WORDS) {
223 default:
224 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
225 break;
226 case 2: set->sig[1] = 0;
227 case 1: ;
228 }
229}
230
231static inline void siginitsetinv(sigset_t *set, unsigned long mask)
232{
233 set->sig[0] = ~mask;
234 switch (_NSIG_WORDS) {
235 default:
236 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
237 break;
238 case 2: set->sig[1] = -1;
239 case 1: ;
240 }
241}
242
243#endif /* __HAVE_ARCH_SIG_SETOPS */
244
245static inline void init_sigpending(struct sigpending *sig)
246{
247 sigemptyset(&sig->signal);
248 INIT_LIST_HEAD(&sig->list);
249}
250
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800251extern void flush_sigqueue(struct sigpending *queue);
252
Jesper Juhle5bdd882005-05-01 08:59:13 -0700253/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
254static inline int valid_signal(unsigned long sig)
255{
256 return sig <= _NSIG ? 1 : 0;
257}
258
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200259struct timespec;
260struct pt_regs;
261
Davide Libenzifba2afa2007-05-10 22:23:13 -0700262extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700263extern int do_send_sig_info(int sig, struct siginfo *info,
264 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
266extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Oleg Nesterov943df142011-04-27 21:44:14 +0200267extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
268 const struct timespec *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269extern int sigprocmask(int, sigset_t *, sigset_t *);
Al Viro77097ae2012-04-27 13:58:59 -0400270extern void set_current_blocked(sigset_t *);
271extern void __set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200272extern int show_unhandled_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Al Viro574c4862012-11-25 22:24:19 -0500274struct sigaction {
David Howells2a148692013-03-19 14:00:53 +0000275#ifndef __ARCH_HAS_IRIX_SIGACTION
Al Viro574c4862012-11-25 22:24:19 -0500276 __sighandler_t sa_handler;
277 unsigned long sa_flags;
278#else
David Howells2a148692013-03-19 14:00:53 +0000279 unsigned int sa_flags;
Al Viro574c4862012-11-25 22:24:19 -0500280 __sighandler_t sa_handler;
281#endif
282#ifdef __ARCH_HAS_SA_RESTORER
283 __sigrestore_t sa_restorer;
284#endif
285 sigset_t sa_mask; /* mask last for extensibility */
286};
287
Al Viro92a3ce42012-11-25 21:20:05 -0500288struct k_sigaction {
289 struct sigaction sa;
290#ifdef __ARCH_HAS_KA_RESTORER
291 __sigrestore_t ka_restorer;
292#endif
293};
Al Viro495dfbf2012-12-25 19:09:45 -0500294
295#ifdef CONFIG_OLD_SIGACTION
296struct old_sigaction {
297 __sighandler_t sa_handler;
298 old_sigset_t sa_mask;
299 unsigned long sa_flags;
300 __sigrestore_t sa_restorer;
301};
302#endif
Al Viro92a3ce42012-11-25 21:20:05 -0500303
Al Viroca86b5d2012-11-07 15:09:38 -0500304struct ksignal {
305 struct k_sigaction ka;
306 siginfo_t info;
307 int sig;
308};
309
Richard Weinberger828b1f62013-10-07 15:26:57 +0200310extern int get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500311extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800312extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700313extern void kernel_sigaction(int, __sighandler_t);
314
315static inline void allow_signal(int sig)
316{
317 /*
318 * Kernel threads handle their own signals. Let the signal code
319 * know it'll be handled, so that they don't get converted to
320 * SIGKILL or just silently dropped.
321 */
322 kernel_sigaction(sig, (__force __sighandler_t)2);
323}
324
325static inline void disallow_signal(int sig)
326{
327 kernel_sigaction(sig, SIG_IGN);
328}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800330extern struct kmem_cache *sighand_cachep;
331
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200332int unhandled_signal(struct task_struct *tsk, int sig);
333
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700334/*
335 * In POSIX a signal is sent either to a specific thread (Linux task)
336 * or to the process as a whole (Linux thread group). How the signal
337 * is sent determines whether it's to one thread or the whole group,
338 * which determines which signal mask(s) are involved in blocking it
339 * from being delivered until later. When the signal is delivered,
340 * either it's caught or ignored by a user handler or it has a default
341 * effect that applies to the whole thread group (POSIX process).
342 *
343 * The possible effects an unblocked signal set to SIG_DFL can have are:
344 * ignore - Nothing Happens
345 * terminate - kill the process, i.e. all threads in the group,
346 * similar to exit_group. The group leader (only) reports
347 * WIFSIGNALED status to its parent.
348 * coredump - write a core dump file describing all threads using
349 * the same mm and then kill all those threads
350 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
351 *
352 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
353 * Other signals when not blocked and set to SIG_DFL behaves as follows.
354 * The job control signals also have other special effects.
355 *
356 * +--------------------+------------------+
357 * | POSIX signal | default action |
358 * +--------------------+------------------+
359 * | SIGHUP | terminate |
360 * | SIGINT | terminate |
361 * | SIGQUIT | coredump |
362 * | SIGILL | coredump |
363 * | SIGTRAP | coredump |
364 * | SIGABRT/SIGIOT | coredump |
365 * | SIGBUS | coredump |
366 * | SIGFPE | coredump |
367 * | SIGKILL | terminate(+) |
368 * | SIGUSR1 | terminate |
369 * | SIGSEGV | coredump |
370 * | SIGUSR2 | terminate |
371 * | SIGPIPE | terminate |
372 * | SIGALRM | terminate |
373 * | SIGTERM | terminate |
374 * | SIGCHLD | ignore |
375 * | SIGCONT | ignore(*) |
376 * | SIGSTOP | stop(*)(+) |
377 * | SIGTSTP | stop(*) |
378 * | SIGTTIN | stop(*) |
379 * | SIGTTOU | stop(*) |
380 * | SIGURG | ignore |
381 * | SIGXCPU | coredump |
382 * | SIGXFSZ | coredump |
383 * | SIGVTALRM | terminate |
384 * | SIGPROF | terminate |
385 * | SIGPOLL/SIGIO | terminate |
386 * | SIGSYS/SIGUNUSED | coredump |
387 * | SIGSTKFLT | terminate |
388 * | SIGWINCH | ignore |
389 * | SIGPWR | terminate |
390 * | SIGRTMIN-SIGRTMAX | terminate |
391 * +--------------------+------------------+
392 * | non-POSIX signal | default action |
393 * +--------------------+------------------+
394 * | SIGEMT | coredump |
395 * +--------------------+------------------+
396 *
397 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
398 * (*) Special job control effects:
399 * When SIGCONT is sent, it resumes the process (all threads in the group)
400 * from TASK_STOPPED state and also clears any pending/queued stop signals
401 * (any of those marked with "stop(*)"). This happens regardless of blocking,
402 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
403 * any pending/queued SIGCONT signals; this happens regardless of blocking,
404 * catching, or ignored the stop signal, though (except for SIGSTOP) the
405 * default action of stopping the process may happen later or never.
406 */
407
408#ifdef SIGEMT
409#define SIGEMT_MASK rt_sigmask(SIGEMT)
410#else
411#define SIGEMT_MASK 0
412#endif
413
414#if SIGRTMIN > BITS_PER_LONG
415#define rt_sigmask(sig) (1ULL << ((sig)-1))
416#else
417#define rt_sigmask(sig) sigmask(sig)
418#endif
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700419
420#define siginmask(sig, mask) \
421 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700422
423#define SIG_KERNEL_ONLY_MASK (\
424 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
425
426#define SIG_KERNEL_STOP_MASK (\
427 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
428 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
429
430#define SIG_KERNEL_COREDUMP_MASK (\
431 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
432 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
433 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
434 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
435 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
436 SIGEMT_MASK )
437
438#define SIG_KERNEL_IGNORE_MASK (\
439 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
440 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
441
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700442#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
443#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
444#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
445#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700446
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700447#define sig_user_defined(t, signr) \
448 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
449 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
450
451#define sig_fatal(t, signr) \
452 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
453 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
454
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800455void signals_init(void);
456
Al Viro5c495742012-11-18 15:29:16 -0500457int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500458int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500459
Al Virobd1c149a2013-09-01 20:35:01 +0100460#define save_altstack_ex(uss, sp) do { \
461 stack_t __user *__uss = uss; \
462 struct task_struct *t = current; \
463 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300464 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
Al Virobd1c149a2013-09-01 20:35:01 +0100465 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300466 if (t->sas_ss_flags & SS_AUTODISARM) \
467 sas_ss_reset(t); \
Al Virobd1c149a2013-09-01 20:35:01 +0100468} while (0);
469
David Howells34db8aa2013-04-12 02:29:19 +0100470#ifdef CONFIG_PROC_FS
471struct seq_file;
472extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
473#endif
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475#endif /* _LINUX_SIGNAL_H */