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(void)
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) {
537 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
543 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
545 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
562 typedef struct IOWatchPoll
567 IOCanReadHandler
*fd_can_read
;
570 QTAILQ_ENTRY(IOWatchPoll
) node
;
573 static QTAILQ_HEAD(, IOWatchPoll
) io_watch_poll_list
=
574 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list
);
576 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
580 QTAILQ_FOREACH(i
, &io_watch_poll_list
, node
) {
581 if (i
->src
== source
) {
589 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
591 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
593 iwp
->max_size
= iwp
->fd_can_read(iwp
->opaque
);
594 if (iwp
->max_size
== 0) {
598 return g_io_watch_funcs
.prepare(source
, timeout_
);
601 static gboolean
io_watch_poll_check(GSource
*source
)
603 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
605 if (iwp
->max_size
== 0) {
609 return g_io_watch_funcs
.check(source
);
612 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
615 return g_io_watch_funcs
.dispatch(source
, callback
, user_data
);
618 static void io_watch_poll_finalize(GSource
*source
)
620 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
621 QTAILQ_REMOVE(&io_watch_poll_list
, iwp
, node
);
622 g_io_watch_funcs
.finalize(source
);
625 static GSourceFuncs io_watch_poll_funcs
= {
626 .prepare
= io_watch_poll_prepare
,
627 .check
= io_watch_poll_check
,
628 .dispatch
= io_watch_poll_dispatch
,
629 .finalize
= io_watch_poll_finalize
,
632 /* Can only be used for read */
633 static guint
io_add_watch_poll(GIOChannel
*channel
,
634 IOCanReadHandler
*fd_can_read
,
642 src
= g_io_create_watch(channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
643 g_source_set_funcs(src
, &io_watch_poll_funcs
);
644 g_source_set_callback(src
, (GSourceFunc
)fd_read
, user_data
, NULL
);
645 tag
= g_source_attach(src
, NULL
);
648 iwp
= g_malloc0(sizeof(*iwp
));
651 iwp
->fd_can_read
= fd_can_read
;
652 iwp
->opaque
= user_data
;
654 QTAILQ_INSERT_HEAD(&io_watch_poll_list
, iwp
, node
);
660 static GIOChannel
*io_channel_from_fd(int fd
)
668 chan
= g_io_channel_unix_new(fd
);
670 g_io_channel_set_encoding(chan
, NULL
, NULL
);
671 g_io_channel_set_buffered(chan
, FALSE
);
677 static GIOChannel
*io_channel_from_socket(int fd
)
686 chan
= g_io_channel_win32_new_socket(fd
);
688 chan
= g_io_channel_unix_new(fd
);
691 g_io_channel_set_encoding(chan
, NULL
, NULL
);
692 g_io_channel_set_buffered(chan
, FALSE
);
697 static int io_channel_send_all(GIOChannel
*fd
, const void *_buf
, int len1
)
702 const uint8_t *buf
= _buf
;
706 status
= g_io_channel_write_chars(fd
, (const gchar
*)buf
, len
,
707 &bytes_written
, NULL
);
708 if (status
!= G_IO_STATUS_NORMAL
) {
709 if (status
== G_IO_STATUS_AGAIN
) {
716 } else if (status
== G_IO_STATUS_EOF
) {
719 buf
+= bytes_written
;
720 len
-= bytes_written
;
728 typedef struct FDCharDriver
{
729 CharDriverState
*chr
;
730 GIOChannel
*fd_in
, *fd_out
;
733 QTAILQ_ENTRY(FDCharDriver
) node
;
736 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
738 FDCharDriver
*s
= chr
->opaque
;
740 return io_channel_send_all(s
->fd_out
, buf
, len
);
743 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
745 CharDriverState
*chr
= opaque
;
746 FDCharDriver
*s
= chr
->opaque
;
748 uint8_t buf
[READ_BUF_LEN
];
753 if (len
> s
->max_size
) {
760 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
761 len
, &bytes_read
, NULL
);
762 if (status
== G_IO_STATUS_EOF
) {
763 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
766 if (status
== G_IO_STATUS_NORMAL
) {
767 qemu_chr_be_write(chr
, buf
, bytes_read
);
773 static int fd_chr_read_poll(void *opaque
)
775 CharDriverState
*chr
= opaque
;
776 FDCharDriver
*s
= chr
->opaque
;
778 s
->max_size
= qemu_chr_be_can_write(chr
);
782 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
784 FDCharDriver
*s
= chr
->opaque
;
785 return g_io_create_watch(s
->fd_out
, cond
);
788 static void fd_chr_update_read_handler(CharDriverState
*chr
)
790 FDCharDriver
*s
= chr
->opaque
;
793 g_source_remove(s
->fd_in_tag
);
797 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
801 static void fd_chr_close(struct CharDriverState
*chr
)
803 FDCharDriver
*s
= chr
->opaque
;
806 g_source_remove(s
->fd_in_tag
);
811 g_io_channel_unref(s
->fd_in
);
814 g_io_channel_unref(s
->fd_out
);
818 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
821 /* open a character device to a unix fd */
822 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
824 CharDriverState
*chr
;
827 chr
= g_malloc0(sizeof(CharDriverState
));
828 s
= g_malloc0(sizeof(FDCharDriver
));
829 s
->fd_in
= io_channel_from_fd(fd_in
);
830 s
->fd_out
= io_channel_from_fd(fd_out
);
831 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
834 chr
->chr_add_watch
= fd_chr_add_watch
;
835 chr
->chr_write
= fd_chr_write
;
836 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
837 chr
->chr_close
= fd_chr_close
;
839 qemu_chr_generic_open(chr
);
844 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
847 char filename_in
[256], filename_out
[256];
848 const char *filename
= qemu_opt_get(opts
, "path");
850 if (filename
== NULL
) {
851 fprintf(stderr
, "chardev: pipe: no filename given\n");
855 snprintf(filename_in
, 256, "%s.in", filename
);
856 snprintf(filename_out
, 256, "%s.out", filename
);
857 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
858 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
859 if (fd_in
< 0 || fd_out
< 0) {
864 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
869 return qemu_chr_open_fd(fd_in
, fd_out
);
872 /* init terminal so that we can grab keys */
873 static struct termios oldtty
;
874 static int old_fd0_flags
;
875 static bool stdio_allow_signal
;
877 static void term_exit(void)
879 tcsetattr (0, TCSANOW
, &oldtty
);
880 fcntl(0, F_SETFL
, old_fd0_flags
);
883 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
889 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
890 |INLCR
|IGNCR
|ICRNL
|IXON
);
891 tty
.c_oflag
|= OPOST
;
892 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
893 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
898 /* if graphical mode, we allow Ctrl-C handling */
899 if (!stdio_allow_signal
)
900 tty
.c_lflag
&= ~ISIG
;
902 tcsetattr (0, TCSANOW
, &tty
);
905 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
911 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
913 CharDriverState
*chr
;
915 if (is_daemonized()) {
916 error_report("cannot use stdio with -daemonize");
919 old_fd0_flags
= fcntl(0, F_GETFL
);
920 tcgetattr (0, &oldtty
);
921 fcntl(0, F_SETFL
, O_NONBLOCK
);
924 chr
= qemu_chr_open_fd(0, 1);
925 chr
->chr_close
= qemu_chr_close_stdio
;
926 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
927 stdio_allow_signal
= display_type
!= DT_NOGRAPHIC
;
928 if (opts
->has_signal
) {
929 stdio_allow_signal
= opts
->signal
;
931 qemu_chr_fe_set_echo(chr
, false);
937 /* Once Solaris has openpty(), this is going to be removed. */
938 static int openpty(int *amaster
, int *aslave
, char *name
,
939 struct termios
*termp
, struct winsize
*winp
)
942 int mfd
= -1, sfd
= -1;
944 *amaster
= *aslave
= -1;
946 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
950 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
953 if ((slave
= ptsname(mfd
)) == NULL
)
956 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
959 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
960 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
968 ioctl(sfd
, TIOCSWINSZ
, winp
);
979 static void cfmakeraw (struct termios
*termios_p
)
981 termios_p
->c_iflag
&=
982 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
983 termios_p
->c_oflag
&= ~OPOST
;
984 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
985 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
986 termios_p
->c_cflag
|= CS8
;
988 termios_p
->c_cc
[VMIN
] = 0;
989 termios_p
->c_cc
[VTIME
] = 0;
993 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
994 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
995 || defined(__GLIBC__)
997 #define HAVE_CHARDEV_TTY 1
1008 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1009 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1011 static gboolean
pty_chr_timer(gpointer opaque
)
1013 struct CharDriverState
*chr
= opaque
;
1014 PtyCharDriver
*s
= chr
->opaque
;
1020 /* If we arrive here without polling being cleared due
1021 * read returning -EIO, then we are (re-)connected */
1022 pty_chr_state(chr
, 1);
1027 pty_chr_update_read_handler(chr
);
1033 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1035 PtyCharDriver
*s
= chr
->opaque
;
1038 g_source_remove(s
->timer_tag
);
1043 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1045 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1049 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1051 PtyCharDriver
*s
= chr
->opaque
;
1053 if (!s
->connected
) {
1054 /* guest sends data, check for (re-)connect */
1055 pty_chr_update_read_handler(chr
);
1058 return io_channel_send_all(s
->fd
, buf
, len
);
1061 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1063 PtyCharDriver
*s
= chr
->opaque
;
1064 return g_io_create_watch(s
->fd
, cond
);
1067 static int pty_chr_read_poll(void *opaque
)
1069 CharDriverState
*chr
= opaque
;
1070 PtyCharDriver
*s
= chr
->opaque
;
1072 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1073 return s
->read_bytes
;
1076 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1078 CharDriverState
*chr
= opaque
;
1079 PtyCharDriver
*s
= chr
->opaque
;
1081 uint8_t buf
[READ_BUF_LEN
];
1085 if (len
> s
->read_bytes
)
1086 len
= s
->read_bytes
;
1089 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1090 if (status
!= G_IO_STATUS_NORMAL
) {
1091 pty_chr_state(chr
, 0);
1094 pty_chr_state(chr
, 1);
1095 qemu_chr_be_write(chr
, buf
, size
);
1100 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1102 PtyCharDriver
*s
= chr
->opaque
;
1105 g_source_remove(s
->fd_tag
);
1108 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1111 * Short timeout here: just need wait long enougth that qemu makes
1112 * it through the poll loop once. When reconnected we want a
1113 * short timeout so we notice it almost instantly. Otherwise
1114 * read() gives us -EIO instantly, making pty_chr_state() reset the
1115 * timeout to the normal (much longer) poll interval before the
1118 pty_chr_rearm_timer(chr
, 10);
1121 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1123 PtyCharDriver
*s
= chr
->opaque
;
1126 g_source_remove(s
->fd_tag
);
1130 /* (re-)connect poll interval for idle guests: once per second.
1131 * We check more frequently in case the guests sends data to
1132 * the virtual device linked to our pty. */
1133 pty_chr_rearm_timer(chr
, 1000);
1136 qemu_chr_generic_open(chr
);
1142 static void pty_chr_close(struct CharDriverState
*chr
)
1144 PtyCharDriver
*s
= chr
->opaque
;
1148 g_source_remove(s
->fd_tag
);
1150 fd
= g_io_channel_unix_get_fd(s
->fd
);
1151 g_io_channel_unref(s
->fd
);
1154 g_source_remove(s
->timer_tag
);
1157 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1160 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1162 CharDriverState
*chr
;
1166 int master_fd
, slave_fd
, len
;
1167 #if defined(__OpenBSD__) || defined(__DragonFly__)
1168 char pty_name
[PATH_MAX
];
1169 #define q_ptsname(x) pty_name
1171 char *pty_name
= NULL
;
1172 #define q_ptsname(x) ptsname(x)
1175 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1179 /* Set raw attributes on the pty. */
1180 tcgetattr(slave_fd
, &tty
);
1182 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1185 chr
= g_malloc0(sizeof(CharDriverState
));
1187 len
= strlen(q_ptsname(master_fd
)) + 5;
1188 chr
->filename
= g_malloc(len
);
1189 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
1190 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
1192 label
= qemu_opts_id(opts
);
1193 fprintf(stderr
, "char device redirected to %s%s%s%s\n",
1194 q_ptsname(master_fd
),
1195 label
? " (label " : "",
1199 s
= g_malloc0(sizeof(PtyCharDriver
));
1201 chr
->chr_write
= pty_chr_write
;
1202 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1203 chr
->chr_close
= pty_chr_close
;
1204 chr
->chr_add_watch
= pty_chr_add_watch
;
1206 s
->fd
= io_channel_from_fd(master_fd
);
1212 static void tty_serial_init(int fd
, int speed
,
1213 int parity
, int data_bits
, int stop_bits
)
1219 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1220 speed
, parity
, data_bits
, stop_bits
);
1222 tcgetattr (fd
, &tty
);
1224 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1225 speed
= speed
* 10 / 11;
1242 /* Non-Posix values follow. They may be unsupported on some systems. */
1244 check_speed(115200);
1246 check_speed(230400);
1249 check_speed(460800);
1252 check_speed(500000);
1255 check_speed(576000);
1258 check_speed(921600);
1261 check_speed(1000000);
1264 check_speed(1152000);
1267 check_speed(1500000);
1270 check_speed(2000000);
1273 check_speed(2500000);
1276 check_speed(3000000);
1279 check_speed(3500000);
1282 check_speed(4000000);
1287 cfsetispeed(&tty
, spd
);
1288 cfsetospeed(&tty
, spd
);
1290 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1291 |INLCR
|IGNCR
|ICRNL
|IXON
);
1292 tty
.c_oflag
|= OPOST
;
1293 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1294 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1315 tty
.c_cflag
|= PARENB
;
1318 tty
.c_cflag
|= PARENB
| PARODD
;
1322 tty
.c_cflag
|= CSTOPB
;
1324 tcsetattr (fd
, TCSANOW
, &tty
);
1327 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1329 FDCharDriver
*s
= chr
->opaque
;
1332 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1334 QEMUSerialSetParams
*ssp
= arg
;
1335 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1336 ssp
->speed
, ssp
->parity
,
1337 ssp
->data_bits
, ssp
->stop_bits
);
1340 case CHR_IOCTL_SERIAL_SET_BREAK
:
1342 int enable
= *(int *)arg
;
1344 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1348 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1351 int *targ
= (int *)arg
;
1352 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1354 if (sarg
& TIOCM_CTS
)
1355 *targ
|= CHR_TIOCM_CTS
;
1356 if (sarg
& TIOCM_CAR
)
1357 *targ
|= CHR_TIOCM_CAR
;
1358 if (sarg
& TIOCM_DSR
)
1359 *targ
|= CHR_TIOCM_DSR
;
1360 if (sarg
& TIOCM_RI
)
1361 *targ
|= CHR_TIOCM_RI
;
1362 if (sarg
& TIOCM_DTR
)
1363 *targ
|= CHR_TIOCM_DTR
;
1364 if (sarg
& TIOCM_RTS
)
1365 *targ
|= CHR_TIOCM_RTS
;
1368 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1370 int sarg
= *(int *)arg
;
1372 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1373 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1374 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1375 if (sarg
& CHR_TIOCM_CTS
)
1377 if (sarg
& CHR_TIOCM_CAR
)
1379 if (sarg
& CHR_TIOCM_DSR
)
1381 if (sarg
& CHR_TIOCM_RI
)
1383 if (sarg
& CHR_TIOCM_DTR
)
1385 if (sarg
& CHR_TIOCM_RTS
)
1387 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1396 static void qemu_chr_close_tty(CharDriverState
*chr
)
1398 FDCharDriver
*s
= chr
->opaque
;
1402 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1412 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1414 CharDriverState
*chr
;
1416 tty_serial_init(fd
, 115200, 'N', 8, 1);
1417 chr
= qemu_chr_open_fd(fd
, fd
);
1418 chr
->chr_ioctl
= tty_serial_ioctl
;
1419 chr
->chr_close
= qemu_chr_close_tty
;
1422 #endif /* __linux__ || __sun__ */
1424 #if defined(__linux__)
1426 #define HAVE_CHARDEV_PARPORT 1
1431 } ParallelCharDriver
;
1433 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1435 if (s
->mode
!= mode
) {
1437 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1444 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1446 ParallelCharDriver
*drv
= chr
->opaque
;
1451 case CHR_IOCTL_PP_READ_DATA
:
1452 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1454 *(uint8_t *)arg
= b
;
1456 case CHR_IOCTL_PP_WRITE_DATA
:
1457 b
= *(uint8_t *)arg
;
1458 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1461 case CHR_IOCTL_PP_READ_CONTROL
:
1462 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1464 /* Linux gives only the lowest bits, and no way to know data
1465 direction! For better compatibility set the fixed upper
1467 *(uint8_t *)arg
= b
| 0xc0;
1469 case CHR_IOCTL_PP_WRITE_CONTROL
:
1470 b
= *(uint8_t *)arg
;
1471 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1474 case CHR_IOCTL_PP_READ_STATUS
:
1475 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1477 *(uint8_t *)arg
= b
;
1479 case CHR_IOCTL_PP_DATA_DIR
:
1480 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1483 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1484 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1485 struct ParallelIOArg
*parg
= arg
;
1486 int n
= read(fd
, parg
->buffer
, parg
->count
);
1487 if (n
!= parg
->count
) {
1492 case CHR_IOCTL_PP_EPP_READ
:
1493 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1494 struct ParallelIOArg
*parg
= arg
;
1495 int n
= read(fd
, parg
->buffer
, parg
->count
);
1496 if (n
!= parg
->count
) {
1501 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1502 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1503 struct ParallelIOArg
*parg
= arg
;
1504 int n
= write(fd
, parg
->buffer
, parg
->count
);
1505 if (n
!= parg
->count
) {
1510 case CHR_IOCTL_PP_EPP_WRITE
:
1511 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1512 struct ParallelIOArg
*parg
= arg
;
1513 int n
= write(fd
, parg
->buffer
, parg
->count
);
1514 if (n
!= parg
->count
) {
1525 static void pp_close(CharDriverState
*chr
)
1527 ParallelCharDriver
*drv
= chr
->opaque
;
1530 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1531 ioctl(fd
, PPRELEASE
);
1534 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1537 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1539 CharDriverState
*chr
;
1540 ParallelCharDriver
*drv
;
1542 if (ioctl(fd
, PPCLAIM
) < 0) {
1547 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1549 drv
->mode
= IEEE1284_MODE_COMPAT
;
1551 chr
= g_malloc0(sizeof(CharDriverState
));
1552 chr
->chr_write
= null_chr_write
;
1553 chr
->chr_ioctl
= pp_ioctl
;
1554 chr
->chr_close
= pp_close
;
1557 qemu_chr_generic_open(chr
);
1561 #endif /* __linux__ */
1563 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1565 #define HAVE_CHARDEV_PARPORT 1
1567 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1569 int fd
= (int)(intptr_t)chr
->opaque
;
1573 case CHR_IOCTL_PP_READ_DATA
:
1574 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1576 *(uint8_t *)arg
= b
;
1578 case CHR_IOCTL_PP_WRITE_DATA
:
1579 b
= *(uint8_t *)arg
;
1580 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1583 case CHR_IOCTL_PP_READ_CONTROL
:
1584 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1586 *(uint8_t *)arg
= b
;
1588 case CHR_IOCTL_PP_WRITE_CONTROL
:
1589 b
= *(uint8_t *)arg
;
1590 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1593 case CHR_IOCTL_PP_READ_STATUS
:
1594 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1596 *(uint8_t *)arg
= b
;
1604 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1606 CharDriverState
*chr
;
1608 chr
= g_malloc0(sizeof(CharDriverState
));
1609 chr
->opaque
= (void *)(intptr_t)fd
;
1610 chr
->chr_write
= null_chr_write
;
1611 chr
->chr_ioctl
= pp_ioctl
;
1620 HANDLE hcom
, hrecv
, hsend
;
1621 OVERLAPPED orecv
, osend
;
1628 HANDLE hInputReadyEvent
;
1629 HANDLE hInputDoneEvent
;
1630 HANDLE hInputThread
;
1631 uint8_t win_stdio_buf
;
1632 } WinStdioCharState
;
1634 #define NSENDBUF 2048
1635 #define NRECVBUF 2048
1636 #define MAXCONNECT 1
1637 #define NTIMEOUT 5000
1639 static int win_chr_poll(void *opaque
);
1640 static int win_chr_pipe_poll(void *opaque
);
1642 static void win_chr_close(CharDriverState
*chr
)
1644 WinCharState
*s
= chr
->opaque
;
1647 CloseHandle(s
->hsend
);
1651 CloseHandle(s
->hrecv
);
1655 CloseHandle(s
->hcom
);
1659 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1661 qemu_del_polling_cb(win_chr_poll
, chr
);
1663 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1666 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1668 WinCharState
*s
= chr
->opaque
;
1670 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1675 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1677 fprintf(stderr
, "Failed CreateEvent\n");
1680 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1682 fprintf(stderr
, "Failed CreateEvent\n");
1686 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1687 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1688 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1689 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1694 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1695 fprintf(stderr
, "Failed SetupComm\n");
1699 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1700 size
= sizeof(COMMCONFIG
);
1701 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1702 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1703 CommConfigDialog(filename
, NULL
, &comcfg
);
1705 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1706 fprintf(stderr
, "Failed SetCommState\n");
1710 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1711 fprintf(stderr
, "Failed SetCommMask\n");
1715 cto
.ReadIntervalTimeout
= MAXDWORD
;
1716 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1717 fprintf(stderr
, "Failed SetCommTimeouts\n");
1721 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1722 fprintf(stderr
, "Failed ClearCommError\n");
1725 qemu_add_polling_cb(win_chr_poll
, chr
);
1733 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1735 WinCharState
*s
= chr
->opaque
;
1736 DWORD len
, ret
, size
, err
;
1739 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1740 s
->osend
.hEvent
= s
->hsend
;
1743 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1745 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1747 err
= GetLastError();
1748 if (err
== ERROR_IO_PENDING
) {
1749 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1767 static int win_chr_read_poll(CharDriverState
*chr
)
1769 WinCharState
*s
= chr
->opaque
;
1771 s
->max_size
= qemu_chr_be_can_write(chr
);
1775 static void win_chr_readfile(CharDriverState
*chr
)
1777 WinCharState
*s
= chr
->opaque
;
1779 uint8_t buf
[READ_BUF_LEN
];
1782 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1783 s
->orecv
.hEvent
= s
->hrecv
;
1784 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1786 err
= GetLastError();
1787 if (err
== ERROR_IO_PENDING
) {
1788 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1793 qemu_chr_be_write(chr
, buf
, size
);
1797 static void win_chr_read(CharDriverState
*chr
)
1799 WinCharState
*s
= chr
->opaque
;
1801 if (s
->len
> s
->max_size
)
1802 s
->len
= s
->max_size
;
1806 win_chr_readfile(chr
);
1809 static int win_chr_poll(void *opaque
)
1811 CharDriverState
*chr
= opaque
;
1812 WinCharState
*s
= chr
->opaque
;
1816 ClearCommError(s
->hcom
, &comerr
, &status
);
1817 if (status
.cbInQue
> 0) {
1818 s
->len
= status
.cbInQue
;
1819 win_chr_read_poll(chr
);
1826 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1828 CharDriverState
*chr
;
1831 chr
= g_malloc0(sizeof(CharDriverState
));
1832 s
= g_malloc0(sizeof(WinCharState
));
1834 chr
->chr_write
= win_chr_write
;
1835 chr
->chr_close
= win_chr_close
;
1837 if (win_chr_init(chr
, filename
) < 0) {
1842 qemu_chr_generic_open(chr
);
1846 static int win_chr_pipe_poll(void *opaque
)
1848 CharDriverState
*chr
= opaque
;
1849 WinCharState
*s
= chr
->opaque
;
1852 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1855 win_chr_read_poll(chr
);
1862 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1864 WinCharState
*s
= chr
->opaque
;
1872 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1874 fprintf(stderr
, "Failed CreateEvent\n");
1877 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1879 fprintf(stderr
, "Failed CreateEvent\n");
1883 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1884 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1885 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1887 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1888 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1889 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1894 ZeroMemory(&ov
, sizeof(ov
));
1895 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1896 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1898 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1902 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1904 fprintf(stderr
, "Failed GetOverlappedResult\n");
1906 CloseHandle(ov
.hEvent
);
1913 CloseHandle(ov
.hEvent
);
1916 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1925 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1927 const char *filename
= qemu_opt_get(opts
, "path");
1928 CharDriverState
*chr
;
1931 chr
= g_malloc0(sizeof(CharDriverState
));
1932 s
= g_malloc0(sizeof(WinCharState
));
1934 chr
->chr_write
= win_chr_write
;
1935 chr
->chr_close
= win_chr_close
;
1937 if (win_chr_pipe_init(chr
, filename
) < 0) {
1942 qemu_chr_generic_open(chr
);
1946 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1948 CharDriverState
*chr
;
1951 chr
= g_malloc0(sizeof(CharDriverState
));
1952 s
= g_malloc0(sizeof(WinCharState
));
1955 chr
->chr_write
= win_chr_write
;
1956 qemu_chr_generic_open(chr
);
1960 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1962 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1965 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1967 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1974 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1984 static void win_stdio_wait_func(void *opaque
)
1986 CharDriverState
*chr
= opaque
;
1987 WinStdioCharState
*stdio
= chr
->opaque
;
1988 INPUT_RECORD buf
[4];
1993 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1997 /* Avoid error storm */
1998 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2002 for (i
= 0; i
< dwSize
; i
++) {
2003 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2005 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2007 if (kev
->uChar
.AsciiChar
!= 0) {
2008 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2009 if (qemu_chr_be_can_write(chr
)) {
2010 uint8_t c
= kev
->uChar
.AsciiChar
;
2011 qemu_chr_be_write(chr
, &c
, 1);
2019 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2021 CharDriverState
*chr
= param
;
2022 WinStdioCharState
*stdio
= chr
->opaque
;
2028 /* Wait for one byte */
2029 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2031 /* Exit in case of error, continue if nothing read */
2039 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2040 if (stdio
->win_stdio_buf
== '\r') {
2044 /* Signal the main thread and wait until the byte was eaten */
2045 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2048 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2054 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2058 static void win_stdio_thread_wait_func(void *opaque
)
2060 CharDriverState
*chr
= opaque
;
2061 WinStdioCharState
*stdio
= chr
->opaque
;
2063 if (qemu_chr_be_can_write(chr
)) {
2064 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2067 SetEvent(stdio
->hInputDoneEvent
);
2070 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2072 WinStdioCharState
*stdio
= chr
->opaque
;
2075 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2078 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2080 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2084 static void win_stdio_close(CharDriverState
*chr
)
2086 WinStdioCharState
*stdio
= chr
->opaque
;
2088 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2089 CloseHandle(stdio
->hInputReadyEvent
);
2091 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2092 CloseHandle(stdio
->hInputDoneEvent
);
2094 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2095 TerminateThread(stdio
->hInputThread
, 0);
2098 g_free(chr
->opaque
);
2102 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2104 CharDriverState
*chr
;
2105 WinStdioCharState
*stdio
;
2109 chr
= g_malloc0(sizeof(CharDriverState
));
2110 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2112 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2113 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2114 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2118 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2120 chr
->opaque
= stdio
;
2121 chr
->chr_write
= win_stdio_write
;
2122 chr
->chr_close
= win_stdio_close
;
2125 if (qemu_add_wait_object(stdio
->hStdIn
,
2126 win_stdio_wait_func
, chr
)) {
2127 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2132 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2133 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2134 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2137 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2138 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2139 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2140 fprintf(stderr
, "cannot create stdio thread or event\n");
2143 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2144 win_stdio_thread_wait_func
, chr
)) {
2145 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2149 dwMode
|= ENABLE_LINE_INPUT
;
2152 /* set the terminal in raw mode */
2153 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2154 dwMode
|= ENABLE_PROCESSED_INPUT
;
2157 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2159 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2160 qemu_chr_fe_set_echo(chr
, false);
2164 #endif /* !_WIN32 */
2167 /***********************************************************/
2168 /* UDP Net console */
2174 uint8_t buf
[READ_BUF_LEN
];
2180 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2182 NetCharDriver
*s
= chr
->opaque
;
2183 gsize bytes_written
;
2186 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2187 if (status
== G_IO_STATUS_EOF
) {
2189 } else if (status
!= G_IO_STATUS_NORMAL
) {
2193 return bytes_written
;
2196 static int udp_chr_read_poll(void *opaque
)
2198 CharDriverState
*chr
= opaque
;
2199 NetCharDriver
*s
= chr
->opaque
;
2201 s
->max_size
= qemu_chr_be_can_write(chr
);
2203 /* If there were any stray characters in the queue process them
2206 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2207 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2209 s
->max_size
= qemu_chr_be_can_write(chr
);
2214 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2216 CharDriverState
*chr
= opaque
;
2217 NetCharDriver
*s
= chr
->opaque
;
2218 gsize bytes_read
= 0;
2221 if (s
->max_size
== 0)
2223 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2225 s
->bufcnt
= bytes_read
;
2226 s
->bufptr
= s
->bufcnt
;
2227 if (status
!= G_IO_STATUS_NORMAL
) {
2232 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2233 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2235 s
->max_size
= qemu_chr_be_can_write(chr
);
2241 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2243 NetCharDriver
*s
= chr
->opaque
;
2246 g_source_remove(s
->tag
);
2251 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2255 static void udp_chr_close(CharDriverState
*chr
)
2257 NetCharDriver
*s
= chr
->opaque
;
2259 g_source_remove(s
->tag
);
2262 g_io_channel_unref(s
->chan
);
2266 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2269 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2271 CharDriverState
*chr
= NULL
;
2272 NetCharDriver
*s
= NULL
;
2273 Error
*local_err
= NULL
;
2276 chr
= g_malloc0(sizeof(CharDriverState
));
2277 s
= g_malloc0(sizeof(NetCharDriver
));
2279 fd
= inet_dgram_opts(opts
, &local_err
);
2285 s
->chan
= io_channel_from_socket(s
->fd
);
2289 chr
->chr_write
= udp_chr_write
;
2290 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2291 chr
->chr_close
= udp_chr_close
;
2296 qerror_report_err(local_err
);
2297 error_free(local_err
);
2307 /***********************************************************/
2308 /* TCP Net console */
2312 GIOChannel
*chan
, *listen_chan
;
2313 guint tag
, listen_tag
;
2323 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2325 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2327 TCPCharDriver
*s
= chr
->opaque
;
2329 return io_channel_send_all(s
->chan
, buf
, len
);
2331 /* XXX: indicate an error ? */
2336 static int tcp_chr_read_poll(void *opaque
)
2338 CharDriverState
*chr
= opaque
;
2339 TCPCharDriver
*s
= chr
->opaque
;
2342 s
->max_size
= qemu_chr_be_can_write(chr
);
2347 #define IAC_BREAK 243
2348 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2350 uint8_t *buf
, int *size
)
2352 /* Handle any telnet client's basic IAC options to satisfy char by
2353 * char mode with no echo. All IAC options will be removed from
2354 * the buf and the do_telnetopt variable will be used to track the
2355 * state of the width of the IAC information.
2357 * IAC commands come in sets of 3 bytes with the exception of the
2358 * "IAC BREAK" command and the double IAC.
2364 for (i
= 0; i
< *size
; i
++) {
2365 if (s
->do_telnetopt
> 1) {
2366 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2367 /* Double IAC means send an IAC */
2371 s
->do_telnetopt
= 1;
2373 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2374 /* Handle IAC break commands by sending a serial break */
2375 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2380 if (s
->do_telnetopt
>= 4) {
2381 s
->do_telnetopt
= 1;
2384 if ((unsigned char)buf
[i
] == IAC
) {
2385 s
->do_telnetopt
= 2;
2396 static int tcp_get_msgfd(CharDriverState
*chr
)
2398 TCPCharDriver
*s
= chr
->opaque
;
2405 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2407 TCPCharDriver
*s
= chr
->opaque
;
2408 struct cmsghdr
*cmsg
;
2410 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2413 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2414 cmsg
->cmsg_level
!= SOL_SOCKET
||
2415 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2418 fd
= *((int *)CMSG_DATA(cmsg
));
2422 #ifndef MSG_CMSG_CLOEXEC
2423 qemu_set_cloexec(fd
);
2431 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2433 TCPCharDriver
*s
= chr
->opaque
;
2434 struct msghdr msg
= { NULL
, };
2435 struct iovec iov
[1];
2437 struct cmsghdr cmsg
;
2438 char control
[CMSG_SPACE(sizeof(int))];
2443 iov
[0].iov_base
= buf
;
2444 iov
[0].iov_len
= len
;
2448 msg
.msg_control
= &msg_control
;
2449 msg
.msg_controllen
= sizeof(msg_control
);
2451 #ifdef MSG_CMSG_CLOEXEC
2452 flags
|= MSG_CMSG_CLOEXEC
;
2454 ret
= recvmsg(s
->fd
, &msg
, flags
);
2455 if (ret
> 0 && s
->is_unix
) {
2456 unix_process_msgfd(chr
, &msg
);
2462 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2464 TCPCharDriver
*s
= chr
->opaque
;
2465 return qemu_recv(s
->fd
, buf
, len
, 0);
2469 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2471 TCPCharDriver
*s
= chr
->opaque
;
2472 return g_io_create_watch(s
->chan
, cond
);
2475 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2477 CharDriverState
*chr
= opaque
;
2478 TCPCharDriver
*s
= chr
->opaque
;
2479 uint8_t buf
[READ_BUF_LEN
];
2482 if (!s
->connected
|| s
->max_size
<= 0) {
2486 if (len
> s
->max_size
)
2488 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2490 /* connection closed */
2492 if (s
->listen_chan
) {
2493 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2495 g_source_remove(s
->tag
);
2497 g_io_channel_unref(s
->chan
);
2501 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2502 } else if (size
> 0) {
2503 if (s
->do_telnetopt
)
2504 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2506 qemu_chr_be_write(chr
, buf
, size
);
2513 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2515 return qemu_chr_open_fd(eventfd
, eventfd
);
2519 static void tcp_chr_connect(void *opaque
)
2521 CharDriverState
*chr
= opaque
;
2522 TCPCharDriver
*s
= chr
->opaque
;
2526 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2528 qemu_chr_generic_open(chr
);
2531 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2532 static void tcp_chr_telnet_init(int fd
)
2535 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2536 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2537 send(fd
, (char *)buf
, 3, 0);
2538 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2539 send(fd
, (char *)buf
, 3, 0);
2540 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2541 send(fd
, (char *)buf
, 3, 0);
2542 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2543 send(fd
, (char *)buf
, 3, 0);
2546 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2548 TCPCharDriver
*s
= chr
->opaque
;
2552 socket_set_nonblock(fd
);
2554 socket_set_nodelay(fd
);
2556 s
->chan
= io_channel_from_socket(fd
);
2557 g_source_remove(s
->listen_tag
);
2559 tcp_chr_connect(chr
);
2564 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2566 CharDriverState
*chr
= opaque
;
2567 TCPCharDriver
*s
= chr
->opaque
;
2568 struct sockaddr_in saddr
;
2570 struct sockaddr_un uaddr
;
2572 struct sockaddr
*addr
;
2579 len
= sizeof(uaddr
);
2580 addr
= (struct sockaddr
*)&uaddr
;
2584 len
= sizeof(saddr
);
2585 addr
= (struct sockaddr
*)&saddr
;
2587 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2588 if (fd
< 0 && errno
!= EINTR
) {
2590 } else if (fd
>= 0) {
2591 if (s
->do_telnetopt
)
2592 tcp_chr_telnet_init(fd
);
2596 if (tcp_chr_add_client(chr
, fd
) < 0)
2602 static void tcp_chr_close(CharDriverState
*chr
)
2604 TCPCharDriver
*s
= chr
->opaque
;
2607 g_source_remove(s
->tag
);
2610 g_io_channel_unref(s
->chan
);
2614 if (s
->listen_fd
>= 0) {
2615 if (s
->listen_tag
) {
2616 g_source_remove(s
->listen_tag
);
2618 if (s
->listen_chan
) {
2619 g_io_channel_unref(s
->listen_chan
);
2621 closesocket(s
->listen_fd
);
2624 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2627 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2628 bool is_listen
, bool is_telnet
,
2629 bool is_waitconnect
,
2632 CharDriverState
*chr
= NULL
;
2633 TCPCharDriver
*s
= NULL
;
2634 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2635 const char *left
= "", *right
= "";
2636 struct sockaddr_storage ss
;
2637 socklen_t ss_len
= sizeof(ss
);
2639 memset(&ss
, 0, ss_len
);
2640 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2641 error_setg(errp
, "getsockname: %s", strerror(errno
));
2645 chr
= g_malloc0(sizeof(CharDriverState
));
2646 s
= g_malloc0(sizeof(TCPCharDriver
));
2653 chr
->filename
= g_malloc(256);
2654 switch (ss
.ss_family
) {
2658 snprintf(chr
->filename
, 256, "unix:%s%s",
2659 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2660 is_listen
? ",server" : "");
2668 s
->do_nodelay
= do_nodelay
;
2669 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2670 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2671 snprintf(chr
->filename
, 256, "%s:%s:%s%s%s%s",
2672 is_telnet
? "telnet" : "tcp",
2673 left
, host
, right
, serv
,
2674 is_listen
? ",server" : "");
2679 chr
->chr_write
= tcp_chr_write
;
2680 chr
->chr_close
= tcp_chr_close
;
2681 chr
->get_msgfd
= tcp_get_msgfd
;
2682 chr
->chr_add_client
= tcp_chr_add_client
;
2683 chr
->chr_add_watch
= tcp_chr_add_watch
;
2687 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2688 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2690 s
->do_telnetopt
= 1;
2695 socket_set_nodelay(fd
);
2696 s
->chan
= io_channel_from_socket(s
->fd
);
2697 tcp_chr_connect(chr
);
2700 if (is_listen
&& is_waitconnect
) {
2701 printf("QEMU waiting for connection on: %s\n",
2703 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2704 socket_set_nonblock(s
->listen_fd
);
2709 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2711 CharDriverState
*chr
= NULL
;
2712 Error
*local_err
= NULL
;
2720 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2721 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2722 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2723 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2724 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2730 fd
= unix_listen_opts(opts
, &local_err
);
2732 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2736 fd
= inet_listen_opts(opts
, 0, &local_err
);
2738 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2745 if (!is_waitconnect
)
2746 socket_set_nonblock(fd
);
2748 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2749 is_waitconnect
, &local_err
);
2750 if (error_is_set(&local_err
)) {
2758 qerror_report_err(local_err
);
2759 error_free(local_err
);
2765 g_free(chr
->opaque
);
2771 /***********************************************************/
2772 /* Memory chardev */
2775 size_t outbuf_capacity
;
2779 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2781 MemoryDriver
*d
= chr
->opaque
;
2783 /* TODO: the QString implementation has the same code, we should
2784 * introduce a generic way to do this in cutils.c */
2785 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2787 d
->outbuf_capacity
+= len
;
2788 d
->outbuf_capacity
*= 2;
2789 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2792 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2793 d
->outbuf_size
+= len
;
2798 void qemu_chr_init_mem(CharDriverState
*chr
)
2802 d
= g_malloc(sizeof(*d
));
2804 d
->outbuf_capacity
= 4096;
2805 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2807 memset(chr
, 0, sizeof(*chr
));
2809 chr
->chr_write
= mem_chr_write
;
2812 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2814 MemoryDriver
*d
= chr
->opaque
;
2815 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2818 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2819 void qemu_chr_close_mem(CharDriverState
*chr
)
2821 MemoryDriver
*d
= chr
->opaque
;
2824 g_free(chr
->opaque
);
2826 chr
->chr_write
= NULL
;
2829 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2831 const MemoryDriver
*d
= chr
->opaque
;
2832 return d
->outbuf_size
;
2835 /*********************************************************/
2836 /* Ring buffer chardev */
2843 } RingBufCharDriver
;
2845 static size_t ringbuf_count(const CharDriverState
*chr
)
2847 const RingBufCharDriver
*d
= chr
->opaque
;
2849 return d
->prod
- d
->cons
;
2852 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2854 RingBufCharDriver
*d
= chr
->opaque
;
2857 if (!buf
|| (len
< 0)) {
2861 for (i
= 0; i
< len
; i
++ ) {
2862 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2863 if (d
->prod
- d
->cons
> d
->size
) {
2864 d
->cons
= d
->prod
- d
->size
;
2871 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2873 RingBufCharDriver
*d
= chr
->opaque
;
2876 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2877 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2883 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2885 RingBufCharDriver
*d
= chr
->opaque
;
2892 static CharDriverState
*qemu_chr_open_ringbuf(QemuOpts
*opts
)
2894 CharDriverState
*chr
;
2895 RingBufCharDriver
*d
;
2897 chr
= g_malloc0(sizeof(CharDriverState
));
2898 d
= g_malloc(sizeof(*d
));
2900 d
->size
= qemu_opt_get_size(opts
, "size", 0);
2905 /* The size must be power of 2 */
2906 if (d
->size
& (d
->size
- 1)) {
2907 error_report("size of ringbuf device must be power of two");
2913 d
->cbuf
= g_malloc0(d
->size
);
2916 chr
->chr_write
= ringbuf_chr_write
;
2917 chr
->chr_close
= ringbuf_chr_close
;
2927 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2929 return chr
->chr_write
== ringbuf_chr_write
;
2932 void qmp_ringbuf_write(const char *device
, const char *data
,
2933 bool has_format
, enum DataFormat format
,
2936 CharDriverState
*chr
;
2937 const uint8_t *write_data
;
2941 chr
= qemu_chr_find(device
);
2943 error_setg(errp
, "Device '%s' not found", device
);
2947 if (!chr_is_ringbuf(chr
)) {
2948 error_setg(errp
,"%s is not a ringbuf device", device
);
2952 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2953 write_data
= g_base64_decode(data
, &write_count
);
2955 write_data
= (uint8_t *)data
;
2956 write_count
= strlen(data
);
2959 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2961 if (write_data
!= (uint8_t *)data
) {
2962 g_free((void *)write_data
);
2966 error_setg(errp
, "Failed to write to device %s", device
);
2971 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2972 bool has_format
, enum DataFormat format
,
2975 CharDriverState
*chr
;
2980 chr
= qemu_chr_find(device
);
2982 error_setg(errp
, "Device '%s' not found", device
);
2986 if (!chr_is_ringbuf(chr
)) {
2987 error_setg(errp
,"%s is not a ringbuf device", device
);
2992 error_setg(errp
, "size must be greater than zero");
2996 count
= ringbuf_count(chr
);
2997 size
= size
> count
? count
: size
;
2998 read_data
= g_malloc(size
+ 1);
3000 ringbuf_chr_read(chr
, read_data
, size
);
3002 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3003 data
= g_base64_encode(read_data
, size
);
3007 * FIXME should read only complete, valid UTF-8 characters up
3008 * to @size bytes. Invalid sequences should be replaced by a
3009 * suitable replacement character. Except when (and only
3010 * when) ring buffer lost characters since last read, initial
3011 * continuation characters should be dropped.
3013 read_data
[size
] = 0;
3014 data
= (char *)read_data
;
3020 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3022 char host
[65], port
[33], width
[8], height
[8];
3026 Error
*local_err
= NULL
;
3028 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3029 if (error_is_set(&local_err
)) {
3030 qerror_report_err(local_err
);
3031 error_free(local_err
);
3035 if (strstart(filename
, "mon:", &p
)) {
3037 qemu_opt_set(opts
, "mux", "on");
3040 if (strcmp(filename
, "null") == 0 ||
3041 strcmp(filename
, "pty") == 0 ||
3042 strcmp(filename
, "msmouse") == 0 ||
3043 strcmp(filename
, "braille") == 0 ||
3044 strcmp(filename
, "stdio") == 0) {
3045 qemu_opt_set(opts
, "backend", filename
);
3048 if (strstart(filename
, "vc", &p
)) {
3049 qemu_opt_set(opts
, "backend", "vc");
3051 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
3053 qemu_opt_set(opts
, "width", width
);
3054 qemu_opt_set(opts
, "height", height
);
3055 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3057 qemu_opt_set(opts
, "cols", width
);
3058 qemu_opt_set(opts
, "rows", height
);
3065 if (strcmp(filename
, "con:") == 0) {
3066 qemu_opt_set(opts
, "backend", "console");
3069 if (strstart(filename
, "COM", NULL
)) {
3070 qemu_opt_set(opts
, "backend", "serial");
3071 qemu_opt_set(opts
, "path", filename
);
3074 if (strstart(filename
, "file:", &p
)) {
3075 qemu_opt_set(opts
, "backend", "file");
3076 qemu_opt_set(opts
, "path", p
);
3079 if (strstart(filename
, "pipe:", &p
)) {
3080 qemu_opt_set(opts
, "backend", "pipe");
3081 qemu_opt_set(opts
, "path", p
);
3084 if (strstart(filename
, "tcp:", &p
) ||
3085 strstart(filename
, "telnet:", &p
)) {
3086 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3088 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3091 qemu_opt_set(opts
, "backend", "socket");
3092 qemu_opt_set(opts
, "host", host
);
3093 qemu_opt_set(opts
, "port", port
);
3094 if (p
[pos
] == ',') {
3095 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3098 if (strstart(filename
, "telnet:", &p
))
3099 qemu_opt_set(opts
, "telnet", "on");
3102 if (strstart(filename
, "udp:", &p
)) {
3103 qemu_opt_set(opts
, "backend", "udp");
3104 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3106 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3110 qemu_opt_set(opts
, "host", host
);
3111 qemu_opt_set(opts
, "port", port
);
3112 if (p
[pos
] == '@') {
3114 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3116 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3120 qemu_opt_set(opts
, "localaddr", host
);
3121 qemu_opt_set(opts
, "localport", port
);
3125 if (strstart(filename
, "unix:", &p
)) {
3126 qemu_opt_set(opts
, "backend", "socket");
3127 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3131 if (strstart(filename
, "/dev/parport", NULL
) ||
3132 strstart(filename
, "/dev/ppi", NULL
)) {
3133 qemu_opt_set(opts
, "backend", "parport");
3134 qemu_opt_set(opts
, "path", filename
);
3137 if (strstart(filename
, "/dev/", NULL
)) {
3138 qemu_opt_set(opts
, "backend", "tty");
3139 qemu_opt_set(opts
, "path", filename
);
3144 qemu_opts_del(opts
);
3148 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3151 const char *path
= qemu_opt_get(opts
, "path");
3154 error_setg(errp
, "chardev: file: no filename given");
3157 backend
->file
= g_new0(ChardevFile
, 1);
3158 backend
->file
->out
= g_strdup(path
);
3161 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3164 backend
->stdio
= g_new0(ChardevStdio
, 1);
3165 backend
->stdio
->has_signal
= true;
3166 backend
->stdio
->signal
=
3167 qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
);
3170 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3173 const char *device
= qemu_opt_get(opts
, "path");
3175 if (device
== NULL
) {
3176 error_setg(errp
, "chardev: serial/tty: no device path given");
3179 backend
->serial
= g_new0(ChardevHostdev
, 1);
3180 backend
->serial
->device
= g_strdup(device
);
3183 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3186 const char *device
= qemu_opt_get(opts
, "path");
3188 if (device
== NULL
) {
3189 error_setg(errp
, "chardev: parallel: no device path given");
3192 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3193 backend
->parallel
->device
= g_strdup(device
);
3196 typedef struct CharDriver
{
3199 CharDriverState
*(*open
)(QemuOpts
*opts
);
3200 /* new, qapi-based */
3202 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3205 static GSList
*backends
;
3207 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3211 s
= g_malloc0(sizeof(*s
));
3212 s
->name
= g_strdup(name
);
3215 backends
= g_slist_append(backends
, s
);
3218 void register_char_driver_qapi(const char *name
, int kind
,
3219 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3223 s
= g_malloc0(sizeof(*s
));
3224 s
->name
= g_strdup(name
);
3228 backends
= g_slist_append(backends
, s
);
3231 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3232 void (*init
)(struct CharDriverState
*s
),
3236 CharDriverState
*chr
;
3239 if (qemu_opts_id(opts
) == NULL
) {
3240 error_setg(errp
, "chardev: no id specified");
3244 if (qemu_opt_get(opts
, "backend") == NULL
) {
3245 error_setg(errp
, "chardev: \"%s\" missing backend",
3246 qemu_opts_id(opts
));
3249 for (i
= backends
; i
; i
= i
->next
) {
3252 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3257 error_setg(errp
, "chardev: backend \"%s\" not found",
3258 qemu_opt_get(opts
, "backend"));
3263 /* using new, qapi init */
3264 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3265 ChardevReturn
*ret
= NULL
;
3266 const char *id
= qemu_opts_id(opts
);
3267 const char *bid
= NULL
;
3269 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3270 bid
= g_strdup_printf("%s-base", id
);
3274 backend
->kind
= cd
->kind
;
3276 cd
->parse(opts
, backend
, errp
);
3277 if (error_is_set(errp
)) {
3281 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3282 if (error_is_set(errp
)) {
3287 qapi_free_ChardevBackend(backend
);
3288 qapi_free_ChardevReturn(ret
);
3289 backend
= g_new0(ChardevBackend
, 1);
3290 backend
->mux
= g_new0(ChardevMux
, 1);
3291 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3292 backend
->mux
->chardev
= g_strdup(bid
);
3293 ret
= qmp_chardev_add(id
, backend
, errp
);
3294 if (error_is_set(errp
)) {
3299 chr
= qemu_chr_find(id
);
3302 qapi_free_ChardevBackend(backend
);
3303 qapi_free_ChardevReturn(ret
);
3307 chr
= cd
->open(opts
);
3309 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3310 qemu_opt_get(opts
, "backend"));
3315 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3317 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3319 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3320 CharDriverState
*base
= chr
;
3321 int len
= strlen(qemu_opts_id(opts
)) + 6;
3322 base
->label
= g_malloc(len
);
3323 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3324 chr
= qemu_chr_open_mux(base
);
3325 chr
->filename
= base
->filename
;
3326 chr
->avail_connections
= MAX_MUX
;
3327 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3329 chr
->avail_connections
= 1;
3331 chr
->label
= g_strdup(qemu_opts_id(opts
));
3336 qemu_opts_del(opts
);
3340 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3343 CharDriverState
*chr
;
3347 if (strstart(filename
, "chardev:", &p
)) {
3348 return qemu_chr_find(p
);
3351 opts
= qemu_chr_parse_compat(label
, filename
);
3355 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3356 if (error_is_set(&err
)) {
3357 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3360 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3361 monitor_init(chr
, MONITOR_USE_READLINE
);
3366 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3368 if (chr
->chr_set_echo
) {
3369 chr
->chr_set_echo(chr
, echo
);
3373 void qemu_chr_fe_open(struct CharDriverState
*chr
)
3375 if (chr
->chr_guest_open
) {
3376 chr
->chr_guest_open(chr
);
3380 void qemu_chr_fe_close(struct CharDriverState
*chr
)
3382 if (chr
->chr_guest_close
) {
3383 chr
->chr_guest_close(chr
);
3387 guint
qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3388 GIOFunc func
, void *user_data
)
3393 if (s
->chr_add_watch
== NULL
) {
3397 src
= s
->chr_add_watch(s
, cond
);
3398 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3399 tag
= g_source_attach(src
, NULL
);
3400 g_source_unref(src
);
3405 void qemu_chr_delete(CharDriverState
*chr
)
3407 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3408 if (chr
->chr_close
) {
3409 chr
->chr_close(chr
);
3411 g_free(chr
->filename
);
3414 qemu_opts_del(chr
->opts
);
3419 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3421 ChardevInfoList
*chr_list
= NULL
;
3422 CharDriverState
*chr
;
3424 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3425 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3426 info
->value
= g_malloc0(sizeof(*info
->value
));
3427 info
->value
->label
= g_strdup(chr
->label
);
3428 info
->value
->filename
= g_strdup(chr
->filename
);
3430 info
->next
= chr_list
;
3437 CharDriverState
*qemu_chr_find(const char *name
)
3439 CharDriverState
*chr
;
3441 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3442 if (strcmp(chr
->label
, name
) != 0)
3449 /* Get a character (serial) device interface. */
3450 CharDriverState
*qemu_char_get_next_serial(void)
3452 static int next_serial
;
3454 /* FIXME: This function needs to go away: use chardev properties! */
3455 return serial_hds
[next_serial
++];
3458 QemuOptsList qemu_chardev_opts
= {
3460 .implied_opt_name
= "backend",
3461 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3465 .type
= QEMU_OPT_STRING
,
3468 .type
= QEMU_OPT_STRING
,
3471 .type
= QEMU_OPT_STRING
,
3474 .type
= QEMU_OPT_STRING
,
3476 .name
= "localaddr",
3477 .type
= QEMU_OPT_STRING
,
3479 .name
= "localport",
3480 .type
= QEMU_OPT_STRING
,
3483 .type
= QEMU_OPT_NUMBER
,
3486 .type
= QEMU_OPT_BOOL
,
3489 .type
= QEMU_OPT_BOOL
,
3492 .type
= QEMU_OPT_BOOL
,
3495 .type
= QEMU_OPT_BOOL
,
3498 .type
= QEMU_OPT_BOOL
,
3501 .type
= QEMU_OPT_BOOL
,
3504 .type
= QEMU_OPT_NUMBER
,
3507 .type
= QEMU_OPT_NUMBER
,
3510 .type
= QEMU_OPT_NUMBER
,
3513 .type
= QEMU_OPT_NUMBER
,
3516 .type
= QEMU_OPT_BOOL
,
3519 .type
= QEMU_OPT_BOOL
,
3522 .type
= QEMU_OPT_STRING
,
3525 .type
= QEMU_OPT_NUMBER
,
3528 .type
= QEMU_OPT_SIZE
,
3530 { /* end of list */ }
3536 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3541 error_setg(errp
, "input file not supported");
3545 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3546 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3547 if (out
== INVALID_HANDLE_VALUE
) {
3548 error_setg(errp
, "open %s failed", file
->out
);
3551 return qemu_chr_open_win_file(out
);
3554 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3557 return qemu_chr_open_win_path(serial
->device
);
3560 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3563 error_setg(errp
, "character device backend type 'parallel' not supported");
3569 static int qmp_chardev_open_file_source(char *src
, int flags
,
3574 TFR(fd
= qemu_open(src
, flags
, 0666));
3576 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3581 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3583 int flags
, in
= -1, out
= -1;
3585 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3586 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3587 if (error_is_set(errp
)) {
3593 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3594 if (error_is_set(errp
)) {
3600 return qemu_chr_open_fd(in
, out
);
3603 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3606 #ifdef HAVE_CHARDEV_TTY
3609 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3610 if (error_is_set(errp
)) {
3613 socket_set_nonblock(fd
);
3614 return qemu_chr_open_tty_fd(fd
);
3616 error_setg(errp
, "character device backend type 'serial' not supported");
3621 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3624 #ifdef HAVE_CHARDEV_PARPORT
3627 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3628 if (error_is_set(errp
)) {
3631 return qemu_chr_open_pp_fd(fd
);
3633 error_setg(errp
, "character device backend type 'parallel' not supported");
3640 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3643 SocketAddress
*addr
= sock
->addr
;
3644 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3645 bool is_listen
= sock
->has_server
? sock
->server
: true;
3646 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3647 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3651 fd
= socket_listen(addr
, errp
);
3653 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3655 if (error_is_set(errp
)) {
3658 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3659 is_telnet
, is_waitconnect
, errp
);
3662 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3665 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3666 CharDriverState
*base
, *chr
= NULL
;
3668 chr
= qemu_chr_find(id
);
3670 error_setg(errp
, "Chardev '%s' already exists", id
);
3675 switch (backend
->kind
) {
3676 case CHARDEV_BACKEND_KIND_FILE
:
3677 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3679 case CHARDEV_BACKEND_KIND_SERIAL
:
3680 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3682 case CHARDEV_BACKEND_KIND_PARALLEL
:
3683 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3685 case CHARDEV_BACKEND_KIND_SOCKET
:
3686 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3688 #ifdef HAVE_CHARDEV_TTY
3689 case CHARDEV_BACKEND_KIND_PTY
:
3691 /* qemu_chr_open_pty sets "path" in opts */
3693 opts
= qemu_opts_create_nofail(qemu_find_opts("chardev"));
3694 chr
= qemu_chr_open_pty(opts
);
3695 ret
->pty
= g_strdup(qemu_opt_get(opts
, "path"));
3696 ret
->has_pty
= true;
3697 qemu_opts_del(opts
);
3701 case CHARDEV_BACKEND_KIND_NULL
:
3702 chr
= qemu_chr_open_null();
3704 case CHARDEV_BACKEND_KIND_MUX
:
3705 base
= qemu_chr_find(backend
->mux
->chardev
);
3707 error_setg(errp
, "mux: base chardev %s not found",
3708 backend
->mux
->chardev
);
3711 chr
= qemu_chr_open_mux(base
);
3713 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3714 chr
= qemu_chr_open_msmouse();
3716 #ifdef CONFIG_BRLAPI
3717 case CHARDEV_BACKEND_KIND_BRAILLE
:
3718 chr
= chr_baum_init();
3721 case CHARDEV_BACKEND_KIND_STDIO
:
3722 chr
= qemu_chr_open_stdio(backend
->stdio
);
3725 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3729 if (chr
== NULL
&& !error_is_set(errp
)) {
3730 error_setg(errp
, "Failed to create chardev");
3733 chr
->label
= g_strdup(id
);
3734 chr
->avail_connections
=
3735 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3736 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3744 void qmp_chardev_remove(const char *id
, Error
**errp
)
3746 CharDriverState
*chr
;
3748 chr
= qemu_chr_find(id
);
3750 error_setg(errp
, "Chardev '%s' not found", id
);
3753 if (chr
->chr_can_read
|| chr
->chr_read
||
3754 chr
->chr_event
|| chr
->handler_opaque
) {
3755 error_setg(errp
, "Chardev '%s' is busy", id
);
3758 qemu_chr_delete(chr
);
3761 static void register_types(void)
3763 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3764 register_char_driver("socket", qemu_chr_open_socket
);
3765 register_char_driver("udp", qemu_chr_open_udp
);
3766 register_char_driver("memory", qemu_chr_open_ringbuf
);
3767 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3768 qemu_chr_parse_file_out
);
3769 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3770 qemu_chr_parse_stdio
);
3771 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3772 qemu_chr_parse_serial
);
3773 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3774 qemu_chr_parse_serial
);
3775 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3776 qemu_chr_parse_parallel
);
3777 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3778 qemu_chr_parse_parallel
);
3780 register_char_driver("pipe", qemu_chr_open_win_pipe
);
3781 register_char_driver("console", qemu_chr_open_win_con
);
3783 register_char_driver("pipe", qemu_chr_open_pipe
);
3785 #ifdef HAVE_CHARDEV_TTY
3786 register_char_driver("pty", qemu_chr_open_pty
);
3790 type_init(register_types
);