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"
28 #include "plugin.h"
29 #include "meta_data.h"
31 /*
32 * Data types
33 */
34 union meta_value_u
35 {
36 char *mv_string;
37 int64_t mv_signed_int;
38 uint64_t mv_unsigned_int;
39 double mv_double;
40 _Bool mv_boolean;
41 };
42 typedef union meta_value_u meta_value_t;
44 struct meta_entry_s;
45 typedef struct meta_entry_s meta_entry_t;
46 struct meta_entry_s
47 {
48 char *key;
49 meta_value_t value;
50 int type;
51 meta_entry_t *next;
52 };
54 struct meta_data_s
55 {
56 meta_entry_t *head;
57 pthread_mutex_t lock;
58 };
60 /*
61 * Private functions
62 */
63 static char *md_strdup (const char *orig) /* {{{ */
64 {
65 size_t sz;
66 char *dest;
68 if (orig == NULL)
69 return (NULL);
71 sz = strlen (orig) + 1;
72 dest = malloc (sz);
73 if (dest == NULL)
74 return (NULL);
76 memcpy (dest, orig, sz);
78 return (dest);
79 } /* }}} char *md_strdup */
81 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
82 {
83 meta_entry_t *e;
85 e = calloc (1, sizeof (*e));
86 if (e == NULL)
87 {
88 ERROR ("md_entry_alloc: calloc failed.");
89 return (NULL);
90 }
92 e->key = md_strdup (key);
93 if (e->key == NULL)
94 {
95 free (e);
96 ERROR ("md_entry_alloc: md_strdup failed.");
97 return (NULL);
98 }
100 e->type = 0;
101 e->next = NULL;
103 return (e);
104 } /* }}} meta_entry_t *md_entry_alloc */
106 /* XXX: The lock on md must be held while calling this function! */
107 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
108 {
109 meta_entry_t *copy;
111 /* WARNINGS :
112 * - we do not check that orig != NULL here. You should have done it before.
113 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
114 */
116 copy = md_entry_alloc (orig->key);
117 if (copy == NULL)
118 return (NULL);
119 copy->type = orig->type;
120 if (copy->type == MD_TYPE_STRING)
121 copy->value.mv_string = strdup (orig->value.mv_string);
122 else
123 copy->value = orig->value;
125 return (copy);
126 } /* }}} meta_entry_t *md_entry_clone_contents */
128 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
129 {
130 meta_entry_t *copy;
132 if (orig == NULL)
133 return (NULL);
135 copy = md_entry_clone_contents(orig);
137 copy->next = md_entry_clone (orig->next);
138 return (copy);
139 } /* }}} meta_entry_t *md_entry_clone */
141 static void md_entry_free (meta_entry_t *e) /* {{{ */
142 {
143 if (e == NULL)
144 return;
146 free (e->key);
148 if (e->type == MD_TYPE_STRING)
149 free (e->value.mv_string);
151 if (e->next != NULL)
152 md_entry_free (e->next);
154 free (e);
155 } /* }}} void md_entry_free */
157 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
158 {
159 meta_entry_t *this;
160 meta_entry_t *prev;
162 if ((md == NULL) || (e == NULL))
163 return (-EINVAL);
165 pthread_mutex_lock (&md->lock);
167 prev = NULL;
168 this = md->head;
169 while (this != NULL)
170 {
171 if (strcasecmp (e->key, this->key) == 0)
172 break;
174 prev = this;
175 this = this->next;
176 }
178 if (this == NULL)
179 {
180 /* This key does not exist yet. */
181 if (md->head == NULL)
182 md->head = e;
183 else
184 {
185 assert (prev != NULL);
186 prev->next = e;
187 }
189 e->next = NULL;
190 }
191 else /* (this != NULL) */
192 {
193 if (prev == NULL)
194 md->head = e;
195 else
196 prev->next = e;
198 e->next = this->next;
199 }
201 pthread_mutex_unlock (&md->lock);
203 if (this != NULL)
204 {
205 this->next = NULL;
206 md_entry_free (this);
207 }
209 return (0);
210 } /* }}} int md_entry_insert */
212 /* XXX: The lock on md must be held while calling this function! */
213 static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */
214 {
215 meta_entry_t *e;
216 meta_entry_t *this;
217 meta_entry_t *prev;
219 /* WARNINGS :
220 * - we do not check that md and e != NULL here. You should have done it before.
221 * - we do not use the lock. You should have set it before.
222 */
224 e = md_entry_clone_contents(orig);
226 prev = NULL;
227 this = md->head;
228 while (this != NULL)
229 {
230 if (strcasecmp (e->key, this->key) == 0)
231 break;
233 prev = this;
234 this = this->next;
235 }
237 if (this == NULL)
238 {
239 /* This key does not exist yet. */
240 if (md->head == NULL)
241 md->head = e;
242 else
243 {
244 assert (prev != NULL);
245 prev->next = e;
246 }
248 e->next = NULL;
249 }
250 else /* (this != NULL) */
251 {
252 if (prev == NULL)
253 md->head = e;
254 else
255 prev->next = e;
257 e->next = this->next;
258 }
260 if (this != NULL)
261 {
262 this->next = NULL;
263 md_entry_free (this);
264 }
266 return (0);
267 } /* }}} int md_entry_insert_clone */
269 /* XXX: The lock on md must be held while calling this function! */
270 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
271 const char *key)
272 {
273 meta_entry_t *e;
275 if ((md == NULL) || (key == NULL))
276 return (NULL);
278 for (e = md->head; e != NULL; e = e->next)
279 if (strcasecmp (key, e->key) == 0)
280 break;
282 return (e);
283 } /* }}} meta_entry_t *md_entry_lookup */
285 /*
286 * Public functions
287 */
288 meta_data_t *meta_data_create (void) /* {{{ */
289 {
290 meta_data_t *md;
292 md = calloc (1, sizeof (*md));
293 if (md == NULL)
294 {
295 ERROR ("meta_data_create: calloc failed.");
296 return (NULL);
297 }
299 pthread_mutex_init (&md->lock, /* attr = */ NULL);
301 return (md);
302 } /* }}} meta_data_t *meta_data_create */
304 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
305 {
306 meta_data_t *copy;
308 if (orig == NULL)
309 return (NULL);
311 copy = meta_data_create ();
312 if (copy == NULL)
313 return (NULL);
315 pthread_mutex_lock (&orig->lock);
316 copy->head = md_entry_clone (orig->head);
317 pthread_mutex_unlock (&orig->lock);
319 return (copy);
320 } /* }}} meta_data_t *meta_data_clone */
322 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
323 {
324 meta_entry_t *e;
326 if (orig == NULL)
327 return (0);
329 if (*dest == NULL) {
330 *dest = meta_data_clone(orig);
331 return(0);
332 }
334 pthread_mutex_lock (&orig->lock);
335 for (e=orig->head; e != NULL; e = e->next)
336 {
337 md_entry_insert_clone((*dest), e);
338 }
339 pthread_mutex_unlock (&orig->lock);
341 return (0);
342 } /* }}} int meta_data_clone_merge */
344 void meta_data_destroy (meta_data_t *md) /* {{{ */
345 {
346 if (md == NULL)
347 return;
349 md_entry_free (md->head);
350 pthread_mutex_destroy (&md->lock);
351 free (md);
352 } /* }}} void meta_data_destroy */
354 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
355 {
356 meta_entry_t *e;
358 if ((md == NULL) || (key == NULL))
359 return (-EINVAL);
361 pthread_mutex_lock (&md->lock);
363 for (e = md->head; e != NULL; e = e->next)
364 {
365 if (strcasecmp (key, e->key) == 0)
366 {
367 pthread_mutex_unlock (&md->lock);
368 return (1);
369 }
370 }
372 pthread_mutex_unlock (&md->lock);
373 return (0);
374 } /* }}} int meta_data_exists */
376 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
377 {
378 meta_entry_t *e;
380 if ((md == NULL) || (key == NULL))
381 return -EINVAL;
383 pthread_mutex_lock (&md->lock);
385 for (e = md->head; e != NULL; e = e->next)
386 {
387 if (strcasecmp (key, e->key) == 0)
388 {
389 pthread_mutex_unlock (&md->lock);
390 return e->type;
391 }
392 }
394 pthread_mutex_unlock (&md->lock);
395 return 0;
396 } /* }}} int meta_data_type */
398 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
399 {
400 int i = 0, count = 0;
401 meta_entry_t *e;
403 if ((md == NULL) || (toc == NULL))
404 return -EINVAL;
406 pthread_mutex_lock (&md->lock);
408 for (e = md->head; e != NULL; e = e->next)
409 ++count;
411 if (count == 0)
412 {
413 pthread_mutex_unlock (&md->lock);
414 return (count);
415 }
417 *toc = calloc(count, sizeof(**toc));
418 for (e = md->head; e != NULL; e = e->next)
419 (*toc)[i++] = strdup(e->key);
421 pthread_mutex_unlock (&md->lock);
422 return count;
423 } /* }}} int meta_data_toc */
425 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
426 {
427 meta_entry_t *this;
428 meta_entry_t *prev;
430 if ((md == NULL) || (key == NULL))
431 return (-EINVAL);
433 pthread_mutex_lock (&md->lock);
435 prev = NULL;
436 this = md->head;
437 while (this != NULL)
438 {
439 if (strcasecmp (key, this->key) == 0)
440 break;
442 prev = this;
443 this = this->next;
444 }
446 if (this == NULL)
447 {
448 pthread_mutex_unlock (&md->lock);
449 return (-ENOENT);
450 }
452 if (prev == NULL)
453 md->head = this->next;
454 else
455 prev->next = this->next;
457 pthread_mutex_unlock (&md->lock);
459 this->next = NULL;
460 md_entry_free (this);
462 return (0);
463 } /* }}} int meta_data_delete */
465 /*
466 * Add functions
467 */
468 int meta_data_add_string (meta_data_t *md, /* {{{ */
469 const char *key, const char *value)
470 {
471 meta_entry_t *e;
473 if ((md == NULL) || (key == NULL) || (value == NULL))
474 return (-EINVAL);
476 e = md_entry_alloc (key);
477 if (e == NULL)
478 return (-ENOMEM);
480 e->value.mv_string = md_strdup (value);
481 if (e->value.mv_string == NULL)
482 {
483 ERROR ("meta_data_add_string: md_strdup failed.");
484 md_entry_free (e);
485 return (-ENOMEM);
486 }
487 e->type = MD_TYPE_STRING;
489 return (md_entry_insert (md, e));
490 } /* }}} int meta_data_add_string */
492 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
493 const char *key, int64_t value)
494 {
495 meta_entry_t *e;
497 if ((md == NULL) || (key == NULL))
498 return (-EINVAL);
500 e = md_entry_alloc (key);
501 if (e == NULL)
502 return (-ENOMEM);
504 e->value.mv_signed_int = value;
505 e->type = MD_TYPE_SIGNED_INT;
507 return (md_entry_insert (md, e));
508 } /* }}} int meta_data_add_signed_int */
510 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
511 const char *key, uint64_t value)
512 {
513 meta_entry_t *e;
515 if ((md == NULL) || (key == NULL))
516 return (-EINVAL);
518 e = md_entry_alloc (key);
519 if (e == NULL)
520 return (-ENOMEM);
522 e->value.mv_unsigned_int = value;
523 e->type = MD_TYPE_UNSIGNED_INT;
525 return (md_entry_insert (md, e));
526 } /* }}} int meta_data_add_unsigned_int */
528 int meta_data_add_double (meta_data_t *md, /* {{{ */
529 const char *key, double value)
530 {
531 meta_entry_t *e;
533 if ((md == NULL) || (key == NULL))
534 return (-EINVAL);
536 e = md_entry_alloc (key);
537 if (e == NULL)
538 return (-ENOMEM);
540 e->value.mv_double = value;
541 e->type = MD_TYPE_DOUBLE;
543 return (md_entry_insert (md, e));
544 } /* }}} int meta_data_add_double */
546 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
547 const char *key, _Bool value)
548 {
549 meta_entry_t *e;
551 if ((md == NULL) || (key == NULL))
552 return (-EINVAL);
554 e = md_entry_alloc (key);
555 if (e == NULL)
556 return (-ENOMEM);
558 e->value.mv_boolean = value;
559 e->type = MD_TYPE_BOOLEAN;
561 return (md_entry_insert (md, e));
562 } /* }}} int meta_data_add_boolean */
564 /*
565 * Get functions
566 */
567 int meta_data_get_string (meta_data_t *md, /* {{{ */
568 const char *key, char **value)
569 {
570 meta_entry_t *e;
571 char *temp;
573 if ((md == NULL) || (key == NULL) || (value == NULL))
574 return (-EINVAL);
576 pthread_mutex_lock (&md->lock);
578 e = md_entry_lookup (md, key);
579 if (e == NULL)
580 {
581 pthread_mutex_unlock (&md->lock);
582 return (-ENOENT);
583 }
585 if (e->type != MD_TYPE_STRING)
586 {
587 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
588 pthread_mutex_unlock (&md->lock);
589 return (-ENOENT);
590 }
592 temp = md_strdup (e->value.mv_string);
593 if (temp == NULL)
594 {
595 pthread_mutex_unlock (&md->lock);
596 ERROR ("meta_data_get_string: md_strdup failed.");
597 return (-ENOMEM);
598 }
600 pthread_mutex_unlock (&md->lock);
602 *value = temp;
604 return (0);
605 } /* }}} int meta_data_get_string */
607 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
608 const char *key, int64_t *value)
609 {
610 meta_entry_t *e;
612 if ((md == NULL) || (key == NULL) || (value == NULL))
613 return (-EINVAL);
615 pthread_mutex_lock (&md->lock);
617 e = md_entry_lookup (md, key);
618 if (e == NULL)
619 {
620 pthread_mutex_unlock (&md->lock);
621 return (-ENOENT);
622 }
624 if (e->type != MD_TYPE_SIGNED_INT)
625 {
626 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
627 pthread_mutex_unlock (&md->lock);
628 return (-ENOENT);
629 }
631 *value = e->value.mv_signed_int;
633 pthread_mutex_unlock (&md->lock);
634 return (0);
635 } /* }}} int meta_data_get_signed_int */
637 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
638 const char *key, uint64_t *value)
639 {
640 meta_entry_t *e;
642 if ((md == NULL) || (key == NULL) || (value == NULL))
643 return (-EINVAL);
645 pthread_mutex_lock (&md->lock);
647 e = md_entry_lookup (md, key);
648 if (e == NULL)
649 {
650 pthread_mutex_unlock (&md->lock);
651 return (-ENOENT);
652 }
654 if (e->type != MD_TYPE_UNSIGNED_INT)
655 {
656 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
657 pthread_mutex_unlock (&md->lock);
658 return (-ENOENT);
659 }
661 *value = e->value.mv_unsigned_int;
663 pthread_mutex_unlock (&md->lock);
664 return (0);
665 } /* }}} int meta_data_get_unsigned_int */
667 int meta_data_get_double (meta_data_t *md, /* {{{ */
668 const char *key, double *value)
669 {
670 meta_entry_t *e;
672 if ((md == NULL) || (key == NULL) || (value == NULL))
673 return (-EINVAL);
675 pthread_mutex_lock (&md->lock);
677 e = md_entry_lookup (md, key);
678 if (e == NULL)
679 {
680 pthread_mutex_unlock (&md->lock);
681 return (-ENOENT);
682 }
684 if (e->type != MD_TYPE_DOUBLE)
685 {
686 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
687 pthread_mutex_unlock (&md->lock);
688 return (-ENOENT);
689 }
691 *value = e->value.mv_double;
693 pthread_mutex_unlock (&md->lock);
694 return (0);
695 } /* }}} int meta_data_get_double */
697 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
698 const char *key, _Bool *value)
699 {
700 meta_entry_t *e;
702 if ((md == NULL) || (key == NULL) || (value == NULL))
703 return (-EINVAL);
705 pthread_mutex_lock (&md->lock);
707 e = md_entry_lookup (md, key);
708 if (e == NULL)
709 {
710 pthread_mutex_unlock (&md->lock);
711 return (-ENOENT);
712 }
714 if (e->type != MD_TYPE_BOOLEAN)
715 {
716 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
717 pthread_mutex_unlock (&md->lock);
718 return (-ENOENT);
719 }
721 *value = e->value.mv_boolean;
723 pthread_mutex_unlock (&md->lock);
724 return (0);
725 } /* }}} int meta_data_get_boolean */
727 /* vim: set sw=2 sts=2 et fdm=marker : */