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>
73 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
74 #include <freebsd/stdlib.h>
79 #include <linux/ppdev.h>
80 #include <linux/parport.h>
84 #include <sys/ethernet.h>
85 #include <sys/sockio.h>
86 #include <netinet/arp.h>
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_icmp.h> // must come after ip.h
91 #include <netinet/udp.h>
92 #include <netinet/tcp.h>
100 #include "qemu_socket.h"
102 /***********************************************************/
103 /* character device */
105 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
106 TAILQ_HEAD_INITIALIZER(chardevs
);
107 static int initial_reset_issued
;
109 static void qemu_chr_event(CharDriverState
*s
, int event
)
113 s
->chr_event(s
->handler_opaque
, event
);
116 static void qemu_chr_reset_bh(void *opaque
)
118 CharDriverState
*s
= opaque
;
119 qemu_chr_event(s
, CHR_EVENT_RESET
);
120 qemu_bh_delete(s
->bh
);
124 void qemu_chr_reset(CharDriverState
*s
)
126 if (s
->bh
== NULL
&& initial_reset_issued
) {
127 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
128 qemu_bh_schedule(s
->bh
);
132 void qemu_chr_initial_reset(void)
134 CharDriverState
*chr
;
136 initial_reset_issued
= 1;
138 TAILQ_FOREACH(chr
, &chardevs
, next
) {
143 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
145 return s
->chr_write(s
, buf
, len
);
148 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
152 return s
->chr_ioctl(s
, cmd
, arg
);
155 int qemu_chr_can_read(CharDriverState
*s
)
157 if (!s
->chr_can_read
)
159 return s
->chr_can_read(s
->handler_opaque
);
162 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
164 s
->chr_read(s
->handler_opaque
, buf
, len
);
167 void qemu_chr_accept_input(CharDriverState
*s
)
169 if (s
->chr_accept_input
)
170 s
->chr_accept_input(s
);
173 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
178 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
179 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
183 void qemu_chr_send_event(CharDriverState
*s
, int event
)
185 if (s
->chr_send_event
)
186 s
->chr_send_event(s
, event
);
189 void qemu_chr_add_handlers(CharDriverState
*s
,
190 IOCanRWHandler
*fd_can_read
,
191 IOReadHandler
*fd_read
,
192 IOEventHandler
*fd_event
,
195 s
->chr_can_read
= fd_can_read
;
196 s
->chr_read
= fd_read
;
197 s
->chr_event
= fd_event
;
198 s
->handler_opaque
= opaque
;
199 if (s
->chr_update_read_handler
)
200 s
->chr_update_read_handler(s
);
203 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
208 static CharDriverState
*qemu_chr_open_null(void)
210 CharDriverState
*chr
;
212 chr
= qemu_mallocz(sizeof(CharDriverState
));
213 chr
->chr_write
= null_chr_write
;
217 /* MUX driver for serial I/O splitting */
218 static int term_timestamps
;
219 static int64_t term_timestamps_start
;
221 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
222 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
224 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
225 IOReadHandler
*chr_read
[MAX_MUX
];
226 IOEventHandler
*chr_event
[MAX_MUX
];
227 void *ext_opaque
[MAX_MUX
];
228 CharDriverState
*drv
;
232 /* Intermediate input buffer allows to catch escape sequences even if the
233 currently active device is not accepting any input - but only until it
235 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
241 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
243 MuxDriver
*d
= chr
->opaque
;
245 if (!term_timestamps
) {
246 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
251 for(i
= 0; i
< len
; i
++) {
252 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
253 if (buf
[i
] == '\n') {
258 ti
= qemu_get_clock(rt_clock
);
259 if (term_timestamps_start
== -1)
260 term_timestamps_start
= ti
;
261 ti
-= term_timestamps_start
;
263 snprintf(buf1
, sizeof(buf1
),
264 "[%02d:%02d:%02d.%03d] ",
269 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
276 static const char * const mux_help
[] = {
277 "% h print this help\n\r",
278 "% x exit emulator\n\r",
279 "% s save disk data back to file (if -snapshot)\n\r",
280 "% t toggle console timestamps\n\r"
281 "% b send break (magic sysrq)\n\r",
282 "% c switch between console and monitor\n\r",
287 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
288 static void mux_print_help(CharDriverState
*chr
)
291 char ebuf
[15] = "Escape-Char";
292 char cbuf
[50] = "\n\r";
294 if (term_escape_char
> 0 && term_escape_char
< 26) {
295 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
296 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
298 snprintf(cbuf
, sizeof(cbuf
),
299 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
302 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
303 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
304 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
305 if (mux_help
[i
][j
] == '%')
306 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
308 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
313 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
315 if (d
->term_got_escape
) {
316 d
->term_got_escape
= 0;
317 if (ch
== term_escape_char
)
326 const char *term
= "QEMU: Terminated\n\r";
327 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
334 for (i
= 0; i
< nb_drives
; i
++) {
335 bdrv_commit(drives_table
[i
].bdrv
);
340 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
343 /* Switch to the next registered device */
345 if (chr
->focus
>= d
->mux_cnt
)
349 term_timestamps
= !term_timestamps
;
350 term_timestamps_start
= -1;
353 } else if (ch
== term_escape_char
) {
354 d
->term_got_escape
= 1;
362 static void mux_chr_accept_input(CharDriverState
*chr
)
365 MuxDriver
*d
= chr
->opaque
;
367 while (d
->prod
[m
] != d
->cons
[m
] &&
368 d
->chr_can_read
[m
] &&
369 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
370 d
->chr_read
[m
](d
->ext_opaque
[m
],
371 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
375 static int mux_chr_can_read(void *opaque
)
377 CharDriverState
*chr
= opaque
;
378 MuxDriver
*d
= chr
->opaque
;
381 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
383 if (d
->chr_can_read
[m
])
384 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
388 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
390 CharDriverState
*chr
= opaque
;
391 MuxDriver
*d
= chr
->opaque
;
395 mux_chr_accept_input (opaque
);
397 for(i
= 0; i
< size
; i
++)
398 if (mux_proc_byte(chr
, d
, buf
[i
])) {
399 if (d
->prod
[m
] == d
->cons
[m
] &&
400 d
->chr_can_read
[m
] &&
401 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
402 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
404 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
408 static void mux_chr_event(void *opaque
, int event
)
410 CharDriverState
*chr
= opaque
;
411 MuxDriver
*d
= chr
->opaque
;
414 /* Send the event to all registered listeners */
415 for (i
= 0; i
< d
->mux_cnt
; i
++)
417 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
420 static void mux_chr_update_read_handler(CharDriverState
*chr
)
422 MuxDriver
*d
= chr
->opaque
;
424 if (d
->mux_cnt
>= MAX_MUX
) {
425 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
428 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
429 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
430 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
431 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
432 /* Fix up the real driver with mux routines */
433 if (d
->mux_cnt
== 0) {
434 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
437 chr
->focus
= d
->mux_cnt
;
441 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
443 CharDriverState
*chr
;
446 chr
= qemu_mallocz(sizeof(CharDriverState
));
447 d
= qemu_mallocz(sizeof(MuxDriver
));
452 chr
->chr_write
= mux_chr_write
;
453 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
454 chr
->chr_accept_input
= mux_chr_accept_input
;
460 int send_all(int fd
, const void *buf
, int len1
)
466 ret
= send(fd
, buf
, len
, 0);
468 errno
= WSAGetLastError();
469 if (errno
!= WSAEWOULDBLOCK
) {
472 } else if (ret
== 0) {
484 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
490 ret
= write(fd
, buf
, len
);
492 if (errno
!= EINTR
&& errno
!= EAGAIN
)
494 } else if (ret
== 0) {
504 int send_all(int fd
, const void *buf
, int len1
)
506 return unix_write(fd
, buf
, len1
);
517 #define STDIO_MAX_CLIENTS 1
518 static int stdio_nb_clients
= 0;
520 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
522 FDCharDriver
*s
= chr
->opaque
;
523 return send_all(s
->fd_out
, buf
, len
);
526 static int fd_chr_read_poll(void *opaque
)
528 CharDriverState
*chr
= opaque
;
529 FDCharDriver
*s
= chr
->opaque
;
531 s
->max_size
= qemu_chr_can_read(chr
);
535 static void fd_chr_read(void *opaque
)
537 CharDriverState
*chr
= opaque
;
538 FDCharDriver
*s
= chr
->opaque
;
543 if (len
> s
->max_size
)
547 size
= read(s
->fd_in
, buf
, len
);
549 /* FD has been closed. Remove it from the active list. */
550 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
554 qemu_chr_read(chr
, buf
, size
);
558 static void fd_chr_update_read_handler(CharDriverState
*chr
)
560 FDCharDriver
*s
= chr
->opaque
;
563 if (nographic
&& s
->fd_in
== 0) {
565 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
566 fd_chr_read
, NULL
, chr
);
571 static void fd_chr_close(struct CharDriverState
*chr
)
573 FDCharDriver
*s
= chr
->opaque
;
576 if (nographic
&& s
->fd_in
== 0) {
578 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
585 /* open a character device to a unix fd */
586 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
588 CharDriverState
*chr
;
591 chr
= qemu_mallocz(sizeof(CharDriverState
));
592 s
= qemu_mallocz(sizeof(FDCharDriver
));
596 chr
->chr_write
= fd_chr_write
;
597 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
598 chr
->chr_close
= fd_chr_close
;
605 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
609 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
612 return qemu_chr_open_fd(-1, fd_out
);
615 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
618 char filename_in
[256], filename_out
[256];
620 snprintf(filename_in
, 256, "%s.in", filename
);
621 snprintf(filename_out
, 256, "%s.out", filename
);
622 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
623 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
624 if (fd_in
< 0 || fd_out
< 0) {
629 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
633 return qemu_chr_open_fd(fd_in
, fd_out
);
637 /* for STDIO, we handle the case where several clients use it
640 #define TERM_FIFO_MAX_SIZE 1
642 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
643 static int term_fifo_size
;
645 static int stdio_read_poll(void *opaque
)
647 CharDriverState
*chr
= opaque
;
649 /* try to flush the queue if needed */
650 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
651 qemu_chr_read(chr
, term_fifo
, 1);
654 /* see if we can absorb more chars */
655 if (term_fifo_size
== 0)
661 static void stdio_read(void *opaque
)
665 CharDriverState
*chr
= opaque
;
667 size
= read(0, buf
, 1);
669 /* stdin has been closed. Remove it from the active list. */
670 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
674 if (qemu_chr_can_read(chr
) > 0) {
675 qemu_chr_read(chr
, buf
, 1);
676 } else if (term_fifo_size
== 0) {
677 term_fifo
[term_fifo_size
++] = buf
[0];
682 /* init terminal so that we can grab keys */
683 static struct termios oldtty
;
684 static int old_fd0_flags
;
685 static int term_atexit_done
;
687 static void term_exit(void)
689 tcsetattr (0, TCSANOW
, &oldtty
);
690 fcntl(0, F_SETFL
, old_fd0_flags
);
693 static void term_init(void)
699 old_fd0_flags
= fcntl(0, F_GETFL
);
701 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
702 |INLCR
|IGNCR
|ICRNL
|IXON
);
703 tty
.c_oflag
|= OPOST
;
704 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
705 /* if graphical mode, we allow Ctrl-C handling */
707 tty
.c_lflag
&= ~ISIG
;
708 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
713 tcsetattr (0, TCSANOW
, &tty
);
715 if (!term_atexit_done
++)
718 fcntl(0, F_SETFL
, O_NONBLOCK
);
721 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
725 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
729 static CharDriverState
*qemu_chr_open_stdio(void)
731 CharDriverState
*chr
;
733 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
735 chr
= qemu_chr_open_fd(0, 1);
736 chr
->chr_close
= qemu_chr_close_stdio
;
737 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
745 /* Once Solaris has openpty(), this is going to be removed. */
746 int openpty(int *amaster
, int *aslave
, char *name
,
747 struct termios
*termp
, struct winsize
*winp
)
750 int mfd
= -1, sfd
= -1;
752 *amaster
= *aslave
= -1;
754 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
758 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
761 if ((slave
= ptsname(mfd
)) == NULL
)
764 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
767 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
768 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
776 ioctl(sfd
, TIOCSWINSZ
, winp
);
787 void cfmakeraw (struct termios
*termios_p
)
789 termios_p
->c_iflag
&=
790 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
791 termios_p
->c_oflag
&= ~OPOST
;
792 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
793 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
794 termios_p
->c_cflag
|= CS8
;
796 termios_p
->c_cc
[VMIN
] = 0;
797 termios_p
->c_cc
[VTIME
] = 0;
801 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
802 || defined(__NetBSD__) || defined(__OpenBSD__)
812 static void pty_chr_update_read_handler(CharDriverState
*chr
);
813 static void pty_chr_state(CharDriverState
*chr
, int connected
);
815 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
817 PtyCharDriver
*s
= chr
->opaque
;
820 /* guest sends data, check for (re-)connect */
821 pty_chr_update_read_handler(chr
);
824 return send_all(s
->fd
, buf
, len
);
827 static int pty_chr_read_poll(void *opaque
)
829 CharDriverState
*chr
= opaque
;
830 PtyCharDriver
*s
= chr
->opaque
;
832 s
->read_bytes
= qemu_chr_can_read(chr
);
833 return s
->read_bytes
;
836 static void pty_chr_read(void *opaque
)
838 CharDriverState
*chr
= opaque
;
839 PtyCharDriver
*s
= chr
->opaque
;
844 if (len
> s
->read_bytes
)
848 size
= read(s
->fd
, buf
, len
);
849 if ((size
== -1 && errno
== EIO
) ||
851 pty_chr_state(chr
, 0);
855 pty_chr_state(chr
, 1);
856 qemu_chr_read(chr
, buf
, size
);
860 static void pty_chr_update_read_handler(CharDriverState
*chr
)
862 PtyCharDriver
*s
= chr
->opaque
;
864 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
865 pty_chr_read
, NULL
, chr
);
868 * Short timeout here: just need wait long enougth that qemu makes
869 * it through the poll loop once. When reconnected we want a
870 * short timeout so we notice it almost instantly. Otherwise
871 * read() gives us -EIO instantly, making pty_chr_state() reset the
872 * timeout to the normal (much longer) poll interval before the
875 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
878 static void pty_chr_state(CharDriverState
*chr
, int connected
)
880 PtyCharDriver
*s
= chr
->opaque
;
883 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
886 /* (re-)connect poll interval for idle guests: once per second.
887 * We check more frequently in case the guests sends data to
888 * the virtual device linked to our pty. */
889 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
897 static void pty_chr_timer(void *opaque
)
899 struct CharDriverState
*chr
= opaque
;
900 PtyCharDriver
*s
= chr
->opaque
;
905 /* If we arrive here without polling being cleared due
906 * read returning -EIO, then we are (re-)connected */
907 pty_chr_state(chr
, 1);
912 pty_chr_update_read_handler(chr
);
915 static void pty_chr_close(struct CharDriverState
*chr
)
917 PtyCharDriver
*s
= chr
->opaque
;
919 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
924 static CharDriverState
*qemu_chr_open_pty(void)
926 CharDriverState
*chr
;
930 #if defined(__OpenBSD__)
931 char pty_name
[PATH_MAX
];
932 #define q_ptsname(x) pty_name
934 char *pty_name
= NULL
;
935 #define q_ptsname(x) ptsname(x)
938 chr
= qemu_mallocz(sizeof(CharDriverState
));
939 s
= qemu_mallocz(sizeof(PtyCharDriver
));
941 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
945 /* Set raw attributes on the pty. */
946 tcgetattr(slave_fd
, &tty
);
948 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
951 len
= strlen(q_ptsname(s
->fd
)) + 5;
952 chr
->filename
= qemu_malloc(len
);
953 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
954 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
957 chr
->chr_write
= pty_chr_write
;
958 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
959 chr
->chr_close
= pty_chr_close
;
961 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
966 static void tty_serial_init(int fd
, int speed
,
967 int parity
, int data_bits
, int stop_bits
)
973 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
974 speed
, parity
, data_bits
, stop_bits
);
976 tcgetattr (fd
, &tty
);
979 if (speed
<= 50 * MARGIN
)
981 else if (speed
<= 75 * MARGIN
)
983 else if (speed
<= 300 * MARGIN
)
985 else if (speed
<= 600 * MARGIN
)
987 else if (speed
<= 1200 * MARGIN
)
989 else if (speed
<= 2400 * MARGIN
)
991 else if (speed
<= 4800 * MARGIN
)
993 else if (speed
<= 9600 * MARGIN
)
995 else if (speed
<= 19200 * MARGIN
)
997 else if (speed
<= 38400 * MARGIN
)
999 else if (speed
<= 57600 * MARGIN
)
1001 else if (speed
<= 115200 * MARGIN
)
1006 cfsetispeed(&tty
, spd
);
1007 cfsetospeed(&tty
, spd
);
1009 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1010 |INLCR
|IGNCR
|ICRNL
|IXON
);
1011 tty
.c_oflag
|= OPOST
;
1012 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1013 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1034 tty
.c_cflag
|= PARENB
;
1037 tty
.c_cflag
|= PARENB
| PARODD
;
1041 tty
.c_cflag
|= CSTOPB
;
1043 tcsetattr (fd
, TCSANOW
, &tty
);
1046 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1048 FDCharDriver
*s
= chr
->opaque
;
1051 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1053 QEMUSerialSetParams
*ssp
= arg
;
1054 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1055 ssp
->data_bits
, ssp
->stop_bits
);
1058 case CHR_IOCTL_SERIAL_SET_BREAK
:
1060 int enable
= *(int *)arg
;
1062 tcsendbreak(s
->fd_in
, 1);
1065 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1068 int *targ
= (int *)arg
;
1069 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1071 if (sarg
& TIOCM_CTS
)
1072 *targ
|= CHR_TIOCM_CTS
;
1073 if (sarg
& TIOCM_CAR
)
1074 *targ
|= CHR_TIOCM_CAR
;
1075 if (sarg
& TIOCM_DSR
)
1076 *targ
|= CHR_TIOCM_DSR
;
1077 if (sarg
& TIOCM_RI
)
1078 *targ
|= CHR_TIOCM_RI
;
1079 if (sarg
& TIOCM_DTR
)
1080 *targ
|= CHR_TIOCM_DTR
;
1081 if (sarg
& TIOCM_RTS
)
1082 *targ
|= CHR_TIOCM_RTS
;
1085 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1087 int sarg
= *(int *)arg
;
1089 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1090 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1091 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1092 if (sarg
& CHR_TIOCM_CTS
)
1094 if (sarg
& CHR_TIOCM_CAR
)
1096 if (sarg
& CHR_TIOCM_DSR
)
1098 if (sarg
& CHR_TIOCM_RI
)
1100 if (sarg
& CHR_TIOCM_DTR
)
1102 if (sarg
& CHR_TIOCM_RTS
)
1104 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1113 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1115 CharDriverState
*chr
;
1118 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1119 tty_serial_init(fd
, 115200, 'N', 8, 1);
1120 chr
= qemu_chr_open_fd(fd
, fd
);
1125 chr
->chr_ioctl
= tty_serial_ioctl
;
1126 qemu_chr_reset(chr
);
1129 #else /* ! __linux__ && ! __sun__ */
1130 static CharDriverState
*qemu_chr_open_pty(void)
1134 #endif /* __linux__ || __sun__ */
1136 #if defined(__linux__)
1140 } ParallelCharDriver
;
1142 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1144 if (s
->mode
!= mode
) {
1146 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1153 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1155 ParallelCharDriver
*drv
= chr
->opaque
;
1160 case CHR_IOCTL_PP_READ_DATA
:
1161 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1163 *(uint8_t *)arg
= b
;
1165 case CHR_IOCTL_PP_WRITE_DATA
:
1166 b
= *(uint8_t *)arg
;
1167 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1170 case CHR_IOCTL_PP_READ_CONTROL
:
1171 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1173 /* Linux gives only the lowest bits, and no way to know data
1174 direction! For better compatibility set the fixed upper
1176 *(uint8_t *)arg
= b
| 0xc0;
1178 case CHR_IOCTL_PP_WRITE_CONTROL
:
1179 b
= *(uint8_t *)arg
;
1180 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1183 case CHR_IOCTL_PP_READ_STATUS
:
1184 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1186 *(uint8_t *)arg
= b
;
1188 case CHR_IOCTL_PP_DATA_DIR
:
1189 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1192 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1193 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1194 struct ParallelIOArg
*parg
= arg
;
1195 int n
= read(fd
, parg
->buffer
, parg
->count
);
1196 if (n
!= parg
->count
) {
1201 case CHR_IOCTL_PP_EPP_READ
:
1202 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1203 struct ParallelIOArg
*parg
= arg
;
1204 int n
= read(fd
, parg
->buffer
, parg
->count
);
1205 if (n
!= parg
->count
) {
1210 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1211 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1212 struct ParallelIOArg
*parg
= arg
;
1213 int n
= write(fd
, parg
->buffer
, parg
->count
);
1214 if (n
!= parg
->count
) {
1219 case CHR_IOCTL_PP_EPP_WRITE
:
1220 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1221 struct ParallelIOArg
*parg
= arg
;
1222 int n
= write(fd
, parg
->buffer
, parg
->count
);
1223 if (n
!= parg
->count
) {
1234 static void pp_close(CharDriverState
*chr
)
1236 ParallelCharDriver
*drv
= chr
->opaque
;
1239 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1240 ioctl(fd
, PPRELEASE
);
1245 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1247 CharDriverState
*chr
;
1248 ParallelCharDriver
*drv
;
1251 TFR(fd
= open(filename
, O_RDWR
));
1255 if (ioctl(fd
, PPCLAIM
) < 0) {
1260 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1262 drv
->mode
= IEEE1284_MODE_COMPAT
;
1264 chr
= qemu_mallocz(sizeof(CharDriverState
));
1265 chr
->chr_write
= null_chr_write
;
1266 chr
->chr_ioctl
= pp_ioctl
;
1267 chr
->chr_close
= pp_close
;
1270 qemu_chr_reset(chr
);
1274 #endif /* __linux__ */
1276 #if defined(__FreeBSD__)
1277 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1279 int fd
= (int)chr
->opaque
;
1283 case CHR_IOCTL_PP_READ_DATA
:
1284 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1286 *(uint8_t *)arg
= b
;
1288 case CHR_IOCTL_PP_WRITE_DATA
:
1289 b
= *(uint8_t *)arg
;
1290 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1293 case CHR_IOCTL_PP_READ_CONTROL
:
1294 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1296 *(uint8_t *)arg
= b
;
1298 case CHR_IOCTL_PP_WRITE_CONTROL
:
1299 b
= *(uint8_t *)arg
;
1300 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1303 case CHR_IOCTL_PP_READ_STATUS
:
1304 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1306 *(uint8_t *)arg
= b
;
1314 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1316 CharDriverState
*chr
;
1319 fd
= open(filename
, O_RDWR
);
1323 chr
= qemu_mallocz(sizeof(CharDriverState
));
1324 chr
->opaque
= (void *)fd
;
1325 chr
->chr_write
= null_chr_write
;
1326 chr
->chr_ioctl
= pp_ioctl
;
1335 HANDLE hcom
, hrecv
, hsend
;
1336 OVERLAPPED orecv
, osend
;
1341 #define NSENDBUF 2048
1342 #define NRECVBUF 2048
1343 #define MAXCONNECT 1
1344 #define NTIMEOUT 5000
1346 static int win_chr_poll(void *opaque
);
1347 static int win_chr_pipe_poll(void *opaque
);
1349 static void win_chr_close(CharDriverState
*chr
)
1351 WinCharState
*s
= chr
->opaque
;
1354 CloseHandle(s
->hsend
);
1358 CloseHandle(s
->hrecv
);
1362 CloseHandle(s
->hcom
);
1366 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1368 qemu_del_polling_cb(win_chr_poll
, chr
);
1371 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1373 WinCharState
*s
= chr
->opaque
;
1375 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1380 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1382 fprintf(stderr
, "Failed CreateEvent\n");
1385 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1387 fprintf(stderr
, "Failed CreateEvent\n");
1391 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1392 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1393 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1394 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1399 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1400 fprintf(stderr
, "Failed SetupComm\n");
1404 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1405 size
= sizeof(COMMCONFIG
);
1406 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1407 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1408 CommConfigDialog(filename
, NULL
, &comcfg
);
1410 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1411 fprintf(stderr
, "Failed SetCommState\n");
1415 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1416 fprintf(stderr
, "Failed SetCommMask\n");
1420 cto
.ReadIntervalTimeout
= MAXDWORD
;
1421 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1422 fprintf(stderr
, "Failed SetCommTimeouts\n");
1426 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1427 fprintf(stderr
, "Failed ClearCommError\n");
1430 qemu_add_polling_cb(win_chr_poll
, chr
);
1438 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1440 WinCharState
*s
= chr
->opaque
;
1441 DWORD len
, ret
, size
, err
;
1444 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1445 s
->osend
.hEvent
= s
->hsend
;
1448 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1450 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1452 err
= GetLastError();
1453 if (err
== ERROR_IO_PENDING
) {
1454 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1472 static int win_chr_read_poll(CharDriverState
*chr
)
1474 WinCharState
*s
= chr
->opaque
;
1476 s
->max_size
= qemu_chr_can_read(chr
);
1480 static void win_chr_readfile(CharDriverState
*chr
)
1482 WinCharState
*s
= chr
->opaque
;
1487 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1488 s
->orecv
.hEvent
= s
->hrecv
;
1489 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1491 err
= GetLastError();
1492 if (err
== ERROR_IO_PENDING
) {
1493 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1498 qemu_chr_read(chr
, buf
, size
);
1502 static void win_chr_read(CharDriverState
*chr
)
1504 WinCharState
*s
= chr
->opaque
;
1506 if (s
->len
> s
->max_size
)
1507 s
->len
= s
->max_size
;
1511 win_chr_readfile(chr
);
1514 static int win_chr_poll(void *opaque
)
1516 CharDriverState
*chr
= opaque
;
1517 WinCharState
*s
= chr
->opaque
;
1521 ClearCommError(s
->hcom
, &comerr
, &status
);
1522 if (status
.cbInQue
> 0) {
1523 s
->len
= status
.cbInQue
;
1524 win_chr_read_poll(chr
);
1531 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1533 CharDriverState
*chr
;
1536 chr
= qemu_mallocz(sizeof(CharDriverState
));
1537 s
= qemu_mallocz(sizeof(WinCharState
));
1539 chr
->chr_write
= win_chr_write
;
1540 chr
->chr_close
= win_chr_close
;
1542 if (win_chr_init(chr
, filename
) < 0) {
1547 qemu_chr_reset(chr
);
1551 static int win_chr_pipe_poll(void *opaque
)
1553 CharDriverState
*chr
= opaque
;
1554 WinCharState
*s
= chr
->opaque
;
1557 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1560 win_chr_read_poll(chr
);
1567 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1569 WinCharState
*s
= chr
->opaque
;
1577 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1579 fprintf(stderr
, "Failed CreateEvent\n");
1582 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1584 fprintf(stderr
, "Failed CreateEvent\n");
1588 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1589 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1590 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1592 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1593 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1594 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1599 ZeroMemory(&ov
, sizeof(ov
));
1600 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1601 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1603 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1607 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1609 fprintf(stderr
, "Failed GetOverlappedResult\n");
1611 CloseHandle(ov
.hEvent
);
1618 CloseHandle(ov
.hEvent
);
1621 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1630 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1632 CharDriverState
*chr
;
1635 chr
= qemu_mallocz(sizeof(CharDriverState
));
1636 s
= qemu_mallocz(sizeof(WinCharState
));
1638 chr
->chr_write
= win_chr_write
;
1639 chr
->chr_close
= win_chr_close
;
1641 if (win_chr_pipe_init(chr
, filename
) < 0) {
1646 qemu_chr_reset(chr
);
1650 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1652 CharDriverState
*chr
;
1655 chr
= qemu_mallocz(sizeof(CharDriverState
));
1656 s
= qemu_mallocz(sizeof(WinCharState
));
1659 chr
->chr_write
= win_chr_write
;
1660 qemu_chr_reset(chr
);
1664 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1666 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1669 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1673 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1674 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1675 if (fd_out
== INVALID_HANDLE_VALUE
)
1678 return qemu_chr_open_win_file(fd_out
);
1680 #endif /* !_WIN32 */
1682 /***********************************************************/
1683 /* UDP Net console */
1687 struct sockaddr_in daddr
;
1694 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1696 NetCharDriver
*s
= chr
->opaque
;
1698 return sendto(s
->fd
, buf
, len
, 0,
1699 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1702 static int udp_chr_read_poll(void *opaque
)
1704 CharDriverState
*chr
= opaque
;
1705 NetCharDriver
*s
= chr
->opaque
;
1707 s
->max_size
= qemu_chr_can_read(chr
);
1709 /* If there were any stray characters in the queue process them
1712 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1713 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1715 s
->max_size
= qemu_chr_can_read(chr
);
1720 static void udp_chr_read(void *opaque
)
1722 CharDriverState
*chr
= opaque
;
1723 NetCharDriver
*s
= chr
->opaque
;
1725 if (s
->max_size
== 0)
1727 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1728 s
->bufptr
= s
->bufcnt
;
1733 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1734 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1736 s
->max_size
= qemu_chr_can_read(chr
);
1740 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1742 NetCharDriver
*s
= chr
->opaque
;
1745 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1746 udp_chr_read
, NULL
, chr
);
1750 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1752 CharDriverState
*chr
= NULL
;
1753 NetCharDriver
*s
= NULL
;
1755 struct sockaddr_in saddr
;
1757 chr
= qemu_mallocz(sizeof(CharDriverState
));
1758 s
= qemu_mallocz(sizeof(NetCharDriver
));
1760 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1762 perror("socket(PF_INET, SOCK_DGRAM)");
1766 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1767 printf("Could not parse: %s\n", def
);
1771 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1781 chr
->chr_write
= udp_chr_write
;
1782 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1795 /***********************************************************/
1796 /* TCP Net console */
1807 static void tcp_chr_accept(void *opaque
);
1809 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1811 TCPCharDriver
*s
= chr
->opaque
;
1813 return send_all(s
->fd
, buf
, len
);
1815 /* XXX: indicate an error ? */
1820 static int tcp_chr_read_poll(void *opaque
)
1822 CharDriverState
*chr
= opaque
;
1823 TCPCharDriver
*s
= chr
->opaque
;
1826 s
->max_size
= qemu_chr_can_read(chr
);
1831 #define IAC_BREAK 243
1832 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1834 uint8_t *buf
, int *size
)
1836 /* Handle any telnet client's basic IAC options to satisfy char by
1837 * char mode with no echo. All IAC options will be removed from
1838 * the buf and the do_telnetopt variable will be used to track the
1839 * state of the width of the IAC information.
1841 * IAC commands come in sets of 3 bytes with the exception of the
1842 * "IAC BREAK" command and the double IAC.
1848 for (i
= 0; i
< *size
; i
++) {
1849 if (s
->do_telnetopt
> 1) {
1850 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1851 /* Double IAC means send an IAC */
1855 s
->do_telnetopt
= 1;
1857 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1858 /* Handle IAC break commands by sending a serial break */
1859 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1864 if (s
->do_telnetopt
>= 4) {
1865 s
->do_telnetopt
= 1;
1868 if ((unsigned char)buf
[i
] == IAC
) {
1869 s
->do_telnetopt
= 2;
1880 static void tcp_chr_read(void *opaque
)
1882 CharDriverState
*chr
= opaque
;
1883 TCPCharDriver
*s
= chr
->opaque
;
1887 if (!s
->connected
|| s
->max_size
<= 0)
1890 if (len
> s
->max_size
)
1892 size
= recv(s
->fd
, buf
, len
, 0);
1894 /* connection closed */
1896 if (s
->listen_fd
>= 0) {
1897 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1899 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1902 } else if (size
> 0) {
1903 if (s
->do_telnetopt
)
1904 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1906 qemu_chr_read(chr
, buf
, size
);
1910 static void tcp_chr_connect(void *opaque
)
1912 CharDriverState
*chr
= opaque
;
1913 TCPCharDriver
*s
= chr
->opaque
;
1916 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1917 tcp_chr_read
, NULL
, chr
);
1918 qemu_chr_reset(chr
);
1921 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1922 static void tcp_chr_telnet_init(int fd
)
1925 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1926 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1927 send(fd
, (char *)buf
, 3, 0);
1928 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1929 send(fd
, (char *)buf
, 3, 0);
1930 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1931 send(fd
, (char *)buf
, 3, 0);
1932 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1933 send(fd
, (char *)buf
, 3, 0);
1936 static void socket_set_nodelay(int fd
)
1939 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1942 static void tcp_chr_accept(void *opaque
)
1944 CharDriverState
*chr
= opaque
;
1945 TCPCharDriver
*s
= chr
->opaque
;
1946 struct sockaddr_in saddr
;
1948 struct sockaddr_un uaddr
;
1950 struct sockaddr
*addr
;
1957 len
= sizeof(uaddr
);
1958 addr
= (struct sockaddr
*)&uaddr
;
1962 len
= sizeof(saddr
);
1963 addr
= (struct sockaddr
*)&saddr
;
1965 fd
= accept(s
->listen_fd
, addr
, &len
);
1966 if (fd
< 0 && errno
!= EINTR
) {
1968 } else if (fd
>= 0) {
1969 if (s
->do_telnetopt
)
1970 tcp_chr_telnet_init(fd
);
1974 socket_set_nonblock(fd
);
1976 socket_set_nodelay(fd
);
1978 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
1979 tcp_chr_connect(chr
);
1982 static void tcp_chr_close(CharDriverState
*chr
)
1984 TCPCharDriver
*s
= chr
->opaque
;
1987 if (s
->listen_fd
>= 0)
1988 closesocket(s
->listen_fd
);
1992 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
1996 CharDriverState
*chr
= NULL
;
1997 TCPCharDriver
*s
= NULL
;
1998 int fd
= -1, offset
= 0;
2000 int is_waitconnect
= 1;
2005 while((ptr
= strchr(ptr
,','))) {
2007 if (!strncmp(ptr
,"server",6)) {
2009 } else if (!strncmp(ptr
,"nowait",6)) {
2011 } else if (!strncmp(ptr
,"nodelay",6)) {
2013 } else if (!strncmp(ptr
,"to=",3)) {
2014 /* nothing, inet_listen() parses this one */;
2015 } else if (!strncmp(ptr
,"ipv4",4)) {
2016 /* nothing, inet_connect() and inet_listen() parse this one */;
2017 } else if (!strncmp(ptr
,"ipv6",4)) {
2018 /* nothing, inet_connect() and inet_listen() parse this one */;
2020 printf("Unknown option: %s\n", ptr
);
2027 chr
= qemu_mallocz(sizeof(CharDriverState
));
2028 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2031 chr
->filename
= qemu_malloc(256);
2033 pstrcpy(chr
->filename
, 256, "unix:");
2034 } else if (is_telnet
) {
2035 pstrcpy(chr
->filename
, 256, "telnet:");
2037 pstrcpy(chr
->filename
, 256, "tcp:");
2039 offset
= strlen(chr
->filename
);
2043 fd
= unix_listen(host_str
, chr
->filename
+ offset
, 256 - offset
);
2045 fd
= unix_connect(host_str
);
2049 fd
= inet_listen(host_str
, chr
->filename
+ offset
, 256 - offset
,
2052 fd
= inet_connect(host_str
, SOCK_STREAM
);
2058 if (!is_waitconnect
)
2059 socket_set_nonblock(fd
);
2064 s
->is_unix
= is_unix
;
2065 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2068 chr
->chr_write
= tcp_chr_write
;
2069 chr
->chr_close
= tcp_chr_close
;
2073 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2075 s
->do_telnetopt
= 1;
2079 socket_set_nodelay(fd
);
2080 tcp_chr_connect(chr
);
2083 if (is_listen
&& is_waitconnect
) {
2084 printf("QEMU waiting for connection on: %s\n",
2085 chr
->filename
? chr
->filename
: host_str
);
2086 tcp_chr_accept(chr
);
2087 socket_set_nonblock(s
->listen_fd
);
2099 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2102 CharDriverState
*chr
;
2104 if (!strcmp(filename
, "vc")) {
2105 chr
= text_console_init(0);
2107 if (strstart(filename
, "vc:", &p
)) {
2108 chr
= text_console_init(p
);
2110 if (!strcmp(filename
, "null")) {
2111 chr
= qemu_chr_open_null();
2113 if (strstart(filename
, "tcp:", &p
)) {
2114 chr
= qemu_chr_open_tcp(p
, 0, 0);
2116 if (strstart(filename
, "telnet:", &p
)) {
2117 chr
= qemu_chr_open_tcp(p
, 1, 0);
2119 if (strstart(filename
, "udp:", &p
)) {
2120 chr
= qemu_chr_open_udp(p
);
2122 if (strstart(filename
, "mon:", &p
)) {
2123 chr
= qemu_chr_open(label
, p
, NULL
);
2125 chr
= qemu_chr_open_mux(chr
);
2126 monitor_init(chr
, 0);
2128 printf("Unable to open driver: %s\n", p
);
2130 } else if (!strcmp(filename
, "msmouse")) {
2131 chr
= qemu_chr_open_msmouse();
2134 if (strstart(filename
, "unix:", &p
)) {
2135 chr
= qemu_chr_open_tcp(p
, 0, 1);
2136 } else if (strstart(filename
, "file:", &p
)) {
2137 chr
= qemu_chr_open_file_out(p
);
2138 } else if (strstart(filename
, "pipe:", &p
)) {
2139 chr
= qemu_chr_open_pipe(p
);
2140 } else if (!strcmp(filename
, "pty")) {
2141 chr
= qemu_chr_open_pty();
2142 } else if (!strcmp(filename
, "stdio")) {
2143 chr
= qemu_chr_open_stdio();
2145 #if defined(__linux__)
2146 if (strstart(filename
, "/dev/parport", NULL
)) {
2147 chr
= qemu_chr_open_pp(filename
);
2149 #elif defined(__FreeBSD__)
2150 if (strstart(filename
, "/dev/ppi", NULL
)) {
2151 chr
= qemu_chr_open_pp(filename
);
2154 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2155 || defined(__NetBSD__) || defined(__OpenBSD__)
2156 if (strstart(filename
, "/dev/", NULL
)) {
2157 chr
= qemu_chr_open_tty(filename
);
2161 if (strstart(filename
, "COM", NULL
)) {
2162 chr
= qemu_chr_open_win(filename
);
2164 if (strstart(filename
, "pipe:", &p
)) {
2165 chr
= qemu_chr_open_win_pipe(p
);
2167 if (strstart(filename
, "con:", NULL
)) {
2168 chr
= qemu_chr_open_win_con(filename
);
2170 if (strstart(filename
, "file:", &p
)) {
2171 chr
= qemu_chr_open_win_file_out(p
);
2174 #ifdef CONFIG_BRLAPI
2175 if (!strcmp(filename
, "braille")) {
2176 chr
= chr_baum_init();
2185 chr
->filename
= qemu_strdup(filename
);
2187 chr
->label
= qemu_strdup(label
);
2188 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2193 void qemu_chr_close(CharDriverState
*chr
)
2195 TAILQ_REMOVE(&chardevs
, chr
, next
);
2197 chr
->chr_close(chr
);
2198 qemu_free(chr
->filename
);
2199 qemu_free(chr
->label
);
2203 void qemu_chr_info(Monitor
*mon
)
2205 CharDriverState
*chr
;
2207 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2208 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);