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