9c6c51
From 968116c9fb74569a6ea61e7eef6a87b6e766a61b Mon Sep 17 00:00:00 2001
9c6c51
Message-Id: <968116c9fb74569a6ea61e7eef6a87b6e766a61b@dist-git>
9c6c51
From: Michal Privoznik <mprivozn@redhat.com>
9c6c51
Date: Wed, 11 Jul 2018 17:27:27 +0200
9c6c51
Subject: [PATCH] qemu_monitor: Introduce qemuMonitorJSONGetPRManagerInfo
9c6c51
MIME-Version: 1.0
9c6c51
Content-Type: text/plain; charset=UTF-8
9c6c51
Content-Transfer-Encoding: 8bit
9c6c51
9c6c51
https://bugzilla.redhat.com/show_bug.cgi?id=1470007
9c6c51
9c6c51
This function fetches status of all pr-managers. So far, qemu
9c6c51
reports only a single attribute "connected" but that fits our
9c6c51
needs.
9c6c51
9c6c51
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
9c6c51
(cherry picked from commit 5f085862e87668e77333cc369bf66ff3b6c19ae8)
9c6c51
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
9c6c51
Reviewed-by: Ján Tomko <jtomko@redhat.com>
9c6c51
---
9c6c51
 src/qemu/qemu_monitor.c      | 25 ++++++++++++
9c6c51
 src/qemu/qemu_monitor.h      |  9 ++++
9c6c51
 src/qemu/qemu_monitor_json.c | 79 ++++++++++++++++++++++++++++++++++++
9c6c51
 src/qemu/qemu_monitor_json.h |  4 ++
9c6c51
 4 files changed, 117 insertions(+)
9c6c51
9c6c51
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
9c6c51
index 86b2b6e985..bc116e4e2d 100644
9c6c51
--- a/src/qemu/qemu_monitor.c
9c6c51
+++ b/src/qemu/qemu_monitor.c
9c6c51
@@ -4346,3 +4346,28 @@ qemuMonitorGetSEVMeasurement(qemuMonitorPtr mon)
9c6c51
 
9c6c51
     return qemuMonitorJSONGetSEVMeasurement(mon);
9c6c51
 }
9c6c51
+
9c6c51
+
9c6c51
+int
9c6c51
+qemuMonitorGetPRManagerInfo(qemuMonitorPtr mon,
9c6c51
+                            virHashTablePtr *retinfo)
9c6c51
+{
9c6c51
+    int ret = -1;
9c6c51
+    virHashTablePtr info = NULL;
9c6c51
+
9c6c51
+    *retinfo = NULL;
9c6c51
+
9c6c51
+    QEMU_CHECK_MONITOR(mon);
9c6c51
+
9c6c51
+    if (!(info = virHashCreate(10, virHashValueFree)))
9c6c51
+        goto cleanup;
9c6c51
+
9c6c51
+    if (qemuMonitorJSONGetPRManagerInfo(mon, info) < 0)
9c6c51
+        goto cleanup;
9c6c51
+
9c6c51
+    VIR_STEAL_PTR(*retinfo, info);
9c6c51
+    ret = 0;
9c6c51
+ cleanup:
9c6c51
+    virHashFree(info);
9c6c51
+    return ret;
9c6c51
+}
9c6c51
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
9c6c51
index a906bc8410..e8ed2d044c 100644
9c6c51
--- a/src/qemu/qemu_monitor.h
9c6c51
+++ b/src/qemu/qemu_monitor.h
9c6c51
@@ -1157,4 +1157,13 @@ int qemuMonitorBlockdevDel(qemuMonitorPtr mon,
9c6c51
 char *
9c6c51
 qemuMonitorGetSEVMeasurement(qemuMonitorPtr mon);
9c6c51
 
9c6c51
+typedef struct _qemuMonitorPRManagerInfo qemuMonitorPRManagerInfo;
9c6c51
+typedef qemuMonitorPRManagerInfo *qemuMonitorPRManagerInfoPtr;
9c6c51
+struct _qemuMonitorPRManagerInfo {
9c6c51
+    bool connected;
9c6c51
+};
9c6c51
+
9c6c51
+int qemuMonitorGetPRManagerInfo(qemuMonitorPtr mon,
9c6c51
+                                virHashTablePtr *retinfo);
9c6c51
+
9c6c51
 #endif /* QEMU_MONITOR_H */
9c6c51
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
9c6c51
index 03c94cd88b..66c525ed0c 100644
9c6c51
--- a/src/qemu/qemu_monitor_json.c
9c6c51
+++ b/src/qemu/qemu_monitor_json.c
9c6c51
@@ -8065,3 +8065,82 @@ qemuMonitorJSONGetSEVMeasurement(qemuMonitorPtr mon)
9c6c51
     virJSONValueFree(reply);
9c6c51
     return measurement;
9c6c51
 }
