X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_prometheus.c;h=572ba561995953adeff43f69d56308272bdc1fa3;hb=80294418289129eb75c9f07e2043adcbd02941a4;hp=97f583f3f479d54e50eb4dfc959f5657dc50e926;hpb=e21b137ec98244a6dc8da6898504ec3763a2ce13;p=collectd.git diff --git a/src/write_prometheus.c b/src/write_prometheus.c index 97f583f3..572ba561 100644 --- a/src/write_prometheus.c +++ b/src/write_prometheus.c @@ -760,10 +760,25 @@ static int prom_open_socket(int addrfamily) { int fd = -1; for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) { - fd = socket(ai->ai_family, ai->ai_socktype | SOCK_CLOEXEC, 0); + int flags = ai->ai_socktype; +#ifdef SOCK_CLOEXEC + flags |= SOCK_CLOEXEC; +#endif + + fd = socket(ai->ai_family, flags, 0); 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;