From c1406efec622b52df82c3f9c644edba26e3e705d Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Fri, 21 Mar 2025 00:43:14 +0100 Subject: [PATCH] updated for framesynchronizer and added versions too --- .../src/FrameSynchronizerApp.cpp | 148 ++++++++++++------ slsReceiverSoftware/src/MultiReceiverApp.cpp | 22 +-- slsReceiverSoftware/src/ReceiverApp.cpp | 9 +- 3 files changed, 116 insertions(+), 63 deletions(-) diff --git a/slsReceiverSoftware/src/FrameSynchronizerApp.cpp b/slsReceiverSoftware/src/FrameSynchronizerApp.cpp index b4e08b32e..dc5d43b10 100644 --- a/slsReceiverSoftware/src/FrameSynchronizerApp.cpp +++ b/slsReceiverSoftware/src/FrameSynchronizerApp.cpp @@ -15,6 +15,7 @@ #include //SIGINT #include #include +#include #include #include #include @@ -507,61 +508,108 @@ void GetDataCallback(slsDetectorDefs::sls_receiver_header &header, * - Default Start TCP port is 1954 */ int main(int argc, char *argv[]) { - - // version - if (argc == 2) { - std::string sargv1 = std::string(argv[1]); - if (sargv1 == "--version" || sargv1 == "-v") { - std::cout << "slsFrameSynchronizer Version: " << APIRECEIVER - << std::endl; - exit(EXIT_SUCCESS); - } - } - - /** - set default values */ - int numReceivers = 1; - uint16_t startTCPPort = DEFAULT_TCP_RX_PORTNO; + uint16_t startPort = DEFAULT_TCP_RX_PORTNO; + uint16_t numReceivers = 1; bool printHeaders = false; + uid_t userid = -1; - /** - get number of receivers and start tcp port from command line - * arguments */ - if (argc > 1) { - try { - if (argc == 3 || argc == 4) { - startTCPPort = sls::StringTo(argv[1]); - if (startTCPPort == 0) { - throw std::runtime_error("Invalid start tcp port"); + std::string help_message = + "\nUsage: " + std::string(argv[0]) + " Options:\n" + + "\t-v, --version : Version of " + std::string(argv[0]) + ".\n" + + "\t-n, --num-receivers : Number of receivers.\n" + + "\t-p, --port : TCP port to communicate with client for " + "configuration. Non-zero and 16 bit.\n" + + "\t-c, --print-headers : Print callback headers for debugging. " + "Disabled by default.\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'}, + {"num-receivers", required_argument, nullptr, 'n'}, + {"port", required_argument, nullptr, 'p'}, + {"print-headers", no_argument, nullptr, 'c'}, + {"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, "vn:p:cu:h", long_options, + &option_index))) { + + switch (opt) { + + case 'v': + std::cout << argv[0] << " Version: " << APIRECEIVER << std::endl; + exit(EXIT_SUCCESS); + + case 'n': + try { + numReceivers = sls::StringTo(optarg); + if (numReceivers == 0 || numReceivers > 100) { + throw std::runtime_error("Invalid argument."); } - numReceivers = std::stoi(argv[2]); - if (numReceivers > 1024) { - cprintf(RED, - "Did you mix up the order of the arguments?\n%s\n", - getHelpMessage().c_str()); - return EXIT_FAILURE; - } - if (numReceivers == 0) { - cprintf(RED, "Invalid number of receivers.\n%s\n", - getHelpMessage().c_str()); - return EXIT_FAILURE; - } - if (argc == 4) { - printHeaders = sls::StringTo(argv[3]); - if (printHeaders) { - printHeadersLevel = sls::logINFOBLUE; - } - } - } else - throw std::runtime_error("Invalid number of arguments"); - } catch (const std::exception &e) { - cprintf(RED, "Error: %s\n%s\n", e.what(), getHelpMessage().c_str()); - return EXIT_FAILURE; + } catch (...) { + throw sls::RuntimeError("Invalid number of receivers." + + help_message); + } + break; + + case 'p': + try { + startPort = sls::StringTo(optarg); + } catch (...) { + throw sls::RuntimeError("Could not scan port number." + + help_message); + } + break; + + case 'c': + printHeaders = true; + break; + + case 'u': + try { + userid = sls::StringTo(optarg); + } catch (...) { + throw sls::RuntimeError("Invalid uid." + help_message); + } + break; + + case 'h': + std::cout << help_message << std::endl; + exit(EXIT_SUCCESS); + default: + throw sls::RuntimeError(help_message); } } - cprintf(RESET, "Number of Receivers: %d\n", numReceivers); - cprintf(RESET, "Start TCP Port: %hu\n", startTCPPort); - cprintf(RESET, "Print Callback Headers: %s\n\n", - (printHeaders ? "Enabled" : "Disabled")); + LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']'; + LOG(sls::logINFO) << "Number of Receivers: " << numReceivers; + LOG(sls::logINFO) << "Start TCP Port: " << startPort; + LOG(sls::logINFO) << "Print Callback Headers: " << printHeaders; + + // set effective id if provided + if (userid != static_cast(-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; @@ -598,7 +646,7 @@ int main(int argc, char *argv[]) { sem_init(semaphore, 1, 0); semaphores.push_back(semaphore); - uint16_t port = startTCPPort + i; + uint16_t port = startPort + i; threads.emplace_back([i, semaphore, port, user_data]() { sls::Receiver receiver(port); receiver.registerCallBackStartAcquisition(StartAcquisitionCallback, diff --git a/slsReceiverSoftware/src/MultiReceiverApp.cpp b/slsReceiverSoftware/src/MultiReceiverApp.cpp index 0c09e828a..73ba6dd62 100644 --- a/slsReceiverSoftware/src/MultiReceiverApp.cpp +++ b/slsReceiverSoftware/src/MultiReceiverApp.cpp @@ -181,8 +181,8 @@ std::string getHelpMessage() { "\t-n, --num-receivers : Number of receivers.\n" + "\t-p, --port : TCP port to communicate with client for " "configuration. Non-zero and 16 bit.\n" + - "\t-c, --callback : Enable dummy callbacks. Disabled (0) by " - "default. Prints frame header for debugging.\n" + + "\t-c, --callback : Enable dummy callbacks for debugging. " + "Disabled by default. \n" + "\t-u, --uid : Set effective user id if receiver started " "with privileges. \n\n"; } @@ -203,22 +203,26 @@ int main(int argc, char *argv[]) { std::string help_message = getHelpMessage(); static struct option long_options[] = { - {"help", no_argument, nullptr, 'h'}, {"version", no_argument, nullptr, 'v'}, {"num-receivers", required_argument, nullptr, 'n'}, {"rx_tcpport", required_argument, nullptr, 't'}, {"port", required_argument, nullptr, 'p'}, - {"callback", required_argument, nullptr, 'c'}, + {"callback", no_argument, nullptr, 'c'}, {"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, "hvn:t:p:u:c:", long_options, + while (-1 != (opt = getopt_long(argc, argv, "vn:t:p:cu:h", long_options, &option_index))) { switch (opt) { + case 'v': + std::cout << argv[0] << " Version: " << APIRECEIVER << std::endl; + exit(EXIT_SUCCESS); + case 'n': try { numReceivers = sls::StringTo(optarg); @@ -234,6 +238,7 @@ int main(int argc, char *argv[]) { case 't': LOG(sls::logWARNING) << "Deprecated option. Please use 'p' or '--port'."; + //[[fallthrough]]; TODO: for when we update to c++17 case 'p': try { startPort = sls::StringTo(optarg); @@ -244,12 +249,7 @@ int main(int argc, char *argv[]) { break; case 'c': - try { - callbackEnabled = sls::StringTo(optarg); - } catch (...) { - throw sls::RuntimeError("Invalid callback enable." + - help_message); - } + callbackEnabled = true; break; case 'u': diff --git a/slsReceiverSoftware/src/ReceiverApp.cpp b/slsReceiverSoftware/src/ReceiverApp.cpp index c4b679ce7..f89bcf78b 100644 --- a/slsReceiverSoftware/src/ReceiverApp.cpp +++ b/slsReceiverSoftware/src/ReceiverApp.cpp @@ -6,6 +6,7 @@ #include "sls/container_utils.h" #include "sls/logger.h" #include "sls/sls_detector_defs.h" +#include "sls/versionAPI.h" #include //SIGINT #include @@ -36,20 +37,24 @@ int main(int argc, char *argv[]) { "with privileges. \n\n"; static struct option long_options[] = { - {"help", no_argument, nullptr, 'h'}, {"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, "hvt:p:u:", long_options, + 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; + exit(EXIT_SUCCESS); + case 't': LOG(sls::logWARNING) << "Deprecated option. Please use 'p' or '--port'.";