From: "Daniel P. Berrange" <berrange(a)redhat.com>
Make virNetSASLContext and virNetSASLSession use virObject APIs
for reference counting
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
daemon/remote.c | 8 ++--
src/remote/remote_driver.c | 4 +-
src/rpc/virnetclient.c | 7 ++-
src/rpc/virnetsaslcontext.c | 106 ++++++++++++++++++------------------------
src/rpc/virnetsaslcontext.h | 8 +---
src/rpc/virnetserverclient.c | 7 ++-
src/rpc/virnetsocket.c | 7 ++-
7 files changed, 61 insertions(+), 86 deletions(-)
diff --git a/daemon/remote.c b/daemon/remote.c
index 095d854..24741d2 100644
--- a/daemon/remote.c
+++ b/daemon/remote.c
@@ -2301,7 +2301,7 @@ authfail:
PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
"client=%p auth=%d",
client, REMOTE_AUTH_SASL);
- virNetSASLSessionFree(sasl);
+ virObjectUnref(sasl);
virMutexUnlock(&priv->lock);
return -1;
}
@@ -2345,7 +2345,7 @@ remoteSASLFinish(virNetServerClientPtr client)
"client=%p auth=%d identity=%s",
client, REMOTE_AUTH_SASL, identity);
- virNetSASLSessionFree(priv->sasl);
+ virObjectUnref(priv->sasl);
priv->sasl = NULL;
return 0;
@@ -2443,7 +2443,7 @@ authdeny:
goto error;
error:
- virNetSASLSessionFree(priv->sasl);
+ virObjectUnref(priv->sasl);
priv->sasl = NULL;
virResetLastError();
virNetError(VIR_ERR_AUTH_FAILED, "%s",
@@ -2541,7 +2541,7 @@ authdeny:
goto error;
error:
- virNetSASLSessionFree(priv->sasl);
+ virObjectUnref(priv->sasl);
priv->sasl = NULL;
virResetLastError();
virNetError(VIR_ERR_AUTH_FAILED, "%s",
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 28035de..f2600a8 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -3374,8 +3374,8 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv,
remoteAuthInteractStateClear(&state, true);
VIR_FREE(saslcb);
- virNetSASLSessionFree(sasl);
- virNetSASLContextFree(saslCtxt);
+ virObjectUnref(sasl);
+ virObjectUnref(saslCtxt);
return ret;
}
diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index 2b51246..8dd09ef 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -477,7 +477,7 @@ void virNetClientFree(virNetClientPtr client)
virNetSocketFree(client->sock);
virObjectUnref(client->tls);
#if HAVE_SASL
- virNetSASLSessionFree(client->sasl);
+ virObjectUnref(client->sasl);
#endif
virNetClientUnlock(client);
virMutexDestroy(&client->lock);
@@ -502,7 +502,7 @@ virNetClientCloseLocked(virNetClientPtr client)
virObjectUnref(client->tls);
client->tls = NULL;
#if HAVE_SASL
- virNetSASLSessionFree(client->sasl);
+ virObjectUnref(client->sasl);
client->sasl = NULL;
#endif
ka = client->keepalive;
@@ -556,8 +556,7 @@ void virNetClientSetSASLSession(virNetClientPtr client,
virNetSASLSessionPtr sasl)
{
virNetClientLock(client);
- client->sasl = sasl;
- virNetSASLSessionRef(sasl);
+ client->sasl = virObjectRef(sasl);
virNetSocketSetSASLSession(client->sock, client->sasl);
virNetClientUnlock(client);
}
diff --git a/src/rpc/virnetsaslcontext.c b/src/rpc/virnetsaslcontext.c
index 8db0e15..f910d4c 100644
--- a/src/rpc/virnetsaslcontext.c
+++ b/src/rpc/virnetsaslcontext.c
@@ -37,24 +37,52 @@
struct _virNetSASLContext {
+ virObject object;
+
virMutex lock;
const char *const*usernameWhitelist;
- int refs;
};
struct _virNetSASLSession {
+ virObject object;
+
virMutex lock;
sasl_conn_t *conn;
- int refs;
size_t maxbufsize;
};
+static virClassPtr virNetSASLContextClass;
+static virClassPtr virNetSASLSessionClass;
+static void virNetSASLContextDispose(void *obj);
+static void virNetSASLSessionDispose(void *obj);
+
+static int virNetSASLContextOnceInit(void)
+{
+ if (!(virNetSASLContextClass = virClassNew("virNetSASLContext",
+ sizeof(virNetSASLContext),
+ virNetSASLContextDispose)))
+ return -1;
+
+ if (!(virNetSASLSessionClass = virClassNew("virNetSASLSession",
+ sizeof(virNetSASLSession),
+ virNetSASLSessionDispose)))
+ return -1;
+
+ return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virNetSASLContext)
+
+
virNetSASLContextPtr virNetSASLContextNewClient(void)
{
virNetSASLContextPtr ctxt;
int err;
+ if (virNetSASLContextInitialize() < 0)
+ return NULL;
+
err = sasl_client_init(NULL);
if (err != SASL_OK) {
virNetError(VIR_ERR_AUTH_FAILED,
@@ -63,10 +91,8 @@ virNetSASLContextPtr virNetSASLContextNewClient(void)
return NULL;
}
- if (VIR_ALLOC(ctxt) < 0) {
- virReportOOMError();
+ if (!(ctxt = virObjectNew(virNetSASLContextClass)))
return NULL;
- }
if (virMutexInit(&ctxt->lock) < 0) {
virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -75,8 +101,6 @@ virNetSASLContextPtr virNetSASLContextNewClient(void)
return NULL;
}
- ctxt->refs = 1;
-
return ctxt;
}
@@ -85,6 +109,9 @@ virNetSASLContextPtr virNetSASLContextNewServer(const char
*const*usernameWhitel
virNetSASLContextPtr ctxt;
int err;
+ if (virNetSASLContextInitialize() < 0)
+ return NULL;
+
err = sasl_server_init(NULL, "libvirt");
if (err != SASL_OK) {
virNetError(VIR_ERR_AUTH_FAILED,
@@ -93,10 +120,8 @@ virNetSASLContextPtr virNetSASLContextNewServer(const char
*const*usernameWhitel
return NULL;
}
- if (VIR_ALLOC(ctxt) < 0) {
- virReportOOMError();
+ if (!(ctxt = virObjectNew(virNetSASLContextClass)))
return NULL;
- }
if (virMutexInit(&ctxt->lock) < 0) {
virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -106,7 +131,6 @@ virNetSASLContextPtr virNetSASLContextNewServer(const char
*const*usernameWhitel
}
ctxt->usernameWhitelist = usernameWhitelist;
- ctxt->refs = 1;
return ctxt;
}
@@ -156,28 +180,11 @@ cleanup:
}
-void virNetSASLContextRef(virNetSASLContextPtr ctxt)
-{
- virMutexLock(&ctxt->lock);
- ctxt->refs++;
- virMutexUnlock(&ctxt->lock);
-}
-
-void virNetSASLContextFree(virNetSASLContextPtr ctxt)
+void virNetSASLContextDispose(void *obj)
{
- if (!ctxt)
- return;
-
- virMutexLock(&ctxt->lock);
- ctxt->refs--;
- if (ctxt->refs > 0) {
- virMutexUnlock(&ctxt->lock);
- return;
- }
+ virNetSASLContextPtr ctxt = obj;
- virMutexUnlock(&ctxt->lock);
virMutexDestroy(&ctxt->lock);
- VIR_FREE(ctxt);
}
virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr ctxt
ATTRIBUTE_UNUSED,
@@ -190,10 +197,8 @@ virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr
ctxt ATTRIB
virNetSASLSessionPtr sasl = NULL;
int err;
- if (VIR_ALLOC(sasl) < 0) {
- virReportOOMError();
- goto cleanup;
- }
+ if (!(sasl = virObjectNew(virNetSASLSessionClass)))
+ return NULL;
if (virMutexInit(&sasl->lock) < 0) {
virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -202,7 +207,6 @@ virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr
ctxt ATTRIB
return NULL;
}
- sasl->refs = 1;
/* Arbitrary size for amount of data we can encode in a single block */
sasl->maxbufsize = 1 << 16;
@@ -223,7 +227,7 @@ virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr
ctxt ATTRIB
return sasl;
cleanup:
- virNetSASLSessionFree(sasl);
+ virObjectUnref(sasl);
return NULL;
}
@@ -235,10 +239,8 @@ virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr
ctxt ATTRIB
virNetSASLSessionPtr sasl = NULL;
int err;
- if (VIR_ALLOC(sasl) < 0) {
- virReportOOMError();
- goto cleanup;
- }
+ if (!(sasl = virObjectNew(virNetSASLSessionClass)))
+ return NULL;
if (virMutexInit(&sasl->lock) < 0) {
virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -247,7 +249,6 @@ virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr
ctxt ATTRIB
return NULL;
}
- sasl->refs = 1;
/* Arbitrary size for amount of data we can encode in a single block */
sasl->maxbufsize = 1 << 16;
@@ -269,17 +270,10 @@ virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr
ctxt ATTRIB
return sasl;
cleanup:
- virNetSASLSessionFree(sasl);
+ virObjectUnref(sasl);
return NULL;
}
-void virNetSASLSessionRef(virNetSASLSessionPtr sasl)
-{
- virMutexLock(&sasl->lock);
- sasl->refs++;
- virMutexUnlock(&sasl->lock);
-}
-
int virNetSASLSessionExtKeySize(virNetSASLSessionPtr sasl,
int ssf)
{
@@ -716,22 +710,12 @@ cleanup:
return ret;
}
-void virNetSASLSessionFree(virNetSASLSessionPtr sasl)
+void virNetSASLSessionDispose(void *obj)
{
- if (!sasl)
- return;
-
- virMutexLock(&sasl->lock);
- sasl->refs--;
- if (sasl->refs > 0) {
- virMutexUnlock(&sasl->lock);
- return;
- }
+ virNetSASLSessionPtr sasl = obj;
if (sasl->conn)
sasl_dispose(&sasl->conn);
- virMutexUnlock(&sasl->lock);
virMutexDestroy(&sasl->lock);
- VIR_FREE(sasl);
}
diff --git a/src/rpc/virnetsaslcontext.h b/src/rpc/virnetsaslcontext.h
index 774139c..961a4a0 100644
--- a/src/rpc/virnetsaslcontext.h
+++ b/src/rpc/virnetsaslcontext.h
@@ -24,6 +24,7 @@
# include <sasl/sasl.h>
# include "internal.h"
+# include "virobject.h"
typedef struct _virNetSASLContext virNetSASLContext;
typedef virNetSASLContext *virNetSASLContextPtr;
@@ -43,9 +44,6 @@ virNetSASLContextPtr virNetSASLContextNewServer(const char
*const*usernameWhitel
int virNetSASLContextCheckIdentity(virNetSASLContextPtr ctxt,
const char *identity);
-void virNetSASLContextRef(virNetSASLContextPtr sasl);
-void virNetSASLContextFree(virNetSASLContextPtr sasl);
-
virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr ctxt,
const char *service,
const char *hostname,
@@ -59,8 +57,6 @@ virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr
ctxt,
char *virNetSASLSessionListMechanisms(virNetSASLSessionPtr sasl);
-void virNetSASLSessionRef(virNetSASLSessionPtr sasl);
-
int virNetSASLSessionExtKeySize(virNetSASLSessionPtr sasl,
int ssf);
@@ -114,6 +110,4 @@ ssize_t virNetSASLSessionDecode(virNetSASLSessionPtr sasl,
const char **output,
size_t *outputlen);
-void virNetSASLSessionFree(virNetSASLSessionPtr sasl);
-
#endif /* __VIR_NET_CLIENT_SASL_CONTEXT_H__ */
diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index 85a457e..97941c9 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -476,8 +476,7 @@ void virNetServerClientSetSASLSession(virNetServerClientPtr client,
* operation do we switch to SASL mode
*/
virNetServerClientLock(client);
- client->sasl = sasl;
- virNetSASLSessionRef(sasl);
+ client->sasl = virObjectRef(sasl);
virNetServerClientUnlock(client);
}
#endif
@@ -593,7 +592,7 @@ void virNetServerClientFree(virNetServerClientPtr client)
VIR_FREE(client->identity);
#if HAVE_SASL
- virNetSASLSessionFree(client->sasl);
+ virObjectUnref(client->sasl);
#endif
if (client->sockTimer > 0)
virEventRemoveTimeout(client->sockTimer);
@@ -1011,7 +1010,7 @@ virNetServerClientDispatchWrite(virNetServerClientPtr client)
*/
if (client->sasl) {
virNetSocketSetSASLSession(client->sock, client->sasl);
- virNetSASLSessionFree(client->sasl);
+ virObjectUnref(client->sasl);
client->sasl = NULL;
}
#endif
diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index a851dad..1a64cc0 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -750,7 +750,7 @@ void virNetSocketFree(virNetSocketPtr sock)
virNetTLSSessionSetIOCallbacks(sock->tlsSession, NULL, NULL, NULL);
virObjectUnref(sock->tlsSession);
#if HAVE_SASL
- virNetSASLSessionFree(sock->saslSession);
+ virObjectUnref(sock->saslSession);
#endif
VIR_FORCE_CLOSE(sock->fd);
@@ -924,9 +924,8 @@ void virNetSocketSetSASLSession(virNetSocketPtr sock,
virNetSASLSessionPtr sess)
{
virMutexLock(&sock->lock);
- virNetSASLSessionFree(sock->saslSession);
- sock->saslSession = sess;
- virNetSASLSessionRef(sess);
+ virObjectUnref(sock->saslSession);
+ sock->saslSession = virObjectRef(sess);
virMutexUnlock(&sock->lock);
}
#endif
--
1.7.10.2