浏览代码

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.)

Miklos Tirpak 17 年之前
父节点
当前提交
30feb9b6d3
共有 3 个文件被更改,包括 10 次插入8 次删除
  1. 8 5
      cfg/cfg_ctx.c
  2. 1 1
      cfg/cfg_ctx.h
  3. 1 2
      doc/cfg.txt

+ 8 - 5
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 */

+ 1 - 1
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);

+ 1 - 2
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;
 	}