]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/signal.h
net: rtnetlink: validate IFLA_MTU attribute in rtnl_create_link()
[mirror_ubuntu-bionic-kernel.git] / include / linux / signal.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef _LINUX_SIGNAL_H
3#define _LINUX_SIGNAL_H
4
1c3bea0e 5#include <linux/bug.h>
e6d930b4 6#include <linux/signal_types.h>
7994200c 7#include <linux/string.h>
1da177e4 8
1477fcc2
SR
9struct task_struct;
10
d33ed52d
DY
11/* for sysctl */
12extern int print_fatal_signals;
1da177e4 13
ca9eb49a
JH
14static inline void copy_siginfo(struct siginfo *to, struct siginfo *from)
15{
16 if (from->si_code < 0)
17 memcpy(to, from, sizeof(*to));
18 else
19 /* _sigchld is currently the largest know union member */
20 memcpy(to, from, __ARCH_SI_PREAMBLE_SIZE + sizeof(from->_sifields._sigchld));
21}
22
b9253a43
CH
23int copy_siginfo_to_user(struct siginfo __user *to, const struct siginfo *from);
24
cc731525
EB
25enum siginfo_layout {
26 SIL_KILL,
27 SIL_TIMER,
28 SIL_POLL,
29 SIL_FAULT,
30 SIL_CHLD,
31 SIL_RT,
32#ifdef __ARCH_SIGSYS
33 SIL_SYS,
34#endif
35};
36
9cbc4a39 37enum siginfo_layout siginfo_layout(unsigned sig, int si_code);
cc731525 38
1da177e4
LT
39/*
40 * Define some primitives to manipulate sigset_t.
41 */
42
43#ifndef __HAVE_ARCH_SIG_BITOPS
44#include <linux/bitops.h>
45
46/* We don't use <linux/bitops.h> for these because there is no need to
47 be atomic. */
48static inline void sigaddset(sigset_t *set, int _sig)
49{
50 unsigned long sig = _sig - 1;
51 if (_NSIG_WORDS == 1)
52 set->sig[0] |= 1UL << sig;
53 else
54 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
55}
56
57static inline void sigdelset(sigset_t *set, int _sig)
58{
59 unsigned long sig = _sig - 1;
60 if (_NSIG_WORDS == 1)
61 set->sig[0] &= ~(1UL << sig);
62 else
63 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
64}
65
66static inline int sigismember(sigset_t *set, int _sig)
67{
68 unsigned long sig = _sig - 1;
69 if (_NSIG_WORDS == 1)
70 return 1 & (set->sig[0] >> sig);
71 else
72 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
73}
74
1da177e4
LT
75#endif /* __HAVE_ARCH_SIG_BITOPS */
76
71fabd5e
GA
77static inline int sigisemptyset(sigset_t *set)
78{
71fabd5e
GA
79 switch (_NSIG_WORDS) {
80 case 4:
81 return (set->sig[3] | set->sig[2] |
82 set->sig[1] | set->sig[0]) == 0;
83 case 2:
84 return (set->sig[1] | set->sig[0]) == 0;
85 case 1:
86 return set->sig[0] == 0;
87 default:
1c3bea0e 88 BUILD_BUG();
71fabd5e
GA
89 return 0;
90 }
91}
92
c7be96af
WL
93static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
94{
95 switch (_NSIG_WORDS) {
96 case 4:
97 return (set1->sig[3] == set2->sig[3]) &&
98 (set1->sig[2] == set2->sig[2]) &&
99 (set1->sig[1] == set2->sig[1]) &&
100 (set1->sig[0] == set2->sig[0]);
101 case 2:
102 return (set1->sig[1] == set2->sig[1]) &&
103 (set1->sig[0] == set2->sig[0]);
104 case 1:
105 return set1->sig[0] == set2->sig[0];
106 }
107 return 0;
108}
109
1da177e4
LT
110#define sigmask(sig) (1UL << ((sig) - 1))
111
112#ifndef __HAVE_ARCH_SIG_SETOPS
113#include <linux/string.h>
114
115#define _SIG_SET_BINOP(name, op) \
116static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
117{ \
1da177e4
LT
118 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
119 \
120 switch (_NSIG_WORDS) { \
1c3bea0e 121 case 4: \
1da177e4
LT
122 a3 = a->sig[3]; a2 = a->sig[2]; \
123 b3 = b->sig[3]; b2 = b->sig[2]; \
124 r->sig[3] = op(a3, b3); \
125 r->sig[2] = op(a2, b2); \
1c3bea0e 126 case 2: \
1da177e4
LT
127 a1 = a->sig[1]; b1 = b->sig[1]; \
128 r->sig[1] = op(a1, b1); \
1c3bea0e 129 case 1: \
1da177e4
LT
130 a0 = a->sig[0]; b0 = b->sig[0]; \
131 r->sig[0] = op(a0, b0); \
132 break; \
1c3bea0e
ON
133 default: \
134 BUILD_BUG(); \
1da177e4
LT
135 } \
136}
137
138#define _sig_or(x,y) ((x) | (y))
139_SIG_SET_BINOP(sigorsets, _sig_or)
140
141#define _sig_and(x,y) ((x) & (y))
142_SIG_SET_BINOP(sigandsets, _sig_and)
143
702a5073
ON
144#define _sig_andn(x,y) ((x) & ~(y))
145_SIG_SET_BINOP(sigandnsets, _sig_andn)
1da177e4
LT
146
147#undef _SIG_SET_BINOP
148#undef _sig_or
149#undef _sig_and
702a5073 150#undef _sig_andn
1da177e4
LT
151
152#define _SIG_SET_OP(name, op) \
153static inline void name(sigset_t *set) \
154{ \
1da177e4 155 switch (_NSIG_WORDS) { \
1c3bea0e
ON
156 case 4: set->sig[3] = op(set->sig[3]); \
157 set->sig[2] = op(set->sig[2]); \
158 case 2: set->sig[1] = op(set->sig[1]); \
159 case 1: set->sig[0] = op(set->sig[0]); \
1da177e4 160 break; \
1c3bea0e
ON
161 default: \
162 BUILD_BUG(); \
1da177e4
LT
163 } \
164}
165
166#define _sig_not(x) (~(x))
167_SIG_SET_OP(signotset, _sig_not)
168
169#undef _SIG_SET_OP
170#undef _sig_not
171
172static inline void sigemptyset(sigset_t *set)
173{
174 switch (_NSIG_WORDS) {
175 default:
176 memset(set, 0, sizeof(sigset_t));
177 break;
178 case 2: set->sig[1] = 0;
179 case 1: set->sig[0] = 0;
180 break;
181 }
182}
183
184static inline void sigfillset(sigset_t *set)
185{
186 switch (_NSIG_WORDS) {
187 default:
188 memset(set, -1, sizeof(sigset_t));
189 break;
190 case 2: set->sig[1] = -1;
191 case 1: set->sig[0] = -1;
192 break;
193 }
194}
195
196/* Some extensions for manipulating the low 32 signals in particular. */
197
198static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
199{
200 set->sig[0] |= mask;
201}
202
203static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
204{
205 set->sig[0] &= ~mask;
206}
207
208static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
209{
210 return (set->sig[0] & mask) != 0;
211}
212
213static inline void siginitset(sigset_t *set, unsigned long mask)
214{
215 set->sig[0] = mask;
216 switch (_NSIG_WORDS) {
217 default:
218 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
219 break;
220 case 2: set->sig[1] = 0;
221 case 1: ;
222 }
223}
224
225static inline void siginitsetinv(sigset_t *set, unsigned long mask)
226{
227 set->sig[0] = ~mask;
228 switch (_NSIG_WORDS) {
229 default:
230 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
231 break;
232 case 2: set->sig[1] = -1;
233 case 1: ;
234 }
235}
236
237#endif /* __HAVE_ARCH_SIG_SETOPS */
238
239static inline void init_sigpending(struct sigpending *sig)
240{
241 sigemptyset(&sig->signal);
242 INIT_LIST_HEAD(&sig->list);
243}
244
6a14c5c9
ON
245extern void flush_sigqueue(struct sigpending *queue);
246
e5bdd883
JJ
247/* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
248static inline int valid_signal(unsigned long sig)
249{
250 return sig <= _NSIG ? 1 : 0;
251}
252
b2b07e4f
ON
253struct timespec;
254struct pt_regs;
255
fba2afaa 256extern int next_signal(struct sigpending *pending, sigset_t *mask);
4a30debf
ON
257extern int do_send_sig_info(int sig, struct siginfo *info,
258 struct task_struct *p, bool group);
1da177e4
LT
259extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
260extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
1da177e4 261extern int sigprocmask(int, sigset_t *, sigset_t *);
77097ae5
AV
262extern void set_current_blocked(sigset_t *);
263extern void __set_current_blocked(const sigset_t *);
abd4f750 264extern int show_unhandled_signals;
1da177e4 265
828b1f65 266extern int get_signal(struct ksignal *ksig);
2ce5da17 267extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
d12619b5 268extern void exit_signals(struct task_struct *tsk);
b4e74264
ON
269extern void kernel_sigaction(int, __sighandler_t);
270
6bd82415
EB
271#define SIG_KTHREAD ((__force __sighandler_t)2)
272#define SIG_KTHREAD_KERNEL ((__force __sighandler_t)3)
273
b4e74264
ON
274static inline void allow_signal(int sig)
275{
276 /*
277 * Kernel threads handle their own signals. Let the signal code
278 * know it'll be handled, so that they don't get converted to
279 * SIGKILL or just silently dropped.
280 */
6bd82415
EB
281 kernel_sigaction(sig, SIG_KTHREAD);
282}
283
284static inline void allow_kernel_signal(int sig)
285{
286 /*
287 * Kernel threads handle their own signals. Let the signal code
288 * know signals sent by the kernel will be handled, so that they
289 * don't get silently dropped.
290 */
291 kernel_sigaction(sig, SIG_KTHREAD_KERNEL);
b4e74264
ON
292}
293
294static inline void disallow_signal(int sig)
295{
296 kernel_sigaction(sig, SIG_IGN);
297}
1da177e4 298
298ec1e2
CL
299extern struct kmem_cache *sighand_cachep;
300
abd4f750
MAS
301int unhandled_signal(struct task_struct *tsk, int sig);
302
55c0d1f8
RM
303/*
304 * In POSIX a signal is sent either to a specific thread (Linux task)
305 * or to the process as a whole (Linux thread group). How the signal
306 * is sent determines whether it's to one thread or the whole group,
307 * which determines which signal mask(s) are involved in blocking it
308 * from being delivered until later. When the signal is delivered,
309 * either it's caught or ignored by a user handler or it has a default
310 * effect that applies to the whole thread group (POSIX process).
311 *
312 * The possible effects an unblocked signal set to SIG_DFL can have are:
313 * ignore - Nothing Happens
314 * terminate - kill the process, i.e. all threads in the group,
315 * similar to exit_group. The group leader (only) reports
316 * WIFSIGNALED status to its parent.
317 * coredump - write a core dump file describing all threads using
318 * the same mm and then kill all those threads
319 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
320 *
321 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
322 * Other signals when not blocked and set to SIG_DFL behaves as follows.
323 * The job control signals also have other special effects.
324 *
325 * +--------------------+------------------+
326 * | POSIX signal | default action |
327 * +--------------------+------------------+
328 * | SIGHUP | terminate |
329 * | SIGINT | terminate |
330 * | SIGQUIT | coredump |
331 * | SIGILL | coredump |
332 * | SIGTRAP | coredump |
333 * | SIGABRT/SIGIOT | coredump |
334 * | SIGBUS | coredump |
335 * | SIGFPE | coredump |
336 * | SIGKILL | terminate(+) |
337 * | SIGUSR1 | terminate |
338 * | SIGSEGV | coredump |
339 * | SIGUSR2 | terminate |
340 * | SIGPIPE | terminate |
341 * | SIGALRM | terminate |
342 * | SIGTERM | terminate |
343 * | SIGCHLD | ignore |
344 * | SIGCONT | ignore(*) |
345 * | SIGSTOP | stop(*)(+) |
346 * | SIGTSTP | stop(*) |
347 * | SIGTTIN | stop(*) |
348 * | SIGTTOU | stop(*) |
349 * | SIGURG | ignore |
350 * | SIGXCPU | coredump |
351 * | SIGXFSZ | coredump |
352 * | SIGVTALRM | terminate |
353 * | SIGPROF | terminate |
354 * | SIGPOLL/SIGIO | terminate |
355 * | SIGSYS/SIGUNUSED | coredump |
356 * | SIGSTKFLT | terminate |
357 * | SIGWINCH | ignore |
358 * | SIGPWR | terminate |
359 * | SIGRTMIN-SIGRTMAX | terminate |
360 * +--------------------+------------------+
361 * | non-POSIX signal | default action |
362 * +--------------------+------------------+
363 * | SIGEMT | coredump |
364 * +--------------------+------------------+
365 *
366 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
367 * (*) Special job control effects:
368 * When SIGCONT is sent, it resumes the process (all threads in the group)
369 * from TASK_STOPPED state and also clears any pending/queued stop signals
370 * (any of those marked with "stop(*)"). This happens regardless of blocking,
371 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
372 * any pending/queued SIGCONT signals; this happens regardless of blocking,
373 * catching, or ignored the stop signal, though (except for SIGSTOP) the
374 * default action of stopping the process may happen later or never.
375 */
376
377#ifdef SIGEMT
378#define SIGEMT_MASK rt_sigmask(SIGEMT)
379#else
380#define SIGEMT_MASK 0
381#endif
382
383#if SIGRTMIN > BITS_PER_LONG
384#define rt_sigmask(sig) (1ULL << ((sig)-1))
385#else
386#define rt_sigmask(sig) sigmask(sig)
387#endif
5c8ccefd
ON
388
389#define siginmask(sig, mask) \
390 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
55c0d1f8
RM
391
392#define SIG_KERNEL_ONLY_MASK (\
393 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
394
395#define SIG_KERNEL_STOP_MASK (\
396 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
397 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
398
399#define SIG_KERNEL_COREDUMP_MASK (\
400 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
401 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
402 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
403 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
404 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
405 SIGEMT_MASK )
406
407#define SIG_KERNEL_IGNORE_MASK (\
408 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
409 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
410
d08477aa
EB
411#define SIG_SPECIFIC_SICODES_MASK (\
412 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
413 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
414 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
415 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
416 SIGEMT_MASK )
417
5c8ccefd
ON
418#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
419#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
420#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
421#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
d08477aa 422#define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
55c0d1f8 423
55c0d1f8
RM
424#define sig_fatal(t, signr) \
425 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
426 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
427
a1c9eea9
AB
428void signals_init(void);
429
5c49574f 430int restore_altstack(const stack_t __user *);
c40702c4 431int __save_altstack(stack_t __user *, unsigned long);
5c49574f 432
bd1c149a
AV
433#define save_altstack_ex(uss, sp) do { \
434 stack_t __user *__uss = uss; \
435 struct task_struct *t = current; \
436 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
2a742138 437 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
bd1c149a 438 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
2a742138
SS
439 if (t->sas_ss_flags & SS_AUTODISARM) \
440 sas_ss_reset(t); \
bd1c149a
AV
441} while (0);
442
34db8aaf
DH
443#ifdef CONFIG_PROC_FS
444struct seq_file;
445extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
446#endif
447
1da177e4 448#endif /* _LINUX_SIGNAL_H */