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"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
34 #include "hw/msmouse.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
55 #include <net/if_tap.h>
58 #include <linux/if_tun.h>
60 #include <arpa/inet.h>
63 #include <sys/select.h>
68 #include <dev/ppbus/ppi.h>
69 #include <dev/ppbus/ppbconf.h>
70 #elif defined(__DragonFly__)
72 #include <dev/misc/ppi/ppi.h>
73 #include <bus/ppbus/ppbconf.h>
77 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
78 #include <freebsd/stdlib.h>
83 #include <linux/ppdev.h>
84 #include <linux/parport.h>
88 #include <sys/ethernet.h>
89 #include <sys/sockio.h>
90 #include <netinet/arp.h>
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_icmp.h> // must come after ip.h
95 #include <netinet/udp.h>
96 #include <netinet/tcp.h>
104 #include "qemu_socket.h"
106 /***********************************************************/
107 /* character device */
109 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
110 TAILQ_HEAD_INITIALIZER(chardevs
);
111 static int initial_reset_issued
;
113 static void qemu_chr_event(CharDriverState
*s
, int event
)
117 s
->chr_event(s
->handler_opaque
, event
);
120 static void qemu_chr_reset_bh(void *opaque
)
122 CharDriverState
*s
= opaque
;
123 qemu_chr_event(s
, CHR_EVENT_RESET
);
124 qemu_bh_delete(s
->bh
);
128 void qemu_chr_reset(CharDriverState
*s
)
130 if (s
->bh
== NULL
&& initial_reset_issued
) {
131 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
132 qemu_bh_schedule(s
->bh
);
136 void qemu_chr_initial_reset(void)
138 CharDriverState
*chr
;
140 initial_reset_issued
= 1;
142 TAILQ_FOREACH(chr
, &chardevs
, next
) {
147 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
149 return s
->chr_write(s
, buf
, len
);
152 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
156 return s
->chr_ioctl(s
, cmd
, arg
);
159 int qemu_chr_can_read(CharDriverState
*s
)
161 if (!s
->chr_can_read
)
163 return s
->chr_can_read(s
->handler_opaque
);
166 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
168 s
->chr_read(s
->handler_opaque
, buf
, len
);
171 void qemu_chr_accept_input(CharDriverState
*s
)
173 if (s
->chr_accept_input
)
174 s
->chr_accept_input(s
);
177 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
182 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
183 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
187 void qemu_chr_send_event(CharDriverState
*s
, int event
)
189 if (s
->chr_send_event
)
190 s
->chr_send_event(s
, event
);
193 void qemu_chr_add_handlers(CharDriverState
*s
,
194 IOCanRWHandler
*fd_can_read
,
195 IOReadHandler
*fd_read
,
196 IOEventHandler
*fd_event
,
199 s
->chr_can_read
= fd_can_read
;
200 s
->chr_read
= fd_read
;
201 s
->chr_event
= fd_event
;
202 s
->handler_opaque
= opaque
;
203 if (s
->chr_update_read_handler
)
204 s
->chr_update_read_handler(s
);
207 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
212 static CharDriverState
*qemu_chr_open_null(void)
214 CharDriverState
*chr
;
216 chr
= qemu_mallocz(sizeof(CharDriverState
));
217 chr
->chr_write
= null_chr_write
;
221 /* MUX driver for serial I/O splitting */
222 static int term_timestamps
;
223 static int64_t term_timestamps_start
;
225 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
226 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
228 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
229 IOReadHandler
*chr_read
[MAX_MUX
];
230 IOEventHandler
*chr_event
[MAX_MUX
];
231 void *ext_opaque
[MAX_MUX
];
232 CharDriverState
*drv
;
236 /* Intermediate input buffer allows to catch escape sequences even if the
237 currently active device is not accepting any input - but only until it
239 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
245 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
247 MuxDriver
*d
= chr
->opaque
;
249 if (!term_timestamps
) {
250 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
255 for(i
= 0; i
< len
; i
++) {
256 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
257 if (buf
[i
] == '\n') {
262 ti
= qemu_get_clock(rt_clock
);
263 if (term_timestamps_start
== -1)
264 term_timestamps_start
= ti
;
265 ti
-= term_timestamps_start
;
267 snprintf(buf1
, sizeof(buf1
),
268 "[%02d:%02d:%02d.%03d] ",
273 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
280 static const char * const mux_help
[] = {
281 "% h print this help\n\r",
282 "% x exit emulator\n\r",
283 "% s save disk data back to file (if -snapshot)\n\r",
284 "% t toggle console timestamps\n\r"
285 "% b send break (magic sysrq)\n\r",
286 "% c switch between console and monitor\n\r",
291 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
292 static void mux_print_help(CharDriverState
*chr
)
295 char ebuf
[15] = "Escape-Char";
296 char cbuf
[50] = "\n\r";
298 if (term_escape_char
> 0 && term_escape_char
< 26) {
299 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
300 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
302 snprintf(cbuf
, sizeof(cbuf
),
303 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
306 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
307 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
308 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
309 if (mux_help
[i
][j
] == '%')
310 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
312 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
317 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
319 if (d
->chr_event
[mux_nr
])
320 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
323 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
325 if (d
->term_got_escape
) {
326 d
->term_got_escape
= 0;
327 if (ch
== term_escape_char
)
336 const char *term
= "QEMU: Terminated\n\r";
337 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
344 for (i
= 0; i
< nb_drives
; i
++) {
345 bdrv_commit(drives_table
[i
].bdrv
);
350 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
353 /* Switch to the next registered device */
354 mux_chr_send_event(d
, chr
->focus
, CHR_EVENT_MUX_OUT
);
356 if (chr
->focus
>= d
->mux_cnt
)
358 mux_chr_send_event(d
, chr
->focus
, CHR_EVENT_MUX_IN
);
361 term_timestamps
= !term_timestamps
;
362 term_timestamps_start
= -1;
365 } else if (ch
== term_escape_char
) {
366 d
->term_got_escape
= 1;
374 static void mux_chr_accept_input(CharDriverState
*chr
)
377 MuxDriver
*d
= chr
->opaque
;
379 while (d
->prod
[m
] != d
->cons
[m
] &&
380 d
->chr_can_read
[m
] &&
381 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
382 d
->chr_read
[m
](d
->ext_opaque
[m
],
383 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
387 static int mux_chr_can_read(void *opaque
)
389 CharDriverState
*chr
= opaque
;
390 MuxDriver
*d
= chr
->opaque
;
393 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
395 if (d
->chr_can_read
[m
])
396 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
400 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
402 CharDriverState
*chr
= opaque
;
403 MuxDriver
*d
= chr
->opaque
;
407 mux_chr_accept_input (opaque
);
409 for(i
= 0; i
< size
; i
++)
410 if (mux_proc_byte(chr
, d
, buf
[i
])) {
411 if (d
->prod
[m
] == d
->cons
[m
] &&
412 d
->chr_can_read
[m
] &&
413 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
414 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
416 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
420 static void mux_chr_event(void *opaque
, int event
)
422 CharDriverState
*chr
= opaque
;
423 MuxDriver
*d
= chr
->opaque
;
426 /* Send the event to all registered listeners */
427 for (i
= 0; i
< d
->mux_cnt
; i
++)
428 mux_chr_send_event(d
, i
, event
);
431 static void mux_chr_update_read_handler(CharDriverState
*chr
)
433 MuxDriver
*d
= chr
->opaque
;
435 if (d
->mux_cnt
>= MAX_MUX
) {
436 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
439 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
440 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
441 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
442 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
443 /* Fix up the real driver with mux routines */
444 if (d
->mux_cnt
== 0) {
445 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
448 chr
->focus
= d
->mux_cnt
;
452 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
454 CharDriverState
*chr
;
457 chr
= qemu_mallocz(sizeof(CharDriverState
));
458 d
= qemu_mallocz(sizeof(MuxDriver
));
463 chr
->chr_write
= mux_chr_write
;
464 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
465 chr
->chr_accept_input
= mux_chr_accept_input
;
471 int send_all(int fd
, const void *buf
, int len1
)
477 ret
= send(fd
, buf
, len
, 0);
479 errno
= WSAGetLastError();
480 if (errno
!= WSAEWOULDBLOCK
) {
483 } else if (ret
== 0) {
495 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
501 ret
= write(fd
, buf
, len
);
503 if (errno
!= EINTR
&& errno
!= EAGAIN
)
505 } else if (ret
== 0) {
515 int send_all(int fd
, const void *buf
, int len1
)
517 return unix_write(fd
, buf
, len1
);
528 #define STDIO_MAX_CLIENTS 1
529 static int stdio_nb_clients
= 0;
531 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
533 FDCharDriver
*s
= chr
->opaque
;
534 return send_all(s
->fd_out
, buf
, len
);
537 static int fd_chr_read_poll(void *opaque
)
539 CharDriverState
*chr
= opaque
;
540 FDCharDriver
*s
= chr
->opaque
;
542 s
->max_size
= qemu_chr_can_read(chr
);
546 static void fd_chr_read(void *opaque
)
548 CharDriverState
*chr
= opaque
;
549 FDCharDriver
*s
= chr
->opaque
;
554 if (len
> s
->max_size
)
558 size
= read(s
->fd_in
, buf
, len
);
560 /* FD has been closed. Remove it from the active list. */
561 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
565 qemu_chr_read(chr
, buf
, size
);
569 static void fd_chr_update_read_handler(CharDriverState
*chr
)
571 FDCharDriver
*s
= chr
->opaque
;
574 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
576 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
577 fd_chr_read
, NULL
, chr
);
582 static void fd_chr_close(struct CharDriverState
*chr
)
584 FDCharDriver
*s
= chr
->opaque
;
587 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
589 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
596 /* open a character device to a unix fd */
597 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
599 CharDriverState
*chr
;
602 chr
= qemu_mallocz(sizeof(CharDriverState
));
603 s
= qemu_mallocz(sizeof(FDCharDriver
));
607 chr
->chr_write
= fd_chr_write
;
608 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
609 chr
->chr_close
= fd_chr_close
;
616 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
620 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
623 return qemu_chr_open_fd(-1, fd_out
);
626 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
629 char filename_in
[256], filename_out
[256];
631 snprintf(filename_in
, 256, "%s.in", filename
);
632 snprintf(filename_out
, 256, "%s.out", filename
);
633 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
634 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
635 if (fd_in
< 0 || fd_out
< 0) {
640 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
644 return qemu_chr_open_fd(fd_in
, fd_out
);
648 /* for STDIO, we handle the case where several clients use it
651 #define TERM_FIFO_MAX_SIZE 1
653 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
654 static int term_fifo_size
;
656 static int stdio_read_poll(void *opaque
)
658 CharDriverState
*chr
= opaque
;
660 /* try to flush the queue if needed */
661 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
662 qemu_chr_read(chr
, term_fifo
, 1);
665 /* see if we can absorb more chars */
666 if (term_fifo_size
== 0)
672 static void stdio_read(void *opaque
)
676 CharDriverState
*chr
= opaque
;
678 size
= read(0, buf
, 1);
680 /* stdin has been closed. Remove it from the active list. */
681 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
685 if (qemu_chr_can_read(chr
) > 0) {
686 qemu_chr_read(chr
, buf
, 1);
687 } else if (term_fifo_size
== 0) {
688 term_fifo
[term_fifo_size
++] = buf
[0];
693 /* init terminal so that we can grab keys */
694 static struct termios oldtty
;
695 static int old_fd0_flags
;
696 static int term_atexit_done
;
698 static void term_exit(void)
700 tcsetattr (0, TCSANOW
, &oldtty
);
701 fcntl(0, F_SETFL
, old_fd0_flags
);
704 static void term_init(void)
710 old_fd0_flags
= fcntl(0, F_GETFL
);
712 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
713 |INLCR
|IGNCR
|ICRNL
|IXON
);
714 tty
.c_oflag
|= OPOST
;
715 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
716 /* if graphical mode, we allow Ctrl-C handling */
717 if (display_type
== DT_NOGRAPHIC
)
718 tty
.c_lflag
&= ~ISIG
;
719 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
724 tcsetattr (0, TCSANOW
, &tty
);
726 if (!term_atexit_done
++)
729 fcntl(0, F_SETFL
, O_NONBLOCK
);
732 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
736 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
740 static CharDriverState
*qemu_chr_open_stdio(void)
742 CharDriverState
*chr
;
744 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
746 chr
= qemu_chr_open_fd(0, 1);
747 chr
->chr_close
= qemu_chr_close_stdio
;
748 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
756 /* Once Solaris has openpty(), this is going to be removed. */
757 static int openpty(int *amaster
, int *aslave
, char *name
,
758 struct termios
*termp
, struct winsize
*winp
)
761 int mfd
= -1, sfd
= -1;
763 *amaster
= *aslave
= -1;
765 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
769 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
772 if ((slave
= ptsname(mfd
)) == NULL
)
775 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
778 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
779 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
787 ioctl(sfd
, TIOCSWINSZ
, winp
);
798 static void cfmakeraw (struct termios
*termios_p
)
800 termios_p
->c_iflag
&=
801 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
802 termios_p
->c_oflag
&= ~OPOST
;
803 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
804 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
805 termios_p
->c_cflag
|= CS8
;
807 termios_p
->c_cc
[VMIN
] = 0;
808 termios_p
->c_cc
[VTIME
] = 0;
812 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
813 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
823 static void pty_chr_update_read_handler(CharDriverState
*chr
);
824 static void pty_chr_state(CharDriverState
*chr
, int connected
);
826 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
828 PtyCharDriver
*s
= chr
->opaque
;
831 /* guest sends data, check for (re-)connect */
832 pty_chr_update_read_handler(chr
);
835 return send_all(s
->fd
, buf
, len
);
838 static int pty_chr_read_poll(void *opaque
)
840 CharDriverState
*chr
= opaque
;
841 PtyCharDriver
*s
= chr
->opaque
;
843 s
->read_bytes
= qemu_chr_can_read(chr
);
844 return s
->read_bytes
;
847 static void pty_chr_read(void *opaque
)
849 CharDriverState
*chr
= opaque
;
850 PtyCharDriver
*s
= chr
->opaque
;
855 if (len
> s
->read_bytes
)
859 size
= read(s
->fd
, buf
, len
);
860 if ((size
== -1 && errno
== EIO
) ||
862 pty_chr_state(chr
, 0);
866 pty_chr_state(chr
, 1);
867 qemu_chr_read(chr
, buf
, size
);
871 static void pty_chr_update_read_handler(CharDriverState
*chr
)
873 PtyCharDriver
*s
= chr
->opaque
;
875 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
876 pty_chr_read
, NULL
, chr
);
879 * Short timeout here: just need wait long enougth that qemu makes
880 * it through the poll loop once. When reconnected we want a
881 * short timeout so we notice it almost instantly. Otherwise
882 * read() gives us -EIO instantly, making pty_chr_state() reset the
883 * timeout to the normal (much longer) poll interval before the
886 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
889 static void pty_chr_state(CharDriverState
*chr
, int connected
)
891 PtyCharDriver
*s
= chr
->opaque
;
894 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
897 /* (re-)connect poll interval for idle guests: once per second.
898 * We check more frequently in case the guests sends data to
899 * the virtual device linked to our pty. */
900 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
908 static void pty_chr_timer(void *opaque
)
910 struct CharDriverState
*chr
= opaque
;
911 PtyCharDriver
*s
= chr
->opaque
;
916 /* If we arrive here without polling being cleared due
917 * read returning -EIO, then we are (re-)connected */
918 pty_chr_state(chr
, 1);
923 pty_chr_update_read_handler(chr
);
926 static void pty_chr_close(struct CharDriverState
*chr
)
928 PtyCharDriver
*s
= chr
->opaque
;
930 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
932 qemu_del_timer(s
->timer
);
933 qemu_free_timer(s
->timer
);
937 static CharDriverState
*qemu_chr_open_pty(void)
939 CharDriverState
*chr
;
943 #if defined(__OpenBSD__) || defined(__DragonFly__)
944 char pty_name
[PATH_MAX
];
945 #define q_ptsname(x) pty_name
947 char *pty_name
= NULL
;
948 #define q_ptsname(x) ptsname(x)
951 chr
= qemu_mallocz(sizeof(CharDriverState
));
952 s
= qemu_mallocz(sizeof(PtyCharDriver
));
954 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
958 /* Set raw attributes on the pty. */
959 tcgetattr(slave_fd
, &tty
);
961 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
964 len
= strlen(q_ptsname(s
->fd
)) + 5;
965 chr
->filename
= qemu_malloc(len
);
966 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
967 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
970 chr
->chr_write
= pty_chr_write
;
971 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
972 chr
->chr_close
= pty_chr_close
;
974 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
979 static void tty_serial_init(int fd
, int speed
,
980 int parity
, int data_bits
, int stop_bits
)
986 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
987 speed
, parity
, data_bits
, stop_bits
);
989 tcgetattr (fd
, &tty
);
992 if (speed
<= 50 * MARGIN
)
994 else if (speed
<= 75 * MARGIN
)
996 else if (speed
<= 300 * MARGIN
)
998 else if (speed
<= 600 * MARGIN
)
1000 else if (speed
<= 1200 * MARGIN
)
1002 else if (speed
<= 2400 * MARGIN
)
1004 else if (speed
<= 4800 * MARGIN
)
1006 else if (speed
<= 9600 * MARGIN
)
1008 else if (speed
<= 19200 * MARGIN
)
1010 else if (speed
<= 38400 * MARGIN
)
1012 else if (speed
<= 57600 * MARGIN
)
1014 else if (speed
<= 115200 * MARGIN
)
1019 cfsetispeed(&tty
, spd
);
1020 cfsetospeed(&tty
, spd
);
1022 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1023 |INLCR
|IGNCR
|ICRNL
|IXON
);
1024 tty
.c_oflag
|= OPOST
;
1025 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1026 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1047 tty
.c_cflag
|= PARENB
;
1050 tty
.c_cflag
|= PARENB
| PARODD
;
1054 tty
.c_cflag
|= CSTOPB
;
1056 tcsetattr (fd
, TCSANOW
, &tty
);
1059 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1061 FDCharDriver
*s
= chr
->opaque
;
1064 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1066 QEMUSerialSetParams
*ssp
= arg
;
1067 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1068 ssp
->data_bits
, ssp
->stop_bits
);
1071 case CHR_IOCTL_SERIAL_SET_BREAK
:
1073 int enable
= *(int *)arg
;
1075 tcsendbreak(s
->fd_in
, 1);
1078 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1081 int *targ
= (int *)arg
;
1082 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1084 if (sarg
& TIOCM_CTS
)
1085 *targ
|= CHR_TIOCM_CTS
;
1086 if (sarg
& TIOCM_CAR
)
1087 *targ
|= CHR_TIOCM_CAR
;
1088 if (sarg
& TIOCM_DSR
)
1089 *targ
|= CHR_TIOCM_DSR
;
1090 if (sarg
& TIOCM_RI
)
1091 *targ
|= CHR_TIOCM_RI
;
1092 if (sarg
& TIOCM_DTR
)
1093 *targ
|= CHR_TIOCM_DTR
;
1094 if (sarg
& TIOCM_RTS
)
1095 *targ
|= CHR_TIOCM_RTS
;
1098 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1100 int sarg
= *(int *)arg
;
1102 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1103 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1104 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1105 if (sarg
& CHR_TIOCM_CTS
)
1107 if (sarg
& CHR_TIOCM_CAR
)
1109 if (sarg
& CHR_TIOCM_DSR
)
1111 if (sarg
& CHR_TIOCM_RI
)
1113 if (sarg
& CHR_TIOCM_DTR
)
1115 if (sarg
& CHR_TIOCM_RTS
)
1117 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1126 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1128 CharDriverState
*chr
;
1131 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1132 tty_serial_init(fd
, 115200, 'N', 8, 1);
1133 chr
= qemu_chr_open_fd(fd
, fd
);
1138 chr
->chr_ioctl
= tty_serial_ioctl
;
1139 qemu_chr_reset(chr
);
1142 #else /* ! __linux__ && ! __sun__ */
1143 static CharDriverState
*qemu_chr_open_pty(void)
1147 #endif /* __linux__ || __sun__ */
1149 #if defined(__linux__)
1153 } ParallelCharDriver
;
1155 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1157 if (s
->mode
!= mode
) {
1159 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1166 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1168 ParallelCharDriver
*drv
= chr
->opaque
;
1173 case CHR_IOCTL_PP_READ_DATA
:
1174 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1176 *(uint8_t *)arg
= b
;
1178 case CHR_IOCTL_PP_WRITE_DATA
:
1179 b
= *(uint8_t *)arg
;
1180 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1183 case CHR_IOCTL_PP_READ_CONTROL
:
1184 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1186 /* Linux gives only the lowest bits, and no way to know data
1187 direction! For better compatibility set the fixed upper
1189 *(uint8_t *)arg
= b
| 0xc0;
1191 case CHR_IOCTL_PP_WRITE_CONTROL
:
1192 b
= *(uint8_t *)arg
;
1193 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1196 case CHR_IOCTL_PP_READ_STATUS
:
1197 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1199 *(uint8_t *)arg
= b
;
1201 case CHR_IOCTL_PP_DATA_DIR
:
1202 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1205 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1206 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1207 struct ParallelIOArg
*parg
= arg
;
1208 int n
= read(fd
, parg
->buffer
, parg
->count
);
1209 if (n
!= parg
->count
) {
1214 case CHR_IOCTL_PP_EPP_READ
:
1215 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1216 struct ParallelIOArg
*parg
= arg
;
1217 int n
= read(fd
, parg
->buffer
, parg
->count
);
1218 if (n
!= parg
->count
) {
1223 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1224 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1225 struct ParallelIOArg
*parg
= arg
;
1226 int n
= write(fd
, parg
->buffer
, parg
->count
);
1227 if (n
!= parg
->count
) {
1232 case CHR_IOCTL_PP_EPP_WRITE
:
1233 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1234 struct ParallelIOArg
*parg
= arg
;
1235 int n
= write(fd
, parg
->buffer
, parg
->count
);
1236 if (n
!= parg
->count
) {
1247 static void pp_close(CharDriverState
*chr
)
1249 ParallelCharDriver
*drv
= chr
->opaque
;
1252 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1253 ioctl(fd
, PPRELEASE
);
1258 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1260 CharDriverState
*chr
;
1261 ParallelCharDriver
*drv
;
1264 TFR(fd
= open(filename
, O_RDWR
));
1268 if (ioctl(fd
, PPCLAIM
) < 0) {
1273 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1275 drv
->mode
= IEEE1284_MODE_COMPAT
;
1277 chr
= qemu_mallocz(sizeof(CharDriverState
));
1278 chr
->chr_write
= null_chr_write
;
1279 chr
->chr_ioctl
= pp_ioctl
;
1280 chr
->chr_close
= pp_close
;
1283 qemu_chr_reset(chr
);
1287 #endif /* __linux__ */
1289 #if defined(__FreeBSD__) || defined(__DragonFly__)
1290 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1292 int fd
= (int)chr
->opaque
;
1296 case CHR_IOCTL_PP_READ_DATA
:
1297 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1299 *(uint8_t *)arg
= b
;
1301 case CHR_IOCTL_PP_WRITE_DATA
:
1302 b
= *(uint8_t *)arg
;
1303 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1306 case CHR_IOCTL_PP_READ_CONTROL
:
1307 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1309 *(uint8_t *)arg
= b
;
1311 case CHR_IOCTL_PP_WRITE_CONTROL
:
1312 b
= *(uint8_t *)arg
;
1313 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1316 case CHR_IOCTL_PP_READ_STATUS
:
1317 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1319 *(uint8_t *)arg
= b
;
1327 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1329 CharDriverState
*chr
;
1332 fd
= open(filename
, O_RDWR
);
1336 chr
= qemu_mallocz(sizeof(CharDriverState
));
1337 chr
->opaque
= (void *)fd
;
1338 chr
->chr_write
= null_chr_write
;
1339 chr
->chr_ioctl
= pp_ioctl
;
1348 HANDLE hcom
, hrecv
, hsend
;
1349 OVERLAPPED orecv
, osend
;
1354 #define NSENDBUF 2048
1355 #define NRECVBUF 2048
1356 #define MAXCONNECT 1
1357 #define NTIMEOUT 5000
1359 static int win_chr_poll(void *opaque
);
1360 static int win_chr_pipe_poll(void *opaque
);
1362 static void win_chr_close(CharDriverState
*chr
)
1364 WinCharState
*s
= chr
->opaque
;
1367 CloseHandle(s
->hsend
);
1371 CloseHandle(s
->hrecv
);
1375 CloseHandle(s
->hcom
);
1379 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1381 qemu_del_polling_cb(win_chr_poll
, chr
);
1384 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1386 WinCharState
*s
= chr
->opaque
;
1388 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1393 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1395 fprintf(stderr
, "Failed CreateEvent\n");
1398 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1400 fprintf(stderr
, "Failed CreateEvent\n");
1404 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1405 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1406 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1407 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1412 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1413 fprintf(stderr
, "Failed SetupComm\n");
1417 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1418 size
= sizeof(COMMCONFIG
);
1419 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1420 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1421 CommConfigDialog(filename
, NULL
, &comcfg
);
1423 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1424 fprintf(stderr
, "Failed SetCommState\n");
1428 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1429 fprintf(stderr
, "Failed SetCommMask\n");
1433 cto
.ReadIntervalTimeout
= MAXDWORD
;
1434 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1435 fprintf(stderr
, "Failed SetCommTimeouts\n");
1439 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1440 fprintf(stderr
, "Failed ClearCommError\n");
1443 qemu_add_polling_cb(win_chr_poll
, chr
);
1451 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1453 WinCharState
*s
= chr
->opaque
;
1454 DWORD len
, ret
, size
, err
;
1457 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1458 s
->osend
.hEvent
= s
->hsend
;
1461 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1463 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1465 err
= GetLastError();
1466 if (err
== ERROR_IO_PENDING
) {
1467 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1485 static int win_chr_read_poll(CharDriverState
*chr
)
1487 WinCharState
*s
= chr
->opaque
;
1489 s
->max_size
= qemu_chr_can_read(chr
);
1493 static void win_chr_readfile(CharDriverState
*chr
)
1495 WinCharState
*s
= chr
->opaque
;
1500 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1501 s
->orecv
.hEvent
= s
->hrecv
;
1502 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1504 err
= GetLastError();
1505 if (err
== ERROR_IO_PENDING
) {
1506 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1511 qemu_chr_read(chr
, buf
, size
);
1515 static void win_chr_read(CharDriverState
*chr
)
1517 WinCharState
*s
= chr
->opaque
;
1519 if (s
->len
> s
->max_size
)
1520 s
->len
= s
->max_size
;
1524 win_chr_readfile(chr
);
1527 static int win_chr_poll(void *opaque
)
1529 CharDriverState
*chr
= opaque
;
1530 WinCharState
*s
= chr
->opaque
;
1534 ClearCommError(s
->hcom
, &comerr
, &status
);
1535 if (status
.cbInQue
> 0) {
1536 s
->len
= status
.cbInQue
;
1537 win_chr_read_poll(chr
);
1544 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1546 CharDriverState
*chr
;
1549 chr
= qemu_mallocz(sizeof(CharDriverState
));
1550 s
= qemu_mallocz(sizeof(WinCharState
));
1552 chr
->chr_write
= win_chr_write
;
1553 chr
->chr_close
= win_chr_close
;
1555 if (win_chr_init(chr
, filename
) < 0) {
1560 qemu_chr_reset(chr
);
1564 static int win_chr_pipe_poll(void *opaque
)
1566 CharDriverState
*chr
= opaque
;
1567 WinCharState
*s
= chr
->opaque
;
1570 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1573 win_chr_read_poll(chr
);
1580 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1582 WinCharState
*s
= chr
->opaque
;
1590 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1592 fprintf(stderr
, "Failed CreateEvent\n");
1595 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1597 fprintf(stderr
, "Failed CreateEvent\n");
1601 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1602 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1603 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1605 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1606 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1607 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1612 ZeroMemory(&ov
, sizeof(ov
));
1613 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1614 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1616 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1620 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1622 fprintf(stderr
, "Failed GetOverlappedResult\n");
1624 CloseHandle(ov
.hEvent
);
1631 CloseHandle(ov
.hEvent
);
1634 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1643 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1645 CharDriverState
*chr
;
1648 chr
= qemu_mallocz(sizeof(CharDriverState
));
1649 s
= qemu_mallocz(sizeof(WinCharState
));
1651 chr
->chr_write
= win_chr_write
;
1652 chr
->chr_close
= win_chr_close
;
1654 if (win_chr_pipe_init(chr
, filename
) < 0) {
1659 qemu_chr_reset(chr
);
1663 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1665 CharDriverState
*chr
;
1668 chr
= qemu_mallocz(sizeof(CharDriverState
));
1669 s
= qemu_mallocz(sizeof(WinCharState
));
1672 chr
->chr_write
= win_chr_write
;
1673 qemu_chr_reset(chr
);
1677 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1679 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1682 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1686 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1687 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1688 if (fd_out
== INVALID_HANDLE_VALUE
)
1691 return qemu_chr_open_win_file(fd_out
);
1693 #endif /* !_WIN32 */
1695 /***********************************************************/
1696 /* UDP Net console */
1700 struct sockaddr_in daddr
;
1707 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1709 NetCharDriver
*s
= chr
->opaque
;
1711 return sendto(s
->fd
, buf
, len
, 0,
1712 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1715 static int udp_chr_read_poll(void *opaque
)
1717 CharDriverState
*chr
= opaque
;
1718 NetCharDriver
*s
= chr
->opaque
;
1720 s
->max_size
= qemu_chr_can_read(chr
);
1722 /* If there were any stray characters in the queue process them
1725 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1726 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1728 s
->max_size
= qemu_chr_can_read(chr
);
1733 static void udp_chr_read(void *opaque
)
1735 CharDriverState
*chr
= opaque
;
1736 NetCharDriver
*s
= chr
->opaque
;
1738 if (s
->max_size
== 0)
1740 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1741 s
->bufptr
= s
->bufcnt
;
1746 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1747 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1749 s
->max_size
= qemu_chr_can_read(chr
);
1753 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1755 NetCharDriver
*s
= chr
->opaque
;
1758 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1759 udp_chr_read
, NULL
, chr
);
1763 static void udp_chr_close(CharDriverState
*chr
)
1765 NetCharDriver
*s
= chr
->opaque
;
1767 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1773 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1775 CharDriverState
*chr
= NULL
;
1776 NetCharDriver
*s
= NULL
;
1778 struct sockaddr_in saddr
;
1780 chr
= qemu_mallocz(sizeof(CharDriverState
));
1781 s
= qemu_mallocz(sizeof(NetCharDriver
));
1783 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1785 perror("socket(PF_INET, SOCK_DGRAM)");
1789 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1790 printf("Could not parse: %s\n", def
);
1794 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1804 chr
->chr_write
= udp_chr_write
;
1805 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1806 chr
->chr_close
= udp_chr_close
;
1819 /***********************************************************/
1820 /* TCP Net console */
1831 static void tcp_chr_accept(void *opaque
);
1833 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1835 TCPCharDriver
*s
= chr
->opaque
;
1837 return send_all(s
->fd
, buf
, len
);
1839 /* XXX: indicate an error ? */
1844 static int tcp_chr_read_poll(void *opaque
)
1846 CharDriverState
*chr
= opaque
;
1847 TCPCharDriver
*s
= chr
->opaque
;
1850 s
->max_size
= qemu_chr_can_read(chr
);
1855 #define IAC_BREAK 243
1856 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1858 uint8_t *buf
, int *size
)
1860 /* Handle any telnet client's basic IAC options to satisfy char by
1861 * char mode with no echo. All IAC options will be removed from
1862 * the buf and the do_telnetopt variable will be used to track the
1863 * state of the width of the IAC information.
1865 * IAC commands come in sets of 3 bytes with the exception of the
1866 * "IAC BREAK" command and the double IAC.
1872 for (i
= 0; i
< *size
; i
++) {
1873 if (s
->do_telnetopt
> 1) {
1874 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1875 /* Double IAC means send an IAC */
1879 s
->do_telnetopt
= 1;
1881 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1882 /* Handle IAC break commands by sending a serial break */
1883 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1888 if (s
->do_telnetopt
>= 4) {
1889 s
->do_telnetopt
= 1;
1892 if ((unsigned char)buf
[i
] == IAC
) {
1893 s
->do_telnetopt
= 2;
1904 static void tcp_chr_read(void *opaque
)
1906 CharDriverState
*chr
= opaque
;
1907 TCPCharDriver
*s
= chr
->opaque
;
1911 if (!s
->connected
|| s
->max_size
<= 0)
1914 if (len
> s
->max_size
)
1916 size
= recv(s
->fd
, buf
, len
, 0);
1918 /* connection closed */
1920 if (s
->listen_fd
>= 0) {
1921 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1923 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1926 } else if (size
> 0) {
1927 if (s
->do_telnetopt
)
1928 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1930 qemu_chr_read(chr
, buf
, size
);
1934 static void tcp_chr_connect(void *opaque
)
1936 CharDriverState
*chr
= opaque
;
1937 TCPCharDriver
*s
= chr
->opaque
;
1940 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1941 tcp_chr_read
, NULL
, chr
);
1942 qemu_chr_reset(chr
);
1945 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1946 static void tcp_chr_telnet_init(int fd
)
1949 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1950 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1951 send(fd
, (char *)buf
, 3, 0);
1952 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1953 send(fd
, (char *)buf
, 3, 0);
1954 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1955 send(fd
, (char *)buf
, 3, 0);
1956 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1957 send(fd
, (char *)buf
, 3, 0);
1960 static void socket_set_nodelay(int fd
)
1963 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1966 static void tcp_chr_accept(void *opaque
)
1968 CharDriverState
*chr
= opaque
;
1969 TCPCharDriver
*s
= chr
->opaque
;
1970 struct sockaddr_in saddr
;
1972 struct sockaddr_un uaddr
;
1974 struct sockaddr
*addr
;
1981 len
= sizeof(uaddr
);
1982 addr
= (struct sockaddr
*)&uaddr
;
1986 len
= sizeof(saddr
);
1987 addr
= (struct sockaddr
*)&saddr
;
1989 fd
= accept(s
->listen_fd
, addr
, &len
);
1990 if (fd
< 0 && errno
!= EINTR
) {
1992 } else if (fd
>= 0) {
1993 if (s
->do_telnetopt
)
1994 tcp_chr_telnet_init(fd
);
1998 socket_set_nonblock(fd
);
2000 socket_set_nodelay(fd
);
2002 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2003 tcp_chr_connect(chr
);
2006 static void tcp_chr_close(CharDriverState
*chr
)
2008 TCPCharDriver
*s
= chr
->opaque
;
2010 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2013 if (s
->listen_fd
>= 0) {
2014 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2015 closesocket(s
->listen_fd
);
2020 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2024 CharDriverState
*chr
= NULL
;
2025 TCPCharDriver
*s
= NULL
;
2026 int fd
= -1, offset
= 0;
2028 int is_waitconnect
= 1;
2033 while((ptr
= strchr(ptr
,','))) {
2035 if (!strncmp(ptr
,"server",6)) {
2037 } else if (!strncmp(ptr
,"nowait",6)) {
2039 } else if (!strncmp(ptr
,"nodelay",6)) {
2041 } else if (!strncmp(ptr
,"to=",3)) {
2042 /* nothing, inet_listen() parses this one */;
2043 } else if (!strncmp(ptr
,"ipv4",4)) {
2044 /* nothing, inet_connect() and inet_listen() parse this one */;
2045 } else if (!strncmp(ptr
,"ipv6",4)) {
2046 /* nothing, inet_connect() and inet_listen() parse this one */;
2048 printf("Unknown option: %s\n", ptr
);
2055 chr
= qemu_mallocz(sizeof(CharDriverState
));
2056 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2059 chr
->filename
= qemu_malloc(256);
2061 pstrcpy(chr
->filename
, 256, "unix:");
2062 } else if (is_telnet
) {
2063 pstrcpy(chr
->filename
, 256, "telnet:");
2065 pstrcpy(chr
->filename
, 256, "tcp:");
2067 offset
= strlen(chr
->filename
);
2071 fd
= unix_listen(host_str
, chr
->filename
+ offset
, 256 - offset
);
2073 fd
= unix_connect(host_str
);
2077 fd
= inet_listen(host_str
, chr
->filename
+ offset
, 256 - offset
,
2080 fd
= inet_connect(host_str
, SOCK_STREAM
);
2086 if (!is_waitconnect
)
2087 socket_set_nonblock(fd
);
2092 s
->is_unix
= is_unix
;
2093 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2096 chr
->chr_write
= tcp_chr_write
;
2097 chr
->chr_close
= tcp_chr_close
;
2101 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2103 s
->do_telnetopt
= 1;
2107 socket_set_nodelay(fd
);
2108 tcp_chr_connect(chr
);
2111 if (is_listen
&& is_waitconnect
) {
2112 printf("QEMU waiting for connection on: %s\n",
2113 chr
->filename
? chr
->filename
: host_str
);
2114 tcp_chr_accept(chr
);
2115 socket_set_nonblock(s
->listen_fd
);
2127 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2130 CharDriverState
*chr
;
2132 if (!strcmp(filename
, "vc")) {
2133 chr
= text_console_init(0);
2135 if (strstart(filename
, "vc:", &p
)) {
2136 chr
= text_console_init(p
);
2138 if (!strcmp(filename
, "null")) {
2139 chr
= qemu_chr_open_null();
2141 if (strstart(filename
, "tcp:", &p
)) {
2142 chr
= qemu_chr_open_tcp(p
, 0, 0);
2144 if (strstart(filename
, "telnet:", &p
)) {
2145 chr
= qemu_chr_open_tcp(p
, 1, 0);
2147 if (strstart(filename
, "udp:", &p
)) {
2148 chr
= qemu_chr_open_udp(p
);
2150 if (strstart(filename
, "mon:", &p
)) {
2151 chr
= qemu_chr_open(label
, p
, NULL
);
2153 chr
= qemu_chr_open_mux(chr
);
2154 monitor_init(chr
, MONITOR_USE_READLINE
);
2156 printf("Unable to open driver: %s\n", p
);
2158 } else if (!strcmp(filename
, "msmouse")) {
2159 chr
= qemu_chr_open_msmouse();
2162 if (strstart(filename
, "unix:", &p
)) {
2163 chr
= qemu_chr_open_tcp(p
, 0, 1);
2164 } else if (strstart(filename
, "file:", &p
)) {
2165 chr
= qemu_chr_open_file_out(p
);
2166 } else if (strstart(filename
, "pipe:", &p
)) {
2167 chr
= qemu_chr_open_pipe(p
);
2168 } else if (!strcmp(filename
, "pty")) {
2169 chr
= qemu_chr_open_pty();
2170 } else if (!strcmp(filename
, "stdio")) {
2171 chr
= qemu_chr_open_stdio();
2173 #if defined(__linux__)
2174 if (strstart(filename
, "/dev/parport", NULL
)) {
2175 chr
= qemu_chr_open_pp(filename
);
2177 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2178 if (strstart(filename
, "/dev/ppi", NULL
)) {
2179 chr
= qemu_chr_open_pp(filename
);
2182 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2183 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2184 if (strstart(filename
, "/dev/", NULL
)) {
2185 chr
= qemu_chr_open_tty(filename
);
2189 if (strstart(filename
, "COM", NULL
)) {
2190 chr
= qemu_chr_open_win(filename
);
2192 if (strstart(filename
, "pipe:", &p
)) {
2193 chr
= qemu_chr_open_win_pipe(p
);
2195 if (strstart(filename
, "con:", NULL
)) {
2196 chr
= qemu_chr_open_win_con(filename
);
2198 if (strstart(filename
, "file:", &p
)) {
2199 chr
= qemu_chr_open_win_file_out(p
);
2202 #ifdef CONFIG_BRLAPI
2203 if (!strcmp(filename
, "braille")) {
2204 chr
= chr_baum_init();
2213 chr
->filename
= qemu_strdup(filename
);
2215 chr
->label
= qemu_strdup(label
);
2216 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2221 void qemu_chr_close(CharDriverState
*chr
)
2223 TAILQ_REMOVE(&chardevs
, chr
, next
);
2225 chr
->chr_close(chr
);
2226 qemu_free(chr
->filename
);
2227 qemu_free(chr
->label
);
2231 void qemu_chr_info(Monitor
*mon
)
2233 CharDriverState
*chr
;
2235 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2236 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);