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 if (orig == NULL)
337 return (0);
339 if (*dest == NULL) {
340 *dest = meta_data_clone(orig);
341 return(0);
342 }
344 pthread_mutex_lock (&orig->lock);
345 for (meta_entry_t *e=orig->head; e != NULL; e = e->next)
346 {
347 md_entry_insert_clone((*dest), e);
348 }
349 pthread_mutex_unlock (&orig->lock);
351 return (0);
352 } /* }}} int meta_data_clone_merge */
354 void meta_data_destroy (meta_data_t *md) /* {{{ */
355 {
356 if (md == NULL)
357 return;
359 md_entry_free (md->head);
360 pthread_mutex_destroy (&md->lock);
361 free (md);
362 } /* }}} void meta_data_destroy */
364 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
365 {
366 if ((md == NULL) || (key == NULL))
367 return (-EINVAL);
369 pthread_mutex_lock (&md->lock);
371 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
372 {
373 if (strcasecmp (key, e->key) == 0)
374 {
375 pthread_mutex_unlock (&md->lock);
376 return (1);
377 }
378 }
380 pthread_mutex_unlock (&md->lock);
381 return (0);
382 } /* }}} int meta_data_exists */
384 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
385 {
386 if ((md == NULL) || (key == NULL))
387 return -EINVAL;
389 pthread_mutex_lock (&md->lock);
391 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
392 {
393 if (strcasecmp (key, e->key) == 0)
394 {
395 pthread_mutex_unlock (&md->lock);
396 return e->type;
397 }
398 }
400 pthread_mutex_unlock (&md->lock);
401 return 0;
402 } /* }}} int meta_data_type */
404 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
405 {
406 int i = 0, count = 0;
408 if ((md == NULL) || (toc == NULL))
409 return -EINVAL;
411 pthread_mutex_lock (&md->lock);
413 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
414 ++count;
416 if (count == 0)
417 {
418 pthread_mutex_unlock (&md->lock);
419 return (count);
420 }
422 *toc = calloc(count, sizeof(**toc));
423 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
424 (*toc)[i++] = strdup(e->key);
426 pthread_mutex_unlock (&md->lock);
427 return count;
428 } /* }}} int meta_data_toc */
430 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
431 {
432 meta_entry_t *this;
433 meta_entry_t *prev;
435 if ((md == NULL) || (key == NULL))
436 return (-EINVAL);
438 pthread_mutex_lock (&md->lock);
440 prev = NULL;
441 this = md->head;
442 while (this != NULL)
443 {
444 if (strcasecmp (key, this->key) == 0)
445 break;
447 prev = this;
448 this = this->next;
449 }
451 if (this == NULL)
452 {
453 pthread_mutex_unlock (&md->lock);
454 return (-ENOENT);
455 }
457 if (prev == NULL)
458 md->head = this->next;
459 else
460 prev->next = this->next;
462 pthread_mutex_unlock (&md->lock);
464 this->next = NULL;
465 md_entry_free (this);
467 return (0);
468 } /* }}} int meta_data_delete */
470 /*
471 * Add functions
472 */
473 int meta_data_add_string (meta_data_t *md, /* {{{ */
474 const char *key, const char *value)
475 {
476 meta_entry_t *e;
478 if ((md == NULL) || (key == NULL) || (value == NULL))
479 return (-EINVAL);
481 e = md_entry_alloc (key);
482 if (e == NULL)
483 return (-ENOMEM);
485 e->value.mv_string = md_strdup (value);
486 if (e->value.mv_string == NULL)
487 {
488 ERROR ("meta_data_add_string: md_strdup failed.");
489 md_entry_free (e);
490 return (-ENOMEM);
491 }
492 e->type = MD_TYPE_STRING;
494 return (md_entry_insert (md, e));
495 } /* }}} int meta_data_add_string */
497 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
498 const char *key, int64_t value)
499 {
500 meta_entry_t *e;
502 if ((md == NULL) || (key == NULL))
503 return (-EINVAL);
505 e = md_entry_alloc (key);
506 if (e == NULL)
507 return (-ENOMEM);
509 e->value.mv_signed_int = value;
510 e->type = MD_TYPE_SIGNED_INT;
512 return (md_entry_insert (md, e));
513 } /* }}} int meta_data_add_signed_int */
515 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
516 const char *key, uint64_t value)
517 {
518 meta_entry_t *e;
520 if ((md == NULL) || (key == NULL))
521 return (-EINVAL);
523 e = md_entry_alloc (key);
524 if (e == NULL)
525 return (-ENOMEM);
527 e->value.mv_unsigned_int = value;
528 e->type = MD_TYPE_UNSIGNED_INT;
530 return (md_entry_insert (md, e));
531 } /* }}} int meta_data_add_unsigned_int */
533 int meta_data_add_double (meta_data_t *md, /* {{{ */
534 const char *key, double value)
535 {
536 meta_entry_t *e;
538 if ((md == NULL) || (key == NULL))
539 return (-EINVAL);
541 e = md_entry_alloc (key);
542 if (e == NULL)
543 return (-ENOMEM);
545 e->value.mv_double = value;
546 e->type = MD_TYPE_DOUBLE;
548 return (md_entry_insert (md, e));
549 } /* }}} int meta_data_add_double */
551 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
552 const char *key, _Bool value)
553 {
554 meta_entry_t *e;
556 if ((md == NULL) || (key == NULL))
557 return (-EINVAL);
559 e = md_entry_alloc (key);
560 if (e == NULL)
561 return (-ENOMEM);
563 e->value.mv_boolean = value;
564 e->type = MD_TYPE_BOOLEAN;
566 return (md_entry_insert (md, e));
567 } /* }}} int meta_data_add_boolean */
569 /*
570 * Get functions
571 */
572 int meta_data_get_string (meta_data_t *md, /* {{{ */
573 const char *key, char **value)
574 {
575 meta_entry_t *e;
576 char *temp;
578 if ((md == NULL) || (key == NULL) || (value == NULL))
579 return (-EINVAL);
581 pthread_mutex_lock (&md->lock);
583 e = md_entry_lookup (md, key);
584 if (e == NULL)
585 {
586 pthread_mutex_unlock (&md->lock);
587 return (-ENOENT);
588 }
590 if (e->type != MD_TYPE_STRING)
591 {
592 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
593 pthread_mutex_unlock (&md->lock);
594 return (-ENOENT);
595 }
597 temp = md_strdup (e->value.mv_string);
598 if (temp == NULL)
599 {
600 pthread_mutex_unlock (&md->lock);
601 ERROR ("meta_data_get_string: md_strdup failed.");
602 return (-ENOMEM);
603 }
605 pthread_mutex_unlock (&md->lock);
607 *value = temp;
609 return (0);
610 } /* }}} int meta_data_get_string */
612 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
613 const char *key, int64_t *value)
614 {
615 meta_entry_t *e;
617 if ((md == NULL) || (key == NULL) || (value == NULL))
618 return (-EINVAL);
620 pthread_mutex_lock (&md->lock);
622 e = md_entry_lookup (md, key);
623 if (e == NULL)
624 {
625 pthread_mutex_unlock (&md->lock);
626 return (-ENOENT);
627 }
629 if (e->type != MD_TYPE_SIGNED_INT)
630 {
631 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
632 pthread_mutex_unlock (&md->lock);
633 return (-ENOENT);
634 }
636 *value = e->value.mv_signed_int;
638 pthread_mutex_unlock (&md->lock);
639 return (0);
640 } /* }}} int meta_data_get_signed_int */
642 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
643 const char *key, uint64_t *value)
644 {
645 meta_entry_t *e;
647 if ((md == NULL) || (key == NULL) || (value == NULL))
648 return (-EINVAL);
650 pthread_mutex_lock (&md->lock);
652 e = md_entry_lookup (md, key);
653 if (e == NULL)
654 {
655 pthread_mutex_unlock (&md->lock);
656 return (-ENOENT);
657 }
659 if (e->type != MD_TYPE_UNSIGNED_INT)
660 {
661 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
662 pthread_mutex_unlock (&md->lock);
663 return (-ENOENT);
664 }
666 *value = e->value.mv_unsigned_int;
668 pthread_mutex_unlock (&md->lock);
669 return (0);
670 } /* }}} int meta_data_get_unsigned_int */
672 int meta_data_get_double (meta_data_t *md, /* {{{ */
673 const char *key, double *value)
674 {
675 meta_entry_t *e;
677 if ((md == NULL) || (key == NULL) || (value == NULL))
678 return (-EINVAL);
680 pthread_mutex_lock (&md->lock);
682 e = md_entry_lookup (md, key);
683 if (e == NULL)
684 {
685 pthread_mutex_unlock (&md->lock);
686 return (-ENOENT);
687 }
689 if (e->type != MD_TYPE_DOUBLE)
690 {
691 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
692 pthread_mutex_unlock (&md->lock);
693 return (-ENOENT);
694 }
696 *value = e->value.mv_double;
698 pthread_mutex_unlock (&md->lock);
699 return (0);
700 } /* }}} int meta_data_get_double */
702 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
703 const char *key, _Bool *value)
704 {
705 meta_entry_t *e;
707 if ((md == NULL) || (key == NULL) || (value == NULL))
708 return (-EINVAL);
710 pthread_mutex_lock (&md->lock);
712 e = md_entry_lookup (md, key);
713 if (e == NULL)
714 {
715 pthread_mutex_unlock (&md->lock);
716 return (-ENOENT);
717 }
719 if (e->type != MD_TYPE_BOOLEAN)
720 {
721 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
722 pthread_mutex_unlock (&md->lock);
723 return (-ENOENT);
724 }
726 *value = e->value.mv_boolean;
728 pthread_mutex_unlock (&md->lock);
729 return (0);
730 } /* }}} int meta_data_get_boolean */
732 /* vim: set sw=2 sts=2 et fdm=marker : */