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 fd;
162         FILE *fhin, *fhout;
163
164         fd = *((int *) arg);
165         free (arg);
166         arg = NULL;
167
168         DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fd);
169
170         fhin  = fdopen (fd, "r");
171         if (fhin == NULL)
172         {
173                 char errbuf[1024];
174                 ERROR ("unixsock plugin: fdopen failed: %s",
175                                 sstrerror (errno, errbuf, sizeof (errbuf)));
176                 close (fd);
177                 pthread_exit ((void *) 1);
178         }
179
180         fhout = fdopen (fd, "w");
181         if (fhout == NULL)
182         {
183                 char errbuf[1024];
184                 ERROR ("unixsock plugin: fdopen failed: %s",
185                                 sstrerror (errno, errbuf, sizeof (errbuf)));
186                 fclose (fhin); /* this closes fd as well */
187                 pthread_exit ((void *) 1);
188         }
189
190         /* change output buffer to line buffered mode */
191         if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
192         {
193                 char errbuf[1024];
194                 ERROR ("unixsock plugin: setvbuf failed: %s",
195                                 sstrerror (errno, errbuf, sizeof (errbuf)));
196                 fclose (fhin);
197                 fclose (fhout);
198                 pthread_exit ((void *) 1);
199         }
200
201         while (42)
202         {
203                 char buffer[1024];
204                 char buffer_copy[1024];
205                 char *fields[128];
206                 int   fields_num;
207                 int   len;
208
209                 errno = 0;
210                 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
211                 {
212                         if (errno != 0)
213                         {
214                                 char errbuf[1024];
215                                 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
216                                                 fileno (fhin),
217                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
218                         }
219                         break;
220                 }
221
222                 len = strlen (buffer);
223                 while ((len > 0)
224                                 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
225                         buffer[--len] = '\0';
226
227                 if (len == 0)
228                         continue;
229
230                 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
231
232                 fields_num = strsplit (buffer_copy, fields,
233                                 sizeof (fields) / sizeof (fields[0]));
234
235                 if (fields_num < 1)
236                 {
237                         close (fd);
238                         break;
239                 }
240
241                 if (strcasecmp (fields[0], "getval") == 0)
242                 {
243                         handle_getval (fhout, buffer);
244                 }
245                 else if (strcasecmp (fields[0], "getthreshold") == 0)
246                 {
247                         handle_getthreshold (fhout, buffer);
248                 }
249                 else if (strcasecmp (fields[0], "putval") == 0)
250                 {
251                         handle_putval (fhout, buffer);
252                 }
253                 else if (strcasecmp (fields[0], "listval") == 0)
254                 {
255                         handle_listval (fhout, buffer);
256                 }
257                 else if (strcasecmp (fields[0], "putnotif") == 0)
258                 {
259                         handle_putnotif (fhout, buffer);
260                 }
261                 else if (strcasecmp (fields[0], "flush") == 0)
262                 {
263                         handle_flush (fhout, buffer);
264                 }
265                 else
266                 {
267                         if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
268                         {
269                                 char errbuf[1024];
270                                 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
271                                                 fileno (fhout),
272                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
273                                 break;
274                         }
275                 }
276         } /* while (fgets) */
277
278         DEBUG ("unixsock plugin: us_handle_client: Exiting..");
279         fclose (fhin);
280         fclose (fhout);
281
282         pthread_exit ((void *) 0);
283         return ((void *) 0);
284 } /* void *us_handle_client */
285
286 static void *us_server_thread (void __attribute__((unused)) *arg)
287 {
288         int  status;
289         int *remote_fd;
290         pthread_t th;
291         pthread_attr_t th_attr;
292
293         if (us_open_socket () != 0)
294                 pthread_exit ((void *) 1);
295
296         while (loop != 0)
297         {
298                 DEBUG ("unixsock plugin: Calling accept..");
299                 status = accept (sock_fd, NULL, NULL);
300                 if (status < 0)
301                 {
302                         char errbuf[1024];
303
304                         if (errno == EINTR)
305                                 continue;
306
307                         ERROR ("unixsock plugin: accept failed: %s",
308                                         sstrerror (errno, errbuf, sizeof (errbuf)));
309                         close (sock_fd);
310                         sock_fd = -1;
311                         pthread_exit ((void *) 1);
312                 }
313
314                 remote_fd = (int *) malloc (sizeof (int));
315                 if (remote_fd == NULL)
316                 {
317                         char errbuf[1024];
318                         WARNING ("unixsock plugin: malloc failed: %s",
319                                         sstrerror (errno, errbuf, sizeof (errbuf)));
320                         close (status);
321                         continue;
322                 }
323                 *remote_fd = status;
324
325                 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
326
327                 pthread_attr_init (&th_attr);
328                 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
329
330                 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
331                 if (status != 0)
332                 {
333                         char errbuf[1024];
334                         WARNING ("unixsock plugin: pthread_create failed: %s",
335                                         sstrerror (errno, errbuf, sizeof (errbuf)));
336                         close (*remote_fd);
337                         free (remote_fd);
338                         continue;
339                 }
340         } /* while (loop) */
341
342         close (sock_fd);
343         sock_fd = -1;
344
345         status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
346         if (status != 0)
347         {
348                 char errbuf[1024];
349                 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
350                                 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
351                                 sstrerror (errno, errbuf, sizeof (errbuf)));
352         }
353
354         return ((void *) 0);
355 } /* void *us_server_thread */
356
357 static int us_config (const char *key, const char *val)
358 {
359         if (strcasecmp (key, "SocketFile") == 0)
360         {
361                 char *new_sock_file = strdup (val);
362                 if (new_sock_file == NULL)
363                         return (1);
364
365                 sfree (sock_file);
366                 sock_file = new_sock_file;
367         }
368         else if (strcasecmp (key, "SocketGroup") == 0)
369         {
370                 char *new_sock_group = strdup (val);
371                 if (new_sock_group == NULL)
372                         return (1);
373
374                 sfree (sock_group);
375                 sock_group = new_sock_group;
376         }
377         else if (strcasecmp (key, "SocketPerms") == 0)
378         {
379                 sock_perms = (int) strtol (val, NULL, 8);
380         }
381         else
382         {
383                 return (-1);
384         }
385
386         return (0);
387 } /* int us_config */
388
389 static int us_init (void)
390 {
391         static int have_init = 0;
392
393         int status;
394
395         /* Initialize only once. */
396         if (have_init != 0)
397                 return (0);
398         have_init = 1;
399
400         loop = 1;
401
402         status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
403         if (status != 0)
404         {
405                 char errbuf[1024];
406                 ERROR ("unixsock plugin: pthread_create failed: %s",
407                                 sstrerror (errno, errbuf, sizeof (errbuf)));
408                 return (-1);
409         }
410
411         return (0);
412 } /* int us_init */
413
414 static int us_shutdown (void)
415 {
416         void *ret;
417
418         loop = 0;
419
420         if (listen_thread != (pthread_t) 0)
421         {
422                 pthread_kill (listen_thread, SIGTERM);
423                 pthread_join (listen_thread, &ret);
424                 listen_thread = (pthread_t) 0;
425         }
426
427         plugin_unregister_init ("unixsock");
428         plugin_unregister_shutdown ("unixsock");
429
430         return (0);
431 } /* int us_shutdown */
432
433 void module_register (void)
434 {
435         plugin_register_config ("unixsock", us_config,
436                         config_keys, config_keys_num);
437         plugin_register_init ("unixsock", us_init);
438         plugin_register_shutdown ("unixsock", us_shutdown);
439 } /* void module_register (void) */
440
441 /* vim: set sw=4 ts=4 sts=4 tw=78 : */