8211bf46e9f7a686395183444b06da7fd90e1977
[collectd.git] / src / unixsock.c
1 /**
2  * collectd - src/unixsock.c
3  * Copyright (C) 2007  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  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26
27 /* Folks without pthread will need to disable this plugin. */
28 #include <pthread.h>
29
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
33 #include <grp.h>
34
35 #ifndef UNIX_PATH_MAX
36 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
37 #endif
38
39 #define US_DEFAULT_PATH PREFIX"/var/run/"PACKAGE_NAME"-unixsock"
40
41 /*
42  * Private data structures
43  */
44 /* linked list of cached values */
45 typedef struct value_cache_s
46 {
47         char       name[4*DATA_MAX_NAME_LEN];
48         int        values_num;
49         gauge_t   *gauge;
50         counter_t *counter;
51         const data_set_t *ds;
52         time_t     time;
53         struct value_cache_s *next;
54 } value_cache_t;
55
56 /*
57  * Private variables
58  */
59 /* valid configuration file keys */
60 static const char *config_keys[] =
61 {
62         "SocketFile",
63         "SocketGroup",
64         "SocketPerms",
65         NULL
66 };
67 static int config_keys_num = 3;
68
69 static int loop = 0;
70
71 /* socket configuration */
72 static int   sock_fd    = -1;
73 static char *sock_file  = NULL;
74 static char *sock_group = NULL;
75 static int   sock_perms = S_IRWXU | S_IRWXG;
76
77 static pthread_t listen_thread = (pthread_t) 0;
78
79 /* Linked list and auxilliary variables for saving values */
80 static value_cache_t   *cache_head = NULL;
81 static pthread_mutex_t  cache_lock = PTHREAD_MUTEX_INITIALIZER;
82 static unsigned int     cache_oldest = UINT_MAX;
83
84 /*
85  * Functions
86  */
87 static int parse_identifier (char *str, char **ret_host,
88                 char **ret_plugin, char **ret_plugin_instance,
89                 char **ret_type, char **ret_type_instance)
90 {
91         char *hostname = NULL;
92         char *plugin = NULL;
93         char *plugin_instance = NULL;
94         char *type = NULL;
95         char *type_instance = NULL;
96
97         hostname = str;
98         if (hostname == NULL)
99                 return (-1);
100
101         plugin = strchr (hostname, '/');
102         if (plugin == NULL)
103                 return (-1);
104         *plugin = '\0'; plugin++;
105
106         type = strchr (plugin, '/');
107         if (type == NULL)
108                 return (-1);
109         *type = '\0'; type++;
110
111         plugin_instance = strchr (plugin, '-');
112         if (plugin_instance != NULL)
113         {
114                 *plugin_instance = '\0';
115                 plugin_instance++;
116         }
117
118         type_instance = strchr (type, '-');
119         if (type_instance != NULL)
120         {
121                 *type_instance = '\0';
122                 type_instance++;
123         }
124
125         *ret_host = hostname;
126         *ret_plugin = plugin;
127         *ret_plugin_instance = plugin_instance;
128         *ret_type = type;
129         *ret_type_instance = type_instance;
130         return (0);
131 } /* int parse_identifier */
132
133 static value_cache_t *cache_search (const char *name)
134 {
135         value_cache_t *vc;
136
137         for (vc = cache_head; vc != NULL; vc = vc->next)
138         {
139                 if (strcmp (vc->name, name) == 0)
140                         break;
141         } /* for vc = cache_head .. NULL */
142
143         return (vc);
144 } /* value_cache_t *cache_search */
145
146 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
147 {
148         /* We're called from `cache_update' so we don't need to lock the mutex */
149         value_cache_t *vc;
150         int i;
151
152         DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
153                         " vl->values_len = %i;",
154                         ds->type, ds->ds_num, vl->values_len);
155         assert (ds->ds_num == vl->values_len);
156
157         vc = (value_cache_t *) malloc (sizeof (value_cache_t));
158         if (vc == NULL)
159         {
160                 char errbuf[1024];
161                 pthread_mutex_unlock (&cache_lock);
162                 ERROR ("unixsock plugin: malloc failed: %s",
163                                 sstrerror (errno, errbuf, sizeof (errbuf)));
164                 return (-1);
165         }
166
167         vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
168         if (vc->gauge == NULL)
169         {
170                 char errbuf[1024];
171                 pthread_mutex_unlock (&cache_lock);
172                 ERROR ("unixsock plugin: malloc failed: %s",
173                                 sstrerror (errno, errbuf, sizeof (errbuf)));
174                 free (vc);
175                 return (-1);
176         }
177
178         vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
179         if (vc->counter == NULL)
180         {
181                 char errbuf[1024];
182                 pthread_mutex_unlock (&cache_lock);
183                 ERROR ("unixsock plugin: malloc failed: %s",
184                                 sstrerror (errno, errbuf, sizeof (errbuf)));
185                 free (vc->gauge);
186                 free (vc);
187                 return (-1);
188         }
189
190         if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
191         {
192                 pthread_mutex_unlock (&cache_lock);
193                 ERROR ("unixsock plugin: FORMAT_VL failed.");
194                 free (vc->counter);
195                 free (vc->gauge);
196                 free (vc);
197                 return (-1);
198         }
199
200         for (i = 0; i < ds->ds_num; i++)
201         {
202                 if (ds->ds[i].type == DS_TYPE_COUNTER)
203                 {
204                         vc->gauge[i] = 0.0;
205                         vc->counter[i] = vl->values[i].counter;
206                 }
207                 else if (ds->ds[i].type == DS_TYPE_GAUGE)
208                 {
209                         vc->gauge[i] = vl->values[i].gauge;
210                         vc->counter[i] = 0;
211                 }
212                 else
213                 {
214                         vc->gauge[i] = 0.0;
215                         vc->counter[i] = 0;
216                 }
217         }
218         vc->values_num = ds->ds_num;
219         vc->ds = ds;
220
221         vc->next = cache_head;
222         cache_head = vc;
223
224         vc->time = vl->time;
225         if (vc->time < cache_oldest)
226                 cache_oldest = vc->time;
227
228         pthread_mutex_unlock (&cache_lock);
229         return (0);
230 } /* int cache_insert */
231
232 static int cache_update (const data_set_t *ds, const value_list_t *vl)
233 {
234         char name[4*DATA_MAX_NAME_LEN];;
235         value_cache_t *vc;
236         int i;
237
238         if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
239                 return (-1);
240
241         pthread_mutex_lock (&cache_lock);
242
243         vc = cache_search (name);
244
245         /* pthread_mutex_lock is called by cache_insert. */
246         if (vc == NULL)
247                 return (cache_insert (ds, vl));
248
249         assert (vc->values_num == ds->ds_num);
250         assert (vc->values_num == vl->values_len);
251
252         /* Avoid floating-point exceptions due to division by zero. */
253         if (vc->time >= vl->time)
254         {
255                 pthread_mutex_unlock (&cache_lock);
256                 ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
257                                 "vl->time = %u; vl = %s;",
258                                 (unsigned int) vc->time, (unsigned int) vl->time,
259                                 name);
260                 return (-1);
261         } /* if (vc->time >= vl->time) */
262
263         /*
264          * Update the values. This is possibly a lot more that you'd expect
265          * because we honor min and max values and handle counter overflows here.
266          */
267         for (i = 0; i < ds->ds_num; i++)
268         {
269                 if (ds->ds[i].type == DS_TYPE_COUNTER)
270                 {
271                         if (vl->values[i].counter < vc->counter[i])
272                         {
273                                 if (vl->values[i].counter <= 4294967295U)
274                                 {
275                                         vc->gauge[i] = ((4294967295U - vl->values[i].counter)
276                                                         + vc->counter[i]) / (vl->time - vc->time);
277                                 }
278                                 else
279                                 {
280                                         vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
281                                                 + vc->counter[i]) / (vl->time - vc->time);
282                                 }
283                         }
284                         else
285                         {
286                                 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
287                                         / (vl->time - vc->time);
288                         }
289
290                         vc->counter[i] = vl->values[i].counter;
291                 }
292                 else if (ds->ds[i].type == DS_TYPE_GAUGE)
293                 {
294                         vc->gauge[i] = vl->values[i].gauge;
295                         vc->counter[i] = 0;
296                 }
297                 else
298                 {
299                         vc->gauge[i] = NAN;
300                         vc->counter[i] = 0;
301                 }
302
303                 if (isnan (vc->gauge[i])
304                                 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
305                                 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
306                         vc->gauge[i] = NAN;
307         } /* for i = 0 .. ds->ds_num */
308
309         vc->ds = ds;
310         vc->time = vl->time;
311
312         if (vc->time < cache_oldest)
313                 cache_oldest = vc->time;
314
315         pthread_mutex_unlock (&cache_lock);
316         return (0);
317 } /* int cache_update */
318
319 static void cache_flush (int max_age)
320 {
321         value_cache_t *this;
322         value_cache_t *prev;
323         time_t now;
324
325         pthread_mutex_lock (&cache_lock);
326
327         now = time (NULL);
328
329         if ((now - cache_oldest) <= max_age)
330         {
331                 pthread_mutex_unlock (&cache_lock);
332                 return;
333         }
334         
335         cache_oldest = now;
336
337         prev = NULL;
338         this = cache_head;
339
340         while (this != NULL)
341         {
342                 if ((now - this->time) <= max_age)
343                 {
344                         if (this->time < cache_oldest)
345                                 cache_oldest = this->time;
346
347                         prev = this;
348                         this = this->next;
349                         continue;
350                 }
351
352                 if (prev == NULL)
353                         cache_head = this->next;
354                 else
355                         prev->next = this->next;
356
357                 free (this->gauge);
358                 free (this->counter);
359                 free (this);
360
361                 if (prev == NULL)
362                         this = cache_head;
363                 else
364                         this = prev->next;
365         } /* while (this != NULL) */
366
367         pthread_mutex_unlock (&cache_lock);
368 } /* int cache_flush */
369
370 static int us_open_socket (void)
371 {
372         struct sockaddr_un sa;
373         int status;
374
375         sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
376         if (sock_fd < 0)
377         {
378                 char errbuf[1024];
379                 ERROR ("unixsock plugin: socket failed: %s",
380                                 sstrerror (errno, errbuf, sizeof (errbuf)));
381                 return (-1);
382         }
383
384         memset (&sa, '\0', sizeof (sa));
385         sa.sun_family = AF_UNIX;
386         strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
387                         sizeof (sa.sun_path) - 1);
388         /* unlink (sa.sun_path); */
389
390         status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
391         if (status != 0)
392         {
393                 char errbuf[1024];
394                 sstrerror (errno, errbuf, sizeof (errbuf));
395                 DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path);
396                 ERROR ("unixsock plugin: bind failed: %s", errbuf);
397                 close (sock_fd);
398                 sock_fd = -1;
399                 return (-1);
400         }
401
402         status = listen (sock_fd, 8);
403         if (status != 0)
404         {
405                 char errbuf[1024];
406                 ERROR ("unixsock plugin: listen failed: %s",
407                                 sstrerror (errno, errbuf, sizeof (errbuf)));
408                 close (sock_fd);
409                 sock_fd = -1;
410                 return (-1);
411         }
412
413         do
414         {
415                 char *grpname;
416                 struct group *g;
417                 struct group sg;
418                 char grbuf[2048];
419
420                 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
421                 g = NULL;
422
423                 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
424                 if (status != 0)
425                 {
426                         char errbuf[1024];
427                         WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
428                                         sstrerror (errno, errbuf, sizeof (errbuf)));
429                         break;
430                 }
431                 if (g == NULL)
432                 {
433                         WARNING ("unixsock plugin: No such group: `%s'",
434                                         grpname);
435                         break;
436                 }
437
438                 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
439                                         (uid_t) -1, g->gr_gid) != 0)
440                 {
441                         char errbuf[1024];
442                         WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
443                                         (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
444                                         (int) g->gr_gid,
445                                         sstrerror (errno, errbuf, sizeof (errbuf)));
446                 }
447         } while (0);
448
449         return (0);
450 } /* int us_open_socket */
451
452 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
453 {
454         char *hostname;
455         char *plugin;
456         char *plugin_instance;
457         char *type;
458         char *type_instance;
459         char  name[4*DATA_MAX_NAME_LEN];
460         value_cache_t *vc;
461         int   status;
462         int   i;
463
464         if (fields_num != 2)
465         {
466                 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
467                 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
468                                 fields_num);
469                 fflush (fh);
470                 return (-1);
471         }
472         DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
473
474         status = parse_identifier (fields[1], &hostname,
475                         &plugin, &plugin_instance,
476                         &type, &type_instance);
477         if (status != 0)
478         {
479                 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
480                 fprintf (fh, "-1 Cannot parse identifier.\n");
481                 fflush (fh);
482                 return (-1);
483         }
484
485         status = format_name (name, sizeof (name),
486                         hostname, plugin, plugin_instance, type, type_instance);
487         /* FIXME: Send some response */
488         if (status != 0)
489                 return (-1);
490
491         pthread_mutex_lock (&cache_lock);
492
493         DEBUG ("vc = cache_search (%s)", name);
494         vc = cache_search (name);
495
496         if (vc == NULL)
497         {
498                 DEBUG ("Did not find cache entry.");
499                 fprintf (fh, "-1 No such value");
500         }
501         else
502         {
503                 DEBUG ("Found cache entry.");
504                 fprintf (fh, "%i", vc->values_num);
505                 for (i = 0; i < vc->values_num; i++)
506                 {
507                         fprintf (fh, " %s=", vc->ds->ds[i].name);
508                         if (isnan (vc->gauge[i]))
509                                 fprintf (fh, "NaN");
510                         else
511                                 fprintf (fh, "%12e", vc->gauge[i]);
512                 }
513         }
514
515         /* Free the mutex as soon as possible and definitely before flushing */
516         pthread_mutex_unlock (&cache_lock);
517
518         fprintf (fh, "\n");
519         fflush (fh);
520
521         return (0);
522 } /* int us_handle_getval */
523
524 static int us_handle_putval (FILE *fh, char **fields, int fields_num)
525 {
526         char *hostname;
527         char *plugin;
528         char *plugin_instance;
529         char *type;
530         char *type_instance;
531         int   status;
532         int   i;
533
534         const data_set_t *ds;
535         value_list_t vl = VALUE_LIST_INIT;
536
537         char **value_ptr;
538
539         if (fields_num != 3)
540         {
541                 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
542                 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
543                                 fields_num);
544                 fflush (fh);
545                 return (-1);
546         }
547
548         status = parse_identifier (fields[1], &hostname,
549                         &plugin, &plugin_instance,
550                         &type, &type_instance);
551         if (status != 0)
552         {
553                 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
554                 fprintf (fh, "-1 Cannot parse identifier.\n");
555                 fflush (fh);
556                 return (-1);
557         }
558
559         /* FIXME: Send some response */
560         if ((strlen (hostname) > sizeof (vl.host))
561                         || (strlen (plugin) > sizeof (vl.plugin))
562                         || ((plugin_instance != NULL)
563                                 && (strlen (plugin_instance) > sizeof (vl.plugin_instance)))
564                         || ((type_instance != NULL)
565                                 && (strlen (type_instance) > sizeof (vl.type_instance))))
566                 return (-1);
567
568         strcpy (vl.host, hostname);
569         strcpy (vl.plugin, plugin);
570         if (plugin_instance != NULL)
571                 strcpy (vl.plugin_instance, plugin_instance);
572         if (type_instance != NULL)
573                 strcpy (vl.type_instance, type_instance);
574
575         { /* parse the time */
576                 char *t = fields[2];
577                 char *v = strchr (t, ':');
578                 if (v == NULL)
579                         return (-1);
580                 *v = '\0'; v++;
581
582                 vl.time = (time_t) atoi (t);
583                 if (vl.time == 0)
584                         vl.time = time (NULL);
585
586                 fields[2] = v;
587         }
588
589         ds = plugin_get_ds (type);
590         if (ds == NULL)
591                 return (-1);
592
593         value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
594         /* FIXME: Send some response */
595         if (value_ptr == NULL)
596                 return (-1);
597
598
599         { /* parse the value-list. It's colon-separated. */
600                 char *dummy;
601                 char *ptr;
602                 char *saveptr;
603
604                 i = 0;
605                 dummy = fields[2];
606                 saveptr = NULL;
607                 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
608                 {
609                         dummy = NULL;
610                         if (i >= ds->ds_num)
611                         {
612                                 i = ds->ds_num + 1;
613                                 break;
614                         }
615                         value_ptr[i] = ptr;
616                         i++;
617                 }
618
619                 if (i != ds->ds_num)
620                 {
621                         sfree (value_ptr);
622                         /* FIXME: Send some response */
623                         return (-1);
624                 }
625         } /* done parsing the value-list */
626
627         vl.values_len = ds->ds_num;
628         vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
629         if (vl.values == NULL)
630         {
631                 sfree (value_ptr);
632                 return (-1);
633         }
634         DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
635
636         for (i = 0; i < ds->ds_num; i++)
637         {
638                 if (strcmp (value_ptr[i], "U") == 0)
639                         vl.values[i].gauge = NAN;
640                 else if (ds->ds[i].type == DS_TYPE_COUNTER)
641                         vl.values[i].counter = atoll (value_ptr[i]);
642                 else if (ds->ds[i].type == DS_TYPE_GAUGE)
643                         vl.values[i].gauge = atof (value_ptr[i]);
644         } /* for (i = 2 .. fields_num) */
645
646         plugin_dispatch_values (type, &vl);
647
648         DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
649
650         sfree (value_ptr);
651         sfree (vl.values); 
652
653         fprintf (fh, "0 Success\n");
654         fflush (fh);
655
656         return (0);
657 } /* int us_handle_putval */
658
659 static void *us_handle_client (void *arg)
660 {
661         int fd;
662         FILE *fh;
663         char buffer[1024];
664         char *fields[128];
665         int   fields_num;
666
667         fd = *((int *) arg);
668         free (arg);
669         arg = NULL;
670
671         DEBUG ("Reading from fd #%i", fd);
672
673         fh = fdopen (fd, "r+");
674         if (fh == NULL)
675         {
676                 char errbuf[1024];
677                 ERROR ("unixsock plugin: fdopen failed: %s",
678                                 sstrerror (errno, errbuf, sizeof (errbuf)));
679                 close (fd);
680                 pthread_exit ((void *) 1);
681         }
682
683         while (fgets (buffer, sizeof (buffer), fh) != NULL)
684         {
685                 int len;
686
687                 len = strlen (buffer);
688                 while ((len > 0)
689                                 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
690                         buffer[--len] = '\0';
691
692                 if (len == 0)
693                         continue;
694
695                 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
696
697                 fields_num = strsplit (buffer, fields,
698                                 sizeof (fields) / sizeof (fields[0]));
699
700                 if (fields_num < 1)
701                 {
702                         close (fd);
703                         break;
704                 }
705
706                 if (strcasecmp (fields[0], "getval") == 0)
707                 {
708                         us_handle_getval (fh, fields, fields_num);
709                 }
710                 else if (strcasecmp (fields[0], "putval") == 0)
711                 {
712                         us_handle_putval (fh, fields, fields_num);
713                 }
714                 else
715                 {
716                         fprintf (fh, "Unknown command: %s\n", fields[0]);
717                         fflush (fh);
718                 }
719         } /* while (fgets) */
720
721         DEBUG ("Exiting..");
722         close (fd);
723
724         pthread_exit ((void *) 0);
725 } /* void *us_handle_client */
726
727 static void *us_server_thread (void *arg)
728 {
729         int  status;
730         int *remote_fd;
731         pthread_t th;
732         pthread_attr_t th_attr;
733
734         if (us_open_socket () != 0)
735                 pthread_exit ((void *) 1);
736
737         while (loop != 0)
738         {
739                 DEBUG ("Calling accept..");
740                 status = accept (sock_fd, NULL, NULL);
741                 if (status < 0)
742                 {
743                         char errbuf[1024];
744
745                         if (errno == EINTR)
746                                 continue;
747
748                         ERROR ("unixsock plugin: accept failed: %s",
749                                         sstrerror (errno, errbuf, sizeof (errbuf)));
750                         close (sock_fd);
751                         sock_fd = -1;
752                         pthread_exit ((void *) 1);
753                 }
754
755                 remote_fd = (int *) malloc (sizeof (int));
756                 if (remote_fd == NULL)
757                 {
758                         char errbuf[1024];
759                         WARNING ("unixsock plugin: malloc failed: %s",
760                                         sstrerror (errno, errbuf, sizeof (errbuf)));
761                         close (status);
762                         continue;
763                 }
764                 *remote_fd = status;
765
766                 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
767
768                 pthread_attr_init (&th_attr);
769                 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
770
771                 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
772                 if (status != 0)
773                 {
774                         char errbuf[1024];
775                         WARNING ("unixsock plugin: pthread_create failed: %s",
776                                         sstrerror (errno, errbuf, sizeof (errbuf)));
777                         close (*remote_fd);
778                         free (remote_fd);
779                         continue;
780                 }
781         } /* while (loop) */
782
783         close (sock_fd);
784         sock_fd = -1;
785
786         status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
787         if (status != 0)
788         {
789                 char errbuf[1024];
790                 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
791                                 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
792                                 sstrerror (errno, errbuf, sizeof (errbuf)));
793         }
794
795         return ((void *) 0);
796 } /* void *us_server_thread */
797
798 static int us_config (const char *key, const char *val)
799 {
800         if (strcasecmp (key, "SocketFile") == 0)
801         {
802                 sfree (sock_file);
803                 sock_file = strdup (val);
804         }
805         else if (strcasecmp (key, "SocketGroup") == 0)
806         {
807                 sfree (sock_group);
808                 sock_group = strdup (val);
809         }
810         else if (strcasecmp (key, "SocketPerms") == 0)
811         {
812                 sock_perms = (int) strtol (val, NULL, 8);
813         }
814         else
815         {
816                 return (-1);
817         }
818
819         return (0);
820 } /* int us_config */
821
822 static int us_init (void)
823 {
824         int status;
825
826         loop = 1;
827
828         status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
829         if (status != 0)
830         {
831                 char errbuf[1024];
832                 ERROR ("unixsock plugin: pthread_create failed: %s",
833                                 sstrerror (errno, errbuf, sizeof (errbuf)));
834                 return (-1);
835         }
836
837         return (0);
838 } /* int us_init */
839
840 static int us_shutdown (void)
841 {
842         void *ret;
843
844         loop = 0;
845
846         if (listen_thread != (pthread_t) 0)
847         {
848                 pthread_kill (listen_thread, SIGTERM);
849                 pthread_join (listen_thread, &ret);
850                 listen_thread = (pthread_t) 0;
851         }
852
853         plugin_unregister_init ("unixsock");
854         plugin_unregister_write ("unixsock");
855         plugin_unregister_shutdown ("unixsock");
856
857         return (0);
858 } /* int us_shutdown */
859
860 static int us_write (const data_set_t *ds, const value_list_t *vl)
861 {
862         cache_update (ds, vl);
863         cache_flush (2 * interval_g);
864
865         return (0);
866 }
867
868 void module_register (void)
869 {
870         plugin_register_config ("unixsock", us_config,
871                         config_keys, config_keys_num);
872         plugin_register_init ("unixsock", us_init);
873         plugin_register_write ("unixsock", us_write);
874         plugin_register_shutdown ("unixsock", us_shutdown);
875 } /* void module_register (void) */
876
877 /* vim: set sw=4 ts=4 sts=4 tw=78 : */