From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58705) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yj7Cj-0007jw-QC for qemu-devel@nongnu.org; Fri, 17 Apr 2015 10:25:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yj7Cg-0003pA-Hz for qemu-devel@nongnu.org; Fri, 17 Apr 2015 10:25:13 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43029) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yj7Cg-0003p5-Av for qemu-devel@nongnu.org; Fri, 17 Apr 2015 10:25:10 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t3HEP9a1010724 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 17 Apr 2015 10:25:09 -0400 From: "Daniel P. Berrange" Date: Fri, 17 Apr 2015 15:22:25 +0100 Message-Id: <1429280557-8887-23-git-send-email-berrange@redhat.com> In-Reply-To: <1429280557-8887-1-git-send-email-berrange@redhat.com> References: <1429280557-8887-1-git-send-email-berrange@redhat.com> Subject: [Qemu-devel] [PATCH v1 RFC 22/34] io: add helper module for creating watches on UNIX FDs List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Gerd Hoffmann , Stefan Hajnoczi 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 --- 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 . + * + */ + +#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 . + * + */ + +#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