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 */
223 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
224 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
226 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
227 IOReadHandler
*chr_read
[MAX_MUX
];
228 IOEventHandler
*chr_event
[MAX_MUX
];
229 void *ext_opaque
[MAX_MUX
];
230 CharDriverState
*drv
;
234 /* Intermediate input buffer allows to catch escape sequences even if the
235 currently active device is not accepting any input - but only until it
237 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
242 int64_t timestamps_start
;
246 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
248 MuxDriver
*d
= chr
->opaque
;
250 if (!d
->timestamps
) {
251 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
256 for (i
= 0; i
< len
; i
++) {
262 ti
= qemu_get_clock(rt_clock
);
263 if (d
->timestamps_start
== -1)
264 d
->timestamps_start
= ti
;
265 ti
-= d
->timestamps_start
;
267 snprintf(buf1
, sizeof(buf1
),
268 "[%02d:%02d:%02d.%03d] ",
273 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
276 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
277 if (buf
[i
] == '\n') {
285 static const char * const mux_help
[] = {
286 "% h print this help\n\r",
287 "% x exit emulator\n\r",
288 "% s save disk data back to file (if -snapshot)\n\r",
289 "% t toggle console timestamps\n\r"
290 "% b send break (magic sysrq)\n\r",
291 "% c switch between console and monitor\n\r",
296 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
297 static void mux_print_help(CharDriverState
*chr
)
300 char ebuf
[15] = "Escape-Char";
301 char cbuf
[50] = "\n\r";
303 if (term_escape_char
> 0 && term_escape_char
< 26) {
304 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
305 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
307 snprintf(cbuf
, sizeof(cbuf
),
308 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
311 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
312 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
313 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
314 if (mux_help
[i
][j
] == '%')
315 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
317 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
322 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
324 if (d
->chr_event
[mux_nr
])
325 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
328 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
330 if (d
->term_got_escape
) {
331 d
->term_got_escape
= 0;
332 if (ch
== term_escape_char
)
341 const char *term
= "QEMU: Terminated\n\r";
342 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
349 for (i
= 0; i
< nb_drives
; i
++) {
350 bdrv_commit(drives_table
[i
].bdrv
);
355 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
358 /* Switch to the next registered device */
359 mux_chr_send_event(d
, chr
->focus
, CHR_EVENT_MUX_OUT
);
361 if (chr
->focus
>= d
->mux_cnt
)
363 mux_chr_send_event(d
, chr
->focus
, CHR_EVENT_MUX_IN
);
366 d
->timestamps
= !d
->timestamps
;
367 d
->timestamps_start
= -1;
371 } else if (ch
== term_escape_char
) {
372 d
->term_got_escape
= 1;
380 static void mux_chr_accept_input(CharDriverState
*chr
)
383 MuxDriver
*d
= chr
->opaque
;
385 while (d
->prod
[m
] != d
->cons
[m
] &&
386 d
->chr_can_read
[m
] &&
387 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
388 d
->chr_read
[m
](d
->ext_opaque
[m
],
389 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
393 static int mux_chr_can_read(void *opaque
)
395 CharDriverState
*chr
= opaque
;
396 MuxDriver
*d
= chr
->opaque
;
399 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
401 if (d
->chr_can_read
[m
])
402 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
406 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
408 CharDriverState
*chr
= opaque
;
409 MuxDriver
*d
= chr
->opaque
;
413 mux_chr_accept_input (opaque
);
415 for(i
= 0; i
< size
; i
++)
416 if (mux_proc_byte(chr
, d
, buf
[i
])) {
417 if (d
->prod
[m
] == d
->cons
[m
] &&
418 d
->chr_can_read
[m
] &&
419 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
420 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
422 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
426 static void mux_chr_event(void *opaque
, int event
)
428 CharDriverState
*chr
= opaque
;
429 MuxDriver
*d
= chr
->opaque
;
432 /* Send the event to all registered listeners */
433 for (i
= 0; i
< d
->mux_cnt
; i
++)
434 mux_chr_send_event(d
, i
, event
);
437 static void mux_chr_update_read_handler(CharDriverState
*chr
)
439 MuxDriver
*d
= chr
->opaque
;
441 if (d
->mux_cnt
>= MAX_MUX
) {
442 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
445 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
446 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
447 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
448 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
449 /* Fix up the real driver with mux routines */
450 if (d
->mux_cnt
== 0) {
451 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
454 chr
->focus
= d
->mux_cnt
;
458 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
460 CharDriverState
*chr
;
463 chr
= qemu_mallocz(sizeof(CharDriverState
));
464 d
= qemu_mallocz(sizeof(MuxDriver
));
469 chr
->chr_write
= mux_chr_write
;
470 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
471 chr
->chr_accept_input
= mux_chr_accept_input
;
477 int send_all(int fd
, const void *buf
, int len1
)
483 ret
= send(fd
, buf
, len
, 0);
485 errno
= WSAGetLastError();
486 if (errno
!= WSAEWOULDBLOCK
) {
489 } else if (ret
== 0) {
501 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
507 ret
= write(fd
, buf
, len
);
509 if (errno
!= EINTR
&& errno
!= EAGAIN
)
511 } else if (ret
== 0) {
521 int send_all(int fd
, const void *buf
, int len1
)
523 return unix_write(fd
, buf
, len1
);
534 #define STDIO_MAX_CLIENTS 1
535 static int stdio_nb_clients
= 0;
537 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
539 FDCharDriver
*s
= chr
->opaque
;
540 return send_all(s
->fd_out
, buf
, len
);
543 static int fd_chr_read_poll(void *opaque
)
545 CharDriverState
*chr
= opaque
;
546 FDCharDriver
*s
= chr
->opaque
;
548 s
->max_size
= qemu_chr_can_read(chr
);
552 static void fd_chr_read(void *opaque
)
554 CharDriverState
*chr
= opaque
;
555 FDCharDriver
*s
= chr
->opaque
;
560 if (len
> s
->max_size
)
564 size
= read(s
->fd_in
, buf
, len
);
566 /* FD has been closed. Remove it from the active list. */
567 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
571 qemu_chr_read(chr
, buf
, size
);
575 static void fd_chr_update_read_handler(CharDriverState
*chr
)
577 FDCharDriver
*s
= chr
->opaque
;
580 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
582 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
583 fd_chr_read
, NULL
, chr
);
588 static void fd_chr_close(struct CharDriverState
*chr
)
590 FDCharDriver
*s
= chr
->opaque
;
593 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
595 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
602 /* open a character device to a unix fd */
603 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
605 CharDriverState
*chr
;
608 chr
= qemu_mallocz(sizeof(CharDriverState
));
609 s
= qemu_mallocz(sizeof(FDCharDriver
));
613 chr
->chr_write
= fd_chr_write
;
614 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
615 chr
->chr_close
= fd_chr_close
;
622 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
626 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
629 return qemu_chr_open_fd(-1, fd_out
);
632 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
635 char filename_in
[256], filename_out
[256];
637 snprintf(filename_in
, 256, "%s.in", filename
);
638 snprintf(filename_out
, 256, "%s.out", filename
);
639 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
640 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
641 if (fd_in
< 0 || fd_out
< 0) {
646 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
650 return qemu_chr_open_fd(fd_in
, fd_out
);
654 /* for STDIO, we handle the case where several clients use it
657 #define TERM_FIFO_MAX_SIZE 1
659 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
660 static int term_fifo_size
;
662 static int stdio_read_poll(void *opaque
)
664 CharDriverState
*chr
= opaque
;
666 /* try to flush the queue if needed */
667 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
668 qemu_chr_read(chr
, term_fifo
, 1);
671 /* see if we can absorb more chars */
672 if (term_fifo_size
== 0)
678 static void stdio_read(void *opaque
)
682 CharDriverState
*chr
= opaque
;
684 size
= read(0, buf
, 1);
686 /* stdin has been closed. Remove it from the active list. */
687 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
691 if (qemu_chr_can_read(chr
) > 0) {
692 qemu_chr_read(chr
, buf
, 1);
693 } else if (term_fifo_size
== 0) {
694 term_fifo
[term_fifo_size
++] = buf
[0];
699 /* init terminal so that we can grab keys */
700 static struct termios oldtty
;
701 static int old_fd0_flags
;
702 static int term_atexit_done
;
704 static void term_exit(void)
706 tcsetattr (0, TCSANOW
, &oldtty
);
707 fcntl(0, F_SETFL
, old_fd0_flags
);
710 static void term_init(void)
716 old_fd0_flags
= fcntl(0, F_GETFL
);
718 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
719 |INLCR
|IGNCR
|ICRNL
|IXON
);
720 tty
.c_oflag
|= OPOST
;
721 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
722 /* if graphical mode, we allow Ctrl-C handling */
723 if (display_type
== DT_NOGRAPHIC
)
724 tty
.c_lflag
&= ~ISIG
;
725 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
730 tcsetattr (0, TCSANOW
, &tty
);
732 if (!term_atexit_done
++)
735 fcntl(0, F_SETFL
, O_NONBLOCK
);
738 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
742 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
746 static CharDriverState
*qemu_chr_open_stdio(void)
748 CharDriverState
*chr
;
750 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
752 chr
= qemu_chr_open_fd(0, 1);
753 chr
->chr_close
= qemu_chr_close_stdio
;
754 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
762 /* Once Solaris has openpty(), this is going to be removed. */
763 static int openpty(int *amaster
, int *aslave
, char *name
,
764 struct termios
*termp
, struct winsize
*winp
)
767 int mfd
= -1, sfd
= -1;
769 *amaster
= *aslave
= -1;
771 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
775 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
778 if ((slave
= ptsname(mfd
)) == NULL
)
781 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
784 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
785 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
793 ioctl(sfd
, TIOCSWINSZ
, winp
);
804 static void cfmakeraw (struct termios
*termios_p
)
806 termios_p
->c_iflag
&=
807 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
808 termios_p
->c_oflag
&= ~OPOST
;
809 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
810 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
811 termios_p
->c_cflag
|= CS8
;
813 termios_p
->c_cc
[VMIN
] = 0;
814 termios_p
->c_cc
[VTIME
] = 0;
818 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
819 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
829 static void pty_chr_update_read_handler(CharDriverState
*chr
);
830 static void pty_chr_state(CharDriverState
*chr
, int connected
);
832 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
834 PtyCharDriver
*s
= chr
->opaque
;
837 /* guest sends data, check for (re-)connect */
838 pty_chr_update_read_handler(chr
);
841 return send_all(s
->fd
, buf
, len
);
844 static int pty_chr_read_poll(void *opaque
)
846 CharDriverState
*chr
= opaque
;
847 PtyCharDriver
*s
= chr
->opaque
;
849 s
->read_bytes
= qemu_chr_can_read(chr
);
850 return s
->read_bytes
;
853 static void pty_chr_read(void *opaque
)
855 CharDriverState
*chr
= opaque
;
856 PtyCharDriver
*s
= chr
->opaque
;
861 if (len
> s
->read_bytes
)
865 size
= read(s
->fd
, buf
, len
);
866 if ((size
== -1 && errno
== EIO
) ||
868 pty_chr_state(chr
, 0);
872 pty_chr_state(chr
, 1);
873 qemu_chr_read(chr
, buf
, size
);
877 static void pty_chr_update_read_handler(CharDriverState
*chr
)
879 PtyCharDriver
*s
= chr
->opaque
;
881 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
882 pty_chr_read
, NULL
, chr
);
885 * Short timeout here: just need wait long enougth that qemu makes
886 * it through the poll loop once. When reconnected we want a
887 * short timeout so we notice it almost instantly. Otherwise
888 * read() gives us -EIO instantly, making pty_chr_state() reset the
889 * timeout to the normal (much longer) poll interval before the
892 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
895 static void pty_chr_state(CharDriverState
*chr
, int connected
)
897 PtyCharDriver
*s
= chr
->opaque
;
900 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
903 /* (re-)connect poll interval for idle guests: once per second.
904 * We check more frequently in case the guests sends data to
905 * the virtual device linked to our pty. */
906 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
914 static void pty_chr_timer(void *opaque
)
916 struct CharDriverState
*chr
= opaque
;
917 PtyCharDriver
*s
= chr
->opaque
;
922 /* If we arrive here without polling being cleared due
923 * read returning -EIO, then we are (re-)connected */
924 pty_chr_state(chr
, 1);
929 pty_chr_update_read_handler(chr
);
932 static void pty_chr_close(struct CharDriverState
*chr
)
934 PtyCharDriver
*s
= chr
->opaque
;
936 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
938 qemu_del_timer(s
->timer
);
939 qemu_free_timer(s
->timer
);
943 static CharDriverState
*qemu_chr_open_pty(void)
945 CharDriverState
*chr
;
949 #if defined(__OpenBSD__) || defined(__DragonFly__)
950 char pty_name
[PATH_MAX
];
951 #define q_ptsname(x) pty_name
953 char *pty_name
= NULL
;
954 #define q_ptsname(x) ptsname(x)
957 chr
= qemu_mallocz(sizeof(CharDriverState
));
958 s
= qemu_mallocz(sizeof(PtyCharDriver
));
960 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
964 /* Set raw attributes on the pty. */
965 tcgetattr(slave_fd
, &tty
);
967 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
970 len
= strlen(q_ptsname(s
->fd
)) + 5;
971 chr
->filename
= qemu_malloc(len
);
972 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
973 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
976 chr
->chr_write
= pty_chr_write
;
977 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
978 chr
->chr_close
= pty_chr_close
;
980 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
985 static void tty_serial_init(int fd
, int speed
,
986 int parity
, int data_bits
, int stop_bits
)
992 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
993 speed
, parity
, data_bits
, stop_bits
);
995 tcgetattr (fd
, &tty
);
998 if (speed
<= 50 * MARGIN
)
1000 else if (speed
<= 75 * MARGIN
)
1002 else if (speed
<= 300 * MARGIN
)
1004 else if (speed
<= 600 * MARGIN
)
1006 else if (speed
<= 1200 * MARGIN
)
1008 else if (speed
<= 2400 * MARGIN
)
1010 else if (speed
<= 4800 * MARGIN
)
1012 else if (speed
<= 9600 * MARGIN
)
1014 else if (speed
<= 19200 * MARGIN
)
1016 else if (speed
<= 38400 * MARGIN
)
1018 else if (speed
<= 57600 * MARGIN
)
1020 else if (speed
<= 115200 * MARGIN
)
1025 cfsetispeed(&tty
, spd
);
1026 cfsetospeed(&tty
, spd
);
1028 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1029 |INLCR
|IGNCR
|ICRNL
|IXON
);
1030 tty
.c_oflag
|= OPOST
;
1031 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1032 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1053 tty
.c_cflag
|= PARENB
;
1056 tty
.c_cflag
|= PARENB
| PARODD
;
1060 tty
.c_cflag
|= CSTOPB
;
1062 tcsetattr (fd
, TCSANOW
, &tty
);
1065 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1067 FDCharDriver
*s
= chr
->opaque
;
1070 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1072 QEMUSerialSetParams
*ssp
= arg
;
1073 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1074 ssp
->data_bits
, ssp
->stop_bits
);
1077 case CHR_IOCTL_SERIAL_SET_BREAK
:
1079 int enable
= *(int *)arg
;
1081 tcsendbreak(s
->fd_in
, 1);
1084 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1087 int *targ
= (int *)arg
;
1088 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1090 if (sarg
& TIOCM_CTS
)
1091 *targ
|= CHR_TIOCM_CTS
;
1092 if (sarg
& TIOCM_CAR
)
1093 *targ
|= CHR_TIOCM_CAR
;
1094 if (sarg
& TIOCM_DSR
)
1095 *targ
|= CHR_TIOCM_DSR
;
1096 if (sarg
& TIOCM_RI
)
1097 *targ
|= CHR_TIOCM_RI
;
1098 if (sarg
& TIOCM_DTR
)
1099 *targ
|= CHR_TIOCM_DTR
;
1100 if (sarg
& TIOCM_RTS
)
1101 *targ
|= CHR_TIOCM_RTS
;
1104 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1106 int sarg
= *(int *)arg
;
1108 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1109 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1110 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1111 if (sarg
& CHR_TIOCM_CTS
)
1113 if (sarg
& CHR_TIOCM_CAR
)
1115 if (sarg
& CHR_TIOCM_DSR
)
1117 if (sarg
& CHR_TIOCM_RI
)
1119 if (sarg
& CHR_TIOCM_DTR
)
1121 if (sarg
& CHR_TIOCM_RTS
)
1123 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1132 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1134 CharDriverState
*chr
;
1137 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1138 tty_serial_init(fd
, 115200, 'N', 8, 1);
1139 chr
= qemu_chr_open_fd(fd
, fd
);
1144 chr
->chr_ioctl
= tty_serial_ioctl
;
1145 qemu_chr_reset(chr
);
1148 #else /* ! __linux__ && ! __sun__ */
1149 static CharDriverState
*qemu_chr_open_pty(void)
1153 #endif /* __linux__ || __sun__ */
1155 #if defined(__linux__)
1159 } ParallelCharDriver
;
1161 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1163 if (s
->mode
!= mode
) {
1165 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1172 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1174 ParallelCharDriver
*drv
= chr
->opaque
;
1179 case CHR_IOCTL_PP_READ_DATA
:
1180 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1182 *(uint8_t *)arg
= b
;
1184 case CHR_IOCTL_PP_WRITE_DATA
:
1185 b
= *(uint8_t *)arg
;
1186 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1189 case CHR_IOCTL_PP_READ_CONTROL
:
1190 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1192 /* Linux gives only the lowest bits, and no way to know data
1193 direction! For better compatibility set the fixed upper
1195 *(uint8_t *)arg
= b
| 0xc0;
1197 case CHR_IOCTL_PP_WRITE_CONTROL
:
1198 b
= *(uint8_t *)arg
;
1199 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1202 case CHR_IOCTL_PP_READ_STATUS
:
1203 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1205 *(uint8_t *)arg
= b
;
1207 case CHR_IOCTL_PP_DATA_DIR
:
1208 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1211 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1212 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1213 struct ParallelIOArg
*parg
= arg
;
1214 int n
= read(fd
, parg
->buffer
, parg
->count
);
1215 if (n
!= parg
->count
) {
1220 case CHR_IOCTL_PP_EPP_READ
:
1221 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1222 struct ParallelIOArg
*parg
= arg
;
1223 int n
= read(fd
, parg
->buffer
, parg
->count
);
1224 if (n
!= parg
->count
) {
1229 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1230 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1231 struct ParallelIOArg
*parg
= arg
;
1232 int n
= write(fd
, parg
->buffer
, parg
->count
);
1233 if (n
!= parg
->count
) {
1238 case CHR_IOCTL_PP_EPP_WRITE
:
1239 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1240 struct ParallelIOArg
*parg
= arg
;
1241 int n
= write(fd
, parg
->buffer
, parg
->count
);
1242 if (n
!= parg
->count
) {
1253 static void pp_close(CharDriverState
*chr
)
1255 ParallelCharDriver
*drv
= chr
->opaque
;
1258 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1259 ioctl(fd
, PPRELEASE
);
1264 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1266 CharDriverState
*chr
;
1267 ParallelCharDriver
*drv
;
1270 TFR(fd
= open(filename
, O_RDWR
));
1274 if (ioctl(fd
, PPCLAIM
) < 0) {
1279 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1281 drv
->mode
= IEEE1284_MODE_COMPAT
;
1283 chr
= qemu_mallocz(sizeof(CharDriverState
));
1284 chr
->chr_write
= null_chr_write
;
1285 chr
->chr_ioctl
= pp_ioctl
;
1286 chr
->chr_close
= pp_close
;
1289 qemu_chr_reset(chr
);
1293 #endif /* __linux__ */
1295 #if defined(__FreeBSD__) || defined(__DragonFly__)
1296 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1298 int fd
= (int)chr
->opaque
;
1302 case CHR_IOCTL_PP_READ_DATA
:
1303 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1305 *(uint8_t *)arg
= b
;
1307 case CHR_IOCTL_PP_WRITE_DATA
:
1308 b
= *(uint8_t *)arg
;
1309 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1312 case CHR_IOCTL_PP_READ_CONTROL
:
1313 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1315 *(uint8_t *)arg
= b
;
1317 case CHR_IOCTL_PP_WRITE_CONTROL
:
1318 b
= *(uint8_t *)arg
;
1319 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1322 case CHR_IOCTL_PP_READ_STATUS
:
1323 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1325 *(uint8_t *)arg
= b
;
1333 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1335 CharDriverState
*chr
;
1338 fd
= open(filename
, O_RDWR
);
1342 chr
= qemu_mallocz(sizeof(CharDriverState
));
1343 chr
->opaque
= (void *)fd
;
1344 chr
->chr_write
= null_chr_write
;
1345 chr
->chr_ioctl
= pp_ioctl
;
1354 HANDLE hcom
, hrecv
, hsend
;
1355 OVERLAPPED orecv
, osend
;
1360 #define NSENDBUF 2048
1361 #define NRECVBUF 2048
1362 #define MAXCONNECT 1
1363 #define NTIMEOUT 5000
1365 static int win_chr_poll(void *opaque
);
1366 static int win_chr_pipe_poll(void *opaque
);
1368 static void win_chr_close(CharDriverState
*chr
)
1370 WinCharState
*s
= chr
->opaque
;
1373 CloseHandle(s
->hsend
);
1377 CloseHandle(s
->hrecv
);
1381 CloseHandle(s
->hcom
);
1385 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1387 qemu_del_polling_cb(win_chr_poll
, chr
);
1390 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1392 WinCharState
*s
= chr
->opaque
;
1394 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1399 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1401 fprintf(stderr
, "Failed CreateEvent\n");
1404 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1406 fprintf(stderr
, "Failed CreateEvent\n");
1410 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1411 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1412 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1413 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1418 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1419 fprintf(stderr
, "Failed SetupComm\n");
1423 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1424 size
= sizeof(COMMCONFIG
);
1425 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1426 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1427 CommConfigDialog(filename
, NULL
, &comcfg
);
1429 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1430 fprintf(stderr
, "Failed SetCommState\n");
1434 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1435 fprintf(stderr
, "Failed SetCommMask\n");
1439 cto
.ReadIntervalTimeout
= MAXDWORD
;
1440 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1441 fprintf(stderr
, "Failed SetCommTimeouts\n");
1445 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1446 fprintf(stderr
, "Failed ClearCommError\n");
1449 qemu_add_polling_cb(win_chr_poll
, chr
);
1457 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1459 WinCharState
*s
= chr
->opaque
;
1460 DWORD len
, ret
, size
, err
;
1463 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1464 s
->osend
.hEvent
= s
->hsend
;
1467 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1469 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1471 err
= GetLastError();
1472 if (err
== ERROR_IO_PENDING
) {
1473 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1491 static int win_chr_read_poll(CharDriverState
*chr
)
1493 WinCharState
*s
= chr
->opaque
;
1495 s
->max_size
= qemu_chr_can_read(chr
);
1499 static void win_chr_readfile(CharDriverState
*chr
)
1501 WinCharState
*s
= chr
->opaque
;
1506 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1507 s
->orecv
.hEvent
= s
->hrecv
;
1508 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1510 err
= GetLastError();
1511 if (err
== ERROR_IO_PENDING
) {
1512 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1517 qemu_chr_read(chr
, buf
, size
);
1521 static void win_chr_read(CharDriverState
*chr
)
1523 WinCharState
*s
= chr
->opaque
;
1525 if (s
->len
> s
->max_size
)
1526 s
->len
= s
->max_size
;
1530 win_chr_readfile(chr
);
1533 static int win_chr_poll(void *opaque
)
1535 CharDriverState
*chr
= opaque
;
1536 WinCharState
*s
= chr
->opaque
;
1540 ClearCommError(s
->hcom
, &comerr
, &status
);
1541 if (status
.cbInQue
> 0) {
1542 s
->len
= status
.cbInQue
;
1543 win_chr_read_poll(chr
);
1550 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1552 CharDriverState
*chr
;
1555 chr
= qemu_mallocz(sizeof(CharDriverState
));
1556 s
= qemu_mallocz(sizeof(WinCharState
));
1558 chr
->chr_write
= win_chr_write
;
1559 chr
->chr_close
= win_chr_close
;
1561 if (win_chr_init(chr
, filename
) < 0) {
1566 qemu_chr_reset(chr
);
1570 static int win_chr_pipe_poll(void *opaque
)
1572 CharDriverState
*chr
= opaque
;
1573 WinCharState
*s
= chr
->opaque
;
1576 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1579 win_chr_read_poll(chr
);
1586 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1588 WinCharState
*s
= chr
->opaque
;
1596 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1598 fprintf(stderr
, "Failed CreateEvent\n");
1601 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1603 fprintf(stderr
, "Failed CreateEvent\n");
1607 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1608 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1609 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1611 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1612 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1613 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1618 ZeroMemory(&ov
, sizeof(ov
));
1619 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1620 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1622 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1626 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1628 fprintf(stderr
, "Failed GetOverlappedResult\n");
1630 CloseHandle(ov
.hEvent
);
1637 CloseHandle(ov
.hEvent
);
1640 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1649 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
1651 CharDriverState
*chr
;
1654 chr
= qemu_mallocz(sizeof(CharDriverState
));
1655 s
= qemu_mallocz(sizeof(WinCharState
));
1657 chr
->chr_write
= win_chr_write
;
1658 chr
->chr_close
= win_chr_close
;
1660 if (win_chr_pipe_init(chr
, filename
) < 0) {
1665 qemu_chr_reset(chr
);
1669 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1671 CharDriverState
*chr
;
1674 chr
= qemu_mallocz(sizeof(CharDriverState
));
1675 s
= qemu_mallocz(sizeof(WinCharState
));
1678 chr
->chr_write
= win_chr_write
;
1679 qemu_chr_reset(chr
);
1683 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
1685 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1688 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
1692 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1693 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1694 if (fd_out
== INVALID_HANDLE_VALUE
)
1697 return qemu_chr_open_win_file(fd_out
);
1699 #endif /* !_WIN32 */
1701 /***********************************************************/
1702 /* UDP Net console */
1706 struct sockaddr_in daddr
;
1713 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1715 NetCharDriver
*s
= chr
->opaque
;
1717 return sendto(s
->fd
, (const void *)buf
, len
, 0,
1718 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1721 static int udp_chr_read_poll(void *opaque
)
1723 CharDriverState
*chr
= opaque
;
1724 NetCharDriver
*s
= chr
->opaque
;
1726 s
->max_size
= qemu_chr_can_read(chr
);
1728 /* If there were any stray characters in the queue process them
1731 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1732 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1734 s
->max_size
= qemu_chr_can_read(chr
);
1739 static void udp_chr_read(void *opaque
)
1741 CharDriverState
*chr
= opaque
;
1742 NetCharDriver
*s
= chr
->opaque
;
1744 if (s
->max_size
== 0)
1746 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1747 s
->bufptr
= s
->bufcnt
;
1752 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1753 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1755 s
->max_size
= qemu_chr_can_read(chr
);
1759 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1761 NetCharDriver
*s
= chr
->opaque
;
1764 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1765 udp_chr_read
, NULL
, chr
);
1769 static void udp_chr_close(CharDriverState
*chr
)
1771 NetCharDriver
*s
= chr
->opaque
;
1773 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1779 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1781 CharDriverState
*chr
= NULL
;
1782 NetCharDriver
*s
= NULL
;
1784 struct sockaddr_in saddr
;
1786 chr
= qemu_mallocz(sizeof(CharDriverState
));
1787 s
= qemu_mallocz(sizeof(NetCharDriver
));
1789 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1791 perror("socket(PF_INET, SOCK_DGRAM)");
1795 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1796 printf("Could not parse: %s\n", def
);
1800 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1810 chr
->chr_write
= udp_chr_write
;
1811 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1812 chr
->chr_close
= udp_chr_close
;
1825 /***********************************************************/
1826 /* TCP Net console */
1837 static void tcp_chr_accept(void *opaque
);
1839 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1841 TCPCharDriver
*s
= chr
->opaque
;
1843 return send_all(s
->fd
, buf
, len
);
1845 /* XXX: indicate an error ? */
1850 static int tcp_chr_read_poll(void *opaque
)
1852 CharDriverState
*chr
= opaque
;
1853 TCPCharDriver
*s
= chr
->opaque
;
1856 s
->max_size
= qemu_chr_can_read(chr
);
1861 #define IAC_BREAK 243
1862 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1864 uint8_t *buf
, int *size
)
1866 /* Handle any telnet client's basic IAC options to satisfy char by
1867 * char mode with no echo. All IAC options will be removed from
1868 * the buf and the do_telnetopt variable will be used to track the
1869 * state of the width of the IAC information.
1871 * IAC commands come in sets of 3 bytes with the exception of the
1872 * "IAC BREAK" command and the double IAC.
1878 for (i
= 0; i
< *size
; i
++) {
1879 if (s
->do_telnetopt
> 1) {
1880 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1881 /* Double IAC means send an IAC */
1885 s
->do_telnetopt
= 1;
1887 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1888 /* Handle IAC break commands by sending a serial break */
1889 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1894 if (s
->do_telnetopt
>= 4) {
1895 s
->do_telnetopt
= 1;
1898 if ((unsigned char)buf
[i
] == IAC
) {
1899 s
->do_telnetopt
= 2;
1910 static void tcp_chr_read(void *opaque
)
1912 CharDriverState
*chr
= opaque
;
1913 TCPCharDriver
*s
= chr
->opaque
;
1917 if (!s
->connected
|| s
->max_size
<= 0)
1920 if (len
> s
->max_size
)
1922 size
= recv(s
->fd
, (void *)buf
, len
, 0);
1924 /* connection closed */
1926 if (s
->listen_fd
>= 0) {
1927 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
1929 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1932 } else if (size
> 0) {
1933 if (s
->do_telnetopt
)
1934 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
1936 qemu_chr_read(chr
, buf
, size
);
1940 static void tcp_chr_connect(void *opaque
)
1942 CharDriverState
*chr
= opaque
;
1943 TCPCharDriver
*s
= chr
->opaque
;
1946 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
1947 tcp_chr_read
, NULL
, chr
);
1948 qemu_chr_reset(chr
);
1951 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1952 static void tcp_chr_telnet_init(int fd
)
1955 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1956 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1957 send(fd
, (char *)buf
, 3, 0);
1958 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1959 send(fd
, (char *)buf
, 3, 0);
1960 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1961 send(fd
, (char *)buf
, 3, 0);
1962 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1963 send(fd
, (char *)buf
, 3, 0);
1966 static void socket_set_nodelay(int fd
)
1969 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1972 static void tcp_chr_accept(void *opaque
)
1974 CharDriverState
*chr
= opaque
;
1975 TCPCharDriver
*s
= chr
->opaque
;
1976 struct sockaddr_in saddr
;
1978 struct sockaddr_un uaddr
;
1980 struct sockaddr
*addr
;
1987 len
= sizeof(uaddr
);
1988 addr
= (struct sockaddr
*)&uaddr
;
1992 len
= sizeof(saddr
);
1993 addr
= (struct sockaddr
*)&saddr
;
1995 fd
= accept(s
->listen_fd
, addr
, &len
);
1996 if (fd
< 0 && errno
!= EINTR
) {
1998 } else if (fd
>= 0) {
1999 if (s
->do_telnetopt
)
2000 tcp_chr_telnet_init(fd
);
2004 socket_set_nonblock(fd
);
2006 socket_set_nodelay(fd
);
2008 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2009 tcp_chr_connect(chr
);
2012 static void tcp_chr_close(CharDriverState
*chr
)
2014 TCPCharDriver
*s
= chr
->opaque
;
2016 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2019 if (s
->listen_fd
>= 0) {
2020 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2021 closesocket(s
->listen_fd
);
2026 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2030 CharDriverState
*chr
= NULL
;
2031 TCPCharDriver
*s
= NULL
;
2032 int fd
= -1, offset
= 0;
2034 int is_waitconnect
= 1;
2039 while((ptr
= strchr(ptr
,','))) {
2041 if (!strncmp(ptr
,"server",6)) {
2043 } else if (!strncmp(ptr
,"nowait",6)) {
2045 } else if (!strncmp(ptr
,"nodelay",6)) {
2047 } else if (!strncmp(ptr
,"to=",3)) {
2048 /* nothing, inet_listen() parses this one */;
2049 } else if (!strncmp(ptr
,"ipv4",4)) {
2050 /* nothing, inet_connect() and inet_listen() parse this one */;
2051 } else if (!strncmp(ptr
,"ipv6",4)) {
2052 /* nothing, inet_connect() and inet_listen() parse this one */;
2054 printf("Unknown option: %s\n", ptr
);
2061 chr
= qemu_mallocz(sizeof(CharDriverState
));
2062 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2065 chr
->filename
= qemu_malloc(256);
2067 pstrcpy(chr
->filename
, 256, "unix:");
2068 } else if (is_telnet
) {
2069 pstrcpy(chr
->filename
, 256, "telnet:");
2071 pstrcpy(chr
->filename
, 256, "tcp:");
2073 offset
= strlen(chr
->filename
);
2077 fd
= unix_listen(host_str
, chr
->filename
+ offset
, 256 - offset
);
2079 fd
= unix_connect(host_str
);
2083 fd
= inet_listen(host_str
, chr
->filename
+ offset
, 256 - offset
,
2086 fd
= inet_connect(host_str
, SOCK_STREAM
);
2092 if (!is_waitconnect
)
2093 socket_set_nonblock(fd
);
2098 s
->is_unix
= is_unix
;
2099 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2102 chr
->chr_write
= tcp_chr_write
;
2103 chr
->chr_close
= tcp_chr_close
;
2107 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2109 s
->do_telnetopt
= 1;
2113 socket_set_nodelay(fd
);
2114 tcp_chr_connect(chr
);
2117 if (is_listen
&& is_waitconnect
) {
2118 printf("QEMU waiting for connection on: %s\n",
2119 chr
->filename
? chr
->filename
: host_str
);
2120 tcp_chr_accept(chr
);
2121 socket_set_nonblock(s
->listen_fd
);
2133 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2136 CharDriverState
*chr
;
2138 if (!strcmp(filename
, "vc")) {
2139 chr
= text_console_init(0);
2141 if (strstart(filename
, "vc:", &p
)) {
2142 chr
= text_console_init(p
);
2144 if (!strcmp(filename
, "null")) {
2145 chr
= qemu_chr_open_null();
2147 if (strstart(filename
, "tcp:", &p
)) {
2148 chr
= qemu_chr_open_tcp(p
, 0, 0);
2150 if (strstart(filename
, "telnet:", &p
)) {
2151 chr
= qemu_chr_open_tcp(p
, 1, 0);
2153 if (strstart(filename
, "udp:", &p
)) {
2154 chr
= qemu_chr_open_udp(p
);
2156 if (strstart(filename
, "mon:", &p
)) {
2157 chr
= qemu_chr_open(label
, p
, NULL
);
2159 chr
= qemu_chr_open_mux(chr
);
2160 monitor_init(chr
, MONITOR_USE_READLINE
);
2162 printf("Unable to open driver: %s\n", p
);
2164 } else if (!strcmp(filename
, "msmouse")) {
2165 chr
= qemu_chr_open_msmouse();
2168 if (strstart(filename
, "unix:", &p
)) {
2169 chr
= qemu_chr_open_tcp(p
, 0, 1);
2170 } else if (strstart(filename
, "file:", &p
)) {
2171 chr
= qemu_chr_open_file_out(p
);
2172 } else if (strstart(filename
, "pipe:", &p
)) {
2173 chr
= qemu_chr_open_pipe(p
);
2174 } else if (!strcmp(filename
, "pty")) {
2175 chr
= qemu_chr_open_pty();
2176 } else if (!strcmp(filename
, "stdio")) {
2177 chr
= qemu_chr_open_stdio();
2179 #if defined(__linux__)
2180 if (strstart(filename
, "/dev/parport", NULL
)) {
2181 chr
= qemu_chr_open_pp(filename
);
2183 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2184 if (strstart(filename
, "/dev/ppi", NULL
)) {
2185 chr
= qemu_chr_open_pp(filename
);
2188 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2189 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2190 if (strstart(filename
, "/dev/", NULL
)) {
2191 chr
= qemu_chr_open_tty(filename
);
2195 if (strstart(filename
, "COM", NULL
)) {
2196 chr
= qemu_chr_open_win(filename
);
2198 if (strstart(filename
, "pipe:", &p
)) {
2199 chr
= qemu_chr_open_win_pipe(p
);
2201 if (strstart(filename
, "con:", NULL
)) {
2202 chr
= qemu_chr_open_win_con(filename
);
2204 if (strstart(filename
, "file:", &p
)) {
2205 chr
= qemu_chr_open_win_file_out(p
);
2208 #ifdef CONFIG_BRLAPI
2209 if (!strcmp(filename
, "braille")) {
2210 chr
= chr_baum_init();
2219 chr
->filename
= qemu_strdup(filename
);
2221 chr
->label
= qemu_strdup(label
);
2222 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2227 void qemu_chr_close(CharDriverState
*chr
)
2229 TAILQ_REMOVE(&chardevs
, chr
, next
);
2231 chr
->chr_close(chr
);
2232 qemu_free(chr
->filename
);
2233 qemu_free(chr
->label
);
2237 void qemu_chr_info(Monitor
*mon
)
2239 CharDriverState
*chr
;
2241 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2242 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);