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