From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48798) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YZzKM-0005oe-3Q for qemu-devel@nongnu.org; Mon, 23 Mar 2015 06:11:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YZzKH-0007Cz-40 for qemu-devel@nongnu.org; Mon, 23 Mar 2015 06:11:22 -0400 Received: from mail-pd0-x22e.google.com ([2607:f8b0:400e:c02::22e]:34376) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YZzKG-0007Cl-TD for qemu-devel@nongnu.org; Mon, 23 Mar 2015 06:11:17 -0400 Received: by pdbni2 with SMTP id ni2so183201774pdb.1 for ; Mon, 23 Mar 2015 03:11:15 -0700 (PDT) Sender: Lin Ma From: Lin Ma Date: Mon, 23 Mar 2015 18:10:41 +0800 Message-Id: <1427105442-23484-1-git-send-email-lma@suse.com> Subject: [Qemu-devel] [PATCH 1/2] object: Add can_be_deleted callback to TypeInfo and TypeImpl List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org, imammedo@redhat.com Cc: pbonzini@redhat.com, Lin Ma Add can_be_deleted callback, If it is not null and returns false, The qmp_object_del won't delete the given object. Signed-off-by: Lin Ma --- include/qom/object.h | 12 ++++++++++++ qmp.c | 7 +++++++ qom/object.c | 12 ++++++++++++ 3 files changed, 31 insertions(+) diff --git a/include/qom/object.h b/include/qom/object.h index d2d7748..6e78cb0 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -428,6 +428,8 @@ struct Object * function. * @abstract: If this field is true, then the class is considered abstract and * cannot be directly instantiated. + * @can_be_deleted: If this function returns true, then the object can be + deleted safely. * @class_size: The size of the class object (derivative of #ObjectClass) * for this object. If @class_size is 0, then the size of the class will be * assumed to be the size of the parent class. This allows a type to avoid @@ -463,6 +465,8 @@ struct TypeInfo bool abstract; size_t class_size; + bool (*can_be_deleted)(Object *obj); + void (*class_init)(ObjectClass *klass, void *data); void (*class_base_init)(ObjectClass *klass, void *data); void (*class_finalize)(ObjectClass *klass, void *data); @@ -671,6 +675,14 @@ ObjectClass *object_get_class(Object *obj); const char *object_get_typename(Object *obj); /** + * object_can_be_deleted: + * @obj: The object to obtain the deletion for. + * + * Returns: %true if @obj can be deleted safely, %false otherwise. + */ +bool object_can_be_deleted(Object *obj); + +/** * type_register_static: * @info: The #TypeInfo of the new type. * diff --git a/qmp.c b/qmp.c index c479e77..dbbcb37 100644 --- a/qmp.c +++ b/qmp.c @@ -711,6 +711,13 @@ void qmp_object_del(const char *id, Error **errp) error_setg(errp, "object id not found"); return; } + + if (!object_can_be_deleted(obj)) { + char *path = object_get_canonical_path_component(obj); + error_setg(errp, "%s is in used, can not be deleted", path); + g_free(path); + return; + } object_unparent(obj); } diff --git a/qom/object.c b/qom/object.c index d167038..dcec108 100644 --- a/qom/object.c +++ b/qom/object.c @@ -57,6 +57,8 @@ struct TypeImpl bool abstract; + bool (*can_be_deleted)(Object *obj); + const char *parent; TypeImpl *parent_type; @@ -121,6 +123,8 @@ static TypeImpl *type_new(const TypeInfo *info) ti->abstract = info->abstract; + ti->can_be_deleted = info->can_be_deleted; + for (i = 0; info->interfaces && info->interfaces[i].type; i++) { ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); } @@ -584,6 +588,14 @@ const char *object_get_typename(Object *obj) return obj->class->type->name; } +bool object_can_be_deleted(Object *obj) +{ + if (obj->class->type->can_be_deleted) + return obj->class->type->can_be_deleted(obj); + else + return true; +} + ObjectClass *object_get_class(Object *obj) { return obj->class; -- 2.1.4