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;
63 static char *sock_group;
64 static int sock_perms = S_IRWXU | S_IRWXG;
65 static bool delete_socket;
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);
141 WARNING("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
146 WARNING("unixsock plugin: No such group: `%s'", grpname);
150 if (chown((sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (uid_t)-1,
152 WARNING("unixsock plugin: chown (%s, -1, %i) failed: %s",
153 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (int)g->gr_gid,
159 } /* int us_open_socket */
161 static void *us_handle_client(void *arg) {
166 fdin = *((int *)arg);
170 DEBUG("unixsock plugin: us_handle_client: Reading from fd #%i", fdin);
174 ERROR("unixsock plugin: dup failed: %s", STRERRNO);
176 pthread_exit((void *)1);
179 fhin = fdopen(fdin, "r");
181 ERROR("unixsock plugin: fdopen failed: %s", STRERRNO);
184 pthread_exit((void *)1);
188 fhout = fdopen(fdout, "w");
190 ERROR("unixsock plugin: fdopen failed: %s", STRERRNO);
191 fclose(fhin); /* this closes fdin as well */
193 pthread_exit((void *)1);
197 /* change output buffer to line buffered mode */
198 if (setvbuf(fhout, NULL, _IOLBF, 0) != 0) {
199 ERROR("unixsock plugin: setvbuf failed: %s", STRERRNO);
202 pthread_exit((void *)1);
208 char buffer_copy[1024];
213 if (fgets(buffer, sizeof(buffer), fhin) == NULL) {
214 if ((errno == EINTR) || (errno == EAGAIN))
218 WARNING("unixsock plugin: failed to read from socket #%i: %s",
219 fileno(fhin), STRERRNO);
224 size_t len = strlen(buffer);
226 ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
227 buffer[--len] = '\0';
232 sstrncpy(buffer_copy, buffer, sizeof(buffer_copy));
235 strsplit(buffer_copy, fields, sizeof(fields) / sizeof(fields[0]));
236 if (fields_num < 1) {
237 fprintf(fhout, "-1 Internal error\n");
240 pthread_exit((void *)1);
244 if (strcasecmp(fields[0], "getval") == 0) {
245 cmd_handle_getval(fhout, buffer);
246 } else if (strcasecmp(fields[0], "getthreshold") == 0) {
247 handle_getthreshold(fhout, buffer);
248 } else if (strcasecmp(fields[0], "putval") == 0) {
249 cmd_handle_putval(fhout, buffer);
250 } else if (strcasecmp(fields[0], "listval") == 0) {
251 cmd_handle_listval(fhout, buffer);
252 } else if (strcasecmp(fields[0], "putnotif") == 0) {
253 handle_putnotif(fhout, buffer);
254 } else if (strcasecmp(fields[0], "flush") == 0) {
255 cmd_handle_flush(fhout, buffer);
257 if (fprintf(fhout, "-1 Unknown command: %s\n", fields[0]) < 0) {
258 WARNING("unixsock plugin: failed to write to socket #%i: %s",
259 fileno(fhout), STRERRNO);
263 } /* while (fgets) */
265 DEBUG("unixsock plugin: us_handle_client: Exiting..");
269 pthread_exit((void *)0);
271 } /* void *us_handle_client */
273 static void *us_server_thread(void __attribute__((unused)) * arg) {
277 pthread_attr_t th_attr;
279 pthread_attr_init(&th_attr);
280 pthread_attr_setdetachstate(&th_attr, PTHREAD_CREATE_DETACHED);
282 if (us_open_socket() != 0)
283 pthread_exit((void *)1);
286 DEBUG("unixsock plugin: Calling accept..");
287 status = accept(sock_fd, NULL, NULL);
293 ERROR("unixsock plugin: accept failed: %s", STRERRNO);
296 pthread_attr_destroy(&th_attr);
297 pthread_exit((void *)1);
300 remote_fd = malloc(sizeof(*remote_fd));
301 if (remote_fd == NULL) {
302 WARNING("unixsock plugin: malloc failed: %s", STRERRNO);
308 DEBUG("Spawning child to handle connection on fd #%i", *remote_fd);
310 status = plugin_thread_create(&th, &th_attr, us_handle_client,
311 (void *)remote_fd, "unixsock conn");
313 WARNING("unixsock plugin: pthread_create failed: %s", STRERRNO);
322 pthread_attr_destroy(&th_attr);
324 status = unlink((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
326 NOTICE("unixsock plugin: unlink (%s) failed: %s",
327 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH, STRERRNO);
331 } /* void *us_server_thread */
333 static int us_config(const char *key, const char *val) {
334 if (strcasecmp(key, "SocketFile") == 0) {
335 char *new_sock_file = strdup(val);
336 if (new_sock_file == NULL)
340 sock_file = new_sock_file;
341 } else if (strcasecmp(key, "SocketGroup") == 0) {
342 char *new_sock_group = strdup(val);
343 if (new_sock_group == NULL)
347 sock_group = new_sock_group;
348 } else if (strcasecmp(key, "SocketPerms") == 0) {
349 sock_perms = (int)strtol(val, NULL, 8);
350 } else if (strcasecmp(key, "DeleteSocket") == 0) {
352 delete_socket = true;
354 delete_socket = false;
360 } /* int us_config */
362 static int us_init(void) {
363 static int have_init;
367 /* Initialize only once. */
374 status = plugin_thread_create(&listen_thread, NULL, us_server_thread, NULL,
377 ERROR("unixsock plugin: pthread_create failed: %s", STRERRNO);
384 static int us_shutdown(void) {
389 if (listen_thread != (pthread_t)0) {
390 pthread_kill(listen_thread, SIGTERM);
391 pthread_join(listen_thread, &ret);
392 listen_thread = (pthread_t)0;
395 plugin_unregister_init("unixsock");
396 plugin_unregister_shutdown("unixsock");
399 } /* int us_shutdown */
401 void module_register(void) {
402 plugin_register_config("unixsock", us_config, config_keys, config_keys_num);
403 plugin_register_init("unixsock", us_init);
404 plugin_register_shutdown("unixsock", us_shutdown);
405 } /* void module_register (void) */