Index: LeahiRt/main.cpp =================================================================== diff -u -r6f9f24886c73d92b380e4c3afd82c0b19559d42d -r0359f9239be3218d0b972a54d444e253cc3fc5d9 --- LeahiRt/main.cpp (.../main.cpp) (revision 6f9f24886c73d92b380e4c3afd82c0b19559d42d) +++ LeahiRt/main.cpp (.../main.cpp) (revision 0359f9239be3218d0b972a54d444e253cc3fc5d9) @@ -1,41 +1,71 @@ -#include +#include +#include +#include + +#include +#include #include #include -#include -#include +#include +#include #include "LeahiRtController.h" #define log qDebug().noquote() -/*! - * \brief signalHandler - * \details When application terminates it quits gracefully. - * \param[in] sig - The Linux signal causes the termination. - */ -void signalHandler(int sig) -{ - if (sig == SIGINT) { - log << "Application terminated by SIGINT"; - qApp->quit(); - } - else if (sig == SIGTERM) { - log << "Application terminated by SIGTERM"; - qApp->quit(); - } -} - int main(int argc, char *argv[]) { - signal(SIGINT , signalHandler); - signal(SIGTERM, signalHandler); + // Block SIGINT and SIGTERM from normal delivery; redirect them to a fd. + sigset_t mask; + sigemptyset(&mask); + sigaddset(&mask, SIGINT); + sigaddset(&mask, SIGTERM); + sigprocmask(SIG_BLOCK, &mask, nullptr); + const int sfd = signalfd(-1, &mask, SFD_CLOEXEC); + if (sfd == -1) { + qCritical("Failed to create signalfd"); + return 1; + } + QCoreApplication app(argc, argv); app.setApplicationName("LeahiRt"); app.setApplicationVersion("1.0"); - LeahiRtController rtController; - rtController.openSocket("localhost", 80); + // Notifier runs on the main thread inside the event loop — safe to call quit(). + QSocketNotifier notifier(sfd, QSocketNotifier::Read); + QObject::connect(¬ifier, &QSocketNotifier::activated, [&](int fd) { + struct signalfd_siginfo info{}; + (void)read(fd, &info, sizeof(info)); + if (info.ssi_signo == SIGINT) { + log << "Application terminated by SIGINT"; + } + else if (info.ssi_signo == SIGTERM) { + log << "Application terminated by SIGTERM"; + } + app.quit(); + }); + QCommandLineParser parser; + parser.setApplicationDescription("Leahi Real-time Cloud Data Transmission daemon."); + parser.addHelpOption(); + parser.addVersionOption(); + + QCommandLineOption configOption( + {"c", "config"}, "Path to the configuration INI file.", "config", + QDir(app.applicationDirPath()).filePath("config/LeahiRt.ini") + ); + QCommandLineOption msgHandlingOption( + {"m", "msg_handling"}, "Path to the message handling INI file.", "msg_handling", + QDir(app.applicationDirPath()).filePath("config/LeahiRtMsgHandling.ini") + ); + parser.addOption(configOption); + parser.addOption(msgHandlingOption); + parser.process(app); + + LeahiRtController rtController(parser.value(configOption), + parser.value(msgHandlingOption)); + rtController.connectToAgent(); + return app.exec(); }