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_be_generic_open_bh(gpointer opaque
)
125 CharDriverState
*s
= opaque
;
126 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
131 void qemu_chr_be_generic_open(CharDriverState
*s
)
133 if (s
->idle_tag
== 0) {
134 s
->idle_tag
= g_idle_add(qemu_chr_be_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_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
148 while (offset
< len
) {
150 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
151 if (res
== -1 && errno
== EAGAIN
) {
154 } while (res
== -1 && errno
== EAGAIN
);
170 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
174 return s
->chr_ioctl(s
, cmd
, arg
);
177 int qemu_chr_be_can_write(CharDriverState
*s
)
179 if (!s
->chr_can_read
)
181 return s
->chr_can_read(s
->handler_opaque
);
184 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
187 s
->chr_read(s
->handler_opaque
, buf
, len
);
191 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
193 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
196 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
198 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
201 void qemu_chr_accept_input(CharDriverState
*s
)
203 if (s
->chr_accept_input
)
204 s
->chr_accept_input(s
);
208 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
210 char buf
[READ_BUF_LEN
];
213 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
214 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
218 void qemu_chr_add_handlers(CharDriverState
*s
,
219 IOCanReadHandler
*fd_can_read
,
220 IOReadHandler
*fd_read
,
221 IOEventHandler
*fd_event
,
226 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
231 s
->chr_can_read
= fd_can_read
;
232 s
->chr_read
= fd_read
;
233 s
->chr_event
= fd_event
;
234 s
->handler_opaque
= opaque
;
235 if (s
->chr_update_read_handler
)
236 s
->chr_update_read_handler(s
);
238 if (!s
->explicit_fe_open
) {
239 qemu_chr_fe_set_open(s
, fe_open
);
242 /* We're connecting to an already opened device, so let's make sure we
243 also get the open event */
244 if (fe_open
&& s
->be_open
) {
245 qemu_chr_be_generic_open(s
);
249 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
254 static CharDriverState
*qemu_chr_open_null(void)
256 CharDriverState
*chr
;
258 chr
= g_malloc0(sizeof(CharDriverState
));
259 chr
->chr_write
= null_chr_write
;
263 /* MUX driver for serial I/O splitting */
265 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
266 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
268 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
269 IOReadHandler
*chr_read
[MAX_MUX
];
270 IOEventHandler
*chr_event
[MAX_MUX
];
271 void *ext_opaque
[MAX_MUX
];
272 CharDriverState
*drv
;
277 /* Intermediate input buffer allows to catch escape sequences even if the
278 currently active device is not accepting any input - but only until it
280 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
285 int64_t timestamps_start
;
289 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
291 MuxDriver
*d
= chr
->opaque
;
293 if (!d
->timestamps
) {
294 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
299 for (i
= 0; i
< len
; i
++) {
305 ti
= qemu_get_clock_ms(rt_clock
);
306 if (d
->timestamps_start
== -1)
307 d
->timestamps_start
= ti
;
308 ti
-= d
->timestamps_start
;
310 snprintf(buf1
, sizeof(buf1
),
311 "[%02d:%02d:%02d.%03d] ",
316 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
319 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
320 if (buf
[i
] == '\n') {
328 static const char * const mux_help
[] = {
329 "% h print this help\n\r",
330 "% x exit emulator\n\r",
331 "% s save disk data back to file (if -snapshot)\n\r",
332 "% t toggle console timestamps\n\r"
333 "% b send break (magic sysrq)\n\r",
334 "% c switch between console and monitor\n\r",
339 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
340 static void mux_print_help(CharDriverState
*chr
)
343 char ebuf
[15] = "Escape-Char";
344 char cbuf
[50] = "\n\r";
346 if (term_escape_char
> 0 && term_escape_char
< 26) {
347 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
348 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
350 snprintf(cbuf
, sizeof(cbuf
),
351 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
354 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
355 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
356 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
357 if (mux_help
[i
][j
] == '%')
358 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
360 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
365 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
367 if (d
->chr_event
[mux_nr
])
368 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
371 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
373 if (d
->term_got_escape
) {
374 d
->term_got_escape
= 0;
375 if (ch
== term_escape_char
)
384 const char *term
= "QEMU: Terminated\n\r";
385 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
393 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
396 /* Switch to the next registered device */
397 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
399 if (d
->focus
>= d
->mux_cnt
)
401 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
404 d
->timestamps
= !d
->timestamps
;
405 d
->timestamps_start
= -1;
409 } else if (ch
== term_escape_char
) {
410 d
->term_got_escape
= 1;
418 static void mux_chr_accept_input(CharDriverState
*chr
)
420 MuxDriver
*d
= chr
->opaque
;
423 while (d
->prod
[m
] != d
->cons
[m
] &&
424 d
->chr_can_read
[m
] &&
425 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
426 d
->chr_read
[m
](d
->ext_opaque
[m
],
427 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
431 static int mux_chr_can_read(void *opaque
)
433 CharDriverState
*chr
= opaque
;
434 MuxDriver
*d
= chr
->opaque
;
437 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
439 if (d
->chr_can_read
[m
])
440 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
444 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
446 CharDriverState
*chr
= opaque
;
447 MuxDriver
*d
= chr
->opaque
;
451 mux_chr_accept_input (opaque
);
453 for(i
= 0; i
< size
; i
++)
454 if (mux_proc_byte(chr
, d
, buf
[i
])) {
455 if (d
->prod
[m
] == d
->cons
[m
] &&
456 d
->chr_can_read
[m
] &&
457 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
458 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
460 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
464 static void mux_chr_event(void *opaque
, int event
)
466 CharDriverState
*chr
= opaque
;
467 MuxDriver
*d
= chr
->opaque
;
470 /* Send the event to all registered listeners */
471 for (i
= 0; i
< d
->mux_cnt
; i
++)
472 mux_chr_send_event(d
, i
, event
);
475 static void mux_chr_update_read_handler(CharDriverState
*chr
)
477 MuxDriver
*d
= chr
->opaque
;
479 if (d
->mux_cnt
>= MAX_MUX
) {
480 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
483 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
484 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
485 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
486 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
487 /* Fix up the real driver with mux routines */
488 if (d
->mux_cnt
== 0) {
489 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
492 if (d
->focus
!= -1) {
493 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
495 d
->focus
= d
->mux_cnt
;
497 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
500 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
502 CharDriverState
*chr
;
505 chr
= g_malloc0(sizeof(CharDriverState
));
506 d
= g_malloc0(sizeof(MuxDriver
));
511 chr
->chr_write
= mux_chr_write
;
512 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
513 chr
->chr_accept_input
= mux_chr_accept_input
;
514 /* Frontend guest-open / -close notification is not support with muxes */
515 chr
->chr_set_fe_open
= NULL
;
517 /* Muxes are always open on creation */
518 qemu_chr_be_generic_open(chr
);
525 int send_all(int fd
, const void *buf
, int len1
)
531 ret
= send(fd
, buf
, len
, 0);
533 errno
= WSAGetLastError();
534 if (errno
!= WSAEWOULDBLOCK
) {
537 } else if (ret
== 0) {
549 int send_all(int fd
, const void *_buf
, int len1
)
552 const uint8_t *buf
= _buf
;
556 ret
= write(fd
, buf
, len
);
558 if (errno
!= EINTR
&& errno
!= EAGAIN
)
560 } else if (ret
== 0) {
570 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
576 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
578 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
595 typedef struct IOWatchPoll
600 IOCanReadHandler
*fd_can_read
;
603 QTAILQ_ENTRY(IOWatchPoll
) node
;
606 static QTAILQ_HEAD(, IOWatchPoll
) io_watch_poll_list
=
607 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list
);
609 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
613 QTAILQ_FOREACH(i
, &io_watch_poll_list
, node
) {
614 if (i
->src
== source
) {
622 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
624 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
626 iwp
->max_size
= iwp
->fd_can_read(iwp
->opaque
);
627 if (iwp
->max_size
== 0) {
631 return g_io_watch_funcs
.prepare(source
, timeout_
);
634 static gboolean
io_watch_poll_check(GSource
*source
)
636 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
638 if (iwp
->max_size
== 0) {
642 return g_io_watch_funcs
.check(source
);
645 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
648 return g_io_watch_funcs
.dispatch(source
, callback
, user_data
);
651 static void io_watch_poll_finalize(GSource
*source
)
653 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
654 QTAILQ_REMOVE(&io_watch_poll_list
, iwp
, node
);
655 g_io_watch_funcs
.finalize(source
);
658 static GSourceFuncs io_watch_poll_funcs
= {
659 .prepare
= io_watch_poll_prepare
,
660 .check
= io_watch_poll_check
,
661 .dispatch
= io_watch_poll_dispatch
,
662 .finalize
= io_watch_poll_finalize
,
665 /* Can only be used for read */
666 static guint
io_add_watch_poll(GIOChannel
*channel
,
667 IOCanReadHandler
*fd_can_read
,
675 src
= g_io_create_watch(channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
676 g_source_set_funcs(src
, &io_watch_poll_funcs
);
677 g_source_set_callback(src
, (GSourceFunc
)fd_read
, user_data
, NULL
);
678 tag
= g_source_attach(src
, NULL
);
681 iwp
= g_malloc0(sizeof(*iwp
));
684 iwp
->fd_can_read
= fd_can_read
;
685 iwp
->opaque
= user_data
;
687 QTAILQ_INSERT_HEAD(&io_watch_poll_list
, iwp
, node
);
693 static GIOChannel
*io_channel_from_fd(int fd
)
701 chan
= g_io_channel_unix_new(fd
);
703 g_io_channel_set_encoding(chan
, NULL
, NULL
);
704 g_io_channel_set_buffered(chan
, FALSE
);
710 static GIOChannel
*io_channel_from_socket(int fd
)
719 chan
= g_io_channel_win32_new_socket(fd
);
721 chan
= g_io_channel_unix_new(fd
);
724 g_io_channel_set_encoding(chan
, NULL
, NULL
);
725 g_io_channel_set_buffered(chan
, FALSE
);
730 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
736 while (offset
< len
) {
739 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
740 &bytes_written
, NULL
);
741 if (status
!= G_IO_STATUS_NORMAL
) {
742 if (status
== G_IO_STATUS_AGAIN
) {
743 /* If we've written any data, return a partial write. */
753 } else if (status
== G_IO_STATUS_EOF
) {
757 offset
+= bytes_written
;
765 typedef struct FDCharDriver
{
766 CharDriverState
*chr
;
767 GIOChannel
*fd_in
, *fd_out
;
770 QTAILQ_ENTRY(FDCharDriver
) node
;
773 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
775 FDCharDriver
*s
= chr
->opaque
;
777 return io_channel_send(s
->fd_out
, buf
, len
);
780 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
782 CharDriverState
*chr
= opaque
;
783 FDCharDriver
*s
= chr
->opaque
;
785 uint8_t buf
[READ_BUF_LEN
];
790 if (len
> s
->max_size
) {
797 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
798 len
, &bytes_read
, NULL
);
799 if (status
== G_IO_STATUS_EOF
) {
800 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
803 if (status
== G_IO_STATUS_NORMAL
) {
804 qemu_chr_be_write(chr
, buf
, bytes_read
);
810 static int fd_chr_read_poll(void *opaque
)
812 CharDriverState
*chr
= opaque
;
813 FDCharDriver
*s
= chr
->opaque
;
815 s
->max_size
= qemu_chr_be_can_write(chr
);
819 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
821 FDCharDriver
*s
= chr
->opaque
;
822 return g_io_create_watch(s
->fd_out
, cond
);
825 static void fd_chr_update_read_handler(CharDriverState
*chr
)
827 FDCharDriver
*s
= chr
->opaque
;
830 g_source_remove(s
->fd_in_tag
);
834 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
838 static void fd_chr_close(struct CharDriverState
*chr
)
840 FDCharDriver
*s
= chr
->opaque
;
843 g_source_remove(s
->fd_in_tag
);
848 g_io_channel_unref(s
->fd_in
);
851 g_io_channel_unref(s
->fd_out
);
855 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
858 /* open a character device to a unix fd */
859 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
861 CharDriverState
*chr
;
864 chr
= g_malloc0(sizeof(CharDriverState
));
865 s
= g_malloc0(sizeof(FDCharDriver
));
866 s
->fd_in
= io_channel_from_fd(fd_in
);
867 s
->fd_out
= io_channel_from_fd(fd_out
);
868 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
871 chr
->chr_add_watch
= fd_chr_add_watch
;
872 chr
->chr_write
= fd_chr_write
;
873 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
874 chr
->chr_close
= fd_chr_close
;
876 qemu_chr_be_generic_open(chr
);
881 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
884 char filename_in
[256], filename_out
[256];
885 const char *filename
= opts
->device
;
887 if (filename
== NULL
) {
888 fprintf(stderr
, "chardev: pipe: no filename given\n");
892 snprintf(filename_in
, 256, "%s.in", filename
);
893 snprintf(filename_out
, 256, "%s.out", filename
);
894 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
895 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
896 if (fd_in
< 0 || fd_out
< 0) {
901 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
906 return qemu_chr_open_fd(fd_in
, fd_out
);
909 /* init terminal so that we can grab keys */
910 static struct termios oldtty
;
911 static int old_fd0_flags
;
912 static bool stdio_allow_signal
;
914 static void term_exit(void)
916 tcsetattr (0, TCSANOW
, &oldtty
);
917 fcntl(0, F_SETFL
, old_fd0_flags
);
920 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
926 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
927 |INLCR
|IGNCR
|ICRNL
|IXON
);
928 tty
.c_oflag
|= OPOST
;
929 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
930 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
935 /* if graphical mode, we allow Ctrl-C handling */
936 if (!stdio_allow_signal
)
937 tty
.c_lflag
&= ~ISIG
;
939 tcsetattr (0, TCSANOW
, &tty
);
942 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
948 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
950 CharDriverState
*chr
;
952 if (is_daemonized()) {
953 error_report("cannot use stdio with -daemonize");
956 old_fd0_flags
= fcntl(0, F_GETFL
);
957 tcgetattr (0, &oldtty
);
958 fcntl(0, F_SETFL
, O_NONBLOCK
);
961 chr
= qemu_chr_open_fd(0, 1);
962 chr
->chr_close
= qemu_chr_close_stdio
;
963 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
964 stdio_allow_signal
= display_type
!= DT_NOGRAPHIC
;
965 if (opts
->has_signal
) {
966 stdio_allow_signal
= opts
->signal
;
968 qemu_chr_fe_set_echo(chr
, false);
974 /* Once Solaris has openpty(), this is going to be removed. */
975 static int openpty(int *amaster
, int *aslave
, char *name
,
976 struct termios
*termp
, struct winsize
*winp
)
979 int mfd
= -1, sfd
= -1;
981 *amaster
= *aslave
= -1;
983 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
987 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
990 if ((slave
= ptsname(mfd
)) == NULL
)
993 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
996 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
997 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
1005 ioctl(sfd
, TIOCSWINSZ
, winp
);
1016 static void cfmakeraw (struct termios
*termios_p
)
1018 termios_p
->c_iflag
&=
1019 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
1020 termios_p
->c_oflag
&= ~OPOST
;
1021 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
1022 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
1023 termios_p
->c_cflag
|= CS8
;
1025 termios_p
->c_cc
[VMIN
] = 0;
1026 termios_p
->c_cc
[VTIME
] = 0;
1030 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1031 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1032 || defined(__GLIBC__)
1034 #define HAVE_CHARDEV_TTY 1
1045 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1046 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1048 static gboolean
pty_chr_timer(gpointer opaque
)
1050 struct CharDriverState
*chr
= opaque
;
1051 PtyCharDriver
*s
= chr
->opaque
;
1057 /* If we arrive here without polling being cleared due
1058 * read returning -EIO, then we are (re-)connected */
1059 pty_chr_state(chr
, 1);
1064 pty_chr_update_read_handler(chr
);
1070 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1072 PtyCharDriver
*s
= chr
->opaque
;
1075 g_source_remove(s
->timer_tag
);
1080 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1082 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1086 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1088 PtyCharDriver
*s
= chr
->opaque
;
1090 if (!s
->connected
) {
1091 /* guest sends data, check for (re-)connect */
1092 pty_chr_update_read_handler(chr
);
1095 return io_channel_send(s
->fd
, buf
, len
);
1098 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1100 PtyCharDriver
*s
= chr
->opaque
;
1101 return g_io_create_watch(s
->fd
, cond
);
1104 static int pty_chr_read_poll(void *opaque
)
1106 CharDriverState
*chr
= opaque
;
1107 PtyCharDriver
*s
= chr
->opaque
;
1109 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1110 return s
->read_bytes
;
1113 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1115 CharDriverState
*chr
= opaque
;
1116 PtyCharDriver
*s
= chr
->opaque
;
1118 uint8_t buf
[READ_BUF_LEN
];
1122 if (len
> s
->read_bytes
)
1123 len
= s
->read_bytes
;
1126 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1127 if (status
!= G_IO_STATUS_NORMAL
) {
1128 pty_chr_state(chr
, 0);
1131 pty_chr_state(chr
, 1);
1132 qemu_chr_be_write(chr
, buf
, size
);
1137 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1139 PtyCharDriver
*s
= chr
->opaque
;
1142 g_source_remove(s
->fd_tag
);
1145 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1148 * Short timeout here: just need wait long enougth that qemu makes
1149 * it through the poll loop once. When reconnected we want a
1150 * short timeout so we notice it almost instantly. Otherwise
1151 * read() gives us -EIO instantly, making pty_chr_state() reset the
1152 * timeout to the normal (much longer) poll interval before the
1155 pty_chr_rearm_timer(chr
, 10);
1158 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1160 PtyCharDriver
*s
= chr
->opaque
;
1163 g_source_remove(s
->fd_tag
);
1167 /* (re-)connect poll interval for idle guests: once per second.
1168 * We check more frequently in case the guests sends data to
1169 * the virtual device linked to our pty. */
1170 pty_chr_rearm_timer(chr
, 1000);
1173 qemu_chr_be_generic_open(chr
);
1179 static void pty_chr_close(struct CharDriverState
*chr
)
1181 PtyCharDriver
*s
= chr
->opaque
;
1185 g_source_remove(s
->fd_tag
);
1187 fd
= g_io_channel_unix_get_fd(s
->fd
);
1188 g_io_channel_unref(s
->fd
);
1191 g_source_remove(s
->timer_tag
);
1194 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1197 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1200 CharDriverState
*chr
;
1203 int master_fd
, slave_fd
;
1204 #if defined(__OpenBSD__) || defined(__DragonFly__)
1205 char pty_name
[PATH_MAX
];
1206 #define q_ptsname(x) pty_name
1208 char *pty_name
= NULL
;
1209 #define q_ptsname(x) ptsname(x)
1212 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1216 /* Set raw attributes on the pty. */
1217 tcgetattr(slave_fd
, &tty
);
1219 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1222 chr
= g_malloc0(sizeof(CharDriverState
));
1224 chr
->filename
= g_strdup_printf("pty:%s", q_ptsname(master_fd
));
1225 ret
->pty
= g_strdup(q_ptsname(master_fd
));
1226 ret
->has_pty
= true;
1228 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1229 q_ptsname(master_fd
), id
);
1231 s
= g_malloc0(sizeof(PtyCharDriver
));
1233 chr
->chr_write
= pty_chr_write
;
1234 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1235 chr
->chr_close
= pty_chr_close
;
1236 chr
->chr_add_watch
= pty_chr_add_watch
;
1238 s
->fd
= io_channel_from_fd(master_fd
);
1244 static void tty_serial_init(int fd
, int speed
,
1245 int parity
, int data_bits
, int stop_bits
)
1251 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1252 speed
, parity
, data_bits
, stop_bits
);
1254 tcgetattr (fd
, &tty
);
1256 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1257 speed
= speed
* 10 / 11;
1274 /* Non-Posix values follow. They may be unsupported on some systems. */
1276 check_speed(115200);
1278 check_speed(230400);
1281 check_speed(460800);
1284 check_speed(500000);
1287 check_speed(576000);
1290 check_speed(921600);
1293 check_speed(1000000);
1296 check_speed(1152000);
1299 check_speed(1500000);
1302 check_speed(2000000);
1305 check_speed(2500000);
1308 check_speed(3000000);
1311 check_speed(3500000);
1314 check_speed(4000000);
1319 cfsetispeed(&tty
, spd
);
1320 cfsetospeed(&tty
, spd
);
1322 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1323 |INLCR
|IGNCR
|ICRNL
|IXON
);
1324 tty
.c_oflag
|= OPOST
;
1325 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1326 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1347 tty
.c_cflag
|= PARENB
;
1350 tty
.c_cflag
|= PARENB
| PARODD
;
1354 tty
.c_cflag
|= CSTOPB
;
1356 tcsetattr (fd
, TCSANOW
, &tty
);
1359 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1361 FDCharDriver
*s
= chr
->opaque
;
1364 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1366 QEMUSerialSetParams
*ssp
= arg
;
1367 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1368 ssp
->speed
, ssp
->parity
,
1369 ssp
->data_bits
, ssp
->stop_bits
);
1372 case CHR_IOCTL_SERIAL_SET_BREAK
:
1374 int enable
= *(int *)arg
;
1376 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1380 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1383 int *targ
= (int *)arg
;
1384 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1386 if (sarg
& TIOCM_CTS
)
1387 *targ
|= CHR_TIOCM_CTS
;
1388 if (sarg
& TIOCM_CAR
)
1389 *targ
|= CHR_TIOCM_CAR
;
1390 if (sarg
& TIOCM_DSR
)
1391 *targ
|= CHR_TIOCM_DSR
;
1392 if (sarg
& TIOCM_RI
)
1393 *targ
|= CHR_TIOCM_RI
;
1394 if (sarg
& TIOCM_DTR
)
1395 *targ
|= CHR_TIOCM_DTR
;
1396 if (sarg
& TIOCM_RTS
)
1397 *targ
|= CHR_TIOCM_RTS
;
1400 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1402 int sarg
= *(int *)arg
;
1404 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1405 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1406 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1407 if (sarg
& CHR_TIOCM_CTS
)
1409 if (sarg
& CHR_TIOCM_CAR
)
1411 if (sarg
& CHR_TIOCM_DSR
)
1413 if (sarg
& CHR_TIOCM_RI
)
1415 if (sarg
& CHR_TIOCM_DTR
)
1417 if (sarg
& CHR_TIOCM_RTS
)
1419 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1428 static void qemu_chr_close_tty(CharDriverState
*chr
)
1430 FDCharDriver
*s
= chr
->opaque
;
1434 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1444 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1446 CharDriverState
*chr
;
1448 tty_serial_init(fd
, 115200, 'N', 8, 1);
1449 chr
= qemu_chr_open_fd(fd
, fd
);
1450 chr
->chr_ioctl
= tty_serial_ioctl
;
1451 chr
->chr_close
= qemu_chr_close_tty
;
1454 #endif /* __linux__ || __sun__ */
1456 #if defined(__linux__)
1458 #define HAVE_CHARDEV_PARPORT 1
1463 } ParallelCharDriver
;
1465 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1467 if (s
->mode
!= mode
) {
1469 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1476 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1478 ParallelCharDriver
*drv
= chr
->opaque
;
1483 case CHR_IOCTL_PP_READ_DATA
:
1484 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1486 *(uint8_t *)arg
= b
;
1488 case CHR_IOCTL_PP_WRITE_DATA
:
1489 b
= *(uint8_t *)arg
;
1490 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1493 case CHR_IOCTL_PP_READ_CONTROL
:
1494 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1496 /* Linux gives only the lowest bits, and no way to know data
1497 direction! For better compatibility set the fixed upper
1499 *(uint8_t *)arg
= b
| 0xc0;
1501 case CHR_IOCTL_PP_WRITE_CONTROL
:
1502 b
= *(uint8_t *)arg
;
1503 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1506 case CHR_IOCTL_PP_READ_STATUS
:
1507 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1509 *(uint8_t *)arg
= b
;
1511 case CHR_IOCTL_PP_DATA_DIR
:
1512 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1515 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1516 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1517 struct ParallelIOArg
*parg
= arg
;
1518 int n
= read(fd
, parg
->buffer
, parg
->count
);
1519 if (n
!= parg
->count
) {
1524 case CHR_IOCTL_PP_EPP_READ
:
1525 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1526 struct ParallelIOArg
*parg
= arg
;
1527 int n
= read(fd
, parg
->buffer
, parg
->count
);
1528 if (n
!= parg
->count
) {
1533 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1534 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1535 struct ParallelIOArg
*parg
= arg
;
1536 int n
= write(fd
, parg
->buffer
, parg
->count
);
1537 if (n
!= parg
->count
) {
1542 case CHR_IOCTL_PP_EPP_WRITE
:
1543 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1544 struct ParallelIOArg
*parg
= arg
;
1545 int n
= write(fd
, parg
->buffer
, parg
->count
);
1546 if (n
!= parg
->count
) {
1557 static void pp_close(CharDriverState
*chr
)
1559 ParallelCharDriver
*drv
= chr
->opaque
;
1562 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1563 ioctl(fd
, PPRELEASE
);
1566 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1569 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1571 CharDriverState
*chr
;
1572 ParallelCharDriver
*drv
;
1574 if (ioctl(fd
, PPCLAIM
) < 0) {
1579 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1581 drv
->mode
= IEEE1284_MODE_COMPAT
;
1583 chr
= g_malloc0(sizeof(CharDriverState
));
1584 chr
->chr_write
= null_chr_write
;
1585 chr
->chr_ioctl
= pp_ioctl
;
1586 chr
->chr_close
= pp_close
;
1589 qemu_chr_be_generic_open(chr
);
1593 #endif /* __linux__ */
1595 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1597 #define HAVE_CHARDEV_PARPORT 1
1599 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1601 int fd
= (int)(intptr_t)chr
->opaque
;
1605 case CHR_IOCTL_PP_READ_DATA
:
1606 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1608 *(uint8_t *)arg
= b
;
1610 case CHR_IOCTL_PP_WRITE_DATA
:
1611 b
= *(uint8_t *)arg
;
1612 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1615 case CHR_IOCTL_PP_READ_CONTROL
:
1616 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1618 *(uint8_t *)arg
= b
;
1620 case CHR_IOCTL_PP_WRITE_CONTROL
:
1621 b
= *(uint8_t *)arg
;
1622 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1625 case CHR_IOCTL_PP_READ_STATUS
:
1626 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1628 *(uint8_t *)arg
= b
;
1636 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1638 CharDriverState
*chr
;
1640 chr
= g_malloc0(sizeof(CharDriverState
));
1641 chr
->opaque
= (void *)(intptr_t)fd
;
1642 chr
->chr_write
= null_chr_write
;
1643 chr
->chr_ioctl
= pp_ioctl
;
1652 HANDLE hcom
, hrecv
, hsend
;
1653 OVERLAPPED orecv
, osend
;
1660 HANDLE hInputReadyEvent
;
1661 HANDLE hInputDoneEvent
;
1662 HANDLE hInputThread
;
1663 uint8_t win_stdio_buf
;
1664 } WinStdioCharState
;
1666 #define NSENDBUF 2048
1667 #define NRECVBUF 2048
1668 #define MAXCONNECT 1
1669 #define NTIMEOUT 5000
1671 static int win_chr_poll(void *opaque
);
1672 static int win_chr_pipe_poll(void *opaque
);
1674 static void win_chr_close(CharDriverState
*chr
)
1676 WinCharState
*s
= chr
->opaque
;
1679 CloseHandle(s
->hsend
);
1683 CloseHandle(s
->hrecv
);
1687 CloseHandle(s
->hcom
);
1691 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1693 qemu_del_polling_cb(win_chr_poll
, chr
);
1695 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1698 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1700 WinCharState
*s
= chr
->opaque
;
1702 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1707 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1709 fprintf(stderr
, "Failed CreateEvent\n");
1712 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1714 fprintf(stderr
, "Failed CreateEvent\n");
1718 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1719 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1720 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1721 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1726 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1727 fprintf(stderr
, "Failed SetupComm\n");
1731 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1732 size
= sizeof(COMMCONFIG
);
1733 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1734 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1735 CommConfigDialog(filename
, NULL
, &comcfg
);
1737 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1738 fprintf(stderr
, "Failed SetCommState\n");
1742 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1743 fprintf(stderr
, "Failed SetCommMask\n");
1747 cto
.ReadIntervalTimeout
= MAXDWORD
;
1748 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1749 fprintf(stderr
, "Failed SetCommTimeouts\n");
1753 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1754 fprintf(stderr
, "Failed ClearCommError\n");
1757 qemu_add_polling_cb(win_chr_poll
, chr
);
1765 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1767 WinCharState
*s
= chr
->opaque
;
1768 DWORD len
, ret
, size
, err
;
1771 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1772 s
->osend
.hEvent
= s
->hsend
;
1775 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1777 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1779 err
= GetLastError();
1780 if (err
== ERROR_IO_PENDING
) {
1781 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1799 static int win_chr_read_poll(CharDriverState
*chr
)
1801 WinCharState
*s
= chr
->opaque
;
1803 s
->max_size
= qemu_chr_be_can_write(chr
);
1807 static void win_chr_readfile(CharDriverState
*chr
)
1809 WinCharState
*s
= chr
->opaque
;
1811 uint8_t buf
[READ_BUF_LEN
];
1814 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1815 s
->orecv
.hEvent
= s
->hrecv
;
1816 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1818 err
= GetLastError();
1819 if (err
== ERROR_IO_PENDING
) {
1820 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1825 qemu_chr_be_write(chr
, buf
, size
);
1829 static void win_chr_read(CharDriverState
*chr
)
1831 WinCharState
*s
= chr
->opaque
;
1833 if (s
->len
> s
->max_size
)
1834 s
->len
= s
->max_size
;
1838 win_chr_readfile(chr
);
1841 static int win_chr_poll(void *opaque
)
1843 CharDriverState
*chr
= opaque
;
1844 WinCharState
*s
= chr
->opaque
;
1848 ClearCommError(s
->hcom
, &comerr
, &status
);
1849 if (status
.cbInQue
> 0) {
1850 s
->len
= status
.cbInQue
;
1851 win_chr_read_poll(chr
);
1858 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1860 CharDriverState
*chr
;
1863 chr
= g_malloc0(sizeof(CharDriverState
));
1864 s
= g_malloc0(sizeof(WinCharState
));
1866 chr
->chr_write
= win_chr_write
;
1867 chr
->chr_close
= win_chr_close
;
1869 if (win_chr_init(chr
, filename
) < 0) {
1874 qemu_chr_be_generic_open(chr
);
1878 static int win_chr_pipe_poll(void *opaque
)
1880 CharDriverState
*chr
= opaque
;
1881 WinCharState
*s
= chr
->opaque
;
1884 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1887 win_chr_read_poll(chr
);
1894 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1896 WinCharState
*s
= chr
->opaque
;
1904 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1906 fprintf(stderr
, "Failed CreateEvent\n");
1909 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1911 fprintf(stderr
, "Failed CreateEvent\n");
1915 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1916 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1917 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1919 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1920 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1921 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1926 ZeroMemory(&ov
, sizeof(ov
));
1927 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1928 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1930 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1934 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1936 fprintf(stderr
, "Failed GetOverlappedResult\n");
1938 CloseHandle(ov
.hEvent
);
1945 CloseHandle(ov
.hEvent
);
1948 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1957 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1959 const char *filename
= opts
->device
;
1960 CharDriverState
*chr
;
1963 chr
= g_malloc0(sizeof(CharDriverState
));
1964 s
= g_malloc0(sizeof(WinCharState
));
1966 chr
->chr_write
= win_chr_write
;
1967 chr
->chr_close
= win_chr_close
;
1969 if (win_chr_pipe_init(chr
, filename
) < 0) {
1974 qemu_chr_be_generic_open(chr
);
1978 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1980 CharDriverState
*chr
;
1983 chr
= g_malloc0(sizeof(CharDriverState
));
1984 s
= g_malloc0(sizeof(WinCharState
));
1987 chr
->chr_write
= win_chr_write
;
1988 qemu_chr_be_generic_open(chr
);
1992 static CharDriverState
*qemu_chr_open_win_con(void)
1994 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1997 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1999 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2006 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2016 static void win_stdio_wait_func(void *opaque
)
2018 CharDriverState
*chr
= opaque
;
2019 WinStdioCharState
*stdio
= chr
->opaque
;
2020 INPUT_RECORD buf
[4];
2025 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
2029 /* Avoid error storm */
2030 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2034 for (i
= 0; i
< dwSize
; i
++) {
2035 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2037 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2039 if (kev
->uChar
.AsciiChar
!= 0) {
2040 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2041 if (qemu_chr_be_can_write(chr
)) {
2042 uint8_t c
= kev
->uChar
.AsciiChar
;
2043 qemu_chr_be_write(chr
, &c
, 1);
2051 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2053 CharDriverState
*chr
= param
;
2054 WinStdioCharState
*stdio
= chr
->opaque
;
2060 /* Wait for one byte */
2061 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2063 /* Exit in case of error, continue if nothing read */
2071 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2072 if (stdio
->win_stdio_buf
== '\r') {
2076 /* Signal the main thread and wait until the byte was eaten */
2077 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2080 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2086 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2090 static void win_stdio_thread_wait_func(void *opaque
)
2092 CharDriverState
*chr
= opaque
;
2093 WinStdioCharState
*stdio
= chr
->opaque
;
2095 if (qemu_chr_be_can_write(chr
)) {
2096 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2099 SetEvent(stdio
->hInputDoneEvent
);
2102 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2104 WinStdioCharState
*stdio
= chr
->opaque
;
2107 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2110 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2112 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2116 static void win_stdio_close(CharDriverState
*chr
)
2118 WinStdioCharState
*stdio
= chr
->opaque
;
2120 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2121 CloseHandle(stdio
->hInputReadyEvent
);
2123 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2124 CloseHandle(stdio
->hInputDoneEvent
);
2126 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2127 TerminateThread(stdio
->hInputThread
, 0);
2130 g_free(chr
->opaque
);
2134 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2136 CharDriverState
*chr
;
2137 WinStdioCharState
*stdio
;
2141 chr
= g_malloc0(sizeof(CharDriverState
));
2142 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2144 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2145 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2146 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2150 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2152 chr
->opaque
= stdio
;
2153 chr
->chr_write
= win_stdio_write
;
2154 chr
->chr_close
= win_stdio_close
;
2157 if (qemu_add_wait_object(stdio
->hStdIn
,
2158 win_stdio_wait_func
, chr
)) {
2159 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2164 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2165 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2166 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2169 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2170 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2171 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2172 fprintf(stderr
, "cannot create stdio thread or event\n");
2175 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2176 win_stdio_thread_wait_func
, chr
)) {
2177 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2181 dwMode
|= ENABLE_LINE_INPUT
;
2184 /* set the terminal in raw mode */
2185 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2186 dwMode
|= ENABLE_PROCESSED_INPUT
;
2189 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2191 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2192 qemu_chr_fe_set_echo(chr
, false);
2196 #endif /* !_WIN32 */
2199 /***********************************************************/
2200 /* UDP Net console */
2206 uint8_t buf
[READ_BUF_LEN
];
2212 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2214 NetCharDriver
*s
= chr
->opaque
;
2215 gsize bytes_written
;
2218 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2219 if (status
== G_IO_STATUS_EOF
) {
2221 } else if (status
!= G_IO_STATUS_NORMAL
) {
2225 return bytes_written
;
2228 static int udp_chr_read_poll(void *opaque
)
2230 CharDriverState
*chr
= opaque
;
2231 NetCharDriver
*s
= chr
->opaque
;
2233 s
->max_size
= qemu_chr_be_can_write(chr
);
2235 /* If there were any stray characters in the queue process them
2238 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2239 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2241 s
->max_size
= qemu_chr_be_can_write(chr
);
2246 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2248 CharDriverState
*chr
= opaque
;
2249 NetCharDriver
*s
= chr
->opaque
;
2250 gsize bytes_read
= 0;
2253 if (s
->max_size
== 0)
2255 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2257 s
->bufcnt
= bytes_read
;
2258 s
->bufptr
= s
->bufcnt
;
2259 if (status
!= G_IO_STATUS_NORMAL
) {
2264 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2265 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2267 s
->max_size
= qemu_chr_be_can_write(chr
);
2273 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2275 NetCharDriver
*s
= chr
->opaque
;
2278 g_source_remove(s
->tag
);
2283 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2287 static void udp_chr_close(CharDriverState
*chr
)
2289 NetCharDriver
*s
= chr
->opaque
;
2291 g_source_remove(s
->tag
);
2294 g_io_channel_unref(s
->chan
);
2298 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2301 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2303 CharDriverState
*chr
= NULL
;
2304 NetCharDriver
*s
= NULL
;
2306 chr
= g_malloc0(sizeof(CharDriverState
));
2307 s
= g_malloc0(sizeof(NetCharDriver
));
2310 s
->chan
= io_channel_from_socket(s
->fd
);
2314 chr
->chr_write
= udp_chr_write
;
2315 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2316 chr
->chr_close
= udp_chr_close
;
2320 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2322 Error
*local_err
= NULL
;
2325 fd
= inet_dgram_opts(opts
, &local_err
);
2329 return qemu_chr_open_udp_fd(fd
);
2332 /***********************************************************/
2333 /* TCP Net console */
2337 GIOChannel
*chan
, *listen_chan
;
2338 guint tag
, listen_tag
;
2348 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2350 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2352 TCPCharDriver
*s
= chr
->opaque
;
2354 return io_channel_send(s
->chan
, buf
, len
);
2356 /* XXX: indicate an error ? */
2361 static int tcp_chr_read_poll(void *opaque
)
2363 CharDriverState
*chr
= opaque
;
2364 TCPCharDriver
*s
= chr
->opaque
;
2367 s
->max_size
= qemu_chr_be_can_write(chr
);
2372 #define IAC_BREAK 243
2373 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2375 uint8_t *buf
, int *size
)
2377 /* Handle any telnet client's basic IAC options to satisfy char by
2378 * char mode with no echo. All IAC options will be removed from
2379 * the buf and the do_telnetopt variable will be used to track the
2380 * state of the width of the IAC information.
2382 * IAC commands come in sets of 3 bytes with the exception of the
2383 * "IAC BREAK" command and the double IAC.
2389 for (i
= 0; i
< *size
; i
++) {
2390 if (s
->do_telnetopt
> 1) {
2391 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2392 /* Double IAC means send an IAC */
2396 s
->do_telnetopt
= 1;
2398 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2399 /* Handle IAC break commands by sending a serial break */
2400 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2405 if (s
->do_telnetopt
>= 4) {
2406 s
->do_telnetopt
= 1;
2409 if ((unsigned char)buf
[i
] == IAC
) {
2410 s
->do_telnetopt
= 2;
2421 static int tcp_get_msgfd(CharDriverState
*chr
)
2423 TCPCharDriver
*s
= chr
->opaque
;
2430 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2432 TCPCharDriver
*s
= chr
->opaque
;
2433 struct cmsghdr
*cmsg
;
2435 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2438 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2439 cmsg
->cmsg_level
!= SOL_SOCKET
||
2440 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2443 fd
= *((int *)CMSG_DATA(cmsg
));
2447 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2450 #ifndef MSG_CMSG_CLOEXEC
2451 qemu_set_cloexec(fd
);
2459 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2461 TCPCharDriver
*s
= chr
->opaque
;
2462 struct msghdr msg
= { NULL
, };
2463 struct iovec iov
[1];
2465 struct cmsghdr cmsg
;
2466 char control
[CMSG_SPACE(sizeof(int))];
2471 iov
[0].iov_base
= buf
;
2472 iov
[0].iov_len
= len
;
2476 msg
.msg_control
= &msg_control
;
2477 msg
.msg_controllen
= sizeof(msg_control
);
2479 #ifdef MSG_CMSG_CLOEXEC
2480 flags
|= MSG_CMSG_CLOEXEC
;
2482 ret
= recvmsg(s
->fd
, &msg
, flags
);
2483 if (ret
> 0 && s
->is_unix
) {
2484 unix_process_msgfd(chr
, &msg
);
2490 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2492 TCPCharDriver
*s
= chr
->opaque
;
2493 return qemu_recv(s
->fd
, buf
, len
, 0);
2497 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2499 TCPCharDriver
*s
= chr
->opaque
;
2500 return g_io_create_watch(s
->chan
, cond
);
2503 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2505 CharDriverState
*chr
= opaque
;
2506 TCPCharDriver
*s
= chr
->opaque
;
2507 uint8_t buf
[READ_BUF_LEN
];
2510 if (!s
->connected
|| s
->max_size
<= 0) {
2514 if (len
> s
->max_size
)
2516 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2518 /* connection closed */
2520 if (s
->listen_chan
) {
2521 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2523 g_source_remove(s
->tag
);
2525 g_io_channel_unref(s
->chan
);
2529 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2530 } else if (size
> 0) {
2531 if (s
->do_telnetopt
)
2532 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2534 qemu_chr_be_write(chr
, buf
, size
);
2541 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2543 return qemu_chr_open_fd(eventfd
, eventfd
);
2547 static void tcp_chr_connect(void *opaque
)
2549 CharDriverState
*chr
= opaque
;
2550 TCPCharDriver
*s
= chr
->opaque
;
2554 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2556 qemu_chr_be_generic_open(chr
);
2559 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2560 static void tcp_chr_telnet_init(int fd
)
2563 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2564 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2565 send(fd
, (char *)buf
, 3, 0);
2566 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2567 send(fd
, (char *)buf
, 3, 0);
2568 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2569 send(fd
, (char *)buf
, 3, 0);
2570 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2571 send(fd
, (char *)buf
, 3, 0);
2574 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2576 TCPCharDriver
*s
= chr
->opaque
;
2580 qemu_set_nonblock(fd
);
2582 socket_set_nodelay(fd
);
2584 s
->chan
= io_channel_from_socket(fd
);
2585 g_source_remove(s
->listen_tag
);
2587 tcp_chr_connect(chr
);
2592 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2594 CharDriverState
*chr
= opaque
;
2595 TCPCharDriver
*s
= chr
->opaque
;
2596 struct sockaddr_in saddr
;
2598 struct sockaddr_un uaddr
;
2600 struct sockaddr
*addr
;
2607 len
= sizeof(uaddr
);
2608 addr
= (struct sockaddr
*)&uaddr
;
2612 len
= sizeof(saddr
);
2613 addr
= (struct sockaddr
*)&saddr
;
2615 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2616 if (fd
< 0 && errno
!= EINTR
) {
2618 } else if (fd
>= 0) {
2619 if (s
->do_telnetopt
)
2620 tcp_chr_telnet_init(fd
);
2624 if (tcp_chr_add_client(chr
, fd
) < 0)
2630 static void tcp_chr_close(CharDriverState
*chr
)
2632 TCPCharDriver
*s
= chr
->opaque
;
2635 g_source_remove(s
->tag
);
2638 g_io_channel_unref(s
->chan
);
2642 if (s
->listen_fd
>= 0) {
2643 if (s
->listen_tag
) {
2644 g_source_remove(s
->listen_tag
);
2646 if (s
->listen_chan
) {
2647 g_io_channel_unref(s
->listen_chan
);
2649 closesocket(s
->listen_fd
);
2652 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2655 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2656 bool is_listen
, bool is_telnet
,
2657 bool is_waitconnect
,
2660 CharDriverState
*chr
= NULL
;
2661 TCPCharDriver
*s
= NULL
;
2662 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2663 const char *left
= "", *right
= "";
2664 struct sockaddr_storage ss
;
2665 socklen_t ss_len
= sizeof(ss
);
2667 memset(&ss
, 0, ss_len
);
2668 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2669 error_setg(errp
, "getsockname: %s", strerror(errno
));
2673 chr
= g_malloc0(sizeof(CharDriverState
));
2674 s
= g_malloc0(sizeof(TCPCharDriver
));
2681 chr
->filename
= g_malloc(256);
2682 switch (ss
.ss_family
) {
2686 snprintf(chr
->filename
, 256, "unix:%s%s",
2687 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2688 is_listen
? ",server" : "");
2696 s
->do_nodelay
= do_nodelay
;
2697 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2698 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2699 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2700 is_telnet
? "telnet" : "tcp",
2701 left
, host
, right
, serv
,
2702 is_listen
? ",server" : "");
2707 chr
->chr_write
= tcp_chr_write
;
2708 chr
->chr_close
= tcp_chr_close
;
2709 chr
->get_msgfd
= tcp_get_msgfd
;
2710 chr
->chr_add_client
= tcp_chr_add_client
;
2711 chr
->chr_add_watch
= tcp_chr_add_watch
;
2715 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2716 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2718 s
->do_telnetopt
= 1;
2723 socket_set_nodelay(fd
);
2724 s
->chan
= io_channel_from_socket(s
->fd
);
2725 tcp_chr_connect(chr
);
2728 if (is_listen
&& is_waitconnect
) {
2729 printf("QEMU waiting for connection on: %s\n",
2731 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2732 qemu_set_nonblock(s
->listen_fd
);
2737 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2739 CharDriverState
*chr
= NULL
;
2740 Error
*local_err
= NULL
;
2748 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2749 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2750 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2751 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2752 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2758 fd
= unix_listen_opts(opts
, &local_err
);
2760 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2764 fd
= inet_listen_opts(opts
, 0, &local_err
);
2766 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2773 if (!is_waitconnect
)
2774 qemu_set_nonblock(fd
);
2776 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2777 is_waitconnect
, &local_err
);
2778 if (error_is_set(&local_err
)) {
2786 qerror_report_err(local_err
);
2787 error_free(local_err
);
2793 g_free(chr
->opaque
);
2799 /***********************************************************/
2800 /* Memory chardev */
2803 size_t outbuf_capacity
;
2807 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2809 MemoryDriver
*d
= chr
->opaque
;
2811 /* TODO: the QString implementation has the same code, we should
2812 * introduce a generic way to do this in cutils.c */
2813 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2815 d
->outbuf_capacity
+= len
;
2816 d
->outbuf_capacity
*= 2;
2817 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2820 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2821 d
->outbuf_size
+= len
;
2826 void qemu_chr_init_mem(CharDriverState
*chr
)
2830 d
= g_malloc(sizeof(*d
));
2832 d
->outbuf_capacity
= 4096;
2833 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2835 memset(chr
, 0, sizeof(*chr
));
2837 chr
->chr_write
= mem_chr_write
;
2840 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2842 MemoryDriver
*d
= chr
->opaque
;
2843 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2846 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2847 void qemu_chr_close_mem(CharDriverState
*chr
)
2849 MemoryDriver
*d
= chr
->opaque
;
2852 g_free(chr
->opaque
);
2854 chr
->chr_write
= NULL
;
2857 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2859 const MemoryDriver
*d
= chr
->opaque
;
2860 return d
->outbuf_size
;
2863 /*********************************************************/
2864 /* Ring buffer chardev */
2871 } RingBufCharDriver
;
2873 static size_t ringbuf_count(const CharDriverState
*chr
)
2875 const RingBufCharDriver
*d
= chr
->opaque
;
2877 return d
->prod
- d
->cons
;
2880 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2882 RingBufCharDriver
*d
= chr
->opaque
;
2885 if (!buf
|| (len
< 0)) {
2889 for (i
= 0; i
< len
; i
++ ) {
2890 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2891 if (d
->prod
- d
->cons
> d
->size
) {
2892 d
->cons
= d
->prod
- d
->size
;
2899 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2901 RingBufCharDriver
*d
= chr
->opaque
;
2904 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2905 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2911 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2913 RingBufCharDriver
*d
= chr
->opaque
;
2920 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2923 CharDriverState
*chr
;
2924 RingBufCharDriver
*d
;
2926 chr
= g_malloc0(sizeof(CharDriverState
));
2927 d
= g_malloc(sizeof(*d
));
2929 d
->size
= opts
->has_size
? opts
->size
: 65536;
2931 /* The size must be power of 2 */
2932 if (d
->size
& (d
->size
- 1)) {
2933 error_setg(errp
, "size of ringbuf chardev must be power of two");
2939 d
->cbuf
= g_malloc0(d
->size
);
2942 chr
->chr_write
= ringbuf_chr_write
;
2943 chr
->chr_close
= ringbuf_chr_close
;
2953 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2955 return chr
->chr_write
== ringbuf_chr_write
;
2958 void qmp_ringbuf_write(const char *device
, const char *data
,
2959 bool has_format
, enum DataFormat format
,
2962 CharDriverState
*chr
;
2963 const uint8_t *write_data
;
2967 chr
= qemu_chr_find(device
);
2969 error_setg(errp
, "Device '%s' not found", device
);
2973 if (!chr_is_ringbuf(chr
)) {
2974 error_setg(errp
,"%s is not a ringbuf device", device
);
2978 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2979 write_data
= g_base64_decode(data
, &write_count
);
2981 write_data
= (uint8_t *)data
;
2982 write_count
= strlen(data
);
2985 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2987 if (write_data
!= (uint8_t *)data
) {
2988 g_free((void *)write_data
);
2992 error_setg(errp
, "Failed to write to device %s", device
);
2997 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2998 bool has_format
, enum DataFormat format
,
3001 CharDriverState
*chr
;
3006 chr
= qemu_chr_find(device
);
3008 error_setg(errp
, "Device '%s' not found", device
);
3012 if (!chr_is_ringbuf(chr
)) {
3013 error_setg(errp
,"%s is not a ringbuf device", device
);
3018 error_setg(errp
, "size must be greater than zero");
3022 count
= ringbuf_count(chr
);
3023 size
= size
> count
? count
: size
;
3024 read_data
= g_malloc(size
+ 1);
3026 ringbuf_chr_read(chr
, read_data
, size
);
3028 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3029 data
= g_base64_encode(read_data
, size
);
3033 * FIXME should read only complete, valid UTF-8 characters up
3034 * to @size bytes. Invalid sequences should be replaced by a
3035 * suitable replacement character. Except when (and only
3036 * when) ring buffer lost characters since last read, initial
3037 * continuation characters should be dropped.
3039 read_data
[size
] = 0;
3040 data
= (char *)read_data
;
3046 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3048 char host
[65], port
[33], width
[8], height
[8];
3052 Error
*local_err
= NULL
;
3054 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3055 if (error_is_set(&local_err
)) {
3056 qerror_report_err(local_err
);
3057 error_free(local_err
);
3061 if (strstart(filename
, "mon:", &p
)) {
3063 qemu_opt_set(opts
, "mux", "on");
3066 if (strcmp(filename
, "null") == 0 ||
3067 strcmp(filename
, "pty") == 0 ||
3068 strcmp(filename
, "msmouse") == 0 ||
3069 strcmp(filename
, "braille") == 0 ||
3070 strcmp(filename
, "stdio") == 0) {
3071 qemu_opt_set(opts
, "backend", filename
);
3074 if (strstart(filename
, "vc", &p
)) {
3075 qemu_opt_set(opts
, "backend", "vc");
3077 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
3079 qemu_opt_set(opts
, "width", width
);
3080 qemu_opt_set(opts
, "height", height
);
3081 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3083 qemu_opt_set(opts
, "cols", width
);
3084 qemu_opt_set(opts
, "rows", height
);
3091 if (strcmp(filename
, "con:") == 0) {
3092 qemu_opt_set(opts
, "backend", "console");
3095 if (strstart(filename
, "COM", NULL
)) {
3096 qemu_opt_set(opts
, "backend", "serial");
3097 qemu_opt_set(opts
, "path", filename
);
3100 if (strstart(filename
, "file:", &p
)) {
3101 qemu_opt_set(opts
, "backend", "file");
3102 qemu_opt_set(opts
, "path", p
);
3105 if (strstart(filename
, "pipe:", &p
)) {
3106 qemu_opt_set(opts
, "backend", "pipe");
3107 qemu_opt_set(opts
, "path", p
);
3110 if (strstart(filename
, "tcp:", &p
) ||
3111 strstart(filename
, "telnet:", &p
)) {
3112 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3114 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3117 qemu_opt_set(opts
, "backend", "socket");
3118 qemu_opt_set(opts
, "host", host
);
3119 qemu_opt_set(opts
, "port", port
);
3120 if (p
[pos
] == ',') {
3121 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3124 if (strstart(filename
, "telnet:", &p
))
3125 qemu_opt_set(opts
, "telnet", "on");
3128 if (strstart(filename
, "udp:", &p
)) {
3129 qemu_opt_set(opts
, "backend", "udp");
3130 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3132 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3136 qemu_opt_set(opts
, "host", host
);
3137 qemu_opt_set(opts
, "port", port
);
3138 if (p
[pos
] == '@') {
3140 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3142 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3146 qemu_opt_set(opts
, "localaddr", host
);
3147 qemu_opt_set(opts
, "localport", port
);
3151 if (strstart(filename
, "unix:", &p
)) {
3152 qemu_opt_set(opts
, "backend", "socket");
3153 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3157 if (strstart(filename
, "/dev/parport", NULL
) ||
3158 strstart(filename
, "/dev/ppi", NULL
)) {
3159 qemu_opt_set(opts
, "backend", "parport");
3160 qemu_opt_set(opts
, "path", filename
);
3163 if (strstart(filename
, "/dev/", NULL
)) {
3164 qemu_opt_set(opts
, "backend", "tty");
3165 qemu_opt_set(opts
, "path", filename
);
3170 qemu_opts_del(opts
);
3174 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3177 const char *path
= qemu_opt_get(opts
, "path");
3180 error_setg(errp
, "chardev: file: no filename given");
3183 backend
->file
= g_new0(ChardevFile
, 1);
3184 backend
->file
->out
= g_strdup(path
);
3187 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3190 backend
->stdio
= g_new0(ChardevStdio
, 1);
3191 backend
->stdio
->has_signal
= true;
3192 backend
->stdio
->signal
=
3193 qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
);
3196 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3199 const char *device
= qemu_opt_get(opts
, "path");
3201 if (device
== NULL
) {
3202 error_setg(errp
, "chardev: serial/tty: no device path given");
3205 backend
->serial
= g_new0(ChardevHostdev
, 1);
3206 backend
->serial
->device
= g_strdup(device
);
3209 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3212 const char *device
= qemu_opt_get(opts
, "path");
3214 if (device
== NULL
) {
3215 error_setg(errp
, "chardev: parallel: no device path given");
3218 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3219 backend
->parallel
->device
= g_strdup(device
);
3222 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3225 const char *device
= qemu_opt_get(opts
, "path");
3227 if (device
== NULL
) {
3228 error_setg(errp
, "chardev: pipe: no device path given");
3231 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3232 backend
->pipe
->device
= g_strdup(device
);
3235 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3240 backend
->memory
= g_new0(ChardevRingbuf
, 1);
3242 val
= qemu_opt_get_number(opts
, "size", 0);
3244 backend
->memory
->has_size
= true;
3245 backend
->memory
->size
= val
;
3249 typedef struct CharDriver
{
3252 CharDriverState
*(*open
)(QemuOpts
*opts
);
3253 /* new, qapi-based */
3255 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3258 static GSList
*backends
;
3260 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3264 s
= g_malloc0(sizeof(*s
));
3265 s
->name
= g_strdup(name
);
3268 backends
= g_slist_append(backends
, s
);
3271 void register_char_driver_qapi(const char *name
, int kind
,
3272 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3276 s
= g_malloc0(sizeof(*s
));
3277 s
->name
= g_strdup(name
);
3281 backends
= g_slist_append(backends
, s
);
3284 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3285 void (*init
)(struct CharDriverState
*s
),
3289 CharDriverState
*chr
;
3292 if (qemu_opts_id(opts
) == NULL
) {
3293 error_setg(errp
, "chardev: no id specified");
3297 if (qemu_opt_get(opts
, "backend") == NULL
) {
3298 error_setg(errp
, "chardev: \"%s\" missing backend",
3299 qemu_opts_id(opts
));
3302 for (i
= backends
; i
; i
= i
->next
) {
3305 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3310 error_setg(errp
, "chardev: backend \"%s\" not found",
3311 qemu_opt_get(opts
, "backend"));
3316 /* using new, qapi init */
3317 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3318 ChardevReturn
*ret
= NULL
;
3319 const char *id
= qemu_opts_id(opts
);
3320 const char *bid
= NULL
;
3322 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3323 bid
= g_strdup_printf("%s-base", id
);
3327 backend
->kind
= cd
->kind
;
3329 cd
->parse(opts
, backend
, errp
);
3330 if (error_is_set(errp
)) {
3334 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3335 if (error_is_set(errp
)) {
3340 qapi_free_ChardevBackend(backend
);
3341 qapi_free_ChardevReturn(ret
);
3342 backend
= g_new0(ChardevBackend
, 1);
3343 backend
->mux
= g_new0(ChardevMux
, 1);
3344 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3345 backend
->mux
->chardev
= g_strdup(bid
);
3346 ret
= qmp_chardev_add(id
, backend
, errp
);
3347 if (error_is_set(errp
)) {
3352 chr
= qemu_chr_find(id
);
3355 qapi_free_ChardevBackend(backend
);
3356 qapi_free_ChardevReturn(ret
);
3360 chr
= cd
->open(opts
);
3362 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3363 qemu_opt_get(opts
, "backend"));
3368 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3370 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3372 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3373 CharDriverState
*base
= chr
;
3374 int len
= strlen(qemu_opts_id(opts
)) + 6;
3375 base
->label
= g_malloc(len
);
3376 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3377 chr
= qemu_chr_open_mux(base
);
3378 chr
->filename
= base
->filename
;
3379 chr
->avail_connections
= MAX_MUX
;
3380 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3382 chr
->avail_connections
= 1;
3384 chr
->label
= g_strdup(qemu_opts_id(opts
));
3389 qemu_opts_del(opts
);
3393 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3396 CharDriverState
*chr
;
3400 if (strstart(filename
, "chardev:", &p
)) {
3401 return qemu_chr_find(p
);
3404 opts
= qemu_chr_parse_compat(label
, filename
);
3408 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3409 if (error_is_set(&err
)) {
3410 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3413 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3414 qemu_chr_fe_claim_no_fail(chr
);
3415 monitor_init(chr
, MONITOR_USE_READLINE
);
3420 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3422 if (chr
->chr_set_echo
) {
3423 chr
->chr_set_echo(chr
, echo
);
3427 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3429 if (chr
->fe_open
== fe_open
) {
3432 chr
->fe_open
= fe_open
;
3433 if (chr
->chr_set_fe_open
) {
3434 chr
->chr_set_fe_open(chr
, fe_open
);
3438 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3439 GIOFunc func
, void *user_data
)
3444 if (s
->chr_add_watch
== NULL
) {
3448 src
= s
->chr_add_watch(s
, cond
);
3449 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3450 tag
= g_source_attach(src
, NULL
);
3451 g_source_unref(src
);
3456 int qemu_chr_fe_claim(CharDriverState
*s
)
3458 if (s
->avail_connections
< 1) {
3461 s
->avail_connections
--;
3465 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3467 if (qemu_chr_fe_claim(s
) != 0) {
3468 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3469 __func__
, s
->label
);
3474 void qemu_chr_fe_release(CharDriverState
*s
)
3476 s
->avail_connections
++;
3479 void qemu_chr_delete(CharDriverState
*chr
)
3481 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3482 if (chr
->chr_close
) {
3483 chr
->chr_close(chr
);
3485 g_free(chr
->filename
);
3488 qemu_opts_del(chr
->opts
);
3493 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3495 ChardevInfoList
*chr_list
= NULL
;
3496 CharDriverState
*chr
;
3498 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3499 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3500 info
->value
= g_malloc0(sizeof(*info
->value
));
3501 info
->value
->label
= g_strdup(chr
->label
);
3502 info
->value
->filename
= g_strdup(chr
->filename
);
3504 info
->next
= chr_list
;
3511 CharDriverState
*qemu_chr_find(const char *name
)
3513 CharDriverState
*chr
;
3515 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3516 if (strcmp(chr
->label
, name
) != 0)
3523 /* Get a character (serial) device interface. */
3524 CharDriverState
*qemu_char_get_next_serial(void)
3526 static int next_serial
;
3527 CharDriverState
*chr
;
3529 /* FIXME: This function needs to go away: use chardev properties! */
3531 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3532 chr
= serial_hds
[next_serial
++];
3533 qemu_chr_fe_claim_no_fail(chr
);
3539 QemuOptsList qemu_chardev_opts
= {
3541 .implied_opt_name
= "backend",
3542 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3546 .type
= QEMU_OPT_STRING
,
3549 .type
= QEMU_OPT_STRING
,
3552 .type
= QEMU_OPT_STRING
,
3555 .type
= QEMU_OPT_STRING
,
3557 .name
= "localaddr",
3558 .type
= QEMU_OPT_STRING
,
3560 .name
= "localport",
3561 .type
= QEMU_OPT_STRING
,
3564 .type
= QEMU_OPT_NUMBER
,
3567 .type
= QEMU_OPT_BOOL
,
3570 .type
= QEMU_OPT_BOOL
,
3573 .type
= QEMU_OPT_BOOL
,
3576 .type
= QEMU_OPT_BOOL
,
3579 .type
= QEMU_OPT_BOOL
,
3582 .type
= QEMU_OPT_BOOL
,
3585 .type
= QEMU_OPT_NUMBER
,
3588 .type
= QEMU_OPT_NUMBER
,
3591 .type
= QEMU_OPT_NUMBER
,
3594 .type
= QEMU_OPT_NUMBER
,
3597 .type
= QEMU_OPT_BOOL
,
3600 .type
= QEMU_OPT_BOOL
,
3603 .type
= QEMU_OPT_STRING
,
3606 .type
= QEMU_OPT_NUMBER
,
3609 .type
= QEMU_OPT_SIZE
,
3611 { /* end of list */ }
3617 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3622 error_setg(errp
, "input file not supported");
3626 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3627 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3628 if (out
== INVALID_HANDLE_VALUE
) {
3629 error_setg(errp
, "open %s failed", file
->out
);
3632 return qemu_chr_open_win_file(out
);
3635 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3638 return qemu_chr_open_win_path(serial
->device
);
3641 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3644 error_setg(errp
, "character device backend type 'parallel' not supported");
3650 static int qmp_chardev_open_file_source(char *src
, int flags
,
3655 TFR(fd
= qemu_open(src
, flags
, 0666));
3657 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3662 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3664 int flags
, in
= -1, out
= -1;
3666 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3667 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3668 if (error_is_set(errp
)) {
3674 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3675 if (error_is_set(errp
)) {
3681 return qemu_chr_open_fd(in
, out
);
3684 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3687 #ifdef HAVE_CHARDEV_TTY
3690 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3691 if (error_is_set(errp
)) {
3694 qemu_set_nonblock(fd
);
3695 return qemu_chr_open_tty_fd(fd
);
3697 error_setg(errp
, "character device backend type 'serial' not supported");
3702 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3705 #ifdef HAVE_CHARDEV_PARPORT
3708 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3709 if (error_is_set(errp
)) {
3712 return qemu_chr_open_pp_fd(fd
);
3714 error_setg(errp
, "character device backend type 'parallel' not supported");
3721 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3724 SocketAddress
*addr
= sock
->addr
;
3725 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3726 bool is_listen
= sock
->has_server
? sock
->server
: true;
3727 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3728 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3732 fd
= socket_listen(addr
, errp
);
3734 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3736 if (error_is_set(errp
)) {
3739 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3740 is_telnet
, is_waitconnect
, errp
);
3743 static CharDriverState
*qmp_chardev_open_dgram(ChardevDgram
*dgram
,
3748 fd
= socket_dgram(dgram
->remote
, dgram
->local
, errp
);
3749 if (error_is_set(errp
)) {
3752 return qemu_chr_open_udp_fd(fd
);
3755 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3758 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3759 CharDriverState
*base
, *chr
= NULL
;
3761 chr
= qemu_chr_find(id
);
3763 error_setg(errp
, "Chardev '%s' already exists", id
);
3768 switch (backend
->kind
) {
3769 case CHARDEV_BACKEND_KIND_FILE
:
3770 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3772 case CHARDEV_BACKEND_KIND_SERIAL
:
3773 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3775 case CHARDEV_BACKEND_KIND_PARALLEL
:
3776 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3778 case CHARDEV_BACKEND_KIND_PIPE
:
3779 chr
= qemu_chr_open_pipe(backend
->pipe
);
3781 case CHARDEV_BACKEND_KIND_SOCKET
:
3782 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3784 case CHARDEV_BACKEND_KIND_DGRAM
:
3785 chr
= qmp_chardev_open_dgram(backend
->dgram
, errp
);
3787 #ifdef HAVE_CHARDEV_TTY
3788 case CHARDEV_BACKEND_KIND_PTY
:
3789 chr
= qemu_chr_open_pty(id
, ret
);
3792 case CHARDEV_BACKEND_KIND_NULL
:
3793 chr
= qemu_chr_open_null();
3795 case CHARDEV_BACKEND_KIND_MUX
:
3796 base
= qemu_chr_find(backend
->mux
->chardev
);
3798 error_setg(errp
, "mux: base chardev %s not found",
3799 backend
->mux
->chardev
);
3802 chr
= qemu_chr_open_mux(base
);
3804 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3805 chr
= qemu_chr_open_msmouse();
3807 #ifdef CONFIG_BRLAPI
3808 case CHARDEV_BACKEND_KIND_BRAILLE
:
3809 chr
= chr_baum_init();
3812 case CHARDEV_BACKEND_KIND_STDIO
:
3813 chr
= qemu_chr_open_stdio(backend
->stdio
);
3816 case CHARDEV_BACKEND_KIND_CONSOLE
:
3817 chr
= qemu_chr_open_win_con();
3821 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3822 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3824 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3825 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3828 case CHARDEV_BACKEND_KIND_VC
:
3829 chr
= vc_init(backend
->vc
);
3831 case CHARDEV_BACKEND_KIND_MEMORY
:
3832 chr
= qemu_chr_open_ringbuf(backend
->memory
, errp
);
3835 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3839 if (chr
== NULL
&& !error_is_set(errp
)) {
3840 error_setg(errp
, "Failed to create chardev");
3843 chr
->label
= g_strdup(id
);
3844 chr
->avail_connections
=
3845 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3846 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3854 void qmp_chardev_remove(const char *id
, Error
**errp
)
3856 CharDriverState
*chr
;
3858 chr
= qemu_chr_find(id
);
3860 error_setg(errp
, "Chardev '%s' not found", id
);
3863 if (chr
->chr_can_read
|| chr
->chr_read
||
3864 chr
->chr_event
|| chr
->handler_opaque
) {
3865 error_setg(errp
, "Chardev '%s' is busy", id
);
3868 qemu_chr_delete(chr
);
3871 static void register_types(void)
3873 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3874 register_char_driver("socket", qemu_chr_open_socket
);
3875 register_char_driver("udp", qemu_chr_open_udp
);
3876 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3877 qemu_chr_parse_ringbuf
);
3878 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3879 qemu_chr_parse_file_out
);
3880 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3881 qemu_chr_parse_stdio
);
3882 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3883 qemu_chr_parse_serial
);
3884 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3885 qemu_chr_parse_serial
);
3886 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3887 qemu_chr_parse_parallel
);
3888 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3889 qemu_chr_parse_parallel
);
3890 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3891 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3892 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3893 qemu_chr_parse_pipe
);
3896 type_init(register_types
);