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