xref: /openbmc/qemu/qom/object_interfaces.c (revision e6b5a071)
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         if (!object_property_set(obj, e->key, v, &local_err)) {
74             break;
75         }
76     }
77     if (!local_err) {
78         visit_check_struct(v, &local_err);
79     }
80     visit_end_struct(v, NULL);
81     if (local_err) {
82         goto out;
83     }
84 
85     if (id != NULL) {
86         object_property_try_add_child(object_get_objects_root(),
87                                       id, obj, &local_err);
88         if (local_err) {
89             goto out;
90         }
91     }
92 
93     if (!user_creatable_complete(USER_CREATABLE(obj), &local_err)) {
94         if (id != NULL) {
95             object_property_del(object_get_objects_root(), id);
96         }
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 bool user_creatable_add_dict(QDict *qdict, bool keyval, Error **errp)
109 {
110     Visitor *v;
111     Object *obj;
112     g_autofree char *type = NULL;
113     g_autofree char *id = NULL;
114 
115     type = g_strdup(qdict_get_try_str(qdict, "qom-type"));
116     if (!type) {
117         error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
118         return false;
119     }
120     qdict_del(qdict, "qom-type");
121 
122     id = g_strdup(qdict_get_try_str(qdict, "id"));
123     if (!id) {
124         error_setg(errp, QERR_MISSING_PARAMETER, "id");
125         return false;
126     }
127     qdict_del(qdict, "id");
128 
129     if (keyval) {
130         v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
131     } else {
132         v = qobject_input_visitor_new(QOBJECT(qdict));
133     }
134     obj = user_creatable_add_type(type, id, qdict, v, errp);
135     visit_free(v);
136     object_unref(obj);
137     return !!obj;
138 }
139 
140 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
141 {
142     Visitor *v;
143     QDict *pdict;
144     Object *obj;
145     const char *id = qemu_opts_id(opts);
146     char *type = qemu_opt_get_del(opts, "qom-type");
147 
148     if (!type) {
149         error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
150         return NULL;
151     }
152     if (!id) {
153         error_setg(errp, QERR_MISSING_PARAMETER, "id");
154         qemu_opt_set(opts, "qom-type", type, &error_abort);
155         g_free(type);
156         return NULL;
157     }
158 
159     qemu_opts_set_id(opts, NULL);
160     pdict = qemu_opts_to_qdict(opts, NULL);
161 
162     v = opts_visitor_new(opts);
163     obj = user_creatable_add_type(type, id, pdict, v, errp);
164     visit_free(v);
165 
166     qemu_opts_set_id(opts, (char *) id);
167     qemu_opt_set(opts, "qom-type", type, &error_abort);
168     g_free(type);
169     qobject_unref(pdict);
170     return obj;
171 }
172 
173 
174 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
175 {
176     bool (*type_opt_predicate)(const char *, QemuOpts *) = opaque;
177     Object *obj = NULL;
178     const char *type;
179 
180     type = qemu_opt_get(opts, "qom-type");
181     if (type && type_opt_predicate &&
182         !type_opt_predicate(type, opts)) {
183         return 0;
184     }
185 
186     obj = user_creatable_add_opts(opts, errp);
187     if (!obj) {
188         return -1;
189     }
190     object_unref(obj);
191     return 0;
192 }
193 
194 char *object_property_help(const char *name, const char *type,
195                            QObject *defval, const char *description)
196 {
197     GString *str = g_string_new(NULL);
198 
199     g_string_append_printf(str, "  %s=<%s>", name, type);
200     if (description || defval) {
201         if (str->len < 24) {
202             g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
203         }
204         g_string_append(str, " - ");
205     }
206     if (description) {
207         g_string_append(str, description);
208     }
209     if (defval) {
210         g_autofree char *def_json = qstring_free(qobject_to_json(defval), 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