xref: /openbmc/qemu/qapi/qmp-dispatch.c (revision f1f7e4bf)
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 "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"
20 
21 static QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp)
22 {
23     const QDictEntry *ent;
24     const char *arg_name;
25     const QObject *arg_obj;
26     bool has_exec_key = false;
27     QDict *dict = NULL;
28 
29     if (qobject_type(request) != QTYPE_QDICT) {
30         error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT,
31                    "request is not a dictionary");
32         return NULL;
33     }
34 
35     dict = qobject_to_qdict(request);
36 
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);
41 
42         if (!strcmp(arg_name, "execute")) {
43             if (qobject_type(arg_obj) != QTYPE_QSTRING) {
44                 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
45                            "string");
46                 return NULL;
47             }
48             has_exec_key = true;
49         } else if (strcmp(arg_name, "arguments")) {
50             error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
51             return NULL;
52         }
53     }
54 
55     if (!has_exec_key) {
56         error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
57         return NULL;
58     }
59 
60     return dict;
61 }
62 
63 static QObject *do_qmp_dispatch(QObject *request, Error **errp)
64 {
65     Error *local_err = NULL;
66     const char *command;
67     QDict *args, *dict;
68     QmpCommand *cmd;
69     QObject *ret = NULL;
70 
71     dict = qmp_dispatch_check_obj(request, errp);
72     if (!dict) {
73         return NULL;
74     }
75 
76     command = qdict_get_str(dict, "execute");
77     cmd = qmp_find_command(command);
78     if (cmd == NULL) {
79         error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
80                   "The command %s has not been found", command);
81         return NULL;
82     }
83     if (!cmd->enabled) {
84         error_setg(errp, "The command %s has been disabled for this instance",
85                    command);
86         return NULL;
87     }
88 
89     if (!qdict_haskey(dict, "arguments")) {
90         args = qdict_new();
91     } else {
92         args = qdict_get_qdict(dict, "arguments");
93         QINCREF(args);
94     }
95 
96     switch (cmd->type) {
97     case QCT_NORMAL:
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         break;
107     }
108 
109     QDECREF(args);
110 
111     return ret;
112 }
113 
114 QObject *qmp_build_error_object(Error *err)
115 {
116     return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
117                               QapiErrorClass_lookup[error_get_class(err)],
118                               error_get_pretty(err));
119 }
120 
121 QObject *qmp_dispatch(QObject *request)
122 {
123     Error *err = NULL;
124     QObject *ret;
125     QDict *rsp;
126 
127     ret = do_qmp_dispatch(request, &err);
128 
129     rsp = qdict_new();
130     if (err) {
131         qdict_put_obj(rsp, "error", qmp_build_error_object(err));
132         error_free(err);
133     } else if (ret) {
134         qdict_put_obj(rsp, "return", ret);
135     } else {
136         QDECREF(rsp);
137         return NULL;
138     }
139 
140     return QOBJECT(rsp);
141 }
142