]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
daemon: Move `data_set_t` and related functions to its own file.
authorFlorian Forster <octo@collectd.org>
Mon, 18 Dec 2023 08:26:28 +0000 (09:26 +0100)
committerFlorian Forster <octo@collectd.org>
Mon, 18 Dec 2023 22:29:47 +0000 (23:29 +0100)
This is part of the legacy support for collectd 5 metrics. Because "data
sets" are a global resource, this has to remain within the daemon.

Makefile.am
src/daemon/data_set.c [new file with mode: 0644]
src/daemon/data_set.h [new file with mode: 0644]
src/daemon/plugin.c
src/daemon/plugin.h

index d3fd990962f9f92ede3e7f4dc3b1b9377271294a..2fe922908a641821311d2770a0e08977f680a362 100644 (file)
@@ -238,6 +238,8 @@ collectd_SOURCES = \
        src/daemon/collectd.h \
        src/daemon/configfile.c \
        src/daemon/configfile.h \
+       src/daemon/data_set.c \
+       src/daemon/data_set.h \
        src/daemon/filter_chain.c \
        src/daemon/filter_chain.h \
        src/daemon/globals.c \
diff --git a/src/daemon/data_set.c b/src/daemon/data_set.c
new file mode 100644 (file)
index 0000000..926ef95
--- /dev/null
@@ -0,0 +1,121 @@
+/**
+ * collectd - src/daemon/data_set.c
+ * Copyright (C) 2005-2023  Florian octo Forster
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Florian octo Forster <octo at collectd.org>
+ *   Sebastian Harl <sh at tokkee.org>
+ *   Manoj Srivastava <srivasta at google.com>
+ **/
+
+#include "daemon/data_set.h"
+#include "daemon/plugin.h"
+#include "utils/avltree/avltree.h"
+#include "utils/common/common.h"
+
+#ifdef WIN32
+#define EXPORT __declspec(dllexport)
+#include <sys/stat.h>
+#include <unistd.h>
+#else
+#define EXPORT
+#endif
+
+static c_avl_tree_t *data_sets;
+
+EXPORT int plugin_register_data_set(const data_set_t *ds) {
+  data_set_t *ds_copy;
+
+  if ((data_sets != NULL) && (c_avl_get(data_sets, ds->type, NULL) == 0)) {
+    NOTICE("Replacing DS `%s' with another version.", ds->type);
+    plugin_unregister_data_set(ds->type);
+  } else if (data_sets == NULL) {
+    data_sets = c_avl_create((int (*)(const void *, const void *))strcmp);
+    if (data_sets == NULL)
+      return -1;
+  }
+
+  ds_copy = malloc(sizeof(*ds_copy));
+  if (ds_copy == NULL)
+    return -1;
+  memcpy(ds_copy, ds, sizeof(data_set_t));
+
+  ds_copy->ds = malloc(sizeof(*ds_copy->ds) * ds->ds_num);
+  if (ds_copy->ds == NULL) {
+    sfree(ds_copy);
+    return -1;
+  }
+
+  for (size_t i = 0; i < ds->ds_num; i++)
+    memcpy(ds_copy->ds + i, ds->ds + i, sizeof(data_source_t));
+
+  return c_avl_insert(data_sets, (void *)ds_copy->type, (void *)ds_copy);
+} /* int plugin_register_data_set */
+
+EXPORT int plugin_unregister_data_set(const char *name) {
+  data_set_t *ds;
+
+  if (data_sets == NULL)
+    return -1;
+
+  if (c_avl_remove(data_sets, name, NULL, (void *)&ds) != 0)
+    return -1;
+
+  sfree(ds->ds);
+  sfree(ds);
+
+  return 0;
+} /* int plugin_unregister_data_set */
+
+EXPORT const data_set_t *plugin_get_ds(const char *name) {
+  data_set_t *ds;
+
+  if (data_sets == NULL) {
+    P_ERROR("plugin_get_ds: No data sets are defined yet.");
+    return NULL;
+  }
+
+  if (c_avl_get(data_sets, name, (void *)&ds) != 0) {
+    DEBUG("No such dataset registered: %s", name);
+    return NULL;
+  }
+
+  return ds;
+} /* data_set_t *plugin_get_ds */
+
+void plugin_free_data_sets(void) {
+  void *key;
+  void *value;
+
+  if (data_sets == NULL)
+    return;
+
+  while (c_avl_pick(data_sets, &key, &value) == 0) {
+    data_set_t *ds = value;
+    /* key is a pointer to ds->type */
+
+    sfree(ds->ds);
+    sfree(ds);
+  }
+
+  c_avl_destroy(data_sets);
+  data_sets = NULL;
+} /* void plugin_free_data_sets */
diff --git a/src/daemon/data_set.h b/src/daemon/data_set.h
new file mode 100644 (file)
index 0000000..3734bf6
--- /dev/null
@@ -0,0 +1,54 @@
+/**
+ * collectd - src/daemon/data_set.h
+ * Copyright (C) 2005-2023  Florian octo Forster
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Florian octo Forster <octo at collectd.org>
+ *   Sebastian Harl <sh at tokkee.org>
+ *   Manoj Srivastava <srivasta at google.com>
+ **/
+
+#ifndef DAEMON_DATA_SET_H
+#define DAEMON_DATA_SET_H
+
+#include "collectd.h"
+
+struct data_source_s {
+  char name[DATA_MAX_NAME_LEN];
+  int type;
+  double min;
+  double max;
+};
+typedef struct data_source_s data_source_t;
+
+struct data_set_s {
+  char type[DATA_MAX_NAME_LEN];
+  size_t ds_num;
+  data_source_t *ds;
+};
+typedef struct data_set_s data_set_t;
+
+int plugin_register_data_set(const data_set_t *ds);
+int plugin_unregister_data_set(const char *name);
+const data_set_t *plugin_get_ds(const char *name);
+void plugin_free_data_sets(void);
+
+#endif /* DAEMON_DATA_SET_H */
index a8773b5b2d55da967d27ac48c9afd01d46b53d1c..a4d9f06d017ea54ea71e6747253e54188b47fa35 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * collectd - src/plugin.c
+ * collectd - src/daemon/plugin.c
  * Copyright (C) 2005-2014  Florian octo Forster
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -32,6 +32,7 @@
 #include "collectd.h"
 
 #include "configfile.h"
