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 "ui/console.h"
27 #include "sysemu/sysemu.h"
28 #include "qemu/timer.h"
29 #include "sysemu/char.h"
31 #include "qmp-commands.h"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #include <arpa/inet.h>
53 #include <sys/select.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57 #include <dev/ppbus/ppi.h>
58 #include <dev/ppbus/ppbconf.h>
59 #elif defined(__DragonFly__)
60 #include <dev/misc/ppi/ppi.h>
61 #include <bus/ppbus/ppbconf.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.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>
83 #include "qemu/sockets.h"
84 #include "ui/qemu-spice.h"
86 #define READ_BUF_LEN 4096
88 /***********************************************************/
89 /* character device */
91 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
92 QTAILQ_HEAD_INITIALIZER(chardevs
);
94 void qemu_chr_be_event(CharDriverState
*s
, int event
)
96 /* Keep track if the char device is open */
98 case CHR_EVENT_OPENED
:
101 case CHR_EVENT_CLOSED
:
108 s
->chr_event(s
->handler_opaque
, event
);
111 void qemu_chr_be_generic_open(CharDriverState
*s
)
113 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
116 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
118 return s
->chr_write(s
, buf
, len
);
121 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
126 while (offset
< len
) {
128 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
129 if (res
== -1 && errno
== EAGAIN
) {
132 } while (res
== -1 && errno
== EAGAIN
);
148 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
152 return s
->chr_ioctl(s
, cmd
, arg
);
155 int qemu_chr_be_can_write(CharDriverState
*s
)
157 if (!s
->chr_can_read
)
159 return s
->chr_can_read(s
->handler_opaque
);
162 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
165 s
->chr_read(s
->handler_opaque
, buf
, len
);
169 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
171 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
174 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
176 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
179 void qemu_chr_accept_input(CharDriverState
*s
)
181 if (s
->chr_accept_input
)
182 s
->chr_accept_input(s
);
186 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
188 char buf
[READ_BUF_LEN
];
191 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
192 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
196 void qemu_chr_add_handlers(CharDriverState
*s
,
197 IOCanReadHandler
*fd_can_read
,
198 IOReadHandler
*fd_read
,
199 IOEventHandler
*fd_event
,
204 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
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
);
216 if (!s
->explicit_fe_open
) {
217 qemu_chr_fe_set_open(s
, fe_open
);
220 /* We're connecting to an already opened device, so let's make sure we
221 also get the open event */
222 if (fe_open
&& s
->be_open
) {
223 qemu_chr_be_generic_open(s
);
227 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
232 static CharDriverState
*qemu_chr_open_null(void)
234 CharDriverState
*chr
;
236 chr
= g_malloc0(sizeof(CharDriverState
));
237 chr
->chr_write
= null_chr_write
;
238 chr
->explicit_be_open
= true;
242 /* MUX driver for serial I/O splitting */
244 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
245 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
247 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
248 IOReadHandler
*chr_read
[MAX_MUX
];
249 IOEventHandler
*chr_event
[MAX_MUX
];
250 void *ext_opaque
[MAX_MUX
];
251 CharDriverState
*drv
;
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
259 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
264 int64_t timestamps_start
;
268 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
270 MuxDriver
*d
= chr
->opaque
;
272 if (!d
->timestamps
) {
273 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
278 for (i
= 0; i
< len
; i
++) {
284 ti
= qemu_get_clock_ms(rt_clock
);
285 if (d
->timestamps_start
== -1)
286 d
->timestamps_start
= ti
;
287 ti
-= d
->timestamps_start
;
289 snprintf(buf1
, sizeof(buf1
),
290 "[%02d:%02d:%02d.%03d] ",
295 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
298 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
299 if (buf
[i
] == '\n') {
307 static 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",
318 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
319 static void mux_print_help(CharDriverState
*chr
)
322 char ebuf
[15] = "Escape-Char";
323 char cbuf
[50] = "\n\r";
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');
329 snprintf(cbuf
, sizeof(cbuf
),
330 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
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
));
339 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
344 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
346 if (d
->chr_event
[mux_nr
])
347 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
350 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
352 if (d
->term_got_escape
) {
353 d
->term_got_escape
= 0;
354 if (ch
== term_escape_char
)
363 const char *term
= "QEMU: Terminated\n\r";
364 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
372 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
375 /* Switch to the next registered device */
376 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
378 if (d
->focus
>= d
->mux_cnt
)
380 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
383 d
->timestamps
= !d
->timestamps
;
384 d
->timestamps_start
= -1;
388 } else if (ch
== term_escape_char
) {
389 d
->term_got_escape
= 1;
397 static void mux_chr_accept_input(CharDriverState
*chr
)
399 MuxDriver
*d
= chr
->opaque
;
402 while (d
->prod
[m
] != d
->cons
[m
] &&
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
],
406 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
410 static int mux_chr_can_read(void *opaque
)
412 CharDriverState
*chr
= opaque
;
413 MuxDriver
*d
= chr
->opaque
;
416 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
418 if (d
->chr_can_read
[m
])
419 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
423 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
425 CharDriverState
*chr
= opaque
;
426 MuxDriver
*d
= chr
->opaque
;
430 mux_chr_accept_input (opaque
);
432 for(i
= 0; i
< size
; i
++)
433 if (mux_proc_byte(chr
, d
, buf
[i
])) {
434 if (d
->prod
[m
] == d
->cons
[m
] &&
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);
439 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
443 static void mux_chr_event(void *opaque
, int event
)
445 CharDriverState
*chr
= opaque
;
446 MuxDriver
*d
= chr
->opaque
;
449 /* Send the event to all registered listeners */
450 for (i
= 0; i
< d
->mux_cnt
; i
++)
451 mux_chr_send_event(d
, i
, event
);
454 static void mux_chr_update_read_handler(CharDriverState
*chr
)
456 MuxDriver
*d
= chr
->opaque
;
458 if (d
->mux_cnt
>= MAX_MUX
) {
459 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
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
,
471 if (d
->focus
!= -1) {
472 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
474 d
->focus
= d
->mux_cnt
;
476 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
479 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
481 CharDriverState
*chr
;
484 chr
= g_malloc0(sizeof(CharDriverState
));
485 d
= g_malloc0(sizeof(MuxDriver
));
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
;
493 /* Frontend guest-open / -close notification is not support with muxes */
494 chr
->chr_set_fe_open
= NULL
;
501 int send_all(int fd
, const void *buf
, int len1
)
507 ret
= send(fd
, buf
, len
, 0);
509 errno
= WSAGetLastError();
510 if (errno
!= WSAEWOULDBLOCK
) {
513 } else if (ret
== 0) {
525 int send_all(int fd
, const void *_buf
, int len1
)
528 const uint8_t *buf
= _buf
;
532 ret
= write(fd
, buf
, len
);
534 if (errno
!= EINTR
&& errno
!= EAGAIN
)
536 } else if (ret
== 0) {
546 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
552 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
554 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
571 typedef struct IOWatchPoll
578 IOCanReadHandler
*fd_can_read
;
583 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
585 return container_of(source
, IOWatchPoll
, parent
);
588 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
590 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
591 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
592 bool was_active
= iwp
->src
!= NULL
;
593 if (was_active
== now_active
) {
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
);
600 g_source_attach(iwp
->src
, NULL
);
602 g_source_destroy(iwp
->src
);
603 g_source_unref(iwp
->src
);
609 static gboolean
io_watch_poll_check(GSource
*source
)
614 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
620 static void io_watch_poll_finalize(GSource
*source
)
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.
629 * This would be solved much more elegantly by child sources,
630 * but we support older glib versions that do not have them.
632 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
633 assert(iwp
->src
== NULL
);
636 static 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
,
643 /* Can only be used for read */
644 static guint
io_add_watch_poll(GIOChannel
*channel
,
645 IOCanReadHandler
*fd_can_read
,
652 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
653 iwp
->fd_can_read
= fd_can_read
;
654 iwp
->opaque
= user_data
;
655 iwp
->channel
= channel
;
656 iwp
->fd_read
= (GSourceFunc
) fd_read
;
659 tag
= g_source_attach(&iwp
->parent
, NULL
);
660 g_source_unref(&iwp
->parent
);
664 static void io_remove_watch_poll(guint tag
)
669 g_return_if_fail (tag
> 0);
671 source
= g_main_context_find_source_by_id(NULL
, tag
);
672 g_return_if_fail (source
!= NULL
);
674 iwp
= io_watch_poll_from_source(source
);
676 g_source_destroy(iwp
->src
);
677 g_source_unref(iwp
->src
);
680 g_source_destroy(&iwp
->parent
);
684 static GIOChannel
*io_channel_from_fd(int fd
)
692 chan
= g_io_channel_unix_new(fd
);
694 g_io_channel_set_encoding(chan
, NULL
, NULL
);
695 g_io_channel_set_buffered(chan
, FALSE
);
701 static GIOChannel
*io_channel_from_socket(int fd
)
710 chan
= g_io_channel_win32_new_socket(fd
);
712 chan
= g_io_channel_unix_new(fd
);
715 g_io_channel_set_encoding(chan
, NULL
, NULL
);
716 g_io_channel_set_buffered(chan
, FALSE
);
721 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
724 GIOStatus status
= G_IO_STATUS_NORMAL
;
726 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
727 gsize bytes_written
= 0;
729 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
730 &bytes_written
, NULL
);
731 offset
+= bytes_written
;
738 case G_IO_STATUS_NORMAL
:
741 case G_IO_STATUS_AGAIN
:
753 typedef struct FDCharDriver
{
754 CharDriverState
*chr
;
755 GIOChannel
*fd_in
, *fd_out
;
758 QTAILQ_ENTRY(FDCharDriver
) node
;
761 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
763 FDCharDriver
*s
= chr
->opaque
;
765 return io_channel_send(s
->fd_out
, buf
, len
);
768 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
770 CharDriverState
*chr
= opaque
;
771 FDCharDriver
*s
= chr
->opaque
;
773 uint8_t buf
[READ_BUF_LEN
];
778 if (len
> s
->max_size
) {
785 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
786 len
, &bytes_read
, NULL
);
787 if (status
== G_IO_STATUS_EOF
) {
789 io_remove_watch_poll(s
->fd_in_tag
);
792 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
795 if (status
== G_IO_STATUS_NORMAL
) {
796 qemu_chr_be_write(chr
, buf
, bytes_read
);
802 static int fd_chr_read_poll(void *opaque
)
804 CharDriverState
*chr
= opaque
;
805 FDCharDriver
*s
= chr
->opaque
;
807 s
->max_size
= qemu_chr_be_can_write(chr
);
811 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
813 FDCharDriver
*s
= chr
->opaque
;
814 return g_io_create_watch(s
->fd_out
, cond
);
817 static void fd_chr_update_read_handler(CharDriverState
*chr
)
819 FDCharDriver
*s
= chr
->opaque
;
822 io_remove_watch_poll(s
->fd_in_tag
);
827 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
831 static void fd_chr_close(struct CharDriverState
*chr
)
833 FDCharDriver
*s
= chr
->opaque
;
836 io_remove_watch_poll(s
->fd_in_tag
);
841 g_io_channel_unref(s
->fd_in
);
844 g_io_channel_unref(s
->fd_out
);
848 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
851 /* open a character device to a unix fd */
852 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
854 CharDriverState
*chr
;
857 chr
= g_malloc0(sizeof(CharDriverState
));
858 s
= g_malloc0(sizeof(FDCharDriver
));
859 s
->fd_in
= io_channel_from_fd(fd_in
);
860 s
->fd_out
= io_channel_from_fd(fd_out
);
861 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
864 chr
->chr_add_watch
= fd_chr_add_watch
;
865 chr
->chr_write
= fd_chr_write
;
866 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
867 chr
->chr_close
= fd_chr_close
;
872 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
875 char filename_in
[256], filename_out
[256];
876 const char *filename
= opts
->device
;
878 if (filename
== NULL
) {
879 fprintf(stderr
, "chardev: pipe: no filename given\n");
883 snprintf(filename_in
, 256, "%s.in", filename
);
884 snprintf(filename_out
, 256, "%s.out", filename
);
885 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
886 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
887 if (fd_in
< 0 || fd_out
< 0) {
892 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
897 return qemu_chr_open_fd(fd_in
, fd_out
);
900 /* init terminal so that we can grab keys */
901 static struct termios oldtty
;
902 static int old_fd0_flags
;
903 static bool stdio_allow_signal
;
905 static void term_exit(void)
907 tcsetattr (0, TCSANOW
, &oldtty
);
908 fcntl(0, F_SETFL
, old_fd0_flags
);
911 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
917 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
918 |INLCR
|IGNCR
|ICRNL
|IXON
);
919 tty
.c_oflag
|= OPOST
;
920 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
921 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
926 if (!stdio_allow_signal
)
927 tty
.c_lflag
&= ~ISIG
;
929 tcsetattr (0, TCSANOW
, &tty
);
932 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
938 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
940 CharDriverState
*chr
;
942 if (is_daemonized()) {
943 error_report("cannot use stdio with -daemonize");
946 old_fd0_flags
= fcntl(0, F_GETFL
);
947 tcgetattr (0, &oldtty
);
948 fcntl(0, F_SETFL
, O_NONBLOCK
);
951 chr
= qemu_chr_open_fd(0, 1);
952 chr
->chr_close
= qemu_chr_close_stdio
;
953 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
954 if (opts
->has_signal
) {
955 stdio_allow_signal
= opts
->signal
;
957 qemu_chr_fe_set_echo(chr
, false);
962 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
963 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
964 || defined(__GLIBC__)
966 #define HAVE_CHARDEV_TTY 1
976 static void pty_chr_update_read_handler(CharDriverState
*chr
);
977 static void pty_chr_state(CharDriverState
*chr
, int connected
);
979 static gboolean
pty_chr_timer(gpointer opaque
)
981 struct CharDriverState
*chr
= opaque
;
982 PtyCharDriver
*s
= chr
->opaque
;
989 pty_chr_update_read_handler(chr
);
996 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
998 PtyCharDriver
*s
= chr
->opaque
;
1001 g_source_remove(s
->timer_tag
);
1006 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1008 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1012 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1014 PtyCharDriver
*s
= chr
->opaque
;
1016 if (!s
->connected
) {
1017 /* guest sends data, check for (re-)connect */
1018 pty_chr_update_read_handler(chr
);
1021 return io_channel_send(s
->fd
, buf
, len
);
1024 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1026 PtyCharDriver
*s
= chr
->opaque
;
1027 return g_io_create_watch(s
->fd
, cond
);
1030 static int pty_chr_read_poll(void *opaque
)
1032 CharDriverState
*chr
= opaque
;
1033 PtyCharDriver
*s
= chr
->opaque
;
1035 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1036 return s
->read_bytes
;
1039 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1041 CharDriverState
*chr
= opaque
;
1042 PtyCharDriver
*s
= chr
->opaque
;
1044 uint8_t buf
[READ_BUF_LEN
];
1048 if (len
> s
->read_bytes
)
1049 len
= s
->read_bytes
;
1053 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1054 if (status
!= G_IO_STATUS_NORMAL
) {
1055 pty_chr_state(chr
, 0);
1058 pty_chr_state(chr
, 1);
1059 qemu_chr_be_write(chr
, buf
, size
);
1064 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1066 PtyCharDriver
*s
= chr
->opaque
;
1069 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1070 pfd
.events
= G_IO_OUT
;
1073 if (pfd
.revents
& G_IO_HUP
) {
1074 pty_chr_state(chr
, 0);
1076 pty_chr_state(chr
, 1);
1080 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1082 PtyCharDriver
*s
= chr
->opaque
;
1086 io_remove_watch_poll(s
->fd_tag
);
1090 /* (re-)connect poll interval for idle guests: once per second.
1091 * We check more frequently in case the guests sends data to
1092 * the virtual device linked to our pty. */
1093 pty_chr_rearm_timer(chr
, 1000);
1096 g_source_remove(s
->timer_tag
);
1099 if (!s
->connected
) {
1100 qemu_chr_be_generic_open(chr
);
1102 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1108 static void pty_chr_close(struct CharDriverState
*chr
)
1110 PtyCharDriver
*s
= chr
->opaque
;
1114 io_remove_watch_poll(s
->fd_tag
);
1117 fd
= g_io_channel_unix_get_fd(s
->fd
);
1118 g_io_channel_unref(s
->fd
);
1121 g_source_remove(s
->timer_tag
);
1125 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1128 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1131 CharDriverState
*chr
;
1133 int master_fd
, slave_fd
;
1134 char pty_name
[PATH_MAX
];
1136 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1137 if (master_fd
< 0) {
1143 chr
= g_malloc0(sizeof(CharDriverState
));
1145 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1146 ret
->pty
= g_strdup(pty_name
);
1147 ret
->has_pty
= true;
1149 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1152 s
= g_malloc0(sizeof(PtyCharDriver
));
1154 chr
->chr_write
= pty_chr_write
;
1155 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1156 chr
->chr_close
= pty_chr_close
;
1157 chr
->chr_add_watch
= pty_chr_add_watch
;
1158 chr
->explicit_be_open
= true;
1160 s
->fd
= io_channel_from_fd(master_fd
);
1166 static void tty_serial_init(int fd
, int speed
,
1167 int parity
, int data_bits
, int stop_bits
)
1173 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1174 speed
, parity
, data_bits
, stop_bits
);
1176 tcgetattr (fd
, &tty
);
1178 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1179 speed
= speed
* 10 / 11;
1196 /* Non-Posix values follow. They may be unsupported on some systems. */
1198 check_speed(115200);
1200 check_speed(230400);
1203 check_speed(460800);
1206 check_speed(500000);
1209 check_speed(576000);
1212 check_speed(921600);
1215 check_speed(1000000);
1218 check_speed(1152000);
1221 check_speed(1500000);
1224 check_speed(2000000);
1227 check_speed(2500000);
1230 check_speed(3000000);
1233 check_speed(3500000);
1236 check_speed(4000000);
1241 cfsetispeed(&tty
, spd
);
1242 cfsetospeed(&tty
, spd
);
1244 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1245 |INLCR
|IGNCR
|ICRNL
|IXON
);
1246 tty
.c_oflag
|= OPOST
;
1247 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1248 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1269 tty
.c_cflag
|= PARENB
;
1272 tty
.c_cflag
|= PARENB
| PARODD
;
1276 tty
.c_cflag
|= CSTOPB
;
1278 tcsetattr (fd
, TCSANOW
, &tty
);
1281 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1283 FDCharDriver
*s
= chr
->opaque
;
1286 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1288 QEMUSerialSetParams
*ssp
= arg
;
1289 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1290 ssp
->speed
, ssp
->parity
,
1291 ssp
->data_bits
, ssp
->stop_bits
);
1294 case CHR_IOCTL_SERIAL_SET_BREAK
:
1296 int enable
= *(int *)arg
;
1298 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1302 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1305 int *targ
= (int *)arg
;
1306 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1308 if (sarg
& TIOCM_CTS
)
1309 *targ
|= CHR_TIOCM_CTS
;
1310 if (sarg
& TIOCM_CAR
)
1311 *targ
|= CHR_TIOCM_CAR
;
1312 if (sarg
& TIOCM_DSR
)
1313 *targ
|= CHR_TIOCM_DSR
;
1314 if (sarg
& TIOCM_RI
)
1315 *targ
|= CHR_TIOCM_RI
;
1316 if (sarg
& TIOCM_DTR
)
1317 *targ
|= CHR_TIOCM_DTR
;
1318 if (sarg
& TIOCM_RTS
)
1319 *targ
|= CHR_TIOCM_RTS
;
1322 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1324 int sarg
= *(int *)arg
;
1326 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1327 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1328 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1329 if (sarg
& CHR_TIOCM_CTS
)
1331 if (sarg
& CHR_TIOCM_CAR
)
1333 if (sarg
& CHR_TIOCM_DSR
)
1335 if (sarg
& CHR_TIOCM_RI
)
1337 if (sarg
& CHR_TIOCM_DTR
)
1339 if (sarg
& CHR_TIOCM_RTS
)
1341 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1350 static void qemu_chr_close_tty(CharDriverState
*chr
)
1352 FDCharDriver
*s
= chr
->opaque
;
1356 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1366 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1368 CharDriverState
*chr
;
1370 tty_serial_init(fd
, 115200, 'N', 8, 1);
1371 chr
= qemu_chr_open_fd(fd
, fd
);
1372 chr
->chr_ioctl
= tty_serial_ioctl
;
1373 chr
->chr_close
= qemu_chr_close_tty
;
1376 #endif /* __linux__ || __sun__ */
1378 #if defined(__linux__)
1380 #define HAVE_CHARDEV_PARPORT 1
1385 } ParallelCharDriver
;
1387 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1389 if (s
->mode
!= mode
) {
1391 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1398 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1400 ParallelCharDriver
*drv
= chr
->opaque
;
1405 case CHR_IOCTL_PP_READ_DATA
:
1406 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1408 *(uint8_t *)arg
= b
;
1410 case CHR_IOCTL_PP_WRITE_DATA
:
1411 b
= *(uint8_t *)arg
;
1412 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1415 case CHR_IOCTL_PP_READ_CONTROL
:
1416 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1418 /* Linux gives only the lowest bits, and no way to know data
1419 direction! For better compatibility set the fixed upper
1421 *(uint8_t *)arg
= b
| 0xc0;
1423 case CHR_IOCTL_PP_WRITE_CONTROL
:
1424 b
= *(uint8_t *)arg
;
1425 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1428 case CHR_IOCTL_PP_READ_STATUS
:
1429 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1431 *(uint8_t *)arg
= b
;
1433 case CHR_IOCTL_PP_DATA_DIR
:
1434 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1437 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1438 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1439 struct ParallelIOArg
*parg
= arg
;
1440 int n
= read(fd
, parg
->buffer
, parg
->count
);
1441 if (n
!= parg
->count
) {
1446 case CHR_IOCTL_PP_EPP_READ
:
1447 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1448 struct ParallelIOArg
*parg
= arg
;
1449 int n
= read(fd
, parg
->buffer
, parg
->count
);
1450 if (n
!= parg
->count
) {
1455 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1456 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1457 struct ParallelIOArg
*parg
= arg
;
1458 int n
= write(fd
, parg
->buffer
, parg
->count
);
1459 if (n
!= parg
->count
) {
1464 case CHR_IOCTL_PP_EPP_WRITE
:
1465 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1466 struct ParallelIOArg
*parg
= arg
;
1467 int n
= write(fd
, parg
->buffer
, parg
->count
);
1468 if (n
!= parg
->count
) {
1479 static void pp_close(CharDriverState
*chr
)
1481 ParallelCharDriver
*drv
= chr
->opaque
;
1484 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1485 ioctl(fd
, PPRELEASE
);
1488 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1491 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1493 CharDriverState
*chr
;
1494 ParallelCharDriver
*drv
;
1496 if (ioctl(fd
, PPCLAIM
) < 0) {
1501 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1503 drv
->mode
= IEEE1284_MODE_COMPAT
;
1505 chr
= g_malloc0(sizeof(CharDriverState
));
1506 chr
->chr_write
= null_chr_write
;
1507 chr
->chr_ioctl
= pp_ioctl
;
1508 chr
->chr_close
= pp_close
;
1513 #endif /* __linux__ */
1515 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1517 #define HAVE_CHARDEV_PARPORT 1
1519 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1521 int fd
= (int)(intptr_t)chr
->opaque
;
1525 case CHR_IOCTL_PP_READ_DATA
:
1526 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1528 *(uint8_t *)arg
= b
;
1530 case CHR_IOCTL_PP_WRITE_DATA
:
1531 b
= *(uint8_t *)arg
;
1532 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1535 case CHR_IOCTL_PP_READ_CONTROL
:
1536 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1538 *(uint8_t *)arg
= b
;
1540 case CHR_IOCTL_PP_WRITE_CONTROL
:
1541 b
= *(uint8_t *)arg
;
1542 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1545 case CHR_IOCTL_PP_READ_STATUS
:
1546 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1548 *(uint8_t *)arg
= b
;
1556 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1558 CharDriverState
*chr
;
1560 chr
= g_malloc0(sizeof(CharDriverState
));
1561 chr
->opaque
= (void *)(intptr_t)fd
;
1562 chr
->chr_write
= null_chr_write
;
1563 chr
->chr_ioctl
= pp_ioctl
;
1564 chr
->explicit_be_open
= true;
1573 HANDLE hcom
, hrecv
, hsend
;
1574 OVERLAPPED orecv
, osend
;
1581 HANDLE hInputReadyEvent
;
1582 HANDLE hInputDoneEvent
;
1583 HANDLE hInputThread
;
1584 uint8_t win_stdio_buf
;
1585 } WinStdioCharState
;
1587 #define NSENDBUF 2048
1588 #define NRECVBUF 2048
1589 #define MAXCONNECT 1
1590 #define NTIMEOUT 5000
1592 static int win_chr_poll(void *opaque
);
1593 static int win_chr_pipe_poll(void *opaque
);
1595 static void win_chr_close(CharDriverState
*chr
)
1597 WinCharState
*s
= chr
->opaque
;
1600 CloseHandle(s
->hsend
);
1604 CloseHandle(s
->hrecv
);
1608 CloseHandle(s
->hcom
);
1612 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1614 qemu_del_polling_cb(win_chr_poll
, chr
);
1616 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1619 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1621 WinCharState
*s
= chr
->opaque
;
1623 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1628 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1630 fprintf(stderr
, "Failed CreateEvent\n");
1633 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1635 fprintf(stderr
, "Failed CreateEvent\n");
1639 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1640 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1641 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1642 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1647 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1648 fprintf(stderr
, "Failed SetupComm\n");
1652 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1653 size
= sizeof(COMMCONFIG
);
1654 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1655 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1656 CommConfigDialog(filename
, NULL
, &comcfg
);
1658 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1659 fprintf(stderr
, "Failed SetCommState\n");
1663 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1664 fprintf(stderr
, "Failed SetCommMask\n");
1668 cto
.ReadIntervalTimeout
= MAXDWORD
;
1669 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1670 fprintf(stderr
, "Failed SetCommTimeouts\n");
1674 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1675 fprintf(stderr
, "Failed ClearCommError\n");
1678 qemu_add_polling_cb(win_chr_poll
, chr
);
1686 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1688 WinCharState
*s
= chr
->opaque
;
1689 DWORD len
, ret
, size
, err
;
1692 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1693 s
->osend
.hEvent
= s
->hsend
;
1696 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1698 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1700 err
= GetLastError();
1701 if (err
== ERROR_IO_PENDING
) {
1702 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1720 static int win_chr_read_poll(CharDriverState
*chr
)
1722 WinCharState
*s
= chr
->opaque
;
1724 s
->max_size
= qemu_chr_be_can_write(chr
);
1728 static void win_chr_readfile(CharDriverState
*chr
)
1730 WinCharState
*s
= chr
->opaque
;
1732 uint8_t buf
[READ_BUF_LEN
];
1735 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1736 s
->orecv
.hEvent
= s
->hrecv
;
1737 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1739 err
= GetLastError();
1740 if (err
== ERROR_IO_PENDING
) {
1741 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1746 qemu_chr_be_write(chr
, buf
, size
);
1750 static void win_chr_read(CharDriverState
*chr
)
1752 WinCharState
*s
= chr
->opaque
;
1754 if (s
->len
> s
->max_size
)
1755 s
->len
= s
->max_size
;
1759 win_chr_readfile(chr
);
1762 static int win_chr_poll(void *opaque
)
1764 CharDriverState
*chr
= opaque
;
1765 WinCharState
*s
= chr
->opaque
;
1769 ClearCommError(s
->hcom
, &comerr
, &status
);
1770 if (status
.cbInQue
> 0) {
1771 s
->len
= status
.cbInQue
;
1772 win_chr_read_poll(chr
);
1779 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1781 CharDriverState
*chr
;
1784 chr
= g_malloc0(sizeof(CharDriverState
));
1785 s
= g_malloc0(sizeof(WinCharState
));
1787 chr
->chr_write
= win_chr_write
;
1788 chr
->chr_close
= win_chr_close
;
1790 if (win_chr_init(chr
, filename
) < 0) {
1798 static int win_chr_pipe_poll(void *opaque
)
1800 CharDriverState
*chr
= opaque
;
1801 WinCharState
*s
= chr
->opaque
;
1804 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1807 win_chr_read_poll(chr
);
1814 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1816 WinCharState
*s
= chr
->opaque
;
1824 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1826 fprintf(stderr
, "Failed CreateEvent\n");
1829 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1831 fprintf(stderr
, "Failed CreateEvent\n");
1835 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1836 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1837 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1839 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1840 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1841 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1846 ZeroMemory(&ov
, sizeof(ov
));
1847 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1848 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1850 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1854 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1856 fprintf(stderr
, "Failed GetOverlappedResult\n");
1858 CloseHandle(ov
.hEvent
);
1865 CloseHandle(ov
.hEvent
);
1868 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1877 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1879 const char *filename
= opts
->device
;
1880 CharDriverState
*chr
;
1883 chr
= g_malloc0(sizeof(CharDriverState
));
1884 s
= g_malloc0(sizeof(WinCharState
));
1886 chr
->chr_write
= win_chr_write
;
1887 chr
->chr_close
= win_chr_close
;
1889 if (win_chr_pipe_init(chr
, filename
) < 0) {
1897 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1899 CharDriverState
*chr
;
1902 chr
= g_malloc0(sizeof(CharDriverState
));
1903 s
= g_malloc0(sizeof(WinCharState
));
1906 chr
->chr_write
= win_chr_write
;
1910 static CharDriverState
*qemu_chr_open_win_con(void)
1912 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1915 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1917 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1924 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1934 static void win_stdio_wait_func(void *opaque
)
1936 CharDriverState
*chr
= opaque
;
1937 WinStdioCharState
*stdio
= chr
->opaque
;
1938 INPUT_RECORD buf
[4];
1943 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1947 /* Avoid error storm */
1948 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1952 for (i
= 0; i
< dwSize
; i
++) {
1953 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1955 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1957 if (kev
->uChar
.AsciiChar
!= 0) {
1958 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1959 if (qemu_chr_be_can_write(chr
)) {
1960 uint8_t c
= kev
->uChar
.AsciiChar
;
1961 qemu_chr_be_write(chr
, &c
, 1);
1969 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
1971 CharDriverState
*chr
= param
;
1972 WinStdioCharState
*stdio
= chr
->opaque
;
1978 /* Wait for one byte */
1979 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
1981 /* Exit in case of error, continue if nothing read */
1989 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1990 if (stdio
->win_stdio_buf
== '\r') {
1994 /* Signal the main thread and wait until the byte was eaten */
1995 if (!SetEvent(stdio
->hInputReadyEvent
)) {
1998 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2004 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2008 static void win_stdio_thread_wait_func(void *opaque
)
2010 CharDriverState
*chr
= opaque
;
2011 WinStdioCharState
*stdio
= chr
->opaque
;
2013 if (qemu_chr_be_can_write(chr
)) {
2014 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2017 SetEvent(stdio
->hInputDoneEvent
);
2020 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2022 WinStdioCharState
*stdio
= chr
->opaque
;
2025 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2028 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2030 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2034 static void win_stdio_close(CharDriverState
*chr
)
2036 WinStdioCharState
*stdio
= chr
->opaque
;
2038 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2039 CloseHandle(stdio
->hInputReadyEvent
);
2041 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2042 CloseHandle(stdio
->hInputDoneEvent
);
2044 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2045 TerminateThread(stdio
->hInputThread
, 0);
2048 g_free(chr
->opaque
);
2052 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2054 CharDriverState
*chr
;
2055 WinStdioCharState
*stdio
;
2059 chr
= g_malloc0(sizeof(CharDriverState
));
2060 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2062 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2063 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2064 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2068 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2070 chr
->opaque
= stdio
;
2071 chr
->chr_write
= win_stdio_write
;
2072 chr
->chr_close
= win_stdio_close
;
2075 if (qemu_add_wait_object(stdio
->hStdIn
,
2076 win_stdio_wait_func
, chr
)) {
2077 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2082 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2083 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2084 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2087 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2088 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2089 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2090 fprintf(stderr
, "cannot create stdio thread or event\n");
2093 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2094 win_stdio_thread_wait_func
, chr
)) {
2095 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2099 dwMode
|= ENABLE_LINE_INPUT
;
2102 /* set the terminal in raw mode */
2103 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2104 dwMode
|= ENABLE_PROCESSED_INPUT
;
2107 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2109 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2110 qemu_chr_fe_set_echo(chr
, false);
2114 #endif /* !_WIN32 */
2117 /***********************************************************/
2118 /* UDP Net console */
2124 uint8_t buf
[READ_BUF_LEN
];
2130 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2132 NetCharDriver
*s
= chr
->opaque
;
2133 gsize bytes_written
;
2136 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2137 if (status
== G_IO_STATUS_EOF
) {
2139 } else if (status
!= G_IO_STATUS_NORMAL
) {
2143 return bytes_written
;
2146 static int udp_chr_read_poll(void *opaque
)
2148 CharDriverState
*chr
= opaque
;
2149 NetCharDriver
*s
= chr
->opaque
;
2151 s
->max_size
= qemu_chr_be_can_write(chr
);
2153 /* If there were any stray characters in the queue process them
2156 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2157 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2159 s
->max_size
= qemu_chr_be_can_write(chr
);
2164 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2166 CharDriverState
*chr
= opaque
;
2167 NetCharDriver
*s
= chr
->opaque
;
2168 gsize bytes_read
= 0;
2171 if (s
->max_size
== 0) {
2174 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2176 s
->bufcnt
= bytes_read
;
2177 s
->bufptr
= s
->bufcnt
;
2178 if (status
!= G_IO_STATUS_NORMAL
) {
2180 io_remove_watch_poll(s
->tag
);
2187 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2188 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2190 s
->max_size
= qemu_chr_be_can_write(chr
);
2196 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2198 NetCharDriver
*s
= chr
->opaque
;
2201 io_remove_watch_poll(s
->tag
);
2206 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2210 static void udp_chr_close(CharDriverState
*chr
)
2212 NetCharDriver
*s
= chr
->opaque
;
2214 io_remove_watch_poll(s
->tag
);
2218 g_io_channel_unref(s
->chan
);
2222 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2225 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2227 CharDriverState
*chr
= NULL
;
2228 NetCharDriver
*s
= NULL
;
2230 chr
= g_malloc0(sizeof(CharDriverState
));
2231 s
= g_malloc0(sizeof(NetCharDriver
));
2234 s
->chan
= io_channel_from_socket(s
->fd
);
2238 chr
->chr_write
= udp_chr_write
;
2239 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2240 chr
->chr_close
= udp_chr_close
;
2241 /* be isn't opened until we get a connection */
2242 chr
->explicit_be_open
= true;
2246 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2248 Error
*local_err
= NULL
;
2251 fd
= inet_dgram_opts(opts
, &local_err
);
2253 qerror_report_err(local_err
);
2254 error_free(local_err
);
2257 return qemu_chr_open_udp_fd(fd
);
2260 /***********************************************************/
2261 /* TCP Net console */
2265 GIOChannel
*chan
, *listen_chan
;
2266 guint tag
, listen_tag
;
2276 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2278 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2280 TCPCharDriver
*s
= chr
->opaque
;
2282 return io_channel_send(s
->chan
, buf
, len
);
2284 /* XXX: indicate an error ? */
2289 static int tcp_chr_read_poll(void *opaque
)
2291 CharDriverState
*chr
= opaque
;
2292 TCPCharDriver
*s
= chr
->opaque
;
2295 s
->max_size
= qemu_chr_be_can_write(chr
);
2300 #define IAC_BREAK 243
2301 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2303 uint8_t *buf
, int *size
)
2305 /* Handle any telnet client's basic IAC options to satisfy char by
2306 * char mode with no echo. All IAC options will be removed from
2307 * the buf and the do_telnetopt variable will be used to track the
2308 * state of the width of the IAC information.
2310 * IAC commands come in sets of 3 bytes with the exception of the
2311 * "IAC BREAK" command and the double IAC.
2317 for (i
= 0; i
< *size
; i
++) {
2318 if (s
->do_telnetopt
> 1) {
2319 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2320 /* Double IAC means send an IAC */
2324 s
->do_telnetopt
= 1;
2326 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2327 /* Handle IAC break commands by sending a serial break */
2328 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2333 if (s
->do_telnetopt
>= 4) {
2334 s
->do_telnetopt
= 1;
2337 if ((unsigned char)buf
[i
] == IAC
) {
2338 s
->do_telnetopt
= 2;
2349 static int tcp_get_msgfd(CharDriverState
*chr
)
2351 TCPCharDriver
*s
= chr
->opaque
;
2358 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2360 TCPCharDriver
*s
= chr
->opaque
;
2361 struct cmsghdr
*cmsg
;
2363 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2366 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2367 cmsg
->cmsg_level
!= SOL_SOCKET
||
2368 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2371 fd
= *((int *)CMSG_DATA(cmsg
));
2375 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2378 #ifndef MSG_CMSG_CLOEXEC
2379 qemu_set_cloexec(fd
);
2387 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2389 TCPCharDriver
*s
= chr
->opaque
;
2390 struct msghdr msg
= { NULL
, };
2391 struct iovec iov
[1];
2393 struct cmsghdr cmsg
;
2394 char control
[CMSG_SPACE(sizeof(int))];
2399 iov
[0].iov_base
= buf
;
2400 iov
[0].iov_len
= len
;
2404 msg
.msg_control
= &msg_control
;
2405 msg
.msg_controllen
= sizeof(msg_control
);
2407 #ifdef MSG_CMSG_CLOEXEC
2408 flags
|= MSG_CMSG_CLOEXEC
;
2410 ret
= recvmsg(s
->fd
, &msg
, flags
);
2411 if (ret
> 0 && s
->is_unix
) {
2412 unix_process_msgfd(chr
, &msg
);
2418 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2420 TCPCharDriver
*s
= chr
->opaque
;
2421 return qemu_recv(s
->fd
, buf
, len
, 0);
2425 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2427 TCPCharDriver
*s
= chr
->opaque
;
2428 return g_io_create_watch(s
->chan
, cond
);
2431 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2433 CharDriverState
*chr
= opaque
;
2434 TCPCharDriver
*s
= chr
->opaque
;
2435 uint8_t buf
[READ_BUF_LEN
];
2438 if (!s
->connected
|| s
->max_size
<= 0) {
2442 if (len
> s
->max_size
)
2444 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2446 /* connection closed */
2448 if (s
->listen_chan
) {
2449 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2452 io_remove_watch_poll(s
->tag
);
2455 g_io_channel_unref(s
->chan
);
2459 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2460 } else if (size
> 0) {
2461 if (s
->do_telnetopt
)
2462 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2464 qemu_chr_be_write(chr
, buf
, size
);
2471 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2473 return qemu_chr_open_fd(eventfd
, eventfd
);
2477 static void tcp_chr_connect(void *opaque
)
2479 CharDriverState
*chr
= opaque
;
2480 TCPCharDriver
*s
= chr
->opaque
;
2484 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2486 qemu_chr_be_generic_open(chr
);
2489 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2490 static void tcp_chr_telnet_init(int fd
)
2493 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2494 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2495 send(fd
, (char *)buf
, 3, 0);
2496 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2497 send(fd
, (char *)buf
, 3, 0);
2498 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2499 send(fd
, (char *)buf
, 3, 0);
2500 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2501 send(fd
, (char *)buf
, 3, 0);
2504 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2506 TCPCharDriver
*s
= chr
->opaque
;
2510 qemu_set_nonblock(fd
);
2512 socket_set_nodelay(fd
);
2514 s
->chan
= io_channel_from_socket(fd
);
2515 if (s
->listen_tag
) {
2516 g_source_remove(s
->listen_tag
);
2519 tcp_chr_connect(chr
);
2524 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2526 CharDriverState
*chr
= opaque
;
2527 TCPCharDriver
*s
= chr
->opaque
;
2528 struct sockaddr_in saddr
;
2530 struct sockaddr_un uaddr
;
2532 struct sockaddr
*addr
;
2539 len
= sizeof(uaddr
);
2540 addr
= (struct sockaddr
*)&uaddr
;
2544 len
= sizeof(saddr
);
2545 addr
= (struct sockaddr
*)&saddr
;
2547 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2548 if (fd
< 0 && errno
!= EINTR
) {
2551 } else if (fd
>= 0) {
2552 if (s
->do_telnetopt
)
2553 tcp_chr_telnet_init(fd
);
2557 if (tcp_chr_add_client(chr
, fd
) < 0)
2563 static void tcp_chr_close(CharDriverState
*chr
)
2565 TCPCharDriver
*s
= chr
->opaque
;
2568 io_remove_watch_poll(s
->tag
);
2572 g_io_channel_unref(s
->chan
);
2576 if (s
->listen_fd
>= 0) {
2577 if (s
->listen_tag
) {
2578 g_source_remove(s
->listen_tag
);
2581 if (s
->listen_chan
) {
2582 g_io_channel_unref(s
->listen_chan
);
2584 closesocket(s
->listen_fd
);
2587 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2590 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2591 bool is_listen
, bool is_telnet
,
2592 bool is_waitconnect
,
2595 CharDriverState
*chr
= NULL
;
2596 TCPCharDriver
*s
= NULL
;
2597 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2598 const char *left
= "", *right
= "";
2599 struct sockaddr_storage ss
;
2600 socklen_t ss_len
= sizeof(ss
);
2602 memset(&ss
, 0, ss_len
);
2603 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2604 error_setg_errno(errp
, errno
, "getsockname");
2608 chr
= g_malloc0(sizeof(CharDriverState
));
2609 s
= g_malloc0(sizeof(TCPCharDriver
));
2616 chr
->filename
= g_malloc(256);
2617 switch (ss
.ss_family
) {
2621 snprintf(chr
->filename
, 256, "unix:%s%s",
2622 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2623 is_listen
? ",server" : "");
2631 s
->do_nodelay
= do_nodelay
;
2632 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2633 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2634 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2635 is_telnet
? "telnet" : "tcp",
2636 left
, host
, right
, serv
,
2637 is_listen
? ",server" : "");
2642 chr
->chr_write
= tcp_chr_write
;
2643 chr
->chr_close
= tcp_chr_close
;
2644 chr
->get_msgfd
= tcp_get_msgfd
;
2645 chr
->chr_add_client
= tcp_chr_add_client
;
2646 chr
->chr_add_watch
= tcp_chr_add_watch
;
2647 /* be isn't opened until we get a connection */
2648 chr
->explicit_be_open
= true;
2652 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2653 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2655 s
->do_telnetopt
= 1;
2660 socket_set_nodelay(fd
);
2661 s
->chan
= io_channel_from_socket(s
->fd
);
2662 tcp_chr_connect(chr
);
2665 if (is_listen
&& is_waitconnect
) {
2666 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
2668 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2669 qemu_set_nonblock(s
->listen_fd
);
2674 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2676 CharDriverState
*chr
= NULL
;
2677 Error
*local_err
= NULL
;
2680 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
2681 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
2682 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
2683 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
2684 bool is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2688 fd
= unix_listen_opts(opts
, &local_err
);
2690 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2694 fd
= inet_listen_opts(opts
, 0, &local_err
);
2696 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2703 if (!is_waitconnect
)
2704 qemu_set_nonblock(fd
);
2706 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2707 is_waitconnect
, &local_err
);
2708 if (error_is_set(&local_err
)) {
2716 qerror_report_err(local_err
);
2717 error_free(local_err
);
2723 g_free(chr
->opaque
);
2729 /*********************************************************/
2730 /* Ring buffer chardev */
2737 } RingBufCharDriver
;
2739 static size_t ringbuf_count(const CharDriverState
*chr
)
2741 const RingBufCharDriver
*d
= chr
->opaque
;
2743 return d
->prod
- d
->cons
;
2746 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2748 RingBufCharDriver
*d
= chr
->opaque
;
2751 if (!buf
|| (len
< 0)) {
2755 for (i
= 0; i
< len
; i
++ ) {
2756 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2757 if (d
->prod
- d
->cons
> d
->size
) {
2758 d
->cons
= d
->prod
- d
->size
;
2765 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2767 RingBufCharDriver
*d
= chr
->opaque
;
2770 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2771 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2777 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2779 RingBufCharDriver
*d
= chr
->opaque
;
2786 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2789 CharDriverState
*chr
;
2790 RingBufCharDriver
*d
;
2792 chr
= g_malloc0(sizeof(CharDriverState
));
2793 d
= g_malloc(sizeof(*d
));
2795 d
->size
= opts
->has_size
? opts
->size
: 65536;
2797 /* The size must be power of 2 */
2798 if (d
->size
& (d
->size
- 1)) {
2799 error_setg(errp
, "size of ringbuf chardev must be power of two");
2805 d
->cbuf
= g_malloc0(d
->size
);
2808 chr
->chr_write
= ringbuf_chr_write
;
2809 chr
->chr_close
= ringbuf_chr_close
;
2819 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2821 return chr
->chr_write
== ringbuf_chr_write
;
2824 void qmp_ringbuf_write(const char *device
, const char *data
,
2825 bool has_format
, enum DataFormat format
,
2828 CharDriverState
*chr
;
2829 const uint8_t *write_data
;
2833 chr
= qemu_chr_find(device
);
2835 error_setg(errp
, "Device '%s' not found", device
);
2839 if (!chr_is_ringbuf(chr
)) {
2840 error_setg(errp
,"%s is not a ringbuf device", device
);
2844 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2845 write_data
= g_base64_decode(data
, &write_count
);
2847 write_data
= (uint8_t *)data
;
2848 write_count
= strlen(data
);
2851 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2853 if (write_data
!= (uint8_t *)data
) {
2854 g_free((void *)write_data
);
2858 error_setg(errp
, "Failed to write to device %s", device
);
2863 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2864 bool has_format
, enum DataFormat format
,
2867 CharDriverState
*chr
;
2872 chr
= qemu_chr_find(device
);
2874 error_setg(errp
, "Device '%s' not found", device
);
2878 if (!chr_is_ringbuf(chr
)) {
2879 error_setg(errp
,"%s is not a ringbuf device", device
);
2884 error_setg(errp
, "size must be greater than zero");
2888 count
= ringbuf_count(chr
);
2889 size
= size
> count
? count
: size
;
2890 read_data
= g_malloc(size
+ 1);
2892 ringbuf_chr_read(chr
, read_data
, size
);
2894 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2895 data
= g_base64_encode(read_data
, size
);
2899 * FIXME should read only complete, valid UTF-8 characters up
2900 * to @size bytes. Invalid sequences should be replaced by a
2901 * suitable replacement character. Except when (and only
2902 * when) ring buffer lost characters since last read, initial
2903 * continuation characters should be dropped.
2905 read_data
[size
] = 0;
2906 data
= (char *)read_data
;
2912 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2914 char host
[65], port
[33], width
[8], height
[8];
2918 Error
*local_err
= NULL
;
2920 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2921 if (error_is_set(&local_err
)) {
2922 qerror_report_err(local_err
);
2923 error_free(local_err
);
2927 if (strstart(filename
, "mon:", &p
)) {
2929 qemu_opt_set(opts
, "mux", "on");
2930 if (strcmp(filename
, "stdio") == 0) {
2931 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
2932 * but pass it to the guest. Handle this only for compat syntax,
2933 * for -chardev syntax we have special option for this.
2934 * This is what -nographic did, redirecting+muxing serial+monitor
2935 * to stdio causing Ctrl+C to be passed to guest. */
2936 qemu_opt_set(opts
, "signal", "off");
2940 if (strcmp(filename
, "null") == 0 ||
2941 strcmp(filename
, "pty") == 0 ||
2942 strcmp(filename
, "msmouse") == 0 ||
2943 strcmp(filename
, "braille") == 0 ||
2944 strcmp(filename
, "stdio") == 0) {
2945 qemu_opt_set(opts
, "backend", filename
);
2948 if (strstart(filename
, "vc", &p
)) {
2949 qemu_opt_set(opts
, "backend", "vc");
2951 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2953 qemu_opt_set(opts
, "width", width
);
2954 qemu_opt_set(opts
, "height", height
);
2955 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2957 qemu_opt_set(opts
, "cols", width
);
2958 qemu_opt_set(opts
, "rows", height
);
2965 if (strcmp(filename
, "con:") == 0) {
2966 qemu_opt_set(opts
, "backend", "console");
2969 if (strstart(filename
, "COM", NULL
)) {
2970 qemu_opt_set(opts
, "backend", "serial");
2971 qemu_opt_set(opts
, "path", filename
);
2974 if (strstart(filename
, "file:", &p
)) {
2975 qemu_opt_set(opts
, "backend", "file");
2976 qemu_opt_set(opts
, "path", p
);
2979 if (strstart(filename
, "pipe:", &p
)) {
2980 qemu_opt_set(opts
, "backend", "pipe");
2981 qemu_opt_set(opts
, "path", p
);
2984 if (strstart(filename
, "tcp:", &p
) ||
2985 strstart(filename
, "telnet:", &p
)) {
2986 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2988 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2991 qemu_opt_set(opts
, "backend", "socket");
2992 qemu_opt_set(opts
, "host", host
);
2993 qemu_opt_set(opts
, "port", port
);
2994 if (p
[pos
] == ',') {
2995 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2998 if (strstart(filename
, "telnet:", &p
))
2999 qemu_opt_set(opts
, "telnet", "on");
3002 if (strstart(filename
, "udp:", &p
)) {
3003 qemu_opt_set(opts
, "backend", "udp");
3004 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3006 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3010 qemu_opt_set(opts
, "host", host
);
3011 qemu_opt_set(opts
, "port", port
);
3012 if (p
[pos
] == '@') {
3014 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3016 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3020 qemu_opt_set(opts
, "localaddr", host
);
3021 qemu_opt_set(opts
, "localport", port
);
3025 if (strstart(filename
, "unix:", &p
)) {
3026 qemu_opt_set(opts
, "backend", "socket");
3027 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3031 if (strstart(filename
, "/dev/parport", NULL
) ||
3032 strstart(filename
, "/dev/ppi", NULL
)) {
3033 qemu_opt_set(opts
, "backend", "parport");
3034 qemu_opt_set(opts
, "path", filename
);
3037 if (strstart(filename
, "/dev/", NULL
)) {
3038 qemu_opt_set(opts
, "backend", "tty");
3039 qemu_opt_set(opts
, "path", filename
);
3044 qemu_opts_del(opts
);
3048 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3051 const char *path
= qemu_opt_get(opts
, "path");
3054 error_setg(errp
, "chardev: file: no filename given");
3057 backend
->file
= g_new0(ChardevFile
, 1);
3058 backend
->file
->out
= g_strdup(path
);
3061 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3064 backend
->stdio
= g_new0(ChardevStdio
, 1);
3065 backend
->stdio
->has_signal
= true;
3066 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3069 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3072 const char *device
= qemu_opt_get(opts
, "path");
3074 if (device
== NULL
) {
3075 error_setg(errp
, "chardev: serial/tty: no device path given");
3078 backend
->serial
= g_new0(ChardevHostdev
, 1);
3079 backend
->serial
->device
= g_strdup(device
);
3082 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3085 const char *device
= qemu_opt_get(opts
, "path");
3087 if (device
== NULL
) {
3088 error_setg(errp
, "chardev: parallel: no device path given");
3091 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3092 backend
->parallel
->device
= g_strdup(device
);
3095 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3098 const char *device
= qemu_opt_get(opts
, "path");
3100 if (device
== NULL
) {
3101 error_setg(errp
, "chardev: pipe: no device path given");
3104 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3105 backend
->pipe
->device
= g_strdup(device
);
3108 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3113 backend
->memory
= g_new0(ChardevRingbuf
, 1);
3115 val
= qemu_opt_get_size(opts
, "size", 0);
3117 backend
->memory
->has_size
= true;
3118 backend
->memory
->size
= val
;
3122 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3125 const char *chardev
= qemu_opt_get(opts
, "chardev");
3127 if (chardev
== NULL
) {
3128 error_setg(errp
, "chardev: mux: no chardev given");
3131 backend
->mux
= g_new0(ChardevMux
, 1);
3132 backend
->mux
->chardev
= g_strdup(chardev
);
3135 typedef struct CharDriver
{
3138 CharDriverState
*(*open
)(QemuOpts
*opts
);
3139 /* new, qapi-based */
3140 ChardevBackendKind kind
;
3141 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3144 static GSList
*backends
;
3146 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3150 s
= g_malloc0(sizeof(*s
));
3151 s
->name
= g_strdup(name
);
3154 backends
= g_slist_append(backends
, s
);
3157 void register_char_driver_qapi(const char *name
, ChardevBackendKind kind
,
3158 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3162 s
= g_malloc0(sizeof(*s
));
3163 s
->name
= g_strdup(name
);
3167 backends
= g_slist_append(backends
, s
);
3170 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3171 void (*init
)(struct CharDriverState
*s
),
3175 CharDriverState
*chr
;
3178 if (qemu_opts_id(opts
) == NULL
) {
3179 error_setg(errp
, "chardev: no id specified");
3183 if (qemu_opt_get(opts
, "backend") == NULL
) {
3184 error_setg(errp
, "chardev: \"%s\" missing backend",
3185 qemu_opts_id(opts
));
3188 for (i
= backends
; i
; i
= i
->next
) {
3191 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3196 error_setg(errp
, "chardev: backend \"%s\" not found",
3197 qemu_opt_get(opts
, "backend"));
3202 /* using new, qapi init */
3203 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3204 ChardevReturn
*ret
= NULL
;
3205 const char *id
= qemu_opts_id(opts
);
3208 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3209 bid
= g_strdup_printf("%s-base", id
);
3213 backend
->kind
= cd
->kind
;
3215 cd
->parse(opts
, backend
, errp
);
3216 if (error_is_set(errp
)) {
3220 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3221 if (error_is_set(errp
)) {
3226 qapi_free_ChardevBackend(backend
);
3227 qapi_free_ChardevReturn(ret
);
3228 backend
= g_new0(ChardevBackend
, 1);
3229 backend
->mux
= g_new0(ChardevMux
, 1);
3230 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3231 backend
->mux
->chardev
= g_strdup(bid
);
3232 ret
= qmp_chardev_add(id
, backend
, errp
);
3233 assert(!error_is_set(errp
));
3236 chr
= qemu_chr_find(id
);
3240 qapi_free_ChardevBackend(backend
);
3241 qapi_free_ChardevReturn(ret
);
3246 chr
= cd
->open(opts
);
3248 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3249 qemu_opt_get(opts
, "backend"));
3254 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3256 /* if we didn't create the chardev via qmp_chardev_add, we
3257 * need to send the OPENED event here
3259 if (!chr
->explicit_be_open
) {
3260 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3262 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3264 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3265 CharDriverState
*base
= chr
;
3266 int len
= strlen(qemu_opts_id(opts
)) + 6;
3267 base
->label
= g_malloc(len
);
3268 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3269 chr
= qemu_chr_open_mux(base
);
3270 chr
->filename
= base
->filename
;
3271 chr
->avail_connections
= MAX_MUX
;
3272 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3274 chr
->avail_connections
= 1;
3276 chr
->label
= g_strdup(qemu_opts_id(opts
));
3281 qemu_opts_del(opts
);
3285 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3288 CharDriverState
*chr
;
3292 if (strstart(filename
, "chardev:", &p
)) {
3293 return qemu_chr_find(p
);
3296 opts
= qemu_chr_parse_compat(label
, filename
);
3300 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3301 if (error_is_set(&err
)) {
3302 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3305 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3306 qemu_chr_fe_claim_no_fail(chr
);
3307 monitor_init(chr
, MONITOR_USE_READLINE
);
3312 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3314 if (chr
->chr_set_echo
) {
3315 chr
->chr_set_echo(chr
, echo
);
3319 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3321 if (chr
->fe_open
== fe_open
) {
3324 chr
->fe_open
= fe_open
;
3325 if (chr
->chr_set_fe_open
) {
3326 chr
->chr_set_fe_open(chr
, fe_open
);
3330 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3331 GIOFunc func
, void *user_data
)
3336 if (s
->chr_add_watch
== NULL
) {
3340 src
= s
->chr_add_watch(s
, cond
);
3341 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3342 tag
= g_source_attach(src
, NULL
);
3343 g_source_unref(src
);
3348 int qemu_chr_fe_claim(CharDriverState
*s
)
3350 if (s
->avail_connections
< 1) {
3353 s
->avail_connections
--;
3357 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3359 if (qemu_chr_fe_claim(s
) != 0) {
3360 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3361 __func__
, s
->label
);
3366 void qemu_chr_fe_release(CharDriverState
*s
)
3368 s
->avail_connections
++;
3371 void qemu_chr_delete(CharDriverState
*chr
)
3373 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3374 if (chr
->chr_close
) {
3375 chr
->chr_close(chr
);
3377 g_free(chr
->filename
);
3380 qemu_opts_del(chr
->opts
);
3385 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3387 ChardevInfoList
*chr_list
= NULL
;
3388 CharDriverState
*chr
;
3390 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3391 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3392 info
->value
= g_malloc0(sizeof(*info
->value
));
3393 info
->value
->label
= g_strdup(chr
->label
);
3394 info
->value
->filename
= g_strdup(chr
->filename
);
3396 info
->next
= chr_list
;
3403 CharDriverState
*qemu_chr_find(const char *name
)
3405 CharDriverState
*chr
;
3407 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3408 if (strcmp(chr
->label
, name
) != 0)
3415 /* Get a character (serial) device interface. */
3416 CharDriverState
*qemu_char_get_next_serial(void)
3418 static int next_serial
;
3419 CharDriverState
*chr
;
3421 /* FIXME: This function needs to go away: use chardev properties! */
3423 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3424 chr
= serial_hds
[next_serial
++];
3425 qemu_chr_fe_claim_no_fail(chr
);
3431 QemuOptsList qemu_chardev_opts
= {
3433 .implied_opt_name
= "backend",
3434 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3438 .type
= QEMU_OPT_STRING
,
3441 .type
= QEMU_OPT_STRING
,
3444 .type
= QEMU_OPT_STRING
,
3447 .type
= QEMU_OPT_STRING
,
3449 .name
= "localaddr",
3450 .type
= QEMU_OPT_STRING
,
3452 .name
= "localport",
3453 .type
= QEMU_OPT_STRING
,
3456 .type
= QEMU_OPT_NUMBER
,
3459 .type
= QEMU_OPT_BOOL
,
3462 .type
= QEMU_OPT_BOOL
,
3465 .type
= QEMU_OPT_BOOL
,
3468 .type
= QEMU_OPT_BOOL
,
3471 .type
= QEMU_OPT_BOOL
,
3474 .type
= QEMU_OPT_BOOL
,
3477 .type
= QEMU_OPT_NUMBER
,
3480 .type
= QEMU_OPT_NUMBER
,
3483 .type
= QEMU_OPT_NUMBER
,
3486 .type
= QEMU_OPT_NUMBER
,
3489 .type
= QEMU_OPT_BOOL
,
3492 .type
= QEMU_OPT_BOOL
,
3495 .type
= QEMU_OPT_STRING
,
3498 .type
= QEMU_OPT_NUMBER
,
3501 .type
= QEMU_OPT_SIZE
,
3504 .type
= QEMU_OPT_STRING
,
3506 { /* end of list */ }
3512 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3517 error_setg(errp
, "input file not supported");
3521 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3522 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3523 if (out
== INVALID_HANDLE_VALUE
) {
3524 error_setg(errp
, "open %s failed", file
->out
);
3527 return qemu_chr_open_win_file(out
);
3530 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3533 return qemu_chr_open_win_path(serial
->device
);
3536 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3539 error_setg(errp
, "character device backend type 'parallel' not supported");
3545 static int qmp_chardev_open_file_source(char *src
, int flags
,
3550 TFR(fd
= qemu_open(src
, flags
, 0666));
3552 error_setg_file_open(errp
, errno
, src
);
3557 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3559 int flags
, in
= -1, out
= -1;
3561 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3562 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3563 if (error_is_set(errp
)) {
3569 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3570 if (error_is_set(errp
)) {
3576 return qemu_chr_open_fd(in
, out
);
3579 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3582 #ifdef HAVE_CHARDEV_TTY
3585 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3586 if (error_is_set(errp
)) {
3589 qemu_set_nonblock(fd
);
3590 return qemu_chr_open_tty_fd(fd
);
3592 error_setg(errp
, "character device backend type 'serial' not supported");
3597 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3600 #ifdef HAVE_CHARDEV_PARPORT
3603 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3604 if (error_is_set(errp
)) {
3607 return qemu_chr_open_pp_fd(fd
);
3609 error_setg(errp
, "character device backend type 'parallel' not supported");
3616 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3619 SocketAddress
*addr
= sock
->addr
;
3620 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3621 bool is_listen
= sock
->has_server
? sock
->server
: true;
3622 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3623 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3627 fd
= socket_listen(addr
, errp
);
3629 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3631 if (error_is_set(errp
)) {
3634 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3635 is_telnet
, is_waitconnect
, errp
);
3638 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
3643 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
3644 if (error_is_set(errp
)) {
3647 return qemu_chr_open_udp_fd(fd
);
3650 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3653 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3654 CharDriverState
*base
, *chr
= NULL
;
3656 chr
= qemu_chr_find(id
);
3658 error_setg(errp
, "Chardev '%s' already exists", id
);
3663 switch (backend
->kind
) {
3664 case CHARDEV_BACKEND_KIND_FILE
:
3665 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3667 case CHARDEV_BACKEND_KIND_SERIAL
:
3668 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3670 case CHARDEV_BACKEND_KIND_PARALLEL
:
3671 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3673 case CHARDEV_BACKEND_KIND_PIPE
:
3674 chr
= qemu_chr_open_pipe(backend
->pipe
);
3676 case CHARDEV_BACKEND_KIND_SOCKET
:
3677 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3679 case CHARDEV_BACKEND_KIND_UDP
:
3680 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
3682 #ifdef HAVE_CHARDEV_TTY
3683 case CHARDEV_BACKEND_KIND_PTY
:
3684 chr
= qemu_chr_open_pty(id
, ret
);
3687 case CHARDEV_BACKEND_KIND_NULL
:
3688 chr
= qemu_chr_open_null();
3690 case CHARDEV_BACKEND_KIND_MUX
:
3691 base
= qemu_chr_find(backend
->mux
->chardev
);
3693 error_setg(errp
, "mux: base chardev %s not found",
3694 backend
->mux
->chardev
);
3697 chr
= qemu_chr_open_mux(base
);
3699 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3700 chr
= qemu_chr_open_msmouse();
3702 #ifdef CONFIG_BRLAPI
3703 case CHARDEV_BACKEND_KIND_BRAILLE
:
3704 chr
= chr_baum_init();
3707 case CHARDEV_BACKEND_KIND_STDIO
:
3708 chr
= qemu_chr_open_stdio(backend
->stdio
);
3711 case CHARDEV_BACKEND_KIND_CONSOLE
:
3712 chr
= qemu_chr_open_win_con();
3716 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3717 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3719 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3720 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3723 case CHARDEV_BACKEND_KIND_VC
:
3724 chr
= vc_init(backend
->vc
);
3726 case CHARDEV_BACKEND_KIND_MEMORY
:
3727 chr
= qemu_chr_open_ringbuf(backend
->memory
, errp
);
3730 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3734 if (chr
== NULL
&& !error_is_set(errp
)) {
3735 error_setg(errp
, "Failed to create chardev");
3738 chr
->label
= g_strdup(id
);
3739 chr
->avail_connections
=
3740 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3741 if (!chr
->filename
) {
3742 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
3744 if (!chr
->explicit_be_open
) {
3745 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3747 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3755 void qmp_chardev_remove(const char *id
, Error
**errp
)
3757 CharDriverState
*chr
;
3759 chr
= qemu_chr_find(id
);
3761 error_setg(errp
, "Chardev '%s' not found", id
);
3764 if (chr
->chr_can_read
|| chr
->chr_read
||
3765 chr
->chr_event
|| chr
->handler_opaque
) {
3766 error_setg(errp
, "Chardev '%s' is busy", id
);
3769 qemu_chr_delete(chr
);
3772 static void register_types(void)
3774 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3775 register_char_driver("socket", qemu_chr_open_socket
);
3776 register_char_driver("udp", qemu_chr_open_udp
);
3777 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_MEMORY
,
3778 qemu_chr_parse_ringbuf
);
3779 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3780 qemu_chr_parse_file_out
);
3781 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3782 qemu_chr_parse_stdio
);
3783 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3784 qemu_chr_parse_serial
);
3785 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3786 qemu_chr_parse_serial
);
3787 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3788 qemu_chr_parse_parallel
);
3789 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3790 qemu_chr_parse_parallel
);
3791 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3792 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3793 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3794 qemu_chr_parse_pipe
);
3795 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX
,
3796 qemu_chr_parse_mux
);
3797 /* Bug-compatibility: */
3798 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3799 qemu_chr_parse_ringbuf
);
3802 type_init(register_types
);