softmmu/qdev-monitor: fix use-after-free in qdev_set_id()
[qemu/kevin.git] / monitor / qmp-cmds.c
blob343353e27a7acaafe661f17a6002144d817a116e
1 /*
2 * QEMU Management Protocol commands
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-common.h"
18 #include "qemu/cutils.h"
19 #include "qemu/option.h"
20 #include "monitor/monitor.h"
21 #include "sysemu/sysemu.h"
22 #include "qemu/config-file.h"
23 #include "qemu/uuid.h"
24 #include "chardev/char.h"
25 #include "ui/qemu-spice.h"
26 #include "ui/console.h"
27 #include "sysemu/kvm.h"
28 #include "sysemu/runstate.h"
29 #include "sysemu/runstate-action.h"
30 #include "sysemu/blockdev.h"
31 #include "sysemu/block-backend.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-commands-acpi.h"
34 #include "qapi/qapi-commands-block.h"
35 #include "qapi/qapi-commands-control.h"
36 #include "qapi/qapi-commands-machine.h"
37 #include "qapi/qapi-commands-misc.h"
38 #include "qapi/qapi-commands-ui.h"
39 #include "qapi/type-helpers.h"
40 #include "qapi/qmp/qerror.h"
41 #include "exec/ramlist.h"
42 #include "hw/mem/memory-device.h"
43 #include "hw/acpi/acpi_dev_interface.h"
44 #include "hw/intc/intc.h"
45 #include "hw/rdma/rdma.h"
47 NameInfo *qmp_query_name(Error **errp)
49 NameInfo *info = g_malloc0(sizeof(*info));
51 if (qemu_name) {
52 info->has_name = true;
53 info->name = g_strdup(qemu_name);
56 return info;
59 KvmInfo *qmp_query_kvm(Error **errp)
61 KvmInfo *info = g_malloc0(sizeof(*info));
63 info->enabled = kvm_enabled();
64 info->present = accel_find("kvm");
66 return info;
69 UuidInfo *qmp_query_uuid(Error **errp)
71 UuidInfo *info = g_malloc0(sizeof(*info));
73 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
74 return info;
77 void qmp_quit(Error **errp)
79 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
80 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
83 void qmp_stop(Error **errp)
85 /* if there is a dump in background, we should wait until the dump
86 * finished */
87 if (dump_in_progress()) {
88 error_setg(errp, "There is a dump in process, please wait.");
89 return;
92 if (runstate_check(RUN_STATE_INMIGRATE)) {
93 autostart = 0;
94 } else {
95 vm_stop(RUN_STATE_PAUSED);
99 void qmp_system_reset(Error **errp)
101 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
104 void qmp_system_powerdown(Error **errp)
106 qemu_system_powerdown_request();
109 void qmp_cont(Error **errp)
111 BlockBackend *blk;
112 BlockJob *job;
113 Error *local_err = NULL;
115 /* if there is a dump in background, we should wait until the dump
116 * finished */
117 if (dump_in_progress()) {
118 error_setg(errp, "There is a dump in process, please wait.");
119 return;
122 if (runstate_needs_reset()) {
123 error_setg(errp, "Resetting the Virtual Machine is required");
124 return;
125 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
126 return;
127 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
128 error_setg(errp, "Migration is not finalized yet");
129 return;
132 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
133 blk_iostatus_reset(blk);
136 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
137 block_job_iostatus_reset(job);
140 /* Continuing after completed migration. Images have been inactivated to
141 * allow the destination to take control. Need to get control back now.
143 * If there are no inactive block nodes (e.g. because the VM was just
144 * paused rather than completing a migration), bdrv_inactivate_all() simply
145 * doesn't do anything. */
146 bdrv_invalidate_cache_all(&local_err);
147 if (local_err) {
148 error_propagate(errp, local_err);
149 return;
152 if (runstate_check(RUN_STATE_INMIGRATE)) {
153 autostart = 1;
154 } else {
155 vm_start();
159 void qmp_system_wakeup(Error **errp)
161 if (!qemu_wakeup_suspend_enabled()) {
162 error_setg(errp,
163 "wake-up from suspend is not supported by this guest");
164 return;
167 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
170 void qmp_set_password(const char *protocol, const char *password,
171 bool has_connected, const char *connected, Error **errp)
173 int disconnect_if_connected = 0;
174 int fail_if_connected = 0;
175 int rc;
177 if (has_connected) {
178 if (strcmp(connected, "fail") == 0) {
179 fail_if_connected = 1;
180 } else if (strcmp(connected, "disconnect") == 0) {
181 disconnect_if_connected = 1;
182 } else if (strcmp(connected, "keep") == 0) {
183 /* nothing */
184 } else {
185 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
186 return;
190 if (strcmp(protocol, "spice") == 0) {
191 if (!qemu_using_spice(errp)) {
192 return;
194 rc = qemu_spice.set_passwd(password, fail_if_connected,
195 disconnect_if_connected);
196 } else if (strcmp(protocol, "vnc") == 0) {
197 if (fail_if_connected || disconnect_if_connected) {
198 /* vnc supports "connected=keep" only */
199 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
200 return;
202 /* Note that setting an empty password will not disable login through
203 * this interface. */
204 rc = vnc_display_password(NULL, password);
205 } else {
206 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol",
207 "'vnc' or 'spice'");
208 return;
211 if (rc != 0) {
212 error_setg(errp, "Could not set password");
216 void qmp_expire_password(const char *protocol, const char *whenstr,
217 Error **errp)
219 time_t when;
220 int rc;
222 if (strcmp(whenstr, "now") == 0) {
223 when = 0;
224 } else if (strcmp(whenstr, "never") == 0) {
225 when = TIME_MAX;
226 } else if (whenstr[0] == '+') {
227 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
228 } else {
229 when = strtoull(whenstr, NULL, 10);
232 if (strcmp(protocol, "spice") == 0) {
233 if (!qemu_using_spice(errp)) {
234 return;
236 rc = qemu_spice.set_pw_expire(when);
237 } else if (strcmp(protocol, "vnc") == 0) {
238 rc = vnc_display_pw_expire(NULL, when);
239 } else {
240 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol",
241 "'vnc' or 'spice'");
242 return;
245 if (rc != 0) {
246 error_setg(errp, "Could not set password expire time");
250 #ifdef CONFIG_VNC
251 void qmp_change_vnc_password(const char *password, Error **errp)
253 if (vnc_display_password(NULL, password) < 0) {
254 error_setg(errp, "Could not set password");
257 #endif
259 void qmp_add_client(const char *protocol, const char *fdname,
260 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
261 Error **errp)
263 Chardev *s;
264 int fd;
266 fd = monitor_get_fd(monitor_cur(), fdname, errp);
267 if (fd < 0) {
268 return;
271 if (strcmp(protocol, "spice") == 0) {
272 if (!qemu_using_spice(errp)) {
273 close(fd);
274 return;
276 skipauth = has_skipauth ? skipauth : false;
277 tls = has_tls ? tls : false;
278 if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
279 error_setg(errp, "spice failed to add client");
280 close(fd);
282 return;
283 #ifdef CONFIG_VNC
284 } else if (strcmp(protocol, "vnc") == 0) {
285 skipauth = has_skipauth ? skipauth : false;
286 vnc_display_add_client(NULL, fd, skipauth);
287 return;
288 #endif
289 } else if ((s = qemu_chr_find(protocol)) != NULL) {
290 if (qemu_chr_add_client(s, fd) < 0) {
291 error_setg(errp, "failed to add client");
292 close(fd);
293 return;
295 return;
298 error_setg(errp, "protocol '%s' is invalid", protocol);
299 close(fd);
303 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
305 return qmp_memory_device_list();
308 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
310 bool ambig;
311 ACPIOSTInfoList *head = NULL;
312 ACPIOSTInfoList **prev = &head;
313 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
315 if (obj) {
316 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
317 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
319 adevc->ospm_status(adev, &prev);
320 } else {
321 error_setg(errp, "command is not supported, missing ACPI device");
324 return head;
327 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
329 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
330 MachineState *ms = MACHINE(qdev_get_machine());
332 mem_info->base_memory = ms->ram_size;
334 mem_info->plugged_memory = get_plugged_memory_size();
335 mem_info->has_plugged_memory =
336 mem_info->plugged_memory != (uint64_t)-1;
338 return mem_info;
341 void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
343 switch (arg->type) {
344 case DISPLAY_RELOAD_TYPE_VNC:
345 #ifdef CONFIG_VNC
346 if (arg->u.vnc.has_tls_certs && arg->u.vnc.tls_certs) {
347 vnc_display_reload_certs(NULL, errp);
349 #else
350 error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
351 #endif
352 break;
353 default:
354 abort();
358 #ifdef CONFIG_PROFILER
360 int64_t dev_time;
362 HumanReadableText *qmp_x_query_profile(Error **errp)
364 g_autoptr(GString) buf = g_string_new("");
365 static int64_t last_cpu_exec_time;
366 int64_t cpu_exec_time;
367 int64_t delta;
369 cpu_exec_time = tcg_cpu_exec_time();
370 delta = cpu_exec_time - last_cpu_exec_time;
372 g_string_append_printf(buf, "async time %" PRId64 " (%0.3f)\n",
373 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
374 g_string_append_printf(buf, "qemu time %" PRId64 " (%0.3f)\n",
375 delta, delta / (double)NANOSECONDS_PER_SECOND);
376 last_cpu_exec_time = cpu_exec_time;
377 dev_time = 0;
379 return human_readable_text_from_str(buf);
381 #else
382 HumanReadableText *qmp_x_query_profile(Error **errp)
384 error_setg(errp, "Internal profiler not compiled");
385 return NULL;
387 #endif
389 static int qmp_x_query_rdma_foreach(Object *obj, void *opaque)
391 RdmaProvider *rdma;
392 RdmaProviderClass *k;
393 GString *buf = opaque;
395 if (object_dynamic_cast(obj, INTERFACE_RDMA_PROVIDER)) {
396 rdma = RDMA_PROVIDER(obj);
397 k = RDMA_PROVIDER_GET_CLASS(obj);
398 if (k->format_statistics) {
399 k->format_statistics(rdma, buf);
400 } else {
401 g_string_append_printf(buf,
402 "RDMA statistics not available for %s.\n",
403 object_get_typename(obj));
407 return 0;
410 HumanReadableText *qmp_x_query_rdma(Error **errp)
412 g_autoptr(GString) buf = g_string_new("");
414 object_child_foreach_recursive(object_get_root(),
415 qmp_x_query_rdma_foreach, buf);
417 return human_readable_text_from_str(buf);
420 HumanReadableText *qmp_x_query_ramblock(Error **errp)
422 g_autoptr(GString) buf = ram_block_format();
424 return human_readable_text_from_str(buf);
427 static int qmp_x_query_irq_foreach(Object *obj, void *opaque)
429 InterruptStatsProvider *intc;
430 InterruptStatsProviderClass *k;
431 GString *buf = opaque;
433 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
434 intc = INTERRUPT_STATS_PROVIDER(obj);
435 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
436 uint64_t *irq_counts;
437 unsigned int nb_irqs, i;
438 if (k->get_statistics &&
439 k->get_statistics(intc, &irq_counts, &nb_irqs)) {
440 if (nb_irqs > 0) {
441 g_string_append_printf(buf, "IRQ statistics for %s:\n",
442 object_get_typename(obj));
443 for (i = 0; i < nb_irqs; i++) {
444 if (irq_counts[i] > 0) {
445 g_string_append_printf(buf, "%2d: %" PRId64 "\n", i,
446 irq_counts[i]);
450 } else {
451 g_string_append_printf(buf,
452 "IRQ statistics not available for %s.\n",
453 object_get_typename(obj));
457 return 0;
460 HumanReadableText *qmp_x_query_irq(Error **errp)
462 g_autoptr(GString) buf = g_string_new("");
464 object_child_foreach_recursive(object_get_root(),
465 qmp_x_query_irq_foreach, buf);
467 return human_readable_text_from_str(buf);