+#include "daemon/data_set.h"
 #include "filter_chain.h"
 #include "plugin.h"
 #include "resource.h"
@@ -150,8 +151,6 @@ static cache_event_func_t list_cache_event[32];
 static fc_chain_t *pre_cache_chain;
 static fc_chain_t *post_cache_chain;
 
-static c_avl_tree_t *data_sets;
-
 static char *plugindir;
 
 #ifndef DEFAULT_MAX_READ_INTERVAL
@@ -1409,54 +1408,6 @@ EXPORT int plugin_register_shutdown(const char *name, int (*callback)(void)) {
   return create_register_callback(&list_shutdown, name, (void *)callback, NULL);
 } /* int plugin_register_shutdown */
 
-static void plugin_free_data_sets(void) {
-  void *key;
-  void *value;
-
-  if (data_sets == NULL)
-    return;
-
-  while (c_avl_pick(data_sets, &key, &value) == 0) {
-    data_set_t *ds = value;
-    /* key is a pointer to ds->type */
-
-    sfree(ds->ds);
-    sfree(ds);
-  }
-
-  c_avl_destroy(data_sets);
-  data_sets = NULL;
-} /* void plugin_free_data_sets */
-
-EXPORT int plugin_register_data_set(const data_set_t *ds) {
-  data_set_t *ds_copy;
-
-  if ((data_sets != NULL) && (c_avl_get(data_sets, ds->type, NULL) == 0)) {
-    NOTICE("Replacing DS `%s' with another version.", ds->type);
-    plugin_unregister_data_set(ds->type);
-  } else if (data_sets == NULL) {
-    data_sets = c_avl_create((int (*)(const void *, const void *))strcmp);
-    if (data_sets == NULL)
-      return -1;
-  }
-
-  ds_copy = malloc(sizeof(*ds_copy));
-  if (ds_copy == NULL)
-    return -1;
-  memcpy(ds_copy, ds, sizeof(data_set_t));
-
-  ds_copy->ds = malloc(sizeof(*ds_copy->ds) * ds->ds_num);
-  if (ds_copy->ds == NULL) {
-    sfree(ds_copy);
-    return -1;
-  }
-
-  for (size_t i = 0; i < ds->ds_num; i++)
-    memcpy(ds_copy->ds + i, ds->ds + i, sizeof(data_source_t));
-
-  return c_avl_insert(data_sets, (void *)ds_copy->type, (void *)ds_copy);
-} /* int plugin_register_data_set */
-
 EXPORT int plugin_register_log(const char *name, plugin_log_cb callback,
                                user_data_t const *ud) {
   return create_register_callback(&list_log, name, (void *)callback, ud);
@@ -1733,21 +1684,6 @@ EXPORT int plugin_unregister_shutdown(const char *name) {
   return plugin_unregister(list_shutdown, name);
 }
 
-EXPORT int plugin_unregister_data_set(const char *name) {
-  data_set_t *ds;
-
-  if (data_sets == NULL)
-    return -1;
-
-  if (c_avl_remove(data_sets, name, NULL, (void *)&ds) != 0)
-    return -1;
-
-  sfree(ds->ds);
-  sfree(ds);
-
-  return 0;
-} /* int plugin_unregister_data_set */
-
 EXPORT int plugin_unregister_log(const char *name) {
   return plugin_unregister(list_log, name);
 }
@@ -2321,22 +2257,6 @@ EXPORT int parse_notif_severity(const char *severity) {
   return notif_severity;
 } /* int parse_notif_severity */
 
-EXPORT const data_set_t *plugin_get_ds(const char *name) {
-  data_set_t *ds;
-
-  if (data_sets == NULL) {
-    P_ERROR("plugin_get_ds: No data sets are defined yet.");
-    return NULL;
-  }
-
-  if (c_avl_get(data_sets, name, (void *)&ds) != 0) {
-    DEBUG("No such dataset registered: %s", name);
-    return NULL;
-  }
-
-  return ds;
-} /* data_set_t *plugin_get_ds */
-
 static int plugin_notification_meta_add(notification_t *n, const char *name,
                                         enum notification_meta_type_e type,
                                         const void *value) {
index 4f92cb9af7d0a366c647fde47b2efd2321e6ea75..e1c54627a6dfc8118507a8bff35fdf08a263bc74 100644 (file)
@@ -86,21 +86,6 @@ struct identifier_s {
 };
 typedef struct identifier_s identifier_t;
 
-struct data_source_s {
-  char name[DATA_MAX_NAME_LEN];
-  int type;
-  double min;
-  double max;
-};
-typedef struct data_source_s data_source_t;
-
-struct data_set_s {
-  char type[DATA_MAX_NAME_LEN];
-  size_t ds_num;
-  data_source_t *ds;
-};
-typedef struct data_set_s data_set_t;
-
 enum notification_meta_type_e {
   NM_TYPE_STRING,
   NM_TYPE_SIGNED_INT,
@@ -276,7 +261,6 @@ int plugin_register_cache_event(const char *name,
                                 plugin_cache_event_cb callback,
                                 user_data_t const *ud);
 int plugin_register_shutdown(const char *name, plugin_shutdown_cb callback);
-int plugin_register_data_set(const data_set_t *ds);
 int plugin_register_log(const char *name, plugin_log_cb callback,
                         user_data_t const *user_data);
 int plugin_register_notification(const char *name,
@@ -293,7 +277,6 @@ int plugin_unregister_flush(const char *name);
 int plugin_unregister_missing(const char *name);
 int plugin_unregister_cache_event(const char *name);
 int plugin_unregister_shutdown(const char *name);
-int plugin_unregister_data_set(const char *name);
 int plugin_unregister_log(const char *name);
 int plugin_unregister_notification(const char *name);
 
@@ -359,8 +342,6 @@ void daemon_log(int level, const char *format, ...)
 #define P_NOTICE(...) daemon_log(LOG_NOTICE, __VA_ARGS__)
 #define P_INFO(...) daemon_log(LOG_INFO, __VA_ARGS__)
 
-const data_set_t *plugin_get_ds(const char *name);
-
 int plugin_notification_meta_add_string(notification_t *n, const char *name,
                                         const char *value);
 int plugin_notification_meta_add_signed_int(notification_t *n, const char *name,