Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx> --- src/rpc/virnetsocket.c | 7 ++----- src/util/virportallocator.c | 5 +---- src/util/virutil.c | 13 ++++++++++--- src/util/virutil.h | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 586a0d7..2192dc8 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -278,10 +278,8 @@ int virNetSocketNewListenTCP(const char *nodename, goto error; } - if (virSetSockReuseAddr(fd) < 0) { - virReportSystemError(errno, "%s", _("Unable to enable port reuse")); + if (virSetSockReuseAddr(fd, true) < 0) goto error; - } #ifdef IPV6_V6ONLY if (runp->ai_family == PF_INET6) { @@ -493,9 +491,8 @@ int virNetSocketNewConnectTCP(const char *nodename, goto error; } - if (virSetSockReuseAddr(fd) < 0) { + if (virSetSockReuseAddr(fd, false) < 0) VIR_WARN("Unable to enable port reuse"); - } if (connect(fd, runp->ai_addr, runp->ai_addrlen) >= 0) break; diff --git a/src/util/virportallocator.c b/src/util/virportallocator.c index ff5691a..f1dade3 100644 --- a/src/util/virportallocator.c +++ b/src/util/virportallocator.c @@ -142,11 +142,8 @@ static int virPortAllocatorBindToPort(bool *used, goto cleanup; } - if (virSetSockReuseAddr(fd) < 0) { - virReportSystemError(errno, "%s", - _("Unable to set socket reuse addr flag")); + if (virSetSockReuseAddr(fd, true) < 0) goto cleanup; - } if (ipv6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&v6only, sizeof(v6only)) < 0) { diff --git a/src/util/virutil.c b/src/util/virutil.c index 04113bb..8d2f62a 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -148,7 +148,7 @@ int virSetCloseExec(int fd) } #ifdef WIN32 -int virSetSockReuseAddr(int fd ATTRIBUTE_UNUSED) +int virSetSockReuseAddr(int fd ATTRIBUTE_UNUSED, bool fatal ATTRIBUTE_UNUSED) { /* * SO_REUSEADDR on Windows is actually akin to SO_REUSEPORT @@ -163,10 +163,17 @@ int virSetSockReuseAddr(int fd ATTRIBUTE_UNUSED) return 0; } #else -int virSetSockReuseAddr(int fd) +int virSetSockReuseAddr(int fd, bool fatal) { int opt = 1; - return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + if (ret < 0 && fatal) { + virReportSystemError(errno, "%s", + _("Unable to set socket reuse addr flag")); + } + + return ret; } #endif diff --git a/src/util/virutil.h b/src/util/virutil.h index 89b7923..54f1148 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -43,7 +43,7 @@ int virSetBlocking(int fd, bool blocking) ATTRIBUTE_RETURN_CHECK; int virSetNonBlock(int fd) ATTRIBUTE_RETURN_CHECK; int virSetInherit(int fd, bool inherit) ATTRIBUTE_RETURN_CHECK; int virSetCloseExec(int fd) ATTRIBUTE_RETURN_CHECK; -int virSetSockReuseAddr(int fd) ATTRIBUTE_RETURN_CHECK; +int virSetSockReuseAddr(int fd, bool fatal) ATTRIBUTE_RETURN_CHECK; int virPipeReadUntilEOF(int outfd, int errfd, char **outbuf, char **errbuf); -- 2.1.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list