1 /**
2 * collectd - src/meta_data.c
3 * Copyright (C) 2008-2011 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "meta_data.h"
26 #include <pthread.h>
28 /*
29 * Data types
30 */
31 union meta_value_u
32 {
33 char *mv_string;
34 int64_t mv_signed_int;
35 uint64_t mv_unsigned_int;
36 double mv_double;
37 _Bool mv_boolean;
38 };
39 typedef union meta_value_u meta_value_t;
41 struct meta_entry_s;
42 typedef struct meta_entry_s meta_entry_t;
43 struct meta_entry_s
44 {
45 char *key;
46 meta_value_t value;
47 int type;
48 meta_entry_t *next;
49 };
51 struct meta_data_s
52 {
53 meta_entry_t *head;
54 pthread_mutex_t lock;
55 };
57 /*
58 * Private functions
59 */
60 static char *md_strdup (const char *orig) /* {{{ */
61 {
62 size_t sz;
63 char *dest;
65 if (orig == NULL)
66 return (NULL);
68 sz = strlen (orig) + 1;
69 dest = (char *) malloc (sz);
70 if (dest == NULL)
71 return (NULL);
73 memcpy (dest, orig, sz);
75 return (dest);
76 } /* }}} char *md_strdup */
78 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
79 {
80 meta_entry_t *e;
82 e = (meta_entry_t *) malloc (sizeof (*e));
83 if (e == NULL)
84 {
85 ERROR ("md_entry_alloc: malloc failed.");
86 return (NULL);
87 }
88 memset (e, 0, sizeof (*e));
90 e->key = md_strdup (key);
91 if (e->key == NULL)
92 {
93 free (e);
94 ERROR ("md_entry_alloc: md_strdup failed.");
95 return (NULL);
96 }
98 e->type = 0;
99 e->next = NULL;
101 return (e);
102 } /* }}} meta_entry_t *md_entry_alloc */
104 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
105 {
106 meta_entry_t *copy;
108 if (orig == NULL)
109 return (NULL);
111 copy = md_entry_alloc (orig->key);
112 if (copy == NULL)
113 return (NULL);
114 copy->type = orig->type;
115 if (copy->type == MD_TYPE_STRING)
116 copy->value.mv_string = strdup (orig->value.mv_string);
117 else
118 copy->value = orig->value;
120 copy->next = md_entry_clone (orig->next);
121 return (copy);
122 } /* }}} meta_entry_t *md_entry_clone */
124 static void md_entry_free (meta_entry_t *e) /* {{{ */
125 {
126 if (e == NULL)
127 return;
129 free (e->key);
131 if (e->type == MD_TYPE_STRING)
132 free (e->value.mv_string);
134 if (e->next != NULL)
135 md_entry_free (e->next);
137 free (e);
138 } /* }}} void md_entry_free */
140 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
141 {
142 meta_entry_t *this;
143 meta_entry_t *prev;
145 if ((md == NULL) || (e == NULL))
146 return (-EINVAL);
148 pthread_mutex_lock (&md->lock);
150 prev = NULL;
151 this = md->head;
152 while (this != NULL)
153 {
154 if (strcasecmp (e->key, this->key) == 0)
155 break;
157 prev = this;
158 this = this->next;
159 }
161 if (this == NULL)
162 {
163 /* This key does not exist yet. */
164 if (md->head == NULL)
165 md->head = e;
166 else
167 {
168 assert (prev != NULL);
169 prev->next = e;
170 }
172 e->next = NULL;
173 }
174 else /* (this != NULL) */
175 {
176 if (prev == NULL)
177 md->head = e;
178 else
179 prev->next = e;
181 e->next = this->next;
182 }
184 pthread_mutex_unlock (&md->lock);
186 if (this != NULL)
187 {
188 this->next = NULL;
189 md_entry_free (this);
190 }
192 return (0);
193 } /* }}} int md_entry_insert */
195 /* XXX: The lock on md must be held while calling this function! */
196 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
197 const char *key)
198 {
199 meta_entry_t *e;
201 if ((md == NULL) || (key == NULL))
202 return (NULL);
204 for (e = md->head; e != NULL; e = e->next)
205 if (strcasecmp (key, e->key) == 0)
206 break;
208 return (e);
209 } /* }}} meta_entry_t *md_entry_lookup */
211 /*
212 * Public functions
213 */
214 meta_data_t *meta_data_create (void) /* {{{ */
215 {
216 meta_data_t *md;
218 md = (meta_data_t *) malloc (sizeof (*md));
219 if (md == NULL)
220 {
221 ERROR ("meta_data_create: malloc failed.");
222 return (NULL);
223 }
224 memset (md, 0, sizeof (*md));
226 md->head = NULL;
227 pthread_mutex_init (&md->lock, /* attr = */ NULL);
229 return (md);
230 } /* }}} meta_data_t *meta_data_create */
232 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
233 {
234 meta_data_t *copy;
236 if (orig == NULL)
237 return (NULL);
239 copy = meta_data_create ();
240 if (copy == NULL)
241 return (NULL);
243 pthread_mutex_lock (&orig->lock);
244 copy->head = md_entry_clone (orig->head);
245 pthread_mutex_unlock (&orig->lock);
247 return (copy);
248 } /* }}} meta_data_t *meta_data_clone */
250 void meta_data_destroy (meta_data_t *md) /* {{{ */
251 {
252 if (md == NULL)
253 return;
255 md_entry_free (md->head);
256 pthread_mutex_destroy (&md->lock);
257 free (md);
258 } /* }}} void meta_data_destroy */
260 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
261 {
262 meta_entry_t *e;
264 if ((md == NULL) || (key == NULL))
265 return (-EINVAL);
267 pthread_mutex_lock (&md->lock);
269 for (e = md->head; e != NULL; e = e->next)
270 {
271 if (strcasecmp (key, e->key) == 0)
272 {
273 pthread_mutex_unlock (&md->lock);
274 return (1);
275 }
276 }
278 pthread_mutex_unlock (&md->lock);
279 return (0);
280 } /* }}} int meta_data_exists */
282 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
283 {
284 meta_entry_t *e;
286 if ((md == NULL) || (key == NULL))
287 return -EINVAL;
289 pthread_mutex_lock (&md->lock);
291 for (e = md->head; e != NULL; e = e->next)
292 {
293 if (strcasecmp (key, e->key) == 0)
294 {
295 pthread_mutex_unlock (&md->lock);
296 return e->type;
297 }
298 }
300 pthread_mutex_unlock (&md->lock);
301 return 0;
302 } /* }}} int meta_data_type */
304 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
305 {
306 int i = 0, count = 0;
307 meta_entry_t *e;
309 if ((md == NULL) || (toc == NULL))
310 return -EINVAL;
312 pthread_mutex_lock (&md->lock);
314 for (e = md->head; e != NULL; e = e->next)
315 ++count;
317 if (count == 0)
318 {
319 pthread_mutex_unlock (&md->lock);
320 return (count);
321 }
323 *toc = calloc(count, sizeof(**toc));
324 for (e = md->head; e != NULL; e = e->next)
325 (*toc)[i++] = strdup(e->key);
327 pthread_mutex_unlock (&md->lock);
328 return count;
329 } /* }}} int meta_data_toc */
331 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
332 {
333 meta_entry_t *this;
334 meta_entry_t *prev;
336 if ((md == NULL) || (key == NULL))
337 return (-EINVAL);
339 pthread_mutex_lock (&md->lock);
341 prev = NULL;
342 this = md->head;
343 while (this != NULL)
344 {
345 if (strcasecmp (key, this->key) == 0)
346 break;
348 prev = this;
349 this = this->next;
350 }
352 if (this == NULL)
353 {
354 pthread_mutex_unlock (&md->lock);
355 return (-ENOENT);
356 }
358 if (prev == NULL)
359 md->head = this->next;
360 else
361 prev->next = this->next;
363 pthread_mutex_unlock (&md->lock);
365 this->next = NULL;
366 md_entry_free (this);
368 return (0);
369 } /* }}} int meta_data_delete */
371 /*
372 * Add functions
373 */
374 int meta_data_add_string (meta_data_t *md, /* {{{ */
375 const char *key, const char *value)
376 {
377 meta_entry_t *e;
379 if ((md == NULL) || (key == NULL) || (value == NULL))
380 return (-EINVAL);
382 e = md_entry_alloc (key);
383 if (e == NULL)
384 return (-ENOMEM);
386 e->value.mv_string = md_strdup (value);
387 if (e->value.mv_string == NULL)
388 {
389 ERROR ("meta_data_add_string: md_strdup failed.");
390 md_entry_free (e);
391 return (-ENOMEM);
392 }
393 e->type = MD_TYPE_STRING;
395 return (md_entry_insert (md, e));
396 } /* }}} int meta_data_add_string */
398 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
399 const char *key, int64_t value)
400 {
401 meta_entry_t *e;
403 if ((md == NULL) || (key == NULL))
404 return (-EINVAL);
406 e = md_entry_alloc (key);
407 if (e == NULL)
408 return (-ENOMEM);
410 e->value.mv_signed_int = value;
411 e->type = MD_TYPE_SIGNED_INT;
413 return (md_entry_insert (md, e));
414 } /* }}} int meta_data_add_signed_int */
416 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
417 const char *key, uint64_t value)
418 {
419 meta_entry_t *e;
421 if ((md == NULL) || (key == NULL))
422 return (-EINVAL);
424 e = md_entry_alloc (key);
425 if (e == NULL)
426 return (-ENOMEM);
428 e->value.mv_unsigned_int = value;
429 e->type = MD_TYPE_UNSIGNED_INT;
431 return (md_entry_insert (md, e));
432 } /* }}} int meta_data_add_unsigned_int */
434 int meta_data_add_double (meta_data_t *md, /* {{{ */
435 const char *key, double value)
436 {
437 meta_entry_t *e;
439 if ((md == NULL) || (key == NULL))
440 return (-EINVAL);
442 e = md_entry_alloc (key);
443 if (e == NULL)
444 return (-ENOMEM);
446 e->value.mv_double = value;
447 e->type = MD_TYPE_DOUBLE;
449 return (md_entry_insert (md, e));
450 } /* }}} int meta_data_add_double */
452 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
453 const char *key, _Bool value)
454 {
455 meta_entry_t *e;
457 if ((md == NULL) || (key == NULL))
458 return (-EINVAL);
460 e = md_entry_alloc (key);
461 if (e == NULL)
462 return (-ENOMEM);
464 e->value.mv_boolean = value;
465 e->type = MD_TYPE_BOOLEAN;
467 return (md_entry_insert (md, e));
468 } /* }}} int meta_data_add_boolean */
470 /*
471 * Get functions
472 */
473 int meta_data_get_string (meta_data_t *md, /* {{{ */
474 const char *key, char **value)
475 {
476 meta_entry_t *e;
477 char *temp;
479 if ((md == NULL) || (key == NULL) || (value == NULL))
480 return (-EINVAL);
482 pthread_mutex_lock (&md->lock);
484 e = md_entry_lookup (md, key);
485 if (e == NULL)
486 {
487 pthread_mutex_unlock (&md->lock);
488 return (-ENOENT);
489 }
491 if (e->type != MD_TYPE_STRING)
492 {
493 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
494 pthread_mutex_unlock (&md->lock);
495 return (-ENOENT);
496 }
498 temp = md_strdup (e->value.mv_string);
499 if (temp == NULL)
500 {
501 pthread_mutex_unlock (&md->lock);
502 ERROR ("meta_data_get_string: md_strdup failed.");
503 return (-ENOMEM);
504 }
506 pthread_mutex_unlock (&md->lock);
508 *value = temp;
510 return (0);
511 } /* }}} int meta_data_get_string */
513 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
514 const char *key, int64_t *value)
515 {
516 meta_entry_t *e;
518 if ((md == NULL) || (key == NULL) || (value == NULL))
519 return (-EINVAL);
521 pthread_mutex_lock (&md->lock);
523 e = md_entry_lookup (md, key);
524 if (e == NULL)
525 {
526 pthread_mutex_unlock (&md->lock);
527 return (-ENOENT);
528 }
530 if (e->type != MD_TYPE_SIGNED_INT)
531 {
532 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
533 pthread_mutex_unlock (&md->lock);
534 return (-ENOENT);
535 }
537 *value = e->value.mv_signed_int;
539 pthread_mutex_unlock (&md->lock);
540 return (0);
541 } /* }}} int meta_data_get_signed_int */
543 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
544 const char *key, uint64_t *value)
545 {
546 meta_entry_t *e;
548 if ((md == NULL) || (key == NULL) || (value == NULL))
549 return (-EINVAL);
551 pthread_mutex_lock (&md->lock);
553 e = md_entry_lookup (md, key);
554 if (e == NULL)
555 {
556 pthread_mutex_unlock (&md->lock);
557 return (-ENOENT);
558 }
560 if (e->type != MD_TYPE_UNSIGNED_INT)
561 {
562 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
563 pthread_mutex_unlock (&md->lock);
564 return (-ENOENT);
565 }
567 *value = e->value.mv_unsigned_int;
569 pthread_mutex_unlock (&md->lock);
570 return (0);
571 } /* }}} int meta_data_get_unsigned_int */
573 int meta_data_get_double (meta_data_t *md, /* {{{ */
574 const char *key, double *value)
575 {
576 meta_entry_t *e;
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_DOUBLE)
591 {
592 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
593 pthread_mutex_unlock (&md->lock);
594 return (-ENOENT);
595 }
597 *value = e->value.mv_double;
599 pthread_mutex_unlock (&md->lock);
600 return (0);
601 } /* }}} int meta_data_get_double */
603 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
604 const char *key, _Bool *value)
605 {
606 meta_entry_t *e;
608 if ((md == NULL) || (key == NULL) || (value == NULL))
609 return (-EINVAL);
611 pthread_mutex_lock (&md->lock);
613 e = md_entry_lookup (md, key);
614 if (e == NULL)
615 {
616 pthread_mutex_unlock (&md->lock);
617 return (-ENOENT);
618 }
620 if (e->type != MD_TYPE_BOOLEAN)
621 {
622 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
623 pthread_mutex_unlock (&md->lock);
624 return (-ENOENT);
625 }
627 *value = e->value.mv_boolean;
629 pthread_mutex_unlock (&md->lock);
630 return (0);
631 } /* }}} int meta_data_get_boolean */
633 /* vim: set sw=2 sts=2 et fdm=marker : */