From: "Daniel P. Berrange" <berrange(a)redhat.com>
Instead of directly manipulating sock->refs, add a virSocketRef
API
* src/rpc/virnetsocket.c, src/rpc/virnetsocket.h: Add virSocketRef
---
src/rpc/virnetsocket.c | 13 +++++++++++--
src/rpc/virnetsocket.h | 1 +
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index 88dc2a4..93e062a 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -655,6 +655,14 @@ int virNetSocketNewConnectExternal(const char **cmdargv,
}
+void virNetSocketRef(virNetSocketPtr sock)
+{
+ virMutexLock(&sock->lock);
+ sock->refs++;
+ virMutexUnlock(&sock->lock);
+}
+
+
void virNetSocketFree(virNetSocketPtr sock)
{
if (!sock)
@@ -1187,20 +1195,19 @@ int virNetSocketAddIOCallback(virNetSocketPtr sock,
{
int ret = -1;
+ virNetSocketRef(sock);
virMutexLock(&sock->lock);
if (sock->watch > 0) {
VIR_DEBUG("Watch already registered on socket %p", sock);
goto cleanup;
}
- sock->refs++;
if ((sock->watch = virEventAddHandle(sock->fd,
events,
virNetSocketEventHandle,
sock,
virNetSocketEventFree)) < 0) {
VIR_DEBUG("Failed to register watch on socket %p", sock);
- sock->refs--;
goto cleanup;
}
sock->func = func;
@@ -1211,6 +1218,8 @@ int virNetSocketAddIOCallback(virNetSocketPtr sock,
cleanup:
virMutexUnlock(&sock->lock);
+ if (ret != 0)
+ virNetSocketFree(sock);
return ret;
}
diff --git a/src/rpc/virnetsocket.h b/src/rpc/virnetsocket.h
index f7e5ebb..9c4f112 100644
--- a/src/rpc/virnetsocket.h
+++ b/src/rpc/virnetsocket.h
@@ -101,6 +101,7 @@ void virNetSocketSetSASLSession(virNetSocketPtr sock,
virNetSASLSessionPtr sess);
# endif
bool virNetSocketHasCachedData(virNetSocketPtr sock);
+void virNetSocketRef(virNetSocketPtr sock);
void virNetSocketFree(virNetSocketPtr sock);
const char *virNetSocketLocalAddrString(virNetSocketPtr sock);
--
1.7.6.4