/* Move the group handle to the next group instance. * This function together with cfg_select_first() can be used * to iterate though the list of instances. * * Return value: * -1: no more group instance found. Note, that the active group * instance is not changed in this case. * 0: the next group instance is successfully selected. */ int cfg_select_next(cfg_group_t *group) { cfg_group_meta_t *meta; cfg_group_inst_t *old_ginst, *new_ginst; int size; if (!cfg_local) { LOG(L_ERR, "ERROR: The child process has no local configuration\n"); return -1; } if (!(meta = CFG_GROUP_META(cfg_local, group))) return -1; if (!(old_ginst = CFG_HANDLE_TO_GINST(*(group->handle)) /* the active group instance */)) { LOG(L_ERR, "ERROR: cfg_select_next(): No group instance is set currently. Forgot to call cfg_select_first()?\n"); return -1; } size = sizeof(cfg_group_inst_t) + group->size - 1; if (((char *)old_ginst - (char *)meta->array)/size + 1 >= meta->num) return -1; /* this is the last group instance */ new_ginst = (cfg_group_inst_t *)((char *)old_ginst + size); cfg_move_handle(group, old_ginst, /* the active group instance */ new_ginst); LOG(L_DBG, "DEBUG: cfg_select_next(): group instance '%.*s[%u]' has been selected\n", group->name_len, group->name, new_ginst->id); return 0; }
/* Move the group handle to the specified group instance. */ int cfg_select(cfg_group_t *group, unsigned int id) { cfg_group_inst_t *ginst; if (!cfg_local) { LOG(L_ERR, "ERROR: The child process has no local configuration\n"); return -1; } if (!(ginst = cfg_find_group(CFG_GROUP_META(cfg_local, group), group->size, id)) ) { LOG(L_ERR, "ERROR: cfg_select(): group instance '%.*s[%u]' does not exist\n", group->name_len, group->name, id); return -1; } cfg_move_handle(group, CFG_HANDLE_TO_GINST(*(group->handle)), /* the active group instance */ ginst); LOG(L_DBG, "DEBUG: cfg_select(): group instance '%.*s[%u]' has been selected\n", group->name_len, group->name, id); return 0; }
/* Reset the group handle to the default, local configuration */ int cfg_reset(cfg_group_t *group) { if (!cfg_local) { LOG(L_ERR, "ERROR: The child process has no local configuration\n"); return -1; } cfg_move_handle(group, CFG_HANDLE_TO_GINST(*(group->handle)), /* the active group instance */ NULL); LOG(L_DBG, "DEBUG: cfg_reset(): default group '%.*s' has been selected\n", group->name_len, group->name); return 0; }
/* Move the group handle to the first group instance. * This function together with cfg_select_next() can be used * to iterate though the list of instances. * * Return value: * -1: no group instance found * 0: first group instance is successfully selected. */ int cfg_select_first(cfg_group_t *group) { cfg_group_meta_t *meta; cfg_group_inst_t *ginst; if (!cfg_local) { LOG(L_ERR, "ERROR: The child process has no local configuration\n"); return -1; } meta = CFG_GROUP_META(cfg_local, group); if (!meta || (meta->num == 0)) return -1; ginst = (cfg_group_inst_t *)meta->array; cfg_move_handle(group, CFG_HANDLE_TO_GINST(*(group->handle)), /* the active group instance */ ginst); LOG(L_DBG, "DEBUG: cfg_select_first(): group instance '%.*s[%u]' has been selected\n", group->name_len, group->name, ginst->id); return 0; }
/* return the selected group instance */ int cfg_selected_inst(str *res, select_t *s, struct sip_msg *msg) { cfg_group_t *group; cfg_group_inst_t *inst; if (msg == NULL) { /* fixup call */ /* one parameter is mandatory: group name */ if (s->n != 2) { LM_ERR("One parameter is expected\n"); return -1; } if (s->params[1].type != SEL_PARAM_STR) { LM_ERR("string parameter is expected\n"); return -1; } /* look-up the group and the variable */ if (!(group = cfg_lookup_group(s->params[1].v.s.s, s->params[1].v.s.len))) { if (cfg_shmized) { LM_ERR("unknown configuration group: %.*s\n", s->params[1].v.s.len, s->params[1].v.s.s); return -1; } /* The group was not found, add it to the non-fixed select list. * So we act as if the fixup was successful, and we retry it later */ if (cfg_new_select(&s->params[1].v.s, NULL, &s->params[1].v.p, NULL)) return -1; LM_DBG("select fixup is postponed: %.*s\n", s->params[1].v.s.len, s->params[1].v.s.s); s->params[1].type = SEL_PARAM_PTR; s->params[1].v.p = NULL; return 0; } s->params[1].type = SEL_PARAM_PTR; s->params[1].v.p = (void *)group; return 1; } group = (cfg_group_t *)s->params[1].v.p; if (!group) return -1; /* Get the current group instance from the group handle. */ inst = CFG_HANDLE_TO_GINST(*(group->handle)); if (inst) { res->s = int2str(inst->id, &res->len); } else { res->s = ""; res->len = 0; } return 0; }