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