From patchwork Wed Apr 20 18:19:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cole Robinson X-Patchwork-Id: 66244 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp2604896qge; Wed, 20 Apr 2016 11:21:48 -0700 (PDT) X-Received: by 10.140.30.247 with SMTP id d110mr12817578qgd.43.1461176508896; Wed, 20 Apr 2016 11:21:48 -0700 (PDT) Return-Path: Received: from mx3-phx2.redhat.com (mx3-phx2.redhat.com. [209.132.183.24]) by mx.google.com with ESMTPS id v67si3776160qkc.283.2016.04.20.11.21.48 (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 20 Apr 2016 11:21:48 -0700 (PDT) Received-SPF: pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.24 as permitted sender) client-ip=209.132.183.24; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.24 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u3KIJCp7006276; Wed, 20 Apr 2016 14:19:12 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u3KIJBcG029512 for ; Wed, 20 Apr 2016 14:19:11 -0400 Received: from colepc.redhat.com (ovpn-113-99.phx2.redhat.com [10.3.113.99]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3KIJAQX024122; Wed, 20 Apr 2016 14:19:10 -0400 From: Cole Robinson To: libvirt-list@redhat.com Date: Wed, 20 Apr 2016 14:19:06 -0400 Message-Id: <41fa02d98d77bb8a72d23321faf4bbc644aa1b81.1461176290.git.crobinso@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] virsh: Don't clear old connection if 'connect $uri' fails X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com virsh # list --all Id Name State ---------------------------------------------------- 1 test running virsh # connect frob error: Failed to connect to the hypervisor error: no connection driver available for frob virsh # list --all error: failed to connect to the hypervisor error: no valid connection error: no connection driver available for frob Seems sensible IMO to just not clear out the old connection state until the new virConnectOpen succeeds. https://bugzilla.redhat.com/show_bug.cgi?id=829160 --- There was an older discussion here: https://www.redhat.com/archives/libvir-list/2012-June/msg00270.html That seemed to go off into the weeds a bit, talking about caching old URIs... This approach seems fine for me to address the bug report but maybe I missed something tools/virsh.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) -- 2.7.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list diff --git a/tools/virsh.c b/tools/virsh.c index 0d8ec5c..353a5d8 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"));