2 * collectd - src/common.c
3 * Copyright (C) 2005-2008 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>
38 /* for ntohl and htonl */
40 # include <arpa/inet.h>
44 extern kstat_ctl_t *kc;
48 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
52 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
55 char *sstrncpy (char *dest, const char *src, size_t n)
57 strncpy (dest, src, n);
61 } /* char *sstrncpy */
63 char *sstrdup (const char *s)
70 if((r = strdup (s)) == NULL)
72 DEBUG ("Not enough memory.");
79 /* Even though Posix requires "strerror_r" to return an "int",
80 * some systems (e.g. the GNU libc) return a "char *" _and_
81 * ignore the second argument ... -tokkee */
82 char *sstrerror (int errnum, char *buf, size_t buflen)
90 pthread_mutex_lock (&strerror_r_lock);
92 temp = strerror (errnum);
93 strncpy (buf, temp, buflen);
95 pthread_mutex_unlock (&strerror_r_lock);
97 /* #endif !HAVE_STRERROR_R */
99 #elif STRERROR_R_CHAR_P
102 temp = strerror_r (errnum, buf, buflen);
105 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
106 strncpy (buf, temp, buflen);
108 strncpy (buf, "strerror_r did not return "
109 "an error message", buflen);
112 /* #endif STRERROR_R_CHAR_P */
115 if (strerror_r (errnum, buf, buflen) != 0)
117 snprintf (buf, buflen, "Error #%i; "
118 "Additionally, strerror_r failed.",
121 #endif /* STRERROR_R_CHAR_P */
123 buf[buflen - 1] = '\0';
125 } /* char *sstrerror */
127 void *smalloc (size_t size)
131 if ((r = malloc (size)) == NULL)
133 DEBUG("Not enough memory.");
141 void sfree (void **ptr)
153 ssize_t sread (int fd, void *buf, size_t count)
164 status = read (fd, (void *) ptr, nleft);
166 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
174 DEBUG ("Received EOF from fd %i. "
175 "Closing fd and returning error.",
181 assert ((0 > status) || (nleft >= (size_t)status));
183 nleft = nleft - status;
191 ssize_t swrite (int fd, const void *buf, size_t count)
197 ptr = (const char *) buf;
202 status = write (fd, (const void *) ptr, nleft);
204 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
210 nleft = nleft - status;
217 int strsplit (char *string, char **fields, size_t size)
226 while ((fields[i] = strtok_r (ptr, " \t", &saveptr)) != NULL)
238 int strjoin (char *dst, size_t dst_len,
239 char **fields, size_t fields_num,
246 memset (dst, '\0', dst_len);
253 sep_len = strlen (sep);
255 for (i = 0; i < (int)fields_num; i++)
257 if ((i > 0) && (sep_len > 0))
259 if (dst_len <= sep_len)
262 strncat (dst, sep, dst_len);
266 field_len = strlen (fields[i]);
268 if (dst_len <= field_len)
271 strncat (dst, fields[i], dst_len);
272 dst_len -= field_len;
275 return (strlen (dst));
278 int strsubstitute (char *str, char c_from, char c_to)
297 } /* int strsubstitute */
299 int escape_slashes (char *buf, int buf_len)
303 if (strcmp (buf, "/") == 0)
308 strncpy (buf, "root", buf_len);
315 /* Move one to the left */
317 memmove (buf, buf + 1, buf_len - 1);
319 for (i = 0; i < buf_len - 1; i++)
323 else if (buf[i] == '/')
329 } /* int escape_slashes */
331 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
333 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
336 if ((tv0->tv_sec < tv1->tv_sec)
337 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
340 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
341 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
343 if (ret->tv_nsec < 0)
345 assert (ret->tv_sec > 0);
347 ret->tv_nsec += 1000000000;
354 int check_create_dir (const char *file_orig)
365 int last_is_file = 1;
366 int path_is_absolute = 0;
371 * Sanity checks first
373 if (file_orig == NULL)
376 if ((len = strlen (file_orig)) < 1)
382 * If `file_orig' ends in a slash the last component is a directory,
383 * otherwise it's a file. Act accordingly..
385 if (file_orig[len - 1] == '/')
387 if (file_orig[0] == '/')
388 path_is_absolute = 1;
391 * Create a copy for `strtok_r' to destroy
393 strncpy (file_copy, file_orig, 512);
394 file_copy[511] = '\0';
397 * Break into components. This will eat up several slashes in a row and
398 * remove leading and trailing slashes..
403 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
408 if (fields_num >= 16)
413 * For each component, do..
415 for (i = 0; i < (fields_num - last_is_file); i++)
418 * Do not create directories that start with a dot. This
419 * prevents `../../' attacks and other likely malicious
422 if (fields[i][0] == '.')
424 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
429 * Join the components together again
432 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
433 fields, i + 1, "/") < 0)
435 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
439 if (stat (dir, &statbuf) == -1)
443 if (mkdir (dir, 0755) == -1)
446 ERROR ("check_create_dir: mkdir (%s): %s", dir,
448 errbuf, sizeof (errbuf)));
455 ERROR ("stat (%s): %s", dir,
456 sstrerror (errno, errbuf,
461 else if (!S_ISDIR (statbuf.st_mode))
463 ERROR ("stat (%s): Not a directory!", dir);
469 } /* check_create_dir */
472 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
479 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
482 if (*ksp_ptr == NULL)
484 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
486 ERROR ("Cound not find kstat %s", ident);
490 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
492 WARNING ("kstat %s has wrong type", ident);
499 assert (*ksp_ptr != NULL);
500 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
503 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
505 WARNING ("kstat %s could not be read", ident);
509 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
511 WARNING ("kstat %s has wrong type", ident);
518 long long get_kstat_value (kstat_t *ksp, char *name)
521 long long retval = -1LL;
524 assert (ksp != NULL);
525 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
529 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
532 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
534 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
539 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
542 if (kn->data_type == KSTAT_DATA_INT32)
543 retval = (long long) kn->value.i32;
544 else if (kn->data_type == KSTAT_DATA_UINT32)
545 retval = (long long) kn->value.ui32;
546 else if (kn->data_type == KSTAT_DATA_INT64)
547 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
548 else if (kn->data_type == KSTAT_DATA_UINT64)
549 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
551 WARNING ("get_kstat_value: Not a numeric value: %s", name);
555 #endif /* HAVE_LIBKSTAT */
557 unsigned long long ntohll (unsigned long long n)
559 #if __BYTE_ORDER == __BIG_ENDIAN
562 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
564 } /* unsigned long long ntohll */
566 unsigned long long htonll (unsigned long long n)
568 #if __BYTE_ORDER == __BIG_ENDIAN
571 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
573 } /* unsigned long long htonll */
575 #if FP_LAYOUT_NEED_NOTHING
576 /* Well, we need nothing.. */
577 /* #endif FP_LAYOUT_NEED_NOTHING */
579 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
580 # if FP_LAYOUT_NEED_ENDIANFLIP
581 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
582 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
583 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
584 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
585 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
586 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
587 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
588 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
590 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
591 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
594 double ntohd (double d)
605 /* NAN in x86 byte order */
606 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
607 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
608 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
609 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
618 ret.integer = FP_CONVERT (tmp);
619 return (ret.floating);
623 double htond (double d)
634 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
635 ret.byte[4] = ret.byte[5] = 0x00;
638 return (ret.floating);
645 tmp = FP_CONVERT (ret.integer);
647 return (ret.floating);
650 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
652 int format_name (char *ret, int ret_len,
653 const char *hostname,
654 const char *plugin, const char *plugin_instance,
655 const char *type, const char *type_instance)
659 assert (plugin != NULL);
660 assert (type != NULL);
662 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
664 if ((type_instance == NULL) || (strlen (type_instance) == 0))
665 status = snprintf (ret, ret_len, "%s/%s/%s",
666 hostname, plugin, type);
668 status = snprintf (ret, ret_len, "%s/%s/%s-%s",
669 hostname, plugin, type,
674 if ((type_instance == NULL) || (strlen (type_instance) == 0))
675 status = snprintf (ret, ret_len, "%s/%s-%s/%s",
676 hostname, plugin, plugin_instance,
679 status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
680 hostname, plugin, plugin_instance,
681 type, type_instance);
684 if ((status < 1) || (status >= ret_len))
687 } /* int format_name */
689 int parse_identifier (char *str, char **ret_host,
690 char **ret_plugin, char **ret_plugin_instance,
691 char **ret_type, char **ret_type_instance)
693 char *hostname = NULL;
695 char *plugin_instance = NULL;
697 char *type_instance = NULL;
700 if (hostname == NULL)
703 plugin = strchr (hostname, '/');
706 *plugin = '\0'; plugin++;
708 type = strchr (plugin, '/');
711 *type = '\0'; type++;
713 plugin_instance = strchr (plugin, '-');
714 if (plugin_instance != NULL)
716 *plugin_instance = '\0';
720 type_instance = strchr (type, '-');
721 if (type_instance != NULL)
723 *type_instance = '\0';
727 *ret_host = hostname;
728 *ret_plugin = plugin;
729 *ret_plugin_instance = plugin_instance;
731 *ret_type_instance = type_instance;
733 } /* int parse_identifier */
735 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
745 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
749 if (i >= vl->values_len)
754 if (strcmp ("N", ptr) == 0)
755 vl->time = time (NULL);
757 vl->time = (time_t) atoi (ptr);
761 if (strcmp ("U", ptr) == 0)
762 vl->values[i].gauge = NAN;
763 else if (ds->ds[i].type == DS_TYPE_COUNTER)
764 vl->values[i].counter = atoll (ptr);
765 else if (ds->ds[i].type == DS_TYPE_GAUGE)
766 vl->values[i].gauge = atof (ptr);
770 } /* while (strtok_r) */
772 if ((ptr != NULL) || (i != vl->values_len))
775 } /* int parse_values */
778 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
779 size_t buflen, struct passwd **pwbufp)
784 memset (pwbuf, '\0', sizeof (struct passwd));
786 pthread_mutex_lock (&getpwnam_r_lock);
790 pw = getpwnam (name);
793 status = (errno != 0) ? errno : ENOENT;
797 #define GETPWNAM_COPY_MEMBER(member) \
798 if (pw->member != NULL) \
800 int len = strlen (pw->member); \
806 sstrncpy (buf, pw->member, buflen); \
807 pwbuf->member = buf; \
809 buflen -= (len + 1); \
811 GETPWNAM_COPY_MEMBER(pw_name);
812 GETPWNAM_COPY_MEMBER(pw_passwd);
813 GETPWNAM_COPY_MEMBER(pw_gecos);
814 GETPWNAM_COPY_MEMBER(pw_dir);
815 GETPWNAM_COPY_MEMBER(pw_shell);
817 pwbuf->pw_uid = pw->pw_uid;
818 pwbuf->pw_gid = pw->pw_gid;
824 pthread_mutex_unlock (&getpwnam_r_lock);
827 } /* int getpwnam_r */
828 #endif /* !HAVE_GETPWNAM_R */
830 int notification_init (notification_t *n, int severity, const char *message,
832 const char *plugin, const char *plugin_instance,
833 const char *type, const char *type_instance)
835 memset (n, '\0', sizeof (notification_t));
837 n->severity = severity;
840 strncpy (n->message, message, sizeof (n->message));
842 strncpy (n->host, host, sizeof (n->host));
844 strncpy (n->plugin, plugin, sizeof (n->plugin));
845 if (plugin_instance != NULL)
846 strncpy (n->plugin_instance, plugin_instance,
847 sizeof (n->plugin_instance));
849 strncpy (n->type, type, sizeof (n->type));
850 if (type_instance != NULL)
851 strncpy (n->type_instance, type_instance,
852 sizeof (n->type_instance));
854 n->message[sizeof (n->message) - 1] = '\0';
855 n->host[sizeof (n->host) - 1] = '\0';
856 n->plugin[sizeof (n->plugin) - 1] = '\0';
857 n->plugin_instance[sizeof (n->plugin_instance) - 1] = '\0';
858 n->type[sizeof (n->type) - 1] = '\0';
859 n->type_instance[sizeof (n->type_instance) - 1] = '\0';
862 } /* int notification_init */