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 int qemu_chr_get_msgfd(CharDriverState
*s
)
173 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
176 void qemu_chr_accept_input(CharDriverState
*s
)
178 if (s
->chr_accept_input
)
179 s
->chr_accept_input(s
);
182 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
187 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
188 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
192 void qemu_chr_send_event(CharDriverState
*s
, int event
)
194 if (s
->chr_send_event
)
195 s
->chr_send_event(s
, event
);
198 void qemu_chr_add_handlers(CharDriverState
*s
,
199 IOCanRWHandler
*fd_can_read
,
200 IOReadHandler
*fd_read
,
201 IOEventHandler
*fd_event
,
204 s
->chr_can_read
= fd_can_read
;
205 s
->chr_read
= fd_read
;
206 s
->chr_event
= fd_event
;
207 s
->handler_opaque
= opaque
;
208 if (s
->chr_update_read_handler
)
209 s
->chr_update_read_handler(s
);
212 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
217 static CharDriverState
*qemu_chr_open_null(void)
219 CharDriverState
*chr
;
221 chr
= qemu_mallocz(sizeof(CharDriverState
));
222 chr
->chr_write
= null_chr_write
;
226 /* MUX driver for serial I/O splitting */
228 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
229 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
231 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
232 IOReadHandler
*chr_read
[MAX_MUX
];
233 IOEventHandler
*chr_event
[MAX_MUX
];
234 void *ext_opaque
[MAX_MUX
];
235 CharDriverState
*drv
;
239 /* Intermediate input buffer allows to catch escape sequences even if the
240 currently active device is not accepting any input - but only until it
242 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
247 int64_t timestamps_start
;
251 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
253 MuxDriver
*d
= chr
->opaque
;
255 if (!d
->timestamps
) {
256 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
261 for (i
= 0; i
< len
; i
++) {
267 ti
= qemu_get_clock(rt_clock
);
268 if (d
->timestamps_start
== -1)
269 d
->timestamps_start
= ti
;
270 ti
-= d
->timestamps_start
;
272 snprintf(buf1
, sizeof(buf1
),
273 "[%02d:%02d:%02d.%03d] ",
278 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
281 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
282 if (buf
[i
] == '\n') {
290 static const char * const mux_help
[] = {
291 "% h print this help\n\r",
292 "% x exit emulator\n\r",
293 "% s save disk data back to file (if -snapshot)\n\r",
294 "% t toggle console timestamps\n\r"
295 "% b send break (magic sysrq)\n\r",
296 "% c switch between console and monitor\n\r",
301 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
302 static void mux_print_help(CharDriverState
*chr
)
305 char ebuf
[15] = "Escape-Char";
306 char cbuf
[50] = "\n\r";
308 if (term_escape_char
> 0 && term_escape_char
< 26) {
309 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
310 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
312 snprintf(cbuf
, sizeof(cbuf
),
313 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
316 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
317 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
318 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
319 if (mux_help
[i
][j
] == '%')
320 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
322 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
327 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
329 if (d
->chr_event
[mux_nr
])
330 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
333 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
335 if (d
->term_got_escape
) {
336 d
->term_got_escape
= 0;
337 if (ch
== term_escape_char
)
346 const char *term
= "QEMU: Terminated\n\r";
347 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
354 for (i
= 0; i
< nb_drives
; i
++) {
355 bdrv_commit(drives_table
[i
].bdrv
);
360 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
363 /* Switch to the next registered device */
364 mux_chr_send_event(d
, chr
->focus
, CHR_EVENT_MUX_OUT
);
366 if (chr
->focus
>= d
->mux_cnt
)
368 mux_chr_send_event(d
, chr
->focus
, CHR_EVENT_MUX_IN
);
371 d
->timestamps
= !d
->timestamps
;
372 d
->timestamps_start
= -1;
376 } else if (ch
== term_escape_char
) {
377 d
->term_got_escape
= 1;
385 static void mux_chr_accept_input(CharDriverState
*chr
)
388 MuxDriver
*d
= chr
->opaque
;
390 while (d
->prod
[m
] != d
->cons
[m
] &&
391 d
->chr_can_read
[m
] &&
392 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
393 d
->chr_read
[m
](d
->ext_opaque
[m
],
394 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
398 static int mux_chr_can_read(void *opaque
)
400 CharDriverState
*chr
= opaque
;
401 MuxDriver
*d
= chr
->opaque
;
404 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
406 if (d
->chr_can_read
[m
])
407 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
411 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
413 CharDriverState
*chr
= opaque
;
414 MuxDriver
*d
= chr
->opaque
;
418 mux_chr_accept_input (opaque
);
420 for(i
= 0; i
< size
; i
++)
421 if (mux_proc_byte(chr
, d
, buf
[i
])) {
422 if (d
->prod
[m
] == d
->cons
[m
] &&
423 d
->chr_can_read
[m
] &&
424 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
425 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
427 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
431 static void mux_chr_event(void *opaque
, int event
)
433 CharDriverState
*chr
= opaque
;
434 MuxDriver
*d
= chr
->opaque
;
437 /* Send the event to all registered listeners */
438 for (i
= 0; i
< d
->mux_cnt
; i
++)
439 mux_chr_send_event(d
, i
, event
);
442 static void mux_chr_update_read_handler(CharDriverState
*chr
)
444 MuxDriver
*d
= chr
->opaque
;
446 if (d
->mux_cnt
>= MAX_MUX
) {
447 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
450 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
451 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
452 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
453 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
454 /* Fix up the real driver with mux routines */
455 if (d
->mux_cnt
== 0) {
456 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
459 chr
->focus
= d
->mux_cnt
;
463 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
465 CharDriverState
*chr
;
468 chr
= qemu_mallocz(sizeof(CharDriverState
));
469 d
= qemu_mallocz(sizeof(MuxDriver
));
474 chr
->chr_write
= mux_chr_write
;
475 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
476 chr
->chr_accept_input
= mux_chr_accept_input
;
482 int send_all(int fd
, const void *buf
, int len1
)
488 ret
= send(fd
, buf
, len
, 0);
490 errno
= WSAGetLastError();
491 if (errno
!= WSAEWOULDBLOCK
) {
494 } else if (ret
== 0) {
506 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
512 ret
= write(fd
, buf
, len
);
514 if (errno
!= EINTR
&& errno
!= EAGAIN
)
516 } else if (ret
== 0) {
526 int send_all(int fd
, const void *buf
, int len1
)
528 return unix_write(fd
, buf
, len1
);
539 #define STDIO_MAX_CLIENTS 1
540 static int stdio_nb_clients
= 0;
542 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
544 FDCharDriver
*s
= chr
->opaque
;
545 return send_all(s
->fd_out
, buf
, len
);
548 static int fd_chr_read_poll(void *opaque
)
550 CharDriverState
*chr
= opaque
;
551 FDCharDriver
*s
= chr
->opaque
;
553 s
->max_size
= qemu_chr_can_read(chr
);
557 static void fd_chr_read(void *opaque
)
559 CharDriverState
*chr
= opaque
;
560 FDCharDriver
*s
= chr
->opaque
;
565 if (len
> s
->max_size
)
569 size
= read(s
->fd_in
, buf
, len
);
571 /* FD has been closed. Remove it from the active list. */
572 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
576 qemu_chr_read(chr
, buf
, size
);
580 static void fd_chr_update_read_handler(CharDriverState
*chr
)
582 FDCharDriver
*s
= chr
->opaque
;
585 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
587 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
588 fd_chr_read
, NULL
, chr
);
593 static void fd_chr_close(struct CharDriverState
*chr
)
595 FDCharDriver
*s
= chr
->opaque
;
598 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
600 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
607 /* open a character device to a unix fd */
608 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
610 CharDriverState
*chr
;
613 chr
= qemu_mallocz(sizeof(CharDriverState
));
614 s
= qemu_mallocz(sizeof(FDCharDriver
));
618 chr
->chr_write
= fd_chr_write
;
619 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
620 chr
->chr_close
= fd_chr_close
;
627 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
631 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
634 return qemu_chr_open_fd(-1, fd_out
);
637 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
640 char filename_in
[256], filename_out
[256];
642 snprintf(filename_in
, 256, "%s.in", filename
);
643 snprintf(filename_out
, 256, "%s.out", filename
);
644 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
645 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
646 if (fd_in
< 0 || fd_out
< 0) {
651 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
655 return qemu_chr_open_fd(fd_in
, fd_out
);
659 /* for STDIO, we handle the case where several clients use it
662 #define TERM_FIFO_MAX_SIZE 1
664 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
665 static int term_fifo_size
;
667 static int stdio_read_poll(void *opaque
)
669 CharDriverState
*chr
= opaque
;
671 /* try to flush the queue if needed */
672 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
673 qemu_chr_read(chr
, term_fifo
, 1);
676 /* see if we can absorb more chars */
677 if (term_fifo_size
== 0)
683 static void stdio_read(void *opaque
)
687 CharDriverState
*chr
= opaque
;
689 size
= read(0, buf
, 1);
691 /* stdin has been closed. Remove it from the active list. */
692 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
696 if (qemu_chr_can_read(chr
) > 0) {
697 qemu_chr_read(chr
, buf
, 1);
698 } else if (term_fifo_size
== 0) {
699 term_fifo
[term_fifo_size
++] = buf
[0];
704 /* init terminal so that we can grab keys */
705 static struct termios oldtty
;
706 static int old_fd0_flags
;
707 static int term_atexit_done
;
709 static void term_exit(void)
711 tcsetattr (0, TCSANOW
, &oldtty
);
712 fcntl(0, F_SETFL
, old_fd0_flags
);
715 static void term_init(void)
721 old_fd0_flags
= fcntl(0, F_GETFL
);
723 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
724 |INLCR
|IGNCR
|ICRNL
|IXON
);
725 tty
.c_oflag
|= OPOST
;
726 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
727 /* if graphical mode, we allow Ctrl-C handling */
728 if (display_type
== DT_NOGRAPHIC
)
729 tty
.c_lflag
&= ~ISIG
;
730 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
735 tcsetattr (0, TCSANOW
, &tty
);
737 if (!term_atexit_done
++)
740 fcntl(0, F_SETFL
, O_NONBLOCK
);
743 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
747 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
751 static CharDriverState
*qemu_chr_open_stdio(void)
753 CharDriverState
*chr
;
755 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
757 chr
= qemu_chr_open_fd(0, 1);
758 chr
->chr_close
= qemu_chr_close_stdio
;
759 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
767 /* Once Solaris has openpty(), this is going to be removed. */
768 static int openpty(int *amaster
, int *aslave
, char *name
,
769 struct termios
*termp
, struct winsize
*winp
)
772 int mfd
= -1, sfd
= -1;
774 *amaster
= *aslave
= -1;
776 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
780 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
783 if ((slave
= ptsname(mfd
)) == NULL
)
786 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
789 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
790 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
798 ioctl(sfd
, TIOCSWINSZ
, winp
);
809 static void cfmakeraw (struct termios
*termios_p
)
811 termios_p
->c_iflag
&=
812 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
813 termios_p
->c_oflag
&= ~OPOST
;
814 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
815 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
816 termios_p
->c_cflag
|= CS8
;
818 termios_p
->c_cc
[VMIN
] = 0;
819 termios_p
->c_cc
[VTIME
] = 0;
823 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
824 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
834 static void pty_chr_update_read_handler(CharDriverState
*chr
);
835 static void pty_chr_state(CharDriverState
*chr
, int connected
);
837 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
839 PtyCharDriver
*s
= chr
->opaque
;
842 /* guest sends data, check for (re-)connect */
843 pty_chr_update_read_handler(chr
);
846 return send_all(s
->fd
, buf
, len
);
849 static int pty_chr_read_poll(void *opaque
)
851 CharDriverState
*chr
= opaque
;
852 PtyCharDriver
*s
= chr
->opaque
;
854 s
->read_bytes
= qemu_chr_can_read(chr
);
855 return s
->read_bytes
;
858 static void pty_chr_read(void *opaque
)
860 CharDriverState
*chr
= opaque
;
861 PtyCharDriver
*s
= chr
->opaque
;
866 if (len
> s
->read_bytes
)
870 size
= read(s
->fd
, buf
, len
);
871 if ((size
== -1 && errno
== EIO
) ||
873 pty_chr_state(chr
, 0);
877 pty_chr_state(chr
, 1);
878 qemu_chr_read(chr
, buf
, size
);
882 static void pty_chr_update_read_handler(CharDriverState
*chr
)
884 PtyCharDriver
*s
= chr
->opaque
;
886 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
887 pty_chr_read
, NULL
, chr
);
890 * Short timeout here: just need wait long enougth that qemu makes
891 * it through the poll loop once. When reconnected we want a
892 * short timeout so we notice it almost instantly. Otherwise
893 * read() gives us -EIO instantly, making pty_chr_state() reset the
894 * timeout to the normal (much longer) poll interval before the
897 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
900 static void pty_chr_state(CharDriverState
*chr
, int connected
)
902 PtyCharDriver
*s
= chr
->opaque
;
905 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
908 /* (re-)connect poll interval for idle guests: once per second.
909 * We check more frequently in case the guests sends data to
910 * the virtual device linked to our pty. */
911 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
919 static void pty_chr_timer(void *opaque
)
921 struct CharDriverState
*chr
= opaque
;
922 PtyCharDriver
*s
= chr
->opaque
;
927 /* If we arrive here without polling being cleared due
928 * read returning -EIO, then we are (re-)connected */
929 pty_chr_state(chr
, 1);
934 pty_chr_update_read_handler(chr
);
937 static void pty_chr_close(struct CharDriverState
*chr
)
939 PtyCharDriver
*s
= chr
->opaque
;
941 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
943 qemu_del_timer(s
->timer
);
944 qemu_free_timer(s
->timer
);
948 static CharDriverState
*qemu_chr_open_pty(void)
950 CharDriverState
*chr
;
954 #if defined(__OpenBSD__) || defined(__DragonFly__)
955 char pty_name
[PATH_MAX
];
956 #define q_ptsname(x) pty_name
958 char *pty_name
= NULL
;
959 #define q_ptsname(x) ptsname(x)
962 chr
= qemu_mallocz(sizeof(CharDriverState
));
963 s
= qemu_mallocz(sizeof(PtyCharDriver
));
965 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
969 /* Set raw attributes on the pty. */
970 tcgetattr(slave_fd
, &tty
);
972 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
975 len
= strlen(q_ptsname(s
->fd
)) + 5;
976 chr
->filename
= qemu_malloc(len
);
977 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
978 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
981 chr
->chr_write
= pty_chr_write
;
982 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
983 chr
->chr_close
= pty_chr_close
;
985 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
990 static void tty_serial_init(int fd
, int speed
,
991 int parity
, int data_bits
, int stop_bits
)
997 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
998 speed
, parity
, data_bits
, stop_bits
);
1000 tcgetattr (fd
, &tty
);
1003 if (speed
<= 50 * MARGIN
)
1005 else if (speed
<= 75 * MARGIN
)
1007 else if (speed
<= 300 * MARGIN
)
1009 else if (speed
<= 600 * MARGIN
)
1011 else if (speed
<= 1200 * MARGIN
)
1013 else if (speed
<= 2400 * MARGIN
)
1015 else if (speed
<= 4800 * MARGIN
)
1017 else if (speed
<= 9600 * MARGIN
)
1019 else if (speed
<= 19200 * MARGIN
)
1021 else if (speed
<= 38400 * MARGIN
)
1023 else if (speed
<= 57600 * MARGIN
)
1025 else if (speed
<= 115200 * MARGIN
)
1030 cfsetispeed(&tty
, spd
);
1031 cfsetospeed(&tty
, spd
);
1033 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1034 |INLCR
|IGNCR
|ICRNL
|IXON
);
1035 tty
.c_oflag
|= OPOST
;
1036 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1037 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1058 tty
.c_cflag
|= PARENB
;
1061 tty
.c_cflag
|= PARENB
| PARODD
;
1065 tty
.c_cflag
|= CSTOPB
;
1067 tcsetattr (fd
, TCSANOW
, &tty
);
1070 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1072 FDCharDriver
*s
= chr
->opaque
;
1075 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1077 QEMUSerialSetParams
*ssp
= arg
;
1078 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1079 ssp
->data_bits
, ssp
->stop_bits
);
1082 case CHR_IOCTL_SERIAL_SET_BREAK
:
1084 int enable
= *(int *)arg
;
1086 tcsendbreak(s
->fd_in
, 1);
1089 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1092 int *targ
= (int *)arg
;
1093 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1095 if (sarg
& TIOCM_CTS
)
1096 *targ
|= CHR_TIOCM_CTS
;
1097 if (sarg
& TIOCM_CAR
)
1098 *targ
|= CHR_TIOCM_CAR
;
1099 if (sarg
& TIOCM_DSR
)
1100 *targ
|= CHR_TIOCM_DSR
;
1101 if (sarg
& TIOCM_RI
)
1102 *targ
|= CHR_TIOCM_RI
;
1103 if (sarg
& TIOCM_DTR
)
1104 *targ
|= CHR_TIOCM_DTR
;
1105 if (sarg
& TIOCM_RTS
)
1106 *targ
|= CHR_TIOCM_RTS
;
1109 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1111 int sarg
= *(int *)arg
;
1113 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1114 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1115 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1116 if (sarg
& CHR_TIOCM_CTS
)
1118 if (sarg
& CHR_TIOCM_CAR
)
1120 if (sarg
& CHR_TIOCM_DSR
)
1122 if (sarg
& CHR_TIOCM_RI
)
1124 if (sarg
& CHR_TIOCM_DTR
)
1126 if (sarg
& CHR_TIOCM_RTS
)
1128 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1137 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1139 CharDriverState
*chr
;
1142 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1143 tty_serial_init(fd
, 115200, 'N', 8, 1);
1144 chr
= qemu_chr_open_fd(fd
, fd
);
1149 chr
->chr_ioctl
= tty_serial_ioctl
;
1150 qemu_chr_reset(chr
);
1153 #else /* ! __linux__ && ! __sun__ */
1154 static CharDriverState
*qemu_chr_open_pty(void)
1158 #endif /* __linux__ || __sun__ */
1160 #if defined(__linux__)
1164 } ParallelCharDriver
;
1166 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1168 if (s
->mode
!= mode
) {
1170 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1177 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1179 ParallelCharDriver
*drv
= chr
->opaque
;
1184 case CHR_IOCTL_PP_READ_DATA
:
1185 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1187 *(uint8_t *)arg
= b
;
1189 case CHR_IOCTL_PP_WRITE_DATA
:
1190 b
= *(uint8_t *)arg
;
1191 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1194 case CHR_IOCTL_PP_READ_CONTROL
:
1195 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1197 /* Linux gives only the lowest bits, and no way to know data
1198 direction! For better compatibility set the fixed upper
1200 *(uint8_t *)arg
= b
| 0xc0;
1202 case CHR_IOCTL_PP_WRITE_CONTROL
:
1203 b
= *(uint8_t *)arg
;
1204 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1207 case CHR_IOCTL_PP_READ_STATUS
:
1208 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1210 *(uint8_t *)arg
= b
;
1212 case CHR_IOCTL_PP_DATA_DIR
:
1213 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1216 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1217 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1218 struct ParallelIOArg
*parg
= arg
;
1219 int n
= read(fd
, parg
->buffer
, parg
->count
);
1220 if (n
!= parg
->count
) {
1225 case CHR_IOCTL_PP_EPP_READ
:
1226 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1227 struct ParallelIOArg
*parg
= arg
;
1228 int n
= read(fd
, parg
->buffer
, parg
->count
);
1229 if (n
!= parg
->count
) {
1234 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1235 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1236 struct ParallelIOArg
*parg
= arg
;
1237 int n
= write(fd
, parg
->buffer
, parg
->count
);
1238 if (n
!= parg
->count
) {
1243 case CHR_IOCTL_PP_EPP_WRITE
:
1244 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1245 struct ParallelIOArg
*parg
= arg
;
1246 int n
= write(fd
, parg
->buffer
, parg
->count
);
1247 if (n
!= parg
->count
) {
1258 static void pp_close(CharDriverState
*chr
)
1260 ParallelCharDriver
*drv
= chr
->opaque
;
1263 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1264 ioctl(fd
, PPRELEASE
);
1269 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1271 CharDriverState
*chr
;
1272 ParallelCharDriver
*drv
;
1275 TFR(fd
= open(filename
, O_RDWR
));
1279 if (ioctl(fd
, PPCLAIM
) < 0) {
1284 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1286 drv
->mode
= IEEE1284_MODE_COMPAT
;
1288 chr
= qemu_mallocz(sizeof(CharDriverState
));
1289 chr
->chr_write
= null_chr_write
;
1290 chr
->chr_ioctl
= pp_ioctl
;
1291 chr
->chr_close
= pp_close
;
1294 qemu_chr_reset(chr
);
1298 #endif /* __linux__ */
1300 #if defined(__FreeBSD__) || defined(__DragonFly__)
1301 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1303 int fd
= (int)chr
->opaque
;
1307 case CHR_IOCTL_PP_READ_DATA
:
1308 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1310 *(uint8_t *)arg
= b
;
1312 case CHR_IOCTL_PP_WRITE_DATA
:
1313 b
= *(uint8_t *)arg
;
1314 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1317 case CHR_IOCTL_PP_READ_CONTROL
:
1318 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1320 *(uint8_t *)arg
= b
;
1322 case CHR_IOCTL_PP_WRITE_CONTROL
:
1323 b
= *(uint8_t *)arg
;
1324 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1327 case CHR_IOCTL_PP_READ_STATUS
:
1328 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1330 *(uint8_t *)arg
= b
;
1338 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1340 CharDriverState
*chr
;
1343 fd
= open(filename
, O_RDWR
);
1347 chr
= qemu_mallocz(sizeof(CharDriverState
));
1348 chr
->opaque
= (void *)fd
;
1349 chr
->chr_write
= null_chr_write
;
1350 chr
->chr_ioctl
= pp_ioctl
;
1359 HANDLE hcom
, hrecv
, hsend
;
1360 OVERLAPPED orecv
, osend
;
1365 #define NSENDBUF 2048
1366 #define NRECVBUF 2048
1367 #define MAXCONNECT 1
1368 #define NTIMEOUT 5000
1370 static int win_chr_poll(void *opaque
);
1371 static int win_chr_pipe_poll(void *opaque
);
1373 static void win_chr_close(CharDriverState
*chr
)
1375 WinCharState
*s
= chr
->opaque
;
1378 CloseHandle(s
->hsend
);
1382 CloseHandle(s
->hrecv
);
1386 CloseHandle(s
->hcom
);
1390 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1392 qemu_del_polling_cb(win_chr_poll
, chr
);
1395 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1397 WinCharState
*s
= chr
->opaque
;
1399 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1404 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1406 fprintf(stderr
, "Failed CreateEvent\n");
1409 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1411 fprintf(stderr
, "Failed CreateEvent\n");
1415 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1416 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1417 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1418 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1423 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1424 fprintf(stderr
, "Failed SetupComm\n");
1428 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1429 size
= sizeof(COMMCONFIG
);
1430 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1431 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1432 CommConfigDialog(filename
, NULL
, &comcfg
);
1434 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1435 fprintf(stderr
, "Failed SetCommState\n");
1439 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1440 fprintf(stderr
, "Failed SetCommMask\n");
1444 cto
.ReadIntervalTimeout
= MAXDWORD
;
1445 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1446 fprintf(stderr
, "Failed SetCommTimeouts\n");
1450 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1451 fprintf(stderr
, "Failed ClearCommError\n");
1454 qemu_add_polling_cb(win_chr_poll
, chr
);
1462 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1464 WinCharState
*s
= chr
->opaque
;
1465 DWORD len
, ret
, size
, err
;
1468 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1469 s
->osend
.hEvent
= s
->hsend
;
1472 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1474 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1476 err
= GetLastError();
1477 if (err
== ERROR_IO_PENDING
) {
1478 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1496 static int win_chr_read_poll(CharDriverState
*chr
)
1498 WinCharState
*s
= chr
->opaque
;
1500 s
->max_size
= qemu_chr_can_read(chr
);
1504 static void win_chr_readfile(CharDriverState
*chr
)
1506 WinCharState
*s
= chr
->opaque
;
1511 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1512 s
->orecv
.hEvent
= s
->hrecv
;
1513 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1515 err
= GetLastError();
1516 if (err
== ERROR_IO_PENDING
) {
1517 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1522 qemu_chr_read(chr
, buf
, size
);
1526 static void win_chr_read(CharDriverState
*chr
)
1528 WinCharState
*s
= chr
->opaque
;
1530 if (s
->len
> s
->max_size
)
1531 s
->len
= s
->max_size
;
1535 win_chr_readfile(chr
);
1538 static int win_chr_poll(void *opaque
)
1540 CharDriverState
*chr
= opaque
;
1541 WinCharState
*s
= chr
->opaque
;
1545 ClearCommError(s
->hcom
, &comerr
, &status
);
1546 if (status
.cbInQue
> 0) {
1547 s
->len
= status
.cbInQue
;
1548 win_chr_read_poll(chr
);
1555 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1557 CharDriverState
*chr
;
1560 chr
= qemu_mallocz(sizeof(CharDriverState
));
1561 s
= qemu_mallocz(sizeof(WinCharState
));
1563 chr
->chr_write
= win_chr_write
;
1564 chr
->chr_close
= win_chr_close
;
1566 if (win_chr_init(chr
, filename
) < 0) {
1571 qemu_chr_reset(chr
);
1575 static int win_chr_pipe_poll(void *opaque
)
1577 CharDriverState
*chr
= opaque
;
1578 WinCharState
*s
= chr
->opaque
;
1581 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1584 win_chr_read_poll(chr
);
1591 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1593 WinCharState
*s
= chr
->opaque
;
1601 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1603 fprintf(stderr
, "Failed CreateEvent\n");
1606 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1608 fprintf(stderr
, "Failed CreateEvent\n");
1612 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1613 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1614 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1616 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1617 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1618 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1623 ZeroMemory(&ov
, sizeof(ov
));
1624 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1625 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1627 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1631 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1633 fprintf(stderr
, "Failed GetOverlappedResult\n");
1635 CloseHandle(ov
.hEvent
);
1642 CloseHandle(ov
.hEvent
);
1645 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1654 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1656 CharDriverState
*chr
;
1659 chr
= qemu_mallocz(sizeof(CharDriverState
));
1660 s
= qemu_mallocz(sizeof(WinCharState
));
1662 chr
->chr_write
= win_chr_write
;
1663 chr
->chr_close
= win_chr_close
;
1665 if (win_chr_pipe_init(chr
, filename
) < 0) {
1670 qemu_chr_reset(chr
);
1674 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1676 CharDriverState
*chr
;
1679 chr
= qemu_mallocz(sizeof(CharDriverState
));
1680 s
= qemu_mallocz(sizeof(WinCharState
));
1683 chr
->chr_write
= win_chr_write
;
1684 qemu_chr_reset(chr
);
1688 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1690 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1693 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1697 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1698 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1699 if (fd_out
== INVALID_HANDLE_VALUE
)
1702 return qemu_chr_open_win_file(fd_out
);
1704 #endif /* !_WIN32 */
1706 /***********************************************************/
1707 /* UDP Net console */
1711 struct sockaddr_in daddr
;
1718 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1720 NetCharDriver
*s
= chr
->opaque
;
1722 return sendto(s
->fd
, (const void *)buf
, len
, 0,
1723 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1726 static int udp_chr_read_poll(void *opaque
)
1728 CharDriverState
*chr
= opaque
;
1729 NetCharDriver
*s
= chr
->opaque
;
1731 s
->max_size
= qemu_chr_can_read(chr
);
1733 /* If there were any stray characters in the queue process them
1736 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1737 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1739 s
->max_size
= qemu_chr_can_read(chr
);
1744 static void udp_chr_read(void *opaque
)
1746 CharDriverState
*chr
= opaque
;
1747 NetCharDriver
*s
= chr
->opaque
;
1749 if (s
->max_size
== 0)
1751 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1752 s
->bufptr
= s
->bufcnt
;
1757 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1758 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1760 s
->max_size
= qemu_chr_can_read(chr
);
1764 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1766 NetCharDriver
*s
= chr
->opaque
;
1769 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1770 udp_chr_read
, NULL
, chr
);
1774 static void udp_chr_close(CharDriverState
*chr
)
1776 NetCharDriver
*s
= chr
->opaque
;
1778 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1784 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1786 CharDriverState
*chr
= NULL
;
1787 NetCharDriver
*s
= NULL
;
1789 struct sockaddr_in saddr
;
1791 chr
= qemu_mallocz(sizeof(CharDriverState
));
1792 s
= qemu_mallocz(sizeof(NetCharDriver
));
1794 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1796 perror("socket(PF_INET, SOCK_DGRAM)");
1800 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1801 printf("Could not parse: %s\n", def
);
1805 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1815 chr
->chr_write
= udp_chr_write
;
1816 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1817 chr
->chr_close
= udp_chr_close
;
1830 /***********************************************************/
1831 /* TCP Net console */
1843 static void tcp_chr_accept(void *opaque
);
1845 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1847 TCPCharDriver
*s
= chr
->opaque
;
1849 return send_all(s
->fd
, buf
, len
);
1851 /* XXX: indicate an error ? */
1856 static int tcp_chr_read_poll(void *opaque
)
1858 CharDriverState
*chr
= opaque
;
1859 TCPCharDriver
*s
= chr
->opaque
;
1862 s
->max_size
= qemu_chr_can_read(chr
);
1867 #define IAC_BREAK 243
1868 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1870 uint8_t *buf
, int *size
)
1872 /* Handle any telnet client's basic IAC options to satisfy char by
1873 * char mode with no echo. All IAC options will be removed from
1874 * the buf and the do_telnetopt variable will be used to track the
1875 * state of the width of the IAC information.
1877 * IAC commands come in sets of 3 bytes with the exception of the
1878 * "IAC BREAK" command and the double IAC.
1884 for (i
= 0; i
< *size
; i
++) {
1885 if (s
->do_telnetopt
> 1) {
1886 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1887 /* Double IAC means send an IAC */
1891 s
->do_telnetopt
= 1;
1893 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1894 /* Handle IAC break commands by sending a serial break */
1895 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1900 if (s
->do_telnetopt
>= 4) {
1901 s
->do_telnetopt
= 1;
1904 if ((unsigned char)buf
[i
] == IAC
) {
1905 s
->do_telnetopt
= 2;
1916 static int tcp_get_msgfd(CharDriverState
*chr
)
1918 TCPCharDriver
*s
= chr
->opaque
;
1924 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1926 TCPCharDriver
*s
= chr
->opaque
;
1927 struct cmsghdr
*cmsg
;
1929 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1932 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1933 cmsg
->cmsg_level
!= SOL_SOCKET
||
1934 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1937 fd
= *((int *)CMSG_DATA(cmsg
));
1947 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1949 TCPCharDriver
*s
= chr
->opaque
;
1950 struct msghdr msg
= { 0, };
1951 struct iovec iov
[1];
1953 struct cmsghdr cmsg
;
1954 char control
[CMSG_SPACE(sizeof(int))];
1958 iov
[0].iov_base
= buf
;
1959 iov
[0].iov_len
= len
;
1963 msg
.msg_control
= &msg_control
;
1964 msg
.msg_controllen
= sizeof(msg_control
);
1966 ret
= recvmsg(s
->fd
, &msg
, 0);
1967 if (ret
> 0 && s
->is_unix
)
1968 unix_process_msgfd(chr
, &msg
);
1973 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1975 TCPCharDriver
*s
= chr
->opaque
;
1976 return recv(s
->fd
, buf
, len
, 0);
1980 static void tcp_chr_read(void *opaque
)
1982 CharDriverState
*chr
= opaque
;
1983 TCPCharDriver
*s
= chr
->opaque
;
1987 if (!s
->connected
|| s
->max_size
<= 0)
1990 if (len
> s
->max_size
)
1992 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
1994 /* connection closed */
1996 if (s
->listen_fd
>= 0) {
1997 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1999 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2002 } else if (size
> 0) {
2003 if (s
->do_telnetopt
)
2004 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2006 qemu_chr_read(chr
, buf
, size
);
2007 if (s
->msgfd
!= -1) {
2014 static void tcp_chr_connect(void *opaque
)
2016 CharDriverState
*chr
= opaque
;
2017 TCPCharDriver
*s
= chr
->opaque
;
2020 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2021 tcp_chr_read
, NULL
, chr
);
2022 qemu_chr_reset(chr
);
2025 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2026 static void tcp_chr_telnet_init(int fd
)
2029 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2030 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2031 send(fd
, (char *)buf
, 3, 0);
2032 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2033 send(fd
, (char *)buf
, 3, 0);
2034 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2035 send(fd
, (char *)buf
, 3, 0);
2036 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2037 send(fd
, (char *)buf
, 3, 0);
2040 static void socket_set_nodelay(int fd
)
2043 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2046 static void tcp_chr_accept(void *opaque
)
2048 CharDriverState
*chr
= opaque
;
2049 TCPCharDriver
*s
= chr
->opaque
;
2050 struct sockaddr_in saddr
;
2052 struct sockaddr_un uaddr
;
2054 struct sockaddr
*addr
;
2061 len
= sizeof(uaddr
);
2062 addr
= (struct sockaddr
*)&uaddr
;
2066 len
= sizeof(saddr
);
2067 addr
= (struct sockaddr
*)&saddr
;
2069 fd
= accept(s
->listen_fd
, addr
, &len
);
2070 if (fd
< 0 && errno
!= EINTR
) {
2072 } else if (fd
>= 0) {
2073 if (s
->do_telnetopt
)
2074 tcp_chr_telnet_init(fd
);
2078 socket_set_nonblock(fd
);
2080 socket_set_nodelay(fd
);
2082 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2083 tcp_chr_connect(chr
);
2086 static void tcp_chr_close(CharDriverState
*chr
)
2088 TCPCharDriver
*s
= chr
->opaque
;
2090 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2093 if (s
->listen_fd
>= 0) {
2094 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2095 closesocket(s
->listen_fd
);
2100 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2104 CharDriverState
*chr
= NULL
;
2105 TCPCharDriver
*s
= NULL
;
2106 int fd
= -1, offset
= 0;
2108 int is_waitconnect
= 1;
2113 while((ptr
= strchr(ptr
,','))) {
2115 if (!strncmp(ptr
,"server",6)) {
2117 } else if (!strncmp(ptr
,"nowait",6)) {
2119 } else if (!strncmp(ptr
,"nodelay",6)) {
2121 } else if (!strncmp(ptr
,"to=",3)) {
2122 /* nothing, inet_listen() parses this one */;
2123 } else if (!strncmp(ptr
,"ipv4",4)) {
2124 /* nothing, inet_connect() and inet_listen() parse this one */;
2125 } else if (!strncmp(ptr
,"ipv6",4)) {
2126 /* nothing, inet_connect() and inet_listen() parse this one */;
2128 printf("Unknown option: %s\n", ptr
);
2135 chr
= qemu_mallocz(sizeof(CharDriverState
));
2136 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2139 chr
->filename
= qemu_malloc(256);
2141 pstrcpy(chr
->filename
, 256, "unix:");
2142 } else if (is_telnet
) {
2143 pstrcpy(chr
->filename
, 256, "telnet:");
2145 pstrcpy(chr
->filename
, 256, "tcp:");
2147 offset
= strlen(chr
->filename
);
2151 fd
= unix_listen(host_str
, chr
->filename
+ offset
, 256 - offset
);
2153 fd
= unix_connect(host_str
);
2157 fd
= inet_listen(host_str
, chr
->filename
+ offset
, 256 - offset
,
2160 fd
= inet_connect(host_str
, SOCK_STREAM
);
2166 if (!is_waitconnect
)
2167 socket_set_nonblock(fd
);
2173 s
->is_unix
= is_unix
;
2174 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2177 chr
->chr_write
= tcp_chr_write
;
2178 chr
->chr_close
= tcp_chr_close
;
2179 chr
->get_msgfd
= tcp_get_msgfd
;
2183 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2185 s
->do_telnetopt
= 1;
2189 socket_set_nodelay(fd
);
2190 tcp_chr_connect(chr
);
2193 if (is_listen
&& is_waitconnect
) {
2194 printf("QEMU waiting for connection on: %s\n",
2195 chr
->filename
? chr
->filename
: host_str
);
2196 tcp_chr_accept(chr
);
2197 socket_set_nonblock(s
->listen_fd
);
2209 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2212 CharDriverState
*chr
;
2214 if (!strcmp(filename
, "vc")) {
2215 chr
= text_console_init(0);
2217 if (strstart(filename
, "vc:", &p
)) {
2218 chr
= text_console_init(p
);
2220 if (!strcmp(filename
, "null")) {
2221 chr
= qemu_chr_open_null();
2223 if (strstart(filename
, "tcp:", &p
)) {
2224 chr
= qemu_chr_open_tcp(p
, 0, 0);
2226 if (strstart(filename
, "telnet:", &p
)) {
2227 chr
= qemu_chr_open_tcp(p
, 1, 0);
2229 if (strstart(filename
, "udp:", &p
)) {
2230 chr
= qemu_chr_open_udp(p
);
2232 if (strstart(filename
, "mon:", &p
)) {
2233 chr
= qemu_chr_open(label
, p
, NULL
);
2235 chr
= qemu_chr_open_mux(chr
);
2236 monitor_init(chr
, MONITOR_USE_READLINE
);
2238 printf("Unable to open driver: %s\n", p
);
2240 } else if (!strcmp(filename
, "msmouse")) {
2241 chr
= qemu_chr_open_msmouse();
2244 if (strstart(filename
, "unix:", &p
)) {
2245 chr
= qemu_chr_open_tcp(p
, 0, 1);
2246 } else if (strstart(filename
, "file:", &p
)) {
2247 chr
= qemu_chr_open_file_out(p
);
2248 } else if (strstart(filename
, "pipe:", &p
)) {
2249 chr
= qemu_chr_open_pipe(p
);
2250 } else if (!strcmp(filename
, "pty")) {
2251 chr
= qemu_chr_open_pty();
2252 } else if (!strcmp(filename
, "stdio")) {
2253 chr
= qemu_chr_open_stdio();
2255 #if defined(__linux__)
2256 if (strstart(filename
, "/dev/parport", NULL
)) {
2257 chr
= qemu_chr_open_pp(filename
);
2259 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2260 if (strstart(filename
, "/dev/ppi", NULL
)) {
2261 chr
= qemu_chr_open_pp(filename
);
2264 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2265 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2266 if (strstart(filename
, "/dev/", NULL
)) {
2267 chr
= qemu_chr_open_tty(filename
);
2271 if (strstart(filename
, "COM", NULL
)) {
2272 chr
= qemu_chr_open_win(filename
);
2274 if (strstart(filename
, "pipe:", &p
)) {
2275 chr
= qemu_chr_open_win_pipe(p
);
2277 if (strstart(filename
, "con:", NULL
)) {
2278 chr
= qemu_chr_open_win_con(filename
);
2280 if (strstart(filename
, "file:", &p
)) {
2281 chr
= qemu_chr_open_win_file_out(p
);
2284 #ifdef CONFIG_BRLAPI
2285 if (!strcmp(filename
, "braille")) {
2286 chr
= chr_baum_init();
2295 chr
->filename
= qemu_strdup(filename
);
2297 chr
->label
= qemu_strdup(label
);
2298 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2303 void qemu_chr_close(CharDriverState
*chr
)
2305 TAILQ_REMOVE(&chardevs
, chr
, next
);
2307 chr
->chr_close(chr
);
2308 qemu_free(chr
->filename
);
2309 qemu_free(chr
->label
);
2313 void qemu_chr_info(Monitor
*mon
)
2315 CharDriverState
*chr
;
2317 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2318 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);