2 * collectd - src/common.c
3 * Copyright (C) 2005-2014 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
20 * Niki W. Waibel <niki.waibel@gmx.net>
21 * Sebastian Harl <sh at tokkee.org>
22 * Michał Mirosław <mirq-linux at rere.qmqm.pl>
32 #include "utils_cache.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
48 # include <netinet/in.h>
51 /* for ntohl and htonl */
53 # include <arpa/inet.h>
57 extern kstat_ctl_t *kc;
61 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
65 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
68 char *sstrncpy (char *dest, const char *src, size_t n)
70 strncpy (dest, src, n);
74 } /* char *sstrncpy */
76 int ssnprintf (char *dest, size_t n, const char *format, ...)
81 va_start (ap, format);
82 ret = vsnprintf (dest, n, format, ap);
89 char *ssnprintf_alloc (char const *format, ...) /* {{{ */
91 char static_buffer[1024] = "";
93 size_t alloc_buffer_size;
97 /* Try printing into the static buffer. In many cases it will be
98 * sufficiently large and we can simply return a strdup() of this
100 va_start (ap, format);
101 status = vsnprintf (static_buffer, sizeof (static_buffer), format, ap);
106 /* "status" does not include the null byte. */
107 alloc_buffer_size = (size_t) (status + 1);
108 if (alloc_buffer_size <= sizeof (static_buffer))
109 return (strdup (static_buffer));
111 /* Allocate a buffer large enough to hold the string. */
112 alloc_buffer = malloc (alloc_buffer_size);
113 if (alloc_buffer == NULL)
115 memset (alloc_buffer, 0, alloc_buffer_size);
117 /* Print again into this new buffer. */
118 va_start (ap, format);
119 status = vsnprintf (alloc_buffer, alloc_buffer_size, format, ap);
123 sfree (alloc_buffer);
127 return (alloc_buffer);
128 } /* }}} char *ssnprintf_alloc */
130 char *sstrdup (const char *s)
138 /* Do not use `strdup' here, because it's not specified in POSIX. It's
139 * ``only'' an XSI extension. */
141 r = (char *) malloc (sizeof (char) * sz);
144 ERROR ("sstrdup: Out of memory.");
147 memcpy (r, s, sizeof (char) * sz);
150 } /* char *sstrdup */
152 /* Even though Posix requires "strerror_r" to return an "int",
153 * some systems (e.g. the GNU libc) return a "char *" _and_
154 * ignore the second argument ... -tokkee */
155 char *sstrerror (int errnum, char *buf, size_t buflen)
163 pthread_mutex_lock (&strerror_r_lock);
165 temp = strerror (errnum);
166 sstrncpy (buf, temp, buflen);
168 pthread_mutex_unlock (&strerror_r_lock);
170 /* #endif !HAVE_STRERROR_R */
172 #elif STRERROR_R_CHAR_P
175 temp = strerror_r (errnum, buf, buflen);
178 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
179 sstrncpy (buf, temp, buflen);
181 sstrncpy (buf, "strerror_r did not return "
182 "an error message", buflen);
185 /* #endif STRERROR_R_CHAR_P */
188 if (strerror_r (errnum, buf, buflen) != 0)
190 ssnprintf (buf, buflen, "Error #%i; "
191 "Additionally, strerror_r failed.",
194 #endif /* STRERROR_R_CHAR_P */
197 } /* char *sstrerror */
199 void *smalloc (size_t size)
203 if ((r = malloc (size)) == NULL)
205 ERROR ("Not enough memory.");
210 } /* void *smalloc */
213 void sfree (void **ptr)
225 ssize_t sread (int fd, void *buf, size_t count)
236 status = read (fd, (void *) ptr, nleft);
238 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
246 DEBUG ("Received EOF from fd %i. "
247 "Closing fd and returning error.",
253 assert ((0 > status) || (nleft >= (size_t)status));
255 nleft = nleft - status;
263 ssize_t swrite (int fd, const void *buf, size_t count)
269 ptr = (const char *) buf;
274 status = write (fd, (const void *) ptr, nleft);
276 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
282 nleft = nleft - status;
289 int strsplit (char *string, char **fields, size_t size)
298 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
310 int strjoin (char *dst, size_t dst_len,
311 char **fields, size_t fields_num,
318 memset (dst, '\0', dst_len);
325 sep_len = strlen (sep);
327 for (i = 0; i < (int)fields_num; i++)
329 if ((i > 0) && (sep_len > 0))
331 if (dst_len <= sep_len)
334 strncat (dst, sep, dst_len);
338 field_len = strlen (fields[i]);
340 if (dst_len <= field_len)
343 strncat (dst, fields[i], dst_len);
344 dst_len -= field_len;
347 return (strlen (dst));
350 int strsubstitute (char *str, char c_from, char c_to)
369 } /* int strsubstitute */
371 int strunescape (char *buf, size_t buf_len)
375 for (i = 0; (i < buf_len) && (buf[i] != '\0'); ++i)
380 if ((i >= buf_len) || (buf[i + 1] == '\0')) {
381 ERROR ("string unescape: backslash found at end of string.");
385 switch (buf[i + 1]) {
400 memmove (buf + i + 1, buf + i + 2, buf_len - i - 2);
403 } /* int strunescape */
405 size_t strstripnewline (char *buffer)
407 size_t buffer_len = strlen (buffer);
409 while (buffer_len > 0)
411 if ((buffer[buffer_len - 1] != '\n')
412 && (buffer[buffer_len - 1] != '\r'))
414 buffer[buffer_len] = 0;
419 } /* size_t strstripnewline */
421 int escape_slashes (char *buffer, size_t buffer_size)
426 buffer_len = strlen (buffer);
430 if (strcmp ("/", buffer) == 0)
434 sstrncpy (buffer, "root", buffer_size);
439 /* Move one to the left */
440 if (buffer[0] == '/')
442 memmove (buffer, buffer + 1, buffer_len);
446 for (i = 0; i < buffer_len - 1; i++)
448 if (buffer[i] == '/')
453 } /* int escape_slashes */
455 void replace_special (char *buffer, size_t buffer_size)
459 for (i = 0; i < buffer_size; i++)
463 if ((!isalnum ((int) buffer[i])) && (buffer[i] != '-'))
466 } /* void replace_special */
468 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta)
470 struct timeval *larger;
471 struct timeval *smaller;
475 NORMALIZE_TIMEVAL (tv0);
476 NORMALIZE_TIMEVAL (tv1);
478 if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec))
487 if ((tv0.tv_sec < tv1.tv_sec)
488 || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec)))
502 delta->tv_sec = larger->tv_sec - smaller->tv_sec;
504 if (smaller->tv_usec <= larger->tv_usec)
505 delta->tv_usec = larger->tv_usec - smaller->tv_usec;
509 delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec;
513 assert ((delta == NULL)
514 || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000)));
517 } /* int timeval_cmp */
519 int check_create_dir (const char *file_orig)
530 int last_is_file = 1;
531 int path_is_absolute = 0;
536 * Sanity checks first
538 if (file_orig == NULL)
541 if ((len = strlen (file_orig)) < 1)
543 else if (len >= sizeof (file_copy))
547 * If `file_orig' ends in a slash the last component is a directory,
548 * otherwise it's a file. Act accordingly..
550 if (file_orig[len - 1] == '/')
552 if (file_orig[0] == '/')
553 path_is_absolute = 1;
556 * Create a copy for `strtok_r' to destroy
558 sstrncpy (file_copy, file_orig, sizeof (file_copy));
561 * Break into components. This will eat up several slashes in a row and
562 * remove leading and trailing slashes..
567 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
572 if (fields_num >= 16)
577 * For each component, do..
579 for (i = 0; i < (fields_num - last_is_file); i++)
582 * Do not create directories that start with a dot. This
583 * prevents `../../' attacks and other likely malicious
586 if (fields[i][0] == '.')
588 ERROR ("Cowardly refusing to create a directory that "
589 "begins with a `.' (dot): `%s'", file_orig);
594 * Join the components together again
597 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
598 fields, i + 1, "/") < 0)
600 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
605 if ((stat (dir, &statbuf) == -1)
606 && (lstat (dir, &statbuf) == -1))
610 if (mkdir (dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0)
613 /* this might happen, if a different thread created
614 * the directory in the meantime
615 * => call stat() again to check for S_ISDIR() */
620 ERROR ("check_create_dir: mkdir (%s): %s", dir,
622 errbuf, sizeof (errbuf)));
628 ERROR ("check_create_dir: stat (%s): %s", dir,
629 sstrerror (errno, errbuf,
634 else if (!S_ISDIR (statbuf.st_mode))
636 ERROR ("check_create_dir: `%s' exists but is not "
637 "a directory!", dir);
645 } /* check_create_dir */
648 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
657 ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
659 *ksp_ptr = kstat_lookup (kc, module, instance, name);
660 if (*ksp_ptr == NULL)
662 ERROR ("get_kstat: Cound not find kstat %s", ident);
666 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
668 ERROR ("get_kstat: kstat %s has wrong type", ident);
674 assert (*ksp_ptr != NULL);
675 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
678 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
680 ERROR ("get_kstat: kstat %s could not be read", ident);
684 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
686 ERROR ("get_kstat: kstat %s has wrong type", ident);
693 long long get_kstat_value (kstat_t *ksp, char *name)
696 long long retval = -1LL;
700 ERROR ("get_kstat_value (\"%s\"): ksp is NULL.", name);
703 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
705 ERROR ("get_kstat_value (\"%s\"): ksp->ks_type (%#x) "
706 "is not KSTAT_TYPE_NAMED (%#x).",
708 (unsigned int) ksp->ks_type,
709 (unsigned int) KSTAT_TYPE_NAMED);
713 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
716 if (kn->data_type == KSTAT_DATA_INT32)
717 retval = (long long) kn->value.i32;
718 else if (kn->data_type == KSTAT_DATA_UINT32)
719 retval = (long long) kn->value.ui32;
720 else if (kn->data_type == KSTAT_DATA_INT64)
721 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
722 else if (kn->data_type == KSTAT_DATA_UINT64)
723 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
725 WARNING ("get_kstat_value: Not a numeric value: %s", name);
729 #endif /* HAVE_LIBKSTAT */
732 unsigned long long ntohll (unsigned long long n)
734 #if BYTE_ORDER == BIG_ENDIAN
737 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
739 } /* unsigned long long ntohll */
741 unsigned long long htonll (unsigned long long n)
743 #if BYTE_ORDER == BIG_ENDIAN
746 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
748 } /* unsigned long long htonll */
749 #endif /* HAVE_HTONLL */
751 #if FP_LAYOUT_NEED_NOTHING
752 /* Well, we need nothing.. */
753 /* #endif FP_LAYOUT_NEED_NOTHING */
755 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
756 # if FP_LAYOUT_NEED_ENDIANFLIP
757 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
758 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
759 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
760 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
761 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
762 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
763 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
764 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
766 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
767 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
770 double ntohd (double d)
781 /* NAN in x86 byte order */
782 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
783 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
784 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
785 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
794 ret.integer = FP_CONVERT (tmp);
795 return (ret.floating);
799 double htond (double d)
810 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
811 ret.byte[4] = ret.byte[5] = 0x00;
814 return (ret.floating);
821 tmp = FP_CONVERT (ret.integer);
823 return (ret.floating);
826 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
828 int format_name (char *ret, int ret_len,
829 const char *hostname,
830 const char *plugin, const char *plugin_instance,
831 const char *type, const char *type_instance)
837 buffer_size = (size_t) ret_len;
839 #define APPEND(str) do { \
840 size_t l = strlen (str); \
841 if (l >= buffer_size) \
843 memcpy (buffer, (str), l); \
844 buffer += l; buffer_size -= l; \
847 assert (plugin != NULL);
848 assert (type != NULL);
853 if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
856 APPEND (plugin_instance);
860 if ((type_instance != NULL) && (type_instance[0] != 0))
863 APPEND (type_instance);
865 assert (buffer_size > 0);
870 } /* int format_name */
872 int format_values (char *ret, size_t ret_len, /* {{{ */
873 const data_set_t *ds, const value_list_t *vl,
879 gauge_t *rates = NULL;
881 assert (0 == strcmp (ds->type, vl->type));
883 memset (ret, 0, ret_len);
885 #define BUFFER_ADD(...) do { \
886 status = ssnprintf (ret + offset, ret_len - offset, \
893 else if (((size_t) status) >= (ret_len - offset)) \
899 offset += ((size_t) status); \
902 BUFFER_ADD ("%.3f", CDTIME_T_TO_DOUBLE (vl->time));
904 for (i = 0; i < ds->ds_num; i++)
906 if (ds->ds[i].type == DS_TYPE_GAUGE)
907 BUFFER_ADD (":%f", vl->values[i].gauge);
908 else if (store_rates)
911 rates = uc_get_rate (ds, vl);
914 WARNING ("format_values: "
915 "uc_get_rate failed.");
918 BUFFER_ADD (":%g", rates[i]);
920 else if (ds->ds[i].type == DS_TYPE_COUNTER)
921 BUFFER_ADD (":%llu", vl->values[i].counter);
922 else if (ds->ds[i].type == DS_TYPE_DERIVE)
923 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
924 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
925 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
928 ERROR ("format_values plugin: Unknown data source type: %i",
933 } /* for ds->ds_num */
939 } /* }}} int format_values */
941 int parse_identifier (char *str, char **ret_host,
942 char **ret_plugin, char **ret_plugin_instance,
943 char **ret_type, char **ret_type_instance)
945 char *hostname = NULL;
947 char *plugin_instance = NULL;
949 char *type_instance = NULL;
952 if (hostname == NULL)
955 plugin = strchr (hostname, '/');
958 *plugin = '\0'; plugin++;
960 type = strchr (plugin, '/');
963 *type = '\0'; type++;
965 plugin_instance = strchr (plugin, '-');
966 if (plugin_instance != NULL)
968 *plugin_instance = '\0';
972 type_instance = strchr (type, '-');
973 if (type_instance != NULL)
975 *type_instance = '\0';
979 *ret_host = hostname;
980 *ret_plugin = plugin;
981 *ret_plugin_instance = plugin_instance;
983 *ret_type_instance = type_instance;
985 } /* int parse_identifier */
987 int parse_identifier_vl (const char *str, value_list_t *vl) /* {{{ */
989 char str_copy[6 * DATA_MAX_NAME_LEN];
992 char *plugin_instance = NULL;
994 char *type_instance = NULL;
997 if ((str == NULL) || (vl == NULL))
1000 sstrncpy (str_copy, str, sizeof (str_copy));
1002 status = parse_identifier (str_copy, &host,
1003 &plugin, &plugin_instance,
1004 &type, &type_instance);
1008 sstrncpy (vl->host, host, sizeof (vl->host));
1009 sstrncpy (vl->plugin, plugin, sizeof (vl->plugin));
1010 sstrncpy (vl->plugin_instance,
1011 (plugin_instance != NULL) ? plugin_instance : "",
1012 sizeof (vl->plugin_instance));
1013 sstrncpy (vl->type, type, sizeof (vl->type));
1014 sstrncpy (vl->type_instance,
1015 (type_instance != NULL) ? type_instance : "",
1016 sizeof (vl->type_instance));
1019 } /* }}} int parse_identifier_vl */
1021 int parse_value (const char *value_orig, value_t *ret_value, int ds_type)
1024 char *endptr = NULL;
1027 if (value_orig == NULL)
1030 value = strdup (value_orig);
1033 value_len = strlen (value);
1035 while ((value_len > 0) && isspace ((int) value[value_len - 1]))
1037 value[value_len - 1] = 0;
1043 case DS_TYPE_COUNTER:
1044 ret_value->counter = (counter_t) strtoull (value, &endptr, 0);
1048 ret_value->gauge = (gauge_t) strtod (value, &endptr);
1051 case DS_TYPE_DERIVE:
1052 ret_value->derive = (derive_t) strtoll (value, &endptr, 0);
1055 case DS_TYPE_ABSOLUTE:
1056 ret_value->absolute = (absolute_t) strtoull (value, &endptr, 0);
1061 ERROR ("parse_value: Invalid data source type: %i.", ds_type);
1065 if (value == endptr) {
1067 ERROR ("parse_value: Failed to parse string as %s: %s.",
1068 DS_TYPE_TO_STRING (ds_type), value);
1071 else if ((NULL != endptr) && ('\0' != *endptr))
1072 INFO ("parse_value: Ignoring trailing garbage \"%s\" after %s value. "
1073 "Input string was \"%s\".",
1074 endptr, DS_TYPE_TO_STRING (ds_type), value_orig);
1078 } /* int parse_value */
1080 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
1090 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
1094 if (i >= vl->values_len)
1096 /* Make sure i is invalid. */
1097 i = vl->values_len + 1;
1103 if (strcmp ("N", ptr) == 0)
1104 vl->time = cdtime ();
1107 char *endptr = NULL;
1111 tmp = strtod (ptr, &endptr);
1112 if ((errno != 0) /* Overflow */
1113 || (endptr == ptr) /* Invalid string */
1114 || (endptr == NULL) /* This should not happen */
1115 || (*endptr != 0)) /* Trailing chars */
1118 vl->time = DOUBLE_TO_CDTIME_T (tmp);
1123 if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE))
1124 vl->values[i].gauge = NAN;
1125 else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i].type))
1130 } /* while (strtok_r) */
1132 if ((ptr != NULL) || (i != vl->values_len))
1135 } /* int parse_values */
1137 #if !HAVE_GETPWNAM_R
1138 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
1139 size_t buflen, struct passwd **pwbufp)
1144 memset (pwbuf, '\0', sizeof (struct passwd));
1146 pthread_mutex_lock (&getpwnam_r_lock);
1150 pw = getpwnam (name);
1153 status = (errno != 0) ? errno : ENOENT;
1157 #define GETPWNAM_COPY_MEMBER(member) \
1158 if (pw->member != NULL) \
1160 int len = strlen (pw->member); \
1161 if (len >= buflen) \
1166 sstrncpy (buf, pw->member, buflen); \
1167 pwbuf->member = buf; \
1169 buflen -= (len + 1); \
1171 GETPWNAM_COPY_MEMBER(pw_name);
1172 GETPWNAM_COPY_MEMBER(pw_passwd);
1173 GETPWNAM_COPY_MEMBER(pw_gecos);
1174 GETPWNAM_COPY_MEMBER(pw_dir);
1175 GETPWNAM_COPY_MEMBER(pw_shell);
1177 pwbuf->pw_uid = pw->pw_uid;
1178 pwbuf->pw_gid = pw->pw_gid;
1184 pthread_mutex_unlock (&getpwnam_r_lock);
1187 } /* int getpwnam_r */
1188 #endif /* !HAVE_GETPWNAM_R */
1190 int notification_init (notification_t *n, int severity, const char *message,
1192 const char *plugin, const char *plugin_instance,
1193 const char *type, const char *type_instance)
1195 memset (n, '\0', sizeof (notification_t));
1197 n->severity = severity;
1199 if (message != NULL)
1200 sstrncpy (n->message, message, sizeof (n->message));
1202 sstrncpy (n->host, host, sizeof (n->host));
1204 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
1205 if (plugin_instance != NULL)
1206 sstrncpy (n->plugin_instance, plugin_instance,
1207 sizeof (n->plugin_instance));
1209 sstrncpy (n->type, type, sizeof (n->type));
1210 if (type_instance != NULL)
1211 sstrncpy (n->type_instance, type_instance,
1212 sizeof (n->type_instance));
1215 } /* int notification_init */
1217 int walk_directory (const char *dir, dirwalk_callback_f callback,
1218 void *user_data, int include_hidden)
1228 if ((dh = opendir (dir)) == NULL)
1231 ERROR ("walk_directory: Cannot open '%s': %s", dir,
1232 sstrerror (errno, errbuf, sizeof (errbuf)));
1236 while ((ent = readdir (dh)) != NULL)
1242 if ((strcmp (".", ent->d_name) == 0)
1243 || (strcmp ("..", ent->d_name) == 0))
1246 else /* if (!include_hidden) */
1248 if (ent->d_name[0]=='.')
1252 status = (*callback) (dir, ent->d_name, user_data);
1261 if ((success == 0) && (failure > 0))
1266 ssize_t read_file_contents (const char *filename, char *buf, size_t bufsize)
1271 fh = fopen (filename, "r");
1275 ret = (ssize_t) fread (buf, 1, bufsize, fh);
1276 if ((ret == 0) && (ferror (fh) != 0))
1278 ERROR ("read_file_contents: Reading file \"%s\" failed.",
1287 counter_t counter_diff (counter_t old_value, counter_t new_value)
1291 if (old_value > new_value)
1293 if (old_value <= 4294967295U)
1294 diff = (4294967295U - old_value) + new_value;
1296 diff = (18446744073709551615ULL - old_value)
1301 diff = new_value - old_value;
1305 } /* counter_t counter_diff */
1307 int rate_to_value (value_t *ret_value, gauge_t rate, /* {{{ */
1308 rate_to_value_state_t *state,
1309 int ds_type, cdtime_t t)
1311 gauge_t delta_gauge;
1314 if (ds_type == DS_TYPE_GAUGE)
1316 state->last_value.gauge = rate;
1317 state->last_time = t;
1319 *ret_value = state->last_value;
1323 /* Counter and absolute can't handle negative rates. Reset "last time"
1324 * to zero, so that the next valid rate will re-initialize the
1327 && ((ds_type == DS_TYPE_COUNTER)
1328 || (ds_type == DS_TYPE_ABSOLUTE)))
1330 memset (state, 0, sizeof (*state));
1334 /* Another invalid state: The time is not increasing. */
1335 if (t <= state->last_time)
1337 memset (state, 0, sizeof (*state));
1341 delta_t = t - state->last_time;
1342 delta_gauge = (rate * CDTIME_T_TO_DOUBLE (delta_t)) + state->residual;
1344 /* Previous value is invalid. */
1345 if (state->last_time == 0) /* {{{ */
1347 if (ds_type == DS_TYPE_DERIVE)
1349 state->last_value.derive = (derive_t) rate;
1350 state->residual = rate - ((gauge_t) state->last_value.derive);
1352 else if (ds_type == DS_TYPE_COUNTER)
1354 state->last_value.counter = (counter_t) rate;
1355 state->residual = rate - ((gauge_t) state->last_value.counter);
1357 else if (ds_type == DS_TYPE_ABSOLUTE)
1359 state->last_value.absolute = (absolute_t) rate;
1360 state->residual = rate - ((gauge_t) state->last_value.absolute);
1367 state->last_time = t;
1371 if (ds_type == DS_TYPE_DERIVE)
1373 derive_t delta_derive = (derive_t) delta_gauge;
1375 state->last_value.derive += delta_derive;
1376 state->residual = delta_gauge - ((gauge_t) delta_derive);
1378 else if (ds_type == DS_TYPE_COUNTER)
1380 counter_t delta_counter = (counter_t) delta_gauge;
1382 state->last_value.counter += delta_counter;
1383 state->residual = delta_gauge - ((gauge_t) delta_counter);
1385 else if (ds_type == DS_TYPE_ABSOLUTE)
1387 absolute_t delta_absolute = (absolute_t) delta_gauge;
1389 state->last_value.absolute = delta_absolute;
1390 state->residual = delta_gauge - ((gauge_t) delta_absolute);
1397 state->last_time = t;
1398 *ret_value = state->last_value;
1400 } /* }}} value_t rate_to_value */
1402 int value_to_rate (value_t *ret_rate, derive_t value, /* {{{ */
1403 value_to_rate_state_t *state,
1404 int ds_type, cdtime_t t)
1408 /* Another invalid state: The time is not increasing. */
1409 if (t <= state->last_time)
1411 memset (state, 0, sizeof (*state));
1415 interval = CDTIME_T_TO_DOUBLE(t - state->last_time);
1417 /* Previous value is invalid. */
1418 if (state->last_time == 0) /* {{{ */
1420 if (ds_type == DS_TYPE_DERIVE)
1422 state->last_value.derive = value;
1424 else if (ds_type == DS_TYPE_COUNTER)
1426 state->last_value.counter = (counter_t) value;
1428 else if (ds_type == DS_TYPE_ABSOLUTE)
1430 state->last_value.absolute = (absolute_t) value;
1437 state->last_time = t;
1441 if (ds_type == DS_TYPE_DERIVE)
1443 ret_rate->gauge = (value - state->last_value.derive) / interval;
1444 state->last_value.derive = value;
1446 else if (ds_type == DS_TYPE_COUNTER)
1448 ret_rate->gauge = (((counter_t)value) - state->last_value.counter) / interval;
1449 state->last_value.counter = (counter_t) value;
1451 else if (ds_type == DS_TYPE_ABSOLUTE)
1453 ret_rate->gauge = (((absolute_t)value) - state->last_value.absolute) / interval;
1454 state->last_value.absolute = (absolute_t) value;
1461 state->last_time = t;
1463 } /* }}} value_t rate_to_value */
1465 int service_name_to_port_number (const char *service_name)
1467 struct addrinfo *ai_list;
1468 struct addrinfo *ai_ptr;
1469 struct addrinfo ai_hints;
1473 if (service_name == NULL)
1477 memset (&ai_hints, 0, sizeof (ai_hints));
1478 ai_hints.ai_family = AF_UNSPEC;
1480 status = getaddrinfo (/* node = */ NULL, service_name,
1481 &ai_hints, &ai_list);
1484 ERROR ("service_name_to_port_number: getaddrinfo failed: %s",
1485 gai_strerror (status));
1489 service_number = -1;
1490 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1492 if (ai_ptr->ai_family == AF_INET)
1494 struct sockaddr_in *sa;
1496 sa = (void *) ai_ptr->ai_addr;
1497 service_number = (int) ntohs (sa->sin_port);
1499 else if (ai_ptr->ai_family == AF_INET6)
1501 struct sockaddr_in6 *sa;
1503 sa = (void *) ai_ptr->ai_addr;
1504 service_number = (int) ntohs (sa->sin6_port);
1507 if ((service_number > 0) && (service_number <= 65535))
1511 freeaddrinfo (ai_list);
1513 if ((service_number > 0) && (service_number <= 65535))
1514 return (service_number);
1516 } /* int service_name_to_port_number */
1518 int strtoderive (const char *string, derive_t *ret_value) /* {{{ */
1523 if ((string == NULL) || (ret_value == NULL))
1528 tmp = (derive_t) strtoll (string, &endptr, /* base = */ 0);
1529 if ((endptr == string) || (errno != 0))
1534 } /* }}} int strtoderive */
1536 int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str) /* {{{ */
1539 size_t array_len = *ret_array_len;
1544 array = realloc (*ret_array,
1545 (array_len + 1) * sizeof (*array));
1550 array[array_len] = strdup (str);
1551 if (array[array_len] == NULL)
1555 *ret_array_len = array_len;
1557 } /* }}} int strarray_add */
1559 void strarray_free (char **array, size_t array_len) /* {{{ */
1563 for (i = 0; i < array_len; i++)
1566 } /* }}} void strarray_free */