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