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"
43 #include <sys/times.h>
47 #include <sys/ioctl.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
53 #include <net/if_tap.h>
56 #include <linux/if_tun.h>
58 #include <arpa/inet.h>
61 #include <sys/select.h>
69 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
70 #include <freebsd/stdlib.h>
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
96 #include "qemu_socket.h"
98 /***********************************************************/
99 /* character device */
101 static void qemu_chr_event(CharDriverState
*s
, int event
)
105 s
->chr_event(s
->handler_opaque
, event
);
108 static void qemu_chr_reset_bh(void *opaque
)
110 CharDriverState
*s
= opaque
;
111 qemu_chr_event(s
, CHR_EVENT_RESET
);
112 qemu_bh_delete(s
->bh
);
116 void qemu_chr_reset(CharDriverState
*s
)
119 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
120 qemu_bh_schedule(s
->bh
);
124 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
126 return s
->chr_write(s
, buf
, len
);
129 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
133 return s
->chr_ioctl(s
, cmd
, arg
);
136 int qemu_chr_can_read(CharDriverState
*s
)
138 if (!s
->chr_can_read
)
140 return s
->chr_can_read(s
->handler_opaque
);
143 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
145 s
->chr_read(s
->handler_opaque
, buf
, len
);
148 void qemu_chr_accept_input(CharDriverState
*s
)
150 if (s
->chr_accept_input
)
151 s
->chr_accept_input(s
);
154 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
159 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
160 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
164 void qemu_chr_send_event(CharDriverState
*s
, int event
)
166 if (s
->chr_send_event
)
167 s
->chr_send_event(s
, event
);
170 void qemu_chr_add_handlers(CharDriverState
*s
,
171 IOCanRWHandler
*fd_can_read
,
172 IOReadHandler
*fd_read
,
173 IOEventHandler
*fd_event
,
176 s
->chr_can_read
= fd_can_read
;
177 s
->chr_read
= fd_read
;
178 s
->chr_event
= fd_event
;
179 s
->handler_opaque
= opaque
;
180 if (s
->chr_update_read_handler
)
181 s
->chr_update_read_handler(s
);
184 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
189 static CharDriverState
*qemu_chr_open_null(void)
191 CharDriverState
*chr
;
193 chr
= qemu_mallocz(sizeof(CharDriverState
));
196 chr
->chr_write
= null_chr_write
;
200 /* MUX driver for serial I/O splitting */
201 static int term_timestamps
;
202 static int64_t term_timestamps_start
;
204 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
205 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
207 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
208 IOReadHandler
*chr_read
[MAX_MUX
];
209 IOEventHandler
*chr_event
[MAX_MUX
];
210 void *ext_opaque
[MAX_MUX
];
211 CharDriverState
*drv
;
212 unsigned char buffer
[MUX_BUFFER_SIZE
];
221 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
223 MuxDriver
*d
= chr
->opaque
;
225 if (!term_timestamps
) {
226 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
231 for(i
= 0; i
< len
; i
++) {
232 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
233 if (buf
[i
] == '\n') {
238 ti
= qemu_get_clock(rt_clock
);
239 if (term_timestamps_start
== -1)
240 term_timestamps_start
= ti
;
241 ti
-= term_timestamps_start
;
242 secs
= ti
/ 1000000000;
243 snprintf(buf1
, sizeof(buf1
),
244 "[%02d:%02d:%02d.%03d] ",
248 (int)((ti
/ 1000000) % 1000));
249 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
256 static const char * const mux_help
[] = {
257 "% h print this help\n\r",
258 "% x exit emulator\n\r",
259 "% s save disk data back to file (if -snapshot)\n\r",
260 "% t toggle console timestamps\n\r"
261 "% b send break (magic sysrq)\n\r",
262 "% c switch between console and monitor\n\r",
267 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
268 static void mux_print_help(CharDriverState
*chr
)
271 char ebuf
[15] = "Escape-Char";
272 char cbuf
[50] = "\n\r";
274 if (term_escape_char
> 0 && term_escape_char
< 26) {
275 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
276 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
278 snprintf(cbuf
, sizeof(cbuf
),
279 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
282 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
283 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
284 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
285 if (mux_help
[i
][j
] == '%')
286 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
288 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
293 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
295 if (d
->term_got_escape
) {
296 d
->term_got_escape
= 0;
297 if (ch
== term_escape_char
)
306 const char *term
= "QEMU: Terminated\n\r";
307 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
314 for (i
= 0; i
< nb_drives
; i
++) {
315 bdrv_commit(drives_table
[i
].bdrv
);
320 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
323 /* Switch to the next registered device */
325 if (chr
->focus
>= d
->mux_cnt
)
329 term_timestamps
= !term_timestamps
;
330 term_timestamps_start
= -1;
333 } else if (ch
== term_escape_char
) {
334 d
->term_got_escape
= 1;
342 static void mux_chr_accept_input(CharDriverState
*chr
)
345 MuxDriver
*d
= chr
->opaque
;
347 while (d
->prod
!= d
->cons
&&
348 d
->chr_can_read
[m
] &&
349 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
350 d
->chr_read
[m
](d
->ext_opaque
[m
],
351 &d
->buffer
[d
->cons
++ & MUX_BUFFER_MASK
], 1);
355 static int mux_chr_can_read(void *opaque
)
357 CharDriverState
*chr
= opaque
;
358 MuxDriver
*d
= chr
->opaque
;
360 if ((d
->prod
- d
->cons
) < MUX_BUFFER_SIZE
)
362 if (d
->chr_can_read
[chr
->focus
])
363 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
367 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
369 CharDriverState
*chr
= opaque
;
370 MuxDriver
*d
= chr
->opaque
;
374 mux_chr_accept_input (opaque
);
376 for(i
= 0; i
< size
; i
++)
377 if (mux_proc_byte(chr
, d
, buf
[i
])) {
378 if (d
->prod
== d
->cons
&&
379 d
->chr_can_read
[m
] &&
380 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
381 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
383 d
->buffer
[d
->prod
++ & MUX_BUFFER_MASK
] = buf
[i
];
387 static void mux_chr_event(void *opaque
, int event
)
389 CharDriverState
*chr
= opaque
;
390 MuxDriver
*d
= chr
->opaque
;
393 /* Send the event to all registered listeners */
394 for (i
= 0; i
< d
->mux_cnt
; i
++)
396 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
399 static void mux_chr_update_read_handler(CharDriverState
*chr
)
401 MuxDriver
*d
= chr
->opaque
;
403 if (d
->mux_cnt
>= MAX_MUX
) {
404 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
407 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
408 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
409 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
410 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
411 /* Fix up the real driver with mux routines */
412 if (d
->mux_cnt
== 0) {
413 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
416 chr
->focus
= d
->mux_cnt
;
420 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
422 CharDriverState
*chr
;
425 chr
= qemu_mallocz(sizeof(CharDriverState
));
428 d
= qemu_mallocz(sizeof(MuxDriver
));
437 chr
->chr_write
= mux_chr_write
;
438 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
439 chr
->chr_accept_input
= mux_chr_accept_input
;
445 int send_all(int fd
, const void *buf
, int len1
)
451 ret
= send(fd
, buf
, len
, 0);
454 errno
= WSAGetLastError();
455 if (errno
!= WSAEWOULDBLOCK
) {
458 } else if (ret
== 0) {
470 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
476 ret
= write(fd
, buf
, len
);
478 if (errno
!= EINTR
&& errno
!= EAGAIN
)
480 } else if (ret
== 0) {
490 int send_all(int fd
, const void *buf
, int len1
)
492 return unix_write(fd
, buf
, len1
);
503 #define STDIO_MAX_CLIENTS 1
504 static int stdio_nb_clients
= 0;
506 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
508 FDCharDriver
*s
= chr
->opaque
;
509 return send_all(s
->fd_out
, buf
, len
);
512 static int fd_chr_read_poll(void *opaque
)
514 CharDriverState
*chr
= opaque
;
515 FDCharDriver
*s
= chr
->opaque
;
517 s
->max_size
= qemu_chr_can_read(chr
);
521 static void fd_chr_read(void *opaque
)
523 CharDriverState
*chr
= opaque
;
524 FDCharDriver
*s
= chr
->opaque
;
529 if (len
> s
->max_size
)
533 size
= read(s
->fd_in
, buf
, len
);
535 /* FD has been closed. Remove it from the active list. */
536 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
540 qemu_chr_read(chr
, buf
, size
);
544 static void fd_chr_update_read_handler(CharDriverState
*chr
)
546 FDCharDriver
*s
= chr
->opaque
;
549 if (nographic
&& s
->fd_in
== 0) {
551 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
552 fd_chr_read
, NULL
, chr
);
557 static void fd_chr_close(struct CharDriverState
*chr
)
559 FDCharDriver
*s
= chr
->opaque
;
562 if (nographic
&& s
->fd_in
== 0) {
564 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
571 /* open a character device to a unix fd */
572 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
574 CharDriverState
*chr
;
577 chr
= qemu_mallocz(sizeof(CharDriverState
));
580 s
= qemu_mallocz(sizeof(FDCharDriver
));
588 chr
->chr_write
= fd_chr_write
;
589 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
590 chr
->chr_close
= fd_chr_close
;
597 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
601 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
604 return qemu_chr_open_fd(-1, fd_out
);
607 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
610 char filename_in
[256], filename_out
[256];
612 snprintf(filename_in
, 256, "%s.in", filename
);
613 snprintf(filename_out
, 256, "%s.out", filename
);
614 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
615 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
616 if (fd_in
< 0 || fd_out
< 0) {
621 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
625 return qemu_chr_open_fd(fd_in
, fd_out
);
629 /* for STDIO, we handle the case where several clients use it
632 #define TERM_FIFO_MAX_SIZE 1
634 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
635 static int term_fifo_size
;
637 static int stdio_read_poll(void *opaque
)
639 CharDriverState
*chr
= opaque
;
641 /* try to flush the queue if needed */
642 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
643 qemu_chr_read(chr
, term_fifo
, 1);
646 /* see if we can absorb more chars */
647 if (term_fifo_size
== 0)
653 static void stdio_read(void *opaque
)
657 CharDriverState
*chr
= opaque
;
659 size
= read(0, buf
, 1);
661 /* stdin has been closed. Remove it from the active list. */
662 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
666 if (qemu_chr_can_read(chr
) > 0) {
667 qemu_chr_read(chr
, buf
, 1);
668 } else if (term_fifo_size
== 0) {
669 term_fifo
[term_fifo_size
++] = buf
[0];
674 /* init terminal so that we can grab keys */
675 static struct termios oldtty
;
676 static int old_fd0_flags
;
677 static int term_atexit_done
;
679 static void term_exit(void)
681 tcsetattr (0, TCSANOW
, &oldtty
);
682 fcntl(0, F_SETFL
, old_fd0_flags
);
685 static void term_init(void)
691 old_fd0_flags
= fcntl(0, F_GETFL
);
693 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
694 |INLCR
|IGNCR
|ICRNL
|IXON
);
695 tty
.c_oflag
|= OPOST
;
696 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
697 /* if graphical mode, we allow Ctrl-C handling */
699 tty
.c_lflag
&= ~ISIG
;
700 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
705 tcsetattr (0, TCSANOW
, &tty
);
707 if (!term_atexit_done
++)
710 fcntl(0, F_SETFL
, O_NONBLOCK
);
713 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
717 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
721 static CharDriverState
*qemu_chr_open_stdio(void)
723 CharDriverState
*chr
;
725 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
727 chr
= qemu_chr_open_fd(0, 1);
728 chr
->chr_close
= qemu_chr_close_stdio
;
729 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
737 /* Once Solaris has openpty(), this is going to be removed. */
738 int openpty(int *amaster
, int *aslave
, char *name
,
739 struct termios
*termp
, struct winsize
*winp
)
742 int mfd
= -1, sfd
= -1;
744 *amaster
= *aslave
= -1;
746 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
750 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
753 if ((slave
= ptsname(mfd
)) == NULL
)
756 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
759 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
760 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
768 ioctl(sfd
, TIOCSWINSZ
, winp
);
779 void cfmakeraw (struct termios
*termios_p
)
781 termios_p
->c_iflag
&=
782 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
783 termios_p
->c_oflag
&= ~OPOST
;
784 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
785 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
786 termios_p
->c_cflag
|= CS8
;
788 termios_p
->c_cc
[VMIN
] = 0;
789 termios_p
->c_cc
[VTIME
] = 0;
793 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
794 || defined(__NetBSD__) || defined(__OpenBSD__)
804 static void pty_chr_update_read_handler(CharDriverState
*chr
);
805 static void pty_chr_state(CharDriverState
*chr
, int connected
);
807 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
809 PtyCharDriver
*s
= chr
->opaque
;
812 /* guest sends data, check for (re-)connect */
813 pty_chr_update_read_handler(chr
);
816 return send_all(s
->fd
, buf
, len
);
819 static int pty_chr_read_poll(void *opaque
)
821 CharDriverState
*chr
= opaque
;
822 PtyCharDriver
*s
= chr
->opaque
;
824 s
->read_bytes
= qemu_chr_can_read(chr
);
825 return s
->read_bytes
;
828 static void pty_chr_read(void *opaque
)
830 CharDriverState
*chr
= opaque
;
831 PtyCharDriver
*s
= chr
->opaque
;
836 if (len
> s
->read_bytes
)
840 size
= read(s
->fd
, buf
, len
);
841 if ((size
== -1 && errno
== EIO
) ||
843 pty_chr_state(chr
, 0);
847 pty_chr_state(chr
, 1);
848 qemu_chr_read(chr
, buf
, size
);
852 static void pty_chr_update_read_handler(CharDriverState
*chr
)
854 PtyCharDriver
*s
= chr
->opaque
;
856 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
857 pty_chr_read
, NULL
, chr
);
860 * Short timeout here: just need wait long enougth that qemu makes
861 * it through the poll loop once. When reconnected we want a
862 * short timeout so we notice it almost instantly. Otherwise
863 * read() gives us -EIO instantly, making pty_chr_state() reset the
864 * timeout to the normal (much longer) poll interval before the
867 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
870 static void pty_chr_state(CharDriverState
*chr
, int connected
)
872 PtyCharDriver
*s
= chr
->opaque
;
875 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
878 /* (re-)connect poll interval for idle guests: once per second.
879 * We check more frequently in case the guests sends data to
880 * the virtual device linked to our pty. */
881 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
889 static void pty_chr_timer(void *opaque
)
891 struct CharDriverState
*chr
= opaque
;
892 PtyCharDriver
*s
= chr
->opaque
;
897 /* If we arrive here without polling being cleared due
898 * read returning -EIO, then we are (re-)connected */
899 pty_chr_state(chr
, 1);
904 pty_chr_update_read_handler(chr
);
907 static void pty_chr_close(struct CharDriverState
*chr
)
909 PtyCharDriver
*s
= chr
->opaque
;
911 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
916 static CharDriverState
*qemu_chr_open_pty(void)
918 CharDriverState
*chr
;
922 #if defined(__OpenBSD__)
923 char pty_name
[PATH_MAX
];
924 #define q_ptsname(x) pty_name
926 char *pty_name
= NULL
;
927 #define q_ptsname(x) ptsname(x)
930 chr
= qemu_mallocz(sizeof(CharDriverState
));
933 s
= qemu_mallocz(sizeof(PtyCharDriver
));
939 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
943 /* Set raw attributes on the pty. */
944 tcgetattr(slave_fd
, &tty
);
946 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
949 len
= strlen(q_ptsname(s
->fd
)) + 5;
950 chr
->filename
= qemu_malloc(len
);
951 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
952 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
955 chr
->chr_write
= pty_chr_write
;
956 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
957 chr
->chr_close
= pty_chr_close
;
959 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
964 static void tty_serial_init(int fd
, int speed
,
965 int parity
, int data_bits
, int stop_bits
)
971 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
972 speed
, parity
, data_bits
, stop_bits
);
974 tcgetattr (fd
, &tty
);
977 if (speed
<= 50 * MARGIN
)
979 else if (speed
<= 75 * MARGIN
)
981 else if (speed
<= 300 * MARGIN
)
983 else if (speed
<= 600 * MARGIN
)
985 else if (speed
<= 1200 * MARGIN
)
987 else if (speed
<= 2400 * MARGIN
)
989 else if (speed
<= 4800 * MARGIN
)
991 else if (speed
<= 9600 * MARGIN
)
993 else if (speed
<= 19200 * MARGIN
)
995 else if (speed
<= 38400 * MARGIN
)
997 else if (speed
<= 57600 * MARGIN
)
999 else if (speed
<= 115200 * MARGIN
)
1004 cfsetispeed(&tty
, spd
);
1005 cfsetospeed(&tty
, spd
);
1007 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1008 |INLCR
|IGNCR
|ICRNL
|IXON
);
1009 tty
.c_oflag
|= OPOST
;
1010 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1011 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1032 tty
.c_cflag
|= PARENB
;
1035 tty
.c_cflag
|= PARENB
| PARODD
;
1039 tty
.c_cflag
|= CSTOPB
;
1041 tcsetattr (fd
, TCSANOW
, &tty
);
1044 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1046 FDCharDriver
*s
= chr
->opaque
;
1049 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1051 QEMUSerialSetParams
*ssp
= arg
;
1052 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1053 ssp
->data_bits
, ssp
->stop_bits
);
1056 case CHR_IOCTL_SERIAL_SET_BREAK
:
1058 int enable
= *(int *)arg
;
1060 tcsendbreak(s
->fd_in
, 1);
1063 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1066 int *targ
= (int *)arg
;
1067 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1069 if (sarg
| TIOCM_CTS
)
1070 *targ
|= CHR_TIOCM_CTS
;
1071 if (sarg
| TIOCM_CAR
)
1072 *targ
|= CHR_TIOCM_CAR
;
1073 if (sarg
| TIOCM_DSR
)
1074 *targ
|= CHR_TIOCM_DSR
;
1075 if (sarg
| TIOCM_RI
)
1076 *targ
|= CHR_TIOCM_RI
;
1077 if (sarg
| TIOCM_DTR
)
1078 *targ
|= CHR_TIOCM_DTR
;
1079 if (sarg
| TIOCM_RTS
)
1080 *targ
|= CHR_TIOCM_RTS
;
1083 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1085 int sarg
= *(int *)arg
;
1087 if (sarg
| CHR_TIOCM_DTR
)
1089 if (sarg
| CHR_TIOCM_RTS
)
1091 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1100 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1102 CharDriverState
*chr
;
1105 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1106 tty_serial_init(fd
, 115200, 'N', 8, 1);
1107 chr
= qemu_chr_open_fd(fd
, fd
);
1112 chr
->chr_ioctl
= tty_serial_ioctl
;
1113 qemu_chr_reset(chr
);
1116 #else /* ! __linux__ && ! __sun__ */
1117 static CharDriverState
*qemu_chr_open_pty(void)
1121 #endif /* __linux__ || __sun__ */
1123 #if defined(__linux__)
1127 } ParallelCharDriver
;
1129 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1131 if (s
->mode
!= mode
) {
1133 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1140 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1142 ParallelCharDriver
*drv
= chr
->opaque
;
1147 case CHR_IOCTL_PP_READ_DATA
:
1148 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1150 *(uint8_t *)arg
= b
;
1152 case CHR_IOCTL_PP_WRITE_DATA
:
1153 b
= *(uint8_t *)arg
;
1154 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1157 case CHR_IOCTL_PP_READ_CONTROL
:
1158 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1160 /* Linux gives only the lowest bits, and no way to know data
1161 direction! For better compatibility set the fixed upper
1163 *(uint8_t *)arg
= b
| 0xc0;
1165 case CHR_IOCTL_PP_WRITE_CONTROL
:
1166 b
= *(uint8_t *)arg
;
1167 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1170 case CHR_IOCTL_PP_READ_STATUS
:
1171 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1173 *(uint8_t *)arg
= b
;
1175 case CHR_IOCTL_PP_DATA_DIR
:
1176 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1179 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1180 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1181 struct ParallelIOArg
*parg
= arg
;
1182 int n
= read(fd
, parg
->buffer
, parg
->count
);
1183 if (n
!= parg
->count
) {
1188 case CHR_IOCTL_PP_EPP_READ
:
1189 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1190 struct ParallelIOArg
*parg
= arg
;
1191 int n
= read(fd
, parg
->buffer
, parg
->count
);
1192 if (n
!= parg
->count
) {
1197 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1198 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1199 struct ParallelIOArg
*parg
= arg
;
1200 int n
= write(fd
, parg
->buffer
, parg
->count
);
1201 if (n
!= parg
->count
) {
1206 case CHR_IOCTL_PP_EPP_WRITE
:
1207 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1208 struct ParallelIOArg
*parg
= arg
;
1209 int n
= write(fd
, parg
->buffer
, parg
->count
);
1210 if (n
!= parg
->count
) {
1221 static void pp_close(CharDriverState
*chr
)
1223 ParallelCharDriver
*drv
= chr
->opaque
;
1226 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1227 ioctl(fd
, PPRELEASE
);
1232 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1234 CharDriverState
*chr
;
1235 ParallelCharDriver
*drv
;
1238 TFR(fd
= open(filename
, O_RDWR
));
1242 if (ioctl(fd
, PPCLAIM
) < 0) {
1247 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1253 drv
->mode
= IEEE1284_MODE_COMPAT
;
1255 chr
= qemu_mallocz(sizeof(CharDriverState
));
1261 chr
->chr_write
= null_chr_write
;
1262 chr
->chr_ioctl
= pp_ioctl
;
1263 chr
->chr_close
= pp_close
;
1266 qemu_chr_reset(chr
);
1270 #endif /* __linux__ */
1276 HANDLE hcom
, hrecv
, hsend
;
1277 OVERLAPPED orecv
, osend
;
1282 #define NSENDBUF 2048
1283 #define NRECVBUF 2048
1284 #define MAXCONNECT 1
1285 #define NTIMEOUT 5000
1287 static int win_chr_poll(void *opaque
);
1288 static int win_chr_pipe_poll(void *opaque
);
1290 static void win_chr_close(CharDriverState
*chr
)
1292 WinCharState
*s
= chr
->opaque
;
1295 CloseHandle(s
->hsend
);
1299 CloseHandle(s
->hrecv
);
1303 CloseHandle(s
->hcom
);
1307 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1309 qemu_del_polling_cb(win_chr_poll
, chr
);
1312 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1314 WinCharState
*s
= chr
->opaque
;
1316 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1321 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1323 fprintf(stderr
, "Failed CreateEvent\n");
1326 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1328 fprintf(stderr
, "Failed CreateEvent\n");
1332 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1333 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1334 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1335 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1340 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1341 fprintf(stderr
, "Failed SetupComm\n");
1345 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1346 size
= sizeof(COMMCONFIG
);
1347 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1348 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1349 CommConfigDialog(filename
, NULL
, &comcfg
);
1351 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1352 fprintf(stderr
, "Failed SetCommState\n");
1356 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1357 fprintf(stderr
, "Failed SetCommMask\n");
1361 cto
.ReadIntervalTimeout
= MAXDWORD
;
1362 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1363 fprintf(stderr
, "Failed SetCommTimeouts\n");
1367 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1368 fprintf(stderr
, "Failed ClearCommError\n");
1371 qemu_add_polling_cb(win_chr_poll
, chr
);
1379 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1381 WinCharState
*s
= chr
->opaque
;
1382 DWORD len
, ret
, size
, err
;
1385 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1386 s
->osend
.hEvent
= s
->hsend
;
1389 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1391 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1393 err
= GetLastError();
1394 if (err
== ERROR_IO_PENDING
) {
1395 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1413 static int win_chr_read_poll(CharDriverState
*chr
)
1415 WinCharState
*s
= chr
->opaque
;
1417 s
->max_size
= qemu_chr_can_read(chr
);
1421 static void win_chr_readfile(CharDriverState
*chr
)
1423 WinCharState
*s
= chr
->opaque
;
1428 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1429 s
->orecv
.hEvent
= s
->hrecv
;
1430 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1432 err
= GetLastError();
1433 if (err
== ERROR_IO_PENDING
) {
1434 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1439 qemu_chr_read(chr
, buf
, size
);
1443 static void win_chr_read(CharDriverState
*chr
)
1445 WinCharState
*s
= chr
->opaque
;
1447 if (s
->len
> s
->max_size
)
1448 s
->len
= s
->max_size
;
1452 win_chr_readfile(chr
);
1455 static int win_chr_poll(void *opaque
)
1457 CharDriverState
*chr
= opaque
;
1458 WinCharState
*s
= chr
->opaque
;
1462 ClearCommError(s
->hcom
, &comerr
, &status
);
1463 if (status
.cbInQue
> 0) {
1464 s
->len
= status
.cbInQue
;
1465 win_chr_read_poll(chr
);
1472 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1474 CharDriverState
*chr
;
1477 chr
= qemu_mallocz(sizeof(CharDriverState
));
1480 s
= qemu_mallocz(sizeof(WinCharState
));
1486 chr
->chr_write
= win_chr_write
;
1487 chr
->chr_close
= win_chr_close
;
1489 if (win_chr_init(chr
, filename
) < 0) {
1494 qemu_chr_reset(chr
);
1498 static int win_chr_pipe_poll(void *opaque
)
1500 CharDriverState
*chr
= opaque
;
1501 WinCharState
*s
= chr
->opaque
;
1504 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1507 win_chr_read_poll(chr
);
1514 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1516 WinCharState
*s
= chr
->opaque
;
1524 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1526 fprintf(stderr
, "Failed CreateEvent\n");
1529 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1531 fprintf(stderr
, "Failed CreateEvent\n");
1535 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1536 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1537 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1539 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1540 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1541 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1546 ZeroMemory(&ov
, sizeof(ov
));
1547 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1548 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1550 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1554 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1556 fprintf(stderr
, "Failed GetOverlappedResult\n");
1558 CloseHandle(ov
.hEvent
);
1565 CloseHandle(ov
.hEvent
);
1568 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1577 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1579 CharDriverState
*chr
;
1582 chr
= qemu_mallocz(sizeof(CharDriverState
));
1585 s
= qemu_mallocz(sizeof(WinCharState
));
1591 chr
->chr_write
= win_chr_write
;
1592 chr
->chr_close
= win_chr_close
;
1594 if (win_chr_pipe_init(chr
, filename
) < 0) {
1599 qemu_chr_reset(chr
);
1603 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1605 CharDriverState
*chr
;
1608 chr
= qemu_mallocz(sizeof(CharDriverState
));
1611 s
= qemu_mallocz(sizeof(WinCharState
));
1618 chr
->chr_write
= win_chr_write
;
1619 qemu_chr_reset(chr
);
1623 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1625 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1628 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1632 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1633 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1634 if (fd_out
== INVALID_HANDLE_VALUE
)
1637 return qemu_chr_open_win_file(fd_out
);
1639 #endif /* !_WIN32 */
1641 /***********************************************************/
1642 /* UDP Net console */
1646 struct sockaddr_in daddr
;
1653 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1655 NetCharDriver
*s
= chr
->opaque
;
1657 return sendto(s
->fd
, buf
, len
, 0,
1658 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1661 static int udp_chr_read_poll(void *opaque
)
1663 CharDriverState
*chr
= opaque
;
1664 NetCharDriver
*s
= chr
->opaque
;
1666 s
->max_size
= qemu_chr_can_read(chr
);
1668 /* If there were any stray characters in the queue process them
1671 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1672 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1674 s
->max_size
= qemu_chr_can_read(chr
);
1679 static void udp_chr_read(void *opaque
)
1681 CharDriverState
*chr
= opaque
;
1682 NetCharDriver
*s
= chr
->opaque
;
1684 if (s
->max_size
== 0)
1686 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1687 s
->bufptr
= s
->bufcnt
;
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
);
1699 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1701 NetCharDriver
*s
= chr
->opaque
;
1704 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1705 udp_chr_read
, NULL
, chr
);
1709 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1711 CharDriverState
*chr
= NULL
;
1712 NetCharDriver
*s
= NULL
;
1714 struct sockaddr_in saddr
;
1716 chr
= qemu_mallocz(sizeof(CharDriverState
));
1719 s
= qemu_mallocz(sizeof(NetCharDriver
));
1723 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1725 perror("socket(PF_INET, SOCK_DGRAM)");
1729 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1730 printf("Could not parse: %s\n", def
);
1734 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1744 chr
->chr_write
= udp_chr_write
;
1745 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1758 /***********************************************************/
1759 /* TCP Net console */
1770 static void tcp_chr_accept(void *opaque
);
1772 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1774 TCPCharDriver
*s
= chr
->opaque
;
1776 return send_all(s
->fd
, buf
, len
);
1778 /* XXX: indicate an error ? */
1783 static int tcp_chr_read_poll(void *opaque
)
1785 CharDriverState
*chr
= opaque
;
1786 TCPCharDriver
*s
= chr
->opaque
;
1789 s
->max_size
= qemu_chr_can_read(chr
);
1794 #define IAC_BREAK 243
1795 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1797 uint8_t *buf
, int *size
)
1799 /* Handle any telnet client's basic IAC options to satisfy char by
1800 * char mode with no echo. All IAC options will be removed from
1801 * the buf and the do_telnetopt variable will be used to track the
1802 * state of the width of the IAC information.
1804 * IAC commands come in sets of 3 bytes with the exception of the
1805 * "IAC BREAK" command and the double IAC.
1811 for (i
= 0; i
< *size
; i
++) {
1812 if (s
->do_telnetopt
> 1) {
1813 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1814 /* Double IAC means send an IAC */
1818 s
->do_telnetopt
= 1;
1820 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1821 /* Handle IAC break commands by sending a serial break */
1822 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1827 if (s
->do_telnetopt
>= 4) {
1828 s
->do_telnetopt
= 1;
1831 if ((unsigned char)buf
[i
] == IAC
) {
1832 s
->do_telnetopt
= 2;
1843 static void tcp_chr_read(void *opaque
)
1845 CharDriverState
*chr
= opaque
;
1846 TCPCharDriver
*s
= chr
->opaque
;
1850 if (!s
->connected
|| s
->max_size
<= 0)
1853 if (len
> s
->max_size
)
1855 size
= recv(s
->fd
, buf
, len
, 0);
1857 /* connection closed */
1859 if (s
->listen_fd
>= 0) {
1860 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1862 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1865 } else if (size
> 0) {
1866 if (s
->do_telnetopt
)
1867 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1869 qemu_chr_read(chr
, buf
, size
);
1873 static void tcp_chr_connect(void *opaque
)
1875 CharDriverState
*chr
= opaque
;
1876 TCPCharDriver
*s
= chr
->opaque
;
1879 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1880 tcp_chr_read
, NULL
, chr
);
1881 qemu_chr_reset(chr
);
1884 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1885 static void tcp_chr_telnet_init(int fd
)
1888 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1889 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1890 send(fd
, (char *)buf
, 3, 0);
1891 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1892 send(fd
, (char *)buf
, 3, 0);
1893 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1894 send(fd
, (char *)buf
, 3, 0);
1895 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1896 send(fd
, (char *)buf
, 3, 0);
1899 static void socket_set_nodelay(int fd
)
1902 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1905 static void tcp_chr_accept(void *opaque
)
1907 CharDriverState
*chr
= opaque
;
1908 TCPCharDriver
*s
= chr
->opaque
;
1909 struct sockaddr_in saddr
;
1911 struct sockaddr_un uaddr
;
1913 struct sockaddr
*addr
;
1920 len
= sizeof(uaddr
);
1921 addr
= (struct sockaddr
*)&uaddr
;
1925 len
= sizeof(saddr
);
1926 addr
= (struct sockaddr
*)&saddr
;
1928 fd
= accept(s
->listen_fd
, addr
, &len
);
1929 if (fd
< 0 && errno
!= EINTR
) {
1931 } else if (fd
>= 0) {
1932 if (s
->do_telnetopt
)
1933 tcp_chr_telnet_init(fd
);
1937 socket_set_nonblock(fd
);
1939 socket_set_nodelay(fd
);
1941 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
1942 tcp_chr_connect(chr
);
1945 static void tcp_chr_close(CharDriverState
*chr
)
1947 TCPCharDriver
*s
= chr
->opaque
;
1950 if (s
->listen_fd
>= 0)
1951 closesocket(s
->listen_fd
);
1955 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
1959 CharDriverState
*chr
= NULL
;
1960 TCPCharDriver
*s
= NULL
;
1961 int fd
= -1, offset
= 0;
1963 int is_waitconnect
= 1;
1968 while((ptr
= strchr(ptr
,','))) {
1970 if (!strncmp(ptr
,"server",6)) {
1972 } else if (!strncmp(ptr
,"nowait",6)) {
1974 } else if (!strncmp(ptr
,"nodelay",6)) {
1976 } else if (!strncmp(ptr
,"to=",3)) {
1977 /* nothing, inet_listen() parses this one */;
1979 printf("Unknown option: %s\n", ptr
);
1986 chr
= qemu_mallocz(sizeof(CharDriverState
));
1989 s
= qemu_mallocz(sizeof(TCPCharDriver
));
1994 chr
->filename
= qemu_malloc(256);
1996 strcpy(chr
->filename
, "unix:");
1997 } else if (is_telnet
) {
1998 strcpy(chr
->filename
, "telnet:");
2000 strcpy(chr
->filename
, "tcp:");
2002 offset
= strlen(chr
->filename
);
2006 fd
= unix_listen(host_str
, chr
->filename
+ offset
, 256 - offset
);
2008 fd
= unix_connect(host_str
);
2012 fd
= inet_listen(host_str
, chr
->filename
+ offset
, 256 - offset
,
2015 fd
= inet_connect(host_str
, SOCK_STREAM
);
2021 if (!is_waitconnect
)
2022 socket_set_nonblock(fd
);
2027 s
->is_unix
= is_unix
;
2028 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2031 chr
->chr_write
= tcp_chr_write
;
2032 chr
->chr_close
= tcp_chr_close
;
2036 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2038 s
->do_telnetopt
= 1;
2042 socket_set_nodelay(fd
);
2043 tcp_chr_connect(chr
);
2046 if (is_listen
&& is_waitconnect
) {
2047 printf("QEMU waiting for connection on: %s\n",
2048 chr
->filename
? chr
->filename
: host_str
);
2049 tcp_chr_accept(chr
);
2050 socket_set_nonblock(s
->listen_fd
);
2062 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
2063 = TAILQ_HEAD_INITIALIZER(chardevs
);
2065 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
)
2068 CharDriverState
*chr
;
2070 if (!strcmp(filename
, "vc")) {
2071 chr
= text_console_init(&display_state
, 0);
2073 if (strstart(filename
, "vc:", &p
)) {
2074 chr
= text_console_init(&display_state
, p
);
2076 if (!strcmp(filename
, "null")) {
2077 chr
= qemu_chr_open_null();
2079 if (strstart(filename
, "tcp:", &p
)) {
2080 chr
= qemu_chr_open_tcp(p
, 0, 0);
2082 if (strstart(filename
, "telnet:", &p
)) {
2083 chr
= qemu_chr_open_tcp(p
, 1, 0);
2085 if (strstart(filename
, "udp:", &p
)) {
2086 chr
= qemu_chr_open_udp(p
);
2088 if (strstart(filename
, "mon:", &p
)) {
2089 chr
= qemu_chr_open(label
, p
);
2091 chr
= qemu_chr_open_mux(chr
);
2092 monitor_init(chr
, !nographic
);
2094 printf("Unable to open driver: %s\n", p
);
2098 if (strstart(filename
, "unix:", &p
)) {
2099 chr
= qemu_chr_open_tcp(p
, 0, 1);
2100 } else if (strstart(filename
, "file:", &p
)) {
2101 chr
= qemu_chr_open_file_out(p
);
2102 } else if (strstart(filename
, "pipe:", &p
)) {
2103 chr
= qemu_chr_open_pipe(p
);
2104 } else if (!strcmp(filename
, "pty")) {
2105 chr
= qemu_chr_open_pty();
2106 } else if (!strcmp(filename
, "stdio")) {
2107 chr
= qemu_chr_open_stdio();
2109 #if defined(__linux__)
2110 if (strstart(filename
, "/dev/parport", NULL
)) {
2111 chr
= qemu_chr_open_pp(filename
);
2114 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2115 || defined(__NetBSD__) || defined(__OpenBSD__)
2116 if (strstart(filename
, "/dev/", NULL
)) {
2117 chr
= qemu_chr_open_tty(filename
);
2121 if (strstart(filename
, "COM", NULL
)) {
2122 chr
= qemu_chr_open_win(filename
);
2124 if (strstart(filename
, "pipe:", &p
)) {
2125 chr
= qemu_chr_open_win_pipe(p
);
2127 if (strstart(filename
, "con:", NULL
)) {
2128 chr
= qemu_chr_open_win_con(filename
);
2130 if (strstart(filename
, "file:", &p
)) {
2131 chr
= qemu_chr_open_win_file_out(p
);
2134 #ifdef CONFIG_BRLAPI
2135 if (!strcmp(filename
, "braille")) {
2136 chr
= chr_baum_init();
2145 chr
->filename
= qemu_strdup(filename
);
2146 chr
->label
= qemu_strdup(label
);
2147 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2152 void qemu_chr_close(CharDriverState
*chr
)
2154 TAILQ_REMOVE(&chardevs
, chr
, next
);
2156 chr
->chr_close(chr
);
2157 qemu_free(chr
->filename
);
2158 qemu_free(chr
->label
);
2162 void qemu_chr_info(void)
2164 CharDriverState
*chr
;
2166 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2167 term_printf("%s: filename=%s\n", chr
->label
, chr
->filename
);