2010-10-26 12:39:19 +04:00
|
|
|
/*
|
|
|
|
* os-win32.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
2016-03-07 14:19:18 +03:00
|
|
|
* Copyright (c) 2010-2016 Red Hat, Inc.
|
2010-10-26 12:39:19 +04: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.
|
2014-05-28 19:42:24 +04:00
|
|
|
*
|
|
|
|
* The implementation of g_poll (functions poll_rest, g_poll) at the end of
|
|
|
|
* this file are based on code from GNOME glib-2 and use a different license,
|
|
|
|
* see the license comment there.
|
2010-10-26 12:39:19 +04:00
|
|
|
*/
|
2019-05-23 17:35:08 +03:00
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2010-10-26 12:39:19 +04:00
|
|
|
#include <windows.h>
|
2019-05-23 17:35:08 +03:00
|
|
|
#include "qemu-common.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 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/main-loop.h"
|
2010-10-26 12:39:19 +04:00
|
|
|
#include "trace.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/sockets.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2020-10-21 20:38:02 +03:00
|
|
|
#include <malloc.h>
|
2010-10-26 12:39:19 +04:00
|
|
|
|
2013-05-18 08:31:48 +04:00
|
|
|
/* this must come after including "trace.h" */
|
|
|
|
#include <shlobj.h>
|
|
|
|
|
2010-10-26 12:39:26 +04:00
|
|
|
void *qemu_oom_check(void *ptr)
|
2010-10-26 12:39:19 +04:00
|
|
|
{
|
|
|
|
if (ptr == NULL) {
|
|
|
|
fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:24:05 +04:00
|
|
|
void *qemu_try_memalign(size_t alignment, size_t size)
|
2010-10-26 12:39:19 +04:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2020-10-21 20:38:02 +03:00
|
|
|
g_assert(size != 0);
|
2020-10-21 20:38:03 +03:00
|
|
|
g_assert(is_power_of_2(alignment));
|
2021-01-11 03:16:06 +03:00
|
|
|
ptr = _aligned_malloc(size, alignment);
|
2010-10-26 12:39:19 +04:00
|
|
|
trace_qemu_memalign(alignment, size, ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:24:05 +04:00
|
|
|
void *qemu_memalign(size_t alignment, size_t size)
|
|
|
|
{
|
|
|
|
return qemu_oom_check(qemu_try_memalign(alignment, size));
|
|
|
|
}
|
|
|
|
|
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 16:34:42 +03:00
|
|
|
static int get_allocation_granularity(void)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
|
|
|
|
GetSystemInfo(&system_info);
|
|
|
|
return system_info.dwAllocationGranularity;
|
|
|
|
}
|
|
|
|
|
2017-12-13 17:37:37 +03:00
|
|
|
void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)
|
2010-10-26 12:39:19 +04:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2013-07-31 17:11:11 +04:00
|
|
|
ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
|
2013-05-13 18:19:55 +04: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 16:34:42 +03:00
|
|
|
|
|
|
|
if (ptr && align) {
|
|
|
|
*align = MAX(get_allocation_granularity(), getpagesize());
|
|
|
|
}
|
2010-10-26 12:39:19 +04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_vfree(void *ptr)
|
|
|
|
{
|
|
|
|
trace_qemu_vfree(ptr);
|
2020-10-21 20:38:02 +03:00
|
|
|
_aligned_free(ptr);
|
2010-10-26 12:39:19 +04:00
|
|
|
}
|
2010-10-26 12:39:20 +04:00
|
|
|
|
2013-05-13 18:19:56 +04: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 02:43:47 +03:00
|
|
|
#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
|
2012-09-23 00:26:19 +04: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 02:43:47 +03:00
|
|
|
#endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
|
2012-09-23 00:26:19 +04:00
|
|
|
|
2016-03-07 23:36:03 +03:00
|
|
|
static int socket_error(void)
|
2016-03-07 14:19:18 +03: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07 21:45:14 +03:00
|
|
|
void qemu_set_block(int fd)
|
|
|
|
{
|
|
|
|
unsigned long opt = 0;
|
|
|
|
WSAEventSelect(fd, NULL, 0);
|
|
|
|
ioctlsocket(fd, FIONBIO, &opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_try_set_nonblock(int fd)
|
|
|
|
{
|
|
|
|
unsigned long opt = 1;
|
|
|
|
if (ioctlsocket(fd, FIONBIO, &opt) != NO_ERROR) {
|
|
|
|
return -socket_error();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_set_nonblock(int fd)
|
|
|
|
{
|
|
|
|
(void)qemu_try_set_nonblock(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 12:39:20 +04:00
|
|
|
int inet_aton(const char *cp, struct in_addr *ia)
|
|
|
|
{
|
|
|
|
uint32_t addr = inet_addr(cp);
|
|
|
|
if (addr == 0xffffffff) {
|
2014-05-28 19:42:24 +04:00
|
|
|
return 0;
|
2010-10-26 12:39:20 +04:00
|
|
|
}
|
|
|
|
ia->s_addr = addr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_set_cloexec(int fd)
|
|
|
|
{
|
|
|
|
}
|
2010-10-26 12:39:23 +04:00
|
|
|
|
|
|
|
/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
|
|
|
|
#define _W32_FT_OFFSET (116444736000000000ULL)
|
|
|
|
|
|
|
|
int qemu_gettimeofday(qemu_timeval *tp)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
|
|
|
|
FILETIME ft;
|
|
|
|
} _now;
|
|
|
|
|
|
|
|
if(tp) {
|
|
|
|
GetSystemTimeAsFileTime (&_now.ft);
|
|
|
|
tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
|
|
|
|
tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
|
|
|
|
}
|
|
|
|
/* Always return 0 as per Open Group Base Specifications Issue 6.
|
|
|
|
Do not set errno on error. */
|
|
|
|
return 0;
|
|
|
|
}
|
2011-09-12 18:20:11 +04:00
|
|
|
|
|
|
|
int qemu_get_thread_id(void)
|
|
|
|
{
|
|
|
|
return GetCurrentThreadId();
|
|
|
|
}
|
2013-05-18 08:31:48 +04:00
|
|
|
|
|
|
|
char *
|
|
|
|
qemu_get_local_state_pathname(const char *relative_pathname)
|
|
|
|
{
|
|
|
|
HRESULT result;
|
|
|
|
char base_path[MAX_PATH+1] = "";
|
|
|
|
|
|
|
|
result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
|
|
|
|
/* SHGFP_TYPE_CURRENT */ 0, base_path);
|
|
|
|
if (result != S_OK) {
|
|
|
|
/* misconfigured environment */
|
|
|
|
g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path,
|
|
|
|
relative_pathname);
|
|
|
|
}
|
2013-11-14 14:54:16 +04: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 10:48:51 +04:00
|
|
|
|
2020-08-18 13:00:59 +03:00
|
|
|
static const char *exec_dir;
|
2014-02-10 10:48:51 +04:00
|
|
|
|
|
|
|
void qemu_init_exec_dir(const char *argv0)
|
|
|
|
{
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
DWORD len;
|
|
|
|
|
2020-08-18 13:11:02 +03:00
|
|
|
if (exec_dir) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-10 10:48:51 +04:00
|
|
|
len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
|
|
|
|
if (len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[len] = 0;
|
|
|
|
p = buf + len - 1;
|
|
|
|
while (p != buf && *p != '\\') {
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
if (access(buf, R_OK) == 0) {
|
2020-08-18 13:11:02 +03:00
|
|
|
exec_dir = g_strdup(buf);
|
2020-08-18 13:00:59 +03:00
|
|
|
} else {
|
|
|
|
exec_dir = CONFIG_BINDIR;
|
2014-02-10 10:48:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:11:02 +03:00
|
|
|
const char *qemu_get_exec_dir(void)
|
2014-02-10 10:48:51 +04:00
|
|
|
{
|
2020-08-18 13:11:02 +03:00
|
|
|
return exec_dir;
|
2014-02-10 10:48:51 +04:00
|
|
|
}
|
glib: fix g_poll early timeout on windows
g_poll has a problem on Windows when using
timeouts < 10ms, in glib/gpoll.c:
/* If not, and we have a significant timeout, poll again with
* timeout then. Note that this will return indication for only
* one event, or only for messages. We ignore timeouts less than
* ten milliseconds as they are mostly pointless on Windows, the
* MsgWaitForMultipleObjectsEx() call will timeout right away
* anyway.
*/
if (retval == 0 && (timeout == INFINITE || timeout >= 10))
retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout);
so whenever g_poll is called with timeout < 10ms it does
a quick poll instead of wait, this causes significant performance
degradation of QEMU, thus we should use WaitForMultipleObjectsEx
directly
Signed-off-by: Stanislav Vorobiov <s.vorobiov@samsung.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-05-08 12:47:10 +04:00
|
|
|
|
2015-11-14 22:25:44 +03:00
|
|
|
int getpagesize(void)
|
2014-05-14 13:43:21 +04:00
|
|
|
{
|
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
|
|
|
|
GetSystemInfo(&system_info);
|
|
|
|
return system_info.dwPageSize;
|
|
|
|
}
|
|
|
|
|
2017-02-24 06:31:43 +03:00
|
|
|
void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
|
|
|
|
Error **errp)
|
2014-05-14 13:43:21 +04:00
|
|
|
{
|
|
|
|
int i;
|
2019-10-13 05:11:45 +03:00
|
|
|
size_t pagesize = qemu_real_host_page_size;
|
2014-05-14 13:43:21 +04:00
|
|
|
|
|
|
|
memory = (memory + pagesize - 1) & -pagesize;
|
|
|
|
for (i = 0; i < memory / pagesize; i++) {
|
|
|
|
memset(area + pagesize * i, 0, 1);
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 19:09:19 +03:00
|
|
|
|
2016-09-27 18:24:56 +03:00
|
|
|
char *qemu_get_pid_name(pid_t pid)
|
|
|
|
{
|
|
|
|
/* XXX Implement me */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-28 16:40:01 +03:00
|
|
|
pid_t qemu_fork(Error **errp)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"cannot fork child process");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-03-07 23:25:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
#undef connect
|
|
|
|
int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = connect(sockfd, addr, addrlen);
|
|
|
|
if (ret < 0) {
|
2019-10-01 16:26:07 +03:00
|
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) {
|
|
|
|
errno = EINPROGRESS;
|
|
|
|
} else {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
2016-03-07 23:25:19 +03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef listen
|
|
|
|
int qemu_listen_wrap(int sockfd, int backlog)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = listen(sockfd, backlog);
|
|
|
|
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;
|
|
|
|
ret = bind(sockfd, addr, addrlen);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef socket
|
|
|
|
int qemu_socket_wrap(int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = socket(domain, type, protocol);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef accept
|
|
|
|
int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
|
|
|
|
socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = accept(sockfd, addr, addrlen);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef shutdown
|
|
|
|
int qemu_shutdown_wrap(int sockfd, int how)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = shutdown(sockfd, how);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef ioctlsocket
|
|
|
|
int qemu_ioctlsocket_wrap(int fd, int req, void *val)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = ioctlsocket(fd, req, val);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef closesocket
|
|
|
|
int qemu_closesocket_wrap(int fd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = closesocket(fd);
|
|
|
|
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;
|
|
|
|
ret = getsockopt(sockfd, level, optname, optval, optlen);
|
|
|
|
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;
|
|
|
|
ret = setsockopt(sockfd, level, optname, optval, optlen);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef getpeername
|
|
|
|
int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
|
|
|
|
socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = getpeername(sockfd, addr, addrlen);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = socket_error();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef getsockname
|
|
|
|
int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
|
|
|
|
socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = getsockname(sockfd, addr, addrlen);
|
|
|
|
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;
|
|
|
|
ret = send(sockfd, buf, len, flags);
|
|
|
|
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;
|
|
|
|
ret = sendto(sockfd, buf, len, flags, addr, addrlen);
|
|
|
|
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;
|
|
|
|
ret = recv(sockfd, buf, len, flags);
|
|
|
|
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;
|
|
|
|
ret = recvfrom(sockfd, buf, len, flags, addr, addrlen);
|
|
|
|
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 17:53:12 +03: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-22 21:19:35 +03:00
|
|
|
|
|
|
|
char *qemu_get_host_name(Error **errp)
|
|
|
|
{
|
|
|
|
wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
|
|
|
|
DWORD size = G_N_ELEMENTS(tmp);
|
|
|
|
|
|
|
|
if (GetComputerNameW(tmp, &size) == 0) {
|
|
|
|
error_setg_win32(errp, GetLastError(), "failed close handle");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
|
|
|
|
}
|
2020-07-24 09:44:57 +03:00
|
|
|
|
|
|
|
size_t qemu_get_host_physmem(void)
|
|
|
|
{
|
2020-07-24 09:44:58 +03:00
|
|
|
MEMORYSTATUSEX statex;
|
|
|
|
statex.dwLength = sizeof(statex);
|
|
|
|
|
|
|
|
if (GlobalMemoryStatusEx(&statex)) {
|
|
|
|
return statex.ullTotalPhys;
|
|
|
|
}
|
2020-07-24 09:44:57 +03:00
|
|
|
return 0;
|
|
|
|
}
|