1 /**
2 * collectd - src/meta_data.c
3 * Copyright (C) 2008 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 /*
23 * First tell the compiler to stick to the C99 and POSIX standards as close as
24 * possible.
25 */
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
28 #endif
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
32 #endif
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
36 #endif
37 #define _POSIX_C_SOURCE 200112L
39 #if 0
40 /* Single UNIX needed for strdup. */
41 #ifdef _XOPEN_SOURCE
42 # undef _XOPEN_SOURCE
43 #endif
44 #define _XOPEN_SOURCE 500
45 #endif
47 #ifndef _REENTRANT
48 # define _REENTRANT
49 #endif
51 #ifndef _THREAD_SAFE
52 # define _THREAD_SAFE
53 #endif
55 #ifdef _GNU_SOURCE
56 # undef _GNU_SOURCE
57 #endif
58 /* }}} */
60 #include "collectd.h"
61 #include "plugin.h"
62 #include "meta_data.h"
64 #include <pthread.h>
66 /*
67 * Defines
68 */
69 #define MD_TYPE_STRING 1
70 #define MD_TYPE_SIGNED_INT 2
71 #define MD_TYPE_UNSIGNED_INT 3
72 #define MD_TYPE_DOUBLE 4
74 /*
75 * Data types
76 */
77 union meta_value_u
78 {
79 char *mv_string;
80 int64_t mv_signed_int;
81 uint64_t mv_unsigned_int;
82 double mv_double;
83 };
84 typedef union meta_value_u meta_value_t;
86 struct meta_entry_s;
87 typedef struct meta_entry_s meta_entry_t;
88 struct meta_entry_s
89 {
90 char *key;
91 meta_value_t value;
92 int type;
93 meta_entry_t *next;
94 };
96 struct meta_data_s
97 {
98 meta_entry_t *head;
99 pthread_mutex_t lock;
100 };
102 /*
103 * Private functions
104 */
105 static char *md_strdup (const char *orig) /* {{{ */
106 {
107 size_t sz;
108 char *dest;
110 if (orig == NULL)
111 return (NULL);
113 sz = strlen (orig) + 1;
114 dest = (char *) malloc (sz);
115 if (dest == NULL)
116 return (NULL);
118 memcpy (dest, orig, sz);
120 return (dest);
121 } /* }}} char *md_strdup */
123 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
124 {
125 meta_entry_t *e;
127 e = (meta_entry_t *) malloc (sizeof (*e));
128 if (e == NULL)
129 {
130 ERROR ("md_entry_alloc: malloc failed.");
131 return (NULL);
132 }
133 memset (e, 0, sizeof (*e));
135 e->key = md_strdup (key);
136 if (e->key == NULL)
137 {
138 free (e);
139 ERROR ("md_entry_alloc: md_strdup failed.");
140 return (NULL);
141 }
143 e->type = 0;
144 e->next = NULL;
146 return (e);
147 } /* }}} meta_entry_t *md_entry_alloc */
149 static void md_entry_free (meta_entry_t *e) /* {{{ */
150 {
151 if (e == NULL)
152 return;
154 free (e->key);
156 if (e->type == MD_TYPE_STRING)
157 free (e->value.mv_string);
159 if (e->next != NULL)
160 md_entry_free (e->next);
162 free (e);
163 } /* }}} void md_entry_free */
165 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
166 {
167 meta_entry_t *this;
168 meta_entry_t *prev;
170 if ((md == NULL) || (e == NULL))
171 return (-EINVAL);
173 pthread_mutex_lock (&md->lock);
175 prev = NULL;
176 this = md->head;
177 while (this != NULL)
178 {
179 if (strcasecmp (e->key, this->key) == 0)
180 break;
182 prev = this;
183 this = this->next;
184 }
186 if (this == NULL)
187 {
188 /* This key does not exist yet. */
189 if (md->head == NULL)
190 md->head = e;
191 else
192 {
193 assert (prev != NULL);
194 prev->next = e;
195 }
197 e->next = NULL;
198 }
199 else /* (this != NULL) */
200 {
201 if (prev == NULL)
202 md->head = e;
203 else
204 prev->next = e;
206 e->next = this->next;
207 }
209 pthread_mutex_unlock (&md->lock);
211 if (this != NULL)
212 {
213 this->next = NULL;
214 md_entry_free (this);
215 }
217 return (0);
218 } /* }}} int md_entry_insert */
220 /* XXX: The lock on md must be held while calling this function! */
221 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
222 const char *key)
223 {
224 meta_entry_t *e;
226 if ((md == NULL) || (key == NULL))
227 return (NULL);
229 for (e = md->head; e != NULL; e = e->next)
230 if (strcasecmp (key, e->key) == 0)
231 break;
233 return (e);
234 } /* }}} meta_entry_t *md_entry_lookup */
236 /*
237 * Public functions
238 */
239 meta_data_t *meta_data_create (void) /* {{{ */
240 {
241 meta_data_t *md;
243 md = (meta_data_t *) malloc (sizeof (*md));
244 if (md == NULL)
245 {
246 ERROR ("meta_data_create: malloc failed.");
247 return (NULL);
248 }
249 memset (md, 0, sizeof (*md));
251 md->head = NULL;
252 pthread_mutex_init (&md->lock, /* attr = */ NULL);
254 return (md);
255 } /* }}} meta_data_t *meta_data_create */
257 void meta_data_destroy (meta_data_t *md) /* {{{ */
258 {
259 if (md == NULL)
260 return;
262 md_entry_free (md->head);
263 free (md);
264 } /* }}} void meta_data_destroy */
266 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
267 {
268 meta_entry_t *e;
270 if ((md == NULL) || (key == NULL))
271 return (-EINVAL);
273 pthread_mutex_lock (&md->lock);
275 for (e = md->head; e != NULL; e = e->next)
276 {
277 if (strcasecmp (key, e->key) == 0)
278 {
279 pthread_mutex_unlock (&md->lock);
280 return (1);
281 }
282 }
284 pthread_mutex_unlock (&md->lock);
285 return (0);
286 } /* }}} int meta_data_exists */
288 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
289 {
290 meta_entry_t *this;
291 meta_entry_t *prev;
293 if ((md == NULL) || (key == NULL))
294 return (-EINVAL);
296 pthread_mutex_lock (&md->lock);
298 prev = NULL;
299 this = md->head;
300 while (this != NULL)
301 {
302 if (strcasecmp (key, this->key) == 0)
303 break;
305 prev = this;
306 this = this->next;
307 }
309 if (this == NULL)
310 {
311 pthread_mutex_unlock (&md->lock);
312 return (-ENOENT);
313 }
315 if (prev == NULL)
316 md->head = this->next;
317 else
318 prev->next = this->next;
320 pthread_mutex_unlock (&md->lock);
322 this->next = NULL;
323 md_entry_free (this);
325 return (0);
326 } /* }}} int meta_data_delete */
328 int meta_data_add_string (meta_data_t *md, /* {{{ */
329 const char *key, const char *value)
330 {
331 meta_entry_t *e;
333 if ((md == NULL) || (key == NULL) || (value == NULL))
334 return (-EINVAL);
336 e = md_entry_alloc (key);
337 if (e == NULL)
338 return (-ENOMEM);
340 e->value.mv_string = md_strdup (value);
341 if (e->value.mv_string == NULL)
342 {
343 ERROR ("meta_data_add_string: md_strdup failed.");
344 md_entry_free (e);
345 return (-ENOMEM);
346 }
347 e->type = MD_TYPE_STRING;
349 return (md_entry_insert (md, e));
350 } /* }}} int meta_data_add_string */
352 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
353 const char *key, int64_t value)
354 {
355 meta_entry_t *e;
357 if ((md == NULL) || (key == NULL))
358 return (-EINVAL);
360 e = md_entry_alloc (key);
361 if (e == NULL)
362 return (-ENOMEM);
364 e->value.mv_signed_int = value;
365 e->type = MD_TYPE_SIGNED_INT;
367 return (md_entry_insert (md, e));
368 } /* }}} int meta_data_add_signed_int */
370 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
371 const char *key, uint64_t value)
372 {
373 meta_entry_t *e;
375 if ((md == NULL) || (key == NULL))
376 return (-EINVAL);
378 e = md_entry_alloc (key);
379 if (e == NULL)
380 return (-ENOMEM);
382 e->value.mv_unsigned_int = value;
383 e->type = MD_TYPE_UNSIGNED_INT;
385 return (md_entry_insert (md, e));
386 } /* }}} int meta_data_add_unsigned_int */
388 int meta_data_add_double (meta_data_t *md, /* {{{ */
389 const char *key, double value)
390 {
391 meta_entry_t *e;
393 if ((md == NULL) || (key == NULL))
394 return (-EINVAL);
396 e = md_entry_alloc (key);
397 if (e == NULL)
398 return (-ENOMEM);
400 e->value.mv_double = value;
401 e->type = MD_TYPE_DOUBLE;
403 return (md_entry_insert (md, e));
404 } /* }}} int meta_data_add_double */
406 int meta_data_get_string (meta_data_t *md, /* {{{ */
407 const char *key, char **value)
408 {
409 meta_entry_t *e;
410 char *temp;
412 if ((md == NULL) || (key == NULL) || (value == NULL))
413 return (-EINVAL);
415 pthread_mutex_lock (&md->lock);
417 e = md_entry_lookup (md, key);
418 if (e == NULL)
419 {
420 pthread_mutex_unlock (&md->lock);
421 return (-ENOENT);
422 }
424 if (e->type != MD_TYPE_SIGNED_INT)
425 {
426 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
427 pthread_mutex_unlock (&md->lock);
428 return (-ENOENT);
429 }
431 temp = md_strdup (e->value.mv_string);
432 if (temp == NULL)
433 {
434 pthread_mutex_unlock (&md->lock);
435 ERROR ("meta_data_get_string: md_strdup failed.");
436 return (-ENOMEM);
437 }
439 pthread_mutex_unlock (&md->lock);
441 *value = temp;
443 return (0);
444 } /* }}} int meta_data_get_string */
446 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
447 const char *key, int64_t *value)
448 {
449 meta_entry_t *e;
451 if ((md == NULL) || (key == NULL) || (value == NULL))
452 return (-EINVAL);
454 pthread_mutex_lock (&md->lock);
456 e = md_entry_lookup (md, key);
457 if (e == NULL)
458 {
459 pthread_mutex_unlock (&md->lock);
460 return (-ENOENT);
461 }
463 if (e->type != MD_TYPE_SIGNED_INT)
464 {
465 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
466 pthread_mutex_unlock (&md->lock);
467 return (-ENOENT);
468 }
470 *value = e->value.mv_signed_int;
472 pthread_mutex_unlock (&md->lock);
473 return (0);
474 } /* }}} int meta_data_get_signed_int */
476 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
477 const char *key, uint64_t *value)
478 {
479 meta_entry_t *e;
481 if ((md == NULL) || (key == NULL) || (value == NULL))
482 return (-EINVAL);
484 pthread_mutex_lock (&md->lock);
486 e = md_entry_lookup (md, key);
487 if (e == NULL)
488 {
489 pthread_mutex_unlock (&md->lock);
490 return (-ENOENT);
491 }
493 if (e->type != MD_TYPE_UNSIGNED_INT)
494 {
495 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
496 pthread_mutex_unlock (&md->lock);
497 return (-ENOENT);
498 }
500 *value = e->value.mv_unsigned_int;
502 pthread_mutex_unlock (&md->lock);
503 return (0);
504 } /* }}} int meta_data_get_unsigned_int */
506 int meta_data_get_double (meta_data_t *md, /* {{{ */
507 const char *key, double *value)
508 {
509 meta_entry_t *e;
511 if ((md == NULL) || (key == NULL) || (value == NULL))
512 return (-EINVAL);
514 pthread_mutex_lock (&md->lock);
516 e = md_entry_lookup (md, key);
517 if (e == NULL)
518 {
519 pthread_mutex_unlock (&md->lock);
520 return (-ENOENT);
521 }
523 if (e->type != MD_TYPE_DOUBLE)
524 {
525 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
526 pthread_mutex_unlock (&md->lock);
527 return (-ENOENT);
528 }
530 *value = e->value.mv_double;
532 pthread_mutex_unlock (&md->lock);
533 return (0);
534 } /* }}} int meta_data_get_double */
536 /* vim: set sw=2 sts=2 et fdm=marker : */