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