Fix virt-viewer.exe on win32

Libvirt uses gnulib for making winsock look like POSIX
sockets. This means that in the libvirt event handle
callbacks the application will be given a file descriptor
rather than a winsock HANDLE object. The g_io_channel_unix_new
method will detect that it is an FD and delegate to the
g_io_channel_win32_new_fd method. Unfortunately the glib Win32
event loop impl is not very good at dealing with FD objects,
simulating poll() by doing a read() on the FD :-(

The API docs for g_io_channel_win32_new_fd say

 "All reads from the file descriptor should be done by
  this internal GLib thread. Your code should call only
  g_io_channel_read()."

This isn't going to fly for libvirt, since it has zero
knowledge of glib at all, so is just doing normal read().

Fortunately we can work around this problem by turning
the FD we get from libvirt back into a HANDLE using the
_get_osfhandle() method.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2014-01-24 16:51:04 +00:00
parent b2a233b711
commit ec31b4fd99

View File

@ -26,6 +26,9 @@
#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"
@ -96,7 +99,12 @@ int virt_viewer_events_add_handle(int 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);