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 "sysemu/char.h"
31 #include "qmp-commands.h"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #include <arpa/inet.h>
53 #include <sys/select.h>
56 #if defined(__GLIBC__)
58 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
63 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64 #include <dev/ppbus/ppi.h>
65 #include <dev/ppbus/ppbconf.h>
66 #elif defined(__DragonFly__)
67 #include <dev/misc/ppi/ppi.h>
68 #include <bus/ppbus/ppbconf.h>
74 #include <linux/ppdev.h>
75 #include <linux/parport.h>
79 #include <sys/ethernet.h>
80 #include <sys/sockio.h>
81 #include <netinet/arp.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_icmp.h> // must come after ip.h
86 #include <netinet/udp.h>
87 #include <netinet/tcp.h>
95 #include "qemu/sockets.h"
96 #include "ui/qemu-spice.h"
98 #define READ_BUF_LEN 4096
100 /***********************************************************/
101 /* character device */
103 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
104 QTAILQ_HEAD_INITIALIZER(chardevs
);
106 void qemu_chr_be_event(CharDriverState
*s
, int event
)
108 /* Keep track if the char device is open */
110 case CHR_EVENT_OPENED
:
113 case CHR_EVENT_CLOSED
:
120 s
->chr_event(s
->handler_opaque
, event
);
123 static gboolean
qemu_chr_be_generic_open_bh(gpointer opaque
)
125 CharDriverState
*s
= opaque
;
126 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
131 void qemu_chr_be_generic_open(CharDriverState
*s
)
133 if (s
->idle_tag
== 0) {
134 s
->idle_tag
= g_idle_add(qemu_chr_be_generic_open_bh
, s
);
138 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
140 return s
->chr_write(s
, buf
, len
);
143 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
148 while (offset
< len
) {
150 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
151 if (res
== -1 && errno
== EAGAIN
) {
154 } while (res
== -1 && errno
== EAGAIN
);
170 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
174 return s
->chr_ioctl(s
, cmd
, arg
);
177 int qemu_chr_be_can_write(CharDriverState
*s
)
179 if (!s
->chr_can_read
)
181 return s
->chr_can_read(s
->handler_opaque
);
184 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
187 s
->chr_read(s
->handler_opaque
, buf
, len
);
191 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
193 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
196 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
198 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
201 void qemu_chr_accept_input(CharDriverState
*s
)
203 if (s
->chr_accept_input
)
204 s
->chr_accept_input(s
);
208 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
210 char buf
[READ_BUF_LEN
];
213 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
214 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
218 void qemu_chr_add_handlers(CharDriverState
*s
,
219 IOCanReadHandler
*fd_can_read
,
220 IOReadHandler
*fd_read
,
221 IOEventHandler
*fd_event
,
226 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
231 s
->chr_can_read
= fd_can_read
;
232 s
->chr_read
= fd_read
;
233 s
->chr_event
= fd_event
;
234 s
->handler_opaque
= opaque
;
235 if (s
->chr_update_read_handler
)
236 s
->chr_update_read_handler(s
);
238 if (!s
->explicit_fe_open
) {
239 qemu_chr_fe_set_open(s
, fe_open
);
242 /* We're connecting to an already opened device, so let's make sure we
243 also get the open event */
244 if (fe_open
&& s
->be_open
) {
245 qemu_chr_be_generic_open(s
);
249 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
254 static CharDriverState
*qemu_chr_open_null(void)
256 CharDriverState
*chr
;
258 chr
= g_malloc0(sizeof(CharDriverState
));
259 chr
->chr_write
= null_chr_write
;
263 /* MUX driver for serial I/O splitting */
265 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
266 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
268 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
269 IOReadHandler
*chr_read
[MAX_MUX
];
270 IOEventHandler
*chr_event
[MAX_MUX
];
271 void *ext_opaque
[MAX_MUX
];
272 CharDriverState
*drv
;
277 /* Intermediate input buffer allows to catch escape sequences even if the
278 currently active device is not accepting any input - but only until it
280 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
285 int64_t timestamps_start
;
289 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
291 MuxDriver
*d
= chr
->opaque
;
293 if (!d
->timestamps
) {
294 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
299 for (i
= 0; i
< len
; i
++) {
305 ti
= qemu_get_clock_ms(rt_clock
);
306 if (d
->timestamps_start
== -1)
307 d
->timestamps_start
= ti
;
308 ti
-= d
->timestamps_start
;
310 snprintf(buf1
, sizeof(buf1
),
311 "[%02d:%02d:%02d.%03d] ",
316 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
319 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
320 if (buf
[i
] == '\n') {
328 static const char * const mux_help
[] = {
329 "% h print this help\n\r",
330 "% x exit emulator\n\r",
331 "% s save disk data back to file (if -snapshot)\n\r",
332 "% t toggle console timestamps\n\r"
333 "% b send break (magic sysrq)\n\r",
334 "% c switch between console and monitor\n\r",
339 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
340 static void mux_print_help(CharDriverState
*chr
)
343 char ebuf
[15] = "Escape-Char";
344 char cbuf
[50] = "\n\r";
346 if (term_escape_char
> 0 && term_escape_char
< 26) {
347 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
348 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
350 snprintf(cbuf
, sizeof(cbuf
),
351 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
354 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
355 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
356 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
357 if (mux_help
[i
][j
] == '%')
358 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
360 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
365 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
367 if (d
->chr_event
[mux_nr
])
368 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
371 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
373 if (d
->term_got_escape
) {
374 d
->term_got_escape
= 0;
375 if (ch
== term_escape_char
)
384 const char *term
= "QEMU: Terminated\n\r";
385 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
393 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
396 /* Switch to the next registered device */
397 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
399 if (d
->focus
>= d
->mux_cnt
)
401 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
404 d
->timestamps
= !d
->timestamps
;
405 d
->timestamps_start
= -1;
409 } else if (ch
== term_escape_char
) {
410 d
->term_got_escape
= 1;
418 static void mux_chr_accept_input(CharDriverState
*chr
)
420 MuxDriver
*d
= chr
->opaque
;
423 while (d
->prod
[m
] != d
->cons
[m
] &&
424 d
->chr_can_read
[m
] &&
425 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
426 d
->chr_read
[m
](d
->ext_opaque
[m
],
427 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
431 static int mux_chr_can_read(void *opaque
)
433 CharDriverState
*chr
= opaque
;
434 MuxDriver
*d
= chr
->opaque
;
437 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
439 if (d
->chr_can_read
[m
])
440 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
444 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
446 CharDriverState
*chr
= opaque
;
447 MuxDriver
*d
= chr
->opaque
;
451 mux_chr_accept_input (opaque
);
453 for(i
= 0; i
< size
; i
++)
454 if (mux_proc_byte(chr
, d
, buf
[i
])) {
455 if (d
->prod
[m
] == d
->cons
[m
] &&
456 d
->chr_can_read
[m
] &&
457 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
458 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
460 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
464 static void mux_chr_event(void *opaque
, int event
)
466 CharDriverState
*chr
= opaque
;
467 MuxDriver
*d
= chr
->opaque
;
470 /* Send the event to all registered listeners */
471 for (i
= 0; i
< d
->mux_cnt
; i
++)
472 mux_chr_send_event(d
, i
, event
);
475 static void mux_chr_update_read_handler(CharDriverState
*chr
)
477 MuxDriver
*d
= chr
->opaque
;
479 if (d
->mux_cnt
>= MAX_MUX
) {
480 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
483 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
484 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
485 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
486 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
487 /* Fix up the real driver with mux routines */
488 if (d
->mux_cnt
== 0) {
489 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
492 if (d
->focus
!= -1) {
493 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
495 d
->focus
= d
->mux_cnt
;
497 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
500 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
502 CharDriverState
*chr
;
505 chr
= g_malloc0(sizeof(CharDriverState
));
506 d
= g_malloc0(sizeof(MuxDriver
));
511 chr
->chr_write
= mux_chr_write
;
512 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
513 chr
->chr_accept_input
= mux_chr_accept_input
;
514 /* Frontend guest-open / -close notification is not support with muxes */
515 chr
->chr_set_fe_open
= NULL
;
517 /* Muxes are always open on creation */
518 qemu_chr_be_generic_open(chr
);
525 int send_all(int fd
, const void *buf
, int len1
)
531 ret
= send(fd
, buf
, len
, 0);
533 errno
= WSAGetLastError();
534 if (errno
!= WSAEWOULDBLOCK
) {
537 } else if (ret
== 0) {
549 int send_all(int fd
, const void *_buf
, int len1
)
552 const uint8_t *buf
= _buf
;
556 ret
= write(fd
, buf
, len
);
558 if (errno
!= EINTR
&& errno
!= EAGAIN
)
560 } else if (ret
== 0) {
570 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
576 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
578 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
595 typedef struct IOWatchPoll
602 IOCanReadHandler
*fd_can_read
;
607 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
609 return container_of(source
, IOWatchPoll
, parent
);
612 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
614 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
615 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
616 bool was_active
= iwp
->src
!= NULL
;
617 if (was_active
== now_active
) {
622 iwp
->src
= g_io_create_watch(iwp
->channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
623 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
624 g_source_attach(iwp
->src
, NULL
);
626 g_source_destroy(iwp
->src
);
627 g_source_unref(iwp
->src
);
633 static gboolean
io_watch_poll_check(GSource
*source
)
638 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
644 static void io_watch_poll_finalize(GSource
*source
)
646 /* Due to a glib bug, removing the last reference to a source
647 * inside a finalize callback causes recursive locking (and a
648 * deadlock). This is not a problem inside other callbacks,
649 * including dispatch callbacks, so we call io_remove_watch_poll
650 * to remove this source. At this point, iwp->src must
651 * be NULL, or we would leak it.
653 * This would be solved much more elegantly by child sources,
654 * but we support older glib versions that do not have them.
656 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
657 assert(iwp
->src
== NULL
);
660 static GSourceFuncs io_watch_poll_funcs
= {
661 .prepare
= io_watch_poll_prepare
,
662 .check
= io_watch_poll_check
,
663 .dispatch
= io_watch_poll_dispatch
,
664 .finalize
= io_watch_poll_finalize
,
667 /* Can only be used for read */
668 static guint
io_add_watch_poll(GIOChannel
*channel
,
669 IOCanReadHandler
*fd_can_read
,
676 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
677 iwp
->fd_can_read
= fd_can_read
;
678 iwp
->opaque
= user_data
;
679 iwp
->channel
= channel
;
680 iwp
->fd_read
= (GSourceFunc
) fd_read
;
683 tag
= g_source_attach(&iwp
->parent
, NULL
);
684 g_source_unref(&iwp
->parent
);
688 static void io_remove_watch_poll(guint tag
)
693 g_return_if_fail (tag
> 0);
695 source
= g_main_context_find_source_by_id(NULL
, tag
);
696 g_return_if_fail (source
!= NULL
);
698 iwp
= io_watch_poll_from_source(source
);
700 g_source_destroy(iwp
->src
);
701 g_source_unref(iwp
->src
);
704 g_source_destroy(&iwp
->parent
);
708 static GIOChannel
*io_channel_from_fd(int fd
)
716 chan
= g_io_channel_unix_new(fd
);
718 g_io_channel_set_encoding(chan
, NULL
, NULL
);
719 g_io_channel_set_buffered(chan
, FALSE
);
725 static GIOChannel
*io_channel_from_socket(int fd
)
734 chan
= g_io_channel_win32_new_socket(fd
);
736 chan
= g_io_channel_unix_new(fd
);
739 g_io_channel_set_encoding(chan
, NULL
, NULL
);
740 g_io_channel_set_buffered(chan
, FALSE
);
745 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
751 while (offset
< len
) {
754 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
755 &bytes_written
, NULL
);
756 if (status
!= G_IO_STATUS_NORMAL
) {
757 if (status
== G_IO_STATUS_AGAIN
) {
758 /* If we've written any data, return a partial write. */
768 } else if (status
== G_IO_STATUS_EOF
) {
772 offset
+= bytes_written
;
780 typedef struct FDCharDriver
{
781 CharDriverState
*chr
;
782 GIOChannel
*fd_in
, *fd_out
;
785 QTAILQ_ENTRY(FDCharDriver
) node
;
788 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
790 FDCharDriver
*s
= chr
->opaque
;
792 return io_channel_send(s
->fd_out
, buf
, len
);
795 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
797 CharDriverState
*chr
= opaque
;
798 FDCharDriver
*s
= chr
->opaque
;
800 uint8_t buf
[READ_BUF_LEN
];
805 if (len
> s
->max_size
) {
812 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
813 len
, &bytes_read
, NULL
);
814 if (status
== G_IO_STATUS_EOF
) {
816 io_remove_watch_poll(s
->fd_in_tag
);
819 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
822 if (status
== G_IO_STATUS_NORMAL
) {
823 qemu_chr_be_write(chr
, buf
, bytes_read
);
829 static int fd_chr_read_poll(void *opaque
)
831 CharDriverState
*chr
= opaque
;
832 FDCharDriver
*s
= chr
->opaque
;
834 s
->max_size
= qemu_chr_be_can_write(chr
);
838 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
840 FDCharDriver
*s
= chr
->opaque
;
841 return g_io_create_watch(s
->fd_out
, cond
);
844 static void fd_chr_update_read_handler(CharDriverState
*chr
)
846 FDCharDriver
*s
= chr
->opaque
;
849 io_remove_watch_poll(s
->fd_in_tag
);
854 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
858 static void fd_chr_close(struct CharDriverState
*chr
)
860 FDCharDriver
*s
= chr
->opaque
;
863 io_remove_watch_poll(s
->fd_in_tag
);
868 g_io_channel_unref(s
->fd_in
);
871 g_io_channel_unref(s
->fd_out
);
875 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
878 /* open a character device to a unix fd */
879 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
881 CharDriverState
*chr
;
884 chr
= g_malloc0(sizeof(CharDriverState
));
885 s
= g_malloc0(sizeof(FDCharDriver
));
886 s
->fd_in
= io_channel_from_fd(fd_in
);
887 s
->fd_out
= io_channel_from_fd(fd_out
);
888 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
891 chr
->chr_add_watch
= fd_chr_add_watch
;
892 chr
->chr_write
= fd_chr_write
;
893 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
894 chr
->chr_close
= fd_chr_close
;
896 qemu_chr_be_generic_open(chr
);
901 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
904 char filename_in
[256], filename_out
[256];
905 const char *filename
= opts
->device
;
907 if (filename
== NULL
) {
908 fprintf(stderr
, "chardev: pipe: no filename given\n");
912 snprintf(filename_in
, 256, "%s.in", filename
);
913 snprintf(filename_out
, 256, "%s.out", filename
);
914 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
915 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
916 if (fd_in
< 0 || fd_out
< 0) {
921 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
926 return qemu_chr_open_fd(fd_in
, fd_out
);
929 /* init terminal so that we can grab keys */
930 static struct termios oldtty
;
931 static int old_fd0_flags
;
932 static bool stdio_allow_signal
;
934 static void term_exit(void)
936 tcsetattr (0, TCSANOW
, &oldtty
);
937 fcntl(0, F_SETFL
, old_fd0_flags
);
940 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
946 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
947 |INLCR
|IGNCR
|ICRNL
|IXON
);
948 tty
.c_oflag
|= OPOST
;
949 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
950 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
955 /* if graphical mode, we allow Ctrl-C handling */
956 if (!stdio_allow_signal
)
957 tty
.c_lflag
&= ~ISIG
;
959 tcsetattr (0, TCSANOW
, &tty
);
962 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
968 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
970 CharDriverState
*chr
;
972 if (is_daemonized()) {
973 error_report("cannot use stdio with -daemonize");
976 old_fd0_flags
= fcntl(0, F_GETFL
);
977 tcgetattr (0, &oldtty
);
978 fcntl(0, F_SETFL
, O_NONBLOCK
);
981 chr
= qemu_chr_open_fd(0, 1);
982 chr
->chr_close
= qemu_chr_close_stdio
;
983 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
984 stdio_allow_signal
= display_type
!= DT_NOGRAPHIC
;
985 if (opts
->has_signal
) {
986 stdio_allow_signal
= opts
->signal
;
988 qemu_chr_fe_set_echo(chr
, false);
994 /* Once Solaris has openpty(), this is going to be removed. */
995 static int openpty(int *amaster
, int *aslave
, char *name
,
996 struct termios
*termp
, struct winsize
*winp
)
999 int mfd
= -1, sfd
= -1;
1001 *amaster
= *aslave
= -1;
1003 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
1007 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
1010 if ((slave
= ptsname(mfd
)) == NULL
)
1013 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
1016 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
1017 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
1025 ioctl(sfd
, TIOCSWINSZ
, winp
);
1036 static void cfmakeraw (struct termios
*termios_p
)
1038 termios_p
->c_iflag
&=
1039 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
1040 termios_p
->c_oflag
&= ~OPOST
;
1041 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
1042 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
1043 termios_p
->c_cflag
|= CS8
;
1045 termios_p
->c_cc
[VMIN
] = 0;
1046 termios_p
->c_cc
[VTIME
] = 0;
1050 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1051 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1052 || defined(__GLIBC__)
1054 #define HAVE_CHARDEV_TTY 1
1064 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1065 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1067 static gboolean
pty_chr_timer(gpointer opaque
)
1069 struct CharDriverState
*chr
= opaque
;
1070 PtyCharDriver
*s
= chr
->opaque
;
1077 pty_chr_update_read_handler(chr
);
1084 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1086 PtyCharDriver
*s
= chr
->opaque
;
1089 g_source_remove(s
->timer_tag
);
1094 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1096 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1100 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1102 PtyCharDriver
*s
= chr
->opaque
;
1104 if (!s
->connected
) {
1105 /* guest sends data, check for (re-)connect */
1106 pty_chr_update_read_handler(chr
);
1109 return io_channel_send(s
->fd
, buf
, len
);
1112 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1114 PtyCharDriver
*s
= chr
->opaque
;
1115 return g_io_create_watch(s
->fd
, cond
);
1118 static int pty_chr_read_poll(void *opaque
)
1120 CharDriverState
*chr
= opaque
;
1121 PtyCharDriver
*s
= chr
->opaque
;
1123 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1124 return s
->read_bytes
;
1127 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1129 CharDriverState
*chr
= opaque
;
1130 PtyCharDriver
*s
= chr
->opaque
;
1132 uint8_t buf
[READ_BUF_LEN
];
1136 if (len
> s
->read_bytes
)
1137 len
= s
->read_bytes
;
1141 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1142 if (status
!= G_IO_STATUS_NORMAL
) {
1143 pty_chr_state(chr
, 0);
1146 pty_chr_state(chr
, 1);
1147 qemu_chr_be_write(chr
, buf
, size
);
1152 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1154 PtyCharDriver
*s
= chr
->opaque
;
1157 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1158 pfd
.events
= G_IO_OUT
;
1161 if (pfd
.revents
& G_IO_HUP
) {
1162 pty_chr_state(chr
, 0);
1164 pty_chr_state(chr
, 1);
1168 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1170 PtyCharDriver
*s
= chr
->opaque
;
1174 io_remove_watch_poll(s
->fd_tag
);
1178 /* (re-)connect poll interval for idle guests: once per second.
1179 * We check more frequently in case the guests sends data to
1180 * the virtual device linked to our pty. */
1181 pty_chr_rearm_timer(chr
, 1000);
1184 g_source_remove(s
->timer_tag
);
1187 if (!s
->connected
) {
1188 qemu_chr_be_generic_open(chr
);
1190 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1196 static void pty_chr_close(struct CharDriverState
*chr
)
1198 PtyCharDriver
*s
= chr
->opaque
;
1202 io_remove_watch_poll(s
->fd_tag
);
1205 fd
= g_io_channel_unix_get_fd(s
->fd
);
1206 g_io_channel_unref(s
->fd
);
1209 g_source_remove(s
->timer_tag
);
1213 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1216 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1219 CharDriverState
*chr
;
1222 int master_fd
, slave_fd
;
1223 #if defined(__OpenBSD__) || defined(__DragonFly__)
1224 char pty_name
[PATH_MAX
];
1225 #define q_ptsname(x) pty_name
1227 char *pty_name
= NULL
;
1228 #define q_ptsname(x) ptsname(x)
1231 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1235 /* Set raw attributes on the pty. */
1236 tcgetattr(slave_fd
, &tty
);
1238 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1241 chr
= g_malloc0(sizeof(CharDriverState
));
1243 chr
->filename
= g_strdup_printf("pty:%s", q_ptsname(master_fd
));
1244 ret
->pty
= g_strdup(q_ptsname(master_fd
));
1245 ret
->has_pty
= true;
1247 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1248 q_ptsname(master_fd
), id
);
1250 s
= g_malloc0(sizeof(PtyCharDriver
));
1252 chr
->chr_write
= pty_chr_write
;
1253 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1254 chr
->chr_close
= pty_chr_close
;
1255 chr
->chr_add_watch
= pty_chr_add_watch
;
1257 s
->fd
= io_channel_from_fd(master_fd
);
1263 static void tty_serial_init(int fd
, int speed
,
1264 int parity
, int data_bits
, int stop_bits
)
1270 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1271 speed
, parity
, data_bits
, stop_bits
);
1273 tcgetattr (fd
, &tty
);
1275 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1276 speed
= speed
* 10 / 11;
1293 /* Non-Posix values follow. They may be unsupported on some systems. */
1295 check_speed(115200);
1297 check_speed(230400);
1300 check_speed(460800);
1303 check_speed(500000);
1306 check_speed(576000);
1309 check_speed(921600);
1312 check_speed(1000000);
1315 check_speed(1152000);
1318 check_speed(1500000);
1321 check_speed(2000000);
1324 check_speed(2500000);
1327 check_speed(3000000);
1330 check_speed(3500000);
1333 check_speed(4000000);
1338 cfsetispeed(&tty
, spd
);
1339 cfsetospeed(&tty
, spd
);
1341 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1342 |INLCR
|IGNCR
|ICRNL
|IXON
);
1343 tty
.c_oflag
|= OPOST
;
1344 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1345 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1366 tty
.c_cflag
|= PARENB
;
1369 tty
.c_cflag
|= PARENB
| PARODD
;
1373 tty
.c_cflag
|= CSTOPB
;
1375 tcsetattr (fd
, TCSANOW
, &tty
);
1378 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1380 FDCharDriver
*s
= chr
->opaque
;
1383 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1385 QEMUSerialSetParams
*ssp
= arg
;
1386 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1387 ssp
->speed
, ssp
->parity
,
1388 ssp
->data_bits
, ssp
->stop_bits
);
1391 case CHR_IOCTL_SERIAL_SET_BREAK
:
1393 int enable
= *(int *)arg
;
1395 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1399 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1402 int *targ
= (int *)arg
;
1403 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1405 if (sarg
& TIOCM_CTS
)
1406 *targ
|= CHR_TIOCM_CTS
;
1407 if (sarg
& TIOCM_CAR
)
1408 *targ
|= CHR_TIOCM_CAR
;
1409 if (sarg
& TIOCM_DSR
)
1410 *targ
|= CHR_TIOCM_DSR
;
1411 if (sarg
& TIOCM_RI
)
1412 *targ
|= CHR_TIOCM_RI
;
1413 if (sarg
& TIOCM_DTR
)
1414 *targ
|= CHR_TIOCM_DTR
;
1415 if (sarg
& TIOCM_RTS
)
1416 *targ
|= CHR_TIOCM_RTS
;
1419 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1421 int sarg
= *(int *)arg
;
1423 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1424 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1425 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1426 if (sarg
& CHR_TIOCM_CTS
)
1428 if (sarg
& CHR_TIOCM_CAR
)
1430 if (sarg
& CHR_TIOCM_DSR
)
1432 if (sarg
& CHR_TIOCM_RI
)
1434 if (sarg
& CHR_TIOCM_DTR
)
1436 if (sarg
& CHR_TIOCM_RTS
)
1438 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1447 static void qemu_chr_close_tty(CharDriverState
*chr
)
1449 FDCharDriver
*s
= chr
->opaque
;
1453 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1463 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1465 CharDriverState
*chr
;
1467 tty_serial_init(fd
, 115200, 'N', 8, 1);
1468 chr
= qemu_chr_open_fd(fd
, fd
);
1469 chr
->chr_ioctl
= tty_serial_ioctl
;
1470 chr
->chr_close
= qemu_chr_close_tty
;
1473 #endif /* __linux__ || __sun__ */
1475 #if defined(__linux__)
1477 #define HAVE_CHARDEV_PARPORT 1
1482 } ParallelCharDriver
;
1484 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1486 if (s
->mode
!= mode
) {
1488 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1495 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1497 ParallelCharDriver
*drv
= chr
->opaque
;
1502 case CHR_IOCTL_PP_READ_DATA
:
1503 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1505 *(uint8_t *)arg
= b
;
1507 case CHR_IOCTL_PP_WRITE_DATA
:
1508 b
= *(uint8_t *)arg
;
1509 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1512 case CHR_IOCTL_PP_READ_CONTROL
:
1513 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1515 /* Linux gives only the lowest bits, and no way to know data
1516 direction! For better compatibility set the fixed upper
1518 *(uint8_t *)arg
= b
| 0xc0;
1520 case CHR_IOCTL_PP_WRITE_CONTROL
:
1521 b
= *(uint8_t *)arg
;
1522 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1525 case CHR_IOCTL_PP_READ_STATUS
:
1526 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1528 *(uint8_t *)arg
= b
;
1530 case CHR_IOCTL_PP_DATA_DIR
:
1531 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1534 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1535 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1536 struct ParallelIOArg
*parg
= arg
;
1537 int n
= read(fd
, parg
->buffer
, parg
->count
);
1538 if (n
!= parg
->count
) {
1543 case CHR_IOCTL_PP_EPP_READ
:
1544 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1545 struct ParallelIOArg
*parg
= arg
;
1546 int n
= read(fd
, parg
->buffer
, parg
->count
);
1547 if (n
!= parg
->count
) {
1552 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1553 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1554 struct ParallelIOArg
*parg
= arg
;
1555 int n
= write(fd
, parg
->buffer
, parg
->count
);
1556 if (n
!= parg
->count
) {
1561 case CHR_IOCTL_PP_EPP_WRITE
:
1562 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1563 struct ParallelIOArg
*parg
= arg
;
1564 int n
= write(fd
, parg
->buffer
, parg
->count
);
1565 if (n
!= parg
->count
) {
1576 static void pp_close(CharDriverState
*chr
)
1578 ParallelCharDriver
*drv
= chr
->opaque
;
1581 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1582 ioctl(fd
, PPRELEASE
);
1585 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1588 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1590 CharDriverState
*chr
;
1591 ParallelCharDriver
*drv
;
1593 if (ioctl(fd
, PPCLAIM
) < 0) {
1598 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1600 drv
->mode
= IEEE1284_MODE_COMPAT
;
1602 chr
= g_malloc0(sizeof(CharDriverState
));
1603 chr
->chr_write
= null_chr_write
;
1604 chr
->chr_ioctl
= pp_ioctl
;
1605 chr
->chr_close
= pp_close
;
1608 qemu_chr_be_generic_open(chr
);
1612 #endif /* __linux__ */
1614 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1616 #define HAVE_CHARDEV_PARPORT 1
1618 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1620 int fd
= (int)(intptr_t)chr
->opaque
;
1624 case CHR_IOCTL_PP_READ_DATA
:
1625 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1627 *(uint8_t *)arg
= b
;
1629 case CHR_IOCTL_PP_WRITE_DATA
:
1630 b
= *(uint8_t *)arg
;
1631 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1634 case CHR_IOCTL_PP_READ_CONTROL
:
1635 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1637 *(uint8_t *)arg
= b
;
1639 case CHR_IOCTL_PP_WRITE_CONTROL
:
1640 b
= *(uint8_t *)arg
;
1641 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1644 case CHR_IOCTL_PP_READ_STATUS
:
1645 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1647 *(uint8_t *)arg
= b
;
1655 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1657 CharDriverState
*chr
;
1659 chr
= g_malloc0(sizeof(CharDriverState
));
1660 chr
->opaque
= (void *)(intptr_t)fd
;
1661 chr
->chr_write
= null_chr_write
;
1662 chr
->chr_ioctl
= pp_ioctl
;
1671 HANDLE hcom
, hrecv
, hsend
;
1672 OVERLAPPED orecv
, osend
;
1679 HANDLE hInputReadyEvent
;
1680 HANDLE hInputDoneEvent
;
1681 HANDLE hInputThread
;
1682 uint8_t win_stdio_buf
;
1683 } WinStdioCharState
;
1685 #define NSENDBUF 2048
1686 #define NRECVBUF 2048
1687 #define MAXCONNECT 1
1688 #define NTIMEOUT 5000
1690 static int win_chr_poll(void *opaque
);
1691 static int win_chr_pipe_poll(void *opaque
);
1693 static void win_chr_close(CharDriverState
*chr
)
1695 WinCharState
*s
= chr
->opaque
;
1698 CloseHandle(s
->hsend
);
1702 CloseHandle(s
->hrecv
);
1706 CloseHandle(s
->hcom
);
1710 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1712 qemu_del_polling_cb(win_chr_poll
, chr
);
1714 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1717 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1719 WinCharState
*s
= chr
->opaque
;
1721 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1726 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1728 fprintf(stderr
, "Failed CreateEvent\n");
1731 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1733 fprintf(stderr
, "Failed CreateEvent\n");
1737 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1738 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1739 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1740 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1745 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1746 fprintf(stderr
, "Failed SetupComm\n");
1750 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1751 size
= sizeof(COMMCONFIG
);
1752 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1753 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1754 CommConfigDialog(filename
, NULL
, &comcfg
);
1756 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1757 fprintf(stderr
, "Failed SetCommState\n");
1761 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1762 fprintf(stderr
, "Failed SetCommMask\n");
1766 cto
.ReadIntervalTimeout
= MAXDWORD
;
1767 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1768 fprintf(stderr
, "Failed SetCommTimeouts\n");
1772 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1773 fprintf(stderr
, "Failed ClearCommError\n");
1776 qemu_add_polling_cb(win_chr_poll
, chr
);
1784 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1786 WinCharState
*s
= chr
->opaque
;
1787 DWORD len
, ret
, size
, err
;
1790 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1791 s
->osend
.hEvent
= s
->hsend
;
1794 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1796 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1798 err
= GetLastError();
1799 if (err
== ERROR_IO_PENDING
) {
1800 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1818 static int win_chr_read_poll(CharDriverState
*chr
)
1820 WinCharState
*s
= chr
->opaque
;
1822 s
->max_size
= qemu_chr_be_can_write(chr
);
1826 static void win_chr_readfile(CharDriverState
*chr
)
1828 WinCharState
*s
= chr
->opaque
;
1830 uint8_t buf
[READ_BUF_LEN
];
1833 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1834 s
->orecv
.hEvent
= s
->hrecv
;
1835 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1837 err
= GetLastError();
1838 if (err
== ERROR_IO_PENDING
) {
1839 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1844 qemu_chr_be_write(chr
, buf
, size
);
1848 static void win_chr_read(CharDriverState
*chr
)
1850 WinCharState
*s
= chr
->opaque
;
1852 if (s
->len
> s
->max_size
)
1853 s
->len
= s
->max_size
;
1857 win_chr_readfile(chr
);
1860 static int win_chr_poll(void *opaque
)
1862 CharDriverState
*chr
= opaque
;
1863 WinCharState
*s
= chr
->opaque
;
1867 ClearCommError(s
->hcom
, &comerr
, &status
);
1868 if (status
.cbInQue
> 0) {
1869 s
->len
= status
.cbInQue
;
1870 win_chr_read_poll(chr
);
1877 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1879 CharDriverState
*chr
;
1882 chr
= g_malloc0(sizeof(CharDriverState
));
1883 s
= g_malloc0(sizeof(WinCharState
));
1885 chr
->chr_write
= win_chr_write
;
1886 chr
->chr_close
= win_chr_close
;
1888 if (win_chr_init(chr
, filename
) < 0) {
1893 qemu_chr_be_generic_open(chr
);
1897 static int win_chr_pipe_poll(void *opaque
)
1899 CharDriverState
*chr
= opaque
;
1900 WinCharState
*s
= chr
->opaque
;
1903 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1906 win_chr_read_poll(chr
);
1913 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1915 WinCharState
*s
= chr
->opaque
;
1923 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1925 fprintf(stderr
, "Failed CreateEvent\n");
1928 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1930 fprintf(stderr
, "Failed CreateEvent\n");
1934 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1935 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1936 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1938 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1939 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1940 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1945 ZeroMemory(&ov
, sizeof(ov
));
1946 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1947 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1949 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1953 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1955 fprintf(stderr
, "Failed GetOverlappedResult\n");
1957 CloseHandle(ov
.hEvent
);
1964 CloseHandle(ov
.hEvent
);
1967 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1976 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1978 const char *filename
= opts
->device
;
1979 CharDriverState
*chr
;
1982 chr
= g_malloc0(sizeof(CharDriverState
));
1983 s
= g_malloc0(sizeof(WinCharState
));
1985 chr
->chr_write
= win_chr_write
;
1986 chr
->chr_close
= win_chr_close
;
1988 if (win_chr_pipe_init(chr
, filename
) < 0) {
1993 qemu_chr_be_generic_open(chr
);
1997 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1999 CharDriverState
*chr
;
2002 chr
= g_malloc0(sizeof(CharDriverState
));
2003 s
= g_malloc0(sizeof(WinCharState
));
2006 chr
->chr_write
= win_chr_write
;
2007 qemu_chr_be_generic_open(chr
);
2011 static CharDriverState
*qemu_chr_open_win_con(void)
2013 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2016 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2018 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2025 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2035 static void win_stdio_wait_func(void *opaque
)
2037 CharDriverState
*chr
= opaque
;
2038 WinStdioCharState
*stdio
= chr
->opaque
;
2039 INPUT_RECORD buf
[4];
2044 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
2048 /* Avoid error storm */
2049 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2053 for (i
= 0; i
< dwSize
; i
++) {
2054 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2056 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2058 if (kev
->uChar
.AsciiChar
!= 0) {
2059 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2060 if (qemu_chr_be_can_write(chr
)) {
2061 uint8_t c
= kev
->uChar
.AsciiChar
;
2062 qemu_chr_be_write(chr
, &c
, 1);
2070 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2072 CharDriverState
*chr
= param
;
2073 WinStdioCharState
*stdio
= chr
->opaque
;
2079 /* Wait for one byte */
2080 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2082 /* Exit in case of error, continue if nothing read */
2090 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2091 if (stdio
->win_stdio_buf
== '\r') {
2095 /* Signal the main thread and wait until the byte was eaten */
2096 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2099 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2105 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2109 static void win_stdio_thread_wait_func(void *opaque
)
2111 CharDriverState
*chr
= opaque
;
2112 WinStdioCharState
*stdio
= chr
->opaque
;
2114 if (qemu_chr_be_can_write(chr
)) {
2115 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2118 SetEvent(stdio
->hInputDoneEvent
);
2121 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2123 WinStdioCharState
*stdio
= chr
->opaque
;
2126 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2129 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2131 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2135 static void win_stdio_close(CharDriverState
*chr
)
2137 WinStdioCharState
*stdio
= chr
->opaque
;
2139 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2140 CloseHandle(stdio
->hInputReadyEvent
);
2142 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2143 CloseHandle(stdio
->hInputDoneEvent
);
2145 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2146 TerminateThread(stdio
->hInputThread
, 0);
2149 g_free(chr
->opaque
);
2153 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2155 CharDriverState
*chr
;
2156 WinStdioCharState
*stdio
;
2160 chr
= g_malloc0(sizeof(CharDriverState
));
2161 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2163 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2164 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2165 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2169 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2171 chr
->opaque
= stdio
;
2172 chr
->chr_write
= win_stdio_write
;
2173 chr
->chr_close
= win_stdio_close
;
2176 if (qemu_add_wait_object(stdio
->hStdIn
,
2177 win_stdio_wait_func
, chr
)) {
2178 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2183 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2184 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2185 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2188 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2189 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2190 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2191 fprintf(stderr
, "cannot create stdio thread or event\n");
2194 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2195 win_stdio_thread_wait_func
, chr
)) {
2196 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2200 dwMode
|= ENABLE_LINE_INPUT
;
2203 /* set the terminal in raw mode */
2204 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2205 dwMode
|= ENABLE_PROCESSED_INPUT
;
2208 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2210 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2211 qemu_chr_fe_set_echo(chr
, false);
2215 #endif /* !_WIN32 */
2218 /***********************************************************/
2219 /* UDP Net console */
2225 uint8_t buf
[READ_BUF_LEN
];
2231 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2233 NetCharDriver
*s
= chr
->opaque
;
2234 gsize bytes_written
;
2237 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2238 if (status
== G_IO_STATUS_EOF
) {
2240 } else if (status
!= G_IO_STATUS_NORMAL
) {
2244 return bytes_written
;
2247 static int udp_chr_read_poll(void *opaque
)
2249 CharDriverState
*chr
= opaque
;
2250 NetCharDriver
*s
= chr
->opaque
;
2252 s
->max_size
= qemu_chr_be_can_write(chr
);
2254 /* If there were any stray characters in the queue process them
2257 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2258 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2260 s
->max_size
= qemu_chr_be_can_write(chr
);
2265 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2267 CharDriverState
*chr
= opaque
;
2268 NetCharDriver
*s
= chr
->opaque
;
2269 gsize bytes_read
= 0;
2272 if (s
->max_size
== 0) {
2275 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2277 s
->bufcnt
= bytes_read
;
2278 s
->bufptr
= s
->bufcnt
;
2279 if (status
!= G_IO_STATUS_NORMAL
) {
2281 io_remove_watch_poll(s
->tag
);
2288 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2289 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2291 s
->max_size
= qemu_chr_be_can_write(chr
);
2297 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2299 NetCharDriver
*s
= chr
->opaque
;
2302 io_remove_watch_poll(s
->tag
);
2307 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2311 static void udp_chr_close(CharDriverState
*chr
)
2313 NetCharDriver
*s
= chr
->opaque
;
2315 io_remove_watch_poll(s
->tag
);
2319 g_io_channel_unref(s
->chan
);
2323 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2326 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2328 CharDriverState
*chr
= NULL
;
2329 NetCharDriver
*s
= NULL
;
2331 chr
= g_malloc0(sizeof(CharDriverState
));
2332 s
= g_malloc0(sizeof(NetCharDriver
));
2335 s
->chan
= io_channel_from_socket(s
->fd
);
2339 chr
->chr_write
= udp_chr_write
;
2340 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2341 chr
->chr_close
= udp_chr_close
;
2345 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2347 Error
*local_err
= NULL
;
2350 fd
= inet_dgram_opts(opts
, &local_err
);
2354 return qemu_chr_open_udp_fd(fd
);
2357 /***********************************************************/
2358 /* TCP Net console */
2362 GIOChannel
*chan
, *listen_chan
;
2363 guint tag
, listen_tag
;
2373 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2375 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2377 TCPCharDriver
*s
= chr
->opaque
;
2379 return io_channel_send(s
->chan
, buf
, len
);
2381 /* XXX: indicate an error ? */
2386 static int tcp_chr_read_poll(void *opaque
)
2388 CharDriverState
*chr
= opaque
;
2389 TCPCharDriver
*s
= chr
->opaque
;
2392 s
->max_size
= qemu_chr_be_can_write(chr
);
2397 #define IAC_BREAK 243
2398 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2400 uint8_t *buf
, int *size
)
2402 /* Handle any telnet client's basic IAC options to satisfy char by
2403 * char mode with no echo. All IAC options will be removed from
2404 * the buf and the do_telnetopt variable will be used to track the
2405 * state of the width of the IAC information.
2407 * IAC commands come in sets of 3 bytes with the exception of the
2408 * "IAC BREAK" command and the double IAC.
2414 for (i
= 0; i
< *size
; i
++) {
2415 if (s
->do_telnetopt
> 1) {
2416 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2417 /* Double IAC means send an IAC */
2421 s
->do_telnetopt
= 1;
2423 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2424 /* Handle IAC break commands by sending a serial break */
2425 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2430 if (s
->do_telnetopt
>= 4) {
2431 s
->do_telnetopt
= 1;
2434 if ((unsigned char)buf
[i
] == IAC
) {
2435 s
->do_telnetopt
= 2;
2446 static int tcp_get_msgfd(CharDriverState
*chr
)
2448 TCPCharDriver
*s
= chr
->opaque
;
2455 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2457 TCPCharDriver
*s
= chr
->opaque
;
2458 struct cmsghdr
*cmsg
;
2460 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2463 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2464 cmsg
->cmsg_level
!= SOL_SOCKET
||
2465 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2468 fd
= *((int *)CMSG_DATA(cmsg
));
2472 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2475 #ifndef MSG_CMSG_CLOEXEC
2476 qemu_set_cloexec(fd
);
2484 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2486 TCPCharDriver
*s
= chr
->opaque
;
2487 struct msghdr msg
= { NULL
, };
2488 struct iovec iov
[1];
2490 struct cmsghdr cmsg
;
2491 char control
[CMSG_SPACE(sizeof(int))];
2496 iov
[0].iov_base
= buf
;
2497 iov
[0].iov_len
= len
;
2501 msg
.msg_control
= &msg_control
;
2502 msg
.msg_controllen
= sizeof(msg_control
);
2504 #ifdef MSG_CMSG_CLOEXEC
2505 flags
|= MSG_CMSG_CLOEXEC
;
2507 ret
= recvmsg(s
->fd
, &msg
, flags
);
2508 if (ret
> 0 && s
->is_unix
) {
2509 unix_process_msgfd(chr
, &msg
);
2515 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2517 TCPCharDriver
*s
= chr
->opaque
;
2518 return qemu_recv(s
->fd
, buf
, len
, 0);
2522 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2524 TCPCharDriver
*s
= chr
->opaque
;
2525 return g_io_create_watch(s
->chan
, cond
);
2528 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2530 CharDriverState
*chr
= opaque
;
2531 TCPCharDriver
*s
= chr
->opaque
;
2532 uint8_t buf
[READ_BUF_LEN
];
2535 if (!s
->connected
|| s
->max_size
<= 0) {
2539 if (len
> s
->max_size
)
2541 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2543 /* connection closed */
2545 if (s
->listen_chan
) {
2546 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2549 io_remove_watch_poll(s
->tag
);
2552 g_io_channel_unref(s
->chan
);
2556 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2557 } else if (size
> 0) {
2558 if (s
->do_telnetopt
)
2559 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2561 qemu_chr_be_write(chr
, buf
, size
);
2568 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2570 return qemu_chr_open_fd(eventfd
, eventfd
);
2574 static void tcp_chr_connect(void *opaque
)
2576 CharDriverState
*chr
= opaque
;
2577 TCPCharDriver
*s
= chr
->opaque
;
2581 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2583 qemu_chr_be_generic_open(chr
);
2586 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2587 static void tcp_chr_telnet_init(int fd
)
2590 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2591 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2592 send(fd
, (char *)buf
, 3, 0);
2593 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2594 send(fd
, (char *)buf
, 3, 0);
2595 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2596 send(fd
, (char *)buf
, 3, 0);
2597 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2598 send(fd
, (char *)buf
, 3, 0);
2601 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2603 TCPCharDriver
*s
= chr
->opaque
;
2607 qemu_set_nonblock(fd
);
2609 socket_set_nodelay(fd
);
2611 s
->chan
= io_channel_from_socket(fd
);
2612 if (s
->listen_tag
) {
2613 g_source_remove(s
->listen_tag
);
2616 tcp_chr_connect(chr
);
2621 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2623 CharDriverState
*chr
= opaque
;
2624 TCPCharDriver
*s
= chr
->opaque
;
2625 struct sockaddr_in saddr
;
2627 struct sockaddr_un uaddr
;
2629 struct sockaddr
*addr
;
2636 len
= sizeof(uaddr
);
2637 addr
= (struct sockaddr
*)&uaddr
;
2641 len
= sizeof(saddr
);
2642 addr
= (struct sockaddr
*)&saddr
;
2644 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2645 if (fd
< 0 && errno
!= EINTR
) {
2648 } else if (fd
>= 0) {
2649 if (s
->do_telnetopt
)
2650 tcp_chr_telnet_init(fd
);
2654 if (tcp_chr_add_client(chr
, fd
) < 0)
2660 static void tcp_chr_close(CharDriverState
*chr
)
2662 TCPCharDriver
*s
= chr
->opaque
;
2665 io_remove_watch_poll(s
->tag
);
2669 g_io_channel_unref(s
->chan
);
2673 if (s
->listen_fd
>= 0) {
2674 if (s
->listen_tag
) {
2675 g_source_remove(s
->listen_tag
);
2678 if (s
->listen_chan
) {
2679 g_io_channel_unref(s
->listen_chan
);
2681 closesocket(s
->listen_fd
);
2684 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2687 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2688 bool is_listen
, bool is_telnet
,
2689 bool is_waitconnect
,
2692 CharDriverState
*chr
= NULL
;
2693 TCPCharDriver
*s
= NULL
;
2694 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2695 const char *left
= "", *right
= "";
2696 struct sockaddr_storage ss
;
2697 socklen_t ss_len
= sizeof(ss
);
2699 memset(&ss
, 0, ss_len
);
2700 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2701 error_setg(errp
, "getsockname: %s", strerror(errno
));
2705 chr
= g_malloc0(sizeof(CharDriverState
));
2706 s
= g_malloc0(sizeof(TCPCharDriver
));
2713 chr
->filename
= g_malloc(256);
2714 switch (ss
.ss_family
) {
2718 snprintf(chr
->filename
, 256, "unix:%s%s",
2719 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2720 is_listen
? ",server" : "");
2728 s
->do_nodelay
= do_nodelay
;
2729 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2730 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2731 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2732 is_telnet
? "telnet" : "tcp",
2733 left
, host
, right
, serv
,
2734 is_listen
? ",server" : "");
2739 chr
->chr_write
= tcp_chr_write
;
2740 chr
->chr_close
= tcp_chr_close
;
2741 chr
->get_msgfd
= tcp_get_msgfd
;
2742 chr
->chr_add_client
= tcp_chr_add_client
;
2743 chr
->chr_add_watch
= tcp_chr_add_watch
;
2747 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2748 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2750 s
->do_telnetopt
= 1;
2755 socket_set_nodelay(fd
);
2756 s
->chan
= io_channel_from_socket(s
->fd
);
2757 tcp_chr_connect(chr
);
2760 if (is_listen
&& is_waitconnect
) {
2761 printf("QEMU waiting for connection on: %s\n",
2763 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2764 qemu_set_nonblock(s
->listen_fd
);
2769 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2771 CharDriverState
*chr
= NULL
;
2772 Error
*local_err
= NULL
;
2780 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2781 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2782 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2783 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2784 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2790 fd
= unix_listen_opts(opts
, &local_err
);
2792 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2796 fd
= inet_listen_opts(opts
, 0, &local_err
);
2798 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2805 if (!is_waitconnect
)
2806 qemu_set_nonblock(fd
);
2808 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2809 is_waitconnect
, &local_err
);
2810 if (error_is_set(&local_err
)) {
2818 qerror_report_err(local_err
);
2819 error_free(local_err
);
2825 g_free(chr
->opaque
);
2831 /*********************************************************/
2832 /* Ring buffer chardev */
2839 } RingBufCharDriver
;
2841 static size_t ringbuf_count(const CharDriverState
*chr
)
2843 const RingBufCharDriver
*d
= chr
->opaque
;
2845 return d
->prod
- d
->cons
;
2848 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2850 RingBufCharDriver
*d
= chr
->opaque
;
2853 if (!buf
|| (len
< 0)) {
2857 for (i
= 0; i
< len
; i
++ ) {
2858 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2859 if (d
->prod
- d
->cons
> d
->size
) {
2860 d
->cons
= d
->prod
- d
->size
;
2867 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2869 RingBufCharDriver
*d
= chr
->opaque
;
2872 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2873 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2879 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2881 RingBufCharDriver
*d
= chr
->opaque
;
2888 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2891 CharDriverState
*chr
;
2892 RingBufCharDriver
*d
;
2894 chr
= g_malloc0(sizeof(CharDriverState
));
2895 d
= g_malloc(sizeof(*d
));
2897 d
->size
= opts
->has_size
? opts
->size
: 65536;
2899 /* The size must be power of 2 */
2900 if (d
->size
& (d
->size
- 1)) {
2901 error_setg(errp
, "size of ringbuf chardev must be power of two");
2907 d
->cbuf
= g_malloc0(d
->size
);
2910 chr
->chr_write
= ringbuf_chr_write
;
2911 chr
->chr_close
= ringbuf_chr_close
;
2921 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2923 return chr
->chr_write
== ringbuf_chr_write
;
2926 void qmp_ringbuf_write(const char *device
, const char *data
,
2927 bool has_format
, enum DataFormat format
,
2930 CharDriverState
*chr
;
2931 const uint8_t *write_data
;
2935 chr
= qemu_chr_find(device
);
2937 error_setg(errp
, "Device '%s' not found", device
);
2941 if (!chr_is_ringbuf(chr
)) {
2942 error_setg(errp
,"%s is not a ringbuf device", device
);
2946 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2947 write_data
= g_base64_decode(data
, &write_count
);
2949 write_data
= (uint8_t *)data
;
2950 write_count
= strlen(data
);
2953 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2955 if (write_data
!= (uint8_t *)data
) {
2956 g_free((void *)write_data
);
2960 error_setg(errp
, "Failed to write to device %s", device
);
2965 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2966 bool has_format
, enum DataFormat format
,
2969 CharDriverState
*chr
;
2974 chr
= qemu_chr_find(device
);
2976 error_setg(errp
, "Device '%s' not found", device
);
2980 if (!chr_is_ringbuf(chr
)) {
2981 error_setg(errp
,"%s is not a ringbuf device", device
);
2986 error_setg(errp
, "size must be greater than zero");
2990 count
= ringbuf_count(chr
);
2991 size
= size
> count
? count
: size
;
2992 read_data
= g_malloc(size
+ 1);
2994 ringbuf_chr_read(chr
, read_data
, size
);
2996 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2997 data
= g_base64_encode(read_data
, size
);
3001 * FIXME should read only complete, valid UTF-8 characters up
3002 * to @size bytes. Invalid sequences should be replaced by a
3003 * suitable replacement character. Except when (and only
3004 * when) ring buffer lost characters since last read, initial
3005 * continuation characters should be dropped.
3007 read_data
[size
] = 0;
3008 data
= (char *)read_data
;
3014 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3016 char host
[65], port
[33], width
[8], height
[8];
3020 Error
*local_err
= NULL
;
3022 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3023 if (error_is_set(&local_err
)) {
3024 qerror_report_err(local_err
);
3025 error_free(local_err
);
3029 if (strstart(filename
, "mon:", &p
)) {
3031 qemu_opt_set(opts
, "mux", "on");
3034 if (strcmp(filename
, "null") == 0 ||
3035 strcmp(filename
, "pty") == 0 ||
3036 strcmp(filename
, "msmouse") == 0 ||
3037 strcmp(filename
, "braille") == 0 ||
3038 strcmp(filename
, "stdio") == 0) {
3039 qemu_opt_set(opts
, "backend", filename
);
3042 if (strstart(filename
, "vc", &p
)) {
3043 qemu_opt_set(opts
, "backend", "vc");
3045 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
3047 qemu_opt_set(opts
, "width", width
);
3048 qemu_opt_set(opts
, "height", height
);
3049 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3051 qemu_opt_set(opts
, "cols", width
);
3052 qemu_opt_set(opts
, "rows", height
);
3059 if (strcmp(filename
, "con:") == 0) {
3060 qemu_opt_set(opts
, "backend", "console");
3063 if (strstart(filename
, "COM", NULL
)) {
3064 qemu_opt_set(opts
, "backend", "serial");
3065 qemu_opt_set(opts
, "path", filename
);
3068 if (strstart(filename
, "file:", &p
)) {
3069 qemu_opt_set(opts
, "backend", "file");
3070 qemu_opt_set(opts
, "path", p
);
3073 if (strstart(filename
, "pipe:", &p
)) {
3074 qemu_opt_set(opts
, "backend", "pipe");
3075 qemu_opt_set(opts
, "path", p
);
3078 if (strstart(filename
, "tcp:", &p
) ||
3079 strstart(filename
, "telnet:", &p
)) {
3080 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3082 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3085 qemu_opt_set(opts
, "backend", "socket");
3086 qemu_opt_set(opts
, "host", host
);
3087 qemu_opt_set(opts
, "port", port
);
3088 if (p
[pos
] == ',') {
3089 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3092 if (strstart(filename
, "telnet:", &p
))
3093 qemu_opt_set(opts
, "telnet", "on");
3096 if (strstart(filename
, "udp:", &p
)) {
3097 qemu_opt_set(opts
, "backend", "udp");
3098 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3100 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3104 qemu_opt_set(opts
, "host", host
);
3105 qemu_opt_set(opts
, "port", port
);
3106 if (p
[pos
] == '@') {
3108 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3110 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3114 qemu_opt_set(opts
, "localaddr", host
);
3115 qemu_opt_set(opts
, "localport", port
);
3119 if (strstart(filename
, "unix:", &p
)) {
3120 qemu_opt_set(opts
, "backend", "socket");
3121 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3125 if (strstart(filename
, "/dev/parport", NULL
) ||
3126 strstart(filename
, "/dev/ppi", NULL
)) {
3127 qemu_opt_set(opts
, "backend", "parport");
3128 qemu_opt_set(opts
, "path", filename
);
3131 if (strstart(filename
, "/dev/", NULL
)) {
3132 qemu_opt_set(opts
, "backend", "tty");
3133 qemu_opt_set(opts
, "path", filename
);
3138 qemu_opts_del(opts
);
3142 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3145 const char *path
= qemu_opt_get(opts
, "path");
3148 error_setg(errp
, "chardev: file: no filename given");
3151 backend
->file
= g_new0(ChardevFile
, 1);
3152 backend
->file
->out
= g_strdup(path
);
3155 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3158 backend
->stdio
= g_new0(ChardevStdio
, 1);
3159 backend
->stdio
->has_signal
= true;
3160 backend
->stdio
->signal
=
3161 qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
);
3164 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3167 const char *device
= qemu_opt_get(opts
, "path");
3169 if (device
== NULL
) {
3170 error_setg(errp
, "chardev: serial/tty: no device path given");
3173 backend
->serial
= g_new0(ChardevHostdev
, 1);
3174 backend
->serial
->device
= g_strdup(device
);
3177 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3180 const char *device
= qemu_opt_get(opts
, "path");
3182 if (device
== NULL
) {
3183 error_setg(errp
, "chardev: parallel: no device path given");
3186 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3187 backend
->parallel
->device
= g_strdup(device
);
3190 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3193 const char *device
= qemu_opt_get(opts
, "path");
3195 if (device
== NULL
) {
3196 error_setg(errp
, "chardev: pipe: no device path given");
3199 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3200 backend
->pipe
->device
= g_strdup(device
);
3203 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3208 backend
->memory
= g_new0(ChardevRingbuf
, 1);
3210 val
= qemu_opt_get_number(opts
, "size", 0);
3212 backend
->memory
->has_size
= true;
3213 backend
->memory
->size
= val
;
3217 typedef struct CharDriver
{
3220 CharDriverState
*(*open
)(QemuOpts
*opts
);
3221 /* new, qapi-based */
3223 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3226 static GSList
*backends
;
3228 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3232 s
= g_malloc0(sizeof(*s
));
3233 s
->name
= g_strdup(name
);
3236 backends
= g_slist_append(backends
, s
);
3239 void register_char_driver_qapi(const char *name
, int kind
,
3240 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3244 s
= g_malloc0(sizeof(*s
));
3245 s
->name
= g_strdup(name
);
3249 backends
= g_slist_append(backends
, s
);
3252 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3253 void (*init
)(struct CharDriverState
*s
),
3257 CharDriverState
*chr
;
3260 if (qemu_opts_id(opts
) == NULL
) {
3261 error_setg(errp
, "chardev: no id specified");
3265 if (qemu_opt_get(opts
, "backend") == NULL
) {
3266 error_setg(errp
, "chardev: \"%s\" missing backend",
3267 qemu_opts_id(opts
));
3270 for (i
= backends
; i
; i
= i
->next
) {
3273 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3278 error_setg(errp
, "chardev: backend \"%s\" not found",
3279 qemu_opt_get(opts
, "backend"));
3284 /* using new, qapi init */
3285 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3286 ChardevReturn
*ret
= NULL
;
3287 const char *id
= qemu_opts_id(opts
);
3288 const char *bid
= NULL
;
3290 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3291 bid
= g_strdup_printf("%s-base", id
);
3295 backend
->kind
= cd
->kind
;
3297 cd
->parse(opts
, backend
, errp
);
3298 if (error_is_set(errp
)) {
3302 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3303 if (error_is_set(errp
)) {
3308 qapi_free_ChardevBackend(backend
);
3309 qapi_free_ChardevReturn(ret
);
3310 backend
= g_new0(ChardevBackend
, 1);
3311 backend
->mux
= g_new0(ChardevMux
, 1);
3312 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3313 backend
->mux
->chardev
= g_strdup(bid
);
3314 ret
= qmp_chardev_add(id
, backend
, errp
);
3315 if (error_is_set(errp
)) {
3320 chr
= qemu_chr_find(id
);
3323 qapi_free_ChardevBackend(backend
);
3324 qapi_free_ChardevReturn(ret
);
3328 chr
= cd
->open(opts
);
3330 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3331 qemu_opt_get(opts
, "backend"));
3336 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3338 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3340 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3341 CharDriverState
*base
= chr
;
3342 int len
= strlen(qemu_opts_id(opts
)) + 6;
3343 base
->label
= g_malloc(len
);
3344 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3345 chr
= qemu_chr_open_mux(base
);
3346 chr
->filename
= base
->filename
;
3347 chr
->avail_connections
= MAX_MUX
;
3348 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3350 chr
->avail_connections
= 1;
3352 chr
->label
= g_strdup(qemu_opts_id(opts
));
3357 qemu_opts_del(opts
);
3361 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3364 CharDriverState
*chr
;
3368 if (strstart(filename
, "chardev:", &p
)) {
3369 return qemu_chr_find(p
);
3372 opts
= qemu_chr_parse_compat(label
, filename
);
3376 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3377 if (error_is_set(&err
)) {
3378 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3381 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3382 qemu_chr_fe_claim_no_fail(chr
);
3383 monitor_init(chr
, MONITOR_USE_READLINE
);
3388 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3390 if (chr
->chr_set_echo
) {
3391 chr
->chr_set_echo(chr
, echo
);
3395 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3397 if (chr
->fe_open
== fe_open
) {
3400 chr
->fe_open
= fe_open
;
3401 if (chr
->chr_set_fe_open
) {
3402 chr
->chr_set_fe_open(chr
, fe_open
);
3406 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3407 GIOFunc func
, void *user_data
)
3412 if (s
->chr_add_watch
== NULL
) {
3416 src
= s
->chr_add_watch(s
, cond
);
3417 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3418 tag
= g_source_attach(src
, NULL
);
3419 g_source_unref(src
);
3424 int qemu_chr_fe_claim(CharDriverState
*s
)
3426 if (s
->avail_connections
< 1) {
3429 s
->avail_connections
--;
3433 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3435 if (qemu_chr_fe_claim(s
) != 0) {
3436 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3437 __func__
, s
->label
);
3442 void qemu_chr_fe_release(CharDriverState
*s
)
3444 s
->avail_connections
++;
3447 void qemu_chr_delete(CharDriverState
*chr
)
3449 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3450 if (chr
->chr_close
) {
3451 chr
->chr_close(chr
);
3453 g_free(chr
->filename
);
3456 qemu_opts_del(chr
->opts
);
3461 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3463 ChardevInfoList
*chr_list
= NULL
;
3464 CharDriverState
*chr
;
3466 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3467 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3468 info
->value
= g_malloc0(sizeof(*info
->value
));
3469 info
->value
->label
= g_strdup(chr
->label
);
3470 info
->value
->filename
= g_strdup(chr
->filename
);
3472 info
->next
= chr_list
;
3479 CharDriverState
*qemu_chr_find(const char *name
)
3481 CharDriverState
*chr
;
3483 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3484 if (strcmp(chr
->label
, name
) != 0)
3491 /* Get a character (serial) device interface. */
3492 CharDriverState
*qemu_char_get_next_serial(void)
3494 static int next_serial
;
3495 CharDriverState
*chr
;
3497 /* FIXME: This function needs to go away: use chardev properties! */
3499 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3500 chr
= serial_hds
[next_serial
++];
3501 qemu_chr_fe_claim_no_fail(chr
);
3507 QemuOptsList qemu_chardev_opts
= {
3509 .implied_opt_name
= "backend",
3510 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3514 .type
= QEMU_OPT_STRING
,
3517 .type
= QEMU_OPT_STRING
,
3520 .type
= QEMU_OPT_STRING
,
3523 .type
= QEMU_OPT_STRING
,
3525 .name
= "localaddr",
3526 .type
= QEMU_OPT_STRING
,
3528 .name
= "localport",
3529 .type
= QEMU_OPT_STRING
,
3532 .type
= QEMU_OPT_NUMBER
,
3535 .type
= QEMU_OPT_BOOL
,
3538 .type
= QEMU_OPT_BOOL
,
3541 .type
= QEMU_OPT_BOOL
,
3544 .type
= QEMU_OPT_BOOL
,
3547 .type
= QEMU_OPT_BOOL
,
3550 .type
= QEMU_OPT_BOOL
,
3553 .type
= QEMU_OPT_NUMBER
,
3556 .type
= QEMU_OPT_NUMBER
,
3559 .type
= QEMU_OPT_NUMBER
,
3562 .type
= QEMU_OPT_NUMBER
,
3565 .type
= QEMU_OPT_BOOL
,
3568 .type
= QEMU_OPT_BOOL
,
3571 .type
= QEMU_OPT_STRING
,
3574 .type
= QEMU_OPT_NUMBER
,
3577 .type
= QEMU_OPT_SIZE
,
3579 { /* end of list */ }
3585 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3590 error_setg(errp
, "input file not supported");
3594 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3595 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3596 if (out
== INVALID_HANDLE_VALUE
) {
3597 error_setg(errp
, "open %s failed", file
->out
);
3600 return qemu_chr_open_win_file(out
);
3603 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3606 return qemu_chr_open_win_path(serial
->device
);
3609 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3612 error_setg(errp
, "character device backend type 'parallel' not supported");
3618 static int qmp_chardev_open_file_source(char *src
, int flags
,
3623 TFR(fd
= qemu_open(src
, flags
, 0666));
3625 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3630 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3632 int flags
, in
= -1, out
= -1;
3634 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3635 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3636 if (error_is_set(errp
)) {
3642 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3643 if (error_is_set(errp
)) {
3649 return qemu_chr_open_fd(in
, out
);
3652 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3655 #ifdef HAVE_CHARDEV_TTY
3658 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3659 if (error_is_set(errp
)) {
3662 qemu_set_nonblock(fd
);
3663 return qemu_chr_open_tty_fd(fd
);
3665 error_setg(errp
, "character device backend type 'serial' not supported");
3670 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3673 #ifdef HAVE_CHARDEV_PARPORT
3676 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3677 if (error_is_set(errp
)) {
3680 return qemu_chr_open_pp_fd(fd
);
3682 error_setg(errp
, "character device backend type 'parallel' not supported");
3689 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3692 SocketAddress
*addr
= sock
->addr
;
3693 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3694 bool is_listen
= sock
->has_server
? sock
->server
: true;
3695 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3696 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3700 fd
= socket_listen(addr
, errp
);
3702 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3704 if (error_is_set(errp
)) {
3707 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3708 is_telnet
, is_waitconnect
, errp
);
3711 static CharDriverState
*qmp_chardev_open_dgram(ChardevDgram
*dgram
,
3716 fd
= socket_dgram(dgram
->remote
, dgram
->local
, errp
);
3717 if (error_is_set(errp
)) {
3720 return qemu_chr_open_udp_fd(fd
);
3723 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3726 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3727 CharDriverState
*base
, *chr
= NULL
;
3729 chr
= qemu_chr_find(id
);
3731 error_setg(errp
, "Chardev '%s' already exists", id
);
3736 switch (backend
->kind
) {
3737 case CHARDEV_BACKEND_KIND_FILE
:
3738 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3740 case CHARDEV_BACKEND_KIND_SERIAL
:
3741 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3743 case CHARDEV_BACKEND_KIND_PARALLEL
:
3744 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3746 case CHARDEV_BACKEND_KIND_PIPE
:
3747 chr
= qemu_chr_open_pipe(backend
->pipe
);
3749 case CHARDEV_BACKEND_KIND_SOCKET
:
3750 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3752 case CHARDEV_BACKEND_KIND_DGRAM
:
3753 chr
= qmp_chardev_open_dgram(backend
->dgram
, errp
);
3755 #ifdef HAVE_CHARDEV_TTY
3756 case CHARDEV_BACKEND_KIND_PTY
:
3757 chr
= qemu_chr_open_pty(id
, ret
);
3760 case CHARDEV_BACKEND_KIND_NULL
:
3761 chr
= qemu_chr_open_null();
3763 case CHARDEV_BACKEND_KIND_MUX
:
3764 base
= qemu_chr_find(backend
->mux
->chardev
);
3766 error_setg(errp
, "mux: base chardev %s not found",
3767 backend
->mux
->chardev
);
3770 chr
= qemu_chr_open_mux(base
);
3772 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3773 chr
= qemu_chr_open_msmouse();
3775 #ifdef CONFIG_BRLAPI
3776 case CHARDEV_BACKEND_KIND_BRAILLE
:
3777 chr
= chr_baum_init();
3780 case CHARDEV_BACKEND_KIND_STDIO
:
3781 chr
= qemu_chr_open_stdio(backend
->stdio
);
3784 case CHARDEV_BACKEND_KIND_CONSOLE
:
3785 chr
= qemu_chr_open_win_con();
3789 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3790 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3792 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3793 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3796 case CHARDEV_BACKEND_KIND_VC
:
3797 chr
= vc_init(backend
->vc
);
3799 case CHARDEV_BACKEND_KIND_MEMORY
:
3800 chr
= qemu_chr_open_ringbuf(backend
->memory
, errp
);
3803 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3807 if (chr
== NULL
&& !error_is_set(errp
)) {
3808 error_setg(errp
, "Failed to create chardev");
3811 chr
->label
= g_strdup(id
);
3812 chr
->avail_connections
=
3813 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3814 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3822 void qmp_chardev_remove(const char *id
, Error
**errp
)
3824 CharDriverState
*chr
;
3826 chr
= qemu_chr_find(id
);
3828 error_setg(errp
, "Chardev '%s' not found", id
);
3831 if (chr
->chr_can_read
|| chr
->chr_read
||
3832 chr
->chr_event
|| chr
->handler_opaque
) {
3833 error_setg(errp
, "Chardev '%s' is busy", id
);
3836 qemu_chr_delete(chr
);
3839 static void register_types(void)
3841 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3842 register_char_driver("socket", qemu_chr_open_socket
);
3843 register_char_driver("udp", qemu_chr_open_udp
);
3844 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3845 qemu_chr_parse_ringbuf
);
3846 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3847 qemu_chr_parse_file_out
);
3848 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3849 qemu_chr_parse_stdio
);
3850 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3851 qemu_chr_parse_serial
);
3852 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3853 qemu_chr_parse_serial
);
3854 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3855 qemu_chr_parse_parallel
);
3856 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3857 qemu_chr_parse_parallel
);
3858 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3859 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3860 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3861 qemu_chr_parse_pipe
);
3864 type_init(register_types
);