blob: 8037b503ce91dcd821a9f1462538179b74702f8b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_SIGNAL_H
3#define _LINUX_SIGNAL_H
4
Oleg Nesterov1c3bea02014-10-13 15:53:33 -07005#include <linux/bug.h>
Ingo Molnare6d930b2017-02-03 23:43:50 +01006#include <linux/signal_types.h>
Christoph Hellwig79942002017-06-03 21:00:59 +02007#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Stephen Rothwell1477fcc2011-05-20 11:11:53 +10009struct task_struct;
10
Dave Youngd33ed522010-03-10 15:23:59 -080011/* for sysctl */
12extern int print_fatal_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Eric W. Biederman8c36fdf2017-07-19 21:30:42 -050014static inline void copy_siginfo(struct siginfo *to, const struct siginfo *from)
James Hoganca9eb492016-02-08 18:43:50 +000015{
Eric W. Biederman8c36fdf2017-07-19 21:30:42 -050016 memcpy(to, from, sizeof(*to));
James Hoganca9eb492016-02-08 18:43:50 +000017}
18
Christoph Hellwigb9253a42017-06-03 21:01:01 +020019int copy_siginfo_to_user(struct siginfo __user *to, const struct siginfo *from);
20
Eric W. Biedermancc731522017-07-16 22:36:59 -050021enum siginfo_layout {
22 SIL_KILL,
23 SIL_TIMER,
24 SIL_POLL,
25 SIL_FAULT,
26 SIL_CHLD,
27 SIL_RT,
28#ifdef __ARCH_SIGSYS
29 SIL_SYS,
30#endif
31};
32
33enum siginfo_layout siginfo_layout(int sig, int si_code);
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * Define some primitives to manipulate sigset_t.
37 */
38
39#ifndef __HAVE_ARCH_SIG_BITOPS
40#include <linux/bitops.h>
41
42/* We don't use <linux/bitops.h> for these because there is no need to
43 be atomic. */
44static inline void sigaddset(sigset_t *set, int _sig)
45{
46 unsigned long sig = _sig - 1;
47 if (_NSIG_WORDS == 1)
48 set->sig[0] |= 1UL << sig;
49 else
50 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
51}
52
53static inline void sigdelset(sigset_t *set, int _sig)
54{
55 unsigned long sig = _sig - 1;
56 if (_NSIG_WORDS == 1)
57 set->sig[0] &= ~(1UL << sig);
58 else
59 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
60}
61
62static inline int sigismember(sigset_t *set, int _sig)
63{
64 unsigned long sig = _sig - 1;
65 if (_NSIG_WORDS == 1)
66 return 1 & (set->sig[0] >> sig);
67 else
68 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif /* __HAVE_ARCH_SIG_BITOPS */
72
George Anzinger71fabd52006-01-08 01:02:48 -080073static inline int sigisemptyset(sigset_t *set)
74{
George Anzinger71fabd52006-01-08 01:02:48 -080075 switch (_NSIG_WORDS) {
76 case 4:
77 return (set->sig[3] | set->sig[2] |
78 set->sig[1] | set->sig[0]) == 0;
79 case 2:
80 return (set->sig[1] | set->sig[0]) == 0;
81 case 1:
82 return set->sig[0] == 0;
83 default:
Oleg Nesterov1c3bea02014-10-13 15:53:33 -070084 BUILD_BUG();
George Anzinger71fabd52006-01-08 01:02:48 -080085 return 0;
86 }
87}
88
Waiman Longc7be96a2016-12-14 15:04:10 -080089static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
90{
91 switch (_NSIG_WORDS) {
92 case 4:
93 return (set1->sig[3] == set2->sig[3]) &&
94 (set1->sig[2] == set2->sig[2]) &&
95 (set1->sig[1] == set2->sig[1]) &&
96 (set1->sig[0] == set2->sig[0]);
97 case 2:
98 return (set1->sig[1] == set2->sig[1]) &&
99 (set1->sig[0] == set2->sig[0]);
100 case 1:
101 return set1->sig[0] == set2->sig[0];
102 }
103 return 0;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define sigmask(sig) (1UL << ((sig) - 1))
107
108#ifndef __HAVE_ARCH_SIG_SETOPS
109#include <linux/string.h>
110
111#define _SIG_SET_BINOP(name, op) \
112static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
113{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
115 \
116 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700117 case 4: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 a3 = a->sig[3]; a2 = a->sig[2]; \
119 b3 = b->sig[3]; b2 = b->sig[2]; \
120 r->sig[3] = op(a3, b3); \
121 r->sig[2] = op(a2, b2); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700122 case 2: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 a1 = a->sig[1]; b1 = b->sig[1]; \
124 r->sig[1] = op(a1, b1); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700125 case 1: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 a0 = a->sig[0]; b0 = b->sig[0]; \
127 r->sig[0] = op(a0, b0); \
128 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700129 default: \
130 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } \
132}
133
134#define _sig_or(x,y) ((x) | (y))
135_SIG_SET_BINOP(sigorsets, _sig_or)
136
137#define _sig_and(x,y) ((x) & (y))
138_SIG_SET_BINOP(sigandsets, _sig_and)
139
Oleg Nesterov702a5072011-04-27 22:01:27 +0200140#define _sig_andn(x,y) ((x) & ~(y))
141_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143#undef _SIG_SET_BINOP
144#undef _sig_or
145#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200146#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148#define _SIG_SET_OP(name, op) \
149static inline void name(sigset_t *set) \
150{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700152 case 4: set->sig[3] = op(set->sig[3]); \
153 set->sig[2] = op(set->sig[2]); \
154 case 2: set->sig[1] = op(set->sig[1]); \
155 case 1: set->sig[0] = op(set->sig[0]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700157 default: \
158 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 } \
160}
161
162#define _sig_not(x) (~(x))
163_SIG_SET_OP(signotset, _sig_not)
164
165#undef _SIG_SET_OP
166#undef _sig_not
167
168static inline void sigemptyset(sigset_t *set)
169{
170 switch (_NSIG_WORDS) {
171 default:
172 memset(set, 0, sizeof(sigset_t));
173 break;
174 case 2: set->sig[1] = 0;
175 case 1: set->sig[0] = 0;
176 break;
177 }
178}
179
180static inline void sigfillset(sigset_t *set)
181{
182 switch (_NSIG_WORDS) {
183 default:
184 memset(set, -1, sizeof(sigset_t));
185 break;
186 case 2: set->sig[1] = -1;
187 case 1: set->sig[0] = -1;
188 break;
189 }
190}
191
192/* Some extensions for manipulating the low 32 signals in particular. */
193
194static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
195{
196 set->sig[0] |= mask;
197}
198
199static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
200{
201 set->sig[0] &= ~mask;
202}
203
204static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
205{
206 return (set->sig[0] & mask) != 0;
207}
208
209static inline void siginitset(sigset_t *set, unsigned long mask)
210{
211 set->sig[0] = mask;
212 switch (_NSIG_WORDS) {
213 default:
214 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
215 break;
216 case 2: set->sig[1] = 0;
217 case 1: ;
218 }
219}
220
221static inline void siginitsetinv(sigset_t *set, unsigned long mask)
222{
223 set->sig[0] = ~mask;
224 switch (_NSIG_WORDS) {
225 default:
226 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
227 break;
228 case 2: set->sig[1] = -1;
229 case 1: ;
230 }
231}
232
233#endif /* __HAVE_ARCH_SIG_SETOPS */
234
235static inline void init_sigpending(struct sigpending *sig)
236{
237 sigemptyset(&sig->signal);
238 INIT_LIST_HEAD(&sig->list);
239}
240
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800241extern void flush_sigqueue(struct sigpending *queue);
242
Jesper Juhle5bdd882005-05-01 08:59:13 -0700243/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
244static inline int valid_signal(unsigned long sig)
245{
246 return sig <= _NSIG ? 1 : 0;
247}
248
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200249struct timespec;
250struct pt_regs;
251
Davide Libenzifba2afa2007-05-10 22:23:13 -0700252extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700253extern int do_send_sig_info(int sig, struct siginfo *info,
254 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
256extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257extern int sigprocmask(int, sigset_t *, sigset_t *);
Al Viro77097ae2012-04-27 13:58:59 -0400258extern void set_current_blocked(sigset_t *);
259extern void __set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200260extern int show_unhandled_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Richard Weinberger828b1f62013-10-07 15:26:57 +0200262extern int get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500263extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800264extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700265extern void kernel_sigaction(int, __sighandler_t);
266
267static inline void allow_signal(int sig)
268{
269 /*
270 * Kernel threads handle their own signals. Let the signal code
271 * know it'll be handled, so that they don't get converted to
272 * SIGKILL or just silently dropped.
273 */
274 kernel_sigaction(sig, (__force __sighandler_t)2);
275}
276
277static inline void disallow_signal(int sig)
278{
279 kernel_sigaction(sig, SIG_IGN);
280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800282extern struct kmem_cache *sighand_cachep;
283
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200284int unhandled_signal(struct task_struct *tsk, int sig);
285
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700286/*
287 * In POSIX a signal is sent either to a specific thread (Linux task)
288 * or to the process as a whole (Linux thread group). How the signal
289 * is sent determines whether it's to one thread or the whole group,
290 * which determines which signal mask(s) are involved in blocking it
291 * from being delivered until later. When the signal is delivered,
292 * either it's caught or ignored by a user handler or it has a default
293 * effect that applies to the whole thread group (POSIX process).
294 *
295 * The possible effects an unblocked signal set to SIG_DFL can have are:
296 * ignore - Nothing Happens
297 * terminate - kill the process, i.e. all threads in the group,
298 * similar to exit_group. The group leader (only) reports
299 * WIFSIGNALED status to its parent.
300 * coredump - write a core dump file describing all threads using
301 * the same mm and then kill all those threads
302 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
303 *
304 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
305 * Other signals when not blocked and set to SIG_DFL behaves as follows.
306 * The job control signals also have other special effects.
307 *
308 * +--------------------+------------------+
309 * | POSIX signal | default action |
310 * +--------------------+------------------+
311 * | SIGHUP | terminate |
312 * | SIGINT | terminate |
313 * | SIGQUIT | coredump |
314 * | SIGILL | coredump |
315 * | SIGTRAP | coredump |
316 * | SIGABRT/SIGIOT | coredump |
317 * | SIGBUS | coredump |
318 * | SIGFPE | coredump |
319 * | SIGKILL | terminate(+) |
320 * | SIGUSR1 | terminate |
321 * | SIGSEGV | coredump |
322 * | SIGUSR2 | terminate |
323 * | SIGPIPE | terminate |
324 * | SIGALRM | terminate |
325 * | SIGTERM | terminate |
326 * | SIGCHLD | ignore |
327 * | SIGCONT | ignore(*) |
328 * | SIGSTOP | stop(*)(+) |
329 * | SIGTSTP | stop(*) |
330 * | SIGTTIN | stop(*) |
331 * | SIGTTOU | stop(*) |
332 * | SIGURG | ignore |
333 * | SIGXCPU | coredump |
334 * | SIGXFSZ | coredump |
335 * | SIGVTALRM | terminate |
336 * | SIGPROF | terminate |
337 * | SIGPOLL/SIGIO | terminate |
338 * | SIGSYS/SIGUNUSED | coredump |
339 * | SIGSTKFLT | terminate |
340 * | SIGWINCH | ignore |
341 * | SIGPWR | terminate |
342 * | SIGRTMIN-SIGRTMAX | terminate |
343 * +--------------------+------------------+
344 * | non-POSIX signal | default action |
345 * +--------------------+------------------+
346 * | SIGEMT | coredump |
347 * +--------------------+------------------+
348 *
349 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
350 * (*) Special job control effects:
351 * When SIGCONT is sent, it resumes the process (all threads in the group)
352 * from TASK_STOPPED state and also clears any pending/queued stop signals
353 * (any of those marked with "stop(*)"). This happens regardless of blocking,
354 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
355 * any pending/queued SIGCONT signals; this happens regardless of blocking,
356 * catching, or ignored the stop signal, though (except for SIGSTOP) the
357 * default action of stopping the process may happen later or never.
358 */
359
360#ifdef SIGEMT
361#define SIGEMT_MASK rt_sigmask(SIGEMT)
362#else
363#define SIGEMT_MASK 0
364#endif
365
366#if SIGRTMIN > BITS_PER_LONG
367#define rt_sigmask(sig) (1ULL << ((sig)-1))
368#else
369#define rt_sigmask(sig) sigmask(sig)
370#endif
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700371
372#define siginmask(sig, mask) \
373 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700374
375#define SIG_KERNEL_ONLY_MASK (\
376 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
377
378#define SIG_KERNEL_STOP_MASK (\
379 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
380 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
381
382#define SIG_KERNEL_COREDUMP_MASK (\
383 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
384 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
385 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
386 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
387 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
388 SIGEMT_MASK )
389
390#define SIG_KERNEL_IGNORE_MASK (\
391 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
392 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
393
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500394#define SIG_SPECIFIC_SICODES_MASK (\
395 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
396 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
397 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
398 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
399 SIGEMT_MASK )
400
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700401#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
402#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
403#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
404#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500405#define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700406
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700407#define sig_fatal(t, signr) \
408 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
409 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
410
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800411void signals_init(void);
412
Al Viro5c495742012-11-18 15:29:16 -0500413int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500414int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500415
Al Virobd1c149a2013-09-01 20:35:01 +0100416#define save_altstack_ex(uss, sp) do { \
417 stack_t __user *__uss = uss; \
418 struct task_struct *t = current; \
419 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300420 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
Al Virobd1c149a2013-09-01 20:35:01 +0100421 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300422 if (t->sas_ss_flags & SS_AUTODISARM) \
423 sas_ss_reset(t); \
Al Virobd1c149a2013-09-01 20:35:01 +0100424} while (0);
425
David Howells34db8aa2013-04-12 02:29:19 +0100426#ifdef CONFIG_PROC_FS
427struct seq_file;
428extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
429#endif
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#endif /* _LINUX_SIGNAL_H */