Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/conf/secret_event.c | 9 ++---- src/conf/virsecretobj.c | 8 +++++- src/datatypes.c | 44 ++++++++++++++++++----------- src/datatypes.h | 14 +++++---- src/libvirt-secret.c | 7 ++--- src/libvirt_private.syms | 1 - src/remote/remote_daemon_dispatch.c | 4 +-- src/remote/remote_driver.c | 4 +-- src/storage/storage_util.c | 4 +-- src/util/virsecret.c | 3 +- 10 files changed, 54 insertions(+), 44 deletions(-) diff --git a/src/conf/secret_event.c b/src/conf/secret_event.c index 1c34dcfbc9..229f90c6c4 100644 --- a/src/conf/secret_event.c +++ b/src/conf/secret_event.c @@ -109,7 +109,7 @@ virSecretEventDispatchDefaultFunc(virConnectPtr conn, virConnectObjectEventGenericCallback cb, void *cbopaque) { - virSecretPtr secret = virGetSecret(conn, + g_autoptr(virSecret) secret = virGetSecret(conn, event->meta.uuid, event->meta.id, event->meta.name); @@ -127,23 +127,20 @@ virSecretEventDispatchDefaultFunc(virConnectPtr conn, secretLifecycleEvent->type, secretLifecycleEvent->detail, cbopaque); - goto cleanup; + return; } case VIR_SECRET_EVENT_ID_VALUE_CHANGED: { ((virConnectSecretEventGenericCallback)cb)(conn, secret, cbopaque); - goto cleanup; + return; } case VIR_SECRET_EVENT_ID_LAST: break; } VIR_WARN("Unexpected event ID %d", event->eventID); - - cleanup: - virObjectUnref(secret); } diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c index 81c9f2c346..861c66bd03 100644 --- a/src/conf/virsecretobj.c +++ b/src/conf/virsecretobj.c @@ -611,7 +611,13 @@ virSecretObjListExport(virConnectPtr conn, return data.nsecrets; error: - virObjectListFree(data.secrets); + if (data.secrets) { + while (data.nsecrets--) { + if (data.secrets[data.nsecrets]) + g_object_unref(data.secrets[data.nsecrets]); + } + VIR_FREE(data.secrets); + } return -1; } diff --git a/src/datatypes.c b/src/datatypes.c index 43767b9fb5..4f2bca6e6d 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -36,13 +36,11 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; -virClassPtr virSecretClass; virClassPtr virStreamClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); -static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); G_DEFINE_TYPE(virDomainCheckpoint, vir_domain_checkpoint, G_TYPE_OBJECT); @@ -173,6 +171,22 @@ vir_nw_filter_binding_class_init(virNWFilterBindingClass *klass) obj->finalize = virNWFilterBindingFinalize; } +G_DEFINE_TYPE(virSecret, vir_secret, G_TYPE_OBJECT); +static void virSecretFinalize(GObject *obj); + +static void +vir_secret_init(virSecret *sec G_GNUC_UNUSED) +{ +} + +static void +vir_secret_class_init(virSecretClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virSecretFinalize; +} + G_DEFINE_TYPE(virStoragePool, vir_storage_pool, G_TYPE_OBJECT); static void virStoragePoolFinalize(GObject *obj); @@ -257,7 +271,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); DECLARE_CLASS_LOCKABLE(virAdmConnect); @@ -862,7 +875,7 @@ virNodeDeviceFinalize(GObject *obj) * @uuid: secret UUID * * Allocates a new secret object. When the object is no longer needed, - * virObjectUnref() must be called in order to not leak data. + * g_object_unref() must be called in order to not leak data. * * Returns a pointer to the secret object, or NULL on error. */ @@ -870,16 +883,15 @@ virSecretPtr virGetSecret(virConnectPtr conn, const unsigned char *uuid, int usageType, const char *usageID) { - virSecretPtr ret = NULL; + g_autoptr(virSecret) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckConnectGoto(conn, error); - virCheckNonNullArgGoto(uuid, error); + virCheckConnectReturn(conn, NULL); + virCheckNonNullArgReturn(uuid, NULL); - if (!(ret = virObjectNew(virSecretClass))) - return NULL; + ret = VIR_SECRET(g_object_new(VIR_TYPE_SECRET, NULL)); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); ret->usageType = usageType; @@ -887,15 +899,11 @@ virGetSecret(virConnectPtr conn, const unsigned char *uuid, ret->conn = virObjectRef(conn); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** - * virSecretDispose: + * virSecretFinalize: * @obj: the secret to release * * Unconditionally release all memory associated with a secret. @@ -905,9 +913,9 @@ virGetSecret(virConnectPtr conn, const unsigned char *uuid, * which may also be released if its ref count hits zero. */ static void -virSecretDispose(void *obj) +virSecretFinalize(GObject *obj) { - virSecretPtr secret = obj; + virSecretPtr secret = VIR_SECRET(obj); char uuidstr[VIR_UUID_STRING_BUFLEN]; virUUIDFormat(secret->uuid, uuidstr); @@ -915,6 +923,8 @@ virSecretDispose(void *obj) VIR_FREE(secret->usageID); virObjectUnref(secret->conn); + + G_OBJECT_CLASS(vir_secret_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index 3b9a31920b..4b0747f728 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; -extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; @@ -72,6 +71,9 @@ G_DECLARE_FINAL_TYPE(virNWFilterBinding, NW_FILTER_BINDING, GObject); +#define VIR_TYPE_SECRET vir_secret_get_type() +G_DECLARE_FINAL_TYPE(virSecret, vir_secret, VIR, SECRET, GObject); + #define VIR_TYPE_STORAGE_POOL vir_storage_pool_get_type() G_DECLARE_FINAL_TYPE(virStoragePool, vir_storage_pool, VIR, STORAGE_POOL, GObject); @@ -278,8 +280,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckSecretReturn(obj, retval) \ do { \ - virSecretPtr _secret = (obj); \ - if (!virObjectIsClass(_secret, virSecretClass) || \ + virSecretPtr _secret = VIR_SECRET(obj); \ + if (!G_IS_OBJECT(_secret) || !(G_OBJECT_TYPE(_secret) == VIR_TYPE_SECRET) || \ !virObjectIsClass(_secret->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_SECRET, \ VIR_ERR_INVALID_SECRET, \ @@ -292,8 +294,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckSecretGoto(obj, label) \ do { \ - virSecretPtr _secret = (obj); \ - if (!virObjectIsClass(_secret, virSecretClass) || \ + virSecretPtr _secret = VIR_SECRET(obj); \ + if (!G_IS_OBJECT(_secret) || !(G_OBJECT_TYPE(_secret) == VIR_TYPE_SECRET) || \ !virObjectIsClass(_secret->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_SECRET, \ VIR_ERR_INVALID_SECRET, \ @@ -745,7 +747,7 @@ struct _virNodeDevice { * Internal structure associated with a secret */ struct _virSecret { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ unsigned char uuid[VIR_UUID_BUFLEN]; /* the secret unique identifier */ int usageType; /* the type of usage */ diff --git a/src/libvirt-secret.c b/src/libvirt-secret.c index 33cbdd7b0b..4248536a5b 100644 --- a/src/libvirt-secret.c +++ b/src/libvirt-secret.c @@ -658,14 +658,13 @@ virSecretUndefine(virSecretPtr secret) int virSecretRef(virSecretPtr secret) { - VIR_DEBUG("secret=%p refs=%d", secret, - secret ? secret->parent.u.s.refs : 0); + VIR_DEBUG("secret=%p", secret); virResetLastError(); virCheckSecretReturn(secret, -1); - virObjectRef(secret); + g_object_ref(secret); return 0; } @@ -687,7 +686,7 @@ virSecretFree(virSecretPtr secret) virCheckSecretReturn(secret, -1); - virObjectUnref(secret); + g_object_unref(secret); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index f2187b7cce..b66c3623f6 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1365,7 +1365,6 @@ virGetStoragePool; virGetStorageVol; virGetStream; virNewConnectCloseCallbackData; -virSecretClass; virStreamClass; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index 9f367bc515..a2dd0415b3 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -4406,7 +4406,7 @@ remoteDispatchSecretGetValue(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(secret); + g_object_unref(secret); return rv; } @@ -6436,7 +6436,7 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(secret); + g_object_unref(secret); return rv; } diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index a3f05563d4..65c2b92f1e 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5579,7 +5579,7 @@ remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, event = virSecretEventLifecycleNew(secret->uuid, secret->usageType, secret->usageID, msg->event, msg->detail); - virObjectUnref(secret); + g_object_unref(secret); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5600,7 +5600,7 @@ remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog G_GNUC_UNUSED, return; event = virSecretEventValueChangedNew(secret->uuid, secret->usageType, secret->usageID); - virObjectUnref(secret); + g_object_unref(secret); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index 1ec1d13625..7e3bfd1393 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -2172,7 +2172,7 @@ virStorageBackendVolDeleteLocal(virStoragePoolObjPtr pool G_GNUC_UNUSED, static int storageBackendLoadDefaultSecrets(virStorageVolDefPtr vol) { - virSecretPtr sec; + g_autoptr(virSecret) sec = NULL; virStorageEncryptionSecretPtr encsec = NULL; virConnectPtr conn = NULL; @@ -2199,7 +2199,6 @@ storageBackendLoadDefaultSecrets(virStorageVolDefPtr vol) if (VIR_ALLOC_N(vol->target.encryption->secrets, 1) < 0 || VIR_ALLOC(encsec) < 0) { VIR_FREE(vol->target.encryption->secrets); - virObjectUnref(sec); return -1; } @@ -2209,7 +2208,6 @@ storageBackendLoadDefaultSecrets(virStorageVolDefPtr vol) encsec->type = VIR_STORAGE_ENCRYPTION_SECRET_TYPE_PASSPHRASE; encsec->seclookupdef.type = VIR_SECRET_LOOKUP_TYPE_UUID; virSecretGetUUID(sec, encsec->seclookupdef.u.uuid); - virObjectUnref(sec); return 0; } diff --git a/src/util/virsecret.c b/src/util/virsecret.c index 54d6bbcb7c..7279d22683 100644 --- a/src/util/virsecret.c +++ b/src/util/virsecret.c @@ -147,7 +147,7 @@ virSecretGetSecretString(virConnectPtr conn, uint8_t **secret, size_t *secret_size) { - virSecretPtr sec = NULL; + g_autoptr(virSecret) sec = NULL; int ret = -1; switch (seclookupdef->type) { @@ -190,6 +190,5 @@ virSecretGetSecretString(virConnectPtr conn, ret = 0; cleanup: - virObjectUnref(sec); return ret; } -- 2.25.1