]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: monitor: Add better APIs for adding of objects to qemu
authorPeter Krempa <pkrempa@redhat.com>
Thu, 17 May 2018 14:43:58 +0000 (16:43 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 5 Jun 2018 06:13:59 +0000 (08:13 +0200)
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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/qemu/qemu_monitor.c
src/qemu/qemu_monitor.h
src/qemu/qemu_monitor_json.c
src/qemu/qemu_monitor_json.h

index 77ad47cb7467a3ee4e1097d513259a4ee934bc29..fe8fdfaf9ae8bdab8dcaac84cfc8c51f91ac49e2 100644 (file)
@@ -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;
 }
 
 
index 77a26d4a8a6075c820ff97496611c9a888c259d2..0c133915204fa3295ac585fa440c2dc691176a61 100644 (file)
@@ -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,
index 9f5c35879587ca1e2f9949bbd830a91f11f91be9..7522eaeef0c01abde12493f60798c3ab295d0b2a 100644 (file)
@@ -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;
 }
 
index f4ac8319ac8aee61efb5f93afebadc2d9e9b444d..5fc51b1d6b1544fa3e3e7f995059fef46f61e35c 100644 (file)
@@ -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,