diff --git a/slsReceiverSoftware/src/CommandLineOptions.cpp b/slsReceiverSoftware/src/CommandLineOptions.cpp index 34a0882be..517b591fd 100644 --- a/slsReceiverSoftware/src/CommandLineOptions.cpp +++ b/slsReceiverSoftware/src/CommandLineOptions.cpp @@ -7,7 +7,6 @@ #include "sls/sls_detector_defs.h" #include "sls/versionAPI.h" -#include #include #include @@ -335,17 +334,6 @@ std::string CommandLineOptions::getHelpMessage() const { throw sls::RuntimeError("Unknown AppType for help message"); } -void CommandLineOptions::setupSignalHandler(int signal, void (*handler)(int)) { - // Catch signal SIGINT to close files and call destructors properly - struct sigaction sa {}; - sa.sa_handler = handler; - sigemptyset(&sa.sa_mask); // dont block additional signals - sa.sa_flags = 0; - if (sigaction(signal, &sa, nullptr) == -1) { - LOG(sls::logERROR) << "Could not set handler for " << strsignal(signal); - } -} - void CommandLineOptions::setEffectiveUID(uid_t uid) { if (geteuid() == uid) { LOG(sls::logINFO) << "Process already has the same Effective UID " diff --git a/slsReceiverSoftware/src/CommandLineOptions.h b/slsReceiverSoftware/src/CommandLineOptions.h index 6bc7bc791..fbdaa01a4 100644 --- a/slsReceiverSoftware/src/CommandLineOptions.h +++ b/slsReceiverSoftware/src/CommandLineOptions.h @@ -39,7 +39,6 @@ class CommandLineOptions { std::string getTypeString() const; std::string getVersion() const; std::string getHelpMessage() const; - static void setupSignalHandler(int signal, void (*handler)(int)); static void setEffectiveUID(uid_t uid); static std::tuple ParseDeprecated(const std::vector &args); diff --git a/slsReceiverSoftware/src/FrameSynchronizerApp.cpp b/slsReceiverSoftware/src/FrameSynchronizerApp.cpp index 69d85534a..fc48814a9 100644 --- a/slsReceiverSoftware/src/FrameSynchronizerApp.cpp +++ b/slsReceiverSoftware/src/FrameSynchronizerApp.cpp @@ -10,6 +10,7 @@ #include "sls/ToString.h" #include "sls/container_utils.h" #include "sls/logger.h" +#include "sls/network_utils.h" #include "sls/sls_detector_defs.h" #include //SIGINT @@ -524,9 +525,9 @@ int main(int argc, char *argv[]) { LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']'; // close files on ctrl+c - CommandLineOptions::setupSignalHandler(SIGINT, sigInterruptHandler); + sls::setupSignalHandler(SIGINT, sigInterruptHandler); // handle locally on socket crash - CommandLineOptions::setupSignalHandler(SIGPIPE, SIG_IGN); + sls::setupSignalHandler(SIGPIPE, SIG_IGN); semaphores.resize(f.numReceivers); for (auto &s : semaphores) { diff --git a/slsReceiverSoftware/src/MultiReceiverApp.cpp b/slsReceiverSoftware/src/MultiReceiverApp.cpp index b4af30490..a1bc4cb83 100644 --- a/slsReceiverSoftware/src/MultiReceiverApp.cpp +++ b/slsReceiverSoftware/src/MultiReceiverApp.cpp @@ -7,6 +7,7 @@ #include "sls/ToString.h" #include "sls/container_utils.h" #include "sls/logger.h" +#include "sls/network_utils.h" #include "sls/sls_detector_defs.h" #include //SIGINT @@ -158,9 +159,9 @@ int main(int argc, char *argv[]) { LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']'; // close files on ctrl+c - CommandLineOptions::setupSignalHandler(SIGINT, sigInterruptHandler); + sls::setupSignalHandler(SIGINT, sigInterruptHandler); // handle locally on socket crash - CommandLineOptions::setupSignalHandler(SIGPIPE, SIG_IGN); + sls::setupSignalHandler(SIGPIPE, SIG_IGN); sem_init(&semaphore, 1, 0); @@ -226,7 +227,7 @@ int main(int argc, char *argv[]) { /** - Parent process ignores SIGINT and waits for all the child processes to * handle the signal */ - CommandLineOptions::setupSignalHandler(SIGINT, SIG_IGN); + sls::setupSignalHandler(SIGINT, SIG_IGN); /** - Print Ready and Instructions how to exit */ std::cout << "Ready ... \n"; diff --git a/slsReceiverSoftware/src/ReceiverApp.cpp b/slsReceiverSoftware/src/ReceiverApp.cpp index 2c477fed8..92ff84b50 100644 --- a/slsReceiverSoftware/src/ReceiverApp.cpp +++ b/slsReceiverSoftware/src/ReceiverApp.cpp @@ -6,6 +6,7 @@ #include "sls/ToString.h" #include "sls/container_utils.h" #include "sls/logger.h" +#include "sls/network_utils.h" #include "sls/sls_detector_defs.h" #include //SIGINT @@ -46,9 +47,9 @@ int main(int argc, char *argv[]) { LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]"; // close files on ctrl+c - CommandLineOptions::setupSignalHandler(SIGINT, sigInterruptHandler); + sls::setupSignalHandler(SIGINT, sigInterruptHandler); // handle locally on socket crash - CommandLineOptions::setupSignalHandler(SIGPIPE, SIG_IGN); + sls::setupSignalHandler(SIGPIPE, SIG_IGN); sem_init(&semaphore, 1, 0); diff --git a/slsSupportLib/include/sls/network_utils.h b/slsSupportLib/include/sls/network_utils.h index cce2c429d..ac5074abf 100644 --- a/slsSupportLib/include/sls/network_utils.h +++ b/slsSupportLib/include/sls/network_utils.h @@ -90,4 +90,5 @@ MacAddr InterfaceNameToMac(const std::string &inf); IpAddr InterfaceNameToIp(const std::string &ifn); void validatePortNumber(uint16_t port); void validatePortRange(uint16_t startPort, int numPorts); +void setupSignalHandler(int signal, void (*handler)(int)); } // namespace sls diff --git a/slsSupportLib/src/network_utils.cpp b/slsSupportLib/src/network_utils.cpp index b597dbc07..28072259b 100644 --- a/slsSupportLib/src/network_utils.cpp +++ b/slsSupportLib/src/network_utils.cpp @@ -1,11 +1,12 @@ // SPDX-License-Identifier: LGPL-3.0-or-other // Copyright (C) 2021 Contributors to the SLS Detector Package +#include "sls/network_utils.h" #include "sls/sls_detector_exceptions.h" -#include "sls/network_utils.h" #include #include #include +#include #include #include #include @@ -217,4 +218,15 @@ void validatePortRange(uint16_t startPort, int numPorts) { validatePortNumber(startPort + numPorts - 1); } +void setupSignalHandler(int signal, void (*handler)(int)) { + // Catch signal SIGINT to close files and call destructors properly + struct sigaction sa {}; + sa.sa_handler = handler; + sigemptyset(&sa.sa_mask); // dont block additional signals + sa.sa_flags = 0; + if (sigaction(signal, &sa, nullptr) == -1) { + throw RuntimeError("Could not set handler for " + + std::string(strsignal(signal))); + } +} } // namespace sls