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/json-parser.h"
18 #include "qapi/qmp/qdict.h"
19 #include "qapi/qmp/qjson.h"
20 #include "qapi/qmp/qbool.h"
21 #include "sysemu/sysemu.h"
23 static QDict
*qmp_dispatch_check_obj(const QObject
*request
, bool allow_oob
,
26 const char *exec_key
= NULL
;
27 const QDictEntry
*ent
;
29 const QObject
*arg_obj
;
32 dict
= qobject_to(QDict
, request
);
34 error_setg(errp
, "QMP input must be a JSON object");
38 for (ent
= qdict_first(dict
); ent
;
39 ent
= qdict_next(dict
, ent
)) {
40 arg_name
= qdict_entry_key(ent
);
41 arg_obj
= qdict_entry_value(ent
);
43 if (!strcmp(arg_name
, "execute")
44 || (!strcmp(arg_name
, "exec-oob") && allow_oob
)) {
45 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
46 error_setg(errp
, "QMP input member '%s' must be a string",
51 error_setg(errp
, "QMP input member '%s' clashes with '%s'",
56 } else if (!strcmp(arg_name
, "arguments")) {
57 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
59 "QMP input member 'arguments' must be an object");
63 error_setg(errp
, "QMP input member '%s' is unexpected",
70 error_setg(errp
, "QMP input lacks member 'execute'");
77 static QObject
*do_qmp_dispatch(QmpCommandList
*cmds
, QObject
*request
,
78 bool allow_oob
, Error
**errp
)
80 Error
*local_err
= NULL
;
87 dict
= qmp_dispatch_check_obj(request
, allow_oob
, errp
);
92 command
= qdict_get_try_str(dict
, "execute");
96 command
= qdict_get_str(dict
, "exec-oob");
99 cmd
= qmp_find_command(cmds
, command
);
101 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
102 "The command %s has not been found", command
);
106 error_setg(errp
, "The command %s has been disabled for this instance",
110 if (oob
&& !(cmd
->options
& QCO_ALLOW_OOB
)) {
111 error_setg(errp
, "The command %s does not support OOB",
116 if (runstate_check(RUN_STATE_PRECONFIG
) &&
117 !(cmd
->options
& QCO_ALLOW_PRECONFIG
)) {
118 error_setg(errp
, "The command '%s' isn't permitted in '%s' state",
119 cmd
->name
, RunState_str(RUN_STATE_PRECONFIG
));
123 if (!qdict_haskey(dict
, "arguments")) {
126 args
= qdict_get_qdict(dict
, "arguments");
130 cmd
->fn(args
, &ret
, &local_err
);
132 error_propagate(errp
, local_err
);
133 } else if (cmd
->options
& QCO_NO_SUCCESS_RESP
) {
136 /* TODO turn into assertion */
137 ret
= QOBJECT(qdict_new());
145 QDict
*qmp_error_response(Error
*err
)
149 rsp
= qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
150 QapiErrorClass_str(error_get_class(err
)),
151 error_get_pretty(err
));
157 * Does @qdict look like a command to be run out-of-band?
159 bool qmp_is_oob(QDict
*dict
)
161 return qdict_haskey(dict
, "exec-oob")
162 && !qdict_haskey(dict
, "execute");
165 QDict
*qmp_dispatch(QmpCommandList
*cmds
, QObject
*request
,
172 ret
= do_qmp_dispatch(cmds
, request
, allow_oob
, &err
);
175 rsp
= qmp_error_response(err
);
178 qdict_put_obj(rsp
, "return", ret
);
180 /* Can only happen for commands with QCO_NO_SUCCESS_RESP */