diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -65,6 +65,8 @@ #endif #ifndef WIN32 +#include +#include #include #include #endif @@ -91,6 +93,31 @@ #define MIN_CORE_FILEDESCRIPTORS 150 #endif +/** + * 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 @@ -276,9 +303,12 @@ #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(); @@ -1888,7 +1918,10 @@ const CChainParams &chainparams = config.GetChainParams(); #ifndef WIN32 - CreatePidFile(GetPidFile(), getpid()); + if (!CreatePidFile()) { + // Detailed error printed inside CreatePidFile(). + return false; + } #endif BCLog::Logger &logger = LogInstance(); diff --git a/src/util/system.h b/src/util/system.h --- a/src/util/system.h +++ b/src/util/system.h @@ -40,7 +40,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; @@ -87,10 +86,6 @@ const fs::path &GetDataDir(bool fNetSpecific = true); 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); #endif diff --git a/src/util/system.cpp b/src/util/system.cpp --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -83,7 +83,6 @@ const int64_t nStartupTime = GetTime(); const char *const BITCOIN_CONF_FILENAME = "bitcoin.conf"; -const char *const BITCOIN_PID_FILENAME = "bitcoind.pid"; ArgsManager gArgs; @@ -1101,21 +1100,6 @@ return CBaseChainParams::MAIN; } -#ifndef WIN32 -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) { #ifdef WIN32 return MoveFileExA(src.string().c_str(), dest.string().c_str(),