Improved Daemonize function, to work with logs

This commit is contained in:
martin 2014-03-11 13:47:54 +01:00 committed by Thulinma
parent 1063daeff4
commit 1d1519111f
2 changed files with 12 additions and 4 deletions

View file

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

View file

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