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 (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 (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 */
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 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 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
);
935 static CharDriverState
*qemu_chr_open_pty(void)
937 CharDriverState
*chr
;
941 #if defined(__OpenBSD__) || defined(__DragonFly__)
942 char pty_name
[PATH_MAX
];
943 #define q_ptsname(x) pty_name
945 char *pty_name
= NULL
;
946 #define q_ptsname(x) ptsname(x)
949 chr
= qemu_mallocz(sizeof(CharDriverState
));
950 s
= qemu_mallocz(sizeof(PtyCharDriver
));
952 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
956 /* Set raw attributes on the pty. */
957 tcgetattr(slave_fd
, &tty
);
959 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
962 len
= strlen(q_ptsname(s
->fd
)) + 5;
963 chr
->filename
= qemu_malloc(len
);
964 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
965 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
968 chr
->chr_write
= pty_chr_write
;
969 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
970 chr
->chr_close
= pty_chr_close
;
972 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
977 static void tty_serial_init(int fd
, int speed
,
978 int parity
, int data_bits
, int stop_bits
)
984 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
985 speed
, parity
, data_bits
, stop_bits
);
987 tcgetattr (fd
, &tty
);
990 if (speed
<= 50 * MARGIN
)
992 else if (speed
<= 75 * MARGIN
)
994 else if (speed
<= 300 * MARGIN
)
996 else if (speed
<= 600 * MARGIN
)
998 else if (speed
<= 1200 * MARGIN
)
1000 else if (speed
<= 2400 * MARGIN
)
1002 else if (speed
<= 4800 * MARGIN
)
1004 else if (speed
<= 9600 * MARGIN
)
1006 else if (speed
<= 19200 * MARGIN
)
1008 else if (speed
<= 38400 * MARGIN
)
1010 else if (speed
<= 57600 * MARGIN
)
1012 else if (speed
<= 115200 * MARGIN
)
1017 cfsetispeed(&tty
, spd
);
1018 cfsetospeed(&tty
, spd
);
1020 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1021 |INLCR
|IGNCR
|ICRNL
|IXON
);
1022 tty
.c_oflag
|= OPOST
;
1023 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1024 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1045 tty
.c_cflag
|= PARENB
;
1048 tty
.c_cflag
|= PARENB
| PARODD
;
1052 tty
.c_cflag
|= CSTOPB
;
1054 tcsetattr (fd
, TCSANOW
, &tty
);
1057 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1059 FDCharDriver
*s
= chr
->opaque
;
1062 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1064 QEMUSerialSetParams
*ssp
= arg
;
1065 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1066 ssp
->data_bits
, ssp
->stop_bits
);
1069 case CHR_IOCTL_SERIAL_SET_BREAK
:
1071 int enable
= *(int *)arg
;
1073 tcsendbreak(s
->fd_in
, 1);
1076 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1079 int *targ
= (int *)arg
;
1080 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1082 if (sarg
& TIOCM_CTS
)
1083 *targ
|= CHR_TIOCM_CTS
;
1084 if (sarg
& TIOCM_CAR
)
1085 *targ
|= CHR_TIOCM_CAR
;
1086 if (sarg
& TIOCM_DSR
)
1087 *targ
|= CHR_TIOCM_DSR
;
1088 if (sarg
& TIOCM_RI
)
1089 *targ
|= CHR_TIOCM_RI
;
1090 if (sarg
& TIOCM_DTR
)
1091 *targ
|= CHR_TIOCM_DTR
;
1092 if (sarg
& TIOCM_RTS
)
1093 *targ
|= CHR_TIOCM_RTS
;
1096 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1098 int sarg
= *(int *)arg
;
1100 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1101 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1102 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1103 if (sarg
& CHR_TIOCM_CTS
)
1105 if (sarg
& CHR_TIOCM_CAR
)
1107 if (sarg
& CHR_TIOCM_DSR
)
1109 if (sarg
& CHR_TIOCM_RI
)
1111 if (sarg
& CHR_TIOCM_DTR
)
1113 if (sarg
& CHR_TIOCM_RTS
)
1115 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1124 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1126 CharDriverState
*chr
;
1129 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1130 tty_serial_init(fd
, 115200, 'N', 8, 1);
1131 chr
= qemu_chr_open_fd(fd
, fd
);
1136 chr
->chr_ioctl
= tty_serial_ioctl
;
1137 qemu_chr_reset(chr
);
1140 #else /* ! __linux__ && ! __sun__ */
1141 static CharDriverState
*qemu_chr_open_pty(void)
1145 #endif /* __linux__ || __sun__ */
1147 #if defined(__linux__)
1151 } ParallelCharDriver
;
1153 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1155 if (s
->mode
!= mode
) {
1157 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1164 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1166 ParallelCharDriver
*drv
= chr
->opaque
;
1171 case CHR_IOCTL_PP_READ_DATA
:
1172 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1174 *(uint8_t *)arg
= b
;
1176 case CHR_IOCTL_PP_WRITE_DATA
:
1177 b
= *(uint8_t *)arg
;
1178 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1181 case CHR_IOCTL_PP_READ_CONTROL
:
1182 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1184 /* Linux gives only the lowest bits, and no way to know data
1185 direction! For better compatibility set the fixed upper
1187 *(uint8_t *)arg
= b
| 0xc0;
1189 case CHR_IOCTL_PP_WRITE_CONTROL
:
1190 b
= *(uint8_t *)arg
;
1191 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1194 case CHR_IOCTL_PP_READ_STATUS
:
1195 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1197 *(uint8_t *)arg
= b
;
1199 case CHR_IOCTL_PP_DATA_DIR
:
1200 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1203 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1204 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1205 struct ParallelIOArg
*parg
= arg
;
1206 int n
= read(fd
, parg
->buffer
, parg
->count
);
1207 if (n
!= parg
->count
) {
1212 case CHR_IOCTL_PP_EPP_READ
:
1213 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1214 struct ParallelIOArg
*parg
= arg
;
1215 int n
= read(fd
, parg
->buffer
, parg
->count
);
1216 if (n
!= parg
->count
) {
1221 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1222 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1223 struct ParallelIOArg
*parg
= arg
;
1224 int n
= write(fd
, parg
->buffer
, parg
->count
);
1225 if (n
!= parg
->count
) {
1230 case CHR_IOCTL_PP_EPP_WRITE
:
1231 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1232 struct ParallelIOArg
*parg
= arg
;
1233 int n
= write(fd
, parg
->buffer
, parg
->count
);
1234 if (n
!= parg
->count
) {
1245 static void pp_close(CharDriverState
*chr
)
1247 ParallelCharDriver
*drv
= chr
->opaque
;
1250 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1251 ioctl(fd
, PPRELEASE
);
1256 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1258 CharDriverState
*chr
;
1259 ParallelCharDriver
*drv
;
1262 TFR(fd
= open(filename
, O_RDWR
));
1266 if (ioctl(fd
, PPCLAIM
) < 0) {
1271 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1273 drv
->mode
= IEEE1284_MODE_COMPAT
;
1275 chr
= qemu_mallocz(sizeof(CharDriverState
));
1276 chr
->chr_write
= null_chr_write
;
1277 chr
->chr_ioctl
= pp_ioctl
;
1278 chr
->chr_close
= pp_close
;
1281 qemu_chr_reset(chr
);
1285 #endif /* __linux__ */
1287 #if defined(__FreeBSD__) || defined(__DragonFly__)
1288 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1290 int fd
= (int)chr
->opaque
;
1294 case CHR_IOCTL_PP_READ_DATA
:
1295 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1297 *(uint8_t *)arg
= b
;
1299 case CHR_IOCTL_PP_WRITE_DATA
:
1300 b
= *(uint8_t *)arg
;
1301 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1304 case CHR_IOCTL_PP_READ_CONTROL
:
1305 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1307 *(uint8_t *)arg
= b
;
1309 case CHR_IOCTL_PP_WRITE_CONTROL
:
1310 b
= *(uint8_t *)arg
;
1311 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1314 case CHR_IOCTL_PP_READ_STATUS
:
1315 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1317 *(uint8_t *)arg
= b
;
1325 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1327 CharDriverState
*chr
;
1330 fd
= open(filename
, O_RDWR
);
1334 chr
= qemu_mallocz(sizeof(CharDriverState
));
1335 chr
->opaque
= (void *)fd
;
1336 chr
->chr_write
= null_chr_write
;
1337 chr
->chr_ioctl
= pp_ioctl
;
1346 HANDLE hcom
, hrecv
, hsend
;
1347 OVERLAPPED orecv
, osend
;
1352 #define NSENDBUF 2048
1353 #define NRECVBUF 2048
1354 #define MAXCONNECT 1
1355 #define NTIMEOUT 5000
1357 static int win_chr_poll(void *opaque
);
1358 static int win_chr_pipe_poll(void *opaque
);
1360 static void win_chr_close(CharDriverState
*chr
)
1362 WinCharState
*s
= chr
->opaque
;
1365 CloseHandle(s
->hsend
);
1369 CloseHandle(s
->hrecv
);
1373 CloseHandle(s
->hcom
);
1377 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1379 qemu_del_polling_cb(win_chr_poll
, chr
);
1382 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1384 WinCharState
*s
= chr
->opaque
;
1386 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1391 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1393 fprintf(stderr
, "Failed CreateEvent\n");
1396 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1398 fprintf(stderr
, "Failed CreateEvent\n");
1402 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1403 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1404 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1405 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1410 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1411 fprintf(stderr
, "Failed SetupComm\n");
1415 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1416 size
= sizeof(COMMCONFIG
);
1417 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1418 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1419 CommConfigDialog(filename
, NULL
, &comcfg
);
1421 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1422 fprintf(stderr
, "Failed SetCommState\n");
1426 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1427 fprintf(stderr
, "Failed SetCommMask\n");
1431 cto
.ReadIntervalTimeout
= MAXDWORD
;
1432 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1433 fprintf(stderr
, "Failed SetCommTimeouts\n");
1437 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1438 fprintf(stderr
, "Failed ClearCommError\n");
1441 qemu_add_polling_cb(win_chr_poll
, chr
);
1449 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1451 WinCharState
*s
= chr
->opaque
;
1452 DWORD len
, ret
, size
, err
;
1455 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1456 s
->osend
.hEvent
= s
->hsend
;
1459 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1461 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1463 err
= GetLastError();
1464 if (err
== ERROR_IO_PENDING
) {
1465 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1483 static int win_chr_read_poll(CharDriverState
*chr
)
1485 WinCharState
*s
= chr
->opaque
;
1487 s
->max_size
= qemu_chr_can_read(chr
);
1491 static void win_chr_readfile(CharDriverState
*chr
)
1493 WinCharState
*s
= chr
->opaque
;
1498 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1499 s
->orecv
.hEvent
= s
->hrecv
;
1500 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1502 err
= GetLastError();
1503 if (err
== ERROR_IO_PENDING
) {
1504 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1509 qemu_chr_read(chr
, buf
, size
);
1513 static void win_chr_read(CharDriverState
*chr
)
1515 WinCharState
*s
= chr
->opaque
;
1517 if (s
->len
> s
->max_size
)
1518 s
->len
= s
->max_size
;
1522 win_chr_readfile(chr
);
1525 static int win_chr_poll(void *opaque
)
1527 CharDriverState
*chr
= opaque
;
1528 WinCharState
*s
= chr
->opaque
;
1532 ClearCommError(s
->hcom
, &comerr
, &status
);
1533 if (status
.cbInQue
> 0) {
1534 s
->len
= status
.cbInQue
;
1535 win_chr_read_poll(chr
);
1542 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1544 CharDriverState
*chr
;
1547 chr
= qemu_mallocz(sizeof(CharDriverState
));
1548 s
= qemu_mallocz(sizeof(WinCharState
));
1550 chr
->chr_write
= win_chr_write
;
1551 chr
->chr_close
= win_chr_close
;
1553 if (win_chr_init(chr
, filename
) < 0) {
1558 qemu_chr_reset(chr
);
1562 static int win_chr_pipe_poll(void *opaque
)
1564 CharDriverState
*chr
= opaque
;
1565 WinCharState
*s
= chr
->opaque
;
1568 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1571 win_chr_read_poll(chr
);
1578 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1580 WinCharState
*s
= chr
->opaque
;
1588 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1590 fprintf(stderr
, "Failed CreateEvent\n");
1593 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1595 fprintf(stderr
, "Failed CreateEvent\n");
1599 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1600 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1601 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1603 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1604 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1605 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1610 ZeroMemory(&ov
, sizeof(ov
));
1611 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1612 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1614 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1618 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1620 fprintf(stderr
, "Failed GetOverlappedResult\n");
1622 CloseHandle(ov
.hEvent
);
1629 CloseHandle(ov
.hEvent
);
1632 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1641 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1643 CharDriverState
*chr
;
1646 chr
= qemu_mallocz(sizeof(CharDriverState
));
1647 s
= qemu_mallocz(sizeof(WinCharState
));
1649 chr
->chr_write
= win_chr_write
;
1650 chr
->chr_close
= win_chr_close
;
1652 if (win_chr_pipe_init(chr
, filename
) < 0) {
1657 qemu_chr_reset(chr
);
1661 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1663 CharDriverState
*chr
;
1666 chr
= qemu_mallocz(sizeof(CharDriverState
));
1667 s
= qemu_mallocz(sizeof(WinCharState
));
1670 chr
->chr_write
= win_chr_write
;
1671 qemu_chr_reset(chr
);
1675 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1677 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1680 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1684 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1685 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1686 if (fd_out
== INVALID_HANDLE_VALUE
)
1689 return qemu_chr_open_win_file(fd_out
);
1691 #endif /* !_WIN32 */
1693 /***********************************************************/
1694 /* UDP Net console */
1698 struct sockaddr_in daddr
;
1705 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1707 NetCharDriver
*s
= chr
->opaque
;
1709 return sendto(s
->fd
, buf
, len
, 0,
1710 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1713 static int udp_chr_read_poll(void *opaque
)
1715 CharDriverState
*chr
= opaque
;
1716 NetCharDriver
*s
= chr
->opaque
;
1718 s
->max_size
= qemu_chr_can_read(chr
);
1720 /* If there were any stray characters in the queue process them
1723 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1724 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1726 s
->max_size
= qemu_chr_can_read(chr
);
1731 static void udp_chr_read(void *opaque
)
1733 CharDriverState
*chr
= opaque
;
1734 NetCharDriver
*s
= chr
->opaque
;
1736 if (s
->max_size
== 0)
1738 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1739 s
->bufptr
= s
->bufcnt
;
1744 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1745 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1747 s
->max_size
= qemu_chr_can_read(chr
);
1751 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1753 NetCharDriver
*s
= chr
->opaque
;
1756 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1757 udp_chr_read
, NULL
, chr
);
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
;
1806 /***********************************************************/
1807 /* TCP Net console */
1818 static void tcp_chr_accept(void *opaque
);
1820 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1822 TCPCharDriver
*s
= chr
->opaque
;
1824 return send_all(s
->fd
, buf
, len
);
1826 /* XXX: indicate an error ? */
1831 static int tcp_chr_read_poll(void *opaque
)
1833 CharDriverState
*chr
= opaque
;
1834 TCPCharDriver
*s
= chr
->opaque
;
1837 s
->max_size
= qemu_chr_can_read(chr
);
1842 #define IAC_BREAK 243
1843 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1845 uint8_t *buf
, int *size
)
1847 /* Handle any telnet client's basic IAC options to satisfy char by
1848 * char mode with no echo. All IAC options will be removed from
1849 * the buf and the do_telnetopt variable will be used to track the
1850 * state of the width of the IAC information.
1852 * IAC commands come in sets of 3 bytes with the exception of the
1853 * "IAC BREAK" command and the double IAC.
1859 for (i
= 0; i
< *size
; i
++) {
1860 if (s
->do_telnetopt
> 1) {
1861 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1862 /* Double IAC means send an IAC */
1866 s
->do_telnetopt
= 1;
1868 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1869 /* Handle IAC break commands by sending a serial break */
1870 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1875 if (s
->do_telnetopt
>= 4) {
1876 s
->do_telnetopt
= 1;
1879 if ((unsigned char)buf
[i
] == IAC
) {
1880 s
->do_telnetopt
= 2;
1891 static void tcp_chr_read(void *opaque
)
1893 CharDriverState
*chr
= opaque
;
1894 TCPCharDriver
*s
= chr
->opaque
;
1898 if (!s
->connected
|| s
->max_size
<= 0)
1901 if (len
> s
->max_size
)
1903 size
= recv(s
->fd
, buf
, len
, 0);
1905 /* connection closed */
1907 if (s
->listen_fd
>= 0) {
1908 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1910 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1913 } else if (size
> 0) {
1914 if (s
->do_telnetopt
)
1915 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1917 qemu_chr_read(chr
, buf
, size
);
1921 static void tcp_chr_connect(void *opaque
)
1923 CharDriverState
*chr
= opaque
;
1924 TCPCharDriver
*s
= chr
->opaque
;
1927 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1928 tcp_chr_read
, NULL
, chr
);
1929 qemu_chr_reset(chr
);
1932 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1933 static void tcp_chr_telnet_init(int fd
)
1936 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1937 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1938 send(fd
, (char *)buf
, 3, 0);
1939 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1940 send(fd
, (char *)buf
, 3, 0);
1941 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1942 send(fd
, (char *)buf
, 3, 0);
1943 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1944 send(fd
, (char *)buf
, 3, 0);
1947 static void socket_set_nodelay(int fd
)
1950 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1953 static void tcp_chr_accept(void *opaque
)
1955 CharDriverState
*chr
= opaque
;
1956 TCPCharDriver
*s
= chr
->opaque
;
1957 struct sockaddr_in saddr
;
1959 struct sockaddr_un uaddr
;
1961 struct sockaddr
*addr
;
1968 len
= sizeof(uaddr
);
1969 addr
= (struct sockaddr
*)&uaddr
;
1973 len
= sizeof(saddr
);
1974 addr
= (struct sockaddr
*)&saddr
;
1976 fd
= accept(s
->listen_fd
, addr
, &len
);
1977 if (fd
< 0 && errno
!= EINTR
) {
1979 } else if (fd
>= 0) {
1980 if (s
->do_telnetopt
)
1981 tcp_chr_telnet_init(fd
);
1985 socket_set_nonblock(fd
);
1987 socket_set_nodelay(fd
);
1989 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
1990 tcp_chr_connect(chr
);
1993 static void tcp_chr_close(CharDriverState
*chr
)
1995 TCPCharDriver
*s
= chr
->opaque
;
1998 if (s
->listen_fd
>= 0)
1999 closesocket(s
->listen_fd
);
2003 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2007 CharDriverState
*chr
= NULL
;
2008 TCPCharDriver
*s
= NULL
;
2009 int fd
= -1, offset
= 0;
2011 int is_waitconnect
= 1;
2016 while((ptr
= strchr(ptr
,','))) {
2018 if (!strncmp(ptr
,"server",6)) {
2020 } else if (!strncmp(ptr
,"nowait",6)) {
2022 } else if (!strncmp(ptr
,"nodelay",6)) {
2024 } else if (!strncmp(ptr
,"to=",3)) {
2025 /* nothing, inet_listen() parses this one */;
2026 } else if (!strncmp(ptr
,"ipv4",4)) {
2027 /* nothing, inet_connect() and inet_listen() parse this one */;
2028 } else if (!strncmp(ptr
,"ipv6",4)) {
2029 /* nothing, inet_connect() and inet_listen() parse this one */;
2031 printf("Unknown option: %s\n", ptr
);
2038 chr
= qemu_mallocz(sizeof(CharDriverState
));
2039 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2042 chr
->filename
= qemu_malloc(256);
2044 pstrcpy(chr
->filename
, 256, "unix:");
2045 } else if (is_telnet
) {
2046 pstrcpy(chr
->filename
, 256, "telnet:");
2048 pstrcpy(chr
->filename
, 256, "tcp:");
2050 offset
= strlen(chr
->filename
);
2054 fd
= unix_listen(host_str
, chr
->filename
+ offset
, 256 - offset
);
2056 fd
= unix_connect(host_str
);
2060 fd
= inet_listen(host_str
, chr
->filename
+ offset
, 256 - offset
,
2063 fd
= inet_connect(host_str
, SOCK_STREAM
);
2069 if (!is_waitconnect
)
2070 socket_set_nonblock(fd
);
2075 s
->is_unix
= is_unix
;
2076 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2079 chr
->chr_write
= tcp_chr_write
;
2080 chr
->chr_close
= tcp_chr_close
;
2084 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2086 s
->do_telnetopt
= 1;
2090 socket_set_nodelay(fd
);
2091 tcp_chr_connect(chr
);
2094 if (is_listen
&& is_waitconnect
) {
2095 printf("QEMU waiting for connection on: %s\n",
2096 chr
->filename
? chr
->filename
: host_str
);
2097 tcp_chr_accept(chr
);
2098 socket_set_nonblock(s
->listen_fd
);
2110 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2113 CharDriverState
*chr
;
2115 if (!strcmp(filename
, "vc")) {
2116 chr
= text_console_init(0);
2118 if (strstart(filename
, "vc:", &p
)) {
2119 chr
= text_console_init(p
);
2121 if (!strcmp(filename
, "null")) {
2122 chr
= qemu_chr_open_null();
2124 if (strstart(filename
, "tcp:", &p
)) {
2125 chr
= qemu_chr_open_tcp(p
, 0, 0);
2127 if (strstart(filename
, "telnet:", &p
)) {
2128 chr
= qemu_chr_open_tcp(p
, 1, 0);
2130 if (strstart(filename
, "udp:", &p
)) {
2131 chr
= qemu_chr_open_udp(p
);
2133 if (strstart(filename
, "mon:", &p
)) {
2134 chr
= qemu_chr_open(label
, p
, NULL
);
2136 chr
= qemu_chr_open_mux(chr
);
2137 monitor_init(chr
, MONITOR_USE_READLINE
);
2139 printf("Unable to open driver: %s\n", p
);
2141 } else if (!strcmp(filename
, "msmouse")) {
2142 chr
= qemu_chr_open_msmouse();
2145 if (strstart(filename
, "unix:", &p
)) {
2146 chr
= qemu_chr_open_tcp(p
, 0, 1);
2147 } else if (strstart(filename
, "file:", &p
)) {
2148 chr
= qemu_chr_open_file_out(p
);
2149 } else if (strstart(filename
, "pipe:", &p
)) {
2150 chr
= qemu_chr_open_pipe(p
);
2151 } else if (!strcmp(filename
, "pty")) {
2152 chr
= qemu_chr_open_pty();
2153 } else if (!strcmp(filename
, "stdio")) {
2154 chr
= qemu_chr_open_stdio();
2156 #if defined(__linux__)
2157 if (strstart(filename
, "/dev/parport", NULL
)) {
2158 chr
= qemu_chr_open_pp(filename
);
2160 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2161 if (strstart(filename
, "/dev/ppi", NULL
)) {
2162 chr
= qemu_chr_open_pp(filename
);
2165 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2166 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2167 if (strstart(filename
, "/dev/", NULL
)) {
2168 chr
= qemu_chr_open_tty(filename
);
2172 if (strstart(filename
, "COM", NULL
)) {
2173 chr
= qemu_chr_open_win(filename
);
2175 if (strstart(filename
, "pipe:", &p
)) {
2176 chr
= qemu_chr_open_win_pipe(p
);
2178 if (strstart(filename
, "con:", NULL
)) {
2179 chr
= qemu_chr_open_win_con(filename
);
2181 if (strstart(filename
, "file:", &p
)) {
2182 chr
= qemu_chr_open_win_file_out(p
);
2185 #ifdef CONFIG_BRLAPI
2186 if (!strcmp(filename
, "braille")) {
2187 chr
= chr_baum_init();
2196 chr
->filename
= qemu_strdup(filename
);
2198 chr
->label
= qemu_strdup(label
);
2199 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2204 void qemu_chr_close(CharDriverState
*chr
)
2206 TAILQ_REMOVE(&chardevs
, chr
, next
);
2208 chr
->chr_close(chr
);
2209 qemu_free(chr
->filename
);
2210 qemu_free(chr
->label
);
2214 void qemu_chr_info(Monitor
*mon
)
2216 CharDriverState
*chr
;
2218 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2219 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);