new file mode 100644
@@ -0,0 +1,50 @@
+/*
+ * QEMU I/O channels UNIX platform helper APIs
+ *
+ * Copyright (c) 2015 Red Hat, Inc.
+ *
+ * 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 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef QIO_CHANNEL_UNIX_H__
+#define QIO_CHANNEL_UNIX_H__
+
+#include "io/channel.h"
+
+/*
+ * This module provides helper functions that will be needed by
+ * the various QIOChannel implementations, for performing UNIX
+ * platform specific tasks. In particular setting up UNIX file
+ * descriptors watches.
+ */
+
+/**
+ * @ioc: the channel object
+ * @fd: the UNIX file descriptor
+ * @condition: the I/O condition
+ *
+ * Create a new main loop source that is able to
+ * monitor the UNIX file descriptor @fd for the
+ * I/O conditions in @condition. This is able
+ * monitor block devices, character devices,
+ * sockets, pipes but not plain files.
+ *
+ * Returns: the new main loop source
+ */
+GSource *qio_channel_unix_create_fd_watch(QIOChannel *ioc,
+ int fd,
+ GIOCondition condition);
+
+#endif /* QIO_CHANNEL_UNIX_H__ */
@@ -1 +1,2 @@
util-obj-y += channel.o
+util-obj-y += channel-unix.o
new file mode 100644
@@ -0,0 +1,100 @@
+/*
+ * QEMU I/O channels UNIX platform helper APIs
+ *
+ * Copyright (c) 2015 Red Hat, Inc.
+ *
+ * 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 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "io/channel-unix.h"
+
+typedef struct QIOChannelUNIXSource QIOChannelUNIXSource;
+struct QIOChannelUNIXSource {
+ GSource parent;
+ GPollFD fd;
+ QIOChannel *ioc;
+ GIOCondition condition;
+};
+
+static gboolean
+qio_channel_unix_source_prepare(GSource *source G_GNUC_UNUSED,
+ gint *timeout)
+{
+ *timeout = -1;
+
+ return FALSE;
+}
+
+static gboolean
+qio_channel_unix_source_check(GSource *source)
+{
+ QIOChannelUNIXSource *ssource = (QIOChannelUNIXSource *)source;
+ GIOCondition poll_condition = ssource->fd.revents;
+
+ return poll_condition & ssource->condition;
+}
+
+static gboolean
+qio_channel_unix_source_dispatch(GSource *source,
+ GSourceFunc callback,
+ gpointer user_data)
+{
+ QIOChannelFunc func = (QIOChannelFunc)callback;
+ QIOChannelUNIXSource *ssource = (QIOChannelUNIXSource *)source;
+
+ return (*func)(ssource->ioc,
+ ssource->fd.revents & ssource->condition,
+ user_data);
+}
+
+static void
+qio_channel_unix_source_finalize(GSource *source)
+{
+ QIOChannelUNIXSource *ssource = (QIOChannelUNIXSource *)source;
+
+ object_unref(OBJECT(ssource->ioc));
+}
+
+GSourceFuncs qio_channel_unix_source_funcs = {
+ qio_channel_unix_source_prepare,
+ qio_channel_unix_source_check,
+ qio_channel_unix_source_dispatch,
+ qio_channel_unix_source_finalize
+};
+
+GSource *qio_channel_unix_create_fd_watch(QIOChannel *ioc,
+ int fd,
+ GIOCondition condition)
+{
+ GSource *source;
+ QIOChannelUNIXSource *ssource;
+
+ source = g_source_new(&qio_channel_unix_source_funcs,
+ sizeof(QIOChannelUNIXSource));
+ g_source_set_name(source, "QIOChannelUNIX");
+ ssource = (QIOChannelUNIXSource *)source;
+
+ ssource->ioc = ioc;
+ object_ref(OBJECT(ioc));
+
+ ssource->condition = condition;
+
+ ssource->fd.fd = fd;
+ ssource->fd.events = condition;
+
+ g_source_add_poll(source, &ssource->fd);
+
+ return source;
+}
A number of the channel implementations will require the ability to create watches on UNIX file descriptors. To avoid duplicating this code in each channel, provide a helper API for dealing with UNIX FDs. This code is explicitly not portable to the Windows platform, which will require its own set of helpers Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- include/io/channel-unix.h | 50 +++++++++++++++++++++++ io/Makefile.objs | 1 + io/channel-unix.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 include/io/channel-unix.h create mode 100644 io/channel-unix.c