From: Pavel Rochnyack Date: Sat, 10 Feb 2018 10:46:30 +0000 (+0700) Subject: write_prometheus: Set SO_REUSEADDR on listening socket X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=2c78bdf94c0fdd6240d5d202cf5aba12ac8c5a20;p=collectd.git write_prometheus: Set SO_REUSEADDR on listening socket Otherwise Collectd fails to bind the socket after restart. Thanks to Richard Weinberger and Brandon Hume for reporting. Closes: #2570 Closes: #2673 --- diff --git a/src/write_prometheus.c b/src/write_prometheus.c index 97f583f3..ba186a7e 100644 --- a/src/write_prometheus.c +++ b/src/write_prometheus.c @@ -764,6 +764,16 @@ static int prom_open_socket(int addrfamily) { if (fd == -1) continue; + int tmp = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp)) != 0) { + char errbuf[1024]; + WARNING("write_prometheus: setsockopt(SO_REUSEADDR) failed: %s", + sstrerror(errno, errbuf, sizeof(errbuf))); + close(fd); + fd = -1; + continue; + } + if (bind(fd, ai->ai_addr, ai->ai_addrlen) != 0) { close(fd); fd = -1;