]> granicus.if.org Git - icinga2/commitdiff
Implement -d (daemonize) and -e (errorlog).
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 3 May 2013 11:39:31 +0000 (13:39 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 3 May 2013 11:39:31 +0000 (13:39 +0200)
etc/init.d/icinga2.in
icinga-app/icinga.cpp

index c97f8b24499274761169234fc68ce8f69559e14d..e5b07cd2b1cddef07ffe76a707b608500e2cf461 100644 (file)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 # chkconfig: 35 90 12
 # description: Icinga 2
@@ -48,8 +48,7 @@ start() {
         fi
 
         printf "Starting Icinga 2: "
-        $DAEMON -c $ICINGA2_CONFIG_FILE </dev/null >/dev/null 2>$ICINGA2_ERROR_LOG &
-       disown
+        $DAEMON -c $ICINGA2_CONFIG_FILE -d -e $ICINGA2_ERROR_LOG
 
         echo "Done"
         echo
index 236b16e82764316d97c82380a76c2d4b0a5728c5..94e7fed28e70394c66258c1a6477fc7bfe6da345 100644 (file)
@@ -129,6 +129,56 @@ static void SigHupHandler(int signum)
 }
 #endif /* _WIN32 */
 
+static bool Daemonize(const String& stderrFile)
+{
+#ifndef _WIN32
+       pid_t pid = fork();
+       if (pid == -1) {
+               return false;
+       }
+
+       if (pid)
+               exit(0);
+
+       int fdnull = open("/dev/null", O_RDWR);
+       if (fdnull > 0) {
+               if (fdnull != 0)
+                       dup2(fdnull, 0);
+
+               if (fdnull != 1)
+                       dup2(fdnull, 1);
+
+               if (fdnull > 2)
+                       close(fdnull);
+       }
+
+       const char *errPath = "/dev/null";
+
+       if (!stderrFile.IsEmpty())
+               errPath = stderrFile.CStr();
+
+       int fderr = open(errPath, O_WRONLY | O_APPEND);
+
+       if (fderr < 0 && errno == ENOENT)
+               fderr = open(errPath, O_CREAT | O_WRONLY | O_APPEND, 0600);
+
+       if (fderr > 0) {
+               if (fderr != 2)
+                       dup2(fderr, 2);
+
+               if (fderr > 2)
+                       close(fderr);
+       }
+
+       pid_t sid = setsid();
+       if (sid == -1) {
+               return false;
+       }
+#endif
+
+       return true;
+}
+
 /**
  * Entry point for the Icinga application.
  *
@@ -181,6 +231,8 @@ int main(int argc, char **argv)
                ("config,c", po::value<std::vector<String> >(), "parse a configuration file")
                ("validate,v", "exit after validating the configuration")
                ("debug,x", "enable debugging")
+               ("daemonize,d", "detach from the controlling terminal")
+               ("errorlog,e", po::value<String>(), "log fatal errors to the specified log file (only works in combination with --daemonize)")
        ;
 
        try {
@@ -267,6 +319,15 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
+       if (g_AppParams.count("daemonize")) {
+               String errorLog;
+
+               if (g_AppParams.count("errorlog"))
+                       errorLog = g_AppParams["errorlog"].as<String>();
+
+               Daemonize(errorLog);
+       }
+
        bool validateOnly = g_AppParams.count("validate");
 
        if (!LoadConfigFiles(validateOnly))