blob: 1f5a16620693a644281d6e108c256de0800f977a [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
James Hoganca9eb492016-02-08 18:43:50 +000012#ifndef HAVE_ARCH_COPY_SIGINFO
13
14#include <linux/string.h>
15
16static inline void copy_siginfo(struct siginfo *to, struct siginfo *from)
17{
18 if (from->si_code < 0)
19 memcpy(to, from, sizeof(*to));
20 else
21 /* _sigchld is currently the largest know union member */
22 memcpy(to, from, __ARCH_SI_PREAMBLE_SIZE + sizeof(from->_sifields._sigchld));
23}
24
25#endif
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/*
28 * Define some primitives to manipulate sigset_t.
29 */
30
31#ifndef __HAVE_ARCH_SIG_BITOPS
32#include <linux/bitops.h>
33
34/* We don't use <linux/bitops.h> for these because there is no need to
35 be atomic. */
36static inline void sigaddset(sigset_t *set, int _sig)
37{
38 unsigned long sig = _sig - 1;
39 if (_NSIG_WORDS == 1)
40 set->sig[0] |= 1UL << sig;
41 else
42 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
43}
44
45static inline void sigdelset(sigset_t *set, int _sig)
46{
47 unsigned long sig = _sig - 1;
48 if (_NSIG_WORDS == 1)
49 set->sig[0] &= ~(1UL << sig);
50 else
51 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
52}
53
54static inline int sigismember(sigset_t *set, int _sig)
55{
56 unsigned long sig = _sig - 1;
57 if (_NSIG_WORDS == 1)
58 return 1 & (set->sig[0] >> sig);
59 else
60 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
61}
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif /* __HAVE_ARCH_SIG_BITOPS */
64
George Anzinger71fabd52006-01-08 01:02:48 -080065static inline int sigisemptyset(sigset_t *set)
66{
George Anzinger71fabd52006-01-08 01:02:48 -080067 switch (_NSIG_WORDS) {
68 case 4:
69 return (set->sig[3] | set->sig[2] |
70 set->sig[1] | set->sig[0]) == 0;
71 case 2:
72 return (set->sig[1] | set->sig[0]) == 0;
73 case 1:
74 return set->sig[0] == 0;
75 default:
Oleg Nesterov1c3bea02014-10-13 15:53:33 -070076 BUILD_BUG();
George Anzinger71fabd52006-01-08 01:02:48 -080077 return 0;
78 }
79}
80
Waiman Longc7be96a2016-12-14 15:04:10 -080081static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
82{
83 switch (_NSIG_WORDS) {
84 case 4:
85 return (set1->sig[3] == set2->sig[3]) &&
86 (set1->sig[2] == set2->sig[2]) &&
87 (set1->sig[1] == set2->sig[1]) &&
88 (set1->sig[0] == set2->sig[0]);
89 case 2:
90 return (set1->sig[1] == set2->sig[1]) &&
91 (set1->sig[0] == set2->sig[0]);
92 case 1:
93 return set1->sig[0] == set2->sig[0];
94 }
95 return 0;
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define sigmask(sig) (1UL << ((sig) - 1))
99
100#ifndef __HAVE_ARCH_SIG_SETOPS
101#include <linux/string.h>
102
103#define _SIG_SET_BINOP(name, op) \
104static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
105{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
107 \
108 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700109 case 4: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 a3 = a->sig[3]; a2 = a->sig[2]; \
111 b3 = b->sig[3]; b2 = b->sig[2]; \
112 r->sig[3] = op(a3, b3); \
113 r->sig[2] = op(a2, b2); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700114 case 2: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 a1 = a->sig[1]; b1 = b->sig[1]; \
116 r->sig[1] = op(a1, b1); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700117 case 1: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 a0 = a->sig[0]; b0 = b->sig[0]; \
119 r->sig[0] = op(a0, b0); \
120 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700121 default: \
122 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } \
124}
125
126#define _sig_or(x,y) ((x) | (y))
127_SIG_SET_BINOP(sigorsets, _sig_or)
128
129#define _sig_and(x,y) ((x) & (y))
130_SIG_SET_BINOP(sigandsets, _sig_and)
131
Oleg Nesterov702a5072011-04-27 22:01:27 +0200132#define _sig_andn(x,y) ((x) & ~(y))
133_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135#undef _SIG_SET_BINOP
136#undef _sig_or
137#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200138#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140#define _SIG_SET_OP(name, op) \
141static inline void name(sigset_t *set) \
142{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700144 case 4: set->sig[3] = op(set->sig[3]); \
145 set->sig[2] = op(set->sig[2]); \
146 case 2: set->sig[1] = op(set->sig[1]); \
147 case 1: set->sig[0] = op(set->sig[0]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700149 default: \
150 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } \
152}
153
154#define _sig_not(x) (~(x))
155_SIG_SET_OP(signotset, _sig_not)
156
157#undef _SIG_SET_OP
158#undef _sig_not
159
160static inline void sigemptyset(sigset_t *set)
161{
162 switch (_NSIG_WORDS) {
163 default:
164 memset(set, 0, sizeof(sigset_t));
165 break;
166 case 2: set->sig[1] = 0;
167 case 1: set->sig[0] = 0;
168 break;
169 }
170}
171
172static inline void sigfillset(sigset_t *set)
173{
174 switch (_NSIG_WORDS) {
175 default:
176 memset(set, -1, sizeof(sigset_t));
177 break;
178 case 2: set->sig[1] = -1;
179 case 1: set->sig[0] = -1;
180 break;
181 }
182}
183
184/* Some extensions for manipulating the low 32 signals in particular. */
185
186static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
187{
188 set->sig[0] |= mask;
189}
190
191static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
192{
193 set->sig[0] &= ~mask;
194}
195
196static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
197{
198 return (set->sig[0] & mask) != 0;
199}
200
201static inline void siginitset(sigset_t *set, unsigned long mask)
202{
203 set->sig[0] = mask;
204 switch (_NSIG_WORDS) {
205 default:
206 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
207 break;
208 case 2: set->sig[1] = 0;
209 case 1: ;
210 }
211}
212
213static inline void siginitsetinv(sigset_t *set, unsigned long mask)
214{
215 set->sig[0] = ~mask;
216 switch (_NSIG_WORDS) {
217 default:
218 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
219 break;
220 case 2: set->sig[1] = -1;
221 case 1: ;
222 }
223}
224
225#endif /* __HAVE_ARCH_SIG_SETOPS */
226
227static inline void init_sigpending(struct sigpending *sig)
228{
229 sigemptyset(&sig->signal);
230 INIT_LIST_HEAD(&sig->list);
231}
232
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800233extern void flush_sigqueue(struct sigpending *queue);
234
Jesper Juhle5bdd882005-05-01 08:59:13 -0700235/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
236static inline int valid_signal(unsigned long sig)
237{
238 return sig <= _NSIG ? 1 : 0;
239}
240
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200241struct timespec;
242struct pt_regs;
243
Davide Libenzifba2afa2007-05-10 22:23:13 -0700244extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700245extern int do_send_sig_info(int sig, struct siginfo *info,
246 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
248extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Oleg Nesterov943df142011-04-27 21:44:14 +0200249extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
250 const struct timespec *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251extern int sigprocmask(int, sigset_t *, sigset_t *);
Al Viro77097ae2012-04-27 13:58:59 -0400252extern void set_current_blocked(sigset_t *);
253extern void __set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200254extern int show_unhandled_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Richard Weinberger828b1f62013-10-07 15:26:57 +0200256extern int get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500257extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800258extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700259extern void kernel_sigaction(int, __sighandler_t);
260
261static inline void allow_signal(int sig)
262{
263 /*
264 * Kernel threads handle their own signals. Let the signal code
265 * know it'll be handled, so that they don't get converted to
266 * SIGKILL or just silently dropped.
267 */
268 kernel_sigaction(sig, (__force __sighandler_t)2);
269}
270
271static inline void disallow_signal(int sig)
272{
273 kernel_sigaction(sig, SIG_IGN);
274}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800276extern struct kmem_cache *sighand_cachep;
277
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200278int unhandled_signal(struct task_struct *tsk, int sig);
279
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700280/*
281 * In POSIX a signal is sent either to a specific thread (Linux task)
282 * or to the process as a whole (Linux thread group). How the signal
283 * is sent determines whether it's to one thread or the whole group,
284 * which determines which signal mask(s) are involved in blocking it
285 * from being delivered until later. When the signal is delivered,
286 * either it's caught or ignored by a user handler or it has a default
287 * effect that applies to the whole thread group (POSIX process).
288 *
289 * The possible effects an unblocked signal set to SIG_DFL can have are:
290 * ignore - Nothing Happens
291 * terminate - kill the process, i.e. all threads in the group,
292 * similar to exit_group. The group leader (only) reports
293 * WIFSIGNALED status to its parent.
294 * coredump - write a core dump file describing all threads using
295 * the same mm and then kill all those threads
296 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
297 *
298 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
299 * Other signals when not blocked and set to SIG_DFL behaves as follows.
300 * The job control signals also have other special effects.
301 *
302 * +--------------------+------------------+
303 * | POSIX signal | default action |
304 * +--------------------+------------------+
305 * | SIGHUP | terminate |
306 * | SIGINT | terminate |
307 * | SIGQUIT | coredump |
308 * | SIGILL | coredump |
309 * | SIGTRAP | coredump |
310 * | SIGABRT/SIGIOT | coredump |
311 * | SIGBUS | coredump |
312 * | SIGFPE | coredump |
313 * | SIGKILL | terminate(+) |
314 * | SIGUSR1 | terminate |
315 * | SIGSEGV | coredump |
316 * | SIGUSR2 | terminate |
317 * | SIGPIPE | terminate |
318 * | SIGALRM | terminate |
319 * | SIGTERM | terminate |
320 * | SIGCHLD | ignore |
321 * | SIGCONT | ignore(*) |
322 * | SIGSTOP | stop(*)(+) |
323 * | SIGTSTP | stop(*) |
324 * | SIGTTIN | stop(*) |
325 * | SIGTTOU | stop(*) |
326 * | SIGURG | ignore |
327 * | SIGXCPU | coredump |
328 * | SIGXFSZ | coredump |
329 * | SIGVTALRM | terminate |
330 * | SIGPROF | terminate |
331 * | SIGPOLL/SIGIO | terminate |
332 * | SIGSYS/SIGUNUSED | coredump |
333 * | SIGSTKFLT | terminate |
334 * | SIGWINCH | ignore |
335 * | SIGPWR | terminate |
336 * | SIGRTMIN-SIGRTMAX | terminate |
337 * +--------------------+------------------+
338 * | non-POSIX signal | default action |
339 * +--------------------+------------------+
340 * | SIGEMT | coredump |
341 * +--------------------+------------------+
342 *
343 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
344 * (*) Special job control effects:
345 * When SIGCONT is sent, it resumes the process (all threads in the group)
346 * from TASK_STOPPED state and also clears any pending/queued stop signals
347 * (any of those marked with "stop(*)"). This happens regardless of blocking,
348 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
349 * any pending/queued SIGCONT signals; this happens regardless of blocking,
350 * catching, or ignored the stop signal, though (except for SIGSTOP) the
351 * default action of stopping the process may happen later or never.
352 */
353
354#ifdef SIGEMT
355#define SIGEMT_MASK rt_sigmask(SIGEMT)
356#else
357#define SIGEMT_MASK 0
358#endif
359
360#if SIGRTMIN > BITS_PER_LONG
361#define rt_sigmask(sig) (1ULL << ((sig)-1))
362#else
363#define rt_sigmask(sig) sigmask(sig)
364#endif
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700365
366#define siginmask(sig, mask) \
367 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700368
369#define SIG_KERNEL_ONLY_MASK (\
370 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
371
372#define SIG_KERNEL_STOP_MASK (\
373 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
374 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
375
376#define SIG_KERNEL_COREDUMP_MASK (\
377 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
378 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
379 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
380 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
381 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
382 SIGEMT_MASK )
383
384#define SIG_KERNEL_IGNORE_MASK (\
385 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
386 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
387
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700388#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
389#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
390#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
391#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700392
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700393#define sig_fatal(t, signr) \
394 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
395 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
396
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800397void signals_init(void);
398
Al Viro5c495742012-11-18 15:29:16 -0500399int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500400int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500401
Al Virobd1c149a2013-09-01 20:35:01 +0100402#define save_altstack_ex(uss, sp) do { \
403 stack_t __user *__uss = uss; \
404 struct task_struct *t = current; \
405 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300406 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
Al Virobd1c149a2013-09-01 20:35:01 +0100407 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300408 if (t->sas_ss_flags & SS_AUTODISARM) \
409 sas_ss_reset(t); \
Al Virobd1c149a2013-09-01 20:35:01 +0100410} while (0);
411
David Howells34db8aa2013-04-12 02:29:19 +0100412#ifdef CONFIG_PROC_FS
413struct seq_file;
414extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
415#endif
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#endif /* _LINUX_SIGNAL_H */