From nobody Sun Feb 8 14:53:52 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1580923158800682.442812142522; Wed, 5 Feb 2020 09:19:18 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-310-BwdWbyISN_SrnbVA383n7g-1; Wed, 05 Feb 2020 12:19:13 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 48FFE14E4; Wed, 5 Feb 2020 17:19:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 236C55D9E2; Wed, 5 Feb 2020 17:19:08 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D6D5285CFF; Wed, 5 Feb 2020 17:19:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 015HJ4Rk015885 for ; Wed, 5 Feb 2020 12:19:04 -0500 Received: by smtp.corp.redhat.com (Postfix) id E0F81857AD; Wed, 5 Feb 2020 17:19:04 +0000 (UTC) Received: from domokun.gsslab.fab.redhat.com (unknown [10.33.8.110]) by smtp.corp.redhat.com (Postfix) with ESMTP id 51E8F81213; Wed, 5 Feb 2020 17:19:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580923157; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=aEaGijF4F0nh0n2N/OR2FNuSOtP8BEApvtDiWc0EGIw=; b=ac16dqFfTGJ6PGaNiXYpdSbC7++WJnHOF+JjLPcp7jcnN91v0pdHVRuWVJK2JWlVeSJMyf 2dghUcWrS2v5zVc2tS/bdOt0U+h81j8AVCw0KPpm009TJpB74q6eF9u99pUnt5c+iLdolJ OEyij/J5nMZhxPUL72qs2d/5Y8z6KuY= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 2/7] src: introduce helper API for creating GSource for socket Date: Wed, 5 Feb 2020 17:18:53 +0000 Message-Id: <20200205171858.2694632-3-berrange@redhat.com> In-Reply-To: <20200205171858.2694632-1-berrange@redhat.com> References: <20200205171858.2694632-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: BwdWbyISN_SrnbVA383n7g-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" We need to be able to create event loop watches using the GSource API for sockets. GIOChannel is able todo this, but we don't want to use the GIOChannel APIs for reading/writing, and testing shows just using its GSource APIs is unreliable on Windows. This patch thus creates a standalone helper API for creating a GSource for a socket file descriptor. This impl is derived from code in QEMU's io/channel-watch.c file that was written by myself & Paolo Bonzini & thus under Red Hat copyright. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Pavel Hrdina --- build-aux/syntax-check.mk | 3 + src/util/Makefile.inc.am | 2 + src/util/vireventglibwatch.c | 249 +++++++++++++++++++++++++++++++++++ src/util/vireventglibwatch.h | 48 +++++++ 4 files changed, 302 insertions(+) create mode 100644 src/util/vireventglibwatch.c create mode 100644 src/util/vireventglibwatch.h diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 46f546615f..d43fa501aa 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -2339,3 +2339,6 @@ exclude_file_name_regexp--sc_prohibit_backslash_align= ment =3D \ =20 exclude_file_name_regexp--sc_prohibit_always_true_header_tests =3D \ ^src/util/(virfile|virnetdev|virnetdevip)\.[c,h]|$$ + +exclude_file_name_regexp--sc_prohibit_select =3D \ + ^build-aux/syntax-check\.mk|src/util/vireventglibwatch\.c$$ diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am index 2abdca548c..a51cba736b 100644 --- a/src/util/Makefile.inc.am +++ b/src/util/Makefile.inc.am @@ -61,6 +61,8 @@ UTIL_SOURCES =3D \ util/virerrorpriv.h \ util/virevent.c \ util/virevent.h \ + util/vireventglibwatch.c \ + util/vireventglibwatch.h \ util/vireventpoll.c \ util/vireventpoll.h \ util/virfcp.c \ diff --git a/src/util/vireventglibwatch.c b/src/util/vireventglibwatch.c new file mode 100644 index 0000000000..7694e74f23 --- /dev/null +++ b/src/util/vireventglibwatch.c @@ -0,0 +1,249 @@ +/* + * vireventglibwatch.c: GSource impl for sockets + * + * Copyright (C) 2015-2020 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.1 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 + +#include "vireventglibwatch.h" + +#ifndef WIN32 +typedef struct virEventGLibFDSource virEventGLibFDSource; +struct virEventGLibFDSource { + GSource parent; + GPollFD pollfd; + int fd; + GIOCondition condition; +}; + + +static gboolean +virEventGLibFDSourcePrepare(GSource *source G_GNUC_UNUSED, + gint *timeout) +{ + *timeout =3D -1; + + return FALSE; +} + + +static gboolean +virEventGLibFDSourceCheck(GSource *source) +{ + virEventGLibFDSource *ssource =3D (virEventGLibFDSource *)source; + + return ssource->pollfd.revents & ssource->condition; +} + + +static gboolean +virEventGLibFDSourceDispatch(GSource *source, + GSourceFunc callback, + gpointer user_data) +{ + virEventGLibSocketFunc func =3D (virEventGLibSocketFunc)callback; + virEventGLibFDSource *ssource =3D (virEventGLibFDSource *)source; + + return (*func)(ssource->fd, + ssource->pollfd.revents & ssource->condition, + user_data); +} + + +static void +virEventGLibFDSourceFinalize(GSource *source G_GNUC_UNUSED) +{ +} + + +GSourceFuncs virEventGLibFDSourceFuncs =3D { + .prepare =3D virEventGLibFDSourcePrepare, + .check =3D virEventGLibFDSourceCheck, + .dispatch =3D virEventGLibFDSourceDispatch, + .finalize =3D virEventGLibFDSourceFinalize +}; + + +GSource *virEventGLibCreateSocketWatch(int fd, + GIOCondition condition) +{ + GSource *source; + virEventGLibFDSource *ssource; + + source =3D g_source_new(&virEventGLibFDSourceFuncs, + sizeof(virEventGLibFDSource)); + ssource =3D (virEventGLibFDSource *)source; + + ssource->condition =3D condition; + ssource->fd =3D fd; + + ssource->pollfd.fd =3D fd; + ssource->pollfd.events =3D condition; + + g_source_add_poll(source, &ssource->pollfd); + + return source; +} + +#else /* WIN32 */ + +# define WIN32_LEAN_AND_MEAN +# include + +typedef struct virEventGLibSocketSource virEventGLibSocketSource; +struct virEventGLibSocketSource { + GSource parent; + GPollFD pollfd; + int fd; + SOCKET socket; + HANDLE event; + int revents; + GIOCondition condition; +}; + + +static gboolean +virEventGLibSocketSourcePrepare(GSource *source G_GNUC_UNUSED, + gint *timeout) +{ + *timeout =3D -1; + + return FALSE; +} + + +/* + * NB, this impl only works when the socket is in non-blocking + * mode on Win32 + */ +static gboolean +virEventGLibSocketSourceCheck(GSource *source) +{ + static struct timeval tv0; + + virEventGLibSocketSource *ssource =3D (virEventGLibSocketSource *)sour= ce; + WSANETWORKEVENTS ev; + fd_set rfds, wfds, xfds; + + if (!ssource->condition) + return 0; + + WSAEnumNetworkEvents(ssource->socket, ssource->event, &ev); + + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_ZERO(&xfds); + if (ssource->condition & G_IO_IN) + FD_SET(ssource->socket, &rfds); + if (ssource->condition & G_IO_OUT) + FD_SET(ssource->socket, &wfds); + if (ssource->condition & G_IO_PRI) + FD_SET(ssource->socket, &xfds); + + ssource->revents =3D 0; + if (select(0, &rfds, &wfds, &xfds, &tv0) =3D=3D 0) + return 0; + + if (FD_ISSET(ssource->socket, &rfds)) + ssource->revents |=3D G_IO_IN; + + if (FD_ISSET(ssource->socket, &wfds)) + ssource->revents |=3D G_IO_OUT; + + if (FD_ISSET(ssource->socket, &xfds)) + ssource->revents |=3D G_IO_PRI; + + return ssource->revents; +} + + +static gboolean +virEventGLibSocketSourceDispatch(GSource *source, + GSourceFunc callback, + gpointer user_data) +{ + virEventGLibSocketFunc func =3D (virEventGLibSocketFunc)callback; + virEventGLibSocketSource *ssource =3D (virEventGLibSocketSource *)sour= ce; + + return (*func)(ssource->fd, ssource->revents, user_data); +} + + +static void +virEventGLibSocketSourceFinalize(GSource *source) +{ + virEventGLibSocketSource *ssource =3D (virEventGLibSocketSource *)sour= ce; + + WSAEventSelect(ssource->socket, NULL, 0); + CloseHandle(ssource->event); +} + + +GSourceFuncs virEventGLibSocketSourceFuncs =3D { + .prepare =3D virEventGLibSocketSourcePrepare, + .check =3D virEventGLibSocketSourceCheck, + .dispatch =3D virEventGLibSocketSourceDispatch, + .finalize =3D virEventGLibSocketSourceFinalize +}; + + +GSource *virEventGLibCreateSocketWatch(int fd, + GIOCondition condition) +{ + GSource *source; + virEventGLibSocketSource *ssource; + + source =3D g_source_new(&virEventGLibSocketSourceFuncs, + sizeof(virEventGLibSocketSource)); + ssource =3D (virEventGLibSocketSource *)source; + + ssource->condition =3D condition; + ssource->fd =3D fd; + ssource->socket =3D _get_osfhandle(fd); + ssource->event =3D CreateEvent(NULL, FALSE, FALSE, NULL); + ssource->revents =3D 0; + + ssource->pollfd.fd =3D (gintptr)ssource->event; + ssource->pollfd.events =3D G_IO_IN; + + WSAEventSelect(ssource->socket, ssource->event, + FD_READ | FD_ACCEPT | FD_CLOSE | + FD_CONNECT | FD_WRITE | FD_OOB); + + g_source_add_poll(source, &ssource->pollfd); + + return source; +} + +#endif /* WIN32 */ + + +guint virEventGLibAddSocketWatch(int fd, + GIOCondition condition, + GMainContext *context, + virEventGLibSocketFunc func, + gpointer opaque, + GDestroyNotify notify) +{ + g_autoptr(GSource) source =3D NULL; + + source =3D virEventGLibCreateSocketWatch(fd, condition); + g_source_set_callback(source, (GSourceFunc)func, opaque, notify); + + return g_source_attach(source, context); +} diff --git a/src/util/vireventglibwatch.h b/src/util/vireventglibwatch.h new file mode 100644 index 0000000000..2f7e61cfba --- /dev/null +++ b/src/util/vireventglibwatch.h @@ -0,0 +1,48 @@ +/* + * vireventglibwatch.h: GSource impl for sockets + * + * Copyright (C) 2015-2020 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.1 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 + * . + */ + +#pragma once + +#include "internal.h" + +/** + * virEventGLibCreateSocketWatch: + * @fd: the file descriptor + * @condition: the I/O condition + * + * Create a new main loop source that is able to + * monitor the file descriptor @fd for the + * I/O conditions in @condition. + * + * Returns: the new main loop source + */ +GSource *virEventGLibCreateSocketWatch(int fd, + GIOCondition condition); + +typedef gboolean (*virEventGLibSocketFunc)(int fd, + GIOCondition condition, + gpointer data); + +guint virEventGLibAddSocketWatch(int fd, + GIOCondition condition, + GMainContext *context, + virEventGLibSocketFunc func, + gpointer opaque, + GDestroyNotify notify); --=20 2.24.1