Browse code

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 authored on 28/01/2008 12:54:58
Showing 3 changed files
... ...
@@ -44,7 +44,7 @@ static cfg_ctx_t	*cfg_ctx_list = NULL;
44 44
 /* creates a new config context that is an interface to the
45 45
  * cfg variables with write permission
46 46
  */
47
-cfg_ctx_t *cfg_register_ctx(cfg_on_declare on_declare_cb)
47
+int cfg_register_ctx(cfg_ctx_t **handle, cfg_on_declare on_declare_cb)
48 48
 {
49 49
 	cfg_ctx_t	*ctx;
50 50
 	cfg_group_t	*group;
... ...
@@ -56,20 +56,23 @@ cfg_ctx_t *cfg_register_ctx(cfg_on_declare on_declare_cb)
56 56
 	ctx = (cfg_ctx_t *)shm_malloc(sizeof(cfg_ctx_t));
57 57
 	if (!ctx) {
58 58
 		LOG(L_ERR, "ERROR: cfg_register_ctx(): not enough shm memory\n");
59
-		return NULL;
59
+		return -1;
60 60
 	}
61 61
 	memset(ctx, 0, sizeof(cfg_ctx_t));
62 62
 	if (lock_init(&ctx->lock) == 0) {
63 63
 		LOG(L_ERR, "ERROR: cfg_register_ctx(): failed to init lock\n");
64 64
 		shm_free(ctx);
65
-		return NULL;
65
+		return -1;
66 66
 	}
67 67
 
68 68
 	/* add the new ctx to the beginning of the list */
69 69
 	ctx->next = cfg_ctx_list;
70 70
 	cfg_ctx_list = ctx;
71 71
 
72
-	/* let the driver know about the already registered groups */
72
+	/* let the driver know about the already registered groups
73
+	 * The handle of the context must be set before calling the
74
+	 * on_declare callbacks. */
75
+	*handle = ctx;
73 76
 	if (on_declare_cb) {
74 77
 		ctx->on_declare_cb = on_declare_cb;
75 78
 
... ...
@@ -87,7 +90,7 @@ cfg_ctx_t *cfg_register_ctx(cfg_on_declare on_declare_cb)
87 90
 		}
88 91
 	}
89 92
 
90
-	return ctx;
93
+	return 0;
91 94
 }
92 95
 
93 96
 /* free the memory allocated for the contexts */
... ...
@@ -72,7 +72,7 @@ typedef struct _cfg_ctx {
72 72
 
73 73
 /* creates a new config context that is an interface to the
74 74
  * cfg variables with write permission */
75
-cfg_ctx_t *cfg_register_ctx(cfg_on_declare on_declare_cb);
75
+int cfg_register_ctx(cfg_ctx_t **handle, cfg_on_declare on_declare_cb);
76 76
 
77 77
 /* free the memory allocated for the contexts */
78 78
 void cfg_ctx_destroy(void);
... ...
@@ -184,8 +184,7 @@ static void on_declare(str *group_name, cfg_def_t *definition)
184 184
 
185 185
 static int mod_init(void)
186 186
 {
187
-	ctx = cfg_register_ctx(on_declare);
188
-	if (!ctx) {
187
+	if (cfg_register_ctx(&ctx, on_declare)) {
189 188
 		/* error */
190 189
 		return -1;
191 190
 	}