]> git.proxmox.com Git - mirror_qemu.git/blame - qmp.c
qga: Consistently name Error ** objects errp, and not err
[mirror_qemu.git] / qmp.c
CommitLineData
48a32bed
AL
1/*
2 * QEMU Management Protocol
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
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.
48a32bed
AL
14 */
15
16#include "qemu-common.h"
9c17d615 17#include "sysemu/sysemu.h"
48a32bed 18#include "qmp-commands.h"
dccfcd0e 19#include "sysemu/char.h"
fbf796fd
LC
20#include "ui/qemu-spice.h"
21#include "ui/vnc.h"
9c17d615
PB
22#include "sysemu/kvm.h"
23#include "sysemu/arch_init.h"
b4b12c62 24#include "hw/qdev.h"
9c17d615 25#include "sysemu/blockdev.h"
14cccb61 26#include "qom/qom-qobject.h"
cff8b2c6
PB
27#include "qapi/qmp/qobject.h"
28#include "qapi/qmp-input-visitor.h"
69ca3ea5 29#include "hw/boards.h"
269e09f3 30#include "qom/object_interfaces.h"
48a32bed
AL
31
32NameInfo *qmp_query_name(Error **errp)
33{
34 NameInfo *info = g_malloc0(sizeof(*info));
35
36 if (qemu_name) {
37 info->has_name = true;
38 info->name = g_strdup(qemu_name);
39 }
40
41 return info;
42}
b9c15f16
LC
43
44VersionInfo *qmp_query_version(Error **err)
45{
46 VersionInfo *info = g_malloc0(sizeof(*info));
47 const char *version = QEMU_VERSION;
48 char *tmp;
49
50 info->qemu.major = strtol(version, &tmp, 10);
51 tmp++;
52 info->qemu.minor = strtol(tmp, &tmp, 10);
53 tmp++;
54 info->qemu.micro = strtol(tmp, &tmp, 10);
55 info->package = g_strdup(QEMU_PKGVERSION);
56
57 return info;
58}
292a2602
LC
59
60KvmInfo *qmp_query_kvm(Error **errp)
61{
62 KvmInfo *info = g_malloc0(sizeof(*info));
63
64 info->enabled = kvm_enabled();
65 info->present = kvm_available();
66
67 return info;
68}
69
efab767e
LC
70UuidInfo *qmp_query_uuid(Error **errp)
71{
72 UuidInfo *info = g_malloc0(sizeof(*info));
73 char uuid[64];
74
75 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
76 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
77 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
78 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
79 qemu_uuid[14], qemu_uuid[15]);
80
81 info->UUID = g_strdup(uuid);
82 return info;
83}
84
7a7f325e
LC
85void qmp_quit(Error **err)
86{
87 no_shutdown = 0;
88 qemu_system_shutdown_request();
89}
90
5f158f21
LC
91void qmp_stop(Error **errp)
92{
1e998146
PB
93 if (runstate_check(RUN_STATE_INMIGRATE)) {
94 autostart = 0;
95 } else {
96 vm_stop(RUN_STATE_PAUSED);
97 }
5f158f21
LC
98}
99
38d22653
LC
100void qmp_system_reset(Error **errp)
101{
102 qemu_system_reset_request();
103}
5bc465e4
LC
104
105void qmp_system_powerdown(Error **erp)
106{
107 qemu_system_powerdown_request();
108}
755f1968
LC
109
110void qmp_cpu(int64_t index, Error **errp)
111{
112 /* Just do nothing */
113}
2b54aa87 114
69ca3ea5
IM
115void qmp_cpu_add(int64_t id, Error **errp)
116{
0056ae24
MA
117 MachineClass *mc;
118
119 mc = MACHINE_GET_CLASS(current_machine);
958db90c
MA
120 if (mc->hot_add_cpu) {
121 mc->hot_add_cpu(id, errp);
69ca3ea5
IM
122 } else {
123 error_setg(errp, "Not supported");
124 }
125}
126
2b54aa87
LC
127#ifndef CONFIG_VNC
128/* If VNC support is enabled, the "true" query-vnc command is
129 defined in the VNC subsystem */
130VncInfo *qmp_query_vnc(Error **errp)
131{
132 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
133 return NULL;
134};
135#endif
d1f29646
LC
136
137#ifndef CONFIG_SPICE
138/* If SPICE support is enabled, the "true" query-spice command is
139 defined in the SPICE subsystem. Also note that we use a small
140 trick to maintain query-spice's original behavior, which is not
141 to be available in the namespace if SPICE is not compiled in */
142SpiceInfo *qmp_query_spice(Error **errp)
143{
144 error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
145 return NULL;
146};
147#endif
e42e818b
LC
148
149static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
150{
151 bdrv_iostatus_reset(bs);
152}
153
154static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
155{
156 Error **err = opaque;
157
158 if (!error_is_set(err) && bdrv_key_required(bs)) {
903a8814
LC
159 error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
160 bdrv_get_encrypted_filename(bs));
e42e818b
LC
161 }
162}
163
164void qmp_cont(Error **errp)
165{
166 Error *local_err = NULL;
167
ede085b3 168 if (runstate_needs_reset()) {
f231b88d 169 error_setg(errp, "Resetting the Virtual Machine is required");
e42e818b 170 return;
ad02b96a
LC
171 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
172 return;
e42e818b
LC
173 }
174
175 bdrv_iterate(iostatus_bdrv_it, NULL);
176 bdrv_iterate(encrypted_bdrv_it, &local_err);
177 if (local_err) {
178 error_propagate(errp, local_err);
179 return;
180 }
181
1e998146
PB
182 if (runstate_check(RUN_STATE_INMIGRATE)) {
183 autostart = 1;
184 } else {
185 vm_start();
186 }
e42e818b 187}
b4b12c62 188
9b9df25a
GH
189void qmp_system_wakeup(Error **errp)
190{
191 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
192}
193
57c9fafe 194ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
b4b12c62 195{
57c9fafe 196 Object *obj;
b4b12c62 197 bool ambiguous = false;
57c9fafe
AL
198 ObjectPropertyInfoList *props = NULL;
199 ObjectProperty *prop;
b4b12c62 200
57c9fafe
AL
201 obj = object_resolve_path(path, &ambiguous);
202 if (obj == NULL) {
79772087
MT
203 if (ambiguous) {
204 error_setg(errp, "Path '%s' is ambiguous", path);
205 } else {
206 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
207 }
b4b12c62
AL
208 return NULL;
209 }
210
57c9fafe
AL
211 QTAILQ_FOREACH(prop, &obj->properties, node) {
212 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
b4b12c62 213
57c9fafe 214 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
b4b12c62
AL
215 entry->next = props;
216 props = entry;
217
218 entry->value->name = g_strdup(prop->name);
219 entry->value->type = g_strdup(prop->type);
220 }
221
222 return props;
223}
eb6e8ea5
AL
224
225/* FIXME: teach qapi about how to pass through Visitors */
226int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
227{
228 const char *path = qdict_get_str(qdict, "path");
229 const char *property = qdict_get_str(qdict, "property");
230 QObject *value = qdict_get(qdict, "value");
231 Error *local_err = NULL;
57c9fafe 232 Object *obj;
eb6e8ea5 233
57c9fafe
AL
234 obj = object_resolve_path(path, NULL);
235 if (!obj) {
eb6e8ea5
AL
236 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
237 goto out;
238 }
239
9f5f1350 240 object_property_set_qobject(obj, value, property, &local_err);
eb6e8ea5
AL
241
242out:
243 if (local_err) {
244 qerror_report_err(local_err);
245 error_free(local_err);
246 return -1;
247 }
248
249 return 0;
250}
251
252int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
253{
254 const char *path = qdict_get_str(qdict, "path");
255 const char *property = qdict_get_str(qdict, "property");
256 Error *local_err = NULL;
57c9fafe 257 Object *obj;
eb6e8ea5 258
57c9fafe
AL
259 obj = object_resolve_path(path, NULL);
260 if (!obj) {
eb6e8ea5
AL
261 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
262 goto out;
263 }
264
9f5f1350 265 *ret = object_property_get_qobject(obj, property, &local_err);
eb6e8ea5
AL
266
267out:
268 if (local_err) {
269 qerror_report_err(local_err);
270 error_free(local_err);
271 return -1;
272 }
273
274 return 0;
275}
fbf796fd
LC
276
277void qmp_set_password(const char *protocol, const char *password,
278 bool has_connected, const char *connected, Error **errp)
279{
280 int disconnect_if_connected = 0;
281 int fail_if_connected = 0;
282 int rc;
283
284 if (has_connected) {
285 if (strcmp(connected, "fail") == 0) {
286 fail_if_connected = 1;
287 } else if (strcmp(connected, "disconnect") == 0) {
288 disconnect_if_connected = 1;
289 } else if (strcmp(connected, "keep") == 0) {
290 /* nothing */
291 } else {
292 error_set(errp, QERR_INVALID_PARAMETER, "connected");
293 return;
294 }
295 }
296
297 if (strcmp(protocol, "spice") == 0) {
298 if (!using_spice) {
299 /* correct one? spice isn't a device ,,, */
300 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
301 return;
302 }
303 rc = qemu_spice_set_passwd(password, fail_if_connected,
304 disconnect_if_connected);
305 if (rc != 0) {
306 error_set(errp, QERR_SET_PASSWD_FAILED);
307 }
308 return;
309 }
310
311 if (strcmp(protocol, "vnc") == 0) {
312 if (fail_if_connected || disconnect_if_connected) {
313 /* vnc supports "connected=keep" only */
314 error_set(errp, QERR_INVALID_PARAMETER, "connected");
315 return;
316 }
317 /* Note that setting an empty password will not disable login through
318 * this interface. */
319 rc = vnc_display_password(NULL, password);
320 if (rc < 0) {
321 error_set(errp, QERR_SET_PASSWD_FAILED);
322 }
323 return;
324 }
325
326 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
327}
9ad5372d
LC
328
329void qmp_expire_password(const char *protocol, const char *whenstr,
330 Error **errp)
331{
332 time_t when;
333 int rc;
334
335 if (strcmp(whenstr, "now") == 0) {
336 when = 0;
337 } else if (strcmp(whenstr, "never") == 0) {
338 when = TIME_MAX;
339 } else if (whenstr[0] == '+') {
340 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
341 } else {
342 when = strtoull(whenstr, NULL, 10);
343 }
344
345 if (strcmp(protocol, "spice") == 0) {
346 if (!using_spice) {
347 /* correct one? spice isn't a device ,,, */
348 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
349 return;
350 }
351 rc = qemu_spice_set_pw_expire(when);
352 if (rc != 0) {
353 error_set(errp, QERR_SET_PASSWD_FAILED);
354 }
355 return;
356 }
357
358 if (strcmp(protocol, "vnc") == 0) {
359 rc = vnc_display_pw_expire(NULL, when);
360 if (rc != 0) {
361 error_set(errp, QERR_SET_PASSWD_FAILED);
362 }
363 return;
364 }
365
366 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
367}
270b243f 368
333a96ec 369#ifdef CONFIG_VNC
270b243f
LC
370void qmp_change_vnc_password(const char *password, Error **errp)
371{
372 if (vnc_display_password(NULL, password) < 0) {
373 error_set(errp, QERR_SET_PASSWD_FAILED);
374 }
375}
333a96ec 376
007fcd3e 377static void qmp_change_vnc_listen(const char *target, Error **errp)
333a96ec 378{
007fcd3e 379 vnc_display_open(NULL, target, errp);
333a96ec
LC
380}
381
382static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
383 Error **errp)
384{
385 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
386 if (!has_arg) {
387 error_set(errp, QERR_MISSING_PARAMETER, "password");
388 } else {
389 qmp_change_vnc_password(arg, errp);
390 }
391 } else {
392 qmp_change_vnc_listen(target, errp);
393 }
394}
395#else
396void qmp_change_vnc_password(const char *password, Error **errp)
397{
398 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
399}
400static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
401 Error **errp)
402{
403 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
404}
405#endif /* !CONFIG_VNC */
406
407void qmp_change(const char *device, const char *target,
408 bool has_arg, const char *arg, Error **err)
409{
410 if (strcmp(device, "vnc") == 0) {
411 qmp_change_vnc(target, has_arg, arg, err);
412 } else {
314f7ea7 413 qmp_change_blockdev(device, target, arg, err);
333a96ec
LC
414 }
415}
5eeee3fa
AL
416
417static void qom_list_types_tramp(ObjectClass *klass, void *data)
418{
419 ObjectTypeInfoList *e, **pret = data;
420 ObjectTypeInfo *info;
421
422 info = g_malloc0(sizeof(*info));
423 info->name = g_strdup(object_class_get_name(klass));
424
425 e = g_malloc0(sizeof(*e));
426 e->value = info;
427 e->next = *pret;
428 *pret = e;
429}
430
431ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
432 const char *implements,
433 bool has_abstract,
434 bool abstract,
435 Error **errp)
436{
437 ObjectTypeInfoList *ret = NULL;
438
439 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
440
441 return ret;
442}
1daa31b9
AL
443
444DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
445 Error **errp)
446{
447 ObjectClass *klass;
448 Property *prop;
449 DevicePropertyInfoList *prop_list = NULL;
450
451 klass = object_class_by_name(typename);
452 if (klass == NULL) {
453 error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
454 return NULL;
455 }
456
457 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
458 if (klass == NULL) {
459 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
460 "name", TYPE_DEVICE);
461 return NULL;
462 }
463
464 do {
465 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
466 DevicePropertyInfoList *entry;
467 DevicePropertyInfo *info;
468
469 /*
470 * TODO Properties without a parser are just for dirty hacks.
471 * qdev_prop_ptr is the only such PropertyInfo. It's marked
472 * for removal. This conditional should be removed along with
473 * it.
474 */
475 if (!prop->info->set) {
476 continue; /* no way to set it, don't show */
477 }
478
479 info = g_malloc0(sizeof(*info));
480 info->name = g_strdup(prop->name);
481 info->type = g_strdup(prop->info->legacy_name ?: prop->info->name);
482
483 entry = g_malloc0(sizeof(*entry));
484 entry->value = info;
485 entry->next = prop_list;
486 prop_list = entry;
487 }
488 klass = object_class_get_parent(klass);
489 } while (klass != object_class_by_name(TYPE_DEVICE));
490
491 return prop_list;
492}
e4e31c63 493
76b64a7a
AL
494CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
495{
496 return arch_query_cpu_definitions(errp);
497}
498
b224e5e2
LC
499void qmp_add_client(const char *protocol, const char *fdname,
500 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
501 Error **errp)
502{
503 CharDriverState *s;
504 int fd;
505
506 fd = monitor_get_fd(cur_mon, fdname, errp);
507 if (fd < 0) {
508 return;
509 }
510
511 if (strcmp(protocol, "spice") == 0) {
512 if (!using_spice) {
513 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
514 close(fd);
515 return;
516 }
517 skipauth = has_skipauth ? skipauth : false;
518 tls = has_tls ? tls : false;
519 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
520 error_setg(errp, "spice failed to add client");
521 close(fd);
522 }
523 return;
524#ifdef CONFIG_VNC
525 } else if (strcmp(protocol, "vnc") == 0) {
526 skipauth = has_skipauth ? skipauth : false;
527 vnc_display_add_client(NULL, fd, skipauth);
528 return;
529#endif
530 } else if ((s = qemu_chr_find(protocol)) != NULL) {
531 if (qemu_chr_add_client(s, fd) < 0) {
532 error_setg(errp, "failed to add client");
533 close(fd);
534 return;
535 }
536 return;
537 }
538
539 error_setg(errp, "protocol '%s' is invalid", protocol);
540 close(fd);
541}
ab2d0531 542
cff8b2c6
PB
543void object_add(const char *type, const char *id, const QDict *qdict,
544 Visitor *v, Error **errp)
545{
546 Object *obj;
c3481247 547 ObjectClass *klass;
cff8b2c6
PB
548 const QDictEntry *e;
549 Error *local_err = NULL;
550
c3481247
EH
551 klass = object_class_by_name(type);
552 if (!klass) {
cff8b2c6
PB
553 error_setg(errp, "invalid class name");
554 return;
555 }
556
c3481247
EH
557 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
558 error_setg(errp, "object type '%s' isn't supported by object-add",
559 type);
560 return;
561 }
562
563 if (object_class_is_abstract(klass)) {
564 error_setg(errp, "object type '%s' is abstract", type);
565 return;
566 }
567
cff8b2c6
PB
568 obj = object_new(type);
569 if (qdict) {
570 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
571 object_property_set(obj, v, e->key, &local_err);
572 if (local_err) {
69252c04 573 goto out;
cff8b2c6
PB
574 }
575 }
576 }
577
269e09f3
IM
578 user_creatable_complete(obj, &local_err);
579 if (local_err) {
580 goto out;
581 }
582
cff8b2c6 583 object_property_add_child(container_get(object_get_root(), "/objects"),
69252c04
IM
584 id, obj, &local_err);
585out:
586 if (local_err) {
587 error_propagate(errp, local_err);
588 }
cff8b2c6
PB
589 object_unref(obj);
590}
591
592int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
593{
594 const char *type = qdict_get_str(qdict, "qom-type");
595 const char *id = qdict_get_str(qdict, "id");
596 QObject *props = qdict_get(qdict, "props");
597 const QDict *pdict = NULL;
598 Error *local_err = NULL;
599 QmpInputVisitor *qiv;
600
601 if (props) {
602 pdict = qobject_to_qdict(props);
603 if (!pdict) {
604 error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
605 goto out;
606 }
607 }
608
609 qiv = qmp_input_visitor_new(props);
610 object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
611 qmp_input_visitor_cleanup(qiv);
612
613out:
614 if (local_err) {
615 qerror_report_err(local_err);
616 error_free(local_err);
617 return -1;
618 }
619
620 return 0;
621}
622
ab2d0531
PB
623void qmp_object_del(const char *id, Error **errp)
624{
625 Object *container;
626 Object *obj;
627
628 container = container_get(object_get_root(), "/objects");
629 obj = object_resolve_path_component(container, id);
630 if (!obj) {
631 error_setg(errp, "object id not found");
632 return;
633 }
634 object_unparent(obj);
635}