blob: a9bc7e1b077e0784c209d134601b8884b833a9d9 [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
Eric W. Biederman8c5dbf22017-07-24 15:28:56 -050019static inline void clear_siginfo(struct siginfo *info)
20{
21 memset(info, 0, sizeof(*info));
22}
23
Christoph Hellwigb9253a42017-06-03 21:01:01 +020024int copy_siginfo_to_user(struct siginfo __user *to, const struct siginfo *from);
25
Eric W. Biedermancc731522017-07-16 22:36:59 -050026enum siginfo_layout {
27 SIL_KILL,
28 SIL_TIMER,
29 SIL_POLL,
30 SIL_FAULT,
31 SIL_CHLD,
32 SIL_RT,
Eric W. Biedermancc731522017-07-16 22:36:59 -050033 SIL_SYS,
Eric W. Biedermancc731522017-07-16 22:36:59 -050034};
35
36enum siginfo_layout siginfo_layout(int sig, int si_code);
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * Define some primitives to manipulate sigset_t.
40 */
41
42#ifndef __HAVE_ARCH_SIG_BITOPS
43#include <linux/bitops.h>
44
45/* We don't use <linux/bitops.h> for these because there is no need to
46 be atomic. */
47static inline void sigaddset(sigset_t *set, int _sig)
48{
49 unsigned long sig = _sig - 1;
50 if (_NSIG_WORDS == 1)
51 set->sig[0] |= 1UL << sig;
52 else
53 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
54}
55
56static inline void sigdelset(sigset_t *set, int _sig)
57{
58 unsigned long sig = _sig - 1;
59 if (_NSIG_WORDS == 1)
60 set->sig[0] &= ~(1UL << sig);
61 else
62 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
63}
64
65static inline int sigismember(sigset_t *set, int _sig)
66{
67 unsigned long sig = _sig - 1;
68 if (_NSIG_WORDS == 1)
69 return 1 & (set->sig[0] >> sig);
70 else
71 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif /* __HAVE_ARCH_SIG_BITOPS */
75
George Anzinger71fabd52006-01-08 01:02:48 -080076static inline int sigisemptyset(sigset_t *set)
77{
George Anzinger71fabd52006-01-08 01:02:48 -080078 switch (_NSIG_WORDS) {
79 case 4:
80 return (set->sig[3] | set->sig[2] |
81 set->sig[1] | set->sig[0]) == 0;
82 case 2:
83 return (set->sig[1] | set->sig[0]) == 0;
84 case 1:
85 return set->sig[0] == 0;
86 default:
Oleg Nesterov1c3bea02014-10-13 15:53:33 -070087 BUILD_BUG();
George Anzinger71fabd52006-01-08 01:02:48 -080088 return 0;
89 }
90}
91
Waiman Longc7be96a2016-12-14 15:04:10 -080092static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
93{
94 switch (_NSIG_WORDS) {
95 case 4:
96 return (set1->sig[3] == set2->sig[3]) &&
97 (set1->sig[2] == set2->sig[2]) &&
98 (set1->sig[1] == set2->sig[1]) &&
99 (set1->sig[0] == set2->sig[0]);
100 case 2:
101 return (set1->sig[1] == set2->sig[1]) &&
102 (set1->sig[0] == set2->sig[0]);
103 case 1:
104 return set1->sig[0] == set2->sig[0];
105 }
106 return 0;
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define sigmask(sig) (1UL << ((sig) - 1))
110
111#ifndef __HAVE_ARCH_SIG_SETOPS
112#include <linux/string.h>
113
114#define _SIG_SET_BINOP(name, op) \
115static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
116{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
118 \
119 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700120 case 4: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 a3 = a->sig[3]; a2 = a->sig[2]; \
122 b3 = b->sig[3]; b2 = b->sig[2]; \
123 r->sig[3] = op(a3, b3); \
124 r->sig[2] = op(a2, b2); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700125 case 2: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 a1 = a->sig[1]; b1 = b->sig[1]; \
127 r->sig[1] = op(a1, b1); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700128 case 1: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 a0 = a->sig[0]; b0 = b->sig[0]; \
130 r->sig[0] = op(a0, b0); \
131 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700132 default: \
133 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 } \
135}
136
137#define _sig_or(x,y) ((x) | (y))
138_SIG_SET_BINOP(sigorsets, _sig_or)
139
140#define _sig_and(x,y) ((x) & (y))
141_SIG_SET_BINOP(sigandsets, _sig_and)
142
Oleg Nesterov702a5072011-04-27 22:01:27 +0200143#define _sig_andn(x,y) ((x) & ~(y))
144_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#undef _SIG_SET_BINOP
147#undef _sig_or
148#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200149#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define _SIG_SET_OP(name, op) \
152static inline void name(sigset_t *set) \
153{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700155 case 4: set->sig[3] = op(set->sig[3]); \
156 set->sig[2] = op(set->sig[2]); \
157 case 2: set->sig[1] = op(set->sig[1]); \
158 case 1: set->sig[0] = op(set->sig[0]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700160 default: \
161 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 } \
163}
164
165#define _sig_not(x) (~(x))
166_SIG_SET_OP(signotset, _sig_not)
167
168#undef _SIG_SET_OP
169#undef _sig_not
170
171static inline void sigemptyset(sigset_t *set)
172{
173 switch (_NSIG_WORDS) {
174 default:
175 memset(set, 0, sizeof(sigset_t));
176 break;
177 case 2: set->sig[1] = 0;
178 case 1: set->sig[0] = 0;
179 break;
180 }
181}
182
183static inline void sigfillset(sigset_t *set)
184{
185 switch (_NSIG_WORDS) {
186 default:
187 memset(set, -1, sizeof(sigset_t));
188 break;
189 case 2: set->sig[1] = -1;
190 case 1: set->sig[0] = -1;
191 break;
192 }
193}
194
195/* Some extensions for manipulating the low 32 signals in particular. */
196
197static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
198{
199 set->sig[0] |= mask;
200}
201
202static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
203{
204 set->sig[0] &= ~mask;
205}
206
207static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
208{
209 return (set->sig[0] & mask) != 0;
210}
211
212static inline void siginitset(sigset_t *set, unsigned long mask)
213{
214 set->sig[0] = mask;
215 switch (_NSIG_WORDS) {
216 default:
217 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
218 break;
219 case 2: set->sig[1] = 0;
220 case 1: ;
221 }
222}
223
224static inline void siginitsetinv(sigset_t *set, unsigned long mask)
225{
226 set->sig[0] = ~mask;
227 switch (_NSIG_WORDS) {
228 default:
229 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
230 break;
231 case 2: set->sig[1] = -1;
232 case 1: ;
233 }
234}
235
236#endif /* __HAVE_ARCH_SIG_SETOPS */
237
238static inline void init_sigpending(struct sigpending *sig)
239{
240 sigemptyset(&sig->signal);
241 INIT_LIST_HEAD(&sig->list);
242}
243
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800244extern void flush_sigqueue(struct sigpending *queue);
245
Jesper Juhle5bdd882005-05-01 08:59:13 -0700246/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
247static inline int valid_signal(unsigned long sig)
248{
249 return sig <= _NSIG ? 1 : 0;
250}
251
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200252struct timespec;
253struct pt_regs;
254
Davide Libenzifba2afa2007-05-10 22:23:13 -0700255extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700256extern int do_send_sig_info(int sig, struct siginfo *info,
257 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
259extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260extern int sigprocmask(int, sigset_t *, sigset_t *);
Al Viro77097ae2012-04-27 13:58:59 -0400261extern void set_current_blocked(sigset_t *);
262extern void __set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200263extern int show_unhandled_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Richard Weinberger828b1f62013-10-07 15:26:57 +0200265extern int get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500266extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800267extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700268extern void kernel_sigaction(int, __sighandler_t);
269
270static inline void allow_signal(int sig)
271{
272 /*
273 * Kernel threads handle their own signals. Let the signal code
274 * know it'll be handled, so that they don't get converted to
275 * SIGKILL or just silently dropped.
276 */
277 kernel_sigaction(sig, (__force __sighandler_t)2);
278}
279
280static inline void disallow_signal(int sig)
281{
282 kernel_sigaction(sig, SIG_IGN);
283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800285extern struct kmem_cache *sighand_cachep;
286
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200287int unhandled_signal(struct task_struct *tsk, int sig);
288
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700289/*
290 * In POSIX a signal is sent either to a specific thread (Linux task)
291 * or to the process as a whole (Linux thread group). How the signal
292 * is sent determines whether it's to one thread or the whole group,
293 * which determines which signal mask(s) are involved in blocking it
294 * from being delivered until later. When the signal is delivered,
295 * either it's caught or ignored by a user handler or it has a default
296 * effect that applies to the whole thread group (POSIX process).
297 *
298 * The possible effects an unblocked signal set to SIG_DFL can have are:
299 * ignore - Nothing Happens
300 * terminate - kill the process, i.e. all threads in the group,
301 * similar to exit_group. The group leader (only) reports
302 * WIFSIGNALED status to its parent.
303 * coredump - write a core dump file describing all threads using
304 * the same mm and then kill all those threads
305 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
306 *
307 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
308 * Other signals when not blocked and set to SIG_DFL behaves as follows.
309 * The job control signals also have other special effects.
310 *
311 * +--------------------+------------------+
312 * | POSIX signal | default action |
313 * +--------------------+------------------+
314 * | SIGHUP | terminate |
315 * | SIGINT | terminate |
316 * | SIGQUIT | coredump |
317 * | SIGILL | coredump |
318 * | SIGTRAP | coredump |
319 * | SIGABRT/SIGIOT | coredump |
320 * | SIGBUS | coredump |
321 * | SIGFPE | coredump |
322 * | SIGKILL | terminate(+) |
323 * | SIGUSR1 | terminate |
324 * | SIGSEGV | coredump |
325 * | SIGUSR2 | terminate |
326 * | SIGPIPE | terminate |
327 * | SIGALRM | terminate |
328 * | SIGTERM | terminate |
329 * | SIGCHLD | ignore |
330 * | SIGCONT | ignore(*) |
331 * | SIGSTOP | stop(*)(+) |
332 * | SIGTSTP | stop(*) |
333 * | SIGTTIN | stop(*) |
334 * | SIGTTOU | stop(*) |
335 * | SIGURG | ignore |
336 * | SIGXCPU | coredump |
337 * | SIGXFSZ | coredump |
338 * | SIGVTALRM | terminate |
339 * | SIGPROF | terminate |
340 * | SIGPOLL/SIGIO | terminate |
341 * | SIGSYS/SIGUNUSED | coredump |
342 * | SIGSTKFLT | terminate |
343 * | SIGWINCH | ignore |
344 * | SIGPWR | terminate |
345 * | SIGRTMIN-SIGRTMAX | terminate |
346 * +--------------------+------------------+
347 * | non-POSIX signal | default action |
348 * +--------------------+------------------+
349 * | SIGEMT | coredump |
350 * +--------------------+------------------+
351 *
352 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
353 * (*) Special job control effects:
354 * When SIGCONT is sent, it resumes the process (all threads in the group)
355 * from TASK_STOPPED state and also clears any pending/queued stop signals
356 * (any of those marked with "stop(*)"). This happens regardless of blocking,
357 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
358 * any pending/queued SIGCONT signals; this happens regardless of blocking,
359 * catching, or ignored the stop signal, though (except for SIGSTOP) the
360 * default action of stopping the process may happen later or never.
361 */
362
363#ifdef SIGEMT
364#define SIGEMT_MASK rt_sigmask(SIGEMT)
365#else
366#define SIGEMT_MASK 0
367#endif
368
369#if SIGRTMIN > BITS_PER_LONG
370#define rt_sigmask(sig) (1ULL << ((sig)-1))
371#else
372#define rt_sigmask(sig) sigmask(sig)
373#endif
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700374
375#define siginmask(sig, mask) \
376 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700377
378#define SIG_KERNEL_ONLY_MASK (\
379 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
380
381#define SIG_KERNEL_STOP_MASK (\
382 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
383 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
384
385#define SIG_KERNEL_COREDUMP_MASK (\
386 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
387 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
388 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
389 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
390 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
391 SIGEMT_MASK )
392
393#define SIG_KERNEL_IGNORE_MASK (\
394 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
395 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
396
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500397#define SIG_SPECIFIC_SICODES_MASK (\
398 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
399 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
400 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
401 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
402 SIGEMT_MASK )
403
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700404#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
405#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
406#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
407#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500408#define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700409
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700410#define sig_fatal(t, signr) \
411 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
412 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
413
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800414void signals_init(void);
415
Al Viro5c495742012-11-18 15:29:16 -0500416int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500417int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500418
Al Virobd1c149a2013-09-01 20:35:01 +0100419#define save_altstack_ex(uss, sp) do { \
420 stack_t __user *__uss = uss; \
421 struct task_struct *t = current; \
422 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300423 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
Al Virobd1c149a2013-09-01 20:35:01 +0100424 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300425 if (t->sas_ss_flags & SS_AUTODISARM) \
426 sas_ss_reset(t); \
Al Virobd1c149a2013-09-01 20:35:01 +0100427} while (0);
428
David Howells34db8aa2013-04-12 02:29:19 +0100429#ifdef CONFIG_PROC_FS
430struct seq_file;
431extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
432#endif
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#endif /* _LINUX_SIGNAL_H */