2 * collectd - src/plugin.c
3 * Copyright (C) 2005-2013 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at collectd.org>
20 * Sebastian Harl <sh at tokkee.org>
26 #include "configfile.h"
27 #include "filter_chain.h"
28 #include "utils_avltree.h"
29 #include "utils_cache.h"
30 #include "utils_complain.h"
31 #include "utils_llist.h"
32 #include "utils_heap.h"
33 #include "utils_time.h"
44 struct callback_func_s
50 typedef struct callback_func_s callback_func_t;
54 #define RF_REMOVE 65535
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 #define rf_ctx rf_super.cf_ctx
62 callback_func_t rf_super;
63 char rf_group[DATA_MAX_NAME_LEN];
64 char rf_name[DATA_MAX_NAME_LEN];
67 cdtime_t rf_effective_interval;
68 cdtime_t rf_next_read;
70 typedef struct read_func_s read_func_t;
73 typedef struct write_queue_s write_queue_t;
84 static llist_t *list_init;
85 static llist_t *list_write;
86 static llist_t *list_flush;
87 static llist_t *list_missing;
88 static llist_t *list_shutdown;
89 static llist_t *list_log;
90 static llist_t *list_notification;
92 static fc_chain_t *pre_cache_chain = NULL;
93 static fc_chain_t *post_cache_chain = NULL;
95 static c_avl_tree_t *data_sets;
97 static char *plugindir = NULL;
99 static c_heap_t *read_heap = NULL;
100 static llist_t *read_list;
101 static int read_loop = 1;
102 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
103 static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
104 static pthread_t *read_threads = NULL;
105 static int read_threads_num = 0;
107 static write_queue_t *write_queue_head;
108 static write_queue_t *write_queue_tail;
109 static _Bool write_loop = 1;
110 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
111 static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
112 static pthread_t *write_threads = NULL;
113 static size_t write_threads_num = 0;
115 static pthread_key_t plugin_ctx_key;
116 static _Bool plugin_ctx_key_initialized = 0;
121 static int plugin_dispatch_values_internal (value_list_t *vl);
123 static const char *plugin_get_dir (void)
125 if (plugindir == NULL)
131 static void destroy_callback (callback_func_t *cf) /* {{{ */
136 if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
138 cf->cf_udata.free_func (cf->cf_udata.data);
139 cf->cf_udata.data = NULL;
140 cf->cf_udata.free_func = NULL;
143 } /* }}} void destroy_callback */
145 static void destroy_all_callbacks (llist_t **list) /* {{{ */
152 le = llist_head (*list);
160 destroy_callback (le->value);
166 llist_destroy (*list);
168 } /* }}} void destroy_all_callbacks */
170 static void destroy_read_heap (void) /* {{{ */
172 if (read_heap == NULL)
179 cf = c_heap_get_root (read_heap);
183 destroy_callback (cf);
186 c_heap_destroy (read_heap);
188 } /* }}} void destroy_read_heap */
190 static int register_callback (llist_t **list, /* {{{ */
191 const char *name, callback_func_t *cf)
198 *list = llist_create ();
201 ERROR ("plugin: register_callback: "
202 "llist_create failed.");
203 destroy_callback (cf);
211 ERROR ("plugin: register_callback: strdup failed.");
212 destroy_callback (cf);
216 le = llist_search (*list, name);
219 le = llentry_create (key, cf);
222 ERROR ("plugin: register_callback: "
223 "llentry_create failed.");
225 destroy_callback (cf);
229 llist_append (*list, le);
233 callback_func_t *old_cf;
238 WARNING ("plugin: register_callback: "
239 "a callback named `%s' already exists - "
240 "overwriting the old entry!", name);
242 destroy_callback (old_cf);
247 } /* }}} int register_callback */
249 static int create_register_callback (llist_t **list, /* {{{ */
250 const char *name, void *callback, user_data_t *ud)
254 cf = (callback_func_t *) malloc (sizeof (*cf));
257 ERROR ("plugin: create_register_callback: malloc failed.");
260 memset (cf, 0, sizeof (*cf));
262 cf->cf_callback = callback;
265 cf->cf_udata.data = NULL;
266 cf->cf_udata.free_func = NULL;
273 cf->cf_ctx = plugin_get_ctx ();
275 return (register_callback (list, name, cf));
276 } /* }}} int create_register_callback */
278 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
285 e = llist_search (list, name);
289 llist_remove (list, e);
292 destroy_callback (e->value);
297 } /* }}} int plugin_unregister */
300 * (Try to) load the shared object `file'. Won't complain if it isn't a shared
301 * object, but it will bitch about a shared object not having a
302 * ``module_register'' symbol..
304 static int plugin_load_file (char *file, uint32_t flags)
307 void (*reg_handle) (void);
310 lt_dlerror (); /* clear errors */
312 #if LIBTOOL_VERSION == 2
313 if (flags & PLUGIN_FLAGS_GLOBAL) {
315 lt_dladvise_init(&advise);
316 lt_dladvise_global(&advise);
317 dlh = lt_dlopenadvise(file, advise);
318 lt_dladvise_destroy(&advise);
320 dlh = lt_dlopen (file);
322 #else /* if LIBTOOL_VERSION == 1 */
323 if (flags & PLUGIN_FLAGS_GLOBAL)
324 WARNING ("plugin_load_file: The global flag is not supported, "
325 "libtool 2 is required for this.");
326 dlh = lt_dlopen (file);
331 char errbuf[1024] = "";
333 ssnprintf (errbuf, sizeof (errbuf),
334 "lt_dlopen (\"%s\") failed: %s. "
335 "The most common cause for this problem are "
336 "missing dependencies. Use ldd(1) to check "
337 "the dependencies of the plugin "
339 file, lt_dlerror ());
341 ERROR ("%s", errbuf);
342 /* Make sure this is printed to STDERR in any case, but also
343 * make sure it's printed only once. */
344 if (list_log != NULL)
345 fprintf (stderr, "ERROR: %s\n", errbuf);
350 if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
352 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
353 file, lt_dlerror ());
363 static void *plugin_read_thread (void __attribute__((unused)) *args)
365 while (read_loop != 0)
368 plugin_ctx_t old_ctx;
374 /* Get the read function that needs to be read next.
375 * We don't need to hold "read_lock" for the heap, but we need
376 * to call c_heap_get_root() and pthread_cond_wait() in the
377 * same protected block. */
378 pthread_mutex_lock (&read_lock);
379 rf = c_heap_get_root (read_heap);
382 pthread_cond_wait (&read_cond, &read_lock);
383 pthread_mutex_unlock (&read_lock);
386 pthread_mutex_unlock (&read_lock);
388 if (rf->rf_interval == 0)
390 /* this should not happen, because the interval is set
391 * for each plugin when loading it
392 * XXX: issue a warning? */
393 rf->rf_interval = plugin_get_interval ();
394 rf->rf_effective_interval = rf->rf_interval;
396 rf->rf_next_read = cdtime ();
399 /* sleep until this entry is due,
400 * using pthread_cond_timedwait */
401 pthread_mutex_lock (&read_lock);
402 /* In pthread_cond_timedwait, spurious wakeups are possible
403 * (and really happen, at least on NetBSD with > 1 CPU), thus
404 * we need to re-evaluate the condition every time
405 * pthread_cond_timedwait returns. */
407 while ((read_loop != 0)
408 && (cdtime () < rf->rf_next_read)
411 struct timespec ts = { 0 };
413 CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
415 rc = pthread_cond_timedwait (&read_cond, &read_lock,
419 /* Must hold `read_lock' when accessing `rf->rf_type'. */
420 rf_type = rf->rf_type;
421 pthread_mutex_unlock (&read_lock);
423 /* Check if we're supposed to stop.. This may have interrupted
427 /* Insert `rf' again, so it can be free'd correctly */
428 c_heap_insert (read_heap, rf);
432 /* The entry has been marked for deletion. The linked list
433 * entry has already been removed by `plugin_unregister_read'.
434 * All we have to do here is free the `read_func_t' and
436 if (rf_type == RF_REMOVE)
438 DEBUG ("plugin_read_thread: Destroying the `%s' "
439 "callback.", rf->rf_name);
440 destroy_callback ((callback_func_t *) rf);
445 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
447 old_ctx = plugin_set_ctx (rf->rf_ctx);
449 if (rf_type == RF_SIMPLE)
451 int (*callback) (void);
453 callback = rf->rf_callback;
454 status = (*callback) ();
458 plugin_read_cb callback;
460 assert (rf_type == RF_COMPLEX);
462 callback = rf->rf_callback;
463 status = (*callback) (&rf->rf_udata);
466 plugin_set_ctx (old_ctx);
468 /* If the function signals failure, we will increase the
469 * intervals in which it will be called. */
472 rf->rf_effective_interval *= 2;
473 if (rf->rf_effective_interval > TIME_T_TO_CDTIME_T (86400))
474 rf->rf_effective_interval = TIME_T_TO_CDTIME_T (86400);
476 NOTICE ("read-function of plugin `%s' failed. "
477 "Will suspend it for %.3f seconds.",
479 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
483 /* Success: Restore the interval, if it was changed. */
484 rf->rf_effective_interval = rf->rf_interval;
487 /* update the ``next read due'' field */
490 DEBUG ("plugin_read_thread: Effective interval of the "
491 "%s plugin is %.3f seconds.",
493 CDTIME_T_TO_DOUBLE (rf->rf_effective_interval));
495 /* Calculate the next (absolute) time at which this function
496 * should be called. */
497 rf->rf_next_read += rf->rf_effective_interval;
499 /* Check, if `rf_next_read' is in the past. */
500 if (rf->rf_next_read < now)
502 /* `rf_next_read' is in the past. Insert `now'
503 * so this value doesn't trail off into the
505 rf->rf_next_read = now;
508 DEBUG ("plugin_read_thread: Next read of the %s plugin at %.3f.",
510 CDTIME_T_TO_DOUBLE (rf->rf_next_read));
512 /* Re-insert this read function into the heap again. */
513 c_heap_insert (read_heap, rf);
514 } /* while (read_loop) */
518 } /* void *plugin_read_thread */
520 static void start_read_threads (int num)
524 if (read_threads != NULL)
527 read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
528 if (read_threads == NULL)
530 ERROR ("plugin: start_read_threads: calloc failed.");
534 read_threads_num = 0;
535 for (i = 0; i < num; i++)
537 if (pthread_create (read_threads + read_threads_num, NULL,
538 plugin_read_thread, NULL) == 0)
544 ERROR ("plugin: start_read_threads: pthread_create failed.");
548 } /* void start_read_threads */
550 static void stop_read_threads (void)
554 if (read_threads == NULL)
557 INFO ("collectd: Stopping %i read threads.", read_threads_num);
559 pthread_mutex_lock (&read_lock);
561 DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
562 pthread_cond_broadcast (&read_cond);
563 pthread_mutex_unlock (&read_lock);
565 for (i = 0; i < read_threads_num; i++)
567 if (pthread_join (read_threads[i], NULL) != 0)
569 ERROR ("plugin: stop_read_threads: pthread_join failed.");
571 read_threads[i] = (pthread_t) 0;
573 sfree (read_threads);
574 read_threads_num = 0;
575 } /* void stop_read_threads */
577 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
582 meta_data_destroy (vl->meta);
585 } /* }}} void plugin_value_list_free */
587 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
594 vl = malloc (sizeof (*vl));
597 memcpy (vl, vl_orig, sizeof (*vl));
599 vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
600 if (vl->values == NULL)
602 plugin_value_list_free (vl);
605 memcpy (vl->values, vl_orig->values,
606 vl_orig->values_len * sizeof (*vl->values));
608 vl->meta = meta_data_clone (vl->meta);
609 if ((vl_orig->meta != NULL) && (vl->meta == NULL))
611 plugin_value_list_free (vl);
616 vl->time = cdtime ();
618 /* Fill in the interval from the thread context, if it is zero. */
619 if (vl->interval == 0)
621 plugin_ctx_t ctx = plugin_get_ctx ();
623 if (ctx.interval != 0)
624 vl->interval = ctx.interval;
627 char name[6 * DATA_MAX_NAME_LEN];
628 FORMAT_VL (name, sizeof (name), vl);
629 ERROR ("plugin_value_list_clone: Unable to determine "
630 "interval from context for "
631 "value list \"%s\". "
632 "This indicates a broken plugin. "
633 "Please report this problem to the "
634 "collectd mailing list or at "
635 "<http://collectd.org/bugs/>.", name);
636 vl->interval = cf_get_default_interval ();
641 } /* }}} value_list_t *plugin_value_list_clone */
643 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
647 q = malloc (sizeof (*q));
652 q->vl = plugin_value_list_clone (vl);
659 /* Store context of caller (read plugin); otherwise, it would not be
660 * available to the write plugins when actually dispatching the
661 * value-list later on. */
662 q->ctx = plugin_get_ctx ();
664 pthread_mutex_lock (&write_lock);
666 if (write_queue_tail == NULL)
668 write_queue_head = q;
669 write_queue_tail = q;
673 write_queue_tail->next = q;
674 write_queue_tail = q;
677 pthread_cond_signal (&write_cond);
678 pthread_mutex_unlock (&write_lock);
681 } /* }}} int plugin_write_enqueue */
683 static value_list_t *plugin_write_dequeue (void) /* {{{ */
688 pthread_mutex_lock (&write_lock);
690 while (write_loop && (write_queue_head == NULL))
691 pthread_cond_wait (&write_cond, &write_lock);
693 if (write_queue_head == NULL)
695 pthread_mutex_unlock (&write_lock);
699 q = write_queue_head;
700 write_queue_head = q->next;
701 if (write_queue_head == NULL)
702 write_queue_tail = NULL;
704 pthread_mutex_unlock (&write_lock);
706 (void) plugin_set_ctx (q->ctx);
711 } /* }}} value_list_t *plugin_write_dequeue */
713 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
717 value_list_t *vl = plugin_write_dequeue ();
721 plugin_dispatch_values_internal (vl);
723 plugin_value_list_free (vl);
728 } /* }}} void *plugin_write_thread */
730 static void start_write_threads (size_t num) /* {{{ */
734 if (write_threads != NULL)
737 write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
738 if (write_threads == NULL)
740 ERROR ("plugin: start_write_threads: calloc failed.");
744 write_threads_num = 0;
745 for (i = 0; i < num; i++)
749 status = pthread_create (write_threads + write_threads_num,
756 ERROR ("plugin: start_write_threads: pthread_create failed "
757 "with status %i (%s).", status,
758 sstrerror (status, errbuf, sizeof (errbuf)));
764 } /* }}} void start_write_threads */
766 static void stop_write_threads (void) /* {{{ */
771 if (write_threads == NULL)
774 INFO ("collectd: Stopping %zu write threads.", write_threads_num);
776 pthread_mutex_lock (&write_lock);
778 DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
779 pthread_cond_broadcast (&write_cond);
780 pthread_mutex_unlock (&write_lock);
782 for (i = 0; i < write_threads_num; i++)
784 if (pthread_join (write_threads[i], NULL) != 0)
786 ERROR ("plugin: stop_write_threads: pthread_join failed.");
788 write_threads[i] = (pthread_t) 0;
790 sfree (write_threads);
791 write_threads_num = 0;
793 pthread_mutex_lock (&write_lock);
795 for (q = write_queue_head; q != NULL; )
797 write_queue_t *q1 = q;
798 plugin_value_list_free (q->vl);
803 write_queue_head = NULL;
804 write_queue_tail = NULL;
805 pthread_mutex_unlock (&write_lock);
809 WARNING ("plugin: %i value list%s left after shutting down "
810 "the write threads.",
811 i, (i == 1) ? " was" : "s were");
813 } /* }}} void stop_write_threads */
818 void plugin_set_dir (const char *dir)
820 if (plugindir != NULL)
825 else if ((plugindir = strdup (dir)) == NULL)
828 ERROR ("strdup failed: %s",
829 sstrerror (errno, errbuf, sizeof (errbuf)));
834 int plugin_load (const char *type, uint32_t flags)
838 char filename[BUFSIZE] = "";
839 char typename[BUFSIZE];
846 dir = plugin_get_dir ();
849 /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
850 * type when matching the filename */
851 status = ssnprintf (typename, sizeof (typename), "%s.so", type);
852 if ((status < 0) || ((size_t) status >= sizeof (typename)))
854 WARNING ("plugin_load: Filename too long: \"%s.so\"", type);
857 typename_len = strlen (typename);
859 if ((dh = opendir (dir)) == NULL)
862 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
863 sstrerror (errno, errbuf, sizeof (errbuf)));
867 while ((de = readdir (dh)) != NULL)
869 if (strncasecmp (de->d_name, typename, typename_len))
872 status = ssnprintf (filename, sizeof (filename),
873 "%s/%s", dir, de->d_name);
874 if ((status < 0) || ((size_t) status >= sizeof (filename)))
876 WARNING ("plugin_load: Filename too long: \"%s/%s\"",
881 if (lstat (filename, &statbuf) == -1)
884 WARNING ("plugin_load: stat (\"%s\") failed: %s",
886 sstrerror (errno, errbuf, sizeof (errbuf)));
889 else if (!S_ISREG (statbuf.st_mode))
891 /* don't follow symlinks */
892 WARNING ("plugin_load: %s is not a regular file.",
897 status = plugin_load_file (filename, flags);
906 ERROR ("plugin_load: Load plugin \"%s\" failed with "
907 "status %i.", type, status);
913 if (filename[0] == 0)
914 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
921 * The `register_*' functions follow
923 int plugin_register_config (const char *name,
924 int (*callback) (const char *key, const char *val),
925 const char **keys, int keys_num)
927 cf_register (name, callback, keys, keys_num);
929 } /* int plugin_register_config */
931 int plugin_register_complex_config (const char *type,
932 int (*callback) (oconfig_item_t *))
934 return (cf_register_complex (type, callback));
935 } /* int plugin_register_complex_config */
937 int plugin_register_init (const char *name,
938 int (*callback) (void))
940 return (create_register_callback (&list_init, name, (void *) callback,
941 /* user_data = */ NULL));
942 } /* plugin_register_init */
944 static int plugin_compare_read_func (const void *arg0, const void *arg1)
946 const read_func_t *rf0;
947 const read_func_t *rf1;
952 if (rf0->rf_next_read < rf1->rf_next_read)
954 else if (rf0->rf_next_read > rf1->rf_next_read)
958 } /* int plugin_compare_read_func */
960 /* Add a read function to both, the heap and a linked list. The linked list if
961 * used to look-up read functions, especially for the remove function. The heap
962 * is used to determine which plugin to read next. */
963 static int plugin_insert_read (read_func_t *rf)
968 rf->rf_next_read = cdtime ();
969 rf->rf_effective_interval = rf->rf_interval;
971 pthread_mutex_lock (&read_lock);
973 if (read_list == NULL)
975 read_list = llist_create ();
976 if (read_list == NULL)
978 pthread_mutex_unlock (&read_lock);
979 ERROR ("plugin_insert_read: read_list failed.");
984 if (read_heap == NULL)
986 read_heap = c_heap_create (plugin_compare_read_func);
987 if (read_heap == NULL)
989 pthread_mutex_unlock (&read_lock);
990 ERROR ("plugin_insert_read: c_heap_create failed.");
995 le = llist_search (read_list, rf->rf_name);
998 pthread_mutex_unlock (&read_lock);
999 WARNING ("The read function \"%s\" is already registered. "
1000 "Check for duplicate \"LoadPlugin\" lines "
1001 "in your configuration!",
1006 le = llentry_create (rf->rf_name, rf);
1009 pthread_mutex_unlock (&read_lock);
1010 ERROR ("plugin_insert_read: llentry_create failed.");
1014 status = c_heap_insert (read_heap, rf);
1017 pthread_mutex_unlock (&read_lock);
1018 ERROR ("plugin_insert_read: c_heap_insert failed.");
1019 llentry_destroy (le);
1023 /* This does not fail. */
1024 llist_append (read_list, le);
1026 /* Wake up all the read threads. */
1027 pthread_cond_broadcast (&read_cond);
1028 pthread_mutex_unlock (&read_lock);
1030 } /* int plugin_insert_read */
1032 int plugin_register_read (const char *name,
1033 int (*callback) (void))
1038 rf = malloc (sizeof (*rf));
1041 ERROR ("plugin_register_read: malloc failed.");
1045 memset (rf, 0, sizeof (read_func_t));
1046 rf->rf_callback = (void *) callback;
1047 rf->rf_udata.data = NULL;
1048 rf->rf_udata.free_func = NULL;
1049 rf->rf_ctx = plugin_get_ctx ();
1050 rf->rf_group[0] = '\0';
1051 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1052 rf->rf_type = RF_SIMPLE;
1053 rf->rf_interval = plugin_get_interval ();
1055 status = plugin_insert_read (rf);
1060 } /* int plugin_register_read */
1062 int plugin_register_complex_read (const char *group, const char *name,
1063 plugin_read_cb callback,
1064 const struct timespec *interval,
1065 user_data_t *user_data)
1070 rf = malloc (sizeof (*rf));
1073 ERROR ("plugin_register_complex_read: malloc failed.");
1077 memset (rf, 0, sizeof (read_func_t));
1078 rf->rf_callback = (void *) callback;
1080 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1082 rf->rf_group[0] = '\0';
1083 sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1084 rf->rf_type = RF_COMPLEX;
1085 if (interval != NULL)
1086 rf->rf_interval = TIMESPEC_TO_CDTIME_T (interval);
1088 rf->rf_interval = plugin_get_interval ();
1091 if (user_data == NULL)
1093 rf->rf_udata.data = NULL;
1094 rf->rf_udata.free_func = NULL;
1098 rf->rf_udata = *user_data;
1101 rf->rf_ctx = plugin_get_ctx ();
1103 status = plugin_insert_read (rf);
1108 } /* int plugin_register_complex_read */
1110 int plugin_register_write (const char *name,
1111 plugin_write_cb callback, user_data_t *ud)
1113 return (create_register_callback (&list_write, name,
1114 (void *) callback, ud));
1115 } /* int plugin_register_write */
1117 int plugin_register_flush (const char *name,
1118 plugin_flush_cb callback, user_data_t *ud)
1120 return (create_register_callback (&list_flush, name,
1121 (void *) callback, ud));
1122 } /* int plugin_register_flush */
1124 int plugin_register_missing (const char *name,
1125 plugin_missing_cb callback, user_data_t *ud)
1127 return (create_register_callback (&list_missing, name,
1128 (void *) callback, ud));
1129 } /* int plugin_register_missing */
1131 int plugin_register_shutdown (const char *name,
1132 int (*callback) (void))
1134 return (create_register_callback (&list_shutdown, name,
1135 (void *) callback, /* user_data = */ NULL));
1136 } /* int plugin_register_shutdown */
1138 static void plugin_free_data_sets (void)
1143 if (data_sets == NULL)
1146 while (c_avl_pick (data_sets, &key, &value) == 0)
1148 data_set_t *ds = value;
1149 /* key is a pointer to ds->type */
1155 c_avl_destroy (data_sets);
1157 } /* void plugin_free_data_sets */
1159 int plugin_register_data_set (const data_set_t *ds)
1161 data_set_t *ds_copy;
1164 if ((data_sets != NULL)
1165 && (c_avl_get (data_sets, ds->type, NULL) == 0))
1167 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1168 plugin_unregister_data_set (ds->type);
1170 else if (data_sets == NULL)
1172 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1173 if (data_sets == NULL)
1177 ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1178 if (ds_copy == NULL)
1180 memcpy(ds_copy, ds, sizeof (data_set_t));
1182 ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1184 if (ds_copy->ds == NULL)
1190 for (i = 0; i < ds->ds_num; i++)
1191 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1193 return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1194 } /* int plugin_register_data_set */
1196 int plugin_register_log (const char *name,
1197 plugin_log_cb callback, user_data_t *ud)
1199 return (create_register_callback (&list_log, name,
1200 (void *) callback, ud));
1201 } /* int plugin_register_log */
1203 int plugin_register_notification (const char *name,
1204 plugin_notification_cb callback, user_data_t *ud)
1206 return (create_register_callback (&list_notification, name,
1207 (void *) callback, ud));
1208 } /* int plugin_register_log */
1210 int plugin_unregister_config (const char *name)
1212 cf_unregister (name);
1214 } /* int plugin_unregister_config */
1216 int plugin_unregister_complex_config (const char *name)
1218 cf_unregister_complex (name);
1220 } /* int plugin_unregister_complex_config */
1222 int plugin_unregister_init (const char *name)
1224 return (plugin_unregister (list_init, name));
1227 int plugin_unregister_read (const char *name) /* {{{ */
1235 pthread_mutex_lock (&read_lock);
1237 if (read_list == NULL)
1239 pthread_mutex_unlock (&read_lock);
1243 le = llist_search (read_list, name);
1246 pthread_mutex_unlock (&read_lock);
1247 WARNING ("plugin_unregister_read: No such read function: %s",
1252 llist_remove (read_list, le);
1255 assert (rf != NULL);
1256 rf->rf_type = RF_REMOVE;
1258 pthread_mutex_unlock (&read_lock);
1260 llentry_destroy (le);
1262 DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1265 } /* }}} int plugin_unregister_read */
1267 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1269 read_func_t *rf = e->value;
1272 return strcmp (rf->rf_group, (const char *)group);
1273 } /* }}} int compare_read_func_group */
1275 int plugin_unregister_read_group (const char *group) /* {{{ */
1285 pthread_mutex_lock (&read_lock);
1287 if (read_list == NULL)
1289 pthread_mutex_unlock (&read_lock);
1295 le = llist_search_custom (read_list,
1296 compare_read_func_group, (void *)group);
1303 llist_remove (read_list, le);
1306 assert (rf != NULL);
1307 rf->rf_type = RF_REMOVE;
1309 llentry_destroy (le);
1311 DEBUG ("plugin_unregister_read_group: "
1312 "Marked `%s' (group `%s') for removal.",
1313 rf->rf_name, group);
1316 pthread_mutex_unlock (&read_lock);
1320 WARNING ("plugin_unregister_read_group: No such "
1321 "group of read function: %s", group);
1326 } /* }}} int plugin_unregister_read_group */
1328 int plugin_unregister_write (const char *name)
1330 return (plugin_unregister (list_write, name));
1333 int plugin_unregister_flush (const char *name)
1335 return (plugin_unregister (list_flush, name));
1338 int plugin_unregister_missing (const char *name)
1340 return (plugin_unregister (list_missing, name));
1343 int plugin_unregister_shutdown (const char *name)
1345 return (plugin_unregister (list_shutdown, name));
1348 int plugin_unregister_data_set (const char *name)
1352 if (data_sets == NULL)
1355 if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1362 } /* int plugin_unregister_data_set */
1364 int plugin_unregister_log (const char *name)
1366 return (plugin_unregister (list_log, name));
1369 int plugin_unregister_notification (const char *name)
1371 return (plugin_unregister (list_notification, name));
1374 void plugin_init_all (void)
1376 const char *chain_name;
1380 /* Init the value cache */
1383 chain_name = global_option_get ("PreCacheChain");
1384 pre_cache_chain = fc_chain_get_by_name (chain_name);
1386 chain_name = global_option_get ("PostCacheChain");
1387 post_cache_chain = fc_chain_get_by_name (chain_name);
1390 char const *tmp = global_option_get ("WriteThreads");
1391 int num = atoi (tmp);
1396 start_write_threads ((size_t) num);
1399 if ((list_init == NULL) && (read_heap == NULL))
1402 /* Calling all init callbacks before checking if read callbacks
1403 * are available allows the init callbacks to register the read
1405 le = llist_head (list_init);
1408 callback_func_t *cf;
1409 plugin_init_cb callback;
1410 plugin_ctx_t old_ctx;
1413 old_ctx = plugin_set_ctx (cf->cf_ctx);
1414 callback = cf->cf_callback;
1415 status = (*callback) ();
1416 plugin_set_ctx (old_ctx);
1420 ERROR ("Initialization of plugin `%s' "
1421 "failed with status %i. "
1422 "Plugin will be unloaded.",
1424 /* Plugins that register read callbacks from the init
1425 * callback should take care of appropriate error
1426 * handling themselves. */
1427 /* FIXME: Unload _all_ functions */
1428 plugin_unregister_read (le->key);
1434 /* Start read-threads */
1435 if (read_heap != NULL)
1439 rt = global_option_get ("ReadThreads");
1442 start_read_threads ((num > 0) ? num : 5);
1444 } /* void plugin_init_all */
1446 /* TODO: Rename this function. */
1447 void plugin_read_all (void)
1449 uc_check_timeout ();
1452 } /* void plugin_read_all */
1454 /* Read function called when the `-T' command line argument is given. */
1455 int plugin_read_all_once (void)
1458 int return_status = 0;
1460 if (read_heap == NULL)
1462 NOTICE ("No read-functions are registered.");
1469 plugin_ctx_t old_ctx;
1471 rf = c_heap_get_root (read_heap);
1475 old_ctx = plugin_set_ctx (rf->rf_ctx);
1477 if (rf->rf_type == RF_SIMPLE)
1479 int (*callback) (void);
1481 callback = rf->rf_callback;
1482 status = (*callback) ();
1486 plugin_read_cb callback;
1488 callback = rf->rf_callback;
1489 status = (*callback) (&rf->rf_udata);
1492 plugin_set_ctx (old_ctx);
1496 NOTICE ("read-function of plugin `%s' failed.",
1501 destroy_callback ((void *) rf);
1504 return (return_status);
1505 } /* int plugin_read_all_once */
1507 int plugin_write (const char *plugin, /* {{{ */
1508 const data_set_t *ds, const value_list_t *vl)
1516 if (list_write == NULL)
1521 ds = plugin_get_ds (vl->type);
1524 ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1534 le = llist_head (list_write);
1537 callback_func_t *cf = le->value;
1538 plugin_write_cb callback;
1540 /* do not switch plugin context; rather keep the context (interval)
1541 * information of the calling read plugin */
1543 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1544 callback = cf->cf_callback;
1545 status = (*callback) (ds, vl, &cf->cf_udata);
1554 if ((success == 0) && (failure != 0))
1559 else /* plugin != NULL */
1561 callback_func_t *cf;
1562 plugin_write_cb callback;
1564 le = llist_head (list_write);
1567 if (strcasecmp (plugin, le->key) == 0)
1578 /* do not switch plugin context; rather keep the context (interval)
1579 * information of the calling read plugin */
1581 DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1582 callback = cf->cf_callback;
1583 status = (*callback) (ds, vl, &cf->cf_udata);
1587 } /* }}} int plugin_write */
1589 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1593 if (list_flush == NULL)
1596 le = llist_head (list_flush);
1599 callback_func_t *cf;
1600 plugin_flush_cb callback;
1601 plugin_ctx_t old_ctx;
1603 if ((plugin != NULL)
1604 && (strcmp (plugin, le->key) != 0))
1611 old_ctx = plugin_set_ctx (cf->cf_ctx);
1612 callback = cf->cf_callback;
1614 (*callback) (timeout, identifier, &cf->cf_udata);
1616 plugin_set_ctx (old_ctx);
1621 } /* int plugin_flush */
1623 void plugin_shutdown_all (void)
1627 stop_read_threads ();
1629 destroy_all_callbacks (&list_init);
1631 pthread_mutex_lock (&read_lock);
1632 llist_destroy (read_list);
1634 pthread_mutex_unlock (&read_lock);
1636 destroy_read_heap ();
1638 plugin_flush (/* plugin = */ NULL,
1640 /* identifier = */ NULL);
1643 if (list_shutdown != NULL)
1644 le = llist_head (list_shutdown);
1648 callback_func_t *cf;
1649 plugin_shutdown_cb callback;
1650 plugin_ctx_t old_ctx;
1653 old_ctx = plugin_set_ctx (cf->cf_ctx);
1654 callback = cf->cf_callback;
1656 /* Advance the pointer before calling the callback allows
1657 * shutdown functions to unregister themselves. If done the
1658 * other way around the memory `le' points to will be freed
1659 * after callback returns. */
1664 plugin_set_ctx (old_ctx);
1667 stop_write_threads ();
1669 /* Write plugins which use the `user_data' pointer usually need the
1670 * same data available to the flush callback. If this is the case, set
1671 * the free_function to NULL when registering the flush callback and to
1672 * the real free function when registering the write callback. This way
1673 * the data isn't freed twice. */
1674 destroy_all_callbacks (&list_flush);
1675 destroy_all_callbacks (&list_missing);
1676 destroy_all_callbacks (&list_write);
1678 destroy_all_callbacks (&list_notification);
1679 destroy_all_callbacks (&list_shutdown);
1680 destroy_all_callbacks (&list_log);
1682 plugin_free_data_sets ();
1683 } /* void plugin_shutdown_all */
1685 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1689 if (list_missing == NULL)
1692 le = llist_head (list_missing);
1695 callback_func_t *cf;
1696 plugin_missing_cb callback;
1697 plugin_ctx_t old_ctx;
1701 old_ctx = plugin_set_ctx (cf->cf_ctx);
1702 callback = cf->cf_callback;
1704 status = (*callback) (vl, &cf->cf_udata);
1705 plugin_set_ctx (old_ctx);
1710 ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1711 "failed with status %i.",
1724 } /* int }}} plugin_dispatch_missing */
1726 static int plugin_dispatch_values_internal (value_list_t *vl)
1729 static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1731 value_t *saved_values;
1732 int saved_values_len;
1736 int free_meta_data = 0;
1738 if ((vl == NULL) || (vl->type[0] == 0)
1739 || (vl->values == NULL) || (vl->values_len < 1))
1741 ERROR ("plugin_dispatch_values: Invalid value list "
1742 "from plugin %s.", vl->plugin);
1746 /* Free meta data only if the calling function didn't specify any. In
1747 * this case matches and targets may add some and the calling function
1748 * may not expect (and therefore free) that data. */
1749 if (vl->meta == NULL)
1752 if (list_write == NULL)
1753 c_complain_once (LOG_WARNING, &no_write_complaint,
1754 "plugin_dispatch_values: No write callback has been "
1755 "registered. Please load at least one output plugin, "
1756 "if you want the collected data to be stored.");
1758 if (data_sets == NULL)
1760 ERROR ("plugin_dispatch_values: No data sets registered. "
1761 "Could the types database be read? Check "
1762 "your `TypesDB' setting!");
1766 if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1768 char ident[6 * DATA_MAX_NAME_LEN];
1770 FORMAT_VL (ident, sizeof (ident), vl);
1771 INFO ("plugin_dispatch_values: Dataset not found: %s "
1772 "(from \"%s\"), check your types.db!",
1777 /* Assured by plugin_value_list_clone(). The time is determined at
1778 * _enqueue_ time. */
1779 assert (vl->time != 0);
1780 assert (vl->interval != 0);
1782 DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1784 "plugin = %s; plugin_instance = %s; "
1785 "type = %s; type_instance = %s;",
1786 CDTIME_T_TO_DOUBLE (vl->time),
1787 CDTIME_T_TO_DOUBLE (vl->interval),
1789 vl->plugin, vl->plugin_instance,
1790 vl->type, vl->type_instance);
1793 assert (0 == strcmp (ds->type, vl->type));
1795 if (0 != strcmp (ds->type, vl->type))
1796 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1797 ds->type, vl->type);
1801 assert (ds->ds_num == vl->values_len);
1803 if (ds->ds_num != vl->values_len)
1805 ERROR ("plugin_dispatch_values: ds->type = %s: "
1806 "(ds->ds_num = %i) != "
1807 "(vl->values_len = %i)",
1808 ds->type, ds->ds_num, vl->values_len);
1813 escape_slashes (vl->host, sizeof (vl->host));
1814 escape_slashes (vl->plugin, sizeof (vl->plugin));
1815 escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1816 escape_slashes (vl->type, sizeof (vl->type));
1817 escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1819 /* Copy the values. This way, we can assure `targets' that they get
1820 * dynamically allocated values, which they can free and replace if
1822 if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1824 saved_values = vl->values;
1825 saved_values_len = vl->values_len;
1827 vl->values = (value_t *) calloc (vl->values_len,
1828 sizeof (*vl->values));
1829 if (vl->values == NULL)
1831 ERROR ("plugin_dispatch_values: calloc failed.");
1832 vl->values = saved_values;
1835 memcpy (vl->values, saved_values,
1836 vl->values_len * sizeof (*vl->values));
1838 else /* if ((pre == NULL) && (post == NULL)) */
1840 saved_values = NULL;
1841 saved_values_len = 0;
1844 if (pre_cache_chain != NULL)
1846 status = fc_process_chain (ds, vl, pre_cache_chain);
1849 WARNING ("plugin_dispatch_values: Running the "
1850 "pre-cache chain failed with "
1854 else if (status == FC_TARGET_STOP)
1856 /* Restore the state of the value_list so that plugins
1857 * don't get confused.. */
1858 if (saved_values != NULL)
1861 vl->values = saved_values;
1862 vl->values_len = saved_values_len;
1868 /* Update the value cache */
1871 if (post_cache_chain != NULL)
1873 status = fc_process_chain (ds, vl, post_cache_chain);
1876 WARNING ("plugin_dispatch_values: Running the "
1877 "post-cache chain failed with "
1883 fc_default_action (ds, vl);
1885 /* Restore the state of the value_list so that plugins don't get
1887 if (saved_values != NULL)
1890 vl->values = saved_values;
1891 vl->values_len = saved_values_len;
1894 if ((free_meta_data != 0) && (vl->meta != NULL))
1896 meta_data_destroy (vl->meta);
1901 } /* int plugin_dispatch_values_internal */
1903 int plugin_dispatch_values (value_list_t const *vl)
1907 status = plugin_write_enqueue (vl);
1911 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1912 "with status %i (%s).", status,
1913 sstrerror (status, errbuf, sizeof (errbuf)));
1920 int plugin_dispatch_notification (const notification_t *notif)
1923 /* Possible TODO: Add flap detection here */
1925 DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1926 "time = %.3f; host = %s;",
1927 notif->severity, notif->message,
1928 CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1930 /* Nobody cares for notifications */
1931 if (list_notification == NULL)
1934 le = llist_head (list_notification);
1937 callback_func_t *cf;
1938 plugin_notification_cb callback;
1941 /* do not switch plugin context; rather keep the context
1942 * (interval) information of the calling plugin */
1945 callback = cf->cf_callback;
1946 status = (*callback) (notif, &cf->cf_udata);
1949 WARNING ("plugin_dispatch_notification: Notification "
1950 "callback %s returned %i.",
1958 } /* int plugin_dispatch_notification */
1960 void plugin_log (int level, const char *format, ...)
1967 if (level >= LOG_DEBUG)
1971 va_start (ap, format);
1972 vsnprintf (msg, sizeof (msg), format, ap);
1973 msg[sizeof (msg) - 1] = '\0';
1976 if (list_log == NULL)
1978 fprintf (stderr, "%s\n", msg);
1982 le = llist_head (list_log);
1985 callback_func_t *cf;
1986 plugin_log_cb callback;
1989 callback = cf->cf_callback;
1991 /* do not switch plugin context; rather keep the context
1992 * (interval) information of the calling plugin */
1994 (*callback) (level, msg, &cf->cf_udata);
1998 } /* void plugin_log */
2000 int parse_log_severity (const char *severity)
2004 if ((0 == strcasecmp (severity, "emerg"))
2005 || (0 == strcasecmp (severity, "alert"))
2006 || (0 == strcasecmp (severity, "crit"))
2007 || (0 == strcasecmp (severity, "err")))
2008 log_level = LOG_ERR;
2009 else if (0 == strcasecmp (severity, "warning"))
2010 log_level = LOG_WARNING;
2011 else if (0 == strcasecmp (severity, "notice"))
2012 log_level = LOG_NOTICE;
2013 else if (0 == strcasecmp (severity, "info"))
2014 log_level = LOG_INFO;
2016 else if (0 == strcasecmp (severity, "debug"))
2017 log_level = LOG_DEBUG;
2018 #endif /* COLLECT_DEBUG */
2021 } /* int parse_log_severity */
2023 int parse_notif_severity (const char *severity)
2025 int notif_severity = -1;
2027 if (strcasecmp (severity, "FAILURE") == 0)
2028 notif_severity = NOTIF_FAILURE;
2029 else if (strcmp (severity, "OKAY") == 0)
2030 notif_severity = NOTIF_OKAY;
2031 else if ((strcmp (severity, "WARNING") == 0)
2032 || (strcmp (severity, "WARN") == 0))
2033 notif_severity = NOTIF_WARNING;
2035 return (notif_severity);
2036 } /* int parse_notif_severity */
2038 const data_set_t *plugin_get_ds (const char *name)
2042 if (data_sets == NULL)
2044 ERROR ("plugin_get_ds: No data sets are defined yet.");
2048 if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2050 DEBUG ("No such dataset registered: %s", name);
2055 } /* data_set_t *plugin_get_ds */
2057 static int plugin_notification_meta_add (notification_t *n,
2059 enum notification_meta_type_e type,
2062 notification_meta_t *meta;
2063 notification_meta_t *tail;
2065 if ((n == NULL) || (name == NULL) || (value == NULL))
2067 ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2071 meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2074 ERROR ("plugin_notification_meta_add: malloc failed.");
2077 memset (meta, 0, sizeof (notification_meta_t));
2079 sstrncpy (meta->name, name, sizeof (meta->name));
2084 case NM_TYPE_STRING:
2086 meta->nm_value.nm_string = strdup ((const char *) value);
2087 if (meta->nm_value.nm_string == NULL)
2089 ERROR ("plugin_notification_meta_add: strdup failed.");
2095 case NM_TYPE_SIGNED_INT:
2097 meta->nm_value.nm_signed_int = *((int64_t *) value);
2100 case NM_TYPE_UNSIGNED_INT:
2102 meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2105 case NM_TYPE_DOUBLE:
2107 meta->nm_value.nm_double = *((double *) value);
2110 case NM_TYPE_BOOLEAN:
2112 meta->nm_value.nm_boolean = *((_Bool *) value);
2117 ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2121 } /* switch (type) */
2125 while ((tail != NULL) && (tail->next != NULL))
2134 } /* int plugin_notification_meta_add */
2136 int plugin_notification_meta_add_string (notification_t *n,
2140 return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2143 int plugin_notification_meta_add_signed_int (notification_t *n,
2147 return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2150 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2154 return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2157 int plugin_notification_meta_add_double (notification_t *n,
2161 return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2164 int plugin_notification_meta_add_boolean (notification_t *n,
2168 return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2171 int plugin_notification_meta_copy (notification_t *dst,
2172 const notification_t *src)
2174 notification_meta_t *meta;
2176 assert (dst != NULL);
2177 assert (src != NULL);
2178 assert (dst != src);
2179 assert ((src->meta == NULL) || (src->meta != dst->meta));
2181 for (meta = src->meta; meta != NULL; meta = meta->next)
2183 if (meta->type == NM_TYPE_STRING)
2184 plugin_notification_meta_add_string (dst, meta->name,
2185 meta->nm_value.nm_string);
2186 else if (meta->type == NM_TYPE_SIGNED_INT)
2187 plugin_notification_meta_add_signed_int (dst, meta->name,
2188 meta->nm_value.nm_signed_int);
2189 else if (meta->type == NM_TYPE_UNSIGNED_INT)
2190 plugin_notification_meta_add_unsigned_int (dst, meta->name,
2191 meta->nm_value.nm_unsigned_int);
2192 else if (meta->type == NM_TYPE_DOUBLE)
2193 plugin_notification_meta_add_double (dst, meta->name,
2194 meta->nm_value.nm_double);
2195 else if (meta->type == NM_TYPE_BOOLEAN)
2196 plugin_notification_meta_add_boolean (dst, meta->name,
2197 meta->nm_value.nm_boolean);
2201 } /* int plugin_notification_meta_copy */
2203 int plugin_notification_meta_free (notification_meta_t *n)
2205 notification_meta_t *this;
2206 notification_meta_t *next;
2210 ERROR ("plugin_notification_meta_free: n == NULL!");
2215 while (this != NULL)
2219 if (this->type == NM_TYPE_STRING)
2221 free ((char *)this->nm_value.nm_string);
2222 this->nm_value.nm_string = NULL;
2230 } /* int plugin_notification_meta_free */
2232 static void plugin_ctx_destructor (void *ctx)
2235 } /* void plugin_ctx_destructor */
2237 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2239 static plugin_ctx_t *plugin_ctx_create (void)
2243 ctx = malloc (sizeof (*ctx));
2246 ERROR ("Failed to allocate plugin context: %s",
2247 sstrerror (errno, errbuf, sizeof (errbuf)));
2252 assert (plugin_ctx_key_initialized);
2253 pthread_setspecific (plugin_ctx_key, ctx);
2254 DEBUG("Created new plugin context.");
2256 } /* int plugin_ctx_create */
2258 void plugin_init_ctx (void)
2260 pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2261 plugin_ctx_key_initialized = 1;
2262 } /* void plugin_init_ctx */
2264 plugin_ctx_t plugin_get_ctx (void)
2268 assert (plugin_ctx_key_initialized);
2269 ctx = pthread_getspecific (plugin_ctx_key);
2272 ctx = plugin_ctx_create ();
2273 /* this must no happen -- exit() instead? */
2279 } /* plugin_ctx_t plugin_get_ctx */
2281 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2286 assert (plugin_ctx_key_initialized);
2287 c = pthread_getspecific (plugin_ctx_key);
2290 c = plugin_ctx_create ();
2291 /* this must no happen -- exit() instead? */
2300 } /* void plugin_set_ctx */
2302 cdtime_t plugin_get_interval (void)
2306 interval = plugin_get_ctx().interval;
2310 return cf_get_default_interval ();
2311 } /* cdtime_t plugin_get_interval */
2315 void *(*start_routine) (void *);
2319 static void *plugin_thread_start (void *arg)
2321 plugin_thread_t *plugin_thread = arg;
2323 void *(*start_routine) (void *) = plugin_thread->start_routine;
2324 void *plugin_arg = plugin_thread->arg;
2326 plugin_set_ctx (plugin_thread->ctx);
2328 free (plugin_thread);
2330 return start_routine (plugin_arg);
2331 } /* void *plugin_thread_start */
2333 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2334 void *(*start_routine) (void *), void *arg)
2336 plugin_thread_t *plugin_thread;
2338 plugin_thread = malloc (sizeof (*plugin_thread));
2339 if (plugin_thread == NULL)
2342 plugin_thread->ctx = plugin_get_ctx ();
2343 plugin_thread->start_routine = start_routine;
2344 plugin_thread->arg = arg;
2346 return pthread_create (thread, attr,
2347 plugin_thread_start, plugin_thread);
2348 } /* int plugin_thread_create */
2350 /* vim: set sw=8 ts=8 noet fdm=marker : */