[virt-tools-list] [PATCH] Switch to use libvirt-glib for events

Daniel P. Berrange berrange at redhat.com
Wed Jan 29 16:19:49 UTC 2014


Rather than using a custom written glib <-> libvirt event
loop implementation, make use of libvirt-glib. This will
solve a number of bugs in the current impl.

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 README                    |   4 +-
 configure.ac              |  16 +-
 mingw-virt-viewer.spec.in |   2 +
 src/Makefile.am           |   3 +-
 src/virt-viewer-events.c  | 364 ----------------------------------------------
 src/virt-viewer-events.h  |  37 -----
 src/virt-viewer-main.c    |   3 +
 src/virt-viewer.c         |   4 +-
 virt-viewer.spec.in       |   1 +
 9 files changed, 26 insertions(+), 408 deletions(-)
 delete mode 100644 src/virt-viewer-events.c
 delete mode 100644 src/virt-viewer-events.h

diff --git a/README b/README
index 8170b59..000e872 100644
--- a/README
+++ b/README
@@ -28,8 +28,8 @@ Use of either SPICE-GTK or GTK-VNC can be disabled at time
 of configure, with --without-gtk-vnc or --without-spice-gtk
 respectively.
 
-Virt Viewer uses libvirt to lookup information about the
-guest OS display. This is available from
+Virt Viewer uses libvirt and libvirt-glib to lookup information
+about the guest OS display. These are available from
 
   http://libvirt.org/
 
diff --git a/configure.ac b/configure.ac
index 7a00bf4..56c3539 100644
--- a/configure.ac
+++ b/configure.ac
@@ -15,6 +15,7 @@ AM_SILENT_RULES([yes])
 GLIB2_REQUIRED=2.22.0
 LIBXML2_REQUIRED="2.6.0"
 LIBVIRT_REQUIRED="0.10.0"
+LIBVIRT_GLIB_REQUIRED="0.1.0"
 GTK2_REQUIRED="2.18.0"
 GTK3_REQUIRED="3.0"
 GTK_VNC1_REQUIRED="0.3.8"
@@ -26,6 +27,7 @@ GOVIRT_REQUIRED="0.3.0"
 AC_SUBST([GLIB2_REQUIRED])
 AC_SUBST([LIBXML2_REQUIRED])
 AC_SUBST([LIBVIRT_REQUIRED])
+AC_SUBST([LIBVIRT_GLIB_REQUIRED])
 AC_SUBST([GTK2_REQUIRED])
 AC_SUBST([GTK3_REQUIRED])
 AC_SUBST([GTK_VNC1_REQUIRED])
