Skip to content

Commit 83c302e

Browse files
authored
Merge pull request #2688 from ziollek/lmdb_single_env
LMDB - fix integration, restoring ability of use lmdb with nginx-modsecurity
2 parents 606f572 + 82326ff commit 83c302e

File tree

2 files changed

+87
-22
lines changed

2 files changed

+87
-22
lines changed

src/collection/backend/lmdb.cc

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <string>
2323
#include <memory>
2424

25+
#include <pthread.h>
26+
2527
#include "modsecurity/variable_value.h"
2628
#include "src/utils/regex.h"
2729
#include "src/variables/variable.h"
@@ -36,22 +38,17 @@ namespace backend {
3638
#ifdef WITH_LMDB
3739

3840
LMDB::LMDB(std::string name) :
39-
Collection(name), m_env(NULL) {
40-
MDB_txn *txn;
41-
mdb_env_create(&m_env);
42-
mdb_env_open(m_env, "./modsec-shared-collections",
43-
MDB_WRITEMAP | MDB_NOSUBDIR, 0664);
44-
mdb_txn_begin(m_env, NULL, 0, &txn);
45-
mdb_dbi_open(txn, NULL, MDB_CREATE | MDB_DUPSORT, &m_dbi);
46-
mdb_txn_commit(txn);
47-
}
48-
41+
Collection(name), m_env(NULL), isOpen(false) {}
4942

50-
LMDB::~LMDB() {
51-
mdb_env_close(m_env);
43+
int LMDB::txn_begin(unsigned int flags, MDB_txn **ret) {
44+
if (!isOpen) {
45+
m_env = MDBEnvProvider::GetInstance().GetEnv();
46+
m_dbi = *(MDBEnvProvider::GetInstance().GetDBI());
47+
isOpen = true;
48+
}
49+
return mdb_txn_begin(m_env, NULL, flags, ret);
5250
}
5351

54-
5552
void LMDB::string2val(const std::string& str, MDB_val *val) {
5653
val->mv_size = sizeof(char)*(str.size());
5754
val->mv_data = const_cast<char *>(str.c_str());
@@ -159,7 +156,7 @@ std::unique_ptr<std::string> LMDB::resolveFirst(const std::string& var) {
159156

160157
string2val(var, &mdb_key);
161158

162-
rc = mdb_txn_begin(m_env, NULL, MDB_RDONLY, &txn);
159+
rc = txn_begin(MDB_RDONLY, &txn);
163160
lmdb_debug(rc, "txn", "resolveFirst");
164161
if (rc != 0) {
165162
goto end_txn;
@@ -192,7 +189,7 @@ bool LMDB::storeOrUpdateFirst(const std::string &key,
192189
string2val(key, &mdb_key);
193190
string2val(value, &mdb_value);
194191

195-
rc = mdb_txn_begin(m_env, NULL, 0, &txn);
192+
rc = txn_begin(0, &txn);
196193
lmdb_debug(rc, "txn", "storeOrUpdateFirst");
197194
if (rc != 0) {
198195
goto end_txn;
@@ -240,7 +237,7 @@ void LMDB::resolveSingleMatch(const std::string& var,
240237
MDB_val mdb_value_ret;
241238
MDB_cursor *cursor;
242239

243-
rc = mdb_txn_begin(m_env, NULL, MDB_RDONLY, &txn);
240+
rc = txn_begin(MDB_RDONLY, &txn);
244241
lmdb_debug(rc, "txn", "resolveSingleMatch");
245242
if (rc != 0) {
246243
goto end_txn;
@@ -271,7 +268,7 @@ void LMDB::store(std::string key, std::string value) {
271268
int rc;
272269
MDB_stat mst;
273270

274-
rc = mdb_txn_begin(m_env, NULL, 0, &txn);
271+
rc = txn_begin(0, &txn);
275272
lmdb_debug(rc, "txn", "store");
276273
if (rc != 0) {
277274
goto end_txn;
@@ -310,7 +307,7 @@ bool LMDB::updateFirst(const std::string &key,
310307
MDB_val mdb_value;
311308
MDB_val mdb_value_ret;
312309

313-
rc = mdb_txn_begin(m_env, NULL, 0, &txn);
310+
rc = txn_begin(0, &txn);
314311
lmdb_debug(rc, "txn", "updateFirst");
315312
if (rc != 0) {
316313
goto end_txn;
@@ -364,7 +361,7 @@ void LMDB::del(const std::string& key) {
364361
MDB_val mdb_value_ret;
365362
MDB_stat mst;
366363

367-
rc = mdb_txn_begin(m_env, NULL, 0, &txn);
364+
rc = txn_begin(0, &txn);
368365
lmdb_debug(rc, "txn", "del");
369366
if (rc != 0) {
370367
goto end_txn;
@@ -411,7 +408,7 @@ void LMDB::resolveMultiMatches(const std::string& var,
411408
size_t keySize = var.size();
412409
MDB_cursor *cursor;
413410

414-
rc = mdb_txn_begin(m_env, NULL, MDB_RDONLY, &txn);
411+
rc = txn_begin(MDB_RDONLY, &txn);
415412
lmdb_debug(rc, "txn", "resolveMultiMatches");
416413
if (rc != 0) {
417414
goto end_txn;
@@ -465,7 +462,7 @@ void LMDB::resolveRegularExpression(const std::string& var,
465462

466463
Utils::Regex r(var, true);
467464

468-
rc = mdb_txn_begin(m_env, NULL, MDB_RDONLY, &txn);
465+
rc = txn_begin(MDB_RDONLY, &txn);
469466
lmdb_debug(rc, "txn", "resolveRegularExpression");
470467
if (rc != 0) {
471468
goto end_txn;
@@ -503,6 +500,30 @@ void LMDB::resolveRegularExpression(const std::string& var,
503500
return;
504501
}
505502

503+
504+
MDBEnvProvider::MDBEnvProvider() : m_env(NULL) {
505+
MDB_txn *txn;
506+
mdb_env_create(&m_env);
507+
mdb_env_open(m_env, "./modsec-shared-collections",
508+
MDB_WRITEMAP | MDB_NOSUBDIR, 0664);
509+
mdb_txn_begin(m_env, NULL, 0, &txn);
510+
mdb_dbi_open(txn, NULL, MDB_CREATE | MDB_DUPSORT, &m_dbi);
511+
mdb_txn_commit(txn);
512+
}
513+
514+
MDB_env* MDBEnvProvider::GetEnv() {
515+
return m_env;
516+
}
517+
518+
MDB_dbi* MDBEnvProvider::GetDBI() {
519+
return &m_dbi;
520+
}
521+
522+
MDBEnvProvider::~MDBEnvProvider() {
523+
mdb_dbi_close(m_env, m_dbi);
524+
mdb_env_close(m_env);
525+
}
526+
506527
#endif
507528

508529
} // namespace backend

src/collection/backend/lmdb.h

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,53 @@ namespace modsecurity {
4848
namespace collection {
4949
namespace backend {
5050

51+
52+
/**
53+
* The MDBEnvProvider class defines the `GetInstance` method that serves as an
54+
* alternative to constructor and lets clients access the same instance of this
55+
* class over and over. Its used to provide single MDB_env instance for each collection
56+
* that uses lmdb to store and retrieve data. That approach satisfies lmdb requirement:
57+
*
58+
* "LMDB uses POSIX locks on files, and these locks have issues if one process opens
59+
* a file multiple times. Because of this, do not mdb_env_open() a file multiple
60+
* times from a single process."
61+
*
62+
* Creation of MDB_env is delayed to moment when first transaction is opened.
63+
* This approach prevents passing env object to forked processes.
64+
* In that way next lmdb requirement be satisfied:
65+
*
66+
* "Use an MDB_env* in the process which opened it, without fork()ing."
67+
*/
68+
class MDBEnvProvider {
69+
70+
public:
71+
MDBEnvProvider(MDBEnvProvider &other) = delete;
72+
void operator=(const MDBEnvProvider &) = delete;
73+
74+
/**
75+
* This is the static method that controls the access to the singleton
76+
* instance. On the first run, it creates a singleton object and places it
77+
* into the static field. On subsequent runs, it returns the client existing
78+
* object stored in the static field (Meyers Singleton implementation).
79+
*/
80+
static MDBEnvProvider& GetInstance() {
81+
static MDBEnvProvider instance;
82+
return instance;
83+
}
84+
MDB_env* GetEnv();
85+
MDB_dbi* GetDBI();
86+
~MDBEnvProvider();
87+
private:
88+
MDB_env *m_env;
89+
MDB_dbi m_dbi;
90+
91+
MDBEnvProvider();
92+
};
93+
5194
class LMDB :
5295
public Collection {
5396
public:
5497
explicit LMDB(std::string name);
55-
~LMDB();
5698
void store(std::string key, std::string value) override;
5799

58100
bool storeOrUpdateFirst(const std::string &key,
@@ -75,11 +117,13 @@ class LMDB :
75117
variables::KeyExclusions &ke) override;
76118

77119
private:
120+
int txn_begin(unsigned int flags, MDB_txn **ret);
78121
void string2val(const std::string& str, MDB_val *val);
79122
void inline lmdb_debug(int rc, std::string op, std::string scope);
80123

81124
MDB_env *m_env;
82125
MDB_dbi m_dbi;
126+
bool isOpen;
83127
};
84128

85129
} // namespace backend

0 commit comments

Comments
 (0)