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"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
50 #include <sys/select.h>
51 #include <arpa/inet.h>
54 #if !defined(__APPLE__) && !defined(__OpenBSD__)
60 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
61 #include <freebsd/stdlib.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
88 #include "qemu_socket.h"
90 /***********************************************************/
91 /* character device */
93 static void qemu_chr_event(CharDriverState
*s
, int event
)
97 s
->chr_event(s
->handler_opaque
, event
);
100 static void qemu_chr_reset_bh(void *opaque
)
102 CharDriverState
*s
= opaque
;
103 qemu_chr_event(s
, CHR_EVENT_RESET
);
104 qemu_bh_delete(s
->bh
);
108 void qemu_chr_reset(CharDriverState
*s
)
111 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
112 qemu_bh_schedule(s
->bh
);
116 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
118 return s
->chr_write(s
, buf
, len
);
121 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
125 return s
->chr_ioctl(s
, cmd
, arg
);
128 int qemu_chr_can_read(CharDriverState
*s
)
130 if (!s
->chr_can_read
)
132 return s
->chr_can_read(s
->handler_opaque
);
135 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
137 s
->chr_read(s
->handler_opaque
, buf
, len
);
140 void qemu_chr_accept_input(CharDriverState
*s
)
142 if (s
->chr_accept_input
)
143 s
->chr_accept_input(s
);
146 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
151 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
152 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
156 void qemu_chr_send_event(CharDriverState
*s
, int event
)
158 if (s
->chr_send_event
)
159 s
->chr_send_event(s
, event
);
162 void qemu_chr_add_handlers(CharDriverState
*s
,
163 IOCanRWHandler
*fd_can_read
,
164 IOReadHandler
*fd_read
,
165 IOEventHandler
*fd_event
,
168 s
->chr_can_read
= fd_can_read
;
169 s
->chr_read
= fd_read
;
170 s
->chr_event
= fd_event
;
171 s
->handler_opaque
= opaque
;
172 if (s
->chr_update_read_handler
)
173 s
->chr_update_read_handler(s
);
176 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
181 static CharDriverState
*qemu_chr_open_null(void)
183 CharDriverState
*chr
;
185 chr
= qemu_mallocz(sizeof(CharDriverState
));
188 chr
->chr_write
= null_chr_write
;
192 /* MUX driver for serial I/O splitting */
193 static int term_timestamps
;
194 static int64_t term_timestamps_start
;
196 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
197 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
199 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
200 IOReadHandler
*chr_read
[MAX_MUX
];
201 IOEventHandler
*chr_event
[MAX_MUX
];
202 void *ext_opaque
[MAX_MUX
];
203 CharDriverState
*drv
;
204 unsigned char buffer
[MUX_BUFFER_SIZE
];
213 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
215 MuxDriver
*d
= chr
->opaque
;
217 if (!term_timestamps
) {
218 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
223 for(i
= 0; i
< len
; i
++) {
224 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
225 if (buf
[i
] == '\n') {
230 ti
= qemu_get_clock(rt_clock
);
231 if (term_timestamps_start
== -1)
232 term_timestamps_start
= ti
;
233 ti
-= term_timestamps_start
;
234 secs
= ti
/ 1000000000;
235 snprintf(buf1
, sizeof(buf1
),
236 "[%02d:%02d:%02d.%03d] ",
240 (int)((ti
/ 1000000) % 1000));
241 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
248 static const char * const mux_help
[] = {
249 "% h print this help\n\r",
250 "% x exit emulator\n\r",
251 "% s save disk data back to file (if -snapshot)\n\r",
252 "% t toggle console timestamps\n\r"
253 "% b send break (magic sysrq)\n\r",
254 "% c switch between console and monitor\n\r",
259 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
260 static void mux_print_help(CharDriverState
*chr
)
263 char ebuf
[15] = "Escape-Char";
264 char cbuf
[50] = "\n\r";
266 if (term_escape_char
> 0 && term_escape_char
< 26) {
267 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
268 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
270 snprintf(cbuf
, sizeof(cbuf
),
271 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
274 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
275 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
276 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
277 if (mux_help
[i
][j
] == '%')
278 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
280 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
285 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
287 if (d
->term_got_escape
) {
288 d
->term_got_escape
= 0;
289 if (ch
== term_escape_char
)
298 const char *term
= "QEMU: Terminated\n\r";
299 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
306 for (i
= 0; i
< nb_drives
; i
++) {
307 bdrv_commit(drives_table
[i
].bdrv
);
312 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
315 /* Switch to the next registered device */
317 if (chr
->focus
>= d
->mux_cnt
)
321 term_timestamps
= !term_timestamps
;
322 term_timestamps_start
= -1;
325 } else if (ch
== term_escape_char
) {
326 d
->term_got_escape
= 1;
334 static void mux_chr_accept_input(CharDriverState
*chr
)
337 MuxDriver
*d
= chr
->opaque
;
339 while (d
->prod
!= d
->cons
&&
340 d
->chr_can_read
[m
] &&
341 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
342 d
->chr_read
[m
](d
->ext_opaque
[m
],
343 &d
->buffer
[d
->cons
++ & MUX_BUFFER_MASK
], 1);
347 static int mux_chr_can_read(void *opaque
)
349 CharDriverState
*chr
= opaque
;
350 MuxDriver
*d
= chr
->opaque
;
352 if ((d
->prod
- d
->cons
) < MUX_BUFFER_SIZE
)
354 if (d
->chr_can_read
[chr
->focus
])
355 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
359 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
361 CharDriverState
*chr
= opaque
;
362 MuxDriver
*d
= chr
->opaque
;
366 mux_chr_accept_input (opaque
);
368 for(i
= 0; i
< size
; i
++)
369 if (mux_proc_byte(chr
, d
, buf
[i
])) {
370 if (d
->prod
== d
->cons
&&
371 d
->chr_can_read
[m
] &&
372 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
373 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
375 d
->buffer
[d
->prod
++ & MUX_BUFFER_MASK
] = buf
[i
];
379 static void mux_chr_event(void *opaque
, int event
)
381 CharDriverState
*chr
= opaque
;
382 MuxDriver
*d
= chr
->opaque
;
385 /* Send the event to all registered listeners */
386 for (i
= 0; i
< d
->mux_cnt
; i
++)
388 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
391 static void mux_chr_update_read_handler(CharDriverState
*chr
)
393 MuxDriver
*d
= chr
->opaque
;
395 if (d
->mux_cnt
>= MAX_MUX
) {
396 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
399 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
400 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
401 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
402 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
403 /* Fix up the real driver with mux routines */
404 if (d
->mux_cnt
== 0) {
405 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
408 chr
->focus
= d
->mux_cnt
;
412 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
414 CharDriverState
*chr
;
417 chr
= qemu_mallocz(sizeof(CharDriverState
));
420 d
= qemu_mallocz(sizeof(MuxDriver
));
429 chr
->chr_write
= mux_chr_write
;
430 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
431 chr
->chr_accept_input
= mux_chr_accept_input
;
437 int send_all(int fd
, const uint8_t *buf
, int len1
)
443 ret
= send(fd
, buf
, len
, 0);
446 errno
= WSAGetLastError();
447 if (errno
!= WSAEWOULDBLOCK
) {
450 } else if (ret
== 0) {
462 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
468 ret
= write(fd
, buf
, len
);
470 if (errno
!= EINTR
&& errno
!= EAGAIN
)
472 } else if (ret
== 0) {
482 inline int send_all(int fd
, const uint8_t *buf
, int len1
)
484 return unix_write(fd
, buf
, len1
);
495 #define STDIO_MAX_CLIENTS 1
496 static int stdio_nb_clients
= 0;
498 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
500 FDCharDriver
*s
= chr
->opaque
;
501 return send_all(s
->fd_out
, buf
, len
);
504 static int fd_chr_read_poll(void *opaque
)
506 CharDriverState
*chr
= opaque
;
507 FDCharDriver
*s
= chr
->opaque
;
509 s
->max_size
= qemu_chr_can_read(chr
);
513 static void fd_chr_read(void *opaque
)
515 CharDriverState
*chr
= opaque
;
516 FDCharDriver
*s
= chr
->opaque
;
521 if (len
> s
->max_size
)
525 size
= read(s
->fd_in
, buf
, len
);
527 /* FD has been closed. Remove it from the active list. */
528 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
532 qemu_chr_read(chr
, buf
, size
);
536 static void fd_chr_update_read_handler(CharDriverState
*chr
)
538 FDCharDriver
*s
= chr
->opaque
;
541 if (nographic
&& s
->fd_in
== 0) {
543 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
544 fd_chr_read
, NULL
, chr
);
549 static void fd_chr_close(struct CharDriverState
*chr
)
551 FDCharDriver
*s
= chr
->opaque
;
554 if (nographic
&& s
->fd_in
== 0) {
556 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
563 /* open a character device to a unix fd */
564 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
566 CharDriverState
*chr
;
569 chr
= qemu_mallocz(sizeof(CharDriverState
));
572 s
= qemu_mallocz(sizeof(FDCharDriver
));
580 chr
->chr_write
= fd_chr_write
;
581 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
582 chr
->chr_close
= fd_chr_close
;
589 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
593 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
596 return qemu_chr_open_fd(-1, fd_out
);
599 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
602 char filename_in
[256], filename_out
[256];
604 snprintf(filename_in
, 256, "%s.in", filename
);
605 snprintf(filename_out
, 256, "%s.out", filename
);
606 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
607 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
608 if (fd_in
< 0 || fd_out
< 0) {
613 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
617 return qemu_chr_open_fd(fd_in
, fd_out
);
621 /* for STDIO, we handle the case where several clients use it
624 #define TERM_FIFO_MAX_SIZE 1
626 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
627 static int term_fifo_size
;
629 static int stdio_read_poll(void *opaque
)
631 CharDriverState
*chr
= opaque
;
633 /* try to flush the queue if needed */
634 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
635 qemu_chr_read(chr
, term_fifo
, 1);
638 /* see if we can absorb more chars */
639 if (term_fifo_size
== 0)
645 static void stdio_read(void *opaque
)
649 CharDriverState
*chr
= opaque
;
651 size
= read(0, buf
, 1);
653 /* stdin has been closed. Remove it from the active list. */
654 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
658 if (qemu_chr_can_read(chr
) > 0) {
659 qemu_chr_read(chr
, buf
, 1);
660 } else if (term_fifo_size
== 0) {
661 term_fifo
[term_fifo_size
++] = buf
[0];
666 /* init terminal so that we can grab keys */
667 static struct termios oldtty
;
668 static int old_fd0_flags
;
669 static int term_atexit_done
;
671 static void term_exit(void)
673 tcsetattr (0, TCSANOW
, &oldtty
);
674 fcntl(0, F_SETFL
, old_fd0_flags
);
677 static void term_init(void)
683 old_fd0_flags
= fcntl(0, F_GETFL
);
685 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
686 |INLCR
|IGNCR
|ICRNL
|IXON
);
687 tty
.c_oflag
|= OPOST
;
688 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
689 /* if graphical mode, we allow Ctrl-C handling */
691 tty
.c_lflag
&= ~ISIG
;
692 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
697 tcsetattr (0, TCSANOW
, &tty
);
699 if (!term_atexit_done
++)
702 fcntl(0, F_SETFL
, O_NONBLOCK
);
705 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
709 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
713 static CharDriverState
*qemu_chr_open_stdio(void)
715 CharDriverState
*chr
;
717 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
719 chr
= qemu_chr_open_fd(0, 1);
720 chr
->chr_close
= qemu_chr_close_stdio
;
721 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
729 /* Once Solaris has openpty(), this is going to be removed. */
730 int openpty(int *amaster
, int *aslave
, char *name
,
731 struct termios
*termp
, struct winsize
*winp
)
734 int mfd
= -1, sfd
= -1;
736 *amaster
= *aslave
= -1;
738 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
742 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
745 if ((slave
= ptsname(mfd
)) == NULL
)
748 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
751 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
752 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
760 ioctl(sfd
, TIOCSWINSZ
, winp
);
771 void cfmakeraw (struct termios
*termios_p
)
773 termios_p
->c_iflag
&=
774 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
775 termios_p
->c_oflag
&= ~OPOST
;
776 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
777 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
778 termios_p
->c_cflag
|= CS8
;
780 termios_p
->c_cc
[VMIN
] = 0;
781 termios_p
->c_cc
[VTIME
] = 0;
785 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
786 || defined(__NetBSD__) || defined(__OpenBSD__)
796 static void pty_chr_update_read_handler(CharDriverState
*chr
);
797 static void pty_chr_state(CharDriverState
*chr
, int connected
);
799 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
801 PtyCharDriver
*s
= chr
->opaque
;
804 /* guest sends data, check for (re-)connect */
805 pty_chr_update_read_handler(chr
);
808 return send_all(s
->fd
, buf
, len
);
811 static int pty_chr_read_poll(void *opaque
)
813 CharDriverState
*chr
= opaque
;
814 PtyCharDriver
*s
= chr
->opaque
;
816 s
->read_bytes
= qemu_chr_can_read(chr
);
817 return s
->read_bytes
;
820 static void pty_chr_read(void *opaque
)
822 CharDriverState
*chr
= opaque
;
823 PtyCharDriver
*s
= chr
->opaque
;
828 if (len
> s
->read_bytes
)
832 size
= read(s
->fd
, buf
, len
);
833 if ((size
== -1 && errno
== EIO
) ||
835 pty_chr_state(chr
, 0);
839 pty_chr_state(chr
, 1);
840 qemu_chr_read(chr
, buf
, size
);
844 static void pty_chr_update_read_handler(CharDriverState
*chr
)
846 PtyCharDriver
*s
= chr
->opaque
;
848 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
849 pty_chr_read
, NULL
, chr
);
852 * Short timeout here: just need wait long enougth that qemu makes
853 * it through the poll loop once. When reconnected we want a
854 * short timeout so we notice it almost instantly. Otherwise
855 * read() gives us -EIO instantly, making pty_chr_state() reset the
856 * timeout to the normal (much longer) poll interval before the
859 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
862 static void pty_chr_state(CharDriverState
*chr
, int connected
)
864 PtyCharDriver
*s
= chr
->opaque
;
867 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
870 /* (re-)connect poll interval for idle guests: once per second.
871 * We check more frequently in case the guests sends data to
872 * the virtual device linked to our pty. */
873 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
881 static void pty_chr_timer(void *opaque
)
883 struct CharDriverState
*chr
= opaque
;
884 PtyCharDriver
*s
= chr
->opaque
;
889 /* If we arrive here without polling being cleared due
890 * read returning -EIO, then we are (re-)connected */
891 pty_chr_state(chr
, 1);
896 pty_chr_update_read_handler(chr
);
899 static void pty_chr_close(struct CharDriverState
*chr
)
901 PtyCharDriver
*s
= chr
->opaque
;
903 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
908 static CharDriverState
*qemu_chr_open_pty(void)
910 CharDriverState
*chr
;
914 #if defined(__OpenBSD__)
915 char pty_name
[PATH_MAX
];
916 #define q_ptsname(x) pty_name
918 char *pty_name
= NULL
;
919 #define q_ptsname(x) ptsname(x)
922 chr
= qemu_mallocz(sizeof(CharDriverState
));
925 s
= qemu_mallocz(sizeof(PtyCharDriver
));
931 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
935 /* Set raw attributes on the pty. */
937 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
940 len
= strlen(q_ptsname(s
->fd
)) + 5;
941 chr
->filename
= qemu_malloc(len
);
942 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
943 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
946 chr
->chr_write
= pty_chr_write
;
947 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
948 chr
->chr_close
= pty_chr_close
;
950 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
955 static void tty_serial_init(int fd
, int speed
,
956 int parity
, int data_bits
, int stop_bits
)
962 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
963 speed
, parity
, data_bits
, stop_bits
);
965 tcgetattr (fd
, &tty
);
968 if (speed
<= 50 * MARGIN
)
970 else if (speed
<= 75 * MARGIN
)
972 else if (speed
<= 300 * MARGIN
)
974 else if (speed
<= 600 * MARGIN
)
976 else if (speed
<= 1200 * MARGIN
)
978 else if (speed
<= 2400 * MARGIN
)
980 else if (speed
<= 4800 * MARGIN
)
982 else if (speed
<= 9600 * MARGIN
)
984 else if (speed
<= 19200 * MARGIN
)
986 else if (speed
<= 38400 * MARGIN
)
988 else if (speed
<= 57600 * MARGIN
)
990 else if (speed
<= 115200 * MARGIN
)
995 cfsetispeed(&tty
, spd
);
996 cfsetospeed(&tty
, spd
);
998 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
999 |INLCR
|IGNCR
|ICRNL
|IXON
);
1000 tty
.c_oflag
|= OPOST
;
1001 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1002 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1023 tty
.c_cflag
|= PARENB
;
1026 tty
.c_cflag
|= PARENB
| PARODD
;
1030 tty
.c_cflag
|= CSTOPB
;
1032 tcsetattr (fd
, TCSANOW
, &tty
);
1035 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1037 FDCharDriver
*s
= chr
->opaque
;
1040 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1042 QEMUSerialSetParams
*ssp
= arg
;
1043 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1044 ssp
->data_bits
, ssp
->stop_bits
);
1047 case CHR_IOCTL_SERIAL_SET_BREAK
:
1049 int enable
= *(int *)arg
;
1051 tcsendbreak(s
->fd_in
, 1);
1054 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1057 int *targ
= (int *)arg
;
1058 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1060 if (sarg
| TIOCM_CTS
)
1061 *targ
|= CHR_TIOCM_CTS
;
1062 if (sarg
| TIOCM_CAR
)
1063 *targ
|= CHR_TIOCM_CAR
;
1064 if (sarg
| TIOCM_DSR
)
1065 *targ
|= CHR_TIOCM_DSR
;
1066 if (sarg
| TIOCM_RI
)
1067 *targ
|= CHR_TIOCM_RI
;
1068 if (sarg
| TIOCM_DTR
)
1069 *targ
|= CHR_TIOCM_DTR
;
1070 if (sarg
| TIOCM_RTS
)
1071 *targ
|= CHR_TIOCM_RTS
;
1074 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1076 int sarg
= *(int *)arg
;
1078 if (sarg
| CHR_TIOCM_DTR
)
1080 if (sarg
| CHR_TIOCM_RTS
)
1082 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1091 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1093 CharDriverState
*chr
;
1096 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1097 tty_serial_init(fd
, 115200, 'N', 8, 1);
1098 chr
= qemu_chr_open_fd(fd
, fd
);
1103 chr
->chr_ioctl
= tty_serial_ioctl
;
1104 qemu_chr_reset(chr
);
1107 #else /* ! __linux__ && ! __sun__ */
1108 static CharDriverState
*qemu_chr_open_pty(void)
1112 #endif /* __linux__ || __sun__ */
1114 #if defined(__linux__)
1118 } ParallelCharDriver
;
1120 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1122 if (s
->mode
!= mode
) {
1124 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1131 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1133 ParallelCharDriver
*drv
= chr
->opaque
;
1138 case CHR_IOCTL_PP_READ_DATA
:
1139 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1141 *(uint8_t *)arg
= b
;
1143 case CHR_IOCTL_PP_WRITE_DATA
:
1144 b
= *(uint8_t *)arg
;
1145 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1148 case CHR_IOCTL_PP_READ_CONTROL
:
1149 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1151 /* Linux gives only the lowest bits, and no way to know data
1152 direction! For better compatibility set the fixed upper
1154 *(uint8_t *)arg
= b
| 0xc0;
1156 case CHR_IOCTL_PP_WRITE_CONTROL
:
1157 b
= *(uint8_t *)arg
;
1158 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1161 case CHR_IOCTL_PP_READ_STATUS
:
1162 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1164 *(uint8_t *)arg
= b
;
1166 case CHR_IOCTL_PP_DATA_DIR
:
1167 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1170 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1171 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1172 struct ParallelIOArg
*parg
= arg
;
1173 int n
= read(fd
, parg
->buffer
, parg
->count
);
1174 if (n
!= parg
->count
) {
1179 case CHR_IOCTL_PP_EPP_READ
:
1180 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
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_WRITE_ADDR
:
1189 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1190 struct ParallelIOArg
*parg
= arg
;
1191 int n
= write(fd
, parg
->buffer
, parg
->count
);
1192 if (n
!= parg
->count
) {
1197 case CHR_IOCTL_PP_EPP_WRITE
:
1198 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1199 struct ParallelIOArg
*parg
= arg
;
1200 int n
= write(fd
, parg
->buffer
, parg
->count
);
1201 if (n
!= parg
->count
) {
1212 static void pp_close(CharDriverState
*chr
)
1214 ParallelCharDriver
*drv
= chr
->opaque
;
1217 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1218 ioctl(fd
, PPRELEASE
);
1223 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1225 CharDriverState
*chr
;
1226 ParallelCharDriver
*drv
;
1229 TFR(fd
= open(filename
, O_RDWR
));
1233 if (ioctl(fd
, PPCLAIM
) < 0) {
1238 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1244 drv
->mode
= IEEE1284_MODE_COMPAT
;
1246 chr
= qemu_mallocz(sizeof(CharDriverState
));
1252 chr
->chr_write
= null_chr_write
;
1253 chr
->chr_ioctl
= pp_ioctl
;
1254 chr
->chr_close
= pp_close
;
1257 qemu_chr_reset(chr
);
1261 #endif /* __linux__ */
1267 HANDLE hcom
, hrecv
, hsend
;
1268 OVERLAPPED orecv
, osend
;
1273 #define NSENDBUF 2048
1274 #define NRECVBUF 2048
1275 #define MAXCONNECT 1
1276 #define NTIMEOUT 5000
1278 static int win_chr_poll(void *opaque
);
1279 static int win_chr_pipe_poll(void *opaque
);
1281 static void win_chr_close(CharDriverState
*chr
)
1283 WinCharState
*s
= chr
->opaque
;
1286 CloseHandle(s
->hsend
);
1290 CloseHandle(s
->hrecv
);
1294 CloseHandle(s
->hcom
);
1298 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1300 qemu_del_polling_cb(win_chr_poll
, chr
);
1303 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1305 WinCharState
*s
= chr
->opaque
;
1307 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1312 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1314 fprintf(stderr
, "Failed CreateEvent\n");
1317 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1319 fprintf(stderr
, "Failed CreateEvent\n");
1323 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1324 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1325 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1326 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1331 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1332 fprintf(stderr
, "Failed SetupComm\n");
1336 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1337 size
= sizeof(COMMCONFIG
);
1338 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1339 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1340 CommConfigDialog(filename
, NULL
, &comcfg
);
1342 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1343 fprintf(stderr
, "Failed SetCommState\n");
1347 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1348 fprintf(stderr
, "Failed SetCommMask\n");
1352 cto
.ReadIntervalTimeout
= MAXDWORD
;
1353 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1354 fprintf(stderr
, "Failed SetCommTimeouts\n");
1358 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1359 fprintf(stderr
, "Failed ClearCommError\n");
1362 qemu_add_polling_cb(win_chr_poll
, chr
);
1370 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1372 WinCharState
*s
= chr
->opaque
;
1373 DWORD len
, ret
, size
, err
;
1376 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1377 s
->osend
.hEvent
= s
->hsend
;
1380 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1382 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1384 err
= GetLastError();
1385 if (err
== ERROR_IO_PENDING
) {
1386 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1404 static int win_chr_read_poll(CharDriverState
*chr
)
1406 WinCharState
*s
= chr
->opaque
;
1408 s
->max_size
= qemu_chr_can_read(chr
);
1412 static void win_chr_readfile(CharDriverState
*chr
)
1414 WinCharState
*s
= chr
->opaque
;
1419 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1420 s
->orecv
.hEvent
= s
->hrecv
;
1421 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1423 err
= GetLastError();
1424 if (err
== ERROR_IO_PENDING
) {
1425 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1430 qemu_chr_read(chr
, buf
, size
);
1434 static void win_chr_read(CharDriverState
*chr
)
1436 WinCharState
*s
= chr
->opaque
;
1438 if (s
->len
> s
->max_size
)
1439 s
->len
= s
->max_size
;
1443 win_chr_readfile(chr
);
1446 static int win_chr_poll(void *opaque
)
1448 CharDriverState
*chr
= opaque
;
1449 WinCharState
*s
= chr
->opaque
;
1453 ClearCommError(s
->hcom
, &comerr
, &status
);
1454 if (status
.cbInQue
> 0) {
1455 s
->len
= status
.cbInQue
;
1456 win_chr_read_poll(chr
);
1463 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1465 CharDriverState
*chr
;
1468 chr
= qemu_mallocz(sizeof(CharDriverState
));
1471 s
= qemu_mallocz(sizeof(WinCharState
));
1477 chr
->chr_write
= win_chr_write
;
1478 chr
->chr_close
= win_chr_close
;
1480 if (win_chr_init(chr
, filename
) < 0) {
1485 qemu_chr_reset(chr
);
1489 static int win_chr_pipe_poll(void *opaque
)
1491 CharDriverState
*chr
= opaque
;
1492 WinCharState
*s
= chr
->opaque
;
1495 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1498 win_chr_read_poll(chr
);
1505 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1507 WinCharState
*s
= chr
->opaque
;
1515 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1517 fprintf(stderr
, "Failed CreateEvent\n");
1520 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1522 fprintf(stderr
, "Failed CreateEvent\n");
1526 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1527 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1528 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1530 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1531 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1532 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1537 ZeroMemory(&ov
, sizeof(ov
));
1538 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1539 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1541 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1545 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1547 fprintf(stderr
, "Failed GetOverlappedResult\n");
1549 CloseHandle(ov
.hEvent
);
1556 CloseHandle(ov
.hEvent
);
1559 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1568 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1570 CharDriverState
*chr
;
1573 chr
= qemu_mallocz(sizeof(CharDriverState
));
1576 s
= qemu_mallocz(sizeof(WinCharState
));
1582 chr
->chr_write
= win_chr_write
;
1583 chr
->chr_close
= win_chr_close
;
1585 if (win_chr_pipe_init(chr
, filename
) < 0) {
1590 qemu_chr_reset(chr
);
1594 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1596 CharDriverState
*chr
;
1599 chr
= qemu_mallocz(sizeof(CharDriverState
));
1602 s
= qemu_mallocz(sizeof(WinCharState
));
1609 chr
->chr_write
= win_chr_write
;
1610 qemu_chr_reset(chr
);
1614 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1616 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1619 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1623 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1624 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1625 if (fd_out
== INVALID_HANDLE_VALUE
)
1628 return qemu_chr_open_win_file(fd_out
);
1630 #endif /* !_WIN32 */
1632 /***********************************************************/
1633 /* UDP Net console */
1637 struct sockaddr_in daddr
;
1644 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1646 NetCharDriver
*s
= chr
->opaque
;
1648 return sendto(s
->fd
, buf
, len
, 0,
1649 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1652 static int udp_chr_read_poll(void *opaque
)
1654 CharDriverState
*chr
= opaque
;
1655 NetCharDriver
*s
= chr
->opaque
;
1657 s
->max_size
= qemu_chr_can_read(chr
);
1659 /* If there were any stray characters in the queue process them
1662 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1663 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1665 s
->max_size
= qemu_chr_can_read(chr
);
1670 static void udp_chr_read(void *opaque
)
1672 CharDriverState
*chr
= opaque
;
1673 NetCharDriver
*s
= chr
->opaque
;
1675 if (s
->max_size
== 0)
1677 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1678 s
->bufptr
= s
->bufcnt
;
1683 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1684 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1686 s
->max_size
= qemu_chr_can_read(chr
);
1690 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1692 NetCharDriver
*s
= chr
->opaque
;
1695 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1696 udp_chr_read
, NULL
, chr
);
1700 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1702 CharDriverState
*chr
= NULL
;
1703 NetCharDriver
*s
= NULL
;
1705 struct sockaddr_in saddr
;
1707 chr
= qemu_mallocz(sizeof(CharDriverState
));
1710 s
= qemu_mallocz(sizeof(NetCharDriver
));
1714 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1716 perror("socket(PF_INET, SOCK_DGRAM)");
1720 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1721 printf("Could not parse: %s\n", def
);
1725 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1735 chr
->chr_write
= udp_chr_write
;
1736 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1749 /***********************************************************/
1750 /* TCP Net console */
1761 static void tcp_chr_accept(void *opaque
);
1763 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1765 TCPCharDriver
*s
= chr
->opaque
;
1767 return send_all(s
->fd
, buf
, len
);
1769 /* XXX: indicate an error ? */
1774 static int tcp_chr_read_poll(void *opaque
)
1776 CharDriverState
*chr
= opaque
;
1777 TCPCharDriver
*s
= chr
->opaque
;
1780 s
->max_size
= qemu_chr_can_read(chr
);
1785 #define IAC_BREAK 243
1786 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1788 uint8_t *buf
, int *size
)
1790 /* Handle any telnet client's basic IAC options to satisfy char by
1791 * char mode with no echo. All IAC options will be removed from
1792 * the buf and the do_telnetopt variable will be used to track the
1793 * state of the width of the IAC information.
1795 * IAC commands come in sets of 3 bytes with the exception of the
1796 * "IAC BREAK" command and the double IAC.
1802 for (i
= 0; i
< *size
; i
++) {
1803 if (s
->do_telnetopt
> 1) {
1804 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1805 /* Double IAC means send an IAC */
1809 s
->do_telnetopt
= 1;
1811 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1812 /* Handle IAC break commands by sending a serial break */
1813 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1818 if (s
->do_telnetopt
>= 4) {
1819 s
->do_telnetopt
= 1;
1822 if ((unsigned char)buf
[i
] == IAC
) {
1823 s
->do_telnetopt
= 2;
1834 static void tcp_chr_read(void *opaque
)
1836 CharDriverState
*chr
= opaque
;
1837 TCPCharDriver
*s
= chr
->opaque
;
1841 if (!s
->connected
|| s
->max_size
<= 0)
1844 if (len
> s
->max_size
)
1846 size
= recv(s
->fd
, buf
, len
, 0);
1848 /* connection closed */
1850 if (s
->listen_fd
>= 0) {
1851 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1853 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1856 } else if (size
> 0) {
1857 if (s
->do_telnetopt
)
1858 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1860 qemu_chr_read(chr
, buf
, size
);
1864 static void tcp_chr_connect(void *opaque
)
1866 CharDriverState
*chr
= opaque
;
1867 TCPCharDriver
*s
= chr
->opaque
;
1870 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1871 tcp_chr_read
, NULL
, chr
);
1872 qemu_chr_reset(chr
);
1875 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1876 static void tcp_chr_telnet_init(int fd
)
1879 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1880 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1881 send(fd
, (char *)buf
, 3, 0);
1882 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1883 send(fd
, (char *)buf
, 3, 0);
1884 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1885 send(fd
, (char *)buf
, 3, 0);
1886 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1887 send(fd
, (char *)buf
, 3, 0);
1890 static void socket_set_nodelay(int fd
)
1893 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1896 static void tcp_chr_accept(void *opaque
)
1898 CharDriverState
*chr
= opaque
;
1899 TCPCharDriver
*s
= chr
->opaque
;
1900 struct sockaddr_in saddr
;
1902 struct sockaddr_un uaddr
;
1904 struct sockaddr
*addr
;
1911 len
= sizeof(uaddr
);
1912 addr
= (struct sockaddr
*)&uaddr
;
1916 len
= sizeof(saddr
);
1917 addr
= (struct sockaddr
*)&saddr
;
1919 fd
= accept(s
->listen_fd
, addr
, &len
);
1920 if (fd
< 0 && errno
!= EINTR
) {
1922 } else if (fd
>= 0) {
1923 if (s
->do_telnetopt
)
1924 tcp_chr_telnet_init(fd
);
1928 socket_set_nonblock(fd
);
1930 socket_set_nodelay(fd
);
1932 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
1933 tcp_chr_connect(chr
);
1936 static void tcp_chr_close(CharDriverState
*chr
)
1938 TCPCharDriver
*s
= chr
->opaque
;
1941 if (s
->listen_fd
>= 0)
1942 closesocket(s
->listen_fd
);
1946 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
1950 CharDriverState
*chr
= NULL
;
1951 TCPCharDriver
*s
= NULL
;
1952 int fd
= -1, ret
, err
, val
;
1954 int is_waitconnect
= 1;
1957 struct sockaddr_in saddr
;
1959 struct sockaddr_un uaddr
;
1961 struct sockaddr
*addr
;
1966 addr
= (struct sockaddr
*)&uaddr
;
1967 addrlen
= sizeof(uaddr
);
1968 if (parse_unix_path(&uaddr
, host_str
) < 0)
1973 addr
= (struct sockaddr
*)&saddr
;
1974 addrlen
= sizeof(saddr
);
1975 if (parse_host_port(&saddr
, host_str
) < 0)
1980 while((ptr
= strchr(ptr
,','))) {
1982 if (!strncmp(ptr
,"server",6)) {
1984 } else if (!strncmp(ptr
,"nowait",6)) {
1986 } else if (!strncmp(ptr
,"nodelay",6)) {
1989 printf("Unknown option: %s\n", ptr
);
1996 chr
= qemu_mallocz(sizeof(CharDriverState
));
1999 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2005 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2008 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2013 if (!is_waitconnect
)
2014 socket_set_nonblock(fd
);
2019 s
->is_unix
= is_unix
;
2020 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2023 chr
->chr_write
= tcp_chr_write
;
2024 chr
->chr_close
= tcp_chr_close
;
2027 /* allow fast reuse */
2031 pstrcpy(path
, sizeof(path
), uaddr
.sun_path
);
2037 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2040 ret
= bind(fd
, addr
, addrlen
);
2044 ret
= listen(fd
, 0);
2049 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2051 s
->do_telnetopt
= 1;
2054 ret
= connect(fd
, addr
, addrlen
);
2056 err
= socket_error();
2057 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2058 } else if (err
== EINPROGRESS
) {
2061 } else if (err
== WSAEALREADY
) {
2073 socket_set_nodelay(fd
);
2075 tcp_chr_connect(chr
);
2077 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2080 if (is_listen
&& is_waitconnect
) {
2081 printf("QEMU waiting for connection on: %s\n", host_str
);
2082 tcp_chr_accept(chr
);
2083 socket_set_nonblock(s
->listen_fd
);
2095 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
2096 = TAILQ_HEAD_INITIALIZER(chardevs
);
2098 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
)
2101 CharDriverState
*chr
;
2103 if (!strcmp(filename
, "vc")) {
2104 chr
= text_console_init(&display_state
, 0);
2106 if (strstart(filename
, "vc:", &p
)) {
2107 chr
= text_console_init(&display_state
, p
);
2109 if (!strcmp(filename
, "null")) {
2110 chr
= qemu_chr_open_null();
2112 if (strstart(filename
, "tcp:", &p
)) {
2113 chr
= qemu_chr_open_tcp(p
, 0, 0);
2115 if (strstart(filename
, "telnet:", &p
)) {
2116 chr
= qemu_chr_open_tcp(p
, 1, 0);
2118 if (strstart(filename
, "udp:", &p
)) {
2119 chr
= qemu_chr_open_udp(p
);
2121 if (strstart(filename
, "mon:", &p
)) {
2122 chr
= qemu_chr_open(label
, p
);
2124 chr
= qemu_chr_open_mux(chr
);
2125 monitor_init(chr
, !nographic
);
2127 printf("Unable to open driver: %s\n", p
);
2131 if (strstart(filename
, "unix:", &p
)) {
2132 chr
= qemu_chr_open_tcp(p
, 0, 1);
2133 } else if (strstart(filename
, "file:", &p
)) {
2134 chr
= qemu_chr_open_file_out(p
);
2135 } else if (strstart(filename
, "pipe:", &p
)) {
2136 chr
= qemu_chr_open_pipe(p
);
2137 } else if (!strcmp(filename
, "pty")) {
2138 chr
= qemu_chr_open_pty();
2139 } else if (!strcmp(filename
, "stdio")) {
2140 chr
= qemu_chr_open_stdio();
2142 #if defined(__linux__)
2143 if (strstart(filename
, "/dev/parport", NULL
)) {
2144 chr
= qemu_chr_open_pp(filename
);
2147 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2148 || defined(__NetBSD__) || defined(__OpenBSD__)
2149 if (strstart(filename
, "/dev/", NULL
)) {
2150 chr
= qemu_chr_open_tty(filename
);
2154 if (strstart(filename
, "COM", NULL
)) {
2155 chr
= qemu_chr_open_win(filename
);
2157 if (strstart(filename
, "pipe:", &p
)) {
2158 chr
= qemu_chr_open_win_pipe(p
);
2160 if (strstart(filename
, "con:", NULL
)) {
2161 chr
= qemu_chr_open_win_con(filename
);
2163 if (strstart(filename
, "file:", &p
)) {
2164 chr
= qemu_chr_open_win_file_out(p
);
2167 #ifdef CONFIG_BRLAPI
2168 if (!strcmp(filename
, "braille")) {
2169 chr
= chr_baum_init();
2178 chr
->filename
= qemu_strdup(filename
);
2179 chr
->label
= qemu_strdup(label
);
2180 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2185 void qemu_chr_close(CharDriverState
*chr
)
2187 TAILQ_REMOVE(&chardevs
, chr
, next
);
2189 chr
->chr_close(chr
);
2190 qemu_free(chr
->filename
);
2191 qemu_free(chr
->label
);
2195 void qemu_chr_info(void)
2197 CharDriverState
*chr
;
2199 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2200 term_printf("%s: filename=%s\n", chr
->label
, chr
->filename
);