daemon: moved code to daemonize_detach()

Moved the code which detaches from the parent process/session to a
separate function.
This commit is contained in:
Max Kellermann 2009-02-15 16:27:09 +01:00
parent fd69782a99
commit fd8c63b619

View File

@ -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");