From 091dff84917080f998806963f66803fa16cd80f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Wed, 7 Jan 2015 18:08:24 +0100 Subject: [PATCH] added daemon option and application parameters --- server/main.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/server/main.cpp b/server/main.cpp index 63d70c9a..bbf5f51d 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -21,13 +21,86 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void daemonizeGuh() { + // Our process ID and Session ID + pid_t pid, sid; + + // 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); + } + + // Change the file mode mask + umask(0); + // Create a new SID for the child process + sid = setsid(); + if (sid < 0) { + // Log the failure + exit(EXIT_FAILURE); + } + + // Change the current working directory + if ((chdir("/")) < 0) { + /* Log the failure */ + exit(EXIT_FAILURE); + } + + // Close out the standard file descriptors + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); +} + int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); + QStringList arguments = a.arguments(); + + if (arguments.contains("-h") || arguments.contains("--help")) { + qDebug() << "guhd" << GUH_VERSION_STRING << "(C) 2014-2015 guh" ; + qDebug() << "Released under the GNU GENERAL PUBLIC LICENSE Version 2"; + qDebug() << ""; + qDebug() << "guh (/[guːh]/ - pronounced German and sounds like \"goo\") is an open source"; + qDebug() << "home automation server, which allows to control a lot of different devices "; + qDebug() << "from many different manufacturers."; + qDebug() << ""; + qDebug() << "options:"; + qDebug() << " -h, --help print this help message"; + qDebug() << " -v, --version print version"; + qDebug() << " -e, --executable start guh as application, not as daemon"; + qDebug() << ""; + + exit(0); + } + + if (arguments.contains("-v") || arguments.contains("--version")) { + qDebug() << GUH_VERSION_STRING; + exit(0); + } + + if (!arguments.contains("-e") && !arguments.contains("--executable")) { + qDebug() << "Starting guhd as daemon."; + daemonizeGuh(); + } else { + qDebug() << "Starting guhd as executable application."; + } a.setOrganizationName("guh"); - GuhCore::instance(); - return a.exec(); }