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