Merge branch 'collectd-4.4' into collectd-4.5
[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 **/
22
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30
31 #if HAVE_PTHREAD_H
32 # include <pthread.h>
33 #endif
34
35 #ifdef HAVE_MATH_H
36 # include <math.h>
37 #endif
38
39 /* for ntohl and htonl */
40 #if HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
42 #endif
43
44 #ifdef HAVE_LIBKSTAT
45 extern kstat_ctl_t *kc;
46 #endif
47
48 #if !HAVE_GETPWNAM_R
49 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
50 #endif
51
52 #if !HAVE_STRERROR_R
53 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
54 #endif
55
56 char *sstrncpy (char *dest, const char *src, size_t n)
57 {
58         strncpy (dest, src, n);
59         dest[n - 1] = '\0';
60
61         return (dest);
62 } /* char *sstrncpy */
63
64 int ssnprintf (char *dest, size_t n, const char *format, ...)
65 {
66         int ret = 0;
67         va_list ap;
68
69         va_start (ap, format);
70         ret = vsnprintf (dest, n, format, ap);
71         dest[n - 1] = '\0';
72         va_end (ap);
73
74         return (ret);
75 } /* int ssnprintf */
76
77 char *sstrdup (const char *s)
78 {
79         char *r;
80
81         if (s == NULL)
82                 return (NULL);
83
84         if((r = strdup (s)) == NULL)
85         {
86                 ERROR ("Not enough memory.");
87                 exit (3);
88         }
89
90         return (r);
91 } /* char *sstrdup */
92
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';
99
100 #if !HAVE_STRERROR_R
101         {
102                 char *temp;
103
104                 pthread_mutex_lock (&strerror_r_lock);
105
106                 temp = strerror (errnum);
107                 sstrncpy (buf, temp, buflen);
108
109                 pthread_mutex_unlock (&strerror_r_lock);
110         }
111 /* #endif !HAVE_STRERROR_R */
112
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 */
127
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 */
136
137         return (buf);
138 } /* char *sstrerror */
139
140 void *smalloc (size_t size)
141 {
142         void *r;
143
144         if ((r = malloc (size)) == NULL)
145         {
146                 ERROR ("Not enough memory.");
147                 exit (3);
148         }
149
150         return (r);
151 } /* void *smalloc */
152
153 #if 0
154 void sfree (void **ptr)
155 {
156         if (ptr == NULL)
157                 return;
158
159         if (*ptr != NULL)
160                 free (*ptr);
161
162         *ptr = NULL;
163 }
164 #endif
165
166 ssize_t sread (int fd, void *buf, size_t count)
167 {
168         char    *ptr;
169         size_t   nleft;
170         ssize_t  status;
171
172         ptr   = (char *) buf;
173         nleft = count;
174
175         while (nleft > 0)
176         {
177                 status = read (fd, (void *) ptr, nleft);
178
179                 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
180                         continue;
181
182                 if (status < 0)
183                         return (status);
184
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                 }
193
194                 assert ((0 > status) || (nleft >= (size_t)status));
195
196                 nleft = nleft - status;
197                 ptr   = ptr   + status;
198         }
199
200         return (0);
201 }
202
203
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;
209
210         ptr   = (const char *) buf;
211         nleft = count;
212
213         while (nleft > 0)
214         {
215                 status = write (fd, (const void *) ptr, nleft);
216
217                 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
218                         continue;
219
220                 if (status < 0)
221                         return (status);
222
223                 nleft = nleft - status;
224                 ptr   = ptr   + status;
225         }
226
227         return (0);
228 }
229
230 int strsplit (char *string, char **fields, size_t size)
231 {
232         size_t i;
233         char *ptr;
234         char *saveptr;
235
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++;
243
244                 if (i >= size)
245                         break;
246         }
247
248         return (i);
249 }
250
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;
258
259         memset (dst, '\0', dst_len);
260
261         if (fields_num <= 0)
262                 return (-1);
263
264         sep_len = 0;
265         if (sep != NULL)
266                 sep_len = strlen (sep);
267
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);
274
275                         strncat (dst, sep, dst_len);
276                         dst_len -= sep_len;
277                 }
278
279                 field_len = strlen (fields[i]);
280
281                 if (dst_len <= field_len)
282                         return (-1);
283
284                 strncat (dst, fields[i], dst_len);
285                 dst_len -= field_len;
286         }
287
288         return (strlen (dst));
289 }
290
291 int strsubstitute (char *str, char c_from, char c_to)
292 {
293         int ret;
294
295         if (str == NULL)
296                 return (-1);
297
298         ret = 0;
299         while (*str != '\0')
300         {
301                 if (*str == c_from)
302                 {
303                         *str = c_to;
304                         ret++;
305                 }
306                 str++;
307         }
308
309         return (ret);
310 } /* int strsubstitute */
311
312 int escape_slashes (char *buf, int buf_len)
313 {
314         int i;
315
316         if (strcmp (buf, "/") == 0)
317         {
318                 if (buf_len < 5)
319                         return (-1);
320
321                 strncpy (buf, "root", buf_len);
322                 return (0);
323         }
324
325         if (buf_len <= 1)
326                 return (0);
327
328         /* Move one to the left */
329         if (buf[0] == '/')
330                 memmove (buf, buf + 1, buf_len - 1);
331
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';
340
341         return (0);
342 } /* int escape_slashes */
343
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);
348
349         if ((tv0->tv_sec < tv1->tv_sec)
350                         || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
351                 return (-1);
352
353         ret->tv_sec  = tv0->tv_sec - tv1->tv_sec;
354         ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
355
356         if (ret->tv_nsec < 0)
357         {
358                 assert (ret->tv_sec > 0);
359
360                 ret->tv_nsec += 1000000000;
361                 ret->tv_sec  -= 1;
362         }
363
364         return (0);
365 }
366
367 int check_create_dir (const char *file_orig)
368 {
369         struct stat statbuf;
370
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;
382
383         /*
384          * Sanity checks first
385          */
386         if (file_orig == NULL)
387                 return (-1);
388
389         if ((len = strlen (file_orig)) < 1)
390                 return (-1);
391         else if (len >= sizeof (file_copy))
392                 return (-1);
393
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;
402
403         /*
404          * Create a copy for `strtok_r' to destroy
405          */
406         sstrncpy (file_copy, file_orig, sizeof (file_copy));
407
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++;
419
420                 if (fields_num >= 16)
421                         break;
422         }
423
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 begins with a `.' (dot): `%s'", file_orig);
437                         return (-2);
438                 }
439
440                 /*
441                  * Join the components together again
442                  */
443                 dir[0] = '/';
444                 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
445                                         fields, i + 1, "/") < 0)
446                 {
447                         ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
448                         return (-1);
449                 }
450
451                 if (stat (dir, &statbuf) == -1)
452                 {
453                         if (errno == ENOENT)
454                         {
455                                 if (mkdir (dir, 0755) == -1)
456                                 {
457                                         char errbuf[1024];
458                                         ERROR ("check_create_dir: mkdir (%s): %s", dir,
459                                                         sstrerror (errno,
460                                                                 errbuf, sizeof (errbuf)));
461                                         return (-1);
462                                 }
463                         }
464                         else
465                         {
466                                 char errbuf[1024];
467                                 ERROR ("stat (%s): %s", dir,
468                                                 sstrerror (errno, errbuf,
469                                                         sizeof (errbuf)));
470                                 return (-1);
471                         }
472                 }
473                 else if (!S_ISDIR (statbuf.st_mode))
474                 {
475                         ERROR ("stat (%s): Not a directory!", dir);
476                         return (-1);
477                 }
478         }
479
480         return (0);
481 } /* check_create_dir */
482
483 #ifdef HAVE_LIBKSTAT
484 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
485 {
486         char ident[128];
487         
488         if (kc == NULL)
489                 return (-1);
490
491         ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
492
493         if (*ksp_ptr == NULL)
494         {
495                 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
496                 {
497                         ERROR ("Cound not find kstat %s", ident);
498                         return (-1);
499                 }
500
501                 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
502                 {
503                         WARNING ("kstat %s has wrong type", ident);
504                         *ksp_ptr = NULL;
505                         return (-1);
506                 }
507         }
508
509 #ifdef assert
510         assert (*ksp_ptr != NULL);
511         assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
512 #endif
513
514         if (kstat_read (kc, *ksp_ptr, NULL) == -1)
515         {
516                 WARNING ("kstat %s could not be read", ident);
517                 return (-1);
518         }
519
520         if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
521         {
522                 WARNING ("kstat %s has wrong type", ident);
523                 return (-1);
524         }
525
526         return (0);
527 }
528
529 long long get_kstat_value (kstat_t *ksp, char *name)
530 {
531         kstat_named_t *kn;
532         long long retval = -1LL;
533
534 #ifdef assert
535         assert (ksp != NULL);
536         assert (ksp->ks_type == KSTAT_TYPE_NAMED);
537 #else
538         if (ksp == NULL)
539         {
540                 ERROR ("ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
541                 return (-1LL);
542         }
543         else if (ksp->ks_type != KSTAT_TYPE_NAMED)
544         {
545                 ERROR ("ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
546                 return (-1LL);
547         }
548 #endif
549
550         if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
551                 return (retval);
552
553         if (kn->data_type == KSTAT_DATA_INT32)
554                 retval = (long long) kn->value.i32;
555         else if (kn->data_type == KSTAT_DATA_UINT32)
556                 retval = (long long) kn->value.ui32;
557         else if (kn->data_type == KSTAT_DATA_INT64)
558                 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
559         else if (kn->data_type == KSTAT_DATA_UINT64)
560                 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
561         else
562                 WARNING ("get_kstat_value: Not a numeric value: %s", name);
563                  
564         return (retval);
565 }
566 #endif /* HAVE_LIBKSTAT */
567
568 unsigned long long ntohll (unsigned long long n)
569 {
570 #if BYTE_ORDER == BIG_ENDIAN
571         return (n);
572 #else
573         return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
574 #endif
575 } /* unsigned long long ntohll */
576
577 unsigned long long htonll (unsigned long long n)
578 {
579 #if BYTE_ORDER == BIG_ENDIAN
580         return (n);
581 #else
582         return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
583 #endif
584 } /* unsigned long long htonll */
585
586 #if FP_LAYOUT_NEED_NOTHING
587 /* Well, we need nothing.. */
588 /* #endif FP_LAYOUT_NEED_NOTHING */
589
590 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
591 # if FP_LAYOUT_NEED_ENDIANFLIP
592 #  define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
593                          (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
594                          (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
595                          (((uint64_t)(A) & 0x000000ff00000000LL) >> 8)  | \
596                          (((uint64_t)(A) & 0x00000000ff000000LL) << 8)  | \
597                          (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
598                          (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
599                          (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
600 # else
601 #  define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
602                          (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
603 # endif
604
605 double ntohd (double d)
606 {
607         union
608         {
609                 uint8_t  byte[8];
610                 uint64_t integer;
611                 double   floating;
612         } ret;
613
614         ret.floating = d;
615
616         /* NAN in x86 byte order */
617         if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
618                         && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
619                         && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
620                         && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
621         {
622                 return (NAN);
623         }
624         else
625         {
626                 uint64_t tmp;
627
628                 tmp = ret.integer;
629                 ret.integer = FP_CONVERT (tmp);
630                 return (ret.floating);
631         }
632 } /* double ntohd */
633
634 double htond (double d)
635 {
636         union
637         {
638                 uint8_t  byte[8];
639                 uint64_t integer;
640                 double   floating;
641         } ret;
642
643         if (isnan (d))
644         {
645                 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
646                 ret.byte[4] = ret.byte[5] = 0x00;
647                 ret.byte[6] = 0xf8;
648                 ret.byte[7] = 0x7f;
649                 return (ret.floating);
650         }
651         else
652         {
653                 uint64_t tmp;
654
655                 ret.floating = d;
656                 tmp = FP_CONVERT (ret.integer);
657                 ret.integer = tmp;
658                 return (ret.floating);
659         }
660 } /* double htond */
661 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
662
663 int format_name (char *ret, int ret_len,
664                 const char *hostname,
665                 const char *plugin, const char *plugin_instance,
666                 const char *type, const char *type_instance)
667 {
668         int  status;
669
670         assert (plugin != NULL);
671         assert (type != NULL);
672
673         if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
674         {
675                 if ((type_instance == NULL) || (strlen (type_instance) == 0))
676                         status = ssnprintf (ret, ret_len, "%s/%s/%s",
677                                         hostname, plugin, type);
678                 else
679                         status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
680                                         hostname, plugin, type,
681                                         type_instance);
682         }
683         else
684         {
685                 if ((type_instance == NULL) || (strlen (type_instance) == 0))
686                         status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
687                                         hostname, plugin, plugin_instance,
688                                         type);
689                 else
690                         status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
691                                         hostname, plugin, plugin_instance,
692                                         type, type_instance);
693         }
694
695         if ((status < 1) || (status >= ret_len))
696                 return (-1);
697         return (0);
698 } /* int format_name */
699
700 int parse_identifier (char *str, char **ret_host,
701                 char **ret_plugin, char **ret_plugin_instance,
702                 char **ret_type, char **ret_type_instance)
703 {
704         char *hostname = NULL;
705         char *plugin = NULL;
706         char *plugin_instance = NULL;
707         char *type = NULL;
708         char *type_instance = NULL;
709
710         hostname = str;
711         if (hostname == NULL)
712                 return (-1);
713
714         plugin = strchr (hostname, '/');
715         if (plugin == NULL)
716                 return (-1);
717         *plugin = '\0'; plugin++;
718
719         type = strchr (plugin, '/');
720         if (type == NULL)
721                 return (-1);
722         *type = '\0'; type++;
723
724         plugin_instance = strchr (plugin, '-');
725         if (plugin_instance != NULL)
726         {
727                 *plugin_instance = '\0';
728                 plugin_instance++;
729         }
730
731         type_instance = strchr (type, '-');
732         if (type_instance != NULL)
733         {
734                 *type_instance = '\0';
735                 type_instance++;
736         }
737
738         *ret_host = hostname;
739         *ret_plugin = plugin;
740         *ret_plugin_instance = plugin_instance;
741         *ret_type = type;
742         *ret_type_instance = type_instance;
743         return (0);
744 } /* int parse_identifier */
745
746 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
747 {
748         int i;
749         char *dummy;
750         char *ptr;
751         char *saveptr;
752
753         i = -1;
754         dummy = buffer;
755         saveptr = NULL;
756         while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
757         {
758                 dummy = NULL;
759
760                 if (i >= vl->values_len)
761                         break;
762
763                 if (i == -1)
764                 {
765                         if (strcmp ("N", ptr) == 0)
766                                 vl->time = time (NULL);
767                         else
768                                 vl->time = (time_t) atoi (ptr);
769                 }
770                 else
771                 {
772                         if (strcmp ("U", ptr) == 0)
773                                 vl->values[i].gauge = NAN;
774                         else if (ds->ds[i].type == DS_TYPE_COUNTER)
775                                 vl->values[i].counter = atoll (ptr);
776                         else if (ds->ds[i].type == DS_TYPE_GAUGE)
777                                 vl->values[i].gauge = atof (ptr);
778                 }
779
780                 i++;
781         } /* while (strtok_r) */
782
783         if ((ptr != NULL) || (i != vl->values_len))
784                 return (-1);
785         return (0);
786 } /* int parse_values */
787
788 #if !HAVE_GETPWNAM_R
789 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
790                 size_t buflen, struct passwd **pwbufp)
791 {
792         int status = 0;
793         struct passwd *pw;
794
795         memset (pwbuf, '\0', sizeof (struct passwd));
796
797         pthread_mutex_lock (&getpwnam_r_lock);
798
799         do
800         {
801                 pw = getpwnam (name);
802                 if (pw == NULL)
803                 {
804                         status = (errno != 0) ? errno : ENOENT;
805                         break;
806                 }
807
808 #define GETPWNAM_COPY_MEMBER(member) \
809                 if (pw->member != NULL) \
810                 { \
811                         int len = strlen (pw->member); \
812                         if (len >= buflen) \
813                         { \
814                                 status = ENOMEM; \
815                                 break; \
816                         } \
817                         sstrncpy (buf, pw->member, buflen); \
818                         pwbuf->member = buf; \
819                         buf    += (len + 1); \
820                         buflen -= (len + 1); \
821                 }
822                 GETPWNAM_COPY_MEMBER(pw_name);
823                 GETPWNAM_COPY_MEMBER(pw_passwd);
824                 GETPWNAM_COPY_MEMBER(pw_gecos);
825                 GETPWNAM_COPY_MEMBER(pw_dir);
826                 GETPWNAM_COPY_MEMBER(pw_shell);
827
828                 pwbuf->pw_uid = pw->pw_uid;
829                 pwbuf->pw_gid = pw->pw_gid;
830
831                 if (pwbufp != NULL)
832                         *pwbufp = pwbuf;
833         } while (0);
834
835         pthread_mutex_unlock (&getpwnam_r_lock);
836
837         return (status);
838 } /* int getpwnam_r */
839 #endif /* !HAVE_GETPWNAM_R */
840
841 int notification_init (notification_t *n, int severity, const char *message,
842                 const char *host,
843                 const char *plugin, const char *plugin_instance,
844                 const char *type, const char *type_instance)
845 {
846         memset (n, '\0', sizeof (notification_t));
847
848         n->severity = severity;
849
850         if (message != NULL)
851                 sstrncpy (n->message, message, sizeof (n->message));
852         if (host != NULL)
853                 sstrncpy (n->host, host, sizeof (n->host));
854         if (plugin != NULL)
855                 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
856         if (plugin_instance != NULL)
857                 sstrncpy (n->plugin_instance, plugin_instance,
858                                 sizeof (n->plugin_instance));
859         if (type != NULL)
860                 sstrncpy (n->type, type, sizeof (n->type));
861         if (type_instance != NULL)
862                 sstrncpy (n->type_instance, type_instance,
863                                 sizeof (n->type_instance));
864
865         return (0);
866 } /* int notification_init */
867
868 int walk_directory (const char *dir, dirwalk_callback_f callback,
869                 void *user_data)
870 {
871         struct dirent *ent;
872         DIR *dh;
873         int success;
874         int failure;
875
876         success = 0;
877         failure = 0;
878
879         if ((dh = opendir (dir)) == NULL)
880         {
881                 char errbuf[1024];
882                 ERROR ("walk_directory: Cannot open '%s': %s", dir,
883                                 sstrerror (errno, errbuf, sizeof (errbuf)));
884                 return -1;
885         }
886
887         while ((ent = readdir (dh)) != NULL)
888         {
889                 int status;
890
891                 if (ent->d_name[0] == '.')
892                         continue;
893
894                 status = (*callback) (dir, ent->d_name, user_data);
895                 if (status != 0)
896                         failure++;
897                 else
898                         success++;
899         }
900
901         closedir (dh);
902
903         if ((success == 0) && (failure > 0))
904                 return (-1);
905         return (0);
906 }
907
908 int read_file_contents (const char *filename, char *buf, int bufsize)
909 {
910         FILE *fh;
911         int n;
912
913         if ((fh = fopen (filename, "r")) == NULL)
914                 return -1;
915
916         n = fread(buf, 1, bufsize, fh);
917         fclose(fh);
918
919         return n;
920 }
921
922