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