Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / meta_data.c
1 /**
2  * collectd - src/meta_data.c
3  * Copyright (C) 2008  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 /*
23  * First tell the compiler to stick to the C99 and POSIX standards as close as
24  * possible.
25  */
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
28 #endif
29
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
32 #endif
33
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
36 #endif
37 #define _POSIX_C_SOURCE 200112L
38
39 #if 0
40 /* Single UNIX needed for strdup. */
41 #ifdef _XOPEN_SOURCE
42 # undef _XOPEN_SOURCE
43 #endif
44 #define _XOPEN_SOURCE 500
45 #endif
46
47 #ifndef _REENTRANT
48 # define _REENTRANT
49 #endif
50
51 #ifndef _THREAD_SAFE
52 # define _THREAD_SAFE
53 #endif
54
55 #ifdef _GNU_SOURCE
56 # undef _GNU_SOURCE
57 #endif
58 /* }}} */
59
60 #include "collectd.h"
61 #include "plugin.h"
62 #include "meta_data.h"
63
64 #include <pthread.h>
65
66 /*
67  * Defines
68  */
69 #define MD_TYPE_STRING       1
70 #define MD_TYPE_SIGNED_INT   2
71 #define MD_TYPE_UNSIGNED_INT 3
72 #define MD_TYPE_DOUBLE       4
73
74 /*
75  * Data types
76  */
77 union meta_value_u
78 {
79   char    *mv_string;
80   int64_t  mv_signed_int;
81   uint64_t mv_unsigned_int;
82   double   mv_double;
83 };
84 typedef union meta_value_u meta_value_t;
85
86 struct meta_entry_s;
87 typedef struct meta_entry_s meta_entry_t;
88 struct meta_entry_s
89 {
90   char         *key;
91   meta_value_t  value;
92   int           type;
93   meta_entry_t *next;
94 };
95
96 struct meta_data_s
97 {
98   meta_entry_t   *head;
99   pthread_mutex_t lock;
100 };
101
102 /*
103  * Private functions
104  */
105 static char *md_strdup (const char *orig) /* {{{ */
106 {
107   size_t sz;
108   char *dest;
109
110   if (orig == NULL)
111     return (NULL);
112
113   sz = strlen (orig) + 1;
114   dest = (char *) malloc (sz);
115   if (dest == NULL)
116     return (NULL);
117
118   memcpy (dest, orig, sz);
119
120   return (dest);
121 } /* }}} char *md_strdup */
122
123 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
124 {
125   meta_entry_t *e;
126
127   e = (meta_entry_t *) malloc (sizeof (*e));
128   if (e == NULL)
129   {
130     ERROR ("md_entry_alloc: malloc failed.");
131     return (NULL);
132   }
133   memset (e, 0, sizeof (*e));
134
135   e->key = md_strdup (key);
136   if (e->key == NULL)
137   {
138     free (e);
139     ERROR ("md_entry_alloc: md_strdup failed.");
140     return (NULL);
141   }
142
143   e->type = 0;
144   e->next = NULL;
145
146   return (e);
147 } /* }}} meta_entry_t *md_entry_alloc */
148
149 static void md_entry_free (meta_entry_t *e) /* {{{ */
150 {
151   if (e == NULL)
152     return;
153
154   free (e->key);
155
156   if (e->type == MD_TYPE_STRING)
157     free (e->value.mv_string);
158
159   if (e->next != NULL)
160     md_entry_free (e->next);
161
162   free (e);
163 } /* }}} void md_entry_free */
164
165 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
166 {
167   meta_entry_t *this;
168   meta_entry_t *prev;
169
170   if ((md == NULL) || (e == NULL))
171     return (-EINVAL);
172
173   pthread_mutex_lock (&md->lock);
174
175   prev = NULL;
176   this = md->head;
177   while (this != NULL)
178   {
179     if (strcasecmp (e->key, this->key) == 0)
180       break;
181
182     prev = this;
183     this = this->next;
184   }
185
186   if (this == NULL)
187   {
188     /* This key does not exist yet. */
189     if (md->head == NULL)
190       md->head = e;
191     else
192     {
193       assert (prev != NULL);
194       prev->next = e;
195     }
196
197     e->next = NULL;
198   }
199   else /* (this != NULL) */
200   {
201     if (prev == NULL)
202       md->head = e;
203     else
204       prev->next = e;
205
206     e->next = this->next;
207   }
208
209   pthread_mutex_unlock (&md->lock);
210
211   if (this != NULL)
212   {
213     this->next = NULL;
214     md_entry_free (this);
215   }
216
217   return (0);
218 } /* }}} int md_entry_insert */
219
220 /* XXX: The lock on md must be held while calling this function! */
221 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
222     const char *key)
223 {
224   meta_entry_t *e;
225
226   if ((md == NULL) || (key == NULL))
227     return (NULL);
228
229   for (e = md->head; e != NULL; e = e->next)
230     if (strcasecmp (key, e->key) == 0)
231       break;
232
233   return (e);
234 } /* }}} meta_entry_t *md_entry_lookup */
235
236 /*
237  * Public functions
238  */
239 meta_data_t *meta_data_create (void) /* {{{ */
240 {
241   meta_data_t *md;
242
243   md = (meta_data_t *) malloc (sizeof (*md));
244   if (md == NULL)
245   {
246     ERROR ("meta_data_create: malloc failed.");
247     return (NULL);
248   }
249   memset (md, 0, sizeof (*md));
250
251   md->head = NULL;
252   pthread_mutex_init (&md->lock, /* attr = */ NULL);
253
254   return (md);
255 } /* }}} meta_data_t *meta_data_create */
256
257 void meta_data_destroy (meta_data_t *md) /* {{{ */
258 {
259   if (md == NULL)
260     return;
261
262   md_entry_free (md->head);
263   free (md);
264 } /* }}} void meta_data_destroy */
265
266 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
267 {
268   meta_entry_t *e;
269
270   if ((md == NULL) || (key == NULL))
271     return (-EINVAL);
272
273   pthread_mutex_lock (&md->lock);
274
275   for (e = md->head; e != NULL; e = e->next)
276   {
277     if (strcasecmp (key, e->key) == 0)
278     {
279       pthread_mutex_unlock (&md->lock);
280       return (1);
281     }
282   }
283
284   pthread_mutex_unlock (&md->lock);
285   return (0);
286 } /* }}} int meta_data_exists */
287
288 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
289 {
290   meta_entry_t *this;
291   meta_entry_t *prev;
292
293   if ((md == NULL) || (key == NULL))
294     return (-EINVAL);
295
296   pthread_mutex_lock (&md->lock);
297
298   prev = NULL;
299   this = md->head;
300   while (this != NULL)
301   {
302     if (strcasecmp (key, this->key) == 0)
303       break;
304
305     prev = this;
306     this = this->next;
307   }
308
309   if (this == NULL)
310   {
311     pthread_mutex_unlock (&md->lock);
312     return (-ENOENT);
313   }
314
315   if (prev == NULL)
316     md->head = this->next;
317   else
318     prev->next = this->next;
319
320   pthread_mutex_unlock (&md->lock);
321
322   this->next = NULL;
323   md_entry_free (this);
324
325   return (0);
326 } /* }}} int meta_data_delete */
327
328 int meta_data_add_string (meta_data_t *md, /* {{{ */
329     const char *key, const char *value)
330 {
331   meta_entry_t *e;
332
333   if ((md == NULL) || (key == NULL) || (value == NULL))
334     return (-EINVAL);
335
336   e = md_entry_alloc (key);
337   if (e == NULL)
338     return (-ENOMEM);
339
340   e->value.mv_string = md_strdup (value);
341   if (e->value.mv_string == NULL)
342   {
343     ERROR ("meta_data_add_string: md_strdup failed.");
344     md_entry_free (e);
345     return (-ENOMEM);
346   }
347   e->type = MD_TYPE_STRING;
348
349   return (md_entry_insert (md, e));
350 } /* }}} int meta_data_add_string */
351
352 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
353     const char *key, int64_t value)
354 {
355   meta_entry_t *e;
356
357   if ((md == NULL) || (key == NULL))
358     return (-EINVAL);
359
360   e = md_entry_alloc (key);
361   if (e == NULL)
362     return (-ENOMEM);
363
364   e->value.mv_signed_int = value;
365   e->type = MD_TYPE_SIGNED_INT;
366
367   return (md_entry_insert (md, e));
368 } /* }}} int meta_data_add_signed_int */
369
370 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
371     const char *key, uint64_t value)
372 {
373   meta_entry_t *e;
374
375   if ((md == NULL) || (key == NULL))
376     return (-EINVAL);
377
378   e = md_entry_alloc (key);
379   if (e == NULL)
380     return (-ENOMEM);
381
382   e->value.mv_unsigned_int = value;
383   e->type = MD_TYPE_UNSIGNED_INT;
384
385   return (md_entry_insert (md, e));
386 } /* }}} int meta_data_add_unsigned_int */
387
388 int meta_data_add_double (meta_data_t *md, /* {{{ */
389     const char *key, double value)
390 {
391   meta_entry_t *e;
392
393   if ((md == NULL) || (key == NULL))
394     return (-EINVAL);
395
396   e = md_entry_alloc (key);
397   if (e == NULL)
398     return (-ENOMEM);
399
400   e->value.mv_double = value;
401   e->type = MD_TYPE_DOUBLE;
402
403   return (md_entry_insert (md, e));
404 } /* }}} int meta_data_add_double */
405
406 int meta_data_get_string (meta_data_t *md, /* {{{ */
407     const char *key, char **value)
408 {
409   meta_entry_t *e;
410   char *temp;
411
412   if ((md == NULL) || (key == NULL) || (value == NULL))
413     return (-EINVAL);
414
415   pthread_mutex_lock (&md->lock);
416
417   e = md_entry_lookup (md, key);
418   if (e == NULL)
419   {
420     pthread_mutex_unlock (&md->lock);
421     return (-ENOENT);
422   }
423
424   if (e->type != MD_TYPE_SIGNED_INT)
425   {
426     ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
427     pthread_mutex_unlock (&md->lock);
428     return (-ENOENT);
429   }
430
431   temp = md_strdup (e->value.mv_string);
432   if (temp == NULL)
433   {
434     pthread_mutex_unlock (&md->lock);
435     ERROR ("meta_data_get_string: md_strdup failed.");
436     return (-ENOMEM);
437   }
438  
439   pthread_mutex_unlock (&md->lock);
440
441   *value = temp;
442
443   return (0);
444 } /* }}} int meta_data_get_string */
445
446 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
447     const char *key, int64_t *value)
448 {
449   meta_entry_t *e;
450
451   if ((md == NULL) || (key == NULL) || (value == NULL))
452     return (-EINVAL);
453
454   pthread_mutex_lock (&md->lock);
455
456   e = md_entry_lookup (md, key);
457   if (e == NULL)
458   {
459     pthread_mutex_unlock (&md->lock);
460     return (-ENOENT);
461   }
462
463   if (e->type != MD_TYPE_SIGNED_INT)
464   {
465     ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
466     pthread_mutex_unlock (&md->lock);
467     return (-ENOENT);
468   }
469
470   *value = e->value.mv_signed_int;
471
472   pthread_mutex_unlock (&md->lock);
473   return (0);
474 } /* }}} int meta_data_get_signed_int */
475
476 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
477     const char *key, uint64_t *value)
478 {
479   meta_entry_t *e;
480
481   if ((md == NULL) || (key == NULL) || (value == NULL))
482     return (-EINVAL);
483
484   pthread_mutex_lock (&md->lock);
485
486   e = md_entry_lookup (md, key);
487   if (e == NULL)
488   {
489     pthread_mutex_unlock (&md->lock);
490     return (-ENOENT);
491   }
492
493   if (e->type != MD_TYPE_UNSIGNED_INT)
494   {
495     ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
496     pthread_mutex_unlock (&md->lock);
497     return (-ENOENT);
498   }
499
500   *value = e->value.mv_unsigned_int;
501
502   pthread_mutex_unlock (&md->lock);
503   return (0);
504 } /* }}} int meta_data_get_unsigned_int */
505
506 int meta_data_get_double (meta_data_t *md, /* {{{ */
507     const char *key, double *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_DOUBLE)
524   {
525     ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
526     pthread_mutex_unlock (&md->lock);
527     return (-ENOENT);
528   }
529
530   *value = e->value.mv_double;
531
532   pthread_mutex_unlock (&md->lock);
533   return (0);
534 } /* }}} int meta_data_get_double */
535
536 /* vim: set sw=2 sts=2 et fdm=marker : */