Commit fd8c63b6 authored by Max Kellermann's avatar Max Kellermann

daemon: moved code to daemonize_detach()

Moved the code which detaches from the parent process/session to a separate function.
parent fd69782a
...@@ -126,6 +126,27 @@ daemonize_set_user(void) ...@@ -126,6 +126,27 @@ daemonize_set_user(void)
#endif #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 void
daemonize(bool detach) daemonize(bool detach)
{ {
...@@ -143,25 +164,8 @@ daemonize(bool detach) ...@@ -143,25 +164,8 @@ daemonize(bool detach)
} }
} }
if (detach) { if (detach)
int pid; daemonize_detach();
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 (pidfile != NULL) { if (pidfile != NULL) {
g_debug("writing pid file"); g_debug("writing pid file");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment