]>
Commit | Line | Data |
---|---|---|
9bbc853b | 1 | #include "qemu/osdep.h" |
3e9297f3 KW |
2 | |
3 | #include "qemu/cutils.h" | |
da34e65c | 4 | #include "qapi/error.h" |
f3750266 | 5 | #include "qapi/qapi-visit-qom.h" |
919a0423 | 6 | #include "qapi/qmp/qobject.h" |
452fcdbc | 7 | #include "qapi/qmp/qdict.h" |
0dd13589 | 8 | #include "qapi/qmp/qerror.h" |
4df81616 | 9 | #include "qapi/qmp/qjson.h" |
d6a5beeb | 10 | #include "qapi/qobject-input-visitor.h" |
f3750266 | 11 | #include "qapi/qobject-output-visitor.h" |
269e09f3 | 12 | #include "qom/object_interfaces.h" |
3e9297f3 | 13 | #include "qemu/help_option.h" |
0bd5a2eb | 14 | #include "qemu/id.h" |
269e09f3 | 15 | #include "qemu/module.h" |
922a01a0 | 16 | #include "qemu/option.h" |
da0a932b | 17 | #include "qemu/qemu-print.h" |
90998d58 | 18 | #include "qapi/opts-visitor.h" |
c645d5ac | 19 | #include "qemu/config-file.h" |
9ca9c893 | 20 | #include "qemu/keyval.h" |
269e09f3 | 21 | |
6fd5bef1 | 22 | bool user_creatable_complete(UserCreatable *uc, Error **errp) |
269e09f3 | 23 | { |
3650b2de | 24 | UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc); |
6fd5bef1 | 25 | Error *err = NULL; |
269e09f3 | 26 | |
269e09f3 | 27 | if (ucc->complete) { |
6fd5bef1 MA |
28 | ucc->complete(uc, &err); |
29 | error_propagate(errp, err); | |
269e09f3 | 30 | } |
6fd5bef1 | 31 | return !err; |
269e09f3 IM |
32 | } |
33 | ||
3beacfb9 | 34 | bool user_creatable_can_be_deleted(UserCreatable *uc) |
d6edb155 LM |
35 | { |
36 | ||
37 | UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc); | |
38 | ||
39 | if (ucc->can_be_deleted) { | |
3beacfb9 | 40 | return ucc->can_be_deleted(uc); |
d6edb155 LM |
41 | } else { |
42 | return true; | |
43 | } | |
44 | } | |
45 | ||
3bb69445 PB |
46 | static void object_set_properties_from_qdict(Object *obj, const QDict *qdict, |
47 | Visitor *v, Error **errp) | |
48 | { | |
49 | const QDictEntry *e; | |
3bb69445 | 50 | |
dbc8221f KW |
51 | if (!visit_start_struct(v, NULL, NULL, 0, errp)) { |
52 | return; | |
3bb69445 PB |
53 | } |
54 | for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) { | |
dbc8221f KW |
55 | if (!object_property_set(obj, e->key, v, errp)) { |
56 | goto out; | |
3bb69445 PB |
57 | } |
58 | } | |
dbc8221f | 59 | visit_check_struct(v, errp); |
3bb69445 | 60 | out: |
dbc8221f | 61 | visit_end_struct(v, NULL); |
3bb69445 PB |
62 | } |
63 | ||
64 | void object_set_properties_from_keyval(Object *obj, const QDict *qdict, | |
65 | bool from_json, Error **errp) | |
66 | { | |
67 | Visitor *v; | |
68 | if (from_json) { | |
69 | v = qobject_input_visitor_new(QOBJECT(qdict)); | |
70 | } else { | |
71 | v = qobject_input_visitor_new_keyval(QOBJECT(qdict)); | |
72 | } | |
73 | object_set_properties_from_qdict(obj, qdict, v, errp); | |
74 | visit_free(v); | |
75 | } | |
76 | ||
90998d58 DB |
77 | Object *user_creatable_add_type(const char *type, const char *id, |
78 | const QDict *qdict, | |
79 | Visitor *v, Error **errp) | |
80 | { | |
0bd5a2eb | 81 | ERRP_GUARD(); |
90998d58 DB |
82 | Object *obj; |
83 | ObjectClass *klass; | |
90998d58 DB |
84 | Error *local_err = NULL; |
85 | ||
0bd5a2eb KW |
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"); | |
90 | return NULL; | |
91 | } | |
92 | ||
90998d58 DB |
93 | klass = object_class_by_name(type); |
94 | if (!klass) { | |
95 | error_setg(errp, "invalid object type: %s", type); | |
96 | return NULL; | |
97 | } | |
98 | ||
99 | if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) { | |
100 | error_setg(errp, "object type '%s' isn't supported by object-add", | |
101 | type); | |
102 | return NULL; | |
103 | } | |
104 | ||
105 | if (object_class_is_abstract(klass)) { | |
106 | error_setg(errp, "object type '%s' is abstract", type); | |
107 | return NULL; | |
108 | } | |
109 | ||
ad739706 | 110 | assert(qdict); |
90998d58 | 111 | obj = object_new(type); |
3bb69445 | 112 | object_set_properties_from_qdict(obj, qdict, v, &local_err); |
ad739706 EB |
113 | if (local_err) { |
114 | goto out; | |
115 | } | |
90998d58 | 116 | |
6134d752 | 117 | if (id != NULL) { |
db57fef1 EA |
118 | object_property_try_add_child(object_get_objects_root(), |
119 | id, obj, &local_err); | |
120 | if (local_err) { | |
121 | goto out; | |
122 | } | |
90998d58 DB |
123 | } |
124 | ||
778a2dc5 | 125 | if (!user_creatable_complete(USER_CREATABLE(obj), &local_err)) { |
6134d752 | 126 | if (id != NULL) { |
df4fe0b2 | 127 | object_property_del(object_get_objects_root(), id); |
6134d752 | 128 | } |
90998d58 DB |
129 | goto out; |
130 | } | |
131 | out: | |
132 | if (local_err) { | |
133 | error_propagate(errp, local_err); | |
134 | object_unref(obj); | |
135 | return NULL; | |
136 | } | |
137 | return obj; | |
138 | } | |
139 | ||
f3750266 KW |
140 | void user_creatable_add_qapi(ObjectOptions *options, Error **errp) |
141 | { | |
142 | Visitor *v; | |
143 | QObject *qobj; | |
144 | QDict *props; | |
145 | Object *obj; | |
146 | ||
147 | v = qobject_output_visitor_new(&qobj); | |
148 | visit_type_ObjectOptions(v, NULL, &options, &error_abort); | |
149 | visit_complete(v, &qobj); | |
150 | visit_free(v); | |
151 | ||
152 | props = qobject_to(QDict, qobj); | |
153 | qdict_del(props, "qom-type"); | |
154 | qdict_del(props, "id"); | |
155 | ||
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); | |
159 | object_unref(obj); | |
160 | qobject_unref(qobj); | |
161 | visit_free(v); | |
162 | } | |
163 | ||
4df81616 MAL |
164 | char *object_property_help(const char *name, const char *type, |
165 | QObject *defval, const char *description) | |
166 | { | |
167 | GString *str = g_string_new(NULL); | |
168 | ||
169 | g_string_append_printf(str, " %s=<%s>", name, type); | |
170 | if (description || defval) { | |
171 | if (str->len < 24) { | |
172 | g_string_append_printf(str, "%*s", 24 - (int)str->len, ""); | |
173 | } | |
174 | g_string_append(str, " - "); | |
175 | } | |
176 | if (description) { | |
177 | g_string_append(str, description); | |
178 | } | |
179 | if (defval) { | |
eab3a467 | 180 | g_autofree char *def_json = g_string_free(qobject_to_json(defval), |
bd74ecd1 | 181 | false); |
4df81616 MAL |
182 | g_string_append_printf(str, " (default: %s)", def_json); |
183 | } | |
184 | ||
185 | return g_string_free(str, false); | |
186 | } | |
187 | ||
0e301d44 KW |
188 | static void user_creatable_print_types(void) |
189 | { | |
190 | GSList *l, *list; | |
191 | ||
da0a932b | 192 | qemu_printf("List of user creatable objects:\n"); |
0e301d44 KW |
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); | |
da0a932b | 196 | qemu_printf(" %s\n", object_class_get_name(oc)); |
0e301d44 KW |
197 | } |
198 | g_slist_free(list); | |
199 | } | |
200 | ||
3bb69445 | 201 | bool type_print_class_properties(const char *type) |
3e9297f3 KW |
202 | { |
203 | ObjectClass *klass; | |
0e301d44 KW |
204 | ObjectPropertyIterator iter; |
205 | ObjectProperty *prop; | |
206 | GPtrArray *array; | |
207 | int i; | |
3e9297f3 | 208 | |
0e301d44 KW |
209 | klass = object_class_by_name(type); |
210 | if (!klass) { | |
211 | return false; | |
212 | } | |
3e9297f3 | 213 | |
0e301d44 KW |
214 | array = g_ptr_array_new(); |
215 | object_class_property_iter_init(&iter, klass); | |
216 | while ((prop = object_property_iter_next(&iter))) { | |
217 | if (!prop->set) { | |
218 | continue; | |
3e9297f3 | 219 | } |
0e301d44 KW |
220 | |
221 | g_ptr_array_add(array, | |
222 | object_property_help(prop->name, prop->type, | |
223 | prop->defval, prop->description)); | |
3e9297f3 | 224 | } |
0e301d44 KW |
225 | g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0); |
226 | if (array->len > 0) { | |
da0a932b | 227 | qemu_printf("%s options:\n", type); |
0e301d44 | 228 | } else { |
da0a932b | 229 | qemu_printf("There are no options for %s.\n", type); |
0e301d44 KW |
230 | } |
231 | for (i = 0; i < array->len; i++) { | |
da0a932b | 232 | qemu_printf("%s\n", (char *)array->pdata[i]); |
0e301d44 KW |
233 | } |
234 | g_ptr_array_set_free_func(array, g_free); | |
235 | g_ptr_array_free(array, true); | |
236 | return true; | |
237 | } | |
3e9297f3 | 238 | |
0e301d44 KW |
239 | bool user_creatable_print_help(const char *type, QemuOpts *opts) |
240 | { | |
241 | if (is_help_option(type)) { | |
242 | user_creatable_print_types(); | |
3e9297f3 KW |
243 | return true; |
244 | } | |
245 | ||
0e301d44 | 246 | if (qemu_opt_has_help_opt(opts)) { |
3bb69445 | 247 | return type_print_class_properties(type); |
0e301d44 KW |
248 | } |
249 | ||
3e9297f3 KW |
250 | return false; |
251 | } | |
90998d58 | 252 | |
f3750266 | 253 | static void user_creatable_print_help_from_qdict(QDict *args) |
c9ac1458 KW |
254 | { |
255 | const char *type = qdict_get_try_str(args, "qom-type"); | |
256 | ||
3bb69445 | 257 | if (!type || !type_print_class_properties(type)) { |
c9ac1458 KW |
258 | user_creatable_print_types(); |
259 | } | |
260 | } | |
261 | ||
e0c7de8d | 262 | ObjectOptions *user_creatable_parse_str(const char *str, Error **errp) |
f3750266 | 263 | { |
ffd58ef8 | 264 | ERRP_GUARD(); |
155b5f8b | 265 | QObject *obj; |
f3750266 KW |
266 | bool help; |
267 | Visitor *v; | |
268 | ObjectOptions *options; | |
269 | ||
e0c7de8d PMD |
270 | if (str[0] == '{') { |
271 | obj = qobject_from_json(str, errp); | |
155b5f8b KW |
272 | if (!obj) { |
273 | return NULL; | |
274 | } | |
275 | v = qobject_input_visitor_new(obj); | |
276 | } else { | |
e0c7de8d | 277 | QDict *args = keyval_parse(str, "qom-type", &help, errp); |
155b5f8b KW |
278 | if (*errp) { |
279 | return NULL; | |
280 | } | |
281 | if (help) { | |
282 | user_creatable_print_help_from_qdict(args); | |
283 | qobject_unref(args); | |
284 | return NULL; | |
285 | } | |
286 | ||
287 | obj = QOBJECT(args); | |
288 | v = qobject_input_visitor_new_keyval(obj); | |
f3750266 KW |
289 | } |
290 | ||
ffd58ef8 | 291 | visit_type_ObjectOptions(v, NULL, &options, errp); |
f3750266 | 292 | visit_free(v); |
155b5f8b | 293 | qobject_unref(obj); |
f3750266 | 294 | |
ddf6dae7 KW |
295 | return options; |
296 | } | |
297 | ||
e0c7de8d | 298 | bool user_creatable_add_from_str(const char *str, Error **errp) |
ddf6dae7 KW |
299 | { |
300 | ERRP_GUARD(); | |
301 | ObjectOptions *options; | |
302 | ||
e0c7de8d | 303 | options = user_creatable_parse_str(str, errp); |
ddf6dae7 KW |
304 | if (!options) { |
305 | return false; | |
ffd58ef8 KW |
306 | } |
307 | ||
308 | user_creatable_add_qapi(options, errp); | |
f3750266 | 309 | qapi_free_ObjectOptions(options); |
ffd58ef8 KW |
310 | return !*errp; |
311 | } | |
312 | ||
e0c7de8d | 313 | void user_creatable_process_cmdline(const char *cmdline) |
ffd58ef8 | 314 | { |
e0c7de8d | 315 | if (!user_creatable_add_from_str(cmdline, &error_fatal)) { |
ffd58ef8 KW |
316 | /* Help was printed */ |
317 | exit(EXIT_SUCCESS); | |
318 | } | |
f3750266 KW |
319 | } |
320 | ||
6fd5bef1 | 321 | bool user_creatable_del(const char *id, Error **errp) |
90998d58 | 322 | { |
98c43b7b | 323 | QemuOptsList *opts_list; |
90998d58 DB |
324 | Object *container; |
325 | Object *obj; | |
326 | ||
327 | container = object_get_objects_root(); | |
328 | obj = object_resolve_path_component(container, id); | |
329 | if (!obj) { | |
330 | error_setg(errp, "object '%s' not found", id); | |
6fd5bef1 | 331 | return false; |
90998d58 DB |
332 | } |
333 | ||
3beacfb9 | 334 | if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) { |
90998d58 | 335 | error_setg(errp, "object '%s' is in use, can not be deleted", id); |
6fd5bef1 | 336 | return false; |
90998d58 | 337 | } |
c645d5ac MR |
338 | |
339 | /* | |
340 | * if object was defined on the command-line, remove its corresponding | |
341 | * option group entry | |
342 | */ | |
98c43b7b KW |
343 | opts_list = qemu_find_opts_err("object", NULL); |
344 | if (opts_list) { | |
345 | qemu_opts_del(qemu_opts_find(opts_list, id)); | |
346 | } | |
c645d5ac | 347 | |
90998d58 | 348 | object_unparent(obj); |
6fd5bef1 | 349 | return true; |
90998d58 DB |
350 | } |
351 | ||
9d5139e5 EH |
352 | void user_creatable_cleanup(void) |
353 | { | |
354 | object_unparent(object_get_objects_root()); | |
355 | } | |
356 | ||
269e09f3 IM |
357 | static void register_types(void) |
358 | { | |
359 | static const TypeInfo uc_interface_info = { | |
360 | .name = TYPE_USER_CREATABLE, | |
361 | .parent = TYPE_INTERFACE, | |
362 | .class_size = sizeof(UserCreatableClass), | |
363 | }; | |
364 | ||
365 | type_register_static(&uc_interface_info); | |
366 | } | |
367 | ||
368 | type_init(register_types) |