win32: do not mix SOCKET and fd space
Subsystems (4)
v3 cover win32: do not mix SOCKET and fd space
From: Marc-André Lureau <[email protected]>
Hi,
A win32 SOCKET handle is often cast to an int file descriptor, as this is what other OS use for sockets. When necessary, QEMU eventually queries whether it's a socket with the help of fd_is_socket(). However, there is no guarantee of conflict between the fd and SOCKET space. Such conflict would have surprising consequences. We can fix this by using FDs only.
After fixing a few missed closesocket(), this patch series makes the win32 socket API wrappers take FDs. It finally get rid of closesocket() usage by using a close() wrapper instead. (note that fdopen/fclose would not be enough either to close the underlying socket appropriately)
v3:
- fix closesocket() to prevent CloseHandle() from close()
- fix direct closesocket() usage (#undef closesocket before)
- add a test for &error_warn
- add r-b tags
v2:
- add clean up patch "util: drop qemu_fork()"
- add a "&error_warn", to help with basic error reporting
- fix errno handling after _get_osfhandle()
- introduce qemu_socket_(un)select() helpers
- add patch "aio_set_fd_handler() only supports SOCKET"
- add meson slirp.wrap RFC
- various misc cleanups
- add r-b tags
Marc-André Lureau (16):
util: drop qemu_fork()
tests: use closesocket()
io: use closesocket()
tests: add test-error-report
error: add global &error_warn destination win32/socket: introduce qemu_socket_select() helper win32/socket: introduce qemu_socket_unselect() helper
aio: make aio_set_fd_poll() static to aio-posix.c aio/win32: aio_set_fd_handler() only supports SOCKET
RFC: build-sys: add slirp.wrap
main-loop: remove qemu_fd_register(), win32/slirp/socket specific
slirp: unregister the win32 SOCKET
slirp: open-code qemu_socket_(un)select()
win32: avoid mixing SOCKET and file descriptor space
os-posix: remove useless ioctlsocket() define
win32: replace closesocket() with close() wrapper
include/block/aio.h | 8 -- include/qapi/error.h | 6 + include/qemu/main-loop.h | 2 - include/qemu/osdep.h | 14 -- include/sysemu/os-posix.h | 3 - include/sysemu/os-win32.h | 15 ++- backends/tpm/tpm_emulator.c | 6 +- crypto/afalg.c | 6 +- hw/hyperv/syndbg.c | 4 +- io/channel-socket.c | 8 +- io/channel-watch.c | 10 +- net/dgram.c | 14 +- net/slirp.c | 16 ++- net/socket.c | 22 +-- tests/qtest/libqtest.c | 8 +- tests/qtest/microbit-test.c | 2 +- tests/qtest/netdev-socket.c | 10 +- tests/unit/socket-helpers.c | 2 +- tests/unit/test-error-report.c | 139 +++++++++++++++++++ util/aio-posix.c | 6 +- util/aio-win32.c | 23 ++-- util/error.c | 10 +- util/main-loop.c | 11 -- util/oslib-posix.c | 70 ---------- util/oslib-win32.c | 240 ++++++++++++++++++++++++++++----- util/qemu-sockets.c | 22 +-- .gitignore | 2 + subprojects/slirp.wrap | 6 + tests/unit/meson.build | 1 + 29 files changed, 461 insertions(+), 225 deletions(-) create mode 100644 tests/unit/test-error-report.c create mode 100644 subprojects/slirp.wrap
Signature · 1 line
2.39.2
PATCH v3 1/16 util: drop qemu_fork()
From: Marc-André Lureau <[email protected]>
Fortunately, qemu_fork() is no longer used since commit a95570e3e4d6 ("io/command: use glib GSpawn, instead of open-coding fork/exec"). (GSpawn uses posix_spawn() whenever possible instead)
--- include/qemu/osdep.h | 14 --------- util/oslib-posix.c | 70 -------------------------------------------- util/oslib-win32.c | 9 ------ 3 files changed, 93 deletions(-)
include/qemu/osdep.h +0 -14
@@ -665,20 +665,6 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,*/ char *qemu_get_pid_name (pid_t pid); -/** -* qemu_fork: -* -* A version of fork that avoids signal handler race -* conditions that can lead to child process getting -* signals that are otherwise only expected by the -* parent. It also resets all signal handlers to the -* default settings. -* -* Returns 0 to child process, pid number to parent -* or -1 on failure. -*/ -pid_t qemu_fork (Error **errp); -/* Using intptr_t ensures that qemu_*_page_mask is sign-extended even * when intptr_t is 32-bit and we are aligning a long long. */
util/oslib-posix.c +0 -70
76 lines not on this page · raw message
util/oslib-win32.c +0 -9
@@ -283,15 +283,6 @@ char *qemu_get_pid_name(pid_t pid) } -pid_t qemu_fork (Error **errp) -{ - errno =ENOSYS ; -error_setg_errno (errp, errno, -"cannot fork child process" ); -return -1 ; -} - -#undef connectint qemu_connect_wrap (int sockfd,const struct sockaddr *addr,socklen_t addrlen)
Signature · 1 line
2.39.2
PATCH v3 2/16 tests: use closesocket()
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Because they are actually sockets...
--- tests/unit/socket-helpers.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tests/unit/socket-helpers.c +3 -3
@@ -117,13 +117,13 @@ static int socket_can_bind_connect(const char *hostname, int family) cleanup:if (afd !=-1 ) { -close (afd); +closesocket (afd); }if (cfd !=-1 ) { -close (cfd); +closesocket (cfd); }if (lfd !=-1 ) { -close (lfd); +closesocket (lfd); }if (res) {freeaddrinfo (res);
Signature · 1 line
2.39.2
PATCH v3 3/16 io: use closesocket()
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Because they are actually sockets...
--- io/channel-socket.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
io/channel-socket.c +3 -3
@@ -159,7 +159,7 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,trace_qio_channel_socket_connect_complete (ioc, fd);if (qio_channel_socket_set_fd (ioc, fd, errp) <0 ) { -close (fd); +closesocket (fd);return -1 ; }
@@ -233,7 +233,7 @@ int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,trace_qio_channel_socket_listen_complete (ioc, fd);if (qio_channel_socket_set_fd (ioc, fd, errp) <0 ) { -close (fd); +closesocket (fd);return -1 ; }qio_channel_set_feature (QIO_CHANNEL (ioc),QIO_CHANNEL_FEATURE_LISTEN );
@@ -310,7 +310,7 @@ int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,trace_qio_channel_socket_dgram_complete (ioc, fd);if (qio_channel_socket_set_fd (ioc, fd, errp) <0 ) { -close (fd); +closesocket (fd);return -1 ; }
Signature · 1 line
2.39.2
PATCH v3 4/16 tests: add test-error-report
In reply to [email protected]From: Marc-André Lureau <[email protected]>
--- tests/unit/test-error-report.c | 121 +++++++++++++++++++++++++++++++++ tests/unit/meson.build | 1 + 2 files changed, 122 insertions(+) create mode 100644 tests/unit/test-error-report.c
tests/unit/test-error-report.c new file +121 -0
@@ -0,0 +1,121 @@ +/* +* Error reporting test +* +* Copyright (C) 2022 Red Hat Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "glib-compat.h" +#include <locale.h> + +#include "qemu/error-report.h" + +static void +test_error_report_simple (void ) +{ +if (g_test_subprocess ()) { +error_report ("%s" ,"test error" ); +warn_report ("%s" ,"test warn" ); +info_report ("%s" ,"test info" ); +return ; + } + +g_test_trap_subprocess (NULL ,0 ,0 ); +g_test_trap_assert_passed (); +g_test_trap_assert_stderr ("\ +test-error-report: test error*\ +test-error-report: warning: test warn*\ +test-error-report: info: test info*\ +" ); +} + +static void +test_error_report_loc (void ) +{ +if (g_test_subprocess ()) { +loc_set_file ("some-file.c" ,7717 ); +error_report ("%s" ,"test error1" ); +loc_set_none (); +error_report ("%s" ,"test error2" ); +return ; + } + +g_test_trap_subprocess (NULL ,0 ,0 ); +g_test_trap_assert_passed (); +g_test_trap_assert_stderr ("\ +test-error-report:some-file.c:7717: test error1*\ +test-error-report: test error2*\ +" ); +} + +static void +test_error_report_glog (void ) +{ +if (g_test_subprocess ()) { +g_message ("gmessage" ); +return ; + } + +g_test_trap_subprocess (NULL ,0 ,0 ); +g_test_trap_assert_passed (); +g_test_trap_assert_stderr ("test-error-report: info: gmessage*" ); +} + +static void +test_error_report_once (void ) +{ +int i; + +if (g_test_subprocess ()) { +for (i =0 ; i <3 ; i++) { +warn_report_once ("warn" ); +error_report_once ("err" ); + } +return ; + } + +g_test_trap_subprocess (NULL ,0 ,0 ); +g_test_trap_assert_passed (); +g_test_trap_assert_stderr ("\ +test-error-report: warning: warn*\ +test-error-report: err*\ +" ); +} + +static void +test_error_report_timestamp (void ) +{ +if (g_test_subprocess ()) { + message_with_timestamp = true; +warn_report ("warn" ); +error_report ("err" ); +return ; + } + +g_test_trap_subprocess (NULL ,0 ,0 ); +g_test_trap_assert_passed (); +g_test_trap_assert_stderr ("\ +*-*-*:*:* test-error-report: warning: warn*\ +*-*-*:*:* test-error-report: err*\ +" ); +} + +int +main (int argc,char *argv[]) +{ +setlocale (LC_ALL ,"" ); + +g_test_init (&argc, &argv,NULL ); +error_init ("test-error-report" ); + +g_test_add_func ("/error-report/simple" , test_error_report_simple); +g_test_add_func ("/error-report/loc" , test_error_report_loc); +g_test_add_func ("/error-report/glog" , test_error_report_glog); +g_test_add_func ("/error-report/once" , test_error_report_once); +g_test_add_func ("/error-report/timestamp" , test_error_report_timestamp); + +return g_test_run (); +}
tests/unit/meson.build +1 -0
@@ -11,6 +11,7 @@ tests = { 'check-qobject': [], 'check-qjson': [], 'check-qlit': [], + 'test-error-report': [], 'test-qobject-output-visitor': [testqapi], 'test-clone-visitor': [testqapi], 'test-qobject-input-visitor': [testqapi],
Signature · 1 line
2.39.2
Re: [PATCH v3 04/16] tests: add test-error-report
Quoted 3 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Signed-off-by Marc-André Lureau <[email protected]>
PATCH v3 5/16 error: add global &error_warn destination
In reply to [email protected]From: Marc-André Lureau <[email protected]>
This can help debugging issues or develop, when error handling is introduced.
--- include/qapi/error.h | 6 ++++++ tests/unit/test-error-report.c | 18 ++++++++++++++++++ util/error.c | 10 +++++++--- 3 files changed, 31 insertions(+), 3 deletions(-)
include/qapi/error.h +6 -0
@@ -519,6 +519,12 @@ static inline void error_propagator_cleanup(ErrorPropagator *prop)G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (ErrorPropagator, error_propagator_cleanup); +/* +* Special error destination to warn on error. +* See error_setg() and error_propagate() for details. +*/ +extern Error *error_warn; +/* * Special error destination to abort on error. * See error_setg() and error_propagate() for details.
tests/unit/test-error-report.c +18 -0
@@ -12,6 +12,7 @@#include <locale.h> #include "qemu/error-report.h" +#include "qapi/error.h" static void test_error_report_simple (void )
@@ -103,6 +104,22 @@ test_error_report_timestamp(void) "); } +static void +test_error_warn (void ) +{ +if (g_test_subprocess ()) { +error_setg (&error_warn,"Testing &error_warn" ); +return ; + } + +g_test_trap_subprocess (NULL ,0 ,0 ); +g_test_trap_assert_passed (); +g_test_trap_assert_stderr ("\ +test-error-report: warning: Testing &error_warn*\ +" ); +} + +int main (int argc,char *argv[]) {
@@ -116,6 +133,7 @@ main(int argc, char *argv[])g_test_add_func ("/error-report/glog" , test_error_report_glog);g_test_add_func ("/error-report/once" , test_error_report_once);g_test_add_func ("/error-report/timestamp" , test_error_report_timestamp); +g_test_add_func ("/error-report/warn" , test_error_warn);return g_test_run (); }
util/error.c +7 -3
@@ -27,8 +27,9 @@ struct ErrorError *error_abort;Error *error_fatal; +Error *error_warn; -static void error_handle_fatal (Error **errp,Error *err) +static void error_handle (Error **errp,Error *err) {if (errp == &error_abort) { fprintf(stderr, "Unexpected error in %s() at %s:%d:\n",
@@ -43,6 +44,9 @@ static void error_handle_fatal(Error **errp, Error *err)error_report_err (err);exit (1 ); } +if (errp == &error_warn) { +warn_report_err (err); + } }G_GNUC_PRINTF (6 ,0 )
@@ -71,7 +75,7 @@ static void error_setv(Error **errp, err->line = line; err->func = func; -error_handle_fatal (errp, err); +error_handle (errp, err); *errp = err; errno = saved_errno;
@@ -284,7 +288,7 @@ void error_propagate(Error **dst_errp, Error *local_err)if (!local_err) {return ; } -error_handle_fatal (dst_errp, local_err); +error_handle (dst_errp, local_err);if (dst_errp && !*dst_errp) { *dst_errp = local_err; }else {
Signature · 1 line
2.39.2
Re: [PATCH v3 05/16] error: add global &error_warn destination
Quoted 6 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
This can help debugging issues or develop, when error handling is introduced.
Signed-off-by Marc-André Lureau <[email protected]>
PATCH v3 6/16 win32/socket: introduce qemu_socket_select() helper
In reply to [email protected]From: Marc-André Lureau <[email protected]>
This is a wrapper for WSAEventSelect, with Error handling. By default, it will produce a warning, so callers don't have to be modified now, and yet we can spot potential mis-use.
--- include/sysemu/os-win32.h | 5 +++++ io/channel-socket.c | 4 ++-- io/channel-watch.c | 6 +++--- util/aio-win32.c | 2 +- util/main-loop.c | 6 +++--- util/oslib-win32.c | 17 ++++++++++++++++- 6 files changed, 30 insertions(+), 10 deletions(-)
include/sysemu/os-win32.h +5 -0
@@ -29,6 +29,7 @@#include <winsock2.h> #include <windows.h> #include <ws2tcpip.h> +#include "qemu/typedefs.h" #ifdef HAVE_AFUNIX_H #include <afunix.h>
@@ -144,6 +145,10 @@ static inline void qemu_funlockfile(FILE *f)#endif } +/* Helper for WSAEventSelect, to report errors */ +bool qemu_socket_select (SOCKET s,WSAEVENT hEventObject, +long lNetworkEvents,Error **errp); +/* We wrap all the sockets functions so that we can * set errno based on WSAGetLastError() */
io/channel-socket.c +2 -2
@@ -442,7 +442,7 @@ static void qio_channel_socket_finalize(Object *obj) } }#ifdef WIN32 -WSAEventSelect (ioc->fd,NULL ,0 ); +qemu_socket_select (ioc->fd,NULL ,0 ,NULL );#endif closesocket (ioc->fd); ioc->fd =-1 ;
@@ -846,7 +846,7 @@ qio_channel_socket_close(QIOChannel *ioc,if (sioc->fd !=-1 ) {#ifdef WIN32 -WSAEventSelect (sioc->fd,NULL ,0 ); +qemu_socket_select (sioc->fd,NULL ,0 ,NULL );#endif if (qio_channel_has_feature (ioc,QIO_CHANNEL_FEATURE_LISTEN )) {socket_listen_cleanup (sioc->fd, errp);
io/channel-watch.c +3 -3
@@ -281,9 +281,9 @@ GSource *qio_channel_create_socket_watch(QIOChannel *ioc,GSource *source;QIOChannelSocketSource *ssource; -WSAEventSelect (socket, ioc->event, -FD_READ |FD_ACCEPT |FD_CLOSE | -FD_CONNECT |FD_WRITE |FD_OOB ); +qemu_socket_select (socket, ioc->event, +FD_READ |FD_ACCEPT |FD_CLOSE | +FD_CONNECT |FD_WRITE |FD_OOB ,NULL ); source =g_source_new (&qio_channel_socket_source_funcs,sizeof (QIOChannelSocketSource));
util/aio-win32.c +1 -1
@@ -115,7 +115,7 @@ void aio_set_fd_handler(AioContext *ctx,QLIST_INSERT_HEAD_RCU (&ctx->aio_handlers, node, node); event =event_notifier_get_handle (&ctx->notifier); -WSAEventSelect (node->pfd.fd, event, bitmask); +qemu_socket_select (node->pfd.fd, event, bitmask,NULL ); }if (old_node) {aio_remove_fd_handler (ctx, old_node);
util/main-loop.c +3 -3
@@ -416,9 +416,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)void qemu_fd_register (int fd) { -WSAEventSelect (fd,event_notifier_get_handle (&qemu_aio_context->notifier), -FD_READ |FD_ACCEPT |FD_CLOSE | -FD_CONNECT |FD_WRITE |FD_OOB ); +qemu_socket_select (fd,event_notifier_get_handle (&qemu_aio_context->notifier), +FD_READ |FD_ACCEPT |FD_CLOSE | +FD_CONNECT |FD_WRITE |FD_OOB ,NULL ); } static int pollfds_fill(GArray *pollfds, fd_set *rfds, fd_set *wfds,
util/oslib-win32.c +16 -1
@@ -180,7 +180,7 @@ static int socket_error(void)void qemu_socket_set_block (int fd) {unsigned long opt =0 ; -WSAEventSelect (fd,NULL ,0 ); +qemu_socket_select (fd,NULL ,0 ,NULL );ioctlsocket (fd,FIONBIO , &opt); }
@@ -283,6 +283,21 @@ char *qemu_get_pid_name(pid_t pid) } +bool qemu_socket_select (SOCKET s,WSAEVENT hEventObject, +long lNetworkEvents,Error **errp) +{ +if (errp ==NULL ) { + errp = &error_warn; + } + +if (WSAEventSelect (s, hEventObject, lNetworkEvents) !=0 ) { +error_setg_win32 (errp,WSAGetLastError (),"failed to WSAEventSelect()" ); +return false; + } + +return true; +} +#undef connectint qemu_connect_wrap (int sockfd,const struct sockaddr *addr,socklen_t addrlen)
Signature · 1 line
2.39.2
Re: [PATCH v3 06/16] win32/socket: introduce qemu_socket_select() helper
Quoted 7 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
This is a wrapper for WSAEventSelect, with Error handling. By default, it will produce a warning, so callers don't have to be modified now, and yet we can spot potential mis-use.
Signed-off-by Marc-André Lureau <[email protected]>
PATCH v3 7/16 win32/socket: introduce qemu_socket_unselect() helper
In reply to [email protected]From: Marc-André Lureau <[email protected]>
A more explicit version of qemu_socket_select() with no events.
--- include/sysemu/os-win32.h | 2 ++ io/channel-socket.c | 4 ++-- util/oslib-win32.c | 7 ++++++- 3 files changed, 10 insertions(+), 3 deletions(-)
include/sysemu/os-win32.h +2 -0
@@ -149,6 +149,8 @@ static inline void qemu_funlockfile(FILE *f)bool qemu_socket_select (SOCKET s,WSAEVENT hEventObject,long lNetworkEvents,Error **errp); +bool qemu_socket_unselect (SOCKET s,Error **errp); +/* We wrap all the sockets functions so that we can * set errno based on WSAGetLastError() */
io/channel-socket.c +2 -2
@@ -442,7 +442,7 @@ static void qio_channel_socket_finalize(Object *obj) } }#ifdef WIN32 -qemu_socket_select (ioc->fd,NULL ,0 ,NULL ); +qemu_socket_unselect (ioc->fd,NULL );#endif closesocket (ioc->fd); ioc->fd =-1 ;
@@ -846,7 +846,7 @@ qio_channel_socket_close(QIOChannel *ioc,if (sioc->fd !=-1 ) {#ifdef WIN32 -qemu_socket_select (sioc->fd,NULL ,0 ,NULL ); +qemu_socket_unselect (sioc->fd,NULL );#endif if (qio_channel_has_feature (ioc,QIO_CHANNEL_FEATURE_LISTEN )) {socket_listen_cleanup (sioc->fd, errp);
util/oslib-win32.c +6 -1
@@ -180,7 +180,7 @@ static int socket_error(void)void qemu_socket_set_block (int fd) {unsigned long opt =0 ; -qemu_socket_select (fd,NULL ,0 ,NULL ); +qemu_socket_unselect (fd,NULL );ioctlsocket (fd,FIONBIO , &opt); }
@@ -298,6 +298,11 @@ bool qemu_socket_select(SOCKET s, WSAEVENT hEventObject,return true; } +bool qemu_socket_unselect (SOCKET s,Error **errp) +{ +return qemu_socket_select (s,NULL ,0 , errp); +} +#undef connectint qemu_connect_wrap (int sockfd,const struct sockaddr *addr,socklen_t addrlen)
Signature · 1 line
2.39.2
Re: [PATCH v3 07/16] win32/socket: introduce qemu_socket_unselect() helper
Quoted 5 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
A more explicit version of
qemu_socket_select()with no events.Signed-off-by Marc-André Lureau <[email protected]>
PATCH v3 8/16 aio: make aio_set_fd_poll() static to aio-posix.c
In reply to [email protected]From: Marc-André Lureau <[email protected]>
--- include/block/aio.h | 8 -------- util/aio-posix.c | 6 +++--- util/aio-win32.c | 7 ------- 3 files changed, 3 insertions(+), 18 deletions(-)
include/block/aio.h +0 -8
@@ -482,14 +482,6 @@ void aio_set_fd_handler(AioContext *ctx, IOHandler *io_poll_ready, void *opaque); -/* Set polling begin/end callbacks for a file descriptor that has already been -* registered with aio_set_fd_handler. Do nothing if the file descriptor is -* not registered. -*/ -void aio_set_fd_poll (AioContext *ctx,int fd, -IOHandler *io_poll_begin, -IOHandler *io_poll_end); -/* Register an event notifier and associated callbacks. Behaves very similarly * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks * will be invoked when using aio_poll().
util/aio-posix.c +3 -3
@@ -180,9 +180,9 @@ void aio_set_fd_handler(AioContext *ctx, } } -void aio_set_fd_poll (AioContext *ctx,int fd, -IOHandler *io_poll_begin, -IOHandler *io_poll_end) +static void aio_set_fd_poll (AioContext *ctx,int fd, +IOHandler *io_poll_begin, +IOHandler *io_poll_end) {AioHandler *node =find_aio_handler (ctx, fd);
util/aio-win32.c +0 -7
@@ -125,13 +125,6 @@ void aio_set_fd_handler(AioContext *ctx,aio_notify (ctx); } -void aio_set_fd_poll (AioContext *ctx,int fd, -IOHandler *io_poll_begin, -IOHandler *io_poll_end) -{ -/* Not implemented */ -} - void aio_set_event_notifier(AioContext *ctx, EventNotifier *e, bool is_external,
Signature · 1 line
2.39.2
Re: [PATCH v3 08/16] aio: make aio_set_fd_poll() static to aio-posix.c
Quoted 3 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Signed-off-by Marc-André Lureau <[email protected]>
PATCH v3 9/16 aio/win32: aio_set_fd_handler() only supports SOCKET
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Let's check if the argument is actually a SOCKET, else report an error and return.
--- util/aio-win32.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
util/aio-win32.c +6 -1
@@ -22,6 +22,7 @@#include "qemu/sockets.h" #include "qapi/error.h" #include "qemu/rcu_queue.h" +#include "qemu/error-report.h" struct AioHandler {EventNotifier *e;
@@ -70,10 +71,14 @@ void aio_set_fd_handler(AioContext *ctx, IOHandler *io_poll_ready, void *opaque) { -/* fd is a SOCKET in our case */ AioHandler *old_node;AioHandler *node =NULL ; +if (!fd_is_socket (fd)) { +error_report ("fd=%d is not a socket, AIO implementation is missing" , fd); +return ; + } +qemu_lockcnt_lock (&ctx->list_lock); QLIST_FOREACH(old_node, &ctx->aio_handlers, node) {if (old_node->pfd.fd == fd && !old_node->deleted) {
Signature · 1 line
2.39.2
Re: [PATCH v3 09/16] aio/win32: aio_set_fd_handler() only supports SOCKET
Quoted 6 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Let's check if the argument is actually a SOCKET, else report an error and return.
Signed-off-by Marc-André Lureau <[email protected]>
PATCH v3 10/16 RFC: build-sys: add slirp.wrap
In reply to [email protected]From: Marc-André Lureau <[email protected]>
This allows to build with --enable-slirp / -D slirp=enabled, even when libslirp is not installed on the system. Meson will pull it from git in that case.
RFC because this is very convenient, for a developper targetting different environments, but might not be considered appropriate, as it is "a kind of" git submodule (without git submodule integration issues though, afaik, experience should tell).
--- .gitignore | 2 ++ subprojects/slirp.wrap | 6 ++++++ 2 files changed, 8 insertions(+) create mode 100644 subprojects/slirp.wrap
.gitignore +2 -0
@@ -20,3 +20,5 @@ GTAGS *.swp *.patch *.gcov + +/subprojects/slirp
subprojects/slirp.wrap new file +6 -0
@@ -0,0 +1,6 @@ +[wrap-git] +url = https://gitlab.freedesktop.org/slirp/libslirp +revision = 15c52d69 + +[provide] +slirp = libslirp_dep
Signature · 1 line
2.39.2
Re: [PATCH v3 10/16] RFC: build-sys: add slirp.wrap
Quoted 6 lines · On 2/21/23 13:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
This allows to build with --enable-slirp / -D slirp=enabled, even when libslirp is not installed on the system. Meson will pull it from git in that case.
Quoted 6 lines
RFC because this is very convenient, for a developper targetting different environments, but might not be considered appropriate, as it is "a kind of" git submodule (without git submodule integration issues though, afaik, experience should tell).
Signed-off-by Marc-André Lureau <[email protected]>
I have no objections to using wraps for slirp and especially libfdt (though the latter is not yet part of wrapdb).
However, right now meson will do network access without respecting --with-git-submodule= or something like that, i.e. with no way to avoid the fallback.
As a start, configure could hardcode --wrap-mode=nodownload, so that wraps would be used only after a conscious decision of the user to use "meson subprojects download".
Paolo
Quoted 28 lines
--- .gitignore | 2 ++ subprojects/slirp.wrap | 6 ++++++ 2 files changed, 8 insertions(+)create mode 100644 subprojects/slirp.wrap
.gitignore +2
-0@@ -20,3 +20,5 @@ GTAGS *.swp *.patch *.gcov + +/subprojects/slirpsubprojects/slirp.wrap new file +6
-0@@ -0,0 +1,6 @@ +[wrap-git] +url = https://gitlab.freedesktop.org/slirp/libslirp +revision = 15c52d69 + +[provide] +slirp = libslirp_dep
PATCH v3 11/16 main-loop: remove qemu_fd_register(), win32/slirp/socket specific
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Open-code the socket registration where it's needed, to avoid artificially used or unclear generic interface.
Furthermore, the following patches are going to make socket handling use FD-only inside QEMU, but we need to handle win32 SOCKET from libslirp.
--- include/qemu/main-loop.h | 2 -- net/slirp.c | 8 +++++++- util/main-loop.c | 11 ----------- 3 files changed, 7 insertions(+), 14 deletions(-)
include/qemu/main-loop.h +0 -2
@@ -387,8 +387,6 @@ void qemu_cond_timedwait_iothread(QemuCond *cond, int ms);/* internal interfaces */ -void qemu_fd_register (int fd); -#define qemu_bh_new (cb, opaque) \ qemu_bh_new_full((cb), (opaque), (stringify(cb)))QEMUBH *qemu_bh_new_full (QEMUBHFunc *cb,void *opaque,const char *name);
net/slirp.c +7 -1
@@ -248,7 +248,13 @@ static void net_slirp_timer_mod(void *timer, int64_t expire_timer,static void net_slirp_register_poll_fd (int fd,void *opaque) { -qemu_fd_register (fd); +#ifdef WIN32 +AioContext *ctxt =qemu_get_aio_context (); + +qemu_socket_select (fd,event_notifier_get_handle (&ctxt->notifier), +FD_READ |FD_ACCEPT |FD_CLOSE | +FD_CONNECT |FD_WRITE |FD_OOB ,NULL ); +#endif }static void net_slirp_unregister_poll_fd (int fd,void *opaque)
util/main-loop.c +0 -11
@@ -252,10 +252,6 @@ static int max_priority;static int glib_pollfds_idx;static int glib_n_poll_fds; -void qemu_fd_register (int fd) -{ -} -static void glib_pollfds_fill (int64_t *cur_timeout) {GMainContext *context =g_main_context_default ();
@@ -414,13 +410,6 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) } } -void qemu_fd_register (int fd) -{ -qemu_socket_select (fd,event_notifier_get_handle (&qemu_aio_context->notifier), -FD_READ |FD_ACCEPT |FD_CLOSE | -FD_CONNECT |FD_WRITE |FD_OOB ,NULL ); -} -static int pollfds_fill (GArray *pollfds,fd_set *rfds,fd_set *wfds,fd_set *xfds) {
Signature · 1 line
2.39.2
Re: [PATCH v3 11/16] main-loop: remove qemu_fd_register(), win32/slirp/socket specific
Quoted 33 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Open-code the socket registration where it's needed, to avoid artificially used or unclear generic interface.
Furthermore, the following patches are going to make socket handling use FD-only inside QEMU, but we need to handle win32 SOCKET from libslirp.
Signed-off-by Marc-André Lureau <[email protected]>--- include/qemu/main-loop.h | 2 -- net/slirp.c | 8 +++++++- util/main-loop.c | 11 ----------- 3 files changed, 7 insertions(+), 14 deletions(-)include/qemu/main-loop.h +0
-2@@ -387,8 +387,6 @@ void qemu_cond_timedwait_iothread(QemuCond *cond, int ms);/* internal interfaces */ -void qemu_fd_register (int fd); -#define qemu_bh_new (cb, opaque) \ qemu_bh_new_full((cb), (opaque), (stringify(cb)))QEMUBH *qemu_bh_new_full (QEMUBHFunc *cb,void *opaque,const char *name);net/slirp.c +0
-0
net/slirp.c static void net_slirp_timer_mod(void *timer, int64_t expire_timer, · line 251
Quoted 5 lines
net/slirp.c +1
-1@@ -248,7 +248,13 @@ static void net_slirp_timer_mod(void *timer, int64_t expire_timer,static void net_slirp_register_poll_fd (int fd,void *opaque) { -qemu_fd_register (fd); +#ifdef WIN32
_WIN32 ?
With this fixed:
PATCH v3 12/16 slirp: unregister the win32 SOCKET
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Presumably, this is what should happen when the SOCKET is to be removed. (it probably worked until now because closesocket() does it implicitly, but we never now how the slirp library could use the SOCKET later)
--- net/slirp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/slirp.c +3 -1
@@ -259,7 +259,9 @@ static void net_slirp_register_poll_fd(int fd, void *opaque)static void net_slirp_unregister_poll_fd (int fd,void *opaque) { -/* no qemu_fd_unregister */ +#ifdef WIN32 +qemu_socket_unselect (fd,NULL ); +#endif }static void net_slirp_notify (void *opaque)
Signature · 1 line
2.39.2
Re: [PATCH v3 12/16] slirp: unregister the win32 SOCKET
Quoted 16 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Presumably, this is what should happen when the SOCKET is to be removed. (it probably worked until now because
closesocket()does it implicitly, but we never now how the slirp library could use the SOCKET later)Signed-off-by Marc-André Lureau <[email protected]>--- net/slirp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)net/slirp.c +0
-0
net/slirp.c static void net_slirp_register_poll_fd(int fd, void *opaque) · line 262
Quoted 5 lines
net/slirp.c +1
-1@@ -259,7 +259,9 @@ static void net_slirp_register_poll_fd(int fd, void *opaque)static void net_slirp_unregister_poll_fd (int fd,void *opaque) { -/* no qemu_fd_unregister */ +#ifdef WIN32
The majority of code seems to use _WIN32. Not sure what is 'right'.
Quoted 5 lines
net/slirp.c +2
-0+qemu_socket_unselect (fd,NULL ); +#endif }static void net_slirp_notify (void *opaque)
Re: [PATCH v3 12/16] slirp: unregister the win32 SOCKET
Hi
Quoted 21 lines · Stefan Berger
net/slirp.c static void net_slirp_register_poll_fd(int fd, void
Quoted 16 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Presumably, this is what should happen when the SOCKET is to be removed. (it probably worked until now because
closesocket()does it implicitly, but we never now how the slirp library could use the SOCKET later)Signed-off-by Marc-André Lureau <[email protected]>--- net/slirp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)net/slirp.c +0
-0@@ -259,7 +259,9 @@ static void net_slirp_register_poll_fd(int fd, void*opaque)
Quoted 1 line
Quoted 7 lines
net/slirp.c static void net_slirp_register_poll_fd(int fd, void *opaque) · line 262
Quoted 4 lines
static void net_slirp_unregister_poll_fd(int fd, void *opaque) {net/slirp.c +1
-1-/* no qemu_fd_unregister */ +#ifdef WIN32 The majority of code seems to use
_WIN32. Not sure what is 'right'.
Both should be correct. I think I like the "WIN32" version better though (see also
https://stackoverflow.com/questions/662084/whats-the-difference-between-the-win32-and-win32-defines-in-c
)
Quoted 3 lines
Reviewed-by Stefan Berger <[email protected]>
thanks
Quoted 7 lines
Quoted 5 lines
net/slirp.c +2
-0+qemu_socket_unselect (fd,NULL ); +#endif }static void net_slirp_notify (void *opaque)
PATCH v3 13/16 slirp: open-code qemu_socket_(un)select()
In reply to [email protected]From: Marc-André Lureau <[email protected]>
We are about to make the QEMU socket API use file-descriptor space only, but libslirp gives us SOCKET as fd, still.
--- net/slirp.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
net/slirp.c +7 -3
@@ -251,16 +251,20 @@ static void net_slirp_register_poll_fd(int fd, void *opaque)#ifdef WIN32 AioContext *ctxt =qemu_get_aio_context (); -qemu_socket_select (fd,event_notifier_get_handle (&ctxt->notifier), +if (WSAEventSelect (fd,event_notifier_get_handle (&ctxt->notifier),FD_READ |FD_ACCEPT |FD_CLOSE | -FD_CONNECT |FD_WRITE |FD_OOB ,NULL ); +FD_CONNECT |FD_WRITE |FD_OOB ) !=0 ) { +error_setg_win32 (&error_warn,WSAGetLastError (),"failed to WSAEventSelect()" ); + }#endif }static void net_slirp_unregister_poll_fd (int fd,void *opaque) {#ifdef WIN32 -qemu_socket_unselect (fd,NULL ); +if (WSAEventSelect (fd,NULL ,0 ) !=0 ) { +error_setg_win32 (&error_warn,WSAGetLastError (),"failed to WSAEventSelect()" ); + }#endif }
Signature · 1 line
2.39.2
Re: [PATCH v3 13/16] slirp: open-code qemu_socket_(un)select()
net/slirp.c static void net_slirp_register_poll_fd(int fd, void *opaque)
Quoted 15 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
We are about to make the QEMU socket API use file-descriptor space only, but libslirp gives us SOCKET as fd, still.
Signed-off-by Marc-André Lureau <[email protected]>--- net/slirp.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)net/slirp.c +0
-0@@ -251,16 +251,20 @@ static void net_slirp_register_poll_fd(int fd, void *opaque)
Shouldn't this int fd rather be a SOCKET s instead? Or do you get compiler warnings then?
net/slirp.c static void net_slirp_register_poll_fd(int fd, void *opaque)
Quoted 13 lines
net/slirp.c +4
-2#ifdef WIN32 AioContext *ctxt =qemu_get_aio_context (); -qemu_socket_select (fd,event_notifier_get_handle (&ctxt->notifier), +if (WSAEventSelect (fd,event_notifier_get_handle (&ctxt->notifier),FD_READ |FD_ACCEPT |FD_CLOSE | -FD_CONNECT |FD_WRITE |FD_OOB ,NULL ); +FD_CONNECT |FD_WRITE |FD_OOB ) !=0 ) { +error_setg_win32 (&error_warn,WSAGetLastError (),"failed to WSAEventSelect()" ); + }#endif } > static void net_slirp_unregister_poll_fd(int fd, void *opaque)
Same here.
Quoted 9 lines
net/slirp.c +3
-1{#ifdef WIN32 -qemu_socket_unselect (fd,NULL ); +if (WSAEventSelect (fd,NULL ,0 ) !=0 ) { +error_setg_win32 (&error_warn,WSAGetLastError (),"failed to WSAEventSelect()" ); + }#endif }
Re: [PATCH v3 13/16] slirp: open-code qemu_socket_(un)select()
Hi
Quoted 20 lines · Stefan Berger
net/slirp.c static void net_slirp_register_poll_fd(int fd,
Quoted 15 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
We are about to make the QEMU socket API use file-descriptor space only, but libslirp gives us SOCKET as fd, still.
Signed-off-by Marc-André Lureau <[email protected]>--- net/slirp.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)net/slirp.c +0
-0@@ -251,16 +251,20 @@ static void net_slirp_register_poll_fd(int fd,void *opaque)
Quoted 4 lines
Shouldn't this int fd rather be a SOCKET s instead? Or do you get compiler warnings then?
Yes, you would get compiler warnings, because the "int fd" argument is from the slirp API, whether it is posix or win32.
Quoted 29 lines
net/slirp.c static void net_slirp_register_poll_fd(int fd, void *opaque)
Quoted 9 lines
net/slirp.c +3
-2#ifdef WIN32 AioContext *ctxt =qemu_get_aio_context (); -qemu_socket_select (fd,event_notifier_get_handle (&ctxt->notifier), +if (WSAEventSelect (fd,event_notifier_get_handle (&ctxt->notifier),FD_READ |FD_ACCEPT |FD_CLOSE | -FD_CONNECT |FD_WRITE |FD_OOB ,NULL ); +FD_CONNECT |FD_WRITE |FD_OOB ) !=0 ) { + error_setg_win32(&error_warn, WSAGetLastError(), "failed to
WSAEventSelect()");net/slirp.c static void net_slirp_register_poll_fd(int fd, void *opaque)
Quoted 4 lines
net/slirp.c +1
-0+ }#endif } > static void net_slirp_unregister_poll_fd(int fd, void *opaque)Same here.
net/slirp.c static void net_slirp_register_poll_fd(int fd, void *opaque)
Quoted 5 lines
net/slirp.c +2
-1{ #ifdef WIN32 -qemu_socket_unselect (fd,NULL ); + if (WSAEventSelect(fd, NULL, 0) != 0) { + error_setg_win32(&error_warn, WSAGetLastError(), "failed to
WSAEventSelect()");Quoted 4 lines
net/slirp.c +1
-0+ }#endif }
Re: [PATCH v3 13/16] slirp: open-code qemu_socket_(un)select()
Quoted 25 lines · Marc-André Lureau
On Mon, Mar 6, 2023 at 5:59 PM Stefan Berger <[email protected] <mailto:[email protected]>> wrote:
On 2/21/23 07:47, [email protected] <mailto:[email protected]> wrote: > From: Marc-André Lureau <[email protected] <mailto:[email protected]>> > > We are about to make the QEMU socket API use file-descriptor space only, > but libslirp gives us SOCKET as fd, still. > > Signed-off-by: Marc-André Lureau <[email protected] <mailto:[email protected]>> > --- > net/slirp.c | 10 +++++++--- > 1 file changed, 7 insertions(+), 3 deletions(-) > > diff --git a/net/slirp.c b/net/slirp.c > index a7c35778a6..c33b3e02e7 100644 > --- a/net/slirp.c > +++ b/net/slirp.c > @@ -251,16 +251,20 @@ static void net_slirp_register_poll_fd(int fd, void *opaque) Shouldn't this int fd rather be a SOCKET s instead? Or do you get compiler warnings then?
Quoted 1 line
Yes, you would get compiler warnings, because the "int fd" argument is from the slirp API, whether it is posix or win32.
Right, this is shared code.
Quoted 27 lines
> #ifdef WIN32
> AioContext *ctxt = qemu_get_aio_context();
>
> - qemu_socket_select(fd, event_notifier_get_handle(&ctxt->notifier),
> + if (WSAEventSelect(fd, event_notifier_get_handle(&ctxt->notifier),
> FD_READ | FD_ACCEPT | FD_CLOSE |
> - FD_CONNECT | FD_WRITE | FD_OOB, NULL);
> + FD_CONNECT | FD_WRITE | FD_OOB) != 0) {
> + error_setg_win32(&error_warn, WSAGetLastError(), "failed to WSAEventSelect()");
> + }
> #endif
> }
> > static void net_slirp_unregister_poll_fd(int fd, void *opaque)
Same here.
> {
> #ifdef WIN32
> - qemu_socket_unselect(fd, NULL);
> + if (WSAEventSelect(fd, NULL, 0) != 0) {
> + error_setg_win32(&error_warn, WSAGetLastError(), "failed to WSAEventSelect()");
> + }
> #endif
> }
>Re: [PATCH v3 13/16] slirp: open-code qemu_socket_(un)select()
In reply to [email protected]Quoted 28 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
We are about to make the QEMU socket API use file-descriptor space only, but libslirp gives us SOCKET as fd, still.
Signed-off-by Marc-André Lureau <[email protected]>--- net/slirp.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)net/slirp.c +4
-2@@ -251,16 +251,20 @@ static void net_slirp_register_poll_fd(int fd, void *opaque)#ifdef WIN32 AioContext *ctxt =qemu_get_aio_context (); -qemu_socket_select (fd,event_notifier_get_handle (&ctxt->notifier), +if (WSAEventSelect (fd,event_notifier_get_handle (&ctxt->notifier),FD_READ |FD_ACCEPT |FD_CLOSE | -FD_CONNECT |FD_WRITE |FD_OOB ,NULL ); +FD_CONNECT |FD_WRITE |FD_OOB ) !=0 ) { +error_setg_win32 (&error_warn,WSAGetLastError (),"failed to WSAEventSelect()" ); + }#endif }
net/slirp.c static void net_slirp_register_poll_fd(int fd, void *opaque) · line 270
Quoted 10 lines
net/slirp.c +3
-1static void net_slirp_unregister_poll_fd (int fd,void *opaque) {#ifdef WIN32 -qemu_socket_unselect (fd,NULL ); +if (WSAEventSelect (fd,NULL ,0 ) !=0 ) { +error_setg_win32 (&error_warn,WSAGetLastError (),"failed to WSAEventSelect()" ); + }#endif }
PATCH v3 14/16 win32: avoid mixing SOCKET and file descriptor space
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Until now, a win32 SOCKET handle is often cast to an int file descriptor, as this is what other OS use for sockets. When necessary, QEMU eventually queries whether it's a socket with the help of fd_is_socket(). However, there is no guarantee of conflict between the fd and SOCKET space. Such conflict would have surprising consequences, we shouldn't mix them.
Also, it is often forgotten that SOCKET must be closed with closesocket(), and not close().
Instead, let's make the win32 socket wrapper functions return and take a file descriptor, and let util/ wrappers do the fd/SOCKET conversion as necessary. A bit of adaptation is necessary in io/ as well.
Unfortunately, we can't drop closesocket() usage, despite _open_osfhandle() documentation claiming transfer of ownership, testing shows bad behaviour if you forget to call closesocket().
--- include/sysemu/os-win32.h | 4 +- io/channel-watch.c | 6 +- util/aio-win32.c | 9 +- util/oslib-win32.c | 219 ++++++++++++++++++++++++++++++++------ 4 files changed, 197 insertions(+), 41 deletions(-)
include/sysemu/os-win32.h +2 -2
@@ -146,10 +146,10 @@ static inline void qemu_funlockfile(FILE *f) }/* Helper for WSAEventSelect, to report errors */ -bool qemu_socket_select (SOCKET s,WSAEVENT hEventObject, +bool qemu_socket_select (int sockfd,WSAEVENT hEventObject,long lNetworkEvents,Error **errp); -bool qemu_socket_unselect (SOCKET s,Error **errp); +bool qemu_socket_unselect (int sockfd,Error **errp);/* We wrap all the sockets functions so that we can * set errno based on WSAGetLastError()
io/channel-watch.c +3 -3
@@ -275,13 +275,13 @@ GSource *qio_channel_create_fd_watch(QIOChannel *ioc,#ifdef CONFIG_WIN32 GSource *qio_channel_create_socket_watch (QIOChannel *ioc, -int socket, +int sockfd,GIOCondition condition) {GSource *source;QIOChannelSocketSource *ssource; -qemu_socket_select (socket, ioc->event, +qemu_socket_select (sockfd, ioc->event,FD_READ |FD_ACCEPT |FD_CLOSE |FD_CONNECT |FD_WRITE |FD_OOB ,NULL );
@@ -293,7 +293,7 @@ GSource *qio_channel_create_socket_watch(QIOChannel *ioc,object_ref (OBJECT (ioc)); ssource->condition = condition; - ssource->socket = socket; + ssource->socket =_get_osfhandle (sockfd); ssource->revents =0 ; ssource->fd.fd = (gintptr )ioc->event;
util/aio-win32.c +6 -3
@@ -73,15 +73,18 @@ void aio_set_fd_handler(AioContext *ctx, {AioHandler *old_node;AioHandler *node =NULL ; +SOCKET s;if (!fd_is_socket (fd)) {error_report ("fd=%d is not a socket, AIO implementation is missing" , fd);return ; } + s =_get_osfhandle (fd); +qemu_lockcnt_lock (&ctx->list_lock); QLIST_FOREACH(old_node, &ctx->aio_handlers, node) { -if (old_node->pfd.fd == fd && !old_node->deleted) { +if (old_node->pfd.fd == s && !old_node->deleted) {break ; } }
@@ -92,7 +95,7 @@ void aio_set_fd_handler(AioContext *ctx,/* Alloc and insert if it's not already there */ node =g_new0 (AioHandler,1 ); - node->pfd.fd = fd; + node->pfd.fd = s; node->pfd.events =0 ;if (node->io_read) {
@@ -120,7 +123,7 @@ void aio_set_fd_handler(AioContext *ctx,QLIST_INSERT_HEAD_RCU (&ctx->aio_handlers, node, node); event =event_notifier_get_handle (&ctx->notifier); -qemu_socket_select (node->pfd.fd, event, bitmask,NULL ); +qemu_socket_select (fd, event, bitmask,NULL ); }if (old_node) {aio_remove_fd_handler (ctx, old_node);
util/oslib-win32.c +186 -33
339 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH v3 14/16] win32: avoid mixing SOCKET and file descriptor space
Quoted 31 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Until now, a win32 SOCKET handle is often cast to an int file descriptor, as this is what other OS use for sockets. When necessary, QEMU eventually queries whether it's a socket with the help of
fd_is_socket(). However, there is no guarantee of conflict between the fd and SOCKET space. Such conflict would have surprising consequences, we shouldn't mix them.Also, it is often forgotten that SOCKET must be closed with
closesocket(), and notclose().Instead, let's make the win32 socket wrapper functions return and take a file descriptor, and let util/ wrappers do the fd/SOCKET conversion as necessary. A bit of adaptation is necessary in io/ as well.
Unfortunately, we can't drop
closesocket()usage, despite_open_osfhandle()documentation claiming transfer of ownership, testing shows bad behaviour if you forget to callclosesocket().Signed-off-by Marc-André Lureau <[email protected]>--- include/sysemu/os-win32.h | 4 +- io/channel-watch.c | 6 +- util/aio-win32.c | 9 +- util/oslib-win32.c | 219 ++++++++++++++++++++++++++++++++------ 4 files changed, 197 insertions(+), 41 deletions(-)#undef connect
util/oslib-win32.c +0
-0@@ -308,7 +315,13 @@ int qemu_connect_wrap(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
util/oslib-win32.c int qemu_connect_wrap(int sockfd, const struct sockaddr *addr, · line 324
Quoted 10 lines
util/oslib-win32.c +7
-1{int ret; - ret =connect (sockfd, addr, addrlen); +SOCKET s =_get_osfhandle (sockfd); + +if (s ==INVALID_SOCKET ) { +return -1 ; + } + + ret =connect (s, addr, addrlen);
Previously you passed int sockfd and now you convert this sockfd to a SOCKET s and you can pass this as an alternative? Or was sockfd before this patch a SOCKET??
Stefan
Re: [PATCH v3 14/16] win32: avoid mixing SOCKET and file descriptor space
Hi
Quoted 32 lines · Stefan Berger
Quoted 27 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Until now, a win32 SOCKET handle is often cast to an int file descriptor, as this is what other OS use for sockets. When necessary, QEMU eventually queries whether it's a socket with the help of
fd_is_socket(). However, there is no guarantee of conflict between the fd and SOCKET space. Such conflict would have surprising consequences, we shouldn't mix them.Also, it is often forgotten that SOCKET must be closed with
closesocket(), and notclose().Instead, let's make the win32 socket wrapper functions return and take a file descriptor, and let util/ wrappers do the fd/SOCKET conversion as necessary. A bit of adaptation is necessary in io/ as well.
Unfortunately, we can't drop
closesocket()usage, despite_open_osfhandle()documentation claiming transfer of ownership, testing shows bad behaviour if you forget to callclosesocket().Signed-off-by Marc-André Lureau <[email protected]>--- include/sysemu/os-win32.h | 4 +- io/channel-watch.c | 6 +- util/aio-win32.c | 9 +- util/oslib-win32.c | 219 ++++++++++++++++++++++++++++++++------ 4 files changed, 197 insertions(+), 41 deletions(-)
Quoted 16 lines
util/oslib-win32.c int qemu_connect_wrap(int sockfd, const struct sockaddr *addr, · line 324
Quoted 13 lines
#undef connect
util/oslib-win32.c +7
-1@@ -308,7 +315,13 @@ int qemu_connect_wrap(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {int ret; - ret =connect (sockfd, addr, addrlen); +SOCKET s =_get_osfhandle (sockfd); + +if (s ==INVALID_SOCKET ) { +return -1 ; + } + + ret =connect (s, addr, addrlen);Previously you passed int sockfd and now you convert this sockfd to a SOCKET s and you can pass this as an alternative? Or was sockfd before this patch a SOCKET??
yes, sockfd was in fact a SOCKET.
Previous to this patch, a SOCKET is cast to int, as a fake fd, and back to SOCKET as necessary. The whole point of this patch is to avoid mixing SOCKET & fd space, instead a SOCKET is associated with a real CRT fd.
thanks
Signature · 1 line
Marc-André Lureau
Re: [PATCH v3 14/16] win32: avoid mixing SOCKET and file descriptor space
In reply to [email protected]Quoted 16 lines · On 2/21/23 07:47, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Until now, a win32 SOCKET handle is often cast to an int file descriptor, as this is what other OS use for sockets. When necessary, QEMU eventually queries whether it's a socket with the help of
fd_is_socket(). However, there is no guarantee of conflict between the fd and SOCKET space. Such conflict would have surprising consequences, we shouldn't mix them.Also, it is often forgotten that SOCKET must be closed with
closesocket(), and notclose().Instead, let's make the win32 socket wrapper functions return and take a file descriptor, and let util/ wrappers do the fd/SOCKET conversion as necessary. A bit of adaptation is necessary in io/ as well.
Quoted 5 lines
Unfortunately, we can't drop
closesocket()usage, despite_open_osfhandle()documentation claiming transfer of ownership, testing shows bad behaviour if you forget to callclosesocket().Signed-off-by Marc-André Lureau <[email protected]>
PATCH v3 15/16 os-posix: remove useless ioctlsocket() define
In reply to [email protected]From: Marc-André Lureau <[email protected]>
The API is specific to win32.
--- include/sysemu/os-posix.h | 1 - 1 file changed, 1 deletion(-)
include/sysemu/os-posix.h +0 -1
@@ -52,7 +52,6 @@ void os_setup_post(void);int os_mlock (void );#define closesocket (s) close(s) -#define ioctlsocket (s, r, v) ioctl(s, r, v)int os_set_daemonize (bool d);bool is_daemonized (void );
Signature · 1 line
2.39.2
Re: [PATCH v3 15/16] os-posix: remove useless ioctlsocket() define
Quoted 6 lines · On 2/21/23 13:48, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
The API is specific to win32.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Daniel P. Berrangé <[email protected]>
The idea was to use for socket ioctls that are common to POSIX and Windows, but it turns out there's no such usecase.
Paolo
Quoted 16 lines
--- include/sysemu/os-posix.h | 1 - 1 file changed, 1 deletion(-)include/sysemu/os-posix.h +0
-1@@ -52,7 +52,6 @@ void os_setup_post(void);int os_mlock (void );#define closesocket (s) close(s) -#define ioctlsocket (s, r, v) ioctl(s, r, v)int os_set_daemonize (bool d);bool is_daemonized (void );
PATCH v3 16/16 win32: replace closesocket() with close() wrapper
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Use a close() wrapper instead, so that we don't need to worry about closesocket() vs close() anymore, let's hope.
--- include/sysemu/os-posix.h | 2 -- include/sysemu/os-win32.h | 8 +++--- backends/tpm/tpm_emulator.c | 6 ++-- crypto/afalg.c | 6 ++-- hw/hyperv/syndbg.c | 4 +-- io/channel-socket.c | 10 +++---- net/dgram.c | 14 +++++----- net/socket.c | 22 +++++++-------- tests/qtest/libqtest.c | 8 +++--- tests/qtest/microbit-test.c | 2 +- tests/qtest/netdev-socket.c | 10 +++---- tests/unit/socket-helpers.c | 8 +++--- util/oslib-win32.c | 56 +++++++++++++++++++------------------ util/qemu-sockets.c | 22 +++++++-------- 14 files changed, 89 insertions(+), 89 deletions(-)
include/sysemu/os-posix.h +0 -2
@@ -51,8 +51,6 @@ void os_daemonize(void);void os_setup_post (void );int os_mlock (void ); -#define closesocket (s) close(s) -int os_set_daemonize (bool d);bool is_daemonized (void );
include/sysemu/os-win32.h +4 -4
@@ -155,6 +155,10 @@ bool qemu_socket_unselect(int sockfd, Error **errp);* set errno based on WSAGetLastError() */ +#undef close +#define close qemu_close_wrap +int qemu_close_wrap (int fd); +#undef connect#define connect qemu_connect_wrap int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
@@ -186,10 +190,6 @@ int qemu_shutdown_wrap(int sockfd, int how);#define ioctlsocket qemu_ioctlsocket_wrapint qemu_ioctlsocket_wrap (int fd,int req,void *val); -#undef closesocket -#define closesocket qemu_closesocket_wrap -int qemu_closesocket_wrap (int fd); -#undef getsockopt#define getsockopt qemu_getsockopt_wrap int qemu_getsockopt_wrap(int sockfd, int level, int optname,
backends/tpm/tpm_emulator.c +3 -3
@@ -573,13 +573,13 @@ static int tpm_emulator_prepare_data_fd(TPMEmulator *tpm_emu) goto err_exit; } -closesocket (fds[1 ]); +close (fds[1 ]);return 0 ; err_exit: -closesocket (fds[0 ]); -closesocket (fds[1 ]); +close (fds[0 ]); +close (fds[1 ]);return -1 ; }
crypto/afalg.c +3 -3
@@ -59,7 +59,7 @@ qcrypto_afalg_socket_bind(const char *type, const char *name,if (bind (sbind, (const struct sockaddr *)&salg,sizeof (salg)) !=0 ) {error_setg_errno (errp, errno,"Failed to bind socket" ); -closesocket (sbind); +close (sbind);return -1 ; }
@@ -105,11 +105,11 @@ void qcrypto_afalg_comm_free(QCryptoAFAlg *afalg) }if (afalg->tfmfd !=-1 ) { -closesocket (afalg->tfmfd); +close (afalg->tfmfd); }if (afalg->opfd !=-1 ) { -closesocket (afalg->opfd); +close (afalg->opfd); }g_free (afalg);
hw/hyperv/syndbg.c +2 -2
@@ -340,7 +340,7 @@ static void hv_syndbg_realize(DeviceState *dev, Error **errp) syndbg->servaddr.sin_family =AF_INET ;if (connect (syndbg->socket, (struct sockaddr *)&syndbg->servaddr,sizeof (syndbg->servaddr)) <0 ) { -closesocket (syndbg->socket); +close (syndbg->socket);error_setg (errp,"%s failed to connect to socket" ,TYPE_HV_SYNDBG );return ; }
@@ -357,7 +357,7 @@ static void hv_syndbg_unrealize(DeviceState *dev)if (syndbg->socket >0 ) {qemu_set_fd_handler (syndbg->socket,NULL ,NULL ,NULL ); -closesocket (syndbg->socket); +close (syndbg->socket); } }
io/channel-socket.c +5 -5
@@ -159,7 +159,7 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,trace_qio_channel_socket_connect_complete (ioc, fd);if (qio_channel_socket_set_fd (ioc, fd, errp) <0 ) { -closesocket (fd); +close (fd);return -1 ; }
@@ -233,7 +233,7 @@ int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,trace_qio_channel_socket_listen_complete (ioc, fd);if (qio_channel_socket_set_fd (ioc, fd, errp) <0 ) { -closesocket (fd); +close (fd);return -1 ; }qio_channel_set_feature (QIO_CHANNEL (ioc),QIO_CHANNEL_FEATURE_LISTEN );
@@ -310,7 +310,7 @@ int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,trace_qio_channel_socket_dgram_complete (ioc, fd);if (qio_channel_socket_set_fd (ioc, fd, errp) <0 ) { -closesocket (fd); +close (fd);return -1 ; }
@@ -444,7 +444,7 @@ static void qio_channel_socket_finalize(Object *obj)#ifdef WIN32 qemu_socket_unselect (ioc->fd,NULL );#endif -closesocket (ioc->fd); +close (ioc->fd); ioc->fd =-1 ; } }
@@ -852,7 +852,7 @@ qio_channel_socket_close(QIOChannel *ioc,socket_listen_cleanup (sioc->fd, errp); } -if (closesocket (sioc->fd) <0 ) { +if (close (sioc->fd) <0 ) { sioc->fd =-1 ;error_setg_errno (&err, errno,"Unable to close socket" );error_propagate (errp, err);
net/dgram.c +7 -7
@@ -230,7 +230,7 @@ static int net_dgram_mcast_create(struct sockaddr_in *mcastaddr,return fd; fail:if (fd >=0 ) { -closesocket (fd); +close (fd); }return -1 ; }
@@ -352,7 +352,7 @@ static int net_dgram_mcast_init(NetClientState *peer,if (convert_host_port (saddr, local->u.inet.host, local->u.inet.port, errp) <0 ) {g_free (saddr); -closesocket (fd); +close (fd);return -1 ; }
@@ -360,14 +360,14 @@ static int net_dgram_mcast_init(NetClientState *peer,if (saddr->sin_addr.s_addr ==0 ) {error_setg (errp,"can't setup multicast destination address" );g_free (saddr); -closesocket (fd); +close (fd);return -1 ; }/* clone dgram socket */ newfd =net_dgram_mcast_create (saddr,NULL , errp);if (newfd <0 ) {g_free (saddr); -closesocket (fd); +close (fd);return -1 ; }/* clone newfd to fd, close newfd */
@@ -494,14 +494,14 @@ int net_init_dgram(const Netdev *netdev, const char *name,if (ret <0 ) {error_setg_errno (errp, errno,"can't set socket option SO_REUSEADDR" ); -closesocket (fd); +close (fd);return -1 ; } ret =bind (fd, (struct sockaddr *)&laddr_in,sizeof (laddr_in));if (ret <0 ) {error_setg_errno (errp, errno,"can't bind ip=%s to socket" ,inet_ntoa (laddr_in.sin_addr)); -closesocket (fd); +close (fd);return -1 ; }qemu_socket_set_nonblock (fd);
@@ -548,7 +548,7 @@ int net_init_dgram(const Netdev *netdev, const char *name,if (ret <0 ) {error_setg_errno (errp, errno,"can't bind unix=%s to socket" , laddr_un.sun_path); -closesocket (fd); +close (fd);return -1 ; }qemu_socket_set_nonblock (fd);
net/socket.c +11 -11
@@ -172,7 +172,7 @@ static void net_socket_send(void *opaque)if (s->listen_fd !=-1 ) {qemu_set_fd_handler (s->listen_fd, net_socket_accept,NULL , s); } -closesocket (s->fd); +close (s->fd); s->fd =-1 ;net_socket_rs_init (&s->rs, net_socket_rs_finalize, false);
@@ -299,7 +299,7 @@ static int net_socket_mcast_create(struct sockaddr_in *mcastaddr,return fd; fail:if (fd >=0 ) -closesocket (fd); +close (fd);return -1 ; }
@@ -314,7 +314,7 @@ static void net_socket_cleanup(NetClientState *nc) }if (s->listen_fd !=-1 ) {qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); -closesocket (s->listen_fd); +close (s->listen_fd); s->listen_fd =-1 ; } }
@@ -399,7 +399,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,return s; err: -closesocket (fd); +close (fd);return NULL ; }
@@ -456,7 +456,7 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,if (getsockopt (fd,SOL_SOCKET ,SO_TYPE , (char *)&so_type, (socklen_t *)&optlen)<0 ) {error_setg (errp,"can't get socket option SO_TYPE" ); -closesocket (fd); +close (fd);return NULL ; }switch (so_type) {
@@ -468,7 +468,7 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,default :error_setg (errp,"socket type=%d for fd=%d must be either" " SOCK_DGRAM or SOCK_STREAM" , so_type, fd); -closesocket (fd); +close (fd); }return NULL ; }
@@ -526,13 +526,13 @@ static int net_socket_listen_init(NetClientState *peer,if (ret <0 ) {error_setg_errno (errp, errno,"can't bind ip=%s to socket" ,inet_ntoa (saddr.sin_addr)); -closesocket (fd); +close (fd);return -1 ; } ret =listen (fd,0 );if (ret <0 ) {error_setg_errno (errp, errno,"can't listen on socket" ); -closesocket (fd); +close (fd);return -1 ; }
@@ -579,7 +579,7 @@ static int net_socket_connect_init(NetClientState *peer,break ; }else {error_setg_errno (errp, errno,"can't connect socket" ); -closesocket (fd); +close (fd);return -1 ; } }else {
@@ -671,14 +671,14 @@ static int net_socket_udp_init(NetClientState *peer,if (ret <0 ) {error_setg_errno (errp, errno,"can't set socket option SO_REUSEADDR" ); -closesocket (fd); +close (fd);return -1 ; } ret =bind (fd, (struct sockaddr *)&laddr,sizeof (laddr));if (ret <0 ) {error_setg_errno (errp, errno,"can't bind ip=%s to socket" ,inet_ntoa (laddr.sin_addr)); -closesocket (fd); +close (fd);return -1 ; }qemu_socket_set_nonblock (fd);
tests/qtest/libqtest.c +4 -4
@@ -124,7 +124,7 @@ static int socket_accept(int sock) (void *)&timeout, sizeof(timeout))) {fprintf (stderr,"%s failed to set SO_RCVTIMEO: %s\n" , __func__,strerror (errno)); -closesocket (sock); +close (sock);return -1 ; }
@@ -135,7 +135,7 @@ static int socket_accept(int sock)if (ret ==-1 ) {fprintf (stderr,"%s failed: %s\n" , __func__,strerror (errno)); } -closesocket (sock); +close (sock);return ret; }
@@ -564,8 +564,8 @@ void qtest_quit(QTestState *s)qtest_remove_abrt_handler (s);qtest_kill_qemu (s); -closesocket (s->fd); -closesocket (s->qmp_fd); +close (s->fd); +close (s->qmp_fd);g_string_free (s->rx, true);for (GList *it = s->pending_events; it !=NULL ; it = it->next) {
tests/qtest/microbit-test.c +1 -1
@@ -107,7 +107,7 @@ static void test_nrf51_uart(void)g_assert_true (recv (sock_fd, s,10 ,0 ) ==5 );g_assert_true (memcmp (s,"world" ,5 ) ==0 ); -closesocket (sock_fd); +close (sock_fd);qtest_quit (qts); }
tests/qtest/netdev-socket.c +5 -5
@@ -95,7 +95,7 @@ static int inet_get_free_port_multiple(int nb, int *port, bool ipv6) nb = i;for (i =0 ; i < nb; i++) { -closesocket (sock[i]); +close (sock[i]); }return nb;
@@ -262,8 +262,8 @@ static void test_stream_fd(void)qtest_quit (qts1);qtest_quit (qts0); -closesocket (sock[0 ]); - closesocket(sock[1]); +close (sock[0 ]); + close(sock[1]); }#endif
@@ -388,8 +388,8 @@ static void test_dgram_fd(void)qtest_quit (qts1);qtest_quit (qts0); -closesocket (sv[0 ]); - closesocket(sv[1]); +close (sv[0 ]); + close(sv[1]); }#endif
tests/unit/socket-helpers.c +4 -4
@@ -117,13 +117,13 @@ static int socket_can_bind_connect(const char *hostname, int family) cleanup:if (afd !=-1 ) { -closesocket (afd); +close (afd); }if (cfd !=-1 ) { -closesocket (cfd); +close (cfd); }if (lfd !=-1 ) { -closesocket (lfd); +close (lfd); }if (res) {freeaddrinfo (res);
@@ -160,7 +160,7 @@ void socket_check_afunix_support(bool *has_afunix)int fd; fd =socket (PF_UNIX ,SOCK_STREAM ,0 ); -closesocket (fd); +close (fd);#ifdef _WIN32 *has_afunix = (fd != (int )INVALID_SOCKET );
util/oslib-win32.c +29 -27
81 lines not on this page · raw message
util/qemu-sockets.c +11 -11
@@ -326,7 +326,7 @@ static int inet_listen_saddr(InetSocketAddress *saddr,* recover from this situation, so we need to recreate the * socket to allow bind attempts for subsequent ports: */ -closesocket (slisten); +close (slisten); slisten =-1 ; } }
@@ -337,7 +337,7 @@ static int inet_listen_saddr(InetSocketAddress *saddr, listen_failed: saved_errno = errno;if (slisten >=0 ) { -closesocket (slisten); +close (slisten); }freeaddrinfo (res); errno = saved_errno;
@@ -380,7 +380,7 @@ static int inet_connect_addr(const InetSocketAddress *saddr,if (rc <0 ) {error_setg_errno (errp, errno,"Failed to connect to '%s:%s'" , saddr->host, saddr->port); -closesocket (sock); +close (sock);return -1 ; }
@@ -483,7 +483,7 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)if (ret <0 ) {error_setg_errno (errp, errno,"Unable to set KEEPALIVE" ); -closesocket (sock); +close (sock);return -1 ; } }
@@ -580,7 +580,7 @@ static int inet_dgram_saddr(InetSocketAddress *sraddr, err:if (sock !=-1 ) { -closesocket (sock); +close (sock); }if (local) {freeaddrinfo (local);
@@ -777,7 +777,7 @@ static int vsock_connect_addr(const VsockSocketAddress *vaddr,if (rc <0 ) {error_setg_errno (errp, errno,"Failed to connect to '%s:%s'" , vaddr->cid, vaddr->port); -closesocket (sock); +close (sock);return -1 ; }
@@ -814,13 +814,13 @@ static int vsock_listen_saddr(VsockSocketAddress *vaddr,if (bind (slisten, (const struct sockaddr *)&svm,sizeof (svm)) !=0 ) {error_setg_errno (errp, errno,"Failed to bind socket" ); -closesocket (slisten); +close (slisten);return -1 ; }if (listen (slisten, num) !=0 ) {error_setg_errno (errp, errno,"Failed to listen on socket" ); -closesocket (slisten); +close (slisten);return -1 ; }return slisten;
@@ -978,7 +978,7 @@ static int unix_listen_saddr(UnixSocketAddress *saddr, err:g_free (pathbuf); -closesocket (sock); +close (sock);return -1 ; }
@@ -1041,7 +1041,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)return sock; err: -closesocket (sock); +close (sock);return -1 ; }
@@ -1238,7 +1238,7 @@ int socket_listen(SocketAddress *addr, int num, Error **errp)*/ if (listen (fd, num) !=0 ) {error_setg_errno (errp, errno,"Failed to listen on fd socket" ); -closesocket (fd); +close (fd);return -1 ; }break ;
Signature · 1 line
2.39.2
Re: [PATCH v3 16/16] win32: replace closesocket() with close() wrapper
Quoted 82 lines · On 2/21/23 07:48, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Use a
close()wrapper instead, so that we don't need to worry aboutclosesocket()vsclose()anymore, let's hope.Signed-off-by Marc-André Lureau <[email protected]>util/oslib-win32.c +24
-24@@ -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; }
util/oslib-win32.c int qemu_closesocket_wrap(int fd) · line 423
Quoted 10 lines
util/oslib-win32.c +5
-3- 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.
Hi
Quoted 20 lines · On Tue, Feb 21, 2023 at 4:48 PM <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
Hi,
A win32 SOCKET handle is often cast to an int file descriptor, as this is what
other OS use for sockets. When necessary, QEMU eventually queries whether it's a
socket with the help offd_is_socket(). However, there is no guarantee of conflict between the fd and SOCKET space. Such conflict would have surprising
consequences. We can fix this by using FDs only.After fixing a few missed
closesocket(), this patch series makes the win32 socket API wrappers take FDs. It finally get rid ofclosesocket()usage by using
aclose()wrapper instead. (note that fdopen/fclose would not be enough either
to close the underlying socket appropriately)
Quoted 7 lines
v3:
- fixclosesocket()to preventCloseHandle()fromclose()
- fix directclosesocket()usage (#undef closesocket before)
- add a test for&error_warn
- add r-b tags
ping (I am missing reviews, thanks)
Quoted 65 lines
v2:
- add clean up patch "util: dropqemu_fork()"
- add a "&error_warn", to help with basic error reporting
- fix errno handling after_get_osfhandle()
- introduce qemu_socket_(un)select() helpers
- add patch "aio_set_fd_handler()only supports SOCKET"
- add meson slirp.wrap RFC
- various misc cleanups
- add r-b tagsMarc-André Lureau (16):
util: dropqemu_fork()
tests: useclosesocket()
io: useclosesocket()
tests: add test-error-report
error: add global&error_warndestination win32/socket: introduceqemu_socket_select()helper win32/socket: introduceqemu_socket_unselect()helper
aio: makeaio_set_fd_poll()static toaio-posix.caio/win32:aio_set_fd_handler()only supports SOCKET
RFC: build-sys: add slirp.wrap
main-loop: removeqemu_fd_register(), win32/slirp/socket specific
slirp: unregister the win32 SOCKET
slirp: open-code qemu_socket_(un)select()
win32: avoid mixing SOCKET and file descriptor space
os-posix: remove uselessioctlsocket()define
win32: replaceclosesocket()withclose()wrapperinclude/block/aio.h | 8 -- include/qapi/error.h | 6 + include/qemu/main-loop.h | 2 - include/qemu/osdep.h | 14 -- include/sysemu/os-posix.h | 3 - include/sysemu/os-win32.h | 15 ++- backends/tpm/tpm_emulator.c | 6 +- crypto/afalg.c | 6 +- hw/hyperv/syndbg.c | 4 +- io/channel-socket.c | 8 +- io/channel-watch.c | 10 +- net/dgram.c | 14 +- net/slirp.c | 16 ++- net/socket.c | 22 +-- tests/qtest/libqtest.c | 8 +- tests/qtest/microbit-test.c | 2 +- tests/qtest/netdev-socket.c | 10 +- tests/unit/socket-helpers.c | 2 +- tests/unit/test-error-report.c | 139 +++++++++++++++++++ util/aio-posix.c | 6 +- util/aio-win32.c | 23 ++-- util/error.c | 10 +- util/main-loop.c | 11 -- util/oslib-posix.c | 70 ---------- util/oslib-win32.c | 240 ++++++++++++++++++++++++++++----- util/qemu-sockets.c | 22 +-- .gitignore | 2 + subprojects/slirp.wrap | 6 + tests/unit/meson.build | 1 + 29 files changed, 461 insertions(+), 225 deletions(-) create mode 100644 tests/unit/test-error-report.c create mode 100644 subprojects/slirp.wrap--
2.39.2
Quoted 9 lines · Marc-André Lureau
v3:
- fixclosesocket()to preventCloseHandle()fromclose()
- fix directclosesocket()usage (#undef closesocket before)
- add a test for&error_warn
- add r-b tagsping (I am missing reviews, thanks)
I'm going to queue this series today if that's fine for you---thanks for reminding!
Paolo
Hi Paolo
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Marc-André Lureau
v3:
- fixclosesocket()to preventCloseHandle()fromclose()
- fix directclosesocket()usage (#undef closesocket before)
- add a test for&error_warn
- add r-b tags
Quoted 6 lines
Quoted 1 line
ping (I am missing reviews, thanks)
I'm going to queue this series today if that's fine for you---thanks for reminding!
Great, thanks! (I suppose you'll drop "RFC: build-sys: add slirp.wrap", and perhaps queue the other meson/wrap series instead)
Quoted 1 line · Marc-André Lureau
Great, thanks! (I suppose you'll drop "RFC: build-sys: add slirp.wrap", and perhaps queue the other meson/wrap series instead)
I don't have time to test the dtc fallback in CI, so I'll queue only the first three patches and note it as experimental in the release notes.
In the next release we'll probably have pip support in configure, so perhaps we can make the --wrap-mode option conditional.
Paolo