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>
34 /* for ntohl and htonl */
36 # include <arpa/inet.h>
40 extern kstat_ctl_t *kc;
43 void sstrncpy (char *d, const char *s, int len)
49 char *sstrdup (const char *s)
56 if((r = strdup (s)) == NULL)
58 DEBUG ("Not enough memory.");
65 /* Don't use the return value of `strerror_r', because the GNU-people got
66 * inventive there.. -octo */
67 char *sstrerror (int errnum, char *buf, size_t buflen)
70 strerror_r (errnum, buf, buflen);
72 } /* char *sstrerror */
74 void *smalloc (size_t size)
78 if ((r = malloc (size)) == NULL)
80 DEBUG("Not enough memory.");
88 void sfree (void **ptr)
100 ssize_t sread (int fd, void *buf, size_t count)
111 status = read (fd, (void *) ptr, nleft);
113 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
121 DEBUG ("Received EOF from fd %i. "
122 "Closing fd and returning error.",
128 assert (nleft >= status);
130 nleft = nleft - status;
138 ssize_t swrite (int fd, const void *buf, size_t count)
144 ptr = (const char *) buf;
149 status = write (fd, (const void *) ptr, nleft);
151 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
157 nleft = nleft - status;
164 int strsplit (char *string, char **fields, size_t size)
173 while ((fields[i] = strtok_r (ptr, " \t", &saveptr)) != NULL)
185 int strjoin (char *dst, size_t dst_len,
186 char **fields, size_t fields_num,
193 memset (dst, '\0', dst_len);
200 sep_len = strlen (sep);
202 for (i = 0; i < fields_num; i++)
204 if ((i > 0) && (sep_len > 0))
206 if (dst_len <= sep_len)
209 strncat (dst, sep, dst_len);
213 field_len = strlen (fields[i]);
215 if (dst_len <= field_len)
218 strncat (dst, fields[i], dst_len);
219 dst_len -= field_len;
222 return (strlen (dst));
225 int strsubstitute (char *str, char c_from, char c_to)
246 int escape_slashes (char *buf, int buf_len)
250 if (strcmp (buf, "/") == 0)
255 strncpy (buf, "root", buf_len);
259 /* Move one to the left */
260 memmove (buf, buf + 1, buf_len - 1);
262 for (i = 0; i < buf_len - 1; i++)
266 else if (buf[i] == '/')
274 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
276 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
279 if ((tv0->tv_sec < tv1->tv_sec)
280 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
283 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
284 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
286 if (ret->tv_nsec < 0)
288 assert (ret->tv_sec > 0);
290 ret->tv_nsec += 1000000000;
297 int check_create_dir (const char *file_orig)
308 int last_is_file = 1;
309 int path_is_absolute = 0;
314 * Sanity checks first
316 if (file_orig == NULL)
319 if ((len = strlen (file_orig)) < 1)
325 * If `file_orig' ends in a slash the last component is a directory,
326 * otherwise it's a file. Act accordingly..
328 if (file_orig[len - 1] == '/')
330 if (file_orig[0] == '/')
331 path_is_absolute = 1;
334 * Create a copy for `strtok_r' to destroy
336 strncpy (file_copy, file_orig, 512);
337 file_copy[511] = '\0';
340 * Break into components. This will eat up several slashes in a row and
341 * remove leading and trailing slashes..
346 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
351 if (fields_num >= 16)
356 * For each component, do..
358 for (i = 0; i < (fields_num - last_is_file); i++)
361 * Do not create directories that start with a dot. This
362 * prevents `../../' attacks and other likely malicious
365 if (fields[i][0] == '.')
367 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
372 * Join the components together again
375 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
376 fields, i + 1, "/") < 0)
378 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
382 if (stat (dir, &statbuf) == -1)
386 if (mkdir (dir, 0755) == -1)
389 ERROR ("mkdir (%s): %s", dir,
391 errbuf, sizeof (errbuf)));
398 ERROR ("stat (%s): %s", dir,
399 sstrerror (errno, errbuf,
404 else if (!S_ISDIR (statbuf.st_mode))
406 ERROR ("stat (%s): Not a directory!", dir);
415 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
422 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
425 if (*ksp_ptr == NULL)
427 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
429 ERROR ("Cound not find kstat %s", ident);
433 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
435 WARNING ("kstat %s has wrong type", ident);
442 assert (*ksp_ptr != NULL);
443 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
446 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
448 WARNING ("kstat %s could not be read", ident);
452 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
454 WARNING ("kstat %s has wrong type", ident);
461 long long get_kstat_value (kstat_t *ksp, char *name)
464 long long retval = -1LL;
467 assert (ksp != NULL);
468 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
472 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
475 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
477 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
482 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
485 if (kn->data_type == KSTAT_DATA_INT32)
486 retval = (long long) kn->value.i32;
487 else if (kn->data_type == KSTAT_DATA_UINT32)
488 retval = (long long) kn->value.ui32;
489 else if (kn->data_type == KSTAT_DATA_INT64)
490 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
491 else if (kn->data_type == KSTAT_DATA_UINT64)
492 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
494 WARNING ("get_kstat_value: Not a numeric value: %s", name);
498 #endif /* HAVE_LIBKSTAT */
500 unsigned long long ntohll (unsigned long long n)
502 #if __BYTE_ORDER == __BIG_ENDIAN
505 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
507 } /* unsigned long long ntohll */
509 unsigned long long htonll (unsigned long long n)
511 #if __BYTE_ORDER == __BIG_ENDIAN
514 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
516 } /* unsigned long long htonll */
518 int format_name (char *ret, int ret_len,
519 const char *hostname,
520 const char *plugin, const char *plugin_instance,
521 const char *type, const char *type_instance)
525 assert (plugin != NULL);
526 assert (type != NULL);
528 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
530 if ((type_instance == NULL) || (strlen (type_instance) == 0))
531 status = snprintf (ret, ret_len, "%s/%s/%s",
532 hostname, plugin, type);
534 status = snprintf (ret, ret_len, "%s/%s/%s-%s",
535 hostname, plugin, type,
540 if ((type_instance == NULL) || (strlen (type_instance) == 0))
541 status = snprintf (ret, ret_len, "%s/%s-%s/%s",
542 hostname, plugin, plugin_instance,
545 status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
546 hostname, plugin, plugin_instance,
547 type, type_instance);
550 if ((status < 1) || (status >= ret_len))
553 } /* int format_name */