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/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 77d050561..a5cd4d3cd 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 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index a5cd4d3cd..f8be487cb 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())) { -- 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 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index f8be487cb..63dc671fd 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(); -- cgit v1.2.3