s390x/pci: Fix harmless mistake in zpci's property fid's setter
[qemu.git] / qom / object_interfaces.c
1 #include "qemu/osdep.h"
2
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 "qapi/qobject-input-visitor.h"
10 #include "qom/object_interfaces.h"
11 #include "qemu/help_option.h"
12 #include "qemu/module.h"
13 #include "qemu/option.h"
14 #include "qapi/opts-visitor.h"
15 #include "qemu/config-file.h"
16
17 void user_creatable_complete(UserCreatable *uc, Error **errp)
18 {
19 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
20
21 if (ucc->complete) {
22 ucc->complete(uc, errp);
23 }
24 }
25
26 bool user_creatable_can_be_deleted(UserCreatable *uc)
27 {
28
29 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
30
31 if (ucc->can_be_deleted) {
32 return ucc->can_be_deleted(uc);
33 } else {
34 return true;
35 }
36 }
37
38 Object *user_creatable_add_type(const char *type, const char *id,
39 const QDict *qdict,
40 Visitor *v, Error **errp)
41 {
42 Object *obj;
43 ObjectClass *klass;
44 const QDictEntry *e;
45 Error *local_err = NULL;
46
47 klass = object_class_by_name(type);
48 if (!klass) {
49 error_setg(errp, "invalid object type: %s", type);
50 return NULL;
51 }
52
53 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
54 error_setg(errp, "object type '%s' isn't supported by object-add",
55 type);
56 return NULL;
57 }
58
59 if (object_class_is_abstract(klass)) {
60 error_setg(errp, "object type '%s' is abstract", type);
61 return NULL;
62 }
63
64 assert(qdict);
65 obj = object_new(type);
66 if (!visit_start_struct(v, NULL, NULL, 0, &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;
73 }
74 }
75 if (!local_err) {
76 visit_check_struct(v, &local_err);
77 }
78 visit_end_struct(v, NULL);
79 if (local_err) {
80 goto out;
81 }
82
83 if (id != NULL) {
84 object_property_add_child(object_get_objects_root(),
85 id, obj);
86 }
87
88 user_creatable_complete(USER_CREATABLE(obj), &local_err);
89 if (local_err) {
90 if (id != NULL) {
91 object_property_del(object_get_objects_root(), id);
92 }
93 goto out;
94 }
95 out:
96 if (local_err) {
97 error_propagate(errp, local_err);
98 object_unref(obj);
99 return NULL;
100 }
101 return obj;
102 }
103
104 void user_creatable_add_dict(QDict *qdict, bool keyval, Error **errp)
105 {
106 Visitor *v;
107 Object *obj;
108 g_autofree char *type = NULL;
109 g_autofree char *id = NULL;
110
111 type = g_strdup(qdict_get_try_str(qdict, "qom-type"));
112 if (!type) {
113 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
114 return;
115 }
116 qdict_del(qdict, "qom-type");
117
118 id = g_strdup(qdict_get_try_str(qdict, "id"));
119 if (!id) {
120 error_setg(errp, QERR_MISSING_PARAMETER, "id");
121 return;
122 }
123 qdict_del(qdict, "id");
124
125 if (keyval) {
126 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
127 } else {
128 v = qobject_input_visitor_new(QOBJECT(qdict));
129 }
130 obj = user_creatable_add_type(type, id, qdict, v, errp);
131 visit_free(v);
132 object_unref(obj);
133 }
134
135 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
136 {
137 Visitor *v;
138 QDict *pdict;
139 Object *obj;
140 const char *id = qemu_opts_id(opts);
141 char *type = qemu_opt_get_del(opts, "qom-type");
142
143 if (!type) {
144 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
145 return NULL;
146 }
147 if (!id) {
148 error_setg(errp, QERR_MISSING_PARAMETER, "id");
149 qemu_opt_set(opts, "qom-type", type, &error_abort);
150 g_free(type);
151 return NULL;
152 }
153
154 qemu_opts_set_id(opts, NULL);
155 pdict = qemu_opts_to_qdict(opts, NULL);
156
157 v = opts_visitor_new(opts);
158 obj = user_creatable_add_type(type, id, pdict, v, errp);
159 visit_free(v);
160
161 qemu_opts_set_id(opts, (char *) id);
162 qemu_opt_set(opts, "qom-type", type, &error_abort);
163 g_free(type);
164 qobject_unref(pdict);
165 return obj;
166 }
167
168
169 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
170 {
171 bool (*type_opt_predicate)(const char *, QemuOpts *) = opaque;
172 Object *obj = NULL;
173 const char *type;
174
175 type = qemu_opt_get(opts, "qom-type");
176 if (type && type_opt_predicate &&
177 !type_opt_predicate(type, opts)) {
178 return 0;
179 }
180
181 obj = user_creatable_add_opts(opts, errp);
182 if (!obj) {
183 return -1;
184 }
185 object_unref(obj);
186 return 0;
187 }
188
189 char *object_property_help(const char *name, const char *type,
190 QObject *defval, const char *description)
191 {
192 GString *str = g_string_new(NULL);
193
194 g_string_append_printf(str, " %s=<%s>", name, type);
195 if (description || defval) {
196 if (str->len < 24) {
197 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
198 }
199 g_string_append(str, " - ");
200 }
201 if (description) {
202 g_string_append(str, description);
203 }
204 if (defval) {
205 g_autofree char *def_json = qstring_free(qobject_to_json(defval), TRUE);
206 g_string_append_printf(str, " (default: %s)", def_json);
207 }
208
209 return g_string_free(str, false);
210 }
211
212 bool user_creatable_print_help(const char *type, QemuOpts *opts)
213 {
214 ObjectClass *klass;
215
216 if (is_help_option(type)) {
217 GSList *l, *list;
218
219 printf("List of user creatable objects:\n");
220 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
221 for (l = list; l != NULL; l = l->next) {
222 ObjectClass *oc = OBJECT_CLASS(l->data);
223 printf(" %s\n", object_class_get_name(oc));
224 }
225 g_slist_free(list);
226 return true;
227 }
228
229 klass = object_class_by_name(type);
230 if (klass && qemu_opt_has_help_opt(opts)) {
231 ObjectPropertyIterator iter;
232 ObjectProperty *prop;
233 GPtrArray *array = g_ptr_array_new();
234 int i;
235
236 object_class_property_iter_init(&iter, klass);
237 while ((prop = object_property_iter_next(&iter))) {
238 if (!prop->set) {
239 continue;
240 }
241
242 g_ptr_array_add(array,
243 object_property_help(prop->name, prop->type,
244 prop->defval, prop->description));
245 }
246 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
247 if (array->len > 0) {
248 printf("%s options:\n", type);
249 } else {
250 printf("There are no options for %s.\n", type);
251 }
252 for (i = 0; i < array->len; i++) {
253 printf("%s\n", (char *)array->pdata[i]);
254 }
255 g_ptr_array_set_free_func(array, g_free);
256 g_ptr_array_free(array, true);
257 return true;
258 }
259
260 return false;
261 }
262
263 void user_creatable_del(const char *id, Error **errp)
264 {
265 Object *container;
266 Object *obj;
267
268 container = object_get_objects_root();
269 obj = object_resolve_path_component(container, id);
270 if (!obj) {
271 error_setg(errp, "object '%s' not found", id);
272 return;
273 }
274
275 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
276 error_setg(errp, "object '%s' is in use, can not be deleted", id);
277 return;
278 }
279
280 /*
281 * if object was defined on the command-line, remove its corresponding
282 * option group entry
283 */
284 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort),
285 id));
286
287 object_unparent(obj);
288 }
289
290 void user_creatable_cleanup(void)
291 {
292 object_unparent(object_get_objects_root());
293 }
294
295 static void register_types(void)
296 {
297 static const TypeInfo uc_interface_info = {
298 .name = TYPE_USER_CREATABLE,
299 .parent = TYPE_INTERFACE,
300 .class_size = sizeof(UserCreatableClass),
301 };
302
303 type_register_static(&uc_interface_info);
304 }
305
306 type_init(register_types)