1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
36
37
38
39static 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
69void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
70 Error **errp)
71{
72 MonitorQMP *mon;
73
74 assert(monitor_is_qmp(cur_mon));
75 mon = container_of(cur_mon, MonitorQMP, common);
76
77 if (mon->commands == &qmp_commands) {
78 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
79 "Capabilities negotiation is already complete, command "
80 "ignored");
81 return;
82 }
83
84 if (!qmp_caps_accept(mon, enable, errp)) {
85 return;
86 }
87
88 mon->commands = &qmp_commands;
89}
90
91VersionInfo *qmp_query_version(Error **errp)
92{
93 VersionInfo *info = g_new0(VersionInfo, 1);
94
95 info->qemu = g_new0(VersionTriple, 1);
96 info->qemu->major = QEMU_VERSION_MAJOR;
97 info->qemu->minor = QEMU_VERSION_MINOR;
98 info->qemu->micro = QEMU_VERSION_MICRO;
99 info->package = g_strdup(QEMU_PKGVERSION);
100
101 return info;
102}
103
104static void query_commands_cb(const QmpCommand *cmd, void *opaque)
105{
106 CommandInfoList *info, **list = opaque;
107
108 if (!cmd->enabled) {
109 return;
110 }
111
112 info = g_malloc0(sizeof(*info));
113 info->value = g_malloc0(sizeof(*info->value));
114 info->value->name = g_strdup(cmd->name);
115 info->next = *list;
116 *list = info;
117}
118
119CommandInfoList *qmp_query_commands(Error **errp)
120{
121 CommandInfoList *list = NULL;
122 MonitorQMP *mon;
123
124 assert(monitor_is_qmp(cur_mon));
125 mon = container_of(cur_mon, MonitorQMP, common);
126
127 qmp_for_each_command(mon->commands, query_commands_cb, &list);
128
129 return list;
130}
131
132EventInfoList *qmp_query_events(Error **errp)
133{
134
135
136
137
138
139 EventInfoList *info, *ev_list = NULL;
140 QAPIEvent e;
141
142 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
143 const char *event_name = QAPIEvent_str(e);
144 assert(event_name != NULL);
145 info = g_malloc0(sizeof(*info));
146 info->value = g_malloc0(sizeof(*info->value));
147 info->value->name = g_strdup(event_name);
148
149 info->next = ev_list;
150 ev_list = info;
151 }
152
153 return ev_list;
154}
155
156
157
158
159
160
161
162
163
164
165void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
166 Error **errp)
167{
168 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
169}
170