From 72e46b3a1e3322f59a591f1da1a63e362f4fc384 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Fri, 13 Jan 2012 22:46:45 +0100 Subject: [PATCH] add daemonize code from other project. (Might be integrated in a future class but i am not sure right now --- src/daemonize.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/daemonize.c diff --git a/src/daemonize.c b/src/daemonize.c new file mode 100644 index 0000000..6776da1 --- /dev/null +++ b/src/daemonize.c @@ -0,0 +1,23 @@ +#include /* for printf() and fprintf() */ +#include /* for getopt */ +#include + + +void daemonize(void) { + pid_t pid; + + if (0 > ((pid = fork()))) { + perror("deamoinze[fork]"); + exit(EXIT_FAILURE); + } else if (0 != pid) { + exit(EXIT_SUCCESS); + } + + /* make new child session leader */ + setsid(); + + /* connect all standard streams to /dev/null */ + freopen("/dev/null", "w", stderr); + freopen("/dev/null", "r", stdin); + freopen("/dev/null", "w", stdout); +}