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