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