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