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 uint8_t *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 uint8_t *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. */
945 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
948 len
= strlen(q_ptsname(s
->fd
)) + 5;
949 chr
->filename
= qemu_malloc(len
);
950 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
951 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
954 chr
->chr_write
= pty_chr_write
;
955 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
956 chr
->chr_close
= pty_chr_close
;
958 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
963 static void tty_serial_init(int fd
, int speed
,
964 int parity
, int data_bits
, int stop_bits
)
970 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
971 speed
, parity
, data_bits
, stop_bits
);
973 tcgetattr (fd
, &tty
);
976 if (speed
<= 50 * MARGIN
)
978 else if (speed
<= 75 * MARGIN
)
980 else if (speed
<= 300 * MARGIN
)
982 else if (speed
<= 600 * MARGIN
)
984 else if (speed
<= 1200 * MARGIN
)
986 else if (speed
<= 2400 * MARGIN
)
988 else if (speed
<= 4800 * MARGIN
)
990 else if (speed
<= 9600 * MARGIN
)
992 else if (speed
<= 19200 * MARGIN
)
994 else if (speed
<= 38400 * MARGIN
)
996 else if (speed
<= 57600 * MARGIN
)
998 else if (speed
<= 115200 * MARGIN
)
1003 cfsetispeed(&tty
, spd
);
1004 cfsetospeed(&tty
, spd
);
1006 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1007 |INLCR
|IGNCR
|ICRNL
|IXON
);
1008 tty
.c_oflag
|= OPOST
;
1009 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1010 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1031 tty
.c_cflag
|= PARENB
;
1034 tty
.c_cflag
|= PARENB
| PARODD
;
1038 tty
.c_cflag
|= CSTOPB
;
1040 tcsetattr (fd
, TCSANOW
, &tty
);
1043 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1045 FDCharDriver
*s
= chr
->opaque
;
1048 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1050 QEMUSerialSetParams
*ssp
= arg
;
1051 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1052 ssp
->data_bits
, ssp
->stop_bits
);
1055 case CHR_IOCTL_SERIAL_SET_BREAK
:
1057 int enable
= *(int *)arg
;
1059 tcsendbreak(s
->fd_in
, 1);
1062 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1065 int *targ
= (int *)arg
;
1066 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1068 if (sarg
| TIOCM_CTS
)
1069 *targ
|= CHR_TIOCM_CTS
;
1070 if (sarg
| TIOCM_CAR
)
1071 *targ
|= CHR_TIOCM_CAR
;
1072 if (sarg
| TIOCM_DSR
)
1073 *targ
|= CHR_TIOCM_DSR
;
1074 if (sarg
| TIOCM_RI
)
1075 *targ
|= CHR_TIOCM_RI
;
1076 if (sarg
| TIOCM_DTR
)
1077 *targ
|= CHR_TIOCM_DTR
;
1078 if (sarg
| TIOCM_RTS
)
1079 *targ
|= CHR_TIOCM_RTS
;
1082 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1084 int sarg
= *(int *)arg
;
1086 if (sarg
| CHR_TIOCM_DTR
)
1088 if (sarg
| CHR_TIOCM_RTS
)
1090 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1099 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1101 CharDriverState
*chr
;
1104 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1105 tty_serial_init(fd
, 115200, 'N', 8, 1);
1106 chr
= qemu_chr_open_fd(fd
, fd
);
1111 chr
->chr_ioctl
= tty_serial_ioctl
;
1112 qemu_chr_reset(chr
);
1115 #else /* ! __linux__ && ! __sun__ */
1116 static CharDriverState
*qemu_chr_open_pty(void)
1120 #endif /* __linux__ || __sun__ */
1122 #if defined(__linux__)
1126 } ParallelCharDriver
;
1128 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1130 if (s
->mode
!= mode
) {
1132 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1139 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1141 ParallelCharDriver
*drv
= chr
->opaque
;
1146 case CHR_IOCTL_PP_READ_DATA
:
1147 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1149 *(uint8_t *)arg
= b
;
1151 case CHR_IOCTL_PP_WRITE_DATA
:
1152 b
= *(uint8_t *)arg
;
1153 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1156 case CHR_IOCTL_PP_READ_CONTROL
:
1157 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1159 /* Linux gives only the lowest bits, and no way to know data
1160 direction! For better compatibility set the fixed upper
1162 *(uint8_t *)arg
= b
| 0xc0;
1164 case CHR_IOCTL_PP_WRITE_CONTROL
:
1165 b
= *(uint8_t *)arg
;
1166 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1169 case CHR_IOCTL_PP_READ_STATUS
:
1170 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1172 *(uint8_t *)arg
= b
;
1174 case CHR_IOCTL_PP_DATA_DIR
:
1175 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1178 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1179 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1180 struct ParallelIOArg
*parg
= arg
;
1181 int n
= read(fd
, parg
->buffer
, parg
->count
);
1182 if (n
!= parg
->count
) {
1187 case CHR_IOCTL_PP_EPP_READ
:
1188 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1189 struct ParallelIOArg
*parg
= arg
;
1190 int n
= read(fd
, parg
->buffer
, parg
->count
);
1191 if (n
!= parg
->count
) {
1196 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1197 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1198 struct ParallelIOArg
*parg
= arg
;
1199 int n
= write(fd
, parg
->buffer
, parg
->count
);
1200 if (n
!= parg
->count
) {
1205 case CHR_IOCTL_PP_EPP_WRITE
:
1206 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1207 struct ParallelIOArg
*parg
= arg
;
1208 int n
= write(fd
, parg
->buffer
, parg
->count
);
1209 if (n
!= parg
->count
) {
1220 static void pp_close(CharDriverState
*chr
)
1222 ParallelCharDriver
*drv
= chr
->opaque
;
1225 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1226 ioctl(fd
, PPRELEASE
);
1231 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1233 CharDriverState
*chr
;
1234 ParallelCharDriver
*drv
;
1237 TFR(fd
= open(filename
, O_RDWR
));
1241 if (ioctl(fd
, PPCLAIM
) < 0) {
1246 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1252 drv
->mode
= IEEE1284_MODE_COMPAT
;
1254 chr
= qemu_mallocz(sizeof(CharDriverState
));
1260 chr
->chr_write
= null_chr_write
;
1261 chr
->chr_ioctl
= pp_ioctl
;
1262 chr
->chr_close
= pp_close
;
1265 qemu_chr_reset(chr
);
1269 #endif /* __linux__ */
1275 HANDLE hcom
, hrecv
, hsend
;
1276 OVERLAPPED orecv
, osend
;
1281 #define NSENDBUF 2048
1282 #define NRECVBUF 2048
1283 #define MAXCONNECT 1
1284 #define NTIMEOUT 5000
1286 static int win_chr_poll(void *opaque
);
1287 static int win_chr_pipe_poll(void *opaque
);
1289 static void win_chr_close(CharDriverState
*chr
)
1291 WinCharState
*s
= chr
->opaque
;
1294 CloseHandle(s
->hsend
);
1298 CloseHandle(s
->hrecv
);
1302 CloseHandle(s
->hcom
);
1306 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1308 qemu_del_polling_cb(win_chr_poll
, chr
);
1311 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1313 WinCharState
*s
= chr
->opaque
;
1315 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1320 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1322 fprintf(stderr
, "Failed CreateEvent\n");
1325 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1327 fprintf(stderr
, "Failed CreateEvent\n");
1331 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1332 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1333 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1334 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1339 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1340 fprintf(stderr
, "Failed SetupComm\n");
1344 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1345 size
= sizeof(COMMCONFIG
);
1346 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1347 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1348 CommConfigDialog(filename
, NULL
, &comcfg
);
1350 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1351 fprintf(stderr
, "Failed SetCommState\n");
1355 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1356 fprintf(stderr
, "Failed SetCommMask\n");
1360 cto
.ReadIntervalTimeout
= MAXDWORD
;
1361 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1362 fprintf(stderr
, "Failed SetCommTimeouts\n");
1366 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1367 fprintf(stderr
, "Failed ClearCommError\n");
1370 qemu_add_polling_cb(win_chr_poll
, chr
);
1378 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1380 WinCharState
*s
= chr
->opaque
;
1381 DWORD len
, ret
, size
, err
;
1384 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1385 s
->osend
.hEvent
= s
->hsend
;
1388 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1390 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1392 err
= GetLastError();
1393 if (err
== ERROR_IO_PENDING
) {
1394 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1412 static int win_chr_read_poll(CharDriverState
*chr
)
1414 WinCharState
*s
= chr
->opaque
;
1416 s
->max_size
= qemu_chr_can_read(chr
);
1420 static void win_chr_readfile(CharDriverState
*chr
)
1422 WinCharState
*s
= chr
->opaque
;
1427 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1428 s
->orecv
.hEvent
= s
->hrecv
;
1429 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1431 err
= GetLastError();
1432 if (err
== ERROR_IO_PENDING
) {
1433 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1438 qemu_chr_read(chr
, buf
, size
);
1442 static void win_chr_read(CharDriverState
*chr
)
1444 WinCharState
*s
= chr
->opaque
;
1446 if (s
->len
> s
->max_size
)
1447 s
->len
= s
->max_size
;
1451 win_chr_readfile(chr
);
1454 static int win_chr_poll(void *opaque
)
1456 CharDriverState
*chr
= opaque
;
1457 WinCharState
*s
= chr
->opaque
;
1461 ClearCommError(s
->hcom
, &comerr
, &status
);
1462 if (status
.cbInQue
> 0) {
1463 s
->len
= status
.cbInQue
;
1464 win_chr_read_poll(chr
);
1471 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1473 CharDriverState
*chr
;
1476 chr
= qemu_mallocz(sizeof(CharDriverState
));
1479 s
= qemu_mallocz(sizeof(WinCharState
));
1485 chr
->chr_write
= win_chr_write
;
1486 chr
->chr_close
= win_chr_close
;
1488 if (win_chr_init(chr
, filename
) < 0) {
1493 qemu_chr_reset(chr
);
1497 static int win_chr_pipe_poll(void *opaque
)
1499 CharDriverState
*chr
= opaque
;
1500 WinCharState
*s
= chr
->opaque
;
1503 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1506 win_chr_read_poll(chr
);
1513 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1515 WinCharState
*s
= chr
->opaque
;
1523 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1525 fprintf(stderr
, "Failed CreateEvent\n");
1528 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1530 fprintf(stderr
, "Failed CreateEvent\n");
1534 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1535 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1536 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1538 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1539 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1540 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1545 ZeroMemory(&ov
, sizeof(ov
));
1546 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1547 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1549 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1553 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1555 fprintf(stderr
, "Failed GetOverlappedResult\n");
1557 CloseHandle(ov
.hEvent
);
1564 CloseHandle(ov
.hEvent
);
1567 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1576 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1578 CharDriverState
*chr
;
1581 chr
= qemu_mallocz(sizeof(CharDriverState
));
1584 s
= qemu_mallocz(sizeof(WinCharState
));
1590 chr
->chr_write
= win_chr_write
;
1591 chr
->chr_close
= win_chr_close
;
1593 if (win_chr_pipe_init(chr
, filename
) < 0) {
1598 qemu_chr_reset(chr
);
1602 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1604 CharDriverState
*chr
;
1607 chr
= qemu_mallocz(sizeof(CharDriverState
));
1610 s
= qemu_mallocz(sizeof(WinCharState
));
1617 chr
->chr_write
= win_chr_write
;
1618 qemu_chr_reset(chr
);
1622 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1624 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1627 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1631 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1632 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1633 if (fd_out
== INVALID_HANDLE_VALUE
)
1636 return qemu_chr_open_win_file(fd_out
);
1638 #endif /* !_WIN32 */
1640 /***********************************************************/
1641 /* UDP Net console */
1645 struct sockaddr_in daddr
;
1652 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1654 NetCharDriver
*s
= chr
->opaque
;
1656 return sendto(s
->fd
, buf
, len
, 0,
1657 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1660 static int udp_chr_read_poll(void *opaque
)
1662 CharDriverState
*chr
= opaque
;
1663 NetCharDriver
*s
= chr
->opaque
;
1665 s
->max_size
= qemu_chr_can_read(chr
);
1667 /* If there were any stray characters in the queue process them
1670 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1671 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1673 s
->max_size
= qemu_chr_can_read(chr
);
1678 static void udp_chr_read(void *opaque
)
1680 CharDriverState
*chr
= opaque
;
1681 NetCharDriver
*s
= chr
->opaque
;
1683 if (s
->max_size
== 0)
1685 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1686 s
->bufptr
= s
->bufcnt
;
1691 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1692 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1694 s
->max_size
= qemu_chr_can_read(chr
);
1698 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1700 NetCharDriver
*s
= chr
->opaque
;
1703 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1704 udp_chr_read
, NULL
, chr
);
1708 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1710 CharDriverState
*chr
= NULL
;
1711 NetCharDriver
*s
= NULL
;
1713 struct sockaddr_in saddr
;
1715 chr
= qemu_mallocz(sizeof(CharDriverState
));
1718 s
= qemu_mallocz(sizeof(NetCharDriver
));
1722 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1724 perror("socket(PF_INET, SOCK_DGRAM)");
1728 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1729 printf("Could not parse: %s\n", def
);
1733 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1743 chr
->chr_write
= udp_chr_write
;
1744 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1757 /***********************************************************/
1758 /* TCP Net console */
1769 static void tcp_chr_accept(void *opaque
);
1771 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1773 TCPCharDriver
*s
= chr
->opaque
;
1775 return send_all(s
->fd
, buf
, len
);
1777 /* XXX: indicate an error ? */
1782 static int tcp_chr_read_poll(void *opaque
)
1784 CharDriverState
*chr
= opaque
;
1785 TCPCharDriver
*s
= chr
->opaque
;
1788 s
->max_size
= qemu_chr_can_read(chr
);
1793 #define IAC_BREAK 243
1794 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1796 uint8_t *buf
, int *size
)
1798 /* Handle any telnet client's basic IAC options to satisfy char by
1799 * char mode with no echo. All IAC options will be removed from
1800 * the buf and the do_telnetopt variable will be used to track the
1801 * state of the width of the IAC information.
1803 * IAC commands come in sets of 3 bytes with the exception of the
1804 * "IAC BREAK" command and the double IAC.
1810 for (i
= 0; i
< *size
; i
++) {
1811 if (s
->do_telnetopt
> 1) {
1812 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1813 /* Double IAC means send an IAC */
1817 s
->do_telnetopt
= 1;
1819 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1820 /* Handle IAC break commands by sending a serial break */
1821 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1826 if (s
->do_telnetopt
>= 4) {
1827 s
->do_telnetopt
= 1;
1830 if ((unsigned char)buf
[i
] == IAC
) {
1831 s
->do_telnetopt
= 2;
1842 static void tcp_chr_read(void *opaque
)
1844 CharDriverState
*chr
= opaque
;
1845 TCPCharDriver
*s
= chr
->opaque
;
1849 if (!s
->connected
|| s
->max_size
<= 0)
1852 if (len
> s
->max_size
)
1854 size
= recv(s
->fd
, buf
, len
, 0);
1856 /* connection closed */
1858 if (s
->listen_fd
>= 0) {
1859 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1861 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1864 } else if (size
> 0) {
1865 if (s
->do_telnetopt
)
1866 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1868 qemu_chr_read(chr
, buf
, size
);
1872 static void tcp_chr_connect(void *opaque
)
1874 CharDriverState
*chr
= opaque
;
1875 TCPCharDriver
*s
= chr
->opaque
;
1878 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1879 tcp_chr_read
, NULL
, chr
);
1880 qemu_chr_reset(chr
);
1883 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1884 static void tcp_chr_telnet_init(int fd
)
1887 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1888 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1889 send(fd
, (char *)buf
, 3, 0);
1890 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1891 send(fd
, (char *)buf
, 3, 0);
1892 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1893 send(fd
, (char *)buf
, 3, 0);
1894 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1895 send(fd
, (char *)buf
, 3, 0);
1898 static void socket_set_nodelay(int fd
)
1901 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1904 static void tcp_chr_accept(void *opaque
)
1906 CharDriverState
*chr
= opaque
;
1907 TCPCharDriver
*s
= chr
->opaque
;
1908 struct sockaddr_in saddr
;
1910 struct sockaddr_un uaddr
;
1912 struct sockaddr
*addr
;
1919 len
= sizeof(uaddr
);
1920 addr
= (struct sockaddr
*)&uaddr
;
1924 len
= sizeof(saddr
);
1925 addr
= (struct sockaddr
*)&saddr
;
1927 fd
= accept(s
->listen_fd
, addr
, &len
);
1928 if (fd
< 0 && errno
!= EINTR
) {
1930 } else if (fd
>= 0) {
1931 if (s
->do_telnetopt
)
1932 tcp_chr_telnet_init(fd
);
1936 socket_set_nonblock(fd
);
1938 socket_set_nodelay(fd
);
1940 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
1941 tcp_chr_connect(chr
);
1944 static void tcp_chr_close(CharDriverState
*chr
)
1946 TCPCharDriver
*s
= chr
->opaque
;
1949 if (s
->listen_fd
>= 0)
1950 closesocket(s
->listen_fd
);
1954 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
1958 CharDriverState
*chr
= NULL
;
1959 TCPCharDriver
*s
= NULL
;
1960 int fd
= -1, ret
, err
, val
;
1962 int is_waitconnect
= 1;
1965 struct sockaddr_in saddr
;
1967 struct sockaddr_un uaddr
;
1969 struct sockaddr
*addr
;
1974 addr
= (struct sockaddr
*)&uaddr
;
1975 addrlen
= sizeof(uaddr
);
1976 if (parse_unix_path(&uaddr
, host_str
) < 0)
1981 addr
= (struct sockaddr
*)&saddr
;
1982 addrlen
= sizeof(saddr
);
1983 if (parse_host_port(&saddr
, host_str
) < 0)
1988 while((ptr
= strchr(ptr
,','))) {
1990 if (!strncmp(ptr
,"server",6)) {
1992 } else if (!strncmp(ptr
,"nowait",6)) {
1994 } else if (!strncmp(ptr
,"nodelay",6)) {
1997 printf("Unknown option: %s\n", ptr
);
2004 chr
= qemu_mallocz(sizeof(CharDriverState
));
2007 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2013 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2016 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
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
;
2035 /* allow fast reuse */
2039 pstrcpy(path
, sizeof(path
), uaddr
.sun_path
);
2045 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2048 ret
= bind(fd
, addr
, addrlen
);
2052 ret
= listen(fd
, 0);
2057 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2059 s
->do_telnetopt
= 1;
2062 ret
= connect(fd
, addr
, addrlen
);
2064 err
= socket_error();
2065 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2066 } else if (err
== EINPROGRESS
) {
2069 } else if (err
== WSAEALREADY
) {
2081 socket_set_nodelay(fd
);
2083 tcp_chr_connect(chr
);
2085 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2088 if (is_listen
&& is_waitconnect
) {
2089 printf("QEMU waiting for connection on: %s\n", host_str
);
2090 tcp_chr_accept(chr
);
2091 socket_set_nonblock(s
->listen_fd
);
2103 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
2104 = TAILQ_HEAD_INITIALIZER(chardevs
);
2106 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
)
2109 CharDriverState
*chr
;
2111 if (!strcmp(filename
, "vc")) {
2112 chr
= text_console_init(&display_state
, 0);
2114 if (strstart(filename
, "vc:", &p
)) {
2115 chr
= text_console_init(&display_state
, p
);
2117 if (!strcmp(filename
, "null")) {
2118 chr
= qemu_chr_open_null();
2120 if (strstart(filename
, "tcp:", &p
)) {
2121 chr
= qemu_chr_open_tcp(p
, 0, 0);
2123 if (strstart(filename
, "telnet:", &p
)) {
2124 chr
= qemu_chr_open_tcp(p
, 1, 0);
2126 if (strstart(filename
, "udp:", &p
)) {
2127 chr
= qemu_chr_open_udp(p
);
2129 if (strstart(filename
, "mon:", &p
)) {
2130 chr
= qemu_chr_open(label
, p
);
2132 chr
= qemu_chr_open_mux(chr
);
2133 monitor_init(chr
, !nographic
);
2135 printf("Unable to open driver: %s\n", p
);
2139 if (strstart(filename
, "unix:", &p
)) {
2140 chr
= qemu_chr_open_tcp(p
, 0, 1);
2141 } else if (strstart(filename
, "file:", &p
)) {
2142 chr
= qemu_chr_open_file_out(p
);
2143 } else if (strstart(filename
, "pipe:", &p
)) {
2144 chr
= qemu_chr_open_pipe(p
);
2145 } else if (!strcmp(filename
, "pty")) {
2146 chr
= qemu_chr_open_pty();
2147 } else if (!strcmp(filename
, "stdio")) {
2148 chr
= qemu_chr_open_stdio();
2150 #if defined(__linux__)
2151 if (strstart(filename
, "/dev/parport", NULL
)) {
2152 chr
= qemu_chr_open_pp(filename
);
2155 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2156 || defined(__NetBSD__) || defined(__OpenBSD__)
2157 if (strstart(filename
, "/dev/", NULL
)) {
2158 chr
= qemu_chr_open_tty(filename
);
2162 if (strstart(filename
, "COM", NULL
)) {
2163 chr
= qemu_chr_open_win(filename
);
2165 if (strstart(filename
, "pipe:", &p
)) {
2166 chr
= qemu_chr_open_win_pipe(p
);
2168 if (strstart(filename
, "con:", NULL
)) {
2169 chr
= qemu_chr_open_win_con(filename
);
2171 if (strstart(filename
, "file:", &p
)) {
2172 chr
= qemu_chr_open_win_file_out(p
);
2175 #ifdef CONFIG_BRLAPI
2176 if (!strcmp(filename
, "braille")) {
2177 chr
= chr_baum_init();
2186 chr
->filename
= qemu_strdup(filename
);
2187 chr
->label
= qemu_strdup(label
);
2188 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2193 void qemu_chr_close(CharDriverState
*chr
)
2195 TAILQ_REMOVE(&chardevs
, chr
, next
);
2197 chr
->chr_close(chr
);
2198 qemu_free(chr
->filename
);
2199 qemu_free(chr
->label
);
2203 void qemu_chr_info(void)
2205 CharDriverState
*chr
;
2207 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2208 term_printf("%s: filename=%s\n", chr
->label
, chr
->filename
);