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 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
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"
39 /* Folks without pthread will need to disable this plugin. */
40 #include <pthread.h>
42 #include <sys/stat.h>
43 #include <sys/un.h>
45 #include <grp.h>
47 #ifndef UNIX_PATH_MAX
48 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
49 #endif
51 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
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);
66 static int loop = 0;
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;
75 static pthread_t listen_thread = (pthread_t) 0;
77 /*
78 * Functions
79 */
80 static int us_open_socket (void)
81 {
82 struct sockaddr_un sa;
83 int status;
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 }
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));
99 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
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 }
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 }
130 status = chmod (sa.sun_path, sock_perms);
131 if (status == -1)
132 {
133 char errbuf[1024];
134 ERROR ("unixsock plugin: chmod failed: %s",
135 sstrerror (errno, errbuf, sizeof (errbuf)));
136 close (sock_fd);
137 sock_fd = -1;
138 return (-1);
139 }
141 status = listen (sock_fd, 8);
142 if (status != 0)
143 {
144 char errbuf[1024];
145 ERROR ("unixsock plugin: listen failed: %s",
146 sstrerror (errno, errbuf, sizeof (errbuf)));
147 close (sock_fd);
148 sock_fd = -1;
149 return (-1);
150 }
152 do
153 {
154 const char *grpname;
155 struct group *g;
156 struct group sg;
157 char grbuf[2048];
159 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
160 g = NULL;
162 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
163 if (status != 0)
164 {
165 char errbuf[1024];
166 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
167 sstrerror (errno, errbuf, sizeof (errbuf)));
168 break;
169 }
170 if (g == NULL)
171 {
172 WARNING ("unixsock plugin: No such group: `%s'",
173 grpname);
174 break;
175 }
177 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
178 (uid_t) -1, g->gr_gid) != 0)
179 {
180 char errbuf[1024];
181 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
182 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
183 (int) g->gr_gid,
184 sstrerror (errno, errbuf, sizeof (errbuf)));
185 }
186 } while (0);
188 return (0);
189 } /* int us_open_socket */
191 static void *us_handle_client (void *arg)
192 {
193 int fdin;
194 int fdout;
195 FILE *fhin, *fhout;
197 fdin = *((int *) arg);
198 free (arg);
199 arg = NULL;
201 DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
203 fdout = dup (fdin);
204 if (fdout < 0)
205 {
206 char errbuf[1024];
207 ERROR ("unixsock plugin: dup failed: %s",
208 sstrerror (errno, errbuf, sizeof (errbuf)));
209 close (fdin);
210 pthread_exit ((void *) 1);
211 }
213 fhin = fdopen (fdin, "r");
214 if (fhin == NULL)
215 {
216 char errbuf[1024];
217 ERROR ("unixsock plugin: fdopen failed: %s",
218 sstrerror (errno, errbuf, sizeof (errbuf)));
219 close (fdin);
220 close (fdout);
221 pthread_exit ((void *) 1);
222 return ((void *) 1);
223 }
225 fhout = fdopen (fdout, "w");
226 if (fhout == NULL)
227 {
228 char errbuf[1024];
229 ERROR ("unixsock plugin: fdopen failed: %s",
230 sstrerror (errno, errbuf, sizeof (errbuf)));
231 fclose (fhin); /* this closes fdin as well */
232 close (fdout);
233 pthread_exit ((void *) 1);
234 return ((void *) 1);
235 }
237 /* change output buffer to line buffered mode */
238 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
239 {
240 char errbuf[1024];
241 ERROR ("unixsock plugin: setvbuf failed: %s",
242 sstrerror (errno, errbuf, sizeof (errbuf)));
243 fclose (fhin);
244 fclose (fhout);
245 pthread_exit ((void *) 1);
246 return ((void *) 0);
247 }
249 while (42)
250 {
251 char buffer[1024];
252 char buffer_copy[1024];
253 char *fields[128];
254 int fields_num;
255 int len;
257 errno = 0;
258 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
259 {
260 if ((errno == EINTR) || (errno == EAGAIN))
261 continue;
263 if (errno != 0)
264 {
265 char errbuf[1024];
266 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
267 fileno (fhin),
268 sstrerror (errno, errbuf, sizeof (errbuf)));
269 }
270 break;
271 }
273 len = strlen (buffer);
274 while ((len > 0)
275 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
276 buffer[--len] = '\0';
278 if (len == 0)
279 continue;
281 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
283 fields_num = strsplit (buffer_copy, fields,
284 sizeof (fields) / sizeof (fields[0]));
285 if (fields_num < 1)
286 {
287 fprintf (fhout, "-1 Internal error\n");
288 fclose (fhin);
289 fclose (fhout);
290 pthread_exit ((void *) 1);
291 return ((void *) 1);
292 }
294 if (strcasecmp (fields[0], "getval") == 0)
295 {
296 handle_getval (fhout, buffer);
297 }
298 else if (strcasecmp (fields[0], "getthreshold") == 0)
299 {
300 handle_getthreshold (fhout, buffer);
301 }
302 else if (strcasecmp (fields[0], "putval") == 0)
303 {
304 handle_putval (fhout, buffer);
305 }
306 else if (strcasecmp (fields[0], "listval") == 0)
307 {
308 handle_listval (fhout, buffer);
309 }
310 else if (strcasecmp (fields[0], "putnotif") == 0)
311 {
312 handle_putnotif (fhout, buffer);
313 }
314 else if (strcasecmp (fields[0], "flush") == 0)
315 {
316 handle_flush (fhout, buffer);
317 }
318 else
319 {
320 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
321 {
322 char errbuf[1024];
323 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
324 fileno (fhout),
325 sstrerror (errno, errbuf, sizeof (errbuf)));
326 break;
327 }
328 }
329 } /* while (fgets) */
331 DEBUG ("unixsock plugin: us_handle_client: Exiting..");
332 fclose (fhin);
333 fclose (fhout);
335 pthread_exit ((void *) 0);
336 return ((void *) 0);
337 } /* void *us_handle_client */
339 static void *us_server_thread (void __attribute__((unused)) *arg)
340 {
341 int status;
342 int *remote_fd;
343 pthread_t th;
344 pthread_attr_t th_attr;
346 pthread_attr_init (&th_attr);
347 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
349 if (us_open_socket () != 0)
350 pthread_exit ((void *) 1);
352 while (loop != 0)
353 {
354 DEBUG ("unixsock plugin: Calling accept..");
355 status = accept (sock_fd, NULL, NULL);
356 if (status < 0)
357 {
358 char errbuf[1024];
360 if (errno == EINTR)
361 continue;
363 ERROR ("unixsock plugin: accept failed: %s",
364 sstrerror (errno, errbuf, sizeof (errbuf)));
365 close (sock_fd);
366 sock_fd = -1;
367 pthread_attr_destroy (&th_attr);
368 pthread_exit ((void *) 1);
369 }
371 remote_fd = malloc (sizeof (*remote_fd));
372 if (remote_fd == NULL)
373 {
374 char errbuf[1024];
375 WARNING ("unixsock plugin: malloc failed: %s",
376 sstrerror (errno, errbuf, sizeof (errbuf)));
377 close (status);
378 continue;
379 }
380 *remote_fd = status;
382 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
384 status = plugin_thread_create (&th, &th_attr,
385 us_handle_client, (void *) remote_fd);
386 if (status != 0)
387 {
388 char errbuf[1024];
389 WARNING ("unixsock plugin: pthread_create failed: %s",
390 sstrerror (errno, errbuf, sizeof (errbuf)));
391 close (*remote_fd);
392 free (remote_fd);
393 continue;
394 }
395 } /* while (loop) */
397 close (sock_fd);
398 sock_fd = -1;
399 pthread_attr_destroy (&th_attr);
401 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
402 if (status != 0)
403 {
404 char errbuf[1024];
405 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
406 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
407 sstrerror (errno, errbuf, sizeof (errbuf)));
408 }
410 return ((void *) 0);
411 } /* void *us_server_thread */
413 static int us_config (const char *key, const char *val)
414 {
415 if (strcasecmp (key, "SocketFile") == 0)
416 {
417 char *new_sock_file = strdup (val);
418 if (new_sock_file == NULL)
419 return (1);
421 sfree (sock_file);
422 sock_file = new_sock_file;
423 }
424 else if (strcasecmp (key, "SocketGroup") == 0)
425 {
426 char *new_sock_group = strdup (val);
427 if (new_sock_group == NULL)
428 return (1);
430 sfree (sock_group);
431 sock_group = new_sock_group;
432 }
433 else if (strcasecmp (key, "SocketPerms") == 0)
434 {
435 sock_perms = (int) strtol (val, NULL, 8);
436 }
437 else if (strcasecmp (key, "DeleteSocket") == 0)
438 {
439 if (IS_TRUE (val))
440 delete_socket = 1;
441 else
442 delete_socket = 0;
443 }
444 else
445 {
446 return (-1);
447 }
449 return (0);
450 } /* int us_config */
452 static int us_init (void)
453 {
454 static int have_init = 0;
456 int status;
458 /* Initialize only once. */
459 if (have_init != 0)
460 return (0);
461 have_init = 1;
463 loop = 1;
465 status = plugin_thread_create (&listen_thread, NULL,
466 us_server_thread, NULL);
467 if (status != 0)
468 {
469 char errbuf[1024];
470 ERROR ("unixsock plugin: pthread_create failed: %s",
471 sstrerror (errno, errbuf, sizeof (errbuf)));
472 return (-1);
473 }
475 return (0);
476 } /* int us_init */
478 static int us_shutdown (void)
479 {
480 void *ret;
482 loop = 0;
484 if (listen_thread != (pthread_t) 0)
485 {
486 pthread_kill (listen_thread, SIGTERM);
487 pthread_join (listen_thread, &ret);
488 listen_thread = (pthread_t) 0;
489 }
491 plugin_unregister_init ("unixsock");
492 plugin_unregister_shutdown ("unixsock");
494 return (0);
495 } /* int us_shutdown */
497 void module_register (void)
498 {
499 plugin_register_config ("unixsock", us_config,
500 config_keys, config_keys_num);
501 plugin_register_init ("unixsock", us_init);
502 plugin_register_shutdown ("unixsock", us_shutdown);
503 } /* void module_register (void) */
505 /* vim: set sw=4 ts=4 sts=4 tw=78 : */