]> git.proxmox.com Git - mirror_qemu.git/blame - qga/channel-posix.c
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / qga / channel-posix.c
CommitLineData
4459bf38 1#include "qemu/osdep.h"
b9947c9c 2#include "qemu/cutils.h"
125b310e 3#include <termios.h>
da34e65c 4#include "qapi/error.h"
1de7afc9 5#include "qemu/sockets.h"
dc03272d 6#include "channel.h"
b9947c9c 7#include "cutils.h"
125b310e 8
e61ab1da
AF
9#ifdef CONFIG_SOLARIS
10#include <stropts.h>
11#endif
12
125b310e
MR
13#define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
14
15struct GAChannel {
16 GIOChannel *listen_channel;
17 GIOChannel *client_channel;
18 GAChannelMethod method;
19 GAChannelCallback event_cb;
20 gpointer user_data;
21};
22
23static int ga_channel_client_add(GAChannel *c, int fd);
24
25static gboolean ga_channel_listen_accept(GIOChannel *channel,
26 GIOCondition condition, gpointer data)
27{
28 GAChannel *c = data;
29 int ret, client_fd;
30 bool accepted = false;
125b310e
MR
31
32 g_assert(channel != NULL);
33
b8093d38 34 client_fd = qemu_accept(g_io_channel_unix_get_fd(channel), NULL, NULL);
125b310e
MR
35 if (client_fd == -1) {
36 g_warning("error converting fd to gsocket: %s", strerror(errno));
37 goto out;
38 }
ff5927ba 39 qemu_socket_set_nonblock(client_fd);
125b310e
MR
40 ret = ga_channel_client_add(c, client_fd);
41 if (ret) {
42 g_warning("error setting up connection");
32c16620 43 close(client_fd);
125b310e
MR
44 goto out;
45 }
46 accepted = true;
47
48out:
49 /* only accept 1 connection at a time */
50 return !accepted;
51}
52
53/* start polling for readable events on listen fd, new==true
54 * indicates we should use the existing s->listen_channel
55 */
56static void ga_channel_listen_add(GAChannel *c, int listen_fd, bool create)
57{
58 if (create) {
59 c->listen_channel = g_io_channel_unix_new(listen_fd);
60 }
61 g_io_add_watch(c->listen_channel, G_IO_IN, ga_channel_listen_accept, c);
62}
63
64static void ga_channel_listen_close(GAChannel *c)
65{
125b310e
MR
66 g_assert(c->listen_channel);
67 g_io_channel_shutdown(c->listen_channel, true, NULL);
68 g_io_channel_unref(c->listen_channel);
69 c->listen_channel = NULL;
70}
71
72/* cleanup state for closed connection/session, start accepting new
73 * connections if we're in listening mode
74 */
75static void ga_channel_client_close(GAChannel *c)
76{
77 g_assert(c->client_channel);
78 g_io_channel_shutdown(c->client_channel, true, NULL);
79 g_io_channel_unref(c->client_channel);
80 c->client_channel = NULL;
f06b2031 81 if (c->listen_channel) {
125b310e
MR
82 ga_channel_listen_add(c, 0, false);
83 }
84}
85
86static gboolean ga_channel_client_event(GIOChannel *channel,
87 GIOCondition condition, gpointer data)
88{
89 GAChannel *c = data;
90 gboolean client_cont;
91
92 g_assert(c);
93 if (c->event_cb) {
94 client_cont = c->event_cb(condition, c->user_data);
95 if (!client_cont) {
96 ga_channel_client_close(c);
97 return false;
98 }
99 }
100 return true;
101}
102
103static int ga_channel_client_add(GAChannel *c, int fd)
104{
105 GIOChannel *client_channel;
106 GError *err = NULL;
107
108 g_assert(c && !c->client_channel);
109 client_channel = g_io_channel_unix_new(fd);
110 g_assert(client_channel);
111 g_io_channel_set_encoding(client_channel, NULL, &err);
112 if (err != NULL) {
113 g_warning("error setting channel encoding to binary");
114 g_error_free(err);
115 return -1;
116 }
117 g_io_add_watch(client_channel, G_IO_IN | G_IO_HUP,
118 ga_channel_client_event, c);
119 c->client_channel = client_channel;
120 return 0;
121}
122
26de2296 123static gboolean ga_channel_open(GAChannel *c, const gchar *path,
87ed8b2c 124 GAChannelMethod method, int fd, Error **errp)
125b310e
MR
125{
126 int ret;
127 c->method = method;
128
129 switch (c->method) {
130 case GA_CHANNEL_VIRTIO_SERIAL: {
26de2296 131 assert(fd < 0);
b9947c9c
MAL
132 fd = qga_open_cloexec(
133 path,
e61ab1da 134#ifndef CONFIG_SOLARIS
b9947c9c 135 O_ASYNC |
e61ab1da 136#endif
b9947c9c
MAL
137 O_RDWR | O_NONBLOCK,
138 0
139 );
125b310e 140 if (fd == -1) {
3845ffff 141 error_setg_errno(errp, errno, "error opening channel '%s'", path);
7868181f 142 return false;
125b310e 143 }
e61ab1da
AF
144#ifdef CONFIG_SOLARIS
145 ret = ioctl(fd, I_SETSIG, S_OUTPUT | S_INPUT | S_HIPRI);
146 if (ret == -1) {
87ed8b2c 147 error_setg_errno(errp, errno, "error setting event mask for channel");
7868181f
MA
148 close(fd);
149 return false;
e61ab1da
AF
150 }
151#endif
c6cd588b
AI
152#ifdef __FreeBSD__
153 /*
154 * In the default state channel sends echo of every command to a
01dc0651 155 * client. The client program doesn't expect this and raises an
c6cd588b
AI
156 * error. Suppress echo by resetting ECHO terminal flag.
157 */
158 struct termios tio;
159 if (tcgetattr(fd, &tio) < 0) {
160 error_setg_errno(errp, errno, "error getting channel termios attrs");
161 close(fd);
162 return false;
163 }
164 tio.c_lflag &= ~ECHO;
165 if (tcsetattr(fd, TCSAFLUSH, &tio) < 0) {
166 error_setg_errno(errp, errno, "error setting channel termios attrs");
167 close(fd);
168 return false;
169 }
170#endif /* __FreeBSD__ */
125b310e
MR
171 ret = ga_channel_client_add(c, fd);
172 if (ret) {
87ed8b2c 173 error_setg(errp, "error adding channel to main loop");
d4f4a3ef 174 close(fd);
125b310e
MR
175 return false;
176 }
177 break;
178 }
179 case GA_CHANNEL_ISA_SERIAL: {
180 struct termios tio;
26de2296
SH
181
182 assert(fd < 0);
b9947c9c 183 fd = qga_open_cloexec(path, O_RDWR | O_NOCTTY | O_NONBLOCK, 0);
125b310e 184 if (fd == -1) {
3845ffff 185 error_setg_errno(errp, errno, "error opening channel '%s'", path);
7868181f 186 return false;
125b310e
MR
187 }
188 tcgetattr(fd, &tio);
189 /* set up serial port for non-canonical, dumb byte streaming */
190 tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
191 INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY |
192 IMAXBEL);
193 tio.c_oflag = 0;
194 tio.c_lflag = 0;
195 tio.c_cflag |= GA_CHANNEL_BAUDRATE_DEFAULT;
196 /* 1 available byte min or reads will block (we'll set non-blocking
197 * elsewhere, else we have to deal with read()=0 instead)
198 */
199 tio.c_cc[VMIN] = 1;
200 tio.c_cc[VTIME] = 0;
201 /* flush everything waiting for read/xmit, it's garbage at this point */
202 tcflush(fd, TCIFLUSH);
203 tcsetattr(fd, TCSANOW, &tio);
204 ret = ga_channel_client_add(c, fd);
205 if (ret) {
87ed8b2c 206 error_setg(errp, "error adding channel to main loop");
7868181f
MA
207 close(fd);
208 return false;
125b310e
MR
209 }
210 break;
211 }
212 case GA_CHANNEL_UNIX_LISTEN: {
26de2296 213 if (fd < 0) {
87ed8b2c
MAL
214 fd = unix_listen(path, errp);
215 if (fd < 0) {
26de2296
SH
216 return false;
217 }
125b310e
MR
218 }
219 ga_channel_listen_add(c, fd, true);
220 break;
221 }
586ef5de 222 case GA_CHANNEL_VSOCK_LISTEN: {
26de2296 223 if (fd < 0) {
26de2296
SH
224 SocketAddress *addr;
225 char *addr_str;
586ef5de 226
26de2296 227 addr_str = g_strdup_printf("vsock:%s", path);
87ed8b2c 228 addr = socket_parse(addr_str, errp);
26de2296 229 g_free(addr_str);
87ed8b2c 230 if (!addr) {
26de2296
SH
231 return false;
232 }
233
87ed8b2c 234 fd = socket_listen(addr, 1, errp);
26de2296 235 qapi_free_SocketAddress(addr);
87ed8b2c 236 if (fd < 0) {
26de2296
SH
237 return false;
238 }
586ef5de
SH
239 }
240 ga_channel_listen_add(c, fd, true);
241 break;
242 }
125b310e 243 default:
87ed8b2c 244 error_setg(errp, "error binding/listening to specified socket");
125b310e
MR
245 return false;
246 }
247
248 return true;
249}
250
251GIOStatus ga_channel_write_all(GAChannel *c, const gchar *buf, gsize size)
252{
253 GError *err = NULL;
254 gsize written = 0;
255 GIOStatus status = G_IO_STATUS_NORMAL;
256
257 while (size) {
f74df9bf 258 g_debug("sending data, count: %d", (int)size);
125b310e
MR
259 status = g_io_channel_write_chars(c->client_channel, buf, size,
260 &written, &err);
f74df9bf
YP
261 if (status == G_IO_STATUS_NORMAL) {
262 size -= written;
263 buf += written;
264 } else if (status != G_IO_STATUS_AGAIN) {
125b310e 265 g_warning("error writing to channel: %s", err->message);
f74df9bf 266 return status;
125b310e 267 }
125b310e
MR
268 }
269
f74df9bf 270 do {
125b310e 271 status = g_io_channel_flush(c->client_channel, &err);
f74df9bf
YP
272 } while (status == G_IO_STATUS_AGAIN);
273
274 if (status != G_IO_STATUS_NORMAL) {
275 g_warning("error flushing channel: %s", err->message);
125b310e
MR
276 }
277
278 return status;
279}
280
281GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
282{
283 return g_io_channel_read_chars(c->client_channel, buf, size, count, NULL);
284}
285
286GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
26de2296 287 int listen_fd, GAChannelCallback cb, gpointer opaque)
125b310e 288{
87ed8b2c 289 Error *err = NULL;
f3a06403 290 GAChannel *c = g_new0(GAChannel, 1);
125b310e
MR
291 c->event_cb = cb;
292 c->user_data = opaque;
293
87ed8b2c
MAL
294 if (!ga_channel_open(c, path, method, listen_fd, &err)) {
295 g_critical("%s", error_get_pretty(err));
296 error_free(err);
125b310e
MR
297 ga_channel_free(c);
298 return NULL;
299 }
300
301 return c;
302}
303
304void ga_channel_free(GAChannel *c)
305{
f06b2031 306 if (c->listen_channel) {
125b310e
MR
307 ga_channel_listen_close(c);
308 }
309 if (c->client_channel) {
310 ga_channel_client_close(c);
311 }
312 g_free(c);
313}