]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Add a qemuMonitorGetVersion() method for QMP query-version command
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 15 Aug 2012 14:04:09 +0000 (15:04 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Thu, 27 Sep 2012 09:46:34 +0000 (10:46 +0100)
Add a new qemuMonitorGetVersion() method to support invocation
of the 'query-version' JSON monitor command. No HMP equivalent
is provided, since this will only be used for QEMU >= 1.2

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/qemu/qemu_monitor.c
src/qemu/qemu_monitor.h
src/qemu/qemu_monitor_json.c
src/qemu/qemu_monitor_json.h
tests/qemumonitorjsontest.c

index 58a9f945cf5350fb811f12fa1c693a053fbec6fa..a6dd47a5b86e8f59fdbf82866d552dbafc4b3caa 100644 (file)
@@ -3022,3 +3022,27 @@ int qemuMonitorSystemWakeup(qemuMonitorPtr mon)
 
     return qemuMonitorJSONSystemWakeup(mon);
 }
+
+int qemuMonitorGetVersion(qemuMonitorPtr mon,
+                          int *major,
+                          int *minor,
+                          int *micro,
+                          char **package)
+{
+    VIR_DEBUG("mon=%p major=%p minor=%p micro=%p package=%p",
+              mon, major, minor, micro, package);
+
+    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 qemuMonitorJSONGetVersion(mon, major, minor, micro, package);
+}
index 3455ab9c2551f2a7e3ab690024e47775dca06139..9d887475c4c1fde6b7b2a6748bc79434ce17ff4a 100644 (file)
@@ -571,6 +571,13 @@ int qemuMonitorGetBlockIoThrottle(qemuMonitorPtr mon,
 
 int qemuMonitorSystemWakeup(qemuMonitorPtr mon);
 
+int qemuMonitorGetVersion(qemuMonitorPtr mon,
+                          int *major,
+                          int *minor,
+                          int *micro,
+                          char **package)
+    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
+
 /**
  * When running two dd process and using <> redirection, we need a
  * shell that will not truncate files.  These two strings serve that
index c55ee2bb090e1710e59ad4f67f852061a197d2ee..f6a9e0b822839e8fbce6773278b531fd6e8860df 100644 (file)
@@ -3863,3 +3863,81 @@ int qemuMonitorJSONSystemWakeup(qemuMonitorPtr mon)
     virJSONValueFree(reply);
     return ret;
 }
+
+int qemuMonitorJSONGetVersion(qemuMonitorPtr mon,
+                              int *major,
+                              int *minor,
+                              int *micro,
+                              char **package)
+{
+    int ret;
+    virJSONValuePtr cmd;
+    virJSONValuePtr reply = NULL;
+    virJSONValuePtr data;
+    virJSONValuePtr qemu;
+
+    *major = *minor = *micro = 0;
+    if (package)
+        *package = NULL;
+
+    if (!(cmd = qemuMonitorJSONMakeCommand("query-version", 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",
+                       _("query-version reply was missing 'return' data"));
+        goto cleanup;
+    }
+
+    if (!(qemu = virJSONValueObjectGet(data, "qemu"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("query-version reply was missing 'qemu' data"));
+        goto cleanup;
+    }
+
+    if (virJSONValueObjectGetNumberInt(qemu, "major", major) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("query-version reply was missing 'major' version"));
+        goto cleanup;
+    }
+    if (virJSONValueObjectGetNumberInt(qemu, "minor", minor) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("query-version reply was missing 'minor' version"));
+        goto cleanup;
+    }
+    if (virJSONValueObjectGetNumberInt(qemu, "micro", micro) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("query-version reply was missing 'micro' version"));
+        goto cleanup;
+    }
+
+    if (package) {
+        const char *tmp;
+        if (!(tmp = virJSONValueObjectGetString(data, "package"))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("query-version reply was missing 'package' version"));
+            goto cleanup;
+        }
+        if (!(*package = strdup(tmp))) {
+            virReportOOMError();
+            goto cleanup;
+        }
+    }
+
+    ret = 0;
+
+cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
index 751b81f948dbc582c33cf1b5af8122dc09e392e1..f37e4a00aea8fe246bc3c06dc7fd1c5e95fc741a 100644 (file)
@@ -283,4 +283,11 @@ int qemuMonitorJSONGetBlockIoThrottle(qemuMonitorPtr mon,
 
 int qemuMonitorJSONSystemWakeup(qemuMonitorPtr mon);
 
+int qemuMonitorJSONGetVersion(qemuMonitorPtr mon,
+                              int *major,
+                              int *minor,
+                              int *micro,
+                              char **package)
+    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
+
 #endif /* QEMU_MONITOR_JSON_H */
index 48e78e7d04ab44567d773c7a76c13878fa353c2c..c3a0c45d17a9587a56b156c8d45bbc2e45291093 100644 (file)
@@ -122,6 +122,107 @@ cleanup:
     return ret;
 }
 
+static int
+testQemuMonitorJSONGetVersion(const void *data)
+{
+    virCapsPtr caps = (virCapsPtr)data;
+    qemuMonitorTestPtr test = qemuMonitorTestNew(true, caps);
+    int ret = -1;
+    int major;
+    int minor;
+    int micro;
+    char *package;
+
+    if (!test)
+        return -1;
+
+    if (qemuMonitorTestAddItem(test, "query-version",
+                               "{ "
+                               "  \"return\":{ "
+                               "     \"qemu\":{ "
+                               "        \"major\":1, "
+                               "        \"minor\":2, "
+                               "        \"micro\":3 "
+                               "      },"
+                               "     \"package\":\"\""
+                               "  }"
+                               "}") < 0)
+        goto cleanup;
+
+    if (qemuMonitorTestAddItem(test, "query-version",
+                               "{ "
+                               "  \"return\":{ "
+                               "     \"qemu\":{ "
+                               "        \"major\":0, "
+                               "        \"minor\":11, "
+                               "        \"micro\":6 "
+                               "      },"
+                               "     \"package\":\"2.283.el6\""
+                               "  }"
+                               "}") < 0)
+        goto cleanup;
+
+    if (qemuMonitorGetVersion(qemuMonitorTestGetMonitor(test),
+                              &major, &minor, &micro,
+                              &package) < 0)
+        goto cleanup;
+
+    if (major != 1) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Major %d was not 1", major);
+        goto cleanup;
+    }
+    if (minor != 2) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Minor %d was not 2", major);
+        goto cleanup;
+    }
+    if (micro != 3) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Micro %d was not 3", major);
+        goto cleanup;
+    }
+
+    if (STRNEQ(package, "")) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Package %s was not ''", package);
+        goto cleanup;
+    }
+
+    if (qemuMonitorGetVersion(qemuMonitorTestGetMonitor(test),
+                              &major, &minor, &micro,
+                              &package) < 0)
+        goto cleanup;
+
+    if (major != 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Major %d was not 0", major);
+        goto cleanup;
+    }
+    if (minor != 11) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Minor %d was not 11", major);
+        goto cleanup;
+    }
+    if (micro != 6) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Micro %d was not 6", major);
+        goto cleanup;
+    }
+
+    if (STRNEQ(package, "2.283.el6")) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Package %s was not '2.283.el6'", package);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    qemuMonitorTestFree(test);
+    return ret;
+}
+
 static int
 mymain(void)
 {
@@ -141,6 +242,7 @@ mymain(void)
         ret = -1
 
     DO_TEST(GetStatus);
+    DO_TEST(GetVersion);
 
     virCapabilitiesFree(caps);