diff --git a/tools/virsh.c b/tools/virsh.c index 366956cd5c75fc722899e4621d0df7d28d5da63a..af072510870de3cbfd5cf26fe1dc3a5f81d5b8d5 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -292,6 +292,17 @@ cmdConnect(vshControl *ctl, const vshCmd *cmd) bool ro = vshCommandOptBool(cmd, "readonly"); const char *name = NULL; virshControlPtr priv = ctl->privData; + virConnectPtr conn; + + if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0) + return false; + + conn = virshConnect(ctl, name, ro); + + if (!conn) { + vshError(ctl, "%s", _("Failed to connect to the hypervisor")); + return false; + } if (priv->conn) { int ret; @@ -303,13 +314,10 @@ cmdConnect(vshControl *ctl, const vshCmd *cmd) else if (ret > 0) vshError(ctl, "%s", _("One or more references were leaked after " "disconnect from the hypervisor")); - priv->conn = NULL; } + priv->conn = conn; VIR_FREE(ctl->connname); - if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0) - return false; - ctl->connname = vshStrdup(ctl, name); priv->useGetInfo = false; @@ -317,13 +325,6 @@ cmdConnect(vshControl *ctl, const vshCmd *cmd) priv->blockJobNoBytes = false; priv->readonly = ro; - priv->conn = virshConnect(ctl, ctl->connname, priv->readonly); - - if (!priv->conn) { - vshError(ctl, "%s", _("Failed to connect to the hypervisor")); - return false; - } - if (virConnectRegisterCloseCallback(priv->conn, virshCatchDisconnect, ctl, NULL) < 0) vshError(ctl, "%s", _("Unable to register disconnect callback"));