Skip to content

Commit b5f23ff

Browse files
unlink shared memory for more termination signals
1 parent 9e4bcd8 commit b5f23ff

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/main.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* This program is free software. You can redistribute it and/or modify it under the terms of the MIT License.
44
*/
55

6-
#include <csignal>
76
#include <filesystem>
87
#include <iostream>
98
#include <memory>
9+
#include <signal.h>
1010
#include <sysexits.h>
1111
#include <unistd.h>
1212

@@ -48,6 +48,17 @@ static void sig_term_handler(int) {
4848
terminate = true;
4949
}
5050

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+
5162
/*! \brief main function
5263
*
5364
* @param argc number of arguments
@@ -66,11 +77,24 @@ int main(int argc, char **argv) {
6677
auto euid = geteuid();
6778
if (!euid) std::cerr << "!!!! WARNING: You should not execute this program with root privileges !!!!" << std::endl;
6879

80+
#ifdef COMPILER_CLANG
81+
# pragma clang diagnostic push
82+
# pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
83+
#endif
6984
// 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;
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+
}
7394
}
95+
#ifdef COMPILER_CLANG
96+
# pragma clang diagnostic pop
97+
#endif
7498

7599
// all command line arguments
76100
// clang-format off

0 commit comments

Comments
 (0)