Module: sip-router
Branch: tirpi/cfg_framework_multivalue
Commit: 5d4d6ae9ed9848d92e10e746a8d870833b5c683d
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=5d4d6ae…
Author: Miklos Tirpak <miklos(a)iptel.org>
Committer: Miklos Tirpak <miklos(a)iptel.org>
Date: Thu Sep 9 12:56:58 2010 +0200
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.
---
cfg/cfg_ctx.c | 36 ++++++++++++++++++++++++++++--------
cfg/cfg_ctx.h | 5 ++++-
2 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/cfg/cfg_ctx.c b/cfg/cfg_ctx.c
index 480fab5..b0fa851 100644
--- a/cfg/cfg_ctx.c
+++ b/cfg/cfg_ctx.c
@@ -387,7 +387,8 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int
*group_id, str *va
group->size,
*group_id);
if (!group_inst) {
- LOG(L_ERR, "ERROR: cfg_set_now(): local group instance is not found\n");
+ LOG(L_ERR, "ERROR: cfg_set_now(): local group instance %.*s[%u] is not
found\n",
+ group_name->len, group_name->s, *group_id);
goto error0;
}
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
group->size,
*group_id);
if (!group_inst) {
- LOG(L_ERR, "ERROR: cfg_set_now(): global group instance is not found\n");
+ LOG(L_ERR, "ERROR: cfg_set_now(): global group instance %.*s[%u] is not
found\n",
+ group_name->len, group_name->s, *group_id);
goto error;
}
var_block = group_inst->vars;
@@ -1038,7 +1040,7 @@ int cfg_rollback(cfg_ctx_t *ctx)
* -1 - error
* 1 - variable exists, but it is not readable
*/
-int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
+int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str
*var_name,
void **val, unsigned int *val_type)
{
cfg_group_t *group;
@@ -1046,6 +1048,7 @@ int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
void *p;
static str s; /* we need the value even
after the function returns */
+ cfg_group_inst_t *group_inst;
/* verify the context even if we do not need it now
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,
return 1;
}
- /* use the module's handle to access the variable
- It means that the variable is read from the local config
- after forking */
- p = *(group->handle) + var->offset;
+ if (group_id) {
+ if (!cfg_local) {
+ LOG(L_ERR, "ERROR: cfg_get_by_name(): Local configuration is missing\n");
+ return -1;
+ }
+ group_inst = cfg_find_group(CFG_GROUP_META(cfg_local, group),
+ group->size,
+ *group_id);
+ if (!group_inst) {
+ LOG(L_ERR, "ERROR: cfg_get_by_name(): local group instance %.*s[%u] is not
found\n",
+ group_name->len, group_name->s, *group_id);
+ return -1;
+ }
+ p = group_inst->vars + var->offset;
+
+ } else {
+ /* use the module's handle to access the variable
+ It means that the variable is read from the local config
+ after forking */
+ p = *(group->handle) + var->offset;
+ }
switch (CFG_VAR_TYPE(var)) {
case CFG_VAR_INT:
@@ -1278,7 +1298,7 @@ int cfg_add_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int
group_id)
/* fill in the new group instance with the default data */
memcpy( new_inst->vars,
CFG_GROUP_DATA(*cfg_global, group),
- sizeof(cfg_group_inst_t) + group->size - 1);
+ group->size);
CFG_GROUP_META(block, group)->array = new_array;
CFG_GROUP_META(block, group)->num++;
diff --git a/cfg/cfg_ctx.h b/cfg/cfg_ctx.h
index 0e2299f..255911e 100644
--- a/cfg/cfg_ctx.h
+++ b/cfg/cfg_ctx.h
@@ -110,7 +110,7 @@ int cfg_commit(cfg_ctx_t *ctx);
int cfg_rollback(cfg_ctx_t *ctx);
/*! \brief returns the value of a variable */
-int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
+int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str
*var_name,
void **val, unsigned int *val_type);
/*! \brief returns the description of a variable */
@@ -177,4 +177,7 @@ int cfg_diff_next(void **h,
/*! \brief destroy the handle of cfg_diff_next() */
void cfg_diff_release(cfg_ctx_t *ctx);
+/* Add a new instance to an existing group */
+int cfg_add_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
+
#endif /* _CFG_CTX_H */