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