From 269e09f3fc922b800d118d9c8a721be46b5462a3 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Thu, 16 Jan 2014 17:34:38 +0100 Subject: add optional 2nd stage initialization to -object/object-add commands Introduces USER_CREATABLE interface that must be implemented by objects which are designed to created with -object CLI option or object-add QMP command. Interface provides an ability to do an optional second stage initialization of the object created with -object/object-add commands. By providing complete() callback, which is called after the object properties were set. It allows to: * prevents misusing of -object/object-add by filtering out objects that are not designed for it. * generalize second stage backend initialization instead of adding custom APIs to perform it * early error detection of backend initialization at -object/ object-add time rather than through a proxy DEVICE object that tries to use backend. Signed-off-by: Igor Mammedov Reviewed-by: Stefan Hajnoczi Signed-off-by: Luiz Capitulino --- backends/rng.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'backends/rng.c') diff --git a/backends/rng.c b/backends/rng.c index 85cb83f5e1..7bab806ee1 100644 --- a/backends/rng.c +++ b/backends/rng.c @@ -12,6 +12,7 @@ #include "sysemu/rng.h" #include "qapi/qmp/qerror.h" +#include "qom/object_interfaces.h" void rng_backend_request_entropy(RngBackend *s, size_t size, EntropyReceiveFunc *receive_entropy, @@ -83,6 +84,10 @@ static const TypeInfo rng_backend_info = { .instance_init = rng_backend_init, .class_size = sizeof(RngBackendClass), .abstract = true, + .interfaces = (InterfaceInfo[]) { + { TYPE_USER_CREATABLE }, + { } + } }; static void register_types(void) -- cgit v1.2.1 From 57d3e1b3f52d07d215ed96df946ee01f8d9f9526 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Thu, 16 Jan 2014 17:34:39 +0100 Subject: virtio_rng: replace custom backend API with UserCreatable.complete() callback in addition fix default backend leak by releasing it if its initialization failed. Signed-off-by: Igor Mammedov Reviewed-by: Stefan Hajnoczi Signed-off-by: Luiz Capitulino --- backends/rng.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'backends/rng.c') diff --git a/backends/rng.c b/backends/rng.c index 7bab806ee1..8b8d5a4973 100644 --- a/backends/rng.c +++ b/backends/rng.c @@ -41,9 +41,9 @@ static bool rng_backend_prop_get_opened(Object *obj, Error **errp) return s->opened; } -void rng_backend_open(RngBackend *s, Error **errp) +static void rng_backend_complete(UserCreatable *uc, Error **errp) { - object_property_set_bool(OBJECT(s), true, "opened", errp); + object_property_set_bool(OBJECT(uc), true, "opened", errp); } static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp) @@ -77,12 +77,20 @@ static void rng_backend_init(Object *obj) NULL); } +static void rng_backend_class_init(ObjectClass *oc, void *data) +{ + UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); + + ucc->complete = rng_backend_complete; +} + static const TypeInfo rng_backend_info = { .name = TYPE_RNG_BACKEND, .parent = TYPE_OBJECT, .instance_size = sizeof(RngBackend), .instance_init = rng_backend_init, .class_size = sizeof(RngBackendClass), + .class_init = rng_backend_class_init, .abstract = true, .interfaces = (InterfaceInfo[]) { { TYPE_USER_CREATABLE }, -- cgit v1.2.1