From fd8c63b619552f379353e9d6c430cda1164aafc4 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 15 Feb 2009 16:27:09 +0100 Subject: daemon: moved code to daemonize_detach() Moved the code which detaches from the parent process/session to a separate function. --- src/daemon.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'src/daemon.c') diff --git a/src/daemon.c b/src/daemon.c index 02b5a4ba..bb2b8ded 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -126,6 +126,27 @@ daemonize_set_user(void) #endif } +static void +daemonize_detach(void) +{ + pid_t pid; + + fflush(NULL); + + pid = fork(); + if (pid > 0) + _exit(EXIT_SUCCESS); + else if (pid < 0) + g_error("problems fork'ing for daemon!"); + + if (chdir("/") < 0) + g_error("problems changing to root directory"); + + setsid(); + + g_debug("daemonized!"); +} + void daemonize(bool detach) { @@ -143,25 +164,8 @@ daemonize(bool detach) } } - if (detach) { - int pid; - - fflush(NULL); - pid = fork(); - if (pid > 0) - _exit(EXIT_SUCCESS); - else if (pid < 0) { - g_error("problems fork'ing for daemon!"); - } - - if (chdir("/") < 0) { - g_error("problems changing to root directory"); - } - - setsid(); - - g_debug("daemonized!"); - } + if (detach) + daemonize_detach(); if (pidfile != NULL) { g_debug("writing pid file"); -- cgit v1.2.3