int read_ipa_config(struct ipa_config **ipacfg) { struct ini_cfgobj *cfgctx = NULL; struct value_obj *obj = NULL; int ret; *ipacfg = calloc(1, sizeof(struct ipa_config)); if (!*ipacfg) { return ENOMEM; } ret = ini_config_create(&cfgctx); if (ret) { return ENOENT; } ret = config_from_file(cfgctx); if (ret) { ini_config_destroy(cfgctx); return EINVAL; } ret = ini_get_config_valueobj("global", "server", cfgctx, INI_GET_LAST_VALUE, &obj); if (ret != 0 || obj == NULL) { /* if called on an IPA server we need to look for 'host' instead */ ret = ini_get_config_valueobj("global", "host", cfgctx, INI_GET_LAST_VALUE, &obj); } if (ret == 0 && obj != NULL) { (*ipacfg)->server_name = ini_get_string_config_value(obj, &ret); } return 0; }
int gp_config_get_int(struct gp_ini_context *ctx, const char *secname, const char *keyname, int *value) { struct ini_cfgobj *ini_config = (struct ini_cfgobj *)ctx->private_data; struct value_obj *vo = NULL; int ret; int val; if (!value) { return EINVAL; } *value = -1; ret = ini_get_config_valueobj(secname, keyname, ini_config, INI_GET_FIRST_VALUE, &vo); if (ret) { return ret; } if (!vo) { return ENOENT; } val = ini_get_int_config_value(vo, 0, /* strict */ 0, /* default */ &ret); if (ret) { return ret; } *value = val; return 0; }
static errno_t parse_ini_file_with_libini(struct ini_cfgobj *ini_config, int *_gpt_version) { int ret; struct value_obj *vobj = NULL; int gpt_version; ret = ini_get_config_valueobj(INI_GENERAL_SECTION, GPT_INI_VERSION, ini_config, INI_GET_FIRST_VALUE, &vobj); if (ret != 0) { DEBUG(SSSDBG_CRIT_FAILURE, "ini_get_config_valueobj failed [%d][%s]\n", ret, strerror(ret)); goto done; } if (vobj == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "section/name not found: [%s][%s]\n", INI_GENERAL_SECTION, GPT_INI_VERSION); ret = EINVAL; goto done; } gpt_version = ini_get_int32_config_value(vobj, 0, -1, &ret); if (ret != 0) { DEBUG(SSSDBG_CRIT_FAILURE, "ini_get_int32_config_value failed [%d][%s]\n", ret, strerror(ret)); goto done; } *_gpt_version = gpt_version; ret = EOK; done: return ret; }
int gp_config_get_string(struct gp_ini_context *ctx, const char *secname, const char *keyname, const char **value) { struct ini_cfgobj *ini_config = (struct ini_cfgobj *)ctx->private_data; struct value_obj *vo = NULL; int ret; const char *val; if (!value) { return -1; } *value = NULL; ret = ini_get_config_valueobj(secname, keyname, ini_config, INI_GET_FIRST_VALUE, &vo); if (ret) { return ret; } if (!vo) { return ENOENT; } val = ini_get_const_string_config_value(vo, &ret); if (ret) { return ret; } *value = val; return 0; }
int main (int argc, char **argv) { struct ini_cfgobj *cfg_ctx = NULL; struct ini_cfgfile *file_ctx = NULL; char **section_list = NULL; int section_count = 0; char **attribute_list = NULL; int attribute_count = 0; struct value_obj *vo = NULL; int result = 0; char *value = NULL; char *path = "/usr/lib/systemd/system/docker.service"; printf("%s\n", path); // This throws an error as incomplete because the struct def is within the .so //file_ctx = malloc(sizeof(struct ini_cfgfile)); if ((result = ini_config_create(&cfg_ctx)) != 0) { printf("Error occured %d\n", result); goto cleanup; } //I suspect that file_ctx may be a member of struct cfg_ctx if ((result = ini_config_file_open(path, 0, &file_ctx)) != 0) { printf("Error occured %d\n", result); goto cleanup; } // This sets INI_MV2S_OVERWRITE by default, so when we parse the /etc version, it overwrites the matching section->attr // Should test this .... if ((result = ini_config_parse(file_ctx, 0, INI_MV1S_ALLOW, 0, cfg_ctx)) != 0) { printf("Error occured during config parsing %d\n", result); } result = 0; section_list = ini_get_section_list(cfg_ctx, §ion_count, &result); if (result != 0) { printf("Error while parsing section list %d\n", result); } for (int i = 0; i < section_count; ++i) { printf("Section\n%s\n ------\n", section_list[i]); attribute_list = NULL; result = 0; attribute_list = ini_get_attribute_list(cfg_ctx, section_list[i], &attribute_count, &result); if (result != 0) { printf("Error parsing attribute list\n"); goto next; } for (int j = 0; j < attribute_count; ++j) { // So we end up with MANY elements of the same attr name in attribute list ... How do we handle this? printf("attr: %s -> ", attribute_list[j]); if ((result = ini_get_config_valueobj(section_list[i], attribute_list[j], cfg_ctx, INI_GET_NEXT_VALUE, &vo)) != 0) { printf("\n Error retriving attribute value"); goto next; } if (vo == NULL) { goto nextattr; } result = 0; value = ini_get_string_config_value(vo, &result); if (result != 0) { printf("\n Error parsing attribute value"); goto next; } printf("%s ; ", value); nextattr: printf("\n"); } next: if (value != NULL) { free(value); value = NULL; } if (vo != NULL) { //How do we free this? //ini_ vo = NULL; } if (attribute_list != NULL) { ini_free_attribute_list(attribute_list); attribute_list = NULL; } } cleanup: if (section_list != NULL) { ini_free_section_list(section_list); } if (file_ctx != NULL) { // Is there a memory leak here .... wooooeeeeeoooooooo ini_config_file_close(file_ctx); } if (cfg_ctx != NULL) { ini_config_destroy(cfg_ctx); } return result; }
int gp_config_get_string_array(struct gp_ini_context *ctx, const char *secname, const char *keyname, int *num_values, const char ***values) { struct ini_cfgobj *ini_config = (struct ini_cfgobj *)ctx->private_data; struct value_obj *vo = NULL; const char *value; int ret; int i, count = 0; const char **array = NULL; const char **t_array; if (!values || !num_values) { return EINVAL; } *num_values = 0; *values = NULL; ret = ini_get_config_valueobj(secname, keyname, ini_config, INI_GET_FIRST_VALUE, &vo); if (ret) { return ret; } if (!vo) { return ENOENT; } value = ini_get_const_string_config_value(vo, &ret); if (ret) { return ret; } array = calloc(1, sizeof(char *)); if (array == NULL) { ret = ENOMEM; goto done; } array[count] = strdup(value); if (array[count] == NULL) { ret = ENOMEM; goto done; } count++; do { ret = ini_get_config_valueobj(secname, keyname, ini_config, INI_GET_NEXT_VALUE, &vo); if (ret) { goto done; } if (!vo) { break; } value = ini_get_const_string_config_value(vo, &ret); if (ret) { goto done; } t_array = realloc(array, (count+1) * sizeof(char *)); if (t_array == NULL) { ret = ENOMEM; goto done; } array = t_array; array[count] = strdup(value); if (array[count] == NULL) { ret = ENOMEM; goto done; } count++; } while (1); *num_values = count; *values = array; ret = 0; done: if (ret && array) { for (i = 0; i < count; i++) { safefree(array[i]); } safefree(array); } return ret; }