block: honor BDRV_O_ALLOW_RDWR when clearing bs->read_only
[qemu/kevin.git] / qmp.c
blobab74cd729d345fc5c7f975482425f06525db9497
1 /*
2 * QEMU Management Protocol
4 * Copyright IBM, Corp. 2011
6 * Authors:
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/osdep.h"
17 #include "qemu-version.h"
18 #include "qemu/cutils.h"
19 #include "monitor/monitor.h"
20 #include "sysemu/sysemu.h"
21 #include "qemu/config-file.h"
22 #include "qemu/uuid.h"
23 #include "qmp-commands.h"
24 #include "sysemu/char.h"
25 #include "ui/qemu-spice.h"
26 #include "ui/vnc.h"
27 #include "sysemu/kvm.h"
28 #include "sysemu/arch_init.h"
29 #include "hw/qdev.h"
30 #include "sysemu/blockdev.h"
31 #include "sysemu/block-backend.h"
32 #include "qom/qom-qobject.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/qmp/qobject.h"
35 #include "qapi/qobject-input-visitor.h"
36 #include "hw/boards.h"
37 #include "qom/object_interfaces.h"
38 #include "hw/mem/pc-dimm.h"
39 #include "hw/acpi/acpi_dev_interface.h"
41 NameInfo *qmp_query_name(Error **errp)
43 NameInfo *info = g_malloc0(sizeof(*info));
45 if (qemu_name) {
46 info->has_name = true;
47 info->name = g_strdup(qemu_name);
50 return info;
53 VersionInfo *qmp_query_version(Error **errp)
55 VersionInfo *info = g_new0(VersionInfo, 1);
57 info->qemu = g_new0(VersionTriple, 1);
58 info->qemu->major = QEMU_VERSION_MAJOR;
59 info->qemu->minor = QEMU_VERSION_MINOR;
60 info->qemu->micro = QEMU_VERSION_MICRO;
61 info->package = g_strdup(QEMU_PKGVERSION);
63 return info;
66 KvmInfo *qmp_query_kvm(Error **errp)
68 KvmInfo *info = g_malloc0(sizeof(*info));
70 info->enabled = kvm_enabled();
71 info->present = kvm_available();
73 return info;
76 UuidInfo *qmp_query_uuid(Error **errp)
78 UuidInfo *info = g_malloc0(sizeof(*info));
80 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
81 return info;
84 void qmp_quit(Error **errp)
86 no_shutdown = 0;
87 qemu_system_shutdown_request();
90 void qmp_stop(Error **errp)
92 /* if there is a dump in background, we should wait until the dump
93 * finished */
94 if (dump_in_progress()) {
95 error_setg(errp, "There is a dump in process, please wait.");
96 return;
99 if (runstate_check(RUN_STATE_INMIGRATE)) {
100 autostart = 0;
101 } else {
102 vm_stop(RUN_STATE_PAUSED);
106 void qmp_system_reset(Error **errp)
108 qemu_system_reset_request();
111 void qmp_system_powerdown(Error **erp)
113 qemu_system_powerdown_request();
116 void qmp_cpu(int64_t index, Error **errp)
118 /* Just do nothing */
121 void qmp_cpu_add(int64_t id, Error **errp)
123 MachineClass *mc;
125 mc = MACHINE_GET_CLASS(current_machine);
126 if (mc->hot_add_cpu) {
127 mc->hot_add_cpu(id, errp);
128 } else {
129 error_setg(errp, "Not supported");
133 #ifndef CONFIG_VNC
134 /* If VNC support is enabled, the "true" query-vnc command is
135 defined in the VNC subsystem */
136 VncInfo *qmp_query_vnc(Error **errp)
138 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
139 return NULL;
142 VncInfo2List *qmp_query_vnc_servers(Error **errp)
144 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
145 return NULL;
147 #endif
149 #ifndef CONFIG_SPICE
151 * qmp-commands.hx ensures that QMP command query-spice exists only
152 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
153 * result. However, the QAPI schema is blissfully unaware of that,
154 * and the QAPI code generator happily generates a dead
155 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
156 * one, or else linking fails. FIXME Educate the QAPI schema on
157 * CONFIG_SPICE.
159 SpiceInfo *qmp_query_spice(Error **errp)
161 abort();
163 #endif
165 void qmp_cont(Error **errp)
167 Error *local_err = NULL;
168 BlockBackend *blk;
169 BlockDriverState *bs;
170 BdrvNextIterator it;
172 /* if there is a dump in background, we should wait until the dump
173 * finished */
174 if (dump_in_progress()) {
175 error_setg(errp, "There is a dump in process, please wait.");
176 return;
179 if (runstate_needs_reset()) {
180 error_setg(errp, "Resetting the Virtual Machine is required");
181 return;
182 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
183 return;
186 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
187 blk_iostatus_reset(blk);
190 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
191 bdrv_add_key(bs, NULL, &local_err);
192 if (local_err) {
193 error_propagate(errp, local_err);
194 return;
198 /* Continuing after completed migration. Images have been inactivated to
199 * allow the destination to take control. Need to get control back now. */
200 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
201 runstate_check(RUN_STATE_POSTMIGRATE))
203 bdrv_invalidate_cache_all(&local_err);
204 if (local_err) {
205 error_propagate(errp, local_err);
206 return;
210 blk_resume_after_migration(&local_err);
211 if (local_err) {
212 error_propagate(errp, local_err);
213 return;
216 if (runstate_check(RUN_STATE_INMIGRATE)) {
217 autostart = 1;
218 } else {
219 vm_start();
223 void qmp_system_wakeup(Error **errp)
225 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
228 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
230 Object *obj;
231 bool ambiguous = false;
232 ObjectPropertyInfoList *props = NULL;
233 ObjectProperty *prop;
234 ObjectPropertyIterator iter;
236 obj = object_resolve_path(path, &ambiguous);
237 if (obj == NULL) {
238 if (ambiguous) {
239 error_setg(errp, "Path '%s' is ambiguous", path);
240 } else {
241 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
242 "Device '%s' not found", path);
244 return NULL;
247 object_property_iter_init(&iter, obj);
248 while ((prop = object_property_iter_next(&iter))) {
249 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
251 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
252 entry->next = props;
253 props = entry;
255 entry->value->name = g_strdup(prop->name);
256 entry->value->type = g_strdup(prop->type);
259 return props;
262 void qmp_qom_set(const char *path, const char *property, QObject *value,
263 Error **errp)
265 Object *obj;
267 obj = object_resolve_path(path, NULL);
268 if (!obj) {
269 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
270 "Device '%s' not found", path);
271 return;
274 object_property_set_qobject(obj, value, property, errp);
277 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
279 Object *obj;
281 obj = object_resolve_path(path, NULL);
282 if (!obj) {
283 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
284 "Device '%s' not found", path);
285 return NULL;
288 return object_property_get_qobject(obj, property, errp);
291 void qmp_set_password(const char *protocol, const char *password,
292 bool has_connected, const char *connected, Error **errp)
294 int disconnect_if_connected = 0;
295 int fail_if_connected = 0;
296 int rc;
298 if (has_connected) {
299 if (strcmp(connected, "fail") == 0) {
300 fail_if_connected = 1;
301 } else if (strcmp(connected, "disconnect") == 0) {
302 disconnect_if_connected = 1;
303 } else if (strcmp(connected, "keep") == 0) {
304 /* nothing */
305 } else {
306 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
307 return;
311 if (strcmp(protocol, "spice") == 0) {
312 if (!qemu_using_spice(errp)) {
313 return;
315 rc = qemu_spice_set_passwd(password, fail_if_connected,
316 disconnect_if_connected);
317 if (rc != 0) {
318 error_setg(errp, QERR_SET_PASSWD_FAILED);
320 return;
323 if (strcmp(protocol, "vnc") == 0) {
324 if (fail_if_connected || disconnect_if_connected) {
325 /* vnc supports "connected=keep" only */
326 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
327 return;
329 /* Note that setting an empty password will not disable login through
330 * this interface. */
331 rc = vnc_display_password(NULL, password);
332 if (rc < 0) {
333 error_setg(errp, QERR_SET_PASSWD_FAILED);
335 return;
338 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
341 void qmp_expire_password(const char *protocol, const char *whenstr,
342 Error **errp)
344 time_t when;
345 int rc;
347 if (strcmp(whenstr, "now") == 0) {
348 when = 0;
349 } else if (strcmp(whenstr, "never") == 0) {
350 when = TIME_MAX;
351 } else if (whenstr[0] == '+') {
352 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
353 } else {
354 when = strtoull(whenstr, NULL, 10);
357 if (strcmp(protocol, "spice") == 0) {
358 if (!qemu_using_spice(errp)) {
359 return;
361 rc = qemu_spice_set_pw_expire(when);
362 if (rc != 0) {
363 error_setg(errp, QERR_SET_PASSWD_FAILED);
365 return;
368 if (strcmp(protocol, "vnc") == 0) {
369 rc = vnc_display_pw_expire(NULL, when);
370 if (rc != 0) {
371 error_setg(errp, QERR_SET_PASSWD_FAILED);
373 return;
376 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
379 #ifdef CONFIG_VNC
380 void qmp_change_vnc_password(const char *password, Error **errp)
382 if (vnc_display_password(NULL, password) < 0) {
383 error_setg(errp, QERR_SET_PASSWD_FAILED);
387 static void qmp_change_vnc_listen(const char *target, Error **errp)
389 QemuOptsList *olist = qemu_find_opts("vnc");
390 QemuOpts *opts;
392 if (strstr(target, "id=")) {
393 error_setg(errp, "id not supported");
394 return;
397 opts = qemu_opts_find(olist, "default");
398 if (opts) {
399 qemu_opts_del(opts);
401 opts = vnc_parse(target, errp);
402 if (!opts) {
403 return;
406 vnc_display_open("default", errp);
409 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
410 Error **errp)
412 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
413 if (!has_arg) {
414 error_setg(errp, QERR_MISSING_PARAMETER, "password");
415 } else {
416 qmp_change_vnc_password(arg, errp);
418 } else {
419 qmp_change_vnc_listen(target, errp);
422 #else
423 void qmp_change_vnc_password(const char *password, Error **errp)
425 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
427 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
428 Error **errp)
430 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
432 #endif /* !CONFIG_VNC */
434 void qmp_change(const char *device, const char *target,
435 bool has_arg, const char *arg, Error **errp)
437 if (strcmp(device, "vnc") == 0) {
438 qmp_change_vnc(target, has_arg, arg, errp);
439 } else {
440 qmp_blockdev_change_medium(true, device, false, NULL, target,
441 has_arg, arg, false, 0, errp);
445 static void qom_list_types_tramp(ObjectClass *klass, void *data)
447 ObjectTypeInfoList *e, **pret = data;
448 ObjectTypeInfo *info;
450 info = g_malloc0(sizeof(*info));
451 info->name = g_strdup(object_class_get_name(klass));
453 e = g_malloc0(sizeof(*e));
454 e->value = info;
455 e->next = *pret;
456 *pret = e;
459 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
460 const char *implements,
461 bool has_abstract,
462 bool abstract,
463 Error **errp)
465 ObjectTypeInfoList *ret = NULL;
467 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
469 return ret;
472 /* Return a DevicePropertyInfo for a qdev property.
474 * If a qdev property with the given name does not exist, use the given default
475 * type. If the qdev property info should not be shown, return NULL.
477 * The caller must free the return value.
479 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
480 const char *name,
481 const char *default_type,
482 const char *description)
484 DevicePropertyInfo *info;
485 Property *prop;
487 do {
488 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
489 if (strcmp(name, prop->name) != 0) {
490 continue;
494 * TODO Properties without a parser are just for dirty hacks.
495 * qdev_prop_ptr is the only such PropertyInfo. It's marked
496 * for removal. This conditional should be removed along with
497 * it.
499 if (!prop->info->set) {
500 return NULL; /* no way to set it, don't show */
503 info = g_malloc0(sizeof(*info));
504 info->name = g_strdup(prop->name);
505 info->type = g_strdup(prop->info->name);
506 info->has_description = !!prop->info->description;
507 info->description = g_strdup(prop->info->description);
508 return info;
510 klass = object_class_get_parent(klass);
511 } while (klass != object_class_by_name(TYPE_DEVICE));
513 /* Not a qdev property, use the default type */
514 info = g_malloc0(sizeof(*info));
515 info->name = g_strdup(name);
516 info->type = g_strdup(default_type);
517 info->has_description = !!description;
518 info->description = g_strdup(description);
520 return info;
523 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
524 Error **errp)
526 ObjectClass *klass;
527 Object *obj;
528 ObjectProperty *prop;
529 ObjectPropertyIterator iter;
530 DevicePropertyInfoList *prop_list = NULL;
532 klass = object_class_by_name(typename);
533 if (klass == NULL) {
534 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
535 "Device '%s' not found", typename);
536 return NULL;
539 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
540 if (klass == NULL) {
541 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
542 return NULL;
545 if (object_class_is_abstract(klass)) {
546 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
547 "non-abstract device type");
548 return NULL;
551 obj = object_new(typename);
553 object_property_iter_init(&iter, obj);
554 while ((prop = object_property_iter_next(&iter))) {
555 DevicePropertyInfo *info;
556 DevicePropertyInfoList *entry;
558 /* Skip Object and DeviceState properties */
559 if (strcmp(prop->name, "type") == 0 ||
560 strcmp(prop->name, "realized") == 0 ||
561 strcmp(prop->name, "hotpluggable") == 0 ||
562 strcmp(prop->name, "hotplugged") == 0 ||
563 strcmp(prop->name, "parent_bus") == 0) {
564 continue;
567 /* Skip legacy properties since they are just string versions of
568 * properties that we already list.
570 if (strstart(prop->name, "legacy-", NULL)) {
571 continue;
574 info = make_device_property_info(klass, prop->name, prop->type,
575 prop->description);
576 if (!info) {
577 continue;
580 entry = g_malloc0(sizeof(*entry));
581 entry->value = info;
582 entry->next = prop_list;
583 prop_list = entry;
586 object_unref(obj);
588 return prop_list;
591 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
593 return arch_query_cpu_definitions(errp);
596 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
597 CpuModelInfo *model,
598 Error **errp)
600 return arch_query_cpu_model_expansion(type, model, errp);
603 CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
604 CpuModelInfo *modelb,
605 Error **errp)
607 return arch_query_cpu_model_comparison(modela, modelb, errp);
610 CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
611 CpuModelInfo *modelb,
612 Error **errp)
614 return arch_query_cpu_model_baseline(modela, modelb, errp);
617 void qmp_add_client(const char *protocol, const char *fdname,
618 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
619 Error **errp)
621 Chardev *s;
622 int fd;
624 fd = monitor_get_fd(cur_mon, fdname, errp);
625 if (fd < 0) {
626 return;
629 if (strcmp(protocol, "spice") == 0) {
630 if (!qemu_using_spice(errp)) {
631 close(fd);
632 return;
634 skipauth = has_skipauth ? skipauth : false;
635 tls = has_tls ? tls : false;
636 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
637 error_setg(errp, "spice failed to add client");
638 close(fd);
640 return;
641 #ifdef CONFIG_VNC
642 } else if (strcmp(protocol, "vnc") == 0) {
643 skipauth = has_skipauth ? skipauth : false;
644 vnc_display_add_client(NULL, fd, skipauth);
645 return;
646 #endif
647 } else if ((s = qemu_chr_find(protocol)) != NULL) {
648 if (qemu_chr_add_client(s, fd) < 0) {
649 error_setg(errp, "failed to add client");
650 close(fd);
651 return;
653 return;
656 error_setg(errp, "protocol '%s' is invalid", protocol);
657 close(fd);
661 void qmp_object_add(const char *type, const char *id,
662 bool has_props, QObject *props, Error **errp)
664 QDict *pdict;
665 Visitor *v;
666 Object *obj;
668 if (props) {
669 pdict = qobject_to_qdict(props);
670 if (!pdict) {
671 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
672 return;
674 QINCREF(pdict);
675 } else {
676 pdict = qdict_new();
679 v = qobject_input_visitor_new(QOBJECT(pdict));
680 obj = user_creatable_add_type(type, id, pdict, v, errp);
681 visit_free(v);
682 if (obj) {
683 object_unref(obj);
685 QDECREF(pdict);
688 void qmp_object_del(const char *id, Error **errp)
690 user_creatable_del(id, errp);
693 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
695 MemoryDeviceInfoList *head = NULL;
696 MemoryDeviceInfoList **prev = &head;
698 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
700 return head;
703 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
705 bool ambig;
706 ACPIOSTInfoList *head = NULL;
707 ACPIOSTInfoList **prev = &head;
708 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
710 if (obj) {
711 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
712 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
714 adevc->ospm_status(adev, &prev);
715 } else {
716 error_setg(errp, "command is not supported, missing ACPI device");
719 return head;