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