]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Use virXPath wrappers for vncdisplay cmd
authorDoug Goldstein <cardoe@cardoe.com>
Sun, 24 Jun 2012 21:36:00 +0000 (16:36 -0500)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 25 Jun 2012 09:25:50 +0000 (11:25 +0200)
Update the vncdisplay command to use the virXPath wrappers as well as
check if the domain is up rather than using the port set to -1 to mean
the domain is not up.

Signed-off-by: Doug Goldstein <cardoe@cardoe.com>
tools/virsh.c

index 035482269b59213e9f507b9d2dc909489f3000f3..75147efd62127dc510ad69d3ec16662d11bfc8d6 100644 (file)
@@ -13844,12 +13844,12 @@ static bool
 cmdVNCDisplay(vshControl *ctl, const vshCmd *cmd)
 {
     xmlDocPtr xml = NULL;
-    xmlXPathObjectPtr obj = NULL;
     xmlXPathContextPtr ctxt = NULL;
     virDomainPtr dom;
     bool ret = false;
     int port = 0;
-    char *doc;
+    char *doc = NULL;
+    char *listen_addr = NULL;
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         return false;
@@ -13857,38 +13857,37 @@ cmdVNCDisplay(vshControl *ctl, const vshCmd *cmd)
     if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
         return false;
 
-    doc = virDomainGetXMLDesc(dom, 0);
-    if (!doc)
+    /* Check if the domain is active and don't rely on -1 for this */
+    if (!virDomainIsActive(dom)) {
+        vshError(ctl, _("Domain is not running"));
         goto cleanup;
+    }
 
-    xml = virXMLParseStringCtxt(doc, _("(domain_definition)"), &ctxt);
-    VIR_FREE(doc);
-    if (!xml)
+    if (!(doc = virDomainGetXMLDesc(dom, 0)))
         goto cleanup;
 
-    obj = xmlXPathEval(BAD_CAST "string(/domain/devices/graphics[@type='vnc']/@port)", ctxt);
-    if (obj == NULL || obj->type != XPATH_STRING ||
-        obj->stringval == NULL || obj->stringval[0] == 0) {
+    if (!(xml = virXMLParseStringCtxt(doc, _("(domain_definition)"), &ctxt)))
         goto cleanup;
-    }
-    if (virStrToLong_i((const char *)obj->stringval, NULL, 10, &port) || port < 0)
+
+    /* Get the VNC port */
+    if (virXPathInt("string(/domain/devices/graphics[@type='vnc']/@port)",
+                    ctxt, &port)) {
+        vshError(ctl, _("Failed to get VNC port. Is this domain using VNC?"));
         goto cleanup;
-    xmlXPathFreeObject(obj);
+    }
 
-    obj = xmlXPathEval(BAD_CAST "string(/domain/devices/graphics[@type='vnc']/@listen)", ctxt);
-    if (obj == NULL || obj->type != XPATH_STRING ||
-        obj->stringval == NULL || obj->stringval[0] == 0 ||
-        STREQ((const char*)obj->stringval, "0.0.0.0")) {
+    listen_addr = virXPathString("string(/domain/devices/graphics"
+                                 "[@type='vnc']/@listen)", ctxt);
+    if (listen_addr == NULL || STREQ(listen_addr, "0.0.0.0"))
         vshPrint(ctl, ":%d\n", port-5900);
-    } else {
-        vshPrint(ctl, "%s:%d\n", (const char *)obj->stringval, port-5900);
-    }
-    xmlXPathFreeObject(obj);
-    obj = NULL;
+    else
+        vshPrint(ctl, "%s:%d\n", listen_addr, port-5900);
+
     ret = true;
 
  cleanup:
-    xmlXPathFreeObject(obj);
+    VIR_FREE(doc);
+    VIR_FREE(listen_addr);
     xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     virDomainFree(dom);