]>
Commit | Line | Data |
---|---|---|
c1b0b93b JS |
1 | /* |
2 | * os-win32.c | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
c6196440 | 5 | * Copyright (c) 2010-2016 Red Hat, Inc. |
c1b0b93b JS |
6 | * |
7 | * QEMU library functions for win32 which are shared between QEMU and | |
8 | * the QEMU tools. | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
e637aa66 SW |
27 | * |
28 | * The implementation of g_poll (functions poll_rest, g_poll) at the end of | |
29 | * this file are based on code from GNOME glib-2 and use a different license, | |
30 | * see the license comment there. | |
c1b0b93b | 31 | */ |
aafd7584 | 32 | #include "qemu/osdep.h" |
c1b0b93b | 33 | #include <windows.h> |
e2ea3515 | 34 | #include <glib.h> |
9c17d615 | 35 | #include "sysemu/sysemu.h" |
1de7afc9 | 36 | #include "qemu/main-loop.h" |
c1b0b93b | 37 | #include "trace.h" |
1de7afc9 | 38 | #include "qemu/sockets.h" |
c1b0b93b | 39 | |
e2ea3515 LE |
40 | /* this must come after including "trace.h" */ |
41 | #include <shlobj.h> | |
42 | ||
b152aa84 | 43 | void *qemu_oom_check(void *ptr) |
c1b0b93b JS |
44 | { |
45 | if (ptr == NULL) { | |
46 | fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError()); | |
47 | abort(); | |
48 | } | |
49 | return ptr; | |
50 | } | |
51 | ||
7d2a35cc | 52 | void *qemu_try_memalign(size_t alignment, size_t size) |
c1b0b93b JS |
53 | { |
54 | void *ptr; | |
55 | ||
56 | if (!size) { | |
57 | abort(); | |
58 | } | |
7d2a35cc | 59 | ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); |
c1b0b93b JS |
60 | trace_qemu_memalign(alignment, size, ptr); |
61 | return ptr; | |
62 | } | |
63 | ||
7d2a35cc KW |
64 | void *qemu_memalign(size_t alignment, size_t size) |
65 | { | |
66 | return qemu_oom_check(qemu_try_memalign(alignment, size)); | |
67 | } | |
68 | ||
a2b257d6 | 69 | void *qemu_anon_ram_alloc(size_t size, uint64_t *align) |
c1b0b93b JS |
70 | { |
71 | void *ptr; | |
72 | ||
73 | /* FIXME: this is not exactly optimal solution since VirtualAlloc | |
74 | has 64Kb granularity, but at least it guarantees us that the | |
75 | memory is page aligned. */ | |
39228250 | 76 | ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); |
6eebf958 | 77 | trace_qemu_anon_ram_alloc(size, ptr); |
c1b0b93b JS |
78 | return ptr; |
79 | } | |
80 | ||
81 | void qemu_vfree(void *ptr) | |
82 | { | |
83 | trace_qemu_vfree(ptr); | |
94c8ff3a MA |
84 | if (ptr) { |
85 | VirtualFree(ptr, 0, MEM_RELEASE); | |
86 | } | |
c1b0b93b | 87 | } |
9549e764 | 88 | |
e7a09b92 PB |
89 | void qemu_anon_ram_free(void *ptr, size_t size) |
90 | { | |
91 | trace_qemu_anon_ram_free(ptr, size); | |
92 | if (ptr) { | |
93 | VirtualFree(ptr, 0, MEM_RELEASE); | |
94 | } | |
95 | } | |
96 | ||
4d9310f4 | 97 | #ifndef CONFIG_LOCALTIME_R |
d3e8f957 SW |
98 | /* FIXME: add proper locking */ |
99 | struct tm *gmtime_r(const time_t *timep, struct tm *result) | |
100 | { | |
101 | struct tm *p = gmtime(timep); | |
102 | memset(result, 0, sizeof(*result)); | |
103 | if (p) { | |
104 | *result = *p; | |
105 | p = result; | |
106 | } | |
107 | return p; | |
108 | } | |
109 | ||
110 | /* FIXME: add proper locking */ | |
111 | struct tm *localtime_r(const time_t *timep, struct tm *result) | |
112 | { | |
113 | struct tm *p = localtime(timep); | |
114 | memset(result, 0, sizeof(*result)); | |
115 | if (p) { | |
116 | *result = *p; | |
117 | p = result; | |
118 | } | |
119 | return p; | |
120 | } | |
4d9310f4 | 121 | #endif /* CONFIG_LOCALTIME_R */ |
d3e8f957 | 122 | |
f9e8cacc | 123 | void qemu_set_block(int fd) |
154b9a0c PB |
124 | { |
125 | unsigned long opt = 0; | |
d3385eb4 | 126 | WSAEventSelect(fd, NULL, 0); |
154b9a0c PB |
127 | ioctlsocket(fd, FIONBIO, &opt); |
128 | } | |
129 | ||
f9e8cacc | 130 | void qemu_set_nonblock(int fd) |
9549e764 JS |
131 | { |
132 | unsigned long opt = 1; | |
133 | ioctlsocket(fd, FIONBIO, &opt); | |
d3385eb4 | 134 | qemu_fd_register(fd); |
9549e764 JS |
135 | } |
136 | ||
606600a1 SO |
137 | int socket_set_fast_reuse(int fd) |
138 | { | |
139 | /* Enabling the reuse of an endpoint that was used by a socket still in | |
140 | * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows | |
141 | * fast reuse is the default and SO_REUSEADDR does strange things. So we | |
142 | * don't have to do anything here. More info can be found at: | |
143 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */ | |
144 | return 0; | |
145 | } | |
146 | ||
c6196440 | 147 | |
b16a44e1 | 148 | static int socket_error(void) |
c6196440 DB |
149 | { |
150 | switch (WSAGetLastError()) { | |
151 | case 0: | |
152 | return 0; | |
153 | case WSAEINTR: | |
154 | return EINTR; | |
155 | case WSAEINVAL: | |
156 | return EINVAL; | |
157 | case WSA_INVALID_HANDLE: | |
158 | return EBADF; | |
159 | case WSA_NOT_ENOUGH_MEMORY: | |
160 | return ENOMEM; | |
161 | case WSA_INVALID_PARAMETER: | |
162 | return EINVAL; | |
163 | case WSAENAMETOOLONG: | |
164 | return ENAMETOOLONG; | |
165 | case WSAENOTEMPTY: | |
166 | return ENOTEMPTY; | |
167 | case WSAEWOULDBLOCK: | |
168 | /* not using EWOULDBLOCK as we don't want code to have | |
169 | * to check both EWOULDBLOCK and EAGAIN */ | |
170 | return EAGAIN; | |
171 | case WSAEINPROGRESS: | |
172 | return EINPROGRESS; | |
173 | case WSAEALREADY: | |
174 | return EALREADY; | |
175 | case WSAENOTSOCK: | |
176 | return ENOTSOCK; | |
177 | case WSAEDESTADDRREQ: | |
178 | return EDESTADDRREQ; | |
179 | case WSAEMSGSIZE: | |
180 | return EMSGSIZE; | |
181 | case WSAEPROTOTYPE: | |
182 | return EPROTOTYPE; | |
183 | case WSAENOPROTOOPT: | |
184 | return ENOPROTOOPT; | |
185 | case WSAEPROTONOSUPPORT: | |
186 | return EPROTONOSUPPORT; | |
187 | case WSAEOPNOTSUPP: | |
188 | return EOPNOTSUPP; | |
189 | case WSAEAFNOSUPPORT: | |
190 | return EAFNOSUPPORT; | |
191 | case WSAEADDRINUSE: | |
192 | return EADDRINUSE; | |
193 | case WSAEADDRNOTAVAIL: | |
194 | return EADDRNOTAVAIL; | |
195 | case WSAENETDOWN: | |
196 | return ENETDOWN; | |
197 | case WSAENETUNREACH: | |
198 | return ENETUNREACH; | |
199 | case WSAENETRESET: | |
200 | return ENETRESET; | |
201 | case WSAECONNABORTED: | |
202 | return ECONNABORTED; | |
203 | case WSAECONNRESET: | |
204 | return ECONNRESET; | |
205 | case WSAENOBUFS: | |
206 | return ENOBUFS; | |
207 | case WSAEISCONN: | |
208 | return EISCONN; | |
209 | case WSAENOTCONN: | |
210 | return ENOTCONN; | |
211 | case WSAETIMEDOUT: | |
212 | return ETIMEDOUT; | |
213 | case WSAECONNREFUSED: | |
214 | return ECONNREFUSED; | |
215 | case WSAELOOP: | |
216 | return ELOOP; | |
217 | case WSAEHOSTUNREACH: | |
218 | return EHOSTUNREACH; | |
219 | default: | |
220 | return EIO; | |
221 | } | |
222 | } | |
223 | ||
9549e764 JS |
224 | int inet_aton(const char *cp, struct in_addr *ia) |
225 | { | |
226 | uint32_t addr = inet_addr(cp); | |
227 | if (addr == 0xffffffff) { | |
e637aa66 | 228 | return 0; |
9549e764 JS |
229 | } |
230 | ia->s_addr = addr; | |
231 | return 1; | |
232 | } | |
233 | ||
234 | void qemu_set_cloexec(int fd) | |
235 | { | |
236 | } | |
dc786bc9 | 237 | |
dc786bc9 JS |
238 | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ |
239 | #define _W32_FT_OFFSET (116444736000000000ULL) | |
240 | ||
241 | int qemu_gettimeofday(qemu_timeval *tp) | |
242 | { | |
243 | union { | |
244 | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ | |
245 | FILETIME ft; | |
246 | } _now; | |
247 | ||
248 | if(tp) { | |
249 | GetSystemTimeAsFileTime (&_now.ft); | |
250 | tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); | |
251 | tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); | |
252 | } | |
253 | /* Always return 0 as per Open Group Base Specifications Issue 6. | |
254 | Do not set errno on error. */ | |
255 | return 0; | |
256 | } | |
cbcfa041 PB |
257 | |
258 | int qemu_get_thread_id(void) | |
259 | { | |
260 | return GetCurrentThreadId(); | |
261 | } | |
e2ea3515 LE |
262 | |
263 | char * | |
264 | qemu_get_local_state_pathname(const char *relative_pathname) | |
265 | { | |
266 | HRESULT result; | |
267 | char base_path[MAX_PATH+1] = ""; | |
268 | ||
269 | result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, | |
270 | /* SHGFP_TYPE_CURRENT */ 0, base_path); | |
271 | if (result != S_OK) { | |
272 | /* misconfigured environment */ | |
273 | g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result); | |
274 | abort(); | |
275 | } | |
276 | return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path, | |
277 | relative_pathname); | |
278 | } | |
13401ba0 SH |
279 | |
280 | void qemu_set_tty_echo(int fd, bool echo) | |
281 | { | |
282 | HANDLE handle = (HANDLE)_get_osfhandle(fd); | |
283 | DWORD dwMode = 0; | |
284 | ||
285 | if (handle == INVALID_HANDLE_VALUE) { | |
286 | return; | |
287 | } | |
288 | ||
289 | GetConsoleMode(handle, &dwMode); | |
290 | ||
291 | if (echo) { | |
292 | SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT); | |
293 | } else { | |
294 | SetConsoleMode(handle, | |
295 | dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)); | |
296 | } | |
297 | } | |
10f5bff6 FZ |
298 | |
299 | static char exec_dir[PATH_MAX]; | |
300 | ||
301 | void qemu_init_exec_dir(const char *argv0) | |
302 | { | |
303 | ||
304 | char *p; | |
305 | char buf[MAX_PATH]; | |
306 | DWORD len; | |
307 | ||
308 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); | |
309 | if (len == 0) { | |
310 | return; | |
311 | } | |
312 | ||
313 | buf[len] = 0; | |
314 | p = buf + len - 1; | |
315 | while (p != buf && *p != '\\') { | |
316 | p--; | |
317 | } | |
318 | *p = 0; | |
319 | if (access(buf, R_OK) == 0) { | |
320 | pstrcpy(exec_dir, sizeof(exec_dir), buf); | |
321 | } | |
322 | } | |
323 | ||
324 | char *qemu_get_exec_dir(void) | |
325 | { | |
326 | return g_strdup(exec_dir); | |
327 | } | |
5a007547 SP |
328 | |
329 | /* | |
e637aa66 SW |
330 | * The original implementation of g_poll from glib has a problem on Windows |
331 | * when using timeouts < 10 ms. | |
5a007547 | 332 | * |
e637aa66 SW |
333 | * Whenever g_poll is called with timeout < 10 ms, it does a quick poll instead |
334 | * of wait. This causes significant performance degradation of QEMU. | |
5a007547 | 335 | * |
e637aa66 SW |
336 | * The following code is a copy of the original code from glib/gpoll.c |
337 | * (glib commit 20f4d1820b8d4d0fc4447188e33efffd6d4a88d8 from 2014-02-19). | |
338 | * Some debug code was removed and the code was reformatted. | |
339 | * All other code modifications are marked with 'QEMU'. | |
340 | */ | |
341 | ||
342 | /* | |
343 | * gpoll.c: poll(2) abstraction | |
344 | * Copyright 1998 Owen Taylor | |
345 | * Copyright 2008 Red Hat, Inc. | |
5a007547 | 346 | * |
e637aa66 SW |
347 | * This library is free software; you can redistribute it and/or |
348 | * modify it under the terms of the GNU Lesser General Public | |
349 | * License as published by the Free Software Foundation; either | |
350 | * version 2 of the License, or (at your option) any later version. | |
351 | * | |
352 | * This library is distributed in the hope that it will be useful, | |
353 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
354 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
355 | * Lesser General Public License for more details. | |
356 | * | |
357 | * You should have received a copy of the GNU Lesser General Public | |
358 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
5a007547 | 359 | */ |
e637aa66 SW |
360 | |
361 | static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles, | |
362 | GPollFD *fds, guint nfds, gint timeout) | |
5a007547 | 363 | { |
e637aa66 SW |
364 | DWORD ready; |
365 | GPollFD *f; | |
366 | int recursed_result; | |
5a007547 | 367 | |
e637aa66 SW |
368 | if (poll_msgs) { |
369 | /* Wait for either messages or handles | |
370 | * -> Use MsgWaitForMultipleObjectsEx | |
371 | */ | |
372 | ready = MsgWaitForMultipleObjectsEx(nhandles, handles, timeout, | |
373 | QS_ALLINPUT, MWMO_ALERTABLE); | |
5a007547 | 374 | |
e637aa66 SW |
375 | if (ready == WAIT_FAILED) { |
376 | gchar *emsg = g_win32_error_message(GetLastError()); | |
377 | g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg); | |
378 | g_free(emsg); | |
5a007547 | 379 | } |
e637aa66 SW |
380 | } else if (nhandles == 0) { |
381 | /* No handles to wait for, just the timeout */ | |
382 | if (timeout == INFINITE) { | |
383 | ready = WAIT_FAILED; | |
384 | } else { | |
385 | SleepEx(timeout, TRUE); | |
386 | ready = WAIT_TIMEOUT; | |
387 | } | |
388 | } else { | |
389 | /* Wait for just handles | |
390 | * -> Use WaitForMultipleObjectsEx | |
5a007547 | 391 | */ |
e637aa66 SW |
392 | ready = |
393 | WaitForMultipleObjectsEx(nhandles, handles, FALSE, timeout, TRUE); | |
394 | if (ready == WAIT_FAILED) { | |
395 | gchar *emsg = g_win32_error_message(GetLastError()); | |
396 | g_warning("WaitForMultipleObjectsEx failed: %s", emsg); | |
397 | g_free(emsg); | |
5a007547 | 398 | } |
e637aa66 | 399 | } |
5a007547 | 400 | |
e637aa66 SW |
401 | if (ready == WAIT_FAILED) { |
402 | return -1; | |
403 | } else if (ready == WAIT_TIMEOUT || ready == WAIT_IO_COMPLETION) { | |
404 | return 0; | |
405 | } else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles) { | |
406 | for (f = fds; f < &fds[nfds]; ++f) { | |
407 | if (f->fd == G_WIN32_MSG_HANDLE && f->events & G_IO_IN) { | |
408 | f->revents |= G_IO_IN; | |
5a007547 SP |
409 | } |
410 | } | |
5a007547 | 411 | |
e637aa66 SW |
412 | /* If we have a timeout, or no handles to poll, be satisfied |
413 | * with just noticing we have messages waiting. | |
414 | */ | |
415 | if (timeout != 0 || nhandles == 0) { | |
416 | return 1; | |
417 | } | |
5a007547 | 418 | |
e637aa66 SW |
419 | /* If no timeout and handles to poll, recurse to poll them, |
420 | * too. | |
421 | */ | |
422 | recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0); | |
423 | return (recursed_result == -1) ? -1 : 1 + recursed_result; | |
424 | } else if (/* QEMU: removed the following unneeded statement which causes | |
425 | * a compiler warning: ready >= WAIT_OBJECT_0 && */ | |
426 | ready < WAIT_OBJECT_0 + nhandles) { | |
427 | for (f = fds; f < &fds[nfds]; ++f) { | |
428 | if ((HANDLE) f->fd == handles[ready - WAIT_OBJECT_0]) { | |
429 | f->revents = f->events; | |
430 | } | |
431 | } | |
5a007547 | 432 | |
e637aa66 SW |
433 | /* If no timeout and polling several handles, recurse to poll |
434 | * the rest of them. | |
435 | */ | |
436 | if (timeout == 0 && nhandles > 1) { | |
437 | /* Remove the handle that fired */ | |
438 | int i; | |
439 | if (ready < nhandles - 1) { | |
440 | for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++) { | |
441 | handles[i-1] = handles[i]; | |
442 | } | |
443 | } | |
444 | nhandles--; | |
445 | recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0); | |
446 | return (recursed_result == -1) ? -1 : 1 + recursed_result; | |
5a007547 | 447 | } |
e637aa66 | 448 | return 1; |
5a007547 SP |
449 | } |
450 | ||
e637aa66 SW |
451 | return 0; |
452 | } | |
5a007547 | 453 | |
e637aa66 SW |
454 | gint g_poll(GPollFD *fds, guint nfds, gint timeout) |
455 | { | |
456 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; | |
457 | gboolean poll_msgs = FALSE; | |
458 | GPollFD *f; | |
459 | gint nhandles = 0; | |
460 | int retval; | |
461 | ||
462 | for (f = fds; f < &fds[nfds]; ++f) { | |
463 | if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN)) { | |
464 | poll_msgs = TRUE; | |
465 | } else if (f->fd > 0) { | |
466 | /* Don't add the same handle several times into the array, as | |
467 | * docs say that is not allowed, even if it actually does seem | |
468 | * to work. | |
469 | */ | |
470 | gint i; | |
471 | ||
472 | for (i = 0; i < nhandles; i++) { | |
473 | if (handles[i] == (HANDLE) f->fd) { | |
474 | break; | |
475 | } | |
5a007547 SP |
476 | } |
477 | ||
e637aa66 SW |
478 | if (i == nhandles) { |
479 | if (nhandles == MAXIMUM_WAIT_OBJECTS) { | |
480 | g_warning("Too many handles to wait for!\n"); | |
481 | break; | |
482 | } else { | |
483 | handles[nhandles++] = (HANDLE) f->fd; | |
484 | } | |
5a007547 SP |
485 | } |
486 | } | |
e637aa66 | 487 | } |
5a007547 | 488 | |
e637aa66 SW |
489 | for (f = fds; f < &fds[nfds]; ++f) { |
490 | f->revents = 0; | |
491 | } | |
5a007547 | 492 | |
e637aa66 SW |
493 | if (timeout == -1) { |
494 | timeout = INFINITE; | |
495 | } | |
5a007547 | 496 | |
e637aa66 SW |
497 | /* Polling for several things? */ |
498 | if (nhandles > 1 || (nhandles > 0 && poll_msgs)) { | |
499 | /* First check if one or several of them are immediately | |
500 | * available | |
501 | */ | |
502 | retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, 0); | |
503 | ||
504 | /* If not, and we have a significant timeout, poll again with | |
505 | * timeout then. Note that this will return indication for only | |
506 | * one event, or only for messages. We ignore timeouts less than | |
507 | * ten milliseconds as they are mostly pointless on Windows, the | |
508 | * MsgWaitForMultipleObjectsEx() call will timeout right away | |
509 | * anyway. | |
510 | * | |
511 | * Modification for QEMU: replaced timeout >= 10 by timeout > 0. | |
5a007547 | 512 | */ |
e637aa66 SW |
513 | if (retval == 0 && (timeout == INFINITE || timeout > 0)) { |
514 | retval = poll_rest(poll_msgs, handles, nhandles, | |
515 | fds, nfds, timeout); | |
5a007547 | 516 | } |
e637aa66 SW |
517 | } else { |
518 | /* Just polling for one thing, so no need to check first if | |
519 | * available immediately | |
520 | */ | |
521 | retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, timeout); | |
522 | } | |
5a007547 | 523 | |
e637aa66 SW |
524 | if (retval == -1) { |
525 | for (f = fds; f < &fds[nfds]; ++f) { | |
526 | f->revents = 0; | |
527 | } | |
5a007547 SP |
528 | } |
529 | ||
e637aa66 | 530 | return retval; |
5a007547 | 531 | } |
38183310 | 532 | |
a28c2f2d | 533 | int getpagesize(void) |
38183310 PB |
534 | { |
535 | SYSTEM_INFO system_info; | |
536 | ||
537 | GetSystemInfo(&system_info); | |
538 | return system_info.dwPageSize; | |
539 | } | |
540 | ||
541 | void os_mem_prealloc(int fd, char *area, size_t memory) | |
542 | { | |
543 | int i; | |
544 | size_t pagesize = getpagesize(); | |
545 | ||
546 | memory = (memory + pagesize - 1) & -pagesize; | |
547 | for (i = 0; i < memory / pagesize; i++) { | |
548 | memset(area + pagesize * i, 0, 1); | |
549 | } | |
550 | } | |
d57e4e48 DB |
551 | |
552 | ||
553 | /* XXX: put correct support for win32 */ | |
554 | int qemu_read_password(char *buf, int buf_size) | |
555 | { | |
556 | int c, i; | |
557 | ||
558 | printf("Password: "); | |
559 | fflush(stdout); | |
560 | i = 0; | |
561 | for (;;) { | |
562 | c = getchar(); | |
563 | if (c < 0) { | |
564 | buf[i] = '\0'; | |
565 | return -1; | |
566 | } else if (c == '\n') { | |
567 | break; | |
568 | } else if (i < (buf_size - 1)) { | |
569 | buf[i++] = c; | |
570 | } | |
571 | } | |
572 | buf[i] = '\0'; | |
573 | return 0; | |
574 | } | |
57cb38b3 DB |
575 | |
576 | ||
577 | pid_t qemu_fork(Error **errp) | |
578 | { | |
579 | errno = ENOSYS; | |
580 | error_setg_errno(errp, errno, | |
581 | "cannot fork child process"); | |
582 | return -1; | |
583 | } | |
a2d96af4 DB |
584 | |
585 | ||
586 | #undef connect | |
587 | int qemu_connect_wrap(int sockfd, const struct sockaddr *addr, | |
588 | socklen_t addrlen) | |
589 | { | |
590 | int ret; | |
591 | ret = connect(sockfd, addr, addrlen); | |
592 | if (ret < 0) { | |
593 | errno = socket_error(); | |
594 | } | |
595 | return ret; | |
596 | } | |
597 | ||
598 | ||
599 | #undef listen | |
600 | int qemu_listen_wrap(int sockfd, int backlog) | |
601 | { | |
602 | int ret; | |
603 | ret = listen(sockfd, backlog); | |
604 | if (ret < 0) { | |
605 | errno = socket_error(); | |
606 | } | |
607 | return ret; | |
608 | } | |
609 | ||
610 | ||
611 | #undef bind | |
612 | int qemu_bind_wrap(int sockfd, const struct sockaddr *addr, | |
613 | socklen_t addrlen) | |
614 | { | |
615 | int ret; | |
616 | ret = bind(sockfd, addr, addrlen); | |
617 | if (ret < 0) { | |
618 | errno = socket_error(); | |
619 | } | |
620 | return ret; | |
621 | } | |
622 | ||
623 | ||
624 | #undef socket | |
625 | int qemu_socket_wrap(int domain, int type, int protocol) | |
626 | { | |
627 | int ret; | |
628 | ret = socket(domain, type, protocol); | |
629 | if (ret < 0) { | |
630 | errno = socket_error(); | |
631 | } | |
632 | return ret; | |
633 | } | |
634 | ||
635 | ||
636 | #undef accept | |
637 | int qemu_accept_wrap(int sockfd, struct sockaddr *addr, | |
638 | socklen_t *addrlen) | |
639 | { | |
640 | int ret; | |
641 | ret = accept(sockfd, addr, addrlen); | |
642 | if (ret < 0) { | |
643 | errno = socket_error(); | |
644 | } | |
645 | return ret; | |
646 | } | |
647 | ||
648 | ||
649 | #undef shutdown | |
650 | int qemu_shutdown_wrap(int sockfd, int how) | |
651 | { | |
652 | int ret; | |
653 | ret = shutdown(sockfd, how); | |
654 | if (ret < 0) { | |
655 | errno = socket_error(); | |
656 | } | |
657 | return ret; | |
658 | } | |
659 | ||
660 | ||
661 | #undef ioctlsocket | |
662 | int qemu_ioctlsocket_wrap(int fd, int req, void *val) | |
663 | { | |
664 | int ret; | |
665 | ret = ioctlsocket(fd, req, val); | |
666 | if (ret < 0) { | |
667 | errno = socket_error(); | |
668 | } | |
669 | return ret; | |
670 | } | |
671 | ||
672 | ||
673 | #undef closesocket | |
674 | int qemu_closesocket_wrap(int fd) | |
675 | { | |
676 | int ret; | |
677 | ret = closesocket(fd); | |
678 | if (ret < 0) { | |
679 | errno = socket_error(); | |
680 | } | |
681 | return ret; | |
682 | } | |
683 | ||
684 | ||
685 | #undef getsockopt | |
686 | int qemu_getsockopt_wrap(int sockfd, int level, int optname, | |
687 | void *optval, socklen_t *optlen) | |
688 | { | |
689 | int ret; | |
690 | ret = getsockopt(sockfd, level, optname, optval, optlen); | |
691 | if (ret < 0) { | |
692 | errno = socket_error(); | |
693 | } | |
694 | return ret; | |
695 | } | |
696 | ||
697 | ||
698 | #undef setsockopt | |
699 | int qemu_setsockopt_wrap(int sockfd, int level, int optname, | |
700 | const void *optval, socklen_t optlen) | |
701 | { | |
702 | int ret; | |
703 | ret = setsockopt(sockfd, level, optname, optval, optlen); | |
704 | if (ret < 0) { | |
705 | errno = socket_error(); | |
706 | } | |
707 | return ret; | |
708 | } | |
709 | ||
710 | ||
711 | #undef getpeername | |
712 | int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr, | |
713 | socklen_t *addrlen) | |
714 | { | |
715 | int ret; | |
716 | ret = getpeername(sockfd, addr, addrlen); | |
717 | if (ret < 0) { | |
718 | errno = socket_error(); | |
719 | } | |
720 | return ret; | |
721 | } | |
722 | ||
723 | ||
724 | #undef getsockname | |
725 | int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr, | |
726 | socklen_t *addrlen) | |
727 | { | |
728 | int ret; | |
729 | ret = getsockname(sockfd, addr, addrlen); | |
730 | if (ret < 0) { | |
731 | errno = socket_error(); | |
732 | } | |
733 | return ret; | |
734 | } | |
735 | ||
736 | ||
737 | #undef send | |
738 | ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags) | |
739 | { | |
740 | int ret; | |
741 | ret = send(sockfd, buf, len, flags); | |
742 | if (ret < 0) { | |
743 | errno = socket_error(); | |
744 | } | |
745 | return ret; | |
746 | } | |
747 | ||
748 | ||
749 | #undef sendto | |
750 | ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags, | |
751 | const struct sockaddr *addr, socklen_t addrlen) | |
752 | { | |
753 | int ret; | |
754 | ret = sendto(sockfd, buf, len, flags, addr, addrlen); | |
755 | if (ret < 0) { | |
756 | errno = socket_error(); | |
757 | } | |
758 | return ret; | |
759 | } | |
760 | ||
761 | ||
762 | #undef recv | |
763 | ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags) | |
764 | { | |
765 | int ret; | |
766 | ret = recv(sockfd, buf, len, flags); | |
767 | if (ret < 0) { | |
768 | errno = socket_error(); | |
769 | } | |
770 | return ret; | |
771 | } | |
772 | ||
773 | ||
774 | #undef recvfrom | |
775 | ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags, | |
776 | struct sockaddr *addr, socklen_t *addrlen) | |
777 | { | |
778 | int ret; | |
779 | ret = recvfrom(sockfd, buf, len, flags, addr, addrlen); | |
780 | if (ret < 0) { | |
781 | errno = socket_error(); | |
782 | } | |
783 | return ret; | |
784 | } |