]>
Commit | Line | Data |
---|---|---|
ea88812f FB |
1 | /* |
2 | * QEMU low level functions | |
5fafdf24 | 3 | * |
ea88812f | 4 | * Copyright (c) 2003 Fabrice Bellard |
5fafdf24 | 5 | * |
ea88812f FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | #include <stdlib.h> | |
25 | #include <stdio.h> | |
26 | #include <stdarg.h> | |
27 | #include <string.h> | |
ea88812f FB |
28 | #include <errno.h> |
29 | #include <unistd.h> | |
aa26bb2d | 30 | #include <fcntl.h> |
f582af58 PB |
31 | |
32 | /* Needed early for CONFIG_BSD etc. */ | |
33 | #include "config-host.h" | |
34 | ||
dfe5fff3 | 35 | #ifdef CONFIG_SOLARIS |
605686cd TS |
36 | #include <sys/types.h> |
37 | #include <sys/statvfs.h> | |
38 | #endif | |
ea88812f | 39 | |
f3dfda61 PB |
40 | #ifdef CONFIG_EVENTFD |
41 | #include <sys/eventfd.h> | |
42 | #endif | |
43 | ||
6e4255f6 FB |
44 | #ifdef _WIN32 |
45 | #include <windows.h> | |
71e72a19 | 46 | #elif defined(CONFIG_BSD) |
194884dd FB |
47 | #include <stdlib.h> |
48 | #else | |
49b470eb | 49 | #include <malloc.h> |
194884dd | 50 | #endif |
49b470eb | 51 | |
511d2b14 | 52 | #include "qemu-common.h" |
cd245a19 | 53 | #include "trace.h" |
511d2b14 | 54 | #include "sysemu.h" |
03ff3ca3 AL |
55 | #include "qemu_socket.h" |
56 | ||
d741429a | 57 | #if !defined(_POSIX_C_SOURCE) || defined(_WIN32) || defined(__sun__) |
d644f8be | 58 | static void *oom_check(void *ptr) |
59 | { | |
60 | if (ptr == NULL) { | |
d2d5adcb SW |
61 | #if defined(_WIN32) |
62 | fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError()); | |
63 | #else | |
64 | fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno)); | |
65 | #endif | |
d644f8be | 66 | abort(); |
67 | } | |
68 | return ptr; | |
69 | } | |
70 | #endif | |
71 | ||
6e4255f6 | 72 | #if defined(_WIN32) |
33f00271 AZ |
73 | void *qemu_memalign(size_t alignment, size_t size) |
74 | { | |
cd245a19 SH |
75 | void *ptr; |
76 | ||
d644f8be | 77 | if (!size) { |
78 | abort(); | |
79 | } | |
cd245a19 SH |
80 | ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); |
81 | trace_qemu_memalign(alignment, size, ptr); | |
82 | return ptr; | |
33f00271 | 83 | } |
6e4255f6 FB |
84 | |
85 | void *qemu_vmalloc(size_t size) | |
86 | { | |
cd245a19 SH |
87 | void *ptr; |
88 | ||
6e4255f6 FB |
89 | /* FIXME: this is not exactly optimal solution since VirtualAlloc |
90 | has 64Kb granularity, but at least it guarantees us that the | |
91 | memory is page aligned. */ | |
d644f8be | 92 | if (!size) { |
93 | abort(); | |
94 | } | |
cd245a19 SH |
95 | ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); |
96 | trace_qemu_vmalloc(size, ptr); | |
97 | return ptr; | |
6e4255f6 FB |
98 | } |
99 | ||
100 | void qemu_vfree(void *ptr) | |
101 | { | |
cd245a19 | 102 | trace_qemu_vfree(ptr); |
6e4255f6 FB |
103 | VirtualFree(ptr, 0, MEM_RELEASE); |
104 | } | |
105 | ||
6cb7ee85 PB |
106 | #else |
107 | ||
33f00271 AZ |
108 | void *qemu_memalign(size_t alignment, size_t size) |
109 | { | |
cd245a19 | 110 | void *ptr; |
d741429a | 111 | #if defined(_POSIX_C_SOURCE) && !defined(__sun__) |
33f00271 | 112 | int ret; |
33f00271 | 113 | ret = posix_memalign(&ptr, alignment, size); |
d2d5adcb SW |
114 | if (ret != 0) { |
115 | fprintf(stderr, "Failed to allocate %zu B: %s\n", | |
116 | size, strerror(ret)); | |
d644f8be | 117 | abort(); |
d2d5adcb | 118 | } |
71e72a19 | 119 | #elif defined(CONFIG_BSD) |
cd245a19 | 120 | ptr = oom_check(valloc(size)); |
33f00271 | 121 | #else |
cd245a19 | 122 | ptr = oom_check(memalign(alignment, size)); |
33f00271 | 123 | #endif |
cd245a19 SH |
124 | trace_qemu_memalign(alignment, size, ptr); |
125 | return ptr; | |
33f00271 AZ |
126 | } |
127 | ||
49b470eb FB |
128 | /* alloc shared memory pages */ |
129 | void *qemu_vmalloc(size_t size) | |
130 | { | |
48253bd8 | 131 | return qemu_memalign(getpagesize(), size); |
49b470eb FB |
132 | } |
133 | ||
134 | void qemu_vfree(void *ptr) | |
135 | { | |
cd245a19 | 136 | trace_qemu_vfree(ptr); |
49b470eb FB |
137 | free(ptr); |
138 | } | |
139 | ||
140 | #endif | |
141 | ||
aa26bb2d TS |
142 | int qemu_create_pidfile(const char *filename) |
143 | { | |
144 | char buffer[128]; | |
145 | int len; | |
146 | #ifndef _WIN32 | |
147 | int fd; | |
148 | ||
40ff6d7e | 149 | fd = qemu_open(filename, O_RDWR | O_CREAT, 0600); |
aa26bb2d TS |
150 | if (fd == -1) |
151 | return -1; | |
152 | ||
153 | if (lockf(fd, F_TLOCK, 0) == -1) | |
154 | return -1; | |
155 | ||
156 | len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); | |
157 | if (write(fd, buffer, len) != len) | |
158 | return -1; | |
159 | #else | |
160 | HANDLE file; | |
aa26bb2d TS |
161 | OVERLAPPED overlap; |
162 | BOOL ret; | |
099fe236 | 163 | memset(&overlap, 0, sizeof(overlap)); |
aa26bb2d | 164 | |
099fe236 | 165 | file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
aa26bb2d TS |
166 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
167 | ||
168 | if (file == INVALID_HANDLE_VALUE) | |
169 | return -1; | |
170 | ||
aa26bb2d | 171 | len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
5fafdf24 | 172 | ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len, |
aa26bb2d TS |
173 | &overlap, NULL); |
174 | if (ret == 0) | |
175 | return -1; | |
176 | #endif | |
177 | return 0; | |
178 | } | |
29b3a662 PB |
179 | |
180 | #ifdef _WIN32 | |
181 | ||
4972d592 SW |
182 | /* mingw32 needs ffs for compilations without optimization. */ |
183 | int ffs(int i) | |
184 | { | |
185 | /* Use gcc's builtin ffs. */ | |
186 | return __builtin_ffs(i); | |
187 | } | |
188 | ||
29b3a662 PB |
189 | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ |
190 | #define _W32_FT_OFFSET (116444736000000000ULL) | |
191 | ||
192 | int qemu_gettimeofday(qemu_timeval *tp) | |
193 | { | |
194 | union { | |
195 | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ | |
196 | FILETIME ft; | |
197 | } _now; | |
198 | ||
199 | if(tp) | |
200 | { | |
201 | GetSystemTimeAsFileTime (&_now.ft); | |
202 | tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); | |
203 | tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); | |
204 | } | |
205 | /* Always return 0 as per Open Group Base Specifications Issue 6. | |
206 | Do not set errno on error. */ | |
207 | return 0; | |
208 | } | |
209 | #endif /* _WIN32 */ | |
03ff3ca3 AL |
210 | |
211 | ||
212 | #ifdef _WIN32 | |
213 | void socket_set_nonblock(int fd) | |
214 | { | |
215 | unsigned long opt = 1; | |
216 | ioctlsocket(fd, FIONBIO, &opt); | |
217 | } | |
218 | ||
219 | int inet_aton(const char *cp, struct in_addr *ia) | |
220 | { | |
221 | uint32_t addr = inet_addr(cp); | |
222 | if (addr == 0xffffffff) | |
223 | return 0; | |
224 | ia->s_addr = addr; | |
225 | return 1; | |
226 | } | |
40ff6d7e KW |
227 | |
228 | void qemu_set_cloexec(int fd) | |
229 | { | |
230 | } | |
231 | ||
03ff3ca3 | 232 | #else |
40ff6d7e | 233 | |
03ff3ca3 AL |
234 | void socket_set_nonblock(int fd) |
235 | { | |
236 | int f; | |
237 | f = fcntl(fd, F_GETFL); | |
238 | fcntl(fd, F_SETFL, f | O_NONBLOCK); | |
239 | } | |
40ff6d7e KW |
240 | |
241 | void qemu_set_cloexec(int fd) | |
242 | { | |
243 | int f; | |
244 | f = fcntl(fd, F_GETFD); | |
245 | fcntl(fd, F_SETFD, f | FD_CLOEXEC); | |
246 | } | |
247 | ||
248 | #endif | |
249 | ||
250 | /* | |
251 | * Opens a file with FD_CLOEXEC set | |
252 | */ | |
253 | int qemu_open(const char *name, int flags, ...) | |
254 | { | |
255 | int ret; | |
256 | int mode = 0; | |
257 | ||
258 | if (flags & O_CREAT) { | |
259 | va_list ap; | |
260 | ||
261 | va_start(ap, flags); | |
262 | mode = va_arg(ap, int); | |
263 | va_end(ap); | |
264 | } | |
265 | ||
266 | #ifdef O_CLOEXEC | |
267 | ret = open(name, flags | O_CLOEXEC, mode); | |
268 | #else | |
269 | ret = open(name, flags, mode); | |
270 | if (ret >= 0) { | |
271 | qemu_set_cloexec(ret); | |
272 | } | |
03ff3ca3 | 273 | #endif |
40ff6d7e KW |
274 | |
275 | return ret; | |
276 | } | |
277 | ||
7b5f699d KS |
278 | /* |
279 | * A variant of write(2) which handles partial write. | |
280 | * | |
281 | * Return the number of bytes transferred. | |
282 | * Set errno if fewer than `count' bytes are written. | |
1298cb68 JQ |
283 | * |
284 | * This function don't work with non-blocking fd's. | |
285 | * Any of the possibilities with non-bloking fd's is bad: | |
286 | * - return a short write (then name is wrong) | |
287 | * - busy wait adding (errno == EAGAIN) to the loop | |
7b5f699d KS |
288 | */ |
289 | ssize_t qemu_write_full(int fd, const void *buf, size_t count) | |
290 | { | |
291 | ssize_t ret = 0; | |
292 | ssize_t total = 0; | |
293 | ||
294 | while (count) { | |
295 | ret = write(fd, buf, count); | |
296 | if (ret < 0) { | |
297 | if (errno == EINTR) | |
298 | continue; | |
299 | break; | |
300 | } | |
301 | ||
302 | count -= ret; | |
303 | buf += ret; | |
304 | total += ret; | |
305 | } | |
306 | ||
307 | return total; | |
308 | } | |
309 | ||
40ff6d7e | 310 | #ifndef _WIN32 |
f3dfda61 PB |
311 | /* |
312 | * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set. | |
313 | */ | |
314 | int qemu_eventfd(int fds[2]) | |
315 | { | |
153ceefb | 316 | #ifdef CONFIG_EVENTFD |
f3dfda61 PB |
317 | int ret; |
318 | ||
f3dfda61 PB |
319 | ret = eventfd(0, 0); |
320 | if (ret >= 0) { | |
321 | fds[0] = ret; | |
322 | qemu_set_cloexec(ret); | |
323 | if ((fds[1] = dup(ret)) == -1) { | |
324 | close(ret); | |
325 | return -1; | |
326 | } | |
327 | qemu_set_cloexec(fds[1]); | |
328 | return 0; | |
329 | } | |
330 | ||
331 | if (errno != ENOSYS) { | |
332 | return -1; | |
333 | } | |
334 | #endif | |
335 | ||
336 | return qemu_pipe(fds); | |
337 | } | |
338 | ||
40ff6d7e KW |
339 | /* |
340 | * Creates a pipe with FD_CLOEXEC set on both file descriptors | |
341 | */ | |
342 | int qemu_pipe(int pipefd[2]) | |
343 | { | |
344 | int ret; | |
345 | ||
346 | #ifdef CONFIG_PIPE2 | |
347 | ret = pipe2(pipefd, O_CLOEXEC); | |
3a03bfa5 AP |
348 | if (ret != -1 || errno != ENOSYS) { |
349 | return ret; | |
350 | } | |
351 | #endif | |
40ff6d7e KW |
352 | ret = pipe(pipefd); |
353 | if (ret == 0) { | |
354 | qemu_set_cloexec(pipefd[0]); | |
355 | qemu_set_cloexec(pipefd[1]); | |
356 | } | |
40ff6d7e KW |
357 | |
358 | return ret; | |
359 | } | |
360 | #endif | |
361 | ||
362 | /* | |
363 | * Opens a socket with FD_CLOEXEC set | |
364 | */ | |
365 | int qemu_socket(int domain, int type, int protocol) | |
366 | { | |
367 | int ret; | |
368 | ||
369 | #ifdef SOCK_CLOEXEC | |
370 | ret = socket(domain, type | SOCK_CLOEXEC, protocol); | |
3a03bfa5 AP |
371 | if (ret != -1 || errno != EINVAL) { |
372 | return ret; | |
373 | } | |
374 | #endif | |
40ff6d7e KW |
375 | ret = socket(domain, type, protocol); |
376 | if (ret >= 0) { | |
377 | qemu_set_cloexec(ret); | |
378 | } | |
40ff6d7e KW |
379 | |
380 | return ret; | |
381 | } | |
382 | ||
383 | /* | |
384 | * Accept a connection and set FD_CLOEXEC | |
385 | */ | |
386 | int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen) | |
387 | { | |
388 | int ret; | |
389 | ||
390 | #ifdef CONFIG_ACCEPT4 | |
391 | ret = accept4(s, addr, addrlen, SOCK_CLOEXEC); | |
347ed55c | 392 | if (ret != -1 || errno != ENOSYS) { |
3a03bfa5 AP |
393 | return ret; |
394 | } | |
395 | #endif | |
40ff6d7e KW |
396 | ret = accept(s, addr, addrlen); |
397 | if (ret >= 0) { | |
398 | qemu_set_cloexec(ret); | |
399 | } | |
40ff6d7e KW |
400 | |
401 | return ret; | |
402 | } |