elf_ops: Don't try to g_mapped_file_unref(NULL)
[qemu/ar7.git] / qapi / qmp-dispatch.c
blob79347e086452ef316490b47fa5df2d32b2b6245e
1 /*
2 * Core Definitions for QAPI/QMP Dispatch
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/dispatch.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qjson.h"
19 #include "sysemu/runstate.h"
20 #include "qapi/qmp/qbool.h"
22 static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob,
23 Error **errp)
25 const char *exec_key = NULL;
26 const QDictEntry *ent;
27 const char *arg_name;
28 const QObject *arg_obj;
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);
35 if (!strcmp(arg_name, "execute")
36 || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
37 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
38 error_setg(errp, "QMP input member '%s' must be a string",
39 arg_name);
40 return NULL;
42 if (exec_key) {
43 error_setg(errp, "QMP input member '%s' clashes with '%s'",
44 arg_name, exec_key);
45 return NULL;
47 exec_key = arg_name;
48 } else if (!strcmp(arg_name, "arguments")) {
49 if (qobject_type(arg_obj) != QTYPE_QDICT) {
50 error_setg(errp,
51 "QMP input member 'arguments' must be an object");
52 return NULL;
54 } else if (!strcmp(arg_name, "id")) {
55 continue;
56 } else {
57 error_setg(errp, "QMP input member '%s' is unexpected",
58 arg_name);
59 return NULL;
63 if (!exec_key) {
64 error_setg(errp, "QMP input lacks member 'execute'");
65 return NULL;
68 return dict;
71 QDict *qmp_error_response(Error *err)
73 QDict *rsp;
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;
83 * Does @qdict look like a command to be run out-of-band?
85 bool qmp_is_oob(const QDict *dict)
87 return qdict_haskey(dict, "exec-oob")
88 && !qdict_haskey(dict, "execute");
91 QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request,
92 bool allow_oob)
94 Error *err = NULL;
95 bool oob;
96 const char *command;
97 QDict *args;
98 const QmpCommand *cmd;
99 QDict *dict;
100 QObject *id;
101 QObject *ret = NULL;
102 QDict *rsp = NULL;
104 dict = qobject_to(QDict, request);
105 if (!dict) {
106 id = NULL;
107 error_setg(&err, "QMP input must be a JSON object");
108 goto out;
111 id = qdict_get(dict, "id");
113 if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) {
114 goto out;
117 command = qdict_get_try_str(dict, "execute");
118 oob = false;
119 if (!command) {
120 assert(allow_oob);
121 command = qdict_get_str(dict, "exec-oob");
122 oob = true;
124 cmd = qmp_find_command(cmds, command);
125 if (cmd == NULL) {
126 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
127 "The command %s has not been found", command);
128 goto out;
130 if (!cmd->enabled) {
131 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
132 "The command %s has been disabled for this instance",
133 command);
134 goto out;
136 if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
137 error_setg(&err, "The command %s does not support OOB",
138 command);
139 goto out;
142 if (runstate_check(RUN_STATE_PRECONFIG) &&
143 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
144 error_setg(&err, "The command '%s' isn't permitted in '%s' state",
145 cmd->name, RunState_str(RUN_STATE_PRECONFIG));
146 goto out;
149 if (!qdict_haskey(dict, "arguments")) {
150 args = qdict_new();
151 } else {
152 args = qdict_get_qdict(dict, "arguments");
153 qobject_ref(args);
155 cmd->fn(args, &ret, &err);
156 qobject_unref(args);
157 if (err) {
158 /* or assert(!ret) after reviewing all handlers: */
159 qobject_unref(ret);
160 goto out;
163 if (cmd->options & QCO_NO_SUCCESS_RESP) {
164 g_assert(!ret);
165 return NULL;
166 } else if (!ret) {
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.
172 ret = QOBJECT(qdict_new());
175 rsp = qdict_new();
176 qdict_put_obj(rsp, "return", ret);
178 out:
179 if (err) {
180 assert(!rsp);
181 rsp = qmp_error_response(err);
184 assert(rsp);
186 if (id) {
187 qdict_put_obj(rsp, "id", qobject_ref(id));
190 return rsp;