c4994a2c2e017e44ae7c2108ed29550698b71f0e
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 **/
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
31 #if HAVE_PTHREAD_H
32 # include <pthread.h>
33 #endif
35 #ifdef HAVE_MATH_H
36 # include <math.h>
37 #endif
39 /* for ntohl and htonl */
40 #if HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
42 #endif
44 #ifdef HAVE_LIBKSTAT
45 extern kstat_ctl_t *kc;
46 #endif
48 #if !HAVE_GETPWNAM_R
49 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
50 #endif
52 #if !HAVE_STRERROR_R
53 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
54 #endif
56 char *sstrncpy (char *dest, const char *src, size_t n)
57 {
58 strncpy (dest, src, n);
59 dest[n - 1] = '\0';
61 return (dest);
62 } /* char *sstrncpy */
64 int ssnprintf (char *dest, size_t n, const char *format, ...)
65 {
66 int ret = 0;
67 va_list ap;
69 va_start (ap, format);
70 ret = vsnprintf (dest, n, format, ap);
71 dest[n - 1] = '\0';
72 va_end (ap);
74 return (ret);
75 } /* int ssnprintf */
77 char *sstrdup (const char *s)
78 {
79 char *r;
80 size_t sz;
82 if (s == NULL)
83 return (NULL);
85 /* Do not use `strdup' here, because it's not specified in POSIX. It's
86 * ``only'' an XSI extension. */
87 sz = strlen (s) + 1;
88 r = (char *) malloc (sizeof (char) * sz);
89 if (r == NULL)
90 {
91 ERROR ("sstrdup: Out of memory.");
92 exit (3);
93 }
94 memcpy (r, s, sizeof (char) * sz);
96 return (r);
97 } /* char *sstrdup */
99 /* Even though Posix requires "strerror_r" to return an "int",
100 * some systems (e.g. the GNU libc) return a "char *" _and_
101 * ignore the second argument ... -tokkee */
102 char *sstrerror (int errnum, char *buf, size_t buflen)
103 {
104 buf[0] = '\0';
106 #if !HAVE_STRERROR_R
107 {
108 char *temp;
110 pthread_mutex_lock (&strerror_r_lock);
112 temp = strerror (errnum);
113 sstrncpy (buf, temp, buflen);
115 pthread_mutex_unlock (&strerror_r_lock);
116 }
117 /* #endif !HAVE_STRERROR_R */
119 #elif STRERROR_R_CHAR_P
120 {
121 char *temp;
122 temp = strerror_r (errnum, buf, buflen);
123 if (buf[0] == '\0')
124 {
125 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
126 sstrncpy (buf, temp, buflen);
127 else
128 sstrncpy (buf, "strerror_r did not return "
129 "an error message", buflen);
130 }
131 }
132 /* #endif STRERROR_R_CHAR_P */
134 #else
135 if (strerror_r (errnum, buf, buflen) != 0)
136 {
137 ssnprintf (buf, buflen, "Error #%i; "
138 "Additionally, strerror_r failed.",
139 errnum);
140 }
141 #endif /* STRERROR_R_CHAR_P */
143 return (buf);
144 } /* char *sstrerror */
146 void *smalloc (size_t size)
147 {
148 void *r;
150 if ((r = malloc (size)) == NULL)
151 {
152 ERROR ("Not enough memory.");
153 exit (3);
154 }
156 return (r);
157 } /* void *smalloc */
159 #if 0
160 void sfree (void **ptr)
161 {
162 if (ptr == NULL)
163 return;
165 if (*ptr != NULL)
166 free (*ptr);
168 *ptr = NULL;
169 }
170 #endif
172 ssize_t sread (int fd, void *buf, size_t count)
173 {
174 char *ptr;
175 size_t nleft;
176 ssize_t status;
178 ptr = (char *) buf;
179 nleft = count;
181 while (nleft > 0)
182 {
183 status = read (fd, (void *) ptr, nleft);
185 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
186 continue;
188 if (status < 0)
189 return (status);
191 if (status == 0)
192 {
193 DEBUG ("Received EOF from fd %i. "
194 "Closing fd and returning error.",
195 fd);
196 close (fd);
197 return (-1);
198 }
200 assert ((0 > status) || (nleft >= (size_t)status));
202 nleft = nleft - status;
203 ptr = ptr + status;
204 }
206 return (0);
207 }
210 ssize_t swrite (int fd, const void *buf, size_t count)
211 {
212 const char *ptr;
213 size_t nleft;
214 ssize_t status;
216 ptr = (const char *) buf;
217 nleft = count;
219 while (nleft > 0)
220 {
221 status = write (fd, (const void *) ptr, nleft);
223 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
224 continue;
226 if (status < 0)
227 return (status);
229 nleft = nleft - status;
230 ptr = ptr + status;
231 }
233 return (0);
234 }
236 int strsplit (char *string, char **fields, size_t size)
237 {
238 size_t i;
239 char *ptr;
240 char *saveptr;
242 i = 0;
243 ptr = string;
244 saveptr = NULL;
245 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
246 {
247 ptr = NULL;
248 i++;
250 if (i >= size)
251 break;
252 }
254 return (i);
255 }
257 int strjoin (char *dst, size_t dst_len,
258 char **fields, size_t fields_num,
259 const char *sep)
260 {
261 size_t field_len;
262 size_t sep_len;
263 int i;
265 memset (dst, '\0', dst_len);
267 if (fields_num <= 0)
268 return (-1);
270 sep_len = 0;
271 if (sep != NULL)
272 sep_len = strlen (sep);
274 for (i = 0; i < (int)fields_num; i++)
275 {
276 if ((i > 0) && (sep_len > 0))
277 {
278 if (dst_len <= sep_len)
279 return (-1);
281 strncat (dst, sep, dst_len);
282 dst_len -= sep_len;
283 }
285 field_len = strlen (fields[i]);
287 if (dst_len <= field_len)
288 return (-1);
290 strncat (dst, fields[i], dst_len);
291 dst_len -= field_len;
292 }
294 return (strlen (dst));
295 }
297 int strsubstitute (char *str, char c_from, char c_to)
298 {
299 int ret;
301 if (str == NULL)
302 return (-1);
304 ret = 0;
305 while (*str != '\0')
306 {
307 if (*str == c_from)
308 {
309 *str = c_to;
310 ret++;
311 }
312 str++;
313 }
315 return (ret);
316 } /* int strsubstitute */
318 int escape_slashes (char *buf, int buf_len)
319 {
320 int i;
322 if (strcmp (buf, "/") == 0)
323 {
324 if (buf_len < 5)
325 return (-1);
327 strncpy (buf, "root", buf_len);
328 return (0);
329 }
331 if (buf_len <= 1)
332 return (0);
334 /* Move one to the left */
335 if (buf[0] == '/')
336 memmove (buf, buf + 1, buf_len - 1);
338 for (i = 0; i < buf_len - 1; i++)
339 {
340 if (buf[i] == '\0')
341 break;
342 else if (buf[i] == '/')
343 buf[i] = '_';
344 }
345 buf[i] = '\0';
347 return (0);
348 } /* int escape_slashes */
350 void replace_special (char *buffer, size_t buffer_size)
351 {
352 size_t i;
354 for (i = 0; i < buffer_size; i++)
355 {
356 if (buffer[i] == 0)
357 return;
358 if ((!isalnum ((int) buffer[i])) && (buffer[i] != '-'))
359 buffer[i] = '_';
360 }
361 } /* void replace_special */
363 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta)
364 {
365 struct timeval *larger;
366 struct timeval *smaller;
368 int status;
370 NORMALIZE_TIMEVAL (tv0);
371 NORMALIZE_TIMEVAL (tv1);
373 if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec))
374 {
375 if (delta != NULL) {
376 delta->tv_sec = 0;
377 delta->tv_usec = 0;
378 }
379 return (0);
380 }
382 if ((tv0.tv_sec < tv1.tv_sec)
383 || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec)))
384 {
385 larger = &tv1;
386 smaller = &tv0;
387 status = -1;
388 }
389 else
390 {
391 larger = &tv0;
392 smaller = &tv1;
393 status = 1;
394 }
396 if (delta != NULL) {
397 delta->tv_sec = larger->tv_sec - smaller->tv_sec;
399 if (smaller->tv_usec <= larger->tv_usec)
400 delta->tv_usec = larger->tv_usec - smaller->tv_usec;
401 else
402 {
403 --delta->tv_sec;
404 delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec;
405 }
406 }
408 assert ((delta == NULL)
409 || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000)));
411 return (status);
412 } /* int timeval_cmp */
414 int check_create_dir (const char *file_orig)
415 {
416 struct stat statbuf;
418 char file_copy[512];
419 char dir[512];
420 int dir_len = 512;
421 char *fields[16];
422 int fields_num;
423 char *ptr;
424 char *saveptr;
425 int last_is_file = 1;
426 int path_is_absolute = 0;
427 size_t len;
428 int i;
430 /*
431 * Sanity checks first
432 */
433 if (file_orig == NULL)
434 return (-1);
436 if ((len = strlen (file_orig)) < 1)
437 return (-1);
438 else if (len >= sizeof (file_copy))
439 return (-1);
441 /*
442 * If `file_orig' ends in a slash the last component is a directory,
443 * otherwise it's a file. Act accordingly..
444 */
445 if (file_orig[len - 1] == '/')
446 last_is_file = 0;
447 if (file_orig[0] == '/')
448 path_is_absolute = 1;
450 /*
451 * Create a copy for `strtok_r' to destroy
452 */
453 sstrncpy (file_copy, file_orig, sizeof (file_copy));
455 /*
456 * Break into components. This will eat up several slashes in a row and
457 * remove leading and trailing slashes..
458 */
459 ptr = file_copy;
460 saveptr = NULL;
461 fields_num = 0;
462 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
463 {
464 ptr = NULL;
465 fields_num++;
467 if (fields_num >= 16)
468 break;
469 }
471 /*
472 * For each component, do..
473 */
474 for (i = 0; i < (fields_num - last_is_file); i++)
475 {
476 /*
477 * Do not create directories that start with a dot. This
478 * prevents `../../' attacks and other likely malicious
479 * behavior.
480 */
481 if (fields[i][0] == '.')
482 {
483 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
484 return (-2);
485 }
487 /*
488 * Join the components together again
489 */
490 dir[0] = '/';
491 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
492 fields, i + 1, "/") < 0)
493 {
494 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
495 return (-1);
496 }
498 if (stat (dir, &statbuf) == -1)
499 {
500 if (errno == ENOENT)
501 {
502 if (mkdir (dir, 0755) == -1)
503 {
504 char errbuf[1024];
505 ERROR ("check_create_dir: mkdir (%s): %s", dir,
506 sstrerror (errno,
507 errbuf, sizeof (errbuf)));
508 return (-1);
509 }
510 }
511 else
512 {
513 char errbuf[1024];
514 ERROR ("stat (%s): %s", dir,
515 sstrerror (errno, errbuf,
516 sizeof (errbuf)));
517 return (-1);
518 }
519 }
520 else if (!S_ISDIR (statbuf.st_mode))
521 {
522 ERROR ("stat (%s): Not a directory!", dir);
523 return (-1);
524 }
525 }
527 return (0);
528 } /* check_create_dir */
530 #ifdef HAVE_LIBKSTAT
531 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
532 {
533 char ident[128];
535 if (kc == NULL)
536 return (-1);
538 ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
540 if (*ksp_ptr == NULL)
541 {
542 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
543 {
544 ERROR ("Cound not find kstat %s", ident);
545 return (-1);
546 }
548 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
549 {
550 WARNING ("kstat %s has wrong type", ident);
551 *ksp_ptr = NULL;
552 return (-1);
553 }
554 }
556 #ifdef assert
557 assert (*ksp_ptr != NULL);
558 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
559 #endif
561 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
562 {
563 WARNING ("kstat %s could not be read", ident);
564 return (-1);
565 }
567 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
568 {
569 WARNING ("kstat %s has wrong type", ident);
570 return (-1);
571 }
573 return (0);
574 }
576 long long get_kstat_value (kstat_t *ksp, char *name)
577 {
578 kstat_named_t *kn;
579 long long retval = -1LL;
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
597 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
598 return (retval);
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);
611 return (retval);
612 }
613 #endif /* HAVE_LIBKSTAT */
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 */
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 */
633 #if FP_LAYOUT_NEED_NOTHING
634 /* Well, we need nothing.. */
635 /* #endif FP_LAYOUT_NEED_NOTHING */
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
652 double ntohd (double d)
653 {
654 union
655 {
656 uint8_t byte[8];
657 uint64_t integer;
658 double floating;
659 } ret;
661 ret.floating = d;
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;
675 tmp = ret.integer;
676 ret.integer = FP_CONVERT (tmp);
677 return (ret.floating);
678 }
679 } /* double ntohd */
681 double htond (double d)
682 {
683 union
684 {
685 uint8_t byte[8];
686 uint64_t integer;
687 double floating;
688 } ret;
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;
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 */
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;
717 assert (plugin != NULL);
718 assert (type != NULL);
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 }
742 if ((status < 1) || (status >= ret_len))
743 return (-1);
744 return (0);
745 } /* int format_name */
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;
757 hostname = str;
758 if (hostname == NULL)
759 return (-1);
761 plugin = strchr (hostname, '/');
762 if (plugin == NULL)
763 return (-1);
764 *plugin = '\0'; plugin++;
766 type = strchr (plugin, '/');
767 if (type == NULL)
768 return (-1);
769 *type = '\0'; type++;
771 plugin_instance = strchr (plugin, '-');
772 if (plugin_instance != NULL)
773 {
774 *plugin_instance = '\0';
775 plugin_instance++;
776 }
778 type_instance = strchr (type, '-');
779 if (type_instance != NULL)
780 {
781 *type_instance = '\0';
782 type_instance++;
783 }
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 */
793 int parse_value (const char *value, value_t *ret_value, const data_source_t ds)
794 {
795 char *endptr = NULL;
797 if (DS_TYPE_COUNTER == ds.type)
798 ret_value->counter = (counter_t)strtoll (value, &endptr, 0);
799 else if (DS_TYPE_GAUGE == ds.type)
800 ret_value->gauge = (gauge_t)strtod (value, &endptr);
801 else {
802 ERROR ("parse_value: Invalid data source \"%s\" "
803 "(type = %i).", ds.name, ds.type);
804 return -1;
805 }
807 if (value == endptr) {
808 ERROR ("parse_value: Failed to parse string as number: %s.", value);
809 return -1;
810 }
811 else if ((NULL != endptr) && ('\0' != *endptr))
812 WARNING ("parse_value: Ignoring trailing garbage after number: %s.",
813 endptr);
814 return 0;
815 } /* int parse_value */
817 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
818 {
819 int i;
820 char *dummy;
821 char *ptr;
822 char *saveptr;
824 i = -1;
825 dummy = buffer;
826 saveptr = NULL;
827 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
828 {
829 dummy = NULL;
831 if (i >= vl->values_len)
832 break;
834 if (i == -1)
835 {
836 if (strcmp ("N", ptr) == 0)
837 vl->time = time (NULL);
838 else
839 vl->time = (time_t) atoi (ptr);
840 }
841 else
842 {
843 if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE))
844 vl->values[i].gauge = NAN;
845 else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i]))
846 return -1;
847 }
849 i++;
850 } /* while (strtok_r) */
852 if ((ptr != NULL) || (i != vl->values_len))
853 return (-1);
854 return (0);
855 } /* int parse_values */
857 #if !HAVE_GETPWNAM_R
858 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
859 size_t buflen, struct passwd **pwbufp)
860 {
861 int status = 0;
862 struct passwd *pw;
864 memset (pwbuf, '\0', sizeof (struct passwd));
866 pthread_mutex_lock (&getpwnam_r_lock);
868 do
869 {
870 pw = getpwnam (name);
871 if (pw == NULL)
872 {
873 status = (errno != 0) ? errno : ENOENT;
874 break;
875 }
877 #define GETPWNAM_COPY_MEMBER(member) \
878 if (pw->member != NULL) \
879 { \
880 int len = strlen (pw->member); \
881 if (len >= buflen) \
882 { \
883 status = ENOMEM; \
884 break; \
885 } \
886 sstrncpy (buf, pw->member, buflen); \
887 pwbuf->member = buf; \
888 buf += (len + 1); \
889 buflen -= (len + 1); \
890 }
891 GETPWNAM_COPY_MEMBER(pw_name);
892 GETPWNAM_COPY_MEMBER(pw_passwd);
893 GETPWNAM_COPY_MEMBER(pw_gecos);
894 GETPWNAM_COPY_MEMBER(pw_dir);
895 GETPWNAM_COPY_MEMBER(pw_shell);
897 pwbuf->pw_uid = pw->pw_uid;
898 pwbuf->pw_gid = pw->pw_gid;
900 if (pwbufp != NULL)
901 *pwbufp = pwbuf;
902 } while (0);
904 pthread_mutex_unlock (&getpwnam_r_lock);
906 return (status);
907 } /* int getpwnam_r */
908 #endif /* !HAVE_GETPWNAM_R */
910 int notification_init (notification_t *n, int severity, const char *message,
911 const char *host,
912 const char *plugin, const char *plugin_instance,
913 const char *type, const char *type_instance)
914 {
915 memset (n, '\0', sizeof (notification_t));
917 n->severity = severity;
919 if (message != NULL)
920 sstrncpy (n->message, message, sizeof (n->message));
921 if (host != NULL)
922 sstrncpy (n->host, host, sizeof (n->host));
923 if (plugin != NULL)
924 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
925 if (plugin_instance != NULL)
926 sstrncpy (n->plugin_instance, plugin_instance,
927 sizeof (n->plugin_instance));
928 if (type != NULL)
929 sstrncpy (n->type, type, sizeof (n->type));
930 if (type_instance != NULL)
931 sstrncpy (n->type_instance, type_instance,
932 sizeof (n->type_instance));
934 return (0);
935 } /* int notification_init */
937 int walk_directory (const char *dir, dirwalk_callback_f callback,
938 void *user_data)
939 {
940 struct dirent *ent;
941 DIR *dh;
942 int success;
943 int failure;
945 success = 0;
946 failure = 0;
948 if ((dh = opendir (dir)) == NULL)
949 {
950 char errbuf[1024];
951 ERROR ("walk_directory: Cannot open '%s': %s", dir,
952 sstrerror (errno, errbuf, sizeof (errbuf)));
953 return -1;
954 }
956 while ((ent = readdir (dh)) != NULL)
957 {
958 int status;
960 if (ent->d_name[0] == '.')
961 continue;
963 status = (*callback) (dir, ent->d_name, user_data);
964 if (status != 0)
965 failure++;
966 else
967 success++;
968 }
970 closedir (dh);
972 if ((success == 0) && (failure > 0))
973 return (-1);
974 return (0);
975 }
977 int read_file_contents (const char *filename, char *buf, int bufsize)
978 {
979 FILE *fh;
980 int n;
982 if ((fh = fopen (filename, "r")) == NULL)
983 return -1;
985 n = fread(buf, 1, bufsize, fh);
986 fclose(fh);
988 return n;
989 }
991 counter_t counter_diff (counter_t old_value, counter_t new_value)
992 {
993 counter_t diff;
995 if (old_value > new_value)
996 {
997 if (old_value <= 4294967295U)
998 diff = (4294967295U - old_value) + new_value;
999 else
1000 diff = (18446744073709551615ULL - old_value)
1001 + new_value;
1002 }
1003 else
1004 {
1005 diff = new_value - old_value;
1006 }
1008 return (diff);
1009 } /* counter_t counter_to_gauge */