1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <signal.h>
5 
6 #include "macro.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                         &(const struct sigaction) {                     \
16                                 .sa_handler = SIG_IGN,                  \
17                                 .sa_flags = SA_RESTART                  \
18                         },                                              \
19                         __VA_ARGS__,                                    \
20                         -1)
21 
22 #define default_signals(...)                                            \
23         sigaction_many_internal(                                        \
24                         &(const struct sigaction) {                     \
25                                 .sa_handler = SIG_DFL,                  \
26                                 .sa_flags = SA_RESTART                  \
27                         },                                              \
28                         __VA_ARGS__,                                    \
29                         -1)
30 
31 #define sigaction_many(sa, ...)                                         \
32         sigaction_many_internal(sa, __VA_ARGS__, -1)
33 
34 int sigset_add_many(sigset_t *ss, ...);
35 int sigprocmask_many(int how, sigset_t *old, ...);
36 
37 const char *signal_to_string(int i) _const_;
38 int signal_from_string(const char *s) _pure_;
39 
40 void nop_signal_handler(int sig);
41 
block_signals_reset(sigset_t * ss)42 static inline void block_signals_reset(sigset_t *ss) {
43         assert_se(sigprocmask(SIG_SETMASK, ss, NULL) >= 0);
44 }
45 
46 #define BLOCK_SIGNALS(...)                                                         \
47         _cleanup_(block_signals_reset) _unused_ sigset_t _saved_sigset = ({        \
48                 sigset_t _t;                                                       \
49                 assert_se(sigprocmask_many(SIG_BLOCK, &_t, __VA_ARGS__, -1) >= 0); \
50                 _t;                                                                \
51         })
52 
SIGNAL_VALID(int signo)53 static inline bool SIGNAL_VALID(int signo) {
54         return signo > 0 && signo < _NSIG;
55 }
56 
signal_to_string_with_check(int n)57 static inline const char* signal_to_string_with_check(int n) {
58         if (!SIGNAL_VALID(n))
59                 return NULL;
60 
61         return signal_to_string(n);
62 }
63 
64 int signal_is_blocked(int sig);
65 
66 int pop_pending_signal_internal(int sig, ...);
67 #define pop_pending_signal(...) pop_pending_signal_internal(__VA_ARGS__, -1)
68