From 745a2ace18ce857bc712d7e66c8bad7c082c07e2 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 2 Feb 2019 00:33:33 +0200 Subject: Improve PID file removing errors logging --- src/init.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/init.cpp b/src/init.cpp index 77d0505610..a5cd4d3cd1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -262,9 +262,11 @@ void Shutdown(InitInterfaces& interfaces) #ifndef WIN32 try { - fs::remove(GetPidFile()); + if (!fs::remove(GetPidFile())) { + LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__); + } } catch (const fs::filesystem_error& e) { - LogPrintf("%s: Unable to remove pidfile: %s\n", __func__, e.what()); + LogPrintf("%s: Unable to remove PID file: %s\n", __func__, e.what()); } #endif interfaces.chain_clients.clear(); -- cgit v1.2.3 From 561e375c73a37934fe77a519762d81edf7a3325c Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 2 Feb 2019 00:40:36 +0200 Subject: Make PID file creating errors fatal --- src/init.cpp | 21 ++++++++++++++++++++- src/util/system.cpp | 10 ---------- src/util/system.h | 1 - 3 files changed, 20 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/init.cpp b/src/init.cpp index a5cd4d3cd1..f8be487cb7 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -53,6 +53,8 @@ #include #ifndef WIN32 +#include +#include #include #include #endif @@ -1192,12 +1194,29 @@ bool AppInitLockDataDirectory() return true; } +#ifndef WIN32 +NODISCARD static bool CreatePidFile() +{ + FILE* file = fsbridge::fopen(GetPidFile(), "w"); + if (file) { + fprintf(file, "%d\n", getpid()); + fclose(file); + return true; + } else { + return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno))); + } +} +#endif + bool AppInitMain(InitInterfaces& interfaces) { const CChainParams& chainparams = Params(); // ********************************************************* Step 4a: application initialization #ifndef WIN32 - CreatePidFile(GetPidFile(), getpid()); + if (!CreatePidFile()) { + // Detailed error printed inside CreatePidFile(). + return false; + } #endif if (g_logger->m_print_to_file) { if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) { diff --git a/src/util/system.cpp b/src/util/system.cpp index 06317a3a90..8268456919 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -963,16 +963,6 @@ fs::path GetPidFile() { return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME))); } - -void CreatePidFile(const fs::path &path, pid_t pid) -{ - FILE* file = fsbridge::fopen(path, "w"); - if (file) - { - fprintf(file, "%d\n", pid); - fclose(file); - } -} #endif bool RenameOver(fs::path src, fs::path dest) diff --git a/src/util/system.h b/src/util/system.h index 5932e55793..44ba740e4e 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -86,7 +86,6 @@ void ClearDatadirCache(); fs::path GetConfigFile(const std::string& confPath); #ifndef WIN32 fs::path GetPidFile(); -void CreatePidFile(const fs::path &path, pid_t pid); #endif #ifdef WIN32 fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true); -- cgit v1.2.3 From 3782075a5fd4ad0c15a6119e8cdaf136898f679e Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 14 Feb 2019 22:53:03 +0200 Subject: Move all PID file stuff to init.cpp It is only used from init.cpp. Move-only refactoring. --- src/init.cpp | 38 ++++++++++++++++++++++++-------------- src/util/system.cpp | 8 -------- src/util/system.h | 4 ---- 3 files changed, 24 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/init.cpp b/src/init.cpp index f8be487cb7..63dc671fdf 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -94,6 +94,30 @@ std::unique_ptr g_banman; static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat"; +/** + * The PID file facilities. + */ +#ifndef WIN32 +static const char* BITCOIN_PID_FILENAME = "bitcoind.pid"; + +static fs::path GetPidFile() +{ + return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME))); +} + +NODISCARD static bool CreatePidFile() +{ + FILE* file = fsbridge::fopen(GetPidFile(), "w"); + if (file) { + fprintf(file, "%d\n", getpid()); + fclose(file); + return true; + } else { + return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno))); + } +} +#endif + ////////////////////////////////////////////////////////////////////////////// // // Shutdown @@ -1194,20 +1218,6 @@ bool AppInitLockDataDirectory() return true; } -#ifndef WIN32 -NODISCARD static bool CreatePidFile() -{ - FILE* file = fsbridge::fopen(GetPidFile(), "w"); - if (file) { - fprintf(file, "%d\n", getpid()); - fclose(file); - return true; - } else { - return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno))); - } -} -#endif - bool AppInitMain(InitInterfaces& interfaces) { const CChainParams& chainparams = Params(); diff --git a/src/util/system.cpp b/src/util/system.cpp index 8268456919..4934174420 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -79,7 +79,6 @@ const int64_t nStartupTime = GetTime(); const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf"; -const char * const BITCOIN_PID_FILENAME = "bitcoind.pid"; ArgsManager gArgs; @@ -958,13 +957,6 @@ std::string ArgsManager::GetChainName() const return CBaseChainParams::MAIN; } -#ifndef WIN32 -fs::path GetPidFile() -{ - return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME))); -} -#endif - bool RenameOver(fs::path src, fs::path dest) { #ifdef WIN32 diff --git a/src/util/system.h b/src/util/system.h index 44ba740e4e..5dc796c9ba 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -39,7 +39,6 @@ int64_t GetStartupTime(); extern const char * const BITCOIN_CONF_FILENAME; -extern const char * const BITCOIN_PID_FILENAME; /** Translate a message to the native language of the user. */ const extern std::function G_TRANSLATION_FUN; @@ -84,9 +83,6 @@ const fs::path &GetBlocksDir(); const fs::path &GetDataDir(bool fNetSpecific = true); void ClearDatadirCache(); fs::path GetConfigFile(const std::string& confPath); -#ifndef WIN32 -fs::path GetPidFile(); -#endif #ifdef WIN32 fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true); #endif -- cgit v1.2.3