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);
78 ERROR("unixsock plugin: socket failed: %s", STRERRNO);
82 sa.sun_family = AF_UNIX;
83 sstrncpy(sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
86 DEBUG("unixsock plugin: socket path = %s", sa.sun_path);
90 status = unlink(sa.sun_path);
91 if ((status != 0) && (errno != ENOENT)) {
92 WARNING("unixsock plugin: Deleting socket file \"%s\" failed: %s",
93 sa.sun_path, STRERRNO);
94 } else if (status == 0) {
95 INFO("unixsock plugin: Successfully deleted socket file \"%s\".",
100 status = bind(sock_fd, (struct sockaddr *)&sa, sizeof(sa));
102 ERROR("unixsock plugin: bind failed: %s", STRERRNO);
108 status = chmod(sa.sun_path, sock_perms);
110 ERROR("unixsock plugin: chmod failed: %s", STRERRNO);
116 status = listen(sock_fd, 8);
118 ERROR("unixsock plugin: listen failed: %s", STRERRNO);
129 long int grbuf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
131 grbuf_size = sysconf(_SC_PAGESIZE);
134 char grbuf[grbuf_size];
136 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
139 status = getgrnam_r(grpname, &sg, grbuf, sizeof(grbuf), &g);
142 WARNING("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
143 sstrerror(status, errbuf, sizeof(errbuf)));
147 WARNING("unixsock plugin: No such group: `%s'", grpname);
151 if (chown((sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (uid_t)-1,
153 WARNING("unixsock plugin: chown (%s, -1, %i) failed: %s",
154 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (int)g->gr_gid,
160 } /* int us_open_socket */
162 static void *us_handle_client(void *arg) {
167 fdin = *((int *)arg);
171 DEBUG("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
175 ERROR("unixsock plugin: dup failed: %s", STRERRNO);
177 pthread_exit((void *)1);
180 fhin = fdopen(fdin, "r");
182 ERROR("unixsock plugin: fdopen failed: %s", STRERRNO);
185 pthread_exit((void *)1);
189 fhout = fdopen(fdout, "w");
191 ERROR("unixsock plugin: fdopen failed: %s", STRERRNO);
192 fclose(fhin); /* this closes fdin as well */
194 pthread_exit((void *)1);
198 /* change output buffer to line buffered mode */
199 if (setvbuf(fhout, NULL, _IOLBF, 0) != 0) {
200 ERROR("unixsock plugin: setvbuf failed: %s", STRERRNO);
203 pthread_exit((void *)1);
209 char buffer_copy[1024];
215 if (fgets(buffer, sizeof(buffer), fhin) == NULL) {
216 if ((errno == EINTR) || (errno == EAGAIN))
220 WARNING("unixsock plugin: failed to read from socket #%i: %s",
221 fileno(fhin), STRERRNO);
226 len = strlen(buffer);
228 ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
229 buffer[--len] = '\0';
234 sstrncpy(buffer_copy, buffer, sizeof(buffer_copy));
237 strsplit(buffer_copy, fields, sizeof(fields) / sizeof(fields[0]));
238 if (fields_num < 1) {
239 fprintf(fhout, "-1 Internal error\n");
242 pthread_exit((void *)1);
246 if (strcasecmp(fields[0], "getval") == 0) {
247 cmd_handle_getval(fhout, buffer);
248 } else if (strcasecmp(fields[0], "getthreshold") == 0) {
249 handle_getthreshold(fhout, buffer);
250 } else if (strcasecmp(fields[0], "putval") == 0) {
251 cmd_handle_putval(fhout, buffer);
252 } else if (strcasecmp(fields[0], "listval") == 0) {
253 cmd_handle_listval(fhout, buffer);
254 } else if (strcasecmp(fields[0], "putnotif") == 0) {
255 handle_putnotif(fhout, buffer);
256 } else if (strcasecmp(fields[0], "flush") == 0) {
257 cmd_handle_flush(fhout, buffer);
259 if (fprintf(fhout, "-1 Unknown command: %s\n", fields[0]) < 0) {
260 WARNING("unixsock plugin: failed to write to socket #%i: %s",
261 fileno(fhout), STRERRNO);
265 } /* while (fgets) */
267 DEBUG("unixsock plugin: us_handle_client: Exiting..");
271 pthread_exit((void *)0);
273 } /* void *us_handle_client */
275 static void *us_server_thread(void __attribute__((unused)) * arg) {
279 pthread_attr_t th_attr;
281 pthread_attr_init(&th_attr);
282 pthread_attr_setdetachstate(&th_attr, PTHREAD_CREATE_DETACHED);
284 if (us_open_socket() != 0)
285 pthread_exit((void *)1);
288 DEBUG("unixsock plugin: Calling accept..");
289 status = accept(sock_fd, NULL, NULL);
295 ERROR("unixsock plugin: accept failed: %s", STRERRNO);
298 pthread_attr_destroy(&th_attr);
299 pthread_exit((void *)1);
302 remote_fd = malloc(sizeof(*remote_fd));
303 if (remote_fd == NULL) {
304 WARNING("unixsock plugin: malloc failed: %s", STRERRNO);
310 DEBUG("Spawning child to handle connection on fd #%i", *remote_fd);
312 status = plugin_thread_create(&th, &th_attr, us_handle_client,
313 (void *)remote_fd, "unixsock conn");
315 WARNING("unixsock plugin: pthread_create failed: %s", STRERRNO);
324 pthread_attr_destroy(&th_attr);
326 status = unlink((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
328 NOTICE("unixsock plugin: unlink (%s) failed: %s",
329 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH, STRERRNO);
333 } /* void *us_server_thread */
335 static int us_config(const char *key, const char *val) {
336 if (strcasecmp(key, "SocketFile") == 0) {
337 char *new_sock_file = strdup(val);
338 if (new_sock_file == NULL)
342 sock_file = new_sock_file;
343 } else if (strcasecmp(key, "SocketGroup") == 0) {
344 char *new_sock_group = strdup(val);
345 if (new_sock_group == NULL)
349 sock_group = new_sock_group;
350 } else if (strcasecmp(key, "SocketPerms") == 0) {
351 sock_perms = (int)strtol(val, NULL, 8);
352 } else if (strcasecmp(key, "DeleteSocket") == 0) {
362 } /* int us_config */
364 static int us_init(void) {
365 static int have_init = 0;
369 /* Initialize only once. */
376 status = plugin_thread_create(&listen_thread, NULL, us_server_thread, NULL,
379 ERROR("unixsock plugin: pthread_create failed: %s", STRERRNO);
386 static int us_shutdown(void) {
391 if (listen_thread != (pthread_t)0) {
392 pthread_kill(listen_thread, SIGTERM);
393 pthread_join(listen_thread, &ret);
394 listen_thread = (pthread_t)0;
397 plugin_unregister_init("unixsock");
398 plugin_unregister_shutdown("unixsock");
401 } /* int us_shutdown */
403 void module_register(void) {
404 plugin_register_config("unixsock", us_config, config_keys, config_keys_num);
405 plugin_register_init("unixsock", us_init);
406 plugin_register_shutdown("unixsock", us_shutdown);
407 } /* void module_register (void) */