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"
25 #include "monitor/monitor.h"
26 #include "ui/console.h"
27 #include "sysemu/sysemu.h"
28 #include "qemu/timer.h"
29 #include "char/char.h"
32 #include "hw/msmouse.h"
33 #include "qmp-commands.h"
43 #include <sys/times.h>
47 #include <sys/ioctl.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
52 #include <arpa/inet.h>
55 #include <sys/select.h>
58 #if defined(__GLIBC__)
60 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
65 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
66 #include <dev/ppbus/ppi.h>
67 #include <dev/ppbus/ppbconf.h>
68 #elif defined(__DragonFly__)
69 #include <dev/misc/ppi/ppi.h>
70 #include <bus/ppbus/ppbconf.h>
76 #include <linux/ppdev.h>
77 #include <linux/parport.h>
81 #include <sys/ethernet.h>
82 #include <sys/sockio.h>
83 #include <netinet/arp.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_icmp.h> // must come after ip.h
88 #include <netinet/udp.h>
89 #include <netinet/tcp.h>
97 #include "qemu/sockets.h"
98 #include "ui/qemu-spice.h"
100 #define READ_BUF_LEN 4096
102 /***********************************************************/
103 /* character device */
105 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
106 QTAILQ_HEAD_INITIALIZER(chardevs
);
108 void qemu_chr_be_event(CharDriverState
*s
, int event
)
110 /* Keep track if the char device is open */
112 case CHR_EVENT_OPENED
:
115 case CHR_EVENT_CLOSED
:
122 s
->chr_event(s
->handler_opaque
, event
);
125 static void qemu_chr_fire_open_event(void *opaque
)
127 CharDriverState
*s
= opaque
;
128 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
129 qemu_free_timer(s
->open_timer
);
130 s
->open_timer
= NULL
;
133 void qemu_chr_generic_open(CharDriverState
*s
)
135 if (s
->open_timer
== NULL
) {
136 s
->open_timer
= qemu_new_timer_ms(rt_clock
,
137 qemu_chr_fire_open_event
, s
);
138 qemu_mod_timer(s
->open_timer
, qemu_get_clock_ms(rt_clock
) - 1);
142 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
144 return s
->chr_write(s
, buf
, len
);
147 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
151 return s
->chr_ioctl(s
, cmd
, arg
);
154 int qemu_chr_be_can_write(CharDriverState
*s
)
156 if (!s
->chr_can_read
)
158 return s
->chr_can_read(s
->handler_opaque
);
161 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
164 s
->chr_read(s
->handler_opaque
, buf
, len
);
168 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
170 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
173 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
175 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
178 void qemu_chr_accept_input(CharDriverState
*s
)
180 if (s
->chr_accept_input
)
181 s
->chr_accept_input(s
);
185 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
187 char buf
[READ_BUF_LEN
];
190 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
191 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
195 void qemu_chr_add_handlers(CharDriverState
*s
,
196 IOCanReadHandler
*fd_can_read
,
197 IOReadHandler
*fd_read
,
198 IOEventHandler
*fd_event
,
201 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
202 /* chr driver being released. */
203 ++s
->avail_connections
;
205 s
->chr_can_read
= fd_can_read
;
206 s
->chr_read
= fd_read
;
207 s
->chr_event
= fd_event
;
208 s
->handler_opaque
= opaque
;
209 if (s
->chr_update_read_handler
)
210 s
->chr_update_read_handler(s
);
212 /* We're connecting to an already opened device, so let's make sure we
213 also get the open event */
215 qemu_chr_generic_open(s
);
219 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
224 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
226 CharDriverState
*chr
;
228 chr
= g_malloc0(sizeof(CharDriverState
));
229 chr
->chr_write
= null_chr_write
;
233 /* MUX driver for serial I/O splitting */
235 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
236 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
238 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
239 IOReadHandler
*chr_read
[MAX_MUX
];
240 IOEventHandler
*chr_event
[MAX_MUX
];
241 void *ext_opaque
[MAX_MUX
];
242 CharDriverState
*drv
;
247 /* Intermediate input buffer allows to catch escape sequences even if the
248 currently active device is not accepting any input - but only until it
250 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
255 int64_t timestamps_start
;
259 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
261 MuxDriver
*d
= chr
->opaque
;
263 if (!d
->timestamps
) {
264 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
269 for (i
= 0; i
< len
; i
++) {
275 ti
= qemu_get_clock_ms(rt_clock
);
276 if (d
->timestamps_start
== -1)
277 d
->timestamps_start
= ti
;
278 ti
-= d
->timestamps_start
;
280 snprintf(buf1
, sizeof(buf1
),
281 "[%02d:%02d:%02d.%03d] ",
286 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
289 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
290 if (buf
[i
] == '\n') {
298 static const char * const mux_help
[] = {
299 "% h print this help\n\r",
300 "% x exit emulator\n\r",
301 "% s save disk data back to file (if -snapshot)\n\r",
302 "% t toggle console timestamps\n\r"
303 "% b send break (magic sysrq)\n\r",
304 "% c switch between console and monitor\n\r",
309 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
310 static void mux_print_help(CharDriverState
*chr
)
313 char ebuf
[15] = "Escape-Char";
314 char cbuf
[50] = "\n\r";
316 if (term_escape_char
> 0 && term_escape_char
< 26) {
317 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
318 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
320 snprintf(cbuf
, sizeof(cbuf
),
321 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
324 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
325 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
326 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
327 if (mux_help
[i
][j
] == '%')
328 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
330 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
335 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
337 if (d
->chr_event
[mux_nr
])
338 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
341 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
343 if (d
->term_got_escape
) {
344 d
->term_got_escape
= 0;
345 if (ch
== term_escape_char
)
354 const char *term
= "QEMU: Terminated\n\r";
355 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
363 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
366 /* Switch to the next registered device */
367 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
369 if (d
->focus
>= d
->mux_cnt
)
371 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
374 d
->timestamps
= !d
->timestamps
;
375 d
->timestamps_start
= -1;
379 } else if (ch
== term_escape_char
) {
380 d
->term_got_escape
= 1;
388 static void mux_chr_accept_input(CharDriverState
*chr
)
390 MuxDriver
*d
= chr
->opaque
;
393 while (d
->prod
[m
] != d
->cons
[m
] &&
394 d
->chr_can_read
[m
] &&
395 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
396 d
->chr_read
[m
](d
->ext_opaque
[m
],
397 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
401 static int mux_chr_can_read(void *opaque
)
403 CharDriverState
*chr
= opaque
;
404 MuxDriver
*d
= chr
->opaque
;
407 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
409 if (d
->chr_can_read
[m
])
410 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
414 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
416 CharDriverState
*chr
= opaque
;
417 MuxDriver
*d
= chr
->opaque
;
421 mux_chr_accept_input (opaque
);
423 for(i
= 0; i
< size
; i
++)
424 if (mux_proc_byte(chr
, d
, buf
[i
])) {
425 if (d
->prod
[m
] == d
->cons
[m
] &&
426 d
->chr_can_read
[m
] &&
427 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
428 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
430 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
434 static void mux_chr_event(void *opaque
, int event
)
436 CharDriverState
*chr
= opaque
;
437 MuxDriver
*d
= chr
->opaque
;
440 /* Send the event to all registered listeners */
441 for (i
= 0; i
< d
->mux_cnt
; i
++)
442 mux_chr_send_event(d
, i
, event
);
445 static void mux_chr_update_read_handler(CharDriverState
*chr
)
447 MuxDriver
*d
= chr
->opaque
;
449 if (d
->mux_cnt
>= MAX_MUX
) {
450 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
453 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
454 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
455 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
456 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
457 /* Fix up the real driver with mux routines */
458 if (d
->mux_cnt
== 0) {
459 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
462 if (d
->focus
!= -1) {
463 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
465 d
->focus
= d
->mux_cnt
;
467 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
470 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
472 CharDriverState
*chr
;
475 chr
= g_malloc0(sizeof(CharDriverState
));
476 d
= g_malloc0(sizeof(MuxDriver
));
481 chr
->chr_write
= mux_chr_write
;
482 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
483 chr
->chr_accept_input
= mux_chr_accept_input
;
484 /* Frontend guest-open / -close notification is not support with muxes */
485 chr
->chr_guest_open
= NULL
;
486 chr
->chr_guest_close
= NULL
;
488 /* Muxes are always open on creation */
489 qemu_chr_generic_open(chr
);
496 int send_all(int fd
, const void *buf
, int len1
)
502 ret
= send(fd
, buf
, len
, 0);
504 errno
= WSAGetLastError();
505 if (errno
!= WSAEWOULDBLOCK
) {
508 } else if (ret
== 0) {
520 int send_all(int fd
, const void *_buf
, int len1
)
523 const uint8_t *buf
= _buf
;
527 ret
= write(fd
, buf
, len
);
529 if (errno
!= EINTR
&& errno
!= EAGAIN
)
531 } else if (ret
== 0) {
550 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
552 FDCharDriver
*s
= chr
->opaque
;
553 return send_all(s
->fd_out
, buf
, len
);
556 static int fd_chr_read_poll(void *opaque
)
558 CharDriverState
*chr
= opaque
;
559 FDCharDriver
*s
= chr
->opaque
;
561 s
->max_size
= qemu_chr_be_can_write(chr
);
565 static void fd_chr_read(void *opaque
)
567 CharDriverState
*chr
= opaque
;
568 FDCharDriver
*s
= chr
->opaque
;
570 uint8_t buf
[READ_BUF_LEN
];
573 if (len
> s
->max_size
)
577 size
= read(s
->fd_in
, buf
, len
);
579 /* FD has been closed. Remove it from the active list. */
580 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
581 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
585 qemu_chr_be_write(chr
, buf
, size
);
589 static void fd_chr_update_read_handler(CharDriverState
*chr
)
591 FDCharDriver
*s
= chr
->opaque
;
594 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
595 fd_chr_read
, NULL
, chr
);
599 static void fd_chr_close(struct CharDriverState
*chr
)
601 FDCharDriver
*s
= chr
->opaque
;
604 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
608 qemu_chr_be_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
= g_malloc0(sizeof(CharDriverState
));
618 s
= g_malloc0(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));
640 return qemu_chr_open_fd(-1, fd_out
);
643 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
646 char filename_in
[256], filename_out
[256];
647 const char *filename
= qemu_opt_get(opts
, "path");
649 if (filename
== NULL
) {
650 fprintf(stderr
, "chardev: pipe: no filename given\n");
654 snprintf(filename_in
, 256, "%s.in", filename
);
655 snprintf(filename_out
, 256, "%s.out", filename
);
656 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
657 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
658 if (fd_in
< 0 || fd_out
< 0) {
663 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
668 return qemu_chr_open_fd(fd_in
, fd_out
);
671 /* init terminal so that we can grab keys */
672 static struct termios oldtty
;
673 static int old_fd0_flags
;
674 static bool stdio_allow_signal
;
676 static void term_exit(void)
678 tcsetattr (0, TCSANOW
, &oldtty
);
679 fcntl(0, F_SETFL
, old_fd0_flags
);
682 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
688 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
689 |INLCR
|IGNCR
|ICRNL
|IXON
);
690 tty
.c_oflag
|= OPOST
;
691 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
692 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
697 /* if graphical mode, we allow Ctrl-C handling */
698 if (!stdio_allow_signal
)
699 tty
.c_lflag
&= ~ISIG
;
701 tcsetattr (0, TCSANOW
, &tty
);
704 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
710 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
712 CharDriverState
*chr
;
714 if (is_daemonized()) {
715 error_report("cannot use stdio with -daemonize");
718 old_fd0_flags
= fcntl(0, F_GETFL
);
719 tcgetattr (0, &oldtty
);
720 fcntl(0, F_SETFL
, O_NONBLOCK
);
723 chr
= qemu_chr_open_fd(0, 1);
724 chr
->chr_close
= qemu_chr_close_stdio
;
725 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
726 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
727 display_type
!= DT_NOGRAPHIC
);
728 qemu_chr_fe_set_echo(chr
, false);
734 /* Once Solaris has openpty(), this is going to be removed. */
735 static int openpty(int *amaster
, int *aslave
, char *name
,
736 struct termios
*termp
, struct winsize
*winp
)
739 int mfd
= -1, sfd
= -1;
741 *amaster
= *aslave
= -1;
743 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
747 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
750 if ((slave
= ptsname(mfd
)) == NULL
)
753 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
756 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
757 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
765 ioctl(sfd
, TIOCSWINSZ
, winp
);
776 static void cfmakeraw (struct termios
*termios_p
)
778 termios_p
->c_iflag
&=
779 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
780 termios_p
->c_oflag
&= ~OPOST
;
781 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
782 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
783 termios_p
->c_cflag
|= CS8
;
785 termios_p
->c_cc
[VMIN
] = 0;
786 termios_p
->c_cc
[VTIME
] = 0;
790 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
791 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
792 || defined(__GLIBC__)
794 #define HAVE_CHARDEV_TTY 1
804 static void pty_chr_update_read_handler(CharDriverState
*chr
);
805 static void pty_chr_state(CharDriverState
*chr
, int connected
);
807 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
809 PtyCharDriver
*s
= chr
->opaque
;
812 /* guest sends data, check for (re-)connect */
813 pty_chr_update_read_handler(chr
);
816 return send_all(s
->fd
, buf
, len
);
819 static int pty_chr_read_poll(void *opaque
)
821 CharDriverState
*chr
= opaque
;
822 PtyCharDriver
*s
= chr
->opaque
;
824 s
->read_bytes
= qemu_chr_be_can_write(chr
);
825 return s
->read_bytes
;
828 static void pty_chr_read(void *opaque
)
830 CharDriverState
*chr
= opaque
;
831 PtyCharDriver
*s
= chr
->opaque
;
833 uint8_t buf
[READ_BUF_LEN
];
836 if (len
> s
->read_bytes
)
840 size
= read(s
->fd
, buf
, len
);
841 if ((size
== -1 && errno
== EIO
) ||
843 pty_chr_state(chr
, 0);
847 pty_chr_state(chr
, 1);
848 qemu_chr_be_write(chr
, buf
, size
);
852 static void pty_chr_update_read_handler(CharDriverState
*chr
)
854 PtyCharDriver
*s
= chr
->opaque
;
856 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
857 pty_chr_read
, NULL
, chr
);
860 * Short timeout here: just need wait long enougth that qemu makes
861 * it through the poll loop once. When reconnected we want a
862 * short timeout so we notice it almost instantly. Otherwise
863 * read() gives us -EIO instantly, making pty_chr_state() reset the
864 * timeout to the normal (much longer) poll interval before the
867 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
870 static void pty_chr_state(CharDriverState
*chr
, int connected
)
872 PtyCharDriver
*s
= chr
->opaque
;
875 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
878 /* (re-)connect poll interval for idle guests: once per second.
879 * We check more frequently in case the guests sends data to
880 * the virtual device linked to our pty. */
881 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
884 qemu_chr_generic_open(chr
);
889 static void pty_chr_timer(void *opaque
)
891 struct CharDriverState
*chr
= opaque
;
892 PtyCharDriver
*s
= chr
->opaque
;
897 /* If we arrive here without polling being cleared due
898 * read returning -EIO, then we are (re-)connected */
899 pty_chr_state(chr
, 1);
904 pty_chr_update_read_handler(chr
);
907 static void pty_chr_close(struct CharDriverState
*chr
)
909 PtyCharDriver
*s
= chr
->opaque
;
911 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
913 qemu_del_timer(s
->timer
);
914 qemu_free_timer(s
->timer
);
916 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
919 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
921 CharDriverState
*chr
;
925 int master_fd
, slave_fd
, len
;
926 #if defined(__OpenBSD__) || defined(__DragonFly__)
927 char pty_name
[PATH_MAX
];
928 #define q_ptsname(x) pty_name
930 char *pty_name
= NULL
;
931 #define q_ptsname(x) ptsname(x)
934 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
938 /* Set raw attributes on the pty. */
939 tcgetattr(slave_fd
, &tty
);
941 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
944 chr
= g_malloc0(sizeof(CharDriverState
));
946 len
= strlen(q_ptsname(master_fd
)) + 5;
947 chr
->filename
= g_malloc(len
);
948 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
949 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
951 label
= qemu_opts_id(opts
);
952 fprintf(stderr
, "char device redirected to %s%s%s%s\n",
953 q_ptsname(master_fd
),
954 label
? " (label " : "",
958 s
= g_malloc0(sizeof(PtyCharDriver
));
960 chr
->chr_write
= pty_chr_write
;
961 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
962 chr
->chr_close
= pty_chr_close
;
965 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
970 static void tty_serial_init(int fd
, int speed
,
971 int parity
, int data_bits
, int stop_bits
)
977 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
978 speed
, parity
, data_bits
, stop_bits
);
980 tcgetattr (fd
, &tty
);
982 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
983 speed
= speed
* 10 / 11;
1000 /* Non-Posix values follow. They may be unsupported on some systems. */
1002 check_speed(115200);
1004 check_speed(230400);
1007 check_speed(460800);
1010 check_speed(500000);
1013 check_speed(576000);
1016 check_speed(921600);
1019 check_speed(1000000);
1022 check_speed(1152000);
1025 check_speed(1500000);
1028 check_speed(2000000);
1031 check_speed(2500000);
1034 check_speed(3000000);
1037 check_speed(3500000);
1040 check_speed(4000000);
1045 cfsetispeed(&tty
, spd
);
1046 cfsetospeed(&tty
, spd
);
1048 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1049 |INLCR
|IGNCR
|ICRNL
|IXON
);
1050 tty
.c_oflag
|= OPOST
;
1051 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1052 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1073 tty
.c_cflag
|= PARENB
;
1076 tty
.c_cflag
|= PARENB
| PARODD
;
1080 tty
.c_cflag
|= CSTOPB
;
1082 tcsetattr (fd
, TCSANOW
, &tty
);
1085 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1087 FDCharDriver
*s
= chr
->opaque
;
1090 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1092 QEMUSerialSetParams
*ssp
= arg
;
1093 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1094 ssp
->data_bits
, ssp
->stop_bits
);
1097 case CHR_IOCTL_SERIAL_SET_BREAK
:
1099 int enable
= *(int *)arg
;
1101 tcsendbreak(s
->fd_in
, 1);
1104 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1107 int *targ
= (int *)arg
;
1108 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1110 if (sarg
& TIOCM_CTS
)
1111 *targ
|= CHR_TIOCM_CTS
;
1112 if (sarg
& TIOCM_CAR
)
1113 *targ
|= CHR_TIOCM_CAR
;
1114 if (sarg
& TIOCM_DSR
)
1115 *targ
|= CHR_TIOCM_DSR
;
1116 if (sarg
& TIOCM_RI
)
1117 *targ
|= CHR_TIOCM_RI
;
1118 if (sarg
& TIOCM_DTR
)
1119 *targ
|= CHR_TIOCM_DTR
;
1120 if (sarg
& TIOCM_RTS
)
1121 *targ
|= CHR_TIOCM_RTS
;
1124 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1126 int sarg
= *(int *)arg
;
1128 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1129 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1130 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1131 if (sarg
& CHR_TIOCM_CTS
)
1133 if (sarg
& CHR_TIOCM_CAR
)
1135 if (sarg
& CHR_TIOCM_DSR
)
1137 if (sarg
& CHR_TIOCM_RI
)
1139 if (sarg
& CHR_TIOCM_DTR
)
1141 if (sarg
& CHR_TIOCM_RTS
)
1143 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1152 static void qemu_chr_close_tty(CharDriverState
*chr
)
1154 FDCharDriver
*s
= chr
->opaque
;
1168 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1170 CharDriverState
*chr
;
1172 tty_serial_init(fd
, 115200, 'N', 8, 1);
1173 chr
= qemu_chr_open_fd(fd
, fd
);
1174 chr
->chr_ioctl
= tty_serial_ioctl
;
1175 chr
->chr_close
= qemu_chr_close_tty
;
1179 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1181 const char *filename
= qemu_opt_get(opts
, "path");
1184 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1188 return qemu_chr_open_tty_fd(fd
);
1190 #endif /* __linux__ || __sun__ */
1192 #if defined(__linux__)
1194 #define HAVE_CHARDEV_PARPORT 1
1199 } ParallelCharDriver
;
1201 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1203 if (s
->mode
!= mode
) {
1205 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1212 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1214 ParallelCharDriver
*drv
= chr
->opaque
;
1219 case CHR_IOCTL_PP_READ_DATA
:
1220 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1222 *(uint8_t *)arg
= b
;
1224 case CHR_IOCTL_PP_WRITE_DATA
:
1225 b
= *(uint8_t *)arg
;
1226 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1229 case CHR_IOCTL_PP_READ_CONTROL
:
1230 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1232 /* Linux gives only the lowest bits, and no way to know data
1233 direction! For better compatibility set the fixed upper
1235 *(uint8_t *)arg
= b
| 0xc0;
1237 case CHR_IOCTL_PP_WRITE_CONTROL
:
1238 b
= *(uint8_t *)arg
;
1239 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1242 case CHR_IOCTL_PP_READ_STATUS
:
1243 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1245 *(uint8_t *)arg
= b
;
1247 case CHR_IOCTL_PP_DATA_DIR
:
1248 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1251 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1252 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1253 struct ParallelIOArg
*parg
= arg
;
1254 int n
= read(fd
, parg
->buffer
, parg
->count
);
1255 if (n
!= parg
->count
) {
1260 case CHR_IOCTL_PP_EPP_READ
:
1261 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1262 struct ParallelIOArg
*parg
= arg
;
1263 int n
= read(fd
, parg
->buffer
, parg
->count
);
1264 if (n
!= parg
->count
) {
1269 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1270 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1271 struct ParallelIOArg
*parg
= arg
;
1272 int n
= write(fd
, parg
->buffer
, parg
->count
);
1273 if (n
!= parg
->count
) {
1278 case CHR_IOCTL_PP_EPP_WRITE
:
1279 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1280 struct ParallelIOArg
*parg
= arg
;
1281 int n
= write(fd
, parg
->buffer
, parg
->count
);
1282 if (n
!= parg
->count
) {
1293 static void pp_close(CharDriverState
*chr
)
1295 ParallelCharDriver
*drv
= chr
->opaque
;
1298 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1299 ioctl(fd
, PPRELEASE
);
1302 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1305 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1307 CharDriverState
*chr
;
1308 ParallelCharDriver
*drv
;
1310 if (ioctl(fd
, PPCLAIM
) < 0) {
1315 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1317 drv
->mode
= IEEE1284_MODE_COMPAT
;
1319 chr
= g_malloc0(sizeof(CharDriverState
));
1320 chr
->chr_write
= null_chr_write
;
1321 chr
->chr_ioctl
= pp_ioctl
;
1322 chr
->chr_close
= pp_close
;
1325 qemu_chr_generic_open(chr
);
1329 #endif /* __linux__ */
1331 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1333 #define HAVE_CHARDEV_PARPORT 1
1335 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1337 int fd
= (int)(intptr_t)chr
->opaque
;
1341 case CHR_IOCTL_PP_READ_DATA
:
1342 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1344 *(uint8_t *)arg
= b
;
1346 case CHR_IOCTL_PP_WRITE_DATA
:
1347 b
= *(uint8_t *)arg
;
1348 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1351 case CHR_IOCTL_PP_READ_CONTROL
:
1352 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1354 *(uint8_t *)arg
= b
;
1356 case CHR_IOCTL_PP_WRITE_CONTROL
:
1357 b
= *(uint8_t *)arg
;
1358 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1361 case CHR_IOCTL_PP_READ_STATUS
:
1362 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1364 *(uint8_t *)arg
= b
;
1372 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1374 CharDriverState
*chr
;
1376 chr
= g_malloc0(sizeof(CharDriverState
));
1377 chr
->opaque
= (void *)(intptr_t)fd
;
1378 chr
->chr_write
= null_chr_write
;
1379 chr
->chr_ioctl
= pp_ioctl
;
1388 HANDLE hcom
, hrecv
, hsend
;
1389 OVERLAPPED orecv
, osend
;
1396 HANDLE hInputReadyEvent
;
1397 HANDLE hInputDoneEvent
;
1398 HANDLE hInputThread
;
1399 uint8_t win_stdio_buf
;
1400 } WinStdioCharState
;
1402 #define NSENDBUF 2048
1403 #define NRECVBUF 2048
1404 #define MAXCONNECT 1
1405 #define NTIMEOUT 5000
1407 static int win_chr_poll(void *opaque
);
1408 static int win_chr_pipe_poll(void *opaque
);
1410 static void win_chr_close(CharDriverState
*chr
)
1412 WinCharState
*s
= chr
->opaque
;
1415 CloseHandle(s
->hsend
);
1419 CloseHandle(s
->hrecv
);
1423 CloseHandle(s
->hcom
);
1427 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1429 qemu_del_polling_cb(win_chr_poll
, chr
);
1431 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1434 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1436 WinCharState
*s
= chr
->opaque
;
1438 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1443 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1445 fprintf(stderr
, "Failed CreateEvent\n");
1448 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1450 fprintf(stderr
, "Failed CreateEvent\n");
1454 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1455 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1456 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1457 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1462 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1463 fprintf(stderr
, "Failed SetupComm\n");
1467 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1468 size
= sizeof(COMMCONFIG
);
1469 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1470 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1471 CommConfigDialog(filename
, NULL
, &comcfg
);
1473 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1474 fprintf(stderr
, "Failed SetCommState\n");
1478 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1479 fprintf(stderr
, "Failed SetCommMask\n");
1483 cto
.ReadIntervalTimeout
= MAXDWORD
;
1484 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1485 fprintf(stderr
, "Failed SetCommTimeouts\n");
1489 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1490 fprintf(stderr
, "Failed ClearCommError\n");
1493 qemu_add_polling_cb(win_chr_poll
, chr
);
1501 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1503 WinCharState
*s
= chr
->opaque
;
1504 DWORD len
, ret
, size
, err
;
1507 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1508 s
->osend
.hEvent
= s
->hsend
;
1511 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1513 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1515 err
= GetLastError();
1516 if (err
== ERROR_IO_PENDING
) {
1517 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1535 static int win_chr_read_poll(CharDriverState
*chr
)
1537 WinCharState
*s
= chr
->opaque
;
1539 s
->max_size
= qemu_chr_be_can_write(chr
);
1543 static void win_chr_readfile(CharDriverState
*chr
)
1545 WinCharState
*s
= chr
->opaque
;
1547 uint8_t buf
[READ_BUF_LEN
];
1550 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1551 s
->orecv
.hEvent
= s
->hrecv
;
1552 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1554 err
= GetLastError();
1555 if (err
== ERROR_IO_PENDING
) {
1556 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1561 qemu_chr_be_write(chr
, buf
, size
);
1565 static void win_chr_read(CharDriverState
*chr
)
1567 WinCharState
*s
= chr
->opaque
;
1569 if (s
->len
> s
->max_size
)
1570 s
->len
= s
->max_size
;
1574 win_chr_readfile(chr
);
1577 static int win_chr_poll(void *opaque
)
1579 CharDriverState
*chr
= opaque
;
1580 WinCharState
*s
= chr
->opaque
;
1584 ClearCommError(s
->hcom
, &comerr
, &status
);
1585 if (status
.cbInQue
> 0) {
1586 s
->len
= status
.cbInQue
;
1587 win_chr_read_poll(chr
);
1594 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1596 CharDriverState
*chr
;
1599 chr
= g_malloc0(sizeof(CharDriverState
));
1600 s
= g_malloc0(sizeof(WinCharState
));
1602 chr
->chr_write
= win_chr_write
;
1603 chr
->chr_close
= win_chr_close
;
1605 if (win_chr_init(chr
, filename
) < 0) {
1610 qemu_chr_generic_open(chr
);
1614 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1616 return qemu_chr_open_win_path(qemu_opt_get(opts
, "path"));
1619 static int win_chr_pipe_poll(void *opaque
)
1621 CharDriverState
*chr
= opaque
;
1622 WinCharState
*s
= chr
->opaque
;
1625 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1628 win_chr_read_poll(chr
);
1635 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1637 WinCharState
*s
= chr
->opaque
;
1645 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1647 fprintf(stderr
, "Failed CreateEvent\n");
1650 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1652 fprintf(stderr
, "Failed CreateEvent\n");
1656 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1657 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1658 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1660 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1661 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1662 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1667 ZeroMemory(&ov
, sizeof(ov
));
1668 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1669 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1671 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1675 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1677 fprintf(stderr
, "Failed GetOverlappedResult\n");
1679 CloseHandle(ov
.hEvent
);
1686 CloseHandle(ov
.hEvent
);
1689 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1698 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1700 const char *filename
= qemu_opt_get(opts
, "path");
1701 CharDriverState
*chr
;
1704 chr
= g_malloc0(sizeof(CharDriverState
));
1705 s
= g_malloc0(sizeof(WinCharState
));
1707 chr
->chr_write
= win_chr_write
;
1708 chr
->chr_close
= win_chr_close
;
1710 if (win_chr_pipe_init(chr
, filename
) < 0) {
1715 qemu_chr_generic_open(chr
);
1719 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1721 CharDriverState
*chr
;
1724 chr
= g_malloc0(sizeof(CharDriverState
));
1725 s
= g_malloc0(sizeof(WinCharState
));
1728 chr
->chr_write
= win_chr_write
;
1729 qemu_chr_generic_open(chr
);
1733 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1735 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1738 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1740 const char *file_out
= qemu_opt_get(opts
, "path");
1743 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1744 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1745 if (fd_out
== INVALID_HANDLE_VALUE
) {
1749 return qemu_chr_open_win_file(fd_out
);
1752 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1754 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1761 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1771 static void win_stdio_wait_func(void *opaque
)
1773 CharDriverState
*chr
= opaque
;
1774 WinStdioCharState
*stdio
= chr
->opaque
;
1775 INPUT_RECORD buf
[4];
1780 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1784 /* Avoid error storm */
1785 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1789 for (i
= 0; i
< dwSize
; i
++) {
1790 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1792 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1794 if (kev
->uChar
.AsciiChar
!= 0) {
1795 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1796 if (qemu_chr_be_can_write(chr
)) {
1797 uint8_t c
= kev
->uChar
.AsciiChar
;
1798 qemu_chr_be_write(chr
, &c
, 1);
1806 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
1808 CharDriverState
*chr
= param
;
1809 WinStdioCharState
*stdio
= chr
->opaque
;
1815 /* Wait for one byte */
1816 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
1818 /* Exit in case of error, continue if nothing read */
1826 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1827 if (stdio
->win_stdio_buf
== '\r') {
1831 /* Signal the main thread and wait until the byte was eaten */
1832 if (!SetEvent(stdio
->hInputReadyEvent
)) {
1835 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
1841 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
1845 static void win_stdio_thread_wait_func(void *opaque
)
1847 CharDriverState
*chr
= opaque
;
1848 WinStdioCharState
*stdio
= chr
->opaque
;
1850 if (qemu_chr_be_can_write(chr
)) {
1851 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
1854 SetEvent(stdio
->hInputDoneEvent
);
1857 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
1859 WinStdioCharState
*stdio
= chr
->opaque
;
1862 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
1865 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
1867 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
1871 static void win_stdio_close(CharDriverState
*chr
)
1873 WinStdioCharState
*stdio
= chr
->opaque
;
1875 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
1876 CloseHandle(stdio
->hInputReadyEvent
);
1878 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
1879 CloseHandle(stdio
->hInputDoneEvent
);
1881 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
1882 TerminateThread(stdio
->hInputThread
, 0);
1885 g_free(chr
->opaque
);
1889 static CharDriverState
*qemu_chr_open_win_stdio(QemuOpts
*opts
)
1891 CharDriverState
*chr
;
1892 WinStdioCharState
*stdio
;
1896 chr
= g_malloc0(sizeof(CharDriverState
));
1897 stdio
= g_malloc0(sizeof(WinStdioCharState
));
1899 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
1900 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
1901 fprintf(stderr
, "cannot open stdio: invalid handle\n");
1905 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
1907 chr
->opaque
= stdio
;
1908 chr
->chr_write
= win_stdio_write
;
1909 chr
->chr_close
= win_stdio_close
;
1912 if (qemu_add_wait_object(stdio
->hStdIn
,
1913 win_stdio_wait_func
, chr
)) {
1914 fprintf(stderr
, "qemu_add_wait_object: failed\n");
1919 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1920 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1921 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
1924 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
1925 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
1926 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
1927 fprintf(stderr
, "cannot create stdio thread or event\n");
1930 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
1931 win_stdio_thread_wait_func
, chr
)) {
1932 fprintf(stderr
, "qemu_add_wait_object: failed\n");
1936 dwMode
|= ENABLE_LINE_INPUT
;
1939 /* set the terminal in raw mode */
1940 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
1941 dwMode
|= ENABLE_PROCESSED_INPUT
;
1944 SetConsoleMode(stdio
->hStdIn
, dwMode
);
1946 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
1947 qemu_chr_fe_set_echo(chr
, false);
1951 #endif /* !_WIN32 */
1953 /***********************************************************/
1954 /* UDP Net console */
1958 uint8_t buf
[READ_BUF_LEN
];
1964 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1966 NetCharDriver
*s
= chr
->opaque
;
1968 return send(s
->fd
, (const void *)buf
, len
, 0);
1971 static int udp_chr_read_poll(void *opaque
)
1973 CharDriverState
*chr
= opaque
;
1974 NetCharDriver
*s
= chr
->opaque
;
1976 s
->max_size
= qemu_chr_be_can_write(chr
);
1978 /* If there were any stray characters in the queue process them
1981 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1982 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
1984 s
->max_size
= qemu_chr_be_can_write(chr
);
1989 static void udp_chr_read(void *opaque
)
1991 CharDriverState
*chr
= opaque
;
1992 NetCharDriver
*s
= chr
->opaque
;
1994 if (s
->max_size
== 0)
1996 s
->bufcnt
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1997 s
->bufptr
= s
->bufcnt
;
2002 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2003 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2005 s
->max_size
= qemu_chr_be_can_write(chr
);
2009 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2011 NetCharDriver
*s
= chr
->opaque
;
2014 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2015 udp_chr_read
, NULL
, chr
);
2019 static void udp_chr_close(CharDriverState
*chr
)
2021 NetCharDriver
*s
= chr
->opaque
;
2023 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2027 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2030 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2032 CharDriverState
*chr
= NULL
;
2033 NetCharDriver
*s
= NULL
;
2034 Error
*local_err
= NULL
;
2037 chr
= g_malloc0(sizeof(CharDriverState
));
2038 s
= g_malloc0(sizeof(NetCharDriver
));
2040 fd
= inet_dgram_opts(opts
, &local_err
);
2049 chr
->chr_write
= udp_chr_write
;
2050 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2051 chr
->chr_close
= udp_chr_close
;
2056 qerror_report_err(local_err
);
2057 error_free(local_err
);
2067 /***********************************************************/
2068 /* TCP Net console */
2080 static void tcp_chr_accept(void *opaque
);
2082 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2084 TCPCharDriver
*s
= chr
->opaque
;
2086 return send_all(s
->fd
, buf
, len
);
2088 /* XXX: indicate an error ? */
2093 static int tcp_chr_read_poll(void *opaque
)
2095 CharDriverState
*chr
= opaque
;
2096 TCPCharDriver
*s
= chr
->opaque
;
2099 s
->max_size
= qemu_chr_be_can_write(chr
);
2104 #define IAC_BREAK 243
2105 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2107 uint8_t *buf
, int *size
)
2109 /* Handle any telnet client's basic IAC options to satisfy char by
2110 * char mode with no echo. All IAC options will be removed from
2111 * the buf and the do_telnetopt variable will be used to track the
2112 * state of the width of the IAC information.
2114 * IAC commands come in sets of 3 bytes with the exception of the
2115 * "IAC BREAK" command and the double IAC.
2121 for (i
= 0; i
< *size
; i
++) {
2122 if (s
->do_telnetopt
> 1) {
2123 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2124 /* Double IAC means send an IAC */
2128 s
->do_telnetopt
= 1;
2130 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2131 /* Handle IAC break commands by sending a serial break */
2132 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2137 if (s
->do_telnetopt
>= 4) {
2138 s
->do_telnetopt
= 1;
2141 if ((unsigned char)buf
[i
] == IAC
) {
2142 s
->do_telnetopt
= 2;
2153 static int tcp_get_msgfd(CharDriverState
*chr
)
2155 TCPCharDriver
*s
= chr
->opaque
;
2162 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2164 TCPCharDriver
*s
= chr
->opaque
;
2165 struct cmsghdr
*cmsg
;
2167 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2170 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2171 cmsg
->cmsg_level
!= SOL_SOCKET
||
2172 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2175 fd
= *((int *)CMSG_DATA(cmsg
));
2179 #ifndef MSG_CMSG_CLOEXEC
2180 qemu_set_cloexec(fd
);
2188 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2190 TCPCharDriver
*s
= chr
->opaque
;
2191 struct msghdr msg
= { NULL
, };
2192 struct iovec iov
[1];
2194 struct cmsghdr cmsg
;
2195 char control
[CMSG_SPACE(sizeof(int))];
2200 iov
[0].iov_base
= buf
;
2201 iov
[0].iov_len
= len
;
2205 msg
.msg_control
= &msg_control
;
2206 msg
.msg_controllen
= sizeof(msg_control
);
2208 #ifdef MSG_CMSG_CLOEXEC
2209 flags
|= MSG_CMSG_CLOEXEC
;
2211 ret
= recvmsg(s
->fd
, &msg
, flags
);
2212 if (ret
> 0 && s
->is_unix
) {
2213 unix_process_msgfd(chr
, &msg
);
2219 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2221 TCPCharDriver
*s
= chr
->opaque
;
2222 return qemu_recv(s
->fd
, buf
, len
, 0);
2226 static void tcp_chr_read(void *opaque
)
2228 CharDriverState
*chr
= opaque
;
2229 TCPCharDriver
*s
= chr
->opaque
;
2230 uint8_t buf
[READ_BUF_LEN
];
2233 if (!s
->connected
|| s
->max_size
<= 0)
2236 if (len
> s
->max_size
)
2238 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2240 /* connection closed */
2242 if (s
->listen_fd
>= 0) {
2243 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2245 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2248 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2249 } else if (size
> 0) {
2250 if (s
->do_telnetopt
)
2251 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2253 qemu_chr_be_write(chr
, buf
, size
);
2258 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2260 return qemu_chr_open_fd(eventfd
, eventfd
);
2264 static void tcp_chr_connect(void *opaque
)
2266 CharDriverState
*chr
= opaque
;
2267 TCPCharDriver
*s
= chr
->opaque
;
2271 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2272 tcp_chr_read
, NULL
, chr
);
2274 qemu_chr_generic_open(chr
);
2277 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2278 static void tcp_chr_telnet_init(int fd
)
2281 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2282 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2283 send(fd
, (char *)buf
, 3, 0);
2284 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2285 send(fd
, (char *)buf
, 3, 0);
2286 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2287 send(fd
, (char *)buf
, 3, 0);
2288 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2289 send(fd
, (char *)buf
, 3, 0);
2292 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2294 TCPCharDriver
*s
= chr
->opaque
;
2298 socket_set_nonblock(fd
);
2300 socket_set_nodelay(fd
);
2302 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2303 tcp_chr_connect(chr
);
2308 static void tcp_chr_accept(void *opaque
)
2310 CharDriverState
*chr
= opaque
;
2311 TCPCharDriver
*s
= chr
->opaque
;
2312 struct sockaddr_in saddr
;
2314 struct sockaddr_un uaddr
;
2316 struct sockaddr
*addr
;
2323 len
= sizeof(uaddr
);
2324 addr
= (struct sockaddr
*)&uaddr
;
2328 len
= sizeof(saddr
);
2329 addr
= (struct sockaddr
*)&saddr
;
2331 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2332 if (fd
< 0 && errno
!= EINTR
) {
2334 } else if (fd
>= 0) {
2335 if (s
->do_telnetopt
)
2336 tcp_chr_telnet_init(fd
);
2340 if (tcp_chr_add_client(chr
, fd
) < 0)
2344 static void tcp_chr_close(CharDriverState
*chr
)
2346 TCPCharDriver
*s
= chr
->opaque
;
2348 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2351 if (s
->listen_fd
>= 0) {
2352 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2353 closesocket(s
->listen_fd
);
2356 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2359 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2360 bool is_listen
, bool is_telnet
,
2361 bool is_waitconnect
,
2364 CharDriverState
*chr
= NULL
;
2365 TCPCharDriver
*s
= NULL
;
2366 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2367 const char *left
= "", *right
= "";
2368 struct sockaddr_storage ss
;
2369 socklen_t ss_len
= sizeof(ss
);
2371 memset(&ss
, 0, ss_len
);
2372 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2373 error_setg(errp
, "getsockname: %s", strerror(errno
));
2377 chr
= g_malloc0(sizeof(CharDriverState
));
2378 s
= g_malloc0(sizeof(TCPCharDriver
));
2385 chr
->filename
= g_malloc(256);
2386 switch (ss
.ss_family
) {
2390 snprintf(chr
->filename
, 256, "unix:%s%s",
2391 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2392 is_listen
? ",server" : "");
2400 s
->do_nodelay
= do_nodelay
;
2401 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2402 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2403 snprintf(chr
->filename
, 256, "%s:%s:%s%s%s%s",
2404 is_telnet
? "telnet" : "tcp",
2405 left
, host
, right
, serv
,
2406 is_listen
? ",server" : "");
2411 chr
->chr_write
= tcp_chr_write
;
2412 chr
->chr_close
= tcp_chr_close
;
2413 chr
->get_msgfd
= tcp_get_msgfd
;
2414 chr
->chr_add_client
= tcp_chr_add_client
;
2418 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2420 s
->do_telnetopt
= 1;
2425 socket_set_nodelay(fd
);
2426 tcp_chr_connect(chr
);
2429 if (is_listen
&& is_waitconnect
) {
2430 printf("QEMU waiting for connection on: %s\n",
2432 tcp_chr_accept(chr
);
2433 socket_set_nonblock(s
->listen_fd
);
2438 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2440 CharDriverState
*chr
= NULL
;
2441 Error
*local_err
= NULL
;
2449 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2450 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2451 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2452 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2453 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2459 fd
= unix_listen_opts(opts
, &local_err
);
2461 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2465 fd
= inet_listen_opts(opts
, 0, &local_err
);
2467 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2474 if (!is_waitconnect
)
2475 socket_set_nonblock(fd
);
2477 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2478 is_waitconnect
, &local_err
);
2479 if (error_is_set(&local_err
)) {
2487 qerror_report_err(local_err
);
2488 error_free(local_err
);
2494 g_free(chr
->opaque
);
2500 /***********************************************************/
2501 /* Memory chardev */
2504 size_t outbuf_capacity
;
2508 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2510 MemoryDriver
*d
= chr
->opaque
;
2512 /* TODO: the QString implementation has the same code, we should
2513 * introduce a generic way to do this in cutils.c */
2514 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2516 d
->outbuf_capacity
+= len
;
2517 d
->outbuf_capacity
*= 2;
2518 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2521 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2522 d
->outbuf_size
+= len
;
2527 void qemu_chr_init_mem(CharDriverState
*chr
)
2531 d
= g_malloc(sizeof(*d
));
2533 d
->outbuf_capacity
= 4096;
2534 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2536 memset(chr
, 0, sizeof(*chr
));
2538 chr
->chr_write
= mem_chr_write
;
2541 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2543 MemoryDriver
*d
= chr
->opaque
;
2544 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2547 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2548 void qemu_chr_close_mem(CharDriverState
*chr
)
2550 MemoryDriver
*d
= chr
->opaque
;
2553 g_free(chr
->opaque
);
2555 chr
->chr_write
= NULL
;
2558 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2560 const MemoryDriver
*d
= chr
->opaque
;
2561 return d
->outbuf_size
;
2564 /*********************************************************/
2565 /* Ring buffer chardev */
2572 } RingBufCharDriver
;
2574 static size_t ringbuf_count(const CharDriverState
*chr
)
2576 const RingBufCharDriver
*d
= chr
->opaque
;
2578 return d
->prod
- d
->cons
;
2581 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2583 RingBufCharDriver
*d
= chr
->opaque
;
2586 if (!buf
|| (len
< 0)) {
2590 for (i
= 0; i
< len
; i
++ ) {
2591 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2592 if (d
->prod
- d
->cons
> d
->size
) {
2593 d
->cons
= d
->prod
- d
->size
;
2600 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2602 RingBufCharDriver
*d
= chr
->opaque
;
2605 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2606 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2612 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2614 RingBufCharDriver
*d
= chr
->opaque
;
2621 static CharDriverState
*qemu_chr_open_ringbuf(QemuOpts
*opts
)
2623 CharDriverState
*chr
;
2624 RingBufCharDriver
*d
;
2626 chr
= g_malloc0(sizeof(CharDriverState
));
2627 d
= g_malloc(sizeof(*d
));
2629 d
->size
= qemu_opt_get_size(opts
, "size", 0);
2634 /* The size must be power of 2 */
2635 if (d
->size
& (d
->size
- 1)) {
2636 error_report("size of ringbuf device must be power of two");
2642 d
->cbuf
= g_malloc0(d
->size
);
2645 chr
->chr_write
= ringbuf_chr_write
;
2646 chr
->chr_close
= ringbuf_chr_close
;
2656 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2658 return chr
->chr_write
== ringbuf_chr_write
;
2661 void qmp_ringbuf_write(const char *device
, const char *data
,
2662 bool has_format
, enum DataFormat format
,
2665 CharDriverState
*chr
;
2666 const uint8_t *write_data
;
2670 chr
= qemu_chr_find(device
);
2672 error_setg(errp
, "Device '%s' not found", device
);
2676 if (!chr_is_ringbuf(chr
)) {
2677 error_setg(errp
,"%s is not a ringbuf device", device
);
2681 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2682 write_data
= g_base64_decode(data
, &write_count
);
2684 write_data
= (uint8_t *)data
;
2685 write_count
= strlen(data
);
2688 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2690 if (write_data
!= (uint8_t *)data
) {
2691 g_free((void *)write_data
);
2695 error_setg(errp
, "Failed to write to device %s", device
);
2700 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2701 bool has_format
, enum DataFormat format
,
2704 CharDriverState
*chr
;
2709 chr
= qemu_chr_find(device
);
2711 error_setg(errp
, "Device '%s' not found", device
);
2715 if (!chr_is_ringbuf(chr
)) {
2716 error_setg(errp
,"%s is not a ringbuf device", device
);
2721 error_setg(errp
, "size must be greater than zero");
2725 count
= ringbuf_count(chr
);
2726 size
= size
> count
? count
: size
;
2727 read_data
= g_malloc(size
+ 1);
2729 ringbuf_chr_read(chr
, read_data
, size
);
2731 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2732 data
= g_base64_encode(read_data
, size
);
2736 * FIXME should read only complete, valid UTF-8 characters up
2737 * to @size bytes. Invalid sequences should be replaced by a
2738 * suitable replacement character. Except when (and only
2739 * when) ring buffer lost characters since last read, initial
2740 * continuation characters should be dropped.
2742 read_data
[size
] = 0;
2743 data
= (char *)read_data
;
2749 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2751 char host
[65], port
[33], width
[8], height
[8];
2755 Error
*local_err
= NULL
;
2757 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2758 if (error_is_set(&local_err
)) {
2759 qerror_report_err(local_err
);
2760 error_free(local_err
);
2764 if (strstart(filename
, "mon:", &p
)) {
2766 qemu_opt_set(opts
, "mux", "on");
2769 if (strcmp(filename
, "null") == 0 ||
2770 strcmp(filename
, "pty") == 0 ||
2771 strcmp(filename
, "msmouse") == 0 ||
2772 strcmp(filename
, "braille") == 0 ||
2773 strcmp(filename
, "stdio") == 0) {
2774 qemu_opt_set(opts
, "backend", filename
);
2777 if (strstart(filename
, "vc", &p
)) {
2778 qemu_opt_set(opts
, "backend", "vc");
2780 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2782 qemu_opt_set(opts
, "width", width
);
2783 qemu_opt_set(opts
, "height", height
);
2784 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2786 qemu_opt_set(opts
, "cols", width
);
2787 qemu_opt_set(opts
, "rows", height
);
2794 if (strcmp(filename
, "con:") == 0) {
2795 qemu_opt_set(opts
, "backend", "console");
2798 if (strstart(filename
, "COM", NULL
)) {
2799 qemu_opt_set(opts
, "backend", "serial");
2800 qemu_opt_set(opts
, "path", filename
);
2803 if (strstart(filename
, "file:", &p
)) {
2804 qemu_opt_set(opts
, "backend", "file");
2805 qemu_opt_set(opts
, "path", p
);
2808 if (strstart(filename
, "pipe:", &p
)) {
2809 qemu_opt_set(opts
, "backend", "pipe");
2810 qemu_opt_set(opts
, "path", p
);
2813 if (strstart(filename
, "tcp:", &p
) ||
2814 strstart(filename
, "telnet:", &p
)) {
2815 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2817 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2820 qemu_opt_set(opts
, "backend", "socket");
2821 qemu_opt_set(opts
, "host", host
);
2822 qemu_opt_set(opts
, "port", port
);
2823 if (p
[pos
] == ',') {
2824 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2827 if (strstart(filename
, "telnet:", &p
))
2828 qemu_opt_set(opts
, "telnet", "on");
2831 if (strstart(filename
, "udp:", &p
)) {
2832 qemu_opt_set(opts
, "backend", "udp");
2833 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2835 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2839 qemu_opt_set(opts
, "host", host
);
2840 qemu_opt_set(opts
, "port", port
);
2841 if (p
[pos
] == '@') {
2843 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2845 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2849 qemu_opt_set(opts
, "localaddr", host
);
2850 qemu_opt_set(opts
, "localport", port
);
2854 if (strstart(filename
, "unix:", &p
)) {
2855 qemu_opt_set(opts
, "backend", "socket");
2856 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2860 if (strstart(filename
, "/dev/parport", NULL
) ||
2861 strstart(filename
, "/dev/ppi", NULL
)) {
2862 qemu_opt_set(opts
, "backend", "parport");
2863 qemu_opt_set(opts
, "path", filename
);
2866 if (strstart(filename
, "/dev/", NULL
)) {
2867 qemu_opt_set(opts
, "backend", "tty");
2868 qemu_opt_set(opts
, "path", filename
);
2873 qemu_opts_del(opts
);
2877 #ifdef HAVE_CHARDEV_PARPORT
2879 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
2881 const char *filename
= qemu_opt_get(opts
, "path");
2884 fd
= qemu_open(filename
, O_RDWR
);
2888 return qemu_chr_open_pp_fd(fd
);
2893 static const struct {
2895 CharDriverState
*(*open
)(QemuOpts
*opts
);
2896 } backend_table
[] = {
2897 { .name
= "null", .open
= qemu_chr_open_null
},
2898 { .name
= "socket", .open
= qemu_chr_open_socket
},
2899 { .name
= "udp", .open
= qemu_chr_open_udp
},
2900 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2901 { .name
= "vc", .open
= vc_init
},
2902 { .name
= "memory", .open
= qemu_chr_open_ringbuf
},
2904 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2905 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2906 { .name
= "console", .open
= qemu_chr_open_win_con
},
2907 { .name
= "serial", .open
= qemu_chr_open_win
},
2908 { .name
= "stdio", .open
= qemu_chr_open_win_stdio
},
2910 { .name
= "file", .open
= qemu_chr_open_file_out
},
2911 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2912 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2914 #ifdef CONFIG_BRLAPI
2915 { .name
= "braille", .open
= chr_baum_init
},
2917 #ifdef HAVE_CHARDEV_TTY
2918 { .name
= "tty", .open
= qemu_chr_open_tty
},
2919 { .name
= "serial", .open
= qemu_chr_open_tty
},
2920 { .name
= "pty", .open
= qemu_chr_open_pty
},
2922 #ifdef HAVE_CHARDEV_PARPORT
2923 { .name
= "parallel", .open
= qemu_chr_open_pp
},
2924 { .name
= "parport", .open
= qemu_chr_open_pp
},
2927 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
2928 #if SPICE_SERVER_VERSION >= 0x000c02
2929 { .name
= "spiceport", .open
= qemu_chr_open_spice_port
},
2934 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
2935 void (*init
)(struct CharDriverState
*s
),
2938 CharDriverState
*chr
;
2941 if (qemu_opts_id(opts
) == NULL
) {
2942 error_setg(errp
, "chardev: no id specified");
2946 if (qemu_opt_get(opts
, "backend") == NULL
) {
2947 error_setg(errp
, "chardev: \"%s\" missing backend",
2948 qemu_opts_id(opts
));
2951 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2952 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2955 if (i
== ARRAY_SIZE(backend_table
)) {
2956 error_setg(errp
, "chardev: backend \"%s\" not found",
2957 qemu_opt_get(opts
, "backend"));
2961 chr
= backend_table
[i
].open(opts
);
2963 error_setg(errp
, "chardev: opening backend \"%s\" failed",
2964 qemu_opt_get(opts
, "backend"));
2969 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
2971 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2973 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2974 CharDriverState
*base
= chr
;
2975 int len
= strlen(qemu_opts_id(opts
)) + 6;
2976 base
->label
= g_malloc(len
);
2977 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2978 chr
= qemu_chr_open_mux(base
);
2979 chr
->filename
= base
->filename
;
2980 chr
->avail_connections
= MAX_MUX
;
2981 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2983 chr
->avail_connections
= 1;
2985 chr
->label
= g_strdup(qemu_opts_id(opts
));
2990 qemu_opts_del(opts
);
2994 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2997 CharDriverState
*chr
;
3001 if (strstart(filename
, "chardev:", &p
)) {
3002 return qemu_chr_find(p
);
3005 opts
= qemu_chr_parse_compat(label
, filename
);
3009 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3010 if (error_is_set(&err
)) {
3011 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3014 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3015 monitor_init(chr
, MONITOR_USE_READLINE
);
3020 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3022 if (chr
->chr_set_echo
) {
3023 chr
->chr_set_echo(chr
, echo
);
3027 void qemu_chr_fe_open(struct CharDriverState
*chr
)
3029 if (chr
->chr_guest_open
) {
3030 chr
->chr_guest_open(chr
);
3034 void qemu_chr_fe_close(struct CharDriverState
*chr
)
3036 if (chr
->chr_guest_close
) {
3037 chr
->chr_guest_close(chr
);
3041 void qemu_chr_delete(CharDriverState
*chr
)
3043 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3044 if (chr
->chr_close
) {
3045 chr
->chr_close(chr
);
3047 g_free(chr
->filename
);
3050 qemu_opts_del(chr
->opts
);
3055 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3057 ChardevInfoList
*chr_list
= NULL
;
3058 CharDriverState
*chr
;
3060 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3061 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3062 info
->value
= g_malloc0(sizeof(*info
->value
));
3063 info
->value
->label
= g_strdup(chr
->label
);
3064 info
->value
->filename
= g_strdup(chr
->filename
);
3066 info
->next
= chr_list
;
3073 CharDriverState
*qemu_chr_find(const char *name
)
3075 CharDriverState
*chr
;
3077 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3078 if (strcmp(chr
->label
, name
) != 0)
3085 /* Get a character (serial) device interface. */
3086 CharDriverState
*qemu_char_get_next_serial(void)
3088 static int next_serial
;
3090 /* FIXME: This function needs to go away: use chardev properties! */
3091 return serial_hds
[next_serial
++];
3094 QemuOptsList qemu_chardev_opts
= {
3096 .implied_opt_name
= "backend",
3097 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3101 .type
= QEMU_OPT_STRING
,
3104 .type
= QEMU_OPT_STRING
,
3107 .type
= QEMU_OPT_STRING
,
3110 .type
= QEMU_OPT_STRING
,
3112 .name
= "localaddr",
3113 .type
= QEMU_OPT_STRING
,
3115 .name
= "localport",
3116 .type
= QEMU_OPT_STRING
,
3119 .type
= QEMU_OPT_NUMBER
,
3122 .type
= QEMU_OPT_BOOL
,
3125 .type
= QEMU_OPT_BOOL
,
3128 .type
= QEMU_OPT_BOOL
,
3131 .type
= QEMU_OPT_BOOL
,
3134 .type
= QEMU_OPT_BOOL
,
3137 .type
= QEMU_OPT_BOOL
,
3140 .type
= QEMU_OPT_NUMBER
,
3143 .type
= QEMU_OPT_NUMBER
,
3146 .type
= QEMU_OPT_NUMBER
,
3149 .type
= QEMU_OPT_NUMBER
,
3152 .type
= QEMU_OPT_BOOL
,
3155 .type
= QEMU_OPT_BOOL
,
3158 .type
= QEMU_OPT_STRING
,
3161 .type
= QEMU_OPT_NUMBER
,
3164 .type
= QEMU_OPT_SIZE
,
3166 { /* end of list */ }
3172 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3177 error_setg(errp
, "input file not supported");
3181 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3182 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3183 if (out
== INVALID_HANDLE_VALUE
) {
3184 error_setg(errp
, "open %s failed", file
->out
);
3187 return qemu_chr_open_win_file(out
);
3190 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3193 return qemu_chr_open_win_path(serial
->device
);
3196 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3199 error_setg(errp
, "character device backend type 'parallel' not supported");
3205 static int qmp_chardev_open_file_source(char *src
, int flags
,
3210 TFR(fd
= qemu_open(src
, flags
, 0666));
3212 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3217 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3219 int flags
, in
= -1, out
= -1;
3221 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3222 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3223 if (error_is_set(errp
)) {
3229 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3230 if (error_is_set(errp
)) {
3236 return qemu_chr_open_fd(in
, out
);
3239 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3242 #ifdef HAVE_CHARDEV_TTY
3245 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3246 if (error_is_set(errp
)) {
3249 socket_set_nonblock(fd
);
3250 return qemu_chr_open_tty_fd(fd
);
3252 error_setg(errp
, "character device backend type 'serial' not supported");
3257 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3260 #ifdef HAVE_CHARDEV_PARPORT
3263 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3264 if (error_is_set(errp
)) {
3267 return qemu_chr_open_pp_fd(fd
);
3269 error_setg(errp
, "character device backend type 'parallel' not supported");
3276 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3279 SocketAddress
*addr
= sock
->addr
;
3280 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3281 bool is_listen
= sock
->has_server
? sock
->server
: true;
3282 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3283 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3287 fd
= socket_listen(addr
, errp
);
3289 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3291 if (error_is_set(errp
)) {
3294 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3295 is_telnet
, is_waitconnect
, errp
);
3298 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3301 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3302 CharDriverState
*chr
= NULL
;
3304 chr
= qemu_chr_find(id
);
3306 error_setg(errp
, "Chardev '%s' already exists", id
);
3311 switch (backend
->kind
) {
3312 case CHARDEV_BACKEND_KIND_FILE
:
3313 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3315 case CHARDEV_BACKEND_KIND_SERIAL
:
3316 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3318 case CHARDEV_BACKEND_KIND_PARALLEL
:
3319 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3321 case CHARDEV_BACKEND_KIND_SOCKET
:
3322 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3324 #ifdef HAVE_CHARDEV_TTY
3325 case CHARDEV_BACKEND_KIND_PTY
:
3327 /* qemu_chr_open_pty sets "path" in opts */
3329 opts
= qemu_opts_create_nofail(qemu_find_opts("chardev"));
3330 chr
= qemu_chr_open_pty(opts
);
3331 ret
->pty
= g_strdup(qemu_opt_get(opts
, "path"));
3332 ret
->has_pty
= true;
3333 qemu_opts_del(opts
);
3337 case CHARDEV_BACKEND_KIND_NULL
:
3338 chr
= qemu_chr_open_null(NULL
);
3341 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3345 if (chr
== NULL
&& !error_is_set(errp
)) {
3346 error_setg(errp
, "Failed to create chardev");
3349 chr
->label
= g_strdup(id
);
3350 chr
->avail_connections
= 1;
3351 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3359 void qmp_chardev_remove(const char *id
, Error
**errp
)
3361 CharDriverState
*chr
;
3363 chr
= qemu_chr_find(id
);
3365 error_setg(errp
, "Chardev '%s' not found", id
);
3368 if (chr
->chr_can_read
|| chr
->chr_read
||
3369 chr
->chr_event
|| chr
->handler_opaque
) {
3370 error_setg(errp
, "Chardev '%s' is busy", id
);
3373 qemu_chr_delete(chr
);