Statistics
| Branch: | Revision:

root / oslib-posix.c @ c28fa5a0

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