]>
Commit | Line | Data |
---|---|---|
6f97dba0 AL |
1 | /* |
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-common.h" | |
83c9089e | 25 | #include "monitor/monitor.h" |
9c17d615 | 26 | #include "sysemu/sysemu.h" |
1de7afc9 | 27 | #include "qemu/timer.h" |
dccfcd0e | 28 | #include "sysemu/char.h" |
cf3ebac7 | 29 | #include "hw/usb.h" |
c5a415a0 | 30 | #include "qmp-commands.h" |
cfb429cb CM |
31 | #include "qapi/qmp-input-visitor.h" |
32 | #include "qapi/qmp-output-visitor.h" | |
33 | #include "qapi-visit.h" | |
6f97dba0 AL |
34 | |
35 | #include <unistd.h> | |
36 | #include <fcntl.h> | |
6f97dba0 AL |
37 | #include <time.h> |
38 | #include <errno.h> | |
39 | #include <sys/time.h> | |
40 | #include <zlib.h> | |
41 | ||
42 | #ifndef _WIN32 | |
43 | #include <sys/times.h> | |
44 | #include <sys/wait.h> | |
45 | #include <termios.h> | |
46 | #include <sys/mman.h> | |
47 | #include <sys/ioctl.h> | |
24646c7e | 48 | #include <sys/resource.h> |
6f97dba0 AL |
49 | #include <sys/socket.h> |
50 | #include <netinet/in.h> | |
24646c7e | 51 | #include <net/if.h> |
24646c7e | 52 | #include <arpa/inet.h> |
6f97dba0 AL |
53 | #include <dirent.h> |
54 | #include <netdb.h> | |
55 | #include <sys/select.h> | |
71e72a19 | 56 | #ifdef CONFIG_BSD |
6f97dba0 | 57 | #include <sys/stat.h> |
3294ce18 MT |
58 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
59 | #include <dev/ppbus/ppi.h> | |
60 | #include <dev/ppbus/ppbconf.h> | |
c5e97233 | 61 | #elif defined(__DragonFly__) |
c5e97233 BS |
62 | #include <dev/misc/ppi/ppi.h> |
63 | #include <bus/ppbus/ppbconf.h> | |
6f97dba0 | 64 | #endif |
bbe813a2 | 65 | #else |
6f97dba0 | 66 | #ifdef __linux__ |
6f97dba0 AL |
67 | #include <linux/ppdev.h> |
68 | #include <linux/parport.h> | |
69 | #endif | |
70 | #ifdef __sun__ | |
71 | #include <sys/stat.h> | |
72 | #include <sys/ethernet.h> | |
73 | #include <sys/sockio.h> | |
74 | #include <netinet/arp.h> | |
75 | #include <netinet/in.h> | |
76 | #include <netinet/in_systm.h> | |
77 | #include <netinet/ip.h> | |
78 | #include <netinet/ip_icmp.h> // must come after ip.h | |
79 | #include <netinet/udp.h> | |
80 | #include <netinet/tcp.h> | |
6f97dba0 AL |
81 | #endif |
82 | #endif | |
83 | #endif | |
84 | ||
1de7afc9 | 85 | #include "qemu/sockets.h" |
cbcc6336 | 86 | #include "ui/qemu-spice.h" |
6f97dba0 | 87 | |
9bd7854e | 88 | #define READ_BUF_LEN 4096 |
7b0bfdf5 | 89 | #define READ_RETRIES 10 |
9f781168 | 90 | #define CHR_MAX_FILENAME_SIZE 256 |
c4095726 | 91 | #define TCP_MAX_FDS 16 |
9bd7854e | 92 | |
cfb429cb CM |
93 | /***********************************************************/ |
94 | /* Socket address helpers */ | |
95 | static void qapi_copy_SocketAddress(SocketAddress **p_dest, | |
96 | SocketAddress *src) | |
97 | { | |
98 | QmpOutputVisitor *qov; | |
99 | QmpInputVisitor *qiv; | |
100 | Visitor *ov, *iv; | |
101 | QObject *obj; | |
102 | ||
103 | *p_dest = NULL; | |
104 | ||
105 | qov = qmp_output_visitor_new(); | |
106 | ov = qmp_output_get_visitor(qov); | |
107 | visit_type_SocketAddress(ov, &src, NULL, &error_abort); | |
108 | obj = qmp_output_get_qobject(qov); | |
109 | qmp_output_visitor_cleanup(qov); | |
110 | if (!obj) { | |
111 | return; | |
112 | } | |
113 | ||
114 | qiv = qmp_input_visitor_new(obj); | |
115 | iv = qmp_input_get_visitor(qiv); | |
116 | visit_type_SocketAddress(iv, p_dest, NULL, &error_abort); | |
117 | qmp_input_visitor_cleanup(qiv); | |
118 | qobject_decref(obj); | |
119 | } | |
120 | ||
16cc4ffe CM |
121 | static int SocketAddress_to_str(char *dest, int max_len, |
122 | const char *prefix, SocketAddress *addr, | |
123 | bool is_listen, bool is_telnet) | |
124 | { | |
125 | switch (addr->kind) { | |
126 | case SOCKET_ADDRESS_KIND_INET: | |
127 | return snprintf(dest, max_len, "%s%s:%s:%s%s", prefix, | |
128 | is_telnet ? "telnet" : "tcp", addr->inet->host, | |
129 | addr->inet->port, is_listen ? ",server" : ""); | |
130 | break; | |
131 | case SOCKET_ADDRESS_KIND_UNIX: | |
132 | return snprintf(dest, max_len, "%sunix:%s%s", prefix, | |
133 | addr->q_unix->path, is_listen ? ",server" : ""); | |
134 | break; | |
135 | case SOCKET_ADDRESS_KIND_FD: | |
136 | return snprintf(dest, max_len, "%sfd:%s%s", prefix, addr->fd->str, | |
137 | is_listen ? ",server" : ""); | |
138 | break; | |
139 | default: | |
140 | abort(); | |
141 | } | |
142 | } | |
143 | ||
144 | static int sockaddr_to_str(char *dest, int max_len, | |
145 | struct sockaddr_storage *ss, socklen_t ss_len, | |
01ca519f | 146 | struct sockaddr_storage *ps, socklen_t ps_len, |
16cc4ffe CM |
147 | bool is_listen, bool is_telnet) |
148 | { | |
01ca519f CM |
149 | char shost[NI_MAXHOST], sserv[NI_MAXSERV]; |
150 | char phost[NI_MAXHOST], pserv[NI_MAXSERV]; | |
16cc4ffe CM |
151 | const char *left = "", *right = ""; |
152 | ||
153 | switch (ss->ss_family) { | |
154 | #ifndef _WIN32 | |
155 | case AF_UNIX: | |
156 | return snprintf(dest, max_len, "unix:%s%s", | |
157 | ((struct sockaddr_un *)(ss))->sun_path, | |
158 | is_listen ? ",server" : ""); | |
159 | #endif | |
160 | case AF_INET6: | |
161 | left = "["; | |
162 | right = "]"; | |
163 | /* fall through */ | |
164 | case AF_INET: | |
01ca519f CM |
165 | getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost), |
166 | sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV); | |
167 | getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost), | |
168 | pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV); | |
169 | return snprintf(dest, max_len, "%s:%s%s%s:%s%s <-> %s%s%s:%s", | |
16cc4ffe | 170 | is_telnet ? "telnet" : "tcp", |
01ca519f CM |
171 | left, shost, right, sserv, |
172 | is_listen ? ",server" : "", | |
173 | left, phost, right, pserv); | |
16cc4ffe CM |
174 | |
175 | default: | |
176 | return snprintf(dest, max_len, "unknown"); | |
177 | } | |
178 | } | |
179 | ||
6f97dba0 AL |
180 | /***********************************************************/ |
181 | /* character device */ | |
182 | ||
72cf2d4f BS |
183 | static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs = |
184 | QTAILQ_HEAD_INITIALIZER(chardevs); | |
2970a6c9 | 185 | |
db39fcf1 PB |
186 | CharDriverState *qemu_chr_alloc(void) |
187 | { | |
188 | CharDriverState *chr = g_malloc0(sizeof(CharDriverState)); | |
f3db17b9 | 189 | qemu_mutex_init(&chr->chr_write_lock); |
db39fcf1 PB |
190 | return chr; |
191 | } | |
192 | ||
a425d23f | 193 | void qemu_chr_be_event(CharDriverState *s, int event) |
6f97dba0 | 194 | { |
73cdf3f2 AG |
195 | /* Keep track if the char device is open */ |
196 | switch (event) { | |
197 | case CHR_EVENT_OPENED: | |
16665b94 | 198 | s->be_open = 1; |
73cdf3f2 AG |
199 | break; |
200 | case CHR_EVENT_CLOSED: | |
16665b94 | 201 | s->be_open = 0; |
73cdf3f2 AG |
202 | break; |
203 | } | |
204 | ||
6f97dba0 AL |
205 | if (!s->chr_event) |
206 | return; | |
207 | s->chr_event(s->handler_opaque, event); | |
208 | } | |
209 | ||
fee204fd | 210 | void qemu_chr_be_generic_open(CharDriverState *s) |
6f97dba0 | 211 | { |
bd5c51ee | 212 | qemu_chr_be_event(s, CHR_EVENT_OPENED); |
6f97dba0 AL |
213 | } |
214 | ||
2cc6e0a1 | 215 | int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len) |
6f97dba0 | 216 | { |
9005b2a7 PB |
217 | int ret; |
218 | ||
219 | qemu_mutex_lock(&s->chr_write_lock); | |
220 | ret = s->chr_write(s, buf, len); | |
221 | qemu_mutex_unlock(&s->chr_write_lock); | |
222 | return ret; | |
6f97dba0 AL |
223 | } |
224 | ||
cd18720a AL |
225 | int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) |
226 | { | |
227 | int offset = 0; | |
09313047 | 228 | int res = 0; |
cd18720a | 229 | |
9005b2a7 | 230 | qemu_mutex_lock(&s->chr_write_lock); |
cd18720a AL |
231 | while (offset < len) { |
232 | do { | |
233 | res = s->chr_write(s, buf + offset, len - offset); | |
234 | if (res == -1 && errno == EAGAIN) { | |
235 | g_usleep(100); | |
236 | } | |
237 | } while (res == -1 && errno == EAGAIN); | |
238 | ||
9005b2a7 | 239 | if (res <= 0) { |
cd18720a AL |
240 | break; |
241 | } | |
242 | ||
cd18720a AL |
243 | offset += res; |
244 | } | |
9005b2a7 | 245 | qemu_mutex_unlock(&s->chr_write_lock); |
cd18720a | 246 | |
9005b2a7 PB |
247 | if (res < 0) { |
248 | return res; | |
249 | } | |
cd18720a AL |
250 | return offset; |
251 | } | |
252 | ||
7b0bfdf5 NN |
253 | int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len) |
254 | { | |
255 | int offset = 0, counter = 10; | |
256 | int res; | |
257 | ||
258 | if (!s->chr_sync_read) { | |
259 | return 0; | |
260 | } | |
261 | ||
262 | while (offset < len) { | |
263 | do { | |
264 | res = s->chr_sync_read(s, buf + offset, len - offset); | |
265 | if (res == -1 && errno == EAGAIN) { | |
266 | g_usleep(100); | |
267 | } | |
268 | } while (res == -1 && errno == EAGAIN); | |
269 | ||
270 | if (res == 0) { | |
271 | break; | |
272 | } | |
273 | ||
274 | if (res < 0) { | |
275 | return res; | |
276 | } | |
277 | ||
278 | offset += res; | |
279 | ||
280 | if (!counter--) { | |
281 | break; | |
282 | } | |
283 | } | |
284 | ||
285 | return offset; | |
286 | } | |
287 | ||
41084f1b | 288 | int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg) |
6f97dba0 AL |
289 | { |
290 | if (!s->chr_ioctl) | |
291 | return -ENOTSUP; | |
292 | return s->chr_ioctl(s, cmd, arg); | |
293 | } | |
294 | ||
909cda12 | 295 | int qemu_chr_be_can_write(CharDriverState *s) |
6f97dba0 AL |
296 | { |
297 | if (!s->chr_can_read) | |
298 | return 0; | |
299 | return s->chr_can_read(s->handler_opaque); | |
300 | } | |
301 | ||
fa5efccb | 302 | void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len) |
6f97dba0 | 303 | { |
ac310734 SW |
304 | if (s->chr_read) { |
305 | s->chr_read(s->handler_opaque, buf, len); | |
306 | } | |
6f97dba0 AL |
307 | } |
308 | ||
74c0d6f0 | 309 | int qemu_chr_fe_get_msgfd(CharDriverState *s) |
7d174059 | 310 | { |
c76bf6bb | 311 | int fd; |
4f858614 | 312 | return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1; |
c76bf6bb NN |
313 | } |
314 | ||
315 | int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len) | |
316 | { | |
317 | return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1; | |
7d174059 MM |
318 | } |
319 | ||
d39aac7a NN |
320 | int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num) |
321 | { | |
322 | return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1; | |
323 | } | |
324 | ||
13661089 DB |
325 | int qemu_chr_add_client(CharDriverState *s, int fd) |
326 | { | |
327 | return s->chr_add_client ? s->chr_add_client(s, fd) : -1; | |
328 | } | |
329 | ||
6f97dba0 AL |
330 | void qemu_chr_accept_input(CharDriverState *s) |
331 | { | |
332 | if (s->chr_accept_input) | |
333 | s->chr_accept_input(s); | |
98c8ee1d | 334 | qemu_notify_event(); |
6f97dba0 AL |
335 | } |
336 | ||
e7e71b0e | 337 | void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...) |
6f97dba0 | 338 | { |
9bd7854e | 339 | char buf[READ_BUF_LEN]; |
6f97dba0 AL |
340 | va_list ap; |
341 | va_start(ap, fmt); | |
342 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
2cc6e0a1 | 343 | qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf)); |
6f97dba0 AL |
344 | va_end(ap); |
345 | } | |
346 | ||
386a5a1e AS |
347 | static void remove_fd_in_watch(CharDriverState *chr); |
348 | ||
6f97dba0 | 349 | void qemu_chr_add_handlers(CharDriverState *s, |
7b27a769 | 350 | IOCanReadHandler *fd_can_read, |
6f97dba0 AL |
351 | IOReadHandler *fd_read, |
352 | IOEventHandler *fd_event, | |
353 | void *opaque) | |
354 | { | |
19083228 HG |
355 | int fe_open; |
356 | ||
da7d998b | 357 | if (!opaque && !fd_can_read && !fd_read && !fd_event) { |
19083228 | 358 | fe_open = 0; |
386a5a1e | 359 | remove_fd_in_watch(s); |
19083228 HG |
360 | } else { |
361 | fe_open = 1; | |
2d6c1ef4 | 362 | } |
6f97dba0 AL |
363 | s->chr_can_read = fd_can_read; |
364 | s->chr_read = fd_read; | |
365 | s->chr_event = fd_event; | |
366 | s->handler_opaque = opaque; | |
ac1b84dd | 367 | if (fe_open && s->chr_update_read_handler) |
6f97dba0 | 368 | s->chr_update_read_handler(s); |
73cdf3f2 | 369 | |
19083228 | 370 | if (!s->explicit_fe_open) { |
8e25daa8 | 371 | qemu_chr_fe_set_open(s, fe_open); |
19083228 HG |
372 | } |
373 | ||
73cdf3f2 AG |
374 | /* We're connecting to an already opened device, so let's make sure we |
375 | also get the open event */ | |
a59bcd31 | 376 | if (fe_open && s->be_open) { |
fee204fd | 377 | qemu_chr_be_generic_open(s); |
73cdf3f2 | 378 | } |
6f97dba0 AL |
379 | } |
380 | ||
381 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
382 | { | |
383 | return len; | |
384 | } | |
385 | ||
80dca9e6 | 386 | static CharDriverState *qemu_chr_open_null(void) |
6f97dba0 AL |
387 | { |
388 | CharDriverState *chr; | |
389 | ||
db39fcf1 | 390 | chr = qemu_chr_alloc(); |
6f97dba0 | 391 | chr->chr_write = null_chr_write; |
bd5c51ee | 392 | chr->explicit_be_open = true; |
1f51470d | 393 | return chr; |
6f97dba0 AL |
394 | } |
395 | ||
396 | /* MUX driver for serial I/O splitting */ | |
6f97dba0 AL |
397 | #define MAX_MUX 4 |
398 | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ | |
399 | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) | |
400 | typedef struct { | |
7b27a769 | 401 | IOCanReadHandler *chr_can_read[MAX_MUX]; |
6f97dba0 AL |
402 | IOReadHandler *chr_read[MAX_MUX]; |
403 | IOEventHandler *chr_event[MAX_MUX]; | |
404 | void *ext_opaque[MAX_MUX]; | |
405 | CharDriverState *drv; | |
799f1f23 | 406 | int focus; |
6f97dba0 AL |
407 | int mux_cnt; |
408 | int term_got_escape; | |
409 | int max_size; | |
a80bf99f AL |
410 | /* Intermediate input buffer allows to catch escape sequences even if the |
411 | currently active device is not accepting any input - but only until it | |
412 | is full as well. */ | |
413 | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; | |
414 | int prod[MAX_MUX]; | |
415 | int cons[MAX_MUX]; | |
2d22959d | 416 | int timestamps; |
9005b2a7 PB |
417 | |
418 | /* Protected by the CharDriverState chr_write_lock. */ | |
4ab312f7 | 419 | int linestart; |
2d22959d | 420 | int64_t timestamps_start; |
6f97dba0 AL |
421 | } MuxDriver; |
422 | ||
423 | ||
9005b2a7 | 424 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
425 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
426 | { | |
427 | MuxDriver *d = chr->opaque; | |
428 | int ret; | |
2d22959d | 429 | if (!d->timestamps) { |
6975b713 | 430 | ret = qemu_chr_fe_write(d->drv, buf, len); |
6f97dba0 AL |
431 | } else { |
432 | int i; | |
433 | ||
434 | ret = 0; | |
4ab312f7 JK |
435 | for (i = 0; i < len; i++) { |
436 | if (d->linestart) { | |
6f97dba0 AL |
437 | char buf1[64]; |
438 | int64_t ti; | |
439 | int secs; | |
440 | ||
bc72ad67 | 441 | ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
2d22959d JK |
442 | if (d->timestamps_start == -1) |
443 | d->timestamps_start = ti; | |
444 | ti -= d->timestamps_start; | |
a4bb1db8 | 445 | secs = ti / 1000; |
6f97dba0 AL |
446 | snprintf(buf1, sizeof(buf1), |
447 | "[%02d:%02d:%02d.%03d] ", | |
448 | secs / 3600, | |
449 | (secs / 60) % 60, | |
450 | secs % 60, | |
a4bb1db8 | 451 | (int)(ti % 1000)); |
6975b713 | 452 | qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
4ab312f7 JK |
453 | d->linestart = 0; |
454 | } | |
6975b713 | 455 | ret += qemu_chr_fe_write(d->drv, buf+i, 1); |
4ab312f7 JK |
456 | if (buf[i] == '\n') { |
457 | d->linestart = 1; | |
6f97dba0 AL |
458 | } |
459 | } | |
460 | } | |
461 | return ret; | |
462 | } | |
463 | ||
464 | static const char * const mux_help[] = { | |
465 | "% h print this help\n\r", | |
466 | "% x exit emulator\n\r", | |
467 | "% s save disk data back to file (if -snapshot)\n\r", | |
86d10328 | 468 | "% t toggle console timestamps\n\r", |
6f97dba0 AL |
469 | "% b send break (magic sysrq)\n\r", |
470 | "% c switch between console and monitor\n\r", | |
471 | "% % sends %\n\r", | |
472 | NULL | |
473 | }; | |
474 | ||
475 | int term_escape_char = 0x01; /* ctrl-a is used for escape */ | |
476 | static void mux_print_help(CharDriverState *chr) | |
477 | { | |
478 | int i, j; | |
479 | char ebuf[15] = "Escape-Char"; | |
480 | char cbuf[50] = "\n\r"; | |
481 | ||
482 | if (term_escape_char > 0 && term_escape_char < 26) { | |
483 | snprintf(cbuf, sizeof(cbuf), "\n\r"); | |
484 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); | |
485 | } else { | |
486 | snprintf(cbuf, sizeof(cbuf), | |
487 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", | |
488 | term_escape_char); | |
489 | } | |
6975b713 | 490 | qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
6f97dba0 AL |
491 | for (i = 0; mux_help[i] != NULL; i++) { |
492 | for (j=0; mux_help[i][j] != '\0'; j++) { | |
493 | if (mux_help[i][j] == '%') | |
6975b713 | 494 | qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
6f97dba0 | 495 | else |
6975b713 | 496 | qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1); |
6f97dba0 AL |
497 | } |
498 | } | |
499 | } | |
500 | ||
2724b180 AL |
501 | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
502 | { | |
503 | if (d->chr_event[mux_nr]) | |
504 | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); | |
505 | } | |
506 | ||
6f97dba0 AL |
507 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
508 | { | |
509 | if (d->term_got_escape) { | |
510 | d->term_got_escape = 0; | |
511 | if (ch == term_escape_char) | |
512 | goto send_char; | |
513 | switch(ch) { | |
514 | case '?': | |
515 | case 'h': | |
516 | mux_print_help(chr); | |
517 | break; | |
518 | case 'x': | |
519 | { | |
520 | const char *term = "QEMU: Terminated\n\r"; | |
6975b713 | 521 | qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term)); |
6f97dba0 AL |
522 | exit(0); |
523 | break; | |
524 | } | |
525 | case 's': | |
6ab4b5ab | 526 | bdrv_commit_all(); |
6f97dba0 AL |
527 | break; |
528 | case 'b': | |
a425d23f | 529 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
6f97dba0 AL |
530 | break; |
531 | case 'c': | |
532 | /* Switch to the next registered device */ | |
799f1f23 GH |
533 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
534 | d->focus++; | |
535 | if (d->focus >= d->mux_cnt) | |
536 | d->focus = 0; | |
537 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); | |
6f97dba0 | 538 | break; |
2d22959d JK |
539 | case 't': |
540 | d->timestamps = !d->timestamps; | |
541 | d->timestamps_start = -1; | |
4ab312f7 | 542 | d->linestart = 0; |
2d22959d | 543 | break; |
6f97dba0 AL |
544 | } |
545 | } else if (ch == term_escape_char) { | |
546 | d->term_got_escape = 1; | |
547 | } else { | |
548 | send_char: | |
549 | return 1; | |
550 | } | |
551 | return 0; | |
552 | } | |
553 | ||
554 | static void mux_chr_accept_input(CharDriverState *chr) | |
555 | { | |
6f97dba0 | 556 | MuxDriver *d = chr->opaque; |
799f1f23 | 557 | int m = d->focus; |
6f97dba0 | 558 | |
a80bf99f | 559 | while (d->prod[m] != d->cons[m] && |
6f97dba0 AL |
560 | d->chr_can_read[m] && |
561 | d->chr_can_read[m](d->ext_opaque[m])) { | |
562 | d->chr_read[m](d->ext_opaque[m], | |
a80bf99f | 563 | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); |
6f97dba0 AL |
564 | } |
565 | } | |
566 | ||
567 | static int mux_chr_can_read(void *opaque) | |
568 | { | |
569 | CharDriverState *chr = opaque; | |
570 | MuxDriver *d = chr->opaque; | |
799f1f23 | 571 | int m = d->focus; |
6f97dba0 | 572 | |
a80bf99f | 573 | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) |
6f97dba0 | 574 | return 1; |
a80bf99f AL |
575 | if (d->chr_can_read[m]) |
576 | return d->chr_can_read[m](d->ext_opaque[m]); | |
6f97dba0 AL |
577 | return 0; |
578 | } | |
579 | ||
580 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) | |
581 | { | |
582 | CharDriverState *chr = opaque; | |
583 | MuxDriver *d = chr->opaque; | |
799f1f23 | 584 | int m = d->focus; |
6f97dba0 AL |
585 | int i; |
586 | ||
587 | mux_chr_accept_input (opaque); | |
588 | ||
589 | for(i = 0; i < size; i++) | |
590 | if (mux_proc_byte(chr, d, buf[i])) { | |
a80bf99f | 591 | if (d->prod[m] == d->cons[m] && |
6f97dba0 AL |
592 | d->chr_can_read[m] && |
593 | d->chr_can_read[m](d->ext_opaque[m])) | |
594 | d->chr_read[m](d->ext_opaque[m], &buf[i], 1); | |
595 | else | |
a80bf99f | 596 | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
6f97dba0 AL |
597 | } |
598 | } | |
599 | ||
600 | static void mux_chr_event(void *opaque, int event) | |
601 | { | |
602 | CharDriverState *chr = opaque; | |
603 | MuxDriver *d = chr->opaque; | |
604 | int i; | |
605 | ||
606 | /* Send the event to all registered listeners */ | |
607 | for (i = 0; i < d->mux_cnt; i++) | |
2724b180 | 608 | mux_chr_send_event(d, i, event); |
6f97dba0 AL |
609 | } |
610 | ||
611 | static void mux_chr_update_read_handler(CharDriverState *chr) | |
612 | { | |
613 | MuxDriver *d = chr->opaque; | |
614 | ||
615 | if (d->mux_cnt >= MAX_MUX) { | |
616 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); | |
617 | return; | |
618 | } | |
619 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; | |
620 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; | |
621 | d->chr_read[d->mux_cnt] = chr->chr_read; | |
622 | d->chr_event[d->mux_cnt] = chr->chr_event; | |
623 | /* Fix up the real driver with mux routines */ | |
624 | if (d->mux_cnt == 0) { | |
625 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, | |
626 | mux_chr_event, chr); | |
627 | } | |
799f1f23 GH |
628 | if (d->focus != -1) { |
629 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); | |
a7aec5da | 630 | } |
799f1f23 | 631 | d->focus = d->mux_cnt; |
6f97dba0 | 632 | d->mux_cnt++; |
799f1f23 | 633 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
6f97dba0 AL |
634 | } |
635 | ||
7b7ab18d MR |
636 | static bool muxes_realized; |
637 | ||
638 | /** | |
639 | * Called after processing of default and command-line-specified | |
640 | * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached | |
641 | * to a mux chardev. This is done here to ensure that | |
642 | * output/prompts/banners are only displayed for the FE that has | |
643 | * focus when initial command-line processing/machine init is | |
644 | * completed. | |
645 | * | |
646 | * After this point, any new FE attached to any new or existing | |
647 | * mux will receive CHR_EVENT_OPENED notifications for the BE | |
648 | * immediately. | |
649 | */ | |
650 | static void muxes_realize_done(Notifier *notifier, void *unused) | |
651 | { | |
652 | CharDriverState *chr; | |
653 | ||
654 | QTAILQ_FOREACH(chr, &chardevs, next) { | |
655 | if (chr->is_mux) { | |
656 | MuxDriver *d = chr->opaque; | |
657 | int i; | |
658 | ||
659 | /* send OPENED to all already-attached FEs */ | |
660 | for (i = 0; i < d->mux_cnt; i++) { | |
661 | mux_chr_send_event(d, i, CHR_EVENT_OPENED); | |
662 | } | |
663 | /* mark mux as OPENED so any new FEs will immediately receive | |
664 | * OPENED event | |
665 | */ | |
666 | qemu_chr_be_generic_open(chr); | |
667 | } | |
668 | } | |
669 | muxes_realized = true; | |
670 | } | |
671 | ||
672 | static Notifier muxes_realize_notify = { | |
673 | .notify = muxes_realize_done, | |
674 | }; | |
675 | ||
3f0838ab KB |
676 | static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond) |
677 | { | |
678 | MuxDriver *d = s->opaque; | |
679 | return d->drv->chr_add_watch(d->drv, cond); | |
680 | } | |
681 | ||
6f97dba0 AL |
682 | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv) |
683 | { | |
684 | CharDriverState *chr; | |
685 | MuxDriver *d; | |
686 | ||
db39fcf1 | 687 | chr = qemu_chr_alloc(); |
7267c094 | 688 | d = g_malloc0(sizeof(MuxDriver)); |
6f97dba0 AL |
689 | |
690 | chr->opaque = d; | |
691 | d->drv = drv; | |
799f1f23 | 692 | d->focus = -1; |
6f97dba0 AL |
693 | chr->chr_write = mux_chr_write; |
694 | chr->chr_update_read_handler = mux_chr_update_read_handler; | |
695 | chr->chr_accept_input = mux_chr_accept_input; | |
7c32c4fe | 696 | /* Frontend guest-open / -close notification is not support with muxes */ |
574b711a | 697 | chr->chr_set_fe_open = NULL; |
3f0838ab KB |
698 | if (drv->chr_add_watch) { |
699 | chr->chr_add_watch = mux_chr_add_watch; | |
700 | } | |
7b7ab18d MR |
701 | /* only default to opened state if we've realized the initial |
702 | * set of muxes | |
703 | */ | |
704 | chr->explicit_be_open = muxes_realized ? 0 : 1; | |
705 | chr->is_mux = 1; | |
73cdf3f2 | 706 | |
6f97dba0 AL |
707 | return chr; |
708 | } | |
709 | ||
710 | ||
711 | #ifdef _WIN32 | |
d247d25f | 712 | int send_all(int fd, const void *buf, int len1) |
6f97dba0 AL |
713 | { |
714 | int ret, len; | |
715 | ||
716 | len = len1; | |
717 | while (len > 0) { | |
718 | ret = send(fd, buf, len, 0); | |
719 | if (ret < 0) { | |
6f97dba0 AL |
720 | errno = WSAGetLastError(); |
721 | if (errno != WSAEWOULDBLOCK) { | |
722 | return -1; | |
723 | } | |
724 | } else if (ret == 0) { | |
725 | break; | |
726 | } else { | |
727 | buf += ret; | |
728 | len -= ret; | |
729 | } | |
730 | } | |
731 | return len1 - len; | |
732 | } | |
733 | ||
734 | #else | |
735 | ||
5fc9cfed | 736 | int send_all(int fd, const void *_buf, int len1) |
6f97dba0 AL |
737 | { |
738 | int ret, len; | |
5fc9cfed | 739 | const uint8_t *buf = _buf; |
6f97dba0 AL |
740 | |
741 | len = len1; | |
742 | while (len > 0) { | |
743 | ret = write(fd, buf, len); | |
744 | if (ret < 0) { | |
745 | if (errno != EINTR && errno != EAGAIN) | |
746 | return -1; | |
747 | } else if (ret == 0) { | |
748 | break; | |
749 | } else { | |
750 | buf += ret; | |
751 | len -= ret; | |
752 | } | |
753 | } | |
754 | return len1 - len; | |
755 | } | |
4549a8b7 SB |
756 | |
757 | int recv_all(int fd, void *_buf, int len1, bool single_read) | |
758 | { | |
759 | int ret, len; | |
760 | uint8_t *buf = _buf; | |
761 | ||
762 | len = len1; | |
763 | while ((len > 0) && (ret = read(fd, buf, len)) != 0) { | |
764 | if (ret < 0) { | |
765 | if (errno != EINTR && errno != EAGAIN) { | |
766 | return -1; | |
767 | } | |
768 | continue; | |
769 | } else { | |
770 | if (single_read) { | |
771 | return ret; | |
772 | } | |
773 | buf += ret; | |
774 | len -= ret; | |
775 | } | |
776 | } | |
777 | return len1 - len; | |
778 | } | |
779 | ||
6f97dba0 AL |
780 | #endif /* !_WIN32 */ |
781 | ||
96c63847 AL |
782 | typedef struct IOWatchPoll |
783 | { | |
d185c094 PB |
784 | GSource parent; |
785 | ||
1e885b25 | 786 | GIOChannel *channel; |
96c63847 | 787 | GSource *src; |
96c63847 AL |
788 | |
789 | IOCanReadHandler *fd_can_read; | |
1e885b25 | 790 | GSourceFunc fd_read; |
96c63847 | 791 | void *opaque; |
96c63847 AL |
792 | } IOWatchPoll; |
793 | ||
96c63847 AL |
794 | static IOWatchPoll *io_watch_poll_from_source(GSource *source) |
795 | { | |
d185c094 | 796 | return container_of(source, IOWatchPoll, parent); |
96c63847 AL |
797 | } |
798 | ||
799 | static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_) | |
800 | { | |
801 | IOWatchPoll *iwp = io_watch_poll_from_source(source); | |
d185c094 | 802 | bool now_active = iwp->fd_can_read(iwp->opaque) > 0; |
1e885b25 | 803 | bool was_active = iwp->src != NULL; |
d185c094 | 804 | if (was_active == now_active) { |
96c63847 AL |
805 | return FALSE; |
806 | } | |
807 | ||
d185c094 | 808 | if (now_active) { |
1e885b25 PB |
809 | iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP); |
810 | g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL); | |
d185c094 PB |
811 | g_source_attach(iwp->src, NULL); |
812 | } else { | |
1e885b25 PB |
813 | g_source_destroy(iwp->src); |
814 | g_source_unref(iwp->src); | |
815 | iwp->src = NULL; | |
d185c094 PB |
816 | } |
817 | return FALSE; | |
96c63847 AL |
818 | } |
819 | ||
820 | static gboolean io_watch_poll_check(GSource *source) | |
821 | { | |
d185c094 | 822 | return FALSE; |
96c63847 AL |
823 | } |
824 | ||
825 | static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback, | |
826 | gpointer user_data) | |
827 | { | |
d185c094 | 828 | abort(); |
96c63847 AL |
829 | } |
830 | ||
831 | static void io_watch_poll_finalize(GSource *source) | |
832 | { | |
2b316774 PB |
833 | /* Due to a glib bug, removing the last reference to a source |
834 | * inside a finalize callback causes recursive locking (and a | |
835 | * deadlock). This is not a problem inside other callbacks, | |
836 | * including dispatch callbacks, so we call io_remove_watch_poll | |
837 | * to remove this source. At this point, iwp->src must | |
838 | * be NULL, or we would leak it. | |
839 | * | |
840 | * This would be solved much more elegantly by child sources, | |
841 | * but we support older glib versions that do not have them. | |
842 | */ | |
96c63847 | 843 | IOWatchPoll *iwp = io_watch_poll_from_source(source); |
2b316774 | 844 | assert(iwp->src == NULL); |
96c63847 AL |
845 | } |
846 | ||
847 | static GSourceFuncs io_watch_poll_funcs = { | |
848 | .prepare = io_watch_poll_prepare, | |
849 | .check = io_watch_poll_check, | |
850 | .dispatch = io_watch_poll_dispatch, | |
851 | .finalize = io_watch_poll_finalize, | |
852 | }; | |
853 | ||
854 | /* Can only be used for read */ | |
855 | static guint io_add_watch_poll(GIOChannel *channel, | |
856 | IOCanReadHandler *fd_can_read, | |
857 | GIOFunc fd_read, | |
858 | gpointer user_data) | |
859 | { | |
860 | IOWatchPoll *iwp; | |
0ca5aa4f | 861 | int tag; |
96c63847 | 862 | |
d185c094 | 863 | iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll)); |
96c63847 AL |
864 | iwp->fd_can_read = fd_can_read; |
865 | iwp->opaque = user_data; | |
1e885b25 PB |
866 | iwp->channel = channel; |
867 | iwp->fd_read = (GSourceFunc) fd_read; | |
868 | iwp->src = NULL; | |
96c63847 | 869 | |
0ca5aa4f PB |
870 | tag = g_source_attach(&iwp->parent, NULL); |
871 | g_source_unref(&iwp->parent); | |
872 | return tag; | |
96c63847 AL |
873 | } |
874 | ||
2b316774 PB |
875 | static void io_remove_watch_poll(guint tag) |
876 | { | |
877 | GSource *source; | |
878 | IOWatchPoll *iwp; | |
879 | ||
880 | g_return_if_fail (tag > 0); | |
881 | ||
882 | source = g_main_context_find_source_by_id(NULL, tag); | |
883 | g_return_if_fail (source != NULL); | |
884 | ||
885 | iwp = io_watch_poll_from_source(source); | |
886 | if (iwp->src) { | |
887 | g_source_destroy(iwp->src); | |
888 | g_source_unref(iwp->src); | |
889 | iwp->src = NULL; | |
890 | } | |
891 | g_source_destroy(&iwp->parent); | |
892 | } | |
893 | ||
26da70c7 AS |
894 | static void remove_fd_in_watch(CharDriverState *chr) |
895 | { | |
896 | if (chr->fd_in_tag) { | |
897 | io_remove_watch_poll(chr->fd_in_tag); | |
898 | chr->fd_in_tag = 0; | |
899 | } | |
900 | } | |
901 | ||
44ab9ed4 | 902 | #ifndef _WIN32 |
96c63847 AL |
903 | static GIOChannel *io_channel_from_fd(int fd) |
904 | { | |
905 | GIOChannel *chan; | |
906 | ||
907 | if (fd == -1) { | |
908 | return NULL; | |
909 | } | |
910 | ||
911 | chan = g_io_channel_unix_new(fd); | |
912 | ||
913 | g_io_channel_set_encoding(chan, NULL, NULL); | |
914 | g_io_channel_set_buffered(chan, FALSE); | |
915 | ||
916 | return chan; | |
917 | } | |
44ab9ed4 | 918 | #endif |
96c63847 | 919 | |
76a9644b AL |
920 | static GIOChannel *io_channel_from_socket(int fd) |
921 | { | |
922 | GIOChannel *chan; | |
923 | ||
924 | if (fd == -1) { | |
925 | return NULL; | |
926 | } | |
927 | ||
928 | #ifdef _WIN32 | |
929 | chan = g_io_channel_win32_new_socket(fd); | |
930 | #else | |
931 | chan = g_io_channel_unix_new(fd); | |
932 | #endif | |
933 | ||
934 | g_io_channel_set_encoding(chan, NULL, NULL); | |
935 | g_io_channel_set_buffered(chan, FALSE); | |
936 | ||
937 | return chan; | |
938 | } | |
939 | ||
684a096e | 940 | static int io_channel_send(GIOChannel *fd, const void *buf, size_t len) |
96c63847 | 941 | { |
ac8c26f6 LE |
942 | size_t offset = 0; |
943 | GIOStatus status = G_IO_STATUS_NORMAL; | |
96c63847 | 944 | |
ac8c26f6 LE |
945 | while (offset < len && status == G_IO_STATUS_NORMAL) { |
946 | gsize bytes_written = 0; | |
684a096e AL |
947 | |
948 | status = g_io_channel_write_chars(fd, buf + offset, len - offset, | |
96c63847 | 949 | &bytes_written, NULL); |
684a096e | 950 | offset += bytes_written; |
96c63847 | 951 | } |
684a096e | 952 | |
ac8c26f6 LE |
953 | if (offset > 0) { |
954 | return offset; | |
955 | } | |
956 | switch (status) { | |
957 | case G_IO_STATUS_NORMAL: | |
958 | g_assert(len == 0); | |
959 | return 0; | |
960 | case G_IO_STATUS_AGAIN: | |
961 | errno = EAGAIN; | |
962 | return -1; | |
963 | default: | |
964 | break; | |
965 | } | |
966 | errno = EINVAL; | |
967 | return -1; | |
96c63847 | 968 | } |
96c63847 | 969 | |
44ab9ed4 BS |
970 | #ifndef _WIN32 |
971 | ||
a29753f8 AL |
972 | typedef struct FDCharDriver { |
973 | CharDriverState *chr; | |
974 | GIOChannel *fd_in, *fd_out; | |
6f97dba0 | 975 | int max_size; |
a29753f8 | 976 | QTAILQ_ENTRY(FDCharDriver) node; |
6f97dba0 AL |
977 | } FDCharDriver; |
978 | ||
9005b2a7 | 979 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
980 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
981 | { | |
982 | FDCharDriver *s = chr->opaque; | |
a29753f8 | 983 | |
684a096e | 984 | return io_channel_send(s->fd_out, buf, len); |
6f97dba0 AL |
985 | } |
986 | ||
a29753f8 | 987 | static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
988 | { |
989 | CharDriverState *chr = opaque; | |
990 | FDCharDriver *s = chr->opaque; | |
a29753f8 | 991 | int len; |
9bd7854e | 992 | uint8_t buf[READ_BUF_LEN]; |
a29753f8 AL |
993 | GIOStatus status; |
994 | gsize bytes_read; | |
6f97dba0 AL |
995 | |
996 | len = sizeof(buf); | |
a29753f8 | 997 | if (len > s->max_size) { |
6f97dba0 | 998 | len = s->max_size; |
a29753f8 AL |
999 | } |
1000 | if (len == 0) { | |
cdbf6e16 | 1001 | return TRUE; |
a29753f8 AL |
1002 | } |
1003 | ||
1004 | status = g_io_channel_read_chars(chan, (gchar *)buf, | |
1005 | len, &bytes_read, NULL); | |
1006 | if (status == G_IO_STATUS_EOF) { | |
26da70c7 | 1007 | remove_fd_in_watch(chr); |
a425d23f | 1008 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
a29753f8 | 1009 | return FALSE; |
6f97dba0 | 1010 | } |
a29753f8 AL |
1011 | if (status == G_IO_STATUS_NORMAL) { |
1012 | qemu_chr_be_write(chr, buf, bytes_read); | |
6f97dba0 | 1013 | } |
a29753f8 AL |
1014 | |
1015 | return TRUE; | |
1016 | } | |
1017 | ||
1018 | static int fd_chr_read_poll(void *opaque) | |
1019 | { | |
1020 | CharDriverState *chr = opaque; | |
1021 | FDCharDriver *s = chr->opaque; | |
1022 | ||
1023 | s->max_size = qemu_chr_be_can_write(chr); | |
1024 | return s->max_size; | |
6f97dba0 AL |
1025 | } |
1026 | ||
23673ca7 AL |
1027 | static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
1028 | { | |
1029 | FDCharDriver *s = chr->opaque; | |
1030 | return g_io_create_watch(s->fd_out, cond); | |
1031 | } | |
1032 | ||
6f97dba0 AL |
1033 | static void fd_chr_update_read_handler(CharDriverState *chr) |
1034 | { | |
1035 | FDCharDriver *s = chr->opaque; | |
1036 | ||
26da70c7 | 1037 | remove_fd_in_watch(chr); |
a29753f8 | 1038 | if (s->fd_in) { |
7ba9addc AS |
1039 | chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, |
1040 | fd_chr_read, chr); | |
6f97dba0 AL |
1041 | } |
1042 | } | |
1043 | ||
1044 | static void fd_chr_close(struct CharDriverState *chr) | |
1045 | { | |
1046 | FDCharDriver *s = chr->opaque; | |
1047 | ||
26da70c7 | 1048 | remove_fd_in_watch(chr); |
a29753f8 AL |
1049 | if (s->fd_in) { |
1050 | g_io_channel_unref(s->fd_in); | |
1051 | } | |
1052 | if (s->fd_out) { | |
1053 | g_io_channel_unref(s->fd_out); | |
6f97dba0 AL |
1054 | } |
1055 | ||
7267c094 | 1056 | g_free(s); |
a425d23f | 1057 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1058 | } |
1059 | ||
1060 | /* open a character device to a unix fd */ | |
1061 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) | |
1062 | { | |
1063 | CharDriverState *chr; | |
1064 | FDCharDriver *s; | |
1065 | ||
db39fcf1 | 1066 | chr = qemu_chr_alloc(); |
7267c094 | 1067 | s = g_malloc0(sizeof(FDCharDriver)); |
a29753f8 AL |
1068 | s->fd_in = io_channel_from_fd(fd_in); |
1069 | s->fd_out = io_channel_from_fd(fd_out); | |
4ff12bdb | 1070 | qemu_set_nonblock(fd_out); |
a29753f8 | 1071 | s->chr = chr; |
6f97dba0 | 1072 | chr->opaque = s; |
23673ca7 | 1073 | chr->chr_add_watch = fd_chr_add_watch; |
6f97dba0 AL |
1074 | chr->chr_write = fd_chr_write; |
1075 | chr->chr_update_read_handler = fd_chr_update_read_handler; | |
1076 | chr->chr_close = fd_chr_close; | |
1077 | ||
6f97dba0 AL |
1078 | return chr; |
1079 | } | |
1080 | ||
548cbb36 | 1081 | static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts) |
6f97dba0 AL |
1082 | { |
1083 | int fd_in, fd_out; | |
9f781168 CM |
1084 | char filename_in[CHR_MAX_FILENAME_SIZE]; |
1085 | char filename_out[CHR_MAX_FILENAME_SIZE]; | |
548cbb36 | 1086 | const char *filename = opts->device; |
7d31544f GH |
1087 | |
1088 | if (filename == NULL) { | |
1089 | fprintf(stderr, "chardev: pipe: no filename given\n"); | |
1f51470d | 1090 | return NULL; |
7d31544f | 1091 | } |
6f97dba0 | 1092 | |
9f781168 CM |
1093 | snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename); |
1094 | snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename); | |
40ff6d7e KW |
1095 | TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); |
1096 | TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); | |
6f97dba0 AL |
1097 | if (fd_in < 0 || fd_out < 0) { |
1098 | if (fd_in >= 0) | |
1099 | close(fd_in); | |
1100 | if (fd_out >= 0) | |
1101 | close(fd_out); | |
b181e047 | 1102 | TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); |
a89dd6c3 | 1103 | if (fd_in < 0) { |
1f51470d | 1104 | return NULL; |
a89dd6c3 | 1105 | } |
6f97dba0 | 1106 | } |
1f51470d | 1107 | return qemu_chr_open_fd(fd_in, fd_out); |
6f97dba0 AL |
1108 | } |
1109 | ||
6f97dba0 AL |
1110 | /* init terminal so that we can grab keys */ |
1111 | static struct termios oldtty; | |
1112 | static int old_fd0_flags; | |
c88930a6 | 1113 | static bool stdio_in_use; |
bb002513 | 1114 | static bool stdio_allow_signal; |
e76d4420 GH |
1115 | static bool stdio_echo_state; |
1116 | ||
1117 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo); | |
6f97dba0 AL |
1118 | |
1119 | static void term_exit(void) | |
1120 | { | |
1121 | tcsetattr (0, TCSANOW, &oldtty); | |
1122 | fcntl(0, F_SETFL, old_fd0_flags); | |
1123 | } | |
1124 | ||
e76d4420 GH |
1125 | static void term_stdio_handler(int sig) |
1126 | { | |
1127 | /* restore echo after resume from suspend. */ | |
1128 | qemu_chr_set_echo_stdio(NULL, stdio_echo_state); | |
1129 | } | |
1130 | ||
bb002513 | 1131 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo) |
6f97dba0 AL |
1132 | { |
1133 | struct termios tty; | |
1134 | ||
e76d4420 | 1135 | stdio_echo_state = echo; |
0369364b | 1136 | tty = oldtty; |
bb002513 PB |
1137 | if (!echo) { |
1138 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
6f97dba0 | 1139 | |INLCR|IGNCR|ICRNL|IXON); |
bb002513 PB |
1140 | tty.c_oflag |= OPOST; |
1141 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); | |
1142 | tty.c_cflag &= ~(CSIZE|PARENB); | |
1143 | tty.c_cflag |= CS8; | |
1144 | tty.c_cc[VMIN] = 1; | |
1145 | tty.c_cc[VTIME] = 0; | |
1146 | } | |
bb002513 | 1147 | if (!stdio_allow_signal) |
6f97dba0 | 1148 | tty.c_lflag &= ~ISIG; |
6f97dba0 AL |
1149 | |
1150 | tcsetattr (0, TCSANOW, &tty); | |
6f97dba0 AL |
1151 | } |
1152 | ||
1153 | static void qemu_chr_close_stdio(struct CharDriverState *chr) | |
1154 | { | |
1155 | term_exit(); | |
6f97dba0 AL |
1156 | fd_chr_close(chr); |
1157 | } | |
1158 | ||
7c358031 | 1159 | static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts) |
6f97dba0 AL |
1160 | { |
1161 | CharDriverState *chr; | |
e76d4420 | 1162 | struct sigaction act; |
6f97dba0 | 1163 | |
ab51b1d5 MT |
1164 | if (is_daemonized()) { |
1165 | error_report("cannot use stdio with -daemonize"); | |
1166 | return NULL; | |
1167 | } | |
c88930a6 LL |
1168 | |
1169 | if (stdio_in_use) { | |
1170 | error_report("cannot use stdio by multiple character devices"); | |
1171 | exit(1); | |
1172 | } | |
1173 | ||
1174 | stdio_in_use = true; | |
ed7a1540 | 1175 | old_fd0_flags = fcntl(0, F_GETFL); |
c88930a6 | 1176 | tcgetattr(0, &oldtty); |
4ff12bdb | 1177 | qemu_set_nonblock(0); |
ed7a1540 | 1178 | atexit(term_exit); |
0369364b | 1179 | |
e76d4420 GH |
1180 | memset(&act, 0, sizeof(act)); |
1181 | act.sa_handler = term_stdio_handler; | |
1182 | sigaction(SIGCONT, &act, NULL); | |
1183 | ||
6f97dba0 AL |
1184 | chr = qemu_chr_open_fd(0, 1); |
1185 | chr->chr_close = qemu_chr_close_stdio; | |
bb002513 | 1186 | chr->chr_set_echo = qemu_chr_set_echo_stdio; |
7c358031 GH |
1187 | if (opts->has_signal) { |
1188 | stdio_allow_signal = opts->signal; | |
1189 | } | |
15f31519 | 1190 | qemu_chr_fe_set_echo(chr, false); |
6f97dba0 | 1191 | |
1f51470d | 1192 | return chr; |
6f97dba0 AL |
1193 | } |
1194 | ||
6f97dba0 | 1195 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ |
a167ba50 AJ |
1196 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
1197 | || defined(__GLIBC__) | |
6f97dba0 | 1198 | |
e551498e GH |
1199 | #define HAVE_CHARDEV_TTY 1 |
1200 | ||
6f97dba0 | 1201 | typedef struct { |
093d3a20 | 1202 | GIOChannel *fd; |
6f97dba0 | 1203 | int read_bytes; |
9005b2a7 PB |
1204 | |
1205 | /* Protected by the CharDriverState chr_write_lock. */ | |
1206 | int connected; | |
8aa33caf | 1207 | guint timer_tag; |
7b3621f4 | 1208 | guint open_tag; |
6f97dba0 AL |
1209 | } PtyCharDriver; |
1210 | ||
9005b2a7 | 1211 | static void pty_chr_update_read_handler_locked(CharDriverState *chr); |
6f97dba0 AL |
1212 | static void pty_chr_state(CharDriverState *chr, int connected); |
1213 | ||
8aa33caf AL |
1214 | static gboolean pty_chr_timer(gpointer opaque) |
1215 | { | |
1216 | struct CharDriverState *chr = opaque; | |
1217 | PtyCharDriver *s = chr->opaque; | |
1218 | ||
9005b2a7 | 1219 | qemu_mutex_lock(&chr->chr_write_lock); |
79f20075 | 1220 | s->timer_tag = 0; |
7b3621f4 | 1221 | s->open_tag = 0; |
b0d768c3 GH |
1222 | if (!s->connected) { |
1223 | /* Next poll ... */ | |
9005b2a7 | 1224 | pty_chr_update_read_handler_locked(chr); |
b0d768c3 | 1225 | } |
9005b2a7 | 1226 | qemu_mutex_unlock(&chr->chr_write_lock); |
8aa33caf AL |
1227 | return FALSE; |
1228 | } | |
1229 | ||
9005b2a7 | 1230 | /* Called with chr_write_lock held. */ |
8aa33caf AL |
1231 | static void pty_chr_rearm_timer(CharDriverState *chr, int ms) |
1232 | { | |
1233 | PtyCharDriver *s = chr->opaque; | |
1234 | ||
1235 | if (s->timer_tag) { | |
1236 | g_source_remove(s->timer_tag); | |
1237 | s->timer_tag = 0; | |
1238 | } | |
1239 | ||
1240 | if (ms == 1000) { | |
1241 | s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr); | |
1242 | } else { | |
1243 | s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr); | |
1244 | } | |
1245 | } | |
1246 | ||
9005b2a7 PB |
1247 | /* Called with chr_write_lock held. */ |
1248 | static void pty_chr_update_read_handler_locked(CharDriverState *chr) | |
1bb7fe72 PB |
1249 | { |
1250 | PtyCharDriver *s = chr->opaque; | |
1251 | GPollFD pfd; | |
1252 | ||
1253 | pfd.fd = g_io_channel_unix_get_fd(s->fd); | |
1254 | pfd.events = G_IO_OUT; | |
1255 | pfd.revents = 0; | |
1256 | g_poll(&pfd, 1, 0); | |
1257 | if (pfd.revents & G_IO_HUP) { | |
1258 | pty_chr_state(chr, 0); | |
1259 | } else { | |
1260 | pty_chr_state(chr, 1); | |
1261 | } | |
1262 | } | |
1263 | ||
9005b2a7 PB |
1264 | static void pty_chr_update_read_handler(CharDriverState *chr) |
1265 | { | |
1266 | qemu_mutex_lock(&chr->chr_write_lock); | |
1267 | pty_chr_update_read_handler_locked(chr); | |
1268 | qemu_mutex_unlock(&chr->chr_write_lock); | |
1269 | } | |
1270 | ||
1271 | /* Called with chr_write_lock held. */ | |
6f97dba0 AL |
1272 | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1273 | { | |
1274 | PtyCharDriver *s = chr->opaque; | |
1275 | ||
1276 | if (!s->connected) { | |
1277 | /* guest sends data, check for (re-)connect */ | |
9005b2a7 | 1278 | pty_chr_update_read_handler_locked(chr); |
cf7330c7 ST |
1279 | if (!s->connected) { |
1280 | return 0; | |
1281 | } | |
6f97dba0 | 1282 | } |
684a096e | 1283 | return io_channel_send(s->fd, buf, len); |
6f97dba0 AL |
1284 | } |
1285 | ||
e6a87ed8 AL |
1286 | static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
1287 | { | |
1288 | PtyCharDriver *s = chr->opaque; | |
62c339c5 PB |
1289 | if (!s->connected) { |
1290 | return NULL; | |
1291 | } | |
e6a87ed8 AL |
1292 | return g_io_create_watch(s->fd, cond); |
1293 | } | |
1294 | ||
6f97dba0 AL |
1295 | static int pty_chr_read_poll(void *opaque) |
1296 | { | |
1297 | CharDriverState *chr = opaque; | |
1298 | PtyCharDriver *s = chr->opaque; | |
1299 | ||
909cda12 | 1300 | s->read_bytes = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
1301 | return s->read_bytes; |
1302 | } | |
1303 | ||
093d3a20 | 1304 | static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
1305 | { |
1306 | CharDriverState *chr = opaque; | |
1307 | PtyCharDriver *s = chr->opaque; | |
093d3a20 | 1308 | gsize size, len; |
9bd7854e | 1309 | uint8_t buf[READ_BUF_LEN]; |
093d3a20 | 1310 | GIOStatus status; |
6f97dba0 AL |
1311 | |
1312 | len = sizeof(buf); | |
1313 | if (len > s->read_bytes) | |
1314 | len = s->read_bytes; | |
cdbf6e16 PB |
1315 | if (len == 0) { |
1316 | return TRUE; | |
1317 | } | |
093d3a20 AL |
1318 | status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL); |
1319 | if (status != G_IO_STATUS_NORMAL) { | |
6f97dba0 | 1320 | pty_chr_state(chr, 0); |
093d3a20 AL |
1321 | return FALSE; |
1322 | } else { | |
6f97dba0 | 1323 | pty_chr_state(chr, 1); |
fa5efccb | 1324 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 | 1325 | } |
093d3a20 | 1326 | return TRUE; |
6f97dba0 AL |
1327 | } |
1328 | ||
7b3621f4 PB |
1329 | static gboolean qemu_chr_be_generic_open_func(gpointer opaque) |
1330 | { | |
1331 | CharDriverState *chr = opaque; | |
1332 | PtyCharDriver *s = chr->opaque; | |
1333 | ||
1334 | s->open_tag = 0; | |
1335 | qemu_chr_be_generic_open(chr); | |
1336 | return FALSE; | |
1337 | } | |
1338 | ||
9005b2a7 | 1339 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
1340 | static void pty_chr_state(CharDriverState *chr, int connected) |
1341 | { | |
1342 | PtyCharDriver *s = chr->opaque; | |
1343 | ||
1344 | if (!connected) { | |
7b3621f4 PB |
1345 | if (s->open_tag) { |
1346 | g_source_remove(s->open_tag); | |
1347 | s->open_tag = 0; | |
1348 | } | |
26da70c7 | 1349 | remove_fd_in_watch(chr); |
6f97dba0 | 1350 | s->connected = 0; |
6f97dba0 AL |
1351 | /* (re-)connect poll interval for idle guests: once per second. |
1352 | * We check more frequently in case the guests sends data to | |
1353 | * the virtual device linked to our pty. */ | |
8aa33caf | 1354 | pty_chr_rearm_timer(chr, 1000); |
6f97dba0 | 1355 | } else { |
85a67692 PB |
1356 | if (s->timer_tag) { |
1357 | g_source_remove(s->timer_tag); | |
1358 | s->timer_tag = 0; | |
1359 | } | |
1360 | if (!s->connected) { | |
7b3621f4 | 1361 | g_assert(s->open_tag == 0); |
85a67692 | 1362 | s->connected = 1; |
7b3621f4 | 1363 | s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr); |
ac1b84dd GH |
1364 | } |
1365 | if (!chr->fd_in_tag) { | |
7ba9addc AS |
1366 | chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, |
1367 | pty_chr_read, chr); | |
85a67692 | 1368 | } |
6f97dba0 AL |
1369 | } |
1370 | } | |
1371 | ||
6f97dba0 AL |
1372 | static void pty_chr_close(struct CharDriverState *chr) |
1373 | { | |
1374 | PtyCharDriver *s = chr->opaque; | |
093d3a20 | 1375 | int fd; |
6f97dba0 | 1376 | |
7b3621f4 PB |
1377 | qemu_mutex_lock(&chr->chr_write_lock); |
1378 | pty_chr_state(chr, 0); | |
093d3a20 AL |
1379 | fd = g_io_channel_unix_get_fd(s->fd); |
1380 | g_io_channel_unref(s->fd); | |
1381 | close(fd); | |
8aa33caf AL |
1382 | if (s->timer_tag) { |
1383 | g_source_remove(s->timer_tag); | |
910b6368 | 1384 | s->timer_tag = 0; |
8aa33caf | 1385 | } |
7b3621f4 | 1386 | qemu_mutex_unlock(&chr->chr_write_lock); |
7267c094 | 1387 | g_free(s); |
a425d23f | 1388 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1389 | } |
1390 | ||
e68c5958 GH |
1391 | static CharDriverState *qemu_chr_open_pty(const char *id, |
1392 | ChardevReturn *ret) | |
6f97dba0 AL |
1393 | { |
1394 | CharDriverState *chr; | |
1395 | PtyCharDriver *s; | |
e68c5958 | 1396 | int master_fd, slave_fd; |
6f97dba0 | 1397 | char pty_name[PATH_MAX]; |
6f97dba0 | 1398 | |
4efeabbb MT |
1399 | master_fd = qemu_openpty_raw(&slave_fd, pty_name); |
1400 | if (master_fd < 0) { | |
1f51470d | 1401 | return NULL; |
6f97dba0 AL |
1402 | } |
1403 | ||
6f97dba0 | 1404 | close(slave_fd); |
fac6688a | 1405 | qemu_set_nonblock(master_fd); |
6f97dba0 | 1406 | |
db39fcf1 | 1407 | chr = qemu_chr_alloc(); |
a4e26048 | 1408 | |
4efeabbb MT |
1409 | chr->filename = g_strdup_printf("pty:%s", pty_name); |
1410 | ret->pty = g_strdup(pty_name); | |
e68c5958 | 1411 | ret->has_pty = true; |
58650218 | 1412 | |
e68c5958 | 1413 | fprintf(stderr, "char device redirected to %s (label %s)\n", |
4efeabbb | 1414 | pty_name, id); |
6f97dba0 | 1415 | |
a4e26048 | 1416 | s = g_malloc0(sizeof(PtyCharDriver)); |
6f97dba0 AL |
1417 | chr->opaque = s; |
1418 | chr->chr_write = pty_chr_write; | |
1419 | chr->chr_update_read_handler = pty_chr_update_read_handler; | |
1420 | chr->chr_close = pty_chr_close; | |
e6a87ed8 | 1421 | chr->chr_add_watch = pty_chr_add_watch; |
bd5c51ee | 1422 | chr->explicit_be_open = true; |
6f97dba0 | 1423 | |
093d3a20 | 1424 | s->fd = io_channel_from_fd(master_fd); |
8aa33caf | 1425 | s->timer_tag = 0; |
6f97dba0 | 1426 | |
1f51470d | 1427 | return chr; |
6f97dba0 AL |
1428 | } |
1429 | ||
1430 | static void tty_serial_init(int fd, int speed, | |
1431 | int parity, int data_bits, int stop_bits) | |
1432 | { | |
1433 | struct termios tty; | |
1434 | speed_t spd; | |
1435 | ||
1436 | #if 0 | |
1437 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", | |
1438 | speed, parity, data_bits, stop_bits); | |
1439 | #endif | |
1440 | tcgetattr (fd, &tty); | |
1441 | ||
45eea13b SW |
1442 | #define check_speed(val) if (speed <= val) { spd = B##val; break; } |
1443 | speed = speed * 10 / 11; | |
1444 | do { | |
1445 | check_speed(50); | |
1446 | check_speed(75); | |
1447 | check_speed(110); | |
1448 | check_speed(134); | |
1449 | check_speed(150); | |
1450 | check_speed(200); | |
1451 | check_speed(300); | |
1452 | check_speed(600); | |
1453 | check_speed(1200); | |
1454 | check_speed(1800); | |
1455 | check_speed(2400); | |
1456 | check_speed(4800); | |
1457 | check_speed(9600); | |
1458 | check_speed(19200); | |
1459 | check_speed(38400); | |
1460 | /* Non-Posix values follow. They may be unsupported on some systems. */ | |
1461 | check_speed(57600); | |
1462 | check_speed(115200); | |
1463 | #ifdef B230400 | |
1464 | check_speed(230400); | |
1465 | #endif | |
1466 | #ifdef B460800 | |
1467 | check_speed(460800); | |
1468 | #endif | |
1469 | #ifdef B500000 | |
1470 | check_speed(500000); | |
1471 | #endif | |
1472 | #ifdef B576000 | |
1473 | check_speed(576000); | |
1474 | #endif | |
1475 | #ifdef B921600 | |
1476 | check_speed(921600); | |
1477 | #endif | |
1478 | #ifdef B1000000 | |
1479 | check_speed(1000000); | |
1480 | #endif | |
1481 | #ifdef B1152000 | |
1482 | check_speed(1152000); | |
1483 | #endif | |
1484 | #ifdef B1500000 | |
1485 | check_speed(1500000); | |
1486 | #endif | |
1487 | #ifdef B2000000 | |
1488 | check_speed(2000000); | |
1489 | #endif | |
1490 | #ifdef B2500000 | |
1491 | check_speed(2500000); | |
1492 | #endif | |
1493 | #ifdef B3000000 | |
1494 | check_speed(3000000); | |
1495 | #endif | |
1496 | #ifdef B3500000 | |
1497 | check_speed(3500000); | |
1498 | #endif | |
1499 | #ifdef B4000000 | |
1500 | check_speed(4000000); | |
1501 | #endif | |
6f97dba0 | 1502 | spd = B115200; |
45eea13b | 1503 | } while (0); |
6f97dba0 AL |
1504 | |
1505 | cfsetispeed(&tty, spd); | |
1506 | cfsetospeed(&tty, spd); | |
1507 | ||
1508 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
1509 | |INLCR|IGNCR|ICRNL|IXON); | |
1510 | tty.c_oflag |= OPOST; | |
1511 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); | |
1512 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); | |
1513 | switch(data_bits) { | |
1514 | default: | |
1515 | case 8: | |
1516 | tty.c_cflag |= CS8; | |
1517 | break; | |
1518 | case 7: | |
1519 | tty.c_cflag |= CS7; | |
1520 | break; | |
1521 | case 6: | |
1522 | tty.c_cflag |= CS6; | |
1523 | break; | |
1524 | case 5: | |
1525 | tty.c_cflag |= CS5; | |
1526 | break; | |
1527 | } | |
1528 | switch(parity) { | |
1529 | default: | |
1530 | case 'N': | |
1531 | break; | |
1532 | case 'E': | |
1533 | tty.c_cflag |= PARENB; | |
1534 | break; | |
1535 | case 'O': | |
1536 | tty.c_cflag |= PARENB | PARODD; | |
1537 | break; | |
1538 | } | |
1539 | if (stop_bits == 2) | |
1540 | tty.c_cflag |= CSTOPB; | |
1541 | ||
1542 | tcsetattr (fd, TCSANOW, &tty); | |
1543 | } | |
1544 | ||
1545 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) | |
1546 | { | |
1547 | FDCharDriver *s = chr->opaque; | |
1548 | ||
1549 | switch(cmd) { | |
1550 | case CHR_IOCTL_SERIAL_SET_PARAMS: | |
1551 | { | |
1552 | QEMUSerialSetParams *ssp = arg; | |
a29753f8 AL |
1553 | tty_serial_init(g_io_channel_unix_get_fd(s->fd_in), |
1554 | ssp->speed, ssp->parity, | |
6f97dba0 AL |
1555 | ssp->data_bits, ssp->stop_bits); |
1556 | } | |
1557 | break; | |
1558 | case CHR_IOCTL_SERIAL_SET_BREAK: | |
1559 | { | |
1560 | int enable = *(int *)arg; | |
a29753f8 AL |
1561 | if (enable) { |
1562 | tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1); | |
1563 | } | |
6f97dba0 AL |
1564 | } |
1565 | break; | |
1566 | case CHR_IOCTL_SERIAL_GET_TIOCM: | |
1567 | { | |
1568 | int sarg = 0; | |
1569 | int *targ = (int *)arg; | |
a29753f8 | 1570 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg); |
6f97dba0 | 1571 | *targ = 0; |
b4abdfa4 | 1572 | if (sarg & TIOCM_CTS) |
6f97dba0 | 1573 | *targ |= CHR_TIOCM_CTS; |
b4abdfa4 | 1574 | if (sarg & TIOCM_CAR) |
6f97dba0 | 1575 | *targ |= CHR_TIOCM_CAR; |
b4abdfa4 | 1576 | if (sarg & TIOCM_DSR) |
6f97dba0 | 1577 | *targ |= CHR_TIOCM_DSR; |
b4abdfa4 | 1578 | if (sarg & TIOCM_RI) |
6f97dba0 | 1579 | *targ |= CHR_TIOCM_RI; |
b4abdfa4 | 1580 | if (sarg & TIOCM_DTR) |
6f97dba0 | 1581 | *targ |= CHR_TIOCM_DTR; |
b4abdfa4 | 1582 | if (sarg & TIOCM_RTS) |
6f97dba0 AL |
1583 | *targ |= CHR_TIOCM_RTS; |
1584 | } | |
1585 | break; | |
1586 | case CHR_IOCTL_SERIAL_SET_TIOCM: | |
1587 | { | |
1588 | int sarg = *(int *)arg; | |
1589 | int targ = 0; | |
a29753f8 | 1590 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ); |
b4abdfa4 AJ |
1591 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR |
1592 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); | |
1593 | if (sarg & CHR_TIOCM_CTS) | |
1594 | targ |= TIOCM_CTS; | |
1595 | if (sarg & CHR_TIOCM_CAR) | |
1596 | targ |= TIOCM_CAR; | |
1597 | if (sarg & CHR_TIOCM_DSR) | |
1598 | targ |= TIOCM_DSR; | |
1599 | if (sarg & CHR_TIOCM_RI) | |
1600 | targ |= TIOCM_RI; | |
1601 | if (sarg & CHR_TIOCM_DTR) | |
6f97dba0 | 1602 | targ |= TIOCM_DTR; |
b4abdfa4 | 1603 | if (sarg & CHR_TIOCM_RTS) |
6f97dba0 | 1604 | targ |= TIOCM_RTS; |
a29753f8 | 1605 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ); |
6f97dba0 AL |
1606 | } |
1607 | break; | |
1608 | default: | |
1609 | return -ENOTSUP; | |
1610 | } | |
1611 | return 0; | |
1612 | } | |
1613 | ||
4266a134 DA |
1614 | static void qemu_chr_close_tty(CharDriverState *chr) |
1615 | { | |
1616 | FDCharDriver *s = chr->opaque; | |
1617 | int fd = -1; | |
1618 | ||
1619 | if (s) { | |
a29753f8 | 1620 | fd = g_io_channel_unix_get_fd(s->fd_in); |
4266a134 DA |
1621 | } |
1622 | ||
1623 | fd_chr_close(chr); | |
1624 | ||
1625 | if (fd >= 0) { | |
1626 | close(fd); | |
1627 | } | |
1628 | } | |
1629 | ||
d59044ef GH |
1630 | static CharDriverState *qemu_chr_open_tty_fd(int fd) |
1631 | { | |
1632 | CharDriverState *chr; | |
1633 | ||
1634 | tty_serial_init(fd, 115200, 'N', 8, 1); | |
1635 | chr = qemu_chr_open_fd(fd, fd); | |
1636 | chr->chr_ioctl = tty_serial_ioctl; | |
1637 | chr->chr_close = qemu_chr_close_tty; | |
1638 | return chr; | |
1639 | } | |
6f97dba0 AL |
1640 | #endif /* __linux__ || __sun__ */ |
1641 | ||
1642 | #if defined(__linux__) | |
e551498e GH |
1643 | |
1644 | #define HAVE_CHARDEV_PARPORT 1 | |
1645 | ||
6f97dba0 AL |
1646 | typedef struct { |
1647 | int fd; | |
1648 | int mode; | |
1649 | } ParallelCharDriver; | |
1650 | ||
1651 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) | |
1652 | { | |
1653 | if (s->mode != mode) { | |
1654 | int m = mode; | |
1655 | if (ioctl(s->fd, PPSETMODE, &m) < 0) | |
1656 | return 0; | |
1657 | s->mode = mode; | |
1658 | } | |
1659 | return 1; | |
1660 | } | |
1661 | ||
1662 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) | |
1663 | { | |
1664 | ParallelCharDriver *drv = chr->opaque; | |
1665 | int fd = drv->fd; | |
1666 | uint8_t b; | |
1667 | ||
1668 | switch(cmd) { | |
1669 | case CHR_IOCTL_PP_READ_DATA: | |
1670 | if (ioctl(fd, PPRDATA, &b) < 0) | |
1671 | return -ENOTSUP; | |
1672 | *(uint8_t *)arg = b; | |
1673 | break; | |
1674 | case CHR_IOCTL_PP_WRITE_DATA: | |
1675 | b = *(uint8_t *)arg; | |
1676 | if (ioctl(fd, PPWDATA, &b) < 0) | |
1677 | return -ENOTSUP; | |
1678 | break; | |
1679 | case CHR_IOCTL_PP_READ_CONTROL: | |
1680 | if (ioctl(fd, PPRCONTROL, &b) < 0) | |
1681 | return -ENOTSUP; | |
1682 | /* Linux gives only the lowest bits, and no way to know data | |
1683 | direction! For better compatibility set the fixed upper | |
1684 | bits. */ | |
1685 | *(uint8_t *)arg = b | 0xc0; | |
1686 | break; | |
1687 | case CHR_IOCTL_PP_WRITE_CONTROL: | |
1688 | b = *(uint8_t *)arg; | |
1689 | if (ioctl(fd, PPWCONTROL, &b) < 0) | |
1690 | return -ENOTSUP; | |
1691 | break; | |
1692 | case CHR_IOCTL_PP_READ_STATUS: | |
1693 | if (ioctl(fd, PPRSTATUS, &b) < 0) | |
1694 | return -ENOTSUP; | |
1695 | *(uint8_t *)arg = b; | |
1696 | break; | |
1697 | case CHR_IOCTL_PP_DATA_DIR: | |
1698 | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) | |
1699 | return -ENOTSUP; | |
1700 | break; | |
1701 | case CHR_IOCTL_PP_EPP_READ_ADDR: | |
1702 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { | |
1703 | struct ParallelIOArg *parg = arg; | |
1704 | int n = read(fd, parg->buffer, parg->count); | |
1705 | if (n != parg->count) { | |
1706 | return -EIO; | |
1707 | } | |
1708 | } | |
1709 | break; | |
1710 | case CHR_IOCTL_PP_EPP_READ: | |
1711 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { | |
1712 | struct ParallelIOArg *parg = arg; | |
1713 | int n = read(fd, parg->buffer, parg->count); | |
1714 | if (n != parg->count) { | |
1715 | return -EIO; | |
1716 | } | |
1717 | } | |
1718 | break; | |
1719 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: | |
1720 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { | |
1721 | struct ParallelIOArg *parg = arg; | |
1722 | int n = write(fd, parg->buffer, parg->count); | |
1723 | if (n != parg->count) { | |
1724 | return -EIO; | |
1725 | } | |
1726 | } | |
1727 | break; | |
1728 | case CHR_IOCTL_PP_EPP_WRITE: | |
1729 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { | |
1730 | struct ParallelIOArg *parg = arg; | |
1731 | int n = write(fd, parg->buffer, parg->count); | |
1732 | if (n != parg->count) { | |
1733 | return -EIO; | |
1734 | } | |
1735 | } | |
1736 | break; | |
1737 | default: | |
1738 | return -ENOTSUP; | |
1739 | } | |
1740 | return 0; | |
1741 | } | |
1742 | ||
1743 | static void pp_close(CharDriverState *chr) | |
1744 | { | |
1745 | ParallelCharDriver *drv = chr->opaque; | |
1746 | int fd = drv->fd; | |
1747 | ||
1748 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); | |
1749 | ioctl(fd, PPRELEASE); | |
1750 | close(fd); | |
7267c094 | 1751 | g_free(drv); |
a425d23f | 1752 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1753 | } |
1754 | ||
88a946d3 | 1755 | static CharDriverState *qemu_chr_open_pp_fd(int fd) |
6f97dba0 AL |
1756 | { |
1757 | CharDriverState *chr; | |
1758 | ParallelCharDriver *drv; | |
6f97dba0 AL |
1759 | |
1760 | if (ioctl(fd, PPCLAIM) < 0) { | |
1761 | close(fd); | |
1f51470d | 1762 | return NULL; |
6f97dba0 AL |
1763 | } |
1764 | ||
7267c094 | 1765 | drv = g_malloc0(sizeof(ParallelCharDriver)); |
6f97dba0 AL |
1766 | drv->fd = fd; |
1767 | drv->mode = IEEE1284_MODE_COMPAT; | |
1768 | ||
db39fcf1 | 1769 | chr = qemu_chr_alloc(); |
6f97dba0 AL |
1770 | chr->chr_write = null_chr_write; |
1771 | chr->chr_ioctl = pp_ioctl; | |
1772 | chr->chr_close = pp_close; | |
1773 | chr->opaque = drv; | |
1774 | ||
1f51470d | 1775 | return chr; |
6f97dba0 AL |
1776 | } |
1777 | #endif /* __linux__ */ | |
1778 | ||
a167ba50 | 1779 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
e551498e GH |
1780 | |
1781 | #define HAVE_CHARDEV_PARPORT 1 | |
1782 | ||
6972f935 BS |
1783 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
1784 | { | |
e0efb993 | 1785 | int fd = (int)(intptr_t)chr->opaque; |
6972f935 BS |
1786 | uint8_t b; |
1787 | ||
1788 | switch(cmd) { | |
1789 | case CHR_IOCTL_PP_READ_DATA: | |
1790 | if (ioctl(fd, PPIGDATA, &b) < 0) | |
1791 | return -ENOTSUP; | |
1792 | *(uint8_t *)arg = b; | |
1793 | break; | |
1794 | case CHR_IOCTL_PP_WRITE_DATA: | |
1795 | b = *(uint8_t *)arg; | |
1796 | if (ioctl(fd, PPISDATA, &b) < 0) | |
1797 | return -ENOTSUP; | |
1798 | break; | |
1799 | case CHR_IOCTL_PP_READ_CONTROL: | |
1800 | if (ioctl(fd, PPIGCTRL, &b) < 0) | |
1801 | return -ENOTSUP; | |
1802 | *(uint8_t *)arg = b; | |
1803 | break; | |
1804 | case CHR_IOCTL_PP_WRITE_CONTROL: | |
1805 | b = *(uint8_t *)arg; | |
1806 | if (ioctl(fd, PPISCTRL, &b) < 0) | |
1807 | return -ENOTSUP; | |
1808 | break; | |
1809 | case CHR_IOCTL_PP_READ_STATUS: | |
1810 | if (ioctl(fd, PPIGSTATUS, &b) < 0) | |
1811 | return -ENOTSUP; | |
1812 | *(uint8_t *)arg = b; | |
1813 | break; | |
1814 | default: | |
1815 | return -ENOTSUP; | |
1816 | } | |
1817 | return 0; | |
1818 | } | |
1819 | ||
88a946d3 | 1820 | static CharDriverState *qemu_chr_open_pp_fd(int fd) |
6972f935 BS |
1821 | { |
1822 | CharDriverState *chr; | |
6972f935 | 1823 | |
db39fcf1 | 1824 | chr = qemu_chr_alloc(); |
e0efb993 | 1825 | chr->opaque = (void *)(intptr_t)fd; |
6972f935 BS |
1826 | chr->chr_write = null_chr_write; |
1827 | chr->chr_ioctl = pp_ioctl; | |
bd5c51ee | 1828 | chr->explicit_be_open = true; |
1f51470d | 1829 | return chr; |
6972f935 BS |
1830 | } |
1831 | #endif | |
1832 | ||
6f97dba0 AL |
1833 | #else /* _WIN32 */ |
1834 | ||
1835 | typedef struct { | |
1836 | int max_size; | |
1837 | HANDLE hcom, hrecv, hsend; | |
9005b2a7 | 1838 | OVERLAPPED orecv; |
6f97dba0 AL |
1839 | BOOL fpipe; |
1840 | DWORD len; | |
9005b2a7 PB |
1841 | |
1842 | /* Protected by the CharDriverState chr_write_lock. */ | |
1843 | OVERLAPPED osend; | |
6f97dba0 AL |
1844 | } WinCharState; |
1845 | ||
db418a0a FC |
1846 | typedef struct { |
1847 | HANDLE hStdIn; | |
1848 | HANDLE hInputReadyEvent; | |
1849 | HANDLE hInputDoneEvent; | |
1850 | HANDLE hInputThread; | |
1851 | uint8_t win_stdio_buf; | |
1852 | } WinStdioCharState; | |
1853 | ||
6f97dba0 AL |
1854 | #define NSENDBUF 2048 |
1855 | #define NRECVBUF 2048 | |
1856 | #define MAXCONNECT 1 | |
1857 | #define NTIMEOUT 5000 | |
1858 | ||
1859 | static int win_chr_poll(void *opaque); | |
1860 | static int win_chr_pipe_poll(void *opaque); | |
1861 | ||
1862 | static void win_chr_close(CharDriverState *chr) | |
1863 | { | |
1864 | WinCharState *s = chr->opaque; | |
1865 | ||
1866 | if (s->hsend) { | |
1867 | CloseHandle(s->hsend); | |
1868 | s->hsend = NULL; | |
1869 | } | |
1870 | if (s->hrecv) { | |
1871 | CloseHandle(s->hrecv); | |
1872 | s->hrecv = NULL; | |
1873 | } | |
1874 | if (s->hcom) { | |
1875 | CloseHandle(s->hcom); | |
1876 | s->hcom = NULL; | |
1877 | } | |
1878 | if (s->fpipe) | |
1879 | qemu_del_polling_cb(win_chr_pipe_poll, chr); | |
1880 | else | |
1881 | qemu_del_polling_cb(win_chr_poll, chr); | |
793cbfb5 | 1882 | |
a425d23f | 1883 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1884 | } |
1885 | ||
1886 | static int win_chr_init(CharDriverState *chr, const char *filename) | |
1887 | { | |
1888 | WinCharState *s = chr->opaque; | |
1889 | COMMCONFIG comcfg; | |
1890 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; | |
1891 | COMSTAT comstat; | |
1892 | DWORD size; | |
1893 | DWORD err; | |
1894 | ||
1895 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1896 | if (!s->hsend) { | |
1897 | fprintf(stderr, "Failed CreateEvent\n"); | |
1898 | goto fail; | |
1899 | } | |
1900 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1901 | if (!s->hrecv) { | |
1902 | fprintf(stderr, "Failed CreateEvent\n"); | |
1903 | goto fail; | |
1904 | } | |
1905 | ||
1906 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, | |
1907 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); | |
1908 | if (s->hcom == INVALID_HANDLE_VALUE) { | |
1909 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); | |
1910 | s->hcom = NULL; | |
1911 | goto fail; | |
1912 | } | |
1913 | ||
1914 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { | |
1915 | fprintf(stderr, "Failed SetupComm\n"); | |
1916 | goto fail; | |
1917 | } | |
1918 | ||
1919 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); | |
1920 | size = sizeof(COMMCONFIG); | |
1921 | GetDefaultCommConfig(filename, &comcfg, &size); | |
1922 | comcfg.dcb.DCBlength = sizeof(DCB); | |
1923 | CommConfigDialog(filename, NULL, &comcfg); | |
1924 | ||
1925 | if (!SetCommState(s->hcom, &comcfg.dcb)) { | |
1926 | fprintf(stderr, "Failed SetCommState\n"); | |
1927 | goto fail; | |
1928 | } | |
1929 | ||
1930 | if (!SetCommMask(s->hcom, EV_ERR)) { | |
1931 | fprintf(stderr, "Failed SetCommMask\n"); | |
1932 | goto fail; | |
1933 | } | |
1934 | ||
1935 | cto.ReadIntervalTimeout = MAXDWORD; | |
1936 | if (!SetCommTimeouts(s->hcom, &cto)) { | |
1937 | fprintf(stderr, "Failed SetCommTimeouts\n"); | |
1938 | goto fail; | |
1939 | } | |
1940 | ||
1941 | if (!ClearCommError(s->hcom, &err, &comstat)) { | |
1942 | fprintf(stderr, "Failed ClearCommError\n"); | |
1943 | goto fail; | |
1944 | } | |
1945 | qemu_add_polling_cb(win_chr_poll, chr); | |
1946 | return 0; | |
1947 | ||
1948 | fail: | |
1949 | win_chr_close(chr); | |
1950 | return -1; | |
1951 | } | |
1952 | ||
9005b2a7 | 1953 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
1954 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
1955 | { | |
1956 | WinCharState *s = chr->opaque; | |
1957 | DWORD len, ret, size, err; | |
1958 | ||
1959 | len = len1; | |
1960 | ZeroMemory(&s->osend, sizeof(s->osend)); | |
1961 | s->osend.hEvent = s->hsend; | |
1962 | while (len > 0) { | |
1963 | if (s->hsend) | |
1964 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); | |
1965 | else | |
1966 | ret = WriteFile(s->hcom, buf, len, &size, NULL); | |
1967 | if (!ret) { | |
1968 | err = GetLastError(); | |
1969 | if (err == ERROR_IO_PENDING) { | |
1970 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); | |
1971 | if (ret) { | |
1972 | buf += size; | |
1973 | len -= size; | |
1974 | } else { | |
1975 | break; | |
1976 | } | |
1977 | } else { | |
1978 | break; | |
1979 | } | |
1980 | } else { | |
1981 | buf += size; | |
1982 | len -= size; | |
1983 | } | |
1984 | } | |
1985 | return len1 - len; | |
1986 | } | |
1987 | ||
1988 | static int win_chr_read_poll(CharDriverState *chr) | |
1989 | { | |
1990 | WinCharState *s = chr->opaque; | |
1991 | ||
909cda12 | 1992 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
1993 | return s->max_size; |
1994 | } | |
1995 | ||
1996 | static void win_chr_readfile(CharDriverState *chr) | |
1997 | { | |
1998 | WinCharState *s = chr->opaque; | |
1999 | int ret, err; | |
9bd7854e | 2000 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2001 | DWORD size; |
2002 | ||
2003 | ZeroMemory(&s->orecv, sizeof(s->orecv)); | |
2004 | s->orecv.hEvent = s->hrecv; | |
2005 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); | |
2006 | if (!ret) { | |
2007 | err = GetLastError(); | |
2008 | if (err == ERROR_IO_PENDING) { | |
2009 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); | |
2010 | } | |
2011 | } | |
2012 | ||
2013 | if (size > 0) { | |
fa5efccb | 2014 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 AL |
2015 | } |
2016 | } | |
2017 | ||
2018 | static void win_chr_read(CharDriverState *chr) | |
2019 | { | |
2020 | WinCharState *s = chr->opaque; | |
2021 | ||
2022 | if (s->len > s->max_size) | |
2023 | s->len = s->max_size; | |
2024 | if (s->len == 0) | |
2025 | return; | |
2026 | ||
2027 | win_chr_readfile(chr); | |
2028 | } | |
2029 | ||
2030 | static int win_chr_poll(void *opaque) | |
2031 | { | |
2032 | CharDriverState *chr = opaque; | |
2033 | WinCharState *s = chr->opaque; | |
2034 | COMSTAT status; | |
2035 | DWORD comerr; | |
2036 | ||
2037 | ClearCommError(s->hcom, &comerr, &status); | |
2038 | if (status.cbInQue > 0) { | |
2039 | s->len = status.cbInQue; | |
2040 | win_chr_read_poll(chr); | |
2041 | win_chr_read(chr); | |
2042 | return 1; | |
2043 | } | |
2044 | return 0; | |
2045 | } | |
2046 | ||
d59044ef | 2047 | static CharDriverState *qemu_chr_open_win_path(const char *filename) |
6f97dba0 AL |
2048 | { |
2049 | CharDriverState *chr; | |
2050 | WinCharState *s; | |
2051 | ||
db39fcf1 | 2052 | chr = qemu_chr_alloc(); |
7267c094 | 2053 | s = g_malloc0(sizeof(WinCharState)); |
6f97dba0 AL |
2054 | chr->opaque = s; |
2055 | chr->chr_write = win_chr_write; | |
2056 | chr->chr_close = win_chr_close; | |
2057 | ||
2058 | if (win_chr_init(chr, filename) < 0) { | |
2e02e18b SW |
2059 | g_free(s); |
2060 | g_free(chr); | |
1f51470d | 2061 | return NULL; |
6f97dba0 | 2062 | } |
1f51470d | 2063 | return chr; |
6f97dba0 AL |
2064 | } |
2065 | ||
2066 | static int win_chr_pipe_poll(void *opaque) | |
2067 | { | |
2068 | CharDriverState *chr = opaque; | |
2069 | WinCharState *s = chr->opaque; | |
2070 | DWORD size; | |
2071 | ||
2072 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); | |
2073 | if (size > 0) { | |
2074 | s->len = size; | |
2075 | win_chr_read_poll(chr); | |
2076 | win_chr_read(chr); | |
2077 | return 1; | |
2078 | } | |
2079 | return 0; | |
2080 | } | |
2081 | ||
2082 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) | |
2083 | { | |
2084 | WinCharState *s = chr->opaque; | |
2085 | OVERLAPPED ov; | |
2086 | int ret; | |
2087 | DWORD size; | |
9f781168 | 2088 | char openname[CHR_MAX_FILENAME_SIZE]; |
6f97dba0 AL |
2089 | |
2090 | s->fpipe = TRUE; | |
2091 | ||
2092 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); | |
2093 | if (!s->hsend) { | |
2094 | fprintf(stderr, "Failed CreateEvent\n"); | |
2095 | goto fail; | |
2096 | } | |
2097 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); | |
2098 | if (!s->hrecv) { | |
2099 | fprintf(stderr, "Failed CreateEvent\n"); | |
2100 | goto fail; | |
2101 | } | |
2102 | ||
2103 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); | |
2104 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, | |
2105 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | | |
2106 | PIPE_WAIT, | |
2107 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); | |
2108 | if (s->hcom == INVALID_HANDLE_VALUE) { | |
2109 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); | |
2110 | s->hcom = NULL; | |
2111 | goto fail; | |
2112 | } | |
2113 | ||
2114 | ZeroMemory(&ov, sizeof(ov)); | |
2115 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); | |
2116 | ret = ConnectNamedPipe(s->hcom, &ov); | |
2117 | if (ret) { | |
2118 | fprintf(stderr, "Failed ConnectNamedPipe\n"); | |
2119 | goto fail; | |
2120 | } | |
2121 | ||
2122 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); | |
2123 | if (!ret) { | |
2124 | fprintf(stderr, "Failed GetOverlappedResult\n"); | |
2125 | if (ov.hEvent) { | |
2126 | CloseHandle(ov.hEvent); | |
2127 | ov.hEvent = NULL; | |
2128 | } | |
2129 | goto fail; | |
2130 | } | |
2131 | ||
2132 | if (ov.hEvent) { | |
2133 | CloseHandle(ov.hEvent); | |
2134 | ov.hEvent = NULL; | |
2135 | } | |
2136 | qemu_add_polling_cb(win_chr_pipe_poll, chr); | |
2137 | return 0; | |
2138 | ||
2139 | fail: | |
2140 | win_chr_close(chr); | |
2141 | return -1; | |
2142 | } | |
2143 | ||
2144 | ||
548cbb36 | 2145 | static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts) |
6f97dba0 | 2146 | { |
548cbb36 | 2147 | const char *filename = opts->device; |
6f97dba0 AL |
2148 | CharDriverState *chr; |
2149 | WinCharState *s; | |
2150 | ||
db39fcf1 | 2151 | chr = qemu_chr_alloc(); |
7267c094 | 2152 | s = g_malloc0(sizeof(WinCharState)); |
6f97dba0 AL |
2153 | chr->opaque = s; |
2154 | chr->chr_write = win_chr_write; | |
2155 | chr->chr_close = win_chr_close; | |
2156 | ||
2157 | if (win_chr_pipe_init(chr, filename) < 0) { | |
2e02e18b SW |
2158 | g_free(s); |
2159 | g_free(chr); | |
1f51470d | 2160 | return NULL; |
6f97dba0 | 2161 | } |
1f51470d | 2162 | return chr; |
6f97dba0 AL |
2163 | } |
2164 | ||
1f51470d | 2165 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
6f97dba0 AL |
2166 | { |
2167 | CharDriverState *chr; | |
2168 | WinCharState *s; | |
2169 | ||
db39fcf1 | 2170 | chr = qemu_chr_alloc(); |
7267c094 | 2171 | s = g_malloc0(sizeof(WinCharState)); |
6f97dba0 AL |
2172 | s->hcom = fd_out; |
2173 | chr->opaque = s; | |
2174 | chr->chr_write = win_chr_write; | |
1f51470d | 2175 | return chr; |
6f97dba0 AL |
2176 | } |
2177 | ||
d9ac374f | 2178 | static CharDriverState *qemu_chr_open_win_con(void) |
6f97dba0 | 2179 | { |
1f51470d | 2180 | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE)); |
6f97dba0 AL |
2181 | } |
2182 | ||
db418a0a FC |
2183 | static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len) |
2184 | { | |
2185 | HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
2186 | DWORD dwSize; | |
2187 | int len1; | |
2188 | ||
2189 | len1 = len; | |
2190 | ||
2191 | while (len1 > 0) { | |
2192 | if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { | |
2193 | break; | |
2194 | } | |
2195 | buf += dwSize; | |
2196 | len1 -= dwSize; | |
2197 | } | |
2198 | ||
2199 | return len - len1; | |
2200 | } | |
2201 | ||
2202 | static void win_stdio_wait_func(void *opaque) | |
2203 | { | |
2204 | CharDriverState *chr = opaque; | |
2205 | WinStdioCharState *stdio = chr->opaque; | |
2206 | INPUT_RECORD buf[4]; | |
2207 | int ret; | |
2208 | DWORD dwSize; | |
2209 | int i; | |
2210 | ||
dff7424d | 2211 | ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize); |
db418a0a FC |
2212 | |
2213 | if (!ret) { | |
2214 | /* Avoid error storm */ | |
2215 | qemu_del_wait_object(stdio->hStdIn, NULL, NULL); | |
2216 | return; | |
2217 | } | |
2218 | ||
2219 | for (i = 0; i < dwSize; i++) { | |
2220 | KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; | |
2221 | ||
2222 | if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { | |
2223 | int j; | |
2224 | if (kev->uChar.AsciiChar != 0) { | |
2225 | for (j = 0; j < kev->wRepeatCount; j++) { | |
2226 | if (qemu_chr_be_can_write(chr)) { | |
2227 | uint8_t c = kev->uChar.AsciiChar; | |
2228 | qemu_chr_be_write(chr, &c, 1); | |
2229 | } | |
2230 | } | |
2231 | } | |
2232 | } | |
2233 | } | |
2234 | } | |
2235 | ||
2236 | static DWORD WINAPI win_stdio_thread(LPVOID param) | |
2237 | { | |
2238 | CharDriverState *chr = param; | |
2239 | WinStdioCharState *stdio = chr->opaque; | |
2240 | int ret; | |
2241 | DWORD dwSize; | |
2242 | ||
2243 | while (1) { | |
2244 | ||
2245 | /* Wait for one byte */ | |
2246 | ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); | |
2247 | ||
2248 | /* Exit in case of error, continue if nothing read */ | |
2249 | if (!ret) { | |
2250 | break; | |
2251 | } | |
2252 | if (!dwSize) { | |
2253 | continue; | |
2254 | } | |
2255 | ||
2256 | /* Some terminal emulator returns \r\n for Enter, just pass \n */ | |
2257 | if (stdio->win_stdio_buf == '\r') { | |
2258 | continue; | |
2259 | } | |
2260 | ||
2261 | /* Signal the main thread and wait until the byte was eaten */ | |
2262 | if (!SetEvent(stdio->hInputReadyEvent)) { | |
2263 | break; | |
2264 | } | |
2265 | if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) | |
2266 | != WAIT_OBJECT_0) { | |
2267 | break; | |
2268 | } | |
2269 | } | |
2270 | ||
2271 | qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); | |
2272 | return 0; | |
2273 | } | |
2274 | ||
2275 | static void win_stdio_thread_wait_func(void *opaque) | |
2276 | { | |
2277 | CharDriverState *chr = opaque; | |
2278 | WinStdioCharState *stdio = chr->opaque; | |
2279 | ||
2280 | if (qemu_chr_be_can_write(chr)) { | |
2281 | qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); | |
2282 | } | |
2283 | ||
2284 | SetEvent(stdio->hInputDoneEvent); | |
2285 | } | |
2286 | ||
2287 | static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo) | |
2288 | { | |
2289 | WinStdioCharState *stdio = chr->opaque; | |
2290 | DWORD dwMode = 0; | |
2291 | ||
2292 | GetConsoleMode(stdio->hStdIn, &dwMode); | |
2293 | ||
2294 | if (echo) { | |
2295 | SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); | |
2296 | } else { | |
2297 | SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); | |
2298 | } | |
2299 | } | |
2300 | ||
2301 | static void win_stdio_close(CharDriverState *chr) | |
2302 | { | |
2303 | WinStdioCharState *stdio = chr->opaque; | |
2304 | ||
2305 | if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { | |
2306 | CloseHandle(stdio->hInputReadyEvent); | |
2307 | } | |
2308 | if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { | |
2309 | CloseHandle(stdio->hInputDoneEvent); | |
2310 | } | |
2311 | if (stdio->hInputThread != INVALID_HANDLE_VALUE) { | |
2312 | TerminateThread(stdio->hInputThread, 0); | |
2313 | } | |
2314 | ||
2315 | g_free(chr->opaque); | |
2316 | g_free(chr); | |
db418a0a FC |
2317 | } |
2318 | ||
7c358031 | 2319 | static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts) |
db418a0a FC |
2320 | { |
2321 | CharDriverState *chr; | |
2322 | WinStdioCharState *stdio; | |
2323 | DWORD dwMode; | |
2324 | int is_console = 0; | |
2325 | ||
db39fcf1 | 2326 | chr = qemu_chr_alloc(); |
db418a0a FC |
2327 | stdio = g_malloc0(sizeof(WinStdioCharState)); |
2328 | ||
2329 | stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); | |
2330 | if (stdio->hStdIn == INVALID_HANDLE_VALUE) { | |
2331 | fprintf(stderr, "cannot open stdio: invalid handle\n"); | |
2332 | exit(1); | |
2333 | } | |
2334 | ||
2335 | is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; | |
2336 | ||
2337 | chr->opaque = stdio; | |
2338 | chr->chr_write = win_stdio_write; | |
2339 | chr->chr_close = win_stdio_close; | |
2340 | ||
ed7a1540 AL |
2341 | if (is_console) { |
2342 | if (qemu_add_wait_object(stdio->hStdIn, | |
2343 | win_stdio_wait_func, chr)) { | |
2344 | fprintf(stderr, "qemu_add_wait_object: failed\n"); | |
2345 | } | |
2346 | } else { | |
2347 | DWORD dwId; | |
2348 | ||
2349 | stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
2350 | stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
2351 | stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, | |
2352 | chr, 0, &dwId); | |
2353 | ||
2354 | if (stdio->hInputThread == INVALID_HANDLE_VALUE | |
2355 | || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE | |
2356 | || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { | |
2357 | fprintf(stderr, "cannot create stdio thread or event\n"); | |
2358 | exit(1); | |
2359 | } | |
2360 | if (qemu_add_wait_object(stdio->hInputReadyEvent, | |
2361 | win_stdio_thread_wait_func, chr)) { | |
2362 | fprintf(stderr, "qemu_add_wait_object: failed\n"); | |
db418a0a FC |
2363 | } |
2364 | } | |
2365 | ||
2366 | dwMode |= ENABLE_LINE_INPUT; | |
2367 | ||
ed7a1540 | 2368 | if (is_console) { |
db418a0a FC |
2369 | /* set the terminal in raw mode */ |
2370 | /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ | |
2371 | dwMode |= ENABLE_PROCESSED_INPUT; | |
2372 | } | |
2373 | ||
2374 | SetConsoleMode(stdio->hStdIn, dwMode); | |
2375 | ||
2376 | chr->chr_set_echo = qemu_chr_set_echo_win_stdio; | |
2377 | qemu_chr_fe_set_echo(chr, false); | |
2378 | ||
1f51470d | 2379 | return chr; |
db418a0a | 2380 | } |
6f97dba0 AL |
2381 | #endif /* !_WIN32 */ |
2382 | ||
5ab8211b | 2383 | |
6f97dba0 AL |
2384 | /***********************************************************/ |
2385 | /* UDP Net console */ | |
2386 | ||
2387 | typedef struct { | |
2388 | int fd; | |
76a9644b | 2389 | GIOChannel *chan; |
9bd7854e | 2390 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2391 | int bufcnt; |
2392 | int bufptr; | |
2393 | int max_size; | |
2394 | } NetCharDriver; | |
2395 | ||
9005b2a7 | 2396 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
2397 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2398 | { | |
2399 | NetCharDriver *s = chr->opaque; | |
76a9644b AL |
2400 | gsize bytes_written; |
2401 | GIOStatus status; | |
2402 | ||
2403 | status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL); | |
2404 | if (status == G_IO_STATUS_EOF) { | |
2405 | return 0; | |
2406 | } else if (status != G_IO_STATUS_NORMAL) { | |
2407 | return -1; | |
2408 | } | |
6f97dba0 | 2409 | |
76a9644b | 2410 | return bytes_written; |
6f97dba0 AL |
2411 | } |
2412 | ||
2413 | static int udp_chr_read_poll(void *opaque) | |
2414 | { | |
2415 | CharDriverState *chr = opaque; | |
2416 | NetCharDriver *s = chr->opaque; | |
2417 | ||
909cda12 | 2418 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2419 | |
2420 | /* If there were any stray characters in the queue process them | |
2421 | * first | |
2422 | */ | |
2423 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { | |
fa5efccb | 2424 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
6f97dba0 | 2425 | s->bufptr++; |
909cda12 | 2426 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2427 | } |
2428 | return s->max_size; | |
2429 | } | |
2430 | ||
76a9644b | 2431 | static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2432 | { |
2433 | CharDriverState *chr = opaque; | |
2434 | NetCharDriver *s = chr->opaque; | |
76a9644b AL |
2435 | gsize bytes_read = 0; |
2436 | GIOStatus status; | |
6f97dba0 | 2437 | |
cdbf6e16 PB |
2438 | if (s->max_size == 0) { |
2439 | return TRUE; | |
2440 | } | |
76a9644b AL |
2441 | status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf), |
2442 | &bytes_read, NULL); | |
2443 | s->bufcnt = bytes_read; | |
6f97dba0 | 2444 | s->bufptr = s->bufcnt; |
76a9644b | 2445 | if (status != G_IO_STATUS_NORMAL) { |
26da70c7 | 2446 | remove_fd_in_watch(chr); |
76a9644b AL |
2447 | return FALSE; |
2448 | } | |
6f97dba0 AL |
2449 | |
2450 | s->bufptr = 0; | |
2451 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { | |
fa5efccb | 2452 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
6f97dba0 | 2453 | s->bufptr++; |
909cda12 | 2454 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 | 2455 | } |
76a9644b AL |
2456 | |
2457 | return TRUE; | |
6f97dba0 AL |
2458 | } |
2459 | ||
2460 | static void udp_chr_update_read_handler(CharDriverState *chr) | |
2461 | { | |
2462 | NetCharDriver *s = chr->opaque; | |
2463 | ||
26da70c7 | 2464 | remove_fd_in_watch(chr); |
76a9644b | 2465 | if (s->chan) { |
7ba9addc AS |
2466 | chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll, |
2467 | udp_chr_read, chr); | |
6f97dba0 AL |
2468 | } |
2469 | } | |
2470 | ||
819f56b7 AL |
2471 | static void udp_chr_close(CharDriverState *chr) |
2472 | { | |
2473 | NetCharDriver *s = chr->opaque; | |
26da70c7 AS |
2474 | |
2475 | remove_fd_in_watch(chr); | |
76a9644b AL |
2476 | if (s->chan) { |
2477 | g_io_channel_unref(s->chan); | |
819f56b7 AL |
2478 | closesocket(s->fd); |
2479 | } | |
7267c094 | 2480 | g_free(s); |
a425d23f | 2481 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
819f56b7 AL |
2482 | } |
2483 | ||
3ecc059d | 2484 | static CharDriverState *qemu_chr_open_udp_fd(int fd) |
6f97dba0 AL |
2485 | { |
2486 | CharDriverState *chr = NULL; | |
2487 | NetCharDriver *s = NULL; | |
6f97dba0 | 2488 | |
db39fcf1 | 2489 | chr = qemu_chr_alloc(); |
7267c094 | 2490 | s = g_malloc0(sizeof(NetCharDriver)); |
6f97dba0 | 2491 | |
6f97dba0 | 2492 | s->fd = fd; |
76a9644b | 2493 | s->chan = io_channel_from_socket(s->fd); |
6f97dba0 AL |
2494 | s->bufcnt = 0; |
2495 | s->bufptr = 0; | |
2496 | chr->opaque = s; | |
2497 | chr->chr_write = udp_chr_write; | |
2498 | chr->chr_update_read_handler = udp_chr_update_read_handler; | |
819f56b7 | 2499 | chr->chr_close = udp_chr_close; |
bd5c51ee MR |
2500 | /* be isn't opened until we get a connection */ |
2501 | chr->explicit_be_open = true; | |
1f51470d | 2502 | return chr; |
3ecc059d | 2503 | } |
6f97dba0 | 2504 | |
6f97dba0 AL |
2505 | /***********************************************************/ |
2506 | /* TCP Net console */ | |
2507 | ||
2508 | typedef struct { | |
2ea5a7af AL |
2509 | |
2510 | GIOChannel *chan, *listen_chan; | |
7ba9addc | 2511 | guint listen_tag; |
6f97dba0 AL |
2512 | int fd, listen_fd; |
2513 | int connected; | |
2514 | int max_size; | |
2515 | int do_telnetopt; | |
2516 | int do_nodelay; | |
2517 | int is_unix; | |
c76bf6bb NN |
2518 | int *read_msgfds; |
2519 | int read_msgfds_num; | |
d39aac7a NN |
2520 | int *write_msgfds; |
2521 | int write_msgfds_num; | |
cfb429cb CM |
2522 | |
2523 | SocketAddress *addr; | |
2524 | bool is_listen; | |
2525 | bool is_telnet; | |
5dd1f02b CM |
2526 | |
2527 | guint reconnect_timer; | |
2528 | int64_t reconnect_time; | |
5008e5b7 | 2529 | bool connect_err_reported; |
6f97dba0 AL |
2530 | } TCPCharDriver; |
2531 | ||
5dd1f02b CM |
2532 | static gboolean socket_reconnect_timeout(gpointer opaque); |
2533 | ||
2534 | static void qemu_chr_socket_restart_timer(CharDriverState *chr) | |
2535 | { | |
2536 | TCPCharDriver *s = chr->opaque; | |
2537 | assert(s->connected == 0); | |
2538 | s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time, | |
2539 | socket_reconnect_timeout, chr); | |
2540 | } | |
2541 | ||
5008e5b7 CM |
2542 | static void check_report_connect_error(CharDriverState *chr, |
2543 | Error *err) | |
2544 | { | |
2545 | TCPCharDriver *s = chr->opaque; | |
2546 | ||
2547 | if (!s->connect_err_reported) { | |
2548 | error_report("Unable to connect character device %s: %s", | |
2549 | chr->label, error_get_pretty(err)); | |
2550 | s->connect_err_reported = true; | |
2551 | } | |
2552 | qemu_chr_socket_restart_timer(chr); | |
2553 | } | |
2554 | ||
2ea5a7af | 2555 | static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque); |
6f97dba0 | 2556 | |
d39aac7a NN |
2557 | #ifndef _WIN32 |
2558 | static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len) | |
2559 | { | |
2560 | TCPCharDriver *s = chr->opaque; | |
2561 | struct msghdr msgh; | |
2562 | struct iovec iov; | |
2563 | int r; | |
2564 | ||
2565 | size_t fd_size = s->write_msgfds_num * sizeof(int); | |
2566 | char control[CMSG_SPACE(fd_size)]; | |
2567 | struct cmsghdr *cmsg; | |
2568 | ||
2569 | memset(&msgh, 0, sizeof(msgh)); | |
2570 | memset(control, 0, sizeof(control)); | |
2571 | ||
2572 | /* set the payload */ | |
2573 | iov.iov_base = (uint8_t *) buf; | |
2574 | iov.iov_len = len; | |
2575 | ||
2576 | msgh.msg_iov = &iov; | |
2577 | msgh.msg_iovlen = 1; | |
2578 | ||
2579 | msgh.msg_control = control; | |
2580 | msgh.msg_controllen = sizeof(control); | |
2581 | ||
2582 | cmsg = CMSG_FIRSTHDR(&msgh); | |
2583 | ||
2584 | cmsg->cmsg_len = CMSG_LEN(fd_size); | |
2585 | cmsg->cmsg_level = SOL_SOCKET; | |
2586 | cmsg->cmsg_type = SCM_RIGHTS; | |
2587 | memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size); | |
2588 | ||
2589 | do { | |
2590 | r = sendmsg(s->fd, &msgh, 0); | |
2591 | } while (r < 0 && errno == EINTR); | |
2592 | ||
2593 | /* free the written msgfds, no matter what */ | |
2594 | if (s->write_msgfds_num) { | |
2595 | g_free(s->write_msgfds); | |
2596 | s->write_msgfds = 0; | |
2597 | s->write_msgfds_num = 0; | |
2598 | } | |
2599 | ||
2600 | return r; | |
2601 | } | |
2602 | #endif | |
2603 | ||
9005b2a7 | 2604 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
2605 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2606 | { | |
2607 | TCPCharDriver *s = chr->opaque; | |
2608 | if (s->connected) { | |
d39aac7a NN |
2609 | #ifndef _WIN32 |
2610 | if (s->is_unix && s->write_msgfds_num) { | |
2611 | return unix_send_msgfds(chr, buf, len); | |
2612 | } else | |
2613 | #endif | |
2614 | { | |
2615 | return io_channel_send(s->chan, buf, len); | |
2616 | } | |
455aa1e0 | 2617 | } else { |
6db0fdce | 2618 | /* XXX: indicate an error ? */ |
455aa1e0 | 2619 | return len; |
6f97dba0 AL |
2620 | } |
2621 | } | |
2622 | ||
2623 | static int tcp_chr_read_poll(void *opaque) | |
2624 | { | |
2625 | CharDriverState *chr = opaque; | |
2626 | TCPCharDriver *s = chr->opaque; | |
2627 | if (!s->connected) | |
2628 | return 0; | |
909cda12 | 2629 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2630 | return s->max_size; |
2631 | } | |
2632 | ||
2633 | #define IAC 255 | |
2634 | #define IAC_BREAK 243 | |
2635 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, | |
2636 | TCPCharDriver *s, | |
2637 | uint8_t *buf, int *size) | |
2638 | { | |
2639 | /* Handle any telnet client's basic IAC options to satisfy char by | |
2640 | * char mode with no echo. All IAC options will be removed from | |
2641 | * the buf and the do_telnetopt variable will be used to track the | |
2642 | * state of the width of the IAC information. | |
2643 | * | |
2644 | * IAC commands come in sets of 3 bytes with the exception of the | |
2645 | * "IAC BREAK" command and the double IAC. | |
2646 | */ | |
2647 | ||
2648 | int i; | |
2649 | int j = 0; | |
2650 | ||
2651 | for (i = 0; i < *size; i++) { | |
2652 | if (s->do_telnetopt > 1) { | |
2653 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { | |
2654 | /* Double IAC means send an IAC */ | |
2655 | if (j != i) | |
2656 | buf[j] = buf[i]; | |
2657 | j++; | |
2658 | s->do_telnetopt = 1; | |
2659 | } else { | |
2660 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { | |
2661 | /* Handle IAC break commands by sending a serial break */ | |
a425d23f | 2662 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
6f97dba0 AL |
2663 | s->do_telnetopt++; |
2664 | } | |
2665 | s->do_telnetopt++; | |
2666 | } | |
2667 | if (s->do_telnetopt >= 4) { | |
2668 | s->do_telnetopt = 1; | |
2669 | } | |
2670 | } else { | |
2671 | if ((unsigned char)buf[i] == IAC) { | |
2672 | s->do_telnetopt = 2; | |
2673 | } else { | |
2674 | if (j != i) | |
2675 | buf[j] = buf[i]; | |
2676 | j++; | |
2677 | } | |
2678 | } | |
2679 | } | |
2680 | *size = j; | |
2681 | } | |
2682 | ||
c76bf6bb | 2683 | static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num) |
7d174059 MM |
2684 | { |
2685 | TCPCharDriver *s = chr->opaque; | |
c76bf6bb NN |
2686 | int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num; |
2687 | ||
c4095726 MT |
2688 | assert(num <= TCP_MAX_FDS); |
2689 | ||
c76bf6bb | 2690 | if (to_copy) { |
d2fc39b4 SH |
2691 | int i; |
2692 | ||
c76bf6bb NN |
2693 | memcpy(fds, s->read_msgfds, to_copy * sizeof(int)); |
2694 | ||
d2fc39b4 SH |
2695 | /* Close unused fds */ |
2696 | for (i = to_copy; i < s->read_msgfds_num; i++) { | |
2697 | close(s->read_msgfds[i]); | |
2698 | } | |
2699 | ||
c76bf6bb NN |
2700 | g_free(s->read_msgfds); |
2701 | s->read_msgfds = 0; | |
2702 | s->read_msgfds_num = 0; | |
2703 | } | |
2704 | ||
2705 | return to_copy; | |
7d174059 MM |
2706 | } |
2707 | ||
d39aac7a NN |
2708 | static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num) |
2709 | { | |
2710 | TCPCharDriver *s = chr->opaque; | |
2711 | ||
2712 | /* clear old pending fd array */ | |
2713 | if (s->write_msgfds) { | |
2714 | g_free(s->write_msgfds); | |
2715 | } | |
2716 | ||
2717 | if (num) { | |
2718 | s->write_msgfds = g_malloc(num * sizeof(int)); | |
2719 | memcpy(s->write_msgfds, fds, num * sizeof(int)); | |
2720 | } | |
2721 | ||
2722 | s->write_msgfds_num = num; | |
2723 | ||
2724 | return 0; | |
2725 | } | |
2726 | ||
73bcc2ac | 2727 | #ifndef _WIN32 |
7d174059 MM |
2728 | static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg) |
2729 | { | |
2730 | TCPCharDriver *s = chr->opaque; | |
2731 | struct cmsghdr *cmsg; | |
2732 | ||
2733 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
c76bf6bb | 2734 | int fd_size, i; |
7d174059 | 2735 | |
c76bf6bb | 2736 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) || |
7d174059 | 2737 | cmsg->cmsg_level != SOL_SOCKET || |
c76bf6bb | 2738 | cmsg->cmsg_type != SCM_RIGHTS) { |
7d174059 | 2739 | continue; |
c76bf6bb | 2740 | } |
7d174059 | 2741 | |
c76bf6bb NN |
2742 | fd_size = cmsg->cmsg_len - CMSG_LEN(0); |
2743 | ||
2744 | if (!fd_size) { | |
7d174059 | 2745 | continue; |
c76bf6bb | 2746 | } |
7d174059 | 2747 | |
c76bf6bb NN |
2748 | /* close and clean read_msgfds */ |
2749 | for (i = 0; i < s->read_msgfds_num; i++) { | |
2750 | close(s->read_msgfds[i]); | |
2751 | } | |
9b938c72 | 2752 | |
c76bf6bb NN |
2753 | if (s->read_msgfds_num) { |
2754 | g_free(s->read_msgfds); | |
2755 | } | |
2756 | ||
2757 | s->read_msgfds_num = fd_size / sizeof(int); | |
2758 | s->read_msgfds = g_malloc(fd_size); | |
2759 | memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size); | |
2760 | ||
2761 | for (i = 0; i < s->read_msgfds_num; i++) { | |
2762 | int fd = s->read_msgfds[i]; | |
2763 | if (fd < 0) { | |
2764 | continue; | |
2765 | } | |
2766 | ||
2767 | /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ | |
2768 | qemu_set_block(fd); | |
2769 | ||
2770 | #ifndef MSG_CMSG_CLOEXEC | |
2771 | qemu_set_cloexec(fd); | |
2772 | #endif | |
2773 | } | |
7d174059 MM |
2774 | } |
2775 | } | |
2776 | ||
9977c894 MM |
2777 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
2778 | { | |
2779 | TCPCharDriver *s = chr->opaque; | |
7cba04f6 | 2780 | struct msghdr msg = { NULL, }; |
9977c894 | 2781 | struct iovec iov[1]; |
7d174059 MM |
2782 | union { |
2783 | struct cmsghdr cmsg; | |
c4095726 | 2784 | char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)]; |
7d174059 | 2785 | } msg_control; |
06138651 | 2786 | int flags = 0; |
7d174059 | 2787 | ssize_t ret; |
9977c894 MM |
2788 | |
2789 | iov[0].iov_base = buf; | |
2790 | iov[0].iov_len = len; | |
2791 | ||
2792 | msg.msg_iov = iov; | |
2793 | msg.msg_iovlen = 1; | |
7d174059 MM |
2794 | msg.msg_control = &msg_control; |
2795 | msg.msg_controllen = sizeof(msg_control); | |
2796 | ||
06138651 CB |
2797 | #ifdef MSG_CMSG_CLOEXEC |
2798 | flags |= MSG_CMSG_CLOEXEC; | |
2799 | #endif | |
2800 | ret = recvmsg(s->fd, &msg, flags); | |
2801 | if (ret > 0 && s->is_unix) { | |
7d174059 | 2802 | unix_process_msgfd(chr, &msg); |
06138651 | 2803 | } |
9977c894 | 2804 | |
7d174059 | 2805 | return ret; |
9977c894 MM |
2806 | } |
2807 | #else | |
2808 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) | |
2809 | { | |
2810 | TCPCharDriver *s = chr->opaque; | |
00aa0040 | 2811 | return qemu_recv(s->fd, buf, len, 0); |
9977c894 MM |
2812 | } |
2813 | #endif | |
2814 | ||
d3cc5bc4 AS |
2815 | static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
2816 | { | |
2817 | TCPCharDriver *s = chr->opaque; | |
2818 | return g_io_create_watch(s->chan, cond); | |
2819 | } | |
2820 | ||
7b0bfdf5 NN |
2821 | static void tcp_chr_disconnect(CharDriverState *chr) |
2822 | { | |
2823 | TCPCharDriver *s = chr->opaque; | |
2824 | ||
2825 | s->connected = 0; | |
2826 | if (s->listen_chan) { | |
2827 | s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, | |
2828 | tcp_chr_accept, chr); | |
2829 | } | |
2830 | remove_fd_in_watch(chr); | |
2831 | g_io_channel_unref(s->chan); | |
2832 | s->chan = NULL; | |
2833 | closesocket(s->fd); | |
2834 | s->fd = -1; | |
16cc4ffe CM |
2835 | SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, |
2836 | "disconnected:", s->addr, s->is_listen, s->is_telnet); | |
7b0bfdf5 | 2837 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
5dd1f02b CM |
2838 | if (s->reconnect_time) { |
2839 | qemu_chr_socket_restart_timer(chr); | |
2840 | } | |
7b0bfdf5 NN |
2841 | } |
2842 | ||
2ea5a7af | 2843 | static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2844 | { |
2845 | CharDriverState *chr = opaque; | |
2846 | TCPCharDriver *s = chr->opaque; | |
9bd7854e | 2847 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2848 | int len, size; |
2849 | ||
812c1057 KB |
2850 | if (cond & G_IO_HUP) { |
2851 | /* connection closed */ | |
2852 | tcp_chr_disconnect(chr); | |
2853 | return TRUE; | |
2854 | } | |
2855 | ||
2ea5a7af | 2856 | if (!s->connected || s->max_size <= 0) { |
cdbf6e16 | 2857 | return TRUE; |
2ea5a7af | 2858 | } |
6f97dba0 AL |
2859 | len = sizeof(buf); |
2860 | if (len > s->max_size) | |
2861 | len = s->max_size; | |
9977c894 | 2862 | size = tcp_chr_recv(chr, (void *)buf, len); |
6f97dba0 AL |
2863 | if (size == 0) { |
2864 | /* connection closed */ | |
7b0bfdf5 | 2865 | tcp_chr_disconnect(chr); |
6f97dba0 AL |
2866 | } else if (size > 0) { |
2867 | if (s->do_telnetopt) | |
2868 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); | |
2869 | if (size > 0) | |
fa5efccb | 2870 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 | 2871 | } |
2ea5a7af AL |
2872 | |
2873 | return TRUE; | |
6f97dba0 AL |
2874 | } |
2875 | ||
7b0bfdf5 NN |
2876 | static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len) |
2877 | { | |
2878 | TCPCharDriver *s = chr->opaque; | |
2879 | int size; | |
2880 | ||
2881 | if (!s->connected) { | |
2882 | return 0; | |
2883 | } | |
2884 | ||
2885 | size = tcp_chr_recv(chr, (void *) buf, len); | |
2886 | if (size == 0) { | |
2887 | /* connection closed */ | |
2888 | tcp_chr_disconnect(chr); | |
2889 | } | |
2890 | ||
2891 | return size; | |
2892 | } | |
2893 | ||
68c18d1c BS |
2894 | #ifndef _WIN32 |
2895 | CharDriverState *qemu_chr_open_eventfd(int eventfd) | |
2896 | { | |
e9d21c43 DM |
2897 | CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd); |
2898 | ||
2899 | if (chr) { | |
2900 | chr->avail_connections = 1; | |
2901 | } | |
2902 | ||
2903 | return chr; | |
6cbf4c8c | 2904 | } |
68c18d1c | 2905 | #endif |
6cbf4c8c | 2906 | |
6f97dba0 AL |
2907 | static void tcp_chr_connect(void *opaque) |
2908 | { | |
2909 | CharDriverState *chr = opaque; | |
2910 | TCPCharDriver *s = chr->opaque; | |
01ca519f CM |
2911 | struct sockaddr_storage ss, ps; |
2912 | socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps); | |
16cc4ffe CM |
2913 | |
2914 | memset(&ss, 0, ss_len); | |
2915 | if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) { | |
2916 | snprintf(chr->filename, CHR_MAX_FILENAME_SIZE, | |
2917 | "Error in getsockname: %s\n", strerror(errno)); | |
01ca519f CM |
2918 | } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) { |
2919 | snprintf(chr->filename, CHR_MAX_FILENAME_SIZE, | |
2920 | "Error in getpeername: %s\n", strerror(errno)); | |
16cc4ffe | 2921 | } else { |
01ca519f CM |
2922 | sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, |
2923 | &ss, ss_len, &ps, ps_len, | |
16cc4ffe CM |
2924 | s->is_listen, s->is_telnet); |
2925 | } | |
6f97dba0 AL |
2926 | |
2927 | s->connected = 1; | |
2ea5a7af | 2928 | if (s->chan) { |
7ba9addc AS |
2929 | chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, |
2930 | tcp_chr_read, chr); | |
bbdd2ad0 | 2931 | } |
fee204fd | 2932 | qemu_chr_be_generic_open(chr); |
6f97dba0 AL |
2933 | } |
2934 | ||
ac1b84dd GH |
2935 | static void tcp_chr_update_read_handler(CharDriverState *chr) |
2936 | { | |
2937 | TCPCharDriver *s = chr->opaque; | |
2938 | ||
2939 | remove_fd_in_watch(chr); | |
2940 | if (s->chan) { | |
2941 | chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, | |
2942 | tcp_chr_read, chr); | |
2943 | } | |
2944 | } | |
2945 | ||
6f97dba0 AL |
2946 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
2947 | static void tcp_chr_telnet_init(int fd) | |
2948 | { | |
2949 | char buf[3]; | |
2950 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ | |
2951 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ | |
2952 | send(fd, (char *)buf, 3, 0); | |
2953 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ | |
2954 | send(fd, (char *)buf, 3, 0); | |
2955 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ | |
2956 | send(fd, (char *)buf, 3, 0); | |
2957 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ | |
2958 | send(fd, (char *)buf, 3, 0); | |
2959 | } | |
2960 | ||
13661089 DB |
2961 | static int tcp_chr_add_client(CharDriverState *chr, int fd) |
2962 | { | |
2963 | TCPCharDriver *s = chr->opaque; | |
2964 | if (s->fd != -1) | |
2965 | return -1; | |
2966 | ||
f9e8cacc | 2967 | qemu_set_nonblock(fd); |
13661089 DB |
2968 | if (s->do_nodelay) |
2969 | socket_set_nodelay(fd); | |
2970 | s->fd = fd; | |
2ea5a7af | 2971 | s->chan = io_channel_from_socket(fd); |
910b6368 PB |
2972 | if (s->listen_tag) { |
2973 | g_source_remove(s->listen_tag); | |
2974 | s->listen_tag = 0; | |
2975 | } | |
13661089 DB |
2976 | tcp_chr_connect(chr); |
2977 | ||
2978 | return 0; | |
2979 | } | |
2980 | ||
2ea5a7af | 2981 | static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2982 | { |
2983 | CharDriverState *chr = opaque; | |
2984 | TCPCharDriver *s = chr->opaque; | |
2985 | struct sockaddr_in saddr; | |
2986 | #ifndef _WIN32 | |
2987 | struct sockaddr_un uaddr; | |
2988 | #endif | |
2989 | struct sockaddr *addr; | |
2990 | socklen_t len; | |
2991 | int fd; | |
2992 | ||
2993 | for(;;) { | |
2994 | #ifndef _WIN32 | |
2995 | if (s->is_unix) { | |
2996 | len = sizeof(uaddr); | |
2997 | addr = (struct sockaddr *)&uaddr; | |
2998 | } else | |
2999 | #endif | |
3000 | { | |
3001 | len = sizeof(saddr); | |
3002 | addr = (struct sockaddr *)&saddr; | |
3003 | } | |
40ff6d7e | 3004 | fd = qemu_accept(s->listen_fd, addr, &len); |
6f97dba0 | 3005 | if (fd < 0 && errno != EINTR) { |
79f20075 | 3006 | s->listen_tag = 0; |
2ea5a7af | 3007 | return FALSE; |
6f97dba0 AL |
3008 | } else if (fd >= 0) { |
3009 | if (s->do_telnetopt) | |
3010 | tcp_chr_telnet_init(fd); | |
3011 | break; | |
3012 | } | |
3013 | } | |
13661089 DB |
3014 | if (tcp_chr_add_client(chr, fd) < 0) |
3015 | close(fd); | |
2ea5a7af AL |
3016 | |
3017 | return TRUE; | |
6f97dba0 AL |
3018 | } |
3019 | ||
3020 | static void tcp_chr_close(CharDriverState *chr) | |
3021 | { | |
3022 | TCPCharDriver *s = chr->opaque; | |
c76bf6bb | 3023 | int i; |
cfb429cb | 3024 | |
5dd1f02b CM |
3025 | if (s->reconnect_timer) { |
3026 | g_source_remove(s->reconnect_timer); | |
3027 | s->reconnect_timer = 0; | |
3028 | } | |
cfb429cb | 3029 | qapi_free_SocketAddress(s->addr); |
819f56b7 | 3030 | if (s->fd >= 0) { |
26da70c7 | 3031 | remove_fd_in_watch(chr); |
2ea5a7af AL |
3032 | if (s->chan) { |
3033 | g_io_channel_unref(s->chan); | |
3034 | } | |
6f97dba0 | 3035 | closesocket(s->fd); |
819f56b7 AL |
3036 | } |
3037 | if (s->listen_fd >= 0) { | |
2ea5a7af AL |
3038 | if (s->listen_tag) { |
3039 | g_source_remove(s->listen_tag); | |
910b6368 | 3040 | s->listen_tag = 0; |
2ea5a7af AL |
3041 | } |
3042 | if (s->listen_chan) { | |
3043 | g_io_channel_unref(s->listen_chan); | |
3044 | } | |
6f97dba0 | 3045 | closesocket(s->listen_fd); |
819f56b7 | 3046 | } |
c76bf6bb NN |
3047 | if (s->read_msgfds_num) { |
3048 | for (i = 0; i < s->read_msgfds_num; i++) { | |
3049 | close(s->read_msgfds[i]); | |
3050 | } | |
3051 | g_free(s->read_msgfds); | |
3052 | } | |
d39aac7a NN |
3053 | if (s->write_msgfds_num) { |
3054 | g_free(s->write_msgfds); | |
3055 | } | |
7267c094 | 3056 | g_free(s); |
a425d23f | 3057 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
3058 | } |
3059 | ||
16cc4ffe | 3060 | static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd) |
6f97dba0 | 3061 | { |
43ded1a0 | 3062 | TCPCharDriver *s = chr->opaque; |
f6bd5d6e | 3063 | |
cfb429cb | 3064 | if (s->is_listen) { |
f6bd5d6e | 3065 | s->listen_fd = fd; |
2ea5a7af | 3066 | s->listen_chan = io_channel_from_socket(s->listen_fd); |
43ded1a0 CM |
3067 | s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, |
3068 | tcp_chr_accept, chr); | |
f6bd5d6e GH |
3069 | } else { |
3070 | s->connected = 1; | |
3071 | s->fd = fd; | |
3072 | socket_set_nodelay(fd); | |
2ea5a7af | 3073 | s->chan = io_channel_from_socket(s->fd); |
f6bd5d6e GH |
3074 | tcp_chr_connect(chr); |
3075 | } | |
43ded1a0 CM |
3076 | } |
3077 | ||
51795029 | 3078 | static void qemu_chr_socket_connected(int fd, Error *err, void *opaque) |
5dd1f02b CM |
3079 | { |
3080 | CharDriverState *chr = opaque; | |
5008e5b7 | 3081 | TCPCharDriver *s = chr->opaque; |
5dd1f02b CM |
3082 | |
3083 | if (fd < 0) { | |
5008e5b7 | 3084 | check_report_connect_error(chr, err); |
5dd1f02b CM |
3085 | return; |
3086 | } | |
3087 | ||
5008e5b7 | 3088 | s->connect_err_reported = false; |
5dd1f02b CM |
3089 | qemu_chr_finish_socket_connection(chr, fd); |
3090 | } | |
3091 | ||
cfb429cb | 3092 | static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp) |
43ded1a0 | 3093 | { |
cfb429cb | 3094 | TCPCharDriver *s = chr->opaque; |
43ded1a0 CM |
3095 | int fd; |
3096 | ||
cfb429cb CM |
3097 | if (s->is_listen) { |
3098 | fd = socket_listen(s->addr, errp); | |
5dd1f02b CM |
3099 | } else if (s->reconnect_time) { |
3100 | fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr); | |
3101 | return fd >= 0; | |
3102 | } else { | |
cfb429cb | 3103 | fd = socket_connect(s->addr, errp, NULL, NULL); |
f6bd5d6e | 3104 | } |
43ded1a0 CM |
3105 | if (fd < 0) { |
3106 | return false; | |
3107 | } | |
3108 | ||
16cc4ffe CM |
3109 | qemu_chr_finish_socket_connection(chr, fd); |
3110 | return true; | |
f6bd5d6e GH |
3111 | } |
3112 | ||
51767e7c | 3113 | /*********************************************************/ |
3949e594 | 3114 | /* Ring buffer chardev */ |
51767e7c LL |
3115 | |
3116 | typedef struct { | |
3117 | size_t size; | |
3118 | size_t prod; | |
3119 | size_t cons; | |
3120 | uint8_t *cbuf; | |
3949e594 | 3121 | } RingBufCharDriver; |
51767e7c | 3122 | |
3949e594 | 3123 | static size_t ringbuf_count(const CharDriverState *chr) |
51767e7c | 3124 | { |
3949e594 | 3125 | const RingBufCharDriver *d = chr->opaque; |
51767e7c | 3126 | |
5c230105 | 3127 | return d->prod - d->cons; |
51767e7c LL |
3128 | } |
3129 | ||
9005b2a7 | 3130 | /* Called with chr_write_lock held. */ |
3949e594 | 3131 | static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
51767e7c | 3132 | { |
3949e594 | 3133 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
3134 | int i; |
3135 | ||
3136 | if (!buf || (len < 0)) { | |
3137 | return -1; | |
3138 | } | |
3139 | ||
3140 | for (i = 0; i < len; i++ ) { | |
5c230105 MA |
3141 | d->cbuf[d->prod++ & (d->size - 1)] = buf[i]; |
3142 | if (d->prod - d->cons > d->size) { | |
51767e7c LL |
3143 | d->cons = d->prod - d->size; |
3144 | } | |
3145 | } | |
3146 | ||
3147 | return 0; | |
3148 | } | |
3149 | ||
3949e594 | 3150 | static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len) |
51767e7c | 3151 | { |
3949e594 | 3152 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
3153 | int i; |
3154 | ||
9005b2a7 | 3155 | qemu_mutex_lock(&chr->chr_write_lock); |
5c230105 MA |
3156 | for (i = 0; i < len && d->cons != d->prod; i++) { |
3157 | buf[i] = d->cbuf[d->cons++ & (d->size - 1)]; | |
51767e7c | 3158 | } |
9005b2a7 | 3159 | qemu_mutex_unlock(&chr->chr_write_lock); |
51767e7c LL |
3160 | |
3161 | return i; | |
3162 | } | |
3163 | ||
3949e594 | 3164 | static void ringbuf_chr_close(struct CharDriverState *chr) |
51767e7c | 3165 | { |
3949e594 | 3166 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
3167 | |
3168 | g_free(d->cbuf); | |
3169 | g_free(d); | |
3170 | chr->opaque = NULL; | |
3171 | } | |
3172 | ||
4f57378f MA |
3173 | static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts, |
3174 | Error **errp) | |
51767e7c LL |
3175 | { |
3176 | CharDriverState *chr; | |
3949e594 | 3177 | RingBufCharDriver *d; |
51767e7c | 3178 | |
db39fcf1 | 3179 | chr = qemu_chr_alloc(); |
51767e7c LL |
3180 | d = g_malloc(sizeof(*d)); |
3181 | ||
1da48c65 | 3182 | d->size = opts->has_size ? opts->size : 65536; |
51767e7c LL |
3183 | |
3184 | /* The size must be power of 2 */ | |
3185 | if (d->size & (d->size - 1)) { | |
4f57378f | 3186 | error_setg(errp, "size of ringbuf chardev must be power of two"); |
51767e7c LL |
3187 | goto fail; |
3188 | } | |
3189 | ||
3190 | d->prod = 0; | |
3191 | d->cons = 0; | |
3192 | d->cbuf = g_malloc0(d->size); | |
3193 | ||
3194 | chr->opaque = d; | |
3949e594 MA |
3195 | chr->chr_write = ringbuf_chr_write; |
3196 | chr->chr_close = ringbuf_chr_close; | |
51767e7c LL |
3197 | |
3198 | return chr; | |
3199 | ||
3200 | fail: | |
3201 | g_free(d); | |
3202 | g_free(chr); | |
3203 | return NULL; | |
3204 | } | |
3205 | ||
8e597779 | 3206 | bool chr_is_ringbuf(const CharDriverState *chr) |
1f590cf9 | 3207 | { |
3949e594 | 3208 | return chr->chr_write == ringbuf_chr_write; |
1f590cf9 LL |
3209 | } |
3210 | ||
3949e594 | 3211 | void qmp_ringbuf_write(const char *device, const char *data, |
82e59a67 | 3212 | bool has_format, enum DataFormat format, |
1f590cf9 LL |
3213 | Error **errp) |
3214 | { | |
3215 | CharDriverState *chr; | |
c4f331b6 | 3216 | const uint8_t *write_data; |
1f590cf9 | 3217 | int ret; |
3d1bba20 | 3218 | gsize write_count; |
1f590cf9 LL |
3219 | |
3220 | chr = qemu_chr_find(device); | |
3221 | if (!chr) { | |
1a69278e | 3222 | error_setg(errp, "Device '%s' not found", device); |
1f590cf9 LL |
3223 | return; |
3224 | } | |
3225 | ||
3949e594 MA |
3226 | if (!chr_is_ringbuf(chr)) { |
3227 | error_setg(errp,"%s is not a ringbuf device", device); | |
1f590cf9 LL |
3228 | return; |
3229 | } | |
3230 | ||
1f590cf9 LL |
3231 | if (has_format && (format == DATA_FORMAT_BASE64)) { |
3232 | write_data = g_base64_decode(data, &write_count); | |
3233 | } else { | |
3234 | write_data = (uint8_t *)data; | |
82e59a67 | 3235 | write_count = strlen(data); |
1f590cf9 LL |
3236 | } |
3237 | ||
3949e594 | 3238 | ret = ringbuf_chr_write(chr, write_data, write_count); |
1f590cf9 | 3239 | |
13289fb5 MA |
3240 | if (write_data != (uint8_t *)data) { |
3241 | g_free((void *)write_data); | |
3242 | } | |
3243 | ||
1f590cf9 LL |
3244 | if (ret < 0) { |
3245 | error_setg(errp, "Failed to write to device %s", device); | |
3246 | return; | |
3247 | } | |
3248 | } | |
3249 | ||
3949e594 | 3250 | char *qmp_ringbuf_read(const char *device, int64_t size, |
3ab651fc MA |
3251 | bool has_format, enum DataFormat format, |
3252 | Error **errp) | |
49b6d722 LL |
3253 | { |
3254 | CharDriverState *chr; | |
c4f331b6 | 3255 | uint8_t *read_data; |
49b6d722 | 3256 | size_t count; |
3ab651fc | 3257 | char *data; |
49b6d722 LL |
3258 | |
3259 | chr = qemu_chr_find(device); | |
3260 | if (!chr) { | |
1a69278e | 3261 | error_setg(errp, "Device '%s' not found", device); |
49b6d722 LL |
3262 | return NULL; |
3263 | } | |
3264 | ||
3949e594 MA |
3265 | if (!chr_is_ringbuf(chr)) { |
3266 | error_setg(errp,"%s is not a ringbuf device", device); | |
49b6d722 LL |
3267 | return NULL; |
3268 | } | |
3269 | ||
3270 | if (size <= 0) { | |
3271 | error_setg(errp, "size must be greater than zero"); | |
3272 | return NULL; | |
3273 | } | |
3274 | ||
3949e594 | 3275 | count = ringbuf_count(chr); |
49b6d722 | 3276 | size = size > count ? count : size; |
44f3bcd2 | 3277 | read_data = g_malloc(size + 1); |
49b6d722 | 3278 | |
3949e594 | 3279 | ringbuf_chr_read(chr, read_data, size); |
49b6d722 LL |
3280 | |
3281 | if (has_format && (format == DATA_FORMAT_BASE64)) { | |
3ab651fc | 3282 | data = g_base64_encode(read_data, size); |
13289fb5 | 3283 | g_free(read_data); |
49b6d722 | 3284 | } else { |
3949e594 MA |
3285 | /* |
3286 | * FIXME should read only complete, valid UTF-8 characters up | |
3287 | * to @size bytes. Invalid sequences should be replaced by a | |
3288 | * suitable replacement character. Except when (and only | |
3289 | * when) ring buffer lost characters since last read, initial | |
3290 | * continuation characters should be dropped. | |
3291 | */ | |
44f3bcd2 | 3292 | read_data[size] = 0; |
3ab651fc | 3293 | data = (char *)read_data; |
49b6d722 LL |
3294 | } |
3295 | ||
3ab651fc | 3296 | return data; |
49b6d722 LL |
3297 | } |
3298 | ||
33521634 | 3299 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
191bc01b | 3300 | { |
6ea314d9 | 3301 | char host[65], port[33], width[8], height[8]; |
aeb2c47a | 3302 | int pos; |
7d31544f | 3303 | const char *p; |
191bc01b | 3304 | QemuOpts *opts; |
8be7e7e4 | 3305 | Error *local_err = NULL; |
191bc01b | 3306 | |
8be7e7e4 | 3307 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); |
84d18f06 | 3308 | if (local_err) { |
33394884 | 3309 | error_report_err(local_err); |
191bc01b | 3310 | return NULL; |
8be7e7e4 | 3311 | } |
191bc01b | 3312 | |
7591c5c1 GH |
3313 | if (strstart(filename, "mon:", &p)) { |
3314 | filename = p; | |
f43e47db | 3315 | qemu_opt_set(opts, "mux", "on", &error_abort); |
02c4bdf1 PB |
3316 | if (strcmp(filename, "stdio") == 0) { |
3317 | /* Monitor is muxed to stdio: do not exit on Ctrl+C by default | |
3318 | * but pass it to the guest. Handle this only for compat syntax, | |
3319 | * for -chardev syntax we have special option for this. | |
3320 | * This is what -nographic did, redirecting+muxing serial+monitor | |
3321 | * to stdio causing Ctrl+C to be passed to guest. */ | |
f43e47db | 3322 | qemu_opt_set(opts, "signal", "off", &error_abort); |
02c4bdf1 | 3323 | } |
7591c5c1 GH |
3324 | } |
3325 | ||
f0457e8d GH |
3326 | if (strcmp(filename, "null") == 0 || |
3327 | strcmp(filename, "pty") == 0 || | |
3328 | strcmp(filename, "msmouse") == 0 || | |
dc1c21e6 | 3329 | strcmp(filename, "braille") == 0 || |
5692399f | 3330 | strcmp(filename, "testdev") == 0 || |
f0457e8d | 3331 | strcmp(filename, "stdio") == 0) { |
f43e47db | 3332 | qemu_opt_set(opts, "backend", filename, &error_abort); |
191bc01b GH |
3333 | return opts; |
3334 | } | |
6ea314d9 | 3335 | if (strstart(filename, "vc", &p)) { |
f43e47db | 3336 | qemu_opt_set(opts, "backend", "vc", &error_abort); |
6ea314d9 | 3337 | if (*p == ':') { |
49aa4058 | 3338 | if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { |
6ea314d9 | 3339 | /* pixels */ |
f43e47db MA |
3340 | qemu_opt_set(opts, "width", width, &error_abort); |
3341 | qemu_opt_set(opts, "height", height, &error_abort); | |
49aa4058 | 3342 | } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { |
6ea314d9 | 3343 | /* chars */ |
f43e47db MA |
3344 | qemu_opt_set(opts, "cols", width, &error_abort); |
3345 | qemu_opt_set(opts, "rows", height, &error_abort); | |
6ea314d9 GH |
3346 | } else { |
3347 | goto fail; | |
3348 | } | |
3349 | } | |
3350 | return opts; | |
3351 | } | |
d6c983cd | 3352 | if (strcmp(filename, "con:") == 0) { |
f43e47db | 3353 | qemu_opt_set(opts, "backend", "console", &error_abort); |
d6c983cd GH |
3354 | return opts; |
3355 | } | |
48b76496 | 3356 | if (strstart(filename, "COM", NULL)) { |
f43e47db MA |
3357 | qemu_opt_set(opts, "backend", "serial", &error_abort); |
3358 | qemu_opt_set(opts, "path", filename, &error_abort); | |
48b76496 GH |
3359 | return opts; |
3360 | } | |
7d31544f | 3361 | if (strstart(filename, "file:", &p)) { |
f43e47db MA |
3362 | qemu_opt_set(opts, "backend", "file", &error_abort); |
3363 | qemu_opt_set(opts, "path", p, &error_abort); | |
7d31544f GH |
3364 | return opts; |
3365 | } | |
3366 | if (strstart(filename, "pipe:", &p)) { | |
f43e47db MA |
3367 | qemu_opt_set(opts, "backend", "pipe", &error_abort); |
3368 | qemu_opt_set(opts, "path", p, &error_abort); | |
7d31544f GH |
3369 | return opts; |
3370 | } | |
aeb2c47a GH |
3371 | if (strstart(filename, "tcp:", &p) || |
3372 | strstart(filename, "telnet:", &p)) { | |
3373 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
3374 | host[0] = 0; | |
3375 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) | |
3376 | goto fail; | |
3377 | } | |
f43e47db MA |
3378 | qemu_opt_set(opts, "backend", "socket", &error_abort); |
3379 | qemu_opt_set(opts, "host", host, &error_abort); | |
3380 | qemu_opt_set(opts, "port", port, &error_abort); | |
aeb2c47a | 3381 | if (p[pos] == ',') { |
dc523cd3 MA |
3382 | qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err); |
3383 | if (local_err) { | |
3384 | error_report_err(local_err); | |
aeb2c47a | 3385 | goto fail; |
dc523cd3 | 3386 | } |
aeb2c47a GH |
3387 | } |
3388 | if (strstart(filename, "telnet:", &p)) | |
f43e47db | 3389 | qemu_opt_set(opts, "telnet", "on", &error_abort); |
aeb2c47a GH |
3390 | return opts; |
3391 | } | |
7e1b35b4 | 3392 | if (strstart(filename, "udp:", &p)) { |
f43e47db | 3393 | qemu_opt_set(opts, "backend", "udp", &error_abort); |
7e1b35b4 GH |
3394 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { |
3395 | host[0] = 0; | |
39324ca4 | 3396 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
7e1b35b4 GH |
3397 | goto fail; |
3398 | } | |
3399 | } | |
f43e47db MA |
3400 | qemu_opt_set(opts, "host", host, &error_abort); |
3401 | qemu_opt_set(opts, "port", port, &error_abort); | |
7e1b35b4 GH |
3402 | if (p[pos] == '@') { |
3403 | p += pos + 1; | |
3404 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
3405 | host[0] = 0; | |
3406 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { | |
7e1b35b4 GH |
3407 | goto fail; |
3408 | } | |
3409 | } | |
f43e47db MA |
3410 | qemu_opt_set(opts, "localaddr", host, &error_abort); |
3411 | qemu_opt_set(opts, "localport", port, &error_abort); | |
7e1b35b4 GH |
3412 | } |
3413 | return opts; | |
3414 | } | |
aeb2c47a | 3415 | if (strstart(filename, "unix:", &p)) { |
f43e47db | 3416 | qemu_opt_set(opts, "backend", "socket", &error_abort); |
dc523cd3 MA |
3417 | qemu_opts_do_parse(opts, p, "path", &local_err); |
3418 | if (local_err) { | |
3419 | error_report_err(local_err); | |
aeb2c47a | 3420 | goto fail; |
dc523cd3 | 3421 | } |
aeb2c47a GH |
3422 | return opts; |
3423 | } | |
48b76496 GH |
3424 | if (strstart(filename, "/dev/parport", NULL) || |
3425 | strstart(filename, "/dev/ppi", NULL)) { | |
f43e47db MA |
3426 | qemu_opt_set(opts, "backend", "parport", &error_abort); |
3427 | qemu_opt_set(opts, "path", filename, &error_abort); | |
48b76496 GH |
3428 | return opts; |
3429 | } | |
3430 | if (strstart(filename, "/dev/", NULL)) { | |
f43e47db MA |
3431 | qemu_opt_set(opts, "backend", "tty", &error_abort); |
3432 | qemu_opt_set(opts, "path", filename, &error_abort); | |
48b76496 GH |
3433 | return opts; |
3434 | } | |
191bc01b | 3435 | |
aeb2c47a | 3436 | fail: |
191bc01b GH |
3437 | qemu_opts_del(opts); |
3438 | return NULL; | |
3439 | } | |
3440 | ||
846e2e49 GH |
3441 | static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend, |
3442 | Error **errp) | |
3443 | { | |
3444 | const char *path = qemu_opt_get(opts, "path"); | |
3445 | ||
3446 | if (path == NULL) { | |
3447 | error_setg(errp, "chardev: file: no filename given"); | |
3448 | return; | |
3449 | } | |
3450 | backend->file = g_new0(ChardevFile, 1); | |
3451 | backend->file->out = g_strdup(path); | |
3452 | } | |
3453 | ||
7c358031 GH |
3454 | static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend, |
3455 | Error **errp) | |
3456 | { | |
3457 | backend->stdio = g_new0(ChardevStdio, 1); | |
3458 | backend->stdio->has_signal = true; | |
02c4bdf1 | 3459 | backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true); |
7c358031 GH |
3460 | } |
3461 | ||
0f1cb51d GH |
3462 | static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend, |
3463 | Error **errp) | |
3464 | { | |
3465 | const char *device = qemu_opt_get(opts, "path"); | |
3466 | ||
3467 | if (device == NULL) { | |
3468 | error_setg(errp, "chardev: serial/tty: no device path given"); | |
3469 | return; | |
3470 | } | |
3471 | backend->serial = g_new0(ChardevHostdev, 1); | |
3472 | backend->serial->device = g_strdup(device); | |
3473 | } | |
3474 | ||
dc375097 GH |
3475 | static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend, |
3476 | Error **errp) | |
3477 | { | |
3478 | const char *device = qemu_opt_get(opts, "path"); | |
3479 | ||
3480 | if (device == NULL) { | |
3481 | error_setg(errp, "chardev: parallel: no device path given"); | |
3482 | return; | |
3483 | } | |
3484 | backend->parallel = g_new0(ChardevHostdev, 1); | |
3485 | backend->parallel->device = g_strdup(device); | |
3486 | } | |
3487 | ||
548cbb36 GH |
3488 | static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, |
3489 | Error **errp) | |
3490 | { | |
3491 | const char *device = qemu_opt_get(opts, "path"); | |
3492 | ||
3493 | if (device == NULL) { | |
3494 | error_setg(errp, "chardev: pipe: no device path given"); | |
3495 | return; | |
3496 | } | |
3497 | backend->pipe = g_new0(ChardevHostdev, 1); | |
3498 | backend->pipe->device = g_strdup(device); | |
3499 | } | |
3500 | ||
4f57378f MA |
3501 | static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend, |
3502 | Error **errp) | |
1da48c65 GH |
3503 | { |
3504 | int val; | |
3505 | ||
3a1da42e | 3506 | backend->ringbuf = g_new0(ChardevRingbuf, 1); |
1da48c65 | 3507 | |
0f953051 | 3508 | val = qemu_opt_get_size(opts, "size", 0); |
1da48c65 | 3509 | if (val != 0) { |
3a1da42e MA |
3510 | backend->ringbuf->has_size = true; |
3511 | backend->ringbuf->size = val; | |
1da48c65 GH |
3512 | } |
3513 | } | |
3514 | ||
bb6fb7c0 GH |
3515 | static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend, |
3516 | Error **errp) | |
3517 | { | |
3518 | const char *chardev = qemu_opt_get(opts, "chardev"); | |
3519 | ||
3520 | if (chardev == NULL) { | |
3521 | error_setg(errp, "chardev: mux: no chardev given"); | |
3522 | return; | |
3523 | } | |
3524 | backend->mux = g_new0(ChardevMux, 1); | |
3525 | backend->mux->chardev = g_strdup(chardev); | |
3526 | } | |
3527 | ||
dafd325d PM |
3528 | static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, |
3529 | Error **errp) | |
3530 | { | |
3531 | bool is_listen = qemu_opt_get_bool(opts, "server", false); | |
3532 | bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true); | |
3533 | bool is_telnet = qemu_opt_get_bool(opts, "telnet", false); | |
3534 | bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true); | |
5dd1f02b | 3535 | int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0); |
dafd325d PM |
3536 | const char *path = qemu_opt_get(opts, "path"); |
3537 | const char *host = qemu_opt_get(opts, "host"); | |
3538 | const char *port = qemu_opt_get(opts, "port"); | |
3539 | SocketAddress *addr; | |
3540 | ||
3541 | if (!path) { | |
3542 | if (!host) { | |
3543 | error_setg(errp, "chardev: socket: no host given"); | |
3544 | return; | |
3545 | } | |
3546 | if (!port) { | |
3547 | error_setg(errp, "chardev: socket: no port given"); | |
3548 | return; | |
3549 | } | |
3550 | } | |
3551 | ||
3552 | backend->socket = g_new0(ChardevSocket, 1); | |
3553 | ||
3554 | backend->socket->has_nodelay = true; | |
3555 | backend->socket->nodelay = do_nodelay; | |
3556 | backend->socket->has_server = true; | |
3557 | backend->socket->server = is_listen; | |
3558 | backend->socket->has_telnet = true; | |
3559 | backend->socket->telnet = is_telnet; | |
3560 | backend->socket->has_wait = true; | |
3561 | backend->socket->wait = is_waitconnect; | |
5dd1f02b CM |
3562 | backend->socket->has_reconnect = true; |
3563 | backend->socket->reconnect = reconnect; | |
dafd325d PM |
3564 | |
3565 | addr = g_new0(SocketAddress, 1); | |
3566 | if (path) { | |
3567 | addr->kind = SOCKET_ADDRESS_KIND_UNIX; | |
3568 | addr->q_unix = g_new0(UnixSocketAddress, 1); | |
3569 | addr->q_unix->path = g_strdup(path); | |
3570 | } else { | |
3571 | addr->kind = SOCKET_ADDRESS_KIND_INET; | |
3572 | addr->inet = g_new0(InetSocketAddress, 1); | |
3573 | addr->inet->host = g_strdup(host); | |
3574 | addr->inet->port = g_strdup(port); | |
3575 | addr->inet->has_to = qemu_opt_get(opts, "to"); | |
3576 | addr->inet->to = qemu_opt_get_number(opts, "to", 0); | |
3577 | addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); | |
3578 | addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); | |
3579 | addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); | |
3580 | addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); | |
3581 | } | |
3582 | backend->socket->addr = addr; | |
3583 | } | |
3584 | ||
90a14bfe PM |
3585 | static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, |
3586 | Error **errp) | |
3587 | { | |
3588 | const char *host = qemu_opt_get(opts, "host"); | |
3589 | const char *port = qemu_opt_get(opts, "port"); | |
3590 | const char *localaddr = qemu_opt_get(opts, "localaddr"); | |
3591 | const char *localport = qemu_opt_get(opts, "localport"); | |
3592 | bool has_local = false; | |
3593 | SocketAddress *addr; | |
3594 | ||
3595 | if (host == NULL || strlen(host) == 0) { | |
3596 | host = "localhost"; | |
3597 | } | |
3598 | if (port == NULL || strlen(port) == 0) { | |
3599 | error_setg(errp, "chardev: udp: remote port not specified"); | |
3600 | return; | |
3601 | } | |
3602 | if (localport == NULL || strlen(localport) == 0) { | |
3603 | localport = "0"; | |
3604 | } else { | |
3605 | has_local = true; | |
3606 | } | |
3607 | if (localaddr == NULL || strlen(localaddr) == 0) { | |
3608 | localaddr = ""; | |
3609 | } else { | |
3610 | has_local = true; | |
3611 | } | |
3612 | ||
3613 | backend->udp = g_new0(ChardevUdp, 1); | |
3614 | ||
3615 | addr = g_new0(SocketAddress, 1); | |
3616 | addr->kind = SOCKET_ADDRESS_KIND_INET; | |
3617 | addr->inet = g_new0(InetSocketAddress, 1); | |
3618 | addr->inet->host = g_strdup(host); | |
3619 | addr->inet->port = g_strdup(port); | |
3620 | addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); | |
3621 | addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); | |
3622 | addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); | |
3623 | addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); | |
3624 | backend->udp->remote = addr; | |
3625 | ||
3626 | if (has_local) { | |
3627 | backend->udp->has_local = true; | |
3628 | addr = g_new0(SocketAddress, 1); | |
3629 | addr->kind = SOCKET_ADDRESS_KIND_INET; | |
3630 | addr->inet = g_new0(InetSocketAddress, 1); | |
3631 | addr->inet->host = g_strdup(localaddr); | |
3632 | addr->inet->port = g_strdup(localport); | |
3633 | backend->udp->local = addr; | |
3634 | } | |
3635 | } | |
3636 | ||
d654f34e | 3637 | typedef struct CharDriver { |
191bc01b | 3638 | const char *name; |
99aec012 | 3639 | ChardevBackendKind kind; |
2c5f4882 | 3640 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); |
d654f34e AL |
3641 | } CharDriver; |
3642 | ||
3643 | static GSList *backends; | |
3644 | ||
e4d50d47 | 3645 | void register_char_driver(const char *name, ChardevBackendKind kind, |
2c5f4882 GH |
3646 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp)) |
3647 | { | |
3648 | CharDriver *s; | |
3649 | ||
3650 | s = g_malloc0(sizeof(*s)); | |
3651 | s->name = g_strdup(name); | |
3652 | s->kind = kind; | |
3653 | s->parse = parse; | |
3654 | ||
3655 | backends = g_slist_append(backends, s); | |
3656 | } | |
3657 | ||
f69554b9 | 3658 | CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, |
bd2d80b2 GH |
3659 | void (*init)(struct CharDriverState *s), |
3660 | Error **errp) | |
191bc01b | 3661 | { |
0aff637e | 3662 | Error *local_err = NULL; |
d654f34e | 3663 | CharDriver *cd; |
191bc01b | 3664 | CharDriverState *chr; |
d654f34e | 3665 | GSList *i; |
a61ae7f8 PM |
3666 | ChardevReturn *ret = NULL; |
3667 | ChardevBackend *backend; | |
3668 | const char *id = qemu_opts_id(opts); | |
3669 | char *bid = NULL; | |
191bc01b | 3670 | |
a61ae7f8 | 3671 | if (id == NULL) { |
312fd5f2 | 3672 | error_setg(errp, "chardev: no id specified"); |
2274ae9d | 3673 | goto err; |
191bc01b GH |
3674 | } |
3675 | ||
1bbd185f | 3676 | if (qemu_opt_get(opts, "backend") == NULL) { |
312fd5f2 | 3677 | error_setg(errp, "chardev: \"%s\" missing backend", |
bd2d80b2 | 3678 | qemu_opts_id(opts)); |
2274ae9d | 3679 | goto err; |
1bbd185f | 3680 | } |
d654f34e AL |
3681 | for (i = backends; i; i = i->next) { |
3682 | cd = i->data; | |
3683 | ||
3684 | if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) { | |
191bc01b | 3685 | break; |
d654f34e | 3686 | } |
191bc01b | 3687 | } |
d654f34e | 3688 | if (i == NULL) { |
312fd5f2 | 3689 | error_setg(errp, "chardev: backend \"%s\" not found", |
bd2d80b2 | 3690 | qemu_opt_get(opts, "backend")); |
e668287d | 3691 | goto err; |
191bc01b GH |
3692 | } |
3693 | ||
a61ae7f8 | 3694 | backend = g_new0(ChardevBackend, 1); |
edb2fb3c | 3695 | |
a61ae7f8 PM |
3696 | if (qemu_opt_get_bool(opts, "mux", 0)) { |
3697 | bid = g_strdup_printf("%s-base", id); | |
3698 | } | |
2c5f4882 | 3699 | |
a61ae7f8 PM |
3700 | chr = NULL; |
3701 | backend->kind = cd->kind; | |
3702 | if (cd->parse) { | |
3703 | cd->parse(opts, backend, &local_err); | |
3704 | if (local_err) { | |
3705 | error_propagate(errp, local_err); | |
2c5f4882 GH |
3706 | goto qapi_out; |
3707 | } | |
2c5f4882 | 3708 | } |
a61ae7f8 PM |
3709 | ret = qmp_chardev_add(bid ? bid : id, backend, errp); |
3710 | if (!ret) { | |
3711 | goto qapi_out; | |
191bc01b GH |
3712 | } |
3713 | ||
a61ae7f8 PM |
3714 | if (bid) { |
3715 | qapi_free_ChardevBackend(backend); | |
3716 | qapi_free_ChardevReturn(ret); | |
3717 | backend = g_new0(ChardevBackend, 1); | |
3718 | backend->mux = g_new0(ChardevMux, 1); | |
3719 | backend->kind = CHARDEV_BACKEND_KIND_MUX; | |
3720 | backend->mux->chardev = g_strdup(bid); | |
3721 | ret = qmp_chardev_add(id, backend, errp); | |
3722 | if (!ret) { | |
3723 | chr = qemu_chr_find(bid); | |
3724 | qemu_chr_delete(chr); | |
3725 | chr = NULL; | |
3726 | goto qapi_out; | |
3727 | } | |
bd5c51ee | 3728 | } |
7591c5c1 | 3729 | |
a61ae7f8 | 3730 | chr = qemu_chr_find(id); |
2274ae9d | 3731 | chr->opts = opts; |
a61ae7f8 PM |
3732 | |
3733 | qapi_out: | |
3734 | qapi_free_ChardevBackend(backend); | |
3735 | qapi_free_ChardevReturn(ret); | |
3736 | g_free(bid); | |
191bc01b | 3737 | return chr; |
2274ae9d GH |
3738 | |
3739 | err: | |
3740 | qemu_opts_del(opts); | |
3741 | return NULL; | |
191bc01b GH |
3742 | } |
3743 | ||
27143a44 | 3744 | CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s)) |
6f97dba0 AL |
3745 | { |
3746 | const char *p; | |
3747 | CharDriverState *chr; | |
191bc01b | 3748 | QemuOpts *opts; |
bd2d80b2 | 3749 | Error *err = NULL; |
191bc01b | 3750 | |
c845f401 GH |
3751 | if (strstart(filename, "chardev:", &p)) { |
3752 | return qemu_chr_find(p); | |
3753 | } | |
3754 | ||
191bc01b | 3755 | opts = qemu_chr_parse_compat(label, filename); |
7e1b35b4 GH |
3756 | if (!opts) |
3757 | return NULL; | |
6f97dba0 | 3758 | |
bd2d80b2 | 3759 | chr = qemu_chr_new_from_opts(opts, init, &err); |
84d18f06 | 3760 | if (err) { |
565f65d2 | 3761 | error_report_err(err); |
bd2d80b2 | 3762 | } |
7e1b35b4 | 3763 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
456d6069 | 3764 | qemu_chr_fe_claim_no_fail(chr); |
7e1b35b4 | 3765 | monitor_init(chr, MONITOR_USE_READLINE); |
6f97dba0 AL |
3766 | } |
3767 | return chr; | |
3768 | } | |
3769 | ||
15f31519 | 3770 | void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo) |
c48855e1 PB |
3771 | { |
3772 | if (chr->chr_set_echo) { | |
3773 | chr->chr_set_echo(chr, echo); | |
3774 | } | |
3775 | } | |
3776 | ||
8e25daa8 | 3777 | void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open) |
7c32c4fe | 3778 | { |
8e25daa8 | 3779 | if (chr->fe_open == fe_open) { |
c0c4bd2c HG |
3780 | return; |
3781 | } | |
8e25daa8 | 3782 | chr->fe_open = fe_open; |
574b711a HG |
3783 | if (chr->chr_set_fe_open) { |
3784 | chr->chr_set_fe_open(chr, fe_open); | |
7c32c4fe HG |
3785 | } |
3786 | } | |
3787 | ||
d61b0c9a MAL |
3788 | void qemu_chr_fe_event(struct CharDriverState *chr, int event) |
3789 | { | |
3790 | if (chr->chr_fe_event) { | |
3791 | chr->chr_fe_event(chr, event); | |
3792 | } | |
3793 | } | |
3794 | ||
2c8a5942 KW |
3795 | int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond, |
3796 | GIOFunc func, void *user_data) | |
23673ca7 AL |
3797 | { |
3798 | GSource *src; | |
3799 | guint tag; | |
3800 | ||
3801 | if (s->chr_add_watch == NULL) { | |
3802 | return -ENOSYS; | |
3803 | } | |
3804 | ||
3805 | src = s->chr_add_watch(s, cond); | |
62c339c5 PB |
3806 | if (!src) { |
3807 | return -EINVAL; | |
3808 | } | |
3809 | ||
23673ca7 AL |
3810 | g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); |
3811 | tag = g_source_attach(src, NULL); | |
3812 | g_source_unref(src); | |
3813 | ||
3814 | return tag; | |
3815 | } | |
3816 | ||
44c473de HG |
3817 | int qemu_chr_fe_claim(CharDriverState *s) |
3818 | { | |
3819 | if (s->avail_connections < 1) { | |
3820 | return -1; | |
3821 | } | |
3822 | s->avail_connections--; | |
3823 | return 0; | |
3824 | } | |
3825 | ||
3826 | void qemu_chr_fe_claim_no_fail(CharDriverState *s) | |
3827 | { | |
3828 | if (qemu_chr_fe_claim(s) != 0) { | |
3829 | fprintf(stderr, "%s: error chardev \"%s\" already used\n", | |
3830 | __func__, s->label); | |
3831 | exit(1); | |
3832 | } | |
3833 | } | |
3834 | ||
3835 | void qemu_chr_fe_release(CharDriverState *s) | |
3836 | { | |
3837 | s->avail_connections++; | |
3838 | } | |
3839 | ||
70f24fb6 | 3840 | void qemu_chr_delete(CharDriverState *chr) |
6f97dba0 | 3841 | { |
72cf2d4f | 3842 | QTAILQ_REMOVE(&chardevs, chr, next); |
2274ae9d | 3843 | if (chr->chr_close) { |
6f97dba0 | 3844 | chr->chr_close(chr); |
2274ae9d | 3845 | } |
7267c094 AL |
3846 | g_free(chr->filename); |
3847 | g_free(chr->label); | |
4ad60880 | 3848 | qemu_opts_del(chr->opts); |
7267c094 | 3849 | g_free(chr); |
6f97dba0 AL |
3850 | } |
3851 | ||
c5a415a0 | 3852 | ChardevInfoList *qmp_query_chardev(Error **errp) |
6f97dba0 | 3853 | { |
c5a415a0 | 3854 | ChardevInfoList *chr_list = NULL; |
6f97dba0 AL |
3855 | CharDriverState *chr; |
3856 | ||
72cf2d4f | 3857 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c5a415a0 LC |
3858 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
3859 | info->value = g_malloc0(sizeof(*info->value)); | |
3860 | info->value->label = g_strdup(chr->label); | |
3861 | info->value->filename = g_strdup(chr->filename); | |
32a97ea1 | 3862 | info->value->frontend_open = chr->fe_open; |
c5a415a0 LC |
3863 | |
3864 | info->next = chr_list; | |
3865 | chr_list = info; | |
6f97dba0 | 3866 | } |
588b3832 | 3867 | |
c5a415a0 | 3868 | return chr_list; |
6f97dba0 | 3869 | } |
c845f401 | 3870 | |
77d1c3c6 MK |
3871 | ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) |
3872 | { | |
3873 | ChardevBackendInfoList *backend_list = NULL; | |
3874 | CharDriver *c = NULL; | |
3875 | GSList *i = NULL; | |
3876 | ||
3877 | for (i = backends; i; i = i->next) { | |
3878 | ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); | |
3879 | c = i->data; | |
3880 | info->value = g_malloc0(sizeof(*info->value)); | |
3881 | info->value->name = g_strdup(c->name); | |
3882 | ||
3883 | info->next = backend_list; | |
3884 | backend_list = info; | |
3885 | } | |
3886 | ||
3887 | return backend_list; | |
3888 | } | |
3889 | ||
c845f401 GH |
3890 | CharDriverState *qemu_chr_find(const char *name) |
3891 | { | |
3892 | CharDriverState *chr; | |
3893 | ||
72cf2d4f | 3894 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c845f401 GH |
3895 | if (strcmp(chr->label, name) != 0) |
3896 | continue; | |
3897 | return chr; | |
3898 | } | |
3899 | return NULL; | |
3900 | } | |
0beb4942 AL |
3901 | |
3902 | /* Get a character (serial) device interface. */ | |
3903 | CharDriverState *qemu_char_get_next_serial(void) | |
3904 | { | |
3905 | static int next_serial; | |
456d6069 | 3906 | CharDriverState *chr; |
0beb4942 AL |
3907 | |
3908 | /* FIXME: This function needs to go away: use chardev properties! */ | |
456d6069 HG |
3909 | |
3910 | while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) { | |
3911 | chr = serial_hds[next_serial++]; | |
3912 | qemu_chr_fe_claim_no_fail(chr); | |
3913 | return chr; | |
3914 | } | |
3915 | return NULL; | |
0beb4942 AL |
3916 | } |
3917 | ||
4d454574 PB |
3918 | QemuOptsList qemu_chardev_opts = { |
3919 | .name = "chardev", | |
3920 | .implied_opt_name = "backend", | |
3921 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), | |
3922 | .desc = { | |
3923 | { | |
3924 | .name = "backend", | |
3925 | .type = QEMU_OPT_STRING, | |
3926 | },{ | |
3927 | .name = "path", | |
3928 | .type = QEMU_OPT_STRING, | |
3929 | },{ | |
3930 | .name = "host", | |
3931 | .type = QEMU_OPT_STRING, | |
3932 | },{ | |
3933 | .name = "port", | |
3934 | .type = QEMU_OPT_STRING, | |
3935 | },{ | |
3936 | .name = "localaddr", | |
3937 | .type = QEMU_OPT_STRING, | |
3938 | },{ | |
3939 | .name = "localport", | |
3940 | .type = QEMU_OPT_STRING, | |
3941 | },{ | |
3942 | .name = "to", | |
3943 | .type = QEMU_OPT_NUMBER, | |
3944 | },{ | |
3945 | .name = "ipv4", | |
3946 | .type = QEMU_OPT_BOOL, | |
3947 | },{ | |
3948 | .name = "ipv6", | |
3949 | .type = QEMU_OPT_BOOL, | |
3950 | },{ | |
3951 | .name = "wait", | |
3952 | .type = QEMU_OPT_BOOL, | |
3953 | },{ | |
3954 | .name = "server", | |
3955 | .type = QEMU_OPT_BOOL, | |
3956 | },{ | |
3957 | .name = "delay", | |
3958 | .type = QEMU_OPT_BOOL, | |
5dd1f02b CM |
3959 | },{ |
3960 | .name = "reconnect", | |
3961 | .type = QEMU_OPT_NUMBER, | |
4d454574 PB |
3962 | },{ |
3963 | .name = "telnet", | |
3964 | .type = QEMU_OPT_BOOL, | |
3965 | },{ | |
3966 | .name = "width", | |
3967 | .type = QEMU_OPT_NUMBER, | |
3968 | },{ | |
3969 | .name = "height", | |
3970 | .type = QEMU_OPT_NUMBER, | |
3971 | },{ | |
3972 | .name = "cols", | |
3973 | .type = QEMU_OPT_NUMBER, | |
3974 | },{ | |
3975 | .name = "rows", | |
3976 | .type = QEMU_OPT_NUMBER, | |
3977 | },{ | |
3978 | .name = "mux", | |
3979 | .type = QEMU_OPT_BOOL, | |
3980 | },{ | |
3981 | .name = "signal", | |
3982 | .type = QEMU_OPT_BOOL, | |
3983 | },{ | |
3984 | .name = "name", | |
3985 | .type = QEMU_OPT_STRING, | |
3986 | },{ | |
3987 | .name = "debug", | |
3988 | .type = QEMU_OPT_NUMBER, | |
51767e7c | 3989 | },{ |
3949e594 | 3990 | .name = "size", |
de1cc36e | 3991 | .type = QEMU_OPT_SIZE, |
bb6fb7c0 GH |
3992 | },{ |
3993 | .name = "chardev", | |
3994 | .type = QEMU_OPT_STRING, | |
4d454574 PB |
3995 | }, |
3996 | { /* end of list */ } | |
3997 | }, | |
3998 | }; | |
f1a1a356 | 3999 | |
ffbdbe59 GH |
4000 | #ifdef _WIN32 |
4001 | ||
4002 | static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) | |
4003 | { | |
4004 | HANDLE out; | |
4005 | ||
e859eda5 | 4006 | if (file->has_in) { |
ffbdbe59 GH |
4007 | error_setg(errp, "input file not supported"); |
4008 | return NULL; | |
4009 | } | |
4010 | ||
4011 | out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL, | |
4012 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
4013 | if (out == INVALID_HANDLE_VALUE) { | |
4014 | error_setg(errp, "open %s failed", file->out); | |
4015 | return NULL; | |
4016 | } | |
4017 | return qemu_chr_open_win_file(out); | |
4018 | } | |
4019 | ||
d36b2b90 MA |
4020 | static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial, |
4021 | Error **errp) | |
d59044ef | 4022 | { |
d36b2b90 MA |
4023 | return qemu_chr_open_win_path(serial->device); |
4024 | } | |
4025 | ||
4026 | static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel, | |
4027 | Error **errp) | |
4028 | { | |
4029 | error_setg(errp, "character device backend type 'parallel' not supported"); | |
4030 | return NULL; | |
d59044ef GH |
4031 | } |
4032 | ||
ffbdbe59 GH |
4033 | #else /* WIN32 */ |
4034 | ||
4035 | static int qmp_chardev_open_file_source(char *src, int flags, | |
4036 | Error **errp) | |
4037 | { | |
4038 | int fd = -1; | |
4039 | ||
4040 | TFR(fd = qemu_open(src, flags, 0666)); | |
4041 | if (fd == -1) { | |
20c39760 | 4042 | error_setg_file_open(errp, errno, src); |
ffbdbe59 GH |
4043 | } |
4044 | return fd; | |
4045 | } | |
4046 | ||
4047 | static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) | |
4048 | { | |
5f758366 | 4049 | int flags, in = -1, out; |
ffbdbe59 GH |
4050 | |
4051 | flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY; | |
4052 | out = qmp_chardev_open_file_source(file->out, flags, errp); | |
5f758366 | 4053 | if (out < 0) { |
ffbdbe59 GH |
4054 | return NULL; |
4055 | } | |
4056 | ||
e859eda5 | 4057 | if (file->has_in) { |
ffbdbe59 GH |
4058 | flags = O_RDONLY; |
4059 | in = qmp_chardev_open_file_source(file->in, flags, errp); | |
5f758366 | 4060 | if (in < 0) { |
ffbdbe59 GH |
4061 | qemu_close(out); |
4062 | return NULL; | |
4063 | } | |
4064 | } | |
4065 | ||
4066 | return qemu_chr_open_fd(in, out); | |
4067 | } | |
4068 | ||
d36b2b90 MA |
4069 | static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial, |
4070 | Error **errp) | |
d59044ef | 4071 | { |
d59044ef | 4072 | #ifdef HAVE_CHARDEV_TTY |
d36b2b90 MA |
4073 | int fd; |
4074 | ||
4075 | fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp); | |
5f758366 | 4076 | if (fd < 0) { |
d36b2b90 | 4077 | return NULL; |
39099991 | 4078 | } |
f9e8cacc | 4079 | qemu_set_nonblock(fd); |
d36b2b90 MA |
4080 | return qemu_chr_open_tty_fd(fd); |
4081 | #else | |
4082 | error_setg(errp, "character device backend type 'serial' not supported"); | |
4083 | return NULL; | |
88a946d3 | 4084 | #endif |
d36b2b90 MA |
4085 | } |
4086 | ||
4087 | static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel, | |
4088 | Error **errp) | |
4089 | { | |
88a946d3 | 4090 | #ifdef HAVE_CHARDEV_PARPORT |
d36b2b90 MA |
4091 | int fd; |
4092 | ||
4093 | fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp); | |
5f758366 | 4094 | if (fd < 0) { |
d59044ef GH |
4095 | return NULL; |
4096 | } | |
d36b2b90 MA |
4097 | return qemu_chr_open_pp_fd(fd); |
4098 | #else | |
4099 | error_setg(errp, "character device backend type 'parallel' not supported"); | |
4100 | return NULL; | |
4101 | #endif | |
d59044ef GH |
4102 | } |
4103 | ||
ffbdbe59 GH |
4104 | #endif /* WIN32 */ |
4105 | ||
5008e5b7 CM |
4106 | static void socket_try_connect(CharDriverState *chr) |
4107 | { | |
4108 | Error *err = NULL; | |
4109 | ||
4110 | if (!qemu_chr_open_socket_fd(chr, &err)) { | |
4111 | check_report_connect_error(chr, err); | |
4112 | } | |
4113 | } | |
4114 | ||
5dd1f02b CM |
4115 | static gboolean socket_reconnect_timeout(gpointer opaque) |
4116 | { | |
4117 | CharDriverState *chr = opaque; | |
4118 | TCPCharDriver *s = chr->opaque; | |
5dd1f02b CM |
4119 | |
4120 | s->reconnect_timer = 0; | |
4121 | ||
4122 | if (chr->be_open) { | |
4123 | return false; | |
4124 | } | |
4125 | ||
5008e5b7 | 4126 | socket_try_connect(chr); |
5dd1f02b CM |
4127 | |
4128 | return false; | |
4129 | } | |
4130 | ||
f6bd5d6e GH |
4131 | static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock, |
4132 | Error **errp) | |
4133 | { | |
43ded1a0 CM |
4134 | CharDriverState *chr; |
4135 | TCPCharDriver *s; | |
f6bd5d6e GH |
4136 | SocketAddress *addr = sock->addr; |
4137 | bool do_nodelay = sock->has_nodelay ? sock->nodelay : false; | |
4138 | bool is_listen = sock->has_server ? sock->server : true; | |
4139 | bool is_telnet = sock->has_telnet ? sock->telnet : false; | |
4140 | bool is_waitconnect = sock->has_wait ? sock->wait : false; | |
5dd1f02b | 4141 | int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0; |
43ded1a0 CM |
4142 | |
4143 | chr = qemu_chr_alloc(); | |
4144 | s = g_malloc0(sizeof(TCPCharDriver)); | |
4145 | ||
4146 | s->fd = -1; | |
4147 | s->listen_fd = -1; | |
4148 | s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX; | |
cfb429cb CM |
4149 | s->is_listen = is_listen; |
4150 | s->is_telnet = is_telnet; | |
43ded1a0 | 4151 | s->do_nodelay = do_nodelay; |
cfb429cb | 4152 | qapi_copy_SocketAddress(&s->addr, sock->addr); |
43ded1a0 CM |
4153 | |
4154 | chr->opaque = s; | |
4155 | chr->chr_write = tcp_chr_write; | |
4156 | chr->chr_sync_read = tcp_chr_sync_read; | |
4157 | chr->chr_close = tcp_chr_close; | |
4158 | chr->get_msgfds = tcp_get_msgfds; | |
4159 | chr->set_msgfds = tcp_set_msgfds; | |
4160 | chr->chr_add_client = tcp_chr_add_client; | |
4161 | chr->chr_add_watch = tcp_chr_add_watch; | |
4162 | chr->chr_update_read_handler = tcp_chr_update_read_handler; | |
4163 | /* be isn't opened until we get a connection */ | |
4164 | chr->explicit_be_open = true; | |
4165 | ||
4166 | chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE); | |
16cc4ffe CM |
4167 | SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:", |
4168 | addr, is_listen, is_telnet); | |
f6bd5d6e GH |
4169 | |
4170 | if (is_listen) { | |
43ded1a0 CM |
4171 | if (is_telnet) { |
4172 | s->do_telnetopt = 1; | |
4173 | } | |
5dd1f02b CM |
4174 | } else if (reconnect > 0) { |
4175 | s->reconnect_time = reconnect; | |
f6bd5d6e | 4176 | } |
43ded1a0 | 4177 | |
5008e5b7 CM |
4178 | if (s->reconnect_time) { |
4179 | socket_try_connect(chr); | |
4180 | } else if (!qemu_chr_open_socket_fd(chr, errp)) { | |
4181 | g_free(s); | |
4182 | g_free(chr->filename); | |
4183 | g_free(chr); | |
4184 | return NULL; | |
f6bd5d6e | 4185 | } |
43ded1a0 CM |
4186 | |
4187 | if (is_listen && is_waitconnect) { | |
4188 | fprintf(stderr, "QEMU waiting for connection on: %s\n", | |
4189 | chr->filename); | |
4190 | tcp_chr_accept(s->listen_chan, G_IO_IN, chr); | |
4191 | qemu_set_nonblock(s->listen_fd); | |
4192 | } | |
4193 | ||
4194 | return chr; | |
f6bd5d6e GH |
4195 | } |
4196 | ||
08d0ab3f LL |
4197 | static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp, |
4198 | Error **errp) | |
3ecc059d GH |
4199 | { |
4200 | int fd; | |
4201 | ||
08d0ab3f | 4202 | fd = socket_dgram(udp->remote, udp->local, errp); |
5f758366 | 4203 | if (fd < 0) { |
3ecc059d GH |
4204 | return NULL; |
4205 | } | |
4206 | return qemu_chr_open_udp_fd(fd); | |
4207 | } | |
4208 | ||
f1a1a356 GH |
4209 | ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, |
4210 | Error **errp) | |
4211 | { | |
4212 | ChardevReturn *ret = g_new0(ChardevReturn, 1); | |
edb2fb3c | 4213 | CharDriverState *base, *chr = NULL; |
f1a1a356 GH |
4214 | |
4215 | chr = qemu_chr_find(id); | |
4216 | if (chr) { | |
4217 | error_setg(errp, "Chardev '%s' already exists", id); | |
4218 | g_free(ret); | |
4219 | return NULL; | |
4220 | } | |
4221 | ||
4222 | switch (backend->kind) { | |
ffbdbe59 GH |
4223 | case CHARDEV_BACKEND_KIND_FILE: |
4224 | chr = qmp_chardev_open_file(backend->file, errp); | |
4225 | break; | |
d36b2b90 MA |
4226 | case CHARDEV_BACKEND_KIND_SERIAL: |
4227 | chr = qmp_chardev_open_serial(backend->serial, errp); | |
4228 | break; | |
4229 | case CHARDEV_BACKEND_KIND_PARALLEL: | |
4230 | chr = qmp_chardev_open_parallel(backend->parallel, errp); | |
d59044ef | 4231 | break; |
548cbb36 GH |
4232 | case CHARDEV_BACKEND_KIND_PIPE: |
4233 | chr = qemu_chr_open_pipe(backend->pipe); | |
4234 | break; | |
f6bd5d6e GH |
4235 | case CHARDEV_BACKEND_KIND_SOCKET: |
4236 | chr = qmp_chardev_open_socket(backend->socket, errp); | |
4237 | break; | |
08d0ab3f LL |
4238 | case CHARDEV_BACKEND_KIND_UDP: |
4239 | chr = qmp_chardev_open_udp(backend->udp, errp); | |
3ecc059d | 4240 | break; |
0a1a7fab GH |
4241 | #ifdef HAVE_CHARDEV_TTY |
4242 | case CHARDEV_BACKEND_KIND_PTY: | |
e68c5958 | 4243 | chr = qemu_chr_open_pty(id, ret); |
0a1a7fab | 4244 | break; |
0a1a7fab | 4245 | #endif |
f1a1a356 | 4246 | case CHARDEV_BACKEND_KIND_NULL: |
80dca9e6 | 4247 | chr = qemu_chr_open_null(); |
f1a1a356 | 4248 | break; |
edb2fb3c GH |
4249 | case CHARDEV_BACKEND_KIND_MUX: |
4250 | base = qemu_chr_find(backend->mux->chardev); | |
4251 | if (base == NULL) { | |
4252 | error_setg(errp, "mux: base chardev %s not found", | |
4253 | backend->mux->chardev); | |
4254 | break; | |
4255 | } | |
4256 | chr = qemu_chr_open_mux(base); | |
4257 | break; | |
f5a51cab GH |
4258 | case CHARDEV_BACKEND_KIND_MSMOUSE: |
4259 | chr = qemu_chr_open_msmouse(); | |
4260 | break; | |
2d57286d GH |
4261 | #ifdef CONFIG_BRLAPI |
4262 | case CHARDEV_BACKEND_KIND_BRAILLE: | |
4263 | chr = chr_baum_init(); | |
4264 | break; | |
4265 | #endif | |
5692399f PB |
4266 | case CHARDEV_BACKEND_KIND_TESTDEV: |
4267 | chr = chr_testdev_init(); | |
4268 | break; | |
7c358031 GH |
4269 | case CHARDEV_BACKEND_KIND_STDIO: |
4270 | chr = qemu_chr_open_stdio(backend->stdio); | |
4271 | break; | |
d9ac374f GH |
4272 | #ifdef _WIN32 |
4273 | case CHARDEV_BACKEND_KIND_CONSOLE: | |
4274 | chr = qemu_chr_open_win_con(); | |
4275 | break; | |
cd153e2a GH |
4276 | #endif |
4277 | #ifdef CONFIG_SPICE | |
4278 | case CHARDEV_BACKEND_KIND_SPICEVMC: | |
4279 | chr = qemu_chr_open_spice_vmc(backend->spicevmc->type); | |
4280 | break; | |
4281 | case CHARDEV_BACKEND_KIND_SPICEPORT: | |
4282 | chr = qemu_chr_open_spice_port(backend->spiceport->fqdn); | |
4283 | break; | |
d9ac374f | 4284 | #endif |
702ec69c GH |
4285 | case CHARDEV_BACKEND_KIND_VC: |
4286 | chr = vc_init(backend->vc); | |
4287 | break; | |
3a1da42e | 4288 | case CHARDEV_BACKEND_KIND_RINGBUF: |
1da48c65 | 4289 | case CHARDEV_BACKEND_KIND_MEMORY: |
3a1da42e | 4290 | chr = qemu_chr_open_ringbuf(backend->ringbuf, errp); |
1da48c65 | 4291 | break; |
f1a1a356 GH |
4292 | default: |
4293 | error_setg(errp, "unknown chardev backend (%d)", backend->kind); | |
4294 | break; | |
4295 | } | |
4296 | ||
3894c787 MA |
4297 | /* |
4298 | * Character backend open hasn't been fully converted to the Error | |
4299 | * API. Some opens fail without setting an error. Set a generic | |
4300 | * error then. | |
4301 | * TODO full conversion to Error API | |
4302 | */ | |
4303 | if (chr == NULL && errp && !*errp) { | |
f1a1a356 GH |
4304 | error_setg(errp, "Failed to create chardev"); |
4305 | } | |
4306 | if (chr) { | |
4307 | chr->label = g_strdup(id); | |
edb2fb3c GH |
4308 | chr->avail_connections = |
4309 | (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1; | |
60d95386 GH |
4310 | if (!chr->filename) { |
4311 | chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]); | |
4312 | } | |
bd5c51ee MR |
4313 | if (!chr->explicit_be_open) { |
4314 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); | |
4315 | } | |
f1a1a356 GH |
4316 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
4317 | return ret; | |
4318 | } else { | |
4319 | g_free(ret); | |
4320 | return NULL; | |
4321 | } | |
4322 | } | |
4323 | ||
4324 | void qmp_chardev_remove(const char *id, Error **errp) | |
4325 | { | |
4326 | CharDriverState *chr; | |
4327 | ||
4328 | chr = qemu_chr_find(id); | |
8108fd3e | 4329 | if (chr == NULL) { |
f1a1a356 GH |
4330 | error_setg(errp, "Chardev '%s' not found", id); |
4331 | return; | |
4332 | } | |
4333 | if (chr->chr_can_read || chr->chr_read || | |
4334 | chr->chr_event || chr->handler_opaque) { | |
4335 | error_setg(errp, "Chardev '%s' is busy", id); | |
4336 | return; | |
4337 | } | |
4338 | qemu_chr_delete(chr); | |
4339 | } | |
d654f34e AL |
4340 | |
4341 | static void register_types(void) | |
4342 | { | |
e4d50d47 PM |
4343 | register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL); |
4344 | register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET, | |
4345 | qemu_chr_parse_socket); | |
4346 | register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp); | |
4347 | register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF, | |
4348 | qemu_chr_parse_ringbuf); | |
4349 | register_char_driver("file", CHARDEV_BACKEND_KIND_FILE, | |
4350 | qemu_chr_parse_file_out); | |
4351 | register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO, | |
4352 | qemu_chr_parse_stdio); | |
4353 | register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL, | |
4354 | qemu_chr_parse_serial); | |
4355 | register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL, | |
4356 | qemu_chr_parse_serial); | |
4357 | register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL, | |
4358 | qemu_chr_parse_parallel); | |
4359 | register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL, | |
4360 | qemu_chr_parse_parallel); | |
4361 | register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL); | |
4362 | register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL); | |
4363 | register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE, | |
4364 | qemu_chr_parse_pipe); | |
4365 | register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux); | |
c11ed966 | 4366 | /* Bug-compatibility: */ |
e4d50d47 PM |
4367 | register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY, |
4368 | qemu_chr_parse_ringbuf); | |
7b7ab18d MR |
4369 | /* this must be done after machine init, since we register FEs with muxes |
4370 | * as part of realize functions like serial_isa_realizefn when -nographic | |
4371 | * is specified | |
4372 | */ | |
4373 | qemu_add_machine_init_done_notifier(&muxes_realize_notify); | |
d654f34e AL |
4374 | } |
4375 | ||
4376 | type_init(register_types); |