4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu-common.h"
25 #include "monitor/monitor.h"
26 #include "sysemu/sysemu.h"
27 #include "qemu/timer.h"
28 #include "sysemu/char.h"
30 #include "qmp-commands.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #include <arpa/inet.h>
52 #include <sys/select.h>
55 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
56 #include <dev/ppbus/ppi.h>
57 #include <dev/ppbus/ppbconf.h>
58 #elif defined(__DragonFly__)
59 #include <dev/misc/ppi/ppi.h>
60 #include <bus/ppbus/ppbconf.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_icmp.h> // must come after ip.h
76 #include <netinet/udp.h>
77 #include <netinet/tcp.h>
82 #include "qemu/sockets.h"
83 #include "ui/qemu-spice.h"
85 #define READ_BUF_LEN 4096
86 #define READ_RETRIES 10
88 /***********************************************************/
89 /* character device */
91 static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
92 QTAILQ_HEAD_INITIALIZER(chardevs);
94 CharDriverState *qemu_chr_alloc(void)
96 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
100 void qemu_chr_be_event(CharDriverState *s, int event)
102 /* Keep track if the char device is open */
104 case CHR_EVENT_OPENED:
107 case CHR_EVENT_CLOSED:
114 s->chr_event(s->handler_opaque, event);
117 void qemu_chr_be_generic_open(CharDriverState *s)
119 qemu_chr_be_event(s, CHR_EVENT_OPENED);
122 int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
124 return s->chr_write(s, buf, len);
127 int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
132 while (offset < len) {
134 res = s->chr_write(s, buf + offset, len - offset);
135 if (res == -1 && errno == EAGAIN) {
138 } while (res == -1 && errno == EAGAIN);
154 int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
156 int offset = 0, counter = 10;
159 if (!s->chr_sync_read) {
163 while (offset < len) {
165 res = s->chr_sync_read(s, buf + offset, len - offset);
166 if (res == -1 && errno == EAGAIN) {
169 } while (res == -1 && errno == EAGAIN);
189 int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
193 return s->chr_ioctl(s, cmd, arg);
196 int qemu_chr_be_can_write(CharDriverState *s)
198 if (!s->chr_can_read)
200 return s->chr_can_read(s->handler_opaque);
203 void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
206 s->chr_read(s->handler_opaque, buf, len);
210 int qemu_chr_fe_get_msgfd(CharDriverState *s)
213 return (qemu_chr_fe_get_msgfds(s, &fd, 1) >= 0) ? fd : -1;
216 int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
218 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
221 int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
223 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
226 int qemu_chr_add_client(CharDriverState *s, int fd)
228 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
231 void qemu_chr_accept_input(CharDriverState *s)
233 if (s->chr_accept_input)
234 s->chr_accept_input(s);
238 void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
240 char buf[READ_BUF_LEN];
243 vsnprintf(buf, sizeof(buf), fmt, ap);
244 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
248 static void remove_fd_in_watch(CharDriverState *chr);
250 void qemu_chr_add_handlers(CharDriverState *s,
251 IOCanReadHandler *fd_can_read,
252 IOReadHandler *fd_read,
253 IOEventHandler *fd_event,
258 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
260 remove_fd_in_watch(s);
264 s->chr_can_read = fd_can_read;
265 s->chr_read = fd_read;
266 s->chr_event = fd_event;
267 s->handler_opaque = opaque;
268 if (fe_open && s->chr_update_read_handler)
269 s->chr_update_read_handler(s);
271 if (!s->explicit_fe_open) {
272 qemu_chr_fe_set_open(s, fe_open);
275 /* We're connecting to an already opened device, so let's make sure we
276 also get the open event */
277 if (fe_open && s->be_open) {
278 qemu_chr_be_generic_open(s);
282 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
287 static CharDriverState *qemu_chr_open_null(void)
289 CharDriverState *chr;
291 chr = qemu_chr_alloc();
292 chr->chr_write = null_chr_write;
293 chr->explicit_be_open = true;
297 /* MUX driver for serial I/O splitting */
299 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
300 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
302 IOCanReadHandler *chr_can_read[MAX_MUX];
303 IOReadHandler *chr_read[MAX_MUX];
304 IOEventHandler *chr_event[MAX_MUX];
305 void *ext_opaque[MAX_MUX];
306 CharDriverState *drv;
311 /* Intermediate input buffer allows to catch escape sequences even if the
312 currently active device is not accepting any input - but only until it
314 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
319 int64_t timestamps_start;
323 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
325 MuxDriver *d = chr->opaque;
327 if (!d->timestamps) {
328 ret = qemu_chr_fe_write(d->drv, buf, len);
333 for (i = 0; i < len; i++) {
339 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
340 if (d->timestamps_start == -1)
341 d->timestamps_start = ti;
342 ti -= d->timestamps_start;
344 snprintf(buf1, sizeof(buf1),
345 "[%02d:%02d:%02d.%03d] ",
350 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
353 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
354 if (buf[i] == '\n') {
362 static const char * const mux_help[] = {
363 "% h print this help\n\r",
364 "% x exit emulator\n\r",
365 "% s save disk data back to file (if -snapshot)\n\r",
366 "% t toggle console timestamps\n\r"
367 "% b send break (magic sysrq)\n\r",
368 "% c switch between console and monitor\n\r",
373 int term_escape_char = 0x01; /* ctrl-a is used for escape */
374 static void mux_print_help(CharDriverState *chr)
377 char ebuf[15] = "Escape-Char";
378 char cbuf[50] = "\n\r";
380 if (term_escape_char > 0 && term_escape_char < 26) {
381 snprintf(cbuf, sizeof(cbuf), "\n\r");
382 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
384 snprintf(cbuf, sizeof(cbuf),
385 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
388 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
389 for (i = 0; mux_help[i] != NULL; i++) {
390 for (j=0; mux_help[i][j] != '\0'; j++) {
391 if (mux_help[i][j] == '%')
392 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
394 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
399 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
401 if (d->chr_event[mux_nr])
402 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
405 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
407 if (d->term_got_escape) {
408 d->term_got_escape = 0;
409 if (ch == term_escape_char)
418 const char *term = "QEMU: Terminated\n\r";
419 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
427 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
430 /* Switch to the next registered device */
431 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
433 if (d->focus >= d->mux_cnt)
435 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
438 d->timestamps = !d->timestamps;
439 d->timestamps_start = -1;
443 } else if (ch == term_escape_char) {
444 d->term_got_escape = 1;
452 static void mux_chr_accept_input(CharDriverState *chr)
454 MuxDriver *d = chr->opaque;
457 while (d->prod[m] != d->cons[m] &&
458 d->chr_can_read[m] &&
459 d->chr_can_read[m](d->ext_opaque[m])) {
460 d->chr_read[m](d->ext_opaque[m],
461 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
465 static int mux_chr_can_read(void *opaque)
467 CharDriverState *chr = opaque;
468 MuxDriver *d = chr->opaque;
471 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
473 if (d->chr_can_read[m])
474 return d->chr_can_read[m](d->ext_opaque[m]);
478 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
480 CharDriverState *chr = opaque;
481 MuxDriver *d = chr->opaque;
485 mux_chr_accept_input (opaque);
487 for(i = 0; i < size; i++)
488 if (mux_proc_byte(chr, d, buf[i])) {
489 if (d->prod[m] == d->cons[m] &&
490 d->chr_can_read[m] &&
491 d->chr_can_read[m](d->ext_opaque[m]))
492 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
494 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
498 static void mux_chr_event(void *opaque, int event)
500 CharDriverState *chr = opaque;
501 MuxDriver *d = chr->opaque;
504 /* Send the event to all registered listeners */
505 for (i = 0; i < d->mux_cnt; i++)
506 mux_chr_send_event(d, i, event);
509 static void mux_chr_update_read_handler(CharDriverState *chr)
511 MuxDriver *d = chr->opaque;
513 if (d->mux_cnt >= MAX_MUX) {
514 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
517 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
518 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
519 d->chr_read[d->mux_cnt] = chr->chr_read;
520 d->chr_event[d->mux_cnt] = chr->chr_event;
521 /* Fix up the real driver with mux routines */
522 if (d->mux_cnt == 0) {
523 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
526 if (d->focus != -1) {
527 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
529 d->focus = d->mux_cnt;
531 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
534 static bool muxes_realized;
537 * Called after processing of default and command-line-specified
538 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
539 * to a mux chardev. This is done here to ensure that
540 * output/prompts/banners are only displayed for the FE that has
541 * focus when initial command-line processing/machine init is
544 * After this point, any new FE attached to any new or existing
545 * mux will receive CHR_EVENT_OPENED notifications for the BE
548 static void muxes_realize_done(Notifier *notifier, void *unused)
550 CharDriverState *chr;
552 QTAILQ_FOREACH(chr, &chardevs, next) {
554 MuxDriver *d = chr->opaque;
557 /* send OPENED to all already-attached FEs */
558 for (i = 0; i < d->mux_cnt; i++) {
559 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
561 /* mark mux as OPENED so any new FEs will immediately receive
564 qemu_chr_be_generic_open(chr);
567 muxes_realized = true;
570 static Notifier muxes_realize_notify = {
571 .notify = muxes_realize_done,
574 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
576 CharDriverState *chr;
579 chr = qemu_chr_alloc();
580 d = g_malloc0(sizeof(MuxDriver));
585 chr->chr_write = mux_chr_write;
586 chr->chr_update_read_handler = mux_chr_update_read_handler;
587 chr->chr_accept_input = mux_chr_accept_input;
588 /* Frontend guest-open / -close notification is not support with muxes */
589 chr->chr_set_fe_open = NULL;
590 /* only default to opened state if we've realized the initial
593 chr->explicit_be_open = muxes_realized ? 0 : 1;
601 int send_all(int fd, const void *buf, int len1)
607 ret = send(fd, buf, len, 0);
609 errno = WSAGetLastError();
610 if (errno != WSAEWOULDBLOCK) {
613 } else if (ret == 0) {
625 int send_all(int fd, const void *_buf, int len1)
628 const uint8_t *buf = _buf;
632 ret = write(fd, buf, len);
634 if (errno != EINTR && errno != EAGAIN)
636 } else if (ret == 0) {
646 int recv_all(int fd, void *_buf, int len1, bool single_read)
652 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
654 if (errno != EINTR && errno != EAGAIN) {
671 typedef struct IOWatchPoll
678 IOCanReadHandler *fd_can_read;
683 static IOWatchPoll *io_watch_poll_from_source(GSource *source)
685 return container_of(source, IOWatchPoll, parent);
688 static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
690 IOWatchPoll *iwp = io_watch_poll_from_source(source);
691 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
692 bool was_active = iwp->src != NULL;
693 if (was_active == now_active) {
698 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
699 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
700 g_source_attach(iwp->src, NULL);
702 g_source_destroy(iwp->src);
703 g_source_unref(iwp->src);
709 static gboolean io_watch_poll_check(GSource *source)
714 static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
720 static void io_watch_poll_finalize(GSource *source)
722 /* Due to a glib bug, removing the last reference to a source
723 * inside a finalize callback causes recursive locking (and a
724 * deadlock). This is not a problem inside other callbacks,
725 * including dispatch callbacks, so we call io_remove_watch_poll
726 * to remove this source. At this point, iwp->src must
727 * be NULL, or we would leak it.
729 * This would be solved much more elegantly by child sources,
730 * but we support older glib versions that do not have them.
732 IOWatchPoll *iwp = io_watch_poll_from_source(source);
733 assert(iwp->src == NULL);
736 static GSourceFuncs io_watch_poll_funcs = {
737 .prepare = io_watch_poll_prepare,
738 .check = io_watch_poll_check,
739 .dispatch = io_watch_poll_dispatch,
740 .finalize = io_watch_poll_finalize,
743 /* Can only be used for read */
744 static guint io_add_watch_poll(GIOChannel *channel,
745 IOCanReadHandler *fd_can_read,
752 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
753 iwp->fd_can_read = fd_can_read;
754 iwp->opaque = user_data;
755 iwp->channel = channel;
756 iwp->fd_read = (GSourceFunc) fd_read;
759 tag = g_source_attach(&iwp->parent, NULL);
760 g_source_unref(&iwp->parent);
764 static void io_remove_watch_poll(guint tag)
769 g_return_if_fail (tag > 0);
771 source = g_main_context_find_source_by_id(NULL, tag);
772 g_return_if_fail (source != NULL);
774 iwp = io_watch_poll_from_source(source);
776 g_source_destroy(iwp->src);
777 g_source_unref(iwp->src);
780 g_source_destroy(&iwp->parent);
783 static void remove_fd_in_watch(CharDriverState *chr)
785 if (chr->fd_in_tag) {
786 io_remove_watch_poll(chr->fd_in_tag);
792 static GIOChannel *io_channel_from_fd(int fd)
800 chan = g_io_channel_unix_new(fd);
802 g_io_channel_set_encoding(chan, NULL, NULL);
803 g_io_channel_set_buffered(chan, FALSE);
809 static GIOChannel *io_channel_from_socket(int fd)
818 chan = g_io_channel_win32_new_socket(fd);
820 chan = g_io_channel_unix_new(fd);
823 g_io_channel_set_encoding(chan, NULL, NULL);
824 g_io_channel_set_buffered(chan, FALSE);
829 static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
832 GIOStatus status = G_IO_STATUS_NORMAL;
834 while (offset < len && status == G_IO_STATUS_NORMAL) {
835 gsize bytes_written = 0;
837 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
838 &bytes_written, NULL);
839 offset += bytes_written;
846 case G_IO_STATUS_NORMAL:
849 case G_IO_STATUS_AGAIN:
861 typedef struct FDCharDriver {
862 CharDriverState *chr;
863 GIOChannel *fd_in, *fd_out;
865 QTAILQ_ENTRY(FDCharDriver) node;
868 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
870 FDCharDriver *s = chr->opaque;
872 return io_channel_send(s->fd_out, buf, len);
875 static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
877 CharDriverState *chr = opaque;
878 FDCharDriver *s = chr->opaque;
880 uint8_t buf[READ_BUF_LEN];
885 if (len > s->max_size) {
892 status = g_io_channel_read_chars(chan, (gchar *)buf,
893 len, &bytes_read, NULL);
894 if (status == G_IO_STATUS_EOF) {
895 remove_fd_in_watch(chr);
896 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
899 if (status == G_IO_STATUS_NORMAL) {
900 qemu_chr_be_write(chr, buf, bytes_read);
906 static int fd_chr_read_poll(void *opaque)
908 CharDriverState *chr = opaque;
909 FDCharDriver *s = chr->opaque;
911 s->max_size = qemu_chr_be_can_write(chr);
915 static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
917 FDCharDriver *s = chr->opaque;
918 return g_io_create_watch(s->fd_out, cond);
921 static void fd_chr_update_read_handler(CharDriverState *chr)
923 FDCharDriver *s = chr->opaque;
925 remove_fd_in_watch(chr);
927 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
932 static void fd_chr_close(struct CharDriverState *chr)
934 FDCharDriver *s = chr->opaque;
936 remove_fd_in_watch(chr);
938 g_io_channel_unref(s->fd_in);
941 g_io_channel_unref(s->fd_out);
945 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
948 /* open a character device to a unix fd */
949 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
951 CharDriverState *chr;
954 chr = qemu_chr_alloc();
955 s = g_malloc0(sizeof(FDCharDriver));
956 s->fd_in = io_channel_from_fd(fd_in);
957 s->fd_out = io_channel_from_fd(fd_out);
958 fcntl(fd_out, F_SETFL, O_NONBLOCK);
961 chr->chr_add_watch = fd_chr_add_watch;
962 chr->chr_write = fd_chr_write;
963 chr->chr_update_read_handler = fd_chr_update_read_handler;
964 chr->chr_close = fd_chr_close;
969 static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
972 char filename_in[256], filename_out[256];
973 const char *filename = opts->device;
975 if (filename == NULL) {
976 fprintf(stderr, "chardev: pipe: no filename given\n");
980 snprintf(filename_in, 256, "%s.in", filename);
981 snprintf(filename_out, 256, "%s.out", filename);
982 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
983 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
984 if (fd_in < 0 || fd_out < 0) {
989 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
994 return qemu_chr_open_fd(fd_in, fd_out);
997 /* init terminal so that we can grab keys */
998 static struct termios oldtty;
999 static int old_fd0_flags;
1000 static bool stdio_allow_signal;
1002 static void term_exit(void)
1004 tcsetattr (0, TCSANOW, &oldtty);
1005 fcntl(0, F_SETFL, old_fd0_flags);
1008 static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
1014 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1015 |INLCR|IGNCR|ICRNL|IXON);
1016 tty.c_oflag |= OPOST;
1017 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1018 tty.c_cflag &= ~(CSIZE|PARENB);
1021 tty.c_cc[VTIME] = 0;
1023 if (!stdio_allow_signal)
1024 tty.c_lflag &= ~ISIG;
1026 tcsetattr (0, TCSANOW, &tty);
1029 static void qemu_chr_close_stdio(struct CharDriverState *chr)
1035 static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
1037 CharDriverState *chr;
1039 if (is_daemonized()) {
1040 error_report("cannot use stdio with -daemonize");
1043 old_fd0_flags = fcntl(0, F_GETFL);
1044 tcgetattr (0, &oldtty);
1045 fcntl(0, F_SETFL, O_NONBLOCK);
1048 chr = qemu_chr_open_fd(0, 1);
1049 chr->chr_close = qemu_chr_close_stdio;
1050 chr->chr_set_echo = qemu_chr_set_echo_stdio;
1051 if (opts->has_signal) {
1052 stdio_allow_signal = opts->signal;
1054 qemu_chr_fe_set_echo(chr, false);
1059 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1060 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1061 || defined(__GLIBC__)
1063 #define HAVE_CHARDEV_TTY 1
1072 static void pty_chr_update_read_handler(CharDriverState *chr);
1073 static void pty_chr_state(CharDriverState *chr, int connected);
1075 static gboolean pty_chr_timer(gpointer opaque)
1077 struct CharDriverState *chr = opaque;
1078 PtyCharDriver *s = chr->opaque;
1081 if (!s->connected) {
1083 pty_chr_update_read_handler(chr);
1088 static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1090 PtyCharDriver *s = chr->opaque;
1093 g_source_remove(s->timer_tag);
1098 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1100 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1104 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1106 PtyCharDriver *s = chr->opaque;
1108 if (!s->connected) {
1109 /* guest sends data, check for (re-)connect */
1110 pty_chr_update_read_handler(chr);
1113 return io_channel_send(s->fd, buf, len);
1116 static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1118 PtyCharDriver *s = chr->opaque;
1119 return g_io_create_watch(s->fd, cond);
1122 static int pty_chr_read_poll(void *opaque)
1124 CharDriverState *chr = opaque;
1125 PtyCharDriver *s = chr->opaque;
1127 s->read_bytes = qemu_chr_be_can_write(chr);
1128 return s->read_bytes;
1131 static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
1133 CharDriverState *chr = opaque;
1134 PtyCharDriver *s = chr->opaque;
1136 uint8_t buf[READ_BUF_LEN];
1140 if (len > s->read_bytes)
1141 len = s->read_bytes;
1145 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1146 if (status != G_IO_STATUS_NORMAL) {
1147 pty_chr_state(chr, 0);
1150 pty_chr_state(chr, 1);
1151 qemu_chr_be_write(chr, buf, size);
1156 static void pty_chr_update_read_handler(CharDriverState *chr)
1158 PtyCharDriver *s = chr->opaque;
1161 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1162 pfd.events = G_IO_OUT;
1165 if (pfd.revents & G_IO_HUP) {
1166 pty_chr_state(chr, 0);
1168 pty_chr_state(chr, 1);
1172 static void pty_chr_state(CharDriverState *chr, int connected)
1174 PtyCharDriver *s = chr->opaque;
1177 remove_fd_in_watch(chr);
1179 /* (re-)connect poll interval for idle guests: once per second.
1180 * We check more frequently in case the guests sends data to
1181 * the virtual device linked to our pty. */
1182 pty_chr_rearm_timer(chr, 1000);
1185 g_source_remove(s->timer_tag);
1188 if (!s->connected) {
1190 qemu_chr_be_generic_open(chr);
1192 if (!chr->fd_in_tag) {
1193 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1199 static void pty_chr_close(struct CharDriverState *chr)
1201 PtyCharDriver *s = chr->opaque;
1204 remove_fd_in_watch(chr);
1205 fd = g_io_channel_unix_get_fd(s->fd);
1206 g_io_channel_unref(s->fd);
1209 g_source_remove(s->timer_tag);
1213 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1216 static CharDriverState *qemu_chr_open_pty(const char *id,
1219 CharDriverState *chr;
1221 int master_fd, slave_fd;
1222 char pty_name[PATH_MAX];
1224 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1225 if (master_fd < 0) {
1231 chr = qemu_chr_alloc();
1233 chr->filename = g_strdup_printf("pty:%s", pty_name);
1234 ret->pty = g_strdup(pty_name);
1235 ret->has_pty = true;
1237 fprintf(stderr, "char device redirected to %s (label %s)\n",
1240 s = g_malloc0(sizeof(PtyCharDriver));
1242 chr->chr_write = pty_chr_write;
1243 chr->chr_update_read_handler = pty_chr_update_read_handler;
1244 chr->chr_close = pty_chr_close;
1245 chr->chr_add_watch = pty_chr_add_watch;
1246 chr->explicit_be_open = true;
1248 s->fd = io_channel_from_fd(master_fd);
1254 static void tty_serial_init(int fd, int speed,
1255 int parity, int data_bits, int stop_bits)
1261 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1262 speed, parity, data_bits, stop_bits);
1264 tcgetattr (fd, &tty);
1266 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1267 speed = speed * 10 / 11;
1284 /* Non-Posix values follow. They may be unsupported on some systems. */
1286 check_speed(115200);
1288 check_speed(230400);
1291 check_speed(460800);
1294 check_speed(500000);
1297 check_speed(576000);
1300 check_speed(921600);
1303 check_speed(1000000);
1306 check_speed(1152000);
1309 check_speed(1500000);
1312 check_speed(2000000);
1315 check_speed(2500000);
1318 check_speed(3000000);
1321 check_speed(3500000);
1324 check_speed(4000000);
1329 cfsetispeed(&tty, spd);
1330 cfsetospeed(&tty, spd);
1332 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1333 |INLCR|IGNCR|ICRNL|IXON);
1334 tty.c_oflag |= OPOST;
1335 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1336 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1357 tty.c_cflag |= PARENB;
1360 tty.c_cflag |= PARENB | PARODD;
1364 tty.c_cflag |= CSTOPB;
1366 tcsetattr (fd, TCSANOW, &tty);
1369 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1371 FDCharDriver *s = chr->opaque;
1374 case CHR_IOCTL_SERIAL_SET_PARAMS:
1376 QEMUSerialSetParams *ssp = arg;
1377 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1378 ssp->speed, ssp->parity,
1379 ssp->data_bits, ssp->stop_bits);
1382 case CHR_IOCTL_SERIAL_SET_BREAK:
1384 int enable = *(int *)arg;
1386 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1390 case CHR_IOCTL_SERIAL_GET_TIOCM:
1393 int *targ = (int *)arg;
1394 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
1396 if (sarg & TIOCM_CTS)
1397 *targ |= CHR_TIOCM_CTS;
1398 if (sarg & TIOCM_CAR)
1399 *targ |= CHR_TIOCM_CAR;
1400 if (sarg & TIOCM_DSR)
1401 *targ |= CHR_TIOCM_DSR;
1402 if (sarg & TIOCM_RI)
1403 *targ |= CHR_TIOCM_RI;
1404 if (sarg & TIOCM_DTR)
1405 *targ |= CHR_TIOCM_DTR;
1406 if (sarg & TIOCM_RTS)
1407 *targ |= CHR_TIOCM_RTS;
1410 case CHR_IOCTL_SERIAL_SET_TIOCM:
1412 int sarg = *(int *)arg;
1414 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
1415 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1416 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1417 if (sarg & CHR_TIOCM_CTS)
1419 if (sarg & CHR_TIOCM_CAR)
1421 if (sarg & CHR_TIOCM_DSR)
1423 if (sarg & CHR_TIOCM_RI)
1425 if (sarg & CHR_TIOCM_DTR)
1427 if (sarg & CHR_TIOCM_RTS)
1429 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
1438 static void qemu_chr_close_tty(CharDriverState *chr)
1440 FDCharDriver *s = chr->opaque;
1444 fd = g_io_channel_unix_get_fd(s->fd_in);
1454 static CharDriverState *qemu_chr_open_tty_fd(int fd)
1456 CharDriverState *chr;
1458 tty_serial_init(fd, 115200, 'N', 8, 1);
1459 chr = qemu_chr_open_fd(fd, fd);
1460 chr->chr_ioctl = tty_serial_ioctl;
1461 chr->chr_close = qemu_chr_close_tty;
1464 #endif /* __linux__ || __sun__ */
1466 #if defined(__linux__)
1468 #define HAVE_CHARDEV_PARPORT 1
1473 } ParallelCharDriver;
1475 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1477 if (s->mode != mode) {
1479 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1486 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1488 ParallelCharDriver *drv = chr->opaque;
1493 case CHR_IOCTL_PP_READ_DATA:
1494 if (ioctl(fd, PPRDATA, &b) < 0)
1496 *(uint8_t *)arg = b;
1498 case CHR_IOCTL_PP_WRITE_DATA:
1499 b = *(uint8_t *)arg;
1500 if (ioctl(fd, PPWDATA, &b) < 0)
1503 case CHR_IOCTL_PP_READ_CONTROL:
1504 if (ioctl(fd, PPRCONTROL, &b) < 0)
1506 /* Linux gives only the lowest bits, and no way to know data
1507 direction! For better compatibility set the fixed upper
1509 *(uint8_t *)arg = b | 0xc0;
1511 case CHR_IOCTL_PP_WRITE_CONTROL:
1512 b = *(uint8_t *)arg;
1513 if (ioctl(fd, PPWCONTROL, &b) < 0)
1516 case CHR_IOCTL_PP_READ_STATUS:
1517 if (ioctl(fd, PPRSTATUS, &b) < 0)
1519 *(uint8_t *)arg = b;
1521 case CHR_IOCTL_PP_DATA_DIR:
1522 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1525 case CHR_IOCTL_PP_EPP_READ_ADDR:
1526 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1527 struct ParallelIOArg *parg = arg;
1528 int n = read(fd, parg->buffer, parg->count);
1529 if (n != parg->count) {
1534 case CHR_IOCTL_PP_EPP_READ:
1535 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1536 struct ParallelIOArg *parg = arg;
1537 int n = read(fd, parg->buffer, parg->count);
1538 if (n != parg->count) {
1543 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1544 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1545 struct ParallelIOArg *parg = arg;
1546 int n = write(fd, parg->buffer, parg->count);
1547 if (n != parg->count) {
1552 case CHR_IOCTL_PP_EPP_WRITE:
1553 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1554 struct ParallelIOArg *parg = arg;
1555 int n = write(fd, parg->buffer, parg->count);
1556 if (n != parg->count) {
1567 static void pp_close(CharDriverState *chr)
1569 ParallelCharDriver *drv = chr->opaque;
1572 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1573 ioctl(fd, PPRELEASE);
1576 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1579 static CharDriverState *qemu_chr_open_pp_fd(int fd)
1581 CharDriverState *chr;
1582 ParallelCharDriver *drv;
1584 if (ioctl(fd, PPCLAIM) < 0) {
1589 drv = g_malloc0(sizeof(ParallelCharDriver));
1591 drv->mode = IEEE1284_MODE_COMPAT;
1593 chr = qemu_chr_alloc();
1594 chr->chr_write = null_chr_write;
1595 chr->chr_ioctl = pp_ioctl;
1596 chr->chr_close = pp_close;
1601 #endif /* __linux__ */
1603 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1605 #define HAVE_CHARDEV_PARPORT 1
1607 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1609 int fd = (int)(intptr_t)chr->opaque;
1613 case CHR_IOCTL_PP_READ_DATA:
1614 if (ioctl(fd, PPIGDATA, &b) < 0)
1616 *(uint8_t *)arg = b;
1618 case CHR_IOCTL_PP_WRITE_DATA:
1619 b = *(uint8_t *)arg;
1620 if (ioctl(fd, PPISDATA, &b) < 0)
1623 case CHR_IOCTL_PP_READ_CONTROL:
1624 if (ioctl(fd, PPIGCTRL, &b) < 0)
1626 *(uint8_t *)arg = b;
1628 case CHR_IOCTL_PP_WRITE_CONTROL:
1629 b = *(uint8_t *)arg;
1630 if (ioctl(fd, PPISCTRL, &b) < 0)
1633 case CHR_IOCTL_PP_READ_STATUS:
1634 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1636 *(uint8_t *)arg = b;
1644 static CharDriverState *qemu_chr_open_pp_fd(int fd)
1646 CharDriverState *chr;
1648 chr = qemu_chr_alloc();
1649 chr->opaque = (void *)(intptr_t)fd;
1650 chr->chr_write = null_chr_write;
1651 chr->chr_ioctl = pp_ioctl;
1652 chr->explicit_be_open = true;
1661 HANDLE hcom, hrecv, hsend;
1662 OVERLAPPED orecv, osend;
1669 HANDLE hInputReadyEvent;
1670 HANDLE hInputDoneEvent;
1671 HANDLE hInputThread;
1672 uint8_t win_stdio_buf;
1673 } WinStdioCharState;
1675 #define NSENDBUF 2048
1676 #define NRECVBUF 2048
1677 #define MAXCONNECT 1
1678 #define NTIMEOUT 5000
1680 static int win_chr_poll(void *opaque);
1681 static int win_chr_pipe_poll(void *opaque);
1683 static void win_chr_close(CharDriverState *chr)
1685 WinCharState *s = chr->opaque;
1688 CloseHandle(s->hsend);
1692 CloseHandle(s->hrecv);
1696 CloseHandle(s->hcom);
1700 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1702 qemu_del_polling_cb(win_chr_poll, chr);
1704 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1707 static int win_chr_init(CharDriverState *chr, const char *filename)
1709 WinCharState *s = chr->opaque;
1711 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1716 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1718 fprintf(stderr, "Failed CreateEvent\n");
1721 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1723 fprintf(stderr, "Failed CreateEvent\n");
1727 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1728 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1729 if (s->hcom == INVALID_HANDLE_VALUE) {
1730 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1735 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1736 fprintf(stderr, "Failed SetupComm\n");
1740 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1741 size = sizeof(COMMCONFIG);
1742 GetDefaultCommConfig(filename, &comcfg, &size);
1743 comcfg.dcb.DCBlength = sizeof(DCB);
1744 CommConfigDialog(filename, NULL, &comcfg);
1746 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1747 fprintf(stderr, "Failed SetCommState\n");
1751 if (!SetCommMask(s->hcom, EV_ERR)) {
1752 fprintf(stderr, "Failed SetCommMask\n");
1756 cto.ReadIntervalTimeout = MAXDWORD;
1757 if (!SetCommTimeouts(s->hcom, &cto)) {
1758 fprintf(stderr, "Failed SetCommTimeouts\n");
1762 if (!ClearCommError(s->hcom, &err, &comstat)) {
1763 fprintf(stderr, "Failed ClearCommError\n");
1766 qemu_add_polling_cb(win_chr_poll, chr);
1774 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1776 WinCharState *s = chr->opaque;
1777 DWORD len, ret, size, err;
1780 ZeroMemory(&s->osend, sizeof(s->osend));
1781 s->osend.hEvent = s->hsend;
1784 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1786 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1788 err = GetLastError();
1789 if (err == ERROR_IO_PENDING) {
1790 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1808 static int win_chr_read_poll(CharDriverState *chr)
1810 WinCharState *s = chr->opaque;
1812 s->max_size = qemu_chr_be_can_write(chr);
1816 static void win_chr_readfile(CharDriverState *chr)
1818 WinCharState *s = chr->opaque;
1820 uint8_t buf[READ_BUF_LEN];
1823 ZeroMemory(&s->orecv, sizeof(s->orecv));
1824 s->orecv.hEvent = s->hrecv;
1825 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1827 err = GetLastError();
1828 if (err == ERROR_IO_PENDING) {
1829 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1834 qemu_chr_be_write(chr, buf, size);
1838 static void win_chr_read(CharDriverState *chr)
1840 WinCharState *s = chr->opaque;
1842 if (s->len > s->max_size)
1843 s->len = s->max_size;
1847 win_chr_readfile(chr);
1850 static int win_chr_poll(void *opaque)
1852 CharDriverState *chr = opaque;
1853 WinCharState *s = chr->opaque;
1857 ClearCommError(s->hcom, &comerr, &status);
1858 if (status.cbInQue > 0) {
1859 s->len = status.cbInQue;
1860 win_chr_read_poll(chr);
1867 static CharDriverState *qemu_chr_open_win_path(const char *filename)
1869 CharDriverState *chr;
1872 chr = qemu_chr_alloc();
1873 s = g_malloc0(sizeof(WinCharState));
1875 chr->chr_write = win_chr_write;
1876 chr->chr_close = win_chr_close;
1878 if (win_chr_init(chr, filename) < 0) {
1886 static int win_chr_pipe_poll(void *opaque)
1888 CharDriverState *chr = opaque;
1889 WinCharState *s = chr->opaque;
1892 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1895 win_chr_read_poll(chr);
1902 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1904 WinCharState *s = chr->opaque;
1912 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1914 fprintf(stderr, "Failed CreateEvent\n");
1917 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1919 fprintf(stderr, "Failed CreateEvent\n");
1923 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1924 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1925 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1927 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1928 if (s->hcom == INVALID_HANDLE_VALUE) {
1929 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1934 ZeroMemory(&ov, sizeof(ov));
1935 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1936 ret = ConnectNamedPipe(s->hcom, &ov);
1938 fprintf(stderr, "Failed ConnectNamedPipe\n");
1942 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1944 fprintf(stderr, "Failed GetOverlappedResult\n");
1946 CloseHandle(ov.hEvent);
1953 CloseHandle(ov.hEvent);
1956 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1965 static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
1967 const char *filename = opts->device;
1968 CharDriverState *chr;
1971 chr = qemu_chr_alloc();
1972 s = g_malloc0(sizeof(WinCharState));
1974 chr->chr_write = win_chr_write;
1975 chr->chr_close = win_chr_close;
1977 if (win_chr_pipe_init(chr, filename) < 0) {
1985 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1987 CharDriverState *chr;
1990 chr = qemu_chr_alloc();
1991 s = g_malloc0(sizeof(WinCharState));
1994 chr->chr_write = win_chr_write;
1998 static CharDriverState *qemu_chr_open_win_con(void)
2000 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
2003 static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2005 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2012 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2022 static void win_stdio_wait_func(void *opaque)
2024 CharDriverState *chr = opaque;
2025 WinStdioCharState *stdio = chr->opaque;
2026 INPUT_RECORD buf[4];
2031 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
2034 /* Avoid error storm */
2035 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2039 for (i = 0; i < dwSize; i++) {
2040 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2042 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2044 if (kev->uChar.AsciiChar != 0) {
2045 for (j = 0; j < kev->wRepeatCount; j++) {
2046 if (qemu_chr_be_can_write(chr)) {
2047 uint8_t c = kev->uChar.AsciiChar;
2048 qemu_chr_be_write(chr, &c, 1);
2056 static DWORD WINAPI win_stdio_thread(LPVOID param)
2058 CharDriverState *chr = param;
2059 WinStdioCharState *stdio = chr->opaque;
2065 /* Wait for one byte */
2066 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2068 /* Exit in case of error, continue if nothing read */
2076 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2077 if (stdio->win_stdio_buf == '\r') {
2081 /* Signal the main thread and wait until the byte was eaten */
2082 if (!SetEvent(stdio->hInputReadyEvent)) {
2085 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2091 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2095 static void win_stdio_thread_wait_func(void *opaque)
2097 CharDriverState *chr = opaque;
2098 WinStdioCharState *stdio = chr->opaque;
2100 if (qemu_chr_be_can_write(chr)) {
2101 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2104 SetEvent(stdio->hInputDoneEvent);
2107 static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2109 WinStdioCharState *stdio = chr->opaque;
2112 GetConsoleMode(stdio->hStdIn, &dwMode);
2115 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2117 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2121 static void win_stdio_close(CharDriverState *chr)
2123 WinStdioCharState *stdio = chr->opaque;
2125 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2126 CloseHandle(stdio->hInputReadyEvent);
2128 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2129 CloseHandle(stdio->hInputDoneEvent);
2131 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2132 TerminateThread(stdio->hInputThread, 0);
2135 g_free(chr->opaque);
2139 static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
2141 CharDriverState *chr;
2142 WinStdioCharState *stdio;
2146 chr = qemu_chr_alloc();
2147 stdio = g_malloc0(sizeof(WinStdioCharState));
2149 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2150 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2151 fprintf(stderr, "cannot open stdio: invalid handle\n");
2155 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2157 chr->opaque = stdio;
2158 chr->chr_write = win_stdio_write;
2159 chr->chr_close = win_stdio_close;
2162 if (qemu_add_wait_object(stdio->hStdIn,
2163 win_stdio_wait_func, chr)) {
2164 fprintf(stderr, "qemu_add_wait_object: failed\n");
2169 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2170 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2171 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2174 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2175 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2176 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2177 fprintf(stderr, "cannot create stdio thread or event\n");
2180 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2181 win_stdio_thread_wait_func, chr)) {
2182 fprintf(stderr, "qemu_add_wait_object: failed\n");
2186 dwMode |= ENABLE_LINE_INPUT;
2189 /* set the terminal in raw mode */
2190 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2191 dwMode |= ENABLE_PROCESSED_INPUT;
2194 SetConsoleMode(stdio->hStdIn, dwMode);
2196 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2197 qemu_chr_fe_set_echo(chr, false);
2201 #endif /* !_WIN32 */
2204 /***********************************************************/
2205 /* UDP Net console */
2210 uint8_t buf[READ_BUF_LEN];
2216 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2218 NetCharDriver *s = chr->opaque;
2219 gsize bytes_written;
2222 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2223 if (status == G_IO_STATUS_EOF) {
2225 } else if (status != G_IO_STATUS_NORMAL) {
2229 return bytes_written;
2232 static int udp_chr_read_poll(void *opaque)
2234 CharDriverState *chr = opaque;
2235 NetCharDriver *s = chr->opaque;
2237 s->max_size = qemu_chr_be_can_write(chr);
2239 /* If there were any stray characters in the queue process them
2242 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
2243 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
2245 s->max_size = qemu_chr_be_can_write(chr);
2250 static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
2252 CharDriverState *chr = opaque;
2253 NetCharDriver *s = chr->opaque;
2254 gsize bytes_read = 0;
2257 if (s->max_size == 0) {
2260 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2262 s->bufcnt = bytes_read;
2263 s->bufptr = s->bufcnt;
2264 if (status != G_IO_STATUS_NORMAL) {
2265 remove_fd_in_watch(chr);
2270 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
2271 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
2273 s->max_size = qemu_chr_be_can_write(chr);
2279 static void udp_chr_update_read_handler(CharDriverState *chr)
2281 NetCharDriver *s = chr->opaque;
2283 remove_fd_in_watch(chr);
2285 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2290 static void udp_chr_close(CharDriverState *chr)
2292 NetCharDriver *s = chr->opaque;
2294 remove_fd_in_watch(chr);
2296 g_io_channel_unref(s->chan);
2300 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2303 static CharDriverState *qemu_chr_open_udp_fd(int fd)
2305 CharDriverState *chr = NULL;
2306 NetCharDriver *s = NULL;
2308 chr = qemu_chr_alloc();
2309 s = g_malloc0(sizeof(NetCharDriver));
2312 s->chan = io_channel_from_socket(s->fd);
2316 chr->chr_write = udp_chr_write;
2317 chr->chr_update_read_handler = udp_chr_update_read_handler;
2318 chr->chr_close = udp_chr_close;
2319 /* be isn't opened until we get a connection */
2320 chr->explicit_be_open = true;
2324 static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2326 Error *local_err = NULL;
2329 fd = inet_dgram_opts(opts, &local_err);
2331 qerror_report_err(local_err);
2332 error_free(local_err);
2335 return qemu_chr_open_udp_fd(fd);
2338 /***********************************************************/
2339 /* TCP Net console */
2343 GIOChannel *chan, *listen_chan;
2352 int read_msgfds_num;
2354 int write_msgfds_num;
2357 static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
2360 static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2362 TCPCharDriver *s = chr->opaque;
2367 size_t fd_size = s->write_msgfds_num * sizeof(int);
2368 char control[CMSG_SPACE(fd_size)];
2369 struct cmsghdr *cmsg;
2371 memset(&msgh, 0, sizeof(msgh));
2372 memset(control, 0, sizeof(control));
2374 /* set the payload */
2375 iov.iov_base = (uint8_t *) buf;
2378 msgh.msg_iov = &iov;
2379 msgh.msg_iovlen = 1;
2381 msgh.msg_control = control;
2382 msgh.msg_controllen = sizeof(control);
2384 cmsg = CMSG_FIRSTHDR(&msgh);
2386 cmsg->cmsg_len = CMSG_LEN(fd_size);
2387 cmsg->cmsg_level = SOL_SOCKET;
2388 cmsg->cmsg_type = SCM_RIGHTS;
2389 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2392 r = sendmsg(s->fd, &msgh, 0);
2393 } while (r < 0 && errno == EINTR);
2395 /* free the written msgfds, no matter what */
2396 if (s->write_msgfds_num) {
2397 g_free(s->write_msgfds);
2398 s->write_msgfds = 0;
2399 s->write_msgfds_num = 0;
2406 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2408 TCPCharDriver *s = chr->opaque;
2411 if (s->is_unix && s->write_msgfds_num) {
2412 return unix_send_msgfds(chr, buf, len);
2416 return io_channel_send(s->chan, buf, len);
2419 /* XXX: indicate an error ? */
2424 static int tcp_chr_read_poll(void *opaque)
2426 CharDriverState *chr = opaque;
2427 TCPCharDriver *s = chr->opaque;
2430 s->max_size = qemu_chr_be_can_write(chr);
2435 #define IAC_BREAK 243
2436 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2438 uint8_t *buf, int *size)
2440 /* Handle any telnet client's basic IAC options to satisfy char by
2441 * char mode with no echo. All IAC options will be removed from
2442 * the buf and the do_telnetopt variable will be used to track the
2443 * state of the width of the IAC information.
2445 * IAC commands come in sets of 3 bytes with the exception of the
2446 * "IAC BREAK" command and the double IAC.
2452 for (i = 0; i < *size; i++) {
2453 if (s->do_telnetopt > 1) {
2454 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2455 /* Double IAC means send an IAC */
2459 s->do_telnetopt = 1;
2461 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2462 /* Handle IAC break commands by sending a serial break */
2463 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
2468 if (s->do_telnetopt >= 4) {
2469 s->do_telnetopt = 1;
2472 if ((unsigned char)buf[i] == IAC) {
2473 s->do_telnetopt = 2;
2484 static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
2486 TCPCharDriver *s = chr->opaque;
2487 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2490 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2492 g_free(s->read_msgfds);
2494 s->read_msgfds_num = 0;
2500 static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2502 TCPCharDriver *s = chr->opaque;
2504 /* clear old pending fd array */
2505 if (s->write_msgfds) {
2506 g_free(s->write_msgfds);
2510 s->write_msgfds = g_malloc(num * sizeof(int));
2511 memcpy(s->write_msgfds, fds, num * sizeof(int));
2514 s->write_msgfds_num = num;
2520 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2522 TCPCharDriver *s = chr->opaque;
2523 struct cmsghdr *cmsg;
2525 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2528 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
2529 cmsg->cmsg_level != SOL_SOCKET ||
2530 cmsg->cmsg_type != SCM_RIGHTS) {
2534 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2540 /* close and clean read_msgfds */
2541 for (i = 0; i < s->read_msgfds_num; i++) {
2542 close(s->read_msgfds[i]);
2545 if (s->read_msgfds_num) {
2546 g_free(s->read_msgfds);
2549 s->read_msgfds_num = fd_size / sizeof(int);
2550 s->read_msgfds = g_malloc(fd_size);
2551 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2553 for (i = 0; i < s->read_msgfds_num; i++) {
2554 int fd = s->read_msgfds[i];
2559 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2562 #ifndef MSG_CMSG_CLOEXEC
2563 qemu_set_cloexec(fd);
2569 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2571 TCPCharDriver *s = chr->opaque;
2572 struct msghdr msg = { NULL, };
2573 struct iovec iov[1];
2575 struct cmsghdr cmsg;
2576 char control[CMSG_SPACE(sizeof(int))];
2581 iov[0].iov_base = buf;
2582 iov[0].iov_len = len;
2586 msg.msg_control = &msg_control;
2587 msg.msg_controllen = sizeof(msg_control);
2589 #ifdef MSG_CMSG_CLOEXEC
2590 flags |= MSG_CMSG_CLOEXEC;
2592 ret = recvmsg(s->fd, &msg, flags);
2593 if (ret > 0 && s->is_unix) {
2594 unix_process_msgfd(chr, &msg);
2600 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2602 TCPCharDriver *s = chr->opaque;
2603 return qemu_recv(s->fd, buf, len, 0);
2607 static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2609 TCPCharDriver *s = chr->opaque;
2610 return g_io_create_watch(s->chan, cond);
2613 static void tcp_chr_disconnect(CharDriverState *chr)
2615 TCPCharDriver *s = chr->opaque;
2618 if (s->listen_chan) {
2619 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2620 tcp_chr_accept, chr);
2622 remove_fd_in_watch(chr);
2623 g_io_channel_unref(s->chan);
2627 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2630 static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
2632 CharDriverState *chr = opaque;
2633 TCPCharDriver *s = chr->opaque;
2634 uint8_t buf[READ_BUF_LEN];
2637 if (!s->connected || s->max_size <= 0) {
2641 if (len > s->max_size)
2643 size = tcp_chr_recv(chr, (void *)buf, len);
2645 /* connection closed */
2646 tcp_chr_disconnect(chr);
2647 } else if (size > 0) {
2648 if (s->do_telnetopt)
2649 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2651 qemu_chr_be_write(chr, buf, size);
2657 static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2659 TCPCharDriver *s = chr->opaque;
2662 if (!s->connected) {
2666 size = tcp_chr_recv(chr, (void *) buf, len);
2668 /* connection closed */
2669 tcp_chr_disconnect(chr);
2676 CharDriverState *qemu_chr_open_eventfd(int eventfd)
2678 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2681 chr->avail_connections = 1;
2688 static gboolean tcp_chr_chan_close(GIOChannel *channel, GIOCondition cond,
2691 CharDriverState *chr = opaque;
2693 if (cond != G_IO_HUP) {
2697 /* connection closed */
2698 tcp_chr_disconnect(chr);
2699 if (chr->fd_hup_tag) {
2700 g_source_remove(chr->fd_hup_tag);
2701 chr->fd_hup_tag = 0;
2707 static void tcp_chr_connect(void *opaque)
2709 CharDriverState *chr = opaque;
2710 TCPCharDriver *s = chr->opaque;
2714 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2716 chr->fd_hup_tag = g_io_add_watch(s->chan, G_IO_HUP, tcp_chr_chan_close,
2719 qemu_chr_be_generic_open(chr);
2722 static void tcp_chr_update_read_handler(CharDriverState *chr)
2724 TCPCharDriver *s = chr->opaque;
2726 remove_fd_in_watch(chr);
2728 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2733 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2734 static void tcp_chr_telnet_init(int fd)
2737 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2738 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2739 send(fd, (char *)buf, 3, 0);
2740 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2741 send(fd, (char *)buf, 3, 0);
2742 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2743 send(fd, (char *)buf, 3, 0);
2744 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2745 send(fd, (char *)buf, 3, 0);
2748 static int tcp_chr_add_client(CharDriverState *chr, int fd)
2750 TCPCharDriver *s = chr->opaque;
2754 qemu_set_nonblock(fd);
2756 socket_set_nodelay(fd);
2758 s->chan = io_channel_from_socket(fd);
2759 if (s->listen_tag) {
2760 g_source_remove(s->listen_tag);
2763 tcp_chr_connect(chr);
2768 static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
2770 CharDriverState *chr = opaque;
2771 TCPCharDriver *s = chr->opaque;
2772 struct sockaddr_in saddr;
2774 struct sockaddr_un uaddr;
2776 struct sockaddr *addr;
2783 len = sizeof(uaddr);
2784 addr = (struct sockaddr *)&uaddr;
2788 len = sizeof(saddr);
2789 addr = (struct sockaddr *)&saddr;
2791 fd = qemu_accept(s->listen_fd, addr, &len);
2792 if (fd < 0 && errno != EINTR) {
2795 } else if (fd >= 0) {
2796 if (s->do_telnetopt)
2797 tcp_chr_telnet_init(fd);
2801 if (tcp_chr_add_client(chr, fd) < 0)
2807 static void tcp_chr_close(CharDriverState *chr)
2809 TCPCharDriver *s = chr->opaque;
2812 remove_fd_in_watch(chr);
2814 g_io_channel_unref(s->chan);
2818 if (s->listen_fd >= 0) {
2819 if (s->listen_tag) {
2820 g_source_remove(s->listen_tag);
2823 if (s->listen_chan) {
2824 g_io_channel_unref(s->listen_chan);
2826 closesocket(s->listen_fd);
2828 if (s->read_msgfds_num) {
2829 for (i = 0; i < s->read_msgfds_num; i++) {
2830 close(s->read_msgfds[i]);
2832 g_free(s->read_msgfds);
2834 if (s->write_msgfds_num) {
2835 g_free(s->write_msgfds);
2838 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2841 static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2842 bool is_listen, bool is_telnet,
2843 bool is_waitconnect,
2846 CharDriverState *chr = NULL;
2847 TCPCharDriver *s = NULL;
2848 char host[NI_MAXHOST], serv[NI_MAXSERV];
2849 const char *left = "", *right = "";
2850 struct sockaddr_storage ss;
2851 socklen_t ss_len = sizeof(ss);
2853 memset(&ss, 0, ss_len);
2854 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2855 error_setg_errno(errp, errno, "getsockname");
2859 chr = qemu_chr_alloc();
2860 s = g_malloc0(sizeof(TCPCharDriver));
2866 s->read_msgfds_num = 0;
2867 s->write_msgfds = 0;
2868 s->write_msgfds_num = 0;
2870 chr->filename = g_malloc(256);
2871 switch (ss.ss_family) {
2875 snprintf(chr->filename, 256, "unix:%s%s",
2876 ((struct sockaddr_un *)(&ss))->sun_path,
2877 is_listen ? ",server" : "");
2885 s->do_nodelay = do_nodelay;
2886 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2887 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
2888 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
2889 is_telnet ? "telnet" : "tcp",
2890 left, host, right, serv,
2891 is_listen ? ",server" : "");
2896 chr->chr_write = tcp_chr_write;
2897 chr->chr_sync_read = tcp_chr_sync_read;
2898 chr->chr_close = tcp_chr_close;
2899 chr->get_msgfds = tcp_get_msgfds;
2900 chr->set_msgfds = tcp_set_msgfds;
2901 chr->chr_add_client = tcp_chr_add_client;
2902 chr->chr_add_watch = tcp_chr_add_watch;
2903 chr->chr_update_read_handler = tcp_chr_update_read_handler;
2904 /* be isn't opened until we get a connection */
2905 chr->explicit_be_open = true;
2909 s->listen_chan = io_channel_from_socket(s->listen_fd);
2910 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
2912 s->do_telnetopt = 1;
2917 socket_set_nodelay(fd);
2918 s->chan = io_channel_from_socket(s->fd);
2919 tcp_chr_connect(chr);
2922 if (is_listen && is_waitconnect) {
2923 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2925 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
2926 qemu_set_nonblock(s->listen_fd);
2931 static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2933 CharDriverState *chr = NULL;
2934 Error *local_err = NULL;
2937 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2938 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2939 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2940 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2941 bool is_unix = qemu_opt_get(opts, "path") != NULL;
2945 fd = unix_listen_opts(opts, &local_err);
2947 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
2951 fd = inet_listen_opts(opts, 0, &local_err);
2953 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
2960 if (!is_waitconnect)
2961 qemu_set_nonblock(fd);
2963 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2964 is_waitconnect, &local_err);
2973 qerror_report_err(local_err);
2974 error_free(local_err);
2980 g_free(chr->opaque);
2986 /*********************************************************/
2987 /* Ring buffer chardev */
2994 } RingBufCharDriver;
2996 static size_t ringbuf_count(const CharDriverState *chr)
2998 const RingBufCharDriver *d = chr->opaque;
3000 return d->prod - d->cons;
3003 static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3005 RingBufCharDriver *d = chr->opaque;
3008 if (!buf || (len < 0)) {
3012 for (i = 0; i < len; i++ ) {
3013 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3014 if (d->prod - d->cons > d->size) {
3015 d->cons = d->prod - d->size;
3022 static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
3024 RingBufCharDriver *d = chr->opaque;
3027 for (i = 0; i < len && d->cons != d->prod; i++) {
3028 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
3034 static void ringbuf_chr_close(struct CharDriverState *chr)
3036 RingBufCharDriver *d = chr->opaque;
3043 static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3046 CharDriverState *chr;
3047 RingBufCharDriver *d;
3049 chr = qemu_chr_alloc();
3050 d = g_malloc(sizeof(*d));
3052 d->size = opts->has_size ? opts->size : 65536;
3054 /* The size must be power of 2 */
3055 if (d->size & (d->size - 1)) {
3056 error_setg(errp, "size of ringbuf chardev must be power of two");
3062 d->cbuf = g_malloc0(d->size);
3065 chr->chr_write = ringbuf_chr_write;
3066 chr->chr_close = ringbuf_chr_close;
3076 bool chr_is_ringbuf(const CharDriverState *chr)
3078 return chr->chr_write == ringbuf_chr_write;
3081 void qmp_ringbuf_write(const char *device, const char *data,
3082 bool has_format, enum DataFormat format,
3085 CharDriverState *chr;
3086 const uint8_t *write_data;
3090 chr = qemu_chr_find(device);
3092 error_setg(errp, "Device '%s' not found", device);
3096 if (!chr_is_ringbuf(chr)) {
3097 error_setg(errp,"%s is not a ringbuf device", device);
3101 if (has_format && (format == DATA_FORMAT_BASE64)) {
3102 write_data = g_base64_decode(data, &write_count);
3104 write_data = (uint8_t *)data;
3105 write_count = strlen(data);
3108 ret = ringbuf_chr_write(chr, write_data, write_count);
3110 if (write_data != (uint8_t *)data) {
3111 g_free((void *)write_data);
3115 error_setg(errp, "Failed to write to device %s", device);
3120 char *qmp_ringbuf_read(const char *device, int64_t size,
3121 bool has_format, enum DataFormat format,
3124 CharDriverState *chr;
3129 chr = qemu_chr_find(device);
3131 error_setg(errp, "Device '%s' not found", device);
3135 if (!chr_is_ringbuf(chr)) {
3136 error_setg(errp,"%s is not a ringbuf device", device);
3141 error_setg(errp, "size must be greater than zero");
3145 count = ringbuf_count(chr);
3146 size = size > count ? count : size;
3147 read_data = g_malloc(size + 1);
3149 ringbuf_chr_read(chr, read_data, size);
3151 if (has_format && (format == DATA_FORMAT_BASE64)) {
3152 data = g_base64_encode(read_data, size);
3156 * FIXME should read only complete, valid UTF-8 characters up
3157 * to @size bytes. Invalid sequences should be replaced by a
3158 * suitable replacement character. Except when (and only
3159 * when) ring buffer lost characters since last read, initial
3160 * continuation characters should be dropped.
3162 read_data[size] = 0;
3163 data = (char *)read_data;
3169 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
3171 char host[65], port[33], width[8], height[8];
3175 Error *local_err = NULL;
3177 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
3179 qerror_report_err(local_err);
3180 error_free(local_err);
3184 if (strstart(filename, "mon:", &p)) {
3186 qemu_opt_set(opts, "mux", "on");
3187 if (strcmp(filename, "stdio") == 0) {
3188 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3189 * but pass it to the guest. Handle this only for compat syntax,
3190 * for -chardev syntax we have special option for this.
3191 * This is what -nographic did, redirecting+muxing serial+monitor
3192 * to stdio causing Ctrl+C to be passed to guest. */
3193 qemu_opt_set(opts, "signal", "off");
3197 if (strcmp(filename, "null") == 0 ||
3198 strcmp(filename, "pty") == 0 ||
3199 strcmp(filename, "msmouse") == 0 ||
3200 strcmp(filename, "braille") == 0 ||
3201 strcmp(filename, "stdio") == 0) {
3202 qemu_opt_set(opts, "backend", filename);
3205 if (strstart(filename, "vc", &p)) {
3206 qemu_opt_set(opts, "backend", "vc");
3208 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
3210 qemu_opt_set(opts, "width", width);
3211 qemu_opt_set(opts, "height", height);
3212 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
3214 qemu_opt_set(opts, "cols", width);
3215 qemu_opt_set(opts, "rows", height);
3222 if (strcmp(filename, "con:") == 0) {
3223 qemu_opt_set(opts, "backend", "console");
3226 if (strstart(filename, "COM", NULL)) {
3227 qemu_opt_set(opts, "backend", "serial");
3228 qemu_opt_set(opts, "path", filename);
3231 if (strstart(filename, "file:", &p)) {
3232 qemu_opt_set(opts, "backend", "file");
3233 qemu_opt_set(opts, "path", p);
3236 if (strstart(filename, "pipe:", &p)) {
3237 qemu_opt_set(opts, "backend", "pipe");
3238 qemu_opt_set(opts, "path", p);
3241 if (strstart(filename, "tcp:", &p) ||
3242 strstart(filename, "telnet:", &p)) {
3243 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3245 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3248 qemu_opt_set(opts, "backend", "socket");
3249 qemu_opt_set(opts, "host", host);
3250 qemu_opt_set(opts, "port", port);
3251 if (p[pos] == ',') {
3252 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3255 if (strstart(filename, "telnet:", &p))
3256 qemu_opt_set(opts, "telnet", "on");
3259 if (strstart(filename, "udp:", &p)) {
3260 qemu_opt_set(opts, "backend", "udp");
3261 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3263 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
3267 qemu_opt_set(opts, "host", host);
3268 qemu_opt_set(opts, "port", port);
3269 if (p[pos] == '@') {
3271 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3273 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
3277 qemu_opt_set(opts, "localaddr", host);
3278 qemu_opt_set(opts, "localport", port);
3282 if (strstart(filename, "unix:", &p)) {
3283 qemu_opt_set(opts, "backend", "socket");
3284 if (qemu_opts_do_parse(opts, p, "path") != 0)
3288 if (strstart(filename, "/dev/parport", NULL) ||
3289 strstart(filename, "/dev/ppi", NULL)) {
3290 qemu_opt_set(opts, "backend", "parport");
3291 qemu_opt_set(opts, "path", filename);
3294 if (strstart(filename, "/dev/", NULL)) {
3295 qemu_opt_set(opts, "backend", "tty");
3296 qemu_opt_set(opts, "path", filename);
3301 qemu_opts_del(opts);
3305 static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3308 const char *path = qemu_opt_get(opts, "path");
3311 error_setg(errp, "chardev: file: no filename given");
3314 backend->file = g_new0(ChardevFile, 1);
3315 backend->file->out = g_strdup(path);
3318 static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3321 backend->stdio = g_new0(ChardevStdio, 1);
3322 backend->stdio->has_signal = true;
3323 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
3326 static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3329 const char *device = qemu_opt_get(opts, "path");
3331 if (device == NULL) {
3332 error_setg(errp, "chardev: serial/tty: no device path given");
3335 backend->serial = g_new0(ChardevHostdev, 1);
3336 backend->serial->device = g_strdup(device);
3339 static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3342 const char *device = qemu_opt_get(opts, "path");
3344 if (device == NULL) {
3345 error_setg(errp, "chardev: parallel: no device path given");
3348 backend->parallel = g_new0(ChardevHostdev, 1);
3349 backend->parallel->device = g_strdup(device);
3352 static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3355 const char *device = qemu_opt_get(opts, "path");
3357 if (device == NULL) {
3358 error_setg(errp, "chardev: pipe: no device path given");
3361 backend->pipe = g_new0(ChardevHostdev, 1);
3362 backend->pipe->device = g_strdup(device);
3365 static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3370 backend->ringbuf = g_new0(ChardevRingbuf, 1);
3372 val = qemu_opt_get_size(opts, "size", 0);
3374 backend->ringbuf->has_size = true;
3375 backend->ringbuf->size = val;
3379 static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3382 const char *chardev = qemu_opt_get(opts, "chardev");
3384 if (chardev == NULL) {
3385 error_setg(errp, "chardev: mux: no chardev given");
3388 backend->mux = g_new0(ChardevMux, 1);
3389 backend->mux->chardev = g_strdup(chardev);
3392 typedef struct CharDriver {
3395 CharDriverState *(*open)(QemuOpts *opts);
3396 /* new, qapi-based */
3397 ChardevBackendKind kind;
3398 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
3401 static GSList *backends;
3403 void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3407 s = g_malloc0(sizeof(*s));
3408 s->name = g_strdup(name);
3411 backends = g_slist_append(backends, s);
3414 void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
3415 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3419 s = g_malloc0(sizeof(*s));
3420 s->name = g_strdup(name);
3424 backends = g_slist_append(backends, s);
3427 CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
3428 void (*init)(struct CharDriverState *s),
3431 Error *local_err = NULL;
3433 CharDriverState *chr;
3436 if (qemu_opts_id(opts) == NULL) {
3437 error_setg(errp, "chardev: no id specified");
3441 if (qemu_opt_get(opts, "backend") == NULL) {
3442 error_setg(errp, "chardev: \"%s\" missing backend",
3443 qemu_opts_id(opts));
3446 for (i = backends; i; i = i->next) {
3449 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
3454 error_setg(errp, "chardev: backend \"%s\" not found",
3455 qemu_opt_get(opts, "backend"));
3460 /* using new, qapi init */
3461 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3462 ChardevReturn *ret = NULL;
3463 const char *id = qemu_opts_id(opts);
3466 if (qemu_opt_get_bool(opts, "mux", 0)) {
3467 bid = g_strdup_printf("%s-base", id);
3471 backend->kind = cd->kind;
3473 cd->parse(opts, backend, &local_err);
3475 error_propagate(errp, local_err);
3479 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3485 qapi_free_ChardevBackend(backend);
3486 qapi_free_ChardevReturn(ret);
3487 backend = g_new0(ChardevBackend, 1);
3488 backend->mux = g_new0(ChardevMux, 1);
3489 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3490 backend->mux->chardev = g_strdup(bid);
3491 ret = qmp_chardev_add(id, backend, errp);
3493 chr = qemu_chr_find(bid);
3494 qemu_chr_delete(chr);
3500 chr = qemu_chr_find(id);
3504 qapi_free_ChardevBackend(backend);
3505 qapi_free_ChardevReturn(ret);
3510 chr = cd->open(opts);
3512 error_setg(errp, "chardev: opening backend \"%s\" failed",
3513 qemu_opt_get(opts, "backend"));
3518 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
3520 /* if we didn't create the chardev via qmp_chardev_add, we
3521 * need to send the OPENED event here
3523 if (!chr->explicit_be_open) {
3524 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3526 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3528 if (qemu_opt_get_bool(opts, "mux", 0)) {
3529 CharDriverState *base = chr;
3530 int len = strlen(qemu_opts_id(opts)) + 6;
3531 base->label = g_malloc(len);
3532 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3533 chr = qemu_chr_open_mux(base);
3534 chr->filename = base->filename;
3535 chr->avail_connections = MAX_MUX;
3536 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3538 chr->avail_connections = 1;
3540 chr->label = g_strdup(qemu_opts_id(opts));
3545 qemu_opts_del(opts);
3549 CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
3552 CharDriverState *chr;
3556 if (strstart(filename, "chardev:", &p)) {
3557 return qemu_chr_find(p);
3560 opts = qemu_chr_parse_compat(label, filename);
3564 chr = qemu_chr_new_from_opts(opts, init, &err);
3566 error_report("%s", error_get_pretty(err));
3569 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3570 qemu_chr_fe_claim_no_fail(chr);
3571 monitor_init(chr, MONITOR_USE_READLINE);
3576 void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
3578 if (chr->chr_set_echo) {
3579 chr->chr_set_echo(chr, echo);
3583 void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
3585 if (chr->fe_open == fe_open) {
3588 chr->fe_open = fe_open;
3589 if (chr->chr_set_fe_open) {
3590 chr->chr_set_fe_open(chr, fe_open);
3594 void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3596 if (chr->chr_fe_event) {
3597 chr->chr_fe_event(chr, event);
3601 int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3602 GIOFunc func, void *user_data)
3607 if (s->chr_add_watch == NULL) {
3611 src = s->chr_add_watch(s, cond);
3612 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3613 tag = g_source_attach(src, NULL);
3614 g_source_unref(src);
3619 int qemu_chr_fe_claim(CharDriverState *s)
3621 if (s->avail_connections < 1) {
3624 s->avail_connections--;
3628 void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3630 if (qemu_chr_fe_claim(s) != 0) {
3631 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3632 __func__, s->label);
3637 void qemu_chr_fe_release(CharDriverState *s)
3639 s->avail_connections++;
3642 void qemu_chr_delete(CharDriverState *chr)
3644 QTAILQ_REMOVE(&chardevs, chr, next);
3645 if (chr->chr_close) {
3646 chr->chr_close(chr);
3648 g_free(chr->filename);
3651 qemu_opts_del(chr->opts);
3656 ChardevInfoList *qmp_query_chardev(Error **errp)
3658 ChardevInfoList *chr_list = NULL;
3659 CharDriverState *chr;
3661 QTAILQ_FOREACH(chr, &chardevs, next) {
3662 ChardevInfoList *info = g_malloc0(sizeof(*info));
3663 info->value = g_malloc0(sizeof(*info->value));
3664 info->value->label = g_strdup(chr->label);
3665 info->value->filename = g_strdup(chr->filename);
3667 info->next = chr_list;
3674 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3676 ChardevBackendInfoList *backend_list = NULL;
3677 CharDriver *c = NULL;
3680 for (i = backends; i; i = i->next) {
3681 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3683 info->value = g_malloc0(sizeof(*info->value));
3684 info->value->name = g_strdup(c->name);
3686 info->next = backend_list;
3687 backend_list = info;
3690 return backend_list;
3693 CharDriverState *qemu_chr_find(const char *name)
3695 CharDriverState *chr;
3697 QTAILQ_FOREACH(chr, &chardevs, next) {
3698 if (strcmp(chr->label, name) != 0)
3705 /* Get a character (serial) device interface. */
3706 CharDriverState *qemu_char_get_next_serial(void)
3708 static int next_serial;
3709 CharDriverState *chr;
3711 /* FIXME: This function needs to go away: use chardev properties! */
3713 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3714 chr = serial_hds[next_serial++];
3715 qemu_chr_fe_claim_no_fail(chr);
3721 QemuOptsList qemu_chardev_opts = {
3723 .implied_opt_name = "backend",
3724 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3728 .type = QEMU_OPT_STRING,
3731 .type = QEMU_OPT_STRING,
3734 .type = QEMU_OPT_STRING,
3737 .type = QEMU_OPT_STRING,
3739 .name = "localaddr",
3740 .type = QEMU_OPT_STRING,
3742 .name = "localport",
3743 .type = QEMU_OPT_STRING,
3746 .type = QEMU_OPT_NUMBER,
3749 .type = QEMU_OPT_BOOL,
3752 .type = QEMU_OPT_BOOL,
3755 .type = QEMU_OPT_BOOL,
3758 .type = QEMU_OPT_BOOL,
3761 .type = QEMU_OPT_BOOL,
3764 .type = QEMU_OPT_BOOL,
3767 .type = QEMU_OPT_NUMBER,
3770 .type = QEMU_OPT_NUMBER,
3773 .type = QEMU_OPT_NUMBER,
3776 .type = QEMU_OPT_NUMBER,
3779 .type = QEMU_OPT_BOOL,
3782 .type = QEMU_OPT_BOOL,
3785 .type = QEMU_OPT_STRING,
3788 .type = QEMU_OPT_NUMBER,
3791 .type = QEMU_OPT_SIZE,
3794 .type = QEMU_OPT_STRING,
3796 { /* end of list */ }
3802 static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3807 error_setg(errp, "input file not supported");
3811 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3812 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3813 if (out == INVALID_HANDLE_VALUE) {
3814 error_setg(errp, "open %s failed", file->out);
3817 return qemu_chr_open_win_file(out);
3820 static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3823 return qemu_chr_open_win_path(serial->device);
3826 static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3829 error_setg(errp, "character device backend type 'parallel' not supported");
3835 static int qmp_chardev_open_file_source(char *src, int flags,
3840 TFR(fd = qemu_open(src, flags, 0666));
3842 error_setg_file_open(errp, errno, src);
3847 static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3849 int flags, in = -1, out;
3851 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3852 out = qmp_chardev_open_file_source(file->out, flags, errp);
3859 in = qmp_chardev_open_file_source(file->in, flags, errp);
3866 return qemu_chr_open_fd(in, out);
3869 static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3872 #ifdef HAVE_CHARDEV_TTY
3875 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3879 qemu_set_nonblock(fd);
3880 return qemu_chr_open_tty_fd(fd);
3882 error_setg(errp, "character device backend type 'serial' not supported");
3887 static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3890 #ifdef HAVE_CHARDEV_PARPORT
3893 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3897 return qemu_chr_open_pp_fd(fd);
3899 error_setg(errp, "character device backend type 'parallel' not supported");
3906 static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3909 SocketAddress *addr = sock->addr;
3910 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3911 bool is_listen = sock->has_server ? sock->server : true;
3912 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3913 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3917 fd = socket_listen(addr, errp);
3919 fd = socket_connect(addr, errp, NULL, NULL);
3924 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3925 is_telnet, is_waitconnect, errp);
3928 static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3933 fd = socket_dgram(udp->remote, udp->local, errp);
3937 return qemu_chr_open_udp_fd(fd);
3940 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3943 ChardevReturn *ret = g_new0(ChardevReturn, 1);
3944 CharDriverState *base, *chr = NULL;
3946 chr = qemu_chr_find(id);
3948 error_setg(errp, "Chardev '%s' already exists", id);
3953 switch (backend->kind) {
3954 case CHARDEV_BACKEND_KIND_FILE:
3955 chr = qmp_chardev_open_file(backend->file, errp);
3957 case CHARDEV_BACKEND_KIND_SERIAL:
3958 chr = qmp_chardev_open_serial(backend->serial, errp);
3960 case CHARDEV_BACKEND_KIND_PARALLEL:
3961 chr = qmp_chardev_open_parallel(backend->parallel, errp);
3963 case CHARDEV_BACKEND_KIND_PIPE:
3964 chr = qemu_chr_open_pipe(backend->pipe);
3966 case CHARDEV_BACKEND_KIND_SOCKET:
3967 chr = qmp_chardev_open_socket(backend->socket, errp);
3969 case CHARDEV_BACKEND_KIND_UDP:
3970 chr = qmp_chardev_open_udp(backend->udp, errp);
3972 #ifdef HAVE_CHARDEV_TTY
3973 case CHARDEV_BACKEND_KIND_PTY:
3974 chr = qemu_chr_open_pty(id, ret);
3977 case CHARDEV_BACKEND_KIND_NULL:
3978 chr = qemu_chr_open_null();
3980 case CHARDEV_BACKEND_KIND_MUX:
3981 base = qemu_chr_find(backend->mux->chardev);
3983 error_setg(errp, "mux: base chardev %s not found",
3984 backend->mux->chardev);
3987 chr = qemu_chr_open_mux(base);
3989 case CHARDEV_BACKEND_KIND_MSMOUSE:
3990 chr = qemu_chr_open_msmouse();
3992 #ifdef CONFIG_BRLAPI
3993 case CHARDEV_BACKEND_KIND_BRAILLE:
3994 chr = chr_baum_init();
3997 case CHARDEV_BACKEND_KIND_STDIO:
3998 chr = qemu_chr_open_stdio(backend->stdio);
4001 case CHARDEV_BACKEND_KIND_CONSOLE:
4002 chr = qemu_chr_open_win_con();
4006 case CHARDEV_BACKEND_KIND_SPICEVMC:
4007 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4009 case CHARDEV_BACKEND_KIND_SPICEPORT:
4010 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4013 case CHARDEV_BACKEND_KIND_VC:
4014 chr = vc_init(backend->vc);
4016 case CHARDEV_BACKEND_KIND_RINGBUF:
4017 case CHARDEV_BACKEND_KIND_MEMORY:
4018 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
4021 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4026 * Character backend open hasn't been fully converted to the Error
4027 * API. Some opens fail without setting an error. Set a generic
4029 * TODO full conversion to Error API
4031 if (chr == NULL && errp && !*errp) {
4032 error_setg(errp, "Failed to create chardev");
4035 chr->label = g_strdup(id);
4036 chr->avail_connections =
4037 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
4038 if (!chr->filename) {
4039 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4041 if (!chr->explicit_be_open) {
4042 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4044 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4052 void qmp_chardev_remove(const char *id, Error **errp)
4054 CharDriverState *chr;
4056 chr = qemu_chr_find(id);
4058 error_setg(errp, "Chardev '%s' not found", id);
4061 if (chr->chr_can_read || chr->chr_read ||
4062 chr->chr_event || chr->handler_opaque) {
4063 error_setg(errp, "Chardev '%s' is busy", id);
4066 qemu_chr_delete(chr);
4069 static void register_types(void)
4071 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4072 register_char_driver("socket", qemu_chr_open_socket);
4073 register_char_driver("udp", qemu_chr_open_udp);
4074 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4075 qemu_chr_parse_ringbuf);
4076 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4077 qemu_chr_parse_file_out);
4078 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4079 qemu_chr_parse_stdio);
4080 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4081 qemu_chr_parse_serial);
4082 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4083 qemu_chr_parse_serial);
4084 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4085 qemu_chr_parse_parallel);
4086 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4087 qemu_chr_parse_parallel);
4088 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4089 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4090 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4091 qemu_chr_parse_pipe);
4092 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4093 qemu_chr_parse_mux);
4094 /* Bug-compatibility: */
4095 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4096 qemu_chr_parse_ringbuf);
4097 /* this must be done after machine init, since we register FEs with muxes
4098 * as part of realize functions like serial_isa_realizefn when -nographic
4101 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
4104 type_init(register_types);