From 73b6b4e7c0a450bb88074a7d59a9237b4703302b Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 13 Jul 2017 20:52:26 +0200 Subject: [PATCH] init: Keep track of whether data directory locked, don't cleanup if not Keep a flag indicating whether the data directory was locked. If not, Interrupt and Shutdown are no-ops. This avoids things from being cleaned up if they were created by another instance. --- src/init.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/init.cpp b/src/init.cpp index 1e85642019c..15b5132ac4b 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -75,6 +75,9 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false; std::unique_ptr g_connman; std::unique_ptr peerLogic; +/** Set at startup if the data directory was succesfully locked */ +bool g_locked_data_directory = false; + #if ENABLE_ZMQ static CZMQNotificationInterface* pzmqNotificationInterface = NULL; #endif @@ -158,6 +161,11 @@ static std::unique_ptr globalVerifyHandle; void Interrupt(boost::thread_group& threadGroup) { + if (!g_locked_data_directory) { + // If we never succeeded in locking the data directory, do not interrupt anything. + // Nothing will have been started anyway. + return; + } InterruptHTTPServer(); InterruptHTTPRPC(); InterruptRPC(); @@ -170,6 +178,11 @@ void Interrupt(boost::thread_group& threadGroup) void Shutdown() { + if (!g_locked_data_directory) { + // If we never succeeded in locking the data directory, do not clean up anything. + // This avoids deleting RPC cookies of the running instance and worse. + return; + } LogPrintf("%s: In progress...\n", __func__); static CCriticalSection cs_Shutdown; TRY_LOCK(cs_Shutdown, lockShutdown); @@ -1184,6 +1197,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler) // Detailed error printed inside LockDataDirectory return false; } + g_locked_data_directory = true; #ifndef WIN32 CreatePidFile(GetPidFile(), getpid());