[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 11/13] Turn virSocket into a virObject



From: "Daniel P. Berrange" <berrange redhat com>

Make virSocket use the virObject APIs for reference counting

Signed-off-by: Daniel P. Berrange <berrange redhat com>
---
 src/libvirt_private.syms      |    1 -
 src/libvirt_probes.d          |    4 +--
 src/qemu/qemu_migration.c     |    4 +--
 src/rpc/virnetclient.c        |    4 +--
 src/rpc/virnetserverclient.c  |    4 +--
 src/rpc/virnetserverservice.c |    4 +--
 src/rpc/virnetsocket.c        |   71 ++++++++++++++++++-----------------------
 src/rpc/virnetsocket.h        |    3 +-
 tests/virnetsockettest.c      |   26 +++++++--------
 9 files changed, 54 insertions(+), 67 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 035658e..ee2f581 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1459,7 +1459,6 @@ virNetServerProgramSendStreamError;
 # virnetsocket.h
 virNetSocketAccept;
 virNetSocketDupFD;
-virNetSocketFree;
 virNetSocketGetFD;
 virNetSocketHasPassFD;
 virNetSocketIsLocal;
diff --git a/src/libvirt_probes.d b/src/libvirt_probes.d
index 807239f..be1d938 100644
--- a/src/libvirt_probes.d
+++ b/src/libvirt_probes.d
@@ -25,11 +25,9 @@ provider libvirt {
 
 	# file: src/rpc/virnetsocket.c
 	# prefix: rpc
-	probe rpc_socket_new(void *sock, int refs, int fd, int errfd, pid_t pid, const char *localAddr, const char *remoteAddr);
+	probe rpc_socket_new(void *sock, int fd, int errfd, pid_t pid, const char *localAddr, const char *remoteAddr);
 	probe rpc_socket_send_fd(void *sock, int fd);
 	probe rpc_socket_recv_fd(void *sock, int fd);
-	probe rpc_socket_ref(void *sock, int refs);
-	probe rpc_socket_free(void *sock, int refs);
 
 
 	# file: src/rpc/virnetserverclient.c
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 61d3d68..8409bc1 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -1805,7 +1805,7 @@ qemuMigrationConnect(struct qemud_driver *driver,
         goto cleanup;
     if (virNetSocketNewConnectTCP(host, port, &sock) == 0) {
         spec->dest.fd.qemu = virNetSocketDupFD(sock, true);
-        virNetSocketFree(sock);
+        virObjectUnref(sock);
     }
     if (virSecurityManagerClearSocketLabel(driver->securityManager, vm->def) < 0 ||
         spec->dest.fd.qemu == -1)
@@ -2157,7 +2157,7 @@ cleanup:
         VIR_FORCE_CLOSE(spec.dest.fd.qemu);
         VIR_FORCE_CLOSE(spec.dest.fd.local);
     } else {
-        virNetSocketFree(sock);
+        virObjectUnref(sock);
         VIR_FREE(spec.dest.unix_socket.file);
     }
 
diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index 45f3309..4da5082 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -474,7 +474,7 @@ void virNetClientFree(virNetClientPtr client)
 
     if (client->sock)
         virNetSocketRemoveIOCallback(client->sock);
-    virNetSocketFree(client->sock);
+    virObjectUnref(client->sock);
     virObjectUnref(client->tls);
 #if HAVE_SASL
     virObjectUnref(client->sasl);
@@ -497,7 +497,7 @@ virNetClientCloseLocked(virNetClientPtr client)
         return;
 
     virNetSocketRemoveIOCallback(client->sock);
-    virNetSocketFree(client->sock);
+    virObjectUnref(client->sock);
     client->sock = NULL;
     virObjectUnref(client->tls);
     client->tls = NULL;
diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index b2ab12f..58b56e1 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -598,7 +598,7 @@ void virNetServerClientFree(virNetServerClientPtr client)
         virEventRemoveTimeout(client->sockTimer);
     virObjectUnref(client->tls);
     virObjectUnref(client->tlsCtxt);
-    virNetSocketFree(client->sock);
+    virObjectUnref(client->sock);
     virNetServerClientUnlock(client);
     virMutexDestroy(&client->lock);
     VIR_FREE(client);
@@ -669,7 +669,7 @@ void virNetServerClientClose(virNetServerClientPtr client)
     }
 
     if (client->sock) {
-        virNetSocketFree(client->sock);
+        virObjectUnref(client->sock);
         client->sock = NULL;
     }
 
diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c
index b4689b4..46cef3a 100644
--- a/src/rpc/virnetserverservice.c
+++ b/src/rpc/virnetserverservice.c
@@ -86,7 +86,7 @@ error:
         virNetServerClientClose(client);
         virNetServerClientFree(client);
     } else {
-        virNetSocketFree(clientsock);
+        virObjectUnref(clientsock);
     }
 }
 
