Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/conf/secret_event.c | 15 +++---- src/conf/virsecretobj.c | 2 +- src/datatypes.c | 68 ++++++++++++++++++++--------- src/datatypes.h | 14 +++--- src/libvirt-secret.c | 7 ++- src/libvirt_private.syms | 2 +- src/remote/remote_daemon_dispatch.c | 6 +-- src/remote/remote_driver.c | 6 +-- src/rpc/gendispatch.pl | 1 + src/storage/storage_util.c | 4 +- src/util/virsecret.c | 3 +- 11 files changed, 73 insertions(+), 55 deletions(-) diff --git a/src/conf/secret_event.c b/src/conf/secret_event.c index 1c34dcfbc9..96fdcc3577 100644 --- a/src/conf/secret_event.c +++ b/src/conf/secret_event.c @@ -109,10 +109,10 @@ virSecretEventDispatchDefaultFunc(virConnectPtr conn, virConnectObjectEventGenericCallback cb, void *cbopaque) { - virSecretPtr secret = virGetSecret(conn, - event->meta.uuid, - event->meta.id, - event->meta.name); + g_autoptr(virSecret) secret = virGetSecret(conn, + event->meta.uuid, + event->meta.id, + event->meta.name); if (!secret) return; @@ -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..3905fc0f9b 100644 --- a/src/conf/virsecretobj.c +++ b/src/conf/virsecretobj.c @@ -611,7 +611,7 @@ virSecretObjListExport(virConnectPtr conn, return data.nsecrets; error: - virObjectListFree(data.secrets); + virGObjectListFreeCount(data.secrets, data.nsecrets); return -1; } diff --git a/src/datatypes.c b/src/datatypes.c index f324a19ada..0bb4cfbd29 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); @@ -189,6 +187,24 @@ vir_nw_filter_binding_class_init(virNWFilterBindingClass *klass) obj->finalize = virNWFilterBindingFinalize; } +G_DEFINE_TYPE(virSecret, vir_secret, G_TYPE_OBJECT); +static void virSecretDispose(GObject *obj); +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->dispose = virSecretDispose; + obj->finalize = virSecretFinalize; +} + G_DEFINE_TYPE(virStoragePool, vir_storage_pool, G_TYPE_OBJECT); static void virStoragePoolDispose(GObject *obj); static void virStoragePoolFinalize(GObject *obj); @@ -279,7 +295,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); DECLARE_CLASS_LOCKABLE(virAdmConnect); @@ -962,7 +977,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. */ @@ -970,16 +985,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; @@ -987,34 +1001,46 @@ virGetSecret(virConnectPtr conn, const unsigned char *uuid, ret->conn = virObjectRef(conn); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** * virSecretDispose: * @obj: the secret to release * - * Unconditionally release all memory associated with a secret. - * The secret object must not be used once this method returns. + * Unreferences the associated connection object, which may also be + * released if its ref count hits zero. + */ +static void +virSecretDispose(GObject *obj) +{ + virSecretPtr secret = VIR_SECRET(obj); + + virObjectUnref(secret->conn); + secret->conn = NULL; + + G_OBJECT_CLASS(vir_secret_parent_class)->dispose(obj); +} + +/** + * virSecretFinalize: + * @obj: the secret to release * - * It will also unreference the associated connection object, - * which may also be released if its ref count hits zero. + * Unconditionally releases all memory associated with a secret. + * The secret object must not be used once this method returns. */ 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); VIR_DEBUG("release secret %p %s", secret, uuidstr); 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 cbf565097f..2f97268691 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; #define VIR_TYPE_DOMAIN_CHECKPOINT vir_domain_checkpoint_get_type() @@ -71,6 +70,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); @@ -277,8 +279,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 (_secret == NULL || \ !virObjectIsClass(_secret->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_SECRET, \ VIR_ERR_INVALID_SECRET, \ @@ -291,8 +293,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 (_secret == NULL || \ !virObjectIsClass(_secret->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_SECRET, \ VIR_ERR_INVALID_SECRET, \ @@ -744,7 +746,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 2112082277..915979bb42 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1354,6 +1354,7 @@ vir_network_port_get_type; vir_node_device_get_type; vir_nw_filter_binding_get_type; vir_nw_filter_get_type; +vir_secret_get_type; vir_storage_pool_get_type; vir_storage_vol_get_type; virConnectClass; @@ -1378,7 +1379,6 @@ virGetStoragePool; virGetStorageVol; virGetStream; virNewConnectCloseCallbackData; -virSecretClass; virStreamClass; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index e7b2a5f363..fb1f1bd469 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -4382,7 +4382,7 @@ remoteDispatchSecretGetValue(virNetServerPtr server G_GNUC_UNUSED, remote_secret_get_value_args *args, remote_secret_get_value_ret *ret) { - virSecretPtr secret = NULL; + g_autoptr(virSecret) secret = NULL; size_t value_size; unsigned char *value; int rv = -1; @@ -4405,7 +4405,6 @@ remoteDispatchSecretGetValue(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(secret); return rv; } @@ -6375,7 +6374,7 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED daemonClientEventCallbackPtr ref; struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); - virSecretPtr secret = NULL; + g_autoptr(virSecret) secret = NULL; virConnectPtr conn = remoteGetSecretConn(client); virMutexLock(&priv->lock); @@ -6433,7 +6432,6 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(secret); return rv; } diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 0d7f352ae0..7801e5e990 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5559,7 +5559,7 @@ remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_secret_event_lifecycle_msg *msg = evdata; - virSecretPtr secret; + g_autoptr(virSecret) secret = NULL; virObjectEventPtr event = NULL; secret = get_nonnull_secret(conn, msg->secret); @@ -5568,7 +5568,6 @@ remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, event = virSecretEventLifecycleNew(secret->uuid, secret->usageType, secret->usageID, msg->event, msg->detail); - virObjectUnref(secret); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5581,7 +5580,7 @@ remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_secret_event_value_changed_msg *msg = evdata; - virSecretPtr secret; + g_autoptr(virSecret) secret = NULL; virObjectEventPtr event = NULL; secret = get_nonnull_secret(conn, msg->secret); @@ -5589,7 +5588,6 @@ remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog G_GNUC_UNUSED, return; event = virSecretEventValueChangedNew(secret->uuid, secret->usageType, secret->usageID); - virObjectUnref(secret); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 6869311f06..902ff091a5 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -204,6 +204,7 @@ my %gobject_impl = ( virNodeDevice => 1, virNWFilter => 1, virNWFilterBinding => 1, + virSecret => 1, virStoragePool => 1, virStorageVol => 1, ); 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.3