]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/qmp-dispatch.c
block/export: Conditionally ignore set-context error
[mirror_qemu.git] / qapi / qmp-dispatch.c
CommitLineData
ab02ab2a
MR
1/*
2 * Core Definitions for QAPI/QMP Dispatch
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
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.
11 *
12 */
13
cbf21151 14#include "qemu/osdep.h"
9ce44e2c
KW
15
16#include "block/aio.h"
6dd75472 17#include "qapi/compat-policy.h"
da34e65c 18#include "qapi/error.h"
7b1b5d19 19#include "qapi/qmp/dispatch.h"
452fcdbc 20#include "qapi/qmp/qdict.h"
c7eb39cb 21#include "qapi/qmp/qjson.h"
db291641 22#include "qapi/qobject-input-visitor.h"
91fa93e5 23#include "qapi/qobject-output-visitor.h"
54d31236 24#include "sysemu/runstate.h"
cf869d53 25#include "qapi/qmp/qbool.h"
9ce44e2c
KW
26#include "qemu/coroutine.h"
27#include "qemu/main-loop.h"
ab02ab2a 28
6dd75472
MA
29CompatPolicy compat_policy;
30
db291641
MA
31Visitor *qobject_input_visitor_new_qmp(QObject *obj)
32{
33 Visitor *v = qobject_input_visitor_new(obj);
34
35 qobject_input_visitor_set_policy(v, compat_policy.deprecated_input);
36 return v;
37}
38
91fa93e5
MA
39Visitor *qobject_output_visitor_new_qmp(QObject **result)
40{
41 Visitor *v = qobject_output_visitor_new(result);
42
43 qobject_output_visitor_set_policy(v, compat_policy.deprecated_output);
44 return v;
45}
46
a62c6174 47static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob,
69240fe6 48 Error **errp)
ab02ab2a 49{
00ecec15 50 const char *exec_key = NULL;
ab02ab2a
MR
51 const QDictEntry *ent;
52 const char *arg_name;
53 const QObject *arg_obj;
ab02ab2a 54
ab02ab2a
MR
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);
59
00ecec15
MA
60 if (!strcmp(arg_name, "execute")
61 || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
ab02ab2a 62 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
00ecec15
MA
63 error_setg(errp, "QMP input member '%s' must be a string",
64 arg_name);
ab02ab2a
MR
65 return NULL;
66 }
00ecec15
MA
67 if (exec_key) {
68 error_setg(errp, "QMP input member '%s' clashes with '%s'",
69 arg_name, exec_key);
74d8c9d9
MA
70 return NULL;
71 }
00ecec15
MA
72 exec_key = arg_name;
73 } else if (!strcmp(arg_name, "arguments")) {
cf869d53
PX
74 if (qobject_type(arg_obj) != QTYPE_QDICT) {
75 error_setg(errp,
00ecec15 76 "QMP input member 'arguments' must be an object");
cf869d53
PX
77 return NULL;
78 }
4eaca8de
MAL
79 } else if (!strcmp(arg_name, "id")) {
80 continue;
74d8c9d9 81 } else {
10e37839 82 error_setg(errp, "QMP input member '%s' is unexpected",
99fb0c53 83 arg_name);
ab02ab2a
MR
84 return NULL;
85 }
86 }
87
00ecec15 88 if (!exec_key) {
10e37839 89 error_setg(errp, "QMP input lacks member 'execute'");
ab02ab2a
MR
90 return NULL;
91 }
92
93 return dict;
94}
95
cf4a0643
MA
96QDict *qmp_error_response(Error *err)
97{
98 QDict *rsp;
99
100 rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
101 QapiErrorClass_str(error_get_class(err)),
102 error_get_pretty(err));
103 error_free(err);
104 return rsp;
105}
106
107/*
108 * Does @qdict look like a command to be run out-of-band?
109 */
110bool qmp_is_oob(const QDict *dict)
ab02ab2a 111{
cf4a0643
MA
112 return qdict_haskey(dict, "exec-oob")
113 && !qdict_haskey(dict, "execute");
114}
115
9ce44e2c
KW
116typedef struct QmpDispatchBH {
117 const QmpCommand *cmd;
118 Monitor *cur_mon;
119 QDict *args;
120 QObject **ret;
121 Error **errp;
122 Coroutine *co;
123} QmpDispatchBH;
124
125static void do_qmp_dispatch_bh(void *opaque)
126{
127 QmpDispatchBH *data = opaque;
128
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);
134}
135
136/*
137 * Runs outside of coroutine context for OOB commands, but in coroutine
138 * context for everything else.
139 */
f0ccc00b 140QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request,
41725fa7 141 bool allow_oob, Monitor *cur_mon)
cf4a0643
MA
142{
143 Error *err = NULL;
69240fe6 144 bool oob;
ab02ab2a 145 const char *command;
cf4a0643 146 QDict *args;
f0ccc00b 147 const QmpCommand *cmd;
a62c6174
MA
148 QDict *dict;
149 QObject *id;
ab02ab2a 150 QObject *ret = NULL;
d3226035 151 QDict *rsp = NULL;
ab02ab2a 152
a62c6174 153 dict = qobject_to(QDict, request);
4af8be1f 154 if (!dict) {
a62c6174
MA
155 id = NULL;
156 error_setg(&err, "QMP input must be a JSON object");
157 goto out;
158 }
159
160 id = qdict_get(dict, "id");
161
162 if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) {
cf4a0643 163 goto out;
ab02ab2a
MR
164 }
165
00ecec15 166 command = qdict_get_try_str(dict, "execute");
69240fe6 167 oob = false;
00ecec15
MA
168 if (!command) {
169 assert(allow_oob);
170 command = qdict_get_str(dict, "exec-oob");
69240fe6 171 oob = true;
00ecec15 172 }
1527badb 173 cmd = qmp_find_command(cmds, command);
ab02ab2a 174 if (cmd == NULL) {
cf4a0643 175 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
a6c90cbc 176 "The command %s has not been found", command);
cf4a0643 177 goto out;
ab02ab2a 178 }
d2032598
MA
179 if (cmd->options & QCO_DEPRECATED) {
180 switch (compat_policy.deprecated_input) {
181 case COMPAT_POLICY_INPUT_ACCEPT:
182 break;
183 case COMPAT_POLICY_INPUT_REJECT:
184 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
185 "Deprecated command %s disabled by policy",
186 command);
187 goto out;
dbb675c1 188 case COMPAT_POLICY_INPUT_CRASH:
d2032598
MA
189 default:
190 abort();
191 }
192 }
abd6cf6d 193 if (!cmd->enabled) {
cf4a0643 194 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
c98939da
MAL
195 "Command %s has been disabled%s%s",
196 command,
197 cmd->disable_reason ? ": " : "",
198 cmd->disable_reason ?: "");
cf4a0643 199 goto out;
abd6cf6d 200 }
69240fe6 201 if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
cf4a0643 202 error_setg(&err, "The command %s does not support OOB",
69240fe6 203 command);
cf4a0643 204 goto out;
69240fe6 205 }
ab02ab2a 206
164dafd1 207 if (!qmp_command_available(cmd, &err)) {
cf4a0643 208 goto out;
047f7038
IM
209 }
210
ab02ab2a
MR
211 if (!qdict_haskey(dict, "arguments")) {
212 args = qdict_new();
213 } else {
214 args = qdict_get_qdict(dict, "arguments");
cb3e7f08 215 qobject_ref(args);
ab02ab2a 216 }
41725fa7 217
9ce44e2c 218 assert(!(oob && qemu_in_coroutine()));
41725fa7 219 assert(monitor_cur() == NULL);
9ce44e2c
KW
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);
224 } else {
225 /*
226 * Actual context doesn't match the one the command needs.
227 *
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.
231 *
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.
236 */
237 assert(!oob && qemu_in_coroutine() && !(cmd->options & QCO_COROUTINE));
238
239 QmpDispatchBH data = {
240 .cur_mon = cur_mon,
241 .cmd = cmd,
242 .args = args,
243 .ret = &ret,
244 .errp = &err,
245 .co = qemu_coroutine_self(),
246 };
247 aio_bh_schedule_oneshot(qemu_get_aio_context(), do_qmp_dispatch_bh,
248 &data);
249 qemu_coroutine_yield();
250 }
d3226035 251 qobject_unref(args);
cf4a0643 252 if (err) {
b3fbb328
MAL
253 /* or assert(!ret) after reviewing all handlers: */
254 qobject_unref(ret);
d3226035
MA
255 goto out;
256 }
257
258 if (cmd->options & QCO_NO_SUCCESS_RESP) {
42a502a7 259 g_assert(!ret);
d3226035 260 return NULL;
42a502a7 261 } else if (!ret) {
4a883738
MA
262 /*
263 * When the command's schema has no 'returns', cmd->fn()
264 * leaves @ret null. The QMP spec calls for an empty object
265 * then; supply it.
266 */
42a502a7 267 ret = QOBJECT(qdict_new());
ab02ab2a
MR
268 }
269
d3226035
MA
270 rsp = qdict_new();
271 qdict_put_obj(rsp, "return", ret);
ab02ab2a 272
cf4a0643 273out:
ab02ab2a 274 if (err) {
d3226035 275 assert(!rsp);
cee32796 276 rsp = qmp_error_response(err);
ab02ab2a
MR
277 }
278
d3226035
MA
279 assert(rsp);
280
281 if (id) {
4eaca8de
MAL
282 qdict_put_obj(rsp, "id", qobject_ref(id));
283 }
284
d43b1694 285 return rsp;
ab02ab2a 286}