qemu_qapi.c 8.1 KB
Newer Older
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
/*
 * qemu_qapi.c: helper functions for QEMU QAPI schema handling
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "qemu_qapi.h"

#include "viralloc.h"
#include "virstring.h"
#include "virerror.h"
#include "virlog.h"

28 29
#include "c-ctype.h"

30 31 32 33 34 35
#define VIR_FROM_THIS VIR_FROM_QEMU

VIR_LOG_INIT("qemu.qemu_qapi");


/**
36
 * virQEMUQAPISchemaObjectGet:
37 38 39
 * @field: name of the object containing the requested type
 * @name: name of the requested type
 * @namefield: name of the object property holding @name
40
 * @elem: QAPI schema entry JSON object
41 42
 *
 * Helper that selects the type of a QMP schema object member or it's variant
43
 * member. Returns the QMP entry on success or NULL on error.
44
 */
45 46 47 48 49
static virJSONValuePtr
virQEMUQAPISchemaObjectGet(const char *field,
                           const char *name,
                           const char *namefield,
                           virJSONValuePtr elem)
50 51 52 53 54 55 56 57 58 59 60
{
    virJSONValuePtr arr;
    virJSONValuePtr cur;
    const char *curname;
    size_t i;

    if (!(arr = virJSONValueObjectGetArray(elem, field)))
        return NULL;

    for (i = 0; i < virJSONValueArraySize(arr); i++) {
        if (!(cur = virJSONValueArrayGet(arr, i)) ||
61
            !(curname = virJSONValueObjectGetString(cur, namefield)))
62 63 64
            continue;

        if (STREQ(name, curname))
65
            return cur;
66 67 68 69 70 71
    }

    return NULL;
}


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
static const char *
virQEMUQAPISchemaTypeFromObject(virJSONValuePtr obj)
{
    if (!obj)
        return NULL;

    return virJSONValueObjectGetString(obj, "type");
}


/**
 * virQEMUQAPISchemaObjectGetType:
 * @field: name of the object containing the requested type
 * @name: name of the requested type
 * @namefield: name of the object property holding @name
 * @elem: QAPI schema entry JSON object
 *
 * Helper that selects the type of a QMP schema object member or it's variant
 * member. Returns the type string on success or NULL on error.
 */
static const char *
virQEMUQAPISchemaObjectGetType(const char *field,
                               const char *name,
                               const char *namefield,
                               virJSONValuePtr elem)
{
    virJSONValuePtr obj = virQEMUQAPISchemaObjectGet(field, name, namefield, elem);

    return virQEMUQAPISchemaTypeFromObject(obj);
}


104
static virJSONValuePtr
105 106 107
virQEMUQAPISchemaTraverse(const char *baseName,
                          char **query,
                          virHashTablePtr schema)
108 109 110
{
    virJSONValuePtr base;
    const char *metatype;
111 112
    const char *querystr;
    char modifier;
113

114
    while (1) {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        if (!(base = virHashLookup(schema, baseName)))
            return NULL;

        if (!*query)
            return base;

        if (!(metatype = virJSONValueObjectGetString(base, "meta-type")))
            return NULL;

        /* flatten arrays by default */
        if (STREQ(metatype, "array")) {
            if (!(baseName = virJSONValueObjectGetString(base, "element-type")))
                return NULL;

            continue;
        } else if (STREQ(metatype, "object")) {
131 132 133 134 135 136 137
            querystr = *query;
            modifier = **query;

            if (!c_isalpha(modifier))
                querystr++;

            if (modifier == '+')
138
                baseName = virQEMUQAPISchemaObjectGetType("variants",
139
                                                          querystr,
140
                                                          "case", base);
141
            else
142
                baseName = virQEMUQAPISchemaObjectGetType("members",
143
                                                          querystr,
144
                                                          "name", base);
145 146 147 148 149 150 151 152 153 154 155 156 157

            if (!baseName)
                return NULL;
        } else if (STREQ(metatype, "command") ||
                   STREQ(metatype, "event")) {
            if (!(baseName = virJSONValueObjectGetString(base, *query)))
                return NULL;
        } else {
            /* alternates, basic types and enums can't be entered */
            return NULL;
        }

        query++;
158
    }
159 160 161 162 163 164

    return base;
}


