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
);
36 enum siginfo_layout
siginfo_layout(int sig
, int si_code
);
39 * Define some primitives to manipulate sigset_t.
42 #ifndef __HAVE_ARCH_SIG_BITOPS
43 #include <linux/bitops.h>
45 /* We don't use <linux/bitops.h> for these because there is no need to
47 static inline void sigaddset(sigset_t
*set
, int _sig
)
49 unsigned long sig
= _sig
- 1;
51 set
->sig
[0] |= 1UL << sig
;
53 set
->sig
[sig
/ _NSIG_BPW
] |= 1UL << (sig
% _NSIG_BPW
);
56 static inline void sigdelset(sigset_t
*set
, int _sig
)
58 unsigned long sig
= _sig
- 1;
60 set
->sig
[0] &= ~(1UL << sig
);
62 set
->sig
[sig
/ _NSIG_BPW
] &= ~(1UL << (sig
% _NSIG_BPW
));
65 static inline int sigismember(sigset_t
*set
, int _sig
)
67 unsigned long sig
= _sig
- 1;
69 return 1 & (set
->sig
[0] >> sig
);
71 return 1 & (set
->sig
[sig
/ _NSIG_BPW
] >> (sig
% _NSIG_BPW
));
74 #endif /* __HAVE_ARCH_SIG_BITOPS */
76 static inline int sigisemptyset(sigset_t
*set
)
78 switch (_NSIG_WORDS
) {
80 return (set
->sig
[3] | set
->sig
[2] |
81 set
->sig
[1] | set
->sig
[0]) == 0;
83 return (set
->sig
[1] | set
->sig
[0]) == 0;
85 return set
->sig
[0] == 0;
92 static inline int sigequalsets(const sigset_t
*set1
, const sigset_t
*set2
)
94 switch (_NSIG_WORDS
) {
96 return (set1
->sig
[3] == set2
->sig
[3]) &&
97 (set1
->sig
[2] == set2
->sig
[2]) &&
98 (set1
->sig
[1] == set2
->sig
[1]) &&
99 (set1
->sig
[0] == set2
->sig
[0]);
101 return (set1
->sig
[1] == set2
->sig
[1]) &&
102 (set1
->sig
[0] == set2
->sig
[0]);
104 return set1
->sig
[0] == set2
->sig
[0];
109 #define sigmask(sig) (1UL << ((sig) - 1))
111 #ifndef __HAVE_ARCH_SIG_SETOPS
112 #include <linux/string.h>
114 #define _SIG_SET_BINOP(name, op) \
115 static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
117 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \
119 switch (_NSIG_WORDS) { \
121 a3 = a->sig[3]; a2 = a->sig[2]; \
122 b3 = b->sig[3]; b2 = b->sig[2]; \
123 r->sig[3] = op(a3, b3); \
124 r->sig[2] = op(a2, b2); \
126 a1 = a->sig[1]; b1 = b->sig[1]; \
127 r->sig[1] = op(a1, b1); \
129 a0 = a->sig[0]; b0 = b->sig[0]; \
130 r->sig[0] = op(a0, b0); \
137 #define _sig_or(x,y) ((x) | (y))
138 _SIG_SET_BINOP(sigorsets
, _sig_or
)
140 #define _sig_and(x,y) ((x) & (y))
141 _SIG_SET_BINOP(sigandsets
, _sig_and
)
143 #define _sig_andn(x,y) ((x) & ~(y))
144 _SIG_SET_BINOP(sigandnsets
, _sig_andn
)
146 #undef _SIG_SET_BINOP
151 #define _SIG_SET_OP(name, op) \
152 static inline void name(sigset_t *set) \
154 switch (_NSIG_WORDS) { \
155 case 4: set->sig[3] = op(set->sig[3]); \
156 set->sig[2] = op(set->sig[2]); \
157 case 2: set->sig[1] = op(set->sig[1]); \
158 case 1: set->sig[0] = op(set->sig[0]); \
165 #define _sig_not(x) (~(x))
166 _SIG_SET_OP(signotset
, _sig_not
)
171 static inline void sigemptyset(sigset_t
*set
)
173 switch (_NSIG_WORDS
) {
175 memset(set
, 0, sizeof(sigset_t
));
177 case 2: set
->sig
[1] = 0;
178 case 1: set
->sig
[0] = 0;
183 static inline void sigfillset(sigset_t
*set
)
185 switch (_NSIG_WORDS
) {
187 memset(set
, -1, sizeof(sigset_t
));
189 case 2: set
->sig
[1] = -1;
190 case 1: set
->sig
[0] = -1;
195 /* Some extensions for manipulating the low 32 signals in particular. */
197 static inline void sigaddsetmask(sigset_t
*set
, unsigned long mask
)
202 static inline void sigdelsetmask(sigset_t
*set
, unsigned long mask
)
204 set
->sig
[0] &= ~mask
;
207 static inline int sigtestsetmask(sigset_t
*set
, unsigned long mask
)
209 return (set
->sig
[0] & mask
) != 0;
212 static inline void siginitset(sigset_t
*set
, unsigned long mask
)
215 switch (_NSIG_WORDS
) {
217 memset(&set
->sig
[1], 0, sizeof(long)*(_NSIG_WORDS
-1));
219 case 2: set
->sig
[1] = 0;
224 static inline void siginitsetinv(sigset_t
*set
, unsigned long mask
)
227 switch (_NSIG_WORDS
) {
229 memset(&set
->sig
[1], -1, sizeof(long)*(_NSIG_WORDS
-1));
231 case 2: set
->sig
[1] = -1;
236 #endif /* __HAVE_ARCH_SIG_SETOPS */
238 static inline void init_sigpending(struct sigpending
*sig
)
240 sigemptyset(&sig
->signal
);
241 INIT_LIST_HEAD(&sig
->list
);
244 extern void flush_sigqueue(struct sigpending
*queue
);
246 /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
247 static inline int valid_signal(unsigned long sig
)
249 return sig
<= _NSIG
? 1 : 0;
255 extern int next_signal(struct sigpending
*pending
, sigset_t
*mask
);
256 extern int do_send_sig_info(int sig
, struct siginfo
*info
,
257 struct task_struct
*p
, bool group
);
258 extern int group_send_sig_info(int sig
, struct siginfo
*info
, struct task_struct
*p
);
259 extern int __group_send_sig_info(int, struct siginfo
*, struct task_struct
*);
260 extern int sigprocmask(int, sigset_t
*, sigset_t
*);
261 extern void set_current_blocked(sigset_t
*);
262 extern void __set_current_blocked(const sigset_t
*);
263 extern int show_unhandled_signals
;
265 extern int get_signal(struct ksignal
*ksig
);
266 extern void signal_setup_done(int failed
, struct ksignal
*ksig
, int stepping
);
267 extern void exit_signals(struct task_struct
*tsk
);
268 extern void kernel_sigaction(int, __sighandler_t
);
270 static inline void allow_signal(int sig
)
273 * Kernel threads handle their own signals. Let the signal code
274 * know it'll be handled, so that they don't get converted to
275 * SIGKILL or just silently dropped.
277 kernel_sigaction(sig
, (__force __sighandler_t
)2);
280 static inline void disallow_signal(int sig
)
282 kernel_sigaction(sig
, SIG_IGN
);
285 extern struct kmem_cache
*sighand_cachep
;
287 int unhandled_signal(struct task_struct
*tsk
, int sig
);
290 * In POSIX a signal is sent either to a specific thread (Linux task)
291 * or to the process as a whole (Linux thread group). How the signal
292 * is sent determines whether it's to one thread or the whole group,
293 * which determines which signal mask(s) are involved in blocking it
294 * from being delivered until later. When the signal is delivered,
295 * either it's caught or ignored by a user handler or it has a default
296 * effect that applies to the whole thread group (POSIX process).
298 * The possible effects an unblocked signal set to SIG_DFL can have are:
299 * ignore - Nothing Happens
300 * terminate - kill the process, i.e. all threads in the group,
301 * similar to exit_group. The group leader (only) reports
302 * WIFSIGNALED status to its parent.
303 * coredump - write a core dump file describing all threads using
304 * the same mm and then kill all those threads
305 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
307 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
308 * Other signals when not blocked and set to SIG_DFL behaves as follows.
309 * The job control signals also have other special effects.
311 * +--------------------+------------------+
312 * | POSIX signal | default action |
313 * +--------------------+------------------+
314 * | SIGHUP | terminate |
315 * | SIGINT | terminate |
316 * | SIGQUIT | coredump |
317 * | SIGILL | coredump |
318 * | SIGTRAP | coredump |
319 * | SIGABRT/SIGIOT | coredump |
320 * | SIGBUS | coredump |
321 * | SIGFPE | coredump |
322 * | SIGKILL | terminate(+) |
323 * | SIGUSR1 | terminate |
324 * | SIGSEGV | coredump |
325 * | SIGUSR2 | terminate |
326 * | SIGPIPE | terminate |
327 * | SIGALRM | terminate |
328 * | SIGTERM | terminate |
329 * | SIGCHLD | ignore |
330 * | SIGCONT | ignore(*) |
331 * | SIGSTOP | stop(*)(+) |
332 * | SIGTSTP | stop(*) |
333 * | SIGTTIN | stop(*) |
334 * | SIGTTOU | stop(*) |
335 * | SIGURG | ignore |
336 * | SIGXCPU | coredump |
337 * | SIGXFSZ | coredump |
338 * | SIGVTALRM | terminate |
339 * | SIGPROF | terminate |
340 * | SIGPOLL/SIGIO | terminate |
341 * | SIGSYS/SIGUNUSED | coredump |
342 * | SIGSTKFLT | terminate |
343 * | SIGWINCH | ignore |
344 * | SIGPWR | terminate |
345 * | SIGRTMIN-SIGRTMAX | terminate |
346 * +--------------------+------------------+
347 * | non-POSIX signal | default action |
348 * +--------------------+------------------+
349 * | SIGEMT | coredump |
350 * +--------------------+------------------+
352 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
353 * (*) Special job control effects:
354 * When SIGCONT is sent, it resumes the process (all threads in the group)
355 * from TASK_STOPPED state and also clears any pending/queued stop signals
356 * (any of those marked with "stop(*)"). This happens regardless of blocking,
357 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
358 * any pending/queued SIGCONT signals; this happens regardless of blocking,
359 * catching, or ignored the stop signal, though (except for SIGSTOP) the
360 * default action of stopping the process may happen later or never.
364 #define SIGEMT_MASK rt_sigmask(SIGEMT)
366 #define SIGEMT_MASK 0
369 #if SIGRTMIN > BITS_PER_LONG
370 #define rt_sigmask(sig) (1ULL << ((sig)-1))
372 #define rt_sigmask(sig) sigmask(sig)
375 #define siginmask(sig, mask) \
376 ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
378 #define SIG_KERNEL_ONLY_MASK (\
379 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP))
381 #define SIG_KERNEL_STOP_MASK (\
382 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \
383 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) )
385 #define SIG_KERNEL_COREDUMP_MASK (\
386 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \
387 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \
388 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \
389 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \
390 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \
393 #define SIG_KERNEL_IGNORE_MASK (\
394 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \
395 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) )
397 #define SIG_SPECIFIC_SICODES_MASK (\
398 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \
399 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \
400 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \
401 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \
404 #define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK)
405 #define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
406 #define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK)
407 #define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK)
408 #define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
410 #define sig_fatal(t, signr) \
411 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
412 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
414 void signals_init(void);
416 int restore_altstack(const stack_t __user
*);
417 int __save_altstack(stack_t __user
*, unsigned long);
419 #define save_altstack_ex(uss, sp) do { \
420 stack_t __user *__uss = uss; \
421 struct task_struct *t = current; \
422 put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
423 put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
424 put_user_ex(t->sas_ss_size, &__uss->ss_size); \
425 if (t->sas_ss_flags & SS_AUTODISARM) \
429 #ifdef CONFIG_PROC_FS
431 extern void render_sigset_t(struct seq_file
*, const char *, sigset_t
*);
434 #endif /* _LINUX_SIGNAL_H */