moving set signal handler to network utils

This commit is contained in:
2025-07-11 10:52:08 +02:00
parent 2698087efa
commit 09709f0f96
7 changed files with 24 additions and 21 deletions

View File

@ -7,7 +7,6 @@
#include "sls/sls_detector_defs.h"
#include "sls/versionAPI.h"
#include <csignal>
#include <cstring>
#include <unistd.h>
@ -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 "

View File

@ -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<uint16_t, uint16_t, bool>
ParseDeprecated(const std::vector<std::string> &args);

View File

@ -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 <csignal> //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) {

View File

@ -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 <csignal> //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";

View File

@ -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 <csignal> //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);

View File

@ -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

View File

@ -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 <algorithm>
#include <arpa/inet.h>
#include <cassert>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <ifaddrs.h>
@ -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