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