@@ -105,8 +107,16 @@ AC_ARG_WITH([libvirt],
 
 AS_IF([test "x$with_libvirt" != "xno"],
       [PKG_CHECK_MODULES(LIBVIRT,
-                         [libvirt >= $LIBVIRT_REQUIRED],
-                         [have_libvirt=yes], [have_libvirt=no])],
+         [libvirt >= $LIBVIRT_REQUIRED],
+         [
+           PKG_CHECK_MODULES(LIBVIRT_GLIB,
+             [libvirt-glib-1.0 >= $LIBVIRT_GLIB_REQUIRED],
+             [have_libvirt=yes],
+             [have_libvirt=no
+              LIBVIRT_CFLAGS=
+              LIBVIRT_LIBS=])
+         ],
+         [have_libvirt=no])],
       [have_libvirt=no])
 
 AS_IF([test "x$have_libvirt" = "xyes"],
@@ -282,5 +292,7 @@ AC_MSG_NOTICE([     LIBXML2: $LIBXML2_CFLAGS $LIBXML2_LIBS])
 AC_MSG_NOTICE([])
 AC_MSG_NOTICE([     LIBVIRT: $LIBVIRT_CFLAGS $LIBVIRT_LIBS])
 AC_MSG_NOTICE([])
+AC_MSG_NOTICE([LIBVIRT-GLIB: $LIBVIRT_GLIB_CFLAGS $LIBVIRT_GLIB_LIBS])
+AC_MSG_NOTICE([])
 AC_MSG_NOTICE([       OVIRT: $OVIRT_CFLAGS $OVIRT_LIBS])
 AC_MSG_NOTICE([])
diff --git a/mingw-virt-viewer.spec.in b/mingw-virt-viewer.spec.in
index 01f8575..1151749 100644
--- a/mingw-virt-viewer.spec.in
+++ b/mingw-virt-viewer.spec.in
@@ -24,6 +24,8 @@ BuildRequires:  mingw32-libusbx
 BuildRequires:  mingw64-libusbx
 BuildRequires:  mingw32-libvirt >= @LIBVIRT_REQUIRED@
 BuildRequires:  mingw64-libvirt >= @LIBVIRT_REQUIRED@
+BuildRequires:  mingw32-libvirt-glib >= @LIBVIRT_GLIB_REQUIRED@
+BuildRequires:  mingw64-libvirt-glib >= @LIBVIRT_GLIB_REQUIRED@
 BuildRequires:  mingw32-libxml2 >= @LIBXML2_REQUIRED@
 BuildRequires:  mingw64-libxml2 >= @LIBXML2_REQUIRED@
 BuildRequires:  mingw32-gtk-vnc >= @GTK_VNC1_REQUIRED@
diff --git a/src/Makefile.am b/src/Makefile.am
index b3a9637..15d1686 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -72,7 +72,6 @@ if HAVE_LIBVIRT
 bin_PROGRAMS += virt-viewer
 virt_viewer_SOURCES =					\
 	$(COMMON_SOURCES)				\
-	virt-viewer-events.h virt-viewer-events.c	\
 	virt-viewer.h virt-viewer.c			\
 	virt-viewer-main.c				\
 	$(NULL)
@@ -83,6 +82,7 @@ virt_viewer_LDFLAGS =				\
 	$(GTK_VNC_LIBS)				\
 	$(LIBXML2_LIBS)				\
 	$(LIBVIRT_LIBS)				\
+	$(LIBVIRT_GLIB_LIBS)			\
 	$(OVIRT_LIBS)				\
 	$(SPICE_GTK_LIBS)			\
 	$(NULL)
@@ -94,6 +94,7 @@ virt_viewer_CFLAGS = 				\
 	$(GTK_VNC_CFLAGS)			\
 	$(LIBXML2_CFLAGS)			\
 	$(LIBVIRT_CFLAGS)			\
+	$(LIBVIRT_GLIB_CFLAGS)			\
 	$(OVIRT_CFLAGS)				\
 	$(SPICE_GTK_CFLAGS)			\
 	$(SPICE_CONTROLLER_CFLAGS)		\
diff --git a/src/virt-viewer-events.c b/src/virt-viewer-events.c
deleted file mode 100644
index 6d7fe9e..0000000
--- a/src/virt-viewer-events.c
+++ /dev/null
@@ -1,364 +0,0 @@
-/*
- * events.c: event loop integration
- *
- * Copyright (C) 2008-2012 Daniel P. Berrange
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
- *
- * Author: Daniel P. Berrange <berrange at redhat.com>
- */
-
-#include <config.h>
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <glib.h>
-#ifdef G_OS_WIN32
-#include <io.h>
-#endif
-#include <libvirt/libvirt.h>
-
-#include "virt-viewer-events.h"
-
-struct virt_viewer_events_handle
-{
-    int watch;
-    int fd;
-    int events;
-    int enabled;
-    GIOChannel *channel;
-    guint source;
-    virEventHandleCallback cb;
-    void *opaque;
-    virFreeCallback ff;
-};
-
-static int nextwatch = 1;
-static unsigned int nhandles = 0;
-static struct virt_viewer_events_handle **handles = NULL;
-
-static gboolean
-virt_viewer_events_dispatch_handle(GIOChannel *source G_GNUC_UNUSED,
-                                   GIOCondition condition,
-                                   gpointer opaque)
-{
-    struct virt_viewer_events_handle *data = opaque;
-    int events = 0;
-
-    if (condition & G_IO_IN)
-        events |= VIR_EVENT_HANDLE_READABLE;
-    if (condition & G_IO_OUT)
-        events |= VIR_EVENT_HANDLE_WRITABLE;
-    if (condition & G_IO_HUP)
-        events |= VIR_EVENT_HANDLE_HANGUP;
-    if (condition & G_IO_ERR)
-        events |= VIR_EVENT_HANDLE_ERROR;
-
-    DEBUG_LOG("Dispatch handler %d %d %p", data->fd, events, data->opaque);
-
-    (data->cb)(data->watch, data->fd, events, data->opaque);
-
-    return TRUE;
-}
-
-
-static
-int virt_viewer_events_add_handle(int fd,
-                                  int events,
-                                  virEventHandleCallback cb,
-                                  void *opaque,
-                                  virFreeCallback ff)
-{
-    struct virt_viewer_events_handle *data;
-    GIOCondition cond = 0;
-
-    handles = g_realloc(handles, sizeof(*handles)*(nhandles+1));
-    data = g_malloc(sizeof(*data));
-    memset(data, 0, sizeof(*data));
-
-    if (events & VIR_EVENT_HANDLE_READABLE)
-        cond |= G_IO_IN;
-    if (events & VIR_EVENT_HANDLE_WRITABLE)
-        cond |= G_IO_OUT;
-
-    data->watch = nextwatch++;
-    data->fd = fd;
-    data->events = events;
-    data->cb = cb;
-    data->opaque = opaque;
-#ifdef G_OS_WIN32
-    DEBUG_LOG("Converted fd %d to handle %d", fd, _get_osfhandle(fd));
-    data->channel = g_io_channel_win32_new_socket(_get_osfhandle(fd));
-#else
-    data->channel = g_io_channel_unix_new(fd);
-#endif
-    data->ff = ff;
-
-    DEBUG_LOG("Add handle %d %d %p", data->fd, events, data->opaque);
-
-    data->source = g_io_add_watch(data->channel,
-                                  cond,
-                                  virt_viewer_events_dispatch_handle,
-                                  data);
-
-    handles[nhandles++] = data;
-
-    return data->watch;
-}
-
-static struct virt_viewer_events_handle *
-virt_viewer_events_find_handle(int watch)
-{
-    unsigned int i;
-    for (i = 0 ; i < nhandles ; i++)
-        if (handles[i]->watch == watch)
-            return handles[i];
-
-    return NULL;
-}
-
-static void
-virt_viewer_events_update_handle(int watch,
-                                 int events)
-{
-    struct virt_viewer_events_handle *data = virt_viewer_events_find_handle(watch);
-
-    if (!data) {
-        DEBUG_LOG("Update for missing handle watch %d", watch);
-        return;
-    }
-
-    if (events) {
-        GIOCondition cond = 0;
-        if (events == data->events)
-            return;
-
-        if (data->source)
-            g_source_remove(data->source);
-
-        cond |= G_IO_HUP;
-        if (events & VIR_EVENT_HANDLE_READABLE)
-            cond |= G_IO_IN;
-        if (events & VIR_EVENT_HANDLE_WRITABLE)
-            cond |= G_IO_OUT;
-        data->source = g_io_add_watch(data->channel,
-                                      cond,
-                                      virt_viewer_events_dispatch_handle,
-                                      data);
-        data->events = events;
-    } else {
-        if (!data->source)
-            return;
-
-        g_source_remove(data->source);
-        data->source = 0;
-        data->events = 0;
-    }
-}
-
-
-static gboolean
-virt_viewer_events_cleanup_handle(gpointer user_data)
-{
-    struct virt_viewer_events_handle *data = user_data;
-
-    DEBUG_LOG("Cleanup of handle %p", data);
-    g_return_val_if_fail(data != NULL, FALSE);
-
-    if (data->ff)
-        (data->ff)(data->opaque);
-
-    free(data);
-    return FALSE;
-}
-
-
-static int
-virt_viewer_events_remove_handle(int watch)
-{
-    struct virt_viewer_events_handle *data = virt_viewer_events_find_handle(watch);
-
-    if (!data) {
-        DEBUG_LOG("Remove of missing watch %d", watch);
-        return -1;
-    }
-
-    DEBUG_LOG("Remove handle %d %d", watch, data->fd);
-
-    if (!data->source)
-        return -1;
-
-    g_source_remove(data->source);
-    data->source = 0;
-    data->events = 0;
-
-    g_idle_add(virt_viewer_events_cleanup_handle, data);
-    return 0;
-}
-
-struct virt_viewer_events_timeout
-{
-    int timer;
-    int interval;
-    guint source;
-    virEventTimeoutCallback cb;
-    void *opaque;
-    virFreeCallback ff;
-};
-
-
-static int nexttimer = 1;
-static unsigned int ntimeouts = 0;
-static struct virt_viewer_events_timeout **timeouts = NULL;
-
-static gboolean
-virt_viewer_events_dispatch_timeout(void *opaque)
-{
-    struct virt_viewer_events_timeout *data = opaque;
-    DEBUG_LOG("Dispatch timeout %p %p %d %p", data, data->cb, data->timer, data->opaque);
-    (data->cb)(data->timer, data->opaque);
-
-    return TRUE;
-}
-
-static int
-virt_viewer_events_add_timeout(int interval,
-                               virEventTimeoutCallback cb,
-                               void *opaque,
-                               virFreeCallback ff)
-{
-    struct virt_viewer_events_timeout *data;
-
-    timeouts = g_realloc(timeouts, sizeof(*timeouts)*(ntimeouts+1));
-    data = g_malloc(sizeof(*data));
-    memset(data, 0, sizeof(*data));
-
-    data->timer = nexttimer++;
-    data->interval = interval;
-    data->cb = cb;
-    data->opaque = opaque;
-    data->ff = ff;
-    if (interval >= 0)
-        data->source = g_timeout_add(interval,
-                                     virt_viewer_events_dispatch_timeout,
-                                     data);
-
-    timeouts[ntimeouts++] = data;
-
-    DEBUG_LOG("Add timeout %p %d %p %p %d", data, interval, cb, opaque, data->timer);
-
-    return data->timer;
-}
-
-
-static struct virt_viewer_events_timeout *
-virt_viewer_events_find_timeout(int timer)
-{
-    unsigned int i;
-    for (i = 0 ; i < ntimeouts ; i++)
-        if (timeouts[i]->timer == timer)
-            return timeouts[i];
-
-    return NULL;
-}
-
-
-static void
-virt_viewer_events_update_timeout(int timer,
-                                  int interval)
-{
-    struct virt_viewer_events_timeout *data = virt_viewer_events_find_timeout(timer);
-
-    if (!data) {
-        DEBUG_LOG("Update of missing timer %d", timer);
-        return;
-    }
-
-    DEBUG_LOG("Update timeout %p %d %d", data, timer, interval);
-
-    if (interval >= 0) {
-        if (data->source)
-            return;
-
-        data->interval = interval;
-        data->source = g_timeout_add(data->interval,
-                                     virt_viewer_events_dispatch_timeout,
-                                     data);
-    } else {
-        if (!data->source)
-            return;
-
-        g_source_remove(data->source);
-        data->source = 0;
-    }
-}
-
-
-static gboolean
-virt_viewer_events_cleanup_timeout(gpointer user_data)
-{
-    struct virt_viewer_events_timeout *data = user_data;
-
-    DEBUG_LOG("Cleanup of timeout %p", data);
-    g_return_val_if_fail(data != NULL, FALSE);
-
-    if (data->ff)
-        (data->ff)(data->opaque);
-
-    free(data);
-    return FALSE;
-}
-
-
-static int
-virt_viewer_events_remove_timeout(int timer)
-{
-    struct virt_viewer_events_timeout *data = virt_viewer_events_find_timeout(timer);
-
-    if (!data) {
-        DEBUG_LOG("Remove of missing timer %d", timer);
-        return -1;
-    }
-
-    DEBUG_LOG("Remove timeout %p %d", data, timer);
-
-    if (!data->source)
-        return -1;
-
-    g_source_remove(data->source);
-    data->source = 0;
-
-    g_idle_add(virt_viewer_events_cleanup_timeout, data);
-    return 0;
-}
-
-
-void virt_viewer_events_register(void) {
-    virEventRegisterImpl(virt_viewer_events_add_handle,
-                         virt_viewer_events_update_handle,
-                         virt_viewer_events_remove_handle,
-                         virt_viewer_events_add_timeout,
-                         virt_viewer_events_update_timeout,
-                         virt_viewer_events_remove_timeout);
-}
-
-/*
- * Local variables:
- *  c-indent-level: 4
- *  c-basic-offset: 4
- *  indent-tabs-mode: nil
- * End:
- */
diff --git a/src/virt-viewer-events.h b/src/virt-viewer-events.h
deleted file mode 100644
index c56950f..0000000
--- a/src/virt-viewer-events.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * events.h: event loop integration
- *
- * Copyright (C) 2008-2012 Daniel P. Berrange
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
- *
- * Author: Daniel P. Berrange <berrange at redhat.com>
- */
-
-#ifndef VIRT_VIEWER_EVENT_H
-#define VIRT_VIEWER_EVENT_H
-
-#include "virt-viewer-util.h"
-
-void virt_viewer_events_register(void);
-
-#endif
-/*
- * Local variables:
- *  c-indent-level: 4
- *  c-basic-offset: 4
- *  indent-tabs-mode: nil
- * End:
- */
diff --git a/src/virt-viewer-main.c b/src/virt-viewer-main.c
index 1f3f112..e006e91 100644
--- a/src/virt-viewer-main.c
+++ b/src/virt-viewer-main.c
@@ -25,6 +25,7 @@
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 #include <stdlib.h>
+#include <libvirt-glib/libvirt-glib.h>
 #ifdef HAVE_GTK_VNC
 #include <vncdisplay.h>
 #endif
@@ -80,6 +81,8 @@ int main(int argc, char **argv)
                                base_name);
     g_free(base_name);
 
