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