1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_SIGNAL_H
3#define _LINUX_SIGNAL_H
4
5#include <linux/bug.h>
6#include <linux/signal_types.h>
7#include <linux/string.h>
8
9struct task_struct;
10
11/* for sysctl */
12extern int print_fatal_signals;
13
14static inline void copy_siginfo(struct siginfo *to, const struct siginfo *from)
15{
16 memcpy(to, from, sizeof(*to));
17}
18
19static inline void clear_siginfo(struct siginfo *info)
20{
21 memset(info, 0, sizeof(*info));
22}
23
24int copy_siginfo_to_user(struct siginfo __user *to, const struct siginfo *from);
25
26enum siginfo_layout {
27 SIL_KILL,
28 SIL_TIMER,
29 SIL_POLL,
30 SIL_FAULT,
31 SIL_FAULT_MCEERR,
32 SIL_FAULT_BNDERR,
33 SIL_FAULT_PKUERR,
34 SIL_CHLD,
35 SIL_RT,
36 SIL_SYS,
37};
38
39enum siginfo_layout siginfo_layout(int sig, int si_code);
40
41/*
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
77#endif /* __HAVE_ARCH_SIG_BITOPS */
78
79static inline int sigisemptyset(sigset_t *set)
80{
81 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:
90 BUILD_BUG();
91 return 0;
92 }
93}
94
95static 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
112#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{ \
120 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
121 \
122 switch (_NSIG_WORDS) { \
123 case 4: \
124 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); \
128 case 2: \
129 a1 = a->sig[1]; b1 = b->sig[1]; \
130 r->sig[1] = op(a1, b1); \
131 case 1: \
132 a0 = a->sig[0]; b0 = b->sig[0]; \
133 r->sig[0] = op(a0, b0); \
134 break; \
135 default: \
136 BUILD_BUG(); \
137 } \
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
146#define _sig_andn(x,y) ((x) & ~(y))
147_SIG_SET_BINOP(sigandnsets, _sig_andn)
148
149#undef _SIG_SET_BINOP
150#undef _sig_or
151#undef _sig_and
152#undef _sig_andn
153
154#define _SIG_SET_OP(name, op) \
155static inline void name(sigset_t *set) \
156{ \
157 switch (_NSIG_WORDS) { \
158 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]); \
162 break; \
163 default: \
164 BUILD_BUG(); \
165 } \
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
247extern void flush_sigqueue(struct sigpending *queue);
248
249/* 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
255struct timespec;
256struct pt_regs;
257enum pid_type;
258
259extern int next_signal(struct sigpending *pending, sigset_t *mask);
260extern int do_send_sig_info(int sig, struct siginfo *info,
261 struct task_struct *p, enum pid_type type);
262extern int group_send_sig_info(int sig, struct siginfo *info,
263 struct task_struct *p, enum pid_type type);
264extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
265extern int sigprocmask(int, sigset_t *, sigset_t *);
266extern void set_current_blocked(sigset_t *);
267extern void __set_current_blocked(const sigset_t *);
268extern int show_unhandled_signals;
269
270extern int get_signal(struct ksignal *ksig);
271extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
272extern void exit_signals(struct task_struct *tsk);
273extern void kernel_sigaction(int, __sighandler_t);
274
275static inline void allow_signal(int sig)
276{
277 /*
278 * Kernel threads handle their own signals. Let the signal code
279 * know it'll be handled, so that they don't get converted to
280 * SIGKILL or just silently dropped.
281 */
282 kernel_sigaction(sig, (__force __sighandler_t)2);
283}
284
285static inline void disallow_signal(int sig)
286{
287 kernel_sigaction(sig, SIG_IGN);
288}
289
290extern struct kmem_cache *sighand_cachep;
291
292int unhandled_signal(struct task_struct *tsk, int sig);
293
294/*
295 * In POSIX a signal is sent either to a specific thread (Linux task)
296 * or to the process as a whole (Linux thread group). How the signal
297 * is sent determines whether it's to one thread or the whole group,
298 * which determines which signal mask(s) are involved in blocking it
299 * from being delivered until later. When the signal is delivered,
300 * either it's caught or ignored by a user handler or it has a default
301 * effect that applies to the whole thread group (POSIX process).
302 *
303 * The possible effects an unblocked signal set to SIG_DFL can have are:
304 * ignore - Nothing Happens
305 * terminate - kill the process, i.e. all threads in the group,
306 * similar to exit_group. The group leader (only) reports
307 * WIFSIGNALED status to its parent.
308 * coredump - write a core dump file describing all threads using
309 * the same mm and then kill all those threads
310 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
311 *
312 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
313 * Other signals when not blocked and set to SIG_DFL behaves as follows.
314 * The job control signals also have other special effects.
315 *
316 * +--------------------+------------------+
317 * | POSIX signal | default action |
318 * +--------------------+------------------+
319 * | SIGHUP | terminate |
320 * | SIGINT | terminate |
321 * | SIGQUIT | coredump |
322 * | SIGILL | coredump |
323 * | SIGTRAP | coredump |
324 * | SIGABRT/SIGIOT | coredump |
325 * | SIGBUS | coredump |
326 * | SIGFPE | coredump |
327 * | SIGKILL | terminate(+) |
328 * | SIGUSR1 | terminate |
329 * | SIGSEGV | coredump |
330 * | SIGUSR2 | terminate |
331 * | SIGPIPE | terminate |
332 * | SIGALRM | terminate |
333 * | SIGTERM | terminate |
334 * | SIGCHLD | ignore |
335 * | SIGCONT | ignore(*) |
336 * | SIGSTOP | stop(*)(+) |
337 * | SIGTSTP | stop(*) |
338 * | SIGTTIN | stop(*) |
339 * | SIGTTOU | stop(*) |
340 * | SIGURG | ignore |
341 * | SIGXCPU | coredump |
342 * | SIGXFSZ | coredump |
343 * | SIGVTALRM | terminate |
344 * | SIGPROF | terminate |
345 * | SIGPOLL/SIGIO | terminate |
346 * | SIGSYS/SIGUNUSED | coredump |
347 * | SIGSTKFLT | terminate |
348 * | SIGWINCH | ignore |
349 * | SIGPWR | terminate |
350 * | SIGRTMIN-SIGRTMAX | terminate |
351 * +--------------------+------------------+
352 * | non-POSIX signal | default action |
353 * +--------------------+------------------+
354 * | SIGEMT | coredump |
355 * +--------------------+------------------+
356 *
357 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
358 * (*) Special job control effects:
359 * When SIGCONT is sent, it resumes the process (all threads in the group)
360 * from TASK_STOPPED state and also clears any pending/queued stop signals
361 * (any of those marked with "stop(*)"). This happens regardless of blocking,
362 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
363 * any pending/queued SIGCONT signals; this happens regardless of blocking,
364 * catching, or ignored the stop signal, though (except for SIGSTOP) the
365 * default action of stopping the process may happen later or never.
366 */
367
368#ifdef SIGEMT
369#define SIGEMT_MASK rt_sigmask(SIGEMT)
370#else
371#define SIGEMT_MASK 0
372#endif
373
374#if SIGRTMIN > BITS_PER_LONG
375#define rt_sigmask(sig) (1ULL << ((sig)-1))
376#else
377#define rt_sigmask(sig) sigmask(sig)
378#endif
379
380#define siginmask(sig, mask) \
381 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
382
383#define SIG_KERNEL_ONLY_MASK (\
384 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
385
386#define SIG_KERNEL_STOP_MASK (\
387 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
388 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
389
390#define SIG_KERNEL_COREDUMP_MASK (\
391 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
392 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
393 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
394 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
395 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
396 SIGEMT_MASK )
397
398#define SIG_KERNEL_IGNORE_MASK (\
399 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
400 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
401
402#define SIG_SPECIFIC_SICODES_MASK (\
403 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
404 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
405 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
406 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
407 SIGEMT_MASK )
408
409#define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
410#define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
411#define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
412#define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
413#define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
414
415#define sig_fatal(t, signr) \
416 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
417 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
418
419void signals_init(void);
420
421int restore_altstack(const stack_t __user *);
422int __save_altstack(stack_t __user *, unsigned long);
423
424#define save_altstack_ex(uss, sp) do { \
425 stack_t __user *__uss = uss; \
426 struct task_struct *t = current; \
427 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
428 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
429 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
430 if (t->sas_ss_flags & SS_AUTODISARM) \
431 sas_ss_reset(t); \
432} while (0);
433
434#ifdef CONFIG_PROC_FS
435struct seq_file;
436extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
437#endif
438
439#endif /* _LINUX_SIGNAL_H */
440