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/socket.h>
49 #include <netinet/in.h>
52 #include <sys/select.h>
53 #include <arpa/inet.h>
56 #if !defined(__APPLE__) && !defined(__OpenBSD__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
69 #include <linux/ppdev.h>
70 #include <linux/parport.h>
74 #include <sys/ethernet.h>
75 #include <sys/sockio.h>
76 #include <netinet/arp.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip_icmp.h> // must come after ip.h
81 #include <netinet/udp.h>
82 #include <netinet/tcp.h>
90 #include "qemu_socket.h"
92 /***********************************************************/
93 /* character device */
95 static void qemu_chr_event(CharDriverState
*s
, int event
)
99 s
->chr_event(s
->handler_opaque
, event
);
102 static void qemu_chr_reset_bh(void *opaque
)
104 CharDriverState
*s
= opaque
;
105 qemu_chr_event(s
, CHR_EVENT_RESET
);
106 qemu_bh_delete(s
->bh
);
110 void qemu_chr_reset(CharDriverState
*s
)
113 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
114 qemu_bh_schedule(s
->bh
);
118 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
120 return s
->chr_write(s
, buf
, len
);
123 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
127 return s
->chr_ioctl(s
, cmd
, arg
);
130 int qemu_chr_can_read(CharDriverState
*s
)
132 if (!s
->chr_can_read
)
134 return s
->chr_can_read(s
->handler_opaque
);
137 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
139 s
->chr_read(s
->handler_opaque
, buf
, len
);
142 void qemu_chr_accept_input(CharDriverState
*s
)
144 if (s
->chr_accept_input
)
145 s
->chr_accept_input(s
);
148 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
153 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
154 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
158 void qemu_chr_send_event(CharDriverState
*s
, int event
)
160 if (s
->chr_send_event
)
161 s
->chr_send_event(s
, event
);
164 void qemu_chr_add_handlers(CharDriverState
*s
,
165 IOCanRWHandler
*fd_can_read
,
166 IOReadHandler
*fd_read
,
167 IOEventHandler
*fd_event
,
170 s
->chr_can_read
= fd_can_read
;
171 s
->chr_read
= fd_read
;
172 s
->chr_event
= fd_event
;
173 s
->handler_opaque
= opaque
;
174 if (s
->chr_update_read_handler
)
175 s
->chr_update_read_handler(s
);
178 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
183 static CharDriverState
*qemu_chr_open_null(void)
185 CharDriverState
*chr
;
187 chr
= qemu_mallocz(sizeof(CharDriverState
));
190 chr
->chr_write
= null_chr_write
;
194 /* MUX driver for serial I/O splitting */
195 static int term_timestamps
;
196 static int64_t term_timestamps_start
;
198 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
199 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
201 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
202 IOReadHandler
*chr_read
[MAX_MUX
];
203 IOEventHandler
*chr_event
[MAX_MUX
];
204 void *ext_opaque
[MAX_MUX
];
205 CharDriverState
*drv
;
206 unsigned char buffer
[MUX_BUFFER_SIZE
];
215 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
217 MuxDriver
*d
= chr
->opaque
;
219 if (!term_timestamps
) {
220 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
225 for(i
= 0; i
< len
; i
++) {
226 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
227 if (buf
[i
] == '\n') {
232 ti
= qemu_get_clock(rt_clock
);
233 if (term_timestamps_start
== -1)
234 term_timestamps_start
= ti
;
235 ti
-= term_timestamps_start
;
236 secs
= ti
/ 1000000000;
237 snprintf(buf1
, sizeof(buf1
),
238 "[%02d:%02d:%02d.%03d] ",
242 (int)((ti
/ 1000000) % 1000));
243 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
250 static const char * const mux_help
[] = {
251 "% h print this help\n\r",
252 "% x exit emulator\n\r",
253 "% s save disk data back to file (if -snapshot)\n\r",
254 "% t toggle console timestamps\n\r"
255 "% b send break (magic sysrq)\n\r",
256 "% c switch between console and monitor\n\r",
261 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
262 static void mux_print_help(CharDriverState
*chr
)
265 char ebuf
[15] = "Escape-Char";
266 char cbuf
[50] = "\n\r";
268 if (term_escape_char
> 0 && term_escape_char
< 26) {
269 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
270 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
272 snprintf(cbuf
, sizeof(cbuf
),
273 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
276 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
277 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
278 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
279 if (mux_help
[i
][j
] == '%')
280 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
282 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
287 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
289 if (d
->term_got_escape
) {
290 d
->term_got_escape
= 0;
291 if (ch
== term_escape_char
)
300 const char *term
= "QEMU: Terminated\n\r";
301 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
308 for (i
= 0; i
< nb_drives
; i
++) {
309 bdrv_commit(drives_table
[i
].bdrv
);
314 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
317 /* Switch to the next registered device */
319 if (chr
->focus
>= d
->mux_cnt
)
323 term_timestamps
= !term_timestamps
;
324 term_timestamps_start
= -1;
327 } else if (ch
== term_escape_char
) {
328 d
->term_got_escape
= 1;
336 static void mux_chr_accept_input(CharDriverState
*chr
)
339 MuxDriver
*d
= chr
->opaque
;
341 while (d
->prod
!= d
->cons
&&
342 d
->chr_can_read
[m
] &&
343 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
344 d
->chr_read
[m
](d
->ext_opaque
[m
],
345 &d
->buffer
[d
->cons
++ & MUX_BUFFER_MASK
], 1);
349 static int mux_chr_can_read(void *opaque
)
351 CharDriverState
*chr
= opaque
;
352 MuxDriver
*d
= chr
->opaque
;
354 if ((d
->prod
- d
->cons
) < MUX_BUFFER_SIZE
)
356 if (d
->chr_can_read
[chr
->focus
])
357 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
361 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
363 CharDriverState
*chr
= opaque
;
364 MuxDriver
*d
= chr
->opaque
;
368 mux_chr_accept_input (opaque
);
370 for(i
= 0; i
< size
; i
++)
371 if (mux_proc_byte(chr
, d
, buf
[i
])) {
372 if (d
->prod
== d
->cons
&&
373 d
->chr_can_read
[m
] &&
374 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
375 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
377 d
->buffer
[d
->prod
++ & MUX_BUFFER_MASK
] = buf
[i
];
381 static void mux_chr_event(void *opaque
, int event
)
383 CharDriverState
*chr
= opaque
;
384 MuxDriver
*d
= chr
->opaque
;
387 /* Send the event to all registered listeners */
388 for (i
= 0; i
< d
->mux_cnt
; i
++)
390 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
393 static void mux_chr_update_read_handler(CharDriverState
*chr
)
395 MuxDriver
*d
= chr
->opaque
;
397 if (d
->mux_cnt
>= MAX_MUX
) {
398 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
401 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
402 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
403 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
404 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
405 /* Fix up the real driver with mux routines */
406 if (d
->mux_cnt
== 0) {
407 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
410 chr
->focus
= d
->mux_cnt
;
414 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
416 CharDriverState
*chr
;
419 chr
= qemu_mallocz(sizeof(CharDriverState
));
422 d
= qemu_mallocz(sizeof(MuxDriver
));
431 chr
->chr_write
= mux_chr_write
;
432 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
433 chr
->chr_accept_input
= mux_chr_accept_input
;
439 int send_all(int fd
, const uint8_t *buf
, int len1
)
445 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 uint8_t *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
));
574 s
= qemu_mallocz(sizeof(FDCharDriver
));
582 chr
->chr_write
= fd_chr_write
;
583 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
584 chr
->chr_close
= fd_chr_close
;
591 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
595 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
598 return qemu_chr_open_fd(-1, fd_out
);
601 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
604 char filename_in
[256], filename_out
[256];
606 snprintf(filename_in
, 256, "%s.in", filename
);
607 snprintf(filename_out
, 256, "%s.out", filename
);
608 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
609 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
610 if (fd_in
< 0 || fd_out
< 0) {
615 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
619 return qemu_chr_open_fd(fd_in
, fd_out
);
623 /* for STDIO, we handle the case where several clients use it
626 #define TERM_FIFO_MAX_SIZE 1
628 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
629 static int term_fifo_size
;
631 static int stdio_read_poll(void *opaque
)
633 CharDriverState
*chr
= opaque
;
635 /* try to flush the queue if needed */
636 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
637 qemu_chr_read(chr
, term_fifo
, 1);
640 /* see if we can absorb more chars */
641 if (term_fifo_size
== 0)
647 static void stdio_read(void *opaque
)
651 CharDriverState
*chr
= opaque
;
653 size
= read(0, buf
, 1);
655 /* stdin has been closed. Remove it from the active list. */
656 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
660 if (qemu_chr_can_read(chr
) > 0) {
661 qemu_chr_read(chr
, buf
, 1);
662 } else if (term_fifo_size
== 0) {
663 term_fifo
[term_fifo_size
++] = buf
[0];
668 /* init terminal so that we can grab keys */
669 static struct termios oldtty
;
670 static int old_fd0_flags
;
671 static int term_atexit_done
;
673 static void term_exit(void)
675 tcsetattr (0, TCSANOW
, &oldtty
);
676 fcntl(0, F_SETFL
, old_fd0_flags
);
679 static void term_init(void)
685 old_fd0_flags
= fcntl(0, F_GETFL
);
687 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
688 |INLCR
|IGNCR
|ICRNL
|IXON
);
689 tty
.c_oflag
|= OPOST
;
690 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
691 /* if graphical mode, we allow Ctrl-C handling */
693 tty
.c_lflag
&= ~ISIG
;
694 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
699 tcsetattr (0, TCSANOW
, &tty
);
701 if (!term_atexit_done
++)
704 fcntl(0, F_SETFL
, O_NONBLOCK
);
707 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
711 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
715 static CharDriverState
*qemu_chr_open_stdio(void)
717 CharDriverState
*chr
;
719 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
721 chr
= qemu_chr_open_fd(0, 1);
722 chr
->chr_close
= qemu_chr_close_stdio
;
723 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
731 /* Once Solaris has openpty(), this is going to be removed. */
732 int openpty(int *amaster
, int *aslave
, char *name
,
733 struct termios
*termp
, struct winsize
*winp
)
736 int mfd
= -1, sfd
= -1;
738 *amaster
= *aslave
= -1;
740 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
744 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
747 if ((slave
= ptsname(mfd
)) == NULL
)
750 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
753 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
754 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
762 ioctl(sfd
, TIOCSWINSZ
, winp
);
773 void cfmakeraw (struct termios
*termios_p
)
775 termios_p
->c_iflag
&=
776 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
777 termios_p
->c_oflag
&= ~OPOST
;
778 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
779 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
780 termios_p
->c_cflag
|= CS8
;
782 termios_p
->c_cc
[VMIN
] = 0;
783 termios_p
->c_cc
[VTIME
] = 0;
787 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
788 || defined(__NetBSD__) || defined(__OpenBSD__)
798 static void pty_chr_update_read_handler(CharDriverState
*chr
);
799 static void pty_chr_state(CharDriverState
*chr
, int connected
);
801 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
803 PtyCharDriver
*s
= chr
->opaque
;
806 /* guest sends data, check for (re-)connect */
807 pty_chr_update_read_handler(chr
);
810 return send_all(s
->fd
, buf
, len
);
813 static int pty_chr_read_poll(void *opaque
)
815 CharDriverState
*chr
= opaque
;
816 PtyCharDriver
*s
= chr
->opaque
;
818 s
->read_bytes
= qemu_chr_can_read(chr
);
819 return s
->read_bytes
;
822 static void pty_chr_read(void *opaque
)
824 CharDriverState
*chr
= opaque
;
825 PtyCharDriver
*s
= chr
->opaque
;
830 if (len
> s
->read_bytes
)
834 size
= read(s
->fd
, buf
, len
);
835 if ((size
== -1 && errno
== EIO
) ||
837 pty_chr_state(chr
, 0);
841 pty_chr_state(chr
, 1);
842 qemu_chr_read(chr
, buf
, size
);
846 static void pty_chr_update_read_handler(CharDriverState
*chr
)
848 PtyCharDriver
*s
= chr
->opaque
;
850 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
851 pty_chr_read
, NULL
, chr
);
854 * Short timeout here: just need wait long enougth that qemu makes
855 * it through the poll loop once. When reconnected we want a
856 * short timeout so we notice it almost instantly. Otherwise
857 * read() gives us -EIO instantly, making pty_chr_state() reset the
858 * timeout to the normal (much longer) poll interval before the
861 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
864 static void pty_chr_state(CharDriverState
*chr
, int connected
)
866 PtyCharDriver
*s
= chr
->opaque
;
869 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
872 /* (re-)connect poll interval for idle guests: once per second.
873 * We check more frequently in case the guests sends data to
874 * the virtual device linked to our pty. */
875 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
883 static void pty_chr_timer(void *opaque
)
885 struct CharDriverState
*chr
= opaque
;
886 PtyCharDriver
*s
= chr
->opaque
;
891 /* If we arrive here without polling being cleared due
892 * read returning -EIO, then we are (re-)connected */
893 pty_chr_state(chr
, 1);
898 pty_chr_update_read_handler(chr
);
901 static void pty_chr_close(struct CharDriverState
*chr
)
903 PtyCharDriver
*s
= chr
->opaque
;
905 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
910 static CharDriverState
*qemu_chr_open_pty(void)
912 CharDriverState
*chr
;
916 #if defined(__OpenBSD__)
917 char pty_name
[PATH_MAX
];
918 #define q_ptsname(x) pty_name
920 char *pty_name
= NULL
;
921 #define q_ptsname(x) ptsname(x)
924 chr
= qemu_mallocz(sizeof(CharDriverState
));
927 s
= qemu_mallocz(sizeof(PtyCharDriver
));
933 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
937 /* Set raw attributes on the pty. */
939 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
942 len
= strlen(q_ptsname(s
->fd
)) + 5;
943 chr
->filename
= qemu_malloc(len
);
944 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
945 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
948 chr
->chr_write
= pty_chr_write
;
949 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
950 chr
->chr_close
= pty_chr_close
;
952 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
957 static void tty_serial_init(int fd
, int speed
,
958 int parity
, int data_bits
, int stop_bits
)
964 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
965 speed
, parity
, data_bits
, stop_bits
);
967 tcgetattr (fd
, &tty
);
970 if (speed
<= 50 * MARGIN
)
972 else if (speed
<= 75 * MARGIN
)
974 else if (speed
<= 300 * MARGIN
)
976 else if (speed
<= 600 * MARGIN
)
978 else if (speed
<= 1200 * MARGIN
)
980 else if (speed
<= 2400 * MARGIN
)
982 else if (speed
<= 4800 * MARGIN
)
984 else if (speed
<= 9600 * MARGIN
)
986 else if (speed
<= 19200 * MARGIN
)
988 else if (speed
<= 38400 * MARGIN
)
990 else if (speed
<= 57600 * MARGIN
)
992 else if (speed
<= 115200 * MARGIN
)
997 cfsetispeed(&tty
, spd
);
998 cfsetospeed(&tty
, spd
);
1000 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1001 |INLCR
|IGNCR
|ICRNL
|IXON
);
1002 tty
.c_oflag
|= OPOST
;
1003 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1004 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1025 tty
.c_cflag
|= PARENB
;
1028 tty
.c_cflag
|= PARENB
| PARODD
;
1032 tty
.c_cflag
|= CSTOPB
;
1034 tcsetattr (fd
, TCSANOW
, &tty
);
1037 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1039 FDCharDriver
*s
= chr
->opaque
;
1042 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1044 QEMUSerialSetParams
*ssp
= arg
;
1045 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1046 ssp
->data_bits
, ssp
->stop_bits
);
1049 case CHR_IOCTL_SERIAL_SET_BREAK
:
1051 int enable
= *(int *)arg
;
1053 tcsendbreak(s
->fd_in
, 1);
1056 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1059 int *targ
= (int *)arg
;
1060 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1062 if (sarg
| TIOCM_CTS
)
1063 *targ
|= CHR_TIOCM_CTS
;
1064 if (sarg
| TIOCM_CAR
)
1065 *targ
|= CHR_TIOCM_CAR
;
1066 if (sarg
| TIOCM_DSR
)
1067 *targ
|= CHR_TIOCM_DSR
;
1068 if (sarg
| TIOCM_RI
)
1069 *targ
|= CHR_TIOCM_RI
;
1070 if (sarg
| TIOCM_DTR
)
1071 *targ
|= CHR_TIOCM_DTR
;
1072 if (sarg
| TIOCM_RTS
)
1073 *targ
|= CHR_TIOCM_RTS
;
1076 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1078 int sarg
= *(int *)arg
;
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
));
1246 drv
->mode
= IEEE1284_MODE_COMPAT
;
1248 chr
= qemu_mallocz(sizeof(CharDriverState
));
1254 chr
->chr_write
= null_chr_write
;
1255 chr
->chr_ioctl
= pp_ioctl
;
1256 chr
->chr_close
= pp_close
;
1259 qemu_chr_reset(chr
);
1263 #endif /* __linux__ */
1269 HANDLE hcom
, hrecv
, hsend
;
1270 OVERLAPPED orecv
, osend
;
1275 #define NSENDBUF 2048
1276 #define NRECVBUF 2048
1277 #define MAXCONNECT 1
1278 #define NTIMEOUT 5000
1280 static int win_chr_poll(void *opaque
);
1281 static int win_chr_pipe_poll(void *opaque
);
1283 static void win_chr_close(CharDriverState
*chr
)
1285 WinCharState
*s
= chr
->opaque
;
1288 CloseHandle(s
->hsend
);
1292 CloseHandle(s
->hrecv
);
1296 CloseHandle(s
->hcom
);
1300 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1302 qemu_del_polling_cb(win_chr_poll
, chr
);
1305 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1307 WinCharState
*s
= chr
->opaque
;
1309 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1314 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1316 fprintf(stderr
, "Failed CreateEvent\n");
1319 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1321 fprintf(stderr
, "Failed CreateEvent\n");
1325 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1326 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1327 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1328 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1333 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1334 fprintf(stderr
, "Failed SetupComm\n");
1338 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1339 size
= sizeof(COMMCONFIG
);
1340 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1341 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1342 CommConfigDialog(filename
, NULL
, &comcfg
);
1344 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1345 fprintf(stderr
, "Failed SetCommState\n");
1349 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1350 fprintf(stderr
, "Failed SetCommMask\n");
1354 cto
.ReadIntervalTimeout
= MAXDWORD
;
1355 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1356 fprintf(stderr
, "Failed SetCommTimeouts\n");
1360 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1361 fprintf(stderr
, "Failed ClearCommError\n");
1364 qemu_add_polling_cb(win_chr_poll
, chr
);
1372 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1374 WinCharState
*s
= chr
->opaque
;
1375 DWORD len
, ret
, size
, err
;
1378 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1379 s
->osend
.hEvent
= s
->hsend
;
1382 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1384 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1386 err
= GetLastError();
1387 if (err
== ERROR_IO_PENDING
) {
1388 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1406 static int win_chr_read_poll(CharDriverState
*chr
)
1408 WinCharState
*s
= chr
->opaque
;
1410 s
->max_size
= qemu_chr_can_read(chr
);
1414 static void win_chr_readfile(CharDriverState
*chr
)
1416 WinCharState
*s
= chr
->opaque
;
1421 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1422 s
->orecv
.hEvent
= s
->hrecv
;
1423 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1425 err
= GetLastError();
1426 if (err
== ERROR_IO_PENDING
) {
1427 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1432 qemu_chr_read(chr
, buf
, size
);
1436 static void win_chr_read(CharDriverState
*chr
)
1438 WinCharState
*s
= chr
->opaque
;
1440 if (s
->len
> s
->max_size
)
1441 s
->len
= s
->max_size
;
1445 win_chr_readfile(chr
);
1448 static int win_chr_poll(void *opaque
)
1450 CharDriverState
*chr
= opaque
;
1451 WinCharState
*s
= chr
->opaque
;
1455 ClearCommError(s
->hcom
, &comerr
, &status
);
1456 if (status
.cbInQue
> 0) {
1457 s
->len
= status
.cbInQue
;
1458 win_chr_read_poll(chr
);
1465 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1467 CharDriverState
*chr
;
1470 chr
= qemu_mallocz(sizeof(CharDriverState
));
1473 s
= qemu_mallocz(sizeof(WinCharState
));
1479 chr
->chr_write
= win_chr_write
;
1480 chr
->chr_close
= win_chr_close
;
1482 if (win_chr_init(chr
, filename
) < 0) {
1487 qemu_chr_reset(chr
);
1491 static int win_chr_pipe_poll(void *opaque
)
1493 CharDriverState
*chr
= opaque
;
1494 WinCharState
*s
= chr
->opaque
;
1497 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1500 win_chr_read_poll(chr
);
1507 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1509 WinCharState
*s
= chr
->opaque
;
1517 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1519 fprintf(stderr
, "Failed CreateEvent\n");
1522 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1524 fprintf(stderr
, "Failed CreateEvent\n");
1528 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1529 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1530 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1532 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1533 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1534 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1539 ZeroMemory(&ov
, sizeof(ov
));
1540 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1541 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1543 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1547 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1549 fprintf(stderr
, "Failed GetOverlappedResult\n");
1551 CloseHandle(ov
.hEvent
);
1558 CloseHandle(ov
.hEvent
);
1561 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1570 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1572 CharDriverState
*chr
;
1575 chr
= qemu_mallocz(sizeof(CharDriverState
));
1578 s
= qemu_mallocz(sizeof(WinCharState
));
1584 chr
->chr_write
= win_chr_write
;
1585 chr
->chr_close
= win_chr_close
;
1587 if (win_chr_pipe_init(chr
, filename
) < 0) {
1592 qemu_chr_reset(chr
);
1596 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1598 CharDriverState
*chr
;
1601 chr
= qemu_mallocz(sizeof(CharDriverState
));
1604 s
= qemu_mallocz(sizeof(WinCharState
));
1611 chr
->chr_write
= win_chr_write
;
1612 qemu_chr_reset(chr
);
1616 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1618 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1621 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1625 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1626 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1627 if (fd_out
== INVALID_HANDLE_VALUE
)
1630 return qemu_chr_open_win_file(fd_out
);
1632 #endif /* !_WIN32 */
1634 /***********************************************************/
1635 /* UDP Net console */
1639 struct sockaddr_in daddr
;
1646 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1648 NetCharDriver
*s
= chr
->opaque
;
1650 return sendto(s
->fd
, buf
, len
, 0,
1651 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1654 static int udp_chr_read_poll(void *opaque
)
1656 CharDriverState
*chr
= opaque
;
1657 NetCharDriver
*s
= chr
->opaque
;
1659 s
->max_size
= qemu_chr_can_read(chr
);
1661 /* If there were any stray characters in the queue process them
1664 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1665 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1667 s
->max_size
= qemu_chr_can_read(chr
);
1672 static void udp_chr_read(void *opaque
)
1674 CharDriverState
*chr
= opaque
;
1675 NetCharDriver
*s
= chr
->opaque
;
1677 if (s
->max_size
== 0)
1679 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1680 s
->bufptr
= s
->bufcnt
;
1685 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1686 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1688 s
->max_size
= qemu_chr_can_read(chr
);
1692 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1694 NetCharDriver
*s
= chr
->opaque
;
1697 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1698 udp_chr_read
, NULL
, chr
);
1702 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1704 CharDriverState
*chr
= NULL
;
1705 NetCharDriver
*s
= NULL
;
1707 struct sockaddr_in saddr
;
1709 chr
= qemu_mallocz(sizeof(CharDriverState
));
1712 s
= qemu_mallocz(sizeof(NetCharDriver
));
1716 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1718 perror("socket(PF_INET, SOCK_DGRAM)");
1722 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1723 printf("Could not parse: %s\n", def
);
1727 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1737 chr
->chr_write
= udp_chr_write
;
1738 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1751 /***********************************************************/
1752 /* TCP Net console */
1763 static void tcp_chr_accept(void *opaque
);
1765 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1767 TCPCharDriver
*s
= chr
->opaque
;
1769 return send_all(s
->fd
, buf
, len
);
1771 /* XXX: indicate an error ? */
1776 static int tcp_chr_read_poll(void *opaque
)
1778 CharDriverState
*chr
= opaque
;
1779 TCPCharDriver
*s
= chr
->opaque
;
1782 s
->max_size
= qemu_chr_can_read(chr
);
1787 #define IAC_BREAK 243
1788 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1790 uint8_t *buf
, int *size
)
1792 /* Handle any telnet client's basic IAC options to satisfy char by
1793 * char mode with no echo. All IAC options will be removed from
1794 * the buf and the do_telnetopt variable will be used to track the
1795 * state of the width of the IAC information.
1797 * IAC commands come in sets of 3 bytes with the exception of the
1798 * "IAC BREAK" command and the double IAC.
1804 for (i
= 0; i
< *size
; i
++) {
1805 if (s
->do_telnetopt
> 1) {
1806 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1807 /* Double IAC means send an IAC */
1811 s
->do_telnetopt
= 1;
1813 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1814 /* Handle IAC break commands by sending a serial break */
1815 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1820 if (s
->do_telnetopt
>= 4) {
1821 s
->do_telnetopt
= 1;
1824 if ((unsigned char)buf
[i
] == IAC
) {
1825 s
->do_telnetopt
= 2;
1836 static void tcp_chr_read(void *opaque
)
1838 CharDriverState
*chr
= opaque
;
1839 TCPCharDriver
*s
= chr
->opaque
;
1843 if (!s
->connected
|| s
->max_size
<= 0)
1846 if (len
> s
->max_size
)
1848 size
= recv(s
->fd
, buf
, len
, 0);
1850 /* connection closed */
1852 if (s
->listen_fd
>= 0) {
1853 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1855 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1858 } else if (size
> 0) {
1859 if (s
->do_telnetopt
)
1860 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1862 qemu_chr_read(chr
, buf
, size
);
1866 static void tcp_chr_connect(void *opaque
)
1868 CharDriverState
*chr
= opaque
;
1869 TCPCharDriver
*s
= chr
->opaque
;
1872 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1873 tcp_chr_read
, NULL
, chr
);
1874 qemu_chr_reset(chr
);
1877 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1878 static void tcp_chr_telnet_init(int fd
)
1881 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1882 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1883 send(fd
, (char *)buf
, 3, 0);
1884 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1885 send(fd
, (char *)buf
, 3, 0);
1886 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1887 send(fd
, (char *)buf
, 3, 0);
1888 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1889 send(fd
, (char *)buf
, 3, 0);
1892 static void socket_set_nodelay(int fd
)
1895 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1898 static void tcp_chr_accept(void *opaque
)
1900 CharDriverState
*chr
= opaque
;
1901 TCPCharDriver
*s
= chr
->opaque
;
1902 struct sockaddr_in saddr
;
1904 struct sockaddr_un uaddr
;
1906 struct sockaddr
*addr
;
1913 len
= sizeof(uaddr
);
1914 addr
= (struct sockaddr
*)&uaddr
;
1918 len
= sizeof(saddr
);
1919 addr
= (struct sockaddr
*)&saddr
;
1921 fd
= accept(s
->listen_fd
, addr
, &len
);
1922 if (fd
< 0 && errno
!= EINTR
) {
1924 } else if (fd
>= 0) {
1925 if (s
->do_telnetopt
)
1926 tcp_chr_telnet_init(fd
);
1930 socket_set_nonblock(fd
);
1932 socket_set_nodelay(fd
);
1934 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
1935 tcp_chr_connect(chr
);
1938 static void tcp_chr_close(CharDriverState
*chr
)
1940 TCPCharDriver
*s
= chr
->opaque
;
1943 if (s
->listen_fd
>= 0)
1944 closesocket(s
->listen_fd
);
1948 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
1952 CharDriverState
*chr
= NULL
;
1953 TCPCharDriver
*s
= NULL
;
1954 int fd
= -1, ret
, err
, val
;
1956 int is_waitconnect
= 1;
1959 struct sockaddr_in saddr
;
1961 struct sockaddr_un uaddr
;
1963 struct sockaddr
*addr
;
1968 addr
= (struct sockaddr
*)&uaddr
;
1969 addrlen
= sizeof(uaddr
);
1970 if (parse_unix_path(&uaddr
, host_str
) < 0)
1975 addr
= (struct sockaddr
*)&saddr
;
1976 addrlen
= sizeof(saddr
);
1977 if (parse_host_port(&saddr
, host_str
) < 0)
1982 while((ptr
= strchr(ptr
,','))) {
1984 if (!strncmp(ptr
,"server",6)) {
1986 } else if (!strncmp(ptr
,"nowait",6)) {
1988 } else if (!strncmp(ptr
,"nodelay",6)) {
1991 printf("Unknown option: %s\n", ptr
);
1998 chr
= qemu_mallocz(sizeof(CharDriverState
));
2001 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2007 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2010 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2015 if (!is_waitconnect
)
2016 socket_set_nonblock(fd
);
2021 s
->is_unix
= is_unix
;
2022 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2025 chr
->chr_write
= tcp_chr_write
;
2026 chr
->chr_close
= tcp_chr_close
;
2029 /* allow fast reuse */
2033 pstrcpy(path
, sizeof(path
), uaddr
.sun_path
);
2039 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2042 ret
= bind(fd
, addr
, addrlen
);
2046 ret
= listen(fd
, 0);
2051 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2053 s
->do_telnetopt
= 1;
2056 ret
= connect(fd
, addr
, addrlen
);
2058 err
= socket_error();
2059 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2060 } else if (err
== EINPROGRESS
) {
2063 } else if (err
== WSAEALREADY
) {
2075 socket_set_nodelay(fd
);
2077 tcp_chr_connect(chr
);
2079 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2082 if (is_listen
&& is_waitconnect
) {
2083 printf("QEMU waiting for connection on: %s\n", host_str
);
2084 tcp_chr_accept(chr
);
2085 socket_set_nonblock(s
->listen_fd
);
2097 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
2098 = TAILQ_HEAD_INITIALIZER(chardevs
);
2100 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
)
2103 CharDriverState
*chr
;
2105 if (!strcmp(filename
, "vc")) {
2106 chr
= text_console_init(&display_state
, 0);
2108 if (strstart(filename
, "vc:", &p
)) {
2109 chr
= text_console_init(&display_state
, p
);
2111 if (!strcmp(filename
, "null")) {
2112 chr
= qemu_chr_open_null();
2114 if (strstart(filename
, "tcp:", &p
)) {
2115 chr
= qemu_chr_open_tcp(p
, 0, 0);
2117 if (strstart(filename
, "telnet:", &p
)) {
2118 chr
= qemu_chr_open_tcp(p
, 1, 0);
2120 if (strstart(filename
, "udp:", &p
)) {
2121 chr
= qemu_chr_open_udp(p
);
2123 if (strstart(filename
, "mon:", &p
)) {
2124 chr
= qemu_chr_open(label
, p
);
2126 chr
= qemu_chr_open_mux(chr
);
2127 monitor_init(chr
, !nographic
);
2129 printf("Unable to open driver: %s\n", p
);
2133 if (strstart(filename
, "unix:", &p
)) {
2134 chr
= qemu_chr_open_tcp(p
, 0, 1);
2135 } else if (strstart(filename
, "file:", &p
)) {
2136 chr
= qemu_chr_open_file_out(p
);
2137 } else if (strstart(filename
, "pipe:", &p
)) {
2138 chr
= qemu_chr_open_pipe(p
);
2139 } else if (!strcmp(filename
, "pty")) {
2140 chr
= qemu_chr_open_pty();
2141 } else if (!strcmp(filename
, "stdio")) {
2142 chr
= qemu_chr_open_stdio();
2144 #if defined(__linux__)
2145 if (strstart(filename
, "/dev/parport", NULL
)) {
2146 chr
= qemu_chr_open_pp(filename
);
2149 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2150 || defined(__NetBSD__) || defined(__OpenBSD__)
2151 if (strstart(filename
, "/dev/", NULL
)) {
2152 chr
= qemu_chr_open_tty(filename
);
2156 if (strstart(filename
, "COM", NULL
)) {
2157 chr
= qemu_chr_open_win(filename
);
2159 if (strstart(filename
, "pipe:", &p
)) {
2160 chr
= qemu_chr_open_win_pipe(p
);
2162 if (strstart(filename
, "con:", NULL
)) {
2163 chr
= qemu_chr_open_win_con(filename
);
2165 if (strstart(filename
, "file:", &p
)) {
2166 chr
= qemu_chr_open_win_file_out(p
);
2169 #ifdef CONFIG_BRLAPI
2170 if (!strcmp(filename
, "braille")) {
2171 chr
= chr_baum_init();
2180 chr
->filename
= qemu_strdup(filename
);
2181 chr
->label
= qemu_strdup(label
);
2182 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2187 void qemu_chr_close(CharDriverState
*chr
)
2189 TAILQ_REMOVE(&chardevs
, chr
, next
);
2191 chr
->chr_close(chr
);
2192 qemu_free(chr
->filename
);
2193 qemu_free(chr
->label
);
2197 void qemu_chr_info(void)
2199 CharDriverState
*chr
;
2201 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2202 term_printf("%s: filename=%s\n", chr
->label
, chr
->filename
);