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 QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
110 QTAILQ_HEAD_INITIALIZER(chardevs
);
112 static void qemu_chr_event(CharDriverState
*s
, int event
)
116 s
->chr_event(s
->handler_opaque
, event
);
119 static void qemu_chr_reset_bh(void *opaque
)
121 CharDriverState
*s
= opaque
;
123 if (s
->initial_reset_issued
) {
124 qemu_chr_event(s
, CHR_EVENT_OPENED
);
126 s
->initial_reset_issued
= true;
128 qemu_bh_delete(s
->bh
);
132 void qemu_chr_reset(CharDriverState
*s
)
135 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
136 qemu_bh_schedule(s
->bh
);
140 void qemu_chr_initial_reset(void)
142 CharDriverState
*chr
;
144 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
149 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
151 return s
->chr_write(s
, buf
, len
);
154 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
158 return s
->chr_ioctl(s
, cmd
, arg
);
161 int qemu_chr_can_read(CharDriverState
*s
)
163 if (!s
->chr_can_read
)
165 return s
->chr_can_read(s
->handler_opaque
);
168 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
170 s
->chr_read(s
->handler_opaque
, buf
, len
);
173 int qemu_chr_get_msgfd(CharDriverState
*s
)
175 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
178 void qemu_chr_accept_input(CharDriverState
*s
)
180 if (s
->chr_accept_input
)
181 s
->chr_accept_input(s
);
184 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
189 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
190 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
194 void qemu_chr_send_event(CharDriverState
*s
, int event
)
196 if (s
->chr_send_event
)
197 s
->chr_send_event(s
, event
);
200 void qemu_chr_add_handlers(CharDriverState
*s
,
201 IOCanRWHandler
*fd_can_read
,
202 IOReadHandler
*fd_read
,
203 IOEventHandler
*fd_event
,
206 s
->chr_can_read
= fd_can_read
;
207 s
->chr_read
= fd_read
;
208 s
->chr_event
= fd_event
;
209 s
->handler_opaque
= opaque
;
210 if (s
->chr_update_read_handler
)
211 s
->chr_update_read_handler(s
);
214 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
219 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
221 CharDriverState
*chr
;
223 chr
= qemu_mallocz(sizeof(CharDriverState
));
224 chr
->chr_write
= null_chr_write
;
228 /* MUX driver for serial I/O splitting */
230 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
231 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
233 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
234 IOReadHandler
*chr_read
[MAX_MUX
];
235 IOEventHandler
*chr_event
[MAX_MUX
];
236 void *ext_opaque
[MAX_MUX
];
237 CharDriverState
*drv
;
242 /* Intermediate input buffer allows to catch escape sequences even if the
243 currently active device is not accepting any input - but only until it
245 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
250 int64_t timestamps_start
;
254 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
256 MuxDriver
*d
= chr
->opaque
;
258 if (!d
->timestamps
) {
259 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
264 for (i
= 0; i
< len
; i
++) {
270 ti
= qemu_get_clock(rt_clock
);
271 if (d
->timestamps_start
== -1)
272 d
->timestamps_start
= ti
;
273 ti
-= d
->timestamps_start
;
275 snprintf(buf1
, sizeof(buf1
),
276 "[%02d:%02d:%02d.%03d] ",
281 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
284 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
285 if (buf
[i
] == '\n') {
293 static const char * const mux_help
[] = {
294 "% h print this help\n\r",
295 "% x exit emulator\n\r",
296 "% s save disk data back to file (if -snapshot)\n\r",
297 "% t toggle console timestamps\n\r"
298 "% b send break (magic sysrq)\n\r",
299 "% c switch between console and monitor\n\r",
304 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
305 static void mux_print_help(CharDriverState
*chr
)
308 char ebuf
[15] = "Escape-Char";
309 char cbuf
[50] = "\n\r";
311 if (term_escape_char
> 0 && term_escape_char
< 26) {
312 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
313 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
315 snprintf(cbuf
, sizeof(cbuf
),
316 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
319 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
320 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
321 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
322 if (mux_help
[i
][j
] == '%')
323 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
325 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
330 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
332 if (d
->chr_event
[mux_nr
])
333 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
336 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
338 if (d
->term_got_escape
) {
339 d
->term_got_escape
= 0;
340 if (ch
== term_escape_char
)
349 const char *term
= "QEMU: Terminated\n\r";
350 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
357 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
358 bdrv_commit(dinfo
->bdrv
);
363 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
366 /* Switch to the next registered device */
367 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
369 if (d
->focus
>= d
->mux_cnt
)
371 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
374 d
->timestamps
= !d
->timestamps
;
375 d
->timestamps_start
= -1;
379 } else if (ch
== term_escape_char
) {
380 d
->term_got_escape
= 1;
388 static void mux_chr_accept_input(CharDriverState
*chr
)
390 MuxDriver
*d
= chr
->opaque
;
393 while (d
->prod
[m
] != d
->cons
[m
] &&
394 d
->chr_can_read
[m
] &&
395 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
396 d
->chr_read
[m
](d
->ext_opaque
[m
],
397 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
401 static int mux_chr_can_read(void *opaque
)
403 CharDriverState
*chr
= opaque
;
404 MuxDriver
*d
= chr
->opaque
;
407 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
409 if (d
->chr_can_read
[m
])
410 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
414 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
416 CharDriverState
*chr
= opaque
;
417 MuxDriver
*d
= chr
->opaque
;
421 mux_chr_accept_input (opaque
);
423 for(i
= 0; i
< size
; i
++)
424 if (mux_proc_byte(chr
, d
, buf
[i
])) {
425 if (d
->prod
[m
] == d
->cons
[m
] &&
426 d
->chr_can_read
[m
] &&
427 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
428 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
430 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
434 static void mux_chr_event(void *opaque
, int event
)
436 CharDriverState
*chr
= opaque
;
437 MuxDriver
*d
= chr
->opaque
;
440 /* Send the event to all registered listeners */
441 for (i
= 0; i
< d
->mux_cnt
; i
++)
442 mux_chr_send_event(d
, i
, event
);
445 static void mux_chr_update_read_handler(CharDriverState
*chr
)
447 MuxDriver
*d
= chr
->opaque
;
449 if (d
->mux_cnt
>= MAX_MUX
) {
450 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
453 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
454 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
455 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
456 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
457 /* Fix up the real driver with mux routines */
458 if (d
->mux_cnt
== 0) {
459 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
462 if (d
->focus
!= -1) {
463 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
465 d
->focus
= d
->mux_cnt
;
467 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
470 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
472 CharDriverState
*chr
;
475 chr
= qemu_mallocz(sizeof(CharDriverState
));
476 d
= qemu_mallocz(sizeof(MuxDriver
));
481 chr
->chr_write
= mux_chr_write
;
482 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
483 chr
->chr_accept_input
= mux_chr_accept_input
;
489 int send_all(int fd
, const void *buf
, int len1
)
495 ret
= send(fd
, buf
, len
, 0);
497 errno
= WSAGetLastError();
498 if (errno
!= WSAEWOULDBLOCK
) {
501 } else if (ret
== 0) {
513 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
519 ret
= write(fd
, buf
, len
);
521 if (errno
!= EINTR
&& errno
!= EAGAIN
)
523 } else if (ret
== 0) {
533 int send_all(int fd
, const void *buf
, int len1
)
535 return unix_write(fd
, buf
, len1
);
546 #define STDIO_MAX_CLIENTS 1
547 static int stdio_nb_clients
= 0;
549 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
551 FDCharDriver
*s
= chr
->opaque
;
552 return send_all(s
->fd_out
, buf
, len
);
555 static int fd_chr_read_poll(void *opaque
)
557 CharDriverState
*chr
= opaque
;
558 FDCharDriver
*s
= chr
->opaque
;
560 s
->max_size
= qemu_chr_can_read(chr
);
564 static void fd_chr_read(void *opaque
)
566 CharDriverState
*chr
= opaque
;
567 FDCharDriver
*s
= chr
->opaque
;
572 if (len
> s
->max_size
)
576 size
= read(s
->fd_in
, buf
, len
);
578 /* FD has been closed. Remove it from the active list. */
579 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
580 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
584 qemu_chr_read(chr
, buf
, size
);
588 static void fd_chr_update_read_handler(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
, fd_chr_read_poll
,
596 fd_chr_read
, NULL
, chr
);
601 static void fd_chr_close(struct CharDriverState
*chr
)
603 FDCharDriver
*s
= chr
->opaque
;
606 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
608 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
613 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
616 /* open a character device to a unix fd */
617 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
619 CharDriverState
*chr
;
622 chr
= qemu_mallocz(sizeof(CharDriverState
));
623 s
= qemu_mallocz(sizeof(FDCharDriver
));
627 chr
->chr_write
= fd_chr_write
;
628 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
629 chr
->chr_close
= fd_chr_close
;
636 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
640 TFR(fd_out
= open(qemu_opt_get(opts
, "path"),
641 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
644 return qemu_chr_open_fd(-1, fd_out
);
647 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
650 char filename_in
[256], filename_out
[256];
651 const char *filename
= qemu_opt_get(opts
, "path");
653 if (filename
== NULL
) {
654 fprintf(stderr
, "chardev: pipe: no filename given\n");
658 snprintf(filename_in
, 256, "%s.in", filename
);
659 snprintf(filename_out
, 256, "%s.out", filename
);
660 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
661 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
662 if (fd_in
< 0 || fd_out
< 0) {
667 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
671 return qemu_chr_open_fd(fd_in
, fd_out
);
675 /* for STDIO, we handle the case where several clients use it
678 #define TERM_FIFO_MAX_SIZE 1
680 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
681 static int term_fifo_size
;
683 static int stdio_read_poll(void *opaque
)
685 CharDriverState
*chr
= opaque
;
687 /* try to flush the queue if needed */
688 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
689 qemu_chr_read(chr
, term_fifo
, 1);
692 /* see if we can absorb more chars */
693 if (term_fifo_size
== 0)
699 static void stdio_read(void *opaque
)
703 CharDriverState
*chr
= opaque
;
705 size
= read(0, buf
, 1);
707 /* stdin has been closed. Remove it from the active list. */
708 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
709 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
713 if (qemu_chr_can_read(chr
) > 0) {
714 qemu_chr_read(chr
, buf
, 1);
715 } else if (term_fifo_size
== 0) {
716 term_fifo
[term_fifo_size
++] = buf
[0];
721 /* init terminal so that we can grab keys */
722 static struct termios oldtty
;
723 static int old_fd0_flags
;
724 static int term_atexit_done
;
726 static void term_exit(void)
728 tcsetattr (0, TCSANOW
, &oldtty
);
729 fcntl(0, F_SETFL
, old_fd0_flags
);
732 static void term_init(void)
738 old_fd0_flags
= fcntl(0, F_GETFL
);
740 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
741 |INLCR
|IGNCR
|ICRNL
|IXON
);
742 tty
.c_oflag
|= OPOST
;
743 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
744 /* if graphical mode, we allow Ctrl-C handling */
745 if (display_type
== DT_NOGRAPHIC
)
746 tty
.c_lflag
&= ~ISIG
;
747 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
752 tcsetattr (0, TCSANOW
, &tty
);
754 if (!term_atexit_done
++)
757 fcntl(0, F_SETFL
, O_NONBLOCK
);
760 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
764 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
768 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
770 CharDriverState
*chr
;
772 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
774 chr
= qemu_chr_open_fd(0, 1);
775 chr
->chr_close
= qemu_chr_close_stdio
;
776 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
784 /* Once Solaris has openpty(), this is going to be removed. */
785 static int openpty(int *amaster
, int *aslave
, char *name
,
786 struct termios
*termp
, struct winsize
*winp
)
789 int mfd
= -1, sfd
= -1;
791 *amaster
= *aslave
= -1;
793 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
797 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
800 if ((slave
= ptsname(mfd
)) == NULL
)
803 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
806 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
807 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
815 ioctl(sfd
, TIOCSWINSZ
, winp
);
826 static void cfmakeraw (struct termios
*termios_p
)
828 termios_p
->c_iflag
&=
829 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
830 termios_p
->c_oflag
&= ~OPOST
;
831 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
832 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
833 termios_p
->c_cflag
|= CS8
;
835 termios_p
->c_cc
[VMIN
] = 0;
836 termios_p
->c_cc
[VTIME
] = 0;
840 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
841 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
851 static void pty_chr_update_read_handler(CharDriverState
*chr
);
852 static void pty_chr_state(CharDriverState
*chr
, int connected
);
854 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
856 PtyCharDriver
*s
= chr
->opaque
;
859 /* guest sends data, check for (re-)connect */
860 pty_chr_update_read_handler(chr
);
863 return send_all(s
->fd
, buf
, len
);
866 static int pty_chr_read_poll(void *opaque
)
868 CharDriverState
*chr
= opaque
;
869 PtyCharDriver
*s
= chr
->opaque
;
871 s
->read_bytes
= qemu_chr_can_read(chr
);
872 return s
->read_bytes
;
875 static void pty_chr_read(void *opaque
)
877 CharDriverState
*chr
= opaque
;
878 PtyCharDriver
*s
= chr
->opaque
;
883 if (len
> s
->read_bytes
)
887 size
= read(s
->fd
, buf
, len
);
888 if ((size
== -1 && errno
== EIO
) ||
890 pty_chr_state(chr
, 0);
894 pty_chr_state(chr
, 1);
895 qemu_chr_read(chr
, buf
, size
);
899 static void pty_chr_update_read_handler(CharDriverState
*chr
)
901 PtyCharDriver
*s
= chr
->opaque
;
903 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
904 pty_chr_read
, NULL
, chr
);
907 * Short timeout here: just need wait long enougth that qemu makes
908 * it through the poll loop once. When reconnected we want a
909 * short timeout so we notice it almost instantly. Otherwise
910 * read() gives us -EIO instantly, making pty_chr_state() reset the
911 * timeout to the normal (much longer) poll interval before the
914 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
917 static void pty_chr_state(CharDriverState
*chr
, int connected
)
919 PtyCharDriver
*s
= chr
->opaque
;
922 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
925 /* (re-)connect poll interval for idle guests: once per second.
926 * We check more frequently in case the guests sends data to
927 * the virtual device linked to our pty. */
928 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
936 static void pty_chr_timer(void *opaque
)
938 struct CharDriverState
*chr
= opaque
;
939 PtyCharDriver
*s
= chr
->opaque
;
944 /* If we arrive here without polling being cleared due
945 * read returning -EIO, then we are (re-)connected */
946 pty_chr_state(chr
, 1);
951 pty_chr_update_read_handler(chr
);
954 static void pty_chr_close(struct CharDriverState
*chr
)
956 PtyCharDriver
*s
= chr
->opaque
;
958 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
960 qemu_del_timer(s
->timer
);
961 qemu_free_timer(s
->timer
);
963 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
966 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
968 CharDriverState
*chr
;
972 #if defined(__OpenBSD__) || defined(__DragonFly__)
973 char pty_name
[PATH_MAX
];
974 #define q_ptsname(x) pty_name
976 char *pty_name
= NULL
;
977 #define q_ptsname(x) ptsname(x)
980 chr
= qemu_mallocz(sizeof(CharDriverState
));
981 s
= qemu_mallocz(sizeof(PtyCharDriver
));
983 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
987 /* Set raw attributes on the pty. */
988 tcgetattr(slave_fd
, &tty
);
990 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
993 len
= strlen(q_ptsname(s
->fd
)) + 5;
994 chr
->filename
= qemu_malloc(len
);
995 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
996 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
997 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
1000 chr
->chr_write
= pty_chr_write
;
1001 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1002 chr
->chr_close
= pty_chr_close
;
1004 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
1009 static void tty_serial_init(int fd
, int speed
,
1010 int parity
, int data_bits
, int stop_bits
)
1016 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1017 speed
, parity
, data_bits
, stop_bits
);
1019 tcgetattr (fd
, &tty
);
1022 if (speed
<= 50 * MARGIN
)
1024 else if (speed
<= 75 * MARGIN
)
1026 else if (speed
<= 300 * MARGIN
)
1028 else if (speed
<= 600 * MARGIN
)
1030 else if (speed
<= 1200 * MARGIN
)
1032 else if (speed
<= 2400 * MARGIN
)
1034 else if (speed
<= 4800 * MARGIN
)
1036 else if (speed
<= 9600 * MARGIN
)
1038 else if (speed
<= 19200 * MARGIN
)
1040 else if (speed
<= 38400 * MARGIN
)
1042 else if (speed
<= 57600 * MARGIN
)
1044 else if (speed
<= 115200 * MARGIN
)
1049 cfsetispeed(&tty
, spd
);
1050 cfsetospeed(&tty
, spd
);
1052 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1053 |INLCR
|IGNCR
|ICRNL
|IXON
);
1054 tty
.c_oflag
|= OPOST
;
1055 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1056 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1077 tty
.c_cflag
|= PARENB
;
1080 tty
.c_cflag
|= PARENB
| PARODD
;
1084 tty
.c_cflag
|= CSTOPB
;
1086 tcsetattr (fd
, TCSANOW
, &tty
);
1089 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1091 FDCharDriver
*s
= chr
->opaque
;
1094 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1096 QEMUSerialSetParams
*ssp
= arg
;
1097 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1098 ssp
->data_bits
, ssp
->stop_bits
);
1101 case CHR_IOCTL_SERIAL_SET_BREAK
:
1103 int enable
= *(int *)arg
;
1105 tcsendbreak(s
->fd_in
, 1);
1108 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1111 int *targ
= (int *)arg
;
1112 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1114 if (sarg
& TIOCM_CTS
)
1115 *targ
|= CHR_TIOCM_CTS
;
1116 if (sarg
& TIOCM_CAR
)
1117 *targ
|= CHR_TIOCM_CAR
;
1118 if (sarg
& TIOCM_DSR
)
1119 *targ
|= CHR_TIOCM_DSR
;
1120 if (sarg
& TIOCM_RI
)
1121 *targ
|= CHR_TIOCM_RI
;
1122 if (sarg
& TIOCM_DTR
)
1123 *targ
|= CHR_TIOCM_DTR
;
1124 if (sarg
& TIOCM_RTS
)
1125 *targ
|= CHR_TIOCM_RTS
;
1128 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1130 int sarg
= *(int *)arg
;
1132 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1133 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1134 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1135 if (sarg
& CHR_TIOCM_CTS
)
1137 if (sarg
& CHR_TIOCM_CAR
)
1139 if (sarg
& CHR_TIOCM_DSR
)
1141 if (sarg
& CHR_TIOCM_RI
)
1143 if (sarg
& CHR_TIOCM_DTR
)
1145 if (sarg
& CHR_TIOCM_RTS
)
1147 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1156 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1158 const char *filename
= qemu_opt_get(opts
, "path");
1159 CharDriverState
*chr
;
1162 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1163 tty_serial_init(fd
, 115200, 'N', 8, 1);
1164 chr
= qemu_chr_open_fd(fd
, fd
);
1169 chr
->chr_ioctl
= tty_serial_ioctl
;
1170 qemu_chr_reset(chr
);
1173 #else /* ! __linux__ && ! __sun__ */
1174 static CharDriverState
*qemu_chr_open_pty(void)
1178 #endif /* __linux__ || __sun__ */
1180 #if defined(__linux__)
1184 } ParallelCharDriver
;
1186 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1188 if (s
->mode
!= mode
) {
1190 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1197 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1199 ParallelCharDriver
*drv
= chr
->opaque
;
1204 case CHR_IOCTL_PP_READ_DATA
:
1205 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1207 *(uint8_t *)arg
= b
;
1209 case CHR_IOCTL_PP_WRITE_DATA
:
1210 b
= *(uint8_t *)arg
;
1211 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1214 case CHR_IOCTL_PP_READ_CONTROL
:
1215 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1217 /* Linux gives only the lowest bits, and no way to know data
1218 direction! For better compatibility set the fixed upper
1220 *(uint8_t *)arg
= b
| 0xc0;
1222 case CHR_IOCTL_PP_WRITE_CONTROL
:
1223 b
= *(uint8_t *)arg
;
1224 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1227 case CHR_IOCTL_PP_READ_STATUS
:
1228 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1230 *(uint8_t *)arg
= b
;
1232 case CHR_IOCTL_PP_DATA_DIR
:
1233 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1236 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1237 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1238 struct ParallelIOArg
*parg
= arg
;
1239 int n
= read(fd
, parg
->buffer
, parg
->count
);
1240 if (n
!= parg
->count
) {
1245 case CHR_IOCTL_PP_EPP_READ
:
1246 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1247 struct ParallelIOArg
*parg
= arg
;
1248 int n
= read(fd
, parg
->buffer
, parg
->count
);
1249 if (n
!= parg
->count
) {
1254 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1255 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1256 struct ParallelIOArg
*parg
= arg
;
1257 int n
= write(fd
, parg
->buffer
, parg
->count
);
1258 if (n
!= parg
->count
) {
1263 case CHR_IOCTL_PP_EPP_WRITE
:
1264 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1265 struct ParallelIOArg
*parg
= arg
;
1266 int n
= write(fd
, parg
->buffer
, parg
->count
);
1267 if (n
!= parg
->count
) {
1278 static void pp_close(CharDriverState
*chr
)
1280 ParallelCharDriver
*drv
= chr
->opaque
;
1283 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1284 ioctl(fd
, PPRELEASE
);
1287 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1290 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1292 const char *filename
= qemu_opt_get(opts
, "path");
1293 CharDriverState
*chr
;
1294 ParallelCharDriver
*drv
;
1297 TFR(fd
= open(filename
, O_RDWR
));
1301 if (ioctl(fd
, PPCLAIM
) < 0) {
1306 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1308 drv
->mode
= IEEE1284_MODE_COMPAT
;
1310 chr
= qemu_mallocz(sizeof(CharDriverState
));
1311 chr
->chr_write
= null_chr_write
;
1312 chr
->chr_ioctl
= pp_ioctl
;
1313 chr
->chr_close
= pp_close
;
1316 qemu_chr_reset(chr
);
1320 #endif /* __linux__ */
1322 #if defined(__FreeBSD__) || defined(__DragonFly__)
1323 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1325 int fd
= (int)chr
->opaque
;
1329 case CHR_IOCTL_PP_READ_DATA
:
1330 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1332 *(uint8_t *)arg
= b
;
1334 case CHR_IOCTL_PP_WRITE_DATA
:
1335 b
= *(uint8_t *)arg
;
1336 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1339 case CHR_IOCTL_PP_READ_CONTROL
:
1340 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1342 *(uint8_t *)arg
= b
;
1344 case CHR_IOCTL_PP_WRITE_CONTROL
:
1345 b
= *(uint8_t *)arg
;
1346 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1349 case CHR_IOCTL_PP_READ_STATUS
:
1350 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1352 *(uint8_t *)arg
= b
;
1360 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1362 const char *filename
= qemu_opt_get(opts
, "path");
1363 CharDriverState
*chr
;
1366 fd
= open(filename
, O_RDWR
);
1370 chr
= qemu_mallocz(sizeof(CharDriverState
));
1371 chr
->opaque
= (void *)fd
;
1372 chr
->chr_write
= null_chr_write
;
1373 chr
->chr_ioctl
= pp_ioctl
;
1382 HANDLE hcom
, hrecv
, hsend
;
1383 OVERLAPPED orecv
, osend
;
1388 #define NSENDBUF 2048
1389 #define NRECVBUF 2048
1390 #define MAXCONNECT 1
1391 #define NTIMEOUT 5000
1393 static int win_chr_poll(void *opaque
);
1394 static int win_chr_pipe_poll(void *opaque
);
1396 static void win_chr_close(CharDriverState
*chr
)
1398 WinCharState
*s
= chr
->opaque
;
1401 CloseHandle(s
->hsend
);
1405 CloseHandle(s
->hrecv
);
1409 CloseHandle(s
->hcom
);
1413 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1415 qemu_del_polling_cb(win_chr_poll
, chr
);
1417 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1420 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1422 WinCharState
*s
= chr
->opaque
;
1424 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1429 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1431 fprintf(stderr
, "Failed CreateEvent\n");
1434 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1436 fprintf(stderr
, "Failed CreateEvent\n");
1440 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1441 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1442 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1443 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1448 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1449 fprintf(stderr
, "Failed SetupComm\n");
1453 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1454 size
= sizeof(COMMCONFIG
);
1455 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1456 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1457 CommConfigDialog(filename
, NULL
, &comcfg
);
1459 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1460 fprintf(stderr
, "Failed SetCommState\n");
1464 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1465 fprintf(stderr
, "Failed SetCommMask\n");
1469 cto
.ReadIntervalTimeout
= MAXDWORD
;
1470 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1471 fprintf(stderr
, "Failed SetCommTimeouts\n");
1475 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1476 fprintf(stderr
, "Failed ClearCommError\n");
1479 qemu_add_polling_cb(win_chr_poll
, chr
);
1487 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1489 WinCharState
*s
= chr
->opaque
;
1490 DWORD len
, ret
, size
, err
;
1493 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1494 s
->osend
.hEvent
= s
->hsend
;
1497 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1499 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1501 err
= GetLastError();
1502 if (err
== ERROR_IO_PENDING
) {
1503 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1521 static int win_chr_read_poll(CharDriverState
*chr
)
1523 WinCharState
*s
= chr
->opaque
;
1525 s
->max_size
= qemu_chr_can_read(chr
);
1529 static void win_chr_readfile(CharDriverState
*chr
)
1531 WinCharState
*s
= chr
->opaque
;
1536 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1537 s
->orecv
.hEvent
= s
->hrecv
;
1538 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1540 err
= GetLastError();
1541 if (err
== ERROR_IO_PENDING
) {
1542 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1547 qemu_chr_read(chr
, buf
, size
);
1551 static void win_chr_read(CharDriverState
*chr
)
1553 WinCharState
*s
= chr
->opaque
;
1555 if (s
->len
> s
->max_size
)
1556 s
->len
= s
->max_size
;
1560 win_chr_readfile(chr
);
1563 static int win_chr_poll(void *opaque
)
1565 CharDriverState
*chr
= opaque
;
1566 WinCharState
*s
= chr
->opaque
;
1570 ClearCommError(s
->hcom
, &comerr
, &status
);
1571 if (status
.cbInQue
> 0) {
1572 s
->len
= status
.cbInQue
;
1573 win_chr_read_poll(chr
);
1580 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1582 const char *filename
= qemu_opt_get(opts
, "path");
1583 CharDriverState
*chr
;
1586 chr
= qemu_mallocz(sizeof(CharDriverState
));
1587 s
= qemu_mallocz(sizeof(WinCharState
));
1589 chr
->chr_write
= win_chr_write
;
1590 chr
->chr_close
= win_chr_close
;
1592 if (win_chr_init(chr
, filename
) < 0) {
1597 qemu_chr_reset(chr
);
1601 static int win_chr_pipe_poll(void *opaque
)
1603 CharDriverState
*chr
= opaque
;
1604 WinCharState
*s
= chr
->opaque
;
1607 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1610 win_chr_read_poll(chr
);
1617 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1619 WinCharState
*s
= chr
->opaque
;
1627 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1629 fprintf(stderr
, "Failed CreateEvent\n");
1632 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1634 fprintf(stderr
, "Failed CreateEvent\n");
1638 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1639 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1640 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1642 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1643 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1644 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1649 ZeroMemory(&ov
, sizeof(ov
));
1650 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1651 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1653 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1657 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1659 fprintf(stderr
, "Failed GetOverlappedResult\n");
1661 CloseHandle(ov
.hEvent
);
1668 CloseHandle(ov
.hEvent
);
1671 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1680 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1682 const char *filename
= qemu_opt_get(opts
, "path");
1683 CharDriverState
*chr
;
1686 chr
= qemu_mallocz(sizeof(CharDriverState
));
1687 s
= qemu_mallocz(sizeof(WinCharState
));
1689 chr
->chr_write
= win_chr_write
;
1690 chr
->chr_close
= win_chr_close
;
1692 if (win_chr_pipe_init(chr
, filename
) < 0) {
1697 qemu_chr_reset(chr
);
1701 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1703 CharDriverState
*chr
;
1706 chr
= qemu_mallocz(sizeof(CharDriverState
));
1707 s
= qemu_mallocz(sizeof(WinCharState
));
1710 chr
->chr_write
= win_chr_write
;
1711 qemu_chr_reset(chr
);
1715 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1717 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1720 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1722 const char *file_out
= qemu_opt_get(opts
, "path");
1725 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1726 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1727 if (fd_out
== INVALID_HANDLE_VALUE
)
1730 return qemu_chr_open_win_file(fd_out
);
1732 #endif /* !_WIN32 */
1734 /***********************************************************/
1735 /* UDP Net console */
1745 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1747 NetCharDriver
*s
= chr
->opaque
;
1749 return send(s
->fd
, (const void *)buf
, len
, 0);
1752 static int udp_chr_read_poll(void *opaque
)
1754 CharDriverState
*chr
= opaque
;
1755 NetCharDriver
*s
= chr
->opaque
;
1757 s
->max_size
= qemu_chr_can_read(chr
);
1759 /* If there were any stray characters in the queue process them
1762 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1763 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1765 s
->max_size
= qemu_chr_can_read(chr
);
1770 static void udp_chr_read(void *opaque
)
1772 CharDriverState
*chr
= opaque
;
1773 NetCharDriver
*s
= chr
->opaque
;
1775 if (s
->max_size
== 0)
1777 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1778 s
->bufptr
= s
->bufcnt
;
1783 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1784 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1786 s
->max_size
= qemu_chr_can_read(chr
);
1790 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1792 NetCharDriver
*s
= chr
->opaque
;
1795 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1796 udp_chr_read
, NULL
, chr
);
1800 static void udp_chr_close(CharDriverState
*chr
)
1802 NetCharDriver
*s
= chr
->opaque
;
1804 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1808 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1811 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1813 CharDriverState
*chr
= NULL
;
1814 NetCharDriver
*s
= NULL
;
1817 chr
= qemu_mallocz(sizeof(CharDriverState
));
1818 s
= qemu_mallocz(sizeof(NetCharDriver
));
1820 fd
= inet_dgram_opts(opts
);
1822 fprintf(stderr
, "inet_dgram_opts failed\n");
1830 chr
->chr_write
= udp_chr_write
;
1831 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1832 chr
->chr_close
= udp_chr_close
;
1845 /***********************************************************/
1846 /* TCP Net console */
1858 static void tcp_chr_accept(void *opaque
);
1860 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1862 TCPCharDriver
*s
= chr
->opaque
;
1864 return send_all(s
->fd
, buf
, len
);
1866 /* XXX: indicate an error ? */
1871 static int tcp_chr_read_poll(void *opaque
)
1873 CharDriverState
*chr
= opaque
;
1874 TCPCharDriver
*s
= chr
->opaque
;
1877 s
->max_size
= qemu_chr_can_read(chr
);
1882 #define IAC_BREAK 243
1883 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1885 uint8_t *buf
, int *size
)
1887 /* Handle any telnet client's basic IAC options to satisfy char by
1888 * char mode with no echo. All IAC options will be removed from
1889 * the buf and the do_telnetopt variable will be used to track the
1890 * state of the width of the IAC information.
1892 * IAC commands come in sets of 3 bytes with the exception of the
1893 * "IAC BREAK" command and the double IAC.
1899 for (i
= 0; i
< *size
; i
++) {
1900 if (s
->do_telnetopt
> 1) {
1901 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1902 /* Double IAC means send an IAC */
1906 s
->do_telnetopt
= 1;
1908 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1909 /* Handle IAC break commands by sending a serial break */
1910 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1915 if (s
->do_telnetopt
>= 4) {
1916 s
->do_telnetopt
= 1;
1919 if ((unsigned char)buf
[i
] == IAC
) {
1920 s
->do_telnetopt
= 2;
1931 static int tcp_get_msgfd(CharDriverState
*chr
)
1933 TCPCharDriver
*s
= chr
->opaque
;
1939 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1941 TCPCharDriver
*s
= chr
->opaque
;
1942 struct cmsghdr
*cmsg
;
1944 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1947 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1948 cmsg
->cmsg_level
!= SOL_SOCKET
||
1949 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1952 fd
= *((int *)CMSG_DATA(cmsg
));
1962 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1964 TCPCharDriver
*s
= chr
->opaque
;
1965 struct msghdr msg
= { NULL
, };
1966 struct iovec iov
[1];
1968 struct cmsghdr cmsg
;
1969 char control
[CMSG_SPACE(sizeof(int))];
1973 iov
[0].iov_base
= buf
;
1974 iov
[0].iov_len
= len
;
1978 msg
.msg_control
= &msg_control
;
1979 msg
.msg_controllen
= sizeof(msg_control
);
1981 ret
= recvmsg(s
->fd
, &msg
, 0);
1982 if (ret
> 0 && s
->is_unix
)
1983 unix_process_msgfd(chr
, &msg
);
1988 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1990 TCPCharDriver
*s
= chr
->opaque
;
1991 return recv(s
->fd
, buf
, len
, 0);
1995 static void tcp_chr_read(void *opaque
)
1997 CharDriverState
*chr
= opaque
;
1998 TCPCharDriver
*s
= chr
->opaque
;
2002 if (!s
->connected
|| s
->max_size
<= 0)
2005 if (len
> s
->max_size
)
2007 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2009 /* connection closed */
2011 if (s
->listen_fd
>= 0) {
2012 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2014 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2017 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2018 } else if (size
> 0) {
2019 if (s
->do_telnetopt
)
2020 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2022 qemu_chr_read(chr
, buf
, size
);
2023 if (s
->msgfd
!= -1) {
2030 static void tcp_chr_connect(void *opaque
)
2032 CharDriverState
*chr
= opaque
;
2033 TCPCharDriver
*s
= chr
->opaque
;
2036 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2037 tcp_chr_read
, NULL
, chr
);
2038 qemu_chr_reset(chr
);
2041 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2042 static void tcp_chr_telnet_init(int fd
)
2045 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2046 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2047 send(fd
, (char *)buf
, 3, 0);
2048 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2049 send(fd
, (char *)buf
, 3, 0);
2050 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2051 send(fd
, (char *)buf
, 3, 0);
2052 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2053 send(fd
, (char *)buf
, 3, 0);
2056 static void socket_set_nodelay(int fd
)
2059 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2062 static void tcp_chr_accept(void *opaque
)
2064 CharDriverState
*chr
= opaque
;
2065 TCPCharDriver
*s
= chr
->opaque
;
2066 struct sockaddr_in saddr
;
2068 struct sockaddr_un uaddr
;
2070 struct sockaddr
*addr
;
2077 len
= sizeof(uaddr
);
2078 addr
= (struct sockaddr
*)&uaddr
;
2082 len
= sizeof(saddr
);
2083 addr
= (struct sockaddr
*)&saddr
;
2085 fd
= accept(s
->listen_fd
, addr
, &len
);
2086 if (fd
< 0 && errno
!= EINTR
) {
2088 } else if (fd
>= 0) {
2089 if (s
->do_telnetopt
)
2090 tcp_chr_telnet_init(fd
);
2094 socket_set_nonblock(fd
);
2096 socket_set_nodelay(fd
);
2098 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2099 tcp_chr_connect(chr
);
2102 static void tcp_chr_close(CharDriverState
*chr
)
2104 TCPCharDriver
*s
= chr
->opaque
;
2106 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2109 if (s
->listen_fd
>= 0) {
2110 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2111 closesocket(s
->listen_fd
);
2114 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2117 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2119 CharDriverState
*chr
= NULL
;
2120 TCPCharDriver
*s
= NULL
;
2128 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2129 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2130 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2131 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2132 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2136 chr
= qemu_mallocz(sizeof(CharDriverState
));
2137 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2141 fd
= unix_listen_opts(opts
);
2143 fd
= unix_connect_opts(opts
);
2147 fd
= inet_listen_opts(opts
, 0);
2149 fd
= inet_connect_opts(opts
);
2155 if (!is_waitconnect
)
2156 socket_set_nonblock(fd
);
2162 s
->is_unix
= is_unix
;
2163 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2166 chr
->chr_write
= tcp_chr_write
;
2167 chr
->chr_close
= tcp_chr_close
;
2168 chr
->get_msgfd
= tcp_get_msgfd
;
2172 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2174 s
->do_telnetopt
= 1;
2179 socket_set_nodelay(fd
);
2180 tcp_chr_connect(chr
);
2183 /* for "info chardev" monitor command */
2184 chr
->filename
= qemu_malloc(256);
2186 snprintf(chr
->filename
, 256, "unix:%s%s",
2187 qemu_opt_get(opts
, "path"),
2188 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2189 } else if (is_telnet
) {
2190 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2191 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2192 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2194 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2195 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2196 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2199 if (is_listen
&& is_waitconnect
) {
2200 printf("QEMU waiting for connection on: %s\n",
2202 tcp_chr_accept(chr
);
2203 socket_set_nonblock(s
->listen_fd
);
2215 static QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2217 char host
[65], port
[33], width
[8], height
[8];
2222 opts
= qemu_opts_create(&qemu_chardev_opts
, label
, 1);
2226 if (strstart(filename
, "mon:", &p
)) {
2228 qemu_opt_set(opts
, "mux", "on");
2231 if (strcmp(filename
, "null") == 0 ||
2232 strcmp(filename
, "pty") == 0 ||
2233 strcmp(filename
, "msmouse") == 0 ||
2234 strcmp(filename
, "braille") == 0 ||
2235 strcmp(filename
, "stdio") == 0) {
2236 qemu_opt_set(opts
, "backend", filename
);
2239 if (strstart(filename
, "vc", &p
)) {
2240 qemu_opt_set(opts
, "backend", "vc");
2242 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2244 qemu_opt_set(opts
, "width", width
);
2245 qemu_opt_set(opts
, "height", height
);
2246 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2248 qemu_opt_set(opts
, "cols", width
);
2249 qemu_opt_set(opts
, "rows", height
);
2256 if (strcmp(filename
, "con:") == 0) {
2257 qemu_opt_set(opts
, "backend", "console");
2260 if (strstart(filename
, "COM", NULL
)) {
2261 qemu_opt_set(opts
, "backend", "serial");
2262 qemu_opt_set(opts
, "path", filename
);
2265 if (strstart(filename
, "file:", &p
)) {
2266 qemu_opt_set(opts
, "backend", "file");
2267 qemu_opt_set(opts
, "path", p
);
2270 if (strstart(filename
, "pipe:", &p
)) {
2271 qemu_opt_set(opts
, "backend", "pipe");
2272 qemu_opt_set(opts
, "path", p
);
2275 if (strstart(filename
, "tcp:", &p
) ||
2276 strstart(filename
, "telnet:", &p
)) {
2277 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2279 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2282 qemu_opt_set(opts
, "backend", "socket");
2283 qemu_opt_set(opts
, "host", host
);
2284 qemu_opt_set(opts
, "port", port
);
2285 if (p
[pos
] == ',') {
2286 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2289 if (strstart(filename
, "telnet:", &p
))
2290 qemu_opt_set(opts
, "telnet", "on");
2293 if (strstart(filename
, "udp:", &p
)) {
2294 qemu_opt_set(opts
, "backend", "udp");
2295 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2297 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2298 fprintf(stderr
, "udp #1\n");
2302 qemu_opt_set(opts
, "host", host
);
2303 qemu_opt_set(opts
, "port", port
);
2304 if (p
[pos
] == '@') {
2306 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2308 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2309 fprintf(stderr
, "udp #2\n");
2313 qemu_opt_set(opts
, "localaddr", host
);
2314 qemu_opt_set(opts
, "localport", port
);
2318 if (strstart(filename
, "unix:", &p
)) {
2319 qemu_opt_set(opts
, "backend", "socket");
2320 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2324 if (strstart(filename
, "/dev/parport", NULL
) ||
2325 strstart(filename
, "/dev/ppi", NULL
)) {
2326 qemu_opt_set(opts
, "backend", "parport");
2327 qemu_opt_set(opts
, "path", filename
);
2330 if (strstart(filename
, "/dev/", NULL
)) {
2331 qemu_opt_set(opts
, "backend", "tty");
2332 qemu_opt_set(opts
, "path", filename
);
2337 fprintf(stderr
, "%s: fail on \"%s\"\n", __FUNCTION__
, filename
);
2338 qemu_opts_del(opts
);
2342 static const struct {
2344 CharDriverState
*(*open
)(QemuOpts
*opts
);
2345 } backend_table
[] = {
2346 { .name
= "null", .open
= qemu_chr_open_null
},
2347 { .name
= "socket", .open
= qemu_chr_open_socket
},
2348 { .name
= "udp", .open
= qemu_chr_open_udp
},
2349 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2350 { .name
= "vc", .open
= text_console_init
},
2352 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2353 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2354 { .name
= "console", .open
= qemu_chr_open_win_con
},
2355 { .name
= "serial", .open
= qemu_chr_open_win
},
2357 { .name
= "file", .open
= qemu_chr_open_file_out
},
2358 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2359 { .name
= "pty", .open
= qemu_chr_open_pty
},
2360 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2362 #ifdef CONFIG_BRLAPI
2363 { .name
= "braille", .open
= chr_baum_init
},
2365 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2366 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2367 { .name
= "tty", .open
= qemu_chr_open_tty
},
2369 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2370 { .name
= "parport", .open
= qemu_chr_open_pp
},
2374 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2375 void (*init
)(struct CharDriverState
*s
))
2377 CharDriverState
*chr
;
2380 if (qemu_opts_id(opts
) == NULL
) {
2381 fprintf(stderr
, "chardev: no id specified\n");
2385 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2386 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2389 if (i
== ARRAY_SIZE(backend_table
)) {
2390 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2391 qemu_opt_get(opts
, "backend"));
2395 chr
= backend_table
[i
].open(opts
);
2397 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2398 qemu_opt_get(opts
, "backend"));
2403 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2405 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2407 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2408 CharDriverState
*base
= chr
;
2409 int len
= strlen(qemu_opts_id(opts
)) + 6;
2410 base
->label
= qemu_malloc(len
);
2411 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2412 chr
= qemu_chr_open_mux(base
);
2413 chr
->filename
= base
->filename
;
2414 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2416 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2420 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2423 CharDriverState
*chr
;
2426 if (strstart(filename
, "chardev:", &p
)) {
2427 return qemu_chr_find(p
);
2430 opts
= qemu_chr_parse_compat(label
, filename
);
2434 chr
= qemu_chr_open_opts(opts
, init
);
2435 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2436 monitor_init(chr
, MONITOR_USE_READLINE
);
2441 void qemu_chr_close(CharDriverState
*chr
)
2443 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2445 chr
->chr_close(chr
);
2446 qemu_free(chr
->filename
);
2447 qemu_free(chr
->label
);
2451 void qemu_chr_info(Monitor
*mon
)
2453 CharDriverState
*chr
;
2455 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2456 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);
2460 CharDriverState
*qemu_chr_find(const char *name
)
2462 CharDriverState
*chr
;
2464 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2465 if (strcmp(chr
->label
, name
) != 0)