xref: /openbmc/qemu/qapi/qmp-dispatch.c (revision 4a09d0bb)
1 /*
2  * Core Definitions for QAPI/QMP Dispatch
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
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 
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/types.h"
17 #include "qapi/qmp/dispatch.h"
18 #include "qapi/qmp/json-parser.h"
19 #include "qapi/qmp/qjson.h"
20 #include "qapi-types.h"
21 #include "qapi/qmp/qerror.h"
22 
23 static QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp)
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 
31     if (qobject_type(request) != QTYPE_QDICT) {
32         error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT,
33                    "request is not a dictionary");
34         return NULL;
35     }
36 
37     dict = qobject_to_qdict(request);
38 
39     for (ent = qdict_first(dict); ent;
40          ent = qdict_next(dict, ent)) {
41         arg_name = qdict_entry_key(ent);
42         arg_obj = qdict_entry_value(ent);
43 
44         if (!strcmp(arg_name, "execute")) {
45             if (qobject_type(arg_obj) != QTYPE_QSTRING) {
46                 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
47                            "string");
48                 return NULL;
49             }
50             has_exec_key = true;
51         } else if (strcmp(arg_name, "arguments")) {
52             error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
53             return NULL;
54         }
55     }
56 
57     if (!has_exec_key) {
58         error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
59         return NULL;
60     }
61 
62     return dict;
63 }
64 
65 static QObject *do_qmp_dispatch(QObject *request, Error **errp)
66 {
67     Error *local_err = NULL;
68     const char *command;
69     QDict *args, *dict;
70     QmpCommand *cmd;
71     QObject *ret = NULL;
72 
73     dict = qmp_dispatch_check_obj(request, errp);
74     if (!dict) {
75         return NULL;
76     }
77 
78     command = qdict_get_str(dict, "execute");
79     cmd = qmp_find_command(command);
80     if (cmd == NULL) {
81         error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
82                   "The command %s has not been found", command);
83         return NULL;
84     }
85     if (!cmd->enabled) {
86         error_setg(errp, "The command %s has been disabled for this instance",
87                    command);
88         return NULL;
89     }
90 
91     if (!qdict_haskey(dict, "arguments")) {
92         args = qdict_new();
93     } else {
94         args = qdict_get_qdict(dict, "arguments");
95         QINCREF(args);
96     }
97 
98     cmd->fn(args, &ret, &local_err);
99     if (local_err) {
100         error_propagate(errp, local_err);
101     } else if (cmd->options & QCO_NO_SUCCESS_RESP) {
102         g_assert(!ret);
103     } else if (!ret) {
104         ret = QOBJECT(qdict_new());
105     }
106 
107     QDECREF(args);
108 
109     return ret;
110 }
111 
112 QObject *qmp_build_error_object(Error *err)
113 {
114     return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
115                               QapiErrorClass_lookup[error_get_class(err)],
116                               error_get_pretty(err));
117 }
118 
119 QObject *qmp_dispatch(QObject *request)
120 {
121     Error *err = NULL;
122     QObject *ret;
123     QDict *rsp;
124 
125     ret = do_qmp_dispatch(request, &err);
126 
127     rsp = qdict_new();
128     if (err) {
129         qdict_put_obj(rsp, "error", qmp_build_error_object(err));
130         error_free(err);
131     } else if (ret) {
132         qdict_put_obj(rsp, "return", ret);
133     } else {
134         QDECREF(rsp);
135         return NULL;
136     }
137 
138     return QOBJECT(rsp);
139 }
140