blob: 30426ffa9a204054c88facef08b2b46fff6a8a91 [file] [log] [blame]
bellardea888122004-02-16 22:12:40 +00001/*
2 * QEMU low level functions
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardea888122004-02-16 22:12:40 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea888122004-02-16 22:12:40 +00006 * 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>
bellardea888122004-02-16 22:12:40 +000028#include <errno.h>
29#include <unistd.h>
thsaa26bb22007-03-25 21:33:06 +000030#include <fcntl.h>
Paolo Bonzinif582af52010-02-02 20:33:11 +010031
32/* Needed early for CONFIG_BSD etc. */
33#include "config-host.h"
34
Juan Quinteladfe5fff2009-07-27 16:12:40 +020035#ifdef CONFIG_SOLARIS
ths605686c2007-01-17 23:31:19 +000036#include <sys/types.h>
37#include <sys/statvfs.h>
38#endif
bellardea888122004-02-16 22:12:40 +000039
Paolo Bonzinif3dfda62010-02-11 00:23:46 +010040#ifdef CONFIG_EVENTFD
41#include <sys/eventfd.h>
42#endif
43
bellard6e4255f2005-04-17 18:33:47 +000044#ifdef _WIN32
45#include <windows.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020046#elif defined(CONFIG_BSD)
bellard194884d2005-02-21 20:10:36 +000047#include <stdlib.h>
48#else
bellard49b470e2005-02-10 21:59:25 +000049#include <malloc.h>
bellard194884d2005-02-21 20:10:36 +000050#endif
bellard49b470e2005-02-10 21:59:25 +000051
blueswir1511d2b12009-03-07 15:32:56 +000052#include "qemu-common.h"
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010053#include "trace.h"
blueswir1511d2b12009-03-07 15:32:56 +000054#include "sysemu.h"
aliguori03ff3ca2008-09-15 15:51:35 +000055#include "qemu_socket.h"
56
Blue Swirld7414292009-09-12 12:36:04 +000057#if !defined(_POSIX_C_SOURCE) || defined(_WIN32) || defined(__sun__)
malcd644f8b2009-07-08 18:24:05 +040058static void *oom_check(void *ptr)
59{
60 if (ptr == NULL) {
Stefan Weild2d5adc2010-01-21 22:24:58 +010061#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
malcd644f8b2009-07-08 18:24:05 +040066 abort();
67 }
68 return ptr;
69}
70#endif
71
bellard6e4255f2005-04-17 18:33:47 +000072#if defined(_WIN32)
balrog33f00272007-12-24 14:33:24 +000073void *qemu_memalign(size_t alignment, size_t size)
74{
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010075 void *ptr;
76
malcd644f8b2009-07-08 18:24:05 +040077 if (!size) {
78 abort();
79 }
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010080 ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
81 trace_qemu_memalign(alignment, size, ptr);
82 return ptr;
balrog33f00272007-12-24 14:33:24 +000083}
bellard6e4255f2005-04-17 18:33:47 +000084
85void *qemu_vmalloc(size_t size)
86{
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010087 void *ptr;
88
bellard6e4255f2005-04-17 18:33:47 +000089 /* 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. */
malcd644f8b2009-07-08 18:24:05 +040092 if (!size) {
93 abort();
94 }
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010095 ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
96 trace_qemu_vmalloc(size, ptr);
97 return ptr;
bellard6e4255f2005-04-17 18:33:47 +000098}
99
100void qemu_vfree(void *ptr)
101{
Stefan Hajnoczicd245a12010-05-22 18:09:25 +0100102 trace_qemu_vfree(ptr);
bellard6e4255f2005-04-17 18:33:47 +0000103 VirtualFree(ptr, 0, MEM_RELEASE);
104}
105
pbrook6cb7ee82006-05-22 14:10:48 +0000106#else
107
balrog33f00272007-12-24 14:33:24 +0000108void *qemu_memalign(size_t alignment, size_t size)
109{
Stefan Hajnoczicd245a12010-05-22 18:09:25 +0100110 void *ptr;
Blue Swirld7414292009-09-12 12:36:04 +0000111#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
balrog33f00272007-12-24 14:33:24 +0000112 int ret;
balrog33f00272007-12-24 14:33:24 +0000113 ret = posix_memalign(&ptr, alignment, size);
Stefan Weild2d5adc2010-01-21 22:24:58 +0100114 if (ret != 0) {
115 fprintf(stderr, "Failed to allocate %zu B: %s\n",
116 size, strerror(ret));
malcd644f8b2009-07-08 18:24:05 +0400117 abort();
Stefan Weild2d5adc2010-01-21 22:24:58 +0100118 }
Juan Quintela71e72a12009-07-27 16:12:56 +0200119#elif defined(CONFIG_BSD)
Stefan Hajnoczicd245a12010-05-22 18:09:25 +0100120 ptr = oom_check(valloc(size));
balrog33f00272007-12-24 14:33:24 +0000121#else
Stefan Hajnoczicd245a12010-05-22 18:09:25 +0100122 ptr = oom_check(memalign(alignment, size));
balrog33f00272007-12-24 14:33:24 +0000123#endif
Stefan Hajnoczicd245a12010-05-22 18:09:25 +0100124 trace_qemu_memalign(alignment, size, ptr);
125 return ptr;
balrog33f00272007-12-24 14:33:24 +0000126}
127
bellard49b470e2005-02-10 21:59:25 +0000128/* alloc shared memory pages */
129void *qemu_vmalloc(size_t size)
130{
malc48253bd2008-11-18 01:42:15 +0000131 return qemu_memalign(getpagesize(), size);
bellard49b470e2005-02-10 21:59:25 +0000132}
133
134void qemu_vfree(void *ptr)
135{
Stefan Hajnoczicd245a12010-05-22 18:09:25 +0100136 trace_qemu_vfree(ptr);
bellard49b470e2005-02-10 21:59:25 +0000137 free(ptr);
138}
139
140#endif
141
thsaa26bb22007-03-25 21:33:06 +0000142int qemu_create_pidfile(const char *filename)
143{
144 char buffer[128];
145 int len;
146#ifndef _WIN32
147 int fd;
148
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100149 fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
thsaa26bb22007-03-25 21:33:06 +0000150 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;
thsaa26bb22007-03-25 21:33:06 +0000161 OVERLAPPED overlap;
162 BOOL ret;
Juha Riihimäki099fe232009-12-03 15:56:03 +0200163 memset(&overlap, 0, sizeof(overlap));
thsaa26bb22007-03-25 21:33:06 +0000164
Juha Riihimäki099fe232009-12-03 15:56:03 +0200165 file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
thsaa26bb22007-03-25 21:33:06 +0000166 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
167
168 if (file == INVALID_HANDLE_VALUE)
169 return -1;
170
thsaa26bb22007-03-25 21:33:06 +0000171 len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
ths5fafdf22007-09-16 21:08:06 +0000172 ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
thsaa26bb22007-03-25 21:33:06 +0000173 &overlap, NULL);
174 if (ret == 0)
175 return -1;
176#endif
177 return 0;
178}
pbrook29b3a662007-06-07 23:09:47 +0000179
180#ifdef _WIN32
181
Stefan Weil4972d592010-06-12 16:07:12 +0200182/* mingw32 needs ffs for compilations without optimization. */
183int ffs(int i)
184{
185 /* Use gcc's builtin ffs. */
186 return __builtin_ffs(i);
187}
188
pbrook29b3a662007-06-07 23:09:47 +0000189/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
190#define _W32_FT_OFFSET (116444736000000000ULL)
191
192int 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 */
aliguori03ff3ca2008-09-15 15:51:35 +0000210
211
212#ifdef _WIN32
213void socket_set_nonblock(int fd)
214{
215 unsigned long opt = 1;
216 ioctlsocket(fd, FIONBIO, &opt);
217}
218
219int 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}
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100227
228void qemu_set_cloexec(int fd)
229{
230}
231
aliguori03ff3ca2008-09-15 15:51:35 +0000232#else
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100233
aliguori03ff3ca2008-09-15 15:51:35 +0000234void socket_set_nonblock(int fd)
235{
236 int f;
237 f = fcntl(fd, F_GETFL);
238 fcntl(fd, F_SETFL, f | O_NONBLOCK);
239}
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100240
241void 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
aliguori03ff3ca2008-09-15 15:51:35 +0000248#endif
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100249
250/*
251 * Opens a file with FD_CLOEXEC set
252 */
253int 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 }
273#endif
274
275 return ret;
276}
277
Kirill A. Shutemov7b5f6992010-01-20 00:56:08 +0100278/*
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.
Juan Quintela1298cb62010-03-04 10:00:39 +0100283 *
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
Kirill A. Shutemov7b5f6992010-01-20 00:56:08 +0100288 */
289ssize_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
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100310#ifndef _WIN32
311/*
Paolo Bonzinif3dfda62010-02-11 00:23:46 +0100312 * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
313 */
314int qemu_eventfd(int fds[2])
315{
Avi Kivity153ceef2010-02-23 10:16:53 +0100316#ifdef CONFIG_EVENTFD
Paolo Bonzinif3dfda62010-02-11 00:23:46 +0100317 int ret;
318
Paolo Bonzinif3dfda62010-02-11 00:23:46 +0100319 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
339/*
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100340 * Creates a pipe with FD_CLOEXEC set on both file descriptors
341 */
342int qemu_pipe(int pipefd[2])
343{
344 int ret;
345
346#ifdef CONFIG_PIPE2
347 ret = pipe2(pipefd, O_CLOEXEC);
Andre Przywara3a03bfa2009-12-18 10:45:07 +0100348 if (ret != -1 || errno != ENOSYS) {
349 return ret;
350 }
351#endif
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100352 ret = pipe(pipefd);
353 if (ret == 0) {
354 qemu_set_cloexec(pipefd[0]);
355 qemu_set_cloexec(pipefd[1]);
356 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100357
358 return ret;
359}
360#endif
361
362/*
363 * Opens a socket with FD_CLOEXEC set
364 */
365int 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);
Andre Przywara3a03bfa2009-12-18 10:45:07 +0100371 if (ret != -1 || errno != EINVAL) {
372 return ret;
373 }
374#endif
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100375 ret = socket(domain, type, protocol);
376 if (ret >= 0) {
377 qemu_set_cloexec(ret);
378 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100379
380 return ret;
381}
382
383/*
384 * Accept a connection and set FD_CLOEXEC
385 */
386int 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);
Kevin Wolf347ed552010-01-13 16:20:56 +0100392 if (ret != -1 || errno != ENOSYS) {
Andre Przywara3a03bfa2009-12-18 10:45:07 +0100393 return ret;
394 }
395#endif
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100396 ret = accept(s, addr, addrlen);
397 if (ret >= 0) {
398 qemu_set_cloexec(ret);
399 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100400
401 return ret;
402}