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>
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;
51 void sstrncpy (char *d, const char *s, int len)
57 char *sstrdup (const char *s)
64 if((r = strdup (s)) == NULL)
66 DEBUG ("Not enough memory.");
73 /* Even though Posix requires "strerror_r" to return an "int",
74 * some systems (e.g. the GNU libc) return a "char *" _and_
75 * ignore the second argument ... -tokkee */
76 char *sstrerror (int errnum, char *buf, size_t buflen)
79 #ifdef STRERROR_R_CHAR_P
82 temp = strerror_r (errnum, buf, buflen);
85 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
86 strncpy (buf, temp, buflen);
88 strncpy (buf, "strerror_r did not return "
89 "an error message", buflen);
90 buf[buflen - 1] = '\0';
94 if (strerror_r (errnum, buf, buflen) != 0)
96 snprintf (buf, buflen, "Error #%i; "
97 "Additionally, strerror_r failed.",
100 #endif /* STRERROR_R_CHAR_P */
101 buf[buflen - 1] = '\0';
103 } /* char *sstrerror */
105 void *smalloc (size_t size)
109 if ((r = malloc (size)) == NULL)
111 DEBUG("Not enough memory.");
119 void sfree (void **ptr)
131 ssize_t sread (int fd, void *buf, size_t count)
142 status = read (fd, (void *) ptr, nleft);
144 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
152 DEBUG ("Received EOF from fd %i. "
153 "Closing fd and returning error.",
159 assert (nleft >= status);
161 nleft = nleft - status;
169 ssize_t swrite (int fd, const void *buf, size_t count)
175 ptr = (const char *) buf;
180 status = write (fd, (const void *) ptr, nleft);
182 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
188 nleft = nleft - status;
195 int strsplit (char *string, char **fields, size_t size)
204 while ((fields[i] = strtok_r (ptr, " \t", &saveptr)) != NULL)
216 int strjoin (char *dst, size_t dst_len,
217 char **fields, size_t fields_num,
224 memset (dst, '\0', dst_len);
231 sep_len = strlen (sep);
233 for (i = 0; i < fields_num; i++)
235 if ((i > 0) && (sep_len > 0))
237 if (dst_len <= sep_len)
240 strncat (dst, sep, dst_len);
244 field_len = strlen (fields[i]);
246 if (dst_len <= field_len)
249 strncat (dst, fields[i], dst_len);
250 dst_len -= field_len;
253 return (strlen (dst));
256 int strsubstitute (char *str, char c_from, char c_to)
275 } /* int strsubstitute */
277 int escape_slashes (char *buf, int buf_len)
281 if (strcmp (buf, "/") == 0)
286 strncpy (buf, "root", buf_len);
293 /* Move one to the left */
295 memmove (buf, buf + 1, buf_len - 1);
297 for (i = 0; i < buf_len - 1; i++)
301 else if (buf[i] == '/')
307 } /* int escape_slashes */
309 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
311 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
314 if ((tv0->tv_sec < tv1->tv_sec)
315 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
318 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
319 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
321 if (ret->tv_nsec < 0)
323 assert (ret->tv_sec > 0);
325 ret->tv_nsec += 1000000000;
332 int check_create_dir (const char *file_orig)
343 int last_is_file = 1;
344 int path_is_absolute = 0;
349 * Sanity checks first
351 if (file_orig == NULL)
354 if ((len = strlen (file_orig)) < 1)
360 * If `file_orig' ends in a slash the last component is a directory,
361 * otherwise it's a file. Act accordingly..
363 if (file_orig[len - 1] == '/')
365 if (file_orig[0] == '/')
366 path_is_absolute = 1;
369 * Create a copy for `strtok_r' to destroy
371 strncpy (file_copy, file_orig, 512);
372 file_copy[511] = '\0';
375 * Break into components. This will eat up several slashes in a row and
376 * remove leading and trailing slashes..
381 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
386 if (fields_num >= 16)
391 * For each component, do..
393 for (i = 0; i < (fields_num - last_is_file); i++)
396 * Do not create directories that start with a dot. This
397 * prevents `../../' attacks and other likely malicious
400 if (fields[i][0] == '.')
402 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
407 * Join the components together again
410 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
411 fields, i + 1, "/") < 0)
413 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
417 if (stat (dir, &statbuf) == -1)
421 if (mkdir (dir, 0755) == -1)
424 ERROR ("mkdir (%s): %s", dir,
426 errbuf, sizeof (errbuf)));
433 ERROR ("stat (%s): %s", dir,
434 sstrerror (errno, errbuf,
439 else if (!S_ISDIR (statbuf.st_mode))
441 ERROR ("stat (%s): Not a directory!", dir);
450 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
457 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
460 if (*ksp_ptr == NULL)
462 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
464 ERROR ("Cound not find kstat %s", ident);
468 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
470 WARNING ("kstat %s has wrong type", ident);
477 assert (*ksp_ptr != NULL);
478 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
481 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
483 WARNING ("kstat %s could not be read", ident);
487 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
489 WARNING ("kstat %s has wrong type", ident);
496 long long get_kstat_value (kstat_t *ksp, char *name)
499 long long retval = -1LL;
502 assert (ksp != NULL);
503 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
507 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
510 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
512 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
517 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
520 if (kn->data_type == KSTAT_DATA_INT32)
521 retval = (long long) kn->value.i32;
522 else if (kn->data_type == KSTAT_DATA_UINT32)
523 retval = (long long) kn->value.ui32;
524 else if (kn->data_type == KSTAT_DATA_INT64)
525 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
526 else if (kn->data_type == KSTAT_DATA_UINT64)
527 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
529 WARNING ("get_kstat_value: Not a numeric value: %s", name);
533 #endif /* HAVE_LIBKSTAT */
535 unsigned long long ntohll (unsigned long long n)
537 #if __BYTE_ORDER == __BIG_ENDIAN
540 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
542 } /* unsigned long long ntohll */
544 unsigned long long htonll (unsigned long long n)
546 #if __BYTE_ORDER == __BIG_ENDIAN
549 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
551 } /* unsigned long long htonll */
553 int format_name (char *ret, int ret_len,
554 const char *hostname,
555 const char *plugin, const char *plugin_instance,
556 const char *type, const char *type_instance)
560 assert (plugin != NULL);
561 assert (type != NULL);
563 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
565 if ((type_instance == NULL) || (strlen (type_instance) == 0))
566 status = snprintf (ret, ret_len, "%s/%s/%s",
567 hostname, plugin, type);
569 status = snprintf (ret, ret_len, "%s/%s/%s-%s",
570 hostname, plugin, type,
575 if ((type_instance == NULL) || (strlen (type_instance) == 0))
576 status = snprintf (ret, ret_len, "%s/%s-%s/%s",
577 hostname, plugin, plugin_instance,
580 status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
581 hostname, plugin, plugin_instance,
582 type, type_instance);
585 if ((status < 1) || (status >= ret_len))
588 } /* int format_name */
590 int parse_identifier (char *str, char **ret_host,
591 char **ret_plugin, char **ret_plugin_instance,
592 char **ret_type, char **ret_type_instance)
594 char *hostname = NULL;
596 char *plugin_instance = NULL;
598 char *type_instance = NULL;
601 if (hostname == NULL)
604 plugin = strchr (hostname, '/');
607 *plugin = '\0'; plugin++;
609 type = strchr (plugin, '/');
612 *type = '\0'; type++;
614 plugin_instance = strchr (plugin, '-');
615 if (plugin_instance != NULL)
617 *plugin_instance = '\0';
621 type_instance = strchr (type, '-');
622 if (type_instance != NULL)
624 *type_instance = '\0';
628 *ret_host = hostname;
629 *ret_plugin = plugin;
630 *ret_plugin_instance = plugin_instance;
632 *ret_type_instance = type_instance;
634 } /* int parse_identifier */
636 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
646 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
650 if (i >= vl->values_len)
655 if (strcmp ("N", ptr) == 0)
656 vl->time = time (NULL);
658 vl->time = (time_t) atoi (ptr);
662 if (strcmp ("U", ptr) == 0)
663 vl->values[i].gauge = NAN;
664 else if (ds->ds[i].type == DS_TYPE_COUNTER)
665 vl->values[i].counter = atoll (ptr);
666 else if (ds->ds[i].type == DS_TYPE_GAUGE)
667 vl->values[i].gauge = atof (ptr);
671 } /* while (strtok_r) */
673 if ((ptr != NULL) || (i != vl->values_len))
676 } /* int parse_values */
679 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
680 size_t buflen, struct passwd **pwbufp)
685 memset (pwbuf, '\0', sizeof (struct passwd));
687 pthread_mutex_lock (&getpwnam_r_lock);
691 pw = getpwnam (name);
694 status = (errno != 0) ? errno : ENOENT;
698 #define GETPWNAM_COPY_MEMBER(member) \
699 if (pw->member != NULL) \
701 int len = strlen (pw->member); \
707 sstrncpy (buf, pw->member, buflen); \
708 pwbuf->member = buf; \
710 buflen -= (len + 1); \
712 GETPWNAM_COPY_MEMBER(pw_name);
713 GETPWNAM_COPY_MEMBER(pw_passwd);
714 GETPWNAM_COPY_MEMBER(pw_gecos);
715 GETPWNAM_COPY_MEMBER(pw_dir);
716 GETPWNAM_COPY_MEMBER(pw_shell);
718 pwbuf->pw_uid = pw->pw_uid;
719 pwbuf->pw_gid = pw->pw_gid;
725 pthread_mutex_unlock (&getpwnam_r_lock);
728 } /* int getpwnam_r */