2
0

oslib-win32.c 20 KB

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