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;
52 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
55 void sstrncpy (char *d, const char *s, int len)
61 char *sstrdup (const char *s)
68 if((r = strdup (s)) == NULL)
70 DEBUG ("Not enough memory.");
77 /* Even though Posix requires "strerror_r" to return an "int",
78 * some systems (e.g. the GNU libc) return a "char *" _and_
79 * ignore the second argument ... -tokkee */
80 char *sstrerror (int errnum, char *buf, size_t buflen)
88 pthread_mutex_lock (&strerror_r_lock);
90 temp = strerror (errnum);
91 strncpy (buf, temp, buflen);
93 pthread_mutex_unlock (&strerror_r_lock);
95 /* #endif !HAVE_STRERROR_R */
97 #elif STRERROR_R_CHAR_P
100 temp = strerror_r (errnum, buf, buflen);
103 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
104 strncpy (buf, temp, buflen);
106 strncpy (buf, "strerror_r did not return "
107 "an error message", buflen);
110 /* #endif STRERROR_R_CHAR_P */
113 if (strerror_r (errnum, buf, buflen) != 0)
115 snprintf (buf, buflen, "Error #%i; "
116 "Additionally, strerror_r failed.",
119 #endif /* STRERROR_R_CHAR_P */
121 buf[buflen - 1] = '\0';
123 } /* char *sstrerror */
125 void *smalloc (size_t size)
129 if ((r = malloc (size)) == NULL)
131 DEBUG("Not enough memory.");
139 void sfree (void **ptr)
151 ssize_t sread (int fd, void *buf, size_t count)
162 status = read (fd, (void *) ptr, nleft);
164 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
172 DEBUG ("Received EOF from fd %i. "
173 "Closing fd and returning error.",
179 assert (nleft >= status);
181 nleft = nleft - status;
189 ssize_t swrite (int fd, const void *buf, size_t count)
195 ptr = (const char *) buf;
200 status = write (fd, (const void *) ptr, nleft);
202 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
208 nleft = nleft - status;
215 int strsplit (char *string, char **fields, size_t size)
224 while ((fields[i] = strtok_r (ptr, " \t", &saveptr)) != NULL)
236 int strjoin (char *dst, size_t dst_len,
237 char **fields, size_t fields_num,
244 memset (dst, '\0', dst_len);
251 sep_len = strlen (sep);
253 for (i = 0; i < fields_num; i++)
255 if ((i > 0) && (sep_len > 0))
257 if (dst_len <= sep_len)
260 strncat (dst, sep, dst_len);
264 field_len = strlen (fields[i]);
266 if (dst_len <= field_len)
269 strncat (dst, fields[i], dst_len);
270 dst_len -= field_len;
273 return (strlen (dst));
276 int strsubstitute (char *str, char c_from, char c_to)
295 } /* int strsubstitute */
297 int escape_slashes (char *buf, int buf_len)
301 if (strcmp (buf, "/") == 0)
306 strncpy (buf, "root", buf_len);
313 /* Move one to the left */
315 memmove (buf, buf + 1, buf_len - 1);
317 for (i = 0; i < buf_len - 1; i++)
321 else if (buf[i] == '/')
327 } /* int escape_slashes */
329 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
331 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
334 if ((tv0->tv_sec < tv1->tv_sec)
335 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
338 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
339 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
341 if (ret->tv_nsec < 0)
343 assert (ret->tv_sec > 0);
345 ret->tv_nsec += 1000000000;
352 int check_create_dir (const char *file_orig)
363 int last_is_file = 1;
364 int path_is_absolute = 0;
369 * Sanity checks first
371 if (file_orig == NULL)
374 if ((len = strlen (file_orig)) < 1)
380 * If `file_orig' ends in a slash the last component is a directory,
381 * otherwise it's a file. Act accordingly..
383 if (file_orig[len - 1] == '/')
385 if (file_orig[0] == '/')
386 path_is_absolute = 1;
389 * Create a copy for `strtok_r' to destroy
391 strncpy (file_copy, file_orig, 512);
392 file_copy[511] = '\0';
395 * Break into components. This will eat up several slashes in a row and
396 * remove leading and trailing slashes..
401 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
406 if (fields_num >= 16)
411 * For each component, do..
413 for (i = 0; i < (fields_num - last_is_file); i++)
416 * Do not create directories that start with a dot. This
417 * prevents `../../' attacks and other likely malicious
420 if (fields[i][0] == '.')
422 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
427 * Join the components together again
430 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
431 fields, i + 1, "/") < 0)
433 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
437 if (stat (dir, &statbuf) == -1)
441 if (mkdir (dir, 0755) == -1)
444 ERROR ("check_create_dir: mkdir (%s): %s", dir,
446 errbuf, sizeof (errbuf)));
453 ERROR ("stat (%s): %s", dir,
454 sstrerror (errno, errbuf,
459 else if (!S_ISDIR (statbuf.st_mode))
461 ERROR ("stat (%s): Not a directory!", dir);
467 } /* check_create_dir */
470 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
477 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
480 if (*ksp_ptr == NULL)
482 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
484 ERROR ("Cound not find kstat %s", ident);
488 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
490 WARNING ("kstat %s has wrong type", ident);
497 assert (*ksp_ptr != NULL);
498 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
501 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
503 WARNING ("kstat %s could not be read", ident);
507 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
509 WARNING ("kstat %s has wrong type", ident);
516 long long get_kstat_value (kstat_t *ksp, char *name)
519 long long retval = -1LL;
522 assert (ksp != NULL);
523 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
527 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
530 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
532 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
537 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
540 if (kn->data_type == KSTAT_DATA_INT32)
541 retval = (long long) kn->value.i32;
542 else if (kn->data_type == KSTAT_DATA_UINT32)
543 retval = (long long) kn->value.ui32;
544 else if (kn->data_type == KSTAT_DATA_INT64)
545 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
546 else if (kn->data_type == KSTAT_DATA_UINT64)
547 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
549 WARNING ("get_kstat_value: Not a numeric value: %s", name);
553 #endif /* HAVE_LIBKSTAT */
555 unsigned long long ntohll (unsigned long long n)
557 #if __BYTE_ORDER == __BIG_ENDIAN
560 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
562 } /* unsigned long long ntohll */
564 unsigned long long htonll (unsigned long long n)
566 #if __BYTE_ORDER == __BIG_ENDIAN
569 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
571 } /* unsigned long long htonll */
573 int format_name (char *ret, int ret_len,
574 const char *hostname,
575 const char *plugin, const char *plugin_instance,
576 const char *type, const char *type_instance)
580 assert (plugin != NULL);
581 assert (type != NULL);
583 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
585 if ((type_instance == NULL) || (strlen (type_instance) == 0))
586 status = snprintf (ret, ret_len, "%s/%s/%s",
587 hostname, plugin, type);
589 status = snprintf (ret, ret_len, "%s/%s/%s-%s",
590 hostname, plugin, type,
595 if ((type_instance == NULL) || (strlen (type_instance) == 0))
596 status = snprintf (ret, ret_len, "%s/%s-%s/%s",
597 hostname, plugin, plugin_instance,
600 status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
601 hostname, plugin, plugin_instance,
602 type, type_instance);
605 if ((status < 1) || (status >= ret_len))
608 } /* int format_name */
610 int parse_identifier (char *str, char **ret_host,
611 char **ret_plugin, char **ret_plugin_instance,
612 char **ret_type, char **ret_type_instance)
614 char *hostname = NULL;
616 char *plugin_instance = NULL;
618 char *type_instance = NULL;
621 if (hostname == NULL)
624 plugin = strchr (hostname, '/');
627 *plugin = '\0'; plugin++;
629 type = strchr (plugin, '/');
632 *type = '\0'; type++;
634 plugin_instance = strchr (plugin, '-');
635 if (plugin_instance != NULL)
637 *plugin_instance = '\0';
641 type_instance = strchr (type, '-');
642 if (type_instance != NULL)
644 *type_instance = '\0';
648 *ret_host = hostname;
649 *ret_plugin = plugin;
650 *ret_plugin_instance = plugin_instance;
652 *ret_type_instance = type_instance;
654 } /* int parse_identifier */
656 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
666 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
670 if (i >= vl->values_len)
675 if (strcmp ("N", ptr) == 0)
676 vl->time = time (NULL);
678 vl->time = (time_t) atoi (ptr);
682 if (strcmp ("U", ptr) == 0)
683 vl->values[i].gauge = NAN;
684 else if (ds->ds[i].type == DS_TYPE_COUNTER)
685 vl->values[i].counter = atoll (ptr);
686 else if (ds->ds[i].type == DS_TYPE_GAUGE)
687 vl->values[i].gauge = atof (ptr);
691 } /* while (strtok_r) */
693 if ((ptr != NULL) || (i != vl->values_len))
696 } /* int parse_values */
699 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
700 size_t buflen, struct passwd **pwbufp)
705 memset (pwbuf, '\0', sizeof (struct passwd));
707 pthread_mutex_lock (&getpwnam_r_lock);
711 pw = getpwnam (name);
714 status = (errno != 0) ? errno : ENOENT;
718 #define GETPWNAM_COPY_MEMBER(member) \
719 if (pw->member != NULL) \
721 int len = strlen (pw->member); \
727 sstrncpy (buf, pw->member, buflen); \
728 pwbuf->member = buf; \
730 buflen -= (len + 1); \
732 GETPWNAM_COPY_MEMBER(pw_name);
733 GETPWNAM_COPY_MEMBER(pw_passwd);
734 GETPWNAM_COPY_MEMBER(pw_gecos);
735 GETPWNAM_COPY_MEMBER(pw_dir);
736 GETPWNAM_COPY_MEMBER(pw_shell);
738 pwbuf->pw_uid = pw->pw_uid;
739 pwbuf->pw_gid = pw->pw_gid;
745 pthread_mutex_unlock (&getpwnam_r_lock);
748 } /* int getpwnam_r */