blob: 3be17b51ca5e4e0b1420130ecb53b743c2d7af0a [file] [log] [blame]
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +03001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24#include "qemu/osdep.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040025#include "chardev/char-io.h"
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +030026
27typedef struct IOWatchPoll {
28 GSource parent;
29
30 QIOChannel *ioc;
31 GSource *src;
32
33 IOCanReadHandler *fd_can_read;
34 GSourceFunc fd_read;
35 void *opaque;
Daniel P. Berrangé038b4212024-03-18 17:08:30 +000036 GMainContext *context;
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +030037} IOWatchPoll;
38
39static IOWatchPoll *io_watch_poll_from_source(GSource *source)
40{
41 return container_of(source, IOWatchPoll, parent);
42}
43
44static gboolean io_watch_poll_prepare(GSource *source,
45 gint *timeout)
46{
47 IOWatchPoll *iwp = io_watch_poll_from_source(source);
48 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
49 bool was_active = iwp->src != NULL;
50 if (was_active == now_active) {
51 return FALSE;
52 }
53
Daniel P. Berrangé038b4212024-03-18 17:08:30 +000054 /*
55 * We do not register the QIOChannel watch as a child GSource.
56 * The 'prepare' function on the parent GSource will be
57 * skipped if a child GSource's 'prepare' function indicates
58 * readiness. We need this prepare function be guaranteed
59 * to run on *every* iteration of the main loop, because
60 * it is critical to ensure we remove the QIOChannel watch
61 * if 'fd_can_read' indicates the frontend cannot receive
62 * more data.
63 */
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +030064 if (now_active) {
65 iwp->src = qio_channel_create_watch(
66 iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
67 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Daniel P. Berrangé038b4212024-03-18 17:08:30 +000068 g_source_attach(iwp->src, iwp->context);
Marc-André Lureaua7077b82018-08-27 15:48:24 +020069 } else {
Daniel P. Berrangé038b4212024-03-18 17:08:30 +000070 g_source_destroy(iwp->src);
71 g_source_unref(iwp->src);
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +030072 iwp->src = NULL;
73 }
74 return FALSE;
75}
76
Daniel P. Berrangé038b4212024-03-18 17:08:30 +000077static gboolean io_watch_poll_check(GSource *source)
78{
79 return FALSE;
80}
81
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +030082static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
83 gpointer user_data)
84{
Daniel P. Berrangé038b4212024-03-18 17:08:30 +000085 abort();
86}
87
88static void io_watch_poll_finalize(GSource *source)
89{
Daniel P. Berrangé038b4212024-03-18 17:08:30 +000090 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Sergey Dyaslie0bf9542024-07-12 09:26:59 +000091 if (iwp->src) {
92 g_source_destroy(iwp->src);
93 g_source_unref(iwp->src);
94 iwp->src = NULL;
95 }
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +030096}
97
98static GSourceFuncs io_watch_poll_funcs = {
99 .prepare = io_watch_poll_prepare,
Daniel P. Berrangé038b4212024-03-18 17:08:30 +0000100 .check = io_watch_poll_check,
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300101 .dispatch = io_watch_poll_dispatch,
Daniel P. Berrangé038b4212024-03-18 17:08:30 +0000102 .finalize = io_watch_poll_finalize,
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300103};
104
zhanghailiangb19456d2017-04-19 09:15:32 +0800105GSource *io_add_watch_poll(Chardev *chr,
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300106 QIOChannel *ioc,
107 IOCanReadHandler *fd_can_read,
108 QIOChannelFunc fd_read,
109 gpointer user_data,
110 GMainContext *context)
111{
112 IOWatchPoll *iwp;
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300113 char *name;
114
115 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs,
116 sizeof(IOWatchPoll));
117 iwp->fd_can_read = fd_can_read;
118 iwp->opaque = user_data;
119 iwp->ioc = ioc;
120 iwp->fd_read = (GSourceFunc) fd_read;
121 iwp->src = NULL;
Daniel P. Berrangé038b4212024-03-18 17:08:30 +0000122 iwp->context = context;
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300123
124 name = g_strdup_printf("chardev-iowatch-%s", chr->label);
125 g_source_set_name((GSource *)iwp, name);
126 g_free(name);
127
zhanghailiangb19456d2017-04-19 09:15:32 +0800128 g_source_attach(&iwp->parent, context);
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300129 g_source_unref(&iwp->parent);
zhanghailiangb19456d2017-04-19 09:15:32 +0800130 return (GSource *)iwp;
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300131}
132
Daniel P. Berrangé038b4212024-03-18 17:08:30 +0000133static void io_remove_watch_poll(GSource *source)
134{
135 IOWatchPoll *iwp;
136
137 iwp = io_watch_poll_from_source(source);
Daniel P. Berrangé038b4212024-03-18 17:08:30 +0000138 g_source_destroy(&iwp->parent);
139}
140
zhanghailiangb19456d2017-04-19 09:15:32 +0800141void remove_fd_in_watch(Chardev *chr)
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300142{
zhanghailiangb19456d2017-04-19 09:15:32 +0800143 if (chr->gsource) {
Daniel P. Berrangé038b4212024-03-18 17:08:30 +0000144 io_remove_watch_poll(chr->gsource);
zhanghailiangb19456d2017-04-19 09:15:32 +0800145 chr->gsource = NULL;
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300146 }
147}
148
149int io_channel_send_full(QIOChannel *ioc,
150 const void *buf, size_t len,
151 int *fds, size_t nfds)
152{
153 size_t offset = 0;
154
155 while (offset < len) {
156 ssize_t ret = 0;
157 struct iovec iov = { .iov_base = (char *)buf + offset,
158 .iov_len = len - offset };
159
160 ret = qio_channel_writev_full(
161 ioc, &iov, 1,
Leonardo Brasb88651c2022-05-13 03:28:31 -0300162 fds, nfds, 0, NULL);
Marc-André Lureaua6da7ff2016-12-12 17:07:52 +0300163 if (ret == QIO_CHANNEL_ERR_BLOCK) {
164 if (offset) {
165 return offset;
166 }
167
168 errno = EAGAIN;
169 return -1;
170 } else if (ret < 0) {
171 errno = EINVAL;
172 return -1;
173 }
174
175 offset += ret;
176 }
177
178 return offset;
179}
180
181int io_channel_send(QIOChannel *ioc, const void *buf, size_t len)
182{
183 return io_channel_send_full(ioc, buf, len, NULL, 0);
184}