hw/dma/xilinx_axidma: remove dead code
[qemu/ar7.git] / migration / qemu-file-unix.c
blob6ca53e7d67f7ed244d216e8544d698296ec4f711
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/error-report.h"
26 #include "qemu/iov.h"
27 #include "qemu/sockets.h"
28 #include "qemu/coroutine.h"
29 #include "migration/qemu-file.h"
30 #include "migration/qemu-file-internal.h"
32 typedef struct QEMUFileSocket {
33 int fd;
34 QEMUFile *file;
35 } QEMUFileSocket;
37 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
38 int64_t pos)
40 QEMUFileSocket *s = opaque;
41 ssize_t len;
42 ssize_t size = iov_size(iov, iovcnt);
43 ssize_t offset = 0;
44 int err;
46 while (size > 0) {
47 len = iov_send(s->fd, iov, iovcnt, offset, size);
49 if (len > 0) {
50 size -= len;
51 offset += len;
54 if (size > 0) {
55 err = socket_error();
57 if (err != EAGAIN && err != EWOULDBLOCK) {
58 error_report("socket_writev_buffer: Got err=%d for (%zu/%zu)",
59 err, (size_t)size, (size_t)len);
61 * If I've already sent some but only just got the error, I
62 * could return the amount validly sent so far and wait for the
63 * next call to report the error, but I'd rather flag the error
64 * immediately.
66 return -err;
69 /* Emulate blocking */
70 GPollFD pfd;
72 pfd.fd = s->fd;
73 pfd.events = G_IO_OUT | G_IO_ERR;
74 pfd.revents = 0;
75 TFR(err = g_poll(&pfd, 1, -1 /* no timeout */));
76 /* Errors other than EINTR intentionally ignored */
80 return offset;
83 static int socket_get_fd(void *opaque)
85 QEMUFileSocket *s = opaque;
87 return s->fd;
90 static ssize_t socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
91 size_t size)
93 QEMUFileSocket *s = opaque;
94 ssize_t len;
96 for (;;) {
97 len = qemu_recv(s->fd, buf, size, 0);
98 if (len != -1) {
99 break;
101 if (socket_error() == EAGAIN) {
102 yield_until_fd_readable(s->fd);
103 } else if (socket_error() != EINTR) {
104 break;
108 if (len == -1) {
109 len = -socket_error();
111 return len;
114 static int socket_close(void *opaque)
116 QEMUFileSocket *s = opaque;
117 closesocket(s->fd);
118 g_free(s);
119 return 0;
122 static int socket_shutdown(void *opaque, bool rd, bool wr)
124 QEMUFileSocket *s = opaque;
126 if (shutdown(s->fd, rd ? (wr ? SHUT_RDWR : SHUT_RD) : SHUT_WR)) {
127 return -errno;
128 } else {
129 return 0;
133 static int socket_return_close(void *opaque)
135 QEMUFileSocket *s = opaque;
137 * Note: We don't close the socket, that should be done by the forward
138 * path.
140 g_free(s);
141 return 0;
144 static const QEMUFileOps socket_return_read_ops = {
145 .get_fd = socket_get_fd,
146 .get_buffer = socket_get_buffer,
147 .close = socket_return_close,
148 .shut_down = socket_shutdown,
151 static const QEMUFileOps socket_return_write_ops = {
152 .get_fd = socket_get_fd,
153 .writev_buffer = socket_writev_buffer,
154 .close = socket_return_close,
155 .shut_down = socket_shutdown,
159 * Give a QEMUFile* off the same socket but data in the opposite
160 * direction.
162 static QEMUFile *socket_get_return_path(void *opaque)
164 QEMUFileSocket *forward = opaque;
165 QEMUFileSocket *reverse;
167 if (qemu_file_get_error(forward->file)) {
168 /* If the forward file is in error, don't try and open a return */
169 return NULL;
172 reverse = g_malloc0(sizeof(QEMUFileSocket));
173 reverse->fd = forward->fd;
174 /* I don't think there's a better way to tell which direction 'this' is */
175 if (forward->file->ops->get_buffer != NULL) {
176 /* being called from the read side, so we need to be able to write */
177 return qemu_fopen_ops(reverse, &socket_return_write_ops);
178 } else {
179 return qemu_fopen_ops(reverse, &socket_return_read_ops);
183 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
184 int64_t pos)
186 QEMUFileSocket *s = opaque;
187 ssize_t len, offset;
188 ssize_t size = iov_size(iov, iovcnt);
189 ssize_t total = 0;
191 assert(iovcnt > 0);
192 offset = 0;
193 while (size > 0) {
194 /* Find the next start position; skip all full-sized vector elements */
195 while (offset >= iov[0].iov_len) {
196 offset -= iov[0].iov_len;
197 iov++, iovcnt--;
200 /* skip `offset' bytes from the (now) first element, undo it on exit */
201 assert(iovcnt > 0);
202 iov[0].iov_base += offset;
203 iov[0].iov_len -= offset;
205 do {
206 len = writev(s->fd, iov, iovcnt);
207 } while (len == -1 && errno == EINTR);
208 if (len == -1) {
209 return -errno;
212 /* Undo the changes above */
213 iov[0].iov_base -= offset;
214 iov[0].iov_len += offset;
216 /* Prepare for the next iteration */
217 offset += len;
218 total += len;
219 size -= len;
222 return total;
225 static ssize_t unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
226 size_t size)
228 QEMUFileSocket *s = opaque;
229 ssize_t len;
231 for (;;) {
232 len = read(s->fd, buf, size);
233 if (len != -1) {
234 break;
236 if (errno == EAGAIN) {
237 yield_until_fd_readable(s->fd);
238 } else if (errno != EINTR) {
239 break;
243 if (len == -1) {
244 len = -errno;
246 return len;
249 static int unix_close(void *opaque)
251 QEMUFileSocket *s = opaque;
252 close(s->fd);
253 g_free(s);
254 return 0;
257 static const QEMUFileOps unix_read_ops = {
258 .get_fd = socket_get_fd,
259 .get_buffer = unix_get_buffer,
260 .close = unix_close
263 static const QEMUFileOps unix_write_ops = {
264 .get_fd = socket_get_fd,
265 .writev_buffer = unix_writev_buffer,
266 .close = unix_close
269 QEMUFile *qemu_fdopen(int fd, const char *mode)
271 QEMUFileSocket *s;
273 if (mode == NULL ||
274 (mode[0] != 'r' && mode[0] != 'w') ||
275 mode[1] != 'b' || mode[2] != 0) {
276 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
277 return NULL;
280 s = g_new0(QEMUFileSocket, 1);
281 s->fd = fd;
283 if (mode[0] == 'r') {
284 s->file = qemu_fopen_ops(s, &unix_read_ops);
285 } else {
286 s->file = qemu_fopen_ops(s, &unix_write_ops);
288 return s->file;
291 static const QEMUFileOps socket_read_ops = {
292 .get_fd = socket_get_fd,
293 .get_buffer = socket_get_buffer,
294 .close = socket_close,
295 .shut_down = socket_shutdown,
296 .get_return_path = socket_get_return_path
299 static const QEMUFileOps socket_write_ops = {
300 .get_fd = socket_get_fd,
301 .writev_buffer = socket_writev_buffer,
302 .close = socket_close,
303 .shut_down = socket_shutdown,
304 .get_return_path = socket_get_return_path
307 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
309 QEMUFileSocket *s;
311 if (qemu_file_mode_is_not_valid(mode)) {
312 return NULL;
315 s = g_new0(QEMUFileSocket, 1);
316 s->fd = fd;
317 if (mode[0] == 'w') {
318 qemu_set_block(s->fd);
319 s->file = qemu_fopen_ops(s, &socket_write_ops);
320 } else {
321 s->file = qemu_fopen_ops(s, &socket_read_ops);
323 return s->file;