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