7e7a1b1a857eb73c4529dd038d011f3b19cf1ed7
[collectd.git] / src / unixsock.c
1 /**
2  * collectd - src/unixsock.c
3  * Copyright (C) 2007,2008  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 #include "utils_cmd_flush.h"
28 #include "utils_cmd_getval.h"
29 #include "utils_cmd_getthreshold.h"
30 #include "utils_cmd_listval.h"
31 #include "utils_cmd_putval.h"
32 #include "utils_cmd_putnotif.h"
33
34 /* Folks without pthread will need to disable this plugin. */
35 #include <pthread.h>
36
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/un.h>
40
41 #include <grp.h>
42
43 #ifndef UNIX_PATH_MAX
44 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
45 #endif
46
47 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
48
49 /*
50  * Private variables
51  */
52 /* valid configuration file keys */
53 static const char *config_keys[] =
54 {
55         "SocketFile",
56         "SocketGroup",
57         "SocketPerms",
58         "DeleteSocket"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61
62 static int loop = 0;
63
64 /* socket configuration */
65 static int   sock_fd    = -1;
66 static char *sock_file  = NULL;
67 static char *sock_group = NULL;
68 static int   sock_perms = S_IRWXU | S_IRWXG;
69 static _Bool delete_socket = 0;
70
71 static pthread_t listen_thread = (pthread_t) 0;
72
73 /*
74  * Functions
75  */
76 static int us_open_socket (void)
77 {
78         struct sockaddr_un sa;
79         int status;
80
81         sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
82         if (sock_fd < 0)
83         {
84                 char errbuf[1024];
85                 ERROR ("unixsock plugin: socket failed: %s",
86                                 sstrerror (errno, errbuf, sizeof (errbuf)));
87                 return (-1);
88         }
89
90         memset (&sa, '\0', sizeof (sa));
91         sa.sun_family = AF_UNIX;
92         sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
93                         sizeof (sa.sun_path));
94
95         DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
96
97         if (delete_socket)
98         {
99                 errno = 0;
100                 status = unlink (sa.sun_path);
101                 if ((status != 0) && (errno != ENOENT))
102                 {
103                         char errbuf[1024];
104                         WARNING ("unixsock plugin: Deleting socket file \"%s\" failed: %s",
105                                         sa.sun_path,
106                                         sstrerror (errno, errbuf, sizeof (errbuf)));
107                 }
108                 else if (status == 0)
109                 {
110                         INFO ("unixsock plugin: Successfully deleted socket file \"%s\".",
111                                         sa.sun_path);
112                 }
113         }
114
115         status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
116         if (status != 0)
117         {
118                 char errbuf[1024];
119                 sstrerror (errno, errbuf, sizeof (errbuf));
120                 ERROR ("unixsock plugin: bind failed: %s", errbuf);
121                 close (sock_fd);
122                 sock_fd = -1;
123                 return (-1);
124         }
125
126         chmod (sa.sun_path, sock_perms);
127
128         status = listen (sock_fd, 8);
129         if (status != 0)
130         {
131                 char errbuf[1024];
132                 ERROR ("unixsock plugin: listen failed: %s",
133                                 sstrerror (errno, errbuf, sizeof (errbuf)));
134                 close (sock_fd);
135                 sock_fd = -1;
136                 return (-1);
137         }
138
139         do
140         {
141                 char *grpname;
142                 struct group *g;
143                 struct group sg;
144                 char grbuf[2048];
145
146                 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
147                 g = NULL;
148
149                 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
150                 if (status != 0)
151                 {
152                         char errbuf[1024];
153                         WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
154                                         sstrerror (errno, errbuf, sizeof (errbuf)));
155                         break;
156                 }
157                 if (g == NULL)
158                 {
159                         WARNING ("unixsock plugin: No such group: `%s'",
160                                         grpname);
161                         break;
162                 }
163
164                 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
165                                         (uid_t) -1, g->gr_gid) != 0)
166                 {
167                         char errbuf[1024];
168                         WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
169                                         (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
170                                         (int) g->gr_gid,
171                                         sstrerror (errno, errbuf, sizeof (errbuf)));
172                 }
173         } while (0);
174
175         return (0);
176 } /* int us_open_socket */
177
178 static void *us_handle_client (void *arg)
179 {
180         int fdin;
181         int fdout;
182         FILE *fhin, *fhout;
183
184         fdin = *((int *) arg);
185         free (arg);
186         arg = NULL;
187
188         DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
189
190         fdout = dup (fdin);
191         if (fdout < 0)
192         {
193                 char errbuf[1024];
194                 ERROR ("unixsock plugin: dup failed: %s",
195                                 sstrerror (errno, errbuf, sizeof (errbuf)));
196                 close (fdin);
197                 pthread_exit ((void *) 1);
198         }
199
200         fhin  = fdopen (fdin, "r");
201         if (fhin == NULL)
202         {
203                 char errbuf[1024];
204                 ERROR ("unixsock plugin: fdopen failed: %s",
205                                 sstrerror (errno, errbuf, sizeof (errbuf)));
206                 close (fdin);
207                 close (fdout);
208                 pthread_exit ((void *) 1);
209                 return ((void *) 1);
210         }
211
212         fhout = fdopen (fdout, "w");
213         if (fhout == NULL)
214         {
215                 char errbuf[1024];
216                 ERROR ("unixsock plugin: fdopen failed: %s",
217                                 sstrerror (errno, errbuf, sizeof (errbuf)));
218                 fclose (fhin); /* this closes fdin as well */
219                 close (fdout);
220                 pthread_exit ((void *) 1);
221                 return ((void *) 1);
222         }
223
224         /* change output buffer to line buffered mode */
225         if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
226         {
227                 char errbuf[1024];
228                 ERROR ("unixsock plugin: setvbuf failed: %s",
229                                 sstrerror (errno, errbuf, sizeof (errbuf)));
230                 fclose (fhin);
231                 fclose (fhout);
232                 pthread_exit ((void *) 1);
233                 return ((void *) 0);
234         }
235
236         while (42)
237         {
238                 char buffer[1024];
239                 char buffer_copy[1024];
240                 char *fields[128];
241                 int   fields_num;
242                 int   len;
243
244                 errno = 0;
245                 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
246                 {
247                         if ((errno == EINTR) || (errno == EAGAIN))
248                                 continue;
249
250                         if (errno != 0)
251                         {
252                                 char errbuf[1024];
253                                 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
254                                                 fileno (fhin),
255                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
256                         }
257                         break;
258                 }
259
260                 len = strlen (buffer);
261                 while ((len > 0)
262                                 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
263                         buffer[--len] = '\0';
264
265                 if (len == 0)
266                         continue;
267
268                 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
269
270                 fields_num = strsplit (buffer_copy, fields,
271                                 sizeof (fields) / sizeof (fields[0]));
272                 if (fields_num < 1)
273                 {
274                         fprintf (fhout, "-1 Internal error\n");
275                         fclose (fhin);
276                         fclose (fhout);
277                         pthread_exit ((void *) 1);
278                         return ((void *) 1);
279                 }
280
281                 if (strcasecmp (fields[0], "getval") == 0)
282                 {
283                         handle_getval (fhout, buffer);
284                 }
285                 else if (strcasecmp (fields[0], "getthreshold") == 0)
286                 {
287                         handle_getthreshold (fhout, buffer);
288                 }
289                 else if (strcasecmp (fields[0], "putval") == 0)
290                 {
291                         handle_putval (fhout, buffer);
292                 }
293                 else if (strcasecmp (fields[0], "listval") == 0)
294                 {
295                         handle_listval (fhout, buffer);
296                 }
297                 else if (strcasecmp (fields[0], "putnotif") == 0)
298                 {
299                         handle_putnotif (fhout, buffer);
300                 }
301                 else if (strcasecmp (fields[0], "flush") == 0)
302                 {
303                         handle_flush (fhout, buffer);
304                 }
305                 else
306                 {
307                         if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
308                         {
309                                 char errbuf[1024];
310                                 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
311                                                 fileno (fhout),
312                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
313                                 break;
314                         }
315                 }
316         } /* while (fgets) */
317
318         DEBUG ("unixsock plugin: us_handle_client: Exiting..");
319         fclose (fhin);
320         fclose (fhout);
321
322         pthread_exit ((void *) 0);
323         return ((void *) 0);
324 } /* void *us_handle_client */
325
326 static void *us_server_thread (void __attribute__((unused)) *arg)
327 {
328         int  status;
329         int *remote_fd;
330         pthread_t th;
331         pthread_attr_t th_attr;
332
333         pthread_attr_init (&th_attr);
334         pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
335
336         if (us_open_socket () != 0)
337                 pthread_exit ((void *) 1);
338
339         while (loop != 0)
340         {
341                 DEBUG ("unixsock plugin: Calling accept..");
342                 status = accept (sock_fd, NULL, NULL);
343                 if (status < 0)
344                 {
345                         char errbuf[1024];
346
347                         if (errno == EINTR)
348                                 continue;
349
350                         ERROR ("unixsock plugin: accept failed: %s",
351                                         sstrerror (errno, errbuf, sizeof (errbuf)));
352                         close (sock_fd);
353                         sock_fd = -1;
354                         pthread_attr_destroy (&th_attr);
355                         pthread_exit ((void *) 1);
356                 }
357
358                 remote_fd = (int *) malloc (sizeof (int));
359                 if (remote_fd == NULL)
360                 {
361                         char errbuf[1024];
362                         WARNING ("unixsock plugin: malloc failed: %s",
363                                         sstrerror (errno, errbuf, sizeof (errbuf)));
364                         close (status);
365                         continue;
366                 }
367                 *remote_fd = status;
368
369                 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
370
371                 status = plugin_thread_create (&th, &th_attr,
372                                 us_handle_client, (void *) remote_fd);
373                 if (status != 0)
374                 {
375                         char errbuf[1024];
376                         WARNING ("unixsock plugin: pthread_create failed: %s",
377                                         sstrerror (errno, errbuf, sizeof (errbuf)));
378                         close (*remote_fd);
379                         free (remote_fd);
380                         continue;
381                 }
382         } /* while (loop) */
383
384         close (sock_fd);
385         sock_fd = -1;
386         pthread_attr_destroy (&th_attr);
387
388         status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
389         if (status != 0)
390         {
391                 char errbuf[1024];
392                 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
393                                 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
394                                 sstrerror (errno, errbuf, sizeof (errbuf)));
395         }
396
397         return ((void *) 0);
398 } /* void *us_server_thread */
399
400 static int us_config (const char *key, const char *val)
401 {
402         if (strcasecmp (key, "SocketFile") == 0)
403         {
404                 char *new_sock_file = strdup (val);
405                 if (new_sock_file == NULL)
406                         return (1);
407
408                 sfree (sock_file);
409                 sock_file = new_sock_file;
410         }
411         else if (strcasecmp (key, "SocketGroup") == 0)
412         {
413                 char *new_sock_group = strdup (val);
414                 if (new_sock_group == NULL)
415                         return (1);
416
417                 sfree (sock_group);
418                 sock_group = new_sock_group;
419         }
420         else if (strcasecmp (key, "SocketPerms") == 0)
421         {
422                 sock_perms = (int) strtol (val, NULL, 8);
423         }
424         else if (strcasecmp (key, "DeleteSocket") == 0)
425         {
426                 if (IS_TRUE (val))
427                         delete_socket = 1;
428                 else
429                         delete_socket = 0;
430         }
431         else
432         {
433                 return (-1);
434         }
435
436         return (0);
437 } /* int us_config */
438
439 static int us_init (void)
440 {
441         static int have_init = 0;
442
443         int status;
444
445         /* Initialize only once. */
446         if (have_init != 0)
447                 return (0);
448         have_init = 1;
449
450         loop = 1;
451
452         status = plugin_thread_create (&listen_thread, NULL,
453                         us_server_thread, NULL);
454         if (status != 0)
455         {
456                 char errbuf[1024];
457                 ERROR ("unixsock plugin: pthread_create failed: %s",
458                                 sstrerror (errno, errbuf, sizeof (errbuf)));
459                 return (-1);
460         }
461
462         return (0);
463 } /* int us_init */
464
465 static int us_shutdown (void)
466 {
467         void *ret;
468
469         loop = 0;
470
471         if (listen_thread != (pthread_t) 0)
472         {
473                 pthread_kill (listen_thread, SIGTERM);
474                 pthread_join (listen_thread, &ret);
475                 listen_thread = (pthread_t) 0;
476         }
477
478         plugin_unregister_init ("unixsock");
479         plugin_unregister_shutdown ("unixsock");
480
481         return (0);
482 } /* int us_shutdown */
483
484 void module_register (void)
485 {
486         plugin_register_config ("unixsock", us_config,
487                         config_keys, config_keys_num);
488         plugin_register_init ("unixsock", us_init);
489         plugin_register_shutdown ("unixsock", us_shutdown);
490 } /* void module_register (void) */
491
492 /* vim: set sw=4 ts=4 sts=4 tw=78 : */