9c6c51
+
9c6c51
+
9c6c51
+/*
9c6c51
+ * Example return data
9c6c51
+ *
9c6c51
+ * "return": [
9c6c51
+ *   { "connected": true, "id": "pr-helper0" }
9c6c51
+ *  ]
9c6c51
+ */
9c6c51
+static int
9c6c51
+qemuMonitorJSONExtractPRManagerInfo(virJSONValuePtr reply,
9c6c51
+                                    virHashTablePtr info)
9c6c51
+{
9c6c51
+    qemuMonitorPRManagerInfoPtr entry = NULL;
9c6c51
+    virJSONValuePtr data;
9c6c51
+    int ret = -1;
9c6c51
+    size_t i;
9c6c51
+
9c6c51
+    data = virJSONValueObjectGetArray(reply, "return");
9c6c51
+
9c6c51
+    for (i = 0; i < virJSONValueArraySize(data); i++) {
9c6c51
+        virJSONValuePtr prManager = virJSONValueArrayGet(data, i);
9c6c51
+        const char *alias;
9c6c51
+
9c6c51
+        if (!(alias = virJSONValueObjectGetString(prManager, "id")))
9c6c51
+            goto malformed;
9c6c51
+
9c6c51
+        if (VIR_ALLOC(entry) < 0)
9c6c51
+            goto cleanup;
9c6c51
+
9c6c51
+        if (virJSONValueObjectGetBoolean(prManager,
9c6c51
+                                         "connected",
9c6c51
+                                         &entry->connected) < 0) {
9c6c51
+            goto malformed;
9c6c51
+        }
9c6c51
+
9c6c51
+        if (virHashAddEntry(info, alias, entry) < 0)
9c6c51
+            goto cleanup;
9c6c51
+
9c6c51
+        entry = NULL;
9c6c51
+    }
9c6c51
+
9c6c51
+    ret = 0;
9c6c51
+ cleanup:
9c6c51
+    VIR_FREE(entry);
9c6c51
+    return ret;
9c6c51
+
9c6c51
+ malformed:
9c6c51
+    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9c6c51
+                   _("malformed prManager reply"));
9c6c51
+    goto cleanup;
9c6c51
+}
9c6c51
+
9c6c51
+
9c6c51
+int
9c6c51
+qemuMonitorJSONGetPRManagerInfo(qemuMonitorPtr mon,
9c6c51
+                                virHashTablePtr info)
9c6c51
+{
9c6c51
+    int ret = -1;
9c6c51
+    virJSONValuePtr cmd;
9c6c51
+    virJSONValuePtr reply = NULL;
9c6c51
+
9c6c51
+    if (!(cmd = qemuMonitorJSONMakeCommand("query-pr-managers",
9c6c51
+                                           NULL)))
9c6c51
+        return -1;
9c6c51
+
9c6c51
+    if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
9c6c51
+        goto cleanup;
9c6c51
+
9c6c51
+    if (qemuMonitorJSONCheckReply(cmd, reply, VIR_JSON_TYPE_ARRAY) < 0)
9c6c51
+        goto cleanup;
9c6c51
+
9c6c51
+    ret = qemuMonitorJSONExtractPRManagerInfo(reply, info);
9c6c51
+ cleanup:
9c6c51
+    virJSONValueFree(cmd);
9c6c51
+    virJSONValueFree(reply);
9c6c51
+    return ret;
9c6c51
+
9c6c51
+}
9c6c51
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
9c6c51
index 6bc0dd3ad2..66536ceb97 100644
9c6c51
--- a/src/qemu/qemu_monitor_json.h
9c6c51
+++ b/src/qemu/qemu_monitor_json.h
9c6c51
@@ -550,4 +550,8 @@ int qemuMonitorJSONBlockdevDel(qemuMonitorPtr mon,
9c6c51
                                const char *nodename)
9c6c51
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
9c6c51
 
9c6c51
+int qemuMonitorJSONGetPRManagerInfo(qemuMonitorPtr mon,
9c6c51
+                                    virHashTablePtr info)
9c6c51
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
9c6c51
+
9c6c51
 #endif /* QEMU_MONITOR_JSON_H */
9c6c51
-- 
9c6c51
2.18.0
9c6c51