From 31c201a44b01a751e5d58c7810d009a3568d5564 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Mon, 18 Jan 2021 17:29:50 +1000 Subject: [PATCH] * refactor filter modules library interface --- src/filter-test.c | 7 +------ src/filter.c | 16 ++++++---------- src/filter.h | 14 +++++--------- src/log.h | 1 + 4 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/filter-test.c b/src/filter-test.c index 8921ea9..a2e7518 100644 --- a/src/filter-test.c +++ b/src/filter-test.c @@ -23,7 +23,6 @@ int main(int argc, char *argv[]) { char line[LOGLINE_MAX] = ""; char stats[4096]; size_t read = 0, matched = 0; - const char *error; FILE *file = NULL; if (argc < 3) @@ -62,12 +61,8 @@ int main(int argc, char *argv[]) { matched++; fprintf(stdout, "+ %s\n", match); continue; - } - error = f2b_filter_error(filter); - if (*error == '\0') { - fprintf(stdout, "- (no-match): %s", line); } else { - fprintf(stdout, "! (error) : %s\n", error); + fprintf(stdout, "- (no-match): %s", line); } } fclose(file); diff --git a/src/filter.c b/src/filter.c index c2204b1..b190209 100644 --- a/src/filter.c +++ b/src/filter.c @@ -97,7 +97,7 @@ f2b_filter_create(f2b_config_section_t *config, const char *file) { goto cleanup; if ((*(void **) (&filter->append) = dlsym(filter->h, "append")) == NULL) goto cleanup; - if ((*(void **) (&filter->error) = dlsym(filter->h, "error")) == NULL) + if ((*(void **) (&filter->logcb) = dlsym(filter->h, "logcb")) == NULL) goto cleanup; if ((*(void **) (&filter->ready) = dlsym(filter->h, "ready")) == NULL) goto cleanup; @@ -116,6 +116,8 @@ f2b_filter_create(f2b_config_section_t *config, const char *file) { goto cleanup; } + filter->logcb(filter->cfg, f2b_log_mod_cb); + /* try init */ for (param = config->param; param != NULL; param = param->next) { if (strcmp(param->name, FILTER_LIBRARY_PARAM) == 0) @@ -175,12 +177,6 @@ f2b_filter_match(f2b_filter_t *filter, const char *line, char *buf, size_t buf_s return filter->match(filter->cfg, line, buf, buf_size); } -const char * -f2b_filter_error(f2b_filter_t *filter) { - assert(filter != NULL); - return filter->error(filter->cfg); -} - void f2b_filter_cmd_stats(char *buf, size_t bufsize, f2b_filter_t *filter) { bool reset = true; @@ -208,7 +204,7 @@ f2b_filter_cmd_reload(char *buf, size_t bufsize, f2b_filter_t *filter) { assert(filter != NULL); filter->flush(filter->cfg); - if (f2b_filter_load_file(filter, filter->file)) - return; - strlcpy(buf, f2b_filter_error(filter), bufsize); + if (f2b_filter_load_file(filter, filter->file)) { + snprintf(buf, bufsize, "can't reload filter"); + } } diff --git a/src/filter.h b/src/filter.h index 81d932f..9182b58 100644 --- a/src/filter.h +++ b/src/filter.h @@ -27,8 +27,8 @@ typedef struct f2b_filter_t { bool (*config) (void *cfg, const char *key, const char *value); /** dlsym pointer to handler of @a append command */ bool (*append) (void *cfg, const char *pattern); - /** dlsym pointer to handler of @a error command */ - char *(*error) (void *cfg); + /** dlsym pointer to handler of @a logcb command */ + void (*logcb) (void *cfg, void (*cb)(log_msgtype_t lvl, const char *msg)); /** dlsym pointer to handler of @a ready command */ bool (*ready) (void *cfg); /** dlsym pointer to handler of @a flush command */ @@ -54,18 +54,13 @@ f2b_filter_t * f2b_filter_create (f2b_config_section_t *config, const char *id) */ void f2b_filter_destroy (f2b_filter_t *f); -/** - * @brief Get last filter error - * @param f Pointer to filter struct - * @returns Pointer to string with description of last error - */ -const char * f2b_filter_error (f2b_filter_t *f); /** * @brief Append pattern to filter * @param f Pointer to filter struct * @param pattern Match pattern - * @returns true on success, false on error with setting last error + * @returns true on success, false on error */ + bool f2b_filter_append(f2b_filter_t *f, const char *pattern); /** * @brief Match a line against given filter @@ -80,6 +75,7 @@ bool f2b_filter_match (f2b_filter_t *f, const char *line, char *buf, size_t bufs /* handlers for csocket commands processing */ /** handler of 'jail $JAIL filter reload' cmd */ void f2b_filter_cmd_reload(char *buf, size_t bufsize, f2b_filter_t *f); + /** handler of 'jail $JAIL filter stats' cmd */ void f2b_filter_cmd_stats (char *buf, size_t bufsize, f2b_filter_t *f); diff --git a/src/log.h b/src/log.h index c45517d..e97ae9a 100644 --- a/src/log.h +++ b/src/log.h @@ -41,6 +41,7 @@ void f2b_log_msg(log_msgtype_t l, const char *fmt, ...) * @param msg Log message string */ 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