xref: /openbmc/qemu/monitor/qmp-cmds-control.c (revision 729cc683)
1 /*
2  * QMP commands related to the monitor (common to sysemu and tools)
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 
27 #include "monitor-internal.h"
28 #include "qemu-version.h"
29 #include "qapi/error.h"
30 #include "qapi/qapi-commands-control.h"
31 #include "qapi/qapi-emit-events.h"
32 #include "qapi/qapi-introspect.h"
33 
34 /*
35  * Accept QMP capabilities in @list for @mon.
36  * On success, set mon->qmp.capab[], and return true.
37  * On error, set @errp, and return false.
38  */
39 static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
40                             Error **errp)
41 {
42     GString *unavailable = NULL;
43     bool capab[QMP_CAPABILITY__MAX];
44 
45     memset(capab, 0, sizeof(capab));
46 
47     for (; list; list = list->next) {
48         if (!mon->capab_offered[list->value]) {
49             if (!unavailable) {
50                 unavailable = g_string_new(QMPCapability_str(list->value));
51             } else {
52                 g_string_append_printf(unavailable, ", %s",
53                                       QMPCapability_str(list->value));
54             }
55         }
56         capab[list->value] = true;
57     }
58 
59     if (unavailable) {
60         error_setg(errp, "Capability %s not available", unavailable->str);
61         g_string_free(unavailable, true);
62         return false;
63     }
64 
65     memcpy(mon->capab, capab, sizeof(capab));
66     return true;
67 }
68 
69 void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
70                           Error **errp)
71 {
72     Monitor *cur_mon = monitor_cur();
73     MonitorQMP *mon;
74 
75     assert(monitor_is_qmp(cur_mon));
76     mon = container_of(cur_mon, MonitorQMP, common);
77 
78     if (mon->commands == &qmp_commands) {
79         error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
80                   "Capabilities negotiation is already complete, command "
81                   "ignored");
82         return;
83     }
84 
85     if (!qmp_caps_accept(mon, enable, errp)) {
86         return;
87     }
88 
89     mon->commands = &qmp_commands;
90 }
91 
92 VersionInfo *qmp_query_version(Error **errp)
93 {
94     VersionInfo *info = g_new0(VersionInfo, 1);
95 
96     info->qemu = g_new0(VersionTriple, 1);
97     info->qemu->major = QEMU_VERSION_MAJOR;
98     info->qemu->minor = QEMU_VERSION_MINOR;
99     info->qemu->micro = QEMU_VERSION_MICRO;
100     info->package = g_strdup(QEMU_PKGVERSION);
101 
102     return info;
103 }
104 
105 static void query_commands_cb(const QmpCommand *cmd, void *opaque)
106 {
107     CommandInfoList *info, **list = opaque;
108 
109     if (!cmd->enabled) {
110         return;
111     }
112 
113     info = g_malloc0(sizeof(*info));
114     info->value = g_malloc0(sizeof(*info->value));
115     info->value->name = g_strdup(cmd->name);
116     info->next = *list;
117     *list = info;
118 }
119 
120 CommandInfoList *qmp_query_commands(Error **errp)
121 {
122     CommandInfoList *list = NULL;
123     Monitor *cur_mon = monitor_cur();
124     MonitorQMP *mon;
125 
126     assert(monitor_is_qmp(cur_mon));
127     mon = container_of(cur_mon, MonitorQMP, common);
128 
129     qmp_for_each_command(mon->commands, query_commands_cb, &list);
130 
131     return list;
132 }
133 
134 EventInfoList *qmp_query_events(Error **errp)
135 {
136     /*
137      * TODO This deprecated command is the only user of
138      * QAPIEvent_str() and QAPIEvent_lookup[].  When the command goes,
139      * they should go, too.
140      */
141     EventInfoList *ev_list = NULL;
142     QAPIEvent e;
143 
144     for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
145         const char *event_name = QAPIEvent_str(e);
146         EventInfo *info;
147 
148         assert(event_name != NULL);
149         info = g_malloc0(sizeof(*info));
150         info->name = g_strdup(event_name);
151 
152         QAPI_LIST_PREPEND(ev_list, info);
153     }
154 
155     return ev_list;
156 }
157 
158 /*
159  * Minor hack: generated marshalling suppressed for this command
160  * ('gen': false in the schema) so we can parse the JSON string
161  * directly into QObject instead of first parsing it with
162  * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
163  * to QObject with generated output marshallers, every time.  Instead,
164  * we do it in test-qobject-input-visitor.c, just to make sure
165  * qapi-gen.py's output actually conforms to the schema.
166  */
167 void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
168                                  Error **errp)
169 {
170     *ret_data = qobject_from_qlit(&qmp_schema_qlit);
171 }
172