2 * Core Definitions for QAPI/QMP Dispatch
4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/dispatch.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qjson.h"
19 #include "sysemu/runstate.h"
20 #include "qapi/qmp/qbool.h"
22 static QDict
*qmp_dispatch_check_obj(QDict
*dict
, bool allow_oob
,
25 const char *exec_key
= NULL
;
26 const QDictEntry
*ent
;
28 const QObject
*arg_obj
;
30 for (ent
= qdict_first(dict
); ent
;
31 ent
= qdict_next(dict
, ent
)) {
32 arg_name
= qdict_entry_key(ent
);
33 arg_obj
= qdict_entry_value(ent
);
35 if (!strcmp(arg_name
, "execute")
36 || (!strcmp(arg_name
, "exec-oob") && allow_oob
)) {
37 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
38 error_setg(errp
, "QMP input member '%s' must be a string",
43 error_setg(errp
, "QMP input member '%s' clashes with '%s'",
48 } else if (!strcmp(arg_name
, "arguments")) {
49 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
51 "QMP input member 'arguments' must be an object");
54 } else if (!strcmp(arg_name
, "id")) {
57 error_setg(errp
, "QMP input member '%s' is unexpected",
64 error_setg(errp
, "QMP input lacks member 'execute'");
71 QDict
*qmp_error_response(Error
*err
)
75 rsp
= qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
76 QapiErrorClass_str(error_get_class(err
)),
77 error_get_pretty(err
));
83 * Does @qdict look like a command to be run out-of-band?
85 bool qmp_is_oob(const QDict
*dict
)
87 return qdict_haskey(dict
, "exec-oob")
88 && !qdict_haskey(dict
, "execute");
91 QDict
*qmp_dispatch(const QmpCommandList
*cmds
, QObject
*request
,
98 const QmpCommand
*cmd
;
104 dict
= qobject_to(QDict
, request
);
107 error_setg(&err
, "QMP input must be a JSON object");
111 id
= qdict_get(dict
, "id");
113 if (!qmp_dispatch_check_obj(dict
, allow_oob
, &err
)) {
117 command
= qdict_get_try_str(dict
, "execute");
121 command
= qdict_get_str(dict
, "exec-oob");
124 cmd
= qmp_find_command(cmds
, command
);
126 error_set(&err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
127 "The command %s has not been found", command
);
131 error_set(&err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
132 "The command %s has been disabled for this instance",
136 if (oob
&& !(cmd
->options
& QCO_ALLOW_OOB
)) {
137 error_setg(&err
, "The command %s does not support OOB",
142 if (runstate_check(RUN_STATE_PRECONFIG
) &&
143 !(cmd
->options
& QCO_ALLOW_PRECONFIG
)) {
144 error_setg(&err
, "The command '%s' isn't permitted in '%s' state",
145 cmd
->name
, RunState_str(RUN_STATE_PRECONFIG
));
149 if (!qdict_haskey(dict
, "arguments")) {
152 args
= qdict_get_qdict(dict
, "arguments");
155 cmd
->fn(args
, &ret
, &err
);
158 /* or assert(!ret) after reviewing all handlers: */
163 if (cmd
->options
& QCO_NO_SUCCESS_RESP
) {
168 * When the command's schema has no 'returns', cmd->fn()
169 * leaves @ret null. The QMP spec calls for an empty object
172 ret
= QOBJECT(qdict_new());
176 qdict_put_obj(rsp
, "return", ret
);
181 rsp
= qmp_error_response(err
);
187 qdict_put_obj(rsp
, "id", qobject_ref(id
));