提交 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 = \
--name=virNetServerMDNSGroupFree \
--name=virNetServerProgramFree \
--name=virNetServerServiceFree \
--name=virNetSocketFree \
--name=virNWFilterDefFree \
--name=virNWFilterEntryFree \
--name=virNWFilterHashTableFree \
......
......@@ -1578,7 +1578,6 @@ virNetSocketAccept;
virNetSocketAddIOCallback;
virNetSocketClose;
virNetSocketDupFD;
virNetSocketFree;
virNetSocketGetFD;
virNetSocketGetPort;
virNetSocketGetUNIXIdentity;
......@@ -1597,7 +1596,6 @@ virNetSocketNewListenTCP;
virNetSocketNewListenUNIX;
virNetSocketRead;
virNetSocketRecvFD;
virNetSocketRef;
virNetSocketRemoteAddrString;
virNetSocketRemoveIOCallback;
virNetSocketSendFD;
......
......@@ -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
......
......@@ -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);
}
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册