2
0

qemu-file-unix.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 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 "qemu-common.h"
  25. #include "qemu/iov.h"
  26. #include "qemu/sockets.h"
  27. #include "block/coroutine.h"
  28. #include "migration/qemu-file.h"
  29. typedef struct QEMUFileSocket {
  30. int fd;
  31. QEMUFile *file;
  32. } QEMUFileSocket;
  33. static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
  34. int64_t pos)
  35. {
  36. QEMUFileSocket *s = opaque;
  37. ssize_t len;
  38. ssize_t size = iov_size(iov, iovcnt);
  39. len = iov_send(s->fd, iov, iovcnt, 0, size);
  40. if (len < size) {
  41. len = -socket_error();
  42. }
  43. return len;
  44. }
  45. static int socket_get_fd(void *opaque)
  46. {
  47. QEMUFileSocket *s = opaque;
  48. return s->fd;
  49. }
  50. static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  51. {
  52. QEMUFileSocket *s = opaque;
  53. ssize_t len;
  54. for (;;) {
  55. len = qemu_recv(s->fd, buf, size, 0);
  56. if (len != -1) {
  57. break;
  58. }
  59. if (socket_error() == EAGAIN) {
  60. yield_until_fd_readable(s->fd);
  61. } else if (socket_error() != EINTR) {
  62. break;
  63. }
  64. }
  65. if (len == -1) {
  66. len = -socket_error();
  67. }
  68. return len;
  69. }
  70. static int socket_close(void *opaque)
  71. {
  72. QEMUFileSocket *s = opaque;
  73. closesocket(s->fd);
  74. g_free(s);
  75. return 0;
  76. }
  77. static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
  78. int64_t pos)
  79. {
  80. QEMUFileSocket *s = opaque;
  81. ssize_t len, offset;
  82. ssize_t size = iov_size(iov, iovcnt);
  83. ssize_t total = 0;
  84. assert(iovcnt > 0);
  85. offset = 0;
  86. while (size > 0) {
  87. /* Find the next start position; skip all full-sized vector elements */
  88. while (offset >= iov[0].iov_len) {
  89. offset -= iov[0].iov_len;
  90. iov++, iovcnt--;
  91. }
  92. /* skip `offset' bytes from the (now) first element, undo it on exit */
  93. assert(iovcnt > 0);
  94. iov[0].iov_base += offset;
  95. iov[0].iov_len -= offset;
  96. do {
  97. len = writev(s->fd, iov, iovcnt);
  98. } while (len == -1 && errno == EINTR);
  99. if (len == -1) {
  100. return -errno;
  101. }
  102. /* Undo the changes above */
  103. iov[0].iov_base -= offset;
  104. iov[0].iov_len += offset;
  105. /* Prepare for the next iteration */
  106. offset += len;
  107. total += len;
  108. size -= len;
  109. }
  110. return total;
  111. }
  112. static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  113. {
  114. QEMUFileSocket *s = opaque;
  115. ssize_t len;
  116. for (;;) {
  117. len = read(s->fd, buf, size);
  118. if (len != -1) {
  119. break;
  120. }
  121. if (errno == EAGAIN) {
  122. yield_until_fd_readable(s->fd);
  123. } else if (errno != EINTR) {
  124. break;
  125. }
  126. }
  127. if (len == -1) {
  128. len = -errno;
  129. }
  130. return len;
  131. }
  132. static int unix_close(void *opaque)
  133. {
  134. QEMUFileSocket *s = opaque;
  135. close(s->fd);
  136. g_free(s);
  137. return 0;
  138. }
  139. static const QEMUFileOps unix_read_ops = {
  140. .get_fd = socket_get_fd,
  141. .get_buffer = unix_get_buffer,
  142. .close = unix_close
  143. };
  144. static const QEMUFileOps unix_write_ops = {
  145. .get_fd = socket_get_fd,
  146. .writev_buffer = unix_writev_buffer,
  147. .close = unix_close
  148. };
  149. QEMUFile *qemu_fdopen(int fd, const char *mode)
  150. {
  151. QEMUFileSocket *s;
  152. if (mode == NULL ||
  153. (mode[0] != 'r' && mode[0] != 'w') ||
  154. mode[1] != 'b' || mode[2] != 0) {
  155. fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
  156. return NULL;
  157. }
  158. s = g_malloc0(sizeof(QEMUFileSocket));
  159. s->fd = fd;
  160. if (mode[0] == 'r') {
  161. s->file = qemu_fopen_ops(s, &unix_read_ops);
  162. } else {
  163. s->file = qemu_fopen_ops(s, &unix_write_ops);
  164. }
  165. return s->file;
  166. }
  167. static const QEMUFileOps socket_read_ops = {
  168. .get_fd = socket_get_fd,
  169. .get_buffer = socket_get_buffer,
  170. .close = socket_close
  171. };
  172. static const QEMUFileOps socket_write_ops = {
  173. .get_fd = socket_get_fd,
  174. .writev_buffer = socket_writev_buffer,
  175. .close = socket_close
  176. };
  177. QEMUFile *qemu_fopen_socket(int fd, const char *mode)
  178. {
  179. QEMUFileSocket *s;
  180. if (qemu_file_mode_is_not_valid(mode)) {
  181. return NULL;
  182. }
  183. s = g_malloc0(sizeof(QEMUFileSocket));
  184. s->fd = fd;
  185. if (mode[0] == 'w') {
  186. qemu_set_block(s->fd);
  187. s->file = qemu_fopen_ops(s, &socket_write_ops);
  188. } else {
  189. s->file = qemu_fopen_ops(s, &socket_read_ops);
  190. }
  191. return s->file;
  192. }