xref: /openbmc/qemu/qapi/qmp-dispatch.c (revision 64547a3b)
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/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"
21 
22 static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob,
23                                      Error **errp)
24 {
25     const char *exec_key = NULL;
26     const QDictEntry *ent;
27     const char *arg_name;
28     const QObject *arg_obj;
29 
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);
34 
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;
41             }
42             if (exec_key) {
43                 error_setg(errp, "QMP input member '%s' clashes with '%s'",
44                            arg_name, exec_key);
45                 return NULL;
46             }
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;
53             }
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;
60         }
61     }
62 
63     if (!exec_key) {
64         error_setg(errp, "QMP input lacks member 'execute'");
65         return NULL;
66     }
67 
68     return dict;
69 }
70 
71 QDict *qmp_error_response(Error *err)
72 {
73     QDict *rsp;
74 
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;
80 }
81 
82 /*
83  * Does @qdict look like a command to be run out-of-band?
84  */
85 bool qmp_is_oob(const QDict *dict)
86 {
87     return qdict_haskey(dict, "exec-oob")
88         && !qdict_haskey(dict, "execute");
89 }
90 
91 QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request,
92                     bool allow_oob)
93 {
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;
103 
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;
109     }
110 
111     id = qdict_get(dict, "id");
112 
113     if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) {
114         goto out;
115     }
116 
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;
123     }
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;
129     }
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;
135     }
136     if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
137         error_setg(&err, "The command %s does not support OOB",
138                    command);
139         goto out;
140     }
141 
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;
147     }
148 
149     if (!qdict_haskey(dict, "arguments")) {
150         args = qdict_new();
151     } else {
152         args = qdict_get_qdict(dict, "arguments");
153         qobject_ref(args);
154     }
155     cmd->fn(args, &ret, &err);
156     qobject_unref(args);
157     if (err) {
158         goto out;
159     }
160 
161     if (cmd->options & QCO_NO_SUCCESS_RESP) {
162         g_assert(!ret);
163         return NULL;
164     } else if (!ret) {
165         /*
166          * When the command's schema has no 'returns', cmd->fn()
167          * leaves @ret null.  The QMP spec calls for an empty object
168          * then; supply it.
169          */
170         ret = QOBJECT(qdict_new());
171     }
172 
173     rsp = qdict_new();
174     qdict_put_obj(rsp, "return", ret);
175 
176 out:
177     if (err) {
178         assert(!rsp);
179         rsp = qmp_error_response(err);
180     }
181 
182     assert(rsp);
183 
184     if (id) {
185         qdict_put_obj(rsp, "id", qobject_ref(id));
186     }
187 
188     return rsp;
189 }
190