X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Funixsock.c;h=664c0184a5d21a793e1fc529dcbe6fbdddfbcde8;hb=56d85ff7bc14d98da63dc443e25085bc5544a1ea;hp=35c77121eb5c792d8f97787cb6503b9f753cf3d5;hpb=932048e614c6602159cdfc0580f296f74af46227;p=collectd.git diff --git a/src/unixsock.c b/src/unixsock.c index 35c77121..664c0184 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) { @@ -349,7 +373,8 @@ static void *us_server_thread (void __attribute__((unused)) *arg) DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd); - 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]; @@ -401,6 +426,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); @@ -422,7 +454,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];