]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Prevent users from opening the same LMDB twice 15738/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 27 Jun 2025 14:37:46 +0000 (16:37 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 27 Jun 2025 14:37:46 +0000 (16:37 +0200)
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 <remi.gacogne@powerdns.com>
pdns/dnsdistdist/dnsdist-kvs.cc
pdns/dnsdistdist/dnsdist-kvs.hh

index d20aa1e980ea35ea3cb8ff365a5aea147dd45a6f..e8084566dc480bc77ee3fa8fa6968d7d7ea44305 100644 (file)
@@ -85,7 +85,7 @@ std::vector<std::string> 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;
index 5672986e82019fdb2ee72081ba31532efb6c1d50..ad8c11c3a3d24a03b6f201016cef50b4c6052cb3 100644 (file)
@@ -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<MDBEnv> d_env;
   MDBDbi d_dbi;
   std::string d_fname;
   std::string d_dbName;