Merge branch 'collectd-5.4' into collectd-5.5
[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   md_entry_free (md->head);
259   pthread_mutex_destroy (&md->lock);
260   free (md);
261 } /* }}} void meta_data_destroy */
262
263 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
264 {
265   meta_entry_t *e;
266
267   if ((md == NULL) || (key == NULL))
268     return (-EINVAL);
269
270   pthread_mutex_lock (&md->lock);
271
272   for (e = md->head; e != NULL; e = e->next)
273   {
274     if (strcasecmp (key, e->key) == 0)
275     {
276       pthread_mutex_unlock (&md->lock);
277       return (1);
278     }
279   }
280
281   pthread_mutex_unlock (&md->lock);
282   return (0);
283 } /* }}} int meta_data_exists */
284
285 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
286 {
287   meta_entry_t *e;
288
289   if ((md == NULL) || (key == NULL))
290     return -EINVAL;
291
292   pthread_mutex_lock (&md->lock);
293
294   for (e = md->head; e != NULL; e = e->next)
295   {
296     if (strcasecmp (key, e->key) == 0)
297     {
298       pthread_mutex_unlock (&md->lock);
299       return e->type;
300     }
301   }
302
303   pthread_mutex_unlock (&md->lock);
304   return 0;
305 } /* }}} int meta_data_type */
306
307 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
308 {
309   int i = 0, count = 0;
310   meta_entry_t *e;
311
312   if ((md == NULL) || (toc == NULL))
313     return -EINVAL;
314
315   pthread_mutex_lock (&md->lock);
316
317   for (e = md->head; e != NULL; e = e->next)
318     ++count;    
319
320   if (count == 0)
321   {
322     pthread_mutex_unlock (&md->lock);
323     return (count);
324   }
325
326   *toc = calloc(count, sizeof(**toc));
327   for (e = md->head; e != NULL; e = e->next)
328     (*toc)[i++] = strdup(e->key);
329   
330   pthread_mutex_unlock (&md->lock);
331   return count;
332 } /* }}} int meta_data_toc */
333
334 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
335 {
336   meta_entry_t *this;
337   meta_entry_t *prev;
338
339   if ((md == NULL) || (key == NULL))
340     return (-EINVAL);
341
342   pthread_mutex_lock (&md->lock);
343
344   prev = NULL;
345   this = md->head;
346   while (this != NULL)
347   {
348     if (strcasecmp (key, this->key) == 0)
349       break;
350
351     prev = this;
352     this = this->next;
353   }
354
355   if (this == NULL)
356   {
357     pthread_mutex_unlock (&md->lock);
358     return (-ENOENT);
359   }
360
361   if (prev == NULL)
362     md->head = this->next;
363   else
364     prev->next = this->next;
365
366   pthread_mutex_unlock (&md->lock);
367
368   this->next = NULL;
369   md_entry_free (this);
370
371   return (0);
372 } /* }}} int meta_data_delete */
373
374 /*
375  * Add functions
376  */
377 int meta_data_add_string (meta_data_t *md, /* {{{ */
378     const char *key, const char *value)
379 {
380   meta_entry_t *e;
381
382   if ((md == NULL) || (key == NULL) || (value == NULL))
383     return (-EINVAL);
384
385   e = md_entry_alloc (key);
386   if (e == NULL)
387     return (-ENOMEM);
388
389   e->value.mv_string = md_strdup (value);
390   if (e->value.mv_string == NULL)
391   {
392     ERROR ("meta_data_add_string: md_strdup failed.");
393     md_entry_free (e);
394     return (-ENOMEM);
395   }
396   e->type = MD_TYPE_STRING;
397
398   return (md_entry_insert (md, e));
399 } /* }}} int meta_data_add_string */
400
401 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
402     const char *key, int64_t value)
403 {
404   meta_entry_t *e;
405
406   if ((md == NULL) || (key == NULL))
407     return (-EINVAL);
408
409   e = md_entry_alloc (key);
410   if (e == NULL)
411     return (-ENOMEM);
412
413   e->value.mv_signed_int = value;
414   e->type = MD_TYPE_SIGNED_INT;
415
416   return (md_entry_insert (md, e));
417 } /* }}} int meta_data_add_signed_int */
418
419 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
420     const char *key, uint64_t value)
421 {
422   meta_entry_t *e;
423
424   if ((md == NULL) || (key == NULL))
425     return (-EINVAL);
426
427   e = md_entry_alloc (key);
428   if (e == NULL)
429     return (-ENOMEM);
430
431   e->value.mv_unsigned_int = value;
432   e->type = MD_TYPE_UNSIGNED_INT;
433
434   return (md_entry_insert (md, e));
435 } /* }}} int meta_data_add_unsigned_int */
436
437 int meta_data_add_double (meta_data_t *md, /* {{{ */
438     const char *key, double value)
439 {
440   meta_entry_t *e;
441
442   if ((md == NULL) || (key == NULL))
443     return (-EINVAL);
444
445   e = md_entry_alloc (key);
446   if (e == NULL)
447     return (-ENOMEM);
448
449   e->value.mv_double = value;
450   e->type = MD_TYPE_DOUBLE;
451
452   return (md_entry_insert (md, e));
453 } /* }}} int meta_data_add_double */
454
455 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
456     const char *key, _Bool value)
457 {
458   meta_entry_t *e;
459
460   if ((md == NULL) || (key == NULL))
461     return (-EINVAL);
462
463   e = md_entry_alloc (key);
464   if (e == NULL)
465     return (-ENOMEM);
466
467   e->value.mv_boolean = value;
468   e->type = MD_TYPE_BOOLEAN;
469
470   return (md_entry_insert (md, e));
471 } /* }}} int meta_data_add_boolean */
472
473 /*
474  * Get functions
475  */
476 int meta_data_get_string (meta_data_t *md, /* {{{ */
477     const char *key, char **value)
478 {
479   meta_entry_t *e;
480   char *temp;
481
482   if ((md == NULL) || (key == NULL) || (value == NULL))
483     return (-EINVAL);
484
485   pthread_mutex_lock (&md->lock);
486
487   e = md_entry_lookup (md, key);
488   if (e == NULL)
489   {
490     pthread_mutex_unlock (&md->lock);
491     return (-ENOENT);
492   }
493
494   if (e->type != MD_TYPE_STRING)
495   {
496     ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
497     pthread_mutex_unlock (&md->lock);
498     return (-ENOENT);
499   }
500
501   temp = md_strdup (e->value.mv_string);
502   if (temp == NULL)
503   {
504     pthread_mutex_unlock (&md->lock);
505     ERROR ("meta_data_get_string: md_strdup failed.");
506     return (-ENOMEM);
507   }
508  
509   pthread_mutex_unlock (&md->lock);
510
511   *value = temp;
512
513   return (0);
514 } /* }}} int meta_data_get_string */
515
516 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
517     const char *key, int64_t *value)
518 {
519   meta_entry_t *e;
520
521   if ((md == NULL) || (key == NULL) || (value == NULL))
522     return (-EINVAL);
523
524   pthread_mutex_lock (&md->lock);
525
526   e = md_entry_lookup (md, key);
527   if (e == NULL)
528   {
529     pthread_mutex_unlock (&md->lock);
530     return (-ENOENT);
531   }
532
533   if (e->type != MD_TYPE_SIGNED_INT)
534   {
535     ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
536     pthread_mutex_unlock (&md->lock);
537     return (-ENOENT);
538   }
539
540   *value = e->value.mv_signed_int;
541
542   pthread_mutex_unlock (&md->lock);
543   return (0);
544 } /* }}} int meta_data_get_signed_int */
545
546 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
547     const char *key, uint64_t *value)
548 {
549   meta_entry_t *e;
550
551   if ((md == NULL) || (key == NULL) || (value == NULL))
552     return (-EINVAL);
553
554   pthread_mutex_lock (&md->lock);
555
556   e = md_entry_lookup (md, key);
557   if (e == NULL)
558   {
559     pthread_mutex_unlock (&md->lock);
560     return (-ENOENT);
561   }
562
563   if (e->type != MD_TYPE_UNSIGNED_INT)
564   {
565     ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
566     pthread_mutex_unlock (&md->lock);
567     return (-ENOENT);
568   }
569
570   *value = e->value.mv_unsigned_int;
571
572   pthread_mutex_unlock (&md->lock);
573   return (0);
574 } /* }}} int meta_data_get_unsigned_int */
575
576 int meta_data_get_double (meta_data_t *md, /* {{{ */
577     const char *key, double *value)
578 {
579   meta_entry_t *e;
580
581   if ((md == NULL) || (key == NULL) || (value == NULL))
582     return (-EINVAL);
583
584   pthread_mutex_lock (&md->lock);
585
586   e = md_entry_lookup (md, key);
587   if (e == NULL)
588   {
589     pthread_mutex_unlock (&md->lock);
590     return (-ENOENT);
591   }
592
593   if (e->type != MD_TYPE_DOUBLE)
594   {
595     ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
596     pthread_mutex_unlock (&md->lock);
597     return (-ENOENT);
598   }
599
600   *value = e->value.mv_double;
601
602   pthread_mutex_unlock (&md->lock);
603   return (0);
604 } /* }}} int meta_data_get_double */
605
606 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
607     const char *key, _Bool *value)
608 {
609   meta_entry_t *e;
610
611   if ((md == NULL) || (key == NULL) || (value == NULL))
612     return (-EINVAL);
613
614   pthread_mutex_lock (&md->lock);
615
616   e = md_entry_lookup (md, key);
617   if (e == NULL)
618   {
619     pthread_mutex_unlock (&md->lock);
620     return (-ENOENT);
621   }
622
623   if (e->type != MD_TYPE_BOOLEAN)
624   {
625     ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
626     pthread_mutex_unlock (&md->lock);
627     return (-ENOENT);
628   }
629
630   *value = e->value.mv_boolean;
631
632   pthread_mutex_unlock (&md->lock);
633   return (0);
634 } /* }}} int meta_data_get_boolean */
635
636 /* vim: set sw=2 sts=2 et fdm=marker : */