version 0.1.10
[fms.git] / src / fmsdaemon.cpp
diff --git a/src/fmsdaemon.cpp b/src/fmsdaemon.cpp
new file mode 100644 (file)
index 0000000..0086a02
--- /dev/null
@@ -0,0 +1,52 @@
+#include "../include/fmsdaemon.h"\r
+\r
+#include <cstdio>\r
+#include <cstdlib>\r
+#include <sys/types.h>\r
+#ifndef _WIN32\r
+       #include <unistd.h>\r
+       #include <sys/stat.h>\r
+#endif\r
+\r
+#ifdef XMEM\r
+       #include <xmem.h>\r
+#endif\r
+\r
+/*\r
+       modified from http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize\r
+*/\r
+void Daemonize()\r
+{\r
+#ifndef _WIN32\r
+    pid_t pid, sid;\r
+\r
+    /* already a daemon */\r
+    if ( getppid() == 1 ) return;\r
+\r
+    /* Fork off the parent process */\r
+    pid = fork();\r
+    if (pid < 0) {\r
+        exit(EXIT_FAILURE);\r
+    }\r
+    /* If we got a good PID, then we can exit the parent process. */\r
+    if (pid > 0) {\r
+        exit(EXIT_SUCCESS);\r
+    }\r
+\r
+    /* At this point we are executing as the child process */\r
+\r
+    /* Change the file mode mask */\r
+    umask(0);\r
+\r
+    /* Create a new SID for the child process */\r
+    sid = setsid();\r
+    if (sid < 0) {\r
+        exit(EXIT_FAILURE);\r
+    }\r
+\r
+    /* Redirect standard files to /dev/null */\r
+    freopen( "/dev/null", "r", stdin);\r
+    freopen( "/dev/null", "w", stdout);\r
+    freopen( "/dev/null", "w", stderr);\r
+#endif\r
+}\r