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