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) {
542 #define STDIO_MAX_CLIENTS 1
543 static int stdio_nb_clients
;
553 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
555 FDCharDriver
*s
= chr
->opaque
;
556 return send_all(s
->fd_out
, buf
, len
);
559 static int fd_chr_read_poll(void *opaque
)
561 CharDriverState
*chr
= opaque
;
562 FDCharDriver
*s
= chr
->opaque
;
564 s
->max_size
= qemu_chr_be_can_write(chr
);
568 static void fd_chr_read(void *opaque
)
570 CharDriverState
*chr
= opaque
;
571 FDCharDriver
*s
= chr
->opaque
;
573 uint8_t buf
[READ_BUF_LEN
];
576 if (len
> s
->max_size
)
580 size
= read(s
->fd_in
, buf
, len
);
582 /* FD has been closed. Remove it from the active list. */
583 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
584 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
588 qemu_chr_be_write(chr
, buf
, size
);
592 static void fd_chr_update_read_handler(CharDriverState
*chr
)
594 FDCharDriver
*s
= chr
->opaque
;
597 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
599 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
600 fd_chr_read
, NULL
, chr
);
605 static void fd_chr_close(struct CharDriverState
*chr
)
607 FDCharDriver
*s
= chr
->opaque
;
610 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
612 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
617 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
620 /* open a character device to a unix fd */
621 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
623 CharDriverState
*chr
;
626 chr
= g_malloc0(sizeof(CharDriverState
));
627 s
= g_malloc0(sizeof(FDCharDriver
));
631 chr
->chr_write
= fd_chr_write
;
632 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
633 chr
->chr_close
= fd_chr_close
;
635 qemu_chr_generic_open(chr
);
640 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
644 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
645 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
649 return qemu_chr_open_fd(-1, fd_out
);
652 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
655 char filename_in
[256], filename_out
[256];
656 const char *filename
= qemu_opt_get(opts
, "path");
658 if (filename
== NULL
) {
659 fprintf(stderr
, "chardev: pipe: no filename given\n");
663 snprintf(filename_in
, 256, "%s.in", filename
);
664 snprintf(filename_out
, 256, "%s.out", filename
);
665 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
666 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
667 if (fd_in
< 0 || fd_out
< 0) {
672 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
677 return qemu_chr_open_fd(fd_in
, fd_out
);
681 /* for STDIO, we handle the case where several clients use it
684 #define TERM_FIFO_MAX_SIZE 1
686 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
687 static int term_fifo_size
;
689 static int stdio_read_poll(void *opaque
)
691 CharDriverState
*chr
= opaque
;
693 /* try to flush the queue if needed */
694 if (term_fifo_size
!= 0 && qemu_chr_be_can_write(chr
) > 0) {
695 qemu_chr_be_write(chr
, term_fifo
, 1);
698 /* see if we can absorb more chars */
699 if (term_fifo_size
== 0)
705 static void stdio_read(void *opaque
)
709 CharDriverState
*chr
= opaque
;
711 size
= read(0, buf
, 1);
713 /* stdin has been closed. Remove it from the active list. */
714 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
715 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
719 if (qemu_chr_be_can_write(chr
) > 0) {
720 qemu_chr_be_write(chr
, buf
, 1);
721 } else if (term_fifo_size
== 0) {
722 term_fifo
[term_fifo_size
++] = buf
[0];
727 /* init terminal so that we can grab keys */
728 static struct termios oldtty
;
729 static int old_fd0_flags
;
730 static bool stdio_allow_signal
;
732 static void term_exit(void)
734 tcsetattr (0, TCSANOW
, &oldtty
);
735 fcntl(0, F_SETFL
, old_fd0_flags
);
738 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
744 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
745 |INLCR
|IGNCR
|ICRNL
|IXON
);
746 tty
.c_oflag
|= OPOST
;
747 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
748 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
753 /* if graphical mode, we allow Ctrl-C handling */
754 if (!stdio_allow_signal
)
755 tty
.c_lflag
&= ~ISIG
;
757 tcsetattr (0, TCSANOW
, &tty
);
760 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
764 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
768 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
770 CharDriverState
*chr
;
772 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
) {
775 if (is_daemonized()) {
776 error_report("cannot use stdio with -daemonize");
779 if (stdio_nb_clients
== 0) {
780 old_fd0_flags
= fcntl(0, F_GETFL
);
781 tcgetattr (0, &oldtty
);
782 fcntl(0, F_SETFL
, O_NONBLOCK
);
786 chr
= qemu_chr_open_fd(0, 1);
787 chr
->chr_close
= qemu_chr_close_stdio
;
788 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
789 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
791 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
792 display_type
!= DT_NOGRAPHIC
);
793 qemu_chr_fe_set_echo(chr
, false);
799 /* Once Solaris has openpty(), this is going to be removed. */
800 static int openpty(int *amaster
, int *aslave
, char *name
,
801 struct termios
*termp
, struct winsize
*winp
)
804 int mfd
= -1, sfd
= -1;
806 *amaster
= *aslave
= -1;
808 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
812 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
815 if ((slave
= ptsname(mfd
)) == NULL
)
818 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
821 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
822 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
830 ioctl(sfd
, TIOCSWINSZ
, winp
);
841 static void cfmakeraw (struct termios
*termios_p
)
843 termios_p
->c_iflag
&=
844 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
845 termios_p
->c_oflag
&= ~OPOST
;
846 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
847 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
848 termios_p
->c_cflag
|= CS8
;
850 termios_p
->c_cc
[VMIN
] = 0;
851 termios_p
->c_cc
[VTIME
] = 0;
855 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
856 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
857 || defined(__GLIBC__)
859 #define HAVE_CHARDEV_TTY 1
869 static void pty_chr_update_read_handler(CharDriverState
*chr
);
870 static void pty_chr_state(CharDriverState
*chr
, int connected
);
872 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
874 PtyCharDriver
*s
= chr
->opaque
;
877 /* guest sends data, check for (re-)connect */
878 pty_chr_update_read_handler(chr
);
881 return send_all(s
->fd
, buf
, len
);
884 static int pty_chr_read_poll(void *opaque
)
886 CharDriverState
*chr
= opaque
;
887 PtyCharDriver
*s
= chr
->opaque
;
889 s
->read_bytes
= qemu_chr_be_can_write(chr
);
890 return s
->read_bytes
;
893 static void pty_chr_read(void *opaque
)
895 CharDriverState
*chr
= opaque
;
896 PtyCharDriver
*s
= chr
->opaque
;
898 uint8_t buf
[READ_BUF_LEN
];
901 if (len
> s
->read_bytes
)
905 size
= read(s
->fd
, buf
, len
);
906 if ((size
== -1 && errno
== EIO
) ||
908 pty_chr_state(chr
, 0);
912 pty_chr_state(chr
, 1);
913 qemu_chr_be_write(chr
, buf
, size
);
917 static void pty_chr_update_read_handler(CharDriverState
*chr
)
919 PtyCharDriver
*s
= chr
->opaque
;
921 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
922 pty_chr_read
, NULL
, chr
);
925 * Short timeout here: just need wait long enougth that qemu makes
926 * it through the poll loop once. When reconnected we want a
927 * short timeout so we notice it almost instantly. Otherwise
928 * read() gives us -EIO instantly, making pty_chr_state() reset the
929 * timeout to the normal (much longer) poll interval before the
932 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
935 static void pty_chr_state(CharDriverState
*chr
, int connected
)
937 PtyCharDriver
*s
= chr
->opaque
;
940 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
943 /* (re-)connect poll interval for idle guests: once per second.
944 * We check more frequently in case the guests sends data to
945 * the virtual device linked to our pty. */
946 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
949 qemu_chr_generic_open(chr
);
954 static void pty_chr_timer(void *opaque
)
956 struct CharDriverState
*chr
= opaque
;
957 PtyCharDriver
*s
= chr
->opaque
;
962 /* If we arrive here without polling being cleared due
963 * read returning -EIO, then we are (re-)connected */
964 pty_chr_state(chr
, 1);
969 pty_chr_update_read_handler(chr
);
972 static void pty_chr_close(struct CharDriverState
*chr
)
974 PtyCharDriver
*s
= chr
->opaque
;
976 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
978 qemu_del_timer(s
->timer
);
979 qemu_free_timer(s
->timer
);
981 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
984 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
986 CharDriverState
*chr
;
990 int master_fd
, slave_fd
, len
;
991 #if defined(__OpenBSD__) || defined(__DragonFly__)
992 char pty_name
[PATH_MAX
];
993 #define q_ptsname(x) pty_name
995 char *pty_name
= NULL
;
996 #define q_ptsname(x) ptsname(x)
999 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1003 /* Set raw attributes on the pty. */
1004 tcgetattr(slave_fd
, &tty
);
1006 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1009 chr
= g_malloc0(sizeof(CharDriverState
));
1011 len
= strlen(q_ptsname(master_fd
)) + 5;
1012 chr
->filename
= g_malloc(len
);
1013 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
1014 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
1016 label
= qemu_opts_id(opts
);
1017 fprintf(stderr
, "char device redirected to %s%s%s%s\n",
1018 q_ptsname(master_fd
),
1019 label
? " (label " : "",
1023 s
= g_malloc0(sizeof(PtyCharDriver
));
1025 chr
->chr_write
= pty_chr_write
;
1026 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1027 chr
->chr_close
= pty_chr_close
;
1030 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
1035 static void tty_serial_init(int fd
, int speed
,
1036 int parity
, int data_bits
, int stop_bits
)
1042 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1043 speed
, parity
, data_bits
, stop_bits
);
1045 tcgetattr (fd
, &tty
);
1047 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1048 speed
= speed
* 10 / 11;
1065 /* Non-Posix values follow. They may be unsupported on some systems. */
1067 check_speed(115200);
1069 check_speed(230400);
1072 check_speed(460800);
1075 check_speed(500000);
1078 check_speed(576000);
1081 check_speed(921600);
1084 check_speed(1000000);
1087 check_speed(1152000);
1090 check_speed(1500000);
1093 check_speed(2000000);
1096 check_speed(2500000);
1099 check_speed(3000000);
1102 check_speed(3500000);
1105 check_speed(4000000);
1110 cfsetispeed(&tty
, spd
);
1111 cfsetospeed(&tty
, spd
);
1113 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1114 |INLCR
|IGNCR
|ICRNL
|IXON
);
1115 tty
.c_oflag
|= OPOST
;
1116 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1117 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1138 tty
.c_cflag
|= PARENB
;
1141 tty
.c_cflag
|= PARENB
| PARODD
;
1145 tty
.c_cflag
|= CSTOPB
;
1147 tcsetattr (fd
, TCSANOW
, &tty
);
1150 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1152 FDCharDriver
*s
= chr
->opaque
;
1155 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1157 QEMUSerialSetParams
*ssp
= arg
;
1158 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1159 ssp
->data_bits
, ssp
->stop_bits
);
1162 case CHR_IOCTL_SERIAL_SET_BREAK
:
1164 int enable
= *(int *)arg
;
1166 tcsendbreak(s
->fd_in
, 1);
1169 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1172 int *targ
= (int *)arg
;
1173 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1175 if (sarg
& TIOCM_CTS
)
1176 *targ
|= CHR_TIOCM_CTS
;
1177 if (sarg
& TIOCM_CAR
)
1178 *targ
|= CHR_TIOCM_CAR
;
1179 if (sarg
& TIOCM_DSR
)
1180 *targ
|= CHR_TIOCM_DSR
;
1181 if (sarg
& TIOCM_RI
)
1182 *targ
|= CHR_TIOCM_RI
;
1183 if (sarg
& TIOCM_DTR
)
1184 *targ
|= CHR_TIOCM_DTR
;
1185 if (sarg
& TIOCM_RTS
)
1186 *targ
|= CHR_TIOCM_RTS
;
1189 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1191 int sarg
= *(int *)arg
;
1193 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1194 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1195 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1196 if (sarg
& CHR_TIOCM_CTS
)
1198 if (sarg
& CHR_TIOCM_CAR
)
1200 if (sarg
& CHR_TIOCM_DSR
)
1202 if (sarg
& CHR_TIOCM_RI
)
1204 if (sarg
& CHR_TIOCM_DTR
)
1206 if (sarg
& CHR_TIOCM_RTS
)
1208 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1217 static void qemu_chr_close_tty(CharDriverState
*chr
)
1219 FDCharDriver
*s
= chr
->opaque
;
1233 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1235 CharDriverState
*chr
;
1237 tty_serial_init(fd
, 115200, 'N', 8, 1);
1238 chr
= qemu_chr_open_fd(fd
, fd
);
1239 chr
->chr_ioctl
= tty_serial_ioctl
;
1240 chr
->chr_close
= qemu_chr_close_tty
;
1244 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1246 const char *filename
= qemu_opt_get(opts
, "path");
1249 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1253 return qemu_chr_open_tty_fd(fd
);
1255 #endif /* __linux__ || __sun__ */
1257 #if defined(__linux__)
1259 #define HAVE_CHARDEV_PARPORT 1
1264 } ParallelCharDriver
;
1266 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1268 if (s
->mode
!= mode
) {
1270 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1277 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1279 ParallelCharDriver
*drv
= chr
->opaque
;
1284 case CHR_IOCTL_PP_READ_DATA
:
1285 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1287 *(uint8_t *)arg
= b
;
1289 case CHR_IOCTL_PP_WRITE_DATA
:
1290 b
= *(uint8_t *)arg
;
1291 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1294 case CHR_IOCTL_PP_READ_CONTROL
:
1295 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1297 /* Linux gives only the lowest bits, and no way to know data
1298 direction! For better compatibility set the fixed upper
1300 *(uint8_t *)arg
= b
| 0xc0;
1302 case CHR_IOCTL_PP_WRITE_CONTROL
:
1303 b
= *(uint8_t *)arg
;
1304 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1307 case CHR_IOCTL_PP_READ_STATUS
:
1308 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1310 *(uint8_t *)arg
= b
;
1312 case CHR_IOCTL_PP_DATA_DIR
:
1313 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1316 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1317 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1318 struct ParallelIOArg
*parg
= arg
;
1319 int n
= read(fd
, parg
->buffer
, parg
->count
);
1320 if (n
!= parg
->count
) {
1325 case CHR_IOCTL_PP_EPP_READ
:
1326 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1327 struct ParallelIOArg
*parg
= arg
;
1328 int n
= read(fd
, parg
->buffer
, parg
->count
);
1329 if (n
!= parg
->count
) {
1334 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1335 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1336 struct ParallelIOArg
*parg
= arg
;
1337 int n
= write(fd
, parg
->buffer
, parg
->count
);
1338 if (n
!= parg
->count
) {
1343 case CHR_IOCTL_PP_EPP_WRITE
:
1344 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1345 struct ParallelIOArg
*parg
= arg
;
1346 int n
= write(fd
, parg
->buffer
, parg
->count
);
1347 if (n
!= parg
->count
) {
1358 static void pp_close(CharDriverState
*chr
)
1360 ParallelCharDriver
*drv
= chr
->opaque
;
1363 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1364 ioctl(fd
, PPRELEASE
);
1367 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1370 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1372 CharDriverState
*chr
;
1373 ParallelCharDriver
*drv
;
1375 if (ioctl(fd
, PPCLAIM
) < 0) {
1380 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1382 drv
->mode
= IEEE1284_MODE_COMPAT
;
1384 chr
= g_malloc0(sizeof(CharDriverState
));
1385 chr
->chr_write
= null_chr_write
;
1386 chr
->chr_ioctl
= pp_ioctl
;
1387 chr
->chr_close
= pp_close
;
1390 qemu_chr_generic_open(chr
);
1394 #endif /* __linux__ */
1396 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1398 #define HAVE_CHARDEV_PARPORT 1
1400 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1402 int fd
= (int)(intptr_t)chr
->opaque
;
1406 case CHR_IOCTL_PP_READ_DATA
:
1407 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1409 *(uint8_t *)arg
= b
;
1411 case CHR_IOCTL_PP_WRITE_DATA
:
1412 b
= *(uint8_t *)arg
;
1413 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1416 case CHR_IOCTL_PP_READ_CONTROL
:
1417 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1419 *(uint8_t *)arg
= b
;
1421 case CHR_IOCTL_PP_WRITE_CONTROL
:
1422 b
= *(uint8_t *)arg
;
1423 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1426 case CHR_IOCTL_PP_READ_STATUS
:
1427 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1429 *(uint8_t *)arg
= b
;
1437 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1439 CharDriverState
*chr
;
1441 chr
= g_malloc0(sizeof(CharDriverState
));
1442 chr
->opaque
= (void *)(intptr_t)fd
;
1443 chr
->chr_write
= null_chr_write
;
1444 chr
->chr_ioctl
= pp_ioctl
;
1451 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1455 HANDLE hcom
, hrecv
, hsend
;
1456 OVERLAPPED orecv
, osend
;
1463 HANDLE hInputReadyEvent
;
1464 HANDLE hInputDoneEvent
;
1465 HANDLE hInputThread
;
1466 uint8_t win_stdio_buf
;
1467 } WinStdioCharState
;
1469 #define NSENDBUF 2048
1470 #define NRECVBUF 2048
1471 #define MAXCONNECT 1
1472 #define NTIMEOUT 5000
1474 static int win_chr_poll(void *opaque
);
1475 static int win_chr_pipe_poll(void *opaque
);
1477 static void win_chr_close(CharDriverState
*chr
)
1479 WinCharState
*s
= chr
->opaque
;
1482 CloseHandle(s
->hsend
);
1486 CloseHandle(s
->hrecv
);
1490 CloseHandle(s
->hcom
);
1494 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1496 qemu_del_polling_cb(win_chr_poll
, chr
);
1498 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1501 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1503 WinCharState
*s
= chr
->opaque
;
1505 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1510 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1512 fprintf(stderr
, "Failed CreateEvent\n");
1515 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1517 fprintf(stderr
, "Failed CreateEvent\n");
1521 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1522 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1523 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1524 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1529 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1530 fprintf(stderr
, "Failed SetupComm\n");
1534 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1535 size
= sizeof(COMMCONFIG
);
1536 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1537 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1538 CommConfigDialog(filename
, NULL
, &comcfg
);
1540 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1541 fprintf(stderr
, "Failed SetCommState\n");
1545 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1546 fprintf(stderr
, "Failed SetCommMask\n");
1550 cto
.ReadIntervalTimeout
= MAXDWORD
;
1551 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1552 fprintf(stderr
, "Failed SetCommTimeouts\n");
1556 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1557 fprintf(stderr
, "Failed ClearCommError\n");
1560 qemu_add_polling_cb(win_chr_poll
, chr
);
1568 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1570 WinCharState
*s
= chr
->opaque
;
1571 DWORD len
, ret
, size
, err
;
1574 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1575 s
->osend
.hEvent
= s
->hsend
;
1578 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1580 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1582 err
= GetLastError();
1583 if (err
== ERROR_IO_PENDING
) {
1584 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1602 static int win_chr_read_poll(CharDriverState
*chr
)
1604 WinCharState
*s
= chr
->opaque
;
1606 s
->max_size
= qemu_chr_be_can_write(chr
);
1610 static void win_chr_readfile(CharDriverState
*chr
)
1612 WinCharState
*s
= chr
->opaque
;
1614 uint8_t buf
[READ_BUF_LEN
];
1617 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1618 s
->orecv
.hEvent
= s
->hrecv
;
1619 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1621 err
= GetLastError();
1622 if (err
== ERROR_IO_PENDING
) {
1623 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1628 qemu_chr_be_write(chr
, buf
, size
);
1632 static void win_chr_read(CharDriverState
*chr
)
1634 WinCharState
*s
= chr
->opaque
;
1636 if (s
->len
> s
->max_size
)
1637 s
->len
= s
->max_size
;
1641 win_chr_readfile(chr
);
1644 static int win_chr_poll(void *opaque
)
1646 CharDriverState
*chr
= opaque
;
1647 WinCharState
*s
= chr
->opaque
;
1651 ClearCommError(s
->hcom
, &comerr
, &status
);
1652 if (status
.cbInQue
> 0) {
1653 s
->len
= status
.cbInQue
;
1654 win_chr_read_poll(chr
);
1661 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1663 CharDriverState
*chr
;
1666 chr
= g_malloc0(sizeof(CharDriverState
));
1667 s
= g_malloc0(sizeof(WinCharState
));
1669 chr
->chr_write
= win_chr_write
;
1670 chr
->chr_close
= win_chr_close
;
1672 if (win_chr_init(chr
, filename
) < 0) {
1677 qemu_chr_generic_open(chr
);
1681 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1683 return qemu_chr_open_win_path(qemu_opt_get(opts
, "path"));
1686 static int win_chr_pipe_poll(void *opaque
)
1688 CharDriverState
*chr
= opaque
;
1689 WinCharState
*s
= chr
->opaque
;
1692 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1695 win_chr_read_poll(chr
);
1702 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1704 WinCharState
*s
= chr
->opaque
;
1712 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1714 fprintf(stderr
, "Failed CreateEvent\n");
1717 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1719 fprintf(stderr
, "Failed CreateEvent\n");
1723 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1724 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1725 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1727 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1728 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1729 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1734 ZeroMemory(&ov
, sizeof(ov
));
1735 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1736 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1738 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1742 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1744 fprintf(stderr
, "Failed GetOverlappedResult\n");
1746 CloseHandle(ov
.hEvent
);
1753 CloseHandle(ov
.hEvent
);
1756 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1765 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1767 const char *filename
= qemu_opt_get(opts
, "path");
1768 CharDriverState
*chr
;
1771 chr
= g_malloc0(sizeof(CharDriverState
));
1772 s
= g_malloc0(sizeof(WinCharState
));
1774 chr
->chr_write
= win_chr_write
;
1775 chr
->chr_close
= win_chr_close
;
1777 if (win_chr_pipe_init(chr
, filename
) < 0) {
1782 qemu_chr_generic_open(chr
);
1786 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1788 CharDriverState
*chr
;
1791 chr
= g_malloc0(sizeof(CharDriverState
));
1792 s
= g_malloc0(sizeof(WinCharState
));
1795 chr
->chr_write
= win_chr_write
;
1796 qemu_chr_generic_open(chr
);
1800 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1802 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1805 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1807 const char *file_out
= qemu_opt_get(opts
, "path");
1810 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1811 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1812 if (fd_out
== INVALID_HANDLE_VALUE
) {
1816 return qemu_chr_open_win_file(fd_out
);
1819 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1821 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1828 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1838 static void win_stdio_wait_func(void *opaque
)
1840 CharDriverState
*chr
= opaque
;
1841 WinStdioCharState
*stdio
= chr
->opaque
;
1842 INPUT_RECORD buf
[4];
1847 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1851 /* Avoid error storm */
1852 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1856 for (i
= 0; i
< dwSize
; i
++) {
1857 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1859 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1861 if (kev
->uChar
.AsciiChar
!= 0) {
1862 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1863 if (qemu_chr_be_can_write(chr
)) {
1864 uint8_t c
= kev
->uChar
.AsciiChar
;
1865 qemu_chr_be_write(chr
, &c
, 1);
1873 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
1875 CharDriverState
*chr
= param
;
1876 WinStdioCharState
*stdio
= chr
->opaque
;
1882 /* Wait for one byte */
1883 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
1885 /* Exit in case of error, continue if nothing read */
1893 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1894 if (stdio
->win_stdio_buf
== '\r') {
1898 /* Signal the main thread and wait until the byte was eaten */
1899 if (!SetEvent(stdio
->hInputReadyEvent
)) {
1902 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
1908 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
1912 static void win_stdio_thread_wait_func(void *opaque
)
1914 CharDriverState
*chr
= opaque
;
1915 WinStdioCharState
*stdio
= chr
->opaque
;
1917 if (qemu_chr_be_can_write(chr
)) {
1918 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
1921 SetEvent(stdio
->hInputDoneEvent
);
1924 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
1926 WinStdioCharState
*stdio
= chr
->opaque
;
1929 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
1932 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
1934 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
1938 static void win_stdio_close(CharDriverState
*chr
)
1940 WinStdioCharState
*stdio
= chr
->opaque
;
1942 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
1943 CloseHandle(stdio
->hInputReadyEvent
);
1945 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
1946 CloseHandle(stdio
->hInputDoneEvent
);
1948 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
1949 TerminateThread(stdio
->hInputThread
, 0);
1952 g_free(chr
->opaque
);
1957 static CharDriverState
*qemu_chr_open_win_stdio(QemuOpts
*opts
)
1959 CharDriverState
*chr
;
1960 WinStdioCharState
*stdio
;
1964 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
1965 || ((display_type
!= DT_NOGRAPHIC
) && (stdio_nb_clients
!= 0))) {
1969 chr
= g_malloc0(sizeof(CharDriverState
));
1970 stdio
= g_malloc0(sizeof(WinStdioCharState
));
1972 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
1973 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
1974 fprintf(stderr
, "cannot open stdio: invalid handle\n");
1978 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
1980 chr
->opaque
= stdio
;
1981 chr
->chr_write
= win_stdio_write
;
1982 chr
->chr_close
= win_stdio_close
;
1984 if (stdio_nb_clients
== 0) {
1986 if (qemu_add_wait_object(stdio
->hStdIn
,
1987 win_stdio_wait_func
, chr
)) {
1988 fprintf(stderr
, "qemu_add_wait_object: failed\n");
1993 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1994 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1995 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
1998 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
1999 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2000 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2001 fprintf(stderr
, "cannot create stdio thread or event\n");
2004 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2005 win_stdio_thread_wait_func
, chr
)) {
2006 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2011 dwMode
|= ENABLE_LINE_INPUT
;
2013 stdio_clients
[stdio_nb_clients
++] = chr
;
2014 if (stdio_nb_clients
== 1 && is_console
) {
2015 /* set the terminal in raw mode */
2016 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2017 dwMode
|= ENABLE_PROCESSED_INPUT
;
2020 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2022 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2023 qemu_chr_fe_set_echo(chr
, false);
2027 #endif /* !_WIN32 */
2029 /***********************************************************/
2030 /* UDP Net console */
2034 uint8_t buf
[READ_BUF_LEN
];
2040 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2042 NetCharDriver
*s
= chr
->opaque
;
2044 return send(s
->fd
, (const void *)buf
, len
, 0);
2047 static int udp_chr_read_poll(void *opaque
)
2049 CharDriverState
*chr
= opaque
;
2050 NetCharDriver
*s
= chr
->opaque
;
2052 s
->max_size
= qemu_chr_be_can_write(chr
);
2054 /* If there were any stray characters in the queue process them
2057 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2058 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2060 s
->max_size
= qemu_chr_be_can_write(chr
);
2065 static void udp_chr_read(void *opaque
)
2067 CharDriverState
*chr
= opaque
;
2068 NetCharDriver
*s
= chr
->opaque
;
2070 if (s
->max_size
== 0)
2072 s
->bufcnt
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2073 s
->bufptr
= s
->bufcnt
;
2078 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2079 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2081 s
->max_size
= qemu_chr_be_can_write(chr
);
2085 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2087 NetCharDriver
*s
= chr
->opaque
;
2090 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2091 udp_chr_read
, NULL
, chr
);
2095 static void udp_chr_close(CharDriverState
*chr
)
2097 NetCharDriver
*s
= chr
->opaque
;
2099 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2103 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2106 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2108 CharDriverState
*chr
= NULL
;
2109 NetCharDriver
*s
= NULL
;
2110 Error
*local_err
= NULL
;
2113 chr
= g_malloc0(sizeof(CharDriverState
));
2114 s
= g_malloc0(sizeof(NetCharDriver
));
2116 fd
= inet_dgram_opts(opts
, &local_err
);
2125 chr
->chr_write
= udp_chr_write
;
2126 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2127 chr
->chr_close
= udp_chr_close
;
2132 qerror_report_err(local_err
);
2133 error_free(local_err
);
2143 /***********************************************************/
2144 /* TCP Net console */
2156 static void tcp_chr_accept(void *opaque
);
2158 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2160 TCPCharDriver
*s
= chr
->opaque
;
2162 return send_all(s
->fd
, buf
, len
);
2164 /* XXX: indicate an error ? */
2169 static int tcp_chr_read_poll(void *opaque
)
2171 CharDriverState
*chr
= opaque
;
2172 TCPCharDriver
*s
= chr
->opaque
;
2175 s
->max_size
= qemu_chr_be_can_write(chr
);
2180 #define IAC_BREAK 243
2181 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2183 uint8_t *buf
, int *size
)
2185 /* Handle any telnet client's basic IAC options to satisfy char by
2186 * char mode with no echo. All IAC options will be removed from
2187 * the buf and the do_telnetopt variable will be used to track the
2188 * state of the width of the IAC information.
2190 * IAC commands come in sets of 3 bytes with the exception of the
2191 * "IAC BREAK" command and the double IAC.
2197 for (i
= 0; i
< *size
; i
++) {
2198 if (s
->do_telnetopt
> 1) {
2199 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2200 /* Double IAC means send an IAC */
2204 s
->do_telnetopt
= 1;
2206 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2207 /* Handle IAC break commands by sending a serial break */
2208 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2213 if (s
->do_telnetopt
>= 4) {
2214 s
->do_telnetopt
= 1;
2217 if ((unsigned char)buf
[i
] == IAC
) {
2218 s
->do_telnetopt
= 2;
2229 static int tcp_get_msgfd(CharDriverState
*chr
)
2231 TCPCharDriver
*s
= chr
->opaque
;
2238 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2240 TCPCharDriver
*s
= chr
->opaque
;
2241 struct cmsghdr
*cmsg
;
2243 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2246 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2247 cmsg
->cmsg_level
!= SOL_SOCKET
||
2248 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2251 fd
= *((int *)CMSG_DATA(cmsg
));
2255 #ifndef MSG_CMSG_CLOEXEC
2256 qemu_set_cloexec(fd
);
2264 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2266 TCPCharDriver
*s
= chr
->opaque
;
2267 struct msghdr msg
= { NULL
, };
2268 struct iovec iov
[1];
2270 struct cmsghdr cmsg
;
2271 char control
[CMSG_SPACE(sizeof(int))];
2276 iov
[0].iov_base
= buf
;
2277 iov
[0].iov_len
= len
;
2281 msg
.msg_control
= &msg_control
;
2282 msg
.msg_controllen
= sizeof(msg_control
);
2284 #ifdef MSG_CMSG_CLOEXEC
2285 flags
|= MSG_CMSG_CLOEXEC
;
2287 ret
= recvmsg(s
->fd
, &msg
, flags
);
2288 if (ret
> 0 && s
->is_unix
) {
2289 unix_process_msgfd(chr
, &msg
);
2295 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2297 TCPCharDriver
*s
= chr
->opaque
;
2298 return qemu_recv(s
->fd
, buf
, len
, 0);
2302 static void tcp_chr_read(void *opaque
)
2304 CharDriverState
*chr
= opaque
;
2305 TCPCharDriver
*s
= chr
->opaque
;
2306 uint8_t buf
[READ_BUF_LEN
];
2309 if (!s
->connected
|| s
->max_size
<= 0)
2312 if (len
> s
->max_size
)
2314 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2316 /* connection closed */
2318 if (s
->listen_fd
>= 0) {
2319 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2321 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2324 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2325 } else if (size
> 0) {
2326 if (s
->do_telnetopt
)
2327 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2329 qemu_chr_be_write(chr
, buf
, size
);
2334 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2336 return qemu_chr_open_fd(eventfd
, eventfd
);
2340 static void tcp_chr_connect(void *opaque
)
2342 CharDriverState
*chr
= opaque
;
2343 TCPCharDriver
*s
= chr
->opaque
;
2347 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2348 tcp_chr_read
, NULL
, chr
);
2350 qemu_chr_generic_open(chr
);
2353 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2354 static void tcp_chr_telnet_init(int fd
)
2357 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2358 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2359 send(fd
, (char *)buf
, 3, 0);
2360 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2361 send(fd
, (char *)buf
, 3, 0);
2362 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2363 send(fd
, (char *)buf
, 3, 0);
2364 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2365 send(fd
, (char *)buf
, 3, 0);
2368 static void socket_set_nodelay(int fd
)
2371 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2374 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2376 TCPCharDriver
*s
= chr
->opaque
;
2380 socket_set_nonblock(fd
);
2382 socket_set_nodelay(fd
);
2384 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2385 tcp_chr_connect(chr
);
2390 static void tcp_chr_accept(void *opaque
)
2392 CharDriverState
*chr
= opaque
;
2393 TCPCharDriver
*s
= chr
->opaque
;
2394 struct sockaddr_in saddr
;
2396 struct sockaddr_un uaddr
;
2398 struct sockaddr
*addr
;
2405 len
= sizeof(uaddr
);
2406 addr
= (struct sockaddr
*)&uaddr
;
2410 len
= sizeof(saddr
);
2411 addr
= (struct sockaddr
*)&saddr
;
2413 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2414 if (fd
< 0 && errno
!= EINTR
) {
2416 } else if (fd
>= 0) {
2417 if (s
->do_telnetopt
)
2418 tcp_chr_telnet_init(fd
);
2422 if (tcp_chr_add_client(chr
, fd
) < 0)
2426 static void tcp_chr_close(CharDriverState
*chr
)
2428 TCPCharDriver
*s
= chr
->opaque
;
2430 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2433 if (s
->listen_fd
>= 0) {
2434 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2435 closesocket(s
->listen_fd
);
2438 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2441 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2442 bool is_listen
, bool is_telnet
,
2443 bool is_waitconnect
,
2446 CharDriverState
*chr
= NULL
;
2447 TCPCharDriver
*s
= NULL
;
2448 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2449 const char *left
= "", *right
= "";
2450 struct sockaddr_storage ss
;
2451 socklen_t ss_len
= sizeof(ss
);
2453 memset(&ss
, 0, ss_len
);
2454 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2455 error_setg(errp
, "getsockname: %s", strerror(errno
));
2459 chr
= g_malloc0(sizeof(CharDriverState
));
2460 s
= g_malloc0(sizeof(TCPCharDriver
));
2467 chr
->filename
= g_malloc(256);
2468 switch (ss
.ss_family
) {
2472 snprintf(chr
->filename
, 256, "unix:%s%s",
2473 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2474 is_listen
? ",server" : "");
2482 s
->do_nodelay
= do_nodelay
;
2483 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2484 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2485 snprintf(chr
->filename
, 256, "%s:%s:%s%s%s%s",
2486 is_telnet
? "telnet" : "tcp",
2487 left
, host
, right
, serv
,
2488 is_listen
? ",server" : "");
2493 chr
->chr_write
= tcp_chr_write
;
2494 chr
->chr_close
= tcp_chr_close
;
2495 chr
->get_msgfd
= tcp_get_msgfd
;
2496 chr
->chr_add_client
= tcp_chr_add_client
;
2500 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2502 s
->do_telnetopt
= 1;
2507 socket_set_nodelay(fd
);
2508 tcp_chr_connect(chr
);
2511 if (is_listen
&& is_waitconnect
) {
2512 printf("QEMU waiting for connection on: %s\n",
2514 tcp_chr_accept(chr
);
2515 socket_set_nonblock(s
->listen_fd
);
2520 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2522 CharDriverState
*chr
= NULL
;
2523 Error
*local_err
= NULL
;
2531 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2532 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2533 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2534 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2535 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2541 fd
= unix_listen_opts(opts
, &local_err
);
2543 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2547 fd
= inet_listen_opts(opts
, 0, &local_err
);
2549 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2556 if (!is_waitconnect
)
2557 socket_set_nonblock(fd
);
2559 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2560 is_waitconnect
, &local_err
);
2561 if (error_is_set(&local_err
)) {
2569 qerror_report_err(local_err
);
2570 error_free(local_err
);
2576 g_free(chr
->opaque
);
2582 /***********************************************************/
2583 /* Memory chardev */
2586 size_t outbuf_capacity
;
2590 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2592 MemoryDriver
*d
= chr
->opaque
;
2594 /* TODO: the QString implementation has the same code, we should
2595 * introduce a generic way to do this in cutils.c */
2596 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2598 d
->outbuf_capacity
+= len
;
2599 d
->outbuf_capacity
*= 2;
2600 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2603 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2604 d
->outbuf_size
+= len
;
2609 void qemu_chr_init_mem(CharDriverState
*chr
)
2613 d
= g_malloc(sizeof(*d
));
2615 d
->outbuf_capacity
= 4096;
2616 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2618 memset(chr
, 0, sizeof(*chr
));
2620 chr
->chr_write
= mem_chr_write
;
2623 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2625 MemoryDriver
*d
= chr
->opaque
;
2626 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2629 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2630 void qemu_chr_close_mem(CharDriverState
*chr
)
2632 MemoryDriver
*d
= chr
->opaque
;
2635 g_free(chr
->opaque
);
2637 chr
->chr_write
= NULL
;
2640 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2642 const MemoryDriver
*d
= chr
->opaque
;
2643 return d
->outbuf_size
;
2646 /*********************************************************/
2647 /* Ring buffer chardev */
2654 } RingBufCharDriver
;
2656 static size_t ringbuf_count(const CharDriverState
*chr
)
2658 const RingBufCharDriver
*d
= chr
->opaque
;
2660 return d
->prod
- d
->cons
;
2663 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2665 RingBufCharDriver
*d
= chr
->opaque
;
2668 if (!buf
|| (len
< 0)) {
2672 for (i
= 0; i
< len
; i
++ ) {
2673 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2674 if (d
->prod
- d
->cons
> d
->size
) {
2675 d
->cons
= d
->prod
- d
->size
;
2682 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2684 RingBufCharDriver
*d
= chr
->opaque
;
2687 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2688 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2694 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2696 RingBufCharDriver
*d
= chr
->opaque
;
2703 static CharDriverState
*qemu_chr_open_ringbuf(QemuOpts
*opts
)
2705 CharDriverState
*chr
;
2706 RingBufCharDriver
*d
;
2708 chr
= g_malloc0(sizeof(CharDriverState
));
2709 d
= g_malloc(sizeof(*d
));
2711 d
->size
= qemu_opt_get_size(opts
, "size", 0);
2716 /* The size must be power of 2 */
2717 if (d
->size
& (d
->size
- 1)) {
2718 error_report("size of ringbuf device must be power of two");
2724 d
->cbuf
= g_malloc0(d
->size
);
2727 chr
->chr_write
= ringbuf_chr_write
;
2728 chr
->chr_close
= ringbuf_chr_close
;
2738 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2740 return chr
->chr_write
== ringbuf_chr_write
;
2743 void qmp_ringbuf_write(const char *device
, const char *data
,
2744 bool has_format
, enum DataFormat format
,
2747 CharDriverState
*chr
;
2748 const uint8_t *write_data
;
2752 chr
= qemu_chr_find(device
);
2754 error_setg(errp
, "Device '%s' not found", device
);
2758 if (!chr_is_ringbuf(chr
)) {
2759 error_setg(errp
,"%s is not a ringbuf device", device
);
2763 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2764 write_data
= g_base64_decode(data
, &write_count
);
2766 write_data
= (uint8_t *)data
;
2767 write_count
= strlen(data
);
2770 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2772 if (write_data
!= (uint8_t *)data
) {
2773 g_free((void *)write_data
);
2777 error_setg(errp
, "Failed to write to device %s", device
);
2782 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2783 bool has_format
, enum DataFormat format
,
2786 CharDriverState
*chr
;
2791 chr
= qemu_chr_find(device
);
2793 error_setg(errp
, "Device '%s' not found", device
);
2797 if (!chr_is_ringbuf(chr
)) {
2798 error_setg(errp
,"%s is not a ringbuf device", device
);
2803 error_setg(errp
, "size must be greater than zero");
2807 count
= ringbuf_count(chr
);
2808 size
= size
> count
? count
: size
;
2809 read_data
= g_malloc(size
+ 1);
2811 ringbuf_chr_read(chr
, read_data
, size
);
2813 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2814 data
= g_base64_encode(read_data
, size
);
2818 * FIXME should read only complete, valid UTF-8 characters up
2819 * to @size bytes. Invalid sequences should be replaced by a
2820 * suitable replacement character. Except when (and only
2821 * when) ring buffer lost characters since last read, initial
2822 * continuation characters should be dropped.
2824 read_data
[size
] = 0;
2825 data
= (char *)read_data
;
2831 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2833 char host
[65], port
[33], width
[8], height
[8];
2837 Error
*local_err
= NULL
;
2839 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2840 if (error_is_set(&local_err
)) {
2841 qerror_report_err(local_err
);
2842 error_free(local_err
);
2846 if (strstart(filename
, "mon:", &p
)) {
2848 qemu_opt_set(opts
, "mux", "on");
2851 if (strcmp(filename
, "null") == 0 ||
2852 strcmp(filename
, "pty") == 0 ||
2853 strcmp(filename
, "msmouse") == 0 ||
2854 strcmp(filename
, "braille") == 0 ||
2855 strcmp(filename
, "stdio") == 0) {
2856 qemu_opt_set(opts
, "backend", filename
);
2859 if (strstart(filename
, "vc", &p
)) {
2860 qemu_opt_set(opts
, "backend", "vc");
2862 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2864 qemu_opt_set(opts
, "width", width
);
2865 qemu_opt_set(opts
, "height", height
);
2866 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2868 qemu_opt_set(opts
, "cols", width
);
2869 qemu_opt_set(opts
, "rows", height
);
2876 if (strcmp(filename
, "con:") == 0) {
2877 qemu_opt_set(opts
, "backend", "console");
2880 if (strstart(filename
, "COM", NULL
)) {
2881 qemu_opt_set(opts
, "backend", "serial");
2882 qemu_opt_set(opts
, "path", filename
);
2885 if (strstart(filename
, "file:", &p
)) {
2886 qemu_opt_set(opts
, "backend", "file");
2887 qemu_opt_set(opts
, "path", p
);
2890 if (strstart(filename
, "pipe:", &p
)) {
2891 qemu_opt_set(opts
, "backend", "pipe");
2892 qemu_opt_set(opts
, "path", p
);
2895 if (strstart(filename
, "tcp:", &p
) ||
2896 strstart(filename
, "telnet:", &p
)) {
2897 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2899 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2902 qemu_opt_set(opts
, "backend", "socket");
2903 qemu_opt_set(opts
, "host", host
);
2904 qemu_opt_set(opts
, "port", port
);
2905 if (p
[pos
] == ',') {
2906 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2909 if (strstart(filename
, "telnet:", &p
))
2910 qemu_opt_set(opts
, "telnet", "on");
2913 if (strstart(filename
, "udp:", &p
)) {
2914 qemu_opt_set(opts
, "backend", "udp");
2915 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2917 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2921 qemu_opt_set(opts
, "host", host
);
2922 qemu_opt_set(opts
, "port", port
);
2923 if (p
[pos
] == '@') {
2925 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2927 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2931 qemu_opt_set(opts
, "localaddr", host
);
2932 qemu_opt_set(opts
, "localport", port
);
2936 if (strstart(filename
, "unix:", &p
)) {
2937 qemu_opt_set(opts
, "backend", "socket");
2938 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2942 if (strstart(filename
, "/dev/parport", NULL
) ||
2943 strstart(filename
, "/dev/ppi", NULL
)) {
2944 qemu_opt_set(opts
, "backend", "parport");
2945 qemu_opt_set(opts
, "path", filename
);
2948 if (strstart(filename
, "/dev/", NULL
)) {
2949 qemu_opt_set(opts
, "backend", "tty");
2950 qemu_opt_set(opts
, "path", filename
);
2955 qemu_opts_del(opts
);
2959 #ifdef HAVE_CHARDEV_PARPORT
2961 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
2963 const char *filename
= qemu_opt_get(opts
, "path");
2966 fd
= qemu_open(filename
, O_RDWR
);
2970 return qemu_chr_open_pp_fd(fd
);
2975 static const struct {
2977 CharDriverState
*(*open
)(QemuOpts
*opts
);
2978 } backend_table
[] = {
2979 { .name
= "null", .open
= qemu_chr_open_null
},
2980 { .name
= "socket", .open
= qemu_chr_open_socket
},
2981 { .name
= "udp", .open
= qemu_chr_open_udp
},
2982 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2983 { .name
= "vc", .open
= vc_init
},
2984 { .name
= "memory", .open
= qemu_chr_open_ringbuf
},
2986 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2987 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2988 { .name
= "console", .open
= qemu_chr_open_win_con
},
2989 { .name
= "serial", .open
= qemu_chr_open_win
},
2990 { .name
= "stdio", .open
= qemu_chr_open_win_stdio
},
2992 { .name
= "file", .open
= qemu_chr_open_file_out
},
2993 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2994 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2996 #ifdef CONFIG_BRLAPI
2997 { .name
= "braille", .open
= chr_baum_init
},
2999 #ifdef HAVE_CHARDEV_TTY
3000 { .name
= "tty", .open
= qemu_chr_open_tty
},
3001 { .name
= "serial", .open
= qemu_chr_open_tty
},
3002 { .name
= "pty", .open
= qemu_chr_open_pty
},
3004 #ifdef HAVE_CHARDEV_PARPORT
3005 { .name
= "parallel", .open
= qemu_chr_open_pp
},
3006 { .name
= "parport", .open
= qemu_chr_open_pp
},
3009 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
3010 #if SPICE_SERVER_VERSION >= 0x000c02
3011 { .name
= "spiceport", .open
= qemu_chr_open_spice_port
},
3016 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3017 void (*init
)(struct CharDriverState
*s
),
3020 CharDriverState
*chr
;
3023 if (qemu_opts_id(opts
) == NULL
) {
3024 error_setg(errp
, "chardev: no id specified");
3028 if (qemu_opt_get(opts
, "backend") == NULL
) {
3029 error_setg(errp
, "chardev: \"%s\" missing backend",
3030 qemu_opts_id(opts
));
3033 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
3034 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
3037 if (i
== ARRAY_SIZE(backend_table
)) {
3038 error_setg(errp
, "chardev: backend \"%s\" not found",
3039 qemu_opt_get(opts
, "backend"));
3043 chr
= backend_table
[i
].open(opts
);
3045 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3046 qemu_opt_get(opts
, "backend"));
3051 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3053 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3055 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3056 CharDriverState
*base
= chr
;
3057 int len
= strlen(qemu_opts_id(opts
)) + 6;
3058 base
->label
= g_malloc(len
);
3059 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3060 chr
= qemu_chr_open_mux(base
);
3061 chr
->filename
= base
->filename
;
3062 chr
->avail_connections
= MAX_MUX
;
3063 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3065 chr
->avail_connections
= 1;
3067 chr
->label
= g_strdup(qemu_opts_id(opts
));
3072 qemu_opts_del(opts
);
3076 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3079 CharDriverState
*chr
;
3083 if (strstart(filename
, "chardev:", &p
)) {
3084 return qemu_chr_find(p
);
3087 opts
= qemu_chr_parse_compat(label
, filename
);
3091 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3092 if (error_is_set(&err
)) {
3093 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3096 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3097 monitor_init(chr
, MONITOR_USE_READLINE
);
3102 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3104 if (chr
->chr_set_echo
) {
3105 chr
->chr_set_echo(chr
, echo
);
3109 void qemu_chr_fe_open(struct CharDriverState
*chr
)
3111 if (chr
->chr_guest_open
) {
3112 chr
->chr_guest_open(chr
);
3116 void qemu_chr_fe_close(struct CharDriverState
*chr
)
3118 if (chr
->chr_guest_close
) {
3119 chr
->chr_guest_close(chr
);
3123 void qemu_chr_delete(CharDriverState
*chr
)
3125 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3126 if (chr
->chr_close
) {
3127 chr
->chr_close(chr
);
3129 g_free(chr
->filename
);
3132 qemu_opts_del(chr
->opts
);
3137 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3139 ChardevInfoList
*chr_list
= NULL
;
3140 CharDriverState
*chr
;
3142 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3143 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3144 info
->value
= g_malloc0(sizeof(*info
->value
));
3145 info
->value
->label
= g_strdup(chr
->label
);
3146 info
->value
->filename
= g_strdup(chr
->filename
);
3148 info
->next
= chr_list
;
3155 CharDriverState
*qemu_chr_find(const char *name
)
3157 CharDriverState
*chr
;
3159 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3160 if (strcmp(chr
->label
, name
) != 0)
3167 /* Get a character (serial) device interface. */
3168 CharDriverState
*qemu_char_get_next_serial(void)
3170 static int next_serial
;
3172 /* FIXME: This function needs to go away: use chardev properties! */
3173 return serial_hds
[next_serial
++];
3176 QemuOptsList qemu_chardev_opts
= {
3178 .implied_opt_name
= "backend",
3179 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3183 .type
= QEMU_OPT_STRING
,
3186 .type
= QEMU_OPT_STRING
,
3189 .type
= QEMU_OPT_STRING
,
3192 .type
= QEMU_OPT_STRING
,
3194 .name
= "localaddr",
3195 .type
= QEMU_OPT_STRING
,
3197 .name
= "localport",
3198 .type
= QEMU_OPT_STRING
,
3201 .type
= QEMU_OPT_NUMBER
,
3204 .type
= QEMU_OPT_BOOL
,
3207 .type
= QEMU_OPT_BOOL
,
3210 .type
= QEMU_OPT_BOOL
,
3213 .type
= QEMU_OPT_BOOL
,
3216 .type
= QEMU_OPT_BOOL
,
3219 .type
= QEMU_OPT_BOOL
,
3222 .type
= QEMU_OPT_NUMBER
,
3225 .type
= QEMU_OPT_NUMBER
,
3228 .type
= QEMU_OPT_NUMBER
,
3231 .type
= QEMU_OPT_NUMBER
,
3234 .type
= QEMU_OPT_BOOL
,
3237 .type
= QEMU_OPT_BOOL
,
3240 .type
= QEMU_OPT_STRING
,
3243 .type
= QEMU_OPT_NUMBER
,
3246 .type
= QEMU_OPT_SIZE
,
3248 { /* end of list */ }
3254 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3259 error_setg(errp
, "input file not supported");
3263 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3264 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3265 if (out
== INVALID_HANDLE_VALUE
) {
3266 error_setg(errp
, "open %s failed", file
->out
);
3269 return qemu_chr_open_win_file(out
);
3272 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3275 return qemu_chr_open_win_path(serial
->device
);
3278 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3281 error_setg(errp
, "character device backend type 'parallel' not supported");
3287 static int qmp_chardev_open_file_source(char *src
, int flags
,
3292 TFR(fd
= qemu_open(src
, flags
, 0666));
3294 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3299 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3301 int flags
, in
= -1, out
= -1;
3303 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3304 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3305 if (error_is_set(errp
)) {
3311 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3312 if (error_is_set(errp
)) {
3318 return qemu_chr_open_fd(in
, out
);
3321 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3324 #ifdef HAVE_CHARDEV_TTY
3327 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3328 if (error_is_set(errp
)) {
3331 socket_set_nonblock(fd
);
3332 return qemu_chr_open_tty_fd(fd
);
3334 error_setg(errp
, "character device backend type 'serial' not supported");
3339 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3342 #ifdef HAVE_CHARDEV_PARPORT
3345 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3346 if (error_is_set(errp
)) {
3349 return qemu_chr_open_pp_fd(fd
);
3351 error_setg(errp
, "character device backend type 'parallel' not supported");
3358 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3361 SocketAddress
*addr
= sock
->addr
;
3362 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3363 bool is_listen
= sock
->has_server
? sock
->server
: true;
3364 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3365 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3369 fd
= socket_listen(addr
, errp
);
3371 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3373 if (error_is_set(errp
)) {
3376 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3377 is_telnet
, is_waitconnect
, errp
);
3380 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3383 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3384 CharDriverState
*chr
= NULL
;
3386 chr
= qemu_chr_find(id
);
3388 error_setg(errp
, "Chardev '%s' already exists", id
);
3393 switch (backend
->kind
) {
3394 case CHARDEV_BACKEND_KIND_FILE
:
3395 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3397 case CHARDEV_BACKEND_KIND_SERIAL
:
3398 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3400 case CHARDEV_BACKEND_KIND_PARALLEL
:
3401 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3403 case CHARDEV_BACKEND_KIND_SOCKET
:
3404 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3406 #ifdef HAVE_CHARDEV_TTY
3407 case CHARDEV_BACKEND_KIND_PTY
:
3409 /* qemu_chr_open_pty sets "path" in opts */
3411 opts
= qemu_opts_create_nofail(qemu_find_opts("chardev"));
3412 chr
= qemu_chr_open_pty(opts
);
3413 ret
->pty
= g_strdup(qemu_opt_get(opts
, "path"));
3414 ret
->has_pty
= true;
3415 qemu_opts_del(opts
);
3419 case CHARDEV_BACKEND_KIND_NULL
:
3420 chr
= qemu_chr_open_null(NULL
);
3423 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3427 if (chr
== NULL
&& !error_is_set(errp
)) {
3428 error_setg(errp
, "Failed to create chardev");
3431 chr
->label
= g_strdup(id
);
3432 chr
->avail_connections
= 1;
3433 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3441 void qmp_chardev_remove(const char *id
, Error
**errp
)
3443 CharDriverState
*chr
;
3445 chr
= qemu_chr_find(id
);
3447 error_setg(errp
, "Chardev '%s' not found", id
);
3450 if (chr
->chr_can_read
|| chr
->chr_read
||
3451 chr
->chr_event
|| chr
->handler_opaque
) {
3452 error_setg(errp
, "Chardev '%s' is busy", id
);
3455 qemu_chr_delete(chr
);