Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/TetraIF/PrtCnv/PrtCnvMain.cpp
T

128 lines
3.1 KiB
C++

// **************************************************************************
//
// (C) Copyright 2009 Swiss SIMTEC AG
//
// **************************************************************************
//
// $HeadURL$
// $LastChangedBy$
// $LastChangedDate$
// $LastChangedRevision$
//
// **************************************************************************
/*!
*
* \page Tetra-Interface Tetra-Interface
*
* This component is needed to communicate between the Server software and the players in
* the field via Tetra. It's an extra component due to historical reasons only.
*
*/
#include "PrtCnvApplication.hpp"
#include <NGLLogger/INGLLogger.h>
#include <nglapplication/nglcoreapplication.h>
#include <LtsCommandLineParser/LtsCommandLineParser.h>
#include <QNetworkProxy>
#ifndef WIN32
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif
// ---------------------------------------------------------------
// Become a daemon
// ---------------------------------------------------------------
static void daemonize()
{
#ifndef WIN32
pid_t pid, sid;
/* already a daemon */
if (getppid() == 1)
return;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0)
{
exit(EXIT_SUCCESS);
}
/* At this point we are executing as the child process */
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
exit(EXIT_FAILURE);
}
/* Change the current working directory. This prevents the current
directory from being locked; hence not being able to remove it. */
if ((chdir("/")) < 0)
{
exit(EXIT_FAILURE);
}
if (freopen("/dev/null", "r", stdin) == nullptr || freopen("/dev/null", "w", stdout) == nullptr || freopen("/dev/null", "w", stderr) == nullptr)
{
exit(EXIT_FAILURE);
}
#endif
}
class CmdLineParser : public LtsCommandLineParser::LtsCommandLineParser
{
public:
CmdLineParser()
: LtsCommandLineParser(ProvideCommandLineParameterOptions(ExerciseId | CustomConfigFile))
{
addOption({QStringList() << QStringLiteral("d") << QStringLiteral("daemonize"), QStringLiteral("Start in background")});
addOption({QStringList() << QStringLiteral("c") << QStringLiteral("cleandb"), QStringLiteral("Start over with clean database")});
}
bool daemonize() const { return isSet(QStringLiteral("d")); }
bool cleanDb() const { return isSet(QStringLiteral("c")); }
};
int main(int argc, char **argv)
{
QNetworkProxy::setApplicationProxy(QNetworkProxy());
NGLCoreApplication app(argc, argv);
CmdLineParser options;
if (!options.init())
return -1;
app.createLogger("PrtCnv", options.exerciseId());
if (options.daemonize())
daemonize();
PrtCnv::PrtCnvApplication prtCnvApp;
prtCnvApp.setCleanDb(options.cleanDb());
prtCnvApp.setExerciseId(options.exerciseId());
if (options.iniFileSet())
prtCnvApp.setConfigFile(options.iniFile());
STARTUP_LOG(app.baseLogger());
prtCnvApp.startApp();
int ret = app.exec();
prtCnvApp.shutdownApp();
SHUTDOWN_LOG(app.baseLogger());
return ret;
}