]>
git.proxmox.com Git - mirror_qemu.git/blob - util/oslib-win32.c
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010-2016 Red Hat, Inc.
7 * QEMU library functions for win32 which are shared between QEMU and
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * The implementation of g_poll (functions poll_rest, g_poll) at the end of
29 * this file are based on code from GNOME glib-2 and use a different license,
30 * see the license comment there.
33 #include "qemu/osdep.h"
35 #include "qapi/error.h"
36 #include "qemu/main-loop.h"
38 #include "qemu/sockets.h"
39 #include "qemu/cutils.h"
40 #include "qemu/error-report.h"
43 /* this must come after including "trace.h" */
46 static int get_allocation_granularity(void)
48 SYSTEM_INFO system_info
;
50 GetSystemInfo(&system_info
);
51 return system_info
.dwAllocationGranularity
;
54 void *qemu_anon_ram_alloc(size_t size
, uint64_t *align
, bool shared
,
61 * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
62 * area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
64 error_report("Skipping reservation of swap space is not supported.");
68 ptr
= VirtualAlloc(NULL
, size
, MEM_COMMIT
, PAGE_READWRITE
);
69 trace_qemu_anon_ram_alloc(size
, ptr
);
72 *align
= MAX(get_allocation_granularity(), getpagesize());
77 void qemu_anon_ram_free(void *ptr
, size_t size
)
79 trace_qemu_anon_ram_free(ptr
, size
);
81 VirtualFree(ptr
, 0, MEM_RELEASE
);
85 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
86 /* FIXME: add proper locking */
87 struct tm
*gmtime_r(const time_t *timep
, struct tm
*result
)
89 struct tm
*p
= gmtime(timep
);
90 memset(result
, 0, sizeof(*result
));
98 /* FIXME: add proper locking */
99 struct tm
*localtime_r(const time_t *timep
, struct tm
*result
)
101 struct tm
*p
= localtime(timep
);
102 memset(result
, 0, sizeof(*result
));
109 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
111 static int socket_error(void)
113 switch (WSAGetLastError()) {
120 case WSA_INVALID_HANDLE
:
122 case WSA_NOT_ENOUGH_MEMORY
:
124 case WSA_INVALID_PARAMETER
:
126 case WSAENAMETOOLONG
:
131 /* not using EWOULDBLOCK as we don't want code to have
132 * to check both EWOULDBLOCK and EAGAIN */
140 case WSAEDESTADDRREQ
:
148 case WSAEPROTONOSUPPORT
:
149 return EPROTONOSUPPORT
;
152 case WSAEAFNOSUPPORT
:
156 case WSAEADDRNOTAVAIL
:
157 return EADDRNOTAVAIL
;
164 case WSAECONNABORTED
:
176 case WSAECONNREFUSED
:
180 case WSAEHOSTUNREACH
:
187 void qemu_socket_set_block(int fd
)
189 unsigned long opt
= 0;
190 WSAEventSelect(fd
, NULL
, 0);
191 ioctlsocket(fd
, FIONBIO
, &opt
);
194 int qemu_socket_try_set_nonblock(int fd
)
196 unsigned long opt
= 1;
197 if (ioctlsocket(fd
, FIONBIO
, &opt
) != NO_ERROR
) {
198 return -socket_error();
203 void qemu_socket_set_nonblock(int fd
)
205 (void)qemu_socket_try_set_nonblock(fd
);
208 int socket_set_fast_reuse(int fd
)
210 /* Enabling the reuse of an endpoint that was used by a socket still in
211 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
212 * fast reuse is the default and SO_REUSEADDR does strange things. So we
213 * don't have to do anything here. More info can be found at:
214 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
218 int inet_aton(const char *cp
, struct in_addr
*ia
)
220 uint32_t addr
= inet_addr(cp
);
221 if (addr
== 0xffffffff) {
228 void qemu_set_cloexec(int fd
)
232 int qemu_get_thread_id(void)
234 return GetCurrentThreadId();
238 qemu_get_local_state_dir(void)
241 char base_path
[MAX_PATH
+1] = "";
243 result
= SHGetFolderPath(NULL
, CSIDL_COMMON_APPDATA
, NULL
,
244 /* SHGFP_TYPE_CURRENT */ 0, base_path
);
245 if (result
!= S_OK
) {
246 /* misconfigured environment */
247 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result
);
250 return g_strdup(base_path
);
253 void qemu_set_tty_echo(int fd
, bool echo
)
255 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
258 if (handle
== INVALID_HANDLE_VALUE
) {
262 GetConsoleMode(handle
, &dwMode
);
265 SetConsoleMode(handle
, dwMode
| ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
);
267 SetConsoleMode(handle
,
268 dwMode
& ~(ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
));
272 int getpagesize(void)
274 SYSTEM_INFO system_info
;
276 GetSystemInfo(&system_info
);
277 return system_info
.dwPageSize
;
280 void os_mem_prealloc(int fd
, char *area
, size_t memory
, int smp_cpus
,
284 size_t pagesize
= qemu_real_host_page_size();
286 memory
= (memory
+ pagesize
- 1) & -pagesize
;
287 for (i
= 0; i
< memory
/ pagesize
; i
++) {
288 memset(area
+ pagesize
* i
, 0, 1);
292 char *qemu_get_pid_name(pid_t pid
)
294 /* XXX Implement me */
299 pid_t
qemu_fork(Error
**errp
)
302 error_setg_errno(errp
, errno
,
303 "cannot fork child process");
309 int qemu_connect_wrap(int sockfd
, const struct sockaddr
*addr
,
313 ret
= connect(sockfd
, addr
, addrlen
);
315 if (WSAGetLastError() == WSAEWOULDBLOCK
) {
318 errno
= socket_error();
326 int qemu_listen_wrap(int sockfd
, int backlog
)
329 ret
= listen(sockfd
, backlog
);
331 errno
= socket_error();
338 int qemu_bind_wrap(int sockfd
, const struct sockaddr
*addr
,
342 ret
= bind(sockfd
, addr
, addrlen
);
344 errno
= socket_error();
351 int qemu_socket_wrap(int domain
, int type
, int protocol
)
354 ret
= socket(domain
, type
, protocol
);
356 errno
= socket_error();
363 int qemu_accept_wrap(int sockfd
, struct sockaddr
*addr
,
367 ret
= accept(sockfd
, addr
, addrlen
);
369 errno
= socket_error();
376 int qemu_shutdown_wrap(int sockfd
, int how
)
379 ret
= shutdown(sockfd
, how
);
381 errno
= socket_error();
388 int qemu_ioctlsocket_wrap(int fd
, int req
, void *val
)
391 ret
= ioctlsocket(fd
, req
, val
);
393 errno
= socket_error();
400 int qemu_closesocket_wrap(int fd
)
403 ret
= closesocket(fd
);
405 errno
= socket_error();
412 int qemu_getsockopt_wrap(int sockfd
, int level
, int optname
,
413 void *optval
, socklen_t
*optlen
)
416 ret
= getsockopt(sockfd
, level
, optname
, optval
, optlen
);
418 errno
= socket_error();
425 int qemu_setsockopt_wrap(int sockfd
, int level
, int optname
,
426 const void *optval
, socklen_t optlen
)
429 ret
= setsockopt(sockfd
, level
, optname
, optval
, optlen
);
431 errno
= socket_error();
438 int qemu_getpeername_wrap(int sockfd
, struct sockaddr
*addr
,
442 ret
= getpeername(sockfd
, addr
, addrlen
);
444 errno
= socket_error();
451 int qemu_getsockname_wrap(int sockfd
, struct sockaddr
*addr
,
455 ret
= getsockname(sockfd
, addr
, addrlen
);
457 errno
= socket_error();
464 ssize_t
qemu_send_wrap(int sockfd
, const void *buf
, size_t len
, int flags
)
467 ret
= send(sockfd
, buf
, len
, flags
);
469 errno
= socket_error();
476 ssize_t
qemu_sendto_wrap(int sockfd
, const void *buf
, size_t len
, int flags
,
477 const struct sockaddr
*addr
, socklen_t addrlen
)
480 ret
= sendto(sockfd
, buf
, len
, flags
, addr
, addrlen
);
482 errno
= socket_error();
489 ssize_t
qemu_recv_wrap(int sockfd
, void *buf
, size_t len
, int flags
)
492 ret
= recv(sockfd
, buf
, len
, flags
);
494 errno
= socket_error();
501 ssize_t
qemu_recvfrom_wrap(int sockfd
, void *buf
, size_t len
, int flags
,
502 struct sockaddr
*addr
, socklen_t
*addrlen
)
505 ret
= recvfrom(sockfd
, buf
, len
, flags
, addr
, addrlen
);
507 errno
= socket_error();
512 bool qemu_write_pidfile(const char *filename
, Error
**errp
)
519 memset(&overlap
, 0, sizeof(overlap
));
521 file
= CreateFile(filename
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
522 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
524 if (file
== INVALID_HANDLE_VALUE
) {
525 error_setg(errp
, "Failed to create PID file");
528 len
= snprintf(buffer
, sizeof(buffer
), FMT_pid
"\n", (pid_t
)getpid());
529 ret
= WriteFile(file
, (LPCVOID
)buffer
, (DWORD
)len
,
533 error_setg(errp
, "Failed to write PID file");
539 size_t qemu_get_host_physmem(void)
541 MEMORYSTATUSEX statex
;
542 statex
.dwLength
= sizeof(statex
);
544 if (GlobalMemoryStatusEx(&statex
)) {
545 return statex
.ullTotalPhys
;
550 int qemu_msync(void *addr
, size_t length
, int fd
)
553 * Perform the sync based on the file descriptor
554 * The sync range will most probably be wider than the one
555 * requested - but it will still get the job done
557 return qemu_fdatasync(fd
);