]>
Commit | Line | Data |
---|---|---|
9bbc853b | 1 | #include "qemu/osdep.h" |
3e9297f3 KW |
2 | |
3 | #include "qemu/cutils.h" | |
da34e65c | 4 | #include "qapi/error.h" |
452fcdbc | 5 | #include "qapi/qmp/qdict.h" |
0dd13589 | 6 | #include "qapi/qmp/qerror.h" |
4df81616 MAL |
7 | #include "qapi/qmp/qjson.h" |
8 | #include "qapi/qmp/qstring.h" | |
269e09f3 | 9 | #include "qom/object_interfaces.h" |
3e9297f3 | 10 | #include "qemu/help_option.h" |
269e09f3 | 11 | #include "qemu/module.h" |
922a01a0 | 12 | #include "qemu/option.h" |
90998d58 | 13 | #include "qapi/opts-visitor.h" |
c645d5ac | 14 | #include "qemu/config-file.h" |
269e09f3 | 15 | |
3650b2de | 16 | void user_creatable_complete(UserCreatable *uc, Error **errp) |
269e09f3 | 17 | { |
3650b2de | 18 | UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc); |
269e09f3 | 19 | |
269e09f3 IM |
20 | if (ucc->complete) { |
21 | ucc->complete(uc, errp); | |
22 | } | |
23 | } | |
24 | ||
3beacfb9 | 25 | bool user_creatable_can_be_deleted(UserCreatable *uc) |
d6edb155 LM |
26 | { |
27 | ||
28 | UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc); | |
29 | ||
30 | if (ucc->can_be_deleted) { | |
3beacfb9 | 31 | return ucc->can_be_deleted(uc); |
d6edb155 LM |
32 | } else { |
33 | return true; | |
34 | } | |
35 | } | |
36 | ||
90998d58 DB |
37 | Object *user_creatable_add_type(const char *type, const char *id, |
38 | const QDict *qdict, | |
39 | Visitor *v, Error **errp) | |
40 | { | |
41 | Object *obj; | |
42 | ObjectClass *klass; | |
43 | const QDictEntry *e; | |
44 | Error *local_err = NULL; | |
45 | ||
46 | klass = object_class_by_name(type); | |
47 | if (!klass) { | |
48 | error_setg(errp, "invalid object type: %s", type); | |
49 | return NULL; | |
50 | } | |
51 | ||
52 | if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) { | |
53 | error_setg(errp, "object type '%s' isn't supported by object-add", | |
54 | type); | |
55 | return NULL; | |
56 | } | |
57 | ||
58 | if (object_class_is_abstract(klass)) { | |
59 | error_setg(errp, "object type '%s' is abstract", type); | |
60 | return NULL; | |
61 | } | |
62 | ||
ad739706 | 63 | assert(qdict); |
90998d58 | 64 | obj = object_new(type); |
ad739706 EB |
65 | visit_start_struct(v, NULL, NULL, 0, &local_err); |
66 | if (local_err) { | |
67 | goto out; | |
68 | } | |
69 | for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) { | |
70 | object_property_set(obj, v, e->key, &local_err); | |
71 | if (local_err) { | |
72 | break; | |
90998d58 DB |
73 | } |
74 | } | |
15c2f669 EB |
75 | if (!local_err) { |
76 | visit_check_struct(v, &local_err); | |
77 | } | |
1158bb2a | 78 | visit_end_struct(v, NULL); |
ad739706 EB |
79 | if (local_err) { |
80 | goto out; | |
81 | } | |
90998d58 | 82 | |
6134d752 DB |
83 | if (id != NULL) { |
84 | object_property_add_child(object_get_objects_root(), | |
85 | id, obj, &local_err); | |
86 | if (local_err) { | |
87 | goto out; | |
88 | } | |
90998d58 DB |
89 | } |
90 | ||
3650b2de | 91 | user_creatable_complete(USER_CREATABLE(obj), &local_err); |
90998d58 | 92 | if (local_err) { |
6134d752 DB |
93 | if (id != NULL) { |
94 | object_property_del(object_get_objects_root(), | |
95 | id, &error_abort); | |
96 | } | |
90998d58 DB |
97 | goto out; |
98 | } | |
99 | out: | |
100 | if (local_err) { | |
101 | error_propagate(errp, local_err); | |
102 | object_unref(obj); | |
103 | return NULL; | |
104 | } | |
105 | return obj; | |
106 | } | |
107 | ||
108 | ||
109 | Object *user_creatable_add_opts(QemuOpts *opts, Error **errp) | |
110 | { | |
09204eac | 111 | Visitor *v; |
90998d58 | 112 | QDict *pdict; |
3a464105 IM |
113 | Object *obj; |
114 | const char *id = qemu_opts_id(opts); | |
9a6d1acb | 115 | char *type = qemu_opt_get_del(opts, "qom-type"); |
3a464105 IM |
116 | |
117 | if (!type) { | |
118 | error_setg(errp, QERR_MISSING_PARAMETER, "qom-type"); | |
119 | return NULL; | |
120 | } | |
121 | if (!id) { | |
122 | error_setg(errp, QERR_MISSING_PARAMETER, "id"); | |
08329701 | 123 | qemu_opt_set(opts, "qom-type", type, &error_abort); |
9a6d1acb | 124 | g_free(type); |
3a464105 IM |
125 | return NULL; |
126 | } | |
90998d58 | 127 | |
9a6d1acb | 128 | qemu_opts_set_id(opts, NULL); |
90998d58 DB |
129 | pdict = qemu_opts_to_qdict(opts, NULL); |
130 | ||
3a464105 IM |
131 | v = opts_visitor_new(opts); |
132 | obj = user_creatable_add_type(type, id, pdict, v, errp); | |
09204eac | 133 | visit_free(v); |
3a464105 | 134 | |
9a6d1acb | 135 | qemu_opts_set_id(opts, (char *) id); |
08329701 | 136 | qemu_opt_set(opts, "qom-type", type, &error_abort); |
9a6d1acb | 137 | g_free(type); |
cb3e7f08 | 138 | qobject_unref(pdict); |
90998d58 DB |
139 | return obj; |
140 | } | |
141 | ||
142 | ||
143 | int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp) | |
144 | { | |
1195fa2b | 145 | bool (*type_opt_predicate)(const char *, QemuOpts *) = opaque; |
90998d58 DB |
146 | Object *obj = NULL; |
147 | const char *type; | |
148 | ||
149 | type = qemu_opt_get(opts, "qom-type"); | |
1195fa2b MAL |
150 | if (type && type_opt_predicate && |
151 | !type_opt_predicate(type, opts)) { | |
90998d58 DB |
152 | return 0; |
153 | } | |
154 | ||
7e1e0c11 | 155 | obj = user_creatable_add_opts(opts, errp); |
90998d58 DB |
156 | if (!obj) { |
157 | return -1; | |
158 | } | |
159 | object_unref(obj); | |
160 | return 0; | |
161 | } | |
162 | ||
4df81616 MAL |
163 | char *object_property_help(const char *name, const char *type, |
164 | QObject *defval, const char *description) | |
165 | { | |
166 | GString *str = g_string_new(NULL); | |
167 | ||
168 | g_string_append_printf(str, " %s=<%s>", name, type); | |
169 | if (description || defval) { | |
170 | if (str->len < 24) { | |
171 | g_string_append_printf(str, "%*s", 24 - (int)str->len, ""); | |
172 | } | |
173 | g_string_append(str, " - "); | |
174 | } | |
175 | if (description) { | |
176 | g_string_append(str, description); | |
177 | } | |
178 | if (defval) { | |
179 | g_autofree char *def_json = qstring_free(qobject_to_json(defval), TRUE); | |
180 | g_string_append_printf(str, " (default: %s)", def_json); | |
181 | } | |
182 | ||
183 | return g_string_free(str, false); | |
184 | } | |
185 | ||
3e9297f3 KW |
186 | bool user_creatable_print_help(const char *type, QemuOpts *opts) |
187 | { | |
188 | ObjectClass *klass; | |
189 | ||
190 | if (is_help_option(type)) { | |
191 | GSList *l, *list; | |
192 | ||
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)); | |
198 | } | |
199 | g_slist_free(list); | |
200 | return true; | |
201 | } | |
202 | ||
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(); | |
208 | int i; | |
209 | ||
210 | object_class_property_iter_init(&iter, klass); | |
211 | while ((prop = object_property_iter_next(&iter))) { | |
3e9297f3 KW |
212 | if (!prop->set) { |
213 | continue; | |
214 | } | |
215 | ||
4df81616 MAL |
216 | g_ptr_array_add(array, |
217 | object_property_help(prop->name, prop->type, | |
218 | prop->defval, prop->description)); | |
3e9297f3 KW |
219 | } |
220 | g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0); | |
221 | if (array->len > 0) { | |
222 | printf("%s options:\n", type); | |
223 | } else { | |
224 | printf("There are no options for %s.\n", type); | |
225 | } | |
226 | for (i = 0; i < array->len; i++) { | |
227 | printf("%s\n", (char *)array->pdata[i]); | |
228 | } | |
229 | g_ptr_array_set_free_func(array, g_free); | |
230 | g_ptr_array_free(array, true); | |
231 | return true; | |
232 | } | |
233 | ||
234 | return false; | |
235 | } | |
90998d58 DB |
236 | |
237 | void user_creatable_del(const char *id, Error **errp) | |
238 | { | |
239 | Object *container; | |
240 | Object *obj; | |
241 | ||
242 | container = object_get_objects_root(); | |
243 | obj = object_resolve_path_component(container, id); | |
244 | if (!obj) { | |
245 | error_setg(errp, "object '%s' not found", id); | |
246 | return; | |
247 | } | |
248 | ||
3beacfb9 | 249 | if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) { |
90998d58 DB |
250 | error_setg(errp, "object '%s' is in use, can not be deleted", id); |
251 | return; | |
252 | } | |
c645d5ac MR |
253 | |
254 | /* | |
255 | * if object was defined on the command-line, remove its corresponding | |
256 | * option group entry | |
257 | */ | |
258 | qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort), | |
259 | id)); | |
260 | ||
90998d58 DB |
261 | object_unparent(obj); |
262 | } | |
263 | ||
9d5139e5 EH |
264 | void user_creatable_cleanup(void) |
265 | { | |
266 | object_unparent(object_get_objects_root()); | |
267 | } | |
268 | ||
269e09f3 IM |
269 | static void register_types(void) |
270 | { | |
271 | static const TypeInfo uc_interface_info = { | |
272 | .name = TYPE_USER_CREATABLE, | |
273 | .parent = TYPE_INTERFACE, | |
274 | .class_size = sizeof(UserCreatableClass), | |
275 | }; | |
276 | ||
277 | type_register_static(&uc_interface_info); | |
278 | } | |
279 | ||
280 | type_init(register_types) |