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"
29 #include "common.h"
30 #include "plugin.h"
32 #include "utils_cmd_flush.h"
33 #include "utils_cmd_getthreshold.h"
34 #include "utils_cmd_getval.h"
35 #include "utils_cmd_listval.h"
36 #include "utils_cmd_putnotif.h"
37 #include "utils_cmd_putval.h"
39 #include <sys/stat.h>
40 #include <sys/un.h>
42 #include <grp.h>
44 #ifndef UNIX_PATH_MAX
45 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
46 #endif
48 #define US_DEFAULT_PATH LOCALSTATEDIR "/run/" PACKAGE_NAME "-unixsock"
50 /*
51 * Private variables
52 */
53 /* valid configuration file keys */
54 static const char *config_keys[] = {"SocketFile", "SocketGroup", "SocketPerms",
55 "DeleteSocket"};
56 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
58 static int loop = 0;
60 /* socket configuration */
61 static int sock_fd = -1;
62 static char *sock_file = NULL;
63 static char *sock_group = NULL;
64 static int sock_perms = S_IRWXU | S_IRWXG;
65 static _Bool delete_socket = 0;
67 static pthread_t listen_thread = (pthread_t)0;
69 /*
70 * Functions
71 */
72 static int us_open_socket(void) {
73 struct sockaddr_un sa = {0};
74 int status;
76 sock_fd = socket(PF_UNIX, SOCK_STREAM, 0);
77 if (sock_fd < 0) {
78 char errbuf[1024];
79 ERROR("unixsock plugin: socket failed: %s",
80 sstrerror(errno, errbuf, sizeof(errbuf)));
81 return (-1);
82 }
84 sa.sun_family = AF_UNIX;
85 sstrncpy(sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
86 sizeof(sa.sun_path));
88 DEBUG("unixsock plugin: socket path = %s", sa.sun_path);
90 if (delete_socket) {
91 errno = 0;
92 status = unlink(sa.sun_path);
93 if ((status != 0) && (errno != ENOENT)) {
94 char errbuf[1024];
95 WARNING("unixsock plugin: Deleting socket file \"%s\" failed: %s",
96 sa.sun_path, sstrerror(errno, errbuf, sizeof(errbuf)));
97 } else if (status == 0) {
98 INFO("unixsock plugin: Successfully deleted socket file \"%s\".",
99 sa.sun_path);
100 }
101 }
103 status = bind(sock_fd, (struct sockaddr *)&sa, sizeof(sa));
104 if (status != 0) {
105 char errbuf[1024];
106 sstrerror(errno, errbuf, sizeof(errbuf));
107 ERROR("unixsock plugin: bind failed: %s", errbuf);
108 close(sock_fd);
109 sock_fd = -1;
110 return (-1);
111 }
113 status = chmod(sa.sun_path, sock_perms);
114 if (status == -1) {
115 char errbuf[1024];
116 ERROR("unixsock plugin: chmod failed: %s",
117 sstrerror(errno, errbuf, sizeof(errbuf)));
118 close(sock_fd);
119 sock_fd = -1;
120 return (-1);
121 }
123 status = listen(sock_fd, 8);
124 if (status != 0) {
125 char errbuf[1024];
126 ERROR("unixsock plugin: listen failed: %s",
127 sstrerror(errno, errbuf, sizeof(errbuf)));
128 close(sock_fd);
129 sock_fd = -1;
130 return (-1);
131 }
133 do {
134 const char *grpname;
135 struct group *g;
136 struct group sg;
137 char grbuf[2048];
139 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
140 g = NULL;
142 status = getgrnam_r(grpname, &sg, grbuf, sizeof(grbuf), &g);
143 if (status != 0) {
144 char errbuf[1024];
145 WARNING("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
146 sstrerror(errno, errbuf, sizeof(errbuf)));
147 break;
148 }
149 if (g == NULL) {
150 WARNING("unixsock plugin: No such group: `%s'", grpname);
151 break;
152 }
154 if (chown((sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (uid_t)-1,
155 g->gr_gid) != 0) {
156 char errbuf[1024];
157 WARNING("unixsock plugin: chown (%s, -1, %i) failed: %s",
158 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (int)g->gr_gid,
159 sstrerror(errno, errbuf, sizeof(errbuf)));
160 }
161 } while (0);
163 return (0);
164 } /* int us_open_socket */
166 static void *us_handle_client(void *arg) {
167 int fdin;
168 int fdout;
169 FILE *fhin, *fhout;
171 fdin = *((int *)arg);
172 free(arg);
173 arg = NULL;
175 DEBUG("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
177 fdout = dup(fdin);
178 if (fdout < 0) {
179 char errbuf[1024];
180 ERROR("unixsock plugin: dup failed: %s",
181 sstrerror(errno, errbuf, sizeof(errbuf)));
182 close(fdin);
183 pthread_exit((void *)1);
184 }
186 fhin = fdopen(fdin, "r");
187 if (fhin == NULL) {
188 char errbuf[1024];
189 ERROR("unixsock plugin: fdopen failed: %s",
190 sstrerror(errno, errbuf, sizeof(errbuf)));
191 close(fdin);
192 close(fdout);
193 pthread_exit((void *)1);
194 return ((void *)1);
195 }
197 fhout = fdopen(fdout, "w");
198 if (fhout == NULL) {
199 char errbuf[1024];
200 ERROR("unixsock plugin: fdopen failed: %s",
201 sstrerror(errno, errbuf, sizeof(errbuf)));
202 fclose(fhin); /* this closes fdin as well */
203 close(fdout);
204 pthread_exit((void *)1);
205 return ((void *)1);
206 }
208 /* change output buffer to line buffered mode */
209 if (setvbuf(fhout, NULL, _IOLBF, 0) != 0) {
210 char errbuf[1024];
211 ERROR("unixsock plugin: setvbuf failed: %s",
212 sstrerror(errno, errbuf, sizeof(errbuf)));
213 fclose(fhin);
214 fclose(fhout);
215 pthread_exit((void *)1);
216 return ((void *)0);
217 }
219 while (42) {
220 char buffer[1024];
221 char buffer_copy[1024];
222 char *fields[128];
223 int fields_num;
224 int len;
226 errno = 0;
227 if (fgets(buffer, sizeof(buffer), fhin) == NULL) {
228 if ((errno == EINTR) || (errno == EAGAIN))
229 continue;
231 if (errno != 0) {
232 char errbuf[1024];
233 WARNING("unixsock plugin: failed to read from socket #%i: %s",
234 fileno(fhin), sstrerror(errno, errbuf, sizeof(errbuf)));
235 }
236 break;
237 }
239 len = strlen(buffer);
240 while ((len > 0) &&
241 ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
242 buffer[--len] = '\0';
244 if (len == 0)
245 continue;
247 sstrncpy(buffer_copy, buffer, sizeof(buffer_copy));
249 fields_num =
250 strsplit(buffer_copy, fields, sizeof(fields) / sizeof(fields[0]));
251 if (fields_num < 1) {
252 fprintf(fhout, "-1 Internal error\n");
253 fclose(fhin);
254 fclose(fhout);
255 pthread_exit((void *)1);
256 return ((void *)1);
257 }
259 if (strcasecmp(fields[0], "getval") == 0) {
260 handle_getval(fhout, buffer);
261 } else if (strcasecmp(fields[0], "getthreshold") == 0) {
262 handle_getthreshold(fhout, buffer);
263 } else if (strcasecmp(fields[0], "putval") == 0) {
264 handle_putval(fhout, buffer);
265 } else if (strcasecmp(fields[0], "listval") == 0) {
266 handle_listval(fhout, buffer);
267 } else if (strcasecmp(fields[0], "putnotif") == 0) {
268 handle_putnotif(fhout, buffer);
269 } else if (strcasecmp(fields[0], "flush") == 0) {
270 handle_flush(fhout, buffer);
271 } else {
272 if (fprintf(fhout, "-1 Unknown command: %s\n", fields[0]) < 0) {
273 char errbuf[1024];
274 WARNING("unixsock plugin: failed to write to socket #%i: %s",
275 fileno(fhout), sstrerror(errno, errbuf, sizeof(errbuf)));
276 break;
277 }
278 }
279 } /* while (fgets) */
281 DEBUG("unixsock plugin: us_handle_client: Exiting..");
282 fclose(fhin);
283 fclose(fhout);
285 pthread_exit((void *)0);
286 return ((void *)0);
287 } /* void *us_handle_client */
289 static void *us_server_thread(void __attribute__((unused)) * arg) {
290 int status;
291 int *remote_fd;
292 pthread_t th;
293 pthread_attr_t th_attr;
295 pthread_attr_init(&th_attr);
296 pthread_attr_setdetachstate(&th_attr, PTHREAD_CREATE_DETACHED);
298 if (us_open_socket() != 0)
299 pthread_exit((void *)1);
301 while (loop != 0) {
302 DEBUG("unixsock plugin: Calling accept..");
303 status = accept(sock_fd, NULL, NULL);
304 if (status < 0) {
305 char errbuf[1024];
307 if (errno == EINTR)
308 continue;
310 ERROR("unixsock plugin: accept failed: %s",
311 sstrerror(errno, errbuf, sizeof(errbuf)));
312 close(sock_fd);
313 sock_fd = -1;
314 pthread_attr_destroy(&th_attr);
315 pthread_exit((void *)1);
316 }
318 remote_fd = malloc(sizeof(*remote_fd));
319 if (remote_fd == NULL) {
320 char errbuf[1024];
321 WARNING("unixsock plugin: malloc failed: %s",
322 sstrerror(errno, errbuf, sizeof(errbuf)));
323 close(status);
324 continue;
325 }
326 *remote_fd = status;
328 DEBUG("Spawning child to handle connection on fd #%i", *remote_fd);
330 status = plugin_thread_create(&th, &th_attr, us_handle_client,
331 (void *)remote_fd);
332 if (status != 0) {
333 char errbuf[1024];
334 WARNING("unixsock plugin: pthread_create failed: %s",
335 sstrerror(errno, errbuf, sizeof(errbuf)));
336 close(*remote_fd);
337 free(remote_fd);
338 continue;
339 }
340 } /* while (loop) */
342 close(sock_fd);
343 sock_fd = -1;
344 pthread_attr_destroy(&th_attr);
346 status = unlink((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
347 if (status != 0) {
348 char errbuf[1024];
349 NOTICE("unixsock plugin: unlink (%s) failed: %s",
350 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
351 sstrerror(errno, errbuf, sizeof(errbuf)));
352 }
354 return ((void *)0);
355 } /* void *us_server_thread */
357 static int us_config(const char *key, const char *val) {
358 if (strcasecmp(key, "SocketFile") == 0) {
359 char *new_sock_file = strdup(val);
360 if (new_sock_file == NULL)
361 return (1);
363 sfree(sock_file);
364 sock_file = new_sock_file;
365 } else if (strcasecmp(key, "SocketGroup") == 0) {
366 char *new_sock_group = strdup(val);
367 if (new_sock_group == NULL)
368 return (1);
370 sfree(sock_group);
371 sock_group = new_sock_group;
372 } else if (strcasecmp(key, "SocketPerms") == 0) {
373 sock_perms = (int)strtol(val, NULL, 8);
374 } else if (strcasecmp(key, "DeleteSocket") == 0) {
375 if (IS_TRUE(val))
376 delete_socket = 1;
377 else
378 delete_socket = 0;
379 } else {
380 return (-1);
381 }
383 return (0);
384 } /* int us_config */
386 static int us_init(void) {
387 static int have_init = 0;
389 int status;
391 /* Initialize only once. */
392 if (have_init != 0)
393 return (0);
394 have_init = 1;
396 loop = 1;
398 status = plugin_thread_create(&listen_thread, NULL, us_server_thread, NULL);
399 if (status != 0) {
400 char errbuf[1024];
401 ERROR("unixsock plugin: pthread_create failed: %s",
402 sstrerror(errno, errbuf, sizeof(errbuf)));
403 return (-1);
404 }
406 return (0);
407 } /* int us_init */
409 static int us_shutdown(void) {
410 void *ret;
412 loop = 0;
414 if (listen_thread != (pthread_t)0) {
415 pthread_kill(listen_thread, SIGTERM);
416 pthread_join(listen_thread, &ret);
417 listen_thread = (pthread_t)0;
418 }
420 plugin_unregister_init("unixsock");
421 plugin_unregister_shutdown("unixsock");
423 return (0);
424 } /* int us_shutdown */
426 void module_register(void) {
427 plugin_register_config("unixsock", us_config, config_keys, config_keys_num);
428 plugin_register_init("unixsock", us_init);
429 plugin_register_shutdown("unixsock", us_shutdown);
430 } /* void module_register (void) */
432 /* vim: set sw=4 ts=4 sts=4 tw=78 : */