Statistics
| Branch: | Revision:

root / oslib-posix.c @ 18ebcc86

History | View | Annotate | Download (5.6 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 f97742d0 Alexandre Raymond
/* The following block of code temporarily renames the daemon() function so the
30 f97742d0 Alexandre Raymond
   compiler does not see the warning associated with it in stdlib.h on OSX */
31 f97742d0 Alexandre Raymond
#ifdef __APPLE__
32 f97742d0 Alexandre Raymond
#define daemon qemu_fake_daemon_function
33 f97742d0 Alexandre Raymond
#include <stdlib.h>
34 f97742d0 Alexandre Raymond
#undef daemon
35 f97742d0 Alexandre Raymond
extern int daemon(int, int);
36 f97742d0 Alexandre Raymond
#endif
37 f97742d0 Alexandre Raymond
38 36b58628 Avi Kivity
#if defined(__linux__) && defined(__x86_64__)
39 c2a8238a Stefan Weil
   /* Use 2 MiB alignment so transparent hugepages can be used by KVM.
40 c2a8238a Stefan Weil
      Valgrind does not support alignments larger than 1 MiB,
41 c2a8238a Stefan Weil
      therefore we need special code which handles running on Valgrind. */
42 36b58628 Avi Kivity
#  define QEMU_VMALLOC_ALIGN (512 * 4096)
43 c2a8238a Stefan Weil
#  define CONFIG_VALGRIND
44 36b58628 Avi Kivity
#else
45 36b58628 Avi Kivity
#  define QEMU_VMALLOC_ALIGN getpagesize()
46 36b58628 Avi Kivity
#endif
47 36b58628 Avi Kivity
48 c1b0b93b Jes Sorensen
#include "config-host.h"
49 c1b0b93b Jes Sorensen
#include "sysemu.h"
50 c1b0b93b Jes Sorensen
#include "trace.h"
51 9549e764 Jes Sorensen
#include "qemu_socket.h"
52 c1b0b93b Jes Sorensen
53 c2a8238a Stefan Weil
#if defined(CONFIG_VALGRIND)
54 c2a8238a Stefan Weil
static int running_on_valgrind = -1;
55 c2a8238a Stefan Weil
#else
56 c2a8238a Stefan Weil
#  define running_on_valgrind 0
57 c2a8238a Stefan Weil
#endif
58 f97742d0 Alexandre Raymond
59 f97742d0 Alexandre Raymond
int qemu_daemon(int nochdir, int noclose)
60 f97742d0 Alexandre Raymond
{
61 f97742d0 Alexandre Raymond
    return daemon(nochdir, noclose);
62 f97742d0 Alexandre Raymond
}
63 f97742d0 Alexandre Raymond
64 b152aa84 Jes Sorensen
void *qemu_oom_check(void *ptr)
65 c1b0b93b Jes Sorensen
{
66 c1b0b93b Jes Sorensen
    if (ptr == NULL) {
67 c1b0b93b Jes Sorensen
        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
68 c1b0b93b Jes Sorensen
        abort();
69 c1b0b93b Jes Sorensen
    }
70 c1b0b93b Jes Sorensen
    return ptr;
71 c1b0b93b Jes Sorensen
}
72 c1b0b93b Jes Sorensen
73 c1b0b93b Jes Sorensen
void *qemu_memalign(size_t alignment, size_t size)
74 c1b0b93b Jes Sorensen
{
75 c1b0b93b Jes Sorensen
    void *ptr;
76 c1b0b93b Jes Sorensen
#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
77 c1b0b93b Jes Sorensen
    int ret;
78 c1b0b93b Jes Sorensen
    ret = posix_memalign(&ptr, alignment, size);
79 c1b0b93b Jes Sorensen
    if (ret != 0) {
80 c1b0b93b Jes Sorensen
        fprintf(stderr, "Failed to allocate %zu B: %s\n",
81 c1b0b93b Jes Sorensen
                size, strerror(ret));
82 c1b0b93b Jes Sorensen
        abort();
83 c1b0b93b Jes Sorensen
    }
84 c1b0b93b Jes Sorensen
#elif defined(CONFIG_BSD)
85 b152aa84 Jes Sorensen
    ptr = qemu_oom_check(valloc(size));
86 c1b0b93b Jes Sorensen
#else
87 b152aa84 Jes Sorensen
    ptr = qemu_oom_check(memalign(alignment, size));
88 c1b0b93b Jes Sorensen
#endif
89 c1b0b93b Jes Sorensen
    trace_qemu_memalign(alignment, size, ptr);
90 c1b0b93b Jes Sorensen
    return ptr;
91 c1b0b93b Jes Sorensen
}
92 c1b0b93b Jes Sorensen
93 c1b0b93b Jes Sorensen
/* alloc shared memory pages */
94 c1b0b93b Jes Sorensen
void *qemu_vmalloc(size_t size)
95 c1b0b93b Jes Sorensen
{
96 c7f4111a Jes Sorensen
    void *ptr;
97 36b58628 Avi Kivity
    size_t align = QEMU_VMALLOC_ALIGN;
98 36b58628 Avi Kivity
99 c2a8238a Stefan Weil
#if defined(CONFIG_VALGRIND)
100 c2a8238a Stefan Weil
    if (running_on_valgrind < 0) {
101 c2a8238a Stefan Weil
        /* First call, test whether we are running on Valgrind.
102 c2a8238a Stefan Weil
           This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */
103 c2a8238a Stefan Weil
        const char *ld = getenv("LD_PRELOAD");
104 c2a8238a Stefan Weil
        running_on_valgrind = (ld != NULL && strstr(ld, "vgpreload"));
105 c2a8238a Stefan Weil
    }
106 c2a8238a Stefan Weil
#endif
107 c2a8238a Stefan Weil
108 c2a8238a Stefan Weil
    if (size < align || running_on_valgrind) {
109 36b58628 Avi Kivity
        align = getpagesize();
110 36b58628 Avi Kivity
    }
111 36b58628 Avi Kivity
    ptr = qemu_memalign(align, size);
112 c7f4111a Jes Sorensen
    trace_qemu_vmalloc(size, ptr);
113 c7f4111a Jes Sorensen
    return ptr;
114 c1b0b93b Jes Sorensen
}
115 c1b0b93b Jes Sorensen
116 c1b0b93b Jes Sorensen
void qemu_vfree(void *ptr)
117 c1b0b93b Jes Sorensen
{
118 c1b0b93b Jes Sorensen
    trace_qemu_vfree(ptr);
119 c1b0b93b Jes Sorensen
    free(ptr);
120 c1b0b93b Jes Sorensen
}
121 9549e764 Jes Sorensen
122 154b9a0c Paolo Bonzini
void socket_set_block(int fd)
123 154b9a0c Paolo Bonzini
{
124 154b9a0c Paolo Bonzini
    int f;
125 154b9a0c Paolo Bonzini
    f = fcntl(fd, F_GETFL);
126 154b9a0c Paolo Bonzini
    fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
127 154b9a0c Paolo Bonzini
}
128 154b9a0c Paolo Bonzini
129 9549e764 Jes Sorensen
void socket_set_nonblock(int fd)
130 9549e764 Jes Sorensen
{
131 9549e764 Jes Sorensen
    int f;
132 9549e764 Jes Sorensen
    f = fcntl(fd, F_GETFL);
133 9549e764 Jes Sorensen
    fcntl(fd, F_SETFL, f | O_NONBLOCK);
134 9549e764 Jes Sorensen
}
135 9549e764 Jes Sorensen
136 9549e764 Jes Sorensen
void qemu_set_cloexec(int fd)
137 9549e764 Jes Sorensen
{
138 9549e764 Jes Sorensen
    int f;
139 9549e764 Jes Sorensen
    f = fcntl(fd, F_GETFD);
140 9549e764 Jes Sorensen
    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
141 9549e764 Jes Sorensen
}
142 70e72ce4 Jes Sorensen
143 70e72ce4 Jes Sorensen
/*
144 70e72ce4 Jes Sorensen
 * Creates a pipe with FD_CLOEXEC set on both file descriptors
145 70e72ce4 Jes Sorensen
 */
146 70e72ce4 Jes Sorensen
int qemu_pipe(int pipefd[2])
147 70e72ce4 Jes Sorensen
{
148 70e72ce4 Jes Sorensen
    int ret;
149 70e72ce4 Jes Sorensen
150 70e72ce4 Jes Sorensen
#ifdef CONFIG_PIPE2
151 70e72ce4 Jes Sorensen
    ret = pipe2(pipefd, O_CLOEXEC);
152 70e72ce4 Jes Sorensen
    if (ret != -1 || errno != ENOSYS) {
153 70e72ce4 Jes Sorensen
        return ret;
154 70e72ce4 Jes Sorensen
    }
155 70e72ce4 Jes Sorensen
#endif
156 70e72ce4 Jes Sorensen
    ret = pipe(pipefd);
157 70e72ce4 Jes Sorensen
    if (ret == 0) {
158 70e72ce4 Jes Sorensen
        qemu_set_cloexec(pipefd[0]);
159 70e72ce4 Jes Sorensen
        qemu_set_cloexec(pipefd[1]);
160 70e72ce4 Jes Sorensen
    }
161 70e72ce4 Jes Sorensen
162 70e72ce4 Jes Sorensen
    return ret;
163 70e72ce4 Jes Sorensen
}
164 38671423 Hidetoshi Seto
165 38671423 Hidetoshi Seto
int qemu_utimensat(int dirfd, const char *path, const struct timespec *times,
166 38671423 Hidetoshi Seto
                   int flags)
167 38671423 Hidetoshi Seto
{
168 38671423 Hidetoshi Seto
    struct timeval tv[2], tv_now;
169 38671423 Hidetoshi Seto
    struct stat st;
170 38671423 Hidetoshi Seto
    int i;
171 38671423 Hidetoshi Seto
#ifdef CONFIG_UTIMENSAT
172 38671423 Hidetoshi Seto
    int ret;
173 38671423 Hidetoshi Seto
174 38671423 Hidetoshi Seto
    ret = utimensat(dirfd, path, times, flags);
175 38671423 Hidetoshi Seto
    if (ret != -1 || errno != ENOSYS) {
176 38671423 Hidetoshi Seto
        return ret;
177 38671423 Hidetoshi Seto
    }
178 38671423 Hidetoshi Seto
#endif
179 38671423 Hidetoshi Seto
    /* Fallback: use utimes() instead of utimensat() */
180 38671423 Hidetoshi Seto
181 38671423 Hidetoshi Seto
    /* happy if special cases */
182 38671423 Hidetoshi Seto
    if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) {
183 38671423 Hidetoshi Seto
        return 0;
184 38671423 Hidetoshi Seto
    }
185 38671423 Hidetoshi Seto
    if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) {
186 38671423 Hidetoshi Seto
        return utimes(path, NULL);
187 38671423 Hidetoshi Seto
    }
