1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SIGNAL_H
3 #define _LINUX_SIGNAL_H
6 #include <linux/signal_types.h>
7 #include <linux/string.h>
12 extern int print_fatal_signals;
14 static inline void copy_siginfo(kernel_siginfo_t *to,
15 const kernel_siginfo_t *from)
17 memcpy(to, from, sizeof(*to));
20 static inline void clear_siginfo(kernel_siginfo_t *info)
22 memset(info, 0, sizeof(*info));
25 #define SI_EXPANSION_SIZE (sizeof(struct siginfo) - sizeof(struct kernel_siginfo))
27 static inline void copy_siginfo_to_external(siginfo_t *to,
28 const kernel_siginfo_t *from)
30 memcpy(to, from, sizeof(*from));
31 memset(((char *)to) + sizeof(struct kernel_siginfo), 0,
35 int copy_siginfo_to_user(siginfo_t __user *to, const kernel_siginfo_t *from);
36 int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from);
51 enum siginfo_layout siginfo_layout(unsigned sig, int si_code);
54 * Define some primitives to manipulate sigset_t.
57 #ifndef __HAVE_ARCH_SIG_BITOPS
58 #include <linux/bitops.h>
60 /* We don't use <linux/bitops.h> for these because there is no need to
62 static inline void sigaddset(sigset_t *set, int _sig)
64 unsigned long sig = _sig - 1;
66 set->sig[0] |= 1UL << sig;
68 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
71 static inline void sigdelset(sigset_t *set, int _sig)
73 unsigned long sig = _sig - 1;
75 set->sig[0] &= ~(1UL << sig);
77 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
80 static inline int sigismember(sigset_t *set, int _sig)
82 unsigned long sig = _sig - 1;
84 return 1 & (set->sig[0] >> sig);
86 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
89 #endif /* __HAVE_ARCH_SIG_BITOPS */
91 static inline int sigisemptyset(sigset_t *set)
93 switch (_NSIG_WORDS) {
95 return (set->sig[3] | set->sig[2] |
96 set->sig[1] | set->sig[0]) == 0;
98 return (set->sig[1] | set->sig[0]) == 0;
100 return set->sig[0] == 0;
107 static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
109 switch (_NSIG_WORDS) {
111 return (set1->sig[3] == set2->sig[3]) &&
112 (set1->sig[2] == set2->sig[2]) &&
113 (set1->sig[1] == set2->sig[1]) &&
114 (set1->sig[0] == set2->sig[0]);
116 return (set1->sig[1] == set2->sig[1]) &&
117 (set1->sig[0] == set2->sig[0]);
119 return set1->sig[0] == set2->sig[0];
124 #define sigmask(sig) (1UL << ((sig) - 1))
126 #ifndef __HAVE_ARCH_SIG_SETOPS
127 #include <linux/string.h>
129 #define _SIG_SET_BINOP(name, op) \
130 static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
132 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
134 switch (_NSIG_WORDS) { \
136 a3 = a->sig[3]; a2 = a->sig[2]; \
137 b3 = b->sig[3]; b2 = b->sig[2]; \
138 r->sig[3] = op(a3, b3); \
139 r->sig[2] = op(a2, b2); \
142 a1 = a->sig[1]; b1 = b->sig[1]; \
143 r->sig[1] = op(a1, b1); \
146 a0 = a->sig[0]; b0 = b->sig[0]; \
147 r->sig[0] = op(a0, b0); \
154 #define _sig_or(x,y) ((x) | (y))
155 _SIG_SET_BINOP(sigorsets, _sig_or)
157 #define _sig_and(x,y) ((x) & (y))
158 _SIG_SET_BINOP(sigandsets, _sig_and)
160 #define _sig_andn(x,y) ((x) & ~(y))
161 _SIG_SET_BINOP(sigandnsets, _sig_andn)
163 #undef _SIG_SET_BINOP
168 #define _SIG_SET_OP(name, op) \
169 static inline void name(sigset_t *set) \
171 switch (_NSIG_WORDS) { \
172 case 4: set->sig[3] = op(set->sig[3]); \
173 set->sig[2] = op(set->sig[2]); \
175 case 2: set->sig[1] = op(set->sig[1]); \
177 case 1: set->sig[0] = op(set->sig[0]); \
184 #define _sig_not(x) (~(x))
185 _SIG_SET_OP(signotset, _sig_not)
190 static inline void sigemptyset(sigset_t *set)
192 switch (_NSIG_WORDS) {
194 memset(set, 0, sizeof(sigset_t));
196 case 2: set->sig[1] = 0;
198 case 1: set->sig[0] = 0;
203 static inline void sigfillset(sigset_t *set)
205 switch (_NSIG_WORDS) {
207 memset(set, -1, sizeof(sigset_t));
209 case 2: set->sig[1] = -1;
211 case 1: set->sig[0] = -1;
216 /* Some extensions for manipulating the low 32 signals in particular. */
218 static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
223 static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
225 set->sig[0] &= ~mask;
228 static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
230 return (set->sig[0] & mask) != 0;
233 static inline void siginitset(sigset_t *set, unsigned long mask)
236 switch (_NSIG_WORDS) {
238 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
240 case 2: set->sig[1] = 0;
245 static inline void siginitsetinv(sigset_t *set, unsigned long mask)
248 switch (_NSIG_WORDS) {
250 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
252 case 2: set->sig[1] = -1;
257 #endif /* __HAVE_ARCH_SIG_SETOPS */
259 static inline void init_sigpending(struct sigpending *sig)
261 sigemptyset(&sig->signal);
262 INIT_LIST_HEAD(&sig->list);
265 extern void flush_sigqueue(struct sigpending *queue);
267 /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
268 static inline int valid_signal(unsigned long sig)
270 return sig <= _NSIG ? 1 : 0;
277 extern int next_signal(struct sigpending *pending, sigset_t *mask);
278 extern int do_send_sig_info(int sig, struct kernel_siginfo *info,
279 struct task_struct *p, enum pid_type type);
280 extern int group_send_sig_info(int sig, struct kernel_siginfo *info,
281 struct task_struct *p, enum pid_type type);
282 extern int __group_send_sig_info(int, struct kernel_siginfo *, struct task_struct *);
283 extern int sigprocmask(int, sigset_t *, sigset_t *);
284 extern void set_current_blocked(sigset_t *);
285 extern void __set_current_blocked(const sigset_t *);
286 extern int show_unhandled_signals;
288 extern bool get_signal(struct ksignal *ksig);
289 extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
290 extern void exit_signals(struct task_struct *tsk);
291 extern void kernel_sigaction(int, __sighandler_t);
293 #define SIG_KTHREAD ((__force __sighandler_t)2)
294 #define SIG_KTHREAD_KERNEL ((__force __sighandler_t)3)
296 static inline void allow_signal(int sig)
299 * Kernel threads handle their own signals. Let the signal code
300 * know it'll be handled, so that they don't get converted to
301 * SIGKILL or just silently dropped.
303 kernel_sigaction(sig, SIG_KTHREAD);
306 static inline void allow_kernel_signal(int sig)
309 * Kernel threads handle their own signals. Let the signal code
310 * know signals sent by the kernel will be handled, so that they
311 * don't get silently dropped.
313 kernel_sigaction(sig, SIG_KTHREAD_KERNEL);
316 static inline void disallow_signal(int sig)
318 kernel_sigaction(sig, SIG_IGN);
321 extern struct kmem_cache *sighand_cachep;
323 extern bool unhandled_signal(struct task_struct *tsk, int sig);
326 * In POSIX a signal is sent either to a specific thread (Linux task)
327 * or to the process as a whole (Linux thread group). How the signal
328 * is sent determines whether it's to one thread or the whole group,
329 * which determines which signal mask(s) are involved in blocking it
330 * from being delivered until later. When the signal is delivered,
331 * either it's caught or ignored by a user handler or it has a default
332 * effect that applies to the whole thread group (POSIX process).
334 * The possible effects an unblocked signal set to SIG_DFL can have are:
335 * ignore - Nothing Happens
336 * terminate - kill the process, i.e. all threads in the group,
337 * similar to exit_group. The group leader (only) reports
338 * WIFSIGNALED status to its parent.
339 * coredump - write a core dump file describing all threads using
340 * the same mm and then kill all those threads
341 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
343 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
344 * Other signals when not blocked and set to SIG_DFL behaves as follows.
345 * The job control signals also have other special effects.
347 * +--------------------+------------------+
348 * | POSIX signal | default action |
349 * +--------------------+------------------+
350 * | SIGHUP | terminate |
351 * | SIGINT | terminate |
352 * | SIGQUIT | coredump |
353 * | SIGILL | coredump |
354 * | SIGTRAP | coredump |
355 * | SIGABRT/SIGIOT | coredump |
356 * | SIGBUS | coredump |
357 * | SIGFPE | coredump |
358 * | SIGKILL | terminate(+) |
359 * | SIGUSR1 | terminate |
360 * | SIGSEGV | coredump |
361 * | SIGUSR2 | terminate |
362 * | SIGPIPE | terminate |
363 * | SIGALRM | terminate |
364 * | SIGTERM | terminate |
365 * | SIGCHLD | ignore |
366 * | SIGCONT | ignore(*) |
367 * | SIGSTOP | stop(*)(+) |
368 * | SIGTSTP | stop(*) |
369 * | SIGTTIN | stop(*) |
370 * | SIGTTOU | stop(*) |
371 * | SIGURG | ignore |
372 * | SIGXCPU | coredump |
373 * | SIGXFSZ | coredump |
374 * | SIGVTALRM | terminate |
375 * | SIGPROF | terminate |
376 * | SIGPOLL/SIGIO | terminate |
377 * | SIGSYS/SIGUNUSED | coredump |
378 * | SIGSTKFLT | terminate |
379 * | SIGWINCH | ignore |
380 * | SIGPWR | terminate |
381 * | SIGRTMIN-SIGRTMAX | terminate |
382 * +--------------------+------------------+
383 * | non-POSIX signal | default action |
384 * +--------------------+------------------+
385 * | SIGEMT | coredump |
386 * +--------------------+------------------+
388 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
389 * (*) Special job control effects:
390 * When SIGCONT is sent, it resumes the process (all threads in the group)
391 * from TASK_STOPPED state and also clears any pending/queued stop signals
392 * (any of those marked with "stop(*)"). This happens regardless of blocking,
393 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
394 * any pending/queued SIGCONT signals; this happens regardless of blocking,
395 * catching, or ignored the stop signal, though (except for SIGSTOP) the
396 * default action of stopping the process may happen later or never.
400 #define SIGEMT_MASK rt_sigmask(SIGEMT)
402 #define SIGEMT_MASK 0
405 #if SIGRTMIN > BITS_PER_LONG
406 #define rt_sigmask(sig) (1ULL << ((sig)-1))
408 #define rt_sigmask(sig) sigmask(sig)
411 #define siginmask(sig, mask) \
412 ((sig) > 0 && (sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
414 #define SIG_KERNEL_ONLY_MASK (\
415 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
417 #define SIG_KERNEL_STOP_MASK (\
418 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
419 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
421 #define SIG_KERNEL_COREDUMP_MASK (\
422 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
423 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
424 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
425 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
426 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
429 #define SIG_KERNEL_IGNORE_MASK (\
430 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
431 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
433 #define SIG_SPECIFIC_SICODES_MASK (\
434 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
435 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
436 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
437 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
440 #define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
441 #define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
442 #define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
443 #define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
444 #define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
446 #define sig_fatal(t, signr) \
447 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
448 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
450 void signals_init(void);
452 int restore_altstack(const stack_t __user *);
453 int __save_altstack(stack_t __user *, unsigned long);
455 #define unsafe_save_altstack(uss, sp, label) do { \
456 stack_t __user *__uss = uss; \
457 struct task_struct *t = current; \
458 unsafe_put_user((void __user *)t->sas_ss_sp, &__uss->ss_sp, label); \
459 unsafe_put_user(t->sas_ss_flags, &__uss->ss_flags, label); \
460 unsafe_put_user(t->sas_ss_size, &__uss->ss_size, label); \
461 if (t->sas_ss_flags & SS_AUTODISARM) \
465 #ifdef CONFIG_PROC_FS
467 extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
470 #endif /* _LINUX_SIGNAL_H */