Browse Source

* chg signature for f2b_config_load()

master
Alex 'AdUser' Z 9 years ago
parent
commit
29f0ce63e5
  1. 11
      src/backend-test.c
  2. 16
      src/config.c
  3. 2
      src/config.h
  4. 17
      src/main.c

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

16
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 *

2
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_ */

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

Loading…
Cancel
Save