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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
27 #include "utils_cmd_flush.h"
28 #include "utils_cmd_getval.h"
29 #include "utils_cmd_listval.h"
30 #include "utils_cmd_putval.h"
31 #include "utils_cmd_putnotif.h"
33 /* Folks without pthread will need to disable this plugin. */
34 #include <pthread.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/un.h>
40 #include <grp.h>
42 #ifndef UNIX_PATH_MAX
43 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
44 #endif
46 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
48 /*
49 * Private variables
50 */
51 /* valid configuration file keys */
52 static const char *config_keys[] =
53 {
54 "SocketFile",
55 "SocketGroup",
56 "SocketPerms",
57 "DeleteSocket"
58 };
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61 static int loop = 0;
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 static _Bool delete_socket = 0;
70 static pthread_t listen_thread = (pthread_t) 0;
72 /*
73 * Functions
74 */
75 static int us_open_socket (void)
76 {
77 struct sockaddr_un sa;
78 int status;
80 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
81 if (sock_fd < 0)
82 {
83 char errbuf[1024];
84 ERROR ("unixsock plugin: socket failed: %s",
85 sstrerror (errno, errbuf, sizeof (errbuf)));
86 return (-1);
87 }
89 memset (&sa, '\0', sizeof (sa));
90 sa.sun_family = AF_UNIX;
91 sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
92 sizeof (sa.sun_path));
94 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
96 if (delete_socket)
97 {
98 errno = 0;
99 status = unlink (sa.sun_path);
100 if ((status != 0) && (errno != ENOENT))
101 {
102 char errbuf[1024];
103 WARNING ("unixsock plugin: Deleting socket file \"%s\" failed: %s",
104 sa.sun_path,
105 sstrerror (errno, errbuf, sizeof (errbuf)));
106 }
107 else if (status == 0)
108 {
109 INFO ("unixsock plugin: Successfully deleted socket file \"%s\".",
110 sa.sun_path);
111 }
112 }
114 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
115 if (status != 0)
116 {
117 char errbuf[1024];
118 sstrerror (errno, errbuf, sizeof (errbuf));
119 ERROR ("unixsock plugin: bind failed: %s", errbuf);
120 close (sock_fd);
121 sock_fd = -1;
122 return (-1);
123 }
125 status = chmod (sa.sun_path, sock_perms);
126 if (status == -1)
127 {
128 char errbuf[1024];
129 ERROR ("unixsock plugin: chmod failed: %s",
130 sstrerror (errno, errbuf, sizeof (errbuf)));
131 close (sock_fd);
132 sock_fd = -1;
133 return (-1);
134 }
136 status = listen (sock_fd, 8);
137 if (status != 0)
138 {
139 char errbuf[1024];
140 ERROR ("unixsock plugin: listen failed: %s",
141 sstrerror (errno, errbuf, sizeof (errbuf)));
142 close (sock_fd);
143 sock_fd = -1;
144 return (-1);
145 }
147 do
148 {
149 char *grpname;
150 struct group *g;
151 struct group sg;
152 char grbuf[2048];
154 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
155 g = NULL;
157 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
158 if (status != 0)
159 {
160 char errbuf[1024];
161 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
162 sstrerror (errno, errbuf, sizeof (errbuf)));
163 break;
164 }
165 if (g == NULL)
166 {
167 WARNING ("unixsock plugin: No such group: `%s'",
168 grpname);
169 break;
170 }
172 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
173 (uid_t) -1, g->gr_gid) != 0)
174 {
175 char errbuf[1024];
176 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
177 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
178 (int) g->gr_gid,
179 sstrerror (errno, errbuf, sizeof (errbuf)));
180 }
181 } while (0);
183 return (0);
184 } /* int us_open_socket */
186 static void *us_handle_client (void *arg)
187 {
188 int fdin;
189 int fdout;
190 FILE *fhin, *fhout;
192 fdin = *((int *) arg);
193 free (arg);
194 arg = NULL;
196 DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
198 fdout = dup (fdin);
199 if (fdout < 0)
200 {
201 char errbuf[1024];
202 ERROR ("unixsock plugin: dup failed: %s",
203 sstrerror (errno, errbuf, sizeof (errbuf)));
204 close (fdin);
205 pthread_exit ((void *) 1);
206 }
208 fhin = fdopen (fdin, "r");
209 if (fhin == NULL)
210 {
211 char errbuf[1024];
212 ERROR ("unixsock plugin: fdopen failed: %s",
213 sstrerror (errno, errbuf, sizeof (errbuf)));
214 close (fdin);
215 close (fdout);
216 pthread_exit ((void *) 1);
217 return ((void *) 1);
218 }
220 fhout = fdopen (fdout, "w");
221 if (fhout == NULL)
222 {
223 char errbuf[1024];
224 ERROR ("unixsock plugin: fdopen failed: %s",
225 sstrerror (errno, errbuf, sizeof (errbuf)));
226 fclose (fhin); /* this closes fdin as well */
227 close (fdout);
228 pthread_exit ((void *) 1);
229 return ((void *) 1);
230 }
232 /* change output buffer to line buffered mode */
233 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
234 {
235 char errbuf[1024];
236 ERROR ("unixsock plugin: setvbuf failed: %s",
237 sstrerror (errno, errbuf, sizeof (errbuf)));
238 fclose (fhin);
239 fclose (fhout);
240 pthread_exit ((void *) 1);
241 return ((void *) 0);
242 }
244 while (42)
245 {
246 char buffer[1024];
247 char buffer_copy[1024];
248 char *fields[128];
249 int fields_num;
250 int len;
252 errno = 0;
253 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
254 {
255 if ((errno == EINTR) || (errno == EAGAIN))
256 continue;
258 if (errno != 0)
259 {
260 char errbuf[1024];
261 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
262 fileno (fhin),
263 sstrerror (errno, errbuf, sizeof (errbuf)));
264 }
265 break;
266 }
268 len = strlen (buffer);
269 while ((len > 0)
270 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
271 buffer[--len] = '\0';
273 if (len == 0)
274 continue;
276 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
278 fields_num = strsplit (buffer_copy, fields,
279 sizeof (fields) / sizeof (fields[0]));
280 if (fields_num < 1)
281 {
282 fprintf (fhout, "-1 Internal error\n");
283 fclose (fhin);
284 fclose (fhout);
285 pthread_exit ((void *) 1);
286 return ((void *) 1);
287 }
289 if (strcasecmp (fields[0], "getval") == 0)
290 {
291 handle_getval (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) */
322 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
323 fclose (fhin);
324 fclose (fhout);
326 pthread_exit ((void *) 0);
327 return ((void *) 0);
328 } /* void *us_handle_client */
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;
337 pthread_attr_init (&th_attr);
338 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
340 if (us_open_socket () != 0)
341 pthread_exit ((void *) 1);
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];
351 if (errno == EINTR)
352 continue;
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 }
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;
373 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
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) */
388 close (sock_fd);
389 sock_fd = -1;
390 pthread_attr_destroy (&th_attr);
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 }
401 return ((void *) 0);
402 } /* void *us_server_thread */
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);
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);
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 }
440 return (0);
441 } /* int us_config */
443 static int us_init (void)
444 {
445 static int have_init = 0;
447 int status;
449 /* Initialize only once. */
450 if (have_init != 0)
451 return (0);
452 have_init = 1;
454 loop = 1;
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 }
466 return (0);
467 } /* int us_init */
469 static int us_shutdown (void)
470 {
471 void *ret;
473 loop = 0;
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 }
482 plugin_unregister_init ("unixsock");
483 plugin_unregister_shutdown ("unixsock");
485 return (0);
486 } /* int us_shutdown */
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) */
496 /* vim: set sw=4 ts=4 sts=4 tw=78 : */