]> Git Repo - qemu.git/blame - qapi/qmp-dispatch.c
qapi/qom: Add ObjectOptions for filter-*
[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 <[email protected]>
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"
da34e65c 17#include "qapi/error.h"
7b1b5d19 18#include "qapi/qmp/dispatch.h"
452fcdbc 19#include "qapi/qmp/qdict.h"
c7eb39cb 20#include "qapi/qmp/qjson.h"
54d31236 21#include "sysemu/runstate.h"
cf869d53 22#include "qapi/qmp/qbool.h"
9ce44e2c
KW
23#include "qemu/coroutine.h"
24#include "qemu/main-loop.h"
ab02ab2a 25
a62c6174 26static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob,
69240fe6 27 Error **errp)
ab02ab2a 28{
00ecec15 29 const char *exec_key = NULL;
ab02ab2a
MR
30 const QDictEntry *ent;
31 const char *arg_name;
32 const QObject *arg_obj;
ab02ab2a 33
ab02ab2a
MR
34 for (ent = qdict_first(dict); ent;
35 ent = qdict_next(dict, ent)) {
36 arg_name = qdict_entry_key(ent);
37 arg_obj = qdict_entry_value(ent);
38
00ecec15
MA
39 if (!strcmp(arg_name, "execute")
40 || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
ab02ab2a 41 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
00ecec15
MA
42 error_setg(errp, "QMP input member '%s' must be a string",
43 arg_name);
ab02ab2a
MR
44 return NULL;
45 }
00ecec15
MA
46 if (exec_key) {
47 error_setg(errp, "QMP input member '%s' clashes with '%s'",
48 arg_name, exec_key);
74d8c9d9
MA
49 return NULL;
50 }
00ecec15
MA
51 exec_key = arg_name;
52 } else if (!strcmp(arg_name, "arguments")) {
cf869d53
PX
53 if (qobject_type(arg_obj) != QTYPE_QDICT) {
54 error_setg(errp,
00ecec15 55 "QMP input member 'arguments' must be an object");
cf869d53
PX
56 return NULL;
57 }
4eaca8de
MAL
58 } else if (!strcmp(arg_name, "id")) {
59 continue;
74d8c9d9 60 } else {
10e37839 61 error_setg(errp, "QMP input member '%s' is unexpected",
99fb0c53 62 arg_name);
ab02ab2a
MR
63 return NULL;
64 }
65 }
66
00ecec15 67 if (!exec_key) {
10e37839 68 error_setg(errp, "QMP input lacks member 'execute'");
ab02ab2a
MR
69 return NULL;
70 }
71
72 return dict;
73}
74
cf4a0643
MA
75QDict *qmp_error_response(Error *err)
76{
77 QDict *rsp;
78
79 rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
80 QapiErrorClass_str(error_get_class(err)),
81 error_get_pretty(err));
82 error_free(err);
83 return rsp;
84}
85
86/*
87 * Does @qdict look like a command to be run out-of-band?
88 */
89bool qmp_is_oob(const QDict *dict)
ab02ab2a 90{
cf4a0643
MA
91 return qdict_haskey(dict, "exec-oob")
92 && !qdict_haskey(dict, "execute");
93}
94
9ce44e2c
KW
95typedef struct QmpDispatchBH {
96 const QmpCommand *cmd;
97 Monitor *cur_mon;
98 QDict *args;
99 QObject **ret;
100 Error **errp;
101 Coroutine *co;
102} QmpDispatchBH;
103
104static void do_qmp_dispatch_bh(void *opaque)
105{
106 QmpDispatchBH *data = opaque;
107
108 assert(monitor_cur() == NULL);
109 monitor_set_cur(qemu_coroutine_self(), data->cur_mon);
110 data->cmd->fn(data->args, data->ret, data->errp);
111 monitor_set_cur(qemu_coroutine_self(), NULL);
112 aio_co_wake(data->co);
113}
114
115/*
116 * Runs outside of coroutine context for OOB commands, but in coroutine
117 * context for everything else.
118 */
f0ccc00b 119QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request,
41725fa7 120 bool allow_oob, Monitor *cur_mon)
cf4a0643
MA
121{
122 Error *err = NULL;
69240fe6 123 bool oob;
ab02ab2a 124 const char *command;
cf4a0643 125 QDict *args;
f0ccc00b 126 const QmpCommand *cmd;
a62c6174
MA
127 QDict *dict;
128 QObject *id;
ab02ab2a 129 QObject *ret = NULL;
d3226035 130 QDict *rsp = NULL;
ab02ab2a 131
a62c6174 132 dict = qobject_to(QDict, request);
4af8be1f 133 if (!dict) {
a62c6174
MA
134 id = NULL;
135 error_setg(&err, "QMP input must be a JSON object");
136 goto out;
137 }
138
139 id = qdict_get(dict, "id");
140
141 if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) {
cf4a0643 142 goto out;
ab02ab2a
MR
143 }
144
00ecec15 145 command = qdict_get_try_str(dict, "execute");
69240fe6 146 oob = false;
00ecec15
MA
147 if (!command) {
148 assert(allow_oob);
149 command = qdict_get_str(dict, "exec-oob");
69240fe6 150 oob = true;
00ecec15 151 }
1527badb 152 cmd = qmp_find_command(cmds, command);
ab02ab2a 153 if (cmd == NULL) {
cf4a0643 154 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
a6c90cbc 155 "The command %s has not been found", command);
cf4a0643 156 goto out;
ab02ab2a 157 }
abd6cf6d 158 if (!cmd->enabled) {
cf4a0643 159 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
c98939da
MAL
160 "Command %s has been disabled%s%s",
161 command,
162 cmd->disable_reason ? ": " : "",
163 cmd->disable_reason ?: "");
cf4a0643 164 goto out;
abd6cf6d 165 }
69240fe6 166 if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
cf4a0643 167 error_setg(&err, "The command %s does not support OOB",
69240fe6 168 command);
cf4a0643 169 goto out;
69240fe6 170 }
ab02ab2a 171
164dafd1 172 if (!qmp_command_available(cmd, &err)) {
cf4a0643 173 goto out;
047f7038
IM
174 }
175
ab02ab2a
MR
176 if (!qdict_haskey(dict, "arguments")) {
177 args = qdict_new();
178 } else {
179 args = qdict_get_qdict(dict, "arguments");
cb3e7f08 180 qobject_ref(args);
ab02ab2a 181 }
41725fa7 182
9ce44e2c 183 assert(!(oob && qemu_in_coroutine()));
41725fa7 184 assert(monitor_cur() == NULL);
9ce44e2c
KW
185 if (!!(cmd->options & QCO_COROUTINE) == qemu_in_coroutine()) {
186 monitor_set_cur(qemu_coroutine_self(), cur_mon);
187 cmd->fn(args, &ret, &err);
188 monitor_set_cur(qemu_coroutine_self(), NULL);
189 } else {
190 /*
191 * Actual context doesn't match the one the command needs.
192 *
193 * Case 1: we are in coroutine context, but command does not
194 * have QCO_COROUTINE. We need to drop out of coroutine
195 * context for executing it.
196 *
197 * Case 2: we are outside coroutine context, but command has
198 * QCO_COROUTINE. Can't actually happen, because we get here
199 * outside coroutine context only when executing a command
200 * out of band, and OOB commands never have QCO_COROUTINE.
201 */
202 assert(!oob && qemu_in_coroutine() && !(cmd->options & QCO_COROUTINE));
203
204 QmpDispatchBH data = {
205 .cur_mon = cur_mon,
206 .cmd = cmd,
207 .args = args,
208 .ret = &ret,
209 .errp = &err,
210 .co = qemu_coroutine_self(),
211 };
212 aio_bh_schedule_oneshot(qemu_get_aio_context(), do_qmp_dispatch_bh,
213 &data);
214 qemu_coroutine_yield();
215 }
d3226035 216 qobject_unref(args);
cf4a0643 217 if (err) {
b3fbb328
MAL
218 /* or assert(!ret) after reviewing all handlers: */
219 qobject_unref(ret);
d3226035
MA
220 goto out;
221 }
222
223 if (cmd->options & QCO_NO_SUCCESS_RESP) {
42a502a7 224 g_assert(!ret);
d3226035 225 return NULL;
42a502a7 226 } else if (!ret) {
4a883738
MA
227 /*
228 * When the command's schema has no 'returns', cmd->fn()
229 * leaves @ret null. The QMP spec calls for an empty object
230 * then; supply it.
231 */
42a502a7 232 ret = QOBJECT(qdict_new());
ab02ab2a
MR
233 }
234
d3226035
MA
235 rsp = qdict_new();
236 qdict_put_obj(rsp, "return", ret);
ab02ab2a 237
cf4a0643 238out:
ab02ab2a 239 if (err) {
d3226035 240 assert(!rsp);
cee32796 241 rsp = qmp_error_response(err);
ab02ab2a
MR
242 }
243
d3226035
MA
244 assert(rsp);
245
246 if (id) {
4eaca8de
MAL
247 qdict_put_obj(rsp, "id", qobject_ref(id));
248 }
249
d43b1694 250 return rsp;
ab02ab2a 251}
This page took 0.532281 seconds and 4 git commands to generate.