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/qemu-print.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 Object
*user_creatable_add_type(const char *type
, const char *id
,
48 Visitor
*v
, Error
**errp
)
54 Error
*local_err
= NULL
;
56 if (id
!= NULL
&& !id_wellformed(id
)) {
57 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
58 error_append_hint(errp
, "Identifiers consist of letters, digits, "
59 "'-', '.', '_', starting with a letter.\n");
63 klass
= object_class_by_name(type
);
65 error_setg(errp
, "invalid object type: %s", type
);
69 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
70 error_setg(errp
, "object type '%s' isn't supported by object-add",
75 if (object_class_is_abstract(klass
)) {
76 error_setg(errp
, "object type '%s' is abstract", type
);
81 obj
= object_new(type
);
82 if (!visit_start_struct(v
, NULL
, NULL
, 0, &local_err
)) {
85 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
86 if (!object_property_set(obj
, e
->key
, v
, &local_err
)) {
91 visit_check_struct(v
, &local_err
);
93 visit_end_struct(v
, NULL
);
99 object_property_try_add_child(object_get_objects_root(),
100 id
, obj
, &local_err
);
106 if (!user_creatable_complete(USER_CREATABLE(obj
), &local_err
)) {
108 object_property_del(object_get_objects_root(), id
);
114 error_propagate(errp
, local_err
);
121 void user_creatable_add_qapi(ObjectOptions
*options
, Error
**errp
)
128 v
= qobject_output_visitor_new(&qobj
);
129 visit_type_ObjectOptions(v
, NULL
, &options
, &error_abort
);
130 visit_complete(v
, &qobj
);
133 props
= qobject_to(QDict
, qobj
);
134 qdict_del(props
, "qom-type");
135 qdict_del(props
, "id");
137 v
= qobject_input_visitor_new(QOBJECT(props
));
138 obj
= user_creatable_add_type(ObjectType_str(options
->qom_type
),
139 options
->id
, props
, v
, errp
);
145 char *object_property_help(const char *name
, const char *type
,
146 QObject
*defval
, const char *description
)
148 GString
*str
= g_string_new(NULL
);
150 g_string_append_printf(str
, " %s=<%s>", name
, type
);
151 if (description
|| defval
) {
153 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
155 g_string_append(str
, " - ");
158 g_string_append(str
, description
);
161 g_autofree
char *def_json
= g_string_free(qobject_to_json(defval
),
163 g_string_append_printf(str
, " (default: %s)", def_json
);
166 return g_string_free(str
, false);
169 static void user_creatable_print_types(void)
173 qemu_printf("List of user creatable objects:\n");
174 list
= object_class_get_list_sorted(TYPE_USER_CREATABLE
, false);
175 for (l
= list
; l
!= NULL
; l
= l
->next
) {
176 ObjectClass
*oc
= OBJECT_CLASS(l
->data
);
177 qemu_printf(" %s\n", object_class_get_name(oc
));
182 static bool user_creatable_print_type_properites(const char *type
)
185 ObjectPropertyIterator iter
;
186 ObjectProperty
*prop
;
190 klass
= object_class_by_name(type
);
195 array
= g_ptr_array_new();
196 object_class_property_iter_init(&iter
, klass
);
197 while ((prop
= object_property_iter_next(&iter
))) {
202 g_ptr_array_add(array
,
203 object_property_help(prop
->name
, prop
->type
,
204 prop
->defval
, prop
->description
));
206 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
207 if (array
->len
> 0) {
208 qemu_printf("%s options:\n", type
);
210 qemu_printf("There are no options for %s.\n", type
);
212 for (i
= 0; i
< array
->len
; i
++) {
213 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
215 g_ptr_array_set_free_func(array
, g_free
);
216 g_ptr_array_free(array
, true);
220 bool user_creatable_print_help(const char *type
, QemuOpts
*opts
)
222 if (is_help_option(type
)) {
223 user_creatable_print_types();
227 if (qemu_opt_has_help_opt(opts
)) {
228 return user_creatable_print_type_properites(type
);
234 static void user_creatable_print_help_from_qdict(QDict
*args
)
236 const char *type
= qdict_get_try_str(args
, "qom-type");
238 if (!type
|| !user_creatable_print_type_properites(type
)) {
239 user_creatable_print_types();
243 ObjectOptions
*user_creatable_parse_str(const char *optarg
, Error
**errp
)
249 ObjectOptions
*options
;
251 if (optarg
[0] == '{') {
252 obj
= qobject_from_json(optarg
, errp
);
256 v
= qobject_input_visitor_new(obj
);
258 QDict
*args
= keyval_parse(optarg
, "qom-type", &help
, errp
);
263 user_creatable_print_help_from_qdict(args
);
269 v
= qobject_input_visitor_new_keyval(obj
);
272 visit_type_ObjectOptions(v
, NULL
, &options
, errp
);
279 bool user_creatable_add_from_str(const char *optarg
, Error
**errp
)
282 ObjectOptions
*options
;
284 options
= user_creatable_parse_str(optarg
, errp
);
289 user_creatable_add_qapi(options
, errp
);
290 qapi_free_ObjectOptions(options
);
294 void user_creatable_process_cmdline(const char *optarg
)
296 if (!user_creatable_add_from_str(optarg
, &error_fatal
)) {
297 /* Help was printed */
302 bool user_creatable_del(const char *id
, Error
**errp
)
304 QemuOptsList
*opts_list
;
308 container
= object_get_objects_root();
309 obj
= object_resolve_path_component(container
, id
);
311 error_setg(errp
, "object '%s' not found", id
);
315 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
))) {
316 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
321 * if object was defined on the command-line, remove its corresponding
324 opts_list
= qemu_find_opts_err("object", NULL
);
326 qemu_opts_del(qemu_opts_find(opts_list
, id
));
329 object_unparent(obj
);
333 void user_creatable_cleanup(void)
335 object_unparent(object_get_objects_root());
338 static void register_types(void)
340 static const TypeInfo uc_interface_info
= {
341 .name
= TYPE_USER_CREATABLE
,
342 .parent
= TYPE_INTERFACE
,
343 .class_size
= sizeof(UserCreatableClass
),
346 type_register_static(&uc_interface_info
);
349 type_init(register_types
)