refactored to take out repetitive code, need to adjust for slsMulti and slsFrameSync

This commit is contained in:
2025-07-04 17:26:41 +02:00
parent 1caf88858b
commit e0810d973d
6 changed files with 244 additions and 87 deletions

View File

@@ -7,6 +7,7 @@
#include "sls/logger.h"
#include "sls/sls_detector_defs.h"
#include "sls/versionAPI.h"
#include "CommandLineOptions.h"
#include <csignal> //SIGINT
#include <getopt.h>
@@ -25,95 +26,15 @@ void sigInterruptHandler(int p) { sem_post(&semaphore); }
int main(int argc, char *argv[]) {
uint16_t port = DEFAULT_TCP_RX_PORTNO;
uid_t userid = -1;
std::string help_message =
"\nUsage: " + std::string(argv[0]) + " Options:\n" +
"\t-v, --version : Version of " + std::string(argv[0]) + ".\n" +
"\t-p, --port : TCP port to communicate with client for "
"configuration. Non-zero and 16 bit.\n" +
"\t-u, --uid : Set effective user id if receiver started "
"with privileges. \n\n";
static struct option long_options[] = {
{"version", no_argument, nullptr, 'v'},
{"rx_tcpport", required_argument, nullptr, 't'},
{"port", required_argument, nullptr, 'p'},
{"uid", required_argument, nullptr, 'u'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}};
int option_index = 0;
int opt = 0;
while (-1 != (opt = getopt_long(argc, argv, "vt:p:u:h", long_options,
&option_index))) {
switch (opt) {
case 'v':
std::cout << argv[0] << " Version: " << APIRECEIVER << std::endl;
return (EXIT_SUCCESS);
case 't':
LOG(sls::logWARNING)
<< "Deprecated option. Please use 'p' or '--port'.";
//[[fallthrough]]; TODO: for when we update to c++17
case 'p':
try {
port = sls::StringTo<uint16_t>(optarg);
} catch (...) {
throw sls::RuntimeError("Could not scan port number." +
help_message);
}
break;
case 'u':
try {
userid = sls::StringTo<uint32_t>(optarg);
} catch (...) {
throw sls::RuntimeError("Invalid uid." + help_message);
}
break;
case 'h':
std::cout << help_message << std::endl;
return (EXIT_SUCCESS);
default:
LOG(sls::logERROR) << help_message;
return (EXIT_FAILURE);
}
}
// remaining arguments
if (optind < argc) {
LOG(sls::logERROR) << "Invalid arguments\n" << help_message;
return (EXIT_FAILURE);
auto opts = parseCommandLine(AppType::SingleReceiver, argc, argv);
auto& o = std::get<CommonOptions>(opts);
if (o.versionRequested || o.helpRequested) {
return EXIT_SUCCESS;
}
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]";
LOG(sls::logINFO) << "Port: " << port;
LOG(sls::logINFO) << "Port: " << o.port;
// set effective id if provided
if (userid != static_cast<uid_t>(-1)) {
if (geteuid() == userid) {
LOG(sls::logINFO)
<< "Process already has the same Effective UID " << userid;
} else {
if (seteuid(userid) != 0) {
std::ostringstream oss;
oss << "Could not set Effective UID to " << userid;
throw sls::RuntimeError(oss.str());
}
if (geteuid() != userid) {
std::ostringstream oss;
oss << "Could not set Effective UID to " << userid << ". Got "
<< geteuid();
throw sls::RuntimeError(oss.str());
}
LOG(sls::logINFO) << "Process Effective UID changed to " << userid;
}
}
// Catch signal SIGINT to close files and call destructors properly
struct sigaction sa;
@@ -138,7 +59,7 @@ int main(int argc, char *argv[]) {
sem_init(&semaphore, 1, 0);
try {
sls::Receiver r(port);
sls::Receiver r(o.port);
LOG(sls::logINFO) << "[ Press \'Ctrl+c\' to exit ]";
sem_wait(&semaphore);
sem_destroy(&semaphore);