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