X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fmeta_data.c;h=a11fccb2e2fb125b02ffc9d8d048ad58f3388972;hb=2f2f63a8c96ecd07e481b2d28f03ce0e82c55f5a;hp=b3f892a22061804ef0cfa79de2323dca3b55c7b0;hpb=354f9991530248e45207d236eb74c1cc3d5238ef;p=collectd.git diff --git a/src/daemon/meta_data.c b/src/daemon/meta_data.c index b3f892a2..a11fccb2 100644 --- a/src/daemon/meta_data.c +++ b/src/daemon/meta_data.c @@ -25,10 +25,12 @@ **/ #include "collectd.h" + +#include "common.h" #include "plugin.h" #include "meta_data.h" -#include +#define MD_MAX_NONSTRING_CHARS 128 /* * Data types @@ -285,6 +287,17 @@ static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */ } /* }}} meta_entry_t *md_entry_lookup */ /* + * Each value_list_t*, as it is going through the system, is handled by exactly + * one thread. Plugins which pass a value_list_t* to another thread, e.g. the + * rrdtool plugin, must create a copy first. The meta data within a + * value_list_t* is not thread safe and doesn't need to be. + * + * The meta data associated with cache entries are a different story. There, we + * need to ensure exclusive locking to prevent leaks and other funky business. + * This is ensured by the uc_meta_data_get_*() functions. + */ + +/* * Public functions */ meta_data_t *meta_data_create (void) /* {{{ */ @@ -323,8 +336,6 @@ meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */ int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */ { - meta_entry_t *e; - if (orig == NULL) return (0); @@ -334,7 +345,7 @@ int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */ } pthread_mutex_lock (&orig->lock); - for (e=orig->head; e != NULL; e = e->next) + for (meta_entry_t *e=orig->head; e != NULL; e = e->next) { md_entry_insert_clone((*dest), e); } @@ -355,14 +366,12 @@ void meta_data_destroy (meta_data_t *md) /* {{{ */ int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */ { - meta_entry_t *e; - if ((md == NULL) || (key == NULL)) return (-EINVAL); pthread_mutex_lock (&md->lock); - for (e = md->head; e != NULL; e = e->next) + for (meta_entry_t *e = md->head; e != NULL; e = e->next) { if (strcasecmp (key, e->key) == 0) { @@ -377,14 +386,12 @@ int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */ int meta_data_type (meta_data_t *md, const char *key) /* {{{ */ { - meta_entry_t *e; - if ((md == NULL) || (key == NULL)) return -EINVAL; pthread_mutex_lock (&md->lock); - for (e = md->head; e != NULL; e = e->next) + for (meta_entry_t *e = md->head; e != NULL; e = e->next) { if (strcasecmp (key, e->key) == 0) { @@ -400,14 +407,13 @@ int meta_data_type (meta_data_t *md, const char *key) /* {{{ */ int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */ { int i = 0, count = 0; - meta_entry_t *e; if ((md == NULL) || (toc == NULL)) return -EINVAL; pthread_mutex_lock (&md->lock); - for (e = md->head; e != NULL; e = e->next) + for (meta_entry_t *e = md->head; e != NULL; e = e->next) ++count; if (count == 0) @@ -417,7 +423,7 @@ int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */ } *toc = calloc(count, sizeof(**toc)); - for (e = md->head; e != NULL; e = e->next) + for (meta_entry_t *e = md->head; e != NULL; e = e->next) (*toc)[i++] = strdup(e->key); pthread_mutex_unlock (&md->lock); @@ -726,4 +732,68 @@ int meta_data_get_boolean (meta_data_t *md, /* {{{ */ return (0); } /* }}} int meta_data_get_boolean */ +int meta_data_as_string (meta_data_t *md, /* {{{ */ + const char *key, char **value) +{ + meta_entry_t *e; + char *actual; + char buffer[MD_MAX_NONSTRING_CHARS]; /* For non-string types. */ + char *temp; + int type; + + if ((md == NULL) || (key == NULL) || (value == NULL)) + return (-EINVAL); + + pthread_mutex_lock (&md->lock); + + e = md_entry_lookup (md, key); + if (e == NULL) + { + pthread_mutex_unlock (&md->lock); + return (-ENOENT); + } + + type = e->type; + + switch (type) + { + case MD_TYPE_STRING: + actual = e->value.mv_string; + break; + case MD_TYPE_SIGNED_INT: + ssnprintf (buffer, sizeof (buffer), "%"PRIi64, e->value.mv_signed_int); + actual = buffer; + break; + case MD_TYPE_UNSIGNED_INT: + ssnprintf (buffer, sizeof (buffer), "%"PRIu64, e->value.mv_unsigned_int); + actual = buffer; + break; + case MD_TYPE_DOUBLE: + ssnprintf (buffer, sizeof (buffer), GAUGE_FORMAT, e->value.mv_double); + actual = buffer; + break; + case MD_TYPE_BOOLEAN: + actual = e->value.mv_boolean ? "true" : "false"; + break; + default: + pthread_mutex_unlock (&md->lock); + ERROR ("meta_data_as_string: unknown type %d for key `%s'", type, key); + return (-ENOENT); + } + + pthread_mutex_unlock (&md->lock); + + temp = md_strdup (actual); + if (temp == NULL) + { + pthread_mutex_unlock (&md->lock); + ERROR ("meta_data_as_string: md_strdup failed for key `%s'.", key); + return (-ENOMEM); + } + + *value = temp; + + return (0); +} /* }}} int meta_data_as_string */ + /* vim: set sw=2 sts=2 et fdm=marker : */