2018-03-20 07:44:11 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_QEMU
|
|
|
|
|
|
|
|
VIR_LOG_INIT("qemu.qemu_qapi");
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-08-15 06:39:19 +00:00
|
|
|
* virQEMUQAPISchemaObjectGet:
|
2018-03-20 07:44:11 +00:00
|
|
|
* @field: name of the object containing the requested type
|
|
|
|
* @name: name of the requested type
|
|
|
|
* @namefield: name of the object property holding @name
|
2018-08-15 06:39:19 +00:00
|
|
|
* @elem: QAPI schema entry JSON object
|
2018-03-20 07:44:11 +00:00
|
|
|
*
|
|
|
|
* Helper that selects the type of a QMP schema object member or it's variant
|
2018-08-15 06:39:19 +00:00
|
|
|
* member. Returns the QMP entry on success or NULL on error.
|
2018-03-20 07:44:11 +00:00
|
|
|
*/
|
2018-08-15 06:39:19 +00:00
|
|
|
static virJSONValuePtr
|
|
|
|
virQEMUQAPISchemaObjectGet(const char *field,
|
|
|
|
const char *name,
|
|
|
|
const char *namefield,
|
|
|
|
virJSONValuePtr elem)
|
2018-03-20 07:44:11 +00:00
|
|
|
{
|
|
|
|
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)) ||
|
2018-08-15 06:39:19 +00:00
|
|
|
!(curname = virJSONValueObjectGetString(cur, namefield)))
|
2018-03-20 07:44:11 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (STREQ(name, curname))
|
2018-08-15 06:39:19 +00:00
|
|
|
return cur;
|
2018-03-20 07:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-12 10:54:11 +00:00
|
|
|
struct virQEMUQAPISchemaTraverseContext {
|
2019-04-15 13:33:58 +00:00
|
|
|
const char *prevquery;
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *schema;
|
2019-04-12 13:59:10 +00:00
|
|
|
char **queries;
|
2019-04-12 10:54:11 +00:00
|
|
|
virJSONValuePtr returnType;
|
2019-08-12 16:31:44 +00:00
|
|
|
size_t depth;
|
2019-04-12 10:54:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-12 16:31:44 +00:00
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverseContextValidateDepth(struct virQEMUQAPISchemaTraverseContext *ctxt)
|
|
|
|
{
|
|
|
|
if (ctxt->depth++ > 1000) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("possible loop in QMP schema"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-12 13:59:10 +00:00
|
|
|
static void
|
|
|
|
virQEMUQAPISchemaTraverseContextInit(struct virQEMUQAPISchemaTraverseContext *ctxt,
|
|
|
|
char **queries,
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *schema)
|
2019-04-12 13:59:10 +00:00
|
|
|
{
|
|
|
|
memset(ctxt, 0, sizeof(*ctxt));
|
|
|
|
ctxt->schema = schema;
|
|
|
|
ctxt->queries = queries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
virQEMUQAPISchemaTraverseContextNextQuery(struct virQEMUQAPISchemaTraverseContext *ctxt)
|
|
|
|
{
|
2019-04-15 13:33:58 +00:00
|
|
|
ctxt->prevquery = ctxt->queries[0];
|
2019-04-12 13:59:10 +00:00
|
|
|
ctxt->queries++;
|
2019-04-15 13:33:58 +00:00
|
|
|
return ctxt->prevquery;
|
2019-04-12 13:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
virQEMUQAPISchemaTraverseContextHasNextQuery(struct virQEMUQAPISchemaTraverseContext *ctxt)
|
|
|
|
{
|
|
|
|
return !!ctxt->queries[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-09 11:25:49 +00:00
|
|
|
static int
|
2018-03-20 07:57:44 +00:00
|
|
|
virQEMUQAPISchemaTraverse(const char *baseName,
|
2019-04-12 10:54:11 +00:00
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt);
|
2019-04-10 12:14:10 +00:00
|
|
|
|
|
|
|
|
2019-04-10 12:52:48 +00:00
|
|
|
/**
|
|
|
|
* @featurename: name of 'feature' field to select
|
|
|
|
* @elem: QAPI JSON entry for a type
|
|
|
|
*
|
|
|
|
* Looks for @featurename in the array of 'features' for given type passed in
|
|
|
|
* via @elem. Returns 1 if @featurename is present, 0 if it's not present
|
|
|
|
* (or @elem has no 'features') or -2 if the schema is malformed.
|
|
|
|
* (see virQEMUQAPISchemaTraverseFunc)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverseHasObjectFeature(const char *featurename,
|
|
|
|
virJSONValuePtr elem)
|
|
|
|
{
|
|
|
|
virJSONValuePtr featuresarray;
|
|
|
|
virJSONValuePtr cur;
|
|
|
|
const char *curstr;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!(featuresarray = virJSONValueObjectGetArray(elem, "features")))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < virJSONValueArraySize(featuresarray); i++) {
|
|
|
|
if (!(cur = virJSONValueArrayGet(featuresarray, i)) ||
|
|
|
|
!(curstr = virJSONValueGetString(cur)))
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (STREQ(featurename, curstr))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-10 12:14:10 +00:00
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverseObject(virJSONValuePtr cur,
|
2019-04-12 10:54:11 +00:00
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt)
|
2018-03-20 07:44:11 +00:00
|
|
|
{
|
2018-08-15 06:51:01 +00:00
|
|
|
virJSONValuePtr obj;
|
2019-04-12 13:59:10 +00:00
|
|
|
const char *query = virQEMUQAPISchemaTraverseContextNextQuery(ctxt);
|
2019-04-12 13:55:54 +00:00
|
|
|
char modifier = *query;
|
2019-04-10 12:14:10 +00:00
|
|
|
|
2019-11-18 14:11:46 +00:00
|
|
|
if (!g_ascii_isalpha(modifier))
|
2019-04-12 13:55:54 +00:00
|
|
|
query++;
|
2019-04-10 12:14:10 +00:00
|
|
|
|
2020-10-02 14:07:27 +00:00
|
|
|
/* exit on modifiers for other types */
|
2019-04-11 12:54:41 +00:00
|
|
|
if (modifier == '^' || modifier == '!')
|
2019-04-11 12:54:41 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-04-10 12:52:48 +00:00
|
|
|
if (modifier == '$') {
|
|
|
|
if (virQEMUQAPISchemaTraverseContextHasNextQuery(ctxt))
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
return virQEMUQAPISchemaTraverseHasObjectFeature(query, cur);
|
|
|
|
}
|
|
|
|
|
2019-04-10 12:14:10 +00:00
|
|
|
if (modifier == '+') {
|
2019-04-12 13:55:54 +00:00
|
|
|
obj = virQEMUQAPISchemaObjectGet("variants", query, "case", cur);
|
2019-04-10 12:14:10 +00:00
|
|
|
} else {
|
2019-04-12 13:55:54 +00:00
|
|
|
obj = virQEMUQAPISchemaObjectGet("members", query, "name", cur);
|
2019-04-10 12:14:10 +00:00
|
|
|
|
|
|
|
if (modifier == '*' &&
|
|
|
|
!virJSONValueObjectHasKey(obj, "default"))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-12 07:19:39 +00:00
|
|
|
if (!obj)
|
|
|
|
return 0;
|
|
|
|
|
2019-04-12 10:54:11 +00:00
|
|
|
return virQEMUQAPISchemaTraverse(virJSONValueObjectGetString(obj, "type"), ctxt);
|
2019-04-10 12:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverseArray(virJSONValuePtr cur,
|
2019-04-12 10:54:11 +00:00
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt)
|
2019-04-10 12:14:10 +00:00
|
|
|
{
|
|
|
|
const char *querytype;
|
|
|
|
|
|
|
|
/* arrays are just flattened by default */
|
|
|
|
if (!(querytype = virJSONValueObjectGetString(cur, "element-type")))
|
2019-04-15 13:33:58 +00:00
|
|
|
return -2;
|
2019-04-10 12:14:10 +00:00
|
|
|
|
2019-04-12 10:54:11 +00:00
|
|
|
return virQEMUQAPISchemaTraverse(querytype, ctxt);
|
2019-04-10 12:14:10 +00:00
|
|
|
}
|
2018-03-20 07:44:11 +00:00
|
|
|
|
2019-04-10 12:14:10 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverseCommand(virJSONValuePtr cur,
|
2019-04-12 10:54:11 +00:00
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt)
|
2019-04-10 12:14:10 +00:00
|
|
|
{
|
2019-04-12 13:59:10 +00:00
|
|
|
const char *query = virQEMUQAPISchemaTraverseContextNextQuery(ctxt);
|
2019-04-10 12:14:10 +00:00
|
|
|
const char *querytype;
|
2019-09-17 15:08:56 +00:00
|
|
|
char modifier = *query;
|
|
|
|
|
2019-11-18 14:11:46 +00:00
|
|
|
if (!g_ascii_isalpha(modifier))
|
2019-09-17 15:08:56 +00:00
|
|
|
query++;
|
|
|
|
|
2020-10-02 14:07:27 +00:00
|
|
|
/* exit on modifiers for other types */
|
2019-09-17 15:08:56 +00:00
|
|
|
if (modifier == '^' || modifier == '!' || modifier == '+' || modifier == '*')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (modifier == '$') {
|
|
|
|
if (virQEMUQAPISchemaTraverseContextHasNextQuery(ctxt))
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
return virQEMUQAPISchemaTraverseHasObjectFeature(query, cur);
|
|
|
|
}
|
2019-04-10 12:14:10 +00:00
|
|
|
|
2019-04-12 13:59:10 +00:00
|
|
|
if (!(querytype = virJSONValueObjectGetString(cur, query)))
|
2019-04-10 12:14:10 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-04-12 10:54:11 +00:00
|
|
|
return virQEMUQAPISchemaTraverse(querytype, ctxt);
|
2019-04-10 12:14:10 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 12:54:41 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverseEnum(virJSONValuePtr cur,
|
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt)
|
|
|
|
{
|
|
|
|
const char *query = virQEMUQAPISchemaTraverseContextNextQuery(ctxt);
|
|
|
|
virJSONValuePtr values;
|
|
|
|
virJSONValuePtr enumval;
|
|
|
|
const char *value;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (query[0] != '^')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (virQEMUQAPISchemaTraverseContextHasNextQuery(ctxt))
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
query++;
|
|
|
|
|
|
|
|
if (!(values = virJSONValueObjectGetArray(cur, "values")))
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
for (i = 0; i < virJSONValueArraySize(values); i++) {
|
|
|
|
if (!(enumval = virJSONValueArrayGet(values, i)) ||
|
|
|
|
!(value = virJSONValueGetString(enumval)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (STREQ(value, query))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-11 12:54:41 +00:00
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverseBuiltin(virJSONValuePtr cur,
|
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt)
|
|
|
|
{
|
|
|
|
const char *query = virQEMUQAPISchemaTraverseContextNextQuery(ctxt);
|
|
|
|
const char *jsontype;
|
|
|
|
|
|
|
|
if (query[0] != '!')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (virQEMUQAPISchemaTraverseContextHasNextQuery(ctxt))
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
query++;
|
|
|
|
|
|
|
|
if (!(jsontype = virJSONValueObjectGetString(cur, "json-type")))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (STREQ(jsontype, query))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-11 12:54:41 +00:00
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverseAlternate(virJSONValuePtr cur,
|
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt)
|
|
|
|
{
|
|
|
|
struct virQEMUQAPISchemaTraverseContext savectxt = *ctxt;
|
|
|
|
virJSONValuePtr members;
|
|
|
|
virJSONValuePtr member;
|
|
|
|
const char *membertype;
|
|
|
|
int rc;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!(members = virJSONValueObjectGetArray(cur, "members")))
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
for (i = 0; i < virJSONValueArraySize(members); i++) {
|
|
|
|
if (!(member = virJSONValueArrayGet(members, i)) ||
|
|
|
|
!(membertype = virJSONValueObjectGetString(member, "type")))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*ctxt = savectxt;
|
|
|
|
|
|
|
|
if ((rc = virQEMUQAPISchemaTraverse(membertype, ctxt)) != 0)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-15 13:33:58 +00:00
|
|
|
/* The function must return 1 on successful query, 0 if the query was not found
|
|
|
|
* -1 when a libvirt error is reported, -2 if the schema is invalid and -3 if
|
|
|
|
* the query component is malformed. */
|
2019-04-10 12:22:14 +00:00
|
|
|
typedef int (*virQEMUQAPISchemaTraverseFunc)(virJSONValuePtr cur,
|
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt);
|
|
|
|
|
|
|
|
struct virQEMUQAPISchemaTraverseMetaType {
|
|
|
|
const char *metatype;
|
|
|
|
virQEMUQAPISchemaTraverseFunc func;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static const struct virQEMUQAPISchemaTraverseMetaType traverseMetaType[] = {
|
|
|
|
{ "object", virQEMUQAPISchemaTraverseObject },
|
|
|
|
{ "array", virQEMUQAPISchemaTraverseArray },
|
|
|
|
{ "command", virQEMUQAPISchemaTraverseCommand },
|
|
|
|
{ "event", virQEMUQAPISchemaTraverseCommand },
|
2019-04-11 12:54:41 +00:00
|
|
|
{ "enum", virQEMUQAPISchemaTraverseEnum },
|
2019-04-11 12:54:41 +00:00
|
|
|
{ "builtin", virQEMUQAPISchemaTraverseBuiltin },
|
2019-04-11 12:54:41 +00:00
|
|
|
{ "alternate", virQEMUQAPISchemaTraverseAlternate },
|
2019-04-10 12:22:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-04-10 12:14:10 +00:00
|
|
|
static int
|
|
|
|
virQEMUQAPISchemaTraverse(const char *baseName,
|
2019-04-12 10:54:11 +00:00
|
|
|
struct virQEMUQAPISchemaTraverseContext *ctxt)
|
2019-04-10 12:14:10 +00:00
|
|
|
{
|
|
|
|
virJSONValuePtr cur;
|
|
|
|
const char *metatype;
|
2019-04-10 12:22:14 +00:00
|
|
|
size_t i;
|
2019-04-10 12:14:10 +00:00
|
|
|
|
2019-08-12 16:31:44 +00:00
|
|
|
if (virQEMUQAPISchemaTraverseContextValidateDepth(ctxt) < 0)
|
|
|
|
return -2;
|
|
|
|
|
2019-04-12 10:54:11 +00:00
|
|
|
if (!(cur = virHashLookup(ctxt->schema, baseName)))
|
2019-04-15 13:33:58 +00:00
|
|
|
return -2;
|
2018-03-20 07:44:11 +00:00
|
|
|
|
2019-04-12 13:59:10 +00:00
|
|
|
if (!virQEMUQAPISchemaTraverseContextHasNextQuery(ctxt)) {
|
2019-04-12 10:54:11 +00:00
|
|
|
ctxt->returnType = cur;
|
2019-04-09 13:11:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-08-15 06:32:04 +00:00
|
|
|
|
2019-04-10 12:14:10 +00:00
|
|
|
if (!(metatype = virJSONValueObjectGetString(cur, "meta-type")))
|
2019-04-15 13:33:58 +00:00
|
|
|
return -2;
|
2018-08-15 06:32:04 +00:00
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(traverseMetaType); i++) {
|
2019-04-10 12:22:14 +00:00
|
|
|
if (STREQ(metatype, traverseMetaType[i].metatype))
|
|
|
|
return traverseMetaType[i].func(cur, ctxt);
|
2018-03-21 15:51:29 +00:00
|
|
|
}
|
2018-03-20 07:44:11 +00:00
|
|
|
|
2019-04-10 12:14:10 +00:00
|
|
|
return 0;
|
2018-03-20 07:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-20 07:57:44 +00:00
|
|
|
* virQEMUQAPISchemaPathGet:
|
2018-03-20 07:44:11 +00:00
|
|
|
* @query: string specifying the required data type (see below)
|
|
|
|
* @schema: hash table containing the schema data
|
2019-04-09 12:09:33 +00:00
|
|
|
* @entry: filled with the located schema object requested by @query (optional)
|
2018-03-20 07:44:11 +00:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
2019-04-15 15:26:47 +00:00
|
|
|
* "command_or_event/attribute/subattribute/subattribute/..."
|
2018-03-20 07:44:11 +00:00
|
|
|
*
|
|
|
|
* 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)
|
2019-04-15 15:26:47 +00:00
|
|
|
*
|
|
|
|
* 'subattribute' may be one or more of the following depending on the first
|
|
|
|
* character.
|
|
|
|
*
|
|
|
|
* - Type queries - @entry is filled on success with the corresponding schema entry:
|
|
|
|
* 'subattribute': selects a plain object member named 'subattribute'
|
|
|
|
* '*subattribute': same as above but the selected member must be optional
|
|
|
|
* (has a property named 'default' in the schema)
|
|
|
|
* '+variant": In the case of unionized objects, select a specific variant of
|
2020-07-09 04:42:21 +00:00
|
|
|
* the previously selected member
|
2019-04-15 15:26:47 +00:00
|
|
|
*
|
|
|
|
* - Boolean queries - @entry remains NULL, return value indicates success:
|
2019-04-11 12:54:41 +00:00
|
|
|
* '^enumval': returns true if the previously selected enum contains 'enumval'
|
2019-04-11 12:54:41 +00:00
|
|
|
* '!basictype': returns true if previously selected type is of 'basictype'
|
2020-07-09 04:42:21 +00:00
|
|
|
* JSON type. Supported are 'null', 'string', 'number', 'value',
|
2019-04-11 12:54:41 +00:00
|
|
|
* 'int' and 'boolean.
|
2019-04-10 12:52:48 +00:00
|
|
|
* '$feature': returns true if the previously selected type supports 'feature'
|
|
|
|
* ('feature' is in the 'features' array of given type)
|
2018-03-20 07:44:11 +00:00
|
|
|
*
|
2018-08-15 06:32:04 +00:00
|
|
|
* If the name of any (sub)attribute starts with non-alphabetical symbols it
|
|
|
|
* needs to be prefixed by a single space.
|
|
|
|
*
|
2019-04-11 12:54:41 +00:00
|
|
|
* Array types are automatically flattened to the singular type. Alternates are
|
|
|
|
* iterated until first success.
|
2018-03-20 07:44:11 +00:00
|
|
|
*
|
|
|
|
* The above types can be chained arbitrarily using slashes to construct any
|
2019-04-15 15:26:47 +00:00
|
|
|
* path into the schema tree, booleans must be always the last component as they
|
|
|
|
* don't refer to a type.
|
2018-03-20 07:44:11 +00:00
|
|
|
*
|
2019-04-09 12:09:33 +00:00
|
|
|
* Returns 1 if @query was found in @schema filling @entry if non-NULL, 0 if
|
|
|
|
* @query was not found in @schema and -1 on other errors along with an appropriate
|
2018-03-20 07:44:11 +00:00
|
|
|
* error message.
|
|
|
|
*/
|
|
|
|
int
|
2018-03-20 07:57:44 +00:00
|
|
|
virQEMUQAPISchemaPathGet(const char *query,
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *schema,
|
2018-03-20 07:57:44 +00:00
|
|
|
virJSONValuePtr *entry)
|
2018-03-20 07:44:11 +00:00
|
|
|
{
|
2020-12-01 08:21:32 +00:00
|
|
|
g_auto(GStrv) elems = NULL;
|
2019-04-12 13:59:10 +00:00
|
|
|
struct virQEMUQAPISchemaTraverseContext ctxt;
|
2019-04-15 13:33:58 +00:00
|
|
|
const char *cmdname;
|
2019-04-12 10:54:11 +00:00
|
|
|
int rc;
|
2018-03-20 07:44:11 +00:00
|
|
|
|
2019-04-09 12:09:33 +00:00
|
|
|
if (entry)
|
|
|
|
*entry = NULL;
|
2018-03-20 07:44:11 +00:00
|
|
|
|
2021-02-05 17:35:07 +00:00
|
|
|
if (!(elems = g_strsplit(query, "/", 0)))
|
2018-03-20 07:44:11 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!*elems) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed query string"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-04-12 13:59:10 +00:00
|
|
|
virQEMUQAPISchemaTraverseContextInit(&ctxt, elems, schema);
|
2019-04-15 13:33:58 +00:00
|
|
|
cmdname = virQEMUQAPISchemaTraverseContextNextQuery(&ctxt);
|
2019-04-12 10:54:11 +00:00
|
|
|
|
2019-04-15 13:33:58 +00:00
|
|
|
if (!virHashLookup(schema, cmdname))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rc = virQEMUQAPISchemaTraverse(cmdname, &ctxt);
|
2019-04-12 10:54:11 +00:00
|
|
|
|
|
|
|
if (entry)
|
|
|
|
*entry = ctxt.returnType;
|
|
|
|
|
2019-04-15 13:33:58 +00:00
|
|
|
if (rc >= 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (rc == -2) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("malformed QAPI schema when querying '%s' of '%s'"),
|
|
|
|
NULLSTR(ctxt.prevquery), query);
|
|
|
|
} else if (rc == -3) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("terminal QAPI query component '%s' of '%s' must not have followers"),
|
|
|
|
NULLSTR(ctxt.prevquery), query);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2018-03-20 07:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2018-03-20 07:57:44 +00:00
|
|
|
virQEMUQAPISchemaPathExists(const char *query,
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *schema)
|
2018-03-20 07:44:11 +00:00
|
|
|
{
|
2019-04-09 12:09:33 +00:00
|
|
|
return virQEMUQAPISchemaPathGet(query, schema, NULL) == 1;
|
2018-03-20 07:44:11 +00:00
|
|
|
}
|
2018-03-20 08:29:30 +00:00
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:33 +00:00
|
|
|
virQEMUQAPISchemaEntryProcess(size_t pos G_GNUC_UNUSED,
|
2018-03-20 08:29:30 +00:00
|
|
|
virJSONValuePtr item,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
const char *name;
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *schema = opaque;
|
2018-03-20 08:29:30 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *
|
2018-03-20 08:29:30 +00:00
|
|
|
virQEMUQAPISchemaConvert(virJSONValuePtr schemareply)
|
|
|
|
{
|
2020-10-22 17:04:18 +00:00
|
|
|
g_autoptr(GHashTable) schema = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) schemajson = schemareply;
|
2018-03-20 08:29:30 +00:00
|
|
|
|
2020-10-20 16:43:20 +00:00
|
|
|
if (!(schema = virHashNew(virJSONValueHashFree)))
|
2019-04-09 08:36:16 +00:00
|
|
|
return NULL;
|
2018-03-20 08:29:30 +00:00
|
|
|
|
2019-04-09 08:36:16 +00:00
|
|
|
if (virJSONValueArrayForeachSteal(schemajson,
|
2018-03-20 08:29:30 +00:00
|
|
|
virQEMUQAPISchemaEntryProcess,
|
|
|
|
schema) < 0)
|
2019-04-09 08:36:16 +00:00
|
|
|
return NULL;
|
2018-03-20 08:29:30 +00:00
|
|
|
|
2019-10-16 11:35:54 +00:00
|
|
|
return g_steal_pointer(&schema);
|
2018-03-20 08:29:30 +00:00
|
|
|
}
|