Debian Codegraph @

src/basic/signal-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <signal.h> /* IWYU pragma: export */
5
6 #include "basic-forward.h"
7
8 int reset_all_signal_handlers(void);
9 int reset_signal_mask(void);
10
11 int sigaction_many_internal(const struct sigaction *sa, ...);
12
13 #define ignore_signals(...) \
14 sigaction_many_internal( \
15 &sigaction_ignore, \
16 __VA_ARGS__, \
17 -1)
18
19 #define default_signals(...) \
20 sigaction_many_internal( \
21 &sigaction_default, \
22 __VA_ARGS__, \
23 -1)
24
25 #define sigaction_many(sa, ...) \
26 sigaction_many_internal(sa, __VA_ARGS__, -1)
27
28 int sigset_add_many_internal(sigset_t *ss, ...);
29 #define sigset_add_many(...) sigset_add_many_internal(__VA_ARGS__, -1)
30
31 int sigprocmask_many_internal(int how, sigset_t *ret_old_mask, ...);
32 #define sigprocmask_many(...) sigprocmask_many_internal(__VA_ARGS__, -1)
33
34 DECLARE_STRING_TABLE_LOOKUP(signal, int);
35 const char* signal_code_to_string(int signo, int code) _const_;
36
37 void nop_signal_handler(int sig);
38
39 static inline void block_signals_reset(sigset_t **ss) {
40 assert(ss);
41
42 if (!*ss)
43 return;
44
45 assert_log(sigprocmask(SIG_SETMASK, *ss, NULL) >= 0);
46 }
47
48 #define BLOCK_SIGNALS(...) \
49 sigset_t _saved_sigset; \
50 _cleanup_(block_signals_reset) _unused_ sigset_t *_saved_sigsetp = \
51 assert_log(sigprocmask_many(SIG_BLOCK, &_saved_sigset, __VA_ARGS__) >= 0) ? \
52 &_saved_sigset : NULL;
53
54 #define SIGNO_INVALID (-EINVAL)
55
56 static inline bool SIGNAL_VALID(int signo) {
57 return signo > 0 && signo < _NSIG;
58 }
59
60 static inline const char* signal_to_string_with_check(int n) {
61 if (!SIGNAL_VALID(n))
62 return NULL;
63
64 return signal_to_string(n);
65 }
66
67 int signal_is_blocked(int sig);
68
69 int autoreaping_enabled(void);
70
71 int pop_pending_signal_internal(int sig, ...);
72 #define pop_pending_signal(...) pop_pending_signal_internal(__VA_ARGS__, -1)
73
74 void propagate_signal(int sig, siginfo_t *siginfo);
75
76 extern const struct sigaction sigaction_ignore;
77 extern const struct sigaction sigaction_default;
78 extern const struct sigaction sigaction_nop_nocldstop;
79
80 int parse_signo(const char *s, int *ret);
81
82 static inline bool si_code_from_process(int si_code) {
83 /* Returns true if the .si_code field of siginfo_t or the .ssi_code field of struct signalfd_siginfo
84 * indicate that the signal originates from a userspace process, and hence the .si_pid/.ssi_pid field
85 * is valid. This check is not obvious, since on one hand SI_USER/SI_QUEUE are supposed to be the
86 * values that kill() and sigqueue() set, and that's documented in sigaction(2), but on the other
87 * hand rt_sigqueueinfo(2) says userspace can actually set any value below zero. Hence check for
88 * either.
89 *
90 * Also quoting POSIX:
91 *
92 * "On systems not supporting the XSI option, the si_pid and si_uid members of siginfo_t are only
93 * required to be valid when si_code is SI_USER or SI_QUEUE. On XSI-conforming systems, they are also
94 * valid for all si_code values less than or equal to 0; however, it is unspecified whether SI_USER
95 * and SI_QUEUE have values less than or equal to zero, and therefore XSI applications should check
96 * whether si_code has the value SI_USER or SI_QUEUE or is less than or equal to 0 to tell whether
97 * si_pid and si_uid are valid."
98 *
99 * From: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html */
100
101 return si_code < 0 || IN_SET(si_code, SI_USER, SI_QUEUE);
102 }
103
104 void sigterm_process_group_handler(int signal, siginfo_t *info, void *ucontext);