Diff-minimise

This commit is contained in:
Luke Dashjr 2025-01-25 16:03:50 +00:00
parent 145dd295d9
commit ca56c7c9c4
5 changed files with 25 additions and 25 deletions

View File

@ -221,10 +221,10 @@ static bool AppInit(NodeContext& node)
return InitError(Untranslated("-daemon is not supported on this operating system")); return InitError(Untranslated("-daemon is not supported on this operating system"));
#endif // HAVE_DECL_FORK #endif // HAVE_DECL_FORK
} }
// Lock critical directories after daemonization // Lock data directory after daemonization
if (!AppInitLockDirectories()) if (!AppInitLockDataDirectory())
{ {
// If locking a directory failed, exit immediately // If locking the data directory failed, exit immediately
return false; return false;
} }
fRet = AppInitInterfaces(node) && AppInitMain(node); fRet = AppInitInterfaces(node) && AppInitMain(node);

View File

@ -1083,19 +1083,19 @@ bool AppInitParameterInteraction(const ArgsManager& args)
return true; return true;
} }
static bool LockDirectory(const fs::path& dir, bool probeOnly) static bool LockDirectory(const fs::path& datadir, bool probeOnly)
{ {
// Make sure only a single process is using the directory. // Make sure only a single Bitcoin process is using the data directory.
switch (util::LockDirectory(dir, ".lock", probeOnly)) { switch (util::LockDirectory(datadir, ".lock", probeOnly)) {
case util::LockResult::ErrorWrite: case util::LockResult::ErrorWrite:
return InitError(strprintf(_("Cannot write to directory '%s'; check permissions."), fs::PathToString(dir))); return InitError(strprintf(_("Cannot write to data directory '%s'; check permissions."), fs::PathToString(datadir)));
case util::LockResult::ErrorLock: case util::LockResult::ErrorLock:
return InitError(strprintf(_("Cannot obtain a lock on directory %s. %s is probably already running."), fs::PathToString(dir), PACKAGE_NAME)); return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running."), fs::PathToString(datadir), PACKAGE_NAME));
case util::LockResult::Success: return true; case util::LockResult::Success: return true;
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
assert(false); assert(false);
} }
static bool LockDirectories(bool probeOnly) static bool LockDataDirectory(bool probeOnly)
{ {
return LockDirectory(gArgs.GetDataDirNet(), probeOnly) && \ return LockDirectory(gArgs.GetDataDirNet(), probeOnly) && \
LockDirectory(gArgs.GetBlocksDirPath(), probeOnly); LockDirectory(gArgs.GetBlocksDirPath(), probeOnly);
@ -1114,19 +1114,19 @@ bool AppInitSanityChecks(const kernel::Context& kernel)
return InitError(strprintf(_("Elliptic curve cryptography sanity check failure. %s is shutting down."), PACKAGE_NAME)); return InitError(strprintf(_("Elliptic curve cryptography sanity check failure. %s is shutting down."), PACKAGE_NAME));
} }
// Probe the directory locks to give an early error message, if possible // Probe the data directory lock to give an early error message, if possible
// We cannot hold the directory locks here, as the forking for daemon() hasn't yet happened, // We cannot hold the data directory lock here, as the forking for daemon() hasn't yet happened,
// and a fork will cause weird behavior to them. // and a fork will cause weird behavior to it.
return LockDirectories(true); return LockDataDirectory(true);
} }
bool AppInitLockDirectories() bool AppInitLockDataDirectory()
{ {
// After daemonization get the directory locks again and hold on to them until exit // After daemonization get the data directory lock again and hold on to it until exit
// This creates a slight window for a race condition to happen, however this condition is harmless: it // This creates a slight window for a race condition to happen, however this condition is harmless: it
// will at most make us exit without printing a message to console. // will at most make us exit without printing a message to console.
if (!LockDirectories(false)) { if (!LockDataDirectory(false)) {
// Detailed error printed inside LockDirectory // Detailed error printed inside LockDataDirectory
return false; return false;
} }
return true; return true;

View File

@ -55,11 +55,11 @@ bool AppInitParameterInteraction(const ArgsManager& args);
*/ */
bool AppInitSanityChecks(const kernel::Context& kernel); bool AppInitSanityChecks(const kernel::Context& kernel);
/** /**
* Lock bitcoin core critical directories. * Lock bitcoin core data directory.
* @note This should only be done after daemonization. Do not call Shutdown() if this function fails. * @note This should only be done after daemonization. Do not call Shutdown() if this function fails.
* @pre Parameters should be parsed and config file should be read, AppInitSanityChecks should have been called. * @pre Parameters should be parsed and config file should be read, AppInitSanityChecks should have been called.
*/ */
bool AppInitLockDirectories(); bool AppInitLockDataDirectory();
/** /**
* Initialize node and wallet interface pointers. Has no prerequisites or side effects besides allocating memory. * Initialize node and wallet interface pointers. Has no prerequisites or side effects besides allocating memory.
*/ */
@ -67,7 +67,7 @@ bool AppInitInterfaces(node::NodeContext& node);
/** /**
* Bitcoin core main initialization. * Bitcoin core main initialization.
* @note This should only be done after daemonization. Call Shutdown() if this function fails. * @note This should only be done after daemonization. Call Shutdown() if this function fails.
* @pre Parameters should be parsed and config file should be read, AppInitLockDirectories should have been called. * @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
*/ */
bool AppInitMain(node::NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr); bool AppInitMain(node::NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr);

View File

@ -111,7 +111,7 @@ public:
m_context->ecc_context = std::make_unique<ECC_Context>(); m_context->ecc_context = std::make_unique<ECC_Context>();
if (!AppInitSanityChecks(*m_context->kernel)) return false; if (!AppInitSanityChecks(*m_context->kernel)) return false;
if (!AppInitLockDirectories()) return false; if (!AppInitLockDataDirectory()) return false;
if (!AppInitInterfaces(*m_context)) return false; if (!AppInitInterfaces(*m_context)) return false;
return true; return true;

View File

@ -24,19 +24,19 @@ class FilelockTest(BitcoinTestFramework):
def run_test(self): def run_test(self):
datadir = self.nodes[0].chain_path datadir = self.nodes[0].chain_path
blocksdir = self.nodes[0].blocks_path
self.log.info(f"Using datadir {datadir}") self.log.info(f"Using datadir {datadir}")
blocksdir = self.nodes[0].blocks_path
self.log.info(f"Using blocksdir {blocksdir}") self.log.info(f"Using blocksdir {blocksdir}")
self.log.info("Check that we can't start a second bitcoind instance using the same datadir") self.log.info("Check that we can't start a second bitcoind instance using the same datadir")
expected_msg = f"Error: Cannot obtain a lock on directory {datadir}. {self.config['environment']['PACKAGE_NAME']} is probably already running." expected_msg = f"Error: Cannot obtain a lock on data directory {datadir}. {self.config['environment']['PACKAGE_NAME']} is probably already running."
self.nodes[1].assert_start_raises_init_error(extra_args=[f'-datadir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg) self.nodes[1].assert_start_raises_init_error(extra_args=[f'-datadir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg)
self.log.info("Check that we can't start a second bitcoind instance using the same blocksdir") self.log.info("Check that we can't start a second bitcoind instance using the same blocksdir")
expected_msg = f"Error: Cannot obtain a lock on directory {blocksdir}. {self.config['environment']['CLIENT_NAME']} is probably already running." expected_msg = f"Error: Cannot obtain a lock on data directory {blocksdir}. {self.config['environment']['PACKAGE_NAME']} is probably already running."
self.nodes[1].assert_start_raises_init_error(extra_args=[f'-blocksdir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg) self.nodes[1].assert_start_raises_init_error(extra_args=[f'-blocksdir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg)
self.log.info("Check that cookie and PID file are not deleted when attempting to start a second bitcoind using the same datadir/blocksdir") self.log.info("Check that cookie and PID file are not deleted when attempting to start a second bitcoind using the same datadir")
cookie_file = datadir / ".cookie" cookie_file = datadir / ".cookie"
assert cookie_file.exists() # should not be deleted during the second bitcoind instance shutdown assert cookie_file.exists() # should not be deleted during the second bitcoind instance shutdown
pid_file = datadir / "bitcoind.pid" pid_file = datadir / "bitcoind.pid"