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