Browse Source

* refactor sources modules library interface

master
Alex 'AdUser' Z 4 years ago
parent
commit
8f036f2983
  1. 8
      src/jail.c
  2. 4
      src/log.c
  3. 7
      src/log.h
  4. 4
      src/source-test.c
  5. 12
      src/source.c
  6. 20
      src/source.h

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

4
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) {

7
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

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

12
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)

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

Loading…
Cancel
Save