From 8f036f2983850220a0482c98efd45c42d0dd429a Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Mon, 18 Jan 2021 15:55:55 +1000 Subject: [PATCH] * refactor sources modules library interface --- src/jail.c | 8 +++----- src/log.c | 4 ++-- src/log.h | 7 ++++--- src/source-test.c | 4 ++-- src/source.c | 12 ++++-------- src/source.h | 20 ++++++-------------- 6 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/jail.c b/src/jail.c index 16236ae..c83e69d 100644 --- a/src/jail.c +++ b/src/jail.c @@ -363,13 +363,12 @@ f2b_jail_init(f2b_jail_t *jail, f2b_config_t *config) { } /* init all */ - if ((jail->source = f2b_source_create(s_section, jail->source_init, f2b_log_error_cb)) == NULL) { + if ((jail->source = f2b_source_create(s_section, jail->source_init)) == NULL) { f2b_log_msg(log_error, "jail '%s': can't init source '%s' with %s", jail->name, jail->source_name, jail->source_init); goto cleanup; } if (!f2b_source_start(jail->source)) { - f2b_log_msg(log_warn, "jail '%s': source action 'start' failed -- %s", - jail->name, f2b_source_error(jail->source)); + f2b_log_msg(log_warn, "jail '%s': source action 'start' failed", jail->name); goto cleanup; } @@ -453,8 +452,7 @@ f2b_jail_stop(f2b_jail_t *jail) { f2b_log_msg(log_info, "jail '%s': gracefull shutdown", jail->name); if (!f2b_source_stop(jail->source)) { - f2b_log_msg(log_error, "jail '%s': action 'stop' for source failed: %s", - jail->name, f2b_source_error(jail->source)); + f2b_log_msg(log_error, "jail '%s': action 'stop' for source failed", jail->name); errors = true; } diff --git a/src/log.c b/src/log.c index f72e908..9ae24c5 100644 --- a/src/log.c +++ b/src/log.c @@ -66,8 +66,8 @@ void f2b_log_msg(log_msgtype_t l, const char *fmt, ...) { return; } -void f2b_log_error_cb(const char *errstr) { - f2b_log_msg(log_error, "%s", errstr); +void f2b_log_mod_cb(log_msgtype_t l, const char *msg) { + f2b_log_msg(l, "%s", msg); } void f2b_log_set_level(const char *level) { diff --git a/src/log.h b/src/log.h index 42076ab..c45517d 100644 --- a/src/log.h +++ b/src/log.h @@ -36,10 +36,11 @@ typedef enum { void f2b_log_msg(log_msgtype_t l, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); /** - * @brief Logging wrapper for use in source module - * @param errstr Error string + * @brief Logging callback function for use in modules + * @param l Level of message + * @param msg Log message string */ -void f2b_log_error_cb(const char *errstr); +void f2b_log_mod_cb(log_msgtype_t l, const char *msg); /** * @brief Limit logging messages by importance * @param level Min level of messages for logging diff --git a/src/source-test.c b/src/source-test.c index e430b96..b6eee72 100644 --- a/src/source-test.c +++ b/src/source-test.c @@ -37,13 +37,13 @@ int main(int argc, char *argv[]) { section = config.sources; } - if ((source = f2b_source_create(section, argv[2], f2b_log_error_cb)) == NULL) { + if ((source = f2b_source_create(section, argv[2])) == NULL) { f2b_log_msg(log_fatal, "can't create source '%s' with init '%s'", section->name, argv[2]); return EXIT_FAILURE; } if (f2b_source_start(source) == false) { - f2b_log_msg(log_fatal, "source start error: %s", f2b_source_error(source)); + f2b_log_msg(log_fatal, "source start error"); exit(EXIT_FAILURE); } diff --git a/src/source.c b/src/source.c index 7c11f03..a113911 100644 --- a/src/source.c +++ b/src/source.c @@ -10,7 +10,7 @@ #define SOURCE_LIBRARY_PARAM "load" f2b_source_t * -f2b_source_create(f2b_config_section_t *config, const char *init, void (*errcb)(const char *)) { +f2b_source_create(f2b_config_section_t *config, const char *init) { f2b_config_param_t *param = NULL; f2b_source_t *source = NULL; int flags = RTLD_NOW | RTLD_LOCAL; @@ -36,9 +36,7 @@ f2b_source_create(f2b_config_section_t *config, const char *init, void (*errcb)( goto cleanup; if ((*(void **) (&source->ready) = dlsym(source->h, "ready")) == NULL) goto cleanup; - if ((*(void **) (&source->error) = dlsym(source->h, "error")) == NULL) - goto cleanup; - if ((*(void **) (&source->errcb) = dlsym(source->h, "errcb")) == NULL) + if ((*(void **) (&source->logcb) = dlsym(source->h, "logcb")) == NULL) goto cleanup; if ((*(void **) (&source->start) = dlsym(source->h, "start")) == NULL) goto cleanup; @@ -54,6 +52,8 @@ f2b_source_create(f2b_config_section_t *config, const char *init, void (*errcb)( goto cleanup; } + source->logcb(source->cfg, f2b_log_mod_cb); + /* try init */ for (param = config->param; param != NULL; param = param->next) { if (strcmp(param->name, SOURCE_LIBRARY_PARAM) == 0) @@ -64,9 +64,6 @@ f2b_source_create(f2b_config_section_t *config, const char *init, void (*errcb)( config->name, param->name, param->value); } - if (errcb) - source->errcb(source->cfg, errcb); - if (source->ready(source->cfg)) return source; @@ -107,7 +104,6 @@ f2b_source_ ## CMD(f2b_source_t *source) { \ return source->CMD(source->cfg); \ } -SOURCE_CMD_ARG0(error, const char *) SOURCE_CMD_ARG0(start, bool) SOURCE_CMD_ARG0(stop, bool) SOURCE_CMD_ARG0(ready, bool) diff --git a/src/source.h b/src/source.h index 2426038..c5e1528 100644 --- a/src/source.h +++ b/src/source.h @@ -26,10 +26,8 @@ typedef struct f2b_source_t { bool (*config) (void *cfg, const char *key, const char *value); /** dlsym pointer to handler of @a ready command */ bool (*ready) (void *cfg); - /** dlsym pointer to handler of @a error command */ - char *(*error) (void *cfg); - /** dlsym pointer to handler of @a errcb command */ - void (*errcb) (void *cfg, void (*cb)(const char *errstr)); + /** dlsym pointer to handler of @a logcb command */ + void (*logcb) (void *cfg, void (*cb)(log_msgtype_t l, const char *msg)); /** dlsym pointer to handler of @a start command */ bool (*start) (void *cfg); /** dlsym pointer to handler of @a next command */ @@ -44,10 +42,10 @@ typedef struct f2b_source_t { * @brief Create module from config * @param config Pointer to config section with module description * @param init Module init string - * @param errcb Error callback + * @param logcb Logging callback * @returns Pointer to allocated module struct or NULL on error */ -f2b_source_t * f2b_source_create (f2b_config_section_t *config, const char *init, void (*errcb)(const char *)); +f2b_source_t * f2b_source_create (f2b_config_section_t *config, const char *init); /** * @brief Free module metadata * @param s Pointer to module struct @@ -57,7 +55,7 @@ void f2b_source_destroy (f2b_source_t *s); /** * @brief Start given source * @param s Pointer to source struct - * @returns true on success, false on error with setting last error + * @returns true on success, false on error */ bool f2b_source_start (f2b_source_t *s); /** @@ -72,14 +70,8 @@ bool f2b_source_next (f2b_source_t *s, char *buf, size_t bufsize, bool reset); /** * @brief Stop given source * @param s Pointer to source struct - * @returns true on success, false on error with setting last error + * @returns true on success, false on error */ bool f2b_source_stop (f2b_source_t *s); -/** - * @brief Get last source error - * @param s Pointer to source struct - * @returns Pointer to string with description of last error - */ -const char * f2b_source_error (f2b_source_t *s); #endif /* F2B_SOURCE_H_ */