2 * collectd - src/common.c
3 * Copyright (C) 2005-2007 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 verplant.org>
20 * Niki W. Waibel <niki.waibel@gmx.net>
39 /* for ntohl and htonl */
41 # include <arpa/inet.h>
45 extern kstat_ctl_t *kc;
49 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
53 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
56 void sstrncpy (char *d, const char *s, int len)
62 char *sstrdup (const char *s)
69 if((r = strdup (s)) == NULL)
71 DEBUG ("Not enough memory.");
78 /* Even though Posix requires "strerror_r" to return an "int",
79 * some systems (e.g. the GNU libc) return a "char *" _and_
80 * ignore the second argument ... -tokkee */
81 char *sstrerror (int errnum, char *buf, size_t buflen)
89 pthread_mutex_lock (&strerror_r_lock);
91 temp = strerror (errnum);
92 strncpy (buf, temp, buflen);
94 pthread_mutex_unlock (&strerror_r_lock);
96 /* #endif !HAVE_STRERROR_R */
98 #elif STRERROR_R_CHAR_P
101 temp = strerror_r (errnum, buf, buflen);
104 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
105 strncpy (buf, temp, buflen);
107 strncpy (buf, "strerror_r did not return "
108 "an error message", buflen);
111 /* #endif STRERROR_R_CHAR_P */
114 if (strerror_r (errnum, buf, buflen) != 0)
116 snprintf (buf, buflen, "Error #%i; "
117 "Additionally, strerror_r failed.",
120 #endif /* STRERROR_R_CHAR_P */
122 buf[buflen - 1] = '\0';
124 } /* char *sstrerror */
126 void *smalloc (size_t size)
130 if ((r = malloc (size)) == NULL)
132 DEBUG("Not enough memory.");
140 void sfree (void **ptr)
152 ssize_t sread (int fd, void *buf, size_t count)
163 status = read (fd, (void *) ptr, nleft);
165 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
173 DEBUG ("Received EOF from fd %i. "
174 "Closing fd and returning error.",
180 assert (nleft >= status);
182 nleft = nleft - status;
190 ssize_t swrite (int fd, const void *buf, size_t count)
196 ptr = (const char *) buf;
201 status = write (fd, (const void *) ptr, nleft);
203 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
209 nleft = nleft - status;
216 int strsplit (char *string, char **fields, size_t size)
225 while ((fields[i] = strtok_r (ptr, " \t", &saveptr)) != NULL)
237 int strjoin (char *dst, size_t dst_len,
238 char **fields, size_t fields_num,
245 memset (dst, '\0', dst_len);
252 sep_len = strlen (sep);
254 for (i = 0; i < fields_num; i++)
256 if ((i > 0) && (sep_len > 0))
258 if (dst_len <= sep_len)
261 strncat (dst, sep, dst_len);
265 field_len = strlen (fields[i]);
267 if (dst_len <= field_len)
270 strncat (dst, fields[i], dst_len);
271 dst_len -= field_len;
274 return (strlen (dst));
277 int strsubstitute (char *str, char c_from, char c_to)
296 } /* int strsubstitute */
298 int escape_slashes (char *buf, int buf_len)
302 if (strcmp (buf, "/") == 0)
307 strncpy (buf, "root", buf_len);
314 /* Move one to the left */
316 memmove (buf, buf + 1, buf_len - 1);
318 for (i = 0; i < buf_len - 1; i++)
322 else if (buf[i] == '/')
328 } /* int escape_slashes */
330 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
332 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
335 if ((tv0->tv_sec < tv1->tv_sec)
336 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
339 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
340 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
342 if (ret->tv_nsec < 0)
344 assert (ret->tv_sec > 0);
346 ret->tv_nsec += 1000000000;
353 int check_create_dir (const char *file_orig)
364 int last_is_file = 1;
365 int path_is_absolute = 0;
370 * Sanity checks first
372 if (file_orig == NULL)
375 if ((len = strlen (file_orig)) < 1)
381 * If `file_orig' ends in a slash the last component is a directory,
382 * otherwise it's a file. Act accordingly..
384 if (file_orig[len - 1] == '/')
386 if (file_orig[0] == '/')
387 path_is_absolute = 1;
390 * Create a copy for `strtok_r' to destroy
392 strncpy (file_copy, file_orig, 512);
393 file_copy[511] = '\0';
396 * Break into components. This will eat up several slashes in a row and
397 * remove leading and trailing slashes..
402 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
407 if (fields_num >= 16)
412 * For each component, do..
414 for (i = 0; i < (fields_num - last_is_file); i++)
417 * Do not create directories that start with a dot. This
418 * prevents `../../' attacks and other likely malicious
421 if (fields[i][0] == '.')
423 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
428 * Join the components together again
431 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
432 fields, i + 1, "/") < 0)
434 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
438 if (stat (dir, &statbuf) == -1)
442 if (mkdir (dir, 0755) == -1)
445 ERROR ("check_create_dir: mkdir (%s): %s", dir,
447 errbuf, sizeof (errbuf)));
454 ERROR ("stat (%s): %s", dir,
455 sstrerror (errno, errbuf,
460 else if (!S_ISDIR (statbuf.st_mode))
462 ERROR ("stat (%s): Not a directory!", dir);
468 } /* check_create_dir */
471 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
478 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
481 if (*ksp_ptr == NULL)
483 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
485 ERROR ("Cound not find kstat %s", ident);
489 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
491 WARNING ("kstat %s has wrong type", ident);
498 assert (*ksp_ptr != NULL);
499 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
502 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
504 WARNING ("kstat %s could not be read", ident);
508 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
510 WARNING ("kstat %s has wrong type", ident);
517 long long get_kstat_value (kstat_t *ksp, char *name)
520 long long retval = -1LL;
523 assert (ksp != NULL);
524 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
528 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
531 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
533 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
538 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
541 if (kn->data_type == KSTAT_DATA_INT32)
542 retval = (long long) kn->value.i32;
543 else if (kn->data_type == KSTAT_DATA_UINT32)
544 retval = (long long) kn->value.ui32;
545 else if (kn->data_type == KSTAT_DATA_INT64)
546 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
547 else if (kn->data_type == KSTAT_DATA_UINT64)
548 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
550 WARNING ("get_kstat_value: Not a numeric value: %s", name);
554 #endif /* HAVE_LIBKSTAT */
556 unsigned long long ntohll (unsigned long long n)
558 #if BYTE_ORDER == BIG_ENDIAN
561 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
563 } /* unsigned long long ntohll */
565 unsigned long long htonll (unsigned long long n)
567 #if BYTE_ORDER == BIG_ENDIAN
570 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
572 } /* unsigned long long htonll */
574 #if FP_LAYOUT_NEED_NOTHING
575 /* Well, we need nothing.. */
576 /* #endif FP_LAYOUT_NEED_NOTHING */
578 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
579 # if FP_LAYOUT_NEED_ENDIANFLIP
580 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
581 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
582 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
583 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
584 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
585 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
586 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
587 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
589 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
590 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
593 double ntohd (double d)
604 /* NAN in x86 byte order */
605 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
606 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
607 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
608 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
617 ret.integer = FP_CONVERT (tmp);
618 return (ret.floating);
622 double htond (double d)
633 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
634 ret.byte[4] = ret.byte[5] = 0x00;
637 return (ret.floating);
644 tmp = FP_CONVERT (ret.integer);
646 return (ret.floating);
649 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
651 int format_name (char *ret, int ret_len,
652 const char *hostname,
653 const char *plugin, const char *plugin_instance,
654 const char *type, const char *type_instance)
658 assert (plugin != NULL);
659 assert (type != NULL);
661 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
663 if ((type_instance == NULL) || (strlen (type_instance) == 0))
664 status = snprintf (ret, ret_len, "%s/%s/%s",
665 hostname, plugin, type);
667 status = snprintf (ret, ret_len, "%s/%s/%s-%s",
668 hostname, plugin, type,
673 if ((type_instance == NULL) || (strlen (type_instance) == 0))
674 status = snprintf (ret, ret_len, "%s/%s-%s/%s",
675 hostname, plugin, plugin_instance,
678 status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
679 hostname, plugin, plugin_instance,
680 type, type_instance);
683 if ((status < 1) || (status >= ret_len))
686 } /* int format_name */
688 int parse_identifier (char *str, char **ret_host,
689 char **ret_plugin, char **ret_plugin_instance,
690 char **ret_type, char **ret_type_instance)
692 char *hostname = NULL;
694 char *plugin_instance = NULL;
696 char *type_instance = NULL;
699 if (hostname == NULL)
702 plugin = strchr (hostname, '/');
705 *plugin = '\0'; plugin++;
707 type = strchr (plugin, '/');
710 *type = '\0'; type++;
712 plugin_instance = strchr (plugin, '-');
713 if (plugin_instance != NULL)
715 *plugin_instance = '\0';
719 type_instance = strchr (type, '-');
720 if (type_instance != NULL)
722 *type_instance = '\0';
726 *ret_host = hostname;
727 *ret_plugin = plugin;
728 *ret_plugin_instance = plugin_instance;
730 *ret_type_instance = type_instance;
732 } /* int parse_identifier */
734 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
744 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
748 if (i >= vl->values_len)
753 if (strcmp ("N", ptr) == 0)
754 vl->time = time (NULL);
756 vl->time = (time_t) atoi (ptr);
760 if (strcmp ("U", ptr) == 0)
761 vl->values[i].gauge = NAN;
762 else if (ds->ds[i].type == DS_TYPE_COUNTER)
763 vl->values[i].counter = atoll (ptr);
764 else if (ds->ds[i].type == DS_TYPE_GAUGE)
765 vl->values[i].gauge = atof (ptr);
769 } /* while (strtok_r) */
771 if ((ptr != NULL) || (i != vl->values_len))
774 } /* int parse_values */
777 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
778 size_t buflen, struct passwd **pwbufp)
783 memset (pwbuf, '\0', sizeof (struct passwd));
785 pthread_mutex_lock (&getpwnam_r_lock);
789 pw = getpwnam (name);
792 status = (errno != 0) ? errno : ENOENT;
796 #define GETPWNAM_COPY_MEMBER(member) \
797 if (pw->member != NULL) \
799 int len = strlen (pw->member); \
805 sstrncpy (buf, pw->member, buflen); \
806 pwbuf->member = buf; \
808 buflen -= (len + 1); \
810 GETPWNAM_COPY_MEMBER(pw_name);
811 GETPWNAM_COPY_MEMBER(pw_passwd);
812 GETPWNAM_COPY_MEMBER(pw_gecos);
813 GETPWNAM_COPY_MEMBER(pw_dir);
814 GETPWNAM_COPY_MEMBER(pw_shell);
816 pwbuf->pw_uid = pw->pw_uid;
817 pwbuf->pw_gid = pw->pw_gid;
823 pthread_mutex_unlock (&getpwnam_r_lock);
826 } /* int getpwnam_r */