From: Florian Forster Date: Wed, 8 Jul 2020 21:07:26 +0000 (+0200) Subject: src/daemon/plugin.c: Refactor plugin_register_flush(). X-Git-Tag: 6.0.0-rc0~144^2~103 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a13330257b1d896425756afbe94abc635b61bb0;p=thirdparty%2Fcollectd.git src/daemon/plugin.c: Refactor plugin_register_flush(). --- diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index 62bef8b33..783a15ab4 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -1291,53 +1291,51 @@ static char *plugin_flush_callback_name(const char *name) { EXPORT int plugin_register_flush(const char *name, plugin_flush_cb callback, user_data_t const *ud) { - int status; plugin_ctx_t ctx = plugin_get_ctx(); - status = create_register_callback(&list_flush, name, (void *)callback, ud); - if (status != 0) + int status = create_register_callback(&list_flush, name, (void *)callback, ud); + if (status != 0) { return status; + } - if (ctx.flush_interval != 0) { - char *flush_name; - flush_callback_t *cb; - - flush_name = plugin_flush_callback_name(name); - if (flush_name == NULL) - return -1; + if (ctx.flush_interval == 0) { + return 0; + } - cb = malloc(sizeof(*cb)); - if (cb == NULL) { - ERROR("plugin_register_flush: malloc failed."); - sfree(flush_name); - return -1; - } + char *flush_name = plugin_flush_callback_name(name); + if (flush_name == NULL) { + return ENOMEM; + } - cb->name = strdup(name); - if (cb->name == NULL) { - ERROR("plugin_register_flush: strdup failed."); - sfree(cb); - sfree(flush_name); - return -1; - } - cb->timeout = ctx.flush_timeout; - - status = plugin_register_complex_read( - /* group = */ "flush", - /* name = */ flush_name, - /* callback = */ plugin_flush_timeout_callback, - /* interval = */ ctx.flush_interval, - /* user data = */ - &(user_data_t){ - .data = cb, - .free_func = plugin_flush_timeout_callback_free, - }); + flush_callback_t *cb = calloc(1, sizeof(*cb)); + if (cb == NULL) { + ERROR("plugin_register_flush: malloc failed."); + sfree(flush_name); + return ENOMEM; + } + cb->name = strdup(name); + if (cb->name == NULL) { + ERROR("plugin_register_flush: strdup failed."); + sfree(cb); sfree(flush_name); - return status; + return ENOMEM; } + cb->timeout = ctx.flush_timeout; - return 0; + status = plugin_register_complex_read( + /* group = */ "flush", + /* name = */ flush_name, + /* callback = */ plugin_flush_timeout_callback, + /* interval = */ ctx.flush_interval, + /* user data = */ + &(user_data_t){ + .data = cb, + .free_func = plugin_flush_timeout_callback_free, + }); + + sfree(flush_name); + return status; } /* int plugin_register_flush */ EXPORT int plugin_register_missing(const char *name, plugin_missing_cb callback,