a7c11c10607352d9cbf0a938a185a54895bfd970
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 0;
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_delete (meta_data_t *md, const char *key) /* {{{ */
266 {
267 meta_entry_t *this;
268 meta_entry_t *prev;
270 if ((md == NULL) || (key == NULL))
271 return (-EINVAL);
273 pthread_mutex_lock (&md->lock);
275 prev = NULL;
276 this = md->head;
277 while (this != NULL)
278 {
279 if (strcasecmp (key, this->key) == 0)
280 break;
282 prev = this;
283 this = this->next;
284 }
286 if (this == NULL)
287 {
288 pthread_mutex_unlock (&md->lock);
289 return (-ENOENT);
290 }
292 if (prev == NULL)
293 md->head = this->next;
294 else
295 prev->next = this->next;
297 pthread_mutex_unlock (&md->lock);
299 this->next = NULL;
300 md_entry_free (this);
302 return (0);
303 } /* }}} int meta_data_delete */
305 /*
306 * Add functions
307 */
308 int meta_data_add_string (meta_data_t *md, /* {{{ */
309 const char *key, const char *value)
310 {
311 meta_entry_t *e;
313 if ((md == NULL) || (key == NULL) || (value == NULL))
314 return (-EINVAL);
316 e = md_entry_alloc (key);
317 if (e == NULL)
318 return (-ENOMEM);
320 e->value.mv_string = md_strdup (value);
321 if (e->value.mv_string == NULL)
322 {
323 ERROR ("meta_data_add_string: md_strdup failed.");
324 md_entry_free (e);
325 return (-ENOMEM);
326 }
327 e->type = MD_TYPE_STRING;
329 return (md_entry_insert (md, e));
330 } /* }}} int meta_data_add_string */
332 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
333 const char *key, int64_t value)
334 {
335 meta_entry_t *e;
337 if ((md == NULL) || (key == NULL))
338 return (-EINVAL);
340 e = md_entry_alloc (key);
341 if (e == NULL)
342 return (-ENOMEM);
344 e->value.mv_signed_int = value;
345 e->type = MD_TYPE_SIGNED_INT;
347 return (md_entry_insert (md, e));
348 } /* }}} int meta_data_add_signed_int */
350 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
351 const char *key, uint64_t value)
352 {
353 meta_entry_t *e;
355 if ((md == NULL) || (key == NULL))
356 return (-EINVAL);
358 e = md_entry_alloc (key);
359 if (e == NULL)
360 return (-ENOMEM);
362 e->value.mv_unsigned_int = value;
363 e->type = MD_TYPE_UNSIGNED_INT;
365 return (md_entry_insert (md, e));
366 } /* }}} int meta_data_add_unsigned_int */
368 int meta_data_add_double (meta_data_t *md, /* {{{ */
369 const char *key, double value)
370 {
371 meta_entry_t *e;
373 if ((md == NULL) || (key == NULL))
374 return (-EINVAL);
376 e = md_entry_alloc (key);
377 if (e == NULL)
378 return (-ENOMEM);
380 e->value.mv_double = value;
381 e->type = MD_TYPE_DOUBLE;
383 return (md_entry_insert (md, e));
384 } /* }}} int meta_data_add_double */
386 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
387 const char *key, _Bool value)
388 {
389 meta_entry_t *e;
391 if ((md == NULL) || (key == NULL))
392 return (-EINVAL);
394 e = md_entry_alloc (key);
395 if (e == NULL)
396 return (-ENOMEM);
398 e->value.mv_boolean = value;
399 e->type = MD_TYPE_BOOLEAN;
401 return (md_entry_insert (md, e));
402 } /* }}} int meta_data_add_boolean */
404 /*
405 * Get functions
406 */
407 int meta_data_get_string (meta_data_t *md, /* {{{ */
408 const char *key, char **value)
409 {
410 meta_entry_t *e;
411 char *temp;
413 if ((md == NULL) || (key == NULL) || (value == NULL))
414 return (-EINVAL);
416 pthread_mutex_lock (&md->lock);
418 e = md_entry_lookup (md, key);
419 if (e == NULL)
420 {
421 pthread_mutex_unlock (&md->lock);
422 return (-ENOENT);
423 }
425 if (e->type != MD_TYPE_STRING)
426 {
427 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
428 pthread_mutex_unlock (&md->lock);
429 return (-ENOENT);
430 }
432 temp = md_strdup (e->value.mv_string);
433 if (temp == NULL)
434 {
435 pthread_mutex_unlock (&md->lock);
436 ERROR ("meta_data_get_string: md_strdup failed.");
437 return (-ENOMEM);
438 }
440 pthread_mutex_unlock (&md->lock);
442 *value = temp;
444 return (0);
445 } /* }}} int meta_data_get_string */
447 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
448 const char *key, int64_t *value)
449 {
450 meta_entry_t *e;
452 if ((md == NULL) || (key == NULL) || (value == NULL))
453 return (-EINVAL);
455 pthread_mutex_lock (&md->lock);
457 e = md_entry_lookup (md, key);
458 if (e == NULL)
459 {
460 pthread_mutex_unlock (&md->lock);
461 return (-ENOENT);
462 }
464 if (e->type != MD_TYPE_SIGNED_INT)
465 {
466 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
467 pthread_mutex_unlock (&md->lock);
468 return (-ENOENT);
469 }
471 *value = e->value.mv_signed_int;
473 pthread_mutex_unlock (&md->lock);
474 return (0);
475 } /* }}} int meta_data_get_signed_int */
477 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
478 const char *key, uint64_t *value)
479 {
480 meta_entry_t *e;
482 if ((md == NULL) || (key == NULL) || (value == NULL))
483 return (-EINVAL);
485 pthread_mutex_lock (&md->lock);
487 e = md_entry_lookup (md, key);
488 if (e == NULL)
489 {
490 pthread_mutex_unlock (&md->lock);
491 return (-ENOENT);
492 }
494 if (e->type != MD_TYPE_UNSIGNED_INT)
495 {
496 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
497 pthread_mutex_unlock (&md->lock);
498 return (-ENOENT);
499 }
501 *value = e->value.mv_unsigned_int;
503 pthread_mutex_unlock (&md->lock);
504 return (0);
505 } /* }}} int meta_data_get_unsigned_int */
507 int meta_data_get_double (meta_data_t *md, /* {{{ */
508 const char *key, double *value)
509 {
510 meta_entry_t *e;
512 if ((md == NULL) || (key == NULL) || (value == NULL))
513 return (-EINVAL);
515 pthread_mutex_lock (&md->lock);
517 e = md_entry_lookup (md, key);
518 if (e == NULL)
519 {
520 pthread_mutex_unlock (&md->lock);
521 return (-ENOENT);
522 }
524 if (e->type != MD_TYPE_DOUBLE)
525 {
526 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
527 pthread_mutex_unlock (&md->lock);
528 return (-ENOENT);
529 }
531 *value = e->value.mv_double;
533 pthread_mutex_unlock (&md->lock);
534 return (0);
535 } /* }}} int meta_data_get_double */
537 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
538 const char *key, _Bool *value)
539 {
540 meta_entry_t *e;
542 if ((md == NULL) || (key == NULL) || (value == NULL))
543 return (-EINVAL);
545 pthread_mutex_lock (&md->lock);
547 e = md_entry_lookup (md, key);
548 if (e == NULL)
549 {
550 pthread_mutex_unlock (&md->lock);
551 return (-ENOENT);
552 }
554 if (e->type != MD_TYPE_BOOLEAN)
555 {
556 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
557 pthread_mutex_unlock (&md->lock);
558 return (-ENOENT);
559 }
561 *value = e->value.mv_boolean;
563 pthread_mutex_unlock (&md->lock);
564 return (0);
565 } /* }}} int meta_data_get_boolean */
567 /* vim: set sw=2 sts=2 et fdm=marker : */