2010-10-26 16:39:19 +08:00
|
|
|
/*
|
|
|
|
* os-win32.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
2016-03-07 19:19:18 +08:00
|
|
|
* Copyright (c) 2010-2016 Red Hat, Inc.
|
2010-10-26 16:39:19 +08:00
|
|
|
*
|
|
|
|
* QEMU library functions for win32 which are shared between QEMU and
|
|
|
|
* the QEMU tools.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 22:35:08 +08:00
|
|
|
|
2016-01-30 01:49:55 +08:00
|
|
|
#include "qemu/osdep.h"
|
2010-10-26 16:39:19 +08:00
|
|
|
#include <windows.h>
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/main-loop.h"
|
2010-10-26 16:39:19 +08:00
|
|
|
#include "trace.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/sockets.h"
|
2016-03-21 01:16:19 +08:00
|
|
|
#include "qemu/cutils.h"
|
2021-05-10 19:43:21 +08:00
|
|
|
#include "qemu/error-report.h"
|
2020-10-22 01:38:02 +08:00
|
|
|
#include <malloc.h>
|
2010-10-26 16:39:19 +08:00
|
|
|
|
util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()
Let's set the alignment just like for the posix variant. This will
implicitly set the alignment of the underlying memory region and
therefore make memory_region_get_alignment(mr) return something > 0 for
all memory backends applicable to PCDIMM/NVDIMM.
The allocation granularity is ususally 64k, while the page size is 4k.
The documentation of VirtualAlloc is not really comprehensible in case
only MEM_COMMIT is specified without an address. We'll detect the actual
values and then go for the bigger one. The expection is, that it will
always be 64k aligned. (The assumption is that MEM_COMMIT does an
implicit MEM_RESERVE, so the address will always be aligned to the
allocation granularity. And the allocation granularity is always bigger
than the page size).
This will allow us to drop special handling in pc.c for
memory_region_get_alignment(mr) == 0, as we can then assume that it is
always set (and AFAICS >= getpagesize()).
For pc in pc_memory_plug(), under Windows TARGET_PAGE_SIZE == getpagesize(),
therefore alignment of DIMMs will not change, and therefore also not the
guest physical memory layout.
For spapr in spapr_memory_plug(), an alignment of 0 would have been used
until now. As QEMU_ALIGN_UP will crash with the alignment being 0, this
never worked, so we don't have to care about compatibility handling.
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: David Hildenbrand <david@redhat.com>
Message-Id: <20180801133444.11269-3-david@redhat.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-08-01 21:34:42 +08:00
|
|
|
static int get_allocation_granularity(void)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
|
|
|
|
GetSystemInfo(&system_info);
|
|
|
|
return system_info.dwAllocationGranularity;
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:43:21 +08:00
|
|
|
void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,
|
|
|
|
bool noreserve)
|
2010-10-26 16:39:19 +08:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2021-05-10 19:43:21 +08:00
|
|
|
if (noreserve) {
|
|
|
|
/*
|
|
|
|
* We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
|
|
|
|
* area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
|
|
|
|
*/
|
|
|
|
error_report("Skipping reservation of swap space is not supported.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-31 21:11:11 +08:00
|
|
|
ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
|
2013-05-13 22:19:55 +08:00
|
|
|
trace_qemu_anon_ram_alloc(size, ptr);
|
util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()
Let's set the alignment just like for the posix variant. This will
implicitly set the alignment of the underlying memory region and
therefore make memory_region_get_alignment(mr) return something > 0 for
all memory backends applicable to PCDIMM/NVDIMM.
The allocation granularity is ususally 64k, while the page size is 4k.
The documentation of VirtualAlloc is not really comprehensible in case
only MEM_COMMIT is specified without an address. We'll detect the actual
values and then go for the bigger one. The expection is, that it will
always be 64k aligned. (The assumption is that MEM_COMMIT does an
implicit MEM_RESERVE, so the address will always be aligned to the
allocation granularity. And the allocation granularity is always bigger
than the page size).
This will allow us to drop special handling in pc.c for
memory_region_get_alignment(mr) == 0, as we can then assume that it is
always set (and AFAICS >= getpagesize()).
For pc in pc_memory_plug(), under Windows TARGET_PAGE_SIZE == getpagesize(),
therefore alignment of DIMMs will not change, and therefore also not the
guest physical memory layout.
For spapr in spapr_memory_plug(), an alignment of 0 would have been used
until now. As QEMU_ALIGN_UP will crash with the alignment being 0, this
never worked, so we don't have to care about compatibility handling.
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: David Hildenbrand <david@redhat.com>
Message-Id: <20180801133444.11269-3-david@redhat.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-08-01 21:34:42 +08:00
|
|
|
|
|
|
|
if (ptr && align) {
|
|
|
|
*align = MAX(get_allocation_granularity(), getpagesize());
|
|
|
|
}
|
2010-10-26 16:39:19 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-05-13 22:19:56 +08:00
|
|
|
void qemu_anon_ram_free(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
trace_qemu_anon_ram_free(ptr, size);
|
|
|
|
if (ptr) {
|
|
|
|
VirtualFree(ptr, 0, MEM_RELEASE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
win32: Simplify gmtime_r detection not depends on if _POSIX_C_SOURCE are defined on msys2/mingw
We remove the CONFIG_LOCALTIME_R detection option in configure, and move the check
existence of gmtime_r from configure into C header and source directly by using macro
`_POSIX_THREAD_SAFE_FUNCTIONS`.
Before this patch, the configure script are always assume the compiler doesn't define
_POSIX_C_SOURCE macro at all, but that's not true, because thirdparty library such
as ncursesw may define -D_POSIX_C_SOURCE in it's pkg-config file. And that C Flags will
added -D_POSIX_C_SOURCE into each QEMU_CFLAGS. And that's causing the following compiling error:
n file included from C:/work/xemu/qemu/include/qemu/osdep.h:119,
from ../softmmu/main.c:25:
C:/work/xemu/qemu/include/sysemu/os-win32.h:53:12: error: redundant redeclaration of 'gmtime_r' [-Werror=redundant-decls]
53 | struct tm *gmtime_r(const time_t *timep, struct tm *result);
| ^~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:94,
from ../softmmu/main.c:25:
C:/CI-Tools/msys64/mingw64/x86_64-w64-mingw32/include/time.h:284:36: note: previous definition of 'gmtime_r' was here
284 | __forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm *_Tm) {
| ^~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:119,
from ../softmmu/main.c:25:
C:/work/xemu/qemu/include/sysemu/os-win32.h:55:12: error: redundant redeclaration of 'localtime_r' [-Werror=redundant-decls]
55 | struct tm *localtime_r(const time_t *timep, struct tm *result);
| ^~~~~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:94,
from ../softmmu/main.c:25:
C:/CI-Tools/msys64/mingw64/x86_64-w64-mingw32/include/time.h:281:36: note: previous definition of 'localtime_r' was here
281 | __forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm *_Tm) {
| ^~~~~~~~~~~
Compiling C object libcommon.fa.p/hw_gpio_zaurus.c.obj
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:119,
from ../hw/i2c/smbus_slave.c:16:
C:/work/xemu/qemu/include/sysemu/os-win32.h:53:12: error: redundant redeclaration of 'gmtime_r' [-Werror=redundant-decls]
53 | struct tm *gmtime_r(const time_t *timep, struct tm *result);
| ^~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:94,
from ../hw/i2c/smbus_slave.c:16:
C:/CI-Tools/msys64/mingw64/x86_64-w64-mingw32/include/time.h:284:36: note: previous definition of 'gmtime_r' was here
284 | __forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm *_Tm) {
| ^~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:119,
from ../hw/i2c/smbus_slave.c:16:
C:/work/xemu/qemu/include/sysemu/os-win32.h:55:12: error: redundant redeclaration of 'localtime_r' [-Werror=redundant-decls]
55 | struct tm *localtime_r(const time_t *timep, struct tm *result);
| ^~~~~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:94,
from ../hw/i2c/smbus_slave.c:16:
C:/CI-Tools/msys64/mingw64/x86_64-w64-mingw32/include/time.h:281:36: note: previous definition of 'localtime_r' was here
281 | __forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm *_Tm) {
| ^~~~~~~~~~~
Compiling C object libcommon.fa.p/hw_dma_xilinx_axidma.c.obj
After this patch, whenever ncursesw or other thirdparty libraries tried to define or not
define _POSIX_C_SOURCE, the source will building properly. Because now, we don't make any
assumption if _POSIX_C_SOURCE are defined. We solely relied on if the macro `_POSIX_THREAD_SAFE_FUNCTIONS`
are defined in msys2/mingw header.
The _POSIX_THREAD_SAFE_FUNCTIONS are defined in mingw header like this:
```
#if defined(_POSIX_C_SOURCE) && !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
#define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
#endif
#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
__forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm *_Tm) {
return localtime_s(_Tm, _Time) ? NULL : _Tm;
}
__forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm *_Tm) {
return gmtime_s(_Tm, _Time) ? NULL : _Tm;
}
__forceinline char *__CRTDECL ctime_r(const time_t *_Time, char *_Str) {
return ctime_s(_Str, 0x7fffffff, _Time) ? NULL : _Str;
}
__forceinline char *__CRTDECL asctime_r(const struct tm *_Tm, char * _Str) {
return asctime_s(_Str, 0x7fffffff, _Tm) ? NULL : _Str;
}
#endif
```
Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-id: 20201012234348.1427-5-luoyonggang@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2020-10-13 07:43:47 +08:00
|
|
|
#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
|
2012-09-23 04:26:19 +08:00
|
|
|
/* FIXME: add proper locking */
|
|
|
|
struct tm *gmtime_r(const time_t *timep, struct tm *result)
|
|
|
|
{
|
|
|
|
struct tm *p = gmtime(timep);
|
|
|
|
memset(result, 0, sizeof(*result));
|
|
|
|
if (p) {
|
|
|
|
*result = *p;
|
|
|
|
p = result;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: add proper locking */
|
|
|
|
struct tm *localtime_r(const time_t *timep, struct tm *result)
|
|
|
|
{
|
|
|
|
struct tm *p = localtime(timep);
|
|
|
|
memset(result, 0, sizeof(*result));
|
|
|
|
if (p) {
|
|
|
|
*result = *p;
|
|
|
|
p = result;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
win32: Simplify gmtime_r detection not depends on if _POSIX_C_SOURCE are defined on msys2/mingw
We remove the CONFIG_LOCALTIME_R detection option in configure, and move the check
existence of gmtime_r from configure into C header and source directly by using macro
`_POSIX_THREAD_SAFE_FUNCTIONS`.
Before this patch, the configure script are always assume the compiler doesn't define
_POSIX_C_SOURCE macro at all, but that's not true, because thirdparty library such
as ncursesw may define -D_POSIX_C_SOURCE in it's pkg-config file. And that C Flags will
added -D_POSIX_C_SOURCE into each QEMU_CFLAGS. And that's causing the following compiling error:
n file included from C:/work/xemu/qemu/include/qemu/osdep.h:119,
from ../softmmu/main.c:25:
C:/work/xemu/qemu/include/sysemu/os-win32.h:53:12: error: redundant redeclaration of 'gmtime_r' [-Werror=redundant-decls]
53 | struct tm *gmtime_r(const time_t *timep, struct tm *result);
| ^~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:94,
from ../softmmu/main.c:25:
C:/CI-Tools/msys64/mingw64/x86_64-w64-mingw32/include/time.h:284:36: note: previous definition of 'gmtime_r' was here
284 | __forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm *_Tm) {
| ^~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:119,
from ../softmmu/main.c:25:
C:/work/xemu/qemu/include/sysemu/os-win32.h:55:12: error: redundant redeclaration of 'localtime_r' [-Werror=redundant-decls]
55 | struct tm *localtime_r(const time_t *timep, struct tm *result);
| ^~~~~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:94,
from ../softmmu/main.c:25:
C:/CI-Tools/msys64/mingw64/x86_64-w64-mingw32/include/time.h:281:36: note: previous definition of 'localtime_r' was here
281 | __forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm *_Tm) {
| ^~~~~~~~~~~
Compiling C object libcommon.fa.p/hw_gpio_zaurus.c.obj
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:119,
from ../hw/i2c/smbus_slave.c:16:
C:/work/xemu/qemu/include/sysemu/os-win32.h:53:12: error: redundant redeclaration of 'gmtime_r' [-Werror=redundant-decls]
53 | struct tm *gmtime_r(const time_t *timep, struct tm *result);
| ^~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:94,
from ../hw/i2c/smbus_slave.c:16:
C:/CI-Tools/msys64/mingw64/x86_64-w64-mingw32/include/time.h:284:36: note: previous definition of 'gmtime_r' was here
284 | __forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm *_Tm) {
| ^~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:119,
from ../hw/i2c/smbus_slave.c:16:
C:/work/xemu/qemu/include/sysemu/os-win32.h:55:12: error: redundant redeclaration of 'localtime_r' [-Werror=redundant-decls]
55 | struct tm *localtime_r(const time_t *timep, struct tm *result);
| ^~~~~~~~~~~
In file included from C:/work/xemu/qemu/include/qemu/osdep.h:94,
from ../hw/i2c/smbus_slave.c:16:
C:/CI-Tools/msys64/mingw64/x86_64-w64-mingw32/include/time.h:281:36: note: previous definition of 'localtime_r' was here
281 | __forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm *_Tm) {
| ^~~~~~~~~~~
Compiling C object libcommon.fa.p/hw_dma_xilinx_axidma.c.obj
After this patch, whenever ncursesw or other thirdparty libraries tried to define or not
define _POSIX_C_SOURCE, the source will building properly. Because now, we don't make any
assumption if _POSIX_C_SOURCE are defined. We solely relied on if the macro `_POSIX_THREAD_SAFE_FUNCTIONS`
are defined in msys2/mingw header.
The _POSIX_THREAD_SAFE_FUNCTIONS are defined in mingw header like this:
```
#if defined(_POSIX_C_SOURCE) && !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
#define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
#endif
#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
__forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm *_Tm) {
return localtime_s(_Tm, _Time) ? NULL : _Tm;
}
__forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm *_Tm) {
return gmtime_s(_Tm, _Time) ? NULL : _Tm;
}
__forceinline char *__CRTDECL ctime_r(const time_t *_Time, char *_Str) {
return ctime_s(_Str, 0x7fffffff, _Time) ? NULL : _Str;
}
__forceinline char *__CRTDECL asctime_r(const struct tm *_Tm, char * _Str) {
return asctime_s(_Str, 0x7fffffff, _Tm) ? NULL : _Str;
}
#endif
```
Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-id: 20201012234348.1427-5-luoyonggang@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2020-10-13 07:43:47 +08:00
|
|
|
#endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
|
2012-09-23 04:26:19 +08:00
|
|
|
|
2016-03-08 04:36:03 +08:00
|
|
|
static int socket_error(void)
|
2016-03-07 19:19:18 +08:00
|
|
|
{
|
|
|
|
switch (WSAGetLastError()) {
|
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
case WSAEINTR:
|
|
|
|
return EINTR;
|
|
|
|
case WSAEINVAL:
|
|
|
|
return EINVAL;
|
|
|
|
case WSA_INVALID_HANDLE:
|
|
|
|
return EBADF;
|
|
|
|
case WSA_NOT_ENOUGH_MEMORY:
|
|
|
|
return ENOMEM;
|
|
|
|
case WSA_INVALID_PARAMETER:
|
|
|
|
return EINVAL;
|
|
|
|
case WSAENAMETOOLONG:
|
|
|
|
return ENAMETOOLONG;
|
|
|
|
case WSAENOTEMPTY:
|
|
|
|
return ENOTEMPTY;
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
/* not using EWOULDBLOCK as we don't want code to have
|
|
|
|
* to check both EWOULDBLOCK and EAGAIN */
|
|
|
|
return EAGAIN;
|
|
|
|
case WSAEINPROGRESS:
|
|
|
|
return EINPROGRESS;
|
|
|
|
case WSAEALREADY:
|
|
|
|
return EALREADY;
|
|
|
|
case WSAENOTSOCK:
|
|
|
|
return ENOTSOCK;
|
|
|
|
case WSAEDESTADDRREQ:
|
|
|
|
return EDESTADDRREQ;
|
|
|
|
case WSAEMSGSIZE:
|
|
|
|
return EMSGSIZE;
|
|
|
|
case WSAEPROTOTYPE:
|
|
|
|
return EPROTOTYPE;
|
|
|
|
case WSAENOPROTOOPT:
|
|
|
|
return ENOPROTOOPT;
|
|
|
|
case WSAEPROTONOSUPPORT:
|
|
|
|
return EPROTONOSUPPORT;
|
|
|
|
case WSAEOPNOTSUPP:
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
case WSAEAFNOSUPPORT:
|
|
|
|
return EAFNOSUPPORT;
|
|
|
|
case WSAEADDRINUSE:
|
|
|
|
return EADDRINUSE;
|
|
|
|
case WSAEADDRNOTAVAIL:
|
|
|
|
return EADDRNOTAVAIL;
|
|
|
|
case WSAENETDOWN:
|
|
|
|
return ENETDOWN;
|
|
|
|
case WSAENETUNREACH:
|
|
|
|
return ENETUNREACH;
|
|
|
|
case WSAENETRESET:
|
|
|
|
return ENETRESET;
|
|
|
|
case WSAECONNABORTED:
|
|
|
|
return ECONNABORTED;
|
|
|
|
case WSAECONNRESET:
|
|
|
|
return ECONNRESET;
|
|
|
|
case WSAENOBUFS:
|
|
|
|
return ENOBUFS;
|
|
|
|
case WSAEISCONN:
|
|
|
|
return EISCONN;
|
|
|
|
case WSAENOTCONN:
|
|
|
|
return ENOTCONN;
|
|
|
|
case WSAETIMEDOUT:
|
|
|
|
return ETIMEDOUT;
|
|
|
|
case WSAECONNREFUSED:
|
|
|
|
return ECONNREFUSED;
|
|
|
|
case WSAELOOP:
|
|
|
|
return ELOOP;
|
|
|
|
case WSAEHOSTUNREACH:
|
|
|
|
return EHOSTUNREACH;
|
|
|
|
default:
|
|
|
|
return EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 21:33:47 +08:00
|
|
|
void qemu_socket_set_block(int fd)
|
net: check if the file descriptor is valid before using it
qemu_set_nonblock() checks that the file descriptor can be used and, if
not, crashes QEMU. An assert() is used for that. The use of assert() is
used to detect programming error and the coredump will allow to debug
the problem.
But in the case of the tap device, this assert() can be triggered by
a misconfiguration by the user. At startup, it's not a real problem, but it
can also happen during the hot-plug of a new device, and here it's a
problem because we can crash a perfectly healthy system.
For instance:
# ip link add link virbr0 name macvtap0 type macvtap mode bridge
# ip link set macvtap0 up
# TAP=/dev/tap$(ip -o link show macvtap0 | cut -d: -f1)
# qemu-system-x86_64 -machine q35 -device pcie-root-port,id=pcie-root-port-0 -monitor stdio 9<> $TAP
(qemu) netdev_add type=tap,id=hostnet0,vhost=on,fd=9
(qemu) device_add driver=virtio-net-pci,netdev=hostnet0,id=net0,bus=pcie-root-port-0
(qemu) device_del net0
(qemu) netdev_del hostnet0
(qemu) netdev_add type=tap,id=hostnet1,vhost=on,fd=9
qemu-system-x86_64: .../util/oslib-posix.c:247: qemu_set_nonblock: Assertion `f != -1' failed.
Aborted (core dumped)
To avoid that, add a function, qemu_try_set_nonblock(), that allows to report the
problem without crashing.
In the same way, we also update the function for vhostfd in net_init_tap_one() and
for fd in net_init_socket() (both descriptors are provided by the user and can
be wrong).
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
2020-07-08 02:45:14 +08:00
|
|
|
{
|
|
|
|
unsigned long opt = 0;
|
2023-02-21 20:47:52 +08:00
|
|
|
qemu_socket_unselect(fd, NULL);
|
net: check if the file descriptor is valid before using it
qemu_set_nonblock() checks that the file descriptor can be used and, if
not, crashes QEMU. An assert() is used for that. The use of assert() is
used to detect programming error and the coredump will allow to debug
the problem.
But in the case of the tap device, this assert() can be triggered by
a misconfiguration by the user. At startup, it's not a real problem, but it
can also happen during the hot-plug of a new device, and here it's a
problem because we can crash a perfectly healthy system.
For instance:
# ip link add link virbr0 name macvtap0 type macvtap mode bridge
# ip link set macvtap0 up
# TAP=/dev/tap$(ip -o link show macvtap0 | cut -d: -f1)
# qemu-system-x86_64 -machine q35 -device pcie-root-port,id=pcie-root-port-0 -monitor stdio 9<> $TAP
(qemu) netdev_add type=tap,id=hostnet0,vhost=on,fd=9
(qemu) device_add driver=virtio-net-pci,netdev=hostnet0,id=net0,bus=pcie-root-port-0
(qemu) device_del net0
(qemu) netdev_del hostnet0
(qemu) netdev_add type=tap,id=hostnet1,vhost=on,fd=9
qemu-system-x86_64: .../util/oslib-posix.c:247: qemu_set_nonblock: Assertion `f != -1' failed.
Aborted (core dumped)
To avoid that, add a function, qemu_try_set_nonblock(), that allows to report the
problem without crashing.
In the same way, we also update the function for vhostfd in net_init_tap_one() and
for fd in net_init_socket() (both descriptors are provided by the user and can
be wrong).
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
2020-07-08 02:45:14 +08:00
|
|
|
ioctlsocket(fd, FIONBIO, &opt);
|
|
|
|
}
|
|
|
|
|
2022-04-25 21:33:47 +08:00
|
|
|
int qemu_socket_try_set_nonblock(int fd)
|
net: check if the file descriptor is valid before using it
qemu_set_nonblock() checks that the file descriptor can be used and, if
not, crashes QEMU. An assert() is used for that. The use of assert() is
used to detect programming error and the coredump will allow to debug
the problem.
But in the case of the tap device, this assert() can be triggered by
a misconfiguration by the user. At startup, it's not a real problem, but it
can also happen during the hot-plug of a new device, and here it's a
problem because we can crash a perfectly healthy system.
For instance:
# ip link add link virbr0 name macvtap0 type macvtap mode bridge
# ip link set macvtap0 up
# TAP=/dev/tap$(ip -o link show macvtap0 | cut -d: -f1)
# qemu-system-x86_64 -machine q35 -device pcie-root-port,id=pcie-root-port-0 -monitor stdio 9<> $TAP
(qemu) netdev_add type=tap,id=hostnet0,vhost=on,fd=9
(qemu) device_add driver=virtio-net-pci,netdev=hostnet0,id=net0,bus=pcie-root-port-0
(qemu) device_del net0
(qemu) netdev_del hostnet0
(qemu) netdev_add type=tap,id=hostnet1,vhost=on,fd=9
qemu-system-x86_64: .../util/oslib-posix.c:247: qemu_set_nonblock: Assertion `f != -1' failed.
Aborted (core dumped)
To avoid that, add a function, qemu_try_set_nonblock(), that allows to report the
problem without crashing.
In the same way, we also update the function for vhostfd in net_init_tap_one() and
for fd in net_init_socket() (both descriptors are provided by the user and can
be wrong).
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
2020-07-08 02:45:14 +08:00
|
|
|
{
|
|
|
|
unsigned long opt = 1;
|
|
|
|
if (ioctlsocket(fd, FIONBIO, &opt) != NO_ERROR) {
|
|
|
|
return -socket_error();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-25 21:33:47 +08:00
|
|
|
void qemu_socket_set_nonblock(int fd)
|
net: check if the file descriptor is valid before using it
qemu_set_nonblock() checks that the file descriptor can be used and, if
not, crashes QEMU. An assert() is used for that. The use of assert() is
used to detect programming error and the coredump will allow to debug
the problem.
But in the case of the tap device, this assert() can be triggered by
a misconfiguration by the user. At startup, it's not a real problem, but it
can also happen during the hot-plug of a new device, and here it's a
problem because we can crash a perfectly healthy system.
For instance:
# ip link add link virbr0 name macvtap0 type macvtap mode bridge
# ip link set macvtap0 up
# TAP=/dev/tap$(ip -o link show macvtap0 | cut -d: -f1)
# qemu-system-x86_64 -machine q35 -device pcie-root-port,id=pcie-root-port-0 -monitor stdio 9<> $TAP
(qemu) netdev_add type=tap,id=hostnet0,vhost=on,fd=9
(qemu) device_add driver=virtio-net-pci,netdev=hostnet0,id=net0,bus=pcie-root-port-0
(qemu) device_del net0
(qemu) netdev_del hostnet0
(qemu) netdev_add type=tap,id=hostnet1,vhost=on,fd=9
qemu-system-x86_64: .../util/oslib-posix.c:247: qemu_set_nonblock: Assertion `f != -1' failed.
Aborted (core dumped)
To avoid that, add a function, qemu_try_set_nonblock(), that allows to report the
problem without crashing.
In the same way, we also update the function for vhostfd in net_init_tap_one() and
for fd in net_init_socket() (both descriptors are provided by the user and can
be wrong).
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
2020-07-08 02:45:14 +08:00
|
|
|
{
|
2022-04-25 21:33:47 +08:00
|
|
|
(void)qemu_socket_try_set_nonblock(fd);
|
net: check if the file descriptor is valid before using it
qemu_set_nonblock() checks that the file descriptor can be used and, if
not, crashes QEMU. An assert() is used for that. The use of assert() is
used to detect programming error and the coredump will allow to debug
the problem.
But in the case of the tap device, this assert() can be triggered by
a misconfiguration by the user. At startup, it's not a real problem, but it
can also happen during the hot-plug of a new device, and here it's a
problem because we can crash a perfectly healthy system.
For instance:
# ip link add link virbr0 name macvtap0 type macvtap mode bridge
# ip link set macvtap0 up
# TAP=/dev/tap$(ip -o link show macvtap0 | cut -d: -f1)
# qemu-system-x86_64 -machine q35 -device pcie-root-port,id=pcie-root-port-0 -monitor stdio 9<> $TAP
(qemu) netdev_add type=tap,id=hostnet0,vhost=on,fd=9
(qemu) device_add driver=virtio-net-pci,netdev=hostnet0,id=net0,bus=pcie-root-port-0
(qemu) device_del net0
(qemu) netdev_del hostnet0
(qemu) netdev_add type=tap,id=hostnet1,vhost=on,fd=9
qemu-system-x86_64: .../util/oslib-posix.c:247: qemu_set_nonblock: Assertion `f != -1' failed.
Aborted (core dumped)
To avoid that, add a function, qemu_try_set_nonblock(), that allows to report the
problem without crashing.
In the same way, we also update the function for vhostfd in net_init_tap_one() and
for fd in net_init_socket() (both descriptors are provided by the user and can
be wrong).
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
2020-07-08 02:45:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int socket_set_fast_reuse(int fd)
|
|
|
|
{
|
|
|
|
/* Enabling the reuse of an endpoint that was used by a socket still in
|
|
|
|
* TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
|
|
|
|
* fast reuse is the default and SO_REUSEADDR does strange things. So we
|
|
|
|
* don't have to do anything here. More info can be found at:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-26 16:39:20 +08:00
|
|
|
int inet_aton(const char *cp, struct in_addr *ia)
|
|
|
|
{
|
|
|
|
uint32_t addr = inet_addr(cp);
|
|
|
|
if (addr == 0xffffffff) {
|
2014-05-28 23:42:24 +08:00
|
|
|
return 0;
|
2010-10-26 16:39:20 +08:00
|
|
|
}
|
|
|
|
ia->s_addr = addr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_set_cloexec(int fd)
|
|
|
|
{
|
|
|
|
}
|
2010-10-26 16:39:23 +08:00
|
|
|
|
2011-09-12 22:20:11 +08:00
|
|
|
int qemu_get_thread_id(void)
|
|
|
|
{
|
|
|
|
return GetCurrentThreadId();
|
|
|
|
}
|
2013-05-18 12:31:48 +08:00
|
|
|
|
|
|
|
char *
|
2022-04-20 21:26:21 +08:00
|
|
|
qemu_get_local_state_dir(void)
|
2013-05-18 12:31:48 +08:00
|
|
|
{
|
2022-05-25 22:41:27 +08:00
|
|
|
const char * const *data_dirs = g_get_system_data_dirs();
|
2013-05-18 12:31:48 +08:00
|
|
|
|
2022-05-25 22:41:27 +08:00
|
|
|
g_assert(data_dirs && data_dirs[0]);
|
|
|
|
|
|
|
|
return g_strdup(data_dirs[0]);
|
2013-05-18 12:31:48 +08:00
|
|
|
}
|
2013-11-14 18:54:16 +08:00
|
|
|
|
|
|
|
void qemu_set_tty_echo(int fd, bool echo)
|
|
|
|
{
|
|
|
|
HANDLE handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
DWORD dwMode = 0;
|
|
|
|
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetConsoleMode(handle, &dwMode);
|
|
|
|
|
|
|
|
if (echo) {
|
|
|
|
SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
|
|
|
|
} else {
|
|
|
|
SetConsoleMode(handle,
|
|
|
|
dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:48:51 +08:00
|
|
|
|
2015-11-15 03:25:44 +08:00
|
|
|
int getpagesize(void)
|
2014-05-14 17:43:21 +08:00
|
|
|
{
|
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
|
|
|
|
GetSystemInfo(&system_info);
|
|
|
|
return system_info.dwPageSize;
|
|
|
|
}
|
|
|
|
|
2022-10-14 21:47:14 +08:00
|
|
|
void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
|
2022-10-14 21:47:18 +08:00
|
|
|
ThreadContext *tc, Error **errp)
|
2014-05-14 17:43:21 +08:00
|
|
|
{
|
|
|
|
int i;
|
2022-03-23 23:57:22 +08:00
|
|
|
size_t pagesize = qemu_real_host_page_size();
|
2014-05-14 17:43:21 +08:00
|
|
|
|
2022-10-14 21:47:14 +08:00
|
|
|
sz = (sz + pagesize - 1) & -pagesize;
|
|
|
|
for (i = 0; i < sz / pagesize; i++) {
|
2014-05-14 17:43:21 +08:00
|
|
|
memset(area + pagesize * i, 0, 1);
|
|
|
|
}
|
|
|
|
}
|
2015-05-13 00:09:19 +08:00
|
|
|
|
2016-09-27 23:24:56 +08:00
|
|
|
char *qemu_get_pid_name(pid_t pid)
|
|
|
|
{
|
|
|
|
/* XXX Implement me */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
bool qemu_socket_select(int sockfd, WSAEVENT hEventObject,
|
2023-02-21 20:47:51 +08:00
|
|
|
long lNetworkEvents, Error **errp)
|
|
|
|
{
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
2023-02-21 20:47:51 +08:00
|
|
|
if (errp == NULL) {
|
|
|
|
errp = &error_warn;
|
|
|
|
}
|
|
|
|
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
error_setg(errp, "invalid socket fd=%d", sockfd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-21 20:47:51 +08:00
|
|
|
if (WSAEventSelect(s, hEventObject, lNetworkEvents) != 0) {
|
|
|
|
error_setg_win32(errp, WSAGetLastError(), "failed to WSAEventSelect()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
bool qemu_socket_unselect(int sockfd, Error **errp)
|
2023-02-21 20:47:52 +08:00
|
|
|
{
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
return qemu_socket_select(sockfd, NULL, 0, errp);
|
2023-02-21 20:47:52 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 20:27:44 +08:00
|
|
|
int qemu_socketpair(int domain, int type, int protocol, int sv[2])
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr = {
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
socklen_t socklen;
|
|
|
|
int listener = -1;
|
|
|
|
int client = -1;
|
|
|
|
int server = -1;
|
|
|
|
g_autofree char *path = NULL;
|
|
|
|
int tmpfd;
|
|
|
|
u_long arg;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
g_return_val_if_fail(sv != NULL, -1);
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
socklen = sizeof(addr);
|
|
|
|
|
|
|
|
tmpfd = g_file_open_tmp(NULL, &path, NULL);
|
|
|
|
if (tmpfd == -1 || !path) {
|
|
|
|
errno = EACCES;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(tmpfd);
|
|
|
|
|
|
|
|
if (strlen(path) >= sizeof(addr.sun_path)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
|
|
|
|
|
|
|
listener = socket(domain, type, protocol);
|
|
|
|
if (listener == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DeleteFile(path) == 0 && GetLastError() != ERROR_FILE_NOT_FOUND) {
|
|
|
|
errno = EACCES;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
g_clear_pointer(&path, g_free);
|
|
|
|
|
|
|
|
if (bind(listener, (struct sockaddr *)&addr, socklen) == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(listener, 1) == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
client = socket(domain, type, protocol);
|
|
|
|
if (client == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = 1;
|
|
|
|
if (ioctlsocket(client, FIONBIO, &arg) != NO_ERROR) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(client, (struct sockaddr *)&addr, socklen) == -1 &&
|
|
|
|
WSAGetLastError() != WSAEWOULDBLOCK) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
server = accept(listener, NULL, NULL);
|
|
|
|
if (server == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = 0;
|
|
|
|
if (ioctlsocket(client, FIONBIO, &arg) != NO_ERROR) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = 0;
|
|
|
|
if (ioctlsocket(client, SIO_AF_UNIX_GETPEERPID, &arg) != NO_ERROR) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg != GetCurrentProcessId()) {
|
|
|
|
errno = EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sv[0] = server;
|
|
|
|
server = -1;
|
|
|
|
sv[1] = client;
|
|
|
|
client = -1;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (listener != -1) {
|
|
|
|
close(listener);
|
|
|
|
}
|
|
|
|
if (client != -1) {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
if (server != -1) {
|
|
|
|
close(server);
|
|
|
|
}
|
|
|
|
if (path) {
|
|
|
|
DeleteFile(path);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-08 04:25:19 +08:00
|
|
|
#undef connect
|
|
|
|
int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = connect(s, addr, addrlen);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
2019-10-01 21:26:07 +08:00
|
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) {
|
|
|
|
errno = EINPROGRESS;
|
|
|
|
} else {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
2016-03-08 04:25:19 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef listen
|
|
|
|
int qemu_listen_wrap(int sockfd, int backlog)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = listen(s, backlog);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef bind
|
|
|
|
int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bind(s, addr, addrlen);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:24:40 +08:00
|
|
|
EXCEPTION_DISPOSITION
|
|
|
|
win32_close_exception_handler(struct _EXCEPTION_RECORD*,
|
|
|
|
void*, struct _CONTEXT*, void*)
|
|
|
|
{
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
}
|
|
|
|
|
2023-02-21 20:48:01 +08:00
|
|
|
#undef close
|
2023-03-20 21:36:41 +08:00
|
|
|
int qemu_close_socket_osfhandle(int fd)
|
2016-03-08 04:25:19 +08:00
|
|
|
{
|
2023-03-20 21:36:41 +08:00
|
|
|
SOCKET s = _get_osfhandle(fd);
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
DWORD flags = 0;
|
|
|
|
|
2023-03-20 21:36:41 +08:00
|
|
|
/*
|
|
|
|
* If we were to just call _close on the descriptor, it would close the
|
|
|
|
* HANDLE, but it wouldn't free any of the resources associated to the
|
|
|
|
* SOCKET, and we can't call _close after calling closesocket, because
|
|
|
|
* closesocket has already closed the HANDLE, and _close would attempt to
|
|
|
|
* close the HANDLE again, resulting in a double free. We can however
|
|
|
|
* protect the HANDLE from actually being closed long enough to close the
|
|
|
|
* file descriptor, then close the socket itself.
|
|
|
|
*/
|
|
|
|
if (!GetHandleInformation((HANDLE)s, &flags)) {
|
|
|
|
errno = EACCES;
|
|
|
|
return -1;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
}
|
|
|
|
|
2023-03-20 21:36:41 +08:00
|
|
|
if (!SetHandleInformation((HANDLE)s, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE)) {
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:24:40 +08:00
|
|
|
__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 {
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
}
|
|
|
|
|
2023-03-20 21:36:41 +08:00
|
|
|
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);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
2016-03-08 04:25:19 +08:00
|
|
|
}
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
|
2016-03-08 04:25:19 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
#undef socket
|
|
|
|
int qemu_socket_wrap(int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
SOCKET s;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
s = socket(domain, type, protocol);
|
|
|
|
if (s == -1) {
|
|
|
|
errno = socket_error();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = _open_osfhandle(s, _O_BINARY);
|
|
|
|
if (fd < 0) {
|
|
|
|
closesocket(s);
|
|
|
|
/* _open_osfhandle may not set errno, and closesocket() may override it */
|
|
|
|
errno = ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-08 04:25:19 +08:00
|
|
|
#undef accept
|
|
|
|
int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
|
|
|
|
socklen_t *addrlen)
|
|
|
|
{
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
int fd;
|
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = accept(s, addr, addrlen);
|
|
|
|
if (s == -1) {
|
2016-03-08 04:25:19 +08:00
|
|
|
errno = socket_error();
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
return -1;
|
2016-03-08 04:25:19 +08:00
|
|
|
}
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
|
|
|
|
fd = _open_osfhandle(s, _O_BINARY);
|
|
|
|
if (fd < 0) {
|
|
|
|
closesocket(s);
|
|
|
|
/* _open_osfhandle may not set errno, and closesocket() may override it */
|
|
|
|
errno = ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
2016-03-08 04:25:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef shutdown
|
|
|
|
int qemu_shutdown_wrap(int sockfd, int how)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = shutdown(s, how);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef ioctlsocket
|
|
|
|
int qemu_ioctlsocket_wrap(int fd, int req, void *val)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(fd);
|
2016-03-08 04:25:19 +08:00
|
|
|
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-03-08 04:25:19 +08:00
|
|
|
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
ret = ioctlsocket(s, req, val);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef getsockopt
|
|
|
|
int qemu_getsockopt_wrap(int sockfd, int level, int optname,
|
|
|
|
void *optval, socklen_t *optlen)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = getsockopt(s, level, optname, optval, optlen);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef setsockopt
|
|
|
|
int qemu_setsockopt_wrap(int sockfd, int level, int optname,
|
|
|
|
const void *optval, socklen_t optlen)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = setsockopt(s, level, optname, optval, optlen);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef getpeername
|
|
|
|
int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
|
|
|
|
socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = getpeername(s, addr, addrlen);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef getsockname
|
|
|
|
int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
|
|
|
|
socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = getsockname(s, addr, addrlen);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef send
|
|
|
|
ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = send(s, buf, len, flags);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef sendto
|
|
|
|
ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
|
|
|
|
const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sendto(s, buf, len, flags, addr, addrlen);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef recv
|
|
|
|
ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = recv(s, buf, len, flags);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef recvfrom
|
|
|
|
ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
|
|
|
|
struct sockaddr *addr, socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
win32: avoid mixing SOCKET and file descriptor space
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().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Message-Id: <20230221124802.4103554-15-marcandre.lureau@redhat.com>
2023-02-21 20:47:59 +08:00
|
|
|
SOCKET s = _get_osfhandle(sockfd);
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = recvfrom(s, buf, len, flags, addr, addrlen);
|
2016-03-08 04:25:19 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
util: add qemu_write_pidfile()
There are variants of qemu_create_pidfile() in qemu-pr-helper and
qemu-ga. Let's have a common implementation in libqemuutil.
The code is initially based from pr-helper write_pidfile(), with
various improvements and suggestions from Daniel Berrangé:
QEMU will leave the pidfile existing on disk when it exits which
initially made me think it avoids the deletion race. The app
managing QEMU, however, may well delete the pidfile after it has
seen QEMU exit, and even if the app locks the pidfile before
deleting it, there is still a race.
eg consider the following sequence
QEMU 1 libvirtd QEMU 2
1. lock(pidfile)
2. exit()
3. open(pidfile)
4. lock(pidfile)
5. open(pidfile)
6. unlink(pidfile)
7. close(pidfile)
8. lock(pidfile)
IOW, at step 8 the new QEMU has successfully acquired the lock, but
the pidfile no longer exists on disk because it was deleted after
the original QEMU exited.
While we could just say no external app should ever delete the
pidfile, I don't think that is satisfactory as people don't read
docs, and admins don't like stale pidfiles being left around on
disk.
To make this robust, I think we might want to copy libvirt's
approach to pidfile acquisition which runs in a loop and checks that
the file on disk /after/ acquiring the lock matches the file that
was locked. Then we could in fact safely let QEMU delete its own
pidfiles on clean exit..
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20180831145314.14736-2-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-08-31 22:53:12 +08:00
|
|
|
|
|
|
|
bool qemu_write_pidfile(const char *filename, Error **errp)
|
|
|
|
{
|
|
|
|
char buffer[128];
|
|
|
|
int len;
|
|
|
|
HANDLE file;
|
|
|
|
OVERLAPPED overlap;
|
|
|
|
BOOL ret;
|
|
|
|
memset(&overlap, 0, sizeof(overlap));
|
|
|
|
|
|
|
|
file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
|
|
|
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
|
|
if (file == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg(errp, "Failed to create PID file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", (pid_t)getpid());
|
|
|
|
ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
|
|
|
|
NULL, &overlap);
|
|
|
|
CloseHandle(file);
|
|
|
|
if (ret == 0) {
|
|
|
|
error_setg(errp, "Failed to write PID file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-23 02:19:35 +08:00
|
|
|
|
2020-07-24 14:44:57 +08:00
|
|
|
size_t qemu_get_host_physmem(void)
|
|
|
|
{
|
2020-07-24 14:44:58 +08:00
|
|
|
MEMORYSTATUSEX statex;
|
|
|
|
statex.dwLength = sizeof(statex);
|
|
|
|
|
|
|
|
if (GlobalMemoryStatusEx(&statex)) {
|
|
|
|
return statex.ullTotalPhys;
|
|
|
|
}
|
2020-07-24 14:44:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2022-04-20 21:26:03 +08:00
|
|
|
|
|
|
|
int qemu_msync(void *addr, size_t length, int fd)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Perform the sync based on the file descriptor
|
|
|
|
* The sync range will most probably be wider than the one
|
|
|
|
* requested - but it will still get the job done
|
|
|
|
*/
|
|
|
|
return qemu_fdatasync(fd);
|
|
|
|
}
|