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