write_prometheus: Set SO_REUSEADDR on listening socket
authorPavel Rochnyack <pavel2000@ngs.ru>
Sat, 10 Feb 2018 10:46:30 +0000 (17:46 +0700)
committerPavel Rochnyack <pavel2000@ngs.ru>
Sat, 10 Feb 2018 11:13:01 +0000 (18:13 +0700)
Otherwise Collectd fails to bind the socket after restart.

Thanks to Richard Weinberger and Brandon Hume for reporting.

Closes: #2570
Closes: #2673

src/write_prometheus.c

index 97f583f..ba186a7 100644 (file)
@@ -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;