2 * collectd - src/common.c
3 * Copyright (C) 2005-2010 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 *buf, int buf_len)
425 if (strcmp (buf, "/") == 0)
430 strncpy (buf, "root", buf_len);
437 /* Move one to the left */
439 memmove (buf, buf + 1, buf_len - 1);
441 for (i = 0; i < buf_len - 1; i++)
445 else if (buf[i] == '/')
451 } /* int escape_slashes */
453 void replace_special (char *buffer, size_t buffer_size)
457 for (i = 0; i < buffer_size; i++)
461 if ((!isalnum ((int) buffer[i])) && (buffer[i] != '-'))
464 } /* void replace_special */
466 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta)
468 struct timeval *larger;
469 struct timeval *smaller;
473 NORMALIZE_TIMEVAL (tv0);
474 NORMALIZE_TIMEVAL (tv1);
476 if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec))
485 if ((tv0.tv_sec < tv1.tv_sec)
486 || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec)))
500 delta->tv_sec = larger->tv_sec - smaller->tv_sec;
502 if (smaller->tv_usec <= larger->tv_usec)
503 delta->tv_usec = larger->tv_usec - smaller->tv_usec;
507 delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec;
511 assert ((delta == NULL)
512 || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000)));
515 } /* int timeval_cmp */
517 int check_create_dir (const char *file_orig)
528 int last_is_file = 1;
529 int path_is_absolute = 0;
534 * Sanity checks first
536 if (file_orig == NULL)
539 if ((len = strlen (file_orig)) < 1)
541 else if (len >= sizeof (file_copy))
545 * If `file_orig' ends in a slash the last component is a directory,
546 * otherwise it's a file. Act accordingly..
548 if (file_orig[len - 1] == '/')
550 if (file_orig[0] == '/')
551 path_is_absolute = 1;
554 * Create a copy for `strtok_r' to destroy
556 sstrncpy (file_copy, file_orig, sizeof (file_copy));
559 * Break into components. This will eat up several slashes in a row and
560 * remove leading and trailing slashes..
565 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
570 if (fields_num >= 16)
575 * For each component, do..
577 for (i = 0; i < (fields_num - last_is_file); i++)
580 * Do not create directories that start with a dot. This
581 * prevents `../../' attacks and other likely malicious
584 if (fields[i][0] == '.')
586 ERROR ("Cowardly refusing to create a directory that "
587 "begins with a `.' (dot): `%s'", file_orig);
592 * Join the components together again
595 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
596 fields, i + 1, "/") < 0)
598 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
603 if ((stat (dir, &statbuf) == -1)
604 && (lstat (dir, &statbuf) == -1))
608 if (mkdir (dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0)
611 /* this might happen, if a different thread created
612 * the directory in the meantime
613 * => call stat() again to check for S_ISDIR() */
618 ERROR ("check_create_dir: mkdir (%s): %s", dir,
620 errbuf, sizeof (errbuf)));
626 ERROR ("check_create_dir: stat (%s): %s", dir,
627 sstrerror (errno, errbuf,
632 else if (!S_ISDIR (statbuf.st_mode))
634 ERROR ("check_create_dir: `%s' exists but is not "
635 "a directory!", dir);
643 } /* check_create_dir */
646 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
655 ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
657 *ksp_ptr = kstat_lookup (kc, module, instance, name);
658 if (*ksp_ptr == NULL)
660 ERROR ("get_kstat: Cound not find kstat %s", ident);
664 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
666 ERROR ("get_kstat: kstat %s has wrong type", ident);
672 assert (*ksp_ptr != NULL);
673 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
676 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
678 ERROR ("get_kstat: kstat %s could not be read", ident);
682 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
684 ERROR ("get_kstat: kstat %s has wrong type", ident);
691 long long get_kstat_value (kstat_t *ksp, char *name)
694 long long retval = -1LL;
698 ERROR ("get_kstat_value (\"%s\"): ksp is NULL.", name);
701 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
703 ERROR ("get_kstat_value (\"%s\"): ksp->ks_type (%#x) "
704 "is not KSTAT_TYPE_NAMED (%#x).",
706 (unsigned int) ksp->ks_type,
707 (unsigned int) KSTAT_TYPE_NAMED);
711 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
714 if (kn->data_type == KSTAT_DATA_INT32)
715 retval = (long long) kn->value.i32;
716 else if (kn->data_type == KSTAT_DATA_UINT32)
717 retval = (long long) kn->value.ui32;
718 else if (kn->data_type == KSTAT_DATA_INT64)
719 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
720 else if (kn->data_type == KSTAT_DATA_UINT64)
721 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
723 WARNING ("get_kstat_value: Not a numeric value: %s", name);
727 #endif /* HAVE_LIBKSTAT */
730 unsigned long long ntohll (unsigned long long n)
732 #if BYTE_ORDER == BIG_ENDIAN
735 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
737 } /* unsigned long long ntohll */
739 unsigned long long htonll (unsigned long long n)
741 #if BYTE_ORDER == BIG_ENDIAN
744 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
746 } /* unsigned long long htonll */
747 #endif /* HAVE_HTONLL */
749 #if FP_LAYOUT_NEED_NOTHING
750 /* Well, we need nothing.. */
751 /* #endif FP_LAYOUT_NEED_NOTHING */
753 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
754 # if FP_LAYOUT_NEED_ENDIANFLIP
755 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
756 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
757 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
758 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
759 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
760 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
761 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
762 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
764 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
765 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
768 double ntohd (double d)
779 /* NAN in x86 byte order */
780 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
781 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
782 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
783 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
792 ret.integer = FP_CONVERT (tmp);
793 return (ret.floating);
797 double htond (double d)
808 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
809 ret.byte[4] = ret.byte[5] = 0x00;
812 return (ret.floating);
819 tmp = FP_CONVERT (ret.integer);
821 return (ret.floating);
824 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
826 int format_name (char *ret, int ret_len,
827 const char *hostname,
828 const char *plugin, const char *plugin_instance,
829 const char *type, const char *type_instance)
833 assert (plugin != NULL);
834 assert (type != NULL);
836 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
838 if ((type_instance == NULL) || (strlen (type_instance) == 0))
839 status = ssnprintf (ret, ret_len, "%s/%s/%s",
840 hostname, plugin, type);
842 status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
843 hostname, plugin, type,
848 if ((type_instance == NULL) || (strlen (type_instance) == 0))
849 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
850 hostname, plugin, plugin_instance,
853 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
854 hostname, plugin, plugin_instance,
855 type, type_instance);
858 if ((status < 1) || (status >= ret_len))
861 } /* int format_name */
863 int format_values (char *ret, size_t ret_len, /* {{{ */
864 const data_set_t *ds, const value_list_t *vl,
870 gauge_t *rates = NULL;
872 assert (0 == strcmp (ds->type, vl->type));
874 memset (ret, 0, ret_len);
876 #define BUFFER_ADD(...) do { \
877 status = ssnprintf (ret + offset, ret_len - offset, \
884 else if (((size_t) status) >= (ret_len - offset)) \
890 offset += ((size_t) status); \
893 BUFFER_ADD ("%.3f", CDTIME_T_TO_DOUBLE (vl->time));
895 for (i = 0; i < ds->ds_num; i++)
897 if (ds->ds[i].type == DS_TYPE_GAUGE)
898 BUFFER_ADD (":%f", vl->values[i].gauge);
899 else if (store_rates)
902 rates = uc_get_rate (ds, vl);
905 WARNING ("format_values: "
906 "uc_get_rate failed.");
909 BUFFER_ADD (":%g", rates[i]);
911 else if (ds->ds[i].type == DS_TYPE_COUNTER)
912 BUFFER_ADD (":%llu", vl->values[i].counter);
913 else if (ds->ds[i].type == DS_TYPE_DERIVE)
914 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
915 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
916 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
919 ERROR ("format_values plugin: Unknown data source type: %i",
924 } /* for ds->ds_num */
930 } /* }}} int format_values */
932 int parse_identifier (char *str, char **ret_host,
933 char **ret_plugin, char **ret_plugin_instance,
934 char **ret_type, char **ret_type_instance)
936 char *hostname = NULL;
938 char *plugin_instance = NULL;
940 char *type_instance = NULL;
943 if (hostname == NULL)
946 plugin = strchr (hostname, '/');
949 *plugin = '\0'; plugin++;
951 type = strchr (plugin, '/');
954 *type = '\0'; type++;
956 plugin_instance = strchr (plugin, '-');
957 if (plugin_instance != NULL)
959 *plugin_instance = '\0';
963 type_instance = strchr (type, '-');
964 if (type_instance != NULL)
966 *type_instance = '\0';
970 *ret_host = hostname;
971 *ret_plugin = plugin;
972 *ret_plugin_instance = plugin_instance;
974 *ret_type_instance = type_instance;
976 } /* int parse_identifier */
978 int parse_identifier_vl (const char *str, value_list_t *vl) /* {{{ */
980 char str_copy[6 * DATA_MAX_NAME_LEN];
983 char *plugin_instance = NULL;
985 char *type_instance = NULL;
988 if ((str == NULL) || (vl == NULL))
991 sstrncpy (str_copy, str, sizeof (str_copy));
993 status = parse_identifier (str_copy, &host,
994 &plugin, &plugin_instance,
995 &type, &type_instance);
999 sstrncpy (vl->host, host, sizeof (vl->host));
1000 sstrncpy (vl->plugin, plugin, sizeof (vl->plugin));
1001 sstrncpy (vl->plugin_instance,
1002 (plugin_instance != NULL) ? plugin_instance : "",
1003 sizeof (vl->plugin_instance));
1004 sstrncpy (vl->type, type, sizeof (vl->type));
1005 sstrncpy (vl->type_instance,
1006 (type_instance != NULL) ? type_instance : "",
1007 sizeof (vl->type_instance));
1010 } /* }}} int parse_identifier_vl */
1012 int parse_value (const char *value_orig, value_t *ret_value, int ds_type)
1015 char *endptr = NULL;
1018 if (value_orig == NULL)
1021 value = strdup (value_orig);
1024 value_len = strlen (value);
1026 while ((value_len > 0) && isspace ((int) value[value_len - 1]))
1028 value[value_len - 1] = 0;
1034 case DS_TYPE_COUNTER:
1035 ret_value->counter = (counter_t) strtoull (value, &endptr, 0);
1039 ret_value->gauge = (gauge_t) strtod (value, &endptr);
1042 case DS_TYPE_DERIVE:
1043 ret_value->derive = (derive_t) strtoll (value, &endptr, 0);
1046 case DS_TYPE_ABSOLUTE:
1047 ret_value->absolute = (absolute_t) strtoull (value, &endptr, 0);
1052 ERROR ("parse_value: Invalid data source type: %i.", ds_type);
1056 if (value == endptr) {
1058 ERROR ("parse_value: Failed to parse string as %s: %s.",
1059 DS_TYPE_TO_STRING (ds_type), value);
1062 else if ((NULL != endptr) && ('\0' != *endptr))
1063 INFO ("parse_value: Ignoring trailing garbage \"%s\" after %s value. "
1064 "Input string was \"%s\".",
1065 endptr, DS_TYPE_TO_STRING (ds_type), value_orig);
1069 } /* int parse_value */
1071 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
1081 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
1085 if (i >= vl->values_len)
1087 /* Make sure i is invalid. */
1088 i = vl->values_len + 1;
1094 if (strcmp ("N", ptr) == 0)
1095 vl->time = cdtime ();
1098 char *endptr = NULL;
1102 tmp = strtod (ptr, &endptr);
1103 if ((errno != 0) /* Overflow */
1104 || (endptr == ptr) /* Invalid string */
1105 || (endptr == NULL) /* This should not happen */
1106 || (*endptr != 0)) /* Trailing chars */
1109 vl->time = DOUBLE_TO_CDTIME_T (tmp);
1114 if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE))
1115 vl->values[i].gauge = NAN;
1116 else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i].type))
1121 } /* while (strtok_r) */
1123 if ((ptr != NULL) || (i != vl->values_len))
1126 } /* int parse_values */
1128 #if !HAVE_GETPWNAM_R
1129 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
1130 size_t buflen, struct passwd **pwbufp)
1135 memset (pwbuf, '\0', sizeof (struct passwd));
1137 pthread_mutex_lock (&getpwnam_r_lock);
1141 pw = getpwnam (name);
1144 status = (errno != 0) ? errno : ENOENT;
1148 #define GETPWNAM_COPY_MEMBER(member) \
1149 if (pw->member != NULL) \
1151 int len = strlen (pw->member); \
1152 if (len >= buflen) \
1157 sstrncpy (buf, pw->member, buflen); \
1158 pwbuf->member = buf; \
1160 buflen -= (len + 1); \
1162 GETPWNAM_COPY_MEMBER(pw_name);
1163 GETPWNAM_COPY_MEMBER(pw_passwd);
1164 GETPWNAM_COPY_MEMBER(pw_gecos);
1165 GETPWNAM_COPY_MEMBER(pw_dir);
1166 GETPWNAM_COPY_MEMBER(pw_shell);
1168 pwbuf->pw_uid = pw->pw_uid;
1169 pwbuf->pw_gid = pw->pw_gid;
1175 pthread_mutex_unlock (&getpwnam_r_lock);
1178 } /* int getpwnam_r */
1179 #endif /* !HAVE_GETPWNAM_R */
1181 int notification_init (notification_t *n, int severity, const char *message,
1183 const char *plugin, const char *plugin_instance,
1184 const char *type, const char *type_instance)
1186 memset (n, '\0', sizeof (notification_t));
1188 n->severity = severity;
1190 if (message != NULL)
1191 sstrncpy (n->message, message, sizeof (n->message));
1193 sstrncpy (n->host, host, sizeof (n->host));
1195 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
1196 if (plugin_instance != NULL)
1197 sstrncpy (n->plugin_instance, plugin_instance,
1198 sizeof (n->plugin_instance));
1200 sstrncpy (n->type, type, sizeof (n->type));
1201 if (type_instance != NULL)
1202 sstrncpy (n->type_instance, type_instance,
1203 sizeof (n->type_instance));
1206 } /* int notification_init */
1208 int walk_directory (const char *dir, dirwalk_callback_f callback,
1209 void *user_data, int include_hidden)
1219 if ((dh = opendir (dir)) == NULL)
1222 ERROR ("walk_directory: Cannot open '%s': %s", dir,
1223 sstrerror (errno, errbuf, sizeof (errbuf)));
1227 while ((ent = readdir (dh)) != NULL)
1233 if ((strcmp (".", ent->d_name) == 0)
1234 || (strcmp ("..", ent->d_name) == 0))
1237 else /* if (!include_hidden) */
1239 if (ent->d_name[0]=='.')
1243 status = (*callback) (dir, ent->d_name, user_data);
1252 if ((success == 0) && (failure > 0))
1257 ssize_t read_file_contents (const char *filename, char *buf, size_t bufsize)
1262 fh = fopen (filename, "r");
1266 ret = (ssize_t) fread (buf, 1, bufsize, fh);
1267 if ((ret == 0) && (ferror (fh) != 0))
1269 ERROR ("read_file_contents: Reading file \"%s\" failed.",
1278 counter_t counter_diff (counter_t old_value, counter_t new_value)
1282 if (old_value > new_value)
1284 if (old_value <= 4294967295U)
1285 diff = (4294967295U - old_value) + new_value;
1287 diff = (18446744073709551615ULL - old_value)
1292 diff = new_value - old_value;
1296 } /* counter_t counter_diff */
1298 int rate_to_value (value_t *ret_value, gauge_t rate, /* {{{ */
1299 rate_to_value_state_t *state,
1300 int ds_type, cdtime_t t)
1302 gauge_t delta_gauge;
1305 if (ds_type == DS_TYPE_GAUGE)
1307 state->last_value.gauge = rate;
1308 state->last_time = t;
1310 *ret_value = state->last_value;
1314 /* Counter and absolute can't handle negative rates. Reset "last time"
1315 * to zero, so that the next valid rate will re-initialize the
1318 && ((ds_type == DS_TYPE_COUNTER)
1319 || (ds_type == DS_TYPE_ABSOLUTE)))
1321 memset (state, 0, sizeof (*state));
1325 /* Another invalid state: The time is not increasing. */
1326 if (t <= state->last_time)
1328 memset (state, 0, sizeof (*state));
1332 delta_t = t - state->last_time;
1333 delta_gauge = (rate * CDTIME_T_TO_DOUBLE (delta_t)) + state->residual;
1335 /* Previous value is invalid. */
1336 if (state->last_time == 0) /* {{{ */
1338 if (ds_type == DS_TYPE_DERIVE)
1340 state->last_value.derive = (derive_t) rate;
1341 state->residual = rate - ((gauge_t) state->last_value.derive);
1343 else if (ds_type == DS_TYPE_COUNTER)
1345 state->last_value.counter = (counter_t) rate;
1346 state->residual = rate - ((gauge_t) state->last_value.counter);
1348 else if (ds_type == DS_TYPE_ABSOLUTE)
1350 state->last_value.absolute = (absolute_t) rate;
1351 state->residual = rate - ((gauge_t) state->last_value.absolute);
1358 state->last_time = t;
1362 if (ds_type == DS_TYPE_DERIVE)
1364 derive_t delta_derive = (derive_t) delta_gauge;
1366 state->last_value.derive += delta_derive;
1367 state->residual = delta_gauge - ((gauge_t) delta_derive);
1369 else if (ds_type == DS_TYPE_COUNTER)
1371 counter_t delta_counter = (counter_t) delta_gauge;
1373 state->last_value.counter += delta_counter;
1374 state->residual = delta_gauge - ((gauge_t) delta_counter);
1376 else if (ds_type == DS_TYPE_ABSOLUTE)
1378 absolute_t delta_absolute = (absolute_t) delta_gauge;
1380 state->last_value.absolute = delta_absolute;
1381 state->residual = delta_gauge - ((gauge_t) delta_absolute);
1388 state->last_time = t;
1389 *ret_value = state->last_value;
1391 } /* }}} value_t rate_to_value */
1393 int service_name_to_port_number (const char *service_name)
1395 struct addrinfo *ai_list;
1396 struct addrinfo *ai_ptr;
1397 struct addrinfo ai_hints;
1401 if (service_name == NULL)
1405 memset (&ai_hints, 0, sizeof (ai_hints));
1406 ai_hints.ai_family = AF_UNSPEC;
1408 status = getaddrinfo (/* node = */ NULL, service_name,
1409 &ai_hints, &ai_list);
1412 ERROR ("service_name_to_port_number: getaddrinfo failed: %s",
1413 gai_strerror (status));
1417 service_number = -1;
1418 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1420 if (ai_ptr->ai_family == AF_INET)
1422 struct sockaddr_in *sa;
1424 sa = (void *) ai_ptr->ai_addr;
1425 service_number = (int) ntohs (sa->sin_port);
1427 else if (ai_ptr->ai_family == AF_INET6)
1429 struct sockaddr_in6 *sa;
1431 sa = (void *) ai_ptr->ai_addr;
1432 service_number = (int) ntohs (sa->sin6_port);
1435 if ((service_number > 0) && (service_number <= 65535))
1439 freeaddrinfo (ai_list);
1441 if ((service_number > 0) && (service_number <= 65535))
1442 return (service_number);
1444 } /* int service_name_to_port_number */
1446 int strtoderive (const char *string, derive_t *ret_value) /* {{{ */
1451 if ((string == NULL) || (ret_value == NULL))
1456 tmp = (derive_t) strtoll (string, &endptr, /* base = */ 0);
1457 if ((endptr == string) || (errno != 0))
1462 } /* }}} int strtoderive */
1464 int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str) /* {{{ */
1467 size_t array_len = *ret_array_len;
1472 array = realloc (*ret_array,
1473 (array_len + 1) * sizeof (*array));
1478 array[array_len] = strdup (str);
1479 if (array[array_len] == NULL)
1483 *ret_array_len = array_len;
1485 } /* }}} int strarray_add */
1487 void strarray_free (char **array, size_t array_len) /* {{{ */
1491 for (i = 0; i < array_len; i++)
1494 } /* }}} void strarray_free */