From: "Daniel P. Berrange" <berrange(a)redhat.com>
Make virSocket use the virObject APIs for reference counting
Signed-off-by: Daniel P. Berrange <berrange(a)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 336fdb7..73a1ec1 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1593,7 +1593,6 @@ virNetSocketAccept;
virNetSocketAddIOCallback;
virNetSocketClose;
virNetSocketDupFD;
-virNetSocketFree;
virNetSocketGetFD;
virNetSocketGetPort;
virNetSocketGetUNIXIdentity;
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 912ba58..57b639f 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 6d8c132..9025221 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -494,7 +494,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);
@@ -530,7 +530,7 @@ virNetClientCloseLocked(virNetClientPtr client)
if (!client->sock)
return;
- 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 3a6439d..85f7c88 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -596,7 +596,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);
@@ -667,7 +667,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 60fe89f..93c0574 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 b6bb211..b6f156b 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -51,8 +51,9 @@
struct _virNetSocket {
+ virObject object;
+
virMutex lock;
- int refs;
int fd;
int watch;
@@ -85,6 +86,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);
}
@@ -1331,7 +1322,7 @@ static void virNetSocketEventFree(void *opaque)
if (ff)
ff(eopaque);
- virNetSocketFree(sock);
+ virObjectUnref(sock);
}
int virNetSocketAddIOCallback(virNetSocketPtr sock,
@@ -1342,7 +1333,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);
@@ -1366,7 +1357,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 6c8e77c..cc3f912 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 0f7bbad..0395601 100644
--- a/tests/virnetsockettest.c
+++ b/tests/virnetsockettest.c
@@ -172,7 +172,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) {
@@ -183,16 +183,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;
}
@@ -228,7 +228,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';
@@ -242,8 +242,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;
@@ -320,9 +320,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;
@@ -352,7 +352,7 @@ static int testSocketCommandNormal(const void *data ATTRIBUTE_UNUSED)
ret = 0;
cleanup:
- virNetSocketFree(csock);
+ virObjectUnref(csock);
return ret;
}
@@ -375,7 +375,7 @@ static int testSocketCommandFail(const void *data ATTRIBUTE_UNUSED)
ret = 0;
cleanup:
- virNetSocketFree(csock);
+ virObjectUnref(csock);
return ret;
}
@@ -444,7 +444,7 @@ static int testSocketSSH(const void *opaque)
ret = 0;
cleanup:
- virNetSocketFree(csock);
+ virObjectUnref(csock);
return ret;
}
--
1.7.10.4