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>
54 #include <arpa/inet.h>
57 #include <sys/select.h>
60 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
62 #include <dev/ppbus/ppi.h>
63 #include <dev/ppbus/ppbconf.h>
64 #if defined(__GLIBC__)
67 #elif defined(__DragonFly__)
69 #include <dev/misc/ppi/ppi.h>
70 #include <bus/ppbus/ppbconf.h>
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
99 #include "qemu_socket.h"
101 #define READ_BUF_LEN 4096
103 /***********************************************************/
104 /* character device */
106 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
107 QTAILQ_HEAD_INITIALIZER(chardevs
);
109 static void qemu_chr_event(CharDriverState
*s
, int event
)
113 s
->chr_event(s
->handler_opaque
, event
);
116 static void qemu_chr_generic_open_bh(void *opaque
)
118 CharDriverState
*s
= opaque
;
119 qemu_chr_event(s
, CHR_EVENT_OPENED
);
120 qemu_bh_delete(s
->bh
);
124 void qemu_chr_generic_open(CharDriverState
*s
)
127 s
->bh
= qemu_bh_new(qemu_chr_generic_open_bh
, s
);
128 qemu_bh_schedule(s
->bh
);
132 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
134 return s
->chr_write(s
, buf
, len
);
137 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
141 return s
->chr_ioctl(s
, cmd
, arg
);
144 int qemu_chr_can_read(CharDriverState
*s
)
146 if (!s
->chr_can_read
)
148 return s
->chr_can_read(s
->handler_opaque
);
151 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
153 s
->chr_read(s
->handler_opaque
, buf
, len
);
156 int qemu_chr_get_msgfd(CharDriverState
*s
)
158 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
161 void qemu_chr_accept_input(CharDriverState
*s
)
163 if (s
->chr_accept_input
)
164 s
->chr_accept_input(s
);
167 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
169 char buf
[READ_BUF_LEN
];
172 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
173 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
177 void qemu_chr_send_event(CharDriverState
*s
, int event
)
179 if (s
->chr_send_event
)
180 s
->chr_send_event(s
, event
);
183 void qemu_chr_add_handlers(CharDriverState
*s
,
184 IOCanRWHandler
*fd_can_read
,
185 IOReadHandler
*fd_read
,
186 IOEventHandler
*fd_event
,
189 s
->chr_can_read
= fd_can_read
;
190 s
->chr_read
= fd_read
;
191 s
->chr_event
= fd_event
;
192 s
->handler_opaque
= opaque
;
193 if (s
->chr_update_read_handler
)
194 s
->chr_update_read_handler(s
);
197 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
202 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
204 CharDriverState
*chr
;
206 chr
= qemu_mallocz(sizeof(CharDriverState
));
207 chr
->chr_write
= null_chr_write
;
211 /* MUX driver for serial I/O splitting */
213 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
214 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
216 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
217 IOReadHandler
*chr_read
[MAX_MUX
];
218 IOEventHandler
*chr_event
[MAX_MUX
];
219 void *ext_opaque
[MAX_MUX
];
220 CharDriverState
*drv
;
225 /* Intermediate input buffer allows to catch escape sequences even if the
226 currently active device is not accepting any input - but only until it
228 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
233 int64_t timestamps_start
;
237 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
239 MuxDriver
*d
= chr
->opaque
;
241 if (!d
->timestamps
) {
242 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
247 for (i
= 0; i
< len
; i
++) {
253 ti
= qemu_get_clock(rt_clock
);
254 if (d
->timestamps_start
== -1)
255 d
->timestamps_start
= ti
;
256 ti
-= d
->timestamps_start
;
258 snprintf(buf1
, sizeof(buf1
),
259 "[%02d:%02d:%02d.%03d] ",
264 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
267 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
268 if (buf
[i
] == '\n') {
276 static const char * const mux_help
[] = {
277 "% h print this help\n\r",
278 "% x exit emulator\n\r",
279 "% s save disk data back to file (if -snapshot)\n\r",
280 "% t toggle console timestamps\n\r"
281 "% b send break (magic sysrq)\n\r",
282 "% c switch between console and monitor\n\r",
287 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
288 static void mux_print_help(CharDriverState
*chr
)
291 char ebuf
[15] = "Escape-Char";
292 char cbuf
[50] = "\n\r";
294 if (term_escape_char
> 0 && term_escape_char
< 26) {
295 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
296 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
298 snprintf(cbuf
, sizeof(cbuf
),
299 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
302 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
303 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
304 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
305 if (mux_help
[i
][j
] == '%')
306 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
308 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
313 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
315 if (d
->chr_event
[mux_nr
])
316 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
319 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
321 if (d
->term_got_escape
) {
322 d
->term_got_escape
= 0;
323 if (ch
== term_escape_char
)
332 const char *term
= "QEMU: Terminated\n\r";
333 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
340 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
341 bdrv_commit(dinfo
->bdrv
);
346 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
349 /* Switch to the next registered device */
350 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
352 if (d
->focus
>= d
->mux_cnt
)
354 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
357 d
->timestamps
= !d
->timestamps
;
358 d
->timestamps_start
= -1;
362 } else if (ch
== term_escape_char
) {
363 d
->term_got_escape
= 1;
371 static void mux_chr_accept_input(CharDriverState
*chr
)
373 MuxDriver
*d
= chr
->opaque
;
376 while (d
->prod
[m
] != d
->cons
[m
] &&
377 d
->chr_can_read
[m
] &&
378 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
379 d
->chr_read
[m
](d
->ext_opaque
[m
],
380 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
384 static int mux_chr_can_read(void *opaque
)
386 CharDriverState
*chr
= opaque
;
387 MuxDriver
*d
= chr
->opaque
;
390 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
392 if (d
->chr_can_read
[m
])
393 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
397 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
399 CharDriverState
*chr
= opaque
;
400 MuxDriver
*d
= chr
->opaque
;
404 mux_chr_accept_input (opaque
);
406 for(i
= 0; i
< size
; i
++)
407 if (mux_proc_byte(chr
, d
, buf
[i
])) {
408 if (d
->prod
[m
] == d
->cons
[m
] &&
409 d
->chr_can_read
[m
] &&
410 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
411 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
413 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
417 static void mux_chr_event(void *opaque
, int event
)
419 CharDriverState
*chr
= opaque
;
420 MuxDriver
*d
= chr
->opaque
;
423 /* Send the event to all registered listeners */
424 for (i
= 0; i
< d
->mux_cnt
; i
++)
425 mux_chr_send_event(d
, i
, event
);
428 static void mux_chr_update_read_handler(CharDriverState
*chr
)
430 MuxDriver
*d
= chr
->opaque
;
432 if (d
->mux_cnt
>= MAX_MUX
) {
433 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
436 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
437 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
438 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
439 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
440 /* Fix up the real driver with mux routines */
441 if (d
->mux_cnt
== 0) {
442 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
445 if (d
->focus
!= -1) {
446 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
448 d
->focus
= d
->mux_cnt
;
450 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
453 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
455 CharDriverState
*chr
;
458 chr
= qemu_mallocz(sizeof(CharDriverState
));
459 d
= qemu_mallocz(sizeof(MuxDriver
));
464 chr
->chr_write
= mux_chr_write
;
465 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
466 chr
->chr_accept_input
= mux_chr_accept_input
;
472 int send_all(int fd
, const void *buf
, int len1
)
478 ret
= send(fd
, buf
, len
, 0);
480 errno
= WSAGetLastError();
481 if (errno
!= WSAEWOULDBLOCK
) {
484 } else if (ret
== 0) {
496 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
502 ret
= write(fd
, buf
, len
);
504 if (errno
!= EINTR
&& errno
!= EAGAIN
)
506 } else if (ret
== 0) {
516 int send_all(int fd
, const void *buf
, int len1
)
518 return unix_write(fd
, buf
, len1
);
529 #define STDIO_MAX_CLIENTS 1
530 static int stdio_nb_clients
= 0;
532 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
534 FDCharDriver
*s
= chr
->opaque
;
535 return send_all(s
->fd_out
, buf
, len
);
538 static int fd_chr_read_poll(void *opaque
)
540 CharDriverState
*chr
= opaque
;
541 FDCharDriver
*s
= chr
->opaque
;
543 s
->max_size
= qemu_chr_can_read(chr
);
547 static void fd_chr_read(void *opaque
)
549 CharDriverState
*chr
= opaque
;
550 FDCharDriver
*s
= chr
->opaque
;
552 uint8_t buf
[READ_BUF_LEN
];
555 if (len
> s
->max_size
)
559 size
= read(s
->fd_in
, buf
, len
);
561 /* FD has been closed. Remove it from the active list. */
562 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
563 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
567 qemu_chr_read(chr
, buf
, size
);
571 static void fd_chr_update_read_handler(CharDriverState
*chr
)
573 FDCharDriver
*s
= chr
->opaque
;
576 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
578 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
579 fd_chr_read
, NULL
, chr
);
584 static void fd_chr_close(struct CharDriverState
*chr
)
586 FDCharDriver
*s
= chr
->opaque
;
589 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
591 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
596 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
599 /* open a character device to a unix fd */
600 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
602 CharDriverState
*chr
;
605 chr
= qemu_mallocz(sizeof(CharDriverState
));
606 s
= qemu_mallocz(sizeof(FDCharDriver
));
610 chr
->chr_write
= fd_chr_write
;
611 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
612 chr
->chr_close
= fd_chr_close
;
614 qemu_chr_generic_open(chr
);
619 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
623 TFR(fd_out
= open(qemu_opt_get(opts
, "path"),
624 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
627 return qemu_chr_open_fd(-1, fd_out
);
630 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
633 char filename_in
[256], filename_out
[256];
634 const char *filename
= qemu_opt_get(opts
, "path");
636 if (filename
== NULL
) {
637 fprintf(stderr
, "chardev: pipe: no filename given\n");
641 snprintf(filename_in
, 256, "%s.in", filename
);
642 snprintf(filename_out
, 256, "%s.out", filename
);
643 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
644 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
645 if (fd_in
< 0 || fd_out
< 0) {
650 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
654 return qemu_chr_open_fd(fd_in
, fd_out
);
658 /* for STDIO, we handle the case where several clients use it
661 #define TERM_FIFO_MAX_SIZE 1
663 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
664 static int term_fifo_size
;
666 static int stdio_read_poll(void *opaque
)
668 CharDriverState
*chr
= opaque
;
670 /* try to flush the queue if needed */
671 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
672 qemu_chr_read(chr
, term_fifo
, 1);
675 /* see if we can absorb more chars */
676 if (term_fifo_size
== 0)
682 static void stdio_read(void *opaque
)
686 CharDriverState
*chr
= opaque
;
688 size
= read(0, buf
, 1);
690 /* stdin has been closed. Remove it from the active list. */
691 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
692 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
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(QemuOpts
*opts
)
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 (!qemu_opt_get_bool(opts
, "signal", 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(QemuOpts
*opts
)
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__) \
825 || defined(__GLIBC__)
835 static void pty_chr_update_read_handler(CharDriverState
*chr
);
836 static void pty_chr_state(CharDriverState
*chr
, int connected
);
838 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
840 PtyCharDriver
*s
= chr
->opaque
;
843 /* guest sends data, check for (re-)connect */
844 pty_chr_update_read_handler(chr
);
847 return send_all(s
->fd
, buf
, len
);
850 static int pty_chr_read_poll(void *opaque
)
852 CharDriverState
*chr
= opaque
;
853 PtyCharDriver
*s
= chr
->opaque
;
855 s
->read_bytes
= qemu_chr_can_read(chr
);
856 return s
->read_bytes
;
859 static void pty_chr_read(void *opaque
)
861 CharDriverState
*chr
= opaque
;
862 PtyCharDriver
*s
= chr
->opaque
;
864 uint8_t buf
[READ_BUF_LEN
];
867 if (len
> s
->read_bytes
)
871 size
= read(s
->fd
, buf
, len
);
872 if ((size
== -1 && errno
== EIO
) ||
874 pty_chr_state(chr
, 0);
878 pty_chr_state(chr
, 1);
879 qemu_chr_read(chr
, buf
, size
);
883 static void pty_chr_update_read_handler(CharDriverState
*chr
)
885 PtyCharDriver
*s
= chr
->opaque
;
887 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
888 pty_chr_read
, NULL
, chr
);
891 * Short timeout here: just need wait long enougth that qemu makes
892 * it through the poll loop once. When reconnected we want a
893 * short timeout so we notice it almost instantly. Otherwise
894 * read() gives us -EIO instantly, making pty_chr_state() reset the
895 * timeout to the normal (much longer) poll interval before the
898 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
901 static void pty_chr_state(CharDriverState
*chr
, int connected
)
903 PtyCharDriver
*s
= chr
->opaque
;
906 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
909 /* (re-)connect poll interval for idle guests: once per second.
910 * We check more frequently in case the guests sends data to
911 * the virtual device linked to our pty. */
912 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
915 qemu_chr_generic_open(chr
);
920 static void pty_chr_timer(void *opaque
)
922 struct CharDriverState
*chr
= opaque
;
923 PtyCharDriver
*s
= chr
->opaque
;
928 /* If we arrive here without polling being cleared due
929 * read returning -EIO, then we are (re-)connected */
930 pty_chr_state(chr
, 1);
935 pty_chr_update_read_handler(chr
);
938 static void pty_chr_close(struct CharDriverState
*chr
)
940 PtyCharDriver
*s
= chr
->opaque
;
942 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
944 qemu_del_timer(s
->timer
);
945 qemu_free_timer(s
->timer
);
947 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
950 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
952 CharDriverState
*chr
;
956 #if defined(__OpenBSD__) || defined(__DragonFly__)
957 char pty_name
[PATH_MAX
];
958 #define q_ptsname(x) pty_name
960 char *pty_name
= NULL
;
961 #define q_ptsname(x) ptsname(x)
964 chr
= qemu_mallocz(sizeof(CharDriverState
));
965 s
= qemu_mallocz(sizeof(PtyCharDriver
));
967 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
971 /* Set raw attributes on the pty. */
972 tcgetattr(slave_fd
, &tty
);
974 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
977 len
= strlen(q_ptsname(s
->fd
)) + 5;
978 chr
->filename
= qemu_malloc(len
);
979 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
980 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
981 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
984 chr
->chr_write
= pty_chr_write
;
985 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
986 chr
->chr_close
= pty_chr_close
;
988 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
993 static void tty_serial_init(int fd
, int speed
,
994 int parity
, int data_bits
, int stop_bits
)
1000 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1001 speed
, parity
, data_bits
, stop_bits
);
1003 tcgetattr (fd
, &tty
);
1005 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1006 speed
= speed
* 10 / 11;
1023 /* Non-Posix values follow. They may be unsupported on some systems. */
1025 check_speed(115200);
1027 check_speed(230400);
1030 check_speed(460800);
1033 check_speed(500000);
1036 check_speed(576000);
1039 check_speed(921600);
1042 check_speed(1000000);
1045 check_speed(1152000);
1048 check_speed(1500000);
1051 check_speed(2000000);
1054 check_speed(2500000);
1057 check_speed(3000000);
1060 check_speed(3500000);
1063 check_speed(4000000);
1068 cfsetispeed(&tty
, spd
);
1069 cfsetospeed(&tty
, spd
);
1071 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1072 |INLCR
|IGNCR
|ICRNL
|IXON
);
1073 tty
.c_oflag
|= OPOST
;
1074 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1075 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1096 tty
.c_cflag
|= PARENB
;
1099 tty
.c_cflag
|= PARENB
| PARODD
;
1103 tty
.c_cflag
|= CSTOPB
;
1105 tcsetattr (fd
, TCSANOW
, &tty
);
1108 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1110 FDCharDriver
*s
= chr
->opaque
;
1113 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1115 QEMUSerialSetParams
*ssp
= arg
;
1116 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1117 ssp
->data_bits
, ssp
->stop_bits
);
1120 case CHR_IOCTL_SERIAL_SET_BREAK
:
1122 int enable
= *(int *)arg
;
1124 tcsendbreak(s
->fd_in
, 1);
1127 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1130 int *targ
= (int *)arg
;
1131 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1133 if (sarg
& TIOCM_CTS
)
1134 *targ
|= CHR_TIOCM_CTS
;
1135 if (sarg
& TIOCM_CAR
)
1136 *targ
|= CHR_TIOCM_CAR
;
1137 if (sarg
& TIOCM_DSR
)
1138 *targ
|= CHR_TIOCM_DSR
;
1139 if (sarg
& TIOCM_RI
)
1140 *targ
|= CHR_TIOCM_RI
;
1141 if (sarg
& TIOCM_DTR
)
1142 *targ
|= CHR_TIOCM_DTR
;
1143 if (sarg
& TIOCM_RTS
)
1144 *targ
|= CHR_TIOCM_RTS
;
1147 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1149 int sarg
= *(int *)arg
;
1151 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1152 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1153 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1154 if (sarg
& CHR_TIOCM_CTS
)
1156 if (sarg
& CHR_TIOCM_CAR
)
1158 if (sarg
& CHR_TIOCM_DSR
)
1160 if (sarg
& CHR_TIOCM_RI
)
1162 if (sarg
& CHR_TIOCM_DTR
)
1164 if (sarg
& CHR_TIOCM_RTS
)
1166 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1175 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1177 const char *filename
= qemu_opt_get(opts
, "path");
1178 CharDriverState
*chr
;
1181 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1182 tty_serial_init(fd
, 115200, 'N', 8, 1);
1183 chr
= qemu_chr_open_fd(fd
, fd
);
1188 chr
->chr_ioctl
= tty_serial_ioctl
;
1189 qemu_chr_generic_open(chr
);
1192 #else /* ! __linux__ && ! __sun__ */
1193 static CharDriverState
*qemu_chr_open_pty(void)
1197 #endif /* __linux__ || __sun__ */
1199 #if defined(__linux__)
1203 } ParallelCharDriver
;
1205 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1207 if (s
->mode
!= mode
) {
1209 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1216 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1218 ParallelCharDriver
*drv
= chr
->opaque
;
1223 case CHR_IOCTL_PP_READ_DATA
:
1224 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1226 *(uint8_t *)arg
= b
;
1228 case CHR_IOCTL_PP_WRITE_DATA
:
1229 b
= *(uint8_t *)arg
;
1230 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1233 case CHR_IOCTL_PP_READ_CONTROL
:
1234 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1236 /* Linux gives only the lowest bits, and no way to know data
1237 direction! For better compatibility set the fixed upper
1239 *(uint8_t *)arg
= b
| 0xc0;
1241 case CHR_IOCTL_PP_WRITE_CONTROL
:
1242 b
= *(uint8_t *)arg
;
1243 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1246 case CHR_IOCTL_PP_READ_STATUS
:
1247 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1249 *(uint8_t *)arg
= b
;
1251 case CHR_IOCTL_PP_DATA_DIR
:
1252 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1255 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1256 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1257 struct ParallelIOArg
*parg
= arg
;
1258 int n
= read(fd
, parg
->buffer
, parg
->count
);
1259 if (n
!= parg
->count
) {
1264 case CHR_IOCTL_PP_EPP_READ
:
1265 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1266 struct ParallelIOArg
*parg
= arg
;
1267 int n
= read(fd
, parg
->buffer
, parg
->count
);
1268 if (n
!= parg
->count
) {
1273 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1274 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1275 struct ParallelIOArg
*parg
= arg
;
1276 int n
= write(fd
, parg
->buffer
, parg
->count
);
1277 if (n
!= parg
->count
) {
1282 case CHR_IOCTL_PP_EPP_WRITE
:
1283 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1284 struct ParallelIOArg
*parg
= arg
;
1285 int n
= write(fd
, parg
->buffer
, parg
->count
);
1286 if (n
!= parg
->count
) {
1297 static void pp_close(CharDriverState
*chr
)
1299 ParallelCharDriver
*drv
= chr
->opaque
;
1302 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1303 ioctl(fd
, PPRELEASE
);
1306 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1309 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1311 const char *filename
= qemu_opt_get(opts
, "path");
1312 CharDriverState
*chr
;
1313 ParallelCharDriver
*drv
;
1316 TFR(fd
= open(filename
, O_RDWR
));
1320 if (ioctl(fd
, PPCLAIM
) < 0) {
1325 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1327 drv
->mode
= IEEE1284_MODE_COMPAT
;
1329 chr
= qemu_mallocz(sizeof(CharDriverState
));
1330 chr
->chr_write
= null_chr_write
;
1331 chr
->chr_ioctl
= pp_ioctl
;
1332 chr
->chr_close
= pp_close
;
1335 qemu_chr_generic_open(chr
);
1339 #endif /* __linux__ */
1341 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1342 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1344 int fd
= (int)chr
->opaque
;
1348 case CHR_IOCTL_PP_READ_DATA
:
1349 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1351 *(uint8_t *)arg
= b
;
1353 case CHR_IOCTL_PP_WRITE_DATA
:
1354 b
= *(uint8_t *)arg
;
1355 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1358 case CHR_IOCTL_PP_READ_CONTROL
:
1359 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1361 *(uint8_t *)arg
= b
;
1363 case CHR_IOCTL_PP_WRITE_CONTROL
:
1364 b
= *(uint8_t *)arg
;
1365 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1368 case CHR_IOCTL_PP_READ_STATUS
:
1369 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1371 *(uint8_t *)arg
= b
;
1379 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1381 const char *filename
= qemu_opt_get(opts
, "path");
1382 CharDriverState
*chr
;
1385 fd
= open(filename
, O_RDWR
);
1389 chr
= qemu_mallocz(sizeof(CharDriverState
));
1390 chr
->opaque
= (void *)fd
;
1391 chr
->chr_write
= null_chr_write
;
1392 chr
->chr_ioctl
= pp_ioctl
;
1401 HANDLE hcom
, hrecv
, hsend
;
1402 OVERLAPPED orecv
, osend
;
1407 #define NSENDBUF 2048
1408 #define NRECVBUF 2048
1409 #define MAXCONNECT 1
1410 #define NTIMEOUT 5000
1412 static int win_chr_poll(void *opaque
);
1413 static int win_chr_pipe_poll(void *opaque
);
1415 static void win_chr_close(CharDriverState
*chr
)
1417 WinCharState
*s
= chr
->opaque
;
1420 CloseHandle(s
->hsend
);
1424 CloseHandle(s
->hrecv
);
1428 CloseHandle(s
->hcom
);
1432 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1434 qemu_del_polling_cb(win_chr_poll
, chr
);
1436 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1439 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1441 WinCharState
*s
= chr
->opaque
;
1443 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1448 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1450 fprintf(stderr
, "Failed CreateEvent\n");
1453 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1455 fprintf(stderr
, "Failed CreateEvent\n");
1459 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1460 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1461 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1462 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1467 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1468 fprintf(stderr
, "Failed SetupComm\n");
1472 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1473 size
= sizeof(COMMCONFIG
);
1474 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1475 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1476 CommConfigDialog(filename
, NULL
, &comcfg
);
1478 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1479 fprintf(stderr
, "Failed SetCommState\n");
1483 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1484 fprintf(stderr
, "Failed SetCommMask\n");
1488 cto
.ReadIntervalTimeout
= MAXDWORD
;
1489 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1490 fprintf(stderr
, "Failed SetCommTimeouts\n");
1494 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1495 fprintf(stderr
, "Failed ClearCommError\n");
1498 qemu_add_polling_cb(win_chr_poll
, chr
);
1506 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1508 WinCharState
*s
= chr
->opaque
;
1509 DWORD len
, ret
, size
, err
;
1512 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1513 s
->osend
.hEvent
= s
->hsend
;
1516 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1518 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1520 err
= GetLastError();
1521 if (err
== ERROR_IO_PENDING
) {
1522 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1540 static int win_chr_read_poll(CharDriverState
*chr
)
1542 WinCharState
*s
= chr
->opaque
;
1544 s
->max_size
= qemu_chr_can_read(chr
);
1548 static void win_chr_readfile(CharDriverState
*chr
)
1550 WinCharState
*s
= chr
->opaque
;
1552 uint8_t buf
[READ_BUF_LEN
];
1555 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1556 s
->orecv
.hEvent
= s
->hrecv
;
1557 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1559 err
= GetLastError();
1560 if (err
== ERROR_IO_PENDING
) {
1561 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1566 qemu_chr_read(chr
, buf
, size
);
1570 static void win_chr_read(CharDriverState
*chr
)
1572 WinCharState
*s
= chr
->opaque
;
1574 if (s
->len
> s
->max_size
)
1575 s
->len
= s
->max_size
;
1579 win_chr_readfile(chr
);
1582 static int win_chr_poll(void *opaque
)
1584 CharDriverState
*chr
= opaque
;
1585 WinCharState
*s
= chr
->opaque
;
1589 ClearCommError(s
->hcom
, &comerr
, &status
);
1590 if (status
.cbInQue
> 0) {
1591 s
->len
= status
.cbInQue
;
1592 win_chr_read_poll(chr
);
1599 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1601 const char *filename
= qemu_opt_get(opts
, "path");
1602 CharDriverState
*chr
;
1605 chr
= qemu_mallocz(sizeof(CharDriverState
));
1606 s
= qemu_mallocz(sizeof(WinCharState
));
1608 chr
->chr_write
= win_chr_write
;
1609 chr
->chr_close
= win_chr_close
;
1611 if (win_chr_init(chr
, filename
) < 0) {
1616 qemu_chr_generic_open(chr
);
1620 static int win_chr_pipe_poll(void *opaque
)
1622 CharDriverState
*chr
= opaque
;
1623 WinCharState
*s
= chr
->opaque
;
1626 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1629 win_chr_read_poll(chr
);
1636 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1638 WinCharState
*s
= chr
->opaque
;
1646 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1648 fprintf(stderr
, "Failed CreateEvent\n");
1651 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1653 fprintf(stderr
, "Failed CreateEvent\n");
1657 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1658 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1659 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1661 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1662 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1663 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1668 ZeroMemory(&ov
, sizeof(ov
));
1669 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1670 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1672 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1676 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1678 fprintf(stderr
, "Failed GetOverlappedResult\n");
1680 CloseHandle(ov
.hEvent
);
1687 CloseHandle(ov
.hEvent
);
1690 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1699 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1701 const char *filename
= qemu_opt_get(opts
, "path");
1702 CharDriverState
*chr
;
1705 chr
= qemu_mallocz(sizeof(CharDriverState
));
1706 s
= qemu_mallocz(sizeof(WinCharState
));
1708 chr
->chr_write
= win_chr_write
;
1709 chr
->chr_close
= win_chr_close
;
1711 if (win_chr_pipe_init(chr
, filename
) < 0) {
1716 qemu_chr_generic_open(chr
);
1720 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1722 CharDriverState
*chr
;
1725 chr
= qemu_mallocz(sizeof(CharDriverState
));
1726 s
= qemu_mallocz(sizeof(WinCharState
));
1729 chr
->chr_write
= win_chr_write
;
1730 qemu_chr_generic_open(chr
);
1734 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1736 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1739 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1741 const char *file_out
= qemu_opt_get(opts
, "path");
1744 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1745 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1746 if (fd_out
== INVALID_HANDLE_VALUE
)
1749 return qemu_chr_open_win_file(fd_out
);
1751 #endif /* !_WIN32 */
1753 /***********************************************************/
1754 /* UDP Net console */
1758 uint8_t buf
[READ_BUF_LEN
];
1764 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1766 NetCharDriver
*s
= chr
->opaque
;
1768 return send(s
->fd
, (const void *)buf
, len
, 0);
1771 static int udp_chr_read_poll(void *opaque
)
1773 CharDriverState
*chr
= opaque
;
1774 NetCharDriver
*s
= chr
->opaque
;
1776 s
->max_size
= qemu_chr_can_read(chr
);
1778 /* If there were any stray characters in the queue process them
1781 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1782 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1784 s
->max_size
= qemu_chr_can_read(chr
);
1789 static void udp_chr_read(void *opaque
)
1791 CharDriverState
*chr
= opaque
;
1792 NetCharDriver
*s
= chr
->opaque
;
1794 if (s
->max_size
== 0)
1796 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1797 s
->bufptr
= s
->bufcnt
;
1802 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1803 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1805 s
->max_size
= qemu_chr_can_read(chr
);
1809 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1811 NetCharDriver
*s
= chr
->opaque
;
1814 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1815 udp_chr_read
, NULL
, chr
);
1819 static void udp_chr_close(CharDriverState
*chr
)
1821 NetCharDriver
*s
= chr
->opaque
;
1823 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1827 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1830 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1832 CharDriverState
*chr
= NULL
;
1833 NetCharDriver
*s
= NULL
;
1836 chr
= qemu_mallocz(sizeof(CharDriverState
));
1837 s
= qemu_mallocz(sizeof(NetCharDriver
));
1839 fd
= inet_dgram_opts(opts
);
1841 fprintf(stderr
, "inet_dgram_opts failed\n");
1849 chr
->chr_write
= udp_chr_write
;
1850 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1851 chr
->chr_close
= udp_chr_close
;
1864 /***********************************************************/
1865 /* TCP Net console */
1877 static void tcp_chr_accept(void *opaque
);
1879 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1881 TCPCharDriver
*s
= chr
->opaque
;
1883 return send_all(s
->fd
, buf
, len
);
1885 /* XXX: indicate an error ? */
1890 static int tcp_chr_read_poll(void *opaque
)
1892 CharDriverState
*chr
= opaque
;
1893 TCPCharDriver
*s
= chr
->opaque
;
1896 s
->max_size
= qemu_chr_can_read(chr
);
1901 #define IAC_BREAK 243
1902 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1904 uint8_t *buf
, int *size
)
1906 /* Handle any telnet client's basic IAC options to satisfy char by
1907 * char mode with no echo. All IAC options will be removed from
1908 * the buf and the do_telnetopt variable will be used to track the
1909 * state of the width of the IAC information.
1911 * IAC commands come in sets of 3 bytes with the exception of the
1912 * "IAC BREAK" command and the double IAC.
1918 for (i
= 0; i
< *size
; i
++) {
1919 if (s
->do_telnetopt
> 1) {
1920 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1921 /* Double IAC means send an IAC */
1925 s
->do_telnetopt
= 1;
1927 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1928 /* Handle IAC break commands by sending a serial break */
1929 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1934 if (s
->do_telnetopt
>= 4) {
1935 s
->do_telnetopt
= 1;
1938 if ((unsigned char)buf
[i
] == IAC
) {
1939 s
->do_telnetopt
= 2;
1950 static int tcp_get_msgfd(CharDriverState
*chr
)
1952 TCPCharDriver
*s
= chr
->opaque
;
1958 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1960 TCPCharDriver
*s
= chr
->opaque
;
1961 struct cmsghdr
*cmsg
;
1963 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1966 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1967 cmsg
->cmsg_level
!= SOL_SOCKET
||
1968 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1971 fd
= *((int *)CMSG_DATA(cmsg
));
1981 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1983 TCPCharDriver
*s
= chr
->opaque
;
1984 struct msghdr msg
= { NULL
, };
1985 struct iovec iov
[1];
1987 struct cmsghdr cmsg
;
1988 char control
[CMSG_SPACE(sizeof(int))];
1992 iov
[0].iov_base
= buf
;
1993 iov
[0].iov_len
= len
;
1997 msg
.msg_control
= &msg_control
;
1998 msg
.msg_controllen
= sizeof(msg_control
);
2000 ret
= recvmsg(s
->fd
, &msg
, 0);
2001 if (ret
> 0 && s
->is_unix
)
2002 unix_process_msgfd(chr
, &msg
);
2007 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2009 TCPCharDriver
*s
= chr
->opaque
;
2010 return recv(s
->fd
, buf
, len
, 0);
2014 static void tcp_chr_read(void *opaque
)
2016 CharDriverState
*chr
= opaque
;
2017 TCPCharDriver
*s
= chr
->opaque
;
2018 uint8_t buf
[READ_BUF_LEN
];
2021 if (!s
->connected
|| s
->max_size
<= 0)
2024 if (len
> s
->max_size
)
2026 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2028 /* connection closed */
2030 if (s
->listen_fd
>= 0) {
2031 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2033 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2036 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2037 } else if (size
> 0) {
2038 if (s
->do_telnetopt
)
2039 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2041 qemu_chr_read(chr
, buf
, size
);
2042 if (s
->msgfd
!= -1) {
2049 static void tcp_chr_connect(void *opaque
)
2051 CharDriverState
*chr
= opaque
;
2052 TCPCharDriver
*s
= chr
->opaque
;
2055 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2056 tcp_chr_read
, NULL
, chr
);
2057 qemu_chr_generic_open(chr
);
2060 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2061 static void tcp_chr_telnet_init(int fd
)
2064 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2065 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2066 send(fd
, (char *)buf
, 3, 0);
2067 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2068 send(fd
, (char *)buf
, 3, 0);
2069 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2070 send(fd
, (char *)buf
, 3, 0);
2071 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2072 send(fd
, (char *)buf
, 3, 0);
2075 static void socket_set_nodelay(int fd
)
2078 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2081 static void tcp_chr_accept(void *opaque
)
2083 CharDriverState
*chr
= opaque
;
2084 TCPCharDriver
*s
= chr
->opaque
;
2085 struct sockaddr_in saddr
;
2087 struct sockaddr_un uaddr
;
2089 struct sockaddr
*addr
;
2096 len
= sizeof(uaddr
);
2097 addr
= (struct sockaddr
*)&uaddr
;
2101 len
= sizeof(saddr
);
2102 addr
= (struct sockaddr
*)&saddr
;
2104 fd
= accept(s
->listen_fd
, addr
, &len
);
2105 if (fd
< 0 && errno
!= EINTR
) {
2107 } else if (fd
>= 0) {
2108 if (s
->do_telnetopt
)
2109 tcp_chr_telnet_init(fd
);
2113 socket_set_nonblock(fd
);
2115 socket_set_nodelay(fd
);
2117 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2118 tcp_chr_connect(chr
);
2121 static void tcp_chr_close(CharDriverState
*chr
)
2123 TCPCharDriver
*s
= chr
->opaque
;
2125 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2128 if (s
->listen_fd
>= 0) {
2129 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2130 closesocket(s
->listen_fd
);
2133 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2136 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2138 CharDriverState
*chr
= NULL
;
2139 TCPCharDriver
*s
= NULL
;
2147 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2148 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2149 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2150 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2151 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2155 chr
= qemu_mallocz(sizeof(CharDriverState
));
2156 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2160 fd
= unix_listen_opts(opts
);
2162 fd
= unix_connect_opts(opts
);
2166 fd
= inet_listen_opts(opts
, 0);
2168 fd
= inet_connect_opts(opts
);
2174 if (!is_waitconnect
)
2175 socket_set_nonblock(fd
);
2181 s
->is_unix
= is_unix
;
2182 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2185 chr
->chr_write
= tcp_chr_write
;
2186 chr
->chr_close
= tcp_chr_close
;
2187 chr
->get_msgfd
= tcp_get_msgfd
;
2191 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2193 s
->do_telnetopt
= 1;
2198 socket_set_nodelay(fd
);
2199 tcp_chr_connect(chr
);
2202 /* for "info chardev" monitor command */
2203 chr
->filename
= qemu_malloc(256);
2205 snprintf(chr
->filename
, 256, "unix:%s%s",
2206 qemu_opt_get(opts
, "path"),
2207 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2208 } else if (is_telnet
) {
2209 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2210 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2211 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2213 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2214 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2215 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2218 if (is_listen
&& is_waitconnect
) {
2219 printf("QEMU waiting for connection on: %s\n",
2221 tcp_chr_accept(chr
);
2222 socket_set_nonblock(s
->listen_fd
);
2234 static QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2236 char host
[65], port
[33], width
[8], height
[8];
2241 opts
= qemu_opts_create(&qemu_chardev_opts
, label
, 1);
2245 if (strstart(filename
, "mon:", &p
)) {
2247 qemu_opt_set(opts
, "mux", "on");
2250 if (strcmp(filename
, "null") == 0 ||
2251 strcmp(filename
, "pty") == 0 ||
2252 strcmp(filename
, "msmouse") == 0 ||
2253 strcmp(filename
, "braille") == 0 ||
2254 strcmp(filename
, "stdio") == 0) {
2255 qemu_opt_set(opts
, "backend", filename
);
2258 if (strstart(filename
, "vc", &p
)) {
2259 qemu_opt_set(opts
, "backend", "vc");
2261 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2263 qemu_opt_set(opts
, "width", width
);
2264 qemu_opt_set(opts
, "height", height
);
2265 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2267 qemu_opt_set(opts
, "cols", width
);
2268 qemu_opt_set(opts
, "rows", height
);
2275 if (strcmp(filename
, "con:") == 0) {
2276 qemu_opt_set(opts
, "backend", "console");
2279 if (strstart(filename
, "COM", NULL
)) {
2280 qemu_opt_set(opts
, "backend", "serial");
2281 qemu_opt_set(opts
, "path", filename
);
2284 if (strstart(filename
, "file:", &p
)) {
2285 qemu_opt_set(opts
, "backend", "file");
2286 qemu_opt_set(opts
, "path", p
);
2289 if (strstart(filename
, "pipe:", &p
)) {
2290 qemu_opt_set(opts
, "backend", "pipe");
2291 qemu_opt_set(opts
, "path", p
);
2294 if (strstart(filename
, "tcp:", &p
) ||
2295 strstart(filename
, "telnet:", &p
)) {
2296 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2298 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2301 qemu_opt_set(opts
, "backend", "socket");
2302 qemu_opt_set(opts
, "host", host
);
2303 qemu_opt_set(opts
, "port", port
);
2304 if (p
[pos
] == ',') {
2305 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2308 if (strstart(filename
, "telnet:", &p
))
2309 qemu_opt_set(opts
, "telnet", "on");
2312 if (strstart(filename
, "udp:", &p
)) {
2313 qemu_opt_set(opts
, "backend", "udp");
2314 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2316 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2317 fprintf(stderr
, "udp #1\n");
2321 qemu_opt_set(opts
, "host", host
);
2322 qemu_opt_set(opts
, "port", port
);
2323 if (p
[pos
] == '@') {
2325 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2327 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2328 fprintf(stderr
, "udp #2\n");
2332 qemu_opt_set(opts
, "localaddr", host
);
2333 qemu_opt_set(opts
, "localport", port
);
2337 if (strstart(filename
, "unix:", &p
)) {
2338 qemu_opt_set(opts
, "backend", "socket");
2339 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2343 if (strstart(filename
, "/dev/parport", NULL
) ||
2344 strstart(filename
, "/dev/ppi", NULL
)) {
2345 qemu_opt_set(opts
, "backend", "parport");
2346 qemu_opt_set(opts
, "path", filename
);
2349 if (strstart(filename
, "/dev/", NULL
)) {
2350 qemu_opt_set(opts
, "backend", "tty");
2351 qemu_opt_set(opts
, "path", filename
);
2356 fprintf(stderr
, "%s: fail on \"%s\"\n", __FUNCTION__
, filename
);
2357 qemu_opts_del(opts
);
2361 static const struct {
2363 CharDriverState
*(*open
)(QemuOpts
*opts
);
2364 } backend_table
[] = {
2365 { .name
= "null", .open
= qemu_chr_open_null
},
2366 { .name
= "socket", .open
= qemu_chr_open_socket
},
2367 { .name
= "udp", .open
= qemu_chr_open_udp
},
2368 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2369 { .name
= "vc", .open
= text_console_init
},
2371 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2372 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2373 { .name
= "console", .open
= qemu_chr_open_win_con
},
2374 { .name
= "serial", .open
= qemu_chr_open_win
},
2376 { .name
= "file", .open
= qemu_chr_open_file_out
},
2377 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2378 { .name
= "pty", .open
= qemu_chr_open_pty
},
2379 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2381 #ifdef CONFIG_BRLAPI
2382 { .name
= "braille", .open
= chr_baum_init
},
2384 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2385 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2386 || defined(__FreeBSD_kernel__)
2387 { .name
= "tty", .open
= qemu_chr_open_tty
},
2389 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2390 || defined(__FreeBSD_kernel__)
2391 { .name
= "parport", .open
= qemu_chr_open_pp
},
2395 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2396 void (*init
)(struct CharDriverState
*s
))
2398 CharDriverState
*chr
;
2401 if (qemu_opts_id(opts
) == NULL
) {
2402 fprintf(stderr
, "chardev: no id specified\n");
2406 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2407 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2410 if (i
== ARRAY_SIZE(backend_table
)) {
2411 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2412 qemu_opt_get(opts
, "backend"));
2416 chr
= backend_table
[i
].open(opts
);
2418 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2419 qemu_opt_get(opts
, "backend"));
2424 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2426 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2428 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2429 CharDriverState
*base
= chr
;
2430 int len
= strlen(qemu_opts_id(opts
)) + 6;
2431 base
->label
= qemu_malloc(len
);
2432 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2433 chr
= qemu_chr_open_mux(base
);
2434 chr
->filename
= base
->filename
;
2435 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2437 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2441 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2444 CharDriverState
*chr
;
2447 if (strstart(filename
, "chardev:", &p
)) {
2448 return qemu_chr_find(p
);
2451 opts
= qemu_chr_parse_compat(label
, filename
);
2455 chr
= qemu_chr_open_opts(opts
, init
);
2456 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2457 monitor_init(chr
, MONITOR_USE_READLINE
);
2462 void qemu_chr_close(CharDriverState
*chr
)
2464 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2466 chr
->chr_close(chr
);
2467 qemu_free(chr
->filename
);
2468 qemu_free(chr
->label
);
2472 void qemu_chr_info(Monitor
*mon
)
2474 CharDriverState
*chr
;
2476 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2477 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);
2481 CharDriverState
*qemu_chr_find(const char *name
)
2483 CharDriverState
*chr
;
2485 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2486 if (strcmp(chr
->label
, name
) != 0)