1 /**
2 * collectd - src/meta_data.c
3 * Copyright (C) 2008,2009 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 void md_entry_free (meta_entry_t *e) /* {{{ */
105 {
106 if (e == NULL)
107 return;
109 free (e->key);
111 if (e->type == MD_TYPE_STRING)
112 free (e->value.mv_string);
114 if (e->next != NULL)
115 md_entry_free (e->next);
117 free (e);
118 } /* }}} void md_entry_free */
120 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
121 {
122 meta_entry_t *this;
123 meta_entry_t *prev;
125 if ((md == NULL) || (e == NULL))
126 return (-EINVAL);
128 pthread_mutex_lock (&md->lock);
130 prev = NULL;
131 this = md->head;
132 while (this != NULL)
133 {
134 if (strcasecmp (e->key, this->key) == 0)
135 break;
137 prev = this;
138 this = this->next;
139 }
141 if (this == NULL)
142 {
143 /* This key does not exist yet. */
144 if (md->head == NULL)
145 md->head = e;
146 else
147 {
148 assert (prev != NULL);
149 prev->next = e;
150 }
152 e->next = NULL;
153 }
154 else /* (this != NULL) */
155 {
156 if (prev == NULL)
157 md->head = e;
158 else
159 prev->next = e;
161 e->next = this->next;
162 }
164 pthread_mutex_unlock (&md->lock);
166 if (this != NULL)
167 {
168 this->next = NULL;
169 md_entry_free (this);
170 }
172 return (0);
173 } /* }}} int md_entry_insert */
175 /* XXX: The lock on md must be held while calling this function! */
176 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
177 const char *key)
178 {
179 meta_entry_t *e;
181 if ((md == NULL) || (key == NULL))
182 return (NULL);
184 for (e = md->head; e != NULL; e = e->next)
185 if (strcasecmp (key, e->key) == 0)
186 break;
188 return (e);
189 } /* }}} meta_entry_t *md_entry_lookup */
191 /*
192 * Public functions
193 */
194 meta_data_t *meta_data_create (void) /* {{{ */
195 {
196 meta_data_t *md;
198 md = (meta_data_t *) malloc (sizeof (*md));
199 if (md == NULL)
200 {
201 ERROR ("meta_data_create: malloc failed.");
202 return (NULL);
203 }
204 memset (md, 0, sizeof (*md));
206 md->head = NULL;
207 pthread_mutex_init (&md->lock, /* attr = */ NULL);
209 return (md);
210 } /* }}} meta_data_t *meta_data_create */
212 void meta_data_destroy (meta_data_t *md) /* {{{ */
213 {
214 if (md == NULL)
215 return;
217 md_entry_free (md->head);
218 free (md);
219 } /* }}} void meta_data_destroy */
221 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
222 {
223 meta_entry_t *e;
225 if ((md == NULL) || (key == NULL))
226 return (-EINVAL);
228 pthread_mutex_lock (&md->lock);
230 for (e = md->head; e != NULL; e = e->next)
231 {
232 if (strcasecmp (key, e->key) == 0)
233 {
234 pthread_mutex_unlock (&md->lock);
235 return (1);
236 }
237 }
239 pthread_mutex_unlock (&md->lock);
240 return (0);
241 } /* }}} int meta_data_exists */
243 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
244 {
245 meta_entry_t *e;
247 if ((md == NULL) || (key == NULL))
248 return -EINVAL;
250 pthread_mutex_lock (&md->lock);
252 for (e = md->head; e != NULL; e = e->next)
253 {
254 if (strcasecmp (key, e->key) == 0)
255 {
256 pthread_mutex_unlock (&md->lock);
257 return e->type;
258 }
259 }
261 pthread_mutex_unlock (&md->lock);
262 return 0;
263 } /* }}} int meta_data_type */
265 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
266 {
267 int i = 0, count = 0;
268 meta_entry_t *e;
270 if ((md == NULL) || (toc == NULL))
271 return -EINVAL;
273 pthread_mutex_lock (&md->lock);
275 for (e = md->head; e != NULL; e = e->next)
276 ++count;
278 *toc = malloc(count * sizeof(**toc));
279 for (e = md->head; e != NULL; e = e->next)
280 (*toc)[i++] = strdup(e->key);
282 pthread_mutex_unlock (&md->lock);
283 return count;
284 } /* }}} int meta_data_toc */
286 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
287 {
288 meta_entry_t *this;
289 meta_entry_t *prev;
291 if ((md == NULL) || (key == NULL))
292 return (-EINVAL);
294 pthread_mutex_lock (&md->lock);
296 prev = NULL;
297 this = md->head;
298 while (this != NULL)
299 {
300 if (strcasecmp (key, this->key) == 0)
301 break;
303 prev = this;
304 this = this->next;
305 }
307 if (this == NULL)
308 {
309 pthread_mutex_unlock (&md->lock);
310 return (-ENOENT);
311 }
313 if (prev == NULL)
314 md->head = this->next;
315 else
316 prev->next = this->next;
318 pthread_mutex_unlock (&md->lock);
320 this->next = NULL;
321 md_entry_free (this);
323 return (0);
324 } /* }}} int meta_data_delete */
326 /*
327 * Add functions
328 */
329 int meta_data_add_string (meta_data_t *md, /* {{{ */
330 const char *key, const char *value)
331 {
332 meta_entry_t *e;
334 if ((md == NULL) || (key == NULL) || (value == NULL))
335 return (-EINVAL);
337 e = md_entry_alloc (key);
338 if (e == NULL)
339 return (-ENOMEM);
341 e->value.mv_string = md_strdup (value);
342 if (e->value.mv_string == NULL)
343 {
344 ERROR ("meta_data_add_string: md_strdup failed.");
345 md_entry_free (e);
346 return (-ENOMEM);
347 }
348 e->type = MD_TYPE_STRING;
350 return (md_entry_insert (md, e));
351 } /* }}} int meta_data_add_string */
353 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
354 const char *key, int64_t value)
355 {
356 meta_entry_t *e;
358 if ((md == NULL) || (key == NULL))
359 return (-EINVAL);
361 e = md_entry_alloc (key);
362 if (e == NULL)
363 return (-ENOMEM);
365 e->value.mv_signed_int = value;
366 e->type = MD_TYPE_SIGNED_INT;
368 return (md_entry_insert (md, e));
369 } /* }}} int meta_data_add_signed_int */
371 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
372 const char *key, uint64_t value)
373 {
374 meta_entry_t *e;
376 if ((md == NULL) || (key == NULL))
377 return (-EINVAL);
379 e = md_entry_alloc (key);
380 if (e == NULL)
381 return (-ENOMEM);
383 e->value.mv_unsigned_int = value;
384 e->type = MD_TYPE_UNSIGNED_INT;
386 return (md_entry_insert (md, e));
387 } /* }}} int meta_data_add_unsigned_int */
389 int meta_data_add_double (meta_data_t *md, /* {{{ */
390 const char *key, double value)
391 {
392 meta_entry_t *e;
394 if ((md == NULL) || (key == NULL))
395 return (-EINVAL);
397 e = md_entry_alloc (key);
398 if (e == NULL)
399 return (-ENOMEM);
401 e->value.mv_double = value;
402 e->type = MD_TYPE_DOUBLE;
404 return (md_entry_insert (md, e));
405 } /* }}} int meta_data_add_double */
407 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
408 const char *key, _Bool value)
409 {
410 meta_entry_t *e;
412 if ((md == NULL) || (key == NULL))
413 return (-EINVAL);
415 e = md_entry_alloc (key);
416 if (e == NULL)
417 return (-ENOMEM);
419 e->value.mv_boolean = value;
420 e->type = MD_TYPE_BOOLEAN;
422 return (md_entry_insert (md, e));
423 } /* }}} int meta_data_add_boolean */
425 /*
426 * Get functions
427 */
428 int meta_data_get_string (meta_data_t *md, /* {{{ */
429 const char *key, char **value)
430 {
431 meta_entry_t *e;
432 char *temp;
434 if ((md == NULL) || (key == NULL) || (value == NULL))
435 return (-EINVAL);
437 pthread_mutex_lock (&md->lock);
439 e = md_entry_lookup (md, key);
440 if (e == NULL)
441 {
442 pthread_mutex_unlock (&md->lock);
443 return (-ENOENT);
444 }
446 if (e->type != MD_TYPE_STRING)
447 {
448 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
449 pthread_mutex_unlock (&md->lock);
450 return (-ENOENT);
451 }
453 temp = md_strdup (e->value.mv_string);
454 if (temp == NULL)
455 {
456 pthread_mutex_unlock (&md->lock);
457 ERROR ("meta_data_get_string: md_strdup failed.");
458 return (-ENOMEM);
459 }
461 pthread_mutex_unlock (&md->lock);
463 *value = temp;
465 return (0);
466 } /* }}} int meta_data_get_string */
468 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
469 const char *key, int64_t *value)
470 {
471 meta_entry_t *e;
473 if ((md == NULL) || (key == NULL) || (value == NULL))
474 return (-EINVAL);
476 pthread_mutex_lock (&md->lock);
478 e = md_entry_lookup (md, key);
479 if (e == NULL)
480 {
481 pthread_mutex_unlock (&md->lock);
482 return (-ENOENT);
483 }
485 if (e->type != MD_TYPE_SIGNED_INT)
486 {
487 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
488 pthread_mutex_unlock (&md->lock);
489 return (-ENOENT);
490 }
492 *value = e->value.mv_signed_int;
494 pthread_mutex_unlock (&md->lock);
495 return (0);
496 } /* }}} int meta_data_get_signed_int */
498 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
499 const char *key, uint64_t *value)
500 {
501 meta_entry_t *e;
503 if ((md == NULL) || (key == NULL) || (value == NULL))
504 return (-EINVAL);
506 pthread_mutex_lock (&md->lock);
508 e = md_entry_lookup (md, key);
509 if (e == NULL)
510 {
511 pthread_mutex_unlock (&md->lock);
512 return (-ENOENT);
513 }
515 if (e->type != MD_TYPE_UNSIGNED_INT)
516 {
517 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
518 pthread_mutex_unlock (&md->lock);
519 return (-ENOENT);
520 }
522 *value = e->value.mv_unsigned_int;
524 pthread_mutex_unlock (&md->lock);
525 return (0);
526 } /* }}} int meta_data_get_unsigned_int */
528 int meta_data_get_double (meta_data_t *md, /* {{{ */
529 const char *key, double *value)
530 {
531 meta_entry_t *e;
533 if ((md == NULL) || (key == NULL) || (value == NULL))
534 return (-EINVAL);
536 pthread_mutex_lock (&md->lock);
538 e = md_entry_lookup (md, key);
539 if (e == NULL)
540 {
541 pthread_mutex_unlock (&md->lock);
542 return (-ENOENT);
543 }
545 if (e->type != MD_TYPE_DOUBLE)
546 {
547 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
548 pthread_mutex_unlock (&md->lock);
549 return (-ENOENT);
550 }
552 *value = e->value.mv_double;
554 pthread_mutex_unlock (&md->lock);
555 return (0);
556 } /* }}} int meta_data_get_double */
558 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
559 const char *key, _Bool *value)
560 {
561 meta_entry_t *e;
563 if ((md == NULL) || (key == NULL) || (value == NULL))
564 return (-EINVAL);
566 pthread_mutex_lock (&md->lock);
568 e = md_entry_lookup (md, key);
569 if (e == NULL)
570 {
571 pthread_mutex_unlock (&md->lock);
572 return (-ENOENT);
573 }
575 if (e->type != MD_TYPE_BOOLEAN)
576 {
577 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
578 pthread_mutex_unlock (&md->lock);
579 return (-ENOENT);
580 }
582 *value = e->value.mv_boolean;
584 pthread_mutex_unlock (&md->lock);
585 return (0);
586 } /* }}} int meta_data_get_boolean */
588 /* vim: set sw=2 sts=2 et fdm=marker : */