]> git.proxmox.com Git - mirror_qemu.git/blame - util/oslib-win32.c
tcg: Introduce INDEX_op_qemu_st8_i32
[mirror_qemu.git] / util / oslib-win32.c
CommitLineData
c1b0b93b
JS
1/*
2 * os-win32.c
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
c6196440 5 * Copyright (c) 2010-2016 Red Hat, Inc.
c1b0b93b
JS
6 *
7 * QEMU library functions for win32 which are shared between QEMU and
8 * the QEMU tools.
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * THE SOFTWARE.
e637aa66
SW
27 *
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.
c1b0b93b 31 */
a8d25326 32
aafd7584 33#include "qemu/osdep.h"
c1b0b93b 34#include <windows.h>
a8d25326 35#include "qemu-common.h"
da34e65c 36#include "qapi/error.h"
9c17d615 37#include "sysemu/sysemu.h"
1de7afc9 38#include "qemu/main-loop.h"
c1b0b93b 39#include "trace.h"
1de7afc9 40#include "qemu/sockets.h"
f348b6d1 41#include "qemu/cutils.h"
c1b0b93b 42
e2ea3515
LE
43/* this must come after including "trace.h" */
44#include <shlobj.h>
45
b152aa84 46void *qemu_oom_check(void *ptr)
c1b0b93b
JS
47{
48 if (ptr == NULL) {
49 fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
50 abort();
51 }
52 return ptr;
53}
54
7d2a35cc 55void *qemu_try_memalign(size_t alignment, size_t size)
c1b0b93b
JS
56{
57 void *ptr;
58
59 if (!size) {
60 abort();
61 }
7d2a35cc 62 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
c1b0b93b
JS
63 trace_qemu_memalign(alignment, size, ptr);
64 return ptr;
65}
66
7d2a35cc
KW
67void *qemu_memalign(size_t alignment, size_t size)
68{
69 return qemu_oom_check(qemu_try_memalign(alignment, size));
70}
71
714efd54
DH
72static int get_allocation_granularity(void)
73{
74 SYSTEM_INFO system_info;
75
76 GetSystemInfo(&system_info);
77 return system_info.dwAllocationGranularity;
78}
79
06329cce 80void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)
c1b0b93b
JS
81{
82 void *ptr;
83
39228250 84 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
6eebf958 85 trace_qemu_anon_ram_alloc(size, ptr);
714efd54
DH
86
87 if (ptr && align) {
88 *align = MAX(get_allocation_granularity(), getpagesize());
89 }
c1b0b93b
JS
90 return ptr;
91}
92
93void qemu_vfree(void *ptr)
94{
95 trace_qemu_vfree(ptr);
94c8ff3a
MA
96 if (ptr) {
97 VirtualFree(ptr, 0, MEM_RELEASE);
98 }
c1b0b93b 99}
9549e764 100
e7a09b92
PB
101void qemu_anon_ram_free(void *ptr, size_t size)
102{
103 trace_qemu_anon_ram_free(ptr, size);
104 if (ptr) {
105 VirtualFree(ptr, 0, MEM_RELEASE);
106 }
107}
108
7c3afc85 109#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
d3e8f957
SW
110/* FIXME: add proper locking */
111struct tm *gmtime_r(const time_t *timep, struct tm *result)
112{
113 struct tm *p = gmtime(timep);
114 memset(result, 0, sizeof(*result));
115 if (p) {
116 *result = *p;
117 p = result;
118 }
119 return p;
120}
121
122/* FIXME: add proper locking */
123struct tm *localtime_r(const time_t *timep, struct tm *result)
124{
125 struct tm *p = localtime(timep);
126 memset(result, 0, sizeof(*result));
127 if (p) {
128 *result = *p;
129 p = result;
130 }
131 return p;
132}
7c3afc85 133#endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
d3e8f957 134
b16a44e1 135static int socket_error(void)
c6196440
DB
136{
137 switch (WSAGetLastError()) {
138 case 0:
139 return 0;
140 case WSAEINTR:
141 return EINTR;
142 case WSAEINVAL:
143 return EINVAL;
144 case WSA_INVALID_HANDLE:
145 return EBADF;
146 case WSA_NOT_ENOUGH_MEMORY:
147 return ENOMEM;
148 case WSA_INVALID_PARAMETER:
149 return EINVAL;
150 case WSAENAMETOOLONG:
151 return ENAMETOOLONG;
152 case WSAENOTEMPTY:
153 return ENOTEMPTY;
154 case WSAEWOULDBLOCK:
155 /* not using EWOULDBLOCK as we don't want code to have
156 * to check both EWOULDBLOCK and EAGAIN */
157 return EAGAIN;
158 case WSAEINPROGRESS:
159 return EINPROGRESS;
160 case WSAEALREADY:
161 return EALREADY;
162 case WSAENOTSOCK:
163 return ENOTSOCK;
164 case WSAEDESTADDRREQ:
165 return EDESTADDRREQ;
166 case WSAEMSGSIZE:
167 return EMSGSIZE;
168 case WSAEPROTOTYPE:
169 return EPROTOTYPE;
170 case WSAENOPROTOOPT:
171 return ENOPROTOOPT;
172 case WSAEPROTONOSUPPORT:
173 return EPROTONOSUPPORT;
174 case WSAEOPNOTSUPP:
175 return EOPNOTSUPP;
176 case WSAEAFNOSUPPORT:
177 return EAFNOSUPPORT;
178 case WSAEADDRINUSE:
179 return EADDRINUSE;
180 case WSAEADDRNOTAVAIL:
181 return EADDRNOTAVAIL;
182 case WSAENETDOWN:
183 return ENETDOWN;
184 case WSAENETUNREACH:
185 return ENETUNREACH;
186 case WSAENETRESET:
187 return ENETRESET;
188 case WSAECONNABORTED:
189 return ECONNABORTED;
190 case WSAECONNRESET:
191 return ECONNRESET;
192 case WSAENOBUFS:
193 return ENOBUFS;
194 case WSAEISCONN:
195 return EISCONN;
196 case WSAENOTCONN:
197 return ENOTCONN;
198 case WSAETIMEDOUT:
199 return ETIMEDOUT;
200 case WSAECONNREFUSED:
201 return ECONNREFUSED;
202 case WSAELOOP:
203 return ELOOP;
204 case WSAEHOSTUNREACH:
205 return EHOSTUNREACH;
206 default:
207 return EIO;
208 }
209}
210
894022e6
LV
211void qemu_set_block(int fd)
212{
213 unsigned long opt = 0;
214 WSAEventSelect(fd, NULL, 0);
215 ioctlsocket(fd, FIONBIO, &opt);
216}
217
218int qemu_try_set_nonblock(int fd)
219{
220 unsigned long opt = 1;
221 if (ioctlsocket(fd, FIONBIO, &opt) != NO_ERROR) {
222 return -socket_error();
223 }
894022e6
LV
224 return 0;
225}
226
227void qemu_set_nonblock(int fd)
228{
229 (void)qemu_try_set_nonblock(fd);
230}
231
232int socket_set_fast_reuse(int fd)
233{
234 /* Enabling the reuse of an endpoint that was used by a socket still in
235 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
236 * fast reuse is the default and SO_REUSEADDR does strange things. So we
237 * don't have to do anything here. More info can be found at:
238 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
239 return 0;
240}
241
9549e764
JS
242int inet_aton(const char *cp, struct in_addr *ia)
243{
244 uint32_t addr = inet_addr(cp);
245 if (addr == 0xffffffff) {
e637aa66 246 return 0;
9549e764
JS
247 }
248 ia->s_addr = addr;
249 return 1;
250}
251
252void qemu_set_cloexec(int fd)
253{
254}
dc786bc9 255
dc786bc9
JS
256/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
257#define _W32_FT_OFFSET (116444736000000000ULL)
258
259int qemu_gettimeofday(qemu_timeval *tp)
260{
261 union {
262 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
263 FILETIME ft;
264 } _now;
265
266 if(tp) {
267 GetSystemTimeAsFileTime (&_now.ft);
268 tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
269 tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
270 }
271 /* Always return 0 as per Open Group Base Specifications Issue 6.
272 Do not set errno on error. */
273 return 0;
274}
cbcfa041
PB
275
276int qemu_get_thread_id(void)
277{
278 return GetCurrentThreadId();
279}
e2ea3515
LE
280
281char *
282qemu_get_local_state_pathname(const char *relative_pathname)
283{
284 HRESULT result;
285 char base_path[MAX_PATH+1] = "";
286
287 result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
288 /* SHGFP_TYPE_CURRENT */ 0, base_path);
289 if (result != S_OK) {
290 /* misconfigured environment */
291 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result);
292 abort();
293 }
294 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path,
295 relative_pathname);
296}
13401ba0
SH
297
298void qemu_set_tty_echo(int fd, bool echo)
299{
300 HANDLE handle = (HANDLE)_get_osfhandle(fd);
301 DWORD dwMode = 0;
302
303 if (handle == INVALID_HANDLE_VALUE) {
304 return;
305 }
306
307 GetConsoleMode(handle, &dwMode);
308
309 if (echo) {
310 SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
311 } else {
312 SetConsoleMode(handle,
313 dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
314 }
315}
10f5bff6 316
9386a4a7 317static const char *exec_dir;
10f5bff6
FZ
318
319void qemu_init_exec_dir(const char *argv0)
320{
321
322 char *p;
323 char buf[MAX_PATH];
324 DWORD len;
325
a4c13869
PB
326 if (exec_dir) {
327 return;
328 }
329
10f5bff6
FZ
330 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
331 if (len == 0) {
332 return;
333 }
334
335 buf[len] = 0;
336 p = buf + len - 1;
337 while (p != buf && *p != '\\') {
338 p--;
339 }
340 *p = 0;
341 if (access(buf, R_OK) == 0) {
a4c13869 342 exec_dir = g_strdup(buf);
9386a4a7
PB
343 } else {
344 exec_dir = CONFIG_BINDIR;
10f5bff6
FZ
345 }
346}
347
a4c13869 348const char *qemu_get_exec_dir(void)
10f5bff6 349{
a4c13869 350 return exec_dir;
10f5bff6 351}
5a007547 352
1706e9d8 353#if !GLIB_CHECK_VERSION(2, 50, 0)
5a007547 354/*
e637aa66
SW
355 * The original implementation of g_poll from glib has a problem on Windows
356 * when using timeouts < 10 ms.
5a007547 357 *
e637aa66
SW
358 * Whenever g_poll is called with timeout < 10 ms, it does a quick poll instead
359 * of wait. This causes significant performance degradation of QEMU.
5a007547 360 *
e637aa66
SW
361 * The following code is a copy of the original code from glib/gpoll.c
362 * (glib commit 20f4d1820b8d4d0fc4447188e33efffd6d4a88d8 from 2014-02-19).
363 * Some debug code was removed and the code was reformatted.
364 * All other code modifications are marked with 'QEMU'.
365 */
366
367/*
368 * gpoll.c: poll(2) abstraction
369 * Copyright 1998 Owen Taylor
370 * Copyright 2008 Red Hat, Inc.
5a007547 371 *
e637aa66
SW
372 * This library is free software; you can redistribute it and/or
373 * modify it under the terms of the GNU Lesser General Public
374 * License as published by the Free Software Foundation; either
c36678b4 375 * version 2.1 of the License, or (at your option) any later version.
e637aa66
SW
376 *
377 * This library is distributed in the hope that it will be useful,
378 * but WITHOUT ANY WARRANTY; without even the implied warranty of
379 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
380 * Lesser General Public License for more details.
381 *
382 * You should have received a copy of the GNU Lesser General Public
383 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
5a007547 384 */
e637aa66
SW
385
386static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles,
387 GPollFD *fds, guint nfds, gint timeout)
5a007547 388{
e637aa66
SW
389 DWORD ready;
390 GPollFD *f;
391 int recursed_result;
5a007547 392
e637aa66
SW
393 if (poll_msgs) {
394 /* Wait for either messages or handles
395 * -> Use MsgWaitForMultipleObjectsEx
396 */
397 ready = MsgWaitForMultipleObjectsEx(nhandles, handles, timeout,
398 QS_ALLINPUT, MWMO_ALERTABLE);
5a007547 399
e637aa66
SW
400 if (ready == WAIT_FAILED) {
401 gchar *emsg = g_win32_error_message(GetLastError());
402 g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg);
403 g_free(emsg);
5a007547 404 }
e637aa66
SW
405 } else if (nhandles == 0) {
406 /* No handles to wait for, just the timeout */
407 if (timeout == INFINITE) {
408 ready = WAIT_FAILED;
409 } else {
410 SleepEx(timeout, TRUE);
411 ready = WAIT_TIMEOUT;
412 }
413 } else {
414 /* Wait for just handles
415 * -> Use WaitForMultipleObjectsEx
5a007547 416 */
e637aa66
SW
417 ready =
418 WaitForMultipleObjectsEx(nhandles, handles, FALSE, timeout, TRUE);
419 if (ready == WAIT_FAILED) {
420 gchar *emsg = g_win32_error_message(GetLastError());
421 g_warning("WaitForMultipleObjectsEx failed: %s", emsg);
422 g_free(emsg);
5a007547 423 }
e637aa66 424 }
5a007547 425
e637aa66
SW
426 if (ready == WAIT_FAILED) {
427 return -1;
428 } else if (ready == WAIT_TIMEOUT || ready == WAIT_IO_COMPLETION) {
429 return 0;
430 } else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles) {
431 for (f = fds; f < &fds[nfds]; ++f) {
432 if (f->fd == G_WIN32_MSG_HANDLE && f->events & G_IO_IN) {
433 f->revents |= G_IO_IN;
5a007547
SP
434 }
435 }
5a007547 436
e637aa66
SW
437 /* If we have a timeout, or no handles to poll, be satisfied
438 * with just noticing we have messages waiting.
439 */
440 if (timeout != 0 || nhandles == 0) {
441 return 1;
442 }
5a007547 443
e637aa66
SW
444 /* If no timeout and handles to poll, recurse to poll them,
445 * too.
446 */
447 recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0);
448 return (recursed_result == -1) ? -1 : 1 + recursed_result;
449 } else if (/* QEMU: removed the following unneeded statement which causes
450 * a compiler warning: ready >= WAIT_OBJECT_0 && */
451 ready < WAIT_OBJECT_0 + nhandles) {
452 for (f = fds; f < &fds[nfds]; ++f) {
453 if ((HANDLE) f->fd == handles[ready - WAIT_OBJECT_0]) {
454 f->revents = f->events;
455 }
456 }
5a007547 457
e637aa66
SW
458 /* If no timeout and polling several handles, recurse to poll
459 * the rest of them.
460 */
461 if (timeout == 0 && nhandles > 1) {
462 /* Remove the handle that fired */
463 int i;
0ec7b534
AF
464 for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++) {
465 handles[i-1] = handles[i];
e637aa66
SW
466 }
467 nhandles--;
468 recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0);
469 return (recursed_result == -1) ? -1 : 1 + recursed_result;
5a007547 470 }
e637aa66 471 return 1;
5a007547
SP
472 }
473
e637aa66
SW
474 return 0;
475}
5a007547 476
e637aa66
SW
477gint g_poll(GPollFD *fds, guint nfds, gint timeout)
478{
479 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
480 gboolean poll_msgs = FALSE;
481 GPollFD *f;
482 gint nhandles = 0;
483 int retval;
484
485 for (f = fds; f < &fds[nfds]; ++f) {
486 if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN)) {
487 poll_msgs = TRUE;
488 } else if (f->fd > 0) {
489 /* Don't add the same handle several times into the array, as
490 * docs say that is not allowed, even if it actually does seem
491 * to work.
492 */
493 gint i;
494
495 for (i = 0; i < nhandles; i++) {
496 if (handles[i] == (HANDLE) f->fd) {
497 break;
498 }
5a007547
SP
499 }
500
e637aa66
SW
501 if (i == nhandles) {
502 if (nhandles == MAXIMUM_WAIT_OBJECTS) {
503 g_warning("Too many handles to wait for!\n");
504 break;
505 } else {
506 handles[nhandles++] = (HANDLE) f->fd;
507 }
5a007547
SP
508 }
509 }
e637aa66 510 }
5a007547 511
e637aa66
SW
512 for (f = fds; f < &fds[nfds]; ++f) {
513 f->revents = 0;
514 }
5a007547 515
e637aa66
SW
516 if (timeout == -1) {
517 timeout = INFINITE;
518 }
5a007547 519
e637aa66
SW
520 /* Polling for several things? */
521 if (nhandles > 1 || (nhandles > 0 && poll_msgs)) {
522 /* First check if one or several of them are immediately
523 * available
524 */
525 retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, 0);
526
527 /* If not, and we have a significant timeout, poll again with
528 * timeout then. Note that this will return indication for only
529 * one event, or only for messages. We ignore timeouts less than
530 * ten milliseconds as they are mostly pointless on Windows, the
531 * MsgWaitForMultipleObjectsEx() call will timeout right away
532 * anyway.
533 *
534 * Modification for QEMU: replaced timeout >= 10 by timeout > 0.
5a007547 535 */
e637aa66
SW
536 if (retval == 0 && (timeout == INFINITE || timeout > 0)) {
537 retval = poll_rest(poll_msgs, handles, nhandles,
538 fds, nfds, timeout);
5a007547 539 }
e637aa66
SW
540 } else {
541 /* Just polling for one thing, so no need to check first if
542 * available immediately
543 */
544 retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, timeout);
545 }
5a007547 546
e637aa66
SW
547 if (retval == -1) {
548 for (f = fds; f < &fds[nfds]; ++f) {
549 f->revents = 0;
550 }
5a007547
SP
551 }
552
e637aa66 553 return retval;
5a007547 554}
1706e9d8 555#endif
38183310 556
a28c2f2d 557int getpagesize(void)
38183310
PB
558{
559 SYSTEM_INFO system_info;
560
561 GetSystemInfo(&system_info);
562 return system_info.dwPageSize;
563}
564
1e356fc1
JK
565void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
566 Error **errp)
38183310
PB
567{
568 int i;
038adc2f 569 size_t pagesize = qemu_real_host_page_size;
38183310
PB
570
571 memory = (memory + pagesize - 1) & -pagesize;
572 for (i = 0; i < memory / pagesize; i++) {
573 memset(area + pagesize * i, 0, 1);
574 }
575}
d57e4e48 576
7dc9ae43
MP
577char *qemu_get_pid_name(pid_t pid)
578{
579 /* XXX Implement me */
580 abort();
581}
582
583
57cb38b3
DB
584pid_t qemu_fork(Error **errp)
585{
586 errno = ENOSYS;
587 error_setg_errno(errp, errno,
588 "cannot fork child process");
589 return -1;
590}
a2d96af4
DB
591
592
593#undef connect
594int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
595 socklen_t addrlen)
596{
597 int ret;
598 ret = connect(sockfd, addr, addrlen);
599 if (ret < 0) {
f1cd5d41
MAL
600 if (WSAGetLastError() == WSAEWOULDBLOCK) {
601 errno = EINPROGRESS;
602 } else {
603 errno = socket_error();
604 }
a2d96af4
DB
605 }
606 return ret;
607}
608
609
610#undef listen
611int qemu_listen_wrap(int sockfd, int backlog)
612{
613 int ret;
614 ret = listen(sockfd, backlog);
615 if (ret < 0) {
616 errno = socket_error();
617 }
618 return ret;
619}
620
621
622#undef bind
623int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
624 socklen_t addrlen)
625{
626 int ret;
627 ret = bind(sockfd, addr, addrlen);
628 if (ret < 0) {
629 errno = socket_error();
630 }
631 return ret;
632}
633
634
635#undef socket
636int qemu_socket_wrap(int domain, int type, int protocol)
637{
638 int ret;
639 ret = socket(domain, type, protocol);
640 if (ret < 0) {
641 errno = socket_error();
642 }
643 return ret;
644}
645
646
647#undef accept
648int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
649 socklen_t *addrlen)
650{
651 int ret;
652 ret = accept(sockfd, addr, addrlen);
653 if (ret < 0) {
654 errno = socket_error();
655 }
656 return ret;
657}
658
659
660#undef shutdown
661int qemu_shutdown_wrap(int sockfd, int how)
662{
663 int ret;
664 ret = shutdown(sockfd, how);
665 if (ret < 0) {
666 errno = socket_error();
667 }
668 return ret;
669}
670
671
672#undef ioctlsocket
673int qemu_ioctlsocket_wrap(int fd, int req, void *val)
674{
675 int ret;
676 ret = ioctlsocket(fd, req, val);
677 if (ret < 0) {
678 errno = socket_error();
679 }
680 return ret;
681}
682
683
684#undef closesocket
685int qemu_closesocket_wrap(int fd)
686{
687 int ret;
688 ret = closesocket(fd);
689 if (ret < 0) {
690 errno = socket_error();
691 }
692 return ret;
693}
694
695
696#undef getsockopt
697int qemu_getsockopt_wrap(int sockfd, int level, int optname,
698 void *optval, socklen_t *optlen)
699{
700 int ret;
701 ret = getsockopt(sockfd, level, optname, optval, optlen);
702 if (ret < 0) {
703 errno = socket_error();
704 }
705 return ret;
706}
707
708
709#undef setsockopt
710int qemu_setsockopt_wrap(int sockfd, int level, int optname,
711 const void *optval, socklen_t optlen)
712{
713 int ret;
714 ret = setsockopt(sockfd, level, optname, optval, optlen);
715 if (ret < 0) {
716 errno = socket_error();
717 }
718 return ret;
719}
720
721
722#undef getpeername
723int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
724 socklen_t *addrlen)
725{
726 int ret;
727 ret = getpeername(sockfd, addr, addrlen);
728 if (ret < 0) {
729 errno = socket_error();
730 }
731 return ret;
732}
733
734
735#undef getsockname
736int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
737 socklen_t *addrlen)
738{
739 int ret;
740 ret = getsockname(sockfd, addr, addrlen);
741 if (ret < 0) {
742 errno = socket_error();
743 }
744 return ret;
745}
746
747
748#undef send
749ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags)
750{
751 int ret;
752 ret = send(sockfd, buf, len, flags);
753 if (ret < 0) {
754 errno = socket_error();
755 }
756 return ret;
757}
758
759
760#undef sendto
761ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
762 const struct sockaddr *addr, socklen_t addrlen)
763{
764 int ret;
765 ret = sendto(sockfd, buf, len, flags, addr, addrlen);
766 if (ret < 0) {
767 errno = socket_error();
768 }
769 return ret;
770}
771
772
773#undef recv
774ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags)
775{
776 int ret;
777 ret = recv(sockfd, buf, len, flags);
778 if (ret < 0) {
779 errno = socket_error();
780 }
781 return ret;
782}
783
784
785#undef recvfrom
786ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
787 struct sockaddr *addr, socklen_t *addrlen)
788{
789 int ret;
790 ret = recvfrom(sockfd, buf, len, flags, addr, addrlen);
791 if (ret < 0) {
792 errno = socket_error();
793 }
794 return ret;
795}
9e6bdef2
MAL
796
797bool qemu_write_pidfile(const char *filename, Error **errp)
798{
799 char buffer[128];
800 int len;
801 HANDLE file;
802 OVERLAPPED overlap;
803 BOOL ret;
804 memset(&overlap, 0, sizeof(overlap));
805
806 file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
807 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
808
809 if (file == INVALID_HANDLE_VALUE) {
810 error_setg(errp, "Failed to create PID file");
811 return false;
812 }
813 len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", (pid_t)getpid());
814 ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
815 NULL, &overlap);
816 CloseHandle(file);
817 if (ret == 0) {
818 error_setg(errp, "Failed to write PID file");
819 return false;
820 }
821 return true;
822}
e47f4765
MP
823
824char *qemu_get_host_name(Error **errp)
825{
826 wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
827 DWORD size = G_N_ELEMENTS(tmp);
828
829 if (GetComputerNameW(tmp, &size) == 0) {
830 error_setg_win32(errp, GetLastError(), "failed close handle");
831 return NULL;
832 }
833
834 return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
835}
ad06ef0e
AB
836
837size_t qemu_get_host_physmem(void)
838{
986babaa
AB
839 MEMORYSTATUSEX statex;
840 statex.dwLength = sizeof(statex);
841
842 if (GlobalMemoryStatusEx(&statex)) {
843 return statex.ullTotalPhys;
844 }
ad06ef0e
AB
845 return 0;
846}