root / oslib-posix.c @ e3e87df4
History | View | Annotate | Download (6.3 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 | cbcfa041 | Paolo Bonzini | #ifdef CONFIG_LINUX
|
59 | cbcfa041 | Paolo Bonzini | #include <sys/syscall.h> |
60 | cbcfa041 | Paolo Bonzini | #endif
|
61 | cbcfa041 | Paolo Bonzini | #ifdef CONFIG_EVENTFD
|
62 | cbcfa041 | Paolo Bonzini | #include <sys/eventfd.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 | c1b0b93b | Jes Sorensen | /* alloc shared memory pages */
|
109 | c1b0b93b | Jes Sorensen | void *qemu_vmalloc(size_t size)
|
110 | c1b0b93b | Jes Sorensen | { |
111 | c7f4111a | Jes Sorensen | void *ptr;
|
112 | 36b58628 | Avi Kivity | size_t align = QEMU_VMALLOC_ALIGN; |
113 | 36b58628 | Avi Kivity | |
114 | c2a8238a | Stefan Weil | #if defined(CONFIG_VALGRIND)
|
115 | c2a8238a | Stefan Weil | if (running_on_valgrind < 0) { |
116 | c2a8238a | Stefan Weil | /* First call, test whether we are running on Valgrind.
|
117 | c2a8238a | Stefan Weil | This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */
|
118 | c2a8238a | Stefan Weil | const char *ld = getenv("LD_PRELOAD"); |
119 | c2a8238a | Stefan Weil | running_on_valgrind = (ld != NULL && strstr(ld, "vgpreload")); |
120 | c2a8238a | Stefan Weil | } |
121 | c2a8238a | Stefan Weil | #endif
|
122 | c2a8238a | Stefan Weil | |
123 | c2a8238a | Stefan Weil | if (size < align || running_on_valgrind) {
|
124 | 36b58628 | Avi Kivity | align = getpagesize(); |
125 | 36b58628 | Avi Kivity | } |
126 | 36b58628 | Avi Kivity | ptr = qemu_memalign(align, size); |
127 | c7f4111a | Jes Sorensen | trace_qemu_vmalloc(size, ptr); |
128 | c7f4111a | Jes Sorensen | return ptr;
|
129 | c1b0b93b | Jes Sorensen | } |
130 | c1b0b93b | Jes Sorensen | |
131 | c1b0b93b | Jes Sorensen | void qemu_vfree(void *ptr) |
132 | c1b0b93b | Jes Sorensen | { |
133 | c1b0b93b | Jes Sorensen | trace_qemu_vfree(ptr); |
134 | c1b0b93b | Jes Sorensen | free(ptr); |
135 | c1b0b93b | Jes Sorensen | } |
136 | 9549e764 | Jes Sorensen | |
137 | 154b9a0c | Paolo Bonzini | void socket_set_block(int fd) |
138 | 154b9a0c | Paolo Bonzini | { |
139 | 154b9a0c | Paolo Bonzini | int f;
|
140 | 154b9a0c | Paolo Bonzini | f = fcntl(fd, F_GETFL); |
141 | 154b9a0c | Paolo Bonzini | fcntl(fd, F_SETFL, f & ~O_NONBLOCK); |
142 | 154b9a0c | Paolo Bonzini | } |
143 | 154b9a0c | Paolo Bonzini | |
144 | 9549e764 | Jes Sorensen | void socket_set_nonblock(int fd) |
145 | 9549e764 | Jes Sorensen | { |
146 | 9549e764 | Jes Sorensen | int f;
|
147 | 9549e764 | Jes Sorensen | f = fcntl(fd, F_GETFL); |
148 | 9549e764 | Jes Sorensen | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
149 | 9549e764 | Jes Sorensen | } |
150 | 9549e764 | Jes Sorensen | |
151 | 9549e764 | Jes Sorensen | void qemu_set_cloexec(int fd) |
152 | 9549e764 | Jes Sorensen | { |
153 | 9549e764 | Jes Sorensen | int f;
|
154 | 9549e764 | Jes Sorensen | f = fcntl(fd, F_GETFD); |
155 | 9549e764 | Jes Sorensen | fcntl(fd, F_SETFD, f | FD_CLOEXEC); |
156 | 9549e764 | Jes Sorensen | } |
157 | 70e72ce4 | Jes Sorensen | |
158 | 70e72ce4 | Jes Sorensen | /*
|
159 | 70e72ce4 | Jes Sorensen | * Creates a pipe with FD_CLOEXEC set on both file descriptors
|
160 | 70e72ce4 | Jes Sorensen | */
|
161 | 70e72ce4 | Jes Sorensen | int qemu_pipe(int pipefd[2]) |
162 | 70e72ce4 | Jes Sorensen | { |
163 | 70e72ce4 | Jes Sorensen | int ret;
|
164 | 70e72ce4 | Jes Sorensen | |
165 | 70e72ce4 | Jes Sorensen | #ifdef CONFIG_PIPE2
|
166 | 70e72ce4 | Jes Sorensen | ret = pipe2(pipefd, O_CLOEXEC); |
167 | 70e72ce4 | Jes Sorensen | if (ret != -1 || errno != ENOSYS) { |
168 | 70e72ce4 | Jes Sorensen | return ret;
|
169 | 70e72ce4 | Jes Sorensen | } |
170 | 70e72ce4 | Jes Sorensen | #endif
|
171 | 70e72ce4 | Jes Sorensen | ret = pipe(pipefd); |
172 | 70e72ce4 | Jes Sorensen | if (ret == 0) { |
173 | 70e72ce4 | Jes Sorensen | qemu_set_cloexec(pipefd[0]);
|
174 | 70e72ce4 | Jes Sorensen | qemu_set_cloexec(pipefd[1]);
|
175 | 70e72ce4 | Jes Sorensen | } |
176 | 70e72ce4 | Jes Sorensen | |
177 | 70e72ce4 | Jes Sorensen | return ret;
|
178 | 70e72ce4 | Jes Sorensen | } |
179 | 38671423 | Hidetoshi Seto | |
180 | cbcfa041 | Paolo Bonzini | /*
|
181 | cbcfa041 | Paolo Bonzini | * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
|
182 | cbcfa041 | Paolo Bonzini | */
|
183 | cbcfa041 | Paolo Bonzini | int qemu_eventfd(int fds[2]) |
184 | cbcfa041 | Paolo Bonzini | { |
185 | cbcfa041 | Paolo Bonzini | #ifdef CONFIG_EVENTFD
|
186 | cbcfa041 | Paolo Bonzini | int ret;
|
187 | cbcfa041 | Paolo Bonzini | |
188 | cbcfa041 | Paolo Bonzini | ret = eventfd(0, 0); |
189 | cbcfa041 | Paolo Bonzini | if (ret >= 0) { |
190 | cbcfa041 | Paolo Bonzini | fds[0] = ret;
|
191 | cbcfa041 | Paolo Bonzini | fds[1] = dup(ret);
|
192 | cbcfa041 | Paolo Bonzini | if (fds[1] == -1) { |
193 | cbcfa041 | Paolo Bonzini | close(ret); |
194 | cbcfa041 | Paolo Bonzini | return -1; |
195 | cbcfa041 | Paolo Bonzini | } |
196 | cbcfa041 | Paolo Bonzini | qemu_set_cloexec(ret); |
197 | cbcfa041 | Paolo Bonzini | qemu_set_cloexec(fds[1]);
|
198 | cbcfa041 | Paolo Bonzini | return 0; |
199 | cbcfa041 | Paolo Bonzini | } |
200 | cbcfa041 | Paolo Bonzini | if (errno != ENOSYS) {
|
201 | cbcfa041 | Paolo Bonzini | return -1; |
202 | cbcfa041 | Paolo Bonzini | } |
203 | cbcfa041 | Paolo Bonzini | #endif
|
204 | cbcfa041 | Paolo Bonzini | |
205 | cbcfa041 | Paolo Bonzini | return qemu_pipe(fds);
|
206 | cbcfa041 | Paolo Bonzini | } |
207 | cbcfa041 | Paolo Bonzini | |
208 | ae0f940e | Paolo Bonzini | int qemu_utimens(const char *path, const struct timespec *times) |
209 | 38671423 | Hidetoshi Seto | { |
210 | 38671423 | Hidetoshi Seto | struct timeval tv[2], tv_now; |
211 | 38671423 | Hidetoshi Seto | struct stat st;
|
212 | 38671423 | Hidetoshi Seto | int i;
|
213 | 38671423 | Hidetoshi Seto | #ifdef CONFIG_UTIMENSAT
|
214 | 38671423 | Hidetoshi Seto | int ret;
|
215 | 38671423 | Hidetoshi Seto | |
216 | ae0f940e | Paolo Bonzini | ret = utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW); |
217 | 38671423 | Hidetoshi Seto | if (ret != -1 || errno != ENOSYS) { |
218 | 38671423 | Hidetoshi Seto | return ret;
|
219 | 38671423 | Hidetoshi Seto | } |
220 | 38671423 | Hidetoshi Seto | #endif
|
221 | 38671423 | Hidetoshi Seto | /* Fallback: use utimes() instead of utimensat() */
|
222 | 38671423 | Hidetoshi Seto | |
223 | 38671423 | Hidetoshi Seto | /* happy if special cases */
|
224 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) { |
225 | 38671423 | Hidetoshi Seto | return 0; |
226 | 38671423 | Hidetoshi Seto | } |
227 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) { |
228 | 38671423 | Hidetoshi Seto | return utimes(path, NULL); |
229 | 38671423 | Hidetoshi Seto | } |
230 | 38671423 | Hidetoshi Seto | |
231 | 38671423 | Hidetoshi Seto | /* prepare for hard cases */
|
232 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) { |
233 | 38671423 | Hidetoshi Seto | gettimeofday(&tv_now, NULL);
|
234 | 38671423 | Hidetoshi Seto | } |
235 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) { |
236 | 38671423 | Hidetoshi Seto | stat(path, &st); |
237 | 38671423 | Hidetoshi Seto | } |
238 | 38671423 | Hidetoshi Seto | |
239 | 38671423 | Hidetoshi Seto | for (i = 0; i < 2; i++) { |
240 | 38671423 | Hidetoshi Seto | if (times[i].tv_nsec == UTIME_NOW) {
|
241 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = tv_now.tv_sec; |
242 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = tv_now.tv_usec; |
243 | 38671423 | Hidetoshi Seto | } else if (times[i].tv_nsec == UTIME_OMIT) { |
244 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
|
245 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = 0;
|
246 | 38671423 | Hidetoshi Seto | } else {
|
247 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = times[i].tv_sec; |
248 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = times[i].tv_nsec / 1000;
|
249 | 38671423 | Hidetoshi Seto | } |
250 | 38671423 | Hidetoshi Seto | } |
251 | 38671423 | Hidetoshi Seto | |
252 | 38671423 | Hidetoshi Seto | return utimes(path, &tv[0]); |
253 | 38671423 | Hidetoshi Seto | } |