1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

virsh: Report error when explicit connection fails

Commit 0c56d9431839 forgot to return false in the cmdConnect command
after the clean up made there.

Before (assuming you don't have uri alias for 'asdf'):
  $ virsh connect asdf
  error: failed to connect to the hypervisor

  $ echo $?
  0

After (with the same assumption):
  $ virsh connect asdf
  error: failed to connect to the hypervisor
  error: no connection driver available for asdf

  $ echo $?
  1

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1356461

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Martin Kletzander 2016-07-29 08:09:22 +02:00
parent a2b97a8d91
commit ff498a9ac7

View File

@ -210,7 +210,7 @@ virshConnect(vshControl *ctl, const char *uri, bool readonly)
* Reconnect after a disconnect from libvirtd * Reconnect after a disconnect from libvirtd
* *
*/ */
static void static int
virshReconnect(vshControl *ctl, const char *name, bool readonly, bool force) virshReconnect(vshControl *ctl, const char *name, bool readonly, bool force)
{ {
bool connected = false; bool connected = false;
@ -237,6 +237,7 @@ virshReconnect(vshControl *ctl, const char *name, bool readonly, bool force)
vshError(ctl, "%s", _("Failed to reconnect to the hypervisor")); vshError(ctl, "%s", _("Failed to reconnect to the hypervisor"));
else else
vshError(ctl, "%s", _("failed to connect to the hypervisor")); vshError(ctl, "%s", _("failed to connect to the hypervisor"));
return -1;
} else { } else {
if (name) { if (name) {
VIR_FREE(ctl->connname); VIR_FREE(ctl->connname);
@ -253,6 +254,7 @@ virshReconnect(vshControl *ctl, const char *name, bool readonly, bool force)
priv->useGetInfo = false; priv->useGetInfo = false;
priv->useSnapshotOld = false; priv->useSnapshotOld = false;
priv->blockJobNoBytes = false; priv->blockJobNoBytes = false;
return 0;
} }
int virshStreamSink(virStreamPtr st ATTRIBUTE_UNUSED, int virshStreamSink(virStreamPtr st ATTRIBUTE_UNUSED,
@ -301,7 +303,8 @@ cmdConnect(vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0) if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0)
return false; return false;
virshReconnect(ctl, name, ro, true); if (virshReconnect(ctl, name, ro, true) < 0)
return false;
return true; return true;
} }
@ -333,11 +336,13 @@ virshConnectionHandler(vshControl *ctl)
{ {
virshControlPtr priv = ctl->privData; virshControlPtr priv = ctl->privData;
if (!priv->conn || disconnected) if ((!priv->conn || disconnected) &&
virshReconnect(ctl, NULL, false, false); virshReconnect(ctl, NULL, false, false) < 0)
return NULL;
if (virshConnectionUsability(ctl, priv->conn)) if (virshConnectionUsability(ctl, priv->conn))
return priv->conn; return priv->conn;
return NULL; return NULL;
} }
@ -444,14 +449,13 @@ virshInit(vshControl *ctl)
return false; return false;
if (ctl->connname) { if (ctl->connname) {
virshReconnect(ctl, NULL, false, false);
/* Connecting to a named connection must succeed, but we delay /* Connecting to a named connection must succeed, but we delay
* connecting to the default connection until we need it * connecting to the default connection until we need it
* (since the first command might be 'connect' which allows a * (since the first command might be 'connect' which allows a
* non-default connection, or might be 'help' which needs no * non-default connection, or might be 'help' which needs no
* connection). * connection).
*/ */
if (!priv->conn) { if (virshReconnect(ctl, NULL, false, false) < 0) {
vshReportError(ctl); vshReportError(ctl);
return false; return false;
} }