Statistics
| Branch: | Revision:

root / oslib-posix.c @ 957f1f99

History | View | Annotate | Download (4.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 c1b0b93b Jes Sorensen
#include "config-host.h"
39 c1b0b93b Jes Sorensen
#include "sysemu.h"
40 c1b0b93b Jes Sorensen
#include "trace.h"
41 9549e764 Jes Sorensen
#include "qemu_socket.h"
42 c1b0b93b Jes Sorensen
43 f97742d0 Alexandre Raymond
44 f97742d0 Alexandre Raymond
45 f97742d0 Alexandre Raymond
int qemu_daemon(int nochdir, int noclose)
46 f97742d0 Alexandre Raymond
{
47 f97742d0 Alexandre Raymond
    return daemon(nochdir, noclose);
48 f97742d0 Alexandre Raymond
}
49 f97742d0 Alexandre Raymond
50 b152aa84 Jes Sorensen
void *qemu_oom_check(void *ptr)
51 c1b0b93b Jes Sorensen
{
52 c1b0b93b Jes Sorensen
    if (ptr == NULL) {
53 c1b0b93b Jes Sorensen
        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
54 c1b0b93b Jes Sorensen
        abort();
55 c1b0b93b Jes Sorensen
    }
56 c1b0b93b Jes Sorensen
    return ptr;
57 c1b0b93b Jes Sorensen
}
58 c1b0b93b Jes Sorensen
59 c1b0b93b Jes Sorensen
void *qemu_memalign(size_t alignment, size_t size)
60 c1b0b93b Jes Sorensen
{
61 c1b0b93b Jes Sorensen
    void *ptr;
62 c1b0b93b Jes Sorensen
#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
63 c1b0b93b Jes Sorensen
    int ret;
64 c1b0b93b Jes Sorensen
    ret = posix_memalign(&ptr, alignment, size);
65 c1b0b93b Jes Sorensen
    if (ret != 0) {
66 c1b0b93b Jes Sorensen
        fprintf(stderr, "Failed to allocate %zu B: %s\n",
67 c1b0b93b Jes Sorensen
                size, strerror(ret));
68 c1b0b93b Jes Sorensen
        abort();
69 c1b0b93b Jes Sorensen
    }
70 c1b0b93b Jes Sorensen
#elif defined(CONFIG_BSD)
71 b152aa84 Jes Sorensen
    ptr = qemu_oom_check(valloc(size));
72 c1b0b93b Jes Sorensen
#else
73 b152aa84 Jes Sorensen
    ptr = qemu_oom_check(memalign(alignment, size));
74 c1b0b93b Jes Sorensen
#endif
75 c1b0b93b Jes Sorensen
    trace_qemu_memalign(alignment, size, ptr);
76 c1b0b93b Jes Sorensen
    return ptr;
77 c1b0b93b Jes Sorensen
}
78 c1b0b93b Jes Sorensen
79 c1b0b93b Jes Sorensen
/* alloc shared memory pages */
80 c1b0b93b Jes Sorensen
void *qemu_vmalloc(size_t size)
81 c1b0b93b Jes Sorensen
{
82 c7f4111a Jes Sorensen
    void *ptr;
83 c7f4111a Jes Sorensen
    ptr = qemu_memalign(getpagesize(), size);
84 c7f4111a Jes Sorensen
    trace_qemu_vmalloc(size, ptr);
85 c7f4111a Jes Sorensen
    return ptr;
86 c1b0b93b Jes Sorensen
}
87 c1b0b93b Jes Sorensen
88 c1b0b93b Jes Sorensen
void qemu_vfree(void *ptr)
89 c1b0b93b Jes Sorensen
{
90 c1b0b93b Jes Sorensen
    trace_qemu_vfree(ptr);
91 c1b0b93b Jes Sorensen
    free(ptr);
92 c1b0b93b Jes Sorensen
}
93 9549e764 Jes Sorensen
94 9549e764 Jes Sorensen
void socket_set_nonblock(int fd)
95 9549e764 Jes Sorensen
{
96 9549e764 Jes Sorensen
    int f;
97 9549e764 Jes Sorensen
    f = fcntl(fd, F_GETFL);
98 9549e764 Jes Sorensen
    fcntl(fd, F_SETFL, f | O_NONBLOCK);
99 9549e764 Jes Sorensen
}
100 9549e764 Jes Sorensen
101 9549e764 Jes Sorensen
void qemu_set_cloexec(int fd)
102 9549e764 Jes Sorensen
{
103 9549e764 Jes Sorensen
    int f;
104 9549e764 Jes Sorensen
    f = fcntl(fd, F_GETFD);
105 9549e764 Jes Sorensen
    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
106 9549e764 Jes Sorensen
}
107 70e72ce4 Jes Sorensen
108 70e72ce4 Jes Sorensen
/*
109 70e72ce4 Jes Sorensen
 * Creates a pipe with FD_CLOEXEC set on both file descriptors
110 70e72ce4 Jes Sorensen
 */
111 70e72ce4 Jes Sorensen
int qemu_pipe(int pipefd[2])
112 70e72ce4 Jes Sorensen
{
113 70e72ce4 Jes Sorensen
    int ret;
114 70e72ce4 Jes Sorensen
115 70e72ce4 Jes Sorensen
#ifdef CONFIG_PIPE2
116 70e72ce4 Jes Sorensen
    ret = pipe2(pipefd, O_CLOEXEC);
117 70e72ce4 Jes Sorensen
    if (ret != -1 || errno != ENOSYS) {
118 70e72ce4 Jes Sorensen
        return ret;
119 70e72ce4 Jes Sorensen
    }
120 70e72ce4 Jes Sorensen
#endif
121 70e72ce4 Jes Sorensen
    ret = pipe(pipefd);
122 70e72ce4 Jes Sorensen
    if (ret == 0) {
123 70e72ce4 Jes Sorensen
        qemu_set_cloexec(pipefd[0]);
124 70e72ce4 Jes Sorensen
        qemu_set_cloexec(pipefd[1]);
125 70e72ce4 Jes Sorensen
    }
126 70e72ce4 Jes Sorensen
127 70e72ce4 Jes Sorensen
    return ret;
128 70e72ce4 Jes Sorensen
}
129 38671423 Hidetoshi Seto
130 38671423 Hidetoshi Seto
int qemu_utimensat(int dirfd, const char *path, const struct timespec *times,
131 38671423 Hidetoshi Seto
                   int flags)
132 38671423 Hidetoshi Seto
{
133 38671423 Hidetoshi Seto
    struct timeval tv[2], tv_now;
134 38671423 Hidetoshi Seto
    struct stat st;
135 38671423 Hidetoshi Seto
    int i;
136 38671423 Hidetoshi Seto
#ifdef CONFIG_UTIMENSAT
137 38671423 Hidetoshi Seto
    int ret;
138 38671423 Hidetoshi Seto
139 38671423 Hidetoshi Seto
    ret = utimensat(dirfd, path, times, flags);
140 38671423 Hidetoshi Seto
    if (ret != -1 || errno != ENOSYS) {
141 38671423 Hidetoshi Seto
        return ret;
142 38671423 Hidetoshi Seto
    }
143 38671423 Hidetoshi Seto
#endif
144 38671423 Hidetoshi Seto
    /* Fallback: use utimes() instead of utimensat() */
145 38671423 Hidetoshi Seto
146 38671423 Hidetoshi Seto
    /* happy if special cases */
147 38671423 Hidetoshi Seto
    if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) {
148 38671423 Hidetoshi Seto
        return 0;
149 38671423 Hidetoshi Seto
    }
150 38671423 Hidetoshi Seto
    if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) {
151 38671423 Hidetoshi Seto
        return utimes(path, NULL);
152 38671423 Hidetoshi Seto
    }
