aboutsummaryrefslogtreecommitdiff
path: root/qom/object_interfaces.c
blob: b17aa57de1352ba8924a17f047bea8cf2a629710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "qemu/osdep.h"

#include "qemu/cutils.h"
#include "qapi/error.h"
#include "qapi/qapi-commands-qom.h"
#include "qapi/qapi-visit-qom.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qerror.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qobject-input-visitor.h"
#include "qapi/qobject-output-visitor.h"
#include "qom/object_interfaces.h"
#include "qemu/help_option.h"
#include "qemu/id.h"
#include "qemu/module.h"
#include "qemu/option.h"
#include "qemu/qemu-print.h"
#include "qapi/opts-visitor.h"
#include "qemu/config-file.h"

bool user_creatable_complete(UserCreatable *uc, Error **errp)
{
    UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
    Error *err = NULL;

    if (ucc->complete) {
        ucc->complete(uc, &err);
        error_propagate(errp, err);
    }
    return !err;
}

bool user_creatable_can_be_deleted(UserCreatable *uc)
{

    UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);

    if (ucc->can_be_deleted) {
        return ucc->can_be_deleted(uc);
    } else {
        return true;
    }
}

Object *user_creatable_add_type(const char *type, const char *id,
                                const QDict *qdict,
                                Visitor *v, Error **errp)
{
    ERRP_GUARD();
    Object *obj;
    ObjectClass *klass;
    const QDictEntry *e;
    Error *local_err = NULL;

    if (id != NULL && !id_wellformed(id)) {
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
        error_append_hint(errp, "Identifiers consist of letters, digits, "
                          "'-', '.', '_', starting with a letter.\n");
        return NULL;
    }

    klass = object_class_by_name(type);
    if (!klass) {
        error_setg(errp, "invalid object type: %s", type);
        return NULL;
    }

    if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
        error_setg(errp, "object type '%s' isn't supported by object-add",
                   type);
        return NULL;
    }

    if (object_class_is_abstract(klass)) {
        error_setg(errp, "object type '%s' is abstract", type);
        return NULL;
    }

    assert(qdict);
    obj = object_new(type);
    if (!visit_start_struct(v, NULL, NULL, 0, &local_err)) {
        goto out;
    }
    for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
        if (!object_property_set(obj, e->key, v, &local_err)) {
            break;
        }
    }
    if (!local_err) {
        visit_check_struct(v, &local_err);
    }
    visit_end_struct(v, NULL);
    if (local_err) {
        goto out;
    }

    if (id != NULL) {
        object_property_try_add_child(object_get_objects_root(),
                                      id, obj, &local_err);
        if (local_err) {
            goto out;
        }
    }

    if (!user_creatable_complete(USER_CREATABLE(obj), &local_err)) {
        if (id != NULL) {
            object_property_del(object_get_objects_root(), id);
        }
        goto out;
    }
out:
    if (local_err) {
        error_propagate(errp, local_err);
        object_unref(obj);
        return NULL;
    }
    return obj;
}

void user_creatable_add_qapi(ObjectOptions *options, Error **errp)
{
    Visitor *v;
    QObject *qobj;
    QDict *props;
    Object *obj;

    v = qobject_output_visitor_new(&qobj);
    visit_type_ObjectOptions(v, NULL, &options, &error_abort);
    visit_complete(v, &qobj);
    visit_free(v);

    props = qobject_to(QDict, qobj);
    qdict_del(props, "qom-type");
    qdict_del(props, "id");

    v = qobject_input_visitor_new(QOBJECT(props));
    obj = user_creatable_add_type(ObjectType_str(options->qom_type),
                                  options->id, props, v, errp);
    object_unref(obj);
    qobject_unref(qobj);
    visit_free(v);
}

char *object_property_help(const char *name, const char *type,
                           QObject *defval, const char *description)
{
    GString *str = g_string_new(NULL);

    g_string_append_printf(str, "  %s=<%s>", name, type);
    if (description || defval) {
        if (str->len < 24) {
            g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
        }
        g_string_append(str, " - ");
    }
    if (description) {
        g_string_append(str, description);
    }
    if (defval) {
        g_autofree char *def_json = g_string_free(qobject_to_json(defval),
                                                  true);
        g_string_append_printf(str, " (default: %s)", def_json);
    }

    return g_string_free(str, false);
}

