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 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; }