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 "char/char.h"
32 #include "hw/msmouse.h"
33 #include "qmp-commands.h"
43 #include <sys/times.h>
47 #include <sys/ioctl.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
52 #include <arpa/inet.h>
55 #include <sys/select.h>
58 #if defined(__GLIBC__)
60 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
65 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
66 #include <dev/ppbus/ppi.h>
67 #include <dev/ppbus/ppbconf.h>
68 #elif defined(__DragonFly__)
69 #include <dev/misc/ppi/ppi.h>
70 #include <bus/ppbus/ppbconf.h>
76 #include <linux/ppdev.h>
77 #include <linux/parport.h>
81 #include <sys/ethernet.h>
82 #include <sys/sockio.h>
83 #include <netinet/arp.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_icmp.h> // must come after ip.h
88 #include <netinet/udp.h>
89 #include <netinet/tcp.h>
97 #include "qemu/sockets.h"
98 #include "ui/qemu-spice.h"
100 #define READ_BUF_LEN 4096
102 /***********************************************************/
103 /* character device */
105 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
106 QTAILQ_HEAD_INITIALIZER(chardevs
);
108 void qemu_chr_be_event(CharDriverState
*s
, int event
)
110 /* Keep track if the char device is open */
112 case CHR_EVENT_OPENED
:
115 case CHR_EVENT_CLOSED
:
122 s
->chr_event(s
->handler_opaque
, event
);
125 static void qemu_chr_fire_open_event(void *opaque
)
127 CharDriverState
*s
= opaque
;
128 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
129 qemu_free_timer(s
->open_timer
);
130 s
->open_timer
= NULL
;
133 void qemu_chr_generic_open(CharDriverState
*s
)
135 if (s
->open_timer
== NULL
) {
136 s
->open_timer
= qemu_new_timer_ms(rt_clock
,
137 qemu_chr_fire_open_event
, s
);
138 qemu_mod_timer(s
->open_timer
, qemu_get_clock_ms(rt_clock
) - 1);
142 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
144 return s
->chr_write(s
, buf
, len
);
147 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
151 return s
->chr_ioctl(s
, cmd
, arg
);
154 int qemu_chr_be_can_write(CharDriverState
*s
)
156 if (!s
->chr_can_read
)
158 return s
->chr_can_read(s
->handler_opaque
);
161 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
164 s
->chr_read(s
->handler_opaque
, buf
, len
);
168 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
170 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
173 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
175 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
178 void qemu_chr_accept_input(CharDriverState
*s
)
180 if (s
->chr_accept_input
)
181 s
->chr_accept_input(s
);
185 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
187 char buf
[READ_BUF_LEN
];
190 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
191 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
195 void qemu_chr_add_handlers(CharDriverState
*s
,
196 IOCanReadHandler
*fd_can_read
,
197 IOReadHandler
*fd_read
,
198 IOEventHandler
*fd_event
,
201 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
202 /* chr driver being released. */
203 ++s
->avail_connections
;
205 s
->chr_can_read
= fd_can_read
;
206 s
->chr_read
= fd_read
;
207 s
->chr_event
= fd_event
;
208 s
->handler_opaque
= opaque
;
209 if (s
->chr_update_read_handler
)
210 s
->chr_update_read_handler(s
);
212 /* We're connecting to an already opened device, so let's make sure we
213 also get the open event */
215 qemu_chr_generic_open(s
);
219 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
224 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
226 CharDriverState
*chr
;
228 chr
= g_malloc0(sizeof(CharDriverState
));
229 chr
->chr_write
= null_chr_write
;
233 /* MUX driver for serial I/O splitting */
235 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
236 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
238 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
239 IOReadHandler
*chr_read
[MAX_MUX
];
240 IOEventHandler
*chr_event
[MAX_MUX
];
241 void *ext_opaque
[MAX_MUX
];
242 CharDriverState
*drv
;
247 /* Intermediate input buffer allows to catch escape sequences even if the
248 currently active device is not accepting any input - but only until it
250 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
255 int64_t timestamps_start
;
259 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
261 MuxDriver
*d
= chr
->opaque
;
263 if (!d
->timestamps
) {
264 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
269 for (i
= 0; i
< len
; i
++) {
275 ti
= qemu_get_clock_ms(rt_clock
);
276 if (d
->timestamps_start
== -1)
277 d
->timestamps_start
= ti
;
278 ti
-= d
->timestamps_start
;
280 snprintf(buf1
, sizeof(buf1
),
281 "[%02d:%02d:%02d.%03d] ",
286 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
289 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
290 if (buf
[i
] == '\n') {
298 static const char * const mux_help
[] = {
299 "% h print this help\n\r",
300 "% x exit emulator\n\r",
301 "% s save disk data back to file (if -snapshot)\n\r",
302 "% t toggle console timestamps\n\r"
303 "% b send break (magic sysrq)\n\r",
304 "% c switch between console and monitor\n\r",
309 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
310 static void mux_print_help(CharDriverState
*chr
)
313 char ebuf
[15] = "Escape-Char";
314 char cbuf
[50] = "\n\r";
316 if (term_escape_char
> 0 && term_escape_char
< 26) {
317 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
318 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
320 snprintf(cbuf
, sizeof(cbuf
),
321 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
324 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
325 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
326 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
327 if (mux_help
[i
][j
] == '%')
328 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
330 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
335 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
337 if (d
->chr_event
[mux_nr
])
338 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
341 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
343 if (d
->term_got_escape
) {
344 d
->term_got_escape
= 0;
345 if (ch
== term_escape_char
)
354 const char *term
= "QEMU: Terminated\n\r";
355 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
363 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
366 /* Switch to the next registered device */
367 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
369 if (d
->focus
>= d
->mux_cnt
)
371 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
374 d
->timestamps
= !d
->timestamps
;
375 d
->timestamps_start
= -1;
379 } else if (ch
== term_escape_char
) {
380 d
->term_got_escape
= 1;
388 static void mux_chr_accept_input(CharDriverState
*chr
)
390 MuxDriver
*d
= chr
->opaque
;
393 while (d
->prod
[m
] != d
->cons
[m
] &&
394 d
->chr_can_read
[m
] &&
395 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
396 d
->chr_read
[m
](d
->ext_opaque
[m
],
397 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
401 static int mux_chr_can_read(void *opaque
)
403 CharDriverState
*chr
= opaque
;
404 MuxDriver
*d
= chr
->opaque
;
407 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
409 if (d
->chr_can_read
[m
])
410 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
414 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
416 CharDriverState
*chr
= opaque
;
417 MuxDriver
*d
= chr
->opaque
;
421 mux_chr_accept_input (opaque
);
423 for(i
= 0; i
< size
; i
++)
424 if (mux_proc_byte(chr
, d
, buf
[i
])) {
425 if (d
->prod
[m
] == d
->cons
[m
] &&
426 d
->chr_can_read
[m
] &&
427 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
428 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
430 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
434 static void mux_chr_event(void *opaque
, int event
)
436 CharDriverState
*chr
= opaque
;
437 MuxDriver
*d
= chr
->opaque
;
440 /* Send the event to all registered listeners */
441 for (i
= 0; i
< d
->mux_cnt
; i
++)
442 mux_chr_send_event(d
, i
, event
);
445 static void mux_chr_update_read_handler(CharDriverState
*chr
)
447 MuxDriver
*d
= chr
->opaque
;
449 if (d
->mux_cnt
>= MAX_MUX
) {
450 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
453 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
454 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
455 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
456 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
457 /* Fix up the real driver with mux routines */
458 if (d
->mux_cnt
== 0) {
459 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
462 if (d
->focus
!= -1) {
463 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
465 d
->focus
= d
->mux_cnt
;
467 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
470 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
472 CharDriverState
*chr
;
475 chr
= g_malloc0(sizeof(CharDriverState
));
476 d
= g_malloc0(sizeof(MuxDriver
));
481 chr
->chr_write
= mux_chr_write
;
482 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
483 chr
->chr_accept_input
= mux_chr_accept_input
;
484 /* Frontend guest-open / -close notification is not support with muxes */
485 chr
->chr_guest_open
= NULL
;
486 chr
->chr_guest_close
= NULL
;
488 /* Muxes are always open on creation */
489 qemu_chr_generic_open(chr
);
496 int send_all(int fd
, const void *buf
, int len1
)
502 ret
= send(fd
, buf
, len
, 0);
504 errno
= WSAGetLastError();
505 if (errno
!= WSAEWOULDBLOCK
) {
508 } else if (ret
== 0) {
520 int send_all(int fd
, const void *_buf
, int len1
)
523 const uint8_t *buf
= _buf
;
527 ret
= write(fd
, buf
, len
);
529 if (errno
!= EINTR
&& errno
!= EAGAIN
)
531 } else if (ret
== 0) {
544 typedef struct IOWatchPoll
549 IOCanReadHandler
*fd_can_read
;
552 QTAILQ_ENTRY(IOWatchPoll
) node
;
555 static QTAILQ_HEAD(, IOWatchPoll
) io_watch_poll_list
=
556 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list
);
558 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
562 QTAILQ_FOREACH(i
, &io_watch_poll_list
, node
) {
563 if (i
->src
== source
) {
571 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
573 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
575 iwp
->max_size
= iwp
->fd_can_read(iwp
->opaque
);
576 if (iwp
->max_size
== 0) {
580 return g_io_watch_funcs
.prepare(source
, timeout_
);
583 static gboolean
io_watch_poll_check(GSource
*source
)
585 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
587 if (iwp
->max_size
== 0) {
591 return g_io_watch_funcs
.check(source
);
594 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
597 return g_io_watch_funcs
.dispatch(source
, callback
, user_data
);
600 static void io_watch_poll_finalize(GSource
*source
)
602 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
603 QTAILQ_REMOVE(&io_watch_poll_list
, iwp
, node
);
604 g_io_watch_funcs
.finalize(source
);
607 static GSourceFuncs io_watch_poll_funcs
= {
608 .prepare
= io_watch_poll_prepare
,
609 .check
= io_watch_poll_check
,
610 .dispatch
= io_watch_poll_dispatch
,
611 .finalize
= io_watch_poll_finalize
,
614 /* Can only be used for read */
615 static guint
io_add_watch_poll(GIOChannel
*channel
,
616 IOCanReadHandler
*fd_can_read
,
624 src
= g_io_create_watch(channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
625 g_source_set_funcs(src
, &io_watch_poll_funcs
);
626 g_source_set_callback(src
, (GSourceFunc
)fd_read
, user_data
, NULL
);
627 tag
= g_source_attach(src
, NULL
);
630 iwp
= g_malloc0(sizeof(*iwp
));
633 iwp
->fd_can_read
= fd_can_read
;
634 iwp
->opaque
= user_data
;
636 QTAILQ_INSERT_HEAD(&io_watch_poll_list
, iwp
, node
);
641 static GIOChannel
*io_channel_from_fd(int fd
)
649 chan
= g_io_channel_unix_new(fd
);
651 g_io_channel_set_encoding(chan
, NULL
, NULL
);
652 g_io_channel_set_buffered(chan
, FALSE
);
657 static GIOChannel
*io_channel_from_socket(int fd
)
666 chan
= g_io_channel_win32_new_socket(fd
);
668 chan
= g_io_channel_unix_new(fd
);
671 g_io_channel_set_encoding(chan
, NULL
, NULL
);
672 g_io_channel_set_buffered(chan
, FALSE
);
677 static int io_channel_send_all(GIOChannel
*fd
, const void *_buf
, int len1
)
682 const uint8_t *buf
= _buf
;
686 status
= g_io_channel_write_chars(fd
, (const gchar
*)buf
, len
,
687 &bytes_written
, NULL
);
688 if (status
!= G_IO_STATUS_NORMAL
) {
689 if (status
!= G_IO_STATUS_AGAIN
) {
692 } else if (status
== G_IO_STATUS_EOF
) {
695 buf
+= bytes_written
;
696 len
-= bytes_written
;
702 typedef struct FDCharDriver
{
703 CharDriverState
*chr
;
704 GIOChannel
*fd_in
, *fd_out
;
707 QTAILQ_ENTRY(FDCharDriver
) node
;
710 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
712 FDCharDriver
*s
= chr
->opaque
;
714 return io_channel_send_all(s
->fd_out
, buf
, len
);
717 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
719 CharDriverState
*chr
= opaque
;
720 FDCharDriver
*s
= chr
->opaque
;
722 uint8_t buf
[READ_BUF_LEN
];
727 if (len
> s
->max_size
) {
734 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
735 len
, &bytes_read
, NULL
);
736 if (status
== G_IO_STATUS_EOF
) {
737 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
740 if (status
== G_IO_STATUS_NORMAL
) {
741 qemu_chr_be_write(chr
, buf
, bytes_read
);
747 static int fd_chr_read_poll(void *opaque
)
749 CharDriverState
*chr
= opaque
;
750 FDCharDriver
*s
= chr
->opaque
;
752 s
->max_size
= qemu_chr_be_can_write(chr
);
756 static void fd_chr_update_read_handler(CharDriverState
*chr
)
758 FDCharDriver
*s
= chr
->opaque
;
761 g_source_remove(s
->fd_in_tag
);
765 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
769 static void fd_chr_close(struct CharDriverState
*chr
)
771 FDCharDriver
*s
= chr
->opaque
;
774 g_source_remove(s
->fd_in_tag
);
779 g_io_channel_unref(s
->fd_in
);
782 g_io_channel_unref(s
->fd_out
);
786 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
789 /* open a character device to a unix fd */
790 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
792 CharDriverState
*chr
;
795 chr
= g_malloc0(sizeof(CharDriverState
));
796 s
= g_malloc0(sizeof(FDCharDriver
));
797 s
->fd_in
= io_channel_from_fd(fd_in
);
798 s
->fd_out
= io_channel_from_fd(fd_out
);
801 chr
->chr_write
= fd_chr_write
;
802 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
803 chr
->chr_close
= fd_chr_close
;
805 qemu_chr_generic_open(chr
);
810 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
814 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
815 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
819 return qemu_chr_open_fd(-1, fd_out
);
822 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
825 char filename_in
[256], filename_out
[256];
826 const char *filename
= qemu_opt_get(opts
, "path");
828 if (filename
== NULL
) {
829 fprintf(stderr
, "chardev: pipe: no filename given\n");
833 snprintf(filename_in
, 256, "%s.in", filename
);
834 snprintf(filename_out
, 256, "%s.out", filename
);
835 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
836 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
837 if (fd_in
< 0 || fd_out
< 0) {
842 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
847 return qemu_chr_open_fd(fd_in
, fd_out
);
850 /* init terminal so that we can grab keys */
851 static struct termios oldtty
;
852 static int old_fd0_flags
;
853 static bool stdio_allow_signal
;
855 static void term_exit(void)
857 tcsetattr (0, TCSANOW
, &oldtty
);
858 fcntl(0, F_SETFL
, old_fd0_flags
);
861 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
867 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
868 |INLCR
|IGNCR
|ICRNL
|IXON
);
869 tty
.c_oflag
|= OPOST
;
870 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
871 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
876 /* if graphical mode, we allow Ctrl-C handling */
877 if (!stdio_allow_signal
)
878 tty
.c_lflag
&= ~ISIG
;
880 tcsetattr (0, TCSANOW
, &tty
);
883 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
889 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
891 CharDriverState
*chr
;
893 if (is_daemonized()) {
894 error_report("cannot use stdio with -daemonize");
897 old_fd0_flags
= fcntl(0, F_GETFL
);
898 tcgetattr (0, &oldtty
);
899 fcntl(0, F_SETFL
, O_NONBLOCK
);
902 chr
= qemu_chr_open_fd(0, 1);
903 chr
->chr_close
= qemu_chr_close_stdio
;
904 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
905 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
906 display_type
!= DT_NOGRAPHIC
);
907 qemu_chr_fe_set_echo(chr
, false);
913 /* Once Solaris has openpty(), this is going to be removed. */
914 static int openpty(int *amaster
, int *aslave
, char *name
,
915 struct termios
*termp
, struct winsize
*winp
)
918 int mfd
= -1, sfd
= -1;
920 *amaster
= *aslave
= -1;
922 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
926 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
929 if ((slave
= ptsname(mfd
)) == NULL
)
932 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
935 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
936 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
944 ioctl(sfd
, TIOCSWINSZ
, winp
);
955 static void cfmakeraw (struct termios
*termios_p
)
957 termios_p
->c_iflag
&=
958 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
959 termios_p
->c_oflag
&= ~OPOST
;
960 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
961 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
962 termios_p
->c_cflag
|= CS8
;
964 termios_p
->c_cc
[VMIN
] = 0;
965 termios_p
->c_cc
[VTIME
] = 0;
969 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
970 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
971 || defined(__GLIBC__)
973 #define HAVE_CHARDEV_TTY 1
984 static void pty_chr_update_read_handler(CharDriverState
*chr
);
985 static void pty_chr_state(CharDriverState
*chr
, int connected
);
987 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
989 PtyCharDriver
*s
= chr
->opaque
;
992 /* guest sends data, check for (re-)connect */
993 pty_chr_update_read_handler(chr
);
996 return io_channel_send_all(s
->fd
, buf
, len
);
999 static int pty_chr_read_poll(void *opaque
)
1001 CharDriverState
*chr
= opaque
;
1002 PtyCharDriver
*s
= chr
->opaque
;
1004 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1005 return s
->read_bytes
;
1008 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1010 CharDriverState
*chr
= opaque
;
1011 PtyCharDriver
*s
= chr
->opaque
;
1013 uint8_t buf
[READ_BUF_LEN
];
1017 if (len
> s
->read_bytes
)
1018 len
= s
->read_bytes
;
1021 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1022 if (status
!= G_IO_STATUS_NORMAL
) {
1023 pty_chr_state(chr
, 0);
1026 pty_chr_state(chr
, 1);
1027 qemu_chr_be_write(chr
, buf
, size
);
1032 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1034 PtyCharDriver
*s
= chr
->opaque
;
1037 g_source_remove(s
->fd_tag
);
1040 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1043 * Short timeout here: just need wait long enougth that qemu makes
1044 * it through the poll loop once. When reconnected we want a
1045 * short timeout so we notice it almost instantly. Otherwise
1046 * read() gives us -EIO instantly, making pty_chr_state() reset the
1047 * timeout to the normal (much longer) poll interval before the
1050 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
1053 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1055 PtyCharDriver
*s
= chr
->opaque
;
1058 g_source_remove(s
->fd_tag
);
1062 /* (re-)connect poll interval for idle guests: once per second.
1063 * We check more frequently in case the guests sends data to
1064 * the virtual device linked to our pty. */
1065 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
1068 qemu_chr_generic_open(chr
);
1073 static void pty_chr_timer(void *opaque
)
1075 struct CharDriverState
*chr
= opaque
;
1076 PtyCharDriver
*s
= chr
->opaque
;
1081 /* If we arrive here without polling being cleared due
1082 * read returning -EIO, then we are (re-)connected */
1083 pty_chr_state(chr
, 1);
1088 pty_chr_update_read_handler(chr
);
1091 static void pty_chr_close(struct CharDriverState
*chr
)
1093 PtyCharDriver
*s
= chr
->opaque
;
1097 g_source_remove(s
->fd_tag
);
1099 fd
= g_io_channel_unix_get_fd(s
->fd
);
1100 g_io_channel_unref(s
->fd
);
1102 qemu_del_timer(s
->timer
);
1103 qemu_free_timer(s
->timer
);
1105 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1108 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1110 CharDriverState
*chr
;
1114 int master_fd
, slave_fd
, len
;
1115 #if defined(__OpenBSD__) || defined(__DragonFly__)
1116 char pty_name
[PATH_MAX
];
1117 #define q_ptsname(x) pty_name
1119 char *pty_name
= NULL
;
1120 #define q_ptsname(x) ptsname(x)
1123 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1127 /* Set raw attributes on the pty. */
1128 tcgetattr(slave_fd
, &tty
);
1130 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1133 chr
= g_malloc0(sizeof(CharDriverState
));
1135 len
= strlen(q_ptsname(master_fd
)) + 5;
1136 chr
->filename
= g_malloc(len
);
1137 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
1138 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
1140 label
= qemu_opts_id(opts
);
1141 fprintf(stderr
, "char device redirected to %s%s%s%s\n",
1142 q_ptsname(master_fd
),
1143 label
? " (label " : "",
1147 s
= g_malloc0(sizeof(PtyCharDriver
));
1149 chr
->chr_write
= pty_chr_write
;
1150 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1151 chr
->chr_close
= pty_chr_close
;
1153 s
->fd
= io_channel_from_fd(master_fd
);
1154 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
1159 static void tty_serial_init(int fd
, int speed
,
1160 int parity
, int data_bits
, int stop_bits
)
1166 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1167 speed
, parity
, data_bits
, stop_bits
);
1169 tcgetattr (fd
, &tty
);
1171 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1172 speed
= speed
* 10 / 11;
1189 /* Non-Posix values follow. They may be unsupported on some systems. */
1191 check_speed(115200);
1193 check_speed(230400);
1196 check_speed(460800);
1199 check_speed(500000);
1202 check_speed(576000);
1205 check_speed(921600);
1208 check_speed(1000000);
1211 check_speed(1152000);
1214 check_speed(1500000);
1217 check_speed(2000000);
1220 check_speed(2500000);
1223 check_speed(3000000);
1226 check_speed(3500000);
1229 check_speed(4000000);
1234 cfsetispeed(&tty
, spd
);
1235 cfsetospeed(&tty
, spd
);
1237 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1238 |INLCR
|IGNCR
|ICRNL
|IXON
);
1239 tty
.c_oflag
|= OPOST
;
1240 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1241 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1262 tty
.c_cflag
|= PARENB
;
1265 tty
.c_cflag
|= PARENB
| PARODD
;
1269 tty
.c_cflag
|= CSTOPB
;
1271 tcsetattr (fd
, TCSANOW
, &tty
);
1274 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1276 FDCharDriver
*s
= chr
->opaque
;
1279 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1281 QEMUSerialSetParams
*ssp
= arg
;
1282 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1283 ssp
->speed
, ssp
->parity
,
1284 ssp
->data_bits
, ssp
->stop_bits
);
1287 case CHR_IOCTL_SERIAL_SET_BREAK
:
1289 int enable
= *(int *)arg
;
1291 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1295 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1298 int *targ
= (int *)arg
;
1299 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1301 if (sarg
& TIOCM_CTS
)
1302 *targ
|= CHR_TIOCM_CTS
;
1303 if (sarg
& TIOCM_CAR
)
1304 *targ
|= CHR_TIOCM_CAR
;
1305 if (sarg
& TIOCM_DSR
)
1306 *targ
|= CHR_TIOCM_DSR
;
1307 if (sarg
& TIOCM_RI
)
1308 *targ
|= CHR_TIOCM_RI
;
1309 if (sarg
& TIOCM_DTR
)
1310 *targ
|= CHR_TIOCM_DTR
;
1311 if (sarg
& TIOCM_RTS
)
1312 *targ
|= CHR_TIOCM_RTS
;
1315 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1317 int sarg
= *(int *)arg
;
1319 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1320 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1321 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1322 if (sarg
& CHR_TIOCM_CTS
)
1324 if (sarg
& CHR_TIOCM_CAR
)
1326 if (sarg
& CHR_TIOCM_DSR
)
1328 if (sarg
& CHR_TIOCM_RI
)
1330 if (sarg
& CHR_TIOCM_DTR
)
1332 if (sarg
& CHR_TIOCM_RTS
)
1334 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1343 static void qemu_chr_close_tty(CharDriverState
*chr
)
1345 FDCharDriver
*s
= chr
->opaque
;
1349 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1359 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1361 CharDriverState
*chr
;
1363 tty_serial_init(fd
, 115200, 'N', 8, 1);
1364 chr
= qemu_chr_open_fd(fd
, fd
);
1365 chr
->chr_ioctl
= tty_serial_ioctl
;
1366 chr
->chr_close
= qemu_chr_close_tty
;
1370 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1372 const char *filename
= qemu_opt_get(opts
, "path");
1375 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1379 return qemu_chr_open_tty_fd(fd
);
1381 #endif /* __linux__ || __sun__ */
1383 #if defined(__linux__)
1385 #define HAVE_CHARDEV_PARPORT 1
1390 } ParallelCharDriver
;
1392 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1394 if (s
->mode
!= mode
) {
1396 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1403 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1405 ParallelCharDriver
*drv
= chr
->opaque
;
1410 case CHR_IOCTL_PP_READ_DATA
:
1411 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1413 *(uint8_t *)arg
= b
;
1415 case CHR_IOCTL_PP_WRITE_DATA
:
1416 b
= *(uint8_t *)arg
;
1417 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1420 case CHR_IOCTL_PP_READ_CONTROL
:
1421 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1423 /* Linux gives only the lowest bits, and no way to know data
1424 direction! For better compatibility set the fixed upper
1426 *(uint8_t *)arg
= b
| 0xc0;
1428 case CHR_IOCTL_PP_WRITE_CONTROL
:
1429 b
= *(uint8_t *)arg
;
1430 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1433 case CHR_IOCTL_PP_READ_STATUS
:
1434 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1436 *(uint8_t *)arg
= b
;
1438 case CHR_IOCTL_PP_DATA_DIR
:
1439 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1442 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1443 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1444 struct ParallelIOArg
*parg
= arg
;
1445 int n
= read(fd
, parg
->buffer
, parg
->count
);
1446 if (n
!= parg
->count
) {
1451 case CHR_IOCTL_PP_EPP_READ
:
1452 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1453 struct ParallelIOArg
*parg
= arg
;
1454 int n
= read(fd
, parg
->buffer
, parg
->count
);
1455 if (n
!= parg
->count
) {
1460 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1461 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1462 struct ParallelIOArg
*parg
= arg
;
1463 int n
= write(fd
, parg
->buffer
, parg
->count
);
1464 if (n
!= parg
->count
) {
1469 case CHR_IOCTL_PP_EPP_WRITE
:
1470 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1471 struct ParallelIOArg
*parg
= arg
;
1472 int n
= write(fd
, parg
->buffer
, parg
->count
);
1473 if (n
!= parg
->count
) {
1484 static void pp_close(CharDriverState
*chr
)
1486 ParallelCharDriver
*drv
= chr
->opaque
;
1489 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1490 ioctl(fd
, PPRELEASE
);
1493 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1496 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1498 CharDriverState
*chr
;
1499 ParallelCharDriver
*drv
;
1501 if (ioctl(fd
, PPCLAIM
) < 0) {
1506 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1508 drv
->mode
= IEEE1284_MODE_COMPAT
;
1510 chr
= g_malloc0(sizeof(CharDriverState
));
1511 chr
->chr_write
= null_chr_write
;
1512 chr
->chr_ioctl
= pp_ioctl
;
1513 chr
->chr_close
= pp_close
;
1516 qemu_chr_generic_open(chr
);
1520 #endif /* __linux__ */
1522 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1524 #define HAVE_CHARDEV_PARPORT 1
1526 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1528 int fd
= (int)(intptr_t)chr
->opaque
;
1532 case CHR_IOCTL_PP_READ_DATA
:
1533 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1535 *(uint8_t *)arg
= b
;
1537 case CHR_IOCTL_PP_WRITE_DATA
:
1538 b
= *(uint8_t *)arg
;
1539 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1542 case CHR_IOCTL_PP_READ_CONTROL
:
1543 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1545 *(uint8_t *)arg
= b
;
1547 case CHR_IOCTL_PP_WRITE_CONTROL
:
1548 b
= *(uint8_t *)arg
;
1549 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1552 case CHR_IOCTL_PP_READ_STATUS
:
1553 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1555 *(uint8_t *)arg
= b
;
1563 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1565 CharDriverState
*chr
;
1567 chr
= g_malloc0(sizeof(CharDriverState
));
1568 chr
->opaque
= (void *)(intptr_t)fd
;
1569 chr
->chr_write
= null_chr_write
;
1570 chr
->chr_ioctl
= pp_ioctl
;
1579 HANDLE hcom
, hrecv
, hsend
;
1580 OVERLAPPED orecv
, osend
;
1587 HANDLE hInputReadyEvent
;
1588 HANDLE hInputDoneEvent
;
1589 HANDLE hInputThread
;
1590 uint8_t win_stdio_buf
;
1591 } WinStdioCharState
;
1593 #define NSENDBUF 2048
1594 #define NRECVBUF 2048
1595 #define MAXCONNECT 1
1596 #define NTIMEOUT 5000
1598 static int win_chr_poll(void *opaque
);
1599 static int win_chr_pipe_poll(void *opaque
);
1601 static void win_chr_close(CharDriverState
*chr
)
1603 WinCharState
*s
= chr
->opaque
;
1606 CloseHandle(s
->hsend
);
1610 CloseHandle(s
->hrecv
);
1614 CloseHandle(s
->hcom
);
1618 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1620 qemu_del_polling_cb(win_chr_poll
, chr
);
1622 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1625 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1627 WinCharState
*s
= chr
->opaque
;
1629 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1634 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1636 fprintf(stderr
, "Failed CreateEvent\n");
1639 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1641 fprintf(stderr
, "Failed CreateEvent\n");
1645 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1646 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1647 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1648 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1653 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1654 fprintf(stderr
, "Failed SetupComm\n");
1658 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1659 size
= sizeof(COMMCONFIG
);
1660 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1661 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1662 CommConfigDialog(filename
, NULL
, &comcfg
);
1664 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1665 fprintf(stderr
, "Failed SetCommState\n");
1669 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1670 fprintf(stderr
, "Failed SetCommMask\n");
1674 cto
.ReadIntervalTimeout
= MAXDWORD
;
1675 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1676 fprintf(stderr
, "Failed SetCommTimeouts\n");
1680 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1681 fprintf(stderr
, "Failed ClearCommError\n");
1684 qemu_add_polling_cb(win_chr_poll
, chr
);
1692 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1694 WinCharState
*s
= chr
->opaque
;
1695 DWORD len
, ret
, size
, err
;
1698 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1699 s
->osend
.hEvent
= s
->hsend
;
1702 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1704 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1706 err
= GetLastError();
1707 if (err
== ERROR_IO_PENDING
) {
1708 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1726 static int win_chr_read_poll(CharDriverState
*chr
)
1728 WinCharState
*s
= chr
->opaque
;
1730 s
->max_size
= qemu_chr_be_can_write(chr
);
1734 static void win_chr_readfile(CharDriverState
*chr
)
1736 WinCharState
*s
= chr
->opaque
;
1738 uint8_t buf
[READ_BUF_LEN
];
1741 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1742 s
->orecv
.hEvent
= s
->hrecv
;
1743 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1745 err
= GetLastError();
1746 if (err
== ERROR_IO_PENDING
) {
1747 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1752 qemu_chr_be_write(chr
, buf
, size
);
1756 static void win_chr_read(CharDriverState
*chr
)
1758 WinCharState
*s
= chr
->opaque
;
1760 if (s
->len
> s
->max_size
)
1761 s
->len
= s
->max_size
;
1765 win_chr_readfile(chr
);
1768 static int win_chr_poll(void *opaque
)
1770 CharDriverState
*chr
= opaque
;
1771 WinCharState
*s
= chr
->opaque
;
1775 ClearCommError(s
->hcom
, &comerr
, &status
);
1776 if (status
.cbInQue
> 0) {
1777 s
->len
= status
.cbInQue
;
1778 win_chr_read_poll(chr
);
1785 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1787 CharDriverState
*chr
;
1790 chr
= g_malloc0(sizeof(CharDriverState
));
1791 s
= g_malloc0(sizeof(WinCharState
));
1793 chr
->chr_write
= win_chr_write
;
1794 chr
->chr_close
= win_chr_close
;
1796 if (win_chr_init(chr
, filename
) < 0) {
1801 qemu_chr_generic_open(chr
);
1805 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1807 return qemu_chr_open_win_path(qemu_opt_get(opts
, "path"));
1810 static int win_chr_pipe_poll(void *opaque
)
1812 CharDriverState
*chr
= opaque
;
1813 WinCharState
*s
= chr
->opaque
;
1816 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1819 win_chr_read_poll(chr
);
1826 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1828 WinCharState
*s
= chr
->opaque
;
1836 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1838 fprintf(stderr
, "Failed CreateEvent\n");
1841 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1843 fprintf(stderr
, "Failed CreateEvent\n");
1847 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1848 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1849 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1851 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1852 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1853 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1858 ZeroMemory(&ov
, sizeof(ov
));
1859 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1860 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1862 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1866 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1868 fprintf(stderr
, "Failed GetOverlappedResult\n");
1870 CloseHandle(ov
.hEvent
);
1877 CloseHandle(ov
.hEvent
);
1880 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1889 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1891 const char *filename
= qemu_opt_get(opts
, "path");
1892 CharDriverState
*chr
;
1895 chr
= g_malloc0(sizeof(CharDriverState
));
1896 s
= g_malloc0(sizeof(WinCharState
));
1898 chr
->chr_write
= win_chr_write
;
1899 chr
->chr_close
= win_chr_close
;
1901 if (win_chr_pipe_init(chr
, filename
) < 0) {
1906 qemu_chr_generic_open(chr
);
1910 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1912 CharDriverState
*chr
;
1915 chr
= g_malloc0(sizeof(CharDriverState
));
1916 s
= g_malloc0(sizeof(WinCharState
));
1919 chr
->chr_write
= win_chr_write
;
1920 qemu_chr_generic_open(chr
);
1924 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1926 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1929 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1931 const char *file_out
= qemu_opt_get(opts
, "path");
1934 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1935 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1936 if (fd_out
== INVALID_HANDLE_VALUE
) {
1940 return qemu_chr_open_win_file(fd_out
);
1943 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1945 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1952 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1962 static void win_stdio_wait_func(void *opaque
)
1964 CharDriverState
*chr
= opaque
;
1965 WinStdioCharState
*stdio
= chr
->opaque
;
1966 INPUT_RECORD buf
[4];
1971 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1975 /* Avoid error storm */
1976 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1980 for (i
= 0; i
< dwSize
; i
++) {
1981 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1983 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1985 if (kev
->uChar
.AsciiChar
!= 0) {
1986 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1987 if (qemu_chr_be_can_write(chr
)) {
1988 uint8_t c
= kev
->uChar
.AsciiChar
;
1989 qemu_chr_be_write(chr
, &c
, 1);
1997 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
1999 CharDriverState
*chr
= param
;
2000 WinStdioCharState
*stdio
= chr
->opaque
;
2006 /* Wait for one byte */
2007 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2009 /* Exit in case of error, continue if nothing read */
2017 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2018 if (stdio
->win_stdio_buf
== '\r') {
2022 /* Signal the main thread and wait until the byte was eaten */
2023 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2026 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2032 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2036 static void win_stdio_thread_wait_func(void *opaque
)
2038 CharDriverState
*chr
= opaque
;
2039 WinStdioCharState
*stdio
= chr
->opaque
;
2041 if (qemu_chr_be_can_write(chr
)) {
2042 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2045 SetEvent(stdio
->hInputDoneEvent
);
2048 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2050 WinStdioCharState
*stdio
= chr
->opaque
;
2053 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2056 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2058 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2062 static void win_stdio_close(CharDriverState
*chr
)
2064 WinStdioCharState
*stdio
= chr
->opaque
;
2066 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2067 CloseHandle(stdio
->hInputReadyEvent
);
2069 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2070 CloseHandle(stdio
->hInputDoneEvent
);
2072 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2073 TerminateThread(stdio
->hInputThread
, 0);
2076 g_free(chr
->opaque
);
2080 static CharDriverState
*qemu_chr_open_win_stdio(QemuOpts
*opts
)
2082 CharDriverState
*chr
;
2083 WinStdioCharState
*stdio
;
2087 chr
= g_malloc0(sizeof(CharDriverState
));
2088 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2090 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2091 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2092 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2096 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2098 chr
->opaque
= stdio
;
2099 chr
->chr_write
= win_stdio_write
;
2100 chr
->chr_close
= win_stdio_close
;
2103 if (qemu_add_wait_object(stdio
->hStdIn
,
2104 win_stdio_wait_func
, chr
)) {
2105 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2110 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2111 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2112 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2115 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2116 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2117 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2118 fprintf(stderr
, "cannot create stdio thread or event\n");
2121 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2122 win_stdio_thread_wait_func
, chr
)) {
2123 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2127 dwMode
|= ENABLE_LINE_INPUT
;
2130 /* set the terminal in raw mode */
2131 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2132 dwMode
|= ENABLE_PROCESSED_INPUT
;
2135 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2137 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2138 qemu_chr_fe_set_echo(chr
, false);
2142 #endif /* !_WIN32 */
2144 /***********************************************************/
2145 /* UDP Net console */
2151 uint8_t buf
[READ_BUF_LEN
];
2157 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2159 NetCharDriver
*s
= chr
->opaque
;
2160 gsize bytes_written
;
2163 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2164 if (status
== G_IO_STATUS_EOF
) {
2166 } else if (status
!= G_IO_STATUS_NORMAL
) {
2170 return bytes_written
;
2173 static int udp_chr_read_poll(void *opaque
)
2175 CharDriverState
*chr
= opaque
;
2176 NetCharDriver
*s
= chr
->opaque
;
2178 s
->max_size
= qemu_chr_be_can_write(chr
);
2180 /* If there were any stray characters in the queue process them
2183 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2184 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2186 s
->max_size
= qemu_chr_be_can_write(chr
);
2191 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2193 CharDriverState
*chr
= opaque
;
2194 NetCharDriver
*s
= chr
->opaque
;
2195 gsize bytes_read
= 0;
2198 if (s
->max_size
== 0)
2200 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2202 s
->bufcnt
= bytes_read
;
2203 s
->bufptr
= s
->bufcnt
;
2204 if (status
!= G_IO_STATUS_NORMAL
) {
2209 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2210 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2212 s
->max_size
= qemu_chr_be_can_write(chr
);
2218 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2220 NetCharDriver
*s
= chr
->opaque
;
2223 g_source_remove(s
->tag
);
2228 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2232 static void udp_chr_close(CharDriverState
*chr
)
2234 NetCharDriver
*s
= chr
->opaque
;
2236 g_source_remove(s
->tag
);
2239 g_io_channel_unref(s
->chan
);
2243 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2246 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2248 CharDriverState
*chr
= NULL
;
2249 NetCharDriver
*s
= NULL
;
2250 Error
*local_err
= NULL
;
2253 chr
= g_malloc0(sizeof(CharDriverState
));
2254 s
= g_malloc0(sizeof(NetCharDriver
));
2256 fd
= inet_dgram_opts(opts
, &local_err
);
2262 s
->chan
= io_channel_from_socket(s
->fd
);
2266 chr
->chr_write
= udp_chr_write
;
2267 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2268 chr
->chr_close
= udp_chr_close
;
2273 qerror_report_err(local_err
);
2274 error_free(local_err
);
2284 /***********************************************************/
2285 /* TCP Net console */
2297 static void tcp_chr_accept(void *opaque
);
2299 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2301 TCPCharDriver
*s
= chr
->opaque
;
2303 return send_all(s
->fd
, buf
, len
);
2305 /* XXX: indicate an error ? */
2310 static int tcp_chr_read_poll(void *opaque
)
2312 CharDriverState
*chr
= opaque
;
2313 TCPCharDriver
*s
= chr
->opaque
;
2316 s
->max_size
= qemu_chr_be_can_write(chr
);
2321 #define IAC_BREAK 243
2322 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2324 uint8_t *buf
, int *size
)
2326 /* Handle any telnet client's basic IAC options to satisfy char by
2327 * char mode with no echo. All IAC options will be removed from
2328 * the buf and the do_telnetopt variable will be used to track the
2329 * state of the width of the IAC information.
2331 * IAC commands come in sets of 3 bytes with the exception of the
2332 * "IAC BREAK" command and the double IAC.
2338 for (i
= 0; i
< *size
; i
++) {
2339 if (s
->do_telnetopt
> 1) {
2340 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2341 /* Double IAC means send an IAC */
2345 s
->do_telnetopt
= 1;
2347 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2348 /* Handle IAC break commands by sending a serial break */
2349 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2354 if (s
->do_telnetopt
>= 4) {
2355 s
->do_telnetopt
= 1;
2358 if ((unsigned char)buf
[i
] == IAC
) {
2359 s
->do_telnetopt
= 2;
2370 static int tcp_get_msgfd(CharDriverState
*chr
)
2372 TCPCharDriver
*s
= chr
->opaque
;
2379 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2381 TCPCharDriver
*s
= chr
->opaque
;
2382 struct cmsghdr
*cmsg
;
2384 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2387 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2388 cmsg
->cmsg_level
!= SOL_SOCKET
||
2389 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2392 fd
= *((int *)CMSG_DATA(cmsg
));
2396 #ifndef MSG_CMSG_CLOEXEC
2397 qemu_set_cloexec(fd
);
2405 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2407 TCPCharDriver
*s
= chr
->opaque
;
2408 struct msghdr msg
= { NULL
, };
2409 struct iovec iov
[1];
2411 struct cmsghdr cmsg
;
2412 char control
[CMSG_SPACE(sizeof(int))];
2417 iov
[0].iov_base
= buf
;
2418 iov
[0].iov_len
= len
;
2422 msg
.msg_control
= &msg_control
;
2423 msg
.msg_controllen
= sizeof(msg_control
);
2425 #ifdef MSG_CMSG_CLOEXEC
2426 flags
|= MSG_CMSG_CLOEXEC
;
2428 ret
= recvmsg(s
->fd
, &msg
, flags
);
2429 if (ret
> 0 && s
->is_unix
) {
2430 unix_process_msgfd(chr
, &msg
);
2436 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2438 TCPCharDriver
*s
= chr
->opaque
;
2439 return qemu_recv(s
->fd
, buf
, len
, 0);
2443 static void tcp_chr_read(void *opaque
)
2445 CharDriverState
*chr
= opaque
;
2446 TCPCharDriver
*s
= chr
->opaque
;
2447 uint8_t buf
[READ_BUF_LEN
];
2450 if (!s
->connected
|| s
->max_size
<= 0)
2453 if (len
> s
->max_size
)
2455 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2457 /* connection closed */
2459 if (s
->listen_fd
>= 0) {
2460 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2462 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2465 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2466 } else if (size
> 0) {
2467 if (s
->do_telnetopt
)
2468 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2470 qemu_chr_be_write(chr
, buf
, size
);
2475 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2477 return qemu_chr_open_fd(eventfd
, eventfd
);
2481 static void tcp_chr_connect(void *opaque
)
2483 CharDriverState
*chr
= opaque
;
2484 TCPCharDriver
*s
= chr
->opaque
;
2488 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2489 tcp_chr_read
, NULL
, chr
);
2491 qemu_chr_generic_open(chr
);
2494 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2495 static void tcp_chr_telnet_init(int fd
)
2498 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2499 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2500 send(fd
, (char *)buf
, 3, 0);
2501 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2502 send(fd
, (char *)buf
, 3, 0);
2503 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2504 send(fd
, (char *)buf
, 3, 0);
2505 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2506 send(fd
, (char *)buf
, 3, 0);
2509 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2511 TCPCharDriver
*s
= chr
->opaque
;
2515 socket_set_nonblock(fd
);
2517 socket_set_nodelay(fd
);
2519 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2520 tcp_chr_connect(chr
);
2525 static void tcp_chr_accept(void *opaque
)
2527 CharDriverState
*chr
= opaque
;
2528 TCPCharDriver
*s
= chr
->opaque
;
2529 struct sockaddr_in saddr
;
2531 struct sockaddr_un uaddr
;
2533 struct sockaddr
*addr
;
2540 len
= sizeof(uaddr
);
2541 addr
= (struct sockaddr
*)&uaddr
;
2545 len
= sizeof(saddr
);
2546 addr
= (struct sockaddr
*)&saddr
;
2548 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2549 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)
2561 static void tcp_chr_close(CharDriverState
*chr
)
2563 TCPCharDriver
*s
= chr
->opaque
;
2565 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2568 if (s
->listen_fd
>= 0) {
2569 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2570 closesocket(s
->listen_fd
);
2573 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2576 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2577 bool is_listen
, bool is_telnet
,
2578 bool is_waitconnect
,
2581 CharDriverState
*chr
= NULL
;
2582 TCPCharDriver
*s
= NULL
;
2583 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2584 const char *left
= "", *right
= "";
2585 struct sockaddr_storage ss
;
2586 socklen_t ss_len
= sizeof(ss
);
2588 memset(&ss
, 0, ss_len
);
2589 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2590 error_setg(errp
, "getsockname: %s", strerror(errno
));
2594 chr
= g_malloc0(sizeof(CharDriverState
));
2595 s
= g_malloc0(sizeof(TCPCharDriver
));
2602 chr
->filename
= g_malloc(256);
2603 switch (ss
.ss_family
) {
2607 snprintf(chr
->filename
, 256, "unix:%s%s",
2608 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2609 is_listen
? ",server" : "");
2617 s
->do_nodelay
= do_nodelay
;
2618 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2619 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2620 snprintf(chr
->filename
, 256, "%s:%s:%s%s%s%s",
2621 is_telnet
? "telnet" : "tcp",
2622 left
, host
, right
, serv
,
2623 is_listen
? ",server" : "");
2628 chr
->chr_write
= tcp_chr_write
;
2629 chr
->chr_close
= tcp_chr_close
;
2630 chr
->get_msgfd
= tcp_get_msgfd
;
2631 chr
->chr_add_client
= tcp_chr_add_client
;
2635 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2637 s
->do_telnetopt
= 1;
2642 socket_set_nodelay(fd
);
2643 tcp_chr_connect(chr
);
2646 if (is_listen
&& is_waitconnect
) {
2647 printf("QEMU waiting for connection on: %s\n",
2649 tcp_chr_accept(chr
);
2650 socket_set_nonblock(s
->listen_fd
);
2655 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2657 CharDriverState
*chr
= NULL
;
2658 Error
*local_err
= NULL
;
2666 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2667 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2668 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2669 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2670 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2676 fd
= unix_listen_opts(opts
, &local_err
);
2678 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2682 fd
= inet_listen_opts(opts
, 0, &local_err
);
2684 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2691 if (!is_waitconnect
)
2692 socket_set_nonblock(fd
);
2694 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2695 is_waitconnect
, &local_err
);
2696 if (error_is_set(&local_err
)) {
2704 qerror_report_err(local_err
);
2705 error_free(local_err
);
2711 g_free(chr
->opaque
);
2717 /***********************************************************/
2718 /* Memory chardev */
2721 size_t outbuf_capacity
;
2725 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2727 MemoryDriver
*d
= chr
->opaque
;
2729 /* TODO: the QString implementation has the same code, we should
2730 * introduce a generic way to do this in cutils.c */
2731 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2733 d
->outbuf_capacity
+= len
;
2734 d
->outbuf_capacity
*= 2;
2735 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2738 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2739 d
->outbuf_size
+= len
;
2744 void qemu_chr_init_mem(CharDriverState
*chr
)
2748 d
= g_malloc(sizeof(*d
));
2750 d
->outbuf_capacity
= 4096;
2751 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2753 memset(chr
, 0, sizeof(*chr
));
2755 chr
->chr_write
= mem_chr_write
;
2758 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2760 MemoryDriver
*d
= chr
->opaque
;
2761 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2764 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2765 void qemu_chr_close_mem(CharDriverState
*chr
)
2767 MemoryDriver
*d
= chr
->opaque
;
2770 g_free(chr
->opaque
);
2772 chr
->chr_write
= NULL
;
2775 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2777 const MemoryDriver
*d
= chr
->opaque
;
2778 return d
->outbuf_size
;
2781 /*********************************************************/
2782 /* Ring buffer chardev */
2789 } RingBufCharDriver
;
2791 static size_t ringbuf_count(const CharDriverState
*chr
)
2793 const RingBufCharDriver
*d
= chr
->opaque
;
2795 return d
->prod
- d
->cons
;
2798 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2800 RingBufCharDriver
*d
= chr
->opaque
;
2803 if (!buf
|| (len
< 0)) {
2807 for (i
= 0; i
< len
; i
++ ) {
2808 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2809 if (d
->prod
- d
->cons
> d
->size
) {
2810 d
->cons
= d
->prod
- d
->size
;
2817 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2819 RingBufCharDriver
*d
= chr
->opaque
;
2822 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2823 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2829 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2831 RingBufCharDriver
*d
= chr
->opaque
;
2838 static CharDriverState
*qemu_chr_open_ringbuf(QemuOpts
*opts
)
2840 CharDriverState
*chr
;
2841 RingBufCharDriver
*d
;
2843 chr
= g_malloc0(sizeof(CharDriverState
));
2844 d
= g_malloc(sizeof(*d
));
2846 d
->size
= qemu_opt_get_size(opts
, "size", 0);
2851 /* The size must be power of 2 */
2852 if (d
->size
& (d
->size
- 1)) {
2853 error_report("size of ringbuf device must be power of two");
2859 d
->cbuf
= g_malloc0(d
->size
);
2862 chr
->chr_write
= ringbuf_chr_write
;
2863 chr
->chr_close
= ringbuf_chr_close
;
2873 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2875 return chr
->chr_write
== ringbuf_chr_write
;
2878 void qmp_ringbuf_write(const char *device
, const char *data
,
2879 bool has_format
, enum DataFormat format
,
2882 CharDriverState
*chr
;
2883 const uint8_t *write_data
;
2887 chr
= qemu_chr_find(device
);
2889 error_setg(errp
, "Device '%s' not found", device
);
2893 if (!chr_is_ringbuf(chr
)) {
2894 error_setg(errp
,"%s is not a ringbuf device", device
);
2898 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2899 write_data
= g_base64_decode(data
, &write_count
);
2901 write_data
= (uint8_t *)data
;
2902 write_count
= strlen(data
);
2905 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2907 if (write_data
!= (uint8_t *)data
) {
2908 g_free((void *)write_data
);
2912 error_setg(errp
, "Failed to write to device %s", device
);
2917 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2918 bool has_format
, enum DataFormat format
,
2921 CharDriverState
*chr
;
2926 chr
= qemu_chr_find(device
);
2928 error_setg(errp
, "Device '%s' not found", device
);
2932 if (!chr_is_ringbuf(chr
)) {
2933 error_setg(errp
,"%s is not a ringbuf device", device
);
2938 error_setg(errp
, "size must be greater than zero");
2942 count
= ringbuf_count(chr
);
2943 size
= size
> count
? count
: size
;
2944 read_data
= g_malloc(size
+ 1);
2946 ringbuf_chr_read(chr
, read_data
, size
);
2948 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2949 data
= g_base64_encode(read_data
, size
);
2953 * FIXME should read only complete, valid UTF-8 characters up
2954 * to @size bytes. Invalid sequences should be replaced by a
2955 * suitable replacement character. Except when (and only
2956 * when) ring buffer lost characters since last read, initial
2957 * continuation characters should be dropped.
2959 read_data
[size
] = 0;
2960 data
= (char *)read_data
;
2966 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2968 char host
[65], port
[33], width
[8], height
[8];
2972 Error
*local_err
= NULL
;
2974 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2975 if (error_is_set(&local_err
)) {
2976 qerror_report_err(local_err
);
2977 error_free(local_err
);
2981 if (strstart(filename
, "mon:", &p
)) {
2983 qemu_opt_set(opts
, "mux", "on");
2986 if (strcmp(filename
, "null") == 0 ||
2987 strcmp(filename
, "pty") == 0 ||
2988 strcmp(filename
, "msmouse") == 0 ||
2989 strcmp(filename
, "braille") == 0 ||
2990 strcmp(filename
, "stdio") == 0) {
2991 qemu_opt_set(opts
, "backend", filename
);
2994 if (strstart(filename
, "vc", &p
)) {
2995 qemu_opt_set(opts
, "backend", "vc");
2997 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2999 qemu_opt_set(opts
, "width", width
);
3000 qemu_opt_set(opts
, "height", height
);
3001 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3003 qemu_opt_set(opts
, "cols", width
);
3004 qemu_opt_set(opts
, "rows", height
);
3011 if (strcmp(filename
, "con:") == 0) {
3012 qemu_opt_set(opts
, "backend", "console");
3015 if (strstart(filename
, "COM", NULL
)) {
3016 qemu_opt_set(opts
, "backend", "serial");
3017 qemu_opt_set(opts
, "path", filename
);
3020 if (strstart(filename
, "file:", &p
)) {
3021 qemu_opt_set(opts
, "backend", "file");
3022 qemu_opt_set(opts
, "path", p
);
3025 if (strstart(filename
, "pipe:", &p
)) {
3026 qemu_opt_set(opts
, "backend", "pipe");
3027 qemu_opt_set(opts
, "path", p
);
3030 if (strstart(filename
, "tcp:", &p
) ||
3031 strstart(filename
, "telnet:", &p
)) {
3032 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3034 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3037 qemu_opt_set(opts
, "backend", "socket");
3038 qemu_opt_set(opts
, "host", host
);
3039 qemu_opt_set(opts
, "port", port
);
3040 if (p
[pos
] == ',') {
3041 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3044 if (strstart(filename
, "telnet:", &p
))
3045 qemu_opt_set(opts
, "telnet", "on");
3048 if (strstart(filename
, "udp:", &p
)) {
3049 qemu_opt_set(opts
, "backend", "udp");
3050 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3052 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3056 qemu_opt_set(opts
, "host", host
);
3057 qemu_opt_set(opts
, "port", port
);
3058 if (p
[pos
] == '@') {
3060 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3062 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3066 qemu_opt_set(opts
, "localaddr", host
);
3067 qemu_opt_set(opts
, "localport", port
);
3071 if (strstart(filename
, "unix:", &p
)) {
3072 qemu_opt_set(opts
, "backend", "socket");
3073 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3077 if (strstart(filename
, "/dev/parport", NULL
) ||
3078 strstart(filename
, "/dev/ppi", NULL
)) {
3079 qemu_opt_set(opts
, "backend", "parport");
3080 qemu_opt_set(opts
, "path", filename
);
3083 if (strstart(filename
, "/dev/", NULL
)) {
3084 qemu_opt_set(opts
, "backend", "tty");
3085 qemu_opt_set(opts
, "path", filename
);
3090 qemu_opts_del(opts
);
3094 #ifdef HAVE_CHARDEV_PARPORT
3096 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
3098 const char *filename
= qemu_opt_get(opts
, "path");
3101 fd
= qemu_open(filename
, O_RDWR
);
3105 return qemu_chr_open_pp_fd(fd
);
3110 static const struct {
3112 CharDriverState
*(*open
)(QemuOpts
*opts
);
3113 } backend_table
[] = {
3114 { .name
= "null", .open
= qemu_chr_open_null
},
3115 { .name
= "socket", .open
= qemu_chr_open_socket
},
3116 { .name
= "udp", .open
= qemu_chr_open_udp
},
3117 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
3118 { .name
= "vc", .open
= vc_init
},
3119 { .name
= "memory", .open
= qemu_chr_open_ringbuf
},
3121 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
3122 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
3123 { .name
= "console", .open
= qemu_chr_open_win_con
},
3124 { .name
= "serial", .open
= qemu_chr_open_win
},
3125 { .name
= "stdio", .open
= qemu_chr_open_win_stdio
},
3127 { .name
= "file", .open
= qemu_chr_open_file_out
},
3128 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
3129 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
3131 #ifdef CONFIG_BRLAPI
3132 { .name
= "braille", .open
= chr_baum_init
},
3134 #ifdef HAVE_CHARDEV_TTY
3135 { .name
= "tty", .open
= qemu_chr_open_tty
},
3136 { .name
= "serial", .open
= qemu_chr_open_tty
},
3137 { .name
= "pty", .open
= qemu_chr_open_pty
},
3139 #ifdef HAVE_CHARDEV_PARPORT
3140 { .name
= "parallel", .open
= qemu_chr_open_pp
},
3141 { .name
= "parport", .open
= qemu_chr_open_pp
},
3144 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
3145 #if SPICE_SERVER_VERSION >= 0x000c02
3146 { .name
= "spiceport", .open
= qemu_chr_open_spice_port
},
3151 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3152 void (*init
)(struct CharDriverState
*s
),
3155 CharDriverState
*chr
;
3158 if (qemu_opts_id(opts
) == NULL
) {
3159 error_setg(errp
, "chardev: no id specified");
3163 if (qemu_opt_get(opts
, "backend") == NULL
) {
3164 error_setg(errp
, "chardev: \"%s\" missing backend",
3165 qemu_opts_id(opts
));
3168 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
3169 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
3172 if (i
== ARRAY_SIZE(backend_table
)) {
3173 error_setg(errp
, "chardev: backend \"%s\" not found",
3174 qemu_opt_get(opts
, "backend"));
3178 chr
= backend_table
[i
].open(opts
);
3180 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3181 qemu_opt_get(opts
, "backend"));
3186 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3188 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3190 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3191 CharDriverState
*base
= chr
;
3192 int len
= strlen(qemu_opts_id(opts
)) + 6;
3193 base
->label
= g_malloc(len
);
3194 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3195 chr
= qemu_chr_open_mux(base
);
3196 chr
->filename
= base
->filename
;
3197 chr
->avail_connections
= MAX_MUX
;
3198 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3200 chr
->avail_connections
= 1;
3202 chr
->label
= g_strdup(qemu_opts_id(opts
));
3207 qemu_opts_del(opts
);
3211 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3214 CharDriverState
*chr
;
3218 if (strstart(filename
, "chardev:", &p
)) {
3219 return qemu_chr_find(p
);
3222 opts
= qemu_chr_parse_compat(label
, filename
);
3226 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3227 if (error_is_set(&err
)) {
3228 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3231 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3232 monitor_init(chr
, MONITOR_USE_READLINE
);
3237 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3239 if (chr
->chr_set_echo
) {
3240 chr
->chr_set_echo(chr
, echo
);
3244 void qemu_chr_fe_open(struct CharDriverState
*chr
)
3246 if (chr
->chr_guest_open
) {
3247 chr
->chr_guest_open(chr
);
3251 void qemu_chr_fe_close(struct CharDriverState
*chr
)
3253 if (chr
->chr_guest_close
) {
3254 chr
->chr_guest_close(chr
);
3258 void qemu_chr_delete(CharDriverState
*chr
)
3260 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3261 if (chr
->chr_close
) {
3262 chr
->chr_close(chr
);
3264 g_free(chr
->filename
);
3267 qemu_opts_del(chr
->opts
);
3272 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3274 ChardevInfoList
*chr_list
= NULL
;
3275 CharDriverState
*chr
;
3277 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3278 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3279 info
->value
= g_malloc0(sizeof(*info
->value
));
3280 info
->value
->label
= g_strdup(chr
->label
);
3281 info
->value
->filename
= g_strdup(chr
->filename
);
3283 info
->next
= chr_list
;
3290 CharDriverState
*qemu_chr_find(const char *name
)
3292 CharDriverState
*chr
;
3294 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3295 if (strcmp(chr
->label
, name
) != 0)
3302 /* Get a character (serial) device interface. */
3303 CharDriverState
*qemu_char_get_next_serial(void)
3305 static int next_serial
;
3307 /* FIXME: This function needs to go away: use chardev properties! */
3308 return serial_hds
[next_serial
++];
3311 QemuOptsList qemu_chardev_opts
= {
3313 .implied_opt_name
= "backend",
3314 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3318 .type
= QEMU_OPT_STRING
,
3321 .type
= QEMU_OPT_STRING
,
3324 .type
= QEMU_OPT_STRING
,
3327 .type
= QEMU_OPT_STRING
,
3329 .name
= "localaddr",
3330 .type
= QEMU_OPT_STRING
,
3332 .name
= "localport",
3333 .type
= QEMU_OPT_STRING
,
3336 .type
= QEMU_OPT_NUMBER
,
3339 .type
= QEMU_OPT_BOOL
,
3342 .type
= QEMU_OPT_BOOL
,
3345 .type
= QEMU_OPT_BOOL
,
3348 .type
= QEMU_OPT_BOOL
,
3351 .type
= QEMU_OPT_BOOL
,
3354 .type
= QEMU_OPT_BOOL
,
3357 .type
= QEMU_OPT_NUMBER
,
3360 .type
= QEMU_OPT_NUMBER
,
3363 .type
= QEMU_OPT_NUMBER
,
3366 .type
= QEMU_OPT_NUMBER
,
3369 .type
= QEMU_OPT_BOOL
,
3372 .type
= QEMU_OPT_BOOL
,
3375 .type
= QEMU_OPT_STRING
,
3378 .type
= QEMU_OPT_NUMBER
,
3381 .type
= QEMU_OPT_SIZE
,
3383 { /* end of list */ }
3389 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3394 error_setg(errp
, "input file not supported");
3398 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3399 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3400 if (out
== INVALID_HANDLE_VALUE
) {
3401 error_setg(errp
, "open %s failed", file
->out
);
3404 return qemu_chr_open_win_file(out
);
3407 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3410 return qemu_chr_open_win_path(serial
->device
);
3413 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3416 error_setg(errp
, "character device backend type 'parallel' not supported");
3422 static int qmp_chardev_open_file_source(char *src
, int flags
,
3427 TFR(fd
= qemu_open(src
, flags
, 0666));
3429 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3434 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3436 int flags
, in
= -1, out
= -1;
3438 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3439 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3440 if (error_is_set(errp
)) {
3446 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3447 if (error_is_set(errp
)) {
3453 return qemu_chr_open_fd(in
, out
);
3456 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3459 #ifdef HAVE_CHARDEV_TTY
3462 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3463 if (error_is_set(errp
)) {
3466 socket_set_nonblock(fd
);
3467 return qemu_chr_open_tty_fd(fd
);
3469 error_setg(errp
, "character device backend type 'serial' not supported");
3474 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3477 #ifdef HAVE_CHARDEV_PARPORT
3480 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3481 if (error_is_set(errp
)) {
3484 return qemu_chr_open_pp_fd(fd
);
3486 error_setg(errp
, "character device backend type 'parallel' not supported");
3493 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3496 SocketAddress
*addr
= sock
->addr
;
3497 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3498 bool is_listen
= sock
->has_server
? sock
->server
: true;
3499 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3500 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3504 fd
= socket_listen(addr
, errp
);
3506 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3508 if (error_is_set(errp
)) {
3511 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3512 is_telnet
, is_waitconnect
, errp
);
3515 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3518 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3519 CharDriverState
*chr
= NULL
;
3521 chr
= qemu_chr_find(id
);
3523 error_setg(errp
, "Chardev '%s' already exists", id
);
3528 switch (backend
->kind
) {
3529 case CHARDEV_BACKEND_KIND_FILE
:
3530 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3532 case CHARDEV_BACKEND_KIND_SERIAL
:
3533 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3535 case CHARDEV_BACKEND_KIND_PARALLEL
:
3536 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3538 case CHARDEV_BACKEND_KIND_SOCKET
:
3539 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3541 #ifdef HAVE_CHARDEV_TTY
3542 case CHARDEV_BACKEND_KIND_PTY
:
3544 /* qemu_chr_open_pty sets "path" in opts */
3546 opts
= qemu_opts_create_nofail(qemu_find_opts("chardev"));
3547 chr
= qemu_chr_open_pty(opts
);
3548 ret
->pty
= g_strdup(qemu_opt_get(opts
, "path"));
3549 ret
->has_pty
= true;
3550 qemu_opts_del(opts
);
3554 case CHARDEV_BACKEND_KIND_NULL
:
3555 chr
= qemu_chr_open_null(NULL
);
3558 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3562 if (chr
== NULL
&& !error_is_set(errp
)) {
3563 error_setg(errp
, "Failed to create chardev");
3566 chr
->label
= g_strdup(id
);
3567 chr
->avail_connections
= 1;
3568 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3576 void qmp_chardev_remove(const char *id
, Error
**errp
)
3578 CharDriverState
*chr
;
3580 chr
= qemu_chr_find(id
);
3582 error_setg(errp
, "Chardev '%s' not found", id
);
3585 if (chr
->chr_can_read
|| chr
->chr_read
||
3586 chr
->chr_event
|| chr
->handler_opaque
) {
3587 error_setg(errp
, "Chardev '%s' is busy", id
);
3590 qemu_chr_delete(chr
);