win32: add qemu_close_socket_osfhandle()
Close the given file descriptor, but returns the underlying SOCKET. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Message-Id: <20230320133643.1618437-2-marcandre.lureau@redhat.com>
This commit is contained in:
parent
aa9e7fa468
commit
f3ab43accf
|
@ -171,10 +171,21 @@ bool qemu_socket_select(int sockfd, WSAEVENT hEventObject,
|
||||||
|
|
||||||
bool qemu_socket_unselect(int sockfd, Error **errp);
|
bool qemu_socket_unselect(int sockfd, Error **errp);
|
||||||
|
|
||||||
/* We wrap all the sockets functions so that we can
|
/* We wrap all the sockets functions so that we can set errno based on
|
||||||
* set errno based on WSAGetLastError()
|
* WSAGetLastError(), and use file-descriptors instead of SOCKET.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* qemu_close_socket_osfhandle:
|
||||||
|
* @fd: a file descriptor associated with a SOCKET
|
||||||
|
*
|
||||||
|
* Close only the C run-time file descriptor, leave the SOCKET opened.
|
||||||
|
*
|
||||||
|
* Returns zero on success. On error, -1 is returned, and errno is set to
|
||||||
|
* indicate the error.
|
||||||
|
*/
|
||||||
|
int qemu_close_socket_osfhandle(int fd);
|
||||||
|
|
||||||
#undef close
|
#undef close
|
||||||
#define close qemu_close_wrap
|
#define close qemu_close_wrap
|
||||||
int qemu_close_wrap(int fd);
|
int qemu_close_wrap(int fd);
|
||||||
|
|
|
@ -479,16 +479,11 @@ int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#undef close
|
#undef close
|
||||||
int qemu_close_wrap(int fd)
|
int qemu_close_socket_osfhandle(int fd)
|
||||||
{
|
{
|
||||||
int ret;
|
SOCKET s = _get_osfhandle(fd);
|
||||||
DWORD flags = 0;
|
DWORD flags = 0;
|
||||||
SOCKET s = INVALID_SOCKET;
|
|
||||||
|
|
||||||
if (fd_is_socket(fd)) {
|
|
||||||
s = _get_osfhandle(fd);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we were to just call _close on the descriptor, it would close the
|
* If we were to just call _close on the descriptor, it would close the
|
||||||
|
@ -508,29 +503,39 @@ int qemu_close_wrap(int fd)
|
||||||
errno = EACCES;
|
errno = EACCES;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ret = close(fd);
|
|
||||||
|
|
||||||
if (s != INVALID_SOCKET && !SetHandleInformation((HANDLE)s, flags, flags)) {
|
|
||||||
errno = EACCES;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* close() returns EBADF since we PROTECT_FROM_CLOSE the underlying handle,
|
* close() returns EBADF since we PROTECT_FROM_CLOSE the underlying handle,
|
||||||
* but the FD is actually freed
|
* but the FD is actually freed
|
||||||
*/
|
*/
|
||||||
if (ret < 0 && (s == INVALID_SOCKET || errno != EBADF)) {
|
if (close(fd) < 0 && errno != EBADF) {
|
||||||
return ret;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s != INVALID_SOCKET) {
|
if (!SetHandleInformation((HANDLE)s, flags, flags)) {
|
||||||
|
errno = EACCES;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int qemu_close_wrap(int fd)
|
||||||
|
{
|
||||||
|
SOCKET s = INVALID_SOCKET;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if (!fd_is_socket(fd)) {
|
||||||
|
return close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
s = _get_osfhandle(fd);
|
||||||
|
qemu_close_socket_osfhandle(fd);
|
||||||
|
|
||||||
ret = closesocket(s);
|
ret = closesocket(s);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
errno = socket_error();
|
errno = socket_error();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue