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