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 "qemu/osdep.h"
22 #include "io/channel.h"
23 #include "qapi/error.h"
24 #include "qemu/coroutine.h"
26 bool qio_channel_has_feature(QIOChannel
*ioc
,
27 QIOChannelFeature feature
)
29 return ioc
->features
& (1 << feature
);
33 void qio_channel_set_feature(QIOChannel
*ioc
,
34 QIOChannelFeature feature
)
36 ioc
->features
|= (1 << feature
);
40 ssize_t
qio_channel_readv_full(QIOChannel
*ioc
,
41 const struct iovec
*iov
,
47 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
50 !qio_channel_has_feature(ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
51 error_setg_errno(errp
, EINVAL
,
52 "Channel does not support file descriptor passing");
56 return klass
->io_readv(ioc
, iov
, niov
, fds
, nfds
, errp
);
60 ssize_t
qio_channel_writev_full(QIOChannel
*ioc
,
61 const struct iovec
*iov
,
67 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
70 !qio_channel_has_feature(ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
71 error_setg_errno(errp
, EINVAL
,
72 "Channel does not support file descriptor passing");
76 return klass
->io_writev(ioc
, iov
, niov
, fds
, nfds
, errp
);
80 ssize_t
qio_channel_readv(QIOChannel
*ioc
,
81 const struct iovec
*iov
,
85 return qio_channel_readv_full(ioc
, iov
, niov
, NULL
, NULL
, errp
);
89 ssize_t
qio_channel_writev(QIOChannel
*ioc
,
90 const struct iovec
*iov
,
94 return qio_channel_writev_full(ioc
, iov
, niov
, NULL
, 0, errp
);
98 ssize_t
qio_channel_read(QIOChannel
*ioc
,
103 struct iovec iov
= { .iov_base
= buf
, .iov_len
= buflen
};
104 return qio_channel_readv_full(ioc
, &iov
, 1, NULL
, NULL
, errp
);
108 ssize_t
qio_channel_write(QIOChannel
*ioc
,
113 struct iovec iov
= { .iov_base
= (char *)buf
, .iov_len
= buflen
};
114 return qio_channel_writev_full(ioc
, &iov
, 1, NULL
, 0, errp
);
118 int qio_channel_set_blocking(QIOChannel
*ioc
,
122 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
123 return klass
->io_set_blocking(ioc
, enabled
, errp
);
127 int qio_channel_close(QIOChannel
*ioc
,
130 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
131 return klass
->io_close(ioc
, errp
);
135 GSource
*qio_channel_create_watch(QIOChannel
*ioc
,
136 GIOCondition condition
)
138 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
139 return klass
->io_create_watch(ioc
, condition
);
143 guint
qio_channel_add_watch(QIOChannel
*ioc
,
144 GIOCondition condition
,
147 GDestroyNotify notify
)
152 source
= qio_channel_create_watch(ioc
, condition
);
154 g_source_set_callback(source
, (GSourceFunc
)func
, user_data
, notify
);
156 id
= g_source_attach(source
, NULL
);
157 g_source_unref(source
);
163 int qio_channel_shutdown(QIOChannel
*ioc
,
164 QIOChannelShutdown how
,
167 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
169 if (!klass
->io_shutdown
) {
170 error_setg(errp
, "Data path shutdown not supported");
174 return klass
->io_shutdown(ioc
, how
, errp
);
178 void qio_channel_set_delay(QIOChannel
*ioc
,
181 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
183 if (klass
->io_set_delay
) {
184 klass
->io_set_delay(ioc
, enabled
);
189 void qio_channel_set_cork(QIOChannel
*ioc
,
192 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
194 if (klass
->io_set_cork
) {
195 klass
->io_set_cork(ioc
, enabled
);
200 off_t
qio_channel_io_seek(QIOChannel
*ioc
,
205 QIOChannelClass
*klass
= QIO_CHANNEL_GET_CLASS(ioc
);
207 if (!klass
->io_seek
) {
208 error_setg(errp
, "Channel does not support random access");
212 return klass
->io_seek(ioc
, offset
, whence
, errp
);
216 typedef struct QIOChannelYieldData QIOChannelYieldData
;
217 struct QIOChannelYieldData
{
223 static gboolean
qio_channel_yield_enter(QIOChannel
*ioc
,
224 GIOCondition condition
,
227 QIOChannelYieldData
*data
= opaque
;
228 qemu_coroutine_enter(data
->co
);
233 void coroutine_fn
qio_channel_yield(QIOChannel
*ioc
,
234 GIOCondition condition
)
236 QIOChannelYieldData data
;
238 assert(qemu_in_coroutine());
240 data
.co
= qemu_coroutine_self();
241 qio_channel_add_watch(ioc
,
243 qio_channel_yield_enter
,
246 qemu_coroutine_yield();
250 static gboolean
qio_channel_wait_complete(QIOChannel
*ioc
,
251 GIOCondition condition
,
254 GMainLoop
*loop
= opaque
;
256 g_main_loop_quit(loop
);
261 void qio_channel_wait(QIOChannel
*ioc
,
262 GIOCondition condition
)
264 GMainContext
*ctxt
= g_main_context_new();
265 GMainLoop
*loop
= g_main_loop_new(ctxt
, TRUE
);
268 source
= qio_channel_create_watch(ioc
, condition
);
270 g_source_set_callback(source
,
271 (GSourceFunc
)qio_channel_wait_complete
,
275 g_source_attach(source
, ctxt
);
277 g_main_loop_run(loop
);
279 g_source_unref(source
);
280 g_main_loop_unref(loop
);
281 g_main_context_unref(ctxt
);
286 static void qio_channel_finalize(Object
*obj
)
288 QIOChannel
*ioc
= QIO_CHANNEL(obj
);
291 CloseHandle(ioc
->event
);
296 static const TypeInfo qio_channel_info
= {
297 .parent
= TYPE_OBJECT
,
298 .name
= TYPE_QIO_CHANNEL
,
299 .instance_size
= sizeof(QIOChannel
),
301 .instance_finalize
= qio_channel_finalize
,
304 .class_size
= sizeof(QIOChannelClass
),
308 static void qio_channel_register_types(void)
310 type_register_static(&qio_channel_info
);
314 type_init(qio_channel_register_types
);