hw/arm/virt-acpi-build: build SLIT when needed
[qemu/ar7.git] / migration / qemu-file-channel.c
blobdc991c90510cc841238b2ca33009760371039181
1 /*
2 * QEMUFile backend for QIOChannel objects
4 * Copyright (c) 2015-2016 Red Hat, Inc
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.
25 #include "qemu/osdep.h"
26 #include "qemu-file-channel.h"
27 #include "migration/qemu-file.h"
28 #include "io/channel-socket.h"
29 #include "qemu/iov.h"
32 static ssize_t channel_writev_buffer(void *opaque,
33 struct iovec *iov,
34 int iovcnt,
35 int64_t pos)
37 QIOChannel *ioc = QIO_CHANNEL(opaque);
38 ssize_t done = 0;
39 struct iovec *local_iov = g_new(struct iovec, iovcnt);
40 struct iovec *local_iov_head = local_iov;
41 unsigned int nlocal_iov = iovcnt;
43 nlocal_iov = iov_copy(local_iov, nlocal_iov,
44 iov, iovcnt,
45 0, iov_size(iov, iovcnt));
47 while (nlocal_iov > 0) {
48 ssize_t len;
49 len = qio_channel_writev(ioc, local_iov, nlocal_iov, NULL);
50 if (len == QIO_CHANNEL_ERR_BLOCK) {
51 qio_channel_wait(ioc, G_IO_OUT);
52 continue;
54 if (len < 0) {
55 /* XXX handle Error objects */
56 done = -EIO;
57 goto cleanup;
60 iov_discard_front(&local_iov, &nlocal_iov, len);
61 done += len;
64 cleanup:
65 g_free(local_iov_head);
66 return done;
70 static ssize_t channel_get_buffer(void *opaque,
71 uint8_t *buf,
72 int64_t pos,
73 size_t size)
75 QIOChannel *ioc = QIO_CHANNEL(opaque);
76 ssize_t ret;
78 do {
79 ret = qio_channel_read(ioc, (char *)buf, size, NULL);
80 if (ret < 0) {
81 if (ret == QIO_CHANNEL_ERR_BLOCK) {
82 qio_channel_yield(ioc, G_IO_IN);
83 } else {
84 /* XXX handle Error * object */
85 return -EIO;
88 } while (ret == QIO_CHANNEL_ERR_BLOCK);
90 return ret;
94 static int channel_close(void *opaque)
96 QIOChannel *ioc = QIO_CHANNEL(opaque);
97 qio_channel_close(ioc, NULL);
98 object_unref(OBJECT(ioc));
99 return 0;
103 static int channel_shutdown(void *opaque,
104 bool rd,
105 bool wr)
107 QIOChannel *ioc = QIO_CHANNEL(opaque);
109 if (qio_channel_has_feature(ioc,
110 QIO_CHANNEL_FEATURE_SHUTDOWN)) {
111 QIOChannelShutdown mode;
112 if (rd && wr) {
113 mode = QIO_CHANNEL_SHUTDOWN_BOTH;
114 } else if (rd) {
115 mode = QIO_CHANNEL_SHUTDOWN_READ;
116 } else {
117 mode = QIO_CHANNEL_SHUTDOWN_WRITE;
119 if (qio_channel_shutdown(ioc, mode, NULL) < 0) {
120 /* XXX handler Error * object */
121 return -EIO;
124 return 0;
128 static int channel_set_blocking(void *opaque,
129 bool enabled)
131 QIOChannel *ioc = QIO_CHANNEL(opaque);
133 if (qio_channel_set_blocking(ioc, enabled, NULL) < 0) {
134 return -1;
136 return 0;
139 static QEMUFile *channel_get_input_return_path(void *opaque)
141 QIOChannel *ioc = QIO_CHANNEL(opaque);
143 return qemu_fopen_channel_output(ioc);
146 static QEMUFile *channel_get_output_return_path(void *opaque)
148 QIOChannel *ioc = QIO_CHANNEL(opaque);
150 return qemu_fopen_channel_input(ioc);
153 static const QEMUFileOps channel_input_ops = {
154 .get_buffer = channel_get_buffer,
155 .close = channel_close,
156 .shut_down = channel_shutdown,
157 .set_blocking = channel_set_blocking,
158 .get_return_path = channel_get_input_return_path,
162 static const QEMUFileOps channel_output_ops = {
163 .writev_buffer = channel_writev_buffer,
164 .close = channel_close,
165 .shut_down = channel_shutdown,
166 .set_blocking = channel_set_blocking,
167 .get_return_path = channel_get_output_return_path,
171 QEMUFile *qemu_fopen_channel_input(QIOChannel *ioc)
173 object_ref(OBJECT(ioc));
174 return qemu_fopen_ops(ioc, &channel_input_ops);
177 QEMUFile *qemu_fopen_channel_output(QIOChannel *ioc)
179 object_ref(OBJECT(ioc));
180 return qemu_fopen_ops(ioc, &channel_output_ops);