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;
81 if (s == NULL)
82 return (NULL);
84 if((r = strdup (s)) == NULL)
85 {
86 ERROR ("Not enough memory.");
87 exit (3);
88 }
90 return (r);
91 } /* char *sstrdup */
93 /* Even though Posix requires "strerror_r" to return an "int",
94 * some systems (e.g. the GNU libc) return a "char *" _and_
95 * ignore the second argument ... -tokkee */
96 char *sstrerror (int errnum, char *buf, size_t buflen)
97 {
98 buf[0] = '\0';
100 #if !HAVE_STRERROR_R
101 {
102 char *temp;
104 pthread_mutex_lock (&strerror_r_lock);
106 temp = strerror (errnum);
107 sstrncpy (buf, temp, buflen);
109 pthread_mutex_unlock (&strerror_r_lock);
110 }
111 /* #endif !HAVE_STRERROR_R */
113 #elif STRERROR_R_CHAR_P
114 {
115 char *temp;
116 temp = strerror_r (errnum, buf, buflen);
117 if (buf[0] == '\0')
118 {
119 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
120 sstrncpy (buf, temp, buflen);
121 else
122 sstrncpy (buf, "strerror_r did not return "
123 "an error message", buflen);
124 }
125 }
126 /* #endif STRERROR_R_CHAR_P */
128 #else
129 if (strerror_r (errnum, buf, buflen) != 0)
130 {
131 ssnprintf (buf, buflen, "Error #%i; "
132 "Additionally, strerror_r failed.",
133 errnum);
134 }
135 #endif /* STRERROR_R_CHAR_P */
137 return (buf);
138 } /* char *sstrerror */
140 void *smalloc (size_t size)
141 {
142 void *r;
144 if ((r = malloc (size)) == NULL)
145 {
146 ERROR ("Not enough memory.");
147 exit (3);
148 }
150 return (r);
151 } /* void *smalloc */
153 #if 0
154 void sfree (void **ptr)
155 {
156 if (ptr == NULL)
157 return;
159 if (*ptr != NULL)
160 free (*ptr);
162 *ptr = NULL;
163 }
164 #endif
166 ssize_t sread (int fd, void *buf, size_t count)
167 {
168 char *ptr;
169 size_t nleft;
170 ssize_t status;
172 ptr = (char *) buf;
173 nleft = count;
175 while (nleft > 0)
176 {
177 status = read (fd, (void *) ptr, nleft);
179 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
180 continue;
182 if (status < 0)
183 return (status);
185 if (status == 0)
186 {
187 DEBUG ("Received EOF from fd %i. "
188 "Closing fd and returning error.",
189 fd);
190 close (fd);
191 return (-1);
192 }
194 assert ((0 > status) || (nleft >= (size_t)status));
196 nleft = nleft - status;
197 ptr = ptr + status;
198 }
200 return (0);
201 }
204 ssize_t swrite (int fd, const void *buf, size_t count)
205 {
206 const char *ptr;
207 size_t nleft;
208 ssize_t status;
210 ptr = (const char *) buf;
211 nleft = count;
213 while (nleft > 0)
214 {
215 status = write (fd, (const void *) ptr, nleft);
217 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
218 continue;
220 if (status < 0)
221 return (status);
223 nleft = nleft - status;
224 ptr = ptr + status;
225 }
227 return (0);
228 }
230 int strsplit (char *string, char **fields, size_t size)
231 {
232 size_t i;
233 char *ptr;
234 char *saveptr;
236 i = 0;
237 ptr = string;
238 saveptr = NULL;
239 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
240 {
241 ptr = NULL;
242 i++;
244 if (i >= size)
245 break;
246 }
248 return (i);
249 }
251 int strjoin (char *dst, size_t dst_len,
252 char **fields, size_t fields_num,
253 const char *sep)
254 {
255 size_t field_len;
256 size_t sep_len;
257 int i;
259 memset (dst, '\0', dst_len);
261 if (fields_num <= 0)
262 return (-1);
264 sep_len = 0;
265 if (sep != NULL)
266 sep_len = strlen (sep);
268 for (i = 0; i < (int)fields_num; i++)
269 {
270 if ((i > 0) && (sep_len > 0))
271 {
272 if (dst_len <= sep_len)
273 return (-1);
275 strncat (dst, sep, dst_len);
276 dst_len -= sep_len;
277 }
279 field_len = strlen (fields[i]);
281 if (dst_len <= field_len)
282 return (-1);
284 strncat (dst, fields[i], dst_len);
285 dst_len -= field_len;
286 }
288 return (strlen (dst));
289 }
291 int strsubstitute (char *str, char c_from, char c_to)
292 {
293 int ret;
295 if (str == NULL)
296 return (-1);
298 ret = 0;
299 while (*str != '\0')
300 {
301 if (*str == c_from)
302 {
303 *str = c_to;
304 ret++;
305 }
306 str++;
307 }
309 return (ret);
310 } /* int strsubstitute */
312 int escape_slashes (char *buf, int buf_len)
313 {
314 int i;
316 if (strcmp (buf, "/") == 0)
317 {
318 if (buf_len < 5)
319 return (-1);
321 strncpy (buf, "root", buf_len);
322 return (0);
323 }
325 if (buf_len <= 1)
326 return (0);
328 /* Move one to the left */
329 if (buf[0] == '/')
330 memmove (buf, buf + 1, buf_len - 1);
332 for (i = 0; i < buf_len - 1; i++)
333 {
334 if (buf[i] == '\0')
335 break;
336 else if (buf[i] == '/')
337 buf[i] = '_';
338 }
339 buf[i] = '\0';
341 return (0);
342 } /* int escape_slashes */
344 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
345 {
346 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
347 return (-2);
349 if ((tv0->tv_sec < tv1->tv_sec)
350 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
351 return (-1);
353 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
354 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
356 if (ret->tv_nsec < 0)
357 {
358 assert (ret->tv_sec > 0);
360 ret->tv_nsec += 1000000000;
361 ret->tv_sec -= 1;
362 }
364 return (0);
365 }
367 int check_create_dir (const char *file_orig)
368 {
369 struct stat statbuf;
371 char file_copy[512];
372 char dir[512];
373 int dir_len = 512;
374 char *fields[16];
375 int fields_num;
376 char *ptr;
377 char *saveptr;
378 int last_is_file = 1;
379 int path_is_absolute = 0;
380 int len;
381 int i;
383 /*
384 * Sanity checks first
385 */
386 if (file_orig == NULL)
387 return (-1);
389 if ((len = strlen (file_orig)) < 1)
390 return (-1);
391 else if (len >= sizeof (file_copy))
392 return (-1);
394 /*
395 * If `file_orig' ends in a slash the last component is a directory,
396 * otherwise it's a file. Act accordingly..
397 */
398 if (file_orig[len - 1] == '/')
399 last_is_file = 0;
400 if (file_orig[0] == '/')
401 path_is_absolute = 1;
403 /*
404 * Create a copy for `strtok_r' to destroy
405 */
406 sstrncpy (file_copy, file_orig, sizeof (file_copy));
408 /*
409 * Break into components. This will eat up several slashes in a row and
410 * remove leading and trailing slashes..
411 */
412 ptr = file_copy;
413 saveptr = NULL;
414 fields_num = 0;
415 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
416 {
417 ptr = NULL;
418 fields_num++;
420 if (fields_num >= 16)
421 break;
422 }
424 /*
425 * For each component, do..
426 */
427 for (i = 0; i < (fields_num - last_is_file); i++)
428 {
429 /*
430 * Do not create directories that start with a dot. This
431 * prevents `../../' attacks and other likely malicious
432 * behavior.
433 */
434 if (fields[i][0] == '.')
435 {
436 ERROR ("Cowardly refusing to create a directory that "
437 "begins with a `.' (dot): `%s'", file_orig);
438 return (-2);
439 }
441 /*
442 * Join the components together again
443 */
444 dir[0] = '/';
445 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
446 fields, i + 1, "/") < 0)
447 {
448 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
449 return (-1);
450 }
452 while (42) {
453 if (stat (dir, &statbuf) == -1)
454 {
455 if (errno == ENOENT)
456 {
457 if (mkdir (dir, 0755) == 0)
458 break;
460 /* this might happen, if a different thread created
461 * the directory in the meantime
462 * => call stat() again to check for S_ISDIR() */
463 if (EEXIST == errno)
464 continue;
466 char errbuf[1024];
467 ERROR ("check_create_dir: mkdir (%s): %s", dir,
468 sstrerror (errno,
469 errbuf, sizeof (errbuf)));
470 return (-1);
471 }
472 else
473 {
474 char errbuf[1024];
475 ERROR ("check_create_dir: stat (%s): %s", dir,
476 sstrerror (errno, errbuf,
477 sizeof (errbuf)));
478 return (-1);
479 }
480 }
481 else if (!S_ISDIR (statbuf.st_mode))
482 {
483 ERROR ("check_create_dir: `%s' exists but is not "
484 "a directory!", dir);
485 return (-1);
486 }
487 break;
488 }
489 }
491 return (0);
492 } /* check_create_dir */
494 #ifdef HAVE_LIBKSTAT
495 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
496 {
497 char ident[128];
499 if (kc == NULL)
500 return (-1);
502 ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
504 if (*ksp_ptr == NULL)
505 {
506 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
507 {
508 ERROR ("Cound not find kstat %s", ident);
509 return (-1);
510 }
512 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
513 {
514 WARNING ("kstat %s has wrong type", ident);
515 *ksp_ptr = NULL;
516 return (-1);
517 }
518 }
520 #ifdef assert
521 assert (*ksp_ptr != NULL);
522 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
523 #endif
525 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
526 {
527 WARNING ("kstat %s could not be read", ident);
528 return (-1);
529 }
531 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
532 {
533 WARNING ("kstat %s has wrong type", ident);
534 return (-1);
535 }
537 return (0);
538 }
540 long long get_kstat_value (kstat_t *ksp, char *name)
541 {
542 kstat_named_t *kn;
543 long long retval = -1LL;
545 #ifdef assert
546 assert (ksp != NULL);
547 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
548 #else
549 if (ksp == NULL)
550 {
551 ERROR ("ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
552 return (-1LL);
553 }
554 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
555 {
556 ERROR ("ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
557 return (-1LL);
558 }
559 #endif
561 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
562 return (retval);
564 if (kn->data_type == KSTAT_DATA_INT32)
565 retval = (long long) kn->value.i32;
566 else if (kn->data_type == KSTAT_DATA_UINT32)
567 retval = (long long) kn->value.ui32;
568 else if (kn->data_type == KSTAT_DATA_INT64)
569 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
570 else if (kn->data_type == KSTAT_DATA_UINT64)
571 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
572 else
573 WARNING ("get_kstat_value: Not a numeric value: %s", name);
575 return (retval);
576 }
577 #endif /* HAVE_LIBKSTAT */
579 unsigned long long ntohll (unsigned long long n)
580 {
581 #if BYTE_ORDER == BIG_ENDIAN
582 return (n);
583 #else
584 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
585 #endif
586 } /* unsigned long long ntohll */
588 unsigned long long htonll (unsigned long long n)
589 {
590 #if BYTE_ORDER == BIG_ENDIAN
591 return (n);
592 #else
593 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
594 #endif
595 } /* unsigned long long htonll */
597 #if FP_LAYOUT_NEED_NOTHING
598 /* Well, we need nothing.. */
599 /* #endif FP_LAYOUT_NEED_NOTHING */
601 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
602 # if FP_LAYOUT_NEED_ENDIANFLIP
603 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
604 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
605 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
606 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
607 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
608 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
609 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
610 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
611 # else
612 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
613 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
614 # endif
616 double ntohd (double d)
617 {
618 union
619 {
620 uint8_t byte[8];
621 uint64_t integer;
622 double floating;
623 } ret;
625 ret.floating = d;
627 /* NAN in x86 byte order */
628 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
629 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
630 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
631 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
632 {
633 return (NAN);
634 }
635 else
636 {
637 uint64_t tmp;
639 tmp = ret.integer;
640 ret.integer = FP_CONVERT (tmp);
641 return (ret.floating);
642 }
643 } /* double ntohd */
645 double htond (double d)
646 {
647 union
648 {
649 uint8_t byte[8];
650 uint64_t integer;
651 double floating;
652 } ret;
654 if (isnan (d))
655 {
656 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
657 ret.byte[4] = ret.byte[5] = 0x00;
658 ret.byte[6] = 0xf8;
659 ret.byte[7] = 0x7f;
660 return (ret.floating);
661 }
662 else
663 {
664 uint64_t tmp;
666 ret.floating = d;
667 tmp = FP_CONVERT (ret.integer);
668 ret.integer = tmp;
669 return (ret.floating);
670 }
671 } /* double htond */
672 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
674 int format_name (char *ret, int ret_len,
675 const char *hostname,
676 const char *plugin, const char *plugin_instance,
677 const char *type, const char *type_instance)
678 {
679 int status;
681 assert (plugin != NULL);
682 assert (type != NULL);
684 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
685 {
686 if ((type_instance == NULL) || (strlen (type_instance) == 0))
687 status = ssnprintf (ret, ret_len, "%s/%s/%s",
688 hostname, plugin, type);
689 else
690 status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
691 hostname, plugin, type,
692 type_instance);
693 }
694 else
695 {
696 if ((type_instance == NULL) || (strlen (type_instance) == 0))
697 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
698 hostname, plugin, plugin_instance,
699 type);
700 else
701 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
702 hostname, plugin, plugin_instance,
703 type, type_instance);
704 }
706 if ((status < 1) || (status >= ret_len))
707 return (-1);
708 return (0);
709 } /* int format_name */
711 int parse_identifier (char *str, char **ret_host,
712 char **ret_plugin, char **ret_plugin_instance,
713 char **ret_type, char **ret_type_instance)
714 {
715 char *hostname = NULL;
716 char *plugin = NULL;
717 char *plugin_instance = NULL;
718 char *type = NULL;
719 char *type_instance = NULL;
721 hostname = str;
722 if (hostname == NULL)
723 return (-1);
725 plugin = strchr (hostname, '/');
726 if (plugin == NULL)
727 return (-1);
728 *plugin = '\0'; plugin++;
730 type = strchr (plugin, '/');
731 if (type == NULL)
732 return (-1);
733 *type = '\0'; type++;
735 plugin_instance = strchr (plugin, '-');
736 if (plugin_instance != NULL)
737 {
738 *plugin_instance = '\0';
739 plugin_instance++;
740 }
742 type_instance = strchr (type, '-');
743 if (type_instance != NULL)
744 {
745 *type_instance = '\0';
746 type_instance++;
747 }
749 *ret_host = hostname;
750 *ret_plugin = plugin;
751 *ret_plugin_instance = plugin_instance;
752 *ret_type = type;
753 *ret_type_instance = type_instance;
754 return (0);
755 } /* int parse_identifier */
757 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
758 {
759 int i;
760 char *dummy;
761 char *ptr;
762 char *saveptr;
764 i = -1;
765 dummy = buffer;
766 saveptr = NULL;
767 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
768 {
769 dummy = NULL;
771 if (i >= vl->values_len)
772 break;
774 if (i == -1)
775 {
776 if (strcmp ("N", ptr) == 0)
777 vl->time = time (NULL);
778 else
779 vl->time = (time_t) atoi (ptr);
780 }
781 else
782 {
783 if (strcmp ("U", ptr) == 0)
784 vl->values[i].gauge = NAN;
785 else if (ds->ds[i].type == DS_TYPE_COUNTER)
786 vl->values[i].counter = atoll (ptr);
787 else if (ds->ds[i].type == DS_TYPE_GAUGE)
788 vl->values[i].gauge = atof (ptr);
789 }
791 i++;
792 } /* while (strtok_r) */
794 if ((ptr != NULL) || (i != vl->values_len))
795 return (-1);
796 return (0);
797 } /* int parse_values */
799 #if !HAVE_GETPWNAM_R
800 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
801 size_t buflen, struct passwd **pwbufp)
802 {
803 int status = 0;
804 struct passwd *pw;
806 memset (pwbuf, '\0', sizeof (struct passwd));
808 pthread_mutex_lock (&getpwnam_r_lock);
810 do
811 {
812 pw = getpwnam (name);
813 if (pw == NULL)
814 {
815 status = (errno != 0) ? errno : ENOENT;
816 break;
817 }
819 #define GETPWNAM_COPY_MEMBER(member) \
820 if (pw->member != NULL) \
821 { \
822 int len = strlen (pw->member); \
823 if (len >= buflen) \
824 { \
825 status = ENOMEM; \
826 break; \
827 } \
828 sstrncpy (buf, pw->member, buflen); \
829 pwbuf->member = buf; \
830 buf += (len + 1); \
831 buflen -= (len + 1); \
832 }
833 GETPWNAM_COPY_MEMBER(pw_name);
834 GETPWNAM_COPY_MEMBER(pw_passwd);
835 GETPWNAM_COPY_MEMBER(pw_gecos);
836 GETPWNAM_COPY_MEMBER(pw_dir);
837 GETPWNAM_COPY_MEMBER(pw_shell);
839 pwbuf->pw_uid = pw->pw_uid;
840 pwbuf->pw_gid = pw->pw_gid;
842 if (pwbufp != NULL)
843 *pwbufp = pwbuf;
844 } while (0);
846 pthread_mutex_unlock (&getpwnam_r_lock);
848 return (status);
849 } /* int getpwnam_r */
850 #endif /* !HAVE_GETPWNAM_R */
852 int notification_init (notification_t *n, int severity, const char *message,
853 const char *host,
854 const char *plugin, const char *plugin_instance,
855 const char *type, const char *type_instance)
856 {
857 memset (n, '\0', sizeof (notification_t));
859 n->severity = severity;
861 if (message != NULL)
862 sstrncpy (n->message, message, sizeof (n->message));
863 if (host != NULL)
864 sstrncpy (n->host, host, sizeof (n->host));
865 if (plugin != NULL)
866 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
867 if (plugin_instance != NULL)
868 sstrncpy (n->plugin_instance, plugin_instance,
869 sizeof (n->plugin_instance));
870 if (type != NULL)
871 sstrncpy (n->type, type, sizeof (n->type));
872 if (type_instance != NULL)
873 sstrncpy (n->type_instance, type_instance,
874 sizeof (n->type_instance));
876 return (0);
877 } /* int notification_init */
879 int walk_directory (const char *dir, dirwalk_callback_f callback,
880 void *user_data)
881 {
882 struct dirent *ent;
883 DIR *dh;
884 int success;
885 int failure;
887 success = 0;
888 failure = 0;
890 if ((dh = opendir (dir)) == NULL)
891 {
892 char errbuf[1024];
893 ERROR ("walk_directory: Cannot open '%s': %s", dir,
894 sstrerror (errno, errbuf, sizeof (errbuf)));
895 return -1;
896 }
898 while ((ent = readdir (dh)) != NULL)
899 {
900 int status;
902 if (ent->d_name[0] == '.')
903 continue;
905 status = (*callback) (dir, ent->d_name, user_data);
906 if (status != 0)
907 failure++;
908 else
909 success++;
910 }
912 closedir (dh);
914 if ((success == 0) && (failure > 0))
915 return (-1);
916 return (0);
917 }
919 int read_file_contents (const char *filename, char *buf, int bufsize)
920 {
921 FILE *fh;
922 int n;
924 if ((fh = fopen (filename, "r")) == NULL)
925 return -1;
927 n = fread(buf, 1, bufsize, fh);
928 fclose(fh);
930 return n;
931 }