1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-15 12:51:49 -05:00

Server: implemented a signal handler for SIGQUIT, SIGINT, SIGTERM, SIGHUP so the application exits nicely

This commit is contained in:
f4exb 2017-12-30 21:29:48 +01:00
parent 305f2f7fe6
commit 415d2222d5

View File

@ -18,9 +18,35 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <signal.h>
#include <unistd.h>
#include <vector>
#include "loggerwithfile.h" #include "loggerwithfile.h"
#include "maincore.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<int>& quitSignals) {
sigset_t blocking_mask;
sigemptyset(&blocking_mask);
for (std::vector<int>::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<int>::const_iterator it = quitSignals.begin(); it != quitSignals.end(); ++it) {
sigaction(*it, &sa, 0);
}
}
static int runQtApplication(int argc, char* argv[], qtwebapp::LoggerWithFile *logger) 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::setApplicationName("SDRangelSrv");
QCoreApplication::setApplicationVersion("3.9.0"); QCoreApplication::setApplicationVersion("3.9.0");
int catchSignals[] = {SIGQUIT, SIGINT, SIGTERM, SIGHUP};
std::vector<int> vsig(catchSignals, catchSignals + sizeof(catchSignals) / sizeof(int));
catchUnixSignals(vsig);
MainParser parser; MainParser parser;
parser.parse(a); parser.parse(a);
MainCore m(logger, parser, &a); MainCore m(logger, parser, &a);