1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "chardev/char-io.h"
26
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;
36 GMainContext *context;
37} 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
54 if (now_active) {
55 iwp->src = qio_channel_create_watch(
56 iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
57 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
58 g_source_attach(iwp->src, iwp->context);
59 } else {
60 g_source_destroy(iwp->src);
61 g_source_unref(iwp->src);
62 iwp->src = NULL;
63 }
64 return FALSE;
65}
66
67static gboolean io_watch_poll_check(GSource *source)
68{
69 return FALSE;
70}
71
72static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
73 gpointer user_data)
74{
75 abort();
76}
77
78static void io_watch_poll_finalize(GSource *source)
79{
80
81
82
83
84
85
86
87
88
89
90 IOWatchPoll *iwp = io_watch_poll_from_source(source);
91 assert(iwp->src == NULL);
92}
93
94static GSourceFuncs io_watch_poll_funcs = {
95 .prepare = io_watch_poll_prepare,
96 .check = io_watch_poll_check,
97 .dispatch = io_watch_poll_dispatch,
98 .finalize = io_watch_poll_finalize,
99};
100
101GSource *io_add_watch_poll(Chardev *chr,
102 QIOChannel *ioc,
103 IOCanReadHandler *fd_can_read,
104 QIOChannelFunc fd_read,
105 gpointer user_data,
106 GMainContext *context)
107{
108 IOWatchPoll *iwp;
109 char *name;
110
111 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs,
112 sizeof(IOWatchPoll));
113 iwp->fd_can_read = fd_can_read;
114 iwp->opaque = user_data;
115 iwp->ioc = ioc;
116 iwp->fd_read = (GSourceFunc) fd_read;
117 iwp->src = NULL;
118 iwp->context = context;
119
120 name = g_strdup_printf("chardev-iowatch-%s", chr->label);
121 g_source_set_name((GSource *)iwp, name);
122 g_free(name);
123
124 g_source_attach(&iwp->parent, context);
125 g_source_unref(&iwp->parent);
126 return (GSource *)iwp;
127}
128
129static void io_remove_watch_poll(GSource *source)
130{
131 IOWatchPoll *iwp;
132
133 iwp = io_watch_poll_from_source(source);
134 if (iwp->src) {
135 g_source_destroy(iwp->src);
136 g_source_unref(iwp->src);
137 iwp->src = NULL;
138 }
139 g_source_destroy(&iwp->parent);
140}
141
142void remove_fd_in_watch(Chardev *chr)
143{
144 if (chr->gsource) {
145 io_remove_watch_poll(chr->gsource);
146 chr->gsource = NULL;
147 }
148}
149
150int io_channel_send_full(QIOChannel *ioc,
151 const void *buf, size_t len,
152 int *fds, size_t nfds)
153{
154 size_t offset = 0;
155
156 while (offset < len) {
157 ssize_t ret = 0;
158 struct iovec iov = { .iov_base = (char *)buf + offset,
159 .iov_len = len - offset };
160
161 ret = qio_channel_writev_full(
162 ioc, &iov, 1,
163 fds, nfds, NULL);
164 if (ret == QIO_CHANNEL_ERR_BLOCK) {
165 if (offset) {
166 return offset;
167 }
168
169 errno = EAGAIN;
170 return -1;
171 } else if (ret < 0) {
172 errno = EINVAL;
173 return -1;
174 }
175
176 offset += ret;
177 }
178
179 return offset;
180}
181
182int io_channel_send(QIOChannel *ioc, const void *buf, size_t len)
183{
184 return io_channel_send_full(ioc, buf, len, NULL, 0);
185}
186