From: Matthias Runge Date: Thu, 6 Feb 2020 09:59:27 +0000 (+0100) Subject: Merge pull request #3339 from jkohen/patch-1 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=master;hp=8d5ac5e09dddd774c2bee26a765efa0f3eebc462 Merge pull request #3339 from jkohen/patch-1 Expose meta_data_toc function in utils_cache. --- diff --git a/.travis.yml b/.travis.yml index f7153435..45fb898a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -111,7 +111,6 @@ addons: packages: - curl - glib - - grpc - hiredis - libdbi - libmemcached diff --git a/configure.ac b/configure.ac index 9fc40b17..00e1f6a5 100644 --- a/configure.ac +++ b/configure.ac @@ -4711,7 +4711,7 @@ if test "$PYTHON_CONFIG" != ""; then if test $? -ne 0; then with_libpython="no" fi - LIBPYTHON_LDFLAGS="`${PYTHON_CONFIG} --ldflags`" + LIBPYTHON_LDFLAGS="`${PYTHON_CONFIG} --ldflags --embed`" || LIBPYTHON_LDFLAGS="`${PYTHON_CONFIG} --ldflags`" if test $? -ne 0; then with_libpython="no" fi diff --git a/src/aggregation.c b/src/aggregation.c index 2c8ef880..e2f8ff1c 100644 --- a/src/aggregation.c +++ b/src/aggregation.c @@ -194,17 +194,21 @@ static int agg_instance_create_name(agg_instance_t *inst, /* {{{ */ sstrncpy(tmp_plugin_instance, agg->ident.plugin_instance, sizeof(tmp_plugin_instance)); + // Both tmp_plugin and tmp_plugin_instance are empty. if ((strcmp("", tmp_plugin) == 0) && (strcmp("", tmp_plugin_instance) == 0)) sstrncpy(inst->ident.plugin_instance, AGG_FUNC_PLACEHOLDER, sizeof(inst->ident.plugin_instance)); - else if (strcmp("", tmp_plugin) != 0) + // tmp_plugin is non-empty, and tmp_plugin_instance is empty. + else if (strcmp("", tmp_plugin_instance) == 0) ssnprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance), "%s-%s", tmp_plugin, AGG_FUNC_PLACEHOLDER); - else if (strcmp("", tmp_plugin_instance) != 0) + // tmp_plugin is empty, and tmp_plugin_instance is non-empty. + else if (strcmp("", tmp_plugin) == 0) ssnprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance), "%s-%s", tmp_plugin_instance, AGG_FUNC_PLACEHOLDER); + // Both tmp_plugin and tmp_plugin_instance are non-empty. else ssnprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance), "%s-%s-%s", tmp_plugin, diff --git a/src/amqp1.c b/src/amqp1.c index 67c96b75..4325f001 100644 --- a/src/amqp1.c +++ b/src/amqp1.c @@ -572,8 +572,10 @@ static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */ else if (strcasecmp("Format", child->key) == 0) { char *key = NULL; status = cf_util_get_string(child, &key); - if (status != 0) + if (status != 0) { + amqp1_config_instance_free(instance); return status; + } assert(key != NULL); if (strcasecmp(key, "Command") == 0) { instance->format = AMQP1_FORMAT_COMMAND; @@ -627,12 +629,14 @@ static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */ status = ssnprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name); if ((status < 0) || (size_t)status >= sizeof(tpname)) { ERROR("amqp1 plugin: Instance name would have been truncated."); + amqp1_config_instance_free(instance); return -1; } status = ssnprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s", transport->address, instance->name); if ((status < 0) || (size_t)status >= sizeof(instance->send_to)) { ERROR("amqp1 plugin: send_to address would have been truncated."); + amqp1_config_instance_free(instance); return -1; } if (instance->notify) { diff --git a/src/battery.c b/src/battery.c index 20ec2612..9b5b82d5 100644 --- a/src/battery.c +++ b/src/battery.c @@ -349,12 +349,10 @@ static int sysfs_file_to_buffer(char const *dir, /* {{{ */ snprintf(filename, sizeof(filename), "%s/%s/%s", dir, power_supply, basename); - status = (int)read_file_contents(filename, buffer, buffer_size - 1); + status = (int)read_text_file_contents(filename, buffer, buffer_size); if (status < 0) return status; - buffer[status] = '\0'; - strstripnewline(buffer); return 0; } /* }}} int sysfs_file_to_buffer */ diff --git a/src/cpu.c b/src/cpu.c index 09d60fe4..8c1d4cbd 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -58,15 +58,14 @@ #include #endif /* HAVE_LIBKSTAT */ -#if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) || \ - (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME) -#ifdef HAVE_SYS_SYSCTL_H +#if (defined(HAVE_SYSCTL) && defined(HAVE_SYSCTLBYNAME)) || defined(__OpenBSD__) +/* Implies BSD variant */ #include #endif #ifdef HAVE_SYS_DKSTAT_H +/* implies BSD variant */ #include -#endif #if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || \ !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES) @@ -77,18 +76,16 @@ #define CP_IDLE 4 #define CPUSTATES 5 #endif -#endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */ +#endif /* HAVE_SYS_DKSTAT_H */ -#if HAVE_SYSCTL +#define CAN_USE_SYSCTL 0 +#if (defined(HAVE_SYSCTL) && defined(HAVE_SYSCTLBYNAME)) || defined(__OpenBSD__) +/* Implies BSD variant */ #if defined(CTL_HW) && defined(HW_NCPU) && defined(CTL_KERN) && \ defined(KERN_CPTIME) && defined(CPUSTATES) #define CAN_USE_SYSCTL 1 -#else -#define CAN_USE_SYSCTL 0 -#endif -#else -#define CAN_USE_SYSCTL 0 #endif +#endif /* HAVE_SYSCTL_H && HAVE_SYSCTLBYNAME || __OpenBSD__ */ #define COLLECTD_CPU_STATE_USER 0 #define COLLECTD_CPU_STATE_SYSTEM 1 @@ -146,10 +143,12 @@ static int numcpu; /* #endif HAVE_LIBKSTAT */ #elif CAN_USE_SYSCTL +/* Only possible for (Open) BSD variant */ static int numcpu; /* #endif CAN_USE_SYSCTL */ #elif defined(HAVE_SYSCTLBYNAME) +/* Implies BSD variant */ static int numcpu; #ifdef HAVE_SYSCTL_KERN_CP_TIMES static int maxcpu; @@ -270,6 +269,7 @@ static int init(void) { /* #endif HAVE_LIBKSTAT */ #elif CAN_USE_SYSCTL + /* Only on (Open) BSD variant */ size_t numcpu_size; int mib[2] = {CTL_HW, HW_NCPU}; int status; @@ -285,6 +285,7 @@ static int init(void) { /* #endif CAN_USE_SYSCTL */ #elif defined(HAVE_SYSCTLBYNAME) + /* Only on BSD varient */ size_t numcpu_size; numcpu_size = sizeof(numcpu); @@ -732,6 +733,7 @@ static int cpu_read(void) { /* }}} #endif defined(HAVE_LIBKSTAT) */ #elif CAN_USE_SYSCTL /* {{{ */ + /* Only on (Open) BSD variant */ uint64_t cpuinfo[numcpu][CPUSTATES]; size_t cpuinfo_size; int status; @@ -790,6 +792,7 @@ static int cpu_read(void) { #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ \ */ + /* Only on BSD variant */ long cpuinfo[maxcpu][CPUSTATES]; size_t cpuinfo_size; @@ -812,6 +815,7 @@ static int cpu_read(void) { /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */ #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */ + /* Only on BSD variant */ long cpuinfo[CPUSTATES]; size_t cpuinfo_size; diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index 3a7364ed..61fa9013 100644 --- a/src/daemon/configfile.c +++ b/src/daemon/configfile.c @@ -404,6 +404,10 @@ static int dispatch_block_plugin(oconfig_item_t *ci) { } } + /* Try to be backward-compatible with previous versions */ + if (ci->children_num == 0) + return 0; + /* Hm, no complex plugin found. Dispatch the values one by one */ cf_callback_t *cf_cb = cf_search(plugin_name); if (cf_cb == NULL) { diff --git a/src/memory.c b/src/memory.c index 10bccdee..107e867f 100644 --- a/src/memory.c +++ b/src/memory.c @@ -28,7 +28,9 @@ #include "plugin.h" #include "utils/common/common.h" -#ifdef HAVE_SYS_SYSCTL_H +#if (defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)) || \ + defined(__OpenBSD__) +/* Implies BSD variant */ #include #endif #ifdef HAVE_SYS_VMMETER_H @@ -80,9 +82,10 @@ static kstat_t *ksp; static kstat_t *ksz; /* #endif HAVE_LIBKSTAT */ -#elif HAVE_SYSCTL +#elif HAVE_SYSCTL && __OpenBSD__ +/* OpenBSD variant does not have sysctlbyname */ static int pagesize; -/* #endif HAVE_SYSCTL */ +/* #endif HAVE_SYSCTL && __OpenBSD__ */ #elif HAVE_LIBSTATGRAB /* no global variables */ @@ -142,13 +145,14 @@ static int memory_init(void) { /* #endif HAVE_LIBKSTAT */ -#elif HAVE_SYSCTL +#elif HAVE_SYSCTL && __OpenBSD__ + /* OpenBSD variant does not have sysctlbyname */ pagesize = getpagesize(); if (pagesize <= 0) { ERROR("memory plugin: Invalid pagesize: %i", pagesize); return -1; } - /* #endif HAVE_SYSCTL */ + /* #endif HAVE_SYSCTL && __OpenBSD__ */ #elif HAVE_LIBSTATGRAB /* no init stuff */ @@ -408,7 +412,8 @@ static int memory_read_internal(value_list_t *vl) { (gauge_t)arcsize, "unusable", (gauge_t)mem_unus); /* #endif HAVE_LIBKSTAT */ -#elif HAVE_SYSCTL +#elif HAVE_SYSCTL && __OpenBSD__ + /* OpenBSD variant does not have HAVE_SYSCTLBYNAME */ int mib[] = {CTL_VM, VM_METER}; struct vmtotal vmtotal = {0}; gauge_t mem_active; @@ -430,7 +435,7 @@ static int memory_read_internal(value_list_t *vl) { MEMORY_SUBMIT("active", mem_active, "inactive", mem_inactive, "free", mem_free); - /* #endif HAVE_SYSCTL */ + /* #endif HAVE_SYSCTL && __OpenBSD__ */ #elif HAVE_LIBSTATGRAB sg_mem_stats *ios; diff --git a/src/network.c b/src/network.c index a1a6e3d0..613caa73 100644 --- a/src/network.c +++ b/src/network.c @@ -2762,6 +2762,7 @@ network_config_set_bind_address(const oconfig_item_t *ci, *bind_address = malloc(sizeof(**bind_address)); if (*bind_address == NULL) { ERROR("network plugin: network_config_set_bind_address: malloc failed."); + freeaddrinfo(res); return -1; } (*bind_address)->ss_family = res->ai_family; diff --git a/src/processes.c b/src/processes.c index f83913af..4761f60e 100644 --- a/src/processes.c +++ b/src/processes.c @@ -1316,11 +1316,10 @@ static int ps_read_process(long pid, process_entry_t *ps, char *state) { snprintf(filename, sizeof(filename), "/proc/%li/stat", pid); - status = read_file_contents(filename, buffer, sizeof(buffer) - 1); + status = read_text_file_contents(filename, buffer, sizeof(buffer)); if (status <= 0) return -1; buffer_len = (size_t)status; - buffer[buffer_len] = 0; /* The name of the process is enclosed in parens. Since the name can * contain parens itself, spaces, numbers and pretty much everything @@ -1569,7 +1568,7 @@ static char *ps_get_cmdline(long pid, snprintf(path, sizeof(path), "/proc/%li/psinfo", pid); - status = read_file_contents(path, (void *)&info, sizeof(info)); + status = read_file_contents(path, &info, sizeof(info)); if ((status < 0) || (((size_t)status) != sizeof(info))) { ERROR("processes plugin: Unexpected return value " "while reading \"%s\": " diff --git a/src/swap.c b/src/swap.c index 61c9e284..0e317e76 100644 --- a/src/swap.c +++ b/src/swap.c @@ -49,7 +49,9 @@ #if HAVE_SYS_PARAM_H #include #endif -#if HAVE_SYS_SYSCTL_H +#if (defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)) || \ + defined(__OpenBSD__) +/* implies BSD variant */ #include #endif #if HAVE_SYS_DKSTAT_H diff --git a/src/thermal.c b/src/thermal.c index 5f448363..1ed56174 100644 --- a/src/thermal.c +++ b/src/thermal.c @@ -103,7 +103,7 @@ static int thermal_procfs_device_read(const char __attribute__((unused)) * dir, if ((len < 0) || ((size_t)len >= sizeof(filename))) return -1; - len = (ssize_t)read_file_contents(filename, data, sizeof(data)); + len = (ssize_t)read_text_file_contents(filename, data, sizeof(data)); if ((len > 0) && ((size_t)len > sizeof(str_temp)) && (data[--len] == '\n') && (!strncmp(data, str_temp, sizeof(str_temp) - 1))) { char *endptr = NULL; diff --git a/src/turbostat.c b/src/turbostat.c index 19a51115..8bbb92b5 100644 --- a/src/turbostat.c +++ b/src/turbostat.c @@ -1103,7 +1103,11 @@ static int __attribute__((format(printf, 1, 2))) parse_int_file(const char *fmt, ...) { va_list args; char path[PATH_MAX]; + char buf[256]; int len; + value_t v; + char *c; + FILE *fp; va_start(args, fmt); len = vsnprintf(path, sizeof(path), fmt, args); @@ -1113,8 +1117,29 @@ parse_int_file(const char *fmt, ...) { return -1; } - value_t v; - if (parse_value_file(path, &v, DS_TYPE_DERIVE) != 0) { + fp = fopen(path, "r"); + if (fp == NULL) { + ERROR("turbostat plugin: unable to open: '%s': %s", path, strerror(errno)); + return -1; + } + + if (fgets(buf, sizeof(buf), fp) == NULL) { + ERROR("turbostat plugin: unable to read: '%s': %s", path, strerror(errno)); + fclose(fp); + return -1; + } + fclose(fp); + + /* We only care about the first integer in the range */ + c = strchr(buf, '-'); + if (c != NULL) + *c = '\0'; + c = strchr(buf, ','); + if (c != NULL) + *c = '\0'; + strstripnewline(buf); + + if (parse_value(buf, &v, DS_TYPE_DERIVE) != 0) { ERROR("turbostat plugin: Parsing \"%s\" failed.", path); return -1; } diff --git a/src/utils/common/common.c b/src/utils/common/common.c index aad767ea..2cebc0d5 100644 --- a/src/utils/common/common.c +++ b/src/utils/common/common.c @@ -1263,7 +1263,7 @@ int walk_directory(const char *dir, dirwalk_callback_f callback, return 0; } -ssize_t read_file_contents(const char *filename, char *buf, size_t bufsize) { +ssize_t read_file_contents(const char *filename, void *buf, size_t bufsize) { FILE *fh; ssize_t ret; @@ -1281,6 +1281,16 @@ ssize_t read_file_contents(const char *filename, char *buf, size_t bufsize) { return ret; } +ssize_t read_text_file_contents(const char *filename, char *buf, + size_t bufsize) { + ssize_t ret = read_file_contents(filename, buf, bufsize - 1); + if (ret < 0) + return ret; + + buf[ret] = '\0'; + return ret + 1; +} + counter_t counter_diff(counter_t old_value, counter_t new_value) { counter_t diff; diff --git a/src/utils/common/common.h b/src/utils/common/common.h index 1ca65054..4e2eceda 100644 --- a/src/utils/common/common.h +++ b/src/utils/common/common.h @@ -356,7 +356,11 @@ typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename, int walk_directory(const char *dir, dirwalk_callback_f callback, void *user_data, int hidden); /* Returns the number of bytes read or negative on error. */ -ssize_t read_file_contents(char const *filename, char *buf, size_t bufsize); +ssize_t read_file_contents(char const *filename, void *buf, size_t bufsize); +/* Writes the contents of the file into the buffer with a trailing NUL. + * Returns the number of bytes written to the buffer or negative on error. */ +ssize_t read_text_file_contents(char const *filename, char *buf, + size_t bufsize); counter_t counter_diff(counter_t old_value, counter_t new_value); diff --git a/src/uuid.c b/src/uuid.c index 60d09b51..a5fba034 100644 --- a/src/uuid.c +++ b/src/uuid.c @@ -29,7 +29,9 @@ #include "plugin.h" #include "utils/common/common.h" -#if HAVE_SYS_SYSCTL_H +#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) || \ + defined(__OpenBSD__) +/* Implies have BSD variant */ #include #endif