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 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
110 {
111 meta_entry_t *copy;
113 if (orig == NULL)
114 return (NULL);
116 copy = md_entry_alloc (orig->key);
117 copy->type = orig->type;
118 if (copy->type == MD_TYPE_STRING)
119 copy->value.mv_string = strdup (orig->value.mv_string);
120 else
121 copy->value = orig->value;
123 copy->next = md_entry_clone (orig->next);
124 return (copy);
125 } /* }}} meta_entry_t *md_entry_clone */
127 static void md_entry_free (meta_entry_t *e) /* {{{ */
128 {
129 if (e == NULL)
130 return;
132 free (e->key);
134 if (e->type == MD_TYPE_STRING)
135 free (e->value.mv_string);
137 if (e->next != NULL)
138 md_entry_free (e->next);
140 free (e);
141 } /* }}} void md_entry_free */
143 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
144 {
145 meta_entry_t *this;
146 meta_entry_t *prev;
148 if ((md == NULL) || (e == NULL))
149 return (-EINVAL);
151 pthread_mutex_lock (&md->lock);
153 prev = NULL;
154 this = md->head;
155 while (this != NULL)
156 {
157 if (strcasecmp (e->key, this->key) == 0)
158 break;
160 prev = this;
161 this = this->next;
162 }
164 if (this == NULL)
165 {
166 /* This key does not exist yet. */
167 if (md->head == NULL)
168 md->head = e;
169 else
170 {
171 assert (prev != NULL);
172 prev->next = e;
173 }
175 e->next = NULL;
176 }
177 else /* (this != NULL) */
178 {
179 if (prev == NULL)
180 md->head = e;
181 else
182 prev->next = e;
184 e->next = this->next;
185 }
187 pthread_mutex_unlock (&md->lock);
189 if (this != NULL)
190 {
191 this->next = NULL;
192 md_entry_free (this);
193 }
195 return (0);
196 } /* }}} int md_entry_insert */
198 /* XXX: The lock on md must be held while calling this function! */
199 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
200 const char *key)
201 {
202 meta_entry_t *e;
204 if ((md == NULL) || (key == NULL))
205 return (NULL);
207 for (e = md->head; e != NULL; e = e->next)
208 if (strcasecmp (key, e->key) == 0)
209 break;
211 return (e);
212 } /* }}} meta_entry_t *md_entry_lookup */
214 /*
215 * Public functions
216 */
217 meta_data_t *meta_data_create (void) /* {{{ */
218 {
219 meta_data_t *md;
221 md = (meta_data_t *) malloc (sizeof (*md));
222 if (md == NULL)
223 {
224 ERROR ("meta_data_create: malloc failed.");
225 return (NULL);
226 }
227 memset (md, 0, sizeof (*md));
229 md->head = NULL;
230 pthread_mutex_init (&md->lock, /* attr = */ NULL);
232 return (md);
233 } /* }}} meta_data_t *meta_data_create */
235 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
236 {
237 meta_data_t *copy;
239 if (orig == NULL)
240 return (NULL);
242 copy = meta_data_create ();
243 if (copy == NULL)
244 return (NULL);
246 pthread_mutex_lock (&orig->lock);
247 copy->head = md_entry_clone (orig->head);
248 pthread_mutex_unlock (&orig->lock);
250 return (copy);
251 } /* }}} meta_data_t *meta_data_clone */
253 void meta_data_destroy (meta_data_t *md) /* {{{ */
254 {
255 if (md == NULL)
256 return;
258 pthread_mutex_destroy(&md->lock);
259 md_entry_free (md->head);
260 pthread_mutex_destroy (&md->lock);
261 free (md);
262 } /* }}} void meta_data_destroy */
264 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
265 {
266 meta_entry_t *e;
268 if ((md == NULL) || (key == NULL))
269 return (-EINVAL);
271 pthread_mutex_lock (&md->lock);
273 for (e = md->head; e != NULL; e = e->next)
274 {
275 if (strcasecmp (key, e->key) == 0)
276 {
277 pthread_mutex_unlock (&md->lock);
278 return (1);
279 }
280 }
282 pthread_mutex_unlock (&md->lock);
283 return (0);
284 } /* }}} int meta_data_exists */
286 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
287 {
288 meta_entry_t *e;
290 if ((md == NULL) || (key == NULL))
291 return -EINVAL;
293 pthread_mutex_lock (&md->lock);
295 for (e = md->head; e != NULL; e = e->next)
296 {
297 if (strcasecmp (key, e->key) == 0)
298 {
299 pthread_mutex_unlock (&md->lock);
300 return e->type;
301 }
302 }
304 pthread_mutex_unlock (&md->lock);
305 return 0;
306 } /* }}} int meta_data_type */
308 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
309 {
310 int i = 0, count = 0;
311 meta_entry_t *e;
313 if ((md == NULL) || (toc == NULL))
314 return -EINVAL;
316 pthread_mutex_lock (&md->lock);
318 for (e = md->head; e != NULL; e = e->next)
319 ++count;
321 *toc = malloc(count * sizeof(**toc));
322 for (e = md->head; e != NULL; e = e->next)
323 (*toc)[i++] = strdup(e->key);
325 pthread_mutex_unlock (&md->lock);
326 return count;
327 } /* }}} int meta_data_toc */
329 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
330 {
331 meta_entry_t *this;
332 meta_entry_t *prev;
334 if ((md == NULL) || (key == NULL))
335 return (-EINVAL);
337 pthread_mutex_lock (&md->lock);
339 prev = NULL;
340 this = md->head;
341 while (this != NULL)
342 {
343 if (strcasecmp (key, this->key) == 0)
344 break;
346 prev = this;
347 this = this->next;
348 }
350 if (this == NULL)
351 {
352 pthread_mutex_unlock (&md->lock);
353 return (-ENOENT);
354 }
356 if (prev == NULL)
357 md->head = this->next;
358 else
359 prev->next = this->next;
361 pthread_mutex_unlock (&md->lock);
363 this->next = NULL;
364 md_entry_free (this);
366 return (0);
367 } /* }}} int meta_data_delete */
369 /*
370 * Add functions
371 */
372 int meta_data_add_string (meta_data_t *md, /* {{{ */
373 const char *key, const char *value)
374 {
375 meta_entry_t *e;
377 if ((md == NULL) || (key == NULL) || (value == NULL))
378 return (-EINVAL);
380 e = md_entry_alloc (key);
381 if (e == NULL)
382 return (-ENOMEM);
384 e->value.mv_string = md_strdup (value);
385 if (e->value.mv_string == NULL)
386 {
387 ERROR ("meta_data_add_string: md_strdup failed.");
388 md_entry_free (e);
389 return (-ENOMEM);
390 }
391 e->type = MD_TYPE_STRING;
393 return (md_entry_insert (md, e));
394 } /* }}} int meta_data_add_string */
396 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
397 const char *key, int64_t value)
398 {
399 meta_entry_t *e;
401 if ((md == NULL) || (key == NULL))
402 return (-EINVAL);
404 e = md_entry_alloc (key);
405 if (e == NULL)
406 return (-ENOMEM);
408 e->value.mv_signed_int = value;
409 e->type = MD_TYPE_SIGNED_INT;
411 return (md_entry_insert (md, e));
412 } /* }}} int meta_data_add_signed_int */
414 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
415 const char *key, uint64_t value)
416 {
417 meta_entry_t *e;
419 if ((md == NULL) || (key == NULL))
420 return (-EINVAL);
422 e = md_entry_alloc (key);
423 if (e == NULL)
424 return (-ENOMEM);
426 e->value.mv_unsigned_int = value;
427 e->type = MD_TYPE_UNSIGNED_INT;
429 return (md_entry_insert (md, e));
430 } /* }}} int meta_data_add_unsigned_int */
432 int meta_data_add_double (meta_data_t *md, /* {{{ */
433 const char *key, double value)
434 {
435 meta_entry_t *e;
437 if ((md == NULL) || (key == NULL))
438 return (-EINVAL);
440 e = md_entry_alloc (key);
441 if (e == NULL)
442 return (-ENOMEM);
444 e->value.mv_double = value;
445 e->type = MD_TYPE_DOUBLE;
447 return (md_entry_insert (md, e));
448 } /* }}} int meta_data_add_double */
450 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
451 const char *key, _Bool value)
452 {
453 meta_entry_t *e;
455 if ((md == NULL) || (key == NULL))
456 return (-EINVAL);
458 e = md_entry_alloc (key);
459 if (e == NULL)
460 return (-ENOMEM);
462 e->value.mv_boolean = value;
463 e->type = MD_TYPE_BOOLEAN;
465 return (md_entry_insert (md, e));
466 } /* }}} int meta_data_add_boolean */
468 /*
469 * Get functions
470 */
471 int meta_data_get_string (meta_data_t *md, /* {{{ */
472 const char *key, char **value)
473 {
474 meta_entry_t *e;
475 char *temp;
477 if ((md == NULL) || (key == NULL) || (value == NULL))
478 return (-EINVAL);
480 pthread_mutex_lock (&md->lock);
482 e = md_entry_lookup (md, key);
483 if (e == NULL)
484 {
485 pthread_mutex_unlock (&md->lock);
486 return (-ENOENT);
487 }
489 if (e->type != MD_TYPE_STRING)
490 {
491 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
492 pthread_mutex_unlock (&md->lock);
493 return (-ENOENT);
494 }
496 temp = md_strdup (e->value.mv_string);
497 if (temp == NULL)
498 {
499 pthread_mutex_unlock (&md->lock);
500 ERROR ("meta_data_get_string: md_strdup failed.");
501 return (-ENOMEM);
502 }
504 pthread_mutex_unlock (&md->lock);
506 *value = temp;
508 return (0);
509 } /* }}} int meta_data_get_string */
511 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
512 const char *key, int64_t *value)
513 {
514 meta_entry_t *e;
516 if ((md == NULL) || (key == NULL) || (value == NULL))
517 return (-EINVAL);
519 pthread_mutex_lock (&md->lock);
521 e = md_entry_lookup (md, key);
522 if (e == NULL)
523 {
524 pthread_mutex_unlock (&md->lock);
525 return (-ENOENT);
526 }
528 if (e->type != MD_TYPE_SIGNED_INT)
529 {
530 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
531 pthread_mutex_unlock (&md->lock);
532 return (-ENOENT);
533 }
535 *value = e->value.mv_signed_int;
537 pthread_mutex_unlock (&md->lock);
538 return (0);
539 } /* }}} int meta_data_get_signed_int */
541 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
542 const char *key, uint64_t *value)
543 {
544 meta_entry_t *e;
546 if ((md == NULL) || (key == NULL) || (value == NULL))
547 return (-EINVAL);
549 pthread_mutex_lock (&md->lock);
551 e = md_entry_lookup (md, key);
552 if (e == NULL)
553 {
554 pthread_mutex_unlock (&md->lock);
555 return (-ENOENT);
556 }
558 if (e->type != MD_TYPE_UNSIGNED_INT)
559 {
560 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
561 pthread_mutex_unlock (&md->lock);
562 return (-ENOENT);
563 }
565 *value = e->value.mv_unsigned_int;
567 pthread_mutex_unlock (&md->lock);
568 return (0);
569 } /* }}} int meta_data_get_unsigned_int */
571 int meta_data_get_double (meta_data_t *md, /* {{{ */
572 const char *key, double *value)
573 {
574 meta_entry_t *e;
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_DOUBLE)
589 {
590 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
591 pthread_mutex_unlock (&md->lock);
592 return (-ENOENT);
593 }
595 *value = e->value.mv_double;
597 pthread_mutex_unlock (&md->lock);
598 return (0);
599 } /* }}} int meta_data_get_double */
601 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
602 const char *key, _Bool *value)
603 {
604 meta_entry_t *e;
606 if ((md == NULL) || (key == NULL) || (value == NULL))
607 return (-EINVAL);
609 pthread_mutex_lock (&md->lock);
611 e = md_entry_lookup (md, key);
612 if (e == NULL)
613 {
614 pthread_mutex_unlock (&md->lock);
615 return (-ENOENT);
616 }
618 if (e->type != MD_TYPE_BOOLEAN)
619 {
620 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
621 pthread_mutex_unlock (&md->lock);
622 return (-ENOENT);
623 }
625 *value = e->value.mv_boolean;
627 pthread_mutex_unlock (&md->lock);
628 return (0);
629 } /* }}} int meta_data_get_boolean */
631 /* vim: set sw=2 sts=2 et fdm=marker : */