Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / common.c
1 /**
2  * collectd - src/common.c
3  * Copyright (C) 2005-2008  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  *   Niki W. Waibel <niki.waibel@gmx.net>
21  *   Sebastian Harl <sh at tokkee.org>
22  *   Michał Mirosław <mirq-linux at rere.qmqm.pl>
23 **/
24
25 #if HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32
33 #if HAVE_PTHREAD_H
34 # include <pthread.h>
35 #endif
36
37 #ifdef HAVE_MATH_H
38 # include <math.h>
39 #endif
40
41 /* for ntohl and htonl */
42 #if HAVE_ARPA_INET_H
43 # include <arpa/inet.h>
44 #endif
45
46 #ifdef HAVE_LIBKSTAT
47 extern kstat_ctl_t *kc;
48 #endif
49
50 #if !HAVE_GETPWNAM_R
51 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
52 #endif
53
54 #if !HAVE_STRERROR_R
55 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
56 #endif
57
58 char *sstrncpy (char *dest, const char *src, size_t n)
59 {
60         strncpy (dest, src, n);
61         dest[n - 1] = '\0';
62
63         return (dest);
64 } /* char *sstrncpy */
65
66 int ssnprintf (char *dest, size_t n, const char *format, ...)
67 {
68         int ret = 0;
69         va_list ap;
70
71         va_start (ap, format);
72         ret = vsnprintf (dest, n, format, ap);
73         dest[n - 1] = '\0';
74         va_end (ap);
75
76         return (ret);
77 } /* int ssnprintf */
78
79 char *sstrdup (const char *s)
80 {
81         char *r;
82         size_t sz;
83
84         if (s == NULL)
85                 return (NULL);
86
87         /* Do not use `strdup' here, because it's not specified in POSIX. It's
88          * ``only'' an XSI extension. */
89         sz = strlen (s) + 1;
90         r = (char *) malloc (sizeof (char) * sz);
91         if (r == NULL)
92         {
93                 ERROR ("sstrdup: Out of memory.");
94                 exit (3);
95         }
96         memcpy (r, s, sizeof (char) * sz);
97
98         return (r);
99 } /* char *sstrdup */
100
101 /* Even though Posix requires "strerror_r" to return an "int",
102  * some systems (e.g. the GNU libc) return a "char *" _and_
103  * ignore the second argument ... -tokkee */
104 char *sstrerror (int errnum, char *buf, size_t buflen)
105 {
106         buf[0] = '\0';
107
108 #if !HAVE_STRERROR_R
109         {
110                 char *temp;
111
112                 pthread_mutex_lock (&strerror_r_lock);
113
114                 temp = strerror (errnum);
115                 sstrncpy (buf, temp, buflen);
116
117                 pthread_mutex_unlock (&strerror_r_lock);
118         }
119 /* #endif !HAVE_STRERROR_R */
120
121 #elif STRERROR_R_CHAR_P
122         {
123                 char *temp;
124                 temp = strerror_r (errnum, buf, buflen);
125                 if (buf[0] == '\0')
126                 {
127                         if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
128                                 sstrncpy (buf, temp, buflen);
129                         else
130                                 sstrncpy (buf, "strerror_r did not return "
131                                                 "an error message", buflen);
132                 }
133         }
134 /* #endif STRERROR_R_CHAR_P */
135
136 #else
137         if (strerror_r (errnum, buf, buflen) != 0)
138         {
139                 ssnprintf (buf, buflen, "Error #%i; "
140                                 "Additionally, strerror_r failed.",
141                                 errnum);
142         }
143 #endif /* STRERROR_R_CHAR_P */
144
145         return (buf);
146 } /* char *sstrerror */
147
148 void *smalloc (size_t size)
149 {
150         void *r;
151
152         if ((r = malloc (size)) == NULL)
153         {
154                 ERROR ("Not enough memory.");
155                 exit (3);
156         }
157
158         return (r);
159 } /* void *smalloc */
160
161 #if 0
162 void sfree (void **ptr)
163 {
164         if (ptr == NULL)
165                 return;
166
167         if (*ptr != NULL)
168                 free (*ptr);
169
170         *ptr = NULL;
171 }
172 #endif
173
174 ssize_t sread (int fd, void *buf, size_t count)
175 {
176         char    *ptr;
177         size_t   nleft;
178         ssize_t  status;
179
180         ptr   = (char *) buf;
181         nleft = count;
182
183         while (nleft > 0)
184         {
185                 status = read (fd, (void *) ptr, nleft);
186
187                 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
188                         continue;
189
190                 if (status < 0)
191                         return (status);
192
193                 if (status == 0)
194                 {
195                         DEBUG ("Received EOF from fd %i. "
196                                         "Closing fd and returning error.",
197                                         fd);
198                         close (fd);
199                         return (-1);
200                 }
201
202                 assert ((0 > status) || (nleft >= (size_t)status));
203
204                 nleft = nleft - status;
205                 ptr   = ptr   + status;
206         }
207
208         return (0);
209 }
210
211
212 ssize_t swrite (int fd, const void *buf, size_t count)
213 {
214         const char *ptr;
215         size_t      nleft;
216         ssize_t     status;
217
218         ptr   = (const char *) buf;
219         nleft = count;
220
221         while (nleft > 0)
222         {
223                 status = write (fd, (const void *) ptr, nleft);
224
225                 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
226                         continue;
227
228                 if (status < 0)
229                         return (status);
230
231                 nleft = nleft - status;
232                 ptr   = ptr   + status;
233         }
234
235         return (0);
236 }
237
238 int strsplit (char *string, char **fields, size_t size)
239 {
240         size_t i;
241         char *ptr;
242         char *saveptr;
243
244         i = 0;
245         ptr = string;
246         saveptr = NULL;
247         while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
248         {
249                 ptr = NULL;
250                 i++;
251
252                 if (i >= size)
253                         break;
254         }
255
256         return (i);
257 }
258
259 int strjoin (char *dst, size_t dst_len,
260                 char **fields, size_t fields_num,
261                 const char *sep)
262 {
263         size_t field_len;
264         size_t sep_len;
265         int i;
266
267         memset (dst, '\0', dst_len);
268
269         if (fields_num <= 0)
270                 return (-1);
271
272         sep_len = 0;
273         if (sep != NULL)
274                 sep_len = strlen (sep);
275
276         for (i = 0; i < (int)fields_num; i++)
277         {
278                 if ((i > 0) && (sep_len > 0))
279                 {
280                         if (dst_len <= sep_len)
281                                 return (-1);
282
283                         strncat (dst, sep, dst_len);
284                         dst_len -= sep_len;
285                 }
286
287                 field_len = strlen (fields[i]);
288
289                 if (dst_len <= field_len)
290                         return (-1);
291
292                 strncat (dst, fields[i], dst_len);
293                 dst_len -= field_len;
294         }
295
296         return (strlen (dst));
297 }
298
299 int strsubstitute (char *str, char c_from, char c_to)
300 {
301         int ret;
302
303         if (str == NULL)
304                 return (-1);
305
306         ret = 0;
307         while (*str != '\0')
308         {
309                 if (*str == c_from)
310                 {
311                         *str = c_to;
312                         ret++;
313                 }
314                 str++;
315         }
316
317         return (ret);
318 } /* int strsubstitute */
319
320 int escape_slashes (char *buf, int buf_len)
321 {
322         int i;
323
324         if (strcmp (buf, "/") == 0)
325         {
326                 if (buf_len < 5)
327                         return (-1);
328
329                 strncpy (buf, "root", buf_len);
330                 return (0);
331         }
332
333         if (buf_len <= 1)
334                 return (0);
335
336         /* Move one to the left */
337         if (buf[0] == '/')
338                 memmove (buf, buf + 1, buf_len - 1);
339
340         for (i = 0; i < buf_len - 1; i++)
341         {
342                 if (buf[i] == '\0')
343                         break;
344                 else if (buf[i] == '/')
345                         buf[i] = '_';
346         }
347         buf[i] = '\0';
348
349         return (0);
350 } /* int escape_slashes */
351
352 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta)
353 {
354         struct timeval *larger;
355         struct timeval *smaller;
356
357         int status;
358
359         NORMALIZE_TIMEVAL (tv0);
360         NORMALIZE_TIMEVAL (tv1);
361
362         if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec))
363         {
364                 if (delta != NULL) {
365                         delta->tv_sec  = 0;
366                         delta->tv_usec = 0;
367                 }
368                 return (0);
369         }
370
371         if ((tv0.tv_sec < tv1.tv_sec)
372                         || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec)))
373         {
374                 larger  = &tv1;
375                 smaller = &tv0;
376                 status  = -1;
377         }
378         else
379         {
380                 larger  = &tv0;
381                 smaller = &tv1;
382                 status  = 1;
383         }
384
385         if (delta != NULL) {
386                 delta->tv_sec = larger->tv_sec - smaller->tv_sec;
387
388                 if (smaller->tv_usec <= larger->tv_usec)
389                         delta->tv_usec = larger->tv_usec - smaller->tv_usec;
390                 else
391                 {
392                         --delta->tv_sec;
393                         delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec;
394                 }
395         }
396
397         assert ((delta == NULL)
398                         || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000)));
399
400         return (status);
401 } /* int timeval_cmp */
402
403 int check_create_dir (const char *file_orig)
404 {
405         struct stat statbuf;
406
407         char  file_copy[512];
408         char  dir[512];
409         int   dir_len = 512;
410         char *fields[16];
411         int   fields_num;
412         char *ptr;
413         char *saveptr;
414         int   last_is_file = 1;
415         int   path_is_absolute = 0;
416         size_t len;
417         int   i;
418
419         /*
420          * Sanity checks first
421          */
422         if (file_orig == NULL)
423                 return (-1);
424
425         if ((len = strlen (file_orig)) < 1)
426                 return (-1);
427         else if (len >= sizeof (file_copy))
428                 return (-1);
429
430         /*
431          * If `file_orig' ends in a slash the last component is a directory,
432          * otherwise it's a file. Act accordingly..
433          */
434         if (file_orig[len - 1] == '/')
435                 last_is_file = 0;
436         if (file_orig[0] == '/')
437                 path_is_absolute = 1;
438
439         /*
440          * Create a copy for `strtok_r' to destroy
441          */
442         sstrncpy (file_copy, file_orig, sizeof (file_copy));
443
444         /*
445          * Break into components. This will eat up several slashes in a row and
446          * remove leading and trailing slashes..
447          */
448         ptr = file_copy;
449         saveptr = NULL;
450         fields_num = 0;
451         while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
452         {
453                 ptr = NULL;
454                 fields_num++;
455
456                 if (fields_num >= 16)
457                         break;
458         }
459
460         /*
461          * For each component, do..
462          */
463         for (i = 0; i < (fields_num - last_is_file); i++)
464         {
465                 /*
466                  * Do not create directories that start with a dot. This
467                  * prevents `../../' attacks and other likely malicious
468                  * behavior.
469                  */
470                 if (fields[i][0] == '.')
471                 {
472                         ERROR ("Cowardly refusing to create a directory that "
473                                         "begins with a `.' (dot): `%s'", file_orig);
474                         return (-2);
475                 }
476
477                 /*
478                  * Join the components together again
479                  */
480                 dir[0] = '/';
481                 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
482                                         fields, i + 1, "/") < 0)
483                 {
484                         ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
485                         return (-1);
486                 }
487
488                 while (42) {
489                         if (stat (dir, &statbuf) == -1)
490                         {
491                                 if (errno == ENOENT)
492                                 {
493                                         if (mkdir (dir, 0755) == 0)
494                                                 break;
495
496                                         /* this might happen, if a different thread created
497                                          * the directory in the meantime
498                                          * => call stat() again to check for S_ISDIR() */
499                                         if (EEXIST == errno)
500                                                 continue;
501
502                                         char errbuf[1024];
503                                         ERROR ("check_create_dir: mkdir (%s): %s", dir,
504                                                         sstrerror (errno,
505                                                                 errbuf, sizeof (errbuf)));
506                                         return (-1);
507                                 }
508                                 else
509                                 {
510                                         char errbuf[1024];
511                                         ERROR ("check_create_dir: stat (%s): %s", dir,
512                                                         sstrerror (errno, errbuf,
513                                                                 sizeof (errbuf)));
514                                         return (-1);
515                                 }
516                         }
517                         else if (!S_ISDIR (statbuf.st_mode))
518                         {
519                                 ERROR ("check_create_dir: `%s' exists but is not "
520                                                 "a directory!", dir);
521                                 return (-1);
522                         }
523                         break;
524                 }
525         }
526
527         return (0);
528 } /* check_create_dir */
529
530 #ifdef HAVE_LIBKSTAT
531 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
532 {
533         char ident[128];
534
535         *ksp_ptr = NULL;
536         
537         if (kc == NULL)
538                 return (-1);
539
540         ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
541
542         *ksp_ptr = kstat_lookup (kc, module, instance, name);
543         if (*ksp_ptr == NULL)
544         {
545                 ERROR ("get_kstat: Cound not find kstat %s", ident);
546                 return (-1);
547         }
548
549         if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
550         {
551                 ERROR ("get_kstat: kstat %s has wrong type", ident);
552                 *ksp_ptr = NULL;
553                 return (-1);
554         }
555
556 #ifdef assert
557         assert (*ksp_ptr != NULL);
558         assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
559 #endif
560
561         if (kstat_read (kc, *ksp_ptr, NULL) == -1)
562         {
563                 ERROR ("get_kstat: kstat %s could not be read", ident);
564                 return (-1);
565         }
566
567         if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
568         {
569                 ERROR ("get_kstat: kstat %s has wrong type", ident);
570                 return (-1);
571         }
572
573         return (0);
574 }
575
576 long long get_kstat_value (kstat_t *ksp, char *name)
577 {
578         kstat_named_t *kn;
579         long long retval = -1LL;
580
581 #ifdef assert
582         assert (ksp != NULL);
583         assert (ksp->ks_type == KSTAT_TYPE_NAMED);
584 #else
585         if (ksp == NULL)
586         {
587                 ERROR ("ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
588                 return (-1LL);
589         }
590         else if (ksp->ks_type != KSTAT_TYPE_NAMED)
591         {
592                 ERROR ("ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
593                 return (-1LL);
594         }
595 #endif
596
597         if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
598                 return (retval);
599
600         if (kn->data_type == KSTAT_DATA_INT32)
601                 retval = (long long) kn->value.i32;
602         else if (kn->data_type == KSTAT_DATA_UINT32)
603                 retval = (long long) kn->value.ui32;
604         else if (kn->data_type == KSTAT_DATA_INT64)
605                 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
606         else if (kn->data_type == KSTAT_DATA_UINT64)
607                 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
608         else
609                 WARNING ("get_kstat_value: Not a numeric value: %s", name);
610                  
611         return (retval);
612 }
613 #endif /* HAVE_LIBKSTAT */
614
615 unsigned long long ntohll (unsigned long long n)
616 {
617 #if BYTE_ORDER == BIG_ENDIAN
618         return (n);
619 #else
620         return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
621 #endif
622 } /* unsigned long long ntohll */
623
624 unsigned long long htonll (unsigned long long n)
625 {
626 #if BYTE_ORDER == BIG_ENDIAN
627         return (n);
628 #else
629         return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
630 #endif
631 } /* unsigned long long htonll */
632
633 #if FP_LAYOUT_NEED_NOTHING
634 /* Well, we need nothing.. */
635 /* #endif FP_LAYOUT_NEED_NOTHING */
636
637 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
638 # if FP_LAYOUT_NEED_ENDIANFLIP
639 #  define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
640                          (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
641                          (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
642                          (((uint64_t)(A) & 0x000000ff00000000LL) >> 8)  | \
643                          (((uint64_t)(A) & 0x00000000ff000000LL) << 8)  | \
644                          (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
645                          (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
646                          (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
647 # else
648 #  define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
649                          (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
650 # endif
651
652 double ntohd (double d)
653 {
654         union
655         {
656                 uint8_t  byte[8];
657                 uint64_t integer;
658                 double   floating;
659         } ret;
660
661         ret.floating = d;
662
663         /* NAN in x86 byte order */
664         if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
665                         && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
666                         && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
667                         && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
668         {
669                 return (NAN);
670         }
671         else
672         {
673                 uint64_t tmp;
674
675                 tmp = ret.integer;
676                 ret.integer = FP_CONVERT (tmp);
677                 return (ret.floating);
678         }
679 } /* double ntohd */
680
681 double htond (double d)
682 {
683         union
684         {
685                 uint8_t  byte[8];
686                 uint64_t integer;
687                 double   floating;
688         } ret;
689
690         if (isnan (d))
691         {
692                 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
693                 ret.byte[4] = ret.byte[5] = 0x00;
694                 ret.byte[6] = 0xf8;
695                 ret.byte[7] = 0x7f;
696                 return (ret.floating);
697         }
698         else
699         {
700                 uint64_t tmp;
701
702                 ret.floating = d;
703                 tmp = FP_CONVERT (ret.integer);
704                 ret.integer = tmp;
705                 return (ret.floating);
706         }
707 } /* double htond */
708 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
709
710 int format_name (char *ret, int ret_len,
711                 const char *hostname,
712                 const char *plugin, const char *plugin_instance,
713                 const char *type, const char *type_instance)
714 {
715         int  status;
716
717         assert (plugin != NULL);
718         assert (type != NULL);
719
720         if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
721         {
722                 if ((type_instance == NULL) || (strlen (type_instance) == 0))
723                         status = ssnprintf (ret, ret_len, "%s/%s/%s",
724                                         hostname, plugin, type);
725                 else
726                         status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
727                                         hostname, plugin, type,
728                                         type_instance);
729         }
730         else
731         {
732                 if ((type_instance == NULL) || (strlen (type_instance) == 0))
733                         status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
734                                         hostname, plugin, plugin_instance,
735                                         type);
736                 else
737                         status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
738                                         hostname, plugin, plugin_instance,
739                                         type, type_instance);
740         }
741
742         if ((status < 1) || (status >= ret_len))
743                 return (-1);
744         return (0);
745 } /* int format_name */
746
747 int parse_identifier (char *str, char **ret_host,
748                 char **ret_plugin, char **ret_plugin_instance,
749                 char **ret_type, char **ret_type_instance)
750 {
751         char *hostname = NULL;
752         char *plugin = NULL;
753         char *plugin_instance = NULL;
754         char *type = NULL;
755         char *type_instance = NULL;
756
757         hostname = str;
758         if (hostname == NULL)
759                 return (-1);
760
761         plugin = strchr (hostname, '/');
762         if (plugin == NULL)
763                 return (-1);
764         *plugin = '\0'; plugin++;
765
766         type = strchr (plugin, '/');
767         if (type == NULL)
768                 return (-1);
769         *type = '\0'; type++;
770
771         plugin_instance = strchr (plugin, '-');
772         if (plugin_instance != NULL)
773         {
774                 *plugin_instance = '\0';
775                 plugin_instance++;
776         }
777
778         type_instance = strchr (type, '-');
779         if (type_instance != NULL)
780         {
781                 *type_instance = '\0';
782                 type_instance++;
783         }
784
785         *ret_host = hostname;
786         *ret_plugin = plugin;
787         *ret_plugin_instance = plugin_instance;
788         *ret_type = type;
789         *ret_type_instance = type_instance;
790         return (0);
791 } /* int parse_identifier */
792
793 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
794 {
795         int i;
796         char *dummy;
797         char *ptr;
798         char *saveptr;
799
800         i = -1;
801         dummy = buffer;
802         saveptr = NULL;
803         while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
804         {
805                 dummy = NULL;
806
807                 if (i >= vl->values_len)
808                         break;
809
810                 if (i == -1)
811                 {
812                         if (strcmp ("N", ptr) == 0)
813                                 vl->time = time (NULL);
814                         else
815                                 vl->time = (time_t) atoi (ptr);
816                 }
817                 else
818                 {
819                         if (strcmp ("U", ptr) == 0)
820                                 vl->values[i].gauge = NAN;
821                         else if (ds->ds[i].type == DS_TYPE_COUNTER)
822                                 vl->values[i].counter = atoll (ptr);
823                         else if (ds->ds[i].type == DS_TYPE_GAUGE)
824                                 vl->values[i].gauge = atof (ptr);
825                 }
826
827                 i++;
828         } /* while (strtok_r) */
829
830         if ((ptr != NULL) || (i != vl->values_len))
831                 return (-1);
832         return (0);
833 } /* int parse_values */
834
835 #if !HAVE_GETPWNAM_R
836 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
837                 size_t buflen, struct passwd **pwbufp)
838 {
839         int status = 0;
840         struct passwd *pw;
841
842         memset (pwbuf, '\0', sizeof (struct passwd));
843
844         pthread_mutex_lock (&getpwnam_r_lock);
845
846         do
847         {
848                 pw = getpwnam (name);
849                 if (pw == NULL)
850                 {
851                         status = (errno != 0) ? errno : ENOENT;
852                         break;
853                 }
854
855 #define GETPWNAM_COPY_MEMBER(member) \
856                 if (pw->member != NULL) \
857                 { \
858                         int len = strlen (pw->member); \
859                         if (len >= buflen) \
860                         { \
861                                 status = ENOMEM; \
862                                 break; \
863                         } \
864                         sstrncpy (buf, pw->member, buflen); \
865                         pwbuf->member = buf; \
866                         buf    += (len + 1); \
867                         buflen -= (len + 1); \
868                 }
869                 GETPWNAM_COPY_MEMBER(pw_name);
870                 GETPWNAM_COPY_MEMBER(pw_passwd);
871                 GETPWNAM_COPY_MEMBER(pw_gecos);
872                 GETPWNAM_COPY_MEMBER(pw_dir);
873                 GETPWNAM_COPY_MEMBER(pw_shell);
874
875                 pwbuf->pw_uid = pw->pw_uid;
876                 pwbuf->pw_gid = pw->pw_gid;
877
878                 if (pwbufp != NULL)
879                         *pwbufp = pwbuf;
880         } while (0);
881
882         pthread_mutex_unlock (&getpwnam_r_lock);
883
884         return (status);
885 } /* int getpwnam_r */
886 #endif /* !HAVE_GETPWNAM_R */
887
888 int notification_init (notification_t *n, int severity, const char *message,
889                 const char *host,
890                 const char *plugin, const char *plugin_instance,
891                 const char *type, const char *type_instance)
892 {
893         memset (n, '\0', sizeof (notification_t));
894
895         n->severity = severity;
896
897         if (message != NULL)
898                 sstrncpy (n->message, message, sizeof (n->message));
899         if (host != NULL)
900                 sstrncpy (n->host, host, sizeof (n->host));
901         if (plugin != NULL)
902                 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
903         if (plugin_instance != NULL)
904                 sstrncpy (n->plugin_instance, plugin_instance,
905                                 sizeof (n->plugin_instance));
906         if (type != NULL)
907                 sstrncpy (n->type, type, sizeof (n->type));
908         if (type_instance != NULL)
909                 sstrncpy (n->type_instance, type_instance,
910                                 sizeof (n->type_instance));
911
912         return (0);
913 } /* int notification_init */
914
915 int walk_directory (const char *dir, dirwalk_callback_f callback,
916                 void *user_data)
917 {
918         struct dirent *ent;
919         DIR *dh;
920         int success;
921         int failure;
922
923         success = 0;
924         failure = 0;
925
926         if ((dh = opendir (dir)) == NULL)
927         {
928                 char errbuf[1024];
929                 ERROR ("walk_directory: Cannot open '%s': %s", dir,
930                                 sstrerror (errno, errbuf, sizeof (errbuf)));
931                 return -1;
932         }
933
934         while ((ent = readdir (dh)) != NULL)
935         {
936                 int status;
937
938                 if (ent->d_name[0] == '.')
939                         continue;
940
941                 status = (*callback) (dir, ent->d_name, user_data);
942                 if (status != 0)
943                         failure++;
944                 else
945                         success++;
946         }
947
948         closedir (dh);
949
950         if ((success == 0) && (failure > 0))
951                 return (-1);
952         return (0);
953 }
954
955 int read_file_contents (const char *filename, char *buf, int bufsize)
956 {
957         FILE *fh;
958         int n;
959
960         if ((fh = fopen (filename, "r")) == NULL)
961                 return -1;
962
963         n = fread(buf, 1, bufsize, fh);
964         fclose(fh);
965
966         return n;
967 }
968
969 counter_t counter_diff (counter_t old_value, counter_t new_value)
970 {
971         counter_t diff;
972
973         if (old_value > new_value)
974         {
975                 if (old_value <= 4294967295U)
976                         diff = (4294967295U - old_value) + new_value;
977                 else
978                         diff = (18446744073709551615ULL - old_value)
979                                 + new_value;
980         }
981         else
982         {
983                 diff = new_value - old_value;
984         }
985
986         return (diff);
987 } /* counter_t counter_to_gauge */