3
3
* This program is free software. You can redistribute it and/or modify it under the terms of the MIT License.
4
4
*/
5
5
6
- #include < csignal>
7
6
#include < filesystem>
8
7
#include < iostream>
9
8
#include < memory>
9
+ #include < signal.h>
10
10
#include < sysexits.h>
11
11
#include < unistd.h>
12
12
@@ -48,6 +48,17 @@ static void sig_term_handler(int) {
48
48
terminate = true ;
49
49
}
50
50
51
+ constexpr std::array<int , 10 > TERM_SIGNALS = {SIGINT,
52
+ SIGTERM,
53
+ SIGHUP,
54
+ SIGIO, // should not happen
55
+ SIGPIPE,
56
+ SIGPOLL, // should not happen
57
+ SIGPROF, // should not happen
58
+ SIGUSR1,
59
+ SIGUSR2,
60
+ SIGVTALRM};
61
+
51
62
/* ! \brief main function
52
63
*
53
64
* @param argc number of arguments
@@ -66,16 +77,24 @@ int main(int argc, char **argv) {
66
77
auto euid = geteuid ();
67
78
if (!euid) std::cerr << " !!!! WARNING: You should not execute this program with root privileges !!!!" << std::endl;
68
79
80
+ #ifdef COMPILER_CLANG
81
+ # pragma clang diagnostic push
82
+ # pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
83
+ #endif
69
84
// establish signal handler
70
- if (signal (SIGINT, sig_term_handler) || signal (SIGTERM, sig_term_handler)) {
71
- perror (" Failed to establish signal handler" );
72
- return EX_OSERR;
73
- }
74
-
75
- if (signal (SIGALRM, [](int ) { exit (EX_OK); })) {
76
- perror (" Failed to establish signal handler" );
77
- return EX_OSERR;
85
+ struct sigaction term_sa;
86
+ term_sa.sa_handler = sig_term_handler;
87
+ term_sa.sa_flags = SA_RESTART;
88
+ sigemptyset (&term_sa.sa_mask );
89
+ for (const auto SIGNO : TERM_SIGNALS) {
90
+ if (sigaction (SIGNO, &term_sa, nullptr )) {
91
+ perror (" Failed to establish signal handler" );
92
+ return EX_OSERR;
93
+ }
78
94
}
95
+ #ifdef COMPILER_CLANG
96
+ # pragma clang diagnostic pop
97
+ #endif
79
98
80
99
// all command line arguments
81
100
// clang-format off
0 commit comments