[virt-tools-list] [virt-viewer][PATCH v2] Reconnect to libvirtd after connection breaks

Michal Privoznik mprivozn at redhat.com
Tue Nov 13 10:57:31 UTC 2012


Currently, if user wants to reconnect to a domain he can use
'-r' cmd line argument. This makes virt-viewer listen to
domain events. However, if connection to libvirtd breaks
somehow, we will receive no longer any event. Hence we must
reconnect to the libvirt.
---

diff to v1:
-clean the patch up a bit (drop debug leftovers)

 AUTHORS               |    1 +
 configure.ac          |    2 +-
 src/virt-viewer-app.c |    2 +
 src/virt-viewer.c     |   61 ++++++++++++++++++++++++++++++++++++++++++------
 4 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/AUTHORS b/AUTHORS
index a559205..69718a7 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -23,6 +23,7 @@ With additional patches from:
     Yann E. MORIN <yann.morin.1998 at anciens.enib.fr>
     Yonit Halperin <yhalperi at redhat.com>
     Zeeshan Ali (Khattak) <zeeshanak at gnome.org>
+    Michal Privoznik <mprivozn at redhat.com>
 
    ...send patches to get your name here...
 
diff --git a/configure.ac b/configure.ac
index f72e615..8dc90c3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -14,7 +14,7 @@ AM_SILENT_RULES([yes])
 
 GLIB2_REQUIRED=2.22.0
 LIBXML2_REQUIRED="2.6.0"
-LIBVIRT_REQUIRED="0.9.7"
+LIBVIRT_REQUIRED="0.10.0"
 GTK2_REQUIRED="2.18.0"
 GTK3_REQUIRED="3.0"
 GTK_VNC1_REQUIRED="0.3.8"
diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
index 7dcc4c1..50a3a2d 100644
--- a/src/virt-viewer-app.c
+++ b/src/virt-viewer-app.c
@@ -1010,6 +1010,8 @@ virt_viewer_app_start_reconnect_poll(VirtViewerApp *self)
     g_return_if_fail(VIRT_VIEWER_IS_APP(self));
     VirtViewerAppPrivate *priv = self->priv;
 
+    DEBUG_LOG("reconnect_poll: %d", priv->reconnect_poll);
+
     if (priv->reconnect_poll != 0)
         return;
 
diff --git a/src/virt-viewer.c b/src/virt-viewer.c
index bada5a9..00d4965 100644
--- a/src/virt-viewer.c
+++ b/src/virt-viewer.c
@@ -481,6 +481,25 @@ virt_viewer_domain_event(virConnectPtr conn G_GNUC_UNUSED,
     return 0;
 }
 
+static void
+virt_viewer_conn_event(virConnectPtr conn G_GNUC_UNUSED,
+                       int reason,
+                       void *opaque)
+{
+    VirtViewer *self = opaque;
+    VirtViewerApp *app = VIRT_VIEWER_APP(self);
+    VirtViewerPrivate *priv = self->priv;
+
+    DEBUG_LOG("Got connection event %d", reason);
+
+    virConnectClose(priv->conn);
+    priv->conn = NULL;
+
+    virt_viewer_app_start_reconnect_poll(app);
+}
+
+static int virt_viewer_connect(VirtViewerApp *app);
+
 static int
 virt_viewer_initial_connect(VirtViewerApp *app)
 {
@@ -490,6 +509,15 @@ virt_viewer_initial_connect(VirtViewerApp *app)
     VirtViewer *self = VIRT_VIEWER(app);
     VirtViewerPrivate *priv = self->priv;
 
+
+    DEBUG_LOG("initial connect");
+
+    if (!priv->conn &&
+        virt_viewer_connect(app) < 0) {
+        virt_viewer_app_show_status(app, _("Waiting for libvirt to start"));
+        goto done;
+    }
+
     virt_viewer_app_show_status(app, _("Finding guest domain"));
     dom = virt_viewer_lookup_domain(self);
     if (!dom) {
@@ -610,9 +638,8 @@ virt_viewer_auth_libvirt_credentials(virConnectCredentialPtr cred,
     return ret;
 }
 
-
-static gboolean
-virt_viewer_start(VirtViewerApp *app)
+static int
+virt_viewer_connect(VirtViewerApp *app)
 {
     VirtViewer *self = VIRT_VIEWER(app);
     VirtViewerPrivate *priv = self->priv;
@@ -629,9 +656,7 @@ virt_viewer_start(VirtViewerApp *app)
     if (!virt_viewer_app_get_attach(app))
         oflags |= VIR_CONNECT_RO;
 
-    virt_viewer_events_register();
-
-    virSetErrorFunc(NULL, virt_viewer_error_func);
+    DEBUG_LOG("connecting ...");
 
     virt_viewer_app_trace(app, "Opening connection to libvirt with URI %s",
                           priv->uri ? priv->uri : "<null>");
@@ -642,11 +667,11 @@ virt_viewer_start(VirtViewerApp *app)
     if (!priv->conn) {
         virt_viewer_app_simple_message_dialog(app, _("Unable to connect to libvirt with URI %s"),
                                               priv->uri ? priv->uri : _("[none]"));
-        return FALSE;
+        return -1;
     }
 
     if (virt_viewer_app_initial_connect(app) < 0)
-        return FALSE;
+        return -1;
 
     if (virConnectDomainEventRegister(priv->conn,
                                       virt_viewer_domain_event,
@@ -662,6 +687,26 @@ virt_viewer_start(VirtViewerApp *app)
         virt_viewer_app_start_reconnect_poll(app);
     }
 
+    if (virConnectRegisterCloseCallback(priv->conn,
+                                        virt_viewer_conn_event,
+                                        self,
+                                        NULL) < 0) {
+        DEBUG_LOG("Unable to register close callback on libvirt connection");
+    }
+
+    return 0;
+}
+
+static gboolean
+virt_viewer_start(VirtViewerApp *app)
+{
+    virt_viewer_events_register();
+
+    virSetErrorFunc(NULL, virt_viewer_error_func);
+
+    if (virt_viewer_connect(app) < 0)
+        return FALSE;
+
     return VIRT_VIEWER_APP_CLASS(virt_viewer_parent_class)->start(app);
 }
 
-- 
1.7.8.6




More information about the virt-tools-list mailing list