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