1 #include "qemu/osdep.h"
3 #include "qemu/cutils.h"
4 #include "qapi/error.h"
5 #include "qapi/qmp/qdict.h"
6 #include "qapi/qmp/qerror.h"
7 #include "qom/object_interfaces.h"
8 #include "qemu/help_option.h"
9 #include "qemu/module.h"
10 #include "qemu/option.h"
11 #include "qapi/opts-visitor.h"
12 #include "qemu/config-file.h"
14 void user_creatable_complete(UserCreatable
*uc
, Error
**errp
)
16 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
19 ucc
->complete(uc
, errp
);
23 bool user_creatable_can_be_deleted(UserCreatable
*uc
)
26 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
28 if (ucc
->can_be_deleted
) {
29 return ucc
->can_be_deleted(uc
);
35 Object
*user_creatable_add_type(const char *type
, const char *id
,
37 Visitor
*v
, Error
**errp
)
42 Error
*local_err
= NULL
;
44 klass
= object_class_by_name(type
);
46 error_setg(errp
, "invalid object type: %s", type
);
50 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
51 error_setg(errp
, "object type '%s' isn't supported by object-add",
56 if (object_class_is_abstract(klass
)) {
57 error_setg(errp
, "object type '%s' is abstract", type
);
62 obj
= object_new(type
);
63 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
67 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
68 object_property_set(obj
, v
, e
->key
, &local_err
);
74 visit_check_struct(v
, &local_err
);
76 visit_end_struct(v
, NULL
);
82 object_property_add_child(object_get_objects_root(),
89 user_creatable_complete(USER_CREATABLE(obj
), &local_err
);
92 object_property_del(object_get_objects_root(),
99 error_propagate(errp
, local_err
);
107 Object
*user_creatable_add_opts(QemuOpts
*opts
, Error
**errp
)
112 const char *id
= qemu_opts_id(opts
);
113 char *type
= qemu_opt_get_del(opts
, "qom-type");
116 error_setg(errp
, QERR_MISSING_PARAMETER
, "qom-type");
120 error_setg(errp
, QERR_MISSING_PARAMETER
, "id");
121 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
126 qemu_opts_set_id(opts
, NULL
);
127 pdict
= qemu_opts_to_qdict(opts
, NULL
);
129 v
= opts_visitor_new(opts
);
130 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
133 qemu_opts_set_id(opts
, (char *) id
);
134 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
136 qobject_unref(pdict
);
141 int user_creatable_add_opts_foreach(void *opaque
, QemuOpts
*opts
, Error
**errp
)
143 bool (*type_opt_predicate
)(const char *, QemuOpts
*) = opaque
;
147 type
= qemu_opt_get(opts
, "qom-type");
148 if (type
&& type_opt_predicate
&&
149 !type_opt_predicate(type
, opts
)) {
153 obj
= user_creatable_add_opts(opts
, errp
);
161 bool user_creatable_print_help(const char *type
, QemuOpts
*opts
)
165 if (is_help_option(type
)) {
168 printf("List of user creatable objects:\n");
169 list
= object_class_get_list_sorted(TYPE_USER_CREATABLE
, false);
170 for (l
= list
; l
!= NULL
; l
= l
->next
) {
171 ObjectClass
*oc
= OBJECT_CLASS(l
->data
);
172 printf(" %s\n", object_class_get_name(oc
));
178 klass
= object_class_by_name(type
);
179 if (klass
&& qemu_opt_has_help_opt(opts
)) {
180 ObjectPropertyIterator iter
;
181 ObjectProperty
*prop
;
182 GPtrArray
*array
= g_ptr_array_new();
185 object_class_property_iter_init(&iter
, klass
);
186 while ((prop
= object_property_iter_next(&iter
))) {
193 str
= g_string_new(NULL
);
194 g_string_append_printf(str
, " %s=<%s>", prop
->name
, prop
->type
);
195 if (prop
->description
) {
197 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
199 g_string_append_printf(str
, " - %s", prop
->description
);
201 g_ptr_array_add(array
, g_string_free(str
, false));
203 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
204 if (array
->len
> 0) {
205 printf("%s options:\n", type
);
207 printf("There are no options for %s.\n", type
);
209 for (i
= 0; i
< array
->len
; i
++) {
210 printf("%s\n", (char *)array
->pdata
[i
]);
212 g_ptr_array_set_free_func(array
, g_free
);
213 g_ptr_array_free(array
, true);
220 void user_creatable_del(const char *id
, Error
**errp
)
225 container
= object_get_objects_root();
226 obj
= object_resolve_path_component(container
, id
);
228 error_setg(errp
, "object '%s' not found", id
);
232 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
))) {
233 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
238 * if object was defined on the command-line, remove its corresponding
241 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort
),
244 object_unparent(obj
);
247 void user_creatable_cleanup(void)
249 object_unparent(object_get_objects_root());
252 static void register_types(void)
254 static const TypeInfo uc_interface_info
= {
255 .name
= TYPE_USER_CREATABLE
,
256 .parent
= TYPE_INTERFACE
,
257 .class_size
= sizeof(UserCreatableClass
),
260 type_register_static(&uc_interface_info
);
263 type_init(register_types
)