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 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
332 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
335 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
337 char buf
[READ_BUF_LEN
];
340 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
341 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
345 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
350 static int qemu_chr_open_null(QemuOpts
*opts
, CharDriverState
**_chr
)
352 CharDriverState
*chr
;
354 chr
= qemu_mallocz(sizeof(CharDriverState
));
355 chr
->chr_write
= null_chr_write
;
362 int send_all(int fd
, const void *buf
, int len1
)
368 ret
= send(fd
, buf
, len
, 0);
370 errno
= WSAGetLastError();
371 if (errno
!= WSAEWOULDBLOCK
) {
374 } else if (ret
== 0) {
386 int send_all(int fd
, const void *_buf
, int len1
)
389 const uint8_t *buf
= _buf
;
393 ret
= write(fd
, buf
, len
);
395 if (errno
!= EINTR
&& errno
!= EAGAIN
)
397 } else if (ret
== 0) {
415 #define STDIO_MAX_CLIENTS 1
416 static int stdio_nb_clients
= 0;
418 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
420 FDCharDriver
*s
= chr
->opaque
;
421 return send_all(s
->fd_out
, buf
, len
);
424 static int fd_chr_read_poll(void *opaque
)
426 CharDriverState
*chr
= opaque
;
427 FDCharDriver
*s
= chr
->opaque
;
429 s
->max_size
= qemu_chr_be_can_write(chr
);
433 static void fd_chr_read(void *opaque
)
435 CharDriverState
*chr
= opaque
;
436 FDCharDriver
*s
= chr
->opaque
;
438 uint8_t buf
[READ_BUF_LEN
];
441 if (len
> s
->max_size
)
445 size
= read(s
->fd_in
, buf
, len
);
447 /* FD has been closed. Remove it from the active list. */
448 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
449 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
453 qemu_chr_be_write(chr
, buf
, size
);
457 static void fd_chr_update_read_handler(CharDriverState
*chr
)
459 FDCharDriver
*s
= chr
->opaque
;
462 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
464 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
465 fd_chr_read
, NULL
, chr
);
470 static void fd_chr_close(struct CharDriverState
*chr
)
472 FDCharDriver
*s
= chr
->opaque
;
475 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
477 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
482 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
485 /* open a character device to a unix fd */
486 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
488 CharDriverState
*chr
;
491 chr
= qemu_mallocz(sizeof(CharDriverState
));
492 s
= qemu_mallocz(sizeof(FDCharDriver
));
496 chr
->chr_write
= fd_chr_write
;
497 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
498 chr
->chr_close
= fd_chr_close
;
500 qemu_chr_generic_open(chr
);
505 static int qemu_chr_open_file_out(QemuOpts
*opts
, CharDriverState
**_chr
)
509 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
510 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
515 *_chr
= qemu_chr_open_fd(-1, fd_out
);
519 static int qemu_chr_open_pipe(QemuOpts
*opts
, CharDriverState
**_chr
)
522 char filename_in
[256], filename_out
[256];
523 const char *filename
= qemu_opt_get(opts
, "path");
525 if (filename
== NULL
) {
526 fprintf(stderr
, "chardev: pipe: no filename given\n");
530 snprintf(filename_in
, 256, "%s.in", filename
);
531 snprintf(filename_out
, 256, "%s.out", filename
);
532 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
533 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
534 if (fd_in
< 0 || fd_out
< 0) {
539 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
545 *_chr
= qemu_chr_open_fd(fd_in
, fd_out
);
550 /* for STDIO, we handle the case where several clients use it
553 #define TERM_FIFO_MAX_SIZE 1
555 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
556 static int term_fifo_size
;
558 static int stdio_read_poll(void *opaque
)
560 CharDriverState
*chr
= opaque
;
562 /* try to flush the queue if needed */
563 if (term_fifo_size
!= 0 && qemu_chr_be_can_write(chr
) > 0) {
564 qemu_chr_be_write(chr
, term_fifo
, 1);
567 /* see if we can absorb more chars */
568 if (term_fifo_size
== 0)
574 static void stdio_read(void *opaque
)
578 CharDriverState
*chr
= opaque
;
580 size
= read(0, buf
, 1);
582 /* stdin has been closed. Remove it from the active list. */
583 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
584 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
588 if (qemu_chr_be_can_write(chr
) > 0) {
589 qemu_chr_be_write(chr
, buf
, 1);
590 } else if (term_fifo_size
== 0) {
591 term_fifo
[term_fifo_size
++] = buf
[0];
596 /* init terminal so that we can grab keys */
597 static struct termios oldtty
;
598 static int old_fd0_flags
;
599 static bool stdio_allow_signal
;
601 static void term_exit(void)
603 tcsetattr (0, TCSANOW
, &oldtty
);
604 fcntl(0, F_SETFL
, old_fd0_flags
);
607 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
613 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
614 |INLCR
|IGNCR
|ICRNL
|IXON
);
615 tty
.c_oflag
|= OPOST
;
616 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
617 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
622 /* if graphical mode, we allow Ctrl-C handling */
623 if (!stdio_allow_signal
)
624 tty
.c_lflag
&= ~ISIG
;
626 tcsetattr (0, TCSANOW
, &tty
);
629 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
633 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
637 static int qemu_chr_open_stdio(QemuOpts
*opts
, CharDriverState
**_chr
)
639 CharDriverState
*chr
;
641 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
) {
645 if (stdio_nb_clients
== 0) {
646 old_fd0_flags
= fcntl(0, F_GETFL
);
647 tcgetattr (0, &oldtty
);
648 fcntl(0, F_SETFL
, O_NONBLOCK
);
652 chr
= qemu_chr_open_fd(0, 1);
653 chr
->chr_close
= qemu_chr_close_stdio
;
654 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
655 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
657 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
658 display_type
!= DT_NOGRAPHIC
);
659 qemu_chr_set_echo(chr
, false);
666 /* Once Solaris has openpty(), this is going to be removed. */
667 static int openpty(int *amaster
, int *aslave
, char *name
,
668 struct termios
*termp
, struct winsize
*winp
)
671 int mfd
= -1, sfd
= -1;
673 *amaster
= *aslave
= -1;
675 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
679 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
682 if ((slave
= ptsname(mfd
)) == NULL
)
685 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
688 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
689 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
697 ioctl(sfd
, TIOCSWINSZ
, winp
);
708 static void cfmakeraw (struct termios
*termios_p
)
710 termios_p
->c_iflag
&=
711 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
712 termios_p
->c_oflag
&= ~OPOST
;
713 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
714 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
715 termios_p
->c_cflag
|= CS8
;
717 termios_p
->c_cc
[VMIN
] = 0;
718 termios_p
->c_cc
[VTIME
] = 0;
722 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
723 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
724 || defined(__GLIBC__)
734 static void pty_chr_update_read_handler(CharDriverState
*chr
);
735 static void pty_chr_state(CharDriverState
*chr
, int connected
);
737 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
739 PtyCharDriver
*s
= chr
->opaque
;
742 /* guest sends data, check for (re-)connect */
743 pty_chr_update_read_handler(chr
);
746 return send_all(s
->fd
, buf
, len
);
749 static int pty_chr_read_poll(void *opaque
)
751 CharDriverState
*chr
= opaque
;
752 PtyCharDriver
*s
= chr
->opaque
;
754 s
->read_bytes
= qemu_chr_be_can_write(chr
);
755 return s
->read_bytes
;
758 static void pty_chr_read(void *opaque
)
760 CharDriverState
*chr
= opaque
;
761 PtyCharDriver
*s
= chr
->opaque
;
763 uint8_t buf
[READ_BUF_LEN
];
766 if (len
> s
->read_bytes
)
770 size
= read(s
->fd
, buf
, len
);
771 if ((size
== -1 && errno
== EIO
) ||
773 pty_chr_state(chr
, 0);
777 pty_chr_state(chr
, 1);
778 qemu_chr_be_write(chr
, buf
, size
);
782 static void pty_chr_update_read_handler(CharDriverState
*chr
)
784 PtyCharDriver
*s
= chr
->opaque
;
786 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
787 pty_chr_read
, NULL
, chr
);
790 * Short timeout here: just need wait long enougth that qemu makes
791 * it through the poll loop once. When reconnected we want a
792 * short timeout so we notice it almost instantly. Otherwise
793 * read() gives us -EIO instantly, making pty_chr_state() reset the
794 * timeout to the normal (much longer) poll interval before the
797 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
800 static void pty_chr_state(CharDriverState
*chr
, int connected
)
802 PtyCharDriver
*s
= chr
->opaque
;
805 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
808 /* (re-)connect poll interval for idle guests: once per second.
809 * We check more frequently in case the guests sends data to
810 * the virtual device linked to our pty. */
811 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
814 qemu_chr_generic_open(chr
);
819 static void pty_chr_timer(void *opaque
)
821 struct CharDriverState
*chr
= opaque
;
822 PtyCharDriver
*s
= chr
->opaque
;
827 /* If we arrive here without polling being cleared due
828 * read returning -EIO, then we are (re-)connected */
829 pty_chr_state(chr
, 1);
834 pty_chr_update_read_handler(chr
);
837 static void pty_chr_close(struct CharDriverState
*chr
)
839 PtyCharDriver
*s
= chr
->opaque
;
841 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
843 qemu_del_timer(s
->timer
);
844 qemu_free_timer(s
->timer
);
846 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
849 static int qemu_chr_open_pty(QemuOpts
*opts
, CharDriverState
**_chr
)
851 CharDriverState
*chr
;
855 #if defined(__OpenBSD__) || defined(__DragonFly__)
856 char pty_name
[PATH_MAX
];
857 #define q_ptsname(x) pty_name
859 char *pty_name
= NULL
;
860 #define q_ptsname(x) ptsname(x)
863 chr
= qemu_mallocz(sizeof(CharDriverState
));
864 s
= qemu_mallocz(sizeof(PtyCharDriver
));
866 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
870 /* Set raw attributes on the pty. */
871 tcgetattr(slave_fd
, &tty
);
873 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
876 len
= strlen(q_ptsname(s
->fd
)) + 5;
877 chr
->filename
= qemu_malloc(len
);
878 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
879 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
880 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
883 chr
->chr_write
= pty_chr_write
;
884 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
885 chr
->chr_close
= pty_chr_close
;
887 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
893 static void tty_serial_init(int fd
, int speed
,
894 int parity
, int data_bits
, int stop_bits
)
900 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
901 speed
, parity
, data_bits
, stop_bits
);
903 tcgetattr (fd
, &tty
);
905 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
906 speed
= speed
* 10 / 11;
923 /* Non-Posix values follow. They may be unsupported on some systems. */
942 check_speed(1000000);
945 check_speed(1152000);
948 check_speed(1500000);
951 check_speed(2000000);
954 check_speed(2500000);
957 check_speed(3000000);
960 check_speed(3500000);
963 check_speed(4000000);
968 cfsetispeed(&tty
, spd
);
969 cfsetospeed(&tty
, spd
);
971 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
972 |INLCR
|IGNCR
|ICRNL
|IXON
);
973 tty
.c_oflag
|= OPOST
;
974 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
975 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
996 tty
.c_cflag
|= PARENB
;
999 tty
.c_cflag
|= PARENB
| PARODD
;
1003 tty
.c_cflag
|= CSTOPB
;
1005 tcsetattr (fd
, TCSANOW
, &tty
);
1008 static int tty_serial_ioctl(void *opaque
, int cmd
, void *arg
)
1010 FDCharDriver
*s
= opaque
;
1013 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1015 QEMUSerialSetParams
*ssp
= arg
;
1016 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1017 ssp
->data_bits
, ssp
->stop_bits
);
1020 case CHR_IOCTL_SERIAL_SET_BREAK
:
1022 int enable
= *(int *)arg
;
1024 tcsendbreak(s
->fd_in
, 1);
1027 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1030 int *targ
= (int *)arg
;
1031 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1033 if (sarg
& TIOCM_CTS
)
1034 *targ
|= CHR_TIOCM_CTS
;
1035 if (sarg
& TIOCM_CAR
)
1036 *targ
|= CHR_TIOCM_CAR
;
1037 if (sarg
& TIOCM_DSR
)
1038 *targ
|= CHR_TIOCM_DSR
;
1039 if (sarg
& TIOCM_RI
)
1040 *targ
|= CHR_TIOCM_RI
;
1041 if (sarg
& TIOCM_DTR
)
1042 *targ
|= CHR_TIOCM_DTR
;
1043 if (sarg
& TIOCM_RTS
)
1044 *targ
|= CHR_TIOCM_RTS
;
1047 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1049 int sarg
= *(int *)arg
;
1051 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1052 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1053 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1054 if (sarg
& CHR_TIOCM_CTS
)
1056 if (sarg
& CHR_TIOCM_CAR
)
1058 if (sarg
& CHR_TIOCM_DSR
)
1060 if (sarg
& CHR_TIOCM_RI
)
1062 if (sarg
& CHR_TIOCM_DTR
)
1064 if (sarg
& CHR_TIOCM_RTS
)
1066 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1075 static void qemu_chr_close_tty(CharDriverState
*chr
)
1077 FDCharDriver
*s
= chr
->opaque
;
1091 static int qemu_chr_open_tty(QemuOpts
*opts
, CharDriverState
**_chr
)
1093 const char *filename
= qemu_opt_get(opts
, "path");
1094 CharDriverState
*chr
;
1097 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1101 tty_serial_init(fd
, 115200, 'N', 8, 1);
1102 chr
= qemu_chr_open_fd(fd
, fd
);
1103 chr
->chr_ioctl
= tty_serial_ioctl
;
1104 chr
->chr_close
= qemu_chr_close_tty
;
1109 #else /* ! __linux__ && ! __sun__ */
1110 static int qemu_chr_open_pty(QemuOpts
*opts
, CharDriverState
**_chr
)
1114 #endif /* __linux__ || __sun__ */
1116 #if defined(__linux__)
1120 } ParallelCharDriver
;
1122 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1124 if (s
->mode
!= mode
) {
1126 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1133 static int pp_ioctl(void *opaque
, int cmd
, void *arg
)
1135 ParallelCharDriver
*drv
= opaque
;
1140 case CHR_IOCTL_PP_READ_DATA
:
1141 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1143 *(uint8_t *)arg
= b
;
1145 case CHR_IOCTL_PP_WRITE_DATA
:
1146 b
= *(uint8_t *)arg
;
1147 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1150 case CHR_IOCTL_PP_READ_CONTROL
:
1151 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1153 /* Linux gives only the lowest bits, and no way to know data
1154 direction! For better compatibility set the fixed upper
1156 *(uint8_t *)arg
= b
| 0xc0;
1158 case CHR_IOCTL_PP_WRITE_CONTROL
:
1159 b
= *(uint8_t *)arg
;
1160 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1163 case CHR_IOCTL_PP_READ_STATUS
:
1164 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1166 *(uint8_t *)arg
= b
;
1168 case CHR_IOCTL_PP_DATA_DIR
:
1169 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1172 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1173 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1174 struct ParallelIOArg
*parg
= arg
;
1175 int n
= read(fd
, parg
->buffer
, parg
->count
);
1176 if (n
!= parg
->count
) {
1181 case CHR_IOCTL_PP_EPP_READ
:
1182 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1183 struct ParallelIOArg
*parg
= arg
;
1184 int n
= read(fd
, parg
->buffer
, parg
->count
);
1185 if (n
!= parg
->count
) {
1190 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1191 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1192 struct ParallelIOArg
*parg
= arg
;
1193 int n
= write(fd
, parg
->buffer
, parg
->count
);
1194 if (n
!= parg
->count
) {
1199 case CHR_IOCTL_PP_EPP_WRITE
:
1200 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1201 struct ParallelIOArg
*parg
= arg
;
1202 int n
= write(fd
, parg
->buffer
, parg
->count
);
1203 if (n
!= parg
->count
) {
1214 static void pp_close(CharDriverState
*chr
)
1216 ParallelCharDriver
*drv
= chr
->opaque
;
1219 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1220 ioctl(fd
, PPRELEASE
);
1223 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1226 static int qemu_chr_open_pp(QemuOpts
*opts
, CharDriverState
**_chr
)
1228 const char *filename
= qemu_opt_get(opts
, "path");
1229 CharDriverState
*chr
;
1230 ParallelCharDriver
*drv
;
1233 TFR(fd
= open(filename
, O_RDWR
));
1238 if (ioctl(fd
, PPCLAIM
) < 0) {
1243 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1245 drv
->mode
= IEEE1284_MODE_COMPAT
;
1247 chr
= qemu_mallocz(sizeof(CharDriverState
));
1248 chr
->chr_write
= null_chr_write
;
1249 chr
->chr_ioctl
= pp_ioctl
;
1250 chr
->chr_close
= pp_close
;
1253 qemu_chr_generic_open(chr
);
1258 #endif /* __linux__ */
1260 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1261 static int pp_ioctl(void *opaque
, int cmd
, void *arg
)
1263 int fd
= (int)(intptr_t)opaque
;
1267 case CHR_IOCTL_PP_READ_DATA
:
1268 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1270 *(uint8_t *)arg
= b
;
1272 case CHR_IOCTL_PP_WRITE_DATA
:
1273 b
= *(uint8_t *)arg
;
1274 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1277 case CHR_IOCTL_PP_READ_CONTROL
:
1278 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1280 *(uint8_t *)arg
= b
;
1282 case CHR_IOCTL_PP_WRITE_CONTROL
:
1283 b
= *(uint8_t *)arg
;
1284 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1287 case CHR_IOCTL_PP_READ_STATUS
:
1288 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1290 *(uint8_t *)arg
= b
;
1298 static int qemu_chr_open_pp(QemuOpts
*opts
, CharDriverState
**_chr
)
1300 const char *filename
= qemu_opt_get(opts
, "path");
1301 CharDriverState
*chr
;
1304 fd
= qemu_open(filename
, O_RDWR
);
1309 chr
= qemu_mallocz(sizeof(CharDriverState
));
1310 chr
->opaque
= (void *)(intptr_t)fd
;
1311 chr
->chr_write
= null_chr_write
;
1312 chr
->chr_ioctl
= pp_ioctl
;
1323 HANDLE hcom
, hrecv
, hsend
;
1324 OVERLAPPED orecv
, osend
;
1329 #define NSENDBUF 2048
1330 #define NRECVBUF 2048
1331 #define MAXCONNECT 1
1332 #define NTIMEOUT 5000
1334 static int win_chr_poll(void *opaque
);
1335 static int win_chr_pipe_poll(void *opaque
);
1337 static void win_chr_close(CharDriverState
*chr
)
1339 WinCharState
*s
= chr
->opaque
;
1342 CloseHandle(s
->hsend
);
1346 CloseHandle(s
->hrecv
);
1350 CloseHandle(s
->hcom
);
1354 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1356 qemu_del_polling_cb(win_chr_poll
, chr
);
1358 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1361 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1363 WinCharState
*s
= chr
->opaque
;
1365 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1370 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1372 fprintf(stderr
, "Failed CreateEvent\n");
1375 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1377 fprintf(stderr
, "Failed CreateEvent\n");
1381 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1382 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1383 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1384 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1389 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1390 fprintf(stderr
, "Failed SetupComm\n");
1394 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1395 size
= sizeof(COMMCONFIG
);
1396 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1397 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1398 CommConfigDialog(filename
, NULL
, &comcfg
);
1400 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1401 fprintf(stderr
, "Failed SetCommState\n");
1405 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1406 fprintf(stderr
, "Failed SetCommMask\n");
1410 cto
.ReadIntervalTimeout
= MAXDWORD
;
1411 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1412 fprintf(stderr
, "Failed SetCommTimeouts\n");
1416 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1417 fprintf(stderr
, "Failed ClearCommError\n");
1420 qemu_add_polling_cb(win_chr_poll
, chr
);
1428 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1430 WinCharState
*s
= chr
->opaque
;
1431 DWORD len
, ret
, size
, err
;
1434 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1435 s
->osend
.hEvent
= s
->hsend
;
1438 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1440 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1442 err
= GetLastError();
1443 if (err
== ERROR_IO_PENDING
) {
1444 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1462 static int win_chr_read_poll(CharDriverState
*chr
)
1464 WinCharState
*s
= chr
->opaque
;
1466 s
->max_size
= qemu_chr_be_can_write(chr
);
1470 static void win_chr_readfile(CharDriverState
*chr
)
1472 WinCharState
*s
= chr
->opaque
;
1474 uint8_t buf
[READ_BUF_LEN
];
1477 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1478 s
->orecv
.hEvent
= s
->hrecv
;
1479 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1481 err
= GetLastError();
1482 if (err
== ERROR_IO_PENDING
) {
1483 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1488 qemu_chr_be_write(chr
, buf
, size
);
1492 static void win_chr_read(CharDriverState
*chr
)
1494 WinCharState
*s
= chr
->opaque
;
1496 if (s
->len
> s
->max_size
)
1497 s
->len
= s
->max_size
;
1501 win_chr_readfile(chr
);
1504 static int win_chr_poll(void *opaque
)
1506 CharDriverState
*chr
= opaque
;
1507 WinCharState
*s
= chr
->opaque
;
1511 ClearCommError(s
->hcom
, &comerr
, &status
);
1512 if (status
.cbInQue
> 0) {
1513 s
->len
= status
.cbInQue
;
1514 win_chr_read_poll(chr
);
1521 static int qemu_chr_open_win(QemuOpts
*opts
, CharDriverState
**_chr
)
1523 const char *filename
= qemu_opt_get(opts
, "path");
1524 CharDriverState
*chr
;
1527 chr
= qemu_mallocz(sizeof(CharDriverState
));
1528 s
= qemu_mallocz(sizeof(WinCharState
));
1530 chr
->chr_write
= win_chr_write
;
1531 chr
->chr_close
= win_chr_close
;
1533 if (win_chr_init(chr
, filename
) < 0) {
1538 qemu_chr_generic_open(chr
);
1544 static int win_chr_pipe_poll(void *opaque
)
1546 CharDriverState
*chr
= opaque
;
1547 WinCharState
*s
= chr
->opaque
;
1550 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1553 win_chr_read_poll(chr
);
1560 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1562 WinCharState
*s
= chr
->opaque
;
1570 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1572 fprintf(stderr
, "Failed CreateEvent\n");
1575 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1577 fprintf(stderr
, "Failed CreateEvent\n");
1581 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1582 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1583 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1585 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1586 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1587 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1592 ZeroMemory(&ov
, sizeof(ov
));
1593 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1594 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1596 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1600 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1602 fprintf(stderr
, "Failed GetOverlappedResult\n");
1604 CloseHandle(ov
.hEvent
);
1611 CloseHandle(ov
.hEvent
);
1614 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1623 static int qemu_chr_open_win_pipe(QemuOpts
*opts
, CharDriverState
**_chr
)
1625 const char *filename
= qemu_opt_get(opts
, "path");
1626 CharDriverState
*chr
;
1629 chr
= qemu_mallocz(sizeof(CharDriverState
));
1630 s
= qemu_mallocz(sizeof(WinCharState
));
1632 chr
->chr_write
= win_chr_write
;
1633 chr
->chr_close
= win_chr_close
;
1635 if (win_chr_pipe_init(chr
, filename
) < 0) {
1640 qemu_chr_generic_open(chr
);
1646 static int qemu_chr_open_win_file(HANDLE fd_out
, CharDriverState
**pchr
)
1648 CharDriverState
*chr
;
1651 chr
= qemu_mallocz(sizeof(CharDriverState
));
1652 s
= qemu_mallocz(sizeof(WinCharState
));
1655 chr
->chr_write
= win_chr_write
;
1656 qemu_chr_generic_open(chr
);
1661 static int qemu_chr_open_win_con(QemuOpts
*opts
, CharDriverState
**chr
)
1663 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
), chr
);
1666 static int qemu_chr_open_win_file_out(QemuOpts
*opts
, CharDriverState
**_chr
)
1668 const char *file_out
= qemu_opt_get(opts
, "path");
1671 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1672 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1673 if (fd_out
== INVALID_HANDLE_VALUE
) {
1677 return qemu_chr_open_win_file(fd_out
, _chr
);
1679 #endif /* !_WIN32 */
1681 /***********************************************************/
1682 /* UDP Net console */
1686 uint8_t buf
[READ_BUF_LEN
];
1692 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1694 NetCharDriver
*s
= chr
->opaque
;
1696 return send(s
->fd
, (const void *)buf
, len
, 0);
1699 static int udp_chr_read_poll(void *opaque
)
1701 CharDriverState
*chr
= opaque
;
1702 NetCharDriver
*s
= chr
->opaque
;
1704 s
->max_size
= qemu_chr_be_can_write(chr
);
1706 /* If there were any stray characters in the queue process them
1709 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1710 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
1712 s
->max_size
= qemu_chr_be_can_write(chr
);
1717 static void udp_chr_read(void *opaque
)
1719 CharDriverState
*chr
= opaque
;
1720 NetCharDriver
*s
= chr
->opaque
;
1722 if (s
->max_size
== 0)
1724 s
->bufcnt
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1725 s
->bufptr
= s
->bufcnt
;
1730 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1731 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
1733 s
->max_size
= qemu_chr_be_can_write(chr
);
1737 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1739 NetCharDriver
*s
= chr
->opaque
;
1742 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1743 udp_chr_read
, NULL
, chr
);
1747 static void udp_chr_close(CharDriverState
*chr
)
1749 NetCharDriver
*s
= chr
->opaque
;
1751 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1755 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1758 static int qemu_chr_open_udp(QemuOpts
*opts
, CharDriverState
**_chr
)
1760 CharDriverState
*chr
= NULL
;
1761 NetCharDriver
*s
= NULL
;
1765 chr
= qemu_mallocz(sizeof(CharDriverState
));
1766 s
= qemu_mallocz(sizeof(NetCharDriver
));
1768 fd
= inet_dgram_opts(opts
);
1770 fprintf(stderr
, "inet_dgram_opts failed\n");
1779 chr
->chr_write
= udp_chr_write
;
1780 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1781 chr
->chr_close
= udp_chr_close
;
1795 /***********************************************************/
1796 /* TCP Net console */
1808 static void tcp_chr_accept(void *opaque
);
1810 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1812 TCPCharDriver
*s
= chr
->opaque
;
1814 return send_all(s
->fd
, buf
, len
);
1816 /* XXX: indicate an error ? */
1821 static int tcp_chr_read_poll(void *opaque
)
1823 CharDriverState
*chr
= opaque
;
1824 TCPCharDriver
*s
= chr
->opaque
;
1827 s
->max_size
= qemu_chr_be_can_write(chr
);
1832 #define IAC_BREAK 243
1833 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1835 uint8_t *buf
, int *size
)
1837 /* Handle any telnet client's basic IAC options to satisfy char by
1838 * char mode with no echo. All IAC options will be removed from
1839 * the buf and the do_telnetopt variable will be used to track the
1840 * state of the width of the IAC information.
1842 * IAC commands come in sets of 3 bytes with the exception of the
1843 * "IAC BREAK" command and the double IAC.
1849 for (i
= 0; i
< *size
; i
++) {
1850 if (s
->do_telnetopt
> 1) {
1851 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1852 /* Double IAC means send an IAC */
1856 s
->do_telnetopt
= 1;
1858 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1859 /* Handle IAC break commands by sending a serial break */
1860 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1865 if (s
->do_telnetopt
>= 4) {
1866 s
->do_telnetopt
= 1;
1869 if ((unsigned char)buf
[i
] == IAC
) {
1870 s
->do_telnetopt
= 2;
1881 static int tcp_chr_ioctl(void *opaque
, int cmd
, void *data
)
1883 TCPCharDriver
*s
= opaque
;
1887 case CHR_GET_MSGFD
: {
1903 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1905 TCPCharDriver
*s
= chr
->opaque
;
1906 struct cmsghdr
*cmsg
;
1908 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1911 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1912 cmsg
->cmsg_level
!= SOL_SOCKET
||
1913 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1916 fd
= *((int *)CMSG_DATA(cmsg
));
1926 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1928 TCPCharDriver
*s
= chr
->opaque
;
1929 struct msghdr msg
= { NULL
, };
1930 struct iovec iov
[1];
1932 struct cmsghdr cmsg
;
1933 char control
[CMSG_SPACE(sizeof(int))];
1937 iov
[0].iov_base
= buf
;
1938 iov
[0].iov_len
= len
;
1942 msg
.msg_control
= &msg_control
;
1943 msg
.msg_controllen
= sizeof(msg_control
);
1945 ret
= recvmsg(s
->fd
, &msg
, 0);
1946 if (ret
> 0 && s
->is_unix
)
1947 unix_process_msgfd(chr
, &msg
);
1952 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1954 TCPCharDriver
*s
= chr
->opaque
;
1955 return qemu_recv(s
->fd
, buf
, len
, 0);
1959 static void tcp_chr_read(void *opaque
)
1961 CharDriverState
*chr
= opaque
;
1962 TCPCharDriver
*s
= chr
->opaque
;
1963 uint8_t buf
[READ_BUF_LEN
];
1966 if (!s
->connected
|| s
->max_size
<= 0)
1969 if (len
> s
->max_size
)
1971 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
1973 /* connection closed */
1975 if (s
->listen_fd
>= 0) {
1976 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1978 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1981 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1982 } else if (size
> 0) {
1983 if (s
->do_telnetopt
)
1984 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1986 qemu_chr_be_write(chr
, buf
, size
);
1991 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
1993 return qemu_chr_open_fd(eventfd
, eventfd
);
1997 static void tcp_chr_connect(void *opaque
)
1999 CharDriverState
*chr
= opaque
;
2000 TCPCharDriver
*s
= chr
->opaque
;
2003 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2004 tcp_chr_read
, NULL
, chr
);
2005 qemu_chr_generic_open(chr
);
2008 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2009 static void tcp_chr_telnet_init(int fd
)
2012 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2013 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2014 send(fd
, (char *)buf
, 3, 0);
2015 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2016 send(fd
, (char *)buf
, 3, 0);
2017 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2018 send(fd
, (char *)buf
, 3, 0);
2019 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2020 send(fd
, (char *)buf
, 3, 0);
2023 static void socket_set_nodelay(int fd
)
2026 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2029 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2031 TCPCharDriver
*s
= chr
->opaque
;
2035 socket_set_nonblock(fd
);
2037 socket_set_nodelay(fd
);
2039 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2040 tcp_chr_connect(chr
);
2045 static void tcp_chr_accept(void *opaque
)
2047 CharDriverState
*chr
= opaque
;
2048 TCPCharDriver
*s
= chr
->opaque
;
2049 struct sockaddr_in saddr
;
2051 struct sockaddr_un uaddr
;
2053 struct sockaddr
*addr
;
2060 len
= sizeof(uaddr
);
2061 addr
= (struct sockaddr
*)&uaddr
;
2065 len
= sizeof(saddr
);
2066 addr
= (struct sockaddr
*)&saddr
;
2068 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2069 if (fd
< 0 && errno
!= EINTR
) {
2071 } else if (fd
>= 0) {
2072 if (s
->do_telnetopt
)
2073 tcp_chr_telnet_init(fd
);
2077 if (tcp_chr_add_client(chr
, fd
) < 0)
2081 static void tcp_chr_close(CharDriverState
*chr
)
2083 TCPCharDriver
*s
= chr
->opaque
;
2085 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2088 if (s
->listen_fd
>= 0) {
2089 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2090 closesocket(s
->listen_fd
);
2093 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2096 static int qemu_chr_open_socket(QemuOpts
*opts
, CharDriverState
**_chr
)
2098 CharDriverState
*chr
= NULL
;
2099 TCPCharDriver
*s
= NULL
;
2108 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2109 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2110 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2111 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2112 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2116 chr
= qemu_mallocz(sizeof(CharDriverState
));
2117 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2121 fd
= unix_listen_opts(opts
);
2123 fd
= unix_connect_opts(opts
);
2127 fd
= inet_listen_opts(opts
, 0);
2129 fd
= inet_connect_opts(opts
);
2137 if (!is_waitconnect
)
2138 socket_set_nonblock(fd
);
2144 s
->is_unix
= is_unix
;
2145 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2148 chr
->chr_write
= tcp_chr_write
;
2149 chr
->chr_close
= tcp_chr_close
;
2150 chr
->chr_add_client
= tcp_chr_add_client
;
2151 chr
->chr_ioctl
= tcp_chr_ioctl
;
2155 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2157 s
->do_telnetopt
= 1;
2162 socket_set_nodelay(fd
);
2163 tcp_chr_connect(chr
);
2166 /* for "info chardev" monitor command */
2167 chr
->filename
= qemu_malloc(256);
2169 snprintf(chr
->filename
, 256, "unix:%s%s",
2170 qemu_opt_get(opts
, "path"),
2171 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2172 } else if (is_telnet
) {
2173 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2174 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2175 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2177 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2178 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2179 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2182 if (is_listen
&& is_waitconnect
) {
2183 printf("QEMU waiting for connection on: %s\n",
2185 tcp_chr_accept(chr
);
2186 socket_set_nonblock(s
->listen_fd
);
2200 /***********************************************************/
2201 /* Memory chardev */
2204 size_t outbuf_capacity
;
2208 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2210 MemoryDriver
*d
= chr
->opaque
;
2212 /* TODO: the QString implementation has the same code, we should
2213 * introduce a generic way to do this in cutils.c */
2214 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2216 d
->outbuf_capacity
+= len
;
2217 d
->outbuf_capacity
*= 2;
2218 d
->outbuf
= qemu_realloc(d
->outbuf
, d
->outbuf_capacity
);
2221 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2222 d
->outbuf_size
+= len
;
2227 void qemu_chr_init_mem(CharDriverState
*chr
)
2231 d
= qemu_malloc(sizeof(*d
));
2233 d
->outbuf_capacity
= 4096;
2234 d
->outbuf
= qemu_mallocz(d
->outbuf_capacity
);
2236 memset(chr
, 0, sizeof(*chr
));
2238 chr
->chr_write
= mem_chr_write
;
2241 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2243 MemoryDriver
*d
= chr
->opaque
;
2244 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2247 /* NOTE: this driver can not be closed with qemu_chr_close()! */
2248 void qemu_chr_close_mem(CharDriverState
*chr
)
2250 MemoryDriver
*d
= chr
->opaque
;
2252 qemu_free(d
->outbuf
);
2253 qemu_free(chr
->opaque
);
2255 chr
->chr_write
= NULL
;
2258 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2260 const MemoryDriver
*d
= chr
->opaque
;
2261 return d
->outbuf_size
;
2264 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2266 char host
[65], port
[33], width
[8], height
[8];
2271 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1);
2275 if (strcmp(filename
, "null") == 0 ||
2276 strcmp(filename
, "pty") == 0 ||
2277 strcmp(filename
, "msmouse") == 0 ||
2278 strcmp(filename
, "braille") == 0 ||
2279 strcmp(filename
, "stdio") == 0) {
2280 qemu_opt_set(opts
, "backend", filename
);
2283 if (strstart(filename
, "vc", &p
)) {
2284 qemu_opt_set(opts
, "backend", "vc");
2286 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2288 qemu_opt_set(opts
, "width", width
);
2289 qemu_opt_set(opts
, "height", height
);
2290 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2292 qemu_opt_set(opts
, "cols", width
);
2293 qemu_opt_set(opts
, "rows", height
);
2300 if (strcmp(filename
, "con:") == 0) {
2301 qemu_opt_set(opts
, "backend", "console");
2304 if (strstart(filename
, "COM", NULL
)) {
2305 qemu_opt_set(opts
, "backend", "serial");
2306 qemu_opt_set(opts
, "path", filename
);
2309 if (strstart(filename
, "file:", &p
)) {
2310 qemu_opt_set(opts
, "backend", "file");
2311 qemu_opt_set(opts
, "path", p
);
2314 if (strstart(filename
, "pipe:", &p
)) {
2315 qemu_opt_set(opts
, "backend", "pipe");
2316 qemu_opt_set(opts
, "path", p
);
2319 if (strstart(filename
, "tcp:", &p
) ||
2320 strstart(filename
, "telnet:", &p
)) {
2321 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2323 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2326 qemu_opt_set(opts
, "backend", "socket");
2327 qemu_opt_set(opts
, "host", host
);
2328 qemu_opt_set(opts
, "port", port
);
2329 if (p
[pos
] == ',') {
2330 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2333 if (strstart(filename
, "telnet:", &p
))
2334 qemu_opt_set(opts
, "telnet", "on");
2337 if (strstart(filename
, "udp:", &p
)) {
2338 qemu_opt_set(opts
, "backend", "udp");
2339 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2341 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2345 qemu_opt_set(opts
, "host", host
);
2346 qemu_opt_set(opts
, "port", port
);
2347 if (p
[pos
] == '@') {
2349 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2351 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2355 qemu_opt_set(opts
, "localaddr", host
);
2356 qemu_opt_set(opts
, "localport", port
);
2360 if (strstart(filename
, "unix:", &p
)) {
2361 qemu_opt_set(opts
, "backend", "socket");
2362 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2366 if (strstart(filename
, "/dev/parport", NULL
) ||
2367 strstart(filename
, "/dev/ppi", NULL
)) {
2368 qemu_opt_set(opts
, "backend", "parport");
2369 qemu_opt_set(opts
, "path", filename
);
2372 if (strstart(filename
, "/dev/", NULL
)) {
2373 qemu_opt_set(opts
, "backend", "tty");
2374 qemu_opt_set(opts
, "path", filename
);
2379 qemu_opts_del(opts
);
2383 static const struct {
2385 int (*open
)(QemuOpts
*opts
, CharDriverState
**chr
);
2386 } backend_table
[] = {
2387 { .name
= "null", .open
= qemu_chr_open_null
},
2388 { .name
= "socket", .open
= qemu_chr_open_socket
},
2389 { .name
= "udp", .open
= qemu_chr_open_udp
},
2390 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2391 { .name
= "vc", .open
= text_console_init
},
2393 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2394 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2395 { .name
= "console", .open
= qemu_chr_open_win_con
},
2396 { .name
= "serial", .open
= qemu_chr_open_win
},
2398 { .name
= "file", .open
= qemu_chr_open_file_out
},
2399 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2400 { .name
= "pty", .open
= qemu_chr_open_pty
},
2401 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2403 #ifdef CONFIG_BRLAPI
2404 { .name
= "braille", .open
= chr_baum_init
},
2406 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2407 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2408 || defined(__FreeBSD_kernel__)
2409 { .name
= "tty", .open
= qemu_chr_open_tty
},
2411 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2412 || defined(__FreeBSD_kernel__)
2413 { .name
= "parport", .open
= qemu_chr_open_pp
},
2416 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
2420 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2421 void (*init
)(struct CharDriverState
*s
))
2423 CharDriverState
*chr
;
2427 if (qemu_opts_id(opts
) == NULL
) {
2428 fprintf(stderr
, "chardev: no id specified\n");
2432 if (qemu_opt_get(opts
, "backend") == NULL
) {
2433 fprintf(stderr
, "chardev: \"%s\" missing backend\n",
2434 qemu_opts_id(opts
));
2437 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2438 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2441 if (i
== ARRAY_SIZE(backend_table
)) {
2442 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2443 qemu_opt_get(opts
, "backend"));
2447 ret
= backend_table
[i
].open(opts
, &chr
);
2449 fprintf(stderr
, "chardev: opening backend \"%s\" failed: %s\n",
2450 qemu_opt_get(opts
, "backend"), strerror(-ret
));
2455 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2457 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2459 chr
->avail_connections
= 1;
2460 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2464 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2467 CharDriverState
*chr
;
2470 if (strstart(filename
, "chardev:", &p
)) {
2471 return qemu_chr_find(p
);
2474 opts
= qemu_chr_parse_compat(label
, filename
);
2478 chr
= qemu_chr_open_opts(opts
, init
);
2479 qemu_opts_del(opts
);
2483 void qemu_chr_set_echo(struct CharDriverState
*chr
, bool echo
)
2485 if (chr
->chr_set_echo
) {
2486 chr
->chr_set_echo(chr
, echo
);
2490 void qemu_chr_fe_open(struct CharDriverState
*chr
)
2494 if (chr
->chr_guest_open
) {
2495 chr
->chr_guest_open(chr
);
2499 void qemu_chr_fe_close(struct CharDriverState
*chr
)
2501 if (chr
->chr_guest_close
) {
2502 chr
->chr_guest_close(chr
);
2508 void qemu_chr_close(CharDriverState
*chr
)
2510 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2512 chr
->chr_close(chr
);
2513 qemu_free(chr
->filename
);
2514 qemu_free(chr
->label
);
2518 static void qemu_chr_qlist_iter(QObject
*obj
, void *opaque
)
2521 Monitor
*mon
= opaque
;
2523 chr_dict
= qobject_to_qdict(obj
);
2524 monitor_printf(mon
, "%s: filename=%s\n", qdict_get_str(chr_dict
, "label"),
2525 qdict_get_str(chr_dict
, "filename"));
2528 void qemu_chr_info_print(Monitor
*mon
, const QObject
*ret_data
)
2530 qlist_iter(qobject_to_qlist(ret_data
), qemu_chr_qlist_iter
, mon
);
2533 void qemu_chr_info(Monitor
*mon
, QObject
**ret_data
)
2536 CharDriverState
*chr
;
2538 chr_list
= qlist_new();
2540 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2541 QObject
*obj
= qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2542 chr
->label
, chr
->filename
);
2543 qlist_append_obj(chr_list
, obj
);
2546 *ret_data
= QOBJECT(chr_list
);
2549 CharDriverState
*qemu_chr_find(const char *name
)
2551 CharDriverState
*chr
;
2553 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2554 if (strcmp(chr
->label
, name
) != 0)