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 cdtime_t 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 now = cdtime ();
352 CDTIME_T_TO_TIMESPEC (now + interval_g, &abstime);
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 }
361 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
362 {
363 now = cdtime ();
365 CDTIME_T_TO_TIMESPEC (interval_g, &rf->rf_interval);
367 rf->rf_effective_interval = rf->rf_interval;
369 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
370 }
372 /* sleep until this entry is due,
373 * using pthread_cond_timedwait */
374 pthread_mutex_lock (&read_lock);
375 /* In pthread_cond_timedwait, spurious wakeups are possible
376 * (and really happen, at least on NetBSD with > 1 CPU), thus
377 * we need to re-evaluate the condition every time
378 * pthread_cond_timedwait returns. */
379 rc = 0;
380 while ((read_loop != 0)
381 && !timeout_reached(rf->rf_next_read)
382 && rc == 0)
383 {
384 rc = pthread_cond_timedwait (&read_cond, &read_lock,
385 &rf->rf_next_read);
386 }
388 /* Must hold `read_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 now = cdtime ();
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 (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
477 {
478 /* `rf_next_read' is in the past. Insert `now'
479 * so this value doesn't trail off into the
480 * past too much. */
481 CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
482 }
484 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
485 rf->rf_name,
486 (int) rf->rf_next_read.tv_sec,
487 (int) rf->rf_next_read.tv_nsec);
489 /* Re-insert this read function into the heap again. */
490 c_heap_insert (read_heap, rf);
491 } /* while (read_loop) */
493 pthread_exit (NULL);
494 return ((void *) 0);
495 } /* void *plugin_read_thread */
497 static void start_read_threads (int num)
498 {
499 int i;
501 if (read_threads != NULL)
502 return;
504 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
505 if (read_threads == NULL)
506 {
507 ERROR ("plugin: start_read_threads: calloc failed.");
508 return;
509 }
511 read_threads_num = 0;
512 for (i = 0; i < num; i++)
513 {
514 if (pthread_create (read_threads + read_threads_num, NULL,
515 plugin_read_thread, NULL) == 0)
516 {
517 read_threads_num++;
518 }
519 else
520 {
521 ERROR ("plugin: start_read_threads: pthread_create failed.");
522 return;
523 }
524 } /* for (i) */
525 } /* void start_read_threads */
527 static void stop_read_threads (void)
528 {
529 int i;
531 if (read_threads == NULL)
532 return;
534 INFO ("collectd: Stopping %i read threads.", read_threads_num);
536 pthread_mutex_lock (&read_lock);
537 read_loop = 0;
538 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
539 pthread_cond_broadcast (&read_cond);
540 pthread_mutex_unlock (&read_lock);
542 for (i = 0; i < read_threads_num; i++)
543 {
544 if (pthread_join (read_threads[i], NULL) != 0)
545 {
546 ERROR ("plugin: stop_read_threads: pthread_join failed.");
547 }
548 read_threads[i] = (pthread_t) 0;
549 }
550 sfree (read_threads);
551 read_threads_num = 0;
552 } /* void stop_read_threads */
554 /*
555 * Public functions
556 */
557 void plugin_set_dir (const char *dir)
558 {
559 if (plugindir != NULL)
560 free (plugindir);
562 if (dir == NULL)
563 plugindir = NULL;
564 else if ((plugindir = strdup (dir)) == NULL)
565 {
566 char errbuf[1024];
567 ERROR ("strdup failed: %s",
568 sstrerror (errno, errbuf, sizeof (errbuf)));
569 }
570 }
572 #define BUFSIZE 512
573 int plugin_load (const char *type, uint32_t flags)
574 {
575 DIR *dh;
576 const char *dir;
577 char filename[BUFSIZE] = "";
578 char typename[BUFSIZE];
579 int typename_len;
580 int ret;
581 struct stat statbuf;
582 struct dirent *de;
583 int status;
585 DEBUG ("type = %s", type);
587 dir = plugin_get_dir ();
588 ret = 1;
590 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
591 * type when matching the filename */
592 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
593 if ((status < 0) || ((size_t) status >= sizeof (typename)))
594 {
595 WARNING ("snprintf: truncated: `%s.so'", type);
596 return (-1);
597 }
598 typename_len = strlen (typename);
600 if ((dh = opendir (dir)) == NULL)
601 {
602 char errbuf[1024];
603 ERROR ("opendir (%s): %s", dir,
604 sstrerror (errno, errbuf, sizeof (errbuf)));
605 return (-1);
606 }
608 while ((de = readdir (dh)) != NULL)
609 {
610 if (strncasecmp (de->d_name, typename, typename_len))
611 continue;
613 status = ssnprintf (filename, sizeof (filename),
614 "%s/%s", dir, de->d_name);
615 if ((status < 0) || ((size_t) status >= sizeof (filename)))
616 {
617 WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
618 continue;
619 }
621 if (lstat (filename, &statbuf) == -1)
622 {
623 char errbuf[1024];
624 WARNING ("stat %s: %s", filename,
625 sstrerror (errno, errbuf, sizeof (errbuf)));
626 continue;
627 }
628 else if (!S_ISREG (statbuf.st_mode))
629 {
630 /* don't follow symlinks */
631 WARNING ("stat %s: not a regular file", filename);
632 continue;
633 }
635 if (plugin_load_file (filename, flags) == 0)
636 {
637 /* success */
638 ret = 0;
639 break;
640 }
641 else
642 {
643 fprintf (stderr, "Unable to load plugin %s.\n", type);
644 }
645 }
647 closedir (dh);
649 if (filename[0] == '\0')
650 fprintf (stderr, "Could not find plugin %s.\n", type);
652 return (ret);
653 }
655 /*
656 * The `register_*' functions follow
657 */
658 int plugin_register_config (const char *name,
659 int (*callback) (const char *key, const char *val),
660 const char **keys, int keys_num)
661 {
662 cf_register (name, callback, keys, keys_num);
663 return (0);
664 } /* int plugin_register_config */
666 int plugin_register_complex_config (const char *type,
667 int (*callback) (oconfig_item_t *))
668 {
669 return (cf_register_complex (type, callback));
670 } /* int plugin_register_complex_config */
672 int plugin_register_init (const char *name,
673 int (*callback) (void))
674 {
675 return (create_register_callback (&list_init, name, (void *) callback,
676 /* user_data = */ NULL));
677 } /* plugin_register_init */
679 static int plugin_compare_read_func (const void *arg0, const void *arg1)
680 {
681 const read_func_t *rf0;
682 const read_func_t *rf1;
684 rf0 = arg0;
685 rf1 = arg1;
687 if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
688 return (-1);
689 else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
690 return (1);
691 else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
692 return (-1);
693 else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
694 return (1);
695 else
696 return (0);
697 } /* int plugin_compare_read_func */
699 /* Add a read function to both, the heap and a linked list. The linked list if
700 * used to look-up read functions, especially for the remove function. The heap
701 * is used to determine which plugin to read next. */
702 static int plugin_insert_read (read_func_t *rf)
703 {
704 int status;
705 llentry_t *le;
707 pthread_mutex_lock (&read_lock);
709 if (read_list == NULL)
710 {
711 read_list = llist_create ();
712 if (read_list == NULL)
713 {
714 pthread_mutex_unlock (&read_lock);
715 ERROR ("plugin_insert_read: read_list failed.");
716 return (-1);
717 }
718 }
720 if (read_heap == NULL)
721 {
722 read_heap = c_heap_create (plugin_compare_read_func);
723 if (read_heap == NULL)
724 {
725 pthread_mutex_unlock (&read_lock);
726 ERROR ("plugin_insert_read: c_heap_create failed.");
727 return (-1);
728 }
729 }
731 le = llentry_create (rf->rf_name, rf);
732 if (le == NULL)
733 {
734 pthread_mutex_unlock (&read_lock);
735 ERROR ("plugin_insert_read: llentry_create failed.");
736 return (-1);
737 }
739 status = c_heap_insert (read_heap, rf);
740 if (status != 0)
741 {
742 pthread_mutex_unlock (&read_lock);
743 ERROR ("plugin_insert_read: c_heap_insert failed.");
744 llentry_destroy (le);
745 return (-1);
746 }
748 /* This does not fail. */
749 llist_append (read_list, le);
751 pthread_mutex_unlock (&read_lock);
752 return (0);
753 } /* int plugin_insert_read */
755 int plugin_register_read (const char *name,
756 int (*callback) (void))
757 {
758 read_func_t *rf;
760 rf = (read_func_t *) malloc (sizeof (read_func_t));
761 if (rf == NULL)
762 {
763 char errbuf[1024];
764 ERROR ("plugin_register_read: malloc failed: %s",
765 sstrerror (errno, errbuf, sizeof (errbuf)));
766 return (-1);
767 }
769 memset (rf, 0, sizeof (read_func_t));
770 rf->rf_callback = (void *) callback;
771 rf->rf_udata.data = NULL;
772 rf->rf_udata.free_func = NULL;
773 rf->rf_group[0] = '\0';
774 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
775 rf->rf_type = RF_SIMPLE;
776 rf->rf_interval.tv_sec = 0;
777 rf->rf_interval.tv_nsec = 0;
778 rf->rf_effective_interval = rf->rf_interval;
780 return (plugin_insert_read (rf));
781 } /* int plugin_register_read */
783 int plugin_register_complex_read (const char *group, const char *name,
784 plugin_read_cb callback,
785 const struct timespec *interval,
786 user_data_t *user_data)
787 {
788 read_func_t *rf;
790 rf = (read_func_t *) malloc (sizeof (read_func_t));
791 if (rf == NULL)
792 {
793 ERROR ("plugin_register_complex_read: malloc failed.");
794 return (-1);
795 }
797 memset (rf, 0, sizeof (read_func_t));
798 rf->rf_callback = (void *) callback;
799 if (group != NULL)
800 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
801 else
802 rf->rf_group[0] = '\0';
803 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
804 rf->rf_type = RF_COMPLEX;
805 if (interval != NULL)
806 {
807 rf->rf_interval = *interval;
808 }
809 rf->rf_effective_interval = rf->rf_interval;
811 /* Set user data */
812 if (user_data == NULL)
813 {
814 rf->rf_udata.data = NULL;
815 rf->rf_udata.free_func = NULL;
816 }
817 else
818 {
819 rf->rf_udata = *user_data;
820 }
822 return (plugin_insert_read (rf));
823 } /* int plugin_register_complex_read */
825 int plugin_register_write (const char *name,
826 plugin_write_cb callback, user_data_t *ud)
827 {
828 return (create_register_callback (&list_write, name,
829 (void *) callback, ud));
830 } /* int plugin_register_write */
832 int plugin_register_flush (const char *name,
833 plugin_flush_cb callback, user_data_t *ud)
834 {
835 return (create_register_callback (&list_flush, name,
836 (void *) callback, ud));
837 } /* int plugin_register_flush */
839 int plugin_register_shutdown (char *name,
840 int (*callback) (void))
841 {
842 return (create_register_callback (&list_shutdown, name,
843 (void *) callback, /* user_data = */ NULL));
844 } /* int plugin_register_shutdown */
846 int plugin_register_data_set (const data_set_t *ds)
847 {
848 data_set_t *ds_copy;
849 int i;
851 if ((data_sets != NULL)
852 && (c_avl_get (data_sets, ds->type, NULL) == 0))
853 {
854 NOTICE ("Replacing DS `%s' with another version.", ds->type);
855 plugin_unregister_data_set (ds->type);
856 }
857 else if (data_sets == NULL)
858 {
859 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
860 if (data_sets == NULL)
861 return (-1);
862 }
864 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
865 if (ds_copy == NULL)
866 return (-1);
867 memcpy(ds_copy, ds, sizeof (data_set_t));
869 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
870 * ds->ds_num);
871 if (ds_copy->ds == NULL)
872 {
873 free (ds_copy);
874 return (-1);
875 }
877 for (i = 0; i < ds->ds_num; i++)
878 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
880 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
881 } /* int plugin_register_data_set */
883 int plugin_register_log (const char *name,
884 plugin_log_cb callback, user_data_t *ud)
885 {
886 return (create_register_callback (&list_log, name,
887 (void *) callback, ud));
888 } /* int plugin_register_log */
890 int plugin_register_notification (const char *name,
891 plugin_notification_cb callback, user_data_t *ud)
892 {
893 return (create_register_callback (&list_notification, name,
894 (void *) callback, ud));
895 } /* int plugin_register_log */
897 int plugin_unregister_config (const char *name)
898 {
899 cf_unregister (name);
900 return (0);
901 } /* int plugin_unregister_config */
903 int plugin_unregister_complex_config (const char *name)
904 {
905 cf_unregister_complex (name);
906 return (0);
907 } /* int plugin_unregister_complex_config */
909 int plugin_unregister_init (const char *name)
910 {
911 return (plugin_unregister (list_init, name));
912 }
914 int plugin_unregister_read (const char *name) /* {{{ */
915 {
916 llentry_t *le;
917 read_func_t *rf;
919 if (name == NULL)
920 return (-ENOENT);
922 pthread_mutex_lock (&read_lock);
924 if (read_list == NULL)
925 {
926 pthread_mutex_unlock (&read_lock);
927 return (-ENOENT);
928 }
930 le = llist_search (read_list, name);
931 if (le == NULL)
932 {
933 pthread_mutex_unlock (&read_lock);
934 WARNING ("plugin_unregister_read: No such read function: %s",
935 name);
936 return (-ENOENT);
937 }
939 llist_remove (read_list, le);
941 rf = le->value;
942 assert (rf != NULL);
943 rf->rf_type = RF_REMOVE;
945 pthread_mutex_unlock (&read_lock);
947 llentry_destroy (le);
949 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
951 return (0);
952 } /* }}} int plugin_unregister_read */
954 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
955 {
956 read_func_t *rf = e->value;
957 char *group = ud;
959 return strcmp (rf->rf_group, (const char *)group);
960 } /* }}} int compare_read_func_group */
962 int plugin_unregister_read_group (const char *group) /* {{{ */
963 {
964 llentry_t *le;
965 read_func_t *rf;
967 int found = 0;
969 if (group == NULL)
970 return (-ENOENT);
972 pthread_mutex_lock (&read_lock);
974 if (read_list == NULL)
975 {
976 pthread_mutex_unlock (&read_lock);
977 return (-ENOENT);
978 }
980 while (42)
981 {
982 le = llist_search_custom (read_list,
983 compare_read_func_group, (void *)group);
985 if (le == NULL)
986 break;
988 ++found;
990 llist_remove (read_list, le);
992 rf = le->value;
993 assert (rf != NULL);
994 rf->rf_type = RF_REMOVE;
996 llentry_destroy (le);
998 DEBUG ("plugin_unregister_read_group: "
999 "Marked `%s' (group `%s') for removal.",
1000 rf->rf_name, group);
1001 }
1003 pthread_mutex_unlock (&read_lock);
1005 if (found == 0)
1006 {
1007 WARNING ("plugin_unregister_read_group: No such "
1008 "group of read function: %s", group);
1009 return (-ENOENT);
1010 }
1012 return (0);
1013 } /* }}} int plugin_unregister_read_group */
1015 int plugin_unregister_write (const char *name)
1016 {
1017 return (plugin_unregister (list_write, name));
1018 }
1020 int plugin_unregister_flush (const char *name)
1021 {
1022 return (plugin_unregister (list_flush, name));
1023 }
1025 int plugin_unregister_shutdown (const char *name)
1026 {
1027 return (plugin_unregister (list_shutdown, name));
1028 }
1030 int plugin_unregister_data_set (const char *name)
1031 {
1032 data_set_t *ds;
1034 if (data_sets == NULL)
1035 return (-1);
1037 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1038 return (-1);
1040 sfree (ds->ds);
1041 sfree (ds);
1043 return (0);
1044 } /* int plugin_unregister_data_set */
1046 int plugin_unregister_log (const char *name)
1047 {
1048 return (plugin_unregister (list_log, name));
1049 }
1051 int plugin_unregister_notification (const char *name)
1052 {
1053 return (plugin_unregister (list_notification, name));
1054 }
1056 void plugin_init_all (void)
1057 {
1058 const char *chain_name;
1059 llentry_t *le;
1060 int status;
1062 /* Init the value cache */
1063 uc_init ();
1065 chain_name = global_option_get ("PreCacheChain");
1066 pre_cache_chain = fc_chain_get_by_name (chain_name);
1068 chain_name = global_option_get ("PostCacheChain");
1069 post_cache_chain = fc_chain_get_by_name (chain_name);
1072 if ((list_init == NULL) && (read_heap == NULL))
1073 return;
1075 /* Calling all init callbacks before checking if read callbacks
1076 * are available allows the init callbacks to register the read
1077 * callback. */
1078 le = llist_head (list_init);
1079 while (le != NULL)
1080 {
1081 callback_func_t *cf;
1082 plugin_init_cb callback;
1084 cf = le->value;
1085 callback = cf->cf_callback;
1086 status = (*callback) ();
1088 if (status != 0)
1089 {
1090 ERROR ("Initialization of plugin `%s' "
1091 "failed with status %i. "
1092 "Plugin will be unloaded.",
1093 le->key, status);
1094 /* Plugins that register read callbacks from the init
1095 * callback should take care of appropriate error
1096 * handling themselves. */
1097 /* FIXME: Unload _all_ functions */
1098 plugin_unregister_read (le->key);
1099 }
1101 le = le->next;
1102 }
1104 /* Start read-threads */
1105 if (read_heap != NULL)
1106 {
1107 const char *rt;
1108 int num;
1109 rt = global_option_get ("ReadThreads");
1110 num = atoi (rt);
1111 if (num != -1)
1112 start_read_threads ((num > 0) ? num : 5);
1113 }
1114 } /* void plugin_init_all */
1116 /* TODO: Rename this function. */
1117 void plugin_read_all (void)
1118 {
1119 uc_check_timeout ();
1121 return;
1122 } /* void plugin_read_all */
1124 /* Read function called when the `-T' command line argument is given. */
1125 int plugin_read_all_once (void)
1126 {
1127 int status;
1128 int return_status = 0;
1130 if (read_heap == NULL)
1131 {
1132 NOTICE ("No read-functions are registered.");
1133 return (0);
1134 }
1136 while (42)
1137 {
1138 read_func_t *rf;
1140 rf = c_heap_get_root (read_heap);
1141 if (rf == NULL)
1142 break;
1144 if (rf->rf_type == RF_SIMPLE)
1145 {
1146 int (*callback) (void);
1148 callback = rf->rf_callback;
1149 status = (*callback) ();
1150 }
1151 else
1152 {
1153 plugin_read_cb callback;
1155 callback = rf->rf_callback;
1156 status = (*callback) (&rf->rf_udata);
1157 }
1159 if (status != 0)
1160 {
1161 NOTICE ("read-function of plugin `%s' failed.",
1162 rf->rf_name);
1163 return_status = -1;
1164 }
1166 destroy_callback ((void *) rf);
1167 }
1169 return (return_status);
1170 } /* int plugin_read_all_once */
1172 int plugin_write (const char *plugin, /* {{{ */
1173 const data_set_t *ds, const value_list_t *vl)
1174 {
1175 llentry_t *le;
1176 int status;
1178 if (vl == NULL)
1179 return (EINVAL);
1181 if (list_write == NULL)
1182 return (ENOENT);
1184 if (ds == NULL)
1185 {
1186 ds = plugin_get_ds (vl->type);
1187 if (ds == NULL)
1188 {
1189 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1190 return (ENOENT);
1191 }
1192 }
1194 if (plugin == NULL)
1195 {
1196 int success = 0;
1197 int failure = 0;
1199 le = llist_head (list_write);
1200 while (le != NULL)
1201 {
1202 callback_func_t *cf = le->value;
1203 plugin_write_cb callback;
1205 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1206 callback = cf->cf_callback;
1207 status = (*callback) (ds, vl, &cf->cf_udata);
1208 if (status != 0)
1209 failure++;
1210 else
1211 success++;
1213 le = le->next;
1214 }
1216 if ((success == 0) && (failure != 0))
1217 status = -1;
1218 else
1219 status = 0;
1220 }
1221 else /* plugin != NULL */
1222 {
1223 callback_func_t *cf;
1224 plugin_write_cb callback;
1226 le = llist_head (list_write);
1227 while (le != NULL)
1228 {
1229 if (strcasecmp (plugin, le->key) == 0)
1230 break;
1232 le = le->next;
1233 }
1235 if (le == NULL)
1236 return (ENOENT);
1238 cf = le->value;
1240 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1241 callback = cf->cf_callback;
1242 status = (*callback) (ds, vl, &cf->cf_udata);
1243 }
1245 return (status);
1246 } /* }}} int plugin_write */
1248 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1249 {
1250 llentry_t *le;
1252 if (list_flush == NULL)
1253 return (0);
1255 le = llist_head (list_flush);
1256 while (le != NULL)
1257 {
1258 callback_func_t *cf;
1259 plugin_flush_cb callback;
1261 if ((plugin != NULL)
1262 && (strcmp (plugin, le->key) != 0))
1263 {
1264 le = le->next;
1265 continue;
1266 }
1268 cf = le->value;
1269 callback = cf->cf_callback;
1271 (*callback) (timeout, identifier, &cf->cf_udata);
1273 le = le->next;
1274 }
1275 return (0);
1276 } /* int plugin_flush */
1278 void plugin_shutdown_all (void)
1279 {
1280 llentry_t *le;
1282 stop_read_threads ();
1284 destroy_all_callbacks (&list_init);
1286 pthread_mutex_lock (&read_lock);
1287 llist_destroy (read_list);
1288 read_list = NULL;
1289 pthread_mutex_unlock (&read_lock);
1291 destroy_read_heap ();
1293 plugin_flush (/* plugin = */ NULL,
1294 /* timeout = */ 0,
1295 /* identifier = */ NULL);
1297 le = NULL;
1298 if (list_shutdown != NULL)
1299 le = llist_head (list_shutdown);
1301 while (le != NULL)
1302 {
1303 callback_func_t *cf;
1304 plugin_shutdown_cb callback;
1306 cf = le->value;
1307 callback = cf->cf_callback;
1309 /* Advance the pointer before calling the callback allows
1310 * shutdown functions to unregister themselves. If done the
1311 * other way around the memory `le' points to will be freed
1312 * after callback returns. */
1313 le = le->next;
1315 (*callback) ();
1316 }
1318 /* Write plugins which use the `user_data' pointer usually need the
1319 * same data available to the flush callback. If this is the case, set
1320 * the free_function to NULL when registering the flush callback and to
1321 * the real free function when registering the write callback. This way
1322 * the data isn't freed twice. */
1323 destroy_all_callbacks (&list_flush);
1324 destroy_all_callbacks (&list_write);
1326 destroy_all_callbacks (&list_notification);
1327 destroy_all_callbacks (&list_shutdown);
1328 destroy_all_callbacks (&list_log);
1329 } /* void plugin_shutdown_all */
1331 int plugin_dispatch_values (value_list_t *vl)
1332 {
1333 int status;
1334 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1336 value_t *saved_values;
1337 int saved_values_len;
1339 data_set_t *ds;
1341 int free_meta_data = 0;
1343 if ((vl == NULL) || (vl->type[0] == 0)
1344 || (vl->values == NULL) || (vl->values_len < 1))
1345 {
1346 ERROR ("plugin_dispatch_values: Invalid value list.");
1347 return (-1);
1348 }
1350 /* Free meta data only if the calling function didn't specify any. In
1351 * this case matches and targets may add some and the calling function
1352 * may not expect (and therefore free) that data. */
1353 if (vl->meta == NULL)
1354 free_meta_data = 1;
1356 if (list_write == NULL)
1357 c_complain_once (LOG_WARNING, &no_write_complaint,
1358 "plugin_dispatch_values: No write callback has been "
1359 "registered. Please load at least one output plugin, "
1360 "if you want the collected data to be stored.");
1362 if (data_sets == NULL)
1363 {
1364 ERROR ("plugin_dispatch_values: No data sets registered. "
1365 "Could the types database be read? Check "
1366 "your `TypesDB' setting!");
1367 return (-1);
1368 }
1370 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1371 {
1372 char ident[6 * DATA_MAX_NAME_LEN];
1374 FORMAT_VL (ident, sizeof (ident), vl);
1375 INFO ("plugin_dispatch_values: Dataset not found: %s "
1376 "(from \"%s\"), check your types.db!",
1377 vl->type, ident);
1378 return (-1);
1379 }
1381 if (vl->time == 0)
1382 vl->time = cdtime ();
1384 if (vl->interval <= 0)
1385 vl->interval = interval_g;
1387 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1388 "host = %s; "
1389 "plugin = %s; plugin_instance = %s; "
1390 "type = %s; type_instance = %s;",
1391 CDTIME_T_TO_DOUBLE (vl->time),
1392 CDTIME_T_TO_DOUBLE (vl->interval),
1393 vl->host,
1394 vl->plugin, vl->plugin_instance,
1395 vl->type, vl->type_instance);
1397 #if COLLECT_DEBUG
1398 assert (0 == strcmp (ds->type, vl->type));
1399 #else
1400 if (0 != strcmp (ds->type, vl->type))
1401 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1402 ds->type, vl->type);
1403 #endif
1405 #if COLLECT_DEBUG
1406 assert (ds->ds_num == vl->values_len);
1407 #else
1408 if (ds->ds_num != vl->values_len)
1409 {
1410 ERROR ("plugin_dispatch_values: ds->type = %s: "
1411 "(ds->ds_num = %i) != "
1412 "(vl->values_len = %i)",
1413 ds->type, ds->ds_num, vl->values_len);
1414 return (-1);
1415 }
1416 #endif
1418 escape_slashes (vl->host, sizeof (vl->host));
1419 escape_slashes (vl->plugin, sizeof (vl->plugin));
1420 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1421 escape_slashes (vl->type, sizeof (vl->type));
1422 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1424 /* Copy the values. This way, we can assure `targets' that they get
1425 * dynamically allocated values, which they can free and replace if
1426 * they like. */
1427 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1428 {
1429 saved_values = vl->values;
1430 saved_values_len = vl->values_len;
1432 vl->values = (value_t *) calloc (vl->values_len,
1433 sizeof (*vl->values));
1434 if (vl->values == NULL)
1435 {
1436 ERROR ("plugin_dispatch_values: calloc failed.");
1437 vl->values = saved_values;
1438 return (-1);
1439 }
1440 memcpy (vl->values, saved_values,
1441 vl->values_len * sizeof (*vl->values));
1442 }
1443 else /* if ((pre == NULL) && (post == NULL)) */
1444 {
1445 saved_values = NULL;
1446 saved_values_len = 0;
1447 }
1449 if (pre_cache_chain != NULL)
1450 {
1451 status = fc_process_chain (ds, vl, pre_cache_chain);
1452 if (status < 0)
1453 {
1454 WARNING ("plugin_dispatch_values: Running the "
1455 "pre-cache chain failed with "
1456 "status %i (%#x).",
1457 status, status);
1458 }
1459 else if (status == FC_TARGET_STOP)
1460 {
1461 /* Restore the state of the value_list so that plugins
1462 * don't get confused.. */
1463 if (saved_values != NULL)
1464 {
1465 free (vl->values);
1466 vl->values = saved_values;
1467 vl->values_len = saved_values_len;
1468 }
1469 return (0);
1470 }
1471 }
1473 /* Update the value cache */
1474 uc_update (ds, vl);
1476 /* Initiate threshold checking */
1477 ut_check_threshold (ds, vl);
1479 if (post_cache_chain != NULL)
1480 {
1481 status = fc_process_chain (ds, vl, post_cache_chain);
1482 if (status < 0)
1483 {
1484 WARNING ("plugin_dispatch_values: Running the "
1485 "post-cache chain failed with "
1486 "status %i (%#x).",
1487 status, status);
1488 }
1489 }
1490 else
1491 fc_default_action (ds, vl);
1493 /* Restore the state of the value_list so that plugins don't get
1494 * confused.. */
1495 if (saved_values != NULL)
1496 {
1497 free (vl->values);
1498 vl->values = saved_values;
1499 vl->values_len = saved_values_len;
1500 }
1502 if ((free_meta_data != 0) && (vl->meta != NULL))
1503 {
1504 meta_data_destroy (vl->meta);
1505 vl->meta = NULL;
1506 }
1508 return (0);
1509 } /* int plugin_dispatch_values */
1511 int plugin_dispatch_notification (const notification_t *notif)
1512 {
1513 llentry_t *le;
1514 /* Possible TODO: Add flap detection here */
1516 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1517 "time = %.3f; host = %s;",
1518 notif->severity, notif->message,
1519 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1521 /* Nobody cares for notifications */
1522 if (list_notification == NULL)
1523 return (-1);
1525 le = llist_head (list_notification);
1526 while (le != NULL)
1527 {
1528 callback_func_t *cf;
1529 plugin_notification_cb callback;
1530 int status;
1532 cf = le->value;
1533 callback = cf->cf_callback;
1534 status = (*callback) (notif, &cf->cf_udata);
1535 if (status != 0)
1536 {
1537 WARNING ("plugin_dispatch_notification: Notification "
1538 "callback %s returned %i.",
1539 le->key, status);
1540 }
1542 le = le->next;
1543 }
1545 return (0);
1546 } /* int plugin_dispatch_notification */
1548 void plugin_log (int level, const char *format, ...)
1549 {
1550 char msg[1024];
1551 va_list ap;
1552 llentry_t *le;
1554 #if !COLLECT_DEBUG
1555 if (level >= LOG_DEBUG)
1556 return;
1557 #endif
1559 va_start (ap, format);
1560 vsnprintf (msg, sizeof (msg), format, ap);
1561 msg[sizeof (msg) - 1] = '\0';
1562 va_end (ap);
1564 if (list_log == NULL)
1565 {
1566 fprintf (stderr, "%s\n", msg);
1567 return;
1568 }
1570 le = llist_head (list_log);
1571 while (le != NULL)
1572 {
1573 callback_func_t *cf;
1574 plugin_log_cb callback;
1576 cf = le->value;
1577 callback = cf->cf_callback;
1579 (*callback) (level, msg, &cf->cf_udata);
1581 le = le->next;
1582 }
1583 } /* void plugin_log */
1585 const data_set_t *plugin_get_ds (const char *name)
1586 {
1587 data_set_t *ds;
1589 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
1590 {
1591 DEBUG ("No such dataset registered: %s", name);
1592 return (NULL);
1593 }
1595 return (ds);
1596 } /* data_set_t *plugin_get_ds */
1598 static int plugin_notification_meta_add (notification_t *n,
1599 const char *name,
1600 enum notification_meta_type_e type,
1601 const void *value)
1602 {
1603 notification_meta_t *meta;
1604 notification_meta_t *tail;
1606 if ((n == NULL) || (name == NULL) || (value == NULL))
1607 {
1608 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
1609 return (-1);
1610 }
1612 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
1613 if (meta == NULL)
1614 {
1615 ERROR ("plugin_notification_meta_add: malloc failed.");
1616 return (-1);
1617 }
1618 memset (meta, 0, sizeof (notification_meta_t));
1620 sstrncpy (meta->name, name, sizeof (meta->name));
1621 meta->type = type;
1623 switch (type)
1624 {
1625 case NM_TYPE_STRING:
1626 {
1627 meta->nm_value.nm_string = strdup ((const char *) value);
1628 if (meta->nm_value.nm_string == NULL)
1629 {
1630 ERROR ("plugin_notification_meta_add: strdup failed.");
1631 sfree (meta);
1632 return (-1);
1633 }
1634 break;
1635 }
1636 case NM_TYPE_SIGNED_INT:
1637 {
1638 meta->nm_value.nm_signed_int = *((int64_t *) value);
1639 break;
1640 }
1641 case NM_TYPE_UNSIGNED_INT:
1642 {
1643 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
1644 break;
1645 }
1646 case NM_TYPE_DOUBLE:
1647 {
1648 meta->nm_value.nm_double = *((double *) value);
1649 break;
1650 }
1651 case NM_TYPE_BOOLEAN:
1652 {
1653 meta->nm_value.nm_boolean = *((_Bool *) value);
1654 break;
1655 }
1656 default:
1657 {
1658 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
1659 sfree (meta);
1660 return (-1);
1661 }
1662 } /* switch (type) */
1664 meta->next = NULL;
1665 tail = n->meta;
1666 while ((tail != NULL) && (tail->next != NULL))
1667 tail = tail->next;
1669 if (tail == NULL)
1670 n->meta = meta;
1671 else
1672 tail->next = meta;
1674 return (0);
1675 } /* int plugin_notification_meta_add */
1677 int plugin_notification_meta_add_string (notification_t *n,
1678 const char *name,
1679 const char *value)
1680 {
1681 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
1682 }
1684 int plugin_notification_meta_add_signed_int (notification_t *n,
1685 const char *name,
1686 int64_t value)
1687 {
1688 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
1689 }
1691 int plugin_notification_meta_add_unsigned_int (notification_t *n,
1692 const char *name,
1693 uint64_t value)
1694 {
1695 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
1696 }
1698 int plugin_notification_meta_add_double (notification_t *n,
1699 const char *name,
1700 double value)
1701 {
1702 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
1703 }
1705 int plugin_notification_meta_add_boolean (notification_t *n,
1706 const char *name,
1707 _Bool value)
1708 {
1709 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
1710 }
1712 int plugin_notification_meta_copy (notification_t *dst,
1713 const notification_t *src)
1714 {
1715 notification_meta_t *meta;
1717 assert (dst != NULL);
1718 assert (src != NULL);
1719 assert (dst != src);
1720 assert ((src->meta == NULL) || (src->meta != dst->meta));
1722 for (meta = src->meta; meta != NULL; meta = meta->next)
1723 {
1724 if (meta->type == NM_TYPE_STRING)
1725 plugin_notification_meta_add_string (dst, meta->name,
1726 meta->nm_value.nm_string);
1727 else if (meta->type == NM_TYPE_SIGNED_INT)
1728 plugin_notification_meta_add_signed_int (dst, meta->name,
1729 meta->nm_value.nm_signed_int);
1730 else if (meta->type == NM_TYPE_UNSIGNED_INT)
1731 plugin_notification_meta_add_unsigned_int (dst, meta->name,
1732 meta->nm_value.nm_unsigned_int);
1733 else if (meta->type == NM_TYPE_DOUBLE)
1734 plugin_notification_meta_add_double (dst, meta->name,
1735 meta->nm_value.nm_double);
1736 else if (meta->type == NM_TYPE_BOOLEAN)
1737 plugin_notification_meta_add_boolean (dst, meta->name,
1738 meta->nm_value.nm_boolean);
1739 }
1741 return (0);
1742 } /* int plugin_notification_meta_copy */
1744 int plugin_notification_meta_free (notification_meta_t *n)
1745 {
1746 notification_meta_t *this;
1747 notification_meta_t *next;
1749 if (n == NULL)
1750 {
1751 ERROR ("plugin_notification_meta_free: n == NULL!");
1752 return (-1);
1753 }
1755 this = n;
1756 while (this != NULL)
1757 {
1758 next = this->next;
1760 if (this->type == NM_TYPE_STRING)
1761 {
1762 free ((char *)this->nm_value.nm_string);
1763 this->nm_value.nm_string = NULL;
1764 }
1765 sfree (this);
1767 this = next;
1768 }
1770 return (0);
1771 } /* int plugin_notification_meta_free */
1773 /* vim: set sw=8 ts=8 noet fdm=marker : */