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_generic_open_bh(void *opaque
)
128 CharDriverState
*s
= opaque
;
129 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
130 qemu_bh_delete(s
->bh
);
134 void qemu_chr_generic_open(CharDriverState
*s
)
137 s
->bh
= qemu_bh_new(qemu_chr_generic_open_bh
, s
);
138 qemu_bh_schedule(s
->bh
);
142 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
144 return s
->chr_write(s
, buf
, len
);
147 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
151 return s
->chr_ioctl(s
, cmd
, arg
);
154 int qemu_chr_be_can_write(CharDriverState
*s
)
156 if (!s
->chr_can_read
)
158 return s
->chr_can_read(s
->handler_opaque
);
161 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
164 s
->chr_read(s
->handler_opaque
, buf
, len
);
168 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
170 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
173 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
175 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
178 void qemu_chr_accept_input(CharDriverState
*s
)
180 if (s
->chr_accept_input
)
181 s
->chr_accept_input(s
);
185 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
187 char buf
[READ_BUF_LEN
];
190 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
191 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
195 void qemu_chr_add_handlers(CharDriverState
*s
,
196 IOCanReadHandler
*fd_can_read
,
197 IOReadHandler
*fd_read
,
198 IOEventHandler
*fd_event
,
201 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
202 /* chr driver being released. */
203 ++s
->avail_connections
;
205 s
->chr_can_read
= fd_can_read
;
206 s
->chr_read
= fd_read
;
207 s
->chr_event
= fd_event
;
208 s
->handler_opaque
= opaque
;
209 if (s
->chr_update_read_handler
)
210 s
->chr_update_read_handler(s
);
212 /* We're connecting to an already opened device, so let's make sure we
213 also get the open event */
215 qemu_chr_generic_open(s
);
219 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
224 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
226 CharDriverState
*chr
;
228 chr
= g_malloc0(sizeof(CharDriverState
));
229 chr
->chr_write
= null_chr_write
;
233 /* MUX driver for serial I/O splitting */
235 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
236 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
238 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
239 IOReadHandler
*chr_read
[MAX_MUX
];
240 IOEventHandler
*chr_event
[MAX_MUX
];
241 void *ext_opaque
[MAX_MUX
];
242 CharDriverState
*drv
;
247 /* Intermediate input buffer allows to catch escape sequences even if the
248 currently active device is not accepting any input - but only until it
250 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
255 int64_t timestamps_start
;
259 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
261 MuxDriver
*d
= chr
->opaque
;
263 if (!d
->timestamps
) {
264 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
269 for (i
= 0; i
< len
; i
++) {
275 ti
= qemu_get_clock_ms(rt_clock
);
276 if (d
->timestamps_start
== -1)
277 d
->timestamps_start
= ti
;
278 ti
-= d
->timestamps_start
;
280 snprintf(buf1
, sizeof(buf1
),
281 "[%02d:%02d:%02d.%03d] ",
286 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
289 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
290 if (buf
[i
] == '\n') {
298 static const char * const mux_help
[] = {
299 "% h print this help\n\r",
300 "% x exit emulator\n\r",
301 "% s save disk data back to file (if -snapshot)\n\r",
302 "% t toggle console timestamps\n\r"
303 "% b send break (magic sysrq)\n\r",
304 "% c switch between console and monitor\n\r",
309 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
310 static void mux_print_help(CharDriverState
*chr
)
313 char ebuf
[15] = "Escape-Char";
314 char cbuf
[50] = "\n\r";
316 if (term_escape_char
> 0 && term_escape_char
< 26) {
317 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
318 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
320 snprintf(cbuf
, sizeof(cbuf
),
321 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
324 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
325 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
326 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
327 if (mux_help
[i
][j
] == '%')
328 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
330 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
335 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
337 if (d
->chr_event
[mux_nr
])
338 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
341 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
343 if (d
->term_got_escape
) {
344 d
->term_got_escape
= 0;
345 if (ch
== term_escape_char
)
354 const char *term
= "QEMU: Terminated\n\r";
355 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
363 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
366 /* Switch to the next registered device */
367 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
369 if (d
->focus
>= d
->mux_cnt
)
371 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
374 d
->timestamps
= !d
->timestamps
;
375 d
->timestamps_start
= -1;
379 } else if (ch
== term_escape_char
) {
380 d
->term_got_escape
= 1;
388 static void mux_chr_accept_input(CharDriverState
*chr
)
390 MuxDriver
*d
= chr
->opaque
;
393 while (d
->prod
[m
] != d
->cons
[m
] &&
394 d
->chr_can_read
[m
] &&
395 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
396 d
->chr_read
[m
](d
->ext_opaque
[m
],
397 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
401 static int mux_chr_can_read(void *opaque
)
403 CharDriverState
*chr
= opaque
;
404 MuxDriver
*d
= chr
->opaque
;
407 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
409 if (d
->chr_can_read
[m
])
410 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
414 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
416 CharDriverState
*chr
= opaque
;
417 MuxDriver
*d
= chr
->opaque
;
421 mux_chr_accept_input (opaque
);
423 for(i
= 0; i
< size
; i
++)
424 if (mux_proc_byte(chr
, d
, buf
[i
])) {
425 if (d
->prod
[m
] == d
->cons
[m
] &&
426 d
->chr_can_read
[m
] &&
427 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
428 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
430 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
434 static void mux_chr_event(void *opaque
, int event
)
436 CharDriverState
*chr
= opaque
;
437 MuxDriver
*d
= chr
->opaque
;
440 /* Send the event to all registered listeners */
441 for (i
= 0; i
< d
->mux_cnt
; i
++)
442 mux_chr_send_event(d
, i
, event
);
445 static void mux_chr_update_read_handler(CharDriverState
*chr
)
447 MuxDriver
*d
= chr
->opaque
;
449 if (d
->mux_cnt
>= MAX_MUX
) {
450 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
453 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
454 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
455 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
456 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
457 /* Fix up the real driver with mux routines */
458 if (d
->mux_cnt
== 0) {
459 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
462 if (d
->focus
!= -1) {
463 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
465 d
->focus
= d
->mux_cnt
;
467 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
470 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
472 CharDriverState
*chr
;
475 chr
= g_malloc0(sizeof(CharDriverState
));
476 d
= g_malloc0(sizeof(MuxDriver
));
481 chr
->chr_write
= mux_chr_write
;
482 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
483 chr
->chr_accept_input
= mux_chr_accept_input
;
484 /* Frontend guest-open / -close notification is not support with muxes */
485 chr
->chr_guest_open
= NULL
;
486 chr
->chr_guest_close
= NULL
;
488 /* Muxes are always open on creation */
489 qemu_chr_generic_open(chr
);
496 int send_all(int fd
, const void *buf
, int len1
)
502 ret
= send(fd
, buf
, len
, 0);
504 errno
= WSAGetLastError();
505 if (errno
!= WSAEWOULDBLOCK
) {
508 } else if (ret
== 0) {
520 int send_all(int fd
, const void *_buf
, int len1
)
523 const uint8_t *buf
= _buf
;
527 ret
= write(fd
, buf
, len
);
529 if (errno
!= EINTR
&& errno
!= EAGAIN
)
531 } else if (ret
== 0) {
542 #define STDIO_MAX_CLIENTS 1
543 static int stdio_nb_clients
;
553 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
555 FDCharDriver
*s
= chr
->opaque
;
556 return send_all(s
->fd_out
, buf
, len
);
559 static int fd_chr_read_poll(void *opaque
)
561 CharDriverState
*chr
= opaque
;
562 FDCharDriver
*s
= chr
->opaque
;
564 s
->max_size
= qemu_chr_be_can_write(chr
);
568 static void fd_chr_read(void *opaque
)
570 CharDriverState
*chr
= opaque
;
571 FDCharDriver
*s
= chr
->opaque
;
573 uint8_t buf
[READ_BUF_LEN
];
576 if (len
> s
->max_size
)
580 size
= read(s
->fd_in
, buf
, len
);
582 /* FD has been closed. Remove it from the active list. */
583 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
584 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
588 qemu_chr_be_write(chr
, buf
, size
);
592 static void fd_chr_update_read_handler(CharDriverState
*chr
)
594 FDCharDriver
*s
= chr
->opaque
;
597 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
599 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
600 fd_chr_read
, NULL
, chr
);
605 static void fd_chr_close(struct CharDriverState
*chr
)
607 FDCharDriver
*s
= chr
->opaque
;
610 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
612 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
617 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
620 /* open a character device to a unix fd */
621 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
623 CharDriverState
*chr
;
626 chr
= g_malloc0(sizeof(CharDriverState
));
627 s
= g_malloc0(sizeof(FDCharDriver
));
631 chr
->chr_write
= fd_chr_write
;
632 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
633 chr
->chr_close
= fd_chr_close
;
635 qemu_chr_generic_open(chr
);
640 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
644 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
645 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
649 return qemu_chr_open_fd(-1, fd_out
);
652 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
655 char filename_in
[256], filename_out
[256];
656 const char *filename
= qemu_opt_get(opts
, "path");
658 if (filename
== NULL
) {
659 fprintf(stderr
, "chardev: pipe: no filename given\n");
663 snprintf(filename_in
, 256, "%s.in", filename
);
664 snprintf(filename_out
, 256, "%s.out", filename
);
665 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
666 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
667 if (fd_in
< 0 || fd_out
< 0) {
672 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
677 return qemu_chr_open_fd(fd_in
, fd_out
);
681 /* for STDIO, we handle the case where several clients use it
684 #define TERM_FIFO_MAX_SIZE 1
686 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
687 static int term_fifo_size
;
689 static int stdio_read_poll(void *opaque
)
691 CharDriverState
*chr
= opaque
;
693 /* try to flush the queue if needed */
694 if (term_fifo_size
!= 0 && qemu_chr_be_can_write(chr
) > 0) {
695 qemu_chr_be_write(chr
, term_fifo
, 1);
698 /* see if we can absorb more chars */
699 if (term_fifo_size
== 0)
705 static void stdio_read(void *opaque
)
709 CharDriverState
*chr
= opaque
;
711 size
= read(0, buf
, 1);
713 /* stdin has been closed. Remove it from the active list. */
714 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
715 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
719 if (qemu_chr_be_can_write(chr
) > 0) {
720 qemu_chr_be_write(chr
, buf
, 1);
721 } else if (term_fifo_size
== 0) {
722 term_fifo
[term_fifo_size
++] = buf
[0];
727 /* init terminal so that we can grab keys */
728 static struct termios oldtty
;
729 static int old_fd0_flags
;
730 static bool stdio_allow_signal
;
732 static void term_exit(void)
734 tcsetattr (0, TCSANOW
, &oldtty
);
735 fcntl(0, F_SETFL
, old_fd0_flags
);
738 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
744 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
745 |INLCR
|IGNCR
|ICRNL
|IXON
);
746 tty
.c_oflag
|= OPOST
;
747 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
748 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
753 /* if graphical mode, we allow Ctrl-C handling */
754 if (!stdio_allow_signal
)
755 tty
.c_lflag
&= ~ISIG
;
757 tcsetattr (0, TCSANOW
, &tty
);
760 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
764 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
768 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
770 CharDriverState
*chr
;
772 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
) {
775 if (stdio_nb_clients
== 0) {
776 old_fd0_flags
= fcntl(0, F_GETFL
);
777 tcgetattr (0, &oldtty
);
778 fcntl(0, F_SETFL
, O_NONBLOCK
);
782 chr
= qemu_chr_open_fd(0, 1);
783 chr
->chr_close
= qemu_chr_close_stdio
;
784 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
785 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
787 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
788 display_type
!= DT_NOGRAPHIC
);
789 qemu_chr_fe_set_echo(chr
, false);
795 /* Once Solaris has openpty(), this is going to be removed. */
796 static int openpty(int *amaster
, int *aslave
, char *name
,
797 struct termios
*termp
, struct winsize
*winp
)
800 int mfd
= -1, sfd
= -1;
802 *amaster
= *aslave
= -1;
804 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
808 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
811 if ((slave
= ptsname(mfd
)) == NULL
)
814 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
817 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
818 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
826 ioctl(sfd
, TIOCSWINSZ
, winp
);
837 static void cfmakeraw (struct termios
*termios_p
)
839 termios_p
->c_iflag
&=
840 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
841 termios_p
->c_oflag
&= ~OPOST
;
842 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
843 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
844 termios_p
->c_cflag
|= CS8
;
846 termios_p
->c_cc
[VMIN
] = 0;
847 termios_p
->c_cc
[VTIME
] = 0;
851 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
852 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
853 || defined(__GLIBC__)
863 static void pty_chr_update_read_handler(CharDriverState
*chr
);
864 static void pty_chr_state(CharDriverState
*chr
, int connected
);
866 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
868 PtyCharDriver
*s
= chr
->opaque
;
871 /* guest sends data, check for (re-)connect */
872 pty_chr_update_read_handler(chr
);
875 return send_all(s
->fd
, buf
, len
);
878 static int pty_chr_read_poll(void *opaque
)
880 CharDriverState
*chr
= opaque
;
881 PtyCharDriver
*s
= chr
->opaque
;
883 s
->read_bytes
= qemu_chr_be_can_write(chr
);
884 return s
->read_bytes
;
887 static void pty_chr_read(void *opaque
)
889 CharDriverState
*chr
= opaque
;
890 PtyCharDriver
*s
= chr
->opaque
;
892 uint8_t buf
[READ_BUF_LEN
];
895 if (len
> s
->read_bytes
)
899 size
= read(s
->fd
, buf
, len
);
900 if ((size
== -1 && errno
== EIO
) ||
902 pty_chr_state(chr
, 0);
906 pty_chr_state(chr
, 1);
907 qemu_chr_be_write(chr
, buf
, size
);
911 static void pty_chr_update_read_handler(CharDriverState
*chr
)
913 PtyCharDriver
*s
= chr
->opaque
;
915 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
916 pty_chr_read
, NULL
, chr
);
919 * Short timeout here: just need wait long enougth that qemu makes
920 * it through the poll loop once. When reconnected we want a
921 * short timeout so we notice it almost instantly. Otherwise
922 * read() gives us -EIO instantly, making pty_chr_state() reset the
923 * timeout to the normal (much longer) poll interval before the
926 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
929 static void pty_chr_state(CharDriverState
*chr
, int connected
)
931 PtyCharDriver
*s
= chr
->opaque
;
934 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
937 /* (re-)connect poll interval for idle guests: once per second.
938 * We check more frequently in case the guests sends data to
939 * the virtual device linked to our pty. */
940 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
943 qemu_chr_generic_open(chr
);
948 static void pty_chr_timer(void *opaque
)
950 struct CharDriverState
*chr
= opaque
;
951 PtyCharDriver
*s
= chr
->opaque
;
956 /* If we arrive here without polling being cleared due
957 * read returning -EIO, then we are (re-)connected */
958 pty_chr_state(chr
, 1);
963 pty_chr_update_read_handler(chr
);
966 static void pty_chr_close(struct CharDriverState
*chr
)
968 PtyCharDriver
*s
= chr
->opaque
;
970 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
972 qemu_del_timer(s
->timer
);
973 qemu_free_timer(s
->timer
);
975 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
978 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
980 CharDriverState
*chr
;
983 int master_fd
, slave_fd
, len
;
984 #if defined(__OpenBSD__) || defined(__DragonFly__)
985 char pty_name
[PATH_MAX
];
986 #define q_ptsname(x) pty_name
988 char *pty_name
= NULL
;
989 #define q_ptsname(x) ptsname(x)
992 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
996 /* Set raw attributes on the pty. */
997 tcgetattr(slave_fd
, &tty
);
999 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1002 chr
= g_malloc0(sizeof(CharDriverState
));
1004 len
= strlen(q_ptsname(master_fd
)) + 5;
1005 chr
->filename
= g_malloc(len
);
1006 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
1007 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
1008 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(master_fd
));
1010 s
= g_malloc0(sizeof(PtyCharDriver
));
1012 chr
->chr_write
= pty_chr_write
;
1013 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1014 chr
->chr_close
= pty_chr_close
;
1017 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
1022 static void tty_serial_init(int fd
, int speed
,
1023 int parity
, int data_bits
, int stop_bits
)
1029 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1030 speed
, parity
, data_bits
, stop_bits
);
1032 tcgetattr (fd
, &tty
);
1034 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1035 speed
= speed
* 10 / 11;
1052 /* Non-Posix values follow. They may be unsupported on some systems. */
1054 check_speed(115200);
1056 check_speed(230400);
1059 check_speed(460800);
1062 check_speed(500000);
1065 check_speed(576000);
1068 check_speed(921600);
1071 check_speed(1000000);
1074 check_speed(1152000);
1077 check_speed(1500000);
1080 check_speed(2000000);
1083 check_speed(2500000);
1086 check_speed(3000000);
1089 check_speed(3500000);
1092 check_speed(4000000);
1097 cfsetispeed(&tty
, spd
);
1098 cfsetospeed(&tty
, spd
);
1100 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1101 |INLCR
|IGNCR
|ICRNL
|IXON
);
1102 tty
.c_oflag
|= OPOST
;
1103 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1104 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1125 tty
.c_cflag
|= PARENB
;
1128 tty
.c_cflag
|= PARENB
| PARODD
;
1132 tty
.c_cflag
|= CSTOPB
;
1134 tcsetattr (fd
, TCSANOW
, &tty
);
1137 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1139 FDCharDriver
*s
= chr
->opaque
;
1142 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1144 QEMUSerialSetParams
*ssp
= arg
;
1145 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1146 ssp
->data_bits
, ssp
->stop_bits
);
1149 case CHR_IOCTL_SERIAL_SET_BREAK
:
1151 int enable
= *(int *)arg
;
1153 tcsendbreak(s
->fd_in
, 1);
1156 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1159 int *targ
= (int *)arg
;
1160 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1162 if (sarg
& TIOCM_CTS
)
1163 *targ
|= CHR_TIOCM_CTS
;
1164 if (sarg
& TIOCM_CAR
)
1165 *targ
|= CHR_TIOCM_CAR
;
1166 if (sarg
& TIOCM_DSR
)
1167 *targ
|= CHR_TIOCM_DSR
;
1168 if (sarg
& TIOCM_RI
)
1169 *targ
|= CHR_TIOCM_RI
;
1170 if (sarg
& TIOCM_DTR
)
1171 *targ
|= CHR_TIOCM_DTR
;
1172 if (sarg
& TIOCM_RTS
)
1173 *targ
|= CHR_TIOCM_RTS
;
1176 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1178 int sarg
= *(int *)arg
;
1180 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1181 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1182 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1183 if (sarg
& CHR_TIOCM_CTS
)
1185 if (sarg
& CHR_TIOCM_CAR
)
1187 if (sarg
& CHR_TIOCM_DSR
)
1189 if (sarg
& CHR_TIOCM_RI
)
1191 if (sarg
& CHR_TIOCM_DTR
)
1193 if (sarg
& CHR_TIOCM_RTS
)
1195 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1204 static void qemu_chr_close_tty(CharDriverState
*chr
)
1206 FDCharDriver
*s
= chr
->opaque
;
1220 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1222 const char *filename
= qemu_opt_get(opts
, "path");
1223 CharDriverState
*chr
;
1226 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1230 tty_serial_init(fd
, 115200, 'N', 8, 1);
1231 chr
= qemu_chr_open_fd(fd
, fd
);
1232 chr
->chr_ioctl
= tty_serial_ioctl
;
1233 chr
->chr_close
= qemu_chr_close_tty
;
1236 #else /* ! __linux__ && ! __sun__ */
1237 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1241 #endif /* __linux__ || __sun__ */
1243 #if defined(__linux__)
1247 } ParallelCharDriver
;
1249 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1251 if (s
->mode
!= mode
) {
1253 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1260 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1262 ParallelCharDriver
*drv
= chr
->opaque
;
1267 case CHR_IOCTL_PP_READ_DATA
:
1268 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1270 *(uint8_t *)arg
= b
;
1272 case CHR_IOCTL_PP_WRITE_DATA
:
1273 b
= *(uint8_t *)arg
;
1274 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1277 case CHR_IOCTL_PP_READ_CONTROL
:
1278 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1280 /* Linux gives only the lowest bits, and no way to know data
1281 direction! For better compatibility set the fixed upper
1283 *(uint8_t *)arg
= b
| 0xc0;
1285 case CHR_IOCTL_PP_WRITE_CONTROL
:
1286 b
= *(uint8_t *)arg
;
1287 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1290 case CHR_IOCTL_PP_READ_STATUS
:
1291 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1293 *(uint8_t *)arg
= b
;
1295 case CHR_IOCTL_PP_DATA_DIR
:
1296 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1299 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1300 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1301 struct ParallelIOArg
*parg
= arg
;
1302 int n
= read(fd
, parg
->buffer
, parg
->count
);
1303 if (n
!= parg
->count
) {
1308 case CHR_IOCTL_PP_EPP_READ
:
1309 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1310 struct ParallelIOArg
*parg
= arg
;
1311 int n
= read(fd
, parg
->buffer
, parg
->count
);
1312 if (n
!= parg
->count
) {
1317 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1318 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1319 struct ParallelIOArg
*parg
= arg
;
1320 int n
= write(fd
, parg
->buffer
, parg
->count
);
1321 if (n
!= parg
->count
) {
1326 case CHR_IOCTL_PP_EPP_WRITE
:
1327 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1328 struct ParallelIOArg
*parg
= arg
;
1329 int n
= write(fd
, parg
->buffer
, parg
->count
);
1330 if (n
!= parg
->count
) {
1341 static void pp_close(CharDriverState
*chr
)
1343 ParallelCharDriver
*drv
= chr
->opaque
;
1346 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1347 ioctl(fd
, PPRELEASE
);
1350 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1353 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1355 const char *filename
= qemu_opt_get(opts
, "path");
1356 CharDriverState
*chr
;
1357 ParallelCharDriver
*drv
;
1360 TFR(fd
= qemu_open(filename
, O_RDWR
));
1365 if (ioctl(fd
, PPCLAIM
) < 0) {
1370 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1372 drv
->mode
= IEEE1284_MODE_COMPAT
;
1374 chr
= g_malloc0(sizeof(CharDriverState
));
1375 chr
->chr_write
= null_chr_write
;
1376 chr
->chr_ioctl
= pp_ioctl
;
1377 chr
->chr_close
= pp_close
;
1380 qemu_chr_generic_open(chr
);
1384 #endif /* __linux__ */
1386 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1387 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1389 int fd
= (int)(intptr_t)chr
->opaque
;
1393 case CHR_IOCTL_PP_READ_DATA
:
1394 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1396 *(uint8_t *)arg
= b
;
1398 case CHR_IOCTL_PP_WRITE_DATA
:
1399 b
= *(uint8_t *)arg
;
1400 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1403 case CHR_IOCTL_PP_READ_CONTROL
:
1404 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1406 *(uint8_t *)arg
= b
;
1408 case CHR_IOCTL_PP_WRITE_CONTROL
:
1409 b
= *(uint8_t *)arg
;
1410 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1413 case CHR_IOCTL_PP_READ_STATUS
:
1414 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1416 *(uint8_t *)arg
= b
;
1424 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1426 const char *filename
= qemu_opt_get(opts
, "path");
1427 CharDriverState
*chr
;
1430 fd
= qemu_open(filename
, O_RDWR
);
1435 chr
= g_malloc0(sizeof(CharDriverState
));
1436 chr
->opaque
= (void *)(intptr_t)fd
;
1437 chr
->chr_write
= null_chr_write
;
1438 chr
->chr_ioctl
= pp_ioctl
;
1445 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1449 HANDLE hcom
, hrecv
, hsend
;
1450 OVERLAPPED orecv
, osend
;
1457 HANDLE hInputReadyEvent
;
1458 HANDLE hInputDoneEvent
;
1459 HANDLE hInputThread
;
1460 uint8_t win_stdio_buf
;
1461 } WinStdioCharState
;
1463 #define NSENDBUF 2048
1464 #define NRECVBUF 2048
1465 #define MAXCONNECT 1
1466 #define NTIMEOUT 5000
1468 static int win_chr_poll(void *opaque
);
1469 static int win_chr_pipe_poll(void *opaque
);
1471 static void win_chr_close(CharDriverState
*chr
)
1473 WinCharState
*s
= chr
->opaque
;
1476 CloseHandle(s
->hsend
);
1480 CloseHandle(s
->hrecv
);
1484 CloseHandle(s
->hcom
);
1488 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1490 qemu_del_polling_cb(win_chr_poll
, chr
);
1492 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1495 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1497 WinCharState
*s
= chr
->opaque
;
1499 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1504 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1506 fprintf(stderr
, "Failed CreateEvent\n");
1509 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1511 fprintf(stderr
, "Failed CreateEvent\n");
1515 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1516 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1517 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1518 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1523 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1524 fprintf(stderr
, "Failed SetupComm\n");
1528 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1529 size
= sizeof(COMMCONFIG
);
1530 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1531 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1532 CommConfigDialog(filename
, NULL
, &comcfg
);
1534 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1535 fprintf(stderr
, "Failed SetCommState\n");
1539 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1540 fprintf(stderr
, "Failed SetCommMask\n");
1544 cto
.ReadIntervalTimeout
= MAXDWORD
;
1545 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1546 fprintf(stderr
, "Failed SetCommTimeouts\n");
1550 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1551 fprintf(stderr
, "Failed ClearCommError\n");
1554 qemu_add_polling_cb(win_chr_poll
, chr
);
1562 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1564 WinCharState
*s
= chr
->opaque
;
1565 DWORD len
, ret
, size
, err
;
1568 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1569 s
->osend
.hEvent
= s
->hsend
;
1572 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1574 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1576 err
= GetLastError();
1577 if (err
== ERROR_IO_PENDING
) {
1578 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1596 static int win_chr_read_poll(CharDriverState
*chr
)
1598 WinCharState
*s
= chr
->opaque
;
1600 s
->max_size
= qemu_chr_be_can_write(chr
);
1604 static void win_chr_readfile(CharDriverState
*chr
)
1606 WinCharState
*s
= chr
->opaque
;
1608 uint8_t buf
[READ_BUF_LEN
];
1611 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1612 s
->orecv
.hEvent
= s
->hrecv
;
1613 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1615 err
= GetLastError();
1616 if (err
== ERROR_IO_PENDING
) {
1617 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1622 qemu_chr_be_write(chr
, buf
, size
);
1626 static void win_chr_read(CharDriverState
*chr
)
1628 WinCharState
*s
= chr
->opaque
;
1630 if (s
->len
> s
->max_size
)
1631 s
->len
= s
->max_size
;
1635 win_chr_readfile(chr
);
1638 static int win_chr_poll(void *opaque
)
1640 CharDriverState
*chr
= opaque
;
1641 WinCharState
*s
= chr
->opaque
;
1645 ClearCommError(s
->hcom
, &comerr
, &status
);
1646 if (status
.cbInQue
> 0) {
1647 s
->len
= status
.cbInQue
;
1648 win_chr_read_poll(chr
);
1655 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1657 const char *filename
= qemu_opt_get(opts
, "path");
1658 CharDriverState
*chr
;
1661 chr
= g_malloc0(sizeof(CharDriverState
));
1662 s
= g_malloc0(sizeof(WinCharState
));
1664 chr
->chr_write
= win_chr_write
;
1665 chr
->chr_close
= win_chr_close
;
1667 if (win_chr_init(chr
, filename
) < 0) {
1672 qemu_chr_generic_open(chr
);
1676 static int win_chr_pipe_poll(void *opaque
)
1678 CharDriverState
*chr
= opaque
;
1679 WinCharState
*s
= chr
->opaque
;
1682 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1685 win_chr_read_poll(chr
);
1692 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1694 WinCharState
*s
= chr
->opaque
;
1702 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1704 fprintf(stderr
, "Failed CreateEvent\n");
1707 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1709 fprintf(stderr
, "Failed CreateEvent\n");
1713 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1714 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1715 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1717 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1718 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1719 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1724 ZeroMemory(&ov
, sizeof(ov
));
1725 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1726 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1728 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1732 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1734 fprintf(stderr
, "Failed GetOverlappedResult\n");
1736 CloseHandle(ov
.hEvent
);
1743 CloseHandle(ov
.hEvent
);
1746 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1755 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1757 const char *filename
= qemu_opt_get(opts
, "path");
1758 CharDriverState
*chr
;
1761 chr
= g_malloc0(sizeof(CharDriverState
));
1762 s
= g_malloc0(sizeof(WinCharState
));
1764 chr
->chr_write
= win_chr_write
;
1765 chr
->chr_close
= win_chr_close
;
1767 if (win_chr_pipe_init(chr
, filename
) < 0) {
1772 qemu_chr_generic_open(chr
);
1776 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1778 CharDriverState
*chr
;
1781 chr
= g_malloc0(sizeof(CharDriverState
));
1782 s
= g_malloc0(sizeof(WinCharState
));
1785 chr
->chr_write
= win_chr_write
;
1786 qemu_chr_generic_open(chr
);
1790 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1792 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1795 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1797 const char *file_out
= qemu_opt_get(opts
, "path");
1800 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1801 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1802 if (fd_out
== INVALID_HANDLE_VALUE
) {
1806 return qemu_chr_open_win_file(fd_out
);
1809 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1811 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1818 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1828 static void win_stdio_wait_func(void *opaque
)
1830 CharDriverState
*chr
= opaque
;
1831 WinStdioCharState
*stdio
= chr
->opaque
;
1832 INPUT_RECORD buf
[4];
1837 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1841 /* Avoid error storm */
1842 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1846 for (i
= 0; i
< dwSize
; i
++) {
1847 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1849 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1851 if (kev
->uChar
.AsciiChar
!= 0) {
1852 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1853 if (qemu_chr_be_can_write(chr
)) {
1854 uint8_t c
= kev
->uChar
.AsciiChar
;
1855 qemu_chr_be_write(chr
, &c
, 1);
1863 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
1865 CharDriverState
*chr
= param
;
1866 WinStdioCharState
*stdio
= chr
->opaque
;
1872 /* Wait for one byte */
1873 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
1875 /* Exit in case of error, continue if nothing read */
1883 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1884 if (stdio
->win_stdio_buf
== '\r') {
1888 /* Signal the main thread and wait until the byte was eaten */
1889 if (!SetEvent(stdio
->hInputReadyEvent
)) {
1892 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
1898 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
1902 static void win_stdio_thread_wait_func(void *opaque
)
1904 CharDriverState
*chr
= opaque
;
1905 WinStdioCharState
*stdio
= chr
->opaque
;
1907 if (qemu_chr_be_can_write(chr
)) {
1908 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
1911 SetEvent(stdio
->hInputDoneEvent
);
1914 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
1916 WinStdioCharState
*stdio
= chr
->opaque
;
1919 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
1922 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
1924 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
1928 static void win_stdio_close(CharDriverState
*chr
)
1930 WinStdioCharState
*stdio
= chr
->opaque
;
1932 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
1933 CloseHandle(stdio
->hInputReadyEvent
);
1935 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
1936 CloseHandle(stdio
->hInputDoneEvent
);
1938 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
1939 TerminateThread(stdio
->hInputThread
, 0);
1942 g_free(chr
->opaque
);
1947 static CharDriverState
*qemu_chr_open_win_stdio(QemuOpts
*opts
)
1949 CharDriverState
*chr
;
1950 WinStdioCharState
*stdio
;
1954 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
1955 || ((display_type
!= DT_NOGRAPHIC
) && (stdio_nb_clients
!= 0))) {
1959 chr
= g_malloc0(sizeof(CharDriverState
));
1960 stdio
= g_malloc0(sizeof(WinStdioCharState
));
1962 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
1963 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
1964 fprintf(stderr
, "cannot open stdio: invalid handle\n");
1968 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
1970 chr
->opaque
= stdio
;
1971 chr
->chr_write
= win_stdio_write
;
1972 chr
->chr_close
= win_stdio_close
;
1974 if (stdio_nb_clients
== 0) {
1976 if (qemu_add_wait_object(stdio
->hStdIn
,
1977 win_stdio_wait_func
, chr
)) {
1978 fprintf(stderr
, "qemu_add_wait_object: failed\n");
1983 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1984 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1985 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
1988 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
1989 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
1990 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
1991 fprintf(stderr
, "cannot create stdio thread or event\n");
1994 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
1995 win_stdio_thread_wait_func
, chr
)) {
1996 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2001 dwMode
|= ENABLE_LINE_INPUT
;
2003 stdio_clients
[stdio_nb_clients
++] = chr
;
2004 if (stdio_nb_clients
== 1 && is_console
) {
2005 /* set the terminal in raw mode */
2006 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2007 dwMode
|= ENABLE_PROCESSED_INPUT
;
2010 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2012 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2013 qemu_chr_fe_set_echo(chr
, false);
2017 #endif /* !_WIN32 */
2019 /***********************************************************/
2020 /* UDP Net console */
2024 uint8_t buf
[READ_BUF_LEN
];
2030 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2032 NetCharDriver
*s
= chr
->opaque
;
2034 return send(s
->fd
, (const void *)buf
, len
, 0);
2037 static int udp_chr_read_poll(void *opaque
)
2039 CharDriverState
*chr
= opaque
;
2040 NetCharDriver
*s
= chr
->opaque
;
2042 s
->max_size
= qemu_chr_be_can_write(chr
);
2044 /* If there were any stray characters in the queue process them
2047 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2048 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2050 s
->max_size
= qemu_chr_be_can_write(chr
);
2055 static void udp_chr_read(void *opaque
)
2057 CharDriverState
*chr
= opaque
;
2058 NetCharDriver
*s
= chr
->opaque
;
2060 if (s
->max_size
== 0)
2062 s
->bufcnt
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2063 s
->bufptr
= s
->bufcnt
;
2068 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2069 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2071 s
->max_size
= qemu_chr_be_can_write(chr
);
2075 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2077 NetCharDriver
*s
= chr
->opaque
;
2080 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2081 udp_chr_read
, NULL
, chr
);
2085 static void udp_chr_close(CharDriverState
*chr
)
2087 NetCharDriver
*s
= chr
->opaque
;
2089 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2093 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2096 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2098 CharDriverState
*chr
= NULL
;
2099 NetCharDriver
*s
= NULL
;
2102 chr
= g_malloc0(sizeof(CharDriverState
));
2103 s
= g_malloc0(sizeof(NetCharDriver
));
2105 fd
= inet_dgram_opts(opts
);
2107 fprintf(stderr
, "inet_dgram_opts failed\n");
2115 chr
->chr_write
= udp_chr_write
;
2116 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2117 chr
->chr_close
= udp_chr_close
;
2129 /***********************************************************/
2130 /* TCP Net console */
2142 static void tcp_chr_accept(void *opaque
);
2144 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2146 TCPCharDriver
*s
= chr
->opaque
;
2148 return send_all(s
->fd
, buf
, len
);
2150 /* XXX: indicate an error ? */
2155 static int tcp_chr_read_poll(void *opaque
)
2157 CharDriverState
*chr
= opaque
;
2158 TCPCharDriver
*s
= chr
->opaque
;
2161 s
->max_size
= qemu_chr_be_can_write(chr
);
2166 #define IAC_BREAK 243
2167 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2169 uint8_t *buf
, int *size
)
2171 /* Handle any telnet client's basic IAC options to satisfy char by
2172 * char mode with no echo. All IAC options will be removed from
2173 * the buf and the do_telnetopt variable will be used to track the
2174 * state of the width of the IAC information.
2176 * IAC commands come in sets of 3 bytes with the exception of the
2177 * "IAC BREAK" command and the double IAC.
2183 for (i
= 0; i
< *size
; i
++) {
2184 if (s
->do_telnetopt
> 1) {
2185 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2186 /* Double IAC means send an IAC */
2190 s
->do_telnetopt
= 1;
2192 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2193 /* Handle IAC break commands by sending a serial break */
2194 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2199 if (s
->do_telnetopt
>= 4) {
2200 s
->do_telnetopt
= 1;
2203 if ((unsigned char)buf
[i
] == IAC
) {
2204 s
->do_telnetopt
= 2;
2215 static int tcp_get_msgfd(CharDriverState
*chr
)
2217 TCPCharDriver
*s
= chr
->opaque
;
2224 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2226 TCPCharDriver
*s
= chr
->opaque
;
2227 struct cmsghdr
*cmsg
;
2229 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2232 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2233 cmsg
->cmsg_level
!= SOL_SOCKET
||
2234 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2237 fd
= *((int *)CMSG_DATA(cmsg
));
2241 #ifndef MSG_CMSG_CLOEXEC
2242 qemu_set_cloexec(fd
);
2250 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2252 TCPCharDriver
*s
= chr
->opaque
;
2253 struct msghdr msg
= { NULL
, };
2254 struct iovec iov
[1];
2256 struct cmsghdr cmsg
;
2257 char control
[CMSG_SPACE(sizeof(int))];
2262 iov
[0].iov_base
= buf
;
2263 iov
[0].iov_len
= len
;
2267 msg
.msg_control
= &msg_control
;
2268 msg
.msg_controllen
= sizeof(msg_control
);
2270 #ifdef MSG_CMSG_CLOEXEC
2271 flags
|= MSG_CMSG_CLOEXEC
;
2273 ret
= recvmsg(s
->fd
, &msg
, flags
);
2274 if (ret
> 0 && s
->is_unix
) {
2275 unix_process_msgfd(chr
, &msg
);
2281 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2283 TCPCharDriver
*s
= chr
->opaque
;
2284 return qemu_recv(s
->fd
, buf
, len
, 0);
2288 static void tcp_chr_read(void *opaque
)
2290 CharDriverState
*chr
= opaque
;
2291 TCPCharDriver
*s
= chr
->opaque
;
2292 uint8_t buf
[READ_BUF_LEN
];
2295 if (!s
->connected
|| s
->max_size
<= 0)
2298 if (len
> s
->max_size
)
2300 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2302 /* connection closed */
2304 if (s
->listen_fd
>= 0) {
2305 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2307 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2310 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2311 } else if (size
> 0) {
2312 if (s
->do_telnetopt
)
2313 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2315 qemu_chr_be_write(chr
, buf
, size
);
2320 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2322 return qemu_chr_open_fd(eventfd
, eventfd
);
2326 static void tcp_chr_connect(void *opaque
)
2328 CharDriverState
*chr
= opaque
;
2329 TCPCharDriver
*s
= chr
->opaque
;
2333 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2334 tcp_chr_read
, NULL
, chr
);
2336 qemu_chr_generic_open(chr
);
2339 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2340 static void tcp_chr_telnet_init(int fd
)
2343 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2344 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2345 send(fd
, (char *)buf
, 3, 0);
2346 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2347 send(fd
, (char *)buf
, 3, 0);
2348 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2349 send(fd
, (char *)buf
, 3, 0);
2350 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2351 send(fd
, (char *)buf
, 3, 0);
2354 static void socket_set_nodelay(int fd
)
2357 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2360 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2362 TCPCharDriver
*s
= chr
->opaque
;
2366 socket_set_nonblock(fd
);
2368 socket_set_nodelay(fd
);
2370 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2371 tcp_chr_connect(chr
);
2376 static void tcp_chr_accept(void *opaque
)
2378 CharDriverState
*chr
= opaque
;
2379 TCPCharDriver
*s
= chr
->opaque
;
2380 struct sockaddr_in saddr
;
2382 struct sockaddr_un uaddr
;
2384 struct sockaddr
*addr
;
2391 len
= sizeof(uaddr
);
2392 addr
= (struct sockaddr
*)&uaddr
;
2396 len
= sizeof(saddr
);
2397 addr
= (struct sockaddr
*)&saddr
;
2399 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2400 if (fd
< 0 && errno
!= EINTR
) {
2402 } else if (fd
>= 0) {
2403 if (s
->do_telnetopt
)
2404 tcp_chr_telnet_init(fd
);
2408 if (tcp_chr_add_client(chr
, fd
) < 0)
2412 static void tcp_chr_close(CharDriverState
*chr
)
2414 TCPCharDriver
*s
= chr
->opaque
;
2416 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2419 if (s
->listen_fd
>= 0) {
2420 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2421 closesocket(s
->listen_fd
);
2424 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2427 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2429 CharDriverState
*chr
= NULL
;
2430 TCPCharDriver
*s
= NULL
;
2438 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2439 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2440 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2441 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2442 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2446 chr
= g_malloc0(sizeof(CharDriverState
));
2447 s
= g_malloc0(sizeof(TCPCharDriver
));
2451 fd
= unix_listen_opts(opts
);
2453 fd
= unix_connect_opts(opts
);
2457 fd
= inet_listen_opts(opts
, 0, NULL
);
2459 fd
= inet_connect_opts(opts
, NULL
, NULL
);
2466 if (!is_waitconnect
)
2467 socket_set_nonblock(fd
);
2473 s
->is_unix
= is_unix
;
2474 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2477 chr
->chr_write
= tcp_chr_write
;
2478 chr
->chr_close
= tcp_chr_close
;
2479 chr
->get_msgfd
= tcp_get_msgfd
;
2480 chr
->chr_add_client
= tcp_chr_add_client
;
2484 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2486 s
->do_telnetopt
= 1;
2491 socket_set_nodelay(fd
);
2492 tcp_chr_connect(chr
);
2495 /* for "info chardev" monitor command */
2496 chr
->filename
= g_malloc(256);
2498 snprintf(chr
->filename
, 256, "unix:%s%s",
2499 qemu_opt_get(opts
, "path"),
2500 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2501 } else if (is_telnet
) {
2502 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2503 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2504 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2506 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2507 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2508 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2511 if (is_listen
&& is_waitconnect
) {
2512 printf("QEMU waiting for connection on: %s\n",
2514 tcp_chr_accept(chr
);
2515 socket_set_nonblock(s
->listen_fd
);
2527 /***********************************************************/
2528 /* Memory chardev */
2531 size_t outbuf_capacity
;
2535 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2537 MemoryDriver
*d
= chr
->opaque
;
2539 /* TODO: the QString implementation has the same code, we should
2540 * introduce a generic way to do this in cutils.c */
2541 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2543 d
->outbuf_capacity
+= len
;
2544 d
->outbuf_capacity
*= 2;
2545 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2548 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2549 d
->outbuf_size
+= len
;
2554 void qemu_chr_init_mem(CharDriverState
*chr
)
2558 d
= g_malloc(sizeof(*d
));
2560 d
->outbuf_capacity
= 4096;
2561 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2563 memset(chr
, 0, sizeof(*chr
));
2565 chr
->chr_write
= mem_chr_write
;
2568 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2570 MemoryDriver
*d
= chr
->opaque
;
2571 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2574 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2575 void qemu_chr_close_mem(CharDriverState
*chr
)
2577 MemoryDriver
*d
= chr
->opaque
;
2580 g_free(chr
->opaque
);
2582 chr
->chr_write
= NULL
;
2585 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2587 const MemoryDriver
*d
= chr
->opaque
;
2588 return d
->outbuf_size
;
2591 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2593 char host
[65], port
[33], width
[8], height
[8];
2597 Error
*local_err
= NULL
;
2599 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2600 if (error_is_set(&local_err
)) {
2601 qerror_report_err(local_err
);
2602 error_free(local_err
);
2606 if (strstart(filename
, "mon:", &p
)) {
2608 qemu_opt_set(opts
, "mux", "on");
2611 if (strcmp(filename
, "null") == 0 ||
2612 strcmp(filename
, "pty") == 0 ||
2613 strcmp(filename
, "msmouse") == 0 ||
2614 strcmp(filename
, "braille") == 0 ||
2615 strcmp(filename
, "stdio") == 0) {
2616 qemu_opt_set(opts
, "backend", filename
);
2619 if (strstart(filename
, "vc", &p
)) {
2620 qemu_opt_set(opts
, "backend", "vc");
2622 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2624 qemu_opt_set(opts
, "width", width
);
2625 qemu_opt_set(opts
, "height", height
);
2626 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2628 qemu_opt_set(opts
, "cols", width
);
2629 qemu_opt_set(opts
, "rows", height
);
2636 if (strcmp(filename
, "con:") == 0) {
2637 qemu_opt_set(opts
, "backend", "console");
2640 if (strstart(filename
, "COM", NULL
)) {
2641 qemu_opt_set(opts
, "backend", "serial");
2642 qemu_opt_set(opts
, "path", filename
);
2645 if (strstart(filename
, "file:", &p
)) {
2646 qemu_opt_set(opts
, "backend", "file");
2647 qemu_opt_set(opts
, "path", p
);
2650 if (strstart(filename
, "pipe:", &p
)) {
2651 qemu_opt_set(opts
, "backend", "pipe");
2652 qemu_opt_set(opts
, "path", p
);
2655 if (strstart(filename
, "tcp:", &p
) ||
2656 strstart(filename
, "telnet:", &p
)) {
2657 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2659 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2662 qemu_opt_set(opts
, "backend", "socket");
2663 qemu_opt_set(opts
, "host", host
);
2664 qemu_opt_set(opts
, "port", port
);
2665 if (p
[pos
] == ',') {
2666 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2669 if (strstart(filename
, "telnet:", &p
))
2670 qemu_opt_set(opts
, "telnet", "on");
2673 if (strstart(filename
, "udp:", &p
)) {
2674 qemu_opt_set(opts
, "backend", "udp");
2675 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2677 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2681 qemu_opt_set(opts
, "host", host
);
2682 qemu_opt_set(opts
, "port", port
);
2683 if (p
[pos
] == '@') {
2685 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2687 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2691 qemu_opt_set(opts
, "localaddr", host
);
2692 qemu_opt_set(opts
, "localport", port
);
2696 if (strstart(filename
, "unix:", &p
)) {
2697 qemu_opt_set(opts
, "backend", "socket");
2698 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2702 if (strstart(filename
, "/dev/parport", NULL
) ||
2703 strstart(filename
, "/dev/ppi", NULL
)) {
2704 qemu_opt_set(opts
, "backend", "parport");
2705 qemu_opt_set(opts
, "path", filename
);
2708 if (strstart(filename
, "/dev/", NULL
)) {
2709 qemu_opt_set(opts
, "backend", "tty");
2710 qemu_opt_set(opts
, "path", filename
);
2715 qemu_opts_del(opts
);
2719 static const struct {
2721 CharDriverState
*(*open
)(QemuOpts
*opts
);
2722 } backend_table
[] = {
2723 { .name
= "null", .open
= qemu_chr_open_null
},
2724 { .name
= "socket", .open
= qemu_chr_open_socket
},
2725 { .name
= "udp", .open
= qemu_chr_open_udp
},
2726 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2727 { .name
= "vc", .open
= text_console_init
},
2729 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2730 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2731 { .name
= "console", .open
= qemu_chr_open_win_con
},
2732 { .name
= "serial", .open
= qemu_chr_open_win
},
2733 { .name
= "stdio", .open
= qemu_chr_open_win_stdio
},
2735 { .name
= "file", .open
= qemu_chr_open_file_out
},
2736 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2737 { .name
= "pty", .open
= qemu_chr_open_pty
},
2738 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2740 #ifdef CONFIG_BRLAPI
2741 { .name
= "braille", .open
= chr_baum_init
},
2743 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2744 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2745 || defined(__FreeBSD_kernel__)
2746 { .name
= "tty", .open
= qemu_chr_open_tty
},
2748 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2749 || defined(__FreeBSD_kernel__)
2750 { .name
= "parport", .open
= qemu_chr_open_pp
},
2753 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
2757 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
2758 void (*init
)(struct CharDriverState
*s
))
2760 CharDriverState
*chr
;
2763 if (qemu_opts_id(opts
) == NULL
) {
2764 fprintf(stderr
, "chardev: no id specified\n");
2768 if (qemu_opt_get(opts
, "backend") == NULL
) {
2769 fprintf(stderr
, "chardev: \"%s\" missing backend\n",
2770 qemu_opts_id(opts
));
2773 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2774 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2777 if (i
== ARRAY_SIZE(backend_table
)) {
2778 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2779 qemu_opt_get(opts
, "backend"));
2783 chr
= backend_table
[i
].open(opts
);
2785 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2786 qemu_opt_get(opts
, "backend"));
2791 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
2793 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2795 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2796 CharDriverState
*base
= chr
;
2797 int len
= strlen(qemu_opts_id(opts
)) + 6;
2798 base
->label
= g_malloc(len
);
2799 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2800 chr
= qemu_chr_open_mux(base
);
2801 chr
->filename
= base
->filename
;
2802 chr
->avail_connections
= MAX_MUX
;
2803 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2805 chr
->avail_connections
= 1;
2807 chr
->label
= g_strdup(qemu_opts_id(opts
));
2811 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2814 CharDriverState
*chr
;
2817 if (strstart(filename
, "chardev:", &p
)) {
2818 return qemu_chr_find(p
);
2821 opts
= qemu_chr_parse_compat(label
, filename
);
2825 chr
= qemu_chr_new_from_opts(opts
, init
);
2826 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2827 monitor_init(chr
, MONITOR_USE_READLINE
);
2829 qemu_opts_del(opts
);
2833 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
2835 if (chr
->chr_set_echo
) {
2836 chr
->chr_set_echo(chr
, echo
);
2840 void qemu_chr_fe_open(struct CharDriverState
*chr
)
2842 if (chr
->chr_guest_open
) {
2843 chr
->chr_guest_open(chr
);
2847 void qemu_chr_fe_close(struct CharDriverState
*chr
)
2849 if (chr
->chr_guest_close
) {
2850 chr
->chr_guest_close(chr
);
2854 void qemu_chr_delete(CharDriverState
*chr
)
2856 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2858 chr
->chr_close(chr
);
2859 g_free(chr
->filename
);
2864 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
2866 ChardevInfoList
*chr_list
= NULL
;
2867 CharDriverState
*chr
;
2869 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2870 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
2871 info
->value
= g_malloc0(sizeof(*info
->value
));
2872 info
->value
->label
= g_strdup(chr
->label
);
2873 info
->value
->filename
= g_strdup(chr
->filename
);
2875 info
->next
= chr_list
;
2882 CharDriverState
*qemu_chr_find(const char *name
)
2884 CharDriverState
*chr
;
2886 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2887 if (strcmp(chr
->label
, name
) != 0)
2894 /* Get a character (serial) device interface. */
2895 CharDriverState
*qemu_char_get_next_serial(void)
2897 static int next_serial
;
2899 /* FIXME: This function needs to go away: use chardev properties! */
2900 return serial_hds
[next_serial
++];