From: Stefan Berger <stefanb@linux.ibm.com>
To: marcandre.lureau@redhat.com, qemu-devel@nongnu.org
Cc: "Samuel Thibault" <samuel.thibault@ens-lyon.org>,
"Jason Wang" <jasowang@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Michael Roth" <michael.roth@amd.com>,
qemu-arm@nongnu.org, "Laurent Vivier" <lvivier@redhat.com>,
"Thomas Huth" <thuth@redhat.com>, "Kevin Wolf" <kwolf@redhat.com>,
qemu-block@nongnu.org, armbru@redhat.com,
"Peter Maydell" <peter.maydell@linaro.org>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Stefan Weil" <sw@weilnetz.de>, "Fam Zheng" <fam@euphon.net>,
"Stefan Berger" <stefanb@linux.vnet.ibm.com>,
"Joel Stanley" <joel@jms.id.au>,
"Hanna Reitz" <hreitz@redhat.com>
Subject: Re: [PATCH v3 16/16] win32: replace closesocket() with close() wrapper
Date: Mon, 6 Mar 2023 09:45:13 -0500 [thread overview]
Message-ID: <1ecbfe9f-402a-1af0-cac9-170ae17a1ceb@linux.ibm.com> (raw)
In-Reply-To: <20230221124802.4103554-17-marcandre.lureau@redhat.com>
On 2/21/23 07:48, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> Use a close() wrapper instead, so that we don't need to worry about
> closesocket() vs close() anymore, let's hope.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> index 7836fb0be3..29a667ae3d 100644
> --- a/util/oslib-win32.c
> +++ b/util/oslib-win32.c
> @@ -370,39 +370,39 @@ int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
> }
>
>
> -#undef closesocket
> -int qemu_closesocket_wrap(int fd)
> +#undef close
> +int qemu_close_wrap(int fd)
> {
> int ret;
> DWORD flags = 0;
> - SOCKET s = _get_osfhandle(fd);
> + SOCKET s = INVALID_SOCKET;
>
> - if (s == INVALID_SOCKET) {
> - return -1;
> - }
> + if (fd_is_socket(fd)) {
> + s = _get_osfhandle(fd);
>
> - /*
> - * If we were to just call _close on the descriptor, it would close the
> - * HANDLE, but it wouldn't free any of the resources associated to the
> - * SOCKET, and we can't call _close after calling closesocket, because
> - * closesocket has already closed the HANDLE, and _close would attempt to
> - * close the HANDLE again, resulting in a double free. We can however
> - * protect the HANDLE from actually being closed long enough to close the
> - * file descriptor, then close the socket itself.
> - */
> - if (!GetHandleInformation((HANDLE)s, &flags)) {
> - errno = EACCES;
> - return -1;
> - }
> + /*
> + * If we were to just call _close on the descriptor, it would close the
> + * HANDLE, but it wouldn't free any of the resources associated to the
> + * SOCKET, and we can't call _close after calling closesocket, because
> + * closesocket has already closed the HANDLE, and _close would attempt to
> + * close the HANDLE again, resulting in a double free. We can however
> + * protect the HANDLE from actually being closed long enough to close the
> + * file descriptor, then close the socket itself.
> + */
> + if (!GetHandleInformation((HANDLE)s, &flags)) {
> + errno = EACCES;
> + return -1;
> + }
>
> - if (!SetHandleInformation((HANDLE)s, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE)) {
> - errno = EACCES;
> - return -1;
> + if (!SetHandleInformation((HANDLE)s, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE)) {
> + errno = EACCES;
> + return -1;
> + }
> }
>
> ret = close(fd);
>
> - if (!SetHandleInformation((HANDLE)s, flags, flags)) {
> + if (s != INVALID_SOCKET && !SetHandleInformation((HANDLE)s, flags, flags)) {
> errno = EACCES;
> return -1;
> }
> @@ -411,13 +411,15 @@ int qemu_closesocket_wrap(int fd)
> * close() returns EBADF since we PROTECT_FROM_CLOSE the underlying handle,
> * but the FD is actually freed
> */
> - if (ret < 0 && errno != EBADF) {
> + if (ret < 0 && (s == INVALID_SOCKET || errno != EBADF)) {
> return ret;
> }
>
> - ret = closesocket(s);
> - if (ret < 0) {
> - errno = socket_error();
> + if (s != INVALID_SOCKET) {
> + ret = closesocket(s);
> + if (ret < 0) {
> + errno = socket_error();
> + }
> }
if (fs_is_socket()) {{
...
close()
...
closesocket()
...
} else {
...
close()
...
}
would avoid the threading and make this function look a bit simpler.
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
next prev parent reply other threads:[~2023-03-06 14:46 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-21 12:47 [PATCH v3 00/16] win32: do not mix SOCKET and fd space marcandre.lureau
2023-02-21 12:47 ` [PATCH v3 01/16] util: drop qemu_fork() marcandre.lureau
2023-02-21 12:47 ` [PATCH v3 02/16] tests: use closesocket() marcandre.lureau
2023-02-21 12:47 ` [PATCH v3 03/16] io: " marcandre.lureau
2023-02-21 12:47 ` [PATCH v3 04/16] tests: add test-error-report marcandre.lureau
2023-03-02 18:02 ` Stefan Berger
2023-02-21 12:47 ` [PATCH v3 05/16] error: add global &error_warn destination marcandre.lureau
2023-03-02 18:18 ` Stefan Berger
2023-02-21 12:47 ` [PATCH v3 06/16] win32/socket: introduce qemu_socket_select() helper marcandre.lureau
2023-03-02 18:23 ` Stefan Berger
2023-02-21 12:47 ` [PATCH v3 07/16] win32/socket: introduce qemu_socket_unselect() helper marcandre.lureau
2023-03-02 18:26 ` Stefan Berger
2023-02-21 12:47 ` [PATCH v3 08/16] aio: make aio_set_fd_poll() static to aio-posix.c marcandre.lureau
2023-03-02 18:29 ` Stefan Berger
2023-02-21 12:47 ` [PATCH v3 09/16] aio/win32: aio_set_fd_handler() only supports SOCKET marcandre.lureau
2023-03-02 18:37 ` Stefan Berger
2023-02-21 12:47 ` [PATCH v3 10/16] RFC: build-sys: add slirp.wrap marcandre.lureau
2023-02-21 14:14 ` Paolo Bonzini
2023-02-21 12:47 ` [PATCH v3 11/16] main-loop: remove qemu_fd_register(), win32/slirp/socket specific marcandre.lureau
2023-03-02 18:43 ` Stefan Berger
2023-02-21 12:47 ` [PATCH v3 12/16] slirp: unregister the win32 SOCKET marcandre.lureau
2023-03-02 18:45 ` Stefan Berger
2023-03-06 7:57 ` Marc-André Lureau
2023-02-21 12:47 ` [PATCH v3 13/16] slirp: open-code qemu_socket_(un)select() marcandre.lureau
2023-03-06 13:59 ` Stefan Berger
2023-03-06 14:03 ` Marc-André Lureau
2023-03-06 14:47 ` Stefan Berger
2023-03-06 14:16 ` Stefan Berger
2023-02-21 12:47 ` [PATCH v3 14/16] win32: avoid mixing SOCKET and file descriptor space marcandre.lureau
2023-03-02 20:53 ` Stefan Berger
2023-03-06 7:54 ` Marc-André Lureau
2023-03-06 14:26 ` Stefan Berger
2023-02-21 12:48 ` [PATCH v3 15/16] os-posix: remove useless ioctlsocket() define marcandre.lureau
2023-02-21 14:16 ` Paolo Bonzini
2023-02-21 12:48 ` [PATCH v3 16/16] win32: replace closesocket() with close() wrapper marcandre.lureau
2023-03-06 14:45 ` Stefan Berger [this message]
2023-03-02 14:09 ` [PATCH v3 00/16] win32: do not mix SOCKET and fd space Marc-André Lureau
2023-03-06 8:04 ` Paolo Bonzini
2023-03-06 8:08 ` Marc-André Lureau
2023-03-06 10:02 ` Paolo Bonzini
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=1ecbfe9f-402a-1af0-cac9-170ae17a1ceb@linux.ibm.com \
--to=stefanb@linux.ibm.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=fam@euphon.net \
--cc=hreitz@redhat.com \
--cc=jasowang@redhat.com \
--cc=joel@jms.id.au \
--cc=kwolf@redhat.com \
--cc=lvivier@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=michael.roth@amd.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=samuel.thibault@ens-lyon.org \
--cc=stefanb@linux.vnet.ibm.com \
--cc=stefanha@redhat.com \
--cc=sw@weilnetz.de \
--cc=thuth@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).