4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
33 #include "hw/msmouse.h"
44 #include <sys/times.h>
48 #include <sys/ioctl.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
54 #include <net/if_tap.h>
57 #include <linux/if_tun.h>
59 #include <arpa/inet.h>
62 #include <sys/select.h>
67 #include <dev/ppbus/ppi.h>
68 #include <dev/ppbus/ppbconf.h>
72 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
73 #include <freebsd/stdlib.h>
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
99 #include "qemu_socket.h"
101 /***********************************************************/
102 /* character device */
104 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
105 TAILQ_HEAD_INITIALIZER(chardevs
);
106 static int initial_reset_issued
;
108 static void qemu_chr_event(CharDriverState
*s
, int event
)
112 s
->chr_event(s
->handler_opaque
, event
);
115 static void qemu_chr_reset_bh(void *opaque
)
117 CharDriverState
*s
= opaque
;
118 qemu_chr_event(s
, CHR_EVENT_RESET
);
119 qemu_bh_delete(s
->bh
);
123 void qemu_chr_reset(CharDriverState
*s
)
125 if (s
->bh
== NULL
&& initial_reset_issued
) {
126 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
127 qemu_bh_schedule(s
->bh
);
131 void qemu_chr_initial_reset(void)
133 CharDriverState
*chr
;
135 initial_reset_issued
= 1;
137 TAILQ_FOREACH(chr
, &chardevs
, next
) {
142 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
144 return s
->chr_write(s
, buf
, len
);
147 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
151 return s
->chr_ioctl(s
, cmd
, arg
);
154 int qemu_chr_can_read(CharDriverState
*s
)
156 if (!s
->chr_can_read
)
158 return s
->chr_can_read(s
->handler_opaque
);
161 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
163 s
->chr_read(s
->handler_opaque
, buf
, len
);
166 void qemu_chr_accept_input(CharDriverState
*s
)
168 if (s
->chr_accept_input
)
169 s
->chr_accept_input(s
);
172 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
177 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
178 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
182 void qemu_chr_send_event(CharDriverState
*s
, int event
)
184 if (s
->chr_send_event
)
185 s
->chr_send_event(s
, event
);
188 void qemu_chr_add_handlers(CharDriverState
*s
,
189 IOCanRWHandler
*fd_can_read
,
190 IOReadHandler
*fd_read
,
191 IOEventHandler
*fd_event
,
194 s
->chr_can_read
= fd_can_read
;
195 s
->chr_read
= fd_read
;
196 s
->chr_event
= fd_event
;
197 s
->handler_opaque
= opaque
;
198 if (s
->chr_update_read_handler
)
199 s
->chr_update_read_handler(s
);
202 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
207 static CharDriverState
*qemu_chr_open_null(void)
209 CharDriverState
*chr
;
211 chr
= qemu_mallocz(sizeof(CharDriverState
));
212 chr
->chr_write
= null_chr_write
;
216 /* MUX driver for serial I/O splitting */
217 static int term_timestamps
;
218 static int64_t term_timestamps_start
;
220 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
221 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
223 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
224 IOReadHandler
*chr_read
[MAX_MUX
];
225 IOEventHandler
*chr_event
[MAX_MUX
];
226 void *ext_opaque
[MAX_MUX
];
227 CharDriverState
*drv
;
231 /* Intermediate input buffer allows to catch escape sequences even if the
232 currently active device is not accepting any input - but only until it
234 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
240 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
242 MuxDriver
*d
= chr
->opaque
;
244 if (!term_timestamps
) {
245 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
250 for(i
= 0; i
< len
; i
++) {
251 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
252 if (buf
[i
] == '\n') {
257 ti
= qemu_get_clock(rt_clock
);
258 if (term_timestamps_start
== -1)
259 term_timestamps_start
= ti
;
260 ti
-= term_timestamps_start
;
262 snprintf(buf1
, sizeof(buf1
),
263 "[%02d:%02d:%02d.%03d] ",
268 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
275 static const char * const mux_help
[] = {
276 "% h print this help\n\r",
277 "% x exit emulator\n\r",
278 "% s save disk data back to file (if -snapshot)\n\r",
279 "% t toggle console timestamps\n\r"
280 "% b send break (magic sysrq)\n\r",
281 "% c switch between console and monitor\n\r",
286 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
287 static void mux_print_help(CharDriverState
*chr
)
290 char ebuf
[15] = "Escape-Char";
291 char cbuf
[50] = "\n\r";
293 if (term_escape_char
> 0 && term_escape_char
< 26) {
294 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
295 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
297 snprintf(cbuf
, sizeof(cbuf
),
298 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
301 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
302 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
303 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
304 if (mux_help
[i
][j
] == '%')
305 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
307 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
312 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
314 if (d
->term_got_escape
) {
315 d
->term_got_escape
= 0;
316 if (ch
== term_escape_char
)
325 const char *term
= "QEMU: Terminated\n\r";
326 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
333 for (i
= 0; i
< nb_drives
; i
++) {
334 bdrv_commit(drives_table
[i
].bdrv
);
339 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
342 /* Switch to the next registered device */
344 if (chr
->focus
>= d
->mux_cnt
)
348 term_timestamps
= !term_timestamps
;
349 term_timestamps_start
= -1;
352 } else if (ch
== term_escape_char
) {
353 d
->term_got_escape
= 1;
361 static void mux_chr_accept_input(CharDriverState
*chr
)
364 MuxDriver
*d
= chr
->opaque
;
366 while (d
->prod
[m
] != d
->cons
[m
] &&
367 d
->chr_can_read
[m
] &&
368 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
369 d
->chr_read
[m
](d
->ext_opaque
[m
],
370 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
374 static int mux_chr_can_read(void *opaque
)
376 CharDriverState
*chr
= opaque
;
377 MuxDriver
*d
= chr
->opaque
;
380 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
382 if (d
->chr_can_read
[m
])
383 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
387 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
389 CharDriverState
*chr
= opaque
;
390 MuxDriver
*d
= chr
->opaque
;
394 mux_chr_accept_input (opaque
);
396 for(i
= 0; i
< size
; i
++)
397 if (mux_proc_byte(chr
, d
, buf
[i
])) {
398 if (d
->prod
[m
] == d
->cons
[m
] &&
399 d
->chr_can_read
[m
] &&
400 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
401 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
403 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
407 static void mux_chr_event(void *opaque
, int event
)
409 CharDriverState
*chr
= opaque
;
410 MuxDriver
*d
= chr
->opaque
;
413 /* Send the event to all registered listeners */
414 for (i
= 0; i
< d
->mux_cnt
; i
++)
416 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
419 static void mux_chr_update_read_handler(CharDriverState
*chr
)
421 MuxDriver
*d
= chr
->opaque
;
423 if (d
->mux_cnt
>= MAX_MUX
) {
424 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
427 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
428 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
429 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
430 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
431 /* Fix up the real driver with mux routines */
432 if (d
->mux_cnt
== 0) {
433 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
436 chr
->focus
= d
->mux_cnt
;
440 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
442 CharDriverState
*chr
;
445 chr
= qemu_mallocz(sizeof(CharDriverState
));
446 d
= qemu_mallocz(sizeof(MuxDriver
));
451 chr
->chr_write
= mux_chr_write
;
452 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
453 chr
->chr_accept_input
= mux_chr_accept_input
;
459 int send_all(int fd
, const void *buf
, int len1
)
465 ret
= send(fd
, buf
, len
, 0);
467 errno
= WSAGetLastError();
468 if (errno
!= WSAEWOULDBLOCK
) {
471 } else if (ret
== 0) {
483 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
489 ret
= write(fd
, buf
, len
);
491 if (errno
!= EINTR
&& errno
!= EAGAIN
)
493 } else if (ret
== 0) {
503 int send_all(int fd
, const void *buf
, int len1
)
505 return unix_write(fd
, buf
, len1
);
516 #define STDIO_MAX_CLIENTS 1
517 static int stdio_nb_clients
= 0;
519 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
521 FDCharDriver
*s
= chr
->opaque
;
522 return send_all(s
->fd_out
, buf
, len
);
525 static int fd_chr_read_poll(void *opaque
)
527 CharDriverState
*chr
= opaque
;
528 FDCharDriver
*s
= chr
->opaque
;
530 s
->max_size
= qemu_chr_can_read(chr
);
534 static void fd_chr_read(void *opaque
)
536 CharDriverState
*chr
= opaque
;
537 FDCharDriver
*s
= chr
->opaque
;
542 if (len
> s
->max_size
)
546 size
= read(s
->fd_in
, buf
, len
);
548 /* FD has been closed. Remove it from the active list. */
549 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
553 qemu_chr_read(chr
, buf
, size
);
557 static void fd_chr_update_read_handler(CharDriverState
*chr
)
559 FDCharDriver
*s
= chr
->opaque
;
562 if (nographic
&& s
->fd_in
== 0) {
564 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
565 fd_chr_read
, NULL
, chr
);
570 static void fd_chr_close(struct CharDriverState
*chr
)
572 FDCharDriver
*s
= chr
->opaque
;
575 if (nographic
&& s
->fd_in
== 0) {
577 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
584 /* open a character device to a unix fd */
585 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
587 CharDriverState
*chr
;
590 chr
= qemu_mallocz(sizeof(CharDriverState
));
591 s
= qemu_mallocz(sizeof(FDCharDriver
));
595 chr
->chr_write
= fd_chr_write
;
596 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
597 chr
->chr_close
= fd_chr_close
;
604 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
608 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
611 return qemu_chr_open_fd(-1, fd_out
);
614 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
617 char filename_in
[256], filename_out
[256];
619 snprintf(filename_in
, 256, "%s.in", filename
);
620 snprintf(filename_out
, 256, "%s.out", filename
);
621 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
622 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
623 if (fd_in
< 0 || fd_out
< 0) {
628 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
632 return qemu_chr_open_fd(fd_in
, fd_out
);
636 /* for STDIO, we handle the case where several clients use it
639 #define TERM_FIFO_MAX_SIZE 1
641 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
642 static int term_fifo_size
;
644 static int stdio_read_poll(void *opaque
)
646 CharDriverState
*chr
= opaque
;
648 /* try to flush the queue if needed */
649 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
650 qemu_chr_read(chr
, term_fifo
, 1);
653 /* see if we can absorb more chars */
654 if (term_fifo_size
== 0)
660 static void stdio_read(void *opaque
)
664 CharDriverState
*chr
= opaque
;
666 size
= read(0, buf
, 1);
668 /* stdin has been closed. Remove it from the active list. */
669 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
673 if (qemu_chr_can_read(chr
) > 0) {
674 qemu_chr_read(chr
, buf
, 1);
675 } else if (term_fifo_size
== 0) {
676 term_fifo
[term_fifo_size
++] = buf
[0];
681 /* init terminal so that we can grab keys */
682 static struct termios oldtty
;
683 static int old_fd0_flags
;
684 static int term_atexit_done
;
686 static void term_exit(void)
688 tcsetattr (0, TCSANOW
, &oldtty
);
689 fcntl(0, F_SETFL
, old_fd0_flags
);
692 static void term_init(void)
698 old_fd0_flags
= fcntl(0, F_GETFL
);
700 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
701 |INLCR
|IGNCR
|ICRNL
|IXON
);
702 tty
.c_oflag
|= OPOST
;
703 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
704 /* if graphical mode, we allow Ctrl-C handling */
706 tty
.c_lflag
&= ~ISIG
;
707 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
712 tcsetattr (0, TCSANOW
, &tty
);
714 if (!term_atexit_done
++)
717 fcntl(0, F_SETFL
, O_NONBLOCK
);
720 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
724 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
728 static CharDriverState
*qemu_chr_open_stdio(void)
730 CharDriverState
*chr
;
732 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
734 chr
= qemu_chr_open_fd(0, 1);
735 chr
->chr_close
= qemu_chr_close_stdio
;
736 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
744 /* Once Solaris has openpty(), this is going to be removed. */
745 int openpty(int *amaster
, int *aslave
, char *name
,
746 struct termios
*termp
, struct winsize
*winp
)
749 int mfd
= -1, sfd
= -1;
751 *amaster
= *aslave
= -1;
753 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
757 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
760 if ((slave
= ptsname(mfd
)) == NULL
)
763 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
766 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
767 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
775 ioctl(sfd
, TIOCSWINSZ
, winp
);
786 void cfmakeraw (struct termios
*termios_p
)
788 termios_p
->c_iflag
&=
789 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
790 termios_p
->c_oflag
&= ~OPOST
;
791 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
792 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
793 termios_p
->c_cflag
|= CS8
;
795 termios_p
->c_cc
[VMIN
] = 0;
796 termios_p
->c_cc
[VTIME
] = 0;
800 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
801 || defined(__NetBSD__) || defined(__OpenBSD__)
811 static void pty_chr_update_read_handler(CharDriverState
*chr
);
812 static void pty_chr_state(CharDriverState
*chr
, int connected
);
814 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
816 PtyCharDriver
*s
= chr
->opaque
;
819 /* guest sends data, check for (re-)connect */
820 pty_chr_update_read_handler(chr
);
823 return send_all(s
->fd
, buf
, len
);
826 static int pty_chr_read_poll(void *opaque
)
828 CharDriverState
*chr
= opaque
;
829 PtyCharDriver
*s
= chr
->opaque
;
831 s
->read_bytes
= qemu_chr_can_read(chr
);
832 return s
->read_bytes
;
835 static void pty_chr_read(void *opaque
)
837 CharDriverState
*chr
= opaque
;
838 PtyCharDriver
*s
= chr
->opaque
;
843 if (len
> s
->read_bytes
)
847 size
= read(s
->fd
, buf
, len
);
848 if ((size
== -1 && errno
== EIO
) ||
850 pty_chr_state(chr
, 0);
854 pty_chr_state(chr
, 1);
855 qemu_chr_read(chr
, buf
, size
);
859 static void pty_chr_update_read_handler(CharDriverState
*chr
)
861 PtyCharDriver
*s
= chr
->opaque
;
863 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
864 pty_chr_read
, NULL
, chr
);
867 * Short timeout here: just need wait long enougth that qemu makes
868 * it through the poll loop once. When reconnected we want a
869 * short timeout so we notice it almost instantly. Otherwise
870 * read() gives us -EIO instantly, making pty_chr_state() reset the
871 * timeout to the normal (much longer) poll interval before the
874 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
877 static void pty_chr_state(CharDriverState
*chr
, int connected
)
879 PtyCharDriver
*s
= chr
->opaque
;
882 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
885 /* (re-)connect poll interval for idle guests: once per second.
886 * We check more frequently in case the guests sends data to
887 * the virtual device linked to our pty. */
888 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
896 static void pty_chr_timer(void *opaque
)
898 struct CharDriverState
*chr
= opaque
;
899 PtyCharDriver
*s
= chr
->opaque
;
904 /* If we arrive here without polling being cleared due
905 * read returning -EIO, then we are (re-)connected */
906 pty_chr_state(chr
, 1);
911 pty_chr_update_read_handler(chr
);
914 static void pty_chr_close(struct CharDriverState
*chr
)
916 PtyCharDriver
*s
= chr
->opaque
;
918 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
920 qemu_del_timer(s
->timer
);
921 qemu_free_timer(s
->timer
);
925 static CharDriverState
*qemu_chr_open_pty(void)
927 CharDriverState
*chr
;
931 #if defined(__OpenBSD__)
932 char pty_name
[PATH_MAX
];
933 #define q_ptsname(x) pty_name
935 char *pty_name
= NULL
;
936 #define q_ptsname(x) ptsname(x)
939 chr
= qemu_mallocz(sizeof(CharDriverState
));
940 s
= qemu_mallocz(sizeof(PtyCharDriver
));
942 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
946 /* Set raw attributes on the pty. */
947 tcgetattr(slave_fd
, &tty
);
949 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
952 len
= strlen(q_ptsname(s
->fd
)) + 5;
953 chr
->filename
= qemu_malloc(len
);
954 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
955 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
958 chr
->chr_write
= pty_chr_write
;
959 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
960 chr
->chr_close
= pty_chr_close
;
962 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
967 static void tty_serial_init(int fd
, int speed
,
968 int parity
, int data_bits
, int stop_bits
)
974 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
975 speed
, parity
, data_bits
, stop_bits
);
977 tcgetattr (fd
, &tty
);
980 if (speed
<= 50 * MARGIN
)
982 else if (speed
<= 75 * MARGIN
)
984 else if (speed
<= 300 * MARGIN
)
986 else if (speed
<= 600 * MARGIN
)
988 else if (speed
<= 1200 * MARGIN
)
990 else if (speed
<= 2400 * MARGIN
)
992 else if (speed
<= 4800 * MARGIN
)
994 else if (speed
<= 9600 * MARGIN
)
996 else if (speed
<= 19200 * MARGIN
)
998 else if (speed
<= 38400 * MARGIN
)
1000 else if (speed
<= 57600 * MARGIN
)
1002 else if (speed
<= 115200 * MARGIN
)
1007 cfsetispeed(&tty
, spd
);
1008 cfsetospeed(&tty
, spd
);
1010 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1011 |INLCR
|IGNCR
|ICRNL
|IXON
);
1012 tty
.c_oflag
|= OPOST
;
1013 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1014 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1035 tty
.c_cflag
|= PARENB
;
1038 tty
.c_cflag
|= PARENB
| PARODD
;
1042 tty
.c_cflag
|= CSTOPB
;
1044 tcsetattr (fd
, TCSANOW
, &tty
);
1047 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1049 FDCharDriver
*s
= chr
->opaque
;
1052 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1054 QEMUSerialSetParams
*ssp
= arg
;
1055 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1056 ssp
->data_bits
, ssp
->stop_bits
);
1059 case CHR_IOCTL_SERIAL_SET_BREAK
:
1061 int enable
= *(int *)arg
;
1063 tcsendbreak(s
->fd_in
, 1);
1066 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1069 int *targ
= (int *)arg
;
1070 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1072 if (sarg
& TIOCM_CTS
)
1073 *targ
|= CHR_TIOCM_CTS
;
1074 if (sarg
& TIOCM_CAR
)
1075 *targ
|= CHR_TIOCM_CAR
;
1076 if (sarg
& TIOCM_DSR
)
1077 *targ
|= CHR_TIOCM_DSR
;
1078 if (sarg
& TIOCM_RI
)
1079 *targ
|= CHR_TIOCM_RI
;
1080 if (sarg
& TIOCM_DTR
)
1081 *targ
|= CHR_TIOCM_DTR
;
1082 if (sarg
& TIOCM_RTS
)
1083 *targ
|= CHR_TIOCM_RTS
;
1086 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1088 int sarg
= *(int *)arg
;
1090 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1091 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1092 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1093 if (sarg
& CHR_TIOCM_CTS
)
1095 if (sarg
& CHR_TIOCM_CAR
)
1097 if (sarg
& CHR_TIOCM_DSR
)
1099 if (sarg
& CHR_TIOCM_RI
)
1101 if (sarg
& CHR_TIOCM_DTR
)
1103 if (sarg
& CHR_TIOCM_RTS
)
1105 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1114 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1116 CharDriverState
*chr
;
1119 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1120 tty_serial_init(fd
, 115200, 'N', 8, 1);
1121 chr
= qemu_chr_open_fd(fd
, fd
);
1126 chr
->chr_ioctl
= tty_serial_ioctl
;
1127 qemu_chr_reset(chr
);
1130 #else /* ! __linux__ && ! __sun__ */
1131 static CharDriverState
*qemu_chr_open_pty(void)
1135 #endif /* __linux__ || __sun__ */
1137 #if defined(__linux__)
1141 } ParallelCharDriver
;
1143 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1145 if (s
->mode
!= mode
) {
1147 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1154 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1156 ParallelCharDriver
*drv
= chr
->opaque
;
1161 case CHR_IOCTL_PP_READ_DATA
:
1162 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1164 *(uint8_t *)arg
= b
;
1166 case CHR_IOCTL_PP_WRITE_DATA
:
1167 b
= *(uint8_t *)arg
;
1168 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1171 case CHR_IOCTL_PP_READ_CONTROL
:
1172 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1174 /* Linux gives only the lowest bits, and no way to know data
1175 direction! For better compatibility set the fixed upper
1177 *(uint8_t *)arg
= b
| 0xc0;
1179 case CHR_IOCTL_PP_WRITE_CONTROL
:
1180 b
= *(uint8_t *)arg
;
1181 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1184 case CHR_IOCTL_PP_READ_STATUS
:
1185 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1187 *(uint8_t *)arg
= b
;
1189 case CHR_IOCTL_PP_DATA_DIR
:
1190 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1193 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1194 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1195 struct ParallelIOArg
*parg
= arg
;
1196 int n
= read(fd
, parg
->buffer
, parg
->count
);
1197 if (n
!= parg
->count
) {
1202 case CHR_IOCTL_PP_EPP_READ
:
1203 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1204 struct ParallelIOArg
*parg
= arg
;
1205 int n
= read(fd
, parg
->buffer
, parg
->count
);
1206 if (n
!= parg
->count
) {
1211 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1212 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1213 struct ParallelIOArg
*parg
= arg
;
1214 int n
= write(fd
, parg
->buffer
, parg
->count
);
1215 if (n
!= parg
->count
) {
1220 case CHR_IOCTL_PP_EPP_WRITE
:
1221 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1222 struct ParallelIOArg
*parg
= arg
;
1223 int n
= write(fd
, parg
->buffer
, parg
->count
);
1224 if (n
!= parg
->count
) {
1235 static void pp_close(CharDriverState
*chr
)
1237 ParallelCharDriver
*drv
= chr
->opaque
;
1240 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1241 ioctl(fd
, PPRELEASE
);
1246 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1248 CharDriverState
*chr
;
1249 ParallelCharDriver
*drv
;
1252 TFR(fd
= open(filename
, O_RDWR
));
1256 if (ioctl(fd
, PPCLAIM
) < 0) {
1261 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1263 drv
->mode
= IEEE1284_MODE_COMPAT
;
1265 chr
= qemu_mallocz(sizeof(CharDriverState
));
1266 chr
->chr_write
= null_chr_write
;
1267 chr
->chr_ioctl
= pp_ioctl
;
1268 chr
->chr_close
= pp_close
;
1271 qemu_chr_reset(chr
);
1275 #endif /* __linux__ */
1277 #if defined(__FreeBSD__)
1278 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1280 int fd
= (int)chr
->opaque
;
1284 case CHR_IOCTL_PP_READ_DATA
:
1285 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1287 *(uint8_t *)arg
= b
;
1289 case CHR_IOCTL_PP_WRITE_DATA
:
1290 b
= *(uint8_t *)arg
;
1291 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1294 case CHR_IOCTL_PP_READ_CONTROL
:
1295 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1297 *(uint8_t *)arg
= b
;
1299 case CHR_IOCTL_PP_WRITE_CONTROL
:
1300 b
= *(uint8_t *)arg
;
1301 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1304 case CHR_IOCTL_PP_READ_STATUS
:
1305 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1307 *(uint8_t *)arg
= b
;
1315 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1317 CharDriverState
*chr
;
1320 fd
= open(filename
, O_RDWR
);
1324 chr
= qemu_mallocz(sizeof(CharDriverState
));
1325 chr
->opaque
= (void *)fd
;
1326 chr
->chr_write
= null_chr_write
;
1327 chr
->chr_ioctl
= pp_ioctl
;
1336 HANDLE hcom
, hrecv
, hsend
;
1337 OVERLAPPED orecv
, osend
;
1342 #define NSENDBUF 2048
1343 #define NRECVBUF 2048
1344 #define MAXCONNECT 1
1345 #define NTIMEOUT 5000
1347 static int win_chr_poll(void *opaque
);
1348 static int win_chr_pipe_poll(void *opaque
);
1350 static void win_chr_close(CharDriverState
*chr
)
1352 WinCharState
*s
= chr
->opaque
;
1355 CloseHandle(s
->hsend
);
1359 CloseHandle(s
->hrecv
);
1363 CloseHandle(s
->hcom
);
1367 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1369 qemu_del_polling_cb(win_chr_poll
, chr
);
1372 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1374 WinCharState
*s
= chr
->opaque
;
1376 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1381 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1383 fprintf(stderr
, "Failed CreateEvent\n");
1386 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1388 fprintf(stderr
, "Failed CreateEvent\n");
1392 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1393 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1394 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1395 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1400 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1401 fprintf(stderr
, "Failed SetupComm\n");
1405 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1406 size
= sizeof(COMMCONFIG
);
1407 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1408 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1409 CommConfigDialog(filename
, NULL
, &comcfg
);
1411 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1412 fprintf(stderr
, "Failed SetCommState\n");
1416 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1417 fprintf(stderr
, "Failed SetCommMask\n");
1421 cto
.ReadIntervalTimeout
= MAXDWORD
;
1422 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1423 fprintf(stderr
, "Failed SetCommTimeouts\n");
1427 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1428 fprintf(stderr
, "Failed ClearCommError\n");
1431 qemu_add_polling_cb(win_chr_poll
, chr
);
1439 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1441 WinCharState
*s
= chr
->opaque
;
1442 DWORD len
, ret
, size
, err
;
1445 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1446 s
->osend
.hEvent
= s
->hsend
;
1449 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1451 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1453 err
= GetLastError();
1454 if (err
== ERROR_IO_PENDING
) {
1455 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1473 static int win_chr_read_poll(CharDriverState
*chr
)
1475 WinCharState
*s
= chr
->opaque
;
1477 s
->max_size
= qemu_chr_can_read(chr
);
1481 static void win_chr_readfile(CharDriverState
*chr
)
1483 WinCharState
*s
= chr
->opaque
;
1488 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1489 s
->orecv
.hEvent
= s
->hrecv
;
1490 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1492 err
= GetLastError();
1493 if (err
== ERROR_IO_PENDING
) {
1494 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1499 qemu_chr_read(chr
, buf
, size
);
1503 static void win_chr_read(CharDriverState
*chr
)
1505 WinCharState
*s
= chr
->opaque
;
1507 if (s
->len
> s
->max_size
)
1508 s
->len
= s
->max_size
;
1512 win_chr_readfile(chr
);
1515 static int win_chr_poll(void *opaque
)
1517 CharDriverState
*chr
= opaque
;
1518 WinCharState
*s
= chr
->opaque
;
1522 ClearCommError(s
->hcom
, &comerr
, &status
);
1523 if (status
.cbInQue
> 0) {
1524 s
->len
= status
.cbInQue
;
1525 win_chr_read_poll(chr
);
1532 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1534 CharDriverState
*chr
;
1537 chr
= qemu_mallocz(sizeof(CharDriverState
));
1538 s
= qemu_mallocz(sizeof(WinCharState
));
1540 chr
->chr_write
= win_chr_write
;
1541 chr
->chr_close
= win_chr_close
;
1543 if (win_chr_init(chr
, filename
) < 0) {
1548 qemu_chr_reset(chr
);
1552 static int win_chr_pipe_poll(void *opaque
)
1554 CharDriverState
*chr
= opaque
;
1555 WinCharState
*s
= chr
->opaque
;
1558 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1561 win_chr_read_poll(chr
);
1568 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1570 WinCharState
*s
= chr
->opaque
;
1578 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1580 fprintf(stderr
, "Failed CreateEvent\n");
1583 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1585 fprintf(stderr
, "Failed CreateEvent\n");
1589 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1590 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1591 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1593 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1594 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1595 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1600 ZeroMemory(&ov
, sizeof(ov
));
1601 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1602 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1604 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1608 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1610 fprintf(stderr
, "Failed GetOverlappedResult\n");
1612 CloseHandle(ov
.hEvent
);
1619 CloseHandle(ov
.hEvent
);
1622 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1631 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1633 CharDriverState
*chr
;
1636 chr
= qemu_mallocz(sizeof(CharDriverState
));
1637 s
= qemu_mallocz(sizeof(WinCharState
));
1639 chr
->chr_write
= win_chr_write
;
1640 chr
->chr_close
= win_chr_close
;
1642 if (win_chr_pipe_init(chr
, filename
) < 0) {
1647 qemu_chr_reset(chr
);
1651 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1653 CharDriverState
*chr
;
1656 chr
= qemu_mallocz(sizeof(CharDriverState
));
1657 s
= qemu_mallocz(sizeof(WinCharState
));
1660 chr
->chr_write
= win_chr_write
;
1661 qemu_chr_reset(chr
);
1665 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1667 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1670 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1674 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1675 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1676 if (fd_out
== INVALID_HANDLE_VALUE
)
1679 return qemu_chr_open_win_file(fd_out
);
1681 #endif /* !_WIN32 */
1683 /***********************************************************/
1684 /* UDP Net console */
1688 struct sockaddr_in daddr
;
1695 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1697 NetCharDriver
*s
= chr
->opaque
;
1699 return sendto(s
->fd
, buf
, len
, 0,
1700 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1703 static int udp_chr_read_poll(void *opaque
)
1705 CharDriverState
*chr
= opaque
;
1706 NetCharDriver
*s
= chr
->opaque
;
1708 s
->max_size
= qemu_chr_can_read(chr
);
1710 /* If there were any stray characters in the queue process them
1713 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1714 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1716 s
->max_size
= qemu_chr_can_read(chr
);
1721 static void udp_chr_read(void *opaque
)
1723 CharDriverState
*chr
= opaque
;
1724 NetCharDriver
*s
= chr
->opaque
;
1726 if (s
->max_size
== 0)
1728 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1729 s
->bufptr
= s
->bufcnt
;
1734 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1735 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1737 s
->max_size
= qemu_chr_can_read(chr
);
1741 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1743 NetCharDriver
*s
= chr
->opaque
;
1746 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1747 udp_chr_read
, NULL
, chr
);
1751 static void udp_chr_close(CharDriverState
*chr
)
1753 NetCharDriver
*s
= chr
->opaque
;
1755 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1761 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1763 CharDriverState
*chr
= NULL
;
1764 NetCharDriver
*s
= NULL
;
1766 struct sockaddr_in saddr
;
1768 chr
= qemu_mallocz(sizeof(CharDriverState
));
1769 s
= qemu_mallocz(sizeof(NetCharDriver
));
1771 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1773 perror("socket(PF_INET, SOCK_DGRAM)");
1777 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1778 printf("Could not parse: %s\n", def
);
1782 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1792 chr
->chr_write
= udp_chr_write
;
1793 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1794 chr
->chr_close
= udp_chr_close
;
1807 /***********************************************************/
1808 /* TCP Net console */
1819 static void tcp_chr_accept(void *opaque
);
1821 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1823 TCPCharDriver
*s
= chr
->opaque
;
1825 return send_all(s
->fd
, buf
, len
);
1827 /* XXX: indicate an error ? */
1832 static int tcp_chr_read_poll(void *opaque
)
1834 CharDriverState
*chr
= opaque
;
1835 TCPCharDriver
*s
= chr
->opaque
;
1838 s
->max_size
= qemu_chr_can_read(chr
);
1843 #define IAC_BREAK 243
1844 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1846 uint8_t *buf
, int *size
)
1848 /* Handle any telnet client's basic IAC options to satisfy char by
1849 * char mode with no echo. All IAC options will be removed from
1850 * the buf and the do_telnetopt variable will be used to track the
1851 * state of the width of the IAC information.
1853 * IAC commands come in sets of 3 bytes with the exception of the
1854 * "IAC BREAK" command and the double IAC.
1860 for (i
= 0; i
< *size
; i
++) {
1861 if (s
->do_telnetopt
> 1) {
1862 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1863 /* Double IAC means send an IAC */
1867 s
->do_telnetopt
= 1;
1869 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1870 /* Handle IAC break commands by sending a serial break */
1871 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1876 if (s
->do_telnetopt
>= 4) {
1877 s
->do_telnetopt
= 1;
1880 if ((unsigned char)buf
[i
] == IAC
) {
1881 s
->do_telnetopt
= 2;
1892 static void tcp_chr_read(void *opaque
)
1894 CharDriverState
*chr
= opaque
;
1895 TCPCharDriver
*s
= chr
->opaque
;
1899 if (!s
->connected
|| s
->max_size
<= 0)
1902 if (len
> s
->max_size
)
1904 size
= recv(s
->fd
, buf
, len
, 0);
1906 /* connection closed */
1908 if (s
->listen_fd
>= 0) {
1909 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1911 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1914 } else if (size
> 0) {
1915 if (s
->do_telnetopt
)
1916 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1918 qemu_chr_read(chr
, buf
, size
);
1922 static void tcp_chr_connect(void *opaque
)
1924 CharDriverState
*chr
= opaque
;
1925 TCPCharDriver
*s
= chr
->opaque
;
1928 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1929 tcp_chr_read
, NULL
, chr
);
1930 qemu_chr_reset(chr
);
1933 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1934 static void tcp_chr_telnet_init(int fd
)
1937 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1938 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1939 send(fd
, (char *)buf
, 3, 0);
1940 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1941 send(fd
, (char *)buf
, 3, 0);
1942 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1943 send(fd
, (char *)buf
, 3, 0);
1944 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1945 send(fd
, (char *)buf
, 3, 0);
1948 static void socket_set_nodelay(int fd
)
1951 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1954 static void tcp_chr_accept(void *opaque
)
1956 CharDriverState
*chr
= opaque
;
1957 TCPCharDriver
*s
= chr
->opaque
;
1958 struct sockaddr_in saddr
;
1960 struct sockaddr_un uaddr
;
1962 struct sockaddr
*addr
;
1969 len
= sizeof(uaddr
);
1970 addr
= (struct sockaddr
*)&uaddr
;
1974 len
= sizeof(saddr
);
1975 addr
= (struct sockaddr
*)&saddr
;
1977 fd
= accept(s
->listen_fd
, addr
, &len
);
1978 if (fd
< 0 && errno
!= EINTR
) {
1980 } else if (fd
>= 0) {
1981 if (s
->do_telnetopt
)
1982 tcp_chr_telnet_init(fd
);
1986 socket_set_nonblock(fd
);
1988 socket_set_nodelay(fd
);
1990 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
1991 tcp_chr_connect(chr
);
1994 static void tcp_chr_close(CharDriverState
*chr
)
1996 TCPCharDriver
*s
= chr
->opaque
;
1998 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2001 if (s
->listen_fd
>= 0) {
2002 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2003 closesocket(s
->listen_fd
);
2008 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2012 CharDriverState
*chr
= NULL
;
2013 TCPCharDriver
*s
= NULL
;
2014 int fd
= -1, offset
= 0;
2016 int is_waitconnect
= 1;
2021 while((ptr
= strchr(ptr
,','))) {
2023 if (!strncmp(ptr
,"server",6)) {
2025 } else if (!strncmp(ptr
,"nowait",6)) {
2027 } else if (!strncmp(ptr
,"nodelay",6)) {
2029 } else if (!strncmp(ptr
,"to=",3)) {
2030 /* nothing, inet_listen() parses this one */;
2031 } else if (!strncmp(ptr
,"ipv4",4)) {
2032 /* nothing, inet_connect() and inet_listen() parse this one */;
2033 } else if (!strncmp(ptr
,"ipv6",4)) {
2034 /* nothing, inet_connect() and inet_listen() parse this one */;
2036 printf("Unknown option: %s\n", ptr
);
2043 chr
= qemu_mallocz(sizeof(CharDriverState
));
2044 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2047 chr
->filename
= qemu_malloc(256);
2049 pstrcpy(chr
->filename
, 256, "unix:");
2050 } else if (is_telnet
) {
2051 pstrcpy(chr
->filename
, 256, "telnet:");
2053 pstrcpy(chr
->filename
, 256, "tcp:");
2055 offset
= strlen(chr
->filename
);
2059 fd
= unix_listen(host_str
, chr
->filename
+ offset
, 256 - offset
);
2061 fd
= unix_connect(host_str
);
2065 fd
= inet_listen(host_str
, chr
->filename
+ offset
, 256 - offset
,
2068 fd
= inet_connect(host_str
, SOCK_STREAM
);
2074 if (!is_waitconnect
)
2075 socket_set_nonblock(fd
);
2080 s
->is_unix
= is_unix
;
2081 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2084 chr
->chr_write
= tcp_chr_write
;
2085 chr
->chr_close
= tcp_chr_close
;
2089 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2091 s
->do_telnetopt
= 1;
2095 socket_set_nodelay(fd
);
2096 tcp_chr_connect(chr
);
2099 if (is_listen
&& is_waitconnect
) {
2100 printf("QEMU waiting for connection on: %s\n",
2101 chr
->filename
? chr
->filename
: host_str
);
2102 tcp_chr_accept(chr
);
2103 socket_set_nonblock(s
->listen_fd
);
2115 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2118 CharDriverState
*chr
;
2120 if (!strcmp(filename
, "vc")) {
2121 chr
= text_console_init(0);
2123 if (strstart(filename
, "vc:", &p
)) {
2124 chr
= text_console_init(p
);
2126 if (!strcmp(filename
, "null")) {
2127 chr
= qemu_chr_open_null();
2129 if (strstart(filename
, "tcp:", &p
)) {
2130 chr
= qemu_chr_open_tcp(p
, 0, 0);
2132 if (strstart(filename
, "telnet:", &p
)) {
2133 chr
= qemu_chr_open_tcp(p
, 1, 0);
2135 if (strstart(filename
, "udp:", &p
)) {
2136 chr
= qemu_chr_open_udp(p
);
2138 if (strstart(filename
, "mon:", &p
)) {
2139 chr
= qemu_chr_open(label
, p
, NULL
);
2141 chr
= qemu_chr_open_mux(chr
);
2142 monitor_init(chr
, !nographic
);
2144 printf("Unable to open driver: %s\n", p
);
2146 } else if (!strcmp(filename
, "msmouse")) {
2147 chr
= qemu_chr_open_msmouse();
2150 if (strstart(filename
, "unix:", &p
)) {
2151 chr
= qemu_chr_open_tcp(p
, 0, 1);
2152 } else if (strstart(filename
, "file:", &p
)) {
2153 chr
= qemu_chr_open_file_out(p
);
2154 } else if (strstart(filename
, "pipe:", &p
)) {
2155 chr
= qemu_chr_open_pipe(p
);
2156 } else if (!strcmp(filename
, "pty")) {
2157 chr
= qemu_chr_open_pty();
2158 } else if (!strcmp(filename
, "stdio")) {
2159 chr
= qemu_chr_open_stdio();
2161 #if defined(__linux__)
2162 if (strstart(filename
, "/dev/parport", NULL
)) {
2163 chr
= qemu_chr_open_pp(filename
);
2165 #elif defined(__FreeBSD__)
2166 if (strstart(filename
, "/dev/ppi", NULL
)) {
2167 chr
= qemu_chr_open_pp(filename
);
2170 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2171 || defined(__NetBSD__) || defined(__OpenBSD__)
2172 if (strstart(filename
, "/dev/", NULL
)) {
2173 chr
= qemu_chr_open_tty(filename
);
2177 if (strstart(filename
, "COM", NULL
)) {
2178 chr
= qemu_chr_open_win(filename
);
2180 if (strstart(filename
, "pipe:", &p
)) {
2181 chr
= qemu_chr_open_win_pipe(p
);
2183 if (strstart(filename
, "con:", NULL
)) {
2184 chr
= qemu_chr_open_win_con(filename
);
2186 if (strstart(filename
, "file:", &p
)) {
2187 chr
= qemu_chr_open_win_file_out(p
);
2190 #ifdef CONFIG_BRLAPI
2191 if (!strcmp(filename
, "braille")) {
2192 chr
= chr_baum_init();
2201 chr
->filename
= qemu_strdup(filename
);
2203 chr
->label
= qemu_strdup(label
);
2204 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2209 void qemu_chr_close(CharDriverState
*chr
)
2211 TAILQ_REMOVE(&chardevs
, chr
, next
);
2213 chr
->chr_close(chr
);
2214 qemu_free(chr
->filename
);
2215 qemu_free(chr
->label
);
2219 void qemu_chr_info(void)
2221 CharDriverState
*chr
;
2223 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2224 term_printf("%s: filename=%s\n", chr
->label
, chr
->filename
);