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 "qapi/qmp/qjson.h"
8 #include "qapi/qmp/qstring.h"
9 #include "qom/object_interfaces.h"
10 #include "qemu/help_option.h"
11 #include "qemu/module.h"
12 #include "qemu/option.h"
13 #include "qapi/opts-visitor.h"
14 #include "qemu/config-file.h"
16 void user_creatable_complete(UserCreatable
*uc
, Error
**errp
)
18 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
21 ucc
->complete(uc
, errp
);
25 bool user_creatable_can_be_deleted(UserCreatable
*uc
)
28 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
30 if (ucc
->can_be_deleted
) {
31 return ucc
->can_be_deleted(uc
);
37 Object
*user_creatable_add_type(const char *type
, const char *id
,
39 Visitor
*v
, Error
**errp
)
44 Error
*local_err
= NULL
;
46 klass
= object_class_by_name(type
);
48 error_setg(errp
, "invalid object type: %s", type
);
52 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
53 error_setg(errp
, "object type '%s' isn't supported by object-add",
58 if (object_class_is_abstract(klass
)) {
59 error_setg(errp
, "object type '%s' is abstract", type
);
64 obj
= object_new(type
);
65 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
69 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
70 object_property_set(obj
, v
, e
->key
, &local_err
);
76 visit_check_struct(v
, &local_err
);
78 visit_end_struct(v
, NULL
);
84 object_property_add_child(object_get_objects_root(),
91 user_creatable_complete(USER_CREATABLE(obj
), &local_err
);
94 object_property_del(object_get_objects_root(),
101 error_propagate(errp
, local_err
);
109 Object
*user_creatable_add_opts(QemuOpts
*opts
, Error
**errp
)
114 const char *id
= qemu_opts_id(opts
);
115 char *type
= qemu_opt_get_del(opts
, "qom-type");
118 error_setg(errp
, QERR_MISSING_PARAMETER
, "qom-type");
122 error_setg(errp
, QERR_MISSING_PARAMETER
, "id");
123 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
128 qemu_opts_set_id(opts
, NULL
);
129 pdict
= qemu_opts_to_qdict(opts
, NULL
);
131 v
= opts_visitor_new(opts
);
132 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
135 qemu_opts_set_id(opts
, (char *) id
);
136 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
138 qobject_unref(pdict
);
143 int user_creatable_add_opts_foreach(void *opaque
, QemuOpts
*opts
, Error
**errp
)
145 bool (*type_opt_predicate
)(const char *, QemuOpts
*) = opaque
;
149 type
= qemu_opt_get(opts
, "qom-type");
150 if (type
&& type_opt_predicate
&&
151 !type_opt_predicate(type
, opts
)) {
155 obj
= user_creatable_add_opts(opts
, errp
);
163 char *object_property_help(const char *name
, const char *type
,
164 QObject
*defval
, const char *description
)
166 GString
*str
= g_string_new(NULL
);
168 g_string_append_printf(str
, " %s=<%s>", name
, type
);
169 if (description
|| defval
) {
171 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
173 g_string_append(str
, " - ");
176 g_string_append(str
, description
);
179 g_autofree
char *def_json
= qstring_free(qobject_to_json(defval
), TRUE
);
180 g_string_append_printf(str
, " (default: %s)", def_json
);
183 return g_string_free(str
, false);
186 bool user_creatable_print_help(const char *type
, QemuOpts
*opts
)
190 if (is_help_option(type
)) {
193 printf("List of user creatable objects:\n");
194 list
= object_class_get_list_sorted(TYPE_USER_CREATABLE
, false);
195 for (l
= list
; l
!= NULL
; l
= l
->next
) {
196 ObjectClass
*oc
= OBJECT_CLASS(l
->data
);
197 printf(" %s\n", object_class_get_name(oc
));
203 klass
= object_class_by_name(type
);
204 if (klass
&& qemu_opt_has_help_opt(opts
)) {
205 ObjectPropertyIterator iter
;
206 ObjectProperty
*prop
;
207 GPtrArray
*array
= g_ptr_array_new();
210 object_class_property_iter_init(&iter
, klass
);
211 while ((prop
= object_property_iter_next(&iter
))) {
216 g_ptr_array_add(array
,
217 object_property_help(prop
->name
, prop
->type
,
218 prop
->defval
, prop
->description
));
220 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
221 if (array
->len
> 0) {
222 printf("%s options:\n", type
);
224 printf("There are no options for %s.\n", type
);
226 for (i
= 0; i
< array
->len
; i
++) {
227 printf("%s\n", (char *)array
->pdata
[i
]);
229 g_ptr_array_set_free_func(array
, g_free
);
230 g_ptr_array_free(array
, true);
237 void user_creatable_del(const char *id
, Error
**errp
)
242 container
= object_get_objects_root();
243 obj
= object_resolve_path_component(container
, id
);
245 error_setg(errp
, "object '%s' not found", id
);
249 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
))) {
250 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
255 * if object was defined on the command-line, remove its corresponding
258 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort
),
261 object_unparent(obj
);
264 void user_creatable_cleanup(void)
266 object_unparent(object_get_objects_root());
269 static void register_types(void)
271 static const TypeInfo uc_interface_info
= {
272 .name
= TYPE_USER_CREATABLE
,
273 .parent
= TYPE_INTERFACE
,
274 .class_size
= sizeof(UserCreatableClass
),
277 type_register_static(&uc_interface_info
);
280 type_init(register_types
)