/**
165
 * virQEMUQAPISchemaPathGet:
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
 * @query: string specifying the required data type (see below)
 * @schema: hash table containing the schema data
 * @entry: filled with the located schema object requested by @query
 *
 * Retrieves the requested schema entry specified by @query to @entry. The
 * @query parameter has the following syntax which is very closely tied to the
 * qemu schema syntax entries separated by slashes with a few special characters:
 *
 * "command_or_event/attribute/subattribute/+variant_discriminator/subattribute"
 *
 * command_or_event: name of the event or attribute to introspect
 * attribute: selects whether arguments or return type should be introspected
 *            ("arg-type" or "ret-type" for commands, "arg-type" for events)
 * subattribute: specifies member name of object types
 * +variant_discriminator: In the case of unionized objects, select a
 *                         specific case to introspect.
 *
183 184 185
 * If the name of any (sub)attribute starts with non-alphabetical symbols it
 * needs to be prefixed by a single space.
 *
186 187 188 189 190 191 192 193 194 195 196
 * Array types are automatically flattened to the singular type. Alternate
 * types are currently not supported.
 *
 * The above types can be chained arbitrarily using slashes to construct any
 * path into the schema tree.
 *
 * Returns 0 on success (including if the requested schema was not found) and
 * fills @entry appropriately. On failure returns -1 and sets an appropriate
 * error message.
 */
int
197 198 199
virQEMUQAPISchemaPathGet(const char *query,
                         virHashTablePtr schema,
                         virJSONValuePtr *entry)
200 201 202 203 204 205 206 207 208 209 210 211 212 213
{
    char **elems = NULL;

    *entry = NULL;

    if (!(elems = virStringSplit(query, "/", 0)))
        return -1;

    if (!*elems) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed query string"));
        virStringListFree(elems);
        return -1;
    }

214
    *entry = virQEMUQAPISchemaTraverse(*elems, elems + 1, schema);
215 216 217 218 219 220 221

    virStringListFree(elems);
    return 0;
}


bool
222 223
virQEMUQAPISchemaPathExists(const char *query,
                            virHashTablePtr schema)
224 225 226
{
    virJSONValuePtr entry;

227
    if (virQEMUQAPISchemaPathGet(query, schema, &entry))
228 229 230 231
        return false;

    return !!entry;
}
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

static int
virQEMUQAPISchemaEntryProcess(size_t pos ATTRIBUTE_UNUSED,
                              virJSONValuePtr item,
                              void *opaque)
{
    const char *name;
    virHashTablePtr schema = opaque;

    if (!(name = virJSONValueObjectGetString(item, "name"))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("malformed QMP schema"));
        return -1;
    }

    if (virHashAddEntry(schema, name, item) < 0)
        return -1;

    return 0;
}


/**
 * virQEMUQAPISchemaConvert:
 * @schemareply: Schema data as returned by the qemu monitor
 *
 * Converts the schema into the hash-table used by the functions working with
 * the schema. @schemareply is consumed and freed.
 */
virHashTablePtr
virQEMUQAPISchemaConvert(virJSONValuePtr schemareply)
{
    virHashTablePtr schema;
    virHashTablePtr ret = NULL;

    if (!(schema = virHashCreate(512, virJSONValueHashFree)))
        goto cleanup;

    if (virJSONValueArrayForeachSteal(schemareply,
                                      virQEMUQAPISchemaEntryProcess,
                                      schema) < 0)
        goto cleanup;

    VIR_STEAL_PTR(ret, schema);

 cleanup:
    virJSONValueFree(schemareply);
    virHashFree(schema);
    return ret;
}