diff --git a/src/bridge/HostBridge.cpp b/src/bridge/HostBridge.cpp index fb0e26ec..592d50c0 100644 --- a/src/bridge/HostBridge.cpp +++ b/src/bridge/HostBridge.cpp @@ -370,13 +370,7 @@ int HostBridge::run() if (m_daemon && g_foreground) m_daemon = false; - // initialize system logging yaml::Node logConf = m_conf["log"]; - ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), - logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U)); - if (!ret) { - ::fatal("unable to open the log file\n"); - } #if !defined(_WIN32) // handle POSIX process forking @@ -385,21 +379,29 @@ int HostBridge::run() pid_t pid = ::fork(); if (pid == -1) { ::fprintf(stderr, "%s: Couldn't fork() , exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } else if (pid != 0) { - ::LogFinalise(); exit(EXIT_SUCCESS); } // create new session and process group if (::setsid() == -1) { ::fprintf(stderr, "%s: Couldn't setsid(), exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } + } +#endif // !defined(_WIN32) + // initialize system logging after forking so the async worker belongs to the daemon child + ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), + logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U)); + if (!ret) { + ::fatal("unable to open the log file\n"); + } + +#if !defined(_WIN32) + if (m_daemon) { // set the working directory to the root directory if (::chdir("/") == -1) { ::fprintf(stderr, "%s: Couldn't cd /, exiting\n", g_progExe.c_str()); diff --git a/src/fne/HostFNE.cpp b/src/fne/HostFNE.cpp index af067fa1..fd7d48d4 100644 --- a/src/fne/HostFNE.cpp +++ b/src/fne/HostFNE.cpp @@ -112,16 +112,10 @@ int HostFNE::run() if (m_daemon && g_foreground) m_daemon = false; - // initialize system logging yaml::Node logConf = m_conf["log"]; bool useSyslog = logConf["useSyslog"].as(false); if (g_foreground) useSyslog = false; - ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), - logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U), false, useSyslog); - if (!ret) { - ::fatal("unable to open the log file\n"); - } #if !defined(_WIN32) // handle POSIX process forking @@ -130,21 +124,29 @@ int HostFNE::run() pid_t pid = ::fork(); if (pid == -1) { ::fprintf(stderr, "%s: Couldn't fork() , exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } else if (pid != 0) { - ::LogFinalise(); exit(EXIT_SUCCESS); } // create new session and process group if (::setsid() == -1) { ::fprintf(stderr, "%s: Couldn't setsid(), exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } + } +#endif // !defined(_WIN32) + // initialize system logging after forking so the async worker belongs to the daemon child + ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), + logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U), false, useSyslog); + if (!ret) { + ::fatal("unable to open the log file\n"); + } + +#if !defined(_WIN32) + if (m_daemon) { // set the working directory to the root directory if (::chdir("/") == -1) { ::fprintf(stderr, "%s: Couldn't cd /, exiting\n", g_progExe.c_str()); diff --git a/src/host/Host.cpp b/src/host/Host.cpp index e92b009f..3cf76f1d 100644 --- a/src/host/Host.cpp +++ b/src/host/Host.cpp @@ -195,17 +195,11 @@ int Host::run() if (m_daemon && g_foreground) m_daemon = false; - // initialize system logging yaml::Node logConf = m_conf["log"]; bool useSyslog = logConf["useSyslog"].as(false); g_disableNonAuthoritativeLogging = logConf["disableNonAuthoritiveLogging"].as(false); if (g_foreground) useSyslog = false; - ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), - logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U), false, useSyslog); - if (!ret) { - ::fatal("unable to open the log file\n"); - } #if !defined(_WIN32) // handle POSIX process forking @@ -214,21 +208,29 @@ int Host::run() pid_t pid = ::fork(); if (pid == -1) { ::fprintf(stderr, "%s: Couldn't fork() , exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } else if (pid != 0) { - ::LogFinalise(); exit(EXIT_SUCCESS); } // create new session and process group if (::setsid() == -1) { ::fprintf(stderr, "%s: Couldn't setsid(), exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } + } +#endif // !defined(_WIN32) + // initialize system logging after forking so the async worker belongs to the daemon child + ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), + logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U), false, useSyslog); + if (!ret) { + ::fatal("unable to open the log file\n"); + } + +#if !defined(_WIN32) + if (m_daemon) { // set the working directory to the root directory if (::chdir("/") == -1) { ::fprintf(stderr, "%s: Couldn't cd /, exiting\n", g_progExe.c_str()); diff --git a/src/patch/HostPatch.cpp b/src/patch/HostPatch.cpp index bca6a3f8..b30cb6f1 100644 --- a/src/patch/HostPatch.cpp +++ b/src/patch/HostPatch.cpp @@ -151,13 +151,7 @@ int HostPatch::run() if (m_daemon && g_foreground) m_daemon = false; - // initialize system logging yaml::Node logConf = m_conf["log"]; - ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), - logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U)); - if (!ret) { - ::fatal("unable to open the log file\n"); - } #if !defined(_WIN32) // handle POSIX process forking @@ -166,21 +160,29 @@ int HostPatch::run() pid_t pid = ::fork(); if (pid == -1) { ::fprintf(stderr, "%s: Couldn't fork() , exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } else if (pid != 0) { - ::LogFinalise(); exit(EXIT_SUCCESS); } // create new session and process group if (::setsid() == -1) { ::fprintf(stderr, "%s: Couldn't setsid(), exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } + } +#endif // !defined(_WIN32) + // initialize system logging after forking so the async worker belongs to the daemon child + ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), + logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U)); + if (!ret) { + ::fatal("unable to open the log file\n"); + } + +#if !defined(_WIN32) + if (m_daemon) { // set the working directory to the root directory if (::chdir("/") == -1) { ::fprintf(stderr, "%s: Couldn't cd /, exiting\n", g_progExe.c_str()); diff --git a/src/sysview/HostWS.cpp b/src/sysview/HostWS.cpp index ef473e4e..56ca6c3c 100644 --- a/src/sysview/HostWS.cpp +++ b/src/sysview/HostWS.cpp @@ -175,13 +175,7 @@ int HostWS::run() if (m_daemon && g_foreground) m_daemon = false; - // re-initialize system logging yaml::Node logConf = m_conf["log"]; - ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), - logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U)); - if (!ret) { - ::fatal("unable to open the log file\n"); - } // handle POSIX process forking if (m_daemon) { @@ -189,21 +183,27 @@ int HostWS::run() pid_t pid = ::fork(); if (pid == -1) { ::fprintf(stderr, "%s: Couldn't fork() , exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } else if (pid != 0) { - ::LogFinalise(); exit(EXIT_SUCCESS); } // create new session and process group if (::setsid() == -1) { ::fprintf(stderr, "%s: Couldn't setsid(), exiting\n", g_progExe.c_str()); - ::LogFinalise(); return EXIT_FAILURE; } + } + + // re-initialize system logging after forking so the async worker belongs to the daemon child + ret = ::LogInitialise(logConf["filePath"].as(), logConf["fileRoot"].as(), + logConf["fileLevel"].as(0U), logConf["displayLevel"].as(0U)); + if (!ret) { + ::fatal("unable to open the log file\n"); + } + if (m_daemon) { // set the working directory to the root directory if (::chdir("/") == -1) { ::fprintf(stderr, "%s: Couldn't cd /, exiting\n", g_progExe.c_str());