command parser: Add support for the GETVAL command.
[collectd.git] / src / unixsock.c
1 /**
2  * collectd - src/unixsock.c
3  * Copyright (C) 2007,2008  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #include "utils_cmd_flush.h"
33 #include "utils_cmd_getval.h"
34 #include "utils_cmd_getthreshold.h"
35 #include "utils_cmd_listval.h"
36 #include "utils_cmd_putval.h"
37 #include "utils_cmd_putnotif.h"
38
39 #include <sys/stat.h>
40 #include <sys/un.h>
41
42 #include <grp.h>
43
44 #ifndef UNIX_PATH_MAX
45 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
46 #endif
47
48 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
49
50 /*
51  * Private variables
52  */
53 /* valid configuration file keys */
54 static const char *config_keys[] =
55 {
56         "SocketFile",
57         "SocketGroup",
58         "SocketPerms",
59         "DeleteSocket"
60 };
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62
63 static int loop = 0;
64
65 /* socket configuration */
66 static int   sock_fd    = -1;
67 static char *sock_file  = NULL;
68 static char *sock_group = NULL;
69 static int   sock_perms = S_IRWXU | S_IRWXG;
70 static _Bool delete_socket = 0;
71
72 static pthread_t listen_thread = (pthread_t) 0;
73
74 /*
75  * Functions
76  */
77 static int us_open_socket (void)
78 {
79         struct sockaddr_un sa = { 0 };
80         int status;
81
82         sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
83         if (sock_fd < 0)
84         {
85                 char errbuf[1024];
86                 ERROR ("unixsock plugin: socket failed: %s",
87                                 sstrerror (errno, errbuf, sizeof (errbuf)));
88                 return (-1);
89         }
90
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         status = chmod (sa.sun_path, sock_perms);
127         if (status == -1)
128         {
129                 char errbuf[1024];
130                 ERROR ("unixsock plugin: chmod failed: %s",
131                                 sstrerror (errno, errbuf, sizeof (errbuf)));
132                 close (sock_fd);
133                 sock_fd = -1;
134                 return (-1);
135         }
136
137         status = listen (sock_fd, 8);
138         if (status != 0)
139         {
140                 char errbuf[1024];
141                 ERROR ("unixsock plugin: listen failed: %s",
142                                 sstrerror (errno, errbuf, sizeof (errbuf)));
143                 close (sock_fd);
144                 sock_fd = -1;
145                 return (-1);
146         }
147
148         do
149         {
150                 const char *grpname;
151                 struct group *g;
152                 struct group sg;
153                 char grbuf[2048];
154
155                 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
156                 g = NULL;
157
158                 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
159                 if (status != 0)
160                 {
161                         char errbuf[1024];
162                         WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
163                                         sstrerror (errno, errbuf, sizeof (errbuf)));
164                         break;
165                 }
166                 if (g == NULL)
167                 {
168                         WARNING ("unixsock plugin: No such group: `%s'",
169                                         grpname);
170                         break;
171                 }
172
173                 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
174                                         (uid_t) -1, g->gr_gid) != 0)
175                 {
176                         char errbuf[1024];
177                         WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
178                                         (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
179                                         (int) g->gr_gid,
180                                         sstrerror (errno, errbuf, sizeof (errbuf)));
181                 }
182         } while (0);
183
184         return (0);
185 } /* int us_open_socket */
186
187 static void *us_handle_client (void *arg)
188 {
189         int fdin;
190         int fdout;
191         FILE *fhin, *fhout;
192
193         fdin = *((int *) arg);
194         free (arg);
195         arg = NULL;
196
197         DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
198
199         fdout = dup (fdin);
200         if (fdout < 0)
201         {
202                 char errbuf[1024];
203                 ERROR ("unixsock plugin: dup failed: %s",
204                                 sstrerror (errno, errbuf, sizeof (errbuf)));
205                 close (fdin);
206                 pthread_exit ((void *) 1);
207         }
208
209         fhin  = fdopen (fdin, "r");
210         if (fhin == NULL)
211         {
212                 char errbuf[1024];
213                 ERROR ("unixsock plugin: fdopen failed: %s",
214                                 sstrerror (errno, errbuf, sizeof (errbuf)));
215                 close (fdin);
216                 close (fdout);
217                 pthread_exit ((void *) 1);
218                 return ((void *) 1);
219         }
220
221         fhout = fdopen (fdout, "w");
222         if (fhout == NULL)
223         {
224                 char errbuf[1024];
225                 ERROR ("unixsock plugin: fdopen failed: %s",
226                                 sstrerror (errno, errbuf, sizeof (errbuf)));
227                 fclose (fhin); /* this closes fdin as well */
228                 close (fdout);
229                 pthread_exit ((void *) 1);
230                 return ((void *) 1);
231         }
232
233         /* change output buffer to line buffered mode */
234         if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
235         {
236                 char errbuf[1024];
237                 ERROR ("unixsock plugin: setvbuf failed: %s",
238                                 sstrerror (errno, errbuf, sizeof (errbuf)));
239                 fclose (fhin);
240                 fclose (fhout);
241                 pthread_exit ((void *) 1);
242                 return ((void *) 0);
243         }
244
245         while (42)
246         {
247                 char buffer[1024];
248                 char buffer_copy[1024];
249                 char *fields[128];
250                 int   fields_num;
251                 int   len;
252
253                 errno = 0;
254                 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
255                 {
256                         if ((errno == EINTR) || (errno == EAGAIN))
257                                 continue;
258
259                         if (errno != 0)
260                         {
261                                 char errbuf[1024];
262                                 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
263                                                 fileno (fhin),
264                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
265                         }
266                         break;
267                 }
268
269                 len = strlen (buffer);
270                 while ((len > 0)
271                                 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
272                         buffer[--len] = '\0';
273
274                 if (len == 0)
275                         continue;
276
277                 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
278
279                 fields_num = strsplit (buffer_copy, fields,
280                                 sizeof (fields) / sizeof (fields[0]));
281                 if (fields_num < 1)
282                 {
283                         fprintf (fhout, "-1 Internal error\n");
284                         fclose (fhin);
285                         fclose (fhout);
286                         pthread_exit ((void *) 1);
287                         return ((void *) 1);
288                 }
289
290                 if (strcasecmp (fields[0], "getval") == 0)
291                 {
292                         cmd_handle_getval (fhout, buffer);
293                 }
294                 else if (strcasecmp (fields[0], "getthreshold") == 0)
295                 {
296                         handle_getthreshold (fhout, buffer);
297                 }
298                 else if (strcasecmp (fields[0], "putval") == 0)
299                 {
300                         cmd_handle_putval (fhout, buffer);
301                 }
302                 else if (strcasecmp (fields[0], "listval") == 0)
303                 {
304                         cmd_handle_listval (fhout, buffer);
305                 }
306                 else if (strcasecmp (fields[0], "putnotif") == 0)
307                 {
308                         handle_putnotif (fhout, buffer);
309                 }
310                 else if (strcasecmp (fields[0], "flush") == 0)
311                 {
312                         cmd_handle_flush (fhout, buffer);
313                 }
314                 else
315                 {
316                         if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
317                         {
318                                 char errbuf[1024];
319                                 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
320                                                 fileno (fhout),
321                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
322                                 break;
323                         }
324                 }
325         } /* while (fgets) */
326
327         DEBUG ("unixsock plugin: us_handle_client: Exiting..");
328         fclose (fhin);
329         fclose (fhout);
330
331         pthread_exit ((void *) 0);
332         return ((void *) 0);
333 } /* void *us_handle_client */
334
335 static void *us_server_thread (void __attribute__((unused)) *arg)
336 {
337         int  status;
338         int *remote_fd;
339         pthread_t th;
340         pthread_attr_t th_attr;
341
342         pthread_attr_init (&th_attr);
343         pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
344
345         if (us_open_socket () != 0)
346                 pthread_exit ((void *) 1);
347
348         while (loop != 0)
349         {
350                 DEBUG ("unixsock plugin: Calling accept..");
351                 status = accept (sock_fd, NULL, NULL);
352                 if (status < 0)
353                 {
354                         char errbuf[1024];
355
356                         if (errno == EINTR)
357                                 continue;
358
359                         ERROR ("unixsock plugin: accept failed: %s",
360                                         sstrerror (errno, errbuf, sizeof (errbuf)));
361                         close (sock_fd);
362                         sock_fd = -1;
363                         pthread_attr_destroy (&th_attr);
364                         pthread_exit ((void *) 1);
365                 }
366
367                 remote_fd = malloc (sizeof (*remote_fd));
368                 if (remote_fd == NULL)
369                 {
370                         char errbuf[1024];
371                         WARNING ("unixsock plugin: malloc failed: %s",
372                                         sstrerror (errno, errbuf, sizeof (errbuf)));
373                         close (status);
374                         continue;
375                 }
376                 *remote_fd = status;
377
378                 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
379
380                 status = plugin_thread_create (&th, &th_attr,
381                                 us_handle_client, (void *) remote_fd);
382                 if (status != 0)
383                 {
384                         char errbuf[1024];
385                         WARNING ("unixsock plugin: pthread_create failed: %s",
386                                         sstrerror (errno, errbuf, sizeof (errbuf)));
387                         close (*remote_fd);
388                         free (remote_fd);
389                         continue;
390                 }
391         } /* while (loop) */
392
393         close (sock_fd);
394         sock_fd = -1;
395         pthread_attr_destroy (&th_attr);
396
397         status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
398         if (status != 0)
399         {
400                 char errbuf[1024];
401                 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
402                                 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
403                                 sstrerror (errno, errbuf, sizeof (errbuf)));
404         }
405
406         return ((void *) 0);
407 } /* void *us_server_thread */
408
409 static int us_config (const char *key, const char *val)
410 {
411         if (strcasecmp (key, "SocketFile") == 0)
412         {
413                 char *new_sock_file = strdup (val);
414                 if (new_sock_file == NULL)
415                         return (1);
416
417                 sfree (sock_file);
418                 sock_file = new_sock_file;
419         }
420         else if (strcasecmp (key, "SocketGroup") == 0)
421         {
422                 char *new_sock_group = strdup (val);
423                 if (new_sock_group == NULL)
424                         return (1);
425
426                 sfree (sock_group);
427                 sock_group = new_sock_group;
428         }
429         else if (strcasecmp (key, "SocketPerms") == 0)
430         {
431                 sock_perms = (int) strtol (val, NULL, 8);
432         }
433         else if (strcasecmp (key, "DeleteSocket") == 0)
434         {
435                 if (IS_TRUE (val))
436                         delete_socket = 1;
437                 else
438                         delete_socket = 0;
439         }
440         else
441         {
442                 return (-1);
443         }
444
445         return (0);
446 } /* int us_config */
447
448 static int us_init (void)
449 {
450         static int have_init = 0;
451
452         int status;
453
454         /* Initialize only once. */
455         if (have_init != 0)
456                 return (0);
457         have_init = 1;
458
459         loop = 1;
460
461         status = plugin_thread_create (&listen_thread, NULL,
462                         us_server_thread, NULL);
463         if (status != 0)
464         {
465                 char errbuf[1024];
466                 ERROR ("unixsock plugin: pthread_create failed: %s",
467                                 sstrerror (errno, errbuf, sizeof (errbuf)));
468                 return (-1);
469         }
470
471         return (0);
472 } /* int us_init */
473
474 static int us_shutdown (void)
475 {
476         void *ret;
477
478         loop = 0;
479
480         if (listen_thread != (pthread_t) 0)
481         {
482                 pthread_kill (listen_thread, SIGTERM);
483                 pthread_join (listen_thread, &ret);
484                 listen_thread = (pthread_t) 0;
485         }
486
487         plugin_unregister_init ("unixsock");
488         plugin_unregister_shutdown ("unixsock");
489
490         return (0);
491 } /* int us_shutdown */
492
493 void module_register (void)
494 {
495         plugin_register_config ("unixsock", us_config,
496                         config_keys, config_keys_num);
497         plugin_register_init ("unixsock", us_init);
498         plugin_register_shutdown ("unixsock", us_shutdown);
499 } /* void module_register (void) */
500
501 /* vim: set sw=4 ts=4 sts=4 tw=78 : */