osdep.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /* Needed early for CONFIG_BSD etc. */
  32. #include "config-host.h"
  33. #if defined(CONFIG_MADVISE) || defined(CONFIG_POSIX_MADVISE)
  34. #include <sys/mman.h>
  35. #endif
  36. #ifdef CONFIG_SOLARIS
  37. #include <sys/types.h>
  38. #include <sys/statvfs.h>
  39. /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
  40. discussion about Solaris header problems */
  41. extern int madvise(caddr_t, size_t, int);
  42. #endif
  43. #include "qemu-common.h"
  44. #include "trace.h"
  45. #include "qemu_socket.h"
  46. int socket_set_cork(int fd, int v)
  47. {
  48. #if defined(SOL_TCP) && defined(TCP_CORK)
  49. return setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v));
  50. #else
  51. return 0;
  52. #endif
  53. }
  54. int qemu_madvise(void *addr, size_t len, int advice)
  55. {
  56. if (advice == QEMU_MADV_INVALID) {
  57. errno = EINVAL;
  58. return -1;
  59. }
  60. #if defined(CONFIG_MADVISE)
  61. return madvise(addr, len, advice);
  62. #elif defined(CONFIG_POSIX_MADVISE)
  63. return posix_madvise(addr, len, advice);
  64. #else
  65. errno = EINVAL;
  66. return -1;
  67. #endif
  68. }
  69. /*
  70. * Opens a file with FD_CLOEXEC set
  71. */
  72. int qemu_open(const char *name, int flags, ...)
  73. {
  74. int ret;
  75. int mode = 0;
  76. if (flags & O_CREAT) {
  77. va_list ap;
  78. va_start(ap, flags);
  79. mode = va_arg(ap, int);
  80. va_end(ap);
  81. }
  82. #ifdef O_CLOEXEC
  83. ret = open(name, flags | O_CLOEXEC, mode);
  84. #else
  85. ret = open(name, flags, mode);
  86. if (ret >= 0) {
  87. qemu_set_cloexec(ret);
  88. }
  89. #endif
  90. return ret;
  91. }
  92. /*
  93. * A variant of write(2) which handles partial write.
  94. *
  95. * Return the number of bytes transferred.
  96. * Set errno if fewer than `count' bytes are written.
  97. *
  98. * This function don't work with non-blocking fd's.
  99. * Any of the possibilities with non-bloking fd's is bad:
  100. * - return a short write (then name is wrong)
  101. * - busy wait adding (errno == EAGAIN) to the loop
  102. */
  103. ssize_t qemu_write_full(int fd, const void *buf, size_t count)
  104. {
  105. ssize_t ret = 0;
  106. ssize_t total = 0;
  107. while (count) {
  108. ret = write(fd, buf, count);
  109. if (ret < 0) {
  110. if (errno == EINTR)
  111. continue;
  112. break;
  113. }
  114. count -= ret;
  115. buf += ret;
  116. total += ret;
  117. }
  118. return total;
  119. }
  120. /*
  121. * Opens a socket with FD_CLOEXEC set
  122. */
  123. int qemu_socket(int domain, int type, int protocol)
  124. {
  125. int ret;
  126. #ifdef SOCK_CLOEXEC
  127. ret = socket(domain, type | SOCK_CLOEXEC, protocol);
  128. if (ret != -1 || errno != EINVAL) {
  129. return ret;
  130. }
  131. #endif
  132. ret = socket(domain, type, protocol);
  133. if (ret >= 0) {
  134. qemu_set_cloexec(ret);
  135. }
  136. return ret;
  137. }
  138. /*
  139. * Accept a connection and set FD_CLOEXEC
  140. */
  141. int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
  142. {
  143. int ret;
  144. #ifdef CONFIG_ACCEPT4
  145. ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
  146. if (ret != -1 || errno != ENOSYS) {
  147. return ret;
  148. }
  149. #endif
  150. ret = accept(s, addr, addrlen);
  151. if (ret >= 0) {
  152. qemu_set_cloexec(ret);
  153. }
  154. return ret;
  155. }
  156. /*
  157. * A variant of send(2) which handles partial write.
  158. *
  159. * Return the number of bytes transferred, which is only
  160. * smaller than `count' if there is an error.
  161. *
  162. * This function won't work with non-blocking fd's.
  163. * Any of the possibilities with non-bloking fd's is bad:
  164. * - return a short write (then name is wrong)
  165. * - busy wait adding (errno == EAGAIN) to the loop
  166. */
  167. ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
  168. {
  169. ssize_t ret = 0;
  170. ssize_t total = 0;
  171. while (count) {
  172. ret = send(fd, buf, count, flags);
  173. if (ret < 0) {
  174. if (errno == EINTR) {
  175. continue;
  176. }
  177. break;
  178. }
  179. count -= ret;
  180. buf += ret;
  181. total += ret;
  182. }
  183. return total;
  184. }
  185. /*
  186. * A variant of recv(2) which handles partial write.
  187. *
  188. * Return the number of bytes transferred, which is only
  189. * smaller than `count' if there is an error.
  190. *
  191. * This function won't work with non-blocking fd's.
  192. * Any of the possibilities with non-bloking fd's is bad:
  193. * - return a short write (then name is wrong)
  194. * - busy wait adding (errno == EAGAIN) to the loop
  195. */
  196. ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
  197. {
  198. ssize_t ret = 0;
  199. ssize_t total = 0;
  200. while (count) {
  201. ret = qemu_recv(fd, buf, count, flags);
  202. if (ret <= 0) {
  203. if (ret < 0 && errno == EINTR) {
  204. continue;
  205. }
  206. break;
  207. }
  208. count -= ret;
  209. buf += ret;
  210. total += ret;
  211. }
  212. return total;
  213. }