From 20b2d8b912ef674b176333adfaf2bcc78495052d Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 19 Feb 2022 18:40:19 +0100 Subject: [PATCH] #20 make it a daemon --- DGWTimeServer/DGWTimeServerApp.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/DGWTimeServer/DGWTimeServerApp.cpp b/DGWTimeServer/DGWTimeServerApp.cpp index 9b26922..7fb39d2 100644 --- a/DGWTimeServer/DGWTimeServerApp.cpp +++ b/DGWTimeServer/DGWTimeServerApp.cpp @@ -28,6 +28,7 @@ #include "DGWTimeServerApp.h" #include "Version.h" #include "Log.h" +#include "Daemon.h" CDGWTimeServerApp * CDGWTimeServerApp::g_app = nullptr; const std::string BANNER_1 = CStringUtils::string_format("%s v%s Copyright (C) %s\n", APPLICATION_NAME.c_str(), LONG_VERSION.c_str(), VENDOR_NAME.c_str()); @@ -61,6 +62,27 @@ int main(int argc, char * argv[]) if(!config.load()) return 1; + TDaemon daemon; + config.getDameon(daemon); + if (daemon.daemon) { + CLog::logInfo("Configured as a daemon, detaching ..."); + auto res = CDaemon::daemonise(daemon.pidFile, daemon.user); + + switch (res) + { + case DR_PARENT: + return 0; + case DR_CHILD: + break; + case DR_PIDFILE_FAILED: + case DR_FAILURE: + default: + CLog::logFatal("Failed to run as daemon"); + CLog::finalise(); + return 1; + } + } + CDGWTimeServerApp app(&config); if(!app.init())