2 * collectd - src/unixsock.c
3 * Copyright (C) 2007,2008 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
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"
45 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
48 #define US_DEFAULT_PATH LOCALSTATEDIR "/run/" PACKAGE_NAME "-unixsock"
53 /* valid configuration file keys */
54 static const char *config_keys[] = {"SocketFile", "SocketGroup", "SocketPerms",
56 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
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;
72 static int us_open_socket(void) {
73 struct sockaddr_un sa = {0};
76 sock_fd = socket(PF_UNIX, SOCK_STREAM, 0);
79 ERROR("unixsock plugin: socket failed: %s",
80 sstrerror(errno, errbuf, sizeof(errbuf)));
84 sa.sun_family = AF_UNIX;
85 sstrncpy(sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
88 DEBUG("unixsock plugin: socket path = %s", sa.sun_path);
92 status = unlink(sa.sun_path);
93 if ((status != 0) && (errno != ENOENT)) {
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\".",
103 status = bind(sock_fd, (struct sockaddr *)&sa, sizeof(sa));
106 sstrerror(errno, errbuf, sizeof(errbuf));
107 ERROR("unixsock plugin: bind failed: %s", errbuf);
113 status = chmod(sa.sun_path, sock_perms);
116 ERROR("unixsock plugin: chmod failed: %s",
117 sstrerror(errno, errbuf, sizeof(errbuf)));
123 status = listen(sock_fd, 8);
126 ERROR("unixsock plugin: listen failed: %s",
127 sstrerror(errno, errbuf, sizeof(errbuf)));
139 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
142 status = getgrnam_r(grpname, &sg, grbuf, sizeof(grbuf), &g);
145 WARNING("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
146 sstrerror(status, errbuf, sizeof(errbuf)));
150 WARNING("unixsock plugin: No such group: `%s'", grpname);
154 if (chown((sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (uid_t)-1,
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)));
164 } /* int us_open_socket */
166 static void *us_handle_client(void *arg) {
171 fdin = *((int *)arg);
175 DEBUG("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
180 ERROR("unixsock plugin: dup failed: %s",
181 sstrerror(errno, errbuf, sizeof(errbuf)));
183 pthread_exit((void *)1);
186 fhin = fdopen(fdin, "r");
189 ERROR("unixsock plugin: fdopen failed: %s",
190 sstrerror(errno, errbuf, sizeof(errbuf)));
193 pthread_exit((void *)1);
197 fhout = fdopen(fdout, "w");
200 ERROR("unixsock plugin: fdopen failed: %s",
201 sstrerror(errno, errbuf, sizeof(errbuf)));
202 fclose(fhin); /* this closes fdin as well */
204 pthread_exit((void *)1);
208 /* change output buffer to line buffered mode */
209 if (setvbuf(fhout, NULL, _IOLBF, 0) != 0) {
211 ERROR("unixsock plugin: setvbuf failed: %s",
212 sstrerror(errno, errbuf, sizeof(errbuf)));
215 pthread_exit((void *)1);
221 char buffer_copy[1024];
227 if (fgets(buffer, sizeof(buffer), fhin) == NULL) {
228 if ((errno == EINTR) || (errno == EAGAIN))
233 WARNING("unixsock plugin: failed to read from socket #%i: %s",
234 fileno(fhin), sstrerror(errno, errbuf, sizeof(errbuf)));
239 len = strlen(buffer);
241 ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
242 buffer[--len] = '\0';
247 sstrncpy(buffer_copy, buffer, sizeof(buffer_copy));
250 strsplit(buffer_copy, fields, sizeof(fields) / sizeof(fields[0]));
251 if (fields_num < 1) {
252 fprintf(fhout, "-1 Internal error\n");
255 pthread_exit((void *)1);
259 if (strcasecmp(fields[0], "getval") == 0) {
260 cmd_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 cmd_handle_putval(fhout, buffer);
265 } else if (strcasecmp(fields[0], "listval") == 0) {
266 cmd_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 cmd_handle_flush(fhout, buffer);
272 if (fprintf(fhout, "-1 Unknown command: %s\n", fields[0]) < 0) {
274 WARNING("unixsock plugin: failed to write to socket #%i: %s",
275 fileno(fhout), sstrerror(errno, errbuf, sizeof(errbuf)));
279 } /* while (fgets) */
281 DEBUG("unixsock plugin: us_handle_client: Exiting..");
285 pthread_exit((void *)0);
287 } /* void *us_handle_client */
289 static void *us_server_thread(void __attribute__((unused)) * arg) {
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);
302 DEBUG("unixsock plugin: Calling accept..");
303 status = accept(sock_fd, NULL, NULL);
310 ERROR("unixsock plugin: accept failed: %s",
311 sstrerror(errno, errbuf, sizeof(errbuf)));
314 pthread_attr_destroy(&th_attr);
315 pthread_exit((void *)1);
318 remote_fd = malloc(sizeof(*remote_fd));
319 if (remote_fd == NULL) {
321 WARNING("unixsock plugin: malloc failed: %s",
322 sstrerror(errno, errbuf, sizeof(errbuf)));
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, "unixsock conn");
334 WARNING("unixsock plugin: pthread_create failed: %s",
335 sstrerror(errno, errbuf, sizeof(errbuf)));
344 pthread_attr_destroy(&th_attr);
346 status = unlink((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
349 NOTICE("unixsock plugin: unlink (%s) failed: %s",
350 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
351 sstrerror(errno, errbuf, sizeof(errbuf)));
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)
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)
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) {
384 } /* int us_config */
386 static int us_init(void) {
387 static int have_init = 0;
391 /* Initialize only once. */
398 status = plugin_thread_create(&listen_thread, NULL, us_server_thread, NULL,
402 ERROR("unixsock plugin: pthread_create failed: %s",
403 sstrerror(errno, errbuf, sizeof(errbuf)));
410 static int us_shutdown(void) {
415 if (listen_thread != (pthread_t)0) {
416 pthread_kill(listen_thread, SIGTERM);
417 pthread_join(listen_thread, &ret);
418 listen_thread = (pthread_t)0;
421 plugin_unregister_init("unixsock");
422 plugin_unregister_shutdown("unixsock");
425 } /* int us_shutdown */
427 void module_register(void) {
428 plugin_register_config("unixsock", us_config, config_keys, config_keys_num);
429 plugin_register_init("unixsock", us_init);
430 plugin_register_shutdown("unixsock", us_shutdown);
431 } /* void module_register (void) */