diff --git a/appsrv/main.cpp b/appsrv/main.cpp index 8f93e22cc..04a45291b 100644 --- a/appsrv/main.cpp +++ b/appsrv/main.cpp @@ -18,9 +18,35 @@ #include +#include +#include +#include + #include "loggerwithfile.h" #include "maincore.h" +void handler(int sig) { + fprintf(stderr, "quit the application by signal(%d).\n", sig); + QCoreApplication::quit(); +} + +void catchUnixSignals(const std::vector& quitSignals) { + sigset_t blocking_mask; + sigemptyset(&blocking_mask); + + for (std::vector::const_iterator it = quitSignals.begin(); it != quitSignals.end(); ++it) { + sigaddset(&blocking_mask, *it); + } + + struct sigaction sa; + sa.sa_handler = handler; + sa.sa_mask = blocking_mask; + sa.sa_flags = 0; + + for (std::vector::const_iterator it = quitSignals.begin(); it != quitSignals.end(); ++it) { + sigaction(*it, &sa, 0); + } +} static int runQtApplication(int argc, char* argv[], qtwebapp::LoggerWithFile *logger) { @@ -30,6 +56,10 @@ static int runQtApplication(int argc, char* argv[], qtwebapp::LoggerWithFile *lo QCoreApplication::setApplicationName("SDRangelSrv"); QCoreApplication::setApplicationVersion("3.9.0"); + int catchSignals[] = {SIGQUIT, SIGINT, SIGTERM, SIGHUP}; + std::vector vsig(catchSignals, catchSignals + sizeof(catchSignals) / sizeof(int)); + catchUnixSignals(vsig); + MainParser parser; parser.parse(a); MainCore m(logger, parser, &a);