153 38671423 Hidetoshi Seto
154 38671423 Hidetoshi Seto
    /* prepare for hard cases */
155 38671423 Hidetoshi Seto
    if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) {
156 38671423 Hidetoshi Seto
        gettimeofday(&tv_now, NULL);
157 38671423 Hidetoshi Seto
    }
158 38671423 Hidetoshi Seto
    if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) {
159 38671423 Hidetoshi Seto
        stat(path, &st);
160 38671423 Hidetoshi Seto
    }
161 38671423 Hidetoshi Seto
162 38671423 Hidetoshi Seto
    for (i = 0; i < 2; i++) {
163 38671423 Hidetoshi Seto
        if (times[i].tv_nsec == UTIME_NOW) {
164 38671423 Hidetoshi Seto
            tv[i].tv_sec = tv_now.tv_sec;
165 38671423 Hidetoshi Seto
            tv[i].tv_usec = tv_now.tv_usec;
166 38671423 Hidetoshi Seto
        } else if (times[i].tv_nsec == UTIME_OMIT) {
167 38671423 Hidetoshi Seto
            tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
168 38671423 Hidetoshi Seto
            tv[i].tv_usec = 0;
169 38671423 Hidetoshi Seto
        } else {
170 38671423 Hidetoshi Seto
            tv[i].tv_sec = times[i].tv_sec;
171 38671423 Hidetoshi Seto
            tv[i].tv_usec = times[i].tv_nsec / 1000;
172 38671423 Hidetoshi Seto
        }
173 38671423 Hidetoshi Seto
    }
174 38671423 Hidetoshi Seto
175 38671423 Hidetoshi Seto
    return utimes(path, &tv[0]);
176 38671423 Hidetoshi Seto
}