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   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   copy->next = md_entry_clone (orig->next);
126   return (copy);
127 } /* }}} meta_entry_t *md_entry_clone */
128
129 static void md_entry_free (meta_entry_t *e) /* {{{ */
130 {
131   if (e == NULL)
132     return;
133
134   free (e->key);
135
136   if (e->type == MD_TYPE_STRING)
137     free (e->value.mv_string);
138
139   if (e->next != NULL)
140     md_entry_free (e->next);
141
142   free (e);
143 } /* }}} void md_entry_free */
144
145 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
146 {
147   meta_entry_t *this;
148   meta_entry_t *prev;
149
150   if ((md == NULL) || (e == NULL))
151     return (-EINVAL);
152
153   pthread_mutex_lock (&md->lock);
154
155   prev = NULL;
156   this = md->head;
157   while (this != NULL)
158   {
159     if (strcasecmp (e->key, this->key) == 0)
160       break;
161
162     prev = this;
163     this = this->next;
164   }
165
166   if (this == NULL)
167   {
168     /* This key does not exist yet. */
169     if (md->head == NULL)
170       md->head = e;
171     else
172     {
173       assert (prev != NULL);
174       prev->next = e;
175     }
176
177     e->next = NULL;
178   }
179   else /* (this != NULL) */
180   {
181     if (prev == NULL)
182       md->head = e;
183     else
184       prev->next = e;
185
186     e->next = this->next;
187   }
188
189   pthread_mutex_unlock (&md->lock);
190
191   if (this != NULL)
192   {
193     this->next = NULL;
194     md_entry_free (this);
195   }
196
197   return (0);
198 } /* }}} int md_entry_insert */
199
200 /* XXX: The lock on md must be held while calling this function! */
201 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
202     const char *key)
203 {
204   meta_entry_t *e;
205
206   if ((md == NULL) || (key == NULL))
207     return (NULL);
208
209   for (e = md->head; e != NULL; e = e->next)
210     if (strcasecmp (key, e->key) == 0)
211       break;
212
213   return (e);
214 } /* }}} meta_entry_t *md_entry_lookup */
215
216 /*
217  * Each value_list_t*, as it is going through the system, is handled by exactly
218  * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
219  * rrdtool plugin, must create a copy first. The meta data within a
220  * value_list_t* is not thread safe and doesn't need to be.
221  *
222  * The meta data associated with cache entries are a different story. There, we
223  * need to ensure exclusive locking to prevent leaks and other funky business.
224  * This is ensured by the uc_meta_data_get_*() functions.
225  */
226
227 /*
228  * Public functions
229  */
230 meta_data_t *meta_data_create (void) /* {{{ */
231 {
232   meta_data_t *md;
233
234   md = (meta_data_t *) malloc (sizeof (*md));
235   if (md == NULL)
236   {
237     ERROR ("meta_data_create: malloc failed.");
238     return (NULL);
239   }
240   memset (md, 0, sizeof (*md));
241
242   md->head = NULL;
243   pthread_mutex_init (&md->lock, /* attr = */ NULL);
244
245   return (md);
246 } /* }}} meta_data_t *meta_data_create */
247
248 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
249 {
250   meta_data_t *copy;
251
252   if (orig == NULL)
253     return (NULL);
254
255   copy = meta_data_create ();
256   if (copy == NULL)
257     return (NULL);
258
259   pthread_mutex_lock (&orig->lock);
260   copy->head = md_entry_clone (orig->head);
261   pthread_mutex_unlock (&orig->lock);
262
263   return (copy);
264 } /* }}} meta_data_t *meta_data_clone */
265
266 void meta_data_destroy (meta_data_t *md) /* {{{ */
267 {
268   if (md == NULL)
269     return;
270
271   md_entry_free (md->head);
272   pthread_mutex_destroy (&md->lock);
273   free (md);
274 } /* }}} void meta_data_destroy */
275
276 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
277 {
278   meta_entry_t *e;
279
280   if ((md == NULL) || (key == NULL))
281     return (-EINVAL);
282
283   pthread_mutex_lock (&md->lock);
284
285   for (e = md->head; e != NULL; e = e->next)
286   {
287     if (strcasecmp (key, e->key) == 0)
288     {
289       pthread_mutex_unlock (&md->lock);
290       return (1);
291     }
292   }
293
294   pthread_mutex_unlock (&md->lock);
295   return (0);
296 } /* }}} int meta_data_exists */
297
298 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
299 {
300   meta_entry_t *e;
301
302   if ((md == NULL) || (key == NULL))
303     return -EINVAL;
304
305   pthread_mutex_lock (&md->lock);
306
307   for (e = md->head; e != NULL; e = e->next)
308   {
309     if (strcasecmp (key, e->key) == 0)
310     {
311       pthread_mutex_unlock (&md->lock);
312       return e->type;
313     }
314   }
315
316   pthread_mutex_unlock (&md->lock);
317   return 0;
318 } /* }}} int meta_data_type */
319
320 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
321 {
322   int i = 0, count = 0;
323   meta_entry_t *e;
324
325   if ((md == NULL) || (toc == NULL))
326     return -EINVAL;
327
328   pthread_mutex_lock (&md->lock);
329
330   for (e = md->head; e != NULL; e = e->next)
331     ++count;    
332
333   if (count == 0)
334   {
335     pthread_mutex_unlock (&md->lock);
336     return (count);
337   }
338
339   *toc = calloc(count, sizeof(**toc));
340   for (e = md->head; e != NULL; e = e->next)
341     (*toc)[i++] = strdup(e->key);
342   
343   pthread_mutex_unlock (&md->lock);
344   return count;
345 } /* }}} int meta_data_toc */
346
347 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
348 {
349   meta_entry_t *this;
350   meta_entry_t *prev;
351
352   if ((md == NULL) || (key == NULL))
353     return (-EINVAL);
354
355   pthread_mutex_lock (&md->lock);
356
357   prev = NULL;
358   this = md->head;
359   while (this != NULL)
360   {
361     if (strcasecmp (key, this->key) == 0)
362       break;
363
364     prev = this;
365     this = this->next;
366   }
367
368   if (this == NULL)
369   {
370     pthread_mutex_unlock (&md->lock);
371     return (-ENOENT);
372   }
373
374   if (prev == NULL)
375     md->head = this->next;
376   else
377     prev->next = this->next;
378
379   pthread_mutex_unlock (&md->lock);
380
381   this->next = NULL;
382   md_entry_free (this);
383
384   return (0);
385 } /* }}} int meta_data_delete */
386
387 /*
388  * Add functions
389  */
390 int meta_data_add_string (meta_data_t *md, /* {{{ */
391     const char *key, const char *value)
392 {
393   meta_entry_t *e;
394
395   if ((md == NULL) || (key == NULL) || (value == NULL))
396     return (-EINVAL);
397
398   e = md_entry_alloc (key);
399   if (e == NULL)
400     return (-ENOMEM);
401
402   e->value.mv_string = md_strdup (value);
403   if (e->value.mv_string == NULL)
404   {
405     ERROR ("meta_data_add_string: md_strdup failed.");
406     md_entry_free (e);
407     return (-ENOMEM);
408   }
409   e->type = MD_TYPE_STRING;
410
411   return (md_entry_insert (md, e));
412 } /* }}} int meta_data_add_string */
413
414 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
415     const char *key, int64_t value)
416 {
417   meta_entry_t *e;
418
419   if ((md == NULL) || (key == NULL))
420     return (-EINVAL);
421
422   e = md_entry_alloc (key);
423   if (e == NULL)
424     return (-ENOMEM);
425
426   e->value.mv_signed_int = value;
427   e->type = MD_TYPE_SIGNED_INT;
428
429   return (md_entry_insert (md, e));
430 } /* }}} int meta_data_add_signed_int */
431
432 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
433     const char *key, uint64_t value)
434 {
435   meta_entry_t *e;
436
437   if ((md == NULL) || (key == NULL))
438     return (-EINVAL);
439
440   e = md_entry_alloc (key);
441   if (e == NULL)
442     return (-ENOMEM);
443
444   e->value.mv_unsigned_int = value;
445   e->type = MD_TYPE_UNSIGNED_INT;
446
447   return (md_entry_insert (md, e));
448 } /* }}} int meta_data_add_unsigned_int */
449
450 int meta_data_add_double (meta_data_t *md, /* {{{ */
451     const char *key, double value)
452 {
453   meta_entry_t *e;
454
455   if ((md == NULL) || (key == NULL))
456     return (-EINVAL);
457
458   e = md_entry_alloc (key);
459   if (e == NULL)
460     return (-ENOMEM);
461
462   e->value.mv_double = value;
463   e->type = MD_TYPE_DOUBLE;
464
465   return (md_entry_insert (md, e));
466 } /* }}} int meta_data_add_double */
467
468 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
469     const char *key, _Bool value)
470 {
471   meta_entry_t *e;
472
473   if ((md == NULL) || (key == NULL))
474     return (-EINVAL);
475
476   e = md_entry_alloc (key);
477   if (e == NULL)
478     return (-ENOMEM);
479
480   e->value.mv_boolean = value;
481   e->type = MD_TYPE_BOOLEAN;
482
483   return (md_entry_insert (md, e));
484 } /* }}} int meta_data_add_boolean */
485
486 /*
487  * Get functions
488  */
489 int meta_data_get_string (meta_data_t *md, /* {{{ */
490     const char *key, char **value)
491 {
492   meta_entry_t *e;
493   char *temp;
494
495   if ((md == NULL) || (key == NULL) || (value == NULL))
496     return (-EINVAL);
497
498   pthread_mutex_lock (&md->lock);
499
500   e = md_entry_lookup (md, key);
501   if (e == NULL)
502   {
503     pthread_mutex_unlock (&md->lock);
504     return (-ENOENT);
505   }
506
507   if (e->type != MD_TYPE_STRING)
508   {
509     ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
510     pthread_mutex_unlock (&md->lock);
511     return (-ENOENT);
512   }
513
514   temp = md_strdup (e->value.mv_string);
515   if (temp == NULL)
516   {
517     pthread_mutex_unlock (&md->lock);
518     ERROR ("meta_data_get_string: md_strdup failed.");
519     return (-ENOMEM);
520   }
521  
522   pthread_mutex_unlock (&md->lock);
523
524   *value = temp;
525
526   return (0);
527 } /* }}} int meta_data_get_string */
528
529 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
530     const char *key, int64_t *value)
531 {
532   meta_entry_t *e;
533
534   if ((md == NULL) || (key == NULL) || (value == NULL))
535     return (-EINVAL);
536
537   pthread_mutex_lock (&md->lock);
538
539   e = md_entry_lookup (md, key);
540   if (e == NULL)
541   {
542     pthread_mutex_unlock (&md->lock);
543     return (-ENOENT);
544   }
545
546   if (e->type != MD_TYPE_SIGNED_INT)
547   {
548     ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
549     pthread_mutex_unlock (&md->lock);
550     return (-ENOENT);
551   }
552
553   *value = e->value.mv_signed_int;
554
555   pthread_mutex_unlock (&md->lock);
556   return (0);
557 } /* }}} int meta_data_get_signed_int */
558
559 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
560     const char *key, uint64_t *value)
561 {
562   meta_entry_t *e;
563
564   if ((md == NULL) || (key == NULL) || (value == NULL))
565     return (-EINVAL);
566
567   pthread_mutex_lock (&md->lock);
568
569   e = md_entry_lookup (md, key);
570   if (e == NULL)
571   {
572     pthread_mutex_unlock (&md->lock);
573     return (-ENOENT);
574   }
575
576   if (e->type != MD_TYPE_UNSIGNED_INT)
577   {
578     ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
579     pthread_mutex_unlock (&md->lock);
580     return (-ENOENT);
581   }
582
583   *value = e->value.mv_unsigned_int;
584
585   pthread_mutex_unlock (&md->lock);
586   return (0);
587 } /* }}} int meta_data_get_unsigned_int */
588
589 int meta_data_get_double (meta_data_t *md, /* {{{ */
590     const char *key, double *value)
591 {
592   meta_entry_t *e;
593
594   if ((md == NULL) || (key == NULL) || (value == NULL))
595     return (-EINVAL);
596
597   pthread_mutex_lock (&md->lock);
598
599   e = md_entry_lookup (md, key);
600   if (e == NULL)
601   {
602     pthread_mutex_unlock (&md->lock);
603     return (-ENOENT);
604   }
605
606   if (e->type != MD_TYPE_DOUBLE)
607   {
608     ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
609     pthread_mutex_unlock (&md->lock);
610     return (-ENOENT);
611   }
612
613   *value = e->value.mv_double;
614
615   pthread_mutex_unlock (&md->lock);
616   return (0);
617 } /* }}} int meta_data_get_double */
618
619 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
620     const char *key, _Bool *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_BOOLEAN)
637   {
638     ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
639     pthread_mutex_unlock (&md->lock);
640     return (-ENOENT);
641   }
642
643   *value = e->value.mv_boolean;
644
645   pthread_mutex_unlock (&md->lock);
646   return (0);
647 } /* }}} int meta_data_get_boolean */
648
649 /* vim: set sw=2 sts=2 et fdm=marker : */