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