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