@@ -258,7 +258,7 @@ void virNetServerServiceFree(virNetServerServicePtr svc)
         return;
 
     for (i = 0 ; i < svc->nsocks ; i++)
-        virNetSocketFree(svc->socks[i]);
+        virObjectUnref(svc->socks[i]);
     VIR_FREE(svc->socks);
 
     virObjectUnref(svc->tls);
diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index 1a64cc0..93ff9dd 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -54,8 +54,9 @@
 
 
 struct _virNetSocket {
+    virObject object;
+
     virMutex lock;
-    int refs;
 
     int fd;
     int watch;
@@ -88,6 +89,22 @@ struct _virNetSocket {
 };
 
 
+static virClassPtr virNetSocketClass;
+static void virNetSocketDispose(void *obj);
+
+static int virNetSocketOnceInit(void)
+{
+    if (!(virNetSocketClass = virClassNew("virNetSocket",
+                                          sizeof(virNetSocket),
+                                          virNetSocketDispose)))
+        return -1;
+
+    return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virNetSocket)
+
+
 #ifndef WIN32
 static int virNetSocketForkDaemon(const char *binary)
 {
@@ -114,6 +131,9 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
     virNetSocketPtr sock;
     int no_slow_start = 1;
 
+    if (virNetSocketInitialize() < 0)
+        return NULL;
+
     VIR_DEBUG("localAddr=%p remoteAddr=%p fd=%d errfd=%d pid=%lld",
               localAddr, remoteAddr,
               fd, errfd, (long long) pid);
@@ -129,10 +149,8 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
         return NULL;
     }
 
-    if (VIR_ALLOC(sock) < 0) {
-        virReportOOMError();
+    if (!(sock = virObjectNew(virNetSocketClass)))
         return NULL;
-    }
 
     if (virMutexInit(&sock->lock) < 0) {
         virReportSystemError(errno, "%s",
@@ -140,7 +158,6 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
         VIR_FREE(sock);
         return NULL;
     }
-    sock->refs = 1;
 
     if (localAddr)
         sock->localAddr = *localAddr;
@@ -174,15 +191,15 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
     sock->client = isClient;
 
     PROBE(RPC_SOCKET_NEW,
-          "sock=%p refs=%d fd=%d errfd=%d pid=%lld localAddr=%s, remoteAddr=%s",
-          sock, sock->refs, fd, errfd, (long long) pid,
+          "sock=%p fd=%d errfd=%d pid=%lld localAddr=%s, remoteAddr=%s",
+          sock, fd, errfd, (long long) pid,
           NULLSTR(sock->localAddrStr), NULLSTR(sock->remoteAddrStr));
 
     return sock;
 
 error:
     sock->fd = sock->errfd = -1; /* Caller owns fd/errfd on failure */
-    virNetSocketFree(sock);
+    virObjectUnref(sock);
     return NULL;
 }
 
@@ -296,7 +313,7 @@ int virNetSocketNewListenTCP(const char *nodename,
 
 error:
     for (i = 0 ; i < nsocks ; i++)
-        virNetSocketFree(socks[i]);
+        virObjectUnref(socks[i]);
     VIR_FREE(socks);
     freeaddrinfo(ai);
     VIR_FORCE_CLOSE(fd);
@@ -704,32 +721,9 @@ int virNetSocketNewConnectExternal(const char **cmdargv,
 }
 
 
-void virNetSocketRef(virNetSocketPtr sock)
-{
-    virMutexLock(&sock->lock);
-    sock->refs++;
-    PROBE(RPC_SOCKET_REF,
-          "sock=%p refs=%d",
-          sock, sock->refs);
-    virMutexUnlock(&sock->lock);
-}
-
-
-void virNetSocketFree(virNetSocketPtr sock)
+void virNetSocketDispose(void *obj)
 {
-    if (!sock)
-        return;
-
-    virMutexLock(&sock->lock);
-    PROBE(RPC_SOCKET_FREE,
-          "sock=%p refs=%d",
-          sock, sock->refs);
-
-    sock->refs--;
-    if (sock->refs > 0) {
-        virMutexUnlock(&sock->lock);
-        return;
-    }
+    virNetSocketPtr sock = obj;
 
     VIR_DEBUG("sock=%p fd=%d", sock, sock->fd);
     if (sock->watch > 0) {
@@ -761,10 +755,7 @@ void virNetSocketFree(virNetSocketPtr sock)
     VIR_FREE(sock->localAddrStr);
     VIR_FREE(sock->remoteAddrStr);
 
-    virMutexUnlock(&sock->lock);
     virMutexDestroy(&sock->lock);
-
-    VIR_FREE(sock);
 }
 
 
@@ -1330,7 +1321,7 @@ static void virNetSocketEventFree(void *opaque)
     if (ff)
         ff(eopaque);
 
-    virNetSocketFree(sock);
+    virObjectUnref(sock);
 }
 
 int virNetSocketAddIOCallback(virNetSocketPtr sock,
@@ -1341,7 +1332,7 @@ int virNetSocketAddIOCallback(virNetSocketPtr sock,
 {
     int ret = -1;
 
-    virNetSocketRef(sock);
+    virObjectRef(sock);
     virMutexLock(&sock->lock);
     if (sock->watch > 0) {
         VIR_DEBUG("Watch already registered on socket %p", sock);
@@ -1365,7 +1356,7 @@ int virNetSocketAddIOCallback(virNetSocketPtr sock,
 cleanup:
     virMutexUnlock(&sock->lock);
     if (ret != 0)
-        virNetSocketFree(sock);
+        virObjectUnref(sock);
     return ret;
 }
 
diff --git a/src/rpc/virnetsocket.h b/src/rpc/virnetsocket.h
index 5ba7c8f..a42ee3a 100644
--- a/src/rpc/virnetsocket.h
+++ b/src/rpc/virnetsocket.h
@@ -27,6 +27,7 @@
 # include "virsocketaddr.h"
 # include "command.h"
 # include "virnettlscontext.h"
+# include "virobject.h"
 # ifdef HAVE_SASL
 #  include "virnetsaslcontext.h"
 # endif
@@ -108,8 +109,6 @@ void virNetSocketSetSASLSession(virNetSocketPtr sock,
 # endif
 bool virNetSocketHasCachedData(virNetSocketPtr sock);
 bool virNetSocketHasPendingData(virNetSocketPtr sock);
-void virNetSocketRef(virNetSocketPtr sock);
-void virNetSocketFree(virNetSocketPtr sock);
 
 const char *virNetSocketLocalAddrString(virNetSocketPtr sock);
 const char *virNetSocketRemoteAddrString(virNetSocketPtr sock);
diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c
index 204113e..080aa9f 100644
--- a/tests/virnetsockettest.c
+++ b/tests/virnetsockettest.c
@@ -167,7 +167,7 @@ static int testSocketTCPAccept(const void *opaque)
     if (virNetSocketNewConnectTCP(data->cnode, portstr, &csock) < 0)
         goto cleanup;
 
-    virNetSocketFree(csock);
+    virObjectUnref(csock);
 
     for (i = 0 ; i < nlsock ; i++) {
         if (virNetSocketAccept(lsock[i], &ssock) != -1 && ssock) {
@@ -178,16 +178,16 @@ static int testSocketTCPAccept(const void *opaque)
                 goto cleanup;
             }
         }
-        virNetSocketFree(ssock);
+        virObjectUnref(ssock);
         ssock = NULL;
     }
 
     ret = 0;
 
 cleanup:
-    virNetSocketFree(ssock);
+    virObjectUnref(ssock);
     for (i = 0 ; i < nlsock ; i++)
-        virNetSocketFree(lsock[i]);
+        virObjectUnref(lsock[i]);
     VIR_FREE(lsock);
     return ret;
 }
@@ -223,7 +223,7 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED)
     if (virNetSocketNewConnectUNIX(path, false, NULL, &csock) < 0)
         goto cleanup;
 
-    virNetSocketFree(csock);
+    virObjectUnref(csock);
 
     if (virNetSocketAccept(lsock, &ssock) != -1) {
         char c = 'a';
@@ -237,8 +237,8 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED)
 
 cleanup:
     VIR_FREE(path);
-    virNetSocketFree(lsock);
-    virNetSocketFree(ssock);
+    virObjectUnref(lsock);
+    virObjectUnref(ssock);
     if (tmpdir)
         rmdir(tmpdir);
     return ret;
@@ -315,9 +315,9 @@ static int testSocketUNIXAddrs(const void *data ATTRIBUTE_UNUSED)
 
 cleanup:
     VIR_FREE(path);
-    virNetSocketFree(lsock);
-    virNetSocketFree(ssock);
-    virNetSocketFree(csock);
+    virObjectUnref(lsock);
+    virObjectUnref(ssock);
+    virObjectUnref(csock);
     if (tmpdir)
         rmdir(tmpdir);
     return ret;
@@ -347,7 +347,7 @@ static int testSocketCommandNormal(const void *data ATTRIBUTE_UNUSED)
     ret = 0;
 
 cleanup:
-    virNetSocketFree(csock);
+    virObjectUnref(csock);
     return ret;
 }
 
@@ -370,7 +370,7 @@ static int testSocketCommandFail(const void *data ATTRIBUTE_UNUSED)
     ret = 0;
 
 cleanup:
-    virNetSocketFree(csock);
+    virObjectUnref(csock);
     return ret;
 }
 
@@ -439,7 +439,7 @@ static int testSocketSSH(const void *opaque)
     ret = 0;
 
 cleanup:
-    virNetSocketFree(csock);
+    virObjectUnref(csock);
     return ret;
 }
 
-- 
1.7.10.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]