]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
src/daemon/plugin.c: Refactor plugin_register_flush().
authorFlorian Forster <octo@collectd.org>
Wed, 8 Jul 2020 21:07:26 +0000 (23:07 +0200)
committerFlorian Forster <octo@google.com>
Tue, 28 Jul 2020 13:18:02 +0000 (15:18 +0200)
src/daemon/plugin.c

index 62bef8b33181368d1169c79a7317fca6dd639215..783a15ab46ea1b5d5b5743cd9a9255a609e018dc 100644 (file)
@@ -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,