osdep.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 qemu_madvise(void *addr, size_t len, int advice)
  47. {
  48. if (advice == QEMU_MADV_INVALID) {
  49. errno = EINVAL;
  50. return -1;
  51. }
  52. #if defined(CONFIG_MADVISE)
  53. return madvise(addr, len, advice);
  54. #elif defined(CONFIG_POSIX_MADVISE)
  55. return posix_madvise(addr, len, advice);
  56. #else
  57. errno = EINVAL;
  58. return -1;
  59. #endif
  60. }
  61. /*
  62. * Opens a file with FD_CLOEXEC set
  63. */
  64. int qemu_open(const char *name, int flags, ...)
  65. {
  66. int ret;
  67. int mode = 0;
  68. if (flags & O_CREAT) {
  69. va_list ap;
  70. va_start(ap, flags);
  71. mode = va_arg(ap, int);
  72. va_end(ap);
  73. }
  74. #ifdef O_CLOEXEC
  75. ret = open(name, flags | O_CLOEXEC, mode);
  76. #else
  77. ret = open(name, flags, mode);
  78. if (ret >= 0) {
  79. qemu_set_cloexec(ret);
  80. }
  81. #endif
  82. return ret;
  83. }
  84. /*
  85. * A variant of write(2) which handles partial write.
  86. *
  87. * Return the number of bytes transferred.
  88. * Set errno if fewer than `count' bytes are written.
  89. *
  90. * This function don't work with non-blocking fd's.
  91. * Any of the possibilities with non-bloking fd's is bad:
  92. * - return a short write (then name is wrong)
  93. * - busy wait adding (errno == EAGAIN) to the loop
  94. */
  95. ssize_t qemu_write_full(int fd, const void *buf, size_t count)
  96. {
  97. ssize_t ret = 0;
  98. ssize_t total = 0;
  99. while (count) {
  100. ret = write(fd, buf, count);
  101. if (ret < 0) {
  102. if (errno == EINTR)
  103. continue;
  104. break;
  105. }
  106. count -= ret;
  107. buf += ret;
  108. total += ret;
  109. }
  110. return total;
  111. }
  112. /*
  113. * Opens a socket with FD_CLOEXEC set
  114. */
  115. int qemu_socket(int domain, int type, int protocol)
  116. {
  117. int ret;
  118. #ifdef SOCK_CLOEXEC
  119. ret = socket(domain, type | SOCK_CLOEXEC, protocol);
  120. if (ret != -1 || errno != EINVAL) {
  121. return ret;
  122. }
  123. #endif
  124. ret = socket(domain, type, protocol);
  125. if (ret >= 0) {
  126. qemu_set_cloexec(ret);
  127. }
  128. return ret;
  129. }
  130. /*
  131. * Accept a connection and set FD_CLOEXEC
  132. */
  133. int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
  134. {
  135. int ret;
  136. #ifdef CONFIG_ACCEPT4
  137. ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
  138. if (ret != -1 || errno != ENOSYS) {
  139. return ret;
  140. }
  141. #endif
  142. ret = accept(s, addr, addrlen);
  143. if (ret >= 0) {
  144. qemu_set_cloexec(ret);
  145. }
  146. return ret;
  147. }