osdep: protect qemu/osdep.h with extern "C"
[qemu/ar7.git] / migration / qemu-file-channel.c
blob876d05a540ff2ed6626e53788529470d4eeca96c
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 "qemu-file.h"
28 #include "io/channel-socket.h"
29 #include "qemu/iov.h"
30 #include "qemu/yank.h"
31 #include "yank_functions.h"
34 static ssize_t channel_writev_buffer(void *opaque,
35 struct iovec *iov,
36 int iovcnt,
37 int64_t pos,
38 Error **errp)
40 QIOChannel *ioc = QIO_CHANNEL(opaque);
41 ssize_t done = 0;
42 struct iovec *local_iov = g_new(struct iovec, iovcnt);
43 struct iovec *local_iov_head = local_iov;
44 unsigned int nlocal_iov = iovcnt;
46 nlocal_iov = iov_copy(local_iov, nlocal_iov,
47 iov, iovcnt,
48 0, iov_size(iov, iovcnt));
50 while (nlocal_iov > 0) {
51 ssize_t len;
52 len = qio_channel_writev(ioc, local_iov, nlocal_iov, errp);
53 if (len == QIO_CHANNEL_ERR_BLOCK) {
54 if (qemu_in_coroutine()) {
55 qio_channel_yield(ioc, G_IO_OUT);
56 } else {
57 qio_channel_wait(ioc, G_IO_OUT);
59 continue;
61 if (len < 0) {
62 done = -EIO;
63 goto cleanup;
66 iov_discard_front(&local_iov, &nlocal_iov, len);
67 done += len;
70 cleanup:
71 g_free(local_iov_head);
72 return done;
76 static ssize_t channel_get_buffer(void *opaque,
77 uint8_t *buf,
78 int64_t pos,
79 size_t size,
80 Error **errp)
82 QIOChannel *ioc = QIO_CHANNEL(opaque);
83 ssize_t ret;
85 do {
86 ret = qio_channel_read(ioc, (char *)buf, size, errp);
87 if (ret < 0) {
88 if (ret == QIO_CHANNEL_ERR_BLOCK) {
89 if (qemu_in_coroutine()) {
90 qio_channel_yield(ioc, G_IO_IN);
91 } else {
92 qio_channel_wait(ioc, G_IO_IN);
94 } else {
95 return -EIO;
98 } while (ret == QIO_CHANNEL_ERR_BLOCK);
100 return ret;
104 static int channel_close(void *opaque, Error **errp)
106 int ret;
107 QIOChannel *ioc = QIO_CHANNEL(opaque);
108 ret = qio_channel_close(ioc, errp);
109 if (object_dynamic_cast(OBJECT(ioc), TYPE_QIO_CHANNEL_SOCKET)
110 && OBJECT(ioc)->ref == 1) {
111 yank_unregister_function(MIGRATION_YANK_INSTANCE,
112 migration_yank_iochannel,
113 QIO_CHANNEL(ioc));
115 object_unref(OBJECT(ioc));
116 return ret;
120 static int channel_shutdown(void *opaque,
121 bool rd,
122 bool wr,
123 Error **errp)
125 QIOChannel *ioc = QIO_CHANNEL(opaque);
127 if (qio_channel_has_feature(ioc,
128 QIO_CHANNEL_FEATURE_SHUTDOWN)) {
129 QIOChannelShutdown mode;
130 if (rd && wr) {
131 mode = QIO_CHANNEL_SHUTDOWN_BOTH;
132 } else if (rd) {
133 mode = QIO_CHANNEL_SHUTDOWN_READ;
134 } else {
135 mode = QIO_CHANNEL_SHUTDOWN_WRITE;
137 if (qio_channel_shutdown(ioc, mode, errp) < 0) {
138 return -EIO;
141 return 0;
145 static int channel_set_blocking(void *opaque,
146 bool enabled,
147 Error **errp)
149 QIOChannel *ioc = QIO_CHANNEL(opaque);
151 if (qio_channel_set_blocking(ioc, enabled, errp) < 0) {
152 return -1;
154 return 0;
157 static QEMUFile *channel_get_input_return_path(void *opaque)
159 QIOChannel *ioc = QIO_CHANNEL(opaque);
161 return qemu_fopen_channel_output(ioc);
164 static QEMUFile *channel_get_output_return_path(void *opaque)
166 QIOChannel *ioc = QIO_CHANNEL(opaque);
168 return qemu_fopen_channel_input(ioc);
171 static const QEMUFileOps channel_input_ops = {
172 .get_buffer = channel_get_buffer,
173 .close = channel_close,
174 .shut_down = channel_shutdown,
175 .set_blocking = channel_set_blocking,
176 .get_return_path = channel_get_input_return_path,
180 static const QEMUFileOps channel_output_ops = {
181 .writev_buffer = channel_writev_buffer,
182 .close = channel_close,
183 .shut_down = channel_shutdown,
184 .set_blocking = channel_set_blocking,
185 .get_return_path = channel_get_output_return_path,
189 QEMUFile *qemu_fopen_channel_input(QIOChannel *ioc)
191 object_ref(OBJECT(ioc));
192 return qemu_fopen_ops(ioc, &channel_input_ops);
195 QEMUFile *qemu_fopen_channel_output(QIOChannel *ioc)
197 object_ref(OBJECT(ioc));
198 return qemu_fopen_ops(ioc, &channel_output_ops);