2011-07-20 03:50:37 +08:00
|
|
|
/*
|
|
|
|
* Core Definitions for QAPI/QMP Dispatch
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-30 01:49:57 +08:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2012-12-18 01:19:43 +08:00
|
|
|
#include "qapi/qmp/types.h"
|
|
|
|
#include "qapi/qmp/dispatch.h"
|
|
|
|
#include "qapi/qmp/json-parser.h"
|
2016-06-10 00:48:32 +08:00
|
|
|
#include "qapi/qmp/qjson.h"
|
2012-08-02 03:30:13 +08:00
|
|
|
#include "qapi-types.h"
|
2012-12-18 01:19:43 +08:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2011-07-20 03:50:37 +08:00
|
|
|
|
|
|
|
static QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp)
|
|
|
|
{
|
|
|
|
const QDictEntry *ent;
|
|
|
|
const char *arg_name;
|
|
|
|
const QObject *arg_obj;
|
|
|
|
bool has_exec_key = false;
|
|
|
|
QDict *dict = NULL;
|
|
|
|
|
2017-02-18 04:38:18 +08:00
|
|
|
dict = qobject_to_qdict(request);
|
|
|
|
if (!dict) {
|
2017-04-27 16:41:23 +08:00
|
|
|
error_setg(errp, "QMP input must be a JSON object");
|
2011-07-20 03:50:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ent = qdict_first(dict); ent;
|
|
|
|
ent = qdict_next(dict, ent)) {
|
|
|
|
arg_name = qdict_entry_key(ent);
|
|
|
|
arg_obj = qdict_entry_value(ent);
|
|
|
|
|
|
|
|
if (!strcmp(arg_name, "execute")) {
|
|
|
|
if (qobject_type(arg_obj) != QTYPE_QSTRING) {
|
2017-04-27 16:41:23 +08:00
|
|
|
error_setg(errp,
|
|
|
|
"QMP input member 'execute' must be a string");
|
2011-07-20 03:50:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
has_exec_key = true;
|
2017-03-03 20:32:21 +08:00
|
|
|
} else if (!strcmp(arg_name, "arguments")) {
|
|
|
|
if (qobject_type(arg_obj) != QTYPE_QDICT) {
|
2017-04-27 16:41:23 +08:00
|
|
|
error_setg(errp,
|
|
|
|
"QMP input member 'arguments' must be an object");
|
2017-03-03 20:32:21 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-27 16:41:23 +08:00
|
|
|
error_setg(errp, "QMP input member '%s' is unexpected",
|
2017-03-03 20:32:29 +08:00
|
|
|
arg_name);
|
2011-07-20 03:50:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_exec_key) {
|
2017-04-27 16:41:23 +08:00
|
|
|
error_setg(errp, "QMP input lacks member 'execute'");
|
2011-07-20 03:50:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:32:25 +08:00
|
|
|
static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request,
|
|
|
|
Error **errp)
|
2011-07-20 03:50:37 +08:00
|
|
|
{
|
2014-05-02 19:26:37 +08:00
|
|
|
Error *local_err = NULL;
|
2011-07-20 03:50:37 +08:00
|
|
|
const char *command;
|
|
|
|
QDict *args, *dict;
|
|
|
|
QmpCommand *cmd;
|
|
|
|
QObject *ret = NULL;
|
|
|
|
|
|
|
|
dict = qmp_dispatch_check_obj(request, errp);
|
2014-05-02 19:26:35 +08:00
|
|
|
if (!dict) {
|
2011-07-20 03:50:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
command = qdict_get_str(dict, "execute");
|
2017-03-03 20:32:25 +08:00
|
|
|
cmd = qmp_find_command(cmds, command);
|
2011-07-20 03:50:37 +08:00
|
|
|
if (cmd == NULL) {
|
2015-01-13 23:16:35 +08:00
|
|
|
error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
|
|
|
|
"The command %s has not been found", command);
|
2011-07-20 03:50:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-12-07 12:03:42 +08:00
|
|
|
if (!cmd->enabled) {
|
2014-03-22 07:42:26 +08:00
|
|
|
error_setg(errp, "The command %s has been disabled for this instance",
|
|
|
|
command);
|
2011-12-07 12:03:42 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-07-20 03:50:37 +08:00
|
|
|
|
|
|
|
if (!qdict_haskey(dict, "arguments")) {
|
|
|
|
args = qdict_new();
|
|
|
|
} else {
|
|
|
|
args = qdict_get_qdict(dict, "arguments");
|
|
|
|
QINCREF(args);
|
|
|
|
}
|
|
|
|
|
2016-04-29 05:45:11 +08:00
|
|
|
cmd->fn(args, &ret, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
} else if (cmd->options & QCO_NO_SUCCESS_RESP) {
|
|
|
|
g_assert(!ret);
|
|
|
|
} else if (!ret) {
|
|
|
|
ret = QOBJECT(qdict_new());
|
2011-07-20 03:50:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QDECREF(args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:26:29 +08:00
|
|
|
QObject *qmp_build_error_object(Error *err)
|
2012-08-02 03:30:13 +08:00
|
|
|
{
|
|
|
|
return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
|
2017-08-24 16:46:08 +08:00
|
|
|
QapiErrorClass_str(error_get_class(err)),
|
2014-05-02 19:26:29 +08:00
|
|
|
error_get_pretty(err));
|
2012-08-02 03:30:13 +08:00
|
|
|
}
|
|
|
|
|
2017-03-03 20:32:25 +08:00
|
|
|
QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request)
|
2011-07-20 03:50:37 +08:00
|
|
|
{
|
|
|
|
Error *err = NULL;
|
|
|
|
QObject *ret;
|
|
|
|
QDict *rsp;
|
|
|
|
|
2017-03-03 20:32:25 +08:00
|
|
|
ret = do_qmp_dispatch(cmds, request, &err);
|
2011-07-20 03:50:37 +08:00
|
|
|
|
|
|
|
rsp = qdict_new();
|
|
|
|
if (err) {
|
2012-08-02 03:30:13 +08:00
|
|
|
qdict_put_obj(rsp, "error", qmp_build_error_object(err));
|
2011-07-20 03:50:37 +08:00
|
|
|
error_free(err);
|
|
|
|
} else if (ret) {
|
|
|
|
qdict_put_obj(rsp, "return", ret);
|
|
|
|
} else {
|
|
|
|
QDECREF(rsp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QOBJECT(rsp);
|
|
|
|
}
|