From 29f0ce63e5811694da717da5b8a7602f883935aa Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Fri, 11 Mar 2016 17:44:36 +1000 Subject: [PATCH] * chg signature for f2b_config_load() --- src/backend-test.c | 11 ++++++----- src/config.c | 16 ++++++---------- src/config.h | 2 +- src/main.c | 17 +++++++++-------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/backend-test.c b/src/backend-test.c index fa4114c..f44c29a 100644 --- a/src/backend-test.c +++ b/src/backend-test.c @@ -10,7 +10,7 @@ void usage() { int main(int argc, char *argv[]) { const char *ip = "127.0.0.17"; - f2b_config_t *config = NULL; + f2b_config_t config; f2b_config_param_t *param = NULL; f2b_config_section_t *section = NULL; f2b_backend_t *backend = NULL; @@ -18,12 +18,13 @@ int main(int argc, char *argv[]) { if (argc < 3) usage(); - if ((config = f2b_config_load(argv[1])) == NULL) { + memset(&config, 0x0, sizeof(config)); + if (f2b_config_load(&config, argv[1]) != true) { f2b_log_msg(log_error, "can't load config"); return EXIT_FAILURE; } - if ((section = f2b_config_section_find(config->jails, "test")) == NULL) { + if ((section = f2b_config_section_find(config.jails, "test")) == NULL) { f2b_log_msg(log_error, "can't find config section for jail 'test'"); return EXIT_FAILURE; } @@ -33,7 +34,7 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - if ((section = f2b_config_section_find(config->backends, param->value)) == NULL) { + if ((section = f2b_config_section_find(config.backends, param->value)) == NULL) { f2b_log_msg(log_error, "can't find config section for backend '%s'", param->value); return EXIT_FAILURE; } @@ -62,7 +63,7 @@ int main(int argc, char *argv[]) { cleanup: f2b_backend_destroy(backend); - f2b_config_free(config); + f2b_config_free(&config); return EXIT_SUCCESS; } diff --git a/src/config.c b/src/config.c index 2c1362f..a714fe1 100644 --- a/src/config.c +++ b/src/config.c @@ -166,9 +166,8 @@ f2b_config_section_append(f2b_config_section_t *section, f2b_config_param_t *par return section; } -f2b_config_t * -f2b_config_load(const char *path) { - f2b_config_t *config = NULL; +bool +f2b_config_load(f2b_config_t *config, const char *path) { f2b_config_section_t *section = NULL; /* always points to current section */ f2b_config_param_t *param = NULL; /* temp pointer */ FILE *f = NULL; /* config file fd */ @@ -177,14 +176,13 @@ f2b_config_load(const char *path) { bool skip_section = true; /* if set - skip parameters unless next section */ size_t linenum = 0; /* current line number in config */ + assert(config != NULL); + if ((f = fopen(path, "r")) == NULL) { f2b_log_msg(log_error, "can't open config file '%s': %s", path, strerror(errno)); - return NULL; + return false; } - if ((config = calloc(1, sizeof(f2b_config_t))) == NULL) - return NULL; - while (1) { p = fgets(line, sizeof(line), f); if (!p && (feof(f) || ferror(f))) @@ -235,7 +233,7 @@ f2b_config_load(const char *path) { } /* while */ fclose(f); - return config; + return true; } #define FREE_SECTIONS(SECTION) \ @@ -258,8 +256,6 @@ f2b_config_free(f2b_config_t *config) { FREE_SECTIONS(config->filters); FREE_SECTIONS(config->backends); FREE_SECTIONS(config->jails); - - FREE(config); } f2b_config_section_t * diff --git a/src/config.h b/src/config.h index a44409f..06f7c1f 100644 --- a/src/config.h +++ b/src/config.h @@ -44,7 +44,7 @@ f2b_config_section_t * f2b_config_section_create(const char *line); f2b_config_section_t * f2b_config_section_find (f2b_config_section_t *s, const char *name); f2b_config_section_t * f2b_config_section_append(f2b_config_section_t *s, f2b_config_param_t *p, bool replace); -f2b_config_t * f2b_config_load (const char *path); +bool f2b_config_load (f2b_config_t *c, const char *path); void f2b_config_free (f2b_config_t *c); f2b_config_section_t * f2b_config_append(f2b_config_t *c, f2b_config_section_t *s); #endif /* CONFIG_H_ */ diff --git a/src/main.c b/src/main.c index ee6cdd4..ff6c8d4 100644 --- a/src/main.c +++ b/src/main.c @@ -37,7 +37,7 @@ void usage(int exitcode) { int main(int argc, char *argv[]) { struct sigaction act; - f2b_config_t *config = NULL; + f2b_config_t config; f2b_config_section_t *section = NULL; f2b_jail_t *jails = NULL; f2b_jail_t *jail = NULL; @@ -63,15 +63,16 @@ int main(int argc, char *argv[]) { if (!config_file) usage(EXIT_FAILURE); - if ((config = f2b_config_load(config_file)) == NULL) { + memset(&config, 0x0, sizeof(config)); + if (f2b_config_load(&config, config_file) != true) { f2b_log_msg(log_error, "can't load config from '%s'", config_file); return EXIT_FAILURE; } - if (config->defaults) - f2b_jail_set_defaults(config->defaults); + if (config.defaults) + f2b_jail_set_defaults(config.defaults); - for (section = config->jails; section != NULL; section = section->next) { + for (section = config.jails; section != NULL; section = section->next) { if ((jail = f2b_jail_create(section)) == NULL) { f2b_log_msg(log_error, "can't create jail '%s'", section->name); continue; @@ -81,7 +82,7 @@ int main(int argc, char *argv[]) { free(jail); continue; } - if (!f2b_jail_init(jail, config)) { + if (!f2b_jail_init(jail, &config)) { f2b_log_msg(log_error, "can't init jail '%s'", section->name); free(jail); continue; @@ -89,10 +90,10 @@ int main(int argc, char *argv[]) { jail->next = jails; jails = jail; } - f2b_config_free(config); + f2b_config_free(&config); if (!jails) { - f2b_log_msg(log_error, "no jails configured"); + f2b_log_msg(log_error, "no jails configured, exiting"); return EXIT_FAILURE; }