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>
24 #include "utils_debug.h"
30 /* for ntohl and htonl */
32 # include <arpa/inet.h>
35 extern int operating_mode;
38 extern kstat_ctl_t *kc;
41 void sstrncpy (char *d, const char *s, int len)
47 char *sstrdup (const char *s)
54 if((r = strdup (s)) == NULL)
56 DBG ("Not enough memory.");
63 void *smalloc (size_t size)
67 if ((r = malloc (size)) == NULL)
69 DBG("Not enough memory.");
77 void sfree (void **ptr)
89 ssize_t sread (int fd, void *buf, size_t count)
100 status = read (fd, (void *) ptr, nleft);
102 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
110 DBG ("Received EOF from fd %i. "
111 "Closing fd and returning error.",
117 assert (nleft >= status);
119 nleft = nleft - status;
127 ssize_t swrite (int fd, const void *buf, size_t count)
133 ptr = (const char *) buf;
138 status = write (fd, (const void *) ptr, nleft);
140 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
146 nleft = nleft - status;
153 int strsplit (char *string, char **fields, size_t size)
160 while ((fields[i] = strtok (ptr, " \t")) != NULL)
172 int strjoin (char *dst, size_t dst_len,
173 char **fields, size_t fields_num,
180 memset (dst, '\0', dst_len);
187 sep_len = strlen (sep);
189 for (i = 0; i < fields_num; i++)
191 if ((i > 0) && (sep_len > 0))
193 if (dst_len <= sep_len)
196 strncat (dst, sep, dst_len);
200 field_len = strlen (fields[i]);
202 if (dst_len <= field_len)
205 strncat (dst, fields[i], dst_len);
206 dst_len -= field_len;
209 return (strlen (dst));
212 int strsubstitute (char *str, char c_from, char c_to)
233 int escape_slashes (char *buf, int buf_len)
237 if (strcmp (buf, "/") == 0)
242 strncpy (buf, "root", buf_len);
246 /* Move one to the left */
247 memmove (buf, buf + 1, buf_len - 1);
249 for (i = 0; i < buf_len - 1; i++)
253 else if (buf[i] == '/')
261 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
263 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
266 if ((tv0->tv_sec < tv1->tv_sec)
267 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
270 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
271 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
273 if (ret->tv_nsec < 0)
275 assert (ret->tv_sec > 0);
277 ret->tv_nsec += 1000000000;
284 int check_create_dir (const char *file_orig)
294 int last_is_file = 1;
299 * Sanity checks first
301 if (file_orig == NULL)
304 if ((len = strlen (file_orig)) < 1)
310 * If `file_orig' ends in a slash the last component is a directory,
311 * otherwise it's a file. Act accordingly..
313 if (file_orig[len - 1] == '/')
317 * Create a copy for `strtok' to destroy
319 strncpy (file_copy, file_orig, 512);
320 file_copy[511] = '\0';
323 * Break into components. This will eat up several slashes in a row and
324 * remove leading and trailing slashes..
328 while ((fields[fields_num] = strtok (ptr, "/")) != NULL)
333 if (fields_num >= 16)
338 * For each component, do..
340 for (i = 0; i < (fields_num - last_is_file); i++)
343 * Do not create directories that start with a dot. This
344 * prevents `../../' attacks and other likely malicious
347 if (fields[i][0] == '.')
349 syslog (LOG_ERR, "Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
354 * Join the components together again
356 if (strjoin (dir, dir_len, fields, i + 1, "/") < 0)
358 syslog (LOG_ERR, "strjoin failed: `%s', component #%i", file_orig, i);
362 if (stat (dir, &statbuf) == -1)
366 if (mkdir (dir, 0755) == -1)
368 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
374 syslog (LOG_ERR, "stat (%s): %s", dir, strerror (errno));
378 else if (!S_ISDIR (statbuf.st_mode))
380 syslog (LOG_ERR, "stat (%s): Not a directory!", dir);
389 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
396 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
399 if (*ksp_ptr == NULL)
401 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
403 syslog (LOG_ERR, "Cound not find kstat %s", ident);
407 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
409 syslog (LOG_WARNING, "kstat %s has wrong type", ident);
416 assert (*ksp_ptr != NULL);
417 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
420 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
422 syslog (LOG_WARNING, "kstat %s could not be read", ident);
426 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
428 syslog (LOG_WARNING, "kstat %s has wrong type", ident);
435 long long get_kstat_value (kstat_t *ksp, char *name)
438 long long retval = -1LL;
441 assert (ksp != NULL);
442 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
446 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
449 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
451 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
456 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
459 if (kn->data_type == KSTAT_DATA_INT32)
460 retval = (long long) kn->value.i32;
461 else if (kn->data_type == KSTAT_DATA_UINT32)
462 retval = (long long) kn->value.ui32;
463 else if (kn->data_type == KSTAT_DATA_INT64)
464 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
465 else if (kn->data_type == KSTAT_DATA_UINT64)
466 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
468 syslog (LOG_WARNING, "get_kstat_value: Not a numeric value: %s", name);
472 #endif /* HAVE_LIBKSTAT */
474 unsigned long long ntohll (unsigned long long n)
476 #if __BYTE_ORDER == __BIG_ENDIAN
479 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
483 unsigned long long htonll (unsigned long long n)
485 #if __BYTE_ORDER == __BIG_ENDIAN
488 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);