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"
33 #include "hw/msmouse.h"
34 #include "qemu-objects.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"
100 #include "ui/qemu-spice.h"
102 #define READ_BUF_LEN 4096
104 /***********************************************************/
105 /* character device */
107 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
108 QTAILQ_HEAD_INITIALIZER(chardevs
);
110 static void qemu_chr_event(CharDriverState
*s
, int event
)
112 /* Keep track if the char device is open */
114 case CHR_EVENT_OPENED
:
117 case CHR_EVENT_CLOSED
:
124 s
->chr_event(s
->handler_opaque
, event
);
127 static void qemu_chr_generic_open_bh(void *opaque
)
129 CharDriverState
*s
= opaque
;
130 qemu_chr_event(s
, CHR_EVENT_OPENED
);
131 qemu_bh_delete(s
->bh
);
135 void qemu_chr_generic_open(CharDriverState
*s
)
138 s
->bh
= qemu_bh_new(qemu_chr_generic_open_bh
, s
);
139 qemu_bh_schedule(s
->bh
);
143 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
145 return s
->chr_write(s
, buf
, len
);
148 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
152 return s
->chr_ioctl(s
, cmd
, arg
);
155 int qemu_chr_can_read(CharDriverState
*s
)
157 if (!s
->chr_can_read
)
159 return s
->chr_can_read(s
->handler_opaque
);
162 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
164 s
->chr_read(s
->handler_opaque
, buf
, len
);
167 int qemu_chr_get_msgfd(CharDriverState
*s
)
169 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
172 void qemu_chr_accept_input(CharDriverState
*s
)
174 if (s
->chr_accept_input
)
175 s
->chr_accept_input(s
);
178 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
180 char buf
[READ_BUF_LEN
];
183 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
184 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
188 void qemu_chr_send_event(CharDriverState
*s
, int event
)
190 if (s
->chr_send_event
)
191 s
->chr_send_event(s
, event
);
194 void qemu_chr_add_handlers(CharDriverState
*s
,
195 IOCanReadHandler
*fd_can_read
,
196 IOReadHandler
*fd_read
,
197 IOEventHandler
*fd_event
,
200 s
->chr_can_read
= fd_can_read
;
201 s
->chr_read
= fd_read
;
202 s
->chr_event
= fd_event
;
203 s
->handler_opaque
= opaque
;
204 if (s
->chr_update_read_handler
)
205 s
->chr_update_read_handler(s
);
207 /* We're connecting to an already opened device, so let's make sure we
208 also get the open event */
210 qemu_chr_generic_open(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 IOCanReadHandler
*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
));
358 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
361 /* Switch to the next registered device */
362 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
364 if (d
->focus
>= d
->mux_cnt
)
366 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
369 d
->timestamps
= !d
->timestamps
;
370 d
->timestamps_start
= -1;
374 } else if (ch
== term_escape_char
) {
375 d
->term_got_escape
= 1;
383 static void mux_chr_accept_input(CharDriverState
*chr
)
385 MuxDriver
*d
= chr
->opaque
;
388 while (d
->prod
[m
] != d
->cons
[m
] &&
389 d
->chr_can_read
[m
] &&
390 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
391 d
->chr_read
[m
](d
->ext_opaque
[m
],
392 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
396 static int mux_chr_can_read(void *opaque
)
398 CharDriverState
*chr
= opaque
;
399 MuxDriver
*d
= chr
->opaque
;
402 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
404 if (d
->chr_can_read
[m
])
405 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
409 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
411 CharDriverState
*chr
= opaque
;
412 MuxDriver
*d
= chr
->opaque
;
416 mux_chr_accept_input (opaque
);
418 for(i
= 0; i
< size
; i
++)
419 if (mux_proc_byte(chr
, d
, buf
[i
])) {
420 if (d
->prod
[m
] == d
->cons
[m
] &&
421 d
->chr_can_read
[m
] &&
422 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
423 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
425 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
429 static void mux_chr_event(void *opaque
, int event
)
431 CharDriverState
*chr
= opaque
;
432 MuxDriver
*d
= chr
->opaque
;
435 /* Send the event to all registered listeners */
436 for (i
= 0; i
< d
->mux_cnt
; i
++)
437 mux_chr_send_event(d
, i
, event
);
440 static void mux_chr_update_read_handler(CharDriverState
*chr
)
442 MuxDriver
*d
= chr
->opaque
;
444 if (d
->mux_cnt
>= MAX_MUX
) {
445 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
448 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
449 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
450 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
451 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
452 /* Fix up the real driver with mux routines */
453 if (d
->mux_cnt
== 0) {
454 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
457 if (d
->focus
!= -1) {
458 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
460 d
->focus
= d
->mux_cnt
;
462 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
465 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
467 CharDriverState
*chr
;
470 chr
= qemu_mallocz(sizeof(CharDriverState
));
471 d
= qemu_mallocz(sizeof(MuxDriver
));
476 chr
->chr_write
= mux_chr_write
;
477 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
478 chr
->chr_accept_input
= mux_chr_accept_input
;
480 /* Muxes are always open on creation */
481 qemu_chr_generic_open(chr
);
488 int send_all(int fd
, const void *buf
, int len1
)
494 ret
= send(fd
, buf
, len
, 0);
496 errno
= WSAGetLastError();
497 if (errno
!= WSAEWOULDBLOCK
) {
500 } else if (ret
== 0) {
512 int send_all(int fd
, const void *_buf
, int len1
)
515 const uint8_t *buf
= _buf
;
519 ret
= write(fd
, buf
, len
);
521 if (errno
!= EINTR
&& errno
!= EAGAIN
)
523 } else if (ret
== 0) {
541 #define STDIO_MAX_CLIENTS 1
542 static int stdio_nb_clients
= 0;
544 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
546 FDCharDriver
*s
= chr
->opaque
;
547 return send_all(s
->fd_out
, buf
, len
);
550 static int fd_chr_read_poll(void *opaque
)
552 CharDriverState
*chr
= opaque
;
553 FDCharDriver
*s
= chr
->opaque
;
555 s
->max_size
= qemu_chr_can_read(chr
);
559 static void fd_chr_read(void *opaque
)
561 CharDriverState
*chr
= opaque
;
562 FDCharDriver
*s
= chr
->opaque
;
564 uint8_t buf
[READ_BUF_LEN
];
567 if (len
> s
->max_size
)
571 size
= read(s
->fd_in
, buf
, len
);
573 /* FD has been closed. Remove it from the active list. */
574 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
575 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
579 qemu_chr_read(chr
, buf
, size
);
583 static void fd_chr_update_read_handler(CharDriverState
*chr
)
585 FDCharDriver
*s
= chr
->opaque
;
588 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
590 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
591 fd_chr_read
, NULL
, chr
);
596 static void fd_chr_close(struct CharDriverState
*chr
)
598 FDCharDriver
*s
= chr
->opaque
;
601 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
603 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
608 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
611 /* open a character device to a unix fd */
612 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
614 CharDriverState
*chr
;
617 chr
= qemu_mallocz(sizeof(CharDriverState
));
618 s
= qemu_mallocz(sizeof(FDCharDriver
));
622 chr
->chr_write
= fd_chr_write
;
623 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
624 chr
->chr_close
= fd_chr_close
;
626 qemu_chr_generic_open(chr
);
631 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
635 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
636 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
639 return qemu_chr_open_fd(-1, fd_out
);
642 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
645 char filename_in
[256], filename_out
[256];
646 const char *filename
= qemu_opt_get(opts
, "path");
648 if (filename
== NULL
) {
649 fprintf(stderr
, "chardev: pipe: no filename given\n");
653 snprintf(filename_in
, 256, "%s.in", filename
);
654 snprintf(filename_out
, 256, "%s.out", filename
);
655 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
656 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
657 if (fd_in
< 0 || fd_out
< 0) {
662 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
666 return qemu_chr_open_fd(fd_in
, fd_out
);
670 /* for STDIO, we handle the case where several clients use it
673 #define TERM_FIFO_MAX_SIZE 1
675 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
676 static int term_fifo_size
;
678 static int stdio_read_poll(void *opaque
)
680 CharDriverState
*chr
= opaque
;
682 /* try to flush the queue if needed */
683 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
684 qemu_chr_read(chr
, term_fifo
, 1);
687 /* see if we can absorb more chars */
688 if (term_fifo_size
== 0)
694 static void stdio_read(void *opaque
)
698 CharDriverState
*chr
= opaque
;
700 size
= read(0, buf
, 1);
702 /* stdin has been closed. Remove it from the active list. */
703 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
704 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
708 if (qemu_chr_can_read(chr
) > 0) {
709 qemu_chr_read(chr
, buf
, 1);
710 } else if (term_fifo_size
== 0) {
711 term_fifo
[term_fifo_size
++] = buf
[0];
716 /* init terminal so that we can grab keys */
717 static struct termios oldtty
;
718 static int old_fd0_flags
;
719 static int term_atexit_done
;
721 static void term_exit(void)
723 tcsetattr (0, TCSANOW
, &oldtty
);
724 fcntl(0, F_SETFL
, old_fd0_flags
);
727 static void term_init(QemuOpts
*opts
)
733 old_fd0_flags
= fcntl(0, F_GETFL
);
735 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
736 |INLCR
|IGNCR
|ICRNL
|IXON
);
737 tty
.c_oflag
|= OPOST
;
738 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
739 /* if graphical mode, we allow Ctrl-C handling */
740 if (!qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
))
741 tty
.c_lflag
&= ~ISIG
;
742 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
747 tcsetattr (0, TCSANOW
, &tty
);
749 if (!term_atexit_done
++)
752 fcntl(0, F_SETFL
, O_NONBLOCK
);
755 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
759 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
763 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
765 CharDriverState
*chr
;
767 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
769 chr
= qemu_chr_open_fd(0, 1);
770 chr
->chr_close
= qemu_chr_close_stdio
;
771 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
779 /* Once Solaris has openpty(), this is going to be removed. */
780 static int openpty(int *amaster
, int *aslave
, char *name
,
781 struct termios
*termp
, struct winsize
*winp
)
784 int mfd
= -1, sfd
= -1;
786 *amaster
= *aslave
= -1;
788 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
792 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
795 if ((slave
= ptsname(mfd
)) == NULL
)
798 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
801 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
802 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
810 ioctl(sfd
, TIOCSWINSZ
, winp
);
821 static void cfmakeraw (struct termios
*termios_p
)
823 termios_p
->c_iflag
&=
824 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
825 termios_p
->c_oflag
&= ~OPOST
;
826 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
827 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
828 termios_p
->c_cflag
|= CS8
;
830 termios_p
->c_cc
[VMIN
] = 0;
831 termios_p
->c_cc
[VTIME
] = 0;
835 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
836 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
837 || defined(__GLIBC__)
847 static void pty_chr_update_read_handler(CharDriverState
*chr
);
848 static void pty_chr_state(CharDriverState
*chr
, int connected
);
850 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
852 PtyCharDriver
*s
= chr
->opaque
;
855 /* guest sends data, check for (re-)connect */
856 pty_chr_update_read_handler(chr
);
859 return send_all(s
->fd
, buf
, len
);
862 static int pty_chr_read_poll(void *opaque
)
864 CharDriverState
*chr
= opaque
;
865 PtyCharDriver
*s
= chr
->opaque
;
867 s
->read_bytes
= qemu_chr_can_read(chr
);
868 return s
->read_bytes
;
871 static void pty_chr_read(void *opaque
)
873 CharDriverState
*chr
= opaque
;
874 PtyCharDriver
*s
= chr
->opaque
;
876 uint8_t buf
[READ_BUF_LEN
];
879 if (len
> s
->read_bytes
)
883 size
= read(s
->fd
, buf
, len
);
884 if ((size
== -1 && errno
== EIO
) ||
886 pty_chr_state(chr
, 0);
890 pty_chr_state(chr
, 1);
891 qemu_chr_read(chr
, buf
, size
);
895 static void pty_chr_update_read_handler(CharDriverState
*chr
)
897 PtyCharDriver
*s
= chr
->opaque
;
899 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
900 pty_chr_read
, NULL
, chr
);
903 * Short timeout here: just need wait long enougth that qemu makes
904 * it through the poll loop once. When reconnected we want a
905 * short timeout so we notice it almost instantly. Otherwise
906 * read() gives us -EIO instantly, making pty_chr_state() reset the
907 * timeout to the normal (much longer) poll interval before the
910 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
913 static void pty_chr_state(CharDriverState
*chr
, int connected
)
915 PtyCharDriver
*s
= chr
->opaque
;
918 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
921 /* (re-)connect poll interval for idle guests: once per second.
922 * We check more frequently in case the guests sends data to
923 * the virtual device linked to our pty. */
924 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
927 qemu_chr_generic_open(chr
);
932 static void pty_chr_timer(void *opaque
)
934 struct CharDriverState
*chr
= opaque
;
935 PtyCharDriver
*s
= chr
->opaque
;
940 /* If we arrive here without polling being cleared due
941 * read returning -EIO, then we are (re-)connected */
942 pty_chr_state(chr
, 1);
947 pty_chr_update_read_handler(chr
);
950 static void pty_chr_close(struct CharDriverState
*chr
)
952 PtyCharDriver
*s
= chr
->opaque
;
954 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
956 qemu_del_timer(s
->timer
);
957 qemu_free_timer(s
->timer
);
959 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
962 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
964 CharDriverState
*chr
;
968 #if defined(__OpenBSD__) || defined(__DragonFly__)
969 char pty_name
[PATH_MAX
];
970 #define q_ptsname(x) pty_name
972 char *pty_name
= NULL
;
973 #define q_ptsname(x) ptsname(x)
976 chr
= qemu_mallocz(sizeof(CharDriverState
));
977 s
= qemu_mallocz(sizeof(PtyCharDriver
));
979 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
983 /* Set raw attributes on the pty. */
984 tcgetattr(slave_fd
, &tty
);
986 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
989 len
= strlen(q_ptsname(s
->fd
)) + 5;
990 chr
->filename
= qemu_malloc(len
);
991 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
992 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
993 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
996 chr
->chr_write
= pty_chr_write
;
997 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
998 chr
->chr_close
= pty_chr_close
;
1000 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
1005 static void tty_serial_init(int fd
, int speed
,
1006 int parity
, int data_bits
, int stop_bits
)
1012 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1013 speed
, parity
, data_bits
, stop_bits
);
1015 tcgetattr (fd
, &tty
);
1016 if (!term_atexit_done
) {
1020 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1021 speed
= speed
* 10 / 11;
1038 /* Non-Posix values follow. They may be unsupported on some systems. */
1040 check_speed(115200);
1042 check_speed(230400);
1045 check_speed(460800);
1048 check_speed(500000);
1051 check_speed(576000);
1054 check_speed(921600);
1057 check_speed(1000000);
1060 check_speed(1152000);
1063 check_speed(1500000);
1066 check_speed(2000000);
1069 check_speed(2500000);
1072 check_speed(3000000);
1075 check_speed(3500000);
1078 check_speed(4000000);
1083 cfsetispeed(&tty
, spd
);
1084 cfsetospeed(&tty
, spd
);
1086 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1087 |INLCR
|IGNCR
|ICRNL
|IXON
);
1088 tty
.c_oflag
|= OPOST
;
1089 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1090 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1111 tty
.c_cflag
|= PARENB
;
1114 tty
.c_cflag
|= PARENB
| PARODD
;
1118 tty
.c_cflag
|= CSTOPB
;
1120 tcsetattr (fd
, TCSANOW
, &tty
);
1123 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1125 FDCharDriver
*s
= chr
->opaque
;
1128 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1130 QEMUSerialSetParams
*ssp
= arg
;
1131 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1132 ssp
->data_bits
, ssp
->stop_bits
);
1135 case CHR_IOCTL_SERIAL_SET_BREAK
:
1137 int enable
= *(int *)arg
;
1139 tcsendbreak(s
->fd_in
, 1);
1142 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1145 int *targ
= (int *)arg
;
1146 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1148 if (sarg
& TIOCM_CTS
)
1149 *targ
|= CHR_TIOCM_CTS
;
1150 if (sarg
& TIOCM_CAR
)
1151 *targ
|= CHR_TIOCM_CAR
;
1152 if (sarg
& TIOCM_DSR
)
1153 *targ
|= CHR_TIOCM_DSR
;
1154 if (sarg
& TIOCM_RI
)
1155 *targ
|= CHR_TIOCM_RI
;
1156 if (sarg
& TIOCM_DTR
)
1157 *targ
|= CHR_TIOCM_DTR
;
1158 if (sarg
& TIOCM_RTS
)
1159 *targ
|= CHR_TIOCM_RTS
;
1162 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1164 int sarg
= *(int *)arg
;
1166 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1167 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1168 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1169 if (sarg
& CHR_TIOCM_CTS
)
1171 if (sarg
& CHR_TIOCM_CAR
)
1173 if (sarg
& CHR_TIOCM_DSR
)
1175 if (sarg
& CHR_TIOCM_RI
)
1177 if (sarg
& CHR_TIOCM_DTR
)
1179 if (sarg
& CHR_TIOCM_RTS
)
1181 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1190 static void tty_exit(void)
1192 tcsetattr(0, TCSANOW
, &oldtty
);
1195 static void qemu_chr_close_tty(CharDriverState
*chr
)
1197 FDCharDriver
*s
= chr
->opaque
;
1211 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1213 const char *filename
= qemu_opt_get(opts
, "path");
1214 CharDriverState
*chr
;
1217 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1221 tty_serial_init(fd
, 115200, 'N', 8, 1);
1222 chr
= qemu_chr_open_fd(fd
, fd
);
1227 chr
->chr_ioctl
= tty_serial_ioctl
;
1228 chr
->chr_close
= qemu_chr_close_tty
;
1229 if (!term_atexit_done
++)
1233 #else /* ! __linux__ && ! __sun__ */
1234 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1238 #endif /* __linux__ || __sun__ */
1240 #if defined(__linux__)
1244 } ParallelCharDriver
;
1246 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1248 if (s
->mode
!= mode
) {
1250 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1257 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1259 ParallelCharDriver
*drv
= chr
->opaque
;
1264 case CHR_IOCTL_PP_READ_DATA
:
1265 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1267 *(uint8_t *)arg
= b
;
1269 case CHR_IOCTL_PP_WRITE_DATA
:
1270 b
= *(uint8_t *)arg
;
1271 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1274 case CHR_IOCTL_PP_READ_CONTROL
:
1275 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1277 /* Linux gives only the lowest bits, and no way to know data
1278 direction! For better compatibility set the fixed upper
1280 *(uint8_t *)arg
= b
| 0xc0;
1282 case CHR_IOCTL_PP_WRITE_CONTROL
:
1283 b
= *(uint8_t *)arg
;
1284 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1287 case CHR_IOCTL_PP_READ_STATUS
:
1288 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1290 *(uint8_t *)arg
= b
;
1292 case CHR_IOCTL_PP_DATA_DIR
:
1293 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1296 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1297 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1298 struct ParallelIOArg
*parg
= arg
;
1299 int n
= read(fd
, parg
->buffer
, parg
->count
);
1300 if (n
!= parg
->count
) {
1305 case CHR_IOCTL_PP_EPP_READ
:
1306 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1307 struct ParallelIOArg
*parg
= arg
;
1308 int n
= read(fd
, parg
->buffer
, parg
->count
);
1309 if (n
!= parg
->count
) {
1314 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1315 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1316 struct ParallelIOArg
*parg
= arg
;
1317 int n
= write(fd
, parg
->buffer
, parg
->count
);
1318 if (n
!= parg
->count
) {
1323 case CHR_IOCTL_PP_EPP_WRITE
:
1324 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1325 struct ParallelIOArg
*parg
= arg
;
1326 int n
= write(fd
, parg
->buffer
, parg
->count
);
1327 if (n
!= parg
->count
) {
1338 static void pp_close(CharDriverState
*chr
)
1340 ParallelCharDriver
*drv
= chr
->opaque
;
1343 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1344 ioctl(fd
, PPRELEASE
);
1347 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1350 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1352 const char *filename
= qemu_opt_get(opts
, "path");
1353 CharDriverState
*chr
;
1354 ParallelCharDriver
*drv
;
1357 TFR(fd
= open(filename
, O_RDWR
));
1361 if (ioctl(fd
, PPCLAIM
) < 0) {
1366 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1368 drv
->mode
= IEEE1284_MODE_COMPAT
;
1370 chr
= qemu_mallocz(sizeof(CharDriverState
));
1371 chr
->chr_write
= null_chr_write
;
1372 chr
->chr_ioctl
= pp_ioctl
;
1373 chr
->chr_close
= pp_close
;
1376 qemu_chr_generic_open(chr
);
1380 #endif /* __linux__ */
1382 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1383 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1385 int fd
= (int)(long)chr
->opaque
;
1389 case CHR_IOCTL_PP_READ_DATA
:
1390 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1392 *(uint8_t *)arg
= b
;
1394 case CHR_IOCTL_PP_WRITE_DATA
:
1395 b
= *(uint8_t *)arg
;
1396 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1399 case CHR_IOCTL_PP_READ_CONTROL
:
1400 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1402 *(uint8_t *)arg
= b
;
1404 case CHR_IOCTL_PP_WRITE_CONTROL
:
1405 b
= *(uint8_t *)arg
;
1406 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1409 case CHR_IOCTL_PP_READ_STATUS
:
1410 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1412 *(uint8_t *)arg
= b
;
1420 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1422 const char *filename
= qemu_opt_get(opts
, "path");
1423 CharDriverState
*chr
;
1426 fd
= open(filename
, O_RDWR
);
1430 chr
= qemu_mallocz(sizeof(CharDriverState
));
1431 chr
->opaque
= (void *)(long)fd
;
1432 chr
->chr_write
= null_chr_write
;
1433 chr
->chr_ioctl
= pp_ioctl
;
1442 HANDLE hcom
, hrecv
, hsend
;
1443 OVERLAPPED orecv
, osend
;
1448 #define NSENDBUF 2048
1449 #define NRECVBUF 2048
1450 #define MAXCONNECT 1
1451 #define NTIMEOUT 5000
1453 static int win_chr_poll(void *opaque
);
1454 static int win_chr_pipe_poll(void *opaque
);
1456 static void win_chr_close(CharDriverState
*chr
)
1458 WinCharState
*s
= chr
->opaque
;
1461 CloseHandle(s
->hsend
);
1465 CloseHandle(s
->hrecv
);
1469 CloseHandle(s
->hcom
);
1473 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1475 qemu_del_polling_cb(win_chr_poll
, chr
);
1477 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1480 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1482 WinCharState
*s
= chr
->opaque
;
1484 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1489 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1491 fprintf(stderr
, "Failed CreateEvent\n");
1494 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1496 fprintf(stderr
, "Failed CreateEvent\n");
1500 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1501 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1502 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1503 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1508 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1509 fprintf(stderr
, "Failed SetupComm\n");
1513 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1514 size
= sizeof(COMMCONFIG
);
1515 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1516 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1517 CommConfigDialog(filename
, NULL
, &comcfg
);
1519 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1520 fprintf(stderr
, "Failed SetCommState\n");
1524 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1525 fprintf(stderr
, "Failed SetCommMask\n");
1529 cto
.ReadIntervalTimeout
= MAXDWORD
;
1530 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1531 fprintf(stderr
, "Failed SetCommTimeouts\n");
1535 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1536 fprintf(stderr
, "Failed ClearCommError\n");
1539 qemu_add_polling_cb(win_chr_poll
, chr
);
1547 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1549 WinCharState
*s
= chr
->opaque
;
1550 DWORD len
, ret
, size
, err
;
1553 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1554 s
->osend
.hEvent
= s
->hsend
;
1557 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1559 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1561 err
= GetLastError();
1562 if (err
== ERROR_IO_PENDING
) {
1563 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1581 static int win_chr_read_poll(CharDriverState
*chr
)
1583 WinCharState
*s
= chr
->opaque
;
1585 s
->max_size
= qemu_chr_can_read(chr
);
1589 static void win_chr_readfile(CharDriverState
*chr
)
1591 WinCharState
*s
= chr
->opaque
;
1593 uint8_t buf
[READ_BUF_LEN
];
1596 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1597 s
->orecv
.hEvent
= s
->hrecv
;
1598 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1600 err
= GetLastError();
1601 if (err
== ERROR_IO_PENDING
) {
1602 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1607 qemu_chr_read(chr
, buf
, size
);
1611 static void win_chr_read(CharDriverState
*chr
)
1613 WinCharState
*s
= chr
->opaque
;
1615 if (s
->len
> s
->max_size
)
1616 s
->len
= s
->max_size
;
1620 win_chr_readfile(chr
);
1623 static int win_chr_poll(void *opaque
)
1625 CharDriverState
*chr
= opaque
;
1626 WinCharState
*s
= chr
->opaque
;
1630 ClearCommError(s
->hcom
, &comerr
, &status
);
1631 if (status
.cbInQue
> 0) {
1632 s
->len
= status
.cbInQue
;
1633 win_chr_read_poll(chr
);
1640 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1642 const char *filename
= qemu_opt_get(opts
, "path");
1643 CharDriverState
*chr
;
1646 chr
= qemu_mallocz(sizeof(CharDriverState
));
1647 s
= qemu_mallocz(sizeof(WinCharState
));
1649 chr
->chr_write
= win_chr_write
;
1650 chr
->chr_close
= win_chr_close
;
1652 if (win_chr_init(chr
, filename
) < 0) {
1657 qemu_chr_generic_open(chr
);
1661 static int win_chr_pipe_poll(void *opaque
)
1663 CharDriverState
*chr
= opaque
;
1664 WinCharState
*s
= chr
->opaque
;
1667 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1670 win_chr_read_poll(chr
);
1677 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1679 WinCharState
*s
= chr
->opaque
;
1687 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1689 fprintf(stderr
, "Failed CreateEvent\n");
1692 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1694 fprintf(stderr
, "Failed CreateEvent\n");
1698 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1699 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1700 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1702 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1703 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1704 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1709 ZeroMemory(&ov
, sizeof(ov
));
1710 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1711 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1713 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1717 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1719 fprintf(stderr
, "Failed GetOverlappedResult\n");
1721 CloseHandle(ov
.hEvent
);
1728 CloseHandle(ov
.hEvent
);
1731 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1740 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1742 const char *filename
= qemu_opt_get(opts
, "path");
1743 CharDriverState
*chr
;
1746 chr
= qemu_mallocz(sizeof(CharDriverState
));
1747 s
= qemu_mallocz(sizeof(WinCharState
));
1749 chr
->chr_write
= win_chr_write
;
1750 chr
->chr_close
= win_chr_close
;
1752 if (win_chr_pipe_init(chr
, filename
) < 0) {
1757 qemu_chr_generic_open(chr
);
1761 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1763 CharDriverState
*chr
;
1766 chr
= qemu_mallocz(sizeof(CharDriverState
));
1767 s
= qemu_mallocz(sizeof(WinCharState
));
1770 chr
->chr_write
= win_chr_write
;
1771 qemu_chr_generic_open(chr
);
1775 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1777 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1780 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1782 const char *file_out
= qemu_opt_get(opts
, "path");
1785 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1786 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1787 if (fd_out
== INVALID_HANDLE_VALUE
)
1790 return qemu_chr_open_win_file(fd_out
);
1792 #endif /* !_WIN32 */
1794 /***********************************************************/
1795 /* UDP Net console */
1799 uint8_t buf
[READ_BUF_LEN
];
1805 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1807 NetCharDriver
*s
= chr
->opaque
;
1809 return send(s
->fd
, (const void *)buf
, len
, 0);
1812 static int udp_chr_read_poll(void *opaque
)
1814 CharDriverState
*chr
= opaque
;
1815 NetCharDriver
*s
= chr
->opaque
;
1817 s
->max_size
= qemu_chr_can_read(chr
);
1819 /* If there were any stray characters in the queue process them
1822 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1823 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1825 s
->max_size
= qemu_chr_can_read(chr
);
1830 static void udp_chr_read(void *opaque
)
1832 CharDriverState
*chr
= opaque
;
1833 NetCharDriver
*s
= chr
->opaque
;
1835 if (s
->max_size
== 0)
1837 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1838 s
->bufptr
= s
->bufcnt
;
1843 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1844 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1846 s
->max_size
= qemu_chr_can_read(chr
);
1850 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1852 NetCharDriver
*s
= chr
->opaque
;
1855 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1856 udp_chr_read
, NULL
, chr
);
1860 static void udp_chr_close(CharDriverState
*chr
)
1862 NetCharDriver
*s
= chr
->opaque
;
1864 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1868 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1871 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1873 CharDriverState
*chr
= NULL
;
1874 NetCharDriver
*s
= NULL
;
1877 chr
= qemu_mallocz(sizeof(CharDriverState
));
1878 s
= qemu_mallocz(sizeof(NetCharDriver
));
1880 fd
= inet_dgram_opts(opts
);
1882 fprintf(stderr
, "inet_dgram_opts failed\n");
1890 chr
->chr_write
= udp_chr_write
;
1891 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1892 chr
->chr_close
= udp_chr_close
;
1905 /***********************************************************/
1906 /* TCP Net console */
1918 static void tcp_chr_accept(void *opaque
);
1920 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1922 TCPCharDriver
*s
= chr
->opaque
;
1924 return send_all(s
->fd
, buf
, len
);
1926 /* XXX: indicate an error ? */
1931 static int tcp_chr_read_poll(void *opaque
)
1933 CharDriverState
*chr
= opaque
;
1934 TCPCharDriver
*s
= chr
->opaque
;
1937 s
->max_size
= qemu_chr_can_read(chr
);
1942 #define IAC_BREAK 243
1943 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1945 uint8_t *buf
, int *size
)
1947 /* Handle any telnet client's basic IAC options to satisfy char by
1948 * char mode with no echo. All IAC options will be removed from
1949 * the buf and the do_telnetopt variable will be used to track the
1950 * state of the width of the IAC information.
1952 * IAC commands come in sets of 3 bytes with the exception of the
1953 * "IAC BREAK" command and the double IAC.
1959 for (i
= 0; i
< *size
; i
++) {
1960 if (s
->do_telnetopt
> 1) {
1961 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1962 /* Double IAC means send an IAC */
1966 s
->do_telnetopt
= 1;
1968 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1969 /* Handle IAC break commands by sending a serial break */
1970 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1975 if (s
->do_telnetopt
>= 4) {
1976 s
->do_telnetopt
= 1;
1979 if ((unsigned char)buf
[i
] == IAC
) {
1980 s
->do_telnetopt
= 2;
1991 static int tcp_get_msgfd(CharDriverState
*chr
)
1993 TCPCharDriver
*s
= chr
->opaque
;
2000 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2002 TCPCharDriver
*s
= chr
->opaque
;
2003 struct cmsghdr
*cmsg
;
2005 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2008 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2009 cmsg
->cmsg_level
!= SOL_SOCKET
||
2010 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2013 fd
= *((int *)CMSG_DATA(cmsg
));
2023 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2025 TCPCharDriver
*s
= chr
->opaque
;
2026 struct msghdr msg
= { NULL
, };
2027 struct iovec iov
[1];
2029 struct cmsghdr cmsg
;
2030 char control
[CMSG_SPACE(sizeof(int))];
2034 iov
[0].iov_base
= buf
;
2035 iov
[0].iov_len
= len
;
2039 msg
.msg_control
= &msg_control
;
2040 msg
.msg_controllen
= sizeof(msg_control
);
2042 ret
= recvmsg(s
->fd
, &msg
, 0);
2043 if (ret
> 0 && s
->is_unix
)
2044 unix_process_msgfd(chr
, &msg
);
2049 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2051 TCPCharDriver
*s
= chr
->opaque
;
2052 return recv(s
->fd
, buf
, len
, 0);
2056 static void tcp_chr_read(void *opaque
)
2058 CharDriverState
*chr
= opaque
;
2059 TCPCharDriver
*s
= chr
->opaque
;
2060 uint8_t buf
[READ_BUF_LEN
];
2063 if (!s
->connected
|| s
->max_size
<= 0)
2066 if (len
> s
->max_size
)
2068 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2070 /* connection closed */
2072 if (s
->listen_fd
>= 0) {
2073 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2075 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2078 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2079 } else if (size
> 0) {
2080 if (s
->do_telnetopt
)
2081 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2083 qemu_chr_read(chr
, buf
, size
);
2088 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2090 return qemu_chr_open_fd(eventfd
, eventfd
);
2094 static void tcp_chr_connect(void *opaque
)
2096 CharDriverState
*chr
= opaque
;
2097 TCPCharDriver
*s
= chr
->opaque
;
2100 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2101 tcp_chr_read
, NULL
, chr
);
2102 qemu_chr_generic_open(chr
);
2105 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2106 static void tcp_chr_telnet_init(int fd
)
2109 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2110 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2111 send(fd
, (char *)buf
, 3, 0);
2112 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2113 send(fd
, (char *)buf
, 3, 0);
2114 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2115 send(fd
, (char *)buf
, 3, 0);
2116 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2117 send(fd
, (char *)buf
, 3, 0);
2120 static void socket_set_nodelay(int fd
)
2123 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2126 static void tcp_chr_accept(void *opaque
)
2128 CharDriverState
*chr
= opaque
;
2129 TCPCharDriver
*s
= chr
->opaque
;
2130 struct sockaddr_in saddr
;
2132 struct sockaddr_un uaddr
;
2134 struct sockaddr
*addr
;
2141 len
= sizeof(uaddr
);
2142 addr
= (struct sockaddr
*)&uaddr
;
2146 len
= sizeof(saddr
);
2147 addr
= (struct sockaddr
*)&saddr
;
2149 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2150 if (fd
< 0 && errno
!= EINTR
) {
2152 } else if (fd
>= 0) {
2153 if (s
->do_telnetopt
)
2154 tcp_chr_telnet_init(fd
);
2158 socket_set_nonblock(fd
);
2160 socket_set_nodelay(fd
);
2162 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2163 tcp_chr_connect(chr
);
2166 static void tcp_chr_close(CharDriverState
*chr
)
2168 TCPCharDriver
*s
= chr
->opaque
;
2170 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2173 if (s
->listen_fd
>= 0) {
2174 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2175 closesocket(s
->listen_fd
);
2178 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2181 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2183 CharDriverState
*chr
= NULL
;
2184 TCPCharDriver
*s
= NULL
;
2192 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2193 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2194 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2195 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2196 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2200 chr
= qemu_mallocz(sizeof(CharDriverState
));
2201 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2205 fd
= unix_listen_opts(opts
);
2207 fd
= unix_connect_opts(opts
);
2211 fd
= inet_listen_opts(opts
, 0);
2213 fd
= inet_connect_opts(opts
);
2219 if (!is_waitconnect
)
2220 socket_set_nonblock(fd
);
2226 s
->is_unix
= is_unix
;
2227 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2230 chr
->chr_write
= tcp_chr_write
;
2231 chr
->chr_close
= tcp_chr_close
;
2232 chr
->get_msgfd
= tcp_get_msgfd
;
2236 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2238 s
->do_telnetopt
= 1;
2243 socket_set_nodelay(fd
);
2244 tcp_chr_connect(chr
);
2247 /* for "info chardev" monitor command */
2248 chr
->filename
= qemu_malloc(256);
2250 snprintf(chr
->filename
, 256, "unix:%s%s",
2251 qemu_opt_get(opts
, "path"),
2252 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2253 } else if (is_telnet
) {
2254 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2255 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2256 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2258 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2259 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2260 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2263 if (is_listen
&& is_waitconnect
) {
2264 printf("QEMU waiting for connection on: %s\n",
2266 tcp_chr_accept(chr
);
2267 socket_set_nonblock(s
->listen_fd
);
2279 /***********************************************************/
2280 /* Memory chardev */
2283 size_t outbuf_capacity
;
2287 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2289 MemoryDriver
*d
= chr
->opaque
;
2291 /* TODO: the QString implementation has the same code, we should
2292 * introduce a generic way to do this in cutils.c */
2293 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2295 d
->outbuf_capacity
+= len
;
2296 d
->outbuf_capacity
*= 2;
2297 d
->outbuf
= qemu_realloc(d
->outbuf
, d
->outbuf_capacity
);
2300 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2301 d
->outbuf_size
+= len
;
2306 void qemu_chr_init_mem(CharDriverState
*chr
)
2310 d
= qemu_malloc(sizeof(*d
));
2312 d
->outbuf_capacity
= 4096;
2313 d
->outbuf
= qemu_mallocz(d
->outbuf_capacity
);
2315 memset(chr
, 0, sizeof(*chr
));
2317 chr
->chr_write
= mem_chr_write
;
2320 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2322 MemoryDriver
*d
= chr
->opaque
;
2323 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2326 /* NOTE: this driver can not be closed with qemu_chr_close()! */
2327 void qemu_chr_close_mem(CharDriverState
*chr
)
2329 MemoryDriver
*d
= chr
->opaque
;
2331 qemu_free(d
->outbuf
);
2332 qemu_free(chr
->opaque
);
2334 chr
->chr_write
= NULL
;
2337 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2339 const MemoryDriver
*d
= chr
->opaque
;
2340 return d
->outbuf_size
;
2343 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2345 char host
[65], port
[33], width
[8], height
[8];
2350 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1);
2354 if (strstart(filename
, "mon:", &p
)) {
2356 qemu_opt_set(opts
, "mux", "on");
2359 if (strcmp(filename
, "null") == 0 ||
2360 strcmp(filename
, "pty") == 0 ||
2361 strcmp(filename
, "msmouse") == 0 ||
2362 strcmp(filename
, "braille") == 0 ||
2363 strcmp(filename
, "stdio") == 0) {
2364 qemu_opt_set(opts
, "backend", filename
);
2367 if (strstart(filename
, "vc", &p
)) {
2368 qemu_opt_set(opts
, "backend", "vc");
2370 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2372 qemu_opt_set(opts
, "width", width
);
2373 qemu_opt_set(opts
, "height", height
);
2374 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2376 qemu_opt_set(opts
, "cols", width
);
2377 qemu_opt_set(opts
, "rows", height
);
2384 if (strcmp(filename
, "con:") == 0) {
2385 qemu_opt_set(opts
, "backend", "console");
2388 if (strstart(filename
, "COM", NULL
)) {
2389 qemu_opt_set(opts
, "backend", "serial");
2390 qemu_opt_set(opts
, "path", filename
);
2393 if (strstart(filename
, "file:", &p
)) {
2394 qemu_opt_set(opts
, "backend", "file");
2395 qemu_opt_set(opts
, "path", p
);
2398 if (strstart(filename
, "pipe:", &p
)) {
2399 qemu_opt_set(opts
, "backend", "pipe");
2400 qemu_opt_set(opts
, "path", p
);
2403 if (strstart(filename
, "tcp:", &p
) ||
2404 strstart(filename
, "telnet:", &p
)) {
2405 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2407 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2410 qemu_opt_set(opts
, "backend", "socket");
2411 qemu_opt_set(opts
, "host", host
);
2412 qemu_opt_set(opts
, "port", port
);
2413 if (p
[pos
] == ',') {
2414 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2417 if (strstart(filename
, "telnet:", &p
))
2418 qemu_opt_set(opts
, "telnet", "on");
2421 if (strstart(filename
, "udp:", &p
)) {
2422 qemu_opt_set(opts
, "backend", "udp");
2423 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2425 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2429 qemu_opt_set(opts
, "host", host
);
2430 qemu_opt_set(opts
, "port", port
);
2431 if (p
[pos
] == '@') {
2433 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2435 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2439 qemu_opt_set(opts
, "localaddr", host
);
2440 qemu_opt_set(opts
, "localport", port
);
2444 if (strstart(filename
, "unix:", &p
)) {
2445 qemu_opt_set(opts
, "backend", "socket");
2446 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2450 if (strstart(filename
, "/dev/parport", NULL
) ||
2451 strstart(filename
, "/dev/ppi", NULL
)) {
2452 qemu_opt_set(opts
, "backend", "parport");
2453 qemu_opt_set(opts
, "path", filename
);
2456 if (strstart(filename
, "/dev/", NULL
)) {
2457 qemu_opt_set(opts
, "backend", "tty");
2458 qemu_opt_set(opts
, "path", filename
);
2463 qemu_opts_del(opts
);
2467 static const struct {
2469 CharDriverState
*(*open
)(QemuOpts
*opts
);
2470 } backend_table
[] = {
2471 { .name
= "null", .open
= qemu_chr_open_null
},
2472 { .name
= "socket", .open
= qemu_chr_open_socket
},
2473 { .name
= "udp", .open
= qemu_chr_open_udp
},
2474 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2475 { .name
= "vc", .open
= text_console_init
},
2477 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2478 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2479 { .name
= "console", .open
= qemu_chr_open_win_con
},
2480 { .name
= "serial", .open
= qemu_chr_open_win
},
2482 { .name
= "file", .open
= qemu_chr_open_file_out
},
2483 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2484 { .name
= "pty", .open
= qemu_chr_open_pty
},
2485 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2487 #ifdef CONFIG_BRLAPI
2488 { .name
= "braille", .open
= chr_baum_init
},
2490 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2491 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2492 || defined(__FreeBSD_kernel__)
2493 { .name
= "tty", .open
= qemu_chr_open_tty
},
2495 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2496 || defined(__FreeBSD_kernel__)
2497 { .name
= "parport", .open
= qemu_chr_open_pp
},
2500 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
2504 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2505 void (*init
)(struct CharDriverState
*s
))
2507 CharDriverState
*chr
;
2510 if (qemu_opts_id(opts
) == NULL
) {
2511 fprintf(stderr
, "chardev: no id specified\n");
2515 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2516 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2519 if (i
== ARRAY_SIZE(backend_table
)) {
2520 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2521 qemu_opt_get(opts
, "backend"));
2525 chr
= backend_table
[i
].open(opts
);
2527 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2528 qemu_opt_get(opts
, "backend"));
2533 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2535 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2537 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2538 CharDriverState
*base
= chr
;
2539 int len
= strlen(qemu_opts_id(opts
)) + 6;
2540 base
->label
= qemu_malloc(len
);
2541 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2542 chr
= qemu_chr_open_mux(base
);
2543 chr
->filename
= base
->filename
;
2544 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2546 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2550 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2553 CharDriverState
*chr
;
2556 if (strstart(filename
, "chardev:", &p
)) {
2557 return qemu_chr_find(p
);
2560 opts
= qemu_chr_parse_compat(label
, filename
);
2564 chr
= qemu_chr_open_opts(opts
, init
);
2565 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2566 monitor_init(chr
, MONITOR_USE_READLINE
);
2571 void qemu_chr_close(CharDriverState
*chr
)
2573 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2575 chr
->chr_close(chr
);
2576 qemu_free(chr
->filename
);
2577 qemu_free(chr
->label
);
2581 static void qemu_chr_qlist_iter(QObject
*obj
, void *opaque
)
2584 Monitor
*mon
= opaque
;
2586 chr_dict
= qobject_to_qdict(obj
);
2587 monitor_printf(mon
, "%s: filename=%s\n", qdict_get_str(chr_dict
, "label"),
2588 qdict_get_str(chr_dict
, "filename"));
2591 void qemu_chr_info_print(Monitor
*mon
, const QObject
*ret_data
)
2593 qlist_iter(qobject_to_qlist(ret_data
), qemu_chr_qlist_iter
, mon
);
2596 void qemu_chr_info(Monitor
*mon
, QObject
**ret_data
)
2599 CharDriverState
*chr
;
2601 chr_list
= qlist_new();
2603 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2604 QObject
*obj
= qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2605 chr
->label
, chr
->filename
);
2606 qlist_append_obj(chr_list
, obj
);
2609 *ret_data
= QOBJECT(chr_list
);
2612 CharDriverState
*qemu_chr_find(const char *name
)
2614 CharDriverState
*chr
;
2616 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2617 if (strcmp(chr
->label
, name
) != 0)