blob: 3c5200137b24f67725997532af950ffb5daf8fdf [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,
Eric W. Biederman31931c92018-04-24 20:59:47 -050031 SIL_FAULT_MCEERR,
32 SIL_FAULT_BNDERR,
33 SIL_FAULT_PKUERR,
Eric W. Biedermancc731522017-07-16 22:36:59 -050034 SIL_CHLD,
35 SIL_RT,
Eric W. Biedermancc731522017-07-16 22:36:59 -050036 SIL_SYS,
Eric W. Biedermancc731522017-07-16 22:36:59 -050037};
38
39enum siginfo_layout siginfo_layout(int sig, int si_code);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
42 * Define some primitives to manipulate sigset_t.
43 */
44
45#ifndef __HAVE_ARCH_SIG_BITOPS
46#include <linux/bitops.h>
47
48/* We don't use <linux/bitops.h> for these because there is no need to
49 be atomic. */
50static inline void sigaddset(sigset_t *set, int _sig)
51{
52 unsigned long sig = _sig - 1;
53 if (_NSIG_WORDS == 1)
54 set->sig[0] |= 1UL << sig;
55 else
56 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
57}
58
59static inline void sigdelset(sigset_t *set, int _sig)
60{
61 unsigned long sig = _sig - 1;
62 if (_NSIG_WORDS == 1)
63 set->sig[0] &= ~(1UL << sig);
64 else
65 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
66}
67
68static inline int sigismember(sigset_t *set, int _sig)
69{
70 unsigned long sig = _sig - 1;
71 if (_NSIG_WORDS == 1)
72 return 1 & (set->sig[0] >> sig);
73 else
74 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif /* __HAVE_ARCH_SIG_BITOPS */
78
George Anzinger71fabd52006-01-08 01:02:48 -080079static inline int sigisemptyset(sigset_t *set)
80{
George Anzinger71fabd52006-01-08 01:02:48 -080081 switch (_NSIG_WORDS) {
82 case 4:
83 return (set->sig[3] | set->sig[2] |
84 set->sig[1] | set->sig[0]) == 0;
85 case 2:
86 return (set->sig[1] | set->sig[0]) == 0;
87 case 1:
88 return set->sig[0] == 0;
89 default:
Oleg Nesterov1c3bea02014-10-13 15:53:33 -070090 BUILD_BUG();
George Anzinger71fabd52006-01-08 01:02:48 -080091 return 0;
92 }
93}
94
Waiman Longc7be96a2016-12-14 15:04:10 -080095static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
96{
97 switch (_NSIG_WORDS) {
98 case 4:
99 return (set1->sig[3] == set2->sig[3]) &&
100 (set1->sig[2] == set2->sig[2]) &&
101 (set1->sig[1] == set2->sig[1]) &&
102 (set1->sig[0] == set2->sig[0]);
103 case 2:
104 return (set1->sig[1] == set2->sig[1]) &&
105 (set1->sig[0] == set2->sig[0]);
106 case 1:
107 return set1->sig[0] == set2->sig[0];
108 }
109 return 0;
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define sigmask(sig) (1UL << ((sig) - 1))
113
114#ifndef __HAVE_ARCH_SIG_SETOPS
115#include <linux/string.h>
116
117#define _SIG_SET_BINOP(name, op) \
118static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
119{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
121 \
122 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700123 case 4: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 a3 = a->sig[3]; a2 = a->sig[2]; \
125 b3 = b->sig[3]; b2 = b->sig[2]; \
126 r->sig[3] = op(a3, b3); \
127 r->sig[2] = op(a2, b2); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700128 case 2: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 a1 = a->sig[1]; b1 = b->sig[1]; \
130 r->sig[1] = op(a1, b1); \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700131 case 1: \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 a0 = a->sig[0]; b0 = b->sig[0]; \
133 r->sig[0] = op(a0, b0); \
134 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700135 default: \
136 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 } \
138}
139
140#define _sig_or(x,y) ((x) | (y))
141_SIG_SET_BINOP(sigorsets, _sig_or)
142
143#define _sig_and(x,y) ((x) & (y))
144_SIG_SET_BINOP(sigandsets, _sig_and)
145
Oleg Nesterov702a5072011-04-27 22:01:27 +0200146#define _sig_andn(x,y) ((x) & ~(y))
147_SIG_SET_BINOP(sigandnsets, _sig_andn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149#undef _SIG_SET_BINOP
150#undef _sig_or
151#undef _sig_and
Oleg Nesterov702a5072011-04-27 22:01:27 +0200152#undef _sig_andn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154#define _SIG_SET_OP(name, op) \
155static inline void name(sigset_t *set) \
156{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 switch (_NSIG_WORDS) { \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700158 case 4: set->sig[3] = op(set->sig[3]); \
159 set->sig[2] = op(set->sig[2]); \
160 case 2: set->sig[1] = op(set->sig[1]); \
161 case 1: set->sig[0] = op(set->sig[0]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break; \
Oleg Nesterov1c3bea02014-10-13 15:53:33 -0700163 default: \
164 BUILD_BUG(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } \
166}
167
168#define _sig_not(x) (~(x))
169_SIG_SET_OP(signotset, _sig_not)
170
171#undef _SIG_SET_OP
172#undef _sig_not
173
174static inline void sigemptyset(sigset_t *set)
175{
176 switch (_NSIG_WORDS) {
177 default:
178 memset(set, 0, sizeof(sigset_t));
179 break;
180 case 2: set->sig[1] = 0;
181 case 1: set->sig[0] = 0;
182 break;
183 }
184}
185
186static inline void sigfillset(sigset_t *set)
187{
188 switch (_NSIG_WORDS) {
189 default:
190 memset(set, -1, sizeof(sigset_t));
191 break;
192 case 2: set->sig[1] = -1;
193 case 1: set->sig[0] = -1;
194 break;
195 }
196}
197
198/* Some extensions for manipulating the low 32 signals in particular. */
199
200static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
201{
202 set->sig[0] |= mask;
203}
204
205static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
206{
207 set->sig[0] &= ~mask;
208}
209
210static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
211{
212 return (set->sig[0] & mask) != 0;
213}
214
215static inline void siginitset(sigset_t *set, unsigned long mask)
216{
217 set->sig[0] = mask;
218 switch (_NSIG_WORDS) {
219 default:
220 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
221 break;
222 case 2: set->sig[1] = 0;
223 case 1: ;
224 }
225}
226
227static inline void siginitsetinv(sigset_t *set, unsigned long mask)
228{
229 set->sig[0] = ~mask;
230 switch (_NSIG_WORDS) {
231 default:
232 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
233 break;
234 case 2: set->sig[1] = -1;
235 case 1: ;
236 }
237}
238
239#endif /* __HAVE_ARCH_SIG_SETOPS */
240
241static inline void init_sigpending(struct sigpending *sig)
242{
243 sigemptyset(&sig->signal);
244 INIT_LIST_HEAD(&sig->list);
245}
246
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800247extern void flush_sigqueue(struct sigpending *queue);
248
Jesper Juhle5bdd882005-05-01 08:59:13 -0700249/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
250static inline int valid_signal(unsigned long sig)
251{
252 return sig <= _NSIG ? 1 : 0;
253}
254
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200255struct timespec;
256struct pt_regs;
257
Davide Libenzifba2afa2007-05-10 22:23:13 -0700258extern int next_signal(struct sigpending *pending, sigset_t *mask);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700259extern int do_send_sig_info(int sig, struct siginfo *info,
260 struct task_struct *p, bool group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
262extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263extern int sigprocmask(int, sigset_t *, sigset_t *);
Al Viro77097ae2012-04-27 13:58:59 -0400264extern void set_current_blocked(sigset_t *);
265extern void __set_current_blocked(const sigset_t *);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200266extern int show_unhandled_signals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Richard Weinberger828b1f62013-10-07 15:26:57 +0200268extern int get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500269extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800270extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700271extern void kernel_sigaction(int, __sighandler_t);
272
273static inline void allow_signal(int sig)
274{
275 /*
276 * Kernel threads handle their own signals. Let the signal code
277 * know it'll be handled, so that they don't get converted to
278 * SIGKILL or just silently dropped.
279 */
280 kernel_sigaction(sig, (__force __sighandler_t)2);
281}
282
283static inline void disallow_signal(int sig)
284{
285 kernel_sigaction(sig, SIG_IGN);
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800288extern struct kmem_cache *sighand_cachep;
289
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200290int unhandled_signal(struct task_struct *tsk, int sig);
291
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700292/*
293 * In POSIX a signal is sent either to a specific thread (Linux task)
294 * or to the process as a whole (Linux thread group). How the signal
295 * is sent determines whether it's to one thread or the whole group,
296 * which determines which signal mask(s) are involved in blocking it
297 * from being delivered until later. When the signal is delivered,
298 * either it's caught or ignored by a user handler or it has a default
299 * effect that applies to the whole thread group (POSIX process).
300 *
301 * The possible effects an unblocked signal set to SIG_DFL can have are:
302 * ignore - Nothing Happens
303 * terminate - kill the process, i.e. all threads in the group,
304 * similar to exit_group. The group leader (only) reports
305 * WIFSIGNALED status to its parent.
306 * coredump - write a core dump file describing all threads using
307 * the same mm and then kill all those threads
308 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
309 *
310 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
311 * Other signals when not blocked and set to SIG_DFL behaves as follows.
312 * The job control signals also have other special effects.
313 *
314 * +--------------------+------------------+
315 * | POSIX signal | default action |
316 * +--------------------+------------------+
317 * | SIGHUP | terminate |
318 * | SIGINT | terminate |
319 * | SIGQUIT | coredump |
320 * | SIGILL | coredump |
321 * | SIGTRAP | coredump |
322 * | SIGABRT/SIGIOT | coredump |
323 * | SIGBUS | coredump |
324 * | SIGFPE | coredump |
325 * | SIGKILL | terminate(+) |
326 * | SIGUSR1 | terminate |
327 * | SIGSEGV | coredump |
328 * | SIGUSR2 | terminate |
329 * | SIGPIPE | terminate |
330 * | SIGALRM | terminate |
331 * | SIGTERM | terminate |
332 * | SIGCHLD | ignore |
333 * | SIGCONT | ignore(*) |
334 * | SIGSTOP | stop(*)(+) |
335 * | SIGTSTP | stop(*) |
336 * | SIGTTIN | stop(*) |
337 * | SIGTTOU | stop(*) |
338 * | SIGURG | ignore |
339 * | SIGXCPU | coredump |
340 * | SIGXFSZ | coredump |
341 * | SIGVTALRM | terminate |
342 * | SIGPROF | terminate |
343 * | SIGPOLL/SIGIO | terminate |
344 * | SIGSYS/SIGUNUSED | coredump |
345 * | SIGSTKFLT | terminate |
346 * | SIGWINCH | ignore |
347 * | SIGPWR | terminate |
348 * | SIGRTMIN-SIGRTMAX | terminate |
349 * +--------------------+------------------+
350 * | non-POSIX signal | default action |
351 * +--------------------+------------------+
352 * | SIGEMT | coredump |
353 * +--------------------+------------------+
354 *
355 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
356 * (*) Special job control effects:
357 * When SIGCONT is sent, it resumes the process (all threads in the group)
358 * from TASK_STOPPED state and also clears any pending/queued stop signals
359 * (any of those marked with "stop(*)"). This happens regardless of blocking,
360 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
361 * any pending/queued SIGCONT signals; this happens regardless of blocking,
362 * catching, or ignored the stop signal, though (except for SIGSTOP) the
363 * default action of stopping the process may happen later or never.
364 */
365
366#ifdef SIGEMT
367#define SIGEMT_MASK rt_sigmask(SIGEMT)
368#else
369#define SIGEMT_MASK 0
370#endif
371
372#if SIGRTMIN > BITS_PER_LONG
373#define rt_sigmask(sig) (1ULL << ((sig)-1))
374#else
375#define rt_sigmask(sig) sigmask(sig)
376#endif
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700377
378#define siginmask(sig, mask) \
379 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700380
381#define SIG_KERNEL_ONLY_MASK (\
382 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
383
384#define SIG_KERNEL_STOP_MASK (\
385 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
386 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
387
388#define SIG_KERNEL_COREDUMP_MASK (\
389 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
390 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
391 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
392 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
393 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
394 SIGEMT_MASK )
395
396#define SIG_KERNEL_IGNORE_MASK (\
397 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
398 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
399
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500400#define SIG_SPECIFIC_SICODES_MASK (\
401 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
402 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
403 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
404 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
405 SIGEMT_MASK )
406
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700407#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
408#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
409#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
410#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500411#define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700412
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700413#define sig_fatal(t, signr) \
414 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
415 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
416
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800417void signals_init(void);
418
Al Viro5c495742012-11-18 15:29:16 -0500419int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500420int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500421
Al Virobd1c149a2013-09-01 20:35:01 +0100422#define save_altstack_ex(uss, sp) do { \
423 stack_t __user *__uss = uss; \
424 struct task_struct *t = current; \
425 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300426 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
Al Virobd1c149a2013-09-01 20:35:01 +0100427 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300428 if (t->sas_ss_flags & SS_AUTODISARM) \
429 sas_ss_reset(t); \
Al Virobd1c149a2013-09-01 20:35:01 +0100430} while (0);
431
David Howells34db8aa2013-04-12 02:29:19 +0100432#ifdef CONFIG_PROC_FS
433struct seq_file;
434extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
435#endif
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#endif /* _LINUX_SIGNAL_H */