1 #include "qemu/osdep.h"
3 #include "qemu/cutils.h"
4 #include "qapi/error.h"
5 #include "qapi/qapi-commands-qom.h"
6 #include "qapi/qapi-visit-qom.h"
7 #include "qapi/qmp/qdict.h"
8 #include "qapi/qmp/qerror.h"
9 #include "qapi/qmp/qjson.h"
10 #include "qapi/qobject-input-visitor.h"
11 #include "qapi/qobject-output-visitor.h"
12 #include "qom/object_interfaces.h"
13 #include "qemu/help_option.h"
15 #include "qemu/module.h"
16 #include "qemu/option.h"
17 #include "qemu/qemu-print.h"
18 #include "qapi/opts-visitor.h"
19 #include "qemu/config-file.h"
21 bool user_creatable_complete(UserCreatable
*uc
, Error
**errp
)
23 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
27 ucc
->complete(uc
, &err
);
28 error_propagate(errp
, err
);
33 bool user_creatable_can_be_deleted(UserCreatable
*uc
)
36 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
38 if (ucc
->can_be_deleted
) {
39 return ucc
->can_be_deleted(uc
);
45 static void object_set_properties_from_qdict(Object
*obj
, const QDict
*qdict
,
46 Visitor
*v
, Error
**errp
)
49 Error
*local_err
= NULL
;
51 if (!visit_start_struct(v
, NULL
, NULL
, 0, &local_err
)) {
54 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
55 if (!object_property_set(obj
, e
->key
, v
, &local_err
)) {
60 visit_check_struct(v
, &local_err
);
62 visit_end_struct(v
, NULL
);
66 error_propagate(errp
, local_err
);
70 void object_set_properties_from_keyval(Object
*obj
, const QDict
*qdict
,
71 bool from_json
, Error
**errp
)
75 v
= qobject_input_visitor_new(QOBJECT(qdict
));
77 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
79 object_set_properties_from_qdict(obj
, qdict
, v
, errp
);
83 Object
*user_creatable_add_type(const char *type
, const char *id
,
85 Visitor
*v
, Error
**errp
)
90 Error
*local_err
= NULL
;
92 if (id
!= NULL
&& !id_wellformed(id
)) {
93 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
94 error_append_hint(errp
, "Identifiers consist of letters, digits, "
95 "'-', '.', '_', starting with a letter.\n");
99 klass
= object_class_by_name(type
);
101 error_setg(errp
, "invalid object type: %s", type
);
105 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
106 error_setg(errp
, "object type '%s' isn't supported by object-add",
111 if (object_class_is_abstract(klass
)) {
112 error_setg(errp
, "object type '%s' is abstract", type
);
117 obj
= object_new(type
);
118 object_set_properties_from_qdict(obj
, qdict
, v
, &local_err
);
124 object_property_try_add_child(object_get_objects_root(),
125 id
, obj
, &local_err
);
131 if (!user_creatable_complete(USER_CREATABLE(obj
), &local_err
)) {
133 object_property_del(object_get_objects_root(), id
);
139 error_propagate(errp
, local_err
);
146 void user_creatable_add_qapi(ObjectOptions
*options
, Error
**errp
)
153 v
= qobject_output_visitor_new(&qobj
);
154 visit_type_ObjectOptions(v
, NULL
, &options
, &error_abort
);
155 visit_complete(v
, &qobj
);
158 props
= qobject_to(QDict
, qobj
);
159 qdict_del(props
, "qom-type");
160 qdict_del(props
, "id");
162 v
= qobject_input_visitor_new(QOBJECT(props
));
163 obj
= user_creatable_add_type(ObjectType_str(options
->qom_type
),
164 options
->id
, props
, v
, errp
);
170 char *object_property_help(const char *name
, const char *type
,
171 QObject
*defval
, const char *description
)
173 GString
*str
= g_string_new(NULL
);
175 g_string_append_printf(str
, " %s=<%s>", name
, type
);
176 if (description
|| defval
) {
178 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
180 g_string_append(str
, " - ");
183 g_string_append(str
, description
);
186 g_autofree
char *def_json
= g_string_free(qobject_to_json(defval
),
188 g_string_append_printf(str
, " (default: %s)", def_json
);
191 return g_string_free(str
, false);
194 static void user_creatable_print_types(void)
198 qemu_printf("List of user creatable objects:\n");
199 list
= object_class_get_list_sorted(TYPE_USER_CREATABLE
, false);
200 for (l
= list
; l
!= NULL
; l
= l
->next
) {
201 ObjectClass
*oc
= OBJECT_CLASS(l
->data
);
202 qemu_printf(" %s\n", object_class_get_name(oc
));
207 bool type_print_class_properties(const char *type
)
210 ObjectPropertyIterator iter
;
211 ObjectProperty
*prop
;
215 klass
= object_class_by_name(type
);
220 array
= g_ptr_array_new();
221 object_class_property_iter_init(&iter
, klass
);
222 while ((prop
= object_property_iter_next(&iter
))) {
227 g_ptr_array_add(array
,
228 object_property_help(prop
->name
, prop
->type
,
229 prop
->defval
, prop
->description
));
231 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
232 if (array
->len
> 0) {
233 qemu_printf("%s options:\n", type
);
235 qemu_printf("There are no options for %s.\n", type
);
237 for (i
= 0; i
< array
->len
; i
++) {
238 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
240 g_ptr_array_set_free_func(array
, g_free
);
241 g_ptr_array_free(array
, true);
245 bool user_creatable_print_help(const char *type
, QemuOpts
*opts
)
247 if (is_help_option(type
)) {
248 user_creatable_print_types();
252 if (qemu_opt_has_help_opt(opts
)) {
253 return type_print_class_properties(type
);
259 static void user_creatable_print_help_from_qdict(QDict
*args
)
261 const char *type
= qdict_get_try_str(args
, "qom-type");
263 if (!type
|| !type_print_class_properties(type
)) {
264 user_creatable_print_types();
268 ObjectOptions
*user_creatable_parse_str(const char *optarg
, Error
**errp
)
274 ObjectOptions
*options
;
276 if (optarg
[0] == '{') {
277 obj
= qobject_from_json(optarg
, errp
);
281 v
= qobject_input_visitor_new(obj
);
283 QDict
*args
= keyval_parse(optarg
, "qom-type", &help
, errp
);
288 user_creatable_print_help_from_qdict(args
);
294 v
= qobject_input_visitor_new_keyval(obj
);
297 visit_type_ObjectOptions(v
, NULL
, &options
, errp
);
304 bool user_creatable_add_from_str(const char *optarg
, Error
**errp
)
307 ObjectOptions
*options
;
309 options
= user_creatable_parse_str(optarg
, errp
);
314 user_creatable_add_qapi(options
, errp
);
315 qapi_free_ObjectOptions(options
);
319 void user_creatable_process_cmdline(const char *optarg
)
321 if (!user_creatable_add_from_str(optarg
, &error_fatal
)) {
322 /* Help was printed */
327 bool user_creatable_del(const char *id
, Error
**errp
)
329 QemuOptsList
*opts_list
;
333 container
= object_get_objects_root();
334 obj
= object_resolve_path_component(container
, id
);
336 error_setg(errp
, "object '%s' not found", id
);
340 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
))) {
341 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
346 * if object was defined on the command-line, remove its corresponding
349 opts_list
= qemu_find_opts_err("object", NULL
);
351 qemu_opts_del(qemu_opts_find(opts_list
, id
));
354 object_unparent(obj
);
358 void user_creatable_cleanup(void)
360 object_unparent(object_get_objects_root());
363 static void register_types(void)
365 static const TypeInfo uc_interface_info
= {
366 .name
= TYPE_USER_CREATABLE
,
367 .parent
= TYPE_INTERFACE
,
368 .class_size
= sizeof(UserCreatableClass
),
371 type_register_static(&uc_interface_info
);
374 type_init(register_types
)