Return path: Open a return path on QEMUFile for sockets
[qemu/cris-port.git] / migration / qemu-file-unix.c
blobbcb744bf547a9596f01d7758438792cf8c14f94d
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "qemu/iov.h"
26 #include "qemu/sockets.h"
27 #include "qemu/coroutine.h"
28 #include "migration/qemu-file.h"
29 #include "migration/qemu-file-internal.h"
31 typedef struct QEMUFileSocket {
32 int fd;
33 QEMUFile *file;
34 } QEMUFileSocket;
36 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
37 int64_t pos)
39 QEMUFileSocket *s = opaque;
40 ssize_t len;
41 ssize_t size = iov_size(iov, iovcnt);
43 len = iov_send(s->fd, iov, iovcnt, 0, size);
44 if (len < size) {
45 len = -socket_error();
47 return len;
50 static int socket_get_fd(void *opaque)
52 QEMUFileSocket *s = opaque;
54 return s->fd;
57 static ssize_t socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
58 size_t size)
60 QEMUFileSocket *s = opaque;
61 ssize_t len;
63 for (;;) {
64 len = qemu_recv(s->fd, buf, size, 0);
65 if (len != -1) {
66 break;
68 if (socket_error() == EAGAIN) {
69 yield_until_fd_readable(s->fd);
70 } else if (socket_error() != EINTR) {
71 break;
75 if (len == -1) {
76 len = -socket_error();
78 return len;
81 static int socket_close(void *opaque)
83 QEMUFileSocket *s = opaque;
84 closesocket(s->fd);
85 g_free(s);
86 return 0;
89 static int socket_shutdown(void *opaque, bool rd, bool wr)
91 QEMUFileSocket *s = opaque;
93 if (shutdown(s->fd, rd ? (wr ? SHUT_RDWR : SHUT_RD) : SHUT_WR)) {
94 return -errno;
95 } else {
96 return 0;
100 static int socket_return_close(void *opaque)
102 QEMUFileSocket *s = opaque;
104 * Note: We don't close the socket, that should be done by the forward
105 * path.
107 g_free(s);
108 return 0;
111 static const QEMUFileOps socket_return_read_ops = {
112 .get_fd = socket_get_fd,
113 .get_buffer = socket_get_buffer,
114 .close = socket_return_close,
115 .shut_down = socket_shutdown,
118 static const QEMUFileOps socket_return_write_ops = {
119 .get_fd = socket_get_fd,
120 .writev_buffer = socket_writev_buffer,
121 .close = socket_return_close,
122 .shut_down = socket_shutdown,
126 * Give a QEMUFile* off the same socket but data in the opposite
127 * direction.
129 static QEMUFile *socket_get_return_path(void *opaque)
131 QEMUFileSocket *forward = opaque;
132 QEMUFileSocket *reverse;
134 if (qemu_file_get_error(forward->file)) {
135 /* If the forward file is in error, don't try and open a return */
136 return NULL;
139 reverse = g_malloc0(sizeof(QEMUFileSocket));
140 reverse->fd = forward->fd;
141 /* I don't think there's a better way to tell which direction 'this' is */
142 if (forward->file->ops->get_buffer != NULL) {
143 /* being called from the read side, so we need to be able to write */
144 return qemu_fopen_ops(reverse, &socket_return_write_ops);
145 } else {
146 return qemu_fopen_ops(reverse, &socket_return_read_ops);
150 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
151 int64_t pos)
153 QEMUFileSocket *s = opaque;
154 ssize_t len, offset;
155 ssize_t size = iov_size(iov, iovcnt);
156 ssize_t total = 0;
158 assert(iovcnt > 0);
159 offset = 0;
160 while (size > 0) {
161 /* Find the next start position; skip all full-sized vector elements */
162 while (offset >= iov[0].iov_len) {
163 offset -= iov[0].iov_len;
164 iov++, iovcnt--;
167 /* skip `offset' bytes from the (now) first element, undo it on exit */
168 assert(iovcnt > 0);
169 iov[0].iov_base += offset;
170 iov[0].iov_len -= offset;
172 do {
173 len = writev(s->fd, iov, iovcnt);
174 } while (len == -1 && errno == EINTR);
175 if (len == -1) {
176 return -errno;
179 /* Undo the changes above */
180 iov[0].iov_base -= offset;
181 iov[0].iov_len += offset;
183 /* Prepare for the next iteration */
184 offset += len;
185 total += len;
186 size -= len;
189 return total;
192 static ssize_t unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
193 size_t size)
195 QEMUFileSocket *s = opaque;
196 ssize_t len;
198 for (;;) {
199 len = read(s->fd, buf, size);
200 if (len != -1) {
201 break;
203 if (errno == EAGAIN) {
204 yield_until_fd_readable(s->fd);
205 } else if (errno != EINTR) {
206 break;
210 if (len == -1) {
211 len = -errno;
213 return len;
216 static int unix_close(void *opaque)
218 QEMUFileSocket *s = opaque;
219 close(s->fd);
220 g_free(s);
221 return 0;
224 static const QEMUFileOps unix_read_ops = {
225 .get_fd = socket_get_fd,
226 .get_buffer = unix_get_buffer,
227 .close = unix_close
230 static const QEMUFileOps unix_write_ops = {
231 .get_fd = socket_get_fd,
232 .writev_buffer = unix_writev_buffer,
233 .close = unix_close
236 QEMUFile *qemu_fdopen(int fd, const char *mode)
238 QEMUFileSocket *s;
240 if (mode == NULL ||
241 (mode[0] != 'r' && mode[0] != 'w') ||
242 mode[1] != 'b' || mode[2] != 0) {
243 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
244 return NULL;
247 s = g_new0(QEMUFileSocket, 1);
248 s->fd = fd;
250 if (mode[0] == 'r') {
251 s->file = qemu_fopen_ops(s, &unix_read_ops);
252 } else {
253 s->file = qemu_fopen_ops(s, &unix_write_ops);
255 return s->file;
258 static const QEMUFileOps socket_read_ops = {
259 .get_fd = socket_get_fd,
260 .get_buffer = socket_get_buffer,
261 .close = socket_close,
262 .shut_down = socket_shutdown,
263 .get_return_path = socket_get_return_path
266 static const QEMUFileOps socket_write_ops = {
267 .get_fd = socket_get_fd,
268 .writev_buffer = socket_writev_buffer,
269 .close = socket_close,
270 .shut_down = socket_shutdown,
271 .get_return_path = socket_get_return_path
274 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
276 QEMUFileSocket *s;
278 if (qemu_file_mode_is_not_valid(mode)) {
279 return NULL;
282 s = g_new0(QEMUFileSocket, 1);
283 s->fd = fd;
284 if (mode[0] == 'w') {
285 qemu_set_block(s->fd);
286 s->file = qemu_fopen_ops(s, &socket_write_ops);
287 } else {
288 s->file = qemu_fopen_ops(s, &socket_read_ops);
290 return s->file;