75f3117e1e54136f2be78d2037df7c1d9316874a
[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 ("ds->ds_num = %i; vl->values_len = %i;",
154                         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         if (vc == NULL)
246                 return (cache_insert (ds, vl));
247
248         assert (vc->values_num == ds->ds_num);
249         assert (vc->values_num == vl->values_len);
250
251         /*
252          * Update the values. This is possibly a lot more that you'd expect
253          * because we honor min and max values and handle counter overflows here.
254          */
255         for (i = 0; i < ds->ds_num; i++)
256         {
257                 if (ds->ds[i].type == DS_TYPE_COUNTER)
258                 {
259                         if (vl->values[i].counter < vc->counter[i])
260                         {
261                                 if (vl->values[i].counter <= 4294967295U)
262                                 {
263                                         vc->gauge[i] = ((4294967295U - vl->values[i].counter)
264                                                         + vc->counter[i]) / (vl->time - vc->time);
265                                 }
266                                 else
267                                 {
268                                         vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
269                                                 + vc->counter[i]) / (vl->time - vc->time);
270                                 }
271                         }
272                         else
273                         {
274                                 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
275                                         / (vl->time - vc->time);
276                         }
277
278                         vc->counter[i] = vl->values[i].counter;
279                 }
280                 else if (ds->ds[i].type == DS_TYPE_GAUGE)
281                 {
282                         vc->gauge[i] = vl->values[i].gauge;
283                         vc->counter[i] = 0;
284                 }
285                 else
286                 {
287                         vc->gauge[i] = NAN;
288                         vc->counter[i] = 0;
289                 }
290
291                 if (isnan (vc->gauge[i])
292                                 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
293                                 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
294                         vc->gauge[i] = NAN;
295         } /* for i = 0 .. ds->ds_num */
296
297         vc->ds = ds;
298         vc->time = vl->time;
299
300         if (vc->time < cache_oldest)
301                 cache_oldest = vc->time;
302
303         pthread_mutex_unlock (&cache_lock);
304         return (0);
305 } /* int cache_update */
306
307 static void cache_flush (int max_age)
308 {
309         value_cache_t *this;
310         value_cache_t *prev;
311         time_t now;
312
313         pthread_mutex_lock (&cache_lock);
314
315         now = time (NULL);
316
317         if ((now - cache_oldest) <= max_age)
318         {
319                 pthread_mutex_unlock (&cache_lock);
320                 return;
321         }
322         
323         cache_oldest = now;
324
325         prev = NULL;
326         this = cache_head;
327
328         while (this != NULL)
329         {
330                 if ((now - this->time) <= max_age)
331                 {
332                         if (this->time < cache_oldest)
333                                 cache_oldest = this->time;
334
335                         prev = this;
336                         this = this->next;
337                         continue;
338                 }
339
340                 if (prev == NULL)
341                         cache_head = this->next;
342                 else
343                         prev->next = this->next;
344
345                 free (this->gauge);
346                 free (this->counter);
347                 free (this);
348
349                 if (prev == NULL)
350                         this = cache_head;
351                 else
352                         this = prev->next;
353         } /* while (this != NULL) */
354
355         pthread_mutex_unlock (&cache_lock);
356 } /* int cache_flush */
357
358 static int us_open_socket (void)
359 {
360         struct sockaddr_un sa;
361         int status;
362
363         sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
364         if (sock_fd < 0)
365         {
366                 char errbuf[1024];
367                 ERROR ("unixsock plugin: socket failed: %s",
368                                 sstrerror (errno, errbuf, sizeof (errbuf)));
369                 return (-1);
370         }
371
372         memset (&sa, '\0', sizeof (sa));
373         sa.sun_family = AF_UNIX;
374         strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
375                         sizeof (sa.sun_path) - 1);
376         /* unlink (sa.sun_path); */
377
378         status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
379         if (status != 0)
380         {
381                 char errbuf[1024];
382                 sstrerror (errno, errbuf, sizeof (errbuf));
383                 DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path);
384                 ERROR ("unixsock plugin: bind failed: %s", errbuf);
385                 close (sock_fd);
386                 sock_fd = -1;
387                 return (-1);
388         }
389
390         status = listen (sock_fd, 8);
391         if (status != 0)
392         {
393                 char errbuf[1024];
394                 ERROR ("unixsock plugin: listen failed: %s",
395                                 sstrerror (errno, errbuf, sizeof (errbuf)));
396                 close (sock_fd);
397                 sock_fd = -1;
398                 return (-1);
399         }
400
401         do
402         {
403                 char *grpname;
404                 struct group *g;
405                 struct group sg;
406                 char grbuf[2048];
407
408                 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
409                 g = NULL;
410
411                 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
412                 if (status != 0)
413                 {
414                         char errbuf[1024];
415                         WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
416                                         sstrerror (errno, errbuf, sizeof (errbuf)));
417                         break;
418                 }
419                 if (g == NULL)
420                 {
421                         WARNING ("unixsock plugin: No such group: `%s'",
422                                         grpname);
423                         break;
424                 }
425
426                 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
427                                         (uid_t) -1, g->gr_gid) != 0)
428                 {
429                         char errbuf[1024];
430                         WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
431                                         (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
432                                         (int) g->gr_gid,
433                                         sstrerror (errno, errbuf, sizeof (errbuf)));
434                 }
435         } while (0);
436
437         return (0);
438 } /* int us_open_socket */
439
440 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
441 {
442         char *hostname;
443         char *plugin;
444         char *plugin_instance;
445         char *type;
446         char *type_instance;
447         char  name[4*DATA_MAX_NAME_LEN];
448         value_cache_t *vc;
449         int   status;
450         int   i;
451
452         if (fields_num != 2)
453         {
454                 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
455                 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
456                                 fields_num);
457                 fflush (fh);
458                 return (-1);
459         }
460         DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
461
462         status = parse_identifier (fields[1], &hostname,
463                         &plugin, &plugin_instance,
464                         &type, &type_instance);
465         if (status != 0)
466         {
467                 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
468                 fprintf (fh, "-1 Cannot parse identifier.\n");
469                 fflush (fh);
470                 return (-1);
471         }
472
473         status = format_name (name, sizeof (name),
474                         hostname, plugin, plugin_instance, type, type_instance);
475         /* FIXME: Send some response */
476         if (status != 0)
477                 return (-1);
478
479         pthread_mutex_lock (&cache_lock);
480
481         DEBUG ("vc = cache_search (%s)", name);
482         vc = cache_search (name);
483
484         if (vc == NULL)
485         {
486                 DEBUG ("Did not find cache entry.");
487                 fprintf (fh, "-1 No such value");
488         }
489         else
490         {
491                 DEBUG ("Found cache entry.");
492                 fprintf (fh, "%i", vc->values_num);
493                 for (i = 0; i < vc->values_num; i++)
494                 {
495                         fprintf (fh, " %s=", vc->ds->ds[i].name);
496                         if (isnan (vc->gauge[i]))
497                                 fprintf (fh, "NaN");
498                         else
499                                 fprintf (fh, "%12e", vc->gauge[i]);
500                 }
501         }
502
503         /* Free the mutex as soon as possible and definitely before flushing */
504         pthread_mutex_unlock (&cache_lock);
505
506         fprintf (fh, "\n");
507         fflush (fh);
508
509         return (0);
510 } /* int us_handle_getval */
511
512 static int us_handle_putval (FILE *fh, char **fields, int fields_num)
513 {
514         char *hostname;
515         char *plugin;
516         char *plugin_instance;
517         char *type;
518         char *type_instance;
519         int   status;
520         int   i;
521
522         const data_set_t *ds;
523         value_list_t vl = VALUE_LIST_INIT;
524
525         char **value_ptr;
526
527         if (fields_num != 3)
528         {
529                 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
530                 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
531                                 fields_num);
532                 fflush (fh);
533                 return (-1);
534         }
535
536         status = parse_identifier (fields[1], &hostname,
537                         &plugin, &plugin_instance,
538                         &type, &type_instance);
539         if (status != 0)
540         {
541                 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
542                 fprintf (fh, "-1 Cannot parse identifier.\n");
543                 fflush (fh);
544                 return (-1);
545         }
546
547         /* FIXME: Send some response */
548         if ((strlen (hostname) > sizeof (vl.host))
549                         || (strlen (plugin) > sizeof (vl.plugin))
550                         || ((plugin_instance != NULL)
551                                 && (strlen (plugin_instance) > sizeof (vl.plugin_instance)))
552                         || ((type_instance != NULL)
553                                 && (strlen (type_instance) > sizeof (vl.type_instance))))
554                 return (-1);
555
556         strcpy (vl.host, hostname);
557         strcpy (vl.plugin, plugin);
558         if (plugin_instance != NULL)
559                 strcpy (vl.plugin_instance, plugin_instance);
560         if (type_instance != NULL)
561                 strcpy (vl.type_instance, type_instance);
562
563         { /* parse the time */
564                 char *t = fields[2];
565                 char *v = strchr (t, ':');
566                 if (v == NULL)
567                         return (-1);
568                 *v = '\0'; v++;
569
570                 vl.time = (time_t) atoi (t);
571                 if (vl.time == 0)
572                         vl.time = time (NULL);
573
574                 fields[2] = v;
575         }
576
577         ds = plugin_get_ds (type);
578         if (ds == NULL)
579                 return (-1);
580
581         value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
582         /* FIXME: Send some response */
583         if (value_ptr == NULL)
584                 return (-1);
585
586
587         { /* parse the value-list. It's colon-separated. */
588                 char *dummy;
589                 char *ptr;
590                 char *saveptr;
591
592                 i = 0;
593                 dummy = fields[2];
594                 saveptr = NULL;
595                 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
596                 {
597                         dummy = NULL;
598                         if (i >= ds->ds_num)
599                         {
600                                 i = ds->ds_num + 1;
601                                 break;
602                         }
603                         value_ptr[i] = ptr;
604                         i++;
605                 }
606
607                 if (i != ds->ds_num)
608                 {
609                         sfree (value_ptr);
610                         /* FIXME: Send some response */
611                         return (-1);
612                 }
613         } /* done parsing the value-list */
614
615         vl.values_len = ds->ds_num;
616         vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
617         if (vl.values == NULL)
618         {
619                 sfree (value_ptr);
620                 return (-1);
621         }
622         DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
623
624         for (i = 0; i < ds->ds_num; i++)
625         {
626                 if (strcmp (value_ptr[i], "U") == 0)
627                         vl.values[i].gauge = NAN;
628                 else if (ds->ds[i].type == DS_TYPE_COUNTER)
629                         vl.values[i].counter = atoll (value_ptr[i]);
630                 else if (ds->ds[i].type == DS_TYPE_GAUGE)
631                         vl.values[i].gauge = atof (value_ptr[i]);
632         } /* for (i = 2 .. fields_num) */
633
634         plugin_dispatch_values (type, &vl);
635
636         DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
637
638         sfree (value_ptr);
639         sfree (vl.values); 
640
641         fprintf (fh, "0 Success\n");
642         fflush (fh);
643
644         return (0);
645 } /* int us_handle_putval */
646
647 static void *us_handle_client (void *arg)
648 {
649         int fd;
650         FILE *fh;
651         char buffer[1024];
652         char *fields[128];
653         int   fields_num;
654
655         fd = *((int *) arg);
656         free (arg);
657         arg = NULL;
658
659         DEBUG ("Reading from fd #%i", fd);
660
661         fh = fdopen (fd, "r+");
662         if (fh == NULL)
663         {
664                 char errbuf[1024];
665                 ERROR ("unixsock plugin: fdopen failed: %s",
666                                 sstrerror (errno, errbuf, sizeof (errbuf)));
667                 close (fd);
668                 pthread_exit ((void *) 1);
669         }
670
671         while (fgets (buffer, sizeof (buffer), fh) != NULL)
672         {
673                 int len;
674
675                 len = strlen (buffer);
676                 while ((len > 0)
677                                 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
678                         buffer[--len] = '\0';
679
680                 if (len == 0)
681                         continue;
682
683                 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
684
685                 fields_num = strsplit (buffer, fields,
686                                 sizeof (fields) / sizeof (fields[0]));
687
688                 if (fields_num < 1)
689                 {
690                         close (fd);
691                         break;
692                 }
693
694                 if (strcasecmp (fields[0], "getval") == 0)
695                 {
696                         us_handle_getval (fh, fields, fields_num);
697                 }
698                 else if (strcasecmp (fields[0], "putval") == 0)
699                 {
700                         us_handle_putval (fh, fields, fields_num);
701                 }
702                 else
703                 {
704                         fprintf (fh, "Unknown command: %s\n", fields[0]);
705                         fflush (fh);
706                 }
707         } /* while (fgets) */
708
709         DEBUG ("Exiting..");
710         close (fd);
711
712         pthread_exit ((void *) 0);
713 } /* void *us_handle_client */
714
715 static void *us_server_thread (void *arg)
716 {
717         int  status;
718         int *remote_fd;
719         pthread_t th;
720         pthread_attr_t th_attr;
721
722         if (us_open_socket () != 0)
723                 pthread_exit ((void *) 1);
724
725         while (loop != 0)
726         {
727                 DEBUG ("Calling accept..");
728                 status = accept (sock_fd, NULL, NULL);
729                 if (status < 0)
730                 {
731                         char errbuf[1024];
732
733                         if (errno == EINTR)
734                                 continue;
735
736                         ERROR ("unixsock plugin: accept failed: %s",
737                                         sstrerror (errno, errbuf, sizeof (errbuf)));
738                         close (sock_fd);
739                         sock_fd = -1;
740                         pthread_exit ((void *) 1);
741                 }
742
743                 remote_fd = (int *) malloc (sizeof (int));
744                 if (remote_fd == NULL)
745                 {
746                         char errbuf[1024];
747                         WARNING ("unixsock plugin: malloc failed: %s",
748                                         sstrerror (errno, errbuf, sizeof (errbuf)));
749                         close (status);
750                         continue;
751                 }
752                 *remote_fd = status;
753
754                 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
755
756                 pthread_attr_init (&th_attr);
757                 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
758
759                 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
760                 if (status != 0)
761                 {
762                         char errbuf[1024];
763                         WARNING ("unixsock plugin: pthread_create failed: %s",
764                                         sstrerror (errno, errbuf, sizeof (errbuf)));
765                         close (*remote_fd);
766                         free (remote_fd);
767                         continue;
768                 }
769         } /* while (loop) */
770
771         close (sock_fd);
772         sock_fd = -1;
773
774         return ((void *) 0);
775 } /* void *us_server_thread */
776
777 static int us_config (const char *key, const char *val)
778 {
779         if (strcasecmp (key, "SocketFile") == 0)
780         {
781                 sfree (sock_file);
782                 sock_file = strdup (val);
783         }
784         else if (strcasecmp (key, "SocketGroup") == 0)
785         {
786                 sfree (sock_group);
787                 sock_group = strdup (val);
788         }
789         else if (strcasecmp (key, "SocketPerms") == 0)
790         {
791                 sock_perms = (int) strtol (val, NULL, 8);
792         }
793         else
794         {
795                 return (-1);
796         }
797
798         return (0);
799 } /* int us_config */
800
801 static int us_init (void)
802 {
803         int status;
804
805         loop = 1;
806
807         status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
808         if (status != 0)
809         {
810                 char errbuf[1024];
811                 ERROR ("unixsock plugin: pthread_create failed: %s",
812                                 sstrerror (errno, errbuf, sizeof (errbuf)));
813                 return (-1);
814         }
815
816         return (0);
817 } /* int us_init */
818
819 static int us_shutdown (void)
820 {
821         void *ret;
822
823         loop = 0;
824
825         if (listen_thread != (pthread_t) 0)
826         {
827                 pthread_kill (listen_thread, SIGTERM);
828                 pthread_join (listen_thread, &ret);
829                 listen_thread = (pthread_t) 0;
830         }
831
832         plugin_unregister_init ("unixsock");
833         plugin_unregister_write ("unixsock");
834         plugin_unregister_shutdown ("unixsock");
835
836         return (0);
837 } /* int us_shutdown */
838
839 static int us_write (const data_set_t *ds, const value_list_t *vl)
840 {
841         cache_update (ds, vl);
842         cache_flush (2 * interval_g);
843
844         return (0);
845 }
846
847 void module_register (void)
848 {
849         plugin_register_config ("unixsock", us_config,
850                         config_keys, config_keys_num);
851         plugin_register_init ("unixsock", us_init);
852         plugin_register_write ("unixsock", us_write);
853         plugin_register_shutdown ("unixsock", us_shutdown);
854 } /* void module_register (void) */
855
856 /* vim: set sw=4 ts=4 sts=4 tw=78 : */