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