diff options
author | willcl-ark <will@256k1.dev> | 2023-11-27 10:29:02 +0000 |
---|---|---|
committer | willcl-ark <will@256k1.dev> | 2023-12-04 12:54:20 +0000 |
commit | 8f6ab318635d823a7e6ad9020ac17b8d2dd0e91b (patch) | |
tree | fbecfe3431f4cf8a46406771e97b4a28c2b11900 /src | |
parent | 160d23677ad799cf9b493eaa923b2ac080c3fb8e (diff) | |
download | bitcoin-8f6ab318635d823a7e6ad9020ac17b8d2dd0e91b.tar.xz |
init: don't delete PID file if it was not generated
Previously, starting a second bitcoind using the same datadir would
correctly fail to init and shutdown. However during shutdown the PID
file belonging to the first instance would be erroneously removed by
the second process shutting down.
Fix this to only delete the PID file if we created it.
Diffstat (limited to 'src')
-rw-r--r-- | src/init.cpp | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/init.cpp b/src/init.cpp index 526a8d6271..ac52b34fc5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -155,6 +155,11 @@ static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map"; * The PID file facilities. */ static const char* BITCOIN_PID_FILENAME = "bitcoind.pid"; +/** + * True if this process has created a PID file. + * Used to determine whether we should remove the PID file on shutdown. + */ +static bool g_generated_pid{false}; static fs::path GetPidFile(const ArgsManager& args) { @@ -170,12 +175,24 @@ static fs::path GetPidFile(const ArgsManager& args) #else tfm::format(file, "%d\n", getpid()); #endif + g_generated_pid = true; return true; } else { return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno))); } } +static void RemovePidFile(const ArgsManager& args) +{ + if (!g_generated_pid) return; + const auto pid_path{GetPidFile(args)}; + if (std::error_code error; !fs::remove(pid_path, error)) { + std::string msg{error ? error.message() : "File does not exist"}; + LogPrintf("Unable to remove PID file (%s): %s\n", fs::PathToString(pid_path), msg); + } +} + + ////////////////////////////////////////////////////////////////////////////// // // Shutdown @@ -352,13 +369,7 @@ void Shutdown(NodeContext& node) node.scheduler.reset(); node.kernel.reset(); - try { - if (!fs::remove(GetPidFile(*node.args))) { - LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__); - } - } catch (const fs::filesystem_error& e) { - LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e)); - } + RemovePidFile(*node.args); LogPrintf("%s: done\n", __func__); } |