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