master
h 1,175 lines 41.6 KB
Raw
1 /*
2 * QEMU I/O channels
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
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.1 of the License, or (at your option) any later version.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #ifndef QIO_CHANNEL_H
22 #define QIO_CHANNEL_H
23
24 #include "qom/object.h"
25 #include "qemu/coroutine-core.h"
26 #include "qemu/aio.h"
27
28 #define TYPE_QIO_CHANNEL "qio-channel"
29 OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass,
30 QIO_CHANNEL)
31
32
33 #define QIO_CHANNEL_ERR_BLOCK -2
34
35 #define QIO_CHANNEL_WRITE_FLAG_ZERO_COPY 0x1
36
37 #define QIO_CHANNEL_READ_FLAG_MSG_PEEK 0x1
38 #define QIO_CHANNEL_READ_FLAG_RELAXED_EOF 0x2
39 #define QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING 0x4
40
41 typedef enum QIOChannelFeature QIOChannelFeature;
42
43 enum QIOChannelFeature {
44 QIO_CHANNEL_FEATURE_FD_PASS,
45 QIO_CHANNEL_FEATURE_SHUTDOWN,
46 QIO_CHANNEL_FEATURE_LISTEN,
47 QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY,
48 QIO_CHANNEL_FEATURE_READ_MSG_PEEK,
49 QIO_CHANNEL_FEATURE_SEEKABLE,
50 QIO_CHANNEL_FEATURE_CONCURRENT_IO,
51 };
52
53
54 typedef enum QIOChannelShutdown QIOChannelShutdown;
55
56 enum QIOChannelShutdown {
57 QIO_CHANNEL_SHUTDOWN_READ = 1,
58 QIO_CHANNEL_SHUTDOWN_WRITE = 2,
59 QIO_CHANNEL_SHUTDOWN_BOTH = 3,
60 };
61
62 typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc,
63 GIOCondition condition,
64 gpointer data);
65
66 /**
67 * QIOChannel:
68 *
69 * The QIOChannel defines the core API for a generic I/O channel
70 * class hierarchy. It is inspired by GIOChannel, but has the
71 * following differences
72 *
73 * - Use QOM to properly support arbitrary subclassing
74 * - Support use of iovecs for efficient I/O with multiple blocks
75 * - None of the character set translation, binary data exclusively
76 * - Direct support for QEMU Error object reporting
77 * - File descriptor passing
78 *
79 * This base class is abstract so cannot be instantiated. There
80 * will be subclasses for dealing with sockets, files, and higher
81 * level protocols such as TLS, WebSocket, etc.
82 */
83
84 struct QIOChannel {
85 Object parent;
86 unsigned int features; /* bitmask of QIOChannelFeatures */
87 char *name;
88 AioContext *read_ctx;
89 Coroutine *read_coroutine;
90 AioContext *write_ctx;
91 Coroutine *write_coroutine;
92 bool follow_coroutine_ctx;
93 #ifdef _WIN32
94 HANDLE event; /* For use with GSource on Win32 */
95 #endif
96 };
97
98 /**
99 * QIOChannelClass:
100 *
101 * This class defines the contract that all subclasses
102 * must follow to provide specific channel implementations.
103 * The first five callbacks are mandatory to support, others
104 * provide additional optional features.
105 *
106 * Consult the corresponding public API docs for a description
107 * of the semantics of each callback. io_shutdown in particular
108 * must be thread-safe, terminate quickly and must not block.
109 */
110 struct QIOChannelClass {
111 ObjectClass parent;
112
113 /* Mandatory callbacks */
114 ssize_t (*io_writev)(QIOChannel *ioc,
115 const struct iovec *iov,
116 size_t niov,
117 int *fds,
118 size_t nfds,
119 int flags,
120 Error **errp);
121
122 /*
123 * The io_readv handler must guarantee that all
124 * incoming fds are set BLOCKING (unless
125 * QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING flag is set) and
126 * CLOEXEC (if available).
127 * @fds and @nfds are set only on success path. Still, setting
128 * @fds and @nfds to zero is acceptable on failure path.
129 */
130 ssize_t (*io_readv)(QIOChannel *ioc,
131 const struct iovec *iov,
132 size_t niov,
133 int **fds,
134 size_t *nfds,
135 int flags,
136 Error **errp);
137
138 int (*io_close)(QIOChannel *ioc,
139 Error **errp);
140 GSource * (*io_create_watch)(QIOChannel *ioc,
141 GIOCondition condition);
142 int (*io_set_blocking)(QIOChannel *ioc,
143 bool enabled,
144 Error **errp);
145
146 /* Optional callbacks */
147 ssize_t (*io_pwritev)(QIOChannel *ioc,
148 const struct iovec *iov,
149 size_t niov,
150 off_t offset,
151 Error **errp);
152 ssize_t (*io_preadv)(QIOChannel *ioc,
153 const struct iovec *iov,
154 size_t niov,
155 off_t offset,
156 Error **errp);
157 int (*io_shutdown)(QIOChannel *ioc,
158 QIOChannelShutdown how,
159 Error **errp);
160 void (*io_set_cork)(QIOChannel *ioc,
161 bool enabled);
162 void (*io_set_delay)(QIOChannel *ioc,
163 bool enabled);
164 off_t (*io_seek)(QIOChannel *ioc,
165 off_t offset,
166 int whence,
167 Error **errp);
168 void (*io_set_aio_fd_handler)(QIOChannel *ioc,
169 AioContext *read_ctx,
170 IOHandler *io_read,
171 AioContext *write_ctx,
172 IOHandler *io_write,
173 void *opaque);
174 int (*io_flush)(QIOChannel *ioc,
175 Error **errp);
176 int (*io_peerpid)(QIOChannel *ioc,
177 unsigned int *pid,
178 Error **errp);
179 };
180
181 /* General I/O handling functions */
182
183 /**
184 * qio_channel_has_feature:
185 * @ioc: the channel object
186 * @feature: the feature to check support of
187 *
188 * Determine whether the channel implementation supports
189 * the optional feature named in @feature.
190 *
191 * Returns: true if supported, false otherwise.
192 */
193 bool qio_channel_has_feature(QIOChannel *ioc,
194 QIOChannelFeature feature);
195
196 /**
197 * qio_channel_set_feature:
198 * @ioc: the channel object
199 * @feature: the feature to set support for
200 *
201 * Add channel support for the feature named in @feature.
202 */
203 void qio_channel_set_feature(QIOChannel *ioc,
204 QIOChannelFeature feature);
205
206 /**
207 * qio_channel_set_name:
208 * @ioc: the channel object
209 * @name: the name of the channel
210 *
211 * Sets the name of the channel, which serves as an aid
212 * to debugging. The name is used when creating GSource
213 * watches for this channel.
214 */
215 void qio_channel_set_name(QIOChannel *ioc,
216 const char *name);
217
218 /**
219 * qio_channel_readv_full:
220 * @ioc: the channel object
221 * @iov: the array of memory regions to read data into
222 * @niov: the length of the @iov array
223 * @fds: pointer to an array that will received file handles
224 * @nfds: pointer filled with number of elements in @fds on return
225 * @flags: read flags (QIO_CHANNEL_READ_FLAG_*)
226 * @errp: pointer to a NULL-initialized error object
227 *
228 * Read data from the IO channel, storing it in the
229 * memory regions referenced by @iov. Each element
230 * in the @iov will be fully populated with data
231 * before the next one is used. The @niov parameter
232 * specifies the total number of elements in @iov.
233 *
234 * It is not required for all @iov to be filled with
235 * data. If the channel is in blocking mode, at least
236 * one byte of data will be read, but no more is
237 * guaranteed. If the channel is non-blocking and no
238 * data is available, it will return QIO_CHANNEL_ERR_BLOCK
239 *
240 * If the channel has passed any file descriptors,
241 * the @fds array pointer will be allocated and
242 * the elements filled with the received file
243 * descriptors. The @nfds pointer will be updated
244 * to indicate the size of the @fds array that
245 * was allocated. It is the callers responsibility
246 * to call close() on each file descriptor and to
247 * call g_free() on the array pointer in @fds.
248 * @fds allocated and set (and @nfds is set too)
249 * _only_ on success path. Still, @fds and @nfds
250 * may be set to zero on failure path.
251 * qio_channel_readv_full() guarantees that all
252 * incoming fds are set BLOCKING (unless
253 * QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING flag
254 * is set) and CLOEXEC (if available).
255 *
256 * It is an error to pass a non-NULL @fds parameter
257 * unless qio_channel_has_feature() returns a true
258 * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
259 *
260 * Returns: the number of bytes read, or -1 on error,
261 * or QIO_CHANNEL_ERR_BLOCK if no data is available
262 * and the channel is non-blocking
263 */
264 ssize_t qio_channel_readv_full(QIOChannel *ioc,
265 const struct iovec *iov,
266 size_t niov,
267 int **fds,
268 size_t *nfds,
269 int flags,
270 Error **errp);
271
272
273 /**
274 * qio_channel_writev_full:
275 * @ioc: the channel object
276 * @iov: the array of memory regions to write data from
277 * @niov: the length of the @iov array
278 * @fds: an array of file handles to send
279 * @nfds: number of file handles in @fds
280 * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
281 * @errp: pointer to a NULL-initialized error object
282 *
283 * Write data to the IO channel, reading it from the
284 * memory regions referenced by @iov. Each element
285 * in the @iov will be fully sent, before the next
286 * one is used. The @niov parameter specifies the
287 * total number of elements in @iov.
288 *
289 * It is not required for all @iov data to be fully
290 * sent. If the channel is in blocking mode, at least
291 * one byte of data will be sent, but no more is
292 * guaranteed. If the channel is non-blocking and no
293 * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
294 *
295 * If there are file descriptors to send, the @fds
296 * array should be non-NULL and provide the handles.
297 * All file descriptors will be sent if at least one
298 * byte of data was sent.
299 *
300 * It is an error to pass a non-NULL @fds parameter
301 * unless qio_channel_has_feature() returns a true
302 * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
303 *
304 * Returns: the number of bytes sent, or -1 on error,
305 * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
306 * and the channel is non-blocking
307 */
308 ssize_t qio_channel_writev_full(QIOChannel *ioc,
309 const struct iovec *iov,
310 size_t niov,
311 int *fds,
312 size_t nfds,
313 int flags,
314 Error **errp);
315
316 /**
317 * qio_channel_readv_all_eof:
318 * @ioc: the channel object
319 * @iov: the array of memory regions to read data into
320 * @niov: the length of the @iov array
321 * @errp: pointer to a NULL-initialized error object
322 *
323 * Read data from the IO channel, storing it in the
324 * memory regions referenced by @iov. Each element
325 * in the @iov will be fully populated with data
326 * before the next one is used. The @niov parameter
327 * specifies the total number of elements in @iov.
328 *
329 * The function will wait for all requested data
330 * to be read, yielding from the current coroutine
331 * if required.
332 *
333 * If end-of-file occurs before any data is read,
334 * no error is reported; otherwise, if it occurs
335 * before all requested data has been read, an error
336 * will be reported.
337 *
338 * Returns: 1 if all bytes were read, 0 if end-of-file
339 * occurs without data, or -1 on error
340 */
341 int coroutine_mixed_fn qio_channel_readv_all_eof(QIOChannel *ioc,
342 const struct iovec *iov,
343 size_t niov,
344 Error **errp);
345
346 /**
347 * qio_channel_readv_all:
348 * @ioc: the channel object
349 * @iov: the array of memory regions to read data into
350 * @niov: the length of the @iov array
351 * @errp: pointer to a NULL-initialized error object
352 *
353 * Read data from the IO channel, storing it in the
354 * memory regions referenced by @iov. Each element
355 * in the @iov will be fully populated with data
356 * before the next one is used. The @niov parameter
357 * specifies the total number of elements in @iov.
358 *
359 * The function will wait for all requested data
360 * to be read, yielding from the current coroutine
361 * if required.
362 *
363 * If end-of-file occurs before all requested data
364 * has been read, an error will be reported.
365 *
366 * Returns: 0 if all bytes were read, or -1 on error
367 */
368 int coroutine_mixed_fn qio_channel_readv_all(QIOChannel *ioc,
369 const struct iovec *iov,
370 size_t niov,
371 Error **errp);
372
373
374 /**
375 * qio_channel_writev_all:
376 * @ioc: the channel object
377 * @iov: the array of memory regions to write data from
378 * @niov: the length of the @iov array
379 * @errp: pointer to a NULL-initialized error object
380 *
381 * Write data to the IO channel, reading it from the
382 * memory regions referenced by @iov. Each element
383 * in the @iov will be fully sent, before the next
384 * one is used. The @niov parameter specifies the
385 * total number of elements in @iov.
386 *
387 * The function will wait for all requested data
388 * to be written, yielding from the current coroutine
389 * if required.
390 *
391 * Returns: 0 if all bytes were written, or -1 on error
392 */
393 int coroutine_mixed_fn qio_channel_writev_all(QIOChannel *ioc,
394 const struct iovec *iov,
395 size_t niov,
396 Error **errp);
397
398 /**
399 * qio_channel_readv:
400 * @ioc: the channel object
401 * @iov: the array of memory regions to read data into
402 * @niov: the length of the @iov array
403 * @errp: pointer to a NULL-initialized error object
404 *
405 * Behaves as qio_channel_readv_full() but does not support
406 * receiving of file handles.
407 */
408 ssize_t qio_channel_readv(QIOChannel *ioc,
409 const struct iovec *iov,
410 size_t niov,
411 Error **errp);
412
413 /**
414 * qio_channel_writev:
415 * @ioc: the channel object
416 * @iov: the array of memory regions to write data from
417 * @niov: the length of the @iov array
418 * @errp: pointer to a NULL-initialized error object
419 *
420 * Behaves as qio_channel_writev_full() but does not support
421 * sending of file handles.
422 */
423 ssize_t qio_channel_writev(QIOChannel *ioc,
424 const struct iovec *iov,
425 size_t niov,
426 Error **errp);
427
428 /**
429 * qio_channel_read:
430 * @ioc: the channel object
431 * @buf: the memory region to read data into
432 * @buflen: the length of @buf
433 * @errp: pointer to a NULL-initialized error object
434 *
435 * Behaves as qio_channel_readv_full() but does not support
436 * receiving of file handles, and only supports reading into
437 * a single memory region.
438 */
439 ssize_t qio_channel_read(QIOChannel *ioc,
440 void *buf,
441 size_t buflen,
442 Error **errp);
443
444 /**
445 * qio_channel_write:
446 * @ioc: the channel object
447 * @buf: the memory regions to send data from
448 * @buflen: the length of @buf
449 * @errp: pointer to a NULL-initialized error object
450 *
451 * Behaves as qio_channel_writev_full() but does not support
452 * sending of file handles, and only supports writing from a
453 * single memory region.
454 */
455 ssize_t qio_channel_write(QIOChannel *ioc,
456 const void *buf,
457 size_t buflen,
458 Error **errp);
459
460 /**
461 * qio_channel_read_all_eof:
462 * @ioc: the channel object
463 * @buf: the memory region to read data into
464 * @buflen: the number of bytes to @buf
465 * @errp: pointer to a NULL-initialized error object
466 *
467 * Reads @buflen bytes into @buf, possibly blocking or (if the
468 * channel is non-blocking) yielding from the current coroutine
469 * multiple times until the entire content is read. If end-of-file
470 * occurs immediately it is not an error, but if it occurs after
471 * data has been read it will return an error rather than a
472 * short-read. Otherwise behaves as qio_channel_read().
473 *
474 * Returns: 1 if all bytes were read, 0 if end-of-file occurs
475 * without data, or -1 on error
476 */
477 int coroutine_mixed_fn qio_channel_read_all_eof(QIOChannel *ioc,
478 void *buf,
479 size_t buflen,
480 Error **errp);
481
482 /**
483 * qio_channel_read_all:
484 * @ioc: the channel object
485 * @buf: the memory region to read data into
486 * @buflen: the number of bytes to @buf
487 * @errp: pointer to a NULL-initialized error object
488 *
489 * Reads @buflen bytes into @buf, possibly blocking or (if the
490 * channel is non-blocking) yielding from the current coroutine
491 * multiple times until the entire content is read. If end-of-file
492 * occurs it will return an error rather than a short-read. Otherwise
493 * behaves as qio_channel_read().
494 *
495 * Returns: 0 if all bytes were read, or -1 on error
496 */
497 int coroutine_mixed_fn qio_channel_read_all(QIOChannel *ioc,
498 void *buf,
499 size_t buflen,
500 Error **errp);
501
502 /**
503 * qio_channel_write_all:
504 * @ioc: the channel object
505 * @buf: the memory region to write data into
506 * @buflen: the number of bytes to @buf
507 * @errp: pointer to a NULL-initialized error object
508 *
509 * Writes @buflen bytes from @buf, possibly blocking or (if the
510 * channel is non-blocking) yielding from the current coroutine
511 * multiple times until the entire content is written. Otherwise
512 * behaves as qio_channel_write().
513 *
514 * Returns: 0 if all bytes were written, or -1 on error
515 */
516 int coroutine_mixed_fn qio_channel_write_all(QIOChannel *ioc,
517 const void *buf,
518 size_t buflen,
519 Error **errp);
520
521 /**
522 * qio_channel_set_blocking:
523 * @ioc: the channel object
524 * @enabled: the blocking flag state
525 * @errp: pointer to a NULL-initialized error object
526 *
527 * If @enabled is true, then the channel is put into
528 * blocking mode, otherwise it will be non-blocking.
529 *
530 * In non-blocking mode, read/write operations may
531 * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
532 * block on I/O
533 */
534 bool qio_channel_set_blocking(QIOChannel *ioc,
535 bool enabled,
536 Error **errp);
537
538 /**
539 * qio_channel_set_follow_coroutine_ctx:
540 * @ioc: the channel object
541 * @enabled: whether or not to follow the coroutine's AioContext
542 *
543 * If @enabled is true, calls to qio_channel_yield() use the current
544 * coroutine's AioContext. Usually this is desirable.
545 *
546 * If @enabled is false, calls to qio_channel_yield() use the global iohandler
547 * AioContext. This is may be used by coroutines that run in the main loop and
548 * do not wish to respond to I/O during nested event loops. This is the
549 * default for compatibility with code that is not aware of AioContexts.
550 */
551 void qio_channel_set_follow_coroutine_ctx(QIOChannel *ioc, bool enabled);
552
553 /**
554 * qio_channel_close:
555 * @ioc: the channel object
556 * @errp: pointer to a NULL-initialized error object
557 *
558 * Close the channel, flushing any pending I/O
559 *
560 * Returns: 0 on success, -1 on error
561 */
562 int qio_channel_close(QIOChannel *ioc,
563 Error **errp);
564
565 /**
566 * qio_channel_pwritev
567 * @ioc: the channel object
568 * @iov: the array of memory regions to write data from
569 * @niov: the length of the @iov array
570 * @offset: offset in the channel where writes should begin
571 * @errp: pointer to a NULL-initialized error object
572 *
573 * Not all implementations will support this facility, so may report
574 * an error. To avoid errors, the caller may check for the feature
575 * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method.
576 *
577 * Behaves as qio_channel_writev_full, apart from not supporting
578 * sending of file handles as well as beginning the write at the
579 * passed @offset
580 *
581 */
582 ssize_t qio_channel_pwritev(QIOChannel *ioc, const struct iovec *iov,
583 size_t niov, off_t offset, Error **errp);
584
585 /**
586 * qio_channel_pwrite
587 * @ioc: the channel object
588 * @buf: the memory region to write data into
589 * @buflen: the number of bytes to @buf
590 * @offset: offset in the channel where writes should begin
591 * @errp: pointer to a NULL-initialized error object
592 *
593 * Not all implementations will support this facility, so may report
594 * an error. To avoid errors, the caller may check for the feature
595 * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method.
596 *
597 */
598 ssize_t qio_channel_pwrite(QIOChannel *ioc, void *buf, size_t buflen,
599 off_t offset, Error **errp);
600
601 /**
602 * qio_channel_pwritev_all:
603 * @ioc: the channel object
604 * @iov: the array of memory regions to write data from
605 * @niov: the length of the @iov array
606 * @offset: the starting offset in the channel to write to
607 * @errp: pointer to a NULL-initialized error object
608 *
609 * Writes @iov, possibly blocking or (if the channel is non-blocking)
610 * yielding from the current coroutine multiple times until the entire
611 * content is written. Otherwise behaves as qio_channel_pwritev().
612 *
613 * Returns: 0 if all bytes were written, or -1 on error
614 */
615 int coroutine_mixed_fn qio_channel_pwritev_all(QIOChannel *ioc,
616 const struct iovec *iov,
617 size_t niov,
618 off_t offset,
619 Error **errp);
620
621 /**
622 * qio_channel_pwrite_all:
623 * @ioc: the channel object
624 * @buf: the memory region to write data from
625 * @buflen: the number of bytes to write from @buf
626 * @offset: the starting offset in the channel to write to
627 * @errp: pointer to a NULL-initialized error object
628 *
629 * Writes @buflen bytes from @buf, possibly blocking or (if the
630 * channel is non-blocking) yielding from the current coroutine
631 * multiple times until the entire content is written. Otherwise
632 * behaves as qio_channel_pwrite().
633 *
634 * Returns: 0 if all bytes were written, or -1 on error
635 */
636 int coroutine_mixed_fn qio_channel_pwrite_all(QIOChannel *ioc,
637 const void *buf,
638 size_t buflen,
639 off_t offset,
640 Error **errp);
641
642 /**
643 * qio_channel_preadv
644 * @ioc: the channel object
645 * @iov: the array of memory regions to read data into
646 * @niov: the length of the @iov array
647 * @offset: offset in the channel where writes should begin
648 * @errp: pointer to a NULL-initialized error object
649 *
650 * Not all implementations will support this facility, so may report
651 * an error. To avoid errors, the caller may check for the feature
652 * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method.
653 *
654 * Behaves as qio_channel_readv_full, apart from not supporting
655 * receiving of file handles as well as beginning the read at the
656 * passed @offset
657 *
658 */
659 ssize_t qio_channel_preadv(QIOChannel *ioc, const struct iovec *iov,
660 size_t niov, off_t offset, Error **errp);
661
662 /**
663 * qio_channel_pread
664 * @ioc: the channel object
665 * @buf: the memory region to write data into
666 * @buflen: the number of bytes to @buf
667 * @offset: offset in the channel where writes should begin
668 * @errp: pointer to a NULL-initialized error object
669 *
670 * Not all implementations will support this facility, so may report
671 * an error. To avoid errors, the caller may check for the feature
672 * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method.
673 *
674 */
675 ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, size_t buflen,
676 off_t offset, Error **errp);
677
678 /**
679 * qio_channel_preadv_all_eof:
680 * @ioc: the channel object
681 * @iov: the array of memory regions to read data into
682 * @niov: the length of the @iov array
683 * @offset: the starting offset in the channel to read from
684 * @errp: pointer to a NULL-initialized error object
685 *
686 * Reads @iov, possibly blocking or (if the channel is non-blocking)
687 * yielding from the current coroutine multiple times until the entire
688 * content is read. If end-of-file occurs immediately it is not an
689 * error, but if it occurs after data has been read it will return
690 * an error rather than a short-read. Otherwise behaves as
691 * qio_channel_preadv().
692 *
693 * Returns: 1 if all bytes were read, 0 if end-of-file occurs
694 * without data, or -1 on error
695 */
696 int coroutine_mixed_fn qio_channel_preadv_all_eof(QIOChannel *ioc,
697 const struct iovec *iov,
698 size_t niov,
699 off_t offset,
700 Error **errp);
701
702 /**
703 * qio_channel_preadv_all:
704 * @ioc: the channel object
705 * @iov: the array of memory regions to read data into
706 * @niov: the length of the @iov array
707 * @offset: the starting offset in the channel to read from
708 * @errp: pointer to a NULL-initialized error object
709 *
710 * Reads @iov, possibly blocking or (if the channel is non-blocking)
711 * yielding from the current coroutine multiple times until the entire
712 * content is read. If end-of-file occurs before all requested data
713 * has been read, an error will be reported. Otherwise behaves as
714 * qio_channel_preadv().
715 *
716 * Returns: 0 if all bytes were read, or -1 on error
717 */
718 int coroutine_mixed_fn qio_channel_preadv_all(QIOChannel *ioc,
719 const struct iovec *iov,
720 size_t niov,
721 off_t offset,
722 Error **errp);
723
724 /**
725 * qio_channel_pread_all_eof:
726 * @ioc: the channel object
727 * @buf: the memory region to read data into
728 * @buflen: the number of bytes to read into @buf
729 * @offset: the starting offset in the channel to read from
730 * @errp: pointer to a NULL-initialized error object
731 *
732 * Reads @buflen bytes, possibly blocking or (if the channel is
733 * non-blocking) yielding from the current coroutine multiple times
734 * until the entire content is read. If end-of-file occurs
735 * immediately it is not an error, but if it occurs after data has
736 * been read it will return an error rather than a short-read.
737 * Otherwise behaves as qio_channel_pread().
738 *
739 * Returns: 1 if all bytes were read, 0 if end-of-file occurs
740 * without data, or -1 on error
741 */
742 int coroutine_mixed_fn qio_channel_pread_all_eof(QIOChannel *ioc,
743 void *buf,
744 size_t buflen,
745 off_t offset,
746 Error **errp);
747
748 /**
749 * qio_channel_pread_all:
750 * @ioc: the channel object
751 * @buf: the memory region to read data into
752 * @buflen: the number of bytes to read into @buf
753 * @offset: the starting offset in the channel to read from
754 * @errp: pointer to a NULL-initialized error object
755 *
756 * Reads @buflen bytes, possibly blocking or (if the channel is
757 * non-blocking) yielding from the current coroutine multiple times
758 * until the entire content is read. If end-of-file occurs before
759 * all requested data has been read, an error will be reported.
760 * Otherwise behaves as qio_channel_pread().
761 *
762 * Returns: 0 if all bytes were read, or -1 on error
763 */
764 int coroutine_mixed_fn qio_channel_pread_all(QIOChannel *ioc,
765 void *buf,
766 size_t buflen,
767 off_t offset,
768 Error **errp);
769
770 /**
771 * qio_channel_shutdown:
772 * @ioc: the channel object
773 * @how: the direction to shutdown
774 * @errp: pointer to a NULL-initialized error object
775 *
776 * Shutdowns transmission and/or receiving of data
777 * without closing the underlying transport.
778 *
779 * Not all implementations will support this facility,
780 * so may report an error. To avoid errors, the
781 * caller may check for the feature flag
782 * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling
783 * this method.
784 *
785 * This function is thread-safe, terminates quickly and does not block.
786 *
787 * Returns: 0 on success, -1 on error
788 */
789 int qio_channel_shutdown(QIOChannel *ioc,
790 QIOChannelShutdown how,
791 Error **errp);
792
793 /**
794 * qio_channel_set_delay:
795 * @ioc: the channel object
796 * @enabled: the new flag state
797 *
798 * Controls whether the underlying transport is
799 * permitted to delay writes in order to merge
800 * small packets. If @enabled is true, then the
801 * writes may be delayed in order to opportunistically
802 * merge small packets into larger ones. If @enabled
803 * is false, writes are dispatched immediately with
804 * no delay.
805 *
806 * When @enabled is false, applications may wish to
807 * use the qio_channel_set_cork() method to explicitly
808 * control write merging.
809 *
810 * On channels which are backed by a socket, this
811 * API corresponds to the inverse of TCP_NODELAY flag,
812 * controlling whether the Nagle algorithm is active.
813 *
814 * This setting is merely a hint, so implementations are
815 * free to ignore this without it being considered an
816 * error.
817 */
818 void qio_channel_set_delay(QIOChannel *ioc,
819 bool enabled);
820
821 /**
822 * qio_channel_set_cork:
823 * @ioc: the channel object
824 * @enabled: the new flag state
825 *
826 * Controls whether the underlying transport is
827 * permitted to dispatch data that is written.
828 * If @enabled is true, then any data written will
829 * be queued in local buffers until @enabled is
830 * set to false once again.
831 *
832 * This feature is typically used when the automatic
833 * write coalescing facility is disabled via the
834 * qio_channel_set_delay() method.
835 *
836 * On channels which are backed by a socket, this
837 * API corresponds to the TCP_CORK flag.
838 *
839 * This setting is merely a hint, so implementations are
840 * free to ignore this without it being considered an
841 * error.
842 */
843 void qio_channel_set_cork(QIOChannel *ioc,
844 bool enabled);
845
846
847 /**
848 * qio_channel_seek:
849 * @ioc: the channel object
850 * @offset: the position to seek to, relative to @whence
851 * @whence: one of the (POSIX) SEEK_* constants listed below
852 * @errp: pointer to a NULL-initialized error object
853 *
854 * Moves the current I/O position within the channel
855 * @ioc, to be @offset. The value of @offset is
856 * interpreted relative to @whence:
857 *
858 * SEEK_SET - the position is set to @offset bytes
859 * SEEK_CUR - the position is moved by @offset bytes
860 * SEEK_END - the position is set to end of the file plus @offset bytes
861 *
862 * Not all implementations will support this facility,
863 * so may report an error.
864 *
865 * Returns: the new position on success, (off_t)-1 on failure
866 */
867 off_t qio_channel_io_seek(QIOChannel *ioc,
868 off_t offset,
869 int whence,
870 Error **errp);
871
872
873 /**
874 * qio_channel_create_watch:
875 * @ioc: the channel object
876 * @condition: the I/O condition to monitor
877 *
878 * Create a new main loop source that is used to watch
879 * for the I/O condition @condition. Typically the
880 * qio_channel_add_watch() method would be used instead
881 * of this, since it directly attaches a callback to
882 * the source
883 *
884 * Returns: the new main loop source.
885 */
886 GSource *qio_channel_create_watch(QIOChannel *ioc,
887 GIOCondition condition);
888
889 /**
890 * qio_channel_add_watch:
891 * @ioc: the channel object
892 * @condition: the I/O condition to monitor
893 * @func: callback to invoke when the source becomes ready
894 * @user_data: opaque data to pass to @func
895 * @notify: callback to free @user_data
896 *
897 * Create a new main loop source that is used to watch
898 * for the I/O condition @condition. The callback @func
899 * will be registered against the source, to be invoked
900 * when the source becomes ready. The optional @user_data
901 * will be passed to @func when it is invoked. The @notify
902 * callback will be used to free @user_data when the
903 * watch is deleted
904 *
905 * The returned source ID can be used with g_source_remove()
906 * to remove and free the source when no longer required.
907 * Alternatively the @func callback can return a FALSE
908 * value.
909 *
910 * Returns: the source ID
911 */
912 guint qio_channel_add_watch(QIOChannel *ioc,
913 GIOCondition condition,
914 QIOChannelFunc func,
915 gpointer user_data,
916 GDestroyNotify notify);
917
918 /**
919 * qio_channel_add_watch_full:
920 * @ioc: the channel object
921 * @condition: the I/O condition to monitor
922 * @func: callback to invoke when the source becomes ready
923 * @user_data: opaque data to pass to @func
924 * @notify: callback to free @user_data
925 * @context: the context to run the watch source
926 *
927 * Similar as qio_channel_add_watch(), but allows to specify context
928 * to run the watch source.
929 *
930 * Returns: the source ID
931 */
932 guint qio_channel_add_watch_full(QIOChannel *ioc,
933 GIOCondition condition,
934 QIOChannelFunc func,
935 gpointer user_data,
936 GDestroyNotify notify,
937 GMainContext *context);
938
939 /**
940 * qio_channel_add_watch_source:
941 * @ioc: the channel object
942 * @condition: the I/O condition to monitor
943 * @func: callback to invoke when the source becomes ready
944 * @user_data: opaque data to pass to @func
945 * @notify: callback to free @user_data
946 * @context: gcontext to bind the source to
947 *
948 * Similar as qio_channel_add_watch(), but allows to specify context
949 * to run the watch source, meanwhile return the GSource object
950 * instead of tag ID, with the GSource referenced already.
951 *
952 * Note: callers is responsible to unref the source when not needed.
953 *
954 * Returns: the source pointer
955 */
956 GSource *qio_channel_add_watch_source(QIOChannel *ioc,
957 GIOCondition condition,
958 QIOChannelFunc func,
959 gpointer user_data,
960 GDestroyNotify notify,
961 GMainContext *context);
962
963 /**
964 * qio_channel_yield:
965 * @ioc: the channel object
966 * @condition: the I/O condition to wait for
967 *
968 * Yields execution from the current coroutine until the condition
969 * indicated by @condition becomes available. @condition must
970 * be either %G_IO_IN or %G_IO_OUT; it cannot contain both. In
971 * addition, no two coroutine can be waiting on the same condition
972 * and channel at the same time.
973 *
974 * This must only be called from coroutine context. It is safe to
975 * reenter the coroutine externally while it is waiting; in this
976 * case the function will return even if @condition is not yet
977 * available.
978 */
979 void coroutine_fn qio_channel_yield(QIOChannel *ioc,
980 GIOCondition condition);
981
982 /**
983 * qio_channel_wake_read:
984 * @ioc: the channel object
985 *
986 * If qio_channel_yield() is currently waiting for the channel to become
987 * readable, interrupt it and reenter immediately. This function is safe to call
988 * from any thread.
989 */
990 void qio_channel_wake_read(QIOChannel *ioc);
991
992 /**
993 * qio_channel_wait:
994 * @ioc: the channel object
995 * @condition: the I/O condition to wait for
996 *
997 * Block execution from the current thread until
998 * the condition indicated by @condition becomes
999 * available.
1000 *
1001 * This will enter a nested event loop to perform
1002 * the wait.
1003 */
1004 void qio_channel_wait(QIOChannel *ioc,
1005 GIOCondition condition);
1006
1007 /**
1008 * qio_channel_wait_cond:
1009 * @ioc: the channel object
1010 * @condition: the I/O condition to wait for
1011 *
1012 * Block execution from the current thread until
1013 * the condition indicated by @condition becomes
1014 * available.
1015 *
1016 * This will work with/without a coroutine context, by automatically select
1017 * the proper API to wait.
1018 */
1019 void coroutine_mixed_fn qio_channel_wait_cond(QIOChannel *ioc,
1020 GIOCondition condition);
1021
1022 /**
1023 * qio_channel_set_aio_fd_handler:
1024 * @ioc: the channel object
1025 * @read_ctx: the AioContext to set the read handler on or NULL
1026 * @io_read: the read handler
1027 * @write_ctx: the AioContext to set the write handler on or NULL
1028 * @io_write: the write handler
1029 * @opaque: the opaque value passed to the handler
1030 *
1031 * This is used internally by qio_channel_yield(). It can
1032 * be used by channel implementations to forward the handlers
1033 * to another channel (e.g. from #QIOChannelTLS to the
1034 * underlying socket).
1035 *
1036 * When @read_ctx is NULL, don't touch the read handler. When @write_ctx is
1037 * NULL, don't touch the write handler. Note that setting the read handler
1038 * clears the write handler, and vice versa, if they share the same AioContext.
1039 * Therefore the caller must pass both handlers together when sharing the same
1040 * AioContext.
1041 */
1042 void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
1043 AioContext *read_ctx,
1044 IOHandler *io_read,
1045 AioContext *write_ctx,
1046 IOHandler *io_write,
1047 void *opaque);
1048
1049 /**
1050 * qio_channel_readv_full_all_eof:
1051 * @ioc: the channel object
1052 * @iov: the array of memory regions to read data to
1053 * @niov: the length of the @iov array
1054 * @fds: an array of file handles to read
1055 * @nfds: number of file handles in @fds
1056 * @flags: read flags (QIO_CHANNEL_READ_FLAG_*)
1057 * @errp: pointer to a NULL-initialized error object
1058 *
1059 *
1060 * Performs same function as qio_channel_readv_all_eof.
1061 * Additionally, attempts to read file descriptors shared
1062 * over the channel. The function will wait for all
1063 * requested data to be read, yielding from the current
1064 * coroutine if required. data refers to both file
1065 * descriptors and the iovs.
1066 *
1067 * Returns: 1 if all bytes were read, 0 if end-of-file
1068 * occurs without data, or -1 on error
1069 */
1070
1071 int coroutine_mixed_fn qio_channel_readv_full_all_eof(QIOChannel *ioc,
1072 const struct iovec *iov,
1073 size_t niov,
1074 int **fds, size_t *nfds,
1075 int flags,
1076 Error **errp);
1077
1078 /**
1079 * qio_channel_readv_full_all:
1080 * @ioc: the channel object
1081 * @iov: the array of memory regions to read data to
1082 * @niov: the length of the @iov array
1083 * @fds: an array of file handles to read
1084 * @nfds: number of file handles in @fds
1085 * @errp: pointer to a NULL-initialized error object
1086 *
1087 *
1088 * Performs same function as qio_channel_readv_all_eof.
1089 * Additionally, attempts to read file descriptors shared
1090 * over the channel. The function will wait for all
1091 * requested data to be read, yielding from the current
1092 * coroutine if required. data refers to both file
1093 * descriptors and the iovs.
1094 *
1095 * Returns: 0 if all bytes were read, or -1 on error
1096 */
1097
1098 int coroutine_mixed_fn qio_channel_readv_full_all(QIOChannel *ioc,
1099 const struct iovec *iov,
1100 size_t niov,
1101 int **fds, size_t *nfds,
1102 Error **errp);
1103
1104 /**
1105 * qio_channel_writev_full_all:
1106 * @ioc: the channel object
1107 * @iov: the array of memory regions to write data from
1108 * @niov: the length of the @iov array
1109 * @fds: an array of file handles to send
1110 * @nfds: number of file handles in @fds
1111 * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
1112 * @errp: pointer to a NULL-initialized error object
1113 *
1114 *
1115 * Behaves like qio_channel_writev_full but will attempt
1116 * to send all data passed (file handles and memory regions).
1117 * The function will wait for all requested data
1118 * to be written, yielding from the current coroutine
1119 * if required.
1120 *
1121 * If QIO_CHANNEL_WRITE_FLAG_ZERO_COPY is passed in flags,
1122 * instead of waiting for all requested data to be written,
1123 * this function will wait until it's all queued for writing.
1124 * In this case, if the buffer gets changed between queueing and
1125 * sending, the updated buffer will be sent. If this is not a
1126 * desired behavior, it's suggested to call qio_channel_flush()
1127 * before reusing the buffer.
1128 *
1129 * Returns: 0 if all bytes were written, or -1 on error
1130 */
1131
1132 int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc,
1133 const struct iovec *iov,
1134 size_t niov,
1135 int *fds, size_t nfds,
1136 int flags, Error **errp);
1137
1138 /**
1139 * qio_channel_flush:
1140 * @ioc: the channel object
1141 * @errp: pointer to a NULL-initialized error object
1142 *
1143 * Will block until every packet queued with
1144 * qio_channel_writev_full() + QIO_CHANNEL_WRITE_FLAG_ZERO_COPY
1145 * is sent, or return in case of any error.
1146 *
1147 * If not implemented, acts as a no-op, and returns 0.
1148 *
1149 * Returns -1 if any error is found,
1150 * 1 if at least one send failed to use zero copy.
1151 * 0 if every send successfully used zero copy.
1152 */
1153
1154 int qio_channel_flush(QIOChannel *ioc,
1155 Error **errp);
1156
1157 /**
1158 * qio_channel_get_peercred:
1159 * @ioc: the channel object
1160 * @pid: pointer to pid
1161 * @errp: pointer to a NULL-initialized error object
1162 *
1163 * Returns the pid of the peer process connected to this socket.
1164 *
1165 * The use of this function is possible only for connected
1166 * AF_UNIX stream sockets and for AF_UNIX stream and datagram
1167 * socket pairs on Linux.
1168 * Return -1 on error with pid -1 for the non-Linux OS.
1169 *
1170 */
1171 int qio_channel_get_peerpid(QIOChannel *ioc,
1172 unsigned int *pid,
1173 Error **errp);
1174
1175 #endif /* QIO_CHANNEL_H */