X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fdaemon%2Fplugin.c;h=daddf684babdf85bf23c81df7e0e7f5b3f3b0639;hp=0f067374d05e7148ce79ae32a44d531e5db42ac2;hb=0bbf058d6f9935e6e727cda7db79312281e2f58d;hpb=35a6c9c5fcd87cb78451a974c4d5b5707926845c diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index 0f067374..daddf684 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -30,23 +30,31 @@ #include "collectd.h" -#include "common.h" #include "configfile.h" #include "filter_chain.h" #include "plugin.h" -#include "utils_avltree.h" +#include "utils/avltree/avltree.h" +#include "utils/common/common.h" +#include "utils/heap/heap.h" #include "utils_cache.h" #include "utils_complain.h" -#include "utils_heap.h" #include "utils_llist.h" #include "utils_random.h" #include "utils_time.h" +#ifdef WIN32 +#define EXPORT __declspec(dllexport) +#include +#include +#else +#define EXPORT +#endif + #if HAVE_PTHREAD_NP_H #include /* for pthread_set_name_np(3) */ #endif -#include +#include /* * Private structures @@ -94,7 +102,7 @@ typedef struct flush_callback_s flush_callback_t; /* * Private variables */ -static c_avl_tree_t *plugins_loaded = NULL; +static c_avl_tree_t *plugins_loaded; static llist_t *list_init; static llist_t *list_write; @@ -104,42 +112,43 @@ static llist_t *list_shutdown; static llist_t *list_log; static llist_t *list_notification; -static fc_chain_t *pre_cache_chain = NULL; -static fc_chain_t *post_cache_chain = NULL; +static fc_chain_t *pre_cache_chain; +static fc_chain_t *post_cache_chain; static c_avl_tree_t *data_sets; -static char *plugindir = NULL; +static char *plugindir; #ifndef DEFAULT_MAX_READ_INTERVAL #define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T_STATIC(86400) #endif -static c_heap_t *read_heap = NULL; +static c_heap_t *read_heap; static llist_t *read_list; static int read_loop = 1; static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER; -static pthread_t *read_threads = NULL; -static size_t read_threads_num = 0; +static pthread_t *read_threads; +static size_t read_threads_num; static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL; static write_queue_t *write_queue_head; static write_queue_t *write_queue_tail; -static long write_queue_length = 0; -static _Bool write_loop = 1; +static long write_queue_length; +static bool write_loop = true; static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER; -static pthread_t *write_threads = NULL; -static size_t write_threads_num = 0; +static pthread_t *write_threads; +static size_t write_threads_num; static pthread_key_t plugin_ctx_key; -static _Bool plugin_ctx_key_initialized = 0; +static bool plugin_ctx_key_initialized; -static long write_limit_high = 0; -static long write_limit_low = 0; +static long write_limit_high; +static long write_limit_low; -static derive_t stats_values_dropped = 0; -static _Bool record_statistics = 0; +static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER; +static derive_t stats_values_dropped; +static bool record_statistics; /* * Static functions @@ -148,9 +157,9 @@ static int plugin_dispatch_values_internal(value_list_t *vl); static const char *plugin_get_dir(void) { if (plugindir == NULL) - return (PLUGINDIR); + return PLUGINDIR; else - return (plugindir); + return plugindir; } static int plugin_update_internal_statistics(void) { /* {{{ */ @@ -191,16 +200,21 @@ static int plugin_update_internal_statistics(void) { /* {{{ */ return 0; } /* }}} int plugin_update_internal_statistics */ -static void destroy_callback(callback_func_t *cf) /* {{{ */ +static void free_userdata(user_data_t const *ud) /* {{{ */ { - if (cf == NULL) + if (ud == NULL) return; - if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL)) { - cf->cf_udata.free_func(cf->cf_udata.data); - cf->cf_udata.data = NULL; - cf->cf_udata.free_func = NULL; + if ((ud->data != NULL) && (ud->free_func != NULL)) { + ud->free_func(ud->data); } +} /* }}} void free_userdata */ + +static void destroy_callback(callback_func_t *cf) /* {{{ */ +{ + if (cf == NULL) + return; + free_userdata(&cf->cf_udata); sfree(cf); } /* }}} void destroy_callback */ @@ -258,7 +272,7 @@ static int register_callback(llist_t **list, /* {{{ */ ERROR("plugin: register_callback: " "llist_create failed."); destroy_callback(cf); - return (-1); + return -1; } } @@ -266,7 +280,7 @@ static int register_callback(llist_t **list, /* {{{ */ if (key == NULL) { ERROR("plugin: register_callback: strdup failed."); destroy_callback(cf); - return (-1); + return -1; } le = llist_search(*list, name); @@ -277,7 +291,7 @@ static int register_callback(llist_t **list, /* {{{ */ "llentry_create failed."); sfree(key); destroy_callback(cf); - return (-1); + return -1; } llist_append(*list, le); @@ -287,16 +301,16 @@ static int register_callback(llist_t **list, /* {{{ */ old_cf = le->value; le->value = cf; - WARNING("plugin: register_callback: " - "a callback named `%s' already exists - " - "overwriting the old entry!", - name); + P_WARNING("register_callback: " + "a callback named `%s' already exists - " + "overwriting the old entry!", + name); destroy_callback(old_cf); sfree(key); } - return (0); + return 0; } /* }}} int register_callback */ static void log_list_callbacks(llist_t **list, /* {{{ */ @@ -306,19 +320,16 @@ static void log_list_callbacks(llist_t **list, /* {{{ */ int i; llentry_t *le; int n; - char **keys; n = llist_size(*list); if (n == 0) { - INFO("%s [none]", comment); + INFO("%s: [none]", comment); return; } - keys = calloc(n, sizeof(char *)); - + char **keys = calloc(n, sizeof(*keys)); if (keys == NULL) { ERROR("%s: failed to allocate memory for list of callbacks", comment); - return; } @@ -341,25 +352,30 @@ static void log_list_callbacks(llist_t **list, /* {{{ */ static int create_register_callback(llist_t **list, /* {{{ */ const char *name, void *callback, user_data_t const *ud) { - callback_func_t *cf; - cf = calloc(1, sizeof(*cf)); + if (name == NULL || callback == NULL) + return EINVAL; + + callback_func_t *cf = calloc(1, sizeof(*cf)); if (cf == NULL) { + free_userdata(ud); ERROR("plugin: create_register_callback: calloc failed."); - return (-1); + return ENOMEM; } cf->cf_callback = callback; if (ud == NULL) { - cf->cf_udata.data = NULL; - cf->cf_udata.free_func = NULL; + cf->cf_udata = (user_data_t){ + .data = NULL, + .free_func = NULL, + }; } else { cf->cf_udata = *ud; } cf->cf_ctx = plugin_get_ctx(); - return (register_callback(list, name, cf)); + return register_callback(list, name, cf); } /* }}} int create_register_callback */ static int plugin_unregister(llist_t *list, const char *name) /* {{{ */ @@ -367,11 +383,11 @@ static int plugin_unregister(llist_t *list, const char *name) /* {{{ */ llentry_t *e; if (list == NULL) - return (-1); + return -1; e = llist_search(list, name); if (e == NULL) - return (-1); + return -1; llist_remove(list, e); @@ -380,68 +396,48 @@ static int plugin_unregister(llist_t *list, const char *name) /* {{{ */ llentry_destroy(e); - return (0); + return 0; } /* }}} int plugin_unregister */ -/* - * (Try to) load the shared object `file'. Won't complain if it isn't a shared - * object, but it will bitch about a shared object not having a - * ``module_register'' symbol.. - */ -static int plugin_load_file(char *file, uint32_t flags) { - lt_dlhandle dlh; - void (*reg_handle)(void); - - lt_dlinit(); - lt_dlerror(); /* clear errors */ - -#if LIBTOOL_VERSION == 2 - if (flags & PLUGIN_FLAGS_GLOBAL) { - lt_dladvise advise; - lt_dladvise_init(&advise); - lt_dladvise_global(&advise); - dlh = lt_dlopenadvise(file, advise); - lt_dladvise_destroy(&advise); - } else { - dlh = lt_dlopen(file); - } -#else /* if LIBTOOL_VERSION == 1 */ - if (flags & PLUGIN_FLAGS_GLOBAL) - WARNING("plugin_load_file: The global flag is not supported, " - "libtool 2 is required for this."); - dlh = lt_dlopen(file); -#endif +/* plugin_load_file loads the shared object "file" and calls its + * "module_register" function. Returns zero on success, non-zero otherwise. */ +static int plugin_load_file(char const *file, bool global) { + int flags = RTLD_NOW; + if (global) + flags |= RTLD_GLOBAL; + void *dlh = dlopen(file, flags); if (dlh == NULL) { char errbuf[1024] = ""; - ssnprintf(errbuf, sizeof(errbuf), - "lt_dlopen (\"%s\") failed: %s. " - "The most common cause for this problem is " - "missing dependencies. Use ldd(1) to check " - "the dependencies of the plugin " - "/ shared object.", - file, lt_dlerror()); - - ERROR("%s", errbuf); - /* Make sure this is printed to STDERR in any case, but also - * make sure it's printed only once. */ - if (list_log != NULL) - fprintf(stderr, "ERROR: %s\n", errbuf); + snprintf(errbuf, sizeof(errbuf), + "dlopen(\"%s\") failed: %s. " + "The most common cause for this problem is missing dependencies. " + "Use ldd(1) to check the dependencies of the plugin / shared " + "object.", + file, dlerror()); + + /* This error is printed to STDERR unconditionally. If list_log is NULL, + * plugin_log() will also print to STDERR. We avoid duplicate output by + * checking that the list of log handlers, list_log, is not NULL. */ + fprintf(stderr, "ERROR: %s\n", errbuf); + if (list_log != NULL) { + ERROR("%s", errbuf); + } - return (1); + return ENOENT; } - if ((reg_handle = (void (*)(void))lt_dlsym(dlh, "module_register")) == NULL) { - WARNING("Couldn't find symbol \"module_register\" in \"%s\": %s\n", file, - lt_dlerror()); - lt_dlclose(dlh); - return (-1); + void (*reg_handle)(void) = dlsym(dlh, "module_register"); + if (reg_handle == NULL) { + ERROR("Couldn't find symbol \"module_register\" in \"%s\": %s\n", file, + dlerror()); + dlclose(dlh); + return ENOENT; } (*reg_handle)(); - - return (0); + return 0; } static void *plugin_read_thread(void __attribute__((unused)) * args) { @@ -596,7 +592,7 @@ static void *plugin_read_thread(void __attribute__((unused)) * args) { } /* while (read_loop) */ pthread_exit(NULL); - return ((void *)0); + return (void *)0; } /* void *plugin_read_thread */ #ifdef PTHREAD_MAX_NAMELEN_NP @@ -618,9 +614,7 @@ static void set_thread_name(pthread_t tid, char const *name) { #if defined(HAVE_PTHREAD_SETNAME_NP) int status = pthread_setname_np(tid, n); if (status != 0) { - char errbuf[1024]; - ERROR("set_thread_name(\"%s\"): %s", n, - sstrerror(status, errbuf, sizeof(errbuf))); + ERROR("set_thread_name(\"%s\"): %s", n, STRERROR(status)); } #else /* if defined(HAVE_PTHREAD_SET_NAME_NP) */ pthread_set_name_np(tid, n); @@ -634,7 +628,7 @@ static void start_read_threads(size_t num) /* {{{ */ if (read_threads != NULL) return; - read_threads = (pthread_t *)calloc(num, sizeof(pthread_t)); + read_threads = calloc(num, sizeof(*read_threads)); if (read_threads == NULL) { ERROR("plugin: start_read_threads: calloc failed."); return; @@ -646,15 +640,15 @@ static void start_read_threads(size_t num) /* {{{ */ /* attr = */ NULL, plugin_read_thread, /* arg = */ NULL); if (status != 0) { - char errbuf[1024]; - ERROR("plugin: start_read_threads: pthread_create failed " - "with status %i (%s).", - status, sstrerror(status, errbuf, sizeof(errbuf))); + ERROR("plugin: start_read_threads: pthread_create failed with status %i " + "(%s).", + status, STRERROR(status)); return; } char name[THREAD_NAME_MAX]; - ssnprintf(name, sizeof(name), "reader#%zu", read_threads_num); + ssnprintf(name, sizeof(name), "reader#%" PRIu64, + (uint64_t)read_threads_num); set_thread_name(read_threads[read_threads_num], name); read_threads_num++; @@ -665,7 +659,7 @@ static void stop_read_threads(void) { if (read_threads == NULL) return; - INFO("collectd: Stopping %zu read threads.", read_threads_num); + INFO("collectd: Stopping %" PRIsz " read threads.", read_threads_num); pthread_mutex_lock(&read_lock); read_loop = 0; @@ -699,11 +693,11 @@ plugin_value_list_clone(value_list_t const *vl_orig) /* {{{ */ value_list_t *vl; if (vl_orig == NULL) - return (NULL); + return NULL; vl = malloc(sizeof(*vl)); if (vl == NULL) - return (NULL); + return NULL; memcpy(vl, vl_orig, sizeof(*vl)); if (vl->host[0] == 0) @@ -712,7 +706,7 @@ plugin_value_list_clone(value_list_t const *vl_orig) /* {{{ */ vl->values = calloc(vl_orig->values_len, sizeof(*vl->values)); if (vl->values == NULL) { plugin_value_list_free(vl); - return (NULL); + return NULL; } memcpy(vl->values, vl_orig->values, vl_orig->values_len * sizeof(*vl->values)); @@ -720,34 +714,17 @@ plugin_value_list_clone(value_list_t const *vl_orig) /* {{{ */ vl->meta = meta_data_clone(vl->meta); if ((vl_orig->meta != NULL) && (vl->meta == NULL)) { plugin_value_list_free(vl); - return (NULL); + return NULL; } if (vl->time == 0) vl->time = cdtime(); /* Fill in the interval from the thread context, if it is zero. */ - if (vl->interval == 0) { - plugin_ctx_t ctx = plugin_get_ctx(); - - if (ctx.interval != 0) - vl->interval = ctx.interval; - else { - char name[6 * DATA_MAX_NAME_LEN]; - FORMAT_VL(name, sizeof(name), vl); - ERROR("plugin_value_list_clone: Unable to determine " - "interval from context for " - "value list \"%s\". " - "This indicates a broken plugin. " - "Please report this problem to the " - "collectd mailing list or at " - ".", - name); - vl->interval = cf_get_default_interval(); - } - } + if (vl->interval == 0) + vl->interval = plugin_get_interval(); - return (vl); + return vl; } /* }}} value_list_t *plugin_value_list_clone */ static int plugin_write_enqueue(value_list_t const *vl) /* {{{ */ @@ -756,13 +733,13 @@ static int plugin_write_enqueue(value_list_t const *vl) /* {{{ */ q = malloc(sizeof(*q)); if (q == NULL) - return (ENOMEM); + return ENOMEM; q->next = NULL; q->vl = plugin_value_list_clone(vl); if (q->vl == NULL) { sfree(q); - return (ENOMEM); + return ENOMEM; } /* Store context of caller (read plugin); otherwise, it would not be @@ -785,7 +762,7 @@ static int plugin_write_enqueue(value_list_t const *vl) /* {{{ */ pthread_cond_signal(&write_cond); pthread_mutex_unlock(&write_lock); - return (0); + return 0; } /* }}} int plugin_write_enqueue */ static value_list_t *plugin_write_dequeue(void) /* {{{ */ @@ -800,7 +777,7 @@ static value_list_t *plugin_write_dequeue(void) /* {{{ */ if (write_queue_head == NULL) { pthread_mutex_unlock(&write_lock); - return (NULL); + return NULL; } q = write_queue_head; @@ -817,7 +794,7 @@ static value_list_t *plugin_write_dequeue(void) /* {{{ */ vl = q->vl; sfree(q); - return (vl); + return vl; } /* }}} value_list_t *plugin_write_dequeue */ static void *plugin_write_thread(void __attribute__((unused)) * args) /* {{{ */ @@ -833,7 +810,7 @@ static void *plugin_write_thread(void __attribute__((unused)) * args) /* {{{ */ } pthread_exit(NULL); - return ((void *)0); + return (void *)0; } /* }}} void *plugin_write_thread */ static void start_write_threads(size_t num) /* {{{ */ @@ -841,7 +818,7 @@ static void start_write_threads(size_t num) /* {{{ */ if (write_threads != NULL) return; - write_threads = (pthread_t *)calloc(num, sizeof(pthread_t)); + write_threads = calloc(num, sizeof(*write_threads)); if (write_threads == NULL) { ERROR("plugin: start_write_threads: calloc failed."); return; @@ -853,15 +830,15 @@ static void start_write_threads(size_t num) /* {{{ */ /* attr = */ NULL, plugin_write_thread, /* arg = */ NULL); if (status != 0) { - char errbuf[1024]; - ERROR("plugin: start_write_threads: pthread_create failed " - "with status %i (%s).", - status, sstrerror(status, errbuf, sizeof(errbuf))); + ERROR("plugin: start_write_threads: pthread_create failed with status %i " + "(%s).", + status, STRERROR(status)); return; } char name[THREAD_NAME_MAX]; - ssnprintf(name, sizeof(name), "writer#%zu", write_threads_num); + ssnprintf(name, sizeof(name), "writer#%" PRIu64, + (uint64_t)write_threads_num); set_thread_name(write_threads[write_threads_num], name); write_threads_num++; @@ -876,10 +853,10 @@ static void stop_write_threads(void) /* {{{ */ if (write_threads == NULL) return; - INFO("collectd: Stopping %zu write threads.", write_threads_num); + INFO("collectd: Stopping %" PRIsz " write threads.", write_threads_num); pthread_mutex_lock(&write_lock); - write_loop = 0; + write_loop = false; DEBUG("plugin: stop_write_threads: Signalling `write_cond'"); pthread_cond_broadcast(&write_cond); pthread_mutex_unlock(&write_lock); @@ -908,7 +885,7 @@ static void stop_write_threads(void) /* {{{ */ pthread_mutex_unlock(&write_lock); if (i > 0) { - WARNING("plugin: %zu value list%s left after shutting down " + WARNING("plugin: %" PRIsz " value list%s left after shutting down " "the write threads.", i, (i == 1) ? " was" : "s were"); } @@ -930,16 +907,14 @@ void plugin_set_dir(const char *dir) { ERROR("plugin_set_dir: strdup(\"%s\") failed", dir); } -static _Bool plugin_is_loaded(char const *name) { - int status; - +bool plugin_is_loaded(char const *name) { if (plugins_loaded == NULL) plugins_loaded = c_avl_create((int (*)(const void *, const void *))strcasecmp); assert(plugins_loaded != NULL); - status = c_avl_get(plugins_loaded, name, /* ret_value = */ NULL); - return (status == 0); + int status = c_avl_get(plugins_loaded, name, /* ret_value = */ NULL); + return status == 0; } static int plugin_mark_loaded(char const *name) { @@ -948,11 +923,11 @@ static int plugin_mark_loaded(char const *name) { name_copy = strdup(name); if (name_copy == NULL) - return (ENOMEM); + return ENOMEM; status = c_avl_insert(plugins_loaded, /* key = */ name_copy, /* value = */ NULL); - return (status); + return status; } static void plugin_free_loaded(void) { @@ -972,7 +947,12 @@ static void plugin_free_loaded(void) { } #define BUFSIZE 512 -int plugin_load(char const *plugin_name, uint32_t flags) { +#ifdef WIN32 +#define SHLIB_SUFFIX ".dll" +#else +#define SHLIB_SUFFIX ".so" +#endif +int plugin_load(char const *plugin_name, bool global) { DIR *dh; const char *dir; char filename[BUFSIZE] = ""; @@ -983,12 +963,12 @@ int plugin_load(char const *plugin_name, uint32_t flags) { int status; if (plugin_name == NULL) - return (EINVAL); + return EINVAL; /* Check if plugin is already loaded and don't do anything in this * case. */ if (plugin_is_loaded(plugin_name)) - return (0); + return 0; dir = plugin_get_dir(); ret = 1; @@ -1006,37 +986,34 @@ int plugin_load(char const *plugin_name, uint32_t flags) { */ if ((strcasecmp("perl", plugin_name) == 0) || (strcasecmp("python", plugin_name) == 0)) - flags |= PLUGIN_FLAGS_GLOBAL; + global = true; - /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the + /* `cpu' should not match `cpufreq'. To solve this we add SHLIB_SUFFIX to the * type when matching the filename */ - status = ssnprintf(typename, sizeof(typename), "%s.so", plugin_name); + status = snprintf(typename, sizeof(typename), "%s" SHLIB_SUFFIX, plugin_name); if ((status < 0) || ((size_t)status >= sizeof(typename))) { - WARNING("plugin_load: Filename too long: \"%s.so\"", plugin_name); - return (-1); + WARNING("plugin_load: Filename too long: \"%s" SHLIB_SUFFIX "\"", + plugin_name); + return -1; } if ((dh = opendir(dir)) == NULL) { - char errbuf[1024]; - ERROR("plugin_load: opendir (%s) failed: %s", dir, - sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + ERROR("plugin_load: opendir (%s) failed: %s", dir, STRERRNO); + return -1; } while ((de = readdir(dh)) != NULL) { if (strcasecmp(de->d_name, typename)) continue; - status = ssnprintf(filename, sizeof(filename), "%s/%s", dir, de->d_name); + status = snprintf(filename, sizeof(filename), "%s/%s", dir, de->d_name); if ((status < 0) || ((size_t)status >= sizeof(filename))) { WARNING("plugin_load: Filename too long: \"%s/%s\"", dir, de->d_name); continue; } if (lstat(filename, &statbuf) == -1) { - char errbuf[1024]; - WARNING("plugin_load: stat (\"%s\") failed: %s", filename, - sstrerror(errno, errbuf, sizeof(errbuf))); + WARNING("plugin_load: stat (\"%s\") failed: %s", filename, STRERRNO); continue; } else if (!S_ISREG(statbuf.st_mode)) { /* don't follow symlinks */ @@ -1044,7 +1021,7 @@ int plugin_load(char const *plugin_name, uint32_t flags) { continue; } - status = plugin_load_file(filename, flags); + status = plugin_load_file(filename, global); if (status == 0) { /* success */ plugin_mark_loaded(plugin_name); @@ -1063,27 +1040,27 @@ int plugin_load(char const *plugin_name, uint32_t flags) { if (filename[0] == 0) ERROR("plugin_load: Could not find plugin \"%s\" in %s", plugin_name, dir); - return (ret); + return ret; } /* * The `register_*' functions follow */ -int plugin_register_config(const char *name, - int (*callback)(const char *key, const char *val), - const char **keys, int keys_num) { +EXPORT int plugin_register_config(const char *name, + int (*callback)(const char *key, + const char *val), + const char **keys, int keys_num) { cf_register(name, callback, keys, keys_num); - return (0); + return 0; } /* int plugin_register_config */ -int plugin_register_complex_config(const char *type, - int (*callback)(oconfig_item_t *)) { - return (cf_register_complex(type, callback)); +EXPORT int plugin_register_complex_config(const char *type, + int (*callback)(oconfig_item_t *)) { + return cf_register_complex(type, callback); } /* int plugin_register_complex_config */ -int plugin_register_init(const char *name, int (*callback)(void)) { - return (create_register_callback(&list_init, name, (void *)callback, - /* user_data = */ NULL)); +EXPORT int plugin_register_init(const char *name, int (*callback)(void)) { + return create_register_callback(&list_init, name, (void *)callback, NULL); } /* plugin_register_init */ static int plugin_compare_read_func(const void *arg0, const void *arg1) { @@ -1094,11 +1071,11 @@ static int plugin_compare_read_func(const void *arg0, const void *arg1) { rf1 = arg1; if (rf0->rf_next_read < rf1->rf_next_read) - return (-1); + return -1; else if (rf0->rf_next_read > rf1->rf_next_read) - return (1); + return 1; else - return (0); + return 0; } /* int plugin_compare_read_func */ /* Add a read function to both, the heap and a linked list. The linked list if @@ -1118,7 +1095,7 @@ static int plugin_insert_read(read_func_t *rf) { if (read_list == NULL) { pthread_mutex_unlock(&read_lock); ERROR("plugin_insert_read: read_list failed."); - return (-1); + return -1; } } @@ -1127,25 +1104,24 @@ static int plugin_insert_read(read_func_t *rf) { if (read_heap == NULL) { pthread_mutex_unlock(&read_lock); ERROR("plugin_insert_read: c_heap_create failed."); - return (-1); + return -1; } } le = llist_search(read_list, rf->rf_name); if (le != NULL) { pthread_mutex_unlock(&read_lock); - WARNING("The read function \"%s\" is already registered. " - "Check for duplicate \"LoadPlugin\" lines " - "in your configuration!", - rf->rf_name); - return (EINVAL); + P_WARNING("The read function \"%s\" is already registered. " + "Check for duplicates in your configuration!", + rf->rf_name); + return EINVAL; } le = llentry_create(rf->rf_name, rf); if (le == NULL) { pthread_mutex_unlock(&read_lock); ERROR("plugin_insert_read: llentry_create failed."); - return (-1); + return -1; } status = c_heap_insert(read_heap, rf); @@ -1153,7 +1129,7 @@ static int plugin_insert_read(read_func_t *rf) { pthread_mutex_unlock(&read_lock); ERROR("plugin_insert_read: c_heap_insert failed."); llentry_destroy(le); - return (-1); + return -1; } /* This does not fail. */ @@ -1162,17 +1138,17 @@ static int plugin_insert_read(read_func_t *rf) { /* Wake up all the read threads. */ pthread_cond_broadcast(&read_cond); pthread_mutex_unlock(&read_lock); - return (0); + return 0; } /* int plugin_insert_read */ -int plugin_register_read(const char *name, int (*callback)(void)) { +EXPORT int plugin_register_read(const char *name, int (*callback)(void)) { read_func_t *rf; int status; rf = calloc(1, sizeof(*rf)); if (rf == NULL) { ERROR("plugin_register_read: calloc failed."); - return (ENOMEM); + return ENOMEM; } rf->rf_callback = (void *)callback; @@ -1183,6 +1159,7 @@ int plugin_register_read(const char *name, int (*callback)(void)) { rf->rf_name = strdup(name); rf->rf_type = RF_SIMPLE; rf->rf_interval = plugin_get_interval(); + rf->rf_ctx.interval = rf->rf_interval; status = plugin_insert_read(rf); if (status != 0) { @@ -1190,19 +1167,21 @@ int plugin_register_read(const char *name, int (*callback)(void)) { sfree(rf); } - return (status); + return status; } /* int plugin_register_read */ -int plugin_register_complex_read(const char *group, const char *name, - plugin_read_cb callback, cdtime_t interval, - user_data_t const *user_data) { +EXPORT int plugin_register_complex_read(const char *group, const char *name, + plugin_read_cb callback, + cdtime_t interval, + user_data_t const *user_data) { read_func_t *rf; int status; rf = calloc(1, sizeof(*rf)); if (rf == NULL) { + free_userdata(user_data); ERROR("plugin_register_complex_read: calloc failed."); - return (ENOMEM); + return ENOMEM; } rf->rf_callback = (void *)callback; @@ -1223,25 +1202,27 @@ int plugin_register_complex_read(const char *group, const char *name, } rf->rf_ctx = plugin_get_ctx(); + rf->rf_ctx.interval = rf->rf_interval; status = plugin_insert_read(rf); if (status != 0) { + free_userdata(&rf->rf_udata); sfree(rf->rf_name); sfree(rf); } - return (status); + return status; } /* int plugin_register_complex_read */ -int plugin_register_write(const char *name, plugin_write_cb callback, - user_data_t const *ud) { - return (create_register_callback(&list_write, name, (void *)callback, ud)); +EXPORT int plugin_register_write(const char *name, plugin_write_cb callback, + user_data_t const *ud) { + return create_register_callback(&list_write, name, (void *)callback, ud); } /* int plugin_register_write */ static int plugin_flush_timeout_callback(user_data_t *ud) { flush_callback_t *cb = ud->data; - return plugin_flush(cb->name, cb->timeout, /* identifier = */ NULL); + return plugin_flush(cb->name, cb->timeout, NULL); } /* static int plugin_flush_callback */ static void plugin_flush_timeout_callback_free(void *data) { @@ -1266,7 +1247,7 @@ static char *plugin_flush_callback_name(const char *name) { flush_name = malloc(name_size + prefix_size + 1); if (flush_name == NULL) { ERROR("plugin_flush_callback_name: malloc failed."); - return (NULL); + return NULL; } sstrncpy(flush_name, flush_prefix, prefix_size + 1); @@ -1275,8 +1256,8 @@ static char *plugin_flush_callback_name(const char *name) { return flush_name; } /* static char *plugin_flush_callback_name */ -int plugin_register_flush(const char *name, plugin_flush_cb callback, - user_data_t const *ud) { +EXPORT int plugin_register_flush(const char *name, plugin_flush_cb callback, + user_data_t const *ud) { int status; plugin_ctx_t ctx = plugin_get_ctx(); @@ -1290,13 +1271,13 @@ int plugin_register_flush(const char *name, plugin_flush_cb callback, flush_name = plugin_flush_callback_name(name); if (flush_name == NULL) - return (-1); + return -1; cb = malloc(sizeof(*cb)); if (cb == NULL) { ERROR("plugin_register_flush: malloc failed."); sfree(flush_name); - return (-1); + return -1; } cb->name = strdup(name); @@ -1304,7 +1285,7 @@ int plugin_register_flush(const char *name, plugin_flush_cb callback, ERROR("plugin_register_flush: strdup failed."); sfree(cb); sfree(flush_name); - return (-1); + return -1; } cb->timeout = ctx.flush_timeout; @@ -1313,29 +1294,26 @@ int plugin_register_flush(const char *name, plugin_flush_cb callback, /* name = */ flush_name, /* callback = */ plugin_flush_timeout_callback, /* interval = */ ctx.flush_interval, - /* user data = */ &(user_data_t){ - .data = cb, .free_func = plugin_flush_timeout_callback_free, + /* user data = */ + &(user_data_t){ + .data = cb, + .free_func = plugin_flush_timeout_callback_free, }); sfree(flush_name); - if (status != 0) { - sfree(cb->name); - sfree(cb); - return status; - } + return status; } return 0; } /* int plugin_register_flush */ -int plugin_register_missing(const char *name, plugin_missing_cb callback, - user_data_t const *ud) { - return (create_register_callback(&list_missing, name, (void *)callback, ud)); +EXPORT int plugin_register_missing(const char *name, plugin_missing_cb callback, + user_data_t const *ud) { + return create_register_callback(&list_missing, name, (void *)callback, ud); } /* int plugin_register_missing */ -int plugin_register_shutdown(const char *name, int (*callback)(void)) { - return (create_register_callback(&list_shutdown, name, (void *)callback, - /* user_data = */ NULL)); +EXPORT int plugin_register_shutdown(const char *name, int (*callback)(void)) { + return create_register_callback(&list_shutdown, name, (void *)callback, NULL); } /* int plugin_register_shutdown */ static void plugin_free_data_sets(void) { @@ -1357,7 +1335,7 @@ static void plugin_free_data_sets(void) { data_sets = NULL; } /* void plugin_free_data_sets */ -int plugin_register_data_set(const data_set_t *ds) { +EXPORT int plugin_register_data_set(const data_set_t *ds) { data_set_t *ds_copy; if ((data_sets != NULL) && (c_avl_get(data_sets, ds->type, NULL) == 0)) { @@ -1366,72 +1344,72 @@ int plugin_register_data_set(const data_set_t *ds) { } else if (data_sets == NULL) { data_sets = c_avl_create((int (*)(const void *, const void *))strcmp); if (data_sets == NULL) - return (-1); + return -1; } ds_copy = malloc(sizeof(*ds_copy)); if (ds_copy == NULL) - return (-1); + return -1; memcpy(ds_copy, ds, sizeof(data_set_t)); ds_copy->ds = malloc(sizeof(*ds_copy->ds) * ds->ds_num); if (ds_copy->ds == NULL) { sfree(ds_copy); - return (-1); + return -1; } for (size_t i = 0; i < ds->ds_num; i++) memcpy(ds_copy->ds + i, ds->ds + i, sizeof(data_source_t)); - return (c_avl_insert(data_sets, (void *)ds_copy->type, (void *)ds_copy)); + return c_avl_insert(data_sets, (void *)ds_copy->type, (void *)ds_copy); } /* int plugin_register_data_set */ -int plugin_register_log(const char *name, plugin_log_cb callback, - user_data_t const *ud) { - return (create_register_callback(&list_log, name, (void *)callback, ud)); +EXPORT int plugin_register_log(const char *name, plugin_log_cb callback, + user_data_t const *ud) { + return create_register_callback(&list_log, name, (void *)callback, ud); } /* int plugin_register_log */ -int plugin_register_notification(const char *name, - plugin_notification_cb callback, - user_data_t const *ud) { - return ( - create_register_callback(&list_notification, name, (void *)callback, ud)); +EXPORT int plugin_register_notification(const char *name, + plugin_notification_cb callback, + user_data_t const *ud) { + return create_register_callback(&list_notification, name, (void *)callback, + ud); } /* int plugin_register_log */ -int plugin_unregister_config(const char *name) { +EXPORT int plugin_unregister_config(const char *name) { cf_unregister(name); - return (0); + return 0; } /* int plugin_unregister_config */ -int plugin_unregister_complex_config(const char *name) { +EXPORT int plugin_unregister_complex_config(const char *name) { cf_unregister_complex(name); - return (0); + return 0; } /* int plugin_unregister_complex_config */ -int plugin_unregister_init(const char *name) { - return (plugin_unregister(list_init, name)); +EXPORT int plugin_unregister_init(const char *name) { + return plugin_unregister(list_init, name); } -int plugin_unregister_read(const char *name) /* {{{ */ +EXPORT int plugin_unregister_read(const char *name) /* {{{ */ { llentry_t *le; read_func_t *rf; if (name == NULL) - return (-ENOENT); + return -ENOENT; pthread_mutex_lock(&read_lock); if (read_list == NULL) { pthread_mutex_unlock(&read_lock); - return (-ENOENT); + return -ENOENT; } le = llist_search(read_list, name); if (le == NULL) { pthread_mutex_unlock(&read_lock); WARNING("plugin_unregister_read: No such read function: %s", name); - return (-ENOENT); + return -ENOENT; } llist_remove(read_list, le); @@ -1446,10 +1424,10 @@ int plugin_unregister_read(const char *name) /* {{{ */ DEBUG("plugin_unregister_read: Marked `%s' for removal.", name); - return (0); + return 0; } /* }}} int plugin_unregister_read */ -void plugin_log_available_writers(void) { +EXPORT void plugin_log_available_writers(void) { log_list_callbacks(&list_write, "Available write targets:"); } @@ -1461,7 +1439,7 @@ static int compare_read_func_group(llentry_t *e, void *ud) /* {{{ */ return strcmp(rf->rf_group, (const char *)group); } /* }}} int compare_read_func_group */ -int plugin_unregister_read_group(const char *group) /* {{{ */ +EXPORT int plugin_unregister_read_group(const char *group) /* {{{ */ { llentry_t *le; read_func_t *rf; @@ -1469,13 +1447,13 @@ int plugin_unregister_read_group(const char *group) /* {{{ */ int found = 0; if (group == NULL) - return (-ENOENT); + return -ENOENT; pthread_mutex_lock(&read_lock); if (read_list == NULL) { pthread_mutex_unlock(&read_lock); - return (-ENOENT); + return -ENOENT; } while (42) { @@ -1505,17 +1483,17 @@ int plugin_unregister_read_group(const char *group) /* {{{ */ WARNING("plugin_unregister_read_group: No such " "group of read function: %s", group); - return (-ENOENT); + return -ENOENT; } - return (0); + return 0; } /* }}} int plugin_unregister_read_group */ -int plugin_unregister_write(const char *name) { - return (plugin_unregister(list_write, name)); +EXPORT int plugin_unregister_write(const char *name) { + return plugin_unregister(list_write, name); } -int plugin_unregister_flush(const char *name) { +EXPORT int plugin_unregister_flush(const char *name) { plugin_ctx_t ctx = plugin_get_ctx(); if (ctx.flush_interval != 0) { @@ -1531,38 +1509,38 @@ int plugin_unregister_flush(const char *name) { return plugin_unregister(list_flush, name); } -int plugin_unregister_missing(const char *name) { - return (plugin_unregister(list_missing, name)); +EXPORT int plugin_unregister_missing(const char *name) { + return plugin_unregister(list_missing, name); } -int plugin_unregister_shutdown(const char *name) { - return (plugin_unregister(list_shutdown, name)); +EXPORT int plugin_unregister_shutdown(const char *name) { + return plugin_unregister(list_shutdown, name); } -int plugin_unregister_data_set(const char *name) { +EXPORT int plugin_unregister_data_set(const char *name) { data_set_t *ds; if (data_sets == NULL) - return (-1); + return -1; if (c_avl_remove(data_sets, name, NULL, (void *)&ds) != 0) - return (-1); + return -1; sfree(ds->ds); sfree(ds); - return (0); + return 0; } /* int plugin_unregister_data_set */ -int plugin_unregister_log(const char *name) { - return (plugin_unregister(list_log, name)); +EXPORT int plugin_unregister_log(const char *name) { + return plugin_unregister(list_log, name); } -int plugin_unregister_notification(const char *name) { - return (plugin_unregister(list_notification, name)); +EXPORT int plugin_unregister_notification(const char *name) { + return plugin_unregister(list_notification, name); } -int plugin_init_all(void) { +EXPORT int plugin_init_all(void) { char const *chain_name; llentry_t *le; int status; @@ -1572,7 +1550,7 @@ int plugin_init_all(void) { uc_init(); if (IS_TRUE(global_option_get("CollectInternalStats"))) { - record_statistics = 1; + record_statistics = true; plugin_register_read("collectd", plugin_update_internal_statistics); } @@ -1661,20 +1639,20 @@ int plugin_init_all(void) { } /* void plugin_init_all */ /* TODO: Rename this function. */ -void plugin_read_all(void) { +EXPORT void plugin_read_all(void) { uc_check_timeout(); return; } /* void plugin_read_all */ /* Read function called when the `-T' command line argument is given. */ -int plugin_read_all_once(void) { +EXPORT int plugin_read_all_once(void) { int status; int return_status = 0; if (read_heap == NULL) { NOTICE("No read-functions are registered."); - return (0); + return 0; } while (42) { @@ -1710,25 +1688,25 @@ int plugin_read_all_once(void) { destroy_callback((void *)rf); } - return (return_status); + return return_status; } /* int plugin_read_all_once */ -int plugin_write(const char *plugin, /* {{{ */ - const data_set_t *ds, const value_list_t *vl) { +EXPORT int plugin_write(const char *plugin, /* {{{ */ + const data_set_t *ds, const value_list_t *vl) { llentry_t *le; int status; if (vl == NULL) - return (EINVAL); + return EINVAL; if (list_write == NULL) - return (ENOENT); + return ENOENT; if (ds == NULL) { ds = plugin_get_ds(vl->type); if (ds == NULL) { ERROR("plugin_write: Unable to lookup type `%s'.", vl->type); - return (ENOENT); + return ENOENT; } } @@ -1741,8 +1719,12 @@ int plugin_write(const char *plugin, /* {{{ */ callback_func_t *cf = le->value; plugin_write_cb callback; - /* do not switch plugin context; rather keep the context (interval) - * information of the calling read plugin */ + /* Keep the read plugin's interval and flush information but update the + * plugin name. */ + plugin_ctx_t old_ctx = plugin_get_ctx(); + plugin_ctx_t ctx = old_ctx; + ctx.name = cf->cf_ctx.name; + plugin_set_ctx(ctx); DEBUG("plugin: plugin_write: Writing values via %s.", le->key); callback = cf->cf_callback; @@ -1752,6 +1734,7 @@ int plugin_write(const char *plugin, /* {{{ */ else success++; + plugin_set_ctx(old_ctx); le = le->next; } @@ -1773,7 +1756,7 @@ int plugin_write(const char *plugin, /* {{{ */ } if (le == NULL) - return (ENOENT); + return ENOENT; cf = le->value; @@ -1785,14 +1768,15 @@ int plugin_write(const char *plugin, /* {{{ */ status = (*callback)(ds, vl, &cf->cf_udata); } - return (status); + return status; } /* }}} int plugin_write */ -int plugin_flush(const char *plugin, cdtime_t timeout, const char *identifier) { +EXPORT int plugin_flush(const char *plugin, cdtime_t timeout, + const char *identifier) { llentry_t *le; if (list_flush == NULL) - return (0); + return 0; le = llist_head(list_flush); while (le != NULL) { @@ -1815,10 +1799,10 @@ int plugin_flush(const char *plugin, cdtime_t timeout, const char *identifier) { le = le->next; } - return (0); + return 0; } /* int plugin_flush */ -int plugin_shutdown_all(void) { +EXPORT int plugin_shutdown_all(void) { llentry_t *le; int ret = 0; // Assume success. @@ -1881,52 +1865,43 @@ int plugin_shutdown_all(void) { plugin_free_loaded(); plugin_free_data_sets(); - return (ret); + return ret; } /* void plugin_shutdown_all */ -int plugin_dispatch_missing(const value_list_t *vl) /* {{{ */ +EXPORT int plugin_dispatch_missing(const value_list_t *vl) /* {{{ */ { - llentry_t *le; - if (list_missing == NULL) - return (0); + return 0; - le = llist_head(list_missing); + llentry_t *le = llist_head(list_missing); while (le != NULL) { - callback_func_t *cf; - plugin_missing_cb callback; - plugin_ctx_t old_ctx; - int status; - - cf = le->value; - old_ctx = plugin_set_ctx(cf->cf_ctx); - callback = cf->cf_callback; + callback_func_t *cf = le->value; + plugin_ctx_t old_ctx = plugin_set_ctx(cf->cf_ctx); + plugin_missing_cb callback = cf->cf_callback; - status = (*callback)(vl, &cf->cf_udata); + int status = (*callback)(vl, &cf->cf_udata); plugin_set_ctx(old_ctx); if (status != 0) { if (status < 0) { ERROR("plugin_dispatch_missing: Callback function \"%s\" " "failed with status %i.", le->key, status); - return (status); + return status; } else { - return (0); + return 0; } } le = le->next; } - return (0); + return 0; } /* int }}} plugin_dispatch_missing */ static int plugin_dispatch_values_internal(value_list_t *vl) { int status; static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC; - data_set_t *ds; - - _Bool free_meta_data = 0; + bool free_meta_data = false; assert(vl != NULL); @@ -1939,14 +1914,14 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { ERROR("plugin_dispatch_values: Invalid value list " "from plugin %s.", vl->plugin); - return (-1); + return -1; } /* Free meta data only if the calling function didn't specify any. In * this case matches and targets may add some and the calling function * may not expect (and therefore free) that data. */ if (vl->meta == NULL) - free_meta_data = 1; + free_meta_data = true; if (list_write == NULL) c_complain_once(LOG_WARNING, &no_write_complaint, @@ -1958,9 +1933,10 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { ERROR("plugin_dispatch_values: No data sets registered. " "Could the types database be read? Check " "your `TypesDB' setting!"); - return (-1); + return -1; } + data_set_t *ds = NULL; if (c_avl_get(data_sets, vl->type, (void *)&ds) != 0) { char ident[6 * DATA_MAX_NAME_LEN]; @@ -1968,7 +1944,7 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { INFO("plugin_dispatch_values: Dataset not found: %s " "(from \"%s\"), check your types.db!", vl->type, ident); - return (-1); + return -1; } DEBUG("plugin_dispatch_values: time = %.3f; interval = %.3f; " @@ -1991,10 +1967,10 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { #else if (ds->ds_num != vl->values_len) { ERROR("plugin_dispatch_values: ds->type = %s: " - "(ds->ds_num = %zu) != " - "(vl->values_len = %zu)", + "(ds->ds_num = %" PRIsz ") != " + "(vl->values_len = %" PRIsz ")", ds->type, ds->ds_num, vl->values_len); - return (-1); + return -1; } #endif @@ -2012,7 +1988,7 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { "status %i (%#x).", status, status); } else if (status == FC_TARGET_STOP) - return (0); + return 0; } /* Update the value cache */ @@ -2029,12 +2005,12 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { } else fc_default_action(ds, vl); - if ((free_meta_data != 0) && (vl->meta != NULL)) { + if ((free_meta_data == true) && (vl->meta != NULL)) { meta_data_destroy(vl->meta); vl->meta = NULL; } - return (0); + return 0; } /* int plugin_dispatch_values_internal */ static double get_drop_probability(void) /* {{{ */ @@ -2048,19 +2024,19 @@ static double get_drop_probability(void) /* {{{ */ pthread_mutex_unlock(&write_lock); if (wql < write_limit_low) - return (0.0); + return 0.0; if (wql >= write_limit_high) - return (1.0); + return 1.0; pos = 1 + wql - write_limit_low; size = 1 + write_limit_high - write_limit_low; - return (((double)pos) / ((double)size)); + return (double)pos / (double)size; } /* }}} double get_drop_probability */ -static _Bool check_drop_value(void) /* {{{ */ +static bool check_drop_value(void) /* {{{ */ { - static cdtime_t last_message_time = 0; + static cdtime_t last_message_time; static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER; double p; @@ -2068,11 +2044,11 @@ static _Bool check_drop_value(void) /* {{{ */ int status; if (write_limit_high == 0) - return (0); + return false; p = get_drop_probability(); if (p == 0.0) - return (0); + return false; status = pthread_mutex_trylock(&last_message_lock); if (status == 0) { @@ -2089,18 +2065,17 @@ static _Bool check_drop_value(void) /* {{{ */ } if (p == 1.0) - return (1); + return true; q = cdrand_d(); if (q > p) - return (1); + return true; else - return (0); -} /* }}} _Bool check_drop_value */ + return false; +} /* }}} bool check_drop_value */ -int plugin_dispatch_values(value_list_t const *vl) { +EXPORT int plugin_dispatch_values(value_list_t const *vl) { int status; - static pthread_mutex_t statistics_lock = PTHREAD_MUTEX_INITIALIZER; if (check_drop_value()) { if (record_statistics) { @@ -2108,29 +2083,37 @@ int plugin_dispatch_values(value_list_t const *vl) { stats_values_dropped++; pthread_mutex_unlock(&statistics_lock); } - return (0); + return 0; } status = plugin_write_enqueue(vl); if (status != 0) { - char errbuf[1024]; - ERROR("plugin_dispatch_values: plugin_write_enqueue failed " - "with status %i (%s).", - status, sstrerror(status, errbuf, sizeof(errbuf))); - return (status); + ERROR("plugin_dispatch_values: plugin_write_enqueue failed with status %i " + "(%s).", + status, STRERROR(status)); + return status; } - return (0); + return 0; } __attribute__((sentinel)) int plugin_dispatch_multivalue(value_list_t const *template, /* {{{ */ - _Bool store_percentage, int store_type, ...) { + bool store_percentage, int store_type, ...) { value_list_t *vl; int failed = 0; gauge_t sum = 0.0; va_list ap; + if (check_drop_value()) { + if (record_statistics) { + pthread_mutex_lock(&statistics_lock); + stats_values_dropped++; + pthread_mutex_unlock(&statistics_lock); + } + return 0; + } + assert(template->values_len == 1); /* Calculate sum for Gauge to calculate percent if needed */ @@ -2195,10 +2178,10 @@ plugin_dispatch_multivalue(value_list_t const *template, /* {{{ */ va_end(ap); plugin_value_list_free(vl); - return (failed); + return failed; } /* }}} int plugin_dispatch_multivalue */ -int plugin_dispatch_notification(const notification_t *notif) { +EXPORT int plugin_dispatch_notification(const notification_t *notif) { llentry_t *le; /* Possible TODO: Add flap detection here */ @@ -2209,7 +2192,7 @@ int plugin_dispatch_notification(const notification_t *notif) { /* Nobody cares for notifications */ if (list_notification == NULL) - return (-1); + return -1; le = llist_head(list_notification); while (le != NULL) { @@ -2232,10 +2215,10 @@ int plugin_dispatch_notification(const notification_t *notif) { le = le->next; } - return (0); + return 0; } /* int plugin_dispatch_notification */ -void plugin_log(int level, const char *format, ...) { +EXPORT void plugin_log(int level, const char *format, ...) { char msg[1024]; va_list ap; llentry_t *le; @@ -2272,6 +2255,21 @@ void plugin_log(int level, const char *format, ...) { } } /* void plugin_log */ +void daemon_log(int level, const char *format, ...) { + char msg[1024] = ""; // Size inherits from plugin_log() + + char const *name = plugin_get_ctx().name; + if (name == NULL) + name = "UNKNOWN"; + + va_list ap; + va_start(ap, format); + vsnprintf(msg, sizeof(msg), format, ap); + va_end(ap); + + plugin_log(level, "%s plugin: %s", name, msg); +} /* void daemon_log */ + int parse_log_severity(const char *severity) { int log_level = -1; @@ -2290,10 +2288,10 @@ int parse_log_severity(const char *severity) { log_level = LOG_DEBUG; #endif /* COLLECT_DEBUG */ - return (log_level); + return log_level; } /* int parse_log_severity */ -int parse_notif_severity(const char *severity) { +EXPORT int parse_notif_severity(const char *severity) { int notif_severity = -1; if (strcasecmp(severity, "FAILURE") == 0) @@ -2304,23 +2302,23 @@ int parse_notif_severity(const char *severity) { (strcmp(severity, "WARN") == 0)) notif_severity = NOTIF_WARNING; - return (notif_severity); + return notif_severity; } /* int parse_notif_severity */ -const data_set_t *plugin_get_ds(const char *name) { +EXPORT const data_set_t *plugin_get_ds(const char *name) { data_set_t *ds; if (data_sets == NULL) { - ERROR("plugin_get_ds: No data sets are defined yet."); - return (NULL); + P_ERROR("plugin_get_ds: No data sets are defined yet."); + return NULL; } if (c_avl_get(data_sets, name, (void *)&ds) != 0) { DEBUG("No such dataset registered: %s", name); - return (NULL); + return NULL; } - return (ds); + return ds; } /* data_set_t *plugin_get_ds */ static int plugin_notification_meta_add(notification_t *n, const char *name, @@ -2331,13 +2329,13 @@ static int plugin_notification_meta_add(notification_t *n, const char *name, if ((n == NULL) || (name == NULL) || (value == NULL)) { ERROR("plugin_notification_meta_add: A pointer is NULL!"); - return (-1); + return -1; } meta = calloc(1, sizeof(*meta)); if (meta == NULL) { ERROR("plugin_notification_meta_add: calloc failed."); - return (-1); + return -1; } sstrncpy(meta->name, name, sizeof(meta->name)); @@ -2349,7 +2347,7 @@ static int plugin_notification_meta_add(notification_t *n, const char *name, if (meta->nm_value.nm_string == NULL) { ERROR("plugin_notification_meta_add: strdup failed."); sfree(meta); - return (-1); + return -1; } break; } @@ -2366,13 +2364,13 @@ static int plugin_notification_meta_add(notification_t *n, const char *name, break; } case NM_TYPE_BOOLEAN: { - meta->nm_value.nm_boolean = *((_Bool *)value); + meta->nm_value.nm_boolean = *((bool *)value); break; } default: { ERROR("plugin_notification_meta_add: Unknown type: %i", type); sfree(meta); - return (-1); + return -1; } } /* switch (type) */ @@ -2386,33 +2384,33 @@ static int plugin_notification_meta_add(notification_t *n, const char *name, else tail->next = meta; - return (0); + return 0; } /* int plugin_notification_meta_add */ int plugin_notification_meta_add_string(notification_t *n, const char *name, const char *value) { - return (plugin_notification_meta_add(n, name, NM_TYPE_STRING, value)); + return plugin_notification_meta_add(n, name, NM_TYPE_STRING, value); } int plugin_notification_meta_add_signed_int(notification_t *n, const char *name, int64_t value) { - return (plugin_notification_meta_add(n, name, NM_TYPE_SIGNED_INT, &value)); + return plugin_notification_meta_add(n, name, NM_TYPE_SIGNED_INT, &value); } int plugin_notification_meta_add_unsigned_int(notification_t *n, const char *name, uint64_t value) { - return (plugin_notification_meta_add(n, name, NM_TYPE_UNSIGNED_INT, &value)); + return plugin_notification_meta_add(n, name, NM_TYPE_UNSIGNED_INT, &value); } int plugin_notification_meta_add_double(notification_t *n, const char *name, double value) { - return (plugin_notification_meta_add(n, name, NM_TYPE_DOUBLE, &value)); + return plugin_notification_meta_add(n, name, NM_TYPE_DOUBLE, &value); } int plugin_notification_meta_add_boolean(notification_t *n, const char *name, - _Bool value) { - return (plugin_notification_meta_add(n, name, NM_TYPE_BOOLEAN, &value)); + bool value) { + return plugin_notification_meta_add(n, name, NM_TYPE_BOOLEAN, &value); } int plugin_notification_meta_copy(notification_t *dst, @@ -2440,7 +2438,7 @@ int plugin_notification_meta_copy(notification_t *dst, meta->nm_value.nm_boolean); } - return (0); + return 0; } /* int plugin_notification_meta_copy */ int plugin_notification_meta_free(notification_meta_t *n) { @@ -2449,7 +2447,7 @@ int plugin_notification_meta_free(notification_meta_t *n) { if (n == NULL) { ERROR("plugin_notification_meta_free: n == NULL!"); - return (-1); + return -1; } this = n; @@ -2469,7 +2467,7 @@ int plugin_notification_meta_free(notification_meta_t *n) { this = next; } - return (0); + return 0; } /* int plugin_notification_meta_free */ static void plugin_ctx_destructor(void *ctx) { @@ -2483,9 +2481,7 @@ static plugin_ctx_t *plugin_ctx_create(void) { ctx = malloc(sizeof(*ctx)); if (ctx == NULL) { - char errbuf[1024]; - ERROR("Failed to allocate plugin context: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("Failed to allocate plugin context: %s", STRERRNO); return NULL; } @@ -2493,15 +2489,15 @@ static plugin_ctx_t *plugin_ctx_create(void) { assert(plugin_ctx_key_initialized); pthread_setspecific(plugin_ctx_key, ctx); DEBUG("Created new plugin context."); - return (ctx); + return ctx; } /* int plugin_ctx_create */ -void plugin_init_ctx(void) { +EXPORT void plugin_init_ctx(void) { pthread_key_create(&plugin_ctx_key, plugin_ctx_destructor); - plugin_ctx_key_initialized = 1; + plugin_ctx_key_initialized = true; } /* void plugin_init_ctx */ -plugin_ctx_t plugin_get_ctx(void) { +EXPORT plugin_ctx_t plugin_get_ctx(void) { plugin_ctx_t *ctx; assert(plugin_ctx_key_initialized); @@ -2514,10 +2510,10 @@ plugin_ctx_t plugin_get_ctx(void) { return ctx_init; } - return (*ctx); + return *ctx; } /* plugin_ctx_t plugin_get_ctx */ -plugin_ctx_t plugin_set_ctx(plugin_ctx_t ctx) { +EXPORT plugin_ctx_t plugin_set_ctx(plugin_ctx_t ctx) { plugin_ctx_t *c; plugin_ctx_t old; @@ -2534,16 +2530,18 @@ plugin_ctx_t plugin_set_ctx(plugin_ctx_t ctx) { old = *c; *c = ctx; - return (old); + return old; } /* void plugin_set_ctx */ -cdtime_t plugin_get_interval(void) { +EXPORT cdtime_t plugin_get_interval(void) { cdtime_t interval; interval = plugin_get_ctx().interval; if (interval > 0) return interval; + P_ERROR("plugin_get_interval: Unable to determine Interval from context."); + return cf_get_default_interval(); } /* cdtime_t plugin_get_interval */ @@ -2590,5 +2588,3 @@ int plugin_thread_create(pthread_t *thread, const pthread_attr_t *attr, return 0; } /* int plugin_thread_create */ - -/* vim: set sw=8 ts=8 noet fdm=marker : */