73c6046d82d036f60dcc263a68e0b23517ba95b2
1 /**
2 * collectd - src/meta_data.c
3 * Copyright (C) 2008-2011 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "plugin.h"
30 #include "meta_data.h"
32 /*
33 * Data types
34 */
35 union meta_value_u
36 {
37 char *mv_string;
38 int64_t mv_signed_int;
39 uint64_t mv_unsigned_int;
40 double mv_double;
41 _Bool mv_boolean;
42 };
43 typedef union meta_value_u meta_value_t;
45 struct meta_entry_s;
46 typedef struct meta_entry_s meta_entry_t;
47 struct meta_entry_s
48 {
49 char *key;
50 meta_value_t value;
51 int type;
52 meta_entry_t *next;
53 };
55 struct meta_data_s
56 {
57 meta_entry_t *head;
58 pthread_mutex_t lock;
59 };
61 /*
62 * Private functions
63 */
64 static char *md_strdup (const char *orig) /* {{{ */
65 {
66 size_t sz;
67 char *dest;
69 if (orig == NULL)
70 return (NULL);
72 sz = strlen (orig) + 1;
73 dest = malloc (sz);
74 if (dest == NULL)
75 return (NULL);
77 memcpy (dest, orig, sz);
79 return (dest);
80 } /* }}} char *md_strdup */
82 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
83 {
84 meta_entry_t *e;
86 e = calloc (1, sizeof (*e));
87 if (e == NULL)
88 {
89 ERROR ("md_entry_alloc: calloc failed.");
90 return (NULL);
91 }
93 e->key = md_strdup (key);
94 if (e->key == NULL)
95 {
96 free (e);
97 ERROR ("md_entry_alloc: md_strdup failed.");
98 return (NULL);
99 }
101 e->type = 0;
102 e->next = NULL;
104 return (e);
105 } /* }}} meta_entry_t *md_entry_alloc */
107 /* XXX: The lock on md must be held while calling this function! */
108 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
109 {
110 meta_entry_t *copy;
112 /* WARNINGS :
113 * - we do not check that orig != NULL here. You should have done it before.
114 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
115 */
117 copy = md_entry_alloc (orig->key);
118 if (copy == NULL)
119 return (NULL);
120 copy->type = orig->type;
121 if (copy->type == MD_TYPE_STRING)
122 copy->value.mv_string = strdup (orig->value.mv_string);
123 else
124 copy->value = orig->value;
126 return (copy);
127 } /* }}} meta_entry_t *md_entry_clone_contents */
129 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
130 {
131 meta_entry_t *copy;
133 if (orig == NULL)
134 return (NULL);
136 copy = md_entry_clone_contents(orig);
138 copy->next = md_entry_clone (orig->next);
139 return (copy);
140 } /* }}} meta_entry_t *md_entry_clone */
142 static void md_entry_free (meta_entry_t *e) /* {{{ */
143 {
144 if (e == NULL)
145 return;
147 free (e->key);
149 if (e->type == MD_TYPE_STRING)
150 free (e->value.mv_string);
152 if (e->next != NULL)
153 md_entry_free (e->next);
155 free (e);
156 } /* }}} void md_entry_free */
158 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
159 {
160 meta_entry_t *this;
161 meta_entry_t *prev;
163 if ((md == NULL) || (e == NULL))
164 return (-EINVAL);
166 pthread_mutex_lock (&md->lock);
168 prev = NULL;
169 this = md->head;
170 while (this != NULL)
171 {
172 if (strcasecmp (e->key, this->key) == 0)
173 break;
175 prev = this;
176 this = this->next;
177 }
179 if (this == NULL)
180 {
181 /* This key does not exist yet. */
182 if (md->head == NULL)
183 md->head = e;
184 else
185 {
186 assert (prev != NULL);
187 prev->next = e;
188 }
190 e->next = NULL;
191 }
192 else /* (this != NULL) */
193 {
194 if (prev == NULL)
195 md->head = e;
196 else
197 prev->next = e;
199 e->next = this->next;
200 }
202 pthread_mutex_unlock (&md->lock);
204 if (this != NULL)
205 {
206 this->next = NULL;
207 md_entry_free (this);
208 }
210 return (0);
211 } /* }}} int md_entry_insert */
213 /* XXX: The lock on md must be held while calling this function! */
214 static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */
215 {
216 meta_entry_t *e;
217 meta_entry_t *this;
218 meta_entry_t *prev;
220 /* WARNINGS :
221 * - we do not check that md and e != NULL here. You should have done it before.
222 * - we do not use the lock. You should have set it before.
223 */
225 e = md_entry_clone_contents(orig);
227 prev = NULL;
228 this = md->head;
229 while (this != NULL)
230 {
231 if (strcasecmp (e->key, this->key) == 0)
232 break;
234 prev = this;
235 this = this->next;
236 }
238 if (this == NULL)
239 {
240 /* This key does not exist yet. */
241 if (md->head == NULL)
242 md->head = e;
243 else
244 {
245 assert (prev != NULL);
246 prev->next = e;
247 }
249 e->next = NULL;
250 }
251 else /* (this != NULL) */
252 {
253 if (prev == NULL)
254 md->head = e;
255 else
256 prev->next = e;
258 e->next = this->next;
259 }
261 if (this != NULL)
262 {
263 this->next = NULL;
264 md_entry_free (this);
265 }
267 return (0);
268 } /* }}} int md_entry_insert_clone */
270 /* XXX: The lock on md must be held while calling this function! */
271 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
272 const char *key)
273 {
274 meta_entry_t *e;
276 if ((md == NULL) || (key == NULL))
277 return (NULL);
279 for (e = md->head; e != NULL; e = e->next)
280 if (strcasecmp (key, e->key) == 0)
281 break;
283 return (e);
284 } /* }}} meta_entry_t *md_entry_lookup */
286 /*
287 * Each value_list_t*, as it is going through the system, is handled by exactly
288 * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
289 * rrdtool plugin, must create a copy first. The meta data within a
290 * value_list_t* is not thread safe and doesn't need to be.
291 *
292 * The meta data associated with cache entries are a different story. There, we
293 * need to ensure exclusive locking to prevent leaks and other funky business.
294 * This is ensured by the uc_meta_data_get_*() functions.
295 */
297 /*
298 * Public functions
299 */
300 meta_data_t *meta_data_create (void) /* {{{ */
301 {
302 meta_data_t *md;
304 md = calloc (1, sizeof (*md));
305 if (md == NULL)
306 {
307 ERROR ("meta_data_create: calloc failed.");
308 return (NULL);
309 }
311 pthread_mutex_init (&md->lock, /* attr = */ NULL);
313 return (md);
314 } /* }}} meta_data_t *meta_data_create */
316 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
317 {
318 meta_data_t *copy;
320 if (orig == NULL)
321 return (NULL);
323 copy = meta_data_create ();
324 if (copy == NULL)
325 return (NULL);
327 pthread_mutex_lock (&orig->lock);
328 copy->head = md_entry_clone (orig->head);
329 pthread_mutex_unlock (&orig->lock);
331 return (copy);
332 } /* }}} meta_data_t *meta_data_clone */
334 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
335 {
336 meta_entry_t *e;
338 if (orig == NULL)
339 return (0);
341 if (*dest == NULL) {
342 *dest = meta_data_clone(orig);
343 return(0);
344 }
346 pthread_mutex_lock (&orig->lock);
347 for (e=orig->head; e != NULL; e = e->next)
348 {
349 md_entry_insert_clone((*dest), e);
350 }
351 pthread_mutex_unlock (&orig->lock);
353 return (0);
354 } /* }}} int meta_data_clone_merge */
356 void meta_data_destroy (meta_data_t *md) /* {{{ */
357 {
358 if (md == NULL)
359 return;
361 md_entry_free (md->head);
362 pthread_mutex_destroy (&md->lock);
363 free (md);
364 } /* }}} void meta_data_destroy */
366 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
367 {
368 meta_entry_t *e;
370 if ((md == NULL) || (key == NULL))
371 return (-EINVAL);
373 pthread_mutex_lock (&md->lock);
375 for (e = md->head; e != NULL; e = e->next)
376 {
377 if (strcasecmp (key, e->key) == 0)
378 {
379 pthread_mutex_unlock (&md->lock);
380 return (1);
381 }
382 }
384 pthread_mutex_unlock (&md->lock);
385 return (0);
386 } /* }}} int meta_data_exists */
388 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
389 {
390 meta_entry_t *e;
392 if ((md == NULL) || (key == NULL))
393 return -EINVAL;
395 pthread_mutex_lock (&md->lock);
397 for (e = md->head; e != NULL; e = e->next)
398 {
399 if (strcasecmp (key, e->key) == 0)
400 {
401 pthread_mutex_unlock (&md->lock);
402 return e->type;
403 }
404 }
406 pthread_mutex_unlock (&md->lock);
407 return 0;
408 } /* }}} int meta_data_type */
410 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
411 {
412 int i = 0, count = 0;
413 meta_entry_t *e;
415 if ((md == NULL) || (toc == NULL))
416 return -EINVAL;
418 pthread_mutex_lock (&md->lock);
420 for (e = md->head; e != NULL; e = e->next)
421 ++count;
423 if (count == 0)
424 {
425 pthread_mutex_unlock (&md->lock);
426 return (count);
427 }
429 *toc = calloc(count, sizeof(**toc));
430 for (e = md->head; e != NULL; e = e->next)
431 (*toc)[i++] = strdup(e->key);
433 pthread_mutex_unlock (&md->lock);
434 return count;
435 } /* }}} int meta_data_toc */
437 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
438 {
439 meta_entry_t *this;
440 meta_entry_t *prev;
442 if ((md == NULL) || (key == NULL))
443 return (-EINVAL);
445 pthread_mutex_lock (&md->lock);
447 prev = NULL;
448 this = md->head;
449 while (this != NULL)
450 {
451 if (strcasecmp (key, this->key) == 0)
452 break;
454 prev = this;
455 this = this->next;
456 }
458 if (this == NULL)
459 {
460 pthread_mutex_unlock (&md->lock);
461 return (-ENOENT);
462 }
464 if (prev == NULL)
465 md->head = this->next;
466 else
467 prev->next = this->next;
469 pthread_mutex_unlock (&md->lock);
471 this->next = NULL;
472 md_entry_free (this);
474 return (0);
475 } /* }}} int meta_data_delete */
477 /*
478 * Add functions
479 */
480 int meta_data_add_string (meta_data_t *md, /* {{{ */
481 const char *key, const char *value)
482 {
483 meta_entry_t *e;
485 if ((md == NULL) || (key == NULL) || (value == NULL))
486 return (-EINVAL);
488 e = md_entry_alloc (key);
489 if (e == NULL)
490 return (-ENOMEM);
492 e->value.mv_string = md_strdup (value);
493 if (e->value.mv_string == NULL)
494 {
495 ERROR ("meta_data_add_string: md_strdup failed.");
496 md_entry_free (e);
497 return (-ENOMEM);
498 }
499 e->type = MD_TYPE_STRING;
501 return (md_entry_insert (md, e));
502 } /* }}} int meta_data_add_string */
504 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
505 const char *key, int64_t value)
506 {
507 meta_entry_t *e;
509 if ((md == NULL) || (key == NULL))
510 return (-EINVAL);
512 e = md_entry_alloc (key);
513 if (e == NULL)
514 return (-ENOMEM);
516 e->value.mv_signed_int = value;
517 e->type = MD_TYPE_SIGNED_INT;
519 return (md_entry_insert (md, e));
520 } /* }}} int meta_data_add_signed_int */
522 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
523 const char *key, uint64_t value)
524 {
525 meta_entry_t *e;
527 if ((md == NULL) || (key == NULL))
528 return (-EINVAL);
530 e = md_entry_alloc (key);
531 if (e == NULL)
532 return (-ENOMEM);
534 e->value.mv_unsigned_int = value;
535 e->type = MD_TYPE_UNSIGNED_INT;
537 return (md_entry_insert (md, e));
538 } /* }}} int meta_data_add_unsigned_int */
540 int meta_data_add_double (meta_data_t *md, /* {{{ */
541 const char *key, double value)
542 {
543 meta_entry_t *e;
545 if ((md == NULL) || (key == NULL))
546 return (-EINVAL);
548 e = md_entry_alloc (key);
549 if (e == NULL)
550 return (-ENOMEM);
552 e->value.mv_double = value;
553 e->type = MD_TYPE_DOUBLE;
555 return (md_entry_insert (md, e));
556 } /* }}} int meta_data_add_double */
558 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
559 const char *key, _Bool value)
560 {
561 meta_entry_t *e;
563 if ((md == NULL) || (key == NULL))
564 return (-EINVAL);
566 e = md_entry_alloc (key);
567 if (e == NULL)
568 return (-ENOMEM);
570 e->value.mv_boolean = value;
571 e->type = MD_TYPE_BOOLEAN;
573 return (md_entry_insert (md, e));
574 } /* }}} int meta_data_add_boolean */
576 /*
577 * Get functions
578 */
579 int meta_data_get_string (meta_data_t *md, /* {{{ */
580 const char *key, char **value)
581 {
582 meta_entry_t *e;
583 char *temp;
585 if ((md == NULL) || (key == NULL) || (value == NULL))
586 return (-EINVAL);
588 pthread_mutex_lock (&md->lock);
590 e = md_entry_lookup (md, key);
591 if (e == NULL)
592 {
593 pthread_mutex_unlock (&md->lock);
594 return (-ENOENT);
595 }
597 if (e->type != MD_TYPE_STRING)
598 {
599 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
600 pthread_mutex_unlock (&md->lock);
601 return (-ENOENT);
602 }
604 temp = md_strdup (e->value.mv_string);
605 if (temp == NULL)
606 {
607 pthread_mutex_unlock (&md->lock);
608 ERROR ("meta_data_get_string: md_strdup failed.");
609 return (-ENOMEM);
610 }
612 pthread_mutex_unlock (&md->lock);
614 *value = temp;
616 return (0);
617 } /* }}} int meta_data_get_string */
619 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
620 const char *key, int64_t *value)
621 {
622 meta_entry_t *e;
624 if ((md == NULL) || (key == NULL) || (value == NULL))
625 return (-EINVAL);
627 pthread_mutex_lock (&md->lock);
629 e = md_entry_lookup (md, key);
630 if (e == NULL)
631 {
632 pthread_mutex_unlock (&md->lock);
633 return (-ENOENT);
634 }
636 if (e->type != MD_TYPE_SIGNED_INT)
637 {
638 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
639 pthread_mutex_unlock (&md->lock);
640 return (-ENOENT);
641 }
643 *value = e->value.mv_signed_int;
645 pthread_mutex_unlock (&md->lock);
646 return (0);
647 } /* }}} int meta_data_get_signed_int */
649 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
650 const char *key, uint64_t *value)
651 {
652 meta_entry_t *e;
654 if ((md == NULL) || (key == NULL) || (value == NULL))
655 return (-EINVAL);
657 pthread_mutex_lock (&md->lock);
659 e = md_entry_lookup (md, key);
660 if (e == NULL)
661 {
662 pthread_mutex_unlock (&md->lock);
663 return (-ENOENT);
664 }
666 if (e->type != MD_TYPE_UNSIGNED_INT)
667 {
668 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
669 pthread_mutex_unlock (&md->lock);
670 return (-ENOENT);
671 }
673 *value = e->value.mv_unsigned_int;
675 pthread_mutex_unlock (&md->lock);
676 return (0);
677 } /* }}} int meta_data_get_unsigned_int */
679 int meta_data_get_double (meta_data_t *md, /* {{{ */
680 const char *key, double *value)
681 {
682 meta_entry_t *e;
684 if ((md == NULL) || (key == NULL) || (value == NULL))
685 return (-EINVAL);
687 pthread_mutex_lock (&md->lock);
689 e = md_entry_lookup (md, key);
690 if (e == NULL)
691 {
692 pthread_mutex_unlock (&md->lock);
693 return (-ENOENT);
694 }
696 if (e->type != MD_TYPE_DOUBLE)
697 {
698 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
699 pthread_mutex_unlock (&md->lock);
700 return (-ENOENT);
701 }
703 *value = e->value.mv_double;
705 pthread_mutex_unlock (&md->lock);
706 return (0);
707 } /* }}} int meta_data_get_double */
709 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
710 const char *key, _Bool *value)
711 {
712 meta_entry_t *e;
714 if ((md == NULL) || (key == NULL) || (value == NULL))
715 return (-EINVAL);
717 pthread_mutex_lock (&md->lock);
719 e = md_entry_lookup (md, key);
720 if (e == NULL)
721 {
722 pthread_mutex_unlock (&md->lock);
723 return (-ENOENT);
724 }
726 if (e->type != MD_TYPE_BOOLEAN)
727 {
728 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
729 pthread_mutex_unlock (&md->lock);
730 return (-ENOENT);
731 }
733 *value = e->value.mv_boolean;
735 pthread_mutex_unlock (&md->lock);
736 return (0);
737 } /* }}} int meta_data_get_boolean */
739 /* vim: set sw=2 sts=2 et fdm=marker : */