From c0b34c53149d6d3a796d5dbdc1de379289281c7d Mon Sep 17 00:00:00 2001 From: pk910 Date: Sun, 5 Feb 2012 13:11:00 +0100 Subject: [PATCH] redirect stdin/stdout/stderr to /dev/null after forking into background && close log file on exit --- src/main.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index dc76d7a..fb8abce 100644 --- a/src/main.c +++ b/src/main.c @@ -149,6 +149,10 @@ void exit_daemon() { if(daemonized) { remove(PID_FILE); } + if(log_fptr) { + fclose(log_fptr); + log_fptr = NULL; + } } int main(int argc, char *argv[]) { @@ -194,6 +198,7 @@ int main(int argc, char *argv[]) { } check_firstrun(); if (run_as_daemon) { + #ifndef WIN32 /* Attempt to fork into the background if daemon mode is on. */ pid_t pid = fork(); if (pid < 0) { @@ -214,9 +219,10 @@ int main(int argc, char *argv[]) { fprintf(pidfile, "%i\n", (int)getpid()); fclose(pidfile); } - fclose(stdin); - fclose(stdout); - fclose(stderr); + fclose(stdin); fopen("/dev/null", "r"); + fclose(stdout); fopen("/dev/null", "w"); + fclose(stderr); fopen("/dev/null", "w"); + #endif } main: -- 2.20.1