[libvirt] [PATCH v3 14/20] Add a qemuMonitorGetObjectTypes() method for QMP qom-list-types command

Daniel P. Berrange berrange at redhat.com
Tue Sep 25 18:00:07 UTC 2012


From: "Daniel P. Berrange" <berrange at redhat.com>

Add a new qemuMonitorGetObjectTypes() method to support invocation
of the 'qom-list' JSON monitor command. No HMP equivalent
is required, since this will only be present for QEMU >= 1.2

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 src/qemu/qemu_monitor.c      | 22 +++++++++++++
 src/qemu/qemu_monitor.h      |  3 ++
 src/qemu/qemu_monitor_json.c | 74 ++++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor_json.h |  4 +++
 4 files changed, 103 insertions(+)

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 9e4c3fb..4ae89bf 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3116,3 +3116,25 @@ int qemuMonitorGetEvents(qemuMonitorPtr mon,
 
     return qemuMonitorJSONGetEvents(mon, events);
 }
+
+
+int qemuMonitorGetObjectTypes(qemuMonitorPtr mon,
+                              char ***types)
+{
+    VIR_DEBUG("mon=%p types=%p",
+              mon, types);
+
+    if (!mon) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("monitor must not be NULL"));
+        return -1;
+    }
+
+    if (!mon->json) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("JSON monitor is required"));
+        return -1;
+    }
+
+    return qemuMonitorJSONGetObjectTypes(mon, types);
+}
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 17cda51..69c554f 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -599,6 +599,9 @@ int qemuMonitorGetCommands(qemuMonitorPtr mon,
 int qemuMonitorGetEvents(qemuMonitorPtr mon,
                          char ***events);
 
+int qemuMonitorGetObjectTypes(qemuMonitorPtr mon,
+                              char ***types);
+
 
 /**
  * When running two dd process and using <> redirection, we need a
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 275f7df..c2bd533 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -4166,3 +4166,77 @@ cleanup:
     virJSONValueFree(reply);
     return ret;
 }
+
+
+int qemuMonitorJSONGetObjectTypes(qemuMonitorPtr mon,
+                                  char ***types)
+{
+    int ret;
+    virJSONValuePtr cmd;
+    virJSONValuePtr reply = NULL;
+    virJSONValuePtr data;
+    char **typelist = NULL;
+    int n = 0;
+    size_t i;
+
+    *types = NULL;
+
+    if (!(cmd = qemuMonitorJSONMakeCommand("qom-list-types", NULL)))
+        return -1;
+
+    ret = qemuMonitorJSONCommand(mon, cmd, &reply);
+
+    if (ret == 0)
+        ret = qemuMonitorJSONCheckError(cmd, reply);
+
+    if (ret < 0)
+        goto cleanup;
+
+    ret = -1;
+
+    if (!(data = virJSONValueObjectGet(reply, "return"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("qom-list-types reply was missing return data"));
+        goto cleanup;
+    }
+
+    if ((n = virJSONValueArraySize(data)) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("qom-list-types reply data was not an array"));
+        goto cleanup;
+    }
+
+    if (VIR_ALLOC_N(typelist, n) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    for (i = 0 ; i < n ; i++) {
+        virJSONValuePtr child = virJSONValueArrayGet(data, i);
+        const char *tmp;
+
+        if (!(tmp = virJSONValueObjectGetString(child, "name"))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("qom-list-types reply data was missing 'name'"));
+            goto cleanup;
+        }
+
+        if (!(typelist[i] = strdup(tmp))) {
+            virReportOOMError();
+            goto cleanup;
+        }
+    }
+
+    ret = n;
+    *types = typelist;
+
+cleanup:
+    if (ret < 0 && typelist) {
+        for (i = 0 ; i < n ; i++)
+            VIR_FREE(typelist[i]);
+        VIR_FREE(typelist);
+    }
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index 4798d75..87be22e 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -302,4 +302,8 @@ int qemuMonitorJSONGetEvents(qemuMonitorPtr mon,
                              char ***events)
     ATTRIBUTE_NONNULL(2);
 
+int qemuMonitorJSONGetObjectTypes(qemuMonitorPtr mon,
+                                  char ***types)
+    ATTRIBUTE_NONNULL(2);
+
 #endif /* QEMU_MONITOR_JSON_H */
-- 
1.7.11.4




More information about the libvir-list mailing list