Merge pull request #1024 from rubenk/fix-version-gen-on-osx
[collectd.git] / src / daemon / meta_data.c
1 /**
2  * collectd - src/meta_data.c
3  * Copyright (C) 2008-2011  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "meta_data.h"
30
31 #include <pthread.h>
32
33 /*
34  * Data types
35  */
36 union meta_value_u
37 {
38   char    *mv_string;
39   int64_t  mv_signed_int;
40   uint64_t mv_unsigned_int;
41   double   mv_double;
42   _Bool    mv_boolean;
43 };
44 typedef union meta_value_u meta_value_t;
45
46 struct meta_entry_s;
47 typedef struct meta_entry_s meta_entry_t;
48 struct meta_entry_s
49 {
50   char         *key;
51   meta_value_t  value;
52   int           type;
53   meta_entry_t *next;
54 };
55
56 struct meta_data_s
57 {
58   meta_entry_t   *head;
59   pthread_mutex_t lock;
60 };
61
62 /*
63  * Private functions
64  */
65 static char *md_strdup (const char *orig) /* {{{ */
66 {
67   size_t sz;
68   char *dest;
69
70   if (orig == NULL)
71     return (NULL);
72
73   sz = strlen (orig) + 1;
74   dest = (char *) malloc (sz);
75   if (dest == NULL)
76     return (NULL);
77
78   memcpy (dest, orig, sz);
79
80   return (dest);
81 } /* }}} char *md_strdup */
82
83 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
84 {
85   meta_entry_t *e;
86
87   e = (meta_entry_t *) malloc (sizeof (*e));
88   if (e == NULL)
89   {
90     ERROR ("md_entry_alloc: malloc failed.");
91     return (NULL);
92   }
93   memset (e, 0, sizeof (*e));
94
95   e->key = md_strdup (key);
96   if (e->key == NULL)
97   {
98     free (e);
99     ERROR ("md_entry_alloc: md_strdup failed.");
100     return (NULL);
101   }
102
103   e->type = 0;
104   e->next = NULL;
105
106   return (e);
107 } /* }}} meta_entry_t *md_entry_alloc */
108
109 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
110 {
111   meta_entry_t *copy;
112
113   if (orig == NULL)
114     return (NULL);
115
116   copy = md_entry_alloc (orig->key);
117   copy->type = orig->type;
118   if (copy->type == MD_TYPE_STRING)
119     copy->value.mv_string = strdup (orig->value.mv_string);
120   else
121     copy->value = orig->value;
122
123   copy->next = md_entry_clone (orig->next);
124   return (copy);
125 } /* }}} meta_entry_t *md_entry_clone */
126
127 static void md_entry_free (meta_entry_t *e) /* {{{ */
128 {
129   if (e == NULL)
130     return;
131
132   free (e->key);
133
134   if (e->type == MD_TYPE_STRING)
135     free (e->value.mv_string);
136
137   if (e->next != NULL)
138     md_entry_free (e->next);
139
140   free (e);
141 } /* }}} void md_entry_free */
142
143 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
144 {
145   meta_entry_t *this;
146   meta_entry_t *prev;
147
148   if ((md == NULL) || (e == NULL))
149     return (-EINVAL);
150
151   pthread_mutex_lock (&md->lock);
152
153   prev = NULL;
154   this = md->head;
155   while (this != NULL)
156   {
157     if (strcasecmp (e->key, this->key) == 0)
158       break;
159
160     prev = this;
161     this = this->next;
162   }
163
164   if (this == NULL)
165   {
166     /* This key does not exist yet. */
167     if (md->head == NULL)
168       md->head = e;
169     else
170     {
171       assert (prev != NULL);
172       prev->next = e;
173     }
174
175     e->next = NULL;
176   }
177   else /* (this != NULL) */
178   {
179     if (prev == NULL)
180       md->head = e;
181     else
182       prev->next = e;
183
184     e->next = this->next;
185   }
186
187   pthread_mutex_unlock (&md->lock);
188
189   if (this != NULL)
190   {
191     this->next = NULL;
192     md_entry_free (this);
193   }
194
195   return (0);
196 } /* }}} int md_entry_insert */
197
198 /* XXX: The lock on md must be held while calling this function! */
199 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
200     const char *key)
201 {
202   meta_entry_t *e;
203
204   if ((md == NULL) || (key == NULL))
205     return (NULL);
206
207   for (e = md->head; e != NULL; e = e->next)
208     if (strcasecmp (key, e->key) == 0)
209       break;
210
211   return (e);
212 } /* }}} meta_entry_t *md_entry_lookup */
213
214 /*
215  * Public functions
216  */
217 meta_data_t *meta_data_create (void) /* {{{ */
218 {
219   meta_data_t *md;
220
221   md = (meta_data_t *) malloc (sizeof (*md));
222   if (md == NULL)
223   {
224     ERROR ("meta_data_create: malloc failed.");
225     return (NULL);
226   }
227   memset (md, 0, sizeof (*md));
228
229   md->head = NULL;
230   pthread_mutex_init (&md->lock, /* attr = */ NULL);
231
232   return (md);
233 } /* }}} meta_data_t *meta_data_create */
234
235 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
236 {
237   meta_data_t *copy;
238
239   if (orig == NULL)
240     return (NULL);
241
242   copy = meta_data_create ();
243   if (copy == NULL)
244     return (NULL);
245
246   pthread_mutex_lock (&orig->lock);
247   copy->head = md_entry_clone (orig->head);
248   pthread_mutex_unlock (&orig->lock);
249
250   return (copy);
251 } /* }}} meta_data_t *meta_data_clone */
252
253 void meta_data_destroy (meta_data_t *md) /* {{{ */
254 {
255   if (md == NULL)
256     return;
257
258   pthread_mutex_destroy(&md->lock);
259   md_entry_free (md->head);
260   pthread_mutex_destroy (&md->lock);
261   free (md);
262 } /* }}} void meta_data_destroy */
263
264 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
265 {
266   meta_entry_t *e;
267
268   if ((md == NULL) || (key == NULL))
269     return (-EINVAL);
270
271   pthread_mutex_lock (&md->lock);
272
273   for (e = md->head; e != NULL; e = e->next)
274   {
275     if (strcasecmp (key, e->key) == 0)
276     {
277       pthread_mutex_unlock (&md->lock);
278       return (1);
279     }
280   }
281
282   pthread_mutex_unlock (&md->lock);
283   return (0);
284 } /* }}} int meta_data_exists */
285
286 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
287 {
288   meta_entry_t *e;
289
290   if ((md == NULL) || (key == NULL))
291     return -EINVAL;
292
293   pthread_mutex_lock (&md->lock);
294
295   for (e = md->head; e != NULL; e = e->next)
296   {
297     if (strcasecmp (key, e->key) == 0)
298     {
299       pthread_mutex_unlock (&md->lock);
300       return e->type;
301     }
302   }
303
304   pthread_mutex_unlock (&md->lock);
305   return 0;
306 } /* }}} int meta_data_type */
307
308 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
309 {
310   int i = 0, count = 0;
311   meta_entry_t *e;
312
313   if ((md == NULL) || (toc == NULL))
314     return -EINVAL;
315
316   pthread_mutex_lock (&md->lock);
317
318   for (e = md->head; e != NULL; e = e->next)
319     ++count;    
320
321   if (count == 0)
322   {
323     pthread_mutex_unlock (&md->lock);
324     return (count);
325   }
326
327   *toc = calloc(count, sizeof(**toc));
328   for (e = md->head; e != NULL; e = e->next)
329     (*toc)[i++] = strdup(e->key);
330   
331   pthread_mutex_unlock (&md->lock);
332   return count;
333 } /* }}} int meta_data_toc */
334
335 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
336 {
337   meta_entry_t *this;
338   meta_entry_t *prev;
339
340   if ((md == NULL) || (key == NULL))
341     return (-EINVAL);
342
343   pthread_mutex_lock (&md->lock);
344
345   prev = NULL;
346   this = md->head;
347   while (this != NULL)
348   {
349     if (strcasecmp (key, this->key) == 0)
350       break;
351
352     prev = this;
353     this = this->next;
354   }
355
356   if (this == NULL)
357   {
358     pthread_mutex_unlock (&md->lock);
359     return (-ENOENT);
360   }
361
362   if (prev == NULL)
363     md->head = this->next;
364   else
365     prev->next = this->next;
366
367   pthread_mutex_unlock (&md->lock);
368
369   this->next = NULL;
370   md_entry_free (this);
371
372   return (0);
373 } /* }}} int meta_data_delete */
374
375 /*
376  * Add functions
377  */
378 int meta_data_add_string (meta_data_t *md, /* {{{ */
379     const char *key, const char *value)
380 {
381   meta_entry_t *e;
382
383   if ((md == NULL) || (key == NULL) || (value == NULL))
384     return (-EINVAL);
385
386   e = md_entry_alloc (key);
387   if (e == NULL)
388     return (-ENOMEM);
389
390   e->value.mv_string = md_strdup (value);
391   if (e->value.mv_string == NULL)
392   {
393     ERROR ("meta_data_add_string: md_strdup failed.");
394     md_entry_free (e);
395     return (-ENOMEM);
396   }
397   e->type = MD_TYPE_STRING;
398
399   return (md_entry_insert (md, e));
400 } /* }}} int meta_data_add_string */
401
402 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
403     const char *key, int64_t value)
404 {
405   meta_entry_t *e;
406
407   if ((md == NULL) || (key == NULL))
408     return (-EINVAL);
409
410   e = md_entry_alloc (key);
411   if (e == NULL)
412     return (-ENOMEM);
413
414   e->value.mv_signed_int = value;
415   e->type = MD_TYPE_SIGNED_INT;
416
417   return (md_entry_insert (md, e));
418 } /* }}} int meta_data_add_signed_int */
419
420 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
421     const char *key, uint64_t value)
422 {
423   meta_entry_t *e;
424
425   if ((md == NULL) || (key == NULL))
426     return (-EINVAL);
427
428   e = md_entry_alloc (key);
429   if (e == NULL)
430     return (-ENOMEM);
431
432   e->value.mv_unsigned_int = value;
433   e->type = MD_TYPE_UNSIGNED_INT;
434
435   return (md_entry_insert (md, e));
436 } /* }}} int meta_data_add_unsigned_int */
437
438 int meta_data_add_double (meta_data_t *md, /* {{{ */
439     const char *key, double value)
440 {
441   meta_entry_t *e;
442
443   if ((md == NULL) || (key == NULL))
444     return (-EINVAL);
445
446   e = md_entry_alloc (key);
447   if (e == NULL)
448     return (-ENOMEM);
449
450   e->value.mv_double = value;
451   e->type = MD_TYPE_DOUBLE;
452
453   return (md_entry_insert (md, e));
454 } /* }}} int meta_data_add_double */
455
456 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
457     const char *key, _Bool value)
458 {
459   meta_entry_t *e;
460
461   if ((md == NULL) || (key == NULL))
462     return (-EINVAL);
463
464   e = md_entry_alloc (key);
465   if (e == NULL)
466     return (-ENOMEM);
467
468   e->value.mv_boolean = value;
469   e->type = MD_TYPE_BOOLEAN;
470
471   return (md_entry_insert (md, e));
472 } /* }}} int meta_data_add_boolean */
473
474 /*
475  * Get functions
476  */
477 int meta_data_get_string (meta_data_t *md, /* {{{ */
478     const char *key, char **value)
479 {
480   meta_entry_t *e;
481   char *temp;
482
483   if ((md == NULL) || (key == NULL) || (value == NULL))
484     return (-EINVAL);
485
486   pthread_mutex_lock (&md->lock);
487
488   e = md_entry_lookup (md, key);
489   if (e == NULL)
490   {
491     pthread_mutex_unlock (&md->lock);
492     return (-ENOENT);
493   }
494
495   if (e->type != MD_TYPE_STRING)
496   {
497     ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
498     pthread_mutex_unlock (&md->lock);
499     return (-ENOENT);
500   }
501
502   temp = md_strdup (e->value.mv_string);
503   if (temp == NULL)
504   {
505     pthread_mutex_unlock (&md->lock);
506     ERROR ("meta_data_get_string: md_strdup failed.");
507     return (-ENOMEM);
508   }
509  
510   pthread_mutex_unlock (&md->lock);
511
512   *value = temp;
513
514   return (0);
515 } /* }}} int meta_data_get_string */
516
517 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
518     const char *key, int64_t *value)
519 {
520   meta_entry_t *e;
521
522   if ((md == NULL) || (key == NULL) || (value == NULL))
523     return (-EINVAL);
524
525   pthread_mutex_lock (&md->lock);
526
527   e = md_entry_lookup (md, key);
528   if (e == NULL)
529   {
530     pthread_mutex_unlock (&md->lock);
531     return (-ENOENT);
532   }
533
534   if (e->type != MD_TYPE_SIGNED_INT)
535   {
536     ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
537     pthread_mutex_unlock (&md->lock);
538     return (-ENOENT);
539   }
540
541   *value = e->value.mv_signed_int;
542
543   pthread_mutex_unlock (&md->lock);
544   return (0);
545 } /* }}} int meta_data_get_signed_int */
546
547 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
548     const char *key, uint64_t *value)
549 {
550   meta_entry_t *e;
551
552   if ((md == NULL) || (key == NULL) || (value == NULL))
553     return (-EINVAL);
554
555   pthread_mutex_lock (&md->lock);
556
557   e = md_entry_lookup (md, key);
558   if (e == NULL)
559   {
560     pthread_mutex_unlock (&md->lock);
561     return (-ENOENT);
562   }
563
564   if (e->type != MD_TYPE_UNSIGNED_INT)
565   {
566     ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
567     pthread_mutex_unlock (&md->lock);
568     return (-ENOENT);
569   }
570
571   *value = e->value.mv_unsigned_int;
572
573   pthread_mutex_unlock (&md->lock);
574   return (0);
575 } /* }}} int meta_data_get_unsigned_int */
576
577 int meta_data_get_double (meta_data_t *md, /* {{{ */
578     const char *key, double *value)
579 {
580   meta_entry_t *e;
581
582   if ((md == NULL) || (key == NULL) || (value == NULL))
583     return (-EINVAL);
584
585   pthread_mutex_lock (&md->lock);
586
587   e = md_entry_lookup (md, key);
588   if (e == NULL)
589   {
590     pthread_mutex_unlock (&md->lock);
591     return (-ENOENT);
592   }
593
594   if (e->type != MD_TYPE_DOUBLE)
595   {
596     ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
597     pthread_mutex_unlock (&md->lock);
598     return (-ENOENT);
599   }
600
601   *value = e->value.mv_double;
602
603   pthread_mutex_unlock (&md->lock);
604   return (0);
605 } /* }}} int meta_data_get_double */
606
607 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
608     const char *key, _Bool *value)
609 {
610   meta_entry_t *e;
611
612   if ((md == NULL) || (key == NULL) || (value == NULL))
613     return (-EINVAL);
614
615   pthread_mutex_lock (&md->lock);
616
617   e = md_entry_lookup (md, key);
618   if (e == NULL)
619   {
620     pthread_mutex_unlock (&md->lock);
621     return (-ENOENT);
622   }
623
624   if (e->type != MD_TYPE_BOOLEAN)
625   {
626     ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
627     pthread_mutex_unlock (&md->lock);
628     return (-ENOENT);
629   }
630
631   *value = e->value.mv_boolean;
632
633   pthread_mutex_unlock (&md->lock);
634   return (0);
635 } /* }}} int meta_data_get_boolean */
636
637 /* vim: set sw=2 sts=2 et fdm=marker : */