2 * Core Definitions for QAPI/QMP Dispatch
4 * Copyright IBM, Corp. 2011
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/json-parser.h"
18 #include "qapi/qmp/qdict.h"
19 #include "qapi/qmp/qjson.h"
21 static QDict
*qmp_dispatch_check_obj(const QObject
*request
, Error
**errp
)
23 const QDictEntry
*ent
;
25 const QObject
*arg_obj
;
26 bool has_exec_key
= false;
29 dict
= qobject_to_qdict(request
);
31 error_setg(errp
, "QMP input must be a JSON object");
35 for (ent
= qdict_first(dict
); ent
;
36 ent
= qdict_next(dict
, ent
)) {
37 arg_name
= qdict_entry_key(ent
);
38 arg_obj
= qdict_entry_value(ent
);
40 if (!strcmp(arg_name
, "execute")) {
41 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
43 "QMP input member 'execute' must be a string");
47 } else if (!strcmp(arg_name
, "arguments")) {
48 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
50 "QMP input member 'arguments' must be an object");
54 error_setg(errp
, "QMP input member '%s' is unexpected",
61 error_setg(errp
, "QMP input lacks member 'execute'");
68 static QObject
*do_qmp_dispatch(QmpCommandList
*cmds
, QObject
*request
,
71 Error
*local_err
= NULL
;
77 dict
= qmp_dispatch_check_obj(request
, errp
);
82 command
= qdict_get_str(dict
, "execute");
83 cmd
= qmp_find_command(cmds
, command
);
85 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
86 "The command %s has not been found", command
);
90 error_setg(errp
, "The command %s has been disabled for this instance",
95 if (!qdict_haskey(dict
, "arguments")) {
98 args
= qdict_get_qdict(dict
, "arguments");
102 cmd
->fn(args
, &ret
, &local_err
);
104 error_propagate(errp
, local_err
);
105 } else if (cmd
->options
& QCO_NO_SUCCESS_RESP
) {
108 ret
= QOBJECT(qdict_new());
116 QObject
*qmp_build_error_object(Error
*err
)
118 return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
119 QapiErrorClass_str(error_get_class(err
)),
120 error_get_pretty(err
));
123 QObject
*qmp_dispatch(QmpCommandList
*cmds
, QObject
*request
)
129 ret
= do_qmp_dispatch(cmds
, request
, &err
);
133 qdict_put_obj(rsp
, "error", qmp_build_error_object(err
));
136 qdict_put_obj(rsp
, "return", ret
);