Index: LeahiRt/main.cpp =================================================================== diff -u -r864d025811eb43fd2592bde174e5cb41fa10d783 -rf9c6b488aa4135e8cd47ccd3fdc6c3ae1cd831aa --- LeahiRt/main.cpp (.../main.cpp) (revision 864d025811eb43fd2592bde174e5cb41fa10d783) +++ LeahiRt/main.cpp (.../main.cpp) (revision f9c6b488aa4135e8cd47ccd3fdc6c3ae1cd831aa) @@ -1,49 +1,57 @@ +#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"); - + + // 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", + {"c", "config"}, "Path to the configuration INI file.", "config", QDir(app.applicationDirPath()).filePath("config/LeahiRt.ini") ); parser.addOption(configOption);