xref: /openbmc/qemu/qom/object_interfaces.c (revision 1da79ecc)
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/qobject-input-visitor.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"
15 
16 bool user_creatable_complete(UserCreatable *uc, Error **errp)
17 {
18     UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
19     Error *err = NULL;
20 
21     if (ucc->complete) {
22         ucc->complete(uc, &err);
23         error_propagate(errp, err);
24     }
25     return !err;
26 }
27 
28 bool user_creatable_can_be_deleted(UserCreatable *uc)
29 {
30 
31     UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
32 
33     if (ucc->can_be_deleted) {
34         return ucc->can_be_deleted(uc);
35     } else {
36         return true;
37     }
38 }
39 
40 Object *user_creatable_add_type(const char *type, const char *id,
41                                 const QDict *qdict,
42                                 Visitor *v, Error **errp)
43 {
44     Object *obj;
45     ObjectClass *klass;
46     const QDictEntry *e;
47     Error *local_err = NULL;
48 
49     klass = object_class_by_name(type);
50     if (!klass) {
51         error_setg(errp, "invalid object type: %s", type);
52         return NULL;
53     }
54 
55     if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
56         error_setg(errp, "object type '%s' isn't supported by object-add",
57                    type);
58         return NULL;
59     }
60 
61     if (object_class_is_abstract(klass)) {
62         error_setg(errp, "object type '%s' is abstract", type);
63         return NULL;
64     }
65 
66     assert(qdict);
67     obj = object_new(type);
68     if (!visit_start_struct(v, NULL, NULL, 0, &local_err)) {
69         goto out;
70     }
71     for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
72         if (!object_property_set(obj, e->key, v, &local_err)) {
73             break;
74         }
75     }
76     if (!local_err) {
77         visit_check_struct(v, &local_err);
78     }
79     visit_end_struct(v, NULL);
80     if (local_err) {
81         goto out;
82     }
83 
84     if (id != NULL) {
85         object_property_try_add_child(object_get_objects_root(),
86                                       id, obj, &local_err);
87         if (local_err) {
88             goto out;
89         }
90     }
91 
92     if (!user_creatable_complete(USER_CREATABLE(obj), &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 = g_string_free(qobject_to_json(defval),
210                                                   true);
211         g_string_append_printf(str, " (default: %s)", def_json);
212     }
213 
214     return g_string_free(str, false);
215 }
216 
217 static void user_creatable_print_types(void)
218 {
219     GSList *l, *list;
220 
221     printf("List of user creatable objects:\n");
222     list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
223     for (l = list; l != NULL; l = l->next) {
224         ObjectClass *oc = OBJECT_CLASS(l->data);
225         printf("  %s\n", object_class_get_name(oc));
226     }
227     g_slist_free(list);
228 }
229 
230 static bool user_creatable_print_type_properites(const char *type)
231 {
232     ObjectClass *klass;
233     ObjectPropertyIterator iter;
234     ObjectProperty *prop;
235     GPtrArray *array;
236     int i;
237 
238     klass = object_class_by_name(type);
239     if (!klass) {
240         return false;
241     }
242 
243     array = g_ptr_array_new();
244     object_class_property_iter_init(&iter, klass);
245     while ((prop = object_property_iter_next(&iter))) {
246         if (!prop->set) {
247             continue;
248         }
249 
250         g_ptr_array_add(array,
251                         object_property_help(prop->name, prop->type,
252                                              prop->defval, prop->description));
253     }
254     g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
255     if (array->len > 0) {
256         printf("%s options:\n", type);
257     } else {
258         printf("There are no options for %s.\n", type);
259     }
260     for (i = 0; i < array->len; i++) {
261         printf("%s\n", (char *)array->pdata[i]);
262     }
263     g_ptr_array_set_free_func(array, g_free);
264     g_ptr_array_free(array, true);
265     return true;
266 }
267 
268 bool user_creatable_print_help(const char *type, QemuOpts *opts)
269 {
270     if (is_help_option(type)) {
271         user_creatable_print_types();
272         return true;
273     }
274 
275     if (qemu_opt_has_help_opt(opts)) {
276         return user_creatable_print_type_properites(type);
277     }
278 
279     return false;
280 }
281 
282 void user_creatable_print_help_from_qdict(QDict *args)
283 {
284     const char *type = qdict_get_try_str(args, "qom-type");
285 
286     if (!type || !user_creatable_print_type_properites(type)) {
287         user_creatable_print_types();
288     }
289 }
290 
291 bool user_creatable_del(const char *id, Error **errp)
292 {
293     Object *container;
294     Object *obj;
295 
296     container = object_get_objects_root();
297     obj = object_resolve_path_component(container, id);
298     if (!obj) {
299         error_setg(errp, "object '%s' not found", id);
300         return false;
301     }
302 
303     if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
304         error_setg(errp, "object '%s' is in use, can not be deleted", id);
305         return false;
306     }
307 
308     /*
309      * if object was defined on the command-line, remove its corresponding
310      * option group entry
311      */
312     qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort),
313                                  id));
314 
315     object_unparent(obj);
316     return true;
317 }
318 
319 void user_creatable_cleanup(void)
320 {
321     object_unparent(object_get_objects_root());
322 }
323 
324 static void register_types(void)
325 {
326     static const TypeInfo uc_interface_info = {
327         .name          = TYPE_USER_CREATABLE,
328         .parent        = TYPE_INTERFACE,
329         .class_size = sizeof(UserCreatableClass),
330     };
331 
332     type_register_static(&uc_interface_info);
333 }
334 
335 type_init(register_types)
336