blob: 200ed96a05afea6b72781960fd11e2f4a0b62217 [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. Biedermanae7795b2018-09-25 11:27:20 +020014static inline void copy_siginfo(kernel_siginfo_t *to,
15 const kernel_siginfo_t *from)
James Hoganca9eb492016-02-08 18:43:50 +000016{
Eric W. Biederman8c36fdf2017-07-19 21:30:42 -050017 memcpy(to, from, sizeof(*to));
James Hoganca9eb492016-02-08 18:43:50 +000018}
19
Eric W. Biedermanae7795b2018-09-25 11:27:20 +020020static inline void clear_siginfo(kernel_siginfo_t *info)
Eric W. Biederman8c5dbf22017-07-24 15:28:56 -050021{
22 memset(info, 0, sizeof(*info));
23}
24
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +020025#define SI_EXPANSION_SIZE (sizeof(struct siginfo) - sizeof(struct kernel_siginfo))
26
Eric W. Biedermanae7795b2018-09-25 11:27:20 +020027int copy_siginfo_to_user(siginfo_t __user *to, const kernel_siginfo_t *from);
28int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from);
Christoph Hellwigb9253a42017-06-03 21:01:01 +020029
Eric W. Biedermancc731522017-07-16 22:36:59 -050030enum siginfo_layout {
31 SIL_KILL,
32 SIL_TIMER,
33 SIL_POLL,
34 SIL_FAULT,
Eric W. Biederman31931c92018-04-24 20:59:47 -050035 SIL_FAULT_MCEERR,
36 SIL_FAULT_BNDERR,
37 SIL_FAULT_PKUERR,
Eric W. Biedermancc731522017-07-16 22:36:59 -050038 SIL_CHLD,
39 SIL_RT,
Eric W. Biedermancc731522017-07-16 22:36:59 -050040 SIL_SYS,
Eric W. Biedermancc731522017-07-16 22:36:59 -050041};
42
Eric W. Biedermana3670052018-10-10 20:29:44 -050043enum siginfo_layout siginfo_layout(unsigned sig, int si_code);
Eric W. Biedermancc731522017-07-16 22:36:59 -050044
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;
Eric W. Biederman01024982018-07-13 18:40:57 -0500261enum pid_type;
Oleg Nesterovb2b07e42011-05-18 15:08:03 +0200262
Davide Libenzifba2afa2007-05-10 22:23:13 -0700263extern int next_signal(struct sigpending *pending, sigset_t *mask);
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200264extern int do_send_sig_info(int sig, struct kernel_siginfo *info,
Eric W. Biederman40b3b022018-07-21 10:45:15 -0500265 struct task_struct *p, enum pid_type type);
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200266extern int group_send_sig_info(int sig, struct kernel_siginfo *info,
Eric W. Biederman01024982018-07-13 18:40:57 -0500267 struct task_struct *p, enum pid_type type);
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200268extern int __group_send_sig_info(int, struct kernel_siginfo *, struct task_struct *);
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
Christian Brauner20ab7212018-08-21 22:00:54 -0700274extern bool get_signal(struct ksignal *ksig);
Al Viro2ce5da12012-11-07 15:11:25 -0500275extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
Oleg Nesterovd12619b2008-02-08 04:19:12 -0800276extern void exit_signals(struct task_struct *tsk);
Oleg Nesterovb4e74262014-06-06 14:37:00 -0700277extern void kernel_sigaction(int, __sighandler_t);
278
279static inline void allow_signal(int sig)
280{
281 /*
282 * Kernel threads handle their own signals. Let the signal code
283 * know it'll be handled, so that they don't get converted to
284 * SIGKILL or just silently dropped.
285 */
286 kernel_sigaction(sig, (__force __sighandler_t)2);
287}
288
289static inline void disallow_signal(int sig)
290{
291 kernel_sigaction(sig, SIG_IGN);
292}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Christoph Lameter298ec1e2006-12-06 20:32:47 -0800294extern struct kmem_cache *sighand_cachep;
295
Christian Brauner67a48a22018-08-21 22:00:34 -0700296extern bool unhandled_signal(struct task_struct *tsk, int sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200297
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700298/*
299 * In POSIX a signal is sent either to a specific thread (Linux task)
300 * or to the process as a whole (Linux thread group). How the signal
301 * is sent determines whether it's to one thread or the whole group,
302 * which determines which signal mask(s) are involved in blocking it
303 * from being delivered until later. When the signal is delivered,
304 * either it's caught or ignored by a user handler or it has a default
305 * effect that applies to the whole thread group (POSIX process).
306 *
307 * The possible effects an unblocked signal set to SIG_DFL can have are:
308 * ignore - Nothing Happens
309 * terminate - kill the process, i.e. all threads in the group,
310 * similar to exit_group. The group leader (only) reports
311 * WIFSIGNALED status to its parent.
312 * coredump - write a core dump file describing all threads using
313 * the same mm and then kill all those threads
314 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
315 *
316 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
317 * Other signals when not blocked and set to SIG_DFL behaves as follows.
318 * The job control signals also have other special effects.
319 *
320 * +--------------------+------------------+
321 * | POSIX signal | default action |
322 * +--------------------+------------------+
323 * | SIGHUP | terminate |
324 * | SIGINT | terminate |
325 * | SIGQUIT | coredump |
326 * | SIGILL | coredump |
327 * | SIGTRAP | coredump |
328 * | SIGABRT/SIGIOT | coredump |
329 * | SIGBUS | coredump |
330 * | SIGFPE | coredump |
331 * | SIGKILL | terminate(+) |
332 * | SIGUSR1 | terminate |
333 * | SIGSEGV | coredump |
334 * | SIGUSR2 | terminate |
335 * | SIGPIPE | terminate |
336 * | SIGALRM | terminate |
337 * | SIGTERM | terminate |
338 * | SIGCHLD | ignore |
339 * | SIGCONT | ignore(*) |
340 * | SIGSTOP | stop(*)(+) |
341 * | SIGTSTP | stop(*) |
342 * | SIGTTIN | stop(*) |
343 * | SIGTTOU | stop(*) |
344 * | SIGURG | ignore |
345 * | SIGXCPU | coredump |
346 * | SIGXFSZ | coredump |
347 * | SIGVTALRM | terminate |
348 * | SIGPROF | terminate |
349 * | SIGPOLL/SIGIO | terminate |
350 * | SIGSYS/SIGUNUSED | coredump |
351 * | SIGSTKFLT | terminate |
352 * | SIGWINCH | ignore |
353 * | SIGPWR | terminate |
354 * | SIGRTMIN-SIGRTMAX | terminate |
355 * +--------------------+------------------+
356 * | non-POSIX signal | default action |
357 * +--------------------+------------------+
358 * | SIGEMT | coredump |
359 * +--------------------+------------------+
360 *
361 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
362 * (*) Special job control effects:
363 * When SIGCONT is sent, it resumes the process (all threads in the group)
364 * from TASK_STOPPED state and also clears any pending/queued stop signals
365 * (any of those marked with "stop(*)"). This happens regardless of blocking,
366 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
367 * any pending/queued SIGCONT signals; this happens regardless of blocking,
368 * catching, or ignored the stop signal, though (except for SIGSTOP) the
369 * default action of stopping the process may happen later or never.
370 */
371
372#ifdef SIGEMT
373#define SIGEMT_MASK rt_sigmask(SIGEMT)
374#else
375#define SIGEMT_MASK 0
376#endif
377
378#if SIGRTMIN > BITS_PER_LONG
379#define rt_sigmask(sig) (1ULL << ((sig)-1))
380#else
381#define rt_sigmask(sig) sigmask(sig)
382#endif
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700383
384#define siginmask(sig, mask) \
385 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700386
387#define SIG_KERNEL_ONLY_MASK (\
388 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
389
390#define SIG_KERNEL_STOP_MASK (\
391 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
392 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
393
394#define SIG_KERNEL_COREDUMP_MASK (\
395 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
396 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
397 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
398 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
399 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
400 SIGEMT_MASK )
401
402#define SIG_KERNEL_IGNORE_MASK (\
403 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
404 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
405
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500406#define SIG_SPECIFIC_SICODES_MASK (\
407 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
408 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
409 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
410 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
411 SIGEMT_MASK )
412
Oleg Nesterov5c8ccef2016-05-23 16:24:02 -0700413#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
414#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
415#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
416#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500417#define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700418
Roland McGrath55c0d1f2007-05-09 02:33:37 -0700419#define sig_fatal(t, signr) \
420 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
421 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
422
Adrian Bunka1c9eea2008-02-06 01:36:44 -0800423void signals_init(void);
424
Al Viro5c495742012-11-18 15:29:16 -0500425int restore_altstack(const stack_t __user *);
Al Viroc40702c2012-11-20 14:24:26 -0500426int __save_altstack(stack_t __user *, unsigned long);
Al Viro5c495742012-11-18 15:29:16 -0500427
Al Virobd1c149a2013-09-01 20:35:01 +0100428#define save_altstack_ex(uss, sp) do { \
429 stack_t __user *__uss = uss; \
430 struct task_struct *t = current; \
431 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300432 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
Al Virobd1c149a2013-09-01 20:35:01 +0100433 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
Stas Sergeev2a742132016-04-14 23:20:04 +0300434 if (t->sas_ss_flags & SS_AUTODISARM) \
435 sas_ss_reset(t); \
Al Virobd1c149a2013-09-01 20:35:01 +0100436} while (0);
437
David Howells34db8aa2013-04-12 02:29:19 +0100438#ifdef CONFIG_PROC_FS
439struct seq_file;
440extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
441#endif
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443#endif /* _LINUX_SIGNAL_H */