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 | 29 +++++++++++++++++++++++++++++
src/libvirt_admin_public.syms | 1 +
tools/virt-admin.c | 4 ++--
4 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/include/libvirt/libvirt-admin.h b/include/libvirt/libvirt-admin.h
index 9997cc2..72671c6 100644
--- a/include/libvirt/libvirt-admin.h
+++ b/include/libvirt/libvirt-admin.h
@@ -52,6 +52,7 @@ virAdmConnectPtr virAdmConnectOpen(const char *name, unsigned int
flags);
int virAdmConnectClose(virAdmConnectPtr conn);
int virAdmConnectRef(virAdmConnectPtr conn);
+int virAdmConnectIsAlive(virAdmConnectPtr conn);
# ifdef __cplusplus
}
diff --git a/src/libvirt-admin.c b/src/libvirt-admin.c
index 5a4fc48..105727f 100644
--- a/src/libvirt-admin.c
+++ b/src/libvirt-admin.c
@@ -386,3 +386,32 @@ virAdmConnectRef(virAdmConnectPtr conn)
return 0;
}
+
+/**
+ * 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 the channel has already
+ * been closed or -1 on error.
+ */
+int
+virAdmConnectIsAlive(virAdmConnectPtr conn)
+{
+ bool ret;
+ remoteAdminPrivPtr priv = conn->privateData;
+
+ VIR_DEBUG("conn=%p", conn);
+
+ virResetLastError();
+ virCheckAdmConnectReturn(conn, -1);
+
+ 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 d9e3c0b..16cfd42 100644
--- a/src/libvirt_admin_public.syms
+++ b/src/libvirt_admin_public.syms
@@ -15,4 +15,5 @@ LIBVIRT_ADMIN_1.3.0 {
virAdmConnectOpen;
virAdmConnectClose;
virAdmConnectRef;
+ virAdmConnectIsAlive;
};
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index ddfba91..6b009ea 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 (!priv->conn || !virAdmConnectIsAlive(priv->conn))
vshAdmReconnect(ctl);
- if (!priv->conn) {
+ if (!priv->conn || !virAdmConnectIsAlive(priv->conn)) {
vshError(ctl, "%s", _("no valid connection"));
return NULL;
}
--
2.4.3