From 30feb9b6d324f136bd3d29190f42581e829dddee Mon Sep 17 00:00:00 2001 From: Miklos Tirpak Date: Mon, 28 Jan 2008 12:54:58 +0000 Subject: [PATCH] changing the syntax of cfg_register_ctx() function (The context handle must be ready when the on_declare() callbacks are called, and it may happen that the callback is executed before cfg_register_ctx() returns.) --- cfg/cfg_ctx.c | 13 ++++++++----- cfg/cfg_ctx.h | 2 +- doc/cfg.txt | 3 +-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cfg/cfg_ctx.c b/cfg/cfg_ctx.c index b13687faa6..c62273ebc4 100644 --- a/cfg/cfg_ctx.c +++ b/cfg/cfg_ctx.c @@ -44,7 +44,7 @@ static cfg_ctx_t *cfg_ctx_list = NULL; /* creates a new config context that is an interface to the * cfg variables with write permission */ -cfg_ctx_t *cfg_register_ctx(cfg_on_declare on_declare_cb) +int cfg_register_ctx(cfg_ctx_t **handle, cfg_on_declare on_declare_cb) { cfg_ctx_t *ctx; cfg_group_t *group; @@ -56,20 +56,23 @@ cfg_ctx_t *cfg_register_ctx(cfg_on_declare on_declare_cb) ctx = (cfg_ctx_t *)shm_malloc(sizeof(cfg_ctx_t)); if (!ctx) { LOG(L_ERR, "ERROR: cfg_register_ctx(): not enough shm memory\n"); - return NULL; + return -1; } memset(ctx, 0, sizeof(cfg_ctx_t)); if (lock_init(&ctx->lock) == 0) { LOG(L_ERR, "ERROR: cfg_register_ctx(): failed to init lock\n"); shm_free(ctx); - return NULL; + return -1; } /* add the new ctx to the beginning of the list */ ctx->next = cfg_ctx_list; cfg_ctx_list = ctx; - /* let the driver know about the already registered groups */ + /* let the driver know about the already registered groups + * The handle of the context must be set before calling the + * on_declare callbacks. */ + *handle = ctx; if (on_declare_cb) { ctx->on_declare_cb = on_declare_cb; @@ -87,7 +90,7 @@ cfg_ctx_t *cfg_register_ctx(cfg_on_declare on_declare_cb) } } - return ctx; + return 0; } /* free the memory allocated for the contexts */ diff --git a/cfg/cfg_ctx.h b/cfg/cfg_ctx.h index b53a89e82f..fc092d1525 100644 --- a/cfg/cfg_ctx.h +++ b/cfg/cfg_ctx.h @@ -72,7 +72,7 @@ typedef struct _cfg_ctx { /* creates a new config context that is an interface to the * cfg variables with write permission */ -cfg_ctx_t *cfg_register_ctx(cfg_on_declare on_declare_cb); +int cfg_register_ctx(cfg_ctx_t **handle, cfg_on_declare on_declare_cb); /* free the memory allocated for the contexts */ void cfg_ctx_destroy(void); diff --git a/doc/cfg.txt b/doc/cfg.txt index de41661360..e3f86eddc1 100644 --- a/doc/cfg.txt +++ b/doc/cfg.txt @@ -184,8 +184,7 @@ static void on_declare(str *group_name, cfg_def_t *definition) static int mod_init(void) { - ctx = cfg_register_ctx(on_declare); - if (!ctx) { + if (cfg_register_ctx(&ctx, on_declare)) { /* error */ return -1; } -- 2.20.1