Merge branch 'st/python'
[collectd.git] / src / plugin.c
1 /**
2  * collectd - src/plugin.c
3  * Copyright (C) 2005-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  *   Sebastian Harl <sh at tokkee.org>
21  **/
22
23 #include "collectd.h"
24 #include "utils_complain.h"
25
26 #include <ltdl.h>
27
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
31
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_cache.h"
39 #include "utils_threshold.h"
40 #include "filter_chain.h"
41
42 /*
43  * Private structures
44  */
45 struct callback_func_s
46 {
47         void *cf_callback;
48         user_data_t cf_udata;
49 };
50 typedef struct callback_func_s callback_func_t;
51
52 #define RF_SIMPLE  0
53 #define RF_COMPLEX 1
54 #define RF_REMOVE  65535
55 struct read_func_s
56 {
57         /* `read_func_t' "inherits" from `callback_func_t'.
58          * The `rf_super' member MUST be the first one in this structure! */
59 #define rf_callback rf_super.cf_callback
60 #define rf_udata rf_super.cf_udata
61         callback_func_t rf_super;
62         char rf_name[DATA_MAX_NAME_LEN];
63         int rf_type;
64         struct timespec rf_interval;
65         struct timespec rf_effective_interval;
66         struct timespec rf_next_read;
67 };
68 typedef struct read_func_s read_func_t;
69
70 /*
71  * Private variables
72  */
73 static llist_t *list_init;
74 static llist_t *list_write;
75 static llist_t *list_flush;
76 static llist_t *list_shutdown;
77 static llist_t *list_log;
78 static llist_t *list_notification;
79
80 static fc_chain_t *pre_cache_chain = NULL;
81 static fc_chain_t *post_cache_chain = NULL;
82
83 static c_avl_tree_t *data_sets;
84
85 static char *plugindir = NULL;
86
87 static c_heap_t       *read_heap = NULL;
88 static llist_t        *read_list;
89 static int             read_loop = 1;
90 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
91 static pthread_cond_t  read_cond = PTHREAD_COND_INITIALIZER;
92 static pthread_t      *read_threads = NULL;
93 static int             read_threads_num = 0;
94
95 /*
96  * Static functions
97  */
98 static const char *plugin_get_dir (void)
99 {
100         if (plugindir == NULL)
101                 return (PLUGINDIR);
102         else
103                 return (plugindir);
104 }
105
106 static void destroy_callback (callback_func_t *cf) /* {{{ */
107 {
108         if (cf == NULL)
109                 return;
110
111         if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
112         {
113                 cf->cf_udata.free_func (cf->cf_udata.data);
114                 cf->cf_udata.data = NULL;
115                 cf->cf_udata.free_func = NULL;
116         }
117         sfree (cf);
118 } /* }}} void destroy_callback */
119
120 static void destroy_all_callbacks (llist_t **list) /* {{{ */
121 {
122         llentry_t *le;
123
124         if (*list == NULL)
125                 return;
126
127         le = llist_head (*list);
128         while (le != NULL)
129         {
130                 llentry_t *le_next;
131
132                 le_next = le->next;
133
134                 sfree (le->key);
135                 destroy_callback (le->value);
136                 le->value = NULL;
137
138                 le = le_next;
139         }
140
141         llist_destroy (*list);
142         *list = NULL;
143 } /* }}} void destroy_all_callbacks */
144
145 static void destroy_read_heap (void) /* {{{ */
146 {
147         if (read_heap == NULL)
148                 return;
149
150         while (42)
151         {
152                 callback_func_t *cf;
153
154                 cf = c_head_get_root (read_heap);
155                 if (cf == NULL)
156                         break;
157
158                 destroy_callback (cf);
159         }
160
161         c_heap_destroy (read_heap);
162         read_heap = NULL;
163 } /* }}} void destroy_read_heap */
164
165 static int register_callback (llist_t **list, /* {{{ */
166                 const char *name, callback_func_t *cf)
167 {
168         llentry_t *le;
169         char *key;
170
171         if (*list == NULL)
172         {
173                 *list = llist_create ();
174                 if (*list == NULL)
175                 {
176                         ERROR ("plugin: create_register_callback: "
177                                         "llist_create failed.");
178                         destroy_callback (cf);
179                         return (-1);
180                 }
181         }
182
183         key = strdup (name);
184         if (key == NULL)
185         {
186                 ERROR ("plugin: create_register_callback: strdup failed.");
187                 destroy_callback (cf);
188                 return (-1);
189         }
190
191         le = llist_search (*list, name);
192         if (le == NULL)
193         {
194                 le = llentry_create (key, cf);
195                 if (le == NULL)
196                 {
197                         ERROR ("plugin: create_register_callback: "
198                                         "llentry_create failed.");
199                         free (key);
200                         destroy_callback (cf);
201                         return (-1);
202                 }
203
204                 llist_append (*list, le);
205         }
206         else
207         {
208                 callback_func_t *old_cf;
209
210                 old_cf = le->value;
211                 le->value = cf;
212
213                 destroy_callback (old_cf);
214                 sfree (key);
215         }
216
217         return (0);
218 } /* }}} int register_callback */
219
220 static int create_register_callback (llist_t **list, /* {{{ */
221                 const char *name, void *callback, user_data_t *ud)
222 {
223         callback_func_t *cf;
224
225         cf = (callback_func_t *) malloc (sizeof (*cf));
226         if (cf == NULL)
227         {
228                 ERROR ("plugin: create_register_callback: malloc failed.");
229                 return (-1);
230         }
231         memset (cf, 0, sizeof (*cf));
232
233         cf->cf_callback = callback;
234         if (ud == NULL)
235         {
236                 cf->cf_udata.data = NULL;
237                 cf->cf_udata.free_func = NULL;
238         }
239         else
240         {
241                 cf->cf_udata = *ud;
242         }
243
244         return (register_callback (list, name, cf));
245 } /* }}} int create_register_callback */
246
247 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
248 {
249         llentry_t *e;
250
251         if (list == NULL)
252                 return (-1);
253
254         e = llist_search (list, name);
255         if (e == NULL)
256                 return (-1);
257
258         llist_remove (list, e);
259
260         sfree (e->key);
261         destroy_callback (e->value);
262
263         llentry_destroy (e);
264
265         return (0);
266 } /* }}} int plugin_unregister */
267
268 /*
269  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
270  * object, but it will bitch about a shared object not having a
271  * ``module_register'' symbol..
272  */
273 static int plugin_load_file (char *file, uint32_t flags)
274 {
275         lt_dlhandle dlh;
276         void (*reg_handle) (void);
277
278         DEBUG ("file = %s", file);
279
280         lt_dlinit ();
281         lt_dlerror (); /* clear errors */
282
283 #if LIBTOOL_VERSION == 2
284         if (flags & PLUGIN_FLAGS_GLOBAL) {
285                 lt_dladvise advise;
286                 lt_dladvise_init(&advise);
287                 lt_dladvise_global(&advise);
288                 dlh = lt_dlopenadvise(file, advise);
289                 lt_dladvise_destroy(&advise);
290         } else {
291                 dlh = lt_dlopen (file);
292         }
293 #else /* if LIBTOOL_VERSION == 1 */
294         if (flags & PLUGIN_FLAGS_GLOBAL)
295                 ERROR ("plugin_load_file: The global flag is not supported, "
296                                 "libtool 2 is required for this.");
297         dlh = lt_dlopen (file);
298 #endif
299
300         if (dlh == NULL)
301         {
302                 const char *error = lt_dlerror ();
303
304                 ERROR ("lt_dlopen (%s) failed: %s", file, error);
305                 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
306                 return (1);
307         }
308
309         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
310         {
311                 WARNING ("Couldn't find symbol `module_register' in `%s': %s\n",
312                                 file, lt_dlerror ());
313                 lt_dlclose (dlh);
314                 return (-1);
315         }
316
317         (*reg_handle) ();
318
319         return (0);
320 }
321
322 static void *plugin_read_thread (void __attribute__((unused)) *args)
323 {
324         while (read_loop != 0)
325         {
326                 read_func_t *rf;
327                 struct timeval now;
328                 int status;
329                 int rf_type;
330
331                 /* Get the read function that needs to be read next. */
332                 rf = c_head_get_root (read_heap);
333                 if (rf == NULL)
334                 {
335                         struct timespec abstime;
336
337                         gettimeofday (&now, /* timezone = */ NULL);
338
339                         abstime.tv_sec = now.tv_sec + interval_g;
340                         abstime.tv_nsec = 1000 * now.tv_usec;
341
342                         pthread_mutex_lock (&read_lock);
343                         pthread_cond_timedwait (&read_cond, &read_lock,
344                                         &abstime);
345                         pthread_mutex_unlock (&read_lock);
346                         continue;
347                 }
348
349                 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
350                 {
351                         gettimeofday (&now, /* timezone = */ NULL);
352
353                         rf->rf_interval.tv_sec = interval_g;
354                         rf->rf_interval.tv_nsec = 0;
355
356                         rf->rf_effective_interval = rf->rf_interval;
357
358                         rf->rf_next_read.tv_sec = now.tv_sec;
359                         rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
360                 }
361
362                 /* sleep until this entry is due,
363                  * using pthread_cond_timedwait */
364                 pthread_mutex_lock (&read_lock);
365                 pthread_cond_timedwait (&read_cond, &read_lock,
366                                 &rf->rf_next_read);
367                 /* Must hold `real_lock' when accessing `rf->rf_type'. */
368                 rf_type = rf->rf_type;
369                 pthread_mutex_unlock (&read_lock);
370
371                 /* Check if we're supposed to stop.. This may have interrupted
372                  * the sleep, too. */
373                 if (read_loop == 0)
374                 {
375                         /* Insert `rf' again, so it can be free'd correctly */
376                         c_heap_insert (read_heap, rf);
377                         break;
378                 }
379
380                 /* The entry has been marked for deletion. The linked list
381                  * entry has already been removed by `plugin_unregister_read'.
382                  * All we have to do here is free the `read_func_t' and
383                  * continue. */
384                 if (rf_type == RF_REMOVE)
385                 {
386                         DEBUG ("plugin_read_thread: Destroying the `%s' "
387                                         "callback.", rf->rf_name);
388                         destroy_callback ((callback_func_t *) rf);
389                         rf = NULL;
390                         continue;
391                 }
392
393                 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
394
395                 if (rf_type == RF_SIMPLE)
396                 {
397                         int (*callback) (void);
398
399                         callback = rf->rf_callback;
400                         status = (*callback) ();
401                 }
402                 else
403                 {
404                         plugin_read_cb callback;
405
406                         assert (rf_type == RF_COMPLEX);
407
408                         callback = rf->rf_callback;
409                         status = (*callback) (&rf->rf_udata);
410                 }
411
412                 /* If the function signals failure, we will increase the
413                  * intervals in which it will be called. */
414                 if (status != 0)
415                 {
416                         rf->rf_effective_interval.tv_sec *= 2;
417                         rf->rf_effective_interval.tv_nsec *= 2;
418                         NORMALIZE_TIMESPEC (rf->rf_effective_interval);
419
420                         if (rf->rf_effective_interval.tv_sec >= 86400)
421                         {
422                                 rf->rf_effective_interval.tv_sec = 86400;
423                                 rf->rf_effective_interval.tv_nsec = 0;
424                         }
425
426                         NOTICE ("read-function of plugin `%s' failed. "
427                                         "Will suspend it for %i seconds.",
428                                         rf->rf_name,
429                                         (int) rf->rf_effective_interval.tv_sec);
430                 }
431                 else
432                 {
433                         /* Success: Restore the interval, if it was changed. */
434                         rf->rf_effective_interval = rf->rf_interval;
435                 }
436
437                 /* update the ``next read due'' field */
438                 gettimeofday (&now, /* timezone = */ NULL);
439
440                 DEBUG ("plugin_read_thread: Effective interval of the "
441                                 "%s plugin is %i.%09i.",
442                                 rf->rf_name,
443                                 (int) rf->rf_effective_interval.tv_sec,
444                                 (int) rf->rf_effective_interval.tv_nsec);
445
446                 /* Calculate the next (absolute) time at which this function
447                  * should be called. */
448                 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
449                         + rf->rf_effective_interval.tv_sec;
450                 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
451                         + rf->rf_effective_interval.tv_nsec;
452                 NORMALIZE_TIMESPEC (rf->rf_next_read);
453
454                 /* Check, if `rf_next_read' is in the past. */
455                 if ((rf->rf_next_read.tv_sec < now.tv_sec)
456                                 || ((rf->rf_next_read.tv_sec == now.tv_sec)
457                                         && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
458                 {
459                         /* `rf_next_read' is in the past. Insert `now'
460                          * so this value doesn't trail off into the
461                          * past too much. */
462                         rf->rf_next_read.tv_sec = now.tv_sec;
463                         rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
464                 }
465
466                 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
467                                 rf->rf_name,
468                                 (int) rf->rf_next_read.tv_sec,
469                                 (int) rf->rf_next_read.tv_nsec);
470
471                 /* Re-insert this read function into the heap again. */
472                 c_heap_insert (read_heap, rf);
473         } /* while (read_loop) */
474
475         pthread_exit (NULL);
476         return ((void *) 0);
477 } /* void *plugin_read_thread */
478
479 static void start_read_threads (int num)
480 {
481         int i;
482
483         if (read_threads != NULL)
484                 return;
485
486         read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
487         if (read_threads == NULL)
488         {
489                 ERROR ("plugin: start_read_threads: calloc failed.");
490                 return;
491         }
492
493         read_threads_num = 0;
494         for (i = 0; i < num; i++)
495         {
496                 if (pthread_create (read_threads + read_threads_num, NULL,
497                                         plugin_read_thread, NULL) == 0)
498                 {
499                         read_threads_num++;
500                 }
501                 else
502                 {
503                         ERROR ("plugin: start_read_threads: pthread_create failed.");
504                         return;
505                 }
506         } /* for (i) */
507 } /* void start_read_threads */
508
509 static void stop_read_threads (void)
510 {
511         int i;
512
513         if (read_threads == NULL)
514                 return;
515
516         INFO ("collectd: Stopping %i read threads.", read_threads_num);
517
518         pthread_mutex_lock (&read_lock);
519         read_loop = 0;
520         DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
521         pthread_cond_broadcast (&read_cond);
522         pthread_mutex_unlock (&read_lock);
523
524         for (i = 0; i < read_threads_num; i++)
525         {
526                 if (pthread_join (read_threads[i], NULL) != 0)
527                 {
528                         ERROR ("plugin: stop_read_threads: pthread_join failed.");
529                 }
530                 read_threads[i] = (pthread_t) 0;
531         }
532         sfree (read_threads);
533         read_threads_num = 0;
534 } /* void stop_read_threads */
535
536 /*
537  * Public functions
538  */
539 void plugin_set_dir (const char *dir)
540 {
541         if (plugindir != NULL)
542                 free (plugindir);
543
544         if (dir == NULL)
545                 plugindir = NULL;
546         else if ((plugindir = strdup (dir)) == NULL)
547         {
548                 char errbuf[1024];
549                 ERROR ("strdup failed: %s",
550                                 sstrerror (errno, errbuf, sizeof (errbuf)));
551         }
552 }
553
554 #define BUFSIZE 512
555 int plugin_load (const char *type, uint32_t flags)
556 {
557         DIR  *dh;
558         const char *dir;
559         char  filename[BUFSIZE] = "";
560         char  typename[BUFSIZE];
561         int   typename_len;
562         int   ret;
563         struct stat    statbuf;
564         struct dirent *de;
565         int status;
566
567         DEBUG ("type = %s", type);
568
569         dir = plugin_get_dir ();
570         ret = 1;
571
572         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
573          * type when matching the filename */
574         status = ssnprintf (typename, sizeof (typename), "%s.so", type);
575         if ((status < 0) || ((size_t) status >= sizeof (typename)))
576         {
577                 WARNING ("snprintf: truncated: `%s.so'", type);
578                 return (-1);
579         }
580         typename_len = strlen (typename);
581
582         if ((dh = opendir (dir)) == NULL)
583         {
584                 char errbuf[1024];
585                 ERROR ("opendir (%s): %s", dir,
586                                 sstrerror (errno, errbuf, sizeof (errbuf)));
587                 return (-1);
588         }
589
590         while ((de = readdir (dh)) != NULL)
591         {
592                 if (strncasecmp (de->d_name, typename, typename_len))
593                         continue;
594
595                 status = ssnprintf (filename, sizeof (filename),
596                                 "%s/%s", dir, de->d_name);
597                 if ((status < 0) || ((size_t) status >= sizeof (filename)))
598                 {
599                         WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
600                         continue;
601                 }
602
603                 if (lstat (filename, &statbuf) == -1)
604                 {
605                         char errbuf[1024];
606                         WARNING ("stat %s: %s", filename,
607                                         sstrerror (errno, errbuf, sizeof (errbuf)));
608                         continue;
609                 }
610                 else if (!S_ISREG (statbuf.st_mode))
611                 {
612                         /* don't follow symlinks */
613                         WARNING ("stat %s: not a regular file", filename);
614                         continue;
615                 }
616
617                 if (plugin_load_file (filename, flags) == 0)
618                 {
619                         /* success */
620                         ret = 0;
621                         break;
622                 }
623                 else
624                 {
625                         fprintf (stderr, "Unable to load plugin %s.\n", type);
626                 }
627         }
628
629         closedir (dh);
630
631         if (filename[0] == '\0')
632                 fprintf (stderr, "Could not find plugin %s.\n", type);
633
634         return (ret);
635 }
636
637 /*
638  * The `register_*' functions follow
639  */
640 int plugin_register_config (const char *name,
641                 int (*callback) (const char *key, const char *val),
642                 const char **keys, int keys_num)
643 {
644         cf_register (name, callback, keys, keys_num);
645         return (0);
646 } /* int plugin_register_config */
647
648 int plugin_register_complex_config (const char *type,
649                 int (*callback) (oconfig_item_t *))
650 {
651         return (cf_register_complex (type, callback));
652 } /* int plugin_register_complex_config */
653
654 int plugin_register_init (const char *name,
655                 int (*callback) (void))
656 {
657         return (create_register_callback (&list_init, name, (void *) callback,
658                                 /* user_data = */ NULL));
659 } /* plugin_register_init */
660
661 static int plugin_compare_read_func (const void *arg0, const void *arg1)
662 {
663         const read_func_t *rf0;
664         const read_func_t *rf1;
665
666         rf0 = arg0;
667         rf1 = arg1;
668
669         if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
670                 return (-1);
671         else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
672                 return (1);
673         else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
674                 return (-1);
675         else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
676                 return (1);
677         else
678                 return (0);
679 } /* int plugin_compare_read_func */
680
681 /* Add a read function to both, the heap and a linked list. The linked list if
682  * used to look-up read functions, especially for the remove function. The heap
683  * is used to determine which plugin to read next. */
684 static int plugin_insert_read (read_func_t *rf)
685 {
686         int status;
687         llentry_t *le;
688
689         pthread_mutex_lock (&read_lock);
690
691         if (read_list == NULL)
692         {
693                 read_list = llist_create ();
694                 if (read_list == NULL)
695                 {
696                         pthread_mutex_unlock (&read_lock);
697                         ERROR ("plugin_insert_read: read_list failed.");
698                         return (-1);
699                 }
700         }
701
702         if (read_heap == NULL)
703         {
704                 read_heap = c_heap_create (plugin_compare_read_func);
705                 if (read_heap == NULL)
706                 {
707                         pthread_mutex_unlock (&read_lock);
708                         ERROR ("plugin_insert_read: c_heap_create failed.");
709                         return (-1);
710                 }
711         }
712
713         le = llentry_create (rf->rf_name, rf);
714         if (le == NULL)
715         {
716                 pthread_mutex_unlock (&read_lock);
717                 ERROR ("plugin_insert_read: llentry_create failed.");
718                 return (-1);
719         }
720
721         status = c_heap_insert (read_heap, rf);
722         if (status != 0)
723         {
724                 pthread_mutex_unlock (&read_lock);
725                 ERROR ("plugin_insert_read: c_heap_insert failed.");
726                 llentry_destroy (le);
727                 return (-1);
728         }
729
730         /* This does not fail. */
731         llist_append (read_list, le);
732
733         pthread_mutex_unlock (&read_lock);
734         return (0);
735 } /* int plugin_insert_read */
736
737 int plugin_register_read (const char *name,
738                 int (*callback) (void))
739 {
740         read_func_t *rf;
741
742         rf = (read_func_t *) malloc (sizeof (read_func_t));
743         if (rf == NULL)
744         {
745                 char errbuf[1024];
746                 ERROR ("plugin_register_read: malloc failed: %s",
747                                 sstrerror (errno, errbuf, sizeof (errbuf)));
748                 return (-1);
749         }
750
751         memset (rf, 0, sizeof (read_func_t));
752         rf->rf_callback = (void *) callback;
753         rf->rf_udata.data = NULL;
754         rf->rf_udata.free_func = NULL;
755         sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
756         rf->rf_type = RF_SIMPLE;
757         rf->rf_interval.tv_sec = 0;
758         rf->rf_interval.tv_nsec = 0;
759         rf->rf_effective_interval = rf->rf_interval;
760
761         return (plugin_insert_read (rf));
762 } /* int plugin_register_read */
763
764 int plugin_register_complex_read (const char *name,
765                 plugin_read_cb callback,
766                 const struct timespec *interval,
767                 user_data_t *user_data)
768 {
769         read_func_t *rf;
770
771         rf = (read_func_t *) malloc (sizeof (read_func_t));
772         if (rf == NULL)
773         {
774                 ERROR ("plugin_register_complex_read: malloc failed.");
775                 return (-1);
776         }
777
778         memset (rf, 0, sizeof (read_func_t));
779         rf->rf_callback = (void *) callback;
780         sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
781         rf->rf_type = RF_COMPLEX;
782         if (interval != NULL)
783         {
784                 rf->rf_interval = *interval;
785         }
786         rf->rf_effective_interval = rf->rf_interval;
787
788         /* Set user data */
789         if (user_data == NULL)
790         {
791                 rf->rf_udata.data = NULL;
792                 rf->rf_udata.free_func = NULL;
793         }
794         else
795         {
796                 rf->rf_udata = *user_data;
797         }
798
799         return (plugin_insert_read (rf));
800 } /* int plugin_register_complex_read */
801
802 int plugin_register_write (const char *name,
803                 plugin_write_cb callback, user_data_t *ud)
804 {
805         return (create_register_callback (&list_write, name,
806                                 (void *) callback, ud));
807 } /* int plugin_register_write */
808
809 int plugin_register_flush (const char *name,
810                 plugin_flush_cb callback, user_data_t *ud)
811 {
812         return (create_register_callback (&list_flush, name,
813                                 (void *) callback, ud));
814 } /* int plugin_register_flush */
815
816 int plugin_register_shutdown (char *name,
817                 int (*callback) (void))
818 {
819         return (create_register_callback (&list_shutdown, name,
820                                 (void *) callback, /* user_data = */ NULL));
821 } /* int plugin_register_shutdown */
822
823 int plugin_register_data_set (const data_set_t *ds)
824 {
825         data_set_t *ds_copy;
826         int i;
827
828         if ((data_sets != NULL)
829                         && (c_avl_get (data_sets, ds->type, NULL) == 0))
830         {
831                 NOTICE ("Replacing DS `%s' with another version.", ds->type);
832                 plugin_unregister_data_set (ds->type);
833         }
834         else if (data_sets == NULL)
835         {
836                 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
837                 if (data_sets == NULL)
838                         return (-1);
839         }
840
841         ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
842         if (ds_copy == NULL)
843                 return (-1);
844         memcpy(ds_copy, ds, sizeof (data_set_t));
845
846         ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
847                         * ds->ds_num);
848         if (ds_copy->ds == NULL)
849         {
850                 free (ds_copy);
851                 return (-1);
852         }
853
854         for (i = 0; i < ds->ds_num; i++)
855                 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
856
857         return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
858 } /* int plugin_register_data_set */
859
860 int plugin_register_log (const char *name,
861                 plugin_log_cb callback, user_data_t *ud)
862 {
863         return (create_register_callback (&list_log, name,
864                                 (void *) callback, ud));
865 } /* int plugin_register_log */
866
867 int plugin_register_notification (const char *name,
868                 plugin_notification_cb callback, user_data_t *ud)
869 {
870         return (create_register_callback (&list_notification, name,
871                                 (void *) callback, ud));
872 } /* int plugin_register_log */
873
874 int plugin_unregister_config (const char *name)
875 {
876         cf_unregister (name);
877         return (0);
878 } /* int plugin_unregister_config */
879
880 int plugin_unregister_complex_config (const char *name)
881 {
882         cf_unregister_complex (name);
883         return (0);
884 } /* int plugin_unregister_complex_config */
885
886 int plugin_unregister_init (const char *name)
887 {
888         return (plugin_unregister (list_init, name));
889 }
890
891 int plugin_unregister_read (const char *name) /* {{{ */
892 {
893         llentry_t *le;
894         read_func_t *rf;
895
896         if (name == NULL)
897                 return (-ENOENT);
898
899         pthread_mutex_lock (&read_lock);
900
901         if (read_list == NULL)
902         {
903                 pthread_mutex_unlock (&read_lock);
904                 return (-ENOENT);
905         }
906
907         le = llist_search (read_list, name);
908         if (le == NULL)
909         {
910                 pthread_mutex_unlock (&read_lock);
911                 WARNING ("plugin_unregister_read: No such read function: %s",
912                                 name);
913                 return (-ENOENT);
914         }
915
916         llist_remove (read_list, le);
917
918         rf = le->value;
919         assert (rf != NULL);
920         rf->rf_type = RF_REMOVE;
921
922         pthread_mutex_unlock (&read_lock);
923
924         llentry_destroy (le);
925
926         DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
927
928         return (0);
929 } /* }}} int plugin_unregister_read */
930
931 int plugin_unregister_write (const char *name)
932 {
933         return (plugin_unregister (list_write, name));
934 }
935
936 int plugin_unregister_flush (const char *name)
937 {
938         return (plugin_unregister (list_flush, name));
939 }
940
941 int plugin_unregister_shutdown (const char *name)
942 {
943         return (plugin_unregister (list_shutdown, name));
944 }
945
946 int plugin_unregister_data_set (const char *name)
947 {
948         data_set_t *ds;
949
950         if (data_sets == NULL)
951                 return (-1);
952
953         if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
954                 return (-1);
955
956         sfree (ds->ds);
957         sfree (ds);
958
959         return (0);
960 } /* int plugin_unregister_data_set */
961
962 int plugin_unregister_log (const char *name)
963 {
964         return (plugin_unregister (list_log, name));
965 }
966
967 int plugin_unregister_notification (const char *name)
968 {
969         return (plugin_unregister (list_notification, name));
970 }
971
972 void plugin_init_all (void)
973 {
974         const char *chain_name;
975         llentry_t *le;
976         int status;
977
978         /* Init the value cache */
979         uc_init ();
980
981         chain_name = global_option_get ("PreCacheChain");
982         pre_cache_chain = fc_chain_get_by_name (chain_name);
983
984         chain_name = global_option_get ("PostCacheChain");
985         post_cache_chain = fc_chain_get_by_name (chain_name);
986
987
988         if ((list_init == NULL) && (read_heap == NULL))
989                 return;
990
991         /* Calling all init callbacks before checking if read callbacks
992          * are available allows the init callbacks to register the read
993          * callback. */
994         le = llist_head (list_init);
995         while (le != NULL)
996         {
997                 callback_func_t *cf;
998                 plugin_init_cb callback;
999
1000                 cf = le->value;
1001                 callback = cf->cf_callback;
1002                 status = (*callback) ();
1003
1004                 if (status != 0)
1005                 {
1006                         ERROR ("Initialization of plugin `%s' "
1007                                         "failed with status %i. "
1008                                         "Plugin will be unloaded.",
1009                                         le->key, status);
1010                         /* Plugins that register read callbacks from the init
1011                          * callback should take care of appropriate error
1012                          * handling themselves. */
1013                         /* FIXME: Unload _all_ functions */
1014                         plugin_unregister_read (le->key);
1015                 }
1016
1017                 le = le->next;
1018         }
1019
1020         /* Start read-threads */
1021         if (read_heap != NULL)
1022         {
1023                 const char *rt;
1024                 int num;
1025                 rt = global_option_get ("ReadThreads");
1026                 num = atoi (rt);
1027                 if (num != -1)
1028                         start_read_threads ((num > 0) ? num : 5);
1029         }
1030 } /* void plugin_init_all */
1031
1032 /* TODO: Rename this function. */
1033 void plugin_read_all (void)
1034 {
1035         uc_check_timeout ();
1036
1037         return;
1038 } /* void plugin_read_all */
1039
1040 /* Read function called when the `-T' command line argument is given. */
1041 int plugin_read_all_once (void)
1042 {
1043         int status;
1044         int return_status = 0;
1045
1046         if (read_heap == NULL)
1047         {
1048                 NOTICE ("No read-functions are registered.");
1049                 return (0);
1050         }
1051
1052         while (42)
1053         {
1054                 read_func_t *rf;
1055
1056                 rf = c_head_get_root (read_heap);
1057                 if (rf == NULL)
1058                         break;
1059
1060                 if (rf->rf_type == RF_SIMPLE)
1061                 {
1062                         int (*callback) (void);
1063
1064                         callback = rf->rf_callback;
1065                         status = (*callback) ();
1066                 }
1067                 else
1068                 {
1069                         plugin_read_cb callback;
1070
1071                         callback = rf->rf_callback;
1072                         status = (*callback) (&rf->rf_udata);
1073                 }
1074
1075                 if (status != 0)
1076                 {
1077                         NOTICE ("read-function of plugin `%s' failed.",
1078                                         rf->rf_name);
1079                         return_status = -1;
1080                 }
1081
1082                 destroy_callback ((void *) rf);
1083         }
1084
1085         return (return_status);
1086 } /* int plugin_read_all_once */
1087
1088 int plugin_write (const char *plugin, /* {{{ */
1089                 const data_set_t *ds, const value_list_t *vl)
1090 {
1091   llentry_t *le;
1092   int status;
1093
1094   if (vl == NULL)
1095     return (EINVAL);
1096
1097   if (list_write == NULL)
1098     return (ENOENT);
1099
1100   if (ds == NULL)
1101   {
1102     ds = plugin_get_ds (vl->type);
1103     if (ds == NULL)
1104     {
1105       ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1106       return (ENOENT);
1107     }
1108   }
1109
1110   if (plugin == NULL)
1111   {
1112     int success = 0;
1113     int failure = 0;
1114
1115     le = llist_head (list_write);
1116     while (le != NULL)
1117     {
1118       callback_func_t *cf = le->value;
1119       plugin_write_cb callback;
1120
1121       DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1122       callback = cf->cf_callback;
1123       status = (*callback) (ds, vl, &cf->cf_udata);
1124       if (status != 0)
1125         failure++;
1126       else
1127         success++;
1128
1129       le = le->next;
1130     }
1131
1132     if ((success == 0) && (failure != 0))
1133       status = -1;
1134     else
1135       status = 0;
1136   }
1137   else /* plugin != NULL */
1138   {
1139     callback_func_t *cf;
1140     plugin_write_cb callback;
1141
1142     le = llist_head (list_write);
1143     while (le != NULL)
1144     {
1145       if (strcasecmp (plugin, le->key) == 0)
1146         break;
1147
1148       le = le->next;
1149     }
1150
1151     if (le == NULL)
1152       return (ENOENT);
1153
1154     cf = le->value;
1155
1156     DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1157     callback = cf->cf_callback;
1158     status = (*callback) (ds, vl, &cf->cf_udata);
1159   }
1160
1161   return (status);
1162 } /* }}} int plugin_write */
1163
1164 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1165 {
1166   llentry_t *le;
1167
1168   if (list_flush == NULL)
1169     return (0);
1170
1171   le = llist_head (list_flush);
1172   while (le != NULL)
1173   {
1174     callback_func_t *cf;
1175     plugin_flush_cb callback;
1176
1177     if ((plugin != NULL)
1178         && (strcmp (plugin, le->key) != 0))
1179     {
1180       le = le->next;
1181       continue;
1182     }
1183
1184     cf = le->value;
1185     callback = cf->cf_callback;
1186
1187     (*callback) (timeout, identifier, &cf->cf_udata);
1188
1189     le = le->next;
1190   }
1191   return (0);
1192 } /* int plugin_flush */
1193
1194 void plugin_shutdown_all (void)
1195 {
1196         llentry_t *le;
1197
1198         stop_read_threads ();
1199
1200         destroy_all_callbacks (&list_init);
1201
1202         pthread_mutex_lock (&read_lock);
1203         llist_destroy (read_list);
1204         read_list = NULL;
1205         pthread_mutex_unlock (&read_lock);
1206
1207         destroy_read_heap ();
1208
1209         plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1210                         /* identifier = */ NULL);
1211
1212         le = NULL;
1213         if (list_shutdown != NULL)
1214                 le = llist_head (list_shutdown);
1215
1216         while (le != NULL)
1217         {
1218                 callback_func_t *cf;
1219                 plugin_shutdown_cb callback;
1220
1221                 cf = le->value;
1222                 callback = cf->cf_callback;
1223
1224                 /* Advance the pointer before calling the callback allows
1225                  * shutdown functions to unregister themselves. If done the
1226                  * other way around the memory `le' points to will be freed
1227                  * after callback returns. */
1228                 le = le->next;
1229
1230                 (*callback) ();
1231         }
1232
1233         /* Write plugins which use the `user_data' pointer usually need the
1234          * same data available to the flush callback. If this is the case, set
1235          * the free_function to NULL when registering the flush callback and to
1236          * the real free function when registering the write callback. This way
1237          * the data isn't freed twice. */
1238         destroy_all_callbacks (&list_flush);
1239         destroy_all_callbacks (&list_write);
1240
1241         destroy_all_callbacks (&list_notification);
1242         destroy_all_callbacks (&list_shutdown);
1243         destroy_all_callbacks (&list_log);
1244 } /* void plugin_shutdown_all */
1245
1246 int plugin_dispatch_values (value_list_t *vl)
1247 {
1248         int status;
1249         static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1250
1251         value_t *saved_values;
1252         int      saved_values_len;
1253
1254         data_set_t *ds;
1255
1256         int free_meta_data = 0;
1257
1258         if ((vl == NULL) || (vl->type[0] == 0)
1259                         || (vl->values == NULL) || (vl->values_len < 1))
1260         {
1261                 ERROR ("plugin_dispatch_values: Invalid value list.");
1262                 return (-1);
1263         }
1264
1265         /* Free meta data only if the calling function didn't specify any. In
1266          * this case matches and targets may add some and the calling function
1267          * may not expect (and therefore free) that data. */
1268         if (vl->meta == NULL)
1269                 free_meta_data = 1;
1270
1271         if (list_write == NULL)
1272                 c_complain_once (LOG_WARNING, &no_write_complaint,
1273                                 "plugin_dispatch_values: No write callback has been "
1274                                 "registered. Please load at least one output plugin, "
1275                                 "if you want the collected data to be stored.");
1276
1277         if (data_sets == NULL)
1278         {
1279                 ERROR ("plugin_dispatch_values: No data sets registered. "
1280                                 "Could the types database be read? Check "
1281                                 "your `TypesDB' setting!");
1282                 return (-1);
1283         }
1284
1285         if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1286         {
1287                 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1288                 return (-1);
1289         }
1290
1291         if (vl->time == 0)
1292                 vl->time = time (NULL);
1293
1294         if (vl->interval <= 0)
1295                 vl->interval = interval_g;
1296
1297         DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1298                         "host = %s; "
1299                         "plugin = %s; plugin_instance = %s; "
1300                         "type = %s; type_instance = %s;",
1301                         (unsigned int) vl->time, vl->interval,
1302                         vl->host,
1303                         vl->plugin, vl->plugin_instance,
1304                         vl->type, vl->type_instance);
1305
1306 #if COLLECT_DEBUG
1307         assert (0 == strcmp (ds->type, vl->type));
1308 #else
1309         if (0 != strcmp (ds->type, vl->type))
1310                 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1311                                 ds->type, vl->type);
1312 #endif
1313
1314 #if COLLECT_DEBUG
1315         assert (ds->ds_num == vl->values_len);
1316 #else
1317         if (ds->ds_num != vl->values_len)
1318         {
1319                 ERROR ("plugin_dispatch_values: ds->type = %s: "
1320                                 "(ds->ds_num = %i) != "
1321                                 "(vl->values_len = %i)",
1322                                 ds->type, ds->ds_num, vl->values_len);
1323                 return (-1);
1324         }
1325 #endif
1326
1327         escape_slashes (vl->host, sizeof (vl->host));
1328         escape_slashes (vl->plugin, sizeof (vl->plugin));
1329         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1330         escape_slashes (vl->type, sizeof (vl->type));
1331         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1332
1333         /* Copy the values. This way, we can assure `targets' that they get
1334          * dynamically allocated values, which they can free and replace if
1335          * they like. */
1336         if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1337         {
1338                 saved_values     = vl->values;
1339                 saved_values_len = vl->values_len;
1340
1341                 vl->values = (value_t *) calloc (vl->values_len,
1342                                 sizeof (*vl->values));
1343                 if (vl->values == NULL)
1344                 {
1345                         ERROR ("plugin_dispatch_values: calloc failed.");
1346                         vl->values = saved_values;
1347                         return (-1);
1348                 }
1349                 memcpy (vl->values, saved_values,
1350                                 vl->values_len * sizeof (*vl->values));
1351         }
1352         else /* if ((pre == NULL) && (post == NULL)) */
1353         {
1354                 saved_values     = NULL;
1355                 saved_values_len = 0;
1356         }
1357
1358         if (pre_cache_chain != NULL)
1359         {
1360                 status = fc_process_chain (ds, vl, pre_cache_chain);
1361                 if (status < 0)
1362                 {
1363                         WARNING ("plugin_dispatch_values: Running the "
1364                                         "pre-cache chain failed with "
1365                                         "status %i (%#x).",
1366                                         status, status);
1367                 }
1368                 else if (status == FC_TARGET_STOP)
1369                 {
1370                         /* Restore the state of the value_list so that plugins
1371                          * don't get confused.. */
1372                         if (saved_values != NULL)
1373                         {
1374                                 free (vl->values);
1375                                 vl->values     = saved_values;
1376                                 vl->values_len = saved_values_len;
1377                         }
1378                         return (0);
1379                 }
1380         }
1381
1382         /* Update the value cache */
1383         uc_update (ds, vl);
1384
1385         /* Initiate threshold checking */
1386         ut_check_threshold (ds, vl);
1387
1388         if (post_cache_chain != NULL)
1389         {
1390                 status = fc_process_chain (ds, vl, post_cache_chain);
1391                 if (status < 0)
1392                 {
1393                         WARNING ("plugin_dispatch_values: Running the "
1394                                         "post-cache chain failed with "
1395                                         "status %i (%#x).",
1396                                         status, status);
1397                 }
1398         }
1399         else
1400                 fc_default_action (ds, vl);
1401
1402         /* Restore the state of the value_list so that plugins don't get
1403          * confused.. */
1404         if (saved_values != NULL)
1405         {
1406                 free (vl->values);
1407                 vl->values     = saved_values;
1408                 vl->values_len = saved_values_len;
1409         }
1410
1411         if ((free_meta_data != 0) && (vl->meta != NULL))
1412         {
1413                 meta_data_destroy (vl->meta);
1414                 vl->meta = NULL;
1415         }
1416
1417         return (0);
1418 } /* int plugin_dispatch_values */
1419
1420 int plugin_dispatch_notification (const notification_t *notif)
1421 {
1422         llentry_t *le;
1423         /* Possible TODO: Add flap detection here */
1424
1425         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1426                         "time = %u; host = %s;",
1427                         notif->severity, notif->message,
1428                         (unsigned int) notif->time, notif->host);
1429
1430         /* Nobody cares for notifications */
1431         if (list_notification == NULL)
1432                 return (-1);
1433
1434         le = llist_head (list_notification);
1435         while (le != NULL)
1436         {
1437                 callback_func_t *cf;
1438                 plugin_notification_cb callback;
1439                 int status;
1440
1441                 cf = le->value;
1442                 callback = cf->cf_callback;
1443                 status = (*callback) (notif, &cf->cf_udata);
1444                 if (status != 0)
1445                 {
1446                         WARNING ("plugin_dispatch_notification: Notification "
1447                                         "callback %s returned %i.",
1448                                         le->key, status);
1449                 }
1450
1451                 le = le->next;
1452         }
1453
1454         return (0);
1455 } /* int plugin_dispatch_notification */
1456
1457 void plugin_log (int level, const char *format, ...)
1458 {
1459         char msg[1024];
1460         va_list ap;
1461         llentry_t *le;
1462
1463         if (list_log == NULL)
1464         {
1465                 va_start (ap, format);
1466                 vfprintf (stderr, format, ap);
1467                 va_end (ap);
1468                 return;
1469         }
1470
1471 #if !COLLECT_DEBUG
1472         if (level >= LOG_DEBUG)
1473                 return;
1474 #endif
1475
1476         va_start (ap, format);
1477         vsnprintf (msg, sizeof (msg), format, ap);
1478         msg[sizeof (msg) - 1] = '\0';
1479         va_end (ap);
1480
1481         le = llist_head (list_log);
1482         while (le != NULL)
1483         {
1484                 callback_func_t *cf;
1485                 plugin_log_cb callback;
1486
1487                 cf = le->value;
1488                 callback = cf->cf_callback;
1489
1490                 (*callback) (level, msg, &cf->cf_udata);
1491
1492                 le = le->next;
1493         }
1494 } /* void plugin_log */
1495
1496 const data_set_t *plugin_get_ds (const char *name)
1497 {
1498         data_set_t *ds;
1499
1500         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1501         {
1502                 DEBUG ("No such dataset registered: %s", name);
1503                 return (NULL);
1504         }
1505
1506         return (ds);
1507 } /* data_set_t *plugin_get_ds */
1508
1509 static int plugin_notification_meta_add (notification_t *n,
1510     const char *name,
1511     enum notification_meta_type_e type,
1512     const void *value)
1513 {
1514   notification_meta_t *meta;
1515   notification_meta_t *tail;
1516
1517   if ((n == NULL) || (name == NULL) || (value == NULL))
1518   {
1519     ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1520     return (-1);
1521   }
1522
1523   meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1524   if (meta == NULL)
1525   {
1526     ERROR ("plugin_notification_meta_add: malloc failed.");
1527     return (-1);
1528   }
1529   memset (meta, 0, sizeof (notification_meta_t));
1530
1531   sstrncpy (meta->name, name, sizeof (meta->name));
1532   meta->type = type;
1533
1534   switch (type)
1535   {
1536     case NM_TYPE_STRING:
1537     {
1538       meta->nm_value.nm_string = strdup ((const char *) value);
1539       if (meta->nm_value.nm_string == NULL)
1540       {
1541         ERROR ("plugin_notification_meta_add: strdup failed.");
1542         sfree (meta);
1543         return (-1);
1544       }
1545       break;
1546     }
1547     case NM_TYPE_SIGNED_INT:
1548     {
1549       meta->nm_value.nm_signed_int = *((int64_t *) value);
1550       break;
1551     }
1552     case NM_TYPE_UNSIGNED_INT:
1553     {
1554       meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1555       break;
1556     }
1557     case NM_TYPE_DOUBLE:
1558     {
1559       meta->nm_value.nm_double = *((double *) value);
1560       break;
1561     }
1562     case NM_TYPE_BOOLEAN:
1563     {
1564       meta->nm_value.nm_boolean = *((bool *) value);
1565       break;
1566     }
1567     default:
1568     {
1569       ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1570       sfree (meta);
1571       return (-1);
1572     }
1573   } /* switch (type) */
1574
1575   meta->next = NULL;
1576   tail = n->meta;
1577   while ((tail != NULL) && (tail->next != NULL))
1578     tail = tail->next;
1579
1580   if (tail == NULL)
1581     n->meta = meta;
1582   else
1583     tail->next = meta;
1584
1585   return (0);
1586 } /* int plugin_notification_meta_add */
1587
1588 int plugin_notification_meta_add_string (notification_t *n,
1589     const char *name,
1590     const char *value)
1591 {
1592   return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1593 }
1594
1595 int plugin_notification_meta_add_signed_int (notification_t *n,
1596     const char *name,
1597     int64_t value)
1598 {
1599   return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1600 }
1601
1602 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1603     const char *name,
1604     uint64_t value)
1605 {
1606   return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1607 }
1608
1609 int plugin_notification_meta_add_double (notification_t *n,
1610     const char *name,
1611     double value)
1612 {
1613   return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1614 }
1615
1616 int plugin_notification_meta_add_boolean (notification_t *n,
1617     const char *name,
1618     bool value)
1619 {
1620   return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1621 }
1622
1623 int plugin_notification_meta_copy (notification_t *dst,
1624     const notification_t *src)
1625 {
1626   notification_meta_t *meta;
1627
1628   assert (dst != NULL);
1629   assert (src != NULL);
1630   assert (dst != src);
1631   assert ((src->meta == NULL) || (src->meta != dst->meta));
1632
1633   for (meta = src->meta; meta != NULL; meta = meta->next)
1634   {
1635     if (meta->type == NM_TYPE_STRING)
1636       plugin_notification_meta_add_string (dst, meta->name,
1637           meta->nm_value.nm_string);
1638     else if (meta->type == NM_TYPE_SIGNED_INT)
1639       plugin_notification_meta_add_signed_int (dst, meta->name,
1640           meta->nm_value.nm_signed_int);
1641     else if (meta->type == NM_TYPE_UNSIGNED_INT)
1642       plugin_notification_meta_add_unsigned_int (dst, meta->name,
1643           meta->nm_value.nm_unsigned_int);
1644     else if (meta->type == NM_TYPE_DOUBLE)
1645       plugin_notification_meta_add_double (dst, meta->name,
1646           meta->nm_value.nm_double);
1647     else if (meta->type == NM_TYPE_BOOLEAN)
1648       plugin_notification_meta_add_boolean (dst, meta->name,
1649           meta->nm_value.nm_boolean);
1650   }
1651
1652   return (0);
1653 } /* int plugin_notification_meta_copy */
1654
1655 int plugin_notification_meta_free (notification_meta_t *n)
1656 {
1657   notification_meta_t *this;
1658   notification_meta_t *next;
1659
1660   if (n == NULL)
1661   {
1662     ERROR ("plugin_notification_meta_free: n == NULL!");
1663     return (-1);
1664   }
1665
1666   this = n;
1667   while (this != NULL)
1668   {
1669     next = this->next;
1670
1671     if (this->type == NM_TYPE_STRING)
1672     {
1673       free ((char *)this->nm_value.nm_string);
1674       this->nm_value.nm_string = NULL;
1675     }
1676     sfree (this);
1677
1678     this = next;
1679   }
1680
1681   return (0);
1682 } /* int plugin_notification_meta_free */
1683
1684 /* vim: set sw=8 ts=8 noet fdm=marker : */