xref: /openbmc/qemu/qom/object_interfaces.c (revision 5e437d3c)
1 #include "qemu/osdep.h"
2 
3 #include "qemu/cutils.h"
4 #include "qapi/error.h"
5 #include "qapi/qapi-commands-qom.h"
6 #include "qapi/qapi-visit-qom.h"
7 #include "qapi/qmp/qdict.h"
8 #include "qapi/qmp/qerror.h"
9 #include "qapi/qmp/qjson.h"
10 #include "qapi/qobject-input-visitor.h"
11 #include "qapi/qobject-output-visitor.h"
12 #include "qom/object_interfaces.h"
13 #include "qemu/help_option.h"
14 #include "qemu/id.h"
15 #include "qemu/module.h"
16 #include "qemu/option.h"
17 #include "qemu/qemu-print.h"
18 #include "qapi/opts-visitor.h"
19 #include "qemu/config-file.h"
20 #include "qemu/qemu-print.h"
21 
22 bool user_creatable_complete(UserCreatable *uc, Error **errp)
23 {
24     UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
25     Error *err = NULL;
26 
27     if (ucc->complete) {
28         ucc->complete(uc, &err);
29         error_propagate(errp, err);
30     }
31     return !err;
32 }
33 
34 bool user_creatable_can_be_deleted(UserCreatable *uc)
35 {
36 
37     UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
38 
39     if (ucc->can_be_deleted) {
40         return ucc->can_be_deleted(uc);
41     } else {
42         return true;
43     }
44 }
45 
46 Object *user_creatable_add_type(const char *type, const char *id,
47                                 const QDict *qdict,
48                                 Visitor *v, Error **errp)
49 {
50     ERRP_GUARD();
51     Object *obj;
52     ObjectClass *klass;
53     const QDictEntry *e;
54     Error *local_err = NULL;
55 
56     if (id != NULL && !id_wellformed(id)) {
57         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
58         error_append_hint(errp, "Identifiers consist of letters, digits, "
59                           "'-', '.', '_', starting with a letter.\n");
60         return NULL;
61     }
62 
63     klass = object_class_by_name(type);
64     if (!klass) {
65         error_setg(errp, "invalid object type: %s", type);
66         return NULL;
67     }
68 
69     if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
70         error_setg(errp, "object type '%s' isn't supported by object-add",
71                    type);
72         return NULL;
73     }
74 
75     if (object_class_is_abstract(klass)) {
76         error_setg(errp, "object type '%s' is abstract", type);
77         return NULL;
78     }
79 
80     assert(qdict);
81     obj = object_new(type);
82     if (!visit_start_struct(v, NULL, NULL, 0, &local_err)) {
83         goto out;
84     }
85     for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
86         if (!object_property_set(obj, e->key, v, &local_err)) {
87             break;
88         }
89     }
90     if (!local_err) {
91         visit_check_struct(v, &local_err);
92     }
93     visit_end_struct(v, NULL);
94     if (local_err) {
95         goto out;
96     }
97 
98     if (id != NULL) {
99         object_property_try_add_child(object_get_objects_root(),
100                                       id, obj, &local_err);
101         if (local_err) {
102             goto out;
103         }
104     }
105 
106     if (!user_creatable_complete(USER_CREATABLE(obj), &local_err)) {
107         if (id != NULL) {
108             object_property_del(object_get_objects_root(), id);
109         }
110         goto out;
111     }
112 out:
113     if (local_err) {
114         error_propagate(errp, local_err);
115         object_unref(obj);
116         return NULL;
117     }
118     return obj;
119 }
120 
121 void user_creatable_add_qapi(ObjectOptions *options, Error **errp)
122 {
123     Visitor *v;
124     QObject *qobj;
125     QDict *props;
126     Object *obj;
127 
128     v = qobject_output_visitor_new(&qobj);
129     visit_type_ObjectOptions(v, NULL, &options, &error_abort);
130     visit_complete(v, &qobj);
131     visit_free(v);
132 
133     props = qobject_to(QDict, qobj);
134     qdict_del(props, "qom-type");
135     qdict_del(props, "id");
136 
137     v = qobject_input_visitor_new(QOBJECT(props));
138     obj = user_creatable_add_type(ObjectType_str(options->qom_type),
139                                   options->id, props, v, errp);
140     object_unref(obj);
141     qobject_unref(qobj);
142     visit_free(v);
143 }
144 
145 char *object_property_help(const char *name, const char *type,
146                            QObject *defval, const char *description)
147 {
148     GString *str = g_string_new(NULL);
149 
150     g_string_append_printf(str, "  %s=<%s>", name, type);
151     if (description || defval) {
152         if (str->len < 24) {
153             g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
154         }
155         g_string_append(str, " - ");
156     }
157     if (description) {
158         g_string_append(str, description);
159     }
160     if (defval) {
161         g_autofree char *def_json = g_string_free(qobject_to_json(defval),
162                                                   true);
163         g_string_append_printf(str, " (default: %s)", def_json);
164     }
165 
166     return g_string_free(str, false);
167 }
168 
169 static void user_creatable_print_types(void)
170 {
171     GSList *l, *list;
172 
173     qemu_printf("List of user creatable objects:\n");
174     list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
175     for (l = list; l != NULL; l = l->next) {
176         ObjectClass *oc = OBJECT_CLASS(l->data);
177         qemu_printf("  %s\n", object_class_get_name(oc));
178     }
179     g_slist_free(list);
180 }
181 
182 static bool user_creatable_print_type_properites(const char *type)
183 {
184     ObjectClass *klass;
185     ObjectPropertyIterator iter;
186     ObjectProperty *prop;
187     GPtrArray *array;
188     int i;
189 
190     klass = object_class_by_name(type);
191     if (!klass) {
192         return false;
193     }
194 
195     array = g_ptr_array_new();
196     object_class_property_iter_init(&iter, klass);
197     while ((prop = object_property_iter_next(&iter))) {
198         if (!prop->set) {
199             continue;
200         }
201 
202         g_ptr_array_add(array,
203                         object_property_help(prop->name, prop->type,
204                                              prop->defval, prop->description));
205     }
206     g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
207     if (array->len > 0) {
208         qemu_printf("%s options:\n", type);
209     } else {
210         qemu_printf("There are no options for %s.\n", type);
211     }
212     for (i = 0; i < array->len; i++) {
213         qemu_printf("%s\n", (char *)array->pdata[i]);
214     }
215     g_ptr_array_set_free_func(array, g_free);
216     g_ptr_array_free(array, true);
217     return true;
218 }
219 
220 bool user_creatable_print_help(const char *type, QemuOpts *opts)
221 {
222     if (is_help_option(type)) {
223         user_creatable_print_types();
224         return true;
225     }
226 
227     if (qemu_opt_has_help_opt(opts)) {
228         return user_creatable_print_type_properites(type);
229     }
230 
231     return false;
232 }
233 
234 static void user_creatable_print_help_from_qdict(QDict *args)
235 {
236     const char *type = qdict_get_try_str(args, "qom-type");
237 
238     if (!type || !user_creatable_print_type_properites(type)) {
239         user_creatable_print_types();
240     }
241 }
242 
243 ObjectOptions *user_creatable_parse_str(const char *optarg, Error **errp)
244 {
245     ERRP_GUARD();
246     QObject *obj;
247     bool help;
248     Visitor *v;
249     ObjectOptions *options;
250 
251     if (optarg[0] == '{') {
252         obj = qobject_from_json(optarg, errp);
253         if (!obj) {
254             return NULL;
255         }
256         v = qobject_input_visitor_new(obj);
257     } else {
258         QDict *args = keyval_parse(optarg, "qom-type", &help, errp);
259         if (*errp) {
260             return NULL;
261         }
262         if (help) {
263             user_creatable_print_help_from_qdict(args);
264             qobject_unref(args);
265             return NULL;
266         }
267 
268         obj = QOBJECT(args);
269         v = qobject_input_visitor_new_keyval(obj);
270     }
271 
272     visit_type_ObjectOptions(v, NULL, &options, errp);
273     visit_free(v);
274     qobject_unref(obj);
275 
276     return options;
277 }
278 
279 bool user_creatable_add_from_str(const char *optarg, Error **errp)
280 {
281     ERRP_GUARD();
282     ObjectOptions *options;
283 
284     options = user_creatable_parse_str(optarg, errp);
285     if (!options) {
286         return false;
287     }
288 
289     user_creatable_add_qapi(options, errp);
290     qapi_free_ObjectOptions(options);
291     return !*errp;
292 }
293 
294 void user_creatable_process_cmdline(const char *optarg)
295 {
296     if (!user_creatable_add_from_str(optarg, &error_fatal)) {
297         /* Help was printed */
298         exit(EXIT_SUCCESS);
299     }
300 }
301 
302 bool user_creatable_del(const char *id, Error **errp)
303 {
304     QemuOptsList *opts_list;
305     Object *container;
306     Object *obj;
307 
308     container = object_get_objects_root();
309     obj = object_resolve_path_component(container, id);
310     if (!obj) {
311         error_setg(errp, "object '%s' not found", id);
312         return false;
313     }
314 
315     if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
316         error_setg(errp, "object '%s' is in use, can not be deleted", id);
317         return false;
318     }
319 
320     /*
321      * if object was defined on the command-line, remove its corresponding
322      * option group entry
323      */
324     opts_list = qemu_find_opts_err("object", NULL);
325     if (opts_list) {
326         qemu_opts_del(qemu_opts_find(opts_list, id));
327     }
328 
329     object_unparent(obj);
330     return true;
331 }
332 
333 void user_creatable_cleanup(void)
334 {
335     object_unparent(object_get_objects_root());
336 }
337 
338 static void register_types(void)
339 {
340     static const TypeInfo uc_interface_info = {
341         .name          = TYPE_USER_CREATABLE,
342         .parent        = TYPE_INTERFACE,
343         .class_size = sizeof(UserCreatableClass),
344     };
345 
346     type_register_static(&uc_interface_info);
347 }
348 
349 type_init(register_types)
350