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"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
33 #include "hw/msmouse.h"
34 #include "qmp-commands.h"
44 #include <sys/times.h>
48 #include <sys/ioctl.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
53 #include <arpa/inet.h>
56 #include <sys/select.h>
59 #if defined(__GLIBC__)
61 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
66 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
67 #include <dev/ppbus/ppi.h>
68 #include <dev/ppbus/ppbconf.h>
69 #elif defined(__DragonFly__)
70 #include <dev/misc/ppi/ppi.h>
71 #include <bus/ppbus/ppbconf.h>
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
98 #include "qemu_socket.h"
99 #include "ui/qemu-spice.h"
101 #define READ_BUF_LEN 4096
103 /***********************************************************/
104 /* character device */
106 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
107 QTAILQ_HEAD_INITIALIZER(chardevs
);
109 void qemu_chr_be_event(CharDriverState
*s
, int event
)
111 /* Keep track if the char device is open */
113 case CHR_EVENT_OPENED
:
116 case CHR_EVENT_CLOSED
:
123 s
->chr_event(s
->handler_opaque
, event
);
126 static void qemu_chr_fire_open_event(void *opaque
)
128 CharDriverState
*s
= opaque
;
129 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
130 qemu_free_timer(s
->open_timer
);
131 s
->open_timer
= NULL
;
134 void qemu_chr_generic_open(CharDriverState
*s
)
136 if (s
->open_timer
== NULL
) {
137 s
->open_timer
= qemu_new_timer_ms(rt_clock
,
138 qemu_chr_fire_open_event
, s
);
139 qemu_mod_timer(s
->open_timer
, qemu_get_clock_ms(rt_clock
) - 1);
143 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
145 return s
->chr_write(s
, buf
, len
);
148 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
152 return s
->chr_ioctl(s
, cmd
, arg
);
155 int qemu_chr_be_can_write(CharDriverState
*s
)
157 if (!s
->chr_can_read
)
159 return s
->chr_can_read(s
->handler_opaque
);
162 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
165 s
->chr_read(s
->handler_opaque
, buf
, len
);
169 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
171 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
174 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
176 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
179 void qemu_chr_accept_input(CharDriverState
*s
)
181 if (s
->chr_accept_input
)
182 s
->chr_accept_input(s
);
186 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
188 char buf
[READ_BUF_LEN
];
191 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
192 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
196 void qemu_chr_add_handlers(CharDriverState
*s
,
197 IOCanReadHandler
*fd_can_read
,
198 IOReadHandler
*fd_read
,
199 IOEventHandler
*fd_event
,
202 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
203 /* chr driver being released. */
204 ++s
->avail_connections
;
206 s
->chr_can_read
= fd_can_read
;
207 s
->chr_read
= fd_read
;
208 s
->chr_event
= fd_event
;
209 s
->handler_opaque
= opaque
;
210 if (s
->chr_update_read_handler
)
211 s
->chr_update_read_handler(s
);
213 /* We're connecting to an already opened device, so let's make sure we
214 also get the open event */
216 qemu_chr_generic_open(s
);
220 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
225 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
227 CharDriverState
*chr
;
229 chr
= g_malloc0(sizeof(CharDriverState
));
230 chr
->chr_write
= null_chr_write
;
234 /* MUX driver for serial I/O splitting */
236 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
237 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
239 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
240 IOReadHandler
*chr_read
[MAX_MUX
];
241 IOEventHandler
*chr_event
[MAX_MUX
];
242 void *ext_opaque
[MAX_MUX
];
243 CharDriverState
*drv
;
248 /* Intermediate input buffer allows to catch escape sequences even if the
249 currently active device is not accepting any input - but only until it
251 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
256 int64_t timestamps_start
;
260 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
262 MuxDriver
*d
= chr
->opaque
;
264 if (!d
->timestamps
) {
265 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
270 for (i
= 0; i
< len
; i
++) {
276 ti
= qemu_get_clock_ms(rt_clock
);
277 if (d
->timestamps_start
== -1)
278 d
->timestamps_start
= ti
;
279 ti
-= d
->timestamps_start
;
281 snprintf(buf1
, sizeof(buf1
),
282 "[%02d:%02d:%02d.%03d] ",
287 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
290 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
291 if (buf
[i
] == '\n') {
299 static const char * const mux_help
[] = {
300 "% h print this help\n\r",
301 "% x exit emulator\n\r",
302 "% s save disk data back to file (if -snapshot)\n\r",
303 "% t toggle console timestamps\n\r"
304 "% b send break (magic sysrq)\n\r",
305 "% c switch between console and monitor\n\r",
310 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
311 static void mux_print_help(CharDriverState
*chr
)
314 char ebuf
[15] = "Escape-Char";
315 char cbuf
[50] = "\n\r";
317 if (term_escape_char
> 0 && term_escape_char
< 26) {
318 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
319 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
321 snprintf(cbuf
, sizeof(cbuf
),
322 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
325 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
326 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
327 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
328 if (mux_help
[i
][j
] == '%')
329 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
331 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
336 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
338 if (d
->chr_event
[mux_nr
])
339 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
342 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
344 if (d
->term_got_escape
) {
345 d
->term_got_escape
= 0;
346 if (ch
== term_escape_char
)
355 const char *term
= "QEMU: Terminated\n\r";
356 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
364 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
367 /* Switch to the next registered device */
368 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
370 if (d
->focus
>= d
->mux_cnt
)
372 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
375 d
->timestamps
= !d
->timestamps
;
376 d
->timestamps_start
= -1;
380 } else if (ch
== term_escape_char
) {
381 d
->term_got_escape
= 1;
389 static void mux_chr_accept_input(CharDriverState
*chr
)
391 MuxDriver
*d
= chr
->opaque
;
394 while (d
->prod
[m
] != d
->cons
[m
] &&
395 d
->chr_can_read
[m
] &&
396 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
397 d
->chr_read
[m
](d
->ext_opaque
[m
],
398 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
402 static int mux_chr_can_read(void *opaque
)
404 CharDriverState
*chr
= opaque
;
405 MuxDriver
*d
= chr
->opaque
;
408 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
410 if (d
->chr_can_read
[m
])
411 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
415 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
417 CharDriverState
*chr
= opaque
;
418 MuxDriver
*d
= chr
->opaque
;
422 mux_chr_accept_input (opaque
);
424 for(i
= 0; i
< size
; i
++)
425 if (mux_proc_byte(chr
, d
, buf
[i
])) {
426 if (d
->prod
[m
] == d
->cons
[m
] &&
427 d
->chr_can_read
[m
] &&
428 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
429 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
431 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
435 static void mux_chr_event(void *opaque
, int event
)
437 CharDriverState
*chr
= opaque
;
438 MuxDriver
*d
= chr
->opaque
;
441 /* Send the event to all registered listeners */
442 for (i
= 0; i
< d
->mux_cnt
; i
++)
443 mux_chr_send_event(d
, i
, event
);
446 static void mux_chr_update_read_handler(CharDriverState
*chr
)
448 MuxDriver
*d
= chr
->opaque
;
450 if (d
->mux_cnt
>= MAX_MUX
) {
451 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
454 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
455 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
456 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
457 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
458 /* Fix up the real driver with mux routines */
459 if (d
->mux_cnt
== 0) {
460 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
463 if (d
->focus
!= -1) {
464 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
466 d
->focus
= d
->mux_cnt
;
468 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
471 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
473 CharDriverState
*chr
;
476 chr
= g_malloc0(sizeof(CharDriverState
));
477 d
= g_malloc0(sizeof(MuxDriver
));
482 chr
->chr_write
= mux_chr_write
;
483 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
484 chr
->chr_accept_input
= mux_chr_accept_input
;
485 /* Frontend guest-open / -close notification is not support with muxes */
486 chr
->chr_guest_open
= NULL
;
487 chr
->chr_guest_close
= NULL
;
489 /* Muxes are always open on creation */
490 qemu_chr_generic_open(chr
);
497 int send_all(int fd
, const void *buf
, int len1
)
503 ret
= send(fd
, buf
, len
, 0);
505 errno
= WSAGetLastError();
506 if (errno
!= WSAEWOULDBLOCK
) {
509 } else if (ret
== 0) {
521 int send_all(int fd
, const void *_buf
, int len1
)
524 const uint8_t *buf
= _buf
;
528 ret
= write(fd
, buf
, len
);
530 if (errno
!= EINTR
&& errno
!= EAGAIN
)
532 } else if (ret
== 0) {
543 #define STDIO_MAX_CLIENTS 1
544 static int stdio_nb_clients
;
554 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
556 FDCharDriver
*s
= chr
->opaque
;
557 return send_all(s
->fd_out
, buf
, len
);
560 static int fd_chr_read_poll(void *opaque
)
562 CharDriverState
*chr
= opaque
;
563 FDCharDriver
*s
= chr
->opaque
;
565 s
->max_size
= qemu_chr_be_can_write(chr
);
569 static void fd_chr_read(void *opaque
)
571 CharDriverState
*chr
= opaque
;
572 FDCharDriver
*s
= chr
->opaque
;
574 uint8_t buf
[READ_BUF_LEN
];
577 if (len
> s
->max_size
)
581 size
= read(s
->fd_in
, buf
, len
);
583 /* FD has been closed. Remove it from the active list. */
584 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
585 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
589 qemu_chr_be_write(chr
, buf
, size
);
593 static void fd_chr_update_read_handler(CharDriverState
*chr
)
595 FDCharDriver
*s
= chr
->opaque
;
598 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
600 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
601 fd_chr_read
, NULL
, chr
);
606 static void fd_chr_close(struct CharDriverState
*chr
)
608 FDCharDriver
*s
= chr
->opaque
;
611 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
613 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
618 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
621 /* open a character device to a unix fd */
622 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
624 CharDriverState
*chr
;
627 chr
= g_malloc0(sizeof(CharDriverState
));
628 s
= g_malloc0(sizeof(FDCharDriver
));
632 chr
->chr_write
= fd_chr_write
;
633 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
634 chr
->chr_close
= fd_chr_close
;
636 qemu_chr_generic_open(chr
);
641 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
645 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
646 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
650 return qemu_chr_open_fd(-1, fd_out
);
653 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
656 char filename_in
[256], filename_out
[256];
657 const char *filename
= qemu_opt_get(opts
, "path");
659 if (filename
== NULL
) {
660 fprintf(stderr
, "chardev: pipe: no filename given\n");
664 snprintf(filename_in
, 256, "%s.in", filename
);
665 snprintf(filename_out
, 256, "%s.out", filename
);
666 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
667 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
668 if (fd_in
< 0 || fd_out
< 0) {
673 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
678 return qemu_chr_open_fd(fd_in
, fd_out
);
682 /* for STDIO, we handle the case where several clients use it
685 #define TERM_FIFO_MAX_SIZE 1
687 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
688 static int term_fifo_size
;
690 static int stdio_read_poll(void *opaque
)
692 CharDriverState
*chr
= opaque
;
694 /* try to flush the queue if needed */
695 if (term_fifo_size
!= 0 && qemu_chr_be_can_write(chr
) > 0) {
696 qemu_chr_be_write(chr
, term_fifo
, 1);
699 /* see if we can absorb more chars */
700 if (term_fifo_size
== 0)
706 static void stdio_read(void *opaque
)
710 CharDriverState
*chr
= opaque
;
712 size
= read(0, buf
, 1);
714 /* stdin has been closed. Remove it from the active list. */
715 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
716 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
720 if (qemu_chr_be_can_write(chr
) > 0) {
721 qemu_chr_be_write(chr
, buf
, 1);
722 } else if (term_fifo_size
== 0) {
723 term_fifo
[term_fifo_size
++] = buf
[0];
728 /* init terminal so that we can grab keys */
729 static struct termios oldtty
;
730 static int old_fd0_flags
;
731 static bool stdio_allow_signal
;
733 static void term_exit(void)
735 tcsetattr (0, TCSANOW
, &oldtty
);
736 fcntl(0, F_SETFL
, old_fd0_flags
);
739 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
745 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
746 |INLCR
|IGNCR
|ICRNL
|IXON
);
747 tty
.c_oflag
|= OPOST
;
748 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
749 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
754 /* if graphical mode, we allow Ctrl-C handling */
755 if (!stdio_allow_signal
)
756 tty
.c_lflag
&= ~ISIG
;
758 tcsetattr (0, TCSANOW
, &tty
);
761 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
765 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
769 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
771 CharDriverState
*chr
;
773 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
) {
776 if (stdio_nb_clients
== 0) {
777 old_fd0_flags
= fcntl(0, F_GETFL
);
778 tcgetattr (0, &oldtty
);
779 fcntl(0, F_SETFL
, O_NONBLOCK
);
783 chr
= qemu_chr_open_fd(0, 1);
784 chr
->chr_close
= qemu_chr_close_stdio
;
785 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
786 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
788 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
789 display_type
!= DT_NOGRAPHIC
);
790 qemu_chr_fe_set_echo(chr
, false);
796 /* Once Solaris has openpty(), this is going to be removed. */
797 static int openpty(int *amaster
, int *aslave
, char *name
,
798 struct termios
*termp
, struct winsize
*winp
)
801 int mfd
= -1, sfd
= -1;
803 *amaster
= *aslave
= -1;
805 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
809 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
812 if ((slave
= ptsname(mfd
)) == NULL
)
815 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
818 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
819 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
827 ioctl(sfd
, TIOCSWINSZ
, winp
);
838 static void cfmakeraw (struct termios
*termios_p
)
840 termios_p
->c_iflag
&=
841 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
842 termios_p
->c_oflag
&= ~OPOST
;
843 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
844 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
845 termios_p
->c_cflag
|= CS8
;
847 termios_p
->c_cc
[VMIN
] = 0;
848 termios_p
->c_cc
[VTIME
] = 0;
852 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
853 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
854 || defined(__GLIBC__)
864 static void pty_chr_update_read_handler(CharDriverState
*chr
);
865 static void pty_chr_state(CharDriverState
*chr
, int connected
);
867 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
869 PtyCharDriver
*s
= chr
->opaque
;
872 /* guest sends data, check for (re-)connect */
873 pty_chr_update_read_handler(chr
);
876 return send_all(s
->fd
, buf
, len
);
879 static int pty_chr_read_poll(void *opaque
)
881 CharDriverState
*chr
= opaque
;
882 PtyCharDriver
*s
= chr
->opaque
;
884 s
->read_bytes
= qemu_chr_be_can_write(chr
);
885 return s
->read_bytes
;
888 static void pty_chr_read(void *opaque
)
890 CharDriverState
*chr
= opaque
;
891 PtyCharDriver
*s
= chr
->opaque
;
893 uint8_t buf
[READ_BUF_LEN
];
896 if (len
> s
->read_bytes
)
900 size
= read(s
->fd
, buf
, len
);
901 if ((size
== -1 && errno
== EIO
) ||
903 pty_chr_state(chr
, 0);
907 pty_chr_state(chr
, 1);
908 qemu_chr_be_write(chr
, buf
, size
);
912 static void pty_chr_update_read_handler(CharDriverState
*chr
)
914 PtyCharDriver
*s
= chr
->opaque
;
916 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
917 pty_chr_read
, NULL
, chr
);
920 * Short timeout here: just need wait long enougth that qemu makes
921 * it through the poll loop once. When reconnected we want a
922 * short timeout so we notice it almost instantly. Otherwise
923 * read() gives us -EIO instantly, making pty_chr_state() reset the
924 * timeout to the normal (much longer) poll interval before the
927 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
930 static void pty_chr_state(CharDriverState
*chr
, int connected
)
932 PtyCharDriver
*s
= chr
->opaque
;
935 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
938 /* (re-)connect poll interval for idle guests: once per second.
939 * We check more frequently in case the guests sends data to
940 * the virtual device linked to our pty. */
941 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
944 qemu_chr_generic_open(chr
);
949 static void pty_chr_timer(void *opaque
)
951 struct CharDriverState
*chr
= opaque
;
952 PtyCharDriver
*s
= chr
->opaque
;
957 /* If we arrive here without polling being cleared due
958 * read returning -EIO, then we are (re-)connected */
959 pty_chr_state(chr
, 1);
964 pty_chr_update_read_handler(chr
);
967 static void pty_chr_close(struct CharDriverState
*chr
)
969 PtyCharDriver
*s
= chr
->opaque
;
971 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
973 qemu_del_timer(s
->timer
);
974 qemu_free_timer(s
->timer
);
976 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
979 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
981 CharDriverState
*chr
;
984 int master_fd
, slave_fd
, len
;
985 #if defined(__OpenBSD__) || defined(__DragonFly__)
986 char pty_name
[PATH_MAX
];
987 #define q_ptsname(x) pty_name
989 char *pty_name
= NULL
;
990 #define q_ptsname(x) ptsname(x)
993 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
997 /* Set raw attributes on the pty. */
998 tcgetattr(slave_fd
, &tty
);
1000 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1003 chr
= g_malloc0(sizeof(CharDriverState
));
1005 len
= strlen(q_ptsname(master_fd
)) + 5;
1006 chr
->filename
= g_malloc(len
);
1007 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
1008 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
1009 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(master_fd
));
1011 s
= g_malloc0(sizeof(PtyCharDriver
));
1013 chr
->chr_write
= pty_chr_write
;
1014 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1015 chr
->chr_close
= pty_chr_close
;
1018 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
1023 static void tty_serial_init(int fd
, int speed
,
1024 int parity
, int data_bits
, int stop_bits
)
1030 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1031 speed
, parity
, data_bits
, stop_bits
);
1033 tcgetattr (fd
, &tty
);
1035 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1036 speed
= speed
* 10 / 11;
1053 /* Non-Posix values follow. They may be unsupported on some systems. */
1055 check_speed(115200);
1057 check_speed(230400);
1060 check_speed(460800);
1063 check_speed(500000);
1066 check_speed(576000);
1069 check_speed(921600);
1072 check_speed(1000000);
1075 check_speed(1152000);
1078 check_speed(1500000);
1081 check_speed(2000000);
1084 check_speed(2500000);
1087 check_speed(3000000);
1090 check_speed(3500000);
1093 check_speed(4000000);
1098 cfsetispeed(&tty
, spd
);
1099 cfsetospeed(&tty
, spd
);
1101 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1102 |INLCR
|IGNCR
|ICRNL
|IXON
);
1103 tty
.c_oflag
|= OPOST
;
1104 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1105 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1126 tty
.c_cflag
|= PARENB
;
1129 tty
.c_cflag
|= PARENB
| PARODD
;
1133 tty
.c_cflag
|= CSTOPB
;
1135 tcsetattr (fd
, TCSANOW
, &tty
);
1138 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1140 FDCharDriver
*s
= chr
->opaque
;
1143 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1145 QEMUSerialSetParams
*ssp
= arg
;
1146 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1147 ssp
->data_bits
, ssp
->stop_bits
);
1150 case CHR_IOCTL_SERIAL_SET_BREAK
:
1152 int enable
= *(int *)arg
;
1154 tcsendbreak(s
->fd_in
, 1);
1157 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1160 int *targ
= (int *)arg
;
1161 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1163 if (sarg
& TIOCM_CTS
)
1164 *targ
|= CHR_TIOCM_CTS
;
1165 if (sarg
& TIOCM_CAR
)
1166 *targ
|= CHR_TIOCM_CAR
;
1167 if (sarg
& TIOCM_DSR
)
1168 *targ
|= CHR_TIOCM_DSR
;
1169 if (sarg
& TIOCM_RI
)
1170 *targ
|= CHR_TIOCM_RI
;
1171 if (sarg
& TIOCM_DTR
)
1172 *targ
|= CHR_TIOCM_DTR
;
1173 if (sarg
& TIOCM_RTS
)
1174 *targ
|= CHR_TIOCM_RTS
;
1177 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1179 int sarg
= *(int *)arg
;
1181 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1182 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1183 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1184 if (sarg
& CHR_TIOCM_CTS
)
1186 if (sarg
& CHR_TIOCM_CAR
)
1188 if (sarg
& CHR_TIOCM_DSR
)
1190 if (sarg
& CHR_TIOCM_RI
)
1192 if (sarg
& CHR_TIOCM_DTR
)
1194 if (sarg
& CHR_TIOCM_RTS
)
1196 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1205 static void qemu_chr_close_tty(CharDriverState
*chr
)
1207 FDCharDriver
*s
= chr
->opaque
;
1221 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1223 const char *filename
= qemu_opt_get(opts
, "path");
1224 CharDriverState
*chr
;
1227 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1231 tty_serial_init(fd
, 115200, 'N', 8, 1);
1232 chr
= qemu_chr_open_fd(fd
, fd
);
1233 chr
->chr_ioctl
= tty_serial_ioctl
;
1234 chr
->chr_close
= qemu_chr_close_tty
;
1237 #else /* ! __linux__ && ! __sun__ */
1238 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1242 #endif /* __linux__ || __sun__ */
1244 #if defined(__linux__)
1248 } ParallelCharDriver
;
1250 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1252 if (s
->mode
!= mode
) {
1254 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1261 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1263 ParallelCharDriver
*drv
= chr
->opaque
;
1268 case CHR_IOCTL_PP_READ_DATA
:
1269 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1271 *(uint8_t *)arg
= b
;
1273 case CHR_IOCTL_PP_WRITE_DATA
:
1274 b
= *(uint8_t *)arg
;
1275 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1278 case CHR_IOCTL_PP_READ_CONTROL
:
1279 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1281 /* Linux gives only the lowest bits, and no way to know data
1282 direction! For better compatibility set the fixed upper
1284 *(uint8_t *)arg
= b
| 0xc0;
1286 case CHR_IOCTL_PP_WRITE_CONTROL
:
1287 b
= *(uint8_t *)arg
;
1288 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1291 case CHR_IOCTL_PP_READ_STATUS
:
1292 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1294 *(uint8_t *)arg
= b
;
1296 case CHR_IOCTL_PP_DATA_DIR
:
1297 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1300 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1301 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1302 struct ParallelIOArg
*parg
= arg
;
1303 int n
= read(fd
, parg
->buffer
, parg
->count
);
1304 if (n
!= parg
->count
) {
1309 case CHR_IOCTL_PP_EPP_READ
:
1310 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1311 struct ParallelIOArg
*parg
= arg
;
1312 int n
= read(fd
, parg
->buffer
, parg
->count
);
1313 if (n
!= parg
->count
) {
1318 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1319 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1320 struct ParallelIOArg
*parg
= arg
;
1321 int n
= write(fd
, parg
->buffer
, parg
->count
);
1322 if (n
!= parg
->count
) {
1327 case CHR_IOCTL_PP_EPP_WRITE
:
1328 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1329 struct ParallelIOArg
*parg
= arg
;
1330 int n
= write(fd
, parg
->buffer
, parg
->count
);
1331 if (n
!= parg
->count
) {
1342 static void pp_close(CharDriverState
*chr
)
1344 ParallelCharDriver
*drv
= chr
->opaque
;
1347 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1348 ioctl(fd
, PPRELEASE
);
1351 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1354 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1356 const char *filename
= qemu_opt_get(opts
, "path");
1357 CharDriverState
*chr
;
1358 ParallelCharDriver
*drv
;
1361 TFR(fd
= qemu_open(filename
, O_RDWR
));
1366 if (ioctl(fd
, PPCLAIM
) < 0) {
1371 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1373 drv
->mode
= IEEE1284_MODE_COMPAT
;
1375 chr
= g_malloc0(sizeof(CharDriverState
));
1376 chr
->chr_write
= null_chr_write
;
1377 chr
->chr_ioctl
= pp_ioctl
;
1378 chr
->chr_close
= pp_close
;
1381 qemu_chr_generic_open(chr
);
1385 #endif /* __linux__ */
1387 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1388 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1390 int fd
= (int)(intptr_t)chr
->opaque
;
1394 case CHR_IOCTL_PP_READ_DATA
:
1395 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1397 *(uint8_t *)arg
= b
;
1399 case CHR_IOCTL_PP_WRITE_DATA
:
1400 b
= *(uint8_t *)arg
;
1401 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1404 case CHR_IOCTL_PP_READ_CONTROL
:
1405 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1407 *(uint8_t *)arg
= b
;
1409 case CHR_IOCTL_PP_WRITE_CONTROL
:
1410 b
= *(uint8_t *)arg
;
1411 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1414 case CHR_IOCTL_PP_READ_STATUS
:
1415 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1417 *(uint8_t *)arg
= b
;
1425 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1427 const char *filename
= qemu_opt_get(opts
, "path");
1428 CharDriverState
*chr
;
1431 fd
= qemu_open(filename
, O_RDWR
);
1436 chr
= g_malloc0(sizeof(CharDriverState
));
1437 chr
->opaque
= (void *)(intptr_t)fd
;
1438 chr
->chr_write
= null_chr_write
;
1439 chr
->chr_ioctl
= pp_ioctl
;
1446 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1450 HANDLE hcom
, hrecv
, hsend
;
1451 OVERLAPPED orecv
, osend
;
1458 HANDLE hInputReadyEvent
;
1459 HANDLE hInputDoneEvent
;
1460 HANDLE hInputThread
;
1461 uint8_t win_stdio_buf
;
1462 } WinStdioCharState
;
1464 #define NSENDBUF 2048
1465 #define NRECVBUF 2048
1466 #define MAXCONNECT 1
1467 #define NTIMEOUT 5000
1469 static int win_chr_poll(void *opaque
);
1470 static int win_chr_pipe_poll(void *opaque
);
1472 static void win_chr_close(CharDriverState
*chr
)
1474 WinCharState
*s
= chr
->opaque
;
1477 CloseHandle(s
->hsend
);
1481 CloseHandle(s
->hrecv
);
1485 CloseHandle(s
->hcom
);
1489 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1491 qemu_del_polling_cb(win_chr_poll
, chr
);
1493 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1496 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1498 WinCharState
*s
= chr
->opaque
;
1500 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1505 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1507 fprintf(stderr
, "Failed CreateEvent\n");
1510 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1512 fprintf(stderr
, "Failed CreateEvent\n");
1516 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1517 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1518 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1519 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1524 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1525 fprintf(stderr
, "Failed SetupComm\n");
1529 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1530 size
= sizeof(COMMCONFIG
);
1531 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1532 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1533 CommConfigDialog(filename
, NULL
, &comcfg
);
1535 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1536 fprintf(stderr
, "Failed SetCommState\n");
1540 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1541 fprintf(stderr
, "Failed SetCommMask\n");
1545 cto
.ReadIntervalTimeout
= MAXDWORD
;
1546 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1547 fprintf(stderr
, "Failed SetCommTimeouts\n");
1551 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1552 fprintf(stderr
, "Failed ClearCommError\n");
1555 qemu_add_polling_cb(win_chr_poll
, chr
);
1563 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1565 WinCharState
*s
= chr
->opaque
;
1566 DWORD len
, ret
, size
, err
;
1569 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1570 s
->osend
.hEvent
= s
->hsend
;
1573 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1575 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1577 err
= GetLastError();
1578 if (err
== ERROR_IO_PENDING
) {
1579 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1597 static int win_chr_read_poll(CharDriverState
*chr
)
1599 WinCharState
*s
= chr
->opaque
;
1601 s
->max_size
= qemu_chr_be_can_write(chr
);
1605 static void win_chr_readfile(CharDriverState
*chr
)
1607 WinCharState
*s
= chr
->opaque
;
1609 uint8_t buf
[READ_BUF_LEN
];
1612 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1613 s
->orecv
.hEvent
= s
->hrecv
;
1614 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1616 err
= GetLastError();
1617 if (err
== ERROR_IO_PENDING
) {
1618 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1623 qemu_chr_be_write(chr
, buf
, size
);
1627 static void win_chr_read(CharDriverState
*chr
)
1629 WinCharState
*s
= chr
->opaque
;
1631 if (s
->len
> s
->max_size
)
1632 s
->len
= s
->max_size
;
1636 win_chr_readfile(chr
);
1639 static int win_chr_poll(void *opaque
)
1641 CharDriverState
*chr
= opaque
;
1642 WinCharState
*s
= chr
->opaque
;
1646 ClearCommError(s
->hcom
, &comerr
, &status
);
1647 if (status
.cbInQue
> 0) {
1648 s
->len
= status
.cbInQue
;
1649 win_chr_read_poll(chr
);
1656 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1658 const char *filename
= qemu_opt_get(opts
, "path");
1659 CharDriverState
*chr
;
1662 chr
= g_malloc0(sizeof(CharDriverState
));
1663 s
= g_malloc0(sizeof(WinCharState
));
1665 chr
->chr_write
= win_chr_write
;
1666 chr
->chr_close
= win_chr_close
;
1668 if (win_chr_init(chr
, filename
) < 0) {
1673 qemu_chr_generic_open(chr
);
1677 static int win_chr_pipe_poll(void *opaque
)
1679 CharDriverState
*chr
= opaque
;
1680 WinCharState
*s
= chr
->opaque
;
1683 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1686 win_chr_read_poll(chr
);
1693 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1695 WinCharState
*s
= chr
->opaque
;
1703 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1705 fprintf(stderr
, "Failed CreateEvent\n");
1708 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1710 fprintf(stderr
, "Failed CreateEvent\n");
1714 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1715 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1716 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1718 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1719 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1720 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1725 ZeroMemory(&ov
, sizeof(ov
));
1726 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1727 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1729 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1733 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1735 fprintf(stderr
, "Failed GetOverlappedResult\n");
1737 CloseHandle(ov
.hEvent
);
1744 CloseHandle(ov
.hEvent
);
1747 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1756 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1758 const char *filename
= qemu_opt_get(opts
, "path");
1759 CharDriverState
*chr
;
1762 chr
= g_malloc0(sizeof(CharDriverState
));
1763 s
= g_malloc0(sizeof(WinCharState
));
1765 chr
->chr_write
= win_chr_write
;
1766 chr
->chr_close
= win_chr_close
;
1768 if (win_chr_pipe_init(chr
, filename
) < 0) {
1773 qemu_chr_generic_open(chr
);
1777 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1779 CharDriverState
*chr
;
1782 chr
= g_malloc0(sizeof(CharDriverState
));
1783 s
= g_malloc0(sizeof(WinCharState
));
1786 chr
->chr_write
= win_chr_write
;
1787 qemu_chr_generic_open(chr
);
1791 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1793 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1796 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1798 const char *file_out
= qemu_opt_get(opts
, "path");
1801 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1802 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1803 if (fd_out
== INVALID_HANDLE_VALUE
) {
1807 return qemu_chr_open_win_file(fd_out
);
1810 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1812 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1819 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1829 static void win_stdio_wait_func(void *opaque
)
1831 CharDriverState
*chr
= opaque
;
1832 WinStdioCharState
*stdio
= chr
->opaque
;
1833 INPUT_RECORD buf
[4];
1838 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1842 /* Avoid error storm */
1843 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1847 for (i
= 0; i
< dwSize
; i
++) {
1848 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1850 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1852 if (kev
->uChar
.AsciiChar
!= 0) {
1853 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1854 if (qemu_chr_be_can_write(chr
)) {
1855 uint8_t c
= kev
->uChar
.AsciiChar
;
1856 qemu_chr_be_write(chr
, &c
, 1);
1864 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
1866 CharDriverState
*chr
= param
;
1867 WinStdioCharState
*stdio
= chr
->opaque
;
1873 /* Wait for one byte */
1874 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
1876 /* Exit in case of error, continue if nothing read */
1884 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1885 if (stdio
->win_stdio_buf
== '\r') {
1889 /* Signal the main thread and wait until the byte was eaten */
1890 if (!SetEvent(stdio
->hInputReadyEvent
)) {
1893 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
1899 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
1903 static void win_stdio_thread_wait_func(void *opaque
)
1905 CharDriverState
*chr
= opaque
;
1906 WinStdioCharState
*stdio
= chr
->opaque
;
1908 if (qemu_chr_be_can_write(chr
)) {
1909 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
1912 SetEvent(stdio
->hInputDoneEvent
);
1915 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
1917 WinStdioCharState
*stdio
= chr
->opaque
;
1920 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
1923 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
1925 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
1929 static void win_stdio_close(CharDriverState
*chr
)
1931 WinStdioCharState
*stdio
= chr
->opaque
;
1933 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
1934 CloseHandle(stdio
->hInputReadyEvent
);
1936 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
1937 CloseHandle(stdio
->hInputDoneEvent
);
1939 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
1940 TerminateThread(stdio
->hInputThread
, 0);
1943 g_free(chr
->opaque
);
1948 static CharDriverState
*qemu_chr_open_win_stdio(QemuOpts
*opts
)
1950 CharDriverState
*chr
;
1951 WinStdioCharState
*stdio
;
1955 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
1956 || ((display_type
!= DT_NOGRAPHIC
) && (stdio_nb_clients
!= 0))) {
1960 chr
= g_malloc0(sizeof(CharDriverState
));
1961 stdio
= g_malloc0(sizeof(WinStdioCharState
));
1963 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
1964 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
1965 fprintf(stderr
, "cannot open stdio: invalid handle\n");
1969 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
1971 chr
->opaque
= stdio
;
1972 chr
->chr_write
= win_stdio_write
;
1973 chr
->chr_close
= win_stdio_close
;
1975 if (stdio_nb_clients
== 0) {
1977 if (qemu_add_wait_object(stdio
->hStdIn
,
1978 win_stdio_wait_func
, chr
)) {
1979 fprintf(stderr
, "qemu_add_wait_object: failed\n");
1984 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1985 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1986 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
1989 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
1990 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
1991 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
1992 fprintf(stderr
, "cannot create stdio thread or event\n");
1995 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
1996 win_stdio_thread_wait_func
, chr
)) {
1997 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2002 dwMode
|= ENABLE_LINE_INPUT
;
2004 stdio_clients
[stdio_nb_clients
++] = chr
;
2005 if (stdio_nb_clients
== 1 && is_console
) {
2006 /* set the terminal in raw mode */
2007 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2008 dwMode
|= ENABLE_PROCESSED_INPUT
;
2011 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2013 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2014 qemu_chr_fe_set_echo(chr
, false);
2018 #endif /* !_WIN32 */
2020 /***********************************************************/
2021 /* UDP Net console */
2025 uint8_t buf
[READ_BUF_LEN
];
2031 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2033 NetCharDriver
*s
= chr
->opaque
;
2035 return send(s
->fd
, (const void *)buf
, len
, 0);
2038 static int udp_chr_read_poll(void *opaque
)
2040 CharDriverState
*chr
= opaque
;
2041 NetCharDriver
*s
= chr
->opaque
;
2043 s
->max_size
= qemu_chr_be_can_write(chr
);
2045 /* If there were any stray characters in the queue process them
2048 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2049 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2051 s
->max_size
= qemu_chr_be_can_write(chr
);
2056 static void udp_chr_read(void *opaque
)
2058 CharDriverState
*chr
= opaque
;
2059 NetCharDriver
*s
= chr
->opaque
;
2061 if (s
->max_size
== 0)
2063 s
->bufcnt
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2064 s
->bufptr
= s
->bufcnt
;
2069 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2070 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2072 s
->max_size
= qemu_chr_be_can_write(chr
);
2076 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2078 NetCharDriver
*s
= chr
->opaque
;
2081 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2082 udp_chr_read
, NULL
, chr
);
2086 static void udp_chr_close(CharDriverState
*chr
)
2088 NetCharDriver
*s
= chr
->opaque
;
2090 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2094 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2097 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2099 CharDriverState
*chr
= NULL
;
2100 NetCharDriver
*s
= NULL
;
2101 Error
*local_err
= NULL
;
2104 chr
= g_malloc0(sizeof(CharDriverState
));
2105 s
= g_malloc0(sizeof(NetCharDriver
));
2107 fd
= inet_dgram_opts(opts
, &local_err
);
2116 chr
->chr_write
= udp_chr_write
;
2117 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2118 chr
->chr_close
= udp_chr_close
;
2123 qerror_report_err(local_err
);
2124 error_free(local_err
);
2134 /***********************************************************/
2135 /* TCP Net console */
2147 static void tcp_chr_accept(void *opaque
);
2149 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2151 TCPCharDriver
*s
= chr
->opaque
;
2153 return send_all(s
->fd
, buf
, len
);
2155 /* XXX: indicate an error ? */
2160 static int tcp_chr_read_poll(void *opaque
)
2162 CharDriverState
*chr
= opaque
;
2163 TCPCharDriver
*s
= chr
->opaque
;
2166 s
->max_size
= qemu_chr_be_can_write(chr
);
2171 #define IAC_BREAK 243
2172 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2174 uint8_t *buf
, int *size
)
2176 /* Handle any telnet client's basic IAC options to satisfy char by
2177 * char mode with no echo. All IAC options will be removed from
2178 * the buf and the do_telnetopt variable will be used to track the
2179 * state of the width of the IAC information.
2181 * IAC commands come in sets of 3 bytes with the exception of the
2182 * "IAC BREAK" command and the double IAC.
2188 for (i
= 0; i
< *size
; i
++) {
2189 if (s
->do_telnetopt
> 1) {
2190 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2191 /* Double IAC means send an IAC */
2195 s
->do_telnetopt
= 1;
2197 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2198 /* Handle IAC break commands by sending a serial break */
2199 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2204 if (s
->do_telnetopt
>= 4) {
2205 s
->do_telnetopt
= 1;
2208 if ((unsigned char)buf
[i
] == IAC
) {
2209 s
->do_telnetopt
= 2;
2220 static int tcp_get_msgfd(CharDriverState
*chr
)
2222 TCPCharDriver
*s
= chr
->opaque
;
2229 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2231 TCPCharDriver
*s
= chr
->opaque
;
2232 struct cmsghdr
*cmsg
;
2234 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2237 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2238 cmsg
->cmsg_level
!= SOL_SOCKET
||
2239 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2242 fd
= *((int *)CMSG_DATA(cmsg
));
2246 #ifndef MSG_CMSG_CLOEXEC
2247 qemu_set_cloexec(fd
);
2255 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2257 TCPCharDriver
*s
= chr
->opaque
;
2258 struct msghdr msg
= { NULL
, };
2259 struct iovec iov
[1];
2261 struct cmsghdr cmsg
;
2262 char control
[CMSG_SPACE(sizeof(int))];
2267 iov
[0].iov_base
= buf
;
2268 iov
[0].iov_len
= len
;
2272 msg
.msg_control
= &msg_control
;
2273 msg
.msg_controllen
= sizeof(msg_control
);
2275 #ifdef MSG_CMSG_CLOEXEC
2276 flags
|= MSG_CMSG_CLOEXEC
;
2278 ret
= recvmsg(s
->fd
, &msg
, flags
);
2279 if (ret
> 0 && s
->is_unix
) {
2280 unix_process_msgfd(chr
, &msg
);
2286 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2288 TCPCharDriver
*s
= chr
->opaque
;
2289 return qemu_recv(s
->fd
, buf
, len
, 0);
2293 static void tcp_chr_read(void *opaque
)
2295 CharDriverState
*chr
= opaque
;
2296 TCPCharDriver
*s
= chr
->opaque
;
2297 uint8_t buf
[READ_BUF_LEN
];
2300 if (!s
->connected
|| s
->max_size
<= 0)
2303 if (len
> s
->max_size
)
2305 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2307 /* connection closed */
2309 if (s
->listen_fd
>= 0) {
2310 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2312 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2315 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2316 } else if (size
> 0) {
2317 if (s
->do_telnetopt
)
2318 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2320 qemu_chr_be_write(chr
, buf
, size
);
2325 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2327 return qemu_chr_open_fd(eventfd
, eventfd
);
2331 static void tcp_chr_connect(void *opaque
)
2333 CharDriverState
*chr
= opaque
;
2334 TCPCharDriver
*s
= chr
->opaque
;
2338 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2339 tcp_chr_read
, NULL
, chr
);
2341 qemu_chr_generic_open(chr
);
2344 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2345 static void tcp_chr_telnet_init(int fd
)
2348 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2349 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2350 send(fd
, (char *)buf
, 3, 0);
2351 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2352 send(fd
, (char *)buf
, 3, 0);
2353 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2354 send(fd
, (char *)buf
, 3, 0);
2355 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2356 send(fd
, (char *)buf
, 3, 0);
2359 static void socket_set_nodelay(int fd
)
2362 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2365 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2367 TCPCharDriver
*s
= chr
->opaque
;
2371 socket_set_nonblock(fd
);
2373 socket_set_nodelay(fd
);
2375 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2376 tcp_chr_connect(chr
);
2381 static void tcp_chr_accept(void *opaque
)
2383 CharDriverState
*chr
= opaque
;
2384 TCPCharDriver
*s
= chr
->opaque
;
2385 struct sockaddr_in saddr
;
2387 struct sockaddr_un uaddr
;
2389 struct sockaddr
*addr
;
2396 len
= sizeof(uaddr
);
2397 addr
= (struct sockaddr
*)&uaddr
;
2401 len
= sizeof(saddr
);
2402 addr
= (struct sockaddr
*)&saddr
;
2404 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2405 if (fd
< 0 && errno
!= EINTR
) {
2407 } else if (fd
>= 0) {
2408 if (s
->do_telnetopt
)
2409 tcp_chr_telnet_init(fd
);
2413 if (tcp_chr_add_client(chr
, fd
) < 0)
2417 static void tcp_chr_close(CharDriverState
*chr
)
2419 TCPCharDriver
*s
= chr
->opaque
;
2421 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2424 if (s
->listen_fd
>= 0) {
2425 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2426 closesocket(s
->listen_fd
);
2429 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2432 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2434 CharDriverState
*chr
= NULL
;
2435 TCPCharDriver
*s
= NULL
;
2436 Error
*local_err
= NULL
;
2444 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2445 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2446 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2447 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2448 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2452 chr
= g_malloc0(sizeof(CharDriverState
));
2453 s
= g_malloc0(sizeof(TCPCharDriver
));
2457 fd
= unix_listen_opts(opts
, &local_err
);
2459 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2463 fd
= inet_listen_opts(opts
, 0, &local_err
);
2465 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2472 if (!is_waitconnect
)
2473 socket_set_nonblock(fd
);
2479 s
->is_unix
= is_unix
;
2480 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2483 chr
->chr_write
= tcp_chr_write
;
2484 chr
->chr_close
= tcp_chr_close
;
2485 chr
->get_msgfd
= tcp_get_msgfd
;
2486 chr
->chr_add_client
= tcp_chr_add_client
;
2490 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2492 s
->do_telnetopt
= 1;
2497 socket_set_nodelay(fd
);
2498 tcp_chr_connect(chr
);
2501 /* for "info chardev" monitor command */
2502 chr
->filename
= g_malloc(256);
2504 snprintf(chr
->filename
, 256, "unix:%s%s",
2505 qemu_opt_get(opts
, "path"),
2506 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2507 } else if (is_telnet
) {
2508 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2509 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2510 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2512 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2513 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2514 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2517 if (is_listen
&& is_waitconnect
) {
2518 printf("QEMU waiting for connection on: %s\n",
2520 tcp_chr_accept(chr
);
2521 socket_set_nonblock(s
->listen_fd
);
2527 qerror_report_err(local_err
);
2528 error_free(local_err
);
2538 /***********************************************************/
2539 /* Memory chardev */
2542 size_t outbuf_capacity
;
2546 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2548 MemoryDriver
*d
= chr
->opaque
;
2550 /* TODO: the QString implementation has the same code, we should
2551 * introduce a generic way to do this in cutils.c */
2552 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2554 d
->outbuf_capacity
+= len
;
2555 d
->outbuf_capacity
*= 2;
2556 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2559 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2560 d
->outbuf_size
+= len
;
2565 void qemu_chr_init_mem(CharDriverState
*chr
)
2569 d
= g_malloc(sizeof(*d
));
2571 d
->outbuf_capacity
= 4096;
2572 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2574 memset(chr
, 0, sizeof(*chr
));
2576 chr
->chr_write
= mem_chr_write
;
2579 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2581 MemoryDriver
*d
= chr
->opaque
;
2582 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2585 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2586 void qemu_chr_close_mem(CharDriverState
*chr
)
2588 MemoryDriver
*d
= chr
->opaque
;
2591 g_free(chr
->opaque
);
2593 chr
->chr_write
= NULL
;
2596 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2598 const MemoryDriver
*d
= chr
->opaque
;
2599 return d
->outbuf_size
;
2602 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2604 char host
[65], port
[33], width
[8], height
[8];
2608 Error
*local_err
= NULL
;
2610 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2611 if (error_is_set(&local_err
)) {
2612 qerror_report_err(local_err
);
2613 error_free(local_err
);
2617 if (strstart(filename
, "mon:", &p
)) {
2619 qemu_opt_set(opts
, "mux", "on");
2622 if (strcmp(filename
, "null") == 0 ||
2623 strcmp(filename
, "pty") == 0 ||
2624 strcmp(filename
, "msmouse") == 0 ||
2625 strcmp(filename
, "braille") == 0 ||
2626 strcmp(filename
, "stdio") == 0) {
2627 qemu_opt_set(opts
, "backend", filename
);
2630 if (strstart(filename
, "vc", &p
)) {
2631 qemu_opt_set(opts
, "backend", "vc");
2633 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2635 qemu_opt_set(opts
, "width", width
);
2636 qemu_opt_set(opts
, "height", height
);
2637 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2639 qemu_opt_set(opts
, "cols", width
);
2640 qemu_opt_set(opts
, "rows", height
);
2647 if (strcmp(filename
, "con:") == 0) {
2648 qemu_opt_set(opts
, "backend", "console");
2651 if (strstart(filename
, "COM", NULL
)) {
2652 qemu_opt_set(opts
, "backend", "serial");
2653 qemu_opt_set(opts
, "path", filename
);
2656 if (strstart(filename
, "file:", &p
)) {
2657 qemu_opt_set(opts
, "backend", "file");
2658 qemu_opt_set(opts
, "path", p
);
2661 if (strstart(filename
, "pipe:", &p
)) {
2662 qemu_opt_set(opts
, "backend", "pipe");
2663 qemu_opt_set(opts
, "path", p
);
2666 if (strstart(filename
, "tcp:", &p
) ||
2667 strstart(filename
, "telnet:", &p
)) {
2668 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2670 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2673 qemu_opt_set(opts
, "backend", "socket");
2674 qemu_opt_set(opts
, "host", host
);
2675 qemu_opt_set(opts
, "port", port
);
2676 if (p
[pos
] == ',') {
2677 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2680 if (strstart(filename
, "telnet:", &p
))
2681 qemu_opt_set(opts
, "telnet", "on");
2684 if (strstart(filename
, "udp:", &p
)) {
2685 qemu_opt_set(opts
, "backend", "udp");
2686 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2688 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2692 qemu_opt_set(opts
, "host", host
);
2693 qemu_opt_set(opts
, "port", port
);
2694 if (p
[pos
] == '@') {
2696 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2698 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2702 qemu_opt_set(opts
, "localaddr", host
);
2703 qemu_opt_set(opts
, "localport", port
);
2707 if (strstart(filename
, "unix:", &p
)) {
2708 qemu_opt_set(opts
, "backend", "socket");
2709 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2713 if (strstart(filename
, "/dev/parport", NULL
) ||
2714 strstart(filename
, "/dev/ppi", NULL
)) {
2715 qemu_opt_set(opts
, "backend", "parport");
2716 qemu_opt_set(opts
, "path", filename
);
2719 if (strstart(filename
, "/dev/", NULL
)) {
2720 qemu_opt_set(opts
, "backend", "tty");
2721 qemu_opt_set(opts
, "path", filename
);
2726 qemu_opts_del(opts
);
2730 static const struct {
2732 CharDriverState
*(*open
)(QemuOpts
*opts
);
2733 } backend_table
[] = {
2734 { .name
= "null", .open
= qemu_chr_open_null
},
2735 { .name
= "socket", .open
= qemu_chr_open_socket
},
2736 { .name
= "udp", .open
= qemu_chr_open_udp
},
2737 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2738 { .name
= "vc", .open
= text_console_init
},
2740 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2741 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2742 { .name
= "console", .open
= qemu_chr_open_win_con
},
2743 { .name
= "serial", .open
= qemu_chr_open_win
},
2744 { .name
= "stdio", .open
= qemu_chr_open_win_stdio
},
2746 { .name
= "file", .open
= qemu_chr_open_file_out
},
2747 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2748 { .name
= "pty", .open
= qemu_chr_open_pty
},
2749 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2751 #ifdef CONFIG_BRLAPI
2752 { .name
= "braille", .open
= chr_baum_init
},
2754 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2755 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2756 || defined(__FreeBSD_kernel__)
2757 { .name
= "tty", .open
= qemu_chr_open_tty
},
2759 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2760 || defined(__FreeBSD_kernel__)
2761 { .name
= "parport", .open
= qemu_chr_open_pp
},
2764 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
2768 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
2769 void (*init
)(struct CharDriverState
*s
))
2771 CharDriverState
*chr
;
2774 if (qemu_opts_id(opts
) == NULL
) {
2775 fprintf(stderr
, "chardev: no id specified\n");
2779 if (qemu_opt_get(opts
, "backend") == NULL
) {
2780 fprintf(stderr
, "chardev: \"%s\" missing backend\n",
2781 qemu_opts_id(opts
));
2784 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2785 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2788 if (i
== ARRAY_SIZE(backend_table
)) {
2789 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2790 qemu_opt_get(opts
, "backend"));
2794 chr
= backend_table
[i
].open(opts
);
2796 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2797 qemu_opt_get(opts
, "backend"));
2802 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
2804 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2806 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2807 CharDriverState
*base
= chr
;
2808 int len
= strlen(qemu_opts_id(opts
)) + 6;
2809 base
->label
= g_malloc(len
);
2810 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2811 chr
= qemu_chr_open_mux(base
);
2812 chr
->filename
= base
->filename
;
2813 chr
->avail_connections
= MAX_MUX
;
2814 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2816 chr
->avail_connections
= 1;
2818 chr
->label
= g_strdup(qemu_opts_id(opts
));
2822 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2825 CharDriverState
*chr
;
2828 if (strstart(filename
, "chardev:", &p
)) {
2829 return qemu_chr_find(p
);
2832 opts
= qemu_chr_parse_compat(label
, filename
);
2836 chr
= qemu_chr_new_from_opts(opts
, init
);
2837 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2838 monitor_init(chr
, MONITOR_USE_READLINE
);
2840 qemu_opts_del(opts
);
2844 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
2846 if (chr
->chr_set_echo
) {
2847 chr
->chr_set_echo(chr
, echo
);
2851 void qemu_chr_fe_open(struct CharDriverState
*chr
)
2853 if (chr
->chr_guest_open
) {
2854 chr
->chr_guest_open(chr
);
2858 void qemu_chr_fe_close(struct CharDriverState
*chr
)
2860 if (chr
->chr_guest_close
) {
2861 chr
->chr_guest_close(chr
);
2865 void qemu_chr_delete(CharDriverState
*chr
)
2867 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2869 chr
->chr_close(chr
);
2870 g_free(chr
->filename
);
2875 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
2877 ChardevInfoList
*chr_list
= NULL
;
2878 CharDriverState
*chr
;
2880 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2881 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
2882 info
->value
= g_malloc0(sizeof(*info
->value
));
2883 info
->value
->label
= g_strdup(chr
->label
);
2884 info
->value
->filename
= g_strdup(chr
->filename
);
2886 info
->next
= chr_list
;
2893 CharDriverState
*qemu_chr_find(const char *name
)
2895 CharDriverState
*chr
;
2897 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2898 if (strcmp(chr
->label
, name
) != 0)
2905 /* Get a character (serial) device interface. */
2906 CharDriverState
*qemu_char_get_next_serial(void)
2908 static int next_serial
;
2910 /* FIXME: This function needs to go away: use chardev properties! */
2911 return serial_hds
[next_serial
++];