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 Visitor
*qobject_input_visitor_new_qmp(QObject
*obj
)
31 Visitor
*v
= qobject_input_visitor_new(obj
);
33 visit_set_policy(v
, &compat_policy
);
37 Visitor
*qobject_output_visitor_new_qmp(QObject
**result
)
39 Visitor
*v
= qobject_output_visitor_new(result
);
41 visit_set_policy(v
, &compat_policy
);
45 static QDict
*qmp_dispatch_check_obj(QDict
*dict
, bool allow_oob
,
48 const char *exec_key
= NULL
;
49 const QDictEntry
*ent
;
51 const QObject
*arg_obj
;
53 for (ent
= qdict_first(dict
); ent
;
54 ent
= qdict_next(dict
, ent
)) {
55 arg_name
= qdict_entry_key(ent
);
56 arg_obj
= qdict_entry_value(ent
);
58 if (!strcmp(arg_name
, "execute")
59 || (!strcmp(arg_name
, "exec-oob") && allow_oob
)) {
60 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
61 error_setg(errp
, "QMP input member '%s' must be a string",
66 error_setg(errp
, "QMP input member '%s' clashes with '%s'",
71 } else if (!strcmp(arg_name
, "arguments")) {
72 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
74 "QMP input member 'arguments' must be an object");
77 } else if (!strcmp(arg_name
, "id")) {
80 error_setg(errp
, "QMP input member '%s' is unexpected",
87 error_setg(errp
, "QMP input lacks member 'execute'");
94 QDict
*qmp_error_response(Error
*err
)
98 rsp
= qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
99 QapiErrorClass_str(error_get_class(err
)),
100 error_get_pretty(err
));
106 * Does @qdict look like a command to be run out-of-band?
108 bool qmp_is_oob(const QDict
*dict
)
110 return qdict_haskey(dict
, "exec-oob")
111 && !qdict_haskey(dict
, "execute");
114 typedef struct QmpDispatchBH
{
115 const QmpCommand
*cmd
;
123 static void do_qmp_dispatch_bh(void *opaque
)
125 QmpDispatchBH
*data
= opaque
;
127 assert(monitor_cur() == NULL
);
128 monitor_set_cur(qemu_coroutine_self(), data
->cur_mon
);
129 data
->cmd
->fn(data
->args
, data
->ret
, data
->errp
);
130 monitor_set_cur(qemu_coroutine_self(), NULL
);
131 aio_co_wake(data
->co
);
135 * Runs outside of coroutine context for OOB commands, but in coroutine
136 * context for everything else.
138 QDict
*qmp_dispatch(const QmpCommandList
*cmds
, QObject
*request
,
139 bool allow_oob
, Monitor
*cur_mon
)
145 const QmpCommand
*cmd
;
151 dict
= qobject_to(QDict
, request
);
154 error_setg(&err
, "QMP input must be a JSON object");
158 id
= qdict_get(dict
, "id");
160 if (!qmp_dispatch_check_obj(dict
, allow_oob
, &err
)) {
164 command
= qdict_get_try_str(dict
, "execute");
168 command
= qdict_get_str(dict
, "exec-oob");
171 cmd
= qmp_find_command(cmds
, command
);
173 error_set(&err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
174 "The command %s has not been found", command
);
177 if (!compat_policy_input_ok(cmd
->special_features
, &compat_policy
,
178 ERROR_CLASS_COMMAND_NOT_FOUND
,
179 "command", command
, &err
)) {
183 error_set(&err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
184 "Command %s has been disabled%s%s",
186 cmd
->disable_reason
? ": " : "",
187 cmd
->disable_reason
?: "");
190 if (oob
&& !(cmd
->options
& QCO_ALLOW_OOB
)) {
191 error_setg(&err
, "The command %s does not support OOB",
196 if (!qmp_command_available(cmd
, &err
)) {
200 if (!qdict_haskey(dict
, "arguments")) {
203 args
= qdict_get_qdict(dict
, "arguments");
207 assert(!(oob
&& qemu_in_coroutine()));
208 assert(monitor_cur() == NULL
);
209 if (!!(cmd
->options
& QCO_COROUTINE
) == qemu_in_coroutine()) {
210 monitor_set_cur(qemu_coroutine_self(), cur_mon
);
211 cmd
->fn(args
, &ret
, &err
);
212 monitor_set_cur(qemu_coroutine_self(), NULL
);
215 * Actual context doesn't match the one the command needs.
217 * Case 1: we are in coroutine context, but command does not
218 * have QCO_COROUTINE. We need to drop out of coroutine
219 * context for executing it.
221 * Case 2: we are outside coroutine context, but command has
222 * QCO_COROUTINE. Can't actually happen, because we get here
223 * outside coroutine context only when executing a command
224 * out of band, and OOB commands never have QCO_COROUTINE.
226 assert(!oob
&& qemu_in_coroutine() && !(cmd
->options
& QCO_COROUTINE
));
228 QmpDispatchBH data
= {
234 .co
= qemu_coroutine_self(),
236 aio_bh_schedule_oneshot(qemu_get_aio_context(), do_qmp_dispatch_bh
,
238 qemu_coroutine_yield();
242 /* or assert(!ret) after reviewing all handlers: */
247 if (cmd
->options
& QCO_NO_SUCCESS_RESP
) {
252 * When the command's schema has no 'returns', cmd->fn()
253 * leaves @ret null. The QMP spec calls for an empty object
256 ret
= QOBJECT(qdict_new());
260 qdict_put_obj(rsp
, "return", ret
);
265 rsp
= qmp_error_response(err
);
271 qdict_put_obj(rsp
, "id", qobject_ref(id
));