From e729f5bf507b540fe9c99200569b0c7aa7b22c44 Mon Sep 17 00:00:00 2001 From: Colin Vidal Date: Mon, 16 Dec 2024 20:16:00 +0100 Subject: [PATCH] fixup! Introduction of cfgmgr --- lib/isccfg/cfgmgr.c | 32 ++++++++++++++++++------------ lib/isccfg/include/isccfg/cfgmgr.h | 2 +- tests/isccfg/cfgmgr_test.c | 26 ++++++++++-------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/isccfg/cfgmgr.c b/lib/isccfg/cfgmgr.c index e84574ad6d..5e262fb74b 100644 --- a/lib/isccfg/cfgmgr.c +++ b/lib/isccfg/cfgmgr.c @@ -18,12 +18,13 @@ #include #include +#include #include #include #include -#define DBPATH "/tmp/lmdb-exp" +#define RANDOM isc_random_uniform(UINT32_MAX); /* * See MDB_MAXKEYSIZE documentation, but not accessible as defined in @@ -50,6 +51,7 @@ typedef struct { } context_t; static isc_mem_t *mctx = NULL; +static const char *dbpath = NULL; static MDB_env *env = NULL; static thread_local context_t ctx = (context_t){ .openedclauses = ISC_LIST_INITIALIZER, @@ -75,14 +77,13 @@ parseid(const char *dbkey) { * start of the id (character 1) is at character index 4 */ idstarts = strlen(ctx.buffer); + INSIST(idstarts > 0 && ctx.buffer[idstarts - 1] == '.'); idends = idstarts; - INSIST(idstarts > 0 && idends == idstarts); - INSIST(ctx.buffer[idstarts - 1] == '.'); - keylen = strlen(dbkey); /* * Cutting the key form the dot after the identifier */ + keylen = strlen(dbkey); REQUIRE(keylen > idends); while (dbkey[idends] != '.') { idends++; @@ -100,7 +101,7 @@ parseid(const char *dbkey) { } isc_result_t -cfgmgr_init(void) { +cfgmgr_init(isc_mem_t *mctx_, const char *dbpath_) { int result = ISC_R_SUCCESS; char dbname[BUFLEN]; char dblockname[BUFLEN]; @@ -111,12 +112,16 @@ cfgmgr_init(void) { REQUIRE(ctx.buffer == NULL); REQUIRE(ctx.cursor == NULL); REQUIRE(ctx.txn == NULL); + REQUIRE(dbpath == NULL); REQUIRE(mctx == NULL); REQUIRE(env == NULL); + REQUIRE(mctx_ != NULL); + REQUIRE(dbpath_ != NULL); - isc_mem_create(&mctx); + isc_mem_attach(mctx_, &mctx); INSIST(mctx != NULL); + dbpath = dbpath_; result = mdb_env_create(&env); if (result != 0) { result = ISC_R_FAILURE; @@ -130,9 +135,9 @@ cfgmgr_init(void) { * corruption doesn't matter: as soon as the process is dead, * the disk data is dead as well) */ - random = arc4random(); - REQUIRE(snprintf(dbname, BUFLEN, "%s-%u", DBPATH, random) < BUFLEN); - REQUIRE(snprintf(dblockname, BUFLEN, "%s-%u-lock", DBPATH, random) < + random = RANDOM(); + REQUIRE(snprintf(dbname, BUFLEN, "%s-%u", dbpath, random) < BUFLEN); + REQUIRE(snprintf(dblockname, BUFLEN, "%s-%u-lock", dbpath, random) < BUFLEN); result = mdb_env_open(env, dbname, MDB_NOSYNC | MDB_NOSUBDIR, 0600); if (result != 0) { @@ -173,12 +178,13 @@ cfgmgr_deinit(void) { REQUIRE(ctx.buffer == NULL); REQUIRE(ctx.cursor == NULL); REQUIRE(ctx.txn == NULL); - REQUIRE(env != NULL); REQUIRE(mctx != NULL); + REQUIRE(env != NULL); mdb_env_close(env); env = NULL; - isc_mem_destroy(&mctx); - ENSURE(mctx == NULL); + dbpath = NULL; + isc_mem_detach(&mctx); + INSIST(mctx == NULL); } static void @@ -511,7 +517,7 @@ cfgmgr_newclause(const char *name) { INSIST(ctx.buffer != NULL); INSIST(ctx.cursor != NULL); INSIST(ctx.readonly == false); - pushclause(name, arc4random()); + pushclause(name, RANDOM()); INSIST(ctx.prefix != NULL); } diff --git a/lib/isccfg/include/isccfg/cfgmgr.h b/lib/isccfg/include/isccfg/cfgmgr.h index 7a0bc9ec0c..f843713931 100644 --- a/lib/isccfg/include/isccfg/cfgmgr.h +++ b/lib/isccfg/include/isccfg/cfgmgr.h @@ -151,7 +151,7 @@ isc_result_t cfgmgr_open(const char *name); isc_result_t -cfgmgr_init(void); +cfgmgr_init(isc_mem_t *mctx_, const char *dbpath_); void cfgmgr_deinit(void); diff --git a/tests/isccfg/cfgmgr_test.c b/tests/isccfg/cfgmgr_test.c index e513a5dd2f..c7c34a2657 100644 --- a/tests/isccfg/cfgmgr_test.c +++ b/tests/isccfg/cfgmgr_test.c @@ -24,6 +24,7 @@ #include +#define INIT SUCCESS(cfgmgr_init(mctx, "/tmp/named-cfgmgr-lmdb")) #define SUCCESS(result) assert_int_equal(result, ISC_R_SUCCESS) #define NOTFOUND(result) assert_int_equal(result, ISC_R_NOTFOUND) #define NOTBOUND(result) assert_int_equal(result, ISC_R_NOTBOUND) @@ -32,8 +33,7 @@ ISC_RUN_TEST_IMPL(cfgmgr_rw) { cfgmgr_val_t val1; cfgmgr_val_t val2; - SUCCESS(cfgmgr_init()); - + INIT; NOTFOUND(cfgmgr_open("foo")); SUCCESS(cfgmgr_newclause("foo")); @@ -177,8 +177,7 @@ ISC_RUN_TEST_IMPL(cfgmgr_parseid) { * exercise the nested clause and repeatable clauses with such * odd names */ - SUCCESS(cfgmgr_init()); - + INIT; SUCCESS(cfgmgr_newclause("123")); val = (cfgmgr_val_t){ .type = UINT32, .data.uint32 = 666666 }; SUCCESS(cfgmgr_setval("123123", &val)); @@ -240,8 +239,7 @@ ISC_RUN_TEST_IMPL(cfgmgr_override) { cfgmgr_val_t val1; cfgmgr_val_t val2; - SUCCESS(cfgmgr_init()); - + INIT; SUCCESS(cfgmgr_newclause("foo")); val1 = (cfgmgr_val_t){ .type = UINT32, .data.uint32 = 4058304 }; @@ -277,8 +275,7 @@ ISC_RUN_TEST_IMPL(cfgmgr_rw_string) { cfgmgr_val_t val1; cfgmgr_val_t val2; - SUCCESS(cfgmgr_init()); - + INIT; SUCCESS(cfgmgr_newclause("foo")); val1 = (cfgmgr_val_t){ .type = STRING, .data.string = "hey there!" }; @@ -327,7 +324,7 @@ ISC_RUN_TEST_IMPL(cfgmgr_rw_string) { ISC_RUN_TEST_IMPL(cfgmgr_list) { cfgmgr_val_t val; - SUCCESS(cfgmgr_init()); + INIT; SUCCESS(cfgmgr_newclause("foo")); val = (cfgmgr_val_t){ .type = STRING, .data.string = "lst1" }; @@ -408,8 +405,7 @@ ISC_RUN_TEST_IMPL(cfgmgr_repeatable_clauses) { cfgmgr_val_t val1; cfgmgr_val_t val2; - SUCCESS(cfgmgr_init()); - + INIT; SUCCESS(cfgmgr_newclause("view")); SUCCESS(cfgmgr_setval("p1", &(cfgmgr_val_t){ .type = STRING, .data.string = "view1 p1 " @@ -463,12 +459,11 @@ ISC_RUN_TEST_IMPL(cfgmgr_repeatable_clauses) { ISC_RUN_TEST_IMPL(cfgmgr_nested_clauses) { cfgmgr_val_t val; - SUCCESS(cfgmgr_init()); - /* * Let's start by writting then reading * foo { bar { baz { gee: none; }; }; }; */ + INIT; SUCCESS(cfgmgr_newclause("foo")); SUCCESS(cfgmgr_newclause("bar")); SUCCESS(cfgmgr_newclause("baz")); @@ -567,11 +562,10 @@ ISC_RUN_TEST_IMPL(cfgmgr_nested_clauses) { ISC_RUN_TEST_IMPL(cfgmgr_delete) { cfgmgr_val_t val; - SUCCESS(cfgmgr_init()); - /* * foo is not found because nothing has been written in there */ + INIT; SUCCESS(cfgmgr_newclause("foo")); SUCCESS(cfgmgr_close()); NOTFOUND(cfgmgr_open("foo")); @@ -690,9 +684,9 @@ ISC_RUN_TEST_IMPL(cfgmgr_threads) { pthread_t thread; sem_t sems[2]; + INIT; REQUIRE(sem_init(&sems[0], 0, 0) == 0); REQUIRE(sem_init(&sems[1], 0, 0) == 0); - SUCCESS(cfgmgr_init()); SUCCESS(cfgmgr_newclause("foo")); SUCCESS(cfgmgr_setval("p", &(cfgmgr_val_t){ .type = NONE })); cfgmgr_close();