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 **/
23 #include "collectd.h"
24 #include "utils_complain.h"
26 #include <ltdl.h>
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
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"
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;
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;
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;
81 static fc_chain_t *pre_cache_chain = NULL;
82 static fc_chain_t *post_cache_chain = NULL;
84 static c_avl_tree_t *data_sets;
86 static char *plugindir = NULL;
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;
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 }
107 static void destroy_callback (callback_func_t *cf) /* {{{ */
108 {
109 if (cf == NULL)
110 return;
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 */
121 static void destroy_all_callbacks (llist_t **list) /* {{{ */
122 {
123 llentry_t *le;
125 if (*list == NULL)
126 return;
128 le = llist_head (*list);
129 while (le != NULL)
130 {
131 llentry_t *le_next;
133 le_next = le->next;
135 sfree (le->key);
136 destroy_callback (le->value);
137 le->value = NULL;
139 le = le_next;
140 }
142 llist_destroy (*list);
143 *list = NULL;
144 } /* }}} void destroy_all_callbacks */
146 static void destroy_read_heap (void) /* {{{ */
147 {
148 if (read_heap == NULL)
149 return;
151 while (42)
152 {
153 callback_func_t *cf;
155 cf = c_heap_get_root (read_heap);
156 if (cf == NULL)
157 break;
159 destroy_callback (cf);
160 }
162 c_heap_destroy (read_heap);
163 read_heap = NULL;
164 } /* }}} void destroy_read_heap */
166 static int register_callback (llist_t **list, /* {{{ */
167 const char *name, callback_func_t *cf)
168 {
169 llentry_t *le;
170 char *key;
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 }
184 key = strdup (name);
185 if (key == NULL)
186 {
187 ERROR ("plugin: register_callback: strdup failed.");
188 destroy_callback (cf);
189 return (-1);
190 }
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 }
205 llist_append (*list, le);
206 }
207 else
208 {
209 callback_func_t *old_cf;
211 old_cf = le->value;
212 le->value = cf;
214 WARNING ("plugin: register_callback: "
215 "a callback named `%s' already exists - "
216 "overwriting the old entry!", name);
218 destroy_callback (old_cf);
219 sfree (key);
220 }
222 return (0);
223 } /* }}} int register_callback */
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;
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));
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 }
249 return (register_callback (list, name, cf));
250 } /* }}} int create_register_callback */
252 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
253 {
254 llentry_t *e;
256 if (list == NULL)
257 return (-1);
259 e = llist_search (list, name);
260 if (e == NULL)
261 return (-1);
263 llist_remove (list, e);
265 sfree (e->key);
266 destroy_callback (e->value);
268 llentry_destroy (e);
270 return (0);
271 } /* }}} int plugin_unregister */
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);
283 DEBUG ("file = %s", file);
285 lt_dlinit ();
286 lt_dlerror (); /* clear errors */
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
305 if (dlh == NULL)
306 {
307 const char *error = lt_dlerror ();
309 ERROR ("lt_dlopen (%s) failed: %s", file, error);
310 fprintf (stderr, "lt_dlopen (%s) failed: %s\n", file, error);
311 return (1);
312 }
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 }
322 (*reg_handle) ();
324 return (0);
325 }
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 }
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;
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;
350 gettimeofday (&now, /* timezone = */ NULL);
352 abstime.tv_sec = now.tv_sec + interval_g;
353 abstime.tv_nsec = 1000 * now.tv_usec;
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 }
362 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
363 {
364 gettimeofday (&now, /* timezone = */ NULL);
366 rf->rf_interval.tv_sec = interval_g;
367 rf->rf_interval.tv_nsec = 0;
369 rf->rf_effective_interval = rf->rf_interval;
371 rf->rf_next_read.tv_sec = now.tv_sec;
372 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
373 }
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 (!timeout_reached(rf->rf_next_read) && rc == 0) {
384 rc = pthread_cond_timedwait (&read_cond, &read_lock,
385 &rf->rf_next_read);
386 }
388 /* Must hold `real_lock' when accessing `rf->rf_type'. */
389 rf_type = rf->rf_type;
390 pthread_mutex_unlock (&read_lock);
392 /* Check if we're supposed to stop.. This may have interrupted
393 * the sleep, too. */
394 if (read_loop == 0)
395 {
396 /* Insert `rf' again, so it can be free'd correctly */
397 c_heap_insert (read_heap, rf);
398 break;
399 }
401 /* The entry has been marked for deletion. The linked list
402 * entry has already been removed by `plugin_unregister_read'.
403 * All we have to do here is free the `read_func_t' and
404 * continue. */
405 if (rf_type == RF_REMOVE)
406 {
407 DEBUG ("plugin_read_thread: Destroying the `%s' "
408 "callback.", rf->rf_name);
409 destroy_callback ((callback_func_t *) rf);
410 rf = NULL;
411 continue;
412 }
414 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
416 if (rf_type == RF_SIMPLE)
417 {
418 int (*callback) (void);
420 callback = rf->rf_callback;
421 status = (*callback) ();
422 }
423 else
424 {
425 plugin_read_cb callback;
427 assert (rf_type == RF_COMPLEX);
429 callback = rf->rf_callback;
430 status = (*callback) (&rf->rf_udata);
431 }
433 /* If the function signals failure, we will increase the
434 * intervals in which it will be called. */
435 if (status != 0)
436 {
437 rf->rf_effective_interval.tv_sec *= 2;
438 rf->rf_effective_interval.tv_nsec *= 2;
439 NORMALIZE_TIMESPEC (rf->rf_effective_interval);
441 if (rf->rf_effective_interval.tv_sec >= 86400)
442 {
443 rf->rf_effective_interval.tv_sec = 86400;
444 rf->rf_effective_interval.tv_nsec = 0;
445 }
447 NOTICE ("read-function of plugin `%s' failed. "
448 "Will suspend it for %i seconds.",
449 rf->rf_name,
450 (int) rf->rf_effective_interval.tv_sec);
451 }
452 else
453 {
454 /* Success: Restore the interval, if it was changed. */
455 rf->rf_effective_interval = rf->rf_interval;
456 }
458 /* update the ``next read due'' field */
459 gettimeofday (&now, /* timezone = */ NULL);
461 DEBUG ("plugin_read_thread: Effective interval of the "
462 "%s plugin is %i.%09i.",
463 rf->rf_name,
464 (int) rf->rf_effective_interval.tv_sec,
465 (int) rf->rf_effective_interval.tv_nsec);
467 /* Calculate the next (absolute) time at which this function
468 * should be called. */
469 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
470 + rf->rf_effective_interval.tv_sec;
471 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
472 + rf->rf_effective_interval.tv_nsec;
473 NORMALIZE_TIMESPEC (rf->rf_next_read);
475 /* Check, if `rf_next_read' is in the past. */
476 if ((rf->rf_next_read.tv_sec < now.tv_sec)
477 || ((rf->rf_next_read.tv_sec == now.tv_sec)
478 && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec))))
479 {
480 /* `rf_next_read' is in the past. Insert `now'
481 * so this value doesn't trail off into the
482 * past too much. */
483 rf->rf_next_read.tv_sec = now.tv_sec;
484 rf->rf_next_read.tv_nsec = 1000 * now.tv_usec;
485 }
487 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
488 rf->rf_name,
489 (int) rf->rf_next_read.tv_sec,
490 (int) rf->rf_next_read.tv_nsec);
492 /* Re-insert this read function into the heap again. */
493 c_heap_insert (read_heap, rf);
494 } /* while (read_loop) */
496 pthread_exit (NULL);
497 return ((void *) 0);
498 } /* void *plugin_read_thread */
500 static void start_read_threads (int num)
501 {
502 int i;
504 if (read_threads != NULL)
505 return;
507 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
508 if (read_threads == NULL)
509 {
510 ERROR ("plugin: start_read_threads: calloc failed.");
511 return;
512 }
514 read_threads_num = 0;
515 for (i = 0; i < num; i++)
516 {
517 if (pthread_create (read_threads + read_threads_num, NULL,
518 plugin_read_thread, NULL) == 0)
519 {
520 read_threads_num++;
521 }
522 else
523 {
524 ERROR ("plugin: start_read_threads: pthread_create failed.");
525 return;
526 }
527 } /* for (i) */
528 } /* void start_read_threads */
530 static void stop_read_threads (void)
531 {
532 int i;
534 if (read_threads == NULL)
535 return;
537 INFO ("collectd: Stopping %i read threads.", read_threads_num);
539 pthread_mutex_lock (&read_lock);
540 read_loop = 0;
541 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
542 pthread_cond_broadcast (&read_cond);
543 pthread_mutex_unlock (&read_lock);
545 for (i = 0; i < read_threads_num; i++)
546 {
547 if (pthread_join (read_threads[i], NULL) != 0)
548 {
549 ERROR ("plugin: stop_read_threads: pthread_join failed.");
550 }
551 read_threads[i] = (pthread_t) 0;
552 }
553 sfree (read_threads);
554 read_threads_num = 0;
555 } /* void stop_read_threads */
557 /*
558 * Public functions
559 */
560 void plugin_set_dir (const char *dir)
561 {
562 if (plugindir != NULL)
563 free (plugindir);
565 if (dir == NULL)
566 plugindir = NULL;
567 else if ((plugindir = strdup (dir)) == NULL)
568 {
569 char errbuf[1024];
570 ERROR ("strdup failed: %s",
571 sstrerror (errno, errbuf, sizeof (errbuf)));
572 }
573 }
575 #define BUFSIZE 512
576 int plugin_load (const char *type, uint32_t flags)
577 {
578 DIR *dh;
579 const char *dir;
580 char filename[BUFSIZE] = "";
581 char typename[BUFSIZE];
582 int typename_len;
583 int ret;
584 struct stat statbuf;
585 struct dirent *de;
586 int status;
588 DEBUG ("type = %s", type);
590 dir = plugin_get_dir ();
591 ret = 1;
593 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
594 * type when matching the filename */
595 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
596 if ((status < 0) || ((size_t) status >= sizeof (typename)))
597 {
598 WARNING ("snprintf: truncated: `%s.so'", type);
599 return (-1);
600 }
601 typename_len = strlen (typename);
603 if ((dh = opendir (dir)) == NULL)
604 {
605 char errbuf[1024];
606 ERROR ("opendir (%s): %s", dir,
607 sstrerror (errno, errbuf, sizeof (errbuf)));
608 return (-1);
609 }
611 while ((de = readdir (dh)) != NULL)
612 {
613 if (strncasecmp (de->d_name, typename, typename_len))
614 continue;
616 status = ssnprintf (filename, sizeof (filename),
617 "%s/%s", dir, de->d_name);
618 if ((status < 0) || ((size_t) status >= sizeof (filename)))
619 {
620 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
621 continue;
622 }
624 if (lstat (filename, &statbuf) == -1)
625 {
626 char errbuf[1024];
627 WARNING ("stat %s: %s", filename,
628 sstrerror (errno, errbuf, sizeof (errbuf)));
629 continue;
630 }
631 else if (!S_ISREG (statbuf.st_mode))
632 {
633 /* don't follow symlinks */
634 WARNING ("stat %s: not a regular file", filename);
635 continue;
636 }
638 if (plugin_load_file (filename, flags) == 0)
639 {
640 /* success */
641 ret = 0;
642 break;
643 }
644 else
645 {
646 fprintf (stderr, "Unable to load plugin %s.\n", type);
647 }
648 }
650 closedir (dh);
652 if (filename[0] == '\0')
653 fprintf (stderr, "Could not find plugin %s.\n", type);
655 return (ret);
656 }
658 /*
659 * The `register_*' functions follow
660 */
661 int plugin_register_config (const char *name,
662 int (*callback) (const char *key, const char *val),
663 const char **keys, int keys_num)
664 {
665 cf_register (name, callback, keys, keys_num);
666 return (0);
667 } /* int plugin_register_config */
669 int plugin_register_complex_config (const char *type,
670 int (*callback) (oconfig_item_t *))
671 {
672 return (cf_register_complex (type, callback));
673 } /* int plugin_register_complex_config */
675 int plugin_register_init (const char *name,
676 int (*callback) (void))
677 {
678 return (create_register_callback (&list_init, name, (void *) callback,
679 /* user_data = */ NULL));
680 } /* plugin_register_init */
682 static int plugin_compare_read_func (const void *arg0, const void *arg1)
683 {
684 const read_func_t *rf0;
685 const read_func_t *rf1;
687 rf0 = arg0;
688 rf1 = arg1;
690 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
691 return (-1);
692 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
693 return (1);
694 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
695 return (-1);
696 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
697 return (1);
698 else
699 return (0);
700 } /* int plugin_compare_read_func */
702 /* Add a read function to both, the heap and a linked list. The linked list if
703 * used to look-up read functions, especially for the remove function. The heap
704 * is used to determine which plugin to read next. */
705 static int plugin_insert_read (read_func_t *rf)
706 {
707 int status;
708 llentry_t *le;
710 pthread_mutex_lock (&read_lock);
712 if (read_list == NULL)
713 {
714 read_list = llist_create ();
715 if (read_list == NULL)
716 {
717 pthread_mutex_unlock (&read_lock);
718 ERROR ("plugin_insert_read: read_list failed.");
719 return (-1);
720 }
721 }
723 if (read_heap == NULL)
724 {
725 read_heap = c_heap_create (plugin_compare_read_func);
726 if (read_heap == NULL)
727 {
728 pthread_mutex_unlock (&read_lock);
729 ERROR ("plugin_insert_read: c_heap_create failed.");
730 return (-1);
731 }
732 }
734 le = llentry_create (rf->rf_name, rf);
735 if (le == NULL)
736 {
737 pthread_mutex_unlock (&read_lock);
738 ERROR ("plugin_insert_read: llentry_create failed.");
739 return (-1);
740 }
742 status = c_heap_insert (read_heap, rf);
743 if (status != 0)
744 {
745 pthread_mutex_unlock (&read_lock);
746 ERROR ("plugin_insert_read: c_heap_insert failed.");
747 llentry_destroy (le);
748 return (-1);
749 }
751 /* This does not fail. */
752 llist_append (read_list, le);
754 pthread_mutex_unlock (&read_lock);
755 return (0);
756 } /* int plugin_insert_read */
758 int plugin_register_read (const char *name,
759 int (*callback) (void))
760 {
761 read_func_t *rf;
763 rf = (read_func_t *) malloc (sizeof (read_func_t));
764 if (rf == NULL)
765 {
766 char errbuf[1024];
767 ERROR ("plugin_register_read: malloc failed: %s",
768 sstrerror (errno, errbuf, sizeof (errbuf)));
769 return (-1);
770 }
772 memset (rf, 0, sizeof (read_func_t));
773 rf->rf_callback = (void *) callback;
774 rf->rf_udata.data = NULL;
775 rf->rf_udata.free_func = NULL;
776 rf->rf_group[0] = '\0';
777 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
778 rf->rf_type = RF_SIMPLE;
779 rf->rf_interval.tv_sec = 0;
780 rf->rf_interval.tv_nsec = 0;
781 rf->rf_effective_interval = rf->rf_interval;
783 return (plugin_insert_read (rf));
784 } /* int plugin_register_read */
786 int plugin_register_complex_read (const char *group, const char *name,
787 plugin_read_cb callback,
788 const struct timespec *interval,
789 user_data_t *user_data)
790 {
791 read_func_t *rf;
793 rf = (read_func_t *) malloc (sizeof (read_func_t));
794 if (rf == NULL)
795 {
796 ERROR ("plugin_register_complex_read: malloc failed.");
797 return (-1);
798 }
800 memset (rf, 0, sizeof (read_func_t));
801 rf->rf_callback = (void *) callback;
802 if (group != NULL)
803 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
804 else
805 rf->rf_group[0] = '\0';
806 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
807 rf->rf_type = RF_COMPLEX;
808 if (interval != NULL)
809 {
810 rf->rf_interval = *interval;
811 }
812 rf->rf_effective_interval = rf->rf_interval;
814 /* Set user data */
815 if (user_data == NULL)
816 {
817 rf->rf_udata.data = NULL;
818 rf->rf_udata.free_func = NULL;
819 }
820 else
821 {
822 rf->rf_udata = *user_data;
823 }
825 return (plugin_insert_read (rf));
826 } /* int plugin_register_complex_read */
828 int plugin_register_write (const char *name,
829 plugin_write_cb callback, user_data_t *ud)
830 {
831 return (create_register_callback (&list_write, name,
832 (void *) callback, ud));
833 } /* int plugin_register_write */
835 int plugin_register_flush (const char *name,
836 plugin_flush_cb callback, user_data_t *ud)
837 {
838 return (create_register_callback (&list_flush, name,
839 (void *) callback, ud));
840 } /* int plugin_register_flush */
842 int plugin_register_shutdown (char *name,
843 int (*callback) (void))
844 {
845 return (create_register_callback (&list_shutdown, name,
846 (void *) callback, /* user_data = */ NULL));
847 } /* int plugin_register_shutdown */
849 int plugin_register_data_set (const data_set_t *ds)
850 {
851 data_set_t *ds_copy;
852 int i;
854 if ((data_sets != NULL)
855 && (c_avl_get (data_sets, ds->type, NULL) == 0))
856 {
857 NOTICE ("Replacing DS `%s' with another version.", ds->type);
858 plugin_unregister_data_set (ds->type);
859 }
860 else if (data_sets == NULL)
861 {
862 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
863 if (data_sets == NULL)
864 return (-1);
865 }
867 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
868 if (ds_copy == NULL)
869 return (-1);
870 memcpy(ds_copy, ds, sizeof (data_set_t));
872 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
873 * ds->ds_num);
874 if (ds_copy->ds == NULL)
875 {
876 free (ds_copy);
877 return (-1);
878 }
880 for (i = 0; i < ds->ds_num; i++)
881 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
883 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
884 } /* int plugin_register_data_set */
886 int plugin_register_log (const char *name,
887 plugin_log_cb callback, user_data_t *ud)
888 {
889 return (create_register_callback (&list_log, name,
890 (void *) callback, ud));
891 } /* int plugin_register_log */
893 int plugin_register_notification (const char *name,
894 plugin_notification_cb callback, user_data_t *ud)
895 {
896 return (create_register_callback (&list_notification, name,
897 (void *) callback, ud));
898 } /* int plugin_register_log */
900 int plugin_unregister_config (const char *name)
901 {
902 cf_unregister (name);
903 return (0);
904 } /* int plugin_unregister_config */
906 int plugin_unregister_complex_config (const char *name)
907 {
908 cf_unregister_complex (name);
909 return (0);
910 } /* int plugin_unregister_complex_config */
912 int plugin_unregister_init (const char *name)
913 {
914 return (plugin_unregister (list_init, name));
915 }
917 int plugin_unregister_read (const char *name) /* {{{ */
918 {
919 llentry_t *le;
920 read_func_t *rf;
922 if (name == NULL)
923 return (-ENOENT);
925 pthread_mutex_lock (&read_lock);
927 if (read_list == NULL)
928 {
929 pthread_mutex_unlock (&read_lock);
930 return (-ENOENT);
931 }
933 le = llist_search (read_list, name);
934 if (le == NULL)
935 {
936 pthread_mutex_unlock (&read_lock);
937 WARNING ("plugin_unregister_read: No such read function: %s",
938 name);
939 return (-ENOENT);
940 }
942 llist_remove (read_list, le);
944 rf = le->value;
945 assert (rf != NULL);
946 rf->rf_type = RF_REMOVE;
948 pthread_mutex_unlock (&read_lock);
950 llentry_destroy (le);
952 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
954 return (0);
955 } /* }}} int plugin_unregister_read */
957 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
958 {
959 read_func_t *rf = e->value;
960 char *group = ud;
962 return strcmp (rf->rf_group, (const char *)group);
963 } /* }}} int compare_read_func_group */
965 int plugin_unregister_read_group (const char *group) /* {{{ */
966 {
967 llentry_t *le;
968 read_func_t *rf;
970 int found = 0;
972 if (group == NULL)
973 return (-ENOENT);
975 pthread_mutex_lock (&read_lock);
977 if (read_list == NULL)
978 {
979 pthread_mutex_unlock (&read_lock);
980 return (-ENOENT);
981 }
983 while (42)
984 {
985 le = llist_search_custom (read_list,
986 compare_read_func_group, (void *)group);
988 if (le == NULL)
989 break;
991 ++found;
993 llist_remove (read_list, le);
995 rf = le->value;
996 assert (rf != NULL);
997 rf->rf_type = RF_REMOVE;
999 llentry_destroy (le);
1001 DEBUG ("plugin_unregister_read_group: "
1002 "Marked `%s' (group `%s') for removal.",
1003 rf->rf_name, group);
1004 }
1006 pthread_mutex_unlock (&read_lock);
1008 if (found == 0)
1009 {
1010 WARNING ("plugin_unregister_read_group: No such "
1011 "group of read function: %s", group);
1012 return (-ENOENT);
1013 }
1015 return (0);
1016 } /* }}} int plugin_unregister_read_group */
1018 int plugin_unregister_write (const char *name)
1019 {
1020 return (plugin_unregister (list_write, name));
1021 }
1023 int plugin_unregister_flush (const char *name)
1024 {
1025 return (plugin_unregister (list_flush, name));
1026 }
1028 int plugin_unregister_shutdown (const char *name)
1029 {
1030 return (plugin_unregister (list_shutdown, name));
1031 }
1033 int plugin_unregister_data_set (const char *name)
1034 {
1035 data_set_t *ds;
1037 if (data_sets == NULL)
1038 return (-1);
1040 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1041 return (-1);
1043 sfree (ds->ds);
1044 sfree (ds);
1046 return (0);
1047 } /* int plugin_unregister_data_set */
1049 int plugin_unregister_log (const char *name)
1050 {
1051 return (plugin_unregister (list_log, name));
1052 }
1054 int plugin_unregister_notification (const char *name)
1055 {
1056 return (plugin_unregister (list_notification, name));
1057 }
1059 void plugin_init_all (void)
1060 {
1061 const char *chain_name;
1062 llentry_t *le;
1063 int status;
1065 /* Init the value cache */
1066 uc_init ();
1068 chain_name = global_option_get ("PreCacheChain");
1069 pre_cache_chain = fc_chain_get_by_name (chain_name);
1071 chain_name = global_option_get ("PostCacheChain");
1072 post_cache_chain = fc_chain_get_by_name (chain_name);
1075 if ((list_init == NULL) && (read_heap == NULL))
1076 return;
1078 /* Calling all init callbacks before checking if read callbacks
1079 * are available allows the init callbacks to register the read
1080 * callback. */
1081 le = llist_head (list_init);
1082 while (le != NULL)
1083 {
1084 callback_func_t *cf;
1085 plugin_init_cb callback;
1087 cf = le->value;
1088 callback = cf->cf_callback;
1089 status = (*callback) ();
1091 if (status != 0)
1092 {
1093 ERROR ("Initialization of plugin `%s' "
1094 "failed with status %i. "
1095 "Plugin will be unloaded.",
1096 le->key, status);
1097 /* Plugins that register read callbacks from the init
1098 * callback should take care of appropriate error
1099 * handling themselves. */
1100 /* FIXME: Unload _all_ functions */
1101 plugin_unregister_read (le->key);
1102 }
1104 le = le->next;
1105 }
1107 /* Start read-threads */
1108 if (read_heap != NULL)
1109 {
1110 const char *rt;
1111 int num;
1112 rt = global_option_get ("ReadThreads");
1113 num = atoi (rt);
1114 if (num != -1)
1115 start_read_threads ((num > 0) ? num : 5);
1116 }
1117 } /* void plugin_init_all */
1119 /* TODO: Rename this function. */
1120 void plugin_read_all (void)
1121 {
1122 uc_check_timeout ();
1124 return;
1125 } /* void plugin_read_all */
1127 /* Read function called when the `-T' command line argument is given. */
1128 int plugin_read_all_once (void)
1129 {
1130 int status;
1131 int return_status = 0;
1133 if (read_heap == NULL)
1134 {
1135 NOTICE ("No read-functions are registered.");
1136 return (0);
1137 }
1139 while (42)
1140 {
1141 read_func_t *rf;
1143 rf = c_heap_get_root (read_heap);
1144 if (rf == NULL)
1145 break;
1147 if (rf->rf_type == RF_SIMPLE)
1148 {
1149 int (*callback) (void);
1151 callback = rf->rf_callback;
1152 status = (*callback) ();
1153 }
1154 else
1155 {
1156 plugin_read_cb callback;
1158 callback = rf->rf_callback;
1159 status = (*callback) (&rf->rf_udata);
1160 }
1162 if (status != 0)
1163 {
1164 NOTICE ("read-function of plugin `%s' failed.",
1165 rf->rf_name);
1166 return_status = -1;
1167 }
1169 destroy_callback ((void *) rf);
1170 }
1172 return (return_status);
1173 } /* int plugin_read_all_once */
1175 int plugin_write (const char *plugin, /* {{{ */
1176 const data_set_t *ds, const value_list_t *vl)
1177 {
1178 llentry_t *le;
1179 int status;
1181 if (vl == NULL)
1182 return (EINVAL);
1184 if (list_write == NULL)
1185 return (ENOENT);
1187 if (ds == NULL)
1188 {
1189 ds = plugin_get_ds (vl->type);
1190 if (ds == NULL)
1191 {
1192 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1193 return (ENOENT);
1194 }
1195 }
1197 if (plugin == NULL)
1198 {
1199 int success = 0;
1200 int failure = 0;
1202 le = llist_head (list_write);
1203 while (le != NULL)
1204 {
1205 callback_func_t *cf = le->value;
1206 plugin_write_cb callback;
1208 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1209 callback = cf->cf_callback;
1210 status = (*callback) (ds, vl, &cf->cf_udata);
1211 if (status != 0)
1212 failure++;
1213 else
1214 success++;
1216 le = le->next;
1217 }
1219 if ((success == 0) && (failure != 0))
1220 status = -1;
1221 else
1222 status = 0;
1223 }
1224 else /* plugin != NULL */
1225 {
1226 callback_func_t *cf;
1227 plugin_write_cb callback;
1229 le = llist_head (list_write);
1230 while (le != NULL)
1231 {
1232 if (strcasecmp (plugin, le->key) == 0)
1233 break;
1235 le = le->next;
1236 }
1238 if (le == NULL)
1239 return (ENOENT);
1241 cf = le->value;
1243 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1244 callback = cf->cf_callback;
1245 status = (*callback) (ds, vl, &cf->cf_udata);
1246 }
1248 return (status);
1249 } /* }}} int plugin_write */
1251 int plugin_flush (const char *plugin, int timeout, const char *identifier)
1252 {
1253 llentry_t *le;
1255 if (list_flush == NULL)
1256 return (0);
1258 le = llist_head (list_flush);
1259 while (le != NULL)
1260 {
1261 callback_func_t *cf;
1262 plugin_flush_cb callback;
1264 if ((plugin != NULL)
1265 && (strcmp (plugin, le->key) != 0))
1266 {
1267 le = le->next;
1268 continue;
1269 }
1271 cf = le->value;
1272 callback = cf->cf_callback;
1274 (*callback) (timeout, identifier, &cf->cf_udata);
1276 le = le->next;
1277 }
1278 return (0);
1279 } /* int plugin_flush */
1281 void plugin_shutdown_all (void)
1282 {
1283 llentry_t *le;
1285 stop_read_threads ();
1287 destroy_all_callbacks (&list_init);
1289 pthread_mutex_lock (&read_lock);
1290 llist_destroy (read_list);
1291 read_list = NULL;
1292 pthread_mutex_unlock (&read_lock);
1294 destroy_read_heap ();
1296 plugin_flush (/* plugin = */ NULL, /* timeout = */ -1,
1297 /* identifier = */ NULL);
1299 le = NULL;
1300 if (list_shutdown != NULL)
1301 le = llist_head (list_shutdown);
1303 while (le != NULL)
1304 {
1305 callback_func_t *cf;
1306 plugin_shutdown_cb callback;
1308 cf = le->value;
1309 callback = cf->cf_callback;
1311 /* Advance the pointer before calling the callback allows
1312 * shutdown functions to unregister themselves. If done the
1313 * other way around the memory `le' points to will be freed
1314 * after callback returns. */
1315 le = le->next;
1317 (*callback) ();
1318 }
1320 /* Write plugins which use the `user_data' pointer usually need the
1321 * same data available to the flush callback. If this is the case, set
1322 * the free_function to NULL when registering the flush callback and to
1323 * the real free function when registering the write callback. This way
1324 * the data isn't freed twice. */
1325 destroy_all_callbacks (&list_flush);
1326 destroy_all_callbacks (&list_write);
1328 destroy_all_callbacks (&list_notification);
1329 destroy_all_callbacks (&list_shutdown);
1330 destroy_all_callbacks (&list_log);
1331 } /* void plugin_shutdown_all */
1333 int plugin_dispatch_values (value_list_t *vl)
1334 {
1335 int status;
1336 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1338 value_t *saved_values;
1339 int saved_values_len;
1341 data_set_t *ds;
1343 int free_meta_data = 0;
1345 if ((vl == NULL) || (vl->type[0] == 0)
1346 || (vl->values == NULL) || (vl->values_len < 1))
1347 {
1348 ERROR ("plugin_dispatch_values: Invalid value list.");
1349 return (-1);
1350 }
1352 /* Free meta data only if the calling function didn't specify any. In
1353 * this case matches and targets may add some and the calling function
1354 * may not expect (and therefore free) that data. */
1355 if (vl->meta == NULL)
1356 free_meta_data = 1;
1358 if (list_write == NULL)
1359 c_complain_once (LOG_WARNING, &no_write_complaint,
1360 "plugin_dispatch_values: No write callback has been "
1361 "registered. Please load at least one output plugin, "
1362 "if you want the collected data to be stored.");
1364 if (data_sets == NULL)
1365 {
1366 ERROR ("plugin_dispatch_values: No data sets registered. "
1367 "Could the types database be read? Check "
1368 "your `TypesDB' setting!");
1369 return (-1);
1370 }
1372 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1373 {
1374 INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
1375 return (-1);
1376 }
1378 if (vl->time == 0)
1379 vl->time = time (NULL);
1381 if (vl->interval <= 0)
1382 vl->interval = interval_g;
1384 DEBUG ("plugin_dispatch_values: time = %u; interval = %i; "
1385 "host = %s; "
1386 "plugin = %s; plugin_instance = %s; "
1387 "type = %s; type_instance = %s;",
1388 (unsigned int) vl->time, vl->interval,
1389 vl->host,
1390 vl->plugin, vl->plugin_instance,
1391 vl->type, vl->type_instance);
1393 #if COLLECT_DEBUG
1394 assert (0 == strcmp (ds->type, vl->type));
1395 #else
1396 if (0 != strcmp (ds->type, vl->type))
1397 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1398 ds->type, vl->type);
1399 #endif
1401 #if COLLECT_DEBUG
1402 assert (ds->ds_num == vl->values_len);
1403 #else
1404 if (ds->ds_num != vl->values_len)
1405 {
1406 ERROR ("plugin_dispatch_values: ds->type = %s: "
1407 "(ds->ds_num = %i) != "
1408 "(vl->values_len = %i)",
1409 ds->type, ds->ds_num, vl->values_len);
1410 return (-1);
1411 }
1412 #endif
1414 escape_slashes (vl->host, sizeof (vl->host));
1415 escape_slashes (vl->plugin, sizeof (vl->plugin));
1416 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1417 escape_slashes (vl->type, sizeof (vl->type));
1418 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1420 /* Copy the values. This way, we can assure `targets' that they get
1421 * dynamically allocated values, which they can free and replace if
1422 * they like. */
1423 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1424 {
1425 saved_values = vl->values;
1426 saved_values_len = vl->values_len;
1428 vl->values = (value_t *) calloc (vl->values_len,
1429 sizeof (*vl->values));
1430 if (vl->values == NULL)
1431 {
1432 ERROR ("plugin_dispatch_values: calloc failed.");
1433 vl->values = saved_values;
1434 return (-1);
1435 }
1436 memcpy (vl->values, saved_values,
1437 vl->values_len * sizeof (*vl->values));
1438 }
1439 else /* if ((pre == NULL) && (post == NULL)) */
1440 {
1441 saved_values = NULL;
1442 saved_values_len = 0;
1443 }
1445 if (pre_cache_chain != NULL)
1446 {
1447 status = fc_process_chain (ds, vl, pre_cache_chain);
1448 if (status < 0)
1449 {
1450 WARNING ("plugin_dispatch_values: Running the "
1451 "pre-cache chain failed with "
1452 "status %i (%#x).",
1453 status, status);
1454 }
1455 else if (status == FC_TARGET_STOP)
1456 {
1457 /* Restore the state of the value_list so that plugins
1458 * don't get confused.. */
1459 if (saved_values != NULL)
1460 {
1461 free (vl->values);
1462 vl->values = saved_values;
1463 vl->values_len = saved_values_len;
1464 }
1465 return (0);
1466 }
1467 }
1469 /* Update the value cache */
1470 uc_update (ds, vl);
1472 /* Initiate threshold checking */
1473 ut_check_threshold (ds, vl);
1475 if (post_cache_chain != NULL)
1476 {
1477 status = fc_process_chain (ds, vl, post_cache_chain);
1478 if (status < 0)
1479 {
1480 WARNING ("plugin_dispatch_values: Running the "
1481 "post-cache chain failed with "
1482 "status %i (%#x).",
1483 status, status);
1484 }
1485 }
1486 else
1487 fc_default_action (ds, vl);
1489 /* Restore the state of the value_list so that plugins don't get
1490 * confused.. */
1491 if (saved_values != NULL)
1492 {
1493 free (vl->values);
1494 vl->values = saved_values;
1495 vl->values_len = saved_values_len;
1496 }
1498 if ((free_meta_data != 0) && (vl->meta != NULL))
1499 {
1500 meta_data_destroy (vl->meta);
1501 vl->meta = NULL;
1502 }
1504 return (0);
1505 } /* int plugin_dispatch_values */
1507 int plugin_dispatch_notification (const notification_t *notif)
1508 {
1509 llentry_t *le;
1510 /* Possible TODO: Add flap detection here */
1512 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1513 "time = %u; host = %s;",
1514 notif->severity, notif->message,
1515 (unsigned int) notif->time, notif->host);
1517 /* Nobody cares for notifications */
1518 if (list_notification == NULL)
1519 return (-1);
1521 le = llist_head (list_notification);
1522 while (le != NULL)
1523 {
1524 callback_func_t *cf;
1525 plugin_notification_cb callback;
1526 int status;
1528 cf = le->value;
1529 callback = cf->cf_callback;
1530 status = (*callback) (notif, &cf->cf_udata);
1531 if (status != 0)
1532 {
1533 WARNING ("plugin_dispatch_notification: Notification "
1534 "callback %s returned %i.",
1535 le->key, status);
1536 }
1538 le = le->next;
1539 }
1541 return (0);
1542 } /* int plugin_dispatch_notification */
1544 void plugin_log (int level, const char *format, ...)
1545 {
1546 char msg[1024];
1547 va_list ap;
1548 llentry_t *le;
1550 #if !COLLECT_DEBUG
1551 if (level >= LOG_DEBUG)
1552 return;
1553 #endif
1555 va_start (ap, format);
1556 vsnprintf (msg, sizeof (msg), format, ap);
1557 msg[sizeof (msg) - 1] = '\0';
1558 va_end (ap);
1560 if (list_log == NULL)
1561 {
1562 fprintf (stderr, "%s\n", msg);
1563 return;
1564 }
1566 le = llist_head (list_log);
1567 while (le != NULL)
1568 {
1569 callback_func_t *cf;
1570 plugin_log_cb callback;
1572 cf = le->value;
1573 callback = cf->cf_callback;
1575 (*callback) (level, msg, &cf->cf_udata);
1577 le = le->next;
1578 }
1579 } /* void plugin_log */
1581 const data_set_t *plugin_get_ds (const char *name)
1582 {
1583 data_set_t *ds;
1585 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1586 {
1587 DEBUG ("No such dataset registered: %s", name);
1588 return (NULL);
1589 }
1591 return (ds);
1592 } /* data_set_t *plugin_get_ds */
1594 static int plugin_notification_meta_add (notification_t *n,
1595 const char *name,
1596 enum notification_meta_type_e type,
1597 const void *value)
1598 {
1599 notification_meta_t *meta;
1600 notification_meta_t *tail;
1602 if ((n == NULL) || (name == NULL) || (value == NULL))
1603 {
1604 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1605 return (-1);
1606 }
1608 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1609 if (meta == NULL)
1610 {
1611 ERROR ("plugin_notification_meta_add: malloc failed.");
1612 return (-1);
1613 }
1614 memset (meta, 0, sizeof (notification_meta_t));
1616 sstrncpy (meta->name, name, sizeof (meta->name));
1617 meta->type = type;
1619 switch (type)
1620 {
1621 case NM_TYPE_STRING:
1622 {
1623 meta->nm_value.nm_string = strdup ((const char *) value);
1624 if (meta->nm_value.nm_string == NULL)
1625 {
1626 ERROR ("plugin_notification_meta_add: strdup failed.");
1627 sfree (meta);
1628 return (-1);
1629 }
1630 break;
1631 }
1632 case NM_TYPE_SIGNED_INT:
1633 {
1634 meta->nm_value.nm_signed_int = *((int64_t *) value);
1635 break;
1636 }
1637 case NM_TYPE_UNSIGNED_INT:
1638 {
1639 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1640 break;
1641 }
1642 case NM_TYPE_DOUBLE:
1643 {
1644 meta->nm_value.nm_double = *((double *) value);
1645 break;
1646 }
1647 case NM_TYPE_BOOLEAN:
1648 {
1649 meta->nm_value.nm_boolean = *((bool *) value);
1650 break;
1651 }
1652 default:
1653 {
1654 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1655 sfree (meta);
1656 return (-1);
1657 }
1658 } /* switch (type) */
1660 meta->next = NULL;
1661 tail = n->meta;
1662 while ((tail != NULL) && (tail->next != NULL))
1663 tail = tail->next;
1665 if (tail == NULL)
1666 n->meta = meta;
1667 else
1668 tail->next = meta;
1670 return (0);
1671 } /* int plugin_notification_meta_add */
1673 int plugin_notification_meta_add_string (notification_t *n,
1674 const char *name,
1675 const char *value)
1676 {
1677 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1678 }
1680 int plugin_notification_meta_add_signed_int (notification_t *n,
1681 const char *name,
1682 int64_t value)
1683 {
1684 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1685 }
1687 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1688 const char *name,
1689 uint64_t value)
1690 {
1691 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1692 }
1694 int plugin_notification_meta_add_double (notification_t *n,
1695 const char *name,
1696 double value)
1697 {
1698 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1699 }
1701 int plugin_notification_meta_add_boolean (notification_t *n,
1702 const char *name,
1703 bool value)
1704 {
1705 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1706 }
1708 int plugin_notification_meta_copy (notification_t *dst,
1709 const notification_t *src)
1710 {
1711 notification_meta_t *meta;
1713 assert (dst != NULL);
1714 assert (src != NULL);
1715 assert (dst != src);
1716 assert ((src->meta == NULL) || (src->meta != dst->meta));
1718 for (meta = src->meta; meta != NULL; meta = meta->next)
1719 {
1720 if (meta->type == NM_TYPE_STRING)
1721 plugin_notification_meta_add_string (dst, meta->name,
1722 meta->nm_value.nm_string);
1723 else if (meta->type == NM_TYPE_SIGNED_INT)
1724 plugin_notification_meta_add_signed_int (dst, meta->name,
1725 meta->nm_value.nm_signed_int);
1726 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1727 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1728 meta->nm_value.nm_unsigned_int);
1729 else if (meta->type == NM_TYPE_DOUBLE)
1730 plugin_notification_meta_add_double (dst, meta->name,
1731 meta->nm_value.nm_double);
1732 else if (meta->type == NM_TYPE_BOOLEAN)
1733 plugin_notification_meta_add_boolean (dst, meta->name,
1734 meta->nm_value.nm_boolean);
1735 }
1737 return (0);
1738 } /* int plugin_notification_meta_copy */
1740 int plugin_notification_meta_free (notification_meta_t *n)
1741 {
1742 notification_meta_t *this;
1743 notification_meta_t *next;
1745 if (n == NULL)
1746 {
1747 ERROR ("plugin_notification_meta_free: n == NULL!");
1748 return (-1);
1749 }
1751 this = n;
1752 while (this != NULL)
1753 {
1754 next = this->next;
1756 if (this->type == NM_TYPE_STRING)
1757 {
1758 free ((char *)this->nm_value.nm_string);
1759 this->nm_value.nm_string = NULL;
1760 }
1761 sfree (this);
1763 this = next;
1764 }
1766 return (0);
1767 } /* int plugin_notification_meta_free */
1769 /* vim: set sw=8 ts=8 noet fdm=marker : */