1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SIGNAL_H
3 #define _LINUX_SIGNAL_H
6 #include <linux/list.h>
7 #include <linux/signal_types.h>
8 #include <linux/string.h>
13 extern int print_fatal_signals
;
15 static inline void copy_siginfo(kernel_siginfo_t
*to
,
16 const kernel_siginfo_t
*from
)
18 memcpy(to
, from
, sizeof(*to
));
21 static inline void clear_siginfo(kernel_siginfo_t
*info
)
23 memset(info
, 0, sizeof(*info
));
26 #define SI_EXPANSION_SIZE (sizeof(struct siginfo) - sizeof(struct kernel_siginfo))
28 static inline void copy_siginfo_to_external(siginfo_t
*to
,
29 const kernel_siginfo_t
*from
)
31 memcpy(to
, from
, sizeof(*from
));
32 memset(((char *)to
) + sizeof(struct kernel_siginfo
), 0,
36 int copy_siginfo_to_user(siginfo_t __user
*to
, const kernel_siginfo_t
*from
);
37 int copy_siginfo_from_user(kernel_siginfo_t
*to
, const siginfo_t __user
*from
);
54 enum siginfo_layout
siginfo_layout(unsigned sig
, int si_code
);
57 * Define some primitives to manipulate sigset_t.
60 #ifndef __HAVE_ARCH_SIG_BITOPS
61 #include <linux/bitops.h>
63 /* We don't use <linux/bitops.h> for these because there is no need to
65 static inline void sigaddset(sigset_t
*set
, int _sig
)
67 unsigned long sig
= _sig
- 1;
69 set
->sig
[0] |= 1UL << sig
;
71 set
->sig
[sig
/ _NSIG_BPW
] |= 1UL << (sig
% _NSIG_BPW
);
74 static inline void sigdelset(sigset_t
*set
, int _sig
)
76 unsigned long sig
= _sig
- 1;
78 set
->sig
[0] &= ~(1UL << sig
);
80 set
->sig
[sig
/ _NSIG_BPW
] &= ~(1UL << (sig
% _NSIG_BPW
));
83 static inline int sigismember(sigset_t
*set
, int _sig
)
85 unsigned long sig
= _sig
- 1;
87 return 1 & (set
->sig
[0] >> sig
);
89 return 1 & (set
->sig
[sig
/ _NSIG_BPW
] >> (sig
% _NSIG_BPW
));
92 #endif /* __HAVE_ARCH_SIG_BITOPS */
94 static inline int sigisemptyset(sigset_t
*set
)
96 switch (_NSIG_WORDS
) {
98 return (set
->sig
[3] | set
->sig
[2] |
99 set
->sig
[1] | set
->sig
[0]) == 0;
101 return (set
->sig
[1] | set
->sig
[0]) == 0;
103 return set
->sig
[0] == 0;
110 static inline int sigequalsets(const sigset_t
*set1
, const sigset_t
*set2
)
112 switch (_NSIG_WORDS
) {
114 return (set1
->sig
[3] == set2
->sig
[3]) &&
115 (set1
->sig
[2] == set2
->sig
[2]) &&
116 (set1
->sig
[1] == set2
->sig
[1]) &&
117 (set1
->sig
[0] == set2
->sig
[0]);
119 return (set1
->sig
[1] == set2
->sig
[1]) &&
120 (set1
->sig
[0] == set2
->sig
[0]);
122 return set1
->sig
[0] == set2
->sig
[0];
127 #define sigmask(sig) (1UL << ((sig) - 1))
129 #ifndef __HAVE_ARCH_SIG_SETOPS
131 #define _SIG_SET_BINOP(name, op) \
132 static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
134 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
136 switch (_NSIG_WORDS) { \
138 a3 = a->sig[3]; a2 = a->sig[2]; \
139 b3 = b->sig[3]; b2 = b->sig[2]; \
140 r->sig[3] = op(a3, b3); \
141 r->sig[2] = op(a2, b2); \
144 a1 = a->sig[1]; b1 = b->sig[1]; \
145 r->sig[1] = op(a1, b1); \
148 a0 = a->sig[0]; b0 = b->sig[0]; \
149 r->sig[0] = op(a0, b0); \
156 #define _sig_or(x,y) ((x) | (y))
157 _SIG_SET_BINOP(sigorsets
, _sig_or
)
159 #define _sig_and(x,y) ((x) & (y))
160 _SIG_SET_BINOP(sigandsets
, _sig_and
)
162 #define _sig_andn(x,y) ((x) & ~(y))
163 _SIG_SET_BINOP(sigandnsets
, _sig_andn
)
165 #undef _SIG_SET_BINOP
170 #define _SIG_SET_OP(name, op) \
171 static inline void name(sigset_t *set) \
173 switch (_NSIG_WORDS) { \
174 case 4: set->sig[3] = op(set->sig[3]); \
175 set->sig[2] = op(set->sig[2]); \
177 case 2: set->sig[1] = op(set->sig[1]); \
179 case 1: set->sig[0] = op(set->sig[0]); \
186 #define _sig_not(x) (~(x))
187 _SIG_SET_OP(signotset
, _sig_not
)
192 static inline void sigemptyset(sigset_t
*set
)
194 switch (_NSIG_WORDS
) {
196 memset(set
, 0, sizeof(sigset_t
));
198 case 2: set
->sig
[1] = 0;
200 case 1: set
->sig
[0] = 0;
205 static inline void sigfillset(sigset_t
*set
)
207 switch (_NSIG_WORDS
) {
209 memset(set
, -1, sizeof(sigset_t
));
211 case 2: set
->sig
[1] = -1;
213 case 1: set
->sig
[0] = -1;
218 /* Some extensions for manipulating the low 32 signals in particular. */
220 static inline void sigaddsetmask(sigset_t
*set
, unsigned long mask
)
225 static inline void sigdelsetmask(sigset_t
*set
, unsigned long mask
)
227 set
->sig
[0] &= ~mask
;
230 static inline int sigtestsetmask(sigset_t
*set
, unsigned long mask
)
232 return (set
->sig
[0] & mask
) != 0;
235 static inline void siginitset(sigset_t
*set
, unsigned long mask
)
238 switch (_NSIG_WORDS
) {
240 memset(&set
->sig
[1], 0, sizeof(long)*(_NSIG_WORDS
-1));
242 case 2: set
->sig
[1] = 0;
248 static inline void siginitsetinv(sigset_t
*set
, unsigned long mask
)
251 switch (_NSIG_WORDS
) {
253 memset(&set
->sig
[1], -1, sizeof(long)*(_NSIG_WORDS
-1));
255 case 2: set
->sig
[1] = -1;
261 #endif /* __HAVE_ARCH_SIG_SETOPS */
263 static inline void init_sigpending(struct sigpending
*sig
)
265 sigemptyset(&sig
->signal
);
266 INIT_LIST_HEAD(&sig
->list
);
269 extern void flush_sigqueue(struct sigpending
*queue
);
271 /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
272 static inline int valid_signal(unsigned long sig
)
274 return sig
<= _NSIG
? 1 : 0;
281 extern int next_signal(struct sigpending
*pending
, sigset_t
*mask
);
282 extern int do_send_sig_info(int sig
, struct kernel_siginfo
*info
,
283 struct task_struct
*p
, enum pid_type type
);
284 extern int group_send_sig_info(int sig
, struct kernel_siginfo
*info
,
285 struct task_struct
*p
, enum pid_type type
);
286 extern int send_signal_locked(int sig
, struct kernel_siginfo
*info
,
287 struct task_struct
*p
, enum pid_type type
);
288 extern int sigprocmask(int, sigset_t
*, sigset_t
*);
289 extern void set_current_blocked(sigset_t
*);
290 extern void __set_current_blocked(const sigset_t
*);
291 extern int show_unhandled_signals
;
293 extern bool get_signal(struct ksignal
*ksig
);
294 extern void signal_setup_done(int failed
, struct ksignal
*ksig
, int stepping
);
295 extern void exit_signals(struct task_struct
*tsk
);
296 extern void kernel_sigaction(int, __sighandler_t
);
298 #define SIG_KTHREAD ((__force __sighandler_t)2)
299 #define SIG_KTHREAD_KERNEL ((__force __sighandler_t)3)
301 static inline void allow_signal(int sig
)
304 * Kernel threads handle their own signals. Let the signal code
305 * know it'll be handled, so that they don't get converted to
306 * SIGKILL or just silently dropped.
308 kernel_sigaction(sig
, SIG_KTHREAD
);
311 static inline void allow_kernel_signal(int sig
)
314 * Kernel threads handle their own signals. Let the signal code
315 * know signals sent by the kernel will be handled, so that they
316 * don't get silently dropped.
318 kernel_sigaction(sig
, SIG_KTHREAD_KERNEL
);
321 static inline void disallow_signal(int sig
)
323 kernel_sigaction(sig
, SIG_IGN
);
326 extern struct kmem_cache
*sighand_cachep
;
328 extern bool unhandled_signal(struct task_struct
*tsk
, int sig
);
331 * In POSIX a signal is sent either to a specific thread (Linux task)
332 * or to the process as a whole (Linux thread group). How the signal
333 * is sent determines whether it's to one thread or the whole group,
334 * which determines which signal mask(s) are involved in blocking it
335 * from being delivered until later. When the signal is delivered,
336 * either it's caught or ignored by a user handler or it has a default
337 * effect that applies to the whole thread group (POSIX process).
339 * The possible effects an unblocked signal set to SIG_DFL can have are:
340 * ignore - Nothing Happens
341 * terminate - kill the process, i.e. all threads in the group,
342 * similar to exit_group. The group leader (only) reports
343 * WIFSIGNALED status to its parent.
344 * coredump - write a core dump file describing all threads using
345 * the same mm and then kill all those threads
346 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
348 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
349 * Other signals when not blocked and set to SIG_DFL behaves as follows.
350 * The job control signals also have other special effects.
352 * +--------------------+------------------+
353 * | POSIX signal | default action |
354 * +--------------------+------------------+
355 * | SIGHUP | terminate |
356 * | SIGINT | terminate |
357 * | SIGQUIT | coredump |
358 * | SIGILL | coredump |
359 * | SIGTRAP | coredump |
360 * | SIGABRT/SIGIOT | coredump |
361 * | SIGBUS | coredump |
362 * | SIGFPE | coredump |
363 * | SIGKILL | terminate(+) |
364 * | SIGUSR1 | terminate |
365 * | SIGSEGV | coredump |
366 * | SIGUSR2 | terminate |
367 * | SIGPIPE | terminate |
368 * | SIGALRM | terminate |
369 * | SIGTERM | terminate |
370 * | SIGCHLD | ignore |
371 * | SIGCONT | ignore(*) |
372 * | SIGSTOP | stop(*)(+) |
373 * | SIGTSTP | stop(*) |
374 * | SIGTTIN | stop(*) |
375 * | SIGTTOU | stop(*) |
376 * | SIGURG | ignore |
377 * | SIGXCPU | coredump |
378 * | SIGXFSZ | coredump |
379 * | SIGVTALRM | terminate |
380 * | SIGPROF | terminate |
381 * | SIGPOLL/SIGIO | terminate |
382 * | SIGSYS/SIGUNUSED | coredump |
383 * | SIGSTKFLT | terminate |
384 * | SIGWINCH | ignore |
385 * | SIGPWR | terminate |
386 * | SIGRTMIN-SIGRTMAX | terminate |
387 * +--------------------+------------------+
388 * | non-POSIX signal | default action |
389 * +--------------------+------------------+
390 * | SIGEMT | coredump |
391 * +--------------------+------------------+
393 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
394 * (*) Special job control effects:
395 * When SIGCONT is sent, it resumes the process (all threads in the group)
396 * from TASK_STOPPED state and also clears any pending/queued stop signals
397 * (any of those marked with "stop(*)"). This happens regardless of blocking,
398 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
399 * any pending/queued SIGCONT signals; this happens regardless of blocking,
400 * catching, or ignored the stop signal, though (except for SIGSTOP) the
401 * default action of stopping the process may happen later or never.
405 #define SIGEMT_MASK rt_sigmask(SIGEMT)
407 #define SIGEMT_MASK 0
410 #if SIGRTMIN > BITS_PER_LONG
411 #define rt_sigmask(sig) (1ULL << ((sig)-1))
413 #define rt_sigmask(sig) sigmask(sig)
416 #define siginmask(sig, mask) \
417 ((sig) > 0 && (sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
419 #define SIG_KERNEL_ONLY_MASK (\
420 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
422 #define SIG_KERNEL_STOP_MASK (\
423 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
424 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
426 #define SIG_KERNEL_COREDUMP_MASK (\
427 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
428 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
429 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
430 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
431 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
434 #define SIG_KERNEL_IGNORE_MASK (\
435 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
436 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
438 #define SIG_SPECIFIC_SICODES_MASK (\
439 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
440 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
441 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
442 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
445 #define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
446 #define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
447 #define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
448 #define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
449 #define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
451 #define sig_fatal(t, signr) \
452 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
453 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
455 void signals_init(void);
457 int restore_altstack(const stack_t __user
*);
458 int __save_altstack(stack_t __user
*, unsigned long);
460 #define unsafe_save_altstack(uss, sp, label) do { \
461 stack_t __user *__uss = uss; \
462 struct task_struct *t = current; \
463 unsafe_put_user((void __user *)t->sas_ss_sp, &__uss->ss_sp, label); \
464 unsafe_put_user(t->sas_ss_flags, &__uss->ss_flags, label); \
465 unsafe_put_user(t->sas_ss_size, &__uss->ss_size, label); \
468 #ifdef CONFIG_DYNAMIC_SIGFRAME
469 bool sigaltstack_size_valid(size_t ss_size
);
471 static inline bool sigaltstack_size_valid(size_t size
) { return true; }
472 #endif /* !CONFIG_DYNAMIC_SIGFRAME */
474 #ifdef CONFIG_PROC_FS
476 extern void render_sigset_t(struct seq_file
*, const char *, sigset_t
*);
479 #ifndef arch_untagged_si_addr
481 * Given a fault address and a signal and si_code which correspond to the
482 * _sigfault union member, returns the address that must appear in si_addr if
483 * the signal handler does not have SA_EXPOSE_TAGBITS enabled in sa_flags.
485 static inline void __user
*arch_untagged_si_addr(void __user
*addr
,
487 unsigned long si_code
)
493 #endif /* _LINUX_SIGNAL_H */