]> Git Repo - qemu.git/blame - qapi/qmp-dispatch.c
tests/test-qga: Demonstrate the guest-agent ignores "control"
[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"
da34e65c 15#include "qapi/error.h"
7b1b5d19
PB
16#include "qapi/qmp/dispatch.h"
17#include "qapi/qmp/json-parser.h"
452fcdbc 18#include "qapi/qmp/qdict.h"
c7eb39cb 19#include "qapi/qmp/qjson.h"
cf869d53 20#include "qapi/qmp/qbool.h"
047f7038 21#include "sysemu/sysemu.h"
ab02ab2a 22
cf869d53 23QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp)
ab02ab2a
MR
24{
25 const QDictEntry *ent;
26 const char *arg_name;
27 const QObject *arg_obj;
28 bool has_exec_key = false;
29 QDict *dict = NULL;
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
42 if (!strcmp(arg_name, "execute")) {
43 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
10e37839
MA
44 error_setg(errp,
45 "QMP input member 'execute' must be a string");
ab02ab2a
MR
46 return NULL;
47 }
48 has_exec_key = true;
74d8c9d9
MA
49 } else if (!strcmp(arg_name, "arguments")) {
50 if (qobject_type(arg_obj) != QTYPE_QDICT) {
10e37839
MA
51 error_setg(errp,
52 "QMP input member 'arguments' must be an object");
74d8c9d9
MA
53 return NULL;
54 }
cf869d53
PX
55 } else if (!strcmp(arg_name, "control")) {
56 if (qobject_type(arg_obj) != QTYPE_QDICT) {
57 error_setg(errp,
58 "QMP input member 'control' must be a dict");
59 return NULL;
60 }
74d8c9d9 61 } else {
10e37839 62 error_setg(errp, "QMP input member '%s' is unexpected",
99fb0c53 63 arg_name);
ab02ab2a
MR
64 return NULL;
65 }
66 }
67
68 if (!has_exec_key) {
10e37839 69 error_setg(errp, "QMP input lacks member 'execute'");
ab02ab2a
MR
70 return NULL;
71 }
72
73 return dict;
74}
75
1527badb
MA
76static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request,
77 Error **errp)
ab02ab2a 78{
ee16ce93 79 Error *local_err = NULL;
ab02ab2a
MR
80 const char *command;
81 QDict *args, *dict;
82 QmpCommand *cmd;
83 QObject *ret = NULL;
84
ab02ab2a 85 dict = qmp_dispatch_check_obj(request, errp);
4af8be1f 86 if (!dict) {
ab02ab2a
MR
87 return NULL;
88 }
89
90 command = qdict_get_str(dict, "execute");
1527badb 91 cmd = qmp_find_command(cmds, command);
ab02ab2a 92 if (cmd == NULL) {
a6c90cbc
MA
93 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
94 "The command %s has not been found", command);
ab02ab2a
MR
95 return NULL;
96 }
abd6cf6d 97 if (!cmd->enabled) {
f231b88d
CR
98 error_setg(errp, "The command %s has been disabled for this instance",
99 command);
abd6cf6d
MR
100 return NULL;
101 }
ab02ab2a 102
047f7038
IM
103 if (runstate_check(RUN_STATE_PRECONFIG) &&
104 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
105 error_setg(errp, "The command '%s' isn't permitted in '%s' state",
106 cmd->name, RunState_str(RUN_STATE_PRECONFIG));
107 return NULL;
108 }
109
ab02ab2a
MR
110 if (!qdict_haskey(dict, "arguments")) {
111 args = qdict_new();
112 } else {
113 args = qdict_get_qdict(dict, "arguments");
cb3e7f08 114 qobject_ref(args);
ab02ab2a
MR
115 }
116
42a502a7
EB
117 cmd->fn(args, &ret, &local_err);
118 if (local_err) {
119 error_propagate(errp, local_err);
120 } else if (cmd->options & QCO_NO_SUCCESS_RESP) {
121 g_assert(!ret);
122 } else if (!ret) {
123 ret = QOBJECT(qdict_new());
ab02ab2a
MR
124 }
125
cb3e7f08 126 qobject_unref(args);
ab02ab2a
MR
127
128 return ret;
129}
130
e940f543 131QObject *qmp_build_error_object(Error *err)
93b91c59
LC
132{
133 return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
977c736f 134 QapiErrorClass_str(error_get_class(err)),
e940f543 135 error_get_pretty(err));
93b91c59
LC
136}
137
cf869d53
PX
138/*
139 * Detect whether a request should be run out-of-band, by quickly
140 * peeking at whether we have: { "control": { "run-oob": true } }. By
141 * default commands are run in-band.
142 */
143bool qmp_is_oob(QDict *dict)
144{
145 QBool *bool_obj;
146
147 dict = qdict_get_qdict(dict, "control");
148 if (!dict) {
149 return false;
150 }
151
152 bool_obj = qobject_to(QBool, qdict_get(dict, "run-oob"));
153 if (!bool_obj) {
154 return false;
155 }
156
157 return qbool_get_bool(bool_obj);
158}
159
1527badb 160QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request)
ab02ab2a
MR
161{
162 Error *err = NULL;
163 QObject *ret;
164 QDict *rsp;
165
1527badb 166 ret = do_qmp_dispatch(cmds, request, &err);
ab02ab2a
MR
167
168 rsp = qdict_new();
169 if (err) {
93b91c59 170 qdict_put_obj(rsp, "error", qmp_build_error_object(err));
ab02ab2a
MR
171 error_free(err);
172 } else if (ret) {
173 qdict_put_obj(rsp, "return", ret);
174 } else {
cb3e7f08 175 qobject_unref(rsp);
ab02ab2a
MR
176 return NULL;
177 }
178
179 return QOBJECT(rsp);
180}
This page took 0.429056 seconds and 4 git commands to generate.