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 Object
*user_creatable_add_type(const char *type
, const char *id
,
47 Visitor
*v
, Error
**errp
)
53 Error
*local_err
= NULL
;
55 if (id
!= NULL
&& !id_wellformed(id
)) {
56 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
57 error_append_hint(errp
, "Identifiers consist of letters, digits, "
58 "'-', '.', '_', starting with a letter.\n");
62 klass
= object_class_by_name(type
);
64 error_setg(errp
, "invalid object type: %s", type
);
68 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
69 error_setg(errp
, "object type '%s' isn't supported by object-add",
74 if (object_class_is_abstract(klass
)) {
75 error_setg(errp
, "object type '%s' is abstract", type
);
80 obj
= object_new(type
);
81 if (!visit_start_struct(v
, NULL
, NULL
, 0, &local_err
)) {
84 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
85 if (!object_property_set(obj
, e
->key
, v
, &local_err
)) {
90 visit_check_struct(v
, &local_err
);
92 visit_end_struct(v
, NULL
);
98 object_property_try_add_child(object_get_objects_root(),
105 if (!user_creatable_complete(USER_CREATABLE(obj
), &local_err
)) {
107 object_property_del(object_get_objects_root(), id
);
113 error_propagate(errp
, local_err
);
120 void user_creatable_add_qapi(ObjectOptions
*options
, Error
**errp
)
127 v
= qobject_output_visitor_new(&qobj
);
128 visit_type_ObjectOptions(v
, NULL
, &options
, &error_abort
);
129 visit_complete(v
, &qobj
);
132 props
= qobject_to(QDict
, qobj
);
133 qdict_del(props
, "qom-type");
134 qdict_del(props
, "id");
136 v
= qobject_input_visitor_new(QOBJECT(props
));
137 obj
= user_creatable_add_type(ObjectType_str(options
->qom_type
),
138 options
->id
, props
, v
, errp
);
144 char *object_property_help(const char *name
, const char *type
,
145 QObject
*defval
, const char *description
)
147 GString
*str
= g_string_new(NULL
);
149 g_string_append_printf(str
, " %s=<%s>", name
, type
);
150 if (description
|| defval
) {
152 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
154 g_string_append(str
, " - ");
157 g_string_append(str
, description
);
160 g_autofree
char *def_json
= g_string_free(qobject_to_json(defval
),
162 g_string_append_printf(str
, " (default: %s)", def_json
);
165 return g_string_free(str
, false);
168 static void user_creatable_print_types(void)
172 qemu_printf("List of user creatable objects:\n");
173 list
= object_class_get_list_sorted(TYPE_USER_CREATABLE
, false);
174 for (l
= list
; l
!= NULL
; l
= l
->next
) {
175 ObjectClass
*oc
= OBJECT_CLASS(l
->data
);
176 qemu_printf(" %s\n", object_class_get_name(oc
));
181 static bool user_creatable_print_type_properites(const char *type
)
184 ObjectPropertyIterator iter
;
185 ObjectProperty
*prop
;
189 klass
= object_class_by_name(type
);
194 array
= g_ptr_array_new();
195 object_class_property_iter_init(&iter
, klass
);
196 while ((prop
= object_property_iter_next(&iter
))) {
201 g_ptr_array_add(array
,
202 object_property_help(prop
->name
, prop
->type
,
203 prop
->defval
, prop
->description
));
205 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
206 if (array
->len
> 0) {
207 qemu_printf("%s options:\n", type
);
209 qemu_printf("There are no options for %s.\n", type
);
211 for (i
= 0; i
< array
->len
; i
++) {
212 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
214 g_ptr_array_set_free_func(array
, g_free
);
215 g_ptr_array_free(array
, true);
219 bool user_creatable_print_help(const char *type
, QemuOpts
*opts
)
221 if (is_help_option(type
)) {
222 user_creatable_print_types();
226 if (qemu_opt_has_help_opt(opts
)) {
227 return user_creatable_print_type_properites(type
);
233 static void user_creatable_print_help_from_qdict(QDict
*args
)
235 const char *type
= qdict_get_try_str(args
, "qom-type");
237 if (!type
|| !user_creatable_print_type_properites(type
)) {
238 user_creatable_print_types();
242 ObjectOptions
*user_creatable_parse_str(const char *optarg
, Error
**errp
)
248 ObjectOptions
*options
;
250 if (optarg
[0] == '{') {
251 obj
= qobject_from_json(optarg
, errp
);
255 v
= qobject_input_visitor_new(obj
);
257 QDict
*args
= keyval_parse(optarg
, "qom-type", &help
, errp
);
262 user_creatable_print_help_from_qdict(args
);
268 v
= qobject_input_visitor_new_keyval(obj
);
271 visit_type_ObjectOptions(v
, NULL
, &options
, errp
);
278 bool user_creatable_add_from_str(const char *optarg
, Error
**errp
)
281 ObjectOptions
*options
;
283 options
= user_creatable_parse_str(optarg
, errp
);
288 user_creatable_add_qapi(options
, errp
);
289 qapi_free_ObjectOptions(options
);
293 void user_creatable_process_cmdline(const char *optarg
)
295 if (!user_creatable_add_from_str(optarg
, &error_fatal
)) {
296 /* Help was printed */
301 bool user_creatable_del(const char *id
, Error
**errp
)
303 QemuOptsList
*opts_list
;
307 container
= object_get_objects_root();
308 obj
= object_resolve_path_component(container
, id
);
310 error_setg(errp
, "object '%s' not found", id
);
314 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
))) {
315 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
320 * if object was defined on the command-line, remove its corresponding
323 opts_list
= qemu_find_opts_err("object", NULL
);
325 qemu_opts_del(qemu_opts_find(opts_list
, id
));
328 object_unparent(obj
);
332 void user_creatable_cleanup(void)
334 object_unparent(object_get_objects_root());
337 static void register_types(void)
339 static const TypeInfo uc_interface_info
= {
340 .name
= TYPE_USER_CREATABLE
,
341 .parent
= TYPE_INTERFACE
,
342 .class_size
= sizeof(UserCreatableClass
),
345 type_register_static(&uc_interface_info
);
348 type_init(register_types
)