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

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