From 38b3b20c66086a2ebcd4e74c99f94123f0e1f2e7 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 20 Mar 2018 09:29:30 +0100 Subject: [PATCH] qemu: monitor: Move processing of QMP schema to the new file The JSON array was processed to the hash table used by the query apis in the monitor code. Move it to a new helper in qemu_qapi.c. Signed-off-by: Peter Krempa --- src/qemu/qemu_capabilities.c | 9 +++++-- src/qemu/qemu_monitor.c | 2 +- src/qemu/qemu_monitor.h | 2 +- src/qemu/qemu_monitor_json.c | 41 ++++------------------------- src/qemu/qemu_monitor_json.h | 2 +- src/qemu/qemu_qapi.c | 50 ++++++++++++++++++++++++++++++++++++ src/qemu/qemu_qapi.h | 4 +++ 7 files changed, 69 insertions(+), 41 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 2c4a59bff4..7f40534732 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -4570,12 +4570,17 @@ virQEMUCapsProbeQMPSchemaCapabilities(virQEMUCapsPtr qemuCaps, qemuMonitorPtr mon) { struct virQEMUCapsStringFlags *entry; - virHashTablePtr schema; + virJSONValuePtr schemareply; + virHashTablePtr schema = NULL; size_t i; - if (!(schema = qemuMonitorQueryQMPSchema(mon))) + if (!(schemareply = qemuMonitorQueryQMPSchema(mon))) return -1; + if (!(schema = virQEMUQAPISchemaConvert(schemareply))) + return -1; + schemareply = NULL; + for (i = 0; i < ARRAY_CARDINALITY(virQEMUCapsQMPSchemaQueries); i++) { entry = virQEMUCapsQMPSchemaQueries + i; diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 1d67a97789..e169553b7e 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -4316,7 +4316,7 @@ qemuMonitorGetRTCTime(qemuMonitorPtr mon, } -virHashTablePtr +virJSONValuePtr qemuMonitorQueryQMPSchema(qemuMonitorPtr mon) { QEMU_CHECK_MONITOR_JSON_NULL(mon); diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index adfa87aba9..7a22323504 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -1175,7 +1175,7 @@ int qemuMonitorMigrateContinue(qemuMonitorPtr mon, int qemuMonitorGetRTCTime(qemuMonitorPtr mon, struct tm *tm); -virHashTablePtr qemuMonitorQueryQMPSchema(qemuMonitorPtr mon); +virJSONValuePtr qemuMonitorQueryQMPSchema(qemuMonitorPtr mon); int qemuMonitorSetBlockThreshold(qemuMonitorPtr mon, const char *nodename, diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 08dfffdf64..d80c4f18d1 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -7776,35 +7776,12 @@ qemuMonitorJSONGetHotpluggableCPUs(qemuMonitorPtr mon, } -static int -qemuMonitorJSONFillQMPSchema(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; -} - - -virHashTablePtr +virJSONValuePtr qemuMonitorJSONQueryQMPSchema(qemuMonitorPtr mon) { virJSONValuePtr cmd; virJSONValuePtr reply = NULL; - virJSONValuePtr arr; - virHashTablePtr schema = NULL; - virHashTablePtr ret = NULL; + virJSONValuePtr ret = NULL; if (!(cmd = qemuMonitorJSONMakeCommand("query-qmp-schema", NULL))) return NULL; @@ -7815,21 +7792,13 @@ qemuMonitorJSONQueryQMPSchema(qemuMonitorPtr mon) if (qemuMonitorJSONCheckError(cmd, reply) < 0) goto cleanup; - arr = virJSONValueObjectGet(reply, "return"); - - if (!(schema = virHashCreate(512, virJSONValueHashFree))) - goto cleanup; - - if (virJSONValueArrayForeachSteal(arr, qemuMonitorJSONFillQMPSchema, - schema) < 0) - goto cleanup; - - VIR_STEAL_PTR(ret, schema); + if (!(ret = virJSONValueObjectStealArray(reply, "return"))) + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-qmp-schema reply is not an array")); cleanup: virJSONValueFree(cmd); virJSONValueFree(reply); - virHashFree(schema); return ret; } diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index ec243becc4..846d366b27 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -516,7 +516,7 @@ int qemuMonitorJSONGetHotpluggableCPUs(qemuMonitorPtr mon, size_t *nentries) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); -virHashTablePtr qemuMonitorJSONQueryQMPSchema(qemuMonitorPtr mon) +virJSONValuePtr qemuMonitorJSONQueryQMPSchema(qemuMonitorPtr mon) ATTRIBUTE_NONNULL(1); int qemuMonitorJSONSetBlockThreshold(qemuMonitorPtr mon, diff --git a/src/qemu/qemu_qapi.c b/src/qemu/qemu_qapi.c index 7970cdedc4..3cb0cf0139 100644 --- a/src/qemu/qemu_qapi.c +++ b/src/qemu/qemu_qapi.c @@ -185,3 +185,53 @@ virQEMUQAPISchemaPathExists(const char *query, return !!entry; } + +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; +} diff --git a/src/qemu/qemu_qapi.h b/src/qemu/qemu_qapi.h index 3695e5846c..e074507dc8 100644 --- a/src/qemu/qemu_qapi.h +++ b/src/qemu/qemu_qapi.h @@ -33,4 +33,8 @@ bool virQEMUQAPISchemaPathExists(const char *query, virHashTablePtr schema); +virHashTablePtr +virQEMUQAPISchemaConvert(virJSONValuePtr schemareply); + + #endif /* __QEMU_QAPI_H__ */