aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_core/cryptonote_core.cpp
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2016-01-31 12:58:08 +0000
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2016-01-31 12:58:08 +0000
commit8b3539bc11639f2feaebdbb2fcbc9578806f5702 (patch)
tree527b7c22dee975a89cfd8ef245f650930ec13067 /src/cryptonote_core/cryptonote_core.cpp
parentb91fc2dc3c4489f44a74915ae80fd1968d150302 (diff)
downloadmonzero-core-8b3539bc11639f2feaebdbb2fcbc9578806f5702.tar.gz
monzero-core-8b3539bc11639f2feaebdbb2fcbc9578806f5702.tar.xz
monzero-core-8b3539bc11639f2feaebdbb2fcbc9578806f5702.zip
core: prevent the database from being used by multiple daemons
A boost lock is used to determine whether more than one process wants to access the database. The boost file_lock doesn't seem to like locking directories, so we use an arbitrary file in it. This allows to still run two daemons if they have different database directories (ie, LMDB/BDB, different data directories).
Diffstat (limited to 'src/cryptonote_core/cryptonote_core.cpp')
-rw-r--r--src/cryptonote_core/cryptonote_core.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index ac0be5a07..8d50e8bfd 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -218,6 +218,39 @@ namespace cryptonote
return m_blockchain_storage.get_alternative_blocks_count();
}
//-----------------------------------------------------------------------------------------------
+ bool core::lock_db_directory(const boost::filesystem::path &path)
+ {
+ // boost doesn't like locking directories...
+ const boost::filesystem::path lock_path = path / ".daemon_lock";
+
+ try
+ {
+ // ensure the file exists
+ std::ofstream(lock_path.string(), std::ios::out).close();
+
+ db_lock = boost::interprocess::file_lock(lock_path.string().c_str());
+ LOG_PRINT_L1("Locking " << lock_path.string());
+ if (!db_lock.try_lock())
+ {
+ LOG_PRINT_L0("Failed to lock " << lock_path.string());
+ return false;
+ }
+ return true;
+ }
+ catch (const std::exception &e)
+ {
+ LOG_PRINT_L0("Error trying to lock " << lock_path.string() << ": " << e.what());
+ return false;
+ }
+ }
+ //-----------------------------------------------------------------------------------------------
+ bool core::unlock_db_directory()
+ {
+ db_lock.unlock();
+ db_lock = boost::interprocess::file_lock();
+ return true;
+ }
+ //-----------------------------------------------------------------------------------------------
bool core::init(const boost::program_options::variables_map& vm)
{
bool r = handle_command_line(vm);
@@ -280,6 +313,13 @@ namespace cryptonote
folder /= db->get_db_name();
LOG_PRINT_L0("Loading blockchain from folder " << folder.string() << " ...");
+ if (!lock_db_directory (folder))
+ {
+ LOG_ERROR ("Failed to lock " << folder);
+ delete db;
+ return false;
+ }
+
const std::string filename = folder.string();
// temporarily default to fastest:async:1000
blockchain_db_sync_mode sync_mode = db_async;
@@ -398,6 +438,7 @@ namespace cryptonote
{
m_blockchain_storage.deinit();
}
+ unlock_db_directory();
return true;
}
//-----------------------------------------------------------------------------------------------