From: "Daniel P. Berrangé" <berrange@redhat.com>
To: marcandre.lureau@redhat.com
Cc: qemu-devel@nongnu.org, "Thomas Huth" <thuth@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Michael Roth" <michael.roth@amd.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Beraldo Leal" <bleal@redhat.com>,
"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
"Stefan Weil" <sw@weilnetz.de>,
"Markus Armbruster" <armbru@redhat.com>,
"Eric Blake" <eblake@redhat.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>
Subject: Re: [PATCH v4 04/11] osdep: implement qemu_socketpair() for win32
Date: Tue, 7 Mar 2023 14:50:06 +0000 [thread overview]
Message-ID: <ZAdPHkheoRweyYzw@redhat.com> (raw)
In-Reply-To: <20230306122751.2355515-5-marcandre.lureau@redhat.com>
On Mon, Mar 06, 2023 at 04:27:44PM +0400, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> Manually implement a socketpair() function, using UNIX sockets and
> simple peer credential checking.
>
> QEMU doesn't make much use of socketpair, beside vhost-user which is not
> available for win32 at this point. However, I intend to use it for
> writing some new portable tests.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> include/qemu/sockets.h | 2 -
> util/oslib-win32.c | 110 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 110 insertions(+), 2 deletions(-)
>
> diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
> index 2b0698a7c9..d935fd80da 100644
> --- a/include/qemu/sockets.h
> +++ b/include/qemu/sockets.h
> @@ -15,7 +15,6 @@ int inet_aton(const char *cp, struct in_addr *ia);
> bool fd_is_socket(int fd);
> int qemu_socket(int domain, int type, int protocol);
>
> -#ifndef WIN32
> /**
> * qemu_socketpair:
> * @domain: specifies a communication domain, such as PF_UNIX
> @@ -30,7 +29,6 @@ int qemu_socket(int domain, int type, int protocol);
> * Return 0 on success.
> */
> int qemu_socketpair(int domain, int type, int protocol, int sv[2]);
> -#endif
>
> int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
> /*
> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> index 29a667ae3d..16f8a67f7e 100644
> --- a/util/oslib-win32.c
> +++ b/util/oslib-win32.c
> @@ -310,6 +310,116 @@ bool qemu_socket_unselect(int sockfd, Error **errp)
> return qemu_socket_select(sockfd, NULL, 0, errp);
> }
>
> +int qemu_socketpair(int domain, int type, int protocol, int sv[2])
> +{
> + struct sockaddr_un addr = {
> + 0,
> + };
> + socklen_t socklen;
> + int listener = -1;
> + int client = -1;
> + int server = -1;
> + g_autofree char *path = NULL;
> + int tmpfd;
> + u_long arg;
> + int ret = -1;
> +
> + g_return_val_if_fail(sv != NULL, -1);
> +
> + addr.sun_family = AF_UNIX;
> + socklen = sizeof(addr);
> +
> + tmpfd = g_file_open_tmp(NULL, &path, NULL);
> + if (tmpfd == -1 || !path) {
> + errno = EACCES;
> + goto out;
> + }
> +
> + close(tmpfd);
> +
> + if (strlen(path) >= sizeof(addr.sun_path)) {
> + errno = EINVAL;
> + goto out;
> + }
> +
> + strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
> +
> + listener = socket(domain, type, protocol);
> + if (listener == -1) {
> + goto out;
> + }
> +
> + if (DeleteFile(path) == 0 && GetLastError() != ERROR_FILE_NOT_FOUND) {
> + errno = EACCES;
> + goto out;
> + }
> + g_clear_pointer(&path, g_free);
> +
> + if (bind(listener, (struct sockaddr *)&addr, socklen) == -1) {
> + goto out;
> + }
> +
> + if (listen(listener, 1) == -1) {
> + goto out;
> + }
> +
> + client = socket(domain, type, protocol);
> + if (client == -1) {
> + goto out;
> + }
> +
> + arg = 1;
> + if (ioctlsocket(client, FIONBIO, &arg) != NO_ERROR) {
> + goto out;
> + }
> +
> + if (connect(client, (struct sockaddr *)&addr, socklen) == -1 &&
> + WSAGetLastError() != WSAEWOULDBLOCK) {
> + goto out;
> + }
> +
> + server = accept(listener, NULL, NULL);
> + if (server == -1) {
> + goto out;
> + }
In theory at this point 'client' if connect() returned WSAEWOULDBLOCK,
then at this point it should be fully connected. I wonder if that is
actually guaranteed though, or should we do something to validate
there's no race condition ?
> +
> + arg = 0;
> + if (ioctlsocket(client, FIONBIO, &arg) != NO_ERROR) {
> + goto out;
> + }
> +
> + arg = 0;
> + if (ioctlsocket(client, SIO_AF_UNIX_GETPEERPID, &arg) != NO_ERROR) {
> + goto out;
> + }
Maybe this will force a synchronization point ?
Alteratively select() + getsockopt(SO_ERROR) is what we used to
do to check for connect() completion (logic removed now but can
be seen in b2587932582333197c88bf663785b19f441989d7)
> +
> + if (arg != GetCurrentProcessId()) {
> + errno = EPERM;
> + goto out;
> + }
> +
> + sv[0] = server;
> + server = -1;
> + sv[1] = client;
> + client = -1;
> + ret = 0;
> +
> +out:
> + if (listener != -1) {
> + close(listener);
> + }
> + if (client != -1) {
> + close(client);
> + }
> + if (server != -1) {
> + close(server);
> + }
> + if (path) {
> + DeleteFile(path);
> + }
> + return ret;
> +}
> +
> #undef connect
> int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
> socklen_t addrlen)
> --
> 2.39.2
>
With regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
next prev parent reply other threads:[~2023-03-07 14:50 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-06 12:27 [PATCH v4 00/11] QMP command to import win32 sockets marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 01/11] tests: fix path separator, use g_build_filename() marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 02/11] char: do not double-close fd when failing to add client marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 03/11] tests/docker: fix a win32 error due to portability marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 04/11] osdep: implement qemu_socketpair() for win32 marcandre.lureau
2023-03-07 14:50 ` Daniel P. Berrangé [this message]
2023-03-08 6:53 ` Marc-André Lureau
2023-03-08 9:27 ` Daniel P. Berrangé
2023-03-06 12:27 ` [PATCH v4 05/11] qmp: 'add_client' actually expects sockets marcandre.lureau
2023-03-06 15:02 ` Markus Armbruster
2023-03-06 12:27 ` [PATCH v4 06/11] monitor: release the lock before calling close() marcandre.lureau
2023-03-06 15:29 ` Markus Armbruster
2023-03-06 15:44 ` Markus Armbruster
2023-03-06 12:27 ` [PATCH v4 07/11] qapi/gen: run C code through clang-format, if possible marcandre.lureau
2023-03-06 16:02 ` Markus Armbruster
2023-03-06 18:26 ` Marc-André Lureau
2023-03-06 16:05 ` Peter Maydell
2023-03-06 18:29 ` Marc-André Lureau
2023-03-06 18:39 ` Peter Maydell
2023-03-07 8:51 ` Markus Armbruster
2023-03-06 12:27 ` [PATCH v4 08/11] qmp: add 'get-win32-socket' marcandre.lureau
2023-03-06 15:47 ` Markus Armbruster
2023-03-07 12:31 ` Marc-André Lureau
2023-03-06 12:27 ` [PATCH v4 09/11] libqtest: make qtest_qmp_add_client work on win32 marcandre.lureau
2023-03-07 14:54 ` Daniel P. Berrangé
2023-03-06 12:27 ` [PATCH v4 10/11] qtest: enable vnc-display test " marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 11/11] QMP/HMP: only actually implement getfd on CONFIG_POSIX marcandre.lureau
2023-03-06 16:01 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZAdPHkheoRweyYzw@redhat.com \
--to=berrange@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=armbru@redhat.com \
--cc=bleal@redhat.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=kraxel@redhat.com \
--cc=lvivier@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=michael.roth@amd.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=sw@weilnetz.de \
--cc=thuth@redhat.com \
--cc=wainersm@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).