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