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 /* Don't use the return value of `strerror_r', because the GNU-people got
74 * inventive there.. -octo */
75 char *sstrerror (int errnum, char *buf, size_t buflen)
78 strerror_r (errnum, buf, buflen);
80 } /* char *sstrerror */
82 void *smalloc (size_t size)
86 if ((r = malloc (size)) == NULL)
88 DEBUG("Not enough memory.");
96 void sfree (void **ptr)
108 ssize_t sread (int fd, void *buf, size_t count)
119 status = read (fd, (void *) ptr, nleft);
121 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
129 DEBUG ("Received EOF from fd %i. "
130 "Closing fd and returning error.",
136 assert (nleft >= status);
138 nleft = nleft - status;
146 ssize_t swrite (int fd, const void *buf, size_t count)
152 ptr = (const char *) buf;
157 status = write (fd, (const void *) ptr, nleft);
159 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
165 nleft = nleft - status;
172 int strsplit (char *string, char **fields, size_t size)
181 while ((fields[i] = strtok_r (ptr, " \t", &saveptr)) != NULL)
193 int strjoin (char *dst, size_t dst_len,
194 char **fields, size_t fields_num,
201 memset (dst, '\0', dst_len);
208 sep_len = strlen (sep);
210 for (i = 0; i < fields_num; i++)
212 if ((i > 0) && (sep_len > 0))
214 if (dst_len <= sep_len)
217 strncat (dst, sep, dst_len);
221 field_len = strlen (fields[i]);
223 if (dst_len <= field_len)
226 strncat (dst, fields[i], dst_len);
227 dst_len -= field_len;
230 return (strlen (dst));
233 int strsubstitute (char *str, char c_from, char c_to)
252 } /* int strsubstitute */
254 int escape_slashes (char *buf, int buf_len)
258 if (strcmp (buf, "/") == 0)
263 strncpy (buf, "root", buf_len);
270 /* Move one to the left */
272 memmove (buf, buf + 1, buf_len - 1);
274 for (i = 0; i < buf_len - 1; i++)
278 else if (buf[i] == '/')
284 } /* int escape_slashes */
286 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
288 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
291 if ((tv0->tv_sec < tv1->tv_sec)
292 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
295 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
296 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
298 if (ret->tv_nsec < 0)
300 assert (ret->tv_sec > 0);
302 ret->tv_nsec += 1000000000;
309 int check_create_dir (const char *file_orig)
320 int last_is_file = 1;
321 int path_is_absolute = 0;
326 * Sanity checks first
328 if (file_orig == NULL)
331 if ((len = strlen (file_orig)) < 1)
337 * If `file_orig' ends in a slash the last component is a directory,
338 * otherwise it's a file. Act accordingly..
340 if (file_orig[len - 1] == '/')
342 if (file_orig[0] == '/')
343 path_is_absolute = 1;
346 * Create a copy for `strtok_r' to destroy
348 strncpy (file_copy, file_orig, 512);
349 file_copy[511] = '\0';
352 * Break into components. This will eat up several slashes in a row and
353 * remove leading and trailing slashes..
358 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
363 if (fields_num >= 16)
368 * For each component, do..
370 for (i = 0; i < (fields_num - last_is_file); i++)
373 * Do not create directories that start with a dot. This
374 * prevents `../../' attacks and other likely malicious
377 if (fields[i][0] == '.')
379 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
384 * Join the components together again
387 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
388 fields, i + 1, "/") < 0)
390 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
394 if (stat (dir, &statbuf) == -1)
398 if (mkdir (dir, 0755) == -1)
401 ERROR ("mkdir (%s): %s", dir,
403 errbuf, sizeof (errbuf)));
410 ERROR ("stat (%s): %s", dir,
411 sstrerror (errno, errbuf,
416 else if (!S_ISDIR (statbuf.st_mode))
418 ERROR ("stat (%s): Not a directory!", dir);
427 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
434 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
437 if (*ksp_ptr == NULL)
439 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
441 ERROR ("Cound not find kstat %s", ident);
445 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
447 WARNING ("kstat %s has wrong type", ident);
454 assert (*ksp_ptr != NULL);
455 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
458 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
460 WARNING ("kstat %s could not be read", ident);
464 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
466 WARNING ("kstat %s has wrong type", ident);
473 long long get_kstat_value (kstat_t *ksp, char *name)
476 long long retval = -1LL;
479 assert (ksp != NULL);
480 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
484 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
487 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
489 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
494 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
497 if (kn->data_type == KSTAT_DATA_INT32)
498 retval = (long long) kn->value.i32;
499 else if (kn->data_type == KSTAT_DATA_UINT32)
500 retval = (long long) kn->value.ui32;
501 else if (kn->data_type == KSTAT_DATA_INT64)
502 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
503 else if (kn->data_type == KSTAT_DATA_UINT64)
504 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
506 WARNING ("get_kstat_value: Not a numeric value: %s", name);
510 #endif /* HAVE_LIBKSTAT */
512 unsigned long long ntohll (unsigned long long n)
514 #if __BYTE_ORDER == __BIG_ENDIAN
517 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
519 } /* unsigned long long ntohll */
521 unsigned long long htonll (unsigned long long n)
523 #if __BYTE_ORDER == __BIG_ENDIAN
526 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
528 } /* unsigned long long htonll */
530 int format_name (char *ret, int ret_len,
531 const char *hostname,
532 const char *plugin, const char *plugin_instance,
533 const char *type, const char *type_instance)
537 assert (plugin != NULL);
538 assert (type != NULL);
540 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
542 if ((type_instance == NULL) || (strlen (type_instance) == 0))
543 status = snprintf (ret, ret_len, "%s/%s/%s",
544 hostname, plugin, type);
546 status = snprintf (ret, ret_len, "%s/%s/%s-%s",
547 hostname, plugin, type,
552 if ((type_instance == NULL) || (strlen (type_instance) == 0))
553 status = snprintf (ret, ret_len, "%s/%s-%s/%s",
554 hostname, plugin, plugin_instance,
557 status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
558 hostname, plugin, plugin_instance,
559 type, type_instance);
562 if ((status < 1) || (status >= ret_len))
565 } /* int format_name */
567 int parse_identifier (char *str, char **ret_host,
568 char **ret_plugin, char **ret_plugin_instance,
569 char **ret_type, char **ret_type_instance)
571 char *hostname = NULL;
573 char *plugin_instance = NULL;
575 char *type_instance = NULL;
578 if (hostname == NULL)
581 plugin = strchr (hostname, '/');
584 *plugin = '\0'; plugin++;
586 type = strchr (plugin, '/');
589 *type = '\0'; type++;
591 plugin_instance = strchr (plugin, '-');
592 if (plugin_instance != NULL)
594 *plugin_instance = '\0';
598 type_instance = strchr (type, '-');
599 if (type_instance != NULL)
601 *type_instance = '\0';
605 *ret_host = hostname;
606 *ret_plugin = plugin;
607 *ret_plugin_instance = plugin_instance;
609 *ret_type_instance = type_instance;
611 } /* int parse_identifier */
613 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
623 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
627 if (i >= vl->values_len)
632 if (strcmp ("N", ptr) == 0)
633 vl->time = time (NULL);
635 vl->time = (time_t) atoi (ptr);
639 if (strcmp ("U", ptr) == 0)
640 vl->values[i].gauge = NAN;
641 else if (ds->ds[i].type == DS_TYPE_COUNTER)
642 vl->values[i].counter = atoll (ptr);
643 else if (ds->ds[i].type == DS_TYPE_GAUGE)
644 vl->values[i].gauge = atof (ptr);
648 } /* while (strtok_r) */
650 if ((ptr != NULL) || (i != vl->values_len))
653 } /* int parse_values */
656 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
657 size_t buflen, struct passwd **pwbufp)
662 memset (pwbuf, '\0', sizeof (struct passwd));
664 pthread_mutex_lock (&getpwnam_r_lock);
668 pw = getpwnam (name);
671 status = (errno != 0) ? errno : ENOENT;
675 #define GETPWNAM_COPY_MEMBER(member) \
676 if (pw->member != NULL) \
678 int len = strlen (pw->member); \
684 sstrncpy (buf, pw->member, buflen); \
685 pwbuf->member = buf; \
687 buflen -= (len + 1); \
689 GETPWNAM_COPY_MEMBER(pw_name);
690 GETPWNAM_COPY_MEMBER(pw_passwd);
691 GETPWNAM_COPY_MEMBER(pw_gecos);
692 GETPWNAM_COPY_MEMBER(pw_dir);
693 GETPWNAM_COPY_MEMBER(pw_shell);
695 pwbuf->pw_uid = pw->pw_uid;
696 pwbuf->pw_gid = pw->pw_gid;
702 pthread_mutex_unlock (&getpwnam_r_lock);
705 } /* int getpwnam_r */