188 38671423 Hidetoshi Seto
189 38671423 Hidetoshi Seto
    /* prepare for hard cases */
190 38671423 Hidetoshi Seto
    if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) {
191 38671423 Hidetoshi Seto
        gettimeofday(&tv_now, NULL);
192 38671423 Hidetoshi Seto
    }
193 38671423 Hidetoshi Seto
    if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) {
194 38671423 Hidetoshi Seto
        stat(path, &st);
195 38671423 Hidetoshi Seto
    }
196 38671423 Hidetoshi Seto
197 38671423 Hidetoshi Seto
    for (i = 0; i < 2; i++) {
198 38671423 Hidetoshi Seto
        if (times[i].tv_nsec == UTIME_NOW) {
199 38671423 Hidetoshi Seto
            tv[i].tv_sec = tv_now.tv_sec;
200 38671423 Hidetoshi Seto
            tv[i].tv_usec = tv_now.tv_usec;
201 38671423 Hidetoshi Seto
        } else if (times[i].tv_nsec == UTIME_OMIT) {
202 38671423 Hidetoshi Seto
            tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
203 38671423 Hidetoshi Seto
            tv[i].tv_usec = 0;
204 38671423 Hidetoshi Seto
        } else {
205 38671423 Hidetoshi Seto
            tv[i].tv_sec = times[i].tv_sec;
206 38671423 Hidetoshi Seto
            tv[i].tv_usec = times[i].tv_nsec / 1000;
207 38671423 Hidetoshi Seto
        }
208 38671423 Hidetoshi Seto
    }
209 38671423 Hidetoshi Seto
210 38671423 Hidetoshi Seto
    return utimes(path, &tv[0]);
211 38671423 Hidetoshi Seto
}