X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Funixsock.c;h=e62ed6c0596b5ddb29848e66805d6e5ffd036f1c;hb=1fc00932fd49fa535551cc4bba09c4e4be059ab4;hp=be36abc8c8f9bbb339c6557b701e41f4c62a61cd;hpb=79c87aeda9172e219a842e393c6499c8bf37662a;p=collectd.git diff --git a/src/unixsock.c b/src/unixsock.c index be36abc8..e62ed6c0 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -2,21 +2,26 @@ * collectd - src/unixsock.c * Copyright (C) 2007,2008 Florian octo Forster * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * - * Author: - * Florian octo Forster + * Authors: + * Florian octo Forster **/ #include "collectd.h" @@ -54,7 +59,8 @@ static const char *config_keys[] = { "SocketFile", "SocketGroup", - "SocketPerms" + "SocketPerms", + "DeleteSocket" }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); @@ -65,6 +71,7 @@ static int sock_fd = -1; static char *sock_file = NULL; static char *sock_group = NULL; static int sock_perms = S_IRWXU | S_IRWXG; +static _Bool delete_socket = 0; static pthread_t listen_thread = (pthread_t) 0; @@ -89,10 +96,27 @@ static int us_open_socket (void) sa.sun_family = AF_UNIX; sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH, sizeof (sa.sun_path)); - /* unlink (sa.sun_path); */ DEBUG ("unixsock plugin: socket path = %s", sa.sun_path); + if (delete_socket) + { + errno = 0; + status = unlink (sa.sun_path); + if ((status != 0) && (errno != ENOENT)) + { + char errbuf[1024]; + WARNING ("unixsock plugin: Deleting socket file \"%s\" failed: %s", + sa.sun_path, + sstrerror (errno, errbuf, sizeof (errbuf))); + } + else if (status == 0) + { + INFO ("unixsock plugin: Successfully deleted socket file \"%s\".", + sa.sun_path); + } + } + status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa)); if (status != 0) { @@ -104,7 +128,16 @@ static int us_open_socket (void) return (-1); } - chmod (sa.sun_path, sock_perms); + status = chmod (sa.sun_path, sock_perms); + if (status == -1) + { + char errbuf[1024]; + ERROR ("unixsock plugin: chmod failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + close (sock_fd); + sock_fd = -1; + return (-1); + } status = listen (sock_fd, 8); if (status != 0) @@ -158,33 +191,48 @@ static int us_open_socket (void) static void *us_handle_client (void *arg) { - int fd; + int fdin; + int fdout; FILE *fhin, *fhout; - fd = *((int *) arg); + fdin = *((int *) arg); free (arg); arg = NULL; - DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fd); + DEBUG ("unixsock plugin: us_handle_client: Reading from fd #%i", fdin); - fhin = fdopen (fd, "r"); + fdout = dup (fdin); + if (fdout < 0) + { + char errbuf[1024]; + ERROR ("unixsock plugin: dup failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + close (fdin); + pthread_exit ((void *) 1); + } + + fhin = fdopen (fdin, "r"); if (fhin == NULL) { char errbuf[1024]; ERROR ("unixsock plugin: fdopen failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); - close (fd); + close (fdin); + close (fdout); pthread_exit ((void *) 1); + return ((void *) 1); } - fhout = fdopen (fd, "w"); + fhout = fdopen (fdout, "w"); if (fhout == NULL) { char errbuf[1024]; ERROR ("unixsock plugin: fdopen failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); - fclose (fhin); /* this closes fd as well */ + fclose (fhin); /* this closes fdin as well */ + close (fdout); pthread_exit ((void *) 1); + return ((void *) 1); } /* change output buffer to line buffered mode */ @@ -196,6 +244,7 @@ static void *us_handle_client (void *arg) fclose (fhin); fclose (fhout); pthread_exit ((void *) 1); + return ((void *) 0); } while (42) @@ -209,6 +258,9 @@ static void *us_handle_client (void *arg) errno = 0; if (fgets (buffer, sizeof (buffer), fhin) == NULL) { + if ((errno == EINTR) || (errno == EAGAIN)) + continue; + if (errno != 0) { char errbuf[1024]; @@ -231,11 +283,13 @@ static void *us_handle_client (void *arg) fields_num = strsplit (buffer_copy, fields, sizeof (fields) / sizeof (fields[0])); - if (fields_num < 1) { - close (fd); - break; + fprintf (fhout, "-1 Internal error\n"); + fclose (fhin); + fclose (fhout); + pthread_exit ((void *) 1); + return ((void *) 1); } if (strcasecmp (fields[0], "getval") == 0) @@ -290,6 +344,9 @@ static void *us_server_thread (void __attribute__((unused)) *arg) pthread_t th; pthread_attr_t th_attr; + pthread_attr_init (&th_attr); + pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED); + if (us_open_socket () != 0) pthread_exit ((void *) 1); @@ -308,6 +365,7 @@ static void *us_server_thread (void __attribute__((unused)) *arg) sstrerror (errno, errbuf, sizeof (errbuf))); close (sock_fd); sock_fd = -1; + pthread_attr_destroy (&th_attr); pthread_exit ((void *) 1); } @@ -324,10 +382,8 @@ static void *us_server_thread (void __attribute__((unused)) *arg) DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd); - pthread_attr_init (&th_attr); - pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED); - - status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd); + status = plugin_thread_create (&th, &th_attr, + us_handle_client, (void *) remote_fd); if (status != 0) { char errbuf[1024]; @@ -341,6 +397,7 @@ static void *us_server_thread (void __attribute__((unused)) *arg) close (sock_fd); sock_fd = -1; + pthread_attr_destroy (&th_attr); status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH); if (status != 0) @@ -378,6 +435,13 @@ static int us_config (const char *key, const char *val) { sock_perms = (int) strtol (val, NULL, 8); } + else if (strcasecmp (key, "DeleteSocket") == 0) + { + if (IS_TRUE (val)) + delete_socket = 1; + else + delete_socket = 0; + } else { return (-1); @@ -399,7 +463,8 @@ static int us_init (void) loop = 1; - status = pthread_create (&listen_thread, NULL, us_server_thread, NULL); + status = plugin_thread_create (&listen_thread, NULL, + us_server_thread, NULL); if (status != 0) { char errbuf[1024];