]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
qemu_monitor_json: Move InetSocketAddress build to a separate function
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 12 Mar 2013 18:18:22 +0000 (19:18 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 12 Jul 2013 08:59:57 +0000 (10:59 +0200)
Currently, we are building InetSocketAddress qemu json type
within the qemuMonitorJSONNBDServerStart function. However, other
future functions may profit from the code as well. So it should
be moved into a static function.

src/qemu/qemu_monitor_json.c

index 63e890a73c93ed321c02f9ff194e6fbf742fc54e..bff46a5b664bdeec3f819879b568f4f2a6e74b38 100644 (file)
@@ -4741,6 +4741,31 @@ cleanup:
     return ret;
 }
 
+static virJSONValuePtr
+qemuMonitorJSONBuildInetSocketAddress(const char *host,
+                                      const char *port)
+{
+    virJSONValuePtr addr = NULL;
+    virJSONValuePtr data = NULL;
+
+    if (!(data = virJSONValueNewObject()) ||
+        !(addr = virJSONValueNewObject()))
+        goto error;
+
+    /* port is really expected as a string here by qemu */
+    if (virJSONValueObjectAppendString(data, "host", host) < 0 ||
+        virJSONValueObjectAppendString(data, "port", port) < 0 ||
+        virJSONValueObjectAppendString(addr, "type", "inet") < 0 ||
+        virJSONValueObjectAppend(addr, "data", data) < 0)
+        goto error;
+
+    return addr;
+error:
+    virJSONValueFree(data);
+    virJSONValueFree(addr);
+    return NULL;
+}
+
 int
 qemuMonitorJSONNBDServerStart(qemuMonitorPtr mon,
                               const char *host,
@@ -4749,24 +4774,14 @@ qemuMonitorJSONNBDServerStart(qemuMonitorPtr mon,
     int ret = -1;
     virJSONValuePtr cmd = NULL;
     virJSONValuePtr reply = NULL;
-    virJSONValuePtr data = NULL;
     virJSONValuePtr addr = NULL;
     char *port_str = NULL;
 
-    if (!(data = virJSONValueNewObject()) ||
-        !(addr = virJSONValueNewObject()) ||
-        (virAsprintf(&port_str, "%u", port) < 0))
-        goto cleanup;
-
-    /* port is really expected as a string here by qemu */
-    if (virJSONValueObjectAppendString(data, "host", host) < 0 ||
-        virJSONValueObjectAppendString(data, "port", port_str) < 0 ||
-        virJSONValueObjectAppendString(addr, "type", "inet") < 0 ||
-        virJSONValueObjectAppend(addr, "data", data) < 0)
-        goto cleanup;
+    if (virAsprintf(&port_str, "%u", port) < 0)
+        return ret;
 
-    /* From now on, @data is part of @addr */
-    data = NULL;
+    if (!(addr = qemuMonitorJSONBuildInetSocketAddress(host, port_str)))
+        return ret;
 
     if (!(cmd = qemuMonitorJSONMakeCommand("nbd-server-start",
                                            "a:addr", addr,
@@ -4789,7 +4804,6 @@ cleanup:
     virJSONValueFree(reply);
     virJSONValueFree(cmd);
     virJSONValueFree(addr);
-    virJSONValueFree(data);
     return ret;
 }