2 * QEMU Management Protocol
4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
18 #include "qmp-commands.h"
19 #include "ui/qemu-spice.h"
22 #include "arch_init.h"
24 #include "qapi/qmp-input-visitor.h"
25 #include "qapi/qmp-output-visitor.h"
27 NameInfo
*qmp_query_name(Error
**errp
)
29 NameInfo
*info
= g_malloc0(sizeof(*info
));
32 info
->has_name
= true;
33 info
->name
= g_strdup(qemu_name
);
39 VersionInfo
*qmp_query_version(Error
**err
)
41 VersionInfo
*info
= g_malloc0(sizeof(*info
));
42 const char *version
= QEMU_VERSION
;
45 info
->qemu
.major
= strtol(version
, &tmp
, 10);
47 info
->qemu
.minor
= strtol(tmp
, &tmp
, 10);
49 info
->qemu
.micro
= strtol(tmp
, &tmp
, 10);
50 info
->package
= g_strdup(QEMU_PKGVERSION
);
55 KvmInfo
*qmp_query_kvm(Error
**errp
)
57 KvmInfo
*info
= g_malloc0(sizeof(*info
));
59 info
->enabled
= kvm_enabled();
60 info
->present
= kvm_available();
65 UuidInfo
*qmp_query_uuid(Error
**errp
)
67 UuidInfo
*info
= g_malloc0(sizeof(*info
));
70 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
71 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
72 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
73 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
74 qemu_uuid
[14], qemu_uuid
[15]);
76 info
->UUID
= g_strdup(uuid
);
80 void qmp_quit(Error
**err
)
83 qemu_system_shutdown_request();
86 void qmp_stop(Error
**errp
)
88 vm_stop(RUN_STATE_PAUSED
);
91 void qmp_system_reset(Error
**errp
)
93 qemu_system_reset_request();
96 void qmp_system_powerdown(Error
**erp
)
98 qemu_system_powerdown_request();
101 void qmp_cpu(int64_t index
, Error
**errp
)
103 /* Just do nothing */
107 /* If VNC support is enabled, the "true" query-vnc command is
108 defined in the VNC subsystem */
109 VncInfo
*qmp_query_vnc(Error
**errp
)
111 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
117 /* If SPICE support is enabled, the "true" query-spice command is
118 defined in the SPICE subsystem. Also note that we use a small
119 trick to maintain query-spice's original behavior, which is not
120 to be available in the namespace if SPICE is not compiled in */
121 SpiceInfo
*qmp_query_spice(Error
**errp
)
123 error_set(errp
, QERR_COMMAND_NOT_FOUND
, "query-spice");
128 static void iostatus_bdrv_it(void *opaque
, BlockDriverState
*bs
)
130 bdrv_iostatus_reset(bs
);
133 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
135 Error
**err
= opaque
;
137 if (!error_is_set(err
) && bdrv_key_required(bs
)) {
138 error_set(err
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
142 void qmp_cont(Error
**errp
)
144 Error
*local_err
= NULL
;
146 if (runstate_check(RUN_STATE_INMIGRATE
)) {
147 error_set(errp
, QERR_MIGRATION_EXPECTED
);
149 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
150 runstate_check(RUN_STATE_SHUTDOWN
)) {
151 error_set(errp
, QERR_RESET_REQUIRED
);
155 bdrv_iterate(iostatus_bdrv_it
, NULL
);
156 bdrv_iterate(encrypted_bdrv_it
, &local_err
);
158 error_propagate(errp
, local_err
);
165 DevicePropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
168 bool ambiguous
= false;
169 DevicePropertyInfoList
*props
= NULL
;
170 DeviceProperty
*prop
;
172 dev
= qdev_resolve_path(path
, &ambiguous
);
174 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
178 QTAILQ_FOREACH(prop
, &dev
->properties
, node
) {
179 DevicePropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
181 entry
->value
= g_malloc0(sizeof(DevicePropertyInfo
));
185 entry
->value
->name
= g_strdup(prop
->name
);
186 entry
->value
->type
= g_strdup(prop
->type
);
192 /* FIXME: teach qapi about how to pass through Visitors */
193 int qmp_qom_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
195 const char *path
= qdict_get_str(qdict
, "path");
196 const char *property
= qdict_get_str(qdict
, "property");
197 QObject
*value
= qdict_get(qdict
, "value");
198 Error
*local_err
= NULL
;
202 dev
= qdev_resolve_path(path
, NULL
);
204 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
208 mi
= qmp_input_visitor_new(value
);
209 qdev_property_set(dev
, qmp_input_get_visitor(mi
), property
, &local_err
);
211 qmp_input_visitor_cleanup(mi
);
215 qerror_report_err(local_err
);
216 error_free(local_err
);
223 int qmp_qom_get(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
225 const char *path
= qdict_get_str(qdict
, "path");
226 const char *property
= qdict_get_str(qdict
, "property");
227 Error
*local_err
= NULL
;
228 QmpOutputVisitor
*mo
;
231 dev
= qdev_resolve_path(path
, NULL
);
233 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
237 mo
= qmp_output_visitor_new();
238 qdev_property_get(dev
, qmp_output_get_visitor(mo
), property
, &local_err
);
240 *ret
= qmp_output_get_qobject(mo
);
243 qmp_output_visitor_cleanup(mo
);
247 qerror_report_err(local_err
);
248 error_free(local_err
);
255 void qmp_set_password(const char *protocol
, const char *password
,
256 bool has_connected
, const char *connected
, Error
**errp
)
258 int disconnect_if_connected
= 0;
259 int fail_if_connected
= 0;
263 if (strcmp(connected
, "fail") == 0) {
264 fail_if_connected
= 1;
265 } else if (strcmp(connected
, "disconnect") == 0) {
266 disconnect_if_connected
= 1;
267 } else if (strcmp(connected
, "keep") == 0) {
270 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
275 if (strcmp(protocol
, "spice") == 0) {
277 /* correct one? spice isn't a device ,,, */
278 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
281 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
282 disconnect_if_connected
);
284 error_set(errp
, QERR_SET_PASSWD_FAILED
);
289 if (strcmp(protocol
, "vnc") == 0) {
290 if (fail_if_connected
|| disconnect_if_connected
) {
291 /* vnc supports "connected=keep" only */
292 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
295 /* Note that setting an empty password will not disable login through
297 rc
= vnc_display_password(NULL
, password
);
299 error_set(errp
, QERR_SET_PASSWD_FAILED
);
304 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
307 void qmp_expire_password(const char *protocol
, const char *whenstr
,
313 if (strcmp(whenstr
, "now") == 0) {
315 } else if (strcmp(whenstr
, "never") == 0) {
317 } else if (whenstr
[0] == '+') {
318 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
320 when
= strtoull(whenstr
, NULL
, 10);
323 if (strcmp(protocol
, "spice") == 0) {
325 /* correct one? spice isn't a device ,,, */
326 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
329 rc
= qemu_spice_set_pw_expire(when
);
331 error_set(errp
, QERR_SET_PASSWD_FAILED
);
336 if (strcmp(protocol
, "vnc") == 0) {
337 rc
= vnc_display_pw_expire(NULL
, when
);
339 error_set(errp
, QERR_SET_PASSWD_FAILED
);
344 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
347 void qmp_change_vnc_password(const char *password
, Error
**errp
)
349 if (vnc_display_password(NULL
, password
) < 0) {
350 error_set(errp
, QERR_SET_PASSWD_FAILED
);