Statistics
| Branch: | Revision:

root / oslib-posix.c @ b152aa84

History | View | Annotate | Download (2.8 kB)

1 c1b0b93b Jes Sorensen
/*
2 c1b0b93b Jes Sorensen
 * os-posix-lib.c
3 c1b0b93b Jes Sorensen
 *
4 c1b0b93b Jes Sorensen
 * Copyright (c) 2003-2008 Fabrice Bellard
5 c1b0b93b Jes Sorensen
 * Copyright (c) 2010 Red Hat, Inc.
6 c1b0b93b Jes Sorensen
 *
7 c1b0b93b Jes Sorensen
 * QEMU library functions on POSIX which are shared between QEMU and
8 c1b0b93b Jes Sorensen
 * the QEMU tools.
9 c1b0b93b Jes Sorensen
 *
10 c1b0b93b Jes Sorensen
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 c1b0b93b Jes Sorensen
 * of this software and associated documentation files (the "Software"), to deal
12 c1b0b93b Jes Sorensen
 * in the Software without restriction, including without limitation the rights
13 c1b0b93b Jes Sorensen
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 c1b0b93b Jes Sorensen
 * copies of the Software, and to permit persons to whom the Software is
15 c1b0b93b Jes Sorensen
 * furnished to do so, subject to the following conditions:
16 c1b0b93b Jes Sorensen
 *
17 c1b0b93b Jes Sorensen
 * The above copyright notice and this permission notice shall be included in
18 c1b0b93b Jes Sorensen
 * all copies or substantial portions of the Software.
19 c1b0b93b Jes Sorensen
 *
20 c1b0b93b Jes Sorensen
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 c1b0b93b Jes Sorensen
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 c1b0b93b Jes Sorensen
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 c1b0b93b Jes Sorensen
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 c1b0b93b Jes Sorensen
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 c1b0b93b Jes Sorensen
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 c1b0b93b Jes Sorensen
 * THE SOFTWARE.
27 c1b0b93b Jes Sorensen
 */
28 c1b0b93b Jes Sorensen
29 c1b0b93b Jes Sorensen
#include "config-host.h"
30 c1b0b93b Jes Sorensen
#include "sysemu.h"
31 c1b0b93b Jes Sorensen
#include "trace.h"
32 9549e764 Jes Sorensen
#include "qemu_socket.h"
33 c1b0b93b Jes Sorensen
34 b152aa84 Jes Sorensen
void *qemu_oom_check(void *ptr)
35 c1b0b93b Jes Sorensen
{
36 c1b0b93b Jes Sorensen
    if (ptr == NULL) {
37 c1b0b93b Jes Sorensen
        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
38 c1b0b93b Jes Sorensen
        abort();
39 c1b0b93b Jes Sorensen
    }
40 c1b0b93b Jes Sorensen
    return ptr;
41 c1b0b93b Jes Sorensen
}
42 c1b0b93b Jes Sorensen
43 c1b0b93b Jes Sorensen
void *qemu_memalign(size_t alignment, size_t size)
44 c1b0b93b Jes Sorensen
{
45 c1b0b93b Jes Sorensen
    void *ptr;
46 c1b0b93b Jes Sorensen
#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
47 c1b0b93b Jes Sorensen
    int ret;
48 c1b0b93b Jes Sorensen
    ret = posix_memalign(&ptr, alignment, size);
49 c1b0b93b Jes Sorensen
    if (ret != 0) {
50 c1b0b93b Jes Sorensen
        fprintf(stderr, "Failed to allocate %zu B: %s\n",
51 c1b0b93b Jes Sorensen
                size, strerror(ret));
52 c1b0b93b Jes Sorensen
        abort();
53 c1b0b93b Jes Sorensen
    }
54 c1b0b93b Jes Sorensen
#elif defined(CONFIG_BSD)
55 b152aa84 Jes Sorensen
    ptr = qemu_oom_check(valloc(size));
56 c1b0b93b Jes Sorensen
#else
57 b152aa84 Jes Sorensen
    ptr = qemu_oom_check(memalign(alignment, size));
58 c1b0b93b Jes Sorensen
#endif
59 c1b0b93b Jes Sorensen
    trace_qemu_memalign(alignment, size, ptr);
60 c1b0b93b Jes Sorensen
    return ptr;
61 c1b0b93b Jes Sorensen
}
62 c1b0b93b Jes Sorensen
63 c1b0b93b Jes Sorensen
/* alloc shared memory pages */
64 c1b0b93b Jes Sorensen
void *qemu_vmalloc(size_t size)
65 c1b0b93b Jes Sorensen
{
66 c1b0b93b Jes Sorensen
    return qemu_memalign(getpagesize(), size);
67 c1b0b93b Jes Sorensen
}
68 c1b0b93b Jes Sorensen
69 c1b0b93b Jes Sorensen
void qemu_vfree(void *ptr)
70 c1b0b93b Jes Sorensen
{
71 c1b0b93b Jes Sorensen
    trace_qemu_vfree(ptr);
72 c1b0b93b Jes Sorensen
    free(ptr);
73 c1b0b93b Jes Sorensen
}
74 9549e764 Jes Sorensen
75 9549e764 Jes Sorensen
void socket_set_nonblock(int fd)
76 9549e764 Jes Sorensen
{
77 9549e764 Jes Sorensen
    int f;
78 9549e764 Jes Sorensen
    f = fcntl(fd, F_GETFL);
79 9549e764 Jes Sorensen
    fcntl(fd, F_SETFL, f | O_NONBLOCK);
80 9549e764 Jes Sorensen
}
81 9549e764 Jes Sorensen
82 9549e764 Jes Sorensen
void qemu_set_cloexec(int fd)
83 9549e764 Jes Sorensen
{
84 9549e764 Jes Sorensen
    int f;
85 9549e764 Jes Sorensen
    f = fcntl(fd, F_GETFD);
86 9549e764 Jes Sorensen
    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
87 9549e764 Jes Sorensen
}
88 70e72ce4 Jes Sorensen
89 70e72ce4 Jes Sorensen
/*
90 70e72ce4 Jes Sorensen
 * Creates a pipe with FD_CLOEXEC set on both file descriptors
91 70e72ce4 Jes Sorensen
 */
92 70e72ce4 Jes Sorensen
int qemu_pipe(int pipefd[2])
93 70e72ce4 Jes Sorensen
{
94 70e72ce4 Jes Sorensen
    int ret;
95 70e72ce4 Jes Sorensen
96 70e72ce4 Jes Sorensen
#ifdef CONFIG_PIPE2
97 70e72ce4 Jes Sorensen
    ret = pipe2(pipefd, O_CLOEXEC);
98 70e72ce4 Jes Sorensen
    if (ret != -1 || errno != ENOSYS) {
99 70e72ce4 Jes Sorensen
        return ret;
100 70e72ce4 Jes Sorensen
    }
101 70e72ce4 Jes Sorensen
#endif
102 70e72ce4 Jes Sorensen
    ret = pipe(pipefd);
103 70e72ce4 Jes Sorensen
    if (ret == 0) {
104 70e72ce4 Jes Sorensen
        qemu_set_cloexec(pipefd[0]);
105 70e72ce4 Jes Sorensen
        qemu_set_cloexec(pipefd[1]);
106 70e72ce4 Jes Sorensen
    }
107 70e72ce4 Jes Sorensen
108 70e72ce4 Jes Sorensen
    return ret;
109 70e72ce4 Jes Sorensen
}