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"
16 #include "block/aio.h"
17 #include "qapi/compat-policy.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/dispatch.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qmp/qjson.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/qobject-output-visitor.h"
24 #include "sysemu/runstate.h"
25 #include "qapi/qmp/qbool.h"
26 #include "qemu/coroutine.h"
27 #include "qemu/main-loop.h"
29 CompatPolicy compat_policy;
31 Visitor *qobject_input_visitor_new_qmp(QObject *obj)
33 Visitor *v = qobject_input_visitor_new(obj);
35 visit_set_policy(v, &compat_policy);
39 Visitor *qobject_output_visitor_new_qmp(QObject **result)
41 Visitor *v = qobject_output_visitor_new(result);
43 visit_set_policy(v, &compat_policy);
47 static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob,
50 const char *exec_key = NULL;
51 const QDictEntry *ent;
53 const QObject *arg_obj;
55 for (ent = qdict_first(dict); ent;
56 ent = qdict_next(dict, ent)) {
57 arg_name = qdict_entry_key(ent);
58 arg_obj = qdict_entry_value(ent);
60 if (!strcmp(arg_name, "execute")
61 || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
62 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
63 error_setg(errp, "QMP input member '%s' must be a string",
68 error_setg(errp, "QMP input member '%s' clashes with '%s'",
73 } else if (!strcmp(arg_name, "arguments")) {
74 if (qobject_type(arg_obj) != QTYPE_QDICT) {
76 "QMP input member 'arguments' must be an object");
79 } else if (!strcmp(arg_name, "id")) {
82 error_setg(errp, "QMP input member '%s' is unexpected",
89 error_setg(errp, "QMP input lacks member 'execute'");
96 QDict *qmp_error_response(Error *err)
100 rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
101 QapiErrorClass_str(error_get_class(err)),
102 error_get_pretty(err));
108 * Does @qdict look like a command to be run out-of-band?
110 bool qmp_is_oob(const QDict *dict)
112 return qdict_haskey(dict, "exec-oob")
113 && !qdict_haskey(dict, "execute");
116 typedef struct QmpDispatchBH {
117 const QmpCommand *cmd;
125 static void do_qmp_dispatch_bh(void *opaque)
127 QmpDispatchBH *data = opaque;
129 assert(monitor_cur() == NULL);
130 monitor_set_cur(qemu_coroutine_self(), data->cur_mon);
131 data->cmd->fn(data->args, data->ret, data->errp);
132 monitor_set_cur(qemu_coroutine_self(), NULL);
133 aio_co_wake(data->co);
137 * Runs outside of coroutine context for OOB commands, but in coroutine
138 * context for everything else.
140 QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request,
141 bool allow_oob, Monitor *cur_mon)
147 const QmpCommand *cmd;
153 dict = qobject_to(QDict, request);
156 error_setg(&err, "QMP input must be a JSON object");
160 id = qdict_get(dict, "id");
162 if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) {
166 command = qdict_get_try_str(dict, "execute");
170 command = qdict_get_str(dict, "exec-oob");
173 cmd = qmp_find_command(cmds, command);
175 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
176 "The command %s has not been found", command);
179 if (cmd->options & QCO_DEPRECATED) {
180 switch (compat_policy.deprecated_input) {
181 case COMPAT_POLICY_INPUT_ACCEPT:
183 case COMPAT_POLICY_INPUT_REJECT:
184 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
185 "Deprecated command %s disabled by policy",
188 case COMPAT_POLICY_INPUT_CRASH:
194 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
195 "Command %s has been disabled%s%s",
197 cmd->disable_reason ? ": " : "",
198 cmd->disable_reason ?: "");
201 if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
202 error_setg(&err, "The command %s does not support OOB",
207 if (!qmp_command_available(cmd, &err)) {
211 if (!qdict_haskey(dict, "arguments")) {
214 args = qdict_get_qdict(dict, "arguments");
218 assert(!(oob && qemu_in_coroutine()));
219 assert(monitor_cur() == NULL);
220 if (!!(cmd->options & QCO_COROUTINE) == qemu_in_coroutine()) {
221 monitor_set_cur(qemu_coroutine_self(), cur_mon);
222 cmd->fn(args, &ret, &err);
223 monitor_set_cur(qemu_coroutine_self(), NULL);
226 * Actual context doesn't match the one the command needs.
228 * Case 1: we are in coroutine context, but command does not
229 * have QCO_COROUTINE. We need to drop out of coroutine
230 * context for executing it.
232 * Case 2: we are outside coroutine context, but command has
233 * QCO_COROUTINE. Can't actually happen, because we get here
234 * outside coroutine context only when executing a command
235 * out of band, and OOB commands never have QCO_COROUTINE.
237 assert(!oob && qemu_in_coroutine() && !(cmd->options & QCO_COROUTINE));
239 QmpDispatchBH data = {
245 .co = qemu_coroutine_self(),
247 aio_bh_schedule_oneshot(qemu_get_aio_context(), do_qmp_dispatch_bh,
249 qemu_coroutine_yield();
253 /* or assert(!ret) after reviewing all handlers: */
258 if (cmd->options & QCO_NO_SUCCESS_RESP) {
263 * When the command's schema has no 'returns', cmd->fn()
264 * leaves @ret null. The QMP spec calls for an empty object
267 ret = QOBJECT(qdict_new());
271 qdict_put_obj(rsp, "return", ret);
276 rsp = qmp_error_response(err);
282 qdict_put_obj(rsp, "id", qobject_ref(id));