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