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"
25 #include "qemu/qom-qobject.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
),
139 bdrv_get_encrypted_filename(bs
));
143 void qmp_cont(Error
**errp
)
145 Error
*local_err
= NULL
;
147 if (runstate_check(RUN_STATE_INMIGRATE
)) {
148 error_set(errp
, QERR_MIGRATION_EXPECTED
);
150 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
151 runstate_check(RUN_STATE_SHUTDOWN
)) {
152 error_set(errp
, QERR_RESET_REQUIRED
);
154 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
158 bdrv_iterate(iostatus_bdrv_it
, NULL
);
159 bdrv_iterate(encrypted_bdrv_it
, &local_err
);
161 error_propagate(errp
, local_err
);
168 void qmp_system_wakeup(Error
**errp
)
170 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
173 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
176 bool ambiguous
= false;
177 ObjectPropertyInfoList
*props
= NULL
;
178 ObjectProperty
*prop
;
180 obj
= object_resolve_path(path
, &ambiguous
);
182 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
186 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
187 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
189 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
193 entry
->value
->name
= g_strdup(prop
->name
);
194 entry
->value
->type
= g_strdup(prop
->type
);
200 /* FIXME: teach qapi about how to pass through Visitors */
201 int qmp_qom_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
203 const char *path
= qdict_get_str(qdict
, "path");
204 const char *property
= qdict_get_str(qdict
, "property");
205 QObject
*value
= qdict_get(qdict
, "value");
206 Error
*local_err
= NULL
;
209 obj
= object_resolve_path(path
, NULL
);
211 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
215 object_property_set_qobject(obj
, value
, property
, &local_err
);
219 qerror_report_err(local_err
);
220 error_free(local_err
);
227 int qmp_qom_get(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
229 const char *path
= qdict_get_str(qdict
, "path");
230 const char *property
= qdict_get_str(qdict
, "property");
231 Error
*local_err
= NULL
;
234 obj
= object_resolve_path(path
, NULL
);
236 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
240 *ret
= object_property_get_qobject(obj
, property
, &local_err
);
244 qerror_report_err(local_err
);
245 error_free(local_err
);
252 void qmp_set_password(const char *protocol
, const char *password
,
253 bool has_connected
, const char *connected
, Error
**errp
)
255 int disconnect_if_connected
= 0;
256 int fail_if_connected
= 0;
260 if (strcmp(connected
, "fail") == 0) {
261 fail_if_connected
= 1;
262 } else if (strcmp(connected
, "disconnect") == 0) {
263 disconnect_if_connected
= 1;
264 } else if (strcmp(connected
, "keep") == 0) {
267 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
272 if (strcmp(protocol
, "spice") == 0) {
274 /* correct one? spice isn't a device ,,, */
275 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
278 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
279 disconnect_if_connected
);
281 error_set(errp
, QERR_SET_PASSWD_FAILED
);
286 if (strcmp(protocol
, "vnc") == 0) {
287 if (fail_if_connected
|| disconnect_if_connected
) {
288 /* vnc supports "connected=keep" only */
289 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
292 /* Note that setting an empty password will not disable login through
294 rc
= vnc_display_password(NULL
, password
);
296 error_set(errp
, QERR_SET_PASSWD_FAILED
);
301 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
304 void qmp_expire_password(const char *protocol
, const char *whenstr
,
310 if (strcmp(whenstr
, "now") == 0) {
312 } else if (strcmp(whenstr
, "never") == 0) {
314 } else if (whenstr
[0] == '+') {
315 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
317 when
= strtoull(whenstr
, NULL
, 10);
320 if (strcmp(protocol
, "spice") == 0) {
322 /* correct one? spice isn't a device ,,, */
323 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
326 rc
= qemu_spice_set_pw_expire(when
);
328 error_set(errp
, QERR_SET_PASSWD_FAILED
);
333 if (strcmp(protocol
, "vnc") == 0) {
334 rc
= vnc_display_pw_expire(NULL
, when
);
336 error_set(errp
, QERR_SET_PASSWD_FAILED
);
341 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
345 void qmp_change_vnc_password(const char *password
, Error
**errp
)
347 if (vnc_display_password(NULL
, password
) < 0) {
348 error_set(errp
, QERR_SET_PASSWD_FAILED
);
352 static void qmp_change_vnc_listen(const char *target
, Error
**err
)
354 if (vnc_display_open(NULL
, target
) < 0) {
355 error_set(err
, QERR_VNC_SERVER_FAILED
, target
);
359 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
362 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
364 error_set(errp
, QERR_MISSING_PARAMETER
, "password");
366 qmp_change_vnc_password(arg
, errp
);
369 qmp_change_vnc_listen(target
, errp
);
373 void qmp_change_vnc_password(const char *password
, Error
**errp
)
375 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
377 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
380 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
382 #endif /* !CONFIG_VNC */
384 void qmp_change(const char *device
, const char *target
,
385 bool has_arg
, const char *arg
, Error
**err
)
387 if (strcmp(device
, "vnc") == 0) {
388 qmp_change_vnc(target
, has_arg
, arg
, err
);
390 qmp_change_blockdev(device
, target
, has_arg
, arg
, err
);
394 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
396 ObjectTypeInfoList
*e
, **pret
= data
;
397 ObjectTypeInfo
*info
;
399 info
= g_malloc0(sizeof(*info
));
400 info
->name
= g_strdup(object_class_get_name(klass
));
402 e
= g_malloc0(sizeof(*e
));
408 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
409 const char *implements
,
414 ObjectTypeInfoList
*ret
= NULL
;
416 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
421 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
426 DevicePropertyInfoList
*prop_list
= NULL
;
428 klass
= object_class_by_name(typename
);
430 error_set(errp
, QERR_DEVICE_NOT_FOUND
, typename
);
434 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
436 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
437 "name", TYPE_DEVICE
);
442 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
443 DevicePropertyInfoList
*entry
;
444 DevicePropertyInfo
*info
;
447 * TODO Properties without a parser are just for dirty hacks.
448 * qdev_prop_ptr is the only such PropertyInfo. It's marked
449 * for removal. This conditional should be removed along with
452 if (!prop
->info
->set
) {
453 continue; /* no way to set it, don't show */
456 info
= g_malloc0(sizeof(*info
));
457 info
->name
= g_strdup(prop
->name
);
458 info
->type
= g_strdup(prop
->info
->legacy_name
?: prop
->info
->name
);
460 entry
= g_malloc0(sizeof(*entry
));
462 entry
->next
= prop_list
;
465 klass
= object_class_get_parent(klass
);
466 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
471 CpuDefinitionInfoList GCC_WEAK
*arch_query_cpu_definitions(Error
**errp
)
473 error_set(errp
, QERR_NOT_SUPPORTED
);
477 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
479 return arch_query_cpu_definitions(errp
);
482 void qmp_add_client(const char *protocol
, const char *fdname
,
483 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
489 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
494 if (strcmp(protocol
, "spice") == 0) {
496 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
500 skipauth
= has_skipauth
? skipauth
: false;
501 tls
= has_tls
? tls
: false;
502 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
503 error_setg(errp
, "spice failed to add client");
508 } else if (strcmp(protocol
, "vnc") == 0) {
509 skipauth
= has_skipauth
? skipauth
: false;
510 vnc_display_add_client(NULL
, fd
, skipauth
);
513 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
514 if (qemu_chr_add_client(s
, fd
) < 0) {
515 error_setg(errp
, "failed to add client");
522 error_setg(errp
, "protocol '%s' is invalid", protocol
);