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 #include <pthread.h>
33 /*
34 * Data types
35 */
36 union meta_value_u
37 {
38 char *mv_string;
39 int64_t mv_signed_int;
40 uint64_t mv_unsigned_int;
41 double mv_double;
42 _Bool mv_boolean;
43 };
44 typedef union meta_value_u meta_value_t;
46 struct meta_entry_s;
47 typedef struct meta_entry_s meta_entry_t;
48 struct meta_entry_s
49 {
50 char *key;
51 meta_value_t value;
52 int type;
53 meta_entry_t *next;
54 };
56 struct meta_data_s
57 {
58 meta_entry_t *head;
59 pthread_mutex_t lock;
60 };
62 /*
63 * Private functions
64 */
65 static char *md_strdup (const char *orig) /* {{{ */
66 {
67 size_t sz;
68 char *dest;
70 if (orig == NULL)
71 return (NULL);
73 sz = strlen (orig) + 1;
74 dest = (char *) malloc (sz);
75 if (dest == NULL)
76 return (NULL);
78 memcpy (dest, orig, sz);
80 return (dest);
81 } /* }}} char *md_strdup */
83 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
84 {
85 meta_entry_t *e;
87 e = (meta_entry_t *) malloc (sizeof (*e));
88 if (e == NULL)
89 {
90 ERROR ("md_entry_alloc: malloc failed.");
91 return (NULL);
92 }
93 memset (e, 0, sizeof (*e));
95 e->key = md_strdup (key);
96 if (e->key == NULL)
97 {
98 free (e);
99 ERROR ("md_entry_alloc: md_strdup failed.");
100 return (NULL);
101 }
103 e->type = 0;
104 e->next = NULL;
106 return (e);
107 } /* }}} meta_entry_t *md_entry_alloc */
109 /* XXX: The lock on md must be held while calling this function! */
110 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
111 {
112 meta_entry_t *copy;
114 /* WARNINGS :
115 * - we do not check that orig != NULL here. You should have done it before.
116 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
117 */
119 copy = md_entry_alloc (orig->key);
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 * Public functions
288 */
289 meta_data_t *meta_data_create (void) /* {{{ */
290 {
291 meta_data_t *md;
293 md = (meta_data_t *) malloc (sizeof (*md));
294 if (md == NULL)
295 {
296 ERROR ("meta_data_create: malloc failed.");
297 return (NULL);
298 }
299 memset (md, 0, sizeof (*md));
301 md->head = NULL;
302 pthread_mutex_init (&md->lock, /* attr = */ NULL);
304 return (md);
305 } /* }}} meta_data_t *meta_data_create */
307 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
308 {
309 meta_data_t *copy;
311 if (orig == NULL)
312 return (NULL);
314 copy = meta_data_create ();
315 if (copy == NULL)
316 return (NULL);
318 pthread_mutex_lock (&orig->lock);
319 copy->head = md_entry_clone (orig->head);
320 pthread_mutex_unlock (&orig->lock);
322 return (copy);
323 } /* }}} meta_data_t *meta_data_clone */
325 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
326 {
327 meta_entry_t *e;
329 if (orig == NULL)
330 return (0);
332 if(NULL == *dest) {
333 *dest = meta_data_clone(orig);
334 return(0);
335 }
337 pthread_mutex_lock (&orig->lock);
338 for(e=orig->head; NULL != e; e = e->next) {
339 md_entry_insert_clone((*dest), e);
340 }
341 pthread_mutex_unlock (&orig->lock);
343 return (0);
344 } /* }}} int meta_data_clone_merge */
346 void meta_data_destroy (meta_data_t *md) /* {{{ */
347 {
348 if (md == NULL)
349 return;
351 pthread_mutex_destroy(&md->lock);
352 md_entry_free (md->head);
353 pthread_mutex_destroy (&md->lock);
354 free (md);
355 } /* }}} void meta_data_destroy */
357 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
358 {
359 meta_entry_t *e;
361 if ((md == NULL) || (key == NULL))
362 return (-EINVAL);
364 pthread_mutex_lock (&md->lock);
366 for (e = md->head; e != NULL; e = e->next)
367 {
368 if (strcasecmp (key, e->key) == 0)
369 {
370 pthread_mutex_unlock (&md->lock);
371 return (1);
372 }
373 }
375 pthread_mutex_unlock (&md->lock);
376 return (0);
377 } /* }}} int meta_data_exists */
379 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
380 {
381 meta_entry_t *e;
383 if ((md == NULL) || (key == NULL))
384 return -EINVAL;
386 pthread_mutex_lock (&md->lock);
388 for (e = md->head; e != NULL; e = e->next)
389 {
390 if (strcasecmp (key, e->key) == 0)
391 {
392 pthread_mutex_unlock (&md->lock);
393 return e->type;
394 }
395 }
397 pthread_mutex_unlock (&md->lock);
398 return 0;
399 } /* }}} int meta_data_type */
401 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
402 {
403 int i = 0, count = 0;
404 meta_entry_t *e;
406 if ((md == NULL) || (toc == NULL))
407 return -EINVAL;
409 pthread_mutex_lock (&md->lock);
411 for (e = md->head; e != NULL; e = e->next)
412 ++count;
414 if (count == 0)
415 {
416 pthread_mutex_unlock (&md->lock);
417 return (count);
418 }
420 *toc = calloc(count, sizeof(**toc));
421 for (e = md->head; e != NULL; e = e->next)
422 (*toc)[i++] = strdup(e->key);
424 pthread_mutex_unlock (&md->lock);
425 return count;
426 } /* }}} int meta_data_toc */
428 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
429 {
430 meta_entry_t *this;
431 meta_entry_t *prev;
433 if ((md == NULL) || (key == NULL))
434 return (-EINVAL);
436 pthread_mutex_lock (&md->lock);
438 prev = NULL;
439 this = md->head;
440 while (this != NULL)
441 {
442 if (strcasecmp (key, this->key) == 0)
443 break;
445 prev = this;
446 this = this->next;
447 }
449 if (this == NULL)
450 {
451 pthread_mutex_unlock (&md->lock);
452 return (-ENOENT);
453 }
455 if (prev == NULL)
456 md->head = this->next;
457 else
458 prev->next = this->next;
460 pthread_mutex_unlock (&md->lock);
462 this->next = NULL;
463 md_entry_free (this);
465 return (0);
466 } /* }}} int meta_data_delete */
468 /*
469 * Add functions
470 */
471 int meta_data_add_string (meta_data_t *md, /* {{{ */
472 const char *key, const char *value)
473 {
474 meta_entry_t *e;
476 if ((md == NULL) || (key == NULL) || (value == NULL))
477 return (-EINVAL);
479 e = md_entry_alloc (key);
480 if (e == NULL)
481 return (-ENOMEM);
483 e->value.mv_string = md_strdup (value);
484 if (e->value.mv_string == NULL)
485 {
486 ERROR ("meta_data_add_string: md_strdup failed.");
487 md_entry_free (e);
488 return (-ENOMEM);
489 }
490 e->type = MD_TYPE_STRING;
492 return (md_entry_insert (md, e));
493 } /* }}} int meta_data_add_string */
495 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
496 const char *key, int64_t value)
497 {
498 meta_entry_t *e;
500 if ((md == NULL) || (key == NULL))
501 return (-EINVAL);
503 e = md_entry_alloc (key);
504 if (e == NULL)
505 return (-ENOMEM);
507 e->value.mv_signed_int = value;
508 e->type = MD_TYPE_SIGNED_INT;
510 return (md_entry_insert (md, e));
511 } /* }}} int meta_data_add_signed_int */
513 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
514 const char *key, uint64_t value)
515 {
516 meta_entry_t *e;
518 if ((md == NULL) || (key == NULL))
519 return (-EINVAL);
521 e = md_entry_alloc (key);
522 if (e == NULL)
523 return (-ENOMEM);
525 e->value.mv_unsigned_int = value;
526 e->type = MD_TYPE_UNSIGNED_INT;
528 return (md_entry_insert (md, e));
529 } /* }}} int meta_data_add_unsigned_int */
531 int meta_data_add_double (meta_data_t *md, /* {{{ */
532 const char *key, double value)
533 {
534 meta_entry_t *e;
536 if ((md == NULL) || (key == NULL))
537 return (-EINVAL);
539 e = md_entry_alloc (key);
540 if (e == NULL)
541 return (-ENOMEM);
543 e->value.mv_double = value;
544 e->type = MD_TYPE_DOUBLE;
546 return (md_entry_insert (md, e));
547 } /* }}} int meta_data_add_double */
549 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
550 const char *key, _Bool value)
551 {
552 meta_entry_t *e;
554 if ((md == NULL) || (key == NULL))
555 return (-EINVAL);
557 e = md_entry_alloc (key);
558 if (e == NULL)
559 return (-ENOMEM);
561 e->value.mv_boolean = value;
562 e->type = MD_TYPE_BOOLEAN;
564 return (md_entry_insert (md, e));
565 } /* }}} int meta_data_add_boolean */
567 /*
568 * Get functions
569 */
570 int meta_data_get_string (meta_data_t *md, /* {{{ */
571 const char *key, char **value)
572 {
573 meta_entry_t *e;
574 char *temp;
576 if ((md == NULL) || (key == NULL) || (value == NULL))
577 return (-EINVAL);
579 pthread_mutex_lock (&md->lock);
581 e = md_entry_lookup (md, key);
582 if (e == NULL)
583 {
584 pthread_mutex_unlock (&md->lock);
585 return (-ENOENT);
586 }
588 if (e->type != MD_TYPE_STRING)
589 {
590 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
591 pthread_mutex_unlock (&md->lock);
592 return (-ENOENT);
593 }
595 temp = md_strdup (e->value.mv_string);
596 if (temp == NULL)
597 {
598 pthread_mutex_unlock (&md->lock);
599 ERROR ("meta_data_get_string: md_strdup failed.");
600 return (-ENOMEM);
601 }
603 pthread_mutex_unlock (&md->lock);
605 *value = temp;
607 return (0);
608 } /* }}} int meta_data_get_string */
610 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
611 const char *key, int64_t *value)
612 {
613 meta_entry_t *e;
615 if ((md == NULL) || (key == NULL) || (value == NULL))
616 return (-EINVAL);
618 pthread_mutex_lock (&md->lock);
620 e = md_entry_lookup (md, key);
621 if (e == NULL)
622 {
623 pthread_mutex_unlock (&md->lock);
624 return (-ENOENT);
625 }
627 if (e->type != MD_TYPE_SIGNED_INT)
628 {
629 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
630 pthread_mutex_unlock (&md->lock);
631 return (-ENOENT);
632 }
634 *value = e->value.mv_signed_int;
636 pthread_mutex_unlock (&md->lock);
637 return (0);
638 } /* }}} int meta_data_get_signed_int */
640 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
641 const char *key, uint64_t *value)
642 {
643 meta_entry_t *e;
645 if ((md == NULL) || (key == NULL) || (value == NULL))
646 return (-EINVAL);
648 pthread_mutex_lock (&md->lock);
650 e = md_entry_lookup (md, key);
651 if (e == NULL)
652 {
653 pthread_mutex_unlock (&md->lock);
654 return (-ENOENT);
655 }
657 if (e->type != MD_TYPE_UNSIGNED_INT)
658 {
659 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
660 pthread_mutex_unlock (&md->lock);
661 return (-ENOENT);
662 }
664 *value = e->value.mv_unsigned_int;
666 pthread_mutex_unlock (&md->lock);
667 return (0);
668 } /* }}} int meta_data_get_unsigned_int */
670 int meta_data_get_double (meta_data_t *md, /* {{{ */
671 const char *key, double *value)
672 {
673 meta_entry_t *e;
675 if ((md == NULL) || (key == NULL) || (value == NULL))
676 return (-EINVAL);
678 pthread_mutex_lock (&md->lock);
680 e = md_entry_lookup (md, key);
681 if (e == NULL)
682 {
683 pthread_mutex_unlock (&md->lock);
684 return (-ENOENT);
685 }
687 if (e->type != MD_TYPE_DOUBLE)
688 {
689 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
690 pthread_mutex_unlock (&md->lock);
691 return (-ENOENT);
692 }
694 *value = e->value.mv_double;
696 pthread_mutex_unlock (&md->lock);
697 return (0);
698 } /* }}} int meta_data_get_double */
700 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
701 const char *key, _Bool *value)
702 {
703 meta_entry_t *e;
705 if ((md == NULL) || (key == NULL) || (value == NULL))
706 return (-EINVAL);
708 pthread_mutex_lock (&md->lock);
710 e = md_entry_lookup (md, key);
711 if (e == NULL)
712 {
713 pthread_mutex_unlock (&md->lock);
714 return (-ENOENT);
715 }
717 if (e->type != MD_TYPE_BOOLEAN)
718 {
719 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
720 pthread_mutex_unlock (&md->lock);
721 return (-ENOENT);
722 }
724 *value = e->value.mv_boolean;
726 pthread_mutex_unlock (&md->lock);
727 return (0);
728 } /* }}} int meta_data_get_boolean */
730 /* vim: set sw=2 sts=2 et fdm=marker : */