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
25 #include "qemu/osdep.h"
26 #include "qemu-file-channel.h"
27 #include "qemu-file.h"
28 #include "io/channel-socket.h"
32 static ssize_t
channel_writev_buffer(void *opaque
,
38 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
40 struct iovec
*local_iov
= g_new(struct iovec
, iovcnt
);
41 struct iovec
*local_iov_head
= local_iov
;
42 unsigned int nlocal_iov
= iovcnt
;
44 nlocal_iov
= iov_copy(local_iov
, nlocal_iov
,
46 0, iov_size(iov
, iovcnt
));
48 while (nlocal_iov
> 0) {
50 len
= qio_channel_writev(ioc
, local_iov
, nlocal_iov
, errp
);
51 if (len
== QIO_CHANNEL_ERR_BLOCK
) {
52 if (qemu_in_coroutine()) {
53 qio_channel_yield(ioc
, G_IO_OUT
);
55 qio_channel_wait(ioc
, G_IO_OUT
);
64 iov_discard_front(&local_iov
, &nlocal_iov
, len
);
69 g_free(local_iov_head
);
74 static ssize_t
channel_get_buffer(void *opaque
,
80 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
84 ret
= qio_channel_read(ioc
, (char *)buf
, size
, errp
);
86 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
87 if (qemu_in_coroutine()) {
88 qio_channel_yield(ioc
, G_IO_IN
);
90 qio_channel_wait(ioc
, G_IO_IN
);
96 } while (ret
== QIO_CHANNEL_ERR_BLOCK
);
102 static int channel_close(void *opaque
, Error
**errp
)
105 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
106 ret
= qio_channel_close(ioc
, errp
);
107 object_unref(OBJECT(ioc
));
112 static int channel_shutdown(void *opaque
,
117 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
119 if (qio_channel_has_feature(ioc
,
120 QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
121 QIOChannelShutdown mode
;
123 mode
= QIO_CHANNEL_SHUTDOWN_BOTH
;
125 mode
= QIO_CHANNEL_SHUTDOWN_READ
;
127 mode
= QIO_CHANNEL_SHUTDOWN_WRITE
;
129 if (qio_channel_shutdown(ioc
, mode
, errp
) < 0) {
137 static int channel_set_blocking(void *opaque
,
141 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
143 if (qio_channel_set_blocking(ioc
, enabled
, errp
) < 0) {
149 static QEMUFile
*channel_get_input_return_path(void *opaque
)
151 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
153 return qemu_fopen_channel_output(ioc
);
156 static QEMUFile
*channel_get_output_return_path(void *opaque
)
158 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
160 return qemu_fopen_channel_input(ioc
);
163 static const QEMUFileOps channel_input_ops
= {
164 .get_buffer
= channel_get_buffer
,
165 .close
= channel_close
,
166 .shut_down
= channel_shutdown
,
167 .set_blocking
= channel_set_blocking
,
168 .get_return_path
= channel_get_input_return_path
,
172 static const QEMUFileOps channel_output_ops
= {
173 .writev_buffer
= channel_writev_buffer
,
174 .close
= channel_close
,
175 .shut_down
= channel_shutdown
,
176 .set_blocking
= channel_set_blocking
,
177 .get_return_path
= channel_get_output_return_path
,
181 QEMUFile
*qemu_fopen_channel_input(QIOChannel
*ioc
)
183 object_ref(OBJECT(ioc
));
184 return qemu_fopen_ops(ioc
, &channel_input_ops
);
187 QEMUFile
*qemu_fopen_channel_output(QIOChannel
*ioc
)
189 object_ref(OBJECT(ioc
));
190 return qemu_fopen_ops(ioc
, &channel_output_ops
);