ae89c44f9aee71ceaf5ceb8df2e117ef418d2d1f
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 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
351 {
352 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
353 return (-2);
355 if ((tv0->tv_sec < tv1->tv_sec)
356 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
357 return (-1);
359 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
360 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
362 if (ret->tv_nsec < 0)
363 {
364 assert (ret->tv_sec > 0);
366 ret->tv_nsec += 1000000000;
367 ret->tv_sec -= 1;
368 }
370 return (0);
371 }
373 int check_create_dir (const char *file_orig)
374 {
375 struct stat statbuf;
377 char file_copy[512];
378 char dir[512];
379 int dir_len = 512;
380 char *fields[16];
381 int fields_num;
382 char *ptr;
383 char *saveptr;
384 int last_is_file = 1;
385 int path_is_absolute = 0;
386 int len;
387 int i;
389 /*
390 * Sanity checks first
391 */
392 if (file_orig == NULL)
393 return (-1);
395 if ((len = strlen (file_orig)) < 1)
396 return (-1);
397 else if (len >= sizeof (file_copy))
398 return (-1);
400 /*
401 * If `file_orig' ends in a slash the last component is a directory,
402 * otherwise it's a file. Act accordingly..
403 */
404 if (file_orig[len - 1] == '/')
405 last_is_file = 0;
406 if (file_orig[0] == '/')
407 path_is_absolute = 1;
409 /*
410 * Create a copy for `strtok_r' to destroy
411 */
412 sstrncpy (file_copy, file_orig, sizeof (file_copy));
414 /*
415 * Break into components. This will eat up several slashes in a row and
416 * remove leading and trailing slashes..
417 */
418 ptr = file_copy;
419 saveptr = NULL;
420 fields_num = 0;
421 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
422 {
423 ptr = NULL;
424 fields_num++;
426 if (fields_num >= 16)
427 break;
428 }
430 /*
431 * For each component, do..
432 */
433 for (i = 0; i < (fields_num - last_is_file); i++)
434 {
435 /*
436 * Do not create directories that start with a dot. This
437 * prevents `../../' attacks and other likely malicious
438 * behavior.
439 */
440 if (fields[i][0] == '.')
441 {
442 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
443 return (-2);
444 }
446 /*
447 * Join the components together again
448 */
449 dir[0] = '/';
450 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
451 fields, i + 1, "/") < 0)
452 {
453 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
454 return (-1);
455 }
457 if (stat (dir, &statbuf) == -1)
458 {
459 if (errno == ENOENT)
460 {
461 if (mkdir (dir, 0755) == -1)
462 {
463 char errbuf[1024];
464 ERROR ("check_create_dir: mkdir (%s): %s", dir,
465 sstrerror (errno,
466 errbuf, sizeof (errbuf)));
467 return (-1);
468 }
469 }
470 else
471 {
472 char errbuf[1024];
473 ERROR ("stat (%s): %s", dir,
474 sstrerror (errno, errbuf,
475 sizeof (errbuf)));
476 return (-1);
477 }
478 }
479 else if (!S_ISDIR (statbuf.st_mode))
480 {
481 ERROR ("stat (%s): Not a directory!", dir);
482 return (-1);
483 }
484 }
486 return (0);
487 } /* check_create_dir */
489 #ifdef HAVE_LIBKSTAT
490 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
491 {
492 char ident[128];
494 if (kc == NULL)
495 return (-1);
497 ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
499 if (*ksp_ptr == NULL)
500 {
501 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
502 {
503 ERROR ("Cound not find kstat %s", ident);
504 return (-1);
505 }
507 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
508 {
509 WARNING ("kstat %s has wrong type", ident);
510 *ksp_ptr = NULL;
511 return (-1);
512 }
513 }
515 #ifdef assert
516 assert (*ksp_ptr != NULL);
517 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
518 #endif
520 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
521 {
522 WARNING ("kstat %s could not be read", ident);
523 return (-1);
524 }
526 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
527 {
528 WARNING ("kstat %s has wrong type", ident);
529 return (-1);
530 }
532 return (0);
533 }
535 long long get_kstat_value (kstat_t *ksp, char *name)
536 {
537 kstat_named_t *kn;
538 long long retval = -1LL;
540 #ifdef assert
541 assert (ksp != NULL);
542 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
543 #else
544 if (ksp == NULL)
545 {
546 ERROR ("ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
547 return (-1LL);
548 }
549 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
550 {
551 ERROR ("ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
552 return (-1LL);
553 }
554 #endif
556 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
557 return (retval);
559 if (kn->data_type == KSTAT_DATA_INT32)
560 retval = (long long) kn->value.i32;
561 else if (kn->data_type == KSTAT_DATA_UINT32)
562 retval = (long long) kn->value.ui32;
563 else if (kn->data_type == KSTAT_DATA_INT64)
564 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
565 else if (kn->data_type == KSTAT_DATA_UINT64)
566 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
567 else
568 WARNING ("get_kstat_value: Not a numeric value: %s", name);
570 return (retval);
571 }
572 #endif /* HAVE_LIBKSTAT */
574 unsigned long long ntohll (unsigned long long n)
575 {
576 #if BYTE_ORDER == BIG_ENDIAN
577 return (n);
578 #else
579 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
580 #endif
581 } /* unsigned long long ntohll */
583 unsigned long long htonll (unsigned long long n)
584 {
585 #if BYTE_ORDER == BIG_ENDIAN
586 return (n);
587 #else
588 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
589 #endif
590 } /* unsigned long long htonll */
592 #if FP_LAYOUT_NEED_NOTHING
593 /* Well, we need nothing.. */
594 /* #endif FP_LAYOUT_NEED_NOTHING */
596 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
597 # if FP_LAYOUT_NEED_ENDIANFLIP
598 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
599 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
600 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
601 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
602 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
603 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
604 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
605 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
606 # else
607 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
608 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
609 # endif
611 double ntohd (double d)
612 {
613 union
614 {
615 uint8_t byte[8];
616 uint64_t integer;
617 double floating;
618 } ret;
620 ret.floating = d;
622 /* NAN in x86 byte order */
623 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
624 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
625 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
626 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
627 {
628 return (NAN);
629 }
630 else
631 {
632 uint64_t tmp;
634 tmp = ret.integer;
635 ret.integer = FP_CONVERT (tmp);
636 return (ret.floating);
637 }
638 } /* double ntohd */
640 double htond (double d)
641 {
642 union
643 {
644 uint8_t byte[8];
645 uint64_t integer;
646 double floating;
647 } ret;
649 if (isnan (d))
650 {
651 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
652 ret.byte[4] = ret.byte[5] = 0x00;
653 ret.byte[6] = 0xf8;
654 ret.byte[7] = 0x7f;
655 return (ret.floating);
656 }
657 else
658 {
659 uint64_t tmp;
661 ret.floating = d;
662 tmp = FP_CONVERT (ret.integer);
663 ret.integer = tmp;
664 return (ret.floating);
665 }
666 } /* double htond */
667 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
669 int format_name (char *ret, int ret_len,
670 const char *hostname,
671 const char *plugin, const char *plugin_instance,
672 const char *type, const char *type_instance)
673 {
674 int status;
676 assert (plugin != NULL);
677 assert (type != NULL);
679 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
680 {
681 if ((type_instance == NULL) || (strlen (type_instance) == 0))
682 status = ssnprintf (ret, ret_len, "%s/%s/%s",
683 hostname, plugin, type);
684 else
685 status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
686 hostname, plugin, type,
687 type_instance);
688 }
689 else
690 {
691 if ((type_instance == NULL) || (strlen (type_instance) == 0))
692 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
693 hostname, plugin, plugin_instance,
694 type);
695 else
696 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
697 hostname, plugin, plugin_instance,
698 type, type_instance);
699 }
701 if ((status < 1) || (status >= ret_len))
702 return (-1);
703 return (0);
704 } /* int format_name */
706 int parse_identifier (char *str, char **ret_host,
707 char **ret_plugin, char **ret_plugin_instance,
708 char **ret_type, char **ret_type_instance)
709 {
710 char *hostname = NULL;
711 char *plugin = NULL;
712 char *plugin_instance = NULL;
713 char *type = NULL;
714 char *type_instance = NULL;
716 hostname = str;
717 if (hostname == NULL)
718 return (-1);
720 plugin = strchr (hostname, '/');
721 if (plugin == NULL)
722 return (-1);
723 *plugin = '\0'; plugin++;
725 type = strchr (plugin, '/');
726 if (type == NULL)
727 return (-1);
728 *type = '\0'; type++;
730 plugin_instance = strchr (plugin, '-');
731 if (plugin_instance != NULL)
732 {
733 *plugin_instance = '\0';
734 plugin_instance++;
735 }
737 type_instance = strchr (type, '-');
738 if (type_instance != NULL)
739 {
740 *type_instance = '\0';
741 type_instance++;
742 }
744 *ret_host = hostname;
745 *ret_plugin = plugin;
746 *ret_plugin_instance = plugin_instance;
747 *ret_type = type;
748 *ret_type_instance = type_instance;
749 return (0);
750 } /* int parse_identifier */
752 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
753 {
754 int i;
755 char *dummy;
756 char *ptr;
757 char *saveptr;
759 i = -1;
760 dummy = buffer;
761 saveptr = NULL;
762 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
763 {
764 dummy = NULL;
766 if (i >= vl->values_len)
767 break;
769 if (i == -1)
770 {
771 if (strcmp ("N", ptr) == 0)
772 vl->time = time (NULL);
773 else
774 vl->time = (time_t) atoi (ptr);
775 }
776 else
777 {
778 if (strcmp ("U", ptr) == 0)
779 vl->values[i].gauge = NAN;
780 else if (ds->ds[i].type == DS_TYPE_COUNTER)
781 vl->values[i].counter = atoll (ptr);
782 else if (ds->ds[i].type == DS_TYPE_GAUGE)
783 vl->values[i].gauge = atof (ptr);
784 }
786 i++;
787 } /* while (strtok_r) */
789 if ((ptr != NULL) || (i != vl->values_len))
790 return (-1);
791 return (0);
792 } /* int parse_values */
794 #if !HAVE_GETPWNAM_R
795 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
796 size_t buflen, struct passwd **pwbufp)
797 {
798 int status = 0;
799 struct passwd *pw;
801 memset (pwbuf, '\0', sizeof (struct passwd));
803 pthread_mutex_lock (&getpwnam_r_lock);
805 do
806 {
807 pw = getpwnam (name);
808 if (pw == NULL)
809 {
810 status = (errno != 0) ? errno : ENOENT;
811 break;
812 }
814 #define GETPWNAM_COPY_MEMBER(member) \
815 if (pw->member != NULL) \
816 { \
817 int len = strlen (pw->member); \
818 if (len >= buflen) \
819 { \
820 status = ENOMEM; \
821 break; \
822 } \
823 sstrncpy (buf, pw->member, buflen); \
824 pwbuf->member = buf; \
825 buf += (len + 1); \
826 buflen -= (len + 1); \
827 }
828 GETPWNAM_COPY_MEMBER(pw_name);
829 GETPWNAM_COPY_MEMBER(pw_passwd);
830 GETPWNAM_COPY_MEMBER(pw_gecos);
831 GETPWNAM_COPY_MEMBER(pw_dir);
832 GETPWNAM_COPY_MEMBER(pw_shell);
834 pwbuf->pw_uid = pw->pw_uid;
835 pwbuf->pw_gid = pw->pw_gid;
837 if (pwbufp != NULL)
838 *pwbufp = pwbuf;
839 } while (0);
841 pthread_mutex_unlock (&getpwnam_r_lock);
843 return (status);
844 } /* int getpwnam_r */
845 #endif /* !HAVE_GETPWNAM_R */
847 int notification_init (notification_t *n, int severity, const char *message,
848 const char *host,
849 const char *plugin, const char *plugin_instance,
850 const char *type, const char *type_instance)
851 {
852 memset (n, '\0', sizeof (notification_t));
854 n->severity = severity;
856 if (message != NULL)
857 sstrncpy (n->message, message, sizeof (n->message));
858 if (host != NULL)
859 sstrncpy (n->host, host, sizeof (n->host));
860 if (plugin != NULL)
861 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
862 if (plugin_instance != NULL)
863 sstrncpy (n->plugin_instance, plugin_instance,
864 sizeof (n->plugin_instance));
865 if (type != NULL)
866 sstrncpy (n->type, type, sizeof (n->type));
867 if (type_instance != NULL)
868 sstrncpy (n->type_instance, type_instance,
869 sizeof (n->type_instance));
871 return (0);
872 } /* int notification_init */
874 int walk_directory (const char *dir, dirwalk_callback_f callback,
875 void *user_data)
876 {
877 struct dirent *ent;
878 DIR *dh;
879 int success;
880 int failure;
882 success = 0;
883 failure = 0;
885 if ((dh = opendir (dir)) == NULL)
886 {
887 char errbuf[1024];
888 ERROR ("walk_directory: Cannot open '%s': %s", dir,
889 sstrerror (errno, errbuf, sizeof (errbuf)));
890 return -1;
891 }
893 while ((ent = readdir (dh)) != NULL)
894 {
895 int status;
897 if (ent->d_name[0] == '.')
898 continue;
900 status = (*callback) (dir, ent->d_name, user_data);
901 if (status != 0)
902 failure++;
903 else
904 success++;
905 }
907 closedir (dh);
909 if ((success == 0) && (failure > 0))
910 return (-1);
911 return (0);
912 }
914 int read_file_contents (const char *filename, char *buf, int bufsize)
915 {
916 FILE *fh;
917 int n;
919 if ((fh = fopen (filename, "r")) == NULL)
920 return -1;
922 n = fread(buf, 1, bufsize, fh);
923 fclose(fh);
925 return n;
926 }