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