From 1d1519111f7bcc51bfeb94bf8eb4b2b43364b497 Mon Sep 17 00:00:00 2001 From: martin Date: Tue, 11 Mar 2014 13:47:54 +0100 Subject: [PATCH] Improved Daemonize function, to work with logs --- lib/config.cpp | 14 +++++++++++--- lib/config.h | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/config.cpp b/lib/config.cpp index fb2fff5e..8300a206 100644 --- a/lib/config.cpp +++ b/lib/config.cpp @@ -397,7 +397,11 @@ void Util::Config::activate(){ setUser(getString("username")); } if (vals.isMember("daemonize") && getBool("daemonize")){ - Daemonize(); + if(vals.isMember("logfile") && getString("logfile") != ""){ + Daemonize(true); + }else{ + Daemonize(false); + } } struct sigaction new_action; new_action.sa_handler = signal_handler; @@ -567,9 +571,13 @@ void Util::setUser(std::string username){ /// Works by calling daemon(1,0): /// Does not change directory to root. /// Does redirect output to /dev/null -void Util::Daemonize(){ +void Util::Daemonize(bool notClose){ DEBUG_MSG(DLVL_DEVEL, "Going into background mode..."); - if (daemon(1, 0) < 0){ + int noClose = 0; + if(notClose){ + noClose = 1; + } + if (daemon(1, noClose) < 0){ DEBUG_MSG(DLVL_ERROR, "Failed to daemonize: %s", strerror(errno)); } } diff --git a/lib/config.h b/lib/config.h index 98f080ee..2e25836b 100644 --- a/lib/config.h +++ b/lib/config.h @@ -52,6 +52,6 @@ namespace Util { void setUser(std::string user); /// Will turn the current process into a daemon. - void Daemonize(); + void Daemonize(bool notClose = false); }