]> granicus.if.org Git - icinga2/commitdiff
Implemented Application::Daemonize method
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 30 Mar 2012 08:24:42 +0000 (10:24 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 30 Mar 2012 08:24:42 +0000 (10:24 +0200)
base/application.cpp
base/application.h

index 31a0b5e68e0b9d3535d6a075bbb374a18573db6f..efd6badf423f330b251500ec64a52e9d29b7b94b 100644 (file)
@@ -108,6 +108,50 @@ void Application::RunEventLoop(void)
        }
 }
 
+bool Application::Daemonize(void) {
+#ifndef _WIN32
+       pid_t pid;
+       pid_t sid;
+       int fd;
+
+       pid = fork();
+       if (pid == -1) {
+               return false;
+       }
+
+       if (pid) {
+               fprintf(stdout, "DONE\n");
+               exit(0);
+       }
+
+       fd = open("/dev/null", O_RDWR);
+       if (fd) {
+               if (fd != 0) {
+                       dup2(fd, 0);
+               }
+
+               if (fd != 1) {
+                       dup2(fd, 1);
+               }
+
+               if (fd != 2) {
+                       dup2(fd, 2);
+               }
+
+               if (fd > 2) {
+                       close(fd);
+               }
+       }
+
+       sid = setsid();
+       if (sid == -1) {
+               return false;
+       }
+#endif
+
+       return true;
+}
+
 void Application::Shutdown(void)
 {
        m_ShuttingDown = true;
index 8515ba8bedaed959c0727d05a1f0a95002305daf..d665319de899f2582650308fb544db0b2f30888d 100644 (file)
@@ -22,6 +22,7 @@ public:
        virtual int Main(const vector<string>& args) = 0;
 
        void RunEventLoop(void);
+       bool Daemonize(void);
        void Shutdown(void);
 };