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