1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qapi/qmp/qdict.h"
4 #include "qapi/qmp/qerror.h"
5 #include "qom/object_interfaces.h"
6 #include "qemu/module.h"
7 #include "qemu/option.h"
8 #include "qapi/opts-visitor.h"
9 #include "qemu/config-file.h"
11 void user_creatable_complete(UserCreatable
*uc
, Error
**errp
)
13 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
16 ucc
->complete(uc
, errp
);
20 bool user_creatable_can_be_deleted(UserCreatable
*uc
)
23 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
25 if (ucc
->can_be_deleted
) {
26 return ucc
->can_be_deleted(uc
);
32 Object
*user_creatable_add_type(const char *type
, const char *id
,
34 Visitor
*v
, Error
**errp
)
39 Error
*local_err
= NULL
;
41 klass
= object_class_by_name(type
);
43 error_setg(errp
, "invalid object type: %s", type
);
47 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
48 error_setg(errp
, "object type '%s' isn't supported by object-add",
53 if (object_class_is_abstract(klass
)) {
54 error_setg(errp
, "object type '%s' is abstract", type
);
59 obj
= object_new(type
);
60 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
64 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
65 object_property_set(obj
, v
, e
->key
, &local_err
);
71 visit_check_struct(v
, &local_err
);
73 visit_end_struct(v
, NULL
);
79 object_property_add_child(object_get_objects_root(),
86 user_creatable_complete(USER_CREATABLE(obj
), &local_err
);
89 object_property_del(object_get_objects_root(),
96 error_propagate(errp
, local_err
);
104 Object
*user_creatable_add_opts(QemuOpts
*opts
, Error
**errp
)
109 const char *id
= qemu_opts_id(opts
);
110 char *type
= qemu_opt_get_del(opts
, "qom-type");
113 error_setg(errp
, QERR_MISSING_PARAMETER
, "qom-type");
117 error_setg(errp
, QERR_MISSING_PARAMETER
, "id");
118 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
123 qemu_opts_set_id(opts
, NULL
);
124 pdict
= qemu_opts_to_qdict(opts
, NULL
);
126 v
= opts_visitor_new(opts
);
127 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
130 qemu_opts_set_id(opts
, (char *) id
);
131 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
133 qobject_unref(pdict
);
138 int user_creatable_add_opts_foreach(void *opaque
, QemuOpts
*opts
, Error
**errp
)
140 bool (*type_opt_predicate
)(const char *, QemuOpts
*) = opaque
;
144 type
= qemu_opt_get(opts
, "qom-type");
145 if (type
&& type_opt_predicate
&&
146 !type_opt_predicate(type
, opts
)) {
150 obj
= user_creatable_add_opts(opts
, errp
);
159 void user_creatable_del(const char *id
, Error
**errp
)
164 container
= object_get_objects_root();
165 obj
= object_resolve_path_component(container
, id
);
167 error_setg(errp
, "object '%s' not found", id
);
171 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
))) {
172 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
177 * if object was defined on the command-line, remove its corresponding
180 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort
),
183 object_unparent(obj
);
186 void user_creatable_cleanup(void)
188 object_unparent(object_get_objects_root());
191 static void register_types(void)
193 static const TypeInfo uc_interface_info
= {
194 .name
= TYPE_USER_CREATABLE
,
195 .parent
= TYPE_INTERFACE
,
196 .class_size
= sizeof(UserCreatableClass
),
199 type_register_static(&uc_interface_info
);
202 type_init(register_types
)