This API provides a NULL-terminated list of devices which are currently attached to a QEMU domain. --- src/qemu/qemu_monitor.c | 21 +++++++++++++++ src/qemu/qemu_monitor.h | 4 +++ src/qemu/qemu_monitor_json.c | 38 +++++++++++++++++++++++++++ src/qemu/qemu_monitor_json.h | 4 +++ tests/qemumonitorjsontest.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+) diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 1f6ce54..0b73411 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -3818,3 +3818,24 @@ int qemuMonitorDetachCharDev(qemuMonitorPtr mon, return qemuMonitorJSONDetachCharDev(mon, chrID); } + +int +qemuMonitorGetDeviceAliases(qemuMonitorPtr mon, + char ***aliases) +{ + VIR_DEBUG("mon=%p, aliases=%p", mon, aliases); + + if (!mon) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("monitor must not be NULL")); + return -1; + } + + if (!mon->json) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("JSON monitor is required")); + return -1; + } + + return qemuMonitorJSONGetDeviceAliases(mon, aliases); +} diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index eef0997..4a55501 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -713,6 +713,10 @@ int qemuMonitorAttachCharDev(qemuMonitorPtr mon, virDomainChrSourceDefPtr chr); int qemuMonitorDetachCharDev(qemuMonitorPtr mon, const char *chrID); + +int qemuMonitorGetDeviceAliases(qemuMonitorPtr mon, + char ***aliases); + /** * When running two dd process and using <> redirection, we need a * shell that will not truncate files. These two strings serve that diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 3318101..12f7e69 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -5415,3 +5415,41 @@ qemuMonitorJSONDetachCharDev(qemuMonitorPtr mon, virJSONValueFree(reply); return ret; } + + +int +qemuMonitorJSONGetDeviceAliases(qemuMonitorPtr mon, + char ***aliases) +{ + qemuMonitorJSONListPathPtr *paths = NULL; + char **alias; + int ret = -1; + size_t i; + int n; + + *aliases = NULL; + + n = qemuMonitorJSONGetObjectListPaths(mon, "/machine/peripheral", &paths); + if (n < 0) + return -1; + + if (VIR_ALLOC_N(*aliases, n + 1) < 0) + goto cleanup; + + alias = *aliases; + for (i = 0; i < n; i++) { + if (STRPREFIX(paths[i]->type, "child<")) { + *alias = paths[i]->name; + paths[i]->name = NULL; + alias++; + } + } + + ret = 0; + +cleanup: + for (i = 0; i < n; i++) + qemuMonitorJSONListPathFree(paths[i]); + VIR_FREE(paths); + return ret; +} diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index e94abf2..51cf19c 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -422,4 +422,8 @@ int qemuMonitorJSONAttachCharDev(qemuMonitorPtr mon, virDomainChrSourceDefPtr chr); int qemuMonitorJSONDetachCharDev(qemuMonitorPtr mon, const char *chrID); + +int qemuMonitorJSONGetDeviceAliases(qemuMonitorPtr mon, + char ***aliases); + #endif /* QEMU_MONITOR_JSON_H */ diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index 3d2ed4b..4061a0c 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -883,6 +883,66 @@ cleanup: static int +testQemuMonitorJSONGetDeviceAliases(const void *data) +{ + const virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data; + qemuMonitorTestPtr test = qemuMonitorTestNew(true, xmlopt); + int ret = -1; + char **aliases = NULL; + char **alias; + const char *expected[] = { + "virtio-disk25", "video0", "serial0", "ide0-0-0", "usb", NULL }; + + if (!test) + return -1; + + if (qemuMonitorTestAddItem(test, + "qom-list", + "{\"return\": [" + " {\"name\": \"virtio-disk25\"," + " \"type\": \"child<virtio-blk-pci>\"}," + " {\"name\": \"video0\"," + " \"type\": \"child<VGA>\"}," + " {\"name\": \"serial0\"," + " \"type\": \"child<isa-serial>\"}," + " {\"name\": \"ide0-0-0\"," + " \"type\": \"child<ide-cd>\"}," + " {\"name\": \"usb\"," + " \"type\": \"child<piix3-usb-uhci>\"}," + " {\"name\": \"type\", \"type\": \"string\"}" + "]}") < 0) + goto cleanup; + + if (qemuMonitorJSONGetDeviceAliases(qemuMonitorTestGetMonitor(test), + &aliases) < 0) + goto cleanup; + + if (!aliases) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", "no aliases returned"); + goto cleanup; + } + + ret = 0; + for (alias = aliases; *alias; alias++) { + if (!virStringArrayHasString((char **) expected, *alias)) { + fprintf(stderr, "got unexpected device alias '%s'\n", *alias); + ret = -1; + } + } + for (alias = (char **) expected; *alias; alias++) { + if (!virStringArrayHasString(aliases, *alias)) { + fprintf(stderr, "missing expected alias '%s'\n", *alias); + ret = -1; + } + } + +cleanup: + virStringFreeList(aliases); + return ret; +} + + +static int mymain(void) { int ret = 0; @@ -915,6 +975,7 @@ mymain(void) DO_TEST(GetListPaths); DO_TEST(GetObjectProperty); DO_TEST(SetObjectProperty); + DO_TEST(GetDeviceAliases); virObjectUnref(xmlopt); -- 1.8.3.2 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list