aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc-André Lureau <marcandre.lureau@redhat.com>2023-05-15 17:24:40 +0400
committerMarc-André Lureau <marcandre.lureau@redhat.com>2023-05-28 13:08:25 +0400
commitd89f30b4df13dfe389a4d6cf8a30b2f87c4c166e (patch)
tree7b4e7d66884f18a1da42936e9beece03b21f242f
parent0b31e48d62c8f3a282d1bffbcc0e90200df9f9f0 (diff)
downloadqemu-d89f30b4df13dfe389a4d6cf8a30b2f87c4c166e.zip
qemu-d89f30b4df13dfe389a4d6cf8a30b2f87c4c166e.tar.gz
qemu-d89f30b4df13dfe389a4d6cf8a30b2f87c4c166e.tar.bz2
win32: wrap socket close() with an exception handler
Since commit abe34282 ("win32: avoid mixing SOCKET and file descriptor space"), we set HANDLE_FLAG_PROTECT_FROM_CLOSE on the socket FD, to prevent closing the HANDLE with CloseHandle. This raises an exception which under gdb is fatal, and qemu exits. Let's catch the expected error instead. Note: this appears to work, but the mingw64 macro is not well documented or tested, and it's not obvious how it is meant to be used. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20230515132440.1025315-1-marcandre.lureau@redhat.com>
-rw-r--r--include/sysemu/os-win32.h4
-rw-r--r--util/oslib-win32.c23
2 files changed, 21 insertions, 6 deletions
diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
index 15c296e..65f6c9e 100644
--- a/include/sysemu/os-win32.h
+++ b/include/sysemu/os-win32.h
@@ -259,6 +259,10 @@ ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags);
ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *addr, socklen_t *addrlen);
+EXCEPTION_DISPOSITION
+win32_close_exception_handler(struct _EXCEPTION_RECORD*, void*,
+ struct _CONTEXT*, void*);
+
#ifdef __cplusplus
}
#endif
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index a986387..fafbab8 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -479,6 +479,13 @@ int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
return ret;
}
+EXCEPTION_DISPOSITION
+win32_close_exception_handler(struct _EXCEPTION_RECORD*,
+ void*, struct _CONTEXT*, void*)
+{
+ return EXCEPTION_EXECUTE_HANDLER;
+}
+
#undef close
int qemu_close_socket_osfhandle(int fd)
{
@@ -504,12 +511,16 @@ int qemu_close_socket_osfhandle(int fd)
return -1;
}
- /*
- * close() returns EBADF since we PROTECT_FROM_CLOSE the underlying handle,
- * but the FD is actually freed
- */
- if (close(fd) < 0 && errno != EBADF) {
- return -1;
+ __try1(win32_close_exception_handler) {
+ /*
+ * close() returns EBADF since we PROTECT_FROM_CLOSE the underlying
+ * handle, but the FD is actually freed
+ */
+ if (close(fd) < 0 && errno != EBADF) {
+ return -1;
+ }
+ }
+ __except1 {
}
if (!SetHandleInformation((HANDLE)s, flags, flags)) {