1 /**
2 * collectd - src/common.c
3 * Copyright (C) 2005-2014 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 * Niki W. Waibel <niki.waibel@gmx.net>
26 * Sebastian Harl <sh at tokkee.org>
27 * Michał Mirosław <mirq-linux at rere.qmqm.pl>
28 **/
30 #if HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include "collectd.h"
36 #include "common.h"
37 #include "plugin.h"
38 #include "utils_cache.h"
40 #ifdef HAVE_MATH_H
41 # include <math.h>
42 #endif
44 /* for getaddrinfo */
45 #include <sys/types.h>
46 #include <netdb.h>
48 #include <poll.h>
50 #if HAVE_NETINET_IN_H
51 # include <netinet/in.h>
52 #endif
54 #if HAVE_NETINET_TCP_H
55 # include <netinet/tcp.h>
56 #endif
58 /* for ntohl and htonl */
59 #if HAVE_ARPA_INET_H
60 # include <arpa/inet.h>
61 #endif
63 #if HAVE_CAPABILITY
64 # include <sys/capability.h>
65 #endif
67 #ifdef HAVE_LIBKSTAT
68 extern kstat_ctl_t *kc;
69 #endif
71 /* AIX doesn't have MSG_DONTWAIT */
72 #ifndef MSG_DONTWAIT
73 # define MSG_DONTWAIT MSG_NONBLOCK
74 #endif
76 #if !HAVE_GETPWNAM_R
77 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
78 #endif
80 #if !HAVE_STRERROR_R
81 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
82 #endif
84 char *sstrncpy (char *dest, const char *src, size_t n)
85 {
86 strncpy (dest, src, n);
87 dest[n - 1] = '\0';
89 return (dest);
90 } /* char *sstrncpy */
92 int ssnprintf (char *dest, size_t n, const char *format, ...)
93 {
94 int ret = 0;
95 va_list ap;
97 va_start (ap, format);
98 ret = vsnprintf (dest, n, format, ap);
99 dest[n - 1] = '\0';
100 va_end (ap);
102 return (ret);
103 } /* int ssnprintf */
105 char *ssnprintf_alloc (char const *format, ...) /* {{{ */
106 {
107 char static_buffer[1024] = "";
108 char *alloc_buffer;
109 size_t alloc_buffer_size;
110 int status;
111 va_list ap;
113 /* Try printing into the static buffer. In many cases it will be
114 * sufficiently large and we can simply return a strdup() of this
115 * buffer. */
116 va_start (ap, format);
117 status = vsnprintf (static_buffer, sizeof (static_buffer), format, ap);
118 va_end (ap);
119 if (status < 0)
120 return (NULL);
122 /* "status" does not include the null byte. */
123 alloc_buffer_size = (size_t) (status + 1);
124 if (alloc_buffer_size <= sizeof (static_buffer))
125 return (strdup (static_buffer));
127 /* Allocate a buffer large enough to hold the string. */
128 alloc_buffer = calloc (1, alloc_buffer_size);
129 if (alloc_buffer == NULL)
130 return (NULL);
132 /* Print again into this new buffer. */
133 va_start (ap, format);
134 status = vsnprintf (alloc_buffer, alloc_buffer_size, format, ap);
135 va_end (ap);
136 if (status < 0)
137 {
138 sfree (alloc_buffer);
139 return (NULL);
140 }
142 return (alloc_buffer);
143 } /* }}} char *ssnprintf_alloc */
145 char *sstrdup (const char *s)
146 {
147 char *r;
148 size_t sz;
150 if (s == NULL)
151 return (NULL);
153 /* Do not use `strdup' here, because it's not specified in POSIX. It's
154 * ``only'' an XSI extension. */
155 sz = strlen (s) + 1;
156 r = malloc (sz);
157 if (r == NULL)
158 {
159 ERROR ("sstrdup: Out of memory.");
160 exit (3);
161 }
162 memcpy (r, s, sizeof (char) * sz);
164 return (r);
165 } /* char *sstrdup */
167 /* Even though Posix requires "strerror_r" to return an "int",
168 * some systems (e.g. the GNU libc) return a "char *" _and_
169 * ignore the second argument ... -tokkee */
170 char *sstrerror (int errnum, char *buf, size_t buflen)
171 {
172 buf[0] = '\0';
174 #if !HAVE_STRERROR_R
175 {
176 char *temp;
178 pthread_mutex_lock (&strerror_r_lock);
180 temp = strerror (errnum);
181 sstrncpy (buf, temp, buflen);
183 pthread_mutex_unlock (&strerror_r_lock);
184 }
185 /* #endif !HAVE_STRERROR_R */
187 #elif STRERROR_R_CHAR_P
188 {
189 char *temp;
190 temp = strerror_r (errnum, buf, buflen);
191 if (buf[0] == '\0')
192 {
193 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
194 sstrncpy (buf, temp, buflen);
195 else
196 sstrncpy (buf, "strerror_r did not return "
197 "an error message", buflen);
198 }
199 }
200 /* #endif STRERROR_R_CHAR_P */
202 #else
203 if (strerror_r (errnum, buf, buflen) != 0)
204 {
205 ssnprintf (buf, buflen, "Error #%i; "
206 "Additionally, strerror_r failed.",
207 errnum);
208 }
209 #endif /* STRERROR_R_CHAR_P */
211 return (buf);
212 } /* char *sstrerror */
214 void *smalloc (size_t size)
215 {
216 void *r;
218 if ((r = malloc (size)) == NULL)
219 {
220 ERROR ("Not enough memory.");
221 exit (3);
222 }
224 return (r);
225 } /* void *smalloc */
227 #if 0
228 void sfree (void **ptr)
229 {
230 if (ptr == NULL)
231 return;
233 if (*ptr != NULL)
234 free (*ptr);
236 *ptr = NULL;
237 }
238 #endif
240 ssize_t sread (int fd, void *buf, size_t count)
241 {
242 char *ptr;
243 size_t nleft;
244 ssize_t status;
246 ptr = (char *) buf;
247 nleft = count;
249 while (nleft > 0)
250 {
251 status = read (fd, (void *) ptr, nleft);
253 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
254 continue;
256 if (status < 0)
257 return (status);
259 if (status == 0)
260 {
261 DEBUG ("Received EOF from fd %i. "
262 "Closing fd and returning error.",
263 fd);
264 close (fd);
265 return (-1);
266 }
268 assert ((0 > status) || (nleft >= (size_t)status));
270 nleft = nleft - ((size_t) status);
271 ptr = ptr + ((size_t) status);
272 }
274 return (0);
275 }
278 ssize_t swrite (int fd, const void *buf, size_t count)
279 {
280 const char *ptr;
281 size_t nleft;
282 ssize_t status;
283 struct pollfd pfd;
285 ptr = (const char *) buf;
286 nleft = count;
288 if (fd < 0)
289 return (-1);
291 /* checking for closed peer connection */
292 pfd.fd = fd;
293 pfd.events = POLLIN | POLLHUP;
294 pfd.revents = 0;
295 if (poll(&pfd, 1, 0) > 0) {
296 char buffer[32];
297 if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
298 // if recv returns zero (even though poll() said there is data to be read),
299 // that means the connection has been closed
300 return -1;
301 }
302 }
304 while (nleft > 0)
305 {
306 status = write (fd, (const void *) ptr, nleft);
308 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
309 continue;
311 if (status < 0)
312 return (status);
314 nleft = nleft - ((size_t) status);
315 ptr = ptr + ((size_t) status);
316 }
318 return (0);
319 }
321 int strsplit (char *string, char **fields, size_t size)
322 {
323 size_t i;
324 char *ptr;
325 char *saveptr;
327 i = 0;
328 ptr = string;
329 saveptr = NULL;
330 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
331 {
332 ptr = NULL;
333 i++;
335 if (i >= size)
336 break;
337 }
339 return ((int) i);
340 }
342 int strjoin (char *buffer, size_t buffer_size,
343 char **fields, size_t fields_num,
344 const char *sep)
345 {
346 size_t avail;
347 char *ptr;
348 size_t sep_len;
350 if ((buffer_size < 1) || (fields_num == 0))
351 return (-1);
353 memset (buffer, 0, buffer_size);
354 ptr = buffer;
355 avail = buffer_size - 1;
357 sep_len = 0;
358 if (sep != NULL)
359 sep_len = strlen (sep);
361 for (size_t i = 0; i < fields_num; i++)
362 {
363 size_t field_len;
365 if ((i > 0) && (sep_len > 0))
366 {
367 if (avail < sep_len)
368 return (-1);
370 memcpy (ptr, sep, sep_len);
371 ptr += sep_len;
372 avail -= sep_len;
373 }
375 field_len = strlen (fields[i]);
376 if (avail < field_len)
377 return (-1);
379 memcpy (ptr, fields[i], field_len);
380 ptr += field_len;
381 avail -= field_len;
382 }
384 assert (buffer[buffer_size - 1] == 0);
385 return ((int) strlen (buffer));
386 }
388 int escape_string (char *buffer, size_t buffer_size)
389 {
390 char *temp;
391 size_t j;
393 /* Check if we need to escape at all first */
394 temp = strpbrk (buffer, " \t\"\\");
395 if (temp == NULL)
396 return (0);
398 if (buffer_size < 3)
399 return (EINVAL);
401 temp = calloc (1, buffer_size);
402 if (temp == NULL)
403 return (ENOMEM);
405 temp[0] = '"';
406 j = 1;
408 for (size_t i = 0; i < buffer_size; i++)
409 {
410 if (buffer[i] == 0)
411 {
412 break;
413 }
414 else if ((buffer[i] == '"') || (buffer[i] == '\\'))
415 {
416 if (j > (buffer_size - 4))
417 break;
418 temp[j] = '\\';
419 temp[j + 1] = buffer[i];
420 j += 2;
421 }
422 else
423 {
424 if (j > (buffer_size - 3))
425 break;
426 temp[j] = buffer[i];
427 j++;
428 }
429 }
431 assert ((j + 1) < buffer_size);
432 temp[j] = '"';
433 temp[j + 1] = 0;
435 sstrncpy (buffer, temp, buffer_size);
436 sfree (temp);
437 return (0);
438 } /* int escape_string */
440 int strunescape (char *buf, size_t buf_len)
441 {
442 for (size_t i = 0; (i < buf_len) && (buf[i] != '\0'); ++i)
443 {
444 if (buf[i] != '\\')
445 continue;
447 if (((i + 1) >= buf_len) || (buf[i + 1] == 0)) {
448 ERROR ("string unescape: backslash found at end of string.");
449 /* Ensure null-byte at the end of the buffer. */
450 buf[i] = 0;
451 return (-1);
452 }
454 switch (buf[i + 1]) {
455 case 't':
456 buf[i] = '\t';
457 break;
458 case 'n':
459 buf[i] = '\n';
460 break;
461 case 'r':
462 buf[i] = '\r';
463 break;
464 default:
465 buf[i] = buf[i + 1];
466 break;
467 }
469 /* Move everything after the position one position to the left.
470 * Add a null-byte as last character in the buffer. */
471 memmove (buf + i + 1, buf + i + 2, buf_len - i - 2);
472 buf[buf_len - 1] = 0;
473 }
474 return (0);
475 } /* int strunescape */
477 size_t strstripnewline (char *buffer)
478 {
479 size_t buffer_len = strlen (buffer);
481 while (buffer_len > 0)
482 {
483 if ((buffer[buffer_len - 1] != '\n')
484 && (buffer[buffer_len - 1] != '\r'))
485 break;
486 buffer_len--;
487 buffer[buffer_len] = 0;
488 }
490 return (buffer_len);
491 } /* size_t strstripnewline */
493 int escape_slashes (char *buffer, size_t buffer_size)
494 {
495 size_t buffer_len;
497 buffer_len = strlen (buffer);
499 if (buffer_len <= 1)
500 {
501 if (strcmp ("/", buffer) == 0)
502 {
503 if (buffer_size < 5)
504 return (-1);
505 sstrncpy (buffer, "root", buffer_size);
506 }
507 return (0);
508 }
510 /* Move one to the left */
511 if (buffer[0] == '/')
512 {
513 memmove (buffer, buffer + 1, buffer_len);
514 buffer_len--;
515 }
517 for (size_t i = 0; i < buffer_len; i++)
518 {
519 if (buffer[i] == '/')
520 buffer[i] = '_';
521 }
523 return (0);
524 } /* int escape_slashes */
526 void replace_special (char *buffer, size_t buffer_size)
527 {
528 for (size_t i = 0; i < buffer_size; i++)
529 {
530 if (buffer[i] == 0)
531 return;
532 if ((!isalnum ((int) buffer[i])) && (buffer[i] != '-'))
533 buffer[i] = '_';
534 }
535 } /* void replace_special */
537 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta)
538 {
539 struct timeval *larger;
540 struct timeval *smaller;
542 int status;
544 NORMALIZE_TIMEVAL (tv0);
545 NORMALIZE_TIMEVAL (tv1);
547 if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec))
548 {
549 if (delta != NULL) {
550 delta->tv_sec = 0;
551 delta->tv_usec = 0;
552 }
553 return (0);
554 }
556 if ((tv0.tv_sec < tv1.tv_sec)
557 || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec)))
558 {
559 larger = &tv1;
560 smaller = &tv0;
561 status = -1;
562 }
563 else
564 {
565 larger = &tv0;
566 smaller = &tv1;
567 status = 1;
568 }
570 if (delta != NULL) {
571 delta->tv_sec = larger->tv_sec - smaller->tv_sec;
573 if (smaller->tv_usec <= larger->tv_usec)
574 delta->tv_usec = larger->tv_usec - smaller->tv_usec;
575 else
576 {
577 --delta->tv_sec;
578 delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec;
579 }
580 }
582 assert ((delta == NULL)
583 || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000)));
585 return (status);
586 } /* int timeval_cmp */
588 int check_create_dir (const char *file_orig)
589 {
590 struct stat statbuf;
592 char file_copy[512];
593 char dir[512];
594 int dir_len = 512;
595 char *fields[16];
596 int fields_num;
597 char *ptr;
598 char *saveptr;
599 int last_is_file = 1;
600 int path_is_absolute = 0;
601 size_t len;
603 /*
604 * Sanity checks first
605 */
606 if (file_orig == NULL)
607 return (-1);
609 if ((len = strlen (file_orig)) < 1)
610 return (-1);
611 else if (len >= sizeof (file_copy))
612 return (-1);
614 /*
615 * If `file_orig' ends in a slash the last component is a directory,
616 * otherwise it's a file. Act accordingly..
617 */
618 if (file_orig[len - 1] == '/')
619 last_is_file = 0;
620 if (file_orig[0] == '/')
621 path_is_absolute = 1;
623 /*
624 * Create a copy for `strtok_r' to destroy
625 */
626 sstrncpy (file_copy, file_orig, sizeof (file_copy));
628 /*
629 * Break into components. This will eat up several slashes in a row and
630 * remove leading and trailing slashes..
631 */
632 ptr = file_copy;
633 saveptr = NULL;
634 fields_num = 0;
635 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
636 {
637 ptr = NULL;
638 fields_num++;
640 if (fields_num >= 16)
641 break;
642 }
644 /*
645 * For each component, do..
646 */
647 for (int i = 0; i < (fields_num - last_is_file); i++)
648 {
649 /*
650 * Do not create directories that start with a dot. This
651 * prevents `../../' attacks and other likely malicious
652 * behavior.
653 */
654 if (fields[i][0] == '.')
655 {
656 ERROR ("Cowardly refusing to create a directory that "
657 "begins with a `.' (dot): `%s'", file_orig);
658 return (-2);
659 }
661 /*
662 * Join the components together again
663 */
664 dir[0] = '/';
665 if (strjoin (dir + path_is_absolute, (size_t) (dir_len - path_is_absolute),
666 fields, (size_t) (i + 1), "/") < 0)
667 {
668 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
669 return (-1);
670 }
672 while (42) {
673 if ((stat (dir, &statbuf) == -1)
674 && (lstat (dir, &statbuf) == -1))
675 {
676 if (errno == ENOENT)
677 {
678 if (mkdir (dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0)
679 break;
681 /* this might happen, if a different thread created
682 * the directory in the meantime
683 * => call stat() again to check for S_ISDIR() */
684 if (EEXIST == errno)
685 continue;
687 char errbuf[1024];
688 ERROR ("check_create_dir: mkdir (%s): %s", dir,
689 sstrerror (errno,
690 errbuf, sizeof (errbuf)));
691 return (-1);
692 }
693 else
694 {
695 char errbuf[1024];
696 ERROR ("check_create_dir: stat (%s): %s", dir,
697 sstrerror (errno, errbuf,
698 sizeof (errbuf)));
699 return (-1);
700 }
701 }
702 else if (!S_ISDIR (statbuf.st_mode))
703 {
704 ERROR ("check_create_dir: `%s' exists but is not "
705 "a directory!", dir);
706 return (-1);
707 }
708 break;
709 }
710 }
712 return (0);
713 } /* check_create_dir */
715 #ifdef HAVE_LIBKSTAT
716 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
717 {
718 char ident[128];
720 *ksp_ptr = NULL;
722 if (kc == NULL)
723 return (-1);
725 ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
727 *ksp_ptr = kstat_lookup (kc, module, instance, name);
728 if (*ksp_ptr == NULL)
729 {
730 ERROR ("get_kstat: Cound not find kstat %s", ident);
731 return (-1);
732 }
734 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
735 {
736 ERROR ("get_kstat: kstat %s has wrong type", ident);
737 *ksp_ptr = NULL;
738 return (-1);
739 }
741 #ifdef assert
742 assert (*ksp_ptr != NULL);
743 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
744 #endif
746 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
747 {
748 ERROR ("get_kstat: kstat %s could not be read", ident);
749 return (-1);
750 }
752 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
753 {
754 ERROR ("get_kstat: kstat %s has wrong type", ident);
755 return (-1);
756 }
758 return (0);
759 }
761 long long get_kstat_value (kstat_t *ksp, char *name)
762 {
763 kstat_named_t *kn;
764 long long retval = -1LL;
766 if (ksp == NULL)
767 {
768 ERROR ("get_kstat_value (\"%s\"): ksp is NULL.", name);
769 return (-1LL);
770 }
771 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
772 {
773 ERROR ("get_kstat_value (\"%s\"): ksp->ks_type (%#x) "
774 "is not KSTAT_TYPE_NAMED (%#x).",
775 name,
776 (unsigned int) ksp->ks_type,
777 (unsigned int) KSTAT_TYPE_NAMED);
778 return (-1LL);
779 }
781 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
782 return (-1LL);
784 if (kn->data_type == KSTAT_DATA_INT32)
785 retval = (long long) kn->value.i32;
786 else if (kn->data_type == KSTAT_DATA_UINT32)
787 retval = (long long) kn->value.ui32;
788 else if (kn->data_type == KSTAT_DATA_INT64)
789 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
790 else if (kn->data_type == KSTAT_DATA_UINT64)
791 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
792 else
793 WARNING ("get_kstat_value: Not a numeric value: %s", name);
795 return (retval);
796 }
797 #endif /* HAVE_LIBKSTAT */
799 #ifndef HAVE_HTONLL
800 unsigned long long ntohll (unsigned long long n)
801 {
802 #if BYTE_ORDER == BIG_ENDIAN
803 return (n);
804 #else
805 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
806 #endif
807 } /* unsigned long long ntohll */
809 unsigned long long htonll (unsigned long long n)
810 {
811 #if BYTE_ORDER == BIG_ENDIAN
812 return (n);
813 #else
814 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
815 #endif
816 } /* unsigned long long htonll */
817 #endif /* HAVE_HTONLL */
819 #if FP_LAYOUT_NEED_NOTHING
820 /* Well, we need nothing.. */
821 /* #endif FP_LAYOUT_NEED_NOTHING */
823 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
824 # if FP_LAYOUT_NEED_ENDIANFLIP
825 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
826 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
827 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
828 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
829 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
830 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
831 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
832 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
833 # else
834 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
835 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
836 # endif
838 double ntohd (double d)
839 {
840 union
841 {
842 uint8_t byte[8];
843 uint64_t integer;
844 double floating;
845 } ret;
847 ret.floating = d;
849 /* NAN in x86 byte order */
850 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
851 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
852 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
853 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
854 {
855 return (NAN);
856 }
857 else
858 {
859 uint64_t tmp;
861 tmp = ret.integer;
862 ret.integer = FP_CONVERT (tmp);
863 return (ret.floating);
864 }
865 } /* double ntohd */
867 double htond (double d)
868 {
869 union
870 {
871 uint8_t byte[8];
872 uint64_t integer;
873 double floating;
874 } ret;
876 if (isnan (d))
877 {
878 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
879 ret.byte[4] = ret.byte[5] = 0x00;
880 ret.byte[6] = 0xf8;
881 ret.byte[7] = 0x7f;
882 return (ret.floating);
883 }
884 else
885 {
886 uint64_t tmp;
888 ret.floating = d;
889 tmp = FP_CONVERT (ret.integer);
890 ret.integer = tmp;
891 return (ret.floating);
892 }
893 } /* double htond */
894 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
896 int format_name (char *ret, int ret_len,
897 const char *hostname,
898 const char *plugin, const char *plugin_instance,
899 const char *type, const char *type_instance)
900 {
901 char *buffer;
902 size_t buffer_size;
904 buffer = ret;
905 buffer_size = (size_t) ret_len;
907 #define APPEND(str) do { \
908 size_t l = strlen (str); \
909 if (l >= buffer_size) \
910 return (ENOBUFS); \
911 memcpy (buffer, (str), l); \
912 buffer += l; buffer_size -= l; \
913 } while (0)
915 assert (plugin != NULL);
916 assert (type != NULL);
918 APPEND (hostname);
919 APPEND ("/");
920 APPEND (plugin);
921 if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
922 {
923 APPEND ("-");
924 APPEND (plugin_instance);
925 }
926 APPEND ("/");
927 APPEND (type);
928 if ((type_instance != NULL) && (type_instance[0] != 0))
929 {
930 APPEND ("-");
931 APPEND (type_instance);
932 }
933 assert (buffer_size > 0);
934 buffer[0] = 0;
936 #undef APPEND
937 return (0);
938 } /* int format_name */
940 int format_values (char *ret, size_t ret_len, /* {{{ */
941 const data_set_t *ds, const value_list_t *vl,
942 _Bool store_rates)
943 {
944 size_t offset = 0;
945 int status;
946 gauge_t *rates = NULL;
948 assert (0 == strcmp (ds->type, vl->type));
950 memset (ret, 0, ret_len);
952 #define BUFFER_ADD(...) do { \
953 status = ssnprintf (ret + offset, ret_len - offset, \
954 __VA_ARGS__); \
955 if (status < 1) \
956 { \
957 sfree (rates); \
958 return (-1); \
959 } \
960 else if (((size_t) status) >= (ret_len - offset)) \
961 { \
962 sfree (rates); \
963 return (-1); \
964 } \
965 else \
966 offset += ((size_t) status); \
967 } while (0)
969 BUFFER_ADD ("%.3f", CDTIME_T_TO_DOUBLE (vl->time));
971 for (size_t i = 0; i < ds->ds_num; i++)
972 {
973 if (ds->ds[i].type == DS_TYPE_GAUGE)
974 BUFFER_ADD (":"GAUGE_FORMAT, vl->values[i].gauge);
975 else if (store_rates)
976 {
977 if (rates == NULL)
978 rates = uc_get_rate (ds, vl);
979 if (rates == NULL)
980 {
981 WARNING ("format_values: uc_get_rate failed.");
982 return (-1);
983 }
984 BUFFER_ADD (":"GAUGE_FORMAT, rates[i]);
985 }
986 else if (ds->ds[i].type == DS_TYPE_COUNTER)
987 BUFFER_ADD (":%llu", vl->values[i].counter);
988 else if (ds->ds[i].type == DS_TYPE_DERIVE)
989 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
990 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
991 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
992 else
993 {
994 ERROR ("format_values: Unknown data source type: %i",
995 ds->ds[i].type);
996 sfree (rates);
997 return (-1);
998 }
999 } /* for ds->ds_num */
1001 #undef BUFFER_ADD
1003 sfree (rates);
1004 return (0);
1005 } /* }}} int format_values */
1007 int parse_identifier (char *str, char **ret_host,
1008 char **ret_plugin, char **ret_plugin_instance,
1009 char **ret_type, char **ret_type_instance)
1010 {
1011 char *hostname = NULL;
1012 char *plugin = NULL;
1013 char *plugin_instance = NULL;
1014 char *type = NULL;
1015 char *type_instance = NULL;
1017 hostname = str;
1018 if (hostname == NULL)
1019 return (-1);
1021 plugin = strchr (hostname, '/');
1022 if (plugin == NULL)
1023 return (-1);
1024 *plugin = '\0'; plugin++;
1026 type = strchr (plugin, '/');
1027 if (type == NULL)
1028 return (-1);
1029 *type = '\0'; type++;
1031 plugin_instance = strchr (plugin, '-');
1032 if (plugin_instance != NULL)
1033 {
1034 *plugin_instance = '\0';
1035 plugin_instance++;
1036 }
1038 type_instance = strchr (type, '-');
1039 if (type_instance != NULL)
1040 {
1041 *type_instance = '\0';
1042 type_instance++;
1043 }
1045 *ret_host = hostname;
1046 *ret_plugin = plugin;
1047 *ret_plugin_instance = plugin_instance;
1048 *ret_type = type;
1049 *ret_type_instance = type_instance;
1050 return (0);
1051 } /* int parse_identifier */
1053 int parse_identifier_vl (const char *str, value_list_t *vl) /* {{{ */
1054 {
1055 char str_copy[6 * DATA_MAX_NAME_LEN];
1056 char *host = NULL;
1057 char *plugin = NULL;
1058 char *plugin_instance = NULL;
1059 char *type = NULL;
1060 char *type_instance = NULL;
1061 int status;
1063 if ((str == NULL) || (vl == NULL))
1064 return (EINVAL);
1066 sstrncpy (str_copy, str, sizeof (str_copy));
1068 status = parse_identifier (str_copy, &host,
1069 &plugin, &plugin_instance,
1070 &type, &type_instance);
1071 if (status != 0)
1072 return (status);
1074 sstrncpy (vl->host, host, sizeof (vl->host));
1075 sstrncpy (vl->plugin, plugin, sizeof (vl->plugin));
1076 sstrncpy (vl->plugin_instance,
1077 (plugin_instance != NULL) ? plugin_instance : "",
1078 sizeof (vl->plugin_instance));
1079 sstrncpy (vl->type, type, sizeof (vl->type));
1080 sstrncpy (vl->type_instance,
1081 (type_instance != NULL) ? type_instance : "",
1082 sizeof (vl->type_instance));
1084 return (0);
1085 } /* }}} int parse_identifier_vl */
1087 int parse_value (const char *value_orig, value_t *ret_value, int ds_type)
1088 {
1089 char *value;
1090 char *endptr = NULL;
1091 size_t value_len;
1093 if (value_orig == NULL)
1094 return (EINVAL);
1096 value = strdup (value_orig);
1097 if (value == NULL)
1098 return (ENOMEM);
1099 value_len = strlen (value);
1101 while ((value_len > 0) && isspace ((int) value[value_len - 1]))
1102 {
1103 value[value_len - 1] = 0;
1104 value_len--;
1105 }
1107 switch (ds_type)
1108 {
1109 case DS_TYPE_COUNTER:
1110 ret_value->counter = (counter_t) strtoull (value, &endptr, 0);
1111 break;
1113 case DS_TYPE_GAUGE:
1114 ret_value->gauge = (gauge_t) strtod (value, &endptr);
1115 break;
1117 case DS_TYPE_DERIVE:
1118 ret_value->derive = (derive_t) strtoll (value, &endptr, 0);
1119 break;
1121 case DS_TYPE_ABSOLUTE:
1122 ret_value->absolute = (absolute_t) strtoull (value, &endptr, 0);
1123 break;
1125 default:
1126 sfree (value);
1127 ERROR ("parse_value: Invalid data source type: %i.", ds_type);
1128 return -1;
1129 }
1131 if (value == endptr) {
1132 ERROR ("parse_value: Failed to parse string as %s: \"%s\".",
1133 DS_TYPE_TO_STRING (ds_type), value);
1134 sfree (value);
1135 return -1;
1136 }
1137 else if ((NULL != endptr) && ('\0' != *endptr))
1138 INFO ("parse_value: Ignoring trailing garbage \"%s\" after %s value. "
1139 "Input string was \"%s\".",
1140 endptr, DS_TYPE_TO_STRING (ds_type), value_orig);
1142 sfree (value);
1143 return 0;
1144 } /* int parse_value */
1146 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
1147 {
1148 size_t i;
1149 char *dummy;
1150 char *ptr;
1151 char *saveptr;
1153 if ((buffer == NULL) || (vl == NULL) || (ds == NULL))
1154 return EINVAL;
1156 i = 0;
1157 dummy = buffer;
1158 saveptr = NULL;
1159 vl->time = 0;
1160 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
1161 {
1162 dummy = NULL;
1164 if (i >= vl->values_len)
1165 {
1166 /* Make sure i is invalid. */
1167 i = 0;
1168 break;
1169 }
1171 if (vl->time == 0)
1172 {
1173 if (strcmp ("N", ptr) == 0)
1174 vl->time = cdtime ();
1175 else
1176 {
1177 char *endptr = NULL;
1178 double tmp;
1180 errno = 0;
1181 tmp = strtod (ptr, &endptr);
1182 if ((errno != 0) /* Overflow */
1183 || (endptr == ptr) /* Invalid string */
1184 || (endptr == NULL) /* This should not happen */
1185 || (*endptr != 0)) /* Trailing chars */
1186 return (-1);
1188 vl->time = DOUBLE_TO_CDTIME_T (tmp);
1189 }
1191 continue;
1192 }
1194 if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE))
1195 vl->values[i].gauge = NAN;
1196 else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i].type))
1197 return -1;
1199 i++;
1200 } /* while (strtok_r) */
1202 if ((ptr != NULL) || (i == 0))
1203 return (-1);
1204 return (0);
1205 } /* int parse_values */
1207 #if !HAVE_GETPWNAM_R
1208 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
1209 size_t buflen, struct passwd **pwbufp)
1210 {
1211 int status = 0;
1212 struct passwd *pw;
1214 memset (pwbuf, '\0', sizeof (struct passwd));
1216 pthread_mutex_lock (&getpwnam_r_lock);
1218 do
1219 {
1220 pw = getpwnam (name);
1221 if (pw == NULL)
1222 {
1223 status = (errno != 0) ? errno : ENOENT;
1224 break;
1225 }
1227 #define GETPWNAM_COPY_MEMBER(member) \
1228 if (pw->member != NULL) \
1229 { \
1230 int len = strlen (pw->member); \
1231 if (len >= buflen) \
1232 { \
1233 status = ENOMEM; \
1234 break; \
1235 } \
1236 sstrncpy (buf, pw->member, buflen); \
1237 pwbuf->member = buf; \
1238 buf += (len + 1); \
1239 buflen -= (len + 1); \
1240 }
1241 GETPWNAM_COPY_MEMBER(pw_name);
1242 GETPWNAM_COPY_MEMBER(pw_passwd);
1243 GETPWNAM_COPY_MEMBER(pw_gecos);
1244 GETPWNAM_COPY_MEMBER(pw_dir);
1245 GETPWNAM_COPY_MEMBER(pw_shell);
1247 pwbuf->pw_uid = pw->pw_uid;
1248 pwbuf->pw_gid = pw->pw_gid;
1250 if (pwbufp != NULL)
1251 *pwbufp = pwbuf;
1252 } while (0);
1254 pthread_mutex_unlock (&getpwnam_r_lock);
1256 return (status);
1257 } /* int getpwnam_r */
1258 #endif /* !HAVE_GETPWNAM_R */
1260 int notification_init (notification_t *n, int severity, const char *message,
1261 const char *host,
1262 const char *plugin, const char *plugin_instance,
1263 const char *type, const char *type_instance)
1264 {
1265 memset (n, '\0', sizeof (notification_t));
1267 n->severity = severity;
1269 if (message != NULL)
1270 sstrncpy (n->message, message, sizeof (n->message));
1271 if (host != NULL)
1272 sstrncpy (n->host, host, sizeof (n->host));
1273 if (plugin != NULL)
1274 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
1275 if (plugin_instance != NULL)
1276 sstrncpy (n->plugin_instance, plugin_instance,
1277 sizeof (n->plugin_instance));
1278 if (type != NULL)
1279 sstrncpy (n->type, type, sizeof (n->type));
1280 if (type_instance != NULL)
1281 sstrncpy (n->type_instance, type_instance,
1282 sizeof (n->type_instance));
1284 return (0);
1285 } /* int notification_init */
1287 int walk_directory (const char *dir, dirwalk_callback_f callback,
1288 void *user_data, int include_hidden)
1289 {
1290 struct dirent *ent;
1291 DIR *dh;
1292 int success;
1293 int failure;
1295 success = 0;
1296 failure = 0;
1298 if ((dh = opendir (dir)) == NULL)
1299 {
1300 char errbuf[1024];
1301 ERROR ("walk_directory: Cannot open '%s': %s", dir,
1302 sstrerror (errno, errbuf, sizeof (errbuf)));
1303 return -1;
1304 }
1306 while ((ent = readdir (dh)) != NULL)
1307 {
1308 int status;
1310 if (include_hidden)
1311 {
1312 if ((strcmp (".", ent->d_name) == 0)
1313 || (strcmp ("..", ent->d_name) == 0))
1314 continue;
1315 }
1316 else /* if (!include_hidden) */
1317 {
1318 if (ent->d_name[0]=='.')
1319 continue;
1320 }
1322 status = (*callback) (dir, ent->d_name, user_data);
1323 if (status != 0)
1324 failure++;
1325 else
1326 success++;
1327 }
1329 closedir (dh);
1331 if ((success == 0) && (failure > 0))
1332 return (-1);
1333 return (0);
1334 }
1336 ssize_t read_file_contents (const char *filename, char *buf, size_t bufsize)
1337 {
1338 FILE *fh;
1339 ssize_t ret;
1341 fh = fopen (filename, "r");
1342 if (fh == NULL)
1343 return (-1);
1345 ret = (ssize_t) fread (buf, 1, bufsize, fh);
1346 if ((ret == 0) && (ferror (fh) != 0))
1347 {
1348 ERROR ("read_file_contents: Reading file \"%s\" failed.",
1349 filename);
1350 ret = -1;
1351 }
1353 fclose(fh);
1354 return (ret);
1355 }
1357 counter_t counter_diff (counter_t old_value, counter_t new_value)
1358 {
1359 counter_t diff;
1361 if (old_value > new_value)
1362 {
1363 if (old_value <= 4294967295U)
1364 diff = (4294967295U - old_value) + new_value + 1;
1365 else
1366 diff = (18446744073709551615ULL - old_value) + new_value + 1;
1367 }
1368 else
1369 {
1370 diff = new_value - old_value;
1371 }
1373 return (diff);
1374 } /* counter_t counter_diff */
1376 int rate_to_value (value_t *ret_value, gauge_t rate, /* {{{ */
1377 rate_to_value_state_t *state,
1378 int ds_type, cdtime_t t)
1379 {
1380 gauge_t delta_gauge;
1381 cdtime_t delta_t;
1383 if (ds_type == DS_TYPE_GAUGE)
1384 {
1385 state->last_value.gauge = rate;
1386 state->last_time = t;
1388 *ret_value = state->last_value;
1389 return (0);
1390 }
1392 /* Counter and absolute can't handle negative rates. Reset "last time"
1393 * to zero, so that the next valid rate will re-initialize the
1394 * structure. */
1395 if ((rate < 0.0)
1396 && ((ds_type == DS_TYPE_COUNTER)
1397 || (ds_type == DS_TYPE_ABSOLUTE)))
1398 {
1399 memset (state, 0, sizeof (*state));
1400 return (EINVAL);
1401 }
1403 /* Another invalid state: The time is not increasing. */
1404 if (t <= state->last_time)
1405 {
1406 memset (state, 0, sizeof (*state));
1407 return (EINVAL);
1408 }
1410 delta_t = t - state->last_time;
1411 delta_gauge = (rate * CDTIME_T_TO_DOUBLE (delta_t)) + state->residual;
1413 /* Previous value is invalid. */
1414 if (state->last_time == 0) /* {{{ */
1415 {
1416 if (ds_type == DS_TYPE_DERIVE)
1417 {
1418 state->last_value.derive = (derive_t) rate;
1419 state->residual = rate - ((gauge_t) state->last_value.derive);
1420 }
1421 else if (ds_type == DS_TYPE_COUNTER)
1422 {
1423 state->last_value.counter = (counter_t) rate;
1424 state->residual = rate - ((gauge_t) state->last_value.counter);
1425 }
1426 else if (ds_type == DS_TYPE_ABSOLUTE)
1427 {
1428 state->last_value.absolute = (absolute_t) rate;
1429 state->residual = rate - ((gauge_t) state->last_value.absolute);
1430 }
1431 else
1432 {
1433 assert (23 == 42);
1434 }
1436 state->last_time = t;
1437 return (EAGAIN);
1438 } /* }}} */
1440 if (ds_type == DS_TYPE_DERIVE)
1441 {
1442 derive_t delta_derive = (derive_t) delta_gauge;
1444 state->last_value.derive += delta_derive;
1445 state->residual = delta_gauge - ((gauge_t) delta_derive);
1446 }
1447 else if (ds_type == DS_TYPE_COUNTER)
1448 {
1449 counter_t delta_counter = (counter_t) delta_gauge;
1451 state->last_value.counter += delta_counter;
1452 state->residual = delta_gauge - ((gauge_t) delta_counter);
1453 }
1454 else if (ds_type == DS_TYPE_ABSOLUTE)
1455 {
1456 absolute_t delta_absolute = (absolute_t) delta_gauge;
1458 state->last_value.absolute = delta_absolute;
1459 state->residual = delta_gauge - ((gauge_t) delta_absolute);
1460 }
1461 else
1462 {
1463 assert (23 == 42);
1464 }
1466 state->last_time = t;
1467 *ret_value = state->last_value;
1468 return (0);
1469 } /* }}} value_t rate_to_value */
1471 int value_to_rate (gauge_t *ret_rate, /* {{{ */
1472 value_t value, int ds_type, cdtime_t t, value_to_rate_state_t *state)
1473 {
1474 gauge_t interval;
1476 /* Another invalid state: The time is not increasing. */
1477 if (t <= state->last_time)
1478 {
1479 memset (state, 0, sizeof (*state));
1480 return (EINVAL);
1481 }
1483 interval = CDTIME_T_TO_DOUBLE(t - state->last_time);
1485 /* Previous value is invalid. */
1486 if (state->last_time == 0)
1487 {
1488 state->last_value = value;
1489 state->last_time = t;
1490 return (EAGAIN);
1491 }
1493 switch (ds_type) {
1494 case DS_TYPE_DERIVE: {
1495 derive_t diff = value.derive - state->last_value.derive;
1496 *ret_rate = ((gauge_t) diff) / ((gauge_t) interval);
1497 break;
1498 }
1499 case DS_TYPE_GAUGE: {
1500 *ret_rate = value.gauge;
1501 break;
1502 }
1503 case DS_TYPE_COUNTER: {
1504 counter_t diff = counter_diff (state->last_value.counter, value.counter);
1505 *ret_rate = ((gauge_t) diff) / ((gauge_t) interval);
1506 break;
1507 }
1508 case DS_TYPE_ABSOLUTE: {
1509 absolute_t diff = value.absolute;
1510 *ret_rate = ((gauge_t) diff) / ((gauge_t) interval);
1511 break;
1512 }
1513 default:
1514 return EINVAL;
1515 }
1517 state->last_value = value;
1518 state->last_time = t;
1519 return (0);
1520 } /* }}} value_t rate_to_value */
1522 int service_name_to_port_number (const char *service_name)
1523 {
1524 struct addrinfo *ai_list;
1525 int status;
1526 int service_number;
1528 if (service_name == NULL)
1529 return (-1);
1531 struct addrinfo ai_hints = {
1532 .ai_family = AF_UNSPEC
1533 };
1535 status = getaddrinfo (/* node = */ NULL, service_name,
1536 &ai_hints, &ai_list);
1537 if (status != 0)
1538 {
1539 ERROR ("service_name_to_port_number: getaddrinfo failed: %s",
1540 gai_strerror (status));
1541 return (-1);
1542 }
1544 service_number = -1;
1545 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1546 {
1547 if (ai_ptr->ai_family == AF_INET)
1548 {
1549 struct sockaddr_in *sa;
1551 sa = (void *) ai_ptr->ai_addr;
1552 service_number = (int) ntohs (sa->sin_port);
1553 }
1554 else if (ai_ptr->ai_family == AF_INET6)
1555 {
1556 struct sockaddr_in6 *sa;
1558 sa = (void *) ai_ptr->ai_addr;
1559 service_number = (int) ntohs (sa->sin6_port);
1560 }
1562 if ((service_number > 0) && (service_number <= 65535))
1563 break;
1564 }
1566 freeaddrinfo (ai_list);
1568 if ((service_number > 0) && (service_number <= 65535))
1569 return (service_number);
1570 return (-1);
1571 } /* int service_name_to_port_number */
1573 void set_sock_opts (int sockfd) /* {{{ */
1574 {
1575 int status;
1576 int socktype;
1578 socklen_t socklen = sizeof (socklen_t);
1579 int so_keepalive = 1;
1581 status = getsockopt (sockfd, SOL_SOCKET, SO_TYPE, &socktype, &socklen);
1582 if (status != 0)
1583 {
1584 WARNING ("set_sock_opts: failed to determine socket type");
1585 return;
1586 }
1588 if (socktype == SOCK_STREAM)
1589 {
1590 status = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
1591 &so_keepalive, sizeof (so_keepalive));
1592 if (status != 0)
1593 WARNING ("set_sock_opts: failed to set socket keepalive flag");
1595 #ifdef TCP_KEEPIDLE
1596 int tcp_keepidle = ((CDTIME_T_TO_MS(plugin_get_interval()) - 1) / 100 + 1);
1597 status = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE,
1598 &tcp_keepidle, sizeof (tcp_keepidle));
1599 if (status != 0)
1600 WARNING ("set_sock_opts: failed to set socket tcp keepalive time");
1601 #endif
1603 #ifdef TCP_KEEPINTVL
1604 int tcp_keepintvl = ((CDTIME_T_TO_MS(plugin_get_interval()) - 1) / 1000 + 1);
1605 status = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL,
1606 &tcp_keepintvl, sizeof (tcp_keepintvl));
1607 if (status != 0)
1608 WARNING ("set_sock_opts: failed to set socket tcp keepalive interval");
1609 #endif
1610 }
1611 } /* }}} void set_sock_opts */
1613 int strtoderive (const char *string, derive_t *ret_value) /* {{{ */
1614 {
1615 derive_t tmp;
1616 char *endptr;
1618 if ((string == NULL) || (ret_value == NULL))
1619 return (EINVAL);
1621 errno = 0;
1622 endptr = NULL;
1623 tmp = (derive_t) strtoll (string, &endptr, /* base = */ 0);
1624 if ((endptr == string) || (errno != 0))
1625 return (-1);
1627 *ret_value = tmp;
1628 return (0);
1629 } /* }}} int strtoderive */
1631 int strtogauge (const char *string, gauge_t *ret_value) /* {{{ */
1632 {
1633 gauge_t tmp;
1634 char *endptr = NULL;
1636 if ((string == NULL) || (ret_value == NULL))
1637 return (EINVAL);
1639 errno = 0;
1640 endptr = NULL;
1641 tmp = (gauge_t) strtod (string, &endptr);
1642 if (errno != 0)
1643 return (errno);
1644 else if ((endptr == NULL) || (*endptr != 0))
1645 return (EINVAL);
1647 *ret_value = tmp;
1648 return (0);
1649 } /* }}} int strtogauge */
1651 int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str) /* {{{ */
1652 {
1653 char **array;
1654 size_t array_len = *ret_array_len;
1656 if (str == NULL)
1657 return (EINVAL);
1659 array = realloc (*ret_array,
1660 (array_len + 1) * sizeof (*array));
1661 if (array == NULL)
1662 return (ENOMEM);
1663 *ret_array = array;
1665 array[array_len] = strdup (str);
1666 if (array[array_len] == NULL)
1667 return (ENOMEM);
1669 array_len++;
1670 *ret_array_len = array_len;
1671 return (0);
1672 } /* }}} int strarray_add */
1674 void strarray_free (char **array, size_t array_len) /* {{{ */
1675 {
1676 for (size_t i = 0; i < array_len; i++)
1677 sfree (array[i]);
1678 sfree (array);
1679 } /* }}} void strarray_free */
1681 #if HAVE_CAPABILITY
1682 int check_capability (int arg) /* {{{ */
1683 {
1684 cap_value_t cap = (cap_value_t) arg;
1686 if (!CAP_IS_SUPPORTED (cap))
1687 return (-1);
1689 int have_cap = cap_get_bound (cap);
1690 if (have_cap != 1)
1691 return (-1);
1693 return (0);
1694 } /* }}} int check_capability */
1695 #else
1696 int check_capability (__attribute__((unused)) int arg) /* {{{ */
1697 {
1698 WARNING ("check_capability: unsupported capability implementation. "
1699 "Some plugin(s) may require elevated privileges to work properly.");
1700 return (0);
1701 } /* }}} int check_capability */
1702 #endif /* HAVE_CAPABILITY */