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 "qemu-objects.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(__FreeBSD__) || defined(__FreeBSD_kernel__)
61 #include <dev/ppbus/ppi.h>
62 #include <dev/ppbus/ppbconf.h>
63 #if defined(__GLIBC__)
66 #elif defined(__DragonFly__)
68 #include <dev/misc/ppi/ppi.h>
69 #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 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
105 /***********************************************************/
106 /* character device */
108 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
109 QTAILQ_HEAD_INITIALIZER(chardevs
);
111 static void qemu_chr_event(CharDriverState
*s
, int event
)
113 /* Keep track if the char device is open */
115 case CHR_EVENT_OPENED
:
118 case CHR_EVENT_CLOSED
:
125 s
->chr_event(s
->handler_opaque
, event
, NULL
);
128 static void qemu_chr_generic_open_bh(void *opaque
)
130 CharDriverState
*s
= opaque
;
131 qemu_chr_event(s
, CHR_EVENT_OPENED
);
132 qemu_bh_delete(s
->bh
);
136 void qemu_chr_generic_open(CharDriverState
*s
)
139 s
->bh
= qemu_bh_new(qemu_chr_generic_open_bh
, s
);
140 qemu_bh_schedule(s
->bh
);
144 static uint32_t char_queue_get_avail(CharQueue
*q
)
146 return sizeof(q
->ring
) - (q
->prod
- q
->cons
);
149 static bool char_queue_get_empty(CharQueue
*q
)
151 return (q
->cons
== q
->prod
);
154 static size_t char_queue_write(CharQueue
*q
, const void *data
, size_t size
)
156 const uint8_t *ptr
= data
;
159 for (i
= 0; i
< size
; i
++) {
160 if (char_queue_get_avail(q
) == 0) {
164 q
->ring
[q
->prod
% sizeof(q
->ring
)] = ptr
[i
];
171 static size_t char_queue_read(CharQueue
*q
, void *data
, size_t size
)
176 for (i
= 0; i
< size
; i
++) {
177 if (char_queue_get_empty(q
)) {
181 ptr
[i
] = q
->ring
[q
->cons
% sizeof(q
->ring
)];
188 static void qemu_chr_flush_fe_tx(CharDriverState
*s
)
190 uint8_t buf
[MAX_CHAR_QUEUE_RING
];
193 /* Don't use polling queue if new style handlers are registered */
194 if (s
->be_read
|| s
->be_write
) {
198 /* Drain the queue into a flat buffer */
199 len
= char_queue_read(&s
->fe_tx
, buf
, sizeof(buf
));
201 s
->chr_write(s
, buf
, len
);
203 /* We drop unwritten data until we have backend flow control */
206 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
211 assert(s
->fe_opened
> 0);
213 is_empty
= char_queue_get_empty(&s
->fe_tx
);
215 ret
= char_queue_write(&s
->fe_tx
, buf
, len
);
217 /* If the queue was empty, and now it's not, generate a read edge
218 * event for the backend. */
219 if (is_empty
&& !char_queue_get_empty(&s
->fe_tx
)) {
221 s
->be_read(s
->opaque
);
225 qemu_chr_flush_fe_tx(s
);
230 int qemu_chr_fe_read(CharDriverState
*s
, uint8_t *buf
, int len
)
235 assert(s
->fe_opened
> 0);
237 is_full
= (char_queue_get_avail(&s
->be_tx
) == 0);
239 ret
= char_queue_read(&s
->be_tx
, buf
, len
);
241 /* If the queue was empty, and now its not, generate a write edge
242 * event for the backend. */
243 if (is_full
&& char_queue_get_avail(&s
->be_tx
)) {
245 s
->be_write(s
->opaque
);
252 void qemu_chr_fe_set_handlers(CharDriverState
*s
,
254 IOHandler
*chr_write
,
255 IOEventHandler
*chr_event
,
258 assert(s
->fe_opened
> 0);
260 if (!opaque
&& !chr_read
&& !chr_write
&& !chr_event
) {
261 /* chr driver being released. */
262 ++s
->avail_connections
;
264 s
->fe_read
= chr_read
;
265 s
->fe_write
= chr_write
;
266 s
->chr_event
= chr_event
;
267 s
->handler_opaque
= opaque
;
269 /* We're connecting to an already opened device, so let's make sure we
270 also get the open event */
272 qemu_chr_generic_open(s
);
276 int qemu_chr_be_can_write(CharDriverState
*s
)
278 /* Try to flush any queued data before returning how much data we can
280 return char_queue_get_avail(&s
->be_tx
);
283 int qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
288 is_empty
= char_queue_get_empty(&s
->be_tx
);
290 ret
= char_queue_write(&s
->be_tx
, buf
, len
);
292 /* If the queue was empty, and now it's not, trigger a read edge
294 if (is_empty
&& !char_queue_get_empty(&s
->be_tx
)) {
296 s
->fe_read(s
->handler_opaque
);
303 int qemu_chr_be_read(CharDriverState
*s
, uint8_t *buf
, int len
)
308 is_full
= (char_queue_get_avail(&s
->fe_tx
) == 0);
310 ret
= char_queue_read(&s
->fe_tx
, buf
, len
);
312 /* If the queue was full, and now it's not, trigger an write edge
314 if (is_full
&& char_queue_get_avail(&s
->fe_tx
)) {
316 s
->fe_write(s
->handler_opaque
);
323 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
327 return s
->chr_ioctl(s
->opaque
, cmd
, arg
);
330 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
332 char buf
[READ_BUF_LEN
];
335 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
336 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
340 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
345 static int qemu_chr_open_null(QemuOpts
*opts
, CharDriverState
**_chr
)
347 CharDriverState
*chr
;
349 chr
= qemu_mallocz(sizeof(CharDriverState
));
350 chr
->chr_write
= null_chr_write
;
357 int send_all(int fd
, const void *buf
, int len1
)
363 ret
= send(fd
, buf
, len
, 0);
365 errno
= WSAGetLastError();
366 if (errno
!= WSAEWOULDBLOCK
) {
369 } else if (ret
== 0) {
381 int send_all(int fd
, const void *_buf
, int len1
)
384 const uint8_t *buf
= _buf
;
388 ret
= write(fd
, buf
, len
);
390 if (errno
!= EINTR
&& errno
!= EAGAIN
)
392 } else if (ret
== 0) {
410 #define STDIO_MAX_CLIENTS 1
411 static int stdio_nb_clients
= 0;
413 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
415 FDCharDriver
*s
= chr
->opaque
;
416 return send_all(s
->fd_out
, buf
, len
);
419 static int fd_chr_read_poll(void *opaque
)
421 CharDriverState
*chr
= opaque
;
422 FDCharDriver
*s
= chr
->opaque
;
424 s
->max_size
= qemu_chr_be_can_write(chr
);
428 static void fd_chr_read(void *opaque
)
430 CharDriverState
*chr
= opaque
;
431 FDCharDriver
*s
= chr
->opaque
;
433 uint8_t buf
[READ_BUF_LEN
];
436 if (len
> s
->max_size
)
440 size
= read(s
->fd_in
, buf
, len
);
442 /* FD has been closed. Remove it from the active list. */
443 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
444 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
448 qemu_chr_be_write(chr
, buf
, size
);
452 static void fd_chr_close(struct CharDriverState
*chr
)
454 FDCharDriver
*s
= chr
->opaque
;
457 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
459 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
464 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
467 /* open a character device to a unix fd */
468 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
470 CharDriverState
*chr
;
473 chr
= qemu_mallocz(sizeof(CharDriverState
));
474 s
= qemu_mallocz(sizeof(FDCharDriver
));
478 chr
->chr_write
= fd_chr_write
;
479 chr
->chr_close
= fd_chr_close
;
481 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
482 fd_chr_read
, NULL
, chr
);
484 qemu_chr_generic_open(chr
);
489 static int qemu_chr_open_file_out(QemuOpts
*opts
, CharDriverState
**_chr
)
493 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
494 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
499 *_chr
= qemu_chr_open_fd(-1, fd_out
);
503 static int qemu_chr_open_pipe(QemuOpts
*opts
, CharDriverState
**_chr
)
506 char filename_in
[256], filename_out
[256];
507 const char *filename
= qemu_opt_get(opts
, "path");
509 if (filename
== NULL
) {
510 fprintf(stderr
, "chardev: pipe: no filename given\n");
514 snprintf(filename_in
, 256, "%s.in", filename
);
515 snprintf(filename_out
, 256, "%s.out", filename
);
516 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
517 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
518 if (fd_in
< 0 || fd_out
< 0) {
523 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
529 *_chr
= qemu_chr_open_fd(fd_in
, fd_out
);
534 /* for STDIO, we handle the case where several clients use it
537 #define TERM_FIFO_MAX_SIZE 1
539 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
540 static int term_fifo_size
;
542 static int stdio_read_poll(void *opaque
)
544 CharDriverState
*chr
= opaque
;
546 /* try to flush the queue if needed */
547 if (term_fifo_size
!= 0 && qemu_chr_be_can_write(chr
) > 0) {
548 qemu_chr_be_write(chr
, term_fifo
, 1);
551 /* see if we can absorb more chars */
552 if (term_fifo_size
== 0)
558 static void stdio_read(void *opaque
)
562 CharDriverState
*chr
= opaque
;
564 size
= read(0, buf
, 1);
566 /* stdin has been closed. Remove it from the active list. */
567 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
568 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
572 if (qemu_chr_be_can_write(chr
) > 0) {
573 qemu_chr_be_write(chr
, buf
, 1);
574 } else if (term_fifo_size
== 0) {
575 term_fifo
[term_fifo_size
++] = buf
[0];
580 /* init terminal so that we can grab keys */
581 static struct termios oldtty
;
582 static int old_fd0_flags
;
583 static bool stdio_allow_signal
;
585 static void term_exit(void)
587 tcsetattr (0, TCSANOW
, &oldtty
);
588 fcntl(0, F_SETFL
, old_fd0_flags
);
591 static int qemu_chr_ioctl_stdio(void *opaque
, int cmd
, void *data
)
596 if (cmd
!= CHR_SET_ECHO
) {
602 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
603 |INLCR
|IGNCR
|ICRNL
|IXON
);
604 tty
.c_oflag
|= OPOST
;
605 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
606 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
611 /* if graphical mode, we allow Ctrl-C handling */
612 if (!stdio_allow_signal
)
613 tty
.c_lflag
&= ~ISIG
;
615 tcsetattr (0, TCSANOW
, &tty
);
620 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
624 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
628 static int qemu_chr_open_stdio(QemuOpts
*opts
, CharDriverState
**_chr
)
630 CharDriverState
*chr
;
632 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
) {
636 if (stdio_nb_clients
== 0) {
637 old_fd0_flags
= fcntl(0, F_GETFL
);
638 tcgetattr (0, &oldtty
);
639 fcntl(0, F_SETFL
, O_NONBLOCK
);
643 chr
= qemu_chr_open_fd(0, 1);
644 chr
->chr_close
= qemu_chr_close_stdio
;
645 chr
->chr_ioctl
= qemu_chr_ioctl_stdio
;
646 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
648 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
649 display_type
!= DT_NOGRAPHIC
);
650 qemu_chr_fe_set_echo(chr
, false);
657 /* Once Solaris has openpty(), this is going to be removed. */
658 static int openpty(int *amaster
, int *aslave
, char *name
,
659 struct termios
*termp
, struct winsize
*winp
)
662 int mfd
= -1, sfd
= -1;
664 *amaster
= *aslave
= -1;
666 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
670 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
673 if ((slave
= ptsname(mfd
)) == NULL
)
676 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
679 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
680 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
688 ioctl(sfd
, TIOCSWINSZ
, winp
);
699 static void cfmakeraw (struct termios
*termios_p
)
701 termios_p
->c_iflag
&=
702 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
703 termios_p
->c_oflag
&= ~OPOST
;
704 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
705 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
706 termios_p
->c_cflag
|= CS8
;
708 termios_p
->c_cc
[VMIN
] = 0;
709 termios_p
->c_cc
[VTIME
] = 0;
713 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
714 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
715 || defined(__GLIBC__)
725 static void pty_chr_update_read_handler(CharDriverState
*chr
);
726 static void pty_chr_state(CharDriverState
*chr
, int connected
);
728 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
730 PtyCharDriver
*s
= chr
->opaque
;
733 /* guest sends data, check for (re-)connect */
734 pty_chr_update_read_handler(chr
);
737 return send_all(s
->fd
, buf
, len
);
740 static int pty_chr_read_poll(void *opaque
)
742 CharDriverState
*chr
= opaque
;
743 PtyCharDriver
*s
= chr
->opaque
;
745 s
->read_bytes
= qemu_chr_be_can_write(chr
);
746 return s
->read_bytes
;
749 static void pty_chr_read(void *opaque
)
751 CharDriverState
*chr
= opaque
;
752 PtyCharDriver
*s
= chr
->opaque
;
754 uint8_t buf
[READ_BUF_LEN
];
757 if (len
> s
->read_bytes
)
761 size
= read(s
->fd
, buf
, len
);
762 if ((size
== -1 && errno
== EIO
) ||
764 pty_chr_state(chr
, 0);
768 pty_chr_state(chr
, 1);
769 qemu_chr_be_write(chr
, buf
, size
);
773 static void pty_chr_update_read_handler(CharDriverState
*chr
)
775 PtyCharDriver
*s
= chr
->opaque
;
777 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
778 pty_chr_read
, NULL
, chr
);
781 * Short timeout here: just need wait long enougth that qemu makes
782 * it through the poll loop once. When reconnected we want a
783 * short timeout so we notice it almost instantly. Otherwise
784 * read() gives us -EIO instantly, making pty_chr_state() reset the
785 * timeout to the normal (much longer) poll interval before the
788 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
791 static void pty_chr_state(CharDriverState
*chr
, int connected
)
793 PtyCharDriver
*s
= chr
->opaque
;
796 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
799 /* (re-)connect poll interval for idle guests: once per second.
800 * We check more frequently in case the guests sends data to
801 * the virtual device linked to our pty. */
802 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
805 qemu_chr_generic_open(chr
);
810 static void pty_chr_timer(void *opaque
)
812 struct CharDriverState
*chr
= opaque
;
813 PtyCharDriver
*s
= chr
->opaque
;
818 /* If we arrive here without polling being cleared due
819 * read returning -EIO, then we are (re-)connected */
820 pty_chr_state(chr
, 1);
825 pty_chr_update_read_handler(chr
);
828 static void pty_chr_close(struct CharDriverState
*chr
)
830 PtyCharDriver
*s
= chr
->opaque
;
832 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
834 qemu_del_timer(s
->timer
);
835 qemu_free_timer(s
->timer
);
837 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
840 static int qemu_chr_open_pty(QemuOpts
*opts
, CharDriverState
**_chr
)
842 CharDriverState
*chr
;
846 #if defined(__OpenBSD__) || defined(__DragonFly__)
847 char pty_name
[PATH_MAX
];
848 #define q_ptsname(x) pty_name
850 char *pty_name
= NULL
;
851 #define q_ptsname(x) ptsname(x)
854 chr
= qemu_mallocz(sizeof(CharDriverState
));
855 s
= qemu_mallocz(sizeof(PtyCharDriver
));
857 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
861 /* Set raw attributes on the pty. */
862 tcgetattr(slave_fd
, &tty
);
864 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
867 len
= strlen(q_ptsname(s
->fd
)) + 5;
868 chr
->filename
= qemu_malloc(len
);
869 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
870 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
871 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
874 chr
->chr_write
= pty_chr_write
;
875 chr
->chr_close
= pty_chr_close
;
877 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
883 static void tty_serial_init(int fd
, int speed
,
884 int parity
, int data_bits
, int stop_bits
)
890 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
891 speed
, parity
, data_bits
, stop_bits
);
893 tcgetattr (fd
, &tty
);
895 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
896 speed
= speed
* 10 / 11;
913 /* Non-Posix values follow. They may be unsupported on some systems. */
932 check_speed(1000000);
935 check_speed(1152000);
938 check_speed(1500000);
941 check_speed(2000000);
944 check_speed(2500000);
947 check_speed(3000000);
950 check_speed(3500000);
953 check_speed(4000000);
958 cfsetispeed(&tty
, spd
);
959 cfsetospeed(&tty
, spd
);
961 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
962 |INLCR
|IGNCR
|ICRNL
|IXON
);
963 tty
.c_oflag
|= OPOST
;
964 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
965 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
986 tty
.c_cflag
|= PARENB
;
989 tty
.c_cflag
|= PARENB
| PARODD
;
993 tty
.c_cflag
|= CSTOPB
;
995 tcsetattr (fd
, TCSANOW
, &tty
);
998 static int tty_serial_ioctl(void *opaque
, int cmd
, void *arg
)
1000 FDCharDriver
*s
= opaque
;
1003 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1005 QEMUSerialSetParams
*ssp
= arg
;
1006 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1007 ssp
->data_bits
, ssp
->stop_bits
);
1010 case CHR_IOCTL_SERIAL_SET_BREAK
:
1012 int enable
= *(int *)arg
;
1014 tcsendbreak(s
->fd_in
, 1);
1017 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1020 int *targ
= (int *)arg
;
1021 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1023 if (sarg
& TIOCM_CTS
)
1024 *targ
|= CHR_TIOCM_CTS
;
1025 if (sarg
& TIOCM_CAR
)
1026 *targ
|= CHR_TIOCM_CAR
;
1027 if (sarg
& TIOCM_DSR
)
1028 *targ
|= CHR_TIOCM_DSR
;
1029 if (sarg
& TIOCM_RI
)
1030 *targ
|= CHR_TIOCM_RI
;
1031 if (sarg
& TIOCM_DTR
)
1032 *targ
|= CHR_TIOCM_DTR
;
1033 if (sarg
& TIOCM_RTS
)
1034 *targ
|= CHR_TIOCM_RTS
;
1037 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1039 int sarg
= *(int *)arg
;
1041 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1042 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1043 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1044 if (sarg
& CHR_TIOCM_CTS
)
1046 if (sarg
& CHR_TIOCM_CAR
)
1048 if (sarg
& CHR_TIOCM_DSR
)
1050 if (sarg
& CHR_TIOCM_RI
)
1052 if (sarg
& CHR_TIOCM_DTR
)
1054 if (sarg
& CHR_TIOCM_RTS
)
1056 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1065 static void qemu_chr_close_tty(CharDriverState
*chr
)
1067 FDCharDriver
*s
= chr
->opaque
;
1081 static int qemu_chr_open_tty(QemuOpts
*opts
, CharDriverState
**_chr
)
1083 const char *filename
= qemu_opt_get(opts
, "path");
1084 CharDriverState
*chr
;
1087 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1091 tty_serial_init(fd
, 115200, 'N', 8, 1);
1092 chr
= qemu_chr_open_fd(fd
, fd
);
1093 chr
->chr_ioctl
= tty_serial_ioctl
;
1094 chr
->chr_close
= qemu_chr_close_tty
;
1099 #else /* ! __linux__ && ! __sun__ */
1100 static int qemu_chr_open_pty(QemuOpts
*opts
, CharDriverState
**_chr
)
1104 #endif /* __linux__ || __sun__ */
1106 #if defined(__linux__)
1110 } ParallelCharDriver
;
1112 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1114 if (s
->mode
!= mode
) {
1116 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1123 static int pp_ioctl(void *opaque
, int cmd
, void *arg
)
1125 ParallelCharDriver
*drv
= opaque
;
1130 case CHR_IOCTL_PP_READ_DATA
:
1131 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1133 *(uint8_t *)arg
= b
;
1135 case CHR_IOCTL_PP_WRITE_DATA
:
1136 b
= *(uint8_t *)arg
;
1137 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1140 case CHR_IOCTL_PP_READ_CONTROL
:
1141 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1143 /* Linux gives only the lowest bits, and no way to know data
1144 direction! For better compatibility set the fixed upper
1146 *(uint8_t *)arg
= b
| 0xc0;
1148 case CHR_IOCTL_PP_WRITE_CONTROL
:
1149 b
= *(uint8_t *)arg
;
1150 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1153 case CHR_IOCTL_PP_READ_STATUS
:
1154 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1156 *(uint8_t *)arg
= b
;
1158 case CHR_IOCTL_PP_DATA_DIR
:
1159 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1162 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1163 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1164 struct ParallelIOArg
*parg
= arg
;
1165 int n
= read(fd
, parg
->buffer
, parg
->count
);
1166 if (n
!= parg
->count
) {
1171 case CHR_IOCTL_PP_EPP_READ
:
1172 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1173 struct ParallelIOArg
*parg
= arg
;
1174 int n
= read(fd
, parg
->buffer
, parg
->count
);
1175 if (n
!= parg
->count
) {
1180 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1181 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1182 struct ParallelIOArg
*parg
= arg
;
1183 int n
= write(fd
, parg
->buffer
, parg
->count
);
1184 if (n
!= parg
->count
) {
1189 case CHR_IOCTL_PP_EPP_WRITE
:
1190 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1191 struct ParallelIOArg
*parg
= arg
;
1192 int n
= write(fd
, parg
->buffer
, parg
->count
);
1193 if (n
!= parg
->count
) {
1204 static void pp_close(CharDriverState
*chr
)
1206 ParallelCharDriver
*drv
= chr
->opaque
;
1209 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1210 ioctl(fd
, PPRELEASE
);
1213 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1216 static int qemu_chr_open_pp(QemuOpts
*opts
, CharDriverState
**_chr
)
1218 const char *filename
= qemu_opt_get(opts
, "path");
1219 CharDriverState
*chr
;
1220 ParallelCharDriver
*drv
;
1223 TFR(fd
= open(filename
, O_RDWR
));
1228 if (ioctl(fd
, PPCLAIM
) < 0) {
1233 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1235 drv
->mode
= IEEE1284_MODE_COMPAT
;
1237 chr
= qemu_mallocz(sizeof(CharDriverState
));
1238 chr
->chr_write
= null_chr_write
;
1239 chr
->chr_ioctl
= pp_ioctl
;
1240 chr
->chr_close
= pp_close
;
1243 qemu_chr_generic_open(chr
);
1248 #endif /* __linux__ */
1250 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1251 static int pp_ioctl(void *opaque
, int cmd
, void *arg
)
1253 int fd
= (int)(intptr_t)opaque
;
1257 case CHR_IOCTL_PP_READ_DATA
:
1258 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1260 *(uint8_t *)arg
= b
;
1262 case CHR_IOCTL_PP_WRITE_DATA
:
1263 b
= *(uint8_t *)arg
;
1264 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1267 case CHR_IOCTL_PP_READ_CONTROL
:
1268 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1270 *(uint8_t *)arg
= b
;
1272 case CHR_IOCTL_PP_WRITE_CONTROL
:
1273 b
= *(uint8_t *)arg
;
1274 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1277 case CHR_IOCTL_PP_READ_STATUS
:
1278 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1280 *(uint8_t *)arg
= b
;
1288 static int qemu_chr_open_pp(QemuOpts
*opts
, CharDriverState
**_chr
)
1290 const char *filename
= qemu_opt_get(opts
, "path");
1291 CharDriverState
*chr
;
1294 fd
= qemu_open(filename
, O_RDWR
);
1299 chr
= qemu_mallocz(sizeof(CharDriverState
));
1300 chr
->opaque
= (void *)(intptr_t)fd
;
1301 chr
->chr_write
= null_chr_write
;
1302 chr
->chr_ioctl
= pp_ioctl
;
1313 HANDLE hcom
, hrecv
, hsend
;
1314 OVERLAPPED orecv
, osend
;
1319 #define NSENDBUF 2048
1320 #define NRECVBUF 2048
1321 #define MAXCONNECT 1
1322 #define NTIMEOUT 5000
1324 static int win_chr_poll(void *opaque
);
1325 static int win_chr_pipe_poll(void *opaque
);
1327 static void win_chr_close(CharDriverState
*chr
)
1329 WinCharState
*s
= chr
->opaque
;
1332 CloseHandle(s
->hsend
);
1336 CloseHandle(s
->hrecv
);
1340 CloseHandle(s
->hcom
);
1344 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1346 qemu_del_polling_cb(win_chr_poll
, chr
);
1348 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1351 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1353 WinCharState
*s
= chr
->opaque
;
1355 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1360 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1362 fprintf(stderr
, "Failed CreateEvent\n");
1365 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1367 fprintf(stderr
, "Failed CreateEvent\n");
1371 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1372 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1373 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1374 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1379 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1380 fprintf(stderr
, "Failed SetupComm\n");
1384 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1385 size
= sizeof(COMMCONFIG
);
1386 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1387 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1388 CommConfigDialog(filename
, NULL
, &comcfg
);
1390 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1391 fprintf(stderr
, "Failed SetCommState\n");
1395 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1396 fprintf(stderr
, "Failed SetCommMask\n");
1400 cto
.ReadIntervalTimeout
= MAXDWORD
;
1401 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1402 fprintf(stderr
, "Failed SetCommTimeouts\n");
1406 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1407 fprintf(stderr
, "Failed ClearCommError\n");
1410 qemu_add_polling_cb(win_chr_poll
, chr
);
1418 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1420 WinCharState
*s
= chr
->opaque
;
1421 DWORD len
, ret
, size
, err
;
1424 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1425 s
->osend
.hEvent
= s
->hsend
;
1428 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1430 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1432 err
= GetLastError();
1433 if (err
== ERROR_IO_PENDING
) {
1434 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1452 static int win_chr_read_poll(CharDriverState
*chr
)
1454 WinCharState
*s
= chr
->opaque
;
1456 s
->max_size
= qemu_chr_be_can_write(chr
);
1460 static void win_chr_readfile(CharDriverState
*chr
)
1462 WinCharState
*s
= chr
->opaque
;
1464 uint8_t buf
[READ_BUF_LEN
];
1467 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1468 s
->orecv
.hEvent
= s
->hrecv
;
1469 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1471 err
= GetLastError();
1472 if (err
== ERROR_IO_PENDING
) {
1473 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1478 qemu_chr_be_write(chr
, buf
, size
);
1482 static void win_chr_read(CharDriverState
*chr
)
1484 WinCharState
*s
= chr
->opaque
;
1486 if (s
->len
> s
->max_size
)
1487 s
->len
= s
->max_size
;
1491 win_chr_readfile(chr
);
1494 static int win_chr_poll(void *opaque
)
1496 CharDriverState
*chr
= opaque
;
1497 WinCharState
*s
= chr
->opaque
;
1501 ClearCommError(s
->hcom
, &comerr
, &status
);
1502 if (status
.cbInQue
> 0) {
1503 s
->len
= status
.cbInQue
;
1504 win_chr_read_poll(chr
);
1511 static int qemu_chr_open_win(QemuOpts
*opts
, CharDriverState
**_chr
)
1513 const char *filename
= qemu_opt_get(opts
, "path");
1514 CharDriverState
*chr
;
1517 chr
= qemu_mallocz(sizeof(CharDriverState
));
1518 s
= qemu_mallocz(sizeof(WinCharState
));
1520 chr
->chr_write
= win_chr_write
;
1521 chr
->chr_close
= win_chr_close
;
1523 if (win_chr_init(chr
, filename
) < 0) {
1528 qemu_chr_generic_open(chr
);
1534 static int win_chr_pipe_poll(void *opaque
)
1536 CharDriverState
*chr
= opaque
;
1537 WinCharState
*s
= chr
->opaque
;
1540 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1543 win_chr_read_poll(chr
);
1550 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1552 WinCharState
*s
= chr
->opaque
;
1560 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1562 fprintf(stderr
, "Failed CreateEvent\n");
1565 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1567 fprintf(stderr
, "Failed CreateEvent\n");
1571 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1572 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1573 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1575 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1576 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1577 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1582 ZeroMemory(&ov
, sizeof(ov
));
1583 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1584 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1586 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1590 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1592 fprintf(stderr
, "Failed GetOverlappedResult\n");
1594 CloseHandle(ov
.hEvent
);
1601 CloseHandle(ov
.hEvent
);
1604 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1613 static int qemu_chr_open_win_pipe(QemuOpts
*opts
, CharDriverState
**_chr
)
1615 const char *filename
= qemu_opt_get(opts
, "path");
1616 CharDriverState
*chr
;
1619 chr
= qemu_mallocz(sizeof(CharDriverState
));
1620 s
= qemu_mallocz(sizeof(WinCharState
));
1622 chr
->chr_write
= win_chr_write
;
1623 chr
->chr_close
= win_chr_close
;
1625 if (win_chr_pipe_init(chr
, filename
) < 0) {
1630 qemu_chr_generic_open(chr
);
1636 static int qemu_chr_open_win_file(HANDLE fd_out
, CharDriverState
**pchr
)
1638 CharDriverState
*chr
;
1641 chr
= qemu_mallocz(sizeof(CharDriverState
));
1642 s
= qemu_mallocz(sizeof(WinCharState
));
1645 chr
->chr_write
= win_chr_write
;
1646 qemu_chr_generic_open(chr
);
1651 static int qemu_chr_open_win_con(QemuOpts
*opts
, CharDriverState
**chr
)
1653 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
), chr
);
1656 static int qemu_chr_open_win_file_out(QemuOpts
*opts
, CharDriverState
**_chr
)
1658 const char *file_out
= qemu_opt_get(opts
, "path");
1661 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1662 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1663 if (fd_out
== INVALID_HANDLE_VALUE
) {
1667 return qemu_chr_open_win_file(fd_out
, _chr
);
1669 #endif /* !_WIN32 */
1671 /***********************************************************/
1672 /* UDP Net console */
1676 uint8_t buf
[READ_BUF_LEN
];
1682 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1684 NetCharDriver
*s
= chr
->opaque
;
1686 return send(s
->fd
, (const void *)buf
, len
, 0);
1689 static int udp_chr_read_poll(void *opaque
)
1691 CharDriverState
*chr
= opaque
;
1692 NetCharDriver
*s
= chr
->opaque
;
1694 s
->max_size
= qemu_chr_be_can_write(chr
);
1696 /* If there were any stray characters in the queue process them
1699 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1700 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
1702 s
->max_size
= qemu_chr_be_can_write(chr
);
1707 static void udp_chr_read(void *opaque
)
1709 CharDriverState
*chr
= opaque
;
1710 NetCharDriver
*s
= chr
->opaque
;
1712 if (s
->max_size
== 0)
1714 s
->bufcnt
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1715 s
->bufptr
= s
->bufcnt
;
1720 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1721 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
1723 s
->max_size
= qemu_chr_be_can_write(chr
);
1727 static void udp_chr_close(CharDriverState
*chr
)
1729 NetCharDriver
*s
= chr
->opaque
;
1731 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1735 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1738 static int qemu_chr_open_udp(QemuOpts
*opts
, CharDriverState
**_chr
)
1740 CharDriverState
*chr
= NULL
;
1741 NetCharDriver
*s
= NULL
;
1745 chr
= qemu_mallocz(sizeof(CharDriverState
));
1746 s
= qemu_mallocz(sizeof(NetCharDriver
));
1748 fd
= inet_dgram_opts(opts
);
1750 fprintf(stderr
, "inet_dgram_opts failed\n");
1759 chr
->chr_write
= udp_chr_write
;
1760 chr
->chr_close
= udp_chr_close
;
1762 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1763 udp_chr_read
, NULL
, chr
);
1777 /***********************************************************/
1778 /* TCP Net console */
1790 static void tcp_chr_accept(void *opaque
);
1792 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1794 TCPCharDriver
*s
= chr
->opaque
;
1796 return send_all(s
->fd
, buf
, len
);
1798 /* XXX: indicate an error ? */
1803 static int tcp_chr_read_poll(void *opaque
)
1805 CharDriverState
*chr
= opaque
;
1806 TCPCharDriver
*s
= chr
->opaque
;
1809 s
->max_size
= qemu_chr_be_can_write(chr
);
1814 #define IAC_BREAK 243
1815 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1817 uint8_t *buf
, int *size
)
1819 /* Handle any telnet client's basic IAC options to satisfy char by
1820 * char mode with no echo. All IAC options will be removed from
1821 * the buf and the do_telnetopt variable will be used to track the
1822 * state of the width of the IAC information.
1824 * IAC commands come in sets of 3 bytes with the exception of the
1825 * "IAC BREAK" command and the double IAC.
1831 for (i
= 0; i
< *size
; i
++) {
1832 if (s
->do_telnetopt
> 1) {
1833 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1834 /* Double IAC means send an IAC */
1838 s
->do_telnetopt
= 1;
1840 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1841 /* Handle IAC break commands by sending a serial break */
1842 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1847 if (s
->do_telnetopt
>= 4) {
1848 s
->do_telnetopt
= 1;
1851 if ((unsigned char)buf
[i
] == IAC
) {
1852 s
->do_telnetopt
= 2;
1863 static int tcp_chr_ioctl(void *opaque
, int cmd
, void *data
)
1865 TCPCharDriver
*s
= opaque
;
1869 case CHR_GET_MSGFD
: {
1885 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1887 TCPCharDriver
*s
= chr
->opaque
;
1888 struct cmsghdr
*cmsg
;
1890 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1893 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1894 cmsg
->cmsg_level
!= SOL_SOCKET
||
1895 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1898 fd
= *((int *)CMSG_DATA(cmsg
));
1908 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1910 TCPCharDriver
*s
= chr
->opaque
;
1911 struct msghdr msg
= { NULL
, };
1912 struct iovec iov
[1];
1914 struct cmsghdr cmsg
;
1915 char control
[CMSG_SPACE(sizeof(int))];
1919 iov
[0].iov_base
= buf
;
1920 iov
[0].iov_len
= len
;
1924 msg
.msg_control
= &msg_control
;
1925 msg
.msg_controllen
= sizeof(msg_control
);
1927 ret
= recvmsg(s
->fd
, &msg
, 0);
1928 if (ret
> 0 && s
->is_unix
)
1929 unix_process_msgfd(chr
, &msg
);
1934 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1936 TCPCharDriver
*s
= chr
->opaque
;
1937 return qemu_recv(s
->fd
, buf
, len
, 0);
1941 static void tcp_chr_read(void *opaque
)
1943 CharDriverState
*chr
= opaque
;
1944 TCPCharDriver
*s
= chr
->opaque
;
1945 uint8_t buf
[READ_BUF_LEN
];
1948 if (!s
->connected
|| s
->max_size
<= 0)
1951 if (len
> s
->max_size
)
1953 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
1955 /* connection closed */
1957 if (s
->listen_fd
>= 0) {
1958 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1960 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1963 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1964 } else if (size
> 0) {
1965 if (s
->do_telnetopt
)
1966 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1968 qemu_chr_be_write(chr
, buf
, size
);
1973 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
1975 return qemu_chr_open_fd(eventfd
, eventfd
);
1979 static void tcp_chr_connect(void *opaque
)
1981 CharDriverState
*chr
= opaque
;
1982 TCPCharDriver
*s
= chr
->opaque
;
1985 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1986 tcp_chr_read
, NULL
, chr
);
1987 qemu_chr_generic_open(chr
);
1990 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1991 static void tcp_chr_telnet_init(int fd
)
1994 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1995 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1996 send(fd
, (char *)buf
, 3, 0);
1997 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1998 send(fd
, (char *)buf
, 3, 0);
1999 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2000 send(fd
, (char *)buf
, 3, 0);
2001 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2002 send(fd
, (char *)buf
, 3, 0);
2005 static void socket_set_nodelay(int fd
)
2008 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2011 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2013 TCPCharDriver
*s
= chr
->opaque
;
2017 socket_set_nonblock(fd
);
2019 socket_set_nodelay(fd
);
2021 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2022 tcp_chr_connect(chr
);
2027 static void tcp_chr_accept(void *opaque
)
2029 CharDriverState
*chr
= opaque
;
2030 TCPCharDriver
*s
= chr
->opaque
;
2031 struct sockaddr_in saddr
;
2033 struct sockaddr_un uaddr
;
2035 struct sockaddr
*addr
;
2042 len
= sizeof(uaddr
);
2043 addr
= (struct sockaddr
*)&uaddr
;
2047 len
= sizeof(saddr
);
2048 addr
= (struct sockaddr
*)&saddr
;
2050 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2051 if (fd
< 0 && errno
!= EINTR
) {
2053 } else if (fd
>= 0) {
2054 if (s
->do_telnetopt
)
2055 tcp_chr_telnet_init(fd
);
2059 if (tcp_chr_add_client(chr
, fd
) < 0)
2063 static void tcp_chr_close(CharDriverState
*chr
)
2065 TCPCharDriver
*s
= chr
->opaque
;
2067 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2070 if (s
->listen_fd
>= 0) {
2071 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2072 closesocket(s
->listen_fd
);
2075 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2078 static int qemu_chr_open_socket(QemuOpts
*opts
, CharDriverState
**_chr
)
2080 CharDriverState
*chr
= NULL
;
2081 TCPCharDriver
*s
= NULL
;
2090 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2091 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2092 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2093 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2094 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2098 chr
= qemu_mallocz(sizeof(CharDriverState
));
2099 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2103 fd
= unix_listen_opts(opts
);
2105 fd
= unix_connect_opts(opts
);
2109 fd
= inet_listen_opts(opts
, 0);
2111 fd
= inet_connect_opts(opts
);
2119 if (!is_waitconnect
)
2120 socket_set_nonblock(fd
);
2126 s
->is_unix
= is_unix
;
2127 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2130 chr
->chr_write
= tcp_chr_write
;
2131 chr
->chr_close
= tcp_chr_close
;
2132 chr
->chr_ioctl
= tcp_chr_ioctl
;
2136 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2138 s
->do_telnetopt
= 1;
2143 socket_set_nodelay(fd
);
2144 tcp_chr_connect(chr
);
2147 /* for "info chardev" monitor command */
2148 chr
->filename
= qemu_malloc(256);
2150 snprintf(chr
->filename
, 256, "unix:%s%s",
2151 qemu_opt_get(opts
, "path"),
2152 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2153 } else if (is_telnet
) {
2154 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2155 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2156 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2158 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2159 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2160 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2163 if (is_listen
&& is_waitconnect
) {
2164 printf("QEMU waiting for connection on: %s\n",
2166 tcp_chr_accept(chr
);
2167 socket_set_nonblock(s
->listen_fd
);
2181 /***********************************************************/
2182 /* Memory chardev */
2185 size_t outbuf_capacity
;
2189 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2191 MemoryDriver
*d
= chr
->opaque
;
2193 /* TODO: the QString implementation has the same code, we should
2194 * introduce a generic way to do this in cutils.c */
2195 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2197 d
->outbuf_capacity
+= len
;
2198 d
->outbuf_capacity
*= 2;
2199 d
->outbuf
= qemu_realloc(d
->outbuf
, d
->outbuf_capacity
);
2202 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2203 d
->outbuf_size
+= len
;
2208 void qemu_chr_init_mem(CharDriverState
*chr
)
2212 d
= qemu_malloc(sizeof(*d
));
2214 d
->outbuf_capacity
= 4096;
2215 d
->outbuf
= qemu_mallocz(d
->outbuf_capacity
);
2217 memset(chr
, 0, sizeof(*chr
));
2219 chr
->chr_write
= mem_chr_write
;
2222 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2224 MemoryDriver
*d
= chr
->opaque
;
2225 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2228 /* NOTE: this driver can not be closed with qemu_chr_fe_delete()! */
2229 void qemu_chr_close_mem(CharDriverState
*chr
)
2231 MemoryDriver
*d
= chr
->opaque
;
2233 qemu_free(d
->outbuf
);
2234 qemu_free(chr
->opaque
);
2236 chr
->chr_write
= NULL
;
2239 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2241 const MemoryDriver
*d
= chr
->opaque
;
2242 return d
->outbuf_size
;
2245 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2247 char host
[65], port
[33], width
[8], height
[8];
2252 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1);
2256 if (strcmp(filename
, "null") == 0 ||
2257 strcmp(filename
, "pty") == 0 ||
2258 strcmp(filename
, "msmouse") == 0 ||
2259 strcmp(filename
, "braille") == 0 ||
2260 strcmp(filename
, "stdio") == 0) {
2261 qemu_opt_set(opts
, "backend", filename
);
2264 if (strstart(filename
, "vc", &p
)) {
2265 qemu_opt_set(opts
, "backend", "vc");
2267 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2269 qemu_opt_set(opts
, "width", width
);
2270 qemu_opt_set(opts
, "height", height
);
2271 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2273 qemu_opt_set(opts
, "cols", width
);
2274 qemu_opt_set(opts
, "rows", height
);
2281 if (strcmp(filename
, "con:") == 0) {
2282 qemu_opt_set(opts
, "backend", "console");
2285 if (strstart(filename
, "COM", NULL
)) {
2286 qemu_opt_set(opts
, "backend", "serial");
2287 qemu_opt_set(opts
, "path", filename
);
2290 if (strstart(filename
, "file:", &p
)) {
2291 qemu_opt_set(opts
, "backend", "file");
2292 qemu_opt_set(opts
, "path", p
);
2295 if (strstart(filename
, "pipe:", &p
)) {
2296 qemu_opt_set(opts
, "backend", "pipe");
2297 qemu_opt_set(opts
, "path", p
);
2300 if (strstart(filename
, "tcp:", &p
) ||
2301 strstart(filename
, "telnet:", &p
)) {
2302 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2304 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2307 qemu_opt_set(opts
, "backend", "socket");
2308 qemu_opt_set(opts
, "host", host
);
2309 qemu_opt_set(opts
, "port", port
);
2310 if (p
[pos
] == ',') {
2311 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2314 if (strstart(filename
, "telnet:", &p
))
2315 qemu_opt_set(opts
, "telnet", "on");
2318 if (strstart(filename
, "udp:", &p
)) {
2319 qemu_opt_set(opts
, "backend", "udp");
2320 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2322 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2326 qemu_opt_set(opts
, "host", host
);
2327 qemu_opt_set(opts
, "port", port
);
2328 if (p
[pos
] == '@') {
2330 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2332 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2336 qemu_opt_set(opts
, "localaddr", host
);
2337 qemu_opt_set(opts
, "localport", port
);
2341 if (strstart(filename
, "unix:", &p
)) {
2342 qemu_opt_set(opts
, "backend", "socket");
2343 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2347 if (strstart(filename
, "/dev/parport", NULL
) ||
2348 strstart(filename
, "/dev/ppi", NULL
)) {
2349 qemu_opt_set(opts
, "backend", "parport");
2350 qemu_opt_set(opts
, "path", filename
);
2353 if (strstart(filename
, "/dev/", NULL
)) {
2354 qemu_opt_set(opts
, "backend", "tty");
2355 qemu_opt_set(opts
, "path", filename
);
2360 qemu_opts_del(opts
);
2364 static const struct {
2366 int (*open
)(QemuOpts
*opts
, CharDriverState
**chr
);
2367 } backend_table
[] = {
2368 { .name
= "null", .open
= qemu_chr_open_null
},
2369 { .name
= "socket", .open
= qemu_chr_open_socket
},
2370 { .name
= "udp", .open
= qemu_chr_open_udp
},
2371 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2372 { .name
= "vc", .open
= text_console_init
},
2374 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2375 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2376 { .name
= "console", .open
= qemu_chr_open_win_con
},
2377 { .name
= "serial", .open
= qemu_chr_open_win
},
2379 { .name
= "file", .open
= qemu_chr_open_file_out
},
2380 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2381 { .name
= "pty", .open
= qemu_chr_open_pty
},
2382 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2384 #ifdef CONFIG_BRLAPI
2385 { .name
= "braille", .open
= chr_baum_init
},
2387 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2388 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2389 || defined(__FreeBSD_kernel__)
2390 { .name
= "tty", .open
= qemu_chr_open_tty
},
2392 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2393 || defined(__FreeBSD_kernel__)
2394 { .name
= "parport", .open
= qemu_chr_open_pp
},
2397 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
2401 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
2402 void (*init
)(struct CharDriverState
*s
))
2404 CharDriverState
*chr
;
2408 if (qemu_opts_id(opts
) == NULL
) {
2409 fprintf(stderr
, "chardev: no id specified\n");
2413 if (qemu_opt_get(opts
, "backend") == NULL
) {
2414 fprintf(stderr
, "chardev: \"%s\" missing backend\n",
2415 qemu_opts_id(opts
));
2418 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2419 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2422 if (i
== ARRAY_SIZE(backend_table
)) {
2423 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2424 qemu_opt_get(opts
, "backend"));
2428 ret
= backend_table
[i
].open(opts
, &chr
);
2430 fprintf(stderr
, "chardev: opening backend \"%s\" failed: %s\n",
2431 qemu_opt_get(opts
, "backend"), strerror(-ret
));
2436 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2438 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2440 chr
->avail_connections
= 1;
2441 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2445 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2448 CharDriverState
*chr
;
2451 if (strstart(filename
, "chardev:", &p
)) {
2452 return qemu_chr_find(p
);
2455 opts
= qemu_chr_parse_compat(label
, filename
);
2459 chr
= qemu_chr_new_from_opts(opts
, init
);
2460 qemu_opts_del(opts
);
2464 void qemu_chr_fe_open(struct CharDriverState
*chr
)
2468 if (chr
->chr_guest_open
) {
2469 chr
->chr_guest_open(chr
);
2473 void qemu_chr_fe_close(struct CharDriverState
*chr
)
2475 if (chr
->chr_guest_close
) {
2476 chr
->chr_guest_close(chr
);
2482 void qemu_chr_fe_delete(CharDriverState
*chr
)
2484 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2486 chr
->chr_close(chr
);
2487 qemu_free(chr
->filename
);
2488 qemu_free(chr
->label
);
2492 static void qemu_chr_qlist_iter(QObject
*obj
, void *opaque
)
2495 Monitor
*mon
= opaque
;
2497 chr_dict
= qobject_to_qdict(obj
);
2498 monitor_printf(mon
, "%s: filename=%s\n", qdict_get_str(chr_dict
, "label"),
2499 qdict_get_str(chr_dict
, "filename"));
2502 void qemu_chr_info_print(Monitor
*mon
, const QObject
*ret_data
)
2504 qlist_iter(qobject_to_qlist(ret_data
), qemu_chr_qlist_iter
, mon
);
2507 void qemu_chr_info(Monitor
*mon
, QObject
**ret_data
)
2510 CharDriverState
*chr
;
2512 chr_list
= qlist_new();
2514 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2515 QObject
*obj
= qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2516 chr
->label
, chr
->filename
);
2517 qlist_append_obj(chr_list
, obj
);
2520 *ret_data
= QOBJECT(chr_list
);
2523 CharDriverState
*qemu_chr_find(const char *name
)
2525 CharDriverState
*chr
;
2527 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2528 if (strcmp(chr
->label
, name
) != 0)