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"
20 #include "qemu/keyval.h"
22 bool user_creatable_complete(UserCreatable
*uc
, Error
**errp
)
24 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
28 ucc
->complete(uc
, &err
);
29 error_propagate(errp
, err
);
34 bool user_creatable_can_be_deleted(UserCreatable
*uc
)
37 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
39 if (ucc
->can_be_deleted
) {
40 return ucc
->can_be_deleted(uc
);
46 static void object_set_properties_from_qdict(Object
*obj
, const QDict
*qdict
,
47 Visitor
*v
, Error
**errp
)
51 if (!visit_start_struct(v
, NULL
, NULL
, 0, errp
)) {
54 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
55 if (!object_property_set(obj
, e
->key
, v
, errp
)) {
59 visit_check_struct(v
, errp
);
61 visit_end_struct(v
, NULL
);
64 void object_set_properties_from_keyval(Object
*obj
, const QDict
*qdict
,
65 bool from_json
, Error
**errp
)
69 v
= qobject_input_visitor_new(QOBJECT(qdict
));
71 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
73 object_set_properties_from_qdict(obj
, qdict
, v
, errp
);
77 Object
*user_creatable_add_type(const char *type
, const char *id
,
79 Visitor
*v
, Error
**errp
)
84 Error
*local_err
= NULL
;
86 if (id
!= NULL
&& !id_wellformed(id
)) {
87 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
88 error_append_hint(errp
, "Identifiers consist of letters, digits, "
89 "'-', '.', '_', starting with a letter.\n");
93 klass
= object_class_by_name(type
);
95 error_setg(errp
, "invalid object type: %s", type
);
99 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
100 error_setg(errp
, "object type '%s' isn't supported by object-add",
105 if (object_class_is_abstract(klass
)) {
106 error_setg(errp
, "object type '%s' is abstract", type
);
111 obj
= object_new(type
);
112 object_set_properties_from_qdict(obj
, qdict
, v
, &local_err
);
118 object_property_try_add_child(object_get_objects_root(),
119 id
, obj
, &local_err
);
125 if (!user_creatable_complete(USER_CREATABLE(obj
), &local_err
)) {
127 object_property_del(object_get_objects_root(), id
);
133 error_propagate(errp
, local_err
);
140 void user_creatable_add_qapi(ObjectOptions
*options
, Error
**errp
)
147 v
= qobject_output_visitor_new(&qobj
);
148 visit_type_ObjectOptions(v
, NULL
, &options
, &error_abort
);
149 visit_complete(v
, &qobj
);
152 props
= qobject_to(QDict
, qobj
);
153 qdict_del(props
, "qom-type");
154 qdict_del(props
, "id");
156 v
= qobject_input_visitor_new(QOBJECT(props
));
157 obj
= user_creatable_add_type(ObjectType_str(options
->qom_type
),
158 options
->id
, props
, v
, errp
);
164 char *object_property_help(const char *name
, const char *type
,
165 QObject
*defval
, const char *description
)
167 GString
*str
= g_string_new(NULL
);
169 g_string_append_printf(str
, " %s=<%s>", name
, type
);
170 if (description
|| defval
) {
172 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
174 g_string_append(str
, " - ");
177 g_string_append(str
, description
);
180 g_autofree
char *def_json
= g_string_free(qobject_to_json(defval
),
182 g_string_append_printf(str
, " (default: %s)", def_json
);
185 return g_string_free(str
, false);
188 static void user_creatable_print_types(void)
192 qemu_printf("List of user creatable objects:\n");
193 list
= object_class_get_list_sorted(TYPE_USER_CREATABLE
, false);
194 for (l
= list
; l
!= NULL
; l
= l
->next
) {
195 ObjectClass
*oc
= OBJECT_CLASS(l
->data
);
196 qemu_printf(" %s\n", object_class_get_name(oc
));
201 bool type_print_class_properties(const char *type
)
204 ObjectPropertyIterator iter
;
205 ObjectProperty
*prop
;
209 klass
= object_class_by_name(type
);
214 array
= g_ptr_array_new();
215 object_class_property_iter_init(&iter
, klass
);
216 while ((prop
= object_property_iter_next(&iter
))) {
221 g_ptr_array_add(array
,
222 object_property_help(prop
->name
, prop
->type
,
223 prop
->defval
, prop
->description
));
225 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
226 if (array
->len
> 0) {
227 qemu_printf("%s options:\n", type
);
229 qemu_printf("There are no options for %s.\n", type
);
231 for (i
= 0; i
< array
->len
; i
++) {
232 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
234 g_ptr_array_set_free_func(array
, g_free
);
235 g_ptr_array_free(array
, true);
239 bool user_creatable_print_help(const char *type
, QemuOpts
*opts
)
241 if (is_help_option(type
)) {
242 user_creatable_print_types();
246 if (qemu_opt_has_help_opt(opts
)) {
247 return type_print_class_properties(type
);
253 static void user_creatable_print_help_from_qdict(QDict
*args
)
255 const char *type
= qdict_get_try_str(args
, "qom-type");
257 if (!type
|| !type_print_class_properties(type
)) {
258 user_creatable_print_types();
262 ObjectOptions
*user_creatable_parse_str(const char *optarg
, Error
**errp
)
268 ObjectOptions
*options
;
270 if (optarg
[0] == '{') {
271 obj
= qobject_from_json(optarg
, errp
);
275 v
= qobject_input_visitor_new(obj
);
277 QDict
*args
= keyval_parse(optarg
, "qom-type", &help
, errp
);
282 user_creatable_print_help_from_qdict(args
);
288 v
= qobject_input_visitor_new_keyval(obj
);
291 visit_type_ObjectOptions(v
, NULL
, &options
, errp
);
298 bool user_creatable_add_from_str(const char *optarg
, Error
**errp
)
301 ObjectOptions
*options
;
303 options
= user_creatable_parse_str(optarg
, errp
);
308 user_creatable_add_qapi(options
, errp
);
309 qapi_free_ObjectOptions(options
);
313 void user_creatable_process_cmdline(const char *optarg
)
315 if (!user_creatable_add_from_str(optarg
, &error_fatal
)) {
316 /* Help was printed */
321 bool user_creatable_del(const char *id
, Error
**errp
)
323 QemuOptsList
*opts_list
;
327 container
= object_get_objects_root();
328 obj
= object_resolve_path_component(container
, id
);
330 error_setg(errp
, "object '%s' not found", id
);
334 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
))) {
335 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
340 * if object was defined on the command-line, remove its corresponding
343 opts_list
= qemu_find_opts_err("object", NULL
);
345 qemu_opts_del(qemu_opts_find(opts_list
, id
));
348 object_unparent(obj
);
352 void user_creatable_cleanup(void)
354 object_unparent(object_get_objects_root());
357 static void register_types(void)
359 static const TypeInfo uc_interface_info
= {
360 .name
= TYPE_USER_CREATABLE
,
361 .parent
= TYPE_INTERFACE
,
362 .class_size
= sizeof(UserCreatableClass
),
365 type_register_static(&uc_interface_info
);
368 type_init(register_types
)