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