4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "io/channel.h"
22 #include "qemu/coroutine.h"
24 bool qio_channel_has_feature(QIOChannel
*ioc
,
25 QIOChannelFeature feature
)
27 return ioc
->features
& (1 << feature
);
31 ssize_t
qio_channel_readv_full(QIOChannel
*ioc
,
32 const struct iovec
*iov
,
38 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
41 !(ioc
->features
& (1 << QIO_CHANNEL_FEATURE_FD_PASS
))) {
42 error_setg_errno(errp
, EINVAL
,
43 "Channel does not support file descriptor passing");
47 return klass
->io_readv(ioc
, iov
, niov
, fds
, nfds
, errp
);
51 ssize_t
qio_channel_writev_full(QIOChannel
*ioc
,
52 const struct iovec
*iov
,
58 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
61 !(ioc
->features
& (1 << QIO_CHANNEL_FEATURE_FD_PASS
))) {
62 error_setg_errno(errp
, EINVAL
,
63 "Channel does not support file descriptor passing");
67 return klass
->io_writev(ioc
, iov
, niov
, fds
, nfds
, errp
);
71 ssize_t
qio_channel_readv(QIOChannel
*ioc
,
72 const struct iovec
*iov
,
76 return qio_channel_readv_full(ioc
, iov
, niov
, NULL
, NULL
, errp
);
80 ssize_t
qio_channel_writev(QIOChannel
*ioc
,
81 const struct iovec
*iov
,
85 return qio_channel_writev_full(ioc
, iov
, niov
, NULL
, 0, errp
);
89 ssize_t
qio_channel_read(QIOChannel
*ioc
,
94 struct iovec iov
= { .iov_base
= buf
, .iov_len
= buflen
};
95 return qio_channel_readv_full(ioc
, &iov
, 1, NULL
, NULL
, errp
);
99 ssize_t
qio_channel_write(QIOChannel
*ioc
,
104 struct iovec iov
= { .iov_base
= (char *)buf
, .iov_len
= buflen
};
105 return qio_channel_writev_full(ioc
, &iov
, 1, NULL
, 0, errp
);
109 int qio_channel_set_blocking(QIOChannel
*ioc
,
113 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
114 return klass
->io_set_blocking(ioc
, enabled
, errp
);
118 int qio_channel_close(QIOChannel
*ioc
,
121 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
122 return klass
->io_close(ioc
, errp
);
126 GSource
*qio_channel_create_watch(QIOChannel
*ioc
,
127 GIOCondition condition
)
129 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
130 return klass
->io_create_watch(ioc
, condition
);
134 guint
qio_channel_add_watch(QIOChannel
*ioc
,
135 GIOCondition condition
,
138 GDestroyNotify notify
)
143 source
= qio_channel_create_watch(ioc
, condition
);
145 g_source_set_callback(source
, (GSourceFunc
)func
, user_data
, notify
);
147 id
= g_source_attach(source
, NULL
);
148 g_source_unref(source
);
154 int qio_channel_shutdown(QIOChannel
*ioc
,
155 QIOChannelShutdown how
,
158 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
160 if (!klass
->io_shutdown
) {
161 error_setg(errp
, "Data path shutdown not supported");
165 return klass
->io_shutdown(ioc
, how
, errp
);
169 void qio_channel_set_delay(QIOChannel
*ioc
,
172 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
174 if (klass
->io_set_delay
) {
175 klass
->io_set_delay(ioc
, enabled
);
180 void qio_channel_set_cork(QIOChannel
*ioc
,
183 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
185 if (klass
->io_set_cork
) {
186 klass
->io_set_cork(ioc
, enabled
);
191 off_t
qio_channel_io_seek(QIOChannel
*ioc
,
196 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
198 if (!klass
->io_seek
) {
199 error_setg(errp
, "Channel does not support random access");
203 return klass
->io_seek(ioc
, offset
, whence
, errp
);
207 typedef struct QIOChannelYieldData QIOChannelYieldData
;
208 struct QIOChannelYieldData
{
214 static gboolean
qio_channel_yield_enter(QIOChannel
*ioc
,
215 GIOCondition condition
,
218 QIOChannelYieldData
*data
= opaque
;
219 qemu_coroutine_enter(data
->co
, NULL
);
224 void coroutine_fn
qio_channel_yield(QIOChannel
*ioc
,
225 GIOCondition condition
)
227 QIOChannelYieldData data
;
229 assert(qemu_in_coroutine());
231 data
.co
= qemu_coroutine_self();
232 qio_channel_add_watch(ioc
,
234 qio_channel_yield_enter
,
237 qemu_coroutine_yield();
241 static gboolean
qio_channel_wait_complete(QIOChannel
*ioc
,
242 GIOCondition condition
,
245 GMainLoop
*loop
= opaque
;
247 g_main_loop_quit(loop
);
252 void qio_channel_wait(QIOChannel
*ioc
,
253 GIOCondition condition
)
255 GMainContext
*ctxt
= g_main_context_new();
256 GMainLoop
*loop
= g_main_loop_new(ctxt
, TRUE
);
259 source
= qio_channel_create_watch(ioc
, condition
);
261 g_source_set_callback(source
,
262 (GSourceFunc
)qio_channel_wait_complete
,
266 g_source_attach(source
, ctxt
);
268 g_main_loop_run(loop
);
270 g_source_unref(source
);
271 g_main_loop_unref(loop
);
272 g_main_context_unref(ctxt
);
276 static const TypeInfo qio_channel_info
= {
277 .parent
= TYPE_OBJECT
,
278 .name
= TYPE_QIO_CHANNEL
,
279 .instance_size
= sizeof(QIOChannel
),
281 .class_size
= sizeof(QIOChannelClass
),
285 static void qio_channel_register_types(void)
287 type_register_static(&qio_channel_info
);
291 type_init(qio_channel_register_types
);