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