4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "monitor/monitor.h"
26 #include "ui/console.h"
27 #include "sysemu/sysemu.h"
28 #include "qemu/timer.h"
29 #include "char/char.h"
32 #include "hw/msmouse.h"
33 #include "qmp-commands.h"
43 #include <sys/times.h>
47 #include <sys/ioctl.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
52 #include <arpa/inet.h>
55 #include <sys/select.h>
58 #if defined(__GLIBC__)
60 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
65 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
66 #include <dev/ppbus/ppi.h>
67 #include <dev/ppbus/ppbconf.h>
68 #elif defined(__DragonFly__)
69 #include <dev/misc/ppi/ppi.h>
70 #include <bus/ppbus/ppbconf.h>
76 #include <linux/ppdev.h>
77 #include <linux/parport.h>
81 #include <sys/ethernet.h>
82 #include <sys/sockio.h>
83 #include <netinet/arp.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_icmp.h> // must come after ip.h
88 #include <netinet/udp.h>
89 #include <netinet/tcp.h>
97 #include "qemu/sockets.h"
98 #include "ui/qemu-spice.h"
100 #define READ_BUF_LEN 4096
101 #define CBUFF_SIZE 65536
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 (is_daemonized()) {
777 error_report("cannot use stdio with -daemonize");
780 if (stdio_nb_clients
== 0) {
781 old_fd0_flags
= fcntl(0, F_GETFL
);
782 tcgetattr (0, &oldtty
);
783 fcntl(0, F_SETFL
, O_NONBLOCK
);
787 chr
= qemu_chr_open_fd(0, 1);
788 chr
->chr_close
= qemu_chr_close_stdio
;
789 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
790 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
792 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
793 display_type
!= DT_NOGRAPHIC
);
794 qemu_chr_fe_set_echo(chr
, false);
800 /* Once Solaris has openpty(), this is going to be removed. */
801 static int openpty(int *amaster
, int *aslave
, char *name
,
802 struct termios
*termp
, struct winsize
*winp
)
805 int mfd
= -1, sfd
= -1;
807 *amaster
= *aslave
= -1;
809 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
813 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
816 if ((slave
= ptsname(mfd
)) == NULL
)
819 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
822 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
823 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
831 ioctl(sfd
, TIOCSWINSZ
, winp
);
842 static void cfmakeraw (struct termios
*termios_p
)
844 termios_p
->c_iflag
&=
845 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
846 termios_p
->c_oflag
&= ~OPOST
;
847 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
848 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
849 termios_p
->c_cflag
|= CS8
;
851 termios_p
->c_cc
[VMIN
] = 0;
852 termios_p
->c_cc
[VTIME
] = 0;
856 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
857 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
858 || defined(__GLIBC__)
860 #define HAVE_CHARDEV_TTY 1
870 static void pty_chr_update_read_handler(CharDriverState
*chr
);
871 static void pty_chr_state(CharDriverState
*chr
, int connected
);
873 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
875 PtyCharDriver
*s
= chr
->opaque
;
878 /* guest sends data, check for (re-)connect */
879 pty_chr_update_read_handler(chr
);
882 return send_all(s
->fd
, buf
, len
);
885 static int pty_chr_read_poll(void *opaque
)
887 CharDriverState
*chr
= opaque
;
888 PtyCharDriver
*s
= chr
->opaque
;
890 s
->read_bytes
= qemu_chr_be_can_write(chr
);
891 return s
->read_bytes
;
894 static void pty_chr_read(void *opaque
)
896 CharDriverState
*chr
= opaque
;
897 PtyCharDriver
*s
= chr
->opaque
;
899 uint8_t buf
[READ_BUF_LEN
];
902 if (len
> s
->read_bytes
)
906 size
= read(s
->fd
, buf
, len
);
907 if ((size
== -1 && errno
== EIO
) ||
909 pty_chr_state(chr
, 0);
913 pty_chr_state(chr
, 1);
914 qemu_chr_be_write(chr
, buf
, size
);
918 static void pty_chr_update_read_handler(CharDriverState
*chr
)
920 PtyCharDriver
*s
= chr
->opaque
;
922 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
923 pty_chr_read
, NULL
, chr
);
926 * Short timeout here: just need wait long enougth that qemu makes
927 * it through the poll loop once. When reconnected we want a
928 * short timeout so we notice it almost instantly. Otherwise
929 * read() gives us -EIO instantly, making pty_chr_state() reset the
930 * timeout to the normal (much longer) poll interval before the
933 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
936 static void pty_chr_state(CharDriverState
*chr
, int connected
)
938 PtyCharDriver
*s
= chr
->opaque
;
941 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
944 /* (re-)connect poll interval for idle guests: once per second.
945 * We check more frequently in case the guests sends data to
946 * the virtual device linked to our pty. */
947 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
950 qemu_chr_generic_open(chr
);
955 static void pty_chr_timer(void *opaque
)
957 struct CharDriverState
*chr
= opaque
;
958 PtyCharDriver
*s
= chr
->opaque
;
963 /* If we arrive here without polling being cleared due
964 * read returning -EIO, then we are (re-)connected */
965 pty_chr_state(chr
, 1);
970 pty_chr_update_read_handler(chr
);
973 static void pty_chr_close(struct CharDriverState
*chr
)
975 PtyCharDriver
*s
= chr
->opaque
;
977 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
979 qemu_del_timer(s
->timer
);
980 qemu_free_timer(s
->timer
);
982 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
985 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
987 CharDriverState
*chr
;
991 int master_fd
, slave_fd
, len
;
992 #if defined(__OpenBSD__) || defined(__DragonFly__)
993 char pty_name
[PATH_MAX
];
994 #define q_ptsname(x) pty_name
996 char *pty_name
= NULL
;
997 #define q_ptsname(x) ptsname(x)
1000 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1004 /* Set raw attributes on the pty. */
1005 tcgetattr(slave_fd
, &tty
);
1007 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1010 chr
= g_malloc0(sizeof(CharDriverState
));
1012 len
= strlen(q_ptsname(master_fd
)) + 5;
1013 chr
->filename
= g_malloc(len
);
1014 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
1015 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
1017 label
= qemu_opts_id(opts
);
1018 fprintf(stderr
, "char device redirected to %s%s%s%s\n",
1019 q_ptsname(master_fd
),
1020 label
? " (label " : "",
1024 s
= g_malloc0(sizeof(PtyCharDriver
));
1026 chr
->chr_write
= pty_chr_write
;
1027 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1028 chr
->chr_close
= pty_chr_close
;
1031 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
1036 static void tty_serial_init(int fd
, int speed
,
1037 int parity
, int data_bits
, int stop_bits
)
1043 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1044 speed
, parity
, data_bits
, stop_bits
);
1046 tcgetattr (fd
, &tty
);
1048 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1049 speed
= speed
* 10 / 11;
1066 /* Non-Posix values follow. They may be unsupported on some systems. */
1068 check_speed(115200);
1070 check_speed(230400);
1073 check_speed(460800);
1076 check_speed(500000);
1079 check_speed(576000);
1082 check_speed(921600);
1085 check_speed(1000000);
1088 check_speed(1152000);
1091 check_speed(1500000);
1094 check_speed(2000000);
1097 check_speed(2500000);
1100 check_speed(3000000);
1103 check_speed(3500000);
1106 check_speed(4000000);
1111 cfsetispeed(&tty
, spd
);
1112 cfsetospeed(&tty
, spd
);
1114 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1115 |INLCR
|IGNCR
|ICRNL
|IXON
);
1116 tty
.c_oflag
|= OPOST
;
1117 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1118 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1139 tty
.c_cflag
|= PARENB
;
1142 tty
.c_cflag
|= PARENB
| PARODD
;
1146 tty
.c_cflag
|= CSTOPB
;
1148 tcsetattr (fd
, TCSANOW
, &tty
);
1151 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1153 FDCharDriver
*s
= chr
->opaque
;
1156 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1158 QEMUSerialSetParams
*ssp
= arg
;
1159 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1160 ssp
->data_bits
, ssp
->stop_bits
);
1163 case CHR_IOCTL_SERIAL_SET_BREAK
:
1165 int enable
= *(int *)arg
;
1167 tcsendbreak(s
->fd_in
, 1);
1170 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1173 int *targ
= (int *)arg
;
1174 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1176 if (sarg
& TIOCM_CTS
)
1177 *targ
|= CHR_TIOCM_CTS
;
1178 if (sarg
& TIOCM_CAR
)
1179 *targ
|= CHR_TIOCM_CAR
;
1180 if (sarg
& TIOCM_DSR
)
1181 *targ
|= CHR_TIOCM_DSR
;
1182 if (sarg
& TIOCM_RI
)
1183 *targ
|= CHR_TIOCM_RI
;
1184 if (sarg
& TIOCM_DTR
)
1185 *targ
|= CHR_TIOCM_DTR
;
1186 if (sarg
& TIOCM_RTS
)
1187 *targ
|= CHR_TIOCM_RTS
;
1190 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1192 int sarg
= *(int *)arg
;
1194 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1195 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1196 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1197 if (sarg
& CHR_TIOCM_CTS
)
1199 if (sarg
& CHR_TIOCM_CAR
)
1201 if (sarg
& CHR_TIOCM_DSR
)
1203 if (sarg
& CHR_TIOCM_RI
)
1205 if (sarg
& CHR_TIOCM_DTR
)
1207 if (sarg
& CHR_TIOCM_RTS
)
1209 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1218 static void qemu_chr_close_tty(CharDriverState
*chr
)
1220 FDCharDriver
*s
= chr
->opaque
;
1234 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1236 CharDriverState
*chr
;
1238 tty_serial_init(fd
, 115200, 'N', 8, 1);
1239 chr
= qemu_chr_open_fd(fd
, fd
);
1240 chr
->chr_ioctl
= tty_serial_ioctl
;
1241 chr
->chr_close
= qemu_chr_close_tty
;
1245 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1247 const char *filename
= qemu_opt_get(opts
, "path");
1250 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1254 return qemu_chr_open_tty_fd(fd
);
1256 #endif /* __linux__ || __sun__ */
1258 #if defined(__linux__)
1260 #define HAVE_CHARDEV_PARPORT 1
1265 } ParallelCharDriver
;
1267 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1269 if (s
->mode
!= mode
) {
1271 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1278 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1280 ParallelCharDriver
*drv
= chr
->opaque
;
1285 case CHR_IOCTL_PP_READ_DATA
:
1286 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1288 *(uint8_t *)arg
= b
;
1290 case CHR_IOCTL_PP_WRITE_DATA
:
1291 b
= *(uint8_t *)arg
;
1292 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1295 case CHR_IOCTL_PP_READ_CONTROL
:
1296 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1298 /* Linux gives only the lowest bits, and no way to know data
1299 direction! For better compatibility set the fixed upper
1301 *(uint8_t *)arg
= b
| 0xc0;
1303 case CHR_IOCTL_PP_WRITE_CONTROL
:
1304 b
= *(uint8_t *)arg
;
1305 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1308 case CHR_IOCTL_PP_READ_STATUS
:
1309 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1311 *(uint8_t *)arg
= b
;
1313 case CHR_IOCTL_PP_DATA_DIR
:
1314 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1317 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1318 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1319 struct ParallelIOArg
*parg
= arg
;
1320 int n
= read(fd
, parg
->buffer
, parg
->count
);
1321 if (n
!= parg
->count
) {
1326 case CHR_IOCTL_PP_EPP_READ
:
1327 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1328 struct ParallelIOArg
*parg
= arg
;
1329 int n
= read(fd
, parg
->buffer
, parg
->count
);
1330 if (n
!= parg
->count
) {
1335 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1336 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1337 struct ParallelIOArg
*parg
= arg
;
1338 int n
= write(fd
, parg
->buffer
, parg
->count
);
1339 if (n
!= parg
->count
) {
1344 case CHR_IOCTL_PP_EPP_WRITE
:
1345 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1346 struct ParallelIOArg
*parg
= arg
;
1347 int n
= write(fd
, parg
->buffer
, parg
->count
);
1348 if (n
!= parg
->count
) {
1359 static void pp_close(CharDriverState
*chr
)
1361 ParallelCharDriver
*drv
= chr
->opaque
;
1364 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1365 ioctl(fd
, PPRELEASE
);
1368 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1371 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1373 CharDriverState
*chr
;
1374 ParallelCharDriver
*drv
;
1376 if (ioctl(fd
, PPCLAIM
) < 0) {
1381 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1383 drv
->mode
= IEEE1284_MODE_COMPAT
;
1385 chr
= g_malloc0(sizeof(CharDriverState
));
1386 chr
->chr_write
= null_chr_write
;
1387 chr
->chr_ioctl
= pp_ioctl
;
1388 chr
->chr_close
= pp_close
;
1391 qemu_chr_generic_open(chr
);
1395 #endif /* __linux__ */
1397 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1399 #define HAVE_CHARDEV_PARPORT 1
1401 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1403 int fd
= (int)(intptr_t)chr
->opaque
;
1407 case CHR_IOCTL_PP_READ_DATA
:
1408 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1410 *(uint8_t *)arg
= b
;
1412 case CHR_IOCTL_PP_WRITE_DATA
:
1413 b
= *(uint8_t *)arg
;
1414 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1417 case CHR_IOCTL_PP_READ_CONTROL
:
1418 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1420 *(uint8_t *)arg
= b
;
1422 case CHR_IOCTL_PP_WRITE_CONTROL
:
1423 b
= *(uint8_t *)arg
;
1424 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1427 case CHR_IOCTL_PP_READ_STATUS
:
1428 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1430 *(uint8_t *)arg
= b
;
1438 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1440 CharDriverState
*chr
;
1442 chr
= g_malloc0(sizeof(CharDriverState
));
1443 chr
->opaque
= (void *)(intptr_t)fd
;
1444 chr
->chr_write
= null_chr_write
;
1445 chr
->chr_ioctl
= pp_ioctl
;
1452 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1456 HANDLE hcom
, hrecv
, hsend
;
1457 OVERLAPPED orecv
, osend
;
1464 HANDLE hInputReadyEvent
;
1465 HANDLE hInputDoneEvent
;
1466 HANDLE hInputThread
;
1467 uint8_t win_stdio_buf
;
1468 } WinStdioCharState
;
1470 #define NSENDBUF 2048
1471 #define NRECVBUF 2048
1472 #define MAXCONNECT 1
1473 #define NTIMEOUT 5000
1475 static int win_chr_poll(void *opaque
);
1476 static int win_chr_pipe_poll(void *opaque
);
1478 static void win_chr_close(CharDriverState
*chr
)
1480 WinCharState
*s
= chr
->opaque
;
1483 CloseHandle(s
->hsend
);
1487 CloseHandle(s
->hrecv
);
1491 CloseHandle(s
->hcom
);
1495 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1497 qemu_del_polling_cb(win_chr_poll
, chr
);
1499 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1502 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1504 WinCharState
*s
= chr
->opaque
;
1506 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1511 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1513 fprintf(stderr
, "Failed CreateEvent\n");
1516 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1518 fprintf(stderr
, "Failed CreateEvent\n");
1522 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1523 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1524 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1525 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1530 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1531 fprintf(stderr
, "Failed SetupComm\n");
1535 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1536 size
= sizeof(COMMCONFIG
);
1537 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1538 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1539 CommConfigDialog(filename
, NULL
, &comcfg
);
1541 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1542 fprintf(stderr
, "Failed SetCommState\n");
1546 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1547 fprintf(stderr
, "Failed SetCommMask\n");
1551 cto
.ReadIntervalTimeout
= MAXDWORD
;
1552 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1553 fprintf(stderr
, "Failed SetCommTimeouts\n");
1557 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1558 fprintf(stderr
, "Failed ClearCommError\n");
1561 qemu_add_polling_cb(win_chr_poll
, chr
);
1569 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1571 WinCharState
*s
= chr
->opaque
;
1572 DWORD len
, ret
, size
, err
;
1575 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1576 s
->osend
.hEvent
= s
->hsend
;
1579 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1581 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1583 err
= GetLastError();
1584 if (err
== ERROR_IO_PENDING
) {
1585 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1603 static int win_chr_read_poll(CharDriverState
*chr
)
1605 WinCharState
*s
= chr
->opaque
;
1607 s
->max_size
= qemu_chr_be_can_write(chr
);
1611 static void win_chr_readfile(CharDriverState
*chr
)
1613 WinCharState
*s
= chr
->opaque
;
1615 uint8_t buf
[READ_BUF_LEN
];
1618 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1619 s
->orecv
.hEvent
= s
->hrecv
;
1620 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1622 err
= GetLastError();
1623 if (err
== ERROR_IO_PENDING
) {
1624 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1629 qemu_chr_be_write(chr
, buf
, size
);
1633 static void win_chr_read(CharDriverState
*chr
)
1635 WinCharState
*s
= chr
->opaque
;
1637 if (s
->len
> s
->max_size
)
1638 s
->len
= s
->max_size
;
1642 win_chr_readfile(chr
);
1645 static int win_chr_poll(void *opaque
)
1647 CharDriverState
*chr
= opaque
;
1648 WinCharState
*s
= chr
->opaque
;
1652 ClearCommError(s
->hcom
, &comerr
, &status
);
1653 if (status
.cbInQue
> 0) {
1654 s
->len
= status
.cbInQue
;
1655 win_chr_read_poll(chr
);
1662 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1664 CharDriverState
*chr
;
1667 chr
= g_malloc0(sizeof(CharDriverState
));
1668 s
= g_malloc0(sizeof(WinCharState
));
1670 chr
->chr_write
= win_chr_write
;
1671 chr
->chr_close
= win_chr_close
;
1673 if (win_chr_init(chr
, filename
) < 0) {
1678 qemu_chr_generic_open(chr
);
1682 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1684 return qemu_chr_open_win_path(qemu_opt_get(opts
, "path"));
1687 static int win_chr_pipe_poll(void *opaque
)
1689 CharDriverState
*chr
= opaque
;
1690 WinCharState
*s
= chr
->opaque
;
1693 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1696 win_chr_read_poll(chr
);
1703 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1705 WinCharState
*s
= chr
->opaque
;
1713 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1715 fprintf(stderr
, "Failed CreateEvent\n");
1718 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1720 fprintf(stderr
, "Failed CreateEvent\n");
1724 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1725 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1726 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1728 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1729 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1730 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1735 ZeroMemory(&ov
, sizeof(ov
));
1736 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1737 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1739 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1743 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1745 fprintf(stderr
, "Failed GetOverlappedResult\n");
1747 CloseHandle(ov
.hEvent
);
1754 CloseHandle(ov
.hEvent
);
1757 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1766 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1768 const char *filename
= qemu_opt_get(opts
, "path");
1769 CharDriverState
*chr
;
1772 chr
= g_malloc0(sizeof(CharDriverState
));
1773 s
= g_malloc0(sizeof(WinCharState
));
1775 chr
->chr_write
= win_chr_write
;
1776 chr
->chr_close
= win_chr_close
;
1778 if (win_chr_pipe_init(chr
, filename
) < 0) {
1783 qemu_chr_generic_open(chr
);
1787 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1789 CharDriverState
*chr
;
1792 chr
= g_malloc0(sizeof(CharDriverState
));
1793 s
= g_malloc0(sizeof(WinCharState
));
1796 chr
->chr_write
= win_chr_write
;
1797 qemu_chr_generic_open(chr
);
1801 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1803 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1806 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1808 const char *file_out
= qemu_opt_get(opts
, "path");
1811 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1812 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1813 if (fd_out
== INVALID_HANDLE_VALUE
) {
1817 return qemu_chr_open_win_file(fd_out
);
1820 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1822 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1829 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1839 static void win_stdio_wait_func(void *opaque
)
1841 CharDriverState
*chr
= opaque
;
1842 WinStdioCharState
*stdio
= chr
->opaque
;
1843 INPUT_RECORD buf
[4];
1848 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1852 /* Avoid error storm */
1853 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1857 for (i
= 0; i
< dwSize
; i
++) {
1858 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1860 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1862 if (kev
->uChar
.AsciiChar
!= 0) {
1863 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1864 if (qemu_chr_be_can_write(chr
)) {
1865 uint8_t c
= kev
->uChar
.AsciiChar
;
1866 qemu_chr_be_write(chr
, &c
, 1);
1874 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
1876 CharDriverState
*chr
= param
;
1877 WinStdioCharState
*stdio
= chr
->opaque
;
1883 /* Wait for one byte */
1884 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
1886 /* Exit in case of error, continue if nothing read */
1894 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1895 if (stdio
->win_stdio_buf
== '\r') {
1899 /* Signal the main thread and wait until the byte was eaten */
1900 if (!SetEvent(stdio
->hInputReadyEvent
)) {
1903 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
1909 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
1913 static void win_stdio_thread_wait_func(void *opaque
)
1915 CharDriverState
*chr
= opaque
;
1916 WinStdioCharState
*stdio
= chr
->opaque
;
1918 if (qemu_chr_be_can_write(chr
)) {
1919 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
1922 SetEvent(stdio
->hInputDoneEvent
);
1925 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
1927 WinStdioCharState
*stdio
= chr
->opaque
;
1930 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
1933 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
1935 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
1939 static void win_stdio_close(CharDriverState
*chr
)
1941 WinStdioCharState
*stdio
= chr
->opaque
;
1943 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
1944 CloseHandle(stdio
->hInputReadyEvent
);
1946 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
1947 CloseHandle(stdio
->hInputDoneEvent
);
1949 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
1950 TerminateThread(stdio
->hInputThread
, 0);
1953 g_free(chr
->opaque
);
1958 static CharDriverState
*qemu_chr_open_win_stdio(QemuOpts
*opts
)
1960 CharDriverState
*chr
;
1961 WinStdioCharState
*stdio
;
1965 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
1966 || ((display_type
!= DT_NOGRAPHIC
) && (stdio_nb_clients
!= 0))) {
1970 chr
= g_malloc0(sizeof(CharDriverState
));
1971 stdio
= g_malloc0(sizeof(WinStdioCharState
));
1973 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
1974 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
1975 fprintf(stderr
, "cannot open stdio: invalid handle\n");
1979 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
1981 chr
->opaque
= stdio
;
1982 chr
->chr_write
= win_stdio_write
;
1983 chr
->chr_close
= win_stdio_close
;
1985 if (stdio_nb_clients
== 0) {
1987 if (qemu_add_wait_object(stdio
->hStdIn
,
1988 win_stdio_wait_func
, chr
)) {
1989 fprintf(stderr
, "qemu_add_wait_object: failed\n");
1994 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1995 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1996 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
1999 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2000 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2001 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2002 fprintf(stderr
, "cannot create stdio thread or event\n");
2005 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2006 win_stdio_thread_wait_func
, chr
)) {
2007 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2012 dwMode
|= ENABLE_LINE_INPUT
;
2014 stdio_clients
[stdio_nb_clients
++] = chr
;
2015 if (stdio_nb_clients
== 1 && is_console
) {
2016 /* set the terminal in raw mode */
2017 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2018 dwMode
|= ENABLE_PROCESSED_INPUT
;
2021 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2023 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2024 qemu_chr_fe_set_echo(chr
, false);
2028 #endif /* !_WIN32 */
2030 /***********************************************************/
2031 /* UDP Net console */
2035 uint8_t buf
[READ_BUF_LEN
];
2041 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2043 NetCharDriver
*s
= chr
->opaque
;
2045 return send(s
->fd
, (const void *)buf
, len
, 0);
2048 static int udp_chr_read_poll(void *opaque
)
2050 CharDriverState
*chr
= opaque
;
2051 NetCharDriver
*s
= chr
->opaque
;
2053 s
->max_size
= qemu_chr_be_can_write(chr
);
2055 /* If there were any stray characters in the queue process them
2058 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2059 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2061 s
->max_size
= qemu_chr_be_can_write(chr
);
2066 static void udp_chr_read(void *opaque
)
2068 CharDriverState
*chr
= opaque
;
2069 NetCharDriver
*s
= chr
->opaque
;
2071 if (s
->max_size
== 0)
2073 s
->bufcnt
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2074 s
->bufptr
= s
->bufcnt
;
2079 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2080 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2082 s
->max_size
= qemu_chr_be_can_write(chr
);
2086 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2088 NetCharDriver
*s
= chr
->opaque
;
2091 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2092 udp_chr_read
, NULL
, chr
);
2096 static void udp_chr_close(CharDriverState
*chr
)
2098 NetCharDriver
*s
= chr
->opaque
;
2100 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2104 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2107 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2109 CharDriverState
*chr
= NULL
;
2110 NetCharDriver
*s
= NULL
;
2111 Error
*local_err
= NULL
;
2114 chr
= g_malloc0(sizeof(CharDriverState
));
2115 s
= g_malloc0(sizeof(NetCharDriver
));
2117 fd
= inet_dgram_opts(opts
, &local_err
);
2126 chr
->chr_write
= udp_chr_write
;
2127 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2128 chr
->chr_close
= udp_chr_close
;
2133 qerror_report_err(local_err
);
2134 error_free(local_err
);
2144 /***********************************************************/
2145 /* TCP Net console */
2157 static void tcp_chr_accept(void *opaque
);
2159 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2161 TCPCharDriver
*s
= chr
->opaque
;
2163 return send_all(s
->fd
, buf
, len
);
2165 /* XXX: indicate an error ? */
2170 static int tcp_chr_read_poll(void *opaque
)
2172 CharDriverState
*chr
= opaque
;
2173 TCPCharDriver
*s
= chr
->opaque
;
2176 s
->max_size
= qemu_chr_be_can_write(chr
);
2181 #define IAC_BREAK 243
2182 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2184 uint8_t *buf
, int *size
)
2186 /* Handle any telnet client's basic IAC options to satisfy char by
2187 * char mode with no echo. All IAC options will be removed from
2188 * the buf and the do_telnetopt variable will be used to track the
2189 * state of the width of the IAC information.
2191 * IAC commands come in sets of 3 bytes with the exception of the
2192 * "IAC BREAK" command and the double IAC.
2198 for (i
= 0; i
< *size
; i
++) {
2199 if (s
->do_telnetopt
> 1) {
2200 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2201 /* Double IAC means send an IAC */
2205 s
->do_telnetopt
= 1;
2207 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2208 /* Handle IAC break commands by sending a serial break */
2209 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2214 if (s
->do_telnetopt
>= 4) {
2215 s
->do_telnetopt
= 1;
2218 if ((unsigned char)buf
[i
] == IAC
) {
2219 s
->do_telnetopt
= 2;
2230 static int tcp_get_msgfd(CharDriverState
*chr
)
2232 TCPCharDriver
*s
= chr
->opaque
;
2239 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2241 TCPCharDriver
*s
= chr
->opaque
;
2242 struct cmsghdr
*cmsg
;
2244 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2247 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2248 cmsg
->cmsg_level
!= SOL_SOCKET
||
2249 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2252 fd
= *((int *)CMSG_DATA(cmsg
));
2256 #ifndef MSG_CMSG_CLOEXEC
2257 qemu_set_cloexec(fd
);
2265 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2267 TCPCharDriver
*s
= chr
->opaque
;
2268 struct msghdr msg
= { NULL
, };
2269 struct iovec iov
[1];
2271 struct cmsghdr cmsg
;
2272 char control
[CMSG_SPACE(sizeof(int))];
2277 iov
[0].iov_base
= buf
;
2278 iov
[0].iov_len
= len
;
2282 msg
.msg_control
= &msg_control
;
2283 msg
.msg_controllen
= sizeof(msg_control
);
2285 #ifdef MSG_CMSG_CLOEXEC
2286 flags
|= MSG_CMSG_CLOEXEC
;
2288 ret
= recvmsg(s
->fd
, &msg
, flags
);
2289 if (ret
> 0 && s
->is_unix
) {
2290 unix_process_msgfd(chr
, &msg
);
2296 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2298 TCPCharDriver
*s
= chr
->opaque
;
2299 return qemu_recv(s
->fd
, buf
, len
, 0);
2303 static void tcp_chr_read(void *opaque
)
2305 CharDriverState
*chr
= opaque
;
2306 TCPCharDriver
*s
= chr
->opaque
;
2307 uint8_t buf
[READ_BUF_LEN
];
2310 if (!s
->connected
|| s
->max_size
<= 0)
2313 if (len
> s
->max_size
)
2315 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2317 /* connection closed */
2319 if (s
->listen_fd
>= 0) {
2320 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2322 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2325 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2326 } else if (size
> 0) {
2327 if (s
->do_telnetopt
)
2328 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2330 qemu_chr_be_write(chr
, buf
, size
);
2335 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2337 return qemu_chr_open_fd(eventfd
, eventfd
);
2341 static void tcp_chr_connect(void *opaque
)
2343 CharDriverState
*chr
= opaque
;
2344 TCPCharDriver
*s
= chr
->opaque
;
2348 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2349 tcp_chr_read
, NULL
, chr
);
2351 qemu_chr_generic_open(chr
);
2354 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2355 static void tcp_chr_telnet_init(int fd
)
2358 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2359 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2360 send(fd
, (char *)buf
, 3, 0);
2361 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2362 send(fd
, (char *)buf
, 3, 0);
2363 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2364 send(fd
, (char *)buf
, 3, 0);
2365 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2366 send(fd
, (char *)buf
, 3, 0);
2369 static void socket_set_nodelay(int fd
)
2372 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2375 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2377 TCPCharDriver
*s
= chr
->opaque
;
2381 socket_set_nonblock(fd
);
2383 socket_set_nodelay(fd
);
2385 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2386 tcp_chr_connect(chr
);
2391 static void tcp_chr_accept(void *opaque
)
2393 CharDriverState
*chr
= opaque
;
2394 TCPCharDriver
*s
= chr
->opaque
;
2395 struct sockaddr_in saddr
;
2397 struct sockaddr_un uaddr
;
2399 struct sockaddr
*addr
;
2406 len
= sizeof(uaddr
);
2407 addr
= (struct sockaddr
*)&uaddr
;
2411 len
= sizeof(saddr
);
2412 addr
= (struct sockaddr
*)&saddr
;
2414 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2415 if (fd
< 0 && errno
!= EINTR
) {
2417 } else if (fd
>= 0) {
2418 if (s
->do_telnetopt
)
2419 tcp_chr_telnet_init(fd
);
2423 if (tcp_chr_add_client(chr
, fd
) < 0)
2427 static void tcp_chr_close(CharDriverState
*chr
)
2429 TCPCharDriver
*s
= chr
->opaque
;
2431 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2434 if (s
->listen_fd
>= 0) {
2435 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2436 closesocket(s
->listen_fd
);
2439 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2442 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2443 bool is_listen
, bool is_telnet
,
2444 bool is_waitconnect
,
2447 CharDriverState
*chr
= NULL
;
2448 TCPCharDriver
*s
= NULL
;
2449 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2450 const char *left
= "", *right
= "";
2451 struct sockaddr_storage ss
;
2452 socklen_t ss_len
= sizeof(ss
);
2454 memset(&ss
, 0, ss_len
);
2455 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2456 error_setg(errp
, "getsockname: %s", strerror(errno
));
2460 chr
= g_malloc0(sizeof(CharDriverState
));
2461 s
= g_malloc0(sizeof(TCPCharDriver
));
2468 chr
->filename
= g_malloc(256);
2469 switch (ss
.ss_family
) {
2473 snprintf(chr
->filename
, 256, "unix:%s%s",
2474 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2475 is_listen
? ",server" : "");
2483 s
->do_nodelay
= do_nodelay
;
2484 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2485 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2486 snprintf(chr
->filename
, 256, "%s:%s:%s%s%s%s",
2487 is_telnet
? "telnet" : "tcp",
2488 left
, host
, right
, serv
,
2489 is_listen
? ",server" : "");
2494 chr
->chr_write
= tcp_chr_write
;
2495 chr
->chr_close
= tcp_chr_close
;
2496 chr
->get_msgfd
= tcp_get_msgfd
;
2497 chr
->chr_add_client
= tcp_chr_add_client
;
2501 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2503 s
->do_telnetopt
= 1;
2508 socket_set_nodelay(fd
);
2509 tcp_chr_connect(chr
);
2512 if (is_listen
&& is_waitconnect
) {
2513 printf("QEMU waiting for connection on: %s\n",
2515 tcp_chr_accept(chr
);
2516 socket_set_nonblock(s
->listen_fd
);
2521 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2523 CharDriverState
*chr
= NULL
;
2524 Error
*local_err
= NULL
;
2532 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2533 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2534 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2535 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2536 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2542 fd
= unix_listen_opts(opts
, &local_err
);
2544 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2548 fd
= inet_listen_opts(opts
, 0, &local_err
);
2550 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2557 if (!is_waitconnect
)
2558 socket_set_nonblock(fd
);
2560 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2561 is_waitconnect
, &local_err
);
2562 if (error_is_set(&local_err
)) {
2570 qerror_report_err(local_err
);
2571 error_free(local_err
);
2577 g_free(chr
->opaque
);
2583 /***********************************************************/
2584 /* Memory chardev */
2587 size_t outbuf_capacity
;
2591 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2593 MemoryDriver
*d
= chr
->opaque
;
2595 /* TODO: the QString implementation has the same code, we should
2596 * introduce a generic way to do this in cutils.c */
2597 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2599 d
->outbuf_capacity
+= len
;
2600 d
->outbuf_capacity
*= 2;
2601 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2604 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2605 d
->outbuf_size
+= len
;
2610 void qemu_chr_init_mem(CharDriverState
*chr
)
2614 d
= g_malloc(sizeof(*d
));
2616 d
->outbuf_capacity
= 4096;
2617 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2619 memset(chr
, 0, sizeof(*chr
));
2621 chr
->chr_write
= mem_chr_write
;
2624 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2626 MemoryDriver
*d
= chr
->opaque
;
2627 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2630 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2631 void qemu_chr_close_mem(CharDriverState
*chr
)
2633 MemoryDriver
*d
= chr
->opaque
;
2636 g_free(chr
->opaque
);
2638 chr
->chr_write
= NULL
;
2641 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2643 const MemoryDriver
*d
= chr
->opaque
;
2644 return d
->outbuf_size
;
2647 /*********************************************************/
2648 /*CircularMemory chardev*/
2657 static bool cirmem_chr_is_empty(const CharDriverState
*chr
)
2659 const CirMemCharDriver
*d
= chr
->opaque
;
2661 return d
->cons
== d
->prod
;
2664 static size_t qemu_chr_cirmem_count(const CharDriverState
*chr
)
2666 const CirMemCharDriver
*d
= chr
->opaque
;
2668 return (d
->prod
- d
->cons
);
2671 static int cirmem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2673 CirMemCharDriver
*d
= chr
->opaque
;
2676 if (!buf
|| (len
< 0)) {
2680 for (i
= 0; i
< len
; i
++ ) {
2681 /* Avoid writing the IAC information to the queue. */
2682 if ((unsigned char)buf
[i
] == IAC
) {
2686 d
->cbuf
[d
->prod
++ % d
->size
] = buf
[i
];
2687 if ((d
->prod
- d
->cons
) > d
->size
) {
2688 d
->cons
= d
->prod
- d
->size
;
2695 static int cirmem_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2697 CirMemCharDriver
*d
= chr
->opaque
;
2700 for (i
= 0; i
< len
&& !cirmem_chr_is_empty(chr
); i
++) {
2701 buf
[i
] = d
->cbuf
[d
->cons
++ % d
->size
];
2707 static void cirmem_chr_close(struct CharDriverState
*chr
)
2709 CirMemCharDriver
*d
= chr
->opaque
;
2716 static CharDriverState
*qemu_chr_open_cirmemchr(QemuOpts
*opts
)
2718 CharDriverState
*chr
;
2719 CirMemCharDriver
*d
;
2721 chr
= g_malloc0(sizeof(CharDriverState
));
2722 d
= g_malloc(sizeof(*d
));
2724 d
->size
= qemu_opt_get_number(opts
, "maxcapacity", 0);
2726 d
->size
= CBUFF_SIZE
;
2729 /* The size must be power of 2 */
2730 if (d
->size
& (d
->size
- 1)) {
2731 fprintf(stderr
, "chardev: size of memory device must be power of 2\n");
2737 d
->cbuf
= g_malloc0(d
->size
);
2740 chr
->chr_write
= cirmem_chr_write
;
2741 chr
->chr_close
= cirmem_chr_close
;
2751 static bool qemu_is_chr(const CharDriverState
*chr
, const char *filename
)
2753 return strcmp(chr
->filename
, filename
);
2756 void qmp_memchar_write(const char *device
, int64_t size
,
2757 const char *data
, bool has_format
,
2758 enum DataFormat format
,
2761 CharDriverState
*chr
;
2766 chr
= qemu_chr_find(device
);
2768 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2772 if (qemu_is_chr(chr
, "memory")) {
2773 error_setg(errp
,"%s is not memory char device", device
);
2777 write_count
= (gsize
)size
;
2779 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2780 write_data
= g_base64_decode(data
, &write_count
);
2782 write_data
= (uint8_t *)data
;
2785 ret
= cirmem_chr_write(chr
, write_data
, write_count
);
2788 error_setg(errp
, "Failed to write to device %s", device
);
2793 MemCharRead
*qmp_memchar_read(const char *device
, int64_t size
,
2794 bool has_format
, enum DataFormat format
,
2797 CharDriverState
*chr
;
2799 MemCharRead
*meminfo
;
2802 chr
= qemu_chr_find(device
);
2804 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2808 if (qemu_is_chr(chr
, "memory")) {
2809 error_setg(errp
,"%s is not memory char device", device
);
2814 error_setg(errp
, "size must be greater than zero");
2818 meminfo
= g_malloc0(sizeof(MemCharRead
));
2820 count
= qemu_chr_cirmem_count(chr
);
2822 meminfo
->data
= g_strdup("");
2826 size
= size
> count
? count
: size
;
2827 read_data
= g_malloc0(size
+ 1);
2829 meminfo
->count
= cirmem_chr_read(chr
, read_data
, size
);
2831 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2832 meminfo
->data
= g_base64_encode(read_data
, (size_t)meminfo
->count
);
2834 meminfo
->data
= (char *)read_data
;
2840 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2842 char host
[65], port
[33], width
[8], height
[8];
2846 Error
*local_err
= NULL
;
2848 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2849 if (error_is_set(&local_err
)) {
2850 qerror_report_err(local_err
);
2851 error_free(local_err
);
2855 if (strstart(filename
, "mon:", &p
)) {
2857 qemu_opt_set(opts
, "mux", "on");
2860 if (strcmp(filename
, "null") == 0 ||
2861 strcmp(filename
, "pty") == 0 ||
2862 strcmp(filename
, "msmouse") == 0 ||
2863 strcmp(filename
, "braille") == 0 ||
2864 strcmp(filename
, "stdio") == 0) {
2865 qemu_opt_set(opts
, "backend", filename
);
2868 if (strstart(filename
, "vc", &p
)) {
2869 qemu_opt_set(opts
, "backend", "vc");
2871 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2873 qemu_opt_set(opts
, "width", width
);
2874 qemu_opt_set(opts
, "height", height
);
2875 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2877 qemu_opt_set(opts
, "cols", width
);
2878 qemu_opt_set(opts
, "rows", height
);
2885 if (strcmp(filename
, "con:") == 0) {
2886 qemu_opt_set(opts
, "backend", "console");
2889 if (strstart(filename
, "COM", NULL
)) {
2890 qemu_opt_set(opts
, "backend", "serial");
2891 qemu_opt_set(opts
, "path", filename
);
2894 if (strstart(filename
, "memory", &p
)) {
2895 qemu_opt_set(opts
, "backend", "memory");
2896 qemu_opt_set(opts
, "maxcapacity", p
);
2899 if (strstart(filename
, "file:", &p
)) {
2900 qemu_opt_set(opts
, "backend", "file");
2901 qemu_opt_set(opts
, "path", p
);
2904 if (strstart(filename
, "pipe:", &p
)) {
2905 qemu_opt_set(opts
, "backend", "pipe");
2906 qemu_opt_set(opts
, "path", p
);
2909 if (strstart(filename
, "tcp:", &p
) ||
2910 strstart(filename
, "telnet:", &p
)) {
2911 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2913 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2916 qemu_opt_set(opts
, "backend", "socket");
2917 qemu_opt_set(opts
, "host", host
);
2918 qemu_opt_set(opts
, "port", port
);
2919 if (p
[pos
] == ',') {
2920 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2923 if (strstart(filename
, "telnet:", &p
))
2924 qemu_opt_set(opts
, "telnet", "on");
2927 if (strstart(filename
, "udp:", &p
)) {
2928 qemu_opt_set(opts
, "backend", "udp");
2929 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2931 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2935 qemu_opt_set(opts
, "host", host
);
2936 qemu_opt_set(opts
, "port", port
);
2937 if (p
[pos
] == '@') {
2939 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2941 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2945 qemu_opt_set(opts
, "localaddr", host
);
2946 qemu_opt_set(opts
, "localport", port
);
2950 if (strstart(filename
, "unix:", &p
)) {
2951 qemu_opt_set(opts
, "backend", "socket");
2952 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2956 if (strstart(filename
, "/dev/parport", NULL
) ||
2957 strstart(filename
, "/dev/ppi", NULL
)) {
2958 qemu_opt_set(opts
, "backend", "parport");
2959 qemu_opt_set(opts
, "path", filename
);
2962 if (strstart(filename
, "/dev/", NULL
)) {
2963 qemu_opt_set(opts
, "backend", "tty");
2964 qemu_opt_set(opts
, "path", filename
);
2969 qemu_opts_del(opts
);
2973 #ifdef HAVE_CHARDEV_PARPORT
2975 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
2977 const char *filename
= qemu_opt_get(opts
, "path");
2980 fd
= qemu_open(filename
, O_RDWR
);
2984 return qemu_chr_open_pp_fd(fd
);
2989 static const struct {
2991 CharDriverState
*(*open
)(QemuOpts
*opts
);
2992 } backend_table
[] = {
2993 { .name
= "null", .open
= qemu_chr_open_null
},
2994 { .name
= "socket", .open
= qemu_chr_open_socket
},
2995 { .name
= "udp", .open
= qemu_chr_open_udp
},
2996 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2997 { .name
= "vc", .open
= text_console_init
},
2998 { .name
= "memory", .open
= qemu_chr_open_cirmemchr
},
3000 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
3001 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
3002 { .name
= "console", .open
= qemu_chr_open_win_con
},
3003 { .name
= "serial", .open
= qemu_chr_open_win
},
3004 { .name
= "stdio", .open
= qemu_chr_open_win_stdio
},
3006 { .name
= "file", .open
= qemu_chr_open_file_out
},
3007 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
3008 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
3010 #ifdef CONFIG_BRLAPI
3011 { .name
= "braille", .open
= chr_baum_init
},
3013 #ifdef HAVE_CHARDEV_TTY
3014 { .name
= "tty", .open
= qemu_chr_open_tty
},
3015 { .name
= "serial", .open
= qemu_chr_open_tty
},
3016 { .name
= "pty", .open
= qemu_chr_open_pty
},
3018 #ifdef HAVE_CHARDEV_PARPORT
3019 { .name
= "parallel", .open
= qemu_chr_open_pp
},
3020 { .name
= "parport", .open
= qemu_chr_open_pp
},
3023 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
3024 #if SPICE_SERVER_VERSION >= 0x000c02
3025 { .name
= "spiceport", .open
= qemu_chr_open_spice_port
},
3030 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3031 void (*init
)(struct CharDriverState
*s
),
3034 CharDriverState
*chr
;
3037 if (qemu_opts_id(opts
) == NULL
) {
3038 error_setg(errp
, "chardev: no id specified\n");
3042 if (qemu_opt_get(opts
, "backend") == NULL
) {
3043 error_setg(errp
, "chardev: \"%s\" missing backend\n",
3044 qemu_opts_id(opts
));
3047 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
3048 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
3051 if (i
== ARRAY_SIZE(backend_table
)) {
3052 error_setg(errp
, "chardev: backend \"%s\" not found\n",
3053 qemu_opt_get(opts
, "backend"));
3057 chr
= backend_table
[i
].open(opts
);
3059 error_setg(errp
, "chardev: opening backend \"%s\" failed\n",
3060 qemu_opt_get(opts
, "backend"));
3065 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3067 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3069 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3070 CharDriverState
*base
= chr
;
3071 int len
= strlen(qemu_opts_id(opts
)) + 6;
3072 base
->label
= g_malloc(len
);
3073 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3074 chr
= qemu_chr_open_mux(base
);
3075 chr
->filename
= base
->filename
;
3076 chr
->avail_connections
= MAX_MUX
;
3077 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3079 chr
->avail_connections
= 1;
3081 chr
->label
= g_strdup(qemu_opts_id(opts
));
3086 qemu_opts_del(opts
);
3090 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3093 CharDriverState
*chr
;
3097 if (strstart(filename
, "chardev:", &p
)) {
3098 return qemu_chr_find(p
);
3101 opts
= qemu_chr_parse_compat(label
, filename
);
3105 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3106 if (error_is_set(&err
)) {
3107 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3110 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3111 monitor_init(chr
, MONITOR_USE_READLINE
);
3116 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3118 if (chr
->chr_set_echo
) {
3119 chr
->chr_set_echo(chr
, echo
);
3123 void qemu_chr_fe_open(struct CharDriverState
*chr
)
3125 if (chr
->chr_guest_open
) {
3126 chr
->chr_guest_open(chr
);
3130 void qemu_chr_fe_close(struct CharDriverState
*chr
)
3132 if (chr
->chr_guest_close
) {
3133 chr
->chr_guest_close(chr
);
3137 void qemu_chr_delete(CharDriverState
*chr
)
3139 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3140 if (chr
->chr_close
) {
3141 chr
->chr_close(chr
);
3143 g_free(chr
->filename
);
3146 qemu_opts_del(chr
->opts
);
3151 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3153 ChardevInfoList
*chr_list
= NULL
;
3154 CharDriverState
*chr
;
3156 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3157 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3158 info
->value
= g_malloc0(sizeof(*info
->value
));
3159 info
->value
->label
= g_strdup(chr
->label
);
3160 info
->value
->filename
= g_strdup(chr
->filename
);
3162 info
->next
= chr_list
;
3169 CharDriverState
*qemu_chr_find(const char *name
)
3171 CharDriverState
*chr
;
3173 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3174 if (strcmp(chr
->label
, name
) != 0)
3181 /* Get a character (serial) device interface. */
3182 CharDriverState
*qemu_char_get_next_serial(void)
3184 static int next_serial
;
3186 /* FIXME: This function needs to go away: use chardev properties! */
3187 return serial_hds
[next_serial
++];
3190 QemuOptsList qemu_chardev_opts
= {
3192 .implied_opt_name
= "backend",
3193 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3197 .type
= QEMU_OPT_STRING
,
3200 .type
= QEMU_OPT_STRING
,
3203 .type
= QEMU_OPT_STRING
,
3206 .type
= QEMU_OPT_STRING
,
3208 .name
= "localaddr",
3209 .type
= QEMU_OPT_STRING
,
3211 .name
= "localport",
3212 .type
= QEMU_OPT_STRING
,
3215 .type
= QEMU_OPT_NUMBER
,
3218 .type
= QEMU_OPT_BOOL
,
3221 .type
= QEMU_OPT_BOOL
,
3224 .type
= QEMU_OPT_BOOL
,
3227 .type
= QEMU_OPT_BOOL
,
3230 .type
= QEMU_OPT_BOOL
,
3233 .type
= QEMU_OPT_BOOL
,
3236 .type
= QEMU_OPT_NUMBER
,
3239 .type
= QEMU_OPT_NUMBER
,
3242 .type
= QEMU_OPT_NUMBER
,
3245 .type
= QEMU_OPT_NUMBER
,
3248 .type
= QEMU_OPT_BOOL
,
3251 .type
= QEMU_OPT_BOOL
,
3254 .type
= QEMU_OPT_STRING
,
3257 .type
= QEMU_OPT_NUMBER
,
3259 .name
= "maxcapacity",
3260 .type
= QEMU_OPT_NUMBER
,
3262 { /* end of list */ }
3268 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3273 error_setg(errp
, "input file not supported");
3277 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3278 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3279 if (out
== INVALID_HANDLE_VALUE
) {
3280 error_setg(errp
, "open %s failed", file
->out
);
3283 return qemu_chr_open_win_file(out
);
3286 static CharDriverState
*qmp_chardev_open_port(ChardevPort
*port
, Error
**errp
)
3288 switch (port
->type
) {
3289 case CHARDEV_PORT_KIND_SERIAL
:
3290 return qemu_chr_open_win_path(port
->device
);
3292 error_setg(errp
, "unknown chardev port (%d)", port
->type
);
3299 static int qmp_chardev_open_file_source(char *src
, int flags
,
3304 TFR(fd
= qemu_open(src
, flags
, 0666));
3306 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3311 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3313 int flags
, in
= -1, out
= -1;
3315 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3316 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3317 if (error_is_set(errp
)) {
3323 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3324 if (error_is_set(errp
)) {
3330 return qemu_chr_open_fd(in
, out
);
3333 static CharDriverState
*qmp_chardev_open_port(ChardevPort
*port
, Error
**errp
)
3335 switch (port
->type
) {
3336 #ifdef HAVE_CHARDEV_TTY
3337 case CHARDEV_PORT_KIND_SERIAL
:
3341 fd
= qmp_chardev_open_file_source(port
->device
, flags
, errp
);
3342 if (error_is_set(errp
)) {
3345 socket_set_nonblock(fd
);
3346 return qemu_chr_open_tty_fd(fd
);
3349 #ifdef HAVE_CHARDEV_PARPORT
3350 case CHARDEV_PORT_KIND_PARALLEL
:
3354 fd
= qmp_chardev_open_file_source(port
->device
, flags
, errp
);
3355 if (error_is_set(errp
)) {
3358 return qemu_chr_open_pp_fd(fd
);
3362 error_setg(errp
, "unknown chardev port (%d)", port
->type
);
3369 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3372 SocketAddress
*addr
= sock
->addr
;
3373 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3374 bool is_listen
= sock
->has_server
? sock
->server
: true;
3375 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3376 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3380 fd
= socket_listen(addr
, errp
);
3382 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3384 if (error_is_set(errp
)) {
3387 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3388 is_telnet
, is_waitconnect
, errp
);
3391 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3394 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3395 CharDriverState
*chr
= NULL
;
3397 chr
= qemu_chr_find(id
);
3399 error_setg(errp
, "Chardev '%s' already exists", id
);
3404 switch (backend
->kind
) {
3405 case CHARDEV_BACKEND_KIND_FILE
:
3406 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3408 case CHARDEV_BACKEND_KIND_PORT
:
3409 chr
= qmp_chardev_open_port(backend
->port
, errp
);
3411 case CHARDEV_BACKEND_KIND_SOCKET
:
3412 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3414 #ifdef HAVE_CHARDEV_TTY
3415 case CHARDEV_BACKEND_KIND_PTY
:
3417 /* qemu_chr_open_pty sets "path" in opts */
3419 opts
= qemu_opts_create_nofail(qemu_find_opts("chardev"));
3420 chr
= qemu_chr_open_pty(opts
);
3421 ret
->pty
= g_strdup(qemu_opt_get(opts
, "path"));
3422 ret
->has_pty
= true;
3423 qemu_opts_del(opts
);
3427 case CHARDEV_BACKEND_KIND_NULL
:
3428 chr
= qemu_chr_open_null(NULL
);
3431 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3435 if (chr
== NULL
&& !error_is_set(errp
)) {
3436 error_setg(errp
, "Failed to create chardev");
3439 chr
->label
= g_strdup(id
);
3440 chr
->avail_connections
= 1;
3441 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3449 void qmp_chardev_remove(const char *id
, Error
**errp
)
3451 CharDriverState
*chr
;
3453 chr
= qemu_chr_find(id
);
3455 error_setg(errp
, "Chardev '%s' not found", id
);
3458 if (chr
->chr_can_read
|| chr
->chr_read
||
3459 chr
->chr_event
|| chr
->handler_opaque
) {
3460 error_setg(errp
, "Chardev '%s' is busy", id
);
3463 qemu_chr_delete(chr
);