]>
Commit | Line | Data |
---|---|---|
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" |
da34e65c | 15 | #include "qapi/error.h" |
7b1b5d19 | 16 | #include "qapi/qmp/dispatch.h" |
452fcdbc | 17 | #include "qapi/qmp/qdict.h" |
c7eb39cb | 18 | #include "qapi/qmp/qjson.h" |
54d31236 | 19 | #include "sysemu/runstate.h" |
cf869d53 | 20 | #include "qapi/qmp/qbool.h" |
ab02ab2a | 21 | |
a62c6174 | 22 | static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob, |
69240fe6 | 23 | Error **errp) |
ab02ab2a | 24 | { |
00ecec15 | 25 | const char *exec_key = NULL; |
ab02ab2a MR |
26 | const QDictEntry *ent; |
27 | const char *arg_name; | |
28 | const QObject *arg_obj; | |
ab02ab2a | 29 | |
ab02ab2a MR |
30 | for (ent = qdict_first(dict); ent; |
31 | ent = qdict_next(dict, ent)) { | |
32 | arg_name = qdict_entry_key(ent); | |
33 | arg_obj = qdict_entry_value(ent); | |
34 | ||
00ecec15 MA |
35 | if (!strcmp(arg_name, "execute") |
36 | || (!strcmp(arg_name, "exec-oob") && allow_oob)) { | |
ab02ab2a | 37 | if (qobject_type(arg_obj) != QTYPE_QSTRING) { |
00ecec15 MA |
38 | error_setg(errp, "QMP input member '%s' must be a string", |
39 | arg_name); | |
ab02ab2a MR |
40 | return NULL; |
41 | } | |
00ecec15 MA |
42 | if (exec_key) { |
43 | error_setg(errp, "QMP input member '%s' clashes with '%s'", | |
44 | arg_name, exec_key); | |
74d8c9d9 MA |
45 | return NULL; |
46 | } | |
00ecec15 MA |
47 | exec_key = arg_name; |
48 | } else if (!strcmp(arg_name, "arguments")) { | |
cf869d53 PX |
49 | if (qobject_type(arg_obj) != QTYPE_QDICT) { |
50 | error_setg(errp, | |
00ecec15 | 51 | "QMP input member 'arguments' must be an object"); |
cf869d53 PX |
52 | return NULL; |
53 | } | |
4eaca8de MAL |
54 | } else if (!strcmp(arg_name, "id")) { |
55 | continue; | |
74d8c9d9 | 56 | } else { |
10e37839 | 57 | error_setg(errp, "QMP input member '%s' is unexpected", |
99fb0c53 | 58 | arg_name); |
ab02ab2a MR |
59 | return NULL; |
60 | } | |
61 | } | |
62 | ||
00ecec15 | 63 | if (!exec_key) { |
10e37839 | 64 | error_setg(errp, "QMP input lacks member 'execute'"); |
ab02ab2a MR |
65 | return NULL; |
66 | } | |
67 | ||
68 | return dict; | |
69 | } | |
70 | ||
cf4a0643 MA |
71 | QDict *qmp_error_response(Error *err) |
72 | { | |
73 | QDict *rsp; | |
74 | ||
75 | rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }", | |
76 | QapiErrorClass_str(error_get_class(err)), | |
77 | error_get_pretty(err)); | |
78 | error_free(err); | |
79 | return rsp; | |
80 | } | |
81 | ||
82 | /* | |
83 | * Does @qdict look like a command to be run out-of-band? | |
84 | */ | |
85 | bool qmp_is_oob(const QDict *dict) | |
ab02ab2a | 86 | { |
cf4a0643 MA |
87 | return qdict_haskey(dict, "exec-oob") |
88 | && !qdict_haskey(dict, "execute"); | |
89 | } | |
90 | ||
f0ccc00b | 91 | QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request, |
cf4a0643 MA |
92 | bool allow_oob) |
93 | { | |
94 | Error *err = NULL; | |
69240fe6 | 95 | bool oob; |
ab02ab2a | 96 | const char *command; |
cf4a0643 | 97 | QDict *args; |
f0ccc00b | 98 | const QmpCommand *cmd; |
a62c6174 MA |
99 | QDict *dict; |
100 | QObject *id; | |
ab02ab2a | 101 | QObject *ret = NULL; |
d3226035 | 102 | QDict *rsp = NULL; |
ab02ab2a | 103 | |
a62c6174 | 104 | dict = qobject_to(QDict, request); |
4af8be1f | 105 | if (!dict) { |
a62c6174 MA |
106 | id = NULL; |
107 | error_setg(&err, "QMP input must be a JSON object"); | |
108 | goto out; | |
109 | } | |
110 | ||
111 | id = qdict_get(dict, "id"); | |
112 | ||
113 | if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) { | |
cf4a0643 | 114 | goto out; |
ab02ab2a MR |
115 | } |
116 | ||
00ecec15 | 117 | command = qdict_get_try_str(dict, "execute"); |
69240fe6 | 118 | oob = false; |
00ecec15 MA |
119 | if (!command) { |
120 | assert(allow_oob); | |
121 | command = qdict_get_str(dict, "exec-oob"); | |
69240fe6 | 122 | oob = true; |
00ecec15 | 123 | } |
1527badb | 124 | cmd = qmp_find_command(cmds, command); |
ab02ab2a | 125 | if (cmd == NULL) { |
cf4a0643 | 126 | error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND, |
a6c90cbc | 127 | "The command %s has not been found", command); |
cf4a0643 | 128 | goto out; |
ab02ab2a | 129 | } |
abd6cf6d | 130 | if (!cmd->enabled) { |
cf4a0643 | 131 | error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND, |
2546be1c MP |
132 | "The command %s has been disabled for this instance", |
133 | command); | |
cf4a0643 | 134 | goto out; |
abd6cf6d | 135 | } |
69240fe6 | 136 | if (oob && !(cmd->options & QCO_ALLOW_OOB)) { |
cf4a0643 | 137 | error_setg(&err, "The command %s does not support OOB", |
69240fe6 | 138 | command); |
cf4a0643 | 139 | goto out; |
69240fe6 | 140 | } |
ab02ab2a | 141 | |
047f7038 IM |
142 | if (runstate_check(RUN_STATE_PRECONFIG) && |
143 | !(cmd->options & QCO_ALLOW_PRECONFIG)) { | |
cf4a0643 | 144 | error_setg(&err, "The command '%s' isn't permitted in '%s' state", |
047f7038 | 145 | cmd->name, RunState_str(RUN_STATE_PRECONFIG)); |
cf4a0643 | 146 | goto out; |
047f7038 IM |
147 | } |
148 | ||
ab02ab2a MR |
149 | if (!qdict_haskey(dict, "arguments")) { |
150 | args = qdict_new(); | |
151 | } else { | |
152 | args = qdict_get_qdict(dict, "arguments"); | |
cb3e7f08 | 153 | qobject_ref(args); |
ab02ab2a | 154 | } |
cf4a0643 | 155 | cmd->fn(args, &ret, &err); |
d3226035 | 156 | qobject_unref(args); |
cf4a0643 | 157 | if (err) { |
b3fbb328 MAL |
158 | /* or assert(!ret) after reviewing all handlers: */ |
159 | qobject_unref(ret); | |
d3226035 MA |
160 | goto out; |
161 | } | |
162 | ||
163 | if (cmd->options & QCO_NO_SUCCESS_RESP) { | |
42a502a7 | 164 | g_assert(!ret); |
d3226035 | 165 | return NULL; |
42a502a7 | 166 | } else if (!ret) { |
4a883738 MA |
167 | /* |
168 | * When the command's schema has no 'returns', cmd->fn() | |
169 | * leaves @ret null. The QMP spec calls for an empty object | |
170 | * then; supply it. | |
171 | */ | |
42a502a7 | 172 | ret = QOBJECT(qdict_new()); |
ab02ab2a MR |
173 | } |
174 | ||
d3226035 MA |
175 | rsp = qdict_new(); |
176 | qdict_put_obj(rsp, "return", ret); | |
ab02ab2a | 177 | |
cf4a0643 | 178 | out: |
ab02ab2a | 179 | if (err) { |
d3226035 | 180 | assert(!rsp); |
cee32796 | 181 | rsp = qmp_error_response(err); |
ab02ab2a MR |
182 | } |
183 | ||
d3226035 MA |
184 | assert(rsp); |
185 | ||
186 | if (id) { | |
4eaca8de MAL |
187 | qdict_put_obj(rsp, "id", qobject_ref(id)); |
188 | } | |
189 | ||
d43b1694 | 190 | return rsp; |
ab02ab2a | 191 | } |