From: Remi Gacogne Date: Fri, 27 Jun 2025 14:37:46 +0000 (+0200) Subject: dnsdist: Prevent users from opening the same LMDB twice X-Git-Tag: rec-5.3.0-alpha2~39^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F15738%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Prevent users from opening the same LMDB twice As noted by Peter: "LMBD requires that database is opened exactly once per process. Opening multiple times breaks file locks silently, which leads to corrupting the database." While I don't expect users to actually do that, we already have a nice helper function to prevent this mistake in the lmdb-safe code base, so let's use it. Signed-off-by: Remi Gacogne --- diff --git a/pdns/dnsdistdist/dnsdist-kvs.cc b/pdns/dnsdistdist/dnsdist-kvs.cc index d20aa1e980..e8084566dc 100644 --- a/pdns/dnsdistdist/dnsdist-kvs.cc +++ b/pdns/dnsdistdist/dnsdist-kvs.cc @@ -85,7 +85,7 @@ std::vector KeyValueLookupKeySuffix::getKeys(const DNSName& qname) bool LMDBKVStore::getValue(const std::string& key, std::string& value) { try { - auto transaction = d_env.getROTransaction(); + auto transaction = d_env->getROTransaction(); MDBOutVal result; int rc = transaction->get(d_dbi, MDBInVal(key), result); if (rc == 0) { @@ -105,7 +105,7 @@ bool LMDBKVStore::getValue(const std::string& key, std::string& value) bool LMDBKVStore::keyExists(const std::string& key) { try { - auto transaction = d_env.getROTransaction(); + auto transaction = d_env->getROTransaction(); MDBOutVal result; int rc = transaction->get(d_dbi, MDBInVal(key), result); if (rc == 0) { @@ -124,7 +124,7 @@ bool LMDBKVStore::keyExists(const std::string& key) bool LMDBKVStore::getRangeValue(const std::string& key, std::string& value) { try { - auto transaction = d_env.getROTransaction(); + auto transaction = d_env->getROTransaction(); auto cursor = transaction->getROCursor(d_dbi); MDBOutVal actualKey; MDBOutVal result; diff --git a/pdns/dnsdistdist/dnsdist-kvs.hh b/pdns/dnsdistdist/dnsdist-kvs.hh index 5672986e82..ad8c11c3a3 100644 --- a/pdns/dnsdistdist/dnsdist-kvs.hh +++ b/pdns/dnsdistdist/dnsdist-kvs.hh @@ -177,7 +177,7 @@ public: class LMDBKVStore: public KeyValueStore { public: - LMDBKVStore(const std::string& fname, const std::string& dbName, bool noLock=false): d_env(fname.c_str(), noLock ? MDB_NOSUBDIR|MDB_RDONLY|MDB_NOLOCK : MDB_NOSUBDIR|MDB_RDONLY, 0600, 0), d_dbi(d_env.openDB(dbName, 0)), d_fname(fname), d_dbName(dbName) + LMDBKVStore(const std::string& fname, const std::string& dbName, bool noLock=false): d_env(getMDBEnv(fname.c_str(), noLock ? MDB_NOSUBDIR|MDB_RDONLY|MDB_NOLOCK : MDB_NOSUBDIR|MDB_RDONLY, 0600, 0)), d_dbi(d_env->openDB(dbName, 0)), d_fname(fname), d_dbName(dbName) { } @@ -186,7 +186,7 @@ public: bool getRangeValue(const std::string& key, std::string& value) override; private: - MDBEnv d_env; + std::shared_ptr d_env; MDBDbi d_dbi; std::string d_fname; std::string d_dbName;