win32/socket: introduce qemu_socket_select() helper

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 <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-7-marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2023-02-21 16:47:51 +04:00
parent 3ffef1a55c
commit f5fd677ae7
6 changed files with 30 additions and 10 deletions

View file

@ -29,6 +29,7 @@
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include "qemu/typedefs.h"
#ifdef HAVE_AFUNIX_H
#include <afunix.h>
@ -164,6 +165,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()
*/

View file

@ -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);

View file

@ -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));

View file

@ -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);

View file

@ -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,

View file

@ -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 connect
int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
socklen_t addrlen)