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