[PATCH 1/2] virsh: Improve checking for connection when running commands

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Almost each virsh command uses the function vshConnectionUsability
before doing anything, to check if the connection is "alive".  Commands
that don't need an conection are already conveniently marked with
VSH_CMD_FLAG_NOCONNECT. We can automaticaly check for the connection
before calling any remote command so we don't forget to do so.

This patch also upgrades the connection check to use virConnectIsAlive
along with the current approach.
---
 tools/virsh.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 7a5b92c..9c96685 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -1481,13 +1481,19 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd)
             !(cmd->def->flags & VSH_CMD_FLAG_NOCONNECT))
             vshReconnect(ctl);

-        if (enable_timing)
-            GETTIMEOFDAY(&before);
+        if ((cmd->def->flags & VSH_CMD_FLAG_NOCONNECT) ||
+            vshConnectionUsability(ctl, ctl->conn)) {
+            if (enable_timing)
+                GETTIMEOFDAY(&before);

-        ret = cmd->def->handler(ctl, cmd);
+            ret = cmd->def->handler(ctl, cmd);

-        if (enable_timing)
-            GETTIMEOFDAY(&after);
+            if (enable_timing)
+                GETTIMEOFDAY(&after);
+        } else {
+            /* connection is not usable, return error */
+            ret = false;
+        }

         /* try to automatically catch disconnections */
         if (!ret &&
@@ -1948,13 +1954,17 @@ vshFindTypedParamByName(const char *name, virTypedParameterPtr list, int count)
 bool
 vshConnectionUsability(vshControl *ctl, virConnectPtr conn)
 {
-    /* TODO: use something like virConnectionState() to
-     *       check usability of the connection
-     */
-    if (!conn) {
+    if (!conn ||
+        virConnectIsAlive(conn) == 0) {
         vshError(ctl, "%s", _("no valid connection"));
         return false;
     }
+
+    /* The connection is considered dead only if
+     * virConnectIsAlive() successfuly says so.
+     */
+    vshResetLibvirtError();
+
     return true;
 }

-- 
1.7.12

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]