From 47a089f06c569d2a24eee239e92ab9966f06b1a7 Mon Sep 17 00:00:00 2001
From: Erik Skultety <eskultet@redhat.com>
Date: Wed, 14 Oct 2015 15:10:02 +0200
Subject: [PATCH] admin: Introduce virAdmConnectIsAlive

Since most of our APIs rely on an acive functional connection to a daemon and
we have such a mechanism in libvirt already, there's need to have such a way in
libvirt-admin as well. By introducing a new public API, this patch provides
support to check for an active connection.
---
 include/libvirt/libvirt-admin.h |  1 +
 src/libvirt-admin.c             | 33 +++++++++++++++++++++++++++++++++
 src/libvirt_admin_public.syms   |  1 +
 tools/virt-admin.c              |  4 ++--
 4 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/include/libvirt/libvirt-admin.h b/include/libvirt/libvirt-admin.h
index 9f012c1ad9..411bd891bf 100644
--- a/include/libvirt/libvirt-admin.h
+++ b/include/libvirt/libvirt-admin.h
@@ -55,6 +55,7 @@ virAdmConnectPtr virAdmConnectOpen(const char *name, unsigned int flags);
 int virAdmConnectClose(virAdmConnectPtr conn);
 
 int virAdmConnectRef(virAdmConnectPtr conn);
+int virAdmConnectIsAlive(virAdmConnectPtr conn);
 
 int virAdmGetVersion(unsigned long long *libVer);
 
diff --git a/src/libvirt-admin.c b/src/libvirt-admin.c
index 4a2f002ffc..eedbd12668 100644
--- a/src/libvirt-admin.c
+++ b/src/libvirt-admin.c
@@ -420,3 +420,36 @@ virAdmGetVersion(unsigned long long *libVer)
     virDispatchError(NULL);
     return -1;
 }
+
+/**
+ * virAdmConnectIsAlive:
+ * @conn: connection to admin server
+ *
+ * Decide whether the connection to the admin server is alive or not.
+ * Connection is considered alive if the channel it is running over is not
+ * closed.
+ *
+ * Returns 1, if the connection is alive, 0 if there isn't an existing
+ * connection at all or the channel has already been closed, or -1 on error.
+ */
+int
+virAdmConnectIsAlive(virAdmConnectPtr conn)
+{
+    bool ret;
+    remoteAdminPrivPtr priv = NULL;
+
+    VIR_DEBUG("conn=%p", conn);
+
+    if (!conn)
+        return 0;
+
+    virCheckAdmConnectReturn(conn, -1);
+    virResetLastError();
+
+    priv = conn->privateData;
+    virObjectLock(priv);
+    ret = virNetClientIsOpen(priv->client);
+    virObjectUnlock(priv);
+
+    return ret;
+}
diff --git a/src/libvirt_admin_public.syms b/src/libvirt_admin_public.syms
index 5e774c2c58..0a70bc7f4c 100644
--- a/src/libvirt_admin_public.syms
+++ b/src/libvirt_admin_public.syms
@@ -16,4 +16,5 @@ LIBVIRT_ADMIN_1.3.0 {
         virAdmConnectClose;
         virAdmConnectRef;
         virAdmGetVersion;
+        virAdmConnectIsAlive;
 };
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index f36d2eddce..2cfa1b1e00 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -159,10 +159,10 @@ vshAdmConnectionHandler(vshControl *ctl)
 {
     vshAdmControlPtr priv = ctl->privData;
 
-    if (!priv->conn)
+    if (!virAdmConnectIsAlive(priv->conn))
         vshAdmReconnect(ctl);
 
-    if (!priv->conn) {
+    if (!virAdmConnectIsAlive(priv->conn)) {
         vshError(ctl, "%s", _("no valid connection"));
         return NULL;
     }
-- 
GitLab