Browse Source

Abstract directory locking into util.cpp

0.16
MeshCollider 7 years ago
parent
commit
2f3bd47d44
  1. 19
      src/init.cpp
  2. 22
      src/util.cpp
  3. 1
      src/util.h
  4. 21
      src/wallet/db.cpp

19
src/init.cpp

@ -1143,23 +1143,10 @@ bool AppInitParameterInteraction()
static bool LockDataDirectory(bool probeOnly) static bool LockDataDirectory(bool probeOnly)
{ {
std::string strDataDir = GetDataDir().string();
// Make sure only a single Bitcoin process is using the data directory. // Make sure only a single Bitcoin process is using the data directory.
fs::path pathLockFile = GetDataDir() / ".lock"; fs::path datadir = GetDataDir();
FILE* file = fsbridge::fopen(pathLockFile, "a"); // empty lock file; created if it doesn't exist. if (!LockDirectory(datadir, ".lock", probeOnly)) {
if (file) fclose(file); return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running."), datadir.string(), _(PACKAGE_NAME)));
try {
static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
if (!lock.try_lock()) {
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running."), strDataDir, _(PACKAGE_NAME)));
}
if (probeOnly) {
lock.unlock();
}
} catch(const boost::interprocess::interprocess_exception& e) {
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running.") + " %s.", strDataDir, _(PACKAGE_NAME), e.what()));
} }
return true; return true;
} }

22
src/util.cpp

@ -72,6 +72,7 @@
#include <boost/algorithm/string/case_conv.hpp> // for to_lower() #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith() #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/program_options/detail/config_file.hpp> #include <boost/program_options/detail/config_file.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <openssl/crypto.h> #include <openssl/crypto.h>
@ -375,6 +376,27 @@ int LogPrintStr(const std::string &str)
return ret; return ret;
} }
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only)
{
fs::path pathLockFile = directory / lockfile_name;
FILE* file = fsbridge::fopen(pathLockFile, "a"); // empty lock file; created if it doesn't exist.
if (file) fclose(file);
try {
static std::map<std::string, boost::interprocess::file_lock> locks;
boost::interprocess::file_lock& lock = locks.emplace(pathLockFile.string(), pathLockFile.string().c_str()).first->second;
if (!lock.try_lock()) {
return false;
}
if (probe_only) {
lock.unlock();
}
} catch (const boost::interprocess::interprocess_exception& e) {
return error("Error while attempting to lock directory %s: %s", directory.string(), e.what());
}
return true;
}
/** Interpret string as boolean, for argument parsing */ /** Interpret string as boolean, for argument parsing */
static bool InterpretBool(const std::string& strValue) static bool InterpretBool(const std::string& strValue)
{ {

1
src/util.h

@ -173,6 +173,7 @@ bool TruncateFile(FILE *file, unsigned int length);
int RaiseFileDescriptorLimit(int nMinFD); int RaiseFileDescriptorLimit(int nMinFD);
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length); void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
bool RenameOver(fs::path src, fs::path dest); bool RenameOver(fs::path src, fs::path dest);
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
bool TryCreateDirectories(const fs::path& p); bool TryCreateDirectories(const fs::path& p);
fs::path GetDefaultDataDir(); fs::path GetDefaultDataDir();
const fs::path &GetDataDir(bool fNetSpecific = true); const fs::path &GetDataDir(bool fNetSpecific = true);

21
src/wallet/db.cpp

@ -18,7 +18,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
namespace { namespace {
@ -53,24 +52,6 @@ void CheckUniqueFileid(const CDBEnv& env, const std::string& filename, Db& db)
} }
} }
} }
bool LockEnvDirectory(const fs::path& env_path)
{
// Make sure only a single Bitcoin process is using the wallet directory.
fs::path lock_file_path = env_path / ".walletlock";
FILE* file = fsbridge::fopen(lock_file_path, "a"); // empty lock file; created if it doesn't exist.
if (file) fclose(file);
try {
static boost::interprocess::file_lock lock(lock_file_path.string().c_str());
if (!lock.try_lock()) {
return false;
}
} catch (const boost::interprocess::interprocess_exception& e) {
return error("Error obtaining lock on wallet directory %s: %s.", env_path.string(), e.what());
}
return true;
}
} // namespace } // namespace
// //
@ -122,7 +103,7 @@ bool CDBEnv::Open(const fs::path& pathIn, bool retry)
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
strPath = pathIn.string(); strPath = pathIn.string();
if (!LockEnvDirectory(pathIn)) { if (!LockDirectory(pathIn, ".walletlock")) {
LogPrintf("Cannot obtain a lock on wallet directory %s. Another instance of bitcoin may be using it.\n", strPath); LogPrintf("Cannot obtain a lock on wallet directory %s. Another instance of bitcoin may be using it.\n", strPath);
return false; return false;
} }

Loading…
Cancel
Save