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 <berra...@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 diff --git a/include/io/channel-unix.h b/include/io/channel-unix.h new file mode 100644 index 0000000..b50aeaf --- /dev/null +++ b/include/io/channel-unix.h @@ -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__ */ diff --git a/io/Makefile.objs b/io/Makefile.objs index ca088a8..a776676 100644 --- a/io/Makefile.objs +++ b/io/Makefile.objs @@ -1 +1,2 @@ util-obj-y += channel.o +util-obj-y += channel-unix.o diff --git a/io/channel-unix.c b/io/channel-unix.c new file mode 100644 index 0000000..2da08a12 --- /dev/null +++ b/io/channel-unix.c @@ -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; +} -- 2.1.0