X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Funixsock.c;h=99e39eee4f39f0a771408c8a08bf09c750857282;hb=0c854759d82a28a5b4c20ad30a97e1b1f23295bc;hp=aa3451e9bc6cf7419bb23ab1feb2ce4b4bf64fbd;hpb=2079ee1517e34de372f58e7e2267ad5c71a8a41f;p=collectd.git diff --git a/src/unixsock.c b/src/unixsock.c index aa3451e9..99e39eee 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -78,7 +78,7 @@ static int us_open_socket(void) { char errbuf[1024]; ERROR("unixsock plugin: socket failed: %s", sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + return -1; } sa.sun_family = AF_UNIX; @@ -107,7 +107,7 @@ static int us_open_socket(void) { ERROR("unixsock plugin: bind failed: %s", errbuf); close(sock_fd); sock_fd = -1; - return (-1); + return -1; } status = chmod(sa.sun_path, sock_perms); @@ -117,7 +117,7 @@ static int us_open_socket(void) { sstrerror(errno, errbuf, sizeof(errbuf))); close(sock_fd); sock_fd = -1; - return (-1); + return -1; } status = listen(sock_fd, 8); @@ -127,14 +127,20 @@ static int us_open_socket(void) { sstrerror(errno, errbuf, sizeof(errbuf))); close(sock_fd); sock_fd = -1; - return (-1); + return -1; } do { const char *grpname; struct group *g; struct group sg; - char grbuf[2048]; + + long int grbuf_size = sysconf(_SC_GETGR_R_SIZE_MAX); + if (grbuf_size <= 0) + grbuf_size = sysconf(_SC_PAGESIZE); + if (grbuf_size <= 0) + grbuf_size = 4096; + char grbuf[grbuf_size]; grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME; g = NULL; @@ -143,7 +149,7 @@ static int us_open_socket(void) { if (status != 0) { char errbuf[1024]; WARNING("unixsock plugin: getgrnam_r (%s) failed: %s", grpname, - sstrerror(errno, errbuf, sizeof(errbuf))); + sstrerror(status, errbuf, sizeof(errbuf))); break; } if (g == NULL) { @@ -160,7 +166,7 @@ static int us_open_socket(void) { } } while (0); - return (0); + return 0; } /* int us_open_socket */ static void *us_handle_client(void *arg) { @@ -191,7 +197,7 @@ static void *us_handle_client(void *arg) { close(fdin); close(fdout); pthread_exit((void *)1); - return ((void *)1); + return (void *)1; } fhout = fdopen(fdout, "w"); @@ -202,7 +208,7 @@ static void *us_handle_client(void *arg) { fclose(fhin); /* this closes fdin as well */ close(fdout); pthread_exit((void *)1); - return ((void *)1); + return (void *)1; } /* change output buffer to line buffered mode */ @@ -213,7 +219,7 @@ static void *us_handle_client(void *arg) { fclose(fhin); fclose(fhout); pthread_exit((void *)1); - return ((void *)0); + return (void *)0; } while (42) { @@ -253,21 +259,21 @@ static void *us_handle_client(void *arg) { fclose(fhin); fclose(fhout); pthread_exit((void *)1); - return ((void *)1); + return (void *)1; } if (strcasecmp(fields[0], "getval") == 0) { - handle_getval(fhout, buffer); + cmd_handle_getval(fhout, buffer); } else if (strcasecmp(fields[0], "getthreshold") == 0) { handle_getthreshold(fhout, buffer); } else if (strcasecmp(fields[0], "putval") == 0) { - handle_putval(fhout, buffer); + cmd_handle_putval(fhout, buffer); } else if (strcasecmp(fields[0], "listval") == 0) { - handle_listval(fhout, buffer); + cmd_handle_listval(fhout, buffer); } else if (strcasecmp(fields[0], "putnotif") == 0) { handle_putnotif(fhout, buffer); } else if (strcasecmp(fields[0], "flush") == 0) { - handle_flush(fhout, buffer); + cmd_handle_flush(fhout, buffer); } else { if (fprintf(fhout, "-1 Unknown command: %s\n", fields[0]) < 0) { char errbuf[1024]; @@ -283,7 +289,7 @@ static void *us_handle_client(void *arg) { fclose(fhout); pthread_exit((void *)0); - return ((void *)0); + return (void *)0; } /* void *us_handle_client */ static void *us_server_thread(void __attribute__((unused)) * arg) { @@ -328,7 +334,7 @@ static void *us_server_thread(void __attribute__((unused)) * arg) { DEBUG("Spawning child to handle connection on fd #%i", *remote_fd); status = plugin_thread_create(&th, &th_attr, us_handle_client, - (void *)remote_fd); + (void *)remote_fd, "unixsock conn"); if (status != 0) { char errbuf[1024]; WARNING("unixsock plugin: pthread_create failed: %s", @@ -351,21 +357,21 @@ static void *us_server_thread(void __attribute__((unused)) * arg) { sstrerror(errno, errbuf, sizeof(errbuf))); } - return ((void *)0); + return (void *)0; } /* void *us_server_thread */ static int us_config(const char *key, const char *val) { if (strcasecmp(key, "SocketFile") == 0) { char *new_sock_file = strdup(val); if (new_sock_file == NULL) - return (1); + return 1; sfree(sock_file); sock_file = new_sock_file; } else if (strcasecmp(key, "SocketGroup") == 0) { char *new_sock_group = strdup(val); if (new_sock_group == NULL) - return (1); + return 1; sfree(sock_group); sock_group = new_sock_group; @@ -377,10 +383,10 @@ static int us_config(const char *key, const char *val) { else delete_socket = 0; } else { - return (-1); + return -1; } - return (0); + return 0; } /* int us_config */ static int us_init(void) { @@ -390,20 +396,21 @@ static int us_init(void) { /* Initialize only once. */ if (have_init != 0) - return (0); + return 0; have_init = 1; loop = 1; - status = plugin_thread_create(&listen_thread, NULL, us_server_thread, NULL); + status = plugin_thread_create(&listen_thread, NULL, us_server_thread, NULL, + "unixsock listen"); if (status != 0) { char errbuf[1024]; ERROR("unixsock plugin: pthread_create failed: %s", sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + return -1; } - return (0); + return 0; } /* int us_init */ static int us_shutdown(void) { @@ -420,7 +427,7 @@ static int us_shutdown(void) { plugin_unregister_init("unixsock"); plugin_unregister_shutdown("unixsock"); - return (0); + return 0; } /* int us_shutdown */ void module_register(void) { @@ -428,5 +435,3 @@ void module_register(void) { plugin_register_init("unixsock", us_init); plugin_register_shutdown("unixsock", us_shutdown); } /* void module_register (void) */ - -/* vim: set sw=4 ts=4 sts=4 tw=78 : */