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