object_interfaces.c 7.1 KB
Newer Older
P
Peter Maydell 已提交
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 8
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qstring.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
void user_creatable_complete(UserCreatable *uc, Error **errp)
17
{
18
    UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
19 20 21 22 23 24

    if (ucc->complete) {
        ucc->complete(uc, errp);
    }
}

25
bool user_creatable_can_be_deleted(UserCreatable *uc)
26 27 28 29 30
{

    UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);

    if (ucc->can_be_deleted) {
31
        return ucc->can_be_deleted(uc);
32 33 34 35 36
    } else {
        return true;
    }
}

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
Object *user_creatable_add_type(const char *type, const char *id,
                                const QDict *qdict,
                                Visitor *v, Error **errp)
{
    Object *obj;
    ObjectClass *klass;
    const QDictEntry *e;
    Error *local_err = 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;
    }

63
    assert(qdict);
64
    obj = object_new(type);
65 66 67 68 69 70 71 72
    visit_start_struct(v, NULL, NULL, 0, &local_err);
    if (local_err) {
        goto out;
    }
    for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
        object_property_set(obj, v, e->key, &local_err);
        if (local_err) {
            break;
73 74
        }
    }
75 76 77
    if (!local_err) {
        visit_check_struct(v, &local_err);
    }
E
Eric Blake 已提交
78
    visit_end_struct(v, NULL);
79 80 81
    if (local_err) {
        goto out;
    }
82

83 84 85 86 87 88
    if (id != NULL) {
        object_property_add_child(object_get_objects_root(),
                                  id, obj, &local_err);
        if (local_err) {
            goto out;
        }
89 90
    }

91
    user_creatable_complete(USER_CREATABLE(obj), &local_err);
92
    if (local_err) {
93 94 95 96
        if (id != NULL) {
            object_property_del(object_get_objects_root(),
                                id, &error_abort);
        }
97 98 99 100 101 102 103 104 105 106 107 108 109 110
        goto out;
    }
out:
    if (local_err) {
        error_propagate(errp, local_err);
        object_unref(obj);
        return NULL;
    }
    return obj;
}


Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
{
111
    Visitor *v;
112
    QDict *pdict;
113 114
    Object *obj;
    const char *id = qemu_opts_id(opts);
115
    char *type = qemu_opt_get_del(opts, "qom-type");
116 117 118 119 120 121 122

    if (!type) {
        error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
        return NULL;
    }
    if (!id) {
        error_setg(errp, QERR_MISSING_PARAMETER, "id");
E
Eric Blake 已提交
123
        qemu_opt_set(opts, "qom-type", type, &error_abort);
124
        g_free(type);
125 126
        return NULL;
    }
127

128
    qemu_opts_set_id(opts, NULL);
129 130
    pdict = qemu_opts_to_qdict(opts, NULL);

131 132
    v = opts_visitor_new(opts);
    obj = user_creatable_add_type(type, id, pdict, v, errp);
133
    visit_free(v);
134

135
    qemu_opts_set_id(opts, (char *) id);
E
Eric Blake 已提交
136
    qemu_opt_set(opts, "qom-type", type, &error_abort);
137
    g_free(type);
138
    qobject_unref(pdict);
139 140 141 142 143 144
    return obj;
}


int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
{
145
    bool (*type_opt_predicate)(const char *, QemuOpts *) = opaque;
146 147 148 149
    Object *obj = NULL;
    const char *type;

    type = qemu_opt_get(opts, "qom-type");
150 151
    if (type && type_opt_predicate &&
        !type_opt_predicate(type, opts)) {
152 153 154
        return 0;
    }

155
    obj = user_creatable_add_opts(opts, errp);
156 157 158 159 160 161 162
    if (!obj) {
        return -1;
    }
    object_unref(obj);
    return 0;
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
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 = qstring_free(qobject_to_json(defval), TRUE);
        g_string_append_printf(str, " (default: %s)", def_json);
    }

    return g_string_free(str, false);
}

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
bool user_creatable_print_help(const char *type, QemuOpts *opts)
{
    ObjectClass *klass;

    if (is_help_option(type)) {
        GSList *l, *list;

        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);
            printf("  %s\n", object_class_get_name(oc));
        }
        g_slist_free(list);
        return true;
    }

    klass = object_class_by_name(type);
    if (klass && qemu_opt_has_help_opt(opts)) {
        ObjectPropertyIterator iter;
        ObjectProperty *prop;
        GPtrArray *array = g_ptr_array_new();
        int i;

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

216 217 218
            g_ptr_array_add(array,
                            object_property_help(prop->name, prop->type,
                                                 prop->defval, prop->description));
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        }
        g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
        if (array->len > 0) {
            printf("%s options:\n", type);
        } else {
            printf("There are no options for %s.\n", type);
        }
        for (i = 0; i < array->len; i++) {
            printf("%s\n", (char *)array->pdata[i]);
        }
        g_ptr_array_set_free_func(array, g_free);
        g_ptr_array_free(array, true);
        return true;
    }

    return false;
}
236 237 238 239 240 241 242 243 244 245 246 247 248

void user_creatable_del(const char *id, Error **errp)
{
    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;
    }

249
    if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
250 251 252
        error_setg(errp, "object '%s' is in use, can not be deleted", id);
        return;
    }
253 254 255 256 257 258 259 260

    /*
     * if object was defined on the command-line, remove its corresponding
     * option group entry
     */
    qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort),
                                 id));

261 262 263
    object_unparent(obj);
}

264 265 266 267 268
void user_creatable_cleanup(void)
{
    object_unparent(object_get_objects_root());
}

269 270 271 272 273 274 275 276 277 278 279 280
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)