win32: do not mix SOCKET and fd space
Subsystems (3)
v2 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)
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 (15):
util: drop qemu_fork()
tests: use closesocket()
io: use closesocket()
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 +- 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 | 202 ++++++++++++++++++++++++++++++------ util/qemu-sockets.c | 22 ++-- .gitignore | 2 + subprojects/slirp.wrap | 6 ++ 27 files changed, 289 insertions(+), 219 deletions(-) create mode 100644 subprojects/slirp.wrap
Signature · 1 line
2.39.1
PATCH v2 1/15 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.1
Re: [PATCH v2 01/15] util: drop qemu_fork()
Quoted 6 lines · On Tue, Feb 21, 2023 at 01:48:45AM +0400, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Fortunately,
qemu_fork()is no longer used since commita95570e3e4d6("io/command: use glib GSpawn, instead of open-coding fork/exec"). (GSpawn usesposix_spawn()whenever possible instead)
Quoted 6 lines
Signed-off-by Marc-André Lureau <[email protected]>--- include/qemu/osdep.h | 14 --------- util/oslib-posix.c | 70 -------------------------------------------- util/oslib-win32.c | 9 ------ 3 files changed, 93 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: 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 :|
PATCH v2 2/15 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.1
PATCH v2 3/15 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.1
PATCH v2 4/15 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 ++++++ util/error.c | 10 +++++++--- 2 files changed, 13 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.
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.1
PATCH v2 5/15 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.1
PATCH v2 6/15 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.1
PATCH v2 7/15 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.1
PATCH v2 8/15 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.1
PATCH v2 9/15 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.1
PATCH v2 10/15 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.1
PATCH v2 11/15 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.1
PATCH v2 12/15 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.1
PATCH v2 13/15 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 | 178 ++++++++++++++++++++++++++++++++------ 4 files changed, 164 insertions(+), 33 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 +153 -25
300 lines not on this page · raw message
Signature · 1 line
2.39.1
PATCH v2 14/15 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.1
Re: [PATCH v2 14/15] os-posix: remove useless ioctlsocket() define
Quoted 8 lines · On Tue, Feb 21, 2023 at 01:48:58AM +0400, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
The API is specific to win32.
Signed-off-by Marc-André Lureau <[email protected]>--- include/sysemu/os-posix.h | 1 - 1 file changed, 1 deletion(-)
With regards,
Daniel
Signature · 3 lines
|: 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 :|
PATCH v2 15/15 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 | 21 +++++++++++---------- util/qemu-sockets.c | 22 +++++++++++----------- 14 files changed, 71 insertions(+), 72 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 +11 -10
@@ -457,16 +457,15 @@ int qemu_ioctlsocket_wrap(int fd, int req, void *val) } -#undef closesocket -int qemu_closesocket_wrap (int fd) +#undef close +int qemu_close_wrap (int fd) { +SOCKET s =INVALID_SOCKET ;int ret; -SOCKET s =_get_osfhandle (fd); -if (s ==INVALID_SOCKET ) { -return -1 ; +if (fd_is_socket (fd)) { + s =_get_osfhandle (fd); } -/* * close() must be called before closesocket(), otherwise close() returns an * error and sets EBADF.
@@ -476,10 +475,12 @@ int qemu_closesocket_wrap(int fd)return ret; } -/* closesocket() is required, even after close()! */ - ret =closesocket (s); -if (ret <0 ) { - errno =socket_error (); +if (s !=INVALID_SOCKET ) { +/* closesocket() is required, even after close()! */ + ret =closesocket (s); +if (ret <0 ) { + errno =socket_error (); + } }return ret;
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.1