Convert to use socket wrappers. Aside from the header file
include change, this requires changing close -> closesocket
since our portability isn't trying to replace the close
function.

Reviewed-by: Pavel Hrdina <phrd...@redhat.com>
Signed-off-by: Daniel P. Berrangé <berra...@redhat.com>
---
 src/rpc/virnetsocket.c      | 34 +++++++++++++++++++++++-----------
 src/util/virportallocator.c |  8 +++-----
 2 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index 9ad7c2cc28..973827ebde 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -44,6 +44,7 @@
 # include <selinux/selinux.h>
 #endif
 
+#include "virsocket.h"
 #include "virnetsocket.h"
 #include "virutil.h"
 #include "viralloc.h"
@@ -403,7 +404,8 @@ int virNetSocketNewListenTCP(const char *nodename,
                 goto error;
             }
             bindErrno = errno;
-            VIR_FORCE_CLOSE(fd);
+            closesocket(fd);
+            fd = -1;
             runp = runp->ai_next;
             continue;
         }
@@ -454,7 +456,8 @@ int virNetSocketNewListenTCP(const char *nodename,
         virObjectUnref(socks[i]);
     VIR_FREE(socks);
     freeaddrinfo(ai);
-    VIR_FORCE_CLOSE(fd);
+    if (fd != -1)
+        closesocket(fd);
     return -1;
 }
 
@@ -521,7 +524,8 @@ int virNetSocketNewListenUNIX(const char *path,
  error:
     if (path[0] != '@')
         unlink(path);
-    VIR_FORCE_CLOSE(fd);
+    if (fd != -1)
+        closesocket(fd);
     return -1;
 }
 #else
@@ -605,7 +609,8 @@ int virNetSocketNewConnectTCP(const char *nodename,
             break;
 
         savedErrno = errno;
-        VIR_FORCE_CLOSE(fd);
+        closesocket(fd);
+        fd = -1;
         runp = runp->ai_next;
     }
 
@@ -637,7 +642,8 @@ int virNetSocketNewConnectTCP(const char *nodename,
 
  error:
     freeaddrinfo(ai);
-    VIR_FORCE_CLOSE(fd);
+    if (fd != -1)
+        closesocket(fd);
     return -1;
 }
 
@@ -758,8 +764,8 @@ int virNetSocketNewConnectUNIX(const char *path,
     VIR_FREE(lockpath);
     VIR_FREE(rundir);
 
-    if (ret < 0)
-        VIR_FORCE_CLOSE(fd);
+    if (ret < 0 && fd != -1)
+        closesocket(fd);
 
     return ret;
 }
@@ -1370,8 +1376,10 @@ void virNetSocketDispose(void *obj)
     virObjectUnref(sock->libsshSession);
 #endif
 
-    if (sock->ownsFd)
-        VIR_FORCE_CLOSE(sock->fd);
+    if (sock->ownsFd && sock->fd != -1) {
+        closesocket(sock->fd);
+        sock->fd = -1;
+    }
     VIR_FORCE_CLOSE(sock->errfd);
 
     virProcessAbort(sock->pid);
@@ -2144,7 +2152,8 @@ int virNetSocketAccept(virNetSocketPtr sock, 
virNetSocketPtr *clientsock)
     ret = 0;
 
  cleanup:
-    VIR_FORCE_CLOSE(fd);
+    if (fd != -1)
+        closesocket(fd);
     virObjectUnlock(sock);
     return ret;
 }
@@ -2264,7 +2273,10 @@ void virNetSocketClose(virNetSocketPtr sock)
 
     virObjectLock(sock);
 
-    VIR_FORCE_CLOSE(sock->fd);
+    if (sock->fd != -1) {
+        closesocket(sock->fd);
+        sock->fd = -1;
+    }
 
 #ifdef HAVE_SYS_UN_H
     /* If a server socket, then unlink UNIX path */
diff --git a/src/util/virportallocator.c b/src/util/virportallocator.c
index 494bf9107a..285b8ddc45 100644
--- a/src/util/virportallocator.c
+++ b/src/util/virportallocator.c
@@ -21,10 +21,7 @@
 
 #include <config.h>
 
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-
+#include "virsocket.h"
 #include "viralloc.h"
 #include "virbitmap.h"
 #include "virportallocator.h"
@@ -192,7 +189,8 @@ virPortAllocatorBindToPort(bool *used,
 
     ret = 0;
  cleanup:
-    VIR_FORCE_CLOSE(fd);
+    if (fd != -1)
+        closesocket(fd);
     return ret;
 }
 
-- 
2.24.1

Reply via email to