Browse code

cfg framework: group instance support in cfg_get_by_name() + bugfix

- cfg_get_by_name() can return the value from both the default or from
a specific group instance.
- bugfix: cfg_add_group_inst() overwrote a larger memory area then the group size.
- More verbose log messages when the group instance is not found.

Miklos Tirpak authored on 09/09/2010 10:56:58
Showing 2 changed files
... ...
@@ -387,7 +387,8 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *va
387 387
 							group->size,
388 388
 							*group_id);
389 389
 			if (!group_inst) {
390
-				LOG(L_ERR, "ERROR: cfg_set_now(): local group instance is not found\n");
390
+				LOG(L_ERR, "ERROR: cfg_set_now(): local group instance %.*s[%u] is not found\n",
391
+					group_name->len, group_name->s, *group_id);
391 392
 				goto error0;
392 393
 			}
393 394
 			var_block = group_inst->vars;
... ...
@@ -432,7 +433,8 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *va
432 433
 							group->size,
433 434
 							*group_id);
434 435
 			if (!group_inst) {
435
-				LOG(L_ERR, "ERROR: cfg_set_now(): global group instance is not found\n");
436
+				LOG(L_ERR, "ERROR: cfg_set_now(): global group instance %.*s[%u] is not found\n",
437
+					group_name->len, group_name->s, *group_id);
436 438
 				goto error;
437 439
 			}
438 440
 			var_block = group_inst->vars;
... ...
@@ -1038,7 +1040,7 @@ int cfg_rollback(cfg_ctx_t *ctx)
1038 1040
  * -1 - error
1039 1041
  *  1 - variable exists, but it is not readable
1040 1042
  */
1041
-int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
1043
+int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
1042 1044
 			void **val, unsigned int *val_type)
1043 1045
 {
1044 1046
 	cfg_group_t	*group;
... ...
@@ -1046,6 +1048,7 @@ int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
1046 1048
 	void		*p;
1047 1049
 	static str	s;	/* we need the value even
1048 1050
 				after the function returns */
1051
+	cfg_group_inst_t	*group_inst;
1049 1052
 
1050 1053
 	/* verify the context even if we do not need it now
1051 1054
 	to make sure that a cfg driver has called the function
... ...
@@ -1066,10 +1069,27 @@ int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
1066 1069
 		return 1;
1067 1070
 	}
1068 1071
 
1069
-	/* use the module's handle to access the variable
1070
-	It means that the variable is read from the local config
1071
-	after forking */
1072
-	p = *(group->handle) + var->offset;
1072
+	if (group_id) {
1073
+		if (!cfg_local) {
1074
+			LOG(L_ERR, "ERROR: cfg_get_by_name(): Local configuration is missing\n");
1075
+			return -1;
1076
+		}
1077
+		group_inst = cfg_find_group(CFG_GROUP_META(cfg_local, group),
1078
+						group->size,
1079
+						*group_id);
1080
+		if (!group_inst) {
1081
+			LOG(L_ERR, "ERROR: cfg_get_by_name(): local group instance %.*s[%u] is not found\n",
1082
+				group_name->len, group_name->s, *group_id);
1083
+			return -1;
1084
+		}
1085
+		p = group_inst->vars + var->offset;
1086
+
1087
+	} else {
1088
+		/* use the module's handle to access the variable
1089
+		It means that the variable is read from the local config
1090
+		after forking */
1091
+		p = *(group->handle) + var->offset;
1092
+	}
1073 1093
 
1074 1094
 	switch (CFG_VAR_TYPE(var)) {
1075 1095
 	case CFG_VAR_INT:
... ...
@@ -1278,7 +1298,7 @@ int cfg_add_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id)
1278 1298
 	/* fill in the new group instance with the default data */
1279 1299
 	memcpy(	new_inst->vars,
1280 1300
 		CFG_GROUP_DATA(*cfg_global, group),
1281
-		sizeof(cfg_group_inst_t) + group->size - 1);
1301
+		group->size);
1282 1302
 
1283 1303
 	CFG_GROUP_META(block, group)->array = new_array;
1284 1304
 	CFG_GROUP_META(block, group)->num++;
... ...
@@ -110,7 +110,7 @@ int cfg_commit(cfg_ctx_t *ctx);
110 110
 int cfg_rollback(cfg_ctx_t *ctx);
111 111
 
112 112
 /*! \brief returns the value of a variable */
113
-int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
113
+int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
114 114
 			void **val, unsigned int *val_type);
115 115
 
116 116
 /*! \brief returns the description of a variable */
... ...
@@ -177,4 +177,7 @@ int cfg_diff_next(void **h,
177 177
 /*! \brief destroy the handle of cfg_diff_next() */
178 178
 void cfg_diff_release(cfg_ctx_t *ctx);
179 179
 
180
+/* Add a new instance to an existing group */
181
+int cfg_add_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
182
+
180 183
 #endif /* _CFG_CTX_H */