sigrok: use pkg-config to find library
[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
29 #include "plugin.h"
30 #include "meta_data.h"
31
32 /*
33  * Data types
34  */
35 union meta_value_u
36 {
37   char    *mv_string;
38   int64_t  mv_signed_int;
39   uint64_t mv_unsigned_int;
40   double   mv_double;
41   _Bool    mv_boolean;
42 };
43 typedef union meta_value_u meta_value_t;
44
45 struct meta_entry_s;
46 typedef struct meta_entry_s meta_entry_t;
47 struct meta_entry_s
48 {
49   char         *key;
50   meta_value_t  value;
51   int           type;
52   meta_entry_t *next;
53 };
54
55 struct meta_data_s
56 {
57   meta_entry_t   *head;
58   pthread_mutex_t lock;
59 };
60
61 /*
62  * Private functions
63  */
64 static char *md_strdup (const char *orig) /* {{{ */
65 {
66   size_t sz;
67   char *dest;
68
69   if (orig == NULL)
70     return (NULL);
71
72   sz = strlen (orig) + 1;
73   dest = malloc (sz);
74   if (dest == NULL)
75     return (NULL);
76
77   memcpy (dest, orig, sz);
78
79   return (dest);
80 } /* }}} char *md_strdup */
81
82 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
83 {
84   meta_entry_t *e;
85
86   e = calloc (1, sizeof (*e));
87   if (e == NULL)
88   {
89     ERROR ("md_entry_alloc: calloc failed.");
90     return (NULL);
91   }
92
93   e->key = md_strdup (key);
94   if (e->key == NULL)
95   {
96     free (e);
97     ERROR ("md_entry_alloc: md_strdup failed.");
98     return (NULL);
99   }
100
101   e->type = 0;
102   e->next = NULL;
103
104   return (e);
105 } /* }}} meta_entry_t *md_entry_alloc */
106
107 /* XXX: The lock on md must be held while calling this function! */
108 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
109 {
110   meta_entry_t *copy;
111
112   /* WARNINGS :
113    *  - we do not check that orig != NULL here. You should have done it before.
114    *  - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
115    */
116
117   copy = md_entry_alloc (orig->key);
118   if (copy == NULL)
119     return (NULL);
120   copy->type = orig->type;
121   if (copy->type == MD_TYPE_STRING)
122     copy->value.mv_string = strdup (orig->value.mv_string);
123   else
124     copy->value = orig->value;
125
126   return (copy);
127 } /* }}} meta_entry_t *md_entry_clone_contents */
128
129 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
130 {
131   meta_entry_t *copy;
132
133   if (orig == NULL)
134     return (NULL);
135
136   copy = md_entry_clone_contents(orig);
137
138   copy->next = md_entry_clone (orig->next);
139   return (copy);
140 } /* }}} meta_entry_t *md_entry_clone */
141
142 static void md_entry_free (meta_entry_t *e) /* {{{ */
143 {
144   if (e == NULL)
145     return;
146
147   free (e->key);
148
149   if (e->type == MD_TYPE_STRING)
150     free (e->value.mv_string);
151
152   if (e->next != NULL)
153     md_entry_free (e->next);
154
155   free (e);
156 } /* }}} void md_entry_free */
157
158 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
159 {
160   meta_entry_t *this;
161   meta_entry_t *prev;
162
163   if ((md == NULL) || (e == NULL))
164     return (-EINVAL);
165
166   pthread_mutex_lock (&md->lock);
167
168   prev = NULL;
169   this = md->head;
170   while (this != NULL)
171   {
172     if (strcasecmp (e->key, this->key) == 0)
173       break;
174
175     prev = this;
176     this = this->next;
177   }
178
179   if (this == NULL)
180   {
181     /* This key does not exist yet. */
182     if (md->head == NULL)
183       md->head = e;
184     else
185     {
186       assert (prev != NULL);
187       prev->next = e;
188     }
189
190     e->next = NULL;
191   }
192   else /* (this != NULL) */
193   {
194     if (prev == NULL)
195       md->head = e;
196     else
197       prev->next = e;
198
199     e->next = this->next;
200   }
201
202   pthread_mutex_unlock (&md->lock);
203
204   if (this != NULL)
205   {
206     this->next = NULL;
207     md_entry_free (this);
208   }
209
210   return (0);
211 } /* }}} int md_entry_insert */
212
213 /* XXX: The lock on md must be held while calling this function! */
214 static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */
215 {
216   meta_entry_t *e;
217   meta_entry_t *this;
218   meta_entry_t *prev;
219
220   /* WARNINGS :
221    *  - we do not check that md and e != NULL here. You should have done it before.
222    *  - we do not use the lock. You should have set it before.
223    */
224
225   e = md_entry_clone_contents(orig);
226
227   prev = NULL;
228   this = md->head;
229   while (this != NULL)
230   {
231     if (strcasecmp (e->key, this->key) == 0)
232       break;
233
234     prev = this;
235     this = this->next;
236   }
237
238   if (this == NULL)
239   {
240     /* This key does not exist yet. */
241     if (md->head == NULL)
242       md->head = e;
243     else
244     {
245       assert (prev != NULL);
246       prev->next = e;
247     }
248
249     e->next = NULL;
250   }
251   else /* (this != NULL) */
252   {
253     if (prev == NULL)
254       md->head = e;
255     else
256       prev->next = e;
257
258     e->next = this->next;
259   }
260
261   if (this != NULL)
262   {
263     this->next = NULL;
264     md_entry_free (this);
265   }
266
267   return (0);
268 } /* }}} int md_entry_insert_clone */
269
270 /* XXX: The lock on md must be held while calling this function! */
271 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
272     const char *key)
273 {
274   meta_entry_t *e;
275
276   if ((md == NULL) || (key == NULL))
277     return (NULL);
278
279   for (e = md->head; e != NULL; e = e->next)
280     if (strcasecmp (key, e->key) == 0)
281       break;
282
283   return (e);
284 } /* }}} meta_entry_t *md_entry_lookup */
285
286 /*
287  * Each value_list_t*, as it is going through the system, is handled by exactly
288  * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
289  * rrdtool plugin, must create a copy first. The meta data within a
290  * value_list_t* is not thread safe and doesn't need to be.
291  *
292  * The meta data associated with cache entries are a different story. There, we
293  * need to ensure exclusive locking to prevent leaks and other funky business.
294  * This is ensured by the uc_meta_data_get_*() functions.
295  */
296
297 /*
298  * Public functions
299  */
300 meta_data_t *meta_data_create (void) /* {{{ */
301 {
302   meta_data_t *md;
303
304   md = calloc (1, sizeof (*md));
305   if (md == NULL)
306   {
307     ERROR ("meta_data_create: calloc failed.");
308     return (NULL);
309   }
310
311   pthread_mutex_init (&md->lock, /* attr = */ NULL);
312
313   return (md);
314 } /* }}} meta_data_t *meta_data_create */
315
316 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
317 {
318   meta_data_t *copy;
319
320   if (orig == NULL)
321     return (NULL);
322
323   copy = meta_data_create ();
324   if (copy == NULL)
325     return (NULL);
326
327   pthread_mutex_lock (&orig->lock);
328   copy->head = md_entry_clone (orig->head);
329   pthread_mutex_unlock (&orig->lock);
330
331   return (copy);
332 } /* }}} meta_data_t *meta_data_clone */
333
334 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
335 {
336   meta_entry_t *e;
337
338   if (orig == NULL)
339     return (0);
340
341   if (*dest == NULL) {
342     *dest = meta_data_clone(orig);
343     return(0);
344   }
345
346   pthread_mutex_lock (&orig->lock);
347   for (e=orig->head; e != NULL; e = e->next)
348   {
349     md_entry_insert_clone((*dest), e);
350   }
351   pthread_mutex_unlock (&orig->lock);
352
353   return (0);
354 } /* }}} int meta_data_clone_merge */
355
356 void meta_data_destroy (meta_data_t *md) /* {{{ */
357 {
358   if (md == NULL)
359     return;
360
361   md_entry_free (md->head);
362   pthread_mutex_destroy (&md->lock);
363   free (md);
364 } /* }}} void meta_data_destroy */
365
366 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
367 {
368   meta_entry_t *e;
369
370   if ((md == NULL) || (key == NULL))
371     return (-EINVAL);
372
373   pthread_mutex_lock (&md->lock);
374
375   for (e = md->head; e != NULL; e = e->next)
376   {
377     if (strcasecmp (key, e->key) == 0)
378     {
379       pthread_mutex_unlock (&md->lock);
380       return (1);
381     }
382   }
383
384   pthread_mutex_unlock (&md->lock);
385   return (0);
386 } /* }}} int meta_data_exists */
387
388 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
389 {
390   meta_entry_t *e;
391
392   if ((md == NULL) || (key == NULL))
393     return -EINVAL;
394
395   pthread_mutex_lock (&md->lock);
396
397   for (e = md->head; e != NULL; e = e->next)
398   {
399     if (strcasecmp (key, e->key) == 0)
400     {
401       pthread_mutex_unlock (&md->lock);
402       return e->type;
403     }
404   }
405
406   pthread_mutex_unlock (&md->lock);
407   return 0;
408 } /* }}} int meta_data_type */
409
410 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
411 {
412   int i = 0, count = 0;
413   meta_entry_t *e;
414
415   if ((md == NULL) || (toc == NULL))
416     return -EINVAL;
417
418   pthread_mutex_lock (&md->lock);
419
420   for (e = md->head; e != NULL; e = e->next)
421     ++count;
422
423   if (count == 0)
424   {
425     pthread_mutex_unlock (&md->lock);
426     return (count);
427   }
428
429   *toc = calloc(count, sizeof(**toc));
430   for (e = md->head; e != NULL; e = e->next)
431     (*toc)[i++] = strdup(e->key);
432
433   pthread_mutex_unlock (&md->lock);
434   return count;
435 } /* }}} int meta_data_toc */
436
437 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
438 {
439   meta_entry_t *this;
440   meta_entry_t *prev;
441
442   if ((md == NULL) || (key == NULL))
443     return (-EINVAL);
444
445   pthread_mutex_lock (&md->lock);
446
447   prev = NULL;
448   this = md->head;
449   while (this != NULL)
450   {
451     if (strcasecmp (key, this->key) == 0)
452       break;
453
454     prev = this;
455     this = this->next;
456   }
457
458   if (this == NULL)
459   {
460     pthread_mutex_unlock (&md->lock);
461     return (-ENOENT);
462   }
463
464   if (prev == NULL)
465     md->head = this->next;
466   else
467     prev->next = this->next;
468
469   pthread_mutex_unlock (&md->lock);
470
471   this->next = NULL;
472   md_entry_free (this);
473
474   return (0);
475 } /* }}} int meta_data_delete */
476
477 /*
478  * Add functions
479  */
480 int meta_data_add_string (meta_data_t *md, /* {{{ */
481     const char *key, const char *value)
482 {
483   meta_entry_t *e;
484
485   if ((md == NULL) || (key == NULL) || (value == NULL))
486     return (-EINVAL);
487
488   e = md_entry_alloc (key);
489   if (e == NULL)
490     return (-ENOMEM);
491
492   e->value.mv_string = md_strdup (value);
493   if (e->value.mv_string == NULL)
494   {
495     ERROR ("meta_data_add_string: md_strdup failed.");
496     md_entry_free (e);
497     return (-ENOMEM);
498   }
499   e->type = MD_TYPE_STRING;
500
501   return (md_entry_insert (md, e));
502 } /* }}} int meta_data_add_string */
503
504 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
505     const char *key, int64_t value)
506 {
507   meta_entry_t *e;
508
509   if ((md == NULL) || (key == NULL))
510     return (-EINVAL);
511
512   e = md_entry_alloc (key);
513   if (e == NULL)
514     return (-ENOMEM);
515
516   e->value.mv_signed_int = value;
517   e->type = MD_TYPE_SIGNED_INT;
518
519   return (md_entry_insert (md, e));
520 } /* }}} int meta_data_add_signed_int */
521
522 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
523     const char *key, uint64_t value)
524 {
525   meta_entry_t *e;
526
527   if ((md == NULL) || (key == NULL))
528     return (-EINVAL);
529
530   e = md_entry_alloc (key);
531   if (e == NULL)
532     return (-ENOMEM);
533
534   e->value.mv_unsigned_int = value;
535   e->type = MD_TYPE_UNSIGNED_INT;
536
537   return (md_entry_insert (md, e));
538 } /* }}} int meta_data_add_unsigned_int */
539
540 int meta_data_add_double (meta_data_t *md, /* {{{ */
541     const char *key, double value)
542 {
543   meta_entry_t *e;
544
545   if ((md == NULL) || (key == NULL))
546     return (-EINVAL);
547
548   e = md_entry_alloc (key);
549   if (e == NULL)
550     return (-ENOMEM);
551
552   e->value.mv_double = value;
553   e->type = MD_TYPE_DOUBLE;
554
555   return (md_entry_insert (md, e));
556 } /* }}} int meta_data_add_double */
557
558 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
559     const char *key, _Bool value)
560 {
561   meta_entry_t *e;
562
563   if ((md == NULL) || (key == NULL))
564     return (-EINVAL);
565
566   e = md_entry_alloc (key);
567   if (e == NULL)
568     return (-ENOMEM);
569
570   e->value.mv_boolean = value;
571   e->type = MD_TYPE_BOOLEAN;
572
573   return (md_entry_insert (md, e));
574 } /* }}} int meta_data_add_boolean */
575
576 /*
577  * Get functions
578  */
579 int meta_data_get_string (meta_data_t *md, /* {{{ */
580     const char *key, char **value)
581 {
582   meta_entry_t *e;
583   char *temp;
584
585   if ((md == NULL) || (key == NULL) || (value == NULL))
586     return (-EINVAL);
587
588   pthread_mutex_lock (&md->lock);
589
590   e = md_entry_lookup (md, key);
591   if (e == NULL)
592   {
593     pthread_mutex_unlock (&md->lock);
594     return (-ENOENT);
595   }
596
597   if (e->type != MD_TYPE_STRING)
598   {
599     ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
600     pthread_mutex_unlock (&md->lock);
601     return (-ENOENT);
602   }
603
604   temp = md_strdup (e->value.mv_string);
605   if (temp == NULL)
606   {
607     pthread_mutex_unlock (&md->lock);
608     ERROR ("meta_data_get_string: md_strdup failed.");
609     return (-ENOMEM);
610   }
611
612   pthread_mutex_unlock (&md->lock);
613
614   *value = temp;
615
616   return (0);
617 } /* }}} int meta_data_get_string */
618
619 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
620     const char *key, int64_t *value)
621 {
622   meta_entry_t *e;
623
624   if ((md == NULL) || (key == NULL) || (value == NULL))
625     return (-EINVAL);
626
627   pthread_mutex_lock (&md->lock);
628
629   e = md_entry_lookup (md, key);
630   if (e == NULL)
631   {
632     pthread_mutex_unlock (&md->lock);
633     return (-ENOENT);
634   }
635
636   if (e->type != MD_TYPE_SIGNED_INT)
637   {
638     ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
639     pthread_mutex_unlock (&md->lock);
640     return (-ENOENT);
641   }
642
643   *value = e->value.mv_signed_int;
644
645   pthread_mutex_unlock (&md->lock);
646   return (0);
647 } /* }}} int meta_data_get_signed_int */
648
649 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
650     const char *key, uint64_t *value)
651 {
652   meta_entry_t *e;
653
654   if ((md == NULL) || (key == NULL) || (value == NULL))
655     return (-EINVAL);
656
657   pthread_mutex_lock (&md->lock);
658
659   e = md_entry_lookup (md, key);
660   if (e == NULL)
661   {
662     pthread_mutex_unlock (&md->lock);
663     return (-ENOENT);
664   }
665
666   if (e->type != MD_TYPE_UNSIGNED_INT)
667   {
668     ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
669     pthread_mutex_unlock (&md->lock);
670     return (-ENOENT);
671   }
672
673   *value = e->value.mv_unsigned_int;
674
675   pthread_mutex_unlock (&md->lock);
676   return (0);
677 } /* }}} int meta_data_get_unsigned_int */
678
679 int meta_data_get_double (meta_data_t *md, /* {{{ */
680     const char *key, double *value)
681 {
682   meta_entry_t *e;
683
684   if ((md == NULL) || (key == NULL) || (value == NULL))
685     return (-EINVAL);
686
687   pthread_mutex_lock (&md->lock);
688
689   e = md_entry_lookup (md, key);
690   if (e == NULL)
691   {
692     pthread_mutex_unlock (&md->lock);
693     return (-ENOENT);
694   }
695
696   if (e->type != MD_TYPE_DOUBLE)
697   {
698     ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
699     pthread_mutex_unlock (&md->lock);
700     return (-ENOENT);
701   }
702
703   *value = e->value.mv_double;
704
705   pthread_mutex_unlock (&md->lock);
706   return (0);
707 } /* }}} int meta_data_get_double */
708
709 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
710     const char *key, _Bool *value)
711 {
712   meta_entry_t *e;
713
714   if ((md == NULL) || (key == NULL) || (value == NULL))
715     return (-EINVAL);
716
717   pthread_mutex_lock (&md->lock);
718
719   e = md_entry_lookup (md, key);
720   if (e == NULL)
721   {
722     pthread_mutex_unlock (&md->lock);
723     return (-ENOENT);
724   }
725
726   if (e->type != MD_TYPE_BOOLEAN)
727   {
728     ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
729     pthread_mutex_unlock (&md->lock);
730     return (-ENOENT);
731   }
732
733   *value = e->value.mv_boolean;
734
735   pthread_mutex_unlock (&md->lock);
736   return (0);
737 } /* }}} int meta_data_get_boolean */
738
739 /* vim: set sw=2 sts=2 et fdm=marker : */