From f65bf3e1f8c747a9ed99217c1986bcb6d0fecfcd Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 12 Aug 2013 16:42:37 +0100 Subject: [PATCH] make a more real daemonizing --- src/utils/daemonize.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/utils/daemonize.c b/src/utils/daemonize.c index 6dd8318..5b1cce3 100644 --- a/src/utils/daemonize.c +++ b/src/utils/daemonize.c @@ -25,7 +25,14 @@ #include // for printf() and fprintf() #include // for getopt #include +#include +#include +#include + + +#define WORKDIR "/" +#define UMASK 0 void daemonize(void) { pid_t pid; @@ -40,6 +47,23 @@ void daemonize(void) { // make new child session leader setsid(); + if (0 > ((pid = fork()))) { + perror("deamoinze[fork]"); + exit(EXIT_FAILURE); + } else if (0 != pid) { + exit(EXIT_SUCCESS); + } + + // set umask and change to working directory to / + umask(UMASK); + chdir(PWD); // this should root and assets needs to be found + // via some kind of configuration. + + // we should close all open filedescriptors now. + // But I assume that this function is called at the very start of the + // program and no more filedescriptors are open than the standard + // ones. + // connect all standard streams to /dev/null stderr = freopen("/dev/null", "w", stderr); stdin = freopen("/dev/null", "r", stdin);