[Spice-devel] [PATCH spice-gtk] usb-redir: Fix read error handling depending on SpiceUsbDevice == libusb_device

Hans de Goede hdegoede at redhat.com
Mon Oct 8 15:10:41 PDT 2012


This has not been true for a while now, but since getting an error return
from usbredirhost_read_guest_data() is rare no one has tripped over this
sofar.

Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 gtk/channel-usbredir-priv.h   |  1 +
 gtk/channel-usbredir.c        | 29 +++++++++++++++++++----------
 gtk/usb-device-manager-priv.h |  2 +-
 gtk/usb-device-manager.c      | 24 +++---------------------
 4 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/gtk/channel-usbredir-priv.h b/gtk/channel-usbredir-priv.h
index 5d28c79..2c4c6f7 100644
--- a/gtk/channel-usbredir-priv.h
+++ b/gtk/channel-usbredir-priv.h
@@ -38,6 +38,7 @@ void spice_usbredir_channel_set_context(SpiceUsbredirChannel *channel,
 void spice_usbredir_channel_connect_device_async(
                                         SpiceUsbredirChannel *channel,
                                         libusb_device        *device,
+                                        SpiceUsbDevice       *spice_device,
                                         GCancellable         *cancellable,
                                         GAsyncReadyCallback   callback,
                                         gpointer              user_data);
diff --git a/gtk/channel-usbredir.c b/gtk/channel-usbredir.c
index 02edcd3..11bf38c 100644
--- a/gtk/channel-usbredir.c
+++ b/gtk/channel-usbredir.c
@@ -66,6 +66,7 @@ enum SpiceUsbredirChannelState {
 
 struct _SpiceUsbredirChannelPrivate {
     libusb_device *device;
+    SpiceUsbDevice *spice_device;
     libusb_context *context;
     struct usbredirhost *host;
     /* To catch usbredirhost error messages and report them as a GError */
@@ -287,6 +288,8 @@ static void spice_usbredir_channel_open_acl_cb(
         g_simple_async_result_take_error(priv->result, err);
         libusb_unref_device(priv->device);
         priv->device = NULL;
+        g_boxed_free(spice_usb_device_get_type(), priv->spice_device);
+        priv->spice_device = NULL;
         priv->state  = STATE_DISCONNECTED;
     }
 
@@ -304,6 +307,7 @@ G_GNUC_INTERNAL
 void spice_usbredir_channel_connect_device_async(
                                           SpiceUsbredirChannel *channel,
                                           libusb_device        *device,
+                                          SpiceUsbDevice       *spice_device,
                                           GCancellable         *cancellable,
                                           GAsyncReadyCallback   callback,
                                           gpointer              user_data)
@@ -337,6 +341,8 @@ void spice_usbredir_channel_connect_device_async(
     }
 
     priv->device = libusb_ref_device(device);
+    priv->spice_device = g_boxed_copy(spice_usb_device_get_type(),
+                                      spice_device);
 #if USE_POLKIT
     priv->result = result;
     priv->state  = STATE_WAITING_FOR_ACL_HELPER;
@@ -355,6 +361,8 @@ void spice_usbredir_channel_connect_device_async(
         g_simple_async_result_take_error(result, err);
         libusb_unref_device(priv->device);
         priv->device = NULL;
+        g_boxed_free(spice_usb_device_get_type(), priv->spice_device);
+        priv->spice_device = NULL;
     }
 #endif
 
@@ -413,6 +421,8 @@ void spice_usbredir_channel_disconnect_device(SpiceUsbredirChannel *channel)
         usbredirhost_set_device(priv->host, NULL);
         libusb_unref_device(priv->device);
         priv->device = NULL;
+        g_boxed_free(spice_usb_device_get_type(), priv->spice_device);
+        priv->spice_device = NULL;
         priv->state  = STATE_DISCONNECTED;
         break;
     }
@@ -568,7 +578,7 @@ enum {
 };
 
 struct DEVICE_ERROR {
-    libusb_device *device;
+    SpiceUsbDevice *spice_device;
     GError *error;
 };
 
@@ -582,12 +592,12 @@ static void do_emit_main_context(GObject *object, int event, gpointer params)
     case DEVICE_ERROR: {
         struct DEVICE_ERROR *p = params;
         /* Check that the device has not changed before we manage to run */
-        if (p->device == priv->device) {
+        if (p->spice_device == priv->spice_device) {
             spice_usbredir_channel_disconnect_device(channel);
             spice_usb_device_manager_device_error(
                 spice_usb_device_manager_get(
                     spice_channel_get_session(SPICE_CHANNEL(channel)), NULL),
-                p->device, p->error);
+                p->spice_device, p->error);
         }
         break;
     }
@@ -642,14 +652,13 @@ static void usbredir_handle_msg(SpiceChannel *c, SpiceMsgIn *in)
 
     r = usbredirhost_read_guest_data(priv->host);
     if (r != 0) {
-        libusb_device *device = priv->device;
+        SpiceUsbDevice *spice_device = priv->spice_device;
         gchar *desc;
         GError *err;
 
-        g_return_if_fail(device != NULL);
+        g_return_if_fail(spice_device != NULL);
 
-        desc = spice_usb_device_get_description((SpiceUsbDevice *)device,
-                                                NULL);
+        desc = spice_usb_device_get_description(spice_device, NULL);
         switch (r) {
         case usbredirhost_read_parse_error:
             err = g_error_new(SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
@@ -673,9 +682,9 @@ static void usbredir_handle_msg(SpiceChannel *c, SpiceMsgIn *in)
 
         CHANNEL_DEBUG(c, "%s", err->message);
 
-        g_boxed_copy(spice_usb_device_get_type(), device);
-        emit_main_context(channel, DEVICE_ERROR, device, err);
-        g_boxed_free(spice_usb_device_get_type(), device);
+        spice_device = g_boxed_copy(spice_usb_device_get_type(), spice_device);
+        emit_main_context(channel, DEVICE_ERROR, spice_device, err);
+        g_boxed_free(spice_usb_device_get_type(), spice_device);
 
         g_error_free(err);
     }
diff --git a/gtk/usb-device-manager-priv.h b/gtk/usb-device-manager-priv.h
index a8617ba..51a38df 100644
--- a/gtk/usb-device-manager-priv.h
+++ b/gtk/usb-device-manager-priv.h
@@ -34,7 +34,7 @@ void spice_usb_device_manager_stop_event_listening(
 #ifdef USE_USBREDIR
 #include <libusb.h>
 void spice_usb_device_manager_device_error(
-    SpiceUsbDeviceManager *manager, libusb_device *libdev, GError *err);
+    SpiceUsbDeviceManager *manager, SpiceUsbDevice *device, GError *err);
 
 guint8 spice_usb_device_get_busnum(SpiceUsbDevice *device);
 guint8 spice_usb_device_get_devaddr(SpiceUsbDevice *device);
diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index 6358da8..229b590 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -155,9 +155,6 @@ static void  spice_usb_device_set_state(SpiceUsbDevice *device, guint8 s);
 
 static gboolean spice_usb_device_equal_libdev(SpiceUsbDevice *device,
                                               libusb_device *libdev);
-static SpiceUsbDevice *
-spice_usb_device_manager_libdev_to_device(SpiceUsbDeviceManager *self,
-                                          libusb_device *libdev);
 static libusb_device *
 spice_usb_device_manager_device_to_libdev(SpiceUsbDeviceManager *self,
                                           SpiceUsbDevice *device);
@@ -920,14 +917,10 @@ void spice_usb_device_manager_stop_event_listening(
 }
 
 void spice_usb_device_manager_device_error(
-    SpiceUsbDeviceManager *self, libusb_device *libdev, GError *err)
+    SpiceUsbDeviceManager *self, SpiceUsbDevice *device, GError *err)
 {
-    SpiceUsbDevice *device;
-
     g_return_if_fail(SPICE_IS_USB_DEVICE_MANAGER(self));
-    g_return_if_fail(libdev != 0);
-
-    device = spice_usb_device_manager_libdev_to_device(self, libdev);
+    g_return_if_fail(device != 0);
 
     g_signal_emit(self, signals[DEVICE_ERROR], 0, device, err);
 }
@@ -1091,6 +1084,7 @@ _spice_usb_device_manager_connect_device_async(SpiceUsbDeviceManager *self,
         }
         spice_usbredir_channel_connect_device_async(channel,
                                  libdev,
+                                 device,
                                  cancellable,
                                  spice_usb_device_manager_channel_connect_cb,
                                  result);
@@ -1471,18 +1465,6 @@ spice_usb_device_equal_libdev(SpiceUsbDevice *device,
     return ((bus1 == bus2) && (addr1 == addr2));
 }
 
-static SpiceUsbDevice *
-spice_usb_device_manager_libdev_to_device(SpiceUsbDeviceManager *self,
-                                          libusb_device *libdev)
-{
-    guint8 bus, addr;
-
-    bus  = libusb_get_bus_number(libdev);
-    addr = libusb_get_device_address(libdev);
-
-    return spice_usb_device_manager_find_device(self, bus, addr);
-}
-
 /*
  * Caller must libusb_unref_device the libusb_device returned by this function.
  * Returns a libusb_device, or NULL upon failure
-- 
1.7.12.1



More information about the Spice-devel mailing list