+    gvir_init(&argc, &argv);
+
     /* Setup command line options */
     context = g_option_context_new (_("- Virtual machine graphical console"));
     g_option_context_add_main_entries (context, options, NULL);
diff --git a/src/virt-viewer.c b/src/virt-viewer.c
index e1553fd..d7947b1 100644
--- a/src/virt-viewer.c
+++ b/src/virt-viewer.c
@@ -35,6 +35,7 @@
 #include <glib/gprintf.h>
 #include <glib/gi18n.h>
 
+#include <libvirt-glib/libvirt-glib.h>
 #include <libvirt/libvirt.h>
 #include <libvirt/virterror.h>
 #include <libxml/xpath.h>
@@ -46,7 +47,6 @@
 
 #include "virt-viewer.h"
 #include "virt-viewer-app.h"
-#include "virt-viewer-events.h"
 #include "virt-viewer-auth.h"
 
 struct _VirtViewerPrivate {
@@ -729,7 +729,7 @@ virt_viewer_connect(VirtViewerApp *app)
 static gboolean
 virt_viewer_start(VirtViewerApp *app)
 {
-    virt_viewer_events_register();
+    gvir_event_register();
 
     virSetErrorFunc(NULL, virt_viewer_error_func);
 
diff --git a/virt-viewer.spec.in b/virt-viewer.spec.in
index d68029c..8f05dcd 100644
--- a/virt-viewer.spec.in
+++ b/virt-viewer.spec.in
@@ -54,6 +54,7 @@ BuildRequires: gtk3-devel >= @GTK3_REQUIRED@
 BuildRequires: gtk2-devel >= @GTK2_REQUIRED@
 %endif
 BuildRequires: libvirt-devel >= @LIBVIRT_REQUIRED@
+BuildRequires: libvirt-glib-devel >= @LIBVIRT_GLIB_REQUIRED@
 BuildRequires: libxml2-devel >= @LIBXML2_REQUIRED@
 %if %{with_gtk3}
 BuildRequires: gtk-vnc2-devel >= @GTK_VNC2_REQUIRED@
-- 
1.8.4.2




More information about the virt-tools-list mailing list