Add fork/background mode for *nix

translations
Phil Taylor 2024-08-13 15:39:13 +01:00
rodzic 1142548e21
commit 1b95413c52
1 zmienionych plików z 74 dodań i 28 usunięć

Wyświetl plik

@ -9,44 +9,82 @@
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h> #include <windows.h>
#include <csignal> #include <csignal>
#else
#include <fcntl.h>
#include <signal.h>
#endif #endif
#include <iostream> #include <iostream>
#include "wfmain.h" #include "wfmain.h"
// Copyright 2017-2022 Elliott H. Liggett
#include "logcategories.h" #include "logcategories.h"
bool debugMode=false;
#ifdef BUILD_WFSERVER #ifdef BUILD_WFSERVER
// Smart pointer to log file // Smart pointer to log file
QScopedPointer<QFile> m_logFile; QScopedPointer<QFile> m_logFile;
QMutex logMutex; QMutex logMutex;
#endif servermain* w=Q_NULLPTR;
bool debugMode=false;
#ifdef BUILD_WFSERVER
servermain* w=Q_NULLPTR;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
bool __stdcall cleanup(DWORD sig) bool __stdcall cleanup(DWORD sig)
#else #else
static void cleanup(int sig) static void cleanup(int sig)
#endif #endif
{ {
Q_UNUSED(sig) switch(sig) {
qDebug() << "Exiting via SIGNAL"; case SIGHUP:
qInfo() << "hangup signal";
break;
case SIGTERM:
qInfo() << "terminate signal catched";
if (w!=Q_NULLPTR) w->deleteLater(); if (w!=Q_NULLPTR) w->deleteLater();
QCoreApplication::quit(); QCoreApplication::quit();
break;
default:
break;
}
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
return true; return true;
#else #else
return; return;
#endif #endif
} }
#ifndef Q_OS_WIN
void initDaemon()
{
int i;
if(getppid()==1)
return; /* already a daemon */
i=fork();
if (i<0)
exit(1); /* fork error */
if (i>0)
exit(0); /* parent exits */
setsid(); /* obtain a new process group */
for (i=getdtablesize();i>=0;--i)
close(i); /* close all descriptors */
i=open("/dev/null",O_RDWR); dup(i); dup(i);
signal(SIGCHLD,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
}
#else
void initDaemon() {
}
#endif
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg); void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg);
#endif #endif
@ -80,7 +118,7 @@ int main(int argc, char *argv[])
QString currentArg; QString currentArg;
const QString helpText = QString("\nUsage: -l --logfile filename.log, -s --settings filename.ini, -d --debug, -v --version\n"); // TODO... const QString helpText = QString("\nUsage: -l --logfile filename.log, -s --settings filename.ini, -b --background (not Windows), -d --debug, -v --version\n"); // TODO...
#ifdef BUILD_WFSERVER #ifdef BUILD_WFSERVER
const QString version = QString("wfserver version: %1 (Git:%2 on %3 at %4 by %5@%6)\nOperating System: %7 (%8)\nBuild Qt Version %9. Current Qt Version: %10\n") const QString version = QString("wfserver version: %1 (Git:%2 on %3 at %4 by %5@%6)\nOperating System: %7 (%8)\nBuild Qt Version %9. Current Qt Version: %10\n")
.arg(QString(WFVIEW_VERSION)) .arg(QString(WFVIEW_VERSION))
@ -134,6 +172,12 @@ int main(int argc, char *argv[])
c += 1; c += 1;
} }
} }
#ifdef BUILD_WFSERVER
else if ((currentArg == "-b") || (currentArg == "--background"))
{
initDaemon();
}
#endif
else if ((currentArg == "-?") || (currentArg == "--help")) else if ((currentArg == "-?") || (currentArg == "--help"))
{ {
std::cout << helpText.toStdString(); std::cout << helpText.toStdString();
@ -143,7 +187,8 @@ int main(int argc, char *argv[])
{ {
std::cout << version.toStdString(); std::cout << version.toStdString();
return 0; return 0;
} else { }
else {
std::cout << "Unrecognized option: " << currentArg.toStdString(); std::cout << "Unrecognized option: " << currentArg.toStdString();
std::cout << helpText.toStdString(); std::cout << helpText.toStdString();
return -1; return -1;
@ -163,14 +208,15 @@ int main(int argc, char *argv[])
qInfo(logSystem()) << version; qInfo(logSystem()) << version;
#endif #endif
#ifdef BUILD_WFSERVER #ifdef BUILD_WFSERVER
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
SetConsoleCtrlHandler((PHANDLER_ROUTINE)cleanup, TRUE); SetConsoleCtrlHandler((PHANDLER_ROUTINE)cleanup, TRUE);
#else #else
signal(SIGINT, cleanup); signal(SIGINT, cleanup);
signal(SIGTERM, cleanup); signal(SIGTERM, cleanup);
signal(SIGKILL, cleanup); signal(SIGKILL, cleanup);
#endif #endif
w = new servermain(settingsFile); w = new servermain(settingsFile);
#else #else
a.setWheelScrollLines(1); // one line per wheel click a.setWheelScrollLines(1); // one line per wheel click