sensor: add pedometer sensor device
[sdk/emulator/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 "monitor/monitor.h"
18 #include "sysemu/sysemu.h"
19 #include "qmp-commands.h"
20 #include "sysemu/char.h"
21 #include "ui/qemu-spice.h"
22 #include "ui/vnc.h"
23 #include "sysemu/kvm.h"
24 #include "sysemu/arch_init.h"
25 #include "hw/qdev.h"
26 #include "sysemu/blockdev.h"
27 #include "sysemu/block-backend.h"
28 #include "qom/qom-qobject.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qapi/qmp/qobject.h"
31 #include "qapi/qmp-input-visitor.h"
32 #include "hw/boards.h"
33 #include "qom/object_interfaces.h"
34 #include "hw/mem/pc-dimm.h"
35 #include "hw/acpi/acpi_dev_interface.h"
36
37 NameInfo *qmp_query_name(Error **errp)
38 {
39     NameInfo *info = g_malloc0(sizeof(*info));
40
41     if (qemu_name) {
42         info->has_name = true;
43         info->name = g_strdup(qemu_name);
44     }
45
46     return info;
47 }
48
49 VersionInfo *qmp_query_version(Error **errp)
50 {
51     VersionInfo *info = g_new0(VersionInfo, 1);
52     const char *version = QEMU_VERSION;
53     const char *tmp;
54     int err;
55
56     info->qemu = g_new0(VersionTriple, 1);
57     err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
58     assert(err == 0);
59     tmp++;
60
61     err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
62     assert(err == 0);
63     tmp++;
64
65     err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
66     assert(err == 0);
67     info->package = g_strdup(QEMU_PKGVERSION);
68
69     return info;
70 }
71
72 KvmInfo *qmp_query_kvm(Error **errp)
73 {
74     KvmInfo *info = g_malloc0(sizeof(*info));
75
76     info->enabled = kvm_enabled();
77     info->present = kvm_available();
78
79     return info;
80 }
81
82 UuidInfo *qmp_query_uuid(Error **errp)
83 {
84     UuidInfo *info = g_malloc0(sizeof(*info));
85     char uuid[64];
86
87     snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
88                    qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
89                    qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
90                    qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
91                    qemu_uuid[14], qemu_uuid[15]);
92
93     info->UUID = g_strdup(uuid);
94     return info;
95 }
96
97 void qmp_quit(Error **errp)
98 {
99     no_shutdown = 0;
100     qemu_system_shutdown_request();
101 }
102
103 void qmp_stop(Error **errp)
104 {
105     if (runstate_check(RUN_STATE_INMIGRATE)) {
106         autostart = 0;
107     } else {
108         vm_stop(RUN_STATE_PAUSED);
109     }
110 }
111
112 void qmp_system_reset(Error **errp)
113 {
114     qemu_system_reset_request();
115 }
116
117 void qmp_system_powerdown(Error **erp)
118 {
119     qemu_system_powerdown_request();
120 }
121
122 void qmp_cpu(int64_t index, Error **errp)
123 {
124     /* Just do nothing */
125 }
126
127 void qmp_cpu_add(int64_t id, Error **errp)
128 {
129     MachineClass *mc;
130
131     mc = MACHINE_GET_CLASS(current_machine);
132     if (mc->hot_add_cpu) {
133         mc->hot_add_cpu(id, errp);
134     } else {
135         error_setg(errp, "Not supported");
136     }
137 }
138
139 #ifndef CONFIG_VNC
140 /* If VNC support is enabled, the "true" query-vnc command is
141    defined in the VNC subsystem */
142 VncInfo *qmp_query_vnc(Error **errp)
143 {
144     error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
145     return NULL;
146 };
147
148 VncInfo2List *qmp_query_vnc_servers(Error **errp)
149 {
150     error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
151     return NULL;
152 };
153 #endif
154
155 #ifndef CONFIG_SPICE
156 /*
157  * qmp-commands.hx ensures that QMP command query-spice exists only
158  * #ifdef CONFIG_SPICE.  Necessary for an accurate query-commands
159  * result.  However, the QAPI schema is blissfully unaware of that,
160  * and the QAPI code generator happily generates a dead
161  * qmp_marshal_query_spice() that calls qmp_query_spice().  Provide it
162  * one, or else linking fails.  FIXME Educate the QAPI schema on
163  * CONFIG_SPICE.
164  */
165 SpiceInfo *qmp_query_spice(Error **errp)
166 {
167     abort();
168 };
169 #endif
170
171 void qmp_cont(Error **errp)
172 {
173     Error *local_err = NULL;
174     BlockBackend *blk;
175     BlockDriverState *bs;
176
177     if (runstate_needs_reset()) {
178         error_setg(errp, "Resetting the Virtual Machine is required");
179         return;
180     } else if (runstate_check(RUN_STATE_SUSPENDED)) {
181         return;
182     }
183
184     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
185         blk_iostatus_reset(blk);
186     }
187     for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
188         bdrv_add_key(bs, NULL, &local_err);
189         if (local_err) {
190             error_propagate(errp, local_err);
191             return;
192         }
193     }
194
195     if (runstate_check(RUN_STATE_INMIGRATE)) {
196         autostart = 1;
197     } else {
198         vm_start();
199     }
200 }
201
202 void qmp_system_wakeup(Error **errp)
203 {
204     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
205 }
206
207 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
208 {
209     Object *obj;
210     bool ambiguous = false;
211     ObjectPropertyInfoList *props = NULL;
212     ObjectProperty *prop;
213     ObjectPropertyIterator *iter;
214
215     obj = object_resolve_path(path, &ambiguous);
216     if (obj == NULL) {
217         if (ambiguous) {
218             error_setg(errp, "Path '%s' is ambiguous", path);
219         } else {
220             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
221                       "Device '%s' not found", path);
222         }
223         return NULL;
224     }
225
226     iter = object_property_iter_init(obj);
227     while ((prop = object_property_iter_next(iter))) {
228         ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
229
230         entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
231         entry->next = props;
232         props = entry;
233
234         entry->value->name = g_strdup(prop->name);
235         entry->value->type = g_strdup(prop->type);
236     }
237     object_property_iter_free(iter);
238
239     return props;
240 }
241
242 void qmp_qom_set(const char *path, const char *property, QObject *value,
243                  Error **errp)
244 {
245     Object *obj;
246
247     obj = object_resolve_path(path, NULL);
248     if (!obj) {
249         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
250                   "Device '%s' not found", path);
251         return;
252     }
253
254     object_property_set_qobject(obj, value, property, errp);
255 }
256
257 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
258 {
259     Object *obj;
260
261     obj = object_resolve_path(path, NULL);
262     if (!obj) {
263         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
264                   "Device '%s' not found", path);
265         return NULL;
266     }
267
268     return object_property_get_qobject(obj, property, errp);
269 }
270
271 void qmp_set_password(const char *protocol, const char *password,
272                       bool has_connected, const char *connected, Error **errp)
273 {
274     int disconnect_if_connected = 0;
275     int fail_if_connected = 0;
276     int rc;
277
278     if (has_connected) {
279         if (strcmp(connected, "fail") == 0) {
280             fail_if_connected = 1;
281         } else if (strcmp(connected, "disconnect") == 0) {
282             disconnect_if_connected = 1;
283         } else if (strcmp(connected, "keep") == 0) {
284             /* nothing */
285         } else {
286             error_setg(errp, QERR_INVALID_PARAMETER, "connected");
287             return;
288         }
289     }
290
291     if (strcmp(protocol, "spice") == 0) {
292         if (!qemu_using_spice(errp)) {
293             return;
294         }
295         rc = qemu_spice_set_passwd(password, fail_if_connected,
296                                    disconnect_if_connected);
297         if (rc != 0) {
298             error_setg(errp, QERR_SET_PASSWD_FAILED);
299         }
300         return;
301     }
302
303     if (strcmp(protocol, "vnc") == 0) {
304         if (fail_if_connected || disconnect_if_connected) {
305             /* vnc supports "connected=keep" only */
306             error_setg(errp, QERR_INVALID_PARAMETER, "connected");
307             return;
308         }
309         /* Note that setting an empty password will not disable login through
310          * this interface. */
311         rc = vnc_display_password(NULL, password);
312         if (rc < 0) {
313             error_setg(errp, QERR_SET_PASSWD_FAILED);
314         }
315         return;
316     }
317
318     error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
319 }
320
321 void qmp_expire_password(const char *protocol, const char *whenstr,
322                          Error **errp)
323 {
324     time_t when;
325     int rc;
326
327     if (strcmp(whenstr, "now") == 0) {
328         when = 0;
329     } else if (strcmp(whenstr, "never") == 0) {
330         when = TIME_MAX;
331     } else if (whenstr[0] == '+') {
332         when = time(NULL) + strtoull(whenstr+1, NULL, 10);
333     } else {
334         when = strtoull(whenstr, NULL, 10);
335     }
336
337     if (strcmp(protocol, "spice") == 0) {
338         if (!qemu_using_spice(errp)) {
339             return;
340         }
341         rc = qemu_spice_set_pw_expire(when);
342         if (rc != 0) {
343             error_setg(errp, QERR_SET_PASSWD_FAILED);
344         }
345         return;
346     }
347
348     if (strcmp(protocol, "vnc") == 0) {
349         rc = vnc_display_pw_expire(NULL, when);
350         if (rc != 0) {
351             error_setg(errp, QERR_SET_PASSWD_FAILED);
352         }
353         return;
354     }
355
356     error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
357 }
358
359 #ifdef CONFIG_VNC
360 void qmp_change_vnc_password(const char *password, Error **errp)
361 {
362     if (vnc_display_password(NULL, password) < 0) {
363         error_setg(errp, QERR_SET_PASSWD_FAILED);
364     }
365 }
366
367 static void qmp_change_vnc_listen(const char *target, Error **errp)
368 {
369     QemuOptsList *olist = qemu_find_opts("vnc");
370     QemuOpts *opts;
371
372     if (strstr(target, "id=")) {
373         error_setg(errp, "id not supported");
374         return;
375     }
376
377     opts = qemu_opts_find(olist, "default");
378     if (opts) {
379         qemu_opts_del(opts);
380     }
381     opts = vnc_parse(target, errp);
382     if (!opts) {
383         return;
384     }
385
386     vnc_display_open("default", errp);
387 }
388
389 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
390                            Error **errp)
391 {
392     if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
393         if (!has_arg) {
394             error_setg(errp, QERR_MISSING_PARAMETER, "password");
395         } else {
396             qmp_change_vnc_password(arg, errp);
397         }
398     } else {
399         qmp_change_vnc_listen(target, errp);
400     }
401 }
402 #else
403 void qmp_change_vnc_password(const char *password, Error **errp)
404 {
405     error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
406 }
407 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
408                            Error **errp)
409 {
410     error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
411 }
412 #endif /* !CONFIG_VNC */
413
414 void qmp_change(const char *device, const char *target,
415                 bool has_arg, const char *arg, Error **errp)
416 {
417     if (strcmp(device, "vnc") == 0) {
418         qmp_change_vnc(target, has_arg, arg, errp);
419     } else {
420         qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
421                                    errp);
422     }
423 }
424
425 static void qom_list_types_tramp(ObjectClass *klass, void *data)
426 {
427     ObjectTypeInfoList *e, **pret = data;
428     ObjectTypeInfo *info;
429
430     info = g_malloc0(sizeof(*info));
431     info->name = g_strdup(object_class_get_name(klass));
432
433     e = g_malloc0(sizeof(*e));
434     e->value = info;
435     e->next = *pret;
436     *pret = e;
437 }
438
439 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
440                                        const char *implements,
441                                        bool has_abstract,
442                                        bool abstract,
443                                        Error **errp)
444 {
445     ObjectTypeInfoList *ret = NULL;
446
447     object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
448
449     return ret;
450 }
451
452 /* Return a DevicePropertyInfo for a qdev property.
453  *
454  * If a qdev property with the given name does not exist, use the given default
455  * type.  If the qdev property info should not be shown, return NULL.
456  *
457  * The caller must free the return value.
458  */
459 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
460                                                      const char *name,
461                                                      const char *default_type,
462                                                      const char *description)
463 {
464     DevicePropertyInfo *info;
465     Property *prop;
466
467     do {
468         for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
469             if (strcmp(name, prop->name) != 0) {
470                 continue;
471             }
472
473             /*
474              * TODO Properties without a parser are just for dirty hacks.
475              * qdev_prop_ptr is the only such PropertyInfo.  It's marked
476              * for removal.  This conditional should be removed along with
477              * it.
478              */
479             if (!prop->info->set) {
480                 return NULL;           /* no way to set it, don't show */
481             }
482
483             info = g_malloc0(sizeof(*info));
484             info->name = g_strdup(prop->name);
485             info->type = g_strdup(prop->info->name);
486             info->has_description = !!prop->info->description;
487             info->description = g_strdup(prop->info->description);
488             return info;
489         }
490         klass = object_class_get_parent(klass);
491     } while (klass != object_class_by_name(TYPE_DEVICE));
492
493     /* Not a qdev property, use the default type */
494     info = g_malloc0(sizeof(*info));
495     info->name = g_strdup(name);
496     info->type = g_strdup(default_type);
497     info->has_description = !!description;
498     info->description = g_strdup(description);
499
500     return info;
501 }
502
503 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
504                                                    Error **errp)
505 {
506     ObjectClass *klass;
507     Object *obj;
508     ObjectProperty *prop;
509     ObjectPropertyIterator *iter;
510     DevicePropertyInfoList *prop_list = NULL;
511
512     klass = object_class_by_name(typename);
513     if (klass == NULL) {
514         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
515                   "Device '%s' not found", typename);
516         return NULL;
517     }
518
519     klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
520     if (klass == NULL) {
521         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
522         return NULL;
523     }
524
525     if (object_class_is_abstract(klass)) {
526         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
527                    "non-abstract device type");
528         return NULL;
529     }
530
531     if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
532         error_setg(errp, "Can't list properties of device '%s'", typename);
533         return NULL;
534     }
535
536     obj = object_new(typename);
537
538     iter = object_property_iter_init(obj);
539     while ((prop = object_property_iter_next(iter))) {
540         DevicePropertyInfo *info;
541         DevicePropertyInfoList *entry;
542
543         /* Skip Object and DeviceState properties */
544         if (strcmp(prop->name, "type") == 0 ||
545             strcmp(prop->name, "realized") == 0 ||
546             strcmp(prop->name, "hotpluggable") == 0 ||
547             strcmp(prop->name, "hotplugged") == 0 ||
548             strcmp(prop->name, "parent_bus") == 0) {
549             continue;
550         }
551
552         /* Skip legacy properties since they are just string versions of
553          * properties that we already list.
554          */
555         if (strstart(prop->name, "legacy-", NULL)) {
556             continue;
557         }
558
559         info = make_device_property_info(klass, prop->name, prop->type,
560                                          prop->description);
561         if (!info) {
562             continue;
563         }
564
565         entry = g_malloc0(sizeof(*entry));
566         entry->value = info;
567         entry->next = prop_list;
568         prop_list = entry;
569     }
570     object_property_iter_free(iter);
571
572     object_unref(obj);
573
574     return prop_list;
575 }
576
577 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
578 {
579     return arch_query_cpu_definitions(errp);
580 }
581
582 void qmp_add_client(const char *protocol, const char *fdname,
583                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
584                     Error **errp)
585 {
586     CharDriverState *s;
587     int fd;
588
589     fd = monitor_get_fd(cur_mon, fdname, errp);
590     if (fd < 0) {
591         return;
592     }
593
594     if (strcmp(protocol, "spice") == 0) {
595         if (!qemu_using_spice(errp)) {
596             close(fd);
597             return;
598         }
599         skipauth = has_skipauth ? skipauth : false;
600         tls = has_tls ? tls : false;
601         if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
602             error_setg(errp, "spice failed to add client");
603             close(fd);
604         }
605         return;
606 #ifdef CONFIG_VNC
607     } else if (strcmp(protocol, "vnc") == 0) {
608         skipauth = has_skipauth ? skipauth : false;
609         vnc_display_add_client(NULL, fd, skipauth);
610         return;
611 #endif
612     } else if ((s = qemu_chr_find(protocol)) != NULL) {
613         if (qemu_chr_add_client(s, fd) < 0) {
614             error_setg(errp, "failed to add client");
615             close(fd);
616             return;
617         }
618         return;
619     }
620
621     error_setg(errp, "protocol '%s' is invalid", protocol);
622     close(fd);
623 }
624
625 void object_add(const char *type, const char *id, const QDict *qdict,
626                 Visitor *v, Error **errp)
627 {
628     Object *obj;
629     ObjectClass *klass;
630     const QDictEntry *e;
631     Error *local_err = NULL;
632
633     klass = object_class_by_name(type);
634     if (!klass) {
635         error_setg(errp, "invalid object type: %s", type);
636         return;
637     }
638
639     if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
640         error_setg(errp, "object type '%s' isn't supported by object-add",
641                    type);
642         return;
643     }
644
645     if (object_class_is_abstract(klass)) {
646         error_setg(errp, "object type '%s' is abstract", type);
647         return;
648     }
649
650     obj = object_new(type);
651     if (qdict) {
652         for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
653             object_property_set(obj, v, e->key, &local_err);
654             if (local_err) {
655                 goto out;
656             }
657         }
658     }
659
660     object_property_add_child(object_get_objects_root(),
661                               id, obj, &local_err);
662     if (local_err) {
663         goto out;
664     }
665
666     user_creatable_complete(obj, &local_err);
667     if (local_err) {
668         object_property_del(object_get_objects_root(),
669                             id, &error_abort);
670         goto out;
671     }
672 out:
673     if (local_err) {
674         error_propagate(errp, local_err);
675     }
676     object_unref(obj);
677 }
678
679 void qmp_object_add(const char *type, const char *id,
680                     bool has_props, QObject *props, Error **errp)
681 {
682     const QDict *pdict = NULL;
683     QmpInputVisitor *qiv;
684
685     if (props) {
686         pdict = qobject_to_qdict(props);
687         if (!pdict) {
688             error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
689             return;
690         }
691     }
692
693     qiv = qmp_input_visitor_new(props);
694     object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
695     qmp_input_visitor_cleanup(qiv);
696 }
697
698 void qmp_object_del(const char *id, Error **errp)
699 {
700     Object *container;
701     Object *obj;
702
703     container = object_get_objects_root();
704     obj = object_resolve_path_component(container, id);
705     if (!obj) {
706         error_setg(errp, "object id not found");
707         return;
708     }
709
710     if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
711         error_setg(errp, "%s is in use, can not be deleted", id);
712         return;
713     }
714     object_unparent(obj);
715 }
716
717 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
718 {
719     MemoryDeviceInfoList *head = NULL;
720     MemoryDeviceInfoList **prev = &head;
721
722     qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
723
724     return head;
725 }
726
727 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
728 {
729     bool ambig;
730     ACPIOSTInfoList *head = NULL;
731     ACPIOSTInfoList **prev = &head;
732     Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
733
734     if (obj) {
735         AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
736         AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
737
738         adevc->ospm_status(adev, &prev);
739     } else {
740         error_setg(errp, "command is not supported, missing ACPI device");
741     }
742
743     return head;
744 }