static void user_creatable_print_types(void)
{
    GSList *l, *list;

    qemu_printf("List of user creatable objects:\n");
    list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
    for (l = list; l != NULL; l = l->next) {
        ObjectClass *oc = OBJECT_CLASS(l->data);
        qemu_printf("  %s\n", object_class_get_name(oc));
    }
    g_slist_free(list);
}

static bool user_creatable_print_type_properites(const char *type)
{
    ObjectClass *klass;
    ObjectPropertyIterator iter;
    ObjectProperty *prop;
    GPtrArray *array;
    int i;

    klass = object_class_by_name(type);
    if (!klass) {
        return false;
    }

    array = g_ptr_array_new();
    object_class_property_iter_init(&iter, klass);
    while ((prop = object_property_iter_next(&iter))) {
        if (!prop->set) {
            continue;
        }

        g_ptr_array_add(array,
                        object_property_help(prop->name, prop->type,
                                             prop->defval, prop->description));
    }
    g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
    if (array->len > 0) {
        qemu_printf("%s options:\n", type);
    } else {
        qemu_printf("There are no options for %s.\n", type);
    }
    for (i = 0; i < array->len; i++) {
        qemu_printf("%s\n", (char *)array->pdata[i]);
    }
    g_ptr_array_set_free_func(array, g_free);
    g_ptr_array_free(array, true);
    return true;
}

bool user_creatable_print_help(const char *type, QemuOpts *opts)
{
    if (is_help_option(type)) {
        user_creatable_print_types();
        return true;
    }

    if (qemu_opt_has_help_opt(opts)) {
        return user_creatable_print_type_properites(type);
    }

    return false;
}

static void user_creatable_print_help_from_qdict(QDict *args)
{
    const char *type = qdict_get_try_str(args, "qom-type");

    if (!type || !user_creatable_print_type_properites(type)) {
        user_creatable_print_types();
    }
}

ObjectOptions *user_creatable_parse_str(const char *optarg, Error **errp)
{
    ERRP_GUARD();
    QObject *obj;
    bool help;
    Visitor *v;
    ObjectOptions *options;

    if (optarg[0] == '{') {
        obj = qobject_from_json(optarg, errp);
        if (!obj) {
            return NULL;
        }
        v = qobject_input_visitor_new(obj);
    } else {
        QDict *args = keyval_parse(optarg, "qom-type", &help, errp);
        if (*errp) {
            return NULL;
        }
        if (help) {
            user_creatable_print_help_from_qdict(args);
            qobject_unref(args);
            return NULL;
        }

        obj = QOBJECT(args);
        v = qobject_input_visitor_new_keyval(obj);
    }

    visit_type_ObjectOptions(v, NULL, &options, errp);
    visit_free(v);
    qobject_unref(obj);

    return options;
}

bool user_creatable_add_from_str(const char *optarg, Error **errp)
{
    ERRP_GUARD();
    ObjectOptions *options;

    options = user_creatable_parse_str(optarg, errp);
    if (!options) {
        return false;
    }

    user_creatable_add_qapi(options, errp);
    qapi_free_ObjectOptions(options);
    return !*errp;
}

void user_creatable_process_cmdline(const char *optarg)
{
    if (!user_creatable_add_from_str(optarg, &error_fatal)) {
        /* Help was printed */
        exit(EXIT_SUCCESS);
    }
}

bool user_creatable_del(const char *id, Error **errp)
{
    QemuOptsList *opts_list;
    Object *container;
    Object *obj;

    container = object_get_objects_root();
    obj = object_resolve_path_component(container, id);
    if (!obj) {
        error_setg(errp, "object '%s' not found", id);
        return false;
    }

    if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
        error_setg(errp, "object '%s' is in use, can not be deleted", id);
        return false;
    }

    /*
     * if object was defined on the command-line, remove its corresponding
     * option group entry
     */
    opts_list = qemu_find_opts_err("object", NULL);
    if (opts_list) {
        qemu_opts_del(qemu_opts_find(opts_list, id));
    }

    object_unparent(obj);
    return true;
}

void user_creatable_cleanup(void)
{
    object_unparent(object_get_objects_root());
}

static void register_types(void)
{
    static const TypeInfo uc_interface_info = {
        .name          = TYPE_USER_CREATABLE,
        .parent        = TYPE_INTERFACE,
        .class_size = sizeof(UserCreatableClass),
    };

    type_register_static(&uc_interface_info);
}

type_init(register_types)