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>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
+#include "qemu/typedefs.h"
#ifdef HAVE_AFUNIX_H
#include <afunix.h>
#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()
*/
}
}
#ifdef WIN32
- WSAEventSelect(ioc->fd, NULL, 0);
+ qemu_socket_select(ioc->fd, NULL, 0, NULL);
#endif
closesocket(ioc->fd);
ioc->fd = -1;
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);
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));
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);
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,
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);
}
}
+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)