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"
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(__GLIBC__)
58 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
63 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64 #include <dev/ppbus/ppi.h>
65 #include <dev/ppbus/ppbconf.h>
66 #elif defined(__DragonFly__)
67 #include <dev/misc/ppi/ppi.h>
68 #include <bus/ppbus/ppbconf.h>
74 #include <linux/ppdev.h>
75 #include <linux/parport.h>
79 #include <sys/ethernet.h>
80 #include <sys/sockio.h>
81 #include <netinet/arp.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_icmp.h> // must come after ip.h
86 #include <netinet/udp.h>
87 #include <netinet/tcp.h>
95 #include "qemu/sockets.h"
96 #include "ui/qemu-spice.h"
98 #define READ_BUF_LEN 4096
100 /***********************************************************/
101 /* character device */
103 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
104 QTAILQ_HEAD_INITIALIZER(chardevs
);
106 void qemu_chr_be_event(CharDriverState
*s
, int event
)
108 /* Keep track if the char device is open */
110 case CHR_EVENT_OPENED
:
113 case CHR_EVENT_CLOSED
:
120 s
->chr_event(s
->handler_opaque
, event
);
123 static gboolean
qemu_chr_generic_open_bh(gpointer opaque
)
125 CharDriverState
*s
= opaque
;
126 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
131 void qemu_chr_generic_open(CharDriverState
*s
)
133 if (s
->idle_tag
== 0) {
134 s
->idle_tag
= g_idle_add(qemu_chr_generic_open_bh
, s
);
138 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
140 return s
->chr_write(s
, buf
, len
);
143 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
147 return s
->chr_ioctl(s
, cmd
, arg
);
150 int qemu_chr_be_can_write(CharDriverState
*s
)
152 if (!s
->chr_can_read
)
154 return s
->chr_can_read(s
->handler_opaque
);
157 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
160 s
->chr_read(s
->handler_opaque
, buf
, len
);
164 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
166 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
169 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
171 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
174 void qemu_chr_accept_input(CharDriverState
*s
)
176 if (s
->chr_accept_input
)
177 s
->chr_accept_input(s
);
181 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
183 char buf
[READ_BUF_LEN
];
186 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
187 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
191 void qemu_chr_add_handlers(CharDriverState
*s
,
192 IOCanReadHandler
*fd_can_read
,
193 IOReadHandler
*fd_read
,
194 IOEventHandler
*fd_event
,
197 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
198 /* chr driver being released. */
199 ++s
->avail_connections
;
201 s
->chr_can_read
= fd_can_read
;
202 s
->chr_read
= fd_read
;
203 s
->chr_event
= fd_event
;
204 s
->handler_opaque
= opaque
;
205 if (s
->chr_update_read_handler
)
206 s
->chr_update_read_handler(s
);
208 /* We're connecting to an already opened device, so let's make sure we
209 also get the open event */
211 qemu_chr_generic_open(s
);
215 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
220 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
222 CharDriverState
*chr
;
224 chr
= g_malloc0(sizeof(CharDriverState
));
225 chr
->chr_write
= null_chr_write
;
229 /* MUX driver for serial I/O splitting */
231 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
232 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
234 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
235 IOReadHandler
*chr_read
[MAX_MUX
];
236 IOEventHandler
*chr_event
[MAX_MUX
];
237 void *ext_opaque
[MAX_MUX
];
238 CharDriverState
*drv
;
243 /* Intermediate input buffer allows to catch escape sequences even if the
244 currently active device is not accepting any input - but only until it
246 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
251 int64_t timestamps_start
;
255 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
257 MuxDriver
*d
= chr
->opaque
;
259 if (!d
->timestamps
) {
260 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
265 for (i
= 0; i
< len
; i
++) {
271 ti
= qemu_get_clock_ms(rt_clock
);
272 if (d
->timestamps_start
== -1)
273 d
->timestamps_start
= ti
;
274 ti
-= d
->timestamps_start
;
276 snprintf(buf1
, sizeof(buf1
),
277 "[%02d:%02d:%02d.%03d] ",
282 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
285 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
286 if (buf
[i
] == '\n') {
294 static const char * const mux_help
[] = {
295 "% h print this help\n\r",
296 "% x exit emulator\n\r",
297 "% s save disk data back to file (if -snapshot)\n\r",
298 "% t toggle console timestamps\n\r"
299 "% b send break (magic sysrq)\n\r",
300 "% c switch between console and monitor\n\r",
305 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
306 static void mux_print_help(CharDriverState
*chr
)
309 char ebuf
[15] = "Escape-Char";
310 char cbuf
[50] = "\n\r";
312 if (term_escape_char
> 0 && term_escape_char
< 26) {
313 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
314 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
316 snprintf(cbuf
, sizeof(cbuf
),
317 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
320 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
321 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
322 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
323 if (mux_help
[i
][j
] == '%')
324 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
326 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
331 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
333 if (d
->chr_event
[mux_nr
])
334 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
337 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
339 if (d
->term_got_escape
) {
340 d
->term_got_escape
= 0;
341 if (ch
== term_escape_char
)
350 const char *term
= "QEMU: Terminated\n\r";
351 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
359 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
362 /* Switch to the next registered device */
363 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
365 if (d
->focus
>= d
->mux_cnt
)
367 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
370 d
->timestamps
= !d
->timestamps
;
371 d
->timestamps_start
= -1;
375 } else if (ch
== term_escape_char
) {
376 d
->term_got_escape
= 1;
384 static void mux_chr_accept_input(CharDriverState
*chr
)
386 MuxDriver
*d
= chr
->opaque
;
389 while (d
->prod
[m
] != d
->cons
[m
] &&
390 d
->chr_can_read
[m
] &&
391 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
392 d
->chr_read
[m
](d
->ext_opaque
[m
],
393 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
397 static int mux_chr_can_read(void *opaque
)
399 CharDriverState
*chr
= opaque
;
400 MuxDriver
*d
= chr
->opaque
;
403 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
405 if (d
->chr_can_read
[m
])
406 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
410 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
412 CharDriverState
*chr
= opaque
;
413 MuxDriver
*d
= chr
->opaque
;
417 mux_chr_accept_input (opaque
);
419 for(i
= 0; i
< size
; i
++)
420 if (mux_proc_byte(chr
, d
, buf
[i
])) {
421 if (d
->prod
[m
] == d
->cons
[m
] &&
422 d
->chr_can_read
[m
] &&
423 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
424 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
426 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
430 static void mux_chr_event(void *opaque
, int event
)
432 CharDriverState
*chr
= opaque
;
433 MuxDriver
*d
= chr
->opaque
;
436 /* Send the event to all registered listeners */
437 for (i
= 0; i
< d
->mux_cnt
; i
++)
438 mux_chr_send_event(d
, i
, event
);
441 static void mux_chr_update_read_handler(CharDriverState
*chr
)
443 MuxDriver
*d
= chr
->opaque
;
445 if (d
->mux_cnt
>= MAX_MUX
) {
446 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
449 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
450 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
451 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
452 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
453 /* Fix up the real driver with mux routines */
454 if (d
->mux_cnt
== 0) {
455 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
458 if (d
->focus
!= -1) {
459 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
461 d
->focus
= d
->mux_cnt
;
463 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
466 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
468 CharDriverState
*chr
;
471 chr
= g_malloc0(sizeof(CharDriverState
));
472 d
= g_malloc0(sizeof(MuxDriver
));
477 chr
->chr_write
= mux_chr_write
;
478 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
479 chr
->chr_accept_input
= mux_chr_accept_input
;
480 /* Frontend guest-open / -close notification is not support with muxes */
481 chr
->chr_guest_open
= NULL
;
482 chr
->chr_guest_close
= NULL
;
484 /* Muxes are always open on creation */
485 qemu_chr_generic_open(chr
);
492 int send_all(int fd
, const void *buf
, int len1
)
498 ret
= send(fd
, buf
, len
, 0);
500 errno
= WSAGetLastError();
501 if (errno
!= WSAEWOULDBLOCK
) {
504 } else if (ret
== 0) {
516 int send_all(int fd
, const void *_buf
, int len1
)
519 const uint8_t *buf
= _buf
;
523 ret
= write(fd
, buf
, len
);
525 if (errno
!= EINTR
&& errno
!= EAGAIN
)
527 } else if (ret
== 0) {
540 typedef struct IOWatchPoll
545 IOCanReadHandler
*fd_can_read
;
548 QTAILQ_ENTRY(IOWatchPoll
) node
;
551 static QTAILQ_HEAD(, IOWatchPoll
) io_watch_poll_list
=
552 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list
);
554 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
558 QTAILQ_FOREACH(i
, &io_watch_poll_list
, node
) {
559 if (i
->src
== source
) {
567 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
569 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
571 iwp
->max_size
= iwp
->fd_can_read(iwp
->opaque
);
572 if (iwp
->max_size
== 0) {
576 return g_io_watch_funcs
.prepare(source
, timeout_
);
579 static gboolean
io_watch_poll_check(GSource
*source
)
581 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
583 if (iwp
->max_size
== 0) {
587 return g_io_watch_funcs
.check(source
);
590 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
593 return g_io_watch_funcs
.dispatch(source
, callback
, user_data
);
596 static void io_watch_poll_finalize(GSource
*source
)
598 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
599 QTAILQ_REMOVE(&io_watch_poll_list
, iwp
, node
);
600 g_io_watch_funcs
.finalize(source
);
603 static GSourceFuncs io_watch_poll_funcs
= {
604 .prepare
= io_watch_poll_prepare
,
605 .check
= io_watch_poll_check
,
606 .dispatch
= io_watch_poll_dispatch
,
607 .finalize
= io_watch_poll_finalize
,
610 /* Can only be used for read */
611 static guint
io_add_watch_poll(GIOChannel
*channel
,
612 IOCanReadHandler
*fd_can_read
,
620 src
= g_io_create_watch(channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
621 g_source_set_funcs(src
, &io_watch_poll_funcs
);
622 g_source_set_callback(src
, (GSourceFunc
)fd_read
, user_data
, NULL
);
623 tag
= g_source_attach(src
, NULL
);
626 iwp
= g_malloc0(sizeof(*iwp
));
629 iwp
->fd_can_read
= fd_can_read
;
630 iwp
->opaque
= user_data
;
632 QTAILQ_INSERT_HEAD(&io_watch_poll_list
, iwp
, node
);
637 static GIOChannel
*io_channel_from_fd(int fd
)
645 chan
= g_io_channel_unix_new(fd
);
647 g_io_channel_set_encoding(chan
, NULL
, NULL
);
648 g_io_channel_set_buffered(chan
, FALSE
);
653 static GIOChannel
*io_channel_from_socket(int fd
)
662 chan
= g_io_channel_win32_new_socket(fd
);
664 chan
= g_io_channel_unix_new(fd
);
667 g_io_channel_set_encoding(chan
, NULL
, NULL
);
668 g_io_channel_set_buffered(chan
, FALSE
);
673 static int io_channel_send_all(GIOChannel
*fd
, const void *_buf
, int len1
)
678 const uint8_t *buf
= _buf
;
682 status
= g_io_channel_write_chars(fd
, (const gchar
*)buf
, len
,
683 &bytes_written
, NULL
);
684 if (status
!= G_IO_STATUS_NORMAL
) {
685 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 GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
758 FDCharDriver
*s
= chr
->opaque
;
759 return g_io_create_watch(s
->fd_out
, cond
);
762 static void fd_chr_update_read_handler(CharDriverState
*chr
)
764 FDCharDriver
*s
= chr
->opaque
;
767 g_source_remove(s
->fd_in_tag
);
771 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
775 static void fd_chr_close(struct CharDriverState
*chr
)
777 FDCharDriver
*s
= chr
->opaque
;
780 g_source_remove(s
->fd_in_tag
);
785 g_io_channel_unref(s
->fd_in
);
788 g_io_channel_unref(s
->fd_out
);
792 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
795 /* open a character device to a unix fd */
796 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
798 CharDriverState
*chr
;
801 chr
= g_malloc0(sizeof(CharDriverState
));
802 s
= g_malloc0(sizeof(FDCharDriver
));
803 s
->fd_in
= io_channel_from_fd(fd_in
);
804 s
->fd_out
= io_channel_from_fd(fd_out
);
805 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
808 chr
->chr_add_watch
= fd_chr_add_watch
;
809 chr
->chr_write
= fd_chr_write
;
810 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
811 chr
->chr_close
= fd_chr_close
;
813 qemu_chr_generic_open(chr
);
818 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
822 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
823 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
827 return qemu_chr_open_fd(-1, fd_out
);
830 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
833 char filename_in
[256], filename_out
[256];
834 const char *filename
= qemu_opt_get(opts
, "path");
836 if (filename
== NULL
) {
837 fprintf(stderr
, "chardev: pipe: no filename given\n");
841 snprintf(filename_in
, 256, "%s.in", filename
);
842 snprintf(filename_out
, 256, "%s.out", filename
);
843 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
844 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
845 if (fd_in
< 0 || fd_out
< 0) {
850 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
855 return qemu_chr_open_fd(fd_in
, fd_out
);
858 /* init terminal so that we can grab keys */
859 static struct termios oldtty
;
860 static int old_fd0_flags
;
861 static bool stdio_allow_signal
;
863 static void term_exit(void)
865 tcsetattr (0, TCSANOW
, &oldtty
);
866 fcntl(0, F_SETFL
, old_fd0_flags
);
869 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
875 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
876 |INLCR
|IGNCR
|ICRNL
|IXON
);
877 tty
.c_oflag
|= OPOST
;
878 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
879 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
884 /* if graphical mode, we allow Ctrl-C handling */
885 if (!stdio_allow_signal
)
886 tty
.c_lflag
&= ~ISIG
;
888 tcsetattr (0, TCSANOW
, &tty
);
891 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
897 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
899 CharDriverState
*chr
;
901 if (is_daemonized()) {
902 error_report("cannot use stdio with -daemonize");
905 old_fd0_flags
= fcntl(0, F_GETFL
);
906 tcgetattr (0, &oldtty
);
907 fcntl(0, F_SETFL
, O_NONBLOCK
);
910 chr
= qemu_chr_open_fd(0, 1);
911 chr
->chr_close
= qemu_chr_close_stdio
;
912 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
913 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
914 display_type
!= DT_NOGRAPHIC
);
915 qemu_chr_fe_set_echo(chr
, false);
921 /* Once Solaris has openpty(), this is going to be removed. */
922 static int openpty(int *amaster
, int *aslave
, char *name
,
923 struct termios
*termp
, struct winsize
*winp
)
926 int mfd
= -1, sfd
= -1;
928 *amaster
= *aslave
= -1;
930 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
934 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
937 if ((slave
= ptsname(mfd
)) == NULL
)
940 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
943 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
944 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
952 ioctl(sfd
, TIOCSWINSZ
, winp
);
963 static void cfmakeraw (struct termios
*termios_p
)
965 termios_p
->c_iflag
&=
966 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
967 termios_p
->c_oflag
&= ~OPOST
;
968 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
969 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
970 termios_p
->c_cflag
|= CS8
;
972 termios_p
->c_cc
[VMIN
] = 0;
973 termios_p
->c_cc
[VTIME
] = 0;
977 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
978 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
979 || defined(__GLIBC__)
981 #define HAVE_CHARDEV_TTY 1
992 static void pty_chr_update_read_handler(CharDriverState
*chr
);
993 static void pty_chr_state(CharDriverState
*chr
, int connected
);
995 static gboolean
pty_chr_timer(gpointer opaque
)
997 struct CharDriverState
*chr
= opaque
;
998 PtyCharDriver
*s
= chr
->opaque
;
1004 /* If we arrive here without polling being cleared due
1005 * read returning -EIO, then we are (re-)connected */
1006 pty_chr_state(chr
, 1);
1011 pty_chr_update_read_handler(chr
);
1017 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1019 PtyCharDriver
*s
= chr
->opaque
;
1022 g_source_remove(s
->timer_tag
);
1027 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1029 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1033 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1035 PtyCharDriver
*s
= chr
->opaque
;
1037 if (!s
->connected
) {
1038 /* guest sends data, check for (re-)connect */
1039 pty_chr_update_read_handler(chr
);
1042 return io_channel_send_all(s
->fd
, buf
, len
);
1045 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1047 PtyCharDriver
*s
= chr
->opaque
;
1048 return g_io_create_watch(s
->fd
, cond
);
1051 static int pty_chr_read_poll(void *opaque
)
1053 CharDriverState
*chr
= opaque
;
1054 PtyCharDriver
*s
= chr
->opaque
;
1056 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1057 return s
->read_bytes
;
1060 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1062 CharDriverState
*chr
= opaque
;
1063 PtyCharDriver
*s
= chr
->opaque
;
1065 uint8_t buf
[READ_BUF_LEN
];
1069 if (len
> s
->read_bytes
)
1070 len
= s
->read_bytes
;
1073 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1074 if (status
!= G_IO_STATUS_NORMAL
) {
1075 pty_chr_state(chr
, 0);
1078 pty_chr_state(chr
, 1);
1079 qemu_chr_be_write(chr
, buf
, size
);
1084 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1086 PtyCharDriver
*s
= chr
->opaque
;
1089 g_source_remove(s
->fd_tag
);
1092 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1095 * Short timeout here: just need wait long enougth that qemu makes
1096 * it through the poll loop once. When reconnected we want a
1097 * short timeout so we notice it almost instantly. Otherwise
1098 * read() gives us -EIO instantly, making pty_chr_state() reset the
1099 * timeout to the normal (much longer) poll interval before the
1102 pty_chr_rearm_timer(chr
, 10);
1105 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1107 PtyCharDriver
*s
= chr
->opaque
;
1110 g_source_remove(s
->fd_tag
);
1114 /* (re-)connect poll interval for idle guests: once per second.
1115 * We check more frequently in case the guests sends data to
1116 * the virtual device linked to our pty. */
1117 pty_chr_rearm_timer(chr
, 1000);
1120 qemu_chr_generic_open(chr
);
1126 static void pty_chr_close(struct CharDriverState
*chr
)
1128 PtyCharDriver
*s
= chr
->opaque
;
1132 g_source_remove(s
->fd_tag
);
1134 fd
= g_io_channel_unix_get_fd(s
->fd
);
1135 g_io_channel_unref(s
->fd
);
1138 g_source_remove(s
->timer_tag
);
1141 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1144 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1146 CharDriverState
*chr
;
1150 int master_fd
, slave_fd
, len
;
1151 #if defined(__OpenBSD__) || defined(__DragonFly__)
1152 char pty_name
[PATH_MAX
];
1153 #define q_ptsname(x) pty_name
1155 char *pty_name
= NULL
;
1156 #define q_ptsname(x) ptsname(x)
1159 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1163 /* Set raw attributes on the pty. */
1164 tcgetattr(slave_fd
, &tty
);
1166 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1169 chr
= g_malloc0(sizeof(CharDriverState
));
1171 len
= strlen(q_ptsname(master_fd
)) + 5;
1172 chr
->filename
= g_malloc(len
);
1173 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
1174 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
1176 label
= qemu_opts_id(opts
);
1177 fprintf(stderr
, "char device redirected to %s%s%s%s\n",
1178 q_ptsname(master_fd
),
1179 label
? " (label " : "",
1183 s
= g_malloc0(sizeof(PtyCharDriver
));
1185 chr
->chr_write
= pty_chr_write
;
1186 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1187 chr
->chr_close
= pty_chr_close
;
1188 chr
->chr_add_watch
= pty_chr_add_watch
;
1190 s
->fd
= io_channel_from_fd(master_fd
);
1196 static void tty_serial_init(int fd
, int speed
,
1197 int parity
, int data_bits
, int stop_bits
)
1203 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1204 speed
, parity
, data_bits
, stop_bits
);
1206 tcgetattr (fd
, &tty
);
1208 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1209 speed
= speed
* 10 / 11;
1226 /* Non-Posix values follow. They may be unsupported on some systems. */
1228 check_speed(115200);
1230 check_speed(230400);
1233 check_speed(460800);
1236 check_speed(500000);
1239 check_speed(576000);
1242 check_speed(921600);
1245 check_speed(1000000);
1248 check_speed(1152000);
1251 check_speed(1500000);
1254 check_speed(2000000);
1257 check_speed(2500000);
1260 check_speed(3000000);
1263 check_speed(3500000);
1266 check_speed(4000000);
1271 cfsetispeed(&tty
, spd
);
1272 cfsetospeed(&tty
, spd
);
1274 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1275 |INLCR
|IGNCR
|ICRNL
|IXON
);
1276 tty
.c_oflag
|= OPOST
;
1277 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1278 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1299 tty
.c_cflag
|= PARENB
;
1302 tty
.c_cflag
|= PARENB
| PARODD
;
1306 tty
.c_cflag
|= CSTOPB
;
1308 tcsetattr (fd
, TCSANOW
, &tty
);
1311 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1313 FDCharDriver
*s
= chr
->opaque
;
1316 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1318 QEMUSerialSetParams
*ssp
= arg
;
1319 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1320 ssp
->speed
, ssp
->parity
,
1321 ssp
->data_bits
, ssp
->stop_bits
);
1324 case CHR_IOCTL_SERIAL_SET_BREAK
:
1326 int enable
= *(int *)arg
;
1328 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1332 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1335 int *targ
= (int *)arg
;
1336 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1338 if (sarg
& TIOCM_CTS
)
1339 *targ
|= CHR_TIOCM_CTS
;
1340 if (sarg
& TIOCM_CAR
)
1341 *targ
|= CHR_TIOCM_CAR
;
1342 if (sarg
& TIOCM_DSR
)
1343 *targ
|= CHR_TIOCM_DSR
;
1344 if (sarg
& TIOCM_RI
)
1345 *targ
|= CHR_TIOCM_RI
;
1346 if (sarg
& TIOCM_DTR
)
1347 *targ
|= CHR_TIOCM_DTR
;
1348 if (sarg
& TIOCM_RTS
)
1349 *targ
|= CHR_TIOCM_RTS
;
1352 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1354 int sarg
= *(int *)arg
;
1356 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1357 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1358 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1359 if (sarg
& CHR_TIOCM_CTS
)
1361 if (sarg
& CHR_TIOCM_CAR
)
1363 if (sarg
& CHR_TIOCM_DSR
)
1365 if (sarg
& CHR_TIOCM_RI
)
1367 if (sarg
& CHR_TIOCM_DTR
)
1369 if (sarg
& CHR_TIOCM_RTS
)
1371 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1380 static void qemu_chr_close_tty(CharDriverState
*chr
)
1382 FDCharDriver
*s
= chr
->opaque
;
1386 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1396 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1398 CharDriverState
*chr
;
1400 tty_serial_init(fd
, 115200, 'N', 8, 1);
1401 chr
= qemu_chr_open_fd(fd
, fd
);
1402 chr
->chr_ioctl
= tty_serial_ioctl
;
1403 chr
->chr_close
= qemu_chr_close_tty
;
1407 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1409 const char *filename
= qemu_opt_get(opts
, "path");
1412 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1416 return qemu_chr_open_tty_fd(fd
);
1418 #endif /* __linux__ || __sun__ */
1420 #if defined(__linux__)
1422 #define HAVE_CHARDEV_PARPORT 1
1427 } ParallelCharDriver
;
1429 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1431 if (s
->mode
!= mode
) {
1433 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1440 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1442 ParallelCharDriver
*drv
= chr
->opaque
;
1447 case CHR_IOCTL_PP_READ_DATA
:
1448 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1450 *(uint8_t *)arg
= b
;
1452 case CHR_IOCTL_PP_WRITE_DATA
:
1453 b
= *(uint8_t *)arg
;
1454 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1457 case CHR_IOCTL_PP_READ_CONTROL
:
1458 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1460 /* Linux gives only the lowest bits, and no way to know data
1461 direction! For better compatibility set the fixed upper
1463 *(uint8_t *)arg
= b
| 0xc0;
1465 case CHR_IOCTL_PP_WRITE_CONTROL
:
1466 b
= *(uint8_t *)arg
;
1467 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1470 case CHR_IOCTL_PP_READ_STATUS
:
1471 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1473 *(uint8_t *)arg
= b
;
1475 case CHR_IOCTL_PP_DATA_DIR
:
1476 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1479 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1480 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1481 struct ParallelIOArg
*parg
= arg
;
1482 int n
= read(fd
, parg
->buffer
, parg
->count
);
1483 if (n
!= parg
->count
) {
1488 case CHR_IOCTL_PP_EPP_READ
:
1489 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1490 struct ParallelIOArg
*parg
= arg
;
1491 int n
= read(fd
, parg
->buffer
, parg
->count
);
1492 if (n
!= parg
->count
) {
1497 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1498 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1499 struct ParallelIOArg
*parg
= arg
;
1500 int n
= write(fd
, parg
->buffer
, parg
->count
);
1501 if (n
!= parg
->count
) {
1506 case CHR_IOCTL_PP_EPP_WRITE
:
1507 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1508 struct ParallelIOArg
*parg
= arg
;
1509 int n
= write(fd
, parg
->buffer
, parg
->count
);
1510 if (n
!= parg
->count
) {
1521 static void pp_close(CharDriverState
*chr
)
1523 ParallelCharDriver
*drv
= chr
->opaque
;
1526 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1527 ioctl(fd
, PPRELEASE
);
1530 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1533 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1535 CharDriverState
*chr
;
1536 ParallelCharDriver
*drv
;
1538 if (ioctl(fd
, PPCLAIM
) < 0) {
1543 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1545 drv
->mode
= IEEE1284_MODE_COMPAT
;
1547 chr
= g_malloc0(sizeof(CharDriverState
));
1548 chr
->chr_write
= null_chr_write
;
1549 chr
->chr_ioctl
= pp_ioctl
;
1550 chr
->chr_close
= pp_close
;
1553 qemu_chr_generic_open(chr
);
1557 #endif /* __linux__ */
1559 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1561 #define HAVE_CHARDEV_PARPORT 1
1563 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1565 int fd
= (int)(intptr_t)chr
->opaque
;
1569 case CHR_IOCTL_PP_READ_DATA
:
1570 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1572 *(uint8_t *)arg
= b
;
1574 case CHR_IOCTL_PP_WRITE_DATA
:
1575 b
= *(uint8_t *)arg
;
1576 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1579 case CHR_IOCTL_PP_READ_CONTROL
:
1580 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1582 *(uint8_t *)arg
= b
;
1584 case CHR_IOCTL_PP_WRITE_CONTROL
:
1585 b
= *(uint8_t *)arg
;
1586 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1589 case CHR_IOCTL_PP_READ_STATUS
:
1590 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1592 *(uint8_t *)arg
= b
;
1600 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1602 CharDriverState
*chr
;
1604 chr
= g_malloc0(sizeof(CharDriverState
));
1605 chr
->opaque
= (void *)(intptr_t)fd
;
1606 chr
->chr_write
= null_chr_write
;
1607 chr
->chr_ioctl
= pp_ioctl
;
1616 HANDLE hcom
, hrecv
, hsend
;
1617 OVERLAPPED orecv
, osend
;
1624 HANDLE hInputReadyEvent
;
1625 HANDLE hInputDoneEvent
;
1626 HANDLE hInputThread
;
1627 uint8_t win_stdio_buf
;
1628 } WinStdioCharState
;
1630 #define NSENDBUF 2048
1631 #define NRECVBUF 2048
1632 #define MAXCONNECT 1
1633 #define NTIMEOUT 5000
1635 static int win_chr_poll(void *opaque
);
1636 static int win_chr_pipe_poll(void *opaque
);
1638 static void win_chr_close(CharDriverState
*chr
)
1640 WinCharState
*s
= chr
->opaque
;
1643 CloseHandle(s
->hsend
);
1647 CloseHandle(s
->hrecv
);
1651 CloseHandle(s
->hcom
);
1655 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1657 qemu_del_polling_cb(win_chr_poll
, chr
);
1659 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1662 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1664 WinCharState
*s
= chr
->opaque
;
1666 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1671 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1673 fprintf(stderr
, "Failed CreateEvent\n");
1676 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1678 fprintf(stderr
, "Failed CreateEvent\n");
1682 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1683 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1684 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1685 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1690 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1691 fprintf(stderr
, "Failed SetupComm\n");
1695 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1696 size
= sizeof(COMMCONFIG
);
1697 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1698 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1699 CommConfigDialog(filename
, NULL
, &comcfg
);
1701 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1702 fprintf(stderr
, "Failed SetCommState\n");
1706 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1707 fprintf(stderr
, "Failed SetCommMask\n");
1711 cto
.ReadIntervalTimeout
= MAXDWORD
;
1712 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1713 fprintf(stderr
, "Failed SetCommTimeouts\n");
1717 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1718 fprintf(stderr
, "Failed ClearCommError\n");
1721 qemu_add_polling_cb(win_chr_poll
, chr
);
1729 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1731 WinCharState
*s
= chr
->opaque
;
1732 DWORD len
, ret
, size
, err
;
1735 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1736 s
->osend
.hEvent
= s
->hsend
;
1739 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1741 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1743 err
= GetLastError();
1744 if (err
== ERROR_IO_PENDING
) {
1745 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1763 static int win_chr_read_poll(CharDriverState
*chr
)
1765 WinCharState
*s
= chr
->opaque
;
1767 s
->max_size
= qemu_chr_be_can_write(chr
);
1771 static void win_chr_readfile(CharDriverState
*chr
)
1773 WinCharState
*s
= chr
->opaque
;
1775 uint8_t buf
[READ_BUF_LEN
];
1778 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1779 s
->orecv
.hEvent
= s
->hrecv
;
1780 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1782 err
= GetLastError();
1783 if (err
== ERROR_IO_PENDING
) {
1784 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1789 qemu_chr_be_write(chr
, buf
, size
);
1793 static void win_chr_read(CharDriverState
*chr
)
1795 WinCharState
*s
= chr
->opaque
;
1797 if (s
->len
> s
->max_size
)
1798 s
->len
= s
->max_size
;
1802 win_chr_readfile(chr
);
1805 static int win_chr_poll(void *opaque
)
1807 CharDriverState
*chr
= opaque
;
1808 WinCharState
*s
= chr
->opaque
;
1812 ClearCommError(s
->hcom
, &comerr
, &status
);
1813 if (status
.cbInQue
> 0) {
1814 s
->len
= status
.cbInQue
;
1815 win_chr_read_poll(chr
);
1822 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1824 CharDriverState
*chr
;
1827 chr
= g_malloc0(sizeof(CharDriverState
));
1828 s
= g_malloc0(sizeof(WinCharState
));
1830 chr
->chr_write
= win_chr_write
;
1831 chr
->chr_close
= win_chr_close
;
1833 if (win_chr_init(chr
, filename
) < 0) {
1838 qemu_chr_generic_open(chr
);
1842 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1844 return qemu_chr_open_win_path(qemu_opt_get(opts
, "path"));
1847 static int win_chr_pipe_poll(void *opaque
)
1849 CharDriverState
*chr
= opaque
;
1850 WinCharState
*s
= chr
->opaque
;
1853 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1856 win_chr_read_poll(chr
);
1863 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1865 WinCharState
*s
= chr
->opaque
;
1873 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1875 fprintf(stderr
, "Failed CreateEvent\n");
1878 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1880 fprintf(stderr
, "Failed CreateEvent\n");
1884 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1885 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1886 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1888 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1889 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1890 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1895 ZeroMemory(&ov
, sizeof(ov
));
1896 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1897 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1899 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1903 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1905 fprintf(stderr
, "Failed GetOverlappedResult\n");
1907 CloseHandle(ov
.hEvent
);
1914 CloseHandle(ov
.hEvent
);
1917 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1926 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1928 const char *filename
= qemu_opt_get(opts
, "path");
1929 CharDriverState
*chr
;
1932 chr
= g_malloc0(sizeof(CharDriverState
));
1933 s
= g_malloc0(sizeof(WinCharState
));
1935 chr
->chr_write
= win_chr_write
;
1936 chr
->chr_close
= win_chr_close
;
1938 if (win_chr_pipe_init(chr
, filename
) < 0) {
1943 qemu_chr_generic_open(chr
);
1947 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1949 CharDriverState
*chr
;
1952 chr
= g_malloc0(sizeof(CharDriverState
));
1953 s
= g_malloc0(sizeof(WinCharState
));
1956 chr
->chr_write
= win_chr_write
;
1957 qemu_chr_generic_open(chr
);
1961 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1963 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1966 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1968 const char *file_out
= qemu_opt_get(opts
, "path");
1971 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1972 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1973 if (fd_out
== INVALID_HANDLE_VALUE
) {
1977 return qemu_chr_open_win_file(fd_out
);
1980 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1982 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1989 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1999 static void win_stdio_wait_func(void *opaque
)
2001 CharDriverState
*chr
= opaque
;
2002 WinStdioCharState
*stdio
= chr
->opaque
;
2003 INPUT_RECORD buf
[4];
2008 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
2012 /* Avoid error storm */
2013 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2017 for (i
= 0; i
< dwSize
; i
++) {
2018 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2020 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2022 if (kev
->uChar
.AsciiChar
!= 0) {
2023 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2024 if (qemu_chr_be_can_write(chr
)) {
2025 uint8_t c
= kev
->uChar
.AsciiChar
;
2026 qemu_chr_be_write(chr
, &c
, 1);
2034 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2036 CharDriverState
*chr
= param
;
2037 WinStdioCharState
*stdio
= chr
->opaque
;
2043 /* Wait for one byte */
2044 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2046 /* Exit in case of error, continue if nothing read */
2054 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2055 if (stdio
->win_stdio_buf
== '\r') {
2059 /* Signal the main thread and wait until the byte was eaten */
2060 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2063 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2069 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2073 static void win_stdio_thread_wait_func(void *opaque
)
2075 CharDriverState
*chr
= opaque
;
2076 WinStdioCharState
*stdio
= chr
->opaque
;
2078 if (qemu_chr_be_can_write(chr
)) {
2079 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2082 SetEvent(stdio
->hInputDoneEvent
);
2085 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2087 WinStdioCharState
*stdio
= chr
->opaque
;
2090 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2093 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2095 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2099 static void win_stdio_close(CharDriverState
*chr
)
2101 WinStdioCharState
*stdio
= chr
->opaque
;
2103 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2104 CloseHandle(stdio
->hInputReadyEvent
);
2106 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2107 CloseHandle(stdio
->hInputDoneEvent
);
2109 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2110 TerminateThread(stdio
->hInputThread
, 0);
2113 g_free(chr
->opaque
);
2117 static CharDriverState
*qemu_chr_open_win_stdio(QemuOpts
*opts
)
2119 CharDriverState
*chr
;
2120 WinStdioCharState
*stdio
;
2124 chr
= g_malloc0(sizeof(CharDriverState
));
2125 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2127 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2128 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2129 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2133 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2135 chr
->opaque
= stdio
;
2136 chr
->chr_write
= win_stdio_write
;
2137 chr
->chr_close
= win_stdio_close
;
2140 if (qemu_add_wait_object(stdio
->hStdIn
,
2141 win_stdio_wait_func
, chr
)) {
2142 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2147 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2148 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2149 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2152 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2153 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2154 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2155 fprintf(stderr
, "cannot create stdio thread or event\n");
2158 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2159 win_stdio_thread_wait_func
, chr
)) {
2160 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2164 dwMode
|= ENABLE_LINE_INPUT
;
2167 /* set the terminal in raw mode */
2168 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2169 dwMode
|= ENABLE_PROCESSED_INPUT
;
2172 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2174 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2175 qemu_chr_fe_set_echo(chr
, false);
2179 #endif /* !_WIN32 */
2182 /***********************************************************/
2183 /* UDP Net console */
2189 uint8_t buf
[READ_BUF_LEN
];
2195 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2197 NetCharDriver
*s
= chr
->opaque
;
2198 gsize bytes_written
;
2201 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2202 if (status
== G_IO_STATUS_EOF
) {
2204 } else if (status
!= G_IO_STATUS_NORMAL
) {
2208 return bytes_written
;
2211 static int udp_chr_read_poll(void *opaque
)
2213 CharDriverState
*chr
= opaque
;
2214 NetCharDriver
*s
= chr
->opaque
;
2216 s
->max_size
= qemu_chr_be_can_write(chr
);
2218 /* If there were any stray characters in the queue process them
2221 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2222 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2224 s
->max_size
= qemu_chr_be_can_write(chr
);
2229 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2231 CharDriverState
*chr
= opaque
;
2232 NetCharDriver
*s
= chr
->opaque
;
2233 gsize bytes_read
= 0;
2236 if (s
->max_size
== 0)
2238 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2240 s
->bufcnt
= bytes_read
;
2241 s
->bufptr
= s
->bufcnt
;
2242 if (status
!= G_IO_STATUS_NORMAL
) {
2247 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2248 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2250 s
->max_size
= qemu_chr_be_can_write(chr
);
2256 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2258 NetCharDriver
*s
= chr
->opaque
;
2261 g_source_remove(s
->tag
);
2266 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2270 static void udp_chr_close(CharDriverState
*chr
)
2272 NetCharDriver
*s
= chr
->opaque
;
2274 g_source_remove(s
->tag
);
2277 g_io_channel_unref(s
->chan
);
2281 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2284 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2286 CharDriverState
*chr
= NULL
;
2287 NetCharDriver
*s
= NULL
;
2288 Error
*local_err
= NULL
;
2291 chr
= g_malloc0(sizeof(CharDriverState
));
2292 s
= g_malloc0(sizeof(NetCharDriver
));
2294 fd
= inet_dgram_opts(opts
, &local_err
);
2300 s
->chan
= io_channel_from_socket(s
->fd
);
2304 chr
->chr_write
= udp_chr_write
;
2305 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2306 chr
->chr_close
= udp_chr_close
;
2311 qerror_report_err(local_err
);
2312 error_free(local_err
);
2322 /***********************************************************/
2323 /* TCP Net console */
2327 GIOChannel
*chan
, *listen_chan
;
2328 guint tag
, listen_tag
;
2338 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2340 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2342 TCPCharDriver
*s
= chr
->opaque
;
2344 return io_channel_send_all(s
->chan
, buf
, len
);
2346 /* XXX: indicate an error ? */
2351 static int tcp_chr_read_poll(void *opaque
)
2353 CharDriverState
*chr
= opaque
;
2354 TCPCharDriver
*s
= chr
->opaque
;
2357 s
->max_size
= qemu_chr_be_can_write(chr
);
2362 #define IAC_BREAK 243
2363 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2365 uint8_t *buf
, int *size
)
2367 /* Handle any telnet client's basic IAC options to satisfy char by
2368 * char mode with no echo. All IAC options will be removed from
2369 * the buf and the do_telnetopt variable will be used to track the
2370 * state of the width of the IAC information.
2372 * IAC commands come in sets of 3 bytes with the exception of the
2373 * "IAC BREAK" command and the double IAC.
2379 for (i
= 0; i
< *size
; i
++) {
2380 if (s
->do_telnetopt
> 1) {
2381 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2382 /* Double IAC means send an IAC */
2386 s
->do_telnetopt
= 1;
2388 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2389 /* Handle IAC break commands by sending a serial break */
2390 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2395 if (s
->do_telnetopt
>= 4) {
2396 s
->do_telnetopt
= 1;
2399 if ((unsigned char)buf
[i
] == IAC
) {
2400 s
->do_telnetopt
= 2;
2411 static int tcp_get_msgfd(CharDriverState
*chr
)
2413 TCPCharDriver
*s
= chr
->opaque
;
2420 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2422 TCPCharDriver
*s
= chr
->opaque
;
2423 struct cmsghdr
*cmsg
;
2425 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2428 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2429 cmsg
->cmsg_level
!= SOL_SOCKET
||
2430 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2433 fd
= *((int *)CMSG_DATA(cmsg
));
2437 #ifndef MSG_CMSG_CLOEXEC
2438 qemu_set_cloexec(fd
);
2446 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2448 TCPCharDriver
*s
= chr
->opaque
;
2449 struct msghdr msg
= { NULL
, };
2450 struct iovec iov
[1];
2452 struct cmsghdr cmsg
;
2453 char control
[CMSG_SPACE(sizeof(int))];
2458 iov
[0].iov_base
= buf
;
2459 iov
[0].iov_len
= len
;
2463 msg
.msg_control
= &msg_control
;
2464 msg
.msg_controllen
= sizeof(msg_control
);
2466 #ifdef MSG_CMSG_CLOEXEC
2467 flags
|= MSG_CMSG_CLOEXEC
;
2469 ret
= recvmsg(s
->fd
, &msg
, flags
);
2470 if (ret
> 0 && s
->is_unix
) {
2471 unix_process_msgfd(chr
, &msg
);
2477 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2479 TCPCharDriver
*s
= chr
->opaque
;
2480 return qemu_recv(s
->fd
, buf
, len
, 0);
2484 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2486 TCPCharDriver
*s
= chr
->opaque
;
2487 return g_io_create_watch(s
->chan
, cond
);
2490 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2492 CharDriverState
*chr
= opaque
;
2493 TCPCharDriver
*s
= chr
->opaque
;
2494 uint8_t buf
[READ_BUF_LEN
];
2497 if (!s
->connected
|| s
->max_size
<= 0) {
2501 if (len
> s
->max_size
)
2503 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2505 /* connection closed */
2507 if (s
->listen_chan
) {
2508 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2510 g_source_remove(s
->tag
);
2512 g_io_channel_unref(s
->chan
);
2516 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2517 } else if (size
> 0) {
2518 if (s
->do_telnetopt
)
2519 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2521 qemu_chr_be_write(chr
, buf
, size
);
2528 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2530 return qemu_chr_open_fd(eventfd
, eventfd
);
2534 static void tcp_chr_connect(void *opaque
)
2536 CharDriverState
*chr
= opaque
;
2537 TCPCharDriver
*s
= chr
->opaque
;
2541 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2543 qemu_chr_generic_open(chr
);
2546 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2547 static void tcp_chr_telnet_init(int fd
)
2550 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2551 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2552 send(fd
, (char *)buf
, 3, 0);
2553 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2554 send(fd
, (char *)buf
, 3, 0);
2555 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2556 send(fd
, (char *)buf
, 3, 0);
2557 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2558 send(fd
, (char *)buf
, 3, 0);
2561 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2563 TCPCharDriver
*s
= chr
->opaque
;
2567 socket_set_nonblock(fd
);
2569 socket_set_nodelay(fd
);
2571 s
->chan
= io_channel_from_socket(fd
);
2572 g_source_remove(s
->listen_tag
);
2574 tcp_chr_connect(chr
);
2579 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2581 CharDriverState
*chr
= opaque
;
2582 TCPCharDriver
*s
= chr
->opaque
;
2583 struct sockaddr_in saddr
;
2585 struct sockaddr_un uaddr
;
2587 struct sockaddr
*addr
;
2594 len
= sizeof(uaddr
);
2595 addr
= (struct sockaddr
*)&uaddr
;
2599 len
= sizeof(saddr
);
2600 addr
= (struct sockaddr
*)&saddr
;
2602 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2603 if (fd
< 0 && errno
!= EINTR
) {
2605 } else if (fd
>= 0) {
2606 if (s
->do_telnetopt
)
2607 tcp_chr_telnet_init(fd
);
2611 if (tcp_chr_add_client(chr
, fd
) < 0)
2617 static void tcp_chr_close(CharDriverState
*chr
)
2619 TCPCharDriver
*s
= chr
->opaque
;
2622 g_source_remove(s
->tag
);
2625 g_io_channel_unref(s
->chan
);
2629 if (s
->listen_fd
>= 0) {
2630 if (s
->listen_tag
) {
2631 g_source_remove(s
->listen_tag
);
2633 if (s
->listen_chan
) {
2634 g_io_channel_unref(s
->listen_chan
);
2636 closesocket(s
->listen_fd
);
2639 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2642 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2643 bool is_listen
, bool is_telnet
,
2644 bool is_waitconnect
,
2647 CharDriverState
*chr
= NULL
;
2648 TCPCharDriver
*s
= NULL
;
2649 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2650 const char *left
= "", *right
= "";
2651 struct sockaddr_storage ss
;
2652 socklen_t ss_len
= sizeof(ss
);
2654 memset(&ss
, 0, ss_len
);
2655 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2656 error_setg(errp
, "getsockname: %s", strerror(errno
));
2660 chr
= g_malloc0(sizeof(CharDriverState
));
2661 s
= g_malloc0(sizeof(TCPCharDriver
));
2668 chr
->filename
= g_malloc(256);
2669 switch (ss
.ss_family
) {
2673 snprintf(chr
->filename
, 256, "unix:%s%s",
2674 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2675 is_listen
? ",server" : "");
2683 s
->do_nodelay
= do_nodelay
;
2684 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2685 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2686 snprintf(chr
->filename
, 256, "%s:%s:%s%s%s%s",
2687 is_telnet
? "telnet" : "tcp",
2688 left
, host
, right
, serv
,
2689 is_listen
? ",server" : "");
2694 chr
->chr_write
= tcp_chr_write
;
2695 chr
->chr_close
= tcp_chr_close
;
2696 chr
->get_msgfd
= tcp_get_msgfd
;
2697 chr
->chr_add_client
= tcp_chr_add_client
;
2698 chr
->chr_add_watch
= tcp_chr_add_watch
;
2702 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2703 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2705 s
->do_telnetopt
= 1;
2710 socket_set_nodelay(fd
);
2711 s
->chan
= io_channel_from_socket(s
->fd
);
2712 tcp_chr_connect(chr
);
2715 if (is_listen
&& is_waitconnect
) {
2716 printf("QEMU waiting for connection on: %s\n",
2718 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2719 socket_set_nonblock(s
->listen_fd
);
2724 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2726 CharDriverState
*chr
= NULL
;
2727 Error
*local_err
= NULL
;
2735 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2736 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2737 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2738 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2739 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2745 fd
= unix_listen_opts(opts
, &local_err
);
2747 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2751 fd
= inet_listen_opts(opts
, 0, &local_err
);
2753 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2760 if (!is_waitconnect
)
2761 socket_set_nonblock(fd
);
2763 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2764 is_waitconnect
, &local_err
);
2765 if (error_is_set(&local_err
)) {
2773 qerror_report_err(local_err
);
2774 error_free(local_err
);
2780 g_free(chr
->opaque
);
2786 /***********************************************************/
2787 /* Memory chardev */
2790 size_t outbuf_capacity
;
2794 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2796 MemoryDriver
*d
= chr
->opaque
;
2798 /* TODO: the QString implementation has the same code, we should
2799 * introduce a generic way to do this in cutils.c */
2800 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2802 d
->outbuf_capacity
+= len
;
2803 d
->outbuf_capacity
*= 2;
2804 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2807 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2808 d
->outbuf_size
+= len
;
2813 void qemu_chr_init_mem(CharDriverState
*chr
)
2817 d
= g_malloc(sizeof(*d
));
2819 d
->outbuf_capacity
= 4096;
2820 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2822 memset(chr
, 0, sizeof(*chr
));
2824 chr
->chr_write
= mem_chr_write
;
2827 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2829 MemoryDriver
*d
= chr
->opaque
;
2830 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2833 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2834 void qemu_chr_close_mem(CharDriverState
*chr
)
2836 MemoryDriver
*d
= chr
->opaque
;
2839 g_free(chr
->opaque
);
2841 chr
->chr_write
= NULL
;
2844 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2846 const MemoryDriver
*d
= chr
->opaque
;
2847 return d
->outbuf_size
;
2850 /*********************************************************/
2851 /* Ring buffer chardev */
2858 } RingBufCharDriver
;
2860 static size_t ringbuf_count(const CharDriverState
*chr
)
2862 const RingBufCharDriver
*d
= chr
->opaque
;
2864 return d
->prod
- d
->cons
;
2867 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2869 RingBufCharDriver
*d
= chr
->opaque
;
2872 if (!buf
|| (len
< 0)) {
2876 for (i
= 0; i
< len
; i
++ ) {
2877 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2878 if (d
->prod
- d
->cons
> d
->size
) {
2879 d
->cons
= d
->prod
- d
->size
;
2886 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2888 RingBufCharDriver
*d
= chr
->opaque
;
2891 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2892 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2898 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2900 RingBufCharDriver
*d
= chr
->opaque
;
2907 static CharDriverState
*qemu_chr_open_ringbuf(QemuOpts
*opts
)
2909 CharDriverState
*chr
;
2910 RingBufCharDriver
*d
;
2912 chr
= g_malloc0(sizeof(CharDriverState
));
2913 d
= g_malloc(sizeof(*d
));
2915 d
->size
= qemu_opt_get_size(opts
, "size", 0);
2920 /* The size must be power of 2 */
2921 if (d
->size
& (d
->size
- 1)) {
2922 error_report("size of ringbuf device must be power of two");
2928 d
->cbuf
= g_malloc0(d
->size
);
2931 chr
->chr_write
= ringbuf_chr_write
;
2932 chr
->chr_close
= ringbuf_chr_close
;
2942 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2944 return chr
->chr_write
== ringbuf_chr_write
;
2947 void qmp_ringbuf_write(const char *device
, const char *data
,
2948 bool has_format
, enum DataFormat format
,
2951 CharDriverState
*chr
;
2952 const uint8_t *write_data
;
2956 chr
= qemu_chr_find(device
);
2958 error_setg(errp
, "Device '%s' not found", device
);
2962 if (!chr_is_ringbuf(chr
)) {
2963 error_setg(errp
,"%s is not a ringbuf device", device
);
2967 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2968 write_data
= g_base64_decode(data
, &write_count
);
2970 write_data
= (uint8_t *)data
;
2971 write_count
= strlen(data
);
2974 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2976 if (write_data
!= (uint8_t *)data
) {
2977 g_free((void *)write_data
);
2981 error_setg(errp
, "Failed to write to device %s", device
);
2986 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2987 bool has_format
, enum DataFormat format
,
2990 CharDriverState
*chr
;
2995 chr
= qemu_chr_find(device
);
2997 error_setg(errp
, "Device '%s' not found", device
);
3001 if (!chr_is_ringbuf(chr
)) {
3002 error_setg(errp
,"%s is not a ringbuf device", device
);
3007 error_setg(errp
, "size must be greater than zero");
3011 count
= ringbuf_count(chr
);
3012 size
= size
> count
? count
: size
;
3013 read_data
= g_malloc(size
+ 1);
3015 ringbuf_chr_read(chr
, read_data
, size
);
3017 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3018 data
= g_base64_encode(read_data
, size
);
3022 * FIXME should read only complete, valid UTF-8 characters up
3023 * to @size bytes. Invalid sequences should be replaced by a
3024 * suitable replacement character. Except when (and only
3025 * when) ring buffer lost characters since last read, initial
3026 * continuation characters should be dropped.
3028 read_data
[size
] = 0;
3029 data
= (char *)read_data
;
3035 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3037 char host
[65], port
[33], width
[8], height
[8];
3041 Error
*local_err
= NULL
;
3043 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3044 if (error_is_set(&local_err
)) {
3045 qerror_report_err(local_err
);
3046 error_free(local_err
);
3050 if (strstart(filename
, "mon:", &p
)) {
3052 qemu_opt_set(opts
, "mux", "on");
3055 if (strcmp(filename
, "null") == 0 ||
3056 strcmp(filename
, "pty") == 0 ||
3057 strcmp(filename
, "msmouse") == 0 ||
3058 strcmp(filename
, "braille") == 0 ||
3059 strcmp(filename
, "stdio") == 0) {
3060 qemu_opt_set(opts
, "backend", filename
);
3063 if (strstart(filename
, "vc", &p
)) {
3064 qemu_opt_set(opts
, "backend", "vc");
3066 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
3068 qemu_opt_set(opts
, "width", width
);
3069 qemu_opt_set(opts
, "height", height
);
3070 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3072 qemu_opt_set(opts
, "cols", width
);
3073 qemu_opt_set(opts
, "rows", height
);
3080 if (strcmp(filename
, "con:") == 0) {
3081 qemu_opt_set(opts
, "backend", "console");
3084 if (strstart(filename
, "COM", NULL
)) {
3085 qemu_opt_set(opts
, "backend", "serial");
3086 qemu_opt_set(opts
, "path", filename
);
3089 if (strstart(filename
, "file:", &p
)) {
3090 qemu_opt_set(opts
, "backend", "file");
3091 qemu_opt_set(opts
, "path", p
);
3094 if (strstart(filename
, "pipe:", &p
)) {
3095 qemu_opt_set(opts
, "backend", "pipe");
3096 qemu_opt_set(opts
, "path", p
);
3099 if (strstart(filename
, "tcp:", &p
) ||
3100 strstart(filename
, "telnet:", &p
)) {
3101 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3103 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3106 qemu_opt_set(opts
, "backend", "socket");
3107 qemu_opt_set(opts
, "host", host
);
3108 qemu_opt_set(opts
, "port", port
);
3109 if (p
[pos
] == ',') {
3110 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3113 if (strstart(filename
, "telnet:", &p
))
3114 qemu_opt_set(opts
, "telnet", "on");
3117 if (strstart(filename
, "udp:", &p
)) {
3118 qemu_opt_set(opts
, "backend", "udp");
3119 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3121 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3125 qemu_opt_set(opts
, "host", host
);
3126 qemu_opt_set(opts
, "port", port
);
3127 if (p
[pos
] == '@') {
3129 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3131 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3135 qemu_opt_set(opts
, "localaddr", host
);
3136 qemu_opt_set(opts
, "localport", port
);
3140 if (strstart(filename
, "unix:", &p
)) {
3141 qemu_opt_set(opts
, "backend", "socket");
3142 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3146 if (strstart(filename
, "/dev/parport", NULL
) ||
3147 strstart(filename
, "/dev/ppi", NULL
)) {
3148 qemu_opt_set(opts
, "backend", "parport");
3149 qemu_opt_set(opts
, "path", filename
);
3152 if (strstart(filename
, "/dev/", NULL
)) {
3153 qemu_opt_set(opts
, "backend", "tty");
3154 qemu_opt_set(opts
, "path", filename
);
3159 qemu_opts_del(opts
);
3163 #ifdef HAVE_CHARDEV_PARPORT
3165 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
3167 const char *filename
= qemu_opt_get(opts
, "path");
3170 fd
= qemu_open(filename
, O_RDWR
);
3174 return qemu_chr_open_pp_fd(fd
);
3179 typedef struct CharDriver
{
3181 CharDriverState
*(*open
)(QemuOpts
*opts
);
3184 static GSList
*backends
;
3186 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3190 s
= g_malloc0(sizeof(*s
));
3191 s
->name
= g_strdup(name
);
3194 backends
= g_slist_append(backends
, s
);
3197 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3198 void (*init
)(struct CharDriverState
*s
),
3202 CharDriverState
*chr
;
3205 if (qemu_opts_id(opts
) == NULL
) {
3206 error_setg(errp
, "chardev: no id specified");
3210 if (qemu_opt_get(opts
, "backend") == NULL
) {
3211 error_setg(errp
, "chardev: \"%s\" missing backend",
3212 qemu_opts_id(opts
));
3215 for (i
= backends
; i
; i
= i
->next
) {
3218 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3223 error_setg(errp
, "chardev: backend \"%s\" not found",
3224 qemu_opt_get(opts
, "backend"));
3228 chr
= cd
->open(opts
);
3230 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3231 qemu_opt_get(opts
, "backend"));
3236 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3238 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3240 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3241 CharDriverState
*base
= chr
;
3242 int len
= strlen(qemu_opts_id(opts
)) + 6;
3243 base
->label
= g_malloc(len
);
3244 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3245 chr
= qemu_chr_open_mux(base
);
3246 chr
->filename
= base
->filename
;
3247 chr
->avail_connections
= MAX_MUX
;
3248 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3250 chr
->avail_connections
= 1;
3252 chr
->label
= g_strdup(qemu_opts_id(opts
));
3257 qemu_opts_del(opts
);
3261 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3264 CharDriverState
*chr
;
3268 if (strstart(filename
, "chardev:", &p
)) {
3269 return qemu_chr_find(p
);
3272 opts
= qemu_chr_parse_compat(label
, filename
);
3276 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3277 if (error_is_set(&err
)) {
3278 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3281 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3282 monitor_init(chr
, MONITOR_USE_READLINE
);
3287 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3289 if (chr
->chr_set_echo
) {
3290 chr
->chr_set_echo(chr
, echo
);
3294 void qemu_chr_fe_open(struct CharDriverState
*chr
)
3296 if (chr
->chr_guest_open
) {
3297 chr
->chr_guest_open(chr
);
3301 void qemu_chr_fe_close(struct CharDriverState
*chr
)
3303 if (chr
->chr_guest_close
) {
3304 chr
->chr_guest_close(chr
);
3308 guint
qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3309 GIOFunc func
, void *user_data
)
3314 if (s
->chr_add_watch
== NULL
) {
3318 src
= s
->chr_add_watch(s
, cond
);
3319 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3320 tag
= g_source_attach(src
, NULL
);
3321 g_source_unref(src
);
3326 void qemu_chr_delete(CharDriverState
*chr
)
3328 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3329 if (chr
->chr_close
) {
3330 chr
->chr_close(chr
);
3332 g_free(chr
->filename
);
3335 qemu_opts_del(chr
->opts
);
3340 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3342 ChardevInfoList
*chr_list
= NULL
;
3343 CharDriverState
*chr
;
3345 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3346 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3347 info
->value
= g_malloc0(sizeof(*info
->value
));
3348 info
->value
->label
= g_strdup(chr
->label
);
3349 info
->value
->filename
= g_strdup(chr
->filename
);
3351 info
->next
= chr_list
;
3358 CharDriverState
*qemu_chr_find(const char *name
)
3360 CharDriverState
*chr
;
3362 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3363 if (strcmp(chr
->label
, name
) != 0)
3370 /* Get a character (serial) device interface. */
3371 CharDriverState
*qemu_char_get_next_serial(void)
3373 static int next_serial
;
3375 /* FIXME: This function needs to go away: use chardev properties! */
3376 return serial_hds
[next_serial
++];
3379 QemuOptsList qemu_chardev_opts
= {
3381 .implied_opt_name
= "backend",
3382 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3386 .type
= QEMU_OPT_STRING
,
3389 .type
= QEMU_OPT_STRING
,
3392 .type
= QEMU_OPT_STRING
,
3395 .type
= QEMU_OPT_STRING
,
3397 .name
= "localaddr",
3398 .type
= QEMU_OPT_STRING
,
3400 .name
= "localport",
3401 .type
= QEMU_OPT_STRING
,
3404 .type
= QEMU_OPT_NUMBER
,
3407 .type
= QEMU_OPT_BOOL
,
3410 .type
= QEMU_OPT_BOOL
,
3413 .type
= QEMU_OPT_BOOL
,
3416 .type
= QEMU_OPT_BOOL
,
3419 .type
= QEMU_OPT_BOOL
,
3422 .type
= QEMU_OPT_BOOL
,
3425 .type
= QEMU_OPT_NUMBER
,
3428 .type
= QEMU_OPT_NUMBER
,
3431 .type
= QEMU_OPT_NUMBER
,
3434 .type
= QEMU_OPT_NUMBER
,
3437 .type
= QEMU_OPT_BOOL
,
3440 .type
= QEMU_OPT_BOOL
,
3443 .type
= QEMU_OPT_STRING
,
3446 .type
= QEMU_OPT_NUMBER
,
3449 .type
= QEMU_OPT_SIZE
,
3451 { /* end of list */ }
3457 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3462 error_setg(errp
, "input file not supported");
3466 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3467 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3468 if (out
== INVALID_HANDLE_VALUE
) {
3469 error_setg(errp
, "open %s failed", file
->out
);
3472 return qemu_chr_open_win_file(out
);
3475 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3478 return qemu_chr_open_win_path(serial
->device
);
3481 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3484 error_setg(errp
, "character device backend type 'parallel' not supported");
3490 static int qmp_chardev_open_file_source(char *src
, int flags
,
3495 TFR(fd
= qemu_open(src
, flags
, 0666));
3497 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3502 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3504 int flags
, in
= -1, out
= -1;
3506 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3507 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3508 if (error_is_set(errp
)) {
3514 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3515 if (error_is_set(errp
)) {
3521 return qemu_chr_open_fd(in
, out
);
3524 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3527 #ifdef HAVE_CHARDEV_TTY
3530 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3531 if (error_is_set(errp
)) {
3534 socket_set_nonblock(fd
);
3535 return qemu_chr_open_tty_fd(fd
);
3537 error_setg(errp
, "character device backend type 'serial' not supported");
3542 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3545 #ifdef HAVE_CHARDEV_PARPORT
3548 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3549 if (error_is_set(errp
)) {
3552 return qemu_chr_open_pp_fd(fd
);
3554 error_setg(errp
, "character device backend type 'parallel' not supported");
3561 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3564 SocketAddress
*addr
= sock
->addr
;
3565 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3566 bool is_listen
= sock
->has_server
? sock
->server
: true;
3567 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3568 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3572 fd
= socket_listen(addr
, errp
);
3574 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3576 if (error_is_set(errp
)) {
3579 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3580 is_telnet
, is_waitconnect
, errp
);
3583 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3586 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3587 CharDriverState
*chr
= NULL
;
3589 chr
= qemu_chr_find(id
);
3591 error_setg(errp
, "Chardev '%s' already exists", id
);
3596 switch (backend
->kind
) {
3597 case CHARDEV_BACKEND_KIND_FILE
:
3598 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3600 case CHARDEV_BACKEND_KIND_SERIAL
:
3601 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3603 case CHARDEV_BACKEND_KIND_PARALLEL
:
3604 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3606 case CHARDEV_BACKEND_KIND_SOCKET
:
3607 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3609 #ifdef HAVE_CHARDEV_TTY
3610 case CHARDEV_BACKEND_KIND_PTY
:
3612 /* qemu_chr_open_pty sets "path" in opts */
3614 opts
= qemu_opts_create_nofail(qemu_find_opts("chardev"));
3615 chr
= qemu_chr_open_pty(opts
);
3616 ret
->pty
= g_strdup(qemu_opt_get(opts
, "path"));
3617 ret
->has_pty
= true;
3618 qemu_opts_del(opts
);
3622 case CHARDEV_BACKEND_KIND_NULL
:
3623 chr
= qemu_chr_open_null(NULL
);
3626 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3630 if (chr
== NULL
&& !error_is_set(errp
)) {
3631 error_setg(errp
, "Failed to create chardev");
3634 chr
->label
= g_strdup(id
);
3635 chr
->avail_connections
= 1;
3636 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3644 void qmp_chardev_remove(const char *id
, Error
**errp
)
3646 CharDriverState
*chr
;
3648 chr
= qemu_chr_find(id
);
3650 error_setg(errp
, "Chardev '%s' not found", id
);
3653 if (chr
->chr_can_read
|| chr
->chr_read
||
3654 chr
->chr_event
|| chr
->handler_opaque
) {
3655 error_setg(errp
, "Chardev '%s' is busy", id
);
3658 qemu_chr_delete(chr
);
3661 static void register_types(void)
3663 register_char_driver("null", qemu_chr_open_null
);
3664 register_char_driver("socket", qemu_chr_open_socket
);
3665 register_char_driver("udp", qemu_chr_open_udp
);
3666 register_char_driver("vc", vc_init
);
3667 register_char_driver("memory", qemu_chr_open_ringbuf
);
3669 register_char_driver("file", qemu_chr_open_win_file_out
);
3670 register_char_driver("pipe", qemu_chr_open_win_pipe
);
3671 register_char_driver("console", qemu_chr_open_win_con
);
3672 register_char_driver("serial", qemu_chr_open_win
);
3673 register_char_driver("stdio", qemu_chr_open_win_stdio
);
3675 register_char_driver("file", qemu_chr_open_file_out
);
3676 register_char_driver("pipe", qemu_chr_open_pipe
);
3677 register_char_driver("stdio", qemu_chr_open_stdio
);
3679 #ifdef HAVE_CHARDEV_TTY
3680 register_char_driver("tty", qemu_chr_open_tty
);
3681 register_char_driver("serial", qemu_chr_open_tty
);
3682 register_char_driver("pty", qemu_chr_open_pty
);
3684 #ifdef HAVE_CHARDEV_PARPORT
3685 register_char_driver("parallel", qemu_chr_open_pp
);
3686 register_char_driver("parport", qemu_chr_open_pp
);
3690 type_init(register_types
);