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(struct siginfo *to, const struct siginfo *from)
16 memcpy(to, from, sizeof(*to));
19 static inline void clear_siginfo(struct siginfo *info)
21 memset(info, 0, sizeof(*info));
24 int copy_siginfo_to_user(struct siginfo __user *to, const struct siginfo *from);
39 enum siginfo_layout siginfo_layout(int sig, int si_code);
42 * Define some primitives to manipulate sigset_t.
45 #ifndef __HAVE_ARCH_SIG_BITOPS
46 #include <linux/bitops.h>
48 /* We don't use <linux/bitops.h> for these because there is no need to
50 static inline void sigaddset(sigset_t *set, int _sig)
52 unsigned long sig = _sig - 1;
54 set->sig[0] |= 1UL << sig;
56 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
59 static inline void sigdelset(sigset_t *set, int _sig)
61 unsigned long sig = _sig - 1;
63 set->sig[0] &= ~(1UL << sig);
65 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
68 static inline int sigismember(sigset_t *set, int _sig)
70 unsigned long sig = _sig - 1;
72 return 1 & (set->sig[0] >> sig);
74 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
77 #endif /* __HAVE_ARCH_SIG_BITOPS */
79 static inline int sigisemptyset(sigset_t *set)
81 switch (_NSIG_WORDS) {
83 return (set->sig[3] | set->sig[2] |
84 set->sig[1] | set->sig[0]) == 0;
86 return (set->sig[1] | set->sig[0]) == 0;
88 return set->sig[0] == 0;
95 static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
97 switch (_NSIG_WORDS) {
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]);
104 return (set1->sig[1] == set2->sig[1]) &&
105 (set1->sig[0] == set2->sig[0]);
107 return set1->sig[0] == set2->sig[0];
112 #define sigmask(sig) (1UL << ((sig) - 1))
114 #ifndef __HAVE_ARCH_SIG_SETOPS
115 #include <linux/string.h>
117 #define _SIG_SET_BINOP(name, op) \
118 static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
120 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
122 switch (_NSIG_WORDS) { \
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); \
129 a1 = a->sig[1]; b1 = b->sig[1]; \
130 r->sig[1] = op(a1, b1); \
132 a0 = a->sig[0]; b0 = b->sig[0]; \
133 r->sig[0] = op(a0, b0); \
140 #define _sig_or(x,y) ((x) | (y))
141 _SIG_SET_BINOP(sigorsets, _sig_or)
143 #define _sig_and(x,y) ((x) & (y))
144 _SIG_SET_BINOP(sigandsets, _sig_and)
146 #define _sig_andn(x,y) ((x) & ~(y))
147 _SIG_SET_BINOP(sigandnsets, _sig_andn)
149 #undef _SIG_SET_BINOP
154 #define _SIG_SET_OP(name, op) \
155 static inline void name(sigset_t *set) \
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]); \
168 #define _sig_not(x) (~(x))
169 _SIG_SET_OP(signotset, _sig_not)
174 static inline void sigemptyset(sigset_t *set)
176 switch (_NSIG_WORDS) {
178 memset(set, 0, sizeof(sigset_t));
180 case 2: set->sig[1] = 0;
181 case 1: set->sig[0] = 0;
186 static inline void sigfillset(sigset_t *set)
188 switch (_NSIG_WORDS) {
190 memset(set, -1, sizeof(sigset_t));
192 case 2: set->sig[1] = -1;
193 case 1: set->sig[0] = -1;
198 /* Some extensions for manipulating the low 32 signals in particular. */
200 static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
205 static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
207 set->sig[0] &= ~mask;
210 static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
212 return (set->sig[0] & mask) != 0;
215 static inline void siginitset(sigset_t *set, unsigned long mask)
218 switch (_NSIG_WORDS) {
220 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
222 case 2: set->sig[1] = 0;
227 static inline void siginitsetinv(sigset_t *set, unsigned long mask)
230 switch (_NSIG_WORDS) {
232 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
234 case 2: set->sig[1] = -1;
239 #endif /* __HAVE_ARCH_SIG_SETOPS */
241 static inline void init_sigpending(struct sigpending *sig)
243 sigemptyset(&sig->signal);
244 INIT_LIST_HEAD(&sig->list);
247 extern void flush_sigqueue(struct sigpending *queue);
249 /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
250 static inline int valid_signal(unsigned long sig)
252 return sig <= _NSIG ? 1 : 0;
259 extern int next_signal(struct sigpending *pending, sigset_t *mask);
260 extern int do_send_sig_info(int sig, struct siginfo *info,
261 struct task_struct *p, enum pid_type type);
262 extern int group_send_sig_info(int sig, struct siginfo *info,
263 struct task_struct *p, enum pid_type type);
264 extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
265 extern int sigprocmask(int, sigset_t *, sigset_t *);
266 extern void set_current_blocked(sigset_t *);
267 extern void __set_current_blocked(const sigset_t *);
268 extern int show_unhandled_signals;
270 extern bool get_signal(struct ksignal *ksig);
271 extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
272 extern void exit_signals(struct task_struct *tsk);
273 extern void kernel_sigaction(int, __sighandler_t);
275 static inline void allow_signal(int sig)
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.
282 kernel_sigaction(sig, (__force __sighandler_t)2);
285 static inline void disallow_signal(int sig)
287 kernel_sigaction(sig, SIG_IGN);
290 extern struct kmem_cache *sighand_cachep;
292 extern bool unhandled_signal(struct task_struct *tsk, int sig);
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).
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
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.
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 * +--------------------+------------------+
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.
369 #define SIGEMT_MASK rt_sigmask(SIGEMT)
371 #define SIGEMT_MASK 0
374 #if SIGRTMIN > BITS_PER_LONG
375 #define rt_sigmask(sig) (1ULL << ((sig)-1))
377 #define rt_sigmask(sig) sigmask(sig)
380 #define siginmask(sig, mask) \
381 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
383 #define SIG_KERNEL_ONLY_MASK (\
384 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
386 #define SIG_KERNEL_STOP_MASK (\
387 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
388 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
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) | \
398 #define SIG_KERNEL_IGNORE_MASK (\
399 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
400 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
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) | \
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)
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)
419 void signals_init(void);
421 int restore_altstack(const stack_t __user *);
422 int __save_altstack(stack_t __user *, unsigned long);
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) \
434 #ifdef CONFIG_PROC_FS
436 extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
439 #endif /* _LINUX_SIGNAL_H */