提交 410a5dac 编写于 作者: D Daniel P. Berrange

Turn virSocket into a virObject

Make virSocket use the virObject APIs for reference counting
Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
上级 2303e920
...@@ -155,7 +155,6 @@ useless_free_options = \ ...@@ -155,7 +155,6 @@ useless_free_options = \
--name=virNetServerMDNSGroupFree \ --name=virNetServerMDNSGroupFree \
--name=virNetServerProgramFree \ --name=virNetServerProgramFree \
--name=virNetServerServiceFree \ --name=virNetServerServiceFree \
--name=virNetSocketFree \
--name=virNWFilterDefFree \ --name=virNWFilterDefFree \
--name=virNWFilterEntryFree \ --name=virNWFilterEntryFree \
--name=virNWFilterHashTableFree \ --name=virNWFilterHashTableFree \
......
...@@ -1578,7 +1578,6 @@ virNetSocketAccept; ...@@ -1578,7 +1578,6 @@ virNetSocketAccept;
virNetSocketAddIOCallback; virNetSocketAddIOCallback;
virNetSocketClose; virNetSocketClose;
virNetSocketDupFD; virNetSocketDupFD;
virNetSocketFree;
virNetSocketGetFD; virNetSocketGetFD;
virNetSocketGetPort; virNetSocketGetPort;
virNetSocketGetUNIXIdentity; virNetSocketGetUNIXIdentity;
...@@ -1597,7 +1596,6 @@ virNetSocketNewListenTCP; ...@@ -1597,7 +1596,6 @@ virNetSocketNewListenTCP;
virNetSocketNewListenUNIX; virNetSocketNewListenUNIX;
virNetSocketRead; virNetSocketRead;
virNetSocketRecvFD; virNetSocketRecvFD;
virNetSocketRef;
virNetSocketRemoteAddrString; virNetSocketRemoteAddrString;
virNetSocketRemoveIOCallback; virNetSocketRemoveIOCallback;
virNetSocketSendFD; virNetSocketSendFD;
......
...@@ -25,11 +25,9 @@ provider libvirt { ...@@ -25,11 +25,9 @@ provider libvirt {
# file: src/rpc/virnetsocket.c # file: src/rpc/virnetsocket.c
# prefix: rpc # 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_send_fd(void *sock, int fd);
probe rpc_socket_recv_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 # file: src/rpc/virnetserverclient.c
......
...@@ -1805,7 +1805,7 @@ qemuMigrationConnect(struct qemud_driver *driver, ...@@ -1805,7 +1805,7 @@ qemuMigrationConnect(struct qemud_driver *driver,
goto cleanup; goto cleanup;
if (virNetSocketNewConnectTCP(host, port, &sock) == 0) { if (virNetSocketNewConnectTCP(host, port, &sock) == 0) {
spec->dest.fd.qemu = virNetSocketDupFD(sock, true); spec->dest.fd.qemu = virNetSocketDupFD(sock, true);
virNetSocketFree(sock); virObjectUnref(sock);
} }
if (virSecurityManagerClearSocketLabel(driver->securityManager, vm->def) < 0 || if (virSecurityManagerClearSocketLabel(driver->securityManager, vm->def) < 0 ||
spec->dest.fd.qemu == -1) spec->dest.fd.qemu == -1)
...@@ -2157,7 +2157,7 @@ cleanup: ...@@ -2157,7 +2157,7 @@ cleanup:
VIR_FORCE_CLOSE(spec.dest.fd.qemu); VIR_FORCE_CLOSE(spec.dest.fd.qemu);
VIR_FORCE_CLOSE(spec.dest.fd.local); VIR_FORCE_CLOSE(spec.dest.fd.local);
} else { } else {
virNetSocketFree(sock); virObjectUnref(sock);
VIR_FREE(spec.dest.unix_socket.file); VIR_FREE(spec.dest.unix_socket.file);
} }
......
...@@ -494,7 +494,7 @@ void virNetClientFree(virNetClientPtr client) ...@@ -494,7 +494,7 @@ void virNetClientFree(virNetClientPtr client)
if (client->sock) if (client->sock)
virNetSocketRemoveIOCallback(client->sock); virNetSocketRemoveIOCallback(client->sock);
virNetSocketFree(client->sock); virObjectUnref(client->sock);
virObjectUnref(client->tls); virObjectUnref(client->tls);
#if HAVE_SASL #if HAVE_SASL
virObjectUnref(client->sasl); virObjectUnref(client->sasl);
...@@ -530,7 +530,7 @@ virNetClientCloseLocked(virNetClientPtr client) ...@@ -530,7 +530,7 @@ virNetClientCloseLocked(virNetClientPtr client)
if (!client->sock) if (!client->sock)
return; return;
virNetSocketFree(client->sock); virObjectUnref(client->sock);
client->sock = NULL; client->sock = NULL;
virObjectUnref(client->tls); virObjectUnref(client->tls);
client->tls = NULL; client->tls = NULL;
......
...@@ -596,7 +596,7 @@ void virNetServerClientFree(virNetServerClientPtr client) ...@@ -596,7 +596,7 @@ void virNetServerClientFree(virNetServerClientPtr client)
virEventRemoveTimeout(client->sockTimer); virEventRemoveTimeout(client->sockTimer);
virObjectUnref(client->tls); virObjectUnref(client->tls);
virObjectUnref(client->tlsCtxt); virObjectUnref(client->tlsCtxt);
virNetSocketFree(client->sock); virObjectUnref(client->sock);
virNetServerClientUnlock(client); virNetServerClientUnlock(client);
virMutexDestroy(&client->lock); virMutexDestroy(&client->lock);
VIR_FREE(client); VIR_FREE(client);
...@@ -667,7 +667,7 @@ void virNetServerClientClose(virNetServerClientPtr client) ...@@ -667,7 +667,7 @@ void virNetServerClientClose(virNetServerClientPtr client)
} }
if (client->sock) { if (client->sock) {
virNetSocketFree(client->sock); virObjectUnref(client->sock);
client->sock = NULL; client->sock = NULL;
} }
......
...@@ -86,7 +86,7 @@ error: ...@@ -86,7 +86,7 @@ error:
virNetServerClientClose(client); virNetServerClientClose(client);
virNetServerClientFree(client); virNetServerClientFree(client);
} else { } else {
virNetSocketFree(clientsock); virObjectUnref(clientsock);
} }
} }
...@@ -258,7 +258,7 @@ void virNetServerServiceFree(virNetServerServicePtr svc) ...@@ -258,7 +258,7 @@ void virNetServerServiceFree(virNetServerServicePtr svc)
return; return;
for (i = 0 ; i < svc->nsocks ; i++) for (i = 0 ; i < svc->nsocks ; i++)
virNetSocketFree(svc->socks[i]); virObjectUnref(svc->socks[i]);
VIR_FREE(svc->socks); VIR_FREE(svc->socks);
virObjectUnref(svc->tls); virObjectUnref(svc->tls);
......
...@@ -51,8 +51,9 @@ ...@@ -51,8 +51,9 @@
struct _virNetSocket { struct _virNetSocket {
virObject object;
virMutex lock; virMutex lock;
int refs;
int fd; int fd;
int watch; int watch;
...@@ -85,6 +86,22 @@ struct _virNetSocket { ...@@ -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 #ifndef WIN32
static int virNetSocketForkDaemon(const char *binary) static int virNetSocketForkDaemon(const char *binary)
{ {
...@@ -114,6 +131,9 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr, ...@@ -114,6 +131,9 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
virNetSocketPtr sock; virNetSocketPtr sock;
int no_slow_start = 1; int no_slow_start = 1;
if (virNetSocketInitialize() < 0)
return NULL;
VIR_DEBUG("localAddr=%p remoteAddr=%p fd=%d errfd=%d pid=%lld", VIR_DEBUG("localAddr=%p remoteAddr=%p fd=%d errfd=%d pid=%lld",
localAddr, remoteAddr, localAddr, remoteAddr,
fd, errfd, (long long) pid); fd, errfd, (long long) pid);
...@@ -129,10 +149,8 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr, ...@@ -129,10 +149,8 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
return NULL; return NULL;
} }
if (VIR_ALLOC(sock) < 0) { if (!(sock = virObjectNew(virNetSocketClass)))
virReportOOMError();
return NULL; return NULL;
}
if (virMutexInit(&sock->lock) < 0) { if (virMutexInit(&sock->lock) < 0) {
virReportSystemError(errno, "%s", virReportSystemError(errno, "%s",
...@@ -140,7 +158,6 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr, ...@@ -140,7 +158,6 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
VIR_FREE(sock); VIR_FREE(sock);
return NULL; return NULL;
} }
sock->refs = 1;
if (localAddr) if (localAddr)
sock->localAddr = *localAddr; sock->localAddr = *localAddr;
...@@ -174,15 +191,15 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr, ...@@ -174,15 +191,15 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
sock->client = isClient; sock->client = isClient;
PROBE(RPC_SOCKET_NEW, PROBE(RPC_SOCKET_NEW,
"sock=%p refs=%d fd=%d errfd=%d pid=%lld localAddr=%s, remoteAddr=%s", "sock=%p fd=%d errfd=%d pid=%lld localAddr=%s, remoteAddr=%s",
sock, sock->refs, fd, errfd, (long long) pid, sock, fd, errfd, (long long) pid,
NULLSTR(sock->localAddrStr), NULLSTR(sock->remoteAddrStr)); NULLSTR(sock->localAddrStr), NULLSTR(sock->remoteAddrStr));
return sock; return sock;
error: error:
sock->fd = sock->errfd = -1; /* Caller owns fd/errfd on failure */ sock->fd = sock->errfd = -1; /* Caller owns fd/errfd on failure */
virNetSocketFree(sock); virObjectUnref(sock);
return NULL; return NULL;
} }
...@@ -296,7 +313,7 @@ int virNetSocketNewListenTCP(const char *nodename, ...@@ -296,7 +313,7 @@ int virNetSocketNewListenTCP(const char *nodename,
error: error:
for (i = 0 ; i < nsocks ; i++) for (i = 0 ; i < nsocks ; i++)
virNetSocketFree(socks[i]); virObjectUnref(socks[i]);
VIR_FREE(socks); VIR_FREE(socks);
freeaddrinfo(ai); freeaddrinfo(ai);
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
...@@ -704,32 +721,9 @@ int virNetSocketNewConnectExternal(const char **cmdargv, ...@@ -704,32 +721,9 @@ int virNetSocketNewConnectExternal(const char **cmdargv,
} }
void virNetSocketRef(virNetSocketPtr sock) void virNetSocketDispose(void *obj)
{
virMutexLock(&sock->lock);
sock->refs++;
PROBE(RPC_SOCKET_REF,
"sock=%p refs=%d",
sock, sock->refs);
virMutexUnlock(&sock->lock);
}
void virNetSocketFree(virNetSocketPtr sock)
{ {
if (!sock) virNetSocketPtr sock = obj;
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;
}
VIR_DEBUG("sock=%p fd=%d", sock, sock->fd); VIR_DEBUG("sock=%p fd=%d", sock, sock->fd);
if (sock->watch > 0) { if (sock->watch > 0) {
...@@ -761,10 +755,7 @@ void virNetSocketFree(virNetSocketPtr sock) ...@@ -761,10 +755,7 @@ void virNetSocketFree(virNetSocketPtr sock)
VIR_FREE(sock->localAddrStr); VIR_FREE(sock->localAddrStr);
VIR_FREE(sock->remoteAddrStr); VIR_FREE(sock->remoteAddrStr);
virMutexUnlock(&sock->lock);
virMutexDestroy(&sock->lock); virMutexDestroy(&sock->lock);
VIR_FREE(sock);
} }
...@@ -1331,7 +1322,7 @@ static void virNetSocketEventFree(void *opaque) ...@@ -1331,7 +1322,7 @@ static void virNetSocketEventFree(void *opaque)
if (ff) if (ff)
ff(eopaque); ff(eopaque);
virNetSocketFree(sock); virObjectUnref(sock);
} }
int virNetSocketAddIOCallback(virNetSocketPtr sock, int virNetSocketAddIOCallback(virNetSocketPtr sock,
...@@ -1342,7 +1333,7 @@ int virNetSocketAddIOCallback(virNetSocketPtr sock, ...@@ -1342,7 +1333,7 @@ int virNetSocketAddIOCallback(virNetSocketPtr sock,
{ {
int ret = -1; int ret = -1;
virNetSocketRef(sock); virObjectRef(sock);
virMutexLock(&sock->lock); virMutexLock(&sock->lock);
if (sock->watch > 0) { if (sock->watch > 0) {
VIR_DEBUG("Watch already registered on socket %p", sock); VIR_DEBUG("Watch already registered on socket %p", sock);
...@@ -1366,7 +1357,7 @@ int virNetSocketAddIOCallback(virNetSocketPtr sock, ...@@ -1366,7 +1357,7 @@ int virNetSocketAddIOCallback(virNetSocketPtr sock,
cleanup: cleanup:
virMutexUnlock(&sock->lock); virMutexUnlock(&sock->lock);
if (ret != 0) if (ret != 0)
virNetSocketFree(sock); virObjectUnref(sock);
return ret; return ret;
} }
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
# include "virsocketaddr.h" # include "virsocketaddr.h"
# include "command.h" # include "command.h"
# include "virnettlscontext.h" # include "virnettlscontext.h"
# include "virobject.h"
# ifdef HAVE_SASL # ifdef HAVE_SASL
# include "virnetsaslcontext.h" # include "virnetsaslcontext.h"
# endif # endif
...@@ -108,8 +109,6 @@ void virNetSocketSetSASLSession(virNetSocketPtr sock, ...@@ -108,8 +109,6 @@ void virNetSocketSetSASLSession(virNetSocketPtr sock,
# endif # endif
bool virNetSocketHasCachedData(virNetSocketPtr sock); bool virNetSocketHasCachedData(virNetSocketPtr sock);
bool virNetSocketHasPendingData(virNetSocketPtr sock); bool virNetSocketHasPendingData(virNetSocketPtr sock);
void virNetSocketRef(virNetSocketPtr sock);
void virNetSocketFree(virNetSocketPtr sock);
const char *virNetSocketLocalAddrString(virNetSocketPtr sock); const char *virNetSocketLocalAddrString(virNetSocketPtr sock);
const char *virNetSocketRemoteAddrString(virNetSocketPtr sock); const char *virNetSocketRemoteAddrString(virNetSocketPtr sock);
......
...@@ -172,7 +172,7 @@ static int testSocketTCPAccept(const void *opaque) ...@@ -172,7 +172,7 @@ static int testSocketTCPAccept(const void *opaque)
if (virNetSocketNewConnectTCP(data->cnode, portstr, &csock) < 0) if (virNetSocketNewConnectTCP(data->cnode, portstr, &csock) < 0)
goto cleanup; goto cleanup;
virNetSocketFree(csock); virObjectUnref(csock);
for (i = 0 ; i < nlsock ; i++) { for (i = 0 ; i < nlsock ; i++) {
if (virNetSocketAccept(lsock[i], &ssock) != -1 && ssock) { if (virNetSocketAccept(lsock[i], &ssock) != -1 && ssock) {
...@@ -183,16 +183,16 @@ static int testSocketTCPAccept(const void *opaque) ...@@ -183,16 +183,16 @@ static int testSocketTCPAccept(const void *opaque)
goto cleanup; goto cleanup;
} }
} }
virNetSocketFree(ssock); virObjectUnref(ssock);
ssock = NULL; ssock = NULL;
} }
ret = 0; ret = 0;
cleanup: cleanup:
virNetSocketFree(ssock); virObjectUnref(ssock);
for (i = 0 ; i < nlsock ; i++) for (i = 0 ; i < nlsock ; i++)
virNetSocketFree(lsock[i]); virObjectUnref(lsock[i]);
VIR_FREE(lsock); VIR_FREE(lsock);
return ret; return ret;
} }
...@@ -228,7 +228,7 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED) ...@@ -228,7 +228,7 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED)
if (virNetSocketNewConnectUNIX(path, false, NULL, &csock) < 0) if (virNetSocketNewConnectUNIX(path, false, NULL, &csock) < 0)
goto cleanup; goto cleanup;
virNetSocketFree(csock); virObjectUnref(csock);
if (virNetSocketAccept(lsock, &ssock) != -1) { if (virNetSocketAccept(lsock, &ssock) != -1) {
char c = 'a'; char c = 'a';
...@@ -242,8 +242,8 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED) ...@@ -242,8 +242,8 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED)
cleanup: cleanup:
VIR_FREE(path); VIR_FREE(path);
virNetSocketFree(lsock); virObjectUnref(lsock);
virNetSocketFree(ssock); virObjectUnref(ssock);
if (tmpdir) if (tmpdir)
rmdir(tmpdir); rmdir(tmpdir);
return ret; return ret;
...@@ -320,9 +320,9 @@ static int testSocketUNIXAddrs(const void *data ATTRIBUTE_UNUSED) ...@@ -320,9 +320,9 @@ static int testSocketUNIXAddrs(const void *data ATTRIBUTE_UNUSED)
cleanup: cleanup:
VIR_FREE(path); VIR_FREE(path);
virNetSocketFree(lsock); virObjectUnref(lsock);
virNetSocketFree(ssock); virObjectUnref(ssock);
virNetSocketFree(csock); virObjectUnref(csock);
if (tmpdir) if (tmpdir)
rmdir(tmpdir); rmdir(tmpdir);
return ret; return ret;
...@@ -352,7 +352,7 @@ static int testSocketCommandNormal(const void *data ATTRIBUTE_UNUSED) ...@@ -352,7 +352,7 @@ static int testSocketCommandNormal(const void *data ATTRIBUTE_UNUSED)
ret = 0; ret = 0;
cleanup: cleanup:
virNetSocketFree(csock); virObjectUnref(csock);
return ret; return ret;
} }
...@@ -375,7 +375,7 @@ static int testSocketCommandFail(const void *data ATTRIBUTE_UNUSED) ...@@ -375,7 +375,7 @@ static int testSocketCommandFail(const void *data ATTRIBUTE_UNUSED)
ret = 0; ret = 0;
cleanup: cleanup:
virNetSocketFree(csock); virObjectUnref(csock);
return ret; return ret;
} }
...@@ -444,7 +444,7 @@ static int testSocketSSH(const void *opaque) ...@@ -444,7 +444,7 @@ static int testSocketSSH(const void *opaque)
ret = 0; ret = 0;
cleanup: cleanup:
virNetSocketFree(csock); virObjectUnref(csock);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册