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) {
545 typedef struct IOWatchPoll
550 IOCanReadHandler
*fd_can_read
;
553 QTAILQ_ENTRY(IOWatchPoll
) node
;
556 static QTAILQ_HEAD(, IOWatchPoll
) io_watch_poll_list
=
557 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list
);
559 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
563 QTAILQ_FOREACH(i
, &io_watch_poll_list
, node
) {
564 if (i
->src
== source
) {
572 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
574 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
576 iwp
->max_size
= iwp
->fd_can_read(iwp
->opaque
);
577 if (iwp
->max_size
== 0) {
581 return g_io_watch_funcs
.prepare(source
, timeout_
);
584 static gboolean
io_watch_poll_check(GSource
*source
)
586 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
588 if (iwp
->max_size
== 0) {
592 return g_io_watch_funcs
.check(source
);
595 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
598 return g_io_watch_funcs
.dispatch(source
, callback
, user_data
);
601 static void io_watch_poll_finalize(GSource
*source
)
603 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
604 QTAILQ_REMOVE(&io_watch_poll_list
, iwp
, node
);
605 g_io_watch_funcs
.finalize(source
);
608 static GSourceFuncs io_watch_poll_funcs
= {
609 .prepare
= io_watch_poll_prepare
,
610 .check
= io_watch_poll_check
,
611 .dispatch
= io_watch_poll_dispatch
,
612 .finalize
= io_watch_poll_finalize
,
615 /* Can only be used for read */
616 static guint
io_add_watch_poll(GIOChannel
*channel
,
617 IOCanReadHandler
*fd_can_read
,
625 src
= g_io_create_watch(channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
626 g_source_set_funcs(src
, &io_watch_poll_funcs
);
627 g_source_set_callback(src
, (GSourceFunc
)fd_read
, user_data
, NULL
);
628 tag
= g_source_attach(src
, NULL
);
631 iwp
= g_malloc0(sizeof(*iwp
));
634 iwp
->fd_can_read
= fd_can_read
;
635 iwp
->opaque
= user_data
;
637 QTAILQ_INSERT_HEAD(&io_watch_poll_list
, iwp
, node
);
642 static GIOChannel
*io_channel_from_fd(int fd
)
650 chan
= g_io_channel_unix_new(fd
);
652 g_io_channel_set_encoding(chan
, NULL
, NULL
);
653 g_io_channel_set_buffered(chan
, FALSE
);
658 static int io_channel_send_all(GIOChannel
*fd
, const void *_buf
, int len1
)
663 const uint8_t *buf
= _buf
;
667 status
= g_io_channel_write_chars(fd
, (const gchar
*)buf
, len
,
668 &bytes_written
, NULL
);
669 if (status
!= G_IO_STATUS_NORMAL
) {
670 if (status
!= G_IO_STATUS_AGAIN
) {
673 } else if (status
== G_IO_STATUS_EOF
) {
676 buf
+= bytes_written
;
677 len
-= bytes_written
;
690 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
692 FDCharDriver
*s
= chr
->opaque
;
693 return send_all(s
->fd_out
, buf
, len
);
696 static int fd_chr_read_poll(void *opaque
)
698 CharDriverState
*chr
= opaque
;
699 FDCharDriver
*s
= chr
->opaque
;
701 s
->max_size
= qemu_chr_be_can_write(chr
);
705 static void fd_chr_read(void *opaque
)
707 CharDriverState
*chr
= opaque
;
708 FDCharDriver
*s
= chr
->opaque
;
710 uint8_t buf
[READ_BUF_LEN
];
713 if (len
> s
->max_size
)
717 size
= read(s
->fd_in
, buf
, len
);
719 /* FD has been closed. Remove it from the active list. */
720 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
721 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
725 qemu_chr_be_write(chr
, buf
, size
);
729 static void fd_chr_update_read_handler(CharDriverState
*chr
)
731 FDCharDriver
*s
= chr
->opaque
;
734 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
735 fd_chr_read
, NULL
, chr
);
739 static void fd_chr_close(struct CharDriverState
*chr
)
741 FDCharDriver
*s
= chr
->opaque
;
744 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
748 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
751 /* open a character device to a unix fd */
752 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
754 CharDriverState
*chr
;
757 chr
= g_malloc0(sizeof(CharDriverState
));
758 s
= g_malloc0(sizeof(FDCharDriver
));
762 chr
->chr_write
= fd_chr_write
;
763 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
764 chr
->chr_close
= fd_chr_close
;
766 qemu_chr_generic_open(chr
);
771 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
775 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
776 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
780 return qemu_chr_open_fd(-1, fd_out
);
783 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
786 char filename_in
[256], filename_out
[256];
787 const char *filename
= qemu_opt_get(opts
, "path");
789 if (filename
== NULL
) {
790 fprintf(stderr
, "chardev: pipe: no filename given\n");
794 snprintf(filename_in
, 256, "%s.in", filename
);
795 snprintf(filename_out
, 256, "%s.out", filename
);
796 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
797 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
798 if (fd_in
< 0 || fd_out
< 0) {
803 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
808 return qemu_chr_open_fd(fd_in
, fd_out
);
811 /* init terminal so that we can grab keys */
812 static struct termios oldtty
;
813 static int old_fd0_flags
;
814 static bool stdio_allow_signal
;
816 static void term_exit(void)
818 tcsetattr (0, TCSANOW
, &oldtty
);
819 fcntl(0, F_SETFL
, old_fd0_flags
);
822 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
828 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
829 |INLCR
|IGNCR
|ICRNL
|IXON
);
830 tty
.c_oflag
|= OPOST
;
831 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
832 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
837 /* if graphical mode, we allow Ctrl-C handling */
838 if (!stdio_allow_signal
)
839 tty
.c_lflag
&= ~ISIG
;
841 tcsetattr (0, TCSANOW
, &tty
);
844 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
850 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
852 CharDriverState
*chr
;
854 if (is_daemonized()) {
855 error_report("cannot use stdio with -daemonize");
858 old_fd0_flags
= fcntl(0, F_GETFL
);
859 tcgetattr (0, &oldtty
);
860 fcntl(0, F_SETFL
, O_NONBLOCK
);
863 chr
= qemu_chr_open_fd(0, 1);
864 chr
->chr_close
= qemu_chr_close_stdio
;
865 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
866 stdio_allow_signal
= qemu_opt_get_bool(opts
, "signal",
867 display_type
!= DT_NOGRAPHIC
);
868 qemu_chr_fe_set_echo(chr
, false);
874 /* Once Solaris has openpty(), this is going to be removed. */
875 static int openpty(int *amaster
, int *aslave
, char *name
,
876 struct termios
*termp
, struct winsize
*winp
)
879 int mfd
= -1, sfd
= -1;
881 *amaster
= *aslave
= -1;
883 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
887 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
890 if ((slave
= ptsname(mfd
)) == NULL
)
893 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
896 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
897 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
905 ioctl(sfd
, TIOCSWINSZ
, winp
);
916 static void cfmakeraw (struct termios
*termios_p
)
918 termios_p
->c_iflag
&=
919 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
920 termios_p
->c_oflag
&= ~OPOST
;
921 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
922 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
923 termios_p
->c_cflag
|= CS8
;
925 termios_p
->c_cc
[VMIN
] = 0;
926 termios_p
->c_cc
[VTIME
] = 0;
930 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
931 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
932 || defined(__GLIBC__)
934 #define HAVE_CHARDEV_TTY 1
944 static void pty_chr_update_read_handler(CharDriverState
*chr
);
945 static void pty_chr_state(CharDriverState
*chr
, int connected
);
947 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
949 PtyCharDriver
*s
= chr
->opaque
;
952 /* guest sends data, check for (re-)connect */
953 pty_chr_update_read_handler(chr
);
956 return send_all(s
->fd
, buf
, len
);
959 static int pty_chr_read_poll(void *opaque
)
961 CharDriverState
*chr
= opaque
;
962 PtyCharDriver
*s
= chr
->opaque
;
964 s
->read_bytes
= qemu_chr_be_can_write(chr
);
965 return s
->read_bytes
;
968 static void pty_chr_read(void *opaque
)
970 CharDriverState
*chr
= opaque
;
971 PtyCharDriver
*s
= chr
->opaque
;
973 uint8_t buf
[READ_BUF_LEN
];
976 if (len
> s
->read_bytes
)
980 size
= read(s
->fd
, buf
, len
);
981 if ((size
== -1 && errno
== EIO
) ||
983 pty_chr_state(chr
, 0);
987 pty_chr_state(chr
, 1);
988 qemu_chr_be_write(chr
, buf
, size
);
992 static void pty_chr_update_read_handler(CharDriverState
*chr
)
994 PtyCharDriver
*s
= chr
->opaque
;
996 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
997 pty_chr_read
, NULL
, chr
);
1000 * Short timeout here: just need wait long enougth that qemu makes
1001 * it through the poll loop once. When reconnected we want a
1002 * short timeout so we notice it almost instantly. Otherwise
1003 * read() gives us -EIO instantly, making pty_chr_state() reset the
1004 * timeout to the normal (much longer) poll interval before the
1007 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 10);
1010 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1012 PtyCharDriver
*s
= chr
->opaque
;
1015 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
1018 /* (re-)connect poll interval for idle guests: once per second.
1019 * We check more frequently in case the guests sends data to
1020 * the virtual device linked to our pty. */
1021 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 1000);
1024 qemu_chr_generic_open(chr
);
1029 static void pty_chr_timer(void *opaque
)
1031 struct CharDriverState
*chr
= opaque
;
1032 PtyCharDriver
*s
= chr
->opaque
;
1037 /* If we arrive here without polling being cleared due
1038 * read returning -EIO, then we are (re-)connected */
1039 pty_chr_state(chr
, 1);
1044 pty_chr_update_read_handler(chr
);
1047 static void pty_chr_close(struct CharDriverState
*chr
)
1049 PtyCharDriver
*s
= chr
->opaque
;
1051 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
1053 qemu_del_timer(s
->timer
);
1054 qemu_free_timer(s
->timer
);
1056 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1059 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1061 CharDriverState
*chr
;
1065 int master_fd
, slave_fd
, len
;
1066 #if defined(__OpenBSD__) || defined(__DragonFly__)
1067 char pty_name
[PATH_MAX
];
1068 #define q_ptsname(x) pty_name
1070 char *pty_name
= NULL
;
1071 #define q_ptsname(x) ptsname(x)
1074 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1078 /* Set raw attributes on the pty. */
1079 tcgetattr(slave_fd
, &tty
);
1081 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1084 chr
= g_malloc0(sizeof(CharDriverState
));
1086 len
= strlen(q_ptsname(master_fd
)) + 5;
1087 chr
->filename
= g_malloc(len
);
1088 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(master_fd
));
1089 qemu_opt_set(opts
, "path", q_ptsname(master_fd
));
1091 label
= qemu_opts_id(opts
);
1092 fprintf(stderr
, "char device redirected to %s%s%s%s\n",
1093 q_ptsname(master_fd
),
1094 label
? " (label " : "",
1098 s
= g_malloc0(sizeof(PtyCharDriver
));
1100 chr
->chr_write
= pty_chr_write
;
1101 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1102 chr
->chr_close
= pty_chr_close
;
1105 s
->timer
= qemu_new_timer_ms(rt_clock
, pty_chr_timer
, chr
);
1110 static void tty_serial_init(int fd
, int speed
,
1111 int parity
, int data_bits
, int stop_bits
)
1117 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1118 speed
, parity
, data_bits
, stop_bits
);
1120 tcgetattr (fd
, &tty
);
1122 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1123 speed
= speed
* 10 / 11;
1140 /* Non-Posix values follow. They may be unsupported on some systems. */
1142 check_speed(115200);
1144 check_speed(230400);
1147 check_speed(460800);
1150 check_speed(500000);
1153 check_speed(576000);
1156 check_speed(921600);
1159 check_speed(1000000);
1162 check_speed(1152000);
1165 check_speed(1500000);
1168 check_speed(2000000);
1171 check_speed(2500000);
1174 check_speed(3000000);
1177 check_speed(3500000);
1180 check_speed(4000000);
1185 cfsetispeed(&tty
, spd
);
1186 cfsetospeed(&tty
, spd
);
1188 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1189 |INLCR
|IGNCR
|ICRNL
|IXON
);
1190 tty
.c_oflag
|= OPOST
;
1191 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1192 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1213 tty
.c_cflag
|= PARENB
;
1216 tty
.c_cflag
|= PARENB
| PARODD
;
1220 tty
.c_cflag
|= CSTOPB
;
1222 tcsetattr (fd
, TCSANOW
, &tty
);
1225 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1227 FDCharDriver
*s
= chr
->opaque
;
1230 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1232 QEMUSerialSetParams
*ssp
= arg
;
1233 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1234 ssp
->data_bits
, ssp
->stop_bits
);
1237 case CHR_IOCTL_SERIAL_SET_BREAK
:
1239 int enable
= *(int *)arg
;
1241 tcsendbreak(s
->fd_in
, 1);
1244 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1247 int *targ
= (int *)arg
;
1248 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1250 if (sarg
& TIOCM_CTS
)
1251 *targ
|= CHR_TIOCM_CTS
;
1252 if (sarg
& TIOCM_CAR
)
1253 *targ
|= CHR_TIOCM_CAR
;
1254 if (sarg
& TIOCM_DSR
)
1255 *targ
|= CHR_TIOCM_DSR
;
1256 if (sarg
& TIOCM_RI
)
1257 *targ
|= CHR_TIOCM_RI
;
1258 if (sarg
& TIOCM_DTR
)
1259 *targ
|= CHR_TIOCM_DTR
;
1260 if (sarg
& TIOCM_RTS
)
1261 *targ
|= CHR_TIOCM_RTS
;
1264 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1266 int sarg
= *(int *)arg
;
1268 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1269 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1270 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1271 if (sarg
& CHR_TIOCM_CTS
)
1273 if (sarg
& CHR_TIOCM_CAR
)
1275 if (sarg
& CHR_TIOCM_DSR
)
1277 if (sarg
& CHR_TIOCM_RI
)
1279 if (sarg
& CHR_TIOCM_DTR
)
1281 if (sarg
& CHR_TIOCM_RTS
)
1283 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1292 static void qemu_chr_close_tty(CharDriverState
*chr
)
1294 FDCharDriver
*s
= chr
->opaque
;
1308 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1310 CharDriverState
*chr
;
1312 tty_serial_init(fd
, 115200, 'N', 8, 1);
1313 chr
= qemu_chr_open_fd(fd
, fd
);
1314 chr
->chr_ioctl
= tty_serial_ioctl
;
1315 chr
->chr_close
= qemu_chr_close_tty
;
1319 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1321 const char *filename
= qemu_opt_get(opts
, "path");
1324 TFR(fd
= qemu_open(filename
, O_RDWR
| O_NONBLOCK
));
1328 return qemu_chr_open_tty_fd(fd
);
1330 #endif /* __linux__ || __sun__ */
1332 #if defined(__linux__)
1334 #define HAVE_CHARDEV_PARPORT 1
1339 } ParallelCharDriver
;
1341 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1343 if (s
->mode
!= mode
) {
1345 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1352 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1354 ParallelCharDriver
*drv
= chr
->opaque
;
1359 case CHR_IOCTL_PP_READ_DATA
:
1360 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1362 *(uint8_t *)arg
= b
;
1364 case CHR_IOCTL_PP_WRITE_DATA
:
1365 b
= *(uint8_t *)arg
;
1366 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1369 case CHR_IOCTL_PP_READ_CONTROL
:
1370 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1372 /* Linux gives only the lowest bits, and no way to know data
1373 direction! For better compatibility set the fixed upper
1375 *(uint8_t *)arg
= b
| 0xc0;
1377 case CHR_IOCTL_PP_WRITE_CONTROL
:
1378 b
= *(uint8_t *)arg
;
1379 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1382 case CHR_IOCTL_PP_READ_STATUS
:
1383 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1385 *(uint8_t *)arg
= b
;
1387 case CHR_IOCTL_PP_DATA_DIR
:
1388 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1391 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1392 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1393 struct ParallelIOArg
*parg
= arg
;
1394 int n
= read(fd
, parg
->buffer
, parg
->count
);
1395 if (n
!= parg
->count
) {
1400 case CHR_IOCTL_PP_EPP_READ
:
1401 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1402 struct ParallelIOArg
*parg
= arg
;
1403 int n
= read(fd
, parg
->buffer
, parg
->count
);
1404 if (n
!= parg
->count
) {
1409 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1410 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1411 struct ParallelIOArg
*parg
= arg
;
1412 int n
= write(fd
, parg
->buffer
, parg
->count
);
1413 if (n
!= parg
->count
) {
1418 case CHR_IOCTL_PP_EPP_WRITE
:
1419 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1420 struct ParallelIOArg
*parg
= arg
;
1421 int n
= write(fd
, parg
->buffer
, parg
->count
);
1422 if (n
!= parg
->count
) {
1433 static void pp_close(CharDriverState
*chr
)
1435 ParallelCharDriver
*drv
= chr
->opaque
;
1438 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1439 ioctl(fd
, PPRELEASE
);
1442 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1445 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1447 CharDriverState
*chr
;
1448 ParallelCharDriver
*drv
;
1450 if (ioctl(fd
, PPCLAIM
) < 0) {
1455 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1457 drv
->mode
= IEEE1284_MODE_COMPAT
;
1459 chr
= g_malloc0(sizeof(CharDriverState
));
1460 chr
->chr_write
= null_chr_write
;
1461 chr
->chr_ioctl
= pp_ioctl
;
1462 chr
->chr_close
= pp_close
;
1465 qemu_chr_generic_open(chr
);
1469 #endif /* __linux__ */
1471 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1473 #define HAVE_CHARDEV_PARPORT 1
1475 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1477 int fd
= (int)(intptr_t)chr
->opaque
;
1481 case CHR_IOCTL_PP_READ_DATA
:
1482 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1484 *(uint8_t *)arg
= b
;
1486 case CHR_IOCTL_PP_WRITE_DATA
:
1487 b
= *(uint8_t *)arg
;
1488 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1491 case CHR_IOCTL_PP_READ_CONTROL
:
1492 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1494 *(uint8_t *)arg
= b
;
1496 case CHR_IOCTL_PP_WRITE_CONTROL
:
1497 b
= *(uint8_t *)arg
;
1498 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1501 case CHR_IOCTL_PP_READ_STATUS
:
1502 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1504 *(uint8_t *)arg
= b
;
1512 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1514 CharDriverState
*chr
;
1516 chr
= g_malloc0(sizeof(CharDriverState
));
1517 chr
->opaque
= (void *)(intptr_t)fd
;
1518 chr
->chr_write
= null_chr_write
;
1519 chr
->chr_ioctl
= pp_ioctl
;
1528 HANDLE hcom
, hrecv
, hsend
;
1529 OVERLAPPED orecv
, osend
;
1536 HANDLE hInputReadyEvent
;
1537 HANDLE hInputDoneEvent
;
1538 HANDLE hInputThread
;
1539 uint8_t win_stdio_buf
;
1540 } WinStdioCharState
;
1542 #define NSENDBUF 2048
1543 #define NRECVBUF 2048
1544 #define MAXCONNECT 1
1545 #define NTIMEOUT 5000
1547 static int win_chr_poll(void *opaque
);
1548 static int win_chr_pipe_poll(void *opaque
);
1550 static void win_chr_close(CharDriverState
*chr
)
1552 WinCharState
*s
= chr
->opaque
;
1555 CloseHandle(s
->hsend
);
1559 CloseHandle(s
->hrecv
);
1563 CloseHandle(s
->hcom
);
1567 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1569 qemu_del_polling_cb(win_chr_poll
, chr
);
1571 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1574 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1576 WinCharState
*s
= chr
->opaque
;
1578 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1583 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1585 fprintf(stderr
, "Failed CreateEvent\n");
1588 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1590 fprintf(stderr
, "Failed CreateEvent\n");
1594 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1595 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1596 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1597 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1602 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1603 fprintf(stderr
, "Failed SetupComm\n");
1607 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1608 size
= sizeof(COMMCONFIG
);
1609 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1610 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1611 CommConfigDialog(filename
, NULL
, &comcfg
);
1613 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1614 fprintf(stderr
, "Failed SetCommState\n");
1618 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1619 fprintf(stderr
, "Failed SetCommMask\n");
1623 cto
.ReadIntervalTimeout
= MAXDWORD
;
1624 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1625 fprintf(stderr
, "Failed SetCommTimeouts\n");
1629 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1630 fprintf(stderr
, "Failed ClearCommError\n");
1633 qemu_add_polling_cb(win_chr_poll
, chr
);
1641 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1643 WinCharState
*s
= chr
->opaque
;
1644 DWORD len
, ret
, size
, err
;
1647 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1648 s
->osend
.hEvent
= s
->hsend
;
1651 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1653 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1655 err
= GetLastError();
1656 if (err
== ERROR_IO_PENDING
) {
1657 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1675 static int win_chr_read_poll(CharDriverState
*chr
)
1677 WinCharState
*s
= chr
->opaque
;
1679 s
->max_size
= qemu_chr_be_can_write(chr
);
1683 static void win_chr_readfile(CharDriverState
*chr
)
1685 WinCharState
*s
= chr
->opaque
;
1687 uint8_t buf
[READ_BUF_LEN
];
1690 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1691 s
->orecv
.hEvent
= s
->hrecv
;
1692 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1694 err
= GetLastError();
1695 if (err
== ERROR_IO_PENDING
) {
1696 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1701 qemu_chr_be_write(chr
, buf
, size
);
1705 static void win_chr_read(CharDriverState
*chr
)
1707 WinCharState
*s
= chr
->opaque
;
1709 if (s
->len
> s
->max_size
)
1710 s
->len
= s
->max_size
;
1714 win_chr_readfile(chr
);
1717 static int win_chr_poll(void *opaque
)
1719 CharDriverState
*chr
= opaque
;
1720 WinCharState
*s
= chr
->opaque
;
1724 ClearCommError(s
->hcom
, &comerr
, &status
);
1725 if (status
.cbInQue
> 0) {
1726 s
->len
= status
.cbInQue
;
1727 win_chr_read_poll(chr
);
1734 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1736 CharDriverState
*chr
;
1739 chr
= g_malloc0(sizeof(CharDriverState
));
1740 s
= g_malloc0(sizeof(WinCharState
));
1742 chr
->chr_write
= win_chr_write
;
1743 chr
->chr_close
= win_chr_close
;
1745 if (win_chr_init(chr
, filename
) < 0) {
1750 qemu_chr_generic_open(chr
);
1754 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1756 return qemu_chr_open_win_path(qemu_opt_get(opts
, "path"));
1759 static int win_chr_pipe_poll(void *opaque
)
1761 CharDriverState
*chr
= opaque
;
1762 WinCharState
*s
= chr
->opaque
;
1765 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1768 win_chr_read_poll(chr
);
1775 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1777 WinCharState
*s
= chr
->opaque
;
1785 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1787 fprintf(stderr
, "Failed CreateEvent\n");
1790 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1792 fprintf(stderr
, "Failed CreateEvent\n");
1796 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1797 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1798 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1800 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1801 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1802 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1807 ZeroMemory(&ov
, sizeof(ov
));
1808 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1809 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1811 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1815 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1817 fprintf(stderr
, "Failed GetOverlappedResult\n");
1819 CloseHandle(ov
.hEvent
);
1826 CloseHandle(ov
.hEvent
);
1829 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1838 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1840 const char *filename
= qemu_opt_get(opts
, "path");
1841 CharDriverState
*chr
;
1844 chr
= g_malloc0(sizeof(CharDriverState
));
1845 s
= g_malloc0(sizeof(WinCharState
));
1847 chr
->chr_write
= win_chr_write
;
1848 chr
->chr_close
= win_chr_close
;
1850 if (win_chr_pipe_init(chr
, filename
) < 0) {
1855 qemu_chr_generic_open(chr
);
1859 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1861 CharDriverState
*chr
;
1864 chr
= g_malloc0(sizeof(CharDriverState
));
1865 s
= g_malloc0(sizeof(WinCharState
));
1868 chr
->chr_write
= win_chr_write
;
1869 qemu_chr_generic_open(chr
);
1873 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1875 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1878 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1880 const char *file_out
= qemu_opt_get(opts
, "path");
1883 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1884 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1885 if (fd_out
== INVALID_HANDLE_VALUE
) {
1889 return qemu_chr_open_win_file(fd_out
);
1892 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1894 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1901 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1911 static void win_stdio_wait_func(void *opaque
)
1913 CharDriverState
*chr
= opaque
;
1914 WinStdioCharState
*stdio
= chr
->opaque
;
1915 INPUT_RECORD buf
[4];
1920 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1924 /* Avoid error storm */
1925 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1929 for (i
= 0; i
< dwSize
; i
++) {
1930 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1932 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1934 if (kev
->uChar
.AsciiChar
!= 0) {
1935 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1936 if (qemu_chr_be_can_write(chr
)) {
1937 uint8_t c
= kev
->uChar
.AsciiChar
;
1938 qemu_chr_be_write(chr
, &c
, 1);
1946 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
1948 CharDriverState
*chr
= param
;
1949 WinStdioCharState
*stdio
= chr
->opaque
;
1955 /* Wait for one byte */
1956 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
1958 /* Exit in case of error, continue if nothing read */
1966 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1967 if (stdio
->win_stdio_buf
== '\r') {
1971 /* Signal the main thread and wait until the byte was eaten */
1972 if (!SetEvent(stdio
->hInputReadyEvent
)) {
1975 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
1981 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
1985 static void win_stdio_thread_wait_func(void *opaque
)
1987 CharDriverState
*chr
= opaque
;
1988 WinStdioCharState
*stdio
= chr
->opaque
;
1990 if (qemu_chr_be_can_write(chr
)) {
1991 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
1994 SetEvent(stdio
->hInputDoneEvent
);
1997 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
1999 WinStdioCharState
*stdio
= chr
->opaque
;
2002 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2005 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2007 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2011 static void win_stdio_close(CharDriverState
*chr
)
2013 WinStdioCharState
*stdio
= chr
->opaque
;
2015 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2016 CloseHandle(stdio
->hInputReadyEvent
);
2018 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2019 CloseHandle(stdio
->hInputDoneEvent
);
2021 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2022 TerminateThread(stdio
->hInputThread
, 0);
2025 g_free(chr
->opaque
);
2029 static CharDriverState
*qemu_chr_open_win_stdio(QemuOpts
*opts
)
2031 CharDriverState
*chr
;
2032 WinStdioCharState
*stdio
;
2036 chr
= g_malloc0(sizeof(CharDriverState
));
2037 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2039 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2040 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2041 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2045 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2047 chr
->opaque
= stdio
;
2048 chr
->chr_write
= win_stdio_write
;
2049 chr
->chr_close
= win_stdio_close
;
2052 if (qemu_add_wait_object(stdio
->hStdIn
,
2053 win_stdio_wait_func
, chr
)) {
2054 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2059 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2060 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2061 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2064 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2065 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2066 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2067 fprintf(stderr
, "cannot create stdio thread or event\n");
2070 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2071 win_stdio_thread_wait_func
, chr
)) {
2072 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2076 dwMode
|= ENABLE_LINE_INPUT
;
2079 /* set the terminal in raw mode */
2080 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2081 dwMode
|= ENABLE_PROCESSED_INPUT
;
2084 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2086 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2087 qemu_chr_fe_set_echo(chr
, false);
2091 #endif /* !_WIN32 */
2093 /***********************************************************/
2094 /* UDP Net console */
2098 uint8_t buf
[READ_BUF_LEN
];
2104 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2106 NetCharDriver
*s
= chr
->opaque
;
2108 return send(s
->fd
, (const void *)buf
, len
, 0);
2111 static int udp_chr_read_poll(void *opaque
)
2113 CharDriverState
*chr
= opaque
;
2114 NetCharDriver
*s
= chr
->opaque
;
2116 s
->max_size
= qemu_chr_be_can_write(chr
);
2118 /* If there were any stray characters in the queue process them
2121 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2122 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2124 s
->max_size
= qemu_chr_be_can_write(chr
);
2129 static void udp_chr_read(void *opaque
)
2131 CharDriverState
*chr
= opaque
;
2132 NetCharDriver
*s
= chr
->opaque
;
2134 if (s
->max_size
== 0)
2136 s
->bufcnt
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2137 s
->bufptr
= s
->bufcnt
;
2142 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2143 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2145 s
->max_size
= qemu_chr_be_can_write(chr
);
2149 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2151 NetCharDriver
*s
= chr
->opaque
;
2154 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2155 udp_chr_read
, NULL
, chr
);
2159 static void udp_chr_close(CharDriverState
*chr
)
2161 NetCharDriver
*s
= chr
->opaque
;
2163 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2167 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2170 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2172 CharDriverState
*chr
= NULL
;
2173 NetCharDriver
*s
= NULL
;
2174 Error
*local_err
= NULL
;
2177 chr
= g_malloc0(sizeof(CharDriverState
));
2178 s
= g_malloc0(sizeof(NetCharDriver
));
2180 fd
= inet_dgram_opts(opts
, &local_err
);
2189 chr
->chr_write
= udp_chr_write
;
2190 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2191 chr
->chr_close
= udp_chr_close
;
2196 qerror_report_err(local_err
);
2197 error_free(local_err
);
2207 /***********************************************************/
2208 /* TCP Net console */
2220 static void tcp_chr_accept(void *opaque
);
2222 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2224 TCPCharDriver
*s
= chr
->opaque
;
2226 return send_all(s
->fd
, buf
, len
);
2228 /* XXX: indicate an error ? */
2233 static int tcp_chr_read_poll(void *opaque
)
2235 CharDriverState
*chr
= opaque
;
2236 TCPCharDriver
*s
= chr
->opaque
;
2239 s
->max_size
= qemu_chr_be_can_write(chr
);
2244 #define IAC_BREAK 243
2245 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2247 uint8_t *buf
, int *size
)
2249 /* Handle any telnet client's basic IAC options to satisfy char by
2250 * char mode with no echo. All IAC options will be removed from
2251 * the buf and the do_telnetopt variable will be used to track the
2252 * state of the width of the IAC information.
2254 * IAC commands come in sets of 3 bytes with the exception of the
2255 * "IAC BREAK" command and the double IAC.
2261 for (i
= 0; i
< *size
; i
++) {
2262 if (s
->do_telnetopt
> 1) {
2263 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2264 /* Double IAC means send an IAC */
2268 s
->do_telnetopt
= 1;
2270 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2271 /* Handle IAC break commands by sending a serial break */
2272 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2277 if (s
->do_telnetopt
>= 4) {
2278 s
->do_telnetopt
= 1;
2281 if ((unsigned char)buf
[i
] == IAC
) {
2282 s
->do_telnetopt
= 2;
2293 static int tcp_get_msgfd(CharDriverState
*chr
)
2295 TCPCharDriver
*s
= chr
->opaque
;
2302 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2304 TCPCharDriver
*s
= chr
->opaque
;
2305 struct cmsghdr
*cmsg
;
2307 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2310 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2311 cmsg
->cmsg_level
!= SOL_SOCKET
||
2312 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2315 fd
= *((int *)CMSG_DATA(cmsg
));
2319 #ifndef MSG_CMSG_CLOEXEC
2320 qemu_set_cloexec(fd
);
2328 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2330 TCPCharDriver
*s
= chr
->opaque
;
2331 struct msghdr msg
= { NULL
, };
2332 struct iovec iov
[1];
2334 struct cmsghdr cmsg
;
2335 char control
[CMSG_SPACE(sizeof(int))];
2340 iov
[0].iov_base
= buf
;
2341 iov
[0].iov_len
= len
;
2345 msg
.msg_control
= &msg_control
;
2346 msg
.msg_controllen
= sizeof(msg_control
);
2348 #ifdef MSG_CMSG_CLOEXEC
2349 flags
|= MSG_CMSG_CLOEXEC
;
2351 ret
= recvmsg(s
->fd
, &msg
, flags
);
2352 if (ret
> 0 && s
->is_unix
) {
2353 unix_process_msgfd(chr
, &msg
);
2359 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2361 TCPCharDriver
*s
= chr
->opaque
;
2362 return qemu_recv(s
->fd
, buf
, len
, 0);
2366 static void tcp_chr_read(void *opaque
)
2368 CharDriverState
*chr
= opaque
;
2369 TCPCharDriver
*s
= chr
->opaque
;
2370 uint8_t buf
[READ_BUF_LEN
];
2373 if (!s
->connected
|| s
->max_size
<= 0)
2376 if (len
> s
->max_size
)
2378 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2380 /* connection closed */
2382 if (s
->listen_fd
>= 0) {
2383 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2385 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2388 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2389 } else if (size
> 0) {
2390 if (s
->do_telnetopt
)
2391 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2393 qemu_chr_be_write(chr
, buf
, size
);
2398 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2400 return qemu_chr_open_fd(eventfd
, eventfd
);
2404 static void tcp_chr_connect(void *opaque
)
2406 CharDriverState
*chr
= opaque
;
2407 TCPCharDriver
*s
= chr
->opaque
;
2411 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2412 tcp_chr_read
, NULL
, chr
);
2414 qemu_chr_generic_open(chr
);
2417 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2418 static void tcp_chr_telnet_init(int fd
)
2421 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2422 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2423 send(fd
, (char *)buf
, 3, 0);
2424 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2425 send(fd
, (char *)buf
, 3, 0);
2426 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2427 send(fd
, (char *)buf
, 3, 0);
2428 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2429 send(fd
, (char *)buf
, 3, 0);
2432 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2434 TCPCharDriver
*s
= chr
->opaque
;
2438 socket_set_nonblock(fd
);
2440 socket_set_nodelay(fd
);
2442 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2443 tcp_chr_connect(chr
);
2448 static void tcp_chr_accept(void *opaque
)
2450 CharDriverState
*chr
= opaque
;
2451 TCPCharDriver
*s
= chr
->opaque
;
2452 struct sockaddr_in saddr
;
2454 struct sockaddr_un uaddr
;
2456 struct sockaddr
*addr
;
2463 len
= sizeof(uaddr
);
2464 addr
= (struct sockaddr
*)&uaddr
;
2468 len
= sizeof(saddr
);
2469 addr
= (struct sockaddr
*)&saddr
;
2471 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2472 if (fd
< 0 && errno
!= EINTR
) {
2474 } else if (fd
>= 0) {
2475 if (s
->do_telnetopt
)
2476 tcp_chr_telnet_init(fd
);
2480 if (tcp_chr_add_client(chr
, fd
) < 0)
2484 static void tcp_chr_close(CharDriverState
*chr
)
2486 TCPCharDriver
*s
= chr
->opaque
;
2488 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
2491 if (s
->listen_fd
>= 0) {
2492 qemu_set_fd_handler2(s
->listen_fd
, NULL
, NULL
, NULL
, NULL
);
2493 closesocket(s
->listen_fd
);
2496 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2499 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2500 bool is_listen
, bool is_telnet
,
2501 bool is_waitconnect
,
2504 CharDriverState
*chr
= NULL
;
2505 TCPCharDriver
*s
= NULL
;
2506 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2507 const char *left
= "", *right
= "";
2508 struct sockaddr_storage ss
;
2509 socklen_t ss_len
= sizeof(ss
);
2511 memset(&ss
, 0, ss_len
);
2512 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2513 error_setg(errp
, "getsockname: %s", strerror(errno
));
2517 chr
= g_malloc0(sizeof(CharDriverState
));
2518 s
= g_malloc0(sizeof(TCPCharDriver
));
2525 chr
->filename
= g_malloc(256);
2526 switch (ss
.ss_family
) {
2530 snprintf(chr
->filename
, 256, "unix:%s%s",
2531 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2532 is_listen
? ",server" : "");
2540 s
->do_nodelay
= do_nodelay
;
2541 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2542 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2543 snprintf(chr
->filename
, 256, "%s:%s:%s%s%s%s",
2544 is_telnet
? "telnet" : "tcp",
2545 left
, host
, right
, serv
,
2546 is_listen
? ",server" : "");
2551 chr
->chr_write
= tcp_chr_write
;
2552 chr
->chr_close
= tcp_chr_close
;
2553 chr
->get_msgfd
= tcp_get_msgfd
;
2554 chr
->chr_add_client
= tcp_chr_add_client
;
2558 qemu_set_fd_handler2(s
->listen_fd
, NULL
, tcp_chr_accept
, NULL
, chr
);
2560 s
->do_telnetopt
= 1;
2565 socket_set_nodelay(fd
);
2566 tcp_chr_connect(chr
);
2569 if (is_listen
&& is_waitconnect
) {
2570 printf("QEMU waiting for connection on: %s\n",
2572 tcp_chr_accept(chr
);
2573 socket_set_nonblock(s
->listen_fd
);
2578 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2580 CharDriverState
*chr
= NULL
;
2581 Error
*local_err
= NULL
;
2589 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2590 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2591 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2592 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2593 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2599 fd
= unix_listen_opts(opts
, &local_err
);
2601 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2605 fd
= inet_listen_opts(opts
, 0, &local_err
);
2607 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2614 if (!is_waitconnect
)
2615 socket_set_nonblock(fd
);
2617 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2618 is_waitconnect
, &local_err
);
2619 if (error_is_set(&local_err
)) {
2627 qerror_report_err(local_err
);
2628 error_free(local_err
);
2634 g_free(chr
->opaque
);
2640 /***********************************************************/
2641 /* Memory chardev */
2644 size_t outbuf_capacity
;
2648 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2650 MemoryDriver
*d
= chr
->opaque
;
2652 /* TODO: the QString implementation has the same code, we should
2653 * introduce a generic way to do this in cutils.c */
2654 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2656 d
->outbuf_capacity
+= len
;
2657 d
->outbuf_capacity
*= 2;
2658 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2661 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2662 d
->outbuf_size
+= len
;
2667 void qemu_chr_init_mem(CharDriverState
*chr
)
2671 d
= g_malloc(sizeof(*d
));
2673 d
->outbuf_capacity
= 4096;
2674 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2676 memset(chr
, 0, sizeof(*chr
));
2678 chr
->chr_write
= mem_chr_write
;
2681 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2683 MemoryDriver
*d
= chr
->opaque
;
2684 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2687 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2688 void qemu_chr_close_mem(CharDriverState
*chr
)
2690 MemoryDriver
*d
= chr
->opaque
;
2693 g_free(chr
->opaque
);
2695 chr
->chr_write
= NULL
;
2698 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2700 const MemoryDriver
*d
= chr
->opaque
;
2701 return d
->outbuf_size
;
2704 /*********************************************************/
2705 /* Ring buffer chardev */
2712 } RingBufCharDriver
;
2714 static size_t ringbuf_count(const CharDriverState
*chr
)
2716 const RingBufCharDriver
*d
= chr
->opaque
;
2718 return d
->prod
- d
->cons
;
2721 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2723 RingBufCharDriver
*d
= chr
->opaque
;
2726 if (!buf
|| (len
< 0)) {
2730 for (i
= 0; i
< len
; i
++ ) {
2731 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2732 if (d
->prod
- d
->cons
> d
->size
) {
2733 d
->cons
= d
->prod
- d
->size
;
2740 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2742 RingBufCharDriver
*d
= chr
->opaque
;
2745 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2746 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2752 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2754 RingBufCharDriver
*d
= chr
->opaque
;
2761 static CharDriverState
*qemu_chr_open_ringbuf(QemuOpts
*opts
)
2763 CharDriverState
*chr
;
2764 RingBufCharDriver
*d
;
2766 chr
= g_malloc0(sizeof(CharDriverState
));
2767 d
= g_malloc(sizeof(*d
));
2769 d
->size
= qemu_opt_get_size(opts
, "size", 0);
2774 /* The size must be power of 2 */
2775 if (d
->size
& (d
->size
- 1)) {
2776 error_report("size of ringbuf device must be power of two");
2782 d
->cbuf
= g_malloc0(d
->size
);
2785 chr
->chr_write
= ringbuf_chr_write
;
2786 chr
->chr_close
= ringbuf_chr_close
;
2796 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2798 return chr
->chr_write
== ringbuf_chr_write
;
2801 void qmp_ringbuf_write(const char *device
, const char *data
,
2802 bool has_format
, enum DataFormat format
,
2805 CharDriverState
*chr
;
2806 const uint8_t *write_data
;
2810 chr
= qemu_chr_find(device
);
2812 error_setg(errp
, "Device '%s' not found", device
);
2816 if (!chr_is_ringbuf(chr
)) {
2817 error_setg(errp
,"%s is not a ringbuf device", device
);
2821 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2822 write_data
= g_base64_decode(data
, &write_count
);
2824 write_data
= (uint8_t *)data
;
2825 write_count
= strlen(data
);
2828 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2830 if (write_data
!= (uint8_t *)data
) {
2831 g_free((void *)write_data
);
2835 error_setg(errp
, "Failed to write to device %s", device
);
2840 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2841 bool has_format
, enum DataFormat format
,
2844 CharDriverState
*chr
;
2849 chr
= qemu_chr_find(device
);
2851 error_setg(errp
, "Device '%s' not found", device
);
2855 if (!chr_is_ringbuf(chr
)) {
2856 error_setg(errp
,"%s is not a ringbuf device", device
);
2861 error_setg(errp
, "size must be greater than zero");
2865 count
= ringbuf_count(chr
);
2866 size
= size
> count
? count
: size
;
2867 read_data
= g_malloc(size
+ 1);
2869 ringbuf_chr_read(chr
, read_data
, size
);
2871 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2872 data
= g_base64_encode(read_data
, size
);
2876 * FIXME should read only complete, valid UTF-8 characters up
2877 * to @size bytes. Invalid sequences should be replaced by a
2878 * suitable replacement character. Except when (and only
2879 * when) ring buffer lost characters since last read, initial
2880 * continuation characters should be dropped.
2882 read_data
[size
] = 0;
2883 data
= (char *)read_data
;
2889 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2891 char host
[65], port
[33], width
[8], height
[8];
2895 Error
*local_err
= NULL
;
2897 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2898 if (error_is_set(&local_err
)) {
2899 qerror_report_err(local_err
);
2900 error_free(local_err
);
2904 if (strstart(filename
, "mon:", &p
)) {
2906 qemu_opt_set(opts
, "mux", "on");
2909 if (strcmp(filename
, "null") == 0 ||
2910 strcmp(filename
, "pty") == 0 ||
2911 strcmp(filename
, "msmouse") == 0 ||
2912 strcmp(filename
, "braille") == 0 ||
2913 strcmp(filename
, "stdio") == 0) {
2914 qemu_opt_set(opts
, "backend", filename
);
2917 if (strstart(filename
, "vc", &p
)) {
2918 qemu_opt_set(opts
, "backend", "vc");
2920 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2922 qemu_opt_set(opts
, "width", width
);
2923 qemu_opt_set(opts
, "height", height
);
2924 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2926 qemu_opt_set(opts
, "cols", width
);
2927 qemu_opt_set(opts
, "rows", height
);
2934 if (strcmp(filename
, "con:") == 0) {
2935 qemu_opt_set(opts
, "backend", "console");
2938 if (strstart(filename
, "COM", NULL
)) {
2939 qemu_opt_set(opts
, "backend", "serial");
2940 qemu_opt_set(opts
, "path", filename
);
2943 if (strstart(filename
, "file:", &p
)) {
2944 qemu_opt_set(opts
, "backend", "file");
2945 qemu_opt_set(opts
, "path", p
);
2948 if (strstart(filename
, "pipe:", &p
)) {
2949 qemu_opt_set(opts
, "backend", "pipe");
2950 qemu_opt_set(opts
, "path", p
);
2953 if (strstart(filename
, "tcp:", &p
) ||
2954 strstart(filename
, "telnet:", &p
)) {
2955 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2957 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2960 qemu_opt_set(opts
, "backend", "socket");
2961 qemu_opt_set(opts
, "host", host
);
2962 qemu_opt_set(opts
, "port", port
);
2963 if (p
[pos
] == ',') {
2964 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2967 if (strstart(filename
, "telnet:", &p
))
2968 qemu_opt_set(opts
, "telnet", "on");
2971 if (strstart(filename
, "udp:", &p
)) {
2972 qemu_opt_set(opts
, "backend", "udp");
2973 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2975 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2979 qemu_opt_set(opts
, "host", host
);
2980 qemu_opt_set(opts
, "port", port
);
2981 if (p
[pos
] == '@') {
2983 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2985 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2989 qemu_opt_set(opts
, "localaddr", host
);
2990 qemu_opt_set(opts
, "localport", port
);
2994 if (strstart(filename
, "unix:", &p
)) {
2995 qemu_opt_set(opts
, "backend", "socket");
2996 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3000 if (strstart(filename
, "/dev/parport", NULL
) ||
3001 strstart(filename
, "/dev/ppi", NULL
)) {
3002 qemu_opt_set(opts
, "backend", "parport");
3003 qemu_opt_set(opts
, "path", filename
);
3006 if (strstart(filename
, "/dev/", NULL
)) {
3007 qemu_opt_set(opts
, "backend", "tty");
3008 qemu_opt_set(opts
, "path", filename
);
3013 qemu_opts_del(opts
);
3017 #ifdef HAVE_CHARDEV_PARPORT
3019 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
3021 const char *filename
= qemu_opt_get(opts
, "path");
3024 fd
= qemu_open(filename
, O_RDWR
);
3028 return qemu_chr_open_pp_fd(fd
);
3033 static const struct {
3035 CharDriverState
*(*open
)(QemuOpts
*opts
);
3036 } backend_table
[] = {
3037 { .name
= "null", .open
= qemu_chr_open_null
},
3038 { .name
= "socket", .open
= qemu_chr_open_socket
},
3039 { .name
= "udp", .open
= qemu_chr_open_udp
},
3040 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
3041 { .name
= "vc", .open
= vc_init
},
3042 { .name
= "memory", .open
= qemu_chr_open_ringbuf
},
3044 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
3045 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
3046 { .name
= "console", .open
= qemu_chr_open_win_con
},
3047 { .name
= "serial", .open
= qemu_chr_open_win
},
3048 { .name
= "stdio", .open
= qemu_chr_open_win_stdio
},
3050 { .name
= "file", .open
= qemu_chr_open_file_out
},
3051 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
3052 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
3054 #ifdef CONFIG_BRLAPI
3055 { .name
= "braille", .open
= chr_baum_init
},
3057 #ifdef HAVE_CHARDEV_TTY
3058 { .name
= "tty", .open
= qemu_chr_open_tty
},
3059 { .name
= "serial", .open
= qemu_chr_open_tty
},
3060 { .name
= "pty", .open
= qemu_chr_open_pty
},
3062 #ifdef HAVE_CHARDEV_PARPORT
3063 { .name
= "parallel", .open
= qemu_chr_open_pp
},
3064 { .name
= "parport", .open
= qemu_chr_open_pp
},
3067 { .name
= "spicevmc", .open
= qemu_chr_open_spice
},
3068 #if SPICE_SERVER_VERSION >= 0x000c02
3069 { .name
= "spiceport", .open
= qemu_chr_open_spice_port
},
3074 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3075 void (*init
)(struct CharDriverState
*s
),
3078 CharDriverState
*chr
;
3081 if (qemu_opts_id(opts
) == NULL
) {
3082 error_setg(errp
, "chardev: no id specified");
3086 if (qemu_opt_get(opts
, "backend") == NULL
) {
3087 error_setg(errp
, "chardev: \"%s\" missing backend",
3088 qemu_opts_id(opts
));
3091 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
3092 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
3095 if (i
== ARRAY_SIZE(backend_table
)) {
3096 error_setg(errp
, "chardev: backend \"%s\" not found",
3097 qemu_opt_get(opts
, "backend"));
3101 chr
= backend_table
[i
].open(opts
);
3103 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3104 qemu_opt_get(opts
, "backend"));
3109 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3111 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3113 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3114 CharDriverState
*base
= chr
;
3115 int len
= strlen(qemu_opts_id(opts
)) + 6;
3116 base
->label
= g_malloc(len
);
3117 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3118 chr
= qemu_chr_open_mux(base
);
3119 chr
->filename
= base
->filename
;
3120 chr
->avail_connections
= MAX_MUX
;
3121 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3123 chr
->avail_connections
= 1;
3125 chr
->label
= g_strdup(qemu_opts_id(opts
));
3130 qemu_opts_del(opts
);
3134 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3137 CharDriverState
*chr
;
3141 if (strstart(filename
, "chardev:", &p
)) {
3142 return qemu_chr_find(p
);
3145 opts
= qemu_chr_parse_compat(label
, filename
);
3149 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3150 if (error_is_set(&err
)) {
3151 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3154 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3155 monitor_init(chr
, MONITOR_USE_READLINE
);
3160 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3162 if (chr
->chr_set_echo
) {
3163 chr
->chr_set_echo(chr
, echo
);
3167 void qemu_chr_fe_open(struct CharDriverState
*chr
)
3169 if (chr
->chr_guest_open
) {
3170 chr
->chr_guest_open(chr
);
3174 void qemu_chr_fe_close(struct CharDriverState
*chr
)
3176 if (chr
->chr_guest_close
) {
3177 chr
->chr_guest_close(chr
);
3181 void qemu_chr_delete(CharDriverState
*chr
)
3183 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3184 if (chr
->chr_close
) {
3185 chr
->chr_close(chr
);
3187 g_free(chr
->filename
);
3190 qemu_opts_del(chr
->opts
);
3195 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3197 ChardevInfoList
*chr_list
= NULL
;
3198 CharDriverState
*chr
;
3200 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3201 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3202 info
->value
= g_malloc0(sizeof(*info
->value
));
3203 info
->value
->label
= g_strdup(chr
->label
);
3204 info
->value
->filename
= g_strdup(chr
->filename
);
3206 info
->next
= chr_list
;
3213 CharDriverState
*qemu_chr_find(const char *name
)
3215 CharDriverState
*chr
;
3217 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3218 if (strcmp(chr
->label
, name
) != 0)
3225 /* Get a character (serial) device interface. */
3226 CharDriverState
*qemu_char_get_next_serial(void)
3228 static int next_serial
;
3230 /* FIXME: This function needs to go away: use chardev properties! */
3231 return serial_hds
[next_serial
++];
3234 QemuOptsList qemu_chardev_opts
= {
3236 .implied_opt_name
= "backend",
3237 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3241 .type
= QEMU_OPT_STRING
,
3244 .type
= QEMU_OPT_STRING
,
3247 .type
= QEMU_OPT_STRING
,
3250 .type
= QEMU_OPT_STRING
,
3252 .name
= "localaddr",
3253 .type
= QEMU_OPT_STRING
,
3255 .name
= "localport",
3256 .type
= QEMU_OPT_STRING
,
3259 .type
= QEMU_OPT_NUMBER
,
3262 .type
= QEMU_OPT_BOOL
,
3265 .type
= QEMU_OPT_BOOL
,
3268 .type
= QEMU_OPT_BOOL
,
3271 .type
= QEMU_OPT_BOOL
,
3274 .type
= QEMU_OPT_BOOL
,
3277 .type
= QEMU_OPT_BOOL
,
3280 .type
= QEMU_OPT_NUMBER
,
3283 .type
= QEMU_OPT_NUMBER
,
3286 .type
= QEMU_OPT_NUMBER
,
3289 .type
= QEMU_OPT_NUMBER
,
3292 .type
= QEMU_OPT_BOOL
,
3295 .type
= QEMU_OPT_BOOL
,
3298 .type
= QEMU_OPT_STRING
,
3301 .type
= QEMU_OPT_NUMBER
,
3304 .type
= QEMU_OPT_SIZE
,
3306 { /* end of list */ }
3312 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3317 error_setg(errp
, "input file not supported");
3321 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3322 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3323 if (out
== INVALID_HANDLE_VALUE
) {
3324 error_setg(errp
, "open %s failed", file
->out
);
3327 return qemu_chr_open_win_file(out
);
3330 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3333 return qemu_chr_open_win_path(serial
->device
);
3336 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3339 error_setg(errp
, "character device backend type 'parallel' not supported");
3345 static int qmp_chardev_open_file_source(char *src
, int flags
,
3350 TFR(fd
= qemu_open(src
, flags
, 0666));
3352 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3357 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3359 int flags
, in
= -1, out
= -1;
3361 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3362 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3363 if (error_is_set(errp
)) {
3369 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3370 if (error_is_set(errp
)) {
3376 return qemu_chr_open_fd(in
, out
);
3379 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3382 #ifdef HAVE_CHARDEV_TTY
3385 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3386 if (error_is_set(errp
)) {
3389 socket_set_nonblock(fd
);
3390 return qemu_chr_open_tty_fd(fd
);
3392 error_setg(errp
, "character device backend type 'serial' not supported");
3397 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3400 #ifdef HAVE_CHARDEV_PARPORT
3403 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3404 if (error_is_set(errp
)) {
3407 return qemu_chr_open_pp_fd(fd
);
3409 error_setg(errp
, "character device backend type 'parallel' not supported");
3416 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3419 SocketAddress
*addr
= sock
->addr
;
3420 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3421 bool is_listen
= sock
->has_server
? sock
->server
: true;
3422 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3423 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3427 fd
= socket_listen(addr
, errp
);
3429 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3431 if (error_is_set(errp
)) {
3434 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3435 is_telnet
, is_waitconnect
, errp
);
3438 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3441 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3442 CharDriverState
*chr
= NULL
;
3444 chr
= qemu_chr_find(id
);
3446 error_setg(errp
, "Chardev '%s' already exists", id
);
3451 switch (backend
->kind
) {
3452 case CHARDEV_BACKEND_KIND_FILE
:
3453 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3455 case CHARDEV_BACKEND_KIND_SERIAL
:
3456 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3458 case CHARDEV_BACKEND_KIND_PARALLEL
:
3459 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3461 case CHARDEV_BACKEND_KIND_SOCKET
:
3462 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3464 #ifdef HAVE_CHARDEV_TTY
3465 case CHARDEV_BACKEND_KIND_PTY
:
3467 /* qemu_chr_open_pty sets "path" in opts */
3469 opts
= qemu_opts_create_nofail(qemu_find_opts("chardev"));
3470 chr
= qemu_chr_open_pty(opts
);
3471 ret
->pty
= g_strdup(qemu_opt_get(opts
, "path"));
3472 ret
->has_pty
= true;
3473 qemu_opts_del(opts
);
3477 case CHARDEV_BACKEND_KIND_NULL
:
3478 chr
= qemu_chr_open_null(NULL
);
3481 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3485 if (chr
== NULL
&& !error_is_set(errp
)) {
3486 error_setg(errp
, "Failed to create chardev");
3489 chr
->label
= g_strdup(id
);
3490 chr
->avail_connections
= 1;
3491 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3499 void qmp_chardev_remove(const char *id
, Error
**errp
)
3501 CharDriverState
*chr
;
3503 chr
= qemu_chr_find(id
);
3505 error_setg(errp
, "Chardev '%s' not found", id
);
3508 if (chr
->chr_can_read
|| chr
->chr_read
||
3509 chr
->chr_event
|| chr
->handler_opaque
) {
3510 error_setg(errp
, "Chardev '%s' is busy", id
);
3513 qemu_chr_delete(chr
);