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 };
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
60 static int loop = 0;
62 /* socket configuration */
63 static int sock_fd = -1;
64 static char *sock_file = NULL;
65 static char *sock_group = NULL;
66 static int sock_perms = S_IRWXU | S_IRWXG;
68 static pthread_t listen_thread = (pthread_t) 0;
70 /*
71 * Functions
72 */
73 static int us_open_socket (void)
74 {
75 struct sockaddr_un sa;
76 int status;
78 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
79 if (sock_fd < 0)
80 {
81 char errbuf[1024];
82 ERROR ("unixsock plugin: socket failed: %s",
83 sstrerror (errno, errbuf, sizeof (errbuf)));
84 return (-1);
85 }
87 memset (&sa, '\0', sizeof (sa));
88 sa.sun_family = AF_UNIX;
89 sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
90 sizeof (sa.sun_path));
91 /* unlink (sa.sun_path); */
93 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
95 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
96 if (status != 0)
97 {
98 char errbuf[1024];
99 sstrerror (errno, errbuf, sizeof (errbuf));
100 ERROR ("unixsock plugin: bind failed: %s", errbuf);
101 close (sock_fd);
102 sock_fd = -1;
103 return (-1);
104 }
106 chmod (sa.sun_path, sock_perms);
108 status = listen (sock_fd, 8);
109 if (status != 0)
110 {
111 char errbuf[1024];
112 ERROR ("unixsock plugin: listen failed: %s",
113 sstrerror (errno, errbuf, sizeof (errbuf)));
114 close (sock_fd);
115 sock_fd = -1;
116 return (-1);
117 }
119 do
120 {
121 char *grpname;
122 struct group *g;
123 struct group sg;
124 char grbuf[2048];
126 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
127 g = NULL;
129 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
130 if (status != 0)
131 {
132 char errbuf[1024];
133 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
134 sstrerror (errno, errbuf, sizeof (errbuf)));
135 break;
136 }
137 if (g == NULL)
138 {
139 WARNING ("unixsock plugin: No such group: `%s'",
140 grpname);
141 break;
142 }
144 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
145 (uid_t) -1, g->gr_gid) != 0)
146 {
147 char errbuf[1024];
148 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
149 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
150 (int) g->gr_gid,
151 sstrerror (errno, errbuf, sizeof (errbuf)));
152 }
153 } while (0);
155 return (0);
156 } /* int us_open_socket */
158 static void *us_handle_client (void *arg)
159 {
160 int fd;
161 FILE *fhin, *fhout;
163 fd = *((int *) arg);
164 free (arg);
165 arg = NULL;
167 DEBUG ("Reading from fd #%i", fd);
169 fhin = fdopen (fd, "r");
170 if (fhin == NULL)
171 {
172 char errbuf[1024];
173 ERROR ("unixsock plugin: fdopen failed: %s",
174 sstrerror (errno, errbuf, sizeof (errbuf)));
175 close (fd);
176 pthread_exit ((void *) 1);
177 }
179 fhout = fdopen (fd, "w");
180 if (fhout == NULL)
181 {
182 char errbuf[1024];
183 ERROR ("unixsock plugin: fdopen failed: %s",
184 sstrerror (errno, errbuf, sizeof (errbuf)));
185 fclose (fhin); /* this closes fd as well */
186 pthread_exit ((void *) 1);
187 }
189 /* change output buffer to line buffered mode */
190 if (setvbuf (fhout, NULL, _IOLBF, 0) != 0)
191 {
192 char errbuf[1024];
193 ERROR ("unixsock plugin: setvbuf failed: %s",
194 sstrerror (errno, errbuf, sizeof (errbuf)));
195 fclose (fhin);
196 fclose (fhout);
197 pthread_exit ((void *) 1);
198 }
200 while (42)
201 {
202 char buffer[1024];
203 char buffer_copy[1024];
204 char *fields[128];
205 int fields_num;
206 int len;
208 errno = 0;
209 if (fgets (buffer, sizeof (buffer), fhin) == NULL)
210 {
211 if (errno != 0)
212 {
213 char errbuf[1024];
214 WARNING ("unixsock plugin: failed to read from socket #%i: %s",
215 fileno (fhin),
216 sstrerror (errno, errbuf, sizeof (errbuf)));
217 }
218 break;
219 }
221 len = strlen (buffer);
222 while ((len > 0)
223 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
224 buffer[--len] = '\0';
226 if (len == 0)
227 continue;
229 sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
231 fields_num = strsplit (buffer_copy, fields,
232 sizeof (fields) / sizeof (fields[0]));
234 if (fields_num < 1)
235 {
236 close (fd);
237 break;
238 }
240 if (strcasecmp (fields[0], "getval") == 0)
241 {
242 handle_getval (fhout, fields, fields_num);
243 }
244 else if (strcasecmp (fields[0], "putval") == 0)
245 {
246 handle_putval (fhout, fields, fields_num);
247 }
248 else if (strcasecmp (fields[0], "listval") == 0)
249 {
250 handle_listval (fhout, fields, fields_num);
251 }
252 else if (strcasecmp (fields[0], "putnotif") == 0)
253 {
254 handle_putnotif (fhout, fields, fields_num);
255 }
256 else if (strcasecmp (fields[0], "flush") == 0)
257 {
258 handle_flush (fhout, buffer);
259 }
260 else
261 {
262 if (fprintf (fhout, "-1 Unknown command: %s\n", fields[0]) < 0)
263 {
264 char errbuf[1024];
265 WARNING ("unixsock plugin: failed to write to socket #%i: %s",
266 fileno (fhout),
267 sstrerror (errno, errbuf, sizeof (errbuf)));
268 break;
269 }
270 }
271 } /* while (fgets) */
273 DEBUG ("Exiting..");
274 fclose (fhin);
275 fclose (fhout);
277 pthread_exit ((void *) 0);
278 return ((void *) 0);
279 } /* void *us_handle_client */
281 static void *us_server_thread (void *arg)
282 {
283 int status;
284 int *remote_fd;
285 pthread_t th;
286 pthread_attr_t th_attr;
288 if (us_open_socket () != 0)
289 pthread_exit ((void *) 1);
291 while (loop != 0)
292 {
293 DEBUG ("unixsock plugin: Calling accept..");
294 status = accept (sock_fd, NULL, NULL);
295 if (status < 0)
296 {
297 char errbuf[1024];
299 if (errno == EINTR)
300 continue;
302 ERROR ("unixsock plugin: accept failed: %s",
303 sstrerror (errno, errbuf, sizeof (errbuf)));
304 close (sock_fd);
305 sock_fd = -1;
306 pthread_exit ((void *) 1);
307 }
309 remote_fd = (int *) malloc (sizeof (int));
310 if (remote_fd == NULL)
311 {
312 char errbuf[1024];
313 WARNING ("unixsock plugin: malloc failed: %s",
314 sstrerror (errno, errbuf, sizeof (errbuf)));
315 close (status);
316 continue;
317 }
318 *remote_fd = status;
320 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
322 pthread_attr_init (&th_attr);
323 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
325 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
326 if (status != 0)
327 {
328 char errbuf[1024];
329 WARNING ("unixsock plugin: pthread_create failed: %s",
330 sstrerror (errno, errbuf, sizeof (errbuf)));
331 close (*remote_fd);
332 free (remote_fd);
333 continue;
334 }
335 } /* while (loop) */
337 close (sock_fd);
338 sock_fd = -1;
340 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
341 if (status != 0)
342 {
343 char errbuf[1024];
344 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
345 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
346 sstrerror (errno, errbuf, sizeof (errbuf)));
347 }
349 return ((void *) 0);
350 } /* void *us_server_thread */
352 static int us_config (const char *key, const char *val)
353 {
354 if (strcasecmp (key, "SocketFile") == 0)
355 {
356 char *new_sock_file = strdup (val);
357 if (new_sock_file == NULL)
358 return (1);
360 sfree (sock_file);
361 sock_file = new_sock_file;
362 }
363 else if (strcasecmp (key, "SocketGroup") == 0)
364 {
365 char *new_sock_group = strdup (val);
366 if (new_sock_group == NULL)
367 return (1);
369 sfree (sock_group);
370 sock_group = new_sock_group;
371 }
372 else if (strcasecmp (key, "SocketPerms") == 0)
373 {
374 sock_perms = (int) strtol (val, NULL, 8);
375 }
376 else
377 {
378 return (-1);
379 }
381 return (0);
382 } /* int us_config */
384 static int us_init (void)
385 {
386 int status;
388 loop = 1;
390 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
391 if (status != 0)
392 {
393 char errbuf[1024];
394 ERROR ("unixsock plugin: pthread_create failed: %s",
395 sstrerror (errno, errbuf, sizeof (errbuf)));
396 return (-1);
397 }
399 return (0);
400 } /* int us_init */
402 static int us_shutdown (void)
403 {
404 void *ret;
406 loop = 0;
408 if (listen_thread != (pthread_t) 0)
409 {
410 pthread_kill (listen_thread, SIGTERM);
411 pthread_join (listen_thread, &ret);
412 listen_thread = (pthread_t) 0;
413 }
415 plugin_unregister_init ("unixsock");
416 plugin_unregister_shutdown ("unixsock");
418 return (0);
419 } /* int us_shutdown */
421 void module_register (void)
422 {
423 plugin_register_config ("unixsock", us_config,
424 config_keys, config_keys_num);
425 plugin_register_init ("unixsock", us_init);
426 plugin_register_shutdown ("unixsock", us_shutdown);
427 } /* void module_register (void) */
429 /* vim: set sw=4 ts=4 sts=4 tw=78 : */