X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fdaemon%2Fplugin.c;h=427a813485adeeefb325c298d3b641fd62e41fa4;hp=ae99d5fdfa13bb9aef4b7d6efb994dcb6078e01b;hb=06a86a60a7dabc685bdbd81ce3d36ea5f7e2c2d4;hpb=d1d1a799a4dd0818877d6d5c1e6c2884c84373ce diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index ae99d5fd..427a8134 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -94,7 +94,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 +104,42 @@ 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 derive_t stats_values_dropped; +static bool record_statistics; /* * Static functions @@ -389,50 +389,44 @@ static int plugin_unregister(llist_t *list, const char *name) /* {{{ */ 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(const char *file, _Bool global) { - void (*reg_handle)(void); - +/* 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] = ""; 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.", + "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()); - 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); + /* 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; } - reg_handle = (void (*)(void))dlsym(dlh, "module_register"); + void (*reg_handle)(void) = dlsym(dlh, "module_register"); if (reg_handle == NULL) { - WARNING("Couldn't find symbol \"module_register\" in \"%s\": %s\n", file, - dlerror()); + ERROR("Couldn't find symbol \"module_register\" in \"%s\": %s\n", file, + dlerror()); dlclose(dlh); - return -1; + return ENOENT; } (*reg_handle)(); - return 0; } @@ -610,9 +604,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); @@ -638,15 +630,14 @@ 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]; - snprintf(name, sizeof(name), "reader#%zu", read_threads_num); + snprintf(name, sizeof(name), "reader#%" PRIsz, read_threads_num); set_thread_name(read_threads[read_threads_num], name); read_threads_num++; @@ -657,7 +648,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; @@ -845,15 +836,14 @@ 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]; - snprintf(name, sizeof(name), "writer#%zu", write_threads_num); + snprintf(name, sizeof(name), "writer#%" PRIsz, write_threads_num); set_thread_name(write_threads[write_threads_num], name); write_threads_num++; @@ -868,10 +858,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); @@ -900,7 +890,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"); } @@ -922,7 +912,7 @@ void plugin_set_dir(const char *dir) { ERROR("plugin_set_dir: strdup(\"%s\") failed", dir); } -static _Bool plugin_is_loaded(char const *name) { +static bool plugin_is_loaded(char const *name) { int status; if (plugins_loaded == NULL) @@ -964,7 +954,7 @@ static void plugin_free_loaded(void) { } #define BUFSIZE 512 -int plugin_load(char const *plugin_name, _Bool global) { +int plugin_load(char const *plugin_name, bool global) { DIR *dh; const char *dir; char filename[BUFSIZE] = ""; @@ -998,7 +988,7 @@ int plugin_load(char const *plugin_name, _Bool global) { */ if ((strcasecmp("perl", plugin_name) == 0) || (strcasecmp("python", plugin_name) == 0)) - global = 1; + global = true; /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the * type when matching the filename */ @@ -1009,9 +999,7 @@ int plugin_load(char const *plugin_name, _Bool global) { } if ((dh = opendir(dir)) == NULL) { - char errbuf[1024]; - ERROR("plugin_load: opendir (%s) failed: %s", dir, - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("plugin_load: opendir (%s) failed: %s", dir, STRERRNO); return -1; } @@ -1026,9 +1014,7 @@ int plugin_load(char const *plugin_name, _Bool global) { } 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 */ @@ -1322,8 +1308,7 @@ int plugin_register_missing(const char *name, plugin_missing_cb callback, } /* int plugin_register_missing */ int plugin_register_shutdown(const char *name, int (*callback)(void)) { - return create_register_callback(&list_shutdown, name, (void *)callback, - NULL); + return create_register_callback(&list_shutdown, name, (void *)callback, NULL); } /* int plugin_register_shutdown */ static void plugin_free_data_sets(void) { @@ -1560,7 +1545,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); } @@ -1874,23 +1859,16 @@ int plugin_shutdown_all(void) { int plugin_dispatch_missing(const value_list_t *vl) /* {{{ */ { - llentry_t *le; - if (list_missing == NULL) 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; + callback_func_t *cf = le->value; + plugin_ctx_t old_ctx = plugin_set_ctx(cf->cf_ctx); + plugin_missing_cb callback = cf->cf_callback; - cf = le->value; - old_ctx = plugin_set_ctx(cf->cf_ctx); - 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) { @@ -1912,9 +1890,7 @@ 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); @@ -1934,7 +1910,7 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { * 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, @@ -1949,6 +1925,7 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { 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]; @@ -1979,8 +1956,8 @@ 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; } @@ -2017,7 +1994,7 @@ 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; } @@ -2046,9 +2023,9 @@ static double get_drop_probability(void) /* {{{ */ 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; @@ -2056,11 +2033,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) { @@ -2077,14 +2054,14 @@ 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) { int status; @@ -2101,10 +2078,9 @@ int plugin_dispatch_values(value_list_t const *vl) { 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))); + ERROR("plugin_dispatch_values: plugin_write_enqueue failed with status %i " + "(%s).", + status, STRERROR(status)); return status; } @@ -2113,7 +2089,7 @@ int plugin_dispatch_values(value_list_t const *vl) { __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; @@ -2354,7 +2330,7 @@ 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: { @@ -2399,7 +2375,7 @@ int plugin_notification_meta_add_double(notification_t *n, const char *name, } int plugin_notification_meta_add_boolean(notification_t *n, const char *name, - _Bool value) { + bool value) { return plugin_notification_meta_add(n, name, NM_TYPE_BOOLEAN, &value); } @@ -2471,9 +2447,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; } @@ -2486,7 +2460,7 @@ static plugin_ctx_t *plugin_ctx_create(void) { 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) {