From bfd20147c7bbfd6d66a3b469e3f95d11c113eb8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Fri, 16 Jun 2017 23:41:58 +0200 Subject: [PATCH] When daemonized, parent closes proxysql.log #1049 --- src/main.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 6026d8f1d..eb42f3d38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,35 @@ #endif +void parent_open_error_log() { + if (GloVars.global.foreground==false) { + int outfd=0; + int errfd=0; + outfd=open(GloVars.errorlog, O_WRONLY | O_APPEND | O_CREAT , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (outfd>0) { + dup2(outfd, STDOUT_FILENO); + close(outfd); + } else { + proxy_error("Impossible to open file\n"); + } + errfd=open(GloVars.errorlog, O_WRONLY | O_APPEND | O_CREAT , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (errfd>0) { + dup2(errfd, STDERR_FILENO); + close(errfd); + } else { + proxy_error("Impossible to open file\n"); + } + } +} + + +void parent_close_error_log() { + if (GloVars.global.foreground==false) { + close(STDOUT_FILENO); + close(STDERR_FILENO); + } +} + time_t laststart; pid_t pid; @@ -650,9 +679,12 @@ bool ProxySQL_daemonize_phase3() { int rc; int status; //daemon_log(LOG_INFO, "Angel process started ProxySQL process %d\n", pid); + parent_open_error_log(); proxy_info("Angel process started ProxySQL process %d\n", pid); + parent_close_error_log(); rc=waitpid(pid, &status, 0); if (rc==-1) { + parent_open_error_log(); perror("waitpid"); //proxy_error("[FATAL]: waitpid: %s\n", perror("waitpid")); exit(EXIT_FAILURE); @@ -662,16 +694,21 @@ bool ProxySQL_daemonize_phase3() { rc=WEXITSTATUS(status); if (rc==0) { //daemon_log(LOG_INFO, "Shutdown angel process\n"); + parent_open_error_log(); proxy_info("Shutdown angel process\n"); exit(EXIT_SUCCESS); } else { //daemon_log(LOG_INFO, "ProxySQL exited with code %d . Restarting!\n", rc); + parent_open_error_log(); proxy_error("ProxySQL exited with code %d . Restarting!\n", rc); + parent_close_error_log(); return false; } } else { //daemon_log(LOG_INFO, "ProxySQL crashed. Restarting!\n"); + parent_open_error_log(); proxy_error("ProxySQL crashed. Restarting!\n"); + parent_close_error_log(); return false; } return true; @@ -733,25 +770,32 @@ int main(int argc, const char * argv[]) { gotofork: if (laststart) { //daemon_log(LOG_INFO, "Angel process is waiting %d seconds before starting a new ProxySQL process\n", glovars.proxy_restart_delay); + parent_open_error_log(); proxy_info("Angel process is waiting %d seconds before starting a new ProxySQL process\n", glovars.proxy_restart_delay); + parent_close_error_log(); sleep(glovars.proxy_restart_delay); } laststart=time(NULL); pid = fork(); if (pid < 0) { //daemon_log(LOG_INFO, "[FATAL]: Error in fork()\n"); + parent_open_error_log(); proxy_error("[FATAL]: Error in fork()\n"); exit(EXIT_FAILURE); } if (pid) { /* The parent */ + parent_close_error_log(); if (ProxySQL_daemonize_phase3()==false) { goto gotofork; } } else { /* The daemon */ + // we open the files also on the child process + // this is required if the child process was created after a crash + parent_open_error_log(); GloVars.global.start_time=monotonic_time(); GloVars.install_signal_handler(); }