From: Florian Forster Date: Sun, 10 Feb 2019 20:44:20 +0000 (+0100) Subject: Merge branch 'collectd-5.7' into collectd-5.8 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=2700666217249cd4794dfc17b4ac3b28f3dca56b;hp=-c Merge branch 'collectd-5.7' into collectd-5.8 --- 2700666217249cd4794dfc17b4ac3b28f3dca56b diff --combined src/daemon/common.c index d5322ed8,8502208d..eb9f590a --- a/src/daemon/common.c +++ b/src/daemon/common.c @@@ -37,6 -37,10 +37,6 @@@ #include "plugin.h" #include "utils_cache.h" -#ifdef HAVE_MATH_H -#include -#endif - /* for getaddrinfo */ #include #include @@@ -60,10 -64,6 +60,10 @@@ #include #endif +#if HAVE_KSTAT_H +#include +#endif + #ifdef HAVE_LIBKSTAT extern kstat_ctl_t *kc; #endif @@@ -85,9 -85,21 +85,9 @@@ char *sstrncpy(char *dest, const char * strncpy(dest, src, n); dest[n - 1] = '\0'; - return (dest); + return dest; } /* char *sstrncpy */ -int ssnprintf(char *dest, size_t n, const char *format, ...) { - int ret = 0; - va_list ap; - - va_start(ap, format); - ret = vsnprintf(dest, n, format, ap); - dest[n - 1] = '\0'; - va_end(ap); - - return (ret); -} /* int ssnprintf */ - char *ssnprintf_alloc(char const *format, ...) /* {{{ */ { char static_buffer[1024] = ""; @@@ -103,17 -115,17 +103,17 @@@ status = vsnprintf(static_buffer, sizeof(static_buffer), format, ap); va_end(ap); if (status < 0) - return (NULL); + return NULL; /* "status" does not include the null byte. */ alloc_buffer_size = (size_t)(status + 1); if (alloc_buffer_size <= sizeof(static_buffer)) - return (strdup(static_buffer)); + return strdup(static_buffer); /* Allocate a buffer large enough to hold the string. */ alloc_buffer = calloc(1, alloc_buffer_size); if (alloc_buffer == NULL) - return (NULL); + return NULL; /* Print again into this new buffer. */ va_start(ap, format); @@@ -121,10 -133,10 +121,10 @@@ va_end(ap); if (status < 0) { sfree(alloc_buffer); - return (NULL); + return NULL; } - return (alloc_buffer); + return alloc_buffer; } /* }}} char *ssnprintf_alloc */ char *sstrdup(const char *s) { @@@ -132,7 -144,7 +132,7 @@@ size_t sz; if (s == NULL) - return (NULL); + return NULL; /* Do not use `strdup' here, because it's not specified in POSIX. It's * ``only'' an XSI extension. */ @@@ -142,9 -154,9 +142,9 @@@ ERROR("sstrdup: Out of memory."); exit(3); } - memcpy(r, s, sizeof(char) * sz); + memcpy(r, s, sz); - return (r); + return r; } /* char *sstrdup */ /* Even though Posix requires "strerror_r" to return an "int", @@@ -183,13 -195,13 +183,13 @@@ char *sstrerror(int errnum, char *buf, #else if (strerror_r(errnum, buf, buflen) != 0) { - ssnprintf(buf, buflen, "Error #%i; " - "Additionally, strerror_r failed.", - errnum); + snprintf(buf, buflen, "Error #%i; " + "Additionally, strerror_r failed.", + errnum); } #endif /* STRERROR_R_CHAR_P */ - return (buf); + return buf; } /* char *sstrerror */ void *smalloc(size_t size) { @@@ -200,7 -212,7 +200,7 @@@ exit(3); } - return (r); + return r; } /* void *smalloc */ #if 0 @@@ -216,7 -228,7 +216,7 @@@ void sfree (void **ptr } #endif -ssize_t sread(int fd, void *buf, size_t count) { +int sread(int fd, void *buf, size_t count) { char *ptr; size_t nleft; ssize_t status; @@@ -231,11 -243,14 +231,11 @@@ continue; if (status < 0) - return (status); + return status; if (status == 0) { - DEBUG("Received EOF from fd %i. " - "Closing fd and returning error.", - fd); - close(fd); - return (-1); + DEBUG("Received EOF from fd %i. ", fd); + return -1; } assert((0 > status) || (nleft >= (size_t)status)); @@@ -244,10 -259,10 +244,10 @@@ ptr = ptr + ((size_t)status); } - return (0); + return 0; } -ssize_t swrite(int fd, const void *buf, size_t count) { +int swrite(int fd, const void *buf, size_t count) { const char *ptr; size_t nleft; ssize_t status; @@@ -270,8 -285,7 +270,8 @@@ if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) { /* if recv returns zero (even though poll() said there is data to be * read), that means the connection has been closed */ - return errno ? errno : -1; + errno = ECONNRESET; + return -1; } } @@@ -288,7 -302,7 +288,7 @@@ ptr = ptr + ((size_t)status); } - return (0); + return 0; } int strsplit(char *string, char **fields, size_t size) { @@@ -307,7 -321,7 +307,7 @@@ break; } - return ((int)i); + return (int)i; } int strjoin(char *buffer, size_t buffer_size, char **fields, size_t fields_num, @@@ -320,7 -334,7 +320,7 @@@ if (((fields_num != 0) && (fields == NULL)) || ((buffer_size != 0) && (buffer == NULL))) - return (-EINVAL); + return -EINVAL; if (buffer != NULL) buffer[0] = 0; @@@ -338,6 -352,9 +338,9 @@@ buffer_req += sep_len; buffer_req += field_len; + if (buffer_size == 0) + continue; + if ((i != 0) && (sep_len > 0)) { if (sep_len >= avail) { /* prevent subsequent iterations from writing to the @@@ -373,14 -390,14 +376,14 @@@ int escape_string(char *buffer, size_t /* Check if we need to escape at all first */ temp = strpbrk(buffer, " \t\"\\"); if (temp == NULL) - return (0); + return 0; if (buffer_size < 3) - return (EINVAL); + return EINVAL; temp = calloc(1, buffer_size); if (temp == NULL) - return (ENOMEM); + return ENOMEM; temp[0] = '"'; j = 1; @@@ -408,7 -425,7 +411,7 @@@ sstrncpy(buffer, temp, buffer_size); sfree(temp); - return (0); + return 0; } /* int escape_string */ int strunescape(char *buf, size_t buf_len) { @@@ -420,7 -437,7 +423,7 @@@ ERROR("string unescape: backslash found at end of string."); /* Ensure null-byte at the end of the buffer. */ buf[i] = 0; - return (-1); + return -1; } switch (buf[i + 1]) { @@@ -443,7 -460,7 +446,7 @@@ memmove(buf + i + 1, buf + i + 2, buf_len - i - 2); buf[buf_len - 1] = 0; } - return (0); + return 0; } /* int strunescape */ size_t strstripnewline(char *buffer) { @@@ -456,7 -473,7 +459,7 @@@ buffer[buffer_len] = 0; } - return (buffer_len); + return buffer_len; } /* size_t strstripnewline */ int escape_slashes(char *buffer, size_t buffer_size) { @@@ -467,10 -484,10 +470,10 @@@ if (buffer_len <= 1) { if (strcmp("/", buffer) == 0) { if (buffer_size < 5) - return (-1); + return -1; sstrncpy(buffer, "root", buffer_size); } - return (0); + return 0; } /* Move one to the left */ @@@ -484,7 -501,7 +487,7 @@@ buffer[i] = '_'; } - return (0); + return 0; } /* int escape_slashes */ void replace_special(char *buffer, size_t buffer_size) { @@@ -510,7 -527,7 +513,7 @@@ int timeval_cmp(struct timeval tv0, str delta->tv_sec = 0; delta->tv_usec = 0; } - return (0); + return 0; } if ((tv0.tv_sec < tv1.tv_sec) || @@@ -538,7 -555,7 +541,7 @@@ assert((delta == NULL) || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000))); - return (status); + return status; } /* int timeval_cmp */ int check_create_dir(const char *file_orig) { @@@ -559,12 -576,12 +562,12 @@@ * Sanity checks first */ if (file_orig == NULL) - return (-1); + return -1; if ((len = strlen(file_orig)) < 1) - return (-1); + return -1; else if (len >= sizeof(file_copy)) - return (-1); + return -1; /* * If `file_orig' ends in a slash the last component is a directory, @@@ -608,7 -625,7 +611,7 @@@ ERROR("Cowardly refusing to create a directory that " "begins with a `.' (dot): `%s'", file_orig); - return (-2); + return -2; } /* @@@ -618,7 -635,7 +621,7 @@@ if (strjoin(dir + path_is_absolute, (size_t)(dir_len - path_is_absolute), fields, (size_t)(i + 1), "/") < 0) { ERROR("strjoin failed: `%s', component #%i", file_orig, i); - return (-1); + return -1; } while (42) { @@@ -636,24 -653,24 +639,24 @@@ char errbuf[1024]; ERROR("check_create_dir: mkdir (%s): %s", dir, sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + return -1; } else { char errbuf[1024]; ERROR("check_create_dir: stat (%s): %s", dir, sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + return -1; } } else if (!S_ISDIR(statbuf.st_mode)) { ERROR("check_create_dir: `%s' exists but is not " "a directory!", dir); - return (-1); + return -1; } break; } } - return (0); + return 0; } /* check_create_dir */ #ifdef HAVE_LIBKSTAT @@@ -663,20 -680,20 +666,20 @@@ int get_kstat(kstat_t **ksp_ptr, char * *ksp_ptr = NULL; if (kc == NULL) - return (-1); + return -1; - ssnprintf(ident, sizeof(ident), "%s,%i,%s", module, instance, name); + snprintf(ident, sizeof(ident), "%s,%i,%s", module, instance, name); *ksp_ptr = kstat_lookup(kc, module, instance, name); if (*ksp_ptr == NULL) { ERROR("get_kstat: Cound not find kstat %s", ident); - return (-1); + return -1; } if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) { ERROR("get_kstat: kstat %s has wrong type", ident); *ksp_ptr = NULL; - return (-1); + return -1; } #ifdef assert @@@ -686,15 -703,15 +689,15 @@@ if (kstat_read(kc, *ksp_ptr, NULL) == -1) { ERROR("get_kstat: kstat %s could not be read", ident); - return (-1); + return -1; } if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) { ERROR("get_kstat: kstat %s has wrong type", ident); - return (-1); + return -1; } - return (0); + return 0; } long long get_kstat_value(kstat_t *ksp, char *name) { @@@ -703,16 -720,16 +706,16 @@@ if (ksp == NULL) { ERROR("get_kstat_value (\"%s\"): ksp is NULL.", name); - return (-1LL); + return -1LL; } else if (ksp->ks_type != KSTAT_TYPE_NAMED) { ERROR("get_kstat_value (\"%s\"): ksp->ks_type (%#x) " "is not KSTAT_TYPE_NAMED (%#x).", name, (unsigned int)ksp->ks_type, (unsigned int)KSTAT_TYPE_NAMED); - return (-1LL); + return -1LL; } if ((kn = (kstat_named_t *)kstat_data_lookup(ksp, name)) == NULL) - return (-1LL); + return -1LL; if (kn->data_type == KSTAT_DATA_INT32) retval = (long long)kn->value.i32; @@@ -727,14 -744,14 +730,14 @@@ else WARNING("get_kstat_value: Not a numeric value: %s", name); - return (retval); + return retval; } #endif /* HAVE_LIBKSTAT */ #ifndef HAVE_HTONLL unsigned long long ntohll(unsigned long long n) { #if BYTE_ORDER == BIG_ENDIAN - return (n); + return n; #else return (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32); #endif @@@ -742,7 -759,7 +745,7 @@@ unsigned long long htonll(unsigned long long n) { #if BYTE_ORDER == BIG_ENDIAN - return (n); + return n; #else return (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32); #endif @@@ -783,13 -800,13 +786,13 @@@ double ntohd(double d) if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00) && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00) && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00) && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f)) { - return (NAN); + return NAN; } else { uint64_t tmp; tmp = ret.integer; ret.integer = FP_CONVERT(tmp); - return (ret.floating); + return ret.floating; } } /* double ntohd */ @@@ -805,14 -822,14 +808,14 @@@ double htond(double d) ret.byte[4] = ret.byte[5] = 0x00; ret.byte[6] = 0xf8; ret.byte[7] = 0x7f; - return (ret.floating); + return ret.floating; } else { uint64_t tmp; ret.floating = d; tmp = FP_CONVERT(ret.integer); ret.integer = tmp; - return (ret.floating); + return ret.floating; } } /* double htond */ #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */ @@@ -830,7 -847,7 +833,7 @@@ int format_name(char *ret, int ret_len do { \ size_t l = strlen(str); \ if (l >= buffer_size) \ - return (ENOBUFS); \ + return ENOBUFS; \ memcpy(buffer, (str), l); \ buffer += l; \ buffer_size -= l; \ @@@ -856,7 -873,7 +859,7 @@@ buffer[0] = 0; #undef APPEND - return (0); + return 0; } /* int format_name */ int format_values(char *ret, size_t ret_len, /* {{{ */ @@@ -872,13 -889,13 +875,13 @@@ #define BUFFER_ADD(...) \ do { \ - status = ssnprintf(ret + offset, ret_len - offset, __VA_ARGS__); \ + status = snprintf(ret + offset, ret_len - offset, __VA_ARGS__); \ if (status < 1) { \ sfree(rates); \ - return (-1); \ + return -1; \ } else if (((size_t)status) >= (ret_len - offset)) { \ sfree(rates); \ - return (-1); \ + return -1; \ } else \ offset += ((size_t)status); \ } while (0) @@@ -893,7 -910,7 +896,7 @@@ rates = uc_get_rate(ds, vl); if (rates == NULL) { WARNING("format_values: uc_get_rate failed."); - return (-1); + return -1; } BUFFER_ADD(":" GAUGE_FORMAT, rates[i]); } else if (ds->ds[i].type == DS_TYPE_COUNTER) @@@ -905,14 -922,14 +908,14 @@@ else { ERROR("format_values: Unknown data source type: %i", ds->ds[i].type); sfree(rates); - return (-1); + return -1; } } /* for ds->ds_num */ #undef BUFFER_ADD sfree(rates); - return (0); + return 0; } /* }}} int format_values */ int parse_identifier(char *str, char **ret_host, char **ret_plugin, @@@ -926,18 -943,18 +929,18 @@@ hostname = str; if (hostname == NULL) - return (-1); + return -1; plugin = strchr(hostname, '/'); if (plugin == NULL) - return (-1); + return -1; *plugin = '\0'; plugin++; type = strchr(plugin, '/'); if (type == NULL) { if (default_host == NULL) - return (-1); + return -1; /* else: no host specified; use default */ type = plugin; plugin = hostname; @@@ -964,7 -981,7 +967,7 @@@ *ret_plugin_instance = plugin_instance; *ret_type = type; *ret_type_instance = type_instance; - return (0); + return 0; } /* int parse_identifier */ int parse_identifier_vl(const char *str, value_list_t *vl) /* {{{ */ @@@ -978,7 -995,7 +981,7 @@@ int status; if ((str == NULL) || (vl == NULL)) - return (EINVAL); + return EINVAL; sstrncpy(str_copy, str, sizeof(str_copy)); @@@ -986,7 -1003,7 +989,7 @@@ &type_instance, /* default_host = */ NULL); if (status != 0) - return (status); + return status; sstrncpy(vl->host, host, sizeof(vl->host)); sstrncpy(vl->plugin, plugin, sizeof(vl->plugin)); @@@ -997,7 -1014,7 +1000,7 @@@ sstrncpy(vl->type_instance, (type_instance != NULL) ? type_instance : "", sizeof(vl->type_instance)); - return (0); + return 0; } /* }}} int parse_identifier_vl */ int parse_value(const char *value_orig, value_t *ret_value, int ds_type) { @@@ -1006,11 -1023,11 +1009,11 @@@ size_t value_len; if (value_orig == NULL) - return (EINVAL); + return EINVAL; value = strdup(value_orig); if (value == NULL) - return (ENOMEM); + return ENOMEM; value_len = strlen(value); while ((value_len > 0) && isspace((int)value[value_len - 1])) { @@@ -1090,7 -1107,7 +1093,7 @@@ int parse_values(char *buffer, value_li || (endptr == ptr) /* Invalid string */ || (endptr == NULL) /* This should not happen */ || (*endptr != 0)) /* Trailing chars */ - return (-1); + return -1; vl->time = DOUBLE_TO_CDTIME_T(tmp); } @@@ -1107,8 -1124,8 +1110,8 @@@ } /* while (strtok_r) */ if ((ptr != NULL) || (i == 0)) - return (-1); - return (0); + return -1; + return 0; } /* int parse_values */ int parse_value_file(char const *path, value_t *ret_value, int ds_type) { @@@ -1117,11 -1134,11 +1120,11 @@@ fh = fopen(path, "r"); if (fh == NULL) - return (-1); + return -1; if (fgets(buffer, sizeof(buffer), fh) == NULL) { fclose(fh); - return (-1); + return -1; } fclose(fh); @@@ -1175,7 -1192,7 +1178,7 @@@ int getpwnam_r(const char *name, struc pthread_mutex_unlock(&getpwnam_r_lock); - return (status); + return status; } /* int getpwnam_r */ #endif /* !HAVE_GETPWNAM_R */ @@@ -1200,7 -1217,7 +1203,7 @@@ int notification_init(notification_t *n if (type_instance != NULL) sstrncpy(n->type_instance, type_instance, sizeof(n->type_instance)); - return (0); + return 0; } /* int notification_init */ int walk_directory(const char *dir, dirwalk_callback_f callback, @@@ -1242,8 -1259,8 +1245,8 @@@ closedir(dh); if ((success == 0) && (failure > 0)) - return (-1); - return (0); + return -1; + return 0; } ssize_t read_file_contents(const char *filename, char *buf, size_t bufsize) { @@@ -1252,7 -1269,7 +1255,7 @@@ fh = fopen(filename, "r"); if (fh == NULL) - return (-1); + return -1; ret = (ssize_t)fread(buf, 1, bufsize, fh); if ((ret == 0) && (ferror(fh) != 0)) { @@@ -1261,7 -1278,7 +1264,7 @@@ } fclose(fh); - return (ret); + return ret; } counter_t counter_diff(counter_t old_value, counter_t new_value) { @@@ -1276,7 -1293,7 +1279,7 @@@ diff = new_value - old_value; } - return (diff); + return diff; } /* counter_t counter_diff */ int rate_to_value(value_t *ret_value, gauge_t rate, /* {{{ */ @@@ -1289,7 -1306,7 +1292,7 @@@ state->last_time = t; *ret_value = state->last_value; - return (0); + return 0; } /* Counter and absolute can't handle negative rates. Reset "last time" @@@ -1298,13 -1315,13 +1301,13 @@@ if ((rate < 0.0) && ((ds_type == DS_TYPE_COUNTER) || (ds_type == DS_TYPE_ABSOLUTE))) { memset(state, 0, sizeof(*state)); - return (EINVAL); + return EINVAL; } /* Another invalid state: The time is not increasing. */ if (t <= state->last_time) { memset(state, 0, sizeof(*state)); - return (EINVAL); + return EINVAL; } delta_t = t - state->last_time; @@@ -1327,7 -1344,7 +1330,7 @@@ } state->last_time = t; - return (EAGAIN); + return EAGAIN; } /* }}} */ if (ds_type == DS_TYPE_DERIVE) { @@@ -1351,7 -1368,7 +1354,7 @@@ state->last_time = t; *ret_value = state->last_value; - return (0); + return 0; } /* }}} value_t rate_to_value */ int value_to_rate(gauge_t *ret_rate, /* {{{ */ @@@ -1362,7 -1379,7 +1365,7 @@@ /* Another invalid state: The time is not increasing. */ if (t <= state->last_time) { memset(state, 0, sizeof(*state)); - return (EINVAL); + return EINVAL; } interval = CDTIME_T_TO_DOUBLE(t - state->last_time); @@@ -1371,7 -1388,7 +1374,7 @@@ if (state->last_time == 0) { state->last_value = value; state->last_time = t; - return (EAGAIN); + return EAGAIN; } switch (ds_type) { @@@ -1400,7 -1417,7 +1403,7 @@@ state->last_value = value; state->last_time = t; - return (0); + return 0; } /* }}} value_t rate_to_value */ int service_name_to_port_number(const char *service_name) { @@@ -1409,7 -1426,7 +1412,7 @@@ int service_number; if (service_name == NULL) - return (-1); + return -1; struct addrinfo ai_hints = {.ai_family = AF_UNSPEC}; @@@ -1417,7 -1434,7 +1420,7 @@@ if (status != 0) { ERROR("service_name_to_port_number: getaddrinfo failed: %s", gai_strerror(status)); - return (-1); + return -1; } service_number = -1; @@@ -1442,8 -1459,8 +1445,8 @@@ freeaddrinfo(ai_list); if ((service_number > 0) && (service_number <= 65535)) - return (service_number); - return (-1); + return service_number; + return -1; } /* int service_name_to_port_number */ void set_sock_opts(int sockfd) /* {{{ */ @@@ -1489,16 -1506,16 +1492,16 @@@ int strtoderive(const char *string, der char *endptr; if ((string == NULL) || (ret_value == NULL)) - return (EINVAL); + return EINVAL; errno = 0; endptr = NULL; tmp = (derive_t)strtoll(string, &endptr, /* base = */ 0); if ((endptr == string) || (errno != 0)) - return (-1); + return -1; *ret_value = tmp; - return (0); + return 0; } /* }}} int strtoderive */ int strtogauge(const char *string, gauge_t *ret_value) /* {{{ */ @@@ -1507,18 -1524,18 +1510,18 @@@ char *endptr = NULL; if ((string == NULL) || (ret_value == NULL)) - return (EINVAL); + return EINVAL; errno = 0; endptr = NULL; tmp = (gauge_t)strtod(string, &endptr); if (errno != 0) - return (errno); + return errno; else if ((endptr == NULL) || (*endptr != 0)) - return (EINVAL); + return EINVAL; *ret_value = tmp; - return (0); + return 0; } /* }}} int strtogauge */ int strarray_add(char ***ret_array, size_t *ret_array_len, @@@ -1528,20 -1545,20 +1531,20 @@@ size_t array_len = *ret_array_len; if (str == NULL) - return (EINVAL); + return EINVAL; array = realloc(*ret_array, (array_len + 1) * sizeof(*array)); if (array == NULL) - return (ENOMEM); + return ENOMEM; *ret_array = array; array[array_len] = strdup(str); if (array[array_len] == NULL) - return (ENOMEM); + return ENOMEM; array_len++; *ret_array_len = array_len; - return (0); + return 0; } /* }}} int strarray_add */ void strarray_free(char **array, size_t array_len) /* {{{ */ @@@ -1559,27 -1576,27 +1562,27 @@@ int check_capability(int arg) /* {{{ * cap_flag_value_t cap_flag_value; if (!CAP_IS_SUPPORTED(cap_value)) - return (-1); + return -1; if (!(cap = cap_get_proc())) { ERROR("check_capability: cap_get_proc failed."); - return (-1); + return -1; } if (cap_get_flag(cap, cap_value, CAP_EFFECTIVE, &cap_flag_value) < 0) { ERROR("check_capability: cap_get_flag failed."); cap_free(cap); - return (-1); + return -1; } cap_free(cap); - return (cap_flag_value != CAP_SET); + return cap_flag_value != CAP_SET; } /* }}} int check_capability */ #else int check_capability(__attribute__((unused)) int arg) /* {{{ */ { WARNING("check_capability: unsupported capability implementation. " "Some plugin(s) may require elevated privileges to work properly."); - return (0); + return 0; } /* }}} int check_capability */ #endif /* HAVE_CAPABILITY */ diff --combined src/daemon/common_test.c index af2840e5,7a9f9142..93a19d1b --- a/src/daemon/common_test.c +++ b/src/daemon/common_test.c @@@ -27,10 -27,6 +27,10 @@@ #include "common.h" #include "testing.h" +#if HAVE_KSTAT_H +#include +#endif + #if HAVE_LIBKSTAT kstat_ctl_t *kc; #endif /* HAVE_LIBKSTAT */ @@@ -59,7 -55,28 +59,7 @@@ DEF_TEST(sstrncpy) EXPECT_EQ_STR("collect", ptr); OK(buffer[3] == buffer[12]); - return (0); -} - -DEF_TEST(ssnprintf) { - char buffer[16] = ""; - char *ptr = &buffer[4]; - int status; - - buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff; - buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff; - - status = ssnprintf(ptr, 8, "%i", 1337); - OK(status == 4); - EXPECT_EQ_STR("1337", ptr); - - status = ssnprintf(ptr, 8, "%s", "collectd"); - OK(status == 8); - OK(ptr[7] == 0); - EXPECT_EQ_STR("collect", ptr); - OK(buffer[3] == buffer[12]); - - return (0); + return 0; } DEF_TEST(sstrdup) { @@@ -74,7 -91,7 +74,7 @@@ ptr = sstrdup(NULL); OK(ptr == NULL); - return (0); + return 0; } DEF_TEST(strsplit) { @@@ -124,7 -141,7 +124,7 @@@ status = strsplit(buffer, fields, 8); OK(status == 0); - return (0); + return 0; } DEF_TEST(strjoin) { @@@ -168,12 -185,14 +168,14 @@@ cases[i].fields_num, cases[i].separator); EXPECT_EQ_INT(cases[i].want_return, status); EXPECT_EQ_STR(cases[i].want_buffer, buffer); - } - /* use (NULL, 0) to determine required buffer size. */ - EXPECT_EQ_INT(3, strjoin(NULL, 0, (char *[]){"a", "b"}, 2, "-")); + /* use (NULL, 0) to determine required buffer size. */ + EXPECT_EQ_INT(cases[i].want_return, + strjoin(NULL, 0, cases[i].fields, cases[i].fields_num, + cases[i].separator)); + } - return (0); + return 0; } DEF_TEST(escape_slashes) { @@@ -247,7 -266,7 +249,7 @@@ DEF_TEST(strunescape) status = strunescape(buffer, sizeof(buffer)); OK(status != 0); EXPECT_EQ_STR("\tbackslash end", buffer); - return (0); + return 0; /* Backslash at buffer end */ strncpy(buffer, "\\t3\\56", sizeof(buffer)); @@@ -261,7 -280,7 +263,7 @@@ OK(buffer[5] == '6'); OK(buffer[6] == '7'); - return (0); + return 0; } DEF_TEST(parse_values) { @@@ -306,7 -325,7 +308,7 @@@ EXPECT_EQ_DOUBLE(cases[i].value, vl.values[0].gauge); } - return (0); + return 0; } DEF_TEST(value_to_rate) { @@@ -363,6 -382,7 +365,6 @@@ int main(void) { RUN_TEST(sstrncpy); - RUN_TEST(ssnprintf); RUN_TEST(sstrdup); RUN_TEST(strsplit); RUN_TEST(strjoin); @@@ -374,3 -394,5 +376,3 @@@ END_TEST; } - -/* vim: set sw=2 sts=2 et : */ diff --combined src/log_logstash.c index 28cc34c8,6ebab3be..8ff90457 --- a/src/log_logstash.c +++ b/src/log_logstash.c @@@ -183,22 -183,14 +183,14 @@@ err static void log_logstash_log(int severity, const char *msg, user_data_t __attribute__((unused)) * user_data) { - yajl_gen g; - #if !defined(HAVE_YAJL_V2) - yajl_gen_config conf = {}; - - conf.beautify = 0; - #endif - if (severity > log_level) return; #if HAVE_YAJL_V2 - g = yajl_gen_alloc(NULL); + yajl_gen g = yajl_gen_alloc(NULL); #else - g = yajl_gen_alloc(&conf, NULL); + yajl_gen g = yajl_gen_alloc(&(yajl_gen_config){0}, NULL); #endif - if (g == NULL) { fprintf(stderr, "Could not allocate JSON generator.\n"); return; @@@ -236,7 -228,7 +228,7 @@@ static int log_logstash_notification(co if (g == NULL) { fprintf(stderr, "Could not allocate JSON generator.\n"); - return (0); + return 0; } if (yajl_gen_map_open(g) != yajl_gen_status_ok) @@@ -323,12 -315,12 +315,12 @@@ } log_logstash_print(g, LOG_INFO, (n->time != 0) ? n->time : cdtime()); - return (0); + return 0; err: yajl_gen_free(g); fprintf(stderr, "Could not correctly generate JSON notification\n"); - return (0); + return 0; } /* int log_logstash_notification */ void module_register(void) { @@@ -339,3 -331,5 +331,3 @@@ plugin_register_notification("log_logstash", log_logstash_notification, /* user_data = */ NULL); } /* void module_register (void) */ - -/* vim: set sw=4 ts=4 tw=78 noexpandtab : */ diff --combined src/write_prometheus.c index 28ab6ce7,572ba561..8554580f --- a/src/write_prometheus.c +++ b/src/write_prometheus.c @@@ -163,8 -163,8 +163,8 @@@ static char *format_labels(char *buffer * know that they are sane. */ for (size_t i = 0; i < m->n_label; i++) { char value[LABEL_VALUE_SIZE]; - ssnprintf(labels[i], LABEL_BUFFER_SIZE, "%s=\"%s\"", m->label[i]->name, - escape_label_value(value, sizeof(value), m->label[i]->value)); + snprintf(labels[i], LABEL_BUFFER_SIZE, "%s=\"%s\"", m->label[i]->name, + escape_label_value(value, sizeof(value), m->label[i]->value)); } strjoin(buffer, buffer_size, labels, m->n_label, ","); @@@ -182,13 -182,13 +182,13 @@@ static void format_text(ProtobufCBuffe while (c_avl_iterator_next(iter, (void *)&unused_name, (void *)&fam) == 0) { char line[1024]; /* 4x DATA_MAX_NAME_LEN? */ - ssnprintf(line, sizeof(line), "# HELP %s %s\n", fam->name, fam->help); + snprintf(line, sizeof(line), "# HELP %s %s\n", fam->name, fam->help); buffer->append(buffer, strlen(line), (uint8_t *)line); - ssnprintf(line, sizeof(line), "# TYPE %s %s\n", fam->name, - (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE) - ? "gauge" - : "counter"); + snprintf(line, sizeof(line), "# TYPE %s %s\n", fam->name, + (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE) + ? "gauge" + : "counter"); buffer->append(buffer, strlen(line), (uint8_t *)line); for (size_t i = 0; i < fam->n_metric; i++) { @@@ -198,17 -198,17 +198,17 @@@ char timestamp_ms[24] = ""; if (m->has_timestamp_ms) - ssnprintf(timestamp_ms, sizeof(timestamp_ms), " %" PRIi64, - m->timestamp_ms); + snprintf(timestamp_ms, sizeof(timestamp_ms), " %" PRIi64, + m->timestamp_ms); if (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE) - ssnprintf(line, sizeof(line), "%s{%s} " GAUGE_FORMAT "%s\n", fam->name, - format_labels(labels, sizeof(labels), m), m->gauge->value, - timestamp_ms); + snprintf(line, sizeof(line), "%s{%s} " GAUGE_FORMAT "%s\n", fam->name, + format_labels(labels, sizeof(labels), m), m->gauge->value, + timestamp_ms); else /* if (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__COUNTER) */ - ssnprintf(line, sizeof(line), "%s{%s} %.0f%s\n", fam->name, - format_labels(labels, sizeof(labels), m), m->counter->value, - timestamp_ms); + snprintf(line, sizeof(line), "%s{%s} %.0f%s\n", fam->name, + format_labels(labels, sizeof(labels), m), m->counter->value, + timestamp_ms); buffer->append(buffer, strlen(line), (uint8_t *)line); } @@@ -216,8 -216,8 +216,8 @@@ c_avl_iterator_destroy(iter); char server[1024]; - ssnprintf(server, sizeof(server), "\n# collectd/write_prometheus %s at %s\n", - PACKAGE_VERSION, hostname_g); + snprintf(server, sizeof(server), "\n# collectd/write_prometheus %s at %s\n", + PACKAGE_VERSION, hostname_g); buffer->append(buffer, strlen(server), (uint8_t *)server); pthread_mutex_unlock(&metrics_lock); @@@ -635,7 -635,7 +635,7 @@@ metric_family_create(char *name, data_s msg->name = name; char help[1024]; - ssnprintf( + snprintf( help, sizeof(help), "write_prometheus plugin: '%s' Type: '%s', Dstype: '%s', Dsname: '%s'", vl->plugin, vl->type, DS_TYPE_TO_STRING(ds->ds[ds_index].type), @@@ -760,7 -760,12 +760,12 @@@ static int prom_open_socket(int addrfam int fd = -1; for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) { - fd = socket(ai->ai_family, ai->ai_socktype | SOCK_CLOEXEC, 0); + int flags = ai->ai_socktype; + #ifdef SOCK_CLOEXEC + flags |= SOCK_CLOEXEC; + #endif + + fd = socket(ai->ai_family, flags, 0); if (fd == -1) continue; @@@ -804,13 -809,8 +809,13 @@@ static struct MHD_Daemon *prom_start_da return NULL; } + unsigned int flags = MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG; +#if MHD_VERSION >= 0x00095300 + flags |= MHD_USE_INTERNAL_POLLING_THREAD; +#endif + struct MHD_Daemon *d = MHD_start_daemon( - MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, httpd_port, + flags, httpd_port, /* MHD_AcceptPolicyCallback = */ NULL, /* MHD_AcceptPolicyCallback arg = */ NULL, http_handler, NULL, MHD_OPTION_LISTEN_SOCKET, fd, MHD_OPTION_EXTERNAL_LOGGER, prom_logger, @@@ -980,3 -980,5 +985,3 @@@ void module_register() /* user data = */ NULL); plugin_register_shutdown("write_prometheus", prom_shutdown); } - -/* vim: set sw=2 sts=2 et fdm=marker : */