[libvirt] [PATCH 04/13] qemu: monitor: Add better APIs for adding of objects to qemu

Peter Krempa pkrempa at redhat.com
Wed May 30 17:06:28 UTC 2018


Use the new monitor command internal API to allow wrapping of the object
name and alias into the JSON props so that they don't have to be passed
out of band.

The new API also takes a double pointer so that it can be cleared when
the value is consumed so that it does not need to happen in every single
caller.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_monitor.c      | 116 +++++++++++++++++++++++++++++++++++++++++--
 src/qemu/qemu_monitor.h      |  13 +++++
 src/qemu/qemu_monitor_json.c |  15 ++----
 src/qemu/qemu_monitor_json.h |   2 -
 4 files changed, 129 insertions(+), 17 deletions(-)

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 77ad47cb74..fe8fdfaf9a 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -2991,6 +2991,109 @@ qemuMonitorAddDeviceArgs(qemuMonitorPtr mon,
 }


+virJSONValuePtr
+qemuMonitorCreateObjectPropsWrap(const char *type,
+                                 const char *alias,
+                                 virJSONValuePtr *props)
+{
+    virJSONValuePtr ret;
+
+    ignore_value(virJSONValueObjectCreate(&ret,
+                                          "s:qom-type", type,
+                                          "s:id", alias,
+                                          "A:props", props,
+                                          NULL));
+    return ret;
+}
+
+
+
+/**
+ * qemuMonitorCreateObjectProps:
+ * @propsret: returns full object properties
+ * @type: Type name of object to add
+ * @objalias: Alias of the new object
+ * @...: Optional arguments for the given object. See virJSONValueObjectAddVArgs.
+ *
+ * Returns a JSONValue containing everything on success and NULL on error.
+ */
+int
+qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
+                             const char *type,
+                             const char *alias,
+                             ...)
+{
+    virJSONValuePtr props = NULL;
+    int ret = -1;
+    va_list args;
+
+    *propsret = NULL;
+
+    va_start(args, alias);
+
+    if (!(virJSONValueObjectCreateVArgs(&props, args)))
+        goto cleanup;
+
+    if (!(*propsret = qemuMonitorCreateObjectPropsWrap(type, alias, &props)))
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    virJSONValueFree(props);
+    va_end(args);
+    return ret;
+}
+
+
+/**
+ * qemuMonitorAddObject:
+ * @mon: Pointer to monitor object
+ * @props: Optional arguments for the given type. The object is consumed and
+ *         the pointer is cleared.
+ * @alias: If not NULL, returns the alias of the added object if it was added
+ *         successfully to qemu. Caller should free the returned pointer.
+ *
+ * Returns 0 on success -1 on error.
+ */
+int
+qemuMonitorAddObject(qemuMonitorPtr mon,
+                     virJSONValuePtr *props,
+                     char **alias)
+{
+    const char *type = virJSONValueObjectGetString(*props, "qom-type");
+    const char *id = virJSONValueObjectGetString(*props, "id");
+    char *tmp = NULL;
+    int ret = -1;
+
+    VIR_DEBUG("type=%s id=%s", NULLSTR(type), NULLSTR(id));
+
+    QEMU_CHECK_MONITOR_GOTO(mon, cleanup);
+
+    if (!id) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("missing alias for qemu object '%s'"), NULLSTR(type));
+        goto cleanup;
+    }
+
+    if (alias && VIR_STRDUP(tmp, id) < 0)
+        goto cleanup;
+
+    ret = qemuMonitorJSONAddObject(mon, *props);
+    *props = NULL;
+
+    if (alias)
+        VIR_STEAL_PTR(*alias, tmp);
+
+ cleanup:
+    VIR_FREE(tmp);
+    virJSONValueFree(*props);
+    *props = NULL;
+    return ret;
+}
+
+
+
 /**
  * qemuMonitorAddObjectType:
  * @mon: Pointer to monitor object
@@ -3007,15 +3110,20 @@ qemuMonitorAddObjectType(qemuMonitorPtr mon,
                          const char *objalias,
                          virJSONValuePtr props)
 {
+    virJSONValuePtr tmpprops = NULL;
+    int ret = -1;
+
     VIR_DEBUG("type=%s objalias=%s props=%p", type, objalias, props);

-    QEMU_CHECK_MONITOR_GOTO(mon, error);
+    if (!(tmpprops = qemuMonitorCreateObjectPropsWrap(type, objalias, &props)))
+        goto cleanup;

-    return qemuMonitorJSONAddObject(mon, type, objalias, props);
+    ret = qemuMonitorAddObject(mon, &tmpprops, NULL);

- error:
+ cleanup:
     virJSONValueFree(props);
-    return -1;
+    virJSONValueFree(tmpprops);
+    return ret;
 }


diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 77a26d4a8a..0c13391520 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -797,6 +797,19 @@ int qemuMonitorAddDeviceWithFd(qemuMonitorPtr mon,
 int qemuMonitorDelDevice(qemuMonitorPtr mon,
                          const char *devalias);

+virJSONValuePtr qemuMonitorCreateObjectPropsWrap(const char *type,
+                                                 const char *alias,
+                                                 virJSONValuePtr *props);
+
+int qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
+                                 const char *type,
+                                 const char *alias,
+                                 ...);
+
+int qemuMonitorAddObject(qemuMonitorPtr mon,
+                         virJSONValuePtr *props,
+                         char **alias);
+
 int qemuMonitorAddObjectType(qemuMonitorPtr mon,
                              const char *type,
                              const char *objalias,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 9f5c358795..7522eaeef0 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -4003,21 +4003,15 @@ qemuMonitorJSONAddDevice(qemuMonitorPtr mon,
 }


-int qemuMonitorJSONAddObject(qemuMonitorPtr mon,
-                             const char *type,
-                             const char *objalias,
-                             virJSONValuePtr props)
+int
+qemuMonitorJSONAddObject(qemuMonitorPtr mon,
+                         virJSONValuePtr props)
 {
     int ret = -1;
     virJSONValuePtr cmd;
     virJSONValuePtr reply = NULL;

-    cmd = qemuMonitorJSONMakeCommand("object-add",
-                                     "s:qom-type", type,
-                                     "s:id", objalias,
-                                     "A:props", &props,
-                                     NULL);
-    if (!cmd)
+    if (!(cmd = qemuMonitorJSONMakeCommandInternal("object-add", props, false)))
         goto cleanup;

     if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
@@ -4030,7 +4024,6 @@ int qemuMonitorJSONAddObject(qemuMonitorPtr mon,
  cleanup:
     virJSONValueFree(cmd);
     virJSONValueFree(reply);
-    virJSONValueFree(props);
     return ret;
 }

diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index f4ac8319ac..5fc51b1d6b 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -230,8 +230,6 @@ int qemuMonitorJSONDelDevice(qemuMonitorPtr mon,
                              const char *devalias);

 int qemuMonitorJSONAddObject(qemuMonitorPtr mon,
-                             const char *type,
-                             const char *objalias,
                              virJSONValuePtr props);

 int qemuMonitorJSONDelObject(qemuMonitorPtr mon,
-- 
2.16.2




More information about the libvir-list mailing list