1b5873521c77d1fc7243faa9b6ccf2719674b7b4
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 copy->type = orig->type;
113 if (copy->type == MD_TYPE_STRING)
114 copy->value.mv_string = strdup (orig->value.mv_string);
115 else
116 copy->value = orig->value;
118 copy->next = md_entry_clone (orig->next);
119 return (copy);
120 } /* }}} meta_entry_t *md_entry_clone */
122 static void md_entry_free (meta_entry_t *e) /* {{{ */
123 {
124 if (e == NULL)
125 return;
127 free (e->key);
129 if (e->type == MD_TYPE_STRING)
130 free (e->value.mv_string);
132 if (e->next != NULL)
133 md_entry_free (e->next);
135 free (e);
136 } /* }}} void md_entry_free */
138 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
139 {
140 meta_entry_t *this;
141 meta_entry_t *prev;
143 if ((md == NULL) || (e == NULL))
144 return (-EINVAL);
146 pthread_mutex_lock (&md->lock);
148 prev = NULL;
149 this = md->head;
150 while (this != NULL)
151 {
152 if (strcasecmp (e->key, this->key) == 0)
153 break;
155 prev = this;
156 this = this->next;
157 }
159 if (this == NULL)
160 {
161 /* This key does not exist yet. */
162 if (md->head == NULL)
163 md->head = e;
164 else
165 {
166 assert (prev != NULL);
167 prev->next = e;
168 }
170 e->next = NULL;
171 }
172 else /* (this != NULL) */
173 {
174 if (prev == NULL)
175 md->head = e;
176 else
177 prev->next = e;
179 e->next = this->next;
180 }
182 pthread_mutex_unlock (&md->lock);
184 if (this != NULL)
185 {
186 this->next = NULL;
187 md_entry_free (this);
188 }
190 return (0);
191 } /* }}} int md_entry_insert */
193 /* XXX: The lock on md must be held while calling this function! */
194 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
195 const char *key)
196 {
197 meta_entry_t *e;
199 if ((md == NULL) || (key == NULL))
200 return (NULL);
202 for (e = md->head; e != NULL; e = e->next)
203 if (strcasecmp (key, e->key) == 0)
204 break;
206 return (e);
207 } /* }}} meta_entry_t *md_entry_lookup */
209 /*
210 * Public functions
211 */
212 meta_data_t *meta_data_create (void) /* {{{ */
213 {
214 meta_data_t *md;
216 md = (meta_data_t *) malloc (sizeof (*md));
217 if (md == NULL)
218 {
219 ERROR ("meta_data_create: malloc failed.");
220 return (NULL);
221 }
222 memset (md, 0, sizeof (*md));
224 md->head = NULL;
225 pthread_mutex_init (&md->lock, /* attr = */ NULL);
227 return (md);
228 } /* }}} meta_data_t *meta_data_create */
230 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
231 {
232 meta_data_t *copy;
234 if (orig == NULL)
235 return (NULL);
237 copy = meta_data_create ();
238 if (copy == NULL)
239 return (NULL);
241 pthread_mutex_lock (&orig->lock);
242 copy->head = md_entry_clone (orig->head);
243 pthread_mutex_unlock (&orig->lock);
245 return (copy);
246 } /* }}} meta_data_t *meta_data_clone */
248 void meta_data_destroy (meta_data_t *md) /* {{{ */
249 {
250 if (md == NULL)
251 return;
253 pthread_mutex_destroy(&md->lock);
254 md_entry_free (md->head);
255 pthread_mutex_destroy (&md->lock);
256 free (md);
257 } /* }}} void meta_data_destroy */
259 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
260 {
261 meta_entry_t *e;
263 if ((md == NULL) || (key == NULL))
264 return (-EINVAL);
266 pthread_mutex_lock (&md->lock);
268 for (e = md->head; e != NULL; e = e->next)
269 {
270 if (strcasecmp (key, e->key) == 0)
271 {
272 pthread_mutex_unlock (&md->lock);
273 return (1);
274 }
275 }
277 pthread_mutex_unlock (&md->lock);
278 return (0);
279 } /* }}} int meta_data_exists */
281 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
282 {
283 meta_entry_t *e;
285 if ((md == NULL) || (key == NULL))
286 return -EINVAL;
288 pthread_mutex_lock (&md->lock);
290 for (e = md->head; e != NULL; e = e->next)
291 {
292 if (strcasecmp (key, e->key) == 0)
293 {
294 pthread_mutex_unlock (&md->lock);
295 return e->type;
296 }
297 }
299 pthread_mutex_unlock (&md->lock);
300 return 0;
301 } /* }}} int meta_data_type */
303 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
304 {
305 int i = 0, count = 0;
306 meta_entry_t *e;
308 if ((md == NULL) || (toc == NULL))
309 return -EINVAL;
311 pthread_mutex_lock (&md->lock);
313 for (e = md->head; e != NULL; e = e->next)
314 ++count;
316 if (count == 0)
317 {
318 pthread_mutex_unlock (&md->lock);
319 return (count);
320 }
322 *toc = calloc(count, sizeof(**toc));
323 for (e = md->head; e != NULL; e = e->next)
324 (*toc)[i++] = strdup(e->key);
326 pthread_mutex_unlock (&md->lock);
327 return count;
328 } /* }}} int meta_data_toc */
330 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
331 {
332 meta_entry_t *this;
333 meta_entry_t *prev;
335 if ((md == NULL) || (key == NULL))
336 return (-EINVAL);
338 pthread_mutex_lock (&md->lock);
340 prev = NULL;
341 this = md->head;
342 while (this != NULL)
343 {
344 if (strcasecmp (key, this->key) == 0)
345 break;
347 prev = this;
348 this = this->next;
349 }
351 if (this == NULL)
352 {
353 pthread_mutex_unlock (&md->lock);
354 return (-ENOENT);
355 }
357 if (prev == NULL)
358 md->head = this->next;
359 else
360 prev->next = this->next;
362 pthread_mutex_unlock (&md->lock);
364 this->next = NULL;
365 md_entry_free (this);
367 return (0);
368 } /* }}} int meta_data_delete */
370 /*
371 * Add functions
372 */
373 int meta_data_add_string (meta_data_t *md, /* {{{ */
374 const char *key, const char *value)
375 {
376 meta_entry_t *e;
378 if ((md == NULL) || (key == NULL) || (value == NULL))
379 return (-EINVAL);
381 e = md_entry_alloc (key);
382 if (e == NULL)
383 return (-ENOMEM);
385 e->value.mv_string = md_strdup (value);
386 if (e->value.mv_string == NULL)
387 {
388 ERROR ("meta_data_add_string: md_strdup failed.");
389 md_entry_free (e);
390 return (-ENOMEM);
391 }
392 e->type = MD_TYPE_STRING;
394 return (md_entry_insert (md, e));
395 } /* }}} int meta_data_add_string */
397 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
398 const char *key, int64_t value)
399 {
400 meta_entry_t *e;
402 if ((md == NULL) || (key == NULL))
403 return (-EINVAL);
405 e = md_entry_alloc (key);
406 if (e == NULL)
407 return (-ENOMEM);
409 e->value.mv_signed_int = value;
410 e->type = MD_TYPE_SIGNED_INT;
412 return (md_entry_insert (md, e));
413 } /* }}} int meta_data_add_signed_int */
415 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
416 const char *key, uint64_t value)
417 {
418 meta_entry_t *e;
420 if ((md == NULL) || (key == NULL))
421 return (-EINVAL);
423 e = md_entry_alloc (key);
424 if (e == NULL)
425 return (-ENOMEM);
427 e->value.mv_unsigned_int = value;
428 e->type = MD_TYPE_UNSIGNED_INT;
430 return (md_entry_insert (md, e));
431 } /* }}} int meta_data_add_unsigned_int */
433 int meta_data_add_double (meta_data_t *md, /* {{{ */
434 const char *key, double value)
435 {
436 meta_entry_t *e;
438 if ((md == NULL) || (key == NULL))
439 return (-EINVAL);
441 e = md_entry_alloc (key);
442 if (e == NULL)
443 return (-ENOMEM);
445 e->value.mv_double = value;
446 e->type = MD_TYPE_DOUBLE;
448 return (md_entry_insert (md, e));
449 } /* }}} int meta_data_add_double */
451 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
452 const char *key, _Bool value)
453 {
454 meta_entry_t *e;
456 if ((md == NULL) || (key == NULL))
457 return (-EINVAL);
459 e = md_entry_alloc (key);
460 if (e == NULL)
461 return (-ENOMEM);
463 e->value.mv_boolean = value;
464 e->type = MD_TYPE_BOOLEAN;
466 return (md_entry_insert (md, e));
467 } /* }}} int meta_data_add_boolean */
469 /*
470 * Get functions
471 */
472 int meta_data_get_string (meta_data_t *md, /* {{{ */
473 const char *key, char **value)
474 {
475 meta_entry_t *e;
476 char *temp;
478 if ((md == NULL) || (key == NULL) || (value == NULL))
479 return (-EINVAL);
481 pthread_mutex_lock (&md->lock);
483 e = md_entry_lookup (md, key);
484 if (e == NULL)
485 {
486 pthread_mutex_unlock (&md->lock);
487 return (-ENOENT);
488 }
490 if (e->type != MD_TYPE_STRING)
491 {
492 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
493 pthread_mutex_unlock (&md->lock);
494 return (-ENOENT);
495 }
497 temp = md_strdup (e->value.mv_string);
498 if (temp == NULL)
499 {
500 pthread_mutex_unlock (&md->lock);
501 ERROR ("meta_data_get_string: md_strdup failed.");
502 return (-ENOMEM);
503 }
505 pthread_mutex_unlock (&md->lock);
507 *value = temp;
509 return (0);
510 } /* }}} int meta_data_get_string */
512 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
513 const char *key, int64_t *value)
514 {
515 meta_entry_t *e;
517 if ((md == NULL) || (key == NULL) || (value == NULL))
518 return (-EINVAL);
520 pthread_mutex_lock (&md->lock);
522 e = md_entry_lookup (md, key);
523 if (e == NULL)
524 {
525 pthread_mutex_unlock (&md->lock);
526 return (-ENOENT);
527 }
529 if (e->type != MD_TYPE_SIGNED_INT)
530 {
531 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
532 pthread_mutex_unlock (&md->lock);
533 return (-ENOENT);
534 }
536 *value = e->value.mv_signed_int;
538 pthread_mutex_unlock (&md->lock);
539 return (0);
540 } /* }}} int meta_data_get_signed_int */
542 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
543 const char *key, uint64_t *value)
544 {
545 meta_entry_t *e;
547 if ((md == NULL) || (key == NULL) || (value == NULL))
548 return (-EINVAL);
550 pthread_mutex_lock (&md->lock);
552 e = md_entry_lookup (md, key);
553 if (e == NULL)
554 {
555 pthread_mutex_unlock (&md->lock);
556 return (-ENOENT);
557 }
559 if (e->type != MD_TYPE_UNSIGNED_INT)
560 {
561 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
562 pthread_mutex_unlock (&md->lock);
563 return (-ENOENT);
564 }
566 *value = e->value.mv_unsigned_int;
568 pthread_mutex_unlock (&md->lock);
569 return (0);
570 } /* }}} int meta_data_get_unsigned_int */
572 int meta_data_get_double (meta_data_t *md, /* {{{ */
573 const char *key, double *value)
574 {
575 meta_entry_t *e;
577 if ((md == NULL) || (key == NULL) || (value == NULL))
578 return (-EINVAL);
580 pthread_mutex_lock (&md->lock);
582 e = md_entry_lookup (md, key);
583 if (e == NULL)
584 {
585 pthread_mutex_unlock (&md->lock);
586 return (-ENOENT);
587 }
589 if (e->type != MD_TYPE_DOUBLE)
590 {
591 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
592 pthread_mutex_unlock (&md->lock);
593 return (-ENOENT);
594 }
596 *value = e->value.mv_double;
598 pthread_mutex_unlock (&md->lock);
599 return (0);
600 } /* }}} int meta_data_get_double */
602 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
603 const char *key, _Bool *value)
604 {
605 meta_entry_t *e;
607 if ((md == NULL) || (key == NULL) || (value == NULL))
608 return (-EINVAL);
610 pthread_mutex_lock (&md->lock);
612 e = md_entry_lookup (md, key);
613 if (e == NULL)
614 {
615 pthread_mutex_unlock (&md->lock);
616 return (-ENOENT);
617 }
619 if (e->type != MD_TYPE_BOOLEAN)
620 {
621 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
622 pthread_mutex_unlock (&md->lock);
623 return (-ENOENT);
624 }
626 *value = e->value.mv_boolean;
628 pthread_mutex_unlock (&md->lock);
629 return (0);
630 } /* }}} int meta_data_get_boolean */
632 /* vim: set sw=2 sts=2 et fdm=marker : */