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 "qapi/qmp/types.h"
15 #include "qapi/qmp/dispatch.h"
16 #include "qapi/qmp/json-parser.h"
17 #include "qapi-types.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qerror.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 if (qobject_type(request
) != QTYPE_QDICT
) {
30 error_set(errp
, QERR_QMP_BAD_INPUT_OBJECT
,
31 "request is not a dictionary");
35 dict
= qobject_to_qdict(request
);
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
);
42 if (!strcmp(arg_name
, "execute")) {
43 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
44 error_set(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
49 } else if (strcmp(arg_name
, "arguments")) {
50 error_set(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
56 error_set(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
63 static QObject
*do_qmp_dispatch(QObject
*request
, Error
**errp
)
71 dict
= qmp_dispatch_check_obj(request
, errp
);
72 if (!dict
|| error_is_set(errp
)) {
76 command
= qdict_get_str(dict
, "execute");
77 cmd
= qmp_find_command(command
);
79 error_set(errp
, QERR_COMMAND_NOT_FOUND
, command
);
83 error_setg(errp
, "The command %s has been disabled for this instance",
88 if (!qdict_haskey(dict
, "arguments")) {
91 args
= qdict_get_qdict(dict
, "arguments");
97 cmd
->fn(args
, &ret
, errp
);
98 if (!error_is_set(errp
)) {
99 if (cmd
->options
& QCO_NO_SUCCESS_RESP
) {
102 ret
= QOBJECT(qdict_new());
113 QObject
*qmp_build_error_object(Error
*errp
)
115 return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
116 ErrorClass_lookup
[error_get_class(errp
)],
117 error_get_pretty(errp
));
120 QObject
*qmp_dispatch(QObject
*request
)
126 ret
= do_qmp_dispatch(request
, &err
);
130 qdict_put_obj(rsp
, "error", qmp_build_error_object(err
));
133 qdict_put_obj(rsp
, "return", ret
);