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"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
33 #include "hw/msmouse.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>
54 #include <net/if_tap.h>
57 #include <linux/if_tun.h>
59 #include <arpa/inet.h>
62 #include <sys/select.h>
67 #include <dev/ppbus/ppi.h>
68 #include <dev/ppbus/ppbconf.h>
72 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
73 #include <freebsd/stdlib.h>
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
99 #include "qemu_socket.h"
101 /***********************************************************/
102 /* character device */
104 static void qemu_chr_event(CharDriverState
*s
, int event
)
108 s
->chr_event(s
->handler_opaque
, event
);
111 static void qemu_chr_reset_bh(void *opaque
)
113 CharDriverState
*s
= opaque
;
114 qemu_chr_event(s
, CHR_EVENT_RESET
);
115 qemu_bh_delete(s
->bh
);
119 void qemu_chr_reset(CharDriverState
*s
)
122 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
123 qemu_bh_schedule(s
->bh
);
127 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
129 return s
->chr_write(s
, buf
, len
);
132 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
136 return s
->chr_ioctl(s
, cmd
, arg
);
139 int qemu_chr_can_read(CharDriverState
*s
)
141 if (!s
->chr_can_read
)
143 return s
->chr_can_read(s
->handler_opaque
);
146 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
148 s
->chr_read(s
->handler_opaque
, buf
, len
);
151 void qemu_chr_accept_input(CharDriverState
*s
)
153 if (s
->chr_accept_input
)
154 s
->chr_accept_input(s
);
157 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
162 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
163 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
167 void qemu_chr_send_event(CharDriverState
*s
, int event
)
169 if (s
->chr_send_event
)
170 s
->chr_send_event(s
, event
);
173 void qemu_chr_add_handlers(CharDriverState
*s
,
174 IOCanRWHandler
*fd_can_read
,
175 IOReadHandler
*fd_read
,
176 IOEventHandler
*fd_event
,
179 s
->chr_can_read
= fd_can_read
;
180 s
->chr_read
= fd_read
;
181 s
->chr_event
= fd_event
;
182 s
->handler_opaque
= opaque
;
183 if (s
->chr_update_read_handler
)
184 s
->chr_update_read_handler(s
);
187 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
192 static CharDriverState
*qemu_chr_open_null(void)
194 CharDriverState
*chr
;
196 chr
= qemu_mallocz(sizeof(CharDriverState
));
197 chr
->chr_write
= null_chr_write
;
201 /* MUX driver for serial I/O splitting */
202 static int term_timestamps
;
203 static int64_t term_timestamps_start
;
205 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
206 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
208 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
209 IOReadHandler
*chr_read
[MAX_MUX
];
210 IOEventHandler
*chr_event
[MAX_MUX
];
211 void *ext_opaque
[MAX_MUX
];
212 CharDriverState
*drv
;
213 unsigned char buffer
[MUX_BUFFER_SIZE
];
222 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
224 MuxDriver
*d
= chr
->opaque
;
226 if (!term_timestamps
) {
227 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
232 for(i
= 0; i
< len
; i
++) {
233 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
234 if (buf
[i
] == '\n') {
239 ti
= qemu_get_clock(rt_clock
);
240 if (term_timestamps_start
== -1)
241 term_timestamps_start
= ti
;
242 ti
-= term_timestamps_start
;
244 snprintf(buf1
, sizeof(buf1
),
245 "[%02d:%02d:%02d.%03d] ",
250 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
257 static const char * const mux_help
[] = {
258 "% h print this help\n\r",
259 "% x exit emulator\n\r",
260 "% s save disk data back to file (if -snapshot)\n\r",
261 "% t toggle console timestamps\n\r"
262 "% b send break (magic sysrq)\n\r",
263 "% c switch between console and monitor\n\r",
268 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
269 static void mux_print_help(CharDriverState
*chr
)
272 char ebuf
[15] = "Escape-Char";
273 char cbuf
[50] = "\n\r";
275 if (term_escape_char
> 0 && term_escape_char
< 26) {
276 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
277 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
279 snprintf(cbuf
, sizeof(cbuf
),
280 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
283 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
284 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
285 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
286 if (mux_help
[i
][j
] == '%')
287 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
289 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
294 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
296 if (d
->term_got_escape
) {
297 d
->term_got_escape
= 0;
298 if (ch
== term_escape_char
)
307 const char *term
= "QEMU: Terminated\n\r";
308 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
315 for (i
= 0; i
< nb_drives
; i
++) {
316 bdrv_commit(drives_table
[i
].bdrv
);
321 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
324 /* Switch to the next registered device */
326 if (chr
->focus
>= d
->mux_cnt
)
330 term_timestamps
= !term_timestamps
;
331 term_timestamps_start
= -1;
334 } else if (ch
== term_escape_char
) {
335 d
->term_got_escape
= 1;
343 static void mux_chr_accept_input(CharDriverState
*chr
)
346 MuxDriver
*d
= chr
->opaque
;
348 while (d
->prod
!= d
->cons
&&
349 d
->chr_can_read
[m
] &&
350 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
351 d
->chr_read
[m
](d
->ext_opaque
[m
],
352 &d
->buffer
[d
->cons
++ & MUX_BUFFER_MASK
], 1);
356 static int mux_chr_can_read(void *opaque
)
358 CharDriverState
*chr
= opaque
;
359 MuxDriver
*d
= chr
->opaque
;
361 if ((d
->prod
- d
->cons
) < MUX_BUFFER_SIZE
)
363 if (d
->chr_can_read
[chr
->focus
])
364 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
368 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
370 CharDriverState
*chr
= opaque
;
371 MuxDriver
*d
= chr
->opaque
;
375 mux_chr_accept_input (opaque
);
377 for(i
= 0; i
< size
; i
++)
378 if (mux_proc_byte(chr
, d
, buf
[i
])) {
379 if (d
->prod
== d
->cons
&&
380 d
->chr_can_read
[m
] &&
381 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
382 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
384 d
->buffer
[d
->prod
++ & MUX_BUFFER_MASK
] = buf
[i
];
388 static void mux_chr_event(void *opaque
, int event
)
390 CharDriverState
*chr
= opaque
;
391 MuxDriver
*d
= chr
->opaque
;
394 /* Send the event to all registered listeners */
395 for (i
= 0; i
< d
->mux_cnt
; i
++)
397 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
400 static void mux_chr_update_read_handler(CharDriverState
*chr
)
402 MuxDriver
*d
= chr
->opaque
;
404 if (d
->mux_cnt
>= MAX_MUX
) {
405 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
408 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
409 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
410 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
411 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
412 /* Fix up the real driver with mux routines */
413 if (d
->mux_cnt
== 0) {
414 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
417 chr
->focus
= d
->mux_cnt
;
421 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
423 CharDriverState
*chr
;
426 chr
= qemu_mallocz(sizeof(CharDriverState
));
427 d
= qemu_mallocz(sizeof(MuxDriver
));
432 chr
->chr_write
= mux_chr_write
;
433 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
434 chr
->chr_accept_input
= mux_chr_accept_input
;
440 int send_all(int fd
, const void *buf
, int len1
)
446 ret
= send(fd
, buf
, len
, 0);
448 errno
= WSAGetLastError();
449 if (errno
!= WSAEWOULDBLOCK
) {
452 } else if (ret
== 0) {
464 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
470 ret
= write(fd
, buf
, len
);
472 if (errno
!= EINTR
&& errno
!= EAGAIN
)
474 } else if (ret
== 0) {
484 int send_all(int fd
, const void *buf
, int len1
)
486 return unix_write(fd
, buf
, len1
);
497 #define STDIO_MAX_CLIENTS 1
498 static int stdio_nb_clients
= 0;
500 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
502 FDCharDriver
*s
= chr
->opaque
;
503 return send_all(s
->fd_out
, buf
, len
);
506 static int fd_chr_read_poll(void *opaque
)
508 CharDriverState
*chr
= opaque
;
509 FDCharDriver
*s
= chr
->opaque
;
511 s
->max_size
= qemu_chr_can_read(chr
);
515 static void fd_chr_read(void *opaque
)
517 CharDriverState
*chr
= opaque
;
518 FDCharDriver
*s
= chr
->opaque
;
523 if (len
> s
->max_size
)
527 size
= read(s
->fd_in
, buf
, len
);
529 /* FD has been closed. Remove it from the active list. */
530 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
534 qemu_chr_read(chr
, buf
, size
);
538 static void fd_chr_update_read_handler(CharDriverState
*chr
)
540 FDCharDriver
*s
= chr
->opaque
;
543 if (nographic
&& s
->fd_in
== 0) {
545 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
546 fd_chr_read
, NULL
, chr
);
551 static void fd_chr_close(struct CharDriverState
*chr
)
553 FDCharDriver
*s
= chr
->opaque
;
556 if (nographic
&& s
->fd_in
== 0) {
558 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
565 /* open a character device to a unix fd */
566 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
568 CharDriverState
*chr
;
571 chr
= qemu_mallocz(sizeof(CharDriverState
));
572 s
= qemu_mallocz(sizeof(FDCharDriver
));
576 chr
->chr_write
= fd_chr_write
;
577 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
578 chr
->chr_close
= fd_chr_close
;
585 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
589 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
592 return qemu_chr_open_fd(-1, fd_out
);
595 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
598 char filename_in
[256], filename_out
[256];
600 snprintf(filename_in
, 256, "%s.in", filename
);
601 snprintf(filename_out
, 256, "%s.out", filename
);
602 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
603 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
604 if (fd_in
< 0 || fd_out
< 0) {
609 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
613 return qemu_chr_open_fd(fd_in
, fd_out
);
617 /* for STDIO, we handle the case where several clients use it
620 #define TERM_FIFO_MAX_SIZE 1
622 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
623 static int term_fifo_size
;
625 static int stdio_read_poll(void *opaque
)
627 CharDriverState
*chr
= opaque
;
629 /* try to flush the queue if needed */
630 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
631 qemu_chr_read(chr
, term_fifo
, 1);
634 /* see if we can absorb more chars */
635 if (term_fifo_size
== 0)
641 static void stdio_read(void *opaque
)
645 CharDriverState
*chr
= opaque
;
647 size
= read(0, buf
, 1);
649 /* stdin has been closed. Remove it from the active list. */
650 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
654 if (qemu_chr_can_read(chr
) > 0) {
655 qemu_chr_read(chr
, buf
, 1);
656 } else if (term_fifo_size
== 0) {
657 term_fifo
[term_fifo_size
++] = buf
[0];
662 /* init terminal so that we can grab keys */
663 static struct termios oldtty
;
664 static int old_fd0_flags
;
665 static int term_atexit_done
;
667 static void term_exit(void)
669 tcsetattr (0, TCSANOW
, &oldtty
);
670 fcntl(0, F_SETFL
, old_fd0_flags
);
673 static void term_init(void)
679 old_fd0_flags
= fcntl(0, F_GETFL
);
681 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
682 |INLCR
|IGNCR
|ICRNL
|IXON
);
683 tty
.c_oflag
|= OPOST
;
684 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
685 /* if graphical mode, we allow Ctrl-C handling */
687 tty
.c_lflag
&= ~ISIG
;
688 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
693 tcsetattr (0, TCSANOW
, &tty
);
695 if (!term_atexit_done
++)
698 fcntl(0, F_SETFL
, O_NONBLOCK
);
701 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
705 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
709 static CharDriverState
*qemu_chr_open_stdio(void)
711 CharDriverState
*chr
;
713 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
715 chr
= qemu_chr_open_fd(0, 1);
716 chr
->chr_close
= qemu_chr_close_stdio
;
717 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
725 /* Once Solaris has openpty(), this is going to be removed. */
726 int openpty(int *amaster
, int *aslave
, char *name
,
727 struct termios
*termp
, struct winsize
*winp
)
730 int mfd
= -1, sfd
= -1;
732 *amaster
= *aslave
= -1;
734 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
738 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
741 if ((slave
= ptsname(mfd
)) == NULL
)
744 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
747 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
748 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
756 ioctl(sfd
, TIOCSWINSZ
, winp
);
767 void cfmakeraw (struct termios
*termios_p
)
769 termios_p
->c_iflag
&=
770 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
771 termios_p
->c_oflag
&= ~OPOST
;
772 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
773 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
774 termios_p
->c_cflag
|= CS8
;
776 termios_p
->c_cc
[VMIN
] = 0;
777 termios_p
->c_cc
[VTIME
] = 0;
781 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
782 || defined(__NetBSD__) || defined(__OpenBSD__)
792 static void pty_chr_update_read_handler(CharDriverState
*chr
);
793 static void pty_chr_state(CharDriverState
*chr
, int connected
);
795 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
797 PtyCharDriver
*s
= chr
->opaque
;
800 /* guest sends data, check for (re-)connect */
801 pty_chr_update_read_handler(chr
);
804 return send_all(s
->fd
, buf
, len
);
807 static int pty_chr_read_poll(void *opaque
)
809 CharDriverState
*chr
= opaque
;
810 PtyCharDriver
*s
= chr
->opaque
;
812 s
->read_bytes
= qemu_chr_can_read(chr
);
813 return s
->read_bytes
;
816 static void pty_chr_read(void *opaque
)
818 CharDriverState
*chr
= opaque
;
819 PtyCharDriver
*s
= chr
->opaque
;
824 if (len
> s
->read_bytes
)
828 size
= read(s
->fd
, buf
, len
);
829 if ((size
== -1 && errno
== EIO
) ||
831 pty_chr_state(chr
, 0);
835 pty_chr_state(chr
, 1);
836 qemu_chr_read(chr
, buf
, size
);
840 static void pty_chr_update_read_handler(CharDriverState
*chr
)
842 PtyCharDriver
*s
= chr
->opaque
;
844 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
845 pty_chr_read
, NULL
, chr
);
848 * Short timeout here: just need wait long enougth that qemu makes
849 * it through the poll loop once. When reconnected we want a
850 * short timeout so we notice it almost instantly. Otherwise
851 * read() gives us -EIO instantly, making pty_chr_state() reset the
852 * timeout to the normal (much longer) poll interval before the
855 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
858 static void pty_chr_state(CharDriverState
*chr
, int connected
)
860 PtyCharDriver
*s
= chr
->opaque
;
863 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
866 /* (re-)connect poll interval for idle guests: once per second.
867 * We check more frequently in case the guests sends data to
868 * the virtual device linked to our pty. */
869 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
877 static void pty_chr_timer(void *opaque
)
879 struct CharDriverState
*chr
= opaque
;
880 PtyCharDriver
*s
= chr
->opaque
;
885 /* If we arrive here without polling being cleared due
886 * read returning -EIO, then we are (re-)connected */
887 pty_chr_state(chr
, 1);
892 pty_chr_update_read_handler(chr
);
895 static void pty_chr_close(struct CharDriverState
*chr
)
897 PtyCharDriver
*s
= chr
->opaque
;
899 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
904 static CharDriverState
*qemu_chr_open_pty(void)
906 CharDriverState
*chr
;
910 #if defined(__OpenBSD__)
911 char pty_name
[PATH_MAX
];
912 #define q_ptsname(x) pty_name
914 char *pty_name
= NULL
;
915 #define q_ptsname(x) ptsname(x)
918 chr
= qemu_mallocz(sizeof(CharDriverState
));
919 s
= qemu_mallocz(sizeof(PtyCharDriver
));
921 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
925 /* Set raw attributes on the pty. */
926 tcgetattr(slave_fd
, &tty
);
928 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
931 len
= strlen(q_ptsname(s
->fd
)) + 5;
932 chr
->filename
= qemu_malloc(len
);
933 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
934 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
937 chr
->chr_write
= pty_chr_write
;
938 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
939 chr
->chr_close
= pty_chr_close
;
941 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
946 static void tty_serial_init(int fd
, int speed
,
947 int parity
, int data_bits
, int stop_bits
)
953 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
954 speed
, parity
, data_bits
, stop_bits
);
956 tcgetattr (fd
, &tty
);
959 if (speed
<= 50 * MARGIN
)
961 else if (speed
<= 75 * MARGIN
)
963 else if (speed
<= 300 * MARGIN
)
965 else if (speed
<= 600 * MARGIN
)
967 else if (speed
<= 1200 * MARGIN
)
969 else if (speed
<= 2400 * MARGIN
)
971 else if (speed
<= 4800 * MARGIN
)
973 else if (speed
<= 9600 * MARGIN
)
975 else if (speed
<= 19200 * MARGIN
)
977 else if (speed
<= 38400 * MARGIN
)
979 else if (speed
<= 57600 * MARGIN
)
981 else if (speed
<= 115200 * MARGIN
)
986 cfsetispeed(&tty
, spd
);
987 cfsetospeed(&tty
, spd
);
989 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
990 |INLCR
|IGNCR
|ICRNL
|IXON
);
991 tty
.c_oflag
|= OPOST
;
992 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
993 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1014 tty
.c_cflag
|= PARENB
;
1017 tty
.c_cflag
|= PARENB
| PARODD
;
1021 tty
.c_cflag
|= CSTOPB
;
1023 tcsetattr (fd
, TCSANOW
, &tty
);
1026 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1028 FDCharDriver
*s
= chr
->opaque
;
1031 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1033 QEMUSerialSetParams
*ssp
= arg
;
1034 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1035 ssp
->data_bits
, ssp
->stop_bits
);
1038 case CHR_IOCTL_SERIAL_SET_BREAK
:
1040 int enable
= *(int *)arg
;
1042 tcsendbreak(s
->fd_in
, 1);
1045 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1048 int *targ
= (int *)arg
;
1049 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1051 if (sarg
& TIOCM_CTS
)
1052 *targ
|= CHR_TIOCM_CTS
;
1053 if (sarg
& TIOCM_CAR
)
1054 *targ
|= CHR_TIOCM_CAR
;
1055 if (sarg
& TIOCM_DSR
)
1056 *targ
|= CHR_TIOCM_DSR
;
1057 if (sarg
& TIOCM_RI
)
1058 *targ
|= CHR_TIOCM_RI
;
1059 if (sarg
& TIOCM_DTR
)
1060 *targ
|= CHR_TIOCM_DTR
;
1061 if (sarg
& TIOCM_RTS
)
1062 *targ
|= CHR_TIOCM_RTS
;
1065 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1067 int sarg
= *(int *)arg
;
1069 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1070 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1071 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1072 if (sarg
& CHR_TIOCM_CTS
)
1074 if (sarg
& CHR_TIOCM_CAR
)
1076 if (sarg
& CHR_TIOCM_DSR
)
1078 if (sarg
& CHR_TIOCM_RI
)
1080 if (sarg
& CHR_TIOCM_DTR
)
1082 if (sarg
& CHR_TIOCM_RTS
)
1084 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1093 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1095 CharDriverState
*chr
;
1098 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1099 tty_serial_init(fd
, 115200, 'N', 8, 1);
1100 chr
= qemu_chr_open_fd(fd
, fd
);
1105 chr
->chr_ioctl
= tty_serial_ioctl
;
1106 qemu_chr_reset(chr
);
1109 #else /* ! __linux__ && ! __sun__ */
1110 static CharDriverState
*qemu_chr_open_pty(void)
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(CharDriverState
*chr
, int cmd
, void *arg
)
1135 ParallelCharDriver
*drv
= chr
->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
);
1225 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1227 CharDriverState
*chr
;
1228 ParallelCharDriver
*drv
;
1231 TFR(fd
= open(filename
, O_RDWR
));
1235 if (ioctl(fd
, PPCLAIM
) < 0) {
1240 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1242 drv
->mode
= IEEE1284_MODE_COMPAT
;
1244 chr
= qemu_mallocz(sizeof(CharDriverState
));
1245 chr
->chr_write
= null_chr_write
;
1246 chr
->chr_ioctl
= pp_ioctl
;
1247 chr
->chr_close
= pp_close
;
1250 qemu_chr_reset(chr
);
1254 #endif /* __linux__ */
1256 #if defined(__FreeBSD__)
1257 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1259 int fd
= (int)chr
->opaque
;
1263 case CHR_IOCTL_PP_READ_DATA
:
1264 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1266 *(uint8_t *)arg
= b
;
1268 case CHR_IOCTL_PP_WRITE_DATA
:
1269 b
= *(uint8_t *)arg
;
1270 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1273 case CHR_IOCTL_PP_READ_CONTROL
:
1274 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1276 *(uint8_t *)arg
= b
;
1278 case CHR_IOCTL_PP_WRITE_CONTROL
:
1279 b
= *(uint8_t *)arg
;
1280 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1283 case CHR_IOCTL_PP_READ_STATUS
:
1284 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1286 *(uint8_t *)arg
= b
;
1294 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1296 CharDriverState
*chr
;
1299 fd
= open(filename
, O_RDWR
);
1303 chr
= qemu_mallocz(sizeof(CharDriverState
));
1304 chr
->opaque
= (void *)fd
;
1305 chr
->chr_write
= null_chr_write
;
1306 chr
->chr_ioctl
= pp_ioctl
;
1315 HANDLE hcom
, hrecv
, hsend
;
1316 OVERLAPPED orecv
, osend
;
1321 #define NSENDBUF 2048
1322 #define NRECVBUF 2048
1323 #define MAXCONNECT 1
1324 #define NTIMEOUT 5000
1326 static int win_chr_poll(void *opaque
);
1327 static int win_chr_pipe_poll(void *opaque
);
1329 static void win_chr_close(CharDriverState
*chr
)
1331 WinCharState
*s
= chr
->opaque
;
1334 CloseHandle(s
->hsend
);
1338 CloseHandle(s
->hrecv
);
1342 CloseHandle(s
->hcom
);
1346 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1348 qemu_del_polling_cb(win_chr_poll
, chr
);
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_can_read(chr
);
1460 static void win_chr_readfile(CharDriverState
*chr
)
1462 WinCharState
*s
= chr
->opaque
;
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_read(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 CharDriverState
*qemu_chr_open_win(const char *filename
)
1513 CharDriverState
*chr
;
1516 chr
= qemu_mallocz(sizeof(CharDriverState
));
1517 s
= qemu_mallocz(sizeof(WinCharState
));
1519 chr
->chr_write
= win_chr_write
;
1520 chr
->chr_close
= win_chr_close
;
1522 if (win_chr_init(chr
, filename
) < 0) {
1527 qemu_chr_reset(chr
);
1531 static int win_chr_pipe_poll(void *opaque
)
1533 CharDriverState
*chr
= opaque
;
1534 WinCharState
*s
= chr
->opaque
;
1537 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1540 win_chr_read_poll(chr
);
1547 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1549 WinCharState
*s
= chr
->opaque
;
1557 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1559 fprintf(stderr
, "Failed CreateEvent\n");
1562 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1564 fprintf(stderr
, "Failed CreateEvent\n");
1568 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1569 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1570 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1572 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1573 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1574 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1579 ZeroMemory(&ov
, sizeof(ov
));
1580 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1581 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1583 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1587 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1589 fprintf(stderr
, "Failed GetOverlappedResult\n");
1591 CloseHandle(ov
.hEvent
);
1598 CloseHandle(ov
.hEvent
);
1601 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1610 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1612 CharDriverState
*chr
;
1615 chr
= qemu_mallocz(sizeof(CharDriverState
));
1616 s
= qemu_mallocz(sizeof(WinCharState
));
1618 chr
->chr_write
= win_chr_write
;
1619 chr
->chr_close
= win_chr_close
;
1621 if (win_chr_pipe_init(chr
, filename
) < 0) {
1626 qemu_chr_reset(chr
);
1630 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1632 CharDriverState
*chr
;
1635 chr
= qemu_mallocz(sizeof(CharDriverState
));
1636 s
= qemu_mallocz(sizeof(WinCharState
));
1639 chr
->chr_write
= win_chr_write
;
1640 qemu_chr_reset(chr
);
1644 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1646 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1649 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1653 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1654 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1655 if (fd_out
== INVALID_HANDLE_VALUE
)
1658 return qemu_chr_open_win_file(fd_out
);
1660 #endif /* !_WIN32 */
1662 /***********************************************************/
1663 /* UDP Net console */
1667 struct sockaddr_in daddr
;
1674 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1676 NetCharDriver
*s
= chr
->opaque
;
1678 return sendto(s
->fd
, buf
, len
, 0,
1679 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1682 static int udp_chr_read_poll(void *opaque
)
1684 CharDriverState
*chr
= opaque
;
1685 NetCharDriver
*s
= chr
->opaque
;
1687 s
->max_size
= qemu_chr_can_read(chr
);
1689 /* If there were any stray characters in the queue process them
1692 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1693 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1695 s
->max_size
= qemu_chr_can_read(chr
);
1700 static void udp_chr_read(void *opaque
)
1702 CharDriverState
*chr
= opaque
;
1703 NetCharDriver
*s
= chr
->opaque
;
1705 if (s
->max_size
== 0)
1707 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1708 s
->bufptr
= s
->bufcnt
;
1713 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1714 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1716 s
->max_size
= qemu_chr_can_read(chr
);
1720 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1722 NetCharDriver
*s
= chr
->opaque
;
1725 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1726 udp_chr_read
, NULL
, chr
);
1730 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1732 CharDriverState
*chr
= NULL
;
1733 NetCharDriver
*s
= NULL
;
1735 struct sockaddr_in saddr
;
1737 chr
= qemu_mallocz(sizeof(CharDriverState
));
1738 s
= qemu_mallocz(sizeof(NetCharDriver
));
1740 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1742 perror("socket(PF_INET, SOCK_DGRAM)");
1746 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1747 printf("Could not parse: %s\n", def
);
1751 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1761 chr
->chr_write
= udp_chr_write
;
1762 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1775 /***********************************************************/
1776 /* TCP Net console */
1787 static void tcp_chr_accept(void *opaque
);
1789 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1791 TCPCharDriver
*s
= chr
->opaque
;
1793 return send_all(s
->fd
, buf
, len
);
1795 /* XXX: indicate an error ? */
1800 static int tcp_chr_read_poll(void *opaque
)
1802 CharDriverState
*chr
= opaque
;
1803 TCPCharDriver
*s
= chr
->opaque
;
1806 s
->max_size
= qemu_chr_can_read(chr
);
1811 #define IAC_BREAK 243
1812 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1814 uint8_t *buf
, int *size
)
1816 /* Handle any telnet client's basic IAC options to satisfy char by
1817 * char mode with no echo. All IAC options will be removed from
1818 * the buf and the do_telnetopt variable will be used to track the
1819 * state of the width of the IAC information.
1821 * IAC commands come in sets of 3 bytes with the exception of the
1822 * "IAC BREAK" command and the double IAC.
1828 for (i
= 0; i
< *size
; i
++) {
1829 if (s
->do_telnetopt
> 1) {
1830 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1831 /* Double IAC means send an IAC */
1835 s
->do_telnetopt
= 1;
1837 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1838 /* Handle IAC break commands by sending a serial break */
1839 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1844 if (s
->do_telnetopt
>= 4) {
1845 s
->do_telnetopt
= 1;
1848 if ((unsigned char)buf
[i
] == IAC
) {
1849 s
->do_telnetopt
= 2;
1860 static void tcp_chr_read(void *opaque
)
1862 CharDriverState
*chr
= opaque
;
1863 TCPCharDriver
*s
= chr
->opaque
;
1867 if (!s
->connected
|| s
->max_size
<= 0)
1870 if (len
> s
->max_size
)
1872 size
= recv(s
->fd
, buf
, len
, 0);
1874 /* connection closed */
1876 if (s
->listen_fd
>= 0) {
1877 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1879 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1882 } else if (size
> 0) {
1883 if (s
->do_telnetopt
)
1884 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1886 qemu_chr_read(chr
, buf
, size
);
1890 static void tcp_chr_connect(void *opaque
)
1892 CharDriverState
*chr
= opaque
;
1893 TCPCharDriver
*s
= chr
->opaque
;
1896 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1897 tcp_chr_read
, NULL
, chr
);
1898 qemu_chr_reset(chr
);
1901 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1902 static void tcp_chr_telnet_init(int fd
)
1905 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1906 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1907 send(fd
, (char *)buf
, 3, 0);
1908 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1909 send(fd
, (char *)buf
, 3, 0);
1910 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1911 send(fd
, (char *)buf
, 3, 0);
1912 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1913 send(fd
, (char *)buf
, 3, 0);
1916 static void socket_set_nodelay(int fd
)
1919 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1922 static void tcp_chr_accept(void *opaque
)
1924 CharDriverState
*chr
= opaque
;
1925 TCPCharDriver
*s
= chr
->opaque
;
1926 struct sockaddr_in saddr
;
1928 struct sockaddr_un uaddr
;
1930 struct sockaddr
*addr
;
1937 len
= sizeof(uaddr
);
1938 addr
= (struct sockaddr
*)&uaddr
;
1942 len
= sizeof(saddr
);
1943 addr
= (struct sockaddr
*)&saddr
;
1945 fd
= accept(s
->listen_fd
, addr
, &len
);
1946 if (fd
< 0 && errno
!= EINTR
) {
1948 } else if (fd
>= 0) {
1949 if (s
->do_telnetopt
)
1950 tcp_chr_telnet_init(fd
);
1954 socket_set_nonblock(fd
);
1956 socket_set_nodelay(fd
);
1958 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
1959 tcp_chr_connect(chr
);
1962 static void tcp_chr_close(CharDriverState
*chr
)
1964 TCPCharDriver
*s
= chr
->opaque
;
1967 if (s
->listen_fd
>= 0)
1968 closesocket(s
->listen_fd
);
1972 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
1976 CharDriverState
*chr
= NULL
;
1977 TCPCharDriver
*s
= NULL
;
1978 int fd
= -1, offset
= 0;
1980 int is_waitconnect
= 1;
1985 while((ptr
= strchr(ptr
,','))) {
1987 if (!strncmp(ptr
,"server",6)) {
1989 } else if (!strncmp(ptr
,"nowait",6)) {
1991 } else if (!strncmp(ptr
,"nodelay",6)) {
1993 } else if (!strncmp(ptr
,"to=",3)) {
1994 /* nothing, inet_listen() parses this one */;
1995 } else if (!strncmp(ptr
,"ipv4",4)) {
1996 /* nothing, inet_connect() and inet_listen() parse this one */;
1997 } else if (!strncmp(ptr
,"ipv6",4)) {
1998 /* nothing, inet_connect() and inet_listen() parse this one */;
2000 printf("Unknown option: %s\n", ptr
);
2007 chr
= qemu_mallocz(sizeof(CharDriverState
));
2008 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2011 chr
->filename
= qemu_malloc(256);
2013 pstrcpy(chr
->filename
, 256, "unix:");
2014 } else if (is_telnet
) {
2015 pstrcpy(chr
->filename
, 256, "telnet:");
2017 pstrcpy(chr
->filename
, 256, "tcp:");
2019 offset
= strlen(chr
->filename
);
2023 fd
= unix_listen(host_str
, chr
->filename
+ offset
, 256 - offset
);
2025 fd
= unix_connect(host_str
);
2029 fd
= inet_listen(host_str
, chr
->filename
+ offset
, 256 - offset
,
2032 fd
= inet_connect(host_str
, SOCK_STREAM
);
2038 if (!is_waitconnect
)
2039 socket_set_nonblock(fd
);
2044 s
->is_unix
= is_unix
;
2045 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2048 chr
->chr_write
= tcp_chr_write
;
2049 chr
->chr_close
= tcp_chr_close
;
2053 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2055 s
->do_telnetopt
= 1;
2059 socket_set_nodelay(fd
);
2060 tcp_chr_connect(chr
);
2063 if (is_listen
&& is_waitconnect
) {
2064 printf("QEMU waiting for connection on: %s\n",
2065 chr
->filename
? chr
->filename
: host_str
);
2066 tcp_chr_accept(chr
);
2067 socket_set_nonblock(s
->listen_fd
);
2079 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
2080 = TAILQ_HEAD_INITIALIZER(chardevs
);
2082 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2085 CharDriverState
*chr
;
2087 if (!strcmp(filename
, "vc")) {
2088 chr
= text_console_init(0);
2090 if (strstart(filename
, "vc:", &p
)) {
2091 chr
= text_console_init(p
);
2093 if (!strcmp(filename
, "null")) {
2094 chr
= qemu_chr_open_null();
2096 if (strstart(filename
, "tcp:", &p
)) {
2097 chr
= qemu_chr_open_tcp(p
, 0, 0);
2099 if (strstart(filename
, "telnet:", &p
)) {
2100 chr
= qemu_chr_open_tcp(p
, 1, 0);
2102 if (strstart(filename
, "udp:", &p
)) {
2103 chr
= qemu_chr_open_udp(p
);
2105 if (strstart(filename
, "mon:", &p
)) {
2106 chr
= qemu_chr_open(label
, p
, NULL
);
2108 chr
= qemu_chr_open_mux(chr
);
2109 monitor_init(chr
, !nographic
);
2111 printf("Unable to open driver: %s\n", p
);
2113 } else if (!strcmp(filename
, "msmouse")) {
2114 chr
= qemu_chr_open_msmouse();
2117 if (strstart(filename
, "unix:", &p
)) {
2118 chr
= qemu_chr_open_tcp(p
, 0, 1);
2119 } else if (strstart(filename
, "file:", &p
)) {
2120 chr
= qemu_chr_open_file_out(p
);
2121 } else if (strstart(filename
, "pipe:", &p
)) {
2122 chr
= qemu_chr_open_pipe(p
);
2123 } else if (!strcmp(filename
, "pty")) {
2124 chr
= qemu_chr_open_pty();
2125 } else if (!strcmp(filename
, "stdio")) {
2126 chr
= qemu_chr_open_stdio();
2128 #if defined(__linux__)
2129 if (strstart(filename
, "/dev/parport", NULL
)) {
2130 chr
= qemu_chr_open_pp(filename
);
2132 #elif defined(__FreeBSD__)
2133 if (strstart(filename
, "/dev/ppi", NULL
)) {
2134 chr
= qemu_chr_open_pp(filename
);
2137 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2138 || defined(__NetBSD__) || defined(__OpenBSD__)
2139 if (strstart(filename
, "/dev/", NULL
)) {
2140 chr
= qemu_chr_open_tty(filename
);
2144 if (strstart(filename
, "COM", NULL
)) {
2145 chr
= qemu_chr_open_win(filename
);
2147 if (strstart(filename
, "pipe:", &p
)) {
2148 chr
= qemu_chr_open_win_pipe(p
);
2150 if (strstart(filename
, "con:", NULL
)) {
2151 chr
= qemu_chr_open_win_con(filename
);
2153 if (strstart(filename
, "file:", &p
)) {
2154 chr
= qemu_chr_open_win_file_out(p
);
2157 #ifdef CONFIG_BRLAPI
2158 if (!strcmp(filename
, "braille")) {
2159 chr
= chr_baum_init();
2168 chr
->filename
= qemu_strdup(filename
);
2170 chr
->label
= qemu_strdup(label
);
2171 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2176 void qemu_chr_close(CharDriverState
*chr
)
2178 TAILQ_REMOVE(&chardevs
, chr
, next
);
2180 chr
->chr_close(chr
);
2181 qemu_free(chr
->filename
);
2182 qemu_free(chr
->label
);
2186 void qemu_chr_info(void)
2188 CharDriverState
*chr
;
2190 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2191 term_printf("%s: filename=%s\n", chr
->label
, chr
->filename
);