root / osdep.c @ 4a1418e0
History | View | Annotate | Download (5 kB)
1 |
/*
|
---|---|
2 |
* QEMU low level functions
|
3 |
*
|
4 |
* Copyright (c) 2003 Fabrice Bellard
|
5 |
*
|
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> |
28 |
#include <errno.h> |
29 |
#include <unistd.h> |
30 |
#include <fcntl.h> |
31 |
#ifdef CONFIG_SOLARIS
|
32 |
#include <sys/types.h> |
33 |
#include <sys/statvfs.h> |
34 |
#endif
|
35 |
|
36 |
/* FIXME: This file should be target independent. However it has kqemu
|
37 |
hacks, so must be built for every target. */
|
38 |
|
39 |
/* Needed early for CONFIG_BSD etc. */
|
40 |
#include "config-host.h" |
41 |
|
42 |
#ifdef _WIN32
|
43 |
#include <windows.h> |
44 |
#elif defined(CONFIG_BSD)
|
45 |
#include <stdlib.h> |
46 |
#else
|
47 |
#include <malloc.h> |
48 |
#endif
|
49 |
|
50 |
#include "qemu-common.h" |
51 |
#include "sysemu.h" |
52 |
#include "qemu_socket.h" |
53 |
|
54 |
#if !defined(_POSIX_C_SOURCE) || defined(_WIN32)
|
55 |
static void *oom_check(void *ptr) |
56 |
{ |
57 |
if (ptr == NULL) { |
58 |
abort(); |
59 |
} |
60 |
return ptr;
|
61 |
} |
62 |
#endif
|
63 |
|
64 |
#if defined(_WIN32)
|
65 |
void *qemu_memalign(size_t alignment, size_t size)
|
66 |
{ |
67 |
if (!size) {
|
68 |
abort(); |
69 |
} |
70 |
return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); |
71 |
} |
72 |
|
73 |
void *qemu_vmalloc(size_t size)
|
74 |
{ |
75 |
/* FIXME: this is not exactly optimal solution since VirtualAlloc
|
76 |
has 64Kb granularity, but at least it guarantees us that the
|
77 |
memory is page aligned. */
|
78 |
if (!size) {
|
79 |
abort(); |
80 |
} |
81 |
return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); |
82 |
} |
83 |
|
84 |
void qemu_vfree(void *ptr) |
85 |
{ |
86 |
VirtualFree(ptr, 0, MEM_RELEASE);
|
87 |
} |
88 |
|
89 |
#else
|
90 |
|
91 |
void *qemu_memalign(size_t alignment, size_t size)
|
92 |
{ |
93 |
#if defined(_POSIX_C_SOURCE)
|
94 |
int ret;
|
95 |
void *ptr;
|
96 |
ret = posix_memalign(&ptr, alignment, size); |
97 |
if (ret != 0) |
98 |
abort(); |
99 |
return ptr;
|
100 |
#elif defined(CONFIG_BSD)
|
101 |
return oom_check(valloc(size));
|
102 |
#else
|
103 |
return oom_check(memalign(alignment, size));
|
104 |
#endif
|
105 |
} |
106 |
|
107 |
/* alloc shared memory pages */
|
108 |
void *qemu_vmalloc(size_t size)
|
109 |
{ |
110 |
return qemu_memalign(getpagesize(), size);
|
111 |
} |
112 |
|
113 |
void qemu_vfree(void *ptr) |
114 |
{ |
115 |
free(ptr); |
116 |
} |
117 |
|
118 |
#endif
|
119 |
|
120 |
int qemu_create_pidfile(const char *filename) |
121 |
{ |
122 |
char buffer[128]; |
123 |
int len;
|
124 |
#ifndef _WIN32
|
125 |
int fd;
|
126 |
|
127 |
fd = open(filename, O_RDWR | O_CREAT, 0600);
|
128 |
if (fd == -1) |
129 |
return -1; |
130 |
|
131 |
if (lockf(fd, F_TLOCK, 0) == -1) |
132 |
return -1; |
133 |
|
134 |
len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
135 |
if (write(fd, buffer, len) != len)
|
136 |
return -1; |
137 |
#else
|
138 |
HANDLE file; |
139 |
DWORD flags; |
140 |
OVERLAPPED overlap; |
141 |
BOOL ret; |
142 |
|
143 |
/* Open for writing with no sharing. */
|
144 |
file = CreateFile(filename, GENERIC_WRITE, 0, NULL, |
145 |
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
146 |
|
147 |
if (file == INVALID_HANDLE_VALUE)
|
148 |
return -1; |
149 |
|
150 |
flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY; |
151 |
overlap.hEvent = 0;
|
152 |
/* Lock 1 byte. */
|
153 |
ret = LockFileEx(file, flags, 0, 0, 1, &overlap); |
154 |
if (ret == 0) |
155 |
return -1; |
156 |
|
157 |
/* Write PID to file. */
|
158 |
len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
159 |
ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len, |
160 |
&overlap, NULL);
|
161 |
if (ret == 0) |
162 |
return -1; |
163 |
#endif
|
164 |
return 0; |
165 |
} |
166 |
|
167 |
#ifdef _WIN32
|
168 |
|
169 |
/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
|
170 |
#define _W32_FT_OFFSET (116444736000000000ULL) |
171 |
|
172 |
int qemu_gettimeofday(qemu_timeval *tp)
|
173 |
{ |
174 |
union {
|
175 |
unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ |
176 |
FILETIME ft; |
177 |
} _now; |
178 |
|
179 |
if(tp)
|
180 |
{ |
181 |
GetSystemTimeAsFileTime (&_now.ft); |
182 |
tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); |
183 |
tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); |
184 |
} |
185 |
/* Always return 0 as per Open Group Base Specifications Issue 6.
|
186 |
Do not set errno on error. */
|
187 |
return 0; |
188 |
} |
189 |
#endif /* _WIN32 */ |
190 |
|
191 |
|
192 |
#ifdef _WIN32
|
193 |
void socket_set_nonblock(int fd) |
194 |
{ |
195 |
unsigned long opt = 1; |
196 |
ioctlsocket(fd, FIONBIO, &opt); |
197 |
} |
198 |
|
199 |
int inet_aton(const char *cp, struct in_addr *ia) |
200 |
{ |
201 |
uint32_t addr = inet_addr(cp); |
202 |
if (addr == 0xffffffff) |
203 |
return 0; |
204 |
ia->s_addr = addr; |
205 |
return 1; |
206 |
} |
207 |
#else
|
208 |
void socket_set_nonblock(int fd) |
209 |
{ |
210 |
int f;
|
211 |
f = fcntl(fd, F_GETFL); |
212 |
fcntl(fd, F_SETFL, f | O_NONBLOCK); |
213 |
} |
214 |
#endif
|