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.
This commit is contained in:
Erik Skultety 2015-10-14 15:10:02 +02:00
parent 6409578790
commit 47a089f06c
4 changed files with 37 additions and 2 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -16,4 +16,5 @@ LIBVIRT_ADMIN_1.3.0 {
virAdmConnectClose;
virAdmConnectRef;
virAdmGetVersion;
virAdmConnectIsAlive;
};

View